waitmask.cpp

来自「这个是串口驱动程序开发包」· C++ 代码 · 共 442 行

CPP
442
字号
/*++

Abstract:

    This module contains the code that is very specific to get/set/wait
    on event mask operations in the serial driver

--*/

#include "precomp.h"

NTSTATUS KdSerialDevice::StartMask()

/*++

Routine Description:

    This routine is used to process the set mask and wait
    mask ioctls.  Calls to this routine are serialized by
    placing irps in the list under the protection of the
    cancel spin lock.

Return Value:

    Will return pending for everything put the first
    request that we actually process.  Even in that
    case it will return pending unless it can complete
    it right away.


--*/

{
    // The current stack location.  This contains much of the
    // information we need to process this particular request.
    KdIrp NewIrp;

    BOOLEAN SetFirstStatus = FALSE;
    NTSTATUS FirstStatus;

    DebugDump(DBG_DIAG3, ("In SerialStartMask\n") );

    ASSERT(m_CurrentMaskIrp);

    do {

        DebugDump(DBG_DIAG4, ("STARMASK - CurrentMaskIrp: %x\n",m_CurrentMaskIrp) );

        ASSERT((m_CurrentMaskIrp.IoctlCode()==IOCTL_SERIAL_WAIT_ON_MASK) ||
               (m_CurrentMaskIrp.IoctlCode()==IOCTL_SERIAL_SET_WAIT_MASK));

        if (m_CurrentMaskIrp.IoctlCode()==IOCTL_SERIAL_SET_WAIT_MASK) 
        {
            DebugDump(DBG_DIAG4, (" %x is a SETMASK irp\n",m_CurrentMaskIrp) );
            // Complete the old wait if there is one.
            m_KdInterrupt.SynchronizeExecution((KDIRQ_SYNC_CALLBACK) FinishOldWait);

            // Any current waits should be on its way to completion
            // at this point.  There certainly shouldn't be any
            // irp mask location.
            ASSERT(!m_IrpMaskLocation);

            m_CurrentMaskIrp.Status() = STATUS_SUCCESS;

            if (!SetFirstStatus) 
            {
                DebugDump(DBG_DIAG4, ("%x was the first irp processed by this\n"
                    "------- invocation of startmask\n",m_CurrentMaskIrp) );
                FirstStatus = STATUS_SUCCESS;
                SetFirstStatus = TRUE;
            }

            // The following call will also cause the current
            // call to be completed.
            GetNextIrp(
                &m_CurrentMaskIrp,
                &m_MaskQueue,
                &NewIrp,
                TRUE
                );
            DebugDump(DBG_DIAG4, ("Perhaps another mask irp was found in the queue\n"
                "------- %x/%x <- values should be the same\n", m_CurrentMaskIrp,NewIrp) );
        } 
        else 
        {
            // First make sure that we have a non-zero mask.
            // If the app queues a wait on a zero mask it can't
            // be statisfied so it makes no sense to start it.
            if ((!m_IsrWaitMask) || (m_CurrentWaitIrp)) 
            {
                DebugDump(DBG_DIAG4, ("WaitIrp is invalid\n"
                     "------- IsrWaitMask: %x\n"
                     "------- CurrentWaitIrp: %x\n",
                     m_IsrWaitMask,
                     m_CurrentWaitIrp) );

                m_CurrentMaskIrp.Status() = STATUS_INVALID_PARAMETER;

                if (!SetFirstStatus) 
                {
                    DebugDump(DBG_DIAG4, ("%x was the first irp processed by this\n"
                         "------- invocation of startmask\n",m_CurrentMaskIrp) );
                    FirstStatus = STATUS_INVALID_PARAMETER;
                    SetFirstStatus = TRUE;
                }

                GetNextIrp(
                    &m_CurrentMaskIrp,
                    &m_MaskQueue,
                    &NewIrp,
                    TRUE
                    );
                DebugDump(DBG_DIAG4, ("Perhaps another mask irp was found in the queue\n"
                     "------- %x/%x <- values should be the same\n",
                     m_CurrentMaskIrp,NewIrp) );
            } 
            else 
            {
                KIRQL OldIrql;

                // Make the current mask irp the current wait irp and
                // get a new current mask irp.  Note that when we get
                // the new current mask irp we DO NOT complete the
                // old current mask irp (which is now the current wait
                // irp.
                //
                // Then under the protection of the cancel spin lock
                // we check to see if the current wait irp needs to
                // be canceled

                IoAcquireCancelSpinLock(&OldIrql);

                if (m_CurrentMaskIrp->Cancel) 
                {
                    DebugDump(DBG_DIAG4, ("%x irp was already marked as cancelled\n",
                         m_CurrentMaskIrp) );
                    IoReleaseCancelSpinLock(OldIrql);
                    m_CurrentMaskIrp.Status() = STATUS_CANCELLED;

                    if (!SetFirstStatus) {

                        DebugDump(DBG_DIAG4, ("%x was the first irp processed by this\n"
                             "------- invocation of startmask\n",m_CurrentMaskIrp) );
                        FirstStatus = STATUS_CANCELLED;
                        SetFirstStatus = TRUE;
                    }

                    GetNextIrp(
                        &m_CurrentMaskIrp,
                        &m_MaskQueue,
                        &NewIrp,
                        TRUE
                        );
                    DebugDump(DBG_DIAG4, ("Perhaps another mask irp was found in the queue\n"
                         "------- %x/%x <- values should be the same\n",
                         m_CurrentMaskIrp,NewIrp) );
                }
                else
                {
                    DebugDump(DBG_DIAG4, ("%x will become the current wait irp\n", m_CurrentMaskIrp) );
                    if (!SetFirstStatus) 
                    {
                        DebugDump(DBG_DIAG4, ("%x was the first irp processed by this\n"
                             "------- invocation of startmask\n",m_CurrentMaskIrp) );
                        FirstStatus = STATUS_PENDING;
                        SetFirstStatus = TRUE;

                        // If we haven't already set a first status
                        // then there is a chance that this packet
                        // was never on the queue.  We should mark
                        // it as pending.

                        m_CurrentMaskIrp.MarkPending();
                    }

                    // There should never be a mask location when
                    // there isn't a current wait irp.  At this point
                    // there shouldn't be a current wait irp also.

                    ASSERT(!m_IrpMaskLocation);
                    ASSERT(!m_CurrentWaitIrp);

                    m_CurrentWaitIrp = m_CurrentMaskIrp;
                    SERIAL_INIT_REFERENCE(m_CurrentWaitIrp);
                    SET_IRP_CANCEL_ROUTINE(m_CurrentWaitIrp, CancelWait);

                    // Since the cancel routine has a reference to
                    // the irp we need to update the reference
                    // count.

                    SERIAL_SET_REFERENCE(m_CurrentWaitIrp, SERIAL_REF_CANCEL);

                    m_KdInterrupt.SynchronizeExecution((KDIRQ_SYNC_CALLBACK) GiveWaitToIsr);

                    // Since it isn't really the mask irp anymore,
                    // null out that pointer.

                    m_CurrentMaskIrp = (PIRP) NULL;

                    IoReleaseCancelSpinLock(OldIrql);

                    GetNextIrp(
                        &m_CurrentMaskIrp,
                        &m_MaskQueue,
                        &NewIrp,
                        FALSE
                        );
                    DebugDump(DBG_DIAG4, ("Perhaps another mask irp was found in the queue\n"
                         "------- %x/%x <- values should be the same\n",
                         m_CurrentMaskIrp,NewIrp) );
                }
            }
        }
    } while (NewIrp);

    return FirstStatus;
}

BOOLEAN KdSerialDevice::GrabWaitFromIsr()

/*++

Routine Description:

    This routine will check to see if the ISR still knows about
    a wait irp by checking to see if the IrpMaskLocation is non-null.
    If it is then it will zero the Irpmasklocation (which in effect
    grabs the irp away from the isr).  This routine is only called
    buy the cancel code for the wait.

    NOTE: This is called by KeSynchronizeExecution.

Return Value:

    Always FALSE.

--*/

{
    DebugDump(DBG_DIAG3, ("In SerialGrabWaitFromIsr\n") );

    if (m_IrpMaskLocation)
    {
        DebugDump(DBG_DIAG4, ("The isr still owns the irp %x, mask location is %x\n"
             "------- and system buffer is %x\n",
             m_CurrentWaitIrp,m_IrpMaskLocation,
             m_CurrentWaitIrp.SystemBuffer()) );

        // The isr still "owns" the irp.

        *m_IrpMaskLocation = 0;
        m_IrpMaskLocation = NULL;

        m_CurrentWaitIrp.Information() = sizeof(ULONG);

        // Since the isr no longer references the irp we need to
        // decrement the reference count.

        SERIAL_CLEAR_REFERENCE(
            m_CurrentWaitIrp,
            SERIAL_REF_ISR
            );
    }
    return FALSE;
}

BOOLEAN KdSerialDevice::GiveWaitToIsr()

/*++

Routine Description:

    This routine simply sets a variable in the device 
    so that the isr knows that we have a wait irp.

    NOTE: This is called by KeSynchronizeExecution.

    NOTE: This routine assumes that it is called with the
          cancel spinlock held.

Return Value:

    Always FALSE.

--*/

