usbd_core.c 38 KB

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