summaryrefslogtreecommitdiff
path: root/general/pcidrv/kmdf/PCIDRV.C
blob: 17ae76bf5d665cc0463767ce1ca26215bf9e24e0 (plain)
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
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
/*++

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:

    PciDrv.c

Abstract:

    This is a generic WDM sample driver for Intel 82557/82558
    based PCI Ethernet Adapter (10/100) and Intel compatibles.
    The WDM interface in this sample is based on the Toaster function
    driver, and all the code to access the hardware is taken from
    the E100BEX NDIS miniport sample from the DDK and converted to
    use WDM interfaces instead of NDIS functions.

    This driver can be installed as a standalone driver (genpci.inf)
    for the Intel PCI device.  Please read the PCIDRV.HTM file for 
    more information.

Environment:

    Kernel mode

--*/

#include "precomp.h"

#if defined(EVENT_TRACING)
//
// The trace message header (.tmh) file must be included in a source file
// before any WPP macro calls and after defining a WPP_CONTROL_GUIDS
// macro (defined in toaster.h). During the compilation, WPP scans the source
// files for DoTraceMessage() calls and builds a .tmh file which stores a unique
// data GUID for each message, the text resource string for each message,
// and the data types of the variables passed in for each message.  This file
// is automatically generated and used during post-processing.
//
#include "pcidrv.tmh"
#endif

//
// Global debug error level
//
#if !defined(EVENT_TRACING)
ULONG DebugLevel = TRACE_LEVEL_INFORMATION;
ULONG DebugFlag = 0x2f;//0x46;//0x4FF; //0x00000006;
#else
ULONG DebugLevel; // wouldn't be used to control the TRACE_LEVEL_VERBOSE
ULONG DebugFlag;
#endif

#ifdef ALLOC_PRAGMA
#pragma alloc_text (INIT, DriverEntry)
#pragma alloc_text (PAGE, PciDrvEvtDeviceAdd)
#pragma alloc_text (PAGE, PciDrvEvtDeviceContextCleanup)
#pragma alloc_text (PAGE, PciDrvEvtDevicePrepareHardware)
#pragma alloc_text (PAGE, PciDrvEvtDeviceReleaseHardware)
#pragma alloc_text (PAGE, PciDrvReadRegistryValue)
#pragma alloc_text (PAGE, PciDrvWriteRegistryValue)
#pragma alloc_text (PAGE, PciDrvEvtDriverContextCleanup)
#pragma alloc_text (PAGE, PciDrvEvtDeviceSelfManagedIoCleanup)
#pragma alloc_text (PAGE, PciDrvEvtDeviceSelfManagedIoSuspend)
#pragma alloc_text (PAGE, PciDrvEvtDeviceWakeArmS0)
#pragma alloc_text (PAGE, PciDrvEvtDeviceWakeTriggeredS0)
#pragma alloc_text (PAGE, PciDrvEvtDeviceWakeArmSx)
#pragma alloc_text (PAGE, PciDrvSetPowerPolicy)
#pragma alloc_text (PAGE, PciDrvReadFdoRegistryKeyValue)
#endif


#define PARAMATER_NAME_LEN 80

NTSTATUS
DriverEntry(
    IN PDRIVER_OBJECT  DriverObject,
    IN PUNICODE_STRING RegistryPath
    )
/*++

Routine Description:

    Installable driver initialization entry point.
    This entry point is called directly by the I/O system.

Arguments:

    DriverObject - pointer to the driver object

    RegistryPath - pointer to a unicode string representing the path,
                   to driver-specific key in the registry.

Return Value:

    STATUS_SUCCESS if successful,
    STATUS_UNSUCCESSFUL otherwise.

--*/
{
    NTSTATUS               status = STATUS_SUCCESS;
    WDF_DRIVER_CONFIG      config;
    WDF_OBJECT_ATTRIBUTES  attrib;
    WDFDRIVER              driver;
    PDRIVER_CONTEXT        driverContext;

    //
    // Allow NonPagedPool to be mapped to NonPagedPoolNx on WIN8 and newer OS's 
    //
    ExInitializeDriverRuntime(DrvRtPoolNxOptIn);

    //
    // Initialize WPP Tracing
    //
    WPP_INIT_TRACING( DriverObject, RegistryPath );

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_INIT, "PCIDRV Sample - Driver Framework Edition \n");

    WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attrib, DRIVER_CONTEXT);

    //
    // Register a cleanup callback so that we can call WPP_CLEANUP when
    // the framework driver object is deleted during driver unload.
    //
    attrib.EvtCleanupCallback = PciDrvEvtDriverContextCleanup;

    //
    // Initialize the Driver Config structure..
    //
    WDF_DRIVER_CONFIG_INIT(&config, PciDrvEvtDeviceAdd);

    //
    // Create a WDFDRIVER object.
    //
    status = WdfDriverCreate(DriverObject,
                             RegistryPath,
                             &attrib,
                             &config,
                             &driver);

    if (!NT_SUCCESS(status)) {
        TraceEvents(TRACE_LEVEL_ERROR, DBG_INIT,
                    "WdfDriverCreate failed with status %!STATUS!\n", status);
        //
        // Cleanup tracing here because DriverContextCleanup will not be called
        // as we have failed to create WDFDRIVER object itself.
        // Please note that if your return failure from DriverEntry after the
        // WDFDRIVER object is created successfully, you don't have to
        // call WPP cleanup because in those cases DriverContextCleanup
        // will be executed when the framework deletes the DriverObject.
        //
        WPP_CLEANUP(DriverObject);
        return status;
    }

    driverContext = GetDriverContext(driver);

    //
    // Create a driver wide lookside list used for allocating memory  for the
    // MP_RFD structure for all device instances (if there are multiple present).
    //
    status = WdfLookasideListCreate(WDF_NO_OBJECT_ATTRIBUTES, // LookAsideAttributes
                                sizeof(MP_RFD),
                                NonPagedPool,
                                WDF_NO_OBJECT_ATTRIBUTES, // MemoryAttributes
                                PCIDRV_POOL_TAG,
                                &driverContext->RecvLookaside
                                );
    if (!NT_SUCCESS(status)) {
        TraceEvents(TRACE_LEVEL_ERROR, DBG_INIT,
                    "Couldn't allocate lookaside list status %!STATUS!\n", status);
        return status;
    }

    return status;

}

