usbh_core.c 36 KB

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