-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathQueue.c
executable file
·436 lines (331 loc) · 12.5 KB
/
Queue.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
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
/*++
Module Name:
queue.c
Abstract:
This file contains the queue entry points and callbacks.
Environment:
User-mode Driver Framework 2
--*/
#include "Driver.h"
#include "Device.h"
#include "Queue.tmh"
NTSTATUS
QueueCreate(
_In_ WDFDEVICE Device,
_Out_ WDFQUEUE* Queue
)
/*++
Routine Description:
This function creates a default, parallel I/O queue to proces IOCTLs
from hidclass.sys.
Arguments:
Device - Handle to a framework device object.
Queue - Output pointer to a framework I/O queue handle, on success.
Return Value:
NTSTATUS
--*/
{
NTSTATUS status;
WDF_IO_QUEUE_CONFIG queueConfig;
WDF_OBJECT_ATTRIBUTES queueAttributes;
WDFQUEUE queue;
PQUEUE_CONTEXT queueContext;
WDF_IO_QUEUE_CONFIG_INIT_DEFAULT_QUEUE(
&queueConfig,
WdfIoQueueDispatchParallel);
#ifdef _KERNEL_MODE
queueConfig.EvtIoInternalDeviceControl = EvtIoDeviceControl;
#else
//
// HIDclass uses INTERNAL_IOCTL which is not supported by UMDF. Therefore
// the hidumdf.sys changes the IOCTL type to DEVICE_CONTROL for next stack
// and sends it down
//
queueConfig.EvtIoDeviceControl = EvtIoDeviceControl;
#endif
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(
&queueAttributes,
QUEUE_CONTEXT);
status = WdfIoQueueCreate(
Device,
&queueConfig,
&queueAttributes,
&queue);
if (!NT_SUCCESS(status)) {
TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "WdfIoQueueCreate failed %!STATUS!", status);
return status;
}
queueContext = GetQueueContext(queue);
queueContext->Queue = queue;
queueContext->DeviceContext = GetDeviceContext(Device);
memset(&queueContext->LastXgmReport, 0, sizeof(queueContext->LastXgmReport));
*Queue = queue;
return status;
}
VOID
EvtIoDeviceControl(
_In_ WDFQUEUE Queue,
_In_ WDFREQUEST Request,
_In_ size_t OutputBufferLength,
_In_ size_t InputBufferLength,
_In_ ULONG IoControlCode
)
/*++
Routine Description:
This event callback function is called when the driver receives an
(KMDF) IOCTL_HID_Xxx code when handlng IRP_MJ_INTERNAL_DEVICE_CONTROL
(UMDF) IOCTL_HID_Xxx, IOCTL_UMDF_HID_Xxx when handling IRP_MJ_DEVICE_CONTROL
Arguments:
Queue - A handle to the queue object that is associated with the I/O request
Request - A handle to a framework request object.
OutputBufferLength - The length, in bytes, of the request's output buffer,
if an output buffer is available.
InputBufferLength - The length, in bytes, of the request's input buffer, if
an input buffer is available.
IoControlCode - The driver or system defined IOCTL associated with the request
Return Value:
NTSTATUS
--*/
{
NTSTATUS status;
BOOLEAN completeRequest = TRUE;
WDFDEVICE device = WdfIoQueueGetDevice(Queue);
PDEVICE_CONTEXT deviceContext = NULL;
PQUEUE_CONTEXT queueContext = GetQueueContext(Queue);
UNREFERENCED_PARAMETER(OutputBufferLength);
UNREFERENCED_PARAMETER(InputBufferLength);
deviceContext = GetDeviceContext(device);
switch (IoControlCode)
{
case IOCTL_HID_GET_DEVICE_DESCRIPTOR: // METHOD_NEITHER
//
// Retrieves the device's HID descriptor.
//
_Analysis_assume_(deviceContext->HidDescriptor.bLength != 0);
status = RequestCopyFromBuffer(Request,
&deviceContext->HidDescriptor,
deviceContext->HidDescriptor.bLength);
break;
case IOCTL_HID_GET_DEVICE_ATTRIBUTES: // METHOD_NEITHER
//
//Retrieves a device's attributes in a HID_DEVICE_ATTRIBUTES structure.
//
status = RequestCopyFromBuffer(Request,
&queueContext->DeviceContext->HidDeviceAttributes,
sizeof(HID_DEVICE_ATTRIBUTES));
break;
case IOCTL_HID_GET_REPORT_DESCRIPTOR: // METHOD_NEITHER
//
//Obtains the report descriptor for the HID device.
//
status = RequestCopyFromBuffer(Request,
deviceContext->ReportDescriptor,
deviceContext->HidDescriptor.DescriptorList[0].wReportLength);
break;
case IOCTL_HID_READ_REPORT: // METHOD_NEITHER
//
// Returns a report from the device into a class driver-supplied
// buffer.
//
status = ReadReport(queueContext, Request, &completeRequest);
break;
case IOCTL_HID_WRITE_REPORT: // METHOD_NEITHER
//
// Transmits a class driver-supplied report to the device.
//
status = WriteReport(queueContext, Request);
break;
#ifdef _KERNEL_MODE
case IOCTL_HID_GET_FEATURE: // METHOD_OUT_DIRECT
status = GetFeature(queueContext, Request);
break;
case IOCTL_HID_SET_FEATURE: // METHOD_IN_DIRECT
status = SetFeature(queueContext, Request);
break;
case IOCTL_HID_GET_INPUT_REPORT: // METHOD_OUT_DIRECT
status = GetInputReport(queueContext, Request);
break;
case IOCTL_HID_SET_OUTPUT_REPORT: // METHOD_IN_DIRECT
status = SetOutputReport(queueContext, Request);
break;
#else // UMDF specific
//
// HID minidriver IOCTL uses HID_XFER_PACKET which contains an embedded pointer.
//
// typedef struct _HID_XFER_PACKET {
// PUCHAR reportBuffer;
// ULONG reportBufferLen;
// UCHAR reportId;
// } HID_XFER_PACKET, *PHID_XFER_PACKET;
//
// UMDF cannot handle embedded pointers when marshalling buffers between processes.
// Therefore a special driver mshidumdf.sys is introduced to convert such IRPs to
// new IRPs (with new IOCTL name like IOCTL_UMDF_HID_Xxxx) where:
//
// reportBuffer - passed as one buffer inside the IRP
// reportId - passed as a second buffer inside the IRP
//
// The new IRP is then passed to UMDF host and driver for further processing.
//
case IOCTL_UMDF_HID_GET_FEATURE: // METHOD_NEITHER
status = GetFeature(queueContext, Request);
break;
case IOCTL_UMDF_HID_SET_FEATURE: // METHOD_NEITHER
status = SetFeature(queueContext, Request);
break;
case IOCTL_UMDF_HID_GET_INPUT_REPORT: // METHOD_NEITHER
status = GetInputReport(queueContext, Request);
break;
case IOCTL_UMDF_HID_SET_OUTPUT_REPORT: // METHOD_NEITHER
status = SetOutputReport(queueContext, Request);
break;
#endif // _KERNEL_MODE
case IOCTL_HID_GET_STRING: // METHOD_NEITHER
status = GetString(Request);
break;
case IOCTL_HID_GET_INDEXED_STRING: // METHOD_OUT_DIRECT
status = GetIndexedString(Request);
break;
case IOCTL_HID_SEND_IDLE_NOTIFICATION_REQUEST: // METHOD_NEITHER
//
// This has the USBSS Idle notification callback. If the lower driver
// can handle it (e.g. USB stack can handle it) then pass it down
// otherwise complete it here as not inplemented. For a virtual
// device, idling is not needed.
//
// Not implemented. fall through...
//
case IOCTL_HID_ACTIVATE_DEVICE: // METHOD_NEITHER
case IOCTL_HID_DEACTIVATE_DEVICE: // METHOD_NEITHER
case IOCTL_GET_PHYSICAL_DESCRIPTOR: // METHOD_OUT_DIRECT
//
// We don't do anything for these IOCTLs but some minidrivers might.
//
// Not implemented. fall through...
//
default:
status = STATUS_NOT_IMPLEMENTED;
break;
}
//
// Complete the request. Information value has already been set by request
// handlers.
//
if (completeRequest) {
WdfRequestComplete(Request, status);
}
}
NTSTATUS
ManualQueueCreate(
_In_ WDFDEVICE Device,
_Out_ WDFQUEUE* Queue
)
/*++
Routine Description:
This function creates a manual I/O queue to receive IOCTL_HID_READ_REPORT
forwarded from the device's default queue handler.
It also creates a periodic timer to check the queue and complete any pending
request with data from the device. Here timer expiring is used to simulate
a hardware event that new data is ready.
The workflow is like this:
- Hidclass.sys sends an ioctl to the miniport to read input report.
- The request reaches the driver's default queue. As data may not be avaiable
yet, the request is forwarded to a second manual queue temporarily.
- Later when data is ready (as simulated by timer expiring), the driver
checks for any pending request in the manual queue, and then completes it.
- Hidclass gets notified for the read request completion and return data to
the caller.
On the other hand, for IOCTL_HID_WRITE_REPORT request, the driver simply
sends the request to the hardware (as simulated by storing the data at
DeviceContext->DeviceData) and completes the request immediately. There is
no need to use another queue for write operation.
Arguments:
Device - Handle to a framework device object.
Queue - Output pointer to a framework I/O queue handle, on success.
Return Value:
NTSTATUS
--*/
{
NTSTATUS status;
WDF_IO_QUEUE_CONFIG queueConfig;
WDF_OBJECT_ATTRIBUTES queueAttributes;
WDFQUEUE queue;
PMANUAL_QUEUE_CONTEXT queueContext;
WDF_TIMER_CONFIG timerConfig;
WDF_OBJECT_ATTRIBUTES timerAttributes;
ULONG timerPeriodInSeconds = 5;
WDF_IO_QUEUE_CONFIG_INIT(
&queueConfig,
WdfIoQueueDispatchManual);
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(
&queueAttributes,
MANUAL_QUEUE_CONTEXT);
status = WdfIoQueueCreate(
Device,
&queueConfig,
&queueAttributes,
&queue);
if (!NT_SUCCESS(status)) {
TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "WdfIoQueueCreate failed %!STATUS!", status);
return status;
}
queueContext = GetManualQueueContext(queue);
queueContext->Queue = queue;
queueContext->DeviceContext = GetDeviceContext(Device);
WDF_TIMER_CONFIG_INIT_PERIODIC(
&timerConfig,
EvtTimerFunc,
timerPeriodInSeconds * 1000);
WDF_OBJECT_ATTRIBUTES_INIT(&timerAttributes);
timerAttributes.ParentObject = queue;
status = WdfTimerCreate(&timerConfig,
&timerAttributes,
&queueContext->Timer);
if (!NT_SUCCESS(status)) {
TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "WdfTimerCreate failed %!STATUS!", status);
return status;
}
WdfTimerStart(queueContext->Timer, WDF_REL_TIMEOUT_IN_SEC(1));
*Queue = queue;
return status;
}
void
EvtTimerFunc(
_In_ WDFTIMER Timer
)
/*++
Routine Description:
This periodic timer callback routine checks the device's manual queue and
completes any pending request with data from the device.
Arguments:
Timer - Handle to a timer object that was obtained from WdfTimerCreate.
Return Value:
VOID
--*/
{
NTSTATUS status;
WDFQUEUE queue;
PMANUAL_QUEUE_CONTEXT queueContext;
WDFREQUEST request;
//HIDMINI_INPUT_REPORT readReport;
TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "Enter %!FUNC!");
queue = (WDFQUEUE)WdfTimerGetParentObject(Timer);
queueContext = GetManualQueueContext(queue);
//
// see if we have a request in manual queue
//
status = WdfIoQueueRetrieveNextRequest(
queueContext->Queue,
&request);
if (NT_SUCCESS(status)) {
/*
readReport.ReportId = CONTROL_FEATURE_REPORT_ID;
readReport.Data = queueContext->DeviceContext->DeviceData;
status = RequestCopyFromBuffer(request,
&readReport,
sizeof(readReport));
*/
status = STATUS_NOT_IMPLEMENTED;
WdfRequestComplete(request, status);
}
}