ReactOS 0.4.15-dev-8207-gd73a838
fxdriverum.cpp
Go to the documentation of this file.
1/*++
2
3Copyright (c) Microsoft Corporation
4
5Module Name:
6
7 FxDriverUm.cpp
8
9Abstract:
10
11 This is the main driver framework.
12
13Author:
14
15
16
17Environment:
18
19 User mode only
20
21Revision History:
22
23
24
25--*/
26
27#include "coreprivshared.hpp"
28#include "fxiotarget.hpp"
29#include "fxldrum.h"
30
31// Tracing support
32extern "C" {
33#include "FxDriverUm.tmh"
34}
35
36
42 _In_ IWudfDeviceStack * DevStack,
43 _In_ LPCWSTR KernelDeviceName,
44 _In_opt_ HKEY PdoKey,
46 _In_ LPCWSTR DevInstanceID,
47 _In_ ULONG DriverID
48 )
49{
51
52 //
53 // Context parameter is CWudfDriverGlobals in legacy UMDF. Not used in
54 // UMDF 2.0
55 //
57
59
60
61
62 if (pDriver != NULL) {
63 return pDriver->AddDevice(DevStack,
64 KernelDeviceName,
65 PdoKey,
67 DevInstanceID,
68 DriverID
69 );
70 }
71
73}
74
78 _In_ IWudfDeviceStack * DevStack,
79 _In_ LPCWSTR KernelDeviceName,
80 _In_opt_ HKEY PdoKey,
82 _In_ LPCWSTR DevInstanceID,
83 _In_ ULONG DriverID
84 )
85{
86 WDFDEVICE_INIT init(this);
89 HRESULT hr = S_OK;
90 LONG lRetVal = -1;
91
93 "Enter AddDevice DevStack %p", DevStack);
94
95 //FX_VERIFY(INTERNAL, CHECK_NOT_NULL(DevStack));
96 //FX_VERIFY(INTERNAL, CHECK_NOT_NULL(KernelDeviceName));
97 //FX_VERIFY(INTERNAL, CHECK_HANDLE(PdoKey));
98 //FX_VERIFY(INTERNAL, CHECK_NOT_NULL(ServiceName));
99 //FX_VERIFY(INTERNAL, CHECK_NOT_NULL(DevInstanceID));
100
101 pDevice = NULL;
102 init.CreatedOnStack = TRUE;
103 init.InitType = FxDeviceInitTypeFdo;
104 init.Fdo.PhysicalDevice = NULL;
105
106 //
107 // Capture the input parameters
108 //
109 init.DevStack = DevStack;
110 init.DriverID = DriverID;
111
112 lRetVal = RegOpenKeyEx(
113 PdoKey,
114 NULL,
115 0,
116 KEY_READ,
117 &init.PdoKey
118 );
119
120 if (ERROR_SUCCESS != lRetVal) {
122 "Registry key open failed for the PDO key, "
123 "winerror %!WINERROR!", lRetVal);
124
125 hr = HRESULT_FROM_WIN32(lRetVal);
126 return FxDevice::NtStatusFromHr(DevStack, hr);
127 }
128
129 size_t len = 0;
131 if (FAILED(hr)) {
133 "Registry path string too long or badly formed "
134 "path. Invalid configuration HRESULT %!hresult!",
135 hr);
136 return FxDevice::NtStatusFromHr(DevStack, hr);
137 }
138
139 len += 1; // Add one for the string termination character
140 init.ConfigRegistryPath = new WCHAR[len];
141 if (NULL == init.ConfigRegistryPath) {
144 "Failed to allocate memory for Config path"
145 " HRESULT %!hresult!", hr);
146
147 return FxDevice::NtStatusFromHr(DevStack, hr);
148 }
149
150 hr = StringCchCopyW(init.ConfigRegistryPath, len, ServiceName);
151 if (FAILED(hr)) {
153 "Failed to copy the configuration path status "
154 "%!hresult!", hr);
155 return FxDevice::NtStatusFromHr(DevStack, hr);
156 }
157
158 //
159 // Capture the PDO device instance ID.
160 //
161 len = 0;
162 hr = StringCchLengthW(DevInstanceID, STRSAFE_MAX_CCH, &len);
163 if (FAILED(hr)) {
165 "Device Instance ID string too long or badly formed"
166 " path. Invalid configuration %!hresult!", hr);
167 return FxDevice::NtStatusFromHr(DevStack, hr);
168 }
169
170 len += 1; // Add one for the string termination character
171 init.DevInstanceID = new WCHAR[len];
172 if (NULL == init.DevInstanceID) {
174 "Failed to allocate memory for DevInstanceID "
175 "%!hresult!", hr);
177 return FxDevice::NtStatusFromHr(DevStack, hr);
178 }
179
180 hr = StringCchCopyW(init.DevInstanceID, len, DevInstanceID);
181 if (FAILED(hr)) {
183 "Unable to copy DevInstanceID %!hresult!", hr);
184 return FxDevice::NtStatusFromHr(DevStack, hr);
185 }
186
187 //
188 // Capture Kernel device name.
189 //
190 len = 0;
191 hr = StringCchLengthW(KernelDeviceName, STRSAFE_MAX_CCH, &len);
192 if (FAILED(hr)) {
194 "Unable to determine KernelDeviceName length"
195 "%!hresult!", hr);
196 return FxDevice::NtStatusFromHr(DevStack, hr);
197 }
198
199 len += 1; // Add one for string termination character.
200 init.KernelDeviceName = new WCHAR[len];
201 if (init.KernelDeviceName == NULL) {
203 "Failed to allocate memory for KernelDeviceName "
204 "%!hresult!", hr);
206 return FxDevice::NtStatusFromHr(DevStack, hr);
207 }
208
209 hr = StringCchCopyW(init.KernelDeviceName, len, KernelDeviceName);
210 if (FAILED(hr)) {
212 "Unable to copy kernel device name KernelDeviceName"
213 " %!hresult!", hr);
214 return FxDevice::NtStatusFromHr(DevStack, hr);
215 }
216
217 //
218 // Invoke driver's AddDevice callback
219 //
221
222 //
223 // Caller returned w/out creating a device, we are done. Returning
224 // STATUS_SUCCESS w/out creating a device and attaching to the stack is OK,
225 // especially for filter drivers which selectively attach to devices.
226 //
227 if (init.CreatedDevice == NULL) {
229 "Driver did not create a device in "
230 "EvtDriverAddDevice, status %!STATUS!", status);
231
232 //
233 // We do not let filters affect the building of the rest of the stack.
234 // If they return error, we convert it to STATUS_SUCCESS.
235 //
236 if (init.Fdo.Filter && !NT_SUCCESS(status)) {
239 "Filter returned %!STATUS! without creating a WDFDEVICE, "
240 "converting to STATUS_SUCCESS", status);
242 }
243
244 return status;
245 }
246
247 pDevice = init.CreatedDevice;
248
249 if (NT_SUCCESS(status)) {
250 //
251 // Make sure that DO_DEVICE_INITIALIZING is cleared.
252 // FxDevice::FdoInitialize does not do this b/c the driver writer may
253 // want the bit set until sometime after WdfDeviceCreate returns
254 //
256 }
257 else {
258 //
259 // Created a device, but returned error.
260 //
261 ASSERT(pDevice->IsPnp());
263
265 pDevice = NULL;
266 }
267
269 "Exit, status %!STATUS!", status);
270
271 return status;
272}
273
277 VOID
278 )
279{
280 //
281 // No allocation needed for user-mode, just store FxDriver in driver object.
282 //
283 m_DriverObject.GetObject()->FxDriver = this;
284
285 return STATUS_SUCCESS;
286}
287
291 )
292{
293 return DriverObject->FxDriver;
294}
295
296VOID
297FxDriver::ClearDriverObjectFxDriver(
298 VOID
299 )
300{
302
303 if (pDriverObj != NULL) {
304 pDriverObj->FxDriver = NULL;
305 }
306}
307
309FxDriver::OpenParametersKey(
310 VOID
311 )
312{
313 HRESULT hr;
315
316 PFX_DRIVER_GLOBALS FxDriverGlobals = GetDriverGlobals();
318 IWudfDeviceStack* pDevStack = (IWudfDeviceStack*)pDrvObj->WudfDevStack;
319
320 UMINT::WDF_PROPERTY_STORE_ROOT rootSpecifier;
321 UMINT::WDF_PROPERTY_STORE_RETRIEVE_FLAGS flags;
322 CANSI_STRING serviceNameA;
324 HKEY hKey;
325
326 RtlInitAnsiString(&serviceNameA, FxDriverGlobals->Public.DriverName);
327 status = RtlAnsiStringToUnicodeString(&serviceNameW,
328 &serviceNameA,
329 FALSE);
330 if (NT_SUCCESS(status)) {
331 rootSpecifier.LengthCb = sizeof(UMINT::WDF_PROPERTY_STORE_ROOT);
332 rootSpecifier.RootClass = UMINT::WdfPropertyStoreRootDriverParametersKey;
333 rootSpecifier.Qualifier.ParametersKey.ServiceName = serviceNameW.Buffer;
334
335 flags = UMINT::WdfPropertyStoreCreateIfMissing;
336
337 hr = pDevStack->CreateRegistryEntry(&rootSpecifier,
338 flags,
340 NULL,
341 &hKey,
342 NULL);
344 if (NT_SUCCESS(status)) {
345 m_DriverParametersKey = hKey;
346 }
347 }
348
349 return status;
350}
351
352
LONG NTSTATUS
Definition: precomp.h:26
static WCHAR ServiceName[]
Definition: browser.c:19
__inline BOOLEAN IsPnp(VOID)
Definition: fxdevice.hpp:1200
WDF_DEVICE_PNP_STATE m_CurrentPnpState
Definition: fxdevice.hpp:507
static NTSTATUS NtStatusFromHr(_In_ IWudfDeviceStack *DevStack, _In_ HRESULT Hr)
Definition: fxdeviceum.cpp:826
VOID FinishInitializing(VOID)
Definition: fxdevice.cpp:1109
_Must_inspect_result_ NTSTATUS DeleteDeviceFromFailedCreate(__in NTSTATUS FailedStatus, __in BOOLEAN UseStateMachine)
Definition: fxdevice.cpp:644
_Must_inspect_result_ NTSTATUS Invoke(__in WDFDRIVER Driver, __in PWDFDEVICE_INIT DeviceInit)
_Must_inspect_result_ NTSTATUS AllocateDriverObjectExtensionAndStoreFxDriver(VOID)
Definition: fxdriverkm.cpp:130
static MdDriverAddDeviceType AddDevice
Definition: fxdriver.hpp:125
FxDriver(__in MdDriverObject DriverObject, __in PWDF_DRIVER_CONFIG DriverConfig, __in PFX_DRIVER_GLOBALS FxDriverGlobals)
Definition: fxdriver.cpp:33
FxDriverDeviceAdd m_DriverDeviceAdd
Definition: fxdriver.hpp:70
static FxDriver * GetFxDriver(__in MdDriverObject DriverObject)
Definition: fxdriverkm.cpp:159
MxDriverObject m_DriverObject
Definition: fxdriver.hpp:62
__inline PFX_DRIVER_GLOBALS GetDriverGlobals(VOID)
Definition: fxobject.hpp:734
__inline MdDriverObject GetObject(VOID)
#define __in
Definition: dbghelp.h:35
#define TRACINGPNP
Definition: dbgtrace.h:67
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
FxDevice * pDevice
DoTraceLevelMessage(pFxDriverGlobals, TRACE_LEVEL_VERBOSE, TRACINGPNP, "Enter, WDFDEVICE %p", Device)
IWudfDeviceStack * pDevStack
@ FxDeviceInitTypeFdo
FxDriver * pDriver
FxAutoRegKey hKey
return pDriver GetDriverObject()
GLbitfield flags
Definition: glext.h:7161
GLenum GLsizei len
Definition: glext.h:6722
#define S_OK
Definition: intsafe.h:52
#define FAILED(hr)
Definition: intsafe.h:51
#define ASSERT(a)
Definition: mode.c:44
#define _Must_inspect_result_
Definition: ms_sal.h:558
#define _In_
Definition: ms_sal.h:308
#define _In_opt_
Definition: ms_sal.h:309
#define WRITE_DAC
Definition: nt_native.h:59
#define KEY_READ
Definition: nt_native.h:1023
NTSYSAPI NTSTATUS NTAPI RtlAnsiStringToUnicodeString(PUNICODE_STRING DestinationString, PANSI_STRING SourceString, BOOLEAN AllocateDestinationString)
#define KEY_CREATE_SUB_KEY
Definition: nt_native.h:1018
#define GENERIC_ALL
Definition: nt_native.h:92
#define GENERIC_WRITE
Definition: nt_native.h:90
NTSYSAPI VOID NTAPI RtlInitAnsiString(PANSI_STRING DestinationString, PCSZ SourceString)
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317
long LONG
Definition: pedump.c:60
#define STATUS_SUCCESS
Definition: shellext.h:65
HRESULT hr
Definition: shlfolder.c:183
#define TRACE_LEVEL_WARNING
Definition: storswtr.h:28
#define TRACE_LEVEL_VERBOSE
Definition: storswtr.h:30
#define TRACE_LEVEL_INFORMATION
Definition: storswtr.h:29
STRSAFEAPI StringCchCopyW(STRSAFE_LPWSTR pszDest, size_t cchDest, STRSAFE_LPCWSTR pszSrc)
Definition: strsafe.h:149
#define STRSAFE_MAX_CCH
Definition: strsafe.h:83
STRSAFEAPI StringCchLengthW(STRSAFE_LPCWSTR psz, size_t cchMax, size_t *pcchLength)
Definition: strsafe.h:842
FxDriver * FxDriver
Definition: fxldrum.h:184
IWudfDeviceStack2 * WudfDevStack
Definition: fxldrum.h:190
Definition: ps.c:97
#define GetHandle(h)
Definition: treelist.c:116
uint32_t ULONG
Definition: typedefs.h:59
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
STRING CANSI_STRING
Definition: umtypes.h:206
#define DECLARE_UNICODE_STRING_SIZE(_var, _size)
Definition: wdfcore.h:155
@ WdfDevStatePnpInit
Definition: wdfdevice.h:69
_Must_inspect_result_ _In_ PDRIVER_OBJECT DriverObject
Definition: wdfdriver.h:213
#define WDF_DRIVER_GLOBALS_NAME_LEN
Definition: wdfglobals.h:51
#define HRESULT_FROM_WIN32(x)
Definition: winerror.h:92
#define RegOpenKeyEx
Definition: winreg.h:520
static int init
Definition: wintirpc.c:33
__wchar_t WCHAR
Definition: xmlstorage.h:180
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185