1 /*++
2 
3 Copyright (c) Microsoft Corporation
4 
5 Module Name:
6 
7     FxDeviceInit.hpp
8 
9 Abstract:
10 
11 
12 Author:
13 
14 
15 Environment:
16 
17     kernel mode only
18 
19 Revision History:
20 
21 --*/
22 
23 #ifndef __FXDEVICEINIT_HPP__
24 #define __FXDEVICEINIT_HPP__
25 
26 enum FxDeviceInitType {
27     FxDeviceInitTypeFdo = 0,
28     FxDeviceInitTypePdo,
29     FxDeviceInitTypeControlDevice
30 };
31 
32 struct FileObjectInit {
33     WDF_FILEOBJECT_CLASS Class;
34 
35     WDF_OBJECT_ATTRIBUTES Attributes;
36 
37     WDF_FILEOBJECT_CONFIG Callbacks;
38 
39     WDF_TRI_STATE AutoForwardCleanupClose;
40 
41     BOOLEAN Set;
42 };
43 
44 struct SecurityInit {
45     FxString* Sddl;
46 
47     GUID DeviceClass;
48 
49     BOOLEAN DeviceClassSet;
50 };
51 
52 struct PnpPowerInit {
53     WDF_PNPPOWER_EVENT_CALLBACKS PnpPowerEventCallbacks;
54 
55     WDF_POWER_POLICY_EVENT_CALLBACKS PolicyEventCallbacks;
56 
57     FxPnpStateCallback* PnpStateCallbacks;
58 
59     FxPowerStateCallback* PowerStateCallbacks;
60 
61     FxPowerPolicyStateCallback* PowerPolicyStateCallbacks;
62 
63     WDF_TRI_STATE PowerPolicyOwner;
64 };
65 
66 struct FdoInit {
67     WDF_FDO_EVENT_CALLBACKS EventCallbacks;
68 
69     WDF_CHILD_LIST_CONFIG ListConfig;
70 
71     WDF_OBJECT_ATTRIBUTES ListConfigAttributes;
72 
73     BOOLEAN Filter;
74 
75     MdDeviceObject PhysicalDevice;
76 };
77 
78 
79 
80 
81 #include "fxdeviceinitshared.hpp"
82 
83 struct ControlInit {
ControlInitControlInit84     ControlInit(
85         VOID
86         )
87     {
88         ShutdownNotification = NULL;
89         Flags = 0;
90     }
91 
92     PFN_WDF_DEVICE_SHUTDOWN_NOTIFICATION ShutdownNotification;
93 
94     UCHAR Flags;
95 };
96 
97 //
98 // The typedef for a pointer to this structure is exposed in wdfdevice.h
99 //
100 struct WDFDEVICE_INIT : public FxStump {
101 public:
102     WDFDEVICE_INIT(
103         __in FxDriver* Driver
104         );
105 
106     ~WDFDEVICE_INIT();
107 
108     VOID
109     SetPdo(
110         __in FxDevice* Parent
111         );
112 
113     BOOLEAN
IsFdoInitWDFDEVICE_INIT114     IsFdoInit(
115         VOID
116         )
117     {
118         return InitType == FxDeviceInitTypeFdo;
119     }
120 
121     BOOLEAN
IsNotFdoInitWDFDEVICE_INIT122     IsNotFdoInit(
123         VOID
124         )
125     {
126         return InitType != FxDeviceInitTypeFdo;
127     }
128 
129     BOOLEAN
IsPdoInitWDFDEVICE_INIT130     IsPdoInit(
131         VOID
132         )
133     {
134         return InitType == FxDeviceInitTypePdo;
135     }
136 
137     BOOLEAN
IsNotPdoInitWDFDEVICE_INIT138     IsNotPdoInit(
139         VOID
140         )
141     {
142         return InitType != FxDeviceInitTypePdo;
143     }
144     BOOLEAN
IsNotControlDeviceInitWDFDEVICE_INIT145     IsNotControlDeviceInit(
146         VOID
147         )
148     {
149         return InitType != FxDeviceInitTypeControlDevice;
150     }
151 
152     BOOLEAN
IsControlDeviceInitWDFDEVICE_INIT153     IsControlDeviceInit(
154         VOID
155         )
156     {
157         return InitType == FxDeviceInitTypeControlDevice;
158     }
159 
160     BOOLEAN
HasNameWDFDEVICE_INIT161     HasName(
162         VOID
163         )
164     {
165         if (DeviceName != NULL ||
166             (Characteristics & FILE_AUTOGENERATED_DEVICE_NAME)) {
167             return TRUE;
168         }
169         else {
170             return FALSE;
171         }
172     }
173 
174     BOOLEAN
175     ShouldCreateSecure(
176         VOID
177         );
178 
179     BOOLEAN
IsPwrPolOwnerWDFDEVICE_INIT180     IsPwrPolOwner(
181         VOID
182         )
183     {
184         if (PnpPower.PowerPolicyOwner == WdfTrue) {
185             return TRUE;
186         }
187         else if (PnpPower.PowerPolicyOwner == WdfFalse) {
188             return FALSE;
189         }
190         else if (IsPdoInit()) {
191             //
192             // This is a PDO. If we are a raw PDO, we own pwr policy.
193             //
194             return Pdo.Raw;
195         }
196         else {
197             ASSERT(IsFdoInit());
198 
199             //
200             // This is an FDO. If we are not a filter, we own pwr policy.
201             //
202             return (Fdo.Filter == FALSE ? TRUE : FALSE);
203         }
204     }
205 
206     _Must_inspect_result_
207     NTSTATUS
208     AssignName(
209         __in PFX_DRIVER_GLOBALS FxDriverGlobals,
210         __in const UNICODE_STRING* Name
211         );
212 
213     static
214     _Must_inspect_result_
215     PWDFDEVICE_INIT
216     _AllocateControlDeviceInit(
217         __in FxDriver* Driver,
218         __in const UNICODE_STRING* SDDLString
219         );
220 
221     VOID
222     AddCxDeviceInit(
223         __in PWDFCXDEVICE_INIT CxDeviceInit
224         );
225 
226     VOID
227     AssignIoType(
228         _In_ PWDF_IO_TYPE_CONFIG IoTypeConfig
229         );
230 
231 public:
232     PFX_DRIVER_GLOBALS DriverGlobals;
233 
234     FxDriver* Driver;
235 
236     FxDevice* CreatedDevice;
237 
238     BOOLEAN CreatedOnStack;
239 
240     BOOLEAN Exclusive;
241 
242     BOOLEAN PowerPageable;
243 
244     BOOLEAN Inrush;
245 
246     //
247     // If set, the Cx/Client intends to leverage Self IO Target
248     //
249     BOOLEAN RequiresSelfIoTarget;
250 
251     ULONG RemoveLockOptionFlags;
252 
253     FxDeviceInitType InitType;
254 
255     WDF_DEVICE_IO_TYPE ReadWriteIoType;
256 
257     DEVICE_TYPE DeviceType;
258 
259     FxString* DeviceName;
260 
261     ULONG Characteristics;
262 
263     FileObjectInit FileObject;
264 
265     SecurityInit Security;
266 
267     WDF_OBJECT_ATTRIBUTES RequestAttributes;
268 
269     FxIrpPreprocessInfo* PreprocessInfo;
270 
271     PFN_WDF_IO_IN_CALLER_CONTEXT IoInCallerContextCallback;
272 
273     WDF_RELEASE_HARDWARE_ORDER_ON_FAILURE ReleaseHardwareOrderOnFailure;
274 
275     PnpPowerInit    PnpPower;
276     FdoInit         Fdo;
277     PdoInit         Pdo;
278     ControlInit     Control;
279 
280     //
281     // Class extension's device init.
282     //
283     LIST_ENTRY      CxDeviceInitListHead;
284 
285 #if (FX_CORE_MODE == FX_CORE_USER_MODE)
286     //
287     // IoType preference for IOCTL
288     //
289     WDF_DEVICE_IO_TYPE DeviceControlIoType;
290 
291     //
292     // Direct I/O threshold
293     //
294     ULONG DirectTransferThreshold;
295 
296     //
297     // Weak reference to host side device stack
298     //
299     IWudfDeviceStack * DevStack;
300 
301     //
302     // Kernel redirector's side object name.
303     //
304     PWSTR KernelDeviceName;
305 
306     //
307     // PnP devinode hw key handle
308     //
309     HKEY PdoKey;
310 
311     //
312     // Registry sub-path name containing driver configuration information.
313     // The registry path name is relative to the "device key".
314     //
315     PWSTR ConfigRegistryPath;
316 
317     //
318     // PDO Instance ID
319     //
320     PWSTR DevInstanceID;
321 
322     //
323     // This ID is used by Host to associated host device and its driver info.
324     //
325     ULONG DriverID;
326 #endif
327 
328 };
329 
330 #endif // __FXDEVICEINIT_HPP__
331