1
0

core.c 13 KB

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