usbd_core.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598
  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. USB_LOG_ERR("unknown vendor code\r\n");
  835. return -1;
  836. }
  837. }
  838. } else if (g_usbd_core[busid].descriptors->msosv2_descriptor) {
  839. if (setup->bRequest == g_usbd_core[busid].descriptors->msosv2_descriptor->vendor_code) {
  840. switch (setup->wIndex) {
  841. case WINUSB_REQUEST_GET_DESCRIPTOR_SET:
  842. desclen = g_usbd_core[busid].descriptors->msosv2_descriptor->compat_id_len;
  843. *data = (uint8_t *)g_usbd_core[busid].descriptors->msosv2_descriptor->compat_id;
  844. //memcpy(*data, g_usbd_core[busid].descriptors->msosv2_descriptor->compat_id, desclen);
  845. *len = g_usbd_core[busid].descriptors->msosv2_descriptor->compat_id_len;
  846. return 0;
  847. default:
  848. USB_LOG_ERR("unknown vendor code\r\n");
  849. return -1;
  850. }
  851. }
  852. }
  853. if (g_usbd_core[busid].descriptors->webusb_url_descriptor) {
  854. if (setup->bRequest == g_usbd_core[busid].descriptors->webusb_url_descriptor->vendor_code) {
  855. switch (setup->wIndex) {
  856. case WEBUSB_REQUEST_GET_URL:
  857. desclen = g_usbd_core[busid].descriptors->webusb_url_descriptor->string_len;
  858. *data = (uint8_t *)g_usbd_core[busid].descriptors->webusb_url_descriptor->string;
  859. //memcpy(*data, g_usbd_core[busid].descriptors->webusb_url_descriptor->string, desclen);
  860. *len = desclen;
  861. return 0;
  862. default:
  863. USB_LOG_ERR("unknown vendor code\r\n");
  864. return -1;
  865. }
  866. }
  867. }
  868. #else
  869. if (g_usbd_core[busid].msosv1_desc) {
  870. if (setup->bRequest == g_usbd_core[busid].msosv1_desc->vendor_code) {
  871. switch (setup->wIndex) {
  872. case 0x04:
  873. *data = (uint8_t *)g_usbd_core[busid].msosv1_desc->compat_id;
  874. desclen = g_usbd_core[busid].msosv1_desc->compat_id[0] +
  875. (g_usbd_core[busid].msosv1_desc->compat_id[1] << 8) +
  876. (g_usbd_core[busid].msosv1_desc->compat_id[2] << 16) +
  877. (g_usbd_core[busid].msosv1_desc->compat_id[3] << 24);
  878. //memcpy(*data, g_usbd_core[busid].msosv1_desc->compat_id, desclen);
  879. *len = desclen;
  880. return 0;
  881. case 0x05:
  882. *data = (uint8_t *)g_usbd_core[busid].msosv1_desc->comp_id_property[setup->wValue];
  883. desclen = g_usbd_core[busid].msosv1_desc->comp_id_property[setup->wValue][0] +
  884. (g_usbd_core[busid].msosv1_desc->comp_id_property[setup->wValue][1] << 8) +
  885. (g_usbd_core[busid].msosv1_desc->comp_id_property[setup->wValue][2] << 16) +
  886. (g_usbd_core[busid].msosv1_desc->comp_id_property[setup->wValue][3] << 24);
  887. //memcpy(*data, g_usbd_core[busid].msosv1_desc->comp_id_property[setup->wValue], desclen);
  888. *len = desclen;
  889. return 0;
  890. default:
  891. USB_LOG_ERR("unknown vendor code\r\n");
  892. return -1;
  893. }
  894. }
  895. } else if (g_usbd_core[busid].msosv2_desc) {
  896. if (setup->bRequest == g_usbd_core[busid].msosv2_desc->vendor_code) {
  897. switch (setup->wIndex) {
  898. case WINUSB_REQUEST_GET_DESCRIPTOR_SET:
  899. *data = (uint8_t *)g_usbd_core[busid].msosv2_desc->compat_id;
  900. //memcpy(*data, g_usbd_core[busid].msosv2_desc->compat_id, g_usbd_core[busid].msosv2_desc->compat_id_len);
  901. *len = g_usbd_core[busid].msosv2_desc->compat_id_len;
  902. return 0;
  903. default:
  904. USB_LOG_ERR("unknown vendor code\r\n");
  905. return -1;
  906. }
  907. }
  908. }
  909. if (g_usbd_core[busid].webusb_url_desc) {
  910. if (setup->bRequest == g_usbd_core[busid].webusb_url_desc->vendor_code) {
  911. switch (setup->wIndex) {
  912. case WEBUSB_REQUEST_GET_URL:
  913. desclen = g_usbd_core[busid].webusb_url_desc->string_len;
  914. *data = (uint8_t *)g_usbd_core[busid].webusb_url_desc->string;
  915. //memcpy(*data, g_usbd_core[busid].webusb_url_desc->string, desclen);
  916. *len = desclen;
  917. return 0;
  918. default:
  919. USB_LOG_ERR("unknown vendor code\r\n");
  920. return -1;
  921. }
  922. }
  923. }
  924. #endif
  925. for (uint8_t i = 0; i < g_usbd_core[busid].intf_offset; i++) {
  926. struct usbd_interface *intf = g_usbd_core[busid].intf[i];
  927. if (intf && intf->vendor_handler && (intf->vendor_handler(busid, setup, data, len) == 0)) {
  928. return 0;
  929. }
  930. }
  931. return -1;
  932. }
  933. /**
  934. * @brief handle setup request( standard/class/vendor/other)
  935. *
  936. * @param [in] busid busid
  937. * @param [in] setup The setup packet
  938. * @param [in,out] data Data buffer
  939. * @param [in,out] len Pointer to data length
  940. *
  941. * @return true if the request was handles successfully
  942. */
  943. static bool usbd_setup_request_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
  944. {
  945. switch (setup->bmRequestType & USB_REQUEST_TYPE_MASK) {
  946. case USB_REQUEST_STANDARD:
  947. if (usbd_standard_request_handler(busid, setup, data, len) < 0) {
  948. /* Ignore error log for getting Device Qualifier Descriptor request */
  949. if ((setup->bRequest == 0x06) && (setup->wValue == 0x0600)) {
  950. //USB_LOG_DBG("Ignore DQD in fs\r\n");
  951. return false;
  952. }
  953. USB_LOG_ERR("standard request error\r\n");
  954. usbd_print_setup(setup);
  955. return false;
  956. }
  957. break;
  958. case USB_REQUEST_CLASS:
  959. if (usbd_class_request_handler(busid, setup, data, len) < 0) {
  960. USB_LOG_ERR("class request error\r\n");
  961. usbd_print_setup(setup);
  962. return false;
  963. }
  964. break;
  965. case USB_REQUEST_VENDOR:
  966. if (usbd_vendor_request_handler(busid, setup, data, len) < 0) {
  967. USB_LOG_ERR("vendor request error\r\n");
  968. usbd_print_setup(setup);
  969. return false;
  970. }
  971. break;
  972. default:
  973. return false;
  974. }
  975. return true;
  976. }
  977. static void usbd_class_event_notify_handler(uint8_t busid, uint8_t event, void *arg)
  978. {
  979. for (uint8_t i = 0; i < g_usbd_core[busid].intf_offset; i++) {
  980. struct usbd_interface *intf = g_usbd_core[busid].intf[i];
  981. if (arg) {
  982. struct usb_interface_descriptor *desc = (struct usb_interface_descriptor *)arg;
  983. if (intf && intf->notify_handler && (desc->bInterfaceNumber == (intf->intf_num))) {
  984. intf->notify_handler(busid, event, arg);
  985. }
  986. } else {
  987. if (intf && intf->notify_handler) {
  988. intf->notify_handler(busid, event, arg);
  989. }
  990. }
  991. }
  992. }
  993. void usbd_event_sof_handler(uint8_t busid)
  994. {
  995. g_usbd_core[busid].event_handler(busid, USBD_EVENT_SOF);
  996. }
  997. void usbd_event_connect_handler(uint8_t busid)
  998. {
  999. g_usbd_core[busid].event_handler(busid, USBD_EVENT_CONNECTED);
  1000. }
  1001. void usbd_event_disconnect_handler(uint8_t busid)
  1002. {
  1003. g_usbd_core[busid].configuration = 0;
  1004. g_usbd_core[busid].event_handler(busid, USBD_EVENT_DISCONNECTED);
  1005. }
  1006. void usbd_event_resume_handler(uint8_t busid)
  1007. {
  1008. g_usbd_core[busid].is_suspend = false;
  1009. g_usbd_core[busid].event_handler(busid, USBD_EVENT_RESUME);
  1010. }
  1011. void usbd_event_suspend_handler(uint8_t busid)
  1012. {
  1013. if (g_usbd_core[busid].device_address > 0) {
  1014. g_usbd_core[busid].is_suspend = true;
  1015. g_usbd_core[busid].event_handler(busid, USBD_EVENT_SUSPEND);
  1016. }
  1017. }
  1018. void usbd_event_reset_handler(uint8_t busid)
  1019. {
  1020. usbd_set_address(busid, 0);
  1021. g_usbd_core[busid].device_address = 0;
  1022. g_usbd_core[busid].configuration = 0;
  1023. g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_SETUP;
  1024. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  1025. g_usbd_core[busid].speed = USB_SPEED_UNKNOWN;
  1026. #endif
  1027. struct usb_endpoint_descriptor ep0;
  1028. ep0.bLength = 7;
  1029. ep0.bDescriptorType = USB_DESCRIPTOR_TYPE_ENDPOINT;
  1030. ep0.wMaxPacketSize = USB_CTRL_EP_MPS;
  1031. ep0.bmAttributes = USB_ENDPOINT_TYPE_CONTROL;
  1032. ep0.bEndpointAddress = USB_CONTROL_IN_EP0;
  1033. ep0.bInterval = 0;
  1034. usbd_ep_open(busid, &ep0);
  1035. ep0.bEndpointAddress = USB_CONTROL_OUT_EP0;
  1036. usbd_ep_open(busid, &ep0);
  1037. usbd_class_event_notify_handler(busid, USBD_EVENT_RESET, NULL);
  1038. g_usbd_core[busid].event_handler(busid, USBD_EVENT_RESET);
  1039. }
  1040. static void __usbd_event_ep0_setup_complete_handler(uint8_t busid, struct usb_setup_packet *setup)
  1041. {
  1042. uint8_t *buf;
  1043. USB_LOG_DBG("[%s] 0x%02x 0x%02x 0x%04x 0x%04x 0x%04x\r\n",
  1044. usb_ep0_state_string[usbd_get_ep0_next_state(busid)],
  1045. setup->bmRequestType,
  1046. setup->bRequest,
  1047. setup->wValue,
  1048. setup->wIndex,
  1049. setup->wLength);
  1050. if (setup->wLength > CONFIG_USBDEV_REQUEST_BUFFER_LEN) {
  1051. if ((setup->bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_OUT) {
  1052. USB_LOG_ERR("Request buffer too small\r\n");
  1053. usbd_ep_set_stall(busid, USB_CONTROL_IN_EP0);
  1054. return;
  1055. }
  1056. }
  1057. g_usbd_core[busid].ep0_data_buf = g_usbd_core[busid].req_data;
  1058. g_usbd_core[busid].ep0_data_buf_residue = setup->wLength;
  1059. g_usbd_core[busid].ep0_data_buf_len = setup->wLength;
  1060. g_usbd_core[busid].zlp_flag = false;
  1061. buf = g_usbd_core[busid].ep0_data_buf;
  1062. /* handle class request when all the data is received */
  1063. if (setup->wLength && ((setup->bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_OUT)) {
  1064. USB_LOG_DBG("Start reading %d bytes from ep0\r\n", setup->wLength);
  1065. g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_OUT_DATA;
  1066. usbd_ep_start_read(busid, USB_CONTROL_OUT_EP0, g_usbd_core[busid].ep0_data_buf, setup->wLength);
  1067. return;
  1068. }
  1069. /* Ask installed handler to process request */
  1070. if (!usbd_setup_request_handler(busid, setup, &buf, &g_usbd_core[busid].ep0_data_buf_len)) {
  1071. g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_SETUP;
  1072. usbd_ep_set_stall(busid, USB_CONTROL_IN_EP0);
  1073. return;
  1074. }
  1075. /* Send smallest of requested and offered length */
  1076. g_usbd_core[busid].ep0_data_buf_residue = MIN(g_usbd_core[busid].ep0_data_buf_len, setup->wLength);
  1077. if (g_usbd_core[busid].ep0_data_buf_residue > CONFIG_USBDEV_REQUEST_BUFFER_LEN) {
  1078. USB_LOG_ERR("Request buffer too small\r\n");
  1079. g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_SETUP;
  1080. usbd_ep_set_stall(busid, USB_CONTROL_IN_EP0);
  1081. return;
  1082. }
  1083. /* use *data = xxx; g_usbd_core[busid].ep0_data_buf records real data address, we should copy data into ep0 buffer.
  1084. * 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).
  1085. */
  1086. if (buf != g_usbd_core[busid].ep0_data_buf) {
  1087. #ifdef CONFIG_USBDEV_EP0_INDATA_NO_COPY
  1088. g_usbd_core[busid].ep0_data_buf = buf;
  1089. #else
  1090. usb_memcpy(g_usbd_core[busid].ep0_data_buf, buf, g_usbd_core[busid].ep0_data_buf_residue);
  1091. #endif
  1092. } else {
  1093. /* use memcpy(*data, xxx, len); has copied into ep0 buffer, we do nothing */
  1094. }
  1095. if (g_usbd_core[busid].ep0_data_buf_residue > 0) {
  1096. g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_IN_DATA;
  1097. } else {
  1098. g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_IN_STATUS;
  1099. }
  1100. /* Send data or status to host */
  1101. usbd_ep_start_write(busid, USB_CONTROL_IN_EP0, g_usbd_core[busid].ep0_data_buf, g_usbd_core[busid].ep0_data_buf_residue);
  1102. /*
  1103. * Set ZLP flag when host asks for a bigger length and the data size is
  1104. * multiplier of USB_CTRL_EP_MPS, to indicate the transfer done after zlp
  1105. * sent.
  1106. */
  1107. if ((setup->wLength > g_usbd_core[busid].ep0_data_buf_len) && (!(g_usbd_core[busid].ep0_data_buf_len % USB_CTRL_EP_MPS))) {
  1108. g_usbd_core[busid].zlp_flag = true;
  1109. }
  1110. }
  1111. void usbd_event_ep0_setup_complete_handler(uint8_t busid, uint8_t *psetup)
  1112. {
  1113. struct usb_setup_packet *setup = &g_usbd_core[busid].setup;
  1114. memcpy(setup, psetup, 8);
  1115. #ifdef CONFIG_USBDEV_EP0_THREAD
  1116. usb_osal_mq_send(g_usbd_core[busid].usbd_ep0_mq, USB_EP0_STATE_SETUP);
  1117. #else
  1118. __usbd_event_ep0_setup_complete_handler(busid, setup);
  1119. #endif
  1120. }
  1121. static void usbd_event_ep0_in_complete_handler(uint8_t busid, uint8_t ep, uint32_t nbytes)
  1122. {
  1123. struct usb_setup_packet *setup = &g_usbd_core[busid].setup;
  1124. (void)ep;
  1125. g_usbd_core[busid].ep0_data_buf += nbytes;
  1126. g_usbd_core[busid].ep0_data_buf_residue -= nbytes;
  1127. USB_LOG_DBG("[%s] in %d bytes, %d remained\r\n",
  1128. usb_ep0_state_string[usbd_get_ep0_next_state(busid)],
  1129. (unsigned int)nbytes,
  1130. (unsigned int)g_usbd_core[busid].ep0_data_buf_residue);
  1131. if (g_usbd_core[busid].ep0_data_buf_residue != 0) {
  1132. /* Start sending the remain data */
  1133. usbd_ep_start_write(busid, USB_CONTROL_IN_EP0, g_usbd_core[busid].ep0_data_buf, g_usbd_core[busid].ep0_data_buf_residue);
  1134. } else {
  1135. if (g_usbd_core[busid].zlp_flag == true) {
  1136. g_usbd_core[busid].zlp_flag = false;
  1137. /* Send zlp to host */
  1138. USB_LOG_DBG("EP0 Send zlp\r\n");
  1139. usbd_ep_start_write(busid, USB_CONTROL_IN_EP0, NULL, 0);
  1140. } else {
  1141. /* Satisfying three conditions will jump here.
  1142. * 1. send status completely
  1143. * 2. send zlp completely
  1144. * 3. send last data completely.
  1145. */
  1146. if (setup->wLength && ((setup->bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_IN)) {
  1147. /* if all data has sent completely, start reading out status */
  1148. g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_OUT_STATUS;
  1149. usbd_ep_start_read(busid, USB_CONTROL_OUT_EP0, NULL, 0);
  1150. return;
  1151. }
  1152. if (g_usbd_core[busid].ep0_next_state == USBD_EP0_STATE_IN_STATUS) {
  1153. g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_SETUP;
  1154. }
  1155. #ifdef CONFIG_USBDEV_TEST_MODE
  1156. if (g_usbd_core[busid].test_req) {
  1157. usbd_execute_test_mode(busid, HI_BYTE(setup->wIndex));
  1158. g_usbd_core[busid].test_req = false;
  1159. }
  1160. #endif
  1161. }
  1162. }
  1163. }
  1164. static void usbd_event_ep0_out_complete_handler(uint8_t busid, uint8_t ep, uint32_t nbytes)
  1165. {
  1166. struct usb_setup_packet *setup = &g_usbd_core[busid].setup;
  1167. (void)ep;
  1168. (void)setup;
  1169. USB_LOG_DBG("[%s] out %d bytes, %d remained\r\n",
  1170. usb_ep0_state_string[usbd_get_ep0_next_state(busid)],
  1171. (unsigned int)nbytes,
  1172. (unsigned int)g_usbd_core[busid].ep0_data_buf_residue);
  1173. if (nbytes > 0) {
  1174. g_usbd_core[busid].ep0_data_buf += nbytes;
  1175. g_usbd_core[busid].ep0_data_buf_residue -= nbytes;
  1176. if (g_usbd_core[busid].ep0_data_buf_residue == 0) {
  1177. #ifdef CONFIG_USBDEV_EP0_THREAD
  1178. usb_osal_mq_send(g_usbd_core[busid].usbd_ep0_mq, USB_EP0_STATE_OUT);
  1179. #else
  1180. /* Received all, send data to handler */
  1181. g_usbd_core[busid].ep0_data_buf = g_usbd_core[busid].req_data;
  1182. if (!usbd_setup_request_handler(busid, setup, &g_usbd_core[busid].ep0_data_buf, &g_usbd_core[busid].ep0_data_buf_len)) {
  1183. g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_SETUP;
  1184. usbd_ep_set_stall(busid, USB_CONTROL_IN_EP0);
  1185. return;
  1186. }
  1187. g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_IN_STATUS;
  1188. /*Send status to host*/
  1189. usbd_ep_start_write(busid, USB_CONTROL_IN_EP0, NULL, 0);
  1190. #endif
  1191. } else {
  1192. /* Start reading the remain data */
  1193. usbd_ep_start_read(busid, USB_CONTROL_OUT_EP0, g_usbd_core[busid].ep0_data_buf, g_usbd_core[busid].ep0_data_buf_residue);
  1194. }
  1195. } else {
  1196. /* Read out status completely, do nothing */
  1197. g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_SETUP;
  1198. }
  1199. }
  1200. void usbd_event_ep_in_complete_handler(uint8_t busid, uint8_t ep, uint32_t nbytes)
  1201. {
  1202. if (g_usbd_core[busid].tx_msg[ep & 0x7f].cb) {
  1203. g_usbd_core[busid].tx_msg[ep & 0x7f].cb(busid, ep, nbytes);
  1204. }
  1205. }
  1206. void usbd_event_ep_out_complete_handler(uint8_t busid, uint8_t ep, uint32_t nbytes)
  1207. {
  1208. if (g_usbd_core[busid].rx_msg[ep & 0x7f].cb) {
  1209. g_usbd_core[busid].rx_msg[ep & 0x7f].cb(busid, ep, nbytes);
  1210. }
  1211. }
  1212. #ifdef CONFIG_USBDEV_ADVANCE_DESC
  1213. void usbd_desc_register(uint8_t busid, const struct usb_descriptor *desc)
  1214. {
  1215. memset(&g_usbd_core[busid], 0, sizeof(struct usbd_core_priv));
  1216. g_usbd_core[busid].descriptors = desc;
  1217. g_usbd_core[busid].intf_offset = 0;
  1218. g_usbd_core[busid].tx_msg[0].ep = 0x80;
  1219. g_usbd_core[busid].tx_msg[0].cb = usbd_event_ep0_in_complete_handler;
  1220. g_usbd_core[busid].rx_msg[0].ep = 0x00;
  1221. g_usbd_core[busid].rx_msg[0].cb = usbd_event_ep0_out_complete_handler;
  1222. }
  1223. #else
  1224. void usbd_desc_register(uint8_t busid, const uint8_t *desc)
  1225. {
  1226. memset(&g_usbd_core[busid], 0, sizeof(struct usbd_core_priv));
  1227. g_usbd_core[busid].descriptors = desc;
  1228. g_usbd_core[busid].intf_offset = 0;
  1229. g_usbd_core[busid].tx_msg[0].ep = 0x80;
  1230. g_usbd_core[busid].tx_msg[0].cb = usbd_event_ep0_in_complete_handler;
  1231. g_usbd_core[busid].rx_msg[0].ep = 0x00;
  1232. g_usbd_core[busid].rx_msg[0].cb = usbd_event_ep0_out_complete_handler;
  1233. }
  1234. /* Register MS OS Descriptors version 1 */
  1235. void usbd_msosv1_desc_register(uint8_t busid, struct usb_msosv1_descriptor *desc)
  1236. {
  1237. g_usbd_core[busid].msosv1_desc = desc;
  1238. }
  1239. /* Register MS OS Descriptors version 2 */
  1240. void usbd_msosv2_desc_register(uint8_t busid, struct usb_msosv2_descriptor *desc)
  1241. {
  1242. g_usbd_core[busid].msosv2_desc = desc;
  1243. }
  1244. void usbd_bos_desc_register(uint8_t busid, struct usb_bos_descriptor *desc)
  1245. {
  1246. g_usbd_core[busid].bos_desc = desc;
  1247. }
  1248. void usbd_webusb_desc_register(uint8_t busid, struct usb_webusb_descriptor *desc)
  1249. {
  1250. g_usbd_core[busid].webusb_url_desc = desc;
  1251. }
  1252. #endif
  1253. void usbd_add_interface(uint8_t busid, struct usbd_interface *intf)
  1254. {
  1255. intf->intf_num = g_usbd_core[busid].intf_offset;
  1256. g_usbd_core[busid].intf[g_usbd_core[busid].intf_offset] = intf;
  1257. g_usbd_core[busid].intf_offset++;
  1258. }
  1259. void usbd_add_endpoint(uint8_t busid, struct usbd_endpoint *ep)
  1260. {
  1261. if (ep->ep_addr & 0x80) {
  1262. g_usbd_core[busid].tx_msg[ep->ep_addr & 0x7f].ep = ep->ep_addr;
  1263. g_usbd_core[busid].tx_msg[ep->ep_addr & 0x7f].cb = ep->ep_cb;
  1264. } else {
  1265. g_usbd_core[busid].rx_msg[ep->ep_addr & 0x7f].ep = ep->ep_addr;
  1266. g_usbd_core[busid].rx_msg[ep->ep_addr & 0x7f].cb = ep->ep_cb;
  1267. }
  1268. }
  1269. uint16_t usbd_get_ep_mps(uint8_t busid, uint8_t ep)
  1270. {
  1271. if (ep & 0x80) {
  1272. return g_usbd_core[busid].tx_msg[ep & 0x7f].ep_mps;
  1273. } else {
  1274. return g_usbd_core[busid].rx_msg[ep & 0x7f].ep_mps;
  1275. }
  1276. }
  1277. uint8_t usbd_get_ep_mult(uint8_t busid, uint8_t ep)
  1278. {
  1279. if (ep & 0x80) {
  1280. return g_usbd_core[busid].tx_msg[ep & 0x7f].ep_mult;
  1281. } else {
  1282. return g_usbd_core[busid].rx_msg[ep & 0x7f].ep_mult;
  1283. }
  1284. }
  1285. bool usb_device_is_configured(uint8_t busid)
  1286. {
  1287. return g_usbd_core[busid].configuration;
  1288. }
  1289. bool usb_device_is_suspend(uint8_t busid)
  1290. {
  1291. return g_usbd_core[busid].is_suspend;
  1292. }
  1293. int usbd_send_remote_wakeup(uint8_t busid)
  1294. {
  1295. if (g_usbd_core[busid].remote_wakeup_support && g_usbd_core[busid].remote_wakeup_enabled && g_usbd_core[busid].is_suspend) {
  1296. return usbd_set_remote_wakeup(busid);
  1297. } else {
  1298. if (!g_usbd_core[busid].remote_wakeup_support) {
  1299. USB_LOG_ERR("device does not support remote wakeup\r\n");
  1300. }
  1301. if (!g_usbd_core[busid].remote_wakeup_enabled) {
  1302. USB_LOG_ERR("device remote wakeup is not enabled\r\n");
  1303. }
  1304. if (!g_usbd_core[busid].is_suspend) {
  1305. USB_LOG_ERR("device is not in suspend state\r\n");
  1306. }
  1307. return -1;
  1308. }
  1309. }
  1310. uint8_t usbd_get_ep0_next_state(uint8_t busid)
  1311. {
  1312. return g_usbd_core[busid].ep0_next_state;
  1313. }
  1314. #ifdef CONFIG_USBDEV_EP0_THREAD
  1315. static void usbdev_ep0_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
  1316. {
  1317. uintptr_t event;
  1318. int ret;
  1319. uint8_t busid = (uint8_t)CONFIG_USB_OSAL_THREAD_GET_ARGV;
  1320. struct usb_setup_packet *setup = &g_usbd_core[busid].setup;
  1321. while (1) {
  1322. ret = usb_osal_mq_recv(g_usbd_core[busid].usbd_ep0_mq, (uintptr_t *)&event, USB_OSAL_WAITING_FOREVER);
  1323. if (ret < 0) {
  1324. continue;
  1325. }
  1326. USB_LOG_DBG("event:%d\r\n", (unsigned int)event);
  1327. switch (event) {
  1328. case USB_EP0_STATE_SETUP:
  1329. __usbd_event_ep0_setup_complete_handler(busid, setup);
  1330. break;
  1331. case USB_EP0_STATE_IN:
  1332. // do nothing
  1333. break;
  1334. case USB_EP0_STATE_OUT:
  1335. /* Received all, send data to handler */
  1336. g_usbd_core[busid].ep0_data_buf = g_usbd_core[busid].req_data;
  1337. if (!usbd_setup_request_handler(busid, setup, &g_usbd_core[busid].ep0_data_buf, &g_usbd_core[busid].ep0_data_buf_len)) {
  1338. g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_SETUP;
  1339. usbd_ep_set_stall(busid, USB_CONTROL_IN_EP0);
  1340. continue;
  1341. }
  1342. g_usbd_core[busid].ep0_next_state = USBD_EP0_STATE_IN_STATUS;
  1343. /*Send status to host*/
  1344. usbd_ep_start_write(busid, USB_CONTROL_IN_EP0, NULL, 0);
  1345. break;
  1346. default:
  1347. break;
  1348. }
  1349. }
  1350. }
  1351. #endif
  1352. int usbd_initialize(uint8_t busid, uintptr_t reg_base, void (*event_handler)(uint8_t busid, uint8_t event))
  1353. {
  1354. int ret;
  1355. struct usbd_bus *bus;
  1356. if (busid >= CONFIG_USBDEV_MAX_BUS) {
  1357. USB_LOG_ERR("bus overflow\r\n");
  1358. while (1) {
  1359. }
  1360. }
  1361. bus = &g_usbdev_bus[busid];
  1362. bus->reg_base = reg_base;
  1363. #ifdef CONFIG_USBDEV_EP0_THREAD
  1364. g_usbd_core[busid].usbd_ep0_mq = usb_osal_mq_create(1);
  1365. if (g_usbd_core[busid].usbd_ep0_mq == NULL) {
  1366. USB_LOG_ERR("No memory to alloc for g_usbd_core[busid].usbd_ep0_mq\r\n");
  1367. while (1) {
  1368. }
  1369. }
  1370. 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);
  1371. if (g_usbd_core[busid].usbd_ep0_thread == NULL) {
  1372. USB_LOG_ERR("No memory to alloc for g_usbd_core[busid].usbd_ep0_thread\r\n");
  1373. while (1) {
  1374. }
  1375. }
  1376. #endif
  1377. g_usbd_core[busid].event_handler = event_handler;
  1378. ret = usb_dc_init(busid);
  1379. usbd_class_event_notify_handler(busid, USBD_EVENT_INIT, NULL);
  1380. g_usbd_core[busid].event_handler(busid, USBD_EVENT_INIT);
  1381. return ret;
  1382. }
  1383. int usbd_deinitialize(uint8_t busid)
  1384. {
  1385. if (busid >= CONFIG_USBDEV_MAX_BUS) {
  1386. USB_LOG_ERR("bus overflow\r\n");
  1387. while (1) {
  1388. }
  1389. }
  1390. g_usbd_core[busid].event_handler(busid, USBD_EVENT_DEINIT);
  1391. usbd_class_event_notify_handler(busid, USBD_EVENT_DEINIT, NULL);
  1392. usb_dc_deinit(busid);
  1393. g_usbd_core[busid].intf_offset = 0;
  1394. #ifdef CONFIG_USBDEV_EP0_THREAD
  1395. if (g_usbd_core[busid].usbd_ep0_mq) {
  1396. usb_osal_mq_delete(g_usbd_core[busid].usbd_ep0_mq);
  1397. }
  1398. if (g_usbd_core[busid].usbd_ep0_thread) {
  1399. usb_osal_thread_delete(g_usbd_core[busid].usbd_ep0_thread);
  1400. }
  1401. #endif
  1402. return 0;
  1403. }