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