drv_usbhost.c 24 KB

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