drv_usbhost.c 22 KB

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