initial check in based on SVN revision 575

This commit is contained in:
2025-05-14 12:57:39 -05:00
commit a3ef12e24a
217 changed files with 95547 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,286 @@
/*
* Copyright (c) 2016, Freescale Semiconductor, Inc.
* Copyright 2016 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef __USB_DEVICE_LPC3511IP_H__
#define __USB_DEVICE_LPC3511IP_H__
#include "fsl_device_registers.h"
/*!
* @addtogroup usb_device_controller_lpcip3511_driver
* @{
*/
/*******************************************************************************
* Definitions
******************************************************************************/
/* For bulk out endpoint in high speed mode, use long length data transfer to decrease the Ping packet count to increase bulk bandwidth */
/* The bigger this macro's value is, the higher bandwidth bulk out endpoint has. However, you need to set a reasonable value for this macro based on USB RAM size of Soc. If this macro's value is too big, link may be failed. */
/* Note that please set this value as integral multiple of 512U */
#if (((defined(USB_DEVICE_CONFIG_MSC)) && (USB_DEVICE_CONFIG_MSC > 0U)) && ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U)))
#define USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX (0U)
#endif
/*! @brief The reserved buffer size, the buffer is for the memory copy if the application transfer buffer is
((not 64 bytes alignment) || (not in the same 64K ram) || (HS && OUT && not multiple of 4)) */
#if ((defined(USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX)) && (USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX > 0U))
/* if use USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX (>0U), need to increase the reserved buffer size */
#define USB_DEVICE_IP3511_ENDPOINT_RESERVED_BUFFER_SIZE ((5U + ((USB_DEVICE_IP3511HS_BULK_OUT_ONE_TIME_TRANSFER_SIZE_MAX - 512U) / 512U)) * 1024U)
#else
#define USB_DEVICE_IP3511_ENDPOINT_RESERVED_BUFFER_SIZE (5U * 1024U)
#endif
/*! @brief Use one bit to represent one reserved 64 bytes to allocate the buffer by uint of 64 bytes. */
#define USB_DEVICE_IP3511_BITS_FOR_RESERVED_BUFFER ((USB_DEVICE_IP3511_ENDPOINT_RESERVED_BUFFER_SIZE + 63U) / 64U)
/*! @brief How many IPs support the reserved buffer */
#define USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY (USB_DEVICE_CONFIG_LPCIP3511FS + USB_DEVICE_CONFIG_LPCIP3511HS)
/*! @brief Prime all the double endpoint buffer at the same time, if the transfer length is larger than max packet size.
*/
#define USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE (1U)
#if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
#define USB_LPC3511IP_Type USBHSD_Type
#define USB_DEVICE_IP3511_ENDPOINTS_NUM FSL_FEATURE_USBHSD_EP_NUM
#else
#define USB_LPC3511IP_Type USB_Type
#define USB_DEVICE_IP3511_ENDPOINTS_NUM FSL_FEATURE_USB_EP_NUM
#endif
/* for out endpoint,only use buffer toggle, disable prime double buffer at the same time*/
/*host send data less than maxpacket size and in endpoint prime length more more than maxpacketsize, there will be state
* mismtach*/
#if USB_DEVICE_IP3511_DOUBLE_BUFFER_ENABLE
#define USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER (1U)
#else
#define USB_DEVICE_IP3511_DISABLE_OUT_DOUBLE_BUFFER (0U)
#endif
#define USB_DEVICE_IP3511HS_LPM_ADPPROBE_ATTACH_DEBOUNCE_COUNT (3)
/* if FSL_FEATURE_USBHSD_HAS_EXIT_HS_ISSUE is true:
* Enable this macro to exit HS mode automatically if the user case is:
* host and device keep cable connected, and host turn off vbus to simulate detachment.
* If user disconnects the cable, there is no issue and don't need enable this macro.
* There is one delay in the isr if enable this macro.
*/
#define USB_DEVICE_IP3511HS_FORCE_EXIT_HS_MODE_ENABLE (0u)
/*! @brief Endpoint state structure */
typedef struct _usb_device_lpc3511ip_endpoint_state_struct
{
uint8_t *transferBuffer; /*!< Address of buffer containing the data to be transmitted */
uint32_t transferLength; /*!< Length of data to transmit. */
uint32_t transferDone; /*!< The data length has been transferred*/
uint32_t transferPrimedLength; /*!< it may larger than transferLength, because the primed length may larger than the
transaction length. */
uint8_t *epPacketBuffer; /*!< The max packet buffer for copying*/
union
{
uint32_t state; /*!< The state of the endpoint */
struct
{
uint32_t maxPacketSize : 11U; /*!< The maximum packet size of the endpoint */
uint32_t stalled : 1U; /*!< The endpoint is stalled or not */
uint32_t transferring : 1U; /*!< The endpoint is transferring */
uint32_t zlt : 1U; /*!< zlt flag */
uint32_t stallPrimed : 1U;
uint32_t epPacketCopyed : 1U; /*!< whether use the copy buffer */
uint32_t epControlDefault : 5u; /*!< The EP command/status 26~30 bits */
uint32_t doubleBufferBusy : 2U; /*!< How many buffers are primed, for control endpoint it is not used */
uint32_t producerOdd : 1U; /*!< When priming one transaction, prime to this endpoint buffer */
uint32_t consumerOdd : 1U; /*!< When transaction is done, read result from this endpoint buffer */
uint32_t endpointType : 2U;
uint32_t reserved1 : 5U;
} stateBitField;
} stateUnion;
union
{
uint16_t epBufferStatus;
/* If double buff is disable, only epBufferStatusUnion[0] is used;
For control endpoint, only epBufferStatusUnion[0] is used. */
struct
{
uint16_t transactionLength : 15U;
uint16_t epPacketCopyed : 1U;
} epBufferStatusField;
} epBufferStatusUnion[2];
} usb_device_lpc3511ip_endpoint_state_struct_t;
/*! @brief LPC USB controller (IP3511) state structure */
typedef struct _usb_device_lpc3511ip_state_struct
{
/*!< control data buffer, must align with 64 */
uint8_t *controlData;
/*!< 8 bytes' setup data, must align with 64 */
uint8_t *setupData;
/*!< 4 bytes for zero length transaction, must align with 64 */
uint8_t *zeroTransactionData;
/* Endpoint state structures */
usb_device_lpc3511ip_endpoint_state_struct_t endpointState[(USB_DEVICE_IP3511_ENDPOINTS_NUM * 2)];
usb_device_handle deviceHandle; /*!< (4 bytes) Device handle used to identify the device object belongs to */
USB_LPC3511IP_Type *registerBase; /*!< (4 bytes) ip base address */
volatile uint32_t *epCommandStatusList; /* endpoint list */
#if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U)) && \
(defined(FSL_FEATURE_SOC_USBHSDCD_COUNT) && (FSL_FEATURE_SOC_USBHSDCD_COUNT > 0U))
void *dcdHandle; /*!< Dcd handle used to identify the device object belongs to */
#endif
uint8_t controllerId; /*!< Controller ID */
uint8_t isResetting; /*!< Is doing device reset or not */
uint8_t deviceSpeed; /*!< some controller support the HS */
#if ((defined(USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY)) && (USB_DEVICE_IP3511_RESERVED_BUFFER_FOR_COPY > 0U))
uint8_t *epReservedBuffer;
uint8_t epReservedBufferBits[(USB_DEVICE_IP3511_BITS_FOR_RESERVED_BUFFER + 7) / 8];
#endif
#if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
uint8_t controllerSpeed;
#endif
#if (defined(USB_DEVICE_CONFIG_DETACH_ENABLE) && (USB_DEVICE_CONFIG_DETACH_ENABLE))
uint8_t deviceState; /*!< Is device attached,1 attached,0 detached */
#endif
#if (defined(USB_DEVICE_CONFIG_LOW_POWER_MODE) && (USB_DEVICE_CONFIG_LOW_POWER_MODE > 0U))
#if (defined(USB_DEVICE_CONFIG_LPM_L1) && (USB_DEVICE_CONFIG_LPM_L1 > 0U))
uint8_t lpmRemoteWakeUp;
#endif
#endif
#if ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))
#if (defined(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK) && \
(FSL_FEATURE_USBHSD_INTERRUPT_DATAX_ISSUE_VERSION_CHECK))
uint8_t hsInterruptIssue;
#endif
#endif
} usb_device_lpc3511ip_state_struct_t;
/*!
* @name USB device controller (IP3511) functions
* @{
*/
/*******************************************************************************
* API
******************************************************************************/
#if defined(__cplusplus)
extern "C" {
#endif
/*!
* @brief Initializes the USB device controller instance.
*
* This function initializes the USB device controller module specified by the controllerId.
*
* @param[in] controllerId The controller ID of the USB IP. See the enumeration type usb_controller_index_t.
* @param[in] handle Pointer of the device handle used to identify the device object belongs to.
* @param[out] controllerHandle An out parameter used to return the pointer of the device controller handle to the
* caller.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceLpc3511IpInit(uint8_t controllerId,
usb_device_handle handle,
usb_device_controller_handle *controllerHandle);
/*!
* @brief Deinitializes the USB device controller instance.
*
* This function deinitializes the USB device controller module.
*
* @param[in] controllerHandle Pointer of the device controller handle.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceLpc3511IpDeinit(usb_device_controller_handle controllerHandle);
/*!
* @brief Sends data through a specified endpoint.
*
* This function sends data through a specified endpoint.
*
* @param[in] controllerHandle Pointer of the device controller handle.
* @param[in] endpointAddress Endpoint index.
* @param[in] buffer The memory address to hold the data need to be sent.
* @param[in] length The data length need to be sent.
*
* @return A USB error code or kStatus_USB_Success.
*
* @note The return value indicates whether the sending request is successful or not. The transfer completion is
* notified by the
* corresponding callback function.
* Currently, only one transfer request can be supported for a specific endpoint.
* If there is a specific requirement to support multiple transfer requests for a specific endpoint, the application
* should implement a queue in the application level.
* The subsequent transfer can begin only when the previous transfer is done (a notification is obtained through the
* endpoint
* callback).
*/
usb_status_t USB_DeviceLpc3511IpSend(usb_device_controller_handle controllerHandle,
uint8_t endpointAddress,
uint8_t *buffer,
uint32_t length);
/*!
* @brief Receives data through a specified endpoint.
*
* This function receives data through a specified endpoint.
*
* @param[in] controllerHandle Pointer of the device controller handle.
* @param[in] endpointAddress Endpoint index.
* @param[in] buffer The memory address to save the received data.
* @param[in] length The data length to be received.
*
* @return A USB error code or kStatus_USB_Success.
*
* @note The return value indicates whether the receiving request is successful or not. The transfer completion is
* notified by the
* corresponding callback function.
* Currently, only one transfer request can be supported for a specific endpoint.
* If there is a specific requirement to support multiple transfer requests for a specific endpoint, the application
* should implement a queue in the application level.
* The subsequent transfer can begin only when the previous transfer is done (a notification is obtained through the
* endpoint
* callback).
*/
usb_status_t USB_DeviceLpc3511IpRecv(usb_device_controller_handle controllerHandle,
uint8_t endpointAddress,
uint8_t *buffer,
uint32_t length);
/*!
* @brief Cancels the pending transfer in a specified endpoint.
*
* The function is used to cancel the pending transfer in a specified endpoint.
*
* @param[in] controllerHandle ointer of the device controller handle.
* @param[in] ep Endpoint address, bit7 is the direction of endpoint, 1U - IN, abd 0U - OUT.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceLpc3511IpCancel(usb_device_controller_handle controllerHandle, uint8_t ep);
/*!
* @brief Controls the status of the selected item.
*
* The function is used to control the status of the selected item.
*
* @param[in] controllerHandle Pointer of the device controller handle.
* @param[in] type The selected item. Please refer to enumeration type usb_device_control_type_t.
* @param[in,out] param The parameter type is determined by the selected item.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceLpc3511IpControl(usb_device_controller_handle controllerHandle,
usb_device_control_type_t type,
void *param);
/*! @} */
#if defined(__cplusplus)
}
#endif
/*! @} */
#endif /* __USB_DEVICE_LPC3511IP_H__ */

View File

@@ -0,0 +1,964 @@
/*
* Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
* Copyright 2016 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "usb_device_config.h"
#include "usb.h"
#include "usb_device.h"
#include "usb_device_dci.h"
#include "usb_device_class.h"
#include "usb_device_ch9.h"
#if ((defined(USB_DEVICE_CONFIG_NUM)) && (USB_DEVICE_CONFIG_NUM > 0U))
/*******************************************************************************
* Definitions
******************************************************************************/
/*!
* @brief Standard request callback function typedef.
*
* This function is used to handle the standard request.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param setup The pointer of the setup packet.
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
* @param length It is an out parameter, the data length.
*
* @return A USB error code or kStatus_USB_Success.
*/
typedef usb_status_t (*usb_standard_request_callback_t)(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length);
/*******************************************************************************
* Prototypes
******************************************************************************/
static usb_status_t USB_DeviceCh9GetStatus(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length);
static usb_status_t USB_DeviceCh9SetClearFeature(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length);
static usb_status_t USB_DeviceCh9SetAddress(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length);
static usb_status_t USB_DeviceCh9GetDescriptor(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length);
static usb_status_t USB_DeviceCh9GetConfiguration(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length);
static usb_status_t USB_DeviceCh9SetConfiguration(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length);
static usb_status_t USB_DeviceCh9GetInterface(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length);
static usb_status_t USB_DeviceCh9SetInterface(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length);
static usb_status_t USB_DeviceCh9SynchFrame(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length);
/*******************************************************************************
* Variables
******************************************************************************/
/* The function list to handle the standard request. */
static const usb_standard_request_callback_t s_UsbDeviceStandardRequest[] = {
USB_DeviceCh9GetStatus,
USB_DeviceCh9SetClearFeature,
(usb_standard_request_callback_t)NULL,
USB_DeviceCh9SetClearFeature,
(usb_standard_request_callback_t)NULL,
USB_DeviceCh9SetAddress,
USB_DeviceCh9GetDescriptor,
(usb_standard_request_callback_t)NULL,
USB_DeviceCh9GetConfiguration,
USB_DeviceCh9SetConfiguration,
USB_DeviceCh9GetInterface,
USB_DeviceCh9SetInterface,
USB_DeviceCh9SynchFrame,
};
/*******************************************************************************
* Code
******************************************************************************/
/*!
* @brief Handle get status request.
*
* This function is used to handle get status request.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param setup The pointer of the setup packet.
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
* @param length It is an out parameter, the data length.
*
* @retval kStatus_USB_Success The request is handled successfully.
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
* or, the request is unsupported.
*/
static usb_status_t USB_DeviceCh9GetStatus(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length)
{
usb_status_t error = kStatus_USB_InvalidRequest;
uint8_t state = 0U;
(void)USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
if (((uint8_t)kUSB_DeviceStateAddress != state) && ((uint8_t)kUSB_DeviceStateConfigured != state))
{
return error;
}
if ((setup->bmRequestType & USB_REQUEST_TYPE_RECIPIENT_MASK) == USB_REQUEST_TYPE_RECIPIENT_DEVICE)
{
#if (defined(USB_DEVICE_CONFIG_OTG) && (USB_DEVICE_CONFIG_OTG))
if (setup->wIndex == USB_REQUEST_STANDARD_GET_STATUS_OTG_STATUS_SELECTOR)
{
error =
USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusOtg, &classHandle->standardTranscationBuffer);
classHandle->standardTranscationBuffer = USB_SHORT_TO_LITTLE_ENDIAN(classHandle->standardTranscationBuffer);
/* The device status length must be USB_DEVICE_STATUS_SIZE. */
*length = 1;
}
else /* Get the device status */
{
#endif
error = USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDevice,
&classHandle->standardTranscationBuffer);
classHandle->standardTranscationBuffer =
classHandle->standardTranscationBuffer & USB_GET_STATUS_DEVICE_MASK;
classHandle->standardTranscationBuffer = USB_SHORT_TO_LITTLE_ENDIAN(classHandle->standardTranscationBuffer);
/* The device status length must be USB_DEVICE_STATUS_SIZE. */
*length = USB_DEVICE_STATUS_SIZE;
#if (defined(USB_DEVICE_CONFIG_OTG) && (USB_DEVICE_CONFIG_OTG))
}
#endif
}
else if ((setup->bmRequestType & USB_REQUEST_TYPE_RECIPIENT_MASK) == USB_REQUEST_TYPE_RECIPIENT_INTERFACE)
{
/* Get the interface status */
error = kStatus_USB_Success;
classHandle->standardTranscationBuffer = 0U;
/* The interface status length must be USB_INTERFACE_STATUS_SIZE. */
*length = USB_INTERFACE_STATUS_SIZE;
}
else if ((setup->bmRequestType & USB_REQUEST_TYPE_RECIPIENT_MASK) == USB_REQUEST_TYPE_RECIPIENT_ENDPOINT)
{
/* Get the endpoint status */
usb_device_endpoint_status_struct_t endpointStatus;
endpointStatus.endpointAddress = (uint8_t)setup->wIndex;
endpointStatus.endpointStatus = (uint16_t)kUSB_DeviceEndpointStateIdle;
error = USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusEndpoint, &endpointStatus);
classHandle->standardTranscationBuffer = endpointStatus.endpointStatus & USB_GET_STATUS_ENDPOINT_MASK;
classHandle->standardTranscationBuffer = USB_SHORT_TO_LITTLE_ENDIAN(classHandle->standardTranscationBuffer);
/* The endpoint status length must be USB_INTERFACE_STATUS_SIZE. */
*length = USB_ENDPOINT_STATUS_SIZE;
}
else
{
/*no action*/
}
*buffer = (uint8_t *)&classHandle->standardTranscationBuffer;
return error;
}
/*!
* @brief Handle set or clear device feature request.
*
* This function is used to handle set or clear device feature request.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param setup The pointer of the setup packet.
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
* @param length It is an out parameter, the data length.
*
* @retval kStatus_USB_Success The request is handled successfully.
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
* or, the request is unsupported.
*/
static usb_status_t USB_DeviceCh9SetClearFeature(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length)
{
usb_status_t error = kStatus_USB_InvalidRequest;
uint8_t state = 0U;
uint8_t isSet = 0U;
(void)USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
if (((uint8_t)kUSB_DeviceStateAddress != state) && ((uint8_t)kUSB_DeviceStateConfigured != state))
{
return error;
}
/* Identify the request is set or clear the feature. */
if (USB_REQUEST_STANDARD_SET_FEATURE == setup->bRequest)
{
isSet = 1U;
}
if ((setup->bmRequestType & USB_REQUEST_TYPE_RECIPIENT_MASK) == USB_REQUEST_TYPE_RECIPIENT_DEVICE)
{
/* Set or Clear the device feature. */
if (USB_REQUEST_STANDARD_FEATURE_SELECTOR_DEVICE_REMOTE_WAKEUP == setup->wValue)
{
#if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U))
USB_DeviceSetStatus(classHandle->handle, kUSB_DeviceStatusRemoteWakeup, &isSet);
#endif
/* Set or Clear the device remote wakeup feature. */
error = USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventSetRemoteWakeup, &isSet);
}
#if ((defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U)) || \
(defined(USB_DEVICE_CONFIG_LPCIP3511HS) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))) && \
(defined(USB_DEVICE_CONFIG_USB20_TEST_MODE) && (USB_DEVICE_CONFIG_USB20_TEST_MODE > 0U))
else if (USB_REQUEST_STANDARD_FEATURE_SELECTOR_DEVICE_TEST_MODE == setup->wValue)
{
state = kUSB_DeviceStateTestMode;
error = USB_DeviceSetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
}
#endif
#if (defined(USB_DEVICE_CONFIG_OTG) && (USB_DEVICE_CONFIG_OTG))
else if (USB_REQUEST_STANDARD_FEATURE_SELECTOR_B_HNP_ENABLE == setup->wValue)
{
error = USB_DeviceClassCallback(classHandle->handle, kUSB_DeviceEventSetBHNPEnable, &isSet);
}
#endif
else
{
}
}
else if ((setup->bmRequestType & USB_REQUEST_TYPE_RECIPIENT_MASK) == USB_REQUEST_TYPE_RECIPIENT_ENDPOINT)
{
/* Set or Clear the endpoint feature. */
if (USB_REQUEST_STANDARD_FEATURE_SELECTOR_ENDPOINT_HALT == setup->wValue)
{
if (USB_CONTROL_ENDPOINT == (setup->wIndex & USB_ENDPOINT_NUMBER_MASK))
{
/* Set or Clear the control endpoint status(halt or not). */
if (0U != isSet)
{
(void)USB_DeviceStallEndpoint(classHandle->handle, (uint8_t)setup->wIndex);
}
else
{
(void)USB_DeviceUnstallEndpoint(classHandle->handle, (uint8_t)setup->wIndex);
}
}
/* Set or Clear the endpoint status feature. */
if (0U != isSet)
{
error = USB_DeviceClassEvent(classHandle->handle, kUSB_DeviceClassEventSetEndpointHalt, &setup->wIndex);
}
else
{
error =
USB_DeviceClassEvent(classHandle->handle, kUSB_DeviceClassEventClearEndpointHalt, &setup->wIndex);
}
}
else
{
/*no action*/
}
}
else
{
/*no action*/
}
return error;
}
/*!
* @brief Handle set address request.
*
* This function is used to handle set address request.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param setup The pointer of the setup packet.
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
* @param length It is an out parameter, the data length.
*
* @retval kStatus_USB_Success The request is handled successfully.
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state.
*/
static usb_status_t USB_DeviceCh9SetAddress(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length)
{
usb_status_t error = kStatus_USB_InvalidRequest;
uint8_t state = 0U;
(void)USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
if (((uint8_t)kUSB_DeviceStateAddressing != state) && ((uint8_t)kUSB_DeviceStateAddress != state) &&
((uint8_t)kUSB_DeviceStateDefault != state) && ((uint8_t)kUSB_DeviceStateConfigured != state))
{
return error;
}
if ((uint8_t)kUSB_DeviceStateAddressing != state)
{
/* If the device address is not setting, pass the address and the device state will change to
* kUSB_DeviceStateAddressing internally. */
state = (uint8_t)(setup->wValue & 0xFFU);
error = USB_DeviceSetStatus(classHandle->handle, kUSB_DeviceStatusAddress, &state);
}
else
{
/* If the device address is setting, set device address and the address will be write into the controller
* internally. */
error = USB_DeviceSetStatus(classHandle->handle, kUSB_DeviceStatusAddress, NULL);
/* And then change the device state to kUSB_DeviceStateAddress. */
if (kStatus_USB_Success == error)
{
state = (uint8_t)kUSB_DeviceStateAddress;
error = USB_DeviceSetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
}
}
return error;
}
/*!
* @brief Handle get descriptor request.
*
* This function is used to handle get descriptor request.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param setup The pointer of the setup packet.
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
* @param length It is an out parameter, the data length.
*
* @retval kStatus_USB_Success The request is handled successfully.
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
* or, the request is unsupported.
*/
static usb_status_t USB_DeviceCh9GetDescriptor(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length)
{
usb_device_get_descriptor_common_union_t commonDescriptor;
usb_status_t error = kStatus_USB_InvalidRequest;
uint8_t state = 0U;
uint8_t descriptorType = (uint8_t)((setup->wValue & 0xFF00U) >> 8U);
uint8_t descriptorIndex = (uint8_t)((setup->wValue & 0x00FFU));
(void)USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
if (((uint8_t)kUSB_DeviceStateAddress != state) && ((uint8_t)kUSB_DeviceStateConfigured != state) &&
((uint8_t)kUSB_DeviceStateDefault != state))
{
return error;
}
commonDescriptor.commonDescriptor.length = setup->wLength;
if (USB_DESCRIPTOR_TYPE_DEVICE == descriptorType)
{
/* Get the device descriptor */
error = USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventGetDeviceDescriptor,
&commonDescriptor.deviceDescriptor);
}
else if (USB_DESCRIPTOR_TYPE_CONFIGURE == descriptorType)
{
/* Get the configuration descriptor */
commonDescriptor.configurationDescriptor.configuration = descriptorIndex;
error = USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventGetConfigurationDescriptor,
&commonDescriptor.configurationDescriptor);
}
else if (USB_DESCRIPTOR_TYPE_STRING == descriptorType)
{
/* Get the string descriptor */
commonDescriptor.stringDescriptor.stringIndex = descriptorIndex;
commonDescriptor.stringDescriptor.languageId = setup->wIndex;
error = USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventGetStringDescriptor,
&commonDescriptor.stringDescriptor);
}
#if (defined(USB_DEVICE_CONFIG_HID) && (USB_DEVICE_CONFIG_HID > 0U))
else if (USB_DESCRIPTOR_TYPE_HID == descriptorType)
{
/* Get the hid descriptor */
commonDescriptor.hidDescriptor.interfaceNumber = (uint8_t)setup->wIndex;
error = USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventGetHidDescriptor,
&commonDescriptor.hidDescriptor);
}
else if (USB_DESCRIPTOR_TYPE_HID_REPORT == descriptorType)
{
/* Get the hid report descriptor */
commonDescriptor.hidReportDescriptor.interfaceNumber = (uint8_t)setup->wIndex;
error = USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventGetHidReportDescriptor,
&commonDescriptor.hidReportDescriptor);
}
else if (USB_DESCRIPTOR_TYPE_HID_PHYSICAL == descriptorType)
{
/* Get the hid physical descriptor */
commonDescriptor.hidPhysicalDescriptor.index = descriptorIndex;
commonDescriptor.hidPhysicalDescriptor.interfaceNumber = (uint8_t)setup->wIndex;
error = USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventGetHidPhysicalDescriptor,
&commonDescriptor.hidPhysicalDescriptor);
}
#endif
#if (defined(USB_DEVICE_CONFIG_CV_TEST) && (USB_DEVICE_CONFIG_CV_TEST > 0U))
else if (USB_DESCRIPTOR_TYPE_DEVICE_QUALITIER == descriptorType)
{
/* Get the device descriptor */
error = USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventGetDeviceQualifierDescriptor,
&commonDescriptor.deviceDescriptor);
}
#endif
#if (defined(USB_DEVICE_CONFIG_LPM_L1) && (USB_DEVICE_CONFIG_LPM_L1 > 0U))
else if (USB_DESCRIPTOR_TYPE_BOS == descriptorType)
{
/* Get the configuration descriptor */
commonDescriptor.configurationDescriptor.configuration = descriptorIndex;
error = USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventGetBOSDescriptor,
&commonDescriptor.configurationDescriptor);
}
#endif
else
{
}
*buffer = commonDescriptor.commonDescriptor.buffer;
*length = commonDescriptor.commonDescriptor.length;
return error;
}
/*!
* @brief Handle get current configuration request.
*
* This function is used to handle get current configuration request.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param setup The pointer of the setup packet.
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
* @param length It is an out parameter, the data length.
*
* @retval kStatus_USB_Success The request is handled successfully.
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
* or, the request is unsupported.
*/
static usb_status_t USB_DeviceCh9GetConfiguration(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length)
{
uint8_t state = 0U;
(void)USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
if (((uint8_t)kUSB_DeviceStateAddress != state) && (((uint8_t)kUSB_DeviceStateConfigured != state)))
{
return kStatus_USB_InvalidRequest;
}
*length = USB_CONFIGURE_SIZE;
*buffer = (uint8_t *)&classHandle->standardTranscationBuffer;
return USB_DeviceClassCallback(classHandle->handle, (uint8_t)kUSB_DeviceEventGetConfiguration,
&classHandle->standardTranscationBuffer);
}
/*!
* @brief Handle set current configuration request.
*
* This function is used to handle set current configuration request.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param setup The pointer of the setup packet.
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
* @param length It is an out parameter, the data length.
*
* @retval kStatus_USB_Success The request is handled successfully.
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
* or, the request is unsupported.
*/
static usb_status_t USB_DeviceCh9SetConfiguration(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length)
{
uint8_t state = 0U;
(void)USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
if (((uint8_t)kUSB_DeviceStateAddress != state) && ((uint8_t)kUSB_DeviceStateConfigured != state))
{
return kStatus_USB_InvalidRequest;
}
/* The device state is changed to kUSB_DeviceStateConfigured */
state = (uint8_t)kUSB_DeviceStateConfigured;
(void)USB_DeviceSetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
if (0U == setup->wValue)
{
/* If the new configuration is zero, the device state is changed to kUSB_DeviceStateAddress */
state = (uint8_t)kUSB_DeviceStateAddress;
(void)USB_DeviceSetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
}
/* Notify the class layer the configuration is changed */
(void)USB_DeviceClassEvent(classHandle->handle, kUSB_DeviceClassEventSetConfiguration, &setup->wValue);
/* Notify the application the configuration is changed */
return USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventSetConfiguration, &setup->wValue);
}
/*!
* @brief Handle get the alternate setting of a interface request.
*
* This function is used to handle get the alternate setting of a interface request.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param setup The pointer of the setup packet.
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
* @param length It is an out parameter, the data length.
*
* @retval kStatus_USB_Success The request is handled successfully.
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
* or, the request is unsupported.
*/
static usb_status_t USB_DeviceCh9GetInterface(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length)
{
usb_status_t error = kStatus_USB_InvalidRequest;
uint8_t state = 0U;
(void)USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
if (state != (uint8_t)kUSB_DeviceStateConfigured)
{
return error;
}
*length = USB_INTERFACE_SIZE;
*buffer = (uint8_t *)&classHandle->standardTranscationBuffer;
classHandle->standardTranscationBuffer = (uint16_t)(((uint32_t)setup->wIndex & 0xFFU) << 8U);
/* The Bit[15~8] is used to save the interface index, and the alternate setting will be saved in Bit[7~0] by
* application. */
error = USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventGetInterface,
&classHandle->standardTranscationBuffer);
classHandle->standardTranscationBuffer = USB_SHORT_TO_LITTLE_ENDIAN(classHandle->standardTranscationBuffer);
return error;
}
/*!
* @brief Handle set the alternate setting of a interface request.
*
* This function is used to handle set the alternate setting of a interface request.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param setup The pointer of the setup packet.
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
* @param length It is an out parameter, the data length.
*
* @retval kStatus_USB_Success The request is handled successfully.
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
* or, the request is unsupported.
*/
static usb_status_t USB_DeviceCh9SetInterface(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length)
{
uint8_t state = 0U;
(void)USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
if (state != (uint8_t)kUSB_DeviceStateConfigured)
{
return kStatus_USB_InvalidRequest;
}
classHandle->standardTranscationBuffer = ((setup->wIndex & 0xFFU) << 8U) | (setup->wValue & 0xFFU);
/* Notify the class driver the alternate setting of the interface is changed. */
/* The Bit[15~8] is used to save the interface index, and the alternate setting is saved in Bit[7~0]. */
(void)USB_DeviceClassEvent(classHandle->handle, kUSB_DeviceClassEventSetInterface,
&classHandle->standardTranscationBuffer);
/* Notify the application the alternate setting of the interface is changed. */
/* The Bit[15~8] is used to save the interface index, and the alternate setting will is saved in Bit[7~0]. */
return USB_DeviceClassCallback(classHandle->handle, (uint32_t)kUSB_DeviceEventSetInterface,
&classHandle->standardTranscationBuffer);
}
/*!
* @brief Handle get sync frame request.
*
* This function is used to handle get sync frame request.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param setup The pointer of the setup packet.
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
* @param length It is an out parameter, the data length.
*
* @retval kStatus_USB_Success The request is handled successfully.
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
* or, the request is unsupported.
*/
static usb_status_t USB_DeviceCh9SynchFrame(usb_device_common_class_struct_t *classHandle,
usb_setup_struct_t *setup,
uint8_t **buffer,
uint32_t *length)
{
usb_status_t error = kStatus_USB_InvalidRequest;
uint8_t state = 0U;
(void)USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
if (state != (uint8_t)kUSB_DeviceStateConfigured)
{
return error;
}
classHandle->standardTranscationBuffer = USB_SHORT_FROM_LITTLE_ENDIAN(setup->wIndex);
/* Get the sync frame value */
error =
USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusSynchFrame, &classHandle->standardTranscationBuffer);
*buffer = (uint8_t *)&classHandle->standardTranscationBuffer;
*length = sizeof(classHandle->standardTranscationBuffer);
return error;
}
/*!
* @brief Send the response to the host.
*
* This function is used to send the response to the host.
*
* There are two cases this function will be called.
* Case one when a setup packet is received in control endpoint callback function:
* 1. If there is not data phase in the setup transfer, the function will prime an IN transfer with the data
* length is zero for status phase.
* 2. If there is an IN data phase, the function will prime an OUT transfer with the actual length to need to
* send for data phase. And then prime an IN transfer with the data length is zero for status phase.
* 3. If there is an OUT data phase, the function will prime an IN transfer with the actual length to want to
* receive for data phase.
*
* Case two when is not a setup packet received in control endpoint callback function:
* 1. The function will prime an IN transfer with data length is zero for status phase.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param setup The pointer of the setup packet.
* @param error The error code returned from the standard request function.
* @param stage The stage of the control transfer.
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
* @param length It is an out parameter, the data length.
*
* @return A USB error code or kStatus_USB_Success.
*/
static usb_status_t USB_DeviceControlCallbackFeedback(usb_device_handle handle,
usb_setup_struct_t *setup,
usb_status_t error,
usb_device_control_read_write_sequence_t stage,
uint8_t **buffer,
uint32_t *length)
{
usb_status_t status;
uint8_t direction = USB_IN;
if (kStatus_USB_InvalidRequest == error)
{
/* Stall the control pipe when the request is unsupported. */
if ((!((setup->bmRequestType & USB_REQUEST_TYPE_TYPE_MASK) == USB_REQUEST_TYPE_TYPE_STANDARD)) &&
((setup->bmRequestType & USB_REQUEST_TYPE_DIR_MASK) == USB_REQUEST_TYPE_DIR_OUT) &&
(0U != setup->wLength) && (kUSB_DeviceControlPipeSetupStage == stage))
{
direction = USB_OUT;
}
status = USB_DeviceStallEndpoint(
handle,
(USB_CONTROL_ENDPOINT) | (uint8_t)((uint32_t)direction << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT));
}
else
{
if (*length > setup->wLength)
{
*length = setup->wLength;
}
status = USB_DeviceSendRequest(handle, (USB_CONTROL_ENDPOINT), *buffer, *length);
if ((kStatus_USB_Success == status) &&
(USB_REQUEST_TYPE_DIR_IN == (setup->bmRequestType & USB_REQUEST_TYPE_DIR_MASK)))
{
status = USB_DeviceRecvRequest(handle, (USB_CONTROL_ENDPOINT), (uint8_t *)NULL, 0U);
}
}
return status;
}
/*!
* @brief Control endpoint callback function.
*
* This callback function is used to notify uplayer the transfser result of a transfer.
* This callback pointer is passed when a specified endpoint initialized by calling API USB_DeviceInitEndpoint.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param message The result of a transfer, includes transfer buffer, transfer length and whether is in setup
* phase for control pipe.
* @param callbackParam The parameter for this callback. It is same with
* usb_device_endpoint_callback_struct_t::callbackParam.
*
* @return A USB error code or kStatus_USB_Success.
*/
static usb_status_t USB_DeviceControlCallback(usb_device_handle handle,
usb_device_endpoint_callback_message_struct_t *message,
void *callbackParam)
{
usb_setup_struct_t *deviceSetup, *setup;
usb_device_common_class_struct_t *classHandle;
uint8_t *buffer = (uint8_t *)NULL;
uint32_t length = 0U;
void *temp;
usb_status_t status = kStatus_USB_InvalidRequest;
uint8_t state = 0U;
/* endpoint callback length is USB_CANCELLED_TRANSFER_LENGTH (0xFFFFFFFFU) when transfer is canceled */
if ((USB_CANCELLED_TRANSFER_LENGTH == message->length) || (NULL == callbackParam))
{
return status;
}
classHandle = (usb_device_common_class_struct_t *)callbackParam;
temp = (void *)&classHandle->setupBuffer[0];
deviceSetup = (usb_setup_struct_t *)temp;
(void)USB_DeviceGetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
if (0U != message->isSetup)
{
if ((USB_SETUP_PACKET_SIZE != message->length) || (NULL == message->buffer))
{
/* If a invalid setup is received, the control pipes should be de-init and init again.
* Due to the IP can not meet this require, it is reserved for feature.
*/
/*
USB_DeviceDeinitEndpoint(handle,
USB_CONTROL_ENDPOINT | (USB_IN << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT));
USB_DeviceDeinitEndpoint(handle,
USB_CONTROL_ENDPOINT | (USB_OUT << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT));
USB_DeviceControlPipeInit(handle, callbackParam);
*/
return status;
}
/* Receive a setup request */
temp = (void *)(message->buffer);
setup = (usb_setup_struct_t *)temp;
/* Copy the setup packet to the application buffer */
deviceSetup->wValue = USB_SHORT_FROM_LITTLE_ENDIAN(setup->wValue);
deviceSetup->wIndex = USB_SHORT_FROM_LITTLE_ENDIAN(setup->wIndex);
deviceSetup->wLength = USB_SHORT_FROM_LITTLE_ENDIAN(setup->wLength);
deviceSetup->bRequest = setup->bRequest;
deviceSetup->bmRequestType = setup->bmRequestType;
if ((deviceSetup->bmRequestType & USB_REQUEST_TYPE_TYPE_MASK) == USB_REQUEST_TYPE_TYPE_STANDARD)
{
/* Handle the standard request, only handle the request in request array. */
if (deviceSetup->bRequest < (sizeof(s_UsbDeviceStandardRequest) / 4U))
{
if (s_UsbDeviceStandardRequest[deviceSetup->bRequest] != (usb_standard_request_callback_t)NULL)
{
status =
s_UsbDeviceStandardRequest[deviceSetup->bRequest](classHandle, deviceSetup, &buffer, &length);
}
}
}
else
{
if ((0U != deviceSetup->wLength) &&
((deviceSetup->bmRequestType & USB_REQUEST_TYPE_DIR_MASK) == USB_REQUEST_TYPE_DIR_OUT))
{
/* Class or vendor request with the OUT data phase. */
if ((0U != deviceSetup->wLength) &&
((deviceSetup->bmRequestType & USB_REQUEST_TYPE_TYPE_CLASS) == USB_REQUEST_TYPE_TYPE_CLASS))
{
/* Get data buffer to receive the data from the host. */
usb_device_control_request_struct_t controlRequest;
controlRequest.buffer = (uint8_t *)NULL;
controlRequest.isSetup = 1U;
controlRequest.setup = deviceSetup;
controlRequest.length = deviceSetup->wLength;
status = USB_DeviceClassEvent(handle, kUSB_DeviceClassEventClassRequest, &controlRequest);
length = controlRequest.length;
buffer = controlRequest.buffer;
}
else if ((0U != deviceSetup->wLength) &&
((deviceSetup->bmRequestType & USB_REQUEST_TYPE_TYPE_VENDOR) == USB_REQUEST_TYPE_TYPE_VENDOR))
{
/* Get data buffer to receive the data from the host. */
usb_device_control_request_struct_t controlRequest;
controlRequest.buffer = (uint8_t *)NULL;
controlRequest.isSetup = 1U;
controlRequest.setup = deviceSetup;
controlRequest.length = deviceSetup->wLength;
status = USB_DeviceClassCallback(handle, (uint32_t)kUSB_DeviceEventVendorRequest, &controlRequest);
length = controlRequest.length;
buffer = controlRequest.buffer;
}
else
{
/*no action*/
}
if (kStatus_USB_Success == status)
{
/* Prime an OUT transfer */
status = USB_DeviceRecvRequest(handle, USB_CONTROL_ENDPOINT, buffer, deviceSetup->wLength);
return status;
}
}
else
{
/* Class or vendor request with the IN data phase. */
if (((deviceSetup->bmRequestType & USB_REQUEST_TYPE_TYPE_CLASS) == USB_REQUEST_TYPE_TYPE_CLASS))
{
/* Get data buffer to response the host. */
usb_device_control_request_struct_t controlRequest;
controlRequest.buffer = (uint8_t *)NULL;
controlRequest.isSetup = 1U;
controlRequest.setup = deviceSetup;
controlRequest.length = deviceSetup->wLength;
status = USB_DeviceClassEvent(handle, kUSB_DeviceClassEventClassRequest, &controlRequest);
length = controlRequest.length;
buffer = controlRequest.buffer;
}
else if (((deviceSetup->bmRequestType & USB_REQUEST_TYPE_TYPE_VENDOR) == USB_REQUEST_TYPE_TYPE_VENDOR))
{
/* Get data buffer to response the host. */
usb_device_control_request_struct_t controlRequest;
controlRequest.buffer = (uint8_t *)NULL;
controlRequest.isSetup = 1U;
controlRequest.setup = deviceSetup;
controlRequest.length = deviceSetup->wLength;
status = USB_DeviceClassCallback(handle, (uint32_t)kUSB_DeviceEventVendorRequest, &controlRequest);
length = controlRequest.length;
buffer = controlRequest.buffer;
}
else
{
/*no action*/
}
}
}
/* Send the response to the host. */
status = USB_DeviceControlCallbackFeedback(handle, deviceSetup, status, kUSB_DeviceControlPipeSetupStage,
&buffer, &length);
}
else if ((uint8_t)kUSB_DeviceStateAddressing == state)
{
/* Set the device address to controller. */
status = s_UsbDeviceStandardRequest[deviceSetup->bRequest](classHandle, deviceSetup, &buffer, &length);
}
#if ((defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U)) || \
(defined(USB_DEVICE_CONFIG_LPCIP3511HS) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))) && \
(defined(USB_DEVICE_CONFIG_USB20_TEST_MODE) && (USB_DEVICE_CONFIG_USB20_TEST_MODE > 0U))
else if ((uint8_t)kUSB_DeviceStateTestMode == state)
{
uint8_t portTestControl = (uint8_t)(deviceSetup->wIndex >> 8);
/* Set the controller.into test mode. */
status = USB_DeviceSetStatus(handle, kUSB_DeviceStatusTestMode, &portTestControl);
}
#endif
else if ((0U != message->length) && (0U != deviceSetup->wLength) &&
((deviceSetup->bmRequestType & USB_REQUEST_TYPE_DIR_MASK) == USB_REQUEST_TYPE_DIR_OUT))
{
if (((deviceSetup->bmRequestType & USB_REQUEST_TYPE_TYPE_CLASS) == USB_REQUEST_TYPE_TYPE_CLASS))
{
/* Data received in OUT phase, and notify the class driver. */
usb_device_control_request_struct_t controlRequest;
controlRequest.buffer = message->buffer;
controlRequest.isSetup = 0U;
controlRequest.setup = deviceSetup;
controlRequest.length = message->length;
status = USB_DeviceClassEvent(handle, kUSB_DeviceClassEventClassRequest, &controlRequest);
}
else if (((deviceSetup->bmRequestType & USB_REQUEST_TYPE_TYPE_VENDOR) == USB_REQUEST_TYPE_TYPE_VENDOR))
{
/* Data received in OUT phase, and notify the application. */
usb_device_control_request_struct_t controlRequest;
controlRequest.buffer = message->buffer;
controlRequest.isSetup = 0U;
controlRequest.setup = deviceSetup;
controlRequest.length = message->length;
status = USB_DeviceClassCallback(handle, (uint32_t)kUSB_DeviceEventVendorRequest, &controlRequest);
}
else
{
/*no action*/
}
/* Send the response to the host. */
status = USB_DeviceControlCallbackFeedback(handle, deviceSetup, status, kUSB_DeviceControlPipeDataStage,
&buffer, &length);
}
else
{
/*no action*/
}
return status;
}
/*!
* @brief Control endpoint initialization function.
*
* This callback function is used to initialize the control pipes.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param param The up layer handle.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceControlPipeInit(usb_device_handle handle, void *param)
{
usb_device_endpoint_init_struct_t epInitStruct;
usb_device_endpoint_callback_struct_t epCallback;
usb_status_t status;
epCallback.callbackFn = USB_DeviceControlCallback;
epCallback.callbackParam = param;
epInitStruct.zlt = 1U;
epInitStruct.transferType = USB_ENDPOINT_CONTROL;
epInitStruct.interval = 0;
epInitStruct.endpointAddress = USB_CONTROL_ENDPOINT | (USB_IN << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT);
epInitStruct.maxPacketSize = USB_CONTROL_MAX_PACKET_SIZE;
/* Initialize the control IN pipe */
status = USB_DeviceInitEndpoint(handle, &epInitStruct, &epCallback);
if (kStatus_USB_Success != status)
{
return status;
}
epInitStruct.endpointAddress = USB_CONTROL_ENDPOINT | (USB_OUT << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT);
/* Initialize the control OUT pipe */
status = USB_DeviceInitEndpoint(handle, &epInitStruct, &epCallback);
if (kStatus_USB_Success != status)
{
(void)USB_DeviceDeinitEndpoint(
handle, USB_CONTROL_ENDPOINT | (USB_IN << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT));
return status;
}
return kStatus_USB_Success;
}
#endif /* USB_DEVICE_CONFIG_NUM */

