drv_usbhost.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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");
  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. #define TIMEOUT_RETRY 3
  343. int retry = TIMEOUT_RETRY;
  344. int ret = usbh_bulk_xfer(psUTR);
  345. if (ret < 0)
  346. {
  347. rt_kprintf("usbh_bulk_xfer %x\n", ret);
  348. return ret;
  349. }
  350. while (retry > 0)
  351. {
  352. if (rt_completion_wait(&(psPortDev->utr_completion), timeouts) != 0)
  353. {
  354. rt_uint32_t level;
  355. rt_kprintf("Request %d Timeout in %d ms!!\n", psUTR->data_len, timeouts);
  356. rt_completion_init(&(psPortDev->utr_completion));
  357. rt_thread_mdelay(1);
  358. // Workaround: To fix timeout case, this way is traveling qh's linking-list again.
  359. level = rt_hw_interrupt_disable();
  360. extern void scan_asynchronous_list();
  361. extern void iaad_remove_qh();
  362. scan_asynchronous_list();
  363. iaad_remove_qh();
  364. rt_hw_interrupt_enable(level);
  365. }
  366. else
  367. break;
  368. retry--;
  369. }
  370. return (retry > 0) ? 0 : -1;
  371. }
  372. static int nu_int_xfer(
  373. upipe_t pipe,
  374. S_NU_PORT_DEV *psPortDev,
  375. UTR_T *psUTR,
  376. int timeouts)
  377. {
  378. int ret;
  379. while (1)
  380. {
  381. ret = usbh_int_xfer(psUTR);
  382. if (ret < 0)
  383. return ret;
  384. if (rt_completion_wait(&(psPortDev->utr_completion), timeouts) != 0)
  385. {
  386. LOG_D("Request %08x Timeout in %d ms", psUTR, timeouts);
  387. usbh_quit_utr(psUTR);
  388. rt_completion_init(&(psPortDev->utr_completion));
  389. rt_thread_mdelay(1);
  390. }
  391. else
  392. {
  393. LOG_D("Transferring done %08x", psUTR);
  394. usbh_quit_utr(psUTR);
  395. break;
  396. }
  397. }
  398. return 0;
  399. }
  400. static void xfer_done_cb(UTR_T *psUTR)
  401. {
  402. S_NU_PORT_DEV *psPortDev = (S_NU_PORT_DEV *)psUTR->context;
  403. //transfer done, signal utr_completion
  404. rt_completion_done(&(psPortDev->utr_completion));
  405. }
  406. static int nu_pipe_xfer(upipe_t pipe, rt_uint8_t token, void *buffer, int nbytes, int timeouts)
  407. {
  408. S_NU_RH_PORT_CTRL *psPortCtrl;
  409. S_NU_PORT_DEV *psPortDev;
  410. UTR_T *psUTR = NULL;
  411. int i32XferLen = -1;
  412. void *buffer_nonch = buffer;
  413. NU_USBHOST_LOCK();
  414. psPortCtrl = GetRHPortControlFromPipe(pipe);
  415. if (psPortCtrl == RT_NULL)
  416. {
  417. goto exit_nu_pipe_xfer;
  418. }
  419. psPortDev = GetPortDevFromPipe(pipe);
  420. if (psPortDev->pUDev == NULL)
  421. {
  422. LOG_D("nu_pipe_xfer ERROR: udev not found");
  423. goto exit_nu_pipe_xfer;
  424. }
  425. //ctrl xfer
  426. if (pipe->ep.bmAttributes == USB_EP_ATTR_CONTROL)
  427. {
  428. int ret;
  429. if (token == USBH_PID_SETUP)
  430. {
  431. struct urequest *psSetup = (struct urequest *)buffer_nonch;
  432. RT_ASSERT(buffer_nonch != RT_NULL);
  433. psPortCtrl->asHubPortDev->u32SentLength[pipe->pipe_index] = 0;
  434. /* Read data from USB device. */
  435. if (psSetup->request_type & USB_REQ_TYPE_DIR_IN)
  436. {
  437. //Store setup request
  438. rt_memcpy(&psPortCtrl->asHubPortDev->asSetupReq[pipe->pipe_index], psSetup, sizeof(struct urequest));
  439. }
  440. else
  441. {
  442. /* Write data to USB device. */
  443. //Trigger USBHostLib Ctrl_Xfer
  444. ret = nu_ctrl_xfer(psPortDev, psSetup, NULL, timeouts);
  445. if (ret != psSetup->wLength)
  446. goto exit_nu_pipe_xfer;
  447. }
  448. }
  449. else
  450. {
  451. //token == USBH_PID_DATA
  452. if (buffer_nonch && ((pipe->ep.bEndpointAddress & USB_DIR_MASK) == USB_DIR_IN))
  453. {
  454. struct urequest *psSetup = &psPortCtrl->asHubPortDev->asSetupReq[pipe->pipe_index];
  455. /* Read data from USB device. */
  456. //Trigger USBHostLib Ctril_Xfer
  457. /*
  458. * Workaround: HCD driver can readback all bytes of setup.wLength, but not support single packet transferring.
  459. */
  460. if (psPortCtrl->asHubPortDev->u32SentLength[pipe->pipe_index] == 0)
  461. {
  462. ret = nu_ctrl_xfer(psPortDev, psSetup, buffer_nonch, timeouts);
  463. psPortCtrl->asHubPortDev->u32SentLength[pipe->pipe_index] = ret;
  464. }
  465. else
  466. {
  467. if (psPortCtrl->asHubPortDev->u32SentLength[pipe->pipe_index] < nbytes)
  468. {
  469. ret = 0;
  470. }
  471. else
  472. {
  473. psPortCtrl->asHubPortDev->u32SentLength[pipe->pipe_index] -= nbytes;
  474. ret = nbytes;
  475. }
  476. }
  477. if (ret <= 0)
  478. goto exit_nu_pipe_xfer;
  479. }
  480. else
  481. {
  482. LOG_D("%d == USBH_PID_DATA, nil buf-%d", token, nbytes);
  483. }
  484. } //else
  485. i32XferLen = nbytes;
  486. goto exit_nu_pipe_xfer;
  487. } // if ( pipe->ep.bmAttributes == USB_EP_ATTR_CONTROL )
  488. else
  489. {
  490. psUTR = alloc_utr(psPortDev->pUDev);
  491. if (!psUTR)
  492. {
  493. LOG_D("nu_pipe_xfer ERROR: unable alloc UTR");
  494. goto exit_nu_pipe_xfer;
  495. }
  496. psUTR->ep = psPortDev->apsEPInfo[pipe->pipe_index];
  497. psUTR->buff = buffer_nonch;
  498. psUTR->data_len = nbytes;
  499. psUTR->xfer_len = 0;
  500. psUTR->func = xfer_done_cb;
  501. psUTR->context = psPortDev;
  502. psUTR->bIsTransferDone = 0;
  503. psUTR->status = 0;
  504. //others xfer
  505. rt_completion_init(&(psPortDev->utr_completion));
  506. if (pipe->ep.bmAttributes == USB_EP_ATTR_BULK)
  507. {
  508. if (nu_bulk_xfer(psPortDev, psUTR, timeouts) < 0)
  509. {
  510. LOG_D("nu_pipe_xfer ERROR: bulk transfer failed");
  511. goto failreport_nu_pipe_xfer;
  512. }
  513. }
  514. else if (pipe->ep.bmAttributes == USB_EP_ATTR_INT)
  515. {
  516. if (nu_int_xfer(pipe, psPortDev, psUTR, timeouts) < 0)
  517. {
  518. LOG_D("nu_pipe_xfer ERROR: int transfer failed");
  519. //goto exit_nu_pipe_xfer;
  520. }
  521. else
  522. {
  523. i32XferLen = nbytes;
  524. }
  525. goto exit_nu_pipe_xfer;
  526. }
  527. else if (pipe->ep.bmAttributes == USB_EP_ATTR_ISOC)
  528. {
  529. //TODO: ISO transfer
  530. LOG_D("nu_pipe_xfer ERROR: isoc transfer not support");
  531. goto exit_nu_pipe_xfer;
  532. }
  533. } //else
  534. failreport_nu_pipe_xfer:
  535. if (psUTR->bIsTransferDone == 0)
  536. {
  537. //Timeout
  538. LOG_D("nu_pipe_xfer ERROR: timeout");
  539. pipe->status = UPIPE_STATUS_ERROR;
  540. usbh_quit_utr(psUTR);
  541. }
  542. else
  543. {
  544. // Transfer Done. Get status
  545. if (psUTR->status == 0)
  546. {
  547. pipe->status = UPIPE_STATUS_OK;
  548. }
  549. else if (psUTR->status == USBH_ERR_STALL)
  550. {
  551. pipe->status = UPIPE_STATUS_STALL;
  552. }
  553. else
  554. {
  555. pipe->status = UPIPE_STATUS_ERROR;
  556. }
  557. }
  558. i32XferLen = psUTR->xfer_len;
  559. exit_nu_pipe_xfer:
  560. //Call callback
  561. if (pipe->callback != RT_NULL)
  562. {
  563. pipe->callback(pipe);
  564. }
  565. if (psUTR)
  566. free_utr(psUTR);
  567. NU_USBHOST_UNLOCK();
  568. return i32XferLen;
  569. }
  570. /* Polling USB root hub status task */
  571. static void nu_usbh_rh_thread_entry(void *parameter)
  572. {
  573. while (1)
  574. {
  575. NU_USBHOST_LOCK();
  576. usbh_polling_root_hubs();
  577. NU_USBHOST_UNLOCK();
  578. rt_thread_mdelay(NU_USBHOST_HUB_POLLING_INTERVAL);
  579. }
  580. }
  581. static void nu_hcd_connect_callback(
  582. struct udev_t *udev,
  583. int param)
  584. {
  585. int i;
  586. int port_index;
  587. S_NU_RH_PORT_CTRL *psPortCtrl;
  588. for (i = 0; i < NU_MAX_USBH_PORT; i++)
  589. {
  590. psPortCtrl = &s_sUSBHDev.asPortCtrl[i];
  591. if (psPortCtrl->sRHPortDev.pUDev == NULL)
  592. break;
  593. }
  594. if (i >= NU_MAX_USBH_PORT)
  595. {
  596. LOG_D("ERROR: port connect slot is full");
  597. return;
  598. }
  599. port_index = i + 1;
  600. psPortCtrl->sRHPortDev.pUDev = udev;
  601. psPortCtrl->sRHPortDev.bRHParent = TRUE;
  602. LOG_D("usb connected");
  603. if (udev->speed == SPEED_HIGH)
  604. rt_usbh_root_hub_connect_handler(&s_sUSBHDev.uhcd, port_index, RT_TRUE);
  605. else
  606. rt_usbh_root_hub_connect_handler(&s_sUSBHDev.uhcd, port_index, RT_FALSE);
  607. }
  608. static void nu_hcd_disconnect_callback(
  609. struct udev_t *udev,
  610. int param)
  611. {
  612. int i;
  613. int port_index;
  614. S_NU_RH_PORT_CTRL *psPortCtrl;
  615. for (i = 0; i < NU_MAX_USBH_PORT; i++)
  616. {
  617. psPortCtrl = &s_sUSBHDev.asPortCtrl[i];
  618. if (psPortCtrl->sRHPortDev.pUDev == udev)
  619. break;
  620. }
  621. if (i >= NU_MAX_USBH_PORT)
  622. {
  623. LOG_D("ERROR: udev not found");
  624. return;
  625. }
  626. port_index = i + 1;
  627. for (i = 0; i < NU_MAX_USBH_PIPE; i++)
  628. {
  629. if (psPortCtrl->sRHPortDev.apsEPInfo[i] != NULL)
  630. {
  631. usbh_quit_xfer(psPortCtrl->sRHPortDev.pUDev, psPortCtrl->sRHPortDev.apsEPInfo[i]);
  632. }
  633. }
  634. psPortCtrl->sRHPortDev.pUDev = NULL;
  635. LOG_D("usb disconnect");
  636. rt_usbh_root_hub_disconnect_handler(&s_sUSBHDev.uhcd, port_index);
  637. }
  638. /* USB host operations -----------------------------------------------------------*/
  639. static struct uhcd_ops nu_uhcd_ops =
  640. {
  641. nu_reset_port,
  642. nu_pipe_xfer,
  643. nu_open_pipe,
  644. nu_close_pipe,
  645. };
  646. static rt_err_t nu_hcd_init(rt_device_t device)
  647. {
  648. struct nu_usbh_dev *pNuUSBHDev = (struct nu_usbh_dev *)device;
  649. usbh_core_init();
  650. //install connect/disconnect callback
  651. usbh_install_conn_callback(nu_hcd_connect_callback, nu_hcd_disconnect_callback);
  652. //create thread for polling usbh port status
  653. /* create usb hub thread */
  654. pNuUSBHDev->polling_thread = rt_thread_create("usbh_drv", nu_usbh_rh_thread_entry, RT_NULL,
  655. NU_USBH_THREAD_STACK_SIZE, 8, 20);
  656. RT_ASSERT(pNuUSBHDev->polling_thread != RT_NULL);
  657. /* startup usb host thread */
  658. rt_thread_startup(pNuUSBHDev->polling_thread);
  659. return RT_EOK;
  660. }
  661. /* global function for USB host library -----------------------------*/
  662. uint32_t usbh_get_ticks(void)
  663. {
  664. return rt_tick_get();
  665. }
  666. void usbh_delay_ms(int msec)
  667. {
  668. rt_thread_mdelay(msec);
  669. }
  670. uint32_t usbh_tick_from_millisecond(uint32_t msec)
  671. {
  672. return rt_tick_from_millisecond(msec);
  673. }
  674. #if defined(RT_USING_PM)
  675. /* device pm suspend() entry. */
  676. static int usbhost_pm_suspend(const struct rt_device *device, rt_uint8_t mode)
  677. {
  678. rt_err_t result;
  679. struct nu_usbh_dev *pNuUSBHDev = (struct nu_usbh_dev *)device;
  680. RT_ASSERT(pNuUSBHDev != RT_NULL);
  681. switch (mode)
  682. {
  683. case PM_SLEEP_MODE_LIGHT:
  684. case PM_SLEEP_MODE_DEEP:
  685. pNuUSBHDev->polling_thread->stat = RT_THREAD_READY;
  686. result = rt_thread_suspend(pNuUSBHDev->polling_thread);
  687. RT_ASSERT(result == RT_EOK);
  688. break;
  689. default:
  690. break;
  691. }
  692. return (int)RT_EOK;
  693. }
  694. /* device pm resume() entry. */
  695. static void usbhost_pm_resume(const struct rt_device *device, rt_uint8_t mode)
  696. {
  697. rt_err_t result;
  698. struct nu_usbh_dev *pNuUSBHDev = (struct nu_usbh_dev *)device;
  699. RT_ASSERT(pNuUSBHDev != RT_NULL);
  700. switch (mode)
  701. {
  702. case PM_SLEEP_MODE_LIGHT:
  703. case PM_SLEEP_MODE_DEEP:
  704. result = rt_thread_resume(pNuUSBHDev->polling_thread);
  705. RT_ASSERT(result == RT_EOK);
  706. break;
  707. default:
  708. break;
  709. }
  710. }
  711. static struct rt_device_pm_ops device_pm_ops =
  712. {
  713. .suspend = usbhost_pm_suspend,
  714. .resume = usbhost_pm_resume,
  715. .frequency_change = RT_NULL
  716. };
  717. #endif
  718. int nu_usbh_register(void)
  719. {
  720. rt_err_t res;
  721. uhcd_t psUHCD;
  722. psUHCD = (uhcd_t)&s_sUSBHDev.uhcd;
  723. psUHCD->parent.type = RT_Device_Class_USBHost;
  724. psUHCD->parent.init = nu_hcd_init;
  725. psUHCD->parent.user_data = &s_sUSBHDev;
  726. psUHCD->ops = &nu_uhcd_ops;
  727. psUHCD->num_ports = NU_MAX_USBH_PORT;
  728. #if !defined(BSP_USING_HSOTG)
  729. {
  730. uint32_t u32RegLockBackup;
  731. u32RegLockBackup = SYS_IsRegLocked();
  732. if (u32RegLockBackup)
  733. SYS_UnlockReg();
  734. #if defined(BSP_USING_HSUSBH)
  735. /* Set USB Host role */
  736. SYS->USBPHY = (SYS->USBPHY & ~SYS_USBPHY_HSUSBROLE_Msk) | (0x1u << SYS_USBPHY_HSUSBROLE_Pos);
  737. SYS->USBPHY |= SYS_USBPHY_HSUSBEN_Msk | SYS_USBPHY_SBO_Msk;
  738. rt_thread_delay(20);
  739. SYS->USBPHY |= SYS_USBPHY_HSUSBACT_Msk;
  740. #endif
  741. #if defined(BSP_USING_USBH)
  742. /* Set USB Host role */
  743. SYS->USBPHY = (SYS->USBPHY & ~SYS_USBPHY_USBROLE_Msk) | (0x1u << SYS_USBPHY_USBROLE_Pos);
  744. SYS->USBPHY |= SYS_USBPHY_USBEN_Msk | SYS_USBPHY_SBO_Msk ;
  745. #endif
  746. if (u32RegLockBackup)
  747. SYS_LockReg();
  748. }
  749. #endif
  750. NU_USBHOST_MUTEX_INIT();
  751. res = rt_device_register(&psUHCD->parent, "usbh", RT_DEVICE_FLAG_DEACTIVATE);
  752. RT_ASSERT(res == RT_EOK);
  753. /*initialize the usb host function */
  754. res = rt_usb_host_init("usbh");
  755. RT_ASSERT(res == RT_EOK);
  756. #if defined(RT_USING_PM)
  757. rt_pm_device_register(&psUHCD->parent, &device_pm_ops);
  758. #endif
  759. return 0;
  760. }
  761. INIT_APP_EXPORT(nu_usbh_register);
  762. #endif