⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lab2.c

📁 通过VC源代码
💻 C
📖 第 1 页 / 共 2 页
字号:

// Hardware Simulator driver

/*****************************************************************************
*           Change Log
*  Date     | Change
*-----------+-----------------------------------------------------------------
* 10-06-97  | END - Created
* 17-Mar-98 | <jmn> Added error printout for bad DeviceIoControl operation
*****************************************************************************/


/*****************************************************************************
*                       To Do
*-----------------------------------------------------------------------------
  put and get packet routines are not implemented
  interrupts not used
  startio not used
  device timeout not implemented

*****************************************************************************/
#include "ntddk.h"

#include "Lab2.h"
#include "Lab2Ioctl.h"
#include "HSTestReadReg.h"
#include "readReg.h"




#define DBG_MSG_HDR "Lab2"



NTSTATUS Lab2Open(
    IN PDEVICE_OBJECT deviceObject,
    IN PIRP Irp );

NTSTATUS Lab2Close(
    IN PDEVICE_OBJECT deviceObject,
    IN PIRP Irp );

NTSTATUS Lab2DeviceControl (
    IN PDEVICE_OBJECT deviceObject,
    IN PIRP Irp );

VOID Lab2Unload(
    IN PDRIVER_OBJECT driverObject );

BOOLEAN LabInterruptServiceRoutine(
    IN PKINTERRUPT Interrupt,
    IN OUT PVOID Context    );

static NTSTATUS LabSetupISR(
        PDEVICE_OBJECT deviceObject,
        ULONG interruptLine );      
        
VOID LabDpcRoutine(
    IN PKDPC Dpc,
    PDEVICE_OBJECT  deviceObject,
    IN PVOID SystemArgument1,
    IN PVOID SystemArgument2 );

NTSTATUS Lab2PutPacket(  
                        PDEVICE_OBJECT deviceObject,
                        UCHAR   *pBuffer,
                        ULONG   inBufferLength,
                        ULONG   *charCount);                         // RETURN transfer SIZE
NTSTATUS Lab2GetPacket(  
                        PDEVICE_OBJECT deviceObject,
                        UCHAR   *pBuffer,
                        ULONG   outBufferLength,
                        ULONG   *charCount);                         // RETURN transfer SIZE

