usbd_transc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*!
  2. \file usbd_transc.c
  3. \brief USBD transaction function
  4. \version 2023-06-30, V2.1.6, firmware for GD32F30x
  5. */
  6. /*
  7. Copyright (c) 2023, GigaDevice Semiconductor Inc.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. 3. Neither the name of the copyright holder nor the names of its contributors
  16. may be used to endorse or promote products derived from this software without
  17. specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. OF SUCH DAMAGE.
  28. */
  29. #include "usbd_enum.h"
  30. #include "usbd_transc.h"
  31. /* local function prototypes ('static') */
  32. static inline void usb_stall_transc (usb_dev *udev);
  33. static inline void usb_ctl_data_in(usb_dev *udev);
  34. static inline void usb_ctl_status_in (usb_dev *udev);
  35. static inline void usb_ctl_data_out (usb_dev *udev);
  36. static inline void usb_ctl_status_out (usb_dev *udev);
  37. static inline void usb_0len_packet_send (usb_dev *udev);
  38. /*!
  39. \brief USB setup stage processing
  40. \param[in] udev: pointer to USB device instance
  41. \param[out] none
  42. \retval none
  43. */
  44. void _usb_setup_transc (usb_dev *udev, uint8_t ep_num)
  45. {
  46. (void)ep_num;
  47. usb_reqsta reqstat = REQ_NOTSUPP;
  48. uint16_t count = udev->drv_handler->ep_read((uint8_t *)(&udev->control.req), 0U, (uint8_t)EP_BUF_SNG);
  49. if (count != USB_SETUP_PACKET_LEN) {
  50. usb_stall_transc(udev);
  51. return;
  52. }
  53. switch (udev->control.req.bmRequestType & USB_REQTYPE_MASK) {
  54. /* standard device request */
  55. case USB_REQTYPE_STRD:
  56. reqstat = usbd_standard_request(udev, &udev->control.req);
  57. break;
  58. /* device class request */
  59. case USB_REQTYPE_CLASS:
  60. reqstat = usbd_class_request(udev, &udev->control.req);
  61. break;
  62. /* vendor defined request */
  63. case USB_REQTYPE_VENDOR:
  64. reqstat = usbd_vendor_request(udev, &udev->control.req);
  65. break;
  66. default:
  67. break;
  68. }
  69. if (REQ_SUPP == reqstat) {
  70. if (0U == udev->control.req.wLength) {
  71. /* USB control transfer status in stage */
  72. usb_ctl_status_in(udev);
  73. } else {
  74. if (udev->control.req.bmRequestType & 0x80U) {
  75. usb_ctl_data_in(udev);
  76. } else {
  77. /* USB control transfer data out stage */
  78. usb_ctl_data_out(udev);
  79. }
  80. }
  81. } else {
  82. usb_stall_transc(udev);
  83. }
  84. }
  85. /*!
  86. \brief data out stage processing
  87. \param[in] udev: pointer to USB device instance
  88. \param[in] ep_num: endpoint identifier(0..7)
  89. \param[out] none
  90. \retval none
  91. */
  92. void _usb_out0_transc (usb_dev *udev, uint8_t ep_num)
  93. {
  94. if (((uint8_t)USBD_CONFIGURED == udev->cur_status) && (udev->class_core->ctlx_out != NULL)) {
  95. /* device class handle */
  96. (void)udev->class_core->ctlx_out(udev);
  97. }
  98. if (USBD_CTL_DATA_OUT == udev->control.ctl_state) {
  99. /* enter the control transaction status IN stage */
  100. usb_ctl_status_in(udev);
  101. } else if (USBD_CTL_STATUS_OUT == udev->control.ctl_state) {
  102. usb_transc_config(&udev->transc_out[ep_num], NULL, 0U, 0U);
  103. udev->control.ctl_state = USBD_CTL_IDLE;
  104. } else {
  105. /* no operation */
  106. }
  107. }
  108. /*!
  109. \brief data in stage processing
  110. \param[in] udev: pointer to USB device instance
  111. \param[in] ep_num: endpoint identifier(0..7)
  112. \param[out] none
  113. \retval none
  114. */
  115. void _usb_in0_transc (usb_dev *udev, uint8_t ep_num)
  116. {
  117. (void)ep_num;
  118. if (udev->control.ctl_zlp) {
  119. /* send 0 length packet */
  120. usb_0len_packet_send(udev);
  121. udev->control.ctl_zlp = 0U;
  122. }
  123. if (((uint8_t)USBD_CONFIGURED == udev->cur_status) && (udev->class_core->ctlx_in != NULL)) {
  124. (void)udev->class_core->ctlx_in(udev);
  125. }
  126. if (USBD_CTL_DATA_IN == udev->control.ctl_state) {
  127. /* USB control transfer status OUT stage */
  128. usb_ctl_status_out(udev);
  129. } else if (USBD_CTL_STATUS_IN == udev->control.ctl_state) {
  130. udev->control.ctl_state = USBD_CTL_IDLE;
  131. }
  132. if (0U != udev->dev_addr) {
  133. udev->drv_handler->set_addr(udev);
  134. udev->dev_addr = 0U;
  135. }
  136. }