usbd_control.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. #if defined(TU_HAS_NO_ATTR_WEAK)
  35. static void (*const MAKE_WEAK_FUNC(dcd_edpt0_status_complete))(uint8_t, tusb_control_request_t const *) = DCD_EDPT0_STATUS_COMPLETE;
  36. #endif
  37. enum
  38. {
  39. EDPT_CTRL_OUT = 0x00,
  40. EDPT_CTRL_IN = 0x80
  41. };
  42. typedef struct
  43. {
  44. tusb_control_request_t request;
  45. uint8_t* buffer;
  46. uint16_t data_len;
  47. uint16_t total_xferred;
  48. usbd_control_xfer_cb_t complete_cb;
  49. } usbd_control_xfer_t;
  50. static usbd_control_xfer_t _ctrl_xfer;
  51. #if defined(TU_HAS_NO_ATTR_ALIGNED)
  52. // Helper union to overcome the lack of the alignment attribute/pragma
  53. static union {
  54. uint16_t : (sizeof(uint16_t) * 8); // Alignment of at least the size of the used type
  55. uint8_t _usbd_ctrl_buf[CFG_TUD_ENDPOINT0_SIZE];
  56. } Align_usbd_ctrl_buf_;
  57. static uint8_t *_usbd_ctrl_buf = (uint8_t*)&Align_usbd_ctrl_buf_;
  58. #else
  59. CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN
  60. static uint8_t _usbd_ctrl_buf[CFG_TUD_ENDPOINT0_SIZE];
  61. #endif
  62. //--------------------------------------------------------------------+
  63. // Application API
  64. //--------------------------------------------------------------------+
  65. // Queue ZLP status transaction
  66. static inline bool _status_stage_xact(uint8_t rhport, tusb_control_request_t const * request)
  67. {
  68. // Opposite to endpoint in Data Phase
  69. uint8_t const ep_addr = request->bmRequestType_bit.direction ? EDPT_CTRL_OUT : EDPT_CTRL_IN;
  70. return usbd_edpt_xfer(rhport, ep_addr, NULL, 0);
  71. }
  72. // Status phase
  73. bool tud_control_status(uint8_t rhport, tusb_control_request_t const * request)
  74. {
  75. _ctrl_xfer.request = (*request);
  76. _ctrl_xfer.buffer = NULL;
  77. _ctrl_xfer.total_xferred = 0;
  78. _ctrl_xfer.data_len = 0;
  79. return _status_stage_xact(rhport, request);
  80. }
  81. // Queue a transaction in Data Stage
  82. // Each transaction has up to Endpoint0's max packet size.
  83. // This function can also transfer an zero-length packet
  84. static bool _data_stage_xact(uint8_t rhport)
  85. {
  86. uint16_t const xact_len = tu_min16(_ctrl_xfer.data_len - _ctrl_xfer.total_xferred, CFG_TUD_ENDPOINT0_SIZE);
  87. uint8_t ep_addr = EDPT_CTRL_OUT;
  88. if ( _ctrl_xfer.request.bmRequestType_bit.direction == TUSB_DIR_IN )
  89. {
  90. ep_addr = EDPT_CTRL_IN;
  91. if ( xact_len ) memcpy(_usbd_ctrl_buf, _ctrl_xfer.buffer, xact_len);
  92. }
  93. return usbd_edpt_xfer(rhport, ep_addr, xact_len ? _usbd_ctrl_buf : NULL, xact_len);
  94. }
  95. // Transmit data to/from the control endpoint.
  96. // If the request's wLength is zero, a status packet is sent instead.
  97. bool tud_control_xfer(uint8_t rhport, tusb_control_request_t const * request, void* buffer, uint16_t len)
  98. {
  99. _ctrl_xfer.request = (*request);
  100. _ctrl_xfer.buffer = (uint8_t*) buffer;
  101. _ctrl_xfer.total_xferred = 0U;
  102. _ctrl_xfer.data_len = tu_min16(len, request->wLength);
  103. if (request->wLength > 0U)
  104. {
  105. if(_ctrl_xfer.data_len > 0U)
  106. {
  107. TU_ASSERT(buffer);
  108. }
  109. // TU_LOG2(" Control total data length is %u bytes\r\n", _ctrl_xfer.data_len);
  110. // Data stage
  111. TU_ASSERT( _data_stage_xact(rhport) );
  112. }
  113. else
  114. {
  115. // Status stage
  116. TU_ASSERT( _status_stage_xact(rhport, request) );
  117. }
  118. return true;
  119. }
  120. //--------------------------------------------------------------------+
  121. // USBD API
  122. //--------------------------------------------------------------------+
  123. void usbd_control_reset(void);
  124. void usbd_control_set_request(tusb_control_request_t const *request);
  125. void usbd_control_set_complete_callback( usbd_control_xfer_cb_t fp );
  126. bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
  127. void usbd_control_reset(void)
  128. {
  129. tu_varclr(&_ctrl_xfer);
  130. }
  131. // Set complete callback
  132. void usbd_control_set_complete_callback( usbd_control_xfer_cb_t fp )
  133. {
  134. _ctrl_xfer.complete_cb = fp;
  135. }
  136. // for dcd_set_address where DCD is responsible for status response
  137. void usbd_control_set_request(tusb_control_request_t const *request)
  138. {
  139. _ctrl_xfer.request = (*request);
  140. _ctrl_xfer.buffer = NULL;
  141. _ctrl_xfer.total_xferred = 0;
  142. _ctrl_xfer.data_len = 0;
  143. }
  144. // callback when a transaction complete on
  145. // - DATA stage of control endpoint or
  146. // - Status stage
  147. bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
  148. {
  149. (void) result;
  150. // Endpoint Address is opposite to direction bit, this is Status Stage complete event
  151. if ( tu_edpt_dir(ep_addr) != _ctrl_xfer.request.bmRequestType_bit.direction )
  152. {
  153. TU_ASSERT(0 == xferred_bytes);
  154. // invoke optional dcd hook if available
  155. if (MAKE_WEAK_FUNC(dcd_edpt0_status_complete)) MAKE_WEAK_FUNC(dcd_edpt0_status_complete)(rhport, &_ctrl_xfer.request);
  156. if (_ctrl_xfer.complete_cb)
  157. {
  158. // TODO refactor with usbd_driver_print_control_complete_name
  159. _ctrl_xfer.complete_cb(rhport, CONTROL_STAGE_ACK, &_ctrl_xfer.request);
  160. }
  161. return true;
  162. }
  163. if ( _ctrl_xfer.request.bmRequestType_bit.direction == TUSB_DIR_OUT )
  164. {
  165. TU_VERIFY(_ctrl_xfer.buffer);
  166. memcpy(_ctrl_xfer.buffer, _usbd_ctrl_buf, xferred_bytes);
  167. }
  168. _ctrl_xfer.total_xferred += xferred_bytes;
  169. _ctrl_xfer.buffer += xferred_bytes;
  170. // Data Stage is complete when all request's length are transferred or
  171. // a short packet is sent including zero-length packet.
  172. if ( (_ctrl_xfer.request.wLength == _ctrl_xfer.total_xferred) || (xferred_bytes < CFG_TUD_ENDPOINT0_SIZE) )
  173. {
  174. // DATA stage is complete
  175. bool is_ok = true;
  176. // invoke complete callback if set
  177. // callback can still stall control in status phase e.g out data does not make sense
  178. if ( _ctrl_xfer.complete_cb )
  179. {
  180. #if CFG_TUSB_DEBUG >= 2
  181. usbd_driver_print_control_complete_name(_ctrl_xfer.complete_cb);
  182. #endif
  183. is_ok = _ctrl_xfer.complete_cb(rhport, CONTROL_STAGE_DATA, &_ctrl_xfer.request);
  184. }
  185. if ( is_ok )
  186. {
  187. // Send status
  188. TU_ASSERT( _status_stage_xact(rhport, &_ctrl_xfer.request) );
  189. }else
  190. {
  191. // Stall both IN and OUT control endpoint
  192. dcd_edpt_stall(rhport, EDPT_CTRL_OUT);
  193. dcd_edpt_stall(rhport, EDPT_CTRL_IN);
  194. }
  195. }
  196. else
  197. {
  198. // More data to transfer
  199. TU_ASSERT( _data_stage_xact(rhport) );
  200. }
  201. return true;
  202. }
  203. #endif