core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * File : core.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2011, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2011-12-12 Yi Qiu first version
  13. */
  14. #include <rtthread.h>
  15. #include <drivers/usb_host.h>
  16. static struct uinstance uinst[USB_MAX_DEVICE];
  17. /**
  18. * This function will allocate an usb device instance from system.
  19. *
  20. * @param parent the hub instance to which the new allocated device attached.
  21. * @param port the hub port.
  22. *
  23. * @return the allocate instance on successful, or RT_NULL on failure.
  24. */
  25. uinst_t rt_usb_alloc_instance(void)
  26. {
  27. int i;
  28. /* lock scheduler */
  29. rt_enter_critical();
  30. for(i=0; i<USB_MAX_DEVICE; i++)
  31. {
  32. /* to find an idle instance handle */
  33. if(uinst[i].status != UINST_STATUS_IDLE) continue;
  34. /* initialize the usb device instance */
  35. rt_memset(&uinst[i], 0, sizeof(struct uinstance));
  36. uinst[i].status = UINST_STATUS_BUSY;
  37. uinst[i].index = i + 1;
  38. uinst[i].address = 0;
  39. uinst[i].max_packet_size = 0x8;
  40. /* unlock scheduler */
  41. rt_exit_critical();
  42. return &uinst[i];
  43. }
  44. /* unlock scheduler */
  45. rt_exit_critical();
  46. return RT_NULL;
  47. }
  48. /**
  49. * This function will attatch an usb device instance to a host controller,
  50. * and do device enumunation process.
  51. *
  52. * @param hcd the host controller driver.
  53. * @param uinst the usb device instance.
  54. *
  55. * @return the error code, RT_EOK on successfully.
  56. */
  57. rt_err_t rt_usb_attatch_instance(uinst_t uinst)
  58. {
  59. int i = 0;
  60. rt_err_t ret = RT_EOK;
  61. struct uconfig_descriptor cfg_desc;
  62. udev_desc_t dev_desc;
  63. uintf_desc_t intf_desc;
  64. ucd_t drv;
  65. RT_ASSERT(uinst != RT_NULL);
  66. rt_memset(&cfg_desc, 0, sizeof(struct uconfig_descriptor));
  67. dev_desc = &uinst->dev_desc;
  68. RT_DEBUG_LOG(RT_DEBUG_USB, ("start enumnation\n"));
  69. /* get device descriptor head */
  70. ret = rt_usb_get_descriptor(uinst, USB_DESC_TYPE_DEVICE, (void*)dev_desc, 8);
  71. if(ret != RT_EOK)
  72. {
  73. rt_kprintf("get device descriptor head failed\n");
  74. return ret;
  75. }
  76. /* set device address */
  77. ret = rt_usb_set_address(uinst);
  78. if(ret != RT_EOK)
  79. {
  80. rt_kprintf("set device address failed\n");
  81. return ret;
  82. }
  83. /* set device max packet size */
  84. uinst->max_packet_size = uinst->dev_desc.bMaxPacketSize0;
  85. RT_DEBUG_LOG(RT_DEBUG_USB, ("get device descriptor length %d\n",
  86. dev_desc->bLength));
  87. /* get full device descriptor again */
  88. ret = rt_usb_get_descriptor
  89. (uinst, USB_DESC_TYPE_DEVICE, (void*)dev_desc, dev_desc->bLength);
  90. if(ret != RT_EOK)
  91. {
  92. rt_kprintf("get full device descriptor failed\n");
  93. return ret;
  94. }
  95. RT_DEBUG_LOG(RT_DEBUG_USB, ("Vendor ID 0x%x\n", dev_desc->idVendor));
  96. RT_DEBUG_LOG(RT_DEBUG_USB, ("Product ID 0x%x\n", dev_desc->idProduct));
  97. /* get configuration descriptor head */
  98. ret = rt_usb_get_descriptor(uinst, USB_DESC_TYPE_CONFIGURATION, &cfg_desc,
  99. sizeof(struct uconfig_descriptor));
  100. if(ret != RT_EOK)
  101. {
  102. rt_kprintf("get configuration descriptor head failed\n");
  103. return ret;
  104. }
  105. /* alloc memory for configuration descriptor */
  106. uinst->cfg_desc = (ucfg_desc_t)rt_malloc(cfg_desc.wTotalLength);
  107. rt_memset(uinst->cfg_desc, 0, cfg_desc.wTotalLength);
  108. /* get full configuration descriptor */
  109. ret = rt_usb_get_descriptor(uinst, USB_DESC_TYPE_CONFIGURATION,
  110. uinst->cfg_desc, cfg_desc.wTotalLength);
  111. if(ret != RT_EOK)
  112. {
  113. rt_kprintf("get full configuration descriptor failed\n");
  114. return ret;
  115. }
  116. /* set configuration */
  117. ret = rt_usb_set_configure(uinst, 1);
  118. if(ret != RT_EOK) return ret;
  119. for(i=0; i<uinst->cfg_desc->bNumInterfaces; i++)
  120. {
  121. /* get interface descriptor through configuration descriptor */
  122. ret = rt_usb_get_interface_descriptor(uinst->cfg_desc, i, &intf_desc);
  123. if(ret != RT_EOK)
  124. {
  125. rt_kprintf("rt_usb_get_interface_descriptor error\n");
  126. return -RT_ERROR;
  127. }
  128. RT_DEBUG_LOG(RT_DEBUG_USB, ("interface class 0x%x, subclass 0x%x\n",
  129. intf_desc->bInterfaceClass, intf_desc->bInterfaceSubClass));
  130. /* find driver by class code found in interface descriptor */
  131. drv = rt_usb_class_driver_find(intf_desc->bInterfaceClass,
  132. intf_desc->bInterfaceSubClass);
  133. if(drv != RT_NULL)
  134. {
  135. /* allocate memory for interface uinst */
  136. uinst->ifinst[i] =
  137. (uifinst_t)rt_malloc(sizeof(struct uifinst));
  138. uinst->ifinst[i]->drv = drv;
  139. uinst->ifinst[i]->uinst = uinst;
  140. uinst->ifinst[i]->intf_desc = intf_desc;
  141. uinst->ifinst[i]->user_data = RT_NULL;
  142. /* open usb class driver */
  143. ret = rt_usb_class_driver_run(drv, (void*)uinst->ifinst[i]);
  144. if(ret != RT_EOK)
  145. {
  146. rt_kprintf("interface %d run class driver error\n", i);
  147. }
  148. }
  149. else
  150. {
  151. rt_kprintf("find usb device driver failed\n");
  152. continue;
  153. }
  154. }
  155. return RT_EOK;
  156. }
  157. /**
  158. * This function will detach an usb device instance from its host controller,
  159. * and release all resource.
  160. *
  161. * @param uinst the usb device instance.
  162. *
  163. * @return the error code, RT_EOK on successfully.
  164. */
  165. rt_err_t rt_usb_detach_instance(uinst_t uinst)
  166. {
  167. int i = 0;
  168. if(uinst == RT_NULL)
  169. {
  170. rt_kprintf("no usb instance to detach\n");
  171. return -RT_ERROR;
  172. }
  173. /* free configration descriptor */
  174. if(uinst->cfg_desc) rt_free(uinst->cfg_desc);
  175. for(i=0; i<uinst->cfg_desc->bNumInterfaces; i++)
  176. {
  177. if(uinst->ifinst[i] == RT_NULL) continue;
  178. if(uinst->ifinst[i]->drv == RT_NULL) continue;
  179. RT_ASSERT(uinst->ifinst[i]->uinst == uinst);
  180. RT_DEBUG_LOG(RT_DEBUG_USB, ("free interface instance %d\n", i));
  181. rt_usb_class_driver_stop(uinst->ifinst[i]->drv, (void*)uinst->ifinst[i]);
  182. }
  183. rt_memset(uinst, 0, sizeof(struct uinstance));
  184. return RT_EOK;
  185. }
  186. /**
  187. * This function will do USB_REQ_GET_DESCRIPTO' request for the usb device instance,
  188. *
  189. * @param uinst the usb device instance.
  190. * @param type the type of descriptor request.
  191. * @param buffer the data buffer to save requested data
  192. * @param nbytes the size of buffer
  193. *
  194. * @return the error code, RT_EOK on successfully.
  195. */
  196. rt_err_t rt_usb_get_descriptor(uinst_t uinst, rt_uint8_t type, void* buffer,
  197. int nbytes)
  198. {
  199. struct ureqest setup;
  200. int timeout = 100;
  201. RT_ASSERT(uinst != RT_NULL);
  202. setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_STANDARD |
  203. USB_REQ_TYPE_DEVICE;
  204. setup.request = USB_REQ_GET_DESCRIPTOR;
  205. setup.index = 0;
  206. setup.length = nbytes;
  207. setup.value = type << 8;
  208. if(rt_usb_hcd_control_xfer(uinst->hcd, uinst, &setup, buffer, nbytes,
  209. timeout) != nbytes) return -RT_EIO;
  210. else return RT_EOK;
  211. }
  212. /**
  213. * This function will set an address to the usb device.
  214. *
  215. * @param uinst the usb device instance.
  216. *
  217. * @return the error code, RT_EOK on successfully.
  218. */
  219. rt_err_t rt_usb_set_address(uinst_t uinst)
  220. {
  221. struct ureqest setup;
  222. int timeout = 100;
  223. RT_ASSERT(uinst != RT_NULL);
  224. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usb_set_address\n"));
  225. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
  226. USB_REQ_TYPE_DEVICE;
  227. setup.request = USB_REQ_SET_ADDRESS;
  228. setup.index = 0;
  229. setup.length = 0;
  230. setup.value = uinst->index;
  231. if(rt_usb_hcd_control_xfer(uinst->hcd, uinst, &setup, RT_NULL, 0,
  232. timeout) != 0) return -RT_EIO;
  233. rt_thread_delay(50);
  234. uinst->address = uinst->index;
  235. return RT_EOK;
  236. }
  237. /**
  238. * This function will set a configuration to the usb device.
  239. *
  240. * @param uinst the usb device instance.
  241. * @param config the configuration number.
  242. *
  243. * @return the error code, RT_EOK on successfully.
  244. */
  245. rt_err_t rt_usb_set_configure(uinst_t uinst, int config)
  246. {
  247. struct ureqest setup;
  248. int timeout = 100;
  249. /* check parameter */
  250. RT_ASSERT(uinst != RT_NULL);
  251. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
  252. USB_REQ_TYPE_DEVICE;
  253. setup.request = USB_REQ_SET_CONFIGURATION;
  254. setup.index = 0;
  255. setup.length = 0;
  256. setup.value = config;
  257. if(rt_usb_hcd_control_xfer(uinst->hcd, uinst, &setup, RT_NULL, 0,
  258. timeout) != 0) return -RT_EIO;
  259. return RT_EOK;
  260. }
  261. /**
  262. * This function will set an interface to the usb device.
  263. *
  264. * @param uinst the usb device instance.
  265. * @param intf the interface number.
  266. *
  267. * @return the error code, RT_EOK on successfully.
  268. */
  269. rt_err_t rt_usb_set_interface(uinst_t uinst, int intf)
  270. {
  271. struct ureqest setup;
  272. int timeout = 100;
  273. /* check parameter */
  274. RT_ASSERT(uinst != RT_NULL);
  275. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
  276. USB_REQ_TYPE_INTERFACE;
  277. setup.request = USB_REQ_SET_INTERFACE;
  278. setup.index = 0;
  279. setup.length = 0;
  280. setup.value = intf;
  281. if(rt_usb_hcd_control_xfer(uinst->hcd, uinst, &setup, RT_NULL, 0,
  282. timeout) != 0) return -RT_EIO;
  283. return RT_EOK;
  284. }
  285. /**
  286. * This function will clear feature for the endpoint of the usb device.
  287. *
  288. * @param uinst the usb device instance.
  289. * @param endpoint the endpoint number of the usb device.
  290. *
  291. * @return the error code, RT_EOK on successfully.
  292. */
  293. rt_err_t rt_usb_clear_feature(uinst_t uinst, int endpoint, int feature)
  294. {
  295. struct ureqest setup;
  296. int timeout = 100;
  297. /* check parameter */
  298. RT_ASSERT(uinst != RT_NULL);
  299. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
  300. USB_REQ_TYPE_ENDPOINT;
  301. setup.request = USB_REQ_CLEAR_FEATURE;
  302. setup.index = endpoint;
  303. setup.length = 0;
  304. setup.value = feature;
  305. if(rt_usb_hcd_control_xfer(uinst->hcd, uinst, &setup, RT_NULL, 0,
  306. timeout) != 0) return -RT_EIO;
  307. return RT_EOK;
  308. }
  309. /**
  310. * This function will get an interface descriptor from the configuration descriptor.
  311. *
  312. * @param cfg_desc the point of configuration descriptor structure.
  313. * @param num the number of interface descriptor.
  314. * @intf_desc the point of interface descriptor point.
  315. *
  316. * @return the error code, RT_EOK on successfully.
  317. */
  318. rt_err_t rt_usb_get_interface_descriptor(ucfg_desc_t cfg_desc, int num,
  319. uintf_desc_t* intf_desc)
  320. {
  321. rt_uint32_t ptr, depth = 0;
  322. udesc_t desc;
  323. /* check parameter */
  324. RT_ASSERT(cfg_desc != RT_NULL);
  325. ptr = (rt_uint32_t)cfg_desc + cfg_desc->bLength;
  326. while(ptr < (rt_uint32_t)cfg_desc + cfg_desc->wTotalLength)
  327. {
  328. if(depth++ > 0x20)
  329. {
  330. *intf_desc = RT_NULL;
  331. return -RT_EIO;
  332. }
  333. desc = (udesc_t)ptr;
  334. if(desc->type == USB_DESC_TYPE_INTERFACE)
  335. {
  336. if(((uintf_desc_t)desc)->bInterfaceNumber == num)
  337. {
  338. *intf_desc = (uintf_desc_t)desc;
  339. RT_DEBUG_LOG(RT_DEBUG_USB,
  340. ("rt_usb_get_interface_descriptor: %d\n", num));
  341. return RT_EOK;
  342. }
  343. }
  344. ptr = (rt_uint32_t)desc + desc->bLength;
  345. }
  346. rt_kprintf("rt_usb_get_interface_descriptor %d failed\n", num);
  347. return -RT_EIO;
  348. }
  349. /**
  350. * This function will get an endpoint descriptor from the interface descriptor.
  351. *
  352. * @param intf_desc the point of interface descriptor structure.
  353. * @param num the number of endpoint descriptor.
  354. * @param ep_desc the point of endpoint descriptor point.
  355. *
  356. * @return the error code, RT_EOK on successfully.
  357. */
  358. rt_err_t rt_usb_get_endpoint_descriptor(uintf_desc_t intf_desc, int num,
  359. uep_desc_t* ep_desc)
  360. {
  361. int count = 0, depth = 0;
  362. rt_uint32_t ptr;
  363. udesc_t desc;
  364. /* check parameter */
  365. RT_ASSERT(intf_desc != RT_NULL);
  366. RT_ASSERT(num < intf_desc->bNumEndpoints);
  367. ptr = (rt_uint32_t)intf_desc + intf_desc->bLength;
  368. while(count < intf_desc->bNumEndpoints)
  369. {
  370. if(depth++ > 0x20)
  371. {
  372. *ep_desc = RT_NULL;
  373. return -RT_EIO;
  374. }
  375. desc = (udesc_t)ptr;
  376. if(desc->type == USB_DESC_TYPE_ENDPOINT)
  377. {
  378. if(num == count)
  379. {
  380. *ep_desc = (uep_desc_t)desc;
  381. RT_DEBUG_LOG(RT_DEBUG_USB,
  382. ("rt_usb_get_endpoint_descriptor: %d\n", num));
  383. return RT_EOK;
  384. }
  385. else count++;
  386. }
  387. ptr = (rt_uint32_t)desc + desc->bLength;
  388. }
  389. rt_kprintf("rt_usb_get_endpoint_descriptor %d failed\n", num);
  390. return -RT_EIO;
  391. }