usb_host_devices.c 53 KB

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