usbh_core.c 38 KB

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