usbd_core.c 47 KB

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