⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 powersleepdevice.cpp

📁 usb wdm开发的电源管理 电源处于休眠状态的例程。
💻 CPP
字号:
// PowerSleepDevice.cpp
// Implementation of PowerSleepDevice device class
//
// Generated by DriverWizard version DriverStudio 2.6.0 (Build 336)
// Requires Compuware's DriverWorks classes
//

#pragma warning(disable:4065) // Allow switch statement with no cases
		  
#include <vdw.h>
#include "..\PowerSleepDeviceinterface.h"

#include "PowerSleep.h"
#include "PowerSleepDevice.h"
#include "..\PowerSleepioctl.h"

#pragma hdrstop("PowerSleep.pch")

extern KTrace t;			// Global driver trace object	

GUID PowerSleepDevice_Guid = PowerSleepDevice_CLASS_GUID;

////////////////////////////////////////////////////////////////////////
//  PowerSleepDevice::PowerSleepDevice
//
//	Routine Description:
//		This is the constructor for the Functional Device Object, or FDO.
//		It is derived from KPnpDevice, which builds in automatic
//	    dispatching of subfunctions of IRP_MJ_POWER and IRP_MJ_PNP to
//		virtual member functions.
//
//	Parameters:
//		Pdo - Physical Device Object - this is a pointer to a system
//			device object that represents the physical device.
//
//		Unit - Unit number. This is a number to append to the device's
//			base device name to form the Logical Device Object's name
//
//	Return Value:
//		None   
//
//	Comments:
//		The object being constructed contains a data member (m_Lower) of type
//		KPnpLowerDevice. By initializing it, the driver binds the FDO to the
//		PDO and creates an interface to the upper edge of the system class driver.
//

PowerSleepDevice::PowerSleepDevice(PDEVICE_OBJECT Pdo, ULONG Unit) :
	KPnpDevice(Pdo, &PowerSleepDevice_Guid)
{
	t << "PowerSleepDevice (constructor)\n";


	// Check constructor status
    if ( ! NT_SUCCESS(m_ConstructorStatus) )
	{
	    return;
	}

	// Remember our unit number
	m_Unit = Unit;

	// Initialize the lower device
	m_Lower.Initialize(this, Pdo);

    // Inform the base class of the lower edge device object
	SetLowerDevice(&m_Lower);

	// Initialize the PnP Policy settings to the "standard" policy
	SetPnpPolicy();

// TODO:	Customize the PnP Policy for this device by setting
//			flags in m_Policies.

	// Initialize the Power Policy settings to the "standard" policy
	SetPowerPolicy();

	// Adjust the standard Power Policy.The standard policy
	// requests the device to power ON only when a new IO
	// request is received.For this sample device driver,
	// the policy will be adjusted to power up the device
	// when the system is powered ON.
	m_PowerPolicies.m_GeneralPolicy.m_PowerUpOnS0 = TRUE;

}

PowerSleepDevice::~PowerSleepDevice()
{
	t << "~PowerSleepDevice() (destructor)\n";
}

NTSTATUS PowerSleepDevice::DefaultPnp(KIrp I) 
{
	I.ForceReuseOfCurrentStackLocationInCalldown();
	return m_Lower.PnpCall(this, I);
}

NTSTATUS PowerSleepDevice::DefaultPower(KIrp I) 
{
	t << "DefaultPower\n";

	I.IndicatePowerIrpProcessed();
	I.CopyParametersDown();
	return m_Lower.PnpPowerCall(this, I);
}

NTSTATUS PowerSleepDevice::SystemControl(KIrp I) 
{
	I.ForceReuseOfCurrentStackLocationInCalldown();
	return m_Lower.PnpCall(this, I);
}

NTSTATUS PowerSleepDevice::OnStartDevice(KIrp I)
{
	t << "OnStartDevice\n";

	return STATUS_SUCCESS;
}

NTSTATUS PowerSleepDevice::OnStopDevice(KIrp I)
{
	t << "OnStopDevice\n";

	return STATUS_SUCCESS;
}

NTSTATUS PowerSleepDevice::OnRemoveDevice(KIrp I)
{
	t << "OnRemoveDevice\n";

	return STATUS_SUCCESS;
}

NTSTATUS PowerSleepDevice::Create(KIrp I)
{
	NTSTATUS status;

	t << "Create\n";

	status = I.PnpComplete(this, STATUS_SUCCESS, IO_NO_INCREMENT);

	return status;
}

NTSTATUS PowerSleepDevice::Close(KIrp I)
{
	NTSTATUS status;

	t << "Close\n";

	status = I.PnpComplete(this, STATUS_SUCCESS, IO_NO_INCREMENT);

    return status;
}

NTSTATUS PowerSleepDevice::DeviceControl(KIrp I) 
{
	NTSTATUS status;

	switch (I.IoctlCode())
	{
		case POWERSLEEP_IOCTL_800:
			t << "POWERSLEEP_IOCTL_800_Handler\n";
			I.Information() = 0;
			status = STATUS_SUCCESS;
			break;

		default:
			// Unrecognized IOCTL request
			status = STATUS_INVALID_PARAMETER;
			break;
	}

	if (status == STATUS_PENDING)
	{
		return status;
	}
	else
	{
		return I.PnpComplete(this, status);
	}
}

///////////////////////////////////////////////////////////////////
// DetermineNewDevicePowerState
//
// This call is made by the framework to obtain the DEVICE_POWER_STATE
// that this device should transition to in response to POWER IRPs.  When
// a SYSTEM POWER IRP is received to change system power, this call is made 
// by the framework with the system's requested SYSTEM_POWER_STATE.  The 
// default base class implementation in KPnpDevice will use the DEVICE_CAPABILITIES
// structure reported from the bus driver for the hardware.  For most real
// devices, this structure will contain the correct DEVICE_POWER_STATE's.
// However, since this sample device driver does not control hardware, the
// system reports all DEVICE_POWER_STATE's to be PowerDeviceD1.  In order to
// demonstrate the framework's behavior for different power states, this
// virtual function has been overrided so that many power state transitions
// for the device can be supported.
//
// Most device drivers will not need to implement this function.
//
DEVICE_POWER_STATE PowerSleepDevice::DetermineNewDevicePowerState(SYSTEM_POWER_STATE SystemPowerState)
{
	DEVICE_POWER_STATE DevicePowerState;
    t << "DetermineNewDevicePowerState\n";

// The system is transitioning power to SystemPowerState.  We return
// the matching device power state for the system power state.  The
// base class KPnpDevice::Power member will handle requesting device 
// POWER IRP's (if required) to change device power based on these states.
	switch(SystemPowerState)
	{
	case PowerSystemWorking:
	// This is the fully ON power state.  If the device is not already in the
	// fully ON state, the base class will handle requesting a DEVICE
	// POWER IRP to put the device in the fully ON state.
		DevicePowerState = PowerDeviceD0;
		break;

	case PowerSystemSleeping1:
		DevicePowerState = PowerDeviceD1;
		break;

	case PowerSystemSleeping2:
		DevicePowerState = PowerDeviceD2;
		break;

	case PowerSystemSleeping3:
		DevicePowerState = PowerDeviceD2;
		break;

	case PowerSystemHibernate:
		DevicePowerState = PowerDeviceD3;
		break;

	case PowerSystemShutdown:
		DevicePowerState = PowerDeviceD3;
		break;

	default:
		break;
	}

	return DevicePowerState;
}

////////////////////////////////////////////////////////////////////////
//  PowerSleepDevice::OnDevicePowerUp
//
//	Routine Description:
//		Handler for IRP_MJ_POWER with minor function IRP_MN_SET_POWER
//		for a request to go to power on state from low power state
//
//	Parameters:
//		I - IRP containing POWER request
//
//	Return Value:
//		NTSTATUS - Status code indicating success or failure
//
//	Comments:
//		This routine implements the OnDevicePowerUp function.
//		This function was called by the framework from the completion
//		routine of the IRP_MJ_POWER dispatch handler in KPnpDevice.
//		The bus driver has completed the IRP and this driver can now
//		access the hardware device.  
//		This routine runs at dispatch level.
//	

NTSTATUS PowerSleepDevice::OnDevicePowerUp(KIrp I)
{
	NTSTATUS status = STATUS_SUCCESS;

	t << "OnDevicePowerUp\n";

// TODO:	Service the device.
//			Restore any context to the hardware device that
//			was saved during the handling of a power down request.
//			See the OnDeviceSleep function.
//			Do NOT complete this IRP.
//
	switch ( I.PowerStateType() ) 
	{
 	case SystemPowerState:
		t << "SystemPowerState " << SystemPowerStateName(I.PowerStateSetting().SystemState) << "\n" ;
		break;

 	case DevicePowerState:
		t << "DevicePowerState " << DevicePowerStateName(I.PowerStateSetting().DeviceState) << "\n" ;
		break;
	}
	return status;
}

////////////////////////////////////////////////////////////////////////
//  PowerSleepDevice::OnDeviceSleep
//
//	Routine Description:
//		Handler for IRP_MJ_POWER with minor function IRP_MN_SET_POWER
//		for a request to go to a low power state from a high power state
//
//	Parameters:
//		I - IRP containing POWER request
//
//	Return Value:
//		NTSTATUS - Status code indicating success or failure
//
//	Comments:
//		This routine implements the OnDeviceSleep function.
//		This function was called by the framework from the IRP_MJ_POWER 
//		dispatch handler in KPnpDevice prior to forwarding to the PDO.
//		The hardware has yet to be powered down and this driver can now
//		access the hardware device.  
//		This routine runs at passive level.
//	

NTSTATUS PowerSleepDevice::OnDeviceSleep(KIrp I)
{
	NTSTATUS status = STATUS_SUCCESS;

	t << "OnDeviceSleep\n";

// TODO:	Service the device.
//			Save any context to the hardware device that will be required 
//			during a power up request. See the OnDevicePowerUp function.
//			Do NOT complete this IRP.  The base class handles forwarding
//			this IRP to the PDO.
//
	switch ( I.PowerStateType() ) 
	{
 	case SystemPowerState:
		t << "SystemPowerState " << SystemPowerStateName(I.PowerStateSetting().SystemState) << "\n" ;
		break;

 	case DevicePowerState:
		t << "DevicePowerState " << DevicePowerStateName(I.PowerStateSetting().DeviceState) << "\n" ;
		break;
	}
	return status;
}

PCHAR DevicePowerStateName(DEVICE_POWER_STATE ps)
{
	static PCHAR PowerStates[] = {
		"PowerDeviceUnspecified",
		"PowerDeviceD0",
		"PowerDeviceD1",
		"PowerDeviceD2",
		"PowerDeviceD3"                  
	};

	if (ps > PowerDeviceD3) 
		return "<undefined power state>";
	else
		return PowerStates[ps];
}

PCHAR SystemPowerStateName(SYSTEM_POWER_STATE ps)
{
	static PCHAR PowerStates[] = {
		"PowerSystemUnspecified",
		"PowerSystemWorking",
		"PowerSystemSleeping1",
		"PowerSystemSleeping2",
		"PowerSystemSleeping3",
		"PowerSystemHibernate",
		"PowerSystemShutdown"
	};

	if (ps > PowerSystemShutdown) 
		return "<undefined power state>";
	else
		return PowerStates[ps];
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -