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
|
/*++
Copyright (c) Microsoft Corporation
Module Name:
power.c
Abstract:
This module contains the code that handles the power IRPs for the serial
driver.
Environment:
Kernel mode
--*/
#include "precomp.h"
#if defined(EVENT_TRACING)
#include "power.tmh"
#endif
PCHAR
DbgDevicePowerString(
IN WDF_POWER_DEVICE_STATE Type
);
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGESER,SerialEvtDeviceD0Exit)
#pragma alloc_text(PAGESER,SerialSaveDeviceState)
#endif // ALLOC_PRAGMA
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";
}
}
NTSTATUS
SerialEvtDeviceD0Entry(
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
--*/
{
PSERIAL_DEVICE_EXTENSION deviceExtension;
PSERIAL_DEVICE_STATE pDevState;
SHORT divisor;
SERIAL_IOCTL_SYNC S;
SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_POWER,
"-->SerialEvtDeviceD0Entry - coming from %s\n", DbgDevicePowerString(PreviousState));
deviceExtension = SerialGetDeviceExtension (Device);
pDevState = &deviceExtension->DeviceState;
//
// Restore the state of the UART. First, that involves disabling
// interrupts both via OUT2 and IER.
//
WRITE_MODEM_CONTROL(deviceExtension, deviceExtension->Controller, 0);
DISABLE_ALL_INTERRUPTS(deviceExtension, deviceExtension->Controller);
//
// Set the baud rate
//
SerialGetDivisorFromBaud(deviceExtension->ClockRate, deviceExtension->CurrentBaud, &divisor);
S.Extension = deviceExtension;
S.Data = (PVOID) (ULONG_PTR) divisor;
#pragma prefast(suppress: __WARNING_INFERRED_IRQ_TOO_LOW, "PFD warning that we are calling interrupt synchronize routine directly. Suppress it because interrupt is disabled above.")
SerialSetBaud(deviceExtension->WdfInterrupt, &S);
//
// Reset / Re-enable the FIFO's
//
if (deviceExtension->FifoPresent) {
WRITE_FIFO_CONTROL(deviceExtension, deviceExtension->Controller, (UCHAR)0);
READ_RECEIVE_BUFFER(deviceExtension, deviceExtension->Controller);
WRITE_FIFO_CONTROL(deviceExtension, deviceExtension->Controller,
(UCHAR)(SERIAL_FCR_ENABLE | deviceExtension->RxFifoTrigger
| SERIAL_FCR_RCVR_RESET
| SERIAL_FCR_TXMT_RESET));
} else {
WRITE_FIFO_CONTROL(deviceExtension, deviceExtension->Controller, (UCHAR)0);
}
//
// Restore a couple more registers
//
WRITE_INTERRUPT_ENABLE(deviceExtension, deviceExtension->Controller, pDevState->IER);
WRITE_LINE_CONTROL(deviceExtension, deviceExtension->Controller, pDevState->LCR);
//
// Clear out any stale interrupts
//
READ_INTERRUPT_ID_REG(deviceExtension, deviceExtension->Controller);
READ_LINE_STATUS(deviceExtension, deviceExtension->Controller);
READ_MODEM_STATUS(deviceExtension, deviceExtension->Controller);
//
// TODO: move this code to EvtInterruptEnable.
//
if (deviceExtension->DeviceState.Reopen == TRUE) {
SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_POWER, "Reopening device\n");
SetDeviceIsOpened(deviceExtension, TRUE, FALSE);
//
// This enables interrupts on the device!
//
WRITE_MODEM_CONTROL(deviceExtension, deviceExtension->Controller,
(UCHAR)(pDevState->MCR | SERIAL_MCR_OUT2));
//
// Refire the state machine
//
DISABLE_ALL_INTERRUPTS(deviceExtension, deviceExtension->Controller);
ENABLE_ALL_INTERRUPTS(deviceExtension, deviceExtension->Controller);
}
SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_POWER, "<--SerialEvtDeviceD0Entry\n");
return STATUS_SUCCESS;
}
NTSTATUS
SerialEvtDeviceD0Exit(
IN WDFDEVICE Device,
IN WDF_POWER_DEVICE_STATE TargetState
)
/*++
Routine Description:
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:
NTSTATUS
--*/
{
PSERIAL_DEVICE_EXTENSION deviceExtension;
SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_POWER,
"-->SerialEvtDeviceD0Exit - moving to %s\n", DbgDevicePowerString(TargetState));
PAGED_CODE();
deviceExtension = SerialGetDeviceExtension (Device);
if (deviceExtension->DeviceIsOpened == TRUE) {
LARGE_INTEGER charTime;
SetDeviceIsOpened(deviceExtension, FALSE, TRUE);
charTime.QuadPart = -SerialGetCharTime(deviceExtension).QuadPart;
//
// Shut down the chip
//
SerialDisableUART(deviceExtension);
//
// Drain the device
//
SerialDrainUART(deviceExtension, &charTime);
//
// Save the device state
//
SerialSaveDeviceState(deviceExtension);
}
else
{
SetDeviceIsOpened(deviceExtension, FALSE, FALSE);
}
SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_POWER, "<--SerialEvtDeviceD0Exit\n");
return STATUS_SUCCESS;
}
VOID
SerialSaveDeviceState(IN PSERIAL_DEVICE_EXTENSION PDevExt)
/*++
Routine Description:
This routine saves the device state of the UART
Arguments:
PDevExt - Pointer to the device extension for the devobj to save the state
for.
Return Value:
VOID
--*/
{
PSERIAL_DEVICE_STATE pDevState = &PDevExt->DeviceState;
PAGED_CODE();
SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_POWER, "Entering SerialSaveDeviceState\n");
//
// Read necessary registers direct
//
pDevState->IER = READ_INTERRUPT_ENABLE(PDevExt, PDevExt->Controller);
pDevState->MCR = READ_MODEM_CONTROL(PDevExt, PDevExt->Controller);
pDevState->LCR = READ_LINE_CONTROL(PDevExt, PDevExt->Controller);
SerialDbgPrintEx(TRACE_LEVEL_INFORMATION, DBG_POWER, "Leaving SerialSaveDeviceState\n");
}
VOID
SetDeviceIsOpened(IN PSERIAL_DEVICE_EXTENSION PDevExt, IN BOOLEAN DeviceIsOpened, IN BOOLEAN Reopen)
{
PDevExt->DeviceIsOpened = DeviceIsOpened;
PDevExt->DeviceState.Reopen = Reopen;
}
|