usbh_core.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. /**
  2. * @file usbh_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 "usbh_core.h"
  24. struct usbh_class_info *usbh_class_info_table_begin = NULL;
  25. struct usbh_class_info *usbh_class_info_table_end = NULL;
  26. static const char *speed_table[] = { "error speed", "low speed", "full speed", "high speed" };
  27. static const struct usbh_class_driver *usbh_find_class_driver(uint8_t class, uint8_t subcalss, uint8_t protocol, uint16_t vid, uint16_t pid);
  28. /* general descriptor field offsets */
  29. #define DESC_bLength 0 /** Length offset */
  30. #define DESC_bDescriptorType 1 /** Descriptor type offset */
  31. #define USB_DEV_ADDR_MAX 0x7f
  32. #define USB_DEV_ADDR_MARK_OFFSET 5
  33. #define USB_DEV_ADDR_MARK_MASK 0x1f
  34. struct usbh_devaddr_priv {
  35. /**
  36. * alloctab[0]:addr from 0~31
  37. * alloctab[1]:addr from 32~63
  38. * alloctab[2]:addr from 64~95
  39. * alloctab[3]:addr from 96~127
  40. *
  41. */
  42. uint8_t next; /* Next device address */
  43. uint32_t alloctab[4]; /* Bit allocation table */
  44. };
  45. struct usbh_roothubport_priv {
  46. struct usbh_hubport hport; /* Common hub port definitions */
  47. struct usbh_devaddr_priv devgen; /* Address generation data */
  48. };
  49. struct usbh_core_priv {
  50. struct usbh_roothubport_priv rhport[CONFIG_USBHOST_RHPORTS];
  51. volatile struct usbh_hubport *active_hport; /* Used to pass external hub port events */
  52. usb_osal_event_t pscevent; /* Semaphore to wait for a port event */
  53. } usbh_core_cfg;
  54. static inline struct usbh_roothubport_priv *usbh_find_roothub_port(struct usbh_hubport *hport)
  55. {
  56. while (hport->parent != NULL) {
  57. hport = hport->parent->parent;
  58. }
  59. return (struct usbh_roothubport_priv *)hport;
  60. }
  61. static int usbh_allocate_devaddr(struct usbh_devaddr_priv *devgen)
  62. {
  63. uint8_t startaddr = devgen->next;
  64. uint8_t devaddr;
  65. int index;
  66. int bitno;
  67. /* Loop until we find a valid device address */
  68. for (;;) {
  69. /* Try the next device address */
  70. devaddr = devgen->next;
  71. if (devgen->next >= 0x7f) {
  72. devgen->next = 1;
  73. } else {
  74. devgen->next++;
  75. }
  76. /* Is this address already allocated? */
  77. index = devaddr >> 5;
  78. bitno = devaddr & 0x1f;
  79. if ((devgen->alloctab[index] & (1 << bitno)) == 0) {
  80. /* No... allocate it now */
  81. devgen->alloctab[index] |= (1 << bitno);
  82. return (int)devaddr;
  83. }
  84. /* This address has already been allocated. The following logic will
  85. * prevent (unexpected) infinite loops.
  86. */
  87. if (startaddr == devaddr) {
  88. /* We are back where we started... the are no free device address */
  89. return -ENOMEM;
  90. }
  91. }
  92. }
  93. static int usbh_free_devaddr(struct usbh_devaddr_priv *devgen, uint8_t devaddr)
  94. {
  95. int index;
  96. int bitno;
  97. if ((devaddr > 0) && (devaddr < USB_DEV_ADDR_MAX)) {
  98. index = devaddr >> USB_DEV_ADDR_MARK_OFFSET;
  99. bitno = devaddr & USB_DEV_ADDR_MARK_MASK;
  100. /* Free the address by clearing the associated bit in the alloctab[]; */
  101. if ((devgen->alloctab[index] |= (1 << bitno)) != 0) {
  102. devgen->alloctab[index] &= ~(1 << bitno);
  103. } else {
  104. return -1;
  105. }
  106. /* Reset the next pointer if the one just released has a lower value */
  107. if (devaddr < devgen->next) {
  108. devgen->next = devaddr;
  109. }
  110. }
  111. return 0;
  112. }
  113. static int usbh_devaddr_create(struct usbh_hubport *hport)
  114. {
  115. struct usbh_roothubport_priv *rhport;
  116. rhport = usbh_find_roothub_port(hport);
  117. return usbh_allocate_devaddr(&rhport->devgen);
  118. }
  119. static int usbh_devaddr_destroy(struct usbh_hubport *hport, uint8_t dev_addr)
  120. {
  121. struct usbh_roothubport_priv *rhport;
  122. rhport = usbh_find_roothub_port(hport);
  123. return usbh_free_devaddr(&rhport->devgen, dev_addr);
  124. }
  125. void usbh_hport_activate(struct usbh_hubport *hport)
  126. {
  127. struct usbh_endpoint_cfg ep0_cfg;
  128. memset(&ep0_cfg, 0, sizeof(struct usbh_endpoint_cfg));
  129. ep0_cfg.ep_addr = 0x00;
  130. ep0_cfg.ep_interval = 0x00;
  131. ep0_cfg.ep_mps = 0x08;
  132. ep0_cfg.ep_type = USB_ENDPOINT_TYPE_CONTROL;
  133. ep0_cfg.hport = hport;
  134. /* Allocate memory for roothub port control endpoint */
  135. usbh_ep_alloc(&hport->ep0, &ep0_cfg);
  136. }
  137. void usbh_hport_deactivate(struct usbh_hubport *hport)
  138. {
  139. size_t flags;
  140. /* Don't free the control pipe of root hub ports! */
  141. if (hport->parent != NULL && hport->ep0 != NULL) {
  142. usb_ep_cancel(hport->ep0);
  143. usbh_ep_free(hport->ep0);
  144. hport->ep0 = NULL;
  145. }
  146. flags = usb_osal_enter_critical_section();
  147. /* Free the device address if one has been assigned */
  148. usbh_devaddr_destroy(hport, hport->dev_addr);
  149. hport->dev_addr = 0;
  150. usb_osal_leave_critical_section(flags);
  151. if (hport->setup)
  152. usb_iofree(hport->setup);
  153. hport->setup = NULL;
  154. }
  155. static int parse_device_descriptor(struct usbh_hubport *hport, struct usb_device_descriptor *desc, uint16_t length)
  156. {
  157. if (desc->bLength != USB_SIZEOF_DEVICE_DESC) {
  158. USB_LOG_ERR("invalid device bLength 0x%02x\r\n", desc->bLength);
  159. return -EINVAL;
  160. } else if (desc->bDescriptorType != USB_DESCRIPTOR_TYPE_DEVICE) {
  161. USB_LOG_ERR("unexpected device descriptor 0x%02x\r\n", desc->bDescriptorType);
  162. return -EINVAL;
  163. } else {
  164. if (length <= 8) {
  165. return 0;
  166. }
  167. #if 0
  168. USB_LOG_DBG("Device Descriptor:\r\n");
  169. USB_LOG_DBG("bLength: 0x%02x \r\n", desc->bLength);
  170. USB_LOG_DBG("bDescriptorType: 0x%02x \r\n", desc->bDescriptorType);
  171. USB_LOG_DBG("bcdUSB: 0x%04x \r\n", desc->bcdUSB);
  172. USB_LOG_DBG("bDeviceClass: 0x%02x \r\n", desc->bDeviceClass);
  173. USB_LOG_DBG("bDeviceSubClass: 0x%02x \r\n", desc->bDeviceSubClass);
  174. USB_LOG_DBG("bDeviceProtocol: 0x%02x \r\n", desc->bDeviceProtocol);
  175. USB_LOG_DBG("bMaxPacketSize0: 0x%02x \r\n", desc->bMaxPacketSize0);
  176. USB_LOG_DBG("idVendor: 0x%04x \r\n", desc->idVendor);
  177. USB_LOG_DBG("idProduct: 0x%04x \r\n", desc->idProduct);
  178. USB_LOG_DBG("bcdDevice: 0x%04x \r\n", desc->bcdDevice);
  179. USB_LOG_DBG("iManufacturer: 0x%02x \r\n", desc->iManufacturer);
  180. USB_LOG_DBG("iProduct: 0x%02x \r\n", desc->iProduct);
  181. USB_LOG_DBG("iSerialNumber: 0x%02x \r\n", desc->iSerialNumber);
  182. USB_LOG_DBG("bNumConfigurations: 0x%02x\r\n", desc->bNumConfigurations);
  183. #endif
  184. hport->device_desc.bLength = desc->bLength;
  185. hport->device_desc.bDescriptorType = desc->bDescriptorType;
  186. hport->device_desc.bcdUSB = desc->bcdUSB;
  187. hport->device_desc.bDeviceClass = desc->bDeviceClass;
  188. hport->device_desc.bDeviceSubClass = desc->bDeviceSubClass;
  189. hport->device_desc.bDeviceProtocol = desc->bDeviceProtocol;
  190. hport->device_desc.bMaxPacketSize0 = desc->bMaxPacketSize0;
  191. hport->device_desc.idVendor = desc->idVendor;
  192. hport->device_desc.idProduct = desc->idProduct;
  193. hport->device_desc.bcdDevice = desc->bcdDevice;
  194. hport->device_desc.iManufacturer = desc->iManufacturer;
  195. hport->device_desc.iProduct = desc->iProduct;
  196. hport->device_desc.iSerialNumber = desc->iSerialNumber;
  197. hport->device_desc.bNumConfigurations = desc->bNumConfigurations;
  198. }
  199. return 0;
  200. }
  201. static int parse_config_descriptor(struct usbh_hubport *hport, struct usb_configuration_descriptor *desc, uint16_t length)
  202. {
  203. uint32_t total_len = 0;
  204. uint8_t ep_num = 0;
  205. uint8_t intf_num = 0;
  206. uint8_t *p = (uint8_t *)desc;
  207. if (desc->bLength != USB_SIZEOF_CONFIG_DESC) {
  208. USB_LOG_ERR("invalid config bLength 0x%02x\r\n", desc->bLength);
  209. return -EINVAL;
  210. } else if (desc->bDescriptorType != USB_DESCRIPTOR_TYPE_CONFIGURATION) {
  211. USB_LOG_ERR("unexpected config descriptor 0x%02x\r\n", desc->bDescriptorType);
  212. return -EINVAL;
  213. } else {
  214. if (length <= USB_SIZEOF_CONFIG_DESC) {
  215. return 0;
  216. }
  217. #if 0
  218. USB_LOG_DBG("Config Descriptor:\r\n");
  219. USB_LOG_DBG("bLength: 0x%02x \r\n", desc->bLength);
  220. USB_LOG_DBG("bDescriptorType: 0x%02x \r\n", desc->bDescriptorType);
  221. USB_LOG_DBG("wTotalLength: 0x%04x \r\n", desc->wTotalLength);
  222. USB_LOG_DBG("bNumInterfaces: 0x%02x \r\n", desc->bNumInterfaces);
  223. USB_LOG_DBG("bConfigurationValue: 0x%02x \r\n", desc->bConfigurationValue);
  224. USB_LOG_DBG("iConfiguration: 0x%02x \r\n", desc->iConfiguration);
  225. USB_LOG_DBG("bmAttributes: 0x%02x \r\n", desc->bmAttributes);
  226. USB_LOG_DBG("bMaxPower: 0x%02x \r\n", desc->bMaxPower);
  227. #endif
  228. hport->config.config_desc.bLength = desc->bLength;
  229. hport->config.config_desc.bDescriptorType = desc->bDescriptorType;
  230. hport->config.config_desc.wTotalLength = desc->wTotalLength;
  231. hport->config.config_desc.bNumInterfaces = desc->bNumInterfaces;
  232. hport->config.config_desc.bConfigurationValue = desc->bConfigurationValue;
  233. hport->config.config_desc.iConfiguration = desc->iConfiguration;
  234. hport->config.config_desc.iConfiguration = desc->iConfiguration;
  235. hport->config.config_desc.bmAttributes = desc->bmAttributes;
  236. hport->config.config_desc.bMaxPower = desc->bMaxPower;
  237. if (length > USB_SIZEOF_CONFIG_DESC) {
  238. while (p[DESC_bLength] && (total_len < desc->wTotalLength) && (intf_num < desc->bNumInterfaces)) {
  239. p += p[DESC_bLength];
  240. total_len += p[DESC_bLength];
  241. if (p[DESC_bDescriptorType] == USB_DESCRIPTOR_TYPE_INTERFACE) {
  242. struct usb_interface_descriptor *intf_desc = (struct usb_interface_descriptor *)p;
  243. #if 0
  244. USB_LOG_DBG("Interface Descriptor:\r\n");
  245. USB_LOG_DBG("bLength: 0x%02x \r\n", intf_desc->bLength);
  246. USB_LOG_DBG("bDescriptorType: 0x%02x \r\n", intf_desc->bDescriptorType);
  247. USB_LOG_DBG("bInterfaceNumber: 0x%02x \r\n", intf_desc->bInterfaceNumber);
  248. USB_LOG_DBG("bAlternateSetting: 0x%02x \r\n", intf_desc->bAlternateSetting);
  249. USB_LOG_DBG("bNumEndpoints: 0x%02x \r\n", intf_desc->bNumEndpoints);
  250. USB_LOG_DBG("bInterfaceClass: 0x%02x \r\n", intf_desc->bInterfaceClass);
  251. USB_LOG_DBG("bInterfaceSubClass: 0x%02x \r\n", intf_desc->bInterfaceSubClass);
  252. USB_LOG_DBG("bInterfaceProtocol: 0x%02x \r\n", intf_desc->bInterfaceProtocol);
  253. USB_LOG_DBG("iInterface: 0x%02x \r\n", intf_desc->iInterface);
  254. #endif
  255. memset(&hport->config.intf[intf_num], 0, sizeof(struct usbh_interface));
  256. hport->config.intf[intf_num].intf_desc.bLength = intf_desc->bLength;
  257. hport->config.intf[intf_num].intf_desc.bDescriptorType = intf_desc->bDescriptorType;
  258. hport->config.intf[intf_num].intf_desc.bInterfaceNumber = intf_desc->bInterfaceNumber;
  259. hport->config.intf[intf_num].intf_desc.bAlternateSetting = intf_desc->bAlternateSetting;
  260. hport->config.intf[intf_num].intf_desc.bNumEndpoints = intf_desc->bNumEndpoints;
  261. hport->config.intf[intf_num].intf_desc.bInterfaceClass = intf_desc->bInterfaceClass;
  262. hport->config.intf[intf_num].intf_desc.bInterfaceSubClass = intf_desc->bInterfaceSubClass;
  263. hport->config.intf[intf_num].intf_desc.bInterfaceProtocol = intf_desc->bInterfaceProtocol;
  264. hport->config.intf[intf_num].intf_desc.iInterface = intf_desc->iInterface;
  265. ep_num = 0;
  266. while (p[DESC_bLength] && (total_len < desc->wTotalLength) && (ep_num < intf_desc->bNumEndpoints)) {
  267. p += p[DESC_bLength];
  268. total_len += p[DESC_bLength];
  269. if (p[DESC_bDescriptorType] == USB_DESCRIPTOR_TYPE_ENDPOINT) {
  270. struct usb_endpoint_descriptor *ep_desc = (struct usb_endpoint_descriptor *)p;
  271. #if 0
  272. USB_LOG_DBG("Endpoint Descriptor:\r\n");
  273. USB_LOG_DBG("bLength: 0x%02x \r\n", ep_desc->bLength);
  274. USB_LOG_DBG("bDescriptorType: 0x%02x \r\n", ep_desc->bDescriptorType);
  275. USB_LOG_DBG("bEndpointAddress: 0x%02x \r\n", ep_desc->bEndpointAddress);
  276. USB_LOG_DBG("bmAttributes: 0x%02x \r\n", ep_desc->bmAttributes);
  277. USB_LOG_DBG("wMaxPacketSize: 0x%04x \r\n", ep_desc->wMaxPacketSize);
  278. USB_LOG_DBG("bInterval: 0x%02x \r\n", ep_desc->bInterval);
  279. #endif
  280. memset(&hport->config.intf[intf_num].ep[ep_num], 0, sizeof(struct usbh_endpoint));
  281. hport->config.intf[intf_num].ep[ep_num].ep_desc.bLength = ep_desc->bLength;
  282. hport->config.intf[intf_num].ep[ep_num].ep_desc.bDescriptorType = ep_desc->bDescriptorType;
  283. hport->config.intf[intf_num].ep[ep_num].ep_desc.bEndpointAddress = ep_desc->bEndpointAddress;
  284. hport->config.intf[intf_num].ep[ep_num].ep_desc.bmAttributes = ep_desc->bmAttributes;
  285. hport->config.intf[intf_num].ep[ep_num].ep_desc.wMaxPacketSize = ep_desc->wMaxPacketSize;
  286. hport->config.intf[intf_num].ep[ep_num].ep_desc.bInterval = ep_desc->bInterval;
  287. ep_num++;
  288. }
  289. }
  290. intf_num++;
  291. }
  292. }
  293. }
  294. }
  295. return 0;
  296. }
  297. #ifdef CONFIG_USBHOST_GET_STRING_DESC
  298. static int parse_string_descriptor(struct usbh_hubport *hport, struct usb_string_descriptor *desc, uint8_t str_idx)
  299. {
  300. uint8_t string[64 + 1] = { 0 };
  301. uint8_t *p = (uint8_t *)desc;
  302. if (desc->bDescriptorType != USB_DESCRIPTOR_TYPE_STRING) {
  303. USB_LOG_ERR("unexpected string descriptor 0x%02x\r\n", desc->bDescriptorType);
  304. return -2;
  305. } else {
  306. p += 2;
  307. for (uint32_t i = 0; i < (desc->bLength - 2) / 2; i++) {
  308. string[i] = *p;
  309. p += 2;
  310. }
  311. if (str_idx == USB_STRING_MFC_INDEX) {
  312. USB_LOG_INFO("Manufacturer :%s\r\n", string);
  313. } else if (str_idx == USB_STRING_PRODUCT_INDEX) {
  314. USB_LOG_INFO("Product :%s\r\n", string);
  315. } else if (str_idx == USB_STRING_SERIAL_INDEX) {
  316. USB_LOG_INFO("SerialNumber :%s\r\n", string);
  317. } else {
  318. }
  319. }
  320. return 0;
  321. }
  322. #endif
  323. static void usbh_print_hubport_info(struct usbh_hubport *hport)
  324. {
  325. USB_LOG_RAW("Device Descriptor:\r\n");
  326. USB_LOG_RAW("bLength: 0x%02x \r\n", hport->device_desc.bLength);
  327. USB_LOG_RAW("bDescriptorType: 0x%02x \r\n", hport->device_desc.bDescriptorType);
  328. USB_LOG_RAW("bcdUSB: 0x%04x \r\n", hport->device_desc.bcdUSB);
  329. USB_LOG_RAW("bDeviceClass: 0x%02x \r\n", hport->device_desc.bDeviceClass);
  330. USB_LOG_RAW("bDeviceSubClass: 0x%02x \r\n", hport->device_desc.bDeviceSubClass);
  331. USB_LOG_RAW("bDeviceProtocol: 0x%02x \r\n", hport->device_desc.bDeviceProtocol);
  332. USB_LOG_RAW("bMaxPacketSize0: 0x%02x \r\n", hport->device_desc.bMaxPacketSize0);
  333. USB_LOG_RAW("idVendor: 0x%04x \r\n", hport->device_desc.idVendor);
  334. USB_LOG_RAW("idProduct: 0x%04x \r\n", hport->device_desc.idProduct);
  335. USB_LOG_RAW("bcdDevice: 0x%04x \r\n", hport->device_desc.bcdDevice);
  336. USB_LOG_RAW("iManufacturer: 0x%02x \r\n", hport->device_desc.iManufacturer);
  337. USB_LOG_RAW("iProduct: 0x%02x \r\n", hport->device_desc.iProduct);
  338. USB_LOG_RAW("iSerialNumber: 0x%02x \r\n", hport->device_desc.iSerialNumber);
  339. USB_LOG_RAW("bNumConfigurations: 0x%02x\r\n", hport->device_desc.bNumConfigurations);
  340. USB_LOG_RAW("Config Descriptor:\r\n");
  341. USB_LOG_RAW("bLength: 0x%02x \r\n", hport->config.config_desc.bLength);
  342. USB_LOG_RAW("bDescriptorType: 0x%02x \r\n", hport->config.config_desc.bDescriptorType);
  343. USB_LOG_RAW("wTotalLength: 0x%04x \r\n", hport->config.config_desc.wTotalLength);
  344. USB_LOG_RAW("bNumInterfaces: 0x%02x \r\n", hport->config.config_desc.bNumInterfaces);
  345. USB_LOG_RAW("bConfigurationValue: 0x%02x \r\n", hport->config.config_desc.bConfigurationValue);
  346. USB_LOG_RAW("iConfiguration: 0x%02x \r\n", hport->config.config_desc.iConfiguration);
  347. USB_LOG_RAW("bmAttributes: 0x%02x \r\n", hport->config.config_desc.bmAttributes);
  348. USB_LOG_RAW("bMaxPower: 0x%02x \r\n", hport->config.config_desc.bMaxPower);
  349. for (uint8_t i = 0; i < hport->config.config_desc.bNumInterfaces; i++) {
  350. USB_LOG_RAW("Interface Descriptor:\r\n");
  351. USB_LOG_RAW("bLength: 0x%02x \r\n", hport->config.intf[i].intf_desc.bLength);
  352. USB_LOG_RAW("bDescriptorType: 0x%02x \r\n", hport->config.intf[i].intf_desc.bDescriptorType);
  353. USB_LOG_RAW("bInterfaceNumber: 0x%02x \r\n", hport->config.intf[i].intf_desc.bInterfaceNumber);
  354. USB_LOG_RAW("bAlternateSetting: 0x%02x \r\n", hport->config.intf[i].intf_desc.bAlternateSetting);
  355. USB_LOG_RAW("bNumEndpoints: 0x%02x \r\n", hport->config.intf[i].intf_desc.bNumEndpoints);
  356. USB_LOG_RAW("bInterfaceClass: 0x%02x \r\n", hport->config.intf[i].intf_desc.bInterfaceClass);
  357. USB_LOG_RAW("bInterfaceSubClass: 0x%02x \r\n", hport->config.intf[i].intf_desc.bInterfaceSubClass);
  358. USB_LOG_RAW("bInterfaceProtocol: 0x%02x \r\n", hport->config.intf[i].intf_desc.bInterfaceProtocol);
  359. USB_LOG_RAW("iInterface: 0x%02x \r\n", hport->config.intf[i].intf_desc.iInterface);
  360. for (uint8_t j = 0; j < hport->config.intf[i].intf_desc.bNumEndpoints; j++) {
  361. USB_LOG_RAW("Endpoint Descriptor:\r\n");
  362. USB_LOG_RAW("bLength: 0x%02x \r\n", hport->config.intf[i].ep[j].ep_desc.bLength);
  363. USB_LOG_RAW("bDescriptorType: 0x%02x \r\n", hport->config.intf[i].ep[j].ep_desc.bDescriptorType);
  364. USB_LOG_RAW("bEndpointAddress: 0x%02x \r\n", hport->config.intf[i].ep[j].ep_desc.bEndpointAddress);
  365. USB_LOG_RAW("bmAttributes: 0x%02x \r\n", hport->config.intf[i].ep[j].ep_desc.bmAttributes);
  366. USB_LOG_RAW("wMaxPacketSize: 0x%04x \r\n", hport->config.intf[i].ep[j].ep_desc.wMaxPacketSize);
  367. USB_LOG_RAW("bInterval: 0x%02x \r\n", hport->config.intf[i].ep[j].ep_desc.bInterval);
  368. }
  369. }
  370. }
  371. static int usbh_enumerate(struct usbh_hubport *hport)
  372. {
  373. struct usb_interface_descriptor *intf_desc;
  374. struct usb_setup_packet *setup;
  375. uint8_t *ep0_buffer;
  376. uint8_t descsize;
  377. int dev_addr;
  378. uint8_t ep_mps;
  379. int ret;
  380. #define USB_REQUEST_BUFFER_SIZE 256
  381. /* Allocate buffer for setup and data buffer */
  382. if (hport->setup == NULL) {
  383. hport->setup = usb_iomalloc(sizeof(struct usb_setup_packet));
  384. if (hport->setup == NULL) {
  385. USB_LOG_ERR("Fail to alloc setup\r\n");
  386. return -ENOMEM;
  387. }
  388. }
  389. setup = hport->setup;
  390. ep0_buffer = usb_iomalloc(USB_REQUEST_BUFFER_SIZE);
  391. if (ep0_buffer == NULL) {
  392. USB_LOG_ERR("Fail to alloc ep0_buffer\r\n");
  393. return -ENOMEM;
  394. }
  395. /* Pick an appropriate packet size for this device
  396. *
  397. * USB 2.0, Paragraph 5.5.3 "Control Transfer Packet Size Constraints"
  398. *
  399. * "An endpoint for control transfers specifies the maximum data
  400. * payload size that the endpoint can accept from or transmit to
  401. * the bus. The allowable maximum control transfer data payload
  402. * sizes for full-speed devices is 8, 16, 32, or 64 bytes; for
  403. * high-speed devices, it is 64 bytes and for low-speed devices,
  404. * it is 8 bytes. This maximum applies to the data payloads of the
  405. * Data packets following a Setup..."
  406. */
  407. if (hport->speed == USB_SPEED_HIGH) {
  408. /* For high-speed, we must use 64 bytes */
  409. ep_mps = 64;
  410. descsize = USB_SIZEOF_DEVICE_DESC;
  411. } else {
  412. /* Eight will work for both low- and full-speed */
  413. ep_mps = 8;
  414. descsize = 8;
  415. }
  416. /* Configure EP0 with the initial maximum packet size */
  417. usbh_ep0_reconfigure(hport->ep0, 0, ep_mps, hport->speed);
  418. /* Read the first 8 bytes of the device descriptor */
  419. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  420. setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
  421. setup->wValue = (uint16_t)((USB_DESCRIPTOR_TYPE_DEVICE << 8) | 0);
  422. setup->wIndex = 0;
  423. setup->wLength = descsize;
  424. ret = usbh_control_transfer(hport->ep0, setup, ep0_buffer);
  425. if (ret < 0) {
  426. USB_LOG_ERR("Failed to get device descriptor,errorcode:%d\r\n", ret);
  427. goto errout;
  428. }
  429. parse_device_descriptor(hport, (struct usb_device_descriptor *)ep0_buffer, descsize);
  430. if ((hport->parent == NULL) && (hport->speed != USB_SPEED_HIGH)) {
  431. usbh_reset_port(hport->port);
  432. }
  433. /* Extract the correct max packetsize from the device descriptor */
  434. ep_mps = ((struct usb_device_descriptor *)ep0_buffer)->bMaxPacketSize0;
  435. /* And reconfigure EP0 with the correct maximum packet size */
  436. usbh_ep0_reconfigure(hport->ep0, 0, ep_mps, hport->speed);
  437. /* Assign a function address to the device connected to this port */
  438. dev_addr = usbh_devaddr_create(hport);
  439. if (dev_addr < 0) {
  440. USB_LOG_ERR("Failed to allocate devaddr,errorcode:%d\r\n", ret);
  441. goto errout;
  442. }
  443. /* Set the USB device address */
  444. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  445. setup->bRequest = USB_REQUEST_SET_ADDRESS;
  446. setup->wValue = dev_addr;
  447. setup->wIndex = 0;
  448. setup->wLength = 0;
  449. ret = usbh_control_transfer(hport->ep0, setup, NULL);
  450. if (ret < 0) {
  451. USB_LOG_ERR("Failed to set devaddr,errorcode:%d\r\n", ret);
  452. goto errout;
  453. }
  454. /* wait device address set completely */
  455. usb_osal_msleep(2);
  456. /* Assign the function address to the port */
  457. hport->dev_addr = dev_addr;
  458. /* And reconfigure EP0 with the correct address */
  459. usbh_ep0_reconfigure(hport->ep0, dev_addr, ep_mps, hport->speed);
  460. /* Read the full device descriptor */
  461. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  462. setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
  463. setup->wValue = (uint16_t)((USB_DESCRIPTOR_TYPE_DEVICE << 8) | 0);
  464. setup->wIndex = 0;
  465. setup->wLength = USB_SIZEOF_DEVICE_DESC;
  466. ret = usbh_control_transfer(hport->ep0, setup, ep0_buffer);
  467. if (ret < 0) {
  468. USB_LOG_ERR("Failed to get full device descriptor,errorcode:%d\r\n", ret);
  469. goto errout;
  470. }
  471. parse_device_descriptor(hport, (struct usb_device_descriptor *)ep0_buffer, USB_SIZEOF_DEVICE_DESC);
  472. USB_LOG_INFO("New device found,idVendor:%04x,idProduct:%04x,bcdDevice:%04x\r\n",
  473. ((struct usb_device_descriptor *)ep0_buffer)->idVendor,
  474. ((struct usb_device_descriptor *)ep0_buffer)->idProduct,
  475. ((struct usb_device_descriptor *)ep0_buffer)->bcdDevice);
  476. /* Read the first 9 bytes of the config descriptor */
  477. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  478. setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
  479. setup->wValue = (uint16_t)((USB_DESCRIPTOR_TYPE_CONFIGURATION << 8) | 0);
  480. setup->wIndex = 0;
  481. setup->wLength = USB_SIZEOF_CONFIG_DESC;
  482. ret = usbh_control_transfer(hport->ep0, setup, ep0_buffer);
  483. if (ret < 0) {
  484. USB_LOG_ERR("Failed to get config descriptor,errorcode:%d\r\n", ret);
  485. goto errout;
  486. }
  487. parse_config_descriptor(hport, (struct usb_configuration_descriptor *)ep0_buffer, USB_SIZEOF_CONFIG_DESC);
  488. /* Read the full size of the configuration data */
  489. uint16_t wTotalLength = ((struct usb_configuration_descriptor *)ep0_buffer)->wTotalLength;
  490. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  491. setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
  492. setup->wValue = (uint16_t)((USB_DESCRIPTOR_TYPE_CONFIGURATION << 8) | 0);
  493. setup->wIndex = 0;
  494. setup->wLength = wTotalLength;
  495. ret = usbh_control_transfer(hport->ep0, setup, ep0_buffer);
  496. if (ret < 0) {
  497. USB_LOG_ERR("Failed to get full config descriptor,errorcode:%d\r\n", ret);
  498. goto errout;
  499. }
  500. parse_config_descriptor(hport, (struct usb_configuration_descriptor *)ep0_buffer, wTotalLength);
  501. USB_LOG_INFO("The device has %d interfaces\r\n", ((struct usb_configuration_descriptor *)ep0_buffer)->bNumInterfaces);
  502. #ifdef CONFIG_USBHOST_GET_STRING_DESC
  503. /* Get Manufacturer string */
  504. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  505. setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
  506. setup->wValue = (uint16_t)((USB_DESCRIPTOR_TYPE_STRING << 8) | USB_STRING_MFC_INDEX);
  507. setup->wIndex = 0x0409;
  508. setup->wLength = 255;
  509. ret = usbh_control_transfer(hport->ep0, setup, ep0_buffer);
  510. if (ret < 0) {
  511. USB_LOG_ERR("Failed to get Manufacturer string,errorcode:%d\r\n", ret);
  512. goto errout;
  513. }
  514. parse_string_descriptor(hport, (struct usb_string_descriptor *)ep0_buffer, USB_STRING_MFC_INDEX);
  515. /* Get Product string */
  516. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  517. setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
  518. setup->wValue = (uint16_t)((USB_DESCRIPTOR_TYPE_STRING << 8) | USB_STRING_PRODUCT_INDEX);
  519. setup->wIndex = 0x0409;
  520. setup->wLength = 255;
  521. ret = usbh_control_transfer(hport->ep0, setup, ep0_buffer);
  522. if (ret < 0) {
  523. USB_LOG_ERR("Failed to get get Product string,errorcode:%d\r\n", ret);
  524. goto errout;
  525. }
  526. parse_string_descriptor(hport, (struct usb_string_descriptor *)ep0_buffer, USB_STRING_PRODUCT_INDEX);
  527. /* Get SerialNumber string */
  528. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  529. setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
  530. setup->wValue = (uint16_t)((USB_DESCRIPTOR_TYPE_STRING << 8) | USB_STRING_SERIAL_INDEX);
  531. setup->wIndex = 0x0409;
  532. setup->wLength = 255;
  533. ret = usbh_control_transfer(hport->ep0, setup, ep0_buffer);
  534. if (ret < 0) {
  535. USB_LOG_ERR("Failed to get get SerialNumber string,errorcode:%d\r\n", ret);
  536. goto errout;
  537. }
  538. parse_string_descriptor(hport, (struct usb_string_descriptor *)ep0_buffer, USB_STRING_SERIAL_INDEX);
  539. #endif
  540. /* Select device configuration 1 */
  541. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  542. setup->bRequest = USB_REQUEST_SET_CONFIGURATION;
  543. setup->wValue = 1;
  544. setup->wIndex = 0;
  545. setup->wLength = 0;
  546. ret = usbh_control_transfer(hport->ep0, setup, NULL);
  547. if (ret < 0) {
  548. USB_LOG_ERR("Failed to set configuration,errorcode:%d\r\n", ret);
  549. goto errout;
  550. }
  551. USB_LOG_INFO("Enumeration success, start loading class driver\r\n");
  552. /*search supported class driver*/
  553. for (uint8_t i = 0; i < hport->config.config_desc.bNumInterfaces; i++) {
  554. intf_desc = &hport->config.intf[i].intf_desc;
  555. struct usbh_class_driver *class_driver = (struct usbh_class_driver *)usbh_find_class_driver(intf_desc->bInterfaceClass, intf_desc->bInterfaceSubClass, intf_desc->bInterfaceProtocol, hport->device_desc.idVendor, hport->device_desc.idProduct);
  556. if (class_driver == NULL) {
  557. USB_LOG_ERR("do not support Class:0x%02x,Subclass:0x%02x,Protocl:0x%02x\r\n",
  558. intf_desc->bInterfaceClass,
  559. intf_desc->bInterfaceSubClass,
  560. intf_desc->bInterfaceProtocol);
  561. continue;
  562. }
  563. hport->config.intf[i].class_driver = class_driver;
  564. USB_LOG_INFO("Loading %s class driver\r\n", class_driver->driver_name);
  565. ret = CLASS_CONNECT(hport, i);
  566. if (ret < 0) {
  567. ret = CLASS_DISCONNECT(hport, i);
  568. goto errout;
  569. }
  570. }
  571. errout:
  572. if (ret < 0) {
  573. usbh_hport_deactivate(hport);
  574. }
  575. if (ep0_buffer) {
  576. usb_iofree(ep0_buffer);
  577. }
  578. return ret;
  579. }
  580. static int usbh_portchange_wait(struct usbh_hubport **hport)
  581. {
  582. struct usbh_hubport *connport = NULL;
  583. uint32_t recved_event;
  584. size_t flags;
  585. int ret;
  586. /* Loop until a change in connection state is detected */
  587. while (1) {
  588. ret = usb_osal_event_recv(usbh_core_cfg.pscevent, USBH_EVENT_CONNECTED | USBH_EVENT_DISCONNECTED, &recved_event);
  589. if (ret < 0) {
  590. continue;
  591. }
  592. flags = usb_osal_enter_critical_section();
  593. for (uint8_t port = USBH_HUB_PORT_START_INDEX; port <= CONFIG_USBHOST_RHPORTS; port++) {
  594. /* Check for a change in the connection state on any root hub port */
  595. connport = &usbh_core_cfg.rhport[port - 1].hport;
  596. if (connport->port_change) {
  597. connport->port_change = false;
  598. /* debounce for port,in order to keep port connect status stability*/
  599. usb_osal_msleep(100);
  600. if (recved_event & USBH_EVENT_CONNECTED) {
  601. if (usbh_get_port_connect_status(port)) {
  602. if (!connport->connected) {
  603. connport->connected = true;
  604. *hport = connport;
  605. usb_osal_leave_critical_section(flags);
  606. return 0;
  607. }
  608. }
  609. }
  610. if (recved_event & USBH_EVENT_DISCONNECTED) {
  611. if (!usbh_get_port_connect_status(port)) {
  612. if (connport->connected) {
  613. connport->connected = false;
  614. *hport = connport;
  615. usb_osal_leave_critical_section(flags);
  616. return 0;
  617. }
  618. }
  619. }
  620. }
  621. }
  622. #ifdef CONFIG_USBHOST_HUB
  623. /* Is a device connected to an external hub? */
  624. if (usbh_core_cfg.active_hport) {
  625. connport = (struct usbh_hubport *)usbh_core_cfg.active_hport;
  626. usbh_core_cfg.active_hport = NULL;
  627. *hport = connport;
  628. usb_osal_leave_critical_section(flags);
  629. return 0;
  630. }
  631. #endif
  632. usb_osal_leave_critical_section(flags);
  633. }
  634. }
  635. static void usbh_portchange_detect_thread(void *argument)
  636. {
  637. struct usbh_hubport *hport = NULL;
  638. usb_hc_sw_init();
  639. for (uint8_t port = USBH_HUB_PORT_START_INDEX; port <= CONFIG_USBHOST_RHPORTS; port++) {
  640. usbh_core_cfg.rhport[port - 1].hport.port = port;
  641. usbh_core_cfg.rhport[port - 1].devgen.next = 1;
  642. usbh_hport_activate(&usbh_core_cfg.rhport[port - 1].hport);
  643. }
  644. usb_hc_hw_init();
  645. while (1) {
  646. usbh_portchange_wait(&hport);
  647. if (hport->connected) {
  648. /*if roothub port,reset port first*/
  649. if (ROOTHUB(hport)) {
  650. /* Reset the host port */
  651. usbh_reset_port(hport->port);
  652. usb_osal_msleep(200);
  653. /* Get the current device speed */
  654. hport->speed = usbh_get_port_speed(hport->port);
  655. USB_LOG_INFO("Hub %u, Port %u connected, %s\r\n", 1, hport->port, speed_table[hport->speed]);
  656. } else {
  657. USB_LOG_INFO("Hub %u, Port %u connected, %s\r\n", hport->parent->index, hport->port, speed_table[hport->speed]);
  658. }
  659. usbh_enumerate(hport);
  660. } else {
  661. usbh_hport_deactivate(hport);
  662. for (uint8_t i = 0; i < hport->config.config_desc.bNumInterfaces; i++) {
  663. if (hport->config.intf[i].class_driver && hport->config.intf[i].class_driver->disconnect) {
  664. CLASS_DISCONNECT(hport, i);
  665. }
  666. }
  667. hport->config.config_desc.bNumInterfaces = 0;
  668. if (ROOTHUB(hport)) {
  669. USB_LOG_INFO("Hub %u,Port:%u disconnected\r\n", 1, hport->port);
  670. } else {
  671. USB_LOG_INFO("Hub %u,Port:%u disconnected\r\n", hport->parent->index, hport->port);
  672. }
  673. }
  674. }
  675. }
  676. void usbh_external_hport_connect(struct usbh_hubport *hport)
  677. {
  678. size_t flags;
  679. usbh_hport_activate(hport);
  680. flags = usb_osal_enter_critical_section();
  681. hport->connected = true;
  682. usbh_core_cfg.active_hport = hport;
  683. usb_osal_leave_critical_section(flags);
  684. usb_osal_event_send(usbh_core_cfg.pscevent, USBH_EVENT_CONNECTED);
  685. }
  686. void usbh_external_hport_disconnect(struct usbh_hubport *hport)
  687. {
  688. size_t flags;
  689. flags = usb_osal_enter_critical_section();
  690. hport->connected = false;
  691. usbh_core_cfg.active_hport = hport;
  692. usb_osal_leave_critical_section(flags);
  693. usb_osal_event_send(usbh_core_cfg.pscevent, USBH_EVENT_DISCONNECTED);
  694. }
  695. void usbh_event_notify_handler(uint8_t event, uint8_t rhport)
  696. {
  697. usbh_core_cfg.rhport[rhport - 1].hport.port_change = true;
  698. usb_osal_event_send(usbh_core_cfg.pscevent, event);
  699. }
  700. int usbh_initialize(void)
  701. {
  702. usb_osal_thread_t usb_thread;
  703. memset(&usbh_core_cfg, 0, sizeof(struct usbh_core_priv));
  704. #ifdef __ARMCC_VERSION /* ARM C Compiler */
  705. extern const int usbh_class_info$$Base;
  706. extern const int usbh_class_info$$Limit;
  707. usbh_class_info_table_begin = (struct usbh_class_info *)&usbh_class_info$$Base;
  708. usbh_class_info_table_end = (struct usbh_class_info *)&usbh_class_info$$Limit;
  709. #elif defined(__GNUC__)
  710. extern uint32_t _usbh_class_info_start;
  711. extern uint32_t _usbh_class_info_end;
  712. usbh_class_info_table_begin = (struct usbh_class_info *)&_usbh_class_info_start;
  713. usbh_class_info_table_end = (struct usbh_class_info *)&_usbh_class_info_end;
  714. #endif
  715. usbh_workq_initialize();
  716. usbh_core_cfg.pscevent = usb_osal_event_create();
  717. if (usbh_core_cfg.pscevent == NULL) {
  718. return -1;
  719. }
  720. usb_thread = usb_osal_thread_create("usbh_psc", CONFIG_USBHOST_PSC_STACKSIZE, CONFIG_USBHOST_PSC_PRIO, usbh_portchange_detect_thread, NULL);
  721. if (usb_thread == NULL) {
  722. return -1;
  723. }
  724. return 0;
  725. }
  726. int lsusb(int argc, char **argv)
  727. {
  728. #ifdef CONFIG_USBHOST_HUB
  729. usb_slist_t *hub_list;
  730. #endif
  731. uint8_t port;
  732. if (argc < 2) {
  733. USB_LOG_RAW("Usage: lsusb [options]...\r\n");
  734. USB_LOG_RAW("List USB devices\r\n");
  735. USB_LOG_RAW(" -v, --verbose\r\n");
  736. USB_LOG_RAW(" Increase verbosity (show descriptors)\r\n");
  737. USB_LOG_RAW(" -s [[bus]:[devnum]]\r\n");
  738. USB_LOG_RAW(" Show only devices with specified device and/or bus numbers (in decimal)\r\n");
  739. USB_LOG_RAW(" -d vendor:[product]\r\n");
  740. USB_LOG_RAW(" Show only devices with the specified vendor and product ID numbers (in hexadecimal)\r\n");
  741. USB_LOG_RAW(" -t, --tree\r\n");
  742. USB_LOG_RAW(" Dump the physical USB device hierachy as a tree\r\n");
  743. USB_LOG_RAW(" -V, --version\r\n");
  744. USB_LOG_RAW(" Show version of program\r\n");
  745. USB_LOG_RAW(" -h, --help\r\n");
  746. USB_LOG_RAW(" Show usage and help\r\n");
  747. return 0;
  748. }
  749. if (argc > 3) {
  750. return 0;
  751. }
  752. if (strcmp(argv[1], "-t") == 0) {
  753. for (port = USBH_HUB_PORT_START_INDEX; port <= CONFIG_USBHOST_RHPORTS; port++) {
  754. if (usbh_core_cfg.rhport[port - 1].hport.connected) {
  755. USB_LOG_RAW("/: Hub %02u,VID:PID 0x%04x:0x%04x\r\n", USBH_ROOT_HUB_INDEX, usbh_core_cfg.rhport[port - 1].hport.device_desc.idVendor, usbh_core_cfg.rhport[port - 1].hport.device_desc.idProduct);
  756. for (uint8_t i = 0; i < usbh_core_cfg.rhport[port - 1].hport.config.config_desc.bNumInterfaces; i++) {
  757. if (usbh_core_cfg.rhport[port - 1].hport.config.intf[i].class_driver->driver_name) {
  758. USB_LOG_RAW(" |__Port %u,Port addr:0x%02x,If %u,ClassDriver=%s\r\n", usbh_core_cfg.rhport[port - 1].hport.port, usbh_core_cfg.rhport[port - 1].hport.dev_addr,
  759. i, usbh_core_cfg.rhport[port - 1].hport.config.intf[i].class_driver->driver_name);
  760. }
  761. }
  762. }
  763. }
  764. #ifdef CONFIG_USBHOST_HUB
  765. usb_slist_for_each(hub_list, &hub_class_head)
  766. {
  767. usbh_hub_t *hub_class = usb_slist_entry(hub_list, struct usbh_hub, list);
  768. for (port = USBH_HUB_PORT_START_INDEX; port <= hub_class->nports; port++) {
  769. if (hub_class->child[port - 1].connected) {
  770. USB_LOG_RAW("/: Hub %02u,VID:PID 0x%04x:0x%04x\r\n", hub_class->index, hub_class->child[port - 1].device_desc.idVendor, hub_class->child[port - 1].device_desc.idProduct);
  771. for (uint8_t i = 0; i < hub_class->child[port - 1].config.config_desc.bNumInterfaces; i++) {
  772. if (hub_class->child[port - 1].config.intf[i].class_driver->driver_name) {
  773. USB_LOG_RAW(" |__Port %u,Port addr:0x%02x,If %u,ClassDriver=%s\r\n", hub_class->child[port - 1].port, hub_class->child[port - 1].dev_addr,
  774. i, hub_class->child[port - 1].config.intf[i].class_driver->driver_name);
  775. }
  776. }
  777. }
  778. }
  779. }
  780. #endif
  781. } else if (strcmp(argv[1], "-v") == 0) {
  782. for (port = USBH_HUB_PORT_START_INDEX; port <= CONFIG_USBHOST_RHPORTS; port++) {
  783. if (usbh_core_cfg.rhport[port - 1].hport.connected) {
  784. USB_LOG_RAW("Hub %02u,Port %u,Port addr:0x%02x,VID:PID 0x%04x:0x%04x\r\n", USBH_ROOT_HUB_INDEX, usbh_core_cfg.rhport[port - 1].hport.port, usbh_core_cfg.rhport[port - 1].hport.dev_addr,
  785. usbh_core_cfg.rhport[port - 1].hport.device_desc.idVendor, usbh_core_cfg.rhport[port - 1].hport.device_desc.idProduct);
  786. usbh_print_hubport_info(&usbh_core_cfg.rhport[port - 1].hport);
  787. }
  788. }
  789. #ifdef CONFIG_USBHOST_HUB
  790. usb_slist_for_each(hub_list, &hub_class_head)
  791. {
  792. usbh_hub_t *hub_class = usb_slist_entry(hub_list, struct usbh_hub, list);
  793. for (port = USBH_HUB_PORT_START_INDEX; port <= hub_class->nports; port++) {
  794. if (hub_class->child[port - 1].connected) {
  795. USB_LOG_RAW("Hub %02u,Port %u,Port addr:0x%02x,VID:PID 0x%04x:0x%04x\r\n", hub_class->index, hub_class->child[port - 1].port, hub_class->child[port - 1].dev_addr,
  796. hub_class->child[port - 1].device_desc.idVendor, hub_class->child[port - 1].device_desc.idProduct);
  797. usbh_print_hubport_info(&hub_class->child[port - 1]);
  798. }
  799. }
  800. }
  801. #endif
  802. }
  803. return 0;
  804. }
  805. struct usbh_hubport *usbh_find_hubport(uint8_t dev_addr)
  806. {
  807. #ifdef CONFIG_USBHOST_HUB
  808. usb_slist_t *hub_list;
  809. #endif
  810. uint8_t port;
  811. for (port = USBH_HUB_PORT_START_INDEX; port <= CONFIG_USBHOST_RHPORTS; port++) {
  812. if (usbh_core_cfg.rhport[port - 1].hport.connected) {
  813. if (usbh_core_cfg.rhport[port - 1].hport.dev_addr == dev_addr) {
  814. return &usbh_core_cfg.rhport[port - 1].hport;
  815. }
  816. }
  817. }
  818. #ifdef CONFIG_USBHOST_HUB
  819. usb_slist_for_each(hub_list, &hub_class_head)
  820. {
  821. usbh_hub_t *hub_class = usb_slist_entry(hub_list, struct usbh_hub, list);
  822. for (port = USBH_HUB_PORT_START_INDEX; port <= hub_class->nports; port++) {
  823. if (hub_class->child[port - 1].connected) {
  824. if (hub_class->child[port - 1].dev_addr == dev_addr) {
  825. return &hub_class->child[port - 1];
  826. }
  827. }
  828. }
  829. }
  830. #endif
  831. return NULL;
  832. }
  833. void *usbh_find_class_instance(const char *devname)
  834. {
  835. #ifdef CONFIG_USBHOST_HUB
  836. usb_slist_t *hub_list;
  837. #endif
  838. struct usbh_hubport *hport;
  839. uint8_t port;
  840. for (port = USBH_HUB_PORT_START_INDEX; port <= CONFIG_USBHOST_RHPORTS; port++) {
  841. hport = &usbh_core_cfg.rhport[port - 1].hport;
  842. if (hport->connected) {
  843. for (uint8_t itf = 0; itf < hport->config.config_desc.bNumInterfaces; itf++) {
  844. if (strncmp(hport->config.intf[itf].devname, devname, CONFIG_USBHOST_DEV_NAMELEN) == 0)
  845. return hport->config.intf[itf].priv;
  846. }
  847. }
  848. }
  849. #ifdef CONFIG_USBHOST_HUB
  850. usb_slist_for_each(hub_list, &hub_class_head)
  851. {
  852. usbh_hub_t *hub_class = usb_slist_entry(hub_list, struct usbh_hub, list);
  853. for (port = USBH_HUB_PORT_START_INDEX; port <= hub_class->nports; port++) {
  854. hport = &hub_class->child[port - 1];
  855. if (hport->connected) {
  856. for (uint8_t itf = 0; itf < hport->config.config_desc.bNumInterfaces; itf++) {
  857. if (strncmp(hport->config.intf[itf].devname, devname, CONFIG_USBHOST_DEV_NAMELEN) == 0)
  858. return hport->config.intf[itf].priv;
  859. }
  860. }
  861. }
  862. }
  863. #endif
  864. return NULL;
  865. }
  866. static const struct usbh_class_driver *usbh_find_class_driver(uint8_t class, uint8_t subclass, uint8_t protocol, uint16_t vid, uint16_t pid)
  867. {
  868. struct usbh_class_info *index = NULL;
  869. for (index = usbh_class_info_table_begin; index < usbh_class_info_table_end; index++) {
  870. if ((index->match_flags & (USB_CLASS_MATCH_VENDOR | USB_CLASS_MATCH_PRODUCT | USB_CLASS_MATCH_INTF_CLASS | USB_CLASS_MATCH_INTF_SUBCLASS | USB_CLASS_MATCH_INTF_PROTOCOL)) ==
  871. (USB_CLASS_MATCH_VENDOR | USB_CLASS_MATCH_PRODUCT | USB_CLASS_MATCH_INTF_CLASS | USB_CLASS_MATCH_INTF_SUBCLASS | USB_CLASS_MATCH_INTF_PROTOCOL)) {
  872. if (index->vid == vid && index->pid == pid &&
  873. index->class == class && index->subclass == subclass && index->protocol == protocol) {
  874. return index->class_driver;
  875. }
  876. } else if ((index->match_flags & (USB_CLASS_MATCH_INTF_CLASS | USB_CLASS_MATCH_INTF_SUBCLASS | USB_CLASS_MATCH_INTF_PROTOCOL)) ==
  877. (USB_CLASS_MATCH_INTF_CLASS | USB_CLASS_MATCH_INTF_SUBCLASS | USB_CLASS_MATCH_INTF_PROTOCOL)) {
  878. if (index->class == class && index->subclass == subclass && index->protocol == protocol) {
  879. return index->class_driver;
  880. }
  881. } else if ((index->match_flags & (USB_CLASS_MATCH_VENDOR | USB_CLASS_MATCH_PRODUCT | USB_CLASS_MATCH_INTF_CLASS)) ==
  882. (USB_CLASS_MATCH_VENDOR | USB_CLASS_MATCH_PRODUCT | USB_CLASS_MATCH_INTF_CLASS)) {
  883. if (index->vid == vid && index->pid == pid && index->class == class) {
  884. return index->class_driver;
  885. }
  886. } else if (index->match_flags & (USB_CLASS_MATCH_INTF_CLASS)) {
  887. if (index->class == class) {
  888. return index->class_driver;
  889. }
  890. }
  891. }
  892. return NULL;
  893. }