summaryrefslogtreecommitdiff
path: root/simbatt/func/wdf.c
blob: 3b9e87adf07882ddfd92eba1e773134a29761d3c (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
/*++

Copyright (c) Microsoft Corporation. All rights reserved.

Module Name:

    wdf.c

Abstract:
    This module implements WDF and WDM functionality required to register as a
    device driver, instantiate devices, and register those devices with the
    battery class driver.

    N.B. This code is provided "AS IS" without any expressed or implied warranty.

--*/

//--------------------------------------------------------------------- Includes

#include "simbatt.h"
#include "simbattdriverif.h"
#include <batclass.h>

//------------------------------------------------------------------- Prototypes

DRIVER_INITIALIZE DriverEntry;
EVT_WDF_DRIVER_DEVICE_ADD SimBattDriverDeviceAdd;
EVT_WDF_DEVICE_SELF_MANAGED_IO_INIT  SimBattSelfManagedIoInit;
EVT_WDF_DEVICE_SELF_MANAGED_IO_CLEANUP  SimBattSelfManagedIoCleanup;
EVT_WDF_DEVICE_QUERY_STOP SimBattQueryStop;
EVT_WDF_DEVICE_PREPARE_HARDWARE SimBattDevicePrepareHardware;
EVT_WDFDEVICE_WDM_IRP_PREPROCESS SimBattWdmIrpPreprocessDeviceControl;
EVT_WDFDEVICE_WDM_IRP_PREPROCESS SimBattWdmIrpPreprocessSystemControl;
WMI_QUERY_REGINFO_CALLBACK SimBattQueryWmiRegInfo;
WMI_QUERY_DATABLOCK_CALLBACK SimBattQueryWmiDataBlock;

//---------------------------------------------------------------------- Pragmas

#pragma alloc_text(INIT, DriverEntry)
#pragma alloc_text(PAGE, SimBattSelfManagedIoInit)
#pragma alloc_text(PAGE, SimBattSelfManagedIoCleanup)
#pragma alloc_text(PAGE, SimBattQueryStop)
#pragma alloc_text(PAGE, SimBattDriverDeviceAdd)
#pragma alloc_text(PAGE, SimBattDevicePrepareHardware)
#pragma alloc_text(PAGE, SimBattWdmIrpPreprocessDeviceControl)
#pragma alloc_text(PAGE, SimBattWdmIrpPreprocessSystemControl)
#pragma alloc_text(PAGE, SimBattQueryWmiRegInfo)
#pragma alloc_text(PAGE, SimBattQueryWmiDataBlock)

//-------------------------------------------------------------------- Functions

_Use_decl_annotations_
NTSTATUS
DriverEntry (
    PDRIVER_OBJECT DriverObject,
    PUNICODE_STRING RegistryPath
    )

/*++

Routine Description:

    DriverEntry initializes the driver and is the first routine called by the
    system after the driver is loaded. DriverEntry configures and creates a WDF
    driver object.

Parameters Description:

    DriverObject - Supplies a pointer to the driver object.

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

Return Value:

    NTSTATUS.

--*/

{

    WDF_OBJECT_ATTRIBUTES DriverAttributes;
    WDF_DRIVER_CONFIG DriverConfig;
    PSIMBATT_GLOBAL_DATA GlobalData;
    NTSTATUS Status;

    DebugEnter();

    WDF_DRIVER_CONFIG_INIT(&DriverConfig, SimBattDriverDeviceAdd);

    //
    // Initialize attributes and a context area for the driver object.
    //
    // N.B. ExecutionLevel is set to Passive because this driver expect callback
    //      functions to be called at PASSIVE_LEVEL.
    //
    // N.B. SynchronizationScope is not specified and therefore it is set to
    //      None. This means that the WDF framework does not synchronize the
    //      callbacks, you may want to set this to a different value based on
    //      how the callbacks are required to be synchronized in your driver.
    //

    WDF_OBJECT_ATTRIBUTES_INIT(&DriverAttributes);
    WDF_OBJECT_ATTRIBUTES_SET_CONTEXT_TYPE(&DriverAttributes,
                                           SIMBATT_GLOBAL_DATA);

    DriverAttributes.ExecutionLevel = WdfExecutionLevelPassive;

    //
    // Create the driver object
    //

    Status = WdfDriverCreate(DriverObject,
                             RegistryPath,
                             &DriverAttributes,
                             &DriverConfig,
                             WDF_NO_HANDLE);

    if (!NT_SUCCESS(Status)) {
        DebugPrint(SIMBATT_ERROR,
                   "WdfDriverCreate() Failed. Status 0x%x\n",
                   Status);

        goto DriverEntryEnd;
    }

    GlobalData = GetGlobalData(WdfGetDriver());
    GlobalData->RegistryPath.MaximumLength = RegistryPath->Length +
                                             sizeof(UNICODE_NULL);

    GlobalData->RegistryPath.Length = RegistryPath->Length;
    GlobalData->RegistryPath.Buffer = WdfDriverGetRegistryPath(WdfGetDriver());

DriverEntryEnd:
    DebugExitStatus(Status);
    return Status;
}

_Use_decl_annotations_
NTSTATUS
SimBattDriverDeviceAdd (
    WDFDRIVER Driver,
    PWDFDEVICE_INIT DeviceInit
    )

/*++
Routine Description:

    EvtDriverDeviceAdd is called by the framework in response to AddDevice
    call from the PnP manager. A WDF device object is created and initialized to
    represent a new instance of the battery device.

Arguments:

    Driver - Supplies a handle to the WDF Driver object.

    DeviceInit - Supplies a pointer to a framework-allocated WDFDEVICE_INIT
        structure.

Return Value:

    NTSTATUS

--*/

{

    WDF_OBJECT_ATTRIBUTES DeviceAttributes;
    PSIMBATT_FDO_DATA DevExt;
    WDFDEVICE DeviceHandle;  
    WDF_OBJECT_ATTRIBUTES LockAttributes;
    WDF_PNPPOWER_EVENT_CALLBACKS PnpPowerCallbacks;
    WDFQUEUE Queue;
    WDF_IO_QUEUE_CONFIG QueueConfig;
    NTSTATUS Status;

    UNREFERENCED_PARAMETER(Driver);

    DebugEnter();
    PAGED_CODE();

    //
    // Initialize the PnpPowerCallbacks structure.  Callback events for PNP
    // and Power are specified here.
    //

    WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&PnpPowerCallbacks);
    PnpPowerCallbacks.EvtDevicePrepareHardware = SimBattDevicePrepareHardware;
    PnpPowerCallbacks.EvtDeviceSelfManagedIoInit = SimBattSelfManagedIoInit;
    PnpPowerCallbacks.EvtDeviceSelfManagedIoCleanup = SimBattSelfManagedIoCleanup;
    PnpPowerCallbacks.EvtDeviceQueryStop = SimBattQueryStop;
    WdfDeviceInitSetPnpPowerEventCallbacks(DeviceInit, &PnpPowerCallbacks);

    //
    // Register WDM preprocess callbacks for IRP_MJ_DEVICE_CONTROL and
    // IRP_MJ_SYSTEM_CONTROL. The battery class driver needs to handle these IO
    // requests directly.
    //

    Status = WdfDeviceInitAssignWdmIrpPreprocessCallback(
                 DeviceInit,
                 SimBattWdmIrpPreprocessDeviceControl,
                 IRP_MJ_DEVICE_CONTROL,
                 NULL,
                 0);

    if (!NT_SUCCESS(Status)) {
         DebugPrint(SIMBATT_ERROR,
                    "WdfDeviceInitAssignWdmIrpPreprocessCallback"
                    "(IRP_MJ_DEVICE_CONTROL) Failed. 0x%x\n",
                    Status);

         goto DriverDeviceAddEnd;
    }

    Status = WdfDeviceInitAssignWdmIrpPreprocessCallback(
                 DeviceInit,
                 SimBattWdmIrpPreprocessSystemControl,
                 IRP_MJ_SYSTEM_CONTROL,
                 NULL,
                 0);

    if (!NT_SUCCESS(Status)) {
         DebugPrint(SIMBATT_ERROR,
                    "WdfDeviceInitAssignWdmIrpPreprocessCallback"
                    "(IRP_MJ_SYSTEM_CONTROL) Failed. 0x%x\n",
                    Status);

         goto DriverDeviceAddEnd;
    }

    //
    // Initialize attributes and a context area for the device object.
    //

    WDF_OBJECT_ATTRIBUTES_INIT(&DeviceAttributes);
    WDF_OBJECT_ATTRIBUTES_SET_CONTEXT_TYPE(&DeviceAttributes, SIMBATT_FDO_DATA);

    //
    // Create a framework device object.  This call will in turn create
    // a WDM device object, attach to the lower stack, and set the
    // appropriate flags and attributes.
    //

    Status = WdfDeviceCreate(&DeviceInit, &DeviceAttributes, &DeviceHandle);
    if (!NT_SUCCESS(Status)) {
        DebugPrint(SIMBATT_ERROR, "WdfDeviceCreate() Failed. 0x%x\n", Status);
        goto DriverDeviceAddEnd;
    }

    //
    // Configure a default queue for IO requests that are not handled by the
    // class driver. For the simulated battery, this queue processes requests
    // to set the simulated status.
    //

    WDF_IO_QUEUE_CONFIG_INIT_DEFAULT_QUEUE(&QueueConfig,
                                           WdfIoQueueDispatchSequential);

    QueueConfig.EvtIoDeviceControl = SimBattIoDeviceControl;
    Status = WdfIoQueueCreate(DeviceHandle,
                              &QueueConfig,
                              WDF_NO_OBJECT_ATTRIBUTES,
                              &Queue);

    if (!NT_SUCCESS(Status)) {
        DebugPrint(SIMBATT_ERROR, "WdfIoQueueCreate() Failed. 0x%x\n", Status);
        goto DriverDeviceAddEnd;
    }

    //
    // Create a device interface for this device to advertise the simulated
    // battery IO interface.
    //

    Status = WdfDeviceCreateDeviceInterface(DeviceHandle,
                                            &SIMBATT_DEVINTERFACE_GUID,
                                            NULL);

    if (!NT_SUCCESS(Status)) {
        goto DriverDeviceAddEnd;
    }

    //
    // Finish initializing the device context area.
    //

    DevExt = GetDeviceExtension(DeviceHandle);
    DevExt->BatteryTag = BATTERY_TAG_INVALID;
    DevExt->ClassHandle = NULL;
    WDF_OBJECT_ATTRIBUTES_INIT(&LockAttributes);
    LockAttributes.ParentObject = DeviceHandle;
    Status = WdfWaitLockCreate(&LockAttributes,
                               &DevExt->ClassInitLock);

    if (!NT_SUCCESS(Status)) {
        DebugPrint(SIMBATT_ERROR,
                   "WdfWaitLockCreate(ClassInitLock) Failed. Status 0x%x\n",
                   Status);

        goto DriverDeviceAddEnd;
    }

    WDF_OBJECT_ATTRIBUTES_INIT(&LockAttributes);
    LockAttributes.ParentObject = DeviceHandle;
    Status = WdfWaitLockCreate(&LockAttributes,
                               &DevExt->StateLock);

    if (!NT_SUCCESS(Status)) {
        DebugPrint(SIMBATT_ERROR,
                   "WdfWaitLockCreate(StateLock) Failed. Status 0x%x\n",
                   Status);

        goto DriverDeviceAddEnd;
    }

DriverDeviceAddEnd:
    DebugExitStatus(Status);
    return Status;
}

_Use_decl_annotations_
NTSTATUS
SimBattSelfManagedIoInit (
    WDFDEVICE Device
    )

/*++

Routine Description:

    The framework calls this function once per device after EvtDeviceD0Entry
    callback has been called for the first time. This function is not called
    again unless device is remove and reconnected or the drivers are reloaded.

Arguments:

    Device - Supplies a handle to a framework device object.

Return Value:

    NTSTATUS

--*/

{

    BATTERY_MINIPORT_INFO_V1_1 BattInit;
    PSIMBATT_FDO_DATA DevExt;
    PDEVICE_OBJECT DeviceObject;
    NTSTATUS Status;

    DebugEnter();
    PAGED_CODE();

    DevExt = GetDeviceExtension(Device);

    //
    // Attach to the battery class driver.
    //

    RtlZeroMemory(&BattInit, sizeof(BattInit));
    BattInit.MajorVersion = BATTERY_CLASS_MAJOR_VERSION;
    BattInit.MinorVersion = BATTERY_CLASS_MINOR_VERSION_1;
    BattInit.Context = DevExt;
    BattInit.QueryTag = SimBattQueryTag;
    BattInit.QueryInformation = SimBattQueryInformation;
    BattInit.SetInformation = SimBattSetInformation;
    BattInit.QueryStatus = SimBattQueryStatus;
    BattInit.SetStatusNotify = SimBattSetStatusNotify;
    BattInit.DisableStatusNotify = SimBattDisableStatusNotify;
    BattInit.Pdo = WdfDeviceWdmGetPhysicalDevice(Device);
    BattInit.DeviceName = NULL;
    BattInit.Fdo = WdfDeviceWdmGetDeviceObject(Device);
    WdfWaitLockAcquire(DevExt->ClassInitLock, NULL);
    Status = BatteryClassInitializeDevice((PBATTERY_MINIPORT_INFO)&BattInit,
                                          &DevExt->ClassHandle);

    WdfWaitLockRelease(DevExt->ClassInitLock);
    if (!NT_SUCCESS(Status)) {
        goto DevicePrepareHardwareEnd;
    }

    //
    // Register the device as a WMI data provider. This is done using WDM
    // methods because the battery class driver uses WDM methods to complete
    // WMI requests.
    //

    DevExt->WmiLibContext.GuidCount = 0;
    DevExt->WmiLibContext.GuidList = NULL;
    DevExt->WmiLibContext.QueryWmiRegInfo = SimBattQueryWmiRegInfo;
    DevExt->WmiLibContext.QueryWmiDataBlock = SimBattQueryWmiDataBlock;
    DevExt->WmiLibContext.SetWmiDataBlock = NULL;
    DevExt->WmiLibContext.SetWmiDataItem = NULL;
    DevExt->WmiLibContext.ExecuteWmiMethod = NULL;
    DevExt->WmiLibContext.WmiFunctionControl = NULL;
    DeviceObject = WdfDeviceWdmGetDeviceObject(Device);
    Status = IoWMIRegistrationControl(DeviceObject, WMIREG_ACTION_REGISTER);

    //
    // Failure to register with WMI is nonfatal.
    //

    if (!NT_SUCCESS(Status)) {
        DebugPrint(SIMBATT_WARN,
                   "IoWMIRegistrationControl() Failed. Status 0x%x\n",
                   Status);

        Status = STATUS_SUCCESS;
    }

DevicePrepareHardwareEnd:
    DebugExitStatus(Status);
    return Status;
}

