usbd_control.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2019 Ha Thach (tinyusb.org)
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. * This file is part of the TinyUSB stack.
  25. */
  26. #include "tusb_option.h"
  27. #if TUSB_OPT_DEVICE_ENABLED
  28. #include "tusb.h"
  29. #include "device/usbd_pvt.h"
  30. #include "dcd.h"
  31. #if CFG_TUSB_DEBUG >= 2
  32. extern void usbd_driver_print_control_complete_name(usbd_control_xfer_cb_t callback);
  33. #endif
  34. enum
  35. {
  36. EDPT_CTRL_OUT = 0x00,
  37. EDPT_CTRL_IN = 0x80
  38. };
  39. typedef struct
  40. {
  41. tusb_control_request_t request;
  42. uint8_t* buffer;
  43. uint16_t data_len;
  44. uint16_t total_xferred;
  45. usbd_control_xfer_cb_t complete_cb;
  46. } usbd_control_xfer_t;
  47. static usbd_control_xfer_t _ctrl_xfer;
  48. CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN
  49. static uint8_t _usbd_ctrl_buf[CFG_TUD_ENDPOINT0_SIZE];
  50. //--------------------------------------------------------------------+
  51. // Application API
  52. //--------------------------------------------------------------------+
  53. // Queue ZLP status transaction
  54. static inline bool _status_stage_xact(uint8_t rhport, tusb_control_request_t const * request)
  55. {
  56. // Opposite to endpoint in Data Phase
  57. uint8_t const ep_addr = request->bmRequestType_bit.direction ? EDPT_CTRL_OUT : EDPT_CTRL_IN;
  58. return usbd_edpt_xfer(rhport, ep_addr, NULL, 0);
  59. }
  60. // Status phase
  61. bool tud_control_status(uint8_t rhport, tusb_control_request_t const * request)
  62. {
  63. _ctrl_xfer.request = (*request);
  64. _ctrl_xfer.buffer = NULL;
  65. _ctrl_xfer.total_xferred = 0;
  66. _ctrl_xfer.data_len = 0;
  67. return _status_stage_xact(rhport, request);
  68. }
  69. // Queue a transaction in Data Stage
  70. // Each transaction has up to Endpoint0's max packet size.
  71. // This function can also transfer an zero-length packet
  72. static bool _data_stage_xact(uint8_t rhport)
  73. {
  74. uint16_t const xact_len = tu_min16(_ctrl_xfer.data_len - _ctrl_xfer.total_xferred, CFG_TUD_ENDPOINT0_SIZE);
  75. uint8_t ep_addr = EDPT_CTRL_OUT;
  76. if ( _ctrl_xfer.request.bmRequestType_bit.direction == TUSB_DIR_IN )
  77. {
  78. ep_addr = EDPT_CTRL_IN;
  79. if ( xact_len ) memcpy(_usbd_ctrl_buf, _ctrl_xfer.buffer, xact_len);
  80. }
  81. return usbd_edpt_xfer(rhport, ep_addr, xact_len ? _usbd_ctrl_buf : NULL, xact_len);
  82. }
  83. // Transmit data to/from the control endpoint.
  84. // If the request's wLength is zero, a status packet is sent instead.
  85. bool tud_control_xfer(uint8_t rhport, tusb_control_request_t const * request, void* buffer, uint16_t len)
  86. {
  87. _ctrl_xfer.request = (*request);
  88. _ctrl_xfer.buffer = (uint8_t*) buffer;
  89. _ctrl_xfer.total_xferred = 0U;
  90. _ctrl_xfer.data_len = tu_min16(len, request->wLength);
  91. if (request->wLength > 0U)
  92. {
  93. if(_ctrl_xfer.data_len > 0U)
  94. {
  95. TU_ASSERT(buffer);
  96. }
  97. // TU_LOG2(" Control total data length is %u bytes\r\n", _ctrl_xfer.data_len);
  98. // Data stage
  99. TU_ASSERT( _data_stage_xact(rhport) );
  100. }
  101. else
  102. {
  103. // Status stage
  104. TU_ASSERT( _status_stage_xact(rhport, request) );
  105. }
  106. return true;
  107. }
  108. //--------------------------------------------------------------------+
  109. // USBD API
  110. //--------------------------------------------------------------------+
  111. void usbd_control_reset(void);
  112. void usbd_control_set_request(tusb_control_request_t const *request);
  113. void usbd_control_set_complete_callback( usbd_control_xfer_cb_t fp );
  114. bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
  115. void usbd_control_reset(void)
  116. {
  117. tu_varclr(&_ctrl_xfer);
  118. }
  119. // Set complete callback
  120. void usbd_control_set_complete_callback( usbd_control_xfer_cb_t fp )
  121. {
  122. _ctrl_xfer.complete_cb = fp;
  123. }
  124. // for dcd_set_address where DCD is responsible for status response
  125. void usbd_control_set_request(tusb_control_request_t const *request)
  126. {
  127. _ctrl_xfer.request = (*request);
  128. _ctrl_xfer.buffer = NULL;
  129. _ctrl_xfer.total_xferred = 0;
  130. _ctrl_xfer.data_len = 0;
  131. }
  132. // callback when a transaction complete on
  133. // - DATA stage of control endpoint or
  134. // - Status stage
  135. bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
  136. {
  137. (void) result;
  138. // Endpoint Address is opposite to direction bit, this is Status Stage complete event
  139. if ( tu_edpt_dir(ep_addr) != _ctrl_xfer.request.bmRequestType_bit.direction )
  140. {
  141. TU_ASSERT(0 == xferred_bytes);
  142. // invoke optional dcd hook if available
  143. if (dcd_edpt0_status_complete) dcd_edpt0_status_complete(rhport, &_ctrl_xfer.request);
  144. if (_ctrl_xfer.complete_cb)
  145. {
  146. // TODO refactor with usbd_driver_print_control_complete_name
  147. _ctrl_xfer.complete_cb(rhport, CONTROL_STAGE_ACK, &_ctrl_xfer.request);
  148. }
  149. return true;
  150. }
  151. if ( _ctrl_xfer.request.bmRequestType_bit.direction == TUSB_DIR_OUT )
  152. {
  153. TU_VERIFY(_ctrl_xfer.buffer);
  154. memcpy(_ctrl_xfer.buffer, _usbd_ctrl_buf, xferred_bytes);
  155. }
  156. _ctrl_xfer.total_xferred += xferred_bytes;
  157. _ctrl_xfer.buffer += xferred_bytes;
  158. // Data Stage is complete when all request's length are transferred or
  159. // a short packet is sent including zero-length packet.
  160. if ( (_ctrl_xfer.request.wLength == _ctrl_xfer.total_xferred) || (xferred_bytes < CFG_TUD_ENDPOINT0_SIZE) )
  161. {
  162. // DATA stage is complete
  163. bool is_ok = true;
  164. // invoke complete callback if set
  165. // callback can still stall control in status phase e.g out data does not make sense
  166. if ( _ctrl_xfer.complete_cb )
  167. {
  168. #if CFG_TUSB_DEBUG >= 2
  169. usbd_driver_print_control_complete_name(_ctrl_xfer.complete_cb);
  170. #endif
  171. is_ok = _ctrl_xfer.complete_cb(rhport, CONTROL_STAGE_DATA, &_ctrl_xfer.request);
  172. }
  173. if ( is_ok )
  174. {
  175. // Send status
  176. TU_ASSERT( _status_stage_xact(rhport, &_ctrl_xfer.request) );
  177. }else
  178. {
  179. // Stall both IN and OUT control endpoint
  180. dcd_edpt_stall(rhport, EDPT_CTRL_OUT);
  181. dcd_edpt_stall(rhport, EDPT_CTRL_IN);
  182. }
  183. }
  184. else
  185. {
  186. // More data to transfer
  187. TU_ASSERT( _data_stage_xact(rhport) );
  188. }
  189. return true;
  190. }
  191. #endif