A simple little driver include wdk h include

  • Slides: 44
Download presentation

A simple little driver

A simple little driver

#include <wdk. h> #include <wdf. h> DRIVER_INITIALIZE Driver. Entry; EVT_WDF_DRIVER_DEVICE_ADD On. Device. Add; NTSTATUS

#include <wdk. h> #include <wdf. h> DRIVER_INITIALIZE Driver. Entry; EVT_WDF_DRIVER_DEVICE_ADD On. Device. Add; NTSTATUS Driver. Entry(PDRIVER_OBJECT Driver. Object, PUNICODE_STRING Registry. Path) { WDF_DRIVER_CONFIG config; WDF_DRIVER_CONFIG_INIT(&config, On. Device. Add); return Wdf. Driver. Create(Driver. Object, Registry. Path, WDF_NO_OBJECT_ATTRIBUTES, &config, WDF_NO_HANDLE); } NTSTATUS On. Device. Add(WDFDRIVER Driver, PWDFDEVICE_INIT Device. Init) { return Wdf. Device. Create(&Device. Init, WDF_NO_OBJECT_ATTRIBUTES, WDF_NO_HANDLE); }

#include <wdk. h> #include <wdf. h> DRIVER_INITIALIZE Driver. Entry; EVT_WDF_DRIVER_DEVICE_ADD On. Device. Add; NTSTATUS

#include <wdk. h> #include <wdf. h> DRIVER_INITIALIZE Driver. Entry; EVT_WDF_DRIVER_DEVICE_ADD On. Device. Add; NTSTATUS Driver. Entry(PDRIVER_OBJECT Driver. Object, PUNICODE_STRING Registry. Path) { WDF_DRIVER_CONFIG config; WDF_DRIVER_CONFIG_INIT(&config, On. Device. Add); return Wdf. Driver. Create(Driver. Object, Registry. Path, WDF_NO_OBJECT_ATTRIBUTES, &config, WDF_NO_HANDLE); } NTSTATUS On. Device. Add(WDFDRIVER Driver, PWDFDEVICE_INIT Device. Init) { return Wdf. Device. Create(&Device. Init, WDF_NO_OBJECT_ATTRIBUTES, WDF_NO_HANDLE); }

#include <wdk. h> #include <wdf. h> DRIVER_INITIALIZE Driver. Entry; EVT_WDF_DRIVER_DEVICE_ADD On. Device. Add; NTSTATUS

#include <wdk. h> #include <wdf. h> DRIVER_INITIALIZE Driver. Entry; EVT_WDF_DRIVER_DEVICE_ADD On. Device. Add; NTSTATUS Driver. Entry(PDRIVER_OBJECT Driver. Object, PUNICODE_STRING Registry. Path) { WDF_DRIVER_CONFIG config; WDF_DRIVER_CONFIG_INIT(&config, On. Device. Add); return Wdf. Driver. Create(Driver. Object, Registry. Path, WDF_NO_OBJECT_ATTRIBUTES, &config, WDF_NO_HANDLE); } NTSTATUS On. Device. Add(WDFDRIVER Driver, PWDFDEVICE_INIT Device. Init) { return Wdf. Device. Create(&Device. Init, WDF_NO_OBJECT_ATTRIBUTES, WDF_NO_HANDLE); }

#include <wdk. h> #include <wdf. h> DRIVER_INITIALIZE Driver. Entry; EVT_WDF_DRIVER_DEVICE_ADD On. Device. Add; NTSTATUS

#include <wdk. h> #include <wdf. h> DRIVER_INITIALIZE Driver. Entry; EVT_WDF_DRIVER_DEVICE_ADD On. Device. Add; NTSTATUS Driver. Entry(PDRIVER_OBJECT Driver. Object, PUNICODE_STRING Registry. Path) { WDF_DRIVER_CONFIG config; WDF_DRIVER_CONFIG_INIT(&config, On. Device. Add); return Wdf. Driver. Create(Driver. Object, Registry. Path, WDF_NO_OBJECT_ATTRIBUTES, &config, WDF_NO_HANDLE); } NTSTATUS On. Device. Add(WDFDRIVER Driver, PWDFDEVICE_INIT Device. Init) { return Wdf. Device. Create(&Device. Init, WDF_NO_OBJECT_ATTRIBUTES, WDF_NO_HANDLE); }

#include <wdk. h> #include <wdf. h> DRIVER_INITIALIZE Driver. Entry; EVT_WDF_DRIVER_DEVICE_ADD On. Device. Add; NTSTATUS

#include <wdk. h> #include <wdf. h> DRIVER_INITIALIZE Driver. Entry; EVT_WDF_DRIVER_DEVICE_ADD On. Device. Add; NTSTATUS Driver. Entry(PDRIVER_OBJECT Driver. Object, PUNICODE_STRING Registry. Path) { WDF_DRIVER_CONFIG config; WDF_DRIVER_CONFIG_INIT(&config, On. Device. Add); return Wdf. Driver. Create(Driver. Object, Registry. Path, WDF_NO_OBJECT_ATTRIBUTES, &config, WDF_NO_HANDLE); } NTSTATUS On. Device. Add(WDFDRIVER Driver, PWDFDEVICE_INIT Device. Init) { return Wdf. Device. Create(&Device. Init, WDF_NO_OBJECT_ATTRIBUTES, WDF_NO_HANDLE); }

#include <wdk. h> #include <wdf. h> DRIVER_INITIALIZE Driver. Entry; EVT_WDF_DRIVER_DEVICE_ADD On. Device. Add; NTSTATUS

#include <wdk. h> #include <wdf. h> DRIVER_INITIALIZE Driver. Entry; EVT_WDF_DRIVER_DEVICE_ADD On. Device. Add; NTSTATUS Driver. Entry(PDRIVER_OBJECT Driver. Object, PUNICODE_STRING Registry. Path) { WDF_DRIVER_CONFIG config; WDF_DRIVER_CONFIG_INIT(&config, On. Device. Add); return Wdf. Driver. Create(Driver. Object, Registry. Path, WDF_NO_OBJECT_ATTRIBUTES, &config, WDF_NO_HANDLE); } NTSTATUS On. Device. Add(WDFDRIVER Driver, PWDFDEVICE_INIT Device. Init) { return Wdf. Device. Create(&Device. Init, WDF_NO_OBJECT_ATTRIBUTES, WDF_NO_HANDLE); }

#include <wdk. h> #include <wdf. h> DRIVER_INITIALIZE Driver. Entry; EVT_WDF_DRIVER_DEVICE_ADD On. Device. Add; NTSTATUS

#include <wdk. h> #include <wdf. h> DRIVER_INITIALIZE Driver. Entry; EVT_WDF_DRIVER_DEVICE_ADD On. Device. Add; NTSTATUS Driver. Entry(PDRIVER_OBJECT Driver. Object, PUNICODE_STRING Registry. Path) { WDF_DRIVER_CONFIG config; WDF_DRIVER_CONFIG_INIT(&config, On. Device. Add); return Wdf. Driver. Create(Driver. Object, Registry. Path, WDF_NO_OBJECT_ATTRIBUTES, &config, WDF_NO_HANDLE); } NTSTATUS On. Device. Add(WDFDRIVER Driver, PWDFDEVICE_INIT Device. Init) { return Wdf. Device. Create(&Device. Init, WDF_NO_OBJECT_ATTRIBUTES, WDF_NO_HANDLE); }

Storing Driver Specific Data

Storing Driver Specific Data

struct Device. Context { MYDEVICE_STATE Current. State; . . . }; WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(Device. Context, Get.

struct Device. Context { MYDEVICE_STATE Current. State; . . . }; WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(Device. Context, Get. Device. Context); NTSTATUS On. Device. Add(. . . ) { WDFDEVICE device; Device. Context* context; WDF_OBJECT_ATTRIBUTES attributes; WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, Device. Context); attributes. Evt. Destroy. Callback = On. Device. Destroy; Wdf. Device. Create(&Device. Init, &attributes, &device); if (failure) {return status; } device. Context = Get. Device. Context(device); device. Context->Current. State = My. Device. State. Uninitialized; . . . }