NTSTATUS
PciDrvEvtDeviceAdd(
    IN WDFDRIVER        Driver,
    IN PWDFDEVICE_INIT  DeviceInit
    )
/*++
Routine Description:

    EvtDeviceAdd is called by the framework in response to AddDevice
    call from the PnP manager.

Arguments:

    Driver - Handle to a framework driver object created in DriverEntry

    DeviceInit - Pointer to a framework-allocated WDFDEVICE_INIT structure.

Return Value:

    NTSTATUS

--*/
{
    NTSTATUS                        status = STATUS_SUCCESS;
    WDF_PNPPOWER_EVENT_CALLBACKS    pnpPowerCallbacks;
    WDF_POWER_POLICY_EVENT_CALLBACKS powerPolicyCallbacks;
    WDF_OBJECT_ATTRIBUTES           fdoAttributes;
    WDFDEVICE                       device;
    PFDO_DATA                       fdoData = NULL;

    PAGED_CODE();

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "-->PciDrvEvtDeviceAdd routine. Driver: 0x%p\n", Driver);

    //
    // I/O type is Buffered by default. If required to use something else,
    // call WdfDeviceInitSetIoType with the appropriate type.
    //
    WdfDeviceInitSetIoType(DeviceInit, WdfDeviceIoDirect);

    //
    // Zero out the PnpPowerCallbacks structure.
    //
    WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&pnpPowerCallbacks);

    //
    // Set Callbacks for any of the functions we are interested in.
    // If no callback is set, Framework will take the default action
    // by itself.  This sample provides many of the possible callbacks,
    // mostly because it's a fairly complex sample that drives full-featured
    // hardware.  Drivers derived from this sample will often be able to
    // provide only some of these.
    //

    //
    // These callback is invoked to tear down all the driver-managed state
    // that is set up in this function.  Many times, this callback won't do
    // much of anything, since many of the things that are set up here will
    // have their lifetimes automatically managed by the Framework.
    //


    //
    // These two callbacks set up and tear down hardware state,
    // specifically that which only has to be done once.
    //

    pnpPowerCallbacks.EvtDevicePrepareHardware = PciDrvEvtDevicePrepareHardware;
    pnpPowerCallbacks.EvtDeviceReleaseHardware = PciDrvEvtDeviceReleaseHardware;

    //
    // These two callbacks set up and tear down hardware state that must be
    // done every time the device moves in and out of the D0-working state.
    //

    pnpPowerCallbacks.EvtDeviceD0Entry = PciDrvEvtDeviceD0Entry;
    pnpPowerCallbacks.EvtDeviceD0Exit  = PciDrvEvtDeviceD0Exit;

    //
    // These next two callbacks are for doing work at PASSIVE_LEVEL (low IRQL)
    // after all the interrupts are connected and before they are disconnected.
    //
    // Some drivers need to do device initialization and tear-down while the
    // interrupt is connected.  (This is a problem for these devices, since
    // it opens them up to taking interrupts before they are actually ready
    // to handle them, or to taking them after they have torn down too much
    // to be able to handle them.)  While this hardware design pattern is to
    // be discouraged, it is possible to handle it by doing device init and
    // tear down in these routines rather than in EvtDeviceD0Entry and
    // EvtDeviceD0Exit.
    //
    // In this sample these callbacks don't do anything.
    //

    pnpPowerCallbacks.EvtDeviceD0EntryPostInterruptsEnabled = NICEvtDeviceD0EntryPostInterruptsEnabled;
    pnpPowerCallbacks.EvtDeviceD0ExitPreInterruptsDisabled = NICEvtDeviceD0ExitPreInterruptsDisabled;

    //
    // This next group of five callbacks allow a driver to become involved in
    // starting and stopping operations within a driver as the driver moves
    // through various PnP/Power states.  These functions are not necessary
    // if the Framework is managing all the device's queues and there is no
    // activity going on that isn't queue-based.  This sample provides these
    // callbacks because it uses watchdog timer to monitor whether the device
    // is working or not and it needs to start and stop the timer when the device
    // is started or removed. It cannot start and stop the timers in the D0Entry
    // and D0Exit callbacks because if the device is surprise-removed, D0Exit
    // will not be called.
    //
    pnpPowerCallbacks.EvtDeviceSelfManagedIoInit    = PciDrvEvtDeviceSelfManagedIoInit;
    pnpPowerCallbacks.EvtDeviceSelfManagedIoCleanup = PciDrvEvtDeviceSelfManagedIoCleanup;
    pnpPowerCallbacks.EvtDeviceSelfManagedIoSuspend = PciDrvEvtDeviceSelfManagedIoSuspend;
    pnpPowerCallbacks.EvtDeviceSelfManagedIoRestart = PciDrvEvtDeviceSelfManagedIoRestart;

    //
    // Register the PnP and power callbacks. Power policy related callbacks will be registered
    // later.
    //
    WdfDeviceInitSetPnpPowerEventCallbacks(DeviceInit, &pnpPowerCallbacks);

    //
    // Init the power policy callbacks
    //
    WDF_POWER_POLICY_EVENT_CALLBACKS_INIT(&powerPolicyCallbacks);

    //
    // This group of three callbacks allows this sample driver to manage
    // arming the device for wake from the S0 state.  Networking devices can
    // optionally be put into a low-power state when there is no networking
    // cable plugged into them.  This sample implements this feature.
    //
    powerPolicyCallbacks.EvtDeviceArmWakeFromS0 = PciDrvEvtDeviceWakeArmS0;
    powerPolicyCallbacks.EvtDeviceDisarmWakeFromS0 = PciDrvEvtDeviceWakeDisarmS0;
    powerPolicyCallbacks.EvtDeviceWakeFromS0Triggered = PciDrvEvtDeviceWakeTriggeredS0;

    //
    // This group of three callbacks allows the device to be armed for wake
    // from Sx (S1, S2, S3 or S4.)  Networking devices can optionally be put
    // into a state where a packet sent to them will cause the device's wake
    // signal to be triggered, which causes the machine to wake, moving back
    // into the S0 state.
    //

    powerPolicyCallbacks.EvtDeviceArmWakeFromSx = PciDrvEvtDeviceWakeArmSx;
    powerPolicyCallbacks.EvtDeviceDisarmWakeFromSx = PciDrvEvtDeviceWakeDisarmSx;
    powerPolicyCallbacks.EvtDeviceWakeFromSxTriggered = PciDrvEvtDeviceWakeTriggeredSx;

    //
    // Register the power policy callbacks.
    //
    WdfDeviceInitSetPowerPolicyEventCallbacks(DeviceInit, &powerPolicyCallbacks);

    // Since we are the function driver, we are now the power policy owner
    // for the device according to the default framework rule. We will register
    // our power policy callbacks after finding the wakeup capability of the device.

    //
    // Specify the context type and size for the device we are about to create.
    //
    WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&fdoAttributes, FDO_DATA);

    //
    // ContextCleanup will be called by the framework when it deletes the device.
    // So you can defer freeing any resources allocated to Cleanup callback in the
    // event AddDevice returns any error after the device is created.
    //
    fdoAttributes.EvtCleanupCallback = PciDrvEvtDeviceContextCleanup;

    status = WdfDeviceCreate(&DeviceInit, &fdoAttributes, &device);

    if ( !NT_SUCCESS(status)) {
        TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP,
                    "WdfDeviceInitialize failed %!STATUS!\n", status);
        return status;
    }

    //
    // Device creation is complete.
    // Get the DeviceExtension and initialize it.
    //
    fdoData = FdoGetData(device);
    fdoData->WdfDevice = device;

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "PDO(0x%p) FDO(0x%p), Lower(0x%p) DevExt (0x%p)\n",
                WdfDeviceWdmGetPhysicalDevice (device),
                WdfDeviceWdmGetDeviceObject (device),
                WdfDeviceWdmGetAttachedDevice(device),
                fdoData);

    //
    // Initialize the device extension and allocate all the software resources
    //
    status = NICAllocateSoftwareResources(fdoData);
    if (!NT_SUCCESS (status)){
        TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP,
                    "NICAllocateSoftwareResources failed: %!STATUS!\n",
                    status);
        return status;
    }

    //
    // If our device supports wait-wake then we will set our power-policy and
    // update S0-Idle policy.
    //
    if (IsPoMgmtSupported(fdoData)) {
        TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                    "Device has wait-wake capability\n");
        status = PciDrvSetPowerPolicy(fdoData);
        if (!NT_SUCCESS (status)) {
            TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP,
                        "PciDrvSetPowerPolicy failed %!STATUS!\n", status);
            return status;
        }
    }

    //
    // Tell the Framework that this device will need an interface so that
    // application can interact with it.
    //
    status = WdfDeviceCreateDeviceInterface(
                 device,
                 (LPGUID) &GUID_DEVINTERFACE_PCIDRV,
                 NULL
             );

    if (!NT_SUCCESS (status)) {
        TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP,
                    "WdfDeviceCreateDeviceInterface failed %!STATUS!\n", status);
        return status;
    }

    status = PciDrvWmiRegistration(device);

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP, "<-- PciDrvEvtDeviceAdd  \n");

    return status;
}

VOID
PciDrvEvtDeviceContextCleanup (
    WDFOBJECT       Device
    )
/*++

Routine Description:

   EvtDeviceContextCleanup event callback cleans up anything done in
   EvtDeviceAdd, except those things that are automatically cleaned
   up by the Framework.

   In the case of this sample, everything is automatically handled.  In a
   driver derived from this sample, it's quite likely that this function could
   be deleted.

Arguments:

    Device - Handle to a framework device object.

Return Value:

    VOID

--*/
{
    PFDO_DATA               fdoData = NULL;

    PAGED_CODE();

    fdoData = FdoGetData((WDFDEVICE)Device);

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "--> PciDrvEvtDeviceContextCleanup\n");

    NICFreeSoftwareResources(fdoData);

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "<-- PciDrvEvtDeviceContextCleanup\n");

}

NTSTATUS
PciDrvEvtDevicePrepareHardware (
    WDFDEVICE      Device,
    WDFCMRESLIST   Resources,
    WDFCMRESLIST   ResourcesTranslated
    )
/*++

Routine Description:

    EvtDeviceStart event callback performs operations that are necessary
    to make the driver's device operational. The framework calls the driver's
    EvtDeviceStart callback when the PnP manager sends an IRP_MN_START_DEVICE
    request to the driver stack.

Arguments:

    Device - Handle to a framework device object.

    Resources - Handle to a collection of framework resource objects.
                This collection identifies the raw (bus-relative) hardware
                resources that have been assigned to the device.

    ResourcesTranslated - Handle 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.
                Use this list of resources to map I/O space and
                device-accessible memory into virtual address space

Return Value:

    WDF status code

--*/
{
    NTSTATUS     status = STATUS_SUCCESS;
    PFDO_DATA    fdoData = NULL;

    UNREFERENCED_PARAMETER(Resources);
    UNREFERENCED_PARAMETER(ResourcesTranslated);

    PAGED_CODE();

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "--> PciDrvEvtDevicePrepareHardware\n");

    fdoData = FdoGetData(Device);

    status = NICMapHWResources(fdoData, Resources, ResourcesTranslated);
    if (!NT_SUCCESS (status)){
        TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP,
                    "NICMapHWResources failed: %!STATUS!\n", status);
        return status;
    }

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "<-- PciDrvEvtDevicePrepareHardware\n");

    return status;

}

NTSTATUS
PciDrvEvtDeviceReleaseHardware(
    IN  WDFDEVICE    Device,
    IN  WDFCMRESLIST ResourcesTranslated
    )
/*++

Routine Description:

    EvtDeviceReleaseHardware is called by the framework whenever the PnP manager
    is revoking ownership of our resources.  This may be in response to either
    IRP_MN_STOP_DEVICE or IRP_MN_REMOVE_DEVICE.  The callback is made before
    passing down the IRP to the lower driver.

    In this callback, do anything necessary to free those resources.

Arguments:

    Device - Handle to a framework device object.

    ResourcesTranslated - Handle 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.
                Use this list of resources to map I/O space and
                device-accessible memory into virtual address space

Return Value:

    NTSTATUS - Failures will be logged, but not acted on.

--*/
{
    PFDO_DATA  fdoData = NULL;

    UNREFERENCED_PARAMETER(ResourcesTranslated);

    PAGED_CODE();

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "--> PciDrvEvtDeviceReleaseHardware\n");

    fdoData = FdoGetData(Device);

    //
    // Unmap any I/O ports. Disconnecting from the interrupt will be done
    // automatically by the framework.
    //
    NICUnmapHWResources(fdoData);

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "<-- PciDrvEvtDeviceReleaseHardware\n");

    return STATUS_SUCCESS;
}

NTSTATUS
PciDrvEvtDeviceD0Entry(
    IN  WDFDEVICE Device,
    IN  WDF_POWER_DEVICE_STATE PreviousState
    )
/*++

Routine Description:

    EvtDeviceD0Entry event callback must perform any operations that are
    necessary before the specified device is used.  It will be called every
    time the hardware needs to be (re-)initialized.  This includes after
    IRP_MN_START_DEVICE, IRP_MN_CANCEL_STOP_DEVICE, IRP_MN_CANCEL_REMOVE_DEVICE,
    IRP_MN_SET_POWER-D0.

    This function is not marked pageable because this function is in the
    device power up path. When a function is marked pagable and the code
    section is paged out, it will generate a page fault which could impact
    the fast resume behavior because the client driver will have to wait
    until the system drivers can service this page fault.

    This function runs at PASSIVE_LEVEL, even though it is not paged.  A
    driver can optionally make this function pageable if DO_POWER_PAGABLE
    is set.  Even if DO_POWER_PAGABLE isn't set, this function still runs
    at PASSIVE_LEVEL.  In this case, though, the function absolutely must
    not do anything that will cause a page fault.

Arguments:

    Device - Handle to a framework device object.

    PreviousState - Device power state which the device was in most recently.
        If the device is being newly started, this will be
        PowerDeviceUnspecified.

Return Value:

    NTSTATUS

--*/
{
    PFDO_DATA               fdoData;

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_POWER,
                "-->PciDrvEvtDeviceD0Entry - coming from %s\n",
                DbgDevicePowerString(PreviousState));

    fdoData = FdoGetData(Device);

    ASSERT(PowerDeviceD0 != PreviousState);

    fdoData->DevicePowerState = PowerDeviceD0;

    if(IsPoMgmtSupported(fdoData)){

        TraceEvents(TRACE_LEVEL_VERBOSE, DBG_POWER,
                    "Entering fully on state\n");
        MPSetPowerD0 (fdoData);
    }


    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_POWER, "<--PciDrvEvtDeviceD0Entry\n");

    return STATUS_SUCCESS;
}


NTSTATUS
PciDrvEvtDeviceD0Exit(
    IN  WDFDEVICE Device,
    IN  WDF_POWER_DEVICE_STATE TargetState
    )
/*++

Routine Description:

    This routine undoes anything done in EvtDeviceD0Entry.  It is called
    whenever the device leaves the D0 state, which happens when the device is
    stopped, when it is removed, and when it is powered off.

    The device is still in D0 when this callback is invoked, which means that
    the driver can still touch hardware in this routine.

    Note that interrupts have already been disabled by the time that this
    callback is invoked.

   EvtDeviceD0Exit event callback must perform any operations that are
   necessary before the specified device is moved out of the D0 state.  If the
   driver needs to save hardware state before the device is powered down, then
   that should be done here.

   This function runs at PASSIVE_LEVEL, though it is generally not paged.  A
   driver can optionally make this function pageable if DO_POWER_PAGABLE is set.

   Even if DO_POWER_PAGABLE isn't set, this function still runs at
   PASSIVE_LEVEL.  In this case, though, the function absolutely must not do
   anything that will cause a page fault.

Arguments:

    Device - Handle to a framework device object.

    TargetState - Device power state which the device will be put in once this
        callback is complete.

Return Value:

    Success implies that the device can be used.  Failure will result in the
    device stack being torn down.

--*/
{
    PFDO_DATA    fdoData;

    UNREFERENCED_PARAMETER(Device);

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_POWER,
                "-->PciDrvEvtDeviceD0Exit - moving to %s\n",
                DbgDevicePowerString(TargetState));

    fdoData = FdoGetData(Device);

    fdoData->DevicePowerState = TargetState;

    switch (TargetState) {
    case WdfPowerDeviceD1:
    case WdfPowerDeviceD2:
    case WdfPowerDeviceD3:

        if(IsPoMgmtSupported(fdoData)){

            TraceEvents(TRACE_LEVEL_VERBOSE, DBG_POWER,
                        "Entering a deeper sleep state\n");
            MPSetPowerLow (fdoData, TargetState);
        }
        break;

    case WdfPowerDevicePrepareForHibernation:

        //
        // Fill in any code to save hardware state here.  Do not put in any
        // code to shut the device off.  If this device cannot support being
        // in the paging path (or being a parent or grandparent of a paging
        // path device) then this whole case can be deleted.
        //
        ASSERT(FALSE); // This driver shouldn't get this.
        break;

    case WdfPowerDeviceD3Final:
        //
        // Reset and put the device into a known initial state we're shutting
        // down for the last time.
        //
        NICShutdown(fdoData);
        break;

    default:
        break;
    }


    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_POWER, "<--PciDrvEvtDeviceD0Exit\n");

    return STATUS_SUCCESS;
}

NTSTATUS
PciDrvEvtDeviceSelfManagedIoInit(
    IN  WDFDEVICE Device
    )
