上篇《Windows 驱动开发 - 2》我们已经添加了EvtDevicePrepareHardware事件,但是我们还没有增加内容。

    对于USB来说主要进行2步操作:

1. 建立USB目标

     使用方法WdfUsbTargetDeviceCreate来建立USB设备。


  1. NTSTATUS WdfUsbTargetDeviceCreate(
  2.   [in] WDFDEVICE Device,
  3.   [in, optional] PWDF_OBJECT_ATTRIBUTES Attributes,
  4.   [out] WDFUSBDEVICE *UsbDevice
  5. );

(1) Context

    为了针对USB设备,我们需要建立一个带USBDEVICE成员的结构指针。


  1. typedef struct _DEVICE_CONTEXT {
  2.   WDFUSBDEVICE UsbDevice;
  3.   WDFUSBINTERFACE UsbInterface;
  4. } DEVICE_CONTEXT, *PDEVICE_CONTEXT;

(2) Create Usb


  1. status = WdfUsbTargetDeviceCreate(Device,
  2.                                     WDF_NO_OBJECT_ATTRIBUTES,
  3.                                     &pDeviceContext->UsbDevice);

2. 配置他

    使用WdfUsbTargetDeviceSelectConfig方法来配置usb设备。


  1. NTSTATUS WdfUsbTargetDeviceSelectConfig(
  2.   [in] WDFUSBDEVICE UsbDevice,
  3.   [in, optional] PWDF_OBJECT_ATTRIBUTES PipeAttributes,
  4.   [in, out] PWDF_USB_DEVICE_SELECT_CONFIG_PARAMS Params
  5. );

(1) 初始化USB配置

    使用WDF_USB_DEVICE_SELECT_CONFIG_PARAMS_INIT_SINGLE_INTERFACE


  1. VOID WDF_USB_DEVICE_SELECT_CONFIG_PARAMS_INIT_SINGLE_INTERFACE(
  2.   _Out_ PWDF_USB_DEVICE_SELECT_CONFIG_PARAMS Params
  3. );
初始化WDF_USB_DEVICE_SELECT_CONFIG_PARAMS结构变量地址。

  1. WDF_USB_DEVICE_SELECT_CONFIG_PARAMS_INIT_SINGLE_INTERFACE(&configParams);

(2) 完成配置


  1. status = WdfUsbTargetDeviceSelectConfig(pDeviceContext->UsbDevice,
  2.                                         WDF_NO_OBJECT_ATTRIBUTES,
  3.                                         &configParams);
附:代码(EvtDevicePrepareHardware事件)

  1. NTSTATUS
  2. EvtDevicePrepareHardware(
  3.     IN WDFDEVICE Device,
  4.     IN WDFCMRESLIST ResourceList,
  5.     IN WDFCMRESLIST ResourceListTranslated
  6.     )
  7. {
  8.     NTSTATUS status;
  9.     PDEVICE_CONTEXT pDeviceContext;
  10.     WDF_USB_DEVICE_SELECT_CONFIG_PARAMS configParams;

  11.     UNREFERENCED_PARAMETER(ResourceList);
  12.     UNREFERENCED_PARAMETER(ResourceListTranslated);

  13.     pDeviceContext = GetDeviceContext(Device);

  14.     //
  15.     // Create the USB device if it is not already created.
  16.     //
  17.     if (pDeviceContext->UsbDevice == NULL) {
  18.         status = WdfUsbTargetDeviceCreate(Device,
  19.                                     WDF_NO_OBJECT_ATTRIBUTES,
  20.                                     &pDeviceContext->UsbDevice);
  21.         if (!NT_SUCCESS(status)) {
  22.             KdPrint(("WdfUsbTargetDeviceCreate failed 0x%x\n", status));
  23.             return status;
  24.         }
  25.     }
  26.     
  27.     WDF_USB_DEVICE_SELECT_CONFIG_PARAMS_INIT_SINGLE_INTERFACE(&configParams);

  28.     status = WdfUsbTargetDeviceSelectConfig(pDeviceContext->UsbDevice,
  29.                                         WDF_NO_OBJECT_ATTRIBUTES,
  30.                                         &configParams);
  31.     if(!NT_SUCCESS(status)) {
  32.         KdPrint(("WdfUsbTargetDeviceSelectConfig failed 0x%x\n", status));
  33.         return status;
  34.     }

  35.     pDeviceContext->UsbInterface =
  36.                 configParams.Types.SingleInterface.ConfiguredUsbInterface;

  37.     return status;
  38. }









10-03 15:59