NTSTATUS On. Device. Add(. . . ) { Wdf. Usb. Target. Create(device, WDF_NO_OBJECT_ATTRIBUTES, &device.

NTSTATUS On. Device. Add(. . . ) { Wdf. Usb. Target. Create(device, WDF_NO_OBJECT_ATTRIBUTES, &device. Context->Usb. Io. Target); if (failure) return status; WDF_OBJECT_ATTRIBUTES attributes; WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, Request. Context); attributes. Parent. Object = device. Context->Usb. Io. Target; Wdf. Request. Create(&attributes, device. Context->Usb. Io. Target, &device. Context->Usb. Request); if (failure) return status; Initialize. Request. Context(device. Context->Usb. Request); if (failure) return status; . . . }

Handling I/O

Handling I/O

NTSTATUS On. Device. Add(…, PWDFDEVICE_INIT Device. Init) {. . . // After creating the

NTSTATUS On. Device. Add(…, PWDFDEVICE_INIT Device. Init) {. . . // After creating the device… WDF_IO_QUEUE_CONFIG queue. Config; WDF_IO_QUEUE_CONFIG_INIT_DEFAULT_QUEUE( &queue. Config, Wdf. Io. Queue. Dispatch. Sequential ); queue. Config. Evt. Io. Device. Control = On. Device. Control; Wdf. Io. Queue. Create(device, &queue. Config, WDF_NO_OBJECT_ATTRIBUTES, WDF_NO_OBJECT); … }

VOID On. Device. Control(WDFQUEUE Queue, WDFREQUEST Request, size_t Input. Cb, size_t Output. Cb, ULONG

VOID On. Device. Control(WDFQUEUE Queue, WDFREQUEST Request, size_t Input. Cb, size_t Output. Cb, ULONG Io. Control. Code) { if (Io. Control. Code != IOCTL_MYDRIVER_DO_SOMETHING) { goto error; } Wdf. Request. Retrieve. Input. Buffer(Request, sizeof(DO_SOMETHING_INPUT), &input, NULL); if (failure) goto error; // Validate input structure if (failure) goto error; // Start request running. if (failure) goto error; return; Error: Wdf. Request. Complete(Request, status); return;

Power Management

Power Management

NTSTATUS On. Device. Add(. . . ) { … WDF_PNPPOWER_EVENT_CALLBACKS power. Cb; WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&power. Cb);

NTSTATUS On. Device. Add(. . . ) { … WDF_PNPPOWER_EVENT_CALLBACKS power. Cb; WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&power. Cb); power. Cb. Evt. Device. D 0 Entry = On. D 0 Entry; power. Cb. Evt. Device. D 0 Exit = On. D 0 Exit; Wdf. Device. Init. Set. Pnp. Power. Event. Callbacks(Device. Init, &power. Cb); // create the device. . . WDF_DEVICE_POWER_POLICY_IDLE_SETTINGS idle. Settings; WDF_DEVICE_POWER_POLICY_IDLE_SETTINGS_INIT( &idle. Settings, Idle. Usb. Selective. Suspend ); idle. Settings. Idle. Timeout = 500; // milliseconds Wdf. Device. Assign. S 0 Idle. Settings(device, &idle. Settings); if (failure) return status; }

Summary

Summary

Developing Drivers with Windows Driver Foundation Kernel-Mode Driver Framework Design Guide User-Mode Driver Framework

Developing Drivers with Windows Driver Foundation Kernel-Mode Driver Framework Design Guide User-Mode Driver Framework Design Guide Choosing a driver model What’s New in Windows Driver Frameworks for Windows 8 wdfinfo@microsoft. com Mine WDF

http: //forums. dev. windows. com http: //bldw. in/Session. Feedback

http: //forums. dev. windows. com http: //bldw. in/Session. Feedback