usbd_core.c 40 KB

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