hub.c 19 KB

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