hub.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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 = 100;
  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 = 100;
  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 = 100;
  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 = 100;
  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 = 100;
  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. /* parameter check */
  313. RT_ASSERT(hub != RT_NULL);
  314. for(i=0; i<times; i++)
  315. {
  316. ret = rt_usbh_hub_get_port_status(hub, port, &pstatus);
  317. if(ret != RT_EOK) return ret;
  318. if(!(pstatus & PORT_CCS))
  319. {
  320. connect = RT_FALSE;
  321. break;
  322. }
  323. rt_thread_delay(1);
  324. }
  325. if(connect) return RT_EOK;
  326. else return -RT_ERROR;
  327. }
  328. /**
  329. * This function will poll all the hub ports to detect port status, especially connect and
  330. * disconnect events.
  331. *
  332. * @param intf the interface instance.
  333. *
  334. * @return the error code, RT_EOK on successfully.
  335. */
  336. static rt_err_t rt_usbh_hub_port_change(uhub_t hub)
  337. {
  338. int i;
  339. rt_bool_t reconnect;
  340. /* parameter check */
  341. RT_ASSERT(hub != RT_NULL);
  342. /* get usb device instance */
  343. for (i = 0; i < hub->num_ports; i++)
  344. {
  345. rt_err_t ret;
  346. struct uinstance* device;
  347. rt_uint32_t pstatus = 0;
  348. reconnect = RT_FALSE;
  349. /* get hub port status */
  350. ret = rt_usbh_hub_get_port_status(hub, i + 1, &pstatus);
  351. if(ret != RT_EOK) continue;
  352. RT_DEBUG_LOG(RT_DEBUG_USB, ("port %d status 0x%x\n", i + 1, pstatus));
  353. /* check port status change */
  354. if (pstatus & PORT_CCSC)
  355. {
  356. /* clear port status change feature */
  357. rt_usbh_hub_clear_port_feature(hub, i + 1, PORT_FEAT_C_CONNECTION);
  358. reconnect = RT_TRUE;
  359. }
  360. if(pstatus & PORT_PESC)
  361. {
  362. rt_usbh_hub_clear_port_feature(hub, i + 1, PORT_FEAT_C_ENABLE);
  363. reconnect = RT_TRUE;
  364. }
  365. if(reconnect)
  366. {
  367. if(hub->child[i] != RT_NULL && hub->child[i]->status != DEV_STATUS_IDLE)
  368. rt_usbh_detach_instance(hub->child[i]);
  369. ret = rt_usbh_hub_port_debounce(hub, i + 1);
  370. if(ret != RT_EOK) continue;
  371. /* allocate an usb instance for new connected device */
  372. device = rt_usbh_alloc_instance(hub->hcd);
  373. if(device == RT_NULL) break;
  374. /* set usb device speed */
  375. device->speed = (pstatus & PORT_LSDA) ? 1 : 0;
  376. device->parent_hub = hub;
  377. device->hcd = hub->hcd;
  378. hub->child[i] = device;
  379. /* reset usb roothub port */
  380. rt_usbh_hub_reset_port(hub, i + 1);
  381. /* attatch the usb instance to the hcd */
  382. rt_usbh_attatch_instance(device);
  383. }
  384. }
  385. return RT_EOK;
  386. }
  387. /**
  388. * This function is the callback function of hub's int endpoint, it is invoked when data comes.
  389. *
  390. * @param context the context of the callback function.
  391. *
  392. * @return none.
  393. */
  394. static void rt_usbh_hub_irq(void* context)
  395. {
  396. upipe_t pipe;
  397. uhub_t hub;
  398. int timeout = 100;
  399. RT_ASSERT(context != RT_NULL);
  400. pipe = (upipe_t)context;
  401. hub = (uhub_t)pipe->user_data;
  402. if(pipe->status != UPIPE_STATUS_OK)
  403. {
  404. RT_DEBUG_LOG(RT_DEBUG_USB,("hub irq error\n"));
  405. return;
  406. }
  407. rt_usbh_hub_port_change(hub);
  408. RT_DEBUG_LOG(RT_DEBUG_USB,("hub int xfer...\n"));
  409. /* parameter check */
  410. RT_ASSERT(pipe->inst->hcd != RT_NULL);
  411. rt_usb_hcd_pipe_xfer(hub->self->hcd, pipe, hub->buffer, pipe->ep.wMaxPacketSize, timeout);
  412. }
  413. /**
  414. * This function will run usb hub class driver when usb hub is detected and identified
  415. * as a hub class device, it will continue to do the enumulate process.
  416. *
  417. * @param arg the argument.
  418. *
  419. * @return the error code, RT_EOK on successfully.
  420. */
  421. static rt_err_t rt_usbh_hub_enable(void *arg)
  422. {
  423. int i = 0;
  424. rt_err_t ret = RT_EOK;
  425. uep_desc_t ep_desc = RT_NULL;
  426. uhub_t hub;
  427. struct uinstance* device;
  428. struct uhintf* intf = (struct uhintf*)arg;
  429. upipe_t pipe_in = RT_NULL;
  430. int timeout = 300;
  431. /* paremeter check */
  432. RT_ASSERT(intf != RT_NULL);
  433. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_hub_run\n"));
  434. /* get usb device instance */
  435. device = intf->device;
  436. /* create a hub instance */
  437. hub = rt_malloc(sizeof(struct uhub));
  438. rt_memset(hub, 0, sizeof(struct uhub));
  439. /* make interface instance's user data point to hub instance */
  440. intf->user_data = (void*)hub;
  441. /* get hub descriptor head */
  442. ret = rt_usbh_hub_get_descriptor(device, (rt_uint8_t*)&hub->hub_desc, 8);
  443. if(ret != RT_EOK)
  444. {
  445. rt_kprintf("get hub descriptor failed\n");
  446. return -RT_ERROR;
  447. }
  448. /* get full hub descriptor */
  449. ret = rt_usbh_hub_get_descriptor(device, (rt_uint8_t*)&hub->hub_desc,
  450. hub->hub_desc.length);
  451. if(ret != RT_EOK)
  452. {
  453. rt_kprintf("get hub descriptor again failed\n");
  454. return -RT_ERROR;
  455. }
  456. /* get hub ports number */
  457. hub->num_ports = hub->hub_desc.num_ports;
  458. hub->hcd = device->hcd;
  459. hub->self = device;
  460. /* reset all hub ports */
  461. for (i = 0; i < hub->num_ports; i++)
  462. {
  463. rt_usbh_hub_set_port_feature(hub, i + 1, PORT_FEAT_POWER);
  464. rt_thread_delay(hub->hub_desc.pwron_to_good
  465. * 2 * RT_TICK_PER_SECOND / 1000 );
  466. }
  467. if(intf->intf_desc->bNumEndpoints != 1)
  468. return -RT_ERROR;
  469. /* get endpoint descriptor from interface descriptor */
  470. rt_usbh_get_endpoint_descriptor(intf->intf_desc, 0, &ep_desc);
  471. if(ep_desc == RT_NULL)
  472. {
  473. rt_kprintf("rt_usb_get_endpoint_descriptor error\n");
  474. return -RT_ERROR;
  475. }
  476. /* the endpoint type of hub class should be interrupt */
  477. if( USB_EP_ATTR(ep_desc->bmAttributes) == USB_EP_ATTR_INT)
  478. {
  479. /* the endpoint direction of hub class should be in */
  480. if(ep_desc->bEndpointAddress & USB_DIR_IN)
  481. {
  482. /* allocate a pipe according to the endpoint type */
  483. pipe_in = rt_usb_instance_find_pipe(device,ep_desc->bEndpointAddress);
  484. if(pipe_in == RT_NULL)
  485. {
  486. return RT_ERROR;
  487. }
  488. rt_usb_pipe_add_callback(pipe_in,rt_usbh_hub_irq);
  489. }
  490. else return -RT_ERROR;
  491. }
  492. /* parameter check */
  493. RT_ASSERT(device->hcd != RT_NULL);
  494. pipe_in->user_data = hub;
  495. rt_usb_hcd_pipe_xfer(hub->hcd, pipe_in, hub->buffer,
  496. pipe_in->ep.wMaxPacketSize, timeout);
  497. return RT_EOK;
  498. }
  499. /**
  500. * This function will be invoked when usb hub plug out is detected and it would clean
  501. * and release all hub class related resources.
  502. *
  503. * @param arg the argument.
  504. *
  505. * @return the error code, RT_EOK on successfully.
  506. */
  507. static rt_err_t rt_usbh_hub_disable(void* arg)
  508. {
  509. int i;
  510. uhub_t hub;
  511. struct uhintf* intf = (struct uhintf*)arg;
  512. /* paremeter check */
  513. RT_ASSERT(intf != RT_NULL);
  514. RT_DEBUG_LOG(RT_DEBUG_USB, ("rt_usbh_hub_stop\n"));
  515. hub = (uhub_t)intf->user_data;
  516. for(i=0; i<hub->num_ports; i++)
  517. {
  518. if(hub->child[i] != RT_NULL)
  519. rt_usbh_detach_instance(hub->child[i]);
  520. }
  521. if(hub != RT_NULL) rt_free(hub);
  522. if(intf != RT_NULL) rt_free(intf);
  523. return RT_EOK;
  524. }
  525. /**
  526. * This function will register hub class driver to the usb class driver manager.
  527. * and it should be invoked in the usb system initialization.
  528. *
  529. * @return the error code, RT_EOK on successfully.
  530. */
  531. ucd_t rt_usbh_class_driver_hub(void)
  532. {
  533. hub_driver.class_code = USB_CLASS_HUB;
  534. hub_driver.enable = rt_usbh_hub_enable;
  535. hub_driver.disable = rt_usbh_hub_disable;
  536. return &hub_driver;
  537. }
  538. /**
  539. * This function is the main entry of usb hub thread, it is in charge of
  540. * processing all messages received from the usb message buffer.
  541. *
  542. * @param parameter the parameter of the usb host thread.
  543. *
  544. * @return none.
  545. */
  546. static void rt_usbh_hub_thread_entry(void* parameter)
  547. {
  548. while(1)
  549. {
  550. struct uhost_msg msg;
  551. /* receive message */
  552. if(rt_mq_recv(usb_mq, &msg, sizeof(struct uhost_msg), RT_WAITING_FOREVER)
  553. != RT_EOK ) continue;
  554. //RT_DEBUG_LOG(RT_DEBUG_USB, ("msg type %d\n", msg.type));
  555. switch (msg.type)
  556. {
  557. case USB_MSG_CONNECT_CHANGE:
  558. rt_usbh_hub_port_change(msg.content.hub);
  559. break;
  560. case USB_MSG_CALLBACK:
  561. /* invoke callback */
  562. msg.content.cb.function(msg.content.cb.context);
  563. break;
  564. default:
  565. break;
  566. }
  567. }
  568. }
  569. /**
  570. * This function will post an message to the usb message queue,
  571. *
  572. * @param msg the message to be posted
  573. *
  574. * @return the error code, RT_EOK on successfully.
  575. */
  576. rt_err_t rt_usbh_event_signal(struct uhost_msg* msg)
  577. {
  578. RT_ASSERT(msg != RT_NULL);
  579. /* send message to usb message queue */
  580. rt_mq_send(usb_mq, (void*)msg, sizeof(struct uhost_msg));
  581. return RT_EOK;
  582. }
  583. /**
  584. * This function will initialize usb hub thread.
  585. *
  586. * @return none.
  587. *
  588. */
  589. void rt_usbh_hub_init(uhcd_t hcd)
  590. {
  591. rt_thread_t thread;
  592. /* link root hub to hcd */
  593. root_hub.is_roothub = RT_TRUE;
  594. hcd->roothub = &root_hub;
  595. root_hub.hcd = hcd;
  596. root_hub.num_ports = hcd->num_ports;
  597. /* create usb message queue */
  598. usb_mq = rt_mq_create("usbh", 32, 16, RT_IPC_FLAG_FIFO);
  599. /* create usb hub thread */
  600. thread = rt_thread_create("usbh", rt_usbh_hub_thread_entry, RT_NULL,
  601. USB_THREAD_STACK_SIZE, 8, 20);
  602. if(thread != RT_NULL)
  603. {
  604. /* startup usb host thread */
  605. rt_thread_startup(thread);
  606. }
  607. }