_Use_decl_annotations_
VOID
SimBattSelfManagedIoCleanup (
    WDFDEVICE Device
    )

/*++

Routine Description:

    This function is called after EvtDeviceSelfManagedIoSuspend callback. This
    function must release any sel-managed I/O operation data.

Arguments:

    Device - Supplies a handle to a framework device object.

Return Value:

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

--*/

{

    PSIMBATT_FDO_DATA DevExt;
    PDEVICE_OBJECT DeviceObject;
    NTSTATUS Status;

    DebugEnter();
    PAGED_CODE();

    DeviceObject = WdfDeviceWdmGetDeviceObject(Device);
    Status = IoWMIRegistrationControl(DeviceObject, WMIREG_ACTION_DEREGISTER);
    if (!NT_SUCCESS(Status)) {
        DebugPrint(SIMBATT_WARN,
                   "IoWMIRegistrationControl() Failed. Status 0x%x\n",
                   Status);

        Status = STATUS_SUCCESS;
    }

    DevExt = GetDeviceExtension(Device);
    WdfWaitLockAcquire(DevExt->ClassInitLock, NULL);
    if (DevExt->ClassHandle != NULL) {
        Status = BatteryClassUnload(DevExt->ClassHandle);
        DevExt->ClassHandle = NULL;
    }

    WdfWaitLockRelease(DevExt->ClassInitLock);
    DebugExitStatus(Status);
    return;
}

