usbd_core.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. /*
  2. * Copyright (c) 2022, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbd_core.h"
  7. /* general descriptor field offsets */
  8. #define DESC_bLength 0 /** Length offset */
  9. #define DESC_bDescriptorType 1 /** Descriptor type offset */
  10. /* config descriptor field offsets */
  11. #define CONF_DESC_wTotalLength 2 /** Total length offset */
  12. #define CONF_DESC_bConfigurationValue 5 /** Configuration value offset */
  13. #define CONF_DESC_bmAttributes 7 /** configuration characteristics */
  14. /* interface descriptor field offsets */
  15. #define INTF_DESC_bInterfaceNumber 2 /** Interface number offset */
  16. #define INTF_DESC_bAlternateSetting 3 /** Alternate setting offset */
  17. struct usbd_tx_rx_msg {
  18. uint8_t ep;
  19. uint8_t ep_mult;
  20. uint16_t ep_mps;
  21. uint32_t nbytes;
  22. usbd_endpoint_callback cb;
  23. };
  24. USB_NOCACHE_RAM_SECTION struct usbd_core_priv {
  25. /** Setup packet */
  26. USB_MEM_ALIGNX struct usb_setup_packet setup;
  27. /** Pointer to data buffer */
  28. uint8_t *ep0_data_buf;
  29. /** Remaining bytes in buffer */
  30. uint32_t ep0_data_buf_residue;
  31. /** Total length of control transfer */
  32. uint32_t ep0_data_buf_len;
  33. /** Zero length packet flag of control transfer */
  34. bool zlp_flag;
  35. /** Pointer to registered descriptors */
  36. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  37. const struct usb_descriptor *descriptors;
  38. #else
  39. const uint8_t *descriptors;
  40. struct usb_msosv1_descriptor *msosv1_desc;
  41. struct usb_msosv2_descriptor *msosv2_desc;
  42. struct usb_bos_descriptor *bos_desc;
  43. #endif
  44. /* Buffer used for storing standard, class and vendor request data */
  45. USB_MEM_ALIGNX uint8_t req_data[CONFIG_USBDEV_REQUEST_BUFFER_LEN];
  46. /** Currently selected configuration */
  47. uint8_t configuration;
  48. bool self_powered;
  49. bool remote_wakeup_support;
  50. bool remote_wakeup_enabled;
  51. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  52. uint8_t speed;
  53. #endif
  54. #ifdef CONFIG_USBDEV_TEST_MODE
  55. bool test_req;
  56. #endif
  57. struct usbd_interface *intf[16];
  58. uint8_t intf_offset;
  59. struct usbd_tx_rx_msg tx_msg[CONFIG_USBDEV_EP_NUM];
  60. struct usbd_tx_rx_msg rx_msg[CONFIG_USBDEV_EP_NUM];
  61. void (*event_handler)(uint8_t busid, uint8_t event);
  62. } g_usbd_core[CONFIG_USBDEV_MAX_BUS];
  63. struct usbd_bus g_usbdev_bus[CONFIG_USBDEV_MAX_BUS];
  64. static void usbd_class_event_notify_handler(uint8_t busid, uint8_t event, void *arg);
  65. static void usbd_print_setup(struct usb_setup_packet *setup)
  66. {
  67. USB_LOG_INFO("Setup: "
  68. "bmRequestType 0x%02x, bRequest 0x%02x, wValue 0x%04x, wIndex 0x%04x, wLength 0x%04x\r\n",
  69. setup->bmRequestType,
  70. setup->bRequest,
  71. setup->wValue,
  72. setup->wIndex,
  73. setup->wLength);
  74. }
  75. static bool is_device_configured(uint8_t busid)
  76. {
  77. return (g_usbd_core[busid].configuration != 0);
  78. }
  79. /**
  80. * @brief configure and enable endpoint
  81. *
  82. * This function sets endpoint configuration according to one specified in USB
  83. * endpoint descriptor and then enables it for data transfers.
  84. *
  85. * @param [in] ep Endpoint descriptor byte array
  86. *
  87. * @return true if successfully configured and enabled
  88. */
  89. static bool usbd_set_endpoint(uint8_t busid, const struct usb_endpoint_descriptor *ep)
  90. {
  91. USB_LOG_DBG("Open ep:0x%02x type:%u mps:%u\r\n",
  92. ep->bEndpointAddress,
  93. USB_GET_ENDPOINT_TYPE(ep->bmAttributes),
  94. USB_GET_MAXPACKETSIZE(ep->wMaxPacketSize));
  95. if (ep->bEndpointAddress & 0x80) {
  96. g_usbd_core[busid].tx_msg[ep->bEndpointAddress & 0x7f].ep_mps = USB_GET_MAXPACKETSIZE(ep->wMaxPacketSize);
  97. g_usbd_core[busid].tx_msg[ep->bEndpointAddress & 0x7f].ep_mult = USB_GET_MULT(ep->wMaxPacketSize);
  98. } else {
  99. g_usbd_core[busid].rx_msg[ep->bEndpointAddress & 0x7f].ep_mps = USB_GET_MAXPACKETSIZE(ep->wMaxPacketSize);
  100. g_usbd_core[busid].rx_msg[ep->bEndpointAddress & 0x7f].ep_mult = USB_GET_MULT(ep->wMaxPacketSize);
  101. }
  102. return usbd_ep_open(busid, ep) == 0 ? true : false;
  103. }
  104. /**
  105. * @brief Disable endpoint for transferring data
  106. *
  107. * This function cancels transfers that are associated with endpoint and
  108. * disabled endpoint itself.
  109. *
  110. * @param [in] ep Endpoint descriptor byte array
  111. *
  112. * @return true if successfully deconfigured and disabled
  113. */
  114. static bool usbd_reset_endpoint(uint8_t busid, const struct usb_endpoint_descriptor *ep)
  115. {
  116. USB_LOG_DBG("Close ep:0x%02x type:%u\r\n",
  117. ep->bEndpointAddress,
  118. USB_GET_ENDPOINT_TYPE(ep->bmAttributes));
  119. return usbd_ep_close(busid, ep->bEndpointAddress) == 0 ? true : false;
  120. }
  121. /**
  122. * @brief get specified USB descriptor
  123. *
  124. * This function parses the list of installed USB descriptors and attempts
  125. * to find the specified USB descriptor.
  126. *
  127. * @param [in] type_index Type and index of the descriptor
  128. * @param [out] data Descriptor data
  129. * @param [out] len Descriptor length
  130. *
  131. * @return true if the descriptor was found, false otherwise
  132. */
  133. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  134. static bool usbd_get_descriptor(uint8_t busid, uint16_t type_index, uint8_t **data, uint32_t *len)
  135. {
  136. uint8_t type = 0U;
  137. uint8_t index = 0U;
  138. bool found = true;
  139. uint32_t desc_len = 0;
  140. const char *string = NULL;
  141. const uint8_t *desc = NULL;
  142. type = HI_BYTE(type_index);
  143. index = LO_BYTE(type_index);
  144. switch (type) {
  145. case USB_DESCRIPTOR_TYPE_DEVICE:
  146. g_usbd_core[busid].speed = usbd_get_port_speed(busid); /* before we get device descriptor, we have known steady port speed */
  147. desc = g_usbd_core[busid].descriptors->device_descriptor_callback(g_usbd_core[busid].speed);
  148. if (desc == NULL) {
  149. found = false;
  150. break;
  151. }
  152. desc_len = desc[0];
  153. break;
  154. case USB_DESCRIPTOR_TYPE_CONFIGURATION:
  155. desc = g_usbd_core[busid].descriptors->config_descriptor_callback(g_usbd_core[busid].speed);
  156. if (desc == NULL) {
  157. found = false;
  158. break;
  159. }
  160. desc_len = ((desc[CONF_DESC_wTotalLength]) | (desc[CONF_DESC_wTotalLength + 1] << 8));
  161. g_usbd_core[busid].self_powered = (desc[7] & USB_CONFIG_SELF_POWERED) ? true : false;
  162. g_usbd_core[busid].remote_wakeup_support = (desc[7] & USB_CONFIG_REMOTE_WAKEUP) ? true : false;
  163. break;
  164. case USB_DESCRIPTOR_TYPE_STRING:
  165. if (index == USB_OSDESC_STRING_DESC_INDEX) {
  166. if (!g_usbd_core[busid].descriptors->msosv1_descriptor) {
  167. found = false;
  168. break;
  169. }
  170. desc = (uint8_t *)g_usbd_core[busid].descriptors->msosv1_descriptor->string;
  171. desc_len = g_usbd_core[busid].descriptors->msosv1_descriptor->string[0];
  172. } else {
  173. string = g_usbd_core[busid].descriptors->string_descriptor_callback(g_usbd_core[busid].speed, index);
  174. if (string == NULL) {
  175. found = false;
  176. break;
  177. }
  178. if (index == USB_STRING_LANGID_INDEX) {
  179. (*data)[0] = 4;
  180. (*data)[1] = USB_DESCRIPTOR_TYPE_STRING;
  181. (*data)[2] = string[0];
  182. (*data)[3] = string[1];
  183. *len = 4;
  184. return true;
  185. }
  186. uint16_t str_size = strlen(string);
  187. uint16_t total_size = 2 * str_size + 2;
  188. if (total_size > CONFIG_USBDEV_REQUEST_BUFFER_LEN) {
  189. USB_LOG_ERR("string size overflow\r\n");
  190. return false;
  191. }
  192. (*data)[0] = total_size;
  193. (*data)[1] = USB_DESCRIPTOR_TYPE_STRING;
  194. for (uint16_t i = 0; i < str_size; i++) {
  195. (*data)[2 * i + 2] = string[i];
  196. (*data)[2 * i + 3] = 0x00;
  197. }
  198. *len = total_size;
  199. return true;
  200. }
  201. break;
  202. case USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER:
  203. #ifndef CONFIG_USB_HS
  204. return false;
  205. #else
  206. desc = g_usbd_core[busid].descriptors->device_quality_descriptor_callback(g_usbd_core[busid].speed);
  207. if (desc == NULL) {
  208. found = false;
  209. break;
  210. }
  211. desc_len = desc[0];
  212. break;
  213. #endif
  214. case USB_DESCRIPTOR_TYPE_OTHER_SPEED:
  215. desc = g_usbd_core[busid].descriptors->other_speed_descriptor_callback(g_usbd_core[busid].speed);
  216. if (desc == NULL) {
  217. found = false;
  218. break;
  219. }
  220. desc_len = ((desc[CONF_DESC_wTotalLength]) | (desc[CONF_DESC_wTotalLength + 1] << 8));
  221. break;
  222. case USB_DESCRIPTOR_TYPE_BINARY_OBJECT_STORE:
  223. if (!g_usbd_core[busid].descriptors->bos_descriptor) {
  224. found = false;
  225. break;
  226. }
  227. desc = (uint8_t *)g_usbd_core[busid].descriptors->bos_descriptor->string;
  228. desc_len = g_usbd_core[busid].descriptors->bos_descriptor->string_len;
  229. break;
  230. default:
  231. found = false;
  232. break;
  233. }
  234. if (found == false) {
  235. /* nothing found */
  236. USB_LOG_ERR("descriptor <type:%x,index:%x> not found!\r\n", type, index);
  237. } else {
  238. *data = (uint8_t *)desc;
  239. //memcpy(*data, desc, desc_len);
  240. *len = desc_len;
  241. }
  242. return found;
  243. }
  244. #else
  245. static bool usbd_get_descriptor(uint8_t busid, uint16_t type_index, uint8_t **data, uint32_t *len)
  246. {
  247. uint8_t type = 0U;
  248. uint8_t index = 0U;
  249. uint8_t *p = NULL;
  250. uint32_t cur_index = 0U;
  251. bool found = false;
  252. type = HI_BYTE(type_index);
  253. index = LO_BYTE(type_index);
  254. if ((type == USB_DESCRIPTOR_TYPE_STRING) && (index == USB_OSDESC_STRING_DESC_INDEX)) {
  255. if (!g_usbd_core[busid].msosv1_desc) {
  256. return false;
  257. }
  258. *data = (uint8_t *)g_usbd_core[busid].msosv1_desc->string;
  259. //memcpy(*data, (uint8_t *)g_usbd_core[busid].msosv1_desc->string, g_usbd_core[busid].msosv1_desc->string[0]);
  260. *len = g_usbd_core[busid].msosv1_desc->string[0];
  261. return true;
  262. } else if (type == USB_DESCRIPTOR_TYPE_BINARY_OBJECT_STORE) {
  263. if (!g_usbd_core[busid].bos_desc) {
  264. return false;
  265. }
  266. *data = (uint8_t *)g_usbd_core[busid].bos_desc->string;
  267. //memcpy(*data, (uint8_t *)g_usbd_core[busid].bos_desc->string, g_usbd_core[busid].bos_desc->string_len);
  268. *len = g_usbd_core[busid].bos_desc->string_len;
  269. return true;
  270. }
  271. /*
  272. * Invalid types of descriptors,
  273. * see USB Spec. Revision 2.0, 9.4.3 Get Descriptor
  274. */
  275. else if ((type == USB_DESCRIPTOR_TYPE_INTERFACE) || (type == USB_DESCRIPTOR_TYPE_ENDPOINT) ||
  276. #ifndef CONFIG_USB_HS
  277. (type > USB_DESCRIPTOR_TYPE_ENDPOINT)) {
  278. #else
  279. (type > USB_DESCRIPTOR_TYPE_OTHER_SPEED)) {
  280. #endif
  281. return false;
  282. }
  283. p = (uint8_t *)g_usbd_core[busid].descriptors;
  284. cur_index = 0U;
  285. while (p[DESC_bLength] != 0U) {
  286. if (p[DESC_bDescriptorType] == type) {
  287. if (cur_index == index) {
  288. found = true;
  289. break;
  290. }
  291. cur_index++;
  292. }
  293. /* skip to next descriptor */
  294. p += p[DESC_bLength];
  295. }
  296. if (found) {
  297. if ((type == USB_DESCRIPTOR_TYPE_CONFIGURATION) || ((type == USB_DESCRIPTOR_TYPE_OTHER_SPEED))) {
  298. /* configuration or other speed descriptor is an
  299. * exception, length is at offset 2 and 3
  300. */
  301. *len = (p[CONF_DESC_wTotalLength]) |
  302. (p[CONF_DESC_wTotalLength + 1] << 8);
  303. g_usbd_core[busid].self_powered = (p[7] & USB_CONFIG_SELF_POWERED) ? true : false;
  304. g_usbd_core[busid].remote_wakeup_support = (p[7] & USB_CONFIG_REMOTE_WAKEUP) ? true : false;
  305. } else {
  306. /* normally length is at offset 0 */
  307. *len = p[DESC_bLength];
  308. }
  309. *data = p;
  310. //memcpy(*data, p, *len);
  311. } else {
  312. /* nothing found */
  313. USB_LOG_ERR("descriptor <type:0x%02x,index:0x%02x> not found!\r\n", type, index);
  314. }
  315. return found;
  316. }
  317. #endif
  318. /**
  319. * @brief set USB configuration
  320. *
  321. * This function configures the device according to the specified configuration
  322. * index and alternate setting by parsing the installed USB descriptor list.
  323. * A configuration index of 0 unconfigures the device.
  324. *
  325. * @param [in] config_index Configuration index
  326. * @param [in] alt_setting Alternate setting number
  327. *
  328. * @return true if successfully configured false if error or unconfigured
  329. */
  330. static bool usbd_set_configuration(uint8_t busid, uint8_t config_index, uint8_t alt_setting)
  331. {
  332. uint8_t cur_alt_setting = 0xFF;
  333. uint8_t cur_config = 0xFF;
  334. bool found = false;
  335. const uint8_t *p;
  336. uint32_t desc_len = 0;
  337. uint32_t current_desc_len = 0;
  338. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  339. p = g_usbd_core[busid].descriptors->config_descriptor_callback(g_usbd_core[busid].speed);
  340. #else
  341. p = (uint8_t *)g_usbd_core[busid].descriptors;
  342. #endif
  343. /* configure endpoints for this configuration/altsetting */
  344. while (p[DESC_bLength] != 0U) {
  345. switch (p[DESC_bDescriptorType]) {
  346. case USB_DESCRIPTOR_TYPE_CONFIGURATION:
  347. /* remember current configuration index */
  348. cur_config = p[CONF_DESC_bConfigurationValue];
  349. if (cur_config == config_index) {
  350. found = true;
  351. current_desc_len = 0;
  352. desc_len = (p[CONF_DESC_wTotalLength]) |
  353. (p[CONF_DESC_wTotalLength + 1] << 8);
  354. }
  355. break;
  356. case USB_DESCRIPTOR_TYPE_INTERFACE:
  357. /* remember current alternate setting */
  358. cur_alt_setting =
  359. p[INTF_DESC_bAlternateSetting];
  360. break;
  361. case USB_DESCRIPTOR_TYPE_ENDPOINT:
  362. if ((cur_config != config_index) ||
  363. (cur_alt_setting != alt_setting)) {
  364. break;
  365. }
  366. found = usbd_set_endpoint(busid, (struct usb_endpoint_descriptor *)p);
  367. break;
  368. default:
  369. break;
  370. }
  371. /* skip to next descriptor */
  372. p += p[DESC_bLength];
  373. current_desc_len += p[DESC_bLength];
  374. if (current_desc_len >= desc_len && desc_len) {
  375. break;
  376. }
  377. }
  378. return found;
  379. }
  380. /**
  381. * @brief set USB interface
  382. *
  383. * @param [in] iface Interface index
  384. * @param [in] alt_setting Alternate setting number
  385. *
  386. * @return true if successfully configured false if error or unconfigured
  387. */
  388. static bool usbd_set_interface(uint8_t busid, uint8_t iface, uint8_t alt_setting)
  389. {
  390. const uint8_t *if_desc = NULL;
  391. struct usb_endpoint_descriptor *ep_desc;
  392. uint8_t cur_alt_setting = 0xFF;
  393. uint8_t cur_iface = 0xFF;
  394. bool ret = false;
  395. const uint8_t *p;
  396. uint32_t desc_len = 0;
  397. uint32_t current_desc_len = 0;
  398. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  399. p = g_usbd_core[busid].descriptors->config_descriptor_callback(g_usbd_core[busid].speed);
  400. #else
  401. p = (uint8_t *)g_usbd_core[busid].descriptors;
  402. #endif
  403. USB_LOG_DBG("iface %u alt_setting %u\r\n", iface, alt_setting);
  404. while (p[DESC_bLength] != 0U) {
  405. switch (p[DESC_bDescriptorType]) {
  406. case USB_DESCRIPTOR_TYPE_CONFIGURATION:
  407. current_desc_len = 0;
  408. desc_len = (p[CONF_DESC_wTotalLength]) |
  409. (p[CONF_DESC_wTotalLength + 1] << 8);
  410. break;
  411. case USB_DESCRIPTOR_TYPE_INTERFACE:
  412. /* remember current alternate setting */
  413. cur_alt_setting = p[INTF_DESC_bAlternateSetting];
  414. cur_iface = p[INTF_DESC_bInterfaceNumber];
  415. if (cur_iface == iface &&
  416. cur_alt_setting == alt_setting) {
  417. if_desc = (void *)p;
  418. }
  419. USB_LOG_DBG("Current iface %u alt setting %u",
  420. cur_iface, cur_alt_setting);
  421. break;
  422. case USB_DESCRIPTOR_TYPE_ENDPOINT:
  423. if (cur_iface == iface) {
  424. ep_desc = (struct usb_endpoint_descriptor *)p;
  425. if (cur_alt_setting != alt_setting) {
  426. ret = usbd_reset_endpoint(busid, ep_desc);
  427. } else {
  428. ret = usbd_set_endpoint(busid, ep_desc);
  429. }
  430. }
  431. break;
  432. default:
  433. break;
  434. }
  435. /* skip to next descriptor */
  436. p += p[DESC_bLength];
  437. current_desc_len += p[DESC_bLength];
  438. if (current_desc_len >= desc_len && desc_len) {
  439. break;
  440. }
  441. }
  442. usbd_class_event_notify_handler(busid, USBD_EVENT_SET_INTERFACE, (void *)if_desc);
  443. return ret;
  444. }
  445. /**
  446. * @brief handle a standard device request
  447. *
  448. * @param [in] setup The setup packet
  449. * @param [in,out] data Data buffer
  450. * @param [in,out] len Pointer to data length
  451. *
  452. * @return true if the request was handled successfully
  453. */
  454. static bool usbd_std_device_req_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
  455. {
  456. uint16_t value = setup->wValue;
  457. bool ret = true;
  458. switch (setup->bRequest) {
  459. case USB_REQUEST_GET_STATUS:
  460. /* bit 0: self-powered */
  461. /* bit 1: remote wakeup */
  462. (*data)[0] = 0x00;
  463. if (g_usbd_core[busid].self_powered) {
  464. (*data)[0] |= USB_GETSTATUS_SELF_POWERED;
  465. }
  466. if (g_usbd_core[busid].remote_wakeup_enabled) {
  467. (*data)[0] |= USB_GETSTATUS_REMOTE_WAKEUP;
  468. }
  469. (*data)[1] = 0x00;
  470. *len = 2;
  471. break;
  472. case USB_REQUEST_CLEAR_FEATURE:
  473. case USB_REQUEST_SET_FEATURE:
  474. if (value == USB_FEATURE_REMOTE_WAKEUP) {
  475. if (setup->bRequest == USB_REQUEST_SET_FEATURE) {
  476. g_usbd_core[busid].remote_wakeup_enabled = true;
  477. g_usbd_core[busid].event_handler(busid, USBD_EVENT_SET_REMOTE_WAKEUP);
  478. } else {
  479. g_usbd_core[busid].remote_wakeup_enabled = false;
  480. g_usbd_core[busid].event_handler(busid, USBD_EVENT_CLR_REMOTE_WAKEUP);
  481. }
  482. } else if (value == USB_FEATURE_TEST_MODE) {
  483. #ifdef CONFIG_USBDEV_TEST_MODE
  484. g_usbd_core[busid].test_req = true;
  485. #endif
  486. }
  487. *len = 0;
  488. break;
  489. case USB_REQUEST_SET_ADDRESS:
  490. usbd_set_address(busid, value);
  491. *len = 0;
  492. break;
  493. case USB_REQUEST_GET_DESCRIPTOR:
  494. ret = usbd_get_descriptor(busid, value, data, len);
  495. break;
  496. case USB_REQUEST_SET_DESCRIPTOR:
  497. ret = false;
  498. break;
  499. case USB_REQUEST_GET_CONFIGURATION:
  500. (*data)[0] = g_usbd_core[busid].configuration;
  501. *len = 1;
  502. break;
  503. case USB_REQUEST_SET_CONFIGURATION:
  504. value &= 0xFF;
  505. if (value == 0) {
  506. g_usbd_core[busid].configuration = 0;
  507. } else if (!usbd_set_configuration(busid, value, 0)) {
  508. ret = false;
  509. } else {
  510. g_usbd_core[busid].configuration = value;
  511. usbd_class_event_notify_handler(busid, USBD_EVENT_CONFIGURED, NULL);
  512. g_usbd_core[busid].event_handler(busid, USBD_EVENT_CONFIGURED);
  513. }
  514. *len = 0;
  515. break;
  516. case USB_REQUEST_GET_INTERFACE:
  517. case USB_REQUEST_SET_INTERFACE:
  518. ret = false;
  519. break;
  520. default:
  521. ret = false;
  522. break;
  523. }
  524. return ret;
  525. }
  526. /**
  527. * @brief handle a standard interface request
  528. *
  529. * @param [in] setup The setup packet
  530. * @param [in,out] data Data buffer
  531. * @param [in,out] len Pointer to data length
  532. *
  533. * @return true if the request was handled successfully
  534. */
  535. static bool usbd_std_interface_req_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
  536. {
  537. uint8_t type = HI_BYTE(setup->wValue);
  538. uint8_t intf_num = LO_BYTE(setup->wIndex);
  539. bool ret = true;
  540. const uint8_t *p;
  541. uint32_t desc_len = 0;
  542. uint32_t current_desc_len = 0;
  543. uint8_t cur_iface = 0xFF;
  544. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  545. p = g_usbd_core[busid].descriptors->config_descriptor_callback(g_usbd_core[busid].speed);
  546. #else
  547. p = (uint8_t *)g_usbd_core[busid].descriptors;
  548. #endif
  549. /* Only when device is configured, then interface requests can be valid. */
  550. if (!is_device_configured(busid)) {
  551. return false;
  552. }
  553. switch (setup->bRequest) {
  554. case USB_REQUEST_GET_STATUS:
  555. (*data)[0] = 0x00;
  556. (*data)[1] = 0x00;
  557. *len = 2;
  558. break;
  559. case USB_REQUEST_GET_DESCRIPTOR:
  560. if (type == 0x21) { /* HID_DESCRIPTOR_TYPE_HID */
  561. while (p[DESC_bLength] != 0U) {
  562. switch (p[DESC_bDescriptorType]) {
  563. case USB_DESCRIPTOR_TYPE_CONFIGURATION:
  564. current_desc_len = 0;
  565. desc_len = (p[CONF_DESC_wTotalLength]) |
  566. (p[CONF_DESC_wTotalLength + 1] << 8);
  567. break;
  568. case USB_DESCRIPTOR_TYPE_INTERFACE:
  569. cur_iface = p[INTF_DESC_bInterfaceNumber];
  570. break;
  571. case 0x21:
  572. if (cur_iface == intf_num) {
  573. *data = (uint8_t *)p;
  574. //memcpy(*data, p, p[DESC_bLength]);
  575. *len = p[DESC_bLength];
  576. return true;
  577. }
  578. break;
  579. default:
  580. break;
  581. }
  582. /* skip to next descriptor */
  583. p += p[DESC_bLength];
  584. current_desc_len += p[DESC_bLength];
  585. if (current_desc_len >= desc_len && desc_len) {
  586. break;
  587. }
  588. }
  589. } else if (type == 0x22) { /* HID_DESCRIPTOR_TYPE_HID_REPORT */
  590. for (uint8_t i = 0; i < g_usbd_core[busid].intf_offset; i++) {
  591. struct usbd_interface *intf = g_usbd_core[busid].intf[i];
  592. if (intf && (intf->intf_num == intf_num)) {
  593. *data = (uint8_t *)intf->hid_report_descriptor;
  594. //memcpy(*data, intf->hid_report_descriptor, intf->hid_report_descriptor_len);
  595. *len = intf->hid_report_descriptor_len;
  596. return true;
  597. }
  598. }
  599. }
  600. ret = false;
  601. break;
  602. case USB_REQUEST_CLEAR_FEATURE:
  603. case USB_REQUEST_SET_FEATURE:
  604. ret = false;
  605. break;
  606. case USB_REQUEST_GET_INTERFACE:
  607. (*data)[0] = 0;
  608. *len = 1;
  609. break;
  610. case USB_REQUEST_SET_INTERFACE:
  611. usbd_set_interface(busid, setup->wIndex, setup->wValue);
  612. *len = 0;
  613. break;
  614. default:
  615. ret = false;
  616. break;
  617. }
  618. return ret;
  619. }
  620. /**
  621. * @brief handle a standard endpoint request
  622. *
  623. * @param [in] setup The setup packet
  624. * @param [in,out] data Data buffer
  625. * @param [in,out] len Pointer to data length
  626. *
  627. * @return true if the request was handled successfully
  628. */
  629. static bool usbd_std_endpoint_req_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
  630. {
  631. uint8_t ep = (uint8_t)setup->wIndex;
  632. bool ret = true;
  633. uint8_t stalled;
  634. /* Only when device is configured, then endpoint requests can be valid. */
  635. if (!is_device_configured(busid)) {
  636. return false;
  637. }
  638. switch (setup->bRequest) {
  639. case USB_REQUEST_GET_STATUS:
  640. usbd_ep_is_stalled(busid, ep, &stalled);
  641. if (stalled) {
  642. (*data)[0] = 0x01;
  643. } else {
  644. (*data)[0] = 0x00;
  645. }
  646. (*data)[1] = 0x00;
  647. *len = 2;
  648. break;
  649. case USB_REQUEST_CLEAR_FEATURE:
  650. if (setup->wValue == USB_FEATURE_ENDPOINT_HALT) {
  651. USB_LOG_ERR("ep:%02x clear halt\r\n", ep);
  652. usbd_ep_clear_stall(busid, ep);
  653. break;
  654. } else {
  655. ret = false;
  656. }
  657. *len = 0;
  658. break;
  659. case USB_REQUEST_SET_FEATURE:
  660. if (setup->wValue == USB_FEATURE_ENDPOINT_HALT) {
  661. USB_LOG_ERR("ep:%02x set halt\r\n", ep);
  662. usbd_ep_set_stall(busid, ep);
  663. } else {
  664. ret = false;
  665. }
  666. *len = 0;
  667. break;
  668. case USB_REQUEST_SYNCH_FRAME:
  669. ret = false;
  670. break;
  671. default:
  672. ret = false;
  673. break;
  674. }
  675. return ret;
  676. }
  677. /**
  678. * @brief handle standard requests (list in chapter 9)
  679. *
  680. * @param [in] setup The setup packet
  681. * @param [in,out] data Data buffer
  682. * @param [in,out] len Pointer to data length
  683. *
  684. * @return true if the request was handled successfully
  685. */
  686. static int usbd_standard_request_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
  687. {
  688. int rc = 0;
  689. switch (setup->bmRequestType & USB_REQUEST_RECIPIENT_MASK) {
  690. case USB_REQUEST_RECIPIENT_DEVICE:
  691. if (usbd_std_device_req_handler(busid, setup, data, len) == false) {
  692. rc = -1;
  693. }
  694. break;
  695. case USB_REQUEST_RECIPIENT_INTERFACE:
  696. if (usbd_std_interface_req_handler(busid, setup, data, len) == false) {
  697. rc = -1;
  698. }
  699. break;
  700. case USB_REQUEST_RECIPIENT_ENDPOINT:
  701. if (usbd_std_endpoint_req_handler(busid, setup, data, len) == false) {
  702. rc = -1;
  703. }
  704. break;
  705. default:
  706. rc = -1;
  707. break;
  708. }
  709. return rc;
  710. }
  711. /**
  712. * @brief handler for class requests
  713. *
  714. * If a custom request handler was installed, this handler is called first.
  715. *
  716. * @param [in] setup The setup packet
  717. * @param [in,out] data Data buffer
  718. * @param [in,out] len Pointer to data length
  719. *
  720. * @return true if the request was handled successfully
  721. */
  722. static int usbd_class_request_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
  723. {
  724. if ((setup->bmRequestType & USB_REQUEST_RECIPIENT_MASK) == USB_REQUEST_RECIPIENT_INTERFACE) {
  725. for (uint8_t i = 0; i < g_usbd_core[busid].intf_offset; i++) {
  726. struct usbd_interface *intf = g_usbd_core[busid].intf[i];
  727. if (intf && intf->class_interface_handler && (intf->intf_num == (setup->wIndex & 0xFF))) {
  728. return intf->class_interface_handler(busid, setup, data, len);
  729. }
  730. }
  731. } else if ((setup->bmRequestType & USB_REQUEST_RECIPIENT_MASK) == USB_REQUEST_RECIPIENT_ENDPOINT) {
  732. for (uint8_t i = 0; i < g_usbd_core[busid].intf_offset; i++) {
  733. struct usbd_interface *intf = g_usbd_core[busid].intf[i];
  734. if (intf && intf->class_endpoint_handler) {
  735. return intf->class_endpoint_handler(busid, setup, data, len);
  736. }
  737. }
  738. }
  739. return -1;
  740. }
  741. /**
  742. * @brief handler for vendor requests
  743. *
  744. * If a custom request handler was installed, this handler is called first.
  745. *
  746. * @param [in] setup The setup packet
  747. * @param [in,out] data Data buffer
  748. * @param [in,out] len Pointer to data length
  749. *
  750. * @return true if the request was handled successfully
  751. */
  752. static int usbd_vendor_request_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
  753. {
  754. uint32_t desclen;
  755. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  756. if (g_usbd_core[busid].descriptors->msosv1_descriptor) {
  757. if (setup->bRequest == g_usbd_core[busid].descriptors->msosv1_descriptor->vendor_code) {
  758. switch (setup->wIndex) {
  759. case 0x04:
  760. desclen = g_usbd_core[busid].descriptors->msosv1_descriptor->compat_id[0] +
  761. (g_usbd_core[busid].descriptors->msosv1_descriptor->compat_id[1] << 8) +
  762. (g_usbd_core[busid].descriptors->msosv1_descriptor->compat_id[2] << 16) +
  763. (g_usbd_core[busid].descriptors->msosv1_descriptor->compat_id[3] << 24);
  764. *data = (uint8_t *)g_usbd_core[busid].descriptors->msosv1_descriptor->compat_id;
  765. //memcpy(*data, g_usbd_core[busid].descriptors->msosv1_descriptor->compat_id, desclen);
  766. *len = desclen;
  767. return 0;
  768. case 0x05:
  769. desclen = g_usbd_core[busid].descriptors->msosv1_descriptor->comp_id_property[setup->wValue][0] +
  770. (g_usbd_core[busid].descriptors->msosv1_descriptor->comp_id_property[setup->wValue][1] << 8) +
  771. (g_usbd_core[busid].descriptors->msosv1_descriptor->comp_id_property[setup->wValue][2] << 16) +
  772. (g_usbd_core[busid].descriptors->msosv1_descriptor->comp_id_property[setup->wValue][3] << 24);
  773. *data = (uint8_t *)g_usbd_core[busid].descriptors->msosv1_descriptor->comp_id_property[setup->wValue];
  774. //memcpy(*data, g_usbd_core[busid].descriptors->msosv1_descriptor->comp_id_property[setup->wValue], desclen);
  775. *len = desclen;
  776. return 0;
  777. default:
  778. USB_LOG_ERR("unknown vendor code\r\n");
  779. return -1;
  780. }
  781. }
  782. } else if (g_usbd_core[busid].descriptors->msosv2_descriptor) {
  783. if (setup->bRequest == g_usbd_core[busid].descriptors->msosv2_descriptor->vendor_code) {
  784. switch (setup->wIndex) {
  785. case WINUSB_REQUEST_GET_DESCRIPTOR_SET:
  786. desclen = g_usbd_core[busid].descriptors->msosv2_descriptor->compat_id_len;
  787. *data = (uint8_t *)g_usbd_core[busid].descriptors->msosv2_descriptor->compat_id;
  788. //memcpy(*data, g_usbd_core[busid].descriptors->msosv2_descriptor->compat_id, desclen);
  789. *len = g_usbd_core[busid].descriptors->msosv2_descriptor->compat_id_len;
  790. return 0;
  791. default:
  792. USB_LOG_ERR("unknown vendor code\r\n");
  793. return -1;
  794. }
  795. }
  796. } else if (g_usbd_core[busid].descriptors->webusb_url_descriptor) {
  797. if (setup->bRequest == g_usbd_core[busid].descriptors->webusb_url_descriptor->vendor_code) {
  798. switch (setup->wIndex) {
  799. case WINUSB_REQUEST_GET_DESCRIPTOR_SET:
  800. desclen = g_usbd_core[busid].descriptors->webusb_url_descriptor->string_len;
  801. *data = (uint8_t *)g_usbd_core[busid].descriptors->webusb_url_descriptor->string;
  802. //memcpy(*data, g_usbd_core[busid].descriptors->webusb_url_descriptor->string, desclen);
  803. *len = desclen;
  804. return 0;
  805. default:
  806. USB_LOG_ERR("unknown vendor code\r\n");
  807. return -1;
  808. }
  809. }
  810. }
  811. #else
  812. if (g_usbd_core[busid].msosv1_desc) {
  813. if (setup->bRequest == g_usbd_core[busid].msosv1_desc->vendor_code) {
  814. switch (setup->wIndex) {
  815. case 0x04:
  816. *data = (uint8_t *)g_usbd_core[busid].msosv1_desc->compat_id;
  817. desclen = g_usbd_core[busid].msosv1_desc->compat_id[0] +
  818. (g_usbd_core[busid].msosv1_desc->compat_id[1] << 8) +
  819. (g_usbd_core[busid].msosv1_desc->compat_id[2] << 16) +
  820. (g_usbd_core[busid].msosv1_desc->compat_id[3] << 24);
  821. //memcpy(*data, g_usbd_core[busid].msosv1_desc->compat_id, desclen);
  822. *len = desclen;
  823. return 0;
  824. case 0x05:
  825. *data = (uint8_t *)g_usbd_core[busid].msosv1_desc->comp_id_property[setup->wValue];
  826. desclen = g_usbd_core[busid].msosv1_desc->comp_id_property[setup->wValue][0] +
  827. (g_usbd_core[busid].msosv1_desc->comp_id_property[setup->wValue][1] << 8) +
  828. (g_usbd_core[busid].msosv1_desc->comp_id_property[setup->wValue][2] << 16) +
  829. (g_usbd_core[busid].msosv1_desc->comp_id_property[setup->wValue][3] << 24);
  830. //memcpy(*data, g_usbd_core[busid].msosv1_desc->comp_id_property[setup->wValue], desclen);
  831. *len = desclen;
  832. return 0;
  833. default:
  834. USB_LOG_ERR("unknown vendor code\r\n");
  835. return -1;
  836. }
  837. }
  838. } else if (g_usbd_core[busid].msosv2_desc) {
  839. if (setup->bRequest == g_usbd_core[busid].msosv2_desc->vendor_code) {
  840. switch (setup->wIndex) {
  841. case WINUSB_REQUEST_GET_DESCRIPTOR_SET:
  842. *data = (uint8_t *)g_usbd_core[busid].msosv2_desc->compat_id;
  843. //memcpy(*data, g_usbd_core[busid].msosv2_desc->compat_id, g_usbd_core[busid].msosv2_desc->compat_id_len);
  844. *len = g_usbd_core[busid].msosv2_desc->compat_id_len;
  845. return 0;
  846. default:
  847. USB_LOG_ERR("unknown vendor code\r\n");
  848. return -1;
  849. }
  850. }
  851. }
  852. #endif
  853. for (uint8_t i = 0; i < g_usbd_core[busid].intf_offset; i++) {
  854. struct usbd_interface *intf = g_usbd_core[busid].intf[i];
  855. if (intf && intf->vendor_handler && (intf->vendor_handler(busid, setup, data, len) == 0)) {
  856. return 0;
  857. }
  858. }
  859. return -1;
  860. }
  861. /**
  862. * @brief handle setup request( standard/class/vendor/other)
  863. *
  864. * @param [in] setup The setup packet
  865. * @param [in,out] data Data buffer
  866. * @param [in,out] len Pointer to data length
  867. *
  868. * @return true if the request was handles successfully
  869. */
  870. static bool usbd_setup_request_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
  871. {
  872. switch (setup->bmRequestType & USB_REQUEST_TYPE_MASK) {
  873. case USB_REQUEST_STANDARD:
  874. if (usbd_standard_request_handler(busid, setup, data, len) < 0) {
  875. /* Ignore error log for getting Device Qualifier Descriptor request */
  876. if ((setup->bRequest == 0x06) && (setup->wValue == 0x0600)) {
  877. //USB_LOG_DBG("Ignore DQD in fs\r\n");
  878. return false;
  879. }
  880. USB_LOG_ERR("standard request error\r\n");
  881. usbd_print_setup(setup);
  882. return false;
  883. }
  884. break;
  885. case USB_REQUEST_CLASS:
  886. if (usbd_class_request_handler(busid, setup, data, len) < 0) {
  887. USB_LOG_ERR("class request error\r\n");
  888. usbd_print_setup(setup);
  889. return false;
  890. }
  891. break;
  892. case USB_REQUEST_VENDOR:
  893. if (usbd_vendor_request_handler(busid, setup, data, len) < 0) {
  894. USB_LOG_ERR("vendor request error\r\n");
  895. usbd_print_setup(setup);
  896. return false;
  897. }
  898. break;
  899. default:
  900. return false;
  901. }
  902. return true;
  903. }
  904. static void usbd_class_event_notify_handler(uint8_t busid, uint8_t event, void *arg)
  905. {
  906. for (uint8_t i = 0; i < g_usbd_core[busid].intf_offset; i++) {
  907. struct usbd_interface *intf = g_usbd_core[busid].intf[i];
  908. if (arg) {
  909. struct usb_interface_descriptor *desc = (struct usb_interface_descriptor *)arg;
  910. if (intf && intf->notify_handler && (desc->bInterfaceNumber == (intf->intf_num))) {
  911. intf->notify_handler(busid, event, arg);
  912. }
  913. } else {
  914. if (intf && intf->notify_handler) {
  915. intf->notify_handler(busid, event, arg);
  916. }
  917. }
  918. }
  919. }
  920. void usbd_event_connect_handler(uint8_t busid)
  921. {
  922. g_usbd_core[busid].event_handler(busid, USBD_EVENT_CONNECTED);
  923. }
  924. void usbd_event_disconnect_handler(uint8_t busid)
  925. {
  926. g_usbd_core[busid].event_handler(busid, USBD_EVENT_DISCONNECTED);
  927. }
  928. void usbd_event_resume_handler(uint8_t busid)
  929. {
  930. g_usbd_core[busid].event_handler(busid, USBD_EVENT_RESUME);
  931. }
  932. void usbd_event_suspend_handler(uint8_t busid)
  933. {
  934. g_usbd_core[busid].event_handler(busid, USBD_EVENT_SUSPEND);
  935. }
  936. void usbd_event_reset_handler(uint8_t busid)
  937. {
  938. usbd_set_address(busid, 0);
  939. g_usbd_core[busid].configuration = 0;
  940. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  941. g_usbd_core[busid].speed = USB_SPEED_UNKNOWN;
  942. #endif
  943. struct usb_endpoint_descriptor ep0;
  944. ep0.bLength = 7;
  945. ep0.bDescriptorType = USB_DESCRIPTOR_TYPE_ENDPOINT;
  946. ep0.wMaxPacketSize = USB_CTRL_EP_MPS;
  947. ep0.bmAttributes = USB_ENDPOINT_TYPE_CONTROL;
  948. ep0.bEndpointAddress = USB_CONTROL_IN_EP0;
  949. ep0.bInterval = 0;
  950. usbd_ep_open(busid, &ep0);
  951. ep0.bEndpointAddress = USB_CONTROL_OUT_EP0;
  952. usbd_ep_open(busid, &ep0);
  953. usbd_class_event_notify_handler(busid, USBD_EVENT_RESET, NULL);
  954. g_usbd_core[busid].event_handler(busid, USBD_EVENT_RESET);
  955. }
  956. void usbd_event_ep0_setup_complete_handler(uint8_t busid, uint8_t *psetup)
  957. {
  958. struct usb_setup_packet *setup = &g_usbd_core[busid].setup;
  959. uint8_t *buf;
  960. memcpy(setup, psetup, 8);
  961. #ifdef CONFIG_USBDEV_SETUP_LOG_PRINT
  962. usbd_print_setup(setup);
  963. #endif
  964. if (setup->wLength > CONFIG_USBDEV_REQUEST_BUFFER_LEN) {
  965. if ((setup->bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_OUT) {
  966. USB_LOG_ERR("Request buffer too small\r\n");
  967. usbd_ep_set_stall(busid, USB_CONTROL_IN_EP0);
  968. return;
  969. }
  970. }
  971. g_usbd_core[busid].ep0_data_buf = g_usbd_core[busid].req_data;
  972. g_usbd_core[busid].ep0_data_buf_residue = setup->wLength;
  973. g_usbd_core[busid].ep0_data_buf_len = setup->wLength;
  974. g_usbd_core[busid].zlp_flag = false;
  975. buf = g_usbd_core[busid].ep0_data_buf;
  976. /* handle class request when all the data is received */
  977. if (setup->wLength && ((setup->bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_OUT)) {
  978. USB_LOG_DBG("Start reading %d bytes from ep0\r\n", setup->wLength);
  979. usbd_ep_start_read(busid, USB_CONTROL_OUT_EP0, g_usbd_core[busid].ep0_data_buf, setup->wLength);
  980. return;
  981. }
  982. /* Ask installed handler to process request */
  983. if (!usbd_setup_request_handler(busid, setup, &buf, &g_usbd_core[busid].ep0_data_buf_len)) {
  984. usbd_ep_set_stall(busid, USB_CONTROL_IN_EP0);
  985. return;
  986. }
  987. /* Send smallest of requested and offered length */
  988. g_usbd_core[busid].ep0_data_buf_residue = MIN(g_usbd_core[busid].ep0_data_buf_len, setup->wLength);
  989. if (g_usbd_core[busid].ep0_data_buf_residue > CONFIG_USBDEV_REQUEST_BUFFER_LEN) {
  990. USB_LOG_ERR("Request buffer too small\r\n");
  991. usbd_ep_set_stall(busid, USB_CONTROL_IN_EP0);
  992. return;
  993. }
  994. /* use *data = xxx; g_usbd_core[busid].ep0_data_buf records real data address, we should copy data into ep0 buffer.
  995. * Why we should copy once? because some chips are not access to flash with dma if real data address is in flash address(such as ch32).
  996. */
  997. if (buf != g_usbd_core[busid].ep0_data_buf) {
  998. #ifdef CONFIG_USBDEV_EP0_INDATA_NO_COPY
  999. g_usbd_core[busid].ep0_data_buf = buf;
  1000. #else
  1001. memcpy(g_usbd_core[busid].ep0_data_buf, buf, g_usbd_core[busid].ep0_data_buf_residue);
  1002. #endif
  1003. } else {
  1004. /* use memcpy(*data, xxx, len); has copied into ep0 buffer, we do nothing */
  1005. }
  1006. /* Send data or status to host */
  1007. usbd_ep_start_write(busid, USB_CONTROL_IN_EP0, g_usbd_core[busid].ep0_data_buf, g_usbd_core[busid].ep0_data_buf_residue);
  1008. /*
  1009. * Set ZLP flag when host asks for a bigger length and the data size is
  1010. * multiplier of USB_CTRL_EP_MPS, to indicate the transfer done after zlp
  1011. * sent.
  1012. */
  1013. if ((setup->wLength > g_usbd_core[busid].ep0_data_buf_len) && (!(g_usbd_core[busid].ep0_data_buf_len % USB_CTRL_EP_MPS))) {
  1014. g_usbd_core[busid].zlp_flag = true;
  1015. USB_LOG_DBG("EP0 Set zlp\r\n");
  1016. }
  1017. }
  1018. void usbd_event_ep0_in_complete_handler(uint8_t busid, uint8_t ep, uint32_t nbytes)
  1019. {
  1020. struct usb_setup_packet *setup = &g_usbd_core[busid].setup;
  1021. g_usbd_core[busid].ep0_data_buf += nbytes;
  1022. g_usbd_core[busid].ep0_data_buf_residue -= nbytes;
  1023. USB_LOG_DBG("EP0 send %d bytes, %d remained\r\n", nbytes, g_usbd_core[busid].ep0_data_buf_residue);
  1024. if (g_usbd_core[busid].ep0_data_buf_residue != 0) {
  1025. /* Start sending the remain data */
  1026. usbd_ep_start_write(busid, USB_CONTROL_IN_EP0, g_usbd_core[busid].ep0_data_buf, g_usbd_core[busid].ep0_data_buf_residue);
  1027. } else {
  1028. if (g_usbd_core[busid].zlp_flag == true) {
  1029. g_usbd_core[busid].zlp_flag = false;
  1030. /* Send zlp to host */
  1031. USB_LOG_DBG("EP0 Send zlp\r\n");
  1032. usbd_ep_start_write(busid, USB_CONTROL_IN_EP0, NULL, 0);
  1033. } else {
  1034. /* Satisfying three conditions will jump here.
  1035. * 1. send status completely
  1036. * 2. send zlp completely
  1037. * 3. send last data completely.
  1038. */
  1039. if (setup->wLength && ((setup->bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_IN)) {
  1040. /* if all data has sent completely, start reading out status */
  1041. usbd_ep_start_read(busid, USB_CONTROL_OUT_EP0, NULL, 0);
  1042. }
  1043. #ifdef CONFIG_USBDEV_TEST_MODE
  1044. if (g_usbd_core[busid].test_req) {
  1045. usbd_execute_test_mode(busid, HI_BYTE(setup->wIndex));
  1046. g_usbd_core[busid].test_req = false;
  1047. }
  1048. #endif
  1049. }
  1050. }
  1051. }
  1052. void usbd_event_ep0_out_complete_handler(uint8_t busid, uint8_t ep, uint32_t nbytes)
  1053. {
  1054. struct usb_setup_packet *setup = &g_usbd_core[busid].setup;
  1055. if (nbytes > 0) {
  1056. g_usbd_core[busid].ep0_data_buf += nbytes;
  1057. g_usbd_core[busid].ep0_data_buf_residue -= nbytes;
  1058. USB_LOG_DBG("EP0 recv %d bytes, %d remained\r\n", nbytes, g_usbd_core[busid].ep0_data_buf_residue);
  1059. if (g_usbd_core[busid].ep0_data_buf_residue == 0) {
  1060. /* Received all, send data to handler */
  1061. g_usbd_core[busid].ep0_data_buf = g_usbd_core[busid].req_data;
  1062. if (!usbd_setup_request_handler(busid, setup, &g_usbd_core[busid].ep0_data_buf, &g_usbd_core[busid].ep0_data_buf_len)) {
  1063. usbd_ep_set_stall(busid, USB_CONTROL_IN_EP0);
  1064. return;
  1065. }
  1066. /*Send status to host*/
  1067. usbd_ep_start_write(busid, USB_CONTROL_IN_EP0, NULL, 0);
  1068. } else {
  1069. /* Start reading the remain data */
  1070. usbd_ep_start_read(busid, USB_CONTROL_OUT_EP0, g_usbd_core[busid].ep0_data_buf, g_usbd_core[busid].ep0_data_buf_residue);
  1071. }
  1072. } else {
  1073. /* Read out status completely, do nothing */
  1074. USB_LOG_DBG("EP0 recv out status\r\n");
  1075. }
  1076. }
  1077. void usbd_event_ep_in_complete_handler(uint8_t busid, uint8_t ep, uint32_t nbytes)
  1078. {
  1079. if (g_usbd_core[busid].tx_msg[ep & 0x7f].cb) {
  1080. g_usbd_core[busid].tx_msg[ep & 0x7f].cb(busid, ep, nbytes);
  1081. }
  1082. }
  1083. void usbd_event_ep_out_complete_handler(uint8_t busid, uint8_t ep, uint32_t nbytes)
  1084. {
  1085. if (g_usbd_core[busid].rx_msg[ep & 0x7f].cb) {
  1086. g_usbd_core[busid].rx_msg[ep & 0x7f].cb(busid, ep, nbytes);
  1087. }
  1088. }
  1089. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  1090. void usbd_desc_register(uint8_t busid, const struct usb_descriptor *desc)
  1091. {
  1092. memset(&g_usbd_core[busid], 0, sizeof(struct usbd_core_priv));
  1093. g_usbd_core[busid].descriptors = desc;
  1094. g_usbd_core[busid].intf_offset = 0;
  1095. g_usbd_core[busid].tx_msg[0].ep = 0x80;
  1096. g_usbd_core[busid].tx_msg[0].cb = usbd_event_ep0_in_complete_handler;
  1097. g_usbd_core[busid].rx_msg[0].ep = 0x00;
  1098. g_usbd_core[busid].rx_msg[0].cb = usbd_event_ep0_out_complete_handler;
  1099. }
  1100. #else
  1101. void usbd_desc_register(uint8_t busid, const uint8_t *desc)
  1102. {
  1103. memset(&g_usbd_core[busid], 0, sizeof(struct usbd_core_priv));
  1104. g_usbd_core[busid].descriptors = desc;
  1105. g_usbd_core[busid].intf_offset = 0;
  1106. g_usbd_core[busid].tx_msg[0].ep = 0x80;
  1107. g_usbd_core[busid].tx_msg[0].cb = usbd_event_ep0_in_complete_handler;
  1108. g_usbd_core[busid].rx_msg[0].ep = 0x00;
  1109. g_usbd_core[busid].rx_msg[0].cb = usbd_event_ep0_out_complete_handler;
  1110. }
  1111. /* Register MS OS Descriptors version 1 */
  1112. void usbd_msosv1_desc_register(uint8_t busid, struct usb_msosv1_descriptor *desc)
  1113. {
  1114. g_usbd_core[busid].msosv1_desc = desc;
  1115. }
  1116. /* Register MS OS Descriptors version 2 */
  1117. void usbd_msosv2_desc_register(uint8_t busid, struct usb_msosv2_descriptor *desc)
  1118. {
  1119. g_usbd_core[busid].msosv2_desc = desc;
  1120. }
  1121. void usbd_bos_desc_register(uint8_t busid, struct usb_bos_descriptor *desc)
  1122. {
  1123. g_usbd_core[busid].bos_desc = desc;
  1124. }
  1125. #endif
  1126. void usbd_add_interface(uint8_t busid, struct usbd_interface *intf)
  1127. {
  1128. intf->intf_num = g_usbd_core[busid].intf_offset;
  1129. g_usbd_core[busid].intf[g_usbd_core[busid].intf_offset] = intf;
  1130. g_usbd_core[busid].intf_offset++;
  1131. }
  1132. void usbd_add_endpoint(uint8_t busid, struct usbd_endpoint *ep)
  1133. {
  1134. if (ep->ep_addr & 0x80) {
  1135. g_usbd_core[busid].tx_msg[ep->ep_addr & 0x7f].ep = ep->ep_addr;
  1136. g_usbd_core[busid].tx_msg[ep->ep_addr & 0x7f].cb = ep->ep_cb;
  1137. } else {
  1138. g_usbd_core[busid].rx_msg[ep->ep_addr & 0x7f].ep = ep->ep_addr;
  1139. g_usbd_core[busid].rx_msg[ep->ep_addr & 0x7f].cb = ep->ep_cb;
  1140. }
  1141. }
  1142. uint16_t usbd_get_ep_mps(uint8_t busid, uint8_t ep)
  1143. {
  1144. if (ep & 0x80) {
  1145. return g_usbd_core[busid].tx_msg[ep & 0x7f].ep_mps;
  1146. } else {
  1147. return g_usbd_core[busid].rx_msg[ep & 0x7f].ep_mps;
  1148. }
  1149. }
  1150. uint8_t usbd_get_ep_mult(uint8_t busid, uint8_t ep)
  1151. {
  1152. if (ep & 0x80) {
  1153. return g_usbd_core[busid].tx_msg[ep & 0x7f].ep_mult;
  1154. } else {
  1155. return g_usbd_core[busid].rx_msg[ep & 0x7f].ep_mult;
  1156. }
  1157. }
  1158. bool usb_device_is_configured(uint8_t busid)
  1159. {
  1160. return g_usbd_core[busid].configuration;
  1161. }
  1162. int usbd_initialize(uint8_t busid, uint32_t reg_base, void (*event_handler)(uint8_t busid, uint8_t event))
  1163. {
  1164. int ret;
  1165. struct usbd_bus *bus;
  1166. if (busid >= CONFIG_USBDEV_MAX_BUS) {
  1167. USB_LOG_ERR("bus overflow\r\n");
  1168. while (1) {
  1169. }
  1170. }
  1171. bus = &g_usbdev_bus[busid];
  1172. bus->reg_base = reg_base;
  1173. g_usbd_core[busid].event_handler = event_handler;
  1174. ret = usb_dc_init(busid);
  1175. usbd_class_event_notify_handler(busid, USBD_EVENT_INIT, NULL);
  1176. g_usbd_core[busid].event_handler(busid, USBD_EVENT_INIT);
  1177. return ret;
  1178. }
  1179. int usbd_deinitialize(uint8_t busid)
  1180. {
  1181. g_usbd_core[busid].intf_offset = 0;
  1182. usb_dc_deinit(busid);
  1183. usbd_class_event_notify_handler(busid, USBD_EVENT_DEINIT, NULL);
  1184. g_usbd_core[busid].event_handler(busid, USBD_EVENT_DEINIT);
  1185. return 0;
  1186. }