5.2.1 Device Driver’s Initialization

The DriverEntry routine of an NT device driver should initialize in the following stages:

  1. Allocate memory to hold the hardware configuration information necessary to communicate with its device(s).

  2. Gather hardware configuration information for the physical device(s) from the registry, from the I/O bus(es) in the machine, and/or by interrogating the device on the bus; otherwise, the driver must supply a set of default hardware configuration values for its device(s).

  3. Set the driver’s Dispatch, StartIo (if any), and Unload (if any) entry points in the given driver object.

  4. Attempt to claim the hardware resources that the driver uses to carry out device I/O operations, such as device memory, bus-relative interrupt vector, and so forth, in the registry.

  5. If the driver can claim the hardware resources it needs in the registry, initialize the physical device(s) and set up the NT objects and any other resources, such as spin locks, that the driver will use to process device I/O requests.

  6. If the driver succeeds in initializing its device(s) and setting up all the resources it needs to process IRPs to its device(s), free the memory it allocated to hold configuration information, or possibly call IoRegisterDriverReinitialization passing a Context pointer to the configuration information, and return STATUS_SUCCESS.

    Otherwise, free any objects and system resources it allocated, including the memory it used to hold configuration information, and return an appropriate NTSTATUS error.

    Every NT device driver writer should consider the following guidelines when implementing a DriverEntry routine: