summaryrefslogtreecommitdiff
path: root/general/toaster/umdf2/func/featured/power.c
blob: 60b5df783932b1ff92c29c9dd28160de2dee6ee4 (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
/*++

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:

    Power.C

Abstract:

    Implements callbacks to manager power transition, wait-wake and selective
    suspend.

Environment:

    Kernel mode

--*/

#include "toaster.h"

#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, ToasterEvtDeviceD0Exit)
#pragma alloc_text(PAGE, ToasterEvtDeviceArmWakeFromS0)
#pragma alloc_text(PAGE, ToasterEvtDeviceArmWakeFromSx)
#pragma alloc_text(PAGE, ToasterEvtDeviceWakeFromS0Triggered)
#pragma alloc_text(PAGE, DbgDevicePowerString)
#endif // ALLOC_PRAGMA


NTSTATUS
ToasterEvtDeviceD0Entry(
    IN WDFDEVICE                Device,
    IN WDF_POWER_DEVICE_STATE   RecentPowerState
    )
/*++
Routine Description:

    EvtDeviceD0Entry event is called to program the device to goto
    D0, which is the working state. The framework calls the driver's
    EvtDeviceD0Entry callback when the Power manager sends an
    IRP_MN_SET_POWER-DevicePower request to the driver stack. The Power manager
    sends this request when the power policy manager of this device stack
    (probaby the FDO) requests a change in D-state by calling PoRequestPowerIrp.

    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.

    RecentPowerState - WDF_POWER_DEVICE_STATE-typed enumerator that identifies the
                device power state that the device was in before this transition
                to D0.

Return Value:

    NTSTATUS    - A failure here will indicate a fatal error in the driver.
                  The Framework will attempt to tear down the stack.

--*/
{
    UNREFERENCED_PARAMETER(Device);
    UNREFERENCED_PARAMETER(RecentPowerState);

    KdPrint(("ToasterEvtDeviceD0Entry - coming from %s\n",
              DbgDevicePowerString(RecentPowerState)));

    return STATUS_SUCCESS;
}

NTSTATUS
ToasterEvtDeviceD0Exit(
    IN WDFDEVICE                Device,
    IN WDF_POWER_DEVICE_STATE   PowerState
    )
/*++
Routine Description:

    EvtDeviceD0Entry event is called to program the device to goto
    D1, D2 or D3, which are the low-power states. The framework calls the
    driver's EvtDeviceD0Exit callback when the Power manager sends an
    IRP_MN_SET_POWER-DevicePower request to the driver stack. The Power manager
    sends this request when the power policy manager of this device stack
    (probaby the FDO) requests a change in D-state by calling PoRequestPowerIrp.

Arguments:

    Device - handle to a framework device object.

    DeviceState - WDF_POWER_DEVICE_STATE-typed enumerator that identifies the
                device power state that the power policy owner (probably the
                FDO) has decided is appropriate.

Return Value:

    NTSTATUS    - A failure here will indicate a fatal error in the driver.
                  The Framework will attempt to tear down the stack.
--*/
{
    UNREFERENCED_PARAMETER(Device);
    UNREFERENCED_PARAMETER(PowerState);

    PAGED_CODE();

    KdPrint(("ToasterEvtDeviceD0Exit %s\n",
              DbgDevicePowerString(PowerState)));

    return STATUS_SUCCESS;
}

NTSTATUS
ToasterEvtDeviceArmWakeFromS0(
    IN WDFDEVICE Device
    )
/*++

Routine Description:

    EvtDeviceArmWakeFromS0 is called when the Framework arms the device for
    wake from S0.  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.

    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.

--*/
{
    UNREFERENCED_PARAMETER(Device);

    PAGED_CODE();

    KdPrint(( "--> ToasterEvtDeviceArmWakeFromS0\n"));

    KdPrint(( "<-- ToasterEvtDeviceArmWakeFromS0\n"));

    return STATUS_SUCCESS;
}

NTSTATUS
ToasterEvtDeviceArmWakeFromSx(
    IN WDFDEVICE Device
    )
/*++

Routine Description:

    EvtDeviceArmWakeFromSx is called when the Framework arms the device for
    wake from Sx.  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.

    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.

--*/
{
    UNREFERENCED_PARAMETER(Device);

    PAGED_CODE();

    KdPrint(( "--> ToasterEvtDeviceArmWakeFromSx\n"));

    KdPrint(( "<-- ToasterEvtDeviceArmWakeFromSx\n"));

    return STATUS_SUCCESS;
}

VOID
ToasterEvtDeviceDisarmWakeFromS0(
    IN WDFDEVICE Device
    )
/*++

Routine Description:

    EvtDeviceDisarmWakeFromS0 reverses anything done in EvtDeviceArmWakeFromS0.

    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.

--*/
{
    UNREFERENCED_PARAMETER(Device);

    KdPrint(( "--> ToasterEvtDeviceDisarmWakeFromS0\n"));

    KdPrint(( "<-- ToasterEvtDeviceDisarmWakeFromS0\n"));

    return ;
}

VOID
ToasterEvtDeviceDisarmWakeFromSx(
    IN WDFDEVICE Device
    )
/*++

Routine Description:

    EvtDeviceDisarmWakeFromSx reverses anything done in EvtDeviceArmWakeFromSx.

    This function will run at PASSIVE_LEVEL.

    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.

--*/
{
    UNREFERENCED_PARAMETER(Device);

    KdPrint(( "--> ToasterEvtDeviceDisarmWakeFromSx\n"));

    KdPrint(( "<-- ToasterEvtDeviceDisarmWakeFromSx\n"));

    return ;
}

VOID
ToasterEvtDeviceWakeFromS0Triggered(
    IN WDFDEVICE Device
    )
/*++

Routine Description:

    EvtDeviceWakeFromS0Triggered will be called whenever the device triggers its
    wake signal after being armed for wake.

    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();

    KdPrint(( "--> ToasterEvtDeviceWakeFromS0Triggered\n"));


    KdPrint(( "<-- ToasterEvtDeviceWakeFromS0Triggered\n"));

}

VOID
ToasterEvtDeviceWakeFromSxTriggered(
    IN WDFDEVICE Device
    )
/*++

Routine Description:

    EvtDeviceWakeFromSxTriggered will be called whenever the device triggers its
    wake signal after being armed for wake.

    This function runs at PASSIVE_LEVEL.

    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

--*/
{
    UNREFERENCED_PARAMETER(Device);

    KdPrint(( "--> ToasterEvtDeviceWakeFromSxTriggered\n"));

    KdPrint(( "<-- ToasterEvtDeviceWakeFromSxTriggered\n"));

}

PCHAR
DbgDevicePowerString(
    IN WDF_POWER_DEVICE_STATE Type
    )
/*++

New Routine Description:
    DbgDevicePowerString converts the device power state code of a power IRP to a
    text string that is helpful when tracing the execution of power IRPs.

Parameters Description:
    Type
    Type specifies the device power state code of a power IRP.

Return Value Description:
    DbgDevicePowerString returns a pointer to a string that represents the
    text description of the incoming device power state code.

--*/
{
    PAGED_CODE();

    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";
    }
}