hub.c 19 KB

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