hub.c 19 KB

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