📄 driver.cpp
字号:
//
// Driver.c - Chapter 8 - Parallel Port Driver
//
// Copyright (C) 2000 by Jerry Lozano
//
#include "Driver.h"
// Forward declarations
//
static NTSTATUS CreateDevice (
IN PDRIVER_OBJECT pDriverObject,
IN ULONG DeviceNumber,
IN ULONG portBase,
IN ULONG Irq );
static VOID DriverUnload (
IN PDRIVER_OBJECT pDriverObject );
NTSTATUS ClaimHardware( PDRIVER_OBJECT pDriverObj,
PDEVICE_OBJECT pDevObj,
ULONG portBase,
ULONG portSpan,
ULONG Irq);
static NTSTATUS DispatchCreate (
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp );
static NTSTATUS DispatchClose (
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp );
static NTSTATUS DispatchWrite (
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp );
static NTSTATUS DispatchRead (
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp );
BOOLEAN Isr (
IN PKINTERRUPT pIntObj,
IN PVOID pServiceContext );
static BOOLEAN TransmitByte(
IN PVOID pArg );
VOID StartIo(
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp
);
VOID DpcForIsr(
IN PKDPC pDpc,
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp,
IN PVOID pContext
);
//++
// Function: DriverEntry
//
// Description:
// Initializes the driver, locating and claiming
// hardware resources. Creates the kernel objects
// needed to process I/O requests.
//
// Arguments:
// pDriverObject - Passed from I/O Manager
// pRegistryPath - UNICODE_STRING pointer to
// registry info (service key)
// for this driver
//
// Return value:
// NTSTATUS signaling success or failure
//--
extern "C" NTSTATUS DriverEntry (
IN PDRIVER_OBJECT pDriverObject,
IN PUNICODE_STRING pRegistryPath ) {
ULONG ulDeviceNumber = 0;
NTSTATUS status;
// Announce other driver entry points
pDriverObject->DriverUnload = DriverUnload;
// This includes Dispatch routines for Create, Write & Read
pDriverObject->MajorFunction[IRP_MJ_CREATE] =
DispatchCreate;
pDriverObject->MajorFunction[IRP_MJ_CLOSE] =
DispatchClose;
pDriverObject->MajorFunction[IRP_MJ_WRITE] =
DispatchWrite;
pDriverObject->MajorFunction[IRP_MJ_READ] =
DispatchRead;
pDriverObject->DriverStartIo = StartIo;
// For each physical or logical device detected
// that will be under this Driver's control,
// a new Device object must be created.
status =
CreateDevice(pDriverObject, ulDeviceNumber,
0x378, 0x7);
// This call would be repeated until all devices are created
return status;
}
//++
// Function: CreateDevice
//
// Description:
// Adds a new device
//
// Arguments:
// pDriverObject - Passed from I/O Manager
// ulDeviceNumber - Logical device number (zero-based)
// portBase - Base Address of Parallel Port Register
// Irq - Interrupt Request Level for Parallel Port
//
// Return value:
// NTSTATUS - Success or Failure code
//--
NTSTATUS CreateDevice (
IN PDRIVER_OBJECT pDriverObject,
IN ULONG ulDeviceNumber,
IN ULONG portBase,
IN ULONG Irq ) {
NTSTATUS status;
PDEVICE_OBJECT pDevObj;
PDEVICE_EXTENSION pDevExt;
// Form the internal Device Name
CUString devName("\\Device\\PPORT"); // for "Parallel Port" device
devName += CUString(ulDeviceNumber);
// Now create the device
status =
IoCreateDevice( pDriverObject,
sizeof(DEVICE_EXTENSION),
&(UNICODE_STRING)devName,
FILE_DEVICE_UNKNOWN,
0, TRUE,
&pDevObj );
if (!NT_SUCCESS(status))
return status;
// Choose to use BUFFERED_IO
pDevObj->Flags |= DO_BUFFERED_IO;
// Initialize the Device Extension
pDevExt = (PDEVICE_EXTENSION)pDevObj->DeviceExtension;
pDevExt->pDevice = pDevObj; // back pointer
pDevExt->DeviceNumber = ulDeviceNumber;
pDevExt->ustrDeviceName = devName;
pDevExt->Irq = Irq;
pDevExt->portBase = (PUCHAR)portBase;
pDevExt->pIntObj = NULL;
// Since this driver controlls real hardware,
// the hardware controlled must be "discovered."
// Chapter 9 will discuss auto-detection,
// but for now we will hard-code the hardware
// resource for the common printer port.
// We use IoReportResourceForDetection to mark
// PORTs and IRQs as "in use."
// This call will fail if another driver
// (such as the standard parallel driver(s))
// already control the hardware
/* status =
ClaimHardware(pDriverObject,
pDevObj,
portBase, // fixed port address
PPORT_REG_LENGTH,
Irq); // fixed irq
if (!NT_SUCCESS(status)) {
// if it fails now, must delete Device object
IoDeleteDevice( pDevObj );
return status;
}
*/
// We need a DpcForIsr registration
IoInitializeDpcRequest(
pDevObj,
DpcForIsr );
// Create & connect to an Interrupt object
// To make interrupts real, we must translate irq into
// a HAL irq and vector (with processor affinity)
KIRQL kIrql;
KAFFINITY kAffinity;
ULONG kVector =
HalGetInterruptVector(Isa, 0, pDevExt->Irq, 0,
&kIrql, &kAffinity);
#if DBG==1
DbgPrint("PPORT: Interrupt %X converted to kIrql = %X, kAffinity = %X, kVector = %X\n",
pDevExt->Irq, kIrql, kAffinity, kVector);
#endif
status =
IoConnectInterrupt(
&pDevExt->pIntObj, // the Interrupt object
Isr, // our ISR
pDevExt, // Service Context
NULL, // no spin lock
kVector, // vector
kIrql, // DIRQL
kIrql, // DIRQL
Latched, // Latched or LevelSensitive
TRUE, // Shared?
kAffinity, // processors in an MP set
FALSE ); // save FP registers?
if (!NT_SUCCESS(status)) {
// if it fails now, must delete Device object
IoDeleteDevice( pDevObj );
return status;
}
#if DBG==1
DbgPrint("PPORT: Interrupt successfully connected\n");
#endif
// Form the symbolic link name
CUString symLinkName("\\??\\PPT");
symLinkName += CUString(ulDeviceNumber+1); // 1 based
pDevExt->ustrSymLinkName = symLinkName;
// Now create the link name
status =
IoCreateSymbolicLink( &(UNICODE_STRING)symLinkName,
&(UNICODE_STRING)devName );
if (!NT_SUCCESS(status)) {
// if it fails now, must delete Device object
IoDeleteDevice( pDevObj );
return status;
}
// Made it
return STATUS_SUCCESS;
}
//++
// Function: DriverUnload
//
// Description:
// Stops & Deletes devices controlled by this driver.
// Stops interrupt processing (if any)
// Releases kernel resources consumed by driver
//
// Arguments:
// pDriverObject - Passed from I/O Manager
//
// Return value:
// None
//--
VOID DriverUnload (
IN PDRIVER_OBJECT pDriverObject ) {
PDEVICE_OBJECT pNextObj;
// Loop through each device controlled by Driver
pNextObj = pDriverObject->DeviceObject;
while (pNextObj != NULL) {
// Dig out the Device Extension from the
// Device Object
PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)
pNextObj->DeviceExtension;
// Delete our Interrupt object
if (pDevExt->pIntObj)
IoDisconnectInterrupt( pDevExt->pIntObj );
// This will yield the symbolic link name
UNICODE_STRING pLinkName =
pDevExt->ustrSymLinkName;
// ... which can now be deleted
IoDeleteSymbolicLink(&pLinkName);
#if DBG==1
DbgPrint("PPORT: Object %ws deleted\n",
(PWSTR)pDevExt->ustrSymLinkName);
#endif
// a little trickery...
// we need to delete the device object, BUT
// the Device object is pointed to by pNextObj
// If we delete the device object first,
// we can't traverse to the next Device in the list
// Rather than create another pointer, we can
// use the DeviceExtension's back pointer to the device
// So, first update the next pointer...
pNextObj = pNextObj->NextDevice;
// then delete the device using the Extension
IoDeleteDevice( pDevExt->pDevice );
}
// Finally, hardware that was allocated in DriverEntry
// would be released here using
// IoReportResourceUsage
}
NTSTATUS ClaimHardware( PDRIVER_OBJECT pDriverObj,
PDEVICE_OBJECT pDevObj,
ULONG portBase,
ULONG portSpan,
ULONG Irq) {
NTSTATUS status;
PHYSICAL_ADDRESS maxPortAddr;
PIO_RESOURCE_REQUIREMENTS_LIST pRRList;
// This size is a headache. The RRLIST already includes
// 1 Resource List plus 1 Resource Descriptor
// we need 1 additional Resource Descriptor (2 total)
int rrSize = sizeof(IO_RESOURCE_REQUIREMENTS_LIST) +
sizeof(IO_RESOURCE_DESCRIPTOR);
pRRList = (PIO_RESOURCE_REQUIREMENTS_LIST)
ExAllocatePool(PagedPool, rrSize);
RtlZeroMemory(pRRList, rrSize);
pRRList->ListSize = rrSize;
pRRList->AlternativeLists = 1; // only 1 Resource List
pRRList->InterfaceType = Isa;
pRRList->List[0].Version = 1;
pRRList->List[0].Revision = 1;
pRRList->List[0].Count = 2; // 2 Resource Descriptors: port & irq
pRRList->List[0].Descriptors[0].Type = CmResourceTypePort;
pRRList->List[0].Descriptors[0].ShareDisposition = CmResourceShareDeviceExclusive;
pRRList->List[0].Descriptors[0].Flags = CM_RESOURCE_PORT_IO;
pRRList->List[0].Descriptors[0].u.Port.Length = portSpan;
pRRList->List[0].Descriptors[0].u.Port.Alignment = FILE_WORD_ALIGNMENT;
maxPortAddr.QuadPart = portBase;
pRRList->List[0].Descriptors[0].u.Port.MinimumAddress = maxPortAddr;
maxPortAddr.LowPart += portSpan-1;
pRRList->List[0].Descriptors[0].u.Port.MaximumAddress = maxPortAddr;
pRRList->List[0].Descriptors[1].Type = CmResourceTypeInterrupt;
pRRList->List[0].Descriptors[1].ShareDisposition = CmResourceShareDeviceExclusive;
pRRList->List[0].Descriptors[1].Flags = CM_RESOURCE_INTERRUPT_LATCHED;
pRRList->List[0].Descriptors[1].u.Interrupt.MinimumVector = Irq;
pRRList->List[0].Descriptors[1].u.Interrupt.MaximumVector = Irq;
status =
IoReportDetectedDevice(
pDriverObj, // DriverObject
Isa, // Bus type
-1, // Bus number
-1, // SlotNumber
NULL, // Driver RESOURCE_LIST
pRRList, // Device Resource List
FALSE, // Already claimed?
&pDevObj ); // device object
ExFreePool(pRRList);
return status;
}
//++
// Function: DispatchCreate
//
// Description:
// Handles call from Win32 CreateFile request
// Does nothing.
//
// Arguments:
// pDevObj - Passed from I/O Manager
// pIrp - Passed from I/O Manager
//
// Return value:
// NTSTATUS - success or failure code
//--
NTSTATUS DispatchCreate (
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp ) {
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = 0; // no bytes xfered
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
return STATUS_SUCCESS;
}
//++
// Function: DispatchClose
//
// Description:
// Handles call from Win32 CreateHandle request
// For PPort driver, frees any buffer
//
// Arguments:
// pDevObj - Passed from I/O Manager
// pIrp - Passed from I/O Manager
//
// Return value:
// NTSTATUS - success or failure code
//--
NTSTATUS DispatchClose (
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp ) {
// Dig out the Device Extension from the Device object
PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)
pDevObj->DeviceExtension;
if (pDevExt->deviceBuffer != NULL) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -