drv_usbhost.c 25 KB

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