_Use_decl_annotations_
NTSTATUS
SimBattQueryStop (
    _In_ WDFDEVICE Device
    )

/*++

Routine Description:

    EvtDeviceQueryStop event callback function determines whether a specified 
    device can be stopped so that the PnP manager can redistribute system 
    hardware resources.

    SimBatt is designed to fail a rebalance operation, for reasons described 
    below. Note however that this approach must *not* be adopted by an actual
    battery driver. 

    SimBatt unregisters itself as a Battery driver by calling 
    BatteryClassUnload() when IRP_MN_STOP_DEVICE arrives at the driver. It
    re-establishes itself as a Battery driver on arrival of IRP_MN_START_DEVICE.
    This results in any IOCTLs normally handeled by the Battery Class driver to 
    be delivered to SimBatt. The IO Queue employed by SimBatt is power managed,
    it causes these IOCTLs to be pended when SimBatt is not in D0. Now if the 
    device attempts to do a shutdown while an IOCTL is pended in SimBatt, it
    would result in a 0x9F bugcheck. By opting out of PNP rebalance operation 
    SimBatt circumvents this issue.

Arguments:

    Device - Supplies a handle to a framework device object.

Return Value:

    NTSTATUS

--*/

{

    UNREFERENCED_PARAMETER(Device);

    PAGED_CODE();

    return STATUS_UNSUCCESSFUL;
}

