Pnp Callbacks called Before AddDevice Completes

Hi All,
I have a KMDF function driver for a PCIe device which intercepts few of the PNP IRP and then forwards them to the lower stacks.

So in my EvtAddDevice I call


so that I can tell the framework which PNP Irps I am interested in and want to preprocess them. Using this I register a PnpPreProcessIrp handler.

At the end of the EvtAddDevice after calling WdfDeviceInitAssignWdmIrpPreprocessCallback I create a lock. This lock is acquired in the PnpPreProcessIrp function. What I am seeing is that

  1. The PnpPreProcessIrp function is getting called.
  2. The PnpPreProcessIrp calls WdfWaitLockAcquire to acquire the Lock that I created in AddDevice.
  3. The WdfWaitLockAcquire crashes, saying that the lock is null, which inturn means that AddDevice did not create the lock yet.

In other words, my code makes an implicit assumption that till I do not return from AddDevice, none of the PNP IRP that my driver registered for will be dispatched to the Driver.

Is this assumption valid?

In my case, I am not ready to handle the PNP IRPS until I return from AddDevice. What is the correct way to serialize this?


the OS will not send PNP IRPS down the stack until the entire stack is built and all AddDevice's have been invoked. Driver initiated pnp requests can potentially be sent though, your driver or the ones below you are the only ones who could that. Which specific pnp minor code are you seeing before AddDevice has returned?

Are you querying for a pnp interface in your AddDevice routine?

As an aside, one problem is that you're using a wait lock... when your Preprocess Callback can be invoked at IRQL DISPATCH_LEVEL.

Thanks Peter. Right on both the points. I think I know what the problem is. Will incorporate your feedback as well.