drv_usbhost.c 23 KB

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