_Use_decl_annotations_
NTSTATUS
SimBattDevicePrepareHardware (
    WDFDEVICE Device,
    WDFCMRESLIST ResourcesRaw,
    WDFCMRESLIST ResourcesTranslated
    )

/*++

Routine Description:

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

Arguments:

    Device - Supplies a handle to a framework device object.

    ResourcesRaw - Supplies a 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 - Supplies a 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

--*/

{

    NTSTATUS Status;

    UNREFERENCED_PARAMETER(ResourcesRaw);
    UNREFERENCED_PARAMETER(ResourcesTranslated);

    DebugEnter();
    PAGED_CODE();

    SimBattPrepareHardware(Device);
    Status = STATUS_SUCCESS;
    DebugExitStatus(Status);
    return Status;
}

_Use_decl_annotations_
NTSTATUS
SimBattWdmIrpPreprocessDeviceControl (
    WDFDEVICE Device,
    PIRP Irp
    )

/*++

Routine Description:

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

    N.B. Battery stack requires the device IOCTLs be sent to it at
         PASSIVE_LEVEL only, any IOCTL comming from user mode is therefore
         fine, kernel components, such as filter drivers sitting on top of
         the battery drivers should be careful to not voilate this
         requirement.

Arguments:

    Device - Supplies a handle to a framework device object.

    Irp - Supplies the IO request being processed.

Return Value:

    NTSTATUS

--*/

{

    PSIMBATT_FDO_DATA DevExt;
    NTSTATUS Status;

    PAGED_CODE();
    DebugEnter();

    ASSERTMSG("Must be called at IRQL = PASSIVE_LEVEL",
              (KeGetCurrentIrql() == PASSIVE_LEVEL));

    DevExt = GetDeviceExtension(Device);
    Status = STATUS_NOT_SUPPORTED;

    //
    // Suppress 28118:Irq Exceeds Caller, see Routine Description for
    // explaination.
    //

    #pragma warning(suppress: 28118)
    WdfWaitLockAcquire(DevExt->ClassInitLock, NULL);

    //
    // N.B. An attempt to queue the IRP with the port driver should happen
    //      before WDF assumes ownership of this IRP, i.e. before
    //      WdfDeviceWdmDispatchPreprocessedIrp is called, this is so that the
    //      Battery port driver, which is a WDM driver, may complete the IRP if
    //      it does endup procesing it.
    //

    if (DevExt->ClassHandle != NULL) {

        //
        // Suppress 28118:Irq Exceeds Caller, see above N.B.
        //

        #pragma warning(suppress: 28118)
        Status = BatteryClassIoctl(DevExt->ClassHandle, Irp);
    }

    WdfWaitLockRelease(DevExt->ClassInitLock);
    if (Status == STATUS_NOT_SUPPORTED) {
        IoSkipCurrentIrpStackLocation(Irp);
        Status = WdfDeviceWdmDispatchPreprocessedIrp(Device, Irp);
    }

    DebugExitStatus(Status);
    return Status;
}

_Use_decl_annotations_
NTSTATUS
SimBattWdmIrpPreprocessSystemControl (
    WDFDEVICE Device,
    PIRP Irp
    )

/*++

Routine Description:

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

    N.B. Battery stack requires the device IOCTLs be sent to it at
         PASSIVE_LEVEL only, any IOCTL comming from user mode is therefore
         fine, kernel components, such as filter drivers sitting on top of
         the battery drivers should be careful to not voilate this
         requirement.

Arguments:

    Device - Supplies a handle to a framework device object.

    Irp - Supplies the IO request being processed.

Return Value:

    NTSTATUS

--*/

{

    PSIMBATT_FDO_DATA DevExt;
    PDEVICE_OBJECT DeviceObject;
    SYSCTL_IRP_DISPOSITION Disposition;
    NTSTATUS Status;

    DebugEnter();
    PAGED_CODE();

    ASSERTMSG("Must be called at IRQL = PASSIVE_LEVEL",(KeGetCurrentIrql() == PASSIVE_LEVEL));

    Status = STATUS_NOT_IMPLEMENTED;
    DevExt = GetDeviceExtension(Device);
    Disposition = IrpForward;

    //
    // Acquire the class initialization lock and attempt to queue the IRP with
    // the class driver.
    //
    // Suppress 28118:Irq Exceeds Caller, see Routine Description for
    // explaination.
    //

    #pragma warning(suppress: 28118)
    WdfWaitLockAcquire(DevExt->ClassInitLock, NULL);
    if (DevExt->ClassHandle != NULL) {
        DeviceObject = WdfDeviceWdmGetDeviceObject(Device);
        Status = BatteryClassSystemControl(DevExt->ClassHandle,
                                           &DevExt->WmiLibContext,
                                           DeviceObject,
                                           Irp,
                                           &Disposition);
    }

    WdfWaitLockRelease(DevExt->ClassInitLock);
    switch (Disposition) {
    case IrpProcessed:
        break;

    case IrpNotCompleted:
        IoCompleteRequest(Irp, IO_NO_INCREMENT);
        break;

    case IrpForward:
    case IrpNotWmi:
    default:
        IoSkipCurrentIrpStackLocation(Irp);
        Status = WdfDeviceWdmDispatchPreprocessedIrp(Device, Irp);
        break;
    }

    DebugExitStatus(Status);
    return Status;
}

_Use_decl_annotations_
NTSTATUS
SimBattQueryWmiRegInfo (
    PDEVICE_OBJECT DeviceObject,
    PULONG RegFlags,
    PUNICODE_STRING InstanceName,
    PUNICODE_STRING *RegistryPath,
    PUNICODE_STRING MofResourceName,
    PDEVICE_OBJECT *Pdo
    )

/*++

Routine Description:

    This routine is a callback into the driver to retrieve the list of
    guids or data blocks that the driver wants to register with WMI. This
    routine may not pend or block. Driver should NOT call
    WmiCompleteRequest.

Arguments:

    DeviceObject - Supplies the device whose data block is being queried.

    RegFlags - Supplies a pointer to return a set of flags that describe the
        guids being registered for this device. If the device wants enable and
        disable collection callbacks before receiving queries for the registered
        guids then it should return the WMIREG_FLAG_EXPENSIVE flag. Also the
        returned flags may specify WMIREG_FLAG_INSTANCE_PDO in which case
        the instance name is determined from the PDO associated with the
        device object. Note that the PDO must have an associated devnode. If
        WMIREG_FLAG_INSTANCE_PDO is not set then Name must return a unique
        name for the device.

    InstanceName - Supplies a pointer to return the instance name for the guids
        if WMIREG_FLAG_INSTANCE_PDO is not set in the returned *RegFlags. The
        caller will call ExFreePool with the buffer returned.

    RegistryPath - Supplies a pointer to return the registry path of the driver.

    MofResourceName - Supplies a pointer to return the name of the MOF resource
        attached to the binary file. If the driver does not have a mof resource
        attached then this can be returned as NULL.

    Pdo - Supplies a pointer to return the device object for the PDO associated
        with this device if the WMIREG_FLAG_INSTANCE_PDO flag is returned in
        *RegFlags.

Return Value:

    NTSTATUS

--*/

