usbh_core.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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 void usbh_print_setup(struct usb_setup_packet *setup)
  297. {
  298. USB_LOG_DBG("Setup: "
  299. "bmRequestType 0x%02x, bRequest 0x%02x, wValue 0x%04x, wIndex 0x%04x, wLength 0x%04x\r\n",
  300. setup->bmRequestType,
  301. setup->bRequest,
  302. setup->wValue,
  303. setup->wIndex,
  304. setup->wLength);
  305. }
  306. static int usbh_get_default_mps(int speed)
  307. {
  308. switch (speed) {
  309. case USB_SPEED_LOW: /* For low speed, we use 8 bytes */
  310. return 8;
  311. case USB_SPEED_FULL: /* For full or high speed, we use 64 bytes */
  312. case USB_SPEED_HIGH:
  313. return 64;
  314. case USB_SPEED_SUPER: /* For super speed , we must use 512 bytes */
  315. case USB_SPEED_SUPER_PLUS:
  316. return 512;
  317. default:
  318. return 64;
  319. }
  320. }
  321. int usbh_enumerate(struct usbh_hubport *hport)
  322. {
  323. struct usb_interface_descriptor *intf_desc;
  324. struct usb_setup_packet *setup;
  325. struct usb_device_descriptor *dev_desc;
  326. struct usb_endpoint_descriptor *ep;
  327. int dev_addr;
  328. uint16_t ep_mps;
  329. uint8_t config_value;
  330. uint8_t config_index;
  331. int ret;
  332. hport->setup = &g_setup_buffer[hport->bus->busid][hport->parent->index - 1][hport->port - 1];
  333. setup = hport->setup;
  334. ep = &hport->ep0;
  335. /* Config EP0 mps from speed */
  336. ep->bEndpointAddress = 0x00;
  337. ep->bDescriptorType = USB_DESCRIPTOR_TYPE_ENDPOINT;
  338. ep->bmAttributes = USB_ENDPOINT_TYPE_CONTROL;
  339. ep->wMaxPacketSize = usbh_get_default_mps(hport->speed);
  340. ep->bInterval = 0;
  341. ep->bLength = 7;
  342. /* Configure EP0 with zero address */
  343. hport->dev_addr = 0;
  344. /* Read the first 8 bytes of the device descriptor */
  345. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  346. setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
  347. setup->wValue = (uint16_t)((USB_DESCRIPTOR_TYPE_DEVICE << 8) | 0);
  348. setup->wIndex = 0;
  349. setup->wLength = 8;
  350. ret = usbh_control_transfer(hport, setup, ep0_request_buffer[hport->bus->busid]);
  351. if (ret < 0) {
  352. USB_LOG_ERR("Failed to get device descriptor,errorcode:%d\r\n", ret);
  353. goto errout;
  354. }
  355. parse_device_descriptor(hport, (struct usb_device_descriptor *)ep0_request_buffer[hport->bus->busid], 8);
  356. /* Extract the correct max packetsize from the device descriptor */
  357. dev_desc = (struct usb_device_descriptor *)ep0_request_buffer[hport->bus->busid];
  358. if (dev_desc->bcdUSB >= USB_3_0) {
  359. ep_mps = 1 << dev_desc->bMaxPacketSize0;
  360. } else {
  361. ep_mps = dev_desc->bMaxPacketSize0;
  362. }
  363. USB_LOG_DBG("Device rev=%04x cls=%02x sub=%02x proto=%02x size=%d\r\n",
  364. dev_desc->bcdUSB, dev_desc->bDeviceClass, dev_desc->bDeviceSubClass,
  365. dev_desc->bDeviceProtocol, ep_mps);
  366. /* Reconfigure EP0 with the correct maximum packet size */
  367. ep->wMaxPacketSize = ep_mps;
  368. /* Assign a function address to the device connected to this port */
  369. dev_addr = usbh_allocate_devaddr(&hport->bus->devgen);
  370. if (dev_addr < 0) {
  371. USB_LOG_ERR("Failed to allocate devaddr,errorcode:%d\r\n", ret);
  372. goto errout;
  373. }
  374. /* Set the USB device address */
  375. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  376. setup->bRequest = USB_REQUEST_SET_ADDRESS;
  377. setup->wValue = dev_addr;
  378. setup->wIndex = 0;
  379. setup->wLength = 0;
  380. ret = usbh_control_transfer(hport, setup, NULL);
  381. if (ret < 0) {
  382. USB_LOG_ERR("Failed to set devaddr,errorcode:%d\r\n", ret);
  383. goto errout;
  384. }
  385. /* Wait device set address completely */
  386. usb_osal_msleep(2);
  387. /*Reconfigure EP0 with the correct address */
  388. hport->dev_addr = dev_addr;
  389. /* Read the full device descriptor */
  390. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  391. setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
  392. setup->wValue = (uint16_t)((USB_DESCRIPTOR_TYPE_DEVICE << 8) | 0);
  393. setup->wIndex = 0;
  394. setup->wLength = USB_SIZEOF_DEVICE_DESC;
  395. ret = usbh_control_transfer(hport, setup, ep0_request_buffer[hport->bus->busid]);
  396. if (ret < 0) {
  397. USB_LOG_ERR("Failed to get full device descriptor,errorcode:%d\r\n", ret);
  398. goto errout;
  399. }
  400. parse_device_descriptor(hport, (struct usb_device_descriptor *)ep0_request_buffer[hport->bus->busid], USB_SIZEOF_DEVICE_DESC);
  401. USB_LOG_INFO("New device found,idVendor:%04x,idProduct:%04x,bcdDevice:%04x\r\n",
  402. ((struct usb_device_descriptor *)ep0_request_buffer[hport->bus->busid])->idVendor,
  403. ((struct usb_device_descriptor *)ep0_request_buffer[hport->bus->busid])->idProduct,
  404. ((struct usb_device_descriptor *)ep0_request_buffer[hport->bus->busid])->bcdDevice);
  405. USB_LOG_INFO("The device has %d bNumConfigurations\r\n", ((struct usb_device_descriptor *)ep0_request_buffer[hport->bus->busid])->bNumConfigurations);
  406. config_index = 0;
  407. USB_LOG_DBG("The device selects config %d\r\n", config_index);
  408. /* Read the first 9 bytes of the config descriptor */
  409. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  410. setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
  411. setup->wValue = (uint16_t)((USB_DESCRIPTOR_TYPE_CONFIGURATION << 8) | config_index);
  412. setup->wIndex = 0;
  413. setup->wLength = USB_SIZEOF_CONFIG_DESC;
  414. ret = usbh_control_transfer(hport, setup, ep0_request_buffer[hport->bus->busid]);
  415. if (ret < 0) {
  416. USB_LOG_ERR("Failed to get config descriptor,errorcode:%d\r\n", ret);
  417. goto errout;
  418. }
  419. parse_config_descriptor(hport, (struct usb_configuration_descriptor *)ep0_request_buffer[hport->bus->busid], USB_SIZEOF_CONFIG_DESC);
  420. /* Read the full size of the configuration data */
  421. uint16_t wTotalLength = ((struct usb_configuration_descriptor *)ep0_request_buffer[hport->bus->busid])->wTotalLength;
  422. if (wTotalLength > CONFIG_USBHOST_REQUEST_BUFFER_LEN) {
  423. ret = -USB_ERR_NOMEM;
  424. USB_LOG_ERR("wTotalLength %d is overflow, default is %d\r\n", wTotalLength, CONFIG_USBHOST_REQUEST_BUFFER_LEN);
  425. goto errout;
  426. }
  427. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  428. setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
  429. setup->wValue = (uint16_t)((USB_DESCRIPTOR_TYPE_CONFIGURATION << 8) | config_index);
  430. setup->wIndex = 0;
  431. setup->wLength = wTotalLength;
  432. ret = usbh_control_transfer(hport, setup, ep0_request_buffer[hport->bus->busid]);
  433. if (ret < 0) {
  434. USB_LOG_ERR("Failed to get full config descriptor,errorcode:%d\r\n", ret);
  435. goto errout;
  436. }
  437. ret = parse_config_descriptor(hport, (struct usb_configuration_descriptor *)ep0_request_buffer[hport->bus->busid], wTotalLength);
  438. if (ret < 0) {
  439. USB_LOG_ERR("Parse config fail\r\n");
  440. goto errout;
  441. }
  442. USB_LOG_INFO("The device has %d interfaces\r\n", ((struct usb_configuration_descriptor *)ep0_request_buffer[hport->bus->busid])->bNumInterfaces);
  443. hport->raw_config_desc = usb_osal_malloc(wTotalLength + 1);
  444. if (hport->raw_config_desc == NULL) {
  445. ret = -USB_ERR_NOMEM;
  446. USB_LOG_ERR("No memory to alloc for raw_config_desc\r\n");
  447. goto errout;
  448. }
  449. config_value = ((struct usb_configuration_descriptor *)ep0_request_buffer[hport->bus->busid])->bConfigurationValue;
  450. memcpy(hport->raw_config_desc, ep0_request_buffer[hport->bus->busid], wTotalLength);
  451. hport->raw_config_desc[wTotalLength] = '\0';
  452. #ifdef CONFIG_USBHOST_GET_STRING_DESC
  453. uint8_t string_buffer[128];
  454. /* Get Manufacturer string */
  455. memset(string_buffer, 0, 128);
  456. ret = usbh_get_string_desc(hport, USB_STRING_MFC_INDEX, string_buffer);
  457. if (ret < 0) {
  458. USB_LOG_ERR("Failed to get Manufacturer string,errorcode:%d\r\n", ret);
  459. goto errout;
  460. }
  461. USB_LOG_INFO("Manufacturer: %s\r\n", string_buffer);
  462. /* Get Product string */
  463. memset(string_buffer, 0, 128);
  464. ret = usbh_get_string_desc(hport, USB_STRING_PRODUCT_INDEX, string_buffer);
  465. if (ret < 0) {
  466. USB_LOG_ERR("Failed to get get Product string,errorcode:%d\r\n", ret);
  467. goto errout;
  468. }
  469. USB_LOG_INFO("Product: %s\r\n", string_buffer);
  470. /* Get SerialNumber string */
  471. memset(string_buffer, 0, 128);
  472. ret = usbh_get_string_desc(hport, USB_STRING_SERIAL_INDEX, string_buffer);
  473. if (ret < 0) {
  474. USB_LOG_ERR("Failed to get get SerialNumber string,errorcode:%d\r\n", ret);
  475. goto errout;
  476. }
  477. USB_LOG_INFO("SerialNumber: %s\r\n", string_buffer);
  478. #endif
  479. /* Select device configuration 1 */
  480. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  481. setup->bRequest = USB_REQUEST_SET_CONFIGURATION;
  482. setup->wValue = config_value;
  483. setup->wIndex = 0;
  484. setup->wLength = 0;
  485. ret = usbh_control_transfer(hport, setup, NULL);
  486. if (ret < 0) {
  487. USB_LOG_ERR("Failed to set configuration,errorcode:%d\r\n", ret);
  488. goto errout;
  489. }
  490. #ifdef CONFIG_USBHOST_MSOS_ENABLE
  491. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
  492. setup->bRequest = CONFIG_USBHOST_MSOS_VENDOR_CODE;
  493. setup->wValue = 0;
  494. setup->wIndex = 0x0004;
  495. setup->wLength = 16;
  496. ret = usbh_control_transfer(hport, setup, ep0_request_buffer[hport->bus->busid]);
  497. if (ret < 0 && (ret != -USB_ERR_STALL)) {
  498. USB_LOG_ERR("Failed to get msosv1 compat id,errorcode:%d\r\n", ret);
  499. goto errout;
  500. }
  501. #endif
  502. USB_LOG_INFO("Enumeration success, start loading class driver\r\n");
  503. /*search supported class driver*/
  504. for (uint8_t i = 0; i < hport->config.config_desc.bNumInterfaces; i++) {
  505. intf_desc = &hport->config.intf[i].altsetting[0].intf_desc;
  506. 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);
  507. if (class_driver == NULL) {
  508. USB_LOG_ERR("do not support Class:0x%02x,Subclass:0x%02x,Protocl:0x%02x\r\n",
  509. intf_desc->bInterfaceClass,
  510. intf_desc->bInterfaceSubClass,
  511. intf_desc->bInterfaceProtocol);
  512. continue;
  513. }
  514. hport->config.intf[i].class_driver = class_driver;
  515. USB_LOG_INFO("Loading %s class driver\r\n", class_driver->driver_name);
  516. ret = CLASS_CONNECT(hport, i);
  517. }
  518. errout:
  519. if (hport->raw_config_desc) {
  520. usb_osal_free(hport->raw_config_desc);
  521. hport->raw_config_desc = NULL;
  522. }
  523. return ret;
  524. }
  525. void usbh_hubport_release(struct usbh_hubport *hport)
  526. {
  527. if (hport->connected) {
  528. hport->connected = false;
  529. usbh_free_devaddr(hport);
  530. for (uint8_t i = 0; i < hport->config.config_desc.bNumInterfaces; i++) {
  531. if (hport->config.intf[i].class_driver && hport->config.intf[i].class_driver->disconnect) {
  532. CLASS_DISCONNECT(hport, i);
  533. }
  534. }
  535. hport->config.config_desc.bNumInterfaces = 0;
  536. usbh_kill_urb(&hport->ep0_urb);
  537. if (hport->mutex) {
  538. usb_osal_mutex_delete(hport->mutex);
  539. }
  540. }
  541. }
  542. static void usbh_bus_init(struct usbh_bus *bus, uint8_t busid, uintptr_t reg_base)
  543. {
  544. memset(bus, 0, sizeof(struct usbh_bus));
  545. bus->busid = busid;
  546. bus->hcd.hcd_id = busid;
  547. bus->hcd.reg_base = reg_base;
  548. /* devaddr 1 is for roothub */
  549. bus->devgen.next = 2;
  550. usb_slist_add_tail(&g_bus_head, &bus->list);
  551. }
  552. int usbh_initialize(uint8_t busid, uintptr_t reg_base)
  553. {
  554. struct usbh_bus *bus;
  555. if (busid >= CONFIG_USBHOST_MAX_BUS) {
  556. USB_LOG_ERR("bus overflow\r\n");
  557. while (1) {
  558. }
  559. }
  560. bus = &g_usbhost_bus[busid];
  561. usbh_bus_init(bus, busid, reg_base);
  562. #ifdef __ARMCC_VERSION /* ARM C Compiler */
  563. extern const int usbh_class_info$$Base;
  564. extern const int usbh_class_info$$Limit;
  565. usbh_class_info_table_begin = (struct usbh_class_info *)&usbh_class_info$$Base;
  566. usbh_class_info_table_end = (struct usbh_class_info *)&usbh_class_info$$Limit;
  567. #elif defined(__GNUC__)
  568. extern uint32_t __usbh_class_info_start__;
  569. extern uint32_t __usbh_class_info_end__;
  570. usbh_class_info_table_begin = (struct usbh_class_info *)&__usbh_class_info_start__;
  571. usbh_class_info_table_end = (struct usbh_class_info *)&__usbh_class_info_end__;
  572. #elif defined(__ICCARM__) || defined(__ICCRX__) || defined(__ICCRISCV__)
  573. usbh_class_info_table_begin = (struct usbh_class_info *)__section_begin(".usbh_class_info");
  574. usbh_class_info_table_end = (struct usbh_class_info *)__section_end(".usbh_class_info");
  575. #endif
  576. usbh_hub_initialize(bus);
  577. return 0;
  578. }
  579. int usbh_deinitialize(uint8_t busid)
  580. {
  581. struct usbh_bus *bus;
  582. if (busid >= CONFIG_USBHOST_MAX_BUS) {
  583. USB_LOG_ERR("bus overflow\r\n");
  584. while (1) {
  585. }
  586. }
  587. bus = &g_usbhost_bus[busid];
  588. usbh_hub_deinitialize(bus);
  589. usb_slist_remove(&g_bus_head, &bus->list);
  590. return 0;
  591. }
  592. int usbh_control_transfer(struct usbh_hubport *hport, struct usb_setup_packet *setup, uint8_t *buffer)
  593. {
  594. struct usbh_urb *urb;
  595. int ret;
  596. if (!hport || !setup) {
  597. return -USB_ERR_INVAL;
  598. }
  599. urb = &hport->ep0_urb;
  600. usb_osal_mutex_take(hport->mutex);
  601. usbh_print_setup(setup);
  602. usbh_control_urb_fill(urb, hport, setup, buffer, setup->wLength, CONFIG_USBHOST_CONTROL_TRANSFER_TIMEOUT, NULL, NULL);
  603. ret = usbh_submit_urb(urb);
  604. if (ret == 0) {
  605. ret = urb->actual_length;
  606. }
  607. usb_osal_mutex_give(hport->mutex);
  608. return ret;
  609. }
  610. int usbh_get_string_desc(struct usbh_hubport *hport, uint8_t index, uint8_t *output)
  611. {
  612. struct usb_setup_packet *setup = hport->setup;
  613. int ret;
  614. uint8_t *src;
  615. uint8_t *dst;
  616. uint16_t len;
  617. uint16_t i = 2;
  618. uint16_t j = 0;
  619. /* Get Manufacturer string */
  620. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  621. setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
  622. setup->wValue = (uint16_t)((USB_DESCRIPTOR_TYPE_STRING << 8) | index);
  623. setup->wIndex = 0x0409;
  624. setup->wLength = 255;
  625. ret = usbh_control_transfer(hport, setup, ep0_request_buffer[hport->bus->busid]);
  626. if (ret < 0) {
  627. return ret;
  628. }
  629. src = ep0_request_buffer[hport->bus->busid];
  630. dst = output;
  631. len = src[0];
  632. while (i < len) {
  633. dst[j] = src[i];
  634. i += 2;
  635. j++;
  636. }
  637. return 0;
  638. }
  639. int usbh_set_interface(struct usbh_hubport *hport, uint8_t intf, uint8_t altsetting)
  640. {
  641. struct usb_setup_packet *setup = hport->setup;
  642. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_INTERFACE;
  643. setup->bRequest = USB_REQUEST_SET_INTERFACE;
  644. setup->wValue = altsetting;
  645. setup->wIndex = intf;
  646. setup->wLength = 0;
  647. return usbh_control_transfer(hport, setup, NULL);
  648. }
  649. static void *usbh_list_all_interface_name(struct usbh_hub *hub, const char *devname)
  650. {
  651. struct usbh_hubport *hport;
  652. struct usbh_hub *hub_next;
  653. void *priv;
  654. for (uint8_t port = 0; port < hub->nports; port++) {
  655. hport = &hub->child[port];
  656. if (hport->connected) {
  657. for (uint8_t itf = 0; itf < hport->config.config_desc.bNumInterfaces; itf++) {
  658. if (hport->config.intf[itf].class_driver && hport->config.intf[itf].class_driver->driver_name) {
  659. if ((strncmp(hport->config.intf[itf].devname, devname, CONFIG_USBHOST_DEV_NAMELEN) == 0) && hport->config.intf[itf].priv)
  660. return hport->config.intf[itf].priv;
  661. if (strcmp(hport->config.intf[itf].class_driver->driver_name, "hub") == 0) {
  662. hub_next = hport->config.intf[itf].priv;
  663. if (hub_next && hub_next->connected) {
  664. priv = usbh_list_all_interface_name(hub_next, devname);
  665. if (priv) {
  666. return priv;
  667. }
  668. }
  669. }
  670. }
  671. }
  672. }
  673. }
  674. return NULL;
  675. }
  676. static void usbh_list_all_interface_driver(struct usbh_hub *hub)
  677. {
  678. struct usbh_hubport *hport;
  679. struct usbh_hub *hub_next;
  680. const char *speed_table[] = { "error-speed", "low-speed", "full-speed", "high-speed", "wireless-speed", "super-speed", "superplus-speed" };
  681. for (uint8_t port = 0; port < hub->nports; port++) {
  682. hport = &hub->child[port];
  683. if (hport->connected) {
  684. for (uint8_t itf = 0; itf < hport->config.config_desc.bNumInterfaces; itf++) {
  685. if (hport->config.intf[itf].class_driver && hport->config.intf[itf].class_driver->driver_name) {
  686. for (uint8_t j = 0; j < hub->index; j++) {
  687. USB_LOG_RAW("\t");
  688. }
  689. USB_LOG_RAW("|__Port %u, dev addr:0x%02x, If %u, ClassDriver=%s, %s\r\n",
  690. hport->port,
  691. hport->dev_addr,
  692. itf,
  693. hport->config.intf[itf].class_driver->driver_name,
  694. speed_table[hport->speed]);
  695. if (strcmp(hport->config.intf[itf].class_driver->driver_name, "hub") == 0) {
  696. hub_next = hport->config.intf[itf].priv;
  697. if (hub_next && hub_next->connected) {
  698. usbh_list_all_interface_driver(hub_next);
  699. }
  700. }
  701. }
  702. }
  703. }
  704. }
  705. }
  706. static void usbh_list_all_interface_desc(struct usbh_bus *bus, struct usbh_hub *hub)
  707. {
  708. struct usbh_hubport *hport;
  709. struct usbh_hub *hub_next;
  710. for (uint8_t port = 0; port < hub->nports; port++) {
  711. hport = &hub->child[port];
  712. if (hport->connected) {
  713. USB_LOG_RAW("\r\nBus %u, Hub %u, Port %u, dev addr:0x%02x, VID:PID 0x%04x:0x%04x\r\n",
  714. bus->busid,
  715. hub->index,
  716. hport->port,
  717. hport->dev_addr,
  718. hport->device_desc.idVendor,
  719. hport->device_desc.idProduct);
  720. usbh_print_hubport_info(hport);
  721. for (uint8_t itf = 0; itf < hport->config.config_desc.bNumInterfaces; itf++) {
  722. if (hport->config.intf[itf].class_driver && hport->config.intf[itf].class_driver->driver_name) {
  723. if (strcmp(hport->config.intf[itf].class_driver->driver_name, "hub") == 0) {
  724. hub_next = hport->config.intf[itf].priv;
  725. if (hub_next && hub_next->connected) {
  726. usbh_list_all_interface_desc(bus, hub_next);
  727. }
  728. }
  729. }
  730. }
  731. }
  732. }
  733. }
  734. static struct usbh_hubport *usbh_list_all_hubport(struct usbh_hub *hub, uint8_t hub_index, uint8_t hub_port)
  735. {
  736. struct usbh_hubport *hport;
  737. struct usbh_hub *hub_next;
  738. if (hub->index == hub_index) {
  739. hport = &hub->child[hub_port - 1];
  740. if (hport->connected) {
  741. return hport;
  742. } else {
  743. return NULL;
  744. }
  745. } else {
  746. for (uint8_t port = 0; port < hub->nports; port++) {
  747. hport = &hub->child[port];
  748. if (hport->connected) {
  749. for (uint8_t itf = 0; itf < hport->config.config_desc.bNumInterfaces; itf++) {
  750. if (hport->config.intf[itf].class_driver && hport->config.intf[itf].class_driver->driver_name) {
  751. if (strcmp(hport->config.intf[itf].class_driver->driver_name, "hub") == 0) {
  752. hub_next = hport->config.intf[itf].priv;
  753. if (hub_next && hub_next->connected) {
  754. hport = usbh_list_all_hubport(hub_next, hub_index, hub_port);
  755. if (hport) {
  756. return hport;
  757. }
  758. }
  759. }
  760. }
  761. }
  762. }
  763. }
  764. }
  765. return NULL;
  766. }
  767. void *usbh_find_class_instance(const char *devname)
  768. {
  769. usb_slist_t *bus_list;
  770. struct usbh_hub *hub;
  771. struct usbh_bus *bus;
  772. void *priv;
  773. size_t flags;
  774. flags = usb_osal_enter_critical_section();
  775. usb_slist_for_each(bus_list, &g_bus_head)
  776. {
  777. bus = usb_slist_entry(bus_list, struct usbh_bus, list);
  778. hub = &bus->hcd.roothub;
  779. priv = usbh_list_all_interface_name(hub, devname);
  780. if (priv) {
  781. usb_osal_leave_critical_section(flags);
  782. return priv;
  783. }
  784. }
  785. usb_osal_leave_critical_section(flags);
  786. return NULL;
  787. }
  788. struct usbh_hubport *usbh_find_hubport(uint8_t busid, uint8_t hub_index, uint8_t hub_port)
  789. {
  790. struct usbh_hub *hub;
  791. struct usbh_bus *bus;
  792. struct usbh_hubport *hport;
  793. size_t flags;
  794. flags = usb_osal_enter_critical_section();
  795. bus = &g_usbhost_bus[busid];
  796. hub = &bus->hcd.roothub;
  797. hport = usbh_list_all_hubport(hub, hub_index, hub_port);
  798. usb_osal_leave_critical_section(flags);
  799. return hport;
  800. }
  801. int lsusb(int argc, char **argv)
  802. {
  803. usb_slist_t *bus_list;
  804. struct usbh_hub *hub;
  805. struct usbh_bus *bus;
  806. size_t flags;
  807. if (argc < 2) {
  808. USB_LOG_RAW("Usage: lsusb [options]...\r\n");
  809. USB_LOG_RAW("List USB devices\r\n");
  810. USB_LOG_RAW(" -v, --verbose\r\n");
  811. USB_LOG_RAW(" Increase verbosity (show descriptors)\r\n");
  812. // USB_LOG_RAW(" -s [[bus]:[devnum]]\r\n");
  813. // USB_LOG_RAW(" Show only devices with specified device and/or bus numbers (in decimal)\r\n");
  814. // USB_LOG_RAW(" -d vendor:[product]\r\n");
  815. // USB_LOG_RAW(" Show only devices with the specified vendor and product ID numbers (in hexadecimal)\r\n");
  816. USB_LOG_RAW(" -t, --tree\r\n");
  817. USB_LOG_RAW(" Dump the physical USB device hierachy as a tree\r\n");
  818. USB_LOG_RAW(" -V, --version\r\n");
  819. USB_LOG_RAW(" Show version of program\r\n");
  820. USB_LOG_RAW(" -h, --help\r\n");
  821. USB_LOG_RAW(" Show usage and help\r\n");
  822. return 0;
  823. }
  824. if (argc > 3) {
  825. return 0;
  826. }
  827. flags = usb_osal_enter_critical_section();
  828. if (strcmp(argv[1], "-V") == 0) {
  829. USB_LOG_RAW("CherryUSB Version %s\r\n", CHERRYUSB_VERSION_STR);
  830. }
  831. if (strcmp(argv[1], "-t") == 0) {
  832. usb_slist_for_each(bus_list, &g_bus_head)
  833. {
  834. bus = usb_slist_entry(bus_list, struct usbh_bus, list);
  835. hub = &bus->hcd.roothub;
  836. USB_LOG_RAW("/: Bus %u, Hub %u, ports=%u, is roothub\r\n",
  837. bus->busid,
  838. hub->index,
  839. hub->nports);
  840. usbh_list_all_interface_driver(hub);
  841. }
  842. }
  843. if (strcmp(argv[1], "-v") == 0) {
  844. usb_slist_for_each(bus_list, &g_bus_head)
  845. {
  846. bus = usb_slist_entry(bus_list, struct usbh_bus, list);
  847. hub = &bus->hcd.roothub;
  848. usbh_list_all_interface_desc(bus, hub);
  849. }
  850. }
  851. usb_osal_leave_critical_section(flags);
  852. return 0;
  853. }