hub.c 15 KB

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