usbd.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  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.h"
  30. #include "device/usbd_pvt.h"
  31. #include "device/dcd.h"
  32. //--------------------------------------------------------------------+
  33. // USBD Configuration
  34. //--------------------------------------------------------------------+
  35. // Debug level of USBD
  36. #define USBD_DBG 2
  37. #ifndef CFG_TUD_TASK_QUEUE_SZ
  38. #define CFG_TUD_TASK_QUEUE_SZ 16
  39. #endif
  40. //--------------------------------------------------------------------+
  41. // Device Data
  42. //--------------------------------------------------------------------+
  43. // Invalid driver ID in itf2drv[] ep2drv[][] mapping
  44. enum { DRVID_INVALID = 0xFFu };
  45. typedef struct
  46. {
  47. struct TU_ATTR_PACKED
  48. {
  49. volatile uint8_t connected : 1;
  50. volatile uint8_t addressed : 1;
  51. volatile uint8_t suspended : 1;
  52. uint8_t remote_wakeup_en : 1; // enable/disable by host
  53. uint8_t remote_wakeup_support : 1; // configuration descriptor's attribute
  54. uint8_t self_powered : 1; // configuration descriptor's attribute
  55. };
  56. volatile uint8_t cfg_num; // current active configuration (0x00 is not configured)
  57. uint8_t speed;
  58. uint8_t itf2drv[16]; // map interface number to driver (0xff is invalid)
  59. uint8_t ep2drv[CFG_TUD_ENDPPOINT_MAX][2]; // map endpoint to driver ( 0xff is invalid )
  60. struct TU_ATTR_PACKED
  61. {
  62. volatile bool busy : 1;
  63. volatile bool stalled : 1;
  64. volatile bool claimed : 1;
  65. // TODO merge ep2drv here, 4-bit should be sufficient
  66. }ep_status[CFG_TUD_ENDPPOINT_MAX][2];
  67. }usbd_device_t;
  68. static usbd_device_t _usbd_dev;
  69. //--------------------------------------------------------------------+
  70. // Class Driver
  71. //--------------------------------------------------------------------+
  72. #if CFG_TUSB_DEBUG >= 2
  73. #define DRIVER_NAME(_name) .name = _name,
  74. #else
  75. #define DRIVER_NAME(_name)
  76. #endif
  77. // Built-in class drivers
  78. static usbd_class_driver_t const _usbd_driver[] =
  79. {
  80. #if CFG_TUD_CDC
  81. {
  82. DRIVER_NAME("CDC")
  83. .init = cdcd_init,
  84. .reset = cdcd_reset,
  85. .open = cdcd_open,
  86. .control_xfer_cb = cdcd_control_xfer_cb,
  87. .xfer_cb = cdcd_xfer_cb,
  88. .sof = NULL
  89. },
  90. #endif
  91. #if CFG_TUD_MSC
  92. {
  93. DRIVER_NAME("MSC")
  94. .init = mscd_init,
  95. .reset = mscd_reset,
  96. .open = mscd_open,
  97. .control_xfer_cb = mscd_control_xfer_cb,
  98. .xfer_cb = mscd_xfer_cb,
  99. .sof = NULL
  100. },
  101. #endif
  102. #if CFG_TUD_HID
  103. {
  104. DRIVER_NAME("HID")
  105. .init = hidd_init,
  106. .reset = hidd_reset,
  107. .open = hidd_open,
  108. .control_xfer_cb = hidd_control_xfer_cb,
  109. .xfer_cb = hidd_xfer_cb,
  110. .sof = NULL
  111. },
  112. #endif
  113. #if CFG_TUD_AUDIO
  114. {
  115. DRIVER_NAME("AUDIO")
  116. .init = audiod_init,
  117. .reset = audiod_reset,
  118. .open = audiod_open,
  119. .control_xfer_cb = audiod_control_xfer_cb,
  120. .xfer_cb = audiod_xfer_cb,
  121. .sof = NULL
  122. },
  123. #endif
  124. #if CFG_TUD_VIDEO
  125. {
  126. DRIVER_NAME("VIDEO")
  127. .init = videod_init,
  128. .reset = videod_reset,
  129. .open = videod_open,
  130. .control_xfer_cb = videod_control_xfer_cb,
  131. .xfer_cb = videod_xfer_cb,
  132. .sof = NULL
  133. },
  134. #endif
  135. #if CFG_TUD_MIDI
  136. {
  137. DRIVER_NAME("MIDI")
  138. .init = midid_init,
  139. .open = midid_open,
  140. .reset = midid_reset,
  141. .control_xfer_cb = midid_control_xfer_cb,
  142. .xfer_cb = midid_xfer_cb,
  143. .sof = NULL
  144. },
  145. #endif
  146. #if CFG_TUD_VENDOR
  147. {
  148. DRIVER_NAME("VENDOR")
  149. .init = vendord_init,
  150. .reset = vendord_reset,
  151. .open = vendord_open,
  152. .control_xfer_cb = tud_vendor_control_xfer_cb,
  153. .xfer_cb = vendord_xfer_cb,
  154. .sof = NULL
  155. },
  156. #endif
  157. #if CFG_TUD_USBTMC
  158. {
  159. DRIVER_NAME("TMC")
  160. .init = usbtmcd_init_cb,
  161. .reset = usbtmcd_reset_cb,
  162. .open = usbtmcd_open_cb,
  163. .control_xfer_cb = usbtmcd_control_xfer_cb,
  164. .xfer_cb = usbtmcd_xfer_cb,
  165. .sof = NULL
  166. },
  167. #endif
  168. #if CFG_TUD_DFU_RUNTIME
  169. {
  170. DRIVER_NAME("DFU-RUNTIME")
  171. .init = dfu_rtd_init,
  172. .reset = dfu_rtd_reset,
  173. .open = dfu_rtd_open,
  174. .control_xfer_cb = dfu_rtd_control_xfer_cb,
  175. .xfer_cb = NULL,
  176. .sof = NULL
  177. },
  178. #endif
  179. #if CFG_TUD_DFU
  180. {
  181. DRIVER_NAME("DFU")
  182. .init = dfu_moded_init,
  183. .reset = dfu_moded_reset,
  184. .open = dfu_moded_open,
  185. .control_xfer_cb = dfu_moded_control_xfer_cb,
  186. .xfer_cb = NULL,
  187. .sof = NULL
  188. },
  189. #endif
  190. #if CFG_TUD_ECM_RNDIS || CFG_TUD_NCM
  191. {
  192. DRIVER_NAME("NET")
  193. .init = netd_init,
  194. .reset = netd_reset,
  195. .open = netd_open,
  196. .control_xfer_cb = netd_control_xfer_cb,
  197. .xfer_cb = netd_xfer_cb,
  198. .sof = NULL,
  199. },
  200. #endif
  201. #if CFG_TUD_BTH
  202. {
  203. DRIVER_NAME("BTH")
  204. .init = btd_init,
  205. .reset = btd_reset,
  206. .open = btd_open,
  207. .control_xfer_cb = btd_control_xfer_cb,
  208. .xfer_cb = btd_xfer_cb,
  209. .sof = NULL
  210. },
  211. #endif
  212. };
  213. enum { BUILTIN_DRIVER_COUNT = TU_ARRAY_SIZE(_usbd_driver) };
  214. // Additional class drivers implemented by application
  215. static usbd_class_driver_t const * _app_driver = NULL;
  216. static uint8_t _app_driver_count = 0;
  217. // virtually joins built-in and application drivers together.
  218. // Application is positioned first to allow overwriting built-in ones.
  219. static inline usbd_class_driver_t const * get_driver(uint8_t drvid)
  220. {
  221. // Application drivers
  222. if ( usbd_app_driver_get_cb )
  223. {
  224. if ( drvid < _app_driver_count ) return &_app_driver[drvid];
  225. drvid -= _app_driver_count;
  226. }
  227. // Built-in drivers
  228. if (drvid < BUILTIN_DRIVER_COUNT) return &_usbd_driver[drvid];
  229. return NULL;
  230. }
  231. #define TOTAL_DRIVER_COUNT (_app_driver_count + BUILTIN_DRIVER_COUNT)
  232. //--------------------------------------------------------------------+
  233. // DCD Event
  234. //--------------------------------------------------------------------+
  235. static bool _usbd_initialized = false;
  236. // Event queue
  237. // OPT_MODE_DEVICE is used by OS NONE for mutex (disable usb isr)
  238. OSAL_QUEUE_DEF(OPT_MODE_DEVICE, _usbd_qdef, CFG_TUD_TASK_QUEUE_SZ, dcd_event_t);
  239. static osal_queue_t _usbd_q;
  240. // Mutex for claiming endpoint, only needed when using with preempted RTOS
  241. #if CFG_TUSB_OS != OPT_OS_NONE
  242. static osal_mutex_def_t _ubsd_mutexdef;
  243. static osal_mutex_t _usbd_mutex;
  244. #endif
  245. //--------------------------------------------------------------------+
  246. // Prototypes
  247. //--------------------------------------------------------------------+
  248. static bool process_control_request(uint8_t rhport, tusb_control_request_t const * p_request);
  249. static bool process_set_config(uint8_t rhport, uint8_t cfg_num);
  250. static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const * p_request);
  251. // from usbd_control.c
  252. void usbd_control_reset(void);
  253. void usbd_control_set_request(tusb_control_request_t const *request);
  254. void usbd_control_set_complete_callback( usbd_control_xfer_cb_t fp );
  255. bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
  256. //--------------------------------------------------------------------+
  257. // Debug
  258. //--------------------------------------------------------------------+
  259. #if CFG_TUSB_DEBUG >= 2
  260. static char const* const _usbd_event_str[DCD_EVENT_COUNT] =
  261. {
  262. "Invalid" ,
  263. "Bus Reset" ,
  264. "Unplugged" ,
  265. "SOF" ,
  266. "Suspend" ,
  267. "Resume" ,
  268. "Setup Received" ,
  269. "Xfer Complete" ,
  270. "Func Call"
  271. };
  272. static char const* const _tusb_std_request_str[] =
  273. {
  274. "Get Status" ,
  275. "Clear Feature" ,
  276. "Reserved" ,
  277. "Set Feature" ,
  278. "Reserved" ,
  279. "Set Address" ,
  280. "Get Descriptor" ,
  281. "Set Descriptor" ,
  282. "Get Configuration" ,
  283. "Set Configuration" ,
  284. "Get Interface" ,
  285. "Set Interface" ,
  286. "Synch Frame"
  287. };
  288. static char const* const _tusb_speed_str[] = { "Full", "Low", "High" };
  289. // for usbd_control to print the name of control complete driver
  290. void usbd_driver_print_control_complete_name(usbd_control_xfer_cb_t callback)
  291. {
  292. for (uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++)
  293. {
  294. usbd_class_driver_t const * driver = get_driver(i);
  295. if ( driver->control_xfer_cb == callback )
  296. {
  297. TU_LOG2(" %s control complete\r\n", driver->name);
  298. return;
  299. }
  300. }
  301. }
  302. #endif
  303. //--------------------------------------------------------------------+
  304. // Application API
  305. //--------------------------------------------------------------------+
  306. tusb_speed_t tud_speed_get(void)
  307. {
  308. return (tusb_speed_t) _usbd_dev.speed;
  309. }
  310. bool tud_connected(void)
  311. {
  312. return _usbd_dev.connected;
  313. }
  314. bool tud_mounted(void)
  315. {
  316. return _usbd_dev.cfg_num ? true : false;
  317. }
  318. bool tud_suspended(void)
  319. {
  320. return _usbd_dev.suspended;
  321. }
  322. bool tud_remote_wakeup(void)
  323. {
  324. // only wake up host if this feature is supported and enabled and we are suspended
  325. TU_VERIFY (_usbd_dev.suspended && _usbd_dev.remote_wakeup_support && _usbd_dev.remote_wakeup_en );
  326. dcd_remote_wakeup(TUD_OPT_RHPORT);
  327. return true;
  328. }
  329. bool tud_disconnect(void)
  330. {
  331. TU_VERIFY(dcd_disconnect);
  332. dcd_disconnect(TUD_OPT_RHPORT);
  333. return true;
  334. }
  335. bool tud_connect(void)
  336. {
  337. TU_VERIFY(dcd_connect);
  338. dcd_connect(TUD_OPT_RHPORT);
  339. return true;
  340. }
  341. //--------------------------------------------------------------------+
  342. // USBD Task
  343. //--------------------------------------------------------------------+
  344. bool tud_inited(void)
  345. {
  346. return _usbd_initialized;
  347. }
  348. bool tud_init (uint8_t rhport)
  349. {
  350. // skip if already initialized
  351. if (_usbd_initialized) return _usbd_initialized;
  352. TU_LOG2("USBD init\r\n");
  353. tu_varclr(&_usbd_dev);
  354. #if CFG_TUSB_OS != OPT_OS_NONE
  355. // Init device mutex
  356. _usbd_mutex = osal_mutex_create(&_ubsd_mutexdef);
  357. TU_ASSERT(_usbd_mutex);
  358. #endif
  359. // Init device queue & task
  360. _usbd_q = osal_queue_create(&_usbd_qdef);
  361. TU_ASSERT(_usbd_q);
  362. // Get application driver if available
  363. if ( usbd_app_driver_get_cb )
  364. {
  365. _app_driver = usbd_app_driver_get_cb(&_app_driver_count);
  366. }
  367. // Init class drivers
  368. for (uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++)
  369. {
  370. usbd_class_driver_t const * driver = get_driver(i);
  371. TU_LOG2("%s init\r\n", driver->name);
  372. driver->init();
  373. }
  374. // Init device controller driver
  375. dcd_init(rhport);
  376. dcd_int_enable(rhport);
  377. _usbd_initialized = true;
  378. return true;
  379. }
  380. static void configuration_reset(uint8_t rhport)
  381. {
  382. for ( uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++ )
  383. {
  384. get_driver(i)->reset(rhport);
  385. }
  386. tu_varclr(&_usbd_dev);
  387. memset(_usbd_dev.itf2drv, DRVID_INVALID, sizeof(_usbd_dev.itf2drv)); // invalid mapping
  388. memset(_usbd_dev.ep2drv , DRVID_INVALID, sizeof(_usbd_dev.ep2drv )); // invalid mapping
  389. }
  390. static void usbd_reset(uint8_t rhport)
  391. {
  392. configuration_reset(rhport);
  393. usbd_control_reset();
  394. }
  395. bool tud_task_event_ready(void)
  396. {
  397. // Skip if stack is not initialized
  398. if ( !tusb_inited() ) return false;
  399. return !osal_queue_empty(_usbd_q);
  400. }
  401. /* USB Device Driver task
  402. * This top level thread manages all device controller event and delegates events to class-specific drivers.
  403. * This should be called periodically within the mainloop or rtos thread.
  404. *
  405. @code
  406. int main(void)
  407. {
  408. application_init();
  409. tusb_init();
  410. while(1) // the mainloop
  411. {
  412. application_code();
  413. tud_task(); // tinyusb device task
  414. }
  415. }
  416. @endcode
  417. */
  418. void tud_task (void)
  419. {
  420. // Skip if stack is not initialized
  421. if ( !tusb_inited() ) return;
  422. // Loop until there is no more events in the queue
  423. while (1)
  424. {
  425. dcd_event_t event;
  426. if ( !osal_queue_receive(_usbd_q, &event) ) return;
  427. #if CFG_TUSB_DEBUG >= 2
  428. if (event.event_id == DCD_EVENT_SETUP_RECEIVED) TU_LOG2("\r\n"); // extra line for setup
  429. TU_LOG2("USBD %s ", event.event_id < DCD_EVENT_COUNT ? _usbd_event_str[event.event_id] : "CORRUPTED");
  430. #endif
  431. switch ( event.event_id )
  432. {
  433. case DCD_EVENT_BUS_RESET:
  434. TU_LOG2(": %s Speed\r\n", _tusb_speed_str[event.bus_reset.speed]);
  435. usbd_reset(event.rhport);
  436. _usbd_dev.speed = event.bus_reset.speed;
  437. break;
  438. case DCD_EVENT_UNPLUGGED:
  439. TU_LOG2("\r\n");
  440. usbd_reset(event.rhport);
  441. // invoke callback
  442. if (tud_umount_cb) tud_umount_cb();
  443. break;
  444. case DCD_EVENT_SETUP_RECEIVED:
  445. TU_LOG2_VAR(&event.setup_received);
  446. TU_LOG2("\r\n");
  447. // Mark as connected after receiving 1st setup packet.
  448. // But it is easier to set it every time instead of wasting time to check then set
  449. _usbd_dev.connected = 1;
  450. // mark both in & out control as free
  451. _usbd_dev.ep_status[0][TUSB_DIR_OUT].busy = false;
  452. _usbd_dev.ep_status[0][TUSB_DIR_OUT].claimed = 0;
  453. _usbd_dev.ep_status[0][TUSB_DIR_IN ].busy = false;
  454. _usbd_dev.ep_status[0][TUSB_DIR_IN ].claimed = 0;
  455. // Process control request
  456. if ( !process_control_request(event.rhport, &event.setup_received) )
  457. {
  458. TU_LOG2(" Stall EP0\r\n");
  459. // Failed -> stall both control endpoint IN and OUT
  460. dcd_edpt_stall(event.rhport, 0);
  461. dcd_edpt_stall(event.rhport, 0 | TUSB_DIR_IN_MASK);
  462. }
  463. break;
  464. case DCD_EVENT_XFER_COMPLETE:
  465. {
  466. // Invoke the class callback associated with the endpoint address
  467. uint8_t const ep_addr = event.xfer_complete.ep_addr;
  468. uint8_t const epnum = tu_edpt_number(ep_addr);
  469. uint8_t const ep_dir = tu_edpt_dir(ep_addr);
  470. TU_LOG2("on EP %02X with %u bytes\r\n", ep_addr, (unsigned int) event.xfer_complete.len);
  471. _usbd_dev.ep_status[epnum][ep_dir].busy = false;
  472. _usbd_dev.ep_status[epnum][ep_dir].claimed = 0;
  473. if ( 0 == epnum )
  474. {
  475. usbd_control_xfer_cb(event.rhport, ep_addr, (xfer_result_t)event.xfer_complete.result, event.xfer_complete.len);
  476. }
  477. else
  478. {
  479. usbd_class_driver_t const * driver = get_driver( _usbd_dev.ep2drv[epnum][ep_dir] );
  480. TU_ASSERT(driver, );
  481. TU_LOG2(" %s xfer callback\r\n", driver->name);
  482. driver->xfer_cb(event.rhport, ep_addr, (xfer_result_t)event.xfer_complete.result, event.xfer_complete.len);
  483. }
  484. }
  485. break;
  486. case DCD_EVENT_SUSPEND:
  487. // NOTE: When plugging/unplugging device, the D+/D- state are unstable and
  488. // can accidentally meet the SUSPEND condition ( Bus Idle for 3ms ), which result in a series of event
  489. // e.g suspend -> resume -> unplug/plug. Skip suspend/resume if not connected
  490. if ( _usbd_dev.connected )
  491. {
  492. TU_LOG2(": Remote Wakeup = %u\r\n", _usbd_dev.remote_wakeup_en);
  493. if (tud_suspend_cb) tud_suspend_cb(_usbd_dev.remote_wakeup_en);
  494. }else
  495. {
  496. TU_LOG2(" Skipped\r\n");
  497. }
  498. break;
  499. case DCD_EVENT_RESUME:
  500. if ( _usbd_dev.connected )
  501. {
  502. TU_LOG2("\r\n");
  503. if (tud_resume_cb) tud_resume_cb();
  504. }else
  505. {
  506. TU_LOG2(" Skipped\r\n");
  507. }
  508. break;
  509. case DCD_EVENT_SOF:
  510. TU_LOG2("\r\n");
  511. for ( uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++ )
  512. {
  513. usbd_class_driver_t const * driver = get_driver(i);
  514. if ( driver->sof ) driver->sof(event.rhport);
  515. }
  516. break;
  517. case USBD_EVENT_FUNC_CALL:
  518. TU_LOG2("\r\n");
  519. if ( event.func_call.func ) event.func_call.func(event.func_call.param);
  520. break;
  521. default:
  522. TU_BREAKPOINT();
  523. break;
  524. }
  525. }
  526. }
  527. //--------------------------------------------------------------------+
  528. // Control Request Parser & Handling
  529. //--------------------------------------------------------------------+
  530. // Helper to invoke class driver control request handler
  531. static bool invoke_class_control(uint8_t rhport, usbd_class_driver_t const * driver, tusb_control_request_t const * request)
  532. {
  533. usbd_control_set_complete_callback(driver->control_xfer_cb);
  534. TU_LOG2(" %s control request\r\n", driver->name);
  535. return driver->control_xfer_cb(rhport, CONTROL_STAGE_SETUP, request);
  536. }
  537. // This handles the actual request and its response.
  538. // return false will cause its caller to stall control endpoint
  539. static bool process_control_request(uint8_t rhport, tusb_control_request_t const * p_request)
  540. {
  541. usbd_control_set_complete_callback(NULL);
  542. TU_ASSERT(p_request->bmRequestType_bit.type < TUSB_REQ_TYPE_INVALID);
  543. // Vendor request
  544. if ( p_request->bmRequestType_bit.type == TUSB_REQ_TYPE_VENDOR )
  545. {
  546. TU_VERIFY(tud_vendor_control_xfer_cb);
  547. usbd_control_set_complete_callback(tud_vendor_control_xfer_cb);
  548. return tud_vendor_control_xfer_cb(rhport, CONTROL_STAGE_SETUP, p_request);
  549. }
  550. #if CFG_TUSB_DEBUG >= 2
  551. if (TUSB_REQ_TYPE_STANDARD == p_request->bmRequestType_bit.type && p_request->bRequest <= TUSB_REQ_SYNCH_FRAME)
  552. {
  553. TU_LOG2(" %s", _tusb_std_request_str[p_request->bRequest]);
  554. if (TUSB_REQ_GET_DESCRIPTOR != p_request->bRequest) TU_LOG2("\r\n");
  555. }
  556. #endif
  557. switch ( p_request->bmRequestType_bit.recipient )
  558. {
  559. //------------- Device Requests e.g in enumeration -------------//
  560. case TUSB_REQ_RCPT_DEVICE:
  561. if ( TUSB_REQ_TYPE_CLASS == p_request->bmRequestType_bit.type )
  562. {
  563. uint8_t const itf = tu_u16_low(p_request->wIndex);
  564. TU_VERIFY(itf < TU_ARRAY_SIZE(_usbd_dev.itf2drv));
  565. usbd_class_driver_t const * driver = get_driver(_usbd_dev.itf2drv[itf]);
  566. TU_VERIFY(driver);
  567. // forward to class driver: "non-STD request to Interface"
  568. return invoke_class_control(rhport, driver, p_request);
  569. }
  570. if ( TUSB_REQ_TYPE_STANDARD != p_request->bmRequestType_bit.type )
  571. {
  572. // Non standard request is not supported
  573. TU_BREAKPOINT();
  574. return false;
  575. }
  576. switch ( p_request->bRequest )
  577. {
  578. case TUSB_REQ_SET_ADDRESS:
  579. // Depending on mcu, status phase could be sent either before or after changing device address,
  580. // or even require stack to not response with status at all
  581. // Therefore DCD must take full responsibility to response and include zlp status packet if needed.
  582. usbd_control_set_request(p_request); // set request since DCD has no access to tud_control_status() API
  583. dcd_set_address(rhport, (uint8_t) p_request->wValue);
  584. // skip tud_control_status()
  585. _usbd_dev.addressed = 1;
  586. break;
  587. case TUSB_REQ_GET_CONFIGURATION:
  588. {
  589. uint8_t cfg_num = _usbd_dev.cfg_num;
  590. tud_control_xfer(rhport, p_request, &cfg_num, 1);
  591. }
  592. break;
  593. case TUSB_REQ_SET_CONFIGURATION:
  594. {
  595. uint8_t const cfg_num = (uint8_t) p_request->wValue;
  596. // Only process if new configure is different
  597. if (_usbd_dev.cfg_num != cfg_num)
  598. {
  599. if ( _usbd_dev.cfg_num )
  600. {
  601. // already configured: need to clear all endpoints and driver first
  602. TU_LOG(USBD_DBG, " Clear current Configuration (%u) before switching\r\n", _usbd_dev.cfg_num);
  603. // close all non-control endpoints, cancel all pending transfers if any
  604. dcd_edpt_close_all(rhport);
  605. // close all drivers and current configured state except bus speed
  606. uint8_t const speed = _usbd_dev.speed;
  607. configuration_reset(rhport);
  608. _usbd_dev.speed = speed; // restore speed
  609. }
  610. // switch to new configuration if not zero
  611. if ( cfg_num ) TU_ASSERT( process_set_config(rhport, cfg_num) );
  612. }
  613. _usbd_dev.cfg_num = cfg_num;
  614. tud_control_status(rhport, p_request);
  615. }
  616. break;
  617. case TUSB_REQ_GET_DESCRIPTOR:
  618. TU_VERIFY( process_get_descriptor(rhport, p_request) );
  619. break;
  620. case TUSB_REQ_SET_FEATURE:
  621. // Only support remote wakeup for device feature
  622. TU_VERIFY(TUSB_REQ_FEATURE_REMOTE_WAKEUP == p_request->wValue);
  623. TU_LOG(USBD_DBG, " Enable Remote Wakeup\r\n");
  624. // Host may enable remote wake up before suspending especially HID device
  625. _usbd_dev.remote_wakeup_en = true;
  626. tud_control_status(rhport, p_request);
  627. break;
  628. case TUSB_REQ_CLEAR_FEATURE:
  629. // Only support remote wakeup for device feature
  630. TU_VERIFY(TUSB_REQ_FEATURE_REMOTE_WAKEUP == p_request->wValue);
  631. TU_LOG(USBD_DBG, " Disable Remote Wakeup\r\n");
  632. // Host may disable remote wake up after resuming
  633. _usbd_dev.remote_wakeup_en = false;
  634. tud_control_status(rhport, p_request);
  635. break;
  636. case TUSB_REQ_GET_STATUS:
  637. {
  638. // Device status bit mask
  639. // - Bit 0: Self Powered
  640. // - Bit 1: Remote Wakeup enabled
  641. uint16_t status = (_usbd_dev.self_powered ? 1 : 0) | (_usbd_dev.remote_wakeup_en ? 2 : 0);
  642. tud_control_xfer(rhport, p_request, &status, 2);
  643. }
  644. break;
  645. // Unknown/Unsupported request
  646. default: TU_BREAKPOINT(); return false;
  647. }
  648. break;
  649. //------------- Class/Interface Specific Request -------------//
  650. case TUSB_REQ_RCPT_INTERFACE:
  651. {
  652. uint8_t const itf = tu_u16_low(p_request->wIndex);
  653. TU_VERIFY(itf < TU_ARRAY_SIZE(_usbd_dev.itf2drv));
  654. usbd_class_driver_t const * driver = get_driver(_usbd_dev.itf2drv[itf]);
  655. TU_VERIFY(driver);
  656. // all requests to Interface (STD or Class) is forwarded to class driver.
  657. // notable requests are: GET HID REPORT DESCRIPTOR, SET_INTERFACE, GET_INTERFACE
  658. if ( !invoke_class_control(rhport, driver, p_request) )
  659. {
  660. // For GET_INTERFACE and SET_INTERFACE, it is mandatory to respond even if the class
  661. // driver doesn't use alternate settings or implement this
  662. TU_VERIFY(TUSB_REQ_TYPE_STANDARD == p_request->bmRequestType_bit.type);
  663. switch(p_request->bRequest)
  664. {
  665. case TUSB_REQ_GET_INTERFACE:
  666. case TUSB_REQ_SET_INTERFACE:
  667. // Clear complete callback if driver set since it can also stall the request.
  668. usbd_control_set_complete_callback(NULL);
  669. if (TUSB_REQ_GET_INTERFACE == p_request->bRequest)
  670. {
  671. uint8_t alternate = 0;
  672. tud_control_xfer(rhport, p_request, &alternate, 1);
  673. }else
  674. {
  675. tud_control_status(rhport, p_request);
  676. }
  677. break;
  678. default: return false;
  679. }
  680. }
  681. }
  682. break;
  683. //------------- Endpoint Request -------------//
  684. case TUSB_REQ_RCPT_ENDPOINT:
  685. {
  686. uint8_t const ep_addr = tu_u16_low(p_request->wIndex);
  687. uint8_t const ep_num = tu_edpt_number(ep_addr);
  688. uint8_t const ep_dir = tu_edpt_dir(ep_addr);
  689. TU_ASSERT(ep_num < TU_ARRAY_SIZE(_usbd_dev.ep2drv) );
  690. usbd_class_driver_t const * driver = get_driver(_usbd_dev.ep2drv[ep_num][ep_dir]);
  691. if ( TUSB_REQ_TYPE_STANDARD != p_request->bmRequestType_bit.type )
  692. {
  693. // Forward class request to its driver
  694. TU_VERIFY(driver);
  695. return invoke_class_control(rhport, driver, p_request);
  696. }
  697. else
  698. {
  699. // Handle STD request to endpoint
  700. switch ( p_request->bRequest )
  701. {
  702. case TUSB_REQ_GET_STATUS:
  703. {
  704. uint16_t status = usbd_edpt_stalled(rhport, ep_addr) ? 0x0001 : 0x0000;
  705. tud_control_xfer(rhport, p_request, &status, 2);
  706. }
  707. break;
  708. case TUSB_REQ_CLEAR_FEATURE:
  709. case TUSB_REQ_SET_FEATURE:
  710. {
  711. if ( TUSB_REQ_FEATURE_EDPT_HALT == p_request->wValue )
  712. {
  713. if ( TUSB_REQ_CLEAR_FEATURE == p_request->bRequest )
  714. {
  715. usbd_edpt_clear_stall(rhport, ep_addr);
  716. }else
  717. {
  718. usbd_edpt_stall(rhport, ep_addr);
  719. }
  720. }
  721. if (driver)
  722. {
  723. // Some classes such as USBTMC needs to clear/re-init its buffer when receiving CLEAR_FEATURE request
  724. // We will also forward std request targeted endpoint to class drivers as well
  725. // STD request must always be ACKed regardless of driver returned value
  726. // Also clear complete callback if driver set since it can also stall the request.
  727. (void) invoke_class_control(rhport, driver, p_request);
  728. usbd_control_set_complete_callback(NULL);
  729. // skip ZLP status if driver already did that
  730. if ( !_usbd_dev.ep_status[0][TUSB_DIR_IN].busy ) tud_control_status(rhport, p_request);
  731. }
  732. }
  733. break;
  734. // Unknown/Unsupported request
  735. default: TU_BREAKPOINT(); return false;
  736. }
  737. }
  738. }
  739. break;
  740. // Unknown recipient
  741. default: TU_BREAKPOINT(); return false;
  742. }
  743. return true;
  744. }
  745. // Process Set Configure Request
  746. // This function parse configuration descriptor & open drivers accordingly
  747. static bool process_set_config(uint8_t rhport, uint8_t cfg_num)
  748. {
  749. // index is cfg_num-1
  750. tusb_desc_configuration_t const * desc_cfg = (tusb_desc_configuration_t const *) tud_descriptor_configuration_cb(cfg_num-1);
  751. TU_ASSERT(desc_cfg != NULL && desc_cfg->bDescriptorType == TUSB_DESC_CONFIGURATION);
  752. // Parse configuration descriptor
  753. _usbd_dev.remote_wakeup_support = (desc_cfg->bmAttributes & TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP) ? 1 : 0;
  754. _usbd_dev.self_powered = (desc_cfg->bmAttributes & TUSB_DESC_CONFIG_ATT_SELF_POWERED ) ? 1 : 0;
  755. // Parse interface descriptor
  756. uint8_t const * p_desc = ((uint8_t const*) desc_cfg) + sizeof(tusb_desc_configuration_t);
  757. uint8_t const * desc_end = ((uint8_t const*) desc_cfg) + tu_le16toh(desc_cfg->wTotalLength);
  758. while( p_desc < desc_end )
  759. {
  760. uint8_t assoc_itf_count = 1;
  761. // Class will always starts with Interface Association (if any) and then Interface descriptor
  762. if ( TUSB_DESC_INTERFACE_ASSOCIATION == tu_desc_type(p_desc) )
  763. {
  764. tusb_desc_interface_assoc_t const * desc_iad = (tusb_desc_interface_assoc_t const *) p_desc;
  765. assoc_itf_count = desc_iad->bInterfaceCount;
  766. p_desc = tu_desc_next(p_desc); // next to Interface
  767. // IAD's first interface number and class should match with opened interface
  768. //TU_ASSERT(desc_iad->bFirstInterface == desc_itf->bInterfaceNumber &&
  769. // desc_iad->bFunctionClass == desc_itf->bInterfaceClass);
  770. }
  771. TU_ASSERT( TUSB_DESC_INTERFACE == tu_desc_type(p_desc) );
  772. tusb_desc_interface_t const * desc_itf = (tusb_desc_interface_t const*) p_desc;
  773. // Find driver for this interface
  774. uint16_t const remaining_len = desc_end-p_desc;
  775. uint8_t drv_id;
  776. for (drv_id = 0; drv_id < TOTAL_DRIVER_COUNT; drv_id++)
  777. {
  778. usbd_class_driver_t const *driver = get_driver(drv_id);
  779. uint16_t const drv_len = driver->open(rhport, desc_itf, remaining_len);
  780. if ( (sizeof(tusb_desc_interface_t) <= drv_len) && (drv_len <= remaining_len) )
  781. {
  782. // Open successfully
  783. TU_LOG2(" %s opened\r\n", driver->name);
  784. // Some drivers use 2 or more interfaces but may not have IAD e.g MIDI (always) or
  785. // BTH (even CDC) with class in device descriptor (single interface)
  786. if ( assoc_itf_count == 1)
  787. {
  788. #if CFG_TUD_CDC
  789. if ( driver->open == cdcd_open ) assoc_itf_count = 2;
  790. #endif
  791. #if CFG_TUD_MIDI
  792. if ( driver->open == midid_open ) assoc_itf_count = 2;
  793. #endif
  794. #if CFG_TUD_BTH && CFG_TUD_BTH_ISO_ALT_COUNT
  795. if ( driver->open == btd_open ) assoc_itf_count = 2;
  796. #endif
  797. }
  798. // bind (associated) interfaces to found driver
  799. for(uint8_t i=0; i<assoc_itf_count; i++)
  800. {
  801. uint8_t const itf_num = desc_itf->bInterfaceNumber+i;
  802. // Interface number must not be used already
  803. TU_ASSERT(DRVID_INVALID == _usbd_dev.itf2drv[itf_num]);
  804. _usbd_dev.itf2drv[itf_num] = drv_id;
  805. }
  806. // bind all endpoints to found driver
  807. tu_edpt_bind_driver(_usbd_dev.ep2drv, desc_itf, drv_len, drv_id);
  808. // next Interface
  809. p_desc += drv_len;
  810. break; // exit driver find loop
  811. }
  812. }
  813. // Failed if there is no supported drivers
  814. TU_ASSERT(drv_id < TOTAL_DRIVER_COUNT);
  815. }
  816. // invoke callback
  817. if (tud_mount_cb) tud_mount_cb();
  818. return true;
  819. }
  820. // return descriptor's buffer and update desc_len
  821. static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const * p_request)
  822. {
  823. tusb_desc_type_t const desc_type = (tusb_desc_type_t) tu_u16_high(p_request->wValue);
  824. uint8_t const desc_index = tu_u16_low( p_request->wValue );
  825. switch(desc_type)
  826. {
  827. case TUSB_DESC_DEVICE:
  828. {
  829. TU_LOG2(" Device\r\n");
  830. void* desc_device = (void*) (uintptr_t) tud_descriptor_device_cb();
  831. // Only response with exactly 1 Packet if: not addressed and host requested more data than device descriptor has.
  832. // This only happens with the very first get device descriptor and EP0 size = 8 or 16.
  833. if ((CFG_TUD_ENDPOINT0_SIZE < sizeof(tusb_desc_device_t)) && !_usbd_dev.addressed &&
  834. ((tusb_control_request_t const*) p_request)->wLength > sizeof(tusb_desc_device_t))
  835. {
  836. // Hack here: we modify the request length to prevent usbd_control response with zlp
  837. // since we are responding with 1 packet & less data than wLength.
  838. tusb_control_request_t mod_request = *p_request;
  839. mod_request.wLength = CFG_TUD_ENDPOINT0_SIZE;
  840. return tud_control_xfer(rhport, &mod_request, desc_device, CFG_TUD_ENDPOINT0_SIZE);
  841. }else
  842. {
  843. return tud_control_xfer(rhport, p_request, desc_device, sizeof(tusb_desc_device_t));
  844. }
  845. }
  846. break;
  847. case TUSB_DESC_BOS:
  848. {
  849. TU_LOG2(" BOS\r\n");
  850. // requested by host if USB > 2.0 ( i.e 2.1 or 3.x )
  851. if (!tud_descriptor_bos_cb) return false;
  852. uintptr_t desc_bos = (uintptr_t) tud_descriptor_bos_cb();
  853. TU_ASSERT(desc_bos);
  854. // Use offsetof to avoid pointer to the odd/misaligned address
  855. uint16_t const total_len = tu_le16toh( tu_unaligned_read16((const void*) (desc_bos + offsetof(tusb_desc_bos_t, wTotalLength))) );
  856. return tud_control_xfer(rhport, p_request, (void*) desc_bos, total_len);
  857. }
  858. break;
  859. case TUSB_DESC_CONFIGURATION:
  860. case TUSB_DESC_OTHER_SPEED_CONFIG:
  861. {
  862. uintptr_t desc_config;
  863. if ( desc_type == TUSB_DESC_CONFIGURATION )
  864. {
  865. TU_LOG2(" Configuration[%u]\r\n", desc_index);
  866. desc_config = (uintptr_t) tud_descriptor_configuration_cb(desc_index);
  867. }else
  868. {
  869. // Host only request this after getting Device Qualifier descriptor
  870. TU_LOG2(" Other Speed Configuration\r\n");
  871. TU_VERIFY( tud_descriptor_other_speed_configuration_cb );
  872. desc_config = (uintptr_t) tud_descriptor_other_speed_configuration_cb(desc_index);
  873. }
  874. TU_ASSERT(desc_config);
  875. // Use offsetof to avoid pointer to the odd/misaligned address
  876. uint16_t const total_len = tu_le16toh( tu_unaligned_read16((const void*) (desc_config + offsetof(tusb_desc_configuration_t, wTotalLength))) );
  877. return tud_control_xfer(rhport, p_request, (void*) desc_config, total_len);
  878. }
  879. break;
  880. case TUSB_DESC_STRING:
  881. {
  882. TU_LOG2(" String[%u]\r\n", desc_index);
  883. // String Descriptor always uses the desc set from user
  884. uint8_t const* desc_str = (uint8_t const*) tud_descriptor_string_cb(desc_index, tu_le16toh(p_request->wIndex));
  885. TU_VERIFY(desc_str);
  886. // first byte of descriptor is its size
  887. return tud_control_xfer(rhport, p_request, (void*) (uintptr_t) desc_str, tu_desc_len(desc_str));
  888. }
  889. break;
  890. case TUSB_DESC_DEVICE_QUALIFIER:
  891. {
  892. TU_LOG2(" Device Qualifier\r\n");
  893. TU_VERIFY( tud_descriptor_device_qualifier_cb );
  894. uint8_t const* desc_qualifier = tud_descriptor_device_qualifier_cb();
  895. TU_VERIFY(desc_qualifier);
  896. // first byte of descriptor is its size
  897. return tud_control_xfer(rhport, p_request, (void*) (uintptr_t) desc_qualifier, tu_desc_len(desc_qualifier));
  898. }
  899. break;
  900. default: return false;
  901. }
  902. }
  903. //--------------------------------------------------------------------+
  904. // DCD Event Handler
  905. //--------------------------------------------------------------------+
  906. void dcd_event_handler(dcd_event_t const * event, bool in_isr)
  907. {
  908. switch (event->event_id)
  909. {
  910. case DCD_EVENT_UNPLUGGED:
  911. _usbd_dev.connected = 0;
  912. _usbd_dev.addressed = 0;
  913. _usbd_dev.cfg_num = 0;
  914. _usbd_dev.suspended = 0;
  915. osal_queue_send(_usbd_q, event, in_isr);
  916. break;
  917. case DCD_EVENT_SUSPEND:
  918. // NOTE: When plugging/unplugging device, the D+/D- state are unstable and
  919. // can accidentally meet the SUSPEND condition ( Bus Idle for 3ms ).
  920. // In addition, some MCUs such as SAMD or boards that haven no VBUS detection cannot distinguish
  921. // suspended vs disconnected. We will skip handling SUSPEND/RESUME event if not currently connected
  922. if ( _usbd_dev.connected )
  923. {
  924. _usbd_dev.suspended = 1;
  925. osal_queue_send(_usbd_q, event, in_isr);
  926. }
  927. break;
  928. case DCD_EVENT_RESUME:
  929. // skip event if not connected (especially required for SAMD)
  930. if ( _usbd_dev.connected )
  931. {
  932. _usbd_dev.suspended = 0;
  933. osal_queue_send(_usbd_q, event, in_isr);
  934. }
  935. break;
  936. case DCD_EVENT_SOF:
  937. // Some MCUs after running dcd_remote_wakeup() does not have way to detect the end of remote wakeup
  938. // which last 1-15 ms. DCD can use SOF as a clear indicator that bus is back to operational
  939. if ( _usbd_dev.suspended )
  940. {
  941. _usbd_dev.suspended = 0;
  942. dcd_event_t const event_resume = { .rhport = event->rhport, .event_id = DCD_EVENT_RESUME };
  943. osal_queue_send(_usbd_q, &event_resume, in_isr);
  944. }
  945. break;
  946. default:
  947. osal_queue_send(_usbd_q, event, in_isr);
  948. break;
  949. }
  950. }
  951. void dcd_event_bus_signal (uint8_t rhport, dcd_eventid_t eid, bool in_isr)
  952. {
  953. dcd_event_t event = { .rhport = rhport, .event_id = eid };
  954. dcd_event_handler(&event, in_isr);
  955. }
  956. void dcd_event_bus_reset (uint8_t rhport, tusb_speed_t speed, bool in_isr)
  957. {
  958. dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_BUS_RESET };
  959. event.bus_reset.speed = speed;
  960. dcd_event_handler(&event, in_isr);
  961. }
  962. void dcd_event_setup_received(uint8_t rhport, uint8_t const * setup, bool in_isr)
  963. {
  964. dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_SETUP_RECEIVED };
  965. memcpy(&event.setup_received, setup, 8);
  966. dcd_event_handler(&event, in_isr);
  967. }
  968. void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, uint8_t result, bool in_isr)
  969. {
  970. dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_XFER_COMPLETE };
  971. event.xfer_complete.ep_addr = ep_addr;
  972. event.xfer_complete.len = xferred_bytes;
  973. event.xfer_complete.result = result;
  974. dcd_event_handler(&event, in_isr);
  975. }
  976. //--------------------------------------------------------------------+
  977. // USBD API For Class Driver
  978. //--------------------------------------------------------------------+
  979. // Parse consecutive endpoint descriptors (IN & OUT)
  980. 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)
  981. {
  982. for(int i=0; i<ep_count; i++)
  983. {
  984. tusb_desc_endpoint_t const * desc_ep = (tusb_desc_endpoint_t const *) p_desc;
  985. TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType && xfer_type == desc_ep->bmAttributes.xfer);
  986. TU_ASSERT(usbd_edpt_open(rhport, desc_ep));
  987. if ( tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN )
  988. {
  989. (*ep_in) = desc_ep->bEndpointAddress;
  990. }else
  991. {
  992. (*ep_out) = desc_ep->bEndpointAddress;
  993. }
  994. p_desc = tu_desc_next(p_desc);
  995. }
  996. return true;
  997. }
  998. // Helper to defer an isr function
  999. void usbd_defer_func(osal_task_func_t func, void* param, bool in_isr)
  1000. {
  1001. dcd_event_t event =
  1002. {
  1003. .rhport = 0,
  1004. .event_id = USBD_EVENT_FUNC_CALL,
  1005. };
  1006. event.func_call.func = func;
  1007. event.func_call.param = param;
  1008. dcd_event_handler(&event, in_isr);
  1009. }
  1010. //--------------------------------------------------------------------+
  1011. // USBD Endpoint API
  1012. //--------------------------------------------------------------------+
  1013. bool usbd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const * desc_ep)
  1014. {
  1015. TU_ASSERT(tu_edpt_number(desc_ep->bEndpointAddress) < CFG_TUD_ENDPPOINT_MAX);
  1016. TU_ASSERT(tu_edpt_validate(desc_ep, (tusb_speed_t) _usbd_dev.speed));
  1017. return dcd_edpt_open(rhport, desc_ep);
  1018. }
  1019. bool usbd_edpt_claim(uint8_t rhport, uint8_t ep_addr)
  1020. {
  1021. (void) rhport;
  1022. // TODO add this check later, also make sure we don't starve an out endpoint while suspending
  1023. // TU_VERIFY(tud_ready());
  1024. uint8_t const epnum = tu_edpt_number(ep_addr);
  1025. uint8_t const dir = tu_edpt_dir(ep_addr);
  1026. #if CFG_TUSB_OS != OPT_OS_NONE
  1027. // pre-check to help reducing mutex lock
  1028. TU_VERIFY((_usbd_dev.ep_status[epnum][dir].busy == 0) && (_usbd_dev.ep_status[epnum][dir].claimed == 0));
  1029. osal_mutex_lock(_usbd_mutex, OSAL_TIMEOUT_WAIT_FOREVER);
  1030. #endif
  1031. // can only claim the endpoint if it is not busy and not claimed yet.
  1032. bool const ret = (_usbd_dev.ep_status[epnum][dir].busy == 0) && (_usbd_dev.ep_status[epnum][dir].claimed == 0);
  1033. if (ret)
  1034. {
  1035. _usbd_dev.ep_status[epnum][dir].claimed = 1;
  1036. }
  1037. #if CFG_TUSB_OS != OPT_OS_NONE
  1038. osal_mutex_unlock(_usbd_mutex);
  1039. #endif
  1040. return ret;
  1041. }
  1042. bool usbd_edpt_release(uint8_t rhport, uint8_t ep_addr)
  1043. {
  1044. (void) rhport;
  1045. uint8_t const epnum = tu_edpt_number(ep_addr);
  1046. uint8_t const dir = tu_edpt_dir(ep_addr);
  1047. #if CFG_TUSB_OS != OPT_OS_NONE
  1048. osal_mutex_lock(_usbd_mutex, OSAL_TIMEOUT_WAIT_FOREVER);
  1049. #endif
  1050. // can only release the endpoint if it is claimed and not busy
  1051. bool const ret = (_usbd_dev.ep_status[epnum][dir].busy == 0) && (_usbd_dev.ep_status[epnum][dir].claimed == 1);
  1052. if (ret)
  1053. {
  1054. _usbd_dev.ep_status[epnum][dir].claimed = 0;
  1055. }
  1056. #if CFG_TUSB_OS != OPT_OS_NONE
  1057. osal_mutex_unlock(_usbd_mutex);
  1058. #endif
  1059. return ret;
  1060. }
  1061. bool usbd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes)
  1062. {
  1063. uint8_t const epnum = tu_edpt_number(ep_addr);
  1064. uint8_t const dir = tu_edpt_dir(ep_addr);
  1065. // TODO skip ready() check for now since enumeration also use this API
  1066. // TU_VERIFY(tud_ready());
  1067. TU_LOG2(" Queue EP %02X with %u bytes ...\r\n", ep_addr, total_bytes);
  1068. // Attempt to transfer on a busy endpoint, sound like an race condition !
  1069. TU_ASSERT(_usbd_dev.ep_status[epnum][dir].busy == 0);
  1070. // Set busy first since the actual transfer can be complete before dcd_edpt_xfer()
  1071. // could return and USBD task can preempt and clear the busy
  1072. _usbd_dev.ep_status[epnum][dir].busy = true;
  1073. if ( dcd_edpt_xfer(rhport, ep_addr, buffer, total_bytes) )
  1074. {
  1075. return true;
  1076. }else
  1077. {
  1078. // DCD error, mark endpoint as ready to allow next transfer
  1079. _usbd_dev.ep_status[epnum][dir].busy = false;
  1080. _usbd_dev.ep_status[epnum][dir].claimed = 0;
  1081. TU_LOG2("FAILED\r\n");
  1082. TU_BREAKPOINT();
  1083. return false;
  1084. }
  1085. }
  1086. // The number of bytes has to be given explicitly to allow more flexible control of how many
  1087. // bytes should be written and second to keep the return value free to give back a boolean
  1088. // success message. If total_bytes is too big, the FIFO will copy only what is available
  1089. // into the USB buffer!
  1090. bool usbd_edpt_xfer_fifo(uint8_t rhport, uint8_t ep_addr, tu_fifo_t * ff, uint16_t total_bytes)
  1091. {
  1092. uint8_t const epnum = tu_edpt_number(ep_addr);
  1093. uint8_t const dir = tu_edpt_dir(ep_addr);
  1094. TU_LOG2(" Queue ISO EP %02X with %u bytes ... ", ep_addr, total_bytes);
  1095. // Attempt to transfer on a busy endpoint, sound like an race condition !
  1096. TU_ASSERT(_usbd_dev.ep_status[epnum][dir].busy == 0);
  1097. // Set busy first since the actual transfer can be complete before dcd_edpt_xfer() could return
  1098. // and usbd task can preempt and clear the busy
  1099. _usbd_dev.ep_status[epnum][dir].busy = true;
  1100. if (dcd_edpt_xfer_fifo(rhport, ep_addr, ff, total_bytes))
  1101. {
  1102. TU_LOG2("OK\r\n");
  1103. return true;
  1104. }else
  1105. {
  1106. // DCD error, mark endpoint as ready to allow next transfer
  1107. _usbd_dev.ep_status[epnum][dir].busy = false;
  1108. _usbd_dev.ep_status[epnum][dir].claimed = 0;
  1109. TU_LOG2("failed\r\n");
  1110. TU_BREAKPOINT();
  1111. return false;
  1112. }
  1113. }
  1114. bool usbd_edpt_busy(uint8_t rhport, uint8_t ep_addr)
  1115. {
  1116. (void) rhport;
  1117. uint8_t const epnum = tu_edpt_number(ep_addr);
  1118. uint8_t const dir = tu_edpt_dir(ep_addr);
  1119. return _usbd_dev.ep_status[epnum][dir].busy;
  1120. }
  1121. void usbd_edpt_stall(uint8_t rhport, uint8_t ep_addr)
  1122. {
  1123. uint8_t const epnum = tu_edpt_number(ep_addr);
  1124. uint8_t const dir = tu_edpt_dir(ep_addr);
  1125. // only stalled if currently cleared
  1126. if ( !_usbd_dev.ep_status[epnum][dir].stalled )
  1127. {
  1128. TU_LOG(USBD_DBG, " Stall EP %02X\r\n", ep_addr);
  1129. dcd_edpt_stall(rhport, ep_addr);
  1130. _usbd_dev.ep_status[epnum][dir].stalled = true;
  1131. _usbd_dev.ep_status[epnum][dir].busy = true;
  1132. }
  1133. }
  1134. void usbd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr)
  1135. {
  1136. uint8_t const epnum = tu_edpt_number(ep_addr);
  1137. uint8_t const dir = tu_edpt_dir(ep_addr);
  1138. // only clear if currently stalled
  1139. if ( _usbd_dev.ep_status[epnum][dir].stalled )
  1140. {
  1141. TU_LOG(USBD_DBG, " Clear Stall EP %02X\r\n", ep_addr);
  1142. dcd_edpt_clear_stall(rhport, ep_addr);
  1143. _usbd_dev.ep_status[epnum][dir].stalled = false;
  1144. _usbd_dev.ep_status[epnum][dir].busy = false;
  1145. }
  1146. }
  1147. bool usbd_edpt_stalled(uint8_t rhport, uint8_t ep_addr)
  1148. {
  1149. (void) rhport;
  1150. uint8_t const epnum = tu_edpt_number(ep_addr);
  1151. uint8_t const dir = tu_edpt_dir(ep_addr);
  1152. return _usbd_dev.ep_status[epnum][dir].stalled;
  1153. }
  1154. /**
  1155. * usbd_edpt_close will disable an endpoint.
  1156. *
  1157. * In progress transfers on this EP may be delivered after this call.
  1158. *
  1159. */
  1160. void usbd_edpt_close(uint8_t rhport, uint8_t ep_addr)
  1161. {
  1162. TU_ASSERT(dcd_edpt_close, /**/);
  1163. TU_LOG2(" CLOSING Endpoint: 0x%02X\r\n", ep_addr);
  1164. uint8_t const epnum = tu_edpt_number(ep_addr);
  1165. uint8_t const dir = tu_edpt_dir(ep_addr);
  1166. dcd_edpt_close(rhport, ep_addr);
  1167. _usbd_dev.ep_status[epnum][dir].stalled = false;
  1168. _usbd_dev.ep_status[epnum][dir].busy = false;
  1169. _usbd_dev.ep_status[epnum][dir].claimed = false;
  1170. return;
  1171. }
  1172. #endif