I've been looking at the WDM book queueing example and it seems there
are two similar methods for making sure driver activity has ceased
before removing a device:
stallcount/WaitForCurrentIrp()
and:
IoAcquireRemoveLock()/IoReleaseRemoveLock()/IoReleaseRemoveLockAndWait()
The idea behind the first method is to prevent any new device I/O from
being initiated and wait for the current I/O to finish.
The idea behind the second method is to prevent any new IRPs from
being forwarded to a lower driver and wait for the current IRPs
forwarded to a lower driver to complete (so that the driver can't
finish the next IRP_MN_REMOVE_DEVICE it might receive until those IRPs
are completed).
The ideas are very similar. They both center around whether some
operation is in progress, whether the device is "busy". It seems like
with some code changes, a single method would work, one that simply
controls whether the device is currently busy and/or in the process of
removal and can wait until the device is no longer busy.