usbd.c 40 KB

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