Are you trying to synchronize MiniportHalt with processing I/O control calls
that you receive on a device object created using NdisMRegisterDevice? If
so, then the rules are similar to using remove locks in standard WDM
drivers. So just use the WDM remove lock. Long story short:
typedef struct _YOUR_ADAPTER_CONTEXT {
…
IO_REMOVE_LOCK RemoveLock;
…
} YOUR_ADAPTER_CONTEXT, *PYOUR_ADAPTER_CONTEXT;
//
// Dispatch routine for device object registered using NdisMRegisterDevice
//
NTSTATUS
DispatchControl(
PDEVICE_OBJECT DeviceObject,
PIRP Irp)
{
NTSTATUS Status;
PYOUR_ADAPTER_CONTEXT AdapterContext;
AdapterContext = FindAdapterContext(…);
Status = IoAcquireRemoveLock(&AdapterContext->RemoveLock, Irp);
if (!NT_SUCCESS(Status)) {
// MiniportHalt has started, and is blocked in a call to
// IoReleaseRemoveLockAndWait.
Irp->IoStatus.Status = Status;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return Status;
}
… process IRP (possibly async) …
IoReleaseRemoveLock(&AdapterContext->RemoveLock, Irp);
ReleaseAdapterContext(AdapterContext);
Status = …;
Irp->IoStatus.Status = Status;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return Status;
}
MiniportInitialize(…)
{
IoInitializeRemoveLock(&AdapterContext->RemoveLock, …);
}
MiniportHalt(…)
{
Status = IoAcquireRemoveLock(&AdapterContext->RemoveLock, NULL);
ASSERT(NT_SUCCESS(Status));
IoReleaseRemoveLockAndWait(&AdapterContext->RemoveLock, NULL);
… other stuff …
… at this point, it is impossible to receive further I/O controls
…
}
The IoReleaseRemoveLockAndWait function marks the remove lock as “removed –
do not allow IoAcquireRemoveLock to succeed”, then blocks the calling thread
until all calls to IoAcquireRemoveLock are balanced by calls to
IoReleaseRemoveLock.
That takes care of synchronizing those two paths, so you won’t crash (at
least not due to this). However, if you are concerned about being able to
unload your device driver when the user-mode app still has a handle open to
your standalone device object, then you may have some difficulties. I’m not
sure if/how NDIS properly supports query remove on the standalone device
(the NdisMRegisterDevice device, not the miniport device). The docs on
NdisMRegisterDevice aren’t too clear on that.
– arlie
-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of S?ren Dreijer
Sent: Wednesday, March 15, 2006 10:19 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] Deregistering a device object
In an NDIS miniport driver, what should one do when MiniportHalt() is called
while a user-mode application still uses its handle to a device object
created by the driver?
According to MSDN “When NdisMRegisterDevice is called, there cannot be any
outstanding user-mode open operations on the device object being deleted. If
there are, a system error occurs.”
Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256
To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer