/
Dmf_VirtualHidMini.c
1591 lines (1239 loc) · 45.1 KB
/
Dmf_VirtualHidMini.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
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT license.
Module Name:
Dmf_VirtualHidMini.c
Abstract:
Provides functionality from the VHIDMINI2 sample. This allows Client to create virtual HID device
in both Kernel-mode and User-mode.
Environment:
Kernel-mode Driver Framework
User-mode Driver Framework
--*/
// DMF and this Module's Library specific definitions.
//
#include "DmfModule.h"
#include "DmfModules.Library.h"
#include "DmfModules.Library.Trace.h"
#if defined(DMF_INCLUDE_TMH)
#include "Dmf_VirtualHidMini.tmh"
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////
// Module Private Enumerations and Structures
///////////////////////////////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////////////////////////////
// Module Private Context
///////////////////////////////////////////////////////////////////////////////////////////////////////
//
typedef struct _DMF_CONTEXT_VirtualHidMini
{
// This Module automatically queues Read requests. They are periodically
// dequeued. Then, data to copy into the requests is retrieved from the Client.
//
WDFQUEUE ManualQueue;
} DMF_CONTEXT_VirtualHidMini;
// This macro declares the following function:
// DMF_CONTEXT_GET()
//
DMF_MODULE_DECLARE_CONTEXT(VirtualHidMini)
// This macro declares the following function:
// DMF_CONFIG_GET()
//
DMF_MODULE_DECLARE_CONFIG(VirtualHidMini)
// MemoryTag.
//
#define MemoryTag 'mDHV'
///////////////////////////////////////////////////////////////////////////////////////////////////////
// DMF Module Support Code
///////////////////////////////////////////////////////////////////////////////////////////////////////
//
typedef struct _MANUAL_QUEUE_CONTEXT
{
WDFQUEUE Queue;
DMFMODULE DmfModule;
} MANUAL_QUEUE_CONTEXT;
WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(MANUAL_QUEUE_CONTEXT, ManualQueueContextGet);
#if !defined(DMF_USER_MODE)
// First let's review Buffer Descriptions for I/O Control Codes:
//
// METHOD_BUFFERED
// - Input buffer: Irp->AssociatedIrp.SystemBuffer
// - Output buffer: Irp->AssociatedIrp.SystemBuffer
//
// METHOD_IN_DIRECT or METHOD_OUT_DIRECT
// - Input buffer: Irp->AssociatedIrp.SystemBuffer
// - Second buffer: Irp->MdlAddress
//
// METHOD_NEITHER
// - Input buffer: Parameters.DeviceIoControl.Type3InputBuffer;
// - Output buffer: Irp->UserBuffer
//
// HID minidriver IOCTL stores a pointer to HID_XFER_PACKET in Irp->UserBuffer.
// For IOCTLs like IOCTL_HID_GET_FEATURE (which is METHOD_OUT_DIRECT) this is
// not the expected buffer location. So we cannot retrieve UserBuffer from the
// IRP using WdfRequestXxx functions. Instead, we have to escape to WDM.
//
NTSTATUS
VirtualHidMini_RequestGetHidXferPacket_ToReadFromDevice(
_In_ WDFREQUEST Request,
_Out_ HID_XFER_PACKET* Packet
)
/*++
Routine Description:
Copy a HID_XFER_PACKET from a given WDFREQUEST in Kernel-mode using
the above information.
Arguments:
Request - The given WDFREQUEST.
Packet - Target buffer where HID_XFER_PACKET data is written.
Return Value:
NTSTATUS
--*/
{
NTSTATUS ntStatus;
WDF_REQUEST_PARAMETERS requestParameters;
WDF_REQUEST_PARAMETERS_INIT(&requestParameters);
WdfRequestGetParameters(Request,
&requestParameters);
if (requestParameters.Parameters.DeviceIoControl.OutputBufferLength < sizeof(HID_XFER_PACKET))
{
ntStatus = STATUS_BUFFER_TOO_SMALL;
TraceEvents(TRACE_LEVEL_ERROR, DMF_TRACE, "Invalid HID_XFER_PACKET");
goto Exit;
}
RtlCopyMemory(Packet,
WdfRequestWdmGetIrp(Request)->UserBuffer,
sizeof(HID_XFER_PACKET));
ntStatus = STATUS_SUCCESS;
Exit:
return ntStatus;
}
NTSTATUS
VirtualHidMini_RequestGetHidXferPacket_ToWriteToDevice(
_In_ WDFREQUEST Request,
_Out_ HID_XFER_PACKET* Packet
)
/*++
Routine Description:
Copy a HID_XFER_PACKET from a given WDFREQUEST in Kernel-mode using
the above information.
Arguments:
Request - The given WDFREQUEST.
Packet - Target buffer where HID_XFER_PACKET data is written.
Return Value:
NTSTATUS
--*/
{
NTSTATUS ntStatus;
WDF_REQUEST_PARAMETERS params;
WDF_REQUEST_PARAMETERS_INIT(¶ms);
WdfRequestGetParameters(Request, ¶ms);
if (params.Parameters.DeviceIoControl.InputBufferLength < sizeof(HID_XFER_PACKET))
{
ntStatus = STATUS_BUFFER_TOO_SMALL;
TraceEvents(TRACE_LEVEL_ERROR, DMF_TRACE, "Invalid HID_XFER_PACKET");
goto Exit;
}
RtlCopyMemory(Packet,
WdfRequestWdmGetIrp(Request)->UserBuffer,
sizeof(HID_XFER_PACKET));
ntStatus = STATUS_SUCCESS;
Exit:
return ntStatus;
}
#else
// 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.
//
NTSTATUS
VirtualHidMini_RequestGetHidXferPacket_ToReadFromDevice(
_In_ WDFREQUEST Request,
_Out_ HID_XFER_PACKET* Packet
)
/*++
Routine Description:
Copy a HID_XFER_PACKET from a given WDFREQUEST in User-mode using
the above information.
NOTE:
Driver needs to write to the output buffer (so that application can read from it).
Report Buffer: Output Buffer
Report Id : Input Buffer
Arguments:
Request - The given WDFREQUEST.
Packet - Target buffer where HID_XFER_PACKET data is written.
Return Value:
NTSTATUS
--*/
{
NTSTATUS ntStatus;
WDFMEMORY inputMemory;
WDFMEMORY outputMemory;
size_t inputBufferLength;
size_t outputBufferLength;
PVOID inputBuffer;
PVOID outputBuffer;
// Get report Id from input buffer.
//
ntStatus = WdfRequestRetrieveInputMemory(Request,
&inputMemory);
if (! NT_SUCCESS(ntStatus) )
{
TraceEvents(TRACE_LEVEL_ERROR, DMF_TRACE, "WdfRequestRetrieveInputMemory fails: ntStatus=%!STATUS!", ntStatus);
goto Exit;
}
inputBuffer = WdfMemoryGetBuffer(inputMemory,
&inputBufferLength);
if (inputBufferLength < sizeof(UCHAR))
{
ntStatus = STATUS_INVALID_BUFFER_SIZE;
TraceEvents(TRACE_LEVEL_ERROR,
DMF_TRACE,
"WdfRequestRetrieveInputMemory fails: invalid input buffer. size %d expect %d",
(int)inputBufferLength,
(int)sizeof(UCHAR));
goto Exit;
}
Packet->reportId = *(UCHAR*)inputBuffer;
// Get report buffer from output buffer.
//
ntStatus = WdfRequestRetrieveOutputMemory(Request,
&outputMemory);
if (! NT_SUCCESS(ntStatus) )
{
TraceEvents(TRACE_LEVEL_ERROR, DMF_TRACE, "WdfRequestRetrieveOutputMemory fails: ntStatus=%!STATUS!", ntStatus);
goto Exit;
}
outputBuffer = WdfMemoryGetBuffer(outputMemory, &outputBufferLength);
Packet->reportBuffer = (UCHAR*) outputBuffer;
Packet->reportBufferLen = (ULONG) outputBufferLength;
Exit:
return ntStatus;
}
NTSTATUS
VirtualHidMini_RequestGetHidXferPacket_ToWriteToDevice(
_In_ WDFREQUEST Request,
_Out_ HID_XFER_PACKET* Packet
)
/*++
Routine Description:
Copy a HID_XFER_PACKET from a given WDFREQUEST in User-mode using
the above information.
NOTE:
Driver needs to read from the input buffer (which was written by application).
Report Buffer: Input Buffer
Report Id : Output Buffer Length
Note that the report id is not stored inside the output buffer, as the
driver has no read-access right to the output buffer, and trying to read
from the buffer will cause an access violation error.
The workaround is to store the report id in the OutputBufferLength field,
to which the driver does have read-access right.
Arguments:
Request - The given WDFREQUEST.
Packet - Target buffer where HID_XFER_PACKET data is written.
Return Value:
NTSTATUS
--*/
{
NTSTATUS ntStatus;
WDFMEMORY inputMemory;
WDFMEMORY outputMemory;
size_t inputBufferLength;
size_t outputBufferLength;
PVOID inputBuffer;
// Get report Id from output buffer length.
//
ntStatus = WdfRequestRetrieveOutputMemory(Request,
&outputMemory);
if ( !NT_SUCCESS(ntStatus) )
{
TraceEvents(TRACE_LEVEL_ERROR, DMF_TRACE, "WdfRequestRetrieveOutputMemory fails: ntStatus=%!STATUS!", ntStatus);
goto Exit;
}
WdfMemoryGetBuffer(outputMemory,
&outputBufferLength);
Packet->reportId = (UCHAR) outputBufferLength;
// Get report buffer from input buffer.
//
ntStatus = WdfRequestRetrieveInputMemory(Request,
&inputMemory);
if ( !NT_SUCCESS(ntStatus) )
{
TraceEvents(TRACE_LEVEL_ERROR, DMF_TRACE, "WdfRequestRetrieveInputMemory fails: ntStatus=%!STATUS!", ntStatus);
return ntStatus;
}
inputBuffer = WdfMemoryGetBuffer(inputMemory,
&inputBufferLength);
Packet->reportBuffer = (UCHAR*)inputBuffer;
Packet->reportBufferLen = (ULONG)inputBufferLength;
Exit:
return ntStatus;
}
#endif
NTSTATUS
VirtualHidMini_RequestCopyFromBuffer(
_In_ WDFREQUEST Request,
_In_ VOID* SourceBuffer,
_When_(NumberOfBytesToCopyFrom == 0, __drv_reportError(NumberOfBytesToCopyFrom cannot be zero))
_In_ size_t NumberOfBytesToCopyFrom
)
/*++
Routine Description:
A helper function to copy specified bytes to the request's output memory.
Arguments:
Request - A handle to a framework request object.
SourceBuffer - The buffer to copy data from.
NumBytesToCopyFrom - The length, in bytes, of data to be copied.
Return Value:
NTSTATUS
--*/
{
NTSTATUS ntStatus;
WDFMEMORY memory;
size_t outputBufferLength;
ntStatus = WdfRequestRetrieveOutputMemory(Request,
&memory);
if ( !NT_SUCCESS(ntStatus) )
{
TraceEvents(TRACE_LEVEL_ERROR, DMF_TRACE, "WdfRequestRetrieveOutputMemory fails: ntStatus=%!STATUS!", ntStatus);
goto Exit;
}
WdfMemoryGetBuffer(memory,
&outputBufferLength);
if (outputBufferLength < NumberOfBytesToCopyFrom)
{
ntStatus = STATUS_INVALID_BUFFER_SIZE;
TraceEvents(TRACE_LEVEL_ERROR,
DMF_TRACE,
"WdfRequestRetrieveOutputMemory fails: buffer too small. Size %d expect %d",
(int)outputBufferLength,
(int)NumberOfBytesToCopyFrom);
goto Exit;
}
ntStatus = WdfMemoryCopyFromBuffer(memory,
0,
SourceBuffer,
NumberOfBytesToCopyFrom);
if (!NT_SUCCESS(ntStatus) )
{
TraceEvents(TRACE_LEVEL_ERROR, DMF_TRACE, "WdfMemoryCopyFromBuffer fails: ntStatus=%!STATUS!", ntStatus);
goto Exit;
}
WdfRequestSetInformation(Request,
NumberOfBytesToCopyFrom);
Exit:
return ntStatus;
}
NTSTATUS
VirtualHidMini_ManualQueueCreate(
_In_ DMFMODULE DmfModule,
_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 available
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 ntStatus;
WDF_IO_QUEUE_CONFIG queueConfig;
WDF_OBJECT_ATTRIBUTES queueAttributes;
WDFQUEUE queue;
MANUAL_QUEUE_CONTEXT* queueContext;
WDFDEVICE device;
DMF_CONFIG_VirtualHidMini* moduleConfig;
device = DMF_ParentDeviceGet(DmfModule);
moduleConfig = DMF_CONFIG_GET(DmfModule);
WDF_IO_QUEUE_CONFIG_INIT(&queueConfig,
WdfIoQueueDispatchManual);
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&queueAttributes,
MANUAL_QUEUE_CONTEXT);
ntStatus = WdfIoQueueCreate(device,
&queueConfig,
&queueAttributes,
&queue);
if ( !NT_SUCCESS(ntStatus) )
{
TraceEvents(TRACE_LEVEL_ERROR, DMF_TRACE, "WdfIoQueueCreate fails: ntStatus=%!STATUS!", ntStatus);
goto Exit;
}
queueContext = ManualQueueContextGet(queue);
queueContext->Queue = queue;
queueContext->DmfModule = DmfModule;
*Queue = queue;
Exit:
return ntStatus;
}
NTSTATUS
VirtualHidMini_ReadReport(
_In_ DMFMODULE DmfModule,
_In_ WDFREQUEST Request,
_Always_(_Out_) BOOLEAN* CompleteRequest
)
/*++
Routine Description:
Handles IOCTL_HID_READ_REPORT for the HID collection. Normally the request
will be forwarded to a manual queue for further process. In that case, the
caller should not try to complete the request at this time, as the request
will later be retrieved back from the manually queue and completed there.
However, if for some reason the forwarding fails, the caller still need
to complete the request with proper error code immediately.
Arguments:
QueueContext - The object context associated with the queue
Request - Pointer to Request Packet.
CompleteRequest - A boolean output value, indicating whether the caller
should complete the request or not
Return Value:
NTSTATUS
--*/
{
NTSTATUS ntStatus;
DMF_CONTEXT_VirtualHidMini* moduleContext;
moduleContext = DMF_CONTEXT_GET(DmfModule);
// Forward the request to manual queue.
//
ntStatus = WdfRequestForwardToIoQueue(Request,
moduleContext->ManualQueue);
if (! NT_SUCCESS(ntStatus))
{
TraceEvents(TRACE_LEVEL_ERROR, DMF_TRACE, "WdfRequestForwardToIoQueue fails: ntStatus=%!STATUS!", ntStatus);
*CompleteRequest = TRUE;
}
else
{
*CompleteRequest = FALSE;
}
return ntStatus;
}
NTSTATUS
VirtualHidMini_WriteReport(
_In_ DMFMODULE DmfModule,
_In_ WDFREQUEST Request
)
/*++
Routine Description:
Handles IOCTL_HID_WRITE_REPORT for all the collection.
Arguments:
QueueContext - The object context associated with the queue.
Request - Pointer to Request Packet.
Return Value:
NTSTATUS
--*/
{
NTSTATUS ntStatus;
HID_XFER_PACKET packet;
ULONG reportSize;
DMF_CONFIG_VirtualHidMini* moduleConfig;
ntStatus = VirtualHidMini_RequestGetHidXferPacket_ToWriteToDevice(Request,
&packet);
if (!NT_SUCCESS(ntStatus))
{
goto Exit;
}
moduleConfig = DMF_CONFIG_GET(DmfModule);
// Call Client.
//
ntStatus = moduleConfig->WriteReport(DmfModule,
Request,
&packet,
&reportSize);
if (STATUS_PENDING == ntStatus)
{
// The Client will complete the request asynchronously.
//
}
else
{
// Prepare to complete the request on behalf of the Client.
//
WdfRequestSetInformation(Request,
reportSize);
}
Exit:
return ntStatus;
}
HRESULT
VirtualHidMini_GetFeature(
_In_ DMFMODULE DmfModule,
_In_ WDFREQUEST Request
)
/*++
Routine Description:
Handles IOCTL_HID_GET_FEATURE for all the collection.
Arguments:
QueueContext - The object context associated with the queue.
Request - Pointer to Request Packet.
Return Value:
NTSTATUS
--*/
{
NTSTATUS ntStatus;
HID_XFER_PACKET packet;
ULONG reportSize;
DMF_CONFIG_VirtualHidMini* moduleConfig;
moduleConfig = DMF_CONFIG_GET(DmfModule);
ntStatus = VirtualHidMini_RequestGetHidXferPacket_ToReadFromDevice(Request,
&packet);
if (!NT_SUCCESS(ntStatus))
{
goto Exit;
}
// Call Client.
//
ntStatus = moduleConfig->GetFeature(DmfModule,
Request,
&packet,
&reportSize);
if (STATUS_PENDING == ntStatus)
{
// The Client will complete the request asynchronously.
//
}
else
{
// Prepare to complete the request on behalf of the Client.
//
packet.reportBuffer[0] = packet.reportId;
reportSize += sizeof(packet.reportId);
WdfRequestSetInformation(Request,
reportSize);
}
Exit:
return ntStatus;
}
NTSTATUS
VirtualHidMini_SetFeature(
_In_ DMFMODULE DmfModule,
_In_ WDFREQUEST Request
)
/*++
Routine Description:
Handles IOCTL_HID_SET_FEATURE for all the collection.
Arguments:
QueueContext - The object context associated with the queue.
Request - Pointer to Request Packet.
Return Value:
NTSTATUS
--*/
{
NTSTATUS ntStatus;
HID_XFER_PACKET packet;
ULONG reportSize;
DMF_CONTEXT_VirtualHidMini* moduleContext;
DMF_CONFIG_VirtualHidMini* moduleConfig;
moduleContext = DMF_CONTEXT_GET(DmfModule);
moduleConfig = DMF_CONFIG_GET(DmfModule);
ntStatus = VirtualHidMini_RequestGetHidXferPacket_ToWriteToDevice(Request,
&packet);
if ( !NT_SUCCESS(ntStatus) )
{
goto Exit;
}
// Call Client.
//
ntStatus = moduleConfig->SetFeature(DmfModule,
Request,
&packet,
&reportSize);
if (STATUS_PENDING == ntStatus)
{
// The Client will complete the request asynchronously.
//
}
else
{
// Prepare to complete the request on behalf of the Client.
//
WdfRequestSetInformation(Request,
reportSize);
}
Exit:
return ntStatus;
}
NTSTATUS
VirtualHidMini_GetInputReport(
_In_ DMFMODULE DmfModule,
_In_ WDFREQUEST Request
)
/*++
Routine Description:
Handles IOCTL_HID_GET_INPUT_REPORT for all the collection.
Arguments:
QueueContext - The object context associated with the queue.
Request - Pointer to Request Packet.
Return Value:
NTSTATUS
--*/
{
NTSTATUS ntStatus;
HID_XFER_PACKET packet;
ULONG reportSize;
DMF_CONTEXT_VirtualHidMini* moduleContext;
DMF_CONFIG_VirtualHidMini* moduleConfig;
moduleContext = DMF_CONTEXT_GET(DmfModule);
moduleConfig = DMF_CONFIG_GET(DmfModule);
ntStatus = VirtualHidMini_RequestGetHidXferPacket_ToReadFromDevice(Request,
&packet);
if ( !NT_SUCCESS(ntStatus) )
{
goto Exit;
}
// Call Client.
//
ntStatus = moduleConfig->GetInputReport(DmfModule,
Request,
&packet,
&reportSize);
if (STATUS_PENDING == ntStatus)
{
// The Client will complete the request asynchronously.
//
}
else
{
// Prepare to complete the request on behalf of the Client.
//
WdfRequestSetInformation(Request,
reportSize);
}
Exit:
return ntStatus;
}
NTSTATUS
VirtualHidMini_SetOutputReport(
_In_ DMFMODULE DmfModule,
_In_ WDFREQUEST Request
)
/*++
Routine Description:
Handles IOCTL_HID_SET_OUTPUT_REPORT for all the collection.
Arguments:
QueueContext - The object context associated with the queue.
Request - Pointer to Request Packet.
Return Value:
NTSTATUS
--*/
{
NTSTATUS ntStatus;
HID_XFER_PACKET packet;
ULONG reportSize;
DMF_CONTEXT_VirtualHidMini* moduleContext;
DMF_CONFIG_VirtualHidMini* moduleConfig;
;
moduleContext = DMF_CONTEXT_GET(DmfModule);
moduleConfig = DMF_CONFIG_GET(DmfModule);
ntStatus = VirtualHidMini_RequestGetHidXferPacket_ToWriteToDevice(Request,
&packet);
if ( !NT_SUCCESS(ntStatus) )
{
goto Exit;
}
// Call Client.
//
ntStatus = moduleConfig->SetOutputReport(DmfModule,
Request,
&packet,
&reportSize);
if (STATUS_PENDING == ntStatus)
{
// The Client will complete the request asynchronously.
//
}
else
{
// Prepare to complete the request on behalf of the Client.
//
WdfRequestSetInformation(Request,
reportSize);
}
Exit:
return ntStatus;
}
NTSTATUS
VirtualHidMini_StringIdGet(
_In_ WDFREQUEST Request,
_Out_ ULONG* StringId,
_Out_ ULONG* LanguageId
)
/*++
Routine Description:
Helper routine to decode IOCTL_HID_GET_INDEXED_STRING and IOCTL_HID_GET_STRING.
Arguments:
Request - Pointer to Request Packet.
StringId - Id of string to retrieve.
LanguageId - Language Id of string to retrieve.
Return Value:
NTSTATUS
--*/
{
NTSTATUS ntStatus;
ULONG inputValue;
#ifdef _KERNEL_MODE
WDF_REQUEST_PARAMETERS requestParameters;
// IOCTL_HID_GET_STRING: // METHOD_NEITHER
// IOCTL_HID_GET_INDEXED_STRING: // METHOD_OUT_DIRECT
//
// The string id (or string index) is passed in Parameters.DeviceIoControl.
// Type3InputBuffer. However, Parameters.DeviceIoControl.InputBufferLength
// was not initialized by hidclass.sys, therefore trying to access the
// buffer with WdfRequestRetrieveInputMemory will fail
//
// Another problem with IOCTL_HID_GET_INDEXED_STRING is that METHOD_OUT_DIRECT
// expects the input buffer to be Irp->AssociatedIrp.SystemBuffer instead of
// Type3InputBuffer. That will also fail WdfRequestRetrieveInputMemory.
//
// The solution to the above two problems is to get Type3InputBuffer directly
//
// Also note that instead of the buffer's content, it is the buffer address
// that was used to store the string id (or index)
//
WDF_REQUEST_PARAMETERS_INIT(&requestParameters);
WdfRequestGetParameters(Request,
&requestParameters);
inputValue = PtrToUlong(requestParameters.Parameters.DeviceIoControl.Type3InputBuffer);
ntStatus = STATUS_SUCCESS;
#else
WDFMEMORY inputMemory;
size_t inputBufferLength;
VOID* inputBuffer;
// mshidumdf.sys updates the IRP and passes the string id (or index) through
// the input buffer correctly based on the IOCTL buffer type.
//
ntStatus = WdfRequestRetrieveInputMemory(Request,
&inputMemory);
if ( !NT_SUCCESS(ntStatus) )
{
TraceEvents(TRACE_LEVEL_ERROR, DMF_TRACE, "WdfRequestRetrieveInputMemory fails: ntStatus=%!STATUS!", ntStatus);
goto Exit;
}
inputBuffer = WdfMemoryGetBuffer(inputMemory,
&inputBufferLength);
// Make sure buffer is big enough.
//
if (inputBufferLength < sizeof(ULONG))
{
ntStatus = STATUS_INVALID_BUFFER_SIZE;
TraceEvents(TRACE_LEVEL_ERROR,
DMF_TRACE,
"VirtualHidMini_StringIdGet: invalid input buffer. size %d expect %d",
(int)inputBufferLength,
(int)sizeof(ULONG));
goto Exit;
}
inputValue = (*(PULONG)inputBuffer);
#endif
// The least significant two bytes of the INT value contain the string id.
//
*StringId = (inputValue & 0x0ffff);
// The most significant two bytes of the INT value contain the language
// ID (for example, a value of 1033 indicates English).
//
*LanguageId = (inputValue >> 16);
#ifndef _KERNEL_MODE
Exit:
#endif
return ntStatus;
}
NTSTATUS
VirtualHidMini_IndexedStringGet(
_In_ DMFMODULE DmfModule,