/*++

Routine Description:

    PciDrvEvtDeviceSelfManagedIoInit is called by the Framework when the device
    enters the D0 state.  Its job is to start any I/O-related actions that the
    Framework isn't managing.  This might include releasing queues that are not
    power-managed, that is, the Framework is not automatically holding and releasing
    them across PnP/Power transitions. (The default behavior for WDFQUEUE is
    auto-managed, so most queues don't need to be dealt with here.)  This might
    also include setting up non-queue-based actions.

    If you allow the Framework to manage most or all of your queues, then when
    you build a driver from this sample, you can probably delete this function.

    In this driver, the SelfManagedIo callbacks are used to implement a watchdog timer.

    This function is not marked pagable because this function is in the
    device power up path. When a function is marked pagable and the code
    section is paged out, it will generate a page fault which could impact
    the fast resume behavior because the client driver will have to wait
    until the system drivers can service this page fault.

Arguments:

    Device - Handle to a framework device object.

Return Value:

    NTSTATUS - Failures will result in the device stack being torn down.

--*/
{
    PFDO_DATA           fdoData = NULL;
    WDF_TIMER_CONFIG    wdfTimerConfig;
    NTSTATUS            status;
    WDF_OBJECT_ATTRIBUTES timerAttributes;

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "--> PciDrvEvtDeviceSelfManagedIoInit\n");

    fdoData = FdoGetData(Device);
    //
    // To minimize init-time, create a timer DPC to do link detection.
    // This DPC will also be used to check for hardware hang.
    //
    WDF_TIMER_CONFIG_INIT(&wdfTimerConfig, NICWatchDogEvtTimerFunc);

    WDF_OBJECT_ATTRIBUTES_INIT(&timerAttributes);
    timerAttributes.ParentObject = fdoData->WdfDevice;

    status = WdfTimerCreate(
       &wdfTimerConfig,
       &timerAttributes,
       &fdoData->WatchDogTimer
       );

    if(!NT_SUCCESS(status) ) {
       TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP,
                   "Error: WdfTimerCreate create failed 0x%x\n", status);
       return status;
    }

    NICStartWatchDogTimer(fdoData);

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "<-- PciDrvEvtDeviceSelfManagedIoInit\n");

    return status;
}

NTSTATUS
PciDrvEvtDeviceSelfManagedIoSuspend(
    IN  WDFDEVICE Device
    )
/*++

Routine Description:

    EvtDeviceSelfManagedIoSuspend is called by the Framework before the device
    leaves the D0 state.  Its job is to stop any I/O-related actions that the
    Framework isn't managing, and which cannot be handled when the device
    hardware isn't available.  In general, this means reversing anything that
    was done in EvtDeviceSelfManagedIoStart.

    If you allow the Framework to manage most or all of your queues, then when
    you build a driver from this sample, you can probably delete this function.

Arguments:

    Device - Handle to a framework device object.

Return Value:

    NTSTATUS - Failures will result in the device stack being torn down.

--*/
{
    PFDO_DATA   fdoData = NULL;

    PAGED_CODE();

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "--> PciDrvEvtDeviceSelfManagedIoSuspend\n");

    fdoData = FdoGetData(Device);

    //
    // Stop the watchdog timer and wait for DPC to run to completion if
    // it's already fired.
    //
    WdfTimerStop(fdoData->WatchDogTimer, TRUE);

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "<-- PciDrvEvtDeviceSelfManagedIoSuspend\n");

    return STATUS_SUCCESS;
}

NTSTATUS
PciDrvEvtDeviceSelfManagedIoRestart(
    IN  WDFDEVICE Device
    )
/*++

Routine Description:

    EvtDeviceSelfManagedIoRestart is called by the Framework before the device
    is restarted for one of the following reasons:
    a) the PnP resources were rebalanced (framework received
        query-stop and stop IRPS )
    b) the device resumed from a low power state to D0.

    This function is not marked pagable because this function is in the
    device power up path. When a function is marked pagable and the code
    section is paged out, it will generate a page fault which could impact
    the fast resume behavior because the client driver will have to wait
    until the system drivers can service this page fault.

Arguments:

    Device - Handle to a framework device object.

Return Value:

    NTSTATUS - Failure will cause the device stack to be torn down.

--*/
{
    PFDO_DATA   fdoData;

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "--> PciDrvEvtDeviceSelfManagedIoRestart\n");

    fdoData = FdoGetData(Device);

    //
    // Restart the watchdog timer.
    //
    NICStartWatchDogTimer(fdoData);

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "<-- PciDrvEvtDeviceSelfManagedIoRestart\n");

    return STATUS_SUCCESS;
}

VOID
PciDrvEvtDeviceSelfManagedIoCleanup(
    IN  WDFDEVICE Device
    )
/*++

Routine Description:

    EvtDeviceSelfManagedIoCleanup is called by the Framework when the device is
    being torn down, either in response to the WDM IRP_MN_REMOVE_DEVICE
    It will be called only once.  Its job is to stop all outstanding I/O in the driver
    that the Framework is not managing.

Arguments:

    Device - Handle to a framework device object.

Return Value:

    None

--*/
{
    PFDO_DATA fdoData = NULL;

    PAGED_CODE();

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "--> PciDrvEvtDeviceSelfManagedIoCleanup\n");

    fdoData = FdoGetData(Device);

    if(fdoData->WatchDogTimer) {
        WdfObjectDelete(fdoData->WatchDogTimer);
    }

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "<-- PciDrvEvtDeviceSelfManagedIoCleanup\n");
}


VOID
PciDrvEvtIoDeviceControl(
    IN WDFQUEUE    Queue,
    IN WDFREQUEST  Request,
    IN size_t      OutputBufferLength,
    IN size_t      InputBufferLength,
    IN ULONG       IoControlCode
    )
