drv_usbd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-12-04 ZYH first implementation
  9. */
  10. #include <usb/include/usb_device_config.h>
  11. #include <usb/include/usb.h>
  12. #include <rtthread.h>
  13. #include <usb/phy/usb_phy.h>
  14. #include <usb/device/usb_device.h>
  15. #include <usb/device/usb_device_dci.h>
  16. #include <rtdevice.h>
  17. #ifdef RT_USING_EHCI0_AS_DEVICE
  18. #ifdef RT_USING_EHCI1_AS_DEVICE
  19. #error Can not using 2 controller as usb device
  20. #endif
  21. #endif
  22. #define FSL_USB_HS
  23. /* USB PHY condfiguration */
  24. #define BOARD_USB_PHY_D_CAL (0x0CU)
  25. #define BOARD_USB_PHY_TXCAL45DP (0x06U)
  26. #define BOARD_USB_PHY_TXCAL45DM (0x06U)
  27. #define BOARD_XTAL0_CLK_HZ 24000000U
  28. #ifdef RT_USING_EHCI0_AS_DEVICE
  29. static usb_device_handle ehci0_handle;
  30. static struct udcd _fsl_udc_0;
  31. #endif
  32. #ifdef RT_USING_EHCI1_AS_DEVICE
  33. static usb_device_handle ehci1_handle;
  34. static struct udcd _fsl_udc_1;
  35. #endif
  36. static usb_status_t usb_device_callback(usb_device_handle handle, uint32_t callbackEvent, void *eventParam);
  37. static usb_status_t usb_device_endpoint_callback(usb_device_handle handle, usb_device_endpoint_callback_message_struct_t *message, void *callbackParam);
  38. void USB_DeviceIsrEnable(uint8_t controllerId)
  39. {
  40. uint8_t irqNumber;
  41. #if defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U)
  42. uint8_t usbDeviceEhciIrq[] = USBHS_IRQS;
  43. irqNumber = usbDeviceEhciIrq[controllerId - kUSB_ControllerEhci0];
  44. #endif
  45. /* Install isr, set priority, and enable IRQ. */
  46. #if defined(__GIC_PRIO_BITS)
  47. GIC_SetPriority((IRQn_Type)irqNumber, 3);
  48. #else
  49. NVIC_SetPriority((IRQn_Type)irqNumber, 3);
  50. #endif
  51. EnableIRQ((IRQn_Type)irqNumber);
  52. }
  53. void USB_DeviceClockInit(uint8_t controllerId)
  54. {
  55. #if defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U)
  56. usb_phy_config_struct_t phyConfig = {
  57. BOARD_USB_PHY_D_CAL, BOARD_USB_PHY_TXCAL45DP, BOARD_USB_PHY_TXCAL45DM,
  58. };
  59. #endif
  60. #if defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U)
  61. if (controllerId == kUSB_ControllerEhci0)
  62. {
  63. CLOCK_EnableUsbhs0PhyPllClock(kCLOCK_Usbphy480M, 480000000U);
  64. CLOCK_EnableUsbhs0Clock(kCLOCK_Usb480M, 480000000U);
  65. }
  66. else
  67. {
  68. CLOCK_EnableUsbhs1PhyPllClock(kCLOCK_Usbphy480M, 480000000U);
  69. CLOCK_EnableUsbhs1Clock(kCLOCK_Usb480M, 480000000U);
  70. }
  71. USB_EhciPhyInit(controllerId, BOARD_XTAL0_CLK_HZ, &phyConfig);
  72. #endif
  73. }
  74. #ifdef RT_USING_EHCI0_AS_DEVICE
  75. #ifdef FSL_USB_HS
  76. static struct ep_id _ehci0_ep_pool[] =
  77. {
  78. {0x0, USB_EP_ATTR_CONTROL, USB_DIR_INOUT, 64, ID_ASSIGNED },
  79. {0x1, USB_EP_ATTR_BULK, USB_DIR_IN, 512, ID_UNASSIGNED},
  80. {0x1, USB_EP_ATTR_BULK, USB_DIR_OUT, 512, ID_UNASSIGNED},
  81. {0x2, USB_EP_ATTR_INT, USB_DIR_IN, 512, ID_UNASSIGNED},
  82. {0x2, USB_EP_ATTR_INT, USB_DIR_OUT, 512, ID_UNASSIGNED},
  83. {0x3, USB_EP_ATTR_BULK, USB_DIR_IN, 512, ID_UNASSIGNED},
  84. {0x3, USB_EP_ATTR_BULK, USB_DIR_OUT, 512, ID_UNASSIGNED},
  85. {0x4, USB_EP_ATTR_INT, USB_DIR_IN, 512, ID_UNASSIGNED},
  86. {0x4, USB_EP_ATTR_INT, USB_DIR_OUT, 512, ID_UNASSIGNED},
  87. {0x5, USB_EP_ATTR_BULK, USB_DIR_IN, 512, ID_UNASSIGNED},
  88. {0x5, USB_EP_ATTR_BULK, USB_DIR_OUT, 512, ID_UNASSIGNED},
  89. {0x6, USB_EP_ATTR_INT, USB_DIR_IN, 512, ID_UNASSIGNED},
  90. {0x6, USB_EP_ATTR_INT, USB_DIR_OUT, 512, ID_UNASSIGNED},
  91. {0x7, USB_EP_ATTR_BULK, USB_DIR_IN, 512, ID_UNASSIGNED},
  92. {0x7, USB_EP_ATTR_BULK, USB_DIR_OUT, 512, ID_UNASSIGNED},
  93. {0xFF, USB_EP_ATTR_TYPE_MASK, USB_DIR_MASK, 0, ID_ASSIGNED },
  94. };
  95. #else
  96. static struct ep_id _ehci0_ep_pool[] =
  97. {
  98. {0x0, USB_EP_ATTR_CONTROL, USB_DIR_INOUT, 64, ID_ASSIGNED },
  99. {0x1, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  100. {0x1, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  101. {0x2, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED},
  102. {0x2, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED},
  103. {0x3, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  104. {0x3, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  105. {0x4, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED},
  106. {0x4, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED},
  107. {0x5, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  108. {0x5, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  109. {0x6, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED},
  110. {0x6, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED},
  111. {0x7, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  112. {0x7, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  113. {0xFF, USB_EP_ATTR_TYPE_MASK, USB_DIR_MASK, 0, ID_ASSIGNED },
  114. };
  115. #endif
  116. void USB_OTG1_IRQHandler(void)
  117. {
  118. /* enter interrupt */
  119. rt_interrupt_enter();
  120. USB_DeviceEhciIsrFunction(ehci0_handle);
  121. /* leave interrupt */
  122. rt_interrupt_leave();
  123. }
  124. static rt_err_t _ehci0_ep_set_stall(rt_uint8_t address)
  125. {
  126. USB_DeviceStallEndpoint(ehci0_handle, address);
  127. return RT_EOK;
  128. }
  129. static rt_err_t _ehci0_ep_clear_stall(rt_uint8_t address)
  130. {
  131. USB_DeviceUnstallEndpoint(ehci0_handle, address);
  132. return RT_EOK;
  133. }
  134. static rt_err_t _ehci0_set_address(rt_uint8_t address)
  135. {
  136. USB_DeviceSetStatus(ehci0_handle, kUSB_DeviceStatusAddress, &address);
  137. return RT_EOK;
  138. }
  139. static rt_err_t _ehci0_set_config(rt_uint8_t address)
  140. {
  141. return RT_EOK;
  142. }
  143. static rt_err_t _ehci0_ep_enable(uep_t ep)
  144. {
  145. usb_device_endpoint_init_struct_t ep_init;
  146. usb_device_endpoint_callback_struct_t ep_callback;
  147. rt_uint32_t param = ep->ep_desc->bEndpointAddress;
  148. RT_ASSERT(ep != RT_NULL);
  149. RT_ASSERT(ep->ep_desc != RT_NULL);
  150. ep_init.maxPacketSize = ep->ep_desc->wMaxPacketSize;
  151. ep_init.endpointAddress = ep->ep_desc->bEndpointAddress;
  152. ep_init.transferType = ep->ep_desc->bmAttributes;
  153. ep_init.zlt = 0;
  154. ep_callback.callbackFn = usb_device_endpoint_callback;
  155. ep_callback.callbackParam = (void *)param;
  156. ep_callback.isBusy = 0;
  157. USB_DeviceInitEndpoint(ehci0_handle,&ep_init,&ep_callback);
  158. return RT_EOK;
  159. }
  160. static rt_err_t _ehci0_ep_disable(uep_t ep)
  161. {
  162. RT_ASSERT(ep != RT_NULL);
  163. RT_ASSERT(ep->ep_desc != RT_NULL);
  164. USB_DeviceDeinitEndpoint(ehci0_handle, ep->ep_desc->bEndpointAddress);
  165. return RT_EOK;
  166. }
  167. static rt_size_t _ehci0_ep_read(rt_uint8_t address, void *buffer)
  168. {
  169. rt_size_t size = 0;
  170. RT_ASSERT(buffer != RT_NULL);
  171. return size;
  172. }
  173. static rt_size_t _ehci0_ep_read_prepare(rt_uint8_t address, void *buffer, rt_size_t size)
  174. {
  175. USB_DeviceRecvRequest(ehci0_handle, address, buffer, size);
  176. return size;
  177. }
  178. static rt_size_t _ehci0_ep_write(rt_uint8_t address, void *buffer, rt_size_t size)
  179. {
  180. USB_DeviceSendRequest(ehci0_handle, address, buffer, size);
  181. return size;
  182. }
  183. static rt_err_t _ehci0_ep0_send_status(void)
  184. {
  185. _ehci0_ep_write(0x00, NULL, 0);
  186. return RT_EOK;
  187. }
  188. static rt_err_t _ehci0_suspend(void)
  189. {
  190. return RT_EOK;
  191. }
  192. static rt_err_t _ehci0_wakeup(void)
  193. {
  194. return RT_EOK;
  195. }
  196. const static struct udcd_ops _ehci0_udc_ops =
  197. {
  198. _ehci0_set_address,
  199. _ehci0_set_config,
  200. _ehci0_ep_set_stall,
  201. _ehci0_ep_clear_stall,
  202. _ehci0_ep_enable,
  203. _ehci0_ep_disable,
  204. _ehci0_ep_read_prepare,
  205. _ehci0_ep_read,
  206. _ehci0_ep_write,
  207. _ehci0_ep0_send_status,
  208. _ehci0_suspend,
  209. _ehci0_wakeup,
  210. };
  211. static rt_err_t drv_ehci0_usbd_init(rt_device_t device)
  212. {
  213. usb_status_t result;
  214. USB_DeviceClockInit(kUSB_ControllerEhci0);
  215. result = USB_DeviceInit(kUSB_ControllerEhci0,usb_device_callback,&ehci0_handle);
  216. RT_ASSERT(ehci0_handle);
  217. if(result == kStatus_USB_Success)
  218. {
  219. USB_DeviceIsrEnable(kUSB_ControllerEhci0);
  220. USB_DeviceRun(ehci0_handle);
  221. }
  222. else
  223. {
  224. rt_kprintf("USB_DeviceInit ehci0 error\r\n");
  225. return RT_ERROR;
  226. }
  227. return RT_EOK;
  228. }
  229. #endif
  230. #ifdef RT_USING_EHCI1_AS_DEVICE
  231. #ifdef FSL_USB_HS
  232. static struct ep_id _ehci1_ep_pool[] =
  233. {
  234. {0x0, USB_EP_ATTR_CONTROL, USB_DIR_INOUT, 64, ID_ASSIGNED },
  235. {0x1, USB_EP_ATTR_BULK, USB_DIR_IN, 512, ID_UNASSIGNED},
  236. {0x1, USB_EP_ATTR_BULK, USB_DIR_OUT, 512, ID_UNASSIGNED},
  237. {0x2, USB_EP_ATTR_INT, USB_DIR_IN, 512, ID_UNASSIGNED},
  238. {0x2, USB_EP_ATTR_INT, USB_DIR_OUT, 512, ID_UNASSIGNED},
  239. {0x3, USB_EP_ATTR_BULK, USB_DIR_IN, 512, ID_UNASSIGNED},
  240. {0x3, USB_EP_ATTR_BULK, USB_DIR_OUT, 512, ID_UNASSIGNED},
  241. {0x4, USB_EP_ATTR_INT, USB_DIR_IN, 512, ID_UNASSIGNED},
  242. {0x4, USB_EP_ATTR_INT, USB_DIR_OUT, 512, ID_UNASSIGNED},
  243. {0x5, USB_EP_ATTR_BULK, USB_DIR_IN, 512, ID_UNASSIGNED},
  244. {0x5, USB_EP_ATTR_BULK, USB_DIR_OUT, 512, ID_UNASSIGNED},
  245. {0x6, USB_EP_ATTR_INT, USB_DIR_IN, 512, ID_UNASSIGNED},
  246. {0x6, USB_EP_ATTR_INT, USB_DIR_OUT, 512, ID_UNASSIGNED},
  247. {0x7, USB_EP_ATTR_BULK, USB_DIR_IN, 512, ID_UNASSIGNED},
  248. {0x7, USB_EP_ATTR_BULK, USB_DIR_OUT, 512, ID_UNASSIGNED},
  249. {0xFF, USB_EP_ATTR_TYPE_MASK, USB_DIR_MASK, 0, ID_ASSIGNED },
  250. };
  251. #else
  252. static struct ep_id _ehci1_ep_pool[] =
  253. {
  254. {0x0, USB_EP_ATTR_CONTROL, USB_DIR_INOUT, 64, ID_ASSIGNED },
  255. {0x1, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  256. {0x1, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  257. {0x2, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED},
  258. {0x2, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED},
  259. {0x3, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  260. {0x3, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  261. {0x4, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED},
  262. {0x4, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED},
  263. {0x5, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  264. {0x5, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  265. {0x6, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED},
  266. {0x6, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED},
  267. {0x7, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  268. {0x7, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  269. {0xFF, USB_EP_ATTR_TYPE_MASK, USB_DIR_MASK, 0, ID_ASSIGNED },
  270. };
  271. #endif
  272. void USB_OTG2_IRQHandler(void)
  273. {
  274. /* enter interrupt */
  275. rt_interrupt_enter();
  276. USB_DeviceEhciIsrFunction(ehci1_handle);
  277. /* leave interrupt */
  278. rt_interrupt_leave();
  279. }
  280. static rt_err_t _ehci1_ep_set_stall(rt_uint8_t address)
  281. {
  282. USB_DeviceStallEndpoint(ehci1_handle, address);
  283. return RT_EOK;
  284. }
  285. static rt_err_t _ehci1_ep_clear_stall(rt_uint8_t address)
  286. {
  287. USB_DeviceUnstallEndpoint(ehci1_handle, address);
  288. return RT_EOK;
  289. }
  290. static rt_err_t _ehci1_set_address(rt_uint8_t address)
  291. {
  292. USB_DeviceSetStatus(ehci1_handle, kUSB_DeviceStatusAddress, &address);
  293. return RT_EOK;
  294. }
  295. static rt_err_t _ehci1_set_config(rt_uint8_t address)
  296. {
  297. return RT_EOK;
  298. }
  299. static rt_err_t _ehci1_ep_enable(uep_t ep)
  300. {
  301. usb_device_endpoint_init_struct_t ep_init;
  302. usb_device_endpoint_callback_struct_t ep_callback;
  303. RT_ASSERT(ep != RT_NULL);
  304. RT_ASSERT(ep->ep_desc != RT_NULL);
  305. ep_init.maxPacketSize = ep->ep_desc->wMaxPacketSize;
  306. ep_init.endpointAddress = ep->ep_desc->bEndpointAddress;
  307. ep_init.transferType = ep->ep_desc->bmAttributes;
  308. ep_init.zlt = 0;
  309. ep_callback.callbackFn = usb_device_endpoint_callback;
  310. ep_callback.callbackParam = (void *)ep_init.endpointAddress;
  311. ep_callback.isBusy = 0;
  312. USB_DeviceInitEndpoint(ehci1_handle,&ep_init,&ep_callback);
  313. return RT_EOK;
  314. }
  315. static rt_err_t _ehci1_ep_disable(uep_t ep)
  316. {
  317. RT_ASSERT(ep != RT_NULL);
  318. RT_ASSERT(ep->ep_desc != RT_NULL);
  319. USB_DeviceDeinitEndpoint(ehci1_handle, ep->ep_desc->bEndpointAddress);
  320. return RT_EOK;
  321. }
  322. static rt_size_t _ehci1_ep_read(rt_uint8_t address, void *buffer)
  323. {
  324. rt_size_t size = 0;
  325. RT_ASSERT(buffer != RT_NULL);
  326. return size;
  327. }
  328. static rt_size_t _ehci1_ep_read_prepare(rt_uint8_t address, void *buffer, rt_size_t size)
  329. {
  330. USB_DeviceRecvRequest(ehci1_handle, address, buffer, size);
  331. return size;
  332. }
  333. static rt_size_t _ehci1_ep_write(rt_uint8_t address, void *buffer, rt_size_t size)
  334. {
  335. USB_DeviceSendRequest(ehci1_handle, address, buffer, size);
  336. return size;
  337. }
  338. static rt_err_t _ehci1_ep0_send_status(void)
  339. {
  340. _ehci1_ep_write(0x00, NULL, 0);
  341. return RT_EOK;
  342. }
  343. static rt_err_t _ehci1_suspend(void)
  344. {
  345. return RT_EOK;
  346. }
  347. static rt_err_t _ehci1_wakeup(void)
  348. {
  349. return RT_EOK;
  350. }
  351. const static struct udcd_ops _ehci1_udc_ops =
  352. {
  353. _ehci1_set_address,
  354. _ehci1_set_config,
  355. _ehci1_ep_set_stall,
  356. _ehci1_ep_clear_stall,
  357. _ehci1_ep_enable,
  358. _ehci1_ep_disable,
  359. _ehci1_ep_read_prepare,
  360. _ehci1_ep_read,
  361. _ehci1_ep_write,
  362. _ehci1_ep0_send_status,
  363. _ehci1_suspend,
  364. _ehci1_wakeup,
  365. };
  366. static rt_err_t drv_ehci1_usbd_init(rt_device_t device)
  367. {
  368. usb_status_t result;
  369. USB_DeviceClockInit(kUSB_ControllerEhci1);
  370. result = USB_DeviceInit(kUSB_ControllerEhci1,usb_device_callback,&ehci1_handle);
  371. RT_ASSERT(ehci1_handle);
  372. if(result == kStatus_USB_Success)
  373. {
  374. USB_DeviceIsrEnable(kUSB_ControllerEhci1);
  375. USB_DeviceRun(ehci1_handle);
  376. }
  377. else
  378. {
  379. rt_kprintf("USB_DeviceInit ehci1 error\r\n");
  380. return RT_ERROR;
  381. }
  382. return RT_EOK;
  383. }
  384. #endif
  385. #if defined(RT_USING_EHCI0_AS_DEVICE) && defined(RT_USING_EHCI1_AS_DEVICE)
  386. #error Can not using both now
  387. #endif
  388. static int rt_usbd_init(void)
  389. {
  390. #ifdef RT_USING_EHCI0_AS_DEVICE
  391. rt_memset((void *)&_fsl_udc_0, 0, sizeof(struct udcd));
  392. _fsl_udc_0.parent.type = RT_Device_Class_USBDevice;
  393. _fsl_udc_0.parent.init = drv_ehci0_usbd_init;
  394. _fsl_udc_0.ops = &_ehci0_udc_ops;
  395. /* Register endpoint infomation */
  396. _fsl_udc_0.ep_pool = _ehci0_ep_pool;
  397. _fsl_udc_0.ep0.id = &_ehci0_ep_pool[0];
  398. #ifdef FSL_USB_HS
  399. _fsl_udc_0.device_is_hs = RT_TRUE;
  400. #else
  401. _fsl_udc_0.device_is_hs = RT_FALSE;
  402. #endif
  403. rt_device_register((rt_device_t)&_fsl_udc_0, "usbd", 0);
  404. rt_usb_device_init();
  405. #endif
  406. #ifdef RT_USING_EHCI1_AS_DEVICE
  407. rt_memset((void *)&_fsl_udc_1, 0, sizeof(struct udcd));
  408. _fsl_udc_1.parent.type = RT_Device_Class_USBDevice;
  409. _fsl_udc_1.parent.init = drv_ehci1_usbd_init;
  410. _fsl_udc_1.ops = &_ehci1_udc_ops;
  411. /* Register endpoint infomation */
  412. _fsl_udc_1.ep_pool = _ehci1_ep_pool;
  413. _fsl_udc_1.ep0.id = &_ehci1_ep_pool[0];
  414. #ifdef FSL_USB_HS
  415. _fsl_udc_1.device_is_hs = RT_TRUE;
  416. #else
  417. _fsl_udc_1.device_is_hs = RT_FALSE;
  418. #endif
  419. rt_device_register((rt_device_t)&_fsl_udc_1, "usbd", 0);
  420. rt_usb_device_init();
  421. #endif
  422. return 0;
  423. }
  424. INIT_DEVICE_EXPORT(rt_usbd_init);
  425. static usb_status_t usb_device_endpoint_callback(usb_device_handle handle, usb_device_endpoint_callback_message_struct_t *message, void *callbackParam)
  426. {
  427. rt_uint32_t ep_addr = (rt_uint32_t)callbackParam;
  428. usb_device_struct_t *deviceHandle = (usb_device_struct_t *)handle;
  429. udcd_t udcd = RT_NULL;
  430. uint8_t state;
  431. if(deviceHandle->controllerId == kUSB_ControllerEhci0)
  432. {
  433. #ifdef RT_USING_EHCI0_AS_DEVICE
  434. udcd = &_fsl_udc_0;
  435. #endif
  436. }
  437. else
  438. {
  439. #ifdef RT_USING_EHCI1_AS_DEVICE
  440. udcd = &_fsl_udc_1;
  441. #endif
  442. }
  443. if(message->isSetup)
  444. {
  445. //rt_kprintf("1udcd:%#08X\n",udcd);
  446. rt_usbd_ep0_setup_handler(udcd, (struct urequest*)message->buffer);
  447. }
  448. else if(ep_addr == 0x00)
  449. {
  450. USB_DeviceGetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
  451. if(state == kUSB_DeviceStateAddressing)
  452. {
  453. if (kStatus_USB_Success == USB_DeviceSetStatus(handle, kUSB_DeviceStatusAddress, NULL))
  454. {
  455. state = kUSB_DeviceStateAddress;
  456. USB_DeviceSetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
  457. }
  458. }
  459. //rt_kprintf("2udcd:%#08X\n",udcd);
  460. rt_usbd_ep0_out_handler(udcd,message->length);
  461. }
  462. else if(ep_addr == 0x80)
  463. {
  464. USB_DeviceGetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
  465. if(state == kUSB_DeviceStateAddressing)
  466. {
  467. if (kStatus_USB_Success == USB_DeviceSetStatus(handle, kUSB_DeviceStatusAddress, NULL))
  468. {
  469. state = kUSB_DeviceStateAddress;
  470. USB_DeviceSetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
  471. }
  472. }
  473. //rt_kprintf("3udcd:%#08X\n",udcd);
  474. rt_usbd_ep0_in_handler(udcd);
  475. }
  476. else if(ep_addr&0x80)
  477. {
  478. rt_usbd_ep_in_handler(udcd, ep_addr, message->length);
  479. }
  480. else
  481. {
  482. rt_usbd_ep_out_handler(udcd, ep_addr, message->length);
  483. }
  484. return kStatus_USB_Success;
  485. }
  486. static usb_status_t usb_device_callback(usb_device_handle handle, uint32_t callbackEvent, void *eventParam)
  487. {
  488. usb_status_t error = kStatus_USB_Error;
  489. usb_device_struct_t *deviceHandle = (usb_device_struct_t *)handle;
  490. usb_device_endpoint_init_struct_t ep0_init =
  491. {
  492. 0x40,
  493. 0x00,
  494. USB_EP_ATTR_CONTROL,
  495. 0
  496. };
  497. usb_device_endpoint_callback_struct_t ep0_callback =
  498. {
  499. usb_device_endpoint_callback,
  500. 0,
  501. 0
  502. };
  503. udcd_t udcd = RT_NULL;
  504. if(deviceHandle->controllerId == kUSB_ControllerEhci0)
  505. {
  506. #ifdef RT_USING_EHCI0_AS_DEVICE
  507. udcd = &_fsl_udc_0;
  508. #endif
  509. }
  510. else
  511. {
  512. #ifdef RT_USING_EHCI1_AS_DEVICE
  513. udcd = &_fsl_udc_1;
  514. #endif
  515. }
  516. switch (callbackEvent)
  517. {
  518. case kUSB_DeviceEventBusReset:
  519. ep0_init.endpointAddress = 0x00;
  520. ep0_callback.callbackParam = (void *)0x00;
  521. USB_DeviceInitEndpoint(deviceHandle,&ep0_init,&ep0_callback);
  522. ep0_init.endpointAddress = 0x80;
  523. ep0_callback.callbackParam = (void *)0x80;
  524. USB_DeviceInitEndpoint(deviceHandle,&ep0_init,&ep0_callback);
  525. rt_usbd_reset_handler(udcd);
  526. break;
  527. case kUSB_DeviceEventAttach:
  528. rt_usbd_connect_handler(udcd);
  529. break;
  530. case kUSB_DeviceEventDetach:
  531. rt_usbd_disconnect_handler(udcd);
  532. break;
  533. }
  534. return error;
  535. }