usbd.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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 "usbd.h"
  30. #include "device/usbd_pvt.h"
  31. #include "dcd.h"
  32. #ifndef CFG_TUD_TASK_QUEUE_SZ
  33. #define CFG_TUD_TASK_QUEUE_SZ 16
  34. #endif
  35. //--------------------------------------------------------------------+
  36. // Device Data
  37. //--------------------------------------------------------------------+
  38. typedef struct {
  39. struct TU_ATTR_PACKED
  40. {
  41. volatile uint8_t connected : 1;
  42. volatile uint8_t addressed : 1;
  43. volatile uint8_t configured : 1;
  44. volatile uint8_t suspended : 1;
  45. uint8_t remote_wakeup_en : 1; // enable/disable by host
  46. uint8_t remote_wakeup_support : 1; // configuration descriptor's attribute
  47. uint8_t self_powered : 1; // configuration descriptor's attribute
  48. };
  49. uint8_t itf2drv[16]; // map interface number to driver (0xff is invalid)
  50. uint8_t ep2drv[8][2]; // map endpoint to driver ( 0xff is invalid )
  51. struct TU_ATTR_PACKED
  52. {
  53. volatile bool busy : 1;
  54. volatile bool stalled : 1;
  55. // TODO merge ep2drv here, 4-bit should be sufficient
  56. }ep_status[8][2];
  57. }usbd_device_t;
  58. static usbd_device_t _usbd_dev;
  59. // Invalid driver ID in itf2drv[] ep2drv[][] mapping
  60. enum { DRVID_INVALID = 0xFFu };
  61. //--------------------------------------------------------------------+
  62. // Class Driver
  63. //--------------------------------------------------------------------+
  64. typedef struct {
  65. uint8_t class_code;
  66. void (* init ) (void);
  67. void (* reset ) (uint8_t rhport);
  68. bool (* open ) (uint8_t rhport, tusb_desc_interface_t const * desc_intf, uint16_t* p_length);
  69. bool (* control_request ) (uint8_t rhport, tusb_control_request_t const * request);
  70. bool (* control_complete ) (uint8_t rhport, tusb_control_request_t const * request);
  71. bool (* xfer_cb ) (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
  72. void (* sof ) (uint8_t rhport);
  73. } usbd_class_driver_t;
  74. static usbd_class_driver_t const usbd_class_drivers[] =
  75. {
  76. #if CFG_TUD_CDC
  77. {
  78. .class_code = TUSB_CLASS_CDC,
  79. .init = cdcd_init,
  80. .reset = cdcd_reset,
  81. .open = cdcd_open,
  82. .control_request = cdcd_control_request,
  83. .control_complete = cdcd_control_complete,
  84. .xfer_cb = cdcd_xfer_cb,
  85. .sof = NULL
  86. },
  87. #endif
  88. #if CFG_TUD_MSC
  89. {
  90. .class_code = TUSB_CLASS_MSC,
  91. .init = mscd_init,
  92. .reset = mscd_reset,
  93. .open = mscd_open,
  94. .control_request = mscd_control_request,
  95. .control_complete = mscd_control_complete,
  96. .xfer_cb = mscd_xfer_cb,
  97. .sof = NULL
  98. },
  99. #endif
  100. #if CFG_TUD_HID
  101. {
  102. .class_code = TUSB_CLASS_HID,
  103. .init = hidd_init,
  104. .reset = hidd_reset,
  105. .open = hidd_open,
  106. .control_request = hidd_control_request,
  107. .control_complete = hidd_control_complete,
  108. .xfer_cb = hidd_xfer_cb,
  109. .sof = NULL
  110. },
  111. #endif
  112. #if CFG_TUD_MIDI
  113. {
  114. .class_code = TUSB_CLASS_AUDIO,
  115. .init = midid_init,
  116. .open = midid_open,
  117. .reset = midid_reset,
  118. .control_request = midid_control_request,
  119. .control_complete = midid_control_complete,
  120. .xfer_cb = midid_xfer_cb,
  121. .sof = NULL
  122. },
  123. #endif
  124. #if CFG_TUD_VENDOR
  125. {
  126. .class_code = TUSB_CLASS_VENDOR_SPECIFIC,
  127. .init = vendord_init,
  128. .reset = vendord_reset,
  129. .open = vendord_open,
  130. .control_request = tud_vendor_control_request_cb,
  131. .control_complete = tud_vendor_control_complete_cb,
  132. .xfer_cb = vendord_xfer_cb,
  133. .sof = NULL
  134. },
  135. #endif
  136. #if CFG_TUD_USBTMC
  137. // Presently USBTMC is the only defined class with the APP_SPECIFIC class code.
  138. // We maybe need to add subclass codes here, or a callback to ask if a driver can
  139. // handle a particular interface.
  140. {
  141. .class_code = TUD_USBTMC_APP_CLASS,
  142. //.subclass_code = TUD_USBTMC_APP_SUBCLASS
  143. .init = usbtmcd_init_cb,
  144. .reset = usbtmcd_reset_cb,
  145. .open = usbtmcd_open_cb,
  146. .control_request = usbtmcd_control_request_cb,
  147. .control_complete = usbtmcd_control_complete_cb,
  148. .xfer_cb = usbtmcd_xfer_cb,
  149. .sof = NULL
  150. },
  151. #endif
  152. #if CFG_TUD_DFU_RT
  153. {
  154. .class_code = TUD_DFU_APP_CLASS,
  155. //.subclass_code = TUD_DFU_APP_SUBCLASS
  156. .init = dfu_rtd_init,
  157. .reset = dfu_rtd_reset,
  158. .open = dfu_rtd_open,
  159. .control_request = dfu_rtd_control_request,
  160. .control_complete = dfu_rtd_control_complete,
  161. .xfer_cb = dfu_rtd_xfer_cb,
  162. .sof = NULL
  163. },
  164. #endif
  165. };
  166. enum { USBD_CLASS_DRIVER_COUNT = TU_ARRAY_SIZE(usbd_class_drivers) };
  167. //--------------------------------------------------------------------+
  168. // DCD Event
  169. //--------------------------------------------------------------------+
  170. // Event queue
  171. // OPT_MODE_DEVICE is used by OS NONE for mutex (disable usb isr)
  172. OSAL_QUEUE_DEF(OPT_MODE_DEVICE, _usbd_qdef, CFG_TUD_TASK_QUEUE_SZ, dcd_event_t);
  173. static osal_queue_t _usbd_q;
  174. //--------------------------------------------------------------------+
  175. // Prototypes
  176. //--------------------------------------------------------------------+
  177. static void mark_interface_endpoint(uint8_t ep2drv[8][2], uint8_t const* p_desc, uint16_t desc_len, uint8_t driver_id);
  178. static bool process_control_request(uint8_t rhport, tusb_control_request_t const * p_request);
  179. static bool process_set_config(uint8_t rhport, uint8_t cfg_num);
  180. static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const * p_request);
  181. void usbd_control_reset (uint8_t rhport);
  182. bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
  183. void usbd_control_set_complete_callback( bool (*fp) (uint8_t, tusb_control_request_t const * ) );
  184. //--------------------------------------------------------------------+
  185. // Debugging
  186. //--------------------------------------------------------------------+
  187. #if CFG_TUSB_DEBUG > 1
  188. static char const* const _usbd_event_str[DCD_EVENT_COUNT] =
  189. {
  190. "INVALID" ,
  191. "BUS_RESET" ,
  192. "UNPLUGGED" ,
  193. "SOF" ,
  194. "SUSPEND" ,
  195. "RESUME" ,
  196. "SETUP_RECEIVED" ,
  197. "XFER_COMPLETE" ,
  198. "FUNC_CALL"
  199. };
  200. // must be same driver order as usbd_class_drivers[]
  201. static char const* const _usbd_driver_str[USBD_CLASS_DRIVER_COUNT] =
  202. {
  203. #if CFG_TUD_CDC
  204. "CDC",
  205. #endif
  206. #if CFG_TUD_MSC
  207. "MSC",
  208. #endif
  209. #if CFG_TUD_HID
  210. "HID",
  211. #endif
  212. #if CFG_TUD_MIDI
  213. "MIDI",
  214. #endif
  215. #if CFG_TUD_VENDOR
  216. "Vendor",
  217. #endif
  218. #if CFG_TUD_USBTMC
  219. "USBTMC"
  220. #endif
  221. };
  222. static char const* const _tusb_std_request_str[] =
  223. {
  224. "Get Status" ,
  225. "Clear Feature" ,
  226. "Reserved" ,
  227. "Set Feature" ,
  228. "Reserved" ,
  229. "Set Address" ,
  230. "Get Descriptor" ,
  231. "Set Descriptor" ,
  232. "Get Configuration" ,
  233. "Set Configuration" ,
  234. "Get Interface" ,
  235. "Set Interface" ,
  236. "Synch Frame"
  237. };
  238. #endif
  239. //--------------------------------------------------------------------+
  240. // Application API
  241. //--------------------------------------------------------------------+
  242. bool tud_mounted(void)
  243. {
  244. return _usbd_dev.configured;
  245. }
  246. bool tud_suspended(void)
  247. {
  248. return _usbd_dev.suspended;
  249. }
  250. bool tud_remote_wakeup(void)
  251. {
  252. // only wake up host if this feature is supported and enabled and we are suspended
  253. TU_VERIFY (_usbd_dev.suspended && _usbd_dev.remote_wakeup_support && _usbd_dev.remote_wakeup_en );
  254. dcd_remote_wakeup(TUD_OPT_RHPORT);
  255. return true;
  256. }
  257. //--------------------------------------------------------------------+
  258. // USBD Task
  259. //--------------------------------------------------------------------+
  260. bool tud_init (void)
  261. {
  262. TU_LOG2("USBD init\r\n");
  263. tu_varclr(&_usbd_dev);
  264. // Init device queue & task
  265. _usbd_q = osal_queue_create(&_usbd_qdef);
  266. TU_ASSERT(_usbd_q != NULL);
  267. // Init class drivers
  268. for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++)
  269. {
  270. TU_LOG2("%s init\r\n", _usbd_driver_str[i]);
  271. usbd_class_drivers[i].init();
  272. }
  273. // Init device controller driver
  274. dcd_init(TUD_OPT_RHPORT);
  275. dcd_int_enable(TUD_OPT_RHPORT);
  276. return true;
  277. }
  278. static void usbd_reset(uint8_t rhport)
  279. {
  280. tu_varclr(&_usbd_dev);
  281. memset(_usbd_dev.itf2drv, DRVID_INVALID, sizeof(_usbd_dev.itf2drv)); // invalid mapping
  282. memset(_usbd_dev.ep2drv , DRVID_INVALID, sizeof(_usbd_dev.ep2drv )); // invalid mapping
  283. usbd_control_reset(rhport);
  284. for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++)
  285. {
  286. if ( usbd_class_drivers[i].reset ) usbd_class_drivers[i].reset( rhport );
  287. }
  288. }
  289. /* USB Device Driver task
  290. * This top level thread manages all device controller event and delegates events to class-specific drivers.
  291. * This should be called periodically within the mainloop or rtos thread.
  292. *
  293. @code
  294. int main(void)
  295. {
  296. application_init();
  297. tusb_init();
  298. while(1) // the mainloop
  299. {
  300. application_code();
  301. tud_task(); // tinyusb device task
  302. }
  303. }
  304. @endcode
  305. */
  306. void tud_task (void)
  307. {
  308. // Skip if stack is not initialized
  309. if ( !tusb_inited() ) return;
  310. // Loop until there is no more events in the queue
  311. while (1)
  312. {
  313. dcd_event_t event;
  314. if ( !osal_queue_receive(_usbd_q, &event) ) return;
  315. TU_LOG2("USBD: event %s\r\n", event.event_id < DCD_EVENT_COUNT ? _usbd_event_str[event.event_id] : "CORRUPTED");
  316. switch ( event.event_id )
  317. {
  318. case DCD_EVENT_BUS_RESET:
  319. usbd_reset(event.rhport);
  320. break;
  321. case DCD_EVENT_UNPLUGGED:
  322. usbd_reset(event.rhport);
  323. // invoke callback
  324. if (tud_umount_cb) tud_umount_cb();
  325. break;
  326. case DCD_EVENT_SETUP_RECEIVED:
  327. TU_LOG2(" ");
  328. TU_LOG2_MEM(&event.setup_received, 1, 8);
  329. // Mark as connected after receiving 1st setup packet.
  330. // But it is easier to set it every time instead of wasting time to check then set
  331. _usbd_dev.connected = 1;
  332. // Process control request
  333. if ( !process_control_request(event.rhport, &event.setup_received) )
  334. {
  335. // Failed -> stall both control endpoint IN and OUT
  336. dcd_edpt_stall(event.rhport, 0);
  337. dcd_edpt_stall(event.rhport, 0 | TUSB_DIR_IN_MASK);
  338. }
  339. break;
  340. case DCD_EVENT_XFER_COMPLETE:
  341. {
  342. // Invoke the class callback associated with the endpoint address
  343. uint8_t const ep_addr = event.xfer_complete.ep_addr;
  344. uint8_t const epnum = tu_edpt_number(ep_addr);
  345. uint8_t const ep_dir = tu_edpt_dir(ep_addr);
  346. TU_LOG2(" Endpoint: 0x%02X, Bytes: %ld\r\n", ep_addr, event.xfer_complete.len);
  347. _usbd_dev.ep_status[epnum][ep_dir].busy = false;
  348. if ( 0 == epnum )
  349. {
  350. usbd_control_xfer_cb(event.rhport, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
  351. }
  352. else
  353. {
  354. uint8_t const drv_id = _usbd_dev.ep2drv[epnum][ep_dir];
  355. TU_ASSERT(drv_id < USBD_CLASS_DRIVER_COUNT,);
  356. TU_LOG2(" %s xfer callback\r\n", _usbd_driver_str[drv_id]);
  357. usbd_class_drivers[drv_id].xfer_cb(event.rhport, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
  358. }
  359. }
  360. break;
  361. case DCD_EVENT_SUSPEND:
  362. if (tud_suspend_cb) tud_suspend_cb(_usbd_dev.remote_wakeup_en);
  363. break;
  364. case DCD_EVENT_RESUME:
  365. if (tud_resume_cb) tud_resume_cb();
  366. break;
  367. case DCD_EVENT_SOF:
  368. for ( uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++ )
  369. {
  370. if ( usbd_class_drivers[i].sof )
  371. {
  372. usbd_class_drivers[i].sof(event.rhport);
  373. }
  374. }
  375. break;
  376. case USBD_EVENT_FUNC_CALL:
  377. if ( event.func_call.func ) event.func_call.func(event.func_call.param);
  378. break;
  379. default:
  380. TU_BREAKPOINT();
  381. break;
  382. }
  383. }
  384. }
  385. //--------------------------------------------------------------------+
  386. // Control Request Parser & Handling
  387. //--------------------------------------------------------------------+
  388. // This handles the actual request and its response.
  389. // return false will cause its caller to stall control endpoint
  390. static bool process_control_request(uint8_t rhport, tusb_control_request_t const * p_request)
  391. {
  392. usbd_control_set_complete_callback(NULL);
  393. TU_ASSERT(p_request->bmRequestType_bit.type < TUSB_REQ_TYPE_INVALID);
  394. // Vendor request
  395. if ( p_request->bmRequestType_bit.type == TUSB_REQ_TYPE_VENDOR )
  396. {
  397. TU_VERIFY(tud_vendor_control_request_cb);
  398. if (tud_vendor_control_complete_cb) usbd_control_set_complete_callback(tud_vendor_control_complete_cb);
  399. return tud_vendor_control_request_cb(rhport, p_request);
  400. }
  401. #if CFG_TUSB_DEBUG > 1
  402. if (TUSB_REQ_TYPE_STANDARD == p_request->bmRequestType_bit.type && p_request->bRequest <= TUSB_REQ_SYNCH_FRAME)
  403. {
  404. TU_LOG2(" %s\r\n", _tusb_std_request_str[p_request->bRequest]);
  405. }
  406. #endif
  407. switch ( p_request->bmRequestType_bit.recipient )
  408. {
  409. //------------- Device Requests e.g in enumeration -------------//
  410. case TUSB_REQ_RCPT_DEVICE:
  411. if ( TUSB_REQ_TYPE_STANDARD != p_request->bmRequestType_bit.type )
  412. {
  413. // Non standard request is not supported
  414. TU_BREAKPOINT();
  415. return false;
  416. }
  417. switch ( p_request->bRequest )
  418. {
  419. case TUSB_REQ_SET_ADDRESS:
  420. // Depending on mcu, status phase could be sent either before or after changing device address
  421. // Therefore DCD must include zero-length status response
  422. dcd_set_address(rhport, (uint8_t) p_request->wValue);
  423. _usbd_dev.addressed = 1;
  424. return true; // skip status
  425. break;
  426. case TUSB_REQ_GET_CONFIGURATION:
  427. {
  428. uint8_t cfgnum = _usbd_dev.configured ? 1 : 0;
  429. tud_control_xfer(rhport, p_request, &cfgnum, 1);
  430. }
  431. break;
  432. case TUSB_REQ_SET_CONFIGURATION:
  433. {
  434. uint8_t const cfg_num = (uint8_t) p_request->wValue;
  435. dcd_set_config(rhport, cfg_num);
  436. _usbd_dev.configured = cfg_num ? 1 : 0;
  437. if ( cfg_num ) TU_ASSERT( process_set_config(rhport, cfg_num) );
  438. tud_control_status(rhport, p_request);
  439. }
  440. break;
  441. case TUSB_REQ_GET_DESCRIPTOR:
  442. TU_VERIFY( process_get_descriptor(rhport, p_request) );
  443. break;
  444. case TUSB_REQ_SET_FEATURE:
  445. // Only support remote wakeup for device feature
  446. TU_VERIFY(TUSB_REQ_FEATURE_REMOTE_WAKEUP == p_request->wValue);
  447. // Host may enable remote wake up before suspending especially HID device
  448. _usbd_dev.remote_wakeup_en = true;
  449. tud_control_status(rhport, p_request);
  450. break;
  451. case TUSB_REQ_CLEAR_FEATURE:
  452. // Only support remote wakeup for device feature
  453. TU_VERIFY(TUSB_REQ_FEATURE_REMOTE_WAKEUP == p_request->wValue);
  454. // Host may disable remote wake up after resuming
  455. _usbd_dev.remote_wakeup_en = false;
  456. tud_control_status(rhport, p_request);
  457. break;
  458. case TUSB_REQ_GET_STATUS:
  459. {
  460. // Device status bit mask
  461. // - Bit 0: Self Powered
  462. // - Bit 1: Remote Wakeup enabled
  463. uint16_t status = (_usbd_dev.self_powered ? 1 : 0) | (_usbd_dev.remote_wakeup_en ? 2 : 0);
  464. tud_control_xfer(rhport, p_request, &status, 2);
  465. }
  466. break;
  467. // Unknown/Unsupported request
  468. default: TU_BREAKPOINT(); return false;
  469. }
  470. break;
  471. //------------- Class/Interface Specific Request -------------//
  472. case TUSB_REQ_RCPT_INTERFACE:
  473. {
  474. uint8_t const itf = tu_u16_low(p_request->wIndex);
  475. TU_VERIFY(itf < TU_ARRAY_SIZE(_usbd_dev.itf2drv));
  476. uint8_t const drvid = _usbd_dev.itf2drv[itf];
  477. TU_VERIFY(drvid < USBD_CLASS_DRIVER_COUNT);
  478. if (p_request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD)
  479. {
  480. switch ( p_request->bRequest )
  481. {
  482. case TUSB_REQ_GET_INTERFACE:
  483. {
  484. // TODO not support alternate interface yet
  485. uint8_t alternate = 0;
  486. tud_control_xfer(rhport, p_request, &alternate, 1);
  487. }
  488. break;
  489. case TUSB_REQ_SET_INTERFACE:
  490. {
  491. uint8_t const alternate = (uint8_t) p_request->wValue;
  492. // TODO not support alternate interface yet
  493. TU_ASSERT(alternate == 0);
  494. tud_control_status(rhport, p_request);
  495. }
  496. break;
  497. default:
  498. // forward to class driver: "STD request to Interface"
  499. // GET HID REPORT DESCRIPTOR falls into this case
  500. // stall control endpoint if driver return false
  501. usbd_control_set_complete_callback(usbd_class_drivers[drvid].control_complete);
  502. TU_LOG2(" %s control request\r\n", _usbd_driver_str[drvid]);
  503. TU_ASSERT(usbd_class_drivers[drvid].control_request != NULL &&
  504. usbd_class_drivers[drvid].control_request(rhport, p_request));
  505. break;
  506. }
  507. }else
  508. {
  509. // forward to class driver: "non-STD request to Interface"
  510. // stall control endpoint if driver return false
  511. usbd_control_set_complete_callback(usbd_class_drivers[drvid].control_complete);
  512. TU_LOG2(" %s control request\r\n", _usbd_driver_str[drvid]);
  513. TU_ASSERT(usbd_class_drivers[drvid].control_request != NULL &&
  514. usbd_class_drivers[drvid].control_request(rhport, p_request));
  515. }
  516. }
  517. break;
  518. //------------- Endpoint Request -------------//
  519. case TUSB_REQ_RCPT_ENDPOINT:
  520. {
  521. uint8_t const ep_addr = tu_u16_low(p_request->wIndex);
  522. uint8_t const ep_num = tu_edpt_number(ep_addr);
  523. uint8_t const ep_dir = tu_edpt_dir(ep_addr);
  524. TU_ASSERT(ep_num < TU_ARRAY_SIZE(_usbd_dev.ep2drv) );
  525. uint8_t const drv_id = _usbd_dev.ep2drv[ep_num][ep_dir];
  526. TU_ASSERT(drv_id < USBD_CLASS_DRIVER_COUNT);
  527. bool ret = false;
  528. if ( TUSB_REQ_TYPE_STANDARD != p_request->bmRequestType_bit.type )
  529. {
  530. // complete callback is also capable of stalling/acking the request
  531. usbd_control_set_complete_callback(usbd_class_drivers[drv_id].control_complete);
  532. }
  533. // Then handle if it is standard request
  534. if ( TUSB_REQ_TYPE_STANDARD == p_request->bmRequestType_bit.type )
  535. {
  536. // force return true for standard request
  537. ret = true;
  538. switch ( p_request->bRequest )
  539. {
  540. case TUSB_REQ_GET_STATUS:
  541. {
  542. uint16_t status = usbd_edpt_stalled(rhport, ep_addr) ? 0x0001 : 0x0000;
  543. tud_control_xfer(rhport, p_request, &status, 2);
  544. }
  545. break;
  546. case TUSB_REQ_CLEAR_FEATURE:
  547. if ( TUSB_REQ_FEATURE_EDPT_HALT == p_request->wValue )
  548. {
  549. usbd_edpt_clear_stall(rhport, ep_addr);
  550. }
  551. tud_control_status(rhport, p_request);
  552. break;
  553. case TUSB_REQ_SET_FEATURE:
  554. if ( TUSB_REQ_FEATURE_EDPT_HALT == p_request->wValue )
  555. {
  556. usbd_edpt_stall(rhport, ep_addr);
  557. }
  558. tud_control_status(rhport, p_request);
  559. break;
  560. // Unknown/Unsupported request
  561. default: TU_BREAKPOINT(); return false;
  562. }
  563. }
  564. // Some classes such as TMC needs to clear/re-init its buffer when receiving CLEAR_FEATURE request
  565. // We will forward all request targeted endpoint to its class driver
  566. // For class-type requests: must (call tud_control_status(); return true) or (return false)
  567. // For std-type requests: non-std request codes are already discarded.
  568. // must not call tud_control_status(), and return value will have no effect
  569. // class driver is invoked last, so that EP already has EP stall cleared (in event of clear feature EP halt)
  570. TU_LOG2(" %s control request\r\n", _usbd_driver_str[drv_id]);
  571. if ( usbd_class_drivers[drv_id].control_request &&
  572. usbd_class_drivers[drv_id].control_request(rhport, p_request))
  573. {
  574. ret = true;
  575. }
  576. return ret;
  577. }
  578. break;
  579. // Unknown recipient
  580. default: TU_BREAKPOINT(); return false;
  581. }
  582. return true;
  583. }
  584. // Process Set Configure Request
  585. // This function parse configuration descriptor & open drivers accordingly
  586. static bool process_set_config(uint8_t rhport, uint8_t cfg_num)
  587. {
  588. tusb_desc_configuration_t const * desc_cfg = (tusb_desc_configuration_t const *) tud_descriptor_configuration_cb(cfg_num-1); // index is cfg_num-1
  589. TU_ASSERT(desc_cfg != NULL && desc_cfg->bDescriptorType == TUSB_DESC_CONFIGURATION);
  590. // Parse configuration descriptor
  591. _usbd_dev.remote_wakeup_support = (desc_cfg->bmAttributes & TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP) ? 1 : 0;
  592. _usbd_dev.self_powered = (desc_cfg->bmAttributes & TUSB_DESC_CONFIG_ATT_SELF_POWERED) ? 1 : 0;
  593. // Parse interface descriptor
  594. uint8_t const * p_desc = ((uint8_t const*) desc_cfg) + sizeof(tusb_desc_configuration_t);
  595. uint8_t const * desc_end = ((uint8_t const*) desc_cfg) + desc_cfg->wTotalLength;
  596. while( p_desc < desc_end )
  597. {
  598. // Each interface always starts with Interface or Association descriptor
  599. if ( TUSB_DESC_INTERFACE_ASSOCIATION == tu_desc_type(p_desc) )
  600. {
  601. p_desc = tu_desc_next(p_desc); // ignore Interface Association
  602. }else
  603. {
  604. TU_ASSERT( TUSB_DESC_INTERFACE == tu_desc_type(p_desc) );
  605. tusb_desc_interface_t* desc_itf = (tusb_desc_interface_t*) p_desc;
  606. // Check if class is supported
  607. uint8_t drv_id;
  608. for (drv_id = 0; drv_id < USBD_CLASS_DRIVER_COUNT; drv_id++)
  609. {
  610. if ( usbd_class_drivers[drv_id].class_code == desc_itf->bInterfaceClass ) break;
  611. }
  612. TU_ASSERT( drv_id < USBD_CLASS_DRIVER_COUNT );
  613. // Interface number must not be used already TODO alternate interface
  614. TU_ASSERT( DRVID_INVALID == _usbd_dev.itf2drv[desc_itf->bInterfaceNumber] );
  615. _usbd_dev.itf2drv[desc_itf->bInterfaceNumber] = drv_id;
  616. uint16_t itf_len=0;
  617. TU_LOG2(" %s open\r\n", _usbd_driver_str[drv_id]);
  618. TU_ASSERT( usbd_class_drivers[drv_id].open(rhport, desc_itf, &itf_len) );
  619. TU_ASSERT( itf_len >= sizeof(tusb_desc_interface_t) );
  620. mark_interface_endpoint(_usbd_dev.ep2drv, p_desc, itf_len, drv_id);
  621. p_desc += itf_len; // next interface
  622. }
  623. }
  624. // invoke callback
  625. if (tud_mount_cb) tud_mount_cb();
  626. return true;
  627. }
  628. // Helper marking endpoint of interface belongs to class driver
  629. static void mark_interface_endpoint(uint8_t ep2drv[8][2], uint8_t const* p_desc, uint16_t desc_len, uint8_t driver_id)
  630. {
  631. uint16_t len = 0;
  632. while( len < desc_len )
  633. {
  634. if ( TUSB_DESC_ENDPOINT == tu_desc_type(p_desc) )
  635. {
  636. uint8_t const ep_addr = ((tusb_desc_endpoint_t const*) p_desc)->bEndpointAddress;
  637. ep2drv[tu_edpt_number(ep_addr)][tu_edpt_dir(ep_addr)] = driver_id;
  638. }
  639. len = (uint16_t)(len + tu_desc_len(p_desc));
  640. p_desc = tu_desc_next(p_desc);
  641. }
  642. }
  643. // return descriptor's buffer and update desc_len
  644. static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const * p_request)
  645. {
  646. tusb_desc_type_t const desc_type = (tusb_desc_type_t) tu_u16_high(p_request->wValue);
  647. uint8_t const desc_index = tu_u16_low( p_request->wValue );
  648. switch(desc_type)
  649. {
  650. case TUSB_DESC_DEVICE:
  651. {
  652. uint16_t len = sizeof(tusb_desc_device_t);
  653. // Only send up to EP0 Packet Size if not addressed
  654. if ((CFG_TUD_ENDPOINT0_SIZE < sizeof(tusb_desc_device_t)) && !_usbd_dev.addressed)
  655. {
  656. len = CFG_TUD_ENDPOINT0_SIZE;
  657. }
  658. return tud_control_xfer(rhport, p_request, (void*) tud_descriptor_device_cb(), len);
  659. }
  660. break;
  661. case TUSB_DESC_BOS:
  662. {
  663. // requested by host if USB > 2.0 ( i.e 2.1 or 3.x )
  664. if (!tud_descriptor_bos_cb) return false;
  665. tusb_desc_bos_t const* desc_bos = (tusb_desc_bos_t const*) tud_descriptor_bos_cb();
  666. uint16_t total_len;
  667. memcpy(&total_len, &desc_bos->wTotalLength, 2); // possibly mis-aligned memory
  668. return tud_control_xfer(rhport, p_request, (void*) desc_bos, total_len);
  669. }
  670. break;
  671. case TUSB_DESC_CONFIGURATION:
  672. {
  673. tusb_desc_configuration_t const* desc_config = (tusb_desc_configuration_t const*) tud_descriptor_configuration_cb(desc_index);
  674. TU_ASSERT(desc_config);
  675. uint16_t total_len;
  676. memcpy(&total_len, &desc_config->wTotalLength, 2); // possibly mis-aligned memory
  677. return tud_control_xfer(rhport, p_request, (void*) desc_config, total_len);
  678. }
  679. break;
  680. case TUSB_DESC_STRING:
  681. // String Descriptor always uses the desc set from user
  682. if ( desc_index == 0xEE )
  683. {
  684. // The 0xEE index string is a Microsoft OS Descriptors.
  685. // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
  686. return false;
  687. }else
  688. {
  689. uint8_t const* desc_str = (uint8_t const*) tud_descriptor_string_cb(desc_index);
  690. TU_ASSERT(desc_str);
  691. // first byte of descriptor is its size
  692. return tud_control_xfer(rhport, p_request, (void*) desc_str, desc_str[0]);
  693. }
  694. break;
  695. case TUSB_DESC_DEVICE_QUALIFIER:
  696. // TODO If not highspeed capable stall this request otherwise
  697. // return the descriptor that could work in highspeed
  698. return false;
  699. break;
  700. default: return false;
  701. }
  702. return true;
  703. }
  704. //--------------------------------------------------------------------+
  705. // DCD Event Handler
  706. //--------------------------------------------------------------------+
  707. void dcd_event_handler(dcd_event_t const * event, bool in_isr)
  708. {
  709. switch (event->event_id)
  710. {
  711. case DCD_EVENT_BUS_RESET:
  712. osal_queue_send(_usbd_q, event, in_isr);
  713. break;
  714. case DCD_EVENT_UNPLUGGED:
  715. _usbd_dev.connected = 0;
  716. _usbd_dev.addressed = 0;
  717. _usbd_dev.configured = 0;
  718. _usbd_dev.suspended = 0;
  719. osal_queue_send(_usbd_q, event, in_isr);
  720. break;
  721. case DCD_EVENT_SOF:
  722. // nothing to do now
  723. break;
  724. case DCD_EVENT_SUSPEND:
  725. // NOTE: When plugging/unplugging device, the D+/D- state are unstable and can accidentally meet the
  726. // SUSPEND condition ( Idle for 3ms ). Some MCUs such as SAMD doesn't distinguish suspend vs disconnect as well.
  727. // We will skip handling SUSPEND/RESUME event if not currently connected
  728. if ( _usbd_dev.connected )
  729. {
  730. _usbd_dev.suspended = 1;
  731. osal_queue_send(_usbd_q, event, in_isr);
  732. }
  733. break;
  734. case DCD_EVENT_RESUME:
  735. if ( _usbd_dev.connected )
  736. {
  737. _usbd_dev.suspended = 0;
  738. osal_queue_send(_usbd_q, event, in_isr);
  739. }
  740. break;
  741. case DCD_EVENT_SETUP_RECEIVED:
  742. osal_queue_send(_usbd_q, event, in_isr);
  743. break;
  744. case DCD_EVENT_XFER_COMPLETE:
  745. osal_queue_send(_usbd_q, event, in_isr);
  746. TU_ASSERT(event->xfer_complete.result == XFER_RESULT_SUCCESS,);
  747. break;
  748. // Not an DCD event, just a convenient way to defer ISR function should we need to
  749. case USBD_EVENT_FUNC_CALL:
  750. osal_queue_send(_usbd_q, event, in_isr);
  751. break;
  752. default: break;
  753. }
  754. }
  755. //--------------------------------------------------------------------+
  756. // Helper
  757. //--------------------------------------------------------------------+
  758. // Parse consecutive endpoint descriptors (IN & OUT)
  759. bool usbd_open_edpt_pair(uint8_t rhport, uint8_t const* p_desc, uint8_t ep_count, uint8_t xfer_type, uint8_t* ep_out, uint8_t* ep_in)
  760. {
  761. for(int i=0; i<ep_count; i++)
  762. {
  763. tusb_desc_endpoint_t const * desc_ep = (tusb_desc_endpoint_t const *) p_desc;
  764. TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType && xfer_type == desc_ep->bmAttributes.xfer);
  765. TU_ASSERT(dcd_edpt_open(rhport, desc_ep));
  766. if ( tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN )
  767. {
  768. (*ep_in) = desc_ep->bEndpointAddress;
  769. }else
  770. {
  771. (*ep_out) = desc_ep->bEndpointAddress;
  772. }
  773. p_desc = tu_desc_next(p_desc);
  774. }
  775. return true;
  776. }
  777. // Helper to defer an isr function
  778. void usbd_defer_func(osal_task_func_t func, void* param, bool in_isr)
  779. {
  780. dcd_event_t event =
  781. {
  782. .rhport = 0,
  783. .event_id = USBD_EVENT_FUNC_CALL,
  784. };
  785. event.func_call.func = func;
  786. event.func_call.param = param;
  787. dcd_event_handler(&event, in_isr);
  788. }
  789. //--------------------------------------------------------------------+
  790. // USBD Endpoint API
  791. //--------------------------------------------------------------------+
  792. bool usbd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes)
  793. {
  794. uint8_t const epnum = tu_edpt_number(ep_addr);
  795. uint8_t const dir = tu_edpt_dir(ep_addr);
  796. TU_VERIFY( dcd_edpt_xfer(rhport, ep_addr, buffer, total_bytes) );
  797. _usbd_dev.ep_status[epnum][dir].busy = true;
  798. return true;
  799. }
  800. bool usbd_edpt_busy(uint8_t rhport, uint8_t ep_addr)
  801. {
  802. (void) rhport;
  803. uint8_t const epnum = tu_edpt_number(ep_addr);
  804. uint8_t const dir = tu_edpt_dir(ep_addr);
  805. return _usbd_dev.ep_status[epnum][dir].busy;
  806. }
  807. void usbd_edpt_stall(uint8_t rhport, uint8_t ep_addr)
  808. {
  809. uint8_t const epnum = tu_edpt_number(ep_addr);
  810. uint8_t const dir = tu_edpt_dir(ep_addr);
  811. dcd_edpt_stall(rhport, ep_addr);
  812. _usbd_dev.ep_status[epnum][dir].stalled = true;
  813. _usbd_dev.ep_status[epnum][dir].busy = true;
  814. }
  815. void usbd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr)
  816. {
  817. uint8_t const epnum = tu_edpt_number(ep_addr);
  818. uint8_t const dir = tu_edpt_dir(ep_addr);
  819. dcd_edpt_clear_stall(rhport, ep_addr);
  820. _usbd_dev.ep_status[epnum][dir].stalled = false;
  821. _usbd_dev.ep_status[epnum][dir].busy = false;
  822. }
  823. bool usbd_edpt_stalled(uint8_t rhport, uint8_t ep_addr)
  824. {
  825. (void) rhport;
  826. uint8_t const epnum = tu_edpt_number(ep_addr);
  827. uint8_t const dir = tu_edpt_dir(ep_addr);
  828. return _usbd_dev.ep_status[epnum][dir].stalled;
  829. }
  830. #endif