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

📄 dt2851.c

📁 DT2851图形卡NT驱动源码
💻 C
📖 第 1 页 / 共 3 页
字号:
                        {
                            extension->portWasMapped = FALSE;
                            extension->portBase = (PVOID)mappedPortAddress64.LowPart;
                        }

                        extension->deviceType      = DT2851_TYPE;
                        extension->portCount       = portCount;
                        extension->portMemoryType  = memType;
                    }
                    else                               // translate address unsuccessful
                    {
                        DT2851KdPrint (("DT2851.SYS: HalTranslateBusAddress failed\n"));
                        IoDeleteDevice (deviceObject);
                        status = STATUS_UNSUCCESSFUL;
                    }
                }
            }
            else                               // translate address unsuccessful
            {
                DT2851KdPrint (("DT2851.SYS: HalTranslateBusAddress failed\n"));
                IoDeleteDevice (deviceObject);
                status = STATUS_UNSUCCESSFUL;
            }
        }                                          
    }                                               
    else                                        // create device failed
    {
        DT2851KdPrint (("DT2851.SYS: IoCreateDevice failed\n"));  
        status = STATUS_UNSUCCESSFUL;
    }


#if 0
 
    if(status == STATUS_UNSUCCESSFUL)
    {
        //
        // Error creating device - release resources
        //

        RtlZeroMemory((PVOID)&resourceList, sizeof(resourceList));
    
        // Unreport our resource usage
        IoReportResourceUsage(
                       NULL,
                       driverObject,
                       &resourceList,
                       sizeof(resourceList),
                       NULL,
                       NULL,
                       0,
                       FALSE,
                       &resourceConflict);
    }

#endif 

    return status;
}

///////////////////////////////////////////////////////////////////////////////

NTSTATUS
DT2851Dispatch(
    IN    PDEVICE_OBJECT pDeviceObject,
    IN    PIRP pIORequestPacket )             

/*

Routine Description:
    This routine is the dispatch handler for the driver.  It is responsible
    for processing the IRPs.

Arguments:
    
    pDeviceObject - Pointer to device object.

    pIORequestPacket - Pointer to the current IRP.   (I/O request packet)
                       created by the I/O mamager as result of request to do 
                       read/write/IOCTL
           

Return Value:

    STATUS_SUCCESS if the IRP was processed successfully, otherwise an error
    indicating the reason for failure.

*/

{
    PDT2851_EXTENSION extension;
    PIO_STACK_LOCATION pIORequestPacketStack;
    NTSTATUS status;

    //  Initialize the I/O request packet info field.
    //  This is used to return the number of bytes transfered.

    pIORequestPacket->IoStatus.Information = 0;

    // get the device extension
    extension = (PDT2851_EXTENSION)pDeviceObject->DeviceExtension;  

    pIORequestPacketStack = IoGetCurrentIrpStackLocation(pIORequestPacket);

    //  Set default return status
    status = STATUS_NOT_IMPLEMENTED;

    // Dispatch based on major fcn code.

    switch (pIORequestPacketStack->MajorFunction)
    {
        case IRP_MJ_CREATE:
        case IRP_MJ_CLOSE:
            // We don't need any special processing on open/close so we'll
            // just return success.
            status = STATUS_SUCCESS;
            break;

        case IRP_MJ_DEVICE_CONTROL:
            //  Dispatch on IOCTL
            switch (pIORequestPacketStack->Parameters.DeviceIoControl.IoControlCode)
            {

                case IOCTL_DT2851_READ_PORT:
                     status = DT2851ReadPort(
                            extension,
                            pIORequestPacket,
                            pIORequestPacketStack);
                     break;      
          
                case IOCTL_DT2851_WRITE_PORT:
                     status = DT2851WritePort(
                            extension, 
                            pIORequestPacket,
                            pIORequestPacketStack);
                     break;  

                case IOCTL_DT2851_READ_MEMORY:
                     status = DT2851ReadMemory(
                            extension,
                            pIORequestPacket,
                            pIORequestPacketStack); 
                     break;          

                case IOCTL_DT2851_WRITE_MEMORY:
                     status = DT2851WriteMemory(
                            extension,
                            pIORequestPacket,
                            pIORequestPacketStack);
                     break;

                case IOCTL_DT2851_FILL_MEMORY:
                     status = DT2851FillMemory(
                            extension,
                            pIORequestPacket,
                            pIORequestPacketStack);
//                     RtlFillMemory (
//                            extension->baseAddress,
//                            MEMORY_SIZE/4,
//                            0x80);
                     break;

                default:
                     DT2851KdPrint (("DT2851.SYS: unknown ioControlCode\n"));
                     pIORequestPacket->IoStatus.Status = STATUS_INVALID_PARAMETER;
                     break;
            }
            break;
    }

    // We're done with I/O request.  Record the status of the I/O action.
    pIORequestPacket->IoStatus.Status = status;

    // Don't boost priority when returning since this took little time.
    IoCompleteRequest(pIORequestPacket, IO_NO_INCREMENT );

    return status;
}

