// Power request handler Smart_Card driver
// Copyright (C) 1999, 2000 by Walter Oney
// All rights reserved
#include "stddcls.h"
#include "driver.h"
// TODO: all functions in this module are in the locked code segment even though the
// AddDevice function sets DO_POWER_PAGABLE. If you *know* that you'll never need a
// non-paged handler, you could move the DispatchPower and DefaultPowerHandler functions
// to PAGEDCODE.
NTSTATUS DefaultPowerHandler(PDEVICE_EXTENSION pdx, IN PIRP Irp);
enum POWSTATE {
InitialState = 0, // initial state of FSM
SysPowerUpPending, // system power-up IRP forwarded
SubPowerUpPending, // waiting for nested device power up to finish
SubPowerDownPending, // waiting from device to power down before forwarding system power-down IRP
SysPowerDownPending, // waiting for system power-down IRP to finish
DevPowerUpPending, // waiting for device power-up IRP
DevPowerDownPending, // waiting for device power-down IRP
ContextSavePending, // context save is underway
ContextRestorePending, // context restore is underway
DevQueryUpPending, // device query for power-up pending
DevQueryDownPending, // device query for power-down pending
QueueStallPending, // waiting for device to be idle
PassiveCompletePending, // waiting to complete main IRP at PASSIVE_LEVEL
FinalState, // final state of FSM
NUMPOWSTATES,
};
enum POWEVENT {
NewIrp = 0, // new query/set IRP
MainIrpComplete, // the main IRP has finished
AsyncNotify, // some other event has occurred
NUMPOWEVENTS,
};
typedef struct _POWCONTEXT {
LONG id; // unique sequence number for this IRP
LONG eventcount; // number of events generated for this IRP
PDEVICE_EXTENSION pdx; // our own device extension
PIRP irp; // the IRP we're processing
enum POWSTATE state; // current state of FSM
NTSTATUS status; // completion status for main IRP
DEVICE_POWER_STATE devstate; // device power state to use
DEVICE_POWER_STATE oldpower; // previous device power state
UCHAR MinorFunction; // minor function to use in requested power IRP
} POWCONTEXT, *PPOWCONTEXT;
NTSTATUS HandlePowerEvent(PPOWCONTEXT ctx, enum POWEVENT event);
static LONG ctxcount = 0; // counter for POWCONTEXT structures
// #define VERBOSETRACE // for debugging HandlePowerEvent
#if DBG
#ifdef VERBOSETRACE
#define POWTRACE(x) DbgPrint x
#else
#define POWTRACE(x)
#endif
static char* fcnname[] = {
"IRP_MN_WAIT_WAKE",
"IRP_MN_POWER_SEQUENCE",
"IRP_MN_SET_POWER",
"IRP_MN_QUERY_POWER",
};
static char* sysstate[] = {
"PowerSystemUnspecified",
"PowerSystemWorking",
"PowerSystemSleeping1",
"PowerSystemSleeping2",
"PowerSystemSleeping3",
"PowerSystemHibernate",
"PowerSystemShutdown",
"PowerSystemMaximum",
};
static char* devstate[] = {
"PowerDeviceUnspecified",
"PowerDeviceD0",
"PowerDeviceD1",
"PowerDeviceD2",
"PowerDeviceD3",
"PowerDeviceMaximum",
};
#else
#define POWTRACE(x)
#endif // DBG
///////////////////////////////////////////////////////////////////////////////
#pragma LOCKEDCODE
NTSTATUS DispatchPower(IN PDEVICE_OBJECT fdo, IN PIRP Irp)
{ // DispatchPower
PAGED_CODE();
PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
NTSTATUS status = IoAcquireRemoveLock(&pdx->RemoveLock, Irp);
if (!NT_SUCCESS(status))
{
PoStartNextPowerIrp(Irp);
return CompleteRequest(Irp, status);
}
PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(Irp);
ASSERT(stack->MajorFunction == IRP_MJ_POWER);
ULONG fcn = stack->MinorFunction;
if (fcn == IRP_MN_SET_POWER || fcn == IRP_MN_QUERY_POWER)
{ // handle set/query
#if DBG
KdPrint((DRIVERNAME " - POWER Request (%s)", fcnname[fcn]));
if (stack->Parameters.Power.Type == SystemPowerState)
KdPrint((", S-state = %s\n", sysstate[stack->Parameters.Power.State.SystemState]));
else
KdPrint((", D-state = %s\n", devstate[stack->Parameters.Power.State.DeviceState]));
#endif // DBG
// Create a context structure and launch the finite state machine that will process
// this IRP asynchronously. The initial call to HandlePowerEvent should return
// STATUS_PENDING. The FSM will eventually complete the IRP.
PPOWCONTEXT ctx = (PPOWCONTEXT) ExAllocatePool(NonPagedPool, sizeof(POWCONTEXT));
if (!ctx)
{
KdPrint((DRIVERNAME " - Can't allocate power context structure\n"));
PoStartNextPowerIrp(Irp);
status = CompleteRequest(Irp, STATUS_INSUFFICIENT_RESOURCES);
}
else
{ // process this IRP
RtlZeroMemory(ctx, sizeof(POWCONTEXT));
ctx->pdx = pdx;
ctx->irp = Irp;
ctx->id = InterlockedIncrement(&ctxcount);
status = HandlePowerEvent(ctx, NewIrp);
} // process this IRP
} // handle set/query
else
{ // handle other power request
KdPrint((DRIVERNAME " - POWER Request (%s)\n", fcn < arraysize(fcnname) ? fcnname[fcn] : "??"));
status = DefaultPowerHandler(pdx, Irp);
} // handle other power request
IoReleaseRemoveLock(&pdx->RemoveLock, Irp);
return status;
} // DispatchPower
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#pragma LOCKEDCODE
NTSTATUS DefaultPowerHandler(PDEVICE_EXTENSION pdx, IN PIRP Irp)
{ // DefaultPowerHandler
PoStartNextPowerIrp(Irp); // must be done while we own the IRP
IoSkipCurrentIrpStackLocation(Irp);
return PoCallDriver(pdx->LowerDeviceObject, Irp);
} // DefaultPowerHandler
///////////////////////////////////////////////////////////////////////////////
// GetLowestDevicePowerState returns the lowest device power state that is
// consistent with a given system power state and the current wakeup state of
// the device.
DEVICE_POWER_STATE GetLowestDevicePowerState(PDEVICE_EXTENSION pdx, SYSTEM_POWER_STATE sysstate)
{ // GetLowestDevicePowerState
DEVICE_POWER_STATE maxstate = pdx->devcaps.DeviceState[sysstate];
DEVICE_POWER_STATE minstate = PowerDeviceD3;
DEVICE_POWER_STATE dstate = minstate > maxstate ? minstate : maxstate;
// TODO choose a different dstate here if you want to
return dstate;
} // GetLowestDevicePowerState
///////////////////////////////////////////////////////////////////////////////
#pragma LOCKEDCODE
VOID SendAsyncNotification(PVOID context)
{ // SendAsyncNotification
HandlePowerEvent((PPOWCONTEXT) context, AsyncNotify);
} // SendAsyncNotification
///////////////////////////////////////////////////////////////////////////////
struct SDSP_CONTEXT {
PKEVENT pev; // event to signal when request complete
NTSTATUS status; // ending status
};
#pragma LOCKEDCODE
VOID SendDeviceSetPowerComplete(PDEVICE_OBJECT junk, UCHAR fcn, POWER_STATE state, SDSP_CONTEXT* context, PIO_STATUS_BLOCK pstatus)
{ // SendDeviceSetPowerComplete
context->status = pstatus->Status;
KeSetEvent(context->pev, EVENT_INCREMENT, FALSE);
} // SendDeviceSetPowerComplete
NTSTATUS SendDeviceSetPower(PDEVICE_EXTENSION pdx, DEVICE_POWER_STATE devpower, BOOLEAN wait /* = FALSE */)
{ // SendDeviceSetPower
POWER_STATE state;
state.DeviceState = devpower;
NTSTATUS status;
if (wait)
{ // synchronous operation
KEVENT event;
KeInitializeEvent(&event, NotificationEvent, FALSE);
SDSP_CONTEXT context = {&event};
status = PoRequestPowerIrp(pdx->Pdo, IRP_MN_SET_POWER, state,
(PREQUEST_POWER_COMPLETE) SendDeviceSetPowerComplete, &context, NULL);
if (status == STATUS_PENDING)
{
KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
status = context.status;
}
} // synchronous operation
else
status = PoRequestPowerIrp(pdx->Pdo, IRP_MN_SET_POW
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
智能卡的读写程序C++.rar (66个子文件)
Smart_Card
version.h 87B
Smart_Card.inf 2KB
objfre_wxp_x86
_objects.mac 1KB
i386
Control.obj 639B
SMART_CARD.pdb 59KB
ReadWrite.sbr 0B
DriverEntry.sbr 0B
stddcls.obj 293B
Power.sbr 0B
Control.sbr 0B
smart_card.sys 10KB
Smart_Card.bsc 641KB
ReadWrite.obj 5KB
DriverEntry.obj 6KB
stddcls.sbr 226KB
PlugPlay.sbr 0B
smart_card.NMS 12KB
vc70.idb 83KB
driver.res 916B
Smart_Card.pch 1.33MB
Power.obj 7KB
PlugPlay.obj 6KB
Smart_Card.dsp 9KB
Smart_Card.opt 49KB
objchk_wxp_x86
_objects.mac 1KB
i386
Control.obj 3KB
SMART_CARD.pdb 227KB
ReadWrite.sbr 0B
DriverEntry.sbr 0B
stddcls.obj 135KB
Power.sbr 0B
Control.sbr 0B
smart_card.sys 24KB
Smart_Card.bsc 649KB
ReadWrite.obj 17KB
DriverEntry.obj 16KB
stddcls.sbr 227KB
PlugPlay.sbr 0B
smart_card.NMS 331KB
vc70.idb 115KB
driver.res 916B
Smart_Card.pch 1.46MB
Power.obj 29KB
PlugPlay.obj 25KB
driver.rc 1KB
guids.h 339B
buildchk_wxp_x86.err 728B
PlugPlay.cpp 18KB
Driver.h 3KB
buildchk_wxp_x86.log 4KB
Ioctls.h 373B
Smart_Card.cat 54B
Smart_Card.ncb 89KB
makefile 252B
Power.cpp 37KB
stddcls.h 1KB
buildfre_wxp_x86.log 3KB
ReadWrite.cpp 12KB
DriverEntry.cpp 9KB
Smart_Card.dsw 545B
buildfre_wxp_x86.err 728B
Control.cpp 1KB
stddcls.cpp 150B
Smart_Card.plg 5KB
sources 223B
Smart_Card.dsp.sav 8KB
共 66 条
- 1
资源评论
- *微光*2013-04-16不是我想要的,只是最基本的示例
- 根号猫2014-09-19用VS 打开报错,不知道是怎么回事 也不能运行,没能帮到我
- hunanchenke2016-08-04下载了好久,简单的看了看,能够使用,谢谢。
baiyang4150
- 粉丝: 9
- 资源: 46
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功