Setting Device Power States
PowerSetPower in Listing 10.3 handles Set device Power IRPs.
If the device must be powered up, the power state must be changed after all the lower drivers have processed the Set device Power IRP. As before, a completion routine, OnCompleteIncreaseDevicePower, is set and the lower drivers are called. OnCompleteIncreaseDevicePower shown in Listing 10.6 eventually calls SetPowerState to change the device power state.
If the device must be powered down, PowerSetPower can simply call SetPowerState and pass the IRP to the lower drivers.
Listing 10.6 OnCompleteIncreaseDevicePower routine
NTSTATUS OnCompleteIncreaseDevicePower(IN PDEVICE_OBJECT fdo, IN PIRP Irp, IN PVOID context) {
PWDM2_DEVICE_EXTENSION dx = (PWDM2_DEVICE_EXTENSION)fdo->DeviceExtension;
if (Irp->PendingReturned) IoMarkIrpPending(Irp);
NTSTATUS status = Irp->IoStatus.Status;
DebugPrint("OnCompleteIncreaseDevicePower %x",status);
if( !NT_SUCCESS(status)) return status;
PIO_STACK_LOCATION IrpStack = IoGetCurrentIrpStackLocation(Irp);
POWER_STATE PowerState = IrpStack->Parameters.Power.State; SetPowerState(dx.PowerState.DeviceState);
PoStartNextPowerIrp(Irp);
return status;
}