/*++

Routine Description:

    This event is called when the framework receives IRP_MJ_DEVICE_CONTROL
    requests from the system.

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;
    PFDO_DATA               fdoData = NULL;
    WDFDEVICE               hDevice;
    WDF_REQUEST_PARAMETERS  params;

    UNREFERENCED_PARAMETER(OutputBufferLength);
    UNREFERENCED_PARAMETER(InputBufferLength);

    TraceEvents(TRACE_LEVEL_VERBOSE, DBG_IOCTLS,
                "PciDrvEvtIoDeviceControl called %p\n", Request);

    hDevice = WdfIoQueueGetDevice(Queue);
    fdoData = FdoGetData(hDevice);

    WDF_REQUEST_PARAMETERS_INIT(&params);

    WdfRequestGetParameters(
        Request,
        &params
        );

    switch (IoControlCode)
    {
        case IOCTL_NDISPROT_QUERY_OID_VALUE:

            ASSERT((IoControlCode & 0x3) == METHOD_BUFFERED);

            NICHandleQueryOidRequest(
                        Queue,
                        Request,
                        &params
                        );
            break;

        case IOCTL_NDISPROT_SET_OID_VALUE:

            ASSERT((IoControlCode & 0x3) == METHOD_BUFFERED);

            NICHandleSetOidRequest(
                        Queue,
                        Request,
                        &params
                        );

            break;

        case IOCTL_NDISPROT_INDICATE_STATUS:

            status = WdfRequestForwardToIoQueue(Request,
                                        fdoData->PendingIoctlQueue);
            if(!NT_SUCCESS(status)){
                TraceEvents(TRACE_LEVEL_ERROR, DBG_IOCTLS,
                      "WdfRequestForwardToIoQueue failed 0x%x\n", status);
                WdfRequestComplete(Request, status);
                break;
            }

            break;

         default:
            ASSERTMSG(FALSE, "Invalid IOCTL request\n");
            WdfRequestComplete(Request, STATUS_INVALID_DEVICE_REQUEST);
            break;
    }

    return;
}

NTSTATUS
PciDrvEvtDeviceWakeArmS0(
    IN WDFDEVICE Device
    )
/*++

Routine Description:

    EvtDeviceWakeArmS0 is called when the Framework arms the device for
    wake in the S0 state.  If there is any device-specific initialization
    that needs to be done to arm internal wake signals, or to route internal
    interrupt signals to the wake logic, it should be done here.  The device
    will be moved out of the D0 state soon after this callback is invoked.

    In this sample, wake from S0 involves waking on packet arrival, as does
    wake from Sx.  A more common NIC implementation might wake on cable
    insertion.

    This function is pageable and it will run at PASSIVE_LEVEL.

Arguments:

    Device - Handle to a Framework device object.

Return Value:

    NTSTATUS - Failure will result in the device remaining in the D0 state.

--*/
{
    NTSTATUS status;
    PFDO_DATA           fdoData;

    PAGED_CODE();

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "--> PciDrvEvtDeviceWakeArmS0\n");

    fdoData = FdoGetData(Device);

    //
    // Add pattern before sending wait-wake
    //
    status = NICConfigureForWakeUp(fdoData, TRUE);

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "<-- PciDrvEvtDeviceWakeArmS0 %x\n", status);

    return status;
}

NTSTATUS
PciDrvEvtDeviceWakeArmSx(
    IN WDFDEVICE Device
    )
/*++

Routine Description:

    EvtDeviceWakeArmSx is called when the Framework arms the device for
    wake from the S1, S2, S3 or S4 states.  If there is any device-specific
    initialization that needs to be done to arm internal wake signals, or to
    route internal interrupt signals to the wake logic, it should be done here.
    The device will be moved out of the D0 state soon after this callback is
    invoked.

    In this sample, wake from Sx involves arming for wake on packet arrival.
    Cable insertion should not be enabled, as nobody would want their machine
    to wake up simply because they plugged the cable in.

    This function runs at PASSIVE_LEVEL.  Whether it is pageable or not depends
    on whether the device has set DO_POWER_PAGABLE.

Arguments:

    Device - Handle to a Framework device object.

Return Value:

    NTSTATUS - Failure will result in the device not being armed for wake
    while the system is in Sx.

--*/
{
    NTSTATUS   status;
    PFDO_DATA  fdoData;

    PAGED_CODE();

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "--> PciDrvEvtDeviceWakeArmSx\n");

    fdoData = FdoGetData(Device);
    //
    // Add pattern before sending wait-wake
    //
    status = NICConfigureForWakeUp(fdoData, TRUE);

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "<-- PciDrvEvtDeviceWakeArmSx %x\n", status);

    return status;
}

VOID
PciDrvEvtDeviceWakeDisarmS0(
    IN WDFDEVICE Device
    )
/*++

Routine Description:

    EvtDeviceWakeDisarmS0 reverses anything done in EvtDeviceWakeArmS0.

    This function is not marked pageable because this function is in the
    device power up path. When a function is marked pagable and the code
    section is paged out, it will generate a page fault which could impact
    the fast resume behavior because the client driver will have to wait
    until the system drivers can service this page fault.

Arguments:

    Device - Handle to a Framework device object.

Return Value:

    VOID.

--*/
{
    NTSTATUS status;
    PFDO_DATA           fdoData;

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "--> PciDrvEvtDeviceWakeDisarmS0\n");

    fdoData = FdoGetData(Device);
    status = NICConfigureForWakeUp(fdoData, FALSE);

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "<-- PciDrvEvtDeviceWakeDisarmS0 %x\n", status);

    return ;
}

VOID
PciDrvEvtDeviceWakeDisarmSx(
    IN WDFDEVICE Device
    )
/*++

Routine Description:

    EvtDeviceWakeDisarmSx reverses anything done in EvtDeviceWakeArmSx.

    This function is not marked pageable because this function is in the
    device power up path. When a function is marked pagable and the code
    section is paged out, it will generate a page fault which could impact
    the fast resume behavior because the client driver will have to wait
    until the system drivers can service this page fault.

    This function runs at PASSIVE_LEVEL, even though it is not paged.  A
    driver can optionally make this function pageable if DO_POWER_PAGABLE
    is set.  Even if DO_POWER_PAGABLE isn't set, this function still runs
    at PASSIVE_LEVEL.  In this case, though, the function absolutely must
    not do anything that will cause a page fault.

Arguments:

    Device - Handle to a Framework device object.

Return Value:

    VOID

--*/
{
    NTSTATUS   status;
    PFDO_DATA  fdoData;

    UNREFERENCED_PARAMETER(Device);

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "--> PciDrvEvtDeviceWakeDisarmSx\n");

    fdoData = FdoGetData(Device);
    status = NICConfigureForWakeUp(fdoData, FALSE);

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "<-- PciDrvEvtDeviceWakeDisarmSx %x\n", status);

    return;
}

VOID
PciDrvEvtDeviceWakeTriggeredS0(
    IN WDFDEVICE Device
    )
/*++

Routine Description:

    EvtDeviceWakeTriggeredS0 will be called whenever the device triggers its
    wake signal after being armed for wake from S0.

    This function is pageable and runs at PASSIVE_LEVEL.

Arguments:

    Device - Handle to a Framework device object.

Return Value:

    VOID

--*/
{
    UNREFERENCED_PARAMETER(Device);

    PAGED_CODE();

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "--> PciDrvEvtDeviceWakeTriggeredS0\n");


    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "<-- PciDrvEvtDeviceWakeTriggeredS0\n");
}

VOID
PciDrvEvtDeviceWakeTriggeredSx(
    IN WDFDEVICE Device
    )
/*++

Routine Description:

    EvtDeviceWakeTriggeredSx will be called whenever the device triggers its
    wake signal after being armed for wake from Sx.

    This function is not marked pageable because this function is in the
    device power up path. When a function is marked pagable and the code
    section is paged out, it will generate a page fault which could impact
    the fast resume behavior because the client driver will have to wait
    until the system drivers can service this page fault.

    This function runs at PASSIVE_LEVEL, even though it is not paged.  A
    driver can optionally make this function pageable if DO_POWER_PAGABLE
    is set.  Even if DO_POWER_PAGABLE isn't set, this function still runs
    at PASSIVE_LEVEL.  In this case, though, the function absolutely must
    not do anything that will cause a page fault.

Arguments:

    Device - Handle to a Framework device object.

Return Value:

    VOID

--*/
{
    UNREFERENCED_PARAMETER(Device);

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "--> PciDrvEvtDeviceWakeTriggeredSx");


    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "<-- PciDrvEvtDeviceWakeTriggeredSx");

    return;
}


NTSTATUS
PciDrvQueuePassiveLevelCallback(
    IN PFDO_DATA        FdoData,
    IN PFN_WDF_WORKITEM CallbackFunction,
    IN PVOID            Context1,
    IN PVOID            Context2
    )
/*++
                                         Routine Description:

    This routine is used to queue workitems so that the callback
    functions can be executed at PASSIVE_LEVEL in the conext of
    a system thread.

Arguments:

   FdoData - pointer to a device extenion.

   CallbackFunction - Function to invoke when at PASSIVE_LEVEL.

   Context1 & 2 - Meaning of the context values depends on the
                  callback function.

Return Value:

--*/
{
    NTSTATUS                status = STATUS_SUCCESS;
    PWORKER_ITEM_CONTEXT    context;
    WDF_OBJECT_ATTRIBUTES   attributes;
    WDF_WORKITEM_CONFIG     workitemConfig;
    WDFWORKITEM             hWorkItem;

    WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, WORKER_ITEM_CONTEXT);

    attributes.ParentObject = FdoData->WdfDevice;

    WDF_WORKITEM_CONFIG_INIT(&workitemConfig, CallbackFunction);

    status = WdfWorkItemCreate( &workitemConfig,
                                &attributes,
                                &hWorkItem);

    if (!NT_SUCCESS(status)) {
        return status;
    }

    context = GetWorkItemContext(hWorkItem);

    context->FdoData = FdoData;
    context->Argument1 = Context1;
    context->Argument2 = Context2;

    //
    // Execute this work item.
    //
    WdfWorkItemEnqueue(hWorkItem);

    return STATUS_SUCCESS;
}


BOOLEAN
PciDrvReadRegistryValue(
    _In_  PFDO_DATA   FdoData,
    _In_  PWSTR       Name,
    _Out_ PULONG      Value
    )
/*++

Routine Description:

    Can be used to read any REG_DWORD registry value stored
    under Device Parameter.

Arguments:

    FdoData - pointer to the device extension
    Name - Name of the registry value
    Value -


Return Value:

   TRUE if successful
   FALSE if not present/error in reading registry

--*/
{
    WDFKEY      hKey = NULL;
    NTSTATUS    status;
    BOOLEAN     retValue = FALSE;
    UNICODE_STRING  valueName;



    PAGED_CODE();

    TraceEvents(TRACE_LEVEL_VERBOSE, DBG_INIT,
                "-->PciDrvReadRegistryValue \n");

    *Value = 0;

    status = WdfDeviceOpenRegistryKey(FdoData->WdfDevice,
                                      PLUGPLAY_REGKEY_DEVICE,
                                      STANDARD_RIGHTS_ALL,
                                      WDF_NO_OBJECT_ATTRIBUTES,
                                      &hKey);

    if (NT_SUCCESS (status)) {

        RtlInitUnicodeString(&valueName,Name);

        status = WdfRegistryQueryULong( hKey,
                                        &valueName,
                                        Value );

        if (NT_SUCCESS (status)) {
            retValue = TRUE;
        }

        WdfRegistryClose(hKey);
    }

    TraceEvents(TRACE_LEVEL_VERBOSE, DBG_INIT,
                "<--PciDrvReadRegistryValue %ws %d \n", Name, *Value);

    return retValue;
}

BOOLEAN
PciDrvWriteRegistryValue(
    _In_ PFDO_DATA  FdoData,
    _In_ PWSTR      Name,
    _In_ ULONG      Value
    )
/*++

Routine Description:

    Can be used to write any REG_DWORD registry value stored
    under Device Parameter.

Arguments:


Return Value:

   TRUE - if write is successful
   FALSE - otherwise

--*/
{
    WDFKEY          hKey = NULL;
    NTSTATUS        status;
    BOOLEAN         retValue = FALSE;
    UNICODE_STRING  valueName;


    PAGED_CODE();

    TraceEvents(TRACE_LEVEL_VERBOSE, DBG_INIT,
                "Entered PciDrvWriteRegistryValue\n");

    //
    // write the value out to the registry
    //
    status = WdfDeviceOpenRegistryKey(FdoData->WdfDevice,
                                      PLUGPLAY_REGKEY_DEVICE,
                                      STANDARD_RIGHTS_ALL,
                                      WDF_NO_OBJECT_ATTRIBUTES,
                                      &hKey);

    if (NT_SUCCESS (status)) {

        RtlInitUnicodeString(&valueName,Name);

        status = WdfRegistryAssignULong (hKey,
                                         &valueName,
                                         Value );

        if (NT_SUCCESS (status)) {
            retValue = TRUE;
        }

        WdfRegistryClose(hKey);
    }

    return retValue;

}

#define PARAMATER_NAME_LEN 80

BOOLEAN
PciDrvReadFdoRegistryKeyValue(
    _In_  PWDFDEVICE_INIT  DeviceInit,
    _In_  PWSTR            Name,
    _Out_ PULONG           Value
    )
