forked from coolstar/if_re-win
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.cpp
175 lines (128 loc) · 5.01 KB
/
driver.cpp
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
#include "precomp.h"
#include "trace.h"
#include "device.h"
#include "adapter.h"
#include "power.h"
#include "interrupt.h"
// {5D364AAF-5B49-41A0-9E03-D3CB2AA2E03E}
TRACELOGGING_DEFINE_PROVIDER(
RealtekTraceProvider,
"Realtek.Trace.Provider",
(0x5d364aaf, 0x5b49, 0x41a0, 0x9e, 0x3, 0xd3, 0xcb, 0x2a, 0xa2, 0xe0, 0x3e));
EXTERN_C __declspec(code_seg("INIT")) DRIVER_INITIALIZE DriverEntry;
EVT_WDF_DRIVER_UNLOAD EvtDriverUnload;
EVT_WDF_DRIVER_DEVICE_ADD EvtDriverDeviceAdd;
_Use_decl_annotations_
__declspec(code_seg("INIT"))
NTSTATUS
DriverEntry(
_In_ PDRIVER_OBJECT driverObject,
_In_ PUNICODE_STRING registryPath)
{
bool traceLoggingRegistered = false;
NTSTATUS status = STATUS_SUCCESS;
GOTO_IF_NOT_NT_SUCCESS(Exit, status,
TraceLoggingRegister(RealtekTraceProvider));
traceLoggingRegistered = true;
TraceEntry(TraceLoggingUnicodeString(registryPath));
WDF_DRIVER_CONFIG driverConfig;
WDF_DRIVER_CONFIG_INIT(&driverConfig, EvtDriverDeviceAdd);
driverConfig.DriverPoolTag = 'RTEK';
driverConfig.EvtDriverUnload = EvtDriverUnload;
GOTO_IF_NOT_NT_SUCCESS(Exit, status,
WdfDriverCreate(driverObject, registryPath, WDF_NO_OBJECT_ATTRIBUTES, &driverConfig, NULL));
Exit:
if (traceLoggingRegistered)
{
TraceExitResult(status);
if (!NT_SUCCESS(status))
{
TraceLoggingUnregister(RealtekTraceProvider);
}
}
return status;
}
_Use_decl_annotations_
NTSTATUS
#pragma prefast(suppress: __WARNING_EXCESSIVESTACKUSAGE, "TVS:12813961 call stack depth well-defined")
EvtDriverDeviceAdd(
_In_ WDFDRIVER driver,
_Inout_ PWDFDEVICE_INIT deviceInit)
{
UNREFERENCED_PARAMETER((driver));
TraceEntry();
NTSTATUS status = STATUS_SUCCESS;
NETADAPTER_INIT* adapterInit = nullptr;
GOTO_IF_NOT_NT_SUCCESS(Exit, status,
NetDeviceInitConfig(deviceInit));
WDF_OBJECT_ATTRIBUTES deviceAttributes;
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&deviceAttributes, RT_DEVICE);
{
WDF_PNPPOWER_EVENT_CALLBACKS pnpPowerCallbacks;
WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&pnpPowerCallbacks);
pnpPowerCallbacks.EvtDevicePrepareHardware = EvtDevicePrepareHardware;
pnpPowerCallbacks.EvtDeviceReleaseHardware = EvtDeviceReleaseHardware;
pnpPowerCallbacks.EvtDeviceD0Entry = EvtDeviceD0Entry;
pnpPowerCallbacks.EvtDeviceD0Exit = EvtDeviceD0Exit;
WdfDeviceInitSetPnpPowerEventCallbacks(deviceInit, &pnpPowerCallbacks);
}
/* {
WDF_POWER_POLICY_EVENT_CALLBACKS powerPolicyCallbacks;
WDF_POWER_POLICY_EVENT_CALLBACKS_INIT(&powerPolicyCallbacks);
powerPolicyCallbacks.EvtDeviceArmWakeFromSx = EvtDeviceArmWakeFromSx;
powerPolicyCallbacks.EvtDeviceDisarmWakeFromSx = EvtDeviceDisarmWakeFromSx;
WdfDeviceInitSetPowerPolicyEventCallbacks(deviceInit, &powerPolicyCallbacks);
}*/
WDFDEVICE wdfDevice;
GOTO_IF_NOT_NT_SUCCESS(Exit, status,
WdfDeviceCreate(&deviceInit, &deviceAttributes, &wdfDevice));
WdfDeviceSetAlignmentRequirement(wdfDevice, FILE_256_BYTE_ALIGNMENT);
WDF_DEVICE_POWER_POLICY_IDLE_SETTINGS idleSettings;
WDF_DEVICE_POWER_POLICY_IDLE_SETTINGS_INIT(&idleSettings, IdleCannotWakeFromS0);
idleSettings.UserControlOfIdleSettings = IdleAllowUserControl;
GOTO_IF_NOT_NT_SUCCESS(Exit, status,
WdfDeviceAssignS0IdleSettings(wdfDevice, &idleSettings));
/*WDF_DEVICE_POWER_POLICY_WAKE_SETTINGS wakeSettings;
WDF_DEVICE_POWER_POLICY_WAKE_SETTINGS_INIT(&wakeSettings);
wakeSettings.UserControlOfWakeSettings = WakeAllowUserControl;
GOTO_IF_NOT_NT_SUCCESS(Exit, status,
WdfDeviceAssignSxWakeSettings(wdfDevice, &wakeSettings));*/
adapterInit = NetAdapterInitAllocate(wdfDevice);
GOTO_WITH_INSUFFICIENT_RESOURCES_IF_NULL(Exit, status, adapterInit);
NET_ADAPTER_DATAPATH_CALLBACKS datapathCallbacks;
NET_ADAPTER_DATAPATH_CALLBACKS_INIT(
&datapathCallbacks,
EvtAdapterCreateTxQueue,
EvtAdapterCreateRxQueue);
NetAdapterInitSetDatapathCallbacks(
adapterInit,
&datapathCallbacks);
WDF_OBJECT_ATTRIBUTES adapterAttributes;
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&adapterAttributes, RT_ADAPTER);
NETADAPTER netAdapter;
GOTO_IF_NOT_NT_SUCCESS(Exit, status,
NetAdapterCreate(adapterInit, &adapterAttributes, &netAdapter));
RT_ADAPTER* adapter = RtGetAdapterContext(netAdapter);
RT_DEVICE* device = RtGetDeviceContext(wdfDevice);
device->Adapter = adapter;
GOTO_IF_NOT_NT_SUCCESS(Exit, status,
RtInitializeAdapterContext(adapter, wdfDevice, netAdapter));
GOTO_IF_NOT_NT_SUCCESS(Exit, status,
RtInterruptCreate(wdfDevice, adapter, &adapter->Interrupt));
Exit:
if (adapterInit != nullptr)
{
NetAdapterInitFree(adapterInit);
}
TraceExitResult(status);
return status;
}
_Use_decl_annotations_
VOID
EvtDriverUnload(
_In_ WDFDRIVER driver)
{
UNREFERENCED_PARAMETER((driver));
TraceEntry();
TraceLoggingUnregister(RealtekTraceProvider);
}