core.c 13 KB

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