usbd_core.c 52 KB

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