{

    WDFDEVICE Device;
    PSIMBATT_GLOBAL_DATA GlobalData;
    NTSTATUS Status;

    UNREFERENCED_PARAMETER(MofResourceName);
    UNREFERENCED_PARAMETER(InstanceName);

    DebugEnter();
    PAGED_CODE();

    Device = WdfWdmDeviceGetWdfDeviceHandle(DeviceObject);
    GlobalData = GetGlobalData(WdfGetDriver());
    *RegFlags = WMIREG_FLAG_INSTANCE_PDO;
    *RegistryPath = &GlobalData->RegistryPath;
    *Pdo = WdfDeviceWdmGetPhysicalDevice(Device);
    Status = STATUS_SUCCESS;
    DebugExitStatus(Status);
    return Status;
}

_Use_decl_annotations_
NTSTATUS
SimBattQueryWmiDataBlock (
    PDEVICE_OBJECT DeviceObject,
    PIRP Irp,
    ULONG GuidIndex,
    ULONG InstanceIndex,
    ULONG InstanceCount,
    PULONG InstanceLengthArray,
    ULONG BufferAvail,
    PUCHAR Buffer
    )

/*++

Routine Description:

    This routine is a callback into the driver to query for the contents of
    a data block. When the driver has finished filling the data block it
    must call WmiCompleteRequest to complete the irp. The driver can
    return STATUS_PENDING if the irp cannot be completed immediately.

Arguments:

    DeviceObject - Supplies the device whose data block is being queried.

    Irp - Supplies the Irp that makes this request.

    GuidIndex - Supplies the index into the list of guids provided when the
        device registered.

    InstanceIndex - Supplies the index that denotes which instance of the data
        block is being queried.

    InstanceCount - Supplies the number of instances expected to be returned for
        the data block.

    InstanceLengthArray - Supplies a pointer to an array of ULONG that returns
        the lengths of each instance of the data block. If this is NULL then
        there was not enough space in the output buffer to fulfill the request
        so the irp should be completed with the buffer needed.

    BufferAvail - Supplies the maximum size available to write the data
        block.

    Buffer - Supplies a pointer to a buffer to return the data block.


Return Value:

    NTSTATUS

--*/

{

    PSIMBATT_FDO_DATA DevExt;
    WDFDEVICE Device;
    NTSTATUS Status;

    UNREFERENCED_PARAMETER(InstanceIndex);
    UNREFERENCED_PARAMETER(InstanceCount);

    DebugEnter();
    PAGED_CODE();

    ASSERT((InstanceIndex == 0) && (InstanceCount == 1));

    if (InstanceLengthArray == NULL) {
        Status = STATUS_BUFFER_TOO_SMALL;
        goto SimBattQueryWmiDataBlockEnd;
    }

    Device = WdfWdmDeviceGetWdfDeviceHandle(DeviceObject);
    DevExt = GetDeviceExtension(Device);

    //
    // The class driver guarantees that all outstanding IO requests will be
    // completed before it finishes unregistering. As a result, the class
    // initialization lock does not need to be acquired in this callback, since
    // it is called during class driver processing of a WMI IRP.
    //

    Status = BatteryClassQueryWmiDataBlock(DevExt->ClassHandle,
                                           DeviceObject,
                                           Irp,
                                           GuidIndex,
                                           InstanceLengthArray,
                                           BufferAvail,
                                           Buffer);

    if (Status == STATUS_WMI_GUID_NOT_FOUND) {
        Status = WmiCompleteRequest(DeviceObject,
                                    Irp,
                                    STATUS_WMI_GUID_NOT_FOUND,
                                    0,
                                    IO_NO_INCREMENT);
    }

SimBattQueryWmiDataBlockEnd:
    DebugExitStatus(Status);
    return Status;
}