core.c 13 KB

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