/****************************************************************************
    Function:
              DriverEntry
    Arguments:

    Description:

    Returns:


*****************************************************************************/
NTSTATUS DriverEntry(
    IN PDRIVER_OBJECT driverObject,
    IN PUNICODE_STRING RegistryPath )
{

    PDEVICE_OBJECT deviceObject = NULL;
    NTSTATUS status;
    UNICODE_STRING uniNtNameString;
    UNICODE_STRING uniDosNameString;

    NTSTATUS retReg;

    BOOLEAN GotResources;

    //
    //  registry values
    //
    ULONG debugMask;
    ULONG eventLog;
    ULONG shouldBreak;
    ULONG interruptLine;
    ULONG interruptIDT;



   unsigned int busNumber;


   PLAB2_DEVICE_EXTENSION extension;

   VOID             *registerAddress;
   PHYSICAL_ADDRESS  registerPhysicalAddress;

   unsigned int      vgaBaseReg;
   unsigned int      baseReg = 0;
   ULONG             baseAddressReg1;

   ULONG             lengthToMap;
   unsigned int      i;

   KdPrint( ("%s: Entered the Lab2 driver!\n",   DBG_MSG_HDR) );


   //
   // Read the registry for our parameters
   //
   retReg = helloReadRegistry(driverObject, RegistryPath, &debugMask, &eventLog,
                              &shouldBreak );

   //
   // read the registry for the "address" of the hardware simulator
   //
   retReg = hardwareSimReadRegistry(driverObject, RegistryPath, &interruptLine, &interruptIDT,
                                    &registerAddress, &registerPhysicalAddress );


   KdPrint( ("%s: returned from readRegistry!\n",   DBG_MSG_HDR) );


   if (shouldBreak)
   {
      DbgBreakPoint();
   }

    //
    // Create counted string version of our device name.
    //

    RtlInitUnicodeString( &uniNtNameString, NT_DEVICE_NAME );



    //
    // Create the device object
    //

    status = IoCreateDevice(
                 driverObject,
                 sizeof(LAB2_DEVICE_EXTENSION),        //  device extension
                 &uniNtNameString,
                 FILE_DEVICE_UNKNOWN,
                 0,                     // No standard device characteristics
                 FALSE,                 // This isn't an exclusive device
                 &deviceObject
                 );

    if ( !NT_SUCCESS(status) )
    {

        KdPrint(("%s could not create device\n",   DBG_MSG_HDR));
        status = STATUS_NO_SUCH_DEVICE;
        return status;
    }
    extension = deviceObject->DeviceExtension;   
    extension->DeviceObject = deviceObject;   

    extension->BusNumber = busNumber;

    // 
    // save information from registry in extension
    //
    
    extension->registerAddress          = registerAddress;        
    extension->registerPhysicalAddress  = registerPhysicalAddress;
    extension->interruptLine            = interruptLine;
    extension->interruptIDT             = interruptIDT;

    extension->debugMask      = debugMask;
    extension->eventLog       = eventLog;
    extension->shouldBreak    = shouldBreak;

    
    //
    // Create dispatch points for create/open, close, unload.
    //

    driverObject->MajorFunction[IRP_MJ_CREATE]          = Lab2Open;
    driverObject->MajorFunction[IRP_MJ_CLOSE]           = Lab2Close;
    driverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL]  = Lab2DeviceControl;

    driverObject->DriverUnload = Lab2Unload;

        //
        // Other possible dispatch entries
        //
        // DriverObject->MajorFunction[IRP_MJ_READ] 					= 
        // DriverObject->MajorFunction[IRP_MJ_WRITE] 					=
        // DriverObject->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL]  =   // For calls from other drivers
        // DriverObject->MajorFunction[IRP_MJ_SHUTDOWN] 				=   // Note driver MUST call IoRegisterShutdownNotification()

        // DriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION] 		=
        // DriverObject->MajorFunction[IRP_MJ_SET_INFORMATION] 			=
        // DriverObject->MajorFunction[IRP_MJ_CLEANUP] 					=
        // DriverObject->MajorFunction[IRP_MJ_FLUSH_BUFFERS] 		    =
        // DriverObject->MajorFunction[IRP_MJ_SET_VOLUME_INFORMATION]   =
        // DriverObject->MajorFunction[IRP_MJ_QUERY_VOLUME_INFORMATION] =
        // DriverObject->MajorFunction[IRP_MJ_DIRECTORY_CONTROL]    	=
        // DriverObject->MajorFunction[IRP_MJ_FILE_SYSTEM_CONTROL]		=
        // DriverObject->MajorFunction[IRP_MJ_LOCK_CONTROL]  			=
        // DriverObject->MajorFunction[IRP_MJ_QUERY_EA]   				=
        // DriverObject->MajorFunction[IRP_MJ_SET_EA]   				=
        // DriverObject->MajorFunction[IRP_MJ_SCSI]  					=



    KdPrint( ("%s: just about ready!\n",   DBG_MSG_HDR) );

	//
	// check if resources (ports and interrupt) are available
	//

//
// A PRODUCT DRIVER WOULD REPORT (NON) RESOURCE USAGE HERE
//


    //
    // Do Buffered I/O.    A nop Read and write are not supported by this driver
    //
    deviceObject->Flags |= DO_BUFFERED_IO;

    //
    // Create counted string version of our Win32 device name.
    //
    RtlInitUnicodeString( &uniDosNameString, DOS_DEVICE_NAME );

    //
    // Create a link from our device name to a name in the Win32 namespace.
    //

    status = IoCreateSymbolicLink( &uniDosNameString, &uniNtNameString );

    if (!NT_SUCCESS(status))
    {
        KdPrint( ("%s: Couldn't create the symbolic link\n",   DBG_MSG_HDR) );

        IoDeleteDevice( driverObject->DeviceObject );
        return status;
    }

    //
    //  Connect an ISR for our device
    //
