urb.c

来自「usb2 driver for vxwokrs」· C语言 代码 · 共 2,135 行 · 第 1/5 页

C
2,135
字号
        OS_LOG_MESSAGE_HIGH(            USBD,            "usbHstSetConfiguration() Failed: INSUFFICIENT_RESOURCE.\n",            0,            0,            0,            0);        /* Free the setup packet memory area allocated */        OS_FREE(pSetup);        return USBHST_INSUFFICIENT_RESOURCE;    }    /* Fill the USB Request block memory area with zero */    OS_MEMSET(pUrb, 0, sizeof(USBHST_URB));    /* Create transfer completion event for making a blocking call */    EventId = OS_CREATE_EVENT(OS_EVENT_NON_SIGNALED);    /* Check if the eventid created is valid */    if (OS_INVALID_EVENT_ID == EventId)    {        OS_LOG_MESSAGE_HIGH(            USBD,            "usbHstSetConfiguration() Failed: INSUFFICIENT_RESOURCE.\n",            0,            0,            0,            0);        /* Free the setup packet memory area allocated */        OS_FREE(pSetup);        /* Free the USB request packet memory area allocated */        OS_FREE(pUrb);        return USBHST_INSUFFICIENT_RESOURCE;    }    /* Wait for the bus operation event */    OS_WAIT_FOR_EVENT(pUsbBusInfo->busOperationLock, OS_WAIT_INFINITE);    if (USBHST_DEFAULT_CONFIGURATION_INDEX != uIndex)    {        /* Update uSize with size of config descriptor */        uSize = sizeof(USBHST_CONFIG_DESCRIPTOR);        /* Get the 9 bytes of new configuration descriptor */        nReturnStatus = usbHstGetDescriptor(hDevice,                                             USBHST_CONFIG_DESC,                                             uIndex,                                             0,                                             &uSize,                                             (PUCHAR)&ConfigDesc);        /* Check the status returned */        if (USBHST_SUCCESS != nReturnStatus)        {            OS_LOG_MESSAGE_HIGH(                USBD,                "usbHstSetConfiguration() Failed: USBHST_GetDescriptor\                    failed .\n",                0,                0,                0,                0);            /* Free the setup packet memory area allocated */            OS_FREE(pSetup);            /* Free the USB request packet memory area allocated */            OS_FREE(pUrb);            /* Destroy the event */            OS_DESTROY_EVENT(EventId);            /* Release the Bus operation event */            OS_RELEASE_EVENT(pUsbBusInfo->busOperationLock);            return nReturnStatus;        }        /* Convert the buffer to CPU endian */        OS_BUFFER_LE_TO_CPU(&ConfigDesc,uSize);        /* Update the uSize with CPU endian conversion */        uSize = OS_UINT16_LE_TO_CPU(ConfigDesc.wTotalLength);        /* Allocate the memory area to the buffer of uSize */        pConfigBuffer = OS_MALLOC(uSize);        if (NULL == pConfigBuffer)        {            OS_LOG_MESSAGE_HIGH(                USBD,                "usbHstSetConfiguration() Failed: INSUFFICIENT_MEMORY.\n",                0,                0,                0,                0);            /* Free the setup packet memory area allocated */            OS_FREE(pSetup);            /* Free the USB request packet memory area allocated */            OS_FREE(pUrb);            /* Destroy the event */            OS_DESTROY_EVENT(EventId);            /* Release the Bus operation event */            OS_RELEASE_EVENT(pUsbBusInfo->busOperationLock);            return USBHST_INSUFFICIENT_MEMORY;        }        /* Set the Config buffer memory area to zero*/        OS_MEMSET(pConfigBuffer,0,uSize);        /* Get the whole configuration descriptor */        nReturnStatus = usbHstGetDescriptor(hDevice,                                             USBHST_CONFIG_DESC,                                             uIndex,                                             0,                                             &uSize,                                             pConfigBuffer);        if (USBHST_SUCCESS != nReturnStatus)        {            OS_LOG_MESSAGE_HIGH(                USBD,                "USBHST_SetConfiguration() Failed: USBHST_GetDescriptor \                    failed .\n",                0,                0,                0,                0);            /* Free the setup packet memory area allocated */            OS_FREE(pSetup);            /* Free the USB request packet memory area allocated */            OS_FREE(pUrb);            /* Destroy the event */            OS_DESTROY_EVENT(EventId);            /* Free the Config buffer memory area */            OS_FREE(pConfigBuffer);            /* Release the Bus operation event */            OS_RELEASE_EVENT(pUsbBusInfo->busOperationLock);            return nReturnStatus;        }        /*         * Compute the CRC vale for the new configuration and check with the         * old configuration CRC vale         */        /*         * Convert the buffer into CPU endian dont convert the length         * since that will be taken care by USBD_ParseConfiguration()         */        OS_BUFFER_LE_TO_CPU(pConfigBuffer,uSize);        /* Compute the CRC value for the new configuration */        uNewConfigCRCValue = usbdComputeCRC(pConfigBuffer,uSize);        /* Check if the new configuration is same as old configuration */        if (uNewConfigCRCValue == pDeviceInfo->uConfigCRCValue)        {            OS_LOG_MESSAGE_LOW(USBD,                               "Exiting usbHstSetConfiguration()",                               0,                               0,                               0,                               0);            /* Free the setup packet memory area allocated */            OS_FREE(pSetup);            /* Free the USB request packet memory area allocated */            OS_FREE(pUrb);            /* Destroy the event */            OS_DESTROY_EVENT(EventId);            /* Free the Config buffer memory area */            OS_FREE(pConfigBuffer);            /* Release the Bus operation event */            OS_RELEASE_EVENT(pUsbBusInfo->busOperationLock);            return USBHST_SUCCESS;        }        /* Check if bandwidth of new configuration is avilable */        nReturnStatus =            pHCDriverInfo->isBandwidthAvailable(                pUsbBusInfo->uRelativeBusIndex,                pDeviceInfo->uDeviceAddress,                pDeviceInfo->uDeviceSpeed,                pDeviceInfo->pCurrentConfiguration,                pConfigBuffer);        /* Check the status returned */        if (USBHST_SUCCESS != nReturnStatus)        {            OS_LOG_MESSAGE(                USBD,                "usbHstSetConfiguration() Failed:INSUFFICIENT_BANDWIDTH.\n",                0,                0);            /* Free the setup packet memory area allocated */            OS_FREE(pSetup);            /* Free the USB request packet memory area allocated */            OS_FREE(pUrb);            /* Destroy the event */            OS_DESTROY_EVENT(EventId);            /* Free the Config buffer memory area */            OS_FREE(pConfigBuffer);            /* Release the Bus operation event */            OS_RELEASE_EVENT(pUsbBusInfo->busOperationLock);            return USBHST_INSUFFICIENT_BANDWIDTH;        }        /*         * Check if hub can support the power required by device in the         * new configuration         */        nReturnStatus =            gpHubFunctionList->checkForPower(                hDevice,                ConfigDesc.MaxPower);        if (USBHST_SUCCESS != nReturnStatus)        {            OS_LOG_MESSAGE(                USBD,                "usbHstSetConfiguration() Failed: INSUFFICIENT_RESOURCE.\n",                0,                0);            /* Free the setup packet memory area allocated */            OS_FREE(pSetup);            /* Free the USB request packet memory area allocated */            OS_FREE(pUrb);            /* Destroy the event */            OS_DESTROY_EVENT(EventId);            /* Free the Config buffer memory area */            OS_FREE(pConfigBuffer);            /* Release the Bus operation event */            OS_RELEASE_EVENT(pUsbBusInfo->busOperationLock);            return USBHST_INSUFFICIENT_RESOURCE;        }    } /* End of if index is not equal to default index value */    if (USBHST_DEFAULT_CONFIGURATION_INDEX == uIndex)    {        ConfigDesc.bConfigurationValue = 0;    }    /* Fill the SETUP_PACKET with the information passed */    USBHST_FILL_SETUP_PACKET(pSetup,                             0,                             USBHST_REQ_SET_CONFIGURATION,                             ConfigDesc.bConfigurationValue,                             0,                             0);    /* Convert the setup packet from CPU format to little endian format */    OS_BUFFER_CPU_TO_LE(pSetup, sizeof(USBHST_SETUP_PACKET));    /* Populate the pUrb structure */    USBHST_FILL_CONTROL_URB(pUrb,                            hDevice,                            0,                            NULL,                            0,                            USBHST_SHORT_TRANSFER_OK,                            pSetup,                            usbHstCompletionCallback,                            EventId,                            USBHST_FAILURE);    /* Submit the URB on default control pipe */    /* Call the HCD submitURB function to submit the URB. */    nReturnStatus = pHCDriverInfo->submitURB(pUsbBusInfo->uRelativeBusIndex,                                               pDeviceInfo->hDefaultPipe,                                               pUrb);    if (USBHST_SUCCESS != nReturnStatus)    {        OS_LOG_MESSAGE_HIGH(            USBD,            "USBHST_SetConfiguration() Failed: HCD_SubmitURB failed.\n",            0,            0,            0,            0);        /* Free the setup packet memory area allocated */        OS_FREE(pSetup);        /* Free the USB request packet memory area allocated */        OS_FREE(pUrb);        /* Destroy the event */        OS_DESTROY_EVENT(EventId);        /*         * Free the Config buffer memory area only if it was allocated i.e         * if (USBHST_DEFAULT_CONFIGURATION_INDEX != uIndex)         */        if (USBHST_DEFAULT_CONFIGURATION_INDEX != uIndex)        {            OS_FREE(pConfigBuffer);        }        /* Release the Bus operation event */        OS_RELEASE_EVENT(pUsbBusInfo->busOperationLock);        return nReturnStatus;    }    /* Wait for the completion of the event */    if (OS_WAIT_TIMEOUT == OS_WAIT_FOR_EVENT(EventId, OS_WAIT_INFINITE))    {        OS_LOG_MESSAGE_HIGH(            USBD,            "USBHST_SetConfiguration() Failed:OS_WAIT_TIMEOUT  .\n",            0,            0,            0,            0);        /* Call the HCD_CancelURB function to cancel the URB. */        pHCDriverInfo->cancelURB(pUsbBusInfo->uRelativeBusIndex,                                   pDeviceInfo->hDefaultPipe,                                   pUrb);        /* Free the setup packet memory area allocated */        OS_FREE(pSetup);        /* Free the USB request packet memory area allocated */        OS_FREE(pUrb);        /* Destroy the event */        OS_DESTROY_EVENT(EventId);        /*         * Free the Config buffer memory area only if it was allocated i.e         * if (USBHST_DEFAULT_CONFIGURATION_INDEX != uIndex)         */        if (USBHST_DEFAULT_CONFIGURATION_INDEX != uIndex)        {            OS_FREE(pConfigBuffer);        }        /* Release the Bus operation event */        OS_RELEASE_EVENT(pUsbBusInfo->busOperationLock);        return USBHST_TIMEOUT;    }    /* if request is completed successfully */    if (USBHST_SUCCESS == pUrb->nStatus)    {        /* check that interfaces info is not NULL */        if (NULL != pDeviceInfo->pInterfacesInfo)        {            /* delete all the endpoint pipes for the current configuration */            for (uInterfaceCount = 0;                 uInterfaceCount < pDeviceInfo->uInterfaceCount;                 uInterfaceCount++)            {                /* Get the interface info for the uInterfaceCount interface */                pTempInterfaceInfo = &pDeviceInfo->pInterfacesInfo[uInterfaceCount];                /* check that endpoint info is not NULL */                if (NULL != pTempInterfaceInfo->pEndpointInformation)                {                    /*                     * For all the endpoints in this interface get pipe                     * and delete them                     */                    for (uEndpointCount = 0;                         uEndpointCount < pTempInterfaceInfo->uNumEndpoints;                         uEndpointCount++)                    {                        /* Get the endpoint info */                        pTempEndpointInfo =                            &pTempInterfaceInfo->pEndpointInformation[uEndpointCount];                        /* If the pipe is valid then delete it */                        if (0 != pTempEndpointInfo->hPipe)                        {                            pHCDriverInfo->deletePipe(                                pUsbBusInfo->uRelativeBusIndex,                                pTempEndpointInfo->hPipe);                        }                    } /* End of FOR loop for the endpoints */                    /* Free the memory allocated for endpoints */                    /*OS_FREE(InterfaceInfo.pEndpointInformation);*/                    OS_FREE(                    pDeviceInfo->pInterfacesInfo[uInterfaceCount].pEndpointInformation);                    /*                     * mark the endpoint inforamtion as NULL.                     * Note: we have to mark the pointer in the device info as NULL!!                     */                    pDeviceInfo->pInterfacesInfo[uInterfaceCount].pEndpointInformation                    = NULL;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?