1 /*++
2 
3 Copyright (c) Microsoft Corporation
4 
5 Module Name:
6 
7     FxDeviceKM.hpp
8 
9 Abstract:
10 
11     This is the definition of the FxDevice object KM specific
12 
13 Author:
14 
15 
16 
17 Environment:
18 
19     Kernel mode only
20 
21 Revision History:
22 
23 --*/
24 
25 #ifndef _FXDEVICEKM_H_
26 #define _FXDEVICEKM_H_
27 
28 __inline
29 FxWdmDeviceExtension*
30 FxDevice::_GetFxWdmExtension(
31     __in MdDeviceObject DeviceObject
32     )
33 {
34     //
35     // DeviceObject->DeviceExtension points to our FxDevice allocation.  We
36     // get the underlying DeviceExtension allocated as part of the
37     // PDEVICE_OBJECT by adding the sizeof(DEVICE_OBJECT) to get the start
38     // of the DeviceExtension.  This is documented behavior on how the
39     // DeviceExtension can be found.
40     //
41     return (FxWdmDeviceExtension*) WDF_PTR_ADD_OFFSET(DeviceObject,
42                                                       sizeof(*DeviceObject));
43 }
44 
45 __inline
46 MdRemoveLock
47 FxDevice::GetRemoveLock(
48     VOID
49     )
50 {
51     return &FxDevice::_GetFxWdmExtension(
52         GetDeviceObject())->IoRemoveLock;
53 }
54 
55 __inline
56 BOOLEAN
57 FxDevice::IsRemoveLockEnabledForIo(
58     VOID
59     )
60 {
61     if (m_DeviceObject.GetObject() != NULL) {
62         return (_GetFxWdmExtension(m_DeviceObject.GetObject())->RemoveLockOptionFlags &
63             WDF_REMOVE_LOCK_OPTION_ACQUIRE_FOR_IO) ? TRUE : FALSE;
64     }
65     else {
66         return FALSE;
67     }
68 }
69 
70 __inline
71 FxDevice*
72 FxDevice::GetFxDevice(
73     __in MdDeviceObject DeviceObject
74     )
75 {
76     MxDeviceObject deviceObject((MdDeviceObject)DeviceObject);
77 
78     //
79     // DeviceExtension points to the start of the first context assigned to
80     // WDFDEVICE.  We walk backwards from the context to the FxDevice*.
81     //
82     return (FxDevice*) CONTAINING_RECORD(deviceObject.GetDeviceExtension(),
83                                          FxContextHeader,
84                                          Context)->Object;
85 }
86 
87 __inline
88 VOID
89 FxDevice::DetachDevice(
90     VOID
91     )
92 {
93     if (m_AttachedDevice.GetObject() != NULL) {
94         Mx::MxDetachDevice(m_AttachedDevice.GetObject());
95         m_AttachedDevice.SetObject(NULL);
96     }
97 }
98 
99 __inline
100 VOID
101 FxDevice::InvalidateDeviceState(
102     VOID
103     )
104 {
105     PDEVICE_OBJECT pdo;
106 
107     //
108     // If the PDO is not yet reported to pnp, do not call the API.  In this
109     // case, the PDO will soon be reported and started and the state that
110     // was just set will be queried for automatically by pnp as a part of
111     // start.
112     //
113     pdo = GetSafePhysicalDevice();
114 
115     if (pdo != NULL) {
116         IoInvalidateDeviceState(pdo);
117     }
118 }
119 
120 VOID
121 __inline
122 FxDevice::DeleteSymbolicLink(
123     VOID
124     )
125 {
126     if (m_SymbolicLinkName.Buffer != NULL) {
127         //
128         // Must be at PASSIVE_LEVEL for this call
129         //
130         if (m_SymbolicLinkName.Length) {
131             Mx::MxDeleteSymbolicLink(&m_SymbolicLinkName);
132         }
133 
134         FxPoolFree(m_SymbolicLinkName.Buffer);
135         RtlZeroMemory(&m_SymbolicLinkName, sizeof(m_SymbolicLinkName));
136     }
137 }
138 
139 __inline
140 NTSTATUS
141 FxDevice::_OpenDeviceRegistryKey(
142     _In_ MdDeviceObject DeviceObject,
143     _In_ ULONG DevInstKeyType,
144     _In_ ACCESS_MASK DesiredAccess,
145     _Out_ PHANDLE DevInstRegKey
146     )
147 {
148     return IoOpenDeviceRegistryKey(DeviceObject,
149                                    DevInstKeyType,
150                                    DesiredAccess,
151                                    DevInstRegKey);
152 }
153 
154 __inline
155 NTSTATUS
156 FxDevice::_GetDeviceProperty(
157     _In_      MdDeviceObject DeviceObject,
158     _In_      DEVICE_REGISTRY_PROPERTY DeviceProperty,
159     _In_      ULONG BufferLength,
160     _Out_opt_ PVOID PropertyBuffer,
161     _Out_     PULONG ResultLength
162     )
163 {
164     return IoGetDeviceProperty(DeviceObject,
165                                DeviceProperty,
166                                BufferLength,
167                                PropertyBuffer,
168                                ResultLength);
169 }
170 
171 #endif //_FXDEVICEKM_H_
172