/*!
 * @brief Allocate a device common class handle.
 *
 * This function allocates a a device common class handle.
 *
 * @param controllerId   The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
 * @param handle          It is out parameter, is used to return pointer of the device common class handle to the
 * caller.
 *
 * @retval kStatus_USB_Success              Get a device handle successfully.
 * @retval kStatus_USB_Busy                 Cannot allocate a common class handle.
 * @retval kStatus_USB_Error                The common class has been initialized.
 */
static usb_status_t USB_DeviceClassAllocateHandle(uint8_t controllerId, usb_device_common_class_struct_t **handle)
{
    int32_t count;
    USB_OSA_SR_ALLOC();

    USB_OSA_ENTER_CRITICAL();
    /* Check the controller is initialized or not. */
    for (count = 0U; count < USB_DEVICE_CONFIG_NUM; count++)
    {
        if ((NULL != s_UsbDeviceCommonClassStruct[count].handle) &&
            (controllerId == s_UsbDeviceCommonClassStruct[count].controllerId))
        {
            USB_OSA_EXIT_CRITICAL();
            return kStatus_USB_Error;
        }
    }
    /* Get a free common class handle. */
    for (count = 0U; count < USB_DEVICE_CONFIG_NUM; count++)
    {
        if (NULL == s_UsbDeviceCommonClassStruct[count].handle)
        {
            s_UsbDeviceCommonClassStruct[count].controllerId = controllerId;
            *handle = &s_UsbDeviceCommonClassStruct[count];
            USB_OSA_EXIT_CRITICAL();
            return kStatus_USB_Success;
        }
    }

    USB_OSA_EXIT_CRITICAL();
    return kStatus_USB_Busy;
}
Beispiel #2
0
static void USB_HostReleaseInstance(usb_host_instance_t *hostInstance)
{
    USB_OSA_SR_ALLOC();
    USB_OSA_ENTER_CRITICAL();
    hostInstance->occupied = 0;
    USB_OSA_EXIT_CRITICAL();
}
Beispiel #3
0
static usb_host_instance_t *USB_HostGetInstance(void)
{
    uint8_t i = 0;
    uint32_t index = 0;
    USB_OSA_SR_ALLOC();
    USB_OSA_ENTER_CRITICAL();
    for (; i < USB_HOST_CONFIG_MAX_HOST; i++)
    {
        if (g_UsbHostInstance[i].occupied != 1)
        {
            uint8_t *buffer = (uint8_t *)&g_UsbHostInstance[i];
            for (uint32_t j = 0U; j < sizeof(usb_host_instance_t); j++)
            {
                buffer[j] = 0x00U;
            }
            g_UsbHostInstance[i].occupied = 1;
            USB_OSA_EXIT_CRITICAL();
            for (index = 0; index < USB_HOST_CONFIG_MAX_TRANSFERS; ++index)
            {
                g_UsbHostInstance[i].transferList[index].setupPacket =
                    (usb_setup_struct_t *)&(s_Setupbuffer[i][index][0]);
            }
            return &g_UsbHostInstance[i];
        }
    }
    USB_OSA_EXIT_CRITICAL();
    return NULL;
}
/*!
 * @brief Get the device handle according to the controller id.
 *
 * This function gets the device handle according to the controller id.
 *
 * @param controllerId   The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
 * @param handle          It is out parameter, is used to return pointer of the device handle to the caller.
 *
 * @retval kStatus_USB_Success              Free device hanlde successfully.
 * @retval kStatus_USB_InvalidParameter     The device handle not be found.
 */
usb_status_t USB_DeviceClassGetDeviceHandle(uint8_t controllerId, usb_device_handle *handle)
{
    int32_t count = 0U;
    USB_OSA_SR_ALLOC();

    USB_OSA_ENTER_CRITICAL();
    for (; count < USB_DEVICE_CONFIG_NUM; count++)
    {
        if ((NULL != s_UsbDeviceCommonClassStruct[count].handle) &&
            (controllerId == s_UsbDeviceCommonClassStruct[count].controllerId))
        {
            *handle = s_UsbDeviceCommonClassStruct[count].handle;
            USB_OSA_EXIT_CRITICAL();
            return kStatus_USB_Success;
        }
    }
    USB_OSA_EXIT_CRITICAL();
    return kStatus_USB_InvalidParameter;
}
/*!
 * @brief Get the device common class handle according to the device handle.
 *
 * This function gets the device common class handle according to the device handle.
 *
 * @param deviceHandle          The device handle, got from the USB_DeviceInit.
 * @param handle                 It is out parameter, is used to return pointer of the device common class handle to the
 * caller.
 *
 * @retval kStatus_USB_Success              Free device hanlde successfully.
 * @retval kStatus_USB_InvalidParameter     The common class can not be found.
 */
static usb_status_t USB_DeviceClassGetHandleByDeviceHandle(usb_device_handle deviceHandle,
                                                           usb_device_common_class_struct_t **handle)
{
    int32_t count = 0U;
    USB_OSA_SR_ALLOC();

    USB_OSA_ENTER_CRITICAL();
    for (; count < USB_DEVICE_CONFIG_NUM; count++)
    {
        if (deviceHandle == s_UsbDeviceCommonClassStruct[count].handle)
        {
            *handle = &s_UsbDeviceCommonClassStruct[count];
            USB_OSA_EXIT_CRITICAL();
            return kStatus_USB_Success;
        }
    }
    USB_OSA_EXIT_CRITICAL();
    return kStatus_USB_InvalidParameter;
}
/*!
 * @brief Free a device common class handle.
 *
 * This function frees a device common class handle.
 *
 * @param controllerId   The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
 *
 * @retval kStatus_USB_Success              Free device hanlde successfully.
 * @retval kStatus_USB_InvalidParameter     The common class can not be found.
 */
static usb_status_t USB_DeviceClassFreeHandle(uint8_t controllerId)
{
    int32_t count = 0U;
    USB_OSA_SR_ALLOC();

    USB_OSA_ENTER_CRITICAL();
    for (; count < USB_DEVICE_CONFIG_NUM; count++)
    {
        if ((NULL != s_UsbDeviceCommonClassStruct[count].handle) &&
            (controllerId == s_UsbDeviceCommonClassStruct[count].controllerId))
        {
            s_UsbDeviceCommonClassStruct[count].handle = NULL;
            s_UsbDeviceCommonClassStruct[count].configList = (usb_device_class_config_list_struct_t *)NULL;
            s_UsbDeviceCommonClassStruct[count].controllerId = 0U;
            USB_OSA_EXIT_CRITICAL();
            return kStatus_USB_Success;
        }
    }
    USB_OSA_EXIT_CRITICAL();

    return kStatus_USB_InvalidParameter;
}