usbd_core.c 54 KB

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