ReactOS  0.4.15-dev-3287-gfec35dc
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 
32 STDCALL
36  )
37 {
40  FxIoTargetRemote* pThis;
42 
45  pThis = (FxIoTargetRemote*) Context;
46 
47  //
48  // In one of these callbacks, the driver may decide to delete the target.
49  // If that is the case, we need to be able to return and deref the object until
50  // we are done.
51  //
52  pThis->ADDREF((PVOID)_PlugPlayNotification);
53 
55 
57 
58  if (FxIsEqualGuid(&pNotification->Event, &GUID_TARGET_DEVICE_QUERY_REMOVE)) {
59 
62  "WDFIOTARGET %p: query remove notification", pThis->GetObjectHandle());
63 
64  //
65  // Device is gracefully being removed. PnP is asking us to close down
66  // the target. If there is a driver callback, there is *no* default
67  // behavior. This is because we don't know what the callback is going
68  // to do. For instance, the driver could reopen the target to a
69  // different device in a multi-path scenario.
70  //
71  if (pThis->m_EvtQueryRemove.m_Method != NULL) {
72  status = pThis->m_EvtQueryRemove.Invoke(pThis->GetHandle());
73  }
74  else {
77  "WDFIOTARGET %p: query remove, default action (close for QR)",
78  pThis->GetObjectHandle());
79 
80  //
81  // No callback, close it down conditionally.
82  //
84  }
85  }
86  else if (FxIsEqualGuid(&pNotification->Event, &GUID_TARGET_DEVICE_REMOVE_COMPLETE)) {
87 
90  "WDFIOTARGET %p: remove complete notification", pThis->GetObjectHandle());
91 
92  //
93  // The device was surprise removed, close it for good if the driver has
94  // no override.
95  //
96  if (pThis->m_EvtRemoveComplete.m_Method != NULL) {
97  pThis->m_EvtRemoveComplete.Invoke(pThis->GetHandle());
98  }
99  else {
102  "WDFIOTARGET %p: remove complete, default action (close)",
103  pThis->GetObjectHandle());
104 
105  //
106  // The device is now gone for good. Close down the target for good.
107  //
109  }
110  }
111  else if (FxIsEqualGuid(&pNotification->Event, &GUID_TARGET_DEVICE_REMOVE_CANCELLED)) {
112 
115  "WDFIOTARGET %p: remove canceled notification", pThis->GetObjectHandle());
116 
117  if (pThis->m_EvtRemoveCanceled.m_Method != NULL) {
118  pThis->m_EvtRemoveCanceled.Invoke(pThis->GetHandle());
119  }
120  else {
122 
125  "WDFIOTARGET %p: remove canceled, default action (reopen)",
126  pThis->GetObjectHandle());
127 
129 
130  //
131  // Attempt to reopen the target with stored settings
132  //
133  status = pThis->Open(&params);
134  }
135  }
136 
137  pThis->RELEASE((PVOID)_PlugPlayNotification);
138 
139  return status;
140 }
141 
142 NTSTATUS
144  )
145 {
147 
148  //
149  // Register for PNP notifications on the handle we just opened.
150  // This will notify us of pnp state changes on the handle.
151  //
154  0,
158  this,
160 
161  return status;
162 }
163 
164 VOID
167  )
168 {
169  if (Handle != NULL) {
170 
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
195  }
196  else {
198  }
199  }
200 }
201 
202 NTSTATUS
206  )
207 {
209  IO_STATUS_BLOCK ioStatus;
211 
213  &pParams->TargetDeviceName,
215  NULL,
216  NULL);
217 
218  status = ZwCreateFile(&m_TargetHandle,
219  pParams->DesiredAccess,
220  &oa,
221  &ioStatus,
222  pParams->AllocationSizePointer,
223  pParams->FileAttributes,
224  pParams->ShareAccess,
225  pParams->CreateDisposition,
226  pParams->CreateOptions,
227  pParams->EaBuffer,
228  pParams->EaBufferLength);
229 
230  OpenParams->FileInformation = (ULONG)ioStatus.Information;
231 
232  if (NT_SUCCESS(status)) {
235  "ZwCreateFile for WDFIOTARGET %p returned status %!STATUS!, info 0x%x",
236  GetObjectHandle(), status, (ULONG) ioStatus.Information);
237 
238  //
239  // The open operation was successful. Dereference the file handle and
240  // obtain a pointer to the device object for the handle.
241  //
244  pParams->DesiredAccess,
246  KernelMode,
248  NULL);
249 
250  if (NT_SUCCESS(status)) {
252 
253  if (m_TargetDevice == NULL) {
256  "WDFIOTARGET %p, could not convert filobj %p to devobj",
258 
260  }
261  }
262  else {
265  "WDFIOTARGET %p, could not convert handle %p to fileobject, "
266  "status %!STATUS!",
268  }
269  }
270  else {
273  "ZwCreateFile for WDFIOTARGET %p returned status %!STATUS!, info 0x%x",
274  GetObjectHandle(), status, (ULONG) ioStatus.Information);
275  }
276 
277  return status;
278 }
279 
280 NTSTATUS
283  )
284 {
286  FxAutoIrp irp(NULL);
287  PIRP pIrp;
289 
291 
293 
294  if (pIrp != NULL) {
296 
297  irp.SetIrp(pIrp);
298 
300  stack->MajorFunction = IRP_MJ_PNP;
301  stack->MinorFunction = IRP_MN_QUERY_DEVICE_RELATIONS;
302  stack->Parameters.QueryDeviceRelations.Type = TargetDeviceRelation;
303 
304  //
305  // Initialize the status to error in case the bus driver decides not
306  // to set it correctly.
307  //
309 
311 
312  if (NT_SUCCESS(status)) {
313  PDEVICE_RELATIONS pRelations;
314 
315  pRelations = (PDEVICE_RELATIONS) irp.GetInformation();
316 
317  ASSERT(pRelations != NULL);
318 
319  //
320  // m_TargetPdo was referenced by the bus driver, it will be
321  // dereferenced when the target is closed.
322  //
323  m_TargetPdo = pRelations->Objects[0];
324 
325  //
326  // We, as the caller, are responsible for freeing the relations
327  // that the bus driver allocated.
328  //
329  ExFreePool(pRelations);
330  }
331  else {
332  //
333  // Could not retrieve the PDO pointer, error handled later
334  //
335  DO_NOTHING();
336  }
337  }
338  else {
339  //
340  // Could not even allocate an irp, failure.
341  //
345  "Unable to allocate memory for IRP WDFIOTARGET %p, %!STATUS!",
347  }
348 
349  //
350  // Only fail the open if we cannot allocate an irp or if the lower
351  // driver could not allocate a relations.
352  //
354  *Close = TRUE;
355  }
356  else {
358  }
359 
360  //
361  // Remove the reference taken by IoGetAttachedDeviceReference
362  //
364 
365  return status;
366 }
367 
_In_ PVOID NotificationStructure
Definition: iofuncs.h:1206
MdDeviceObject m_TargetDevice
Definition: fxiotarget.hpp:910
return STATUS_NOT_SUPPORTED
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
struct _TARGET_DEVICE_REMOVAL_NOTIFICATION * PTARGET_DEVICE_REMOVAL_NOTIFICATION
VOID UnregisterForPnpNotification(_In_ MdTargetNotifyHandle Handle)
#define _Inout_
Definition: ms_sal.h:378
VOID SetStatus(__in NTSTATUS Status)
Definition: fxirpum.cpp:457
#define TRUE
Definition: types.h:120
PDEVICE_OBJECT Objects[1]
Definition: iotypes.h:2163
#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 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
if(dx==0 &&dy==0)
Definition: linetemp.h:174
#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:494
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
#define _In_
Definition: ms_sal.h:308
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 STDCALL
Definition: wdf.h:45
#define ASSERT(a)
Definition: mode.c:44
#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
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
#define _Must_inspect_result_
Definition: ms_sal.h:558
POBJECT_TYPE IoFileObjectType
Definition: iomgr.c:36
FxLibraryGlobalsType FxLibraryGlobals
Definition: globals.cpp:95
#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
__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
static DRIVER_NOTIFICATION_CALLBACK_ROUTINE _PlugPlayNotification
#define DO_NOTHING()
Definition: mxgeneral.h:32
Definition: ps.c:97