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
|
// Copyright (c) Microsoft Corporation. All rights reserved.
#include "precomp.h"
#include "device.h"
#include "driver.h"
#include "driver.tmh"
NTSTATUS DriverEntry(
_In_ PDRIVER_OBJECT driverObject,
_In_ PUNICODE_STRING registryPath)
/*++
Routine Description:
DriverEntry initializes the driver and is the first routine called by the
system after the driver is loaded. DriverEntry specifies the other entry
points in the function driver, such as EvtDevice and DriverUnload.
Parameters Description:
DriverObject - represents the instance of the function driver that is loaded
into memory. DriverEntry must initialize members of DriverObject before it
returns to the caller. DriverObject is allocated by the system before the
driver is loaded, and it is released by the system after the system unloads
the function driver from memory.
RegistryPath - represents the driver specific path in the Registry.
The function driver can use the path to store driver related data between
reboots. The path does not store hardware instance specific data.
Return Value:
STATUS_SUCCESS if successful,
STATUS_UNSUCCESSFUL otherwise.
--*/
{
NTSTATUS status = STATUS_SUCCESS;
WDF_DRIVER_CONFIG driverConfig{};
WDF_OBJECT_ATTRIBUTES attributes;
//
// Initialize WPP Tracing
//
WPP_INIT_TRACING(driverObject, registryPath);
// Since WPP tracing is now initialized, we can use Trace functions
TraceEntry();
//
// Register a cleanup callback so that we can call WPP_CLEANUP when
// the framework driver object is deleted during driver unload.
//
WDF_OBJECT_ATTRIBUTES_INIT(&attributes);
attributes.EvtCleanupCallback = EvtWifiDriverContextCleanup;
WDF_DRIVER_CONFIG_INIT(&driverConfig, EvtWifiDriverDeviceAdd);
driverConfig.DriverPoolTag = WIFI_DRIVER_DEFAULT_POOL_TAG;
status = WdfDriverCreate(driverObject, registryPath, &attributes, &driverConfig, WDF_NO_HANDLE);
if (!NT_SUCCESS(status))
{
WFCError("WdfDriverCreate failed %!STATUS!", status);
WPP_CLEANUP(driverObject);
return status;
}
TraceExit(status);
return status;
}
NTSTATUS EvtWifiDriverDeviceAdd(_In_ WDFDRIVER driver, _Inout_ PWDFDEVICE_INIT deviceInit)
/*++
Routine Description:
EvtWifiDriverDeviceAdd is called by the framework in response to AddDevice
call from the PnP manager. We create and initialize a device object to
represent a new instance of the device.
Arguments:
Driver - Handle to a framework driver object created in DriverEntry
DeviceInit - Pointer to a framework-allocated WDFDEVICE_INIT structure.
Return Value:
NTSTATUS
--*/
{
UNREFERENCED_PARAMETER(driver);
TraceEntry();
NTSTATUS status = STATUS_SUCCESS;
// Configure the device init for NetAdapterCx (Data Path)
status = NetDeviceInitConfig(deviceInit);
if (!NT_SUCCESS(status))
{
WFCError("NetDeviceInitConfig failed, status=0x%x", status);
goto Exit;
}
// Configure the device init for WifiCx (Control Path)
status = WifiDeviceInitConfig(deviceInit);
if (!NT_SUCCESS(status))
{
WFCError("WifiDeviceInitConfig failed, status=0x%x", status);
goto Exit;
}
// Set PnP and Power Callbacks.
// [Scope: Only PrepareHardware and ReleaseHardware are implemented in this sample.]
WDF_PNPPOWER_EVENT_CALLBACKS pnpPowerCallbacks;
WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&pnpPowerCallbacks);
pnpPowerCallbacks.EvtDevicePrepareHardware = EvtDevicePrepareHardware;
pnpPowerCallbacks.EvtDeviceReleaseHardware = EvtDeviceReleaseHardware;
WdfDeviceInitSetPnpPowerEventCallbacks(deviceInit, &pnpPowerCallbacks);
// [Scope: ArmWake and DisarmWake are not implemented in this sample.]
//WDF_POWER_POLICY_EVENT_CALLBACKS powerPolicyCallbacks;
//WdfDeviceInitSetPowerPolicyEventCallbacks(deviceInit, &powerPolicyCallbacks);
WDFDEVICE wdfIhvDevice{};
// Create the device object context to store the device specific information
WDF_OBJECT_ATTRIBUTES ihvDeviceAttributes;
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&ihvDeviceAttributes, WIFI_IHV_DEVICE_CONTEXT);
status = WdfDeviceCreate(&deviceInit, &ihvDeviceAttributes, &wdfIhvDevice);
if (!NT_SUCCESS(status))
{
WFCError("WdfDeviceCreate failed, status=0x%x", status);
goto Exit;
}
//
// Create a device interface so that applications can find and talk
// to us.
//
status = WdfDeviceCreateDeviceInterface(wdfIhvDevice, &GUID_WIFICX_SAMPLE_CLIENT_INTERFACE, nullptr);
if (!NT_SUCCESS(status))
{
WFCError("WdfDeviceCreateDeviceInterface failed with status=0x%x", status);
goto Exit;
}
// Initialize WifiCx device now that the WDFDEVICE has been created.
// In short the WifICx is the "wdf managed WDI", so the Wdi core
// concepts still applies.
WIFI_DEVICE_CONFIG wifiDeviceConfig;
WIFI_DEVICE_CONFIG_INIT(
&wifiDeviceConfig,
WDI_VERSION_LATEST, // The "WDI" version supported by this Ihv driver
EvtWifiDeviceSendCommand, // The Wdi command and task, now called "wifirequest".
EvtWifiDeviceCreateAdapter, // The Wdi "ports", now support by the netadapter instances.
EvtWifiDeviceCreateWifiDirectDevice); // [Scope: No WiFi Direct support in this sample]
// Initialize the WifiCx device with the configuration above to let OS side ready.
status = WifiDeviceInitialize(wdfIhvDevice, &wifiDeviceConfig);
if (!NT_SUCCESS(status))
{
WFCError("WifiDeviceInitialize failed, status=0x%x", status);
goto Exit;
}
// Get a pointer to the device context structure that we just associated
// with the device object. We define this structure in the device.h
// header file. WifiGetIhvDeviceContext is an inline function generated by
// using the WDF_DECLARE_CONTEXT_TYPE_WITH_NAME macro in device.h.
// This function will do the type checking and return the device context.
// If you pass a wrong object handle it will return NULL and assert if
// run under framework verifier mode.
auto deviceIhvContext = WifiGetIhvDeviceContext(wdfIhvDevice);
deviceIhvContext->WdfDevice = wdfIhvDevice;
deviceIhvContext->WdfTriageInfoPtr = WdfGetTriageInfo();
deviceIhvContext->TlvContext.AllocationContext = 0;
deviceIhvContext->TlvContext.PeerVersion = WifiDeviceGetOsWdiVersion(wdfIhvDevice);
Exit:
TraceExit(status);
return status;
}
void EvtWifiDriverContextCleanup(_In_ WDFOBJECT DriverObject)
/*++
Routine Description:
Free all the resources allocated in DriverEntry.
Arguments:
DriverObject - handle to a WDF Driver object.
Return Value:
VOID.
--*/
{
#ifndef _KERNEL_MODE
// follow https://github.com/MicrosoftDocs/windows-driver-docs/blob/staging/windows-driver-docs-pr/wdf/using-wpp-software-tracing-in-kmdf-and-umdf-2-drivers.md
// because UMDF drivers use the kernel-mode signatures of these macros for initializing and cleaning up tracing, the calls look identical for KMDF and UMDF.
UNREFERENCED_PARAMETER(DriverObject);
#endif // !_KERNEL_MODE
WPP_CLEANUP(WdfDriverWdmGetDriverObject(static_cast<WDFDRIVER>(DriverObject)));
}
|