1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
Module Name:
wdf.c
Abstract:
The module implements WDF boilerplate for the simulate power limit client.
--*/
//-------------------------------------------------------------------- Includes
#include "plclient.h"
//--------------------------------------------------------------------- Globals
WDFWAITLOCK GlobalMutex;
//------------------------------------------------------------------ Prototypes
DRIVER_INITIALIZE DriverEntry;
EVT_WDF_DRIVER_DEVICE_ADD EvtDriverDeviceAdd;
EVT_WDF_DRIVER_UNLOAD EvtDriverUnload;
EVT_WDF_OBJECT_CONTEXT_DESTROY EvtDeviceDestroy;
EVT_WDF_IO_QUEUE_IO_DEVICE_CONTROL EvtIoDeviceControl;
//--------------------------------------------------------------------- Pragmas
#pragma alloc_text(INIT, DriverEntry)
#pragma alloc_text(PAGE, EvtDriverDeviceAdd)
#pragma alloc_text(PAGE, EvtDriverUnload)
#pragma alloc_text(PAGE, EvtIoDeviceControl)
#pragma alloc_text(PAGE, EvtDeviceDestroy)
//------------------------------------------------------------------- Functions
_Use_decl_annotations_
NTSTATUS
DriverEntry (
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPath
)
/*++
Routine Description:
DriverEntry initializes the driver and is the first routine called by the
system after the driver is loaded. DriverEntry configures and creates a WDF
driver object.
Parameters Description:
DriverObject - Supplies a pointer to the driver object.
RegistryPath - Supplies a pointer to a unicode string representing the path
to the driver-specific key in the registry.
Return Value:
NTSTATUS.
--*/
{
WDF_DRIVER_CONFIG DriverConfig;
NTSTATUS Status;
UNREFERENCED_PARAMETER(RegistryPath);
//
// Initiialize the DriverConfig data that controls the attributes that are
// global to this driver.
//
DebugEnter();
WDF_DRIVER_CONFIG_INIT(&DriverConfig, EvtDriverDeviceAdd);
DriverConfig.EvtDriverUnload = EvtDriverUnload;
//
// Create the driver object
//
Status = WdfDriverCreate(DriverObject,
RegistryPath,
WDF_NO_OBJECT_ATTRIBUTES,
&DriverConfig,
WDF_NO_HANDLE);
if (!NT_SUCCESS(Status)) {
DebugPrint(PLCLIENT_PRINT_ERROR,
"WdfDriverCreate() Failed. Status 0x%x\n",
Status);
goto DriverEntryEnd;
}
//
// Initialize global mutex.
//
Status = WdfWaitLockCreate(WDF_NO_OBJECT_ATTRIBUTES, &GlobalMutex);
if (!NT_SUCCESS(Status)) {
DebugPrint(PLCLIENT_PRINT_ERROR,
"WdfWaitLockCreate() Failed! 0x%x\n",
Status);
goto DriverEntryEnd;
}
DriverEntryEnd:
DebugExitStatus(Status);
return Status;
}
_Use_decl_annotations_
NTSTATUS
EvtDriverDeviceAdd (
WDFDRIVER Driver,
PWDFDEVICE_INIT DeviceInit
)
/*++
Routine Description:
This routine is called by the framework in response to AddDevice call from
the PnP manager. A WDF device object is created and initialized to represent
a new instance of the power limit client device.
Arguments:
Driver - Supplies a handle to the WDF Driver object.
DeviceInit - Supplies a pointer to a framework-allocated WDFDEVICE_INIT structure.
Return Value:
NTSTATUS
--*/
{
PFDO_DATA DevExt;
WDF_OBJECT_ATTRIBUTES DeviceAttributes;
WDFDEVICE DeviceHandle;
POWER_LIMIT_INTERFACE PowerLimitInterface;
WDFQUEUE Queue;
WDF_IO_QUEUE_CONFIG QueueConfig;
WDF_QUERY_INTERFACE_CONFIG QueryInterfaceConfig;
NTSTATUS Status;
UNREFERENCED_PARAMETER(Driver);
PAGED_CODE();
DevExt = NULL;
DebugEnter();
//
// Initialize attributes and a context area for the device object.
//
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&DeviceAttributes, FDO_DATA);
DeviceAttributes.EvtDestroyCallback = &EvtDeviceDestroy;
//
// Create a framework device object. This call will in turn create
// a WDM device object, attach to the lower stack, and set the
// appropriate flags and attributes.
//
Status = WdfDeviceCreate(&DeviceInit, &DeviceAttributes, &DeviceHandle);
if (!NT_SUCCESS(Status)) {
DebugPrint(PLCLIENT_PRINT_ERROR,
"WdfDeviceCreate() Failed. 0x%x\n",
Status);
goto DriverDeviceAddEnd;
}
//
// Configure a default queue for IO requests. This queue processes requests
// to read the simulated state.
//
// N.B. Those IOCTLs supplies another approach to validate device driver
// interface, which are not needed by production code.
//
WDF_IO_QUEUE_CONFIG_INIT_DEFAULT_QUEUE(&QueueConfig,
WdfIoQueueDispatchSequential);
QueueConfig.EvtIoDeviceControl = EvtIoDeviceControl;
Status = WdfIoQueueCreate(DeviceHandle,
&QueueConfig,
WDF_NO_OBJECT_ATTRIBUTES,
&Queue);
if (!NT_SUCCESS(Status)) {
DebugPrint(PLCLIENT_PRINT_ERROR,
"WdfIoQueueCreate() Failed. 0x%x\n",
Status);
goto DriverDeviceAddEnd;
}
//
// Initialize the device extension.
//
DevExt = GetDeviceExtension(DeviceHandle);
Status = InitPowerLimitValues(DevExt);
if (!NT_SUCCESS(Status)) {
DebugPrint(PLCLIENT_PRINT_ERROR,
"InitPowerLimitValues() Failed. 0x%x\n",
Status);
goto DriverDeviceAddEnd;
}
//
// Create a device interface for this device to advertise the simulated
// power limit client IO interface.
//
Status = WdfDeviceCreateDeviceInterface(
DeviceHandle,
&GUID_DEVINTERFACE_POWER_LIMIT,
NULL);
if (!NT_SUCCESS(Status)) {
goto DriverDeviceAddEnd;
}
//
// Create a driver interface for this device to advertise the power limit
// interface.
//
RtlZeroMemory(&PowerLimitInterface, sizeof(PowerLimitInterface));
PowerLimitInterface.Version = 1;
PowerLimitInterface.Size = sizeof(PowerLimitInterface);
PowerLimitInterface.Context = DeviceHandle;
PowerLimitInterface.InterfaceReference = WdfDeviceInterfaceReferenceNoOp;
PowerLimitInterface.InterfaceDereference = WdfDeviceInterfaceDereferenceNoOp;
PowerLimitInterface.DomainCount = PLCLIENT_DEFAULT_DOMAIN_COUNT;
PowerLimitInterface.QueryAttributes = PLCQueryAttributes;
PowerLimitInterface.SetPowerLimit = PLCSetLimits;
PowerLimitInterface.QueryPowerLimit = PLCQueryLimitValues;
WDF_QUERY_INTERFACE_CONFIG_INIT(&QueryInterfaceConfig,
(PINTERFACE)&PowerLimitInterface,
&GUID_POWER_LIMIT_INTERFACE,
NULL);
Status = WdfDeviceAddQueryInterface(DeviceHandle, &QueryInterfaceConfig);
if (!NT_SUCCESS(Status)) {
DebugPrint(PLCLIENT_PRINT_ERROR,
"WdfDeviceAddQueryInterface() Failed. 0x%x\n",
Status);
goto DriverDeviceAddEnd;
}
DriverDeviceAddEnd:
if (!NT_SUCCESS(Status)) {
CleanupPowerLimitValues(DevExt);
}
DebugExitStatus(Status);
return Status;
}
_Use_decl_annotations_
VOID
EvtDeviceDestroy (
WDFOBJECT Object
)
/*++
Routine Description:
This routine destroys the device's data.
Arguments:
Object - Supplies the WDF reference to the device that is being removed.
Return Value:
None.
--*/
{
PFDO_DATA DevExt;
PAGED_CODE();
DebugEnter();
DevExt = GetDeviceExtension(Object);
CleanupPowerLimitValues(DevExt);
DebugExit();
return;
}
_Use_decl_annotations_
VOID
EvtIoDeviceControl (
WDFQUEUE Queue,
WDFREQUEST Request,
size_t OutputBufferLength,
size_t InputBufferLength,
ULONG IoControlCode
)
/*++
Routine Description:
Handles requests to read the simulated device state.
Arguments:
Queue - Supplies a handle to the framework queue object that is associated
with the I/O request.
Request - Supplies a handle to a framework request object. This one
represents the IRP_MJ_DEVICE_CONTROL IRP received by the framework.
OutputBufferLength - Supplies the length, in bytes, of the request's output
buffer, if an output buffer is available.
InputBufferLength - Supplies the length, in bytes, of the request's input
buffer, if an input buffer is available.
IoControlCode - Supplies the Driver-defined or system-defined I/O control
code (IOCTL) that is associated with the request.
Return Value:
VOID
--*/
{
ULONG BytesReturned;
WDFDEVICE Device;
PFDO_DATA DevExt;
PVOID OutputBuffer;
NTSTATUS Status;
UNREFERENCED_PARAMETER(InputBufferLength);
PAGED_CODE();
Device = WdfIoQueueGetDevice(Queue);
DevExt = GetDeviceExtension(Device);
DebugPrint(PLCLIENT_PRINT_TRACE,
"EvtIoDeviceControl: 0x%08x\n",
IoControlCode);
BytesReturned = 0;
OutputBuffer = NULL;
if (OutputBufferLength > 0) {
Status = WdfRequestRetrieveOutputBuffer(Request,
OutputBufferLength,
&OutputBuffer,
NULL);
if (!NT_SUCCESS(Status)) {
goto DeviceIoControlEnd;
}
}
Status = STATUS_NOT_SUPPORTED;
switch(IoControlCode) {
case IOCTL_POWERLIMIT_CLIENT_QUERY_LIMIT_COUNT:
if (OutputBufferLength == sizeof(ULONG)) {
AcquireGlobalMutex();
*((PULONG)OutputBuffer) = DevExt->LimitCount;
BytesReturned = sizeof(ULONG);
ReleaseGlobalMutex();
Status = STATUS_SUCCESS;
} else {
Status = STATUS_BUFFER_OVERFLOW;
}
break;
case IOCTL_POWERLIMIT_CLIENT_QUERY_ATTRIBUTES:
if (OutputBufferLength == sizeof(POWER_LIMIT_ATTRIBUTES) * DevExt->LimitCount) {
AcquireGlobalMutex();
RtlCopyMemory(OutputBuffer, DevExt->LimitAttributes, OutputBufferLength);
ReleaseGlobalMutex();
BytesReturned = (ULONG)OutputBufferLength;
Status = STATUS_SUCCESS;
} else {
Status = STATUS_BUFFER_OVERFLOW;
}
break;
case IOCTL_POWERLIMIT_CLIENT_QUERY_LIMITS:
if (OutputBufferLength == sizeof(POWER_LIMIT_VALUE) * DevExt->LimitCount) {
AcquireGlobalMutex();
RtlCopyMemory(OutputBuffer, DevExt->LimitValues, OutputBufferLength);
ReleaseGlobalMutex();
BytesReturned = (ULONG)OutputBufferLength;
Status = STATUS_SUCCESS;
} else {
Status = STATUS_BUFFER_OVERFLOW;
}
break;
default:
break;
}
DeviceIoControlEnd:
WdfRequestCompleteWithInformation(Request, Status, BytesReturned);
DebugExitStatus(Status);
return;
}
_Use_decl_annotations_
VOID
EvtDriverUnload (
WDFDRIVER Driver
)
/*++
Routine Description:
EvtDriverUnload is called when the driver is being unloaded to clean up
driver state.
Arguments:
Driver - Supplies a handle to the WDF Driver object.
Return Value:
None
--*/
{
UNREFERENCED_PARAMETER(Driver);
PAGED_CODE();
DebugEnter();
//
// N.B. Does nothing since we don't have anything to clean up, just print
// some debug info.
//
DebugExit();
return;
}
|