core.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2011-12-12 Yi Qiu first version
  9. */
  10. #include <rtthread.h>
  11. #include <drivers/usb_host.h>
  12. static struct uinstance dev[USB_MAX_DEVICE];
  13. /**
  14. * This function will allocate an usb device instance from system.
  15. *
  16. * @param parent the hub instance to which the new allocated device attached.
  17. * @param port the hub port.
  18. *
  19. * @return the allocate instance on successful, or RT_NULL on failure.
  20. */
  21. uinst_t rt_usbh_alloc_instance(uhcd_t uhcd)
  22. {
  23. int i;
  24. /* lock scheduler */
  25. rt_enter_critical();
  26. for(i=0; i<USB_MAX_DEVICE; i++)
  27. {
  28. /* to find an idle instance handle */
  29. if(dev[i].status != DEV_STATUS_IDLE) continue;
  30. /* initialize the usb device instance */
  31. rt_memset(&dev[i], 0, sizeof(struct uinstance));
  32. dev[i].status = DEV_STATUS_BUSY;
  33. dev[i].index = i + 1;
  34. dev[i].address = 0;
  35. dev[i].max_packet_size = 0x8;
  36. rt_list_init(&dev[i].pipe);
  37. dev[i].hcd = uhcd;
  38. /* unlock scheduler */
  39. rt_exit_critical();
  40. return &dev[i];
  41. }
  42. /* unlock scheduler */
  43. rt_exit_critical();
  44. return RT_NULL;
  45. }
  46. /**
  47. * This function will attatch an usb device instance to a host controller,
  48. * and do device enumunation process.
  49. *
  50. * @param hcd the host controller driver.
  51. * @param device the usb device instance.
  52. *
  53. * @return the error code, RT_EOK on successfully.
  54. */
  55. static struct uendpoint_descriptor ep0_out_desc =
  56. {
  57. /*endpoint descriptor*/
  58. USB_DESC_LENGTH_ENDPOINT,
  59. USB_DESC_TYPE_ENDPOINT,
  60. 0x00 | USB_DIR_OUT,
  61. USB_EP_ATTR_CONTROL,
  62. 0x00,
  63. 0x00,
  64. };
  65. static struct uendpoint_descriptor ep0_in_desc =
  66. {
  67. /*endpoint descriptor*/
  68. USB_DESC_LENGTH_ENDPOINT,
  69. USB_DESC_TYPE_ENDPOINT,
  70. 0x00 | USB_DIR_IN,
  71. USB_EP_ATTR_CONTROL,
  72. 0x00,
  73. 0x00,
  74. };
  75. rt_err_t rt_usbh_attatch_instance(uinst_t device)
  76. {
  77. int i = 0;
  78. rt_err_t ret = RT_EOK;
  79. struct uconfig_descriptor cfg_desc;
  80. udev_desc_t dev_desc;
  81. uintf_desc_t intf_desc;
  82. uep_desc_t ep_desc;
  83. rt_uint8_t ep_index;
  84. upipe_t pipe;
  85. ucd_t drv;
  86. RT_ASSERT(device != RT_NULL);
  87. rt_memset(&cfg_desc, 0, sizeof(struct uconfig_descriptor));
  88. dev_desc = &device->dev_desc;
  89. /* alloc address 0 ep0 pipe*/
  90. ep0_out_desc.wMaxPacketSize = 8;
  91. ep0_in_desc.wMaxPacketSize = 8;
  92. rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_out, device, &ep0_out_desc);
  93. rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_in, device, &ep0_in_desc);
  94. RT_DEBUG_LOG(RT_DEBUG_USB, ("start enumnation\n"));
  95. /* get device descriptor head */
  96. ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_DEVICE, (void*)dev_desc, 8);
  97. if(ret != RT_EOK)
  98. {
  99. rt_kprintf("get device descriptor head failed\n");
  100. return ret;
  101. }
  102. /* reset bus */
  103. rt_usbh_hub_reset_port(device->parent_hub, device->port);
  104. rt_thread_delay(2);
  105. rt_usbh_hub_clear_port_feature(device->parent_hub, i + 1, PORT_FEAT_C_CONNECTION);
  106. /* set device address */
  107. ret = rt_usbh_set_address(device);
  108. if(ret != RT_EOK)
  109. {
  110. rt_kprintf("set device address failed\n");
  111. return ret;
  112. }
  113. /* free address 0 ep0 pipe*/
  114. rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_out);
  115. rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_in);
  116. /* set device max packet size */
  117. ep0_out_desc.wMaxPacketSize = device->dev_desc.bMaxPacketSize0;
  118. ep0_in_desc.wMaxPacketSize = device->dev_desc.bMaxPacketSize0;
  119. /* alloc true address ep0 pipe*/
  120. rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_out, device, &ep0_out_desc);
  121. rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_in, device, &ep0_in_desc);
  122. RT_DEBUG_LOG(RT_DEBUG_USB, ("get device descriptor length %d\n",
  123. dev_desc->bLength));
  124. /* get full device descriptor again */
  125. ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_DEVICE, (void*)dev_desc, dev_desc->bLength);
  126. if(ret != RT_EOK)
  127. {
  128. rt_kprintf("get full device descriptor failed\n");
  129. return ret;
  130. }
  131. RT_DEBUG_LOG(RT_DEBUG_USB, ("Vendor ID 0x%x\n", dev_desc->idVendor));
  132. RT_DEBUG_LOG(RT_DEBUG_USB, ("Product ID 0x%x\n", dev_desc->idProduct));
  133. /* get configuration descriptor head */
  134. ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_CONFIGURATION, &cfg_desc, 18);
  135. if(ret != RT_EOK)
  136. {
  137. rt_kprintf("get configuration descriptor head failed\n");
  138. return ret;
  139. }
  140. /* alloc memory for configuration descriptor */
  141. device->cfg_desc = (ucfg_desc_t)rt_malloc(cfg_desc.wTotalLength);
  142. rt_memset(device->cfg_desc, 0, cfg_desc.wTotalLength);
  143. /* get full configuration descriptor */
  144. ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_CONFIGURATION,
  145. device->cfg_desc, cfg_desc.wTotalLength);
  146. if(ret != RT_EOK)
  147. {
  148. rt_kprintf("get full configuration descriptor failed\n");
  149. return ret;
  150. }
  151. /* set configuration */
  152. ret = rt_usbh_set_configure(device, 1);
  153. if(ret != RT_EOK)
  154. {
  155. return ret;
  156. }
  157. for(i=0; i<device->cfg_desc->bNumInterfaces; i++)
  158. {
  159. /* get interface descriptor through configuration descriptor */
  160. ret = rt_usbh_get_interface_descriptor(device->cfg_desc, i, &intf_desc);
  161. if(ret != RT_EOK)
  162. {
  163. rt_kprintf("rt_usb_get_interface_descriptor error\n");
  164. return -RT_ERROR;
  165. }
  166. RT_DEBUG_LOG(RT_DEBUG_USB, ("interface class 0x%x, subclass 0x%x\n",
  167. intf_desc->bInterfaceClass,
  168. intf_desc->bInterfaceSubClass));
  169. /* alloc pipe*/
  170. for(ep_index = 0; ep_index < intf_desc->bNumEndpoints; ep_index++)
  171. {
  172. rt_usbh_get_endpoint_descriptor(intf_desc, ep_index, &ep_desc);
  173. if(ep_desc != RT_NULL)
  174. {
  175. if(rt_usb_hcd_alloc_pipe(device->hcd, &pipe, device, ep_desc) != RT_EOK)
  176. {
  177. rt_kprintf("alloc pipe failed\n");
  178. return RT_ERROR;
  179. }
  180. rt_usb_instance_add_pipe(device,pipe);
  181. }
  182. else
  183. {
  184. rt_kprintf("get endpoint desc failed\n");
  185. return RT_ERROR;
  186. }
  187. }
  188. /* find driver by class code found in interface descriptor */
  189. drv = rt_usbh_class_driver_find(intf_desc->bInterfaceClass,
  190. intf_desc->bInterfaceSubClass);
  191. if(drv != RT_NULL)
  192. {
  193. /* allocate memory for interface device */
  194. device->intf[i] = (struct uhintf*)rt_malloc(sizeof(struct uhintf));
  195. device->intf[i]->drv = drv;
  196. device->intf[i]->device = device;
  197. device->intf[i]->intf_desc = intf_desc;
  198. device->intf[i]->user_data = RT_NULL;
  199. /* open usb class driver */
  200. ret = rt_usbh_class_driver_enable(drv, (void*)device->intf[i]);
  201. if(ret != RT_EOK)
  202. {
  203. rt_kprintf("interface %d run class driver error\n", i);
  204. }
  205. }
  206. else
  207. {
  208. rt_kprintf("find usb device driver failed\n");
  209. continue;
  210. }
  211. }
  212. return RT_EOK;
  213. }
  214. /**
  215. * This function will detach an usb device instance from its host controller,
  216. * and release all resource.
  217. *
  218. * @param device the usb device instance.
  219. *
  220. * @return the error code, RT_EOK on successfully.
  221. */
  222. rt_err_t rt_usbh_detach_instance(uinst_t device)
  223. {
  224. int i = 0;
  225. rt_list_t * l;
  226. if(device == RT_NULL)
  227. {
  228. rt_kprintf("no usb instance to detach\n");
  229. return -RT_ERROR;
  230. }
  231. /* free configration descriptor */
  232. if (device->cfg_desc) {
  233. for (i = 0; i < device->cfg_desc->bNumInterfaces; i++)
  234. {
  235. if (device->intf[i] == RT_NULL) continue;
  236. if (device->intf[i]->drv == RT_NULL) continue;
  237. RT_ASSERT(device->intf[i]->device == device);
  238. RT_DEBUG_LOG(RT_DEBUG_USB, ("free interface instance %d\n", i));
  239. rt_usbh_class_driver_disable(device->intf[i]->drv, (void*)device->intf[i]);
  240. rt_free(device->intf[i]);
  241. }
  242. rt_free(device->cfg_desc);
  243. }
  244. rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_out);
  245. rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_in);
  246. while(device->pipe.next!= &device->pipe)
  247. {
  248. l = device->pipe.next;
  249. rt_list_remove(l);
  250. rt_usb_hcd_free_pipe(device->hcd,rt_list_entry(l,struct upipe,list));
  251. }
  252. rt_memset(device, 0, sizeof(struct uinstance));
  253. return RT_EOK;
  254. }
  255. /**
  256. * This function will do USB_REQ_GET_DESCRIPTO' bRequest for the usb device instance,
  257. *
  258. * @param device the usb device instance.
  259. * @param type the type of descriptor bRequest.
  260. * @param buffer the data buffer to save requested data
  261. * @param nbytes the size of buffer
  262. *
  263. * @return the error code, RT_EOK on successfully.
  264. */
  265. rt_err_t rt_usbh_get_descriptor(uinst_t device, rt_uint8_t type, void* buffer,
  266. int nbytes)
  267. {
  268. struct urequest setup;
  269. int timeout = USB_TIMEOUT_BASIC;
  270. RT_ASSERT(device != RT_NULL);
  271. setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_STANDARD |
  272. USB_REQ_TYPE_DEVICE;
  273. setup.bRequest = USB_REQ_GET_DESCRIPTOR;
  274. setup.wIndex = 0;
  275. setup.wLength = nbytes;
  276. setup.wValue = type << 8;
  277. if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
  278. {
  279. if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, buffer, nbytes, timeout) == nbytes)
  280. {
  281. if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) == 0)
  282. {
  283. return RT_EOK;
  284. }
  285. }
  286. }
  287. return RT_ERROR;
  288. }
  289. /**
  290. * This function will set an address to the usb device.
  291. *
  292. * @param device the usb device instance.
  293. *
  294. * @return the error code, RT_EOK on successfully.
  295. */
  296. rt_err_t rt_usbh_set_address(uinst_t device)
  297. {
  298. struct urequest setup;
  299. int timeout = USB_TIMEOUT_BASIC;
  300. RT_ASSERT(device != RT_NULL);
  301. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usb_set_address\n"));
  302. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
  303. USB_REQ_TYPE_DEVICE;
  304. setup.bRequest = USB_REQ_SET_ADDRESS;
  305. setup.wIndex = 0;
  306. setup.wLength = 0;
  307. setup.wValue = device->index;
  308. if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
  309. {
  310. return RT_ERROR;
  311. }
  312. if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, RT_NULL, 0, timeout) == 0)
  313. {
  314. device->address = device->index;
  315. }
  316. return RT_EOK;
  317. }
  318. /**
  319. * This function will set a configuration to the usb device.
  320. *
  321. * @param device the usb device instance.
  322. * @param config the configuration number.
  323. *
  324. * @return the error code, RT_EOK on successfully.
  325. */
  326. rt_err_t rt_usbh_set_configure(uinst_t device, int config)
  327. {
  328. struct urequest setup;
  329. int timeout = USB_TIMEOUT_BASIC;
  330. /* check parameter */
  331. RT_ASSERT(device != RT_NULL);
  332. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
  333. USB_REQ_TYPE_DEVICE;
  334. setup.bRequest = USB_REQ_SET_CONFIGURATION;
  335. setup.wIndex = 0;
  336. setup.wLength = 0;
  337. setup.wValue = config;
  338. if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
  339. {
  340. return RT_ERROR;
  341. }
  342. if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, RT_NULL, 0, timeout) != 0)
  343. {
  344. return RT_ERROR;
  345. }
  346. return RT_EOK;
  347. }
  348. /**
  349. * This function will set an interface to the usb device.
  350. *
  351. * @param device the usb device instance.
  352. * @param intf the interface number.
  353. *
  354. * @return the error code, RT_EOK on successfully.
  355. */
  356. rt_err_t rt_usbh_set_interface(uinst_t device, int intf)
  357. {
  358. struct urequest setup;
  359. int timeout = USB_TIMEOUT_BASIC;
  360. /* check parameter */
  361. RT_ASSERT(device != RT_NULL);
  362. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
  363. USB_REQ_TYPE_INTERFACE;
  364. setup.bRequest = USB_REQ_SET_INTERFACE;
  365. setup.wIndex = 0;
  366. setup.wLength = 0;
  367. setup.wValue = intf;
  368. if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
  369. {
  370. return RT_ERROR;
  371. }
  372. return RT_EOK;
  373. }
  374. /**
  375. * This function will clear feature for the endpoint of the usb device.
  376. *
  377. * @param device the usb device instance.
  378. * @param endpoint the endpoint number of the usb device.
  379. *
  380. * @return the error code, RT_EOK on successfully.
  381. */
  382. rt_err_t rt_usbh_clear_feature(uinst_t device, int endpoint, int feature)
  383. {
  384. struct urequest setup;
  385. int timeout = USB_TIMEOUT_BASIC;
  386. /* check parameter */
  387. RT_ASSERT(device != RT_NULL);
  388. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
  389. USB_REQ_TYPE_ENDPOINT;
  390. setup.bRequest = USB_REQ_CLEAR_FEATURE;
  391. setup.wIndex = endpoint;
  392. setup.wLength = 0;
  393. setup.wValue = feature;
  394. if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
  395. {
  396. return RT_ERROR;
  397. }
  398. return RT_EOK;
  399. }
  400. /**
  401. * This function will get an interface descriptor from the configuration descriptor.
  402. *
  403. * @param cfg_desc the point of configuration descriptor structure.
  404. * @param num the number of interface descriptor.
  405. * @intf_desc the point of interface descriptor point.
  406. *
  407. * @return the error code, RT_EOK on successfully.
  408. */
  409. rt_err_t rt_usbh_get_interface_descriptor(ucfg_desc_t cfg_desc, int num,
  410. uintf_desc_t* intf_desc)
  411. {
  412. rt_uint32_t ptr, depth = 0;
  413. udesc_t desc;
  414. /* check parameter */
  415. RT_ASSERT(cfg_desc != RT_NULL);
  416. ptr = (rt_uint32_t)cfg_desc + cfg_desc->bLength;
  417. while(ptr < (rt_uint32_t)cfg_desc + cfg_desc->wTotalLength)
  418. {
  419. if(depth++ > 0x20)
  420. {
  421. *intf_desc = RT_NULL;
  422. return -RT_EIO;
  423. }
  424. desc = (udesc_t)ptr;
  425. if(desc->type == USB_DESC_TYPE_INTERFACE)
  426. {
  427. if(((uintf_desc_t)desc)->bInterfaceNumber == num)
  428. {
  429. *intf_desc = (uintf_desc_t)desc;
  430. RT_DEBUG_LOG(RT_DEBUG_USB,
  431. ("rt_usb_get_interface_descriptor: %d\n", num));
  432. return RT_EOK;
  433. }
  434. }
  435. ptr = (rt_uint32_t)desc + desc->bLength;
  436. }
  437. rt_kprintf("rt_usb_get_interface_descriptor %d failed\n", num);
  438. return -RT_EIO;
  439. }
  440. /**
  441. * This function will get an endpoint descriptor from the interface descriptor.
  442. *
  443. * @param intf_desc the point of interface descriptor structure.
  444. * @param num the number of endpoint descriptor.
  445. * @param ep_desc the point of endpoint descriptor point.
  446. *
  447. * @return the error code, RT_EOK on successfully.
  448. */
  449. rt_err_t rt_usbh_get_endpoint_descriptor(uintf_desc_t intf_desc, int num,
  450. uep_desc_t* ep_desc)
  451. {
  452. int count = 0, depth = 0;
  453. rt_uint32_t ptr;
  454. udesc_t desc;
  455. /* check parameter */
  456. RT_ASSERT(intf_desc != RT_NULL);
  457. RT_ASSERT(num < intf_desc->bNumEndpoints);
  458. *ep_desc = RT_NULL;
  459. ptr = (rt_uint32_t)intf_desc + intf_desc->bLength;
  460. while(count < intf_desc->bNumEndpoints)
  461. {
  462. if(depth++ > 0x20)
  463. {
  464. *ep_desc = RT_NULL;
  465. return -RT_EIO;
  466. }
  467. desc = (udesc_t)ptr;
  468. if(desc->type == USB_DESC_TYPE_ENDPOINT)
  469. {
  470. if(num == count)
  471. {
  472. *ep_desc = (uep_desc_t)desc;
  473. RT_DEBUG_LOG(RT_DEBUG_USB,
  474. ("rt_usb_get_endpoint_descriptor: %d\n", num));
  475. return RT_EOK;
  476. }
  477. else count++;
  478. }
  479. ptr = (rt_uint32_t)desc + desc->bLength;
  480. }
  481. rt_kprintf("rt_usb_get_endpoint_descriptor %d failed\n", num);
  482. return -RT_EIO;
  483. }
  484. int rt_usb_hcd_pipe_xfer(uhcd_t hcd, upipe_t pipe, void* buffer, int nbytes, int timeout)
  485. {
  486. rt_size_t remain_size;
  487. rt_size_t send_size;
  488. remain_size = nbytes;
  489. rt_uint8_t * pbuffer = (rt_uint8_t *)buffer;
  490. do
  491. {
  492. RT_DEBUG_LOG(RT_DEBUG_USB,("pipe transform remain size,: %d\n", remain_size));
  493. send_size = (remain_size > pipe->ep.wMaxPacketSize) ? pipe->ep.wMaxPacketSize : remain_size;
  494. if(hcd->ops->pipe_xfer(pipe, USBH_PID_DATA, pbuffer, send_size, timeout) == send_size)
  495. {
  496. remain_size -= send_size;
  497. pbuffer += send_size;
  498. }
  499. else
  500. {
  501. return 0;
  502. }
  503. }while(remain_size > 0);
  504. return nbytes;
  505. }