Windows Kernel Power Management API Reference

This section provides detailed documentation for the Windows Kernel Power Management APIs. These APIs allow drivers and system components to interact with the system's power management framework, enabling efficient energy usage and responsiveness.

Key Concepts

Core Functions and Structures

Driver-Level Power Management

Drivers typically handle power management through IRP_MJ_POWER requests. The framework dispatches these requests to the driver's dispatch routine.

Function/Structure Description
PoCallDriver Allows a driver to send a power IRP to the next-lower driver in the stack.
PoStartNextPowerIrp Notifies the power manager that a driver has completed processing a power IRP and is ready for the next one.
PoRegisterDeviceForIdleDetection Registers a device with the system for idle detection, allowing it to enter low-power states when inactive.
PoSetPowerState Informs the power manager of a device's new power state.
PoSetRundownPowerState Sets the device's power state for the rundown operation.

System-Level Power Management

These functions are primarily used by system components to control system power policy and manage wake events.

Function/Structure Description
PoRequestPowerIrp Requests a power IRP to be sent to a specified device.
PoSetSystemState Sets the system's power state. Deprecated in favor of PoSetSystemPowerState.
PoSetSystemPowerState Sets the system's power state (e.g., S1, S3, S5).
PoCreateThermalZone Creates a thermal zone object used for managing device temperature.
PoRegisterThermalZone Registers a driver-created thermal zone with the system.

Key IRP Structures

POWER_ACTION

Represents the action to be taken on the system or device.

Members: PowerState (SYSTEM_POWER_STATE, DEVICE_POWER_STATE)
Action (POWER_ACTION)

IO_STACK_LOCATION (Parameters for IRP_MJ_POWER)

Contains the parameters for a power IRP dispatched to a driver.

Members: MinorFunction (e.g., IRP_MN_SET_POWER, IRP_MN_QUERY_POWER)
Parameters.Power.State (SYSTEM_POWER_STATE or DEVICE_POWER_STATE)
Parameters.Power.Type (DeviceUsage or SystemUsage)

Example Snippets

Handling IRP_MN_SET_POWER


NTSTATUS MyPowerDispatch(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
    PIO_STACK_LOCATION irpSp;
    NTSTATUS status;

    irpSp = IoGetCurrentIrpStackLocation(Irp);

    switch (irpSp->MinorFunction) {
        case IRP_MN_SET_POWER:
            // Handle power state change requests
            status = HandleSetPower(DeviceObject, Irp, irpSp);
            break;
        case IRP_MN_QUERY_POWER:
            // Handle power state query requests
            status = HandleQueryPower(DeviceObject, Irp, irpSp);
            break;
        // ... other minor functions
        default:
            status = STATUS_INVALID_DEVICE_REQUEST;
            break;
    }

    return status;
}
            

Important Notes

Always ensure that power IRPs are passed down the device stack using PoCallDriver after your driver has completed its processing. Failure to do so can lead to system instability.

Proper handling of wake events is crucial for devices that support them. Implement wake-enable and wake-disable routines correctly.