hub.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /*
  2. * File : hub.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. #define USB_THREAD_STACK_SIZE 2048
  27. static struct rt_messagequeue *usb_mq;
  28. static struct uclass_driver hub_driver;
  29. /**
  30. * This function will do USB_REQ_GET_DESCRIPTOR request for the device instance
  31. * to get usb hub descriptor.
  32. *
  33. * @param intf the interface instance.
  34. * @buffer the data buffer to save usb hub descriptor.
  35. * @param nbytes the size of buffer
  36. *
  37. * @return the error code, RT_EOK on successfully.
  38. */
  39. rt_err_t rt_usbh_hub_get_descriptor(struct uinstance* device, rt_uint8_t *buffer,
  40. rt_size_t nbytes)
  41. {
  42. struct urequest setup;
  43. int timeout = 100;
  44. /* parameter check */
  45. RT_ASSERT(device != RT_NULL);
  46. setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS |
  47. USB_REQ_TYPE_DEVICE;
  48. setup.request = USB_REQ_GET_DESCRIPTOR;
  49. setup.index = 0;
  50. setup.length = nbytes;
  51. setup.value = USB_DESC_TYPE_HUB << 8;
  52. if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, buffer, nbytes,
  53. timeout) == nbytes) return RT_EOK;
  54. else return -RT_FALSE;
  55. }
  56. /**
  57. * This function will do USB_REQ_GET_STATUS request for the device instance
  58. * to get usb hub status.
  59. *
  60. * @param intf the interface instance.
  61. * @buffer the data buffer to save usb hub status.
  62. *
  63. * @return the error code, RT_EOK on successfully.
  64. */
  65. rt_err_t rt_usbh_hub_get_status(struct uinstance* device, rt_uint8_t* buffer)
  66. {
  67. struct urequest setup;
  68. int timeout = 100;
  69. int length = 4;
  70. /* parameter check */
  71. RT_ASSERT(device != RT_NULL);
  72. setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS |
  73. USB_REQ_TYPE_DEVICE;
  74. setup.request = USB_REQ_GET_STATUS;
  75. setup.index = 0;
  76. setup.length = length;
  77. setup.value = 0;
  78. if(rt_usb_hcd_control_xfer(device->hcd, device, &setup, buffer, length,
  79. timeout) == length) return RT_EOK;
  80. else return -RT_FALSE;
  81. }
  82. /**
  83. * This function will do USB_REQ_GET_STATUS request for the device instance
  84. * to get hub port status.
  85. *
  86. * @param intf the interface instance.
  87. * @port the hub port to get status.
  88. * @buffer the data buffer to save usb hub status.
  89. *
  90. * @return the error code, RT_EOK on successfully.
  91. */
  92. rt_err_t rt_usbh_hub_get_port_status(uhub_t hub, rt_uint16_t port,
  93. rt_uint8_t* buffer)
  94. {
  95. struct urequest setup;
  96. int timeout = 100;
  97. int length = 4;
  98. /* parameter check */
  99. RT_ASSERT(hub != RT_NULL);
  100. /* get roothub port status */
  101. if(hub->is_roothub)
  102. {
  103. rt_usb_hcd_hub_control(hub->hcd, port, RH_GET_PORT_STATUS,
  104. (void*)buffer);
  105. return RT_EOK;
  106. }
  107. setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS |
  108. USB_REQ_TYPE_OTHER;
  109. setup.request = USB_REQ_GET_STATUS;
  110. setup.index = port;
  111. setup.length = 4;
  112. setup.value = 0;
  113. if(rt_usb_hcd_control_xfer(hub->hcd, hub->self, &setup, buffer,
  114. length, timeout) == timeout) return RT_EOK;
  115. else return -RT_FALSE;
  116. }
  117. /**
  118. * This function will do USB_REQ_CLEAR_FEATURE request for the device instance
  119. * to clear feature of the hub port.
  120. *
  121. * @param intf the interface instance.
  122. * @port the hub port.
  123. * @feature feature to be cleared.
  124. *
  125. * @return the error code, RT_EOK on successfully.
  126. */
  127. rt_err_t rt_usbh_hub_clear_port_feature(uhub_t hub, rt_uint16_t port,
  128. rt_uint16_t feature)
  129. {
  130. struct urequest setup;
  131. int timeout = 100;
  132. /* parameter check */
  133. RT_ASSERT(hub != RT_NULL);
  134. /* clear roothub feature */
  135. if(hub->is_roothub)
  136. {
  137. rt_usb_hcd_hub_control(hub->hcd, port, RH_CLEAR_PORT_FEATURE,
  138. (void*)feature);
  139. return RT_EOK;
  140. }
  141. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
  142. USB_REQ_TYPE_OTHER;
  143. setup.request = USB_REQ_CLEAR_FEATURE;
  144. setup.index = port;
  145. setup.length = 0;
  146. setup.value = feature;
  147. if(rt_usb_hcd_control_xfer(hub->hcd, hub->self, &setup, RT_NULL, 0,
  148. timeout) == 0) return RT_EOK;
  149. else return -RT_FALSE;
  150. }
  151. /**
  152. * This function will do USB_REQ_SET_FEATURE request for the device instance
  153. * to set feature of the hub port.
  154. *
  155. * @param intf the interface instance.
  156. * @port the hub port.
  157. * @feature feature to be set.
  158. *
  159. * @return the error code, RT_EOK on successfully.
  160. */
  161. rt_err_t rt_usbh_hub_set_port_feature(uhub_t hub, rt_uint16_t port,
  162. rt_uint16_t feature)
  163. {
  164. struct urequest setup;
  165. int timeout = 100;
  166. /* parameter check */
  167. RT_ASSERT(hub != RT_NULL);
  168. /* clear roothub feature */
  169. if(hub->is_roothub)
  170. {
  171. rt_usb_hcd_hub_control(hub->hcd, port, RH_SET_PORT_FEATURE,
  172. (void*)feature);
  173. return RT_EOK;
  174. }
  175. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
  176. USB_REQ_TYPE_OTHER;
  177. setup.request = USB_REQ_SET_FEATURE;
  178. setup.index = port;
  179. setup.length = 0;
  180. setup.value = feature;
  181. if(rt_usb_hcd_control_xfer(hub->hcd, hub->self, &setup, RT_NULL, 0,
  182. timeout) == 0) return RT_EOK;
  183. else return -RT_FALSE;
  184. }
  185. /**
  186. * This function will rest hub port, it is invoked when sub device attached to the hub port.
  187. *
  188. * @param intf the interface instance.
  189. * @param port the hub port.
  190. *
  191. * @return the error code, RT_EOK on successfully.
  192. */
  193. rt_err_t rt_usbh_hub_reset_port(uhub_t hub, rt_uint16_t port)
  194. {
  195. rt_err_t ret;
  196. rt_uint32_t pstatus;
  197. /* parameter check */
  198. RT_ASSERT(hub != RT_NULL);
  199. rt_thread_delay(50);
  200. /* reset hub port */
  201. ret = rt_usbh_hub_set_port_feature(hub, port, PORT_FEAT_RESET);
  202. if(ret != RT_EOK) return ret;
  203. while(1)
  204. {
  205. ret = rt_usbh_hub_get_port_status(hub, port, (rt_uint8_t*)&pstatus);
  206. if(!(pstatus & PORT_PRS)) break;
  207. }
  208. /* clear port reset feature */
  209. ret = rt_usbh_hub_clear_port_feature(hub, port, PORT_FEAT_C_RESET);
  210. if(ret != RT_EOK) return ret;
  211. rt_thread_delay(50);
  212. return RT_EOK;
  213. }
  214. /**
  215. * This function will do debouce, it is invoked when sub device attached to the hub port.
  216. *
  217. * @param device the usb instance.
  218. * @param port the hub port.
  219. *
  220. * @return the error code, RT_EOK on successfully.
  221. */
  222. rt_err_t rt_usbh_hub_port_debounce(uhub_t hub, rt_uint16_t port)
  223. {
  224. rt_err_t ret;
  225. int i = 0, times = 20;
  226. rt_uint32_t pstatus;
  227. rt_bool_t connect = RT_TRUE;
  228. /* parameter check */
  229. RT_ASSERT(hub != RT_NULL);
  230. for(i=0; i<times; i++)
  231. {
  232. ret = rt_usbh_hub_get_port_status(hub, port, (rt_uint8_t*)&pstatus);
  233. if(ret != RT_EOK) return ret;
  234. if(!(pstatus & PORT_CCS))
  235. {
  236. connect = RT_FALSE;
  237. break;
  238. }
  239. rt_thread_delay(1);
  240. }
  241. if(connect) return RT_EOK;
  242. else return -RT_ERROR;
  243. }
  244. /**
  245. * This function will poll all the hub ports to detect port status, especially connect and
  246. * disconnect events.
  247. *
  248. * @param intf the interface instance.
  249. *
  250. * @return the error code, RT_EOK on successfully.
  251. */
  252. static rt_err_t rt_usbh_hub_port_change(uhub_t hub)
  253. {
  254. int i;
  255. rt_bool_t reconnect;
  256. /* parameter check */
  257. RT_ASSERT(hub != RT_NULL);
  258. /* get usb device instance */
  259. for (i = 0; i < hub->num_ports; i++)
  260. {
  261. rt_err_t ret;
  262. struct uinstance* device;
  263. rt_uint32_t pstatus = 0;
  264. reconnect = RT_FALSE;
  265. /* get hub port status */
  266. ret = rt_usbh_hub_get_port_status(hub, i + 1, (rt_uint8_t*)&pstatus);
  267. if(ret != RT_EOK) continue;
  268. RT_DEBUG_LOG(RT_DEBUG_USB, ("port %d status 0x%x\n", i, pstatus));
  269. /* check port status change */
  270. if ((pstatus & PORT_CCSC))
  271. {
  272. /* clear port status change feature */
  273. rt_usbh_hub_clear_port_feature(hub, i + 1, PORT_FEAT_C_CONNECTION);
  274. reconnect = RT_TRUE;
  275. }
  276. if(pstatus & PORT_PESC)
  277. {
  278. rt_usbh_hub_clear_port_feature(hub, i + 1, PORT_FEAT_C_ENABLE);
  279. reconnect = RT_TRUE;
  280. }
  281. if(reconnect)
  282. {
  283. if(hub->child[i]->status != DEV_STATUS_IDLE)
  284. rt_usbh_detach_instance(hub->child[i]);
  285. ret = rt_usbh_hub_port_debounce(hub, i + 1);
  286. if(ret != RT_EOK) continue;
  287. /* allocate an usb instance for new connected device */
  288. device = rt_usbh_alloc_instance();
  289. if(device == RT_NULL) break;
  290. /* set usb device speed */
  291. device->speed = (pstatus & PORT_LSDA) ? 1 : 0;
  292. device->parent = hub;
  293. device->hcd = hub->hcd;
  294. hub->child[i] = device;
  295. /* reset usb roothub port */
  296. rt_usbh_hub_reset_port(hub, i + 1);
  297. /* attatch the usb instance to the hcd */
  298. rt_usbh_attatch_instance(device);
  299. }
  300. }
  301. return RT_EOK;
  302. }
  303. /**
  304. * This function is the callback function of hub's int endpoint, it is invoked when data comes.
  305. *
  306. * @param context the context of the callback function.
  307. *
  308. * @return none.
  309. */
  310. static void rt_usbh_hub_irq(void* context)
  311. {
  312. upipe_t pipe;
  313. struct uintf* intf;
  314. uhub_t hub;
  315. int timeout = 100;
  316. RT_ASSERT(context != RT_NULL);
  317. pipe = (upipe_t)context;
  318. intf = pipe->intf;
  319. hub = (uhub_t)intf->user_data;
  320. if(pipe->status != UPIPE_STATUS_OK)
  321. {
  322. rt_kprintf("hub irq error\n");
  323. return;
  324. }
  325. rt_usbh_hub_port_change(hub);
  326. rt_kprintf("hub int xfer...\n");
  327. /* parameter check */
  328. RT_ASSERT(pipe->intf->device->hcd != RT_NULL);
  329. rt_usb_hcd_int_xfer(intf->device->hcd, pipe, hub->buffer,
  330. pipe->ep.wMaxPacketSize, timeout);
  331. }
  332. /**
  333. * This function will run usb hub class driver when usb hub is detected and identified
  334. * as a hub class device, it will continue to do the enumulate process.
  335. *
  336. * @param arg the argument.
  337. *
  338. * @return the error code, RT_EOK on successfully.
  339. */
  340. static rt_err_t rt_usbh_hub_enable(void *arg)
  341. {
  342. int i = 0;
  343. rt_err_t ret = RT_EOK;
  344. uep_desc_t ep_desc;
  345. uhub_t hub;
  346. struct uinstance* device;
  347. struct uintf* intf = (struct uintf*)arg;
  348. int timeout = 300;
  349. /* paremeter check */
  350. RT_ASSERT(intf != RT_NULL);
  351. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_hub_run\n"));
  352. /* get usb device instance */
  353. device = intf->device;
  354. /* create a hub instance */
  355. hub = rt_malloc(sizeof(struct uhub));
  356. rt_memset(hub, 0, sizeof(struct uhub));
  357. /* make interface instance's user data point to hub instance */
  358. intf->user_data = (void*)hub;
  359. /* get hub descriptor head */
  360. ret = rt_usbh_hub_get_descriptor(device, (rt_uint8_t*)&hub->hub_desc, 8);
  361. if(ret != RT_EOK)
  362. {
  363. rt_kprintf("get hub descriptor failed\n");
  364. return -RT_ERROR;
  365. }
  366. /* get full hub descriptor */
  367. ret = rt_usbh_hub_get_descriptor(device, (rt_uint8_t*)&hub->hub_desc,
  368. hub->hub_desc.length);
  369. if(ret != RT_EOK)
  370. {
  371. rt_kprintf("get hub descriptor again failed\n");
  372. return -RT_ERROR;
  373. }
  374. /* get hub ports number */
  375. hub->num_ports = hub->hub_desc.num_ports;
  376. hub->hcd = device->hcd;
  377. hub->self = device;
  378. /* reset all hub ports */
  379. for (i = 0; i < hub->num_ports; i++)
  380. {
  381. rt_usbh_hub_set_port_feature(hub, i + 1, PORT_FEAT_POWER);
  382. rt_thread_delay(hub->hub_desc.pwron_to_good
  383. * 2 * RT_TICK_PER_SECOND / 1000 );
  384. }
  385. if(intf->intf_desc->bNumEndpoints != 1)
  386. return -RT_ERROR;
  387. /* get endpoint descriptor from interface descriptor */
  388. rt_usbh_get_endpoint_descriptor(intf->intf_desc, 0, &ep_desc);
  389. if(ep_desc == RT_NULL)
  390. {
  391. rt_kprintf("rt_usb_get_endpoint_descriptor error\n");
  392. return -RT_ERROR;
  393. }
  394. /* the endpoint type of hub class should be interrupt */
  395. if( USB_EP_ATTR(ep_desc->bmAttributes) == USB_EP_ATTR_INT)
  396. {
  397. /* the endpoint direction of hub class should be in */
  398. if(ep_desc->bEndpointAddress & USB_DIR_IN)
  399. {
  400. /* allocate a pipe according to the endpoint type */
  401. rt_usb_hcd_alloc_pipe(device->hcd, &hub->pipe_in, intf,
  402. ep_desc, rt_usbh_hub_irq);
  403. }
  404. else return -RT_ERROR;
  405. }
  406. /* parameter check */
  407. RT_ASSERT(device->hcd != RT_NULL);
  408. rt_usb_hcd_int_xfer(device->hcd, hub->pipe_in, hub->buffer,
  409. hub->pipe_in->ep.wMaxPacketSize, timeout);
  410. return RT_EOK;
  411. }
  412. /**
  413. * This function will be invoked when usb hub plug out is detected and it would clean
  414. * and release all hub class related resources.
  415. *
  416. * @param arg the argument.
  417. *
  418. * @return the error code, RT_EOK on successfully.
  419. */
  420. static rt_err_t rt_usbh_hub_disable(void* arg)
  421. {
  422. int i;
  423. uhub_t hub;
  424. struct uinstance* device;
  425. struct uintf* intf = (struct uintf*)arg;
  426. /* paremeter check */
  427. RT_ASSERT(intf != RT_NULL);
  428. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_hub_stop\n"));
  429. device = intf->device;
  430. hub = (uhub_t)intf->user_data;
  431. if(hub->pipe_in != RT_NULL)
  432. rt_usb_hcd_free_pipe(device->hcd, hub->pipe_in);
  433. for(i=0; i<hub->num_ports; i++)
  434. {
  435. if(hub->child[i] != RT_NULL)
  436. rt_usbh_detach_instance(hub->child[i]);
  437. }
  438. if(hub != RT_NULL) rt_free(hub);
  439. if(intf != RT_NULL) rt_free(intf);
  440. return RT_EOK;
  441. }
  442. /**
  443. * This function will register hub class driver to the usb class driver manager.
  444. * and it should be invoked in the usb system initialization.
  445. *
  446. * @return the error code, RT_EOK on successfully.
  447. */
  448. ucd_t rt_usbh_class_driver_hub(void)
  449. {
  450. hub_driver.class_code = USB_CLASS_HUB;
  451. hub_driver.enable = rt_usbh_hub_enable;
  452. hub_driver.disable = rt_usbh_hub_disable;
  453. return &hub_driver;
  454. }
  455. /**
  456. * This function is the main entry of usb hub thread, it is in charge of
  457. * processing all messages received from the usb message buffer.
  458. *
  459. * @param parameter the parameter of the usb host thread.
  460. *
  461. * @return none.
  462. */
  463. static void rt_usbh_hub_thread_entry(void* parameter)
  464. {
  465. while(1)
  466. {
  467. struct uhost_msg msg;
  468. /* receive message */
  469. if(rt_mq_recv(usb_mq, &msg, sizeof(struct uhost_msg), RT_WAITING_FOREVER)
  470. != RT_EOK ) continue;
  471. RT_DEBUG_LOG(RT_DEBUG_USB, ("msg type %d\n", msg.type));
  472. switch (msg.type)
  473. {
  474. case USB_MSG_CONNECT_CHANGE:
  475. rt_usbh_hub_port_change(msg.content.hub);
  476. break;
  477. case USB_MSG_CALLBACK:
  478. /* invoke callback */
  479. msg.content.cb.function(msg.content.cb.context);
  480. break;
  481. default:
  482. break;
  483. }
  484. }
  485. }
  486. /**
  487. * This function will post an message to the usb message queue,
  488. *
  489. * @param msg the message to be posted
  490. *
  491. * @return the error code, RT_EOK on successfully.
  492. */
  493. rt_err_t rt_usbh_event_signal(struct uhost_msg* msg)
  494. {
  495. RT_ASSERT(msg != RT_NULL);
  496. /* send message to usb message queue */
  497. rt_mq_send(usb_mq, (void*)msg, sizeof(struct uhost_msg));
  498. return RT_EOK;
  499. }
  500. /**
  501. * This function will initialize usb hub thread.
  502. *
  503. * @return none.
  504. *
  505. */
  506. void rt_usbh_hub_init(void)
  507. {
  508. rt_thread_t thread;
  509. /* create usb message queue */
  510. usb_mq = rt_mq_create("usbh", 32, 16, RT_IPC_FLAG_FIFO);
  511. /* create usb hub thread */
  512. thread = rt_thread_create("usbh", rt_usbh_hub_thread_entry, RT_NULL,
  513. USB_THREAD_STACK_SIZE, 8, 20);
  514. if(thread != RT_NULL)
  515. {
  516. /* startup usb host thread */
  517. rt_thread_startup(thread);
  518. }
  519. }