Книга: Writing Windows WDM Device Drivers
Plug and Play Messages
Plug and Play Messages
This section looks at the messages that a PnP driver receives. Its AddDevice routine is called when a device is added. After that, messages are sent using the PnP IRP. The PnP messages have the same major function code IRP_MJ_PNP, but each message uses a different minor function code. Each message has different parameters in the IRP.
A PnP driver must implement an AddDevice routine and handle various PnP IRPs. Table 8.2 is a list of all the minor function codes. The first eight of these minor codes are the most important. The other minor function codes are usually just handled by bus drivers.
Table 8.2 Plug and Play minor function codes
Common PnP IRPs | |
---|---|
IRP_MN_START_DEVICE |
Assign resources and start a device |
IRP_MN_QUERY_REMOVE_DEVICE |
Ask if a device can be removed |
IRP_MN_CANCEL_REMOVE_DEVICE |
Cancel a query remove request |
IRP_MN_REMOVE_DEVICE | Device has been unplugged or uninstalled Deallocate resources and remove a device |
IRP_MN_SURPRISE_REMOVAL (W2000 only) |
A user has unexpectedly unplugged a device |
IRP_MN_QUERY_STOP_DEVICE |
Ask if a device can be stopped |
IRP_MN_CANCEL_STOP_DEVICE |
Cancel a query stop request |
IRP_MN_STOP_DEVICE |
Stop a device for resource reallocation |
Unusual PnP IRPs | |
IRP_MN_QUERY_DEVICE_RELATIONS |
Ask for PDOs that have certain characteristics |
IRP_MN_QUERY_INTERFACE |
Let a driver export a direct-call interface |
IRP_MN_QUERY_CAPABILITIES |
Ask about device capabilities (e.g., whether it can be locked or ejected) |
IRP_MN_QUERY_RESOURCES |
Get a device's boot configuration resources |
IRP_MN_QUERY_RESOURCE_REQUIREMENTS |
Ask what resources a device requires |
IRP_MN_QUERY_DEVICE_TEXT |
Get a device's description or location string |
IRP_MN_FILTER_RESOURCE_REQUIREMENTS |
Let filter and function drivers filter a device's resource requirements |
IRP_MN_READ_CONFIG |
Read configuration space information |
IRP_MN_WRITE_CONFIG |
Set configuration space information |
IRP_MN_EJECT |
Eject the device from its slot |
IRP_MN_SET_LOCK |
Set device locking state |
IRP_MN_QUERY_ID |
Get hardware, compatible, and instance IDs for a device |
IRP_MN_QUERY_PNP_DEVICE_STATE |
Set bits in a device state bitmap |
IRP_MN_QUERY_BUS_INFORMATION |
Get type and instance number of parent bus |
IRP_MN_DEVICE_USAGE_NOTIFICATION |
Notify whether a device is in the path of a paging, hibernation, or crash dump file. |
IRP_MN_QUERY_LEGACY_BUS_INFORMATION |
Returns legacy bus information (W2000 only) |
Figure 8.3 shows the PnP states for a device and the messages that are sent to change state. The minor function code names are shortened in the figure (e.g., IRP_MN_START_DEVICE is shown as START_DEVICE). In this book, a PnP IRP with an IRP_MN_START_DEVICE minor function code is called a Start Device message. The other PnP function codes are given similar names.
When a device is added to the system, Windows finds the correct driver and calls its DriverEntry routine. Chapter 11 explains how Windows finds the correct drivers.
The PnP Manager then calls the driver's AddDevice routine to tell it that a device has been added. It is at this point that the driver makes its own device object, the Functional Device Object (FDO). However, the driver should not try to access its device hardware yet.
In due course, the driver receives an IRP_MN_START_DEVICE IRP that includes information about the resources the device has been assigned. It can then start talking properly to the device hardware.
If a device is about to be unplugged, Windows asks the driver if it is all right for the device to be removed using an IRP_MN_QUERY_REMOVE_DEVICE IRP. If the driver agrees, an IRP_MN_ REMOVE_DEVICE IRP is sent to remove the device. If the driver does not want its device removed (e.g., if it is in the middle of a long transfer) it fails the remove request. It is then sent an IRP_MN_CANCEL_REMOVE_DEVICE IRP to put it back in the started state.
If a user unexpectedly pulls out a device, the driver is sent an IRP_MN_REMOVE_DEVICE IRP in Windows 98 or an IRP_MN_SURPRISE_REMOVAL IRP in Windows 2000. You have to cope with interrupted transfers as well as you can.
The other main state change occurs when the PnP Manager wishes to reallocate some of the driver's resources. This might happen if a new device of some sort is plugged in, meaning that the resource assignments need to be juggled about. The PnP Manager asks to stop the driver temporarily while its resources are reassigned. Similarly, in response to remove requests, an IRP_MN_QUERY_STOP_DEVICE IRP asks if it is OK to stop your device. If it is, an IRP_MN_STOP_DEVICE IRP is issued to take the device into the stopped state. If not, an IRP_MN_ CANCEL_STOP_DEVICE IRP moves the device back into the started state. While stopped, the driver should not access its device. An IRP_MN_START_DEVICE IRP informs the driver of its new resources and starts the device again.
Figure 8.3 Plug and Play device states and messages
Note that a driver might receive a remove device message while in the stopped or awaiting resources states. These state changes are not shown in the diagram for the sake of clarity.
The next chapter looks in detail at how to handle these state changes. For the moment, I will look at device enumeration and then how layers of PnP drivers work together to form a device stack.
- Chapter 8 Plug and Play and Device Stacks
- Plug and Play Notification
- Разработка приложений баз данных InterBase на Borland Delphi
- Open Source Insight and Discussion
- Introduction to Microprocessors and Microcontrollers
- Chapter 6. Traversing of tables and chains
- Chapter 8. Saving and restoring large rule-sets
- Chapter 11. Iptables targets and jumps
- Chapter 5 Installing and Configuring VirtualCenter 2.0
- Chapter 16. Commercial products based on Linux, iptables and netfilter
- Appendix A. Detailed explanations of special commands
- Appendix B. Common problems and questions