📄 isaintdevice.cpp
字号:
// If you have created additional queues, select
// the appropriate queue for this Irp here.
m_DriverManagedQueue.PnpNextIrp(I);
}
////////////////////////////////////////////////////////////////////////
// ISAINTDevice::Read
//
// Routine Description:
// Handler for IRP_MJ_READ
//
// Parameters:
// I Current IRP
//
// Return Value:
// NTSTATUS Result code
//
// Comments:
// This routine handles read requests.
//
// The KPnpDevice class handles restricting IRP flow
// if the device is stopping or being removed.
//
NTSTATUS ISAINTDevice::Read(KIrp I)
{
// TODO: Check the incoming request. Replace "FALSE" in the following
// line with a check that returns TRUE if the request is not valid.
if (FALSE) // If (Request is invalid)
{
// Invalid parameter in the Read request
I.Information() = 0;
return I.PnpComplete(this, STATUS_INVALID_PARAMETER);
}
// Always ok to read 0 elements.
if (I.ReadSize() == 0)
{
I.Information() = 0;
return I.PnpComplete(this, STATUS_SUCCESS);
}
// Queue the IRP for processing in the driver managed queue.
// The actual read function is performed in SerialRead
// TODO: The Wizard creates a single queue for all Irps.
// If you have created additional queues, select
// the appropriate queue for this Irp here.
return m_DriverManagedQueue.QueueIrp(I);
}
////////////////////////////////////////////////////////////////////////
// ISAINTDevice::SerialWrite
//
// Routine Description:
// Handler for serialized WRITE
//
// Parameters:
// I - Current IRP
//
// Return Value:
// None
//
// Comments:
// This routine is called when the IRP is removed from the
// STARTIO queue. This guarantees that multiple requests are
// never processed simultaneously.
//
void ISAINTDevice::SerialWrite(KIrp I)
{
NTSTATUS status = STATUS_SUCCESS;
PUCHAR pBuffer = (PUCHAR)I.BufferedWriteSource();
ULONG dwTotalSize = I.WriteSize(CURRENT);
ULONG dwBytesSent = 0;
// TODO: If the write can be satisfied immediately, set the Information
// and Status fields now, then call NextIrp to complete this IRP
// and start processing the next IRP in the queue.
// TODO: If the device cannot accept all of the data yet, initiate a
// request to the physical device here, and defer the Information,
// Status, and NextIrp handling until the hardware indicates that
// the write is complete. Typically, this might be handled in a
// DPC that is called after the hardware finishes transferring
// the data.
// TODO: To satisfy the write now, transfer data to the device
// from caller's buffer at "pBuffer". Then, indicate how much
// data was transferred:
I.Information() = dwBytesSent;
I.Status() = status;
// PnpNextIrp completes this IRP and starts processing
// for the next IRP in the driver managed queue.
// TODO: The Wizard creates a single queue for all Irps.
// If you have created additional queues, select
// the appropriate queue for this Irp here.
m_DriverManagedQueue.PnpNextIrp(I);
}
////////////////////////////////////////////////////////////////////////
// ISAINTDevice::Write
//
// Routine Description:
// Handler for IRP_MJ_WRITE
//
// Parameters:
// I - Current IRP
//
// Return Value:
// NTSTATUS - Result code
//
// Comments:
// This routine handles write requests.
//
// The KPnpDevice class handles restricting IRP flow
// if the device is stopping or being removed.
//
NTSTATUS ISAINTDevice::Write(KIrp I)
{
// TODO: Check the incoming request. Replace "FALSE" in the following
// line with a check that returns TRUE if the request is not valid.
if (FALSE)
{
// Invalid parameter in the Write request
I.Information() = 0;
return I.PnpComplete(this, STATUS_INVALID_PARAMETER);
}
// Always ok to write 0 elements.
if (I.WriteSize() == 0)
{
I.Information() = 0;
return I.PnpComplete(this, STATUS_SUCCESS);
}
// Queue the IRP for processing in the driver managed queue.
// The actual write function is performed in SerialWrite
// TODO: The Wizard creates a single queue for all Irps.
// If you have created additional queues, select
// the appropriate queue for this Irp here.
return m_DriverManagedQueue.QueueIrp(I);
}
////////////////////////////////////////////////////////////////////////
// ISAINTDevice::DeviceControl
//
// Routine Description:
// Handler for IRP_MJ_DEVICE_CONTROL
//
// Parameters:
// I - Current IRP
//
// Return Value:
// None
//
// Comments:
// This routine is the first handler for Device Control requests.
// Some function codes may be handled immediately,
// while others may be serialized through the StartIo routine.
//
// The KPnpDevice class handles restricting IRP flow
// if the device is stopping or being removed.
//
NTSTATUS ISAINTDevice::DeviceControl(KIrp I)
{
NTSTATUS status;
switch (I.IoctlCode())
{
case ISAINT_IOCTL_READ_PORT:
status = ISAINT_IOCTL_READ_PORT_Handler(I);
break;
case ISAINT_IOCTL_WRITE_PORT:
status = ISAINT_IOCTL_WRITE_PORT_Handler(I);
break;
case ISAINT_IOCTL_INTEVENT:
status = ISAINT_IOCTL_INTEVENT_Handler(I);
break;
default:
// Unrecognized IOCTL request
status = STATUS_INVALID_PARAMETER;
break;
}
// If the IRP was queued, or its IOCTL handler deferred processing using some
// driver specific scheme, the status variable is set to STATUS_PENDING.
// In this case we simply return that status, and the IRP will be completed
// later. Otherwise, complete the IRP using the status returned by the
// IOCTL handler.
if (status == STATUS_PENDING)
{
return status;
}
else
{
return I.PnpComplete(this, status);
}
}
////////////////////////////////////////////////////////////////////////
// ISAINTDevice::ISAINT_IOCTL_READ_PORT_Handler
//
// Routine Description:
// Handler for IO Control Code ISAINT_IOCTL_READ_PORT
//
// Parameters:
// I - IRP containing IOCTL request
//
// Return Value:
// NTSTATUS - Status code indicating success or failure
//
// Comments:
// This routine implements the ISAINT_IOCTL_READ_PORT function.
// This routine runs at passive level.
//
NTSTATUS ISAINTDevice::ISAINT_IOCTL_READ_PORT_Handler(KIrp I)
{
NTSTATUS status = STATUS_SUCCESS;
// TODO: Verify that the input parameters are correct
// If not, return STATUS_INVALID_PARAMETER
// TODO: Handle the the ISAINT_IOCTL_READ_PORT request, or
// defer the processing of the IRP (i.e. by queuing) and set
// status to STATUS_PENDING.
// TODO: Assuming that the request was handled here. Set I.Information
// to indicate how much data to copy back to the user.
I.Information() = 0;
return status;
}
////////////////////////////////////////////////////////////////////////
// ISAINTDevice::ISAINT_IOCTL_WRITE_PORT_Handler
//
// Routine Description:
// Handler for IO Control Code ISAINT_IOCTL_WRITE_PORT
//
// Parameters:
// I - IRP containing IOCTL request
//
// Return Value:
// NTSTATUS - Status code indicating success or failure
//
// Comments:
// This routine implements the ISAINT_IOCTL_WRITE_PORT function.
// This routine runs at passive level.
//
NTSTATUS ISAINTDevice::ISAINT_IOCTL_WRITE_PORT_Handler(KIrp I)
{
NTSTATUS status = STATUS_SUCCESS;
// TODO: Verify that the input parameters are correct
// If not, return STATUS_INVALID_PARAMETER
// TODO: Handle the the ISAINT_IOCTL_WRITE_PORT request, or
// defer the processing of the IRP (i.e. by queuing) and set
// status to STATUS_PENDING.
// TODO: Assuming that the request was handled here. Set I.Information
// to indicate how much data to copy back to the user.
I.Information() = 0;
return status;
}
////////////////////////////////////////////////////////////////////////
// ISAINTDevice::ISAINT_IOCTL_INTEVENT_Handler
//
// Routine Description:
// Handler for IO Control Code ISAINT_IOCTL_INTEVENT
//
// Parameters:
// I - IRP containing IOCTL request
//
// Return Value:
// NTSTATUS - Status code indicating success or failure
//
// Comments:
// This routine implements the ISAINT_IOCTL_INTEVENT function.
// This routine runs at passive level.
//
NTSTATUS ISAINTDevice::ISAINT_IOCTL_INTEVENT_Handler(KIrp I)
{
NTSTATUS status = STATUS_SUCCESS;
// TODO: Verify that the input parameters are correct
// If not, return STATUS_INVALID_PARAMETER
// TODO: Handle the the ISAINT_IOCTL_INTEVENT request, or
// defer the processing of the IRP (i.e. by queuing) and set
// status to STATUS_PENDING.
// TODO: Assuming that the request was handled here. Set I.Information
// to indicate how much data to copy back to the user.
I.Information() = 0;
return status;
}
////////////////////////////////////////////////////////////////////////////////
// ISAINTDevice_DriverManagedQueue::StartIo
//
// Routine Description:
// This routine is called when an IRP is taken off
// the Driver Managed Queue (used for serializing I/O) and
// presented for processing.
//
// Parameters:
// I - IRP removed from queue
//
// Return Value:
// None
//
// Comments:
//
VOID ISAINTDevice_DriverManagedQueue::StartIo(KIrp I)
{
// The KDriverManagedQueueEx class gives us the Irp in a non-cancelable state
// (cancel routine set to NULL) so we can process it without having to worry
// about clearing the cancel routine first, as is the case with system queuing,
// or the legacy class KDriverManagedQueue. You may want to set a different cancel
// routine here, or at other points during the processing of this Irp.
// Find the device class so we can call the serialized
// routines in the device class. The handlers can be
// moved to the DriverManagedQueue class if it is more
// convenient.
ISAINTDevice *pDev = (ISAINTDevice *) KDevicePTR(I.DeviceObject());
// Start processing request.
// Switch on the IRP's function:
switch (I.MajorFunction())
{
case IRP_MJ_READ:
pDev->SerialRead(I);
break;
case IRP_MJ_WRITE:
pDev->SerialWrite(I);
break;
case IRP_MJ_DEVICE_CONTROL:
switch (I.IoctlCode())
{
default:
// We queued a request that shouldn't have been queued
// (should never get here)
ASSERT(FALSE);
break;
}
break;
default:
// Error - unexpected IRP received
// NextIrp completes this IRP and starts processing
// for the next IRP in the queue.
ASSERT(FALSE);
I.Status() = STATUS_INVALID_PARAMETER;
PnpNextIrp(I);
break;
}
}
////////////////////////////////////////////////////////////////////////
// ISAINTDevice::DpcFor_Irq
//
// Routine Description:
// Deferred Procedure Call (DPC) for Irq
//
// Parameters:
// Arg1 - User-defined context variable
// Arg2 - User-defined context variable
//
// Return Value:
// None
//
// Comments:
// This function is called for secondary processing of an interrupt.
// Most code that runs at elevated IRQL should run here rather than
// in the ISR, so that other interrupt handlers can continue to run.
//
VOID ISAINTDevice::DpcFor_Irq(PVOID Arg1, PVOID Arg2)
{
// TODO: Typically, the interrupt signals the end of a data transfer
// operation for a READ or WRITE operation. The following code
// assumes the driver will handle the completion of the IRP
// associated with this operation here. It further assumes that the
// IRP to be completed is the current IRP on the driver managed queue.
// Modify or replace the code here to handle the function of your DPC.
// Make a KIrp object to reference the current Irp
// TODO: The Wizard creates a single queue for all Irps.
// If you have created additional queues, select
// the appropriate queue for this Irp here.
KIrp I(m_DriverManagedQueue.CurrentIrp());
if (!I.IsNull()) {
// TODO: Set the Status and Information fields to reflect the
// success and size of the transfer
I.Status() = STATUS_SUCCESS;
I.Information() = 0;
// PnpNextIrp completes this IRP and starts processing
// for the next IRP in the driver managed queue.
m_DriverManagedQueue.PnpNextIrp(I);
}
// TODO: Enable further interrupts on the device.
// The following macros simply allows compilation at Warning Level 4
// If you reference these parameters in the function simply remove the macro.
UNREFERENCED_PARAMETER(Arg1);
UNREFERENCED_PARAMETER(Arg2);
}
////////////////////////////////////////////////////////////////////////
// ISAINTDevice::Isr_Irq
//
// Routine Description:
// Interrupt Service Routine (ISR) for IRQ Irq
//
// Parameters:
// None
//
// Return Value:
// BOOLEAN True if this is our interrupt
//
// Comments:
//
BOOLEAN ISAINTDevice::Isr_Irq(void)
{
// TODO: Verify that the interrupt was caused by our device.
// Replace "TRUE" in next line with actual test.
if (TRUE)
{
// Return FALSE to indicate that this device did not cause the interrupt.
return FALSE;
}
// TODO: Service the device.
// Minimal processing may be done here in the ISR, but
// most processing should be deferred to a DPC.
// Generally, you should:
// o stop the device from generating interrupts
// o perform any additional time-critical functions
// o schedule a DPC to perform the bulk of the work.
//
// Request deferred procedure call
// The arguments to Request may be any values that you choose
if (!m_DpcFor_Irq.Request(NULL, NULL))
{
// TODO: Request is already in the queue
// You may want to set flags or perform
// other actions in this case
}
// Return TRUE to indicate that our device caused the interrupt
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -