VOID InitInterfacePair(__in WDFUSBDEVICE UsbDevice, __in PWDF_USB_INTERFACE_SETTING_PAIR pUsbInterfacePair, __in UCHAR ucNumberOfInterface) { UCHAR iIndex; for(iIndex=0;iIndex<ucNumberOfInterface;iIndex++) { pUsbInterfacePair[iIndex].SettingIndex =0; pUsbInterfacePair[iIndex].UsbInterface = WdfUsbTargetDeviceGetInterface( UsbDevice, iIndex); } }
// 初始化结构体WDF_USB_INTERFACE_SETTING_PAIR。 // 用来配置多接口设备。 int InitSettingPairs(IN WDFUSBDEVICE UsbDevice, // 设备对象 OUT PWDF_USB_INTERFACE_SETTING_PAIR Pairs,// 结构体指针。 IN ULONG NumSettings // 接口个数 ) { int i; // 最多支持8个接口,把多余的切掉。 if(NumSettings > MAX_INTERFACES) NumSettings = MAX_INTERFACES; // 配置接口 for(i = 0; i < NumSettings; i++) { Pairs[i].UsbInterface = WdfUsbTargetDeviceGetInterface(UsbDevice, i);// 设置接口句柄 Pairs[i].SettingIndex = MultiInterfaceSettings[i]; // 设置接口可选值(Alternate Setting) } return NumSettings; }
NTSTATUS SelectInterfaces( _In_ WDFDEVICE Device ) /*++ Routine Description: This helper routine selects the configuration, interface and creates a context for every pipe (end point) in that interface. Arguments: Device - Handle to a framework device Return Value: NT status value --*/ { WDF_USB_DEVICE_SELECT_CONFIG_PARAMS configParams; NTSTATUS status = STATUS_SUCCESS; PDEVICE_CONTEXT pDeviceContext; WDFUSBPIPE pipe; WDF_USB_PIPE_INFORMATION pipeInfo; UCHAR index; UCHAR numberConfiguredPipes; WDFUSBINTERFACE usbInterface; PAGED_CODE(); pDeviceContext = GetDeviceContext(Device); WDF_USB_DEVICE_SELECT_CONFIG_PARAMS_INIT_SINGLE_INTERFACE( &configParams); usbInterface = WdfUsbTargetDeviceGetInterface(pDeviceContext->UsbDevice, 0); if (NULL == usbInterface) { status = STATUS_UNSUCCESSFUL; TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP, "WdfUsbTargetDeviceGetInterface 0 failed %!STATUS! \n", status); return status; } configParams.Types.SingleInterface.ConfiguredUsbInterface = usbInterface; configParams.Types.SingleInterface.NumberConfiguredPipes = WdfUsbInterfaceGetNumConfiguredPipes(usbInterface); pDeviceContext->UsbInterface = configParams.Types.SingleInterface.ConfiguredUsbInterface; numberConfiguredPipes = configParams.Types.SingleInterface.NumberConfiguredPipes; // // Get pipe handles // for(index=0; index < numberConfiguredPipes; index++) { WDF_USB_PIPE_INFORMATION_INIT(&pipeInfo); pipe = WdfUsbInterfaceGetConfiguredPipe( pDeviceContext->UsbInterface, index, //PipeIndex, &pipeInfo ); // // Tell the framework that it's okay to read less than // MaximumPacketSize // WdfUsbTargetPipeSetNoMaximumPacketSizeCheck(pipe); if(WdfUsbPipeTypeInterrupt == pipeInfo.PipeType) { TraceEvents(TRACE_LEVEL_INFORMATION, DBG_IOCTL, "Interrupt Pipe is 0x%p\n", pipe); pDeviceContext->InterruptPipe = pipe; } if(WdfUsbPipeTypeBulk == pipeInfo.PipeType && WdfUsbTargetPipeIsInEndpoint(pipe)) { TraceEvents(TRACE_LEVEL_INFORMATION, DBG_IOCTL, "BulkInput Pipe is 0x%p\n", pipe); pDeviceContext->BulkReadPipe = pipe; } if(WdfUsbPipeTypeBulk == pipeInfo.PipeType && WdfUsbTargetPipeIsOutEndpoint(pipe)) { TraceEvents(TRACE_LEVEL_INFORMATION, DBG_IOCTL, "BulkOutput Pipe is 0x%p\n", pipe); pDeviceContext->BulkWritePipe = pipe; } } // // If we didn't find all the 3 pipes, fail the start. // if(!(pDeviceContext->BulkWritePipe && pDeviceContext->BulkReadPipe && pDeviceContext->InterruptPipe)) { status = STATUS_INVALID_DEVICE_STATE; TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP, "Device is not configured properly %!STATUS!\n", status); return status; } return status; }
NTSTATUS AndroidUsbDeviceObject::SelectInterfaces() { ASSERT_IRQL_PASSIVE(); ASSERT(IsDeviceConfigured()); if (!IsDeviceConfigured()) return STATUS_INTERNAL_ERROR; WDF_USB_DEVICE_SELECT_CONFIG_PARAMS config_params; PWDF_USB_INTERFACE_SETTING_PAIR pairs = NULL; // TODO: We need to find a way (possibly by looking at each // interface descriptor) to get index of the ADB interface in multiinterface // configuration. UCHAR adb_interface_index = 0; if (IsSingleInterfaceDevice()) { // Our device has only one interface, so we don't have to bother with // multiple interfaces at all. GoogleDbgPrint("\n********** Device reports single interface"); // Select single interface configuration WDF_USB_DEVICE_SELECT_CONFIG_PARAMS_INIT_SINGLE_INTERFACE(&config_params); } else { // Configure multiple interfaces ULONG num_interf = GetInterfaceCount(); GoogleDbgPrint("\n********** Device reports %u interfaces", num_interf); // Allocate pairs for each interface pairs = new(PagedPool, GANDR_POOL_TAG_INTERF_PAIRS) WDF_USB_INTERFACE_SETTING_PAIR[num_interf]; ASSERT(NULL != pairs); if (NULL == pairs) return STATUS_INSUFFICIENT_RESOURCES; adb_interface_index = 1; // Initialize each interface pair for (UCHAR pair = 0; pair < num_interf; pair++) { pairs[pair].SettingIndex = 0; pairs[pair].UsbInterface = WdfUsbTargetDeviceGetInterface(wdf_target_device(), pair); ASSERT(NULL != pairs[pair].UsbInterface); if (NULL == pairs[pair].UsbInterface) { delete[] pairs; return STATUS_INTERNAL_ERROR; } } // Select multiinterface configuration WDF_USB_DEVICE_SELECT_CONFIG_PARAMS_INIT_MULTIPLE_INTERFACES(&config_params, (UCHAR)num_interf, pairs); } NTSTATUS status = WdfUsbTargetDeviceSelectConfig(wdf_target_device(), WDF_NO_OBJECT_ATTRIBUTES, &config_params); if (NULL != pairs) delete[] pairs; // ASSERT(NT_SUCCESS(status)); if (!NT_SUCCESS(status)) return status; #if DBG PrintSelectedConfig(&config_params); #endif // DBG wdf_usb_interface_ = WdfUsbTargetDeviceGetInterface(wdf_target_device(), adb_interface_index); ASSERT(NULL != wdf_usb_interface_); if (NULL == wdf_usb_interface_) return STATUS_INTERNAL_ERROR; configured_pipes_num_ = WdfUsbInterfaceGetNumEndpoints(wdf_usb_interface(), 0); ASSERT(0 != configured_pipes_num_); // Cache selected interface descriptor BYTE setting_index = WdfUsbInterfaceGetConfiguredSettingIndex(wdf_usb_interface()); WdfUsbInterfaceGetDescriptor(wdf_usb_interface(), setting_index, &interface_descriptor_); #if DBG PrintInterfaceDescriptor(interface_descriptor()); #endif // DBG // Iterate over pipes, decoding and saving info about bulk r/w pipes for // easier and faster addressing later on when they get opened for (UCHAR pipe = 0; pipe < configured_pipes_num(); pipe++) { WDF_USB_PIPE_INFORMATION pipe_info; WDF_USB_PIPE_INFORMATION_INIT(&pipe_info); WDFUSBPIPE wdf_pipe_obj = WdfUsbInterfaceGetConfiguredPipe(wdf_usb_interface(), pipe, &pipe_info); ASSERT(NULL != wdf_pipe_obj); if (NULL != wdf_pipe_obj) { if ((WdfUsbPipeTypeBulk == pipe_info.PipeType) && WDF_USB_PIPE_DIRECTION_IN(pipe_info.EndpointAddress)) { // This is a bulk read pipe ASSERT(!IsBulkReadPipeKnown()); bulk_read_pipe_index_ = pipe; } else { ASSERT(!IsBulkWritePipeKnown()); bulk_write_pipe_index_ = pipe; } } #if DBG PrintPipeInformation(&pipe_info, pipe); #endif // DBG } // At the end we must have calculated indexes for both, // bulk read and write pipes ASSERT(!NT_SUCCESS(status) || (IsBulkReadPipeKnown() && IsBulkWritePipeKnown())); return status; }