Writing Windows WDM Device Drivers

Supporting Plug and Play primarily means implementing an AddDevice routine and an IRP_MJ_PNP handler. This PnP IRP has eight minor function codes that most WDM drivers need to support.
IRP_MN_START_DEVICE ( Start Device)
IRP_MN_QUERY_REMOVE_DEVICE ( Query Remove)
IRP_MN_REMOVE_DEVICE ( Remove Device)
IRP_MN_CANCEL_REMOVE_DEVICE ( Cancel Remove)
IRP_MN_STOP_DEVICE ( Stop Device)
IRP_MN_QUERY_STOP_DEVICE ( Query Stop)
IRP_MN_CANCEL_STOP_DEVICE ( Stop Device)
IRP_MN_SURPRISE_REMOVAL ( Surprise Removal)
Looking at this list, it might not seem too complicated to handle Plug and Play in a driver. In fact, there are many things to get right. At a basic level this means:
Coping with adding and removing devices
Getting resource assignments
Handling Query Stop and Query Remove messages
Handling Stop messages
Handling Surprise Removal messages
However, as will be shown, it soon becomes apparent that you must also do the following tasks:
Allow only I/O requests while the device is started
Not allow a device to be removed while there are any open handles
Queue I/O requests while the device is not started
Wait until any I/O requests have completed before handling remove requests
Process Start Device messages after lower devices have started
Pass unsupported IRPs down the stack
The rest of this chapter will look at the theory behind all the important PnP messages. The Wdm2 example driver shows how to implement most of the related tasks listed above.