ReactOS 0.4.15-dev-7998-gdb93cb1
fxiotargetremotekm.cpp
Go to the documentation of this file.
1/*++
2
3Copyright (c) Microsoft Corporation
4
5Module Name:
6
7 FxIoTargetRemoteKm.cpp
8
9Abstract:
10
11Author:
12
13Environment:
14
15 kernel mode only
16
17Revision History:
18
19--*/
20
21#include "../../fxtargetsshared.hpp"
22
23extern "C" {
24// #include "FxIoTargetRemoteKm.tmh"
25}
26
27#include <initguid.h>
28#include "wdmguid.h"
29
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
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
164VOID
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
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,
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
283 )
284{
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
unsigned char BOOLEAN
LONG NTSTATUS
Definition: precomp.h:26
#define IRP_MJ_PNP
Definition: cdrw_usr.h:52
__inline MdDriverObject GetDriverObject(VOID)
Definition: fxdriver.hpp:252
FxIoTargetRemoveComplete m_EvtRemoveComplete
_Must_inspect_result_ NTSTATUS Open(__in PWDF_IO_TARGET_OPEN_PARAMS OpenParams)
__inline WDFIOTARGET GetHandle(VOID)
FxIoTargetQueryRemove m_EvtQueryRemove
NTSTATUS GetTargetDeviceRelations(_Out_ BOOLEAN *Close)
VOID UnregisterForPnpNotification(_In_ MdTargetNotifyHandle Handle)
MdTargetNotifyHandle m_TargetNotifyHandle
VOID Close(__in FxIoTargetRemoteCloseReason Reason)
NTSTATUS RegisterForPnpNotification(VOID)
FxIoTargetRemoveCanceled m_EvtRemoveCanceled
NTSTATUS OpenTargetHandle(_In_ PWDF_IO_TARGET_OPEN_PARAMS OpenParams, _Inout_ FxIoTargetRemoveOpenParams *pParams)
static DRIVER_NOTIFICATION_CALLBACK_ROUTINE _PlugPlayNotification
MdDeviceObject m_TargetDevice
Definition: fxiotarget.hpp:910
MdDeviceObject m_TargetPdo
Definition: fxiotarget.hpp:918
FxDriver * m_Driver
Definition: fxiotarget.hpp:900
MdFileObject m_TargetFileObject
Definition: fxiotarget.hpp:923
CHECK_RETURN_IF_USER_MODE NTSTATUS SendIrpSynchronously(__in MdDeviceObject DeviceObject)
Definition: fxirpum.cpp:151
VOID SetStatus(__in NTSTATUS Status)
Definition: fxirpum.cpp:457
PIO_STACK_LOCATION GetNextIrpStackLocation(VOID)
Definition: fxirpum.cpp:387
MdIrp SetIrp(MdIrp irp)
Definition: fxirpkm.hpp:71
ULONG_PTR GetInformation()
Definition: fxirpum.cpp:513
PVOID __inline GetObjectHandle(VOID)
Definition: fxobject.hpp:603
__inline PFX_DRIVER_GLOBALS GetDriverGlobals(VOID)
Definition: fxobject.hpp:734
static __inline KIRQL MxGetCurrentIrql()
Definition: mxgeneralkm.h:86
Definition: _stack.h:55
#define __inout_opt
Definition: dbghelp.h:53
#define __in
Definition: dbghelp.h:35
#define TRACINGIOTARGET
Definition: dbgtrace.h:72
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
#define DISPATCH_LEVEL
Definition: env_spec_w32.h:696
DoTraceLevelMessage(pFxDriverGlobals, TRACE_LEVEL_VERBOSE, TRACINGPNP, "Enter, WDFDEVICE %p", Device)
PFX_DRIVER_GLOBALS pFxDriverGlobals
return pObject GetObjectHandle()
_Must_inspect_result_ BOOLEAN __inline FxIsEqualGuid(__in CONST GUID *Lhs, __in CONST GUID *Rhs)
Definition: fxglobals.h:977
PDEVICE_OBJECT pTopOfStack
@ FxIoTargetRemoteCloseReasonPlainClose
@ FxIoTargetRemoteCloseReasonQueryRemove
FxIrp * pIrp
FxIrp * irp
ULONG Handle
Definition: gdb_input.c:15
GLenum const GLfloat * params
Definition: glext.h:5645
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
POBJECT_TYPE IoFileObjectType
Definition: iomgr.c:36
if(dx< 0)
Definition: linetemp.h:194
#define ASSERT(a)
Definition: mode.c:44
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define _Inout_
Definition: ms_sal.h:378
#define _Must_inspect_result_
Definition: ms_sal.h:558
#define _In_
Definition: ms_sal.h:308
#define DO_NOTHING()
Definition: mxgeneral.h:32
#define KernelMode
Definition: asm.h:34
PDEVICE_OBJECT NTAPI IoGetRelatedDeviceObject(IN PFILE_OBJECT FileObject)
Definition: device.c:1539
PDEVICE_OBJECT NTAPI IoGetAttachedDeviceReference(PDEVICE_OBJECT DeviceObject)
Definition: device.c:1406
PIRP NTAPI IoAllocateIrp(IN CCHAR StackSize, IN BOOLEAN ChargeQuota)
Definition: irp.c:615
#define STATUS_NOT_SUPPORTED
Definition: ntstatus.h:423
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
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 NTAPI IoUnregisterPlugPlayNotification(_In_ PVOID NotificationEntry)
Definition: pnpnotify.c:479
FxLibraryGlobalsType FxLibraryGlobals
Definition: globals.cpp:95
@ Close
Definition: sacdrv.h:268
#define STATUS_SUCCESS
Definition: shellext.h:65
#define TRACE_LEVEL_WARNING
Definition: storswtr.h:28
#define TRACE_LEVEL_VERBOSE
Definition: storswtr.h:30
#define TRACE_LEVEL_ERROR
Definition: storswtr.h:27
PFN_WDF_IO_TARGET_QUERY_REMOVE m_Method
_Must_inspect_result_ NTSTATUS Invoke(__in WDFIOTARGET IoTarget)
VOID Invoke(__in WDFIOTARGET Target)
PFN_WDF_IO_TARGET_REMOVE_CANCELED m_Method
PFN_WDF_IO_TARGET_REMOVE_COMPLETE m_Method
VOID Invoke(__in WDFIOTARGET Target)
PFN_IO_UNREGISTER_PLUGPLAY_NOTIFICATION_EX IoUnregisterPlugPlayNotificationEx
Definition: fxglobals.h:740
PDEVICE_OBJECT Objects[1]
Definition: iotypes.h:2163
Definition: ps.c:97
uint32_t ULONG
Definition: typedefs.h:59
#define STATUS_NO_SUCH_DEVICE
Definition: udferr_usr.h:136
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
#define STDCALL
Definition: wdf.h:45
_Must_inspect_result_ _In_ WDFIOTARGET _In_ PWDF_IO_TARGET_OPEN_PARAMS OpenParams
Definition: wdfiotarget.h:401
FORCEINLINE VOID WDF_IO_TARGET_OPEN_PARAMS_INIT_REOPEN(_Out_ PWDF_IO_TARGET_OPEN_PARAMS Params)
Definition: wdfiotarget.h:340
_In_ PVOID NotificationStructure
Definition: iofuncs.h:1206
@ TargetDeviceRelation
Definition: iotypes.h:2156
struct _DEVICE_RELATIONS * PDEVICE_RELATIONS
#define IRP_MN_QUERY_DEVICE_RELATIONS
@ EventCategoryTargetDeviceChange
Definition: iotypes.h:1227
struct _TARGET_DEVICE_REMOVAL_NOTIFICATION * PTARGET_DEVICE_REMOVAL_NOTIFICATION
#define ObDereferenceObject
Definition: obfuncs.h:203