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

📄 usb2com.power.cpp

📁 这是一个关于USB转接串口的驱动程序开发
💻 CPP
字号:
//********************************************************************
//	created:	11:7:2008   21:31
//	file:		usb2com.power.cpp
//	author:		tiamo
//	purpose:	power
//********************************************************************

#include "stdafx.h"

//
// power dispatch routine
//
NTSTATUS Usb2ComPower(__in PDEVICE_OBJECT DeviceObject,__in PIRP Irp)
{
	PUSB2COM_DEVICE_EXTENSION DevExt					= static_cast<PUSB2COM_DEVICE_EXTENSION>(DeviceObject->DeviceExtension);
	PIO_STACK_LOCATION IrpSp							= IoGetCurrentIrpStackLocation(Irp);
	NTSTATUS Status										= STATUS_SUCCESS;

	switch(IrpSp->MinorFunction)
	{
	case IRP_MN_SET_POWER:
		{
			if(IrpSp->Parameters.Power.Type == SystemPowerState)
			{
				POWER_STATE NewDevicePowerState;
				NewDevicePowerState.DeviceState			= DevExt->DeviceCapabilities.DeviceState[IrpSp->Parameters.Power.State.SystemState];
				if(DevExt->CurrentPowerState != NewDevicePowerState.DeviceState)
				{
					DevExt->SavedSystemPowerIrp			= Irp;
					Status								= PoRequestPowerIrp(DevExt->PhysicalDeviceObject,IRP_MN_SET_POWER,NewDevicePowerState,
																			&Usb2ComContinueSendSystemPowerRequest,DevExt,0);

					if(!NT_SUCCESS(Status))
					{
						DevExt->SavedSystemPowerIrp		= 0;
						Irp->IoStatus.Status			= Status;
						IoCompleteRequest(Irp,IO_NO_INCREMENT);
					}
				}
				else
				{
					IoCopyCurrentIrpStackLocationToNext(Irp);
					PoStartNextPowerIrp(Irp);
					Status								= PoCallDriver(DevExt->LowerDeviceObject,Irp);
				}
			}
			else if(IrpSp->Parameters.Power.Type == DevicePowerState)
			{
				IoCopyCurrentIrpStackLocationToNext(Irp);

				if(DevExt->CurrentPowerState < IrpSp->Parameters.Power.State.DeviceState)	
					IoSetCompletionRoutine(Irp,&Usb2ComDevicePowerUpComplete,DevExt,TRUE,TRUE,TRUE);
				else if(DevExt->CurrentPowerState > IrpSp->Parameters.Power.State.DeviceState)
					DevExt->CurrentPowerState			= IrpSp->Parameters.Power.State.DeviceState;

				PoStartNextPowerIrp(Irp);
				Status									= PoCallDriver(DevExt->LowerDeviceObject,Irp);
			}
			else
			{
				Status									= STATUS_INVALID_PARAMETER;
				Irp->IoStatus.Status					= STATUS_INVALID_PARAMETER;
				Irp->IoStatus.Information				= 0;
				IoCompleteRequest(Irp,IO_NO_INCREMENT);
			}
		}
		break;

	//case IRP_MN_WAIT_WAKE:
	//	break;

	default:
		IoCopyCurrentIrpStackLocationToNext(Irp);
		PoStartNextPowerIrp(Irp);
		Status											= PoCallDriver(DevExt->LowerDeviceObject,Irp);
		break;
	}

	return Status;
}

//
// device power up complete
//
NTSTATUS Usb2ComDevicePowerUpComplete(__in PDEVICE_OBJECT DeviceObject,__in PIRP Irp,__in PVOID Context)
{
	PUSB2COM_DEVICE_EXTENSION DevExt					= static_cast<PUSB2COM_DEVICE_EXTENSION>(Context);

	if(Irp->PendingReturned)
		IoMarkIrpPending(Irp);

	if(NT_SUCCESS(Irp->IoStatus.Status))
		DevExt->CurrentPowerState						= IoGetCurrentIrpStackLocation(Irp)->Parameters.Power.State.DeviceState;

	return STATUS_SUCCESS;
}

//
// continue send system power
//
VOID Usb2ComContinueSendSystemPowerRequest(__in PDEVICE_OBJECT DeviceObject,__in UCHAR Minor,__in POWER_STATE PowerState,
										   __in PVOID Context,__in PIO_STATUS_BLOCK IoStatus)
{
	PUSB2COM_DEVICE_EXTENSION DevExt					= static_cast<PUSB2COM_DEVICE_EXTENSION>(Context);
	PIRP Irp											= DevExt->SavedSystemPowerIrp;

	IoCopyCurrentIrpStackLocationToNext(Irp);
	PoStartNextPowerIrp(Irp);

	PoCallDriver(DevExt->LowerDeviceObject,Irp);
}

⌨️ 快捷键说明

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