drv_usbhost.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-5-4 CHChen First version
  10. *
  11. ******************************************************************************/
  12. #include <rtconfig.h>
  13. #if defined(BSP_USING_USBH) || defined(BSP_USING_HSUSBH)
  14. #include <rtdevice.h>
  15. #include <rthw.h>
  16. #include <NuMicro.h>
  17. #include "usb.h"
  18. #include "usbh_lib.h"
  19. #if !defined(NU_USBHOST_HUB_POLLING_INTERVAL)
  20. #define NU_USBHOST_HUB_POLLING_INTERVAL (100)
  21. #endif
  22. #define NU_MAX_USBH_PORT 2 //USB1.1 + USB2.0 port
  23. #define NU_MAX_USBH_PIPE 16
  24. #define NU_USBH_THREAD_STACK_SIZE 2048
  25. #define NU_MAX_USBH_HUB_PORT_DEV USB_HUB_PORT_NUM
  26. /* Private typedef --------------------------------------------------------------*/
  27. typedef struct nu_port_dev
  28. {
  29. rt_bool_t bRHParent;
  30. UDEV_T *pUDev;
  31. EP_INFO_T *apsEPInfo[NU_MAX_USBH_PIPE];
  32. struct urequest asSetupReq[NU_MAX_USBH_PIPE];
  33. struct rt_completion utr_completion;
  34. int port_num;
  35. rt_bool_t bEnumDone;
  36. } S_NU_PORT_DEV;
  37. typedef struct nu_port_ctrl
  38. {
  39. S_NU_PORT_DEV sRHPortDev;
  40. S_NU_PORT_DEV asHubPortDev[NU_MAX_USBH_HUB_PORT_DEV];
  41. } S_NU_RH_PORT_CTRL;
  42. struct nu_usbh_dev
  43. {
  44. uhcd_t uhcd;
  45. rt_thread_t polling_thread;
  46. S_NU_RH_PORT_CTRL asPortCtrl[NU_MAX_USBH_PORT];
  47. };
  48. /* Private variables ------------------------------------------------------------*/
  49. static struct nu_usbh_dev s_sUSBHDev =
  50. {
  51. .uhcd = RT_NULL,
  52. };
  53. static S_NU_RH_PORT_CTRL *
  54. GetRHPortControlFromPipe(
  55. upipe_t pipe
  56. )
  57. {
  58. uinst_t inst;
  59. int port;
  60. if (pipe->inst->parent_hub->is_roothub)
  61. {
  62. //case: device ---> root hub
  63. inst = pipe->inst;
  64. port = inst->port;
  65. }
  66. else
  67. {
  68. //case: device ---> hub ---> root hub
  69. inst = pipe->inst->parent_hub->self;
  70. port = inst->port;
  71. }
  72. if (port > NU_MAX_USBH_PORT)
  73. {
  74. RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_open_pipe ERROR: port index over NU_MAX_USBH_PORT\n"));
  75. return RT_NULL;
  76. }
  77. return &s_sUSBHDev.asPortCtrl[port - 1];;
  78. }
  79. static S_NU_PORT_DEV *
  80. GetPortDevFromPipe(
  81. upipe_t pipe
  82. )
  83. {
  84. S_NU_RH_PORT_CTRL *psRHPortCtrl = GetRHPortControlFromPipe(pipe);
  85. int i;
  86. if (psRHPortCtrl == RT_NULL)
  87. return RT_NULL;
  88. if (pipe->inst->parent_hub->is_roothub)
  89. {
  90. //case: device ---> root hub
  91. return &psRHPortCtrl->sRHPortDev;
  92. }
  93. //case: device ---> hub ---> root hub
  94. for (i = 0 ; i < NU_MAX_USBH_HUB_PORT_DEV; i ++)
  95. {
  96. if (psRHPortCtrl->asHubPortDev[i].port_num == pipe->inst->port)
  97. break;
  98. }
  99. if (i >= NU_MAX_USBH_HUB_PORT_DEV)
  100. return RT_NULL;
  101. return &psRHPortCtrl->asHubPortDev[i];
  102. }
  103. static rt_err_t nu_reset_port(rt_uint8_t port)
  104. {
  105. S_NU_RH_PORT_CTRL *psPortCtrl;
  106. if (port > NU_MAX_USBH_PORT)
  107. {
  108. RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_reset_port ERROR: port index over NU_MAX_USBH_PORT\n"));
  109. return RT_EIO;
  110. }
  111. psPortCtrl = &s_sUSBHDev.asPortCtrl[port - 1];
  112. if (psPortCtrl->sRHPortDev.pUDev == NULL)
  113. {
  114. RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_reset_port ERROR: udev not found\n"));
  115. return RT_EIO;
  116. }
  117. usbh_reset_port(psPortCtrl->sRHPortDev.pUDev);
  118. return RT_EOK;
  119. }
  120. static EP_INFO_T *GetFreePipe(
  121. S_NU_RH_PORT_CTRL *psPortCtrl,
  122. S_NU_PORT_DEV *psPortDev,
  123. rt_uint8_t *pu8PipeIndex
  124. )
  125. {
  126. rt_uint8_t i;
  127. if (psPortCtrl == NULL)
  128. return NULL;
  129. for (i = 1; i < NU_MAX_USBH_PIPE; i ++)
  130. {
  131. if (psPortDev->apsEPInfo[i] == NULL)
  132. break;
  133. }
  134. if (i >= NU_MAX_USBH_PIPE)
  135. return RT_NULL;
  136. EP_INFO_T *psEPInfo = rt_malloc(sizeof(EP_INFO_T));
  137. psPortDev->apsEPInfo[i] = psEPInfo;
  138. *pu8PipeIndex = i;
  139. return psEPInfo;
  140. }
  141. static void FreePipe(
  142. S_NU_RH_PORT_CTRL *psPortCtrl,
  143. S_NU_PORT_DEV *psPortDev,
  144. rt_uint8_t u8PipeIndex
  145. )
  146. {
  147. if (psPortCtrl == NULL)
  148. return;
  149. if (u8PipeIndex >= NU_MAX_USBH_PIPE)
  150. return;
  151. if (psPortDev->apsEPInfo[u8PipeIndex])
  152. {
  153. rt_free(psPortDev->apsEPInfo[u8PipeIndex]);
  154. psPortDev->apsEPInfo[u8PipeIndex] = RT_NULL;
  155. }
  156. }
  157. static S_NU_PORT_DEV *
  158. AllocateNewUDev(
  159. S_NU_RH_PORT_CTRL *psRHPortCtrl
  160. )
  161. {
  162. int i;
  163. for (i = 0 ; i < NU_MAX_USBH_HUB_PORT_DEV; i ++)
  164. {
  165. if (psRHPortCtrl->asHubPortDev[i].pUDev == NULL)
  166. break;
  167. }
  168. if (i >= NU_MAX_USBH_HUB_PORT_DEV)
  169. return RT_NULL;
  170. psRHPortCtrl->asHubPortDev[i].pUDev = alloc_device();
  171. if (psRHPortCtrl->asHubPortDev[i].pUDev == NULL)
  172. return RT_NULL;
  173. return &psRHPortCtrl->asHubPortDev[i];
  174. }
  175. static rt_err_t nu_open_pipe(upipe_t pipe)
  176. {
  177. S_NU_RH_PORT_CTRL *psPortCtrl;
  178. S_NU_PORT_DEV *psPortDev;
  179. psPortCtrl = GetRHPortControlFromPipe(pipe);
  180. if (psPortCtrl == RT_NULL)
  181. {
  182. return RT_EIO;
  183. }
  184. if (psPortCtrl->sRHPortDev.pUDev == NULL)
  185. {
  186. RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_open_pipe ERROR: udev not found\n"));
  187. return RT_EIO;
  188. }
  189. psPortDev = GetPortDevFromPipe(pipe);
  190. if ((psPortDev == NULL) || (psPortDev->pUDev == NULL))
  191. {
  192. //allocate new dev for hub device
  193. psPortDev = AllocateNewUDev(psPortCtrl);
  194. if (psPortDev == RT_NULL)
  195. {
  196. RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_open_pipe ERROR: udev allocate failed\n"));
  197. return RT_EIO;
  198. }
  199. if (pipe->inst->speed)
  200. {
  201. psPortDev->pUDev->speed = SPEED_FULL;
  202. }
  203. else
  204. {
  205. psPortDev->pUDev->speed = SPEED_HIGH;
  206. }
  207. psPortDev->pUDev->parent = NULL;
  208. psPortDev->pUDev->hc_driver = psPortCtrl->sRHPortDev.pUDev->hc_driver;
  209. psPortDev->port_num = pipe->inst->port;
  210. psPortDev->pUDev->port_num = pipe->inst->port;
  211. psPortDev->bEnumDone = FALSE;
  212. }
  213. //For ep0 control transfer
  214. if ((pipe->ep.bEndpointAddress & 0x7F) == 0)
  215. {
  216. pipe->pipe_index = 0;
  217. return RT_EOK;
  218. }
  219. EP_INFO_T *psEPInfo = GetFreePipe(psPortCtrl, psPortDev, &pipe->pipe_index);
  220. if (psEPInfo == RT_NULL)
  221. {
  222. RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_open_pipe ERROR: get free pipe failed\n"));
  223. return RT_ENOMEM;
  224. }
  225. int pksz;
  226. psEPInfo->bEndpointAddress = pipe->ep.bEndpointAddress;
  227. psEPInfo->bmAttributes = pipe->ep.bmAttributes;
  228. pksz = pipe->ep.wMaxPacketSize;
  229. pksz = (pksz & 0x07ff) * (1 + ((pksz >> 11) & 3));
  230. psEPInfo->wMaxPacketSize = pksz;
  231. psEPInfo->bInterval = pipe->ep.bInterval;
  232. psEPInfo->hw_pipe = NULL;
  233. psEPInfo->bToggle = 0;
  234. return RT_EOK;
  235. }
  236. static rt_err_t nu_close_pipe(upipe_t pipe)
  237. {
  238. S_NU_RH_PORT_CTRL *psPortCtrl;
  239. S_NU_PORT_DEV *psPortDev;
  240. psPortCtrl = GetRHPortControlFromPipe(pipe);
  241. if (psPortCtrl == RT_NULL)
  242. {
  243. return RT_EIO;
  244. }
  245. psPortDev = GetPortDevFromPipe(pipe);
  246. //For ep0 control transfer
  247. if ((pipe->ep.bEndpointAddress & 0x7F) == 0)
  248. {
  249. if ((psPortDev) && (psPortDev->bRHParent == FALSE) && (psPortDev->bEnumDone == TRUE))
  250. {
  251. if (psPortDev->pUDev)
  252. {
  253. int i;
  254. for (i = 0; i < NU_MAX_USBH_PIPE; i++)
  255. {
  256. if (psPortDev->apsEPInfo[i] != NULL)
  257. {
  258. usbh_quit_xfer(psPortDev->pUDev, psPortDev->apsEPInfo[i]);
  259. }
  260. }
  261. free_device(psPortDev->pUDev);
  262. psPortDev->pUDev = NULL;
  263. }
  264. }
  265. }
  266. if (psPortDev != NULL)
  267. {
  268. FreePipe(psPortCtrl, psPortDev, pipe->pipe_index);
  269. }
  270. return RT_EOK;
  271. }
  272. static int nu_ctrl_xfer(
  273. S_NU_PORT_DEV *psPortDev,
  274. struct urequest *psSetup,
  275. void *buffer,
  276. int timeouts
  277. )
  278. {
  279. uint32_t xfer_len;
  280. int ret;
  281. ret = usbh_ctrl_xfer(psPortDev->pUDev, psSetup->request_type, psSetup->bRequest, psSetup->wValue, psSetup->wIndex, psSetup->wLength, buffer, &xfer_len, timeouts * 10);
  282. if (ret < 0)
  283. {
  284. RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_ctrl_xfer ERROR: xfer failed %d\n", ret));
  285. return ret;
  286. }
  287. if (xfer_len != psSetup->wLength)
  288. {
  289. RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_ctrl_xfer ERROR: xfer length %d %d\n", psSetup->wLength, xfer_len));
  290. }
  291. if ((psSetup->bRequest == USB_REQ_SET_ADDRESS) && ((psSetup->request_type & 0x60) == REQ_TYPE_STD_DEV))
  292. psPortDev->pUDev->dev_num = psSetup->wValue;
  293. if ((psSetup->bRequest == USB_REQ_SET_CONFIGURATION) && ((psSetup->request_type & 0x60) == REQ_TYPE_STD_DEV))
  294. {
  295. psPortDev->pUDev->cur_conf = psSetup->wValue;
  296. psPortDev->bEnumDone = TRUE;
  297. }
  298. return xfer_len;
  299. }
  300. static int nu_bulk_xfer(
  301. S_NU_PORT_DEV *psPortDev,
  302. UTR_T *psUTR,
  303. int timeouts
  304. )
  305. {
  306. int ret;
  307. ret = usbh_bulk_xfer(psUTR);
  308. if (ret < 0)
  309. return ret;
  310. //wait transfer done
  311. rt_completion_wait(&(psPortDev->utr_completion), timeouts);
  312. return 0;
  313. }
  314. static int nu_int_xfer(
  315. upipe_t pipe,
  316. S_NU_PORT_DEV *psPortDev,
  317. UTR_T *psUTR,
  318. int timeouts
  319. )
  320. {
  321. int ret;
  322. int retry = 3;
  323. while (retry > 0)
  324. {
  325. ret = usbh_int_xfer(psUTR);
  326. if (ret == 0)
  327. break;
  328. RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_int_xfer ERROR: failed to submit interrupt request\n"));
  329. rt_thread_delay((pipe->ep.bInterval * RT_TICK_PER_SECOND / 1000) > 0 ? (pipe->ep.bInterval * RT_TICK_PER_SECOND / 1000) : 1);
  330. retry --;
  331. }
  332. if (ret < 0)
  333. return ret;
  334. return 0;
  335. }
  336. static void xfer_done_cb(UTR_T *psUTR)
  337. {
  338. S_NU_PORT_DEV *psPortDev = (S_NU_PORT_DEV *)psUTR->context;
  339. //transfer done, signal utr_completion
  340. rt_completion_done(&(psPortDev->utr_completion));
  341. }
  342. static void int_xfer_done_cb(UTR_T *psUTR)
  343. {
  344. upipe_t pipe = (upipe_t)psUTR->context;
  345. if (psUTR->status != 0)
  346. {
  347. rt_kprintf("Interrupt xfer failed %d\n", psUTR->status);
  348. free_utr(psUTR);
  349. return;
  350. }
  351. if (pipe->callback != RT_NULL)
  352. {
  353. struct uhost_msg msg;
  354. msg.type = USB_MSG_CALLBACK;
  355. msg.content.cb.function = pipe->callback;
  356. msg.content.cb.context = pipe;
  357. rt_usbh_event_signal(&msg);
  358. }
  359. free_utr(psUTR);
  360. }
  361. static int nu_pipe_xfer(upipe_t pipe, rt_uint8_t token, void *buffer, int nbytes, int timeouts)
  362. {
  363. S_NU_RH_PORT_CTRL *psPortCtrl;
  364. S_NU_PORT_DEV *psPortDev;
  365. psPortCtrl = GetRHPortControlFromPipe(pipe);
  366. if (psPortCtrl == RT_NULL)
  367. {
  368. return RT_EIO;
  369. }
  370. psPortDev = GetPortDevFromPipe(pipe);
  371. if (psPortDev->pUDev == NULL)
  372. {
  373. RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_pipe_xfer ERROR: udev not found\n"));
  374. return -RT_ERROR;
  375. }
  376. //ctrl xfer
  377. if (pipe->ep.bmAttributes == USB_EP_ATTR_CONTROL)
  378. {
  379. if (token == USBH_PID_SETUP)
  380. {
  381. struct urequest *psSetup;
  382. psSetup = (struct urequest *)buffer;
  383. if (psSetup->request_type & USB_REQ_TYPE_DIR_IN)
  384. {
  385. //Store setup request
  386. rt_memcpy(&psPortCtrl->asHubPortDev->asSetupReq[pipe->pipe_index], psSetup, sizeof(struct urequest));
  387. }
  388. else
  389. {
  390. //Trigger USBHostLib Ctril_Xfer
  391. nu_ctrl_xfer(psPortDev, psSetup, NULL, timeouts);
  392. }
  393. return nbytes;
  394. }
  395. else
  396. {
  397. //token == USBH_PID_DATA
  398. if (buffer == RT_NULL)
  399. return nbytes;
  400. if ((pipe->ep.bEndpointAddress & USB_DIR_MASK) == USB_DIR_IN)
  401. {
  402. //Trigger USBHostLib Ctril_Xfer
  403. nu_ctrl_xfer(psPortDev, &psPortCtrl->asHubPortDev->asSetupReq[pipe->pipe_index], buffer, timeouts);
  404. }
  405. else
  406. {
  407. RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_pipe_xfer ERROR: can not handle ctrl xfer case: buffer!=NULL and USB_DIR_OUT\n"));
  408. }
  409. return nbytes;
  410. }
  411. }
  412. //others xfer
  413. rt_completion_init(&(psPortDev->utr_completion));
  414. //setup UTR
  415. UTR_T *psUTR;
  416. int i32XferLen;
  417. psUTR = alloc_utr(psPortDev->pUDev);
  418. if (!psUTR)
  419. {
  420. RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_pipe_xfer ERROR: unable alloc UTR\n"));
  421. return -(RT_ERROR);
  422. }
  423. psUTR->ep = psPortDev->apsEPInfo[pipe->pipe_index];
  424. psUTR->buff = buffer;
  425. psUTR->data_len = nbytes;
  426. psUTR->xfer_len = 0;
  427. psUTR->func = xfer_done_cb;
  428. psUTR->context = psPortDev;
  429. psUTR->bIsTransferDone = 0;
  430. psUTR->status = 0;
  431. if (pipe->ep.bmAttributes == USB_EP_ATTR_BULK)
  432. {
  433. if (nu_bulk_xfer(psPortDev, psUTR, timeouts) < 0)
  434. {
  435. free_utr(psUTR);
  436. RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_pipe_xfer ERROR: bulk transfer failed\n"));
  437. return -(RT_ERROR);
  438. }
  439. }
  440. if (pipe->ep.bmAttributes == USB_EP_ATTR_INT)
  441. {
  442. psUTR->func = int_xfer_done_cb;
  443. psUTR->context = pipe;
  444. if (nu_int_xfer(pipe, psPortDev, psUTR, timeouts) < 0)
  445. {
  446. free_utr(psUTR);
  447. RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_pipe_xfer ERROR: int transfer failed\n"));
  448. return -(RT_ERROR);
  449. }
  450. return nbytes;
  451. }
  452. //TODO: ISO transfer
  453. if (pipe->ep.bmAttributes == USB_EP_ATTR_ISOC)
  454. {
  455. free_utr(psUTR);
  456. RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_pipe_xfer ERROR: isoc transfer not support\n"));
  457. return -1;
  458. }
  459. if (psUTR->bIsTransferDone == 0)
  460. {
  461. //Timeout
  462. RT_DEBUG_LOG(RT_DEBUG_USB, ("nu_pipe_xfer ERROR: timeout\n"));
  463. pipe->status = UPIPE_STATUS_ERROR;
  464. usbh_quit_utr(psUTR);
  465. }
  466. else
  467. {
  468. // Transfer Done. Get status
  469. if (psUTR->status == 0)
  470. {
  471. pipe->status = UPIPE_STATUS_OK;
  472. }
  473. else if (psUTR->status == USBH_ERR_STALL)
  474. {
  475. pipe->status = UPIPE_STATUS_STALL;
  476. }
  477. else
  478. {
  479. pipe->status = UPIPE_STATUS_ERROR;
  480. }
  481. }
  482. i32XferLen = psUTR->xfer_len;
  483. free_utr(psUTR);
  484. //Call callback
  485. if (pipe->callback != RT_NULL)
  486. {
  487. struct uhost_msg msg;
  488. msg.type = USB_MSG_CALLBACK;
  489. msg.content.cb.function = pipe->callback;
  490. msg.content.cb.context = pipe->user_data;
  491. rt_usbh_event_signal(&msg);
  492. }
  493. if (pipe->status != UPIPE_STATUS_OK)
  494. return -(RT_ERROR);
  495. if ((pipe->ep.bEndpointAddress & USB_DIR_MASK) == USB_DIR_IN)
  496. {
  497. return i32XferLen;
  498. }
  499. else if ((pipe->ep.bEndpointAddress & USB_DIR_MASK) == USB_DIR_OUT)
  500. {
  501. return i32XferLen;
  502. }
  503. return nbytes;
  504. }
  505. //Pooling USB root hub status task
  506. static void nu_usbh_rh_thread_entry(void *parameter)
  507. {
  508. while (1)
  509. {
  510. usbh_polling_root_hubs();
  511. rt_thread_mdelay(NU_USBHOST_HUB_POLLING_INTERVAL);
  512. }
  513. }
  514. static void nu_hcd_connect_callback(
  515. struct udev_t *udev,
  516. int param
  517. )
  518. {
  519. int i;
  520. int port_index;
  521. S_NU_RH_PORT_CTRL *psPortCtrl;
  522. for (i = 0; i < NU_MAX_USBH_PORT; i++)
  523. {
  524. psPortCtrl = &s_sUSBHDev.asPortCtrl[i];
  525. if (psPortCtrl->sRHPortDev.pUDev == NULL)
  526. break;
  527. }
  528. if (i >= NU_MAX_USBH_PORT)
  529. {
  530. RT_DEBUG_LOG(RT_DEBUG_USB, ("ERROR: port connect slot is full\n"));
  531. return;
  532. }
  533. port_index = i + 1;
  534. psPortCtrl->sRHPortDev.pUDev = udev;
  535. psPortCtrl->sRHPortDev.bRHParent = TRUE;
  536. RT_DEBUG_LOG(RT_DEBUG_USB, ("usb connected\n"));
  537. if (udev->speed == SPEED_HIGH)
  538. rt_usbh_root_hub_connect_handler(s_sUSBHDev.uhcd, port_index, RT_TRUE);
  539. else
  540. rt_usbh_root_hub_connect_handler(s_sUSBHDev.uhcd, port_index, RT_FALSE);
  541. }
  542. static void nu_hcd_disconnect_callback(
  543. struct udev_t *udev,
  544. int param
  545. )
  546. {
  547. int i;
  548. int port_index;
  549. S_NU_RH_PORT_CTRL *psPortCtrl;
  550. for (i = 0; i < NU_MAX_USBH_PORT; i++)
  551. {
  552. psPortCtrl = &s_sUSBHDev.asPortCtrl[i];
  553. if (psPortCtrl->sRHPortDev.pUDev == udev)
  554. break;
  555. }
  556. if (i >= NU_MAX_USBH_PORT)
  557. {
  558. RT_DEBUG_LOG(RT_DEBUG_USB, ("ERROR: udev not found\n"));
  559. return;
  560. }
  561. port_index = i + 1;
  562. for (i = 0; i < NU_MAX_USBH_PIPE; i++)
  563. {
  564. if (psPortCtrl->sRHPortDev.apsEPInfo[i] != NULL)
  565. {
  566. usbh_quit_xfer(psPortCtrl->sRHPortDev.pUDev, psPortCtrl->sRHPortDev.apsEPInfo[i]);
  567. }
  568. }
  569. psPortCtrl->sRHPortDev.pUDev = NULL;
  570. RT_DEBUG_LOG(RT_DEBUG_USB, ("usb disconnect\n"));
  571. rt_usbh_root_hub_disconnect_handler(s_sUSBHDev.uhcd, port_index);
  572. }
  573. /* USB host operations -----------------------------------------------------------*/
  574. static struct uhcd_ops nu_uhcd_ops =
  575. {
  576. nu_reset_port,
  577. nu_pipe_xfer,
  578. nu_open_pipe,
  579. nu_close_pipe,
  580. };
  581. static rt_err_t nu_hcd_init(rt_device_t device)
  582. {
  583. struct nu_usbh_dev * pNuUSBHDev = (struct nu_usbh_dev *)device;
  584. usbh_core_init();
  585. //install connect/disconnect callback
  586. usbh_install_conn_callback(nu_hcd_connect_callback, nu_hcd_disconnect_callback);
  587. usbh_polling_root_hubs();
  588. //create thread for polling usbh port status
  589. /* create usb hub thread */
  590. pNuUSBHDev->polling_thread = rt_thread_create("usbh_drv", nu_usbh_rh_thread_entry, RT_NULL,
  591. NU_USBH_THREAD_STACK_SIZE, 8, 20);
  592. if ( pNuUSBHDev->polling_thread != RT_NULL)
  593. {
  594. /* startup usb host thread */
  595. rt_thread_startup( pNuUSBHDev->polling_thread );
  596. }
  597. else
  598. {
  599. return -(RT_EEMPTY);
  600. }
  601. return RT_EOK;
  602. }
  603. /* global function for USB host library -----------------------------*/
  604. uint32_t usbh_get_ticks(void)
  605. {
  606. return rt_tick_get();
  607. }
  608. void usbh_delay_ms(int msec)
  609. {
  610. rt_thread_mdelay(msec);
  611. }
  612. uint32_t usbh_tick_from_millisecond(uint32_t msec)
  613. {
  614. return rt_tick_from_millisecond(msec);
  615. }
  616. #if defined(RT_USING_PM)
  617. /* device pm suspend() entry. */
  618. static int usbhost_pm_suspend(const struct rt_device *device, rt_uint8_t mode)
  619. {
  620. struct nu_usbh_dev * pNuUSBHDev = (struct nu_usbh_dev *)device;
  621. RT_ASSERT(pNuUSBHDev!=RT_NULL);
  622. switch (mode)
  623. {
  624. case PM_SLEEP_MODE_LIGHT:
  625. case PM_SLEEP_MODE_DEEP:
  626. pNuUSBHDev->polling_thread->stat = RT_THREAD_READY;
  627. rt_thread_suspend(pNuUSBHDev->polling_thread);
  628. break;
  629. default:
  630. break;
  631. }
  632. return (int)RT_EOK;
  633. }
  634. /* device pm resume() entry. */
  635. static void usbhost_pm_resume(const struct rt_device *device, rt_uint8_t mode)
  636. {
  637. struct nu_usbh_dev * pNuUSBHDev = (struct nu_usbh_dev *)device;
  638. RT_ASSERT(pNuUSBHDev!=RT_NULL);
  639. switch (mode)
  640. {
  641. case PM_SLEEP_MODE_LIGHT:
  642. case PM_SLEEP_MODE_DEEP:
  643. rt_thread_resume(pNuUSBHDev->polling_thread);
  644. break;
  645. default:
  646. break;
  647. }
  648. }
  649. static struct rt_device_pm_ops device_pm_ops =
  650. {
  651. .suspend = usbhost_pm_suspend,
  652. .resume = usbhost_pm_resume,
  653. .frequency_change = RT_NULL
  654. };
  655. #endif
  656. int nu_usbh_register(void)
  657. {
  658. rt_err_t res = -RT_ERROR;
  659. #if !defined(BSP_USING_HSOTG)
  660. SYS_UnlockReg();
  661. #if defined(BSP_USING_HSUSBH)
  662. /* Set USB Host role */
  663. SYS->USBPHY = (SYS->USBPHY & ~SYS_USBPHY_HSUSBROLE_Msk) | (0x1u << SYS_USBPHY_HSUSBROLE_Pos);
  664. SYS->USBPHY |= SYS_USBPHY_HSUSBEN_Msk | SYS_USBPHY_SBO_Msk;
  665. rt_thread_delay(20);
  666. SYS->USBPHY |= SYS_USBPHY_HSUSBACT_Msk;
  667. #endif
  668. #if defined(BSP_USING_USBH)
  669. /* Set USB Host role */
  670. SYS->USBPHY = (SYS->USBPHY & ~SYS_USBPHY_USBROLE_Msk) | (0x1u << SYS_USBPHY_USBROLE_Pos);
  671. SYS->USBPHY |= SYS_USBPHY_USBEN_Msk | SYS_USBPHY_SBO_Msk ;
  672. #endif
  673. SYS_LockReg();
  674. #endif
  675. rt_memset(&s_sUSBHDev, 0x0, sizeof(struct nu_usbh_dev));
  676. uhcd_t uhcd = (uhcd_t)rt_malloc(sizeof(struct uhcd));
  677. RT_ASSERT(res != RT_NULL);
  678. rt_memset((void *)uhcd, 0, sizeof(struct uhcd));
  679. uhcd->parent.type = RT_Device_Class_USBHost;
  680. uhcd->parent.init = nu_hcd_init;
  681. uhcd->parent.user_data = &s_sUSBHDev;
  682. uhcd->ops = &nu_uhcd_ops;
  683. uhcd->num_ports = NU_MAX_USBH_PORT;
  684. s_sUSBHDev.uhcd = uhcd;
  685. res = rt_device_register(&uhcd->parent, "usbh", RT_DEVICE_FLAG_DEACTIVATE);
  686. RT_ASSERT(res == RT_EOK);
  687. /*initialize the usb host function */
  688. res = rt_usb_host_init("usbh");
  689. RT_ASSERT(res == RT_EOK);
  690. #if defined(RT_USING_PM)
  691. rt_pm_device_register(&uhcd->parent, &device_pm_ops);
  692. #endif
  693. return RT_EOK;
  694. }
  695. INIT_DEVICE_EXPORT(nu_usbh_register);
  696. #endif