core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. /* get device descriptor head */
  69. ret = rt_usb_get_descriptor(uinst, USB_DESC_TYPE_DEVICE, (void*)dev_desc, 8);
  70. if(ret != RT_EOK)
  71. {
  72. rt_kprintf("get device descriptor head failed\n");
  73. return ret;
  74. }
  75. /* set device address */
  76. ret = rt_usb_set_address(uinst);
  77. if(ret != RT_EOK)
  78. {
  79. rt_kprintf("set device address failed\n");
  80. return ret;
  81. }
  82. /* set device max packet size */
  83. uinst->max_packet_size = uinst->dev_desc.bMaxPacketSize0;
  84. RT_DEBUG_LOG(RT_DEBUG_USB, ("get device descriptor length %d\n",
  85. dev_desc->bLength));
  86. /* get full device descriptor again */
  87. ret = rt_usb_get_descriptor
  88. (uinst, USB_DESC_TYPE_DEVICE, (void*)dev_desc, dev_desc->bLength);
  89. if(ret != RT_EOK)
  90. {
  91. rt_kprintf("get full device descriptor failed\n");
  92. return ret;
  93. }
  94. /* get configuration descriptor head */
  95. ret = rt_usb_get_descriptor(uinst, USB_DESC_TYPE_CONFIGURATION, &cfg_desc,
  96. sizeof(struct uconfig_descriptor));
  97. if(ret != RT_EOK)
  98. {
  99. rt_kprintf("get configuration descriptor head failed\n");
  100. return ret;
  101. }
  102. /* alloc memory for configuration descriptor */
  103. uinst->cfg_desc = (ucfg_desc_t)rt_malloc(cfg_desc.wTotalLength);
  104. rt_memset(uinst->cfg_desc, 0, cfg_desc.wTotalLength);
  105. /* get full configuration descriptor */
  106. ret = rt_usb_get_descriptor(uinst, USB_DESC_TYPE_CONFIGURATION,
  107. uinst->cfg_desc, cfg_desc.wTotalLength);
  108. if(ret != RT_EOK)
  109. {
  110. rt_kprintf("get full configuration descriptor failed\n");
  111. return ret;
  112. }
  113. /* set configuration */
  114. ret = rt_usb_set_configure(uinst, 1);
  115. if(ret != RT_EOK) return ret;
  116. //for(i=0; i<uinst->cfg_desc->bNumInterfaces; i++)
  117. {
  118. /* get interface descriptor through configuration descriptor */
  119. ret = rt_usb_get_interface_descriptor(uinst->cfg_desc, i, &intf_desc);
  120. if(ret != RT_EOK)
  121. {
  122. rt_kprintf("rt_usb_get_interface_descriptor error\n");
  123. return -RT_ERROR;
  124. }
  125. /* find driver by class code found in interface descriptor */
  126. drv = rt_usb_class_driver_find(intf_desc->bInterfaceClass,
  127. intf_desc->bInterfaceSubClass);
  128. if(drv != RT_NULL)
  129. {
  130. /* allocate memory for interface uinst */
  131. uinst->ifinst[i] =
  132. (uifinst_t)rt_malloc(sizeof(struct uifinst));
  133. uinst->ifinst[i]->drv = drv;
  134. uinst->ifinst[i]->uinst = uinst;
  135. uinst->ifinst[i]->intf_desc = intf_desc;
  136. /* open usb class driver */
  137. ret = rt_usb_class_driver_run(drv, (void*)uinst->ifinst[i]);
  138. if(ret != RT_EOK)
  139. {
  140. rt_kprintf("interface %d run class driver error\n", i);
  141. }
  142. }
  143. else
  144. {
  145. rt_kprintf("find usb device driver failed\n");
  146. return -RT_ERROR;
  147. }
  148. }
  149. return RT_EOK;
  150. }
  151. /**
  152. * This function will detach an usb device instance from its host controller,
  153. * and release all resource.
  154. *
  155. * @param uinst the usb device instance.
  156. *
  157. * @return the error code, RT_EOK on successfully.
  158. */
  159. rt_err_t rt_usb_detach_instance(uinst_t uinst)
  160. {
  161. int i = 0;
  162. if(uinst == RT_NULL)
  163. {
  164. rt_kprintf("no usb instance to detach\n");
  165. return -RT_ERROR;
  166. }
  167. /* free configration descriptor */
  168. if(uinst->cfg_desc) rt_free(uinst->cfg_desc);
  169. for(i=0; i<uinst->cfg_desc->bNumInterfaces; i++)
  170. {
  171. if(uinst->ifinst[i] == RT_NULL) continue;
  172. if(uinst->ifinst[i]->drv == RT_NULL) continue;
  173. RT_ASSERT(uinst->ifinst[i]->uinst == uinst);
  174. RT_DEBUG_LOG(RT_DEBUG_USB, ("free interface instance %d\n", i));
  175. rt_usb_class_driver_stop(uinst->ifinst[i]->drv, (void*)uinst->ifinst[i]);
  176. }
  177. rt_memset(uinst, 0, sizeof(struct uinstance));
  178. return RT_EOK;
  179. }
  180. /**
  181. * This function will do USB_REQ_GET_DESCRIPTO' request for the usb device instance,
  182. *
  183. * @param uinst the usb device instance.
  184. * @param type the type of descriptor request.
  185. * @param buffer the data buffer to save requested data
  186. * @param nbytes the size of buffer
  187. *
  188. * @return the error code, RT_EOK on successfully.
  189. */
  190. rt_err_t rt_usb_get_descriptor(uinst_t uinst, rt_uint8_t type, void* buffer,
  191. int nbytes)
  192. {
  193. struct ureqest setup;
  194. int timeout = 100;
  195. RT_ASSERT(uinst != RT_NULL);
  196. setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_STANDARD |
  197. USB_REQ_TYPE_DEVICE;
  198. setup.request = USB_REQ_GET_DESCRIPTOR;
  199. setup.index = 0;
  200. setup.length = nbytes;
  201. setup.value = type << 8;
  202. if(rt_usb_hcd_control_xfer(uinst->hcd, uinst, &setup, buffer, nbytes,
  203. timeout) != nbytes) return -RT_EIO;
  204. else return RT_EOK;
  205. }
  206. /**
  207. * This function will set an address to the usb device.
  208. *
  209. * @param uinst the usb device instance.
  210. *
  211. * @return the error code, RT_EOK on successfully.
  212. */
  213. rt_err_t rt_usb_set_address(uinst_t uinst)
  214. {
  215. struct ureqest setup;
  216. int timeout = 100;
  217. RT_ASSERT(uinst != RT_NULL);
  218. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usb_set_address\n"));
  219. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
  220. USB_REQ_TYPE_DEVICE;
  221. setup.request = USB_REQ_SET_ADDRESS;
  222. setup.index = 0;
  223. setup.length = 0;
  224. setup.value = uinst->index;
  225. if(rt_usb_hcd_control_xfer(uinst->hcd, uinst, &setup, RT_NULL, 0,
  226. timeout) != 0) return -RT_EIO;
  227. rt_thread_delay(50);
  228. uinst->address = uinst->index;
  229. return RT_EOK;
  230. }
  231. /**
  232. * This function will set a configuration to the usb device.
  233. *
  234. * @param uinst the usb device instance.
  235. * @param config the configuration number.
  236. *
  237. * @return the error code, RT_EOK on successfully.
  238. */
  239. rt_err_t rt_usb_set_configure(uinst_t uinst, int config)
  240. {
  241. struct ureqest setup;
  242. int timeout = 100;
  243. /* check parameter */
  244. RT_ASSERT(uinst != RT_NULL);
  245. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
  246. USB_REQ_TYPE_DEVICE;
  247. setup.request = USB_REQ_SET_CONFIGURATION;
  248. setup.index = 0;
  249. setup.length = 0;
  250. setup.value = config;
  251. if(rt_usb_hcd_control_xfer(uinst->hcd, uinst, &setup, RT_NULL, 0,
  252. timeout) != 0) return -RT_EIO;
  253. return RT_EOK;
  254. }
  255. /**
  256. * This function will set an interface to the usb device.
  257. *
  258. * @param uinst the usb device instance.
  259. * @param intf the interface number.
  260. *
  261. * @return the error code, RT_EOK on successfully.
  262. */
  263. rt_err_t rt_usb_set_interface(uinst_t uinst, int intf)
  264. {
  265. struct ureqest setup;
  266. int timeout = 100;
  267. /* check parameter */
  268. RT_ASSERT(uinst != RT_NULL);
  269. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
  270. USB_REQ_TYPE_INTERFACE;
  271. setup.request = USB_REQ_SET_INTERFACE;
  272. setup.index = 0;
  273. setup.length = 0;
  274. setup.value = intf;
  275. if(rt_usb_hcd_control_xfer(uinst->hcd, uinst, &setup, RT_NULL, 0,
  276. timeout) != 0) return -RT_EIO;
  277. return RT_EOK;
  278. }
  279. /**
  280. * This function will clear feature for the endpoint of the usb device.
  281. *
  282. * @param uinst the usb device instance.
  283. * @param endpoint the endpoint number of the usb device.
  284. *
  285. * @return the error code, RT_EOK on successfully.
  286. */
  287. rt_err_t rt_usb_clear_feature(uinst_t uinst, int endpoint, int feature)
  288. {
  289. struct ureqest setup;
  290. int timeout = 100;
  291. /* check parameter */
  292. RT_ASSERT(uinst != RT_NULL);
  293. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
  294. USB_REQ_TYPE_ENDPOINT;
  295. setup.request = USB_REQ_CLEAR_FEATURE;
  296. setup.index = endpoint;
  297. setup.length = 0;
  298. setup.value = feature;
  299. if(rt_usb_hcd_control_xfer(uinst->hcd, uinst, &setup, RT_NULL, 0,
  300. timeout) != 0) return -RT_EIO;
  301. return RT_EOK;
  302. }
  303. /**
  304. * This function will get an interface descriptor from the configuration descriptor.
  305. *
  306. * @param cfg_desc the point of configuration descriptor structure.
  307. * @param num the number of interface descriptor.
  308. * @intf_desc the point of interface descriptor point.
  309. *
  310. * @return the error code, RT_EOK on successfully.
  311. */
  312. rt_err_t rt_usb_get_interface_descriptor(ucfg_desc_t cfg_desc, int num,
  313. uintf_desc_t* intf_desc)
  314. {
  315. rt_uint32_t ptr, depth = 0;
  316. udesc_t desc;
  317. /* check parameter */
  318. RT_ASSERT(cfg_desc != RT_NULL);
  319. ptr = (rt_uint32_t)cfg_desc + cfg_desc->bLength;
  320. while(ptr < (rt_uint32_t)cfg_desc + cfg_desc->wTotalLength)
  321. {
  322. if(depth++ > 0x20)
  323. {
  324. *intf_desc = RT_NULL;
  325. return -RT_EIO;
  326. }
  327. desc = (udesc_t)ptr;
  328. if(desc->type == USB_DESC_TYPE_INTERFACE)
  329. {
  330. if(((uintf_desc_t)desc)->bInterfaceNumber == num)
  331. {
  332. *intf_desc = (uintf_desc_t)desc;
  333. RT_DEBUG_LOG(RT_DEBUG_USB,
  334. ("rt_usb_get_interface_descriptor: %d\n", num));
  335. return RT_EOK;
  336. }
  337. }
  338. ptr = (rt_uint32_t)desc + desc->bLength;
  339. }
  340. rt_kprintf("rt_usb_get_interface_descriptor %d failed\n", num);
  341. return -RT_EIO;
  342. }
  343. /**
  344. * This function will get an endpoint descriptor from the interface descriptor.
  345. *
  346. * @param intf_desc the point of interface descriptor structure.
  347. * @param num the number of endpoint descriptor.
  348. * @param ep_desc the point of endpoint descriptor point.
  349. *
  350. * @return the error code, RT_EOK on successfully.
  351. */
  352. rt_err_t rt_usb_get_endpoint_descriptor(uintf_desc_t intf_desc, int num,
  353. uep_desc_t* ep_desc)
  354. {
  355. int count = 0, depth = 0;
  356. rt_uint32_t ptr;
  357. udesc_t desc;
  358. /* check parameter */
  359. RT_ASSERT(intf_desc != RT_NULL);
  360. RT_ASSERT(num < intf_desc->bNumEndpoints);
  361. ptr = (rt_uint32_t)intf_desc + intf_desc->bLength;
  362. while(count < intf_desc->bNumEndpoints)
  363. {
  364. if(depth++ > 0x20)
  365. {
  366. *ep_desc = RT_NULL;
  367. return -RT_EIO;
  368. }
  369. desc = (udesc_t)ptr;
  370. if(desc->type == USB_DESC_TYPE_ENDPOINT)
  371. {
  372. if(num == count)
  373. {
  374. *ep_desc = (uep_desc_t)desc;
  375. RT_DEBUG_LOG(RT_DEBUG_USB,
  376. ("rt_usb_get_endpoint_descriptor: %d\n", num));
  377. return RT_EOK;
  378. }
  379. else count++;
  380. }
  381. ptr = (rt_uint32_t)desc + desc->bLength;
  382. }
  383. rt_kprintf("rt_usb_get_endpoint_descriptor %d failed\n", num);
  384. return -RT_EIO;
  385. }