core.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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(uhcd_t uhcd)
  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. rt_list_init(&dev[i].pipe);
  51. dev[i].hcd = uhcd;
  52. /* unlock scheduler */
  53. rt_exit_critical();
  54. return &dev[i];
  55. }
  56. /* unlock scheduler */
  57. rt_exit_critical();
  58. return RT_NULL;
  59. }
  60. /**
  61. * This function will attatch an usb device instance to a host controller,
  62. * and do device enumunation process.
  63. *
  64. * @param hcd the host controller driver.
  65. * @param device the usb device instance.
  66. *
  67. * @return the error code, RT_EOK on successfully.
  68. */
  69. static struct uendpoint_descriptor ep0_out_desc =
  70. {
  71. /*endpoint descriptor*/
  72. USB_DESC_LENGTH_ENDPOINT,
  73. USB_DESC_TYPE_ENDPOINT,
  74. 0x00 | USB_DIR_OUT,
  75. USB_EP_ATTR_CONTROL,
  76. 0x40,
  77. 0x00,
  78. };
  79. static struct uendpoint_descriptor ep0_in_desc =
  80. {
  81. /*endpoint descriptor*/
  82. USB_DESC_LENGTH_ENDPOINT,
  83. USB_DESC_TYPE_ENDPOINT,
  84. 0x00 | USB_DIR_IN,
  85. USB_EP_ATTR_CONTROL,
  86. 0x40,
  87. 0x00,
  88. };
  89. rt_err_t rt_usbh_attatch_instance(uinst_t device)
  90. {
  91. int i = 0;
  92. rt_err_t ret = RT_EOK;
  93. struct uconfig_descriptor cfg_desc;
  94. udev_desc_t dev_desc;
  95. uintf_desc_t intf_desc;
  96. uep_desc_t ep_desc;
  97. rt_uint8_t ep_index;
  98. upipe_t pipe;
  99. ucd_t drv;
  100. RT_ASSERT(device != RT_NULL);
  101. rt_memset(&cfg_desc, 0, sizeof(struct uconfig_descriptor));
  102. dev_desc = &device->dev_desc;
  103. /* alloc address 0 ep0 pipe*/
  104. rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_out, device, &ep0_out_desc);
  105. rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_in, device, &ep0_in_desc);
  106. RT_DEBUG_LOG(RT_DEBUG_USB, ("start enumnation\n"));
  107. /* get device descriptor head */
  108. ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_DEVICE, (void*)dev_desc, 8);
  109. if(ret != RT_EOK)
  110. {
  111. rt_kprintf("get device descriptor head failed\n");
  112. return ret;
  113. }
  114. //should reset bus
  115. rt_usbh_hub_reset_port(device->parent_hub, device->port);
  116. /* set device address */
  117. ret = rt_usbh_set_address(device);
  118. /* free address 0 ep0 pipe*/
  119. rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_out);
  120. rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_in);
  121. if(ret != RT_EOK)
  122. {
  123. rt_kprintf("set device address failed\n");
  124. return ret;
  125. }
  126. /* free true address ep0 pipe*/
  127. rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_out, device, &ep0_out_desc);
  128. rt_usb_hcd_alloc_pipe(device->hcd, &device->pipe_ep0_in, device, &ep0_in_desc);
  129. /* set device max packet size */
  130. device->max_packet_size = device->dev_desc.bMaxPacketSize0;
  131. RT_DEBUG_LOG(1, ("get device descriptor length %d\n",
  132. dev_desc->bLength));
  133. /* get full device descriptor again */
  134. ret = rt_usbh_get_descriptor
  135. (device, USB_DESC_TYPE_DEVICE, (void*)dev_desc, dev_desc->bLength);
  136. if(ret != RT_EOK)
  137. {
  138. rt_kprintf("get full device descriptor failed\n");
  139. return ret;
  140. }
  141. RT_DEBUG_LOG(1, ("Vendor ID 0x%x\n", dev_desc->idVendor));
  142. RT_DEBUG_LOG(1, ("Product ID 0x%x\n", dev_desc->idProduct));
  143. /* get configuration descriptor head */
  144. ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_CONFIGURATION, &cfg_desc, 18);
  145. if(ret != RT_EOK)
  146. {
  147. rt_kprintf("get configuration descriptor head failed\n");
  148. return ret;
  149. }
  150. /* alloc memory for configuration descriptor */
  151. device->cfg_desc = (ucfg_desc_t)rt_malloc(cfg_desc.wTotalLength);
  152. rt_memset(device->cfg_desc, 0, cfg_desc.wTotalLength);
  153. /* get full configuration descriptor */
  154. ret = rt_usbh_get_descriptor(device, USB_DESC_TYPE_CONFIGURATION,
  155. device->cfg_desc, cfg_desc.wTotalLength);
  156. if(ret != RT_EOK)
  157. {
  158. rt_kprintf("get full configuration descriptor failed\n");
  159. return ret;
  160. }
  161. /* set configuration */
  162. ret = rt_usbh_set_configure(device, 1);
  163. if(ret != RT_EOK)
  164. {
  165. return ret;
  166. }
  167. for(i=0; i<device->cfg_desc->bNumInterfaces; i++)
  168. {
  169. /* get interface descriptor through configuration descriptor */
  170. ret = rt_usbh_get_interface_descriptor(device->cfg_desc, i, &intf_desc);
  171. if(ret != RT_EOK)
  172. {
  173. rt_kprintf("rt_usb_get_interface_descriptor error\n");
  174. return -RT_ERROR;
  175. }
  176. RT_DEBUG_LOG(1, ("interface class 0x%x, subclass 0x%x\n",
  177. intf_desc->bInterfaceClass,
  178. intf_desc->bInterfaceSubClass));
  179. /* alloc pipe*/
  180. for(ep_index = 0; ep_index < intf_desc->bNumEndpoints; ep_index++)
  181. {
  182. rt_usbh_get_endpoint_descriptor(intf_desc, ep_index, &ep_desc);
  183. if(ep_desc != RT_NULL)
  184. {
  185. if(rt_usb_hcd_alloc_pipe(device->hcd, &pipe, device, ep_desc) != RT_EOK)
  186. {
  187. rt_kprintf("alloc pipe failed\n");
  188. return RT_ERROR;
  189. }
  190. rt_usb_instance_add_pipe(device,pipe);
  191. }
  192. else
  193. {
  194. rt_kprintf("get endpoint desc failed\n");
  195. return RT_ERROR;
  196. }
  197. }
  198. /* find driver by class code found in interface descriptor */
  199. drv = rt_usbh_class_driver_find(intf_desc->bInterfaceClass,
  200. intf_desc->bInterfaceSubClass);
  201. if(drv != RT_NULL)
  202. {
  203. /* allocate memory for interface device */
  204. device->intf[i] = (struct uhintf*)rt_malloc(sizeof(struct uhintf));
  205. device->intf[i]->drv = drv;
  206. device->intf[i]->device = device;
  207. device->intf[i]->intf_desc = intf_desc;
  208. device->intf[i]->user_data = RT_NULL;
  209. /* open usb class driver */
  210. ret = rt_usbh_class_driver_enable(drv, (void*)device->intf[i]);
  211. if(ret != RT_EOK)
  212. {
  213. rt_kprintf("interface %d run class driver error\n", i);
  214. }
  215. }
  216. else
  217. {
  218. rt_kprintf("find usb device driver failed\n");
  219. continue;
  220. }
  221. }
  222. return RT_EOK;
  223. }
  224. /**
  225. * This function will detach an usb device instance from its host controller,
  226. * and release all resource.
  227. *
  228. * @param device the usb device instance.
  229. *
  230. * @return the error code, RT_EOK on successfully.
  231. */
  232. rt_err_t rt_usbh_detach_instance(uinst_t device)
  233. {
  234. int i = 0;
  235. rt_list_t * l;
  236. if(device == RT_NULL)
  237. {
  238. rt_kprintf("no usb instance to detach\n");
  239. return -RT_ERROR;
  240. }
  241. /* free configration descriptor */
  242. for(i=0; i<device->cfg_desc->bNumInterfaces; i++)
  243. {
  244. if(device->intf[i] == RT_NULL) continue;
  245. if(device->intf[i]->drv == RT_NULL) continue;
  246. RT_ASSERT(device->intf[i]->device == device);
  247. RT_DEBUG_LOG(RT_DEBUG_USB, ("free interface instance %d\n", i));
  248. rt_usbh_class_driver_disable(device->intf[i]->drv, (void*)device->intf[i]);
  249. }
  250. if(device->cfg_desc) rt_free(device->cfg_desc);
  251. rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_out);
  252. rt_usb_hcd_free_pipe(device->hcd,device->pipe_ep0_in);
  253. for(l = device->pipe.next;l != &device->pipe;l = l->next)
  254. {
  255. rt_usb_hcd_free_pipe(device->hcd,rt_list_entry(l,struct upipe,list));
  256. }
  257. rt_memset(device, 0, sizeof(struct uinstance));
  258. return RT_EOK;
  259. }
  260. /**
  261. * This function will do USB_REQ_GET_DESCRIPTO' bRequest for the usb device instance,
  262. *
  263. * @param device the usb device instance.
  264. * @param type the type of descriptor bRequest.
  265. * @param buffer the data buffer to save requested data
  266. * @param nbytes the size of buffer
  267. *
  268. * @return the error code, RT_EOK on successfully.
  269. */
  270. rt_err_t rt_usbh_get_descriptor(uinst_t device, rt_uint8_t type, void* buffer,
  271. int nbytes)
  272. {
  273. struct urequest setup;
  274. int timeout = 100;
  275. RT_ASSERT(device != RT_NULL);
  276. setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_STANDARD |
  277. USB_REQ_TYPE_DEVICE;
  278. setup.bRequest = USB_REQ_GET_DESCRIPTOR;
  279. setup.wIndex = 0;
  280. setup.wLength = nbytes;
  281. setup.wValue = type << 8;
  282. if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
  283. {
  284. if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, buffer, nbytes, timeout) == nbytes)
  285. {
  286. if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_out, RT_NULL, 0, timeout) == 0)
  287. {
  288. return RT_EOK;
  289. }
  290. }
  291. }
  292. return RT_ERROR;
  293. }
  294. /**
  295. * This function will set an address to the usb device.
  296. *
  297. * @param device the usb device instance.
  298. *
  299. * @return the error code, RT_EOK on successfully.
  300. */
  301. rt_err_t rt_usbh_set_address(uinst_t device)
  302. {
  303. struct urequest setup;
  304. int timeout = 100;
  305. RT_ASSERT(device != RT_NULL);
  306. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usb_set_address\n"));
  307. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
  308. USB_REQ_TYPE_DEVICE;
  309. setup.bRequest = USB_REQ_SET_ADDRESS;
  310. setup.wIndex = 0;
  311. setup.wLength = 0;
  312. setup.wValue = device->index;
  313. if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
  314. {
  315. return RT_ERROR;
  316. }
  317. if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, RT_NULL, 0, timeout) == 0)
  318. {
  319. device->address = device->index;
  320. }
  321. return RT_EOK;
  322. }
  323. /**
  324. * This function will set a configuration to the usb device.
  325. *
  326. * @param device the usb device instance.
  327. * @param config the configuration number.
  328. *
  329. * @return the error code, RT_EOK on successfully.
  330. */
  331. rt_err_t rt_usbh_set_configure(uinst_t device, int config)
  332. {
  333. struct urequest setup;
  334. int timeout = 100;
  335. /* check parameter */
  336. RT_ASSERT(device != RT_NULL);
  337. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
  338. USB_REQ_TYPE_DEVICE;
  339. setup.bRequest = USB_REQ_SET_CONFIGURATION;
  340. setup.wIndex = 0;
  341. setup.wLength = 0;
  342. setup.wValue = config;
  343. if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
  344. {
  345. return RT_ERROR;
  346. }
  347. return RT_EOK;
  348. }
  349. /**
  350. * This function will set an interface to the usb device.
  351. *
  352. * @param device the usb device instance.
  353. * @param intf the interface number.
  354. *
  355. * @return the error code, RT_EOK on successfully.
  356. */
  357. rt_err_t rt_usbh_set_interface(uinst_t device, int intf)
  358. {
  359. struct urequest setup;
  360. int timeout = 100;
  361. /* check parameter */
  362. RT_ASSERT(device != RT_NULL);
  363. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
  364. USB_REQ_TYPE_INTERFACE;
  365. setup.bRequest = USB_REQ_SET_INTERFACE;
  366. setup.wIndex = 0;
  367. setup.wLength = 0;
  368. setup.wValue = intf;
  369. if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
  370. {
  371. return RT_ERROR;
  372. }
  373. return RT_EOK;
  374. }
  375. /**
  376. * This function will clear feature for the endpoint of the usb device.
  377. *
  378. * @param device the usb device instance.
  379. * @param endpoint the endpoint number of the usb device.
  380. *
  381. * @return the error code, RT_EOK on successfully.
  382. */
  383. rt_err_t rt_usbh_clear_feature(uinst_t device, int endpoint, int feature)
  384. {
  385. struct urequest setup;
  386. int timeout = 100;
  387. /* check parameter */
  388. RT_ASSERT(device != RT_NULL);
  389. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_STANDARD |
  390. USB_REQ_TYPE_ENDPOINT;
  391. setup.bRequest = USB_REQ_CLEAR_FEATURE;
  392. setup.wIndex = endpoint;
  393. setup.wLength = 0;
  394. setup.wValue = feature;
  395. if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) != 8)
  396. {
  397. return RT_ERROR;
  398. }
  399. return RT_EOK;
  400. }
  401. /**
  402. * This function will get an interface descriptor from the configuration descriptor.
  403. *
  404. * @param cfg_desc the point of configuration descriptor structure.
  405. * @param num the number of interface descriptor.
  406. * @intf_desc the point of interface descriptor point.
  407. *
  408. * @return the error code, RT_EOK on successfully.
  409. */
  410. rt_err_t rt_usbh_get_interface_descriptor(ucfg_desc_t cfg_desc, int num,
  411. uintf_desc_t* intf_desc)
  412. {
  413. rt_uint32_t ptr, depth = 0;
  414. udesc_t desc;
  415. /* check parameter */
  416. RT_ASSERT(cfg_desc != RT_NULL);
  417. ptr = (rt_uint32_t)cfg_desc + cfg_desc->bLength;
  418. while(ptr < (rt_uint32_t)cfg_desc + cfg_desc->wTotalLength)
  419. {
  420. if(depth++ > 0x20)
  421. {
  422. *intf_desc = RT_NULL;
  423. return -RT_EIO;
  424. }
  425. desc = (udesc_t)ptr;
  426. if(desc->type == USB_DESC_TYPE_INTERFACE)
  427. {
  428. if(((uintf_desc_t)desc)->bInterfaceNumber == num)
  429. {
  430. *intf_desc = (uintf_desc_t)desc;
  431. RT_DEBUG_LOG(RT_DEBUG_USB,
  432. ("rt_usb_get_interface_descriptor: %d\n", num));
  433. return RT_EOK;
  434. }
  435. }
  436. ptr = (rt_uint32_t)desc + desc->bLength;
  437. }
  438. rt_kprintf("rt_usb_get_interface_descriptor %d failed\n", num);
  439. return -RT_EIO;
  440. }
  441. /**
  442. * This function will get an endpoint descriptor from the interface descriptor.
  443. *
  444. * @param intf_desc the point of interface descriptor structure.
  445. * @param num the number of endpoint descriptor.
  446. * @param ep_desc the point of endpoint descriptor point.
  447. *
  448. * @return the error code, RT_EOK on successfully.
  449. */
  450. rt_err_t rt_usbh_get_endpoint_descriptor(uintf_desc_t intf_desc, int num,
  451. uep_desc_t* ep_desc)
  452. {
  453. int count = 0, depth = 0;
  454. rt_uint32_t ptr;
  455. udesc_t desc;
  456. /* check parameter */
  457. RT_ASSERT(intf_desc != RT_NULL);
  458. RT_ASSERT(num < intf_desc->bNumEndpoints);
  459. *ep_desc = RT_NULL;
  460. ptr = (rt_uint32_t)intf_desc + intf_desc->bLength;
  461. while(count < intf_desc->bNumEndpoints)
  462. {
  463. if(depth++ > 0x20)
  464. {
  465. *ep_desc = RT_NULL;
  466. return -RT_EIO;
  467. }
  468. desc = (udesc_t)ptr;
  469. if(desc->type == USB_DESC_TYPE_ENDPOINT)
  470. {
  471. if(num == count)
  472. {
  473. *ep_desc = (uep_desc_t)desc;
  474. RT_DEBUG_LOG(RT_DEBUG_USB,
  475. ("rt_usb_get_endpoint_descriptor: %d\n", num));
  476. return RT_EOK;
  477. }
  478. else count++;
  479. }
  480. ptr = (rt_uint32_t)desc + desc->bLength;
  481. }
  482. rt_kprintf("rt_usb_get_endpoint_descriptor %d failed\n", num);
  483. return -RT_EIO;
  484. }
  485. int rt_usb_hcd_pipe_xfer(uhcd_t hcd, upipe_t pipe, void* buffer, int nbytes, int timeout)
  486. {
  487. rt_size_t remain_size;
  488. rt_size_t send_size;
  489. remain_size = nbytes;
  490. do
  491. {
  492. send_size = (remain_size > pipe->ep.wMaxPacketSize) ? pipe->ep.wMaxPacketSize : remain_size;
  493. if(hcd->ops->pipe_xfer(pipe, USBH_PID_DATA, buffer, send_size, timeout) == send_size)
  494. {
  495. remain_size -= send_size;
  496. }
  497. else
  498. {
  499. return 0;
  500. }
  501. }while(remain_size > 0);
  502. return nbytes;
  503. }