View File

@@ -0,0 +1,79 @@
/*
* Copyright (c) 2015, Freescale Semiconductor, Inc.
* Copyright 2016 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef __USB_DEVICE_CH9_H__
#define __USB_DEVICE_CH9_H__
/*******************************************************************************
* Definitions
******************************************************************************/
/*!
* @addtogroup usb_device_ch9
* @{
*/
/*! @brief Defines USB device status size when the host request to get device status */
#define USB_DEVICE_STATUS_SIZE (0x02U)
/*! @brief Defines USB device interface status size when the host request to get interface status */
#define USB_INTERFACE_STATUS_SIZE (0x02U)
/*! @brief Defines USB device endpoint status size when the host request to get endpoint status */
#define USB_ENDPOINT_STATUS_SIZE (0x02U)
/*! @brief Defines USB device configuration size when the host request to get current configuration */
#define USB_CONFIGURE_SIZE (0X01U)
/*! @brief Defines USB device interface alternate setting size when the host request to get interface alternate setting
*/
#define USB_INTERFACE_SIZE (0X01U)
/*! @brief Defines USB device status mask */
#define USB_GET_STATUS_DEVICE_MASK (0x03U)
/*! @brief Defines USB device interface status mask */
#define USB_GET_STATUS_INTERFACE_MASK (0x03U)
/*! @brief Defines USB device endpoint status mask */
#define USB_GET_STATUS_ENDPOINT_MASK (0x03U)
/*! @brief Control read and write sequence */
typedef enum _usb_device_control_read_write_sequence
{
kUSB_DeviceControlPipeSetupStage = 0U, /*!< Setup stage */
kUSB_DeviceControlPipeDataStage, /*!< Data stage */
kUSB_DeviceControlPipeStatusStage, /*!< status stage */
} usb_device_control_read_write_sequence_t;
#if defined(__cplusplus)
extern "C" {
#endif
/*******************************************************************************
* API
******************************************************************************/
/*!
* @brief Initializes the control pipes.
*
* The function is used to initialize the control pipes. This function should be called when event
* kUSB_DeviceEventBusReset is received.
*
* @param[in] handle The device handle.
* @param[in] param The event parameter.
*
* @return A USB error code or kStatus_USB_Success.
*/
extern usb_status_t USB_DeviceControlPipeInit(usb_device_handle handle, void *param);
#if defined(__cplusplus)
}
#endif
/*! @}*/
#endif /* __USB_DEVICE_CH9_H__ */

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,170 @@
/*
* Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
* Copyright 2016 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef __USB_DEVICE_DCI_H__
#define __USB_DEVICE_DCI_H__
/*!
* @addtogroup usb_device_controller_driver
* @{
*/
/*******************************************************************************
* Definitions
******************************************************************************/
/*! @brief Macro to define controller handle */
#define usb_device_controller_handle usb_device_handle
#define USB_DEVICE_MESSAGES_SIZE \
(sizeof(uint32_t) * (1U + (sizeof(usb_device_callback_message_struct_t) - 1U) / sizeof(uint32_t)))
/*! @brief Available notify types for device notification */
typedef enum _usb_device_notification
{
kUSB_DeviceNotifyBusReset = 0x10U, /*!< Reset signal detected */
kUSB_DeviceNotifySuspend, /*!< Suspend signal detected */
kUSB_DeviceNotifyResume, /*!< Resume signal detected */
kUSB_DeviceNotifyLPMSleep, /*!< LPM signal detected */
kUSB_DeviceNotifyLPMResume, /*!< Resume signal detected */
kUSB_DeviceNotifyError, /*!< Errors happened in bus */
kUSB_DeviceNotifyDetach, /*!< Device disconnected from a host */
kUSB_DeviceNotifyAttach, /*!< Device connected to a host */
#if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U))
kUSB_DeviceNotifyDcdDetectFinished, /*!< Device charger detection finished */
#endif
} usb_device_notification_t;
/*! @brief Device notification message structure */
typedef struct _usb_device_callback_message_struct
{
uint8_t *buffer; /*!< Transferred buffer */
uint32_t length; /*!< Transferred data length */
uint8_t code; /*!< Notification code */
uint8_t isSetup; /*!< Is in a setup phase */
} usb_device_callback_message_struct_t;
/*! @brief Control type for controller */
typedef enum _usb_device_control_type
{
kUSB_DeviceControlRun = 0U, /*!< Enable the device functionality */
kUSB_DeviceControlStop, /*!< Disable the device functionality */
kUSB_DeviceControlEndpointInit, /*!< Initialize a specified endpoint */
kUSB_DeviceControlEndpointDeinit, /*!< De-initialize a specified endpoint */
kUSB_DeviceControlEndpointStall, /*!< Stall a specified endpoint */
kUSB_DeviceControlEndpointUnstall, /*!< Un-stall a specified endpoint */
kUSB_DeviceControlGetDeviceStatus, /*!< Get device status */
kUSB_DeviceControlGetEndpointStatus, /*!< Get endpoint status */
kUSB_DeviceControlSetDeviceAddress, /*!< Set device address */
kUSB_DeviceControlGetSynchFrame, /*!< Get current frame */
kUSB_DeviceControlResume, /*!< Drive controller to generate a resume signal in USB bus */
kUSB_DeviceControlSleepResume, /*!< Drive controller to generate a LPM resume signal in USB bus */
kUSB_DeviceControlSuspend, /*!< Drive controller to enter into suspend mode */
kUSB_DeviceControlSleep, /*!< Drive controller to enter into sleep mode */
kUSB_DeviceControlSetDefaultStatus, /*!< Set controller to default status */
kUSB_DeviceControlGetSpeed, /*!< Get current speed */
kUSB_DeviceControlGetOtgStatus, /*!< Get OTG status */
kUSB_DeviceControlSetOtgStatus, /*!< Set OTG status */
kUSB_DeviceControlSetTestMode, /*!< Drive xCHI into test mode */
kUSB_DeviceControlGetRemoteWakeUp, /*!< Get flag of LPM Remote Wake-up Enabled by USB host. */
#if (defined(USB_DEVICE_CONFIG_CHARGER_DETECT) && (USB_DEVICE_CONFIG_CHARGER_DETECT > 0U))
kUSB_DeviceControlDcdDisable, /*!< disable dcd module function. */
kUSB_DeviceControlDcdEnable, /*!< enable dcd module function. */
#endif
kUSB_DeviceControlPreSetDeviceAddress, /*!< Pre set device address */
kUSB_DeviceControlUpdateHwTick, /*!< update hardware tick */
#if defined(USB_DEVICE_CONFIG_GET_SOF_COUNT) && (USB_DEVICE_CONFIG_GET_SOF_COUNT > 0U)
kUSB_DeviceControlGetCurrentFrameCount, /*!< Get current frame count */
#endif
} usb_device_control_type_t;
/*! @brief USB device controller initialization function typedef */
typedef usb_status_t (*usb_device_controller_init_t)(uint8_t controllerId,
usb_device_handle handle,
usb_device_controller_handle *controllerHandle);
/*! @brief USB device controller de-initialization function typedef */
typedef usb_status_t (*usb_device_controller_deinit_t)(usb_device_controller_handle controllerHandle);
/*! @brief USB device controller send data function typedef */
typedef usb_status_t (*usb_device_controller_send_t)(usb_device_controller_handle controllerHandle,
uint8_t endpointAddress,
uint8_t *buffer,
uint32_t length);
/*! @brief USB device controller receive data function typedef */
typedef usb_status_t (*usb_device_controller_recv_t)(usb_device_controller_handle controllerHandle,
uint8_t endpointAddress,
uint8_t *buffer,
uint32_t length);
/*! @brief USB device controller cancel transfer function in a specified endpoint typedef */
typedef usb_status_t (*usb_device_controller_cancel_t)(usb_device_controller_handle controllerHandle,
uint8_t endpointAddress);
/*! @brief USB device controller control function typedef */
typedef usb_status_t (*usb_device_controller_control_t)(usb_device_controller_handle controllerHandle,
usb_device_control_type_t command,
void *param);
/*! @brief USB device controller interface structure */
typedef struct _usb_device_controller_interface_struct
{
usb_device_controller_init_t deviceInit; /*!< Controller initialization */
usb_device_controller_deinit_t deviceDeinit; /*!< Controller de-initialization */
usb_device_controller_send_t deviceSend; /*!< Controller send data */
usb_device_controller_recv_t deviceRecv; /*!< Controller receive data */
usb_device_controller_cancel_t deviceCancel; /*!< Controller cancel transfer */
usb_device_controller_control_t deviceControl; /*!< Controller control */
} usb_device_controller_interface_struct_t;
/*! @brief USB device status structure */
typedef struct _usb_device_struct
{
#if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U)) || \
(defined(FSL_FEATURE_SOC_USB_ANALOG_COUNT) && (FSL_FEATURE_SOC_USB_ANALOG_COUNT > 0U))
volatile uint64_t hwTick; /*!< Current hw tick(ms)*/
#endif
usb_device_controller_handle controllerHandle; /*!< Controller handle */
const usb_device_controller_interface_struct_t *controllerInterface; /*!< Controller interface handle */
#if USB_DEVICE_CONFIG_USE_TASK
OSA_MSGQ_HANDLE_DEFINE(notificationQueueBuffer,
USB_DEVICE_CONFIG_MAX_MESSAGES,
USB_DEVICE_MESSAGES_SIZE); /*!< Message queue buffer*/
osa_msgq_handle_t notificationQueue; /*!< Message queue*/
#endif
usb_device_callback_t deviceCallback; /*!< Device callback function pointer */
usb_device_endpoint_callback_struct_t
epCallback[USB_DEVICE_CONFIG_ENDPOINTS << 1U]; /*!< Endpoint callback function structure */
uint8_t deviceAddress; /*!< Current device address */
uint8_t controllerId; /*!< Controller ID */
uint8_t state; /*!< Current device state */
#if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U))
uint8_t remotewakeup; /*!< Remote wakeup is enabled or not */
#endif
uint8_t isResetting; /*!< Is doing device reset or not */
#if (defined(USB_DEVICE_CONFIG_USE_TASK) && (USB_DEVICE_CONFIG_USE_TASK > 0U))
uint8_t epCallbackDirectly; /*!< Whether call ep callback directly when the task is enabled */
#endif
} usb_device_struct_t;
/*******************************************************************************
* API
******************************************************************************/
/*!
* @brief Notify the device that the controller status changed.
*
* This function is used to notify the device that the controller status changed.
*
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
* @param message The device callback message handle.
*
* @return A USB error code or kStatus_USB_Success.
*/
usb_status_t USB_DeviceNotificationTrigger(void *handle, void *msg);
/*! @}*/
#endif /* __USB_DEVICE_DCI_H__ */