//     status = LabSetupISR( deviceObject, interruptLine );




    KdPrint( ("%s: All initialized\n",   DBG_MSG_HDR) );


    return status;
}

/****************************************************************************
    Function:
              Lab2Open
    Arguments:

    Description:

    Returns:


*****************************************************************************/
NTSTATUS Lab2Open(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp )
{
    KdPrint( ("%s: Opened!!\n",   DBG_MSG_HDR) );


    //
    // No need to do anything.
    //

    //
    // Fill these in before calling IoCompleteRequest.
    //
    // DON'T get cute and try to use the status field of
    // the irp in the return status.  That IRP IS GONE as
    // soon as you call IoCompleteRequest.
    //


    Irp->IoStatus.Status = STATUS_SUCCESS;
    Irp->IoStatus.Information = 0;

    IoCompleteRequest( Irp, IO_NO_INCREMENT );

    return STATUS_SUCCESS;
}

/****************************************************************************
    Function:
              Lab2Close
    Arguments:

    Description:

    Returns:


*****************************************************************************/
NTSTATUS Lab2Close(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp )
{

    KdPrint( ("%s: Closed!!\n",   DBG_MSG_HDR) );

    //
    // No need to do anything.
    //

    //
    // Fill these in before calling IoCompleteRequest.
    //
    // DON'T get cute and try to use the status field of
    // the irp in the return status.  That IRP IS GONE as
    // soon as you call IoCompleteRequest.
    //

    Irp->IoStatus.Status = STATUS_SUCCESS;
    Irp->IoStatus.Information = 0;



    IoCompleteRequest( Irp, IO_NO_INCREMENT );

    return STATUS_SUCCESS;
}

/****************************************************************************
    Function:
              Lab2DeviceControl
    Arguments:

    Description:

    Returns:


*****************************************************************************/
NTSTATUS  Lab2DeviceControl(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp )
{
    NTSTATUS  ret = STATUS_SUCCESS;
    PIO_STACK_LOCATION irpStack;

    UCHAR *pBuffer;
    ULONG outBufferLength;
    ULONG inBufferLength;
    ULONG charCount = 0;

    PLAB2_DEVICE_EXTENSION extension;

    NTSTATUS status;


    KdPrint( ("%s: Device Control!!\n",   DBG_MSG_HDR) );

    extension = DeviceObject->DeviceExtension;
    irpStack = IoGetCurrentIrpStackLocation(Irp);

    pBuffer  = (UCHAR *)Irp->AssociatedIrp.SystemBuffer;  // for buffered i/o
    inBufferLength = irpStack->Parameters.DeviceIoControl.InputBufferLength;
    outBufferLength = irpStack->Parameters.DeviceIoControl.OutputBufferLength;


    switch(irpStack->Parameters.DeviceIoControl.IoControlCode)
    { /* ioControlCode */
        case  IOCTL_LAB_INITIALIZE:
             KdPrint( ("%s: Device Control  -- enter Initialize\n",   DBG_MSG_HDR) );

             break;

        case IOCTL_LAB_HELLO_READ:
             KdPrint( ("%s: Device Control  -- enter IOCTL_READ\n",   DBG_MSG_HDR) );

             status = Lab2GetPacket(  
                        DeviceObject,
                        pBuffer,
                        outBufferLength,
                        &charCount);       // RETURN transfer SIZE


             break;

        case IOCTL_LAB_HELLO_WRITE:
             KdPrint( ("%s: Device Control  -- enter IOCTL_WRITE\n",   DBG_MSG_HDR) );

             status = Lab2PutPacket(  
                        DeviceObject,
                        pBuffer,
                        inBufferLength,
                        &charCount);        // RETURN transfer SIZE

             break;
        default:
	     	KdPrint( ("%s: Unknown DeviceIoControl: %08x\n", 
			  DBG_MSG_HDR, 
			  irpStack->Parameters.DeviceIoControl.IoControlCode));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -