usbh_core.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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. #ifndef CONFIG_USBHOST_XHCI
  68. if (hport->dev_addr > 0) {
  69. __usbh_free_devaddr(&hport->bus->devgen, hport->dev_addr);
  70. }
  71. #endif
  72. hport->dev_addr = 0;
  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->class == class)) {
  81. continue;
  82. }
  83. if ((index->match_flags & USB_CLASS_MATCH_INTF_SUBCLASS) && !(index->subclass == subclass)) {
  84. continue;
  85. }
  86. if ((index->match_flags & USB_CLASS_MATCH_INTF_PROTOCOL) && !(index->protocol == 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] && index->id_table[i][0] != vid && index->id_table[i][1] != pid; i++) {
  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. #ifdef CONFIG_USBHOST_XHCI
  359. extern int usbh_get_xhci_devaddr(usbh_pipe_t * pipe);
  360. /* Assign a function address to the device connected to this port */
  361. dev_addr = usbh_get_xhci_devaddr(hport->ep0);
  362. if (dev_addr < 0) {
  363. USB_LOG_ERR("Failed to allocate devaddr,errorcode:%d\r\n", ret);
  364. goto errout;
  365. }
  366. #else
  367. /* Assign a function address to the device connected to this port */
  368. dev_addr = usbh_allocate_devaddr(&hport->bus->devgen);
  369. if (dev_addr < 0) {
  370. USB_LOG_ERR("Failed to allocate devaddr,errorcode:%d\r\n", ret);
  371. goto errout;
  372. }
  373. #endif
  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_malloc(wTotalLength);
  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. #ifdef CONFIG_USBHOST_GET_STRING_DESC
  452. uint8_t string_buffer[128];
  453. /* Get Manufacturer string */
  454. memset(string_buffer, 0, 128);
  455. ret = usbh_get_string_desc(hport, USB_STRING_MFC_INDEX, string_buffer);
  456. if (ret < 0) {
  457. USB_LOG_ERR("Failed to get Manufacturer string,errorcode:%d\r\n", ret);
  458. goto errout;
  459. }
  460. USB_LOG_INFO("Manufacturer: %s\r\n", string_buffer);
  461. /* Get Product string */
  462. memset(string_buffer, 0, 128);
  463. ret = usbh_get_string_desc(hport, USB_STRING_PRODUCT_INDEX, string_buffer);
  464. if (ret < 0) {
  465. USB_LOG_ERR("Failed to get get Product string,errorcode:%d\r\n", ret);
  466. goto errout;
  467. }
  468. USB_LOG_INFO("Product: %s\r\n", string_buffer);
  469. /* Get SerialNumber string */
  470. memset(string_buffer, 0, 128);
  471. ret = usbh_get_string_desc(hport, USB_STRING_SERIAL_INDEX, string_buffer);
  472. if (ret < 0) {
  473. USB_LOG_ERR("Failed to get get SerialNumber string,errorcode:%d\r\n", ret);
  474. goto errout;
  475. }
  476. USB_LOG_INFO("SerialNumber: %s\r\n", string_buffer);
  477. #endif
  478. /* Select device configuration 1 */
  479. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  480. setup->bRequest = USB_REQUEST_SET_CONFIGURATION;
  481. setup->wValue = config_value;
  482. setup->wIndex = 0;
  483. setup->wLength = 0;
  484. ret = usbh_control_transfer(hport, setup, NULL);
  485. if (ret < 0) {
  486. USB_LOG_ERR("Failed to set configuration,errorcode:%d\r\n", ret);
  487. goto errout;
  488. }
  489. #ifdef CONFIG_USBHOST_MSOS_ENABLE
  490. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
  491. setup->bRequest = CONFIG_USBHOST_MSOS_VENDOR_CODE;
  492. setup->wValue = 0;
  493. setup->wIndex = 0x0004;
  494. setup->wLength = 16;
  495. ret = usbh_control_transfer(hport, setup, ep0_request_buffer[hport->bus->busid]);
  496. if (ret < 0 && (ret != -EPERM)) {
  497. USB_LOG_ERR("Failed to get msosv1 compat id,errorcode:%d\r\n", ret);
  498. goto errout;
  499. }
  500. #endif
  501. USB_LOG_INFO("Enumeration success, start loading class driver\r\n");
  502. /*search supported class driver*/
  503. for (uint8_t i = 0; i < hport->config.config_desc.bNumInterfaces; i++) {
  504. intf_desc = &hport->config.intf[i].altsetting[0].intf_desc;
  505. 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);
  506. if (class_driver == NULL) {
  507. USB_LOG_ERR("do not support Class:0x%02x,Subclass:0x%02x,Protocl:0x%02x\r\n",
  508. intf_desc->bInterfaceClass,
  509. intf_desc->bInterfaceSubClass,
  510. intf_desc->bInterfaceProtocol);
  511. continue;
  512. }
  513. hport->config.intf[i].class_driver = class_driver;
  514. USB_LOG_INFO("Loading %s class driver\r\n", class_driver->driver_name);
  515. ret = CLASS_CONNECT(hport, i);
  516. }
  517. errout:
  518. if (hport->raw_config_desc) {
  519. usb_free(hport->raw_config_desc);
  520. hport->raw_config_desc = NULL;
  521. }
  522. return ret;
  523. }
  524. void usbh_hubport_release(struct usbh_hubport *hport)
  525. {
  526. if (hport->connected) {
  527. hport->connected = false;
  528. usbh_free_devaddr(hport);
  529. for (uint8_t i = 0; i < hport->config.config_desc.bNumInterfaces; i++) {
  530. if (hport->config.intf[i].class_driver && hport->config.intf[i].class_driver->disconnect) {
  531. CLASS_DISCONNECT(hport, i);
  532. }
  533. }
  534. hport->config.config_desc.bNumInterfaces = 0;
  535. usbh_kill_urb(&hport->ep0_urb);
  536. if (hport->mutex) {
  537. usb_osal_mutex_delete(hport->mutex);
  538. }
  539. }
  540. }
  541. static void usbh_bus_init(struct usbh_bus *bus, uint8_t busid, uint32_t reg_base)
  542. {
  543. struct usbh_hub *hub;
  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_init(&bus->hub_list);
  551. hub = &bus->hcd.roothub;
  552. hub->connected = true;
  553. hub->index = 1;
  554. hub->is_roothub = true;
  555. hub->parent = NULL;
  556. hub->hub_addr = 1;
  557. hub->hub_desc.bNbrPorts = CONFIG_USBHOST_MAX_RHPORTS;
  558. hub->int_buffer = bus->hcd.roothub_intbuf;
  559. hub->bus = bus;
  560. usb_slist_init(&bus->hub_list);
  561. usb_slist_add_tail(&bus->hub_list, &hub->list);
  562. usb_slist_add_tail(&g_bus_head, &bus->list);
  563. }
  564. int usbh_initialize(uint8_t busid, uint32_t reg_base)
  565. {
  566. struct usbh_bus *bus;
  567. if (busid >= CONFIG_USBHOST_MAX_BUS) {
  568. USB_LOG_ERR("bus overflow\r\n");
  569. while (1) {
  570. }
  571. }
  572. bus = &g_usbhost_bus[busid];
  573. usbh_bus_init(bus, busid, reg_base);
  574. #ifdef __ARMCC_VERSION /* ARM C Compiler */
  575. extern const int usbh_class_info$$Base;
  576. extern const int usbh_class_info$$Limit;
  577. usbh_class_info_table_begin = (struct usbh_class_info *)&usbh_class_info$$Base;
  578. usbh_class_info_table_end = (struct usbh_class_info *)&usbh_class_info$$Limit;
  579. #elif defined(__GNUC__)
  580. extern uint32_t __usbh_class_info_start__;
  581. extern uint32_t __usbh_class_info_end__;
  582. usbh_class_info_table_begin = (struct usbh_class_info *)&__usbh_class_info_start__;
  583. usbh_class_info_table_end = (struct usbh_class_info *)&__usbh_class_info_end__;
  584. #elif defined(__ICCARM__) || defined(__ICCRX__) || defined(__ICCRISCV__)
  585. usbh_class_info_table_begin = (struct usbh_class_info *)__section_begin(".usbh_class_info");
  586. usbh_class_info_table_end = (struct usbh_class_info *)__section_end(".usbh_class_info");
  587. #endif
  588. usbh_hub_initialize(bus);
  589. return 0;
  590. }
  591. int usbh_deinitialize(uint8_t busid)
  592. {
  593. struct usbh_bus *bus;
  594. bus = &g_usbhost_bus[busid];
  595. usbh_hub_deinitialize(bus);
  596. usb_slist_init(&bus->hub_list);
  597. usb_slist_remove(&g_bus_head, &bus->list);
  598. return 0;
  599. }
  600. int usbh_control_transfer(struct usbh_hubport *hport, struct usb_setup_packet *setup, uint8_t *buffer)
  601. {
  602. struct usbh_urb *urb;
  603. int ret;
  604. urb = &hport->ep0_urb;
  605. usb_osal_mutex_take(hport->mutex);
  606. memset(urb, 0, sizeof(struct usbh_urb));
  607. usbh_control_urb_fill(urb, hport, setup, buffer, setup->wLength, CONFIG_USBHOST_CONTROL_TRANSFER_TIMEOUT, NULL, NULL);
  608. ret = usbh_submit_urb(urb);
  609. if (ret == 0) {
  610. ret = urb->actual_length;
  611. }
  612. usb_osal_mutex_give(hport->mutex);
  613. return ret;
  614. }
  615. int usbh_get_string_desc(struct usbh_hubport *hport, uint8_t index, uint8_t *output)
  616. {
  617. struct usb_setup_packet *setup = hport->setup;
  618. int ret;
  619. uint8_t *src;
  620. uint8_t *dst;
  621. uint16_t len;
  622. uint16_t i = 2;
  623. uint16_t j = 0;
  624. /* Get Manufacturer string */
  625. setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_DEVICE;
  626. setup->bRequest = USB_REQUEST_GET_DESCRIPTOR;
  627. setup->wValue = (uint16_t)((USB_DESCRIPTOR_TYPE_STRING << 8) | index);
  628. setup->wIndex = 0x0409;
  629. setup->wLength = 255;
  630. ret = usbh_control_transfer(hport, setup, ep0_request_buffer[hport->bus->busid]);
  631. if (ret < 0) {
  632. return ret;
  633. }
  634. src = ep0_request_buffer[hport->bus->busid];
  635. dst = output;
  636. len = src[0];
  637. while (i < len) {
  638. dst[j] = src[i];
  639. i += 2;
  640. j++;
  641. }
  642. return 0;
  643. }
  644. int usbh_set_interface(struct usbh_hubport *hport, uint8_t intf, uint8_t altsetting)
  645. {
  646. struct usb_setup_packet *setup = hport->setup;
  647. setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_STANDARD | USB_REQUEST_RECIPIENT_INTERFACE;
  648. setup->bRequest = USB_REQUEST_SET_INTERFACE;
  649. setup->wValue = altsetting;
  650. setup->wIndex = intf;
  651. setup->wLength = 0;
  652. return usbh_control_transfer(hport, setup, NULL);
  653. }
  654. void *usbh_find_class_instance(const char *devname)
  655. {
  656. struct usbh_hubport *hport;
  657. usb_slist_t *hub_list;
  658. usb_slist_t *bus_list;
  659. usb_slist_for_each(bus_list, &g_bus_head)
  660. {
  661. struct usbh_bus *bus = usb_slist_entry(bus_list, struct usbh_bus, list);
  662. usb_slist_for_each(hub_list, &bus->hub_list)
  663. {
  664. struct usbh_hub *hub = usb_slist_entry(hub_list, struct usbh_hub, list);
  665. for (uint8_t port = 0; port < hub->hub_desc.bNbrPorts; port++) {
  666. hport = &hub->child[port];
  667. if (hport->connected) {
  668. for (uint8_t itf = 0; itf < hport->config.config_desc.bNumInterfaces; itf++) {
  669. if ((strncmp(hport->config.intf[itf].devname, devname, CONFIG_USBHOST_DEV_NAMELEN) == 0) && hport->config.intf[itf].priv)
  670. return hport->config.intf[itf].priv;
  671. }
  672. }
  673. }
  674. }
  675. }
  676. return NULL;
  677. }
  678. int lsusb(int argc, char **argv)
  679. {
  680. usb_slist_t *hub_list;
  681. usb_slist_t *bus_list;
  682. struct usbh_hubport *hport;
  683. if (argc < 2) {
  684. USB_LOG_RAW("Usage: lsusb [options]...\r\n");
  685. USB_LOG_RAW("List USB devices\r\n");
  686. USB_LOG_RAW(" -v, --verbose\r\n");
  687. USB_LOG_RAW(" Increase verbosity (show descriptors)\r\n");
  688. // USB_LOG_RAW(" -s [[bus]:[devnum]]\r\n");
  689. // USB_LOG_RAW(" Show only devices with specified device and/or bus numbers (in decimal)\r\n");
  690. // USB_LOG_RAW(" -d vendor:[product]\r\n");
  691. // USB_LOG_RAW(" Show only devices with the specified vendor and product ID numbers (in hexadecimal)\r\n");
  692. USB_LOG_RAW(" -t, --tree\r\n");
  693. USB_LOG_RAW(" Dump the physical USB device hierachy as a tree\r\n");
  694. USB_LOG_RAW(" -V, --version\r\n");
  695. USB_LOG_RAW(" Show version of program\r\n");
  696. USB_LOG_RAW(" -h, --help\r\n");
  697. USB_LOG_RAW(" Show usage and help\r\n");
  698. return 0;
  699. }
  700. if (argc > 3) {
  701. return 0;
  702. }
  703. if (strcmp(argv[1], "-t") == 0) {
  704. usb_slist_for_each(bus_list, &g_bus_head)
  705. {
  706. struct usbh_bus *bus = usb_slist_entry(bus_list, struct usbh_bus, list);
  707. usb_slist_for_each(hub_list, &bus->hub_list)
  708. {
  709. struct usbh_hub *hub = usb_slist_entry(hub_list, struct usbh_hub, list);
  710. if (hub->is_roothub) {
  711. USB_LOG_RAW("/: Bus %u, Hub %u, ports=%u, is roothub\r\n",
  712. bus->busid,
  713. hub->index,
  714. hub->hub_desc.bNbrPorts);
  715. } else {
  716. USB_LOG_RAW("/: Bus %u, Hub %u, ports=%u, mounted on Hub %02u:Port %u\r\n",
  717. bus->busid,
  718. hub->index,
  719. hub->hub_desc.bNbrPorts,
  720. hub->parent->parent->index,
  721. hub->parent->port);
  722. }
  723. for (uint8_t port = 0; port < hub->hub_desc.bNbrPorts; port++) {
  724. hport = &hub->child[port];
  725. if (hport->connected) {
  726. for (uint8_t i = 0; i < hport->config.config_desc.bNumInterfaces; i++) {
  727. if (hport->config.intf[i].class_driver && hport->config.intf[i].class_driver->driver_name) {
  728. USB_LOG_RAW("\t|__Port %u, dev addr:0x%02x, If %u, ClassDriver=%s\r\n",
  729. hport->port,
  730. hport->dev_addr,
  731. i,
  732. hport->config.intf[i].class_driver->driver_name);
  733. }
  734. }
  735. }
  736. }
  737. }
  738. }
  739. }
  740. if (strcmp(argv[1], "-v") == 0) {
  741. usb_slist_for_each(bus_list, &g_bus_head)
  742. {
  743. struct usbh_bus *bus = usb_slist_entry(bus_list, struct usbh_bus, list);
  744. usb_slist_for_each(hub_list, &bus->hub_list)
  745. {
  746. struct usbh_hub *hub = usb_slist_entry(hub_list, struct usbh_hub, list);
  747. for (uint8_t port = 0; port < hub->hub_desc.bNbrPorts; port++) {
  748. hport = &hub->child[port];
  749. if (hport->connected) {
  750. USB_LOG_RAW("Bus %u, Hub %02u, Port %u, dev addr:0x%02x, VID:PID 0x%04x:0x%04x\r\n",
  751. bus->busid,
  752. hub->index,
  753. hport->port,
  754. hport->dev_addr,
  755. hport->device_desc.idVendor,
  756. hport->device_desc.idProduct);
  757. usbh_print_hubport_info(hport);
  758. }
  759. }
  760. }
  761. }
  762. }
  763. return 0;
  764. }