hub.c 15 KB

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