2019-12-26 14:10:06 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
|
2021-02-23 09:44:27 +08:00
|
|
|
* Copyright 2016 NXP
|
2019-12-26 14:10:06 +08:00
|
|
|
*
|
2021-02-23 09:44:27 +08:00
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* o Redistributions of source code must retain the above copyright notice, this list
|
|
|
|
* of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* o Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
* other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* o Neither the name of the copyright holder nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from this
|
|
|
|
* software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2019-12-26 14:10:06 +08:00
|
|
|
*/
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
#include <usb/include/usb_device_config.h>
|
2019-12-26 14:10:06 +08:00
|
|
|
#include "usb_host.h"
|
|
|
|
#include "usb_host_hci.h"
|
|
|
|
#include "usb_host_devices.h"
|
2021-02-23 09:44:27 +08:00
|
|
|
|
2019-12-26 14:10:06 +08:00
|
|
|
#if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
|
|
|
|
#include "usb_host_hub.h"
|
|
|
|
#endif /* USB_HOST_CONFIG_HUB */
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Definitions
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Prototypes
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* @brief enumeration transfer callback function.
|
|
|
|
*
|
|
|
|
* @param param callback parameter.
|
|
|
|
* @param transfer the transfer.
|
|
|
|
* @param status transfer result status.
|
|
|
|
*/
|
|
|
|
static void USB_HostEnumerationTransferCallback(void *param, usb_host_transfer_t *transfer, usb_status_t status);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* @brief process the new step state.
|
|
|
|
*
|
|
|
|
* @param deviceInstance device instance pointer.
|
|
|
|
*
|
|
|
|
* @return kStatus_USB_Success or error codes
|
|
|
|
*/
|
|
|
|
static usb_status_t USB_HostProcessState(usb_host_device_instance_t *deviceInstance);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* @brief process the previous step transfer result.
|
|
|
|
*
|
|
|
|
* @param deviceInstance device instance pointer.
|
|
|
|
*
|
|
|
|
* @return kStatus_USB_Success or error codes
|
|
|
|
*/
|
2021-02-23 09:44:27 +08:00
|
|
|
static usb_status_t USB_HostProcessCallback(usb_host_device_instance_t *deviceInstance);
|
2019-12-26 14:10:06 +08:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* @brief notify the application event, the callback is registered when initializing host.
|
|
|
|
*
|
|
|
|
* @param deviceInstance device instance pointer.
|
|
|
|
* @param eventCode event code.
|
|
|
|
*
|
|
|
|
* @return kStatus_USB_Success or error codes
|
|
|
|
*/
|
2021-02-23 09:44:27 +08:00
|
|
|
static usb_status_t USB_HostNotifyDevice(usb_host_device_instance_t *deviceInstance, uint32_t eventCode);
|
2019-12-26 14:10:06 +08:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* @brief allocate one address.
|
|
|
|
*
|
|
|
|
* @param hostInstance host instance pointer.
|
|
|
|
*
|
|
|
|
* @return address, 0 is invalid.
|
|
|
|
*/
|
|
|
|
static uint8_t USB_HostAllocateDeviceAddress(usb_host_instance_t *hostInstance);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* @brief release one address.
|
|
|
|
*
|
|
|
|
* @param hostInstance host instance pointer.
|
|
|
|
* @param address releasing address.
|
|
|
|
*/
|
|
|
|
static void USB_HostReleaseDeviceAddress(usb_host_instance_t *hostInstance, uint8_t address);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* @brief release device resource.
|
|
|
|
*
|
|
|
|
* @param hostInstance host instance pointer.
|
|
|
|
* @param deviceInstance device instance pointer.
|
|
|
|
*/
|
|
|
|
static void USB_HostReleaseDeviceResource(usb_host_instance_t *hostInstance,
|
|
|
|
usb_host_device_instance_t *deviceInstance);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* @brief parse device configuration descriptor.
|
|
|
|
*
|
|
|
|
* @param deviceHandle device handle.
|
|
|
|
*
|
|
|
|
* @return kStatus_USB_Success or error codes.
|
|
|
|
*/
|
|
|
|
static usb_status_t USB_HostParseDeviceConfigurationDescriptor(usb_device_handle deviceHandle);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* @brief remove device instance from host device list.
|
|
|
|
*
|
|
|
|
* @param hostHandle host instance handle.
|
|
|
|
* @param deviceHandle device handle.
|
|
|
|
*
|
|
|
|
* @return kStatus_USB_Success or error codes.
|
|
|
|
*/
|
|
|
|
static usb_status_t USB_HostRemoveDeviceInstance(usb_host_handle hostHandle, usb_device_handle deviceHandle);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* @brief control the bus.
|
|
|
|
*
|
|
|
|
* This function control the host bus.
|
|
|
|
*
|
|
|
|
* @param[in] hostHandle the host handle.
|
|
|
|
* @param[in] controlType the control code, please reference to bus_event_t.
|
|
|
|
*
|
|
|
|
* @retval kStatus_USB_Success control successfully.
|
|
|
|
* @retval kStatus_USB_InvalidHandle The hostHandle is a NULL pointer.
|
|
|
|
*/
|
|
|
|
static usb_status_t USB_HostControlBus(usb_host_handle hostHandle, uint8_t controlType);
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
extern usb_status_t USB_HostStandardSetGetDescriptor(usb_host_device_instance_t *deviceInstance,
|
|
|
|
usb_host_transfer_t *transfer,
|
|
|
|
void *param);
|
|
|
|
extern usb_status_t USB_HostStandardSetAddress(usb_host_device_instance_t *deviceInstance,
|
|
|
|
usb_host_transfer_t *transfer,
|
|
|
|
void *param);
|
|
|
|
extern usb_status_t USB_HostCh9RequestCommon(usb_host_device_instance_t *deviceInstance,
|
|
|
|
usb_host_transfer_t *transfer,
|
|
|
|
uint8_t *buffer,
|
|
|
|
uint32_t bufferLen);
|
|
|
|
|
2019-12-26 14:10:06 +08:00
|
|
|
#if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
extern usb_status_t USB_HostHubDeviceEvent(usb_host_handle hostHandle,
|
|
|
|
usb_device_handle deviceHandle,
|
|
|
|
usb_host_configuration_handle configurationHandle,
|
|
|
|
uint32_t eventCode);
|
|
|
|
|
|
|
|
extern uint32_t USB_HostHubGetHsHubNumber(usb_host_handle hostHandle, uint8_t parentHubNo);
|
|
|
|
|
|
|
|
extern uint32_t USB_HostHubGetHsHubPort(usb_host_handle hostHandle, uint8_t parentHubNo, uint8_t parentPortNo);
|
|
|
|
|
|
|
|
extern usb_status_t USB_HostHubRemovePort(usb_host_handle hostHandle, uint8_t hubNumber, uint8_t portNumber);
|
|
|
|
|
2019-12-26 14:10:06 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Variables
|
|
|
|
******************************************************************************/
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
extern usb_host_instance_t g_UsbHostInstance[USB_HOST_CONFIG_MAX_HOST];
|
|
|
|
|
2019-12-26 14:10:06 +08:00
|
|
|
/*! @brief enumeration step process array */
|
2021-02-23 09:44:27 +08:00
|
|
|
static const usb_host_enum_process_entry_t s_EnumEntries[] = \
|
|
|
|
{
|
2019-12-26 14:10:06 +08:00
|
|
|
/* kStatus_dev_initial */
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
0, 0, NULL,
|
2019-12-26 14:10:06 +08:00
|
|
|
},
|
|
|
|
/* kStatus_DEV_GetDes8 */
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
kStatus_DEV_SetAddress, kStatus_DEV_GetDes8, USB_HostProcessCallback,
|
2019-12-26 14:10:06 +08:00
|
|
|
},
|
|
|
|
/* kStatus_DEV_SetAddress */
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
kStatus_DEV_GetDes, kStatus_DEV_SetAddress, USB_HostProcessCallback,
|
2019-12-26 14:10:06 +08:00
|
|
|
},
|
|
|
|
/* kStatus_DEV_GetDes */
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
kStatus_DEV_GetCfg9, kStatus_DEV_GetDes, NULL,
|
2019-12-26 14:10:06 +08:00
|
|
|
},
|
|
|
|
/* kStatus_DEV_GetCfg9 */
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
kStatus_DEV_GetCfg, kStatus_DEV_GetCfg9, USB_HostProcessCallback,
|
2019-12-26 14:10:06 +08:00
|
|
|
},
|
|
|
|
/* kStatus_DEV_GetCfg */
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
kStatus_DEV_SetCfg, kStatus_DEV_GetCfg9, USB_HostProcessCallback,
|
2019-12-26 14:10:06 +08:00
|
|
|
},
|
|
|
|
/* kStatus_DEV_SetCfg */
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
kStatus_DEV_EnumDone, kStatus_DEV_SetCfg, NULL,
|
2019-12-26 14:10:06 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Code
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
static void USB_HostEnumerationTransferCallback(void *param, usb_host_transfer_t *transfer, usb_status_t status)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
uint8_t nextStep = 0;
|
2019-12-26 14:10:06 +08:00
|
|
|
usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)param;
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostFreeTransfer(deviceInstance->hostHandle, transfer); /* free transfer */
|
2019-12-26 14:10:06 +08:00
|
|
|
|
|
|
|
if (status == kStatus_USB_Success)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
nextStep = 1;
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
else if (status == kStatus_USB_TransferStall)
|
|
|
|
{
|
|
|
|
#if ((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
|
2021-02-23 09:44:27 +08:00
|
|
|
usb_echo("no response from device\r\n");
|
|
|
|
#endif /* USB_HOST_CONFIG_COMPLIANCE_TEST */
|
|
|
|
if (deviceInstance->stallRetries > 0) /* retry same transfer when stall */
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
deviceInstance->stallRetries--;
|
|
|
|
}
|
2021-02-23 09:44:27 +08:00
|
|
|
else /* process next state when all retries stall */
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
nextStep = 1;
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (status == kStatus_USB_TransferCancel)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
return;
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
if (deviceInstance->enumRetries > 0) /* next whole retry */
|
|
|
|
{
|
|
|
|
deviceInstance->enumRetries--;
|
|
|
|
deviceInstance->stallRetries = USB_HOST_CONFIG_ENUMERATION_MAX_STALL_RETRIES;
|
|
|
|
deviceInstance->configurationValue = 0;
|
|
|
|
deviceInstance->state = kStatus_DEV_GetDes8;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if ((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
|
|
|
|
usb_echo("Device No Response\r\n");
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
if (nextStep == 1)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
deviceInstance->stallRetries = USB_HOST_CONFIG_ENUMERATION_MAX_STALL_RETRIES;
|
2021-02-23 09:44:27 +08:00
|
|
|
if (s_EnumEntries[deviceInstance->state - 1].process == NULL)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
deviceInstance->state = s_EnumEntries[deviceInstance->state - 1].successState; /* next state */
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
status = s_EnumEntries[deviceInstance->state - 1].process(
|
|
|
|
deviceInstance); /* process the previous state result */
|
2019-12-26 14:10:06 +08:00
|
|
|
if (status == kStatus_USB_Success) /* process success */
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
deviceInstance->state = s_EnumEntries[deviceInstance->state - 1].successState;
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
else if (status == kStatus_USB_Retry) /* need retry */
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
deviceInstance->state = s_EnumEntries[deviceInstance->state - 1].retryState;
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
else if (status == kStatus_USB_NotSupported) /* device don't suport by the application */
|
|
|
|
{
|
|
|
|
return; /* unrecoverable fail */
|
|
|
|
}
|
|
|
|
else /* process error, next retry */
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
if (deviceInstance->enumRetries > 0) /* next whole retry */
|
|
|
|
{
|
|
|
|
deviceInstance->enumRetries--;
|
|
|
|
deviceInstance->stallRetries = USB_HOST_CONFIG_ENUMERATION_MAX_STALL_RETRIES;
|
|
|
|
deviceInstance->configurationValue = 0;
|
|
|
|
deviceInstance->state = kStatus_DEV_GetDes8;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-26 14:10:06 +08:00
|
|
|
#if ((defined USB_HOST_CONFIG_COMPLIANCE_TEST) && (USB_HOST_CONFIG_COMPLIANCE_TEST))
|
2021-02-23 09:44:27 +08:00
|
|
|
usb_echo("Device No Response\r\n");
|
2019-12-26 14:10:06 +08:00
|
|
|
#endif
|
2021-02-23 09:44:27 +08:00
|
|
|
return; /* unrecoverable fail */
|
|
|
|
}
|
|
|
|
}
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
if (USB_HostProcessState(deviceInstance) != kStatus_USB_Success) /* process the new state */
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
#ifdef HOST_ECHO
|
2021-02-23 09:44:27 +08:00
|
|
|
usb_echo("enumation setup error\r\n");
|
2019-12-26 14:10:06 +08:00
|
|
|
#endif
|
2021-02-23 09:44:27 +08:00
|
|
|
return;
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static usb_status_t USB_HostProcessState(usb_host_device_instance_t *deviceInstance)
|
|
|
|
{
|
|
|
|
usb_status_t status = kStatus_USB_Success;
|
|
|
|
usb_host_process_descriptor_param_t getDescriptorParam;
|
|
|
|
usb_host_transfer_t *transfer;
|
|
|
|
|
|
|
|
/* malloc transfer */
|
2021-02-23 09:44:27 +08:00
|
|
|
if (deviceInstance->state != kStatus_DEV_EnumDone)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
if (USB_HostMallocTransfer(deviceInstance->hostHandle, &transfer) != kStatus_USB_Success)
|
|
|
|
{
|
|
|
|
#ifdef HOST_ECHO
|
|
|
|
usb_echo("error to get transfer\r\n");
|
|
|
|
#endif
|
|
|
|
return kStatus_USB_Error;
|
|
|
|
}
|
2021-02-23 09:44:27 +08:00
|
|
|
transfer->callbackFn = USB_HostEnumerationTransferCallback;
|
2019-12-26 14:10:06 +08:00
|
|
|
transfer->callbackParam = deviceInstance;
|
|
|
|
|
|
|
|
/* reset transfer fields */
|
2021-02-23 09:44:27 +08:00
|
|
|
transfer->setupPacket->bmRequestType = 0x00;
|
|
|
|
transfer->setupPacket->wIndex = 0;
|
|
|
|
transfer->setupPacket->wLength = 0;
|
|
|
|
transfer->setupPacket->wValue = 0;
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
switch (deviceInstance->state)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
case kStatus_DEV_GetDes8:
|
|
|
|
case kStatus_DEV_GetDes: /* get descriptor state */
|
|
|
|
getDescriptorParam.descriptorLength = sizeof(usb_descriptor_device_t);
|
2021-02-23 09:44:27 +08:00
|
|
|
if (deviceInstance->state == kStatus_DEV_GetDes8)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
getDescriptorParam.descriptorLength = 8;
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
getDescriptorParam.descriptorBuffer = (uint8_t *)deviceInstance->deviceDescriptor;
|
2021-02-23 09:44:27 +08:00
|
|
|
getDescriptorParam.descriptorType = USB_DESCRIPTOR_TYPE_DEVICE;
|
|
|
|
getDescriptorParam.descriptorIndex = 0;
|
|
|
|
getDescriptorParam.languageId = 0;
|
2019-12-26 14:10:06 +08:00
|
|
|
|
|
|
|
transfer->setupPacket->bmRequestType |= USB_REQUEST_TYPE_DIR_IN;
|
|
|
|
transfer->setupPacket->bRequest = USB_REQUEST_STANDARD_GET_DESCRIPTOR;
|
|
|
|
status = USB_HostStandardSetGetDescriptor(deviceInstance, transfer, &getDescriptorParam);
|
|
|
|
break;
|
|
|
|
case kStatus_DEV_SetAddress: /* set address state */
|
|
|
|
transfer->setupPacket->bRequest = USB_REQUEST_STANDARD_SET_ADDRESS;
|
|
|
|
status = USB_HostStandardSetAddress(deviceInstance, transfer, &deviceInstance->allocatedAddress);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kStatus_DEV_GetCfg9: /* get 9 bytes configuration state */
|
|
|
|
getDescriptorParam.descriptorBuffer = deviceInstance->enumBuffer;
|
2021-02-23 09:44:27 +08:00
|
|
|
getDescriptorParam.descriptorType = USB_DESCRIPTOR_TYPE_CONFIGURE;
|
|
|
|
getDescriptorParam.descriptorIndex = deviceInstance->configurationValue;
|
2019-12-26 14:10:06 +08:00
|
|
|
getDescriptorParam.descriptorLength = 9;
|
2021-02-23 09:44:27 +08:00
|
|
|
getDescriptorParam.languageId = 0;
|
2019-12-26 14:10:06 +08:00
|
|
|
|
|
|
|
transfer->setupPacket->bmRequestType |= USB_REQUEST_TYPE_DIR_IN;
|
|
|
|
transfer->setupPacket->bRequest = USB_REQUEST_STANDARD_GET_DESCRIPTOR;
|
|
|
|
status = USB_HostStandardSetGetDescriptor(deviceInstance, transfer, &getDescriptorParam);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kStatus_DEV_GetCfg: /* get configuration state */
|
|
|
|
getDescriptorParam.descriptorBuffer = deviceInstance->configurationDesc;
|
2021-02-23 09:44:27 +08:00
|
|
|
getDescriptorParam.descriptorType = USB_DESCRIPTOR_TYPE_CONFIGURE;
|
|
|
|
getDescriptorParam.descriptorIndex = deviceInstance->configurationValue;
|
2019-12-26 14:10:06 +08:00
|
|
|
getDescriptorParam.descriptorLength = deviceInstance->configurationLen;
|
2021-02-23 09:44:27 +08:00
|
|
|
getDescriptorParam.languageId = 0;
|
2019-12-26 14:10:06 +08:00
|
|
|
|
|
|
|
transfer->setupPacket->bmRequestType |= USB_REQUEST_TYPE_DIR_IN;
|
|
|
|
transfer->setupPacket->bRequest = USB_REQUEST_STANDARD_GET_DESCRIPTOR;
|
|
|
|
status = USB_HostStandardSetGetDescriptor(deviceInstance, transfer, &getDescriptorParam);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kStatus_DEV_SetCfg: /* set configuration state */
|
|
|
|
transfer->setupPacket->wValue =
|
|
|
|
USB_SHORT_TO_LITTLE_ENDIAN(deviceInstance->configuration.configurationDesc->bConfigurationValue);
|
|
|
|
transfer->setupPacket->bRequest = USB_REQUEST_STANDARD_SET_CONFIGURATION;
|
2021-02-23 09:44:27 +08:00
|
|
|
status = USB_HostCh9RequestCommon(deviceInstance, transfer, NULL, 0);
|
2019-12-26 14:10:06 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case kStatus_DEV_EnumDone: /* enumeration done state */
|
2021-02-23 09:44:27 +08:00
|
|
|
status = USB_HostNotifyDevice(deviceInstance,
|
|
|
|
kUSB_HostEventEnumerationDone); /* notify device enumeration done */
|
2019-12-26 14:10:06 +08:00
|
|
|
if (status == kStatus_USB_Success)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
deviceInstance->state = kStatus_DEV_AppUsed;
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
static usb_status_t USB_HostProcessCallback(usb_host_device_instance_t *deviceInstance)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
usb_host_pipe_t *pipe = (usb_host_pipe_t *)deviceInstance->controlPipe;
|
2021-02-23 09:44:27 +08:00
|
|
|
usb_status_t status = kStatus_USB_Success;
|
2019-12-26 14:10:06 +08:00
|
|
|
usb_descriptor_configuration_t *configureDesc;
|
|
|
|
usb_host_instance_t *hostInstance = (usb_host_instance_t *)deviceInstance->hostHandle;
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
switch (deviceInstance->state)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
case kStatus_DEV_GetDes8: /* process get 8 bytes descriptor result */
|
|
|
|
pipe->maxPacketSize = deviceInstance->deviceDescriptor->bMaxPacketSize0;
|
2021-02-23 09:44:27 +08:00
|
|
|
hostInstance->controllerTable->controllerIoctl(
|
2019-12-26 14:10:06 +08:00
|
|
|
hostInstance->controllerHandle, kUSB_HostUpdateControlPacketSize, deviceInstance->controlPipe);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kStatus_DEV_SetAddress: /* process set address result */
|
|
|
|
deviceInstance->setAddress = deviceInstance->allocatedAddress;
|
2021-02-23 09:44:27 +08:00
|
|
|
hostInstance->controllerTable->controllerIoctl(
|
2019-12-26 14:10:06 +08:00
|
|
|
hostInstance->controllerHandle, kUSB_HostUpdateControlEndpointAddress, deviceInstance->controlPipe);
|
|
|
|
break;
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
case kStatus_DEV_GetDes: /* process set address result */
|
|
|
|
/* NULL */
|
2019-12-26 14:10:06 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case kStatus_DEV_GetCfg9: /* process get 9 bytes configuration result */
|
2021-02-23 09:44:27 +08:00
|
|
|
configureDesc = (usb_descriptor_configuration_t *)&deviceInstance->enumBuffer[0];
|
2019-12-26 14:10:06 +08:00
|
|
|
|
|
|
|
deviceInstance->configurationLen = USB_SHORT_FROM_LITTLE_ENDIAN_ADDRESS(configureDesc->wTotalLength);
|
|
|
|
if (deviceInstance->configurationDesc != NULL)
|
|
|
|
{
|
|
|
|
#if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
|
|
|
|
SDK_Free(deviceInstance->configurationDesc);
|
|
|
|
#else
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_OsaMemoryFree(deviceInstance->configurationDesc);
|
2019-12-26 14:10:06 +08:00
|
|
|
#endif
|
|
|
|
deviceInstance->configurationDesc = NULL;
|
|
|
|
}
|
|
|
|
/* for KHCI, the start address and the length should be 4 byte align */
|
2021-02-23 09:44:27 +08:00
|
|
|
if ((deviceInstance->configurationLen & 0x03) != 0)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
#if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
|
|
|
|
deviceInstance->configurationDesc =
|
2021-02-23 09:44:27 +08:00
|
|
|
(uint8_t *)SDK_Malloc((deviceInstance->configurationLen & 0xFFFFFFFCu) + 4, USB_CACHE_LINESIZE);
|
2019-12-26 14:10:06 +08:00
|
|
|
#else
|
|
|
|
deviceInstance->configurationDesc =
|
2021-02-23 09:44:27 +08:00
|
|
|
(uint8_t *)USB_OsaMemoryAllocate((deviceInstance->configurationLen & 0xFFFFFFFCu) + 4);
|
2019-12-26 14:10:06 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
|
|
|
|
deviceInstance->configurationDesc =
|
|
|
|
(uint8_t *)SDK_Malloc(deviceInstance->configurationLen, USB_CACHE_LINESIZE);
|
|
|
|
#else
|
2021-02-23 09:44:27 +08:00
|
|
|
deviceInstance->configurationDesc = (uint8_t *)USB_OsaMemoryAllocate(deviceInstance->configurationLen);
|
2019-12-26 14:10:06 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (deviceInstance->configurationDesc == NULL)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
return kStatus_USB_Error;
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
case kStatus_DEV_GetCfg: /* process get cofiguration result */
|
|
|
|
if (((usb_descriptor_configuration_t *)deviceInstance->configurationDesc)->bMaxPower >
|
|
|
|
USB_HOST_CONFIG_MAX_POWER)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
return kStatus_USB_Error;
|
|
|
|
}
|
|
|
|
deviceInstance->configurationValue++;
|
|
|
|
if (USB_HostParseDeviceConfigurationDescriptor(deviceInstance) !=
|
|
|
|
kStatus_USB_Success) /* parse configuration descriptor */
|
|
|
|
{
|
|
|
|
return kStatus_USB_Error;
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
status = USB_HostNotifyDevice(deviceInstance, kUSB_HostEventAttach);
|
2019-12-26 14:10:06 +08:00
|
|
|
|
|
|
|
if (status != kStatus_USB_Success)
|
|
|
|
{
|
|
|
|
/* next configuration */
|
|
|
|
if (deviceInstance->configurationValue < deviceInstance->deviceDescriptor->bNumConfigurations)
|
|
|
|
{
|
|
|
|
return kStatus_USB_Retry;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostNotifyDevice(deviceInstance,
|
|
|
|
kUSB_HostEventNotSupported); /* notify application device is not supported */
|
2019-12-26 14:10:06 +08:00
|
|
|
return kStatus_USB_NotSupported;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kStatus_DEV_SetCfg:
|
|
|
|
/* NULL */
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
static usb_status_t USB_HostNotifyDevice(usb_host_device_instance_t *deviceInstance, uint32_t eventCode)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
usb_host_instance_t *hostInstance;
|
|
|
|
usb_status_t status1 = kStatus_USB_Error;
|
|
|
|
#if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
|
|
|
|
usb_status_t status2 = kStatus_USB_Error;
|
|
|
|
uint8_t haveHub;
|
|
|
|
uint8_t haveNoHub;
|
|
|
|
uint8_t interfaceIndex;
|
|
|
|
#endif /* USB_HOST_CONFIG_HUB */
|
|
|
|
|
|
|
|
if (deviceInstance == NULL)
|
|
|
|
{
|
|
|
|
return kStatus_USB_InvalidHandle;
|
|
|
|
}
|
2021-02-23 09:44:27 +08:00
|
|
|
hostInstance = (usb_host_instance_t *)deviceInstance->hostHandle;
|
2019-12-26 14:10:06 +08:00
|
|
|
|
|
|
|
#if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
|
2021-02-23 09:44:27 +08:00
|
|
|
haveHub = 0;
|
|
|
|
haveNoHub = 0;
|
|
|
|
for (interfaceIndex = 0; interfaceIndex < deviceInstance->configuration.interfaceCount; ++interfaceIndex)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
if (((usb_descriptor_interface_t *)deviceInstance->configuration.interfaceList[interfaceIndex].interfaceDesc)
|
|
|
|
->bInterfaceClass == USB_HOST_HUB_CLASS_CODE)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
haveHub = 1;
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
haveNoHub = 1;
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
if ((haveNoHub == 1) && (hostInstance->deviceCallback != NULL))
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
status1 = hostInstance->deviceCallback(deviceInstance, &deviceInstance->configuration,
|
|
|
|
eventCode); /* notify application event */
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
2021-02-23 09:44:27 +08:00
|
|
|
if (haveHub)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
status2 = USB_HostHubDeviceEvent(hostInstance, deviceInstance, &deviceInstance->configuration,
|
|
|
|
eventCode); /* notify hub event */
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((status1 == kStatus_USB_Success) || (status2 == kStatus_USB_Success)) /* the device is supported */
|
|
|
|
{
|
|
|
|
return kStatus_USB_Success;
|
|
|
|
}
|
2021-02-23 09:44:27 +08:00
|
|
|
else if (eventCode == kUSB_HostEventAttach) /* attach event */
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
status1 = kStatus_USB_NotSupported;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status1 = kStatus_USB_Error;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (hostInstance->deviceCallback != NULL)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
status1 = hostInstance->deviceCallback(deviceInstance, &deviceInstance->configuration,
|
|
|
|
eventCode); /* call host callback function */
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return status1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t USB_HostAllocateDeviceAddress(usb_host_instance_t *hostInstance)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
uint8_t address = 0;
|
2019-12-26 14:10:06 +08:00
|
|
|
uint8_t addressIndex;
|
|
|
|
uint8_t addressBitIndex;
|
2021-02-23 09:44:27 +08:00
|
|
|
for (addressIndex = 0; addressIndex < 8; ++addressIndex) /* find the idle address postion byte */
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
if (hostInstance->addressBitMap[addressIndex] != 0xFF)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-02-23 09:44:27 +08:00
|
|
|
if (addressIndex < 8)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
for (addressBitIndex = 0; addressBitIndex < 8; ++addressBitIndex) /* find the idle address position bit */
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
if (!(hostInstance->addressBitMap[addressIndex] & (0x01u << addressBitIndex)))
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
hostInstance->addressBitMap[addressIndex] |= (0x01u << addressBitIndex); /* set the allocated bit */
|
|
|
|
address = addressIndex * 8 + addressBitIndex + 1; /* the address minimum is 1 */
|
2019-12-26 14:10:06 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void USB_HostReleaseDeviceAddress(usb_host_instance_t *hostInstance, uint8_t address)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostLock();
|
|
|
|
hostInstance->addressBitMap[(uint32_t)(address - 1) >> 3] &=
|
|
|
|
(~(0x01u << (((uint32_t)address - 1) & 0x07U))); /* reset the allocated bit */
|
|
|
|
USB_HostUnlock();
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static usb_status_t USB_HostRemoveDeviceInstance(usb_host_handle hostHandle, usb_device_handle deviceHandle)
|
|
|
|
{
|
|
|
|
usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
|
|
|
|
usb_host_device_instance_t *currentInstance;
|
|
|
|
usb_host_device_instance_t *prevInstance;
|
|
|
|
if ((hostHandle == NULL) || (deviceHandle == NULL))
|
|
|
|
{
|
|
|
|
return kStatus_USB_InvalidHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* search and remove device instance */
|
|
|
|
prevInstance = (usb_host_device_instance_t *)hostInstance->deviceList;
|
|
|
|
if (prevInstance == deviceHandle)
|
|
|
|
{
|
|
|
|
hostInstance->deviceList = prevInstance->next;
|
|
|
|
return kStatus_USB_Success;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
currentInstance = prevInstance->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (currentInstance != NULL)
|
|
|
|
{
|
|
|
|
if (currentInstance == deviceHandle)
|
|
|
|
{
|
|
|
|
prevInstance->next = currentInstance->next;
|
|
|
|
return kStatus_USB_Success;
|
|
|
|
}
|
2021-02-23 09:44:27 +08:00
|
|
|
prevInstance = currentInstance;
|
2019-12-26 14:10:06 +08:00
|
|
|
currentInstance = currentInstance->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return kStatus_USB_Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void USB_HostReleaseDeviceResource(usb_host_instance_t *hostInstance, usb_host_device_instance_t *deviceInstance)
|
|
|
|
{
|
|
|
|
#if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
|
|
|
|
uint8_t level = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ((defined(USB_HOST_CONFIG_LOW_POWER_MODE)) && (USB_HOST_CONFIG_LOW_POWER_MODE > 0U))
|
|
|
|
if (deviceInstance == hostInstance->suspendedDevice)
|
|
|
|
{
|
|
|
|
hostInstance->suspendedDevice = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* release device's address */
|
2021-02-23 09:44:27 +08:00
|
|
|
if (deviceInstance->setAddress != 0)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
USB_HostReleaseDeviceAddress(hostInstance, deviceInstance->setAddress);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
if (deviceInstance->allocatedAddress != 0)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
USB_HostReleaseDeviceAddress(hostInstance, deviceInstance->allocatedAddress);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* close control pipe */
|
|
|
|
if (deviceInstance->controlPipe != NULL)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostCancelTransfer(hostInstance, deviceInstance->controlPipe, NULL);
|
2019-12-26 14:10:06 +08:00
|
|
|
if (USB_HostClosePipe(hostInstance, deviceInstance->controlPipe) != kStatus_USB_Success)
|
|
|
|
{
|
|
|
|
#ifdef HOST_ECHO
|
|
|
|
usb_echo("error when close pipe\r\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
deviceInstance->controlPipe = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free configuration buffer */
|
|
|
|
if (deviceInstance->configurationDesc != NULL)
|
|
|
|
{
|
|
|
|
#if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
|
|
|
|
SDK_Free(deviceInstance->configurationDesc);
|
|
|
|
#else
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_OsaMemoryFree(deviceInstance->configurationDesc);
|
2019-12-26 14:10:06 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
|
|
|
|
level = deviceInstance->level;
|
|
|
|
#endif
|
|
|
|
#if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
|
|
|
|
SDK_Free(deviceInstance->deviceDescriptor);
|
|
|
|
#else
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_OsaMemoryFree(deviceInstance->deviceDescriptor);
|
2019-12-26 14:10:06 +08:00
|
|
|
#endif
|
|
|
|
/* free device instance buffer */
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_OsaMemoryFree(deviceInstance);
|
2019-12-26 14:10:06 +08:00
|
|
|
|
|
|
|
#if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
|
|
|
|
/* enable controller attach if root hub */
|
2021-02-23 09:44:27 +08:00
|
|
|
if (level == 1)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostControlBus(hostInstance, kUSB_HostBusEnableAttach);
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* enable controller attach */
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostControlBus(hostInstance, kUSB_HostBusEnableAttach);
|
2019-12-26 14:10:06 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static usb_status_t USB_HostParseDeviceConfigurationDescriptor(usb_device_handle deviceHandle)
|
|
|
|
{
|
|
|
|
usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)deviceHandle;
|
|
|
|
uint32_t endPos;
|
|
|
|
usb_descriptor_union_t *unionDes;
|
|
|
|
usb_host_interface_t *interfaceParse = NULL;
|
|
|
|
usb_host_ep_t *epParse;
|
|
|
|
uint8_t *buffer;
|
|
|
|
|
|
|
|
if (deviceHandle == NULL)
|
|
|
|
{
|
|
|
|
return kStatus_USB_InvalidParameter;
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
buffer = (uint8_t *)&deviceInstance->configuration;
|
2019-12-26 14:10:06 +08:00
|
|
|
/* clear the previous parse result, note: end_pos means buffer index here*/
|
2021-02-23 09:44:27 +08:00
|
|
|
for (endPos = 0; endPos < sizeof(usb_host_configuration_t); endPos++)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
buffer[endPos] = 0;
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
2021-02-23 09:44:27 +08:00
|
|
|
for (endPos = 0; endPos < USB_HOST_CONFIG_CONFIGURATION_MAX_INTERFACE; ++endPos)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
deviceInstance->interfaceStatus[endPos] = 0;
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* parse configuration descriptor */
|
2021-02-23 09:44:27 +08:00
|
|
|
unionDes = (usb_descriptor_union_t *)deviceInstance->configurationDesc;
|
|
|
|
endPos = (uint32_t)(deviceInstance->configurationDesc + deviceInstance->configurationLen);
|
2019-12-26 14:10:06 +08:00
|
|
|
|
|
|
|
if ((unionDes->common.bLength == USB_DESCRIPTOR_LENGTH_CONFIGURE) &&
|
|
|
|
(unionDes->common.bDescriptorType == USB_DESCRIPTOR_TYPE_CONFIGURE))
|
|
|
|
{
|
|
|
|
/* configuration descriptor */
|
2021-02-23 09:44:27 +08:00
|
|
|
deviceInstance->configuration.configurationDesc = (usb_descriptor_configuration_t *)unionDes;
|
2019-12-26 14:10:06 +08:00
|
|
|
deviceInstance->configuration.configurationExtensionLength = 0;
|
2021-02-23 09:44:27 +08:00
|
|
|
deviceInstance->configuration.configurationExtension = NULL;
|
|
|
|
deviceInstance->configuration.interfaceCount = 0;
|
2019-12-26 14:10:06 +08:00
|
|
|
unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
|
|
|
|
while ((uint32_t)unionDes < endPos)
|
|
|
|
{
|
|
|
|
if (unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_INTERFACE)
|
|
|
|
{
|
|
|
|
if (deviceInstance->configuration.configurationExtension == NULL)
|
|
|
|
{
|
|
|
|
deviceInstance->configuration.configurationExtension = (uint8_t *)unionDes;
|
|
|
|
}
|
2021-02-23 09:44:27 +08:00
|
|
|
if ((unionDes->common.bDescriptorType == 0x00) ||
|
|
|
|
(unionDes->common.bLength == 0x00)) /* the descriptor data is wrong */
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
return kStatus_USB_Error;
|
|
|
|
}
|
|
|
|
deviceInstance->configuration.configurationExtensionLength += unionDes->common.bLength;
|
|
|
|
unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* interface descriptor */
|
2021-02-23 09:44:27 +08:00
|
|
|
deviceInstance->configuration.interfaceCount = 0;
|
2019-12-26 14:10:06 +08:00
|
|
|
while ((uint32_t)unionDes < endPos)
|
|
|
|
{
|
|
|
|
if (unionDes->common.bDescriptorType == USB_DESCRIPTOR_TYPE_INTERFACE)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
if (unionDes->interface.bAlternateSetting == 0x00)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
if (deviceInstance->configuration.interfaceCount >= USB_HOST_CONFIG_CONFIGURATION_MAX_INTERFACE)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
#ifdef HOST_ECHO
|
|
|
|
usb_echo(
|
2019-12-26 14:10:06 +08:00
|
|
|
"Unsupported Device attached\r\n too many interfaces in one configuration, please increase "
|
|
|
|
"the USB_HOST_CONFIG_CONFIGURATION_MAX_INTERFACE value\n");
|
|
|
|
#endif
|
|
|
|
return kStatus_USB_Error;
|
|
|
|
}
|
|
|
|
interfaceParse =
|
|
|
|
&deviceInstance->configuration.interfaceList[deviceInstance->configuration.interfaceCount];
|
|
|
|
deviceInstance->configuration.interfaceCount++;
|
2021-02-23 09:44:27 +08:00
|
|
|
interfaceParse->alternateSettingNumber = 0;
|
|
|
|
interfaceParse->epCount = 0;
|
|
|
|
interfaceParse->interfaceDesc = &unionDes->interface;
|
2019-12-26 14:10:06 +08:00
|
|
|
interfaceParse->interfaceExtensionLength = 0;
|
2021-02-23 09:44:27 +08:00
|
|
|
interfaceParse->interfaceExtension = NULL;
|
|
|
|
interfaceParse->interfaceIndex = unionDes->interface.bInterfaceNumber;
|
|
|
|
if (unionDes->common.bLength == 0x00) /* the descriptor data is wrong */
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
return kStatus_USB_Error;
|
|
|
|
}
|
|
|
|
unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
|
|
|
|
while ((uint32_t)unionDes < endPos)
|
|
|
|
{
|
|
|
|
if ((unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_INTERFACE) &&
|
|
|
|
(unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_ENDPOINT))
|
|
|
|
{
|
|
|
|
if (interfaceParse->interfaceExtension == NULL)
|
|
|
|
{
|
|
|
|
interfaceParse->interfaceExtension = (uint8_t *)unionDes;
|
|
|
|
}
|
2021-02-23 09:44:27 +08:00
|
|
|
if ((unionDes->common.bDescriptorType == 0x00) ||
|
|
|
|
(unionDes->common.bLength == 0x00)) /* the descriptor data is wrong */
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
return kStatus_USB_Error;
|
|
|
|
}
|
|
|
|
interfaceParse->interfaceExtensionLength += unionDes->common.bLength;
|
|
|
|
unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* endpoint descriptor */
|
2021-02-23 09:44:27 +08:00
|
|
|
if (interfaceParse->interfaceDesc->bNumEndpoints != 0)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
if ((unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_ENDPOINT) ||
|
|
|
|
(interfaceParse->interfaceDesc->bNumEndpoints > USB_HOST_CONFIG_INTERFACE_MAX_EP))
|
|
|
|
{
|
|
|
|
#ifdef HOST_ECHO
|
|
|
|
usb_echo("interface descriptor error\n");
|
|
|
|
#endif
|
|
|
|
return kStatus_USB_Error;
|
|
|
|
}
|
|
|
|
for (; interfaceParse->epCount < interfaceParse->interfaceDesc->bNumEndpoints;
|
|
|
|
(interfaceParse->epCount)++)
|
|
|
|
{
|
|
|
|
if (((uint32_t)unionDes >= endPos) ||
|
|
|
|
(unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_ENDPOINT))
|
|
|
|
{
|
|
|
|
#ifdef HOST_ECHO
|
|
|
|
usb_echo("endpoint descriptor error\n");
|
|
|
|
#endif
|
|
|
|
return kStatus_USB_Error;
|
|
|
|
}
|
2021-02-23 09:44:27 +08:00
|
|
|
epParse = (usb_host_ep_t *)&interfaceParse->epList[interfaceParse->epCount];
|
|
|
|
epParse->epDesc = (usb_descriptor_endpoint_t *)unionDes;
|
|
|
|
epParse->epExtensionLength = 0;
|
|
|
|
epParse->epExtension = NULL;
|
|
|
|
if (unionDes->common.bLength == 0x00) /* the descriptor data is wrong */
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
return kStatus_USB_Error;
|
|
|
|
}
|
|
|
|
unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
|
|
|
|
while ((uint32_t)unionDes < endPos)
|
|
|
|
{
|
|
|
|
if ((unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_ENDPOINT) &&
|
|
|
|
(unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_INTERFACE))
|
|
|
|
{
|
|
|
|
if (epParse->epExtension == NULL)
|
|
|
|
{
|
|
|
|
epParse->epExtension = (uint8_t *)unionDes;
|
|
|
|
}
|
2021-02-23 09:44:27 +08:00
|
|
|
if ((unionDes->common.bDescriptorType == 0x00) ||
|
|
|
|
(unionDes->common.bLength == 0x00)) /* the descriptor data is wrong */
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
return kStatus_USB_Error;
|
|
|
|
}
|
|
|
|
epParse->epExtensionLength += unionDes->common.bLength;
|
|
|
|
unionDes =
|
|
|
|
(usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (interfaceParse == NULL)
|
|
|
|
{
|
|
|
|
return kStatus_USB_Error; /* in normal situation this cannot reach */
|
|
|
|
}
|
|
|
|
interfaceParse->alternateSettingNumber++;
|
|
|
|
if (interfaceParse->interfaceExtension == NULL)
|
|
|
|
{
|
|
|
|
interfaceParse->interfaceExtension = (uint8_t *)unionDes;
|
|
|
|
}
|
2021-02-23 09:44:27 +08:00
|
|
|
if (unionDes->common.bLength == 0x00) /* the descriptor data is wrong */
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
return kStatus_USB_Error;
|
|
|
|
}
|
|
|
|
interfaceParse->interfaceExtensionLength += unionDes->common.bLength;
|
|
|
|
unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
|
|
|
|
while ((uint32_t)unionDes < endPos)
|
|
|
|
{
|
|
|
|
if (unionDes->common.bDescriptorType != USB_DESCRIPTOR_TYPE_INTERFACE)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
if ((unionDes->common.bDescriptorType == 0x00) ||
|
|
|
|
(unionDes->common.bLength == 0x00)) /* the descriptor data is wrong */
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
return kStatus_USB_Error;
|
|
|
|
}
|
|
|
|
interfaceParse->interfaceExtensionLength += unionDes->common.bLength;
|
|
|
|
unionDes = (usb_descriptor_union_t *)((uint32_t)unionDes + unionDes->common.bLength);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return kStatus_USB_Error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
for (endPos = 0; endPos < deviceInstance->configuration.interfaceCount; ++endPos)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
deviceInstance->interfaceStatus[endPos] = kStatus_interface_Attached;
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return kStatus_USB_Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
usb_status_t USB_HostAttachDevice(usb_host_handle hostHandle,
|
|
|
|
uint8_t speed,
|
|
|
|
uint8_t hubNumber,
|
|
|
|
uint8_t portNumber,
|
|
|
|
uint8_t level,
|
|
|
|
usb_device_handle *deviceHandle)
|
|
|
|
{
|
|
|
|
usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
|
|
|
|
usb_host_device_instance_t *newInstance;
|
|
|
|
#if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
|
|
|
|
usb_host_device_instance_t *currentInstance;
|
|
|
|
#endif
|
|
|
|
uint8_t address;
|
|
|
|
usb_host_pipe_init_t pipeInit;
|
|
|
|
|
|
|
|
if (hostHandle == NULL)
|
|
|
|
{
|
|
|
|
return kStatus_USB_InvalidHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check whether is the device attached? */
|
|
|
|
#if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
|
|
|
|
currentInstance = (usb_host_device_instance_t *)hostInstance->deviceList;
|
|
|
|
while (currentInstance != NULL)
|
|
|
|
{
|
|
|
|
if ((currentInstance->hubNumber == hubNumber) && (currentInstance->portNumber == portNumber))
|
|
|
|
{
|
|
|
|
*deviceHandle = NULL;
|
|
|
|
#ifdef HOST_ECHO
|
|
|
|
usb_echo("device has attached\r\n");
|
|
|
|
#endif
|
|
|
|
return kStatus_USB_Busy;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
currentInstance = currentInstance->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (hostInstance->deviceList != NULL)
|
|
|
|
{
|
|
|
|
*deviceHandle = NULL;
|
|
|
|
usb_echo("device has attached\r\n");
|
|
|
|
return kStatus_USB_Busy;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Allocate new device instance */
|
2021-02-23 09:44:27 +08:00
|
|
|
newInstance = (usb_host_device_instance_t *)USB_OsaMemoryAllocate(sizeof(usb_host_device_instance_t));
|
2019-12-26 14:10:06 +08:00
|
|
|
if (newInstance == NULL)
|
|
|
|
{
|
|
|
|
#ifdef HOST_ECHO
|
|
|
|
usb_echo("allocate dev instance fail\r\n");
|
|
|
|
#endif
|
|
|
|
return kStatus_USB_AllocFail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* new instance fields init */
|
2021-02-23 09:44:27 +08:00
|
|
|
newInstance->hostHandle = hostHandle;
|
|
|
|
newInstance->speed = speed;
|
|
|
|
newInstance->stallRetries = USB_HOST_CONFIG_ENUMERATION_MAX_STALL_RETRIES;
|
|
|
|
newInstance->enumRetries = USB_HOST_CONFIG_ENUMERATION_MAX_RETRIES;
|
|
|
|
newInstance->setAddress = 0;
|
|
|
|
newInstance->deviceAttachState = kStatus_device_Attached;
|
2019-12-26 14:10:06 +08:00
|
|
|
#if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
|
|
|
|
newInstance->deviceDescriptor =
|
|
|
|
(usb_descriptor_device_t *)SDK_Malloc(sizeof(usb_descriptor_device_t) + 9, USB_CACHE_LINESIZE);
|
|
|
|
#else
|
2021-02-23 09:44:27 +08:00
|
|
|
newInstance->deviceDescriptor =
|
|
|
|
(usb_descriptor_device_t *)USB_OsaMemoryAllocate(sizeof(usb_descriptor_device_t) + 9);
|
2019-12-26 14:10:06 +08:00
|
|
|
#endif
|
|
|
|
if (newInstance->deviceDescriptor == NULL)
|
|
|
|
{
|
|
|
|
#ifdef HOST_ECHO
|
|
|
|
usb_echo("allocate newInstance->deviceDescriptor fail\r\n");
|
|
|
|
#endif
|
|
|
|
#if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
|
|
|
|
SDK_Free(newInstance->deviceDescriptor);
|
|
|
|
#else
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_OsaMemoryFree(newInstance->deviceDescriptor);
|
2019-12-26 14:10:06 +08:00
|
|
|
#endif
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_OsaMemoryFree(newInstance);
|
2019-12-26 14:10:06 +08:00
|
|
|
return kStatus_USB_AllocFail;
|
|
|
|
}
|
|
|
|
newInstance->enumBuffer = (uint8_t *)((uint8_t *)newInstance->deviceDescriptor + sizeof(usb_descriptor_device_t));
|
|
|
|
#if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
|
2021-02-23 09:44:27 +08:00
|
|
|
newInstance->hubNumber = hubNumber;
|
2019-12-26 14:10:06 +08:00
|
|
|
newInstance->portNumber = portNumber;
|
2021-02-23 09:44:27 +08:00
|
|
|
newInstance->level = level;
|
2019-12-26 14:10:06 +08:00
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
if ((speed != USB_SPEED_HIGH) && (level > 1))
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
newInstance->hsHubNumber = USB_HostHubGetHsHubNumber(hostHandle, hubNumber);
|
|
|
|
newInstance->hsHubPort = USB_HostHubGetHsHubPort(hostHandle, hubNumber, portNumber);
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newInstance->hsHubNumber = hubNumber;
|
2021-02-23 09:44:27 +08:00
|
|
|
newInstance->hsHubPort = portNumber;
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
#endif /* USB_HOST_CONFIG_HUB */
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostLock();
|
2019-12-26 14:10:06 +08:00
|
|
|
/* allocate address && insert to the dev list */
|
|
|
|
address = USB_HostAllocateDeviceAddress(hostInstance);
|
2021-02-23 09:44:27 +08:00
|
|
|
if (address == 0)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
#ifdef HOST_ECHO
|
|
|
|
usb_echo("allocate address fail\r\n");
|
|
|
|
#endif
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostUnlock();
|
2019-12-26 14:10:06 +08:00
|
|
|
#if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
|
|
|
|
SDK_Free(newInstance->deviceDescriptor);
|
|
|
|
#else
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_OsaMemoryFree(newInstance->deviceDescriptor);
|
2019-12-26 14:10:06 +08:00
|
|
|
#endif
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_OsaMemoryFree(newInstance);
|
2019-12-26 14:10:06 +08:00
|
|
|
return kStatus_USB_Error;
|
|
|
|
}
|
|
|
|
newInstance->allocatedAddress = address;
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
newInstance->next = (usb_host_device_instance_t *)hostInstance->deviceList;
|
2019-12-26 14:10:06 +08:00
|
|
|
hostInstance->deviceList = newInstance;
|
2021-02-23 09:44:27 +08:00
|
|
|
newInstance->state = kStatus_DEV_Initial;
|
|
|
|
USB_HostUnlock();
|
2019-12-26 14:10:06 +08:00
|
|
|
|
|
|
|
/* open control pipe */
|
2021-02-23 09:44:27 +08:00
|
|
|
pipeInit.devInstance = newInstance;
|
|
|
|
pipeInit.pipeType = USB_ENDPOINT_CONTROL;
|
|
|
|
pipeInit.direction = 0;
|
2019-12-26 14:10:06 +08:00
|
|
|
pipeInit.endpointAddress = 0;
|
2021-02-23 09:44:27 +08:00
|
|
|
pipeInit.interval = 0;
|
|
|
|
pipeInit.maxPacketSize = 8;
|
2019-12-26 14:10:06 +08:00
|
|
|
pipeInit.numberPerUframe = 0;
|
2021-02-23 09:44:27 +08:00
|
|
|
pipeInit.nakCount = USB_HOST_CONFIG_MAX_NAK;
|
2019-12-26 14:10:06 +08:00
|
|
|
if (USB_HostOpenPipe(hostHandle, &newInstance->controlPipe, &pipeInit) != kStatus_USB_Success)
|
|
|
|
{
|
|
|
|
/* don't need release resource, resource is released when detach */
|
|
|
|
*deviceHandle = newInstance;
|
|
|
|
#if ((defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE)) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE > 0U))
|
|
|
|
SDK_Free(newInstance->deviceDescriptor);
|
|
|
|
#else
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_OsaMemoryFree(newInstance->deviceDescriptor);
|
2019-12-26 14:10:06 +08:00
|
|
|
#endif
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_OsaMemoryFree(newInstance);
|
2019-12-26 14:10:06 +08:00
|
|
|
return kStatus_USB_Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start enumeration */
|
2021-02-23 09:44:27 +08:00
|
|
|
newInstance->state = kStatus_DEV_GetDes8;
|
|
|
|
USB_HostProcessState(newInstance); /* process enumeration state machine */
|
2019-12-26 14:10:06 +08:00
|
|
|
|
|
|
|
*deviceHandle = newInstance;
|
|
|
|
return kStatus_USB_Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
usb_status_t USB_HostDetachDevice(usb_host_handle hostHandle, uint8_t hubNumber, uint8_t portNumber)
|
|
|
|
{
|
|
|
|
usb_host_device_instance_t *deviceInstance;
|
|
|
|
usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
|
|
|
|
|
|
|
|
if (hostHandle == NULL)
|
|
|
|
{
|
|
|
|
return kStatus_USB_InvalidHandle;
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostLock();
|
2019-12-26 14:10:06 +08:00
|
|
|
/* search for device instance handle */
|
|
|
|
#if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
|
|
|
|
deviceInstance = (usb_host_device_instance_t *)hostInstance->deviceList;
|
|
|
|
while (deviceInstance != NULL)
|
|
|
|
{
|
|
|
|
if ((deviceInstance->hubNumber == hubNumber) && (deviceInstance->portNumber == portNumber))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
deviceInstance = deviceInstance->next;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
deviceInstance = (usb_host_device_instance_t *)hostInstance->deviceList;
|
|
|
|
#endif
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostUnlock();
|
2019-12-26 14:10:06 +08:00
|
|
|
if (deviceInstance != NULL)
|
|
|
|
{
|
|
|
|
return USB_HostDetachDeviceInternal(hostHandle, deviceInstance); /* device instance detach */
|
|
|
|
}
|
|
|
|
return kStatus_USB_Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
usb_status_t USB_HostDetachDeviceInternal(usb_host_handle hostHandle, usb_device_handle deviceHandle)
|
|
|
|
{
|
|
|
|
usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)deviceHandle;
|
2021-02-23 09:44:27 +08:00
|
|
|
usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
|
2019-12-26 14:10:06 +08:00
|
|
|
if ((hostHandle == NULL) || (deviceHandle == NULL))
|
|
|
|
{
|
|
|
|
return kStatus_USB_InvalidHandle;
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
deviceInstance->deviceAttachState = kStatus_device_Detached; /* mark the device is detached from host */
|
2019-12-26 14:10:06 +08:00
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
if (deviceInstance->state >= kStatus_DEV_Initial) /* device instance is valid */
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
/* detach internally */
|
2021-02-23 09:44:27 +08:00
|
|
|
if (deviceInstance->state < kStatus_DEV_AppUsed) /* enumeration is not done */
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
if (deviceInstance->controlPipe != NULL)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostCancelTransfer(hostInstance, deviceInstance->controlPipe, NULL);
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* remove device instance from host */
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostRemoveDeviceInstance(hostInstance, deviceInstance);
|
2019-12-26 14:10:06 +08:00
|
|
|
USB_HostReleaseDeviceResource(hostInstance, deviceInstance);
|
|
|
|
}
|
2021-02-23 09:44:27 +08:00
|
|
|
else /* enumeration has be done and notifed application */
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostNotifyDevice(deviceInstance, kUSB_HostEventDetach); /* notify application device detach */
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return kStatus_USB_Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t USB_HostGetDeviceAttachState(usb_device_handle deviceHandle)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
return deviceHandle ? ((usb_host_device_instance_t *)deviceHandle)->deviceAttachState : 0x0;
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
usb_status_t USB_HostValidateDevice(usb_host_handle hostHandle, usb_device_handle deviceHandle)
|
|
|
|
{
|
|
|
|
usb_host_device_instance_t *searchDev;
|
|
|
|
|
|
|
|
if (deviceHandle == NULL)
|
|
|
|
{
|
|
|
|
return kStatus_USB_InvalidParameter;
|
|
|
|
}
|
|
|
|
/* search for the device */
|
|
|
|
searchDev = (usb_host_device_instance_t *)((usb_host_instance_t *)hostHandle)->deviceList;
|
|
|
|
while ((searchDev != NULL) && ((usb_device_handle)searchDev != deviceHandle))
|
|
|
|
{
|
|
|
|
searchDev = searchDev->next;
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
if (searchDev)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
return kStatus_USB_Success;
|
|
|
|
}
|
|
|
|
return kStatus_USB_Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static usb_status_t USB_HostControlBus(usb_host_handle hostHandle, uint8_t controlType)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
usb_status_t status = kStatus_USB_Success;
|
2019-12-26 14:10:06 +08:00
|
|
|
usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
|
|
|
|
|
|
|
|
if (hostHandle == NULL)
|
|
|
|
{
|
|
|
|
return kStatus_USB_InvalidHandle;
|
|
|
|
}
|
2021-02-23 09:44:27 +08:00
|
|
|
|
2019-12-26 14:10:06 +08:00
|
|
|
status = hostInstance->controllerTable->controllerIoctl(hostInstance->controllerHandle, kUSB_HostBusControl,
|
|
|
|
&controlType);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
usb_status_t USB_HostOpenDeviceInterface(usb_device_handle deviceHandle, usb_host_interface_handle interfaceHandle)
|
|
|
|
{
|
|
|
|
usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)deviceHandle;
|
2021-02-23 09:44:27 +08:00
|
|
|
usb_host_instance_t *hostInstance = NULL;
|
2019-12-26 14:10:06 +08:00
|
|
|
uint8_t interfaceIndex;
|
|
|
|
uint8_t index = 0;
|
|
|
|
|
|
|
|
if ((deviceHandle == NULL) || (interfaceHandle == NULL))
|
|
|
|
{
|
|
|
|
return kStatus_USB_InvalidHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
hostInstance = (usb_host_instance_t *)deviceInstance->hostHandle;
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostLock();
|
2019-12-26 14:10:06 +08:00
|
|
|
/* check host_instance valid? */
|
|
|
|
for (; index < USB_HOST_CONFIG_MAX_HOST; ++index)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
if ((g_UsbHostInstance[index].occupied == 1) &&
|
2019-12-26 14:10:06 +08:00
|
|
|
((usb_host_instance_t *)(&g_UsbHostInstance[index]) == (hostInstance)))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (index >= USB_HOST_CONFIG_MAX_HOST)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostUnlock();
|
2019-12-26 14:10:06 +08:00
|
|
|
return kStatus_USB_Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check deviceHandle valid? */
|
|
|
|
if (USB_HostValidateDevice(hostInstance, deviceHandle) != kStatus_USB_Success)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostUnlock();
|
2019-12-26 14:10:06 +08:00
|
|
|
return kStatus_USB_Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* search interface and set the interface as opened */
|
|
|
|
for (interfaceIndex = 0; interfaceIndex < deviceInstance->configuration.interfaceCount; ++interfaceIndex)
|
|
|
|
{
|
|
|
|
if (&deviceInstance->configuration.interfaceList[interfaceIndex] == interfaceHandle)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
deviceInstance->interfaceStatus[interfaceIndex] = kStatus_interface_Opened;
|
2019-12-26 14:10:06 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostUnlock();
|
2019-12-26 14:10:06 +08:00
|
|
|
|
|
|
|
return kStatus_USB_Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
usb_status_t USB_HostCloseDeviceInterface(usb_device_handle deviceHandle, usb_host_interface_handle interfaceHandle)
|
|
|
|
{
|
|
|
|
usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)deviceHandle;
|
2021-02-23 09:44:27 +08:00
|
|
|
usb_host_instance_t *hostInstance = NULL;
|
2019-12-26 14:10:06 +08:00
|
|
|
uint8_t interfaceIndex;
|
|
|
|
uint8_t removeLabel = 1;
|
2021-02-23 09:44:27 +08:00
|
|
|
uint8_t index = 0;
|
2019-12-26 14:10:06 +08:00
|
|
|
|
|
|
|
if (deviceHandle == NULL)
|
|
|
|
{
|
|
|
|
return kStatus_USB_InvalidHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
hostInstance = (usb_host_instance_t *)deviceInstance->hostHandle;
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostLock();
|
2019-12-26 14:10:06 +08:00
|
|
|
/* check host_instance valid? */
|
|
|
|
for (; index < USB_HOST_CONFIG_MAX_HOST; ++index)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
if ((g_UsbHostInstance[index].occupied == 1) &&
|
2019-12-26 14:10:06 +08:00
|
|
|
((usb_host_instance_t *)(&g_UsbHostInstance[index]) == (hostInstance)))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (index >= USB_HOST_CONFIG_MAX_HOST)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostUnlock();
|
2019-12-26 14:10:06 +08:00
|
|
|
return kStatus_USB_Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check deviceHandle valid? */
|
|
|
|
if (USB_HostValidateDevice(hostInstance, deviceHandle) != kStatus_USB_Success)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostUnlock();
|
2019-12-26 14:10:06 +08:00
|
|
|
return kStatus_USB_Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (interfaceHandle != NULL)
|
|
|
|
{
|
|
|
|
/* search interface and set the interface as detached */
|
|
|
|
for (interfaceIndex = 0; interfaceIndex < deviceInstance->configuration.interfaceCount; ++interfaceIndex)
|
|
|
|
{
|
|
|
|
if (&deviceInstance->configuration.interfaceList[interfaceIndex] == interfaceHandle)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
deviceInstance->interfaceStatus[interfaceIndex] = kStatus_interface_Detached;
|
2019-12-26 14:10:06 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
if (deviceInstance->deviceAttachState == kStatus_device_Detached) /* device is removed from host */
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
removeLabel = 1;
|
|
|
|
/* check all the interfaces of the device are not opened */
|
|
|
|
for (interfaceIndex = 0; interfaceIndex < deviceInstance->configuration.interfaceCount; ++interfaceIndex)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
if (deviceInstance->interfaceStatus[interfaceIndex] == kStatus_interface_Opened)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
removeLabel = 0;
|
2019-12-26 14:10:06 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-02-23 09:44:27 +08:00
|
|
|
if (removeLabel == 1)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
/* remove device instance from host */
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostRemoveDeviceInstance(hostInstance, deviceInstance);
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostUnlock();
|
2019-12-26 14:10:06 +08:00
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
if (removeLabel == 1)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
USB_HostReleaseDeviceResource((usb_host_instance_t *)deviceInstance->hostHandle,
|
|
|
|
deviceInstance); /* release device resource */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostUnlock();
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return kStatus_USB_Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
usb_status_t USB_HostRemoveDevice(usb_host_handle hostHandle, usb_device_handle deviceHandle)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
usb_host_instance_t *hostInstance = (usb_host_instance_t *)hostHandle;
|
2019-12-26 14:10:06 +08:00
|
|
|
usb_host_device_instance_t *deviceInstance = (usb_host_device_instance_t *)deviceHandle;
|
2021-02-23 09:44:27 +08:00
|
|
|
uint8_t interfaceIndex = 0;
|
2019-12-26 14:10:06 +08:00
|
|
|
#if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
|
|
|
|
uint8_t level = 0;
|
|
|
|
uint8_t devHubNo;
|
|
|
|
uint8_t devPortNo;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if ((hostHandle == NULL) || (deviceHandle == NULL))
|
|
|
|
{
|
|
|
|
return kStatus_USB_InvalidHandle;
|
|
|
|
}
|
|
|
|
if (deviceInstance->hostHandle != hostHandle)
|
|
|
|
{
|
|
|
|
return kStatus_USB_InvalidParameter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (USB_HostValidateDevice(hostInstance, deviceInstance) == kStatus_USB_Success) /* device is valid */
|
|
|
|
{
|
|
|
|
#if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
|
2021-02-23 09:44:27 +08:00
|
|
|
devHubNo = deviceInstance->hubNumber;
|
2019-12-26 14:10:06 +08:00
|
|
|
devPortNo = deviceInstance->portNumber;
|
2021-02-23 09:44:27 +08:00
|
|
|
level = deviceInstance->level;
|
2019-12-26 14:10:06 +08:00
|
|
|
#endif
|
|
|
|
|
2021-02-23 09:44:27 +08:00
|
|
|
deviceInstance->deviceAttachState = kStatus_device_Detached;
|
|
|
|
if (deviceInstance->state >= kStatus_DEV_Initial) /* device is valid */
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
if (deviceInstance->state < kStatus_DEV_AppUsed) /* enumeraion is not done or application don't use */
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
/* detach internally */
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostDetachDeviceInternal(hostHandle, deviceHandle);
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
else /* application use the device */
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
for (interfaceIndex = 0; interfaceIndex < deviceInstance->configuration.interfaceCount;
|
2019-12-26 14:10:06 +08:00
|
|
|
++interfaceIndex)
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
if (deviceInstance->interfaceStatus[interfaceIndex] == kStatus_interface_Opened)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
|
|
|
#ifdef HOST_ECHO
|
|
|
|
usb_echo("error: there is class instance that is not deinited\r\n");
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* remove device instance from host */
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostRemoveDeviceInstance(hostInstance, deviceInstance);
|
2019-12-26 14:10:06 +08:00
|
|
|
USB_HostReleaseDeviceResource(hostInstance, deviceInstance); /* release resource */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ((defined USB_HOST_CONFIG_HUB) && (USB_HOST_CONFIG_HUB))
|
2021-02-23 09:44:27 +08:00
|
|
|
if (level == 1)
|
2019-12-26 14:10:06 +08:00
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostControlBus(hostHandle, kUSB_HostBusReset); /* reset controller port */
|
|
|
|
USB_HostControlBus(hostHandle, kUSB_HostBusRestart); /* restart controller port */
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-23 09:44:27 +08:00
|
|
|
USB_HostHubRemovePort(hostHandle, devHubNo, devPortNo); /* reset hub port */
|
2019-12-26 14:10:06 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
USB_HostControlBus(hostHandle, kUSB_HostBusReset); /* reset controller port */
|
|
|
|
USB_HostControlBus(hostHandle, kUSB_HostBusRestart); /* restart controller port */
|
|
|
|
#endif /* USB_HOST_CONFIG_HUB */
|
|
|
|
}
|
|
|
|
|
|
|
|
return kStatus_USB_Success;
|
|
|
|
}
|