usbd_core.c 54 KB

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