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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
|
/*++
Copyright (c) 1990-2010 Microsoft Corporation
Module Name:
Device.cpp
Abstract:
This is a simple UMDF device driver that consumes GPIO pins for I/O and interrupt.
Environment:
UMDF
--*/
//
// Inlcude the below set of headers to get the CM_RESOURCE_ definitions
//
#include "internal.h"
DEFINE_GUID (GUID_DEVINTERFACE_ECHO,
0xcdc35b6e, 0xbe4, 0x4936, 0xbf, 0x5f, 0x55, 0x37, 0x38, 0xa, 0x7c, 0x1a);
// {CDC35B6E-0BE4-4936-BF5F-5537380A7C1A}
HRESULT
CSimdevice::CreateInstance(
_In_ IWDFDriver *FxDriver,
_In_ IWDFDeviceInitialize * FxDeviceInit,
_Out_ PCSimdevice *Device
)
/*++
Routine Description:
This method creates and initializs an instance of the driver's
device callback object.
Arguments:
FxDeviceInit - the settings for the device.
Device - a location to store the referenced pointer to the device object.
Return Value:
Status
--*/
{
PCSimdevice device;
HRESULT hr;
//
// Allocate a new instance of the device class.
//
device = new CSimdevice();
if (NULL == device) {
return E_OUTOFMEMORY;
}
//
// Initialize the instance.
//
hr = device->Initialize(FxDriver, FxDeviceInit);
if (SUCCEEDED(hr)) {
*Device = device;
}
else {
device->Release();
}
return hr;
}
HRESULT
CSimdevice::Initialize(
_In_ IWDFDriver * FxDriver,
_In_ IWDFDeviceInitialize * FxDeviceInit
)
/*++
Routine Description:
This method initializes the device callback object and creates the
partner device object.
The method should perform any device-specific configuration that:
* could fail (these can't be done in the constructor)
* must be done before the partner object is created -or-
* can be done after the partner object is created and which aren't
influenced by any device-level parameters the parent (the driver
in this case) might set.
Arguments:
FxDeviceInit - the settings for this device.
Return Value:
status.
--*/
{
IWDFDevice *fxDevice;
IWDFDeviceInitialize2 *fxDeviceInit2;
HRESULT hr;
//
// Configure things like the locking model before we go to create our
// partner device.
//
//
// Set no locking unless you need an automatic callbacks synchronization
//
FxDeviceInit->SetLockingConstraint(None);
//
// Create a new FX device object and assign the new callback object to
// handle any device level events that occur.
//
//
// Set retrieval mode to direct I/O. This needs to be done before the call
// to CreateDevice.
//
FxDeviceInit->QueryInterface(IID_PPV_ARGS(&fxDeviceInit2));
if (fxDeviceInit2 == NULL) {
hr = E_FAIL;
return hr;
}
fxDeviceInit2->SetIoTypePreference(WdfDeviceIoBufferRetrievalDeferred,
WdfDeviceIoDirect,
WdfDeviceIoDirect);
SAFE_RELEASE(fxDeviceInit2);
//
// QueryIUnknown references the IUnknown interface that it returns
// (which is the same as referencing the device). We pass that to
// CreateDevice, which takes its own reference if everything works.
//
{
IUnknown *unknown = this->QueryIUnknown();
hr = FxDriver->CreateDevice(FxDeviceInit, unknown, &fxDevice);
unknown->Release();
}
//
// If that succeeded then set our FxDevice member variable.
//
if (SUCCEEDED(hr)) {
m_FxDevice = fxDevice;
//
// Drop the reference we got from CreateDevice. Since this object
// is partnered with the framework object they have the same
// lifespan - there is no need for an additional reference.
//
fxDevice->Release();
}
return hr;
}
HRESULT
CSimdevice::Configure(
VOID
)
/*++
Routine Description:
This method is called after the device callback object has been initialized
and returned to the driver. It would setup the device's queues and their
corresponding callback objects.
Arguments:
FxDevice - the framework device object for which we're handling events.
Return Value:
status
--*/
{
PCSimdeviceQueue defaultQueue;
HRESULT hr;
hr = CSimdeviceQueue::CreateInstance(m_FxDevice, &defaultQueue);
if (FAILED(hr)) {
return hr;
}
hr = defaultQueue->Configure();
if (SUCCEEDED(hr)) {
//
// In case of success store defaultQueue in our member
// The reference is transferred to m_DefaultQueue
//
m_Queue = defaultQueue;
}
else {
//
// In case of failure release the reference
//
defaultQueue->Release();
}
if (SUCCEEDED(hr)) {
hr = m_FxDevice->CreateDeviceInterface(&GUID_DEVINTERFACE_ECHO,
NULL);
}
return hr;
}
HRESULT
CSimdevice::QueryInterface(
_In_ REFIID InterfaceId,
_Out_ PVOID *Object
)
/*++
Routine Description:
This method is called to get a pointer to one of the object's callback
interfaces.
Since the sample driver doesn't support any of the device events, this
method simply calls the base class's BaseQueryInterface.
If the sample is extended to include device event interfaces then this
method must be changed to check the IID and return pointers to them as
appropriate.
Arguments:
InterfaceId - the interface being requested
Object - a location to store the interface pointer if successful
Return Value:
S_OK or E_NOINTERFACE
--*/
{
HRESULT hr;
if (IsEqualIID(InterfaceId, __uuidof(IPnpCallbackSelfManagedIo))) {
*Object = QueryIPnpCallbackSelfManagedIo();
hr = S_OK;
} else if (IsEqualIID(InterfaceId, __uuidof(IPnpCallbackHardware2))) {
*Object = QueryIPnpCallbackHardware2();
hr = S_OK;
} else if (IsEqualIID(InterfaceId, __uuidof(IPnpCallback))) {
*Object = QueryIPnpCallback();
hr = S_OK;
} else {
hr = CUnknown::QueryInterface(InterfaceId, Object);
}
return hr;
}
HRESULT
CSimdevice::OnSelfManagedIoInit(
_In_ IWDFDevice * pWdfDevice
)
/*++
Routine Description:
This method is called to allow driver to initialize any resources
that driver might need to process I/O.
Echo driver needs a thread to process completions. We initialize
this thread here
Arguments:
pWdfDevice - framework device object for which to initialze resources
Return Value:
S_OK in case of success
HRESULT correponding to error returned by CreateThread, in case of failure
--*/
{
HRESULT hr = S_OK;
UNREFERENCED_PARAMETER(pWdfDevice);
m_ThreadHandle = CreateThread( NULL, // Default Security Attrib.
0, // Initial Stack Size,
CSimdeviceQueue::CompletionThread, // Thread Func
(LPVOID)m_Queue, // Arg to Thread Func is Queue
0, // Creation Flags
NULL ); // Don't need the Thread Id.
if (m_ThreadHandle == NULL) {
hr = HRESULT_FROM_WIN32(GetLastError());
}
return hr;
}
void
CSimdevice::OnSelfManagedIoCleanup(
_In_ IWDFDevice * pWdfDevice
)
/*++
Routine Description:
This method is called to allow driver to cleanup any resources
that driver allocated to process I/O.
It is critical that, in this routine driver wait for all of the
threads which it created to exit. Otherwise those threads could
continue to execute when framework unloads the driver which
would lead to a crash.
Echo driver created a thread to handle completions. We wait for
that thread to exit in this routine
Arguments:
pWdfDevice - framework device object for which to cleanup resources
Return Value:
None
--*/
{
//
// Kill the thread and
// wait for the thread to die.
//
UNREFERENCED_PARAMETER(pWdfDevice);
if (m_ThreadHandle) {
//
// Ask queue to set terminate flag which will make
// the thread exit
//
m_Queue->SetExitThread();
//
// Wait for the thread to exit
//
WaitForSingleObject(m_ThreadHandle, INFINITE);
//
// Close the thread handle
//
CloseHandle(m_ThreadHandle);
m_ThreadHandle = NULL;
}
//
// Release the reference we took on the queue callback object
// to keep it alive until the thread exits
//
SAFE_RELEASE(m_Queue);
}
HRESULT
CSimdevice::OnPrepareHardware(
_In_ IWDFDevice3 * pWdfDevice,
_In_ IWDFCmResourceList * pWdfResourcesRaw,
_In_ IWDFCmResourceList * pWdfResourcesTranslated
)
/*++
Routine Description:
This routine is called by WUDF to initialize hardware resources (e.g. interrupts,
IO resources)
Arguments:
pWdfDevice - pointer to an IWDFDevice object for the device
pWdfResourcesRaw - Supplies a pointer to a collection of framework resource
objects. This collection identifies the raw (bus-relative) hardware
resources that have been assigned to the device.
pWdfResourcesTranslated - Supplies a pointer to a collection of framework
resource objects. This collection identifies the translated
(system-physical) hardware resources that have been assigned to the
device. The resources appear from the CPU's point of view.
Return Value:
HRESULT
--*/
{
ULONG i;
HRESULT hr = S_OK;
ULONG ConnectionCount = 0;
BOOLEAN fInterruptFound = FALSE;
PCM_PARTIAL_RESOURCE_DESCRIPTOR DescriptorTranslated;
PCM_PARTIAL_RESOURCE_DESCRIPTOR DescriptorRaw;
//
// Parse the resource list and save the resource information.
//
for (i=0; i < pWdfResourcesTranslated->GetCount(); i++) {
DescriptorTranslated = (PCM_PARTIAL_RESOURCE_DESCRIPTOR)pWdfResourcesTranslated->GetDescriptor(i);
DescriptorRaw = (PCM_PARTIAL_RESOURCE_DESCRIPTOR)pWdfResourcesRaw->GetDescriptor(i);
if(DescriptorTranslated == NULL) {
return E_FAIL;
}
switch (DescriptorTranslated->Type) {
//
// One or more GPIO IO resources are expected. In this implementation of the sample
// driver, however, only the first IO resource will be used (see OnD0Entry).
//
case CmResourceTypeConnection:
//
// Check against expected connection type for a GPIO IO descriptor
//
if ((DescriptorTranslated->u.Connection.Class ==
CM_RESOURCE_CONNECTION_CLASS_GPIO) &&
(DescriptorTranslated->u.Connection.Type ==
CM_RESOURCE_CONNECTION_TYPE_GPIO_IO)) {
if (ConnectionCount >= MAX_CONNECTIONS) {
break;
}
//
// Store GPIO IO resource connection ID in the device extension
//
this->GetDeviceExtension()->ConnectionId[ConnectionCount].LowPart =
DescriptorTranslated->u.Connection.IdLowPart;
this->GetDeviceExtension()->ConnectionId[ConnectionCount].HighPart =
DescriptorTranslated->u.Connection.IdHighPart;
}
break;
case CmResourceTypeInterrupt:
//
// Connect the first interrupt resource we find
//
if (fInterruptFound == FALSE) {
hr = this->SimdeviceConnectInterrupt(
pWdfDevice,
DescriptorRaw,
DescriptorTranslated);
if (SUCCEEDED(hr)) {
fInterruptFound = TRUE;
}
}
default:
//
// Ignore all other descriptors
//
break;
}
}
return hr;
}
HRESULT
CSimdevice::OnReleaseHardware(
_In_ IWDFDevice3 * pWdfDevice,
_In_ IWDFCmResourceList * pWdfResourcesTranslated
)
/*++
Routine Description:
This method is called by WUDF to uninitialize the hardware.
Parameters:
pWdfDevice - pointer to an IWDFDevice object for the device
pWdfResourcesTranslated - pointer to the translated resource list
Return Values:
status
--*/
{
UNREFERENCED_PARAMETER(pWdfDevice);
UNREFERENCED_PARAMETER(pWdfResourcesTranslated);
return S_OK;
}
HRESULT
CSimdevice::OnD0Entry(
_In_ IWDFDevice* pWdfDevice,
_In_ WDF_POWER_DEVICE_STATE previousState
)
/*++
Routine Description:
This method is called after a new device enters the system
Parameters:
pWdfDevice - pointer to a device object
previousState - previous WDF power state
Return Values:
status
--*/
{
PDEVICE_EXTENSION DevExt;
HRESULT hr = S_OK;
BYTE Data;
NTSTATUS Status;
NTSTATUS Status1;
WCHAR ReadStringBuffer[100];
WCHAR WriteStringBuffer[100];
UNREFERENCED_PARAMETER(pWdfDevice);
UNREFERENCED_PARAMETER(previousState);
DevExt = GetDeviceExtension();
Status = StringCbPrintfW(&ReadStringBuffer[0],
sizeof(ReadStringBuffer),
L"\\\\.\\RESOURCE_HUB\\%0*I64x",
(size_t)(sizeof(LARGE_INTEGER) * 2),
DevExt->ConnectionId[0].QuadPart);
Status = StringCbPrintfW(&WriteStringBuffer[0],
sizeof(WriteStringBuffer),
L"\\\\.\\RESOURCE_HUB\\%0*I64x",
(size_t)(sizeof(LARGE_INTEGER) * 2),
DevExt->ConnectionId[0].QuadPart);
//
// Connect the first GPIO IO descriptor we find (in OnPrepareHardware) for read and write operations
//
Data = 0xDB;
Status = STATUS_SUCCESS;
Status1 = TestReadWrite(pWdfDevice, &WriteStringBuffer[0], FALSE, &Data, sizeof(Data), NULL);
if (!NT_SUCCESS(Status1)) {
Status = Status1;
}
Data = 0;
Status1 = TestReadWrite(pWdfDevice, &ReadStringBuffer[0], TRUE, &Data, sizeof(Data), NULL);
if (!NT_SUCCESS(Status1)) {
Status = Status1;
}
return hr;
}
HRESULT
CSimdevice::OnD0Exit(
_In_ IWDFDevice* pWdfDevice,
_In_ WDF_POWER_DEVICE_STATE TargetState
)
/*++
Routine Description:
This method is called when a device exit D0
Parameters:
pWdfDevice pointer to a device object
TargetState - target D-state
Return Values:
status
--*/
{
UNREFERENCED_PARAMETER(pWdfDevice);
UNREFERENCED_PARAMETER(TargetState);
return S_OK;
}
HRESULT
CSimdevice::TestReadWrite(
_In_ IWDFDevice* pWdfDevice,
_In_ PWSTR RequestString,
_In_ BOOLEAN ReadOperation,
_Inout_updates_bytes_(Size) UCHAR *Data,
_In_ ULONG Size,
_Inout_opt_ IWDFRemoteTarget *IoTargetOut
)
/*++
Routine Description:
This is a utility routine to test read or write on a set of GPIO pins.
Arguments:
pWdfDevice - Supplies a pointer to the framework device object.
RequestString - Supplies a pointer to the unicode string to be opened.
ReadOperation - Supplies a boolean that identifies whether read (TRUE) or
write (FALSE) should be performed.
Data - Supplies a pointer containing the buffer that should be read from
or written to.
Size - Supplies the size of the data buffer in bytes.
IoTargetOut - Supplies a pointer that receives the IOTARGET created by
UMDF.
Return Value:
HRESULT
--*/
{
IWDFMemory *pOutputMemory = NULL;
IWDFMemory *pInputMemory = NULL;
IRequestCallbackRequestCompletion *pICallback = NULL;
IWDFIoRequest *pIoRequest = NULL;
IWDFRemoteTarget *pRemoteTarget = NULL;
UMDF_IO_TARGET_OPEN_PARAMS OpenParams;
DWORD DesiredAccess;
HRESULT hr = S_OK;
IWDFRequestCompletionParams * FxComplParams = NULL;
IWDFDriver *pWdfDriver = NULL;
UNREFERENCED_PARAMETER(IoTargetOut);
if (ReadOperation != FALSE) {
DesiredAccess = FILE_GENERIC_READ;
} else {
DesiredAccess = FILE_GENERIC_WRITE;
}
//
// QueryIUnknown references the IUnknown interface that it returns
// (which is the same as referencing the CMyRemoteTarget). We pass that
// to the various Create* calls, which take their own reference if
// everything works.
//
IUnknown * unknown = this->QueryIUnknown();
IWDFDevice2 *pWdfDevice2 = NULL;
hr = pWdfDevice->QueryInterface(IID_PPV_ARGS(&pWdfDevice2));
// Create the IoTarget
if (SUCCEEDED(hr)) {
hr = pWdfDevice2->CreateRemoteTarget(
unknown,
NULL,
&pRemoteTarget);
}
//
// Determine whether the request is a read or write
//
OpenParams.dwCreationDisposition = OPEN_EXISTING;
OpenParams.dwFlagsAndAttributes = FILE_FLAG_OVERLAPPED;
OpenParams.dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
if (SUCCEEDED(hr)) {
hr = pRemoteTarget->OpenFileByName(RequestString,
DesiredAccess,
&OpenParams);
}
//
//Create a new IO request
//
if (SUCCEEDED(hr)) {
hr = pWdfDevice->CreateRequest(NULL, pWdfDevice, &pIoRequest);
}
if (SUCCEEDED(hr)) {
hr = this->QueryInterface(__uuidof(IRequestCallbackRequestCompletion), (PVOID*)&pICallback);
//
//Set completion callback
//
if (SUCCEEDED(hr)){
pIoRequest->SetCompletionCallback(pICallback, NULL);
}
pWdfDevice->GetDriver(&pWdfDriver);
hr = pWdfDriver->CreatePreallocatedWdfMemory(Data,
Size,
NULL, // no object event callback
pIoRequest, // request object as parent
&pInputMemory);
hr = pWdfDriver->CreatePreallocatedWdfMemory(Data,
Size,
NULL, // no object event callback
pIoRequest, // request object as parent
&pOutputMemory);
//
//Format IO request
//
if (ReadOperation != FALSE){
if (SUCCEEDED(hr)) {
hr = pRemoteTarget->FormatRequestForIoctl(pIoRequest,
IOCTL_GPIO_READ_PINS,
NULL,
NULL,
NULL,
pOutputMemory,
NULL);
}
}
else {
if (SUCCEEDED(hr)) {
hr = pRemoteTarget->FormatRequestForIoctl(pIoRequest,
IOCTL_GPIO_WRITE_PINS,
NULL,
pInputMemory,
NULL,
pOutputMemory,
NULL);
}
}
//
//Send down the request
//
if (SUCCEEDED(hr)) {
hr = pIoRequest->Send(pRemoteTarget,
WDF_REQUEST_SEND_OPTION_SYNCHRONOUS,
0); //No timeout
}
if (SUCCEEDED(hr)) {
//
//Get IWDFRequestCompletionParams interface and then get completion status.
//
pIoRequest->GetCompletionParams(&FxComplParams);
hr = FxComplParams->GetCompletionStatus();
}
if(FAILED(hr)) {
pIoRequest->DeleteWdfObject();
pIoRequest = NULL;
}
}
//
// Clean-up
//
if (pWdfDevice2 != NULL) {
SAFE_RELEASE(pWdfDevice2);
}
if (pWdfDriver != NULL) {
SAFE_RELEASE(pWdfDriver);
}
if (pRemoteTarget != NULL) {
pRemoteTarget->Close();
}
return hr;
}
void
CSimdevice::OnSurpriseRemoval(
_In_ IWDFDevice* pWdfDevice
)
/*++
Routine Description:
This method is called when a device is surprise removed
Parameters:
pWdfDevice pointer to a device object
Return Values:
None
--*/
{
UNREFERENCED_PARAMETER(pWdfDevice);
}
HRESULT
CSimdevice::OnQueryRemove(
_In_ IWDFDevice* pWdfDevice
)
/*++
Routine Description:
This method is called when a device processes the query remove IRP
Parameters:
pWdfDevice pointer to a device object
Return Values:
HRESULT
--*/
{
UNREFERENCED_PARAMETER(pWdfDevice);
return S_OK;
}
HRESULT
CSimdevice::OnQueryStop(
_In_ IWDFDevice* pWdfDevice
)
/*++
Routine Description:
This method is called when a device processes the query stop IRP
Parameters:
pWdfDevice pointer to a device object
Return Values:
HRESULT
--*/
{
UNREFERENCED_PARAMETER(pWdfDevice);
return S_OK;
}
HRESULT
CSimdevice::SimdeviceConnectInterrupt(
_In_ IWDFDevice* pWdfDevice,
_In_opt_ PCM_PARTIAL_RESOURCE_DESCRIPTOR RawResource,
_In_opt_ PCM_PARTIAL_RESOURCE_DESCRIPTOR TranslatedResource
)
/*++
Routine Description:
This is a utility routine to create an interrupt descriptor from the interrupt resource
acquired from OnPrepareHardware and connect the device ISR routine.
Arguments:
pWdfDevice - Supplies a pointer to the framework device object.
pWdfResourcesRaw - Supplies a pointer to a collection of framework resource
objects. This collection identifies the raw (bus-relative) hardware
resources that have been assigned to the device.
pWdfResourcesTranslated - Supplies a pointer to a collection of framework
resource objects. This collection identifies the translated
(system-physical) hardware resources that have been assigned to the
device. The resources appear from the CPU's point of view.
Return Value:
None.
--*/
{
IWDFDevice3 * pIWDFDevice3 = NULL;
IWDFInterrupt * spInterrupt = NULL;
HRESULT hr = S_OK;
if (pWdfDevice == NULL) {
hr = E_INVALIDARG;
}
if (SUCCEEDED(hr)) {
hr = pWdfDevice->QueryInterface(IID_PPV_ARGS(&pIWDFDevice3));
}
if (SUCCEEDED(hr)) {
//
// Create interrupt
//
WUDF_INTERRUPT_CONFIG config;
WUDF_INTERRUPT_CONFIG_INIT(
&config,
&CSimdevice::OnInterruptIsr,
&CSimdevice::OnInterruptWorkItem);
config.InterruptRaw = RawResource;
config.InterruptTranslated = TranslatedResource;
hr = pIWDFDevice3->CreateInterrupt(&config, &spInterrupt);
}
//
// Associate the device context with the interrupt
//
if (SUCCEEDED(hr)) {
hr = spInterrupt->AssignContext(NULL, (void*)this);
}
return hr;
}
BOOLEAN
CSimdevice::OnInterruptIsr(
_In_ IWDFInterrupt* pInterrupt,
_In_ ULONG MessageID,
_In_ ULONG Reserved
)
/* ++
Routine Description:
This method is called when an interrupt occurs. It determines if the
driver owns the interrupt and queues a work item to defer processing
of the data.
Arguments:
pInterrupt - pointer to the interrupt object
MessageID - interrupt message ID
Reserved -
Return Values:
TRUE if interrupt recognized, else FALSE.
-- */
{
UNREFERENCED_PARAMETER(MessageID);
UNREFERENCED_PARAMETER(Reserved);
IWDFDevice* pWdfDevice = NULL;
CSimdevice* pMyDevice = NULL;
HRESULT hr;
hr = pInterrupt->RetrieveContext((void**)&pMyDevice);
if (FAILED(hr)) {
pWdfDevice = pInterrupt->GetDevice();
pWdfDevice->SetPnpState(WdfPnpStateFailed, WdfTrue);
pWdfDevice->CommitPnpState();
}
//
// The sample driver always returns TRUE (e.g. claiming the interrupt)
// from its ISR. In reality, the driver needs to do whatever necessary to
// quiesce the interrupt before claiming the interrupt.
// If additional work needs to be done at , schedule a work item (as we do here).
//
pInterrupt->QueueWorkItemForIsr();
return TRUE;
}
VOID
CSimdevice::OnInterruptWorkItem(
_In_ IWDFInterrupt* pInterrupt,
_In_ IWDFObject* AssociatedObject
)
/* ++
Routine Description:
This method is called on behalf of an interrupt to defer processing.
It retrieves latest data and posts it.
Arguments:
pInterrupt - pointer to the interrupt object
AssociatedObject - pointer to the associated object
Return Values:
None.
--*/
{
UNREFERENCED_PARAMETER(AssociatedObject);
IWDFDevice* pWdfDevice = NULL;
CSimdevice* pMyDevice;
HRESULT hr;
hr = pInterrupt->RetrieveContext((void**)&pMyDevice);
if (FAILED(hr)) {
pWdfDevice = pInterrupt->GetDevice();
pWdfDevice->SetPnpState(WdfPnpStateFailed, WdfTrue);
pWdfDevice->CommitPnpState();
}
//
// The sample driver does nothing in the work item routine. The real driver
// can add code here to accomplish all that is required to complete the serving
// of the interrupt
//
return;
}
|