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

📄 genport.c

📁 win2000下ISA接口的串口板卡的驱动程序
💻 C
📖 第 1 页 / 共 5 页
字号:
GpdDispatchPower(
    IN PDEVICE_OBJECT    DeviceObject,
    IN PIRP              Irp
    )
/*++

Routine Description:

    This routine is the dispatch routine for power irps.
    Does nothing except forwarding the IRP to the next device
    in the stack.

Arguments:

    DeviceObject - Pointer to the device object.

    Irp - Pointer to the request packet.

Return Value:

    NT Status code
--*/
{
    PLOCAL_DEVICE_INFO   deviceInfo;
    
    deviceInfo = (PLOCAL_DEVICE_INFO) DeviceObject->DeviceExtension;

    //
    // If the device has been removed, the driver should not pass 
    // the IRP down to the next lower driver.
    //
    
    if (deviceInfo->Removed) {
        
        PoStartNextPowerIrp(Irp);
        Irp->IoStatus.Status =  STATUS_DELETE_PENDING;
        IoCompleteRequest(Irp, IO_NO_INCREMENT );
        return STATUS_DELETE_PENDING;
    }
    
    PoStartNextPowerIrp(Irp);
    IoSkipCurrentIrpStackLocation(Irp);
    return PoCallDriver(deviceInfo->NextLowerDriver, Irp);
}

NTSTATUS 
GpdDispatchSystemControl(
    IN PDEVICE_OBJECT    DeviceObject,
    IN PIRP              Irp
    )
/*++

Routine Description:

    This routine is the dispatch routine for WMI irps.
    Does nothing except forwarding the IRP to the next device
    in the stack.
    
Arguments:

    DeviceObject - Pointer to the device object.

    Irp - Pointer to the request packet.

Return Value:

    NT Status code
--*/
{
    PLOCAL_DEVICE_INFO   deviceInfo;

    PAGED_CODE();

    deviceInfo = (PLOCAL_DEVICE_INFO) DeviceObject->DeviceExtension;
    IoSkipCurrentIrpStackLocation(Irp);
    return IoCallDriver(deviceInfo->NextLowerDriver, Irp);
}

    
VOID
GpdUnload(
    IN PDRIVER_OBJECT DriverObject
    )
/*++

Routine Description:

    Free all the allocated resources, etc.

Arguments:

    DriverObject - pointer to a driver object.

Return Value:

    VOID.

--*/
{
    PAGED_CODE ();

    //
    // The device object(s) should be NULL now
    // (since we unload, all the devices objects associated with this
    // driver must have been deleted.
    //
    ASSERT(DriverObject->DeviceObject == NULL);
    DebugPrint (("unload\n"));

    return;
}

NTSTATUS
GpdDispatch(
    IN    PDEVICE_OBJECT pDO,
    IN    PIRP pIrp             
    )

/*++

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

Arguments:
    
    pDO - Pointer to device object.

    pIrp - Pointer to the current IRP.

Return Value:

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

--*/

