Beyond Logic


Interrupts and Deferred Procedure Calls on Windows NT4/2000/XP

The following example is a kernel mode driver which displays kernel debug messages when an interrupt is generated on the Parallel Port. The ISR queues a DPC (Deferred Procedure Call). The source and makefiles can be downloaded here

/******************************************************************/
/*  Example NT/2000/XP Interrupt & DPC Driver                     */
/*  Copyright 2001 Craig Peacock, Craig.Peacock@beyondlogic.org   */
/*  See http://www.beyondlogic.org for a complete description     */
/*  Last Updated 11th November 2001                               */
/******************************************************************/

#include <ntddk.h>

#define PortAddress 		0x378
#define CONTROL			PortAddress+2
#define outportb(ADDR,BYTE) 	outp(ADDR,BYTE)
#define inportb(ADDR)		inp(ADDR)

typedef struct _LOCAL_DEVICE_INFO {
    PKINTERRUPT 			InterruptObject;
    ULONG				Level;      
    ULONG				Vector;     
    KAFFINITY				Affinity;   
} LOCAL_DEVICE_INFO, *PLOCAL_DEVICE_INFO;

NTSTATUS InterruptCreateDispatch(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
    )
{
	Irp->IoStatus.Information = 0;
	Irp->IoStatus.Status = STATUS_SUCCESS;
	IoCompleteRequest(Irp, IO_NO_INCREMENT);
	return STATUS_SUCCESS;
}

BOOLEAN InterruptIsr(IN PKINTERRUPT Interrupt, IN OUT PVOID Context)
{
	PDEVICE_OBJECT DeviceObject = Context;

	KdPrint( ("Interrupt.sys: Interrupt Service Routine\n") );

	/* We should check if the interrupt comes from us and return */
	
	IoRequestDpc(DeviceObject,
	 		 DeviceObject->CurrentIrp,
			 NULL);

	return TRUE;
}

VOID InterruptDpcRoutine(IN PKDPC Dpc, PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN PVOID Context)
{
	PLOCAL_DEVICE_INFO DeviceExtension;
	PIRP pIrp;

	pIrp = DeviceObject->CurrentIrp;
	DeviceExtension = DeviceObject->DeviceExtension;

        KdPrint( ("Interrupt.sys: DPC Routine - Completing Interrupt Request\n") );
	
	/* Do any processing here */
	
	return;
}

VOID InterruptUnload(IN PDRIVER_OBJECT DriverObject)
{
	WCHAR DOSNameBuffer[] = L"\\DosDevices\\Interrupt";
	UNICODE_STRING uniDOSString;
        PLOCAL_DEVICE_INFO extension = DriverObject->DeviceObject->DeviceExtension;

	KdPrint( ("Interrupt.sys: Interrupt.sys is Unloading") );

	/* Disable Parallel Port IRQ's */
        outportb(CONTROL, inportb(CONTROL) & 0xEF); 

	/* Disconnect Interrupt */ 
	IoDisconnectInterrupt(extension->InterruptObject); 

	/* Delete Symbolic Link */
	RtlInitUnicodeString(&uniDOSString, DOSNameBuffer);
	IoDeleteSymbolicLink (&uniDOSString);
  
        /* Delete Device */
	IoDeleteDevice(DriverObject->DeviceObject);
}

NTSTATUS DriverEntry(
    IN PDRIVER_OBJECT DriverObject,
    IN PUNICODE_STRING RegistryPath
    )
{
	PDEVICE_OBJECT		DeviceObject;
	NTSTATUS		status;
   	PLOCAL_DEVICE_INFO	DeviceExtension;

   	ULONG			MappedVector;
   	KIRQL			Irql;

	WCHAR			NameBuffer[] = L"\\Device\\Interrupt";
	WCHAR			DOSNameBuffer[] = L"\\DosDevices\\Interrupt";
	UNICODE_STRING		uniNameString, uniDOSString;
	
	KdPrint( ("Interrupt.sys: Interrupt.sys Loading") );
	
	RtlInitUnicodeString(&uniNameString, NameBuffer);
	RtlInitUnicodeString(&uniDOSString, DOSNameBuffer);

        status = IoCreateDevice(DriverObject,			        // DriverObject
					sizeof(LOCAL_DEVICE_INFO),	// DeviceExtensionSize
					&uniNameString,			// DeviceName
					FILE_DEVICE_UNKNOWN,		// DeviceType
					0,				// DeviceCharacteristics
					TRUE,				// Exclusive
					&DeviceObject);			// *DeviceObject

	if(!NT_SUCCESS(status)) return status;

	DeviceExtension = DeviceObject->DeviceExtension;

	status = IoCreateSymbolicLink (&uniDOSString, &uniNameString);

	if (!NT_SUCCESS(status)) return status;

	DeviceExtension->Level = 7;
	DeviceExtension->Vector = DeviceExtension->Level;

   	MappedVector = HalGetInterruptVector(Isa,						
                                           0,							
                                           DeviceExtension->Level,		
                                           DeviceExtension->Vector,	
                                           &Irql,						
                                           &DeviceExtension->Affinity);

   	KdPrint( ("Interrupt.sys: MappedVector = 0x%08X\n",MappedVector) );
   	KdPrint( ("Interrupt.sys: Level = 0x%08X\n",DeviceExtension->Level) );
   	KdPrint( ("Interrupt.sys: Vector = 0x%08X\n",DeviceExtension->Vector) );
   	KdPrint( ("Interrupt.sys: Irql = 0x%08X\n",Irql) );
   	KdPrint( ("Interrupt.sys: Affinity = 0x%08X\n",DeviceExtension->Affinity) );

 	if (MappedVector == 0) DbgPrint("Interrupt.sys: HalGetInterruptVector failed\n");

 	IoInitializeDpcRequest(DeviceObject,InterruptDpcRoutine);

  	status = IoConnectInterrupt(&DeviceExtension->InterruptObject, 	// InterruptObject
  					    InterruptIsr,	   	// ServiceRoutine
  					    DeviceObject,		// ServiceContext
 					    NULL,			// SpinLock
					    MappedVector,		// Vector
  					    Irql,			// Irql
					    Irql,			// SynchronizeIrql
					    Latched,			// InterruptMode
  					    FALSE,			// ShareVector
					    DeviceExtension->Affinity,	// ProcessorEnableMask
					    FALSE);			// FloatingSave

  	if (!NT_SUCCESS (status)) DbgPrint("Interrupt.sys: IoConnectInterrupt Failed\n");

	/* Enable Parallel Port IRQ's */
	outportb(CONTROL, inportb(CONTROL) | 0x10); 

        DriverObject->MajorFunction[IRP_MJ_CREATE] = InterruptCreateDispatch;
  	DriverObject->DriverUnload = InterruptUnload;
  
	return STATUS_SUCCESS;
}

If you are looking for a simular driver which interacts with a usermode program, try the Win 2000/XP Driver for DeVaSys USBLPT-PD11 USB Boards. It contains a driver complete with source which has a DeviceIoControl call named IOCTL_WAIT_INTERRUPT. This call returns when an specific interrupt is generated.



Copyright 2002-2005 Craig Peacock - 15th June 2005.