-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrawpdo.c
369 lines (297 loc) · 11.1 KB
/
rawpdo.c
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
/*--
Copyright (c) Microsoft Corporation. All rights reserved.
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
PURPOSE.
Module Name:
RawPdo.c
Abstract: This module have the code enumerate a raw PDO for every device
the filter attaches to so that it can provide a direct
sideband communication with the usermode application.
The toaster filter driver sample demonstrates an alternation
approach where you can create one control-device for all the
instances of the filter device.
Environment:
Kernel mode only.
--*/
#include "kbfiltr.h"
#include "public.h"
VOID
KbFilter_EvtIoDeviceControlForRawPdo(
IN WDFQUEUE Queue,
IN WDFREQUEST Request,
IN size_t OutputBufferLength,
IN size_t InputBufferLength,
IN ULONG IoControlCode
)
/*++
Routine Description:
This routine is the dispatch routine for device control requests.
Arguments:
Queue - Handle to the framework queue object that is associated
with the I/O request.
Request - Handle to a framework request object.
OutputBufferLength - length of the request's output buffer,
if an output buffer is available.
InputBufferLength - length of the request's input buffer,
if an input buffer is available.
IoControlCode - the driver-defined or system-defined I/O control code
(IOCTL) that is associated with the request.
Return Value:
VOID
--*/
{
NTSTATUS status = STATUS_SUCCESS;
WDFDEVICE parent = WdfIoQueueGetDevice(Queue);
PRPDO_DEVICE_DATA pdoData;
WDF_REQUEST_FORWARD_OPTIONS forwardOptions;
pdoData = PdoGetData(parent);
UNREFERENCED_PARAMETER(OutputBufferLength);
UNREFERENCED_PARAMETER(InputBufferLength);
DebugPrint(("Entered KbFilter_EvtIoDeviceControlForRawPdo\n"));
//
// Process the ioctl and complete it when you are done.
// Since the queue is configured for serial dispatch, you will
// not receive another ioctl request until you complete this one.
//
switch (IoControlCode) {
case IOCTL_KBFILTR_GET_KEYBOARD_ATTRIBUTES:
WDF_REQUEST_FORWARD_OPTIONS_INIT(&forwardOptions);
status = WdfRequestForwardToParentDeviceIoQueue(Request, pdoData->ParentQueue, &forwardOptions);
if (!NT_SUCCESS(status)) {
WdfRequestComplete(Request, status);
}
break;
default:
WdfRequestComplete(Request, status);
break;
}
return;
}
#define MAX_ID_LEN 128
NTSTATUS
KbFiltr_CreateRawPdo(
WDFDEVICE Device,
ULONG InstanceNo
)
/*++
Routine Description:
This routine creates and initialize a PDO.
Arguments:
Return Value:
NT Status code.
--*/
{
NTSTATUS status;
PWDFDEVICE_INIT pDeviceInit = NULL;
PRPDO_DEVICE_DATA pdoData = NULL;
WDFDEVICE hChild = NULL;
WDF_OBJECT_ATTRIBUTES pdoAttributes;
WDF_DEVICE_PNP_CAPABILITIES pnpCaps;
WDF_IO_QUEUE_CONFIG ioQueueConfig;
WDFQUEUE queue;
WDF_DEVICE_STATE deviceState;
PDEVICE_EXTENSION devExt;
DECLARE_CONST_UNICODE_STRING(deviceId,KBFILTR_DEVICE_ID );
DECLARE_CONST_UNICODE_STRING(hardwareId,KBFILTR_DEVICE_ID );
DECLARE_CONST_UNICODE_STRING(deviceLocation,L"Keyboard Filter\0" );
DECLARE_UNICODE_STRING_SIZE(buffer, MAX_ID_LEN);
DebugPrint(("Entered KbFiltr_CreateRawPdo\n"));
//
// Allocate a WDFDEVICE_INIT structure and set the properties
// so that we can create a device object for the child.
//
pDeviceInit = WdfPdoInitAllocate(Device);
if (pDeviceInit == NULL) {
status = STATUS_INSUFFICIENT_RESOURCES;
goto Cleanup;
}
//
// Mark the device RAW so that the child device can be started
// and accessed without requiring a function driver. Since we are
// creating a RAW PDO, we must provide a class guid.
//
status = WdfPdoInitAssignRawDevice(pDeviceInit, &GUID_DEVCLASS_KEYBOARD);
if (!NT_SUCCESS(status)) {
goto Cleanup;
}
//
// Since keyboard is secure device, we must protect ourselves from random
// users sending ioctls and creating trouble.
//
status = WdfDeviceInitAssignSDDLString(pDeviceInit,
&SDDL_DEVOBJ_SYS_ALL_ADM_ALL);
if (!NT_SUCCESS(status)) {
goto Cleanup;
}
//
// Assign DeviceID - This will be reported to IRP_MN_QUERY_ID/BusQueryDeviceID
//
status = WdfPdoInitAssignDeviceID(pDeviceInit, &deviceId);
if (!NT_SUCCESS(status)) {
goto Cleanup;
}
//
// For RAW PDO, there is no need to provide BusQueryHardwareIDs
// and BusQueryCompatibleIDs IDs unless we are running on
// Windows 2000.
//
if (!RtlIsNtDdiVersionAvailable(NTDDI_WINXP)) {
//
// On Win2K, we must provide a HWID for the device to get enumerated.
// Since we are providing a HWID, we will have to provide a NULL inf
// to avoid the "found new device" popup and get the device installed
// silently.
//
status = WdfPdoInitAddHardwareID(pDeviceInit, &hardwareId);
if (!NT_SUCCESS(status)) {
goto Cleanup;
}
}
//
// We could be enumerating more than one children if the filter attaches
// to multiple instances of keyboard, so we must provide a
// BusQueryInstanceID. If we don't, system will throw CA bugcheck.
//
status = RtlUnicodeStringPrintf(&buffer, L"%02d", InstanceNo);
if (!NT_SUCCESS(status)) {
goto Cleanup;
}
status = WdfPdoInitAssignInstanceID(pDeviceInit, &buffer);
if (!NT_SUCCESS(status)) {
goto Cleanup;
}
//
// Provide a description about the device. This text is usually read from
// the device. In the case of USB device, this text comes from the string
// descriptor. This text is displayed momentarily by the PnP manager while
// it's looking for a matching INF. If it finds one, it uses the Device
// Description from the INF file to display in the device manager.
// Since our device is raw device and we don't provide any hardware ID
// to match with an INF, this text will be displayed in the device manager.
//
status = RtlUnicodeStringPrintf(&buffer,L"Keyboard_Filter_%02d", InstanceNo );
if (!NT_SUCCESS(status)) {
goto Cleanup;
}
//
// You can call WdfPdoInitAddDeviceText multiple times, adding device
// text for multiple locales. When the system displays the text, it
// chooses the text that matches the current locale, if available.
// Otherwise it will use the string for the default locale.
// The driver can specify the driver's default locale by calling
// WdfPdoInitSetDefaultLocale.
//
status = WdfPdoInitAddDeviceText(pDeviceInit,
&buffer,
&deviceLocation,
0x409
);
if (!NT_SUCCESS(status)) {
goto Cleanup;
}
WdfPdoInitSetDefaultLocale(pDeviceInit, 0x409);
//
// Initialize the attributes to specify the size of PDO device extension.
// All the state information private to the PDO will be tracked here.
//
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&pdoAttributes, RPDO_DEVICE_DATA);
//
// Set up our queue to allow forwarding of requests to the parent
// This is done so that the cached Keyboard Attributes can be retrieved
//
WdfPdoInitAllowForwardingRequestToParent(pDeviceInit);
status = WdfDeviceCreate(&pDeviceInit, &pdoAttributes, &hChild);
if (!NT_SUCCESS(status)) {
goto Cleanup;
}
//
// Get the device context.
//
pdoData = PdoGetData(hChild);
pdoData->InstanceNo = InstanceNo;
//
// Get the parent queue we will be forwarding to
//
devExt = FilterGetData(Device);
pdoData->ParentQueue = devExt->rawPdoQueue;
//
// Configure the default queue associated with the control device object
// to be Serial so that request passed to EvtIoDeviceControl are serialized.
// A default queue gets all the requests that are not
// configure-fowarded using WdfDeviceConfigureRequestDispatching.
//
WDF_IO_QUEUE_CONFIG_INIT_DEFAULT_QUEUE(&ioQueueConfig,
WdfIoQueueDispatchSequential);
ioQueueConfig.EvtIoDeviceControl = KbFilter_EvtIoDeviceControlForRawPdo;
status = WdfIoQueueCreate(hChild,
&ioQueueConfig,
WDF_NO_OBJECT_ATTRIBUTES,
&queue // pointer to default queue
);
if (!NT_SUCCESS(status)) {
DebugPrint( ("WdfIoQueueCreate failed 0x%x\n", status));
goto Cleanup;
}
//
// Set some properties for the child device.
//
WDF_DEVICE_PNP_CAPABILITIES_INIT(&pnpCaps);
pnpCaps.Removable = WdfTrue;
pnpCaps.SurpriseRemovalOK = WdfTrue;
pnpCaps.NoDisplayInUI = WdfTrue;
pnpCaps.Address = InstanceNo;
pnpCaps.UINumber = InstanceNo;
WdfDeviceSetPnpCapabilities(hChild, &pnpCaps);
//
// TODO: In addition to setting NoDisplayInUI in DeviceCaps, we
// have to do the following to hide the device. Following call
// tells the framework to report the device state in
// IRP_MN_QUERY_DEVICE_STATE request.
//
WDF_DEVICE_STATE_INIT(&deviceState);
deviceState.DontDisplayInUI = WdfTrue;
WdfDeviceSetDeviceState(hChild, &deviceState);
//
// Tell the Framework that this device will need an interface so that
// application can find our device and talk to it.
//
status = WdfDeviceCreateDeviceInterface(
hChild,
&GUID_DEVINTERFACE_KBFILTER,
NULL
);
if (!NT_SUCCESS (status)) {
DebugPrint( ("WdfDeviceCreateDeviceInterface failed 0x%x\n", status));
goto Cleanup;
}
//
// Add this device to the FDO's collection of children.
// After the child device is added to the static collection successfully,
// driver must call WdfPdoMarkMissing to get the device deleted. It
// shouldn't delete the child device directly by calling WdfObjectDelete.
//
status = WdfFdoAddStaticChild(Device, hChild);
if (!NT_SUCCESS(status)) {
goto Cleanup;
}
//
// pDeviceInit will be freed by WDF.
//
return STATUS_SUCCESS;
Cleanup:
DebugPrint(("KbFiltr_CreatePdo failed %x\n", status));
//
// Call WdfDeviceInitFree if you encounter an error while initializing
// a new framework device object. If you call WdfDeviceInitFree,
// do not call WdfDeviceCreate.
//
if (pDeviceInit != NULL) {
WdfDeviceInitFree(pDeviceInit);
}
if(hChild) {
WdfObjectDelete(hChild);
}
return status;
}