hub.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2011-12-12 Yi Qiu first version
  9. */
  10. #include <rtthread.h>
  11. #include <drivers/usb_host.h>
  12. #define USB_THREAD_STACK_SIZE 4096
  13. static struct rt_messagequeue *usb_mq;
  14. static struct uclass_driver hub_driver;
  15. static struct uhub root_hub;
  16. static rt_err_t root_hub_ctrl(struct uhcd *hcd, rt_uint16_t port, rt_uint8_t cmd, void *args)
  17. {
  18. switch(cmd)
  19. {
  20. case RH_GET_PORT_STATUS:
  21. (*(rt_uint32_t *)args) = hcd->roothub->port_status[port-1];
  22. break;
  23. case RH_SET_PORT_STATUS:
  24. hcd->roothub->port_status[port-1] = (*(rt_uint32_t *)args);
  25. break;
  26. case RH_CLEAR_PORT_FEATURE:
  27. switch(((rt_uint32_t)args))
  28. {
  29. case PORT_FEAT_C_CONNECTION:
  30. hcd->roothub->port_status[port-1] &= ~PORT_CCSC;
  31. break;
  32. case PORT_FEAT_C_ENABLE:
  33. hcd->roothub->port_status[port-1] &= ~PORT_PESC;
  34. break;
  35. case PORT_FEAT_C_SUSPEND:
  36. hcd->roothub->port_status[port-1] &= ~PORT_PSSC;
  37. break;
  38. case PORT_FEAT_C_OVER_CURRENT:
  39. hcd->roothub->port_status[port-1] &= ~PORT_POCIC;
  40. break;
  41. case PORT_FEAT_C_RESET:
  42. hcd->roothub->port_status[port-1] &= ~PORT_PRSC;
  43. break;
  44. }
  45. break;
  46. case RH_SET_PORT_FEATURE:
  47. switch((rt_uint32_t)args)
  48. {
  49. case PORT_FEAT_CONNECTION:
  50. hcd->roothub->port_status[port-1] |= PORT_CCSC;
  51. break;
  52. case PORT_FEAT_ENABLE:
  53. hcd->roothub->port_status[port-1] |= PORT_PESC;
  54. break;
  55. case PORT_FEAT_SUSPEND:
  56. hcd->roothub->port_status[port-1] |= PORT_PSSC;
  57. break;
  58. case PORT_FEAT_OVER_CURRENT:
  59. hcd->roothub->port_status[port-1] |= PORT_POCIC;
  60. break;
  61. case PORT_FEAT_RESET:
  62. hcd->ops->reset_port(port);
  63. break;
  64. case PORT_FEAT_POWER:
  65. break;
  66. case PORT_FEAT_LOWSPEED:
  67. break;
  68. case PORT_FEAT_HIGHSPEED:
  69. break;
  70. }
  71. break;
  72. default:
  73. return RT_ERROR;
  74. }
  75. return RT_EOK;
  76. }
  77. void rt_usbh_root_hub_connect_handler(struct uhcd *hcd, rt_uint8_t port, rt_bool_t isHS)
  78. {
  79. struct uhost_msg msg;
  80. msg.type = USB_MSG_CONNECT_CHANGE;
  81. msg.content.hub = hcd->roothub;
  82. hcd->roothub->port_status[port - 1] |= PORT_CCS | PORT_CCSC;
  83. if(isHS)
  84. {
  85. hcd->roothub->port_status[port - 1] &= ~PORT_LSDA;
  86. }
  87. else
  88. {
  89. hcd->roothub->port_status[port - 1] |= PORT_LSDA;
  90. }
  91. rt_usbh_event_signal(&msg);
  92. }
  93. void rt_usbh_root_hub_disconnect_handler(struct uhcd *hcd, rt_uint8_t port)
  94. {
  95. struct uhost_msg msg;
  96. msg.type = USB_MSG_CONNECT_CHANGE;
  97. msg.content.hub = hcd->roothub;
  98. hcd->roothub->port_status[port - 1] |= PORT_CCSC;
  99. hcd->roothub->port_status[port - 1] &= ~PORT_CCS;
  100. rt_usbh_event_signal(&msg);
  101. }
  102. /**
  103. * This function will do USB_REQ_GET_DESCRIPTOR bRequest for the device instance
  104. * to get usb hub descriptor.
  105. *
  106. * @param intf the interface instance.
  107. * @buffer the data buffer to save usb hub descriptor.
  108. * @param nbytes the size of buffer
  109. *
  110. * @return the error code, RT_EOK on successfully.
  111. */
  112. rt_err_t rt_usbh_hub_get_descriptor(struct uinstance* device, rt_uint8_t *buffer, rt_size_t nbytes)
  113. {
  114. struct urequest setup;
  115. int timeout = USB_TIMEOUT_BASIC;
  116. /* parameter check */
  117. RT_ASSERT(device != RT_NULL);
  118. setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_DEVICE;
  119. setup.bRequest = USB_REQ_GET_DESCRIPTOR;
  120. setup.wIndex = 0;
  121. setup.wLength = nbytes;
  122. setup.wValue = USB_DESC_TYPE_HUB << 8;
  123. if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
  124. {
  125. if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, buffer, nbytes, timeout) == nbytes)
  126. {
  127. return RT_EOK;
  128. }
  129. }
  130. return -RT_FALSE;
  131. }
  132. /**
  133. * This function will do USB_REQ_GET_STATUS bRequest for the device instance
  134. * to get usb hub status.
  135. *
  136. * @param intf the interface instance.
  137. * @buffer the data buffer to save usb hub status.
  138. *
  139. * @return the error code, RT_EOK on successfully.
  140. */
  141. rt_err_t rt_usbh_hub_get_status(struct uinstance* device, rt_uint32_t* buffer)
  142. {
  143. struct urequest setup;
  144. int timeout = USB_TIMEOUT_BASIC;
  145. /* parameter check */
  146. RT_ASSERT(device != RT_NULL);
  147. setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_DEVICE;
  148. setup.bRequest = USB_REQ_GET_STATUS;
  149. setup.wIndex = 0;
  150. setup.wLength = 4;
  151. setup.wValue = 0;
  152. if(rt_usb_hcd_setup_xfer(device->hcd, device->pipe_ep0_out, &setup, timeout) == 8)
  153. {
  154. if(rt_usb_hcd_pipe_xfer(device->hcd, device->pipe_ep0_in, buffer, 4, timeout) == 4)
  155. {
  156. return RT_EOK;
  157. }
  158. }
  159. return -RT_FALSE;
  160. }
  161. /**
  162. * This function will do USB_REQ_GET_STATUS bRequest for the device instance
  163. * to get hub port status.
  164. *
  165. * @param intf the interface instance.
  166. * @port the hub port to get status.
  167. * @buffer the data buffer to save usb hub status.
  168. *
  169. * @return the error code, RT_EOK on successfully.
  170. */
  171. rt_err_t rt_usbh_hub_get_port_status(uhub_t hub, rt_uint16_t port, rt_uint32_t* buffer)
  172. {
  173. struct urequest setup;
  174. int timeout = USB_TIMEOUT_BASIC;
  175. /* parameter check */
  176. RT_ASSERT(hub != RT_NULL);
  177. /* get roothub port status */
  178. if(hub->is_roothub)
  179. {
  180. root_hub_ctrl(hub->hcd, port, RH_GET_PORT_STATUS,
  181. (void*)buffer);
  182. return RT_EOK;
  183. }
  184. setup.request_type = USB_REQ_TYPE_DIR_IN | USB_REQ_TYPE_CLASS | USB_REQ_TYPE_OTHER;
  185. setup.bRequest = USB_REQ_GET_STATUS;
  186. setup.wIndex = port;
  187. setup.wLength = 4;
  188. setup.wValue = 0;
  189. if(rt_usb_hcd_setup_xfer(hub->hcd, hub->self->pipe_ep0_out, &setup, timeout) == 8)
  190. {
  191. if(rt_usb_hcd_pipe_xfer(hub->hcd, hub->self->pipe_ep0_in, buffer, 4, timeout) == 4)
  192. {
  193. return RT_EOK;
  194. }
  195. }
  196. return -RT_FALSE;
  197. }
  198. /**
  199. * This function will do USB_REQ_CLEAR_FEATURE bRequest for the device instance
  200. * to clear feature of the hub port.
  201. *
  202. * @param intf the interface instance.
  203. * @port the hub port.
  204. * @feature feature to be cleared.
  205. *
  206. * @return the error code, RT_EOK on successfully.
  207. */
  208. rt_err_t rt_usbh_hub_clear_port_feature(uhub_t hub, rt_uint16_t port, rt_uint16_t feature)
  209. {
  210. struct urequest setup;
  211. int timeout = USB_TIMEOUT_BASIC;
  212. /* parameter check */
  213. RT_ASSERT(hub != RT_NULL);
  214. /* clear roothub feature */
  215. if(hub->is_roothub)
  216. {
  217. root_hub_ctrl(hub->hcd, port, RH_CLEAR_PORT_FEATURE,
  218. (void*)(rt_uint32_t)feature);
  219. return RT_EOK;
  220. }
  221. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
  222. USB_REQ_TYPE_OTHER;
  223. setup.bRequest = USB_REQ_CLEAR_FEATURE;
  224. setup.wIndex = port;
  225. setup.wLength = 0;
  226. setup.wValue = feature;
  227. if(rt_usb_hcd_setup_xfer(hub->hcd, hub->self->pipe_ep0_out, &setup, timeout) == 8)
  228. {
  229. return RT_EOK;
  230. }
  231. return -RT_FALSE;
  232. }
  233. /**
  234. * This function will do USB_REQ_SET_FEATURE bRequest for the device instance
  235. * to set feature of the hub port.
  236. *
  237. * @param intf the interface instance.
  238. * @port the hub port.
  239. * @feature feature to be set.
  240. *
  241. * @return the error code, RT_EOK on successfully.
  242. */
  243. rt_err_t rt_usbh_hub_set_port_feature(uhub_t hub, rt_uint16_t port,
  244. rt_uint16_t feature)
  245. {
  246. struct urequest setup;
  247. int timeout = USB_TIMEOUT_BASIC;
  248. /* parameter check */
  249. RT_ASSERT(hub != RT_NULL);
  250. /* clear roothub feature */
  251. if(hub->is_roothub)
  252. {
  253. root_hub_ctrl(hub->hcd, port, RH_SET_PORT_FEATURE,
  254. (void*)(rt_uint32_t)feature);
  255. return RT_EOK;
  256. }
  257. setup.request_type = USB_REQ_TYPE_DIR_OUT | USB_REQ_TYPE_CLASS |
  258. USB_REQ_TYPE_OTHER;
  259. setup.bRequest = USB_REQ_SET_FEATURE;
  260. setup.wIndex = port;
  261. setup.wLength = 0;
  262. setup.wValue = feature;
  263. if(rt_usb_hcd_setup_xfer(hub->hcd, hub->self->pipe_ep0_out, &setup, timeout) == 8)
  264. {
  265. return RT_EOK;
  266. }
  267. else return -RT_FALSE;
  268. }
  269. /**
  270. * This function will rest hub port, it is invoked when sub device attached to the hub port.
  271. *
  272. * @param intf the interface instance.
  273. * @param port the hub port.
  274. *
  275. * @return the error code, RT_EOK on successfully.
  276. */
  277. rt_err_t rt_usbh_hub_reset_port(uhub_t hub, rt_uint16_t port)
  278. {
  279. rt_err_t ret;
  280. rt_uint32_t pstatus;
  281. /* parameter check */
  282. RT_ASSERT(hub != RT_NULL);
  283. rt_thread_delay(50);
  284. /* reset hub port */
  285. ret = rt_usbh_hub_set_port_feature(hub, port, PORT_FEAT_RESET);
  286. if(ret != RT_EOK) return ret;
  287. while(1)
  288. {
  289. ret = rt_usbh_hub_get_port_status(hub, port, &pstatus);
  290. if(!(pstatus & PORT_PRS)) break;
  291. }
  292. /* clear port reset feature */
  293. ret = rt_usbh_hub_clear_port_feature(hub, port, PORT_FEAT_C_RESET);
  294. if(ret != RT_EOK) return ret;
  295. rt_thread_delay(50);
  296. return RT_EOK;
  297. }
  298. /**
  299. * This function will do debouce, it is invoked when sub device attached to the hub port.
  300. *
  301. * @param device the usb instance.
  302. * @param port the hub port.
  303. *
  304. * @return the error code, RT_EOK on successfully.
  305. */
  306. rt_err_t rt_usbh_hub_port_debounce(uhub_t hub, rt_uint16_t port)
  307. {
  308. rt_err_t ret;
  309. int i = 0, times = 20;
  310. rt_uint32_t pstatus;
  311. rt_bool_t connect = RT_TRUE;
  312. int delayticks = USB_DEBOUNCE_TIME / times;
  313. if (delayticks < 1)
  314. delayticks = 1;
  315. /* parameter check */
  316. RT_ASSERT(hub != RT_NULL);
  317. for(i=0; i<times; i++)
  318. {
  319. ret = rt_usbh_hub_get_port_status(hub, port, &pstatus);
  320. if(ret != RT_EOK) return ret;
  321. if(!(pstatus & PORT_CCS))
  322. {
  323. connect = RT_FALSE;
  324. break;
  325. }
  326. rt_thread_delay(delayticks);
  327. }
  328. if(connect) return RT_EOK;
  329. else return -RT_ERROR;
  330. }
  331. /**
  332. * This function will poll all the hub ports to detect port status, especially connect and
  333. * disconnect events.
  334. *
  335. * @param intf the interface instance.
  336. *
  337. * @return the error code, RT_EOK on successfully.
  338. */
  339. static rt_err_t rt_usbh_hub_port_change(uhub_t hub)
  340. {
  341. int i;
  342. rt_bool_t reconnect;
  343. /* parameter check */
  344. RT_ASSERT(hub != RT_NULL);
  345. /* get usb device instance */
  346. for (i = 0; i < hub->num_ports; i++)
  347. {
  348. rt_err_t ret;
  349. struct uinstance* device;
  350. rt_uint32_t pstatus = 0;
  351. reconnect = RT_FALSE;
  352. /* get hub port status */
  353. ret = rt_usbh_hub_get_port_status(hub, i + 1, &pstatus);
  354. if(ret != RT_EOK) continue;
  355. RT_DEBUG_LOG(RT_DEBUG_USB, ("port %d status 0x%x\n", i + 1, pstatus));
  356. /* check port status change */
  357. if (pstatus & PORT_CCSC)
  358. {
  359. /* clear port status change feature */
  360. rt_usbh_hub_clear_port_feature(hub, i + 1, PORT_FEAT_C_CONNECTION);
  361. reconnect = RT_TRUE;
  362. }
  363. if(pstatus & PORT_PESC)
  364. {
  365. rt_usbh_hub_clear_port_feature(hub, i + 1, PORT_FEAT_C_ENABLE);
  366. reconnect = RT_TRUE;
  367. }
  368. if(reconnect)
  369. {
  370. if(hub->child[i] != RT_NULL && hub->child[i]->status != DEV_STATUS_IDLE)
  371. rt_usbh_detach_instance(hub->child[i]);
  372. ret = rt_usbh_hub_port_debounce(hub, i + 1);
  373. if(ret != RT_EOK) continue;
  374. /* allocate an usb instance for new connected device */
  375. device = rt_usbh_alloc_instance(hub->hcd);
  376. if(device == RT_NULL) break;
  377. /* set usb device speed */
  378. device->speed = (pstatus & PORT_LSDA) ? 1 : 0;
  379. device->parent_hub = hub;
  380. device->hcd = hub->hcd;
  381. device->port = i + 1;
  382. hub->child[i] = device;
  383. /* reset usb roothub port */
  384. rt_usbh_hub_reset_port(hub, i + 1);
  385. /* attatch the usb instance to the hcd */
  386. rt_usbh_attatch_instance(device);
  387. }
  388. }
  389. return RT_EOK;
  390. }
  391. /**
  392. * This function is the callback function of hub's int endpoint, it is invoked when data comes.
  393. *
  394. * @param context the context of the callback function.
  395. *
  396. * @return none.
  397. */
  398. static void rt_usbh_hub_irq(void* context)
  399. {
  400. upipe_t pipe;
  401. uhub_t hub;
  402. int timeout = USB_TIMEOUT_BASIC;
  403. RT_ASSERT(context != RT_NULL);
  404. pipe = (upipe_t)context;
  405. hub = (uhub_t)pipe->user_data;
  406. if(pipe->status != UPIPE_STATUS_OK)
  407. {
  408. RT_DEBUG_LOG(RT_DEBUG_USB,("hub irq error\n"));
  409. return;
  410. }
  411. rt_usbh_hub_port_change(hub);
  412. RT_DEBUG_LOG(RT_DEBUG_USB,("hub int xfer...\n"));
  413. /* parameter check */
  414. RT_ASSERT(pipe->inst->hcd != RT_NULL);
  415. rt_usb_hcd_pipe_xfer(hub->self->hcd, pipe, hub->buffer, pipe->ep.wMaxPacketSize, timeout);
  416. }
  417. /**
  418. * This function will run usb hub class driver when usb hub is detected and identified
  419. * as a hub class device, it will continue to do the enumulate process.
  420. *
  421. * @param arg the argument.
  422. *
  423. * @return the error code, RT_EOK on successfully.
  424. */
  425. static rt_err_t rt_usbh_hub_enable(void *arg)
  426. {
  427. int i = 0;
  428. rt_err_t ret = RT_EOK;
  429. uep_desc_t ep_desc = RT_NULL;
  430. uhub_t hub;
  431. struct uinstance* device;
  432. struct uhintf* intf = (struct uhintf*)arg;
  433. upipe_t pipe_in = RT_NULL;
  434. int timeout = USB_TIMEOUT_LONG;
  435. /* paremeter check */
  436. RT_ASSERT(intf != RT_NULL);
  437. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_hub_run\n"));
  438. /* get usb device instance */
  439. device = intf->device;
  440. /* create a hub instance */
  441. hub = rt_malloc(sizeof(struct uhub));
  442. RT_ASSERT(hub != RT_NULL);
  443. rt_memset(hub, 0, sizeof(struct uhub));
  444. /* make interface instance's user data point to hub instance */
  445. intf->user_data = (void*)hub;
  446. /* get hub descriptor head */
  447. ret = rt_usbh_hub_get_descriptor(device, (rt_uint8_t*)&hub->hub_desc, 8);
  448. if(ret != RT_EOK)
  449. {
  450. rt_kprintf("get hub descriptor failed\n");
  451. return -RT_ERROR;
  452. }
  453. /* get full hub descriptor */
  454. ret = rt_usbh_hub_get_descriptor(device, (rt_uint8_t*)&hub->hub_desc,
  455. hub->hub_desc.length);
  456. if(ret != RT_EOK)
  457. {
  458. rt_kprintf("get hub descriptor again failed\n");
  459. return -RT_ERROR;
  460. }
  461. /* get hub ports number */
  462. hub->num_ports = hub->hub_desc.num_ports;
  463. hub->hcd = device->hcd;
  464. hub->self = device;
  465. /* reset all hub ports */
  466. for (i = 0; i < hub->num_ports; i++)
  467. {
  468. rt_usbh_hub_set_port_feature(hub, i + 1, PORT_FEAT_POWER);
  469. rt_thread_delay(hub->hub_desc.pwron_to_good
  470. * 2 * RT_TICK_PER_SECOND / 1000 );
  471. }
  472. if(intf->intf_desc->bNumEndpoints != 1)
  473. return -RT_ERROR;
  474. /* get endpoint descriptor from interface descriptor */
  475. rt_usbh_get_endpoint_descriptor(intf->intf_desc, 0, &ep_desc);
  476. if(ep_desc == RT_NULL)
  477. {
  478. rt_kprintf("rt_usb_get_endpoint_descriptor error\n");
  479. return -RT_ERROR;
  480. }
  481. /* the endpoint type of hub class should be interrupt */
  482. if( USB_EP_ATTR(ep_desc->bmAttributes) == USB_EP_ATTR_INT)
  483. {
  484. /* the endpoint direction of hub class should be in */
  485. if(ep_desc->bEndpointAddress & USB_DIR_IN)
  486. {
  487. /* allocate a pipe according to the endpoint type */
  488. pipe_in = rt_usb_instance_find_pipe(device,ep_desc->bEndpointAddress);
  489. if(pipe_in == RT_NULL)
  490. {
  491. return RT_ERROR;
  492. }
  493. rt_usb_pipe_add_callback(pipe_in,rt_usbh_hub_irq);
  494. }
  495. else return -RT_ERROR;
  496. }
  497. /* parameter check */
  498. RT_ASSERT(device->hcd != RT_NULL);
  499. pipe_in->user_data = hub;
  500. rt_usb_hcd_pipe_xfer(hub->hcd, pipe_in, hub->buffer,
  501. pipe_in->ep.wMaxPacketSize, timeout);
  502. return RT_EOK;
  503. }
  504. /**
  505. * This function will be invoked when usb hub plug out is detected and it would clean
  506. * and release all hub class related resources.
  507. *
  508. * @param arg the argument.
  509. *
  510. * @return the error code, RT_EOK on successfully.
  511. */
  512. static rt_err_t rt_usbh_hub_disable(void* arg)
  513. {
  514. int i;
  515. uhub_t hub;
  516. struct uhintf* intf = (struct uhintf*)arg;
  517. /* paremeter check */
  518. RT_ASSERT(intf != RT_NULL);
  519. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_hub_stop\n"));
  520. hub = (uhub_t)intf->user_data;
  521. for(i=0; i<hub->num_ports; i++)
  522. {
  523. if(hub->child[i] != RT_NULL)
  524. rt_usbh_detach_instance(hub->child[i]);
  525. }
  526. if(hub != RT_NULL) rt_free(hub);
  527. if(intf != RT_NULL) rt_free(intf);
  528. return RT_EOK;
  529. }
  530. /**
  531. * This function will register hub class driver to the usb class driver manager.
  532. * and it should be invoked in the usb system initialization.
  533. *
  534. * @return the error code, RT_EOK on successfully.
  535. */
  536. ucd_t rt_usbh_class_driver_hub(void)
  537. {
  538. hub_driver.class_code = USB_CLASS_HUB;
  539. hub_driver.enable = rt_usbh_hub_enable;
  540. hub_driver.disable = rt_usbh_hub_disable;
  541. return &hub_driver;
  542. }
  543. /**
  544. * This function is the main entry of usb hub thread, it is in charge of
  545. * processing all messages received from the usb message buffer.
  546. *
  547. * @param parameter the parameter of the usb host thread.
  548. *
  549. * @return none.
  550. */
  551. static void rt_usbh_hub_thread_entry(void* parameter)
  552. {
  553. while(1)
  554. {
  555. struct uhost_msg msg;
  556. /* receive message */
  557. if(rt_mq_recv(usb_mq, &msg, sizeof(struct uhost_msg), RT_WAITING_FOREVER)
  558. != RT_EOK ) continue;
  559. //RT_DEBUG_LOG(RT_DEBUG_USB, ("msg type %d\n", msg.type));
  560. switch (msg.type)
  561. {
  562. case USB_MSG_CONNECT_CHANGE:
  563. rt_usbh_hub_port_change(msg.content.hub);
  564. break;
  565. case USB_MSG_CALLBACK:
  566. /* invoke callback */
  567. msg.content.cb.function(msg.content.cb.context);
  568. break;
  569. default:
  570. break;
  571. }
  572. }
  573. }
  574. /**
  575. * This function will post an message to the usb message queue,
  576. *
  577. * @param msg the message to be posted
  578. *
  579. * @return the error code, RT_EOK on successfully.
  580. */
  581. rt_err_t rt_usbh_event_signal(struct uhost_msg* msg)
  582. {
  583. RT_ASSERT(msg != RT_NULL);
  584. /* send message to usb message queue */
  585. rt_mq_send(usb_mq, (void*)msg, sizeof(struct uhost_msg));
  586. return RT_EOK;
  587. }
  588. /**
  589. * This function will initialize usb hub thread.
  590. *
  591. * @return none.
  592. *
  593. */
  594. void rt_usbh_hub_init(uhcd_t hcd)
  595. {
  596. rt_thread_t thread;
  597. /* link root hub to hcd */
  598. root_hub.is_roothub = RT_TRUE;
  599. hcd->roothub = &root_hub;
  600. root_hub.hcd = hcd;
  601. root_hub.num_ports = hcd->num_ports;
  602. /* create usb message queue */
  603. usb_mq = rt_mq_create("usbh", 32, 16, RT_IPC_FLAG_FIFO);
  604. /* create usb hub thread */
  605. thread = rt_thread_create("usbh", rt_usbh_hub_thread_entry, RT_NULL,
  606. USB_THREAD_STACK_SIZE, 8, 20);
  607. if(thread != RT_NULL)
  608. {
  609. /* startup usb host thread */
  610. rt_thread_startup(thread);
  611. }
  612. }