{
    PLOCAL_DEVICE_INFO pLDI;
    PIO_STACK_LOCATION pIrpStack;
    NTSTATUS Status;
	UCHAR CurrentISR;
    LARGE_INTEGER DpcTime;
	int j;
    PAGED_CODE();

    pIrp->IoStatus.Information = 0;
    pLDI = (PLOCAL_DEVICE_INFO)pDO->DeviceExtension;    // Get local info struct

//    DebugPrint (("Entered GpdDispatch\n"));

    Status = IoAcquireRemoveLock (&pLDI->RemoveLock, pIrp);//在处理IRP请求之前,先上锁,以免被其他程序将该请求删除,引起冲突
															//与其对应的解锁操作是IoReleaseRemoveLock(&pLDI->RemoveLock, pIrp);
    if (!NT_SUCCESS (Status)) {
		DebugPrint ((">>>  IoAcquireRemoveLock Failed! %X\n",Status));
        pIrp->IoStatus.Information = 0;
        pIrp->IoStatus.Status = Status;
        IoCompleteRequest (pIrp, IO_NO_INCREMENT);
        return Status;
    }

    if (!pLDI->Started) {//如果所指定的设备还没有被“start”,将报“STATUS_DEVICE_NOT_READY”状态错误
        //
        // We fail all the IRPs that arrive before the device is started.
        //
        pIrp->IoStatus.Status = Status = STATUS_DEVICE_NOT_READY;
        IoCompleteRequest(pIrp, IO_NO_INCREMENT );
        IoReleaseRemoveLock(&pLDI->RemoveLock, pIrp);       
		DebugPrint ((">>>  !pLDI->Started ! STATUS_DEVICE_NOT_READY\n"));
        return Status;
    }
    
    pIrpStack = IoGetCurrentIrpStackLocation(pIrp);//从IRP栈内取一个包,准备处理

    // Dispatch based on major fcn code.

    switch (pIrpStack->MajorFunction)
    {
        case IRP_MJ_CREATE:
			DebugPrint ((">>>  IRP_MJ_CREATE\n"));
			DpcTime.QuadPart = -5    
								* 10       // from 100 ns to us
								     //      us to ms
								;    //      ms to s

			if (KeSetTimerEx(&pLDI->RecTimer,
//							RtlConvertLongToLargeInteger(10),
							DpcTime,
							INTERVAL_DPCFORREC, 
							&pLDI->RecDPC)) 
//			if(KeInsertQueueDpc(&pLDI->RecDPC,))
			{
				DebugPrint((">>>  KeSetTimerEx Succeeded! -10 2000\n"));
			}
			else
			{
				DebugPrint((">>>  KeSetTimerEx Failed! -10 2000\n"));
			}
            Status = STATUS_SUCCESS;
            break;
        case IRP_MJ_CLOSE:
            // We don't need any special processing on open/close so we'll
            // just return success.
			DebugPrint ((">>>  IRP_MJ_CLOSE\n"));
			KeCancelTimer(&pLDI->RecTimer);
			DebugPrint((">>>  KeCancelTimer Finished!\n"));
			for(j = 0;j < COM_NUM;j++)
			{
				pLDI->Inited[j] = FALSE;
			}
            Status = STATUS_SUCCESS;
            break;
        case IRP_MJ_DEVICE_CONTROL:
            //  Dispatch on IOCTL
//			DebugPrint ((">>>  IRP_MJ_DEVICE_CONTROL\n"));

            switch (pIrpStack->Parameters.DeviceIoControl.IoControlCode)//pIrpStack->MajorFunction 如果是 IRP_MJ_DEVICE_CONTROL
				//则需要查看 pIrpStack->Parameters.DeviceIoControl.IoControlCode 的代码的值,再分别进行处理
            {
            case IOCTL_GPD_READ_PORT_UCHAR:
            case IOCTL_GPD_READ_PORT_USHORT:
            case IOCTL_GPD_READ_PORT_ULONG:
				DebugPrint ((">>>  IOCTL_GPD_READ_PORT_UCHAR\n"));
                Status = GpdIoctlReadPort(
                            pLDI,
                            pIrp,
                            pIrpStack,
                            pIrpStack->Parameters.DeviceIoControl.IoControlCode
                            );
               break;

            case IOCTL_GPD_WRITE_PORT_UCHAR:
            case IOCTL_GPD_WRITE_PORT_USHORT:
            case IOCTL_GPD_WRITE_PORT_ULONG:
				DebugPrint ((">>>  IOCTL_GPD_WRITE_PORT_UCHAR TestIsr = %d\n",pLDI->TestIsr));
                Status = GpdIoctlWritePort(
                            pLDI, 
                            pIrp,
                            pIrpStack,
                            pIrpStack->Parameters.DeviceIoControl.IoControlCode
                            );
				CurrentISR = READ_INTERRUPT_STATE_REG((ULONG_PTR)pLDI->PortBase);
				DebugPrint (("W CurrentISR = %X\n",CurrentISR));
                break;
			case IOCTL_SET_BAUD_MODE://设置串口波特率,和工作方式
				DebugPrint ((">>>  IOCTL_SET_BAUD_MODE \n"));
				Status = SerialSetBaud(pDO,pIrp,pIrpStack);
				break;
			case IOCTL_READ_DATA://读串口0的数据
//				DebugPrint ((">>>  IOCTL_READ_DATA \n"));
				Status = SerialReadData(pDO,pIrp,pIrpStack,0);
				break;
			case IOCTL_WRITE_DATA://写串口0的数据
//				DebugPrint ((">>>  IOCTL_WRITE_DATA \n"));
				Status = SerialWriteData(pDO,pIrp,pIrpStack,0);
				break;
			case IOCTL_READ_DATA_1://读串口1的数据
//				DebugPrint ((">>>  IOCTL_READ_DATA_1 \n"));
				Status = SerialReadData(pDO,pIrp,pIrpStack,1);
				break;
			case IOCTL_WRITE_DATA_1://写串口1的数据
//				DebugPrint ((">>>  IOCTL_WRITE_DATA_1 \n"));
				Status = SerialWriteData(pDO,pIrp,pIrpStack,1);
				break;
			////////////////////////////////
			case IOCTL_READ_DATA_2://读串口2的数据
//				DebugPrint ((">>>  IOCTL_READ_DATA_2 \n"));
				Status = SerialReadData(pDO,pIrp,pIrpStack,2);
				break;
			case IOCTL_WRITE_DATA_2://写串口2的数据
//				DebugPrint ((">>>  IOCTL_WRITE_DATA_2 \n"));
				Status = SerialWriteData(pDO,pIrp,pIrpStack,2);
				break;
			case IOCTL_READ_DATA_3://读串口3的数据
//				DebugPrint ((">>>  IOCTL_READ_DATA_3 \n"));
				Status = SerialReadData(pDO,pIrp,pIrpStack,3);
				break;
			case IOCTL_WRITE_DATA_3://写串口3的数据
//				DebugPrint ((">>>  IOCTL_WRITE_DATA_3 \n"));
				Status = SerialWriteData(pDO,pIrp,pIrpStack,3);
				break;
			case IOCTL_READ_DATA_4://读串口4的数据
//				DebugPrint ((">>>  IOCTL_READ_DATA_4 \n"));
				Status = SerialReadData(pDO,pIrp,pIrpStack,4);
				break;
			case IOCTL_WRITE_DATA_4://写串口4的数据
//				DebugPrint ((">>>  IOCTL_WRITE_DATA_4 \n"));
				Status = SerialWriteData(pDO,pIrp,pIrpStack,4);
				break;
			case IOCTL_READ_DATA_5://读串口5的数据
//				DebugPrint ((">>>  IOCTL_READ_DATA_5 \n"));
				Status = SerialReadData(pDO,pIrp,pIrpStack,5);
				break;
			case IOCTL_WRITE_DATA_5://写串口5的数据
//				DebugPrint ((">>>  IOCTL_WRITE_DATA_5 \n"));
				Status = SerialWriteData(pDO,pIrp,pIrpStack,5);
				break;
			case IOCTL_READ_DATA_6://读串口6的数据
//				DebugPrint ((">>>  IOCTL_READ_DATA_6 \n"));
				Status = SerialReadData(pDO,pIrp,pIrpStack,6);
				break;
			case IOCTL_WRITE_DATA_6://写串口6的数据
//				DebugPrint ((">>>  IOCTL_WRITE_DATA_6 \n"));
				Status = SerialWriteData(pDO,pIrp,pIrpStack,6);
				break;
			case IOCTL_READ_DATA_7://读串口7的数据
//				DebugPrint ((">>>  IOCTL_READ_DATA_7 \n"));
				Status = SerialReadData(pDO,pIrp,pIrpStack,7);
				break;
			case IOCTL_WRITE_DATA_7://写串口7的数据
//				DebugPrint ((">>>  IOCTL_WRITE_DATA_7 \n"));
				Status = SerialWriteData(pDO,pIrp,pIrpStack,7);
				break;
			case IOCTL_READ_DATA_8://读串口7的数据
//				DebugPrint ((">>>  IOCTL_READ_DATA_7 \n"));
				Status = SerialReadData(pDO,pIrp,pIrpStack,8);
				break;
			case IOCTL_WRITE_DATA_8://写串口7的数据
//				DebugPrint ((">>>  IOCTL_WRITE_DATA_7 \n"));
				Status = SerialWriteData(pDO,pIrp,pIrpStack,8);
				break;
			case IOCTL_READ_DATA_9://读串口7的数据
//				DebugPrint ((">>>  IOCTL_READ_DATA_7 \n"));
				Status = SerialReadData(pDO,pIrp,pIrpStack,9);
				break;
			case IOCTL_WRITE_DATA_9://写串口7的数据
//				DebugPrint ((">>>  IOCTL_WRITE_DATA_7 \n"));
				Status = SerialWriteData(pDO,pIrp,pIrpStack,9);
				break;
			case IOCTL_READ_DATA_10://读串口7的数据
//				DebugPrint ((">>>  IOCTL_READ_DATA_7 \n"));
				Status = SerialReadData(pDO,pIrp,pIrpStack,10);
				break;
			case IOCTL_WRITE_DATA_10://写串口7的数据
//				DebugPrint ((">>>  IOCTL_WRITE_DATA_7 \n"));
				Status = SerialWriteData(pDO,pIrp,pIrpStack,10);
				break;
			case IOCTL_READ_DATA_11://读串口7的数据
//				DebugPrint ((">>>  IOCTL_READ_DATA_7 \n"));
				Status = SerialReadData(pDO,pIrp,pIrpStack,11);
				break;
			case IOCTL_WRITE_DATA_11://写串口7的数据
//				DebugPrint ((">>>  IOCTL_WRITE_DATA_7 \n"));
				Status = SerialWriteData(pDO,pIrp,pIrpStack,11);
				break;
			////////////////////////////////
            default:      
				DebugPrint ((">>>  STATUS_INVALID_PARAMETER \n"));
                Status = STATUS_INVALID_PARAMETER;

            }
            break;
        default: 
            Status = STATUS_NOT_IMPLEMENTED;
            break;
    }

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

    // Don't boost priority when returning since this took little time.
    IoCompleteRequest(pIrp, IO_NO_INCREMENT );
    IoReleaseRemoveLock(&pLDI->RemoveLock, pIrp);       
    return Status;
}
//
BOOLEAN
GpdInterruptService(
    IN  PKINTERRUPT Interrupt,
    IN  PVOID       Extension
    )

/*++
      
Routine Description:
      
    This routine services the interrupt for the parallel port.
      This routine will call out to all of the interrupt routines
      that connected with this device via
      IOCTL_INTERNAL_PARALLEL_CONNECT_INTERRUPT in order until
      one of them returns TRUE.
      
Arguments:
      
    Interrupt   - Supplies the interrupt object.
      
    Extension   - Supplies the device extension.
      
Return Value:
      
    FALSE   - The interrupt was not handled.
    TRUE    - The interrupt was handled.
      

⌨️ 快捷键说明

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