usbh_core.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. /*
  2. * Copyright (c) 2022, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbh_core.h"
  7. #undef USB_DBG_TAG
  8. #define USB_DBG_TAG "usbh_core"
  9. #include "usb_log.h"
  10. struct usbh_class_info *usbh_class_info_table_begin = NULL;
  11. struct usbh_class_info *usbh_class_info_table_end = NULL;
  12. usb_slist_t g_bus_head = USB_SLIST_OBJECT_INIT(g_bus_head);
  13. struct setup_align_buffer {
  14. uint8_t buffer[USB_ALIGN_UP(8, CONFIG_USB_ALIGN_SIZE)];
  15. };
  16. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t ep0_request_buffer[CONFIG_USBHOST_MAX_BUS][USB_ALIGN_UP(CONFIG_USBHOST_REQUEST_BUFFER_LEN, CONFIG_USB_ALIGN_SIZE)];
  17. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX struct setup_align_buffer g_setup_buffer[CONFIG_USBHOST_MAX_BUS][CONFIG_USBHOST_MAX_EXTHUBS + 1][CONFIG_USBHOST_MAX_EHPORTS];
  18. struct usbh_bus g_usbhost_bus[CONFIG_USBHOST_MAX_BUS];
  19. /* general descriptor field offsets */
  20. #define DESC_bLength 0 /** Length offset */
  21. #define DESC_bDescriptorType 1 /** Descriptor type offset */
  22. #define USB_DEV_ADDR_MAX 0x7f
  23. #define USB_DEV_ADDR_MARK_OFFSET 5
  24. #define USB_DEV_ADDR_MARK_MASK 0x1f
  25. static int usbh_allocate_devaddr(struct usbh_devaddr_map *devgen)
  26. {
  27. uint8_t startaddr = devgen->next;
  28. uint8_t devaddr;
  29. int index;
  30. int bitno;
  31. for (;;) {
  32. devaddr = devgen->next;
  33. if (devgen->next >= 0x7f) {
  34. devgen->next = 2;
  35. } else {
  36. devgen->next++;
  37. }
  38. index = devaddr >> 5;
  39. bitno = devaddr & 0x1f;
  40. if ((devgen->alloctab[index] & (1 << bitno)) == 0) {
  41. devgen->alloctab[index] |= (1 << bitno);
  42. return (int)devaddr;
  43. }
  44. if (startaddr == devaddr) {
  45. return -USB_ERR_NOMEM;
  46. }
  47. }
  48. }
  49. static int __usbh_free_devaddr(struct usbh_devaddr_map *devgen, uint8_t devaddr)
  50. {
  51. int index;
  52. int bitno;
  53. if ((devaddr > 0) && (devaddr < USB_DEV_ADDR_MAX)) {
  54. index = devaddr >> USB_DEV_ADDR_MARK_OFFSET;
  55. bitno = devaddr & USB_DEV_ADDR_MARK_MASK;
  56. /* Free the address */
  57. if ((devgen->alloctab[index] |= (1 << bitno)) != 0) {
  58. devgen->alloctab[index] &= ~(1 << bitno);
  59. } else {
  60. return -1;
  61. }
  62. if (devaddr < devgen->next) {
  63. devgen->next = devaddr;
  64. }
  65. }
  66. return 0;
  67. }
  68. static int usbh_free_devaddr(struct usbh_hubport *hport)
  69. {
  70. if (hport->dev_addr > 0) {
  71. __usbh_free_devaddr(&hport->bus->devgen, hport->dev_addr);
  72. }
  73. return 0;
  74. }
  75. static const struct usbh_class_driver *usbh_find_class_driver(uint8_t class, uint8_t subclass, uint8_t protocol,
  76. uint16_t vid, uint16_t pid)
  77. {
  78. struct usbh_class_info *index = NULL;
  79. for (index = usbh_class_info_table_begin; index < usbh_class_info_table_end; index++) {
  80. if ((index->match_flags & USB_CLASS_MATCH_INTF_CLASS) && !(index->bInterfaceClass == class)) {
  81. continue;
  82. }
  83. if ((index->match_flags & USB_CLASS_MATCH_INTF_SUBCLASS) && !(index->bInterfaceSubClass == subclass)) {
  84. continue;
  85. }
  86. if ((index->match_flags & USB_CLASS_MATCH_INTF_PROTOCOL) && !(index->bInterfaceProtocol == protocol)) {
  87. continue;
  88. }
  89. if (index->match_flags & USB_CLASS_MATCH_VID_PID && index->id_table) {
  90. /* scan id table */
  91. uint32_t i;
  92. for (i = 0; index->id_table[i][0]; i++) {
  93. if (index->id_table[i][0] == vid && index->id_table[i][1] == pid) {
  94. break;
  95. }
  96. }
  97. /* do not match, continue next */
  98. if (!index->id_table[i][0]) {
  99. continue;
  100. }
  101. }
  102. return index->class_driver;
  103. }
  104. return NULL;
  105. }
  106. static int parse_device_descriptor(struct usbh_hubport *hport, struct usb_device_descriptor *desc, uint16_t length)
  107. {
  108. if (desc->bLength != USB_SIZEOF_DEVICE_DESC) {
  109. USB_LOG_ERR("invalid device bLength 0x%02x\r\n", desc->bLength);
  110. return -USB_ERR_INVAL;
  111. } else if (desc->bDescriptorType != USB_DESCRIPTOR_TYPE_DEVICE) {
  112. USB_LOG_ERR("unexpected device descriptor 0x%02x\r\n", desc->bDescriptorType);
  113. return -USB_ERR_INVAL;
  114. } else {
  115. if (length <= 8) {
  116. return 0;
  117. }
  118. #if 0
  119. USB_LOG_DBG("Device Descriptor:\r\n");
  120. USB_LOG_DBG("bLength: 0x%02x \r\n", desc->bLength);
  121. USB_LOG_DBG("bDescriptorType: 0x%02x \r\n", desc->bDescriptorType);
  122. USB_LOG_DBG("bcdUSB: 0x%04x \r\n", desc->bcdUSB);
  123. USB_LOG_DBG("bDeviceClass: 0x%02x \r\n", desc->bDeviceClass);
  124. USB_LOG_DBG("bDeviceSubClass: 0x%02x \r\n", desc->bDeviceSubClass);
  125. USB_LOG_DBG("bDeviceProtocol: 0x%02x \r\n", desc->bDeviceProtocol);
  126. USB_LOG_DBG("bMaxPacketSize0: 0x%02x \r\n", desc->bMaxPacketSize0);
  127. USB_LOG_DBG("idVendor: 0x%04x \r\n", desc->idVendor);
  128. USB_LOG_DBG("idProduct: 0x%04x \r\n", desc->idProduct);
  129. USB_LOG_DBG("bcdDevice: 0x%04x \r\n", desc->bcdDevice);
  130. USB_LOG_DBG("iManufacturer: 0x%02x \r\n", desc->iManufacturer);
  131. USB_LOG_DBG("iProduct: 0x%02x \r\n", desc->iProduct);
  132. USB_LOG_DBG("iSerialNumber: 0x%02x \r\n", desc->iSerialNumber);
  133. USB_LOG_DBG("bNumConfigurations: 0x%02x\r\n", desc->bNumConfigurations);
  134. #endif
  135. hport->device_desc.bLength = desc->bLength;
  136. hport->device_desc.bDescriptorType = desc->bDescriptorType;
  137. hport->device_desc.bcdUSB = desc->bcdUSB;
  138. hport->device_desc.bDeviceClass = desc->bDeviceClass;
  139. hport->device_desc.bDeviceSubClass = desc->bDeviceSubClass;
  140. hport->device_desc.bDeviceProtocol = desc->bDeviceProtocol;
  141. hport->device_desc.bMaxPacketSize0 = desc->bMaxPacketSize0;
  142. hport->device_desc.idVendor = desc->idVendor;
  143. hport->device_desc.idProduct = desc->idProduct;
  144. hport->device_desc.bcdDevice = desc->bcdDevice;
  145. hport->device_desc.iManufacturer = desc->iManufacturer;
  146. hport->device_desc.iProduct = desc->iProduct;
  147. hport->device_desc.iSerialNumber = desc->iSerialNumber;
  148. hport->device_desc.bNumConfigurations = desc->bNumConfigurations;
  149. }
  150. return 0;
  151. }
  152. static int parse_config_descriptor(struct usbh_hubport *hport, struct usb_configuration_descriptor *desc, uint16_t length)
  153. {
  154. struct usb_interface_descriptor *intf_desc;
  155. struct usb_endpoint_descriptor *ep_desc;
  156. uint8_t cur_alt_setting = 0xff;
  157. uint8_t cur_iface = 0xff;
  158. uint8_t cur_ep = 0xff;
  159. uint8_t cur_ep_num = 0xff;
  160. uint32_t desc_len = 0;
  161. uint8_t *p;
  162. if (desc->bLength != USB_SIZEOF_CONFIG_DESC) {
  163. USB_LOG_ERR("invalid config bLength 0x%02x\r\n", desc->bLength);
  164. return -USB_ERR_INVAL;
  165. } else if (desc->bDescriptorType != USB_DESCRIPTOR_TYPE_CONFIGURATION) {
  166. USB_LOG_ERR("unexpected config descriptor 0x%02x\r\n", desc->bDescriptorType);
  167. return -USB_ERR_INVAL;
  168. } else {
  169. if (length <= USB_SIZEOF_CONFIG_DESC) {
  170. return 0;
  171. }
  172. #if 0
  173. USB_LOG_DBG("Config Descriptor:\r\n");
  174. USB_LOG_DBG("bLength: 0x%02x \r\n", desc->bLength);
  175. USB_LOG_DBG("bDescriptorType: 0x%02x \r\n", desc->bDescriptorType);
  176. USB_LOG_DBG("wTotalLength: 0x%04x \r\n", desc->wTotalLength);
  177. USB_LOG_DBG("bNumInterfaces: 0x%02x \r\n", desc->bNumInterfaces);
  178. USB_LOG_DBG("bConfigurationValue: 0x%02x \r\n", desc->bConfigurationValue);
  179. USB_LOG_DBG("iConfiguration: 0x%02x \r\n", desc->iConfiguration);
  180. USB_LOG_DBG("bmAttributes: 0x%02x \r\n", desc->bmAttributes);
  181. USB_LOG_DBG("bMaxPower: 0x%02x \r\n", desc->bMaxPower);
  182. #endif
  183. hport->config.config_desc.bLength = desc->bLength;
  184. hport->config.config_desc.bDescriptorType = desc->bDescriptorType;
  185. hport->config.config_desc.wTotalLength = desc->wTotalLength;
  186. hport->config.config_desc.bNumInterfaces = desc->bNumInterfaces;
  187. hport->config.config_desc.bConfigurationValue = desc->bConfigurationValue;
  188. hport->config.config_desc.iConfiguration = desc->iConfiguration;
  189. hport->config.config_desc.iConfiguration = desc->iConfiguration;
  190. hport->config.config_desc.bmAttributes = desc->bmAttributes;
  191. hport->config.config_desc.bMaxPower = desc->bMaxPower;
  192. p = (uint8_t *)desc;
  193. p += USB_SIZEOF_CONFIG_DESC;
  194. desc_len = USB_SIZEOF_CONFIG_DESC;
  195. memset(hport->config.intf, 0, sizeof(struct usbh_interface) * CONFIG_USBHOST_MAX_INTERFACES);
  196. while (p[DESC_bLength] && (desc_len <= length)) {
  197. switch (p[DESC_bDescriptorType]) {
  198. case USB_DESCRIPTOR_TYPE_INTERFACE:
  199. intf_desc = (struct usb_interface_descriptor *)p;
  200. cur_iface = intf_desc->bInterfaceNumber;
  201. cur_alt_setting = intf_desc->bAlternateSetting;
  202. cur_ep_num = intf_desc->bNumEndpoints;
  203. cur_ep = 0;
  204. if (cur_iface >= CONFIG_USBHOST_MAX_INTERFACES) {
  205. USB_LOG_ERR("Interface num %d overflow\r\n", cur_iface);
  206. return -USB_ERR_NOMEM;
  207. }
  208. if (cur_ep_num >= CONFIG_USBHOST_MAX_ENDPOINTS) {
  209. USB_LOG_ERR("Endpoint num %d overflow\r\n", cur_ep_num);
  210. return -USB_ERR_NOMEM;
  211. }
  212. if (cur_alt_setting >= CONFIG_USBHOST_MAX_INTF_ALTSETTINGS) {
  213. USB_LOG_ERR("Interface altsetting num %d overflow\r\n", cur_alt_setting);
  214. return -USB_ERR_NOMEM;
  215. }
  216. #if 0
  217. USB_LOG_DBG("Interface Descriptor:\r\n");
  218. USB_LOG_DBG("bLength: 0x%02x \r\n", intf_desc->bLength);
  219. USB_LOG_DBG("bDescriptorType: 0x%02x \r\n", intf_desc->bDescriptorType);
  220. USB_LOG_DBG("bInterfaceNumber: 0x%02x \r\n", intf_desc->bInterfaceNumber);
  221. USB_LOG_DBG("bAlternateSetting: 0x%02x \r\n", intf_desc->bAlternateSetting);
  222. USB_LOG_DBG("bNumEndpoints: 0x%02x \r\n", intf_desc->bNumEndpoints);
  223. USB_LOG_DBG("bInterfaceClass: 0x%02x \r\n", intf_desc->bInterfaceClass);
  224. USB_LOG_DBG("bInterfaceSubClass: 0x%02x \r\n", intf_desc->bInterfaceSubClass);
  225. USB_LOG_DBG("bInterfaceProtocol: 0x%02x \r\n", intf_desc->bInterfaceProtocol);
  226. USB_LOG_DBG("iInterface: 0x%02x \r\n", intf_desc->iInterface);
  227. #endif
  228. memcpy(&hport->config.intf[cur_iface].altsetting[cur_alt_setting].intf_desc, intf_desc, 9);
  229. hport->config.intf[cur_iface].altsetting_num = cur_alt_setting + 1;
  230. break;
  231. case USB_DESCRIPTOR_TYPE_ENDPOINT:
  232. ep_desc = (struct usb_endpoint_descriptor *)p;
  233. memcpy(&hport->config.intf[cur_iface].altsetting[cur_alt_setting].ep[cur_ep].ep_desc, ep_desc, 7);
  234. cur_ep++;
  235. break;
  236. default:
  237. break;
  238. }
  239. /* skip to next descriptor */
  240. p += p[DESC_bLength];
  241. desc_len += p[DESC_bLength];
  242. }
  243. }
  244. return 0;
  245. }
  246. static void usbh_print_hubport_info(struct usbh_hubport *hport)
  247. {
  248. USB_LOG_RAW("Device Descriptor:\r\n");
  249. USB_LOG_RAW("bLength: 0x%02x \r\n", hport->device_desc.bLength);
  250. USB_LOG_RAW("bDescriptorType: 0x%02x \r\n", hport->device_desc.bDescriptorType);
  251. USB_LOG_RAW("bcdUSB: 0x%04x \r\n", hport->device_desc.bcdUSB);
  252. USB_LOG_RAW("bDeviceClass: 0x%02x \r\n", hport->device_desc.bDeviceClass);
  253. USB_LOG_RAW("bDeviceSubClass: 0x%02x \r\n", hport->device_desc.bDeviceSubClass);
  254. USB_LOG_RAW("bDeviceProtocol: 0x%02x \r\n", hport->device_desc.bDeviceProtocol);
  255. USB_LOG_RAW("bMaxPacketSize0: 0x%02x \r\n", hport->device_desc.bMaxPacketSize0);
  256. USB_LOG_RAW("idVendor: 0x%04x \r\n", hport->device_desc.idVendor);
  257. USB_LOG_RAW("idProduct: 0x%04x \r\n", hport->device_desc.idProduct);
  258. USB_LOG_RAW("bcdDevice: 0x%04x \r\n", hport->device_desc.bcdDevice);
  259. USB_LOG_RAW("iManufacturer: 0x%02x \r\n", hport->device_desc.iManufacturer);
  260. USB_LOG_RAW("iProduct: 0x%02x \r\n", hport->device_desc.iProduct);
  261. USB_LOG_RAW("iSerialNumber: 0x%02x \r\n", hport->device_desc.iSerialNumber);
  262. USB_LOG_RAW("bNumConfigurations: 0x%02x\r\n", hport->device_desc.bNumConfigurations);
  263. USB_LOG_RAW("Config Descriptor:\r\n");
  264. USB_LOG_RAW("bLength: 0x%02x \r\n", hport->config.config_desc.bLength);
  265. USB_LOG_RAW("bDescriptorType: 0x%02x \r\n", hport->config.config_desc.bDescriptorType);
  266. USB_LOG_RAW("wTotalLength: 0x%04x \r\n", hport->config.config_desc.wTotalLength);
  267. USB_LOG_RAW("bNumInterfaces: 0x%02x \r\n", hport->config.config_desc.bNumInterfaces);
  268. USB_LOG_RAW("bConfigurationValue: 0x%02x \r\n", hport->config.config_desc.bConfigurationValue);
  269. USB_LOG_RAW("iConfiguration: 0x%02x \r\n", hport->config.config_desc.iConfiguration);
  270. USB_LOG_RAW("bmAttributes: 0x%02x \r\n", hport->config.config_desc.bmAttributes);
  271. USB_LOG_RAW("bMaxPower: 0x%02x \r\n", hport->config.config_desc.bMaxPower);
  272. for (uint8_t i = 0; i < hport->config.config_desc.bNumInterfaces; i++) {
  273. for (uint8_t j = 0; j < hport->config.intf[i].altsetting_num; j++) {
  274. USB_LOG_RAW("\tInterface Descriptor:\r\n");
  275. USB_LOG_RAW("\tbLength: 0x%02x \r\n", hport->config.intf[i].altsetting[j].intf_desc.bLength);
  276. USB_LOG_RAW("\tbDescriptorType: 0x%02x \r\n", hport->config.intf[i].altsetting[j].intf_desc.bDescriptorType);
  277. USB_LOG_RAW("\tbInterfaceNumber: 0x%02x \r\n", hport->config.intf[i].altsetting[j].intf_desc.bInterfaceNumber);
  278. USB_LOG_RAW("\tbAlternateSetting: 0x%02x \r\n", hport->config.intf[i].altsetting[j].intf_desc.bAlternateSetting);
  279. USB_LOG_RAW("\tbNumEndpoints: 0x%02x \r\n", hport->config.intf[i].altsetting[j].intf_desc.bNumEndpoints);
  280. USB_LOG_RAW("\tbInterfaceClass: 0x%02x \r\n", hport->config.intf[i].altsetting[j].intf_desc.bInterfaceClass);
  281. USB_LOG_RAW("\tbInterfaceSubClass: 0x%02x \r\n", hport->config.intf[i].altsetting[j].intf_desc.bInterfaceSubClass);
  282. USB_LOG_RAW("\tbInterfaceProtocol: 0x%02x \r\n", hport->config.intf[i].altsetting[j].intf_desc.bInterfaceProtocol);
  283. USB_LOG_RAW("\tiInterface: 0x%02x \r\n", hport->config.intf[i].altsetting[j].intf_desc.iInterface);
  284. for (uint8_t k = 0; k < hport->config.intf[i].altsetting[j].intf_desc.bNumEndpoints; k++) {
  285. USB_LOG_RAW("\t\tEndpoint Descriptor:\r\n");
  286. USB_LOG_RAW("\t\tbLength: 0x%02x \r\n", hport->config.intf[i].altsetting[j].ep[k].ep_desc.bLength);
  287. USB_LOG_RAW("\t\tbDescriptorType: 0x%02x \r\n", hport->config.intf[i].altsetting[j].ep[k].ep_desc.bDescriptorType);
  288. USB_LOG_RAW("\t\tbEndpointAddress: 0x%02x \r\n", hport->config.intf[i].altsetting[j].ep[k].ep_desc.bEndpointAddress);
  289. USB_LOG_RAW("\t\tbmAttributes: 0x%02x \r\n", hport->config.intf[i].altsetting[j].ep[k].ep_desc.bmAttributes);
  290. USB_LOG_RAW("\t\twMaxPacketSize: 0x%04x \r\n", hport->config.intf[i].altsetting[j].ep[k].ep_desc.wMaxPacketSize);
  291. USB_LOG_RAW("\t\tbInterval: 0x%02x \r\n", hport->config.intf[i].altsetting[j].ep[k].ep_desc.bInterval);
  292. }
  293. }
  294. }
  295. }
  296. static void usbh_print_setup(struct usb_setup_packet *setup)
  297. {
  298. (void)setup;
  299. USB_LOG_DBG("Setup: "
  300. "bmRequestType 0x%02x, bRequest 0x%02x, wValue 0x%04x, wIndex 0x%04x, wLength 0x%04x\r\n",
  301. setup->bmRequestType,
  302. setup->bRequest,
  303. setup->wValue,
  304. setup->wIndex,
  305. setup->wLength);
  306. }
  307. static int usbh_get_default_mps(int speed)
  308. {
  309. switch (speed) {
  310. case USB_SPEED_LOW: /* For low speed, we use 8 bytes */
  311. return 8;
  312. case USB_SPEED_FULL: /* For full or high speed, we use 64 bytes */
  313. case USB_SPEED_HIGH:
  314. return 64;
  315. case USB_SPEED_SUPER: /* For super speed , we must use 512 bytes */
  316. case USB_SPEED_SUPER_PLUS:
  317. return 512;
  318. default:
  319. return 64;
  320. }
  321. }
  322. int usbh_enumerate(struct usbh_hubport *hport)
  323. {
  324. struct usb_interface_descriptor *intf_desc;
  325. struct usb_setup_packet *setup;
  326. struct usb_device_descriptor *dev_desc;
  327. struct usb_endpoint_descriptor *ep;
  328. int dev_addr;
  329. uint16_t ep_mps;
  330. uint8_t config_value;
  331. uint8_t config_index;
  332. int ret;
  333. hport->setup = (struct usb_setup_packet *)&g_setup_buffer[hport->bus->busid][hport->parent->index - 1][hport->port - 1];
  334. setup = hport->setup;
  335. ep = &hport->ep0;
  336. /* Config EP0 mps from speed */
  337. ep->bEndpointAddress = 0x00;
  338. ep->bDescriptorType = USB_DESCRIPTOR_TYPE_ENDPOINT;
  339. ep->bmAttributes = USB_ENDPOINT_TYPE_CONTROL;
  340. ep->wMaxPacketSize = usbh_get_default_mps(hport->speed);
  341. ep->bInterval = 0;
  342. ep->bLength = 7;
  343. /* Configure EP0 with zero address */
  344. hport->dev_addr = 0;
  345. /* Read the first 8 bytes of the device descriptor */
  346. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  347. setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
  348. setup->wValue = (uint16_t)((USB_DESCRIPTOR_TYPE_DEVICE << 8) | 0);
  349. setup->wIndex = 0;
  350. setup->wLength = 8;
  351. ret = usbh_control_transfer(hport, setup, ep0_request_buffer[hport->bus->busid]);
  352. if (ret < 0) {
  353. USB_LOG_ERR("Failed to get device descriptor,errorcode:%d\r\n", ret);
  354. goto errout;
  355. }
  356. ret = parse_device_descriptor(hport, (struct usb_device_descriptor *)ep0_request_buffer[hport->bus->busid], 8);
  357. if (ret < 0) {
  358. USB_LOG_ERR("Parse device descriptor fail\r\n");
  359. goto errout;
  360. }
  361. /* Extract the correct max packetsize from the device descriptor */
  362. dev_desc = (struct usb_device_descriptor *)ep0_request_buffer[hport->bus->busid];
  363. if (dev_desc->bcdUSB >= USB_3_0) {
  364. ep_mps = 1 << dev_desc->bMaxPacketSize0;
  365. } else {
  366. ep_mps = dev_desc->bMaxPacketSize0;
  367. }
  368. USB_LOG_DBG("Device rev=%04x cls=%02x sub=%02x proto=%02x size=%d\r\n",
  369. dev_desc->bcdUSB, dev_desc->bDeviceClass, dev_desc->bDeviceSubClass,
  370. dev_desc->bDeviceProtocol, ep_mps);
  371. /* Reconfigure EP0 with the correct maximum packet size */
  372. ep->wMaxPacketSize = ep_mps;
  373. /* Assign a function address to the device connected to this port */
  374. dev_addr = usbh_allocate_devaddr(&hport->bus->devgen);
  375. if (dev_addr < 0) {
  376. USB_LOG_ERR("Failed to allocate devaddr,errorcode:%d\r\n", ret);
  377. goto errout;
  378. }
  379. /* Set the USB device address */
  380. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  381. setup->bRequest = USB_REQUEST_SET_ADDRESS;
  382. setup->wValue = dev_addr;
  383. setup->wIndex = 0;
  384. setup->wLength = 0;
  385. ret = usbh_control_transfer(hport, setup, NULL);
  386. if (ret < 0) {
  387. USB_LOG_ERR("Failed to set devaddr,errorcode:%d\r\n", ret);
  388. goto errout;
  389. }
  390. /* Wait device set address completely */
  391. usb_osal_msleep(10);
  392. /*Reconfigure EP0 with the correct address */
  393. hport->dev_addr = dev_addr;
  394. /* Read the full device descriptor */
  395. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  396. setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
  397. setup->wValue = (uint16_t)((USB_DESCRIPTOR_TYPE_DEVICE << 8) | 0);
  398. setup->wIndex = 0;
  399. setup->wLength = USB_SIZEOF_DEVICE_DESC;
  400. ret = usbh_control_transfer(hport, setup, ep0_request_buffer[hport->bus->busid]);
  401. if (ret < 0) {
  402. USB_LOG_ERR("Failed to get full device descriptor,errorcode:%d\r\n", ret);
  403. goto errout;
  404. }
  405. parse_device_descriptor(hport, (struct usb_device_descriptor *)ep0_request_buffer[hport->bus->busid], USB_SIZEOF_DEVICE_DESC);
  406. USB_LOG_INFO("New device found,idVendor:%04x,idProduct:%04x,bcdDevice:%04x\r\n",
  407. ((struct usb_device_descriptor *)ep0_request_buffer[hport->bus->busid])->idVendor,
  408. ((struct usb_device_descriptor *)ep0_request_buffer[hport->bus->busid])->idProduct,
  409. ((struct usb_device_descriptor *)ep0_request_buffer[hport->bus->busid])->bcdDevice);
  410. USB_LOG_INFO("The device has %d bNumConfigurations\r\n", ((struct usb_device_descriptor *)ep0_request_buffer[hport->bus->busid])->bNumConfigurations);
  411. config_index = 0;
  412. USB_LOG_DBG("The device selects config %d\r\n", config_index);
  413. /* Read the first 9 bytes of the config descriptor */
  414. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  415. setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
  416. setup->wValue = (uint16_t)((USB_DESCRIPTOR_TYPE_CONFIGURATION << 8) | config_index);
  417. setup->wIndex = 0;
  418. setup->wLength = USB_SIZEOF_CONFIG_DESC;
  419. ret = usbh_control_transfer(hport, setup, ep0_request_buffer[hport->bus->busid]);
  420. if (ret < 0) {
  421. USB_LOG_ERR("Failed to get config descriptor,errorcode:%d\r\n", ret);
  422. goto errout;
  423. }
  424. ret = parse_config_descriptor(hport, (struct usb_configuration_descriptor *)ep0_request_buffer[hport->bus->busid], USB_SIZEOF_CONFIG_DESC);
  425. if (ret < 0) {
  426. USB_LOG_ERR("Parse config descriptor fail\r\n");
  427. goto errout;
  428. }
  429. /* Read the full size of the configuration data */
  430. uint16_t wTotalLength = ((struct usb_configuration_descriptor *)ep0_request_buffer[hport->bus->busid])->wTotalLength;
  431. if (wTotalLength > CONFIG_USBHOST_REQUEST_BUFFER_LEN) {
  432. ret = -USB_ERR_NOMEM;
  433. USB_LOG_ERR("wTotalLength %d is overflow, default is %d\r\n", wTotalLength, (unsigned int)CONFIG_USBHOST_REQUEST_BUFFER_LEN);
  434. goto errout;
  435. }
  436. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  437. setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
  438. setup->wValue = (uint16_t)((USB_DESCRIPTOR_TYPE_CONFIGURATION << 8) | config_index);
  439. setup->wIndex = 0;
  440. setup->wLength = wTotalLength;
  441. ret = usbh_control_transfer(hport, setup, ep0_request_buffer[hport->bus->busid]);
  442. if (ret < 0) {
  443. USB_LOG_ERR("Failed to get full config descriptor,errorcode:%d\r\n", ret);
  444. goto errout;
  445. }
  446. ret = parse_config_descriptor(hport, (struct usb_configuration_descriptor *)ep0_request_buffer[hport->bus->busid], wTotalLength);
  447. if (ret < 0) {
  448. USB_LOG_ERR("Parse config descriptor fail\r\n");
  449. goto errout;
  450. }
  451. USB_LOG_INFO("The device has %d interfaces\r\n", ((struct usb_configuration_descriptor *)ep0_request_buffer[hport->bus->busid])->bNumInterfaces);
  452. hport->raw_config_desc = usb_osal_malloc(wTotalLength + 1);
  453. if (hport->raw_config_desc == NULL) {
  454. ret = -USB_ERR_NOMEM;
  455. USB_LOG_ERR("No memory to alloc for raw_config_desc\r\n");
  456. goto errout;
  457. }
  458. config_value = ((struct usb_configuration_descriptor *)ep0_request_buffer[hport->bus->busid])->bConfigurationValue;
  459. memcpy(hport->raw_config_desc, ep0_request_buffer[hport->bus->busid], wTotalLength);
  460. hport->raw_config_desc[wTotalLength] = '\0';
  461. #ifdef CONFIG_USBHOST_GET_STRING_DESC
  462. uint8_t string_buffer[128];
  463. if (hport->device_desc.iManufacturer > 0) {
  464. /* Get Manufacturer string */
  465. memset(string_buffer, 0, 128);
  466. ret = usbh_get_string_desc(hport, USB_STRING_MFC_INDEX, string_buffer, 128);
  467. if (ret < 0) {
  468. USB_LOG_ERR("Failed to get Manufacturer string,errorcode:%d\r\n", ret);
  469. goto errout;
  470. }
  471. USB_LOG_INFO("Manufacturer: %s\r\n", string_buffer);
  472. } else {
  473. USB_LOG_WRN("Do not support Manufacturer string\r\n");
  474. }
  475. if (hport->device_desc.iProduct > 0) {
  476. /* Get Product string */
  477. memset(string_buffer, 0, 128);
  478. ret = usbh_get_string_desc(hport, USB_STRING_PRODUCT_INDEX, string_buffer, 128);
  479. if (ret < 0) {
  480. USB_LOG_ERR("Failed to get Product string,errorcode:%d\r\n", ret);
  481. goto errout;
  482. }
  483. USB_LOG_INFO("Product: %s\r\n", string_buffer);
  484. } else {
  485. USB_LOG_WRN("Do not support Product string\r\n");
  486. }
  487. if (hport->device_desc.iSerialNumber > 0) {
  488. /* Get SerialNumber string */
  489. memset(string_buffer, 0, 128);
  490. ret = usbh_get_string_desc(hport, USB_STRING_SERIAL_INDEX, string_buffer, 128);
  491. if (ret < 0) {
  492. USB_LOG_ERR("Failed to get SerialNumber string,errorcode:%d\r\n", ret);
  493. goto errout;
  494. }
  495. USB_LOG_INFO("SerialNumber: %s\r\n", string_buffer);
  496. } else {
  497. USB_LOG_WRN("Do not support SerialNumber string\r\n");
  498. }
  499. #endif
  500. /* Select device configuration 1 */
  501. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  502. setup->bRequest = USB_REQUEST_SET_CONFIGURATION;
  503. setup->wValue = config_value;
  504. setup->wIndex = 0;
  505. setup->wLength = 0;
  506. ret = usbh_control_transfer(hport, setup, NULL);
  507. if (ret < 0) {
  508. USB_LOG_ERR("Failed to set configuration,errorcode:%d\r\n", ret);
  509. goto errout;
  510. }
  511. #ifdef CONFIG_USBHOST_MSOS_ENABLE
  512. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
  513. setup->bRequest = CONFIG_USBHOST_MSOS_VENDOR_CODE;
  514. setup->wValue = 0;
  515. setup->wIndex = 0x0004;
  516. setup->wLength = 16;
  517. ret = usbh_control_transfer(hport, setup, ep0_request_buffer[hport->bus->busid]);
  518. if (ret < 0 && (ret != -USB_ERR_STALL)) {
  519. USB_LOG_ERR("Failed to get msosv1 compat id,errorcode:%d\r\n", ret);
  520. goto errout;
  521. }
  522. #endif
  523. USB_LOG_INFO("Enumeration success, start loading class driver\r\n");
  524. /*search supported class driver*/
  525. for (uint8_t i = 0; i < hport->config.config_desc.bNumInterfaces; i++) {
  526. intf_desc = &hport->config.intf[i].altsetting[0].intf_desc;
  527. 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);
  528. if (class_driver == NULL) {
  529. USB_LOG_ERR("do not support Class:0x%02x,Subclass:0x%02x,Protocl:0x%02x\r\n",
  530. intf_desc->bInterfaceClass,
  531. intf_desc->bInterfaceSubClass,
  532. intf_desc->bInterfaceProtocol);
  533. continue;
  534. }
  535. hport->config.intf[i].class_driver = class_driver;
  536. USB_LOG_INFO("Loading %s class driver\r\n", class_driver->driver_name);
  537. ret = CLASS_CONNECT(hport, i);
  538. }
  539. errout:
  540. if (hport->raw_config_desc) {
  541. usb_osal_free(hport->raw_config_desc);
  542. hport->raw_config_desc = NULL;
  543. }
  544. return ret;
  545. }
  546. void usbh_hubport_release(struct usbh_hubport *hport)
  547. {
  548. if (hport->connected) {
  549. hport->connected = false;
  550. usbh_free_devaddr(hport);
  551. for (uint8_t i = 0; i < hport->config.config_desc.bNumInterfaces; i++) {
  552. if (hport->config.intf[i].class_driver && hport->config.intf[i].class_driver->disconnect) {
  553. CLASS_DISCONNECT(hport, i);
  554. }
  555. }
  556. hport->config.config_desc.bNumInterfaces = 0;
  557. usbh_kill_urb(&hport->ep0_urb);
  558. if (hport->mutex) {
  559. usb_osal_mutex_delete(hport->mutex);
  560. }
  561. }
  562. }
  563. static void usbh_bus_init(struct usbh_bus *bus, uint8_t busid, uintptr_t reg_base)
  564. {
  565. memset(bus, 0, sizeof(struct usbh_bus));
  566. bus->busid = busid;
  567. bus->hcd.hcd_id = busid;
  568. bus->hcd.reg_base = reg_base;
  569. /* devaddr 1 is for roothub */
  570. bus->devgen.next = 2;
  571. usb_slist_add_tail(&g_bus_head, &bus->list);
  572. }
  573. int usbh_initialize(uint8_t busid, uintptr_t reg_base)
  574. {
  575. struct usbh_bus *bus;
  576. if (busid >= CONFIG_USBHOST_MAX_BUS) {
  577. USB_LOG_ERR("bus overflow\r\n");
  578. while (1) {
  579. }
  580. }
  581. bus = &g_usbhost_bus[busid];
  582. usbh_bus_init(bus, busid, reg_base);
  583. #ifdef __ARMCC_VERSION /* ARM C Compiler */
  584. extern const int usbh_class_info$$Base;
  585. extern const int usbh_class_info$$Limit;
  586. usbh_class_info_table_begin = (struct usbh_class_info *)&usbh_class_info$$Base;
  587. usbh_class_info_table_end = (struct usbh_class_info *)&usbh_class_info$$Limit;
  588. #elif defined(__GNUC__)
  589. extern uint32_t __usbh_class_info_start__;
  590. extern uint32_t __usbh_class_info_end__;
  591. usbh_class_info_table_begin = (struct usbh_class_info *)&__usbh_class_info_start__;
  592. usbh_class_info_table_end = (struct usbh_class_info *)&__usbh_class_info_end__;
  593. #elif defined(__ICCARM__) || defined(__ICCRX__) || defined(__ICCRISCV__)
  594. usbh_class_info_table_begin = (struct usbh_class_info *)__section_begin(".usbh_class_info");
  595. usbh_class_info_table_end = (struct usbh_class_info *)__section_end(".usbh_class_info");
  596. #endif
  597. usbh_hub_initialize(bus);
  598. return 0;
  599. }
  600. int usbh_deinitialize(uint8_t busid)
  601. {
  602. struct usbh_bus *bus;
  603. if (busid >= CONFIG_USBHOST_MAX_BUS) {
  604. USB_LOG_ERR("bus overflow\r\n");
  605. while (1) {
  606. }
  607. }
  608. bus = &g_usbhost_bus[busid];
  609. usbh_hub_deinitialize(bus);
  610. usb_slist_remove(&g_bus_head, &bus->list);
  611. return 0;
  612. }
  613. int usbh_control_transfer(struct usbh_hubport *hport, struct usb_setup_packet *setup, uint8_t *buffer)
  614. {
  615. struct usbh_urb *urb;
  616. int ret;
  617. if (!hport || !setup) {
  618. return -USB_ERR_INVAL;
  619. }
  620. urb = &hport->ep0_urb;
  621. usb_osal_mutex_take(hport->mutex);
  622. usbh_print_setup(setup);
  623. usbh_control_urb_fill(urb, hport, setup, buffer, setup->wLength, CONFIG_USBHOST_CONTROL_TRANSFER_TIMEOUT, NULL, NULL);
  624. ret = usbh_submit_urb(urb);
  625. if (ret == 0) {
  626. ret = urb->actual_length;
  627. }
  628. usb_osal_mutex_give(hport->mutex);
  629. return ret;
  630. }
  631. int usbh_get_string_desc(struct usbh_hubport *hport, uint8_t index, uint8_t *output, uint16_t output_len)
  632. {
  633. struct usb_setup_packet *setup = hport->setup;
  634. int ret;
  635. uint8_t *src;
  636. uint8_t *dst;
  637. uint16_t len;
  638. uint16_t i = 2;
  639. uint16_t j = 0;
  640. /* Get Manufacturer string */
  641. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  642. setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
  643. setup->wValue = (uint16_t)((USB_DESCRIPTOR_TYPE_STRING << 8) | index);
  644. setup->wIndex = 0x0409;
  645. setup->wLength = 255;
  646. ret = usbh_control_transfer(hport, setup, ep0_request_buffer[hport->bus->busid]);
  647. if (ret < 0) {
  648. return ret;
  649. }
  650. src = ep0_request_buffer[hport->bus->busid];
  651. dst = output;
  652. len = src[0];
  653. if (((len - 2) / 2) > output_len) {
  654. return -USB_ERR_NOMEM;
  655. }
  656. while (i < len) {
  657. dst[j] = src[i];
  658. i += 2;
  659. j++;
  660. }
  661. return 0;
  662. }
  663. int usbh_set_interface(struct usbh_hubport *hport, uint8_t intf, uint8_t altsetting)
  664. {
  665. struct usb_setup_packet *setup = hport->setup;
  666. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_INTERFACE;
  667. setup->bRequest = USB_REQUEST_SET_INTERFACE;
  668. setup->wValue = altsetting;
  669. setup->wIndex = intf;
  670. setup->wLength = 0;
  671. return usbh_control_transfer(hport, setup, NULL);
  672. }
  673. static void *usbh_list_all_interface_name(struct usbh_hub *hub, const char *devname)
  674. {
  675. struct usbh_hubport *hport;
  676. struct usbh_hub *hub_next;
  677. void *priv;
  678. for (uint8_t port = 0; port < hub->nports; port++) {
  679. hport = &hub->child[port];
  680. if (hport->connected) {
  681. for (uint8_t itf = 0; itf < hport->config.config_desc.bNumInterfaces; itf++) {
  682. if (hport->config.intf[itf].class_driver && hport->config.intf[itf].class_driver->driver_name) {
  683. if ((strncmp(hport->config.intf[itf].devname, devname, CONFIG_USBHOST_DEV_NAMELEN) == 0) && hport->config.intf[itf].priv)
  684. return hport->config.intf[itf].priv;
  685. if (strcmp(hport->config.intf[itf].class_driver->driver_name, "hub") == 0) {
  686. hub_next = hport->config.intf[itf].priv;
  687. if (hub_next && hub_next->connected) {
  688. priv = usbh_list_all_interface_name(hub_next, devname);
  689. if (priv) {
  690. return priv;
  691. }
  692. }
  693. }
  694. }
  695. }
  696. }
  697. }
  698. return NULL;
  699. }
  700. static void usbh_list_all_interface_driver(struct usbh_hub *hub)
  701. {
  702. struct usbh_hubport *hport;
  703. struct usbh_hub *hub_next;
  704. const char *speed_table[] = { "error-speed", "low-speed", "full-speed", "high-speed", "wireless-speed", "super-speed", "superplus-speed" };
  705. for (uint8_t port = 0; port < hub->nports; port++) {
  706. hport = &hub->child[port];
  707. if (hport->connected) {
  708. for (uint8_t itf = 0; itf < hport->config.config_desc.bNumInterfaces; itf++) {
  709. if (hport->config.intf[itf].class_driver && hport->config.intf[itf].class_driver->driver_name) {
  710. for (uint8_t j = 0; j < hub->index; j++) {
  711. USB_LOG_RAW("\t");
  712. }
  713. USB_LOG_RAW("|__Port %u, dev addr:0x%02x, If %u, ClassDriver=%s, %s\r\n",
  714. hport->port,
  715. hport->dev_addr,
  716. itf,
  717. hport->config.intf[itf].class_driver->driver_name,
  718. speed_table[hport->speed]);
  719. if (strcmp(hport->config.intf[itf].class_driver->driver_name, "hub") == 0) {
  720. hub_next = hport->config.intf[itf].priv;
  721. if (hub_next && hub_next->connected) {
  722. usbh_list_all_interface_driver(hub_next);
  723. }
  724. }
  725. }
  726. }
  727. }
  728. }
  729. }
  730. static void usbh_list_all_interface_desc(struct usbh_bus *bus, struct usbh_hub *hub)
  731. {
  732. struct usbh_hubport *hport;
  733. struct usbh_hub *hub_next;
  734. for (uint8_t port = 0; port < hub->nports; port++) {
  735. hport = &hub->child[port];
  736. if (hport->connected) {
  737. USB_LOG_RAW("\r\nBus %u, Hub %u, Port %u, dev addr:0x%02x, VID:PID 0x%04x:0x%04x\r\n",
  738. bus->busid,
  739. hub->index,
  740. hport->port,
  741. hport->dev_addr,
  742. hport->device_desc.idVendor,
  743. hport->device_desc.idProduct);
  744. usbh_print_hubport_info(hport);
  745. for (uint8_t itf = 0; itf < hport->config.config_desc.bNumInterfaces; itf++) {
  746. if (hport->config.intf[itf].class_driver && hport->config.intf[itf].class_driver->driver_name) {
  747. if (strcmp(hport->config.intf[itf].class_driver->driver_name, "hub") == 0) {
  748. hub_next = hport->config.intf[itf].priv;
  749. if (hub_next && hub_next->connected) {
  750. usbh_list_all_interface_desc(bus, hub_next);
  751. }
  752. }
  753. }
  754. }
  755. }
  756. }
  757. }
  758. static struct usbh_hubport *usbh_list_all_hubport(struct usbh_hub *hub, uint8_t hub_index, uint8_t hub_port)
  759. {
  760. struct usbh_hubport *hport;
  761. struct usbh_hub *hub_next;
  762. if (hub->index == hub_index) {
  763. hport = &hub->child[hub_port - 1];
  764. if (hport->connected) {
  765. return hport;
  766. } else {
  767. return NULL;
  768. }
  769. } else {
  770. for (uint8_t port = 0; port < hub->nports; port++) {
  771. hport = &hub->child[port];
  772. if (hport->connected) {
  773. for (uint8_t itf = 0; itf < hport->config.config_desc.bNumInterfaces; itf++) {
  774. if (hport->config.intf[itf].class_driver && hport->config.intf[itf].class_driver->driver_name) {
  775. if (strcmp(hport->config.intf[itf].class_driver->driver_name, "hub") == 0) {
  776. hub_next = hport->config.intf[itf].priv;
  777. if (hub_next && hub_next->connected) {
  778. hport = usbh_list_all_hubport(hub_next, hub_index, hub_port);
  779. if (hport) {
  780. return hport;
  781. }
  782. }
  783. }
  784. }
  785. }
  786. }
  787. }
  788. }
  789. return NULL;
  790. }
  791. void *usbh_find_class_instance(const char *devname)
  792. {
  793. usb_slist_t *bus_list;
  794. struct usbh_hub *hub;
  795. struct usbh_bus *bus;
  796. void *priv;
  797. size_t flags;
  798. flags = usb_osal_enter_critical_section();
  799. usb_slist_for_each(bus_list, &g_bus_head)
  800. {
  801. bus = usb_slist_entry(bus_list, struct usbh_bus, list);
  802. hub = &bus->hcd.roothub;
  803. priv = usbh_list_all_interface_name(hub, devname);
  804. if (priv) {
  805. usb_osal_leave_critical_section(flags);
  806. return priv;
  807. }
  808. }
  809. usb_osal_leave_critical_section(flags);
  810. return NULL;
  811. }
  812. struct usbh_hubport *usbh_find_hubport(uint8_t busid, uint8_t hub_index, uint8_t hub_port)
  813. {
  814. struct usbh_hub *hub;
  815. struct usbh_bus *bus;
  816. struct usbh_hubport *hport;
  817. size_t flags;
  818. flags = usb_osal_enter_critical_section();
  819. bus = &g_usbhost_bus[busid];
  820. hub = &bus->hcd.roothub;
  821. hport = usbh_list_all_hubport(hub, hub_index, hub_port);
  822. usb_osal_leave_critical_section(flags);
  823. return hport;
  824. }
  825. int lsusb(int argc, char **argv)
  826. {
  827. usb_slist_t *bus_list;
  828. struct usbh_hub *hub;
  829. struct usbh_bus *bus;
  830. size_t flags;
  831. if (argc < 2) {
  832. USB_LOG_RAW("Usage: lsusb [options]...\r\n");
  833. USB_LOG_RAW("List USB devices\r\n");
  834. USB_LOG_RAW(" -v, --verbose\r\n");
  835. USB_LOG_RAW(" Increase verbosity (show descriptors)\r\n");
  836. // USB_LOG_RAW(" -s [[bus]:[devnum]]\r\n");
  837. // USB_LOG_RAW(" Show only devices with specified device and/or bus numbers (in decimal)\r\n");
  838. // USB_LOG_RAW(" -d vendor:[product]\r\n");
  839. // USB_LOG_RAW(" Show only devices with the specified vendor and product ID numbers (in hexadecimal)\r\n");
  840. USB_LOG_RAW(" -t, --tree\r\n");
  841. USB_LOG_RAW(" Dump the physical USB device hierachy as a tree\r\n");
  842. USB_LOG_RAW(" -V, --version\r\n");
  843. USB_LOG_RAW(" Show version of program\r\n");
  844. USB_LOG_RAW(" -h, --help\r\n");
  845. USB_LOG_RAW(" Show usage and help\r\n");
  846. return 0;
  847. }
  848. if (argc > 3) {
  849. return 0;
  850. }
  851. flags = usb_osal_enter_critical_section();
  852. if (strcmp(argv[1], "-V") == 0) {
  853. USB_LOG_RAW("CherryUSB Version %s\r\n", CHERRYUSB_VERSION_STR);
  854. }
  855. if (strcmp(argv[1], "-t") == 0) {
  856. usb_slist_for_each(bus_list, &g_bus_head)
  857. {
  858. bus = usb_slist_entry(bus_list, struct usbh_bus, list);
  859. hub = &bus->hcd.roothub;
  860. USB_LOG_RAW("/: Bus %u, Hub %u, ports=%u, is roothub\r\n",
  861. bus->busid,
  862. hub->index,
  863. hub->nports);
  864. usbh_list_all_interface_driver(hub);
  865. }
  866. }
  867. if (strcmp(argv[1], "-v") == 0) {
  868. usb_slist_for_each(bus_list, &g_bus_head)
  869. {
  870. bus = usb_slist_entry(bus_list, struct usbh_bus, list);
  871. hub = &bus->hcd.roothub;
  872. usbh_list_all_interface_desc(bus, hub);
  873. }
  874. }
  875. usb_osal_leave_critical_section(flags);
  876. return 0;
  877. }