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
|
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
Module Name:
device.c - WDFDEVICE creation and callback handling for the sample
controller device.
Abstract:
This file contains the device entry points and callbacks.
Environment:
Kernel-mode Driver Framework
--*/
#include "initguid.h"
#include "device.h"
#include "ufxdevice.h"
#include "registers.h"
#include "ufxendpoint.h"
#include "interrupt.h"
#include "defaultqueue.h"
#include "usbfnioctl.h"
#include "device.tmh"
//
// Minimum amount of time to wait before exiting D0. The purpose of waiting
// is to ensure that the current state / port are stabilized. For example,
// on IDCP, we may be connected to a suspended PC or disconnected hub, and
// in these cases we will get a suspend interrupt after ~3ms.
//
#define IDLE_TIMEOUT (100)
EVT_WDF_DEVICE_PREPARE_HARDWARE OnEvtDevicePrepareHardware;
EVT_WDF_DEVICE_RELEASE_HARDWARE OnEvtDeviceReleaseHardware;
EVT_WDF_DEVICE_D0_ENTRY OnEvtDeviceD0Entry;
EVT_WDF_DEVICE_D0_EXIT OnEvtDeviceD0Exit;
_Must_inspect_result_
_IRQL_requires_max_(PASSIVE_LEVEL)
static
NTSTATUS
DeviceInitialize(
_In_ WDFDEVICE Device
);
#ifdef ALLOC_PRAGMA
#pragma alloc_text (PAGE, UfxClientDeviceCreate)
#pragma alloc_text (PAGE, OnEvtDevicePrepareHardware)
#pragma alloc_text (PAGE, OnEvtDeviceReleaseHardware)
#endif
_Must_inspect_result_
_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
UfxClientDeviceCreate(
_In_ WDFDRIVER Driver,
_In_ PWDFDEVICE_INIT DeviceInit
)
/*++
Routine Description:
Worker routine called to create a device and its software resources.
Arguments:
Driver - WDF driver object
DeviceInit - Pointer to an opaque init structure. Memory for this
structure will be freed by the framework when the WdfDeviceCreate
succeeds. So don't access the structure after that point.
Return Value:
Appropriate NTSTATUS value
--*/
{
WDF_OBJECT_ATTRIBUTES DeviceAttributes;
WDFDEVICE WdfDevice;
NTSTATUS Status;
WDF_PNPPOWER_EVENT_CALLBACKS PnpCallbacks;
WDF_DMA_ENABLER_CONFIG DmaConfig;
PCONTROLLER_CONTEXT ControllerContext;
PAGED_CODE();
TraceEntry();
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&DeviceAttributes, CONTROLLER_CONTEXT);
//
// Do UFX-specific initialization
//
Status = UfxFdoInit(Driver, DeviceInit, &DeviceAttributes);
CHK_NT_MSG(Status, "Failed UFX initialization");
WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&PnpCallbacks);
PnpCallbacks.EvtDevicePrepareHardware = OnEvtDevicePrepareHardware;
PnpCallbacks.EvtDeviceReleaseHardware = OnEvtDeviceReleaseHardware;
PnpCallbacks.EvtDeviceD0Entry = OnEvtDeviceD0Entry;
PnpCallbacks.EvtDeviceD0Exit = OnEvtDeviceD0Exit;
WdfDeviceInitSetPnpPowerEventCallbacks(DeviceInit, &PnpCallbacks);
Status = WdfDeviceCreate(&DeviceInit, &DeviceAttributes, &WdfDevice);
CHK_NT_MSG(Status, "Failed to create wdf device");
ControllerContext = DeviceGetControllerContext(WdfDevice);
KeInitializeEvent(&ControllerContext->DetachEvent,
NotificationEvent,
FALSE);
WDF_DEVICE_POWER_POLICY_IDLE_SETTINGS_INIT(&ControllerContext->IdleSettings, IdleCanWakeFromS0);
ControllerContext->IdleSettings.IdleTimeoutType = SystemManagedIdleTimeoutWithHint;
ControllerContext->IdleSettings.IdleTimeout = IDLE_TIMEOUT;
ControllerContext->IdleSettings.DxState = PowerDeviceD3;
Status = WdfDeviceAssignS0IdleSettings(WdfDevice, &ControllerContext->IdleSettings);
LOG_NT_MSG(Status, "Failed to set S0 Idle Settings");
//
// Create and initialize device's default queue
//
Status = DefaultQueueCreate(WdfDevice);
CHK_NT_MSG(Status, "Failed to intialize default queue");
//
// Set alignment required by controller
//
WdfDeviceSetAlignmentRequirement(WdfDevice, UFX_CLIENT_ALIGNMENT);
//
// Create and Initialize DMA Enabler object for the device.
//
WDF_DMA_ENABLER_CONFIG_INIT(
&DmaConfig,
WdfDmaProfileScatterGatherDuplex,
MAX_DMA_LENGTH);
//
// Version 3 is required to perform multiple
// simultaneous transfers.
//
DmaConfig.WdmDmaVersionOverride = 3;
Status = WdfDmaEnablerCreate(
WdfDevice,
&DmaConfig,
WDF_NO_OBJECT_ATTRIBUTES,
&ControllerContext->DmaEnabler);
CHK_NT_MSG(Status, "Failed to create DMA enabler object");
//
// Create UFXDEVICE object
//
Status = UfxDevice_DeviceCreate(WdfDevice);
CHK_NT_MSG(Status, "Failed to create UFX Device object");
//
// Create DPC Lock
//
Status = WdfSpinLockCreate(WDF_NO_OBJECT_ATTRIBUTES, &ControllerContext->DpcLock);
CHK_NT_MSG(Status, "Failed to create DPC lock");
Status = WdfWaitLockCreate(WDF_NO_OBJECT_ATTRIBUTES, &ControllerContext->InitializeDefaultEndpointLock);
CHK_NT_MSG(Status, "Failed to create Ep0 init lock");
End:
TraceExit();
return Status;
}
NTSTATUS
OnEvtDevicePrepareHardware (
_In_ WDFDEVICE Device,
_In_ WDFCMRESLIST ResourcesRaw,
_In_ WDFCMRESLIST ResourcesTranslated
)
/*++
Routine Description:
EvtDevicePrepareHardware callback handler for the controller FDO.
It expects Register memory and two interrupts - Device and Wake.
Arguments:
Device - WDFDEVICE object representing the controller.
ResourcesRaw - Raw resources relative to the parent bus driver.
ResourcesTranslated - Translated resources available for the controller.
Return Value:
Appropriate NTSTATUS value
--*/
{
NTSTATUS Status;
ULONG ResCount;
ULONG ResIndex;
PCONTROLLER_CONTEXT ControllerContext;
BOOLEAN MemoryResourceMapped;
PAGED_CODE();
TraceEntry();
ControllerContext = DeviceGetControllerContext(Device);
MemoryResourceMapped = FALSE;
ResCount = WdfCmResourceListGetCount(ResourcesRaw);
Status = STATUS_SUCCESS;
for (ResIndex = 0; ResIndex < ResCount; ResIndex++) {
PCM_PARTIAL_RESOURCE_DESCRIPTOR ResourceDescriptorRaw;
PCM_PARTIAL_RESOURCE_DESCRIPTOR ResourceDescriptorTranslated;
ResourceDescriptorRaw = WdfCmResourceListGetDescriptor(
ResourcesRaw,
ResIndex);
switch (ResourceDescriptorRaw->Type) {
case CmResourceTypeMemory:
if (MemoryResourceMapped == FALSE) {
MemoryResourceMapped = TRUE;
Status = RegistersCreate(Device, ResourceDescriptorRaw);
CHK_NT_MSG(Status, "Failed to read the HW registers");
}
break;
case CmResourceTypeInterrupt:
ResourceDescriptorTranslated = WdfCmResourceListGetDescriptor(
ResourcesTranslated,
ResIndex);
Status = InterruptCreate(
Device,
ResourceDescriptorRaw,
ResourceDescriptorTranslated);
CHK_NT_MSG(Status, "Failed to create WDFINTERRUPT object");
break;
default:
break;
}
}
NT_ASSERT(ControllerContext->DeviceInterrupt != NULL);
End:
TraceExit();
return STATUS_SUCCESS;
}
NTSTATUS
OnEvtDeviceReleaseHardware (
_In_ WDFDEVICE Device,
_In_ WDFCMRESLIST ResourcesTranslated
)
/*++
Routine Description:
EvtDeviceReleaseHardware callback handler for the controller FDO.
It expects Register memory and two interrupts - Device and Wake.
Arguments:
Device - WDFDEVICE object representing the controller.
ResourcesTranslated - Translated resources available for the controller.
Return Value:
Appropriate NTSTATUS value
--*/
{
PREGISTERS_CONTEXT RegistersContext;
PCONTROLLER_CONTEXT ControllerContext;
TraceEntry();
PAGED_CODE();
UNREFERENCED_PARAMETER(ResourcesTranslated);
ControllerContext = DeviceGetControllerContext(Device);
ControllerContext->DeviceInterrupt = NULL;
ControllerContext->AttachDetachInterrupt = NULL;
RegistersContext = DeviceGetRegistersContext(Device);
if ((RegistersContext == NULL) || (RegistersContext->RegisterBase == NULL)) {
goto End;
}
MmUnmapIoSpace(RegistersContext->RegisterBase, RegistersContext->RegistersLength);
RtlZeroMemory(RegistersContext, sizeof(*RegistersContext));
End:
TraceExit();
return STATUS_SUCCESS;
}
NTSTATUS
OnEvtDeviceD0Entry (
_In_ WDFDEVICE Device,
_In_ WDF_POWER_DEVICE_STATE PreviousState
)
/*++
Routine Description:
Called by the framework after entering D0 state.
Arguments:
Device - WDFDEVICE framework handle to the bus FDO.
PreviousState - The WDF_POWER_DEVICE_STATE from which the stack is
making this transition.
Return Value:
Returns STATUS_SUCCESS or an appropriate NTSTATUS code otherwise.
--*/
{
PCONTROLLER_CONTEXT ControllerContext;
TraceEntry();
ControllerContext = DeviceGetControllerContext(Device);
if (PreviousState > WdfPowerDeviceD1) {
DevicePerformSoftReset(Device);
WdfWaitLockAcquire(ControllerContext->InitializeDefaultEndpointLock, NULL);
ControllerContext->InitializeDefaultEndpoint = TRUE;
WdfWaitLockRelease(ControllerContext->InitializeDefaultEndpointLock);
}
if (PreviousState == WdfPowerDeviceD3Final) {
//
// Notify UFX that HW is now ready
//
UfxDeviceNotifyHardwareReady(ControllerContext->UfxDevice);
}
TraceExit();
return STATUS_SUCCESS;
}
NTSTATUS
OnEvtDeviceD0Exit (
_In_ WDFDEVICE Device,
_In_ WDF_POWER_DEVICE_STATE TargetState
)
/*++
Routine Description:
Called by the framework when leaving our operational state (D0).
Arguments:
Device - WDFDEVICE framework handle to the bus FDO.
TargetState - The WDF_POWER_DEVICE_STATE to which the stack is
making its transition.
Return Value:
Returns STATUS_SUCCESS.
Any failed status code will leave the device stack in failed state and
device will not get any D0Entry callbacks after that.
--*/
{
PCONTROLLER_CONTEXT ControllerContext;
TraceEntry();
ControllerContext = DeviceGetControllerContext(Device);
WdfWaitLockAcquire(ControllerContext->InitializeDefaultEndpointLock, NULL);
ControllerContext->InitializeDefaultEndpoint = FALSE;
WdfWaitLockRelease(ControllerContext->InitializeDefaultEndpointLock);
if (TargetState != WdfPowerDeviceD3Final) {
goto End;
}
//
// If the stack is being removed while we are attached to a host, simulate a detach. At this
// point all attach/detach notification mechanisms are disabled, so no other code will report a
// detach, nor will "WasAttached" change from underneath us.
//
if (ControllerContext->WasAttached) {
KeClearEvent(&ControllerContext->DetachEvent);
UfxDeviceNotifyDetach(ControllerContext->UfxDevice);
ControllerContext->WasAttached = FALSE;
KeWaitForSingleObject(&ControllerContext->DetachEvent,
Executive,
KernelMode,
FALSE,
NULL);
}
End:
TraceExit();
return STATUS_SUCCESS;
}
_IRQL_requires_max_(DISPATCH_LEVEL)
VOID
DevicePerformSoftReset (
_In_ WDFDEVICE Device
)
/*++
Routine Description:
This functions performs soft reset of the controller and completes
any initialization that needs to be done afterwards.
Arguments:
Device - Wdf FDO device object representing the controller
--*/
{
TraceEntry();
UNREFERENCED_PARAMETER(Device);
//
// #### TODO: Insert code that performs a controller soft reset ####
//
TraceExit();
}
_IRQL_requires_max_(PASSIVE_LEVEL)
VOID
DeviceInitializeDefaultEndpoint (
_In_ WDFDEVICE Device
)
/*++
Routine Description:
This function initializes the default control endpoint
Arguments:
Device - Wdf FDO device object representing the controller
--*/
{
UFXENDPOINT Endpoint;
PUFXDEVICE_CONTEXT DeviceContext;
PCONTROLLER_CONTEXT ControllerContext;
NTSTATUS Status;
TraceEntry();
ControllerContext = DeviceGetControllerContext(Device);
DeviceContext = UfxDeviceGetContext(ControllerContext->UfxDevice);
if (DeviceContext->PhysicalEndpointToUfxEndpoint[0] != NULL) {
Endpoint = DeviceContext->PhysicalEndpointToUfxEndpoint[0];
//
// Re-initialize endpoint 0
//
TransferDestroy(Endpoint);
Status = TransferInitialize(Endpoint);
LOG_NT_MSG(Status, "Failed to initialize default endpoint");
UfxEndpointConfigureHardware(Endpoint, FALSE);
TransferStart(Endpoint);
} else {
NT_ASSERT(FALSE);
}
TraceExit();
}
_IRQL_requires_max_(DISPATCH_LEVEL)
VOID
DeviceHardwareFailure (
_In_ WDFDEVICE Device,
_In_ ULONG ExceptionCode
)
/*++
Routine Description:
The client driver may call this function if the controller has encountered a non-recoverable error, to
allow UFX to attempt to reset the controller if possible.
Arguments:
Device - Wdf FDO device object representing the controller
ExceptionCode - Exception code indicating the specific failure encountered by the controller. This
value is specific to the type of controller.
--*/
{
PCONTROLLER_CONTEXT ControllerContext;
PUFXDEVICE_CONTEXT DeviceContext;
PHARDWARE_FAILURE_CONTEXT HardwareFailureContext;
TraceEntry();
NT_ASSERT(ExceptionCode < ExceptionMaximum);
ControllerContext = DeviceGetControllerContext(Device);
DeviceContext = UfxDeviceGetContext(ControllerContext->UfxDevice);
#pragma prefast(suppress:6014, "Memory allocation is expected")
HardwareFailureContext = ExAllocatePoolWithTag(
NonPagedPoolNx,
sizeof(HARDWARE_FAILURE_CONTEXT),
UFX_CLIENT_TAG);
if (HardwareFailureContext) {
HardwareFailureContext->Size = sizeof(*HardwareFailureContext);
HardwareFailureContext->ExceptionCode = ExceptionCode;
RtlCopyMemory(
&HardwareFailureContext->ControllerContext,
ControllerContext,
sizeof(CONTROLLER_CONTEXT));
RtlCopyMemory(
&HardwareFailureContext->DeviceContext,
DeviceContext,
sizeof(UFXDEVICE_CONTEXT));
} else {
TraceError("Failed to allocate hardware failure context!");
}
UfxDeviceNotifyHardwareFailure(
ControllerContext->UfxDevice,
(PUFX_HARDWARE_FAILURE_CONTEXT) HardwareFailureContext);
TraceExit();
}
|