上篇《Windows 驱动开发 - 2》我们已经添加了EvtDevicePrepareHardware事件,但是我们还没有增加内容。
对于USB来说主要进行2步操作:
使用方法WdfUsbTargetDeviceCreate来建立USB设备。
NTSTATUS WdfUsbTargetDeviceCreate( [in] WDFDEVICE Device, [in, optional] PWDF_OBJECT_ATTRIBUTES Attributes, [out] WDFUSBDEVICE *UsbDevice );
为了针对USB设备,我们需要建立一个带USBDEVICE成员的结构指针。
typedef struct _DEVICE_CONTEXT { WDFUSBDEVICE UsbDevice; WDFUSBINTERFACE UsbInterface; } DEVICE_CONTEXT, *PDEVICE_CONTEXT;
status = WdfUsbTargetDeviceCreate(Device, WDF_NO_OBJECT_ATTRIBUTES, &pDeviceContext->UsbDevice);
NTSTATUS WdfUsbTargetDeviceSelectConfig( [in] WDFUSBDEVICE UsbDevice, [in, optional] PWDF_OBJECT_ATTRIBUTES PipeAttributes, [in, out] PWDF_USB_DEVICE_SELECT_CONFIG_PARAMS Params );
VOID WDF_USB_DEVICE_SELECT_CONFIG_PARAMS_INIT_SINGLE_INTERFACE( _Out_ PWDF_USB_DEVICE_SELECT_CONFIG_PARAMS Params );
WDF_USB_DEVICE_SELECT_CONFIG_PARAMS_INIT_SINGLE_INTERFACE(&configParams);
status = WdfUsbTargetDeviceSelectConfig(pDeviceContext->UsbDevice, WDF_NO_OBJECT_ATTRIBUTES, &configParams);
NTSTATUS EvtDevicePrepareHardware( IN WDFDEVICE Device, IN WDFCMRESLIST ResourceList, IN WDFCMRESLIST ResourceListTranslated ) { NTSTATUS status; PDEVICE_CONTEXT pDeviceContext; WDF_USB_DEVICE_SELECT_CONFIG_PARAMS configParams; UNREFERENCED_PARAMETER(ResourceList); UNREFERENCED_PARAMETER(ResourceListTranslated); pDeviceContext = GetDeviceContext(Device); // // Create the USB device if it is not already created. // if (pDeviceContext->UsbDevice == NULL) { status = WdfUsbTargetDeviceCreate(Device, WDF_NO_OBJECT_ATTRIBUTES, &pDeviceContext->UsbDevice); if (!NT_SUCCESS(status)) { KdPrint(("WdfUsbTargetDeviceCreate failed 0x%x\n", status)); return status; } } WDF_USB_DEVICE_SELECT_CONFIG_PARAMS_INIT_SINGLE_INTERFACE(&configParams); status = WdfUsbTargetDeviceSelectConfig(pDeviceContext->UsbDevice, WDF_NO_OBJECT_ATTRIBUTES, &configParams); if(!NT_SUCCESS(status)) { KdPrint(("WdfUsbTargetDeviceSelectConfig failed 0x%x\n", status)); return status; } pDeviceContext->UsbInterface = configParams.Types.SingleInterface.ConfiguredUsbInterface; return status; }