summaryrefslogtreecommitdiff
path: root/pofx/WDF/Driver/MultiComp/lib/device.c
blob: aa17eaea496a4861f0132db1b769cf189b4f7746 (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
/*++

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:

    device.c

Abstract:
    This module contains routines that implement power management for the device

Environment:

    Kernel mode

--*/

#include "WdfPoFxPriv.h"
#include "device.tmh"

#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, _PfhEvtSelfManagedIoInit)
#pragma alloc_text(PAGE, _PfhEvtSelfManagedIoFlush)
#endif

VOID
UnregisterWithPowerFrameworkWorker(
    _In_ WDFDEVICE Device
    )
/*++
Routine description:
    This routine unregisters a device with the power framework. This routine 
    can be called in EvtDeviceSelfManagedioRestart, which can not be made
    pageable, therefore this function should also not be pageable. 
    
Arguments:
    Device - Handle to the KMDF device object

Return value:
    None
--*/
{
    PPOFX_DEVICE_CONTEXT devCtx;
    PPFH_CALLBACK_POHANDLE_UNAVAILABLE pfhCallbackPoHandleUnavailable;

    //
    // Get the device context
    //
    devCtx = HelperGetDeviceContext(Device);

    if (NULL == devCtx->PoHandle) {
        //
        // We didn't successfully register with the power framework, so nothing
        // to do here.
        //
        return;
    }

    //
    // Notify the driver layer that the POHANDLE is about to be invalidated
    //
    pfhCallbackPoHandleUnavailable = 
        devCtx->DeviceInitSettings.PfhCallbackPoHandleUnavailable;
    if (NULL != pfhCallbackPoHandleUnavailable) {
        pfhCallbackPoHandleUnavailable(Device, devCtx->PoHandle);
    }

    //
    // Unregister with the power framework
    //
    PoFxUnregisterDevice(devCtx->PoHandle);
    devCtx->PoHandle = NULL;

    return;
}

NTSTATUS
RegisterWithPowerFrameworkWorker(
    _In_ WDFDEVICE Device
    )
/*++
Routine description:
    This routine registers a device with the power framework.

Arguments:
    Device - Handle to the KMDF device object

Return value:
    An NTSTATUS value representing success or failure of the function.
--*/
{
    NTSTATUS status;
    PPOFX_DEVICE_CONTEXT devCtx;
    PPFH_CALLBACK_POHANDLE_AVAILABLE pfhCallbackPoHandleAvailable;
    ULONG i;
    
    //
    // Get the device context
    //
    devCtx = HelperGetDeviceContext(Device);

    if (NULL != devCtx->PoHandle) {
        //
        // Already registered with power framework, nothing to do here.
        //
        status = STATUS_SUCCESS;
        goto exit;
    }

    for (i=0; i < (devCtx->PoFxDeviceInfo->ComponentCount); i++) {
        //
        // Initially, all components are active
        //
        devCtx->ComponentInfo[i].IsActive = TRUE;

        //
        // For PDOs, we unregister when the device is disabled and re-register
        // when the device is re-enabled. Before re-registering, we need to 
        // start the component queues. They would all be in the purged state
        // when the device was disabled.
        // NOTE: It is okay to start an already-started queue, so we can handle
        // first registration and re-registration in the same way.
        //
        if (NULL != devCtx->ComponentInfo[i].Queue) {
            WdfIoQueueStart(devCtx->ComponentInfo[i].Queue);
        }            
    }        

    //
    // Register with the power framework
    //
    status = PoFxRegisterDevice(
                WdfDeviceWdmGetPhysicalDevice(Device),
                devCtx->PoFxDeviceInfo,
                &(devCtx->PoHandle)
                );
    if (FALSE == NT_SUCCESS(status)) {
        Trace(TRACE_LEVEL_ERROR, 
              "%!FUNC! - PoFxRegisterDevice failed with %!status!.", 
              status);
        goto exit;
    }

    //
    // Notify the driver layer that the POHANDLE is available
    //
    pfhCallbackPoHandleAvailable = 
        devCtx->DeviceInitSettings.PfhCallbackPoHandleAvailable;
    if (NULL != pfhCallbackPoHandleAvailable) {
        status = pfhCallbackPoHandleAvailable(Device, devCtx->PoHandle);
        if (FALSE == NT_SUCCESS(status)) {
            goto exit;
        }
    }

    //
    // Tell the power framework to start its power management
    //
    PoFxStartDevicePowerManagement(devCtx->PoHandle);

    status = STATUS_SUCCESS;
    
exit:
    if (FALSE == NT_SUCCESS(status)) {
        //
        // If an error occurred, we need to unregister with the power framework.
        // Note: the function below handles the case where we haven't registered
        // yet.
        //
        UnregisterWithPowerFrameworkWorker(Device);
    }
    return status;
}

NTSTATUS
_PfhEvtSelfManagedIoInit(
    _In_ WDFDEVICE Device
    )
/*++
Routine Description:

    In this routine, we initialize self-managed I/O operations.

Arguments:

    Device - Handle to the KMDF device object

Return Value:

    An NTSTATUS value representing success or failure of the function.

--*/
{
    NTSTATUS status;
    PPOFX_DEVICE_CONTEXT devCtx;
    PFN_WDF_DEVICE_SELF_MANAGED_IO_INIT evtDeviceSelfManagedIoInit;
    
    PAGED_CODE();

    //
    // Get the device context
    //
    devCtx = HelperGetDeviceContext(Device);

    //
    // If the driver layer supplied a self-managed-IO-init callback, invoke it.
    // We need to do this before checking if power framework settings are 
    // available so that the driver has the opportunity to specify power 
    // framework settings in the self-managed-IO-init callback.
    //
    evtDeviceSelfManagedIoInit = 
        devCtx->DeviceInitSettings.EvtDeviceSelfManagedIoInit;
    if (NULL != evtDeviceSelfManagedIoInit) {
        status = evtDeviceSelfManagedIoInit(Device);
        if (FALSE == NT_SUCCESS(status)) {
            goto exit;
        }
    }

    if (FALSE == ArePowerFrameworkSettingsAvailable(Device)) {
        status = STATUS_INVALID_DEVICE_REQUEST;
        Trace(TRACE_LEVEL_ERROR, 
              "%!FUNC! - PfhInitializePowerFrameworkSettings has not yet "
              "been called for WDFDEVICE %p. %!status!.",
              Device,
              status);
        WdfVerifierDbgBreakPoint();
        goto exit;
    }

    //
    // If S0-idle power management is supported, prevent the device from 
    // powering down to Dx due to S0-idle. We will allow the device to power 
    // down to Dx only after our device-power-not-required callback is invoked.
    //
    STOP_DEVICE_IDLE_ON_DEVICE_START(status, Device, devCtx, exit);
    
    //
    // Register with the power framework.
    //
    // The EvtDeviceSelfManagedIoInit callback is called after the first D0 
    // entry, but not after subsequent D0 entries. This means that it is not
    // called after D0 entries that occur due to S0-idle, system resuming from 
    // sleep or resource rebalance. Therefore, we register with the power 
    // framework here. We retain the registration until the device is removed.
    // 
    // Note: The function below handles the case where the driver layer already
    // registered with the power framework by calling 
    // PfhRegisterDeviceProactive.
    //
    status = RegisterWithPowerFrameworkWorker(Device);
    if (FALSE == NT_SUCCESS(status)) {
        goto exit;
    }

    status = STATUS_SUCCESS;

exit:
    return status;
}

VOID
_PfhEvtSelfManagedIoFlush(
    _In_ WDFDEVICE Device
    )
/*++
Routine Description:

    In this routine, we flush self-managed I/O operations.

Arguments:

    Device - Handle to the KMDF device object

Return Value:

    None

--*/
{
    PPOFX_DEVICE_CONTEXT devCtx;
    PFN_WDF_DEVICE_SELF_MANAGED_IO_FLUSH evtDeviceSelfManagedIoFlush;
    
    PAGED_CODE();

    if (FALSE == ArePowerFrameworkSettingsAvailable(Device)) {
        Trace(TRACE_LEVEL_ERROR, 
              "%!FUNC! - PfhInitializePowerFrameworkSettings has not yet "
              "been called for WDFDEVICE %p.",
              Device);
        WdfVerifierDbgBreakPoint();
    }
    
    //
    // Get the device context
    //
    devCtx = HelperGetDeviceContext(Device);

    if (devCtx->ShouldReportDevicePoweredOn) {
        //
        // We received an S0 IRP and we were supposed to call 
        // PoFxReportDevicePoweredOn after entering D0 in response to the S0 
        // IRP. But an error occurred that prevented us from entering D0 and 
        // KMDF has initiated a device teardown as a result of this error. 
        // Before we attempt to unregister with the power framework, we should
        // call PoFxReportDevicePoweredOn in order to bring the power framework 
        // to a consistent state.
        //
        devCtx->ShouldReportDevicePoweredOn = FALSE;
        PoFxReportDevicePoweredOn(devCtx->PoHandle);
    }

    //
    // The EvtDeviceSelfManagedIoFlush callback is called when the device is 
    // being removed. In this callback, we unregister with the power framework.
    // Note: the function below handles the case where we did not register 
    // successfully.
    //
    UnregisterWithPowerFrameworkWorker(Device);

    //
    // Perform tasks specific to S0-idle power management (if enabled)
    //
    SELF_MANAGED_IO_FLUSH_FOR_S0_IDLE(devCtx);

    //
    // If the driver layer supplied a self-managed-IO-flush callback, invoke it
    //
    evtDeviceSelfManagedIoFlush = 
        devCtx->DeviceInitSettings.EvtDeviceSelfManagedIoFlush;
    if (NULL != evtDeviceSelfManagedIoFlush) {
        evtDeviceSelfManagedIoFlush(Device);
    }

    return;
}

BOOLEAN
IsS0Irp(
    _In_ PIRP Irp,
    _In_ BOOLEAN StackLocationAdjusted
    )
/*++
Routine description:
    This routine determines whether the given IRP is an S0 IRP.

Arguments:
    Irp - Pointer to the IRP

    StackLocationAdjusted - A BOOLEAN value that indicates whether or not the
        IRP's stack location has already been adjusted for forwarding to the 
        next layer in the device stack. If TRUE, it means that the IRP stack 
        location has already been adjusted and this routine must use the next
        IRP stack location. If FALSE, it means that the IRP stack location has
        not yet been adjusted and this routine must use the previous IRP stack
        location.

Return value:
    TRUE if the IRP is an S0 IRP, FALSE otherwise
--*/
{
    PIO_STACK_LOCATION stackLoc;

    if (StackLocationAdjusted) {
        stackLoc = IoGetNextIrpStackLocation(Irp);
    } else {
        stackLoc = IoGetCurrentIrpStackLocation(Irp);
    }
 
    if ((IRP_MJ_POWER == stackLoc->MajorFunction) &&
        (IRP_MN_SET_POWER == stackLoc->MinorFunction) &&
        (SystemPowerState == stackLoc->Parameters.Power.Type) &&
        (PowerSystemWorking == stackLoc->Parameters.Power.State.SystemState)) {
        
        return TRUE;
        
    } else {
    
        return FALSE;
    }
}

NTSTATUS
PreprocessIrpAndDispatch(
    _In_ WDFDEVICE Device,
    _Inout_ PIRP Irp,
    _In_ BOOLEAN StackLocationAdjusted
    )
/*++
Routine description:
    This routine pre-processes a power IRP and dispatches it to KMDF.
    
Arguments:
    Device - Handle to the KMDF device object

    Irp - Pointer to the IRP

    StackLocationAdjusted - A BOOLEAN value that indicates whether or not the
        IRP's stack location has already been adjusted for forwarding to the 
        next layer in the device stack. If TRUE, it means that the IRP stack 
        location has already been adjusted and this routine must use the next
        IRP stack location. If FALSE, it means that the IRP stack location has
        not yet been adjusted and this routine must use the previous IRP stack
        location.

Return value:
    The NTSTATUS value that is returned from the 
    WdfDeviceWdmDispatchPreprocessedIrp call made by this routine to dispatch 
    the IRP to KMDF.
--*/
{
    NTSTATUS status;
    PPOFX_DEVICE_CONTEXT devCtx = NULL;

    if (IsS0Irp(Irp, StackLocationAdjusted)) {
        //
        // We have received an S0 IRP. The power framework requires that we call
        // PoFxReportDevicePoweredOn after we have returned to D0 as a result of
        // receiving an S0 IRP. Therefore, we make a note in our device context
        // that we need to call PoFxReportDevicePoweredOn from our next 
        // EvtDeviceD0Entry callback.
        //
        devCtx = HelperGetDeviceContext(Device);
        devCtx->ShouldReportDevicePoweredOn = TRUE;
    }
    
    //
    // Forward the IRP for KMDF to handle
    //
    if (FALSE == StackLocationAdjusted) {
        IoSkipCurrentIrpStackLocation(Irp);
    }        
    status = WdfDeviceWdmDispatchPreprocessedIrp(Device, Irp);

    return status;
}

NTSTATUS
_PfhEvtWdmPowerIrpPreprocess(
    _In_ WDFDEVICE Device,
    _Inout_ PIRP Irp
    )
/*++
Routine description:
    This routine receives power IRPs before KMDF has processed them. It pre-
    processes the power IRP before eventually forwarding it to KMDF.

Arguments:
    Device - Handle to the KMDF device object

    Irp - Pointer to the IRP

Return value:
    If the driver layer is not interested in pre-processing the IRP, the return 
    value is the NTSTATUS value that is returned from the 
    WdfDeviceWdmDispatchPreprocessedIrp call made by this routine to dispatch 
    the IRP to KMDF.

    If the driver layer is interested in pre-processing the IRP, the return 
    value is the NTSTATUS value that is returned by the driver layer's WDM pre-
    process routine for this IRP.
--*/
{
    NTSTATUS status;
    PIO_STACK_LOCATION stackLoc;
    UCHAR minorFunction;
    PPOFX_DEVICE_CONTEXT devCtx = NULL;
    BOOLEAN invokeDriverLayerCallback = FALSE;
    ULONG i;
    PFN_WDFDEVICE_WDM_IRP_PREPROCESS evtDeviceWdmPowerIrpPreprocess = NULL;

    if (FALSE == ArePowerFrameworkSettingsAvailable(Device)) {
        Trace(TRACE_LEVEL_ERROR, 
              "%!FUNC! - PfhInitializePowerFrameworkSettings has not yet "
              "been called for WDFDEVICE %p.",
              Device);
        WdfVerifierDbgBreakPoint();
    }

    //
    // Get the minor function
    //
    stackLoc = IoGetCurrentIrpStackLocation(Irp);
    minorFunction = stackLoc->MinorFunction;

    //
    // Get the device context
    //
    devCtx = HelperGetDeviceContext(Device);

    //
    // Check if the driver layer is interested in pre-processing an IRP with 
    // this minor function code.
    //
    for (i=0; 
         i < devCtx->DriverLayerPowerIrpPreprocessInfo->NumMinorFunctions;
         i++) {

         if (minorFunction == 
             devCtx->DriverLayerPowerIrpPreprocessInfo->MinorFunctions[i]) {
            //
            // The driver layer is interested in this minor function code 
            //
            invokeDriverLayerCallback = TRUE;
            break;
        }
    }

    if (invokeDriverLayerCallback) {
        //
        // Invoke the driver layer's callback. After the driver layer has 
        // pre-processed the IRP, it will call 
        // PfhWdmDispatchPreprocessedPowerIrp. We will pre-process the IRP
        // at that time.
        //        
        evtDeviceWdmPowerIrpPreprocess = 
            devCtx->DeviceInitSettings.EvtDeviceWdmPowerIrpPreprocess;
        ASSERT(NULL != evtDeviceWdmPowerIrpPreprocess);
        
        status = evtDeviceWdmPowerIrpPreprocess(Device, Irp);

    } else {
        //
        // Preprocess IRP and dispatch to KMDF
        //
        status = PreprocessIrpAndDispatch(Device, 
                                          Irp, 
                                          FALSE /* StackLocationAdjusted */);
    }
        
    return status;
}


_IRQL_requires_max_(DISPATCH_LEVEL)
NTSTATUS
PfhWdmDispatchPreprocessedPowerIrp(
    _In_ WDFDEVICE Device,
    _Inout_ PIRP Irp
    )
// See comments in WdfPoFx.h
{
    PIO_STACK_LOCATION stackLoc;

    if (FALSE == ArePowerFrameworkSettingsAvailable(Device)) {
        Trace(TRACE_LEVEL_ERROR, 
              "%!FUNC! - PfhInitializePowerFrameworkSettings has not yet "
              "been called for WDFDEVICE %p.",
              Device);
        WdfVerifierDbgBreakPoint();
    }

    //
    // Verify that this is a power IRP
    //
    stackLoc = IoGetNextIrpStackLocation(Irp);
    if (IRP_MJ_POWER != stackLoc->MajorFunction) {
        Trace(TRACE_LEVEL_ERROR, 
              "%!FUNC! - PfhWdmDispatchPreprocessedPowerIrp must be called "
              "for power IRPs only. It has been called for IRP %p, which is not"
              " a power IRP.",
              Irp);
        WdfVerifierDbgBreakPoint();
    }

    //
    // The driver layer would have already adjusted the stack location for KMDF
    //
    return PreprocessIrpAndDispatch(Device, 
                                    Irp, 
                                    TRUE /* StackLocationAdjusted */);
}

NTSTATUS
_PfhEvtD0Entry(
    _In_ WDFDEVICE Device,
    _In_ WDF_POWER_DEVICE_STATE PreviousState
    )
/*++
Routine Description:

    In this routine, we perform operations that are needed when the device 
    enters the D0 power state.

Arguments:

    Device - Handle to the KMDF device object

    PreviousState - A WDF_POWER_DEVICE_STATE-typed enumerator that identifies 
        the previous device power state.

Return Value:

    An NTSTATUS value representing success or failure of the function.

--*/
{
    NTSTATUS status;
    PPOFX_DEVICE_CONTEXT devCtx = NULL;
    PFN_WDF_DEVICE_D0_ENTRY evtDeviceD0Entry;

    if (FALSE == IsDeviceInitialized(Device)) {
        status = STATUS_INVALID_DEVICE_REQUEST;
        Trace(TRACE_LEVEL_ERROR, 
              "%!FUNC! - PfhInitializeDeviceSettings has not yet been "
              "called for WDFDEVICE %p. %!status!.",
              Device,
              status);
        WdfVerifierDbgBreakPoint();
        goto exit;
    }

    //
    // Get the device context
    //
    devCtx = HelperGetDeviceContext(Device);

    //
    // If the driver layer supplied a D0Entry callback, invoke it
    //
    evtDeviceD0Entry = devCtx->DeviceInitSettings.EvtDeviceD0Entry;
    if (NULL != evtDeviceD0Entry) {
        status = evtDeviceD0Entry(Device, PreviousState);
        if (FALSE == NT_SUCCESS(status)) {
            goto exit;
        }
    }
    
    if (devCtx->ShouldReportDevicePoweredOn) {
        //
        // This is the first time we are entering D0 after receiving an S0 IRP.
        // We should call PoFxReportDevicePoweredOn.
        //
        devCtx->ShouldReportDevicePoweredOn = FALSE;
        PoFxReportDevicePoweredOn(devCtx->PoHandle);
    }

    status = STATUS_SUCCESS;

exit:

    return status;
}

NTSTATUS
_PfhEvtSelfManagedIoRestart(
    _In_ WDFDEVICE Device
    )
/*++
Routine Description:

    In this routine, we restart self-managed I/O operations.

Arguments:

    Device - Handle to the KMDF device object

Return Value:

    An NTSTATUS value representing success or failure of the function.

--*/
{
    NTSTATUS status;
    PPOFX_DEVICE_CONTEXT devCtx = NULL;
    PFN_WDF_DEVICE_SELF_MANAGED_IO_RESTART evtDeviceSelfManagedIoRestart;

    if (FALSE == ArePowerFrameworkSettingsAvailable(Device)) {
        status = STATUS_INVALID_DEVICE_REQUEST;
        Trace(TRACE_LEVEL_ERROR, 
              "%!FUNC! - PfhInitializePowerFrameworkSettings has not yet "
              "been called for WDFDEVICE %p. %!status!.",
              Device,
              status);
        WdfVerifierDbgBreakPoint();
        goto exit;
    }
    
    //
    // Get the device context
    //
    devCtx = HelperGetDeviceContext(Device);

    //
    // If the driver layer supplied a self-managed-IO-restart callback, invoke
    // it
    //
    evtDeviceSelfManagedIoRestart = 
        devCtx->DeviceInitSettings.EvtDeviceSelfManagedIoRestart;
    if (NULL != evtDeviceSelfManagedIoRestart) {
        status = evtDeviceSelfManagedIoRestart(Device);
        if (FALSE == NT_SUCCESS(status)) {
            goto exit;
        }
    }

    //
    // For a PDO, if this is the first start after the device was disabled and 
    // if S0-idle power management is supported for the device, then we prevent
    // the device from powering down to Dx due to S0-idle. We will allow the 
    // device to power down to Dx only after our device-power-not-required 
    // callback is invoked.
    //
    // NOTE: The function below figures out whether this is the first start 
    // after the device was disabled and does the right thing based on that.
    //
    STOP_DEVICE_IDLE_ON_DEVICE_START(status, Device, devCtx, exit);
    
    //
    // For a PDO, we need to register with the power framework in the
    // self-managed-IO-restart callback if the device was disabled and then 
    // re-enabled. In this case, we would have unregistered with the power 
    // framework in the self-managed-IO-flush callback. Therefore, we need to 
    // re-register now.
    //
    // NOTE: The function below handles the case where we are already registered
    // with the power framework.
    //
    status = RegisterWithPowerFrameworkWorker(Device);
    if (FALSE == NT_SUCCESS(status)) {
        goto exit;
    }

    status = STATUS_SUCCESS;
    
exit:
    return status;
}

VOID
_PfhDevicePowerRequiredCallback(
    _In_ PVOID Context
    )
/*++
Routine Description:

    The power framework invokes this routine to notify us that we need to
    enter/remain in the D0 state

Arguments:

    Context - Context that we passed in to the power framework
      
Return Value:

    None
    
--*/
{
    WDFDEVICE device = NULL;
    PPOFX_DEVICE_CONTEXT devCtx = NULL;
    
    //
    // Get the handle to the KMDF device object
    //
    device = (WDFDEVICE) Context;

    //
    // Get the device context
    //
    devCtx = HelperGetDeviceContext(device);

    //
    // Perform actions specific to S0-idle power management (if enabled)
    //
    DEVICE_POWER_REQUIRED_FOR_S0_IDLE(device, devCtx);
    return;
}

VOID
_PfhDevicePowerNotRequiredCallback(
    _In_ PVOID Context
    )
/*++
Routine Description:

    The power framework invokes this routine to notify us that we may enter
    a low-power Dx state

Arguments:

    Context - Context that we passed in to the power framework
      
Return Value:

    None
    
--*/
{
    WDFDEVICE device = NULL;
    PPOFX_DEVICE_CONTEXT devCtx = NULL;
    
    //
    // Get the handle to the KMDF device object
    //
    device = (WDFDEVICE) Context;

    //
    // Get the device context
    //
    devCtx = HelperGetDeviceContext(device);

    //
    // Perform actions specific to S0-idle power management (if enabled)
    //
    DEVICE_POWER_NOT_REQUIRED_FOR_S0_IDLE(device, devCtx);
    
    //
    // Tell the power framework that we've finished processing device-power-
    // not-required
    //
    PoFxCompleteDevicePowerNotRequired(devCtx->PoHandle);

    return;
}

_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
PfhRegisterDeviceProactive(
    _In_ WDFDEVICE Device
    )
// See comments in WdfPoFx.h
{
    NTSTATUS status;
    PPOFX_DEVICE_CONTEXT devCtx;

    if (FALSE == ArePowerFrameworkSettingsAvailable(Device)) {
        status = STATUS_INVALID_DEVICE_REQUEST;
        Trace(TRACE_LEVEL_ERROR, 
              "%!FUNC! - PfhInitializePowerFrameworkSettings has not yet "
              "been called for WDFDEVICE %p. %!status!.",
              Device,
              status);
        WdfVerifierDbgBreakPoint();
        goto exit;
    }

    //
    // Get the device context
    //
    devCtx = HelperGetDeviceContext(Device);

    if (NULL != devCtx->PoHandle) {
        status = STATUS_INVALID_DEVICE_REQUEST;
        Trace(TRACE_LEVEL_ERROR, 
              "%!FUNC! - PfhRegisterDeviceProactive was called when the "
              "device was already registered with the power framework. "
              "%!status!.",
              status);
        WdfVerifierDbgBreakPoint();
        goto exit;
    }
    
    status = RegisterWithPowerFrameworkWorker(Device);
    if (FALSE == NT_SUCCESS(status)) {
        goto exit;
    }

    status = STATUS_SUCCESS;
    
exit:
    return status;
}