ReactOS  0.4.15-dev-2701-g34593d9
fxdevicebase.cpp
Go to the documentation of this file.
1 /*++
2 
3 Copyright (c) Microsoft Corporation
4 
5 Module Name:
6 
7  FxDeviceBase.cpp
8 
9 Abstract:
10 
11  This is the class implementation for the base device class.
12 
13 Author:
14 
15 
16 
17 Environment:
18 
19  Both kernel and user mode
20 
21 Revision History:
22 
23 --*/
24 
25 #include "coreprivshared.hpp"
26 
27 extern "C" {
28 // #include "FxDeviceBase.tmh"
29 }
30 
32  __in PFX_DRIVER_GLOBALS FxDriverGlobals,
36  ) :
37  FxNonPagedObject(Type, Size, FxDriverGlobals)
38 {
39  m_Driver = Driver;
40 
43 
45 
47 
50 
52  SetDeviceBase(this);
53 }
54 
56  VOID
57  )
58 {
59  if (m_DisposeList != NULL) {
62  }
63 
64  if (m_CallbackLockPtr != NULL) {
65  delete m_CallbackLockPtr;
67  }
68 }
69 
74  )
75 {
76  switch (Params->Type) {
78  *Params->Object = this;
79  break;
80 
82  *Params->Object = (IFxHasCallbacks*) this;
83  break;
84 
85  default:
86  return FxNonPagedObject::QueryInterface(Params); // __super call
87  }
88 
89  return STATUS_SUCCESS;
90 }
91 
95  )
96 {
98  WDF_EXECUTION_LEVEL driverLevel;
99  WDF_SYNCHRONIZATION_SCOPE driverScope;
100 
101  ASSERT(m_Driver != NULL);
102 
103  //
104  // If WDF_OBJECT_ATTRIBUTES is specified, these override any
105  // default settings.
106  //
107  if (ObjectAttributes != NULL) {
108  m_ExecutionLevel = ObjectAttributes->ExecutionLevel;
109  m_SynchronizationScope = ObjectAttributes->SynchronizationScope;
110  }
111 
112  //
113  // If no WDFDEVICE specific attributes are specified, we
114  // get them from WDFDRIVER, which allows WDFDRIVER to
115  // provide a default for all WDFDEVICE's created.
116  //
117  m_Driver->GetConstraints(&driverLevel, &driverScope);
118 
120  m_ExecutionLevel = driverLevel;
121  }
122 
124  m_SynchronizationScope = driverScope;
125  }
126 
127  //
128  // Configure The Execution Level Constraint
129  //
133  //
134  // Currently, all event callbacks from FxDevice into the driver
135  // are from PASSIVE_LEVEL threads, and there is no need to defer
136  // to a system workitem like IoQueue. So we don't allocate and
137  // setup an FxSystemWorkItem here.
138  //
139  // If the FxDevice starts raising events to the device driver
140  // whose thread starts out above PASSIVE_LEVEL, then an FxSystemWorkItem
141  // would need to be allocated when WdfExecutionLevelPassive is specified.
142  //
143  // (FDO and PDO variants of FxDevice may own their own event dispatch
144  // and deferral logic separate from FxDevice.)
145  //
146  }
147  else {
150  }
151 
152  //
153  // Finish initializing the spin/mutex lock.
154  //
155  if (NULL == m_CallbackLockPtr) {
159  "WDFDEVICE %p, could not allocate callback lock, %!STATUS!",
160  GetHandle(), status);
161  goto Done;
162  }
163 
166 
168 
169 Done:
170  return status;
171 }
172 
173 VOID
175  __out_opt WDF_EXECUTION_LEVEL* ExecutionLevel,
176  __out_opt WDF_SYNCHRONIZATION_SCOPE* SynchronizationScope
177  )
178 {
179  if (ExecutionLevel != NULL) {
180  *ExecutionLevel = m_ExecutionLevel;
181  }
182 
183  if (SynchronizationScope != NULL) {
184  *SynchronizationScope = m_SynchronizationScope;
185  }
186 }
187 
191  )
192 {
193  if (LockObject != NULL) {
195  }
196 
197  return m_CallbackLockPtr;
198 }
199 
200 
201 VOID
204  __in MdDeviceObject AttachedDevice,
206  )
207 {
209  m_AttachedDevice.SetObject(AttachedDevice);
211 }
212 
217  )
218 {
219  FxObject* pParent, *pOrigParent;
222  PVOID pTag;
223 
225  if (pDeviceBase == NULL) {
227  Object->GetDriverGlobals(), TRACE_LEVEL_ERROR, TRACINGOBJECT,
228  "WDFHANDLE %p does not have a WDFDEVICE as an ancestor",
229  Object->GetObjectHandle());
230  return NULL;
231  }
232 
233  if (Callbacks == NULL) {
234  //
235  // Caller is not interested in the callbacks interface, just return
236  // now without searching for it.
237  //
238  return pDeviceBase;
239  }
240 
241  //
242  // Init out parameter.
243  //
244  *Callbacks = NULL;
245 
246  pOrigParent = Object;
247  pTag = pOrigParent;
248 
249  //
250  // By adding a reference now, we simulate what GetParentObjectReferenced
251  // does later, thus allowing simple logic on when/how to release the
252  // reference on exit.
253  //
254  Object->ADDREF(pTag);
255 
256  do {
257  //
258  // If successful, Callbacks will be != NULL
259  //
260  if (NT_SUCCESS(Object->QueryInterface(&cbParams))) {
261  ASSERT(*Callbacks != NULL);
262  //
263  // Release the reference previously taken by the top of the function
264  // or GetParentObjectReferenced in a previous pass in the loop.
265  //
266  Object->RELEASE(pTag);
267  return pDeviceBase;
268  }
269 
271 
272  //
273  // Release the reference previously taken by the top of the function
274  // or GetParentObjectReferenced in a previous pass in the loop.
275  //
276  Object->RELEASE(pTag);
277 
278  Object = pParent;
279  } while (Object != NULL);
280 
281  ASSERT(Object == NULL);
282 
283  //
284  // Queue presented requests do not have parents (to increase performance).
285  // Try to find the callback interface on this object's device base.
286  //
287  if (NT_SUCCESS(pDeviceBase->QueryInterface(&cbParams))) {
288  ASSERT(*Callbacks != NULL);
289  //
290  // Success, we got a callback interface.
291  //
292  return pDeviceBase;
293  }
294 
297  "WDFHANDLE %p does not have a callbacks interface in its object tree"
298  "(WDFDEVICE %p)", pOrigParent->GetObjectHandle(),
300 
301  return pDeviceBase;
302 }
303 
306  __in PFX_DRIVER_GLOBALS FxDriverGlobals,
308  )
309 {
310  FxObject* pParentObject;
312 
313  if (Attributes == NULL || Attributes->ParentObject == NULL) {
314  return NULL;
315  }
316 
317  FxObjectHandleGetPtr(FxDriverGlobals,
318  Attributes->ParentObject,
320  (PVOID*) &pParentObject);
321 
322  pDeviceBase = _SearchForDevice(pParentObject, NULL);
323 
324  return pDeviceBase;
325 }
326 
328 NTSTATUS
331  _In_ BOOLEAN SelfTarget
332  )
333 /*++
334 
335 Routine Description:
336 
337  Allocates an IO Target or an Self IO target for the FxDevice.
338 
339 Arguments:
340 
341  Target - Out - returns the pointer to the allocated IO target.
342 
343  SelfTarget - If TRUE allocates an Self IO Target, if FALSE allocates
344  a regular IO target.
345 
346 Returns:
347 
348  NTSTATUS
349 
350 --*/
351 {
354 
355  if (SelfTarget) {
358  } else {
361  }
362 
363  if (pTarget == NULL) {
365 
368  "WDFDEVICE %p could not allocate a WDFIOTARGET, %!STATUS!",
369  GetHandle(), status);
370 
371  goto Done;
372  }
373 
375  if (!NT_SUCCESS(status)) {
378  "WDFDEVICE %p failed to initialize (add) a WDFIOTARGET, %!STATUS!",
379  GetHandle(), status);
380 
381  goto Done;
382  }
383 
384  status = pTarget->Init(this);
385  if (!NT_SUCCESS(status)) {
388  "WDFDEVICE %p failed to initialize a WDFIOTARGET, %!STATUS!",
389  GetHandle(), status);
390 
391  goto Done;
392  }
393 
395  if (!NT_SUCCESS(status)) {
398  "WDFDEVICE %p failed to initialize (commit) a WDFIOTARGET, %!STATUS!",
399  GetHandle(), status);
400 
401  goto Done;
402  }
403 
405 
406 Done:
407  if (!NT_SUCCESS(status)) {
408  if (pTarget != NULL) {
410  pTarget = NULL;
411  }
412  }
413 
414  *Target = pTarget;
415 
416  return status;
417 }
NTSTATUS ConfigureConstraints(__in_opt PWDF_OBJECT_ATTRIBUTES ObjectAttributes)
FxIoTarget * pTarget
Definition: fxdeviceapi.cpp:97
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
virtual VOID DeleteObject(VOID)
MxDeviceObject m_AttachedDevice
Definition: fxdevice.hpp:472
#define _Must_inspect_result_
Definition: no_sal2.h:62
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
FxDeviceBase * pDeviceBase
_In_ WDFIOTARGET _In_ PWDF_REQUEST_COMPLETION_PARAMS Params
Definition: wdfrequest.h:306
_Must_inspect_result_ NTSTATUS Commit(__in_opt PWDF_OBJECT_ATTRIBUTES Attributes, __out_opt WDFOBJECT *ObjectHandle, __in_opt FxObject *Parent=NULL, __in BOOLEAN AssignDriverAsDefaultParent=TRUE)
Definition: fxobject.cpp:904
_Must_inspect_result_ NTSTATUS QueryInterface(__inout FxQueryInterfaceParams *Params)
#define __in_opt
Definition: dbghelp.h:38
WDFDEVICE __inline GetHandle(VOID)
Definition: fxdevice.hpp:237
LONG NTSTATUS
Definition: precomp.h:26
VOID MarkPassiveDispose(__in FxObjectLockState State=ObjectLock)
Definition: fxobject.hpp:944
VOID Init(__in MdDeviceObject DeviceObject, __in MdDeviceObject AttachedDevice, __in MdDeviceObject PhysicalDevice)
_Must_inspect_result_ NTSTATUS AllocateTarget(_Out_ FxIoTarget **Target, _In_ BOOLEAN SelfTarget)
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
VOID SetDeviceBase(__in CfxDeviceBase *DeviceBase)
Definition: fxobject.hpp:797
FxObjectHandleGetPtr(GetFxDriverGlobals(DriverGlobals), Fdo, FX_TYPE_DEVICE,(PVOID *)&pFdo)
FxDeviceBase(__in PFX_DRIVER_GLOBALS FxDriverGlobals, __in FxDriver *Driver, __in WDFTYPE Type, __in USHORT Size)
#define LockObject(Object, Irql)
Definition: titypes.h:34
static FxDeviceBase * _SearchForDevice(__in FxObject *Object, __out_opt IFxHasCallbacks **Callbacks)
_In_ PDEVICE_OBJECT DeviceObject
Definition: wdfdevice.h:2055
#define __out_opt
Definition: dbghelp.h:65
virtual _Must_inspect_result_ NTSTATUS AddIoTarget(__inout FxIoTarget *IoTarget)
Definition: fxdevice.hpp:247
PVOID __inline GetObjectHandle(VOID)
Definition: fxobject.hpp:603
#define TRACINGOBJECT
Definition: dbgtrace.h:59
virtual void Initialize(FxObject *ParentObject)=0
unsigned char BOOLEAN
FxCallbackLock * m_CallbackLockPtr
Definition: fxdevice.hpp:475
#define _Out_
Definition: no_sal2.h:160
enum _WDF_SYNCHRONIZATION_SCOPE WDF_SYNCHRONIZATION_SCOPE
FxDisposeList * m_DisposeList
Definition: fxdevice.hpp:466
#define TRACINGDEVICE
Definition: dbgtrace.h:58
#define ASSERT(a)
Definition: mode.c:44
const struct winhelp_callbacks Callbacks
Definition: callback.c:161
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
Type
Definition: Type.h:6
WDF_EXECUTION_LEVEL m_ExecutionLevel
Definition: fxdevice.hpp:478
MxDeviceObject m_PhysicalDevice
Definition: fxdevice.hpp:473
#define __inout
Definition: dbghelp.h:50
_Must_inspect_result_ NTSTATUS Init(__in CfxDeviceBase *Device)
_Must_inspect_result_ _In_ WDFCOLLECTION _In_ WDFOBJECT Object
#define TRACE_LEVEL_ERROR
Definition: storswtr.h:27
#define _In_
Definition: no_sal2.h:158
WDF_SYNCHRONIZATION_SCOPE m_SynchronizationScope
Definition: fxdevice.hpp:479
__inline PFX_DRIVER_GLOBALS GetDriverGlobals(VOID)
Definition: fxobject.hpp:734
unsigned short USHORT
Definition: pedump.c:61
FxCallbackLock * GetCallbackLockPtr(__out_opt FxObject **LockObject)
FxObject * m_CallbackLockObjectPtr
Definition: fxdevice.hpp:476
virtual VOID GetConstraints(__out WDF_EXECUTION_LEVEL *ExecutionLevel, __out WDF_SYNCHRONIZATION_SCOPE *SynchronizationScope)
Definition: fxdriver.hpp:282
USHORT WDFTYPE
Definition: fxtypes.h:29
__inline VOID SetObject(__in_opt MdDeviceObject DeviceObject)
DoTraceLevelMessage(pFxDriverGlobals, TRACE_LEVEL_VERBOSE, TRACINGPNP, "Enter, WDFDEVICE %p", Device)
#define NULL
Definition: types.h:112
#define WDF_NO_OBJECT_ATTRIBUTES
Definition: wdftypes.h:105
_In_ WDFIOTARGET Target
Definition: wdfrequest.h:306
VOID DeleteFromFailedCreate(VOID)
Definition: fxobject.cpp:391
VOID GetConstraints(__out_opt WDF_EXECUTION_LEVEL *ExecutionLevel, __out_opt WDF_SYNCHRONIZATION_SCOPE *SynchronizationScope)
FxObject * pParent
Definition: fxdpcapi.cpp:86
WDF_EXTERN_C_START enum _WDF_EXECUTION_LEVEL WDF_EXECUTION_LEVEL
FxDriver * m_Driver
Definition: fxdevice.hpp:469
virtual _Must_inspect_result_ NTSTATUS QueryInterface(__in FxQueryInterfaceParams *Params)
Definition: fxobject.cpp:255
#define STATUS_SUCCESS
Definition: shellext.h:65
LONG m_DmaPacketTransactionStatus
Definition: fxdevice.hpp:484
#define __in
Definition: dbghelp.h:35
_Must_inspect_result_ _In_ WDFDRIVER Driver
Definition: wdfcontrol.h:83
_Must_inspect_result_ FxObject * GetParentObjectReferenced(__in PVOID Tag)
Definition: fxobject.cpp:856
static SERVICE_STATUS status
Definition: service.c:31
CfxDeviceBase * GetDeviceBase(VOID)
Definition: fxobject.hpp:789
MxDeviceObject m_DeviceObject
Definition: fxdevice.hpp:471
~FxDeviceBase(VOID)
_Must_inspect_result_ _In_ WDFDEVICE _In_ PDEVICE_OBJECT PhysicalDevice
Definition: wdfdevice.h:2320
_Must_inspect_result_ _In_ WDFDMAENABLER _In_ _In_opt_ PWDF_OBJECT_ATTRIBUTES Attributes
Definition: ps.c:97