StartIO例程------------IoStartPacket、IoStartNextPacket、IoCancelIrp实现代码

StartIO例程------------IoStartPacket、IoStartNextPacket、IoCancelIrp实现代码

根据wdk代码做的IoStartPacket、IoStartNextPacket、IoCancelIrp简化版:

VOID
IoStartPacket(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp,
    IN PULONG Key OPTIONAL,
    IN PDRIVER_CANCEL CancelFunction OPTIONAL
    )
/*++
Routine Description:
    This routine attempts to start the specified packet request (IRP) on the
    specified device.  If the device is already busy, then the packet is
    simply queued to the device queue. If a non-NULL CancelFunction is
    supplied, it will be put in the IRP.  If the IRP has been canceled, the
    CancelFunction will be called after the IRP has been inserted into the
    queue or made the current packet.

Arguments:
    DeviceObject - Pointer to device object itself.
    Irp - I/O Request Packet which should be started on the device.
    Key - Key to be used in inserting packet into device queue;  optional
        (if not specified, then packet is inserted at the tail).
    CancelFunction - Pointer to an optional cancel routine.

Return Value:
    None.
--*/

{
    KIRQL oldIrql;
    KIRQL cancelIrql = PASSIVE_LEVEL;
    BOOLEAN i;

    // Raise the IRQL of the processor to dispatch level for synchronization.

    KeRaiseIrql( DISPATCH_LEVEL, &oldIrql );

    // If the driver has indicated that packets are cancelable, then acquire
    // the cancel spinlock and set the address of the cancel function to
    // indicate that the packet is not only cancelable, but indicates what
    // routine to invoke should it be cancelled.

    if (CancelFunction) {
        IoAcquireCancelSpinLock( &cancelIrql );
        Irp->CancelRoutine = CancelFunction;
    }
    i = KeInsertDeviceQueue( &DeviceObject->DeviceQueue,
                             &Irp->Tail.Overlay.DeviceQueueEntry );

    // If the packet was not inserted into the queue, then this request is
    // now the current packet for this device.  Indicate so by storing its
    // address in the current IRP field, and begin processing the request.

    if (!i) {
        DeviceObject->CurrentIrp = Irp;
        if (CancelFunction) {
            IoReleaseCancelSpinLock( cancelIrql );
        }

        // Invoke the driver's start I/O routine to get the request going on the device.
        // The StartIo routine should handle the cancellation.

        DeviceObject->DriverObject->DriverStartIo( DeviceObject, Irp );

    } else {

        // The packet was successfully inserted into the device's work queue.
        // Make one last check to determine whether or not the packet has
        // already been marked cancelled.  If it has, then invoke the
        // driver's cancel routine now.  Note that because the cancel
        // spinlock is currently being held, an attempt to cancel the request
        // from another processor at this point will simply wait until this
        // routine is finished, and then get it cancelled.

        if (CancelFunction) {
            if (Irp->Cancel) {
                Irp->CancelIrql = cancelIrql;
                Irp->CancelRoutine = (PDRIVER_CANCEL) NULL;
                CancelFunction( DeviceObject, Irp );
            } else {
                IoReleaseCancelSpinLock( cancelIrql );
            }
        }
    }

    // Restore the IRQL back to its value upon entry to this function before
    // returning to the caller.

    KeLowerIrql( oldIrql );
}

 

VOID
IopStartNextPacket(
    IN PDEVICE_OBJECT DeviceObject,
    IN LOGICAL Cancelable
    )
/*++
Routine Description:

    This routine is invoked to dequeue the next packet (IRP) from the
    specified device work queue and invoke the device driver's start I/O
    routine for it.  If the Cancelable parameter is TRUE, then the update of
    current IRP is synchronized using the cancel spinlock.

Arguments:
    DeviceObject - Pointer to device object itself.
    Cancelable - Indicates that IRPs in the device queue may be cancelable.

Return Value:
    None.
--*/
{
    KIRQL cancelIrql = PASSIVE_LEVEL;
    PIRP irp;
    PKDEVICE_QUEUE_ENTRY packet;

    // Begin by checking to see whether or not this driver's requests are
    // to be considered cancelable.  If so, then acquire the cancel spinlock.

    if (Cancelable) {
        IoAcquireCancelSpinLock( &cancelIrql );
    }

    // Clear the current IRP field before starting another request.

    DeviceObject->CurrentIrp = (PIRP) NULL;

    // Remove the next packet from the head of the queue.  If a packet was
    // found, then process it.

    packet = KeRemoveDeviceQueue( &DeviceObject->DeviceQueue );

    if (packet) {
        irp = CONTAINING_RECORD( packet, IRP, Tail.Overlay.DeviceQueueEntry );

        // A packet was located so make it the current packet for this
        // device.

        DeviceObject->CurrentIrp = irp;
        if (Cancelable) {     
           IoReleaseCancelSpinLock( cancelIrql );
        }

        // Invoke the driver's start I/O routine for this packet.

        DeviceObject->DriverObject->DriverStartIo( DeviceObject, irp );
    } else {

        // No packet was found, so simply release the cancel spinlock if
        // it was acquired.

        if (Cancelable) {
           IoReleaseCancelSpinLock( cancelIrql );
        }
    }
}

 

BOOLEAN
IoCancelIrp(
    IN PIRP Irp
    )
{
    PDRIVER_CANCEL cancelRoutine;
    KIRQL irql;
    BOOLEAN returnValue;

    ASSERT( Irp->Type == IO_TYPE_IRP );

    // Acquire the cancel spin lock.

    IoAcquireCancelSpinLock( &irql );

    // Set the cancel flag in the IRP.

    Irp->Cancel = TRUE;

    // Obtain the address of the cancel routine, and if one was specified,
    // invoke it.

    cancelRoutine = (PDRIVER_CANCEL) (ULONG_PTR) InterlockedExchangePointer( (PVOID *) &Irp->CancelRoutine, NULL );
    if (cancelRoutine) {
        Irp->CancelIrql = irql;
        cancelRoutine( Irp->Tail.Overlay.CurrentStackLocation->DeviceObject,
                       Irp );
        // The cancel spinlock should have been released by the cancel routine.

        return(TRUE);
    } else {

        // There was no cancel routine, so release the cancel spinlock and
        // return indicating the Irp was not currently cancelable.

        IoReleaseCancelSpinLock( irql );
        return(FALSE);
    }
}

#define IoSetCancelRoutine( Irp, NewCancelRoutine ) (  /
    (PDRIVER_CANCEL) (ULONG_PTR) InterlockedExchangePointer( (PVOID *) &(Irp)->CancelRoutine, (PVOID) (ULONG_PTR)(NewCancelRoutine) ) )

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值