core.c 17 KB

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