ReactOS  0.4.15-dev-1632-g4e289ce
fxiotargetremotekm.cpp
Go to the documentation of this file.
1 /*++
2 
3 Copyright (c) Microsoft Corporation
4 
5 Module Name:
6 
7  FxIoTargetRemoteKm.cpp
8 
9 Abstract:
10 
11 Author:
12 
13 Environment:
14 
15  kernel mode only
16 
17 Revision History:
18 
19 --*/
20 
21 #include "../../fxtargetsshared.hpp"
22 
23 extern "C" {
24 // #include "FxIoTargetRemoteKm.tmh"
25 }
26 
27 #include <initguid.h>
28 #include "wdmguid.h"
29 
35  )
36 {
39  FxIoTargetRemote* pThis;
41 
44  pThis = (FxIoTargetRemote*) Context;
45 
46  //
47  // In one of these callbacks, the driver may decide to delete the target.
48  // If that is the case, we need to be able to return and deref the object until
49  // we are done.
50  //
51  pThis->ADDREF((PVOID)_PlugPlayNotification);
52 
54 
56 
57  if (FxIsEqualGuid(&pNotification->Event, &GUID_TARGET_DEVICE_QUERY_REMOVE)) {
58 
61  "WDFIOTARGET %p: query remove notification", pThis->GetObjectHandle());
62 
63  //
64  // Device is gracefully being removed. PnP is asking us to close down
65  // the target. If there is a driver callback, there is *no* default
66  // behavior. This is because we don't know what the callback is going
67  // to do. For instance, the driver could reopen the target to a
68  // different device in a multi-path scenario.
69  //
70  if (pThis->m_EvtQueryRemove.m_Method != NULL) {
71  status = pThis->m_EvtQueryRemove.Invoke(pThis->GetHandle());
72  }
73  else {
76  "WDFIOTARGET %p: query remove, default action (close for QR)",
77  pThis->GetObjectHandle());
78 
79  //
80  // No callback, close it down conditionally.
81  //
83  }
84  }
85  else if (FxIsEqualGuid(&pNotification->Event, &GUID_TARGET_DEVICE_REMOVE_COMPLETE)) {
86 
89  "WDFIOTARGET %p: remove complete notification", pThis->GetObjectHandle());
90 
91  //
92  // The device was surprise removed, close it for good if the driver has
93  // no override.
94  //
95  if (pThis->m_EvtRemoveComplete.m_Method != NULL) {
96  pThis->m_EvtRemoveComplete.Invoke(pThis->GetHandle());
97  }
98  else {
101  "WDFIOTARGET %p: remove complete, default action (close)",
102  pThis->GetObjectHandle());
103 
104  //
105  // The device is now gone for good. Close down the target for good.
106  //
108  }
109  }
110  else if (FxIsEqualGuid(&pNotification->Event, &GUID_TARGET_DEVICE_REMOVE_CANCELLED)) {
111 
114  "WDFIOTARGET %p: remove canceled notification", pThis->GetObjectHandle());
115 
116  if (pThis->m_EvtRemoveCanceled.m_Method != NULL) {
117  pThis->m_EvtRemoveCanceled.Invoke(pThis->GetHandle());
118  }
119  else {
121 
124  "WDFIOTARGET %p: remove canceled, default action (reopen)",
125  pThis->GetObjectHandle());
126 
128 
129  //
130  // Attempt to reopen the target with stored settings
131  //
132  status = pThis->Open(&params);
133  }
134  }
135 
136  pThis->RELEASE((PVOID)_PlugPlayNotification);
137 
138  return status;
139 }
140 
141 NTSTATUS
143  )
144 {
146 
147  //
148  // Register for PNP notifications on the handle we just opened.
149  // This will notify us of pnp state changes on the handle.
150  //
153  0,
157  this,
159 
160  return status;
161 }
162 
163 VOID
166  )
167 {
168  if (Handle != NULL) {
169 
170 
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
194  }
195  else {
197  }
198  }
199 }
200 
201 NTSTATUS
205  )
206 {
208  IO_STATUS_BLOCK ioStatus;
210 
212  &pParams->TargetDeviceName,
214  NULL,
215  NULL);
216 
217  status = ZwCreateFile(&m_TargetHandle,
218  pParams->DesiredAccess,
219  &oa,
220  &ioStatus,
221  pParams->AllocationSizePointer,
222  pParams->FileAttributes,
223  pParams->ShareAccess,
224  pParams->CreateDisposition,
225  pParams->CreateOptions,
226  pParams->EaBuffer,
227  pParams->EaBufferLength);
228 
229  OpenParams->FileInformation = (ULONG)ioStatus.Information;
230 
231  if (NT_SUCCESS(status)) {
234  "ZwCreateFile for WDFIOTARGET %p returned status %!STATUS!, info 0x%x",
235  GetObjectHandle(), status, (ULONG) ioStatus.Information);
236 
237  //
238  // The open operation was successful. Dereference the file handle and
239  // obtain a pointer to the device object for the handle.
240  //
243  pParams->DesiredAccess,
245  KernelMode,
247  NULL);
248 
249  if (NT_SUCCESS(status)) {
251 
252  if (m_TargetDevice == NULL) {
255  "WDFIOTARGET %p, could not convert filobj %p to devobj",
257 
259  }
260  }
261  else {
264  "WDFIOTARGET %p, could not convert handle %p to fileobject, "
265  "status %!STATUS!",
267  }
268  }
269  else {
272  "ZwCreateFile for WDFIOTARGET %p returned status %!STATUS!, info 0x%x",
273  GetObjectHandle(), status, (ULONG) ioStatus.Information);
274  }
275 
276  return status;
277 }
278 
279 NTSTATUS
282  )
283 {
285  FxAutoIrp irp(NULL);
286  PIRP pIrp;
288 
290 
292 
293  if (pIrp != NULL) {
295 
296  irp.SetIrp(pIrp);
297 
299  stack->MajorFunction = IRP_MJ_PNP;
300  stack->MinorFunction = IRP_MN_QUERY_DEVICE_RELATIONS;
301  stack->Parameters.QueryDeviceRelations.Type = TargetDeviceRelation;
302 
303  //
304  // Initialize the status to error in case the bus driver decides not
305  // to set it correctly.
306  //
308 
310 
311  if (NT_SUCCESS(status)) {
312  PDEVICE_RELATIONS pRelations;
313 
314  pRelations = (PDEVICE_RELATIONS) irp.GetInformation();
315 
316  ASSERT(pRelations != NULL);
317 
318  //
319  // m_TargetPdo was referenced by the bus driver, it will be
320  // dereferenced when the target is closed.
321  //
322  m_TargetPdo = pRelations->Objects[0];
323 
324  //
325  // We, as the caller, are responsible for freeing the relations
326  // that the bus driver allocated.
327  //
328  ExFreePool(pRelations);
329  }
330  else {
331  //
332  // Could not retrieve the PDO pointer, error handled later
333  //
334  DO_NOTHING();
335  }
336  }
337  else {
338  //
339  // Could not even allocate an irp, failure.
340  //
344  "Unable to allocate memory for IRP WDFIOTARGET %p, %!STATUS!",
346  }
347 
348  //
349  // Only fail the open if we cannot allocate an irp or if the lower
350  // driver could not allocate a relations.
351  //
353  *Close = TRUE;
354  }
355  else {
357  }
358 
359  //
360  // Remove the reference taken by IoGetAttachedDeviceReference
361  //
363 
364  return status;
365 }
366 
_In_ PVOID NotificationStructure
Definition: iofuncs.h:1203
MdDeviceObject m_TargetDevice
Definition: fxiotarget.hpp:910
return STATUS_NOT_SUPPORTED
#define _Must_inspect_result_
Definition: no_sal2.h:62
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
struct _TARGET_DEVICE_REMOVAL_NOTIFICATION * PTARGET_DEVICE_REMOVAL_NOTIFICATION
VOID UnregisterForPnpNotification(_In_ MdTargetNotifyHandle Handle)
VOID SetStatus(__in NTSTATUS Status)
Definition: fxirpum.cpp:457
#define TRUE
Definition: types.h:120
PDEVICE_OBJECT Objects[1]
Definition: iotypes.h:2160
#define IRP_MJ_PNP
Definition: cdrw_usr.h:52
VOID Invoke(__in WDFIOTARGET Target)
LONG NTSTATUS
Definition: precomp.h:26
_Must_inspect_result_ BOOLEAN __inline FxIsEqualGuid(__in CONST GUID *Lhs, __in CONST GUID *Rhs)
Definition: fxglobals.h:977
static DRIVER_NOTIFICATION_CALLBACK_ROUTINE _PlugPlayNotification
static stack_node_t * stack
Definition: rpn_ieee.c:37
PDEVICE_OBJECT NTAPI IoGetRelatedDeviceObject(IN PFILE_OBJECT FileObject)
Definition: device.c:1539
PFN_WDF_IO_TARGET_QUERY_REMOVE m_Method
FxIrp * pIrp
#define __inout_opt
Definition: dbghelp.h:53
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
FxDriver * m_Driver
Definition: fxiotarget.hpp:900
FxIoTargetRemoveComplete m_EvtRemoveComplete
FxIoTargetQueryRemove m_EvtQueryRemove
NTSTATUS NTAPI ObReferenceObjectByHandle(IN HANDLE Handle, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, OUT PVOID *Object, OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL)
Definition: obref.c:496
PDEVICE_OBJECT pTopOfStack
#define FALSE
Definition: types.h:117
GLenum const GLfloat * params
Definition: glext.h:5645
CHECK_RETURN_IF_USER_MODE NTSTATUS SendIrpSynchronously(__in MdDeviceObject DeviceObject)
Definition: fxirpum.cpp:151
#define TRACE_LEVEL_VERBOSE
Definition: storswtr.h:30
PVOID __inline GetObjectHandle(VOID)
Definition: fxobject.hpp:603
struct _DEVICE_RELATIONS * PDEVICE_RELATIONS
unsigned char BOOLEAN
MdFileObject m_TargetFileObject
Definition: fxiotarget.hpp:923
MdDeviceObject m_TargetPdo
Definition: fxiotarget.hpp:918
PIO_STACK_LOCATION GetNextIrpStackLocation(VOID)
Definition: fxirpum.cpp:387
PDEVICE_OBJECT NTAPI IoGetAttachedDeviceReference(PDEVICE_OBJECT DeviceObject)
Definition: device.c:1406
PFN_IO_UNREGISTER_PLUGPLAY_NOTIFICATION_EX IoUnregisterPlugPlayNotificationEx
Definition: fxglobals.h:740
Definition: _stack.h:47
NTSTATUS GetTargetDeviceRelations(_Out_ BOOLEAN *Close)
PFX_DRIVER_GLOBALS pFxDriverGlobals
#define ASSERT(a)
Definition: mode.c:45
if(!(yy_init))
Definition: macro.lex.yy.c:714
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_NO_SUCH_DEVICE
Definition: udferr_usr.h:136
#define ObDereferenceObject
Definition: obfuncs.h:203
static __inline KIRQL MxGetCurrentIrql()
Definition: mxgeneralkm.h:86
#define _Inout_
Definition: no_sal2.h:162
FORCEINLINE VOID WDF_IO_TARGET_OPEN_PARAMS_INIT_REOPEN(_Out_ PWDF_IO_TARGET_OPEN_PARAMS Params)
Definition: wdfiotarget.h:340
_Must_inspect_result_ NTSTATUS Invoke(__in WDFIOTARGET IoTarget)
NTSTATUS NTAPI IoUnregisterPlugPlayNotification(_In_ PVOID NotificationEntry)
Definition: pnpnotify.c:479
VOID Close(__in FxIoTargetRemoteCloseReason Reason)
_Must_inspect_result_ _In_ WDFIOTARGET _In_ PWDF_IO_TARGET_OPEN_PARAMS OpenParams
Definition: wdfiotarget.h:398
FxLibraryGlobalsType FxLibraryGlobals
Definition: globals.cpp:95
POBJECT_TYPE IoFileObjectType
Definition: iomgr.c:36
#define TRACE_LEVEL_ERROR
Definition: storswtr.h:27
#define TRACE_LEVEL_WARNING
Definition: storswtr.h:28
#define DISPATCH_LEVEL
Definition: env_spec_w32.h:696
#define _In_
Definition: no_sal2.h:158
__inline MdDriverObject GetDriverObject(VOID)
Definition: fxdriver.hpp:252
ULONG_PTR GetInformation()
Definition: fxirpum.cpp:513
__inline PFX_DRIVER_GLOBALS GetDriverGlobals(VOID)
Definition: fxobject.hpp:734
MdTargetNotifyHandle m_TargetNotifyHandle
PFN_WDF_IO_TARGET_REMOVE_CANCELED m_Method
Definition: sacdrv.h:267
FxIoTargetRemoveCanceled m_EvtRemoveCanceled
DoTraceLevelMessage(pFxDriverGlobals, TRACE_LEVEL_VERBOSE, TRACINGPNP, "Enter, WDFDEVICE %p", Device)
_Must_inspect_result_ NTSTATUS Open(__in PWDF_IO_TARGET_OPEN_PARAMS OpenParams)
#define NULL
Definition: types.h:112
#define IRP_MN_QUERY_DEVICE_RELATIONS
_In_ HANDLE Handle
Definition: extypes.h:390
VOID Invoke(__in WDFIOTARGET Target)
unsigned int ULONG
Definition: retypes.h:1
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
NTSTATUS NTAPI IoRegisterPlugPlayNotification(_In_ IO_NOTIFICATION_EVENT_CATEGORY EventCategory, _In_ ULONG EventCategoryFlags, _In_opt_ PVOID EventCategoryData, _In_ PDRIVER_OBJECT DriverObject, _In_ PDRIVER_NOTIFICATION_CALLBACK_ROUTINE CallbackRoutine, _Inout_opt_ PVOID Context, _Out_ PVOID *NotificationEntry)
Definition: pnpnotify.c:345
NTSTATUS RegisterForPnpNotification(VOID)
PIRP NTAPI IoAllocateIrp(IN CCHAR StackSize, IN BOOLEAN ChargeQuota)
Definition: irp.c:615
#define STATUS_SUCCESS
Definition: shellext.h:65
MdIrp SetIrp(MdIrp irp)
Definition: fxirpkm.hpp:71
#define __in
Definition: dbghelp.h:35
static SERVICE_STATUS status
Definition: service.c:31
__inline WDFIOTARGET GetHandle(VOID)
#define TRACINGIOTARGET
Definition: dbgtrace.h:72
NTSTATUS OpenTargetHandle(_In_ PWDF_IO_TARGET_OPEN_PARAMS OpenParams, _Inout_ FxIoTargetRemoveOpenParams *pParams)
FxIrp * irp
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
PFN_WDF_IO_TARGET_REMOVE_COMPLETE m_Method
#define DO_NOTHING()
Definition: mxgeneral.h:32
Definition: ps.c:97