⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbhubutility.c

📁 风河的vxworks-6.3 FOR amcc440epx BSP!
💻 C
📖 第 1 页 / 共 5 页
字号:

        OS_FREE (pDeviceDescriptor);

        OS_FREE (pConfigDescriptor);

        return Result;
        
    }/* End of if ( USBHST_SUCCESS !=Result) */

    /*
     * Parse the configuration descriptor and find the complete length of the
     * descriptor.
     */
    uLength=USB_HUB_CONFIG_wTotalLength(pConfigDescriptor);

    /* Call OS_FREE to free the Buffer.*/
    OS_FREE(pConfigDescriptor);

   /*
    * Call OS_MALLOC() to allocate configuration buffer for the complete length
    * of the descriptor. If call failed, return USBHST_INSUFFICIENT_MEMORY.
    */
    pConfigDescriptor = (UINT8 *) OS_MALLOC(uLength*sizeof(UINT8));

    /* Check for call failure */
    if (NULL == pConfigDescriptor)
    {
        /* Debug Message */
        OS_LOG_MESSAGE_HIGH(
            HUB,
            "usbHubConfigure:INSUFF MEMORY-pConfigdesc %d\n",
            uLength*sizeof(UINT8),
            0,
            0,
            0);
            
            OS_FREE (pDeviceDescriptor);

        return USBHST_INSUFFICIENT_MEMORY;

    }/* End of if (NULL == pConfigDescriptor ) */

    /* Clear the allocated buffer */
    OS_MEMSET(pConfigDescriptor,0,sizeof(UINT8)*uLength);

    /* 
     * Call HUB_GetDescriptor() for uDeviceHandle, CONFIGURATION_DESCRIPTOR and
     * for complete length. If call fails,
     * i.    Call OS_FREE() to free the configuration buffer.
     * ii.    Return USBHST_FAILURE.
     */
    Result= usbHstGetDescriptor(    uDeviceHandle, /* Device Handle */
                                     USBHST_CONFIG_DESC, /* Desc type */
                                     /* descriptor index */
                                     USB_DEF_ACTIVE_HUB_CONFIG_INDEX,
                                     0 , /* language ID */
                                     &uLength , /* Size of the descriptor */
                                     /* buffer to copy to */
                                     (UINT8 *)pConfigDescriptor );

    /* check  the  result and lenth of descriptor*/
    if (( USBHST_SUCCESS != Result)||
        (uLength != (UINT32)USB_HUB_CONFIG_wTotalLength(pConfigDescriptor)) )
    {
        /* Debug Message */
        OS_LOG_MESSAGE_HIGH(
            HUB,
            "usbHubConfigure:getconfdesc fail R=0x%x L=%d\n",
            Result,
            uLength,
            0,
            0);/* End of if (( USBHST_SUCCESS != Result.......*/
            
        /* Free the memory allocated */
        OS_FREE (pDeviceDescriptor);

        /* Free the memory allocated */
        OS_FREE (pConfigDescriptor);
        return Result;

    }/* End of if ( USBHST_SUCCESS !=Result) */

     /* pointer for parsing the desc */
     pParseDesc = pConfigDescriptor;

     /*
     * In this loop we parse the config desc and also keep track of the sequence
     * in which the desriptors arrive
     * 1) If Default Interface Descriptor then update uDescriptorSequence as
     *    value one left shifted by (Four * ucount)
     * 2) If Alternate Interface Descriptor then update uDescriptorSequence as
     *    value Four left shifted by (Four * ucount)
     * 3) If End Point Descriptor then update uDescriptorSequence as
     *    value two left shifted by (Four * ucount)
     */
     while(uCount < 3)
	 {

        /* this switch  selects the descriptor type  */
        switch (pParseDesc[1])
		{
         /* this is a interface descriptor */
         case USBHST_INTERFACE_DESC :

           /*validate the endpoint descriptor */
           Result = usbHubValidateDescriptor
                        (
                        pParseDesc,
                        pParseDeviceDesc->bcdUSB,
                        USBHST_INTERFACE_DESC
                        );

      	   if(USBHST_SUCCESS != Result)
	  	        {

                /* Debug Message */
                OS_LOG_MESSAGE_HIGH(
                  HUB,"usbHubConfigure:ValidateInterfaceDescriptor fail \n",0,0,0,0);

                /* Free the memory allocated */
                OS_FREE (pDeviceDescriptor);

                /* Free the memory allocated */
                OS_FREE (pConfigDescriptor);

                return USBHST_INVALID_PARAMETER ;

				}/* end of if(USBHST_SUCCESS != Result) */

				/* this is valid interface descriptor */

                /* this is default interface descriptor */
                if(pParseDesc[3] == 0 )
				    {
                     pParseInterfaceDesc = (pUSBHST_INTERFACE_DESCRIPTOR)pParseDesc;
                     uDescriptorSequence = (0x0001<<(4 * uCount)) | uDescriptorSequence ;
				    }/* end of if(pParseDesc[3]......)*/

                /* this is Alternate interface descriptor */
                else
				{
                     pParseAlternateInterfaceDesc = (pUSBHST_INTERFACE_DESCRIPTOR)pParseDesc;
                     uDescriptorSequence = (0x0004<<(4 * uCount)) | uDescriptorSequence ;
				} /* end of else */

         break;

         /* this is a endpoint descriptor */
         case USBHST_ENDPOINT_DESC :

             /*validate the endpoint descriptor */
             Result = usbHubValidateDescriptor
                        (
                        pParseDesc,
                        pParseDeviceDesc->bcdUSB,
                        USBHST_ENDPOINT_DESC
                        );

    	     if(USBHST_SUCCESS != Result)
	      	    {

                /* Debug Message */
                OS_LOG_MESSAGE_HIGH(
                  HUB,"usbHubConfigure:ValidateEndpointDescriptor fail \n",0,0,0,0);

                /* Free the memory allocated */
                OS_FREE (pDeviceDescriptor);

                /* Free the memory allocated */
                OS_FREE (pConfigDescriptor);

                return USBHST_INVALID_PARAMETER ;

				}/* end of if(USBHST_SUCCESS != Result) */

				/* This is a valid endpoint descriptor */
		        pParseEndpointDesc = (pUSBHST_ENDPOINT_DESCRIPTOR) pParseDesc;
                uDescriptorSequence  = 
                                (0x0002<<(4 * uCount)) | uDescriptorSequence ;

            break;

        /* this is a configuration descriptor */
        case USBHST_CONFIG_DESC :
         /* validate the configuration descriptor */
	    Result = usbHubValidateDescriptor
                (
                pParseDesc,
                pParseDeviceDesc->bcdUSB,
                USBHST_CONFIG_DESC
                );

            if(USBHST_SUCCESS != Result)

		        {

                /* Debug Message */
                OS_LOG_MESSAGE_HIGH(
                   HUB,"usbHubConfigure:ValidateConfigurationDescriptor fail \n",0,0,0,0);

                /* Free the memory allocated */
                OS_FREE (pDeviceDescriptor);

                /* Free the memory allocated */
                OS_FREE (pConfigDescriptor);

                return USBHST_INVALID_PARAMETER ;

			}/* end of if(USBHST_SUCCESS != Result) */

             /* This is a valid configuration descriptor */
			 pParseConfigDesc = (pUSBHST_CONFIG_DESCRIPTOR)pParseDesc;

             break;

        default : /* invalid descriptor type */

            /* Debug Message */
            OS_LOG_MESSAGE_HIGH(
                HUB,"usbHubConfigure:InvalidDesc fail \n",0,0,0,0);

            /* Free the memory allocated */
            OS_FREE (pDeviceDescriptor);

             /* Free the memory allocated */
            OS_FREE (pConfigDescriptor);

            return USBHST_INVALID_PARAMETER ;

		}/*end of switch (pParseDesc[1]) */

    uDescLength= pParseDesc[0];

    /* check are  we jumping off the  descriptor  */
    if ( uDescLength + uLengthParsed >= USB_HUB_CONFIG_wTotalLength((UINT8 *)
                                                           pParseConfigDesc))
	{
        break;
    }/* end of if ( uDescLength + ....)*/

     /* update the length parsed */
     uLengthParsed += uDescLength ; 

    /*
     * move the pointer to the next desc by adding the length of
     * descriptor to the pointer
     */

     pParseDesc =(UINT8 *) ( ((UINT32)pParseDesc)+(UINT32)uDescLength);
     uCount ++;
     }/* end of  while(uCount < 3)    */

    /* Validate if the  descriptor have arrived in correct sequence */
    if(1!= ( USB_VALIDATE_DESCRIPTOR_SEQUENCE(uDescriptorSequence)
                & (uCount <= 3)))
	{
        OS_LOG_MESSAGE_HIGH(
            HUB,"usbHubConfigure:InvalidDescSequence fail \n",0,0,0,0);

        /* Free the memory allocated */
        OS_FREE (pDeviceDescriptor);

        /* Free the memory allocated */
        OS_FREE (pConfigDescriptor);

         return USBHST_INVALID_PARAMETER ;
	}/* end of if(1!= ( USB_VALIDATE_DESCRIPTOR_SEQUENCE(uDescriptorSequence).....)*/

     /*
     * Call USBHST_SetConfiguration() with configuration value and device
     * handle. if failed:
     * i.    Call OS_FREE() to free the configuration buffer.
     * ii.   Return result.
     */

    Result= usbHstSetConfiguration ( uDeviceHandle,
                                      USB_DEF_ACTIVE_HUB_CONFIG_INDEX);

    /* Check the result */
    if (USBHST_SUCCESS != Result)
    {
        /* Debug Message */
        OS_LOG_MESSAGE_HIGH(
            HUB,"usbHubConfigure:setconf fail R=0x%x \n",Result,0,0,0);

        /* Free the memory allocated */
        OS_FREE (pDeviceDescriptor);

        /* Free the memory allocated */
        OS_FREE (pConfigDescriptor);

        return Result;
        
    } /* End of (USBHST_SUCCESS !=Result ) */

    /* Set the Hub Descriptor length as 6 */
    uLength = 6;

    /* Allocate memory for the hub descriptor */

    pHubDescriptor = OS_MALLOC(7);

    /* Check if memory allocation is successful */

    if (pHubDescriptor == NULL)
        {

        /* Debug Message */
        OS_LOG_MESSAGE_HIGH(
            HUB,
            "usbHubConfigureHub:memAllocation fail R=0x%x L=%d\n",
            USBHST_INSUFFICIENT_MEMORY,
            uLength,
            0,
            0);

        /* Free the memory allocated */
        OS_FREE (pDeviceDescriptor);

        /* Free the memory allocated */
        OS_FREE (pConfigDescriptor);

        return USBHST_INSUFFICIENT_MEMORY;

        }

    /* Initialize all the fields */

    OS_MEMSET(pHubDescriptor, 0, 7);


    /* 
     * Call HUB_GetHubDescriptor() for uDeviceHandle,
     * HUB_DESCRIPTOR and for 6 bytes. If call fails,
     * i.    Call OS_FREE() to free the configuration buffer.
     * ii.   Return USBHST_FAILURE.
     */
    Result= usbHubGetDescriptor(uDeviceHandle, /* Device Handle */
                                 /* buffer to copy to */
                                 (UINT8 *)pHubDescriptor,
                                 /* Size of the descriptor */
                                 &uLength);

    /* Check the result */
    if ( (USBHST_SUCCESS != Result) ||(6 != uLength) )
    {
        /* Debug Message */
        OS_LOG_MESSAGE_HIGH(
            HUB,
            "usbHubConfigureHub:gethubdesc fail R=0x%x L=%d\n",
            Result,
            uLength,
            0,
            0);

        /* Free the memory allocated */
        OS_FREE (pDeviceDescriptor);

        /* Free the memory allocated */
        OS_FREE (pConfigDescriptor);

        /* Free memory allocated for hub descriptor */

        OS_FREE(pHubDescriptor);

        return Result;

    } /* End of (USBHST_SUCCESS !=Result ) */


    /* Allocate memory for hub status */
    pStatusOfHub = OS_MALLOC(2);

    /* Check if memory allocation is successful */
    if (pStatusOfHub == NULL)
        {

        /* Debug Message */
        OS_LOG_MESSAGE_HIGH(
            HUB,
            "usbHubConfigure:Memory allocation failure fail R=0x%x \n",
            USBHST_INSUFFICIENT_MEMORY,
            0,
            0,
            0);

        /* Free the memory allocated */
        OS_FREE (pDeviceDescriptor);

        /* Free the memory allocated */
        OS_FREE (pConfigDescriptor);

        /* Free memory allocated for hub descriptor */
        OS_FREE (pHubDescriptor);

        return USBHST_INSUFFICIENT_MEMORY;

        }

    /*
     * Get the Power requirement of the hub 
     * - Do a get status to the hub.
     * If the current status returns that the hub is
     * self powered, it can supply 500 mA per port
     * if it is bus powered, it can supply 100mA per port
     */

    Result = usbHstGetStatus(uDeviceHandle, /* Device Handle */
                              USBHST_RECIPIENT_DEVICE, /* uRecipient */
                              0,  /* Index */
                              (UCHAR *) pStatusOfHub);  /* Buffer */

    /* Check the result */
    if (USBHST_SUCCESS != Result)
    {
        /* Debug Message */
        OS_LOG_MESSAGE_HIGH(
            HUB,
            "usbHubConfigure:getStatusOfDevice fail R=0x%x \n",
            Result,
            0,
            0,
            0);

        /* Free the memory allocated */
        OS_FREE (pDeviceDescriptor);

        /* Free the memory allocated */
        OS_FREE (pConfigDescriptor);

        /* Free memory allocated for hub descriptor */
        OS_FREE (pHubDescriptor);

        /* Free memory allocated for hub status */
        OS_FREE (pStatusOfHub);

        return Result;

    } /* End of (USBHST_SUCCESS !=Result ) */

    /* swap */

    *pStatusOfHub = OS_UINT16_LE_TO_CPU(*pStatusOfHub);

    /* check whether the hub is operating in bus powered or self powered mode */

    if ( !(*pStatusOfHub & USB_HUB_POWER_CHECK))
       {

⌨️ 快捷键说明

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