usbd_core.c 54 KB

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