usb_host_devices.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  1. /*
  2. * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016,2019 NXP
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <usb/include/usb_host_config.h>
  8. #include "usb_host.h"
  9. #include "usb_host_hci.h"
  10. #include "usb_host_devices.h"
  11. #include "usb_host_framework.h"
  12. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  13. #include "usb_host_hub.h"
  14. #include "usb_host_hub_app.h"
  15. #endif /* USB_HOST_CONFIG_HUB */
  16. /*******************************************************************************
  17. * Definitions
  18. ******************************************************************************/
  19. #ifndef USB_HOST_CONFIG_MAX_CONFIGURATION_DESCRIPTOR_LENGTH
  20. #define USB_HOST_CONFIG_MAX_CONFIGURATION_DESCRIPTOR_LENGTH (5000U)
  21. #endif
  22. /*******************************************************************************
  23. * Prototypes
  24. ******************************************************************************/
  25. /*!
  26. * @brief enumeration transfer callback function.
  27. *
  28. * @param param callback parameter.
  29. * @param transfer the transfer.
  30. * @param status transfer result status.
  31. */
  32. static void USB_HostEnumerationTransferCallback(void *param, usb_host_transfer_t *transfer, usb_status_t status);
  33. /*!
  34. * @brief process the new step state.
  35. *
  36. * @param deviceInstance device instance pointer.
  37. *
  38. * @return kStatus_USB_Success or error codes
  39. */
  40. static usb_status_t USB_HostProcessState(usb_host_device_instance_t *deviceInstance);
  41. /*!
  42. * @brief process the previous step transfer result.
  43. *
  44. * @param deviceInstance device instance pointer.
  45. *
  46. * @return kStatus_USB_Success or error codes
  47. */
  48. static usb_status_t USB_HostProcessCallback(usb_host_device_instance_t *deviceInstance, uint32_t dataLength);
  49. /*!
  50. * @brief notify the application event, the callback is registered when initializing host.
  51. *
  52. * @param deviceInstance device instance pointer.
  53. * @param eventCode event code.
  54. *
  55. * @return kStatus_USB_Success or error codes
  56. */
  57. static usb_status_t USB_HostNotifyDevice(usb_host_handle hostHandle,
  58. usb_host_device_instance_t *deviceInstance,
  59. uint32_t eventCode,
  60. uint32_t eventParameter);
  61. /*!
  62. * @brief allocate one address.
  63. *
  64. * @param hostInstance host instance pointer.
  65. *
  66. * @return address, 0 is invalid.
  67. */
  68. static uint8_t USB_HostAllocateDeviceAddress(usb_host_instance_t *hostInstance);
  69. /*!
  70. * @brief release one address.
  71. *
  72. * @param hostInstance host instance pointer.
  73. * @param address releasing address.
  74. */
  75. static void USB_HostReleaseDeviceAddress(usb_host_instance_t *hostInstance, uint8_t address);
  76. /*!
  77. * @brief release device resource.
  78. *
  79. * @param hostInstance host instance pointer.
  80. * @param deviceInstance device instance pointer.
  81. */
  82. static void USB_HostReleaseDeviceResource(usb_host_instance_t *hostInstance,
  83. usb_host_device_instance_t *deviceInstance);
  84. /*!
  85. * @brief parse device configuration descriptor.
  86. *
  87. * @param deviceHandle device handle.
  88. *
  89. * @return kStatus_USB_Success or error codes.
  90. */
  91. static usb_status_t USB_HostParseDeviceConfigurationDescriptor(usb_device_handle deviceHandle);
  92. /*!
  93. * @brief remove device instance from host device list.
  94. *
  95. * @param hostHandle host instance handle.
  96. * @param deviceHandle device handle.
  97. *
  98. * @return kStatus_USB_Success or error codes.
  99. */
  100. static usb_status_t USB_HostRemoveDeviceInstance(usb_host_handle hostHandle, usb_device_handle deviceHandle);
  101. /*!
  102. * @brief control the bus.
  103. *
  104. * This function control the host bus.
  105. *
  106. * @param[in] hostHandle the host handle.
  107. * @param[in] controlType the control code, please reference to bus_event_t.
  108. *
  109. * @retval kStatus_USB_Success control successfully.
  110. * @retval kStatus_USB_InvalidHandle The hostHandle is a NULL pointer.
  111. */
  112. static usb_status_t USB_HostControlBus(usb_host_handle hostHandle, uint8_t controlType);
  113. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  114. #endif
  115. /*******************************************************************************
  116. * Variables
  117. ******************************************************************************/
  118. /*! @brief enumeration step process array */
  119. static const usb_host_enum_process_entry_t s_EnumEntries[] = {
  120. /* kStatus_dev_initial */
  121. {
  122. kStatus_DEV_Notinit,
  123. kStatus_DEV_Notinit,
  124. NULL,
  125. },
  126. /* kStatus_DEV_GetDes8 */
  127. {
  128. kStatus_DEV_SetAddress,
  129. kStatus_DEV_GetDes8,
  130. USB_HostProcessCallback,
  131. },
  132. /* kStatus_DEV_SetAddress */
  133. {
  134. kStatus_DEV_GetDes,
  135. kStatus_DEV_SetAddress,
  136. USB_HostProcessCallback,
  137. },
  138. /* kStatus_DEV_GetDes */
  139. {
  140. kStatus_DEV_GetCfg9,
  141. kStatus_DEV_GetDes,
  142. NULL,
  143. },
  144. /* kStatus_DEV_GetCfg9 */
  145. {
  146. kStatus_DEV_GetCfg,
  147. kStatus_DEV_GetCfg9,
  148. USB_HostProcessCallback,
  149. },
  150. /* kStatus_DEV_GetCfg */
  151. {
  152. kStatus_DEV_SetCfg,
  153. kStatus_DEV_GetCfg9,
  154. USB_HostProcessCallback,
  155. },
  156. /* kStatus_DEV_SetCfg */
  157. {
  158. kStatus_DEV_EnumDone,
  159. kStatus_DEV_SetCfg,
  160. NULL,
  161. },
  162. };
  163. /*******************************************************************************
  164. * Code
  165. ******************************************************************************/
  166. static void USB_HostEnumerationTransferCallback(void *param, usb_host_transfer_t *transfer, usb_status_t status)
  167. {
  168. /* 0 - retry current transfer, 1 - transfer success process,
  169. * 2 - retry whole process, 3 - fail process
  170. */
  171. uint8_t nextStep = 0U;
  172. usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)param;
  173. usb_status_t failReason = kStatus_USB_Success;
  174. uint32_t dataLength;
  175. dataLength = transfer->transferSofar;
  176. (void)USB_HostFreeTransfer(deviceInstance->hostHandle, transfer); /* free transfer */
  177. if (status == kStatus_USB_Success)
  178. {
  179. nextStep = 1U;
  180. }
  181. else if (status == kStatus_USB_TransferStall)
  182. {
  183. #if ((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
  184. (void)usb_echo("no response from device\r\n");
  185. #endif /* USB_HOST_CONFIG_COMPLIANCE_TEST */
  186. if (deviceInstance->stallRetries > 0U) /* retry same transfer when stall */
  187. {
  188. nextStep = 0U;
  189. deviceInstance->stallRetries--;
  190. }
  191. else
  192. {
  193. failReason = kStatus_USB_TransferFailed;
  194. nextStep = 2U;
  195. }
  196. }
  197. else if (status == kStatus_USB_TransferCancel)
  198. {
  199. failReason = kStatus_USB_TransferCancel;
  200. nextStep = 3U;
  201. }
  202. else
  203. {
  204. failReason = kStatus_USB_TransferFailed;
  205. nextStep = 2U;
  206. }
  207. if (nextStep == 1U)
  208. {
  209. deviceInstance->stallRetries = USB_HOST_CONFIG_ENUMERATION_MAX_STALL_RETRIES;
  210. if (s_EnumEntries[deviceInstance->state - 1U].process == NULL)
  211. {
  212. deviceInstance->state = (uint8_t)s_EnumEntries[deviceInstance->state - 1U].successState; /* next state */
  213. }
  214. else
  215. {
  216. status = s_EnumEntries[deviceInstance->state - 1U].process(
  217. deviceInstance, dataLength); /* process the previous state result */
  218. if (status == kStatus_USB_Success) /* process success */
  219. {
  220. deviceInstance->state = (uint8_t)s_EnumEntries[deviceInstance->state - 1U].successState;
  221. }
  222. else if (status == kStatus_USB_Retry) /* need retry */
  223. {
  224. deviceInstance->state = (uint8_t)s_EnumEntries[deviceInstance->state - 1U].retryState;
  225. }
  226. else if (status == kStatus_USB_NotSupported) /* device don't suport by the application */
  227. {
  228. return; /* unrecoverable fail */
  229. }
  230. else /* process error, next retry */
  231. {
  232. /* kStatus_USB_Error or kStatus_USB_AllocFail */
  233. failReason = status;
  234. nextStep = 2U;
  235. }
  236. }
  237. }
  238. if (nextStep == 2U)
  239. {
  240. if (deviceInstance->enumRetries > 0U) /* next whole retry */
  241. {
  242. deviceInstance->enumRetries--;
  243. deviceInstance->stallRetries = USB_HOST_CONFIG_ENUMERATION_MAX_STALL_RETRIES;
  244. deviceInstance->configurationValue = 0U;
  245. deviceInstance->state = (uint8_t)kStatus_DEV_GetDes8;
  246. }
  247. else
  248. {
  249. #if ((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
  250. usb_echo("Device No Response\r\n");
  251. #endif
  252. nextStep = 3U;
  253. }
  254. }
  255. /* process the state */
  256. if (nextStep != 3U)
  257. {
  258. if (USB_HostProcessState(deviceInstance) != kStatus_USB_Success)
  259. {
  260. #ifdef HOST_ECHO
  261. usb_echo("enumation setup error\r\n");
  262. #endif
  263. failReason = kStatus_USB_Error;
  264. nextStep = 3U;
  265. }
  266. }
  267. if (nextStep == 3U)
  268. {
  269. (void)USB_HostNotifyDevice(deviceInstance->hostHandle, deviceInstance, (uint32_t)kUSB_HostEventEnumerationFail,
  270. (uint32_t)failReason);
  271. }
  272. }
  273. static usb_status_t USB_HostProcessState(usb_host_device_instance_t *deviceInstance)
  274. {
  275. usb_status_t status = kStatus_USB_Success;
  276. usb_host_process_descriptor_param_t getDescriptorParam;
  277. usb_host_transfer_t *transfer;
  278. usb_host_device_enumeration_status_t state;
  279. /* malloc transfer */
  280. state = (usb_host_device_enumeration_status_t)deviceInstance->state;
  281. if (state != kStatus_DEV_EnumDone)
  282. {
  283. if (USB_HostMallocTransfer(deviceInstance->hostHandle, &transfer) != kStatus_USB_Success)
  284. {
  285. #ifdef HOST_ECHO
  286. usb_echo("error to get transfer\r\n");
  287. #endif
  288. return kStatus_USB_Error;
  289. }
  290. transfer->callbackFn = USB_HostEnumerationTransferCallback;
  291. transfer->callbackParam = deviceInstance;
  292. /* reset transfer fields */
  293. transfer->setupPacket->bmRequestType = 0x00U;
  294. transfer->setupPacket->wIndex = 0U;
  295. transfer->setupPacket->wLength = 0U;
  296. transfer->setupPacket->wValue = 0U;
  297. }
  298. switch (state)
  299. {
  300. case kStatus_DEV_GetDes8:
  301. case kStatus_DEV_GetDes: /* get descriptor state */
  302. getDescriptorParam.descriptorLength = sizeof(usb_descriptor_device_t);
  303. if (deviceInstance->state == (uint8_t)kStatus_DEV_GetDes8)
  304. {
  305. getDescriptorParam.descriptorLength = 8U;
  306. }
  307. getDescriptorParam.descriptorBuffer = (uint8_t *)deviceInstance->deviceDescriptor;
  308. getDescriptorParam.descriptorType = USB_DESCRIPTOR_TYPE_DEVICE;
  309. getDescriptorParam.descriptorIndex = 0U;
  310. getDescriptorParam.languageId = 0U;
  311. transfer->setupPacket->bmRequestType |= USB_REQUEST_TYPE_DIR_IN;
  312. transfer->setupPacket->bRequest = USB_REQUEST_STANDARD_GET_DESCRIPTOR;
  313. status = USB_HostStandardSetGetDescriptor(deviceInstance, transfer, &getDescriptorParam);
  314. break;
  315. case kStatus_DEV_SetAddress: /* set address state */
  316. transfer->setupPacket->bRequest = USB_REQUEST_STANDARD_SET_ADDRESS;
  317. status = USB_HostStandardSetAddress(deviceInstance, transfer, &deviceInstance->allocatedAddress);
  318. break;
  319. case kStatus_DEV_GetCfg9: /* get 9 bytes configuration state */
  320. getDescriptorParam.descriptorBuffer = deviceInstance->enumBuffer;
  321. getDescriptorParam.descriptorType = USB_DESCRIPTOR_TYPE_CONFIGURE;
  322. getDescriptorParam.descriptorIndex = deviceInstance->configurationValue;
  323. getDescriptorParam.descriptorLength = 9;
  324. getDescriptorParam.languageId = 0;
  325. transfer->setupPacket->bmRequestType |= USB_REQUEST_TYPE_DIR_IN;
  326. transfer->setupPacket->bRequest = USB_REQUEST_STANDARD_GET_DESCRIPTOR;
  327. status = USB_HostStandardSetGetDescriptor(deviceInstance, transfer, &getDescriptorParam);
  328. break;
  329. case kStatus_DEV_GetCfg: /* get configuration state */
  330. getDescriptorParam.descriptorBuffer = deviceInstance->configurationDesc;
  331. getDescriptorParam.descriptorType = USB_DESCRIPTOR_TYPE_CONFIGURE;
  332. getDescriptorParam.descriptorIndex = deviceInstance->configurationValue;
  333. getDescriptorParam.descriptorLength = deviceInstance->configurationLen;
  334. getDescriptorParam.languageId = 0;
  335. transfer->setupPacket->bmRequestType |= USB_REQUEST_TYPE_DIR_IN;
  336. transfer->setupPacket->bRequest = USB_REQUEST_STANDARD_GET_DESCRIPTOR;
  337. status = USB_HostStandardSetGetDescriptor(deviceInstance, transfer, &getDescriptorParam);
  338. break;
  339. case kStatus_DEV_SetCfg: /* set configuration state */
  340. transfer->setupPacket->wValue =
  341. USB_SHORT_TO_LITTLE_ENDIAN(deviceInstance->configuration.configurationDesc->bConfigurationValue);
  342. transfer->setupPacket->bRequest = USB_REQUEST_STANDARD_SET_CONFIGURATION;
  343. status = USB_HostCh9RequestCommon(deviceInstance, transfer, NULL, 0);
  344. break;
  345. case kStatus_DEV_EnumDone: /* enumeration done state */
  346. /* notify device enumeration done */
  347. status = USB_HostNotifyDevice(deviceInstance->hostHandle, deviceInstance,
  348. (uint32_t)kUSB_HostEventEnumerationDone, (uint32_t)kStatus_USB_Success);
  349. if (status == kStatus_USB_Success)
  350. {
  351. deviceInstance->state = (uint8_t)kStatus_DEV_AppUsed;
  352. }
  353. break;
  354. default:
  355. /*no action*/
  356. break;
  357. }
  358. return status;
  359. }
  360. static usb_status_t USB_HostProcessCallback(usb_host_device_instance_t *deviceInstance, uint32_t dataLength)
  361. {
  362. usb_host_pipe_t *pipe = (usb_host_pipe_t *)deviceInstance->controlPipe;
  363. usb_status_t status = kStatus_USB_Success;
  364. usb_descriptor_configuration_t *configureDesc;
  365. usb_host_instance_t *hostInstance = (usb_host_instance_t *)deviceInstance->hostHandle;
  366. void *temp;
  367. usb_host_device_enumeration_status_t state;
  368. state = (usb_host_device_enumeration_status_t)deviceInstance->state;
  369. switch (state)
  370. {
  371. case kStatus_DEV_GetDes8: /* process get 8 bytes descriptor result */
  372. if (dataLength != 8u)
  373. {
  374. return kStatus_USB_Error;
  375. }
  376. pipe->maxPacketSize = deviceInstance->deviceDescriptor->bMaxPacketSize0;
  377. /* the callbackFn is initialized in USB_HostGetControllerInterface */
  378. (void)hostInstance->controllerTable->controllerIoctl(
  379. hostInstance->controllerHandle, kUSB_HostUpdateControlPacketSize, deviceInstance->controlPipe);
  380. break;
  381. case kStatus_DEV_SetAddress: /* process set address result */
  382. deviceInstance->setAddress = deviceInstance->allocatedAddress;
  383. /* the callbackFn is initialized in USB_HostGetControllerInterface */
  384. (void)hostInstance->controllerTable->controllerIoctl(
  385. hostInstance->controllerHandle, kUSB_HostUpdateControlEndpointAddress, deviceInstance->controlPipe);
  386. break;
  387. case kStatus_DEV_GetDes: /* process get full device descriptor result */
  388. if (dataLength != sizeof(usb_descriptor_device_t))
  389. {
  390. return kStatus_USB_Error;
  391. }
  392. break;
  393. case kStatus_DEV_GetCfg9: /* process get 9 bytes configuration result */
  394. if (dataLength != 9u)
  395. {
  396. return kStatus_USB_Error;
  397. }
  398. temp = (void *)&deviceInstance->enumBuffer[0];
  399. configureDesc = (usb_descriptor_configuration_t *)temp;
  400. deviceInstance->configurationLen = USB_SHORT_FROM_LITTLE_ENDIAN_ADDRESS(configureDesc->wTotalLength);
  401. if (deviceInstance->configurationDesc != NULL)
  402. {
  403. #if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
  404. SDK_Free(deviceInstance->configurationDesc);
  405. #else
  406. OSA_MemoryFree(deviceInstance->configurationDesc);
  407. #endif
  408. deviceInstance->configurationDesc = NULL;
  409. }
  410. /* fix misra 4.14 */
  411. if (deviceInstance->configurationLen > USB_HOST_CONFIG_MAX_CONFIGURATION_DESCRIPTOR_LENGTH)
  412. {
  413. return kStatus_USB_Error;
  414. }
  415. /* for KHCI, the start address and the length should be 4 byte align */
  416. if ((deviceInstance->configurationLen & 0x03U) != 0U)
  417. {
  418. #if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
  419. deviceInstance->configurationDesc =
  420. (uint8_t *)SDK_Malloc((deviceInstance->configurationLen & 0xFFFCu) + 4, USB_CACHE_LINESIZE);
  421. #else
  422. deviceInstance->configurationDesc =
  423. (uint8_t *)OSA_MemoryAllocate((((uint32_t)deviceInstance->configurationLen) & 0xFFFCU) + 4UL);
  424. #endif
  425. }
  426. else
  427. {
  428. #if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
  429. deviceInstance->configurationDesc =
  430. (uint8_t *)SDK_Malloc(deviceInstance->configurationLen, USB_CACHE_LINESIZE);
  431. #else
  432. deviceInstance->configurationDesc = (uint8_t *)OSA_MemoryAllocate(deviceInstance->configurationLen);
  433. #endif
  434. }
  435. if (deviceInstance->configurationDesc == NULL)
  436. {
  437. return kStatus_USB_AllocFail;
  438. }
  439. break;
  440. case kStatus_DEV_GetCfg: /* process get configuration result */
  441. if (dataLength != deviceInstance->configurationLen)
  442. {
  443. return kStatus_USB_Error;
  444. }
  445. temp = (void *)deviceInstance->configurationDesc;
  446. if (((usb_descriptor_configuration_t *)temp)->bMaxPower > USB_HOST_CONFIG_MAX_POWER)
  447. {
  448. return kStatus_USB_Error;
  449. }
  450. deviceInstance->configurationValue++;
  451. if (USB_HostParseDeviceConfigurationDescriptor(deviceInstance) !=
  452. kStatus_USB_Success) /* parse configuration descriptor */
  453. {
  454. return kStatus_USB_Error;
  455. }
  456. status = USB_HostNotifyDevice(deviceInstance->hostHandle, deviceInstance, (uint32_t)kUSB_HostEventAttach,
  457. (uint32_t)kStatus_USB_Success);
  458. if (status != kStatus_USB_Success)
  459. {
  460. /* next configuration */
  461. if (deviceInstance->configurationValue < deviceInstance->deviceDescriptor->bNumConfigurations)
  462. {
  463. return kStatus_USB_Retry;
  464. }
  465. else
  466. {
  467. /* notify application device is not supported */
  468. (void)USB_HostNotifyDevice(deviceInstance->hostHandle, deviceInstance,
  469. (uint32_t)kUSB_HostEventNotSupported, (uint32_t)kStatus_USB_Success);
  470. return kStatus_USB_NotSupported;
  471. }
  472. }
  473. break;
  474. case kStatus_DEV_SetCfg:
  475. /* NULL */
  476. break;
  477. default:
  478. /*no action*/
  479. break;
  480. }
  481. return status;
  482. }
  483. static usb_status_t USB_HostNotifyDevice(usb_host_handle hostHandle,
  484. usb_host_device_instance_t *deviceInstance,
  485. uint32_t eventCode,
  486. uint32_t eventParameter)
  487. {
  488. usb_host_instance_t *hostInstance;
  489. usb_status_t status1 = kStatus_USB_Error;
  490. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  491. usb_status_t status2 = kStatus_USB_Error;
  492. uint8_t haveHub;
  493. uint8_t haveNoHub;
  494. uint8_t interfaceIndex;
  495. #endif /* USB_HOST_CONFIG_HUB */
  496. eventCode = (((uint32_t)eventParameter << 16U) | (uint32_t)eventCode);
  497. hostInstance = (usb_host_instance_t *)hostHandle;
  498. if (deviceInstance == NULL)
  499. {
  500. (void)hostInstance->deviceCallback(NULL, NULL, eventCode);
  501. return kStatus_USB_InvalidHandle;
  502. }
  503. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  504. haveHub = 0U;
  505. haveNoHub = 0U;
  506. for (interfaceIndex = 0U; interfaceIndex < deviceInstance->configuration.interfaceCount; ++interfaceIndex)
  507. {
  508. if (((usb_descriptor_interface_t *)deviceInstance->configuration.interfaceList[interfaceIndex].interfaceDesc)
  509. ->bInterfaceClass == USB_HOST_HUB_CLASS_CODE)
  510. {
  511. haveHub = 1U;
  512. }
  513. else
  514. {
  515. haveNoHub = 1U;
  516. }
  517. }
  518. if ((hostInstance->deviceCallback != NULL) &&
  519. ((haveNoHub == 1U) || (deviceInstance->configuration.interfaceCount == 0U)))
  520. {
  521. /* call host callback function, function is initialized in USB_HostInit */
  522. status1 = hostInstance->deviceCallback(deviceInstance, &deviceInstance->configuration, eventCode);
  523. }
  524. if (0U != haveHub)
  525. {
  526. status2 = USB_HostHubDeviceEvent(hostInstance, deviceInstance, &deviceInstance->configuration,
  527. eventCode); /* notify hub event */
  528. }
  529. if ((status1 == kStatus_USB_Success) || (status2 == kStatus_USB_Success)) /* the device is supported */
  530. {
  531. return kStatus_USB_Success;
  532. }
  533. else if (eventCode == (uint32_t)kUSB_HostEventAttach) /* attach event */
  534. {
  535. status1 = kStatus_USB_NotSupported;
  536. }
  537. else
  538. {
  539. status1 = kStatus_USB_Error;
  540. }
  541. #else
  542. if (hostInstance->deviceCallback != NULL)
  543. {
  544. /* call host callback function, function is initialized in USB_HostInit */
  545. status1 = hostInstance->deviceCallback(deviceInstance, &deviceInstance->configuration, eventCode);
  546. }
  547. #endif
  548. return status1;
  549. }
  550. static uint8_t USB_HostAllocateDeviceAddress(usb_host_instance_t *hostInstance)
  551. {
  552. uint8_t address = 0U;
  553. uint8_t addressIndex;
  554. uint8_t addressBitIndex;
  555. for (addressIndex = 0U; addressIndex < 8U; ++addressIndex) /* find the idle address position byte */
  556. {
  557. if (hostInstance->addressBitMap[addressIndex] != 0xFFU)
  558. {
  559. break;
  560. }
  561. }
  562. if (addressIndex < 8U)
  563. {
  564. for (addressBitIndex = 0U; addressBitIndex < 8U; ++addressBitIndex) /* find the idle address position bit */
  565. {
  566. if (0U == (hostInstance->addressBitMap[addressIndex] & (0x01U << addressBitIndex)))
  567. {
  568. hostInstance->addressBitMap[addressIndex] |= (0x01U << addressBitIndex); /* set the allocated bit */
  569. address = addressIndex * 8U + addressBitIndex + 1U; /* the address minimum is 1 */
  570. break;
  571. }
  572. }
  573. }
  574. return address;
  575. }
  576. static void USB_HostReleaseDeviceAddress(usb_host_instance_t *hostInstance, uint8_t address)
  577. {
  578. (void)USB_HostLock();
  579. hostInstance->addressBitMap[((uint32_t)address - 1U) >> 3U] &=
  580. (~(0x01U << (((uint32_t)address - 1U) & 0x07U))); /* reset the allocated bit */
  581. (void)USB_HostUnlock();
  582. }
  583. static usb_status_t USB_HostRemoveDeviceInstance(usb_host_handle hostHandle, usb_device_handle deviceHandle)
  584. {
  585. usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
  586. usb_host_device_instance_t *currentInstance;
  587. usb_host_device_instance_t *prevInstance;
  588. if ((hostHandle == NULL) || (deviceHandle == NULL))
  589. {
  590. return kStatus_USB_InvalidHandle;
  591. }
  592. /* search and remove device instance */
  593. prevInstance = (usb_host_device_instance_t *)hostInstance->deviceList;
  594. if (prevInstance == deviceHandle)
  595. {
  596. hostInstance->deviceList = prevInstance->next;
  597. return kStatus_USB_Success;
  598. }
  599. else
  600. {
  601. currentInstance = prevInstance->next;
  602. }
  603. while (currentInstance != NULL)
  604. {
  605. if (currentInstance == deviceHandle)
  606. {
  607. prevInstance->next = currentInstance->next;
  608. return kStatus_USB_Success;
  609. }
  610. prevInstance = currentInstance;
  611. currentInstance = currentInstance->next;
  612. }
  613. return kStatus_USB_Success;
  614. }
  615. static void USB_HostReleaseDeviceResource(usb_host_instance_t *hostInstance, usb_host_device_instance_t *deviceInstance)
  616. {
  617. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  618. uint8_t level = 0;
  619. #endif
  620. #if ((defined(USB_HOST_CONFIG_LOW_POWER_MODE)) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
  621. if (deviceInstance == hostInstance->suspendedDevice)
  622. {
  623. hostInstance->suspendedDevice = NULL;
  624. }
  625. #endif
  626. /* release device's address */
  627. if (deviceInstance->setAddress != 0U)
  628. {
  629. USB_HostReleaseDeviceAddress(hostInstance, deviceInstance->setAddress);
  630. }
  631. else
  632. {
  633. if (deviceInstance->allocatedAddress != 0U)
  634. {
  635. USB_HostReleaseDeviceAddress(hostInstance, deviceInstance->allocatedAddress);
  636. }
  637. }
  638. /* close control pipe */
  639. if (deviceInstance->controlPipe != NULL)
  640. {
  641. (void)USB_HostCancelTransfer(hostInstance, deviceInstance->controlPipe, NULL);
  642. if (USB_HostClosePipe(hostInstance, deviceInstance->controlPipe) != kStatus_USB_Success)
  643. {
  644. #ifdef HOST_ECHO
  645. usb_echo("error when close pipe\r\n");
  646. #endif
  647. }
  648. deviceInstance->controlPipe = NULL;
  649. }
  650. /* free configuration buffer */
  651. if (deviceInstance->configurationDesc != NULL)
  652. {
  653. #if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
  654. SDK_Free(deviceInstance->configurationDesc);
  655. #else
  656. OSA_MemoryFree(deviceInstance->configurationDesc);
  657. #endif
  658. }
  659. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  660. level = deviceInstance->level;
  661. #endif
  662. #if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
  663. SDK_Free(deviceInstance->deviceDescriptor);
  664. #else
  665. OSA_MemoryFree(deviceInstance->deviceDescriptor);
  666. #endif
  667. /* free device instance buffer */
  668. OSA_MemoryFree(deviceInstance);
  669. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  670. /* enable controller attach if root hub */
  671. if (level == 1U)
  672. {
  673. (void)USB_HostControlBus(hostInstance, (uint8_t)kUSB_HostBusEnableAttach);
  674. }
  675. #else
  676. /* enable controller attach */
  677. USB_HostControlBus(hostInstance, (uint8_t)kUSB_HostBusEnableAttach);
  678. #endif
  679. }
  680. static usb_status_t USB_HostParseDeviceConfigurationDescriptor(usb_device_handle deviceHandle)
  681. {
  682. usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)deviceHandle;
  683. uint32_t endPos;
  684. usb_descriptor_union_t *unionDes;
  685. usb_host_interface_t *interfaceParse = NULL;
  686. usb_host_ep_t *epParse;
  687. uint8_t *buffer;
  688. void *temp;
  689. if (deviceHandle == NULL)
  690. {
  691. return kStatus_USB_InvalidParameter;
  692. }
  693. temp = (void *)&deviceInstance->configuration;
  694. buffer = (uint8_t *)temp;
  695. /* clear the previous parse result, note: end_pos means buffer index here*/
  696. for (endPos = 0U; endPos < sizeof(usb_host_configuration_t); endPos++)
  697. {
  698. buffer[endPos] = 0U;
  699. }
  700. for (endPos = 0U; endPos < USB_HOST_CONFIG_CONFIGURATION_MAX_INTERFACE; ++endPos)
  701. {
  702. deviceInstance->interfaceStatus[endPos] = 0U;
  703. }
  704. /* parse configuration descriptor */
  705. temp = (void *)deviceInstance->configurationDesc;
  706. unionDes = (usb_descriptor_union_t *)temp;
  707. endPos = (uint32_t)(deviceInstance->configurationDesc + deviceInstance->configurationLen);
  708. if ((unionDes->common.bLength == USB_DESCRIPTOR_LENGTH_CONFIGURE) &&
  709. (unionDes->common.bDescriptorType == USB_DESCRIPTOR_TYPE_CONFIGURE))
  710. {
  711. /* configuration descriptor */
  712. temp = (void *)unionDes;
  713. deviceInstance->configuration.configurationDesc = (usb_descriptor_configuration_t *)temp;
  714. deviceInstance->configuration.configurationExtensionLength = 0;
  715. deviceInstance->configuration.configurationExtension = NULL;
  716. deviceInstance->configuration.interfaceCount = 0;
  717. unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
  718. while ((uint32_t)unionDes < endPos)
  719. {
  720. if (unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_INTERFACE)
  721. {
  722. if (deviceInstance->configuration.configurationExtension == NULL)
  723. {
  724. deviceInstance->configuration.configurationExtension = (uint8_t *)unionDes;
  725. }
  726. if ((unionDes->common.bDescriptorType == 0x00U) ||
  727. (unionDes->common.bLength == 0x00U)) /* the descriptor data is wrong */
  728. {
  729. return kStatus_USB_Error;
  730. }
  731. deviceInstance->configuration.configurationExtensionLength += unionDes->common.bLength;
  732. unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
  733. }
  734. else
  735. {
  736. break;
  737. }
  738. }
  739. /* interface descriptor */
  740. deviceInstance->configuration.interfaceCount = 0U;
  741. while ((uint32_t)unionDes < endPos)
  742. {
  743. if (unionDes->common.bDescriptorType == USB_DESCRIPTOR_TYPE_INTERFACE)
  744. {
  745. if (unionDes->interface.bAlternateSetting == 0x00U)
  746. {
  747. if (deviceInstance->configuration.interfaceCount >= USB_HOST_CONFIG_CONFIGURATION_MAX_INTERFACE)
  748. {
  749. #if (((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST)) || defined(HOST_ECHO))
  750. (void)usb_echo(
  751. "Unsupported Device attached\r\n too many interfaces in one configuration, please increase "
  752. "the USB_HOST_CONFIG_CONFIGURATION_MAX_INTERFACE value\n");
  753. #endif
  754. return kStatus_USB_Error;
  755. }
  756. interfaceParse =
  757. &deviceInstance->configuration.interfaceList[deviceInstance->configuration.interfaceCount];
  758. deviceInstance->configuration.interfaceCount++;
  759. interfaceParse->alternateSettingNumber = 0;
  760. interfaceParse->epCount = 0;
  761. interfaceParse->interfaceDesc = &unionDes->interface;
  762. interfaceParse->interfaceExtensionLength = 0;
  763. interfaceParse->interfaceExtension = NULL;
  764. interfaceParse->interfaceIndex = unionDes->interface.bInterfaceNumber;
  765. if (unionDes->common.bLength == 0x00U) /* the descriptor data is wrong */
  766. {
  767. return kStatus_USB_Error;
  768. }
  769. unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
  770. while ((uint32_t)unionDes < endPos)
  771. {
  772. if ((unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_INTERFACE) &&
  773. (unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_ENDPOINT))
  774. {
  775. if (interfaceParse->interfaceExtension == NULL)
  776. {
  777. interfaceParse->interfaceExtension = (uint8_t *)unionDes;
  778. }
  779. if ((unionDes->common.bDescriptorType == 0x00U) ||
  780. (unionDes->common.bLength == 0x00U)) /* the descriptor data is wrong */
  781. {
  782. return kStatus_USB_Error;
  783. }
  784. interfaceParse->interfaceExtensionLength += unionDes->common.bLength;
  785. unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
  786. }
  787. else
  788. {
  789. break;
  790. }
  791. }
  792. /* endpoint descriptor */
  793. if (interfaceParse->interfaceDesc->bNumEndpoints != 0U)
  794. {
  795. if ((unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_ENDPOINT) ||
  796. (interfaceParse->interfaceDesc->bNumEndpoints > USB_HOST_CONFIG_INTERFACE_MAX_EP))
  797. {
  798. #ifdef HOST_ECHO
  799. usb_echo("interface descriptor error\n");
  800. #endif
  801. return kStatus_USB_Error;
  802. }
  803. for (; interfaceParse->epCount < interfaceParse->interfaceDesc->bNumEndpoints;
  804. (interfaceParse->epCount)++)
  805. {
  806. if (((uint32_t)unionDes >= endPos) ||
  807. (unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_ENDPOINT))
  808. {
  809. #ifdef HOST_ECHO
  810. usb_echo("endpoint descriptor error\n");
  811. #endif
  812. return kStatus_USB_Error;
  813. }
  814. temp = (void *)&interfaceParse->epList[interfaceParse->epCount];
  815. epParse = (usb_host_ep_t *)temp;
  816. temp = (void *)unionDes;
  817. epParse->epDesc = (usb_descriptor_endpoint_t *)temp;
  818. epParse->epExtensionLength = 0U;
  819. epParse->epExtension = NULL;
  820. if (unionDes->common.bLength == 0x00U) /* the descriptor data is wrong */
  821. {
  822. return kStatus_USB_Error;
  823. }
  824. unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
  825. while ((uint32_t)unionDes < endPos)
  826. {
  827. if ((unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_ENDPOINT) &&
  828. (unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_INTERFACE))
  829. {
  830. if (epParse->epExtension == NULL)
  831. {
  832. epParse->epExtension = (uint8_t *)unionDes;
  833. }
  834. if ((unionDes->common.bDescriptorType == 0x00U) ||
  835. (unionDes->common.bLength == 0x00U)) /* the descriptor data is wrong */
  836. {
  837. return kStatus_USB_Error;
  838. }
  839. epParse->epExtensionLength += unionDes->common.bLength;
  840. unionDes =
  841. (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
  842. }
  843. else
  844. {
  845. break;
  846. }
  847. }
  848. }
  849. }
  850. }
  851. else
  852. {
  853. if (interfaceParse == NULL)
  854. {
  855. return kStatus_USB_Error; /* in normal situation this cannot reach */
  856. }
  857. interfaceParse->alternateSettingNumber++;
  858. if (interfaceParse->interfaceExtension == NULL)
  859. {
  860. interfaceParse->interfaceExtension = (uint8_t *)unionDes;
  861. }
  862. if (unionDes->common.bLength == 0x00U) /* the descriptor data is wrong */
  863. {
  864. return kStatus_USB_Error;
  865. }
  866. interfaceParse->interfaceExtensionLength += unionDes->common.bLength;
  867. unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
  868. while ((uint32_t)unionDes < endPos)
  869. {
  870. if (unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_INTERFACE)
  871. {
  872. if ((unionDes->common.bDescriptorType == 0x00U) ||
  873. (unionDes->common.bLength == 0x00U)) /* the descriptor data is wrong */
  874. {
  875. return kStatus_USB_Error;
  876. }
  877. interfaceParse->interfaceExtensionLength += unionDes->common.bLength;
  878. unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
  879. }
  880. else
  881. {
  882. break;
  883. }
  884. }
  885. }
  886. }
  887. else
  888. {
  889. return kStatus_USB_Error;
  890. }
  891. }
  892. }
  893. for (endPos = 0U; endPos < deviceInstance->configuration.interfaceCount; ++endPos)
  894. {
  895. deviceInstance->interfaceStatus[endPos] = (uint8_t)kStatus_interface_Attached;
  896. }
  897. return kStatus_USB_Success;
  898. }
  899. usb_status_t USB_HostAttachDevice(usb_host_handle hostHandle,
  900. uint8_t speed,
  901. uint8_t hubNumber,
  902. uint8_t portNumber,
  903. uint8_t level,
  904. usb_device_handle *deviceHandle)
  905. {
  906. usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
  907. usb_host_device_instance_t *newInstance;
  908. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  909. usb_host_device_instance_t *currentInstance;
  910. #endif
  911. uint8_t address;
  912. usb_host_pipe_init_t pipeInit;
  913. if (hostHandle == NULL)
  914. {
  915. return kStatus_USB_InvalidHandle;
  916. }
  917. /* check whether is the device attached? */
  918. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  919. currentInstance = (usb_host_device_instance_t *)hostInstance->deviceList;
  920. while (currentInstance != NULL)
  921. {
  922. if ((currentInstance->hubNumber == hubNumber) && (currentInstance->portNumber == portNumber))
  923. {
  924. *deviceHandle = NULL;
  925. #ifdef HOST_ECHO
  926. usb_echo("device has attached\r\n");
  927. #endif
  928. return kStatus_USB_Busy;
  929. }
  930. else
  931. {
  932. currentInstance = currentInstance->next;
  933. }
  934. }
  935. #else
  936. if (hostInstance->deviceList != NULL)
  937. {
  938. *deviceHandle = NULL;
  939. usb_echo("device has attached\r\n");
  940. return kStatus_USB_Busy;
  941. }
  942. #endif
  943. /* Allocate new device instance */
  944. newInstance = (usb_host_device_instance_t *)OSA_MemoryAllocate(sizeof(usb_host_device_instance_t));
  945. if (newInstance == NULL)
  946. {
  947. #ifdef HOST_ECHO
  948. usb_echo("allocate dev instance fail\r\n");
  949. #endif
  950. (void)USB_HostNotifyDevice(hostInstance, NULL, (uint32_t)kUSB_HostEventEnumerationFail,
  951. (uint32_t)kStatus_USB_AllocFail);
  952. return kStatus_USB_AllocFail;
  953. }
  954. /* new instance fields init */
  955. newInstance->hostHandle = hostHandle;
  956. newInstance->speed = speed;
  957. newInstance->stallRetries = USB_HOST_CONFIG_ENUMERATION_MAX_STALL_RETRIES;
  958. newInstance->enumRetries = USB_HOST_CONFIG_ENUMERATION_MAX_RETRIES;
  959. newInstance->setAddress = 0;
  960. newInstance->deviceAttachState = (uint8_t)kStatus_device_Attached;
  961. #if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
  962. newInstance->deviceDescriptor =
  963. (usb_descriptor_device_t *)SDK_Malloc(sizeof(usb_descriptor_device_t) + 9, USB_CACHE_LINESIZE);
  964. #else
  965. newInstance->deviceDescriptor = (usb_descriptor_device_t *)OSA_MemoryAllocate(sizeof(usb_descriptor_device_t) + 9U);
  966. #endif
  967. if (newInstance->deviceDescriptor == NULL)
  968. {
  969. #ifdef HOST_ECHO
  970. usb_echo("allocate newInstance->deviceDescriptor fail\r\n");
  971. #endif
  972. #if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
  973. SDK_Free(newInstance->deviceDescriptor);
  974. #else
  975. OSA_MemoryFree(newInstance->deviceDescriptor);
  976. #endif
  977. OSA_MemoryFree(newInstance);
  978. (void)USB_HostNotifyDevice(hostInstance, NULL, (uint32_t)kUSB_HostEventEnumerationFail,
  979. (uint32_t)kStatus_USB_AllocFail);
  980. return kStatus_USB_AllocFail;
  981. }
  982. newInstance->enumBuffer = (uint8_t *)((uint8_t *)newInstance->deviceDescriptor + sizeof(usb_descriptor_device_t));
  983. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  984. newInstance->hubNumber = hubNumber;
  985. newInstance->portNumber = portNumber;
  986. newInstance->level = level;
  987. if ((speed != USB_SPEED_HIGH) && (level > 1U))
  988. {
  989. newInstance->hsHubNumber = (uint8_t)USB_HostHubGetHsHubNumber(hostHandle, hubNumber);
  990. newInstance->hsHubPort = (uint8_t)USB_HostHubGetHsHubPort(hostHandle, hubNumber, portNumber);
  991. }
  992. else
  993. {
  994. newInstance->hsHubNumber = hubNumber;
  995. newInstance->hsHubPort = portNumber;
  996. }
  997. #endif /* USB_HOST_CONFIG_HUB */
  998. (void)USB_HostLock();
  999. /* allocate address && insert to the dev list */
  1000. address = USB_HostAllocateDeviceAddress(hostInstance);
  1001. if (address == 0U)
  1002. {
  1003. #ifdef HOST_ECHO
  1004. usb_echo("allocate address fail\r\n");
  1005. #endif
  1006. (void)USB_HostUnlock();
  1007. #if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
  1008. SDK_Free(newInstance->deviceDescriptor);
  1009. #else
  1010. OSA_MemoryFree(newInstance->deviceDescriptor);
  1011. #endif
  1012. OSA_MemoryFree(newInstance);
  1013. (void)USB_HostNotifyDevice(hostInstance, NULL, (uint32_t)kUSB_HostEventEnumerationFail,
  1014. (uint32_t)kStatus_USB_Error);
  1015. return kStatus_USB_Error;
  1016. }
  1017. newInstance->allocatedAddress = address;
  1018. newInstance->next = (usb_host_device_instance_t *)hostInstance->deviceList;
  1019. hostInstance->deviceList = newInstance;
  1020. newInstance->state = (uint8_t)kStatus_DEV_Initial;
  1021. (void)USB_HostUnlock();
  1022. /* open control pipe */
  1023. pipeInit.devInstance = newInstance;
  1024. pipeInit.pipeType = USB_ENDPOINT_CONTROL;
  1025. pipeInit.direction = 0;
  1026. pipeInit.endpointAddress = 0;
  1027. pipeInit.interval = 0;
  1028. pipeInit.maxPacketSize = 8;
  1029. pipeInit.numberPerUframe = 0;
  1030. pipeInit.nakCount = USB_HOST_CONFIG_MAX_NAK;
  1031. if (USB_HostOpenPipe(hostHandle, &newInstance->controlPipe, &pipeInit) != kStatus_USB_Success)
  1032. {
  1033. /* don't need release resource, resource is released when detach */
  1034. *deviceHandle = newInstance;
  1035. #if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
  1036. SDK_Free(newInstance->deviceDescriptor);
  1037. #else
  1038. OSA_MemoryFree(newInstance->deviceDescriptor);
  1039. #endif
  1040. OSA_MemoryFree(newInstance);
  1041. (void)USB_HostNotifyDevice(hostInstance, NULL, (uint32_t)kUSB_HostEventEnumerationFail,
  1042. (uint32_t)kStatus_USB_Error);
  1043. return kStatus_USB_Error;
  1044. }
  1045. /* start enumeration */
  1046. newInstance->state = (uint8_t)kStatus_DEV_GetDes8;
  1047. /* process enumeration state machine */
  1048. if (USB_HostProcessState(newInstance) != kStatus_USB_Success)
  1049. {
  1050. (void)USB_HostNotifyDevice(hostInstance, newInstance, (uint32_t)kUSB_HostEventEnumerationFail,
  1051. (uint32_t)kStatus_USB_Error);
  1052. }
  1053. *deviceHandle = newInstance;
  1054. return kStatus_USB_Success;
  1055. }
  1056. usb_status_t USB_HostDetachDevice(usb_host_handle hostHandle, uint8_t hubNumber, uint8_t portNumber)
  1057. {
  1058. usb_host_device_instance_t *deviceInstance;
  1059. usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
  1060. if (hostHandle == NULL)
  1061. {
  1062. return kStatus_USB_InvalidHandle;
  1063. }
  1064. (void)USB_HostLock();
  1065. /* search for device instance handle */
  1066. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  1067. deviceInstance = (usb_host_device_instance_t *)hostInstance->deviceList;
  1068. while (deviceInstance != NULL)
  1069. {
  1070. if ((deviceInstance->hubNumber == hubNumber) && (deviceInstance->portNumber == portNumber))
  1071. {
  1072. break;
  1073. }
  1074. deviceInstance = deviceInstance->next;
  1075. }
  1076. #else
  1077. deviceInstance = (usb_host_device_instance_t *)hostInstance->deviceList;
  1078. #endif
  1079. (void)USB_HostUnlock();
  1080. if (deviceInstance != NULL)
  1081. {
  1082. return USB_HostDetachDeviceInternal(hostHandle, deviceInstance); /* device instance detach */
  1083. }
  1084. return kStatus_USB_Success;
  1085. }
  1086. usb_status_t USB_HostDetachDeviceInternal(usb_host_handle hostHandle, usb_device_handle deviceHandle)
  1087. {
  1088. usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)deviceHandle;
  1089. usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
  1090. if ((hostHandle == NULL) || (deviceHandle == NULL))
  1091. {
  1092. return kStatus_USB_InvalidHandle;
  1093. }
  1094. deviceInstance->deviceAttachState = (uint8_t)kStatus_device_Detached; /* mark the device is detached from host */
  1095. if (deviceInstance->state >= (uint8_t)kStatus_DEV_Initial) /* device instance is valid */
  1096. {
  1097. /* detach internally */
  1098. if (deviceInstance->state < (uint8_t)kStatus_DEV_AppUsed) /* enumeration is not done */
  1099. {
  1100. if (deviceInstance->controlPipe != NULL)
  1101. {
  1102. (void)USB_HostCancelTransfer(hostInstance, deviceInstance->controlPipe, NULL);
  1103. }
  1104. /* remove device instance from host */
  1105. (void)USB_HostRemoveDeviceInstance(hostInstance, deviceInstance);
  1106. USB_HostReleaseDeviceResource(hostInstance, deviceInstance);
  1107. }
  1108. else /* enumeration has be done and notified application */
  1109. {
  1110. /* notify application device detach */
  1111. (void)USB_HostNotifyDevice(hostInstance, deviceInstance, (uint32_t)kUSB_HostEventDetach,
  1112. (uint32_t)kStatus_USB_Success);
  1113. }
  1114. }
  1115. return kStatus_USB_Success;
  1116. }
  1117. uint8_t USB_HostGetDeviceAttachState(usb_device_handle deviceHandle)
  1118. {
  1119. return (NULL != deviceHandle) ? ((usb_host_device_instance_t *)deviceHandle)->deviceAttachState : 0x0U;
  1120. }
  1121. usb_status_t USB_HostValidateDevice(usb_host_handle hostHandle, usb_device_handle deviceHandle)
  1122. {
  1123. usb_host_device_instance_t *searchDev;
  1124. if (deviceHandle == NULL)
  1125. {
  1126. return kStatus_USB_InvalidParameter;
  1127. }
  1128. /* search for the device */
  1129. searchDev = (usb_host_device_instance_t *)((usb_host_instance_t *)hostHandle)->deviceList;
  1130. while ((searchDev != NULL) && ((usb_device_handle)searchDev != deviceHandle))
  1131. {
  1132. searchDev = searchDev->next;
  1133. }
  1134. if (NULL != searchDev)
  1135. {
  1136. return kStatus_USB_Success;
  1137. }
  1138. return kStatus_USB_Error;
  1139. }
  1140. static usb_status_t USB_HostControlBus(usb_host_handle hostHandle, uint8_t controlType)
  1141. {
  1142. usb_status_t status = kStatus_USB_Success;
  1143. usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
  1144. if (hostHandle == NULL)
  1145. {
  1146. return kStatus_USB_InvalidHandle;
  1147. }
  1148. /* the callbackFn is initialized in USB_HostGetControllerInterface */
  1149. status = hostInstance->controllerTable->controllerIoctl(hostInstance->controllerHandle, kUSB_HostBusControl,
  1150. &controlType);
  1151. return status;
  1152. }
  1153. usb_status_t USB_HostOpenDeviceInterface(usb_device_handle deviceHandle, usb_host_interface_handle interfaceHandle)
  1154. {
  1155. usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)deviceHandle;
  1156. usb_host_instance_t *hostInstance = NULL;
  1157. uint8_t interfaceIndex;
  1158. uint8_t index = 0;
  1159. if ((deviceHandle == NULL) || (interfaceHandle == NULL))
  1160. {
  1161. return kStatus_USB_InvalidHandle;
  1162. }
  1163. hostInstance = (usb_host_instance_t *)deviceInstance->hostHandle;
  1164. (void)USB_HostLock();
  1165. /* check host_instance valid? */
  1166. for (; index < USB_HOST_CONFIG_MAX_HOST; ++index)
  1167. {
  1168. if ((g_UsbHostInstance[index].occupied == 1U) &&
  1169. ((usb_host_instance_t *)(&g_UsbHostInstance[index]) == (hostInstance)))
  1170. {
  1171. break;
  1172. }
  1173. }
  1174. if (index >= USB_HOST_CONFIG_MAX_HOST)
  1175. {
  1176. (void)USB_HostUnlock();
  1177. return kStatus_USB_Error;
  1178. }
  1179. /* check deviceHandle valid? */
  1180. if (USB_HostValidateDevice(hostInstance, deviceHandle) != kStatus_USB_Success)
  1181. {
  1182. (void)USB_HostUnlock();
  1183. return kStatus_USB_Error;
  1184. }
  1185. /* search interface and set the interface as opened */
  1186. for (interfaceIndex = 0; interfaceIndex < deviceInstance->configuration.interfaceCount; ++interfaceIndex)
  1187. {
  1188. if (&deviceInstance->configuration.interfaceList[interfaceIndex] == interfaceHandle)
  1189. {
  1190. deviceInstance->interfaceStatus[interfaceIndex] = (uint8_t)kStatus_interface_Opened;
  1191. break;
  1192. }
  1193. }
  1194. (void)USB_HostUnlock();
  1195. return kStatus_USB_Success;
  1196. }
  1197. usb_status_t USB_HostCloseDeviceInterface(usb_device_handle deviceHandle, usb_host_interface_handle interfaceHandle)
  1198. {
  1199. usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)deviceHandle;
  1200. usb_host_instance_t *hostInstance = NULL;
  1201. uint8_t interfaceIndex;
  1202. uint8_t removeLabel = 1;
  1203. uint8_t index = 0;
  1204. if (deviceHandle == NULL)
  1205. {
  1206. return kStatus_USB_InvalidHandle;
  1207. }
  1208. hostInstance = (usb_host_instance_t *)deviceInstance->hostHandle;
  1209. (void)USB_HostLock();
  1210. /* check host_instance valid? */
  1211. for (; index < USB_HOST_CONFIG_MAX_HOST; ++index)
  1212. {
  1213. if ((g_UsbHostInstance[index].occupied == 1U) &&
  1214. ((usb_host_instance_t *)(&g_UsbHostInstance[index]) == (hostInstance)))
  1215. {
  1216. break;
  1217. }
  1218. }
  1219. if (index >= USB_HOST_CONFIG_MAX_HOST)
  1220. {
  1221. (void)USB_HostUnlock();
  1222. return kStatus_USB_Error;
  1223. }
  1224. /* check deviceHandle valid? */
  1225. if (USB_HostValidateDevice(hostInstance, deviceHandle) != kStatus_USB_Success)
  1226. {
  1227. (void)USB_HostUnlock();
  1228. return kStatus_USB_Error;
  1229. }
  1230. if (interfaceHandle != NULL)
  1231. {
  1232. /* search interface and set the interface as detached */
  1233. for (interfaceIndex = 0; interfaceIndex < deviceInstance->configuration.interfaceCount; ++interfaceIndex)
  1234. {
  1235. if (&deviceInstance->configuration.interfaceList[interfaceIndex] == interfaceHandle)
  1236. {
  1237. deviceInstance->interfaceStatus[interfaceIndex] = (uint8_t)kStatus_interface_Detached;
  1238. break;
  1239. }
  1240. }
  1241. }
  1242. if (deviceInstance->deviceAttachState == (uint8_t)kStatus_device_Detached) /* device is removed from host */
  1243. {
  1244. removeLabel = 1;
  1245. /* check all the interfaces of the device are not opened */
  1246. for (interfaceIndex = 0; interfaceIndex < deviceInstance->configuration.interfaceCount; ++interfaceIndex)
  1247. {
  1248. if (deviceInstance->interfaceStatus[interfaceIndex] == (uint8_t)kStatus_interface_Opened)
  1249. {
  1250. removeLabel = 0U;
  1251. break;
  1252. }
  1253. }
  1254. if (removeLabel == 1U)
  1255. {
  1256. /* remove device instance from host */
  1257. (void)USB_HostRemoveDeviceInstance(hostInstance, deviceInstance);
  1258. }
  1259. (void)USB_HostUnlock();
  1260. if (removeLabel == 1U)
  1261. {
  1262. USB_HostReleaseDeviceResource((usb_host_instance_t *)deviceInstance->hostHandle,
  1263. deviceInstance); /* release device resource */
  1264. }
  1265. }
  1266. else
  1267. {
  1268. (void)USB_HostUnlock();
  1269. }
  1270. return kStatus_USB_Success;
  1271. }
  1272. usb_status_t USB_HostRemoveDevice(usb_host_handle hostHandle, usb_device_handle deviceHandle)
  1273. {
  1274. usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
  1275. usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)deviceHandle;
  1276. uint8_t interfaceIndex = 0;
  1277. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  1278. uint8_t level = 0;
  1279. uint8_t devHubNo;
  1280. uint8_t devPortNo;
  1281. #endif
  1282. if ((hostHandle == NULL) || (deviceHandle == NULL))
  1283. {
  1284. return kStatus_USB_InvalidHandle;
  1285. }
  1286. if (deviceInstance->hostHandle != hostHandle)
  1287. {
  1288. return kStatus_USB_InvalidParameter;
  1289. }
  1290. if (USB_HostValidateDevice(hostInstance, deviceInstance) == kStatus_USB_Success) /* device is valid */
  1291. {
  1292. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  1293. devHubNo = deviceInstance->hubNumber;
  1294. devPortNo = deviceInstance->portNumber;
  1295. level = deviceInstance->level;
  1296. #endif
  1297. deviceInstance->deviceAttachState = (uint8_t)kStatus_device_Detached;
  1298. if (deviceInstance->state >= (uint8_t)kStatus_DEV_Initial) /* device is valid */
  1299. {
  1300. if (deviceInstance->state <
  1301. (uint8_t)kStatus_DEV_AppUsed) /* enumeration is not done or application don't use */
  1302. {
  1303. /* detach internally */
  1304. (void)USB_HostDetachDeviceInternal(hostHandle, deviceHandle);
  1305. }
  1306. else /* application use the device */
  1307. {
  1308. for (interfaceIndex = 0U; interfaceIndex < deviceInstance->configuration.interfaceCount;
  1309. ++interfaceIndex)
  1310. {
  1311. if (deviceInstance->interfaceStatus[interfaceIndex] == (uint8_t)kStatus_interface_Opened)
  1312. {
  1313. #ifdef HOST_ECHO
  1314. usb_echo("error: there is class instance that is not deinited\r\n");
  1315. #endif
  1316. break;
  1317. }
  1318. }
  1319. /* remove device instance from host */
  1320. (void)USB_HostRemoveDeviceInstance(hostInstance, deviceInstance);
  1321. USB_HostReleaseDeviceResource(hostInstance, deviceInstance); /* release resource */
  1322. }
  1323. }
  1324. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  1325. if (level == 1U)
  1326. {
  1327. (void)USB_HostControlBus(hostHandle, (uint8_t)kUSB_HostBusReset); /* reset controller port */
  1328. (void)USB_HostControlBus(hostHandle, (uint8_t)kUSB_HostBusRestart); /* restart controller port */
  1329. }
  1330. else
  1331. {
  1332. (void)USB_HostHubRemovePort(hostHandle, devHubNo, devPortNo); /* reset hub port */
  1333. }
  1334. #else
  1335. USB_HostControlBus(hostHandle, kUSB_HostBusReset); /* reset controller port */
  1336. USB_HostControlBus(hostHandle, kUSB_HostBusRestart); /* restart controller port */
  1337. #endif /* USB_HOST_CONFIG_HUB */
  1338. }
  1339. return kStatus_USB_Success;
  1340. }