{
    DebugDump(DBG_DIAG3, ("In SerialGiveWaitToIsr\n") );
    // There certainly shouldn't be a current mask location at
    // this point since we have a new current wait irp.

    ASSERT(!m_IrpMaskLocation);

    // The isr may or may not actually reference this irp.  It
    // won't if the wait can be satisfied immediately.  However,
    // since it will then go through the normal completion sequence,
    // we need to have an incremented reference count anyway.

    SERIAL_SET_REFERENCE(
        m_CurrentWaitIrp,
        SERIAL_REF_ISR
        );

    if (!m_HistoryMask) 
    {
        DebugDump(DBG_DIAG4, ("No events occured prior to the wait call\n") );

        // Although this wait might not be for empty transmit
        // queue, it doesn't hurt anything to set it to false.

        m_EmptiedTransmit = FALSE;

        // Record where the "completion mask" should be set.

        m_IrpMaskLocation = (PULONG)
            m_CurrentWaitIrp.SystemBuffer();
        DebugDump(DBG_DIAG4, ("The isr owns the irp %x, mask location is %x\n"
             "------- and system buffer is %x\n",
             m_CurrentWaitIrp,m_IrpMaskLocation,
             m_CurrentWaitIrp.SystemBuffer()) );
    }
    else
    {
        DebugDump(DBG_DIAG4,("%x occurred prior to the wait - starting the\n"
             "------- completion code for %x\n",
             m_HistoryMask,m_CurrentWaitIrp) );
        *((PULONG)m_CurrentWaitIrp.SystemBuffer()) =
            m_HistoryMask;
        m_HistoryMask = 0;
        m_CurrentWaitIrp.Information() = sizeof(ULONG);
        m_CurrentWaitIrp.Status() = STATUS_SUCCESS;
        m_KdDpc_CommWait.InsertQueue();
    }
    return FALSE;
}

BOOLEAN KdSerialDevice::FinishOldWait()

/*++

Routine Description:

    This routine will check to see if the ISR still knows about
    a wait irp by checking to see if the Irpmasklocation is non-null.
    If it is then it will zero the Irpmasklocation (which in effect
    grabs the irp away from the isr).  This routine is only called
    buy the cancel code for the wait.

    NOTE: This is called by KeSynchronizeExecution.

Return Value:

    Always FALSE.

--*/

{
    DebugDump(DBG_DIAG3, ("In SerialFinishOldWait\n") );
    if (m_IrpMaskLocation)
    {
        DebugDump(DBG_DIAG4, ("The isr still owns the irp %x, mask location is %x\n"
             "------- and system buffer is %x\n",
             m_CurrentWaitIrp,m_IrpMaskLocation,
             m_CurrentWaitIrp.SystemBuffer()) );
        // The isr still "owns" the irp.

        *m_IrpMaskLocation = 0;
        m_IrpMaskLocation = NULL;

        m_CurrentWaitIrp.Information() = sizeof(ULONG);

        // We don't decrement the reference since the completion routine
        // will do that.
        m_KdDpc_CommWait.InsertQueue();
    }

    // Don't wipe out any historical data we are still interested in.

    m_HistoryMask &= *((PULONG)m_CurrentMaskIrp.SystemBuffer());

    m_IsrWaitMask = *((PULONG)m_CurrentMaskIrp.SystemBuffer());
    DebugDump(DBG_DIAG4, ("Set mask location of %x, in irp %x, with system buffer of %x\n",
         m_IrpMaskLocation,
         m_CurrentMaskIrp,m_CurrentMaskIrp.SystemBuffer()) );
    return FALSE;
}

VOID KdSerialDevice::CancelWait(KdIrp Irp)
/*++
Routine Description:
    This routine is used to cancel a irp that is waiting on
    a comm event.

Arguments:
    DeviceObject - Pointer to the device object for this device
    Irp - Pointer to the IRP for the current request
--*/
{
    DebugDump(DBG_DIAG3, ("In CancelWait\n") );

    DebugDump(DBG_DIAG4, ("Canceling wait for irp %x\n",m_CurrentWaitIrp) );
    TryToCompleteCurrent(
        (KDIRQ_SYNC_CALLBACK) GrabWaitFromIsr,
        Irp->CancelIrql,
        STATUS_CANCELLED,
        &m_CurrentWaitIrp,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        SERIAL_REF_CANCEL
        );
}

VOID KdSerialDevice::CompleteWait(
    IN PKdDpc Dpc,
    IN PVOID SystemContext1,
    IN PVOID SystemContext2
    )
{
    KIRQL OldIrql;

    DebugDump(DBG_DIAG3, ("In SerialCompleteWait\n") );

    IoAcquireCancelSpinLock(&OldIrql);

    DebugDump(DBG_DIAG4, ("Completing wait for irp %x\n",m_CurrentWaitIrp) );
    TryToCompleteCurrent(
        NULL,
        OldIrql,
        STATUS_SUCCESS,
        &m_CurrentWaitIrp,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        SERIAL_REF_ISR
        );
}

⌨️ 快捷键说明

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