📄 pnp.cpp
字号:
/******************************************************************************
* * File Name:
* * PlugPlay.cpp
* * Description:
* * Plug 'n' Play handler functions
* * Revision History:
* * 03-2-02 : PCI9052Demo v3.20
*******************************************************************************/
#define INITGUID // Initialize WDMIO_GUID in this module
#include "PCI9052Demo.h"
#pragma code_seg("PAGE") // start PAGE section
/******************************************************************************
*
* Function : AddDevice
*
* Description: Add a new functional device object for a physical one
*
******************************************************************************/
NTSTATUS AddDevice(IN PDRIVER_OBJECT pDriverObject,
IN PDEVICE_OBJECT pdo)
{ // AddDevice
NTSTATUS status;
PDEVICE_OBJECT fdo;
PDEVICE_EXTENSION pdx;
DebugPrint("AddDevice Start.");
//Create a functional device object to represent the hardware we're managing.
//创建设备
status = IoCreateDevice(pDriverObject, sizeof(DEVICE_EXTENSION), NULL,
FILE_DEVICE_UNKNOWN, 0, FALSE, &fdo); //DeviceCharacteristics be setted 0
if (!NT_SUCCESS(status))
{ // can't create device object
DebugPrint("PCI9052Demo: - IoCreateDevice failed - %x", status);
return status;
} // can't create device object
pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
// From this point forward, any error will have side effects that need to
// be cleaned up. Using a try-finally block allows us to modify the program
// easily without losing track of the side effects.
__try
{ // Finish initialization
//变量初始化
pdx->pDeviceObject = fdo;
pdx->pPhysicalDeviceObject = pdo;
pdx->UsageCount = 1; // Locked until RemoveDevice
KeInitializeEvent(&pdx->StoppingEvent,NotificationEvent,FALSE);
pdx->bStopping = FALSE;
pdx->GotResource=FALSE;
// Indicate the I/O Manager buffer management method
//该变量初始化后,不能更改
fdo->Flags |= DO_BUFFERED_IO;
// Initialize DPC object
IoInitializeDpcRequest(fdo, DpcForIsr);
// Register a device interface
status = IoRegisterDeviceInterface(pdo,&GUID_INTERFACE_PCI9052Demo,
NULL,&pdx->InterfaceName);
if (!NT_SUCCESS(status))
{ // unable to register interface
DebugPrint("PCI9052Demo: - IoRegisterDeviceInterface failed - %x.", status);
__leave;
} // unable to register interface
// Link our device object into the stack leading to the PDO
pdx->pLowerDeviceObject = IoAttachDeviceToDeviceStack(fdo, pdo);
if (!pdx->pLowerDeviceObject)
{ // can't attach device
DebugPrint("PCI9052Demo: - IoAttachDeviceToDeviceStack failed.");
status = STATUS_DEVICE_REMOVED;
__leave;
} // can't attach device
//设置设备接口状态
IoSetDeviceInterfaceState(&pdx->InterfaceName, TRUE);
// Indicate that our initial power state is D0 (fully on). Also indicate that
// we have a pagable power handler (otherwise, we'll never get idle shutdown
// messages!)
pdx->SystemPower = PowerSystemWorking;
pdx->DevicePower = PowerDeviceD0;
fdo->Flags |= DO_POWER_PAGABLE;
POWER_STATE state;
state.DeviceState = PowerDeviceD0;
PoSetPowerState(fdo, DevicePowerState, state);
pdx->bSetWaitEvent = FALSE;
pdx->pWaitEvent = NULL;
DebugPrint("Adddevice End.");
// Clear the "initializing" flag so that we can get IRPs
fdo->Flags &= ~DO_DEVICE_INITIALIZING;
} // finish initialization
__finally
{ // cleanup side effects
if (!NT_SUCCESS(status))
{ // need to cleanup
if (pdx->InterfaceName.Buffer)
RtlFreeUnicodeString(&pdx->InterfaceName);
if (pdx->pLowerDeviceObject)
IoDetachDevice(pdx->pLowerDeviceObject);
IoDeleteDevice(fdo);
} // need to cleanup
} // cleanup side effects
return STATUS_SUCCESS;
} // AddDevice
/******************************************************************************
*
* Function : DispatchPnp
*
* Description: Handle PnP requests
*
******************************************************************************/
NTSTATUS DispatchPnp(IN PDEVICE_OBJECT fdo,
IN PIRP pIrp)
{ //DispatchPnp
NTSTATUS status;
PIO_STACK_LOCATION stack;
PDEVICE_EXTENSION pdx;
DebugPrint("Pnp Start.");
pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
if (!LockDevice(pdx))
{
return CompleteRequestInfo(pIrp,STATUS_DELETE_PENDING,0);
}
stack = IoGetCurrentIrpStackLocation(pIrp);
// Check minor code
//如果没有需要处理的,默认处理
switch (stack->MinorFunction)
{
case IRP_MN_START_DEVICE: //必须实现
DebugPrint("IRP_MN_START_DEVICE.");
status = PnpStartDevice(fdo,pIrp);
break;
case IRP_MN_STOP_DEVICE:
DebugPrint("IRP_MN_STOP_DEVICE.");
status = PnpStopDevice(fdo,pIrp);
break;
case IRP_MN_REMOVE_DEVICE:
DebugPrint("IRP_MN_REMOVE_DEVICE.");
status = PnpRemoveDevice(fdo,pIrp);
return status;
break;
case IRP_MN_QUERY_REMOVE_DEVICE:
DebugPrint("IRP_MN_QUERY_REMOVE_DEVICE.");
status = DefaultPnpHandler(fdo,pIrp);
break;
case IRP_MN_CANCEL_REMOVE_DEVICE:
DebugPrint("IRP_MN_CANCEL_REMOVE_DEVICE.");
status = DefaultPnpHandler(fdo,pIrp);
break;
case IRP_MN_QUERY_STOP_DEVICE:
DebugPrint("IRP_MN_QUERY_STOP_DEVICE.");
status = DefaultPnpHandler(fdo,pIrp);
break;
case IRP_MN_CANCEL_STOP_DEVICE:
DebugPrint("IRP_MN_CANCEL_STOP_DEVICE.");
status = DefaultPnpHandler(fdo,pIrp);
break;
case IRP_MN_QUERY_DEVICE_RELATIONS:
DebugPrint("IRP_MN_QUERY_DEVICE_RELATIONS.");
status = DefaultPnpHandler(fdo,pIrp);
break;
case IRP_MN_QUERY_INTERFACE:
DebugPrint("IRP_MN_QUERY_INTERFACE.");
status = DefaultPnpHandler(fdo,pIrp);
break;
case IRP_MN_QUERY_CAPABILITIES:
DebugPrint("IRP_MN_QUERY_CAPABILITIES.");
status = DefaultPnpHandler(fdo,pIrp);
break;
case IRP_MN_QUERY_RESOURCES:
DebugPrint("IRP_MN_QUERY_RESOURCES.");
status = DefaultPnpHandler(fdo,pIrp);
break;
case IRP_MN_QUERY_RESOURCE_REQUIREMENTS:
DebugPrint("IRP_MN_QUERY_RESOURCE_REQUIREMENTS.");
status = DefaultPnpHandler(fdo,pIrp);
break;
case IRP_MN_QUERY_DEVICE_TEXT:
DebugPrint("IRP_MN_QUERY_DEVICE_TEXT.");
status = DefaultPnpHandler(fdo,pIrp);
break;
case IRP_MN_FILTER_RESOURCE_REQUIREMENTS:
DebugPrint("IRP_MN_FILTER_RESOURCE_REQUIREMENTS.");
status = DefaultPnpHandler(fdo,pIrp);
break;
case IRP_MN_READ_CONFIG:
DebugPrint("IRP_MN_READ_CONFIG.");
status = DefaultPnpHandler(fdo,pIrp);
break;
case IRP_MN_WRITE_CONFIG:
DebugPrint("IRP_MN_WRITE_CONFIG.");
status = DefaultPnpHandler(fdo,pIrp);
break;
case IRP_MN_EJECT:
DebugPrint("IRP_MN_EJECT.");
status = DefaultPnpHandler(fdo,pIrp);
break;
case IRP_MN_SET_LOCK:
DebugPrint("IRP_MN_SET_LOCK.");
status = DefaultPnpHandler(fdo,pIrp);
break;
case IRP_MN_QUERY_ID:
DebugPrint("IRP_MN_QUERY_ID.");
status = DefaultPnpHandler(fdo,pIrp);
break;
case IRP_MN_QUERY_PNP_DEVICE_STATE:
DebugPrint("IRP_MN_QUERY_PNP_DEVICE_STATE.");
status = DefaultPnpHandler(fdo,pIrp);
break;
case IRP_MN_QUERY_BUS_INFORMATION:
DebugPrint("IRP_MN_QUERY_BUS_INFORMATION.");
status = DefaultPnpHandler(fdo,pIrp);
break;
case IRP_MN_DEVICE_USAGE_NOTIFICATION:
DebugPrint("IRP_MN_DEVICE_USAGE_NOTIFICATION.");
status = DefaultPnpHandler(fdo,pIrp);
break;
case IRP_MN_SURPRISE_REMOVAL:
DebugPrint("IRP_MN_SURPRISE_REMOVAL.");
status = DefaultPnpHandler(fdo,pIrp);
break;
// case IRP_MN_QUERY_LEGACY_BUS_INFORMATION:
// KdPrint(("IRP_MN_QUERY_LEGACY_BUS_INFORMATION\n"));
// status = DefaultPnpHandler(fdo,pIrp);
// break;
default:
DebugPrint("Unsupported IRP_MN_Xxx (0x%x).", stack->MinorFunction);
status = DefaultPnpHandler(fdo,pIrp);
break;
}
UnlockDevice(pdx);
DebugPrint("Pnp End.");
return status;
} //DispatchPnp
/******************************************************************************
*
* Function : DefaultPnpHandler
*
* Description: Handle standard PnP requests
*
******************************************************************************/
NTSTATUS DefaultPnpHandler(IN PDEVICE_OBJECT fdo,
IN PIRP pIrp)
{ //DefaultPnpHandler
IoSkipCurrentIrpStackLocation(pIrp);
return IoCallDriver(((DEVICE_EXTENSION *)fdo->DeviceExtension)->pLowerDeviceObject,
pIrp);
} //DefaultPnpHandler
/******************************************************************************
*
* Function : HandleStartDevice
*
* Description: Handle the IRP_MN_START_DEVICE PnP request
*
******************************************************************************/
NTSTATUS PnpStartDevice(IN PDEVICE_OBJECT fdo,
IN PIRP pIrp )
{ //HandleStartDevice
NTSTATUS status;
PIO_STACK_LOCATION stack;
//First let all lower-level drivers handle this request. In this
//particular sample, the only lower-level driver should be the physical
//device created by the bus driver, but there could theoretically be any
//number of intervening bus filter devices. Those drivers may need to do
//some setup at this point in time before they'll be ready to handle
//non-PnP IRP's.
pIrp->IoStatus.Status = STATUS_SUCCESS;
status = ForwardAndWait(fdo,pIrp); //Send IRP to lower driver and wait for finishing
if (!NT_SUCCESS(status))
{
return CompleteRequest(pIrp, status);
}
stack = IoGetCurrentIrpStackLocation(pIrp);
// Assign resources to the devices
status = StartDevice(fdo,
&stack->Parameters.StartDevice.AllocatedResources->List[0].PartialResourceList,
&stack->Parameters.StartDevice.AllocatedResourcesTranslated->List[0].PartialResourceList);
return CompleteRequestInfo(pIrp,status,0);
} //HandleStartDevice
/******************************************************************************
*
* Function : StartDevice
*
* Description: Start a device
*
******************************************************************************/
NTSTATUS StartDevice(IN PDEVICE_OBJECT fdo,
IN PCM_PARTIAL_RESOURCE_LIST ResourceListRaw,
IN PCM_PARTIAL_RESOURCE_LIST ResourceList)
{ //StartDevice
ULONG i;
ULONG vector;
KIRQL IrqL;
BOOLEAN GotInterrupt;
BOOLEAN GotPdcMem;
BOOLEAN GotBaseMem;
NTSTATUS status;
KAFFINITY affinity;
BOOLEAN irqshare;
KINTERRUPT_MODE mode;
PDEVICE_EXTENSION pdx;
PCM_PARTIAL_RESOURCE_DESCRIPTOR ResourceRaw;
PCM_PARTIAL_RESOURCE_DESCRIPTOR Resource;
pdx = (PDEVICE_EXTENSION)fdo->DeviceExtension;
if (pdx->GotResource)
return STATUS_SUCCESS;
GotInterrupt= FALSE;
GotPdcMem = FALSE;
GotBaseMem = FALSE;
ResourceRaw = ResourceListRaw->PartialDescriptors;
Resource = ResourceList->PartialDescriptors;
DebugPrint("Start Device Start.");
for (i = 0; i < ResourceListRaw->Count; ++i, ++Resource, ++ResourceRaw)
{
switch (ResourceRaw->Type)
{
case CmResourceTypeInterrupt: //中断资源
GotInterrupt = TRUE;
IrqL = (KIRQL) Resource->u.Interrupt.Level;
vector = Resource->u.Interrupt.Vector;
affinity = Resource->u.Interrupt.Affinity;
if (ResourceRaw->Flags == CM_RESOURCE_INTERRUPT_LATCHED)
{
mode = Latched;
}
else
{
mode = LevelSensitive;
}
irqshare = Resource->ShareDisposition == CmResourceShareShared;
DebugPrint("ResouceType: Interrupt:Vector:0x%x(Translated=0x%x)",
ResourceRaw->u.Interrupt.Vector, vector);
DebugPrint(" IRQL:0x%x(Translated=0x%x)Affinity:0x%x(Translated=0x%x).",
ResourceRaw->u.Interrupt.Level, IrqL,
ResourceRaw->u.Interrupt.Affinity, affinity);
break;
case CmResourceTypePort:
//PCI9052内部的寄存器占用一个IO资源和Mem资源,可以不用IO资源
DebugPrint("Port is reqired.");
DebugPrint("ResoureType: I/O Port:Address:0x%x(Translated=0x%x);Size:0x%x.",
ResourceRaw->u.Port.Start.LowPart,
Resource->u.Port.Start.LowPart,
ResourceRaw->u.Port.Length);
break;
case CmResourceTypeMemory:
DebugPrint("ResoureType: Memory Space:Address:0x%x(Translated=0x%x);Size: 0x%x.",
ResourceRaw->u.Memory.Start.LowPart,
Resource->u.Memory.Start.LowPart,
ResourceRaw->u.Memory.Length);
//其实在对Mem资源进行读取时,按照PCI BAR0-5进行的,可以根据数量来判断各个BAR.
if (ResourceRaw->u.Memory.Length == 0x80)
{ //PCI Bar 0
pdx->PhysicalMemBase = ResourceRaw->u.Memory.Start;
pdx->MemCount = ResourceRaw->u.Memory.Length;
pdx->MemBase = (ULONG *)MmMapIoSpace(pdx->PhysicalMemBase,
pdx->MemCount,MmNonCached);
DebugPrint("The MEM Base 0 is %x; Count is %x.",pdx->MemBase,pdx->MemCount);
if (pdx->MemBase == NULL)
{
// PCI BAR 0 is required for register access
DebugPrint("PCI9052Demo: ERROR - BAR 0 mapping is required\n");
return STATUS_INSUFFICIENT_RESOURCES;
}
GotBaseMem = TRUE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -