📄 device.c
字号:
/* * This loop search the entire configuration descriptor for the * given interface number and the given alternate setting number */ while (uLength <= uTotalLength) { /* Copy the descriptor header from the buffer */ pHeader = (pUSBHST_DESCRIPTOR_HEADER)(pConfigBuffer + uLength); /* * This loop skips the descriptors until it finds the interface * descriptor */ while (pHeader->uDescriptorType != USBHST_INTERFACE_DESC) { /* Move the pointer to read the next descriptor */ uLength = uLength + pHeader->uLength; /* * Check if whole interface descriptor buffer is read before the * expected information is fetched. */ if (uLength >= uTotalLength) { OS_LOG_MESSAGE_HIGH( USBD, "usbdGetInterfaceOffset() Failed: Invalid configuration\ descriptor.\n", 0, 0, 0, 0); return USBHST_FAILURE; }/* END if whole interface descriptor buffer is read...*/ /* Copy the next descriptor header from the buffer */ pHeader = (pUSBHST_DESCRIPTOR_HEADER)(pConfigBuffer + uLength); } /* Copy the interface info from the buffer */ pInterfaceDesc = (pUSBHST_INTERFACE_DESCRIPTOR) (pConfigBuffer + uLength); /* * if this interface's bInterfaceNumber is uInterfaceNum and * uAlternateSetting is uAltSetting then break */ if ((uInterfaceNum == pInterfaceDesc->bInterfaceNumber) && (uAltIndex == pInterfaceDesc->bAlternateSetting)) { /* Given interface match is found */ bMatchFlag = TRUE; break; } else { /* Skip the size of interface desc size */ uLength = uLength + sizeof(USBHST_INTERFACE_DESCRIPTOR); } } /*End of while loop*/ /* If the match is found update the offset and return success */ if (TRUE == bMatchFlag) { /* Update the offset */ *pStartOffset = uLength; OS_LOG_MESSAGE_LOW(USBD, "Exiting usbdGetInterfaceOffset() Function.\n", 0, 0, 0, 0); return USBHST_SUCCESS; } OS_LOG_MESSAGE_LOW( USBD, "usbdGetInterfaceOffset() Failed: Matching interface not found .\n", 0, 0, 0, 0); return USBHST_FAILURE; } /* End of usbdGetInterfaceOffset() *//***************************************************************************** usbdCreatePipeForInterface - parses the interface descriptor.** parses the interface descriptor creates and updates the endpoint* info in device info** RETURNS: USBHST_SUCCESS, USBHST_INSUFFICIENT_MEMORY on insufficient * resource** ERRNO: None** \NOMANUAL*/LOCAL USBHST_STATUS usbdCreatePipeForInterface ( pUSBD_DEVICE_INFO pDeviceInfo, /* USB Device info */ UCHAR *pNewInterfaceDesc, /* Ptr to interface descriptor */ UINT16 uInterfaceNum /* Interface number */ ) { UINT16 uLength = 0; /* Current pos in descr */ pUSBD_ENDPOINT_INFO pEndpointInfo = NULL; /* Endpoint information */ UINT16 uNumEndpoints = 0; /* Number of endpoints */ UINT16 uEndpointCount = 0; /* Endpoint number */ pUSBHST_DESCRIPTOR_HEADER pHeader = NULL; /* Descriptor header */ pUSBD_BUS_INFO pUsbBusInfo = NULL; /* Bus Information */ pUSBHST_HC_DRIVER pHCDriverInfo = NULL; /* Host Controller info */ pUSBHST_ENDPOINT_DESCRIPTOR pEndpointDesc = NULL; /* Endpoint descriptor */ /* WindView Instrumentation */ USB_USBD_LOG_EVENT( USB_USBD_WV_DEVICE, "Entering usbdCreatePipeForInterface() Function.", USB_USBD_WV_FILTER); OS_LOG_MESSAGE_LOW(USBD, "Entering usbdCreatePipeForInterface() Function.\n", 0, 0, 0, 0); /* Store the number of endpoints */ uNumEndpoints = pDeviceInfo->pInterfacesInfo[uInterfaceNum].uNumEndpoints; /* if there is only default pipe then return success */ if (0 == uNumEndpoints ) { OS_LOG_MESSAGE_LOW(USBD, "Exiting usbdCreatePipeForInterface() Function.\n", 0, 0, 0, 0); return USBHST_SUCCESS; } /* Get the bus information for this request */ pUsbBusInfo = &(gUSBBusInfoList[pDeviceInfo->uBusIndex]); /* Get the HC driver information for this request */ pHCDriverInfo = &(gHCDriverList[pUsbBusInfo->hcdIndex]); /* Allocate memory to store endpoint info */ pEndpointInfo = (pUSBD_ENDPOINT_INFO)OS_MALLOC( sizeof(USBD_ENDPOINT_INFO) * uNumEndpoints); /* Check if memory is allocated to store interface info */ if (NULL == pEndpointInfo) { OS_LOG_MESSAGE_HIGH( USBD, "usbdCreatePipeForInterface Failed :INSUFFICIENT_MEMORY.\n", 0, 0, 0, 0); return USBHST_INSUFFICIENT_MEMORY; } /* Fill the endpoint info memory area with 0 */ OS_MEMSET(pEndpointInfo, 0, sizeof(USBD_ENDPOINT_INFO) * uNumEndpoints); /* Move the buffer pointer to point to the endpoint descriptor */ uLength = ((pUSBHST_INTERFACE_DESCRIPTOR)pNewInterfaceDesc)->bLength; /* * For this interface read all the corresponding endpoint descriptors * and store them as endpoint info in the respective interface */ while (uEndpointCount < uNumEndpoints) { /* Copy the descriptor header from the buffer */ pHeader = (pUSBHST_DESCRIPTOR_HEADER)(pNewInterfaceDesc + uLength); /* Check if the descriptor is endpoint */ while (pHeader->uDescriptorType != USBHST_ENDPOINT_DESC) { /* Move the pointer to read the next descriptor */ uLength = uLength + pHeader->uLength; /* Copy the next descriptor header from the buffer */ pHeader = (pUSBHST_DESCRIPTOR_HEADER)(pNewInterfaceDesc + uLength); } /* Copy the endpoint descriptor from the buffer */ pEndpointDesc = (pUSBHST_ENDPOINT_DESCRIPTOR) (pNewInterfaceDesc + uLength); /* Store the endpoint address */ pEndpointInfo[uEndpointCount].uEndpointAddress = pEndpointDesc->bEndpointAddress; /* Call the HCD CreatePipe function to create the pipe. */ pHCDriverInfo->createPipe(pUsbBusInfo->uRelativeBusIndex, pDeviceInfo->uDeviceAddress, pDeviceInfo->uDeviceSpeed, (UCHAR *) pEndpointDesc, pDeviceInfo->uHighSpeedHubInfo, &(pEndpointInfo[uEndpointCount].hPipe)); /* Move the buffer pointer to point to the next descriptor */ uLength = uLength + sizeof(USBHST_ENDPOINT_DESCRIPTOR); /* Increment the endpoint count */ uEndpointCount = uEndpointCount + 1; } /* End of WHILE loop for endpoint */ /* Update the end point information for the interface */ pDeviceInfo->pInterfacesInfo[uInterfaceNum].pEndpointInformation = pEndpointInfo; OS_LOG_MESSAGE_LOW(USBD, "Exiting usbdCreatePipeForInterface() Function.\n", 0, 0, 0, 0); return USBHST_SUCCESS; } /* End of usbdCreatePipeForInterface() *//***************************************************************************** usbHstDeviceNew - handles a new device connection** The Hub Class Driver calls this function, when a device connection * is detected.** RETURNS: USBHST_SUCCESS, USBHST_INSUFFICIENT_MEMORY,* USBHST_INSUFFICIENT_RESOURCES, USBHST_INVALID_PARAMETERS,* USBHST_FAILURE if Operation failed** ERRNO: None** \NOMANUAL*/LOCAL USBHST_STATUS usbHstDeviceNew ( UINT8 uSpeed, /* Speed of device */ UINT8 uBusIndex, /* Index of bus */ UINT32 hHighSpeedHub, /* Handle to nearest high spped hub */ UINT8 uHighSpeedHubPortNumber, /* Port number of nearest high speed hub */ UINT32 *pDeviceHandle /* Device handle */ ) { /* To store new device info */ pUSBD_DEVICE_INFO pNewDeviceInfo = NULL; /* New device information */ UINT16 uHighSpeedHubInfo = 0; /* High speed hub info */ UINT8 uAddress = 0; /* New device address */ UCHAR *pBuffer = NULL; /* Device descr buffer */ UCHAR *pNewBuffer = NULL; /* Device descr buffer */ UINT32 uDefaultPipe = 0; /* Default pipe for device*/ pUSBD_BUS_INFO pUsbBusInfo = NULL; /* USB Bus information */ pUSBHST_HC_DRIVER pHCDriverInfo = NULL; /* HC Driver info */ USBHST_STATUS nStatus = USBHST_FAILURE; /* Return status */ UINT8 uRelativeBusIndex = 0; /* Relative bus index */ UINT32 uDescSize = 0; /* Descriptor size */ /* To store end descriptor details */ USBHST_ENDPOINT_DESCRIPTOR DefaultEndpointDesc = {7, USBHST_ENDPOINT_DESC, 0, USBHST_CONTROL_TRANSFER, 8, 0}; /* WindView Instrumentation */ USB_USBD_LOG_EVENT( USB_USBD_WV_DEVICE, "Entering usbHstDeviceNew() Function.", USB_USBD_WV_FILTER); OS_LOG_MESSAGE_LOW(USBD, "Entering usbHstDeviceNew() Function.\n",0,0,0,0); /* Check if uSpeed is either low, full or high speed */ if (USBHST_HIGH_SPEED < uSpeed) { OS_LOG_MESSAGE_MEDIUM( USBD, "usbHstNewDevice() Failed: Invalid parameter.\n", 0, 0, 0, 0); return USBHST_INVALID_PARAMETER; } /* Check if gUSBBusInfoList entry is valid for the specified Bus Index */ if ((USBD_MAX_BUS_NUM <= uBusIndex) || (0 == gUSBBusInfoList[uBusIndex].hDefaultPipe)) { OS_LOG_MESSAGE_MEDIUM( USBD, "usbHstDeviceNew() Failed: Invalid parameter.\n", 0, 0, 0, 0); return USBHST_INVALID_PARAMETER; } /* Check if device handle is valid*/ if (NULL == pDeviceHandle) { OS_LOG_MESSAGE_MEDIUM( USBD, "usbHstDeviceNew() Failed: Invalid parameter.\n", 0, 0, 0, 0); return USBHST_INVALID_PARAMETER; } /* Check if the adress and bus index for the hHighSpeedHub is valid */ if ( (127 < (hHighSpeedHub & 0x000000FF)) || (USBD_MAX_BUS_NUM <= ((hHighSpeedHub & 0x0000FF00) >> 8)) ) { OS_LOG_MESSAGE_HIGH( USBD, "usbHstDeviceNew() Failed: Invalid high speed hub handle.\n", 0, 0, 0, 0); return USBHST_INVALID_PARAMETER; } /* Obtain an USB Address on the specified USB Bus */ nStatus = usbdGetFreeAddress(uBusIndex, &uAddress); if ((USBHST_SUCCESS != nStatus) || (127 < uAddress)) { OS_LOG_MESSAGE_HIGH( USBD, "usbHstDeviceNew() Failed: usbdGetFreeAddress failed.\n", 0, 0, 0, 0); return nStatus; } /* Compute the new device handle for the device */ usbdGenerateDeviceHandle(pDeviceHandle, uBusIndex, uAddress); /* Ensure that the reserved fields are zero and the handle is non null */ if ((0 == (*pDeviceHandle)) || (0 != ((*pDeviceHandle) >> 16))) { OS_LOG_MESSAGE_MEDIUM( USBD, "usbHstDeviceNew() Failed:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -