usbd_core.c 44 KB

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