📄 usbhubutility.c
字号:
/* 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 Result; } /* * 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) ||(6 != uLength) ) { /* 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); /* Parse the number of ports from the hub descriptor.*/ uNumPorts=USB_HUB_DESC_bNbrPorts(pHubDescriptor); /* * Call OS_MALLOC() to allocate HUB_BYTE_GRANURALITY(number of ports + 1) * bytes of status change data buffer. If failed, * i. Call OS_FREE() to free the configuration buffer. * ii. Return USBHST_INSUFFICIENT_MEMORY. */ uLength= USB_HUB_BYTE_GRANULARITY(uNumPorts + 1); /* allocate the memory */ pStatusChangeData= OS_MALLOC(uLength); /* Check the result */ if (NULL == pStatusChangeData) { /* Debug Message */ OS_LOG_MESSAGE_HIGH( HUB, "usbHubConfigure:INSUFF MEMORY-pStatusChngData %d\n",uLength,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 USBHST_INSUFFICIENT_MEMORY; } /* End of (USBHST_SUCCESS !=Result ) */ /* allocate the memory */ pStatusData= OS_MALLOC(uLength); /* Check the result */ if (NULL == pStatusData) { /* Debug Message */ OS_LOG_MESSAGE_HIGH( HUB,"usbHubConfigure:INSUFF MEMORY-pStatusData %d\n",uLength,0,0,0); /* Free the memory allocated */ OS_FREE (pDeviceDescriptor); OS_FREE (pConfigDescriptor); /* Free the memory allocated */ OS_FREE (pStatusChangeData); /* Free memory allocated for hub descriptor */ OS_FREE (pHubDescriptor); /* Free memory allocated for hub status */ OS_FREE (pStatusOfHub); return USBHST_INSUFFICIENT_MEMORY; } /* End of (USBHST_SUCCESS !=Result ) */ /* Clear the allocated buffer for status change data. */ OS_MEMSET(pStatusChangeData,0,uLength); /* Clear the allocated buffer for the status data. */ OS_MEMSET(pStatusData,0,uLength); /* * Call OS_MALLOC() to allocate memory for HUB_INFO as * sizeof(USB_HUB_INFO)+ ((number of ports-1)*sizeof(PHUB_PORT_INFO)) bytes. * If Failed, * i. Call OS_FREE() to free the configuration buffer. * ii. Call OS_FREE() to free the status change buffer. * iii. Return USBHST_INSUFFICIENT_MEMORY. */ /* * This is an over allocation of the HUB_INFO structure. * this allows us to access pPortList[n] count for all the ports. * [<------------->y][y][y][y]...
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -