usb_host_devices.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  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 = (uint16_t)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. #if (defined(USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
  369. #if (defined(USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  370. usb_host_hub_instance_t *hubInstance4Device = NULL;
  371. #endif
  372. #endif
  373. state = (usb_host_device_enumeration_status_t)deviceInstance->state;
  374. switch (state)
  375. {
  376. case kStatus_DEV_GetDes8: /* process get 8 bytes descriptor result */
  377. if (dataLength != 8u)
  378. {
  379. return kStatus_USB_Error;
  380. }
  381. pipe->maxPacketSize = deviceInstance->deviceDescriptor->bMaxPacketSize0;
  382. /* the callbackFn is initialized in USB_HostGetControllerInterface */
  383. (void)hostInstance->controllerTable->controllerIoctl(
  384. hostInstance->controllerHandle, kUSB_HostUpdateControlPacketSize, deviceInstance->controlPipe);
  385. break;
  386. case kStatus_DEV_SetAddress: /* process set address result */
  387. deviceInstance->setAddress = deviceInstance->allocatedAddress;
  388. /* the callbackFn is initialized in USB_HostGetControllerInterface */
  389. (void)hostInstance->controllerTable->controllerIoctl(
  390. hostInstance->controllerHandle, kUSB_HostUpdateControlEndpointAddress, deviceInstance->controlPipe);
  391. break;
  392. case kStatus_DEV_GetDes: /* process get full device descriptor result */
  393. if (dataLength != sizeof(usb_descriptor_device_t))
  394. {
  395. return kStatus_USB_Error;
  396. }
  397. break;
  398. case kStatus_DEV_GetCfg9: /* process get 9 bytes configuration result */
  399. if (dataLength != 9u)
  400. {
  401. return kStatus_USB_Error;
  402. }
  403. temp = (void *)&deviceInstance->enumBuffer[0];
  404. configureDesc = (usb_descriptor_configuration_t *)temp;
  405. deviceInstance->configurationLen = USB_SHORT_FROM_LITTLE_ENDIAN_ADDRESS(configureDesc->wTotalLength);
  406. if (deviceInstance->configurationDesc != NULL)
  407. {
  408. #if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
  409. SDK_Free(deviceInstance->configurationDesc);
  410. #else
  411. OSA_MemoryFree(deviceInstance->configurationDesc);
  412. #endif
  413. deviceInstance->configurationDesc = NULL;
  414. }
  415. /* the only configuration descriptor is at least 9 bytes length */
  416. if (deviceInstance->configurationLen < 9U)
  417. {
  418. return kStatus_USB_Error;
  419. }
  420. /* fix misra 4.14 */
  421. if (deviceInstance->configurationLen > USB_HOST_CONFIG_MAX_CONFIGURATION_DESCRIPTOR_LENGTH)
  422. {
  423. return kStatus_USB_Error;
  424. }
  425. /* for KHCI, the start address and the length should be 4 byte align */
  426. if ((deviceInstance->configurationLen & 0x03U) != 0U)
  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 & 0xFFFCu) + 4, USB_CACHE_LINESIZE);
  431. #else
  432. deviceInstance->configurationDesc =
  433. (uint8_t *)OSA_MemoryAllocate((((uint32_t)deviceInstance->configurationLen) & 0xFFFCU) + 4UL);
  434. #endif
  435. }
  436. else
  437. {
  438. #if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
  439. deviceInstance->configurationDesc =
  440. (uint8_t *)SDK_Malloc(deviceInstance->configurationLen, USB_CACHE_LINESIZE);
  441. #else
  442. deviceInstance->configurationDesc = (uint8_t *)OSA_MemoryAllocate(deviceInstance->configurationLen);
  443. #endif
  444. }
  445. if (deviceInstance->configurationDesc == NULL)
  446. {
  447. return kStatus_USB_AllocFail;
  448. }
  449. break;
  450. case kStatus_DEV_GetCfg: /* process get configuration result */
  451. if (dataLength != deviceInstance->configurationLen)
  452. {
  453. #if (defined(USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
  454. usb_echo("Host can only provide a maximum of 500mA current\r\n");
  455. #endif
  456. return kStatus_USB_Error;
  457. }
  458. #if (defined(USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
  459. #if (defined(USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  460. hubInstance4Device = USB_HostHubGetHubDeviceHandle(hostInstance, deviceInstance->hubNumber);
  461. if ((!(((usb_descriptor_configuration_t *)deviceInstance->configurationDesc)->bmAttributes &
  462. USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_SELF_POWERED_MASK)) &&
  463. (((usb_descriptor_configuration_t *)deviceInstance->configurationDesc)->bMaxPower > 50) &&
  464. (hubInstance4Device != NULL) &&
  465. (!(((usb_descriptor_configuration_t *)((usb_host_device_instance_t *)hubInstance4Device->deviceHandle)
  466. ->configurationDesc)
  467. ->bmAttributes &
  468. USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_SELF_POWERED_MASK)))
  469. {
  470. usb_echo("The device power exceeded\r\n");
  471. return kStatus_USB_Error;
  472. }
  473. #endif
  474. #endif
  475. temp = (void *)deviceInstance->configurationDesc;
  476. if (((usb_descriptor_configuration_t *)temp)->bMaxPower > USB_HOST_CONFIG_MAX_POWER)
  477. {
  478. return kStatus_USB_Error;
  479. }
  480. deviceInstance->configurationValue++;
  481. if (USB_HostParseDeviceConfigurationDescriptor(deviceInstance) !=
  482. kStatus_USB_Success) /* parse configuration descriptor */
  483. {
  484. return kStatus_USB_Error;
  485. }
  486. status = USB_HostNotifyDevice(deviceInstance->hostHandle, deviceInstance, (uint32_t)kUSB_HostEventAttach,
  487. (uint32_t)kStatus_USB_Success);
  488. if (status != kStatus_USB_Success)
  489. {
  490. /* next configuration */
  491. if (deviceInstance->configurationValue < deviceInstance->deviceDescriptor->bNumConfigurations)
  492. {
  493. return kStatus_USB_Retry;
  494. }
  495. else
  496. {
  497. /* notify application device is not supported */
  498. (void)USB_HostNotifyDevice(deviceInstance->hostHandle, deviceInstance,
  499. (uint32_t)kUSB_HostEventNotSupported, (uint32_t)kStatus_USB_Success);
  500. return kStatus_USB_NotSupported;
  501. }
  502. }
  503. break;
  504. case kStatus_DEV_SetCfg:
  505. /* NULL */
  506. break;
  507. default:
  508. /*no action*/
  509. break;
  510. }
  511. return status;
  512. }
  513. static usb_status_t USB_HostNotifyDevice(usb_host_handle hostHandle,
  514. usb_host_device_instance_t *deviceInstance,
  515. uint32_t eventCode,
  516. uint32_t eventParameter)
  517. {
  518. usb_host_instance_t *hostInstance;
  519. usb_status_t status1 = kStatus_USB_Error;
  520. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  521. usb_status_t status2 = kStatus_USB_Error;
  522. uint8_t haveHub;
  523. uint8_t haveNoHub;
  524. uint8_t interfaceIndex;
  525. #endif /* USB_HOST_CONFIG_HUB */
  526. eventCode = (((uint32_t)eventParameter << 16U) | (uint32_t)eventCode);
  527. hostInstance = (usb_host_instance_t *)hostHandle;
  528. if (deviceInstance == NULL)
  529. {
  530. (void)hostInstance->deviceCallback(NULL, NULL, eventCode);
  531. return kStatus_USB_InvalidHandle;
  532. }
  533. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  534. haveHub = 0U;
  535. haveNoHub = 0U;
  536. for (interfaceIndex = 0U; interfaceIndex < deviceInstance->configuration.interfaceCount; ++interfaceIndex)
  537. {
  538. if (((usb_descriptor_interface_t *)deviceInstance->configuration.interfaceList[interfaceIndex].interfaceDesc)
  539. ->bInterfaceClass == USB_HOST_HUB_CLASS_CODE)
  540. {
  541. haveHub = 1U;
  542. }
  543. else
  544. {
  545. haveNoHub = 1U;
  546. }
  547. }
  548. if ((hostInstance->deviceCallback != NULL) &&
  549. ((haveNoHub == 1U) || (deviceInstance->configuration.interfaceCount == 0U)))
  550. {
  551. /* call host callback function, function is initialized in USB_HostInit */
  552. status1 = hostInstance->deviceCallback(deviceInstance, &deviceInstance->configuration, eventCode);
  553. }
  554. if (0U != haveHub)
  555. {
  556. status2 = USB_HostHubDeviceEvent(hostInstance, deviceInstance, &deviceInstance->configuration,
  557. eventCode); /* notify hub event */
  558. }
  559. if ((status1 == kStatus_USB_Success) || (status2 == kStatus_USB_Success)) /* the device is supported */
  560. {
  561. return kStatus_USB_Success;
  562. }
  563. else if (eventCode == (uint32_t)kUSB_HostEventAttach) /* attach event */
  564. {
  565. status1 = kStatus_USB_NotSupported;
  566. }
  567. else
  568. {
  569. status1 = kStatus_USB_Error;
  570. }
  571. #else
  572. if (hostInstance->deviceCallback != NULL)
  573. {
  574. /* call host callback function, function is initialized in USB_HostInit */
  575. status1 = hostInstance->deviceCallback(deviceInstance, &deviceInstance->configuration, eventCode);
  576. }
  577. #endif
  578. return status1;
  579. }
  580. static uint8_t USB_HostAllocateDeviceAddress(usb_host_instance_t *hostInstance)
  581. {
  582. uint8_t address = 0U;
  583. uint8_t addressIndex;
  584. uint8_t addressBitIndex;
  585. for (addressIndex = 0U; addressIndex < 8U; ++addressIndex) /* find the idle address position byte */
  586. {
  587. if (hostInstance->addressBitMap[addressIndex] != 0xFFU)
  588. {
  589. break;
  590. }
  591. }
  592. if (addressIndex < 8U)
  593. {
  594. for (addressBitIndex = 0U; addressBitIndex < 8U; ++addressBitIndex) /* find the idle address position bit */
  595. {
  596. if (0U == (hostInstance->addressBitMap[addressIndex] & (0x01U << addressBitIndex)))
  597. {
  598. hostInstance->addressBitMap[addressIndex] |= (0x01U << addressBitIndex); /* set the allocated bit */
  599. address = addressIndex * 8U + addressBitIndex + 1U; /* the address minimum is 1 */
  600. break;
  601. }
  602. }
  603. }
  604. return address;
  605. }
  606. static void USB_HostReleaseDeviceAddress(usb_host_instance_t *hostInstance, uint8_t address)
  607. {
  608. (void)USB_HostLock();
  609. hostInstance->addressBitMap[((uint32_t)address - 1U) >> 3U] &=
  610. (~(0x01U << (((uint32_t)address - 1U) & 0x07U))); /* reset the allocated bit */
  611. (void)USB_HostUnlock();
  612. }
  613. static usb_status_t USB_HostRemoveDeviceInstance(usb_host_handle hostHandle, usb_device_handle deviceHandle)
  614. {
  615. usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
  616. usb_host_device_instance_t *currentInstance;
  617. usb_host_device_instance_t *prevInstance;
  618. if ((hostHandle == NULL) || (deviceHandle == NULL))
  619. {
  620. return kStatus_USB_InvalidHandle;
  621. }
  622. /* search and remove device instance */
  623. prevInstance = (usb_host_device_instance_t *)hostInstance->deviceList;
  624. if (prevInstance == deviceHandle)
  625. {
  626. hostInstance->deviceList = prevInstance->next;
  627. return kStatus_USB_Success;
  628. }
  629. else
  630. {
  631. currentInstance = prevInstance->next;
  632. }
  633. while (currentInstance != NULL)
  634. {
  635. if (currentInstance == deviceHandle)
  636. {
  637. prevInstance->next = currentInstance->next;
  638. return kStatus_USB_Success;
  639. }
  640. prevInstance = currentInstance;
  641. currentInstance = currentInstance->next;
  642. }
  643. return kStatus_USB_Success;
  644. }
  645. static void USB_HostReleaseDeviceResource(usb_host_instance_t *hostInstance, usb_host_device_instance_t *deviceInstance)
  646. {
  647. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  648. uint8_t level = 0;
  649. #endif
  650. #if ((defined(USB_HOST_CONFIG_LOW_POWER_MODE)) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
  651. if (deviceInstance == hostInstance->suspendedDevice)
  652. {
  653. hostInstance->suspendedDevice = NULL;
  654. }
  655. #endif
  656. /* release device's address */
  657. if (deviceInstance->setAddress != 0U)
  658. {
  659. USB_HostReleaseDeviceAddress(hostInstance, deviceInstance->setAddress);
  660. }
  661. else
  662. {
  663. if (deviceInstance->allocatedAddress != 0U)
  664. {
  665. USB_HostReleaseDeviceAddress(hostInstance, deviceInstance->allocatedAddress);
  666. }
  667. }
  668. /* close control pipe */
  669. if (deviceInstance->controlPipe != NULL)
  670. {
  671. (void)USB_HostCancelTransfer(hostInstance, deviceInstance->controlPipe, NULL);
  672. if (USB_HostClosePipe(hostInstance, deviceInstance->controlPipe) != kStatus_USB_Success)
  673. {
  674. #ifdef HOST_ECHO
  675. usb_echo("error when close pipe\r\n");
  676. #endif
  677. }
  678. deviceInstance->controlPipe = NULL;
  679. }
  680. /* free configuration buffer */
  681. if (deviceInstance->configurationDesc != NULL)
  682. {
  683. #if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
  684. SDK_Free(deviceInstance->configurationDesc);
  685. #else
  686. OSA_MemoryFree(deviceInstance->configurationDesc);
  687. #endif
  688. }
  689. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  690. level = deviceInstance->level;
  691. #endif
  692. #if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
  693. SDK_Free(deviceInstance->deviceDescriptor);
  694. #else
  695. OSA_MemoryFree(deviceInstance->deviceDescriptor);
  696. #endif
  697. /* free device instance buffer */
  698. OSA_MemoryFree(deviceInstance);
  699. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  700. /* enable controller attach if root hub */
  701. if (level == 1U)
  702. {
  703. (void)USB_HostControlBus(hostInstance, (uint8_t)kUSB_HostBusEnableAttach);
  704. }
  705. #else
  706. /* enable controller attach */
  707. USB_HostControlBus(hostInstance, (uint8_t)kUSB_HostBusEnableAttach);
  708. #endif
  709. }
  710. static usb_status_t USB_HostParseDeviceConfigurationDescriptor(usb_device_handle deviceHandle)
  711. {
  712. usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)deviceHandle;
  713. uint32_t endPos;
  714. usb_descriptor_union_t *unionDes;
  715. usb_host_interface_t *interfaceParse = NULL;
  716. usb_host_ep_t *epParse;
  717. uint8_t *buffer;
  718. void *temp;
  719. if (deviceHandle == NULL)
  720. {
  721. return kStatus_USB_InvalidParameter;
  722. }
  723. temp = (void *)&deviceInstance->configuration;
  724. buffer = (uint8_t *)temp;
  725. /* clear the previous parse result, note: end_pos means buffer index here*/
  726. for (endPos = 0U; endPos < sizeof(usb_host_configuration_t); endPos++)
  727. {
  728. buffer[endPos] = 0U;
  729. }
  730. for (endPos = 0U; endPos < USB_HOST_CONFIG_CONFIGURATION_MAX_INTERFACE; ++endPos)
  731. {
  732. deviceInstance->interfaceStatus[endPos] = 0U;
  733. }
  734. /* parse configuration descriptor */
  735. temp = (void *)deviceInstance->configurationDesc;
  736. unionDes = (usb_descriptor_union_t *)temp;
  737. endPos = (uint32_t)(deviceInstance->configurationDesc + deviceInstance->configurationLen);
  738. if (((((uint32_t)unionDes) + 1U) < endPos) && (unionDes->common.bLength == USB_DESCRIPTOR_LENGTH_CONFIGURE) &&
  739. (unionDes->common.bDescriptorType == USB_DESCRIPTOR_TYPE_CONFIGURE))
  740. {
  741. /* configuration descriptor */
  742. temp = (void *)unionDes;
  743. deviceInstance->configuration.configurationDesc = (usb_descriptor_configuration_t *)temp;
  744. deviceInstance->configuration.configurationExtensionLength = 0;
  745. deviceInstance->configuration.configurationExtension = NULL;
  746. deviceInstance->configuration.interfaceCount = 0;
  747. unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
  748. while ((uint32_t)unionDes < endPos)
  749. {
  750. if (((((uint32_t)unionDes) + 1U) < endPos) &&
  751. (unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_INTERFACE))
  752. {
  753. if (deviceInstance->configuration.configurationExtension == NULL)
  754. {
  755. deviceInstance->configuration.configurationExtension = (uint8_t *)unionDes;
  756. }
  757. if ((unionDes->common.bDescriptorType == 0x00U) ||
  758. (unionDes->common.bLength == 0x00U)) /* the descriptor data is wrong */
  759. {
  760. return kStatus_USB_Error;
  761. }
  762. deviceInstance->configuration.configurationExtensionLength += unionDes->common.bLength;
  763. unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
  764. }
  765. else
  766. {
  767. break;
  768. }
  769. }
  770. /* interface descriptor */
  771. deviceInstance->configuration.interfaceCount = 0U;
  772. while ((uint32_t)unionDes < endPos)
  773. {
  774. if (((((uint32_t)unionDes) + 1U) < endPos) &&
  775. (unionDes->common.bDescriptorType == USB_DESCRIPTOR_TYPE_INTERFACE))
  776. {
  777. /* the interface descriptor length is 9 bytes */
  778. if ((((uint32_t)unionDes) + 9U) > endPos)
  779. {
  780. return kStatus_USB_Error;
  781. }
  782. if (unionDes->interface.bAlternateSetting == 0x00U)
  783. {
  784. if (deviceInstance->configuration.interfaceCount >= USB_HOST_CONFIG_CONFIGURATION_MAX_INTERFACE)
  785. {
  786. #if (((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST)) || defined(HOST_ECHO))
  787. (void)usb_echo(
  788. "Unsupported Device attached\r\n too many interfaces in one configuration, please increase "
  789. "the USB_HOST_CONFIG_CONFIGURATION_MAX_INTERFACE value\n");
  790. #endif
  791. return kStatus_USB_Error;
  792. }
  793. interfaceParse =
  794. &deviceInstance->configuration.interfaceList[deviceInstance->configuration.interfaceCount];
  795. deviceInstance->configuration.interfaceCount++;
  796. interfaceParse->alternateSettingNumber = 0;
  797. interfaceParse->epCount = 0;
  798. interfaceParse->interfaceDesc = &unionDes->interface;
  799. interfaceParse->interfaceExtensionLength = 0;
  800. interfaceParse->interfaceExtension = NULL;
  801. interfaceParse->interfaceIndex = unionDes->interface.bInterfaceNumber;
  802. if (unionDes->common.bLength == 0x00U) /* the descriptor data is wrong */
  803. {
  804. return kStatus_USB_Error;
  805. }
  806. unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
  807. while ((uint32_t)unionDes < endPos)
  808. {
  809. if (((((uint32_t)unionDes) + 1U) < endPos) &&
  810. (unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_INTERFACE) &&
  811. (unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_ENDPOINT))
  812. {
  813. if (interfaceParse->interfaceExtension == NULL)
  814. {
  815. interfaceParse->interfaceExtension = (uint8_t *)unionDes;
  816. }
  817. if ((unionDes->common.bDescriptorType == 0x00U) ||
  818. (unionDes->common.bLength == 0x00U)) /* the descriptor data is wrong */
  819. {
  820. return kStatus_USB_Error;
  821. }
  822. interfaceParse->interfaceExtensionLength += unionDes->common.bLength;
  823. unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
  824. }
  825. else
  826. {
  827. break;
  828. }
  829. }
  830. /* endpoint descriptor */
  831. if (interfaceParse->interfaceDesc->bNumEndpoints != 0U)
  832. {
  833. if (((((uint32_t)unionDes) + 1U) >= endPos) ||
  834. (unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_ENDPOINT) ||
  835. (interfaceParse->interfaceDesc->bNumEndpoints > USB_HOST_CONFIG_INTERFACE_MAX_EP))
  836. {
  837. #ifdef HOST_ECHO
  838. usb_echo("interface descriptor error\n");
  839. #endif
  840. return kStatus_USB_Error;
  841. }
  842. for (; interfaceParse->epCount < interfaceParse->interfaceDesc->bNumEndpoints;
  843. (interfaceParse->epCount)++)
  844. {
  845. if (((((uint32_t)unionDes) + 1U) >= endPos) ||
  846. (unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_ENDPOINT))
  847. {
  848. #ifdef HOST_ECHO
  849. usb_echo("endpoint descriptor error\n");
  850. #endif
  851. return kStatus_USB_Error;
  852. }
  853. temp = (void *)&interfaceParse->epList[interfaceParse->epCount];
  854. epParse = (usb_host_ep_t *)temp;
  855. temp = (void *)unionDes;
  856. epParse->epDesc = (usb_descriptor_endpoint_t *)temp;
  857. epParse->epExtensionLength = 0U;
  858. epParse->epExtension = NULL;
  859. if (unionDes->common.bLength == 0x00U) /* the descriptor data is wrong */
  860. {
  861. return kStatus_USB_Error;
  862. }
  863. unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
  864. while ((uint32_t)unionDes < endPos)
  865. {
  866. if (((((uint32_t)unionDes) + 1U) < endPos) &&
  867. (unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_ENDPOINT) &&
  868. (unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_INTERFACE))
  869. {
  870. if (epParse->epExtension == NULL)
  871. {
  872. epParse->epExtension = (uint8_t *)unionDes;
  873. }
  874. if ((unionDes->common.bDescriptorType == 0x00U) ||
  875. (unionDes->common.bLength == 0x00U)) /* the descriptor data is wrong */
  876. {
  877. return kStatus_USB_Error;
  878. }
  879. epParse->epExtensionLength += unionDes->common.bLength;
  880. unionDes =
  881. (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
  882. }
  883. else
  884. {
  885. break;
  886. }
  887. }
  888. }
  889. }
  890. }
  891. else
  892. {
  893. if (interfaceParse == NULL)
  894. {
  895. return kStatus_USB_Error; /* in normal situation this cannot reach */
  896. }
  897. interfaceParse->alternateSettingNumber++;
  898. if (interfaceParse->interfaceExtension == NULL)
  899. {
  900. interfaceParse->interfaceExtension = (uint8_t *)unionDes;
  901. }
  902. if (unionDes->common.bLength == 0x00U) /* the descriptor data is wrong */
  903. {
  904. return kStatus_USB_Error;
  905. }
  906. interfaceParse->interfaceExtensionLength += unionDes->common.bLength;
  907. unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
  908. while ((uint32_t)unionDes < endPos)
  909. {
  910. if (((((uint32_t)unionDes) + 1U) < endPos) &&
  911. (unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_INTERFACE))
  912. {
  913. if ((unionDes->common.bDescriptorType == 0x00U) ||
  914. (unionDes->common.bLength == 0x00U)) /* the descriptor data is wrong */
  915. {
  916. return kStatus_USB_Error;
  917. }
  918. interfaceParse->interfaceExtensionLength += unionDes->common.bLength;
  919. unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
  920. }
  921. else
  922. {
  923. break;
  924. }
  925. }
  926. }
  927. }
  928. else
  929. {
  930. return kStatus_USB_Error;
  931. }
  932. }
  933. }
  934. else
  935. {
  936. return kStatus_USB_Error;
  937. }
  938. for (endPos = 0U; endPos < deviceInstance->configuration.interfaceCount; ++endPos)
  939. {
  940. deviceInstance->interfaceStatus[endPos] = (uint8_t)kStatus_interface_Attached;
  941. }
  942. return kStatus_USB_Success;
  943. }
  944. usb_status_t USB_HostAttachDevice(usb_host_handle hostHandle,
  945. uint8_t speed,
  946. uint8_t hubNumber,
  947. uint8_t portNumber,
  948. uint8_t level,
  949. usb_device_handle *deviceHandle)
  950. {
  951. usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
  952. usb_host_device_instance_t *newInstance;
  953. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  954. usb_host_device_instance_t *currentInstance;
  955. #endif
  956. uint8_t address;
  957. usb_host_pipe_init_t pipeInit;
  958. if (hostHandle == NULL)
  959. {
  960. return kStatus_USB_InvalidHandle;
  961. }
  962. /* check whether is the device attached? */
  963. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  964. currentInstance = (usb_host_device_instance_t *)hostInstance->deviceList;
  965. while (currentInstance != NULL)
  966. {
  967. if ((currentInstance->hubNumber == hubNumber) && (currentInstance->portNumber == portNumber))
  968. {
  969. *deviceHandle = NULL;
  970. #ifdef HOST_ECHO
  971. usb_echo("device has attached\r\n");
  972. #endif
  973. return kStatus_USB_Busy;
  974. }
  975. else
  976. {
  977. currentInstance = currentInstance->next;
  978. }
  979. }
  980. #else
  981. if (hostInstance->deviceList != NULL)
  982. {
  983. *deviceHandle = NULL;
  984. usb_echo("device has attached\r\n");
  985. return kStatus_USB_Busy;
  986. }
  987. #endif
  988. /* Allocate new device instance */
  989. newInstance = (usb_host_device_instance_t *)OSA_MemoryAllocate(sizeof(usb_host_device_instance_t));
  990. if (newInstance == NULL)
  991. {
  992. #ifdef HOST_ECHO
  993. usb_echo("allocate dev instance fail\r\n");
  994. #endif
  995. (void)USB_HostNotifyDevice(hostInstance, NULL, (uint32_t)kUSB_HostEventEnumerationFail,
  996. (uint32_t)kStatus_USB_AllocFail);
  997. return kStatus_USB_AllocFail;
  998. }
  999. /* new instance fields init */
  1000. newInstance->hostHandle = hostHandle;
  1001. newInstance->speed = speed;
  1002. newInstance->stallRetries = USB_HOST_CONFIG_ENUMERATION_MAX_STALL_RETRIES;
  1003. newInstance->enumRetries = USB_HOST_CONFIG_ENUMERATION_MAX_RETRIES;
  1004. newInstance->setAddress = 0;
  1005. newInstance->deviceAttachState = (uint8_t)kStatus_device_Attached;
  1006. #if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
  1007. newInstance->deviceDescriptor =
  1008. (usb_descriptor_device_t *)SDK_Malloc(sizeof(usb_descriptor_device_t) + 9, USB_CACHE_LINESIZE);
  1009. #else
  1010. newInstance->deviceDescriptor = (usb_descriptor_device_t *)OSA_MemoryAllocate(sizeof(usb_descriptor_device_t) + 9U);
  1011. #endif
  1012. if (newInstance->deviceDescriptor == NULL)
  1013. {
  1014. #ifdef HOST_ECHO
  1015. usb_echo("allocate newInstance->deviceDescriptor fail\r\n");
  1016. #endif
  1017. #if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
  1018. SDK_Free(newInstance->deviceDescriptor);
  1019. #else
  1020. OSA_MemoryFree(newInstance->deviceDescriptor);
  1021. #endif
  1022. OSA_MemoryFree(newInstance);
  1023. (void)USB_HostNotifyDevice(hostInstance, NULL, (uint32_t)kUSB_HostEventEnumerationFail,
  1024. (uint32_t)kStatus_USB_AllocFail);
  1025. return kStatus_USB_AllocFail;
  1026. }
  1027. newInstance->enumBuffer = (uint8_t *)((uint8_t *)newInstance->deviceDescriptor + sizeof(usb_descriptor_device_t));
  1028. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  1029. newInstance->hubNumber = hubNumber;
  1030. newInstance->portNumber = portNumber;
  1031. newInstance->level = level;
  1032. if ((speed != USB_SPEED_HIGH) && (level > 1U))
  1033. {
  1034. newInstance->hsHubNumber = (uint8_t)USB_HostHubGetHsHubNumber(hostHandle, hubNumber);
  1035. newInstance->hsHubPort = (uint8_t)USB_HostHubGetHsHubPort(hostHandle, hubNumber, portNumber);
  1036. }
  1037. else
  1038. {
  1039. newInstance->hsHubNumber = hubNumber;
  1040. newInstance->hsHubPort = portNumber;
  1041. }
  1042. #endif /* USB_HOST_CONFIG_HUB */
  1043. (void)USB_HostLock();
  1044. /* allocate address && insert to the dev list */
  1045. address = USB_HostAllocateDeviceAddress(hostInstance);
  1046. if (address == 0U)
  1047. {
  1048. #ifdef HOST_ECHO
  1049. usb_echo("allocate address fail\r\n");
  1050. #endif
  1051. (void)USB_HostUnlock();
  1052. #if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
  1053. SDK_Free(newInstance->deviceDescriptor);
  1054. #else
  1055. OSA_MemoryFree(newInstance->deviceDescriptor);
  1056. #endif
  1057. OSA_MemoryFree(newInstance);
  1058. (void)USB_HostNotifyDevice(hostInstance, NULL, (uint32_t)kUSB_HostEventEnumerationFail,
  1059. (uint32_t)kStatus_USB_Error);
  1060. return kStatus_USB_Error;
  1061. }
  1062. newInstance->allocatedAddress = address;
  1063. newInstance->next = (usb_host_device_instance_t *)hostInstance->deviceList;
  1064. hostInstance->deviceList = newInstance;
  1065. newInstance->state = (uint8_t)kStatus_DEV_Initial;
  1066. (void)USB_HostUnlock();
  1067. /* open control pipe */
  1068. pipeInit.devInstance = newInstance;
  1069. pipeInit.pipeType = USB_ENDPOINT_CONTROL;
  1070. pipeInit.direction = 0;
  1071. pipeInit.endpointAddress = 0;
  1072. pipeInit.interval = 0;
  1073. pipeInit.maxPacketSize = 8;
  1074. pipeInit.numberPerUframe = 0;
  1075. pipeInit.nakCount = USB_HOST_CONFIG_MAX_NAK;
  1076. if (USB_HostOpenPipe(hostHandle, &newInstance->controlPipe, &pipeInit) != kStatus_USB_Success)
  1077. {
  1078. /* don't need release resource, resource is released when detach */
  1079. *deviceHandle = newInstance;
  1080. #if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
  1081. SDK_Free(newInstance->deviceDescriptor);
  1082. #else
  1083. OSA_MemoryFree(newInstance->deviceDescriptor);
  1084. #endif
  1085. OSA_MemoryFree(newInstance);
  1086. (void)USB_HostNotifyDevice(hostInstance, NULL, (uint32_t)kUSB_HostEventEnumerationFail,
  1087. (uint32_t)kStatus_USB_Error);
  1088. return kStatus_USB_Error;
  1089. }
  1090. /* start enumeration */
  1091. newInstance->state = (uint8_t)kStatus_DEV_GetDes8;
  1092. /* process enumeration state machine */
  1093. if (USB_HostProcessState(newInstance) != kStatus_USB_Success)
  1094. {
  1095. (void)USB_HostNotifyDevice(hostInstance, newInstance, (uint32_t)kUSB_HostEventEnumerationFail,
  1096. (uint32_t)kStatus_USB_Error);
  1097. }
  1098. *deviceHandle = newInstance;
  1099. return kStatus_USB_Success;
  1100. }
  1101. usb_status_t USB_HostDetachDevice(usb_host_handle hostHandle, uint8_t hubNumber, uint8_t portNumber)
  1102. {
  1103. usb_host_device_instance_t *deviceInstance;
  1104. usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
  1105. if (hostHandle == NULL)
  1106. {
  1107. return kStatus_USB_InvalidHandle;
  1108. }
  1109. (void)USB_HostLock();
  1110. /* search for device instance handle */
  1111. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  1112. deviceInstance = (usb_host_device_instance_t *)hostInstance->deviceList;
  1113. while (deviceInstance != NULL)
  1114. {
  1115. if ((deviceInstance->hubNumber == hubNumber) && (deviceInstance->portNumber == portNumber))
  1116. {
  1117. break;
  1118. }
  1119. deviceInstance = deviceInstance->next;
  1120. }
  1121. #else
  1122. deviceInstance = (usb_host_device_instance_t *)hostInstance->deviceList;
  1123. #endif
  1124. (void)USB_HostUnlock();
  1125. if (deviceInstance != NULL)
  1126. {
  1127. return USB_HostDetachDeviceInternal(hostHandle, deviceInstance); /* device instance detach */
  1128. }
  1129. return kStatus_USB_Success;
  1130. }
  1131. usb_status_t USB_HostDetachDeviceInternal(usb_host_handle hostHandle, usb_device_handle deviceHandle)
  1132. {
  1133. usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)deviceHandle;
  1134. usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
  1135. if ((hostHandle == NULL) || (deviceHandle == NULL))
  1136. {
  1137. return kStatus_USB_InvalidHandle;
  1138. }
  1139. deviceInstance->deviceAttachState = (uint8_t)kStatus_device_Detached; /* mark the device is detached from host */
  1140. if (deviceInstance->state >= (uint8_t)kStatus_DEV_Initial) /* device instance is valid */
  1141. {
  1142. /* detach internally */
  1143. if (deviceInstance->state < (uint8_t)kStatus_DEV_AppUsed) /* enumeration is not done */
  1144. {
  1145. if (deviceInstance->controlPipe != NULL)
  1146. {
  1147. (void)USB_HostCancelTransfer(hostInstance, deviceInstance->controlPipe, NULL);
  1148. }
  1149. /* remove device instance from host */
  1150. (void)USB_HostRemoveDeviceInstance(hostInstance, deviceInstance);
  1151. USB_HostReleaseDeviceResource(hostInstance, deviceInstance);
  1152. }
  1153. else /* enumeration has be done and notified application */
  1154. {
  1155. /* notify application device detach */
  1156. (void)USB_HostNotifyDevice(hostInstance, deviceInstance, (uint32_t)kUSB_HostEventDetach,
  1157. (uint32_t)kStatus_USB_Success);
  1158. }
  1159. }
  1160. return kStatus_USB_Success;
  1161. }
  1162. uint8_t USB_HostGetDeviceAttachState(usb_device_handle deviceHandle)
  1163. {
  1164. return (NULL != deviceHandle) ? ((usb_host_device_instance_t *)deviceHandle)->deviceAttachState : 0x0U;
  1165. }
  1166. usb_status_t USB_HostValidateDevice(usb_host_handle hostHandle, usb_device_handle deviceHandle)
  1167. {
  1168. usb_host_device_instance_t *searchDev;
  1169. if (deviceHandle == NULL)
  1170. {
  1171. return kStatus_USB_InvalidParameter;
  1172. }
  1173. /* search for the device */
  1174. searchDev = (usb_host_device_instance_t *)((usb_host_instance_t *)hostHandle)->deviceList;
  1175. while ((searchDev != NULL) && ((usb_device_handle)searchDev != deviceHandle))
  1176. {
  1177. searchDev = searchDev->next;
  1178. }
  1179. if (NULL != searchDev)
  1180. {
  1181. return kStatus_USB_Success;
  1182. }
  1183. return kStatus_USB_Error;
  1184. }
  1185. static usb_status_t USB_HostControlBus(usb_host_handle hostHandle, uint8_t controlType)
  1186. {
  1187. usb_status_t status = kStatus_USB_Success;
  1188. usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
  1189. if (hostHandle == NULL)
  1190. {
  1191. return kStatus_USB_InvalidHandle;
  1192. }
  1193. /* the callbackFn is initialized in USB_HostGetControllerInterface */
  1194. status = hostInstance->controllerTable->controllerIoctl(hostInstance->controllerHandle, kUSB_HostBusControl,
  1195. &controlType);
  1196. return status;
  1197. }
  1198. usb_status_t USB_HostOpenDeviceInterface(usb_device_handle deviceHandle, usb_host_interface_handle interfaceHandle)
  1199. {
  1200. usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)deviceHandle;
  1201. usb_host_instance_t *hostInstance = NULL;
  1202. uint8_t interfaceIndex;
  1203. uint8_t index = 0;
  1204. if ((deviceHandle == NULL) || (interfaceHandle == 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 < (uint8_t)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 >= (uint8_t)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. /* search interface and set the interface as opened */
  1231. for (interfaceIndex = 0; interfaceIndex < deviceInstance->configuration.interfaceCount; ++interfaceIndex)
  1232. {
  1233. if (&deviceInstance->configuration.interfaceList[interfaceIndex] == interfaceHandle)
  1234. {
  1235. deviceInstance->interfaceStatus[interfaceIndex] = (uint8_t)kStatus_interface_Opened;
  1236. break;
  1237. }
  1238. }
  1239. (void)USB_HostUnlock();
  1240. return kStatus_USB_Success;
  1241. }
  1242. usb_status_t USB_HostCloseDeviceInterface(usb_device_handle deviceHandle, usb_host_interface_handle interfaceHandle)
  1243. {
  1244. usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)deviceHandle;
  1245. usb_host_instance_t *hostInstance = NULL;
  1246. uint8_t interfaceIndex;
  1247. uint8_t removeLabel = 1;
  1248. uint8_t index = 0;
  1249. if (deviceHandle == NULL)
  1250. {
  1251. return kStatus_USB_InvalidHandle;
  1252. }
  1253. hostInstance = (usb_host_instance_t *)deviceInstance->hostHandle;
  1254. (void)USB_HostLock();
  1255. /* check host_instance valid? */
  1256. for (; index < (uint8_t)USB_HOST_CONFIG_MAX_HOST; ++index)
  1257. {
  1258. if ((g_UsbHostInstance[index].occupied == 1U) &&
  1259. ((usb_host_instance_t *)(&g_UsbHostInstance[index]) == (hostInstance)))
  1260. {
  1261. break;
  1262. }
  1263. }
  1264. if (index >= (uint8_t)USB_HOST_CONFIG_MAX_HOST)
  1265. {
  1266. (void)USB_HostUnlock();
  1267. return kStatus_USB_Error;
  1268. }
  1269. /* check deviceHandle valid? */
  1270. if (USB_HostValidateDevice(hostInstance, deviceHandle) != kStatus_USB_Success)
  1271. {
  1272. (void)USB_HostUnlock();
  1273. return kStatus_USB_Error;
  1274. }
  1275. if (interfaceHandle != NULL)
  1276. {
  1277. /* search interface and set the interface as detached */
  1278. for (interfaceIndex = 0; interfaceIndex < deviceInstance->configuration.interfaceCount; ++interfaceIndex)
  1279. {
  1280. if (&deviceInstance->configuration.interfaceList[interfaceIndex] == interfaceHandle)
  1281. {
  1282. deviceInstance->interfaceStatus[interfaceIndex] = (uint8_t)kStatus_interface_Detached;
  1283. break;
  1284. }
  1285. }
  1286. }
  1287. if (deviceInstance->deviceAttachState == (uint8_t)kStatus_device_Detached) /* device is removed from host */
  1288. {
  1289. removeLabel = 1;
  1290. /* check all the interfaces of the device are not opened */
  1291. for (interfaceIndex = 0; interfaceIndex < deviceInstance->configuration.interfaceCount; ++interfaceIndex)
  1292. {
  1293. if (deviceInstance->interfaceStatus[interfaceIndex] == (uint8_t)kStatus_interface_Opened)
  1294. {
  1295. removeLabel = 0U;
  1296. break;
  1297. }
  1298. }
  1299. if (removeLabel == 1U)
  1300. {
  1301. /* remove device instance from host */
  1302. (void)USB_HostRemoveDeviceInstance(hostInstance, deviceInstance);
  1303. }
  1304. (void)USB_HostUnlock();
  1305. if (removeLabel == 1U)
  1306. {
  1307. USB_HostReleaseDeviceResource((usb_host_instance_t *)deviceInstance->hostHandle,
  1308. deviceInstance); /* release device resource */
  1309. }
  1310. }
  1311. else
  1312. {
  1313. (void)USB_HostUnlock();
  1314. }
  1315. return kStatus_USB_Success;
  1316. }
  1317. usb_status_t USB_HostRemoveDevice(usb_host_handle hostHandle, usb_device_handle deviceHandle)
  1318. {
  1319. usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
  1320. usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)deviceHandle;
  1321. uint8_t interfaceIndex = 0;
  1322. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  1323. uint8_t level = 0;
  1324. uint8_t devHubNo;
  1325. uint8_t devPortNo;
  1326. #endif
  1327. if ((hostHandle == NULL) || (deviceHandle == NULL))
  1328. {
  1329. return kStatus_USB_InvalidHandle;
  1330. }
  1331. if (deviceInstance->hostHandle != hostHandle)
  1332. {
  1333. return kStatus_USB_InvalidParameter;
  1334. }
  1335. if (USB_HostValidateDevice(hostInstance, deviceInstance) == kStatus_USB_Success) /* device is valid */
  1336. {
  1337. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  1338. devHubNo = deviceInstance->hubNumber;
  1339. devPortNo = deviceInstance->portNumber;
  1340. level = deviceInstance->level;
  1341. #endif
  1342. deviceInstance->deviceAttachState = (uint8_t)kStatus_device_Detached;
  1343. if (deviceInstance->state >= (uint8_t)kStatus_DEV_Initial) /* device is valid */
  1344. {
  1345. if (deviceInstance->state <
  1346. (uint8_t)kStatus_DEV_AppUsed) /* enumeration is not done or application don't use */
  1347. {
  1348. /* detach internally */
  1349. (void)USB_HostDetachDeviceInternal(hostHandle, deviceHandle);
  1350. }
  1351. else /* application use the device */
  1352. {
  1353. for (interfaceIndex = 0U; interfaceIndex < deviceInstance->configuration.interfaceCount;
  1354. ++interfaceIndex)
  1355. {
  1356. if (deviceInstance->interfaceStatus[interfaceIndex] == (uint8_t)kStatus_interface_Opened)
  1357. {
  1358. #ifdef HOST_ECHO
  1359. usb_echo("error: there is class instance that is not deinited\r\n");
  1360. #endif
  1361. break;
  1362. }
  1363. }
  1364. /* remove device instance from host */
  1365. (void)USB_HostRemoveDeviceInstance(hostInstance, deviceInstance);
  1366. USB_HostReleaseDeviceResource(hostInstance, deviceInstance); /* release resource */
  1367. }
  1368. }
  1369. #if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
  1370. if (level == 1U)
  1371. {
  1372. (void)USB_HostControlBus(hostHandle, (uint8_t)kUSB_HostBusReset); /* reset controller port */
  1373. (void)USB_HostControlBus(hostHandle, (uint8_t)kUSB_HostBusRestart); /* restart controller port */
  1374. }
  1375. else
  1376. {
  1377. (void)USB_HostHubRemovePort(hostHandle, devHubNo, devPortNo); /* reset hub port */
  1378. }
  1379. #else
  1380. USB_HostControlBus(hostHandle, kUSB_HostBusReset); /* reset controller port */
  1381. USB_HostControlBus(hostHandle, kUSB_HostBusRestart); /* restart controller port */
  1382. #endif /* USB_HOST_CONFIG_HUB */
  1383. }
  1384. return kStatus_USB_Success;
  1385. }