usbd_core.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  1. /**
  2. * @file usbd_core.c
  3. * @brief
  4. *
  5. * Copyright (c) 2022 sakumisu
  6. *
  7. * Licensed to the Apache Software Foundation (ASF) under one or more
  8. * contributor license agreements. See the NOTICE file distributed with
  9. * this work for additional information regarding copyright ownership. The
  10. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  11. * "License"); you may not use this file except in compliance with the
  12. * License. You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  19. * License for the specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. */
  23. #include "usbd_core.h"
  24. #define USBD_EP_CALLBACK_LIST_SEARCH 0
  25. #define USBD_EP_CALLBACK_ARR_SEARCH 1
  26. #define USBD_EP_CALLBACK_SEARCH_METHOD USBD_EP_CALLBACK_ARR_SEARCH
  27. /* general descriptor field offsets */
  28. #define DESC_bLength 0 /** Length offset */
  29. #define DESC_bDescriptorType 1 /** Descriptor type offset */
  30. /* config descriptor field offsets */
  31. #define CONF_DESC_wTotalLength 2 /** Total length offset */
  32. #define CONF_DESC_bConfigurationValue 5 /** Configuration value offset */
  33. #define CONF_DESC_bmAttributes 7 /** configuration characteristics */
  34. /* interface descriptor field offsets */
  35. #define INTF_DESC_bInterfaceNumber 2 /** Interface number offset */
  36. #define INTF_DESC_bAlternateSetting 3 /** Alternate setting offset */
  37. #define USB_REQUEST_BUFFER_SIZE 256
  38. #define USB_EP_OUT_NUM 8
  39. #define USB_EP_IN_NUM 8
  40. static struct usbd_core_cfg_priv {
  41. /** Setup packet */
  42. struct usb_setup_packet setup;
  43. /** Pointer to data buffer */
  44. uint8_t *ep0_data_buf;
  45. /** Remaining bytes in buffer */
  46. uint32_t ep0_data_buf_residue;
  47. /** Total length of control transfer */
  48. uint32_t ep0_data_buf_len;
  49. /** Zero length packet flag of control transfer */
  50. bool zlp_flag;
  51. /** Pointer to registered descriptors */
  52. const uint8_t *descriptors;
  53. /* Buffer used for storing standard, class and vendor request data */
  54. uint8_t req_data[USB_REQUEST_BUFFER_SIZE];
  55. #if USBD_EP_CALLBACK_SEARCH_METHOD == USBD_EP_CALLBACK_ARR_SEARCH
  56. usbd_endpoint_callback in_ep_cb[USB_EP_IN_NUM];
  57. usbd_endpoint_callback out_ep_cb[USB_EP_OUT_NUM];
  58. #endif
  59. /** Variable to check whether the usb has been configured */
  60. bool configured;
  61. /** Currently selected configuration */
  62. uint8_t configuration;
  63. /** Remote wakeup feature status */
  64. uint16_t remote_wakeup;
  65. uint8_t reserved;
  66. } usbd_core_cfg;
  67. static usb_slist_t usbd_class_head = USB_SLIST_OBJECT_INIT(usbd_class_head);
  68. static struct usb_msosv1_descriptor *msosv1_desc;
  69. static struct usb_msosv2_descriptor *msosv2_desc;
  70. static struct usb_bos_descriptor *bos_desc;
  71. /**
  72. * @brief print the contents of a setup packet
  73. *
  74. * @param [in] setup The setup packet
  75. *
  76. */
  77. static void usbd_print_setup(struct usb_setup_packet *setup)
  78. {
  79. USB_LOG_INFO("Setup: "
  80. "bmRequestType 0x%02x, bRequest 0x%02x, wValue 0x%04x, wIndex 0x%04x, wLength 0x%04x\r\n",
  81. setup->bmRequestType,
  82. setup->bRequest,
  83. setup->wValue,
  84. setup->wIndex,
  85. setup->wLength);
  86. }
  87. /**
  88. * @brief Check if the device is in Configured state
  89. *
  90. * @return true if Configured, false otherwise.
  91. */
  92. static bool is_device_configured(void)
  93. {
  94. return (usbd_core_cfg.configuration != 0);
  95. }
  96. /**
  97. * @brief Check if the interface of given number is valid
  98. *
  99. * @param [in] interface Number of the addressed interface
  100. *
  101. * This function searches through descriptor and checks
  102. * is the Host has addressed valid interface.
  103. *
  104. * @return true if interface exists - valid
  105. */
  106. static bool is_interface_valid(uint8_t interface)
  107. {
  108. const uint8_t *p = (uint8_t *)usbd_core_cfg.descriptors;
  109. const struct usb_configuration_descriptor *cfg_descr;
  110. /* Search through descriptor for matching interface */
  111. while (p[DESC_bLength] != 0U) {
  112. if (p[DESC_bDescriptorType] == USB_DESCRIPTOR_TYPE_CONFIGURATION) {
  113. cfg_descr = (const struct usb_configuration_descriptor *)p;
  114. if (interface < cfg_descr->bNumInterfaces) {
  115. return true;
  116. }
  117. }
  118. p += p[DESC_bLength];
  119. }
  120. return false;
  121. }
  122. /**
  123. * @brief Check if the endpoint of given address is valid
  124. *
  125. * @param [in] ep Address of the Endpoint
  126. *
  127. * This function checks if the Endpoint of given address
  128. * is valid for the configured device. Valid Endpoint is
  129. * either Control Endpoint or one used by the device.
  130. *
  131. * @return true if endpoint exists - valid
  132. */
  133. static bool is_ep_valid(uint8_t ep)
  134. {
  135. /* Check if its Endpoint 0 */
  136. if ((ep & 0x7f) == 0) {
  137. return true;
  138. }
  139. return true;
  140. }
  141. #if USBD_EP_CALLBACK_SEARCH_METHOD == USBD_EP_CALLBACK_ARR_SEARCH
  142. static void usbd_ep_callback_register(void)
  143. {
  144. usb_slist_t *i, *j, *k;
  145. usb_slist_for_each(i, &usbd_class_head)
  146. {
  147. usbd_class_t *class = usb_slist_entry(i, struct usbd_class, list);
  148. usb_slist_for_each(j, &class->intf_list)
  149. {
  150. usbd_interface_t *intf = usb_slist_entry(j, struct usbd_interface, list);
  151. usb_slist_for_each(k, &intf->ep_list)
  152. {
  153. usbd_endpoint_t *ept = usb_slist_entry(k, struct usbd_endpoint, list);
  154. if (ept->ep_cb) {
  155. if (ept->ep_addr & 0x80) {
  156. usbd_core_cfg.in_ep_cb[ept->ep_addr & 0x7f] = ept->ep_cb;
  157. } else {
  158. usbd_core_cfg.out_ep_cb[ept->ep_addr & 0x7f] = ept->ep_cb;
  159. }
  160. }
  161. }
  162. }
  163. }
  164. }
  165. #endif
  166. /**
  167. * @brief configure and enable endpoint
  168. *
  169. * This function sets endpoint configuration according to one specified in USB
  170. * endpoint descriptor and then enables it for data transfers.
  171. *
  172. * @param [in] ep_desc Endpoint descriptor byte array
  173. *
  174. * @return true if successfully configured and enabled
  175. */
  176. static bool usbd_set_endpoint(const struct usb_endpoint_descriptor *ep_desc)
  177. {
  178. struct usbd_endpoint_cfg ep_cfg;
  179. ep_cfg.ep_addr = ep_desc->bEndpointAddress;
  180. ep_cfg.ep_mps = ep_desc->wMaxPacketSize;
  181. ep_cfg.ep_type = ep_desc->bmAttributes & USBD_EP_TYPE_MASK;
  182. USB_LOG_INFO("Open endpoint:0x%x type:%u mps:%u\r\n",
  183. ep_cfg.ep_addr, ep_cfg.ep_type, ep_cfg.ep_mps);
  184. usbd_ep_open(&ep_cfg);
  185. usbd_core_cfg.configured = true;
  186. return true;
  187. }
  188. /**
  189. * @brief Disable endpoint for transferring data
  190. *
  191. * This function cancels transfers that are associated with endpoint and
  192. * disabled endpoint itself.
  193. *
  194. * @param [in] ep_desc Endpoint descriptor byte array
  195. *
  196. * @return true if successfully deconfigured and disabled
  197. */
  198. static bool usbd_reset_endpoint(const struct usb_endpoint_descriptor *ep_desc)
  199. {
  200. struct usbd_endpoint_cfg ep_cfg;
  201. ep_cfg.ep_addr = ep_desc->bEndpointAddress;
  202. ep_cfg.ep_mps = ep_desc->wMaxPacketSize;
  203. ep_cfg.ep_type = ep_desc->bmAttributes & USBD_EP_TYPE_MASK;
  204. USB_LOG_INFO("Close endpoint:0x%x type:%u\r\n",
  205. ep_cfg.ep_addr, ep_cfg.ep_type);
  206. usbd_ep_close(ep_cfg.ep_addr);
  207. return true;
  208. }
  209. /**
  210. * @brief get specified USB descriptor
  211. *
  212. * This function parses the list of installed USB descriptors and attempts
  213. * to find the specified USB descriptor.
  214. *
  215. * @param [in] type_index Type and index of the descriptor
  216. * @param [out] data Descriptor data
  217. * @param [out] len Descriptor length
  218. *
  219. * @return true if the descriptor was found, false otherwise
  220. */
  221. static bool usbd_get_descriptor(uint16_t type_index, uint8_t **data, uint32_t *len)
  222. {
  223. uint8_t type = 0U;
  224. uint8_t index = 0U;
  225. uint8_t *p = NULL;
  226. uint32_t cur_index = 0U;
  227. bool found = false;
  228. type = GET_DESC_TYPE(type_index);
  229. index = GET_DESC_INDEX(type_index);
  230. if ((type == USB_DESCRIPTOR_TYPE_STRING) && (index == USB_OSDESC_STRING_DESC_INDEX)) {
  231. USB_LOG_INFO("read MS OS 2.0 descriptor string\r\n");
  232. if (!msosv1_desc) {
  233. return false;
  234. }
  235. *data = (uint8_t *)msosv1_desc->string;
  236. *len = msosv1_desc->string_len;
  237. return true;
  238. } else if (type == USB_DESCRIPTOR_TYPE_BINARY_OBJECT_STORE) {
  239. USB_LOG_INFO("read BOS descriptor string\r\n");
  240. if (!bos_desc) {
  241. return false;
  242. }
  243. *data = bos_desc->string;
  244. *len = bos_desc->string_len;
  245. return true;
  246. }
  247. /*
  248. * Invalid types of descriptors,
  249. * see USB Spec. Revision 2.0, 9.4.3 Get Descriptor
  250. */
  251. else if ((type == USB_DESCRIPTOR_TYPE_INTERFACE) || (type == USB_DESCRIPTOR_TYPE_ENDPOINT) ||
  252. #ifndef CONFIG_USB_HS
  253. (type > USB_DESCRIPTOR_TYPE_ENDPOINT)) {
  254. #else
  255. (type > USB_DESCRIPTOR_TYPE_OTHER_SPEED)) {
  256. #endif
  257. return false;
  258. }
  259. p = (uint8_t *)usbd_core_cfg.descriptors;
  260. cur_index = 0U;
  261. while (p[DESC_bLength] != 0U) {
  262. if (p[DESC_bDescriptorType] == type) {
  263. if (cur_index == index) {
  264. found = true;
  265. break;
  266. }
  267. cur_index++;
  268. }
  269. /* skip to next descriptor */
  270. p += p[DESC_bLength];
  271. }
  272. if (found) {
  273. /* set data pointer */
  274. *data = p;
  275. /* get length from structure */
  276. if (type == USB_DESCRIPTOR_TYPE_CONFIGURATION) {
  277. /* configuration descriptor is an
  278. * exception, length is at offset
  279. * 2 and 3
  280. */
  281. *len = (p[CONF_DESC_wTotalLength]) |
  282. (p[CONF_DESC_wTotalLength + 1] << 8);
  283. } else {
  284. /* normally length is at offset 0 */
  285. *len = p[DESC_bLength];
  286. }
  287. } else {
  288. /* nothing found */
  289. USB_LOG_ERR("descriptor <type:%x,index:%x> not found!\r\n", type, index);
  290. }
  291. return found;
  292. }
  293. /**
  294. * @brief set USB configuration
  295. *
  296. * This function configures the device according to the specified configuration
  297. * index and alternate setting by parsing the installed USB descriptor list.
  298. * A configuration index of 0 unconfigures the device.
  299. *
  300. * @param [in] config_index Configuration index
  301. * @param [in] alt_setting Alternate setting number
  302. *
  303. * @return true if successfully configured false if error or unconfigured
  304. */
  305. static bool usbd_set_configuration(uint8_t config_index, uint8_t alt_setting)
  306. {
  307. uint8_t *p = (uint8_t *)usbd_core_cfg.descriptors;
  308. uint8_t cur_alt_setting = 0xFF;
  309. uint8_t cur_config = 0xFF;
  310. bool found = false;
  311. if (config_index == 0U) {
  312. /* TODO: unconfigure device */
  313. USB_LOG_ERR("Device not configured - invalid configuration\r\n");
  314. return true;
  315. }
  316. /* configure endpoints for this configuration/altsetting */
  317. while (p[DESC_bLength] != 0U) {
  318. switch (p[DESC_bDescriptorType]) {
  319. case USB_DESCRIPTOR_TYPE_CONFIGURATION:
  320. /* remember current configuration index */
  321. cur_config = p[CONF_DESC_bConfigurationValue];
  322. if (cur_config == config_index) {
  323. found = true;
  324. }
  325. break;
  326. case USB_DESCRIPTOR_TYPE_INTERFACE:
  327. /* remember current alternate setting */
  328. cur_alt_setting =
  329. p[INTF_DESC_bAlternateSetting];
  330. break;
  331. case USB_DESCRIPTOR_TYPE_ENDPOINT:
  332. if ((cur_config != config_index) ||
  333. (cur_alt_setting != alt_setting)) {
  334. break;
  335. }
  336. found = usbd_set_endpoint((struct usb_endpoint_descriptor *)p);
  337. break;
  338. default:
  339. break;
  340. }
  341. /* skip to next descriptor */
  342. p += p[DESC_bLength];
  343. }
  344. return found;
  345. }
  346. /**
  347. * @brief set USB interface
  348. *
  349. * @param [in] iface Interface index
  350. * @param [in] alt_setting Alternate setting number
  351. *
  352. * @return true if successfully configured false if error or unconfigured
  353. */
  354. static bool usbd_set_interface(uint8_t iface, uint8_t alt_setting)
  355. {
  356. const uint8_t *p = usbd_core_cfg.descriptors;
  357. const uint8_t *if_desc = NULL;
  358. struct usb_endpoint_descriptor *ep_desc;
  359. uint8_t cur_alt_setting = 0xFF;
  360. uint8_t cur_iface = 0xFF;
  361. bool ret = false;
  362. USB_LOG_DBG("iface %u alt_setting %u\r\n", iface, alt_setting);
  363. while (p[DESC_bLength] != 0U) {
  364. switch (p[DESC_bDescriptorType]) {
  365. case USB_DESCRIPTOR_TYPE_INTERFACE:
  366. /* remember current alternate setting */
  367. cur_alt_setting = p[INTF_DESC_bAlternateSetting];
  368. cur_iface = p[INTF_DESC_bInterfaceNumber];
  369. if (cur_iface == iface &&
  370. cur_alt_setting == alt_setting) {
  371. if_desc = (void *)p;
  372. }
  373. USB_LOG_DBG("Current iface %u alt setting %u",
  374. cur_iface, cur_alt_setting);
  375. break;
  376. case USB_DESCRIPTOR_TYPE_ENDPOINT:
  377. if (cur_iface == iface) {
  378. ep_desc = (struct usb_endpoint_descriptor *)p;
  379. if (cur_alt_setting != alt_setting) {
  380. ret = usbd_reset_endpoint(ep_desc);
  381. } else {
  382. ret = usbd_set_endpoint(ep_desc);
  383. }
  384. }
  385. break;
  386. default:
  387. break;
  388. }
  389. /* skip to next descriptor */
  390. p += p[DESC_bLength];
  391. }
  392. usbd_event_notify_handler(USBD_EVENT_SET_INTERFACE, (void *)if_desc);
  393. return ret;
  394. }
  395. /**
  396. * @brief handle a standard device request
  397. *
  398. * @param [in] setup The setup packet
  399. * @param [in,out] data Data buffer
  400. * @param [in,out] len Pointer to data length
  401. *
  402. * @return true if the request was handled successfully
  403. */
  404. static bool usbd_std_device_req_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
  405. {
  406. uint16_t value = setup->wValue;
  407. // uint16_t index = setup->wIndex;
  408. bool ret = true;
  409. switch (setup->bRequest) {
  410. case USB_REQUEST_GET_STATUS:
  411. USB_LOG_DBG("REQ_GET_STATUS\r\n");
  412. /* bit 0: self-powered */
  413. /* bit 1: remote wakeup */
  414. *data = (uint8_t *)&usbd_core_cfg.remote_wakeup;
  415. *len = 2;
  416. break;
  417. case USB_REQUEST_CLEAR_FEATURE:
  418. USB_LOG_DBG("REQ_CLEAR_FEATURE\r\n");
  419. ret = false;
  420. if (value == USB_FEATURE_REMOTE_WAKEUP) {
  421. usbd_core_cfg.remote_wakeup = 0;
  422. usbd_event_notify_handler(USBD_EVENT_CLEAR_REMOTE_WAKEUP, NULL);
  423. ret = true;
  424. }
  425. break;
  426. case USB_REQUEST_SET_FEATURE:
  427. USB_LOG_DBG("REQ_SET_FEATURE\r\n");
  428. ret = false;
  429. if (value == USB_FEATURE_REMOTE_WAKEUP) {
  430. usbd_core_cfg.remote_wakeup = 1;
  431. usbd_event_notify_handler(USBD_EVENT_SET_REMOTE_WAKEUP, NULL);
  432. ret = true;
  433. }
  434. if (value == USB_FEATURE_TEST_MODE) {
  435. /* put TEST_MODE code here */
  436. }
  437. break;
  438. case USB_REQUEST_SET_ADDRESS:
  439. USB_LOG_DBG("REQ_SET_ADDRESS, addr 0x%x\r\n", value);
  440. usbd_set_address(value);
  441. break;
  442. case USB_REQUEST_GET_DESCRIPTOR:
  443. USB_LOG_DBG("REQ_GET_DESCRIPTOR\r\n");
  444. ret = usbd_get_descriptor(value, data, len);
  445. break;
  446. case USB_REQUEST_SET_DESCRIPTOR:
  447. USB_LOG_DBG("Device req 0x%02x not implemented\r\n", setup->bRequest);
  448. ret = false;
  449. break;
  450. case USB_REQUEST_GET_CONFIGURATION:
  451. USB_LOG_DBG("REQ_GET_CONFIGURATION\r\n");
  452. /* indicate if we are configured */
  453. *data = (uint8_t *)&usbd_core_cfg.configuration;
  454. *len = 1;
  455. break;
  456. case USB_REQUEST_SET_CONFIGURATION:
  457. value &= 0xFF;
  458. USB_LOG_DBG("REQ_SET_CONFIGURATION, conf 0x%x\r\n", value);
  459. if (!usbd_set_configuration(value, 0)) {
  460. USB_LOG_DBG("USB Set Configuration failed\r\n");
  461. ret = false;
  462. } else {
  463. /* configuration successful,
  464. * update current configuration
  465. */
  466. usbd_core_cfg.configuration = value;
  467. usbd_event_notify_handler(USBD_EVENT_CONFIGURED, NULL);
  468. }
  469. break;
  470. case USB_REQUEST_GET_INTERFACE:
  471. break;
  472. case USB_REQUEST_SET_INTERFACE:
  473. break;
  474. default:
  475. USB_LOG_ERR("Illegal device req 0x%02x\r\n", setup->bRequest);
  476. ret = false;
  477. break;
  478. }
  479. return ret;
  480. }
  481. /**
  482. * @brief handle a standard interface request
  483. *
  484. * @param [in] setup The setup packet
  485. * @param [in,out] data Data buffer
  486. * @param [in,out] len Pointer to data length
  487. *
  488. * @return true if the request was handled successfully
  489. */
  490. static bool usbd_std_interface_req_handler(struct usb_setup_packet *setup,
  491. uint8_t **data, uint32_t *len)
  492. {
  493. /** The device must be configured to accept standard interface
  494. * requests and the addressed Interface must be valid.
  495. */
  496. if (!is_device_configured() ||
  497. (!is_interface_valid((uint8_t)setup->wIndex))) {
  498. return false;
  499. }
  500. switch (setup->bRequest) {
  501. case USB_REQUEST_GET_STATUS:
  502. /* no bits specified */
  503. *data = (uint8_t *)&usbd_core_cfg.remote_wakeup;
  504. *len = 2;
  505. break;
  506. case USB_REQUEST_CLEAR_FEATURE:
  507. case USB_REQUEST_SET_FEATURE:
  508. /* not defined for interface */
  509. return false;
  510. case USB_REQUEST_GET_INTERFACE:
  511. /** This handler is called for classes that does not support
  512. * alternate Interfaces so always return 0. Classes that
  513. * support alternative interfaces handles GET_INTERFACE
  514. * in custom_handler.
  515. */
  516. *data = (uint8_t *)&usbd_core_cfg.reserved;
  517. *len = 1;
  518. break;
  519. case USB_REQUEST_SET_INTERFACE:
  520. USB_LOG_DBG("REQ_SET_INTERFACE\r\n");
  521. usbd_set_interface(setup->wIndex, setup->wValue);
  522. break;
  523. default:
  524. USB_LOG_ERR("Illegal interface req 0x%02x\r\n", setup->bRequest);
  525. return false;
  526. }
  527. return true;
  528. }
  529. /**
  530. * @brief handle a standard endpoint request
  531. *
  532. * @param [in] setup The setup packet
  533. * @param [in,out] data Data buffer
  534. * @param [in,out] len Pointer to data length
  535. *
  536. * @return true if the request was handled successfully
  537. */
  538. static bool usbd_std_endpoint_req_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
  539. {
  540. uint8_t ep = (uint8_t)setup->wIndex;
  541. /* Check if request addresses valid Endpoint */
  542. if (!is_ep_valid(ep)) {
  543. return false;
  544. }
  545. switch (setup->bRequest) {
  546. case USB_REQUEST_GET_STATUS:
  547. /** This request is valid for Control Endpoints when
  548. * the device is not yet configured. For other
  549. * Endpoints the device must be configured.
  550. * Firstly check if addressed ep is Control Endpoint.
  551. * If no then the device must be in Configured state
  552. * to accept the request.
  553. */
  554. if (((ep & 0x7f) == 0) || is_device_configured()) {
  555. /* bit 0 - Endpoint halted or not */
  556. usbd_ep_is_stalled(ep, (uint8_t *)&usbd_core_cfg.remote_wakeup);
  557. *data = (uint8_t *)&usbd_core_cfg.remote_wakeup;
  558. *len = 2;
  559. break;
  560. }
  561. return false;
  562. case USB_REQUEST_CLEAR_FEATURE:
  563. if (setup->wValue == USB_FEATURE_ENDPOINT_HALT) {
  564. /** This request is valid for Control Endpoints when
  565. * the device is not yet configured. For other
  566. * Endpoints the device must be configured.
  567. * Firstly check if addressed ep is Control Endpoint.
  568. * If no then the device must be in Configured state
  569. * to accept the request.
  570. */
  571. if (((ep & 0x7f) == 0) || is_device_configured()) {
  572. USB_LOG_ERR("ep:%x clear halt\r\n", ep);
  573. usbd_ep_clear_stall(ep);
  574. usbd_event_notify_handler(USBD_EVENT_CLEAR_HALT, NULL);
  575. break;
  576. }
  577. }
  578. /* only ENDPOINT_HALT defined for endpoints */
  579. return false;
  580. case USB_REQUEST_SET_FEATURE:
  581. if (setup->wValue == USB_FEATURE_ENDPOINT_HALT) {
  582. /** This request is valid for Control Endpoints when
  583. * the device is not yet configured. For other
  584. * Endpoints the device must be configured.
  585. * Firstly check if addressed ep is Control Endpoint.
  586. * If no then the device must be in Configured state
  587. * to accept the request.
  588. */
  589. if (((ep & 0x7f) == 0) || is_device_configured()) {
  590. /* set HALT by stalling */
  591. USB_LOG_ERR("ep:%x set halt\r\n", ep);
  592. usbd_ep_set_stall(ep);
  593. usbd_event_notify_handler(USBD_EVENT_SET_HALT, NULL);
  594. break;
  595. }
  596. }
  597. /* only ENDPOINT_HALT defined for endpoints */
  598. return false;
  599. case USB_REQUEST_SYNCH_FRAME:
  600. /* For Synch Frame request the device must be configured */
  601. if (is_device_configured()) {
  602. /* Not supported, return false anyway */
  603. USB_LOG_DBG("ep req 0x%02x not implemented\r\n", setup->bRequest);
  604. }
  605. return false;
  606. default:
  607. USB_LOG_ERR("Illegal ep req 0x%02x\r\n", setup->bRequest);
  608. return false;
  609. }
  610. return true;
  611. }
  612. /**
  613. * @brief default handler for standard ('chapter 9') requests
  614. *
  615. * If a custom request handler was installed, this handler is called first.
  616. *
  617. * @param [in] setup The setup packet
  618. * @param [in,out] data Data buffer
  619. * @param [in,out] len Pointer to data length
  620. *
  621. * @return true if the request was handled successfully
  622. */
  623. static int usbd_standard_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
  624. {
  625. int rc = 0;
  626. switch (setup->bmRequestType & USB_REQUEST_RECIPIENT_MASK) {
  627. case USB_REQUEST_RECIPIENT_DEVICE:
  628. if (usbd_std_device_req_handler(setup, data, len) == false) {
  629. rc = -1;
  630. }
  631. break;
  632. case USB_REQUEST_RECIPIENT_INTERFACE:
  633. if (usbd_std_interface_req_handler(setup, data, len) == false) {
  634. rc = -1;
  635. }
  636. break;
  637. case USB_REQUEST_RECIPIENT_ENDPOINT:
  638. if (usbd_std_endpoint_req_handler(setup, data, len) == false) {
  639. rc = -1;
  640. }
  641. break;
  642. default:
  643. rc = -1;
  644. }
  645. return rc;
  646. }
  647. /**
  648. * @brief handler for class requests
  649. *
  650. * If a custom request handler was installed, this handler is called first.
  651. *
  652. * @param [in] setup The setup packet
  653. * @param [in,out] data Data buffer
  654. * @param [in,out] len Pointer to data length
  655. *
  656. * @return true if the request was handled successfully
  657. */
  658. static int usbd_class_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
  659. {
  660. USB_LOG_DBG("bRequest 0x%02x, wIndex 0x%04x\r\n", setup->bRequest, setup->wIndex);
  661. if ((setup->bmRequestType & USB_REQUEST_RECIPIENT_MASK) != USB_REQUEST_RECIPIENT_INTERFACE) {
  662. return -1;
  663. }
  664. usb_slist_t *i, *j;
  665. usb_slist_for_each(i, &usbd_class_head)
  666. {
  667. usbd_class_t *class = usb_slist_entry(i, struct usbd_class, list);
  668. usb_slist_for_each(j, &class->intf_list)
  669. {
  670. usbd_interface_t *intf = usb_slist_entry(j, struct usbd_interface, list);
  671. if (intf->class_handler && (intf->intf_num == (setup->wIndex & 0xFF))) {
  672. return intf->class_handler(setup, data, len);
  673. }
  674. }
  675. }
  676. return -1;
  677. }
  678. /**
  679. * @brief handler for vendor requests
  680. *
  681. * If a custom request handler was installed, this handler is called first.
  682. *
  683. * @param [in] setup The setup packet
  684. * @param [in,out] data Data buffer
  685. * @param [in,out] len Pointer to data length
  686. *
  687. * @return true if the request was handled successfully
  688. */
  689. static int usbd_vendor_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
  690. {
  691. USB_LOG_DBG("bRequest 0x%02x, wValue0x%04x, wIndex 0x%04x\r\n", setup->bRequest, setup->wValue, setup->wIndex);
  692. // if((setup->bmRequestType & USB_REQUEST_RECIPIENT_MASK) != USB_REQUEST_RECIPIENT_DEVICE)
  693. // {
  694. // return -1;
  695. // }
  696. if (msosv1_desc) {
  697. if (setup->bRequest == msosv1_desc->vendor_code) {
  698. switch (setup->wIndex) {
  699. case 0x04:
  700. USB_LOG_INFO("get Compat ID\r\n");
  701. *data = (uint8_t *)msosv1_desc->compat_id;
  702. *len = msosv1_desc->compat_id_len;
  703. return 0;
  704. case 0x05:
  705. USB_LOG_INFO("get Compat id properties\r\n");
  706. *data = (uint8_t *)msosv1_desc->comp_id_property;
  707. *len = msosv1_desc->comp_id_property_len;
  708. return 0;
  709. default:
  710. USB_LOG_ERR("unknown vendor code\r\n");
  711. return -1;
  712. }
  713. }
  714. } else if (msosv2_desc) {
  715. if (setup->bRequest == msosv2_desc->vendor_code) {
  716. switch (setup->wIndex) {
  717. case WINUSB_REQUEST_GET_DESCRIPTOR_SET:
  718. USB_LOG_INFO("GET MS OS 2.0 Descriptor\r\n");
  719. *data = (uint8_t *)msosv2_desc->compat_id;
  720. *len = msosv2_desc->compat_id_len;
  721. return 0;
  722. default:
  723. USB_LOG_ERR("unknown vendor code\r\n");
  724. return -1;
  725. }
  726. }
  727. }
  728. usb_slist_t *i, *j;
  729. usb_slist_for_each(i, &usbd_class_head)
  730. {
  731. usbd_class_t *class = usb_slist_entry(i, struct usbd_class, list);
  732. usb_slist_for_each(j, &class->intf_list)
  733. {
  734. usbd_interface_t *intf = usb_slist_entry(j, struct usbd_interface, list);
  735. if (intf->vendor_handler && !intf->vendor_handler(setup, data, len)) {
  736. return 0;
  737. }
  738. }
  739. }
  740. return -1;
  741. }
  742. /**
  743. * @brief handler for special requests
  744. *
  745. * If a custom request handler was installed, this handler is called first.
  746. *
  747. * @param [in] setup The setup packet
  748. * @param [in,out] data Data buffer
  749. * @param [in,out] len Pointer to data length
  750. *
  751. * @return true if the request was handled successfully
  752. */
  753. static int usbd_custom_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
  754. {
  755. USB_LOG_DBG("bRequest 0x%02x, wIndex 0x%04x\r\n", setup->bRequest, setup->wIndex);
  756. if ((setup->bmRequestType & USB_REQUEST_RECIPIENT_MASK) != USB_REQUEST_RECIPIENT_INTERFACE) {
  757. return -1;
  758. }
  759. usb_slist_t *i, *j;
  760. usb_slist_for_each(i, &usbd_class_head)
  761. {
  762. usbd_class_t *class = usb_slist_entry(i, struct usbd_class, list);
  763. usb_slist_for_each(j, &class->intf_list)
  764. {
  765. usbd_interface_t *intf = usb_slist_entry(j, struct usbd_interface, list);
  766. if (intf->custom_handler && (intf->intf_num == (setup->wIndex & 0xFF))) {
  767. return intf->custom_handler(setup, data, len);
  768. }
  769. }
  770. }
  771. return -1;
  772. }
  773. /**
  774. * @brief handle a request by calling one of the installed request handlers
  775. *
  776. * Local function to handle a request by calling one of the installed request
  777. * handlers. In case of data going from host to device, the data is at *ppbData.
  778. * In case of data going from device to host, the handler can either choose to
  779. * write its data at *ppbData or update the data pointer.
  780. *
  781. * @param [in] setup The setup packet
  782. * @param [in,out] data Data buffer
  783. * @param [in,out] len Pointer to data length
  784. *
  785. * @return true if the request was handles successfully
  786. */
  787. static bool usbd_setup_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
  788. {
  789. uint8_t type = setup->bmRequestType & USB_REQUEST_TYPE_MASK;
  790. if (!usbd_custom_request_handler(setup, data, len)) {
  791. return true;
  792. }
  793. if (type == USB_REQUEST_STANDARD) {
  794. if (usbd_standard_request_handler(setup, data, len) < 0) {
  795. USB_LOG_ERR("Handler Error %d\r\n", type);
  796. usbd_print_setup(setup);
  797. return false;
  798. }
  799. } else if (type == USB_REQUEST_CLASS) {
  800. if (usbd_class_request_handler(setup, data, len) < 0) {
  801. USB_LOG_ERR("Handler Error %d\r\n", type);
  802. usbd_print_setup(setup);
  803. return false;
  804. }
  805. } else if (type == USB_REQUEST_VENDOR) {
  806. if (usbd_vendor_request_handler(setup, data, len) < 0) {
  807. USB_LOG_ERR("Handler Error %d\r\n", type);
  808. usbd_print_setup(setup);
  809. return false;
  810. }
  811. } else {
  812. return false;
  813. }
  814. return true;
  815. }
  816. /**
  817. * @brief send data or status to host
  818. *
  819. * @return N/A
  820. */
  821. static void usbd_send_to_host(uint16_t len)
  822. {
  823. uint32_t chunk = 0U;
  824. if (usbd_core_cfg.zlp_flag == false) {
  825. chunk = usbd_core_cfg.ep0_data_buf_residue;
  826. if (usbd_ep_write(USB_CONTROL_IN_EP0, usbd_core_cfg.ep0_data_buf, usbd_core_cfg.ep0_data_buf_residue, &chunk) < 0) {
  827. USB_LOG_ERR("USB write data failed\r\n");
  828. return;
  829. }
  830. usbd_core_cfg.ep0_data_buf += chunk;
  831. usbd_core_cfg.ep0_data_buf_residue -= chunk;
  832. /*
  833. * Set ZLP flag when host asks for a bigger length and the
  834. * last chunk is wMaxPacketSize long, to indicate the last
  835. * packet.
  836. */
  837. if ((!usbd_core_cfg.ep0_data_buf_residue) && (usbd_core_cfg.ep0_data_buf_len >= USB_CTRL_EP_MPS) && !(usbd_core_cfg.ep0_data_buf_len % USB_CTRL_EP_MPS)) {
  838. /* Transfers a zero-length packet next*/
  839. usbd_core_cfg.zlp_flag = true;
  840. }
  841. } else {
  842. usbd_core_cfg.zlp_flag = false;
  843. USB_LOG_DBG("send zlp\r\n");
  844. if (usbd_ep_write(USB_CONTROL_IN_EP0, NULL, 0, NULL) < 0) {
  845. USB_LOG_ERR("USB write zlp failed\r\n");
  846. return;
  847. }
  848. }
  849. }
  850. static void usbd_ep0_setup_handler(void)
  851. {
  852. struct usb_setup_packet *setup = &usbd_core_cfg.setup;
  853. /*
  854. * OUT transfer, Setup packet,
  855. * reset request message state machine
  856. */
  857. if (usbd_ep_read(USB_CONTROL_OUT_EP0, (uint8_t *)setup,
  858. sizeof(struct usb_setup_packet), NULL) < 0) {
  859. USB_LOG_ERR("Read Setup Packet failed\r\n");
  860. usbd_ep_set_stall(USB_CONTROL_IN_EP0);
  861. return;
  862. }
  863. //usbd_print_setup(setup);
  864. if (setup->wLength > USB_REQUEST_BUFFER_SIZE) {
  865. if ((setup->bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_OUT) {
  866. USB_LOG_ERR("Request buffer too small\r\n");
  867. usbd_ep_set_stall(USB_CONTROL_IN_EP0);
  868. return;
  869. }
  870. }
  871. // usbd_core_cfg.ep0_data_buf = usbd_core_cfg.req_data;
  872. usbd_core_cfg.ep0_data_buf_residue = setup->wLength;
  873. usbd_core_cfg.ep0_data_buf_len = setup->wLength;
  874. usbd_core_cfg.zlp_flag = false;
  875. /* this maybe set code in class request code */
  876. if (setup->wLength &&
  877. (setup->bmRequestType & USB_REQUEST_DIR_MASK) == USB_REQUEST_DIR_OUT) {
  878. USB_LOG_DBG("prepare to out data\r\n");
  879. return;
  880. }
  881. /* Ask installed handler to process request */
  882. if (!usbd_setup_request_handler(setup, &usbd_core_cfg.ep0_data_buf, &usbd_core_cfg.ep0_data_buf_len)) {
  883. USB_LOG_ERR("usbd_setup_request_handler failed\r\n");
  884. usbd_ep_set_stall(USB_CONTROL_IN_EP0);
  885. return;
  886. }
  887. /* Send smallest of requested and offered length */
  888. usbd_core_cfg.ep0_data_buf_residue = MIN(usbd_core_cfg.ep0_data_buf_len,
  889. setup->wLength);
  890. /*Send data or status to host*/
  891. usbd_send_to_host(setup->wLength);
  892. }
  893. static void usbd_ep0_out_handler(void)
  894. {
  895. uint32_t chunk = 0U;
  896. struct usb_setup_packet *setup = &usbd_core_cfg.setup;
  897. /* OUT transfer, status packets */
  898. if (usbd_core_cfg.ep0_data_buf_residue == 0) {
  899. /* absorb zero-length status message */
  900. USB_LOG_DBG("recv status\r\n");
  901. if (usbd_ep_read(USB_CONTROL_OUT_EP0,
  902. NULL,
  903. 0, NULL) < 0) {
  904. USB_LOG_ERR("Read DATA Packet failed\r\n");
  905. usbd_ep_set_stall(USB_CONTROL_IN_EP0);
  906. }
  907. return;
  908. }
  909. usbd_core_cfg.ep0_data_buf = usbd_core_cfg.req_data;
  910. /* OUT transfer, data packets */
  911. if (usbd_ep_read(USB_CONTROL_OUT_EP0,
  912. usbd_core_cfg.ep0_data_buf,
  913. usbd_core_cfg.ep0_data_buf_residue, &chunk) < 0) {
  914. USB_LOG_ERR("Read DATA Packet failed\r\n");
  915. usbd_ep_set_stall(USB_CONTROL_IN_EP0);
  916. return;
  917. }
  918. usbd_core_cfg.ep0_data_buf += chunk;
  919. usbd_core_cfg.ep0_data_buf_residue -= chunk;
  920. if (usbd_core_cfg.ep0_data_buf_residue == 0) {
  921. /* Received all, send data to handler */
  922. usbd_core_cfg.ep0_data_buf = usbd_core_cfg.req_data;
  923. if (!usbd_setup_request_handler(setup, &usbd_core_cfg.ep0_data_buf, &usbd_core_cfg.ep0_data_buf_len)) {
  924. USB_LOG_ERR("usbd_setup_request_handler1 failed\r\n");
  925. usbd_ep_set_stall(USB_CONTROL_IN_EP0);
  926. return;
  927. }
  928. /*Send status to host*/
  929. usbd_send_to_host(setup->wLength);
  930. } else {
  931. USB_LOG_ERR("ep0_data_buf_residue is not zero\r\n");
  932. }
  933. }
  934. static void usbd_ep0_in_handler(void)
  935. {
  936. struct usb_setup_packet *setup = &usbd_core_cfg.setup;
  937. /* Send more data if available */
  938. if (usbd_core_cfg.ep0_data_buf_residue != 0 || usbd_core_cfg.zlp_flag == true) {
  939. usbd_send_to_host(setup->wLength);
  940. } else {
  941. /*ep0 tx has completed,and no data to send,so do nothing*/
  942. }
  943. }
  944. static void usbd_ep_out_handler(uint8_t ep)
  945. {
  946. #if USBD_EP_CALLBACK_SEARCH_METHOD == USBD_EP_CALLBACK_LIST_SEARCH
  947. usb_slist_t *i, *j, *k;
  948. usb_slist_for_each(i, &usbd_class_head)
  949. {
  950. usbd_class_t *class = usb_slist_entry(i, struct usbd_class, list);
  951. usb_slist_for_each(j, &class->intf_list)
  952. {
  953. usbd_interface_t *intf = usb_slist_entry(j, struct usbd_interface, list);
  954. usb_slist_for_each(k, &intf->ep_list)
  955. {
  956. usbd_endpoint_t *ept = usb_slist_entry(k, struct usbd_endpoint, list);
  957. if ((ept->ep_addr == ep) && ept->ep_cb) {
  958. ept->ep_cb(ep);
  959. }
  960. }
  961. }
  962. }
  963. #else
  964. if (usbd_core_cfg.out_ep_cb[ep & 0x7f]) {
  965. usbd_core_cfg.out_ep_cb[ep & 0x7f](ep);
  966. }
  967. #endif
  968. }
  969. static void usbd_ep_in_handler(uint8_t ep)
  970. {
  971. #if USBD_EP_CALLBACK_SEARCH_METHOD == USBD_EP_CALLBACK_LIST_SEARCH
  972. usb_slist_t *i, *j, *k;
  973. usb_slist_for_each(i, &usbd_class_head)
  974. {
  975. usbd_class_t *class = usb_slist_entry(i, struct usbd_class, list);
  976. usb_slist_for_each(j, &class->intf_list)
  977. {
  978. usbd_interface_t *intf = usb_slist_entry(j, struct usbd_interface, list);
  979. usb_slist_for_each(k, &intf->ep_list)
  980. {
  981. usbd_endpoint_t *ept = usb_slist_entry(k, struct usbd_endpoint, list);
  982. if ((ept->ep_addr == ep) && ept->ep_cb) {
  983. ept->ep_cb(ep);
  984. }
  985. }
  986. }
  987. }
  988. #else
  989. if (usbd_core_cfg.in_ep_cb[ep & 0x7f]) {
  990. usbd_core_cfg.in_ep_cb[ep & 0x7f](ep);
  991. }
  992. #endif
  993. }
  994. static void usbd_class_event_notify_handler(uint8_t event, void *arg)
  995. {
  996. usb_slist_t *i, *j;
  997. usb_slist_for_each(i, &usbd_class_head)
  998. {
  999. usbd_class_t *class = usb_slist_entry(i, struct usbd_class, list);
  1000. usb_slist_for_each(j, &class->intf_list)
  1001. {
  1002. usbd_interface_t *intf = usb_slist_entry(j, struct usbd_interface, list);
  1003. if (intf->notify_handler) {
  1004. intf->notify_handler(event, arg);
  1005. }
  1006. }
  1007. }
  1008. }
  1009. void usbd_event_notify_handler(uint8_t event, void *arg)
  1010. {
  1011. switch (event) {
  1012. case USBD_EVENT_RESET:
  1013. usbd_set_address(0);
  1014. usbd_core_cfg.configured = 0;
  1015. usbd_core_cfg.configuration = 0;
  1016. struct usbd_endpoint_cfg ep0_cfg;
  1017. ep0_cfg.ep_mps = USB_CTRL_EP_MPS;
  1018. ep0_cfg.ep_type = USBD_EP_TYPE_CTRL;
  1019. ep0_cfg.ep_addr = USB_CONTROL_IN_EP0;
  1020. /*set USB_CONTROL_IN_EP0 nak*/
  1021. usbd_ep_open(&ep0_cfg);
  1022. ep0_cfg.ep_addr = USB_CONTROL_OUT_EP0;
  1023. /*set USB_CONTROL_OUT_EP0 ack to prepare receiving setup data*/
  1024. usbd_ep_open(&ep0_cfg);
  1025. #if USBD_EP_CALLBACK_SEARCH_METHOD == USBD_EP_CALLBACK_ARR_SEARCH
  1026. usbd_ep_callback_register();
  1027. #endif
  1028. case USBD_EVENT_ERROR:
  1029. case USBD_EVENT_SOF:
  1030. case USBD_EVENT_CONNECTED:
  1031. case USBD_EVENT_CONFIGURED:
  1032. case USBD_EVENT_SUSPEND:
  1033. case USBD_EVENT_DISCONNECTED:
  1034. case USBD_EVENT_RESUME:
  1035. case USBD_EVENT_SET_INTERFACE:
  1036. case USBD_EVENT_SET_REMOTE_WAKEUP:
  1037. case USBD_EVENT_CLEAR_REMOTE_WAKEUP:
  1038. case USBD_EVENT_SET_HALT:
  1039. case USBD_EVENT_CLEAR_HALT:
  1040. usbd_class_event_notify_handler(event, arg);
  1041. break;
  1042. case USBD_EVENT_SETUP_NOTIFY:
  1043. usbd_ep0_setup_handler();
  1044. break;
  1045. case USBD_EVENT_EP0_IN_NOTIFY:
  1046. usbd_ep0_in_handler();
  1047. break;
  1048. case USBD_EVENT_EP0_OUT_NOTIFY:
  1049. usbd_ep0_out_handler();
  1050. break;
  1051. case USBD_EVENT_EP_IN_NOTIFY:
  1052. usbd_ep_in_handler((uint32_t)arg);
  1053. break;
  1054. case USBD_EVENT_EP_OUT_NOTIFY:
  1055. usbd_ep_out_handler((uint32_t)arg);
  1056. break;
  1057. default:
  1058. USB_LOG_ERR("USB unknown event: %d\r\n", event);
  1059. break;
  1060. }
  1061. }
  1062. void usbd_desc_register(const uint8_t *desc)
  1063. {
  1064. usbd_core_cfg.descriptors = desc;
  1065. }
  1066. /* Register MS OS Descriptors version 1 */
  1067. void usbd_msosv1_desc_register(struct usb_msosv1_descriptor *desc)
  1068. {
  1069. msosv1_desc = desc;
  1070. }
  1071. /* Register MS OS Descriptors version 2 */
  1072. void usbd_msosv2_desc_register(struct usb_msosv2_descriptor *desc)
  1073. {
  1074. msosv2_desc = desc;
  1075. }
  1076. void usbd_bos_desc_register(struct usb_bos_descriptor *desc)
  1077. {
  1078. bos_desc = desc;
  1079. }
  1080. void usbd_class_register(usbd_class_t *class)
  1081. {
  1082. usb_slist_add_tail(&usbd_class_head, &class->list);
  1083. usb_slist_init(&class->intf_list);
  1084. }
  1085. void usbd_class_add_interface(usbd_class_t *class, usbd_interface_t *intf)
  1086. {
  1087. static uint8_t intf_offset = 0;
  1088. intf->intf_num = intf_offset;
  1089. usb_slist_add_tail(&class->intf_list, &intf->list);
  1090. usb_slist_init(&intf->ep_list);
  1091. intf_offset++;
  1092. }
  1093. void usbd_interface_add_endpoint(usbd_interface_t *intf, usbd_endpoint_t *ep)
  1094. {
  1095. usb_slist_add_tail(&intf->ep_list, &ep->list);
  1096. }
  1097. bool usb_device_is_configured(void)
  1098. {
  1099. return usbd_core_cfg.configured;
  1100. }