上篇《Windows 驱动开发 - 2》我们已经添加了EvtDevicePrepareHardware事件,但是我们还没有增加内容。
对于USB来说主要进行2步操作:
1. 建立USB目标
使用方法WdfUsbTargetDeviceCreate来建立USB设备。
NTSTATUS WdfUsbTargetDeviceCreate( [in] WDFDEVICE Device, [in, optional] PWDF_OBJECT_ATTRIBUTES Attributes, [out] WDFUSBDEVICE *UsbDevice);
(1) Context
为了针对USB设备,我们需要建立一个带USBDEVICE成员的结构指针。
typedef struct _DEVICE_CONTEXT { WDFUSBDEVICE UsbDevice; WDFUSBINTERFACE UsbInterface;} DEVICE_CONTEXT, *PDEVICE_CONTEXT;
(2) Create Usb
status = WdfUsbTargetDeviceCreate(Device, WDF_NO_OBJECT_ATTRIBUTES, &pDeviceContext->UsbDevice);
2. 配置他
使用WdfUsbTargetDeviceSelectConfig方法来配置usb设备。
NTSTATUS WdfUsbTargetDeviceSelectConfig( [in] WDFUSBDEVICE UsbDevice, [in, optional] PWDF_OBJECT_ATTRIBUTES PipeAttributes, [in, out] PWDF_USB_DEVICE_SELECT_CONFIG_PARAMS Params);
(1) 初始化USB配置
使用WDF_USB_DEVICE_SELECT_CONFIG_PARAMS_INIT_SINGLE_INTERFACE
VOID WDF_USB_DEVICE_SELECT_CONFIG_PARAMS_INIT_SINGLE_INTERFACE( _Out_ PWDF_USB_DEVICE_SELECT_CONFIG_PARAMS Params);
初始化WDF_USB_DEVICE_SELECT_CONFIG_PARAMS结构变量地址。
WDF_USB_DEVICE_SELECT_CONFIG_PARAMS_INIT_SINGLE_INTERFACE(&configParams);
(2) 完成配置
status = WdfUsbTargetDeviceSelectConfig(pDeviceContext->UsbDevice, WDF_NO_OBJECT_ATTRIBUTES, &configParams);
附:代码(EvtDevicePrepareHardware事件)
NTSTATUSEvtDevicePrepareHardware( 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;}