usb_host_hci.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. /*
  2. * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016 - 2019 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include <usb/include/usb_host_config.h>
  9. #include "fsl_common.h"
  10. #include "usb_host.h"
  11. #include "usb_host_hci.h"
  12. #include "usb_host_devices.h"
  13. #include "fsl_device_registers.h"
  14. #if ((defined USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE))
  15. #include "fsl_cache.h"
  16. #endif
  17. /*******************************************************************************
  18. * Definitions
  19. ******************************************************************************/
  20. /* Component ID definition, used by tools. */
  21. #ifndef FSL_COMPONENT_ID
  22. #define FSL_COMPONENT_ID "middleware.usb.host_stack"
  23. #endif
  24. #if defined __CORTEX_M && (__CORTEX_M == 7U)
  25. #if (defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
  26. #warning USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE is not supported.
  27. #endif
  28. #endif
  29. /*******************************************************************************
  30. * Prototypes
  31. ******************************************************************************/
  32. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  33. #include "usb_host_hub.h"
  34. #include "usb_host_hub_app.h"
  35. #endif
  36. /*!
  37. * @brief get the idle host instance.
  38. *
  39. * @return host instance pointer.
  40. */
  41. static usb_host_instance_t *USB_HostGetInstance(void);
  42. /*!
  43. * @brief release host instance.
  44. *
  45. * @param hostInstance host instance pointer.
  46. */
  47. static void USB_HostReleaseInstance(usb_host_instance_t *hostInstance);
  48. /*!
  49. * @brief get the khci/ehci interface.
  50. *
  51. * @param controllerId controller id.
  52. * @param controllerTable return controller interface structure.
  53. */
  54. static void USB_HostGetControllerInterface(uint8_t controllerId,
  55. const usb_host_controller_interface_t **controllerTable);
  56. /*******************************************************************************
  57. * Variables
  58. ******************************************************************************/
  59. /*! @brief USB host instance resource */
  60. usb_host_instance_t g_UsbHostInstance[USB_HOST_CONFIG_MAX_HOST];
  61. #if ((defined USB_HOST_CONFIG_EHCI) && (USB_HOST_CONFIG_EHCI))
  62. #include "usb_host_ehci.h"
  63. static const usb_host_controller_interface_t s_EhciInterface = {
  64. USB_HostEhciCreate, USB_HostEhciDestory, USB_HostEhciOpenPipe, USB_HostEhciClosePipe,
  65. USB_HostEhciWritePipe, USB_HostEhciReadpipe, USB_HostEhciIoctl,
  66. };
  67. #endif /* USB_HOST_CONFIG_EHCI */
  68. #if ((defined USB_HOST_CONFIG_KHCI) && (USB_HOST_CONFIG_KHCI))
  69. #include "usb_host_khci.h"
  70. static const usb_host_controller_interface_t s_KhciInterface = {
  71. USB_HostKhciCreate, USB_HostKhciDestory, USB_HostKhciOpenPipe, USB_HostKhciClosePipe,
  72. USB_HostKhciWritePipe, USB_HostKhciReadpipe, USB_HostKciIoctl,
  73. };
  74. #endif /* USB_HOST_CONFIG_KHCI */
  75. #if ((defined USB_HOST_CONFIG_OHCI) && (USB_HOST_CONFIG_OHCI > 0U))
  76. #include "usb_host_ohci.h"
  77. static const usb_host_controller_interface_t s_OhciInterface = {
  78. USB_HostOhciCreate, USB_HostOhciDestory, USB_HostOhciOpenPipe, USB_HostOhciClosePipe,
  79. USB_HostOhciWritePipe, USB_HostOhciReadPipe, USB_HostOhciIoctl,
  80. };
  81. #endif /* USB_HOST_CONFIG_OHCI */
  82. #if ((defined USB_HOST_CONFIG_IP3516HS) && (USB_HOST_CONFIG_IP3516HS > 0U))
  83. #include "usb_host_ip3516hs.h"
  84. static const usb_host_controller_interface_t s_Ip3516HsInterface = {
  85. USB_HostIp3516HsCreate, USB_HostIp3516HsDestory, USB_HostIp3516HsOpenPipe, USB_HostIp3516HsClosePipe,
  86. USB_HostIp3516HsWritePipe, USB_HostIp3516HsReadPipe, USB_HostIp3516HsIoctl,
  87. };
  88. #endif /* USB_HOST_CONFIG_IP3516HS */
  89. USB_DMA_NONINIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE)
  90. static uint8_t s_Setupbuffer[USB_HOST_CONFIG_MAX_HOST][USB_HOST_CONFIG_MAX_TRANSFERS][USB_DATA_ALIGN_SIZE_MULTIPLE(8)];
  91. /*******************************************************************************
  92. * Code
  93. ******************************************************************************/
  94. #if ((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
  95. /*FUNCTION*----------------------------------------------------------------
  96. *
  97. * Function Name : usb_test_mode_init
  98. * Returned Value : None
  99. * Comments :
  100. * This function is called by common class to initialize the class driver. It
  101. * is called in response to a select interface call by application
  102. *
  103. *END*--------------------------------------------------------------------*/
  104. usb_status_t USB_HostTestModeInit(usb_device_handle deviceHandle)
  105. {
  106. #if (((defined USB_HOST_CONFIG_EHCI) && (USB_HOST_CONFIG_EHCI)) || \
  107. ((defined USB_HOST_CONFIG_IP3516HS) && (USB_HOST_CONFIG_IP3516HS)))
  108. usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)deviceHandle;
  109. usb_host_instance_t *hostInstance = (usb_host_instance_t *)deviceInstance->hostHandle;
  110. #endif
  111. uint32_t productId;
  112. uint32_t vendorId;
  113. (void)usb_echo("usb host test init\r\n");
  114. (void)USB_HostHelperGetPeripheralInformation(deviceHandle, (uint32_t)kUSB_HostGetDevicePID, &productId);
  115. (void)USB_HostHelperGetPeripheralInformation(deviceHandle, (uint32_t)kUSB_HostGetDeviceVID, &vendorId);
  116. (void)usb_echo(" vendor id :0x%x product id:0x%x \r\n", vendorId, productId);
  117. if ((productId != 0x0200U) && (productId != 0x0101U) && (productId != 0x0102U) && (productId != 0x0103U) &&
  118. (productId != 0x0104U) && (productId != 0x0105U) && (productId != 0x0106U) && (productId != 0x0107U) &&
  119. (productId != 0x0108U))
  120. {
  121. (void)usb_echo("Unsupported Device\r\n");
  122. }
  123. if (productId == 0x0200U)
  124. {
  125. (void)usb_echo("PET test device attached\r\n");
  126. }
  127. else
  128. {
  129. #if ((defined USB_HOST_CONFIG_EHCI) && (USB_HOST_CONFIG_EHCI))
  130. if (hostInstance->controllerTable == &s_EhciInterface)
  131. {
  132. (void)hostInstance->controllerTable->controllerIoctl(hostInstance->controllerHandle, kUSB_HostTestModeInit,
  133. (void *)deviceHandle);
  134. }
  135. #elif ((defined USB_HOST_CONFIG_IP3516HS) && (USB_HOST_CONFIG_IP3516HS))
  136. if (hostInstance->controllerTable == &s_Ip3516HsInterface)
  137. {
  138. (void)hostInstance->controllerTable->controllerIoctl(hostInstance->controllerHandle, kUSB_HostTestModeInit,
  139. (void *)deviceHandle);
  140. }
  141. #endif
  142. }
  143. return kStatus_USB_Success;
  144. }
  145. #endif
  146. static usb_host_instance_t *USB_HostGetInstance(void)
  147. {
  148. uint8_t i = 0;
  149. uint32_t index = 0;
  150. void *temp;
  151. OSA_SR_ALLOC();
  152. OSA_ENTER_CRITICAL();
  153. for (; i < (uint8_t)USB_HOST_CONFIG_MAX_HOST; i++)
  154. {
  155. if (g_UsbHostInstance[i].occupied != 1U)
  156. {
  157. uint8_t *buffer = (uint8_t *)&g_UsbHostInstance[i];
  158. for (uint32_t j = 0U; j < sizeof(usb_host_instance_t); j++)
  159. {
  160. buffer[j] = 0x00U;
  161. }
  162. g_UsbHostInstance[i].occupied = 1;
  163. OSA_EXIT_CRITICAL();
  164. for (index = 0; index < (uint32_t)USB_HOST_CONFIG_MAX_TRANSFERS; ++index)
  165. {
  166. temp = (void *)&(s_Setupbuffer[i][index][0]);
  167. g_UsbHostInstance[i].transferList[index].setupPacket = (usb_setup_struct_t *)temp;
  168. }
  169. return &g_UsbHostInstance[i];
  170. }
  171. }
  172. OSA_EXIT_CRITICAL();
  173. return NULL;
  174. }
  175. static void USB_HostReleaseInstance(usb_host_instance_t *hostInstance)
  176. {
  177. OSA_SR_ALLOC();
  178. OSA_ENTER_CRITICAL();
  179. hostInstance->occupied = 0;
  180. OSA_EXIT_CRITICAL();
  181. }
  182. static void USB_HostGetControllerInterface(uint8_t controllerId,
  183. const usb_host_controller_interface_t **controllerTable)
  184. {
  185. #if ((defined USB_HOST_CONFIG_KHCI) && (USB_HOST_CONFIG_KHCI))
  186. if (controllerId == (uint8_t)kUSB_ControllerKhci0)
  187. {
  188. *controllerTable = &s_KhciInterface;
  189. }
  190. #endif /* USB_HOST_CONFIG_KHCI */
  191. #if ((defined USB_HOST_CONFIG_EHCI) && (USB_HOST_CONFIG_EHCI))
  192. if ((controllerId == (uint8_t)kUSB_ControllerEhci0) || (controllerId == (uint8_t)kUSB_ControllerEhci1))
  193. {
  194. *controllerTable = &s_EhciInterface;
  195. }
  196. #endif /* USB_HOST_CONFIG_EHCI */
  197. #if ((defined USB_HOST_CONFIG_OHCI) && (USB_HOST_CONFIG_OHCI > 0U))
  198. if (controllerId == (uint8_t)kUSB_ControllerOhci0)
  199. {
  200. *controllerTable = &s_OhciInterface;
  201. }
  202. #endif /* USB_HOST_CONFIG_OHCI */
  203. #if ((defined USB_HOST_CONFIG_IP3516HS) && (USB_HOST_CONFIG_IP3516HS > 0U))
  204. if (controllerId == (uint8_t)kUSB_ControllerIp3516Hs0)
  205. {
  206. *controllerTable = &s_Ip3516HsInterface;
  207. }
  208. #endif /* USB_HOST_CONFIG_IP3516HS */
  209. }
  210. usb_status_t USB_HostInit(uint8_t controllerId, usb_host_handle *hostHandle, host_callback_t callbackFn)
  211. {
  212. usb_status_t status = kStatus_USB_Success;
  213. usb_host_instance_t *hostInstance = NULL;
  214. usb_host_transfer_t *transferPrev = NULL;
  215. uint8_t i = 0;
  216. hostInstance = USB_HostGetInstance(); /* get one host instance */
  217. if (hostInstance == NULL)
  218. {
  219. return kStatus_USB_InvalidHandle;
  220. }
  221. /* get khci/ehci API table */
  222. USB_HostGetControllerInterface(controllerId, &hostInstance->controllerTable);
  223. if (hostInstance->controllerTable == NULL)
  224. {
  225. USB_HostReleaseInstance(hostInstance);
  226. return kStatus_USB_ControllerNotFound;
  227. }
  228. /* judge the controller interface one time at here */
  229. if ((hostInstance->controllerTable->controllerCreate == NULL) ||
  230. (hostInstance->controllerTable->controllerDestory == NULL) ||
  231. (hostInstance->controllerTable->controllerOpenPipe == NULL) ||
  232. (hostInstance->controllerTable->controllerClosePipe == NULL) ||
  233. (hostInstance->controllerTable->controllerWritePipe == NULL) ||
  234. (hostInstance->controllerTable->controllerReadPipe == NULL) ||
  235. (hostInstance->controllerTable->controllerIoctl == NULL))
  236. {
  237. return kStatus_USB_Error;
  238. }
  239. /* HOST instance init*/
  240. hostInstance->controllerId = controllerId;
  241. hostInstance->deviceCallback = callbackFn;
  242. hostInstance->deviceList = NULL;
  243. hostInstance->hostMutex = (osa_mutex_handle_t)(&hostInstance->mutexBuffer[0]);
  244. if (KOSA_StatusSuccess != OSA_MutexCreate(hostInstance->hostMutex))
  245. {
  246. USB_HostReleaseInstance(hostInstance);
  247. #ifdef HOST_ECHO
  248. usb_echo("host init: create host mutex fail\r\n");
  249. #endif
  250. return kStatus_USB_Error;
  251. }
  252. /* initialize transfer list */
  253. hostInstance->transferHead = &hostInstance->transferList[0];
  254. transferPrev = hostInstance->transferHead;
  255. for (i = 1; i < USB_HOST_CONFIG_MAX_TRANSFERS; ++i)
  256. {
  257. transferPrev->next = &hostInstance->transferList[i];
  258. transferPrev = transferPrev->next;
  259. }
  260. /* controller create, the callbackFn is initialized in USB_HostGetControllerInterface */
  261. status =
  262. hostInstance->controllerTable->controllerCreate(controllerId, hostInstance, &(hostInstance->controllerHandle));
  263. if ((status != kStatus_USB_Success) || (hostInstance->controllerHandle == NULL))
  264. {
  265. (void)OSA_MutexDestroy(hostInstance->hostMutex);
  266. USB_HostReleaseInstance(hostInstance);
  267. #ifdef HOST_ECHO
  268. usb_echo("host init: controller init fail\r\n");
  269. #endif
  270. return kStatus_USB_Error;
  271. }
  272. *hostHandle = hostInstance;
  273. return kStatus_USB_Success;
  274. }
  275. usb_status_t USB_HostDeinit(usb_host_handle hostHandle)
  276. {
  277. usb_status_t status = kStatus_USB_Success;
  278. usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
  279. usb_host_device_instance_t *deviceInstance = NULL;
  280. if (hostHandle == NULL)
  281. {
  282. return kStatus_USB_InvalidHandle;
  283. }
  284. /* device list detach */
  285. deviceInstance = (usb_host_device_instance_t *)hostInstance->deviceList;
  286. while (deviceInstance != NULL)
  287. {
  288. deviceInstance = (usb_host_device_instance_t *)hostInstance->deviceList;
  289. (void)USB_HostDetachDeviceInternal(hostHandle, deviceInstance);
  290. }
  291. /* controller instance destroy, the callbackFn is initialized in USB_HostGetControllerInterface */
  292. status = hostInstance->controllerTable->controllerDestory(hostInstance->controllerHandle);
  293. hostInstance->controllerHandle = NULL;
  294. if (status != kStatus_USB_Success)
  295. {
  296. #ifdef HOST_ECHO
  297. usb_echo("host controller destroy fail\r\n");
  298. #endif
  299. }
  300. /* resource release */
  301. if (NULL != hostInstance->hostMutex)
  302. {
  303. (void)OSA_MutexDestroy(hostInstance->hostMutex);
  304. hostInstance->hostMutex = NULL;
  305. }
  306. USB_HostReleaseInstance(hostInstance);
  307. return status;
  308. }
  309. usb_status_t USB_HostOpenPipe(usb_host_handle hostHandle,
  310. usb_host_pipe_handle *pipeHandle,
  311. usb_host_pipe_init_t *pipeInit)
  312. {
  313. usb_status_t status = kStatus_USB_Success;
  314. usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
  315. if ((hostHandle == NULL) || (pipeInit == NULL))
  316. {
  317. return kStatus_USB_InvalidHandle;
  318. }
  319. /* call controller open pipe interface, the callbackFn is initialized in USB_HostGetControllerInterface */
  320. status = hostInstance->controllerTable->controllerOpenPipe(hostInstance->controllerHandle, pipeHandle, pipeInit);
  321. return status;
  322. }
  323. usb_status_t USB_HostClosePipe(usb_host_handle hostHandle, usb_host_pipe_handle pipeHandle)
  324. {
  325. usb_status_t status = kStatus_USB_Success;
  326. usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
  327. if ((hostHandle == NULL) || (pipeHandle == NULL))
  328. {
  329. return kStatus_USB_InvalidHandle;
  330. }
  331. /* call controller close pipe interface, the callbackFn is initialized in USB_HostGetControllerInterface */
  332. status = hostInstance->controllerTable->controllerClosePipe(hostInstance->controllerHandle, pipeHandle);
  333. return status;
  334. }
  335. usb_status_t USB_HostSend(usb_host_handle hostHandle, usb_host_pipe_handle pipeHandle, usb_host_transfer_t *transfer)
  336. {
  337. usb_status_t status = kStatus_USB_Success;
  338. usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
  339. if ((hostHandle == NULL) || (pipeHandle == NULL) || (transfer == NULL))
  340. {
  341. return kStatus_USB_InvalidHandle;
  342. }
  343. /* initialize transfer */
  344. transfer->transferSofar = 0;
  345. transfer->direction = USB_OUT;
  346. (void)USB_HostLock(); /* This api can be called by host task and app task */
  347. /* keep this code: in normal situation application will guarantee the device is attached when call send/receive function
  348. */
  349. #if 0
  350. if ((USB_HostValidateDevice(pipe_ptr->deviceHandle) != kStatus_USB_Success) || (!(USB_HostGetDeviceAttachState(pipe_ptr->deviceHandle))))
  351. {
  352. USB_HostUnlock();
  353. return status;
  354. }
  355. #endif
  356. /* call controller write pipe interface */
  357. #if ((defined USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE))
  358. if (transfer->transferLength > 0)
  359. {
  360. DCACHE_CleanByRange((uint32_t)transfer->transferBuffer, transfer->transferLength);
  361. }
  362. #endif
  363. /* the callbackFn is initialized in USB_HostGetControllerInterface */
  364. status = hostInstance->controllerTable->controllerWritePipe(hostInstance->controllerHandle, pipeHandle, transfer);
  365. (void)USB_HostUnlock();
  366. return status;
  367. }
  368. usb_status_t USB_HostSendSetup(usb_host_handle hostHandle,
  369. usb_host_pipe_handle pipeHandle,
  370. usb_host_transfer_t *transfer)
  371. {
  372. usb_status_t status = kStatus_USB_Success;
  373. usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
  374. if ((hostHandle == NULL) || (pipeHandle == NULL) || (transfer == NULL))
  375. {
  376. return kStatus_USB_InvalidHandle;
  377. }
  378. /* initialize transfer */
  379. transfer->transferSofar = 0;
  380. transfer->next = NULL;
  381. transfer->setupStatus = 0;
  382. if ((transfer->setupPacket->bmRequestType & USB_REQUEST_TYPE_DIR_MASK) == USB_REQUEST_TYPE_DIR_IN)
  383. {
  384. transfer->direction = USB_IN;
  385. }
  386. else
  387. {
  388. transfer->direction = USB_OUT;
  389. }
  390. (void)USB_HostLock(); /* This API can be called by host task and application task */
  391. /* keep this code: in normal situation application will guarantee the device is attached when call send/receive function
  392. */
  393. #if 0
  394. if ((USB_HostValidateDevice(pipe_ptr->deviceHandle) != kStatus_USB_Success) || (!(USB_HostGetDeviceAttachState(pipe_ptr->deviceHandle))))
  395. {
  396. USB_HostUnlock();
  397. return status;
  398. }
  399. #endif
  400. /* call controller write pipe interface */
  401. #if ((defined USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE))
  402. DCACHE_CleanByRange((uint32_t)&transfer->setupPacket->bmRequestType, sizeof(usb_setup_struct_t));
  403. if (transfer->transferLength > 0)
  404. {
  405. DCACHE_CleanInvalidateByRange((uint32_t)transfer->transferBuffer, transfer->transferLength);
  406. }
  407. #endif
  408. /* the callbackFn is initialized in USB_HostGetControllerInterface */
  409. status = hostInstance->controllerTable->controllerWritePipe(hostInstance->controllerHandle, pipeHandle, transfer);
  410. (void)USB_HostUnlock();
  411. return status;
  412. }
  413. usb_status_t USB_HostRecv(usb_host_handle hostHandle, usb_host_pipe_handle pipeHandle, usb_host_transfer_t *transfer)
  414. {
  415. usb_status_t status = kStatus_USB_Success;
  416. usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
  417. if ((hostHandle == NULL) || (pipeHandle == NULL) || (transfer == NULL))
  418. {
  419. return kStatus_USB_InvalidHandle;
  420. }
  421. /* initialize transfer */
  422. transfer->transferSofar = 0;
  423. transfer->direction = USB_IN;
  424. (void)USB_HostLock(); /* This API can be called by host task and application task */
  425. /* keep this code: in normal situation application will guarantee the device is attached when call send/receive function
  426. */
  427. #if 0
  428. if ((USB_HostValidateDevice(pipe_ptr->deviceHandle) != kStatus_USB_Success) || (!(USB_HostGetDeviceAttachState(pipe_ptr->deviceHandle))))
  429. {
  430. USB_HostUnlock();
  431. return status;
  432. }
  433. #endif
  434. #if ((defined USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE))
  435. if (transfer->transferLength > 0)
  436. {
  437. DCACHE_CleanInvalidateByRange((uint32_t)transfer->transferBuffer, transfer->transferLength);
  438. }
  439. #endif
  440. /* the callbackFn is initialized in USB_HostGetControllerInterface */
  441. status = hostInstance->controllerTable->controllerReadPipe(hostInstance->controllerHandle, pipeHandle, transfer);
  442. (void)USB_HostUnlock();
  443. return status;
  444. }
  445. usb_status_t USB_HostCancelTransfer(usb_host_handle hostHandle,
  446. usb_host_pipe_handle pipeHandle,
  447. usb_host_transfer_t *transfer)
  448. {
  449. usb_status_t status = kStatus_USB_Success;
  450. usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
  451. usb_host_cancel_param_t cancelParam;
  452. if ((hostHandle == NULL) || (pipeHandle == NULL))
  453. {
  454. return kStatus_USB_InvalidHandle;
  455. }
  456. /* initialize cancel parameter */
  457. cancelParam.pipeHandle = pipeHandle;
  458. cancelParam.transfer = transfer;
  459. /* USB_HostLock(); This api can be called by host task and app task */
  460. /* the callbackFn is initialized in USB_HostGetControllerInterface */
  461. status = hostInstance->controllerTable->controllerIoctl(hostInstance->controllerHandle, kUSB_HostCancelTransfer,
  462. &cancelParam);
  463. /* USB_HostUnlock(); */
  464. return status;
  465. }
  466. usb_status_t USB_HostMallocTransfer(usb_host_handle hostHandle, usb_host_transfer_t **transfer)
  467. {
  468. usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
  469. if ((hostHandle == NULL) || (transfer == NULL))
  470. {
  471. return kStatus_USB_InvalidHandle;
  472. }
  473. /* get one from the transfer_head */
  474. (void)USB_HostLock();
  475. if (hostInstance->transferHead != NULL)
  476. {
  477. *transfer = hostInstance->transferHead;
  478. hostInstance->transferHead = hostInstance->transferHead->next;
  479. (void)USB_HostUnlock();
  480. return kStatus_USB_Success;
  481. }
  482. else
  483. {
  484. *transfer = NULL;
  485. (void)USB_HostUnlock();
  486. return kStatus_USB_Error;
  487. }
  488. }
  489. usb_status_t USB_HostFreeTransfer(usb_host_handle hostHandle, usb_host_transfer_t *transfer)
  490. {
  491. usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
  492. if (hostHandle == NULL)
  493. {
  494. return kStatus_USB_InvalidHandle;
  495. }
  496. if (transfer == NULL)
  497. {
  498. return kStatus_USB_Success;
  499. }
  500. /* release one to the transfer_head */
  501. (void)USB_HostLock();
  502. transfer->next = hostInstance->transferHead;
  503. hostInstance->transferHead = transfer;
  504. (void)USB_HostUnlock();
  505. return kStatus_USB_Success;
  506. }
  507. usb_status_t USB_HostHelperGetPeripheralInformation(usb_device_handle deviceHandle,
  508. uint32_t infoCode,
  509. uint32_t *infoValue)
  510. {
  511. usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)deviceHandle;
  512. uint32_t *temp;
  513. usb_host_dev_info_t devInfo;
  514. if ((deviceHandle == NULL) || (infoValue == NULL))
  515. {
  516. return kStatus_USB_InvalidParameter;
  517. }
  518. devInfo = (usb_host_dev_info_t)infoCode;
  519. switch (devInfo)
  520. {
  521. case kUSB_HostGetDeviceAddress: /* device address */
  522. *infoValue = (uint32_t)deviceInstance->setAddress;
  523. break;
  524. case kUSB_HostGetDeviceControlPipe: /* device control pipe */
  525. temp = (uint32_t *)deviceInstance->controlPipe;
  526. *infoValue = (uint32_t)temp;
  527. break;
  528. case kUSB_HostGetHostHandle: /* device host handle */
  529. temp = (uint32_t *)deviceInstance->hostHandle;
  530. *infoValue = (uint32_t)temp;
  531. break;
  532. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  533. case kUSB_HostGetDeviceHubNumber: /* device hub address */
  534. *infoValue = (uint32_t)deviceInstance->hubNumber;
  535. break;
  536. case kUSB_HostGetDevicePortNumber: /* device port no */
  537. *infoValue = (uint32_t)deviceInstance->portNumber;
  538. break;
  539. case kUSB_HostGetDeviceLevel: /* device level */
  540. *infoValue = (uint32_t)deviceInstance->level;
  541. break;
  542. case kUSB_HostGetDeviceHSHubNumber: /* device high-speed hub address */
  543. *infoValue = (uint32_t)deviceInstance->hsHubNumber;
  544. break;
  545. case kUSB_HostGetDeviceHSHubPort: /* device high-speed hub port no */
  546. *infoValue = (uint32_t)deviceInstance->hsHubPort;
  547. break;
  548. case kUSB_HostGetHubThinkTime: /* device hub think time */
  549. *infoValue = USB_HostHubGetTotalThinkTime(deviceInstance->hostHandle, deviceInstance->hubNumber);
  550. break;
  551. #else
  552. case kUSB_HostGetDeviceHubNumber: /* device hub address */
  553. case kUSB_HostGetDevicePortNumber: /* device port no */
  554. case kUSB_HostGetDeviceHSHubNumber: /* device high-speed hub address */
  555. case kUSB_HostGetDeviceHSHubPort: /* device high-speed hub port no */
  556. case kUSB_HostGetHubThinkTime: /* device hub think time */
  557. *infoValue = 0;
  558. break;
  559. case kUSB_HostGetDeviceLevel: /* device level */
  560. *infoValue = 1;
  561. break;
  562. #endif /* USB_HOST_CONFIG_HUB */
  563. case kUSB_HostGetDeviceSpeed: /* device speed */
  564. *infoValue = (uint32_t)deviceInstance->speed;
  565. break;
  566. case kUSB_HostGetDevicePID: /* device pid */
  567. *infoValue = (uint32_t)USB_SHORT_FROM_LITTLE_ENDIAN_ADDRESS(deviceInstance->deviceDescriptor->idProduct);
  568. break;
  569. case kUSB_HostGetDeviceVID: /* device vid */
  570. *infoValue = (uint32_t)USB_SHORT_FROM_LITTLE_ENDIAN_ADDRESS(deviceInstance->deviceDescriptor->idVendor);
  571. break;
  572. case kUSB_HostGetDeviceConfigIndex: /* device config index */
  573. *infoValue = (uint32_t)deviceInstance->configurationValue - 1U;
  574. break;
  575. case kUSB_HostGetConfigurationDes: /* configuration descriptor pointer */
  576. *infoValue = (uint32_t)deviceInstance->configurationDesc;
  577. break;
  578. case kUSB_HostGetConfigurationLength: /* configuration descriptor length */
  579. *infoValue = (uint32_t)deviceInstance->configurationLen;
  580. break;
  581. default:
  582. /*no action*/
  583. break;
  584. }
  585. return kStatus_USB_Success;
  586. }
  587. usb_status_t USB_HostHelperParseAlternateSetting(usb_host_interface_handle interfaceHandle,
  588. uint8_t alternateSetting,
  589. usb_host_interface_t *interface)
  590. {
  591. uint32_t endPosition;
  592. usb_descriptor_union_t *unionDes;
  593. usb_host_ep_t *epParse;
  594. void *temp;
  595. if (interfaceHandle == NULL)
  596. {
  597. return kStatus_USB_InvalidHandle;
  598. }
  599. if (alternateSetting == 0U)
  600. {
  601. return kStatus_USB_InvalidParameter;
  602. }
  603. /* parse configuration descriptor */
  604. temp = (void *)((usb_host_interface_t *)interfaceHandle)->interfaceExtension;
  605. unionDes = (usb_descriptor_union_t *)temp; /* interface extend descriptor start */
  606. endPosition = (uint32_t)unionDes +
  607. ((usb_host_interface_t *)interfaceHandle)->interfaceExtensionLength; /* interface extend descriptor end */
  608. /* search for the alternate setting interface descriptor */
  609. while ((uint32_t)unionDes < endPosition)
  610. {
  611. if (unionDes->interface.bDescriptorType == USB_DESCRIPTOR_TYPE_INTERFACE)
  612. {
  613. if (unionDes->interface.bAlternateSetting == alternateSetting)
  614. {
  615. break;
  616. }
  617. else
  618. {
  619. unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
  620. }
  621. }
  622. else
  623. {
  624. unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
  625. }
  626. }
  627. if ((uint32_t)unionDes >= endPosition)
  628. {
  629. return kStatus_USB_Error;
  630. }
  631. /* initialize interface handle structure instance */
  632. interface->interfaceDesc = &unionDes->interface;
  633. interface->alternateSettingNumber = 0U;
  634. interface->epCount = 0U;
  635. interface->interfaceExtension = NULL;
  636. interface->interfaceExtensionLength = 0U;
  637. interface->interfaceIndex = unionDes->interface.bInterfaceNumber;
  638. /* search for endpoint descriptor start position */
  639. unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
  640. while ((uint32_t)unionDes < endPosition)
  641. {
  642. if ((unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_INTERFACE) &&
  643. (unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_ENDPOINT))
  644. {
  645. if (interface->interfaceExtension == NULL)
  646. {
  647. interface->interfaceExtension = (uint8_t *)unionDes;
  648. }
  649. interface->interfaceExtensionLength += unionDes->common.bLength;
  650. unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
  651. }
  652. else
  653. {
  654. break;
  655. }
  656. }
  657. /* parse endpoint descriptor */
  658. if (interface->interfaceDesc->bNumEndpoints != 0U)
  659. {
  660. if ((unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_ENDPOINT) ||
  661. (interface->interfaceDesc->bNumEndpoints > USB_HOST_CONFIG_INTERFACE_MAX_EP))
  662. {
  663. #ifdef HOST_ECHO
  664. usb_echo("interface descriptor error\n");
  665. #endif
  666. return kStatus_USB_Error;
  667. }
  668. for (; interface->epCount < interface->interfaceDesc->bNumEndpoints; (interface->epCount)++)
  669. {
  670. if (((uint32_t)unionDes >= endPosition) ||
  671. (unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_ENDPOINT))
  672. {
  673. #ifdef HOST_ECHO
  674. usb_echo("endpoint descriptor error\n");
  675. #endif
  676. return kStatus_USB_Error;
  677. }
  678. epParse = (usb_host_ep_t *)&interface->epList[interface->epCount];
  679. temp = (void *)unionDes;
  680. epParse->epDesc = (usb_descriptor_endpoint_t *)temp;
  681. epParse->epExtensionLength = 0;
  682. epParse->epExtension = NULL;
  683. unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
  684. while ((uint32_t)unionDes < endPosition)
  685. {
  686. if ((unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_ENDPOINT) &&
  687. (unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_INTERFACE))
  688. {
  689. if (epParse->epExtension == NULL)
  690. {
  691. epParse->epExtension = (uint8_t *)unionDes;
  692. }
  693. epParse->epExtensionLength += unionDes->common.bLength;
  694. unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
  695. }
  696. else
  697. {
  698. break;
  699. }
  700. }
  701. }
  702. }
  703. return kStatus_USB_Success;
  704. }
  705. void USB_HostGetVersion(uint32_t *version)
  706. {
  707. if (NULL != version)
  708. {
  709. *version =
  710. (uint32_t)USB_MAKE_VERSION(USB_STACK_VERSION_MAJOR, USB_STACK_VERSION_MINOR, USB_STACK_VERSION_BUGFIX);
  711. }
  712. }
  713. #if ((defined(USB_HOST_CONFIG_LOW_POWER_MODE)) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
  714. /* Send BUS or specific device suspend request */
  715. usb_status_t USB_HostSuspendDeviceResquest(usb_host_handle hostHandle, usb_device_handle deviceHandle)
  716. {
  717. usb_host_instance_t *hostInstance;
  718. usb_host_device_instance_t *deviceInstance;
  719. usb_status_t status = kStatus_USB_Error;
  720. usb_host_bus_control_t type = kUSB_HostBusSuspend;
  721. if (hostHandle == NULL)
  722. {
  723. return kStatus_USB_InvalidHandle;
  724. }
  725. hostInstance = (usb_host_instance_t *)hostHandle;
  726. hostInstance->suspendedDevice = (void *)deviceHandle;
  727. if (NULL == deviceHandle)
  728. {
  729. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  730. status = USB_HostHubSuspendDevice(hostInstance);
  731. #else
  732. /* the callbackFn is initialized in USB_HostGetControllerInterface */
  733. status =
  734. hostInstance->controllerTable->controllerIoctl(hostInstance->controllerHandle, kUSB_HostBusControl, &type);
  735. #endif
  736. }
  737. else
  738. {
  739. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  740. deviceInstance = (usb_host_device_instance_t *)deviceHandle;
  741. if (0U == deviceInstance->hubNumber)
  742. {
  743. #endif
  744. if (hostInstance->deviceList == deviceHandle)
  745. {
  746. /* the callbackFn is initialized in USB_HostGetControllerInterface */
  747. status = hostInstance->controllerTable->controllerIoctl(hostInstance->controllerHandle,
  748. kUSB_HostBusControl, &type);
  749. }
  750. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  751. }
  752. else
  753. {
  754. if (kStatus_USB_Success == USB_HostValidateDevice(hostInstance, deviceHandle))
  755. {
  756. status = USB_HostHubSuspendDevice(hostInstance);
  757. }
  758. }
  759. #endif
  760. }
  761. if (kStatus_USB_Error == status)
  762. {
  763. hostInstance->suspendedDevice = NULL;
  764. }
  765. return status;
  766. }
  767. /* Send BUS or specific device resume request */
  768. usb_status_t USB_HostResumeDeviceResquest(usb_host_handle hostHandle, usb_device_handle deviceHandle)
  769. {
  770. usb_host_instance_t *hostInstance;
  771. usb_host_device_instance_t *deviceInstance;
  772. usb_status_t status = kStatus_USB_Error;
  773. usb_host_bus_control_t type = kUSB_HostBusResume;
  774. if (hostHandle == NULL)
  775. {
  776. return kStatus_USB_InvalidHandle;
  777. }
  778. hostInstance = (usb_host_instance_t *)hostHandle;
  779. if (hostInstance->suspendedDevice != deviceHandle)
  780. {
  781. return kStatus_USB_InvalidParameter;
  782. }
  783. hostInstance->suspendedDevice = (void *)deviceHandle;
  784. if (NULL == deviceHandle)
  785. {
  786. /* the callbackFn is initialized in USB_HostGetControllerInterface */
  787. status =
  788. hostInstance->controllerTable->controllerIoctl(hostInstance->controllerHandle, kUSB_HostBusControl, &type);
  789. }
  790. else
  791. {
  792. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  793. deviceInstance = (usb_host_device_instance_t *)deviceHandle;
  794. if (0U == deviceInstance->hubNumber)
  795. {
  796. #endif
  797. if (hostInstance->deviceList == deviceHandle)
  798. {
  799. /* the callbackFn is initialized in USB_HostGetControllerInterface */
  800. status = hostInstance->controllerTable->controllerIoctl(hostInstance->controllerHandle,
  801. kUSB_HostBusControl, &type);
  802. }
  803. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  804. }
  805. else
  806. {
  807. if (kStatus_USB_Success == USB_HostValidateDevice(hostInstance, deviceHandle))
  808. {
  809. status = USB_HostHubResumeDevice(hostInstance);
  810. }
  811. }
  812. #endif
  813. }
  814. return status;
  815. }
  816. #if ((defined(USB_HOST_CONFIG_LPM_L1)) && (USB_HOST_CONFIG_LPM_L1 > 0U))
  817. /* Send BUS or specific device suspend request */
  818. usb_status_t USB_HostL1SleepDeviceResquest(usb_host_handle hostHandle,
  819. usb_device_handle deviceHandle,
  820. uint8_t sleepType)
  821. {
  822. usb_host_instance_t *hostInstance;
  823. usb_status_t status = kStatus_USB_Error;
  824. usb_host_bus_control_t type = kUSB_HostBusL1Sleep;
  825. if (hostHandle == NULL)
  826. {
  827. return kStatus_USB_InvalidHandle;
  828. }
  829. hostInstance = (usb_host_instance_t *)hostHandle;
  830. hostInstance->suspendedDevice = (void *)deviceHandle;
  831. if (1U == sleepType)
  832. {
  833. /*#if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))*/
  834. /*To do, incomplete hub L1 suspend device*/
  835. /*#else*/
  836. /* the callbackFn is initialized in USB_HostGetControllerInterface */
  837. status =
  838. hostInstance->controllerTable->controllerIoctl(hostInstance->controllerHandle, kUSB_HostBusControl, &type);
  839. /*#endif*/
  840. }
  841. else
  842. {
  843. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  844. /*To do, if device hub number is 0, need suspend the bus ,else suspend the corresponding device*/
  845. #endif
  846. if (hostInstance->deviceList == deviceHandle)
  847. {
  848. /* the callbackFn is initialized in USB_HostGetControllerInterface */
  849. status = hostInstance->controllerTable->controllerIoctl(hostInstance->controllerHandle, kUSB_HostBusControl,
  850. &type);
  851. }
  852. }
  853. if (kStatus_USB_Error == status)
  854. {
  855. hostInstance->suspendedDevice = NULL;
  856. }
  857. return status;
  858. }
  859. /* Send BUS or specific device suspend request */
  860. usb_status_t USB_HostL1SleepDeviceResquestConfig(usb_host_handle hostHandle, uint8_t *lpmParam)
  861. {
  862. usb_host_instance_t *hostInstance;
  863. usb_status_t status = kStatus_USB_Error;
  864. if (hostHandle == NULL)
  865. {
  866. return kStatus_USB_InvalidHandle;
  867. }
  868. hostInstance = (usb_host_instance_t *)hostHandle;
  869. /* the callbackFn is initialized in USB_HostGetControllerInterface */
  870. status =
  871. hostInstance->controllerTable->controllerIoctl(hostInstance->controllerHandle, kUSB_HostL1Config, lpmParam);
  872. return status;
  873. }
  874. /* Send BUS or specific device resume request */
  875. usb_status_t USB_HostL1ResumeDeviceResquest(usb_host_handle hostHandle,
  876. usb_device_handle deviceHandle,
  877. uint8_t sleepType)
  878. {
  879. usb_host_instance_t *hostInstance;
  880. usb_status_t status = kStatus_USB_Error;
  881. usb_host_bus_control_t type = kUSB_HostBusL1Resume;
  882. if (hostHandle == NULL)
  883. {
  884. return kStatus_USB_InvalidHandle;
  885. }
  886. hostInstance = (usb_host_instance_t *)hostHandle;
  887. if (1U == sleepType)
  888. {
  889. /* the callbackFn is initialized in USB_HostGetControllerInterface */
  890. status =
  891. hostInstance->controllerTable->controllerIoctl(hostInstance->controllerHandle, kUSB_HostBusControl, &type);
  892. }
  893. else
  894. {
  895. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  896. /*To do, if device hub number is 0, need suspend the bus ,else suspend the corresponding device*/
  897. #endif
  898. if (hostInstance->deviceList == deviceHandle)
  899. {
  900. /* the callbackFn is initialized in USB_HostGetControllerInterface */
  901. status = hostInstance->controllerTable->controllerIoctl(hostInstance->controllerHandle, kUSB_HostBusControl,
  902. &type);
  903. }
  904. }
  905. return status;
  906. }
  907. #endif
  908. /* Update HW tick(unit is ms) */
  909. usb_status_t USB_HostUpdateHwTick(usb_host_handle hostHandle, uint64_t tick)
  910. {
  911. usb_host_instance_t *hostInstance;
  912. usb_status_t status = kStatus_USB_Success;
  913. if (hostHandle == NULL)
  914. {
  915. return kStatus_USB_InvalidHandle;
  916. }
  917. hostInstance = (usb_host_instance_t *)hostHandle;
  918. hostInstance->hwTick = tick;
  919. return status;
  920. }
  921. #endif
  922. #if ((defined(USB_HOST_CONFIG_BATTERY_CHARGER)) && (USB_HOST_CONFIG_BATTERY_CHARGER > 0U))
  923. usb_status_t USB_HostSetChargerType(usb_host_handle hostHandle, uint8_t type)
  924. {
  925. usb_host_instance_t *hostInstance;
  926. if (hostHandle == NULL)
  927. {
  928. return kStatus_USB_InvalidHandle;
  929. }
  930. hostInstance = (usb_host_instance_t *)hostHandle;
  931. return hostInstance->controllerTable->controllerIoctl(hostInstance->controllerHandle, kUSB_HostSetChargerType,
  932. &type);
  933. }
  934. #endif