/*++

Routine Description:

    Can be used to read any REG_DWORD registry value stored
    under Device Parameter.

Arguments:

    FdoData - pointer to the device extension
    Name - Name of the registry value
    Value -


Return Value:

   TRUE if successful
   FALSE if not present/error in reading registry

--*/
{
    WDFKEY      hKey = NULL;
    NTSTATUS    status;
    BOOLEAN     retValue = FALSE;
    UNICODE_STRING  valueName;

    PAGED_CODE();

    TraceEvents(TRACE_LEVEL_VERBOSE, DBG_PNP,
                "-->PciDrvReadFdoRegistryKeyValue\n");

    *Value = 0;

    status = WdfFdoInitOpenRegistryKey(DeviceInit,
                                  PLUGPLAY_REGKEY_DEVICE,
                                  STANDARD_RIGHTS_ALL,
                                  WDF_NO_OBJECT_ATTRIBUTES,
                                  &hKey);

    if (NT_SUCCESS (status)) {

        RtlInitUnicodeString(&valueName,Name);

        status = WdfRegistryQueryULong (hKey,
                                  &valueName,
                                  Value);

        if (NT_SUCCESS (status)) {
            retValue = TRUE;
        }

        WdfRegistryClose(hKey);
    }

    TraceEvents(TRACE_LEVEL_VERBOSE, DBG_PNP,
                "<--PciDrvReadFdoRegistryKeyValue %ws %d \n",
                Name, *Value);

    return retValue;
}

VOID
PciDrvEvtDriverContextCleanup(
    IN WDFOBJECT Driver
    )
/*++
Routine Description:

    Free all the resources allocated in DriverEntry.

Arguments:

    Driver - handle to a WDF Driver object.

Return Value:

    VOID.

--*/
{
    PDRIVER_CONTEXT driverContext;

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_INIT,
                    "--> PciDrvEvtDriverContextCleanup\n");
    PAGED_CODE ();

    driverContext = GetDriverContext((WDFDRIVER)Driver);

    if (driverContext->RecvLookaside) {
        WdfObjectDelete(driverContext->RecvLookaside);
    }
    //
    // Stop WPP Tracing
    //
    WPP_CLEANUP( WdfDriverWdmGetDriverObject( (WDFDRIVER)Driver ) );

}

NTSTATUS
PciDrvSetPowerPolicy(
        IN PFDO_DATA FdoData
    )
{
    WDF_DEVICE_POWER_POLICY_IDLE_SETTINGS idleSettings;
    WDF_DEVICE_POWER_POLICY_WAKE_SETTINGS wakeSettings;
    NTSTATUS    status = STATUS_SUCCESS;

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "--> PciDrvSetPowerPolicy\n");

    PAGED_CODE();

    //
    // Init the idle policy structure.
    //
    WDF_DEVICE_POWER_POLICY_IDLE_SETTINGS_INIT(&idleSettings, IdleCanWakeFromS0);
    idleSettings.IdleTimeout = 10000; // 10-sec

    status = WdfDeviceAssignS0IdleSettings(FdoData->WdfDevice, &idleSettings);
    if ( !NT_SUCCESS(status)) {
        TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP,
                    "WdfDeviceSetPowerPolicyS0IdlePolicy failed %x\n", status);
        return status;
    }

    //
    // Init wait-wake policy structure.
    //
    WDF_DEVICE_POWER_POLICY_WAKE_SETTINGS_INIT(&wakeSettings);

    status = WdfDeviceAssignSxWakeSettings(FdoData->WdfDevice, &wakeSettings);
    if (!NT_SUCCESS(status)) {
        TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP,
                    "WdfDeviceAssignSxWakeSettings failed %x\n", status);
        return status;
    }


    //
    // Functions that program wakeup patterns on the device
    // check this variable to see whether the NDIS edge has enabled
    // wakeup on this device. If there is no ndis edge, this variable and all
    // the checks can be removed because framework as a power policy owner
    // it knows when to call the driver to arm/disarm for wakeup.
    //
    FdoData->AllowWakeArming = TRUE;

    TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP,
                "<-- PciDrvSetPowerPolicy\n");

    return status;
}

PCHAR
DbgDevicePowerString(
    IN WDF_POWER_DEVICE_STATE Type
    )
/*++

Updated Routine Description:
    DbgDevicePowerString does not change in this stage of the function driver.

--*/
{
    switch (Type)
    {
    case WdfPowerDeviceInvalid:
        return "WdfPowerDeviceInvalid";
    case WdfPowerDeviceD0:
        return "WdfPowerDeviceD0";
    case WdfPowerDeviceD1:
        return "WdfPowerDeviceD1";
    case WdfPowerDeviceD2:
        return "WdfPowerDeviceD2";
    case WdfPowerDeviceD3:
        return "WdfPowerDeviceD3";
    case WdfPowerDeviceD3Final:
        return "WdfPowerDeviceD3Final";
    case WdfPowerDevicePrepareForHibernation:
        return "WdfPowerDevicePrepareForHibernation";
    case WdfPowerDeviceMaximum:
        return "WdfPowerDeviceMaximum";
    default:
        return "UnKnown Device Power State";
    }
}


#if !defined(EVENT_TRACING)

VOID
TraceEvents    (
    IN ULONG   TraceEventsLevel,
    IN ULONG   TraceEventsFlag,
    IN PCCHAR  DebugMessage,
    ...
    )

/*++

Routine Description:

    Debug print for the sample driver.

Arguments:

    TraceEventsLevel - print level between 0 and 3, with 3 the most verbose

Return Value:

    None.

 --*/
 {
#if DBG
#define     TEMP_BUFFER_SIZE        512
    va_list    list;
    CHAR       debugMessageBuffer[TEMP_BUFFER_SIZE];
    NTSTATUS   status;

    va_start(list, DebugMessage);

    if (DebugMessage) {

        //
        // Using new safe string functions instead of _vsnprintf.
        // This function takes care of NULL terminating if the message
        // is longer than the buffer.
        //
        status = RtlStringCbVPrintfA( debugMessageBuffer,
                                      sizeof(debugMessageBuffer),
                                      DebugMessage,
                                      list );
        if(!NT_SUCCESS(status)) {

            DbgPrint (_DRIVER_NAME_": RtlStringCbVPrintfA failed %x\n",
                      status);
            return;
        }
        if (TraceEventsLevel <= TRACE_LEVEL_INFORMATION ||
            (TraceEventsLevel <= DebugLevel &&
             ((TraceEventsFlag & DebugFlag) == TraceEventsFlag))) {
            DbgPrint(debugMessageBuffer);
        }
    }
    va_end(list);

    return;
#else
    UNREFERENCED_PARAMETER(TraceEventsLevel);
    UNREFERENCED_PARAMETER(TraceEventsFlag);
    UNREFERENCED_PARAMETER(DebugMessage);
#endif
}

#endif