hub.c 19 KB

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