///////////////////////////////////////////////////////////////////////////////

NTSTATUS
DT2851ReadPort(
    IN PDT2851_EXTENSION extension,
    IN PIRP pIORequestPacket,
    IN PIO_STACK_LOCATION pIrpStack )


/*

Routine Description:
    This routine processes the IOCTLs which read from the ports.

Arguments:
    
    extension        - the device extension
    pIORequestPacket - IO request packet
    IrpStack         - The current stack location

Return Value:
    STATUS_SUCCESS           -- OK

    STATUS_INVALID_PARAMETER -- The buffer sent to the driver
                                was too small to contain the
                                port, or the buffer which
                                would be sent back to the driver
                                was not a multiple of the data size.

    STATUS_ACCESS_VIOLATION  -- An illegal port number was given.

*/

{
                                // NOTE:  Use METHOD_BUFFERED ioctls.
    PULONG pIOBuffer;           // Pointer to transfer buffer
                                //      (treated as an array of longs).
    ULONG InBufferSize;         // Amount of data avail. from caller.
    ULONG OutBufferSize;        // Max data that caller can accept.
    ULONG nPort;                // Port number to read
    ULONG DataBufferSize;

    // Size of buffer containing data from application
    InBufferSize  = pIrpStack->Parameters.DeviceIoControl.InputBufferLength;

    // Size of buffer for data to be sent to application
    OutBufferSize = pIrpStack->Parameters.DeviceIoControl.OutputBufferLength;

    // NT copies inbuf here before entry and copies this to outbuf after
    // return, for METHOD_BUFFERED IOCTL's.
    pIOBuffer     = (PULONG)pIORequestPacket->AssociatedIrp.SystemBuffer;

    // Check to ensure input buffer is big enough to hold a port number and
    // the output buffer is at least as big as the port data width.

    DataBufferSize = sizeof(USHORT);

    if ( InBufferSize != sizeof(ULONG) || OutBufferSize < DataBufferSize )
    {
        return STATUS_INVALID_PARAMETER;
    }

    // Buffers are big enough.

    nPort = *pIOBuffer;             // Get the I/O port number from the buffer.

    if (nPort >= extension->portCount ||
        (nPort + DataBufferSize) > extension->portCount ||
        (((ULONG)extension->portBase + nPort) & (DataBufferSize - 1)) != 0)
    {
        return STATUS_ACCESS_VIOLATION;   // It was not legal.
    }
    
    if (extension->portMemoryType == 1)
    {
        // Address is in I/O space
        
        *(PUSHORT)pIOBuffer = READ_PORT_USHORT(
                        (PUSHORT)((ULONG)extension->portBase + nPort) );
    } else {
        // Address is in Memory space
        
        *(PUSHORT)pIOBuffer = READ_REGISTER_USHORT(
                        (PUSHORT)((ULONG)extension->portBase + nPort) );
    }
    
    // Indicate # of bytes read
    //
    
    pIORequestPacket->IoStatus.Information = DataBufferSize;

    return STATUS_SUCCESS;
}

///////////////////////////////////////////////////////////////////////////////

NTSTATUS
DT2851WritePort(
    IN PDT2851_EXTENSION extension, 
    IN PIRP pIORequestPacket, 
    IN PIO_STACK_LOCATION pIrpStack )

/*

Routine Description:
    This routine processes the IOCTLs which write to the ports.

Arguments:
    
    extension        - the device extension
    pIORequestPacket - IO request packet
    IrpStack         - The current stack location

Return Value:
    STATUS_SUCCESS           -- OK

    STATUS_INVALID_PARAMETER -- The buffer sent to the driver
                                was too small to contain the
                                port, or the buffer which
                                would be sent back to the driver
                                was not a multiple of the data size.

    STATUS_ACCESS_VIOLATION  -- An illegal port number was given.

*/

{
                                // NOTE:  Use METHOD_BUFFERED ioctls.
    PULONG pIOBuffer;           // Pointer to transfer buffer
                                //      (treated as array of longs).
    ULONG InBufferSize ;        // Amount of data avail. from caller.
    ULONG OutBufferSize ;       // Max data that caller can accept.
    ULONG nPort;                // Port number to read or write.
    ULONG DataBufferSize;

    // Size of buffer containing data from application
    InBufferSize  = pIrpStack->Parameters.DeviceIoControl.InputBufferLength;

    // Size of buffer for data to be sent to application
    OutBufferSize = pIrpStack->Parameters.DeviceIoControl.OutputBufferLength;

⌨️ 快捷键说明

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