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
|
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
Module Name:
Fdo.cpp
Abstract:
FDO callbacks, functions, and types.
Environment:
Kernel-mode only.
--*/
#include "Pch.h"
#include "Fdo.tmh"
EXTERN_C_START
EVT_WDF_DEVICE_PREPARE_HARDWARE Fdo_EvtDevicePrepareHardware;
EVT_WDF_DEVICE_RELEASE_HARDWARE Fdo_EvtDeviceReleaseHardware;
EVT_WDF_DEVICE_D0_ENTRY Fdo_EvtDeviceD0Entry;
EVT_WDF_DEVICE_D0_EXIT Fdo_EvtDeviceD0Exit;
EVT_WDF_DEVICE_SELF_MANAGED_IO_INIT Fdo_EvtDeviceSelfManagedIoInit;
EVT_WDF_DEVICE_SELF_MANAGED_IO_RESTART Fdo_EvtDeviceSelfManagedIoRestart;
EVT_WDF_WORKITEM Fdo_ConnectorAndNotificationWorkItem;
_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
Fdo_Initialize (
_In_ PFDO_CONTEXT FdoCtx
);
EXTERN_C_END
#pragma alloc_text(PAGE, Fdo_Create)
#pragma alloc_text(PAGE, Fdo_Initialize)
#pragma alloc_text(PAGE, Fdo_EvtDevicePrepareHardware)
#pragma alloc_text(PAGE, Fdo_EvtDeviceReleaseHardware)
#pragma alloc_text(PAGE, Fdo_EvtDeviceD0Entry)
#pragma alloc_text(PAGE, Fdo_EvtDeviceD0Exit)
#pragma alloc_text(PAGE, Fdo_EvtDeviceSelfManagedIoInit)
#pragma alloc_text(PAGE, Fdo_EvtDeviceSelfManagedIoRestart)
_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
Fdo_Create (
_Inout_ PWDFDEVICE_INIT DeviceInit
)
{
WDF_OBJECT_ATTRIBUTES attributes;
WDF_PNPPOWER_EVENT_CALLBACKS pnpPowerCallbacks;
PFDO_CONTEXT fdoCtx;
WDFDEVICE wdfDevice;
NTSTATUS status;
PAGED_CODE();
TRACE_FUNC_ENTRY(TRACE_FLAG_FDO);
WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&pnpPowerCallbacks);
pnpPowerCallbacks.EvtDevicePrepareHardware = Fdo_EvtDevicePrepareHardware;
pnpPowerCallbacks.EvtDeviceReleaseHardware = Fdo_EvtDeviceReleaseHardware;
pnpPowerCallbacks.EvtDeviceD0Entry = Fdo_EvtDeviceD0Entry;
pnpPowerCallbacks.EvtDeviceD0Exit = Fdo_EvtDeviceD0Exit;
pnpPowerCallbacks.EvtDeviceSelfManagedIoInit = Fdo_EvtDeviceSelfManagedIoInit;
pnpPowerCallbacks.EvtDeviceSelfManagedIoRestart = Fdo_EvtDeviceSelfManagedIoRestart;
WdfDeviceInitSetPnpPowerEventCallbacks(DeviceInit, &pnpPowerCallbacks);
WdfDeviceInitSetPowerPageable(DeviceInit);
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, FDO_CONTEXT);
status = WdfDeviceCreate(&DeviceInit, &attributes, &wdfDevice);
if (!NT_SUCCESS(status))
{
TRACE_ERROR(TRACE_FLAG_FDO, "[DeviceInit: 0x%p] WdfDeviceCreate failed - %!STATUS!", DeviceInit, status);
goto Exit;
}
fdoCtx = Fdo_GetContext(wdfDevice);
fdoCtx->WdfDevice = wdfDevice;
status = Fdo_Initialize(fdoCtx);
if (!NT_SUCCESS(status))
{
goto Exit;
}
status = Ppm_Initialize(&fdoCtx->PpmCtx);
if (!NT_SUCCESS(status))
{
goto Exit;
}
TRACE_INFO(TRACE_FLAG_FDO, "[Device: 0x%p] Device created", wdfDevice);
Exit:
TRACE_FUNC_EXIT(TRACE_FLAG_FDO);
return status;
}
_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
Fdo_Initialize (
_In_ PFDO_CONTEXT FdoCtx
)
{
NTSTATUS status;
WDFDEVICE device;
WDF_DEVICE_STATE deviceState;
WDF_WORKITEM_CONFIG workItemConfig;
WDF_OBJECT_ATTRIBUTES attributes;
PAGED_CODE();
TRACE_FUNC_ENTRY(TRACE_FLAG_FDO);
device = FdoCtx->WdfDevice;
//
// ACPI-enumerated devices are not disableable by default. Override that,
// since there is no need for that restriction.
//
WDF_DEVICE_STATE_INIT(&deviceState);
deviceState.NotDisableable = WdfFalse;
WdfDeviceSetDeviceState(device, &deviceState);
//
// Create a workitem that will create connectors and enable notifications
// so that we don't block D0 Entry and thereby boot sequence of the system.
//
WDF_WORKITEM_CONFIG_INIT(&workItemConfig, Fdo_ConnectorAndNotificationWorkItem);
WDF_OBJECT_ATTRIBUTES_INIT(&attributes);
attributes.ParentObject = device;
status = WdfWorkItemCreate(&workItemConfig, &attributes, &FdoCtx->ConnectorAndNotificationWorkItem);
if (!NT_SUCCESS(status))
{
TRACE_ERROR(TRACE_FLAG_FDO, "[Device: 0x%p] WdfWorkItemCreate for ConnectorAndNotificationWorkItem failed - %!STATUS!", device, status);
goto Exit;
}
TRACE_INFO(TRACE_FLAG_FDO, "[Device: 0x%p] FDO initialized", device);
Exit:
TRACE_FUNC_EXIT(TRACE_FLAG_FDO);
return STATUS_SUCCESS;
}
NTSTATUS
Fdo_EvtDevicePrepareHardware (
_In_ WDFDEVICE Device,
_In_ WDFCMRESLIST ResourcesRaw,
_In_ WDFCMRESLIST ResourcesTranslated
)
{
NTSTATUS status;
PFDO_CONTEXT fdoCtx;
ULONG index;
ULONG resourceCount;
BOOLEAN allResourcesFound;
PCM_PARTIAL_RESOURCE_DESCRIPTOR res;
PCM_PARTIAL_RESOURCE_DESCRIPTOR rawRes;
PAGED_CODE();
TRACE_FUNC_ENTRY(TRACE_FLAG_FDO);
fdoCtx = Fdo_GetContext(Device);
resourceCount = WdfCmResourceListGetCount(ResourcesTranslated);
allResourcesFound = FALSE;
for (index = 0; !allResourcesFound && (index < resourceCount); ++index)
{
res = WdfCmResourceListGetDescriptor(ResourcesTranslated, index);
if (res->Type != CmResourceTypeMemory)
{
continue;
}
rawRes = WdfCmResourceListGetDescriptor(ResourcesRaw, index);
//
// Verify if address is below 4GB and not straddling the 4GB boundary.
//
if ((rawRes->u.Memory.Start.HighPart != 0) ||
((rawRes->u.Memory.Start.LowPart + rawRes->u.Memory.Length) <
rawRes->u.Memory.Start.LowPart))
{
status = STATUS_INSUFFICIENT_RESOURCES;
TRACE_ERROR(TRACE_FLAG_FDO, "[Device: 0x%p] Memory resource address (%I64x) not below 4GB", Device, rawRes->u.Memory.Start.QuadPart);
goto Exit;
}
status = Ppm_PrepareHardware(&fdoCtx->PpmCtx, res->u.Memory.Start, res->u.Memory.Length);
if (!NT_SUCCESS(status))
{
goto Exit;
}
allResourcesFound = TRUE;
}
if (allResourcesFound == FALSE)
{
status = STATUS_INSUFFICIENT_RESOURCES;
TRACE_ERROR(TRACE_FLAG_FDO, "[Device: 0x%p] Could not find required resources", Device);
goto Exit;
}
status = Acpi_PrepareHardware(&fdoCtx->AcpiCtx);
if (!NT_SUCCESS(status))
{
goto Exit;
}
TRACE_INFO(TRACE_FLAG_FDO, "[Device: 0x%p] Prepare hardware completed", Device);
Exit:
TRACE_FUNC_EXIT(TRACE_FLAG_FDO);
return status;
}
NTSTATUS
Fdo_EvtDeviceReleaseHardware (
_In_ WDFDEVICE Device,
_In_ WDFCMRESLIST ResourcesTranslated
)
{
PFDO_CONTEXT fdoCtx;
UNREFERENCED_PARAMETER(ResourcesTranslated);
PAGED_CODE();
TRACE_FUNC_ENTRY(TRACE_FLAG_FDO);
fdoCtx = Fdo_GetContext(Device);
Acpi_ReleaseHardware(&fdoCtx->AcpiCtx);
Ppm_ReleaseHardware(&fdoCtx->PpmCtx);
TRACE_INFO(TRACE_FLAG_FDO, "[Device: 0x%p] Release hardware completed", Device);
TRACE_FUNC_EXIT(TRACE_FLAG_FDO);
return STATUS_SUCCESS;
}
NTSTATUS
Fdo_EvtDeviceD0Entry (
_In_ WDFDEVICE Device,
_In_ WDF_POWER_DEVICE_STATE PreviousState
)
{
NTSTATUS status;
PFDO_CONTEXT fdoCtx;
PAGED_CODE();
TRACE_FUNC_ENTRY(TRACE_FLAG_FDO);
fdoCtx = Fdo_GetContext(Device);
TRACE_INFO(TRACE_FLAG_FDO, "[Device: 0x%p] Entering D0 from %!WDF_POWER_DEVICE_STATE!", Device, PreviousState);
status = Ppm_PowerOn(&fdoCtx->PpmCtx);
if (!NT_SUCCESS(status))
{
goto Exit;
}
Exit:
TRACE_FUNC_EXIT(TRACE_FLAG_FDO);
return status;
}
NTSTATUS
Fdo_EvtDeviceD0Exit (
_In_ WDFDEVICE Device,
_In_ WDF_POWER_DEVICE_STATE TargetState
)
{
PFDO_CONTEXT fdoCtx;
PAGED_CODE();
TRACE_FUNC_ENTRY(TRACE_FLAG_FDO);
fdoCtx = Fdo_GetContext(Device);
TRACE_INFO(TRACE_FLAG_FDO, "[Device: 0x%p] Exiting D0 to %!WDF_POWER_DEVICE_STATE!", Device, TargetState);
Ppm_PowerOff(&fdoCtx->PpmCtx);
TRACE_FUNC_EXIT(TRACE_FLAG_FDO);
return STATUS_SUCCESS;
}
NTSTATUS
Fdo_EvtDeviceSelfManagedIoInit (
_In_ WDFDEVICE Device
)
{
PFDO_CONTEXT fdoCtx;
PAGED_CODE();
TRACE_FUNC_ENTRY(TRACE_FLAG_FDO);
fdoCtx = Fdo_GetContext(Device);
WdfWorkItemEnqueue(fdoCtx->ConnectorAndNotificationWorkItem);
TRACE_FUNC_EXIT(TRACE_FLAG_FDO);
return STATUS_SUCCESS;
}
NTSTATUS
Fdo_EvtDeviceSelfManagedIoRestart (
_In_ WDFDEVICE Device
)
{
NTSTATUS status;
PPPM_CONTEXT ppmCtx;
PAGED_CODE();
TRACE_FUNC_ENTRY(TRACE_FLAG_FDO);
ppmCtx = &Fdo_GetContext(Device)->PpmCtx;
status = Ppm_EnableNotifications(ppmCtx);
if (!NT_SUCCESS(status))
{
goto Exit;
}
Exit:
TRACE_FUNC_EXIT(TRACE_FLAG_FDO);
return status;
}
VOID
Fdo_ConnectorAndNotificationWorkItem(
_In_ WDFWORKITEM WorkItem
)
{
NTSTATUS status;
PPPM_CONTEXT ppmCtx;
PFDO_CONTEXT fdoCtx;
WDFDEVICE device;
PAGED_CODE();
TRACE_FUNC_ENTRY(TRACE_FLAG_FDO);
device = (WDFDEVICE)WdfWorkItemGetParentObject(WorkItem);
fdoCtx = Fdo_GetContext(device);
ppmCtx = &fdoCtx->PpmCtx;
status = Ucm_CreateConnectors(ppmCtx);
if (!NT_SUCCESS(status))
{
goto Exit;
}
//
// Connector objects are ready. Now we can enable all notifications.
//
status = Ppm_EnableNotifications(ppmCtx);
if (!NT_SUCCESS(status))
{
goto Exit;
}
Exit:
//
// Failing to create the connectors or enable notifications are both
// unrecoverable failures. Attempt to have WDF reload the driver.
//
if (!NT_SUCCESS(status))
{
TRACE_ERROR(TRACE_FLAG_FDO, "[Device: 0x%p] Failed to initialize PPM connectors - attempting to restart device.", device);
WdfDeviceSetFailed(fdoCtx->WdfDevice, WdfDeviceFailedAttemptRestart);
}
TRACE_INFO(TRACE_FLAG_FDO, "[Device: 0x%p] Work item complete.", device);
TRACE_FUNC_EXIT(TRACE_FLAG_FDO);
}
|