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

📄 miniport.c

📁 James Antognini和Tom Divine提供的PASSTHRU的编成实例。
💻 C
📖 第 1 页 / 共 5 页
字号:
            else
            {
                //
                // The driver cannot allocate a packet.
                // 
                ADAPT_DECR_PENDING_SENDS(pAdapt);
            }
        }                                             // End 'do' not NDIS 5.1.  ja, 04.10.2003.
        while (FALSE);

        if (Status != NDIS_STATUS_PENDING)
        {
            NdisMSendComplete(ADAPT_MINIPORT_HANDLE(pAdapt),
                              Packet,
                              Status);
        }
    }                                                 // End 'for' handle packets.  ja, 04.10.2003.
}


NDIS_STATUS
MPQueryInformation(
    IN NDIS_HANDLE                MiniportAdapterContext,
    IN NDIS_OID                   Oid,
    IN PVOID                      InformationBuffer,
    IN ULONG                      InformationBufferLength,
    OUT PULONG                    BytesWritten,
    OUT PULONG                    BytesNeeded
    )
/*++

Routine Description:

    Entry point called by NDIS to query for the value of the specified OID.
    Typical processing is to forward the query down to the underlying miniport.

    The following OIDs are filtered here:

    OID_PNP_QUERY_POWER - return success right here

    OID_GEN_SUPPORTED_GUIDS - do not forward, otherwise we will show up
    multiple instances of private GUIDs supported by the underlying miniport.

    OID_PNP_CAPABILITIES - we do send this down to the lower miniport, but
    the values returned are postprocessed before we complete this request;
    see PtRequestComplete.

    NOTE on OID_TCP_TASK_OFFLOAD - if this IM driver modifies the contents
    of data it passes through such that a lower miniport may not be able
    to perform TCP task offload, then it should not forward this OID down,
    but fail it here with the status NDIS_STATUS_NOT_SUPPORTED. This is to
    avoid performing incorrect transformations on data.

    If our miniport edge (upper edge) is at a low-power state, fail the request.

    If our protocol edge (lower edge) has been notified of a low-power state,
    we pend this request until the miniport below has been set to D0. Since
    requests to miniports are serialized always, at most a single request will
    be pended.

Arguments:

    MiniportAdapterContext    Pointer to the adapter structure
    Oid                       Oid for this query
    InformationBuffer         Buffer for information
    InformationBufferLength   Size of this buffer
    BytesWritten              Specifies how much info is written
    BytesNeeded               In case the buffer is smaller than what we need, tell them how much is needed


Return Value:

    Return code from the NdisRequest below.

--*/
{
    PADAPT        pAdapt = (PADAPT)MiniportAdapterContext;
    NDIS_STATUS   Status = NDIS_STATUS_FAILURE;
    ULONG         ulInfoLen;                          // ja, 14.09.2003.    

    do
    {
        if (Oid == OID_PNP_QUERY_POWER)
        {
            //
            //  Do not forward this.
            //
            Status = NDIS_STATUS_SUCCESS;
            break;
        }

        if (Oid == OID_GEN_SUPPORTED_GUIDS)
          {                                           // ja, 28.09.2003.
           DBGPRINT(("MPQueryInformation called\n"));

           ulInfoLen = sizeof(NICGuidList);

           if (ulInfoLen>InformationBufferLength)
             {
              *BytesNeeded = ulInfoLen;
              Status = NDIS_STATUS_BUFFER_TOO_SHORT;
              break;
             }

           *BytesWritten = ulInfoLen;
           NdisMoveMemory(InformationBuffer, NICGuidList, ulInfoLen);
           Status = NDIS_STATUS_SUCCESS;
           break;
          }

        if (OID_CUSTOM_DRIVER_STATISTICS==Oid)        // Query statistics?  ja, 30.11.2003.
          {
           // In the author's experience, NDIS reports WMI data to user-space routines preceded by a 4-byte count of the
           // number of actual data fields.  This is true whether a particular MOF class begins with a "count" property (as
           // with Count in the PassthruStatistics class) or not. Since NDIS so behaves, the MOF file does in fact have
           // each class begin with a count property so that user-space programs "know" to expect the count field at the
           // beginning of WMI data.
           
           // (Apparently NDIS determines the count field it returns by dividing the number of bytes returned (BytesWritten
           // points to that) by the size of an element as reported in the third member (Size) of the NDIS_GUID structure 
           // pertaining to the GUID/OID.  For example, if the size returned is 24 (6 ULONGs) and the element size is 4 (a
           // ULONG), the count field will contain 6.)

           // It is programmatically convenient to be able to keep the WMI values in the adapter structure (ADAPT) in an
           // order different from the order laid down by the PassthruStatistics class in the .mof file.  Since the class
           // begins with the Count property and since NDIS will compute the property's value (see above), a bit of program-
           // ming trickery is employed here:  The output pointer is made to begin 4 bytes earlier (size of Count, namely, 
           // the size of a ULONG) than that given by InformationBuffer.
           
           PPassthruStatistics pOut =                 // Get a convenient pointer.
             (PPassthruStatistics)((PUCHAR)InformationBuffer - PassthruStatistics_Count_SIZE);

           ulInfoLen = sizeof(PassthruStatistics) - PassthruStatistics_Count_SIZE;

           if (ulInfoLen>InformationBufferLength)
             {
              *BytesNeeded = ulInfoLen;
              Status = NDIS_STATUS_BUFFER_TOO_SHORT;
              break;
             }

           *BytesWritten = ulInfoLen;

           pOut->MPSendPktsSeen    = pAdapt->PassThruStats.MPSendPktsSeen;
           pOut->MPSendPktsDropped = pAdapt->PassThruStats.MPSendPktsDropped;
           pOut->PTRcvSeen         = pAdapt->PassThruStats.PTRcvSeen;
           pOut->PTRcvDropped      = pAdapt->PassThruStats.PTRcvDropped;
           pOut->PTRcvPktsSeen     = pAdapt->PassThruStats.PTRcvPktSeen;
           pOut->PTRcvPktsDropped  = pAdapt->PassThruStats.PTRcvPktDropped;

           Status = NDIS_STATUS_SUCCESS;
           break;
          }

        if (OID_CUSTOM_ARRAY==Oid)                    // Query custom array?  ja, 28.09.2003.
          {
           ULONG const          LoopLim = 5,
                                szMem = 1024;
           ULONG                i,
                                ulUsed;
           PPassthruIPAddrArray pAddrArr;
           NDIS_STATUS          lclStatus;
           BOOLEAN              bGotStorage = FALSE;
           union
             {
              PassthruIPAddrArray lclIPArray;
              char                stuff[256];
             }
              xyz;

           for (i = 0; i < LoopLim; i ++)             // Try several times to get current address array, with ever-increasing amounts of working storage.
             {
              if (0==i)                               // First try?
                {
                 pAddrArr =                           // Use working storage on the stack.
                   (PPassthruIPAddrArray)&xyz;
                 ulUsed = sizeof(xyz);
                }
              else
                {                                     // Use dynamically obtained storage.
                 ulUsed = i*szMem;

                 lclStatus =                          // Get working storage.
                   NdisAllocateMemoryWithTag(&pAddrArr, ulUsed, TAG);

                 if (NDIS_STATUS_FAILURE==lclStatus)  // A problem?
                   {
                    DBGPRINT(("MPQueryInformation():  Failed to get memory for IP address array for adapter at 0x%08x!\n", pAdapt));
                    Status = NDIS_STATUS_RESOURCES;
                    *BytesNeeded = 0;
                    *BytesWritten = 0;
                    goto DoneOidQuery;
                   }

                 bGotStorage = TRUE;
                }

              lclStatus =                             // Try to get copy of address array.
                PassthruWMIGetAddrArray(
                                        pAdapt,
                                        &ulUsed,
                                        pAddrArr
                                       );

              if (NDIS_STATUS_SUCCESS==lclStatus)     // Success?
                break;

              if (i+1==LoopLim)                       // At loop limit?
                {
                 DBGPRINT(("MPQueryInformation():  Failed to get array last time, status 0x%08x!\n", lclStatus));
                 Status = NDIS_STATUS_FAILURE;
                 *BytesNeeded = 0;
                 *BytesWritten = 0;
                 goto DoneOidQuery;
                }

              if (TRUE==bGotStorage)                  // Got working storage?
                {
                 NdisFreeMemory(pAddrArr, ulUsed, 0);
                 bGotStorage = FALSE;
                }
             }                                        // End 'for' try several times to get current address array.

           ulInfoLen =                                // Get required size.
             pAddrArr->NumberElements * sizeof(pAddrArr->IPAddrArray);

//         DBGPRINT(("MPQueryInformation():  For adapter at 0x%08x, Oid = OID_CUSTOM_ARRAY needed = %d, given = %d\n",
//                   pAdapt, ulInfoLen, InformationBufferLength));

           if (ulInfoLen>InformationBufferLength)     // Too big for area supplied by NDIS?
             {
              *BytesNeeded = ulInfoLen;
              Status = NDIS_STATUS_BUFFER_TOO_SHORT;
              goto DoneOidQuery;
             }

           NdisMoveMemory(InformationBuffer,          // Copy elements.
                          &pAddrArr->IPAddrArray,
                          ulInfoLen
                         );
           *BytesWritten = ulInfoLen;
           Status = NDIS_STATUS_SUCCESS;

DoneOidQuery:
           if (TRUE==bGotStorage)                     // Got working storage?
             NdisFreeMemory(pAddrArr, ulUsed, 0);
           break;
          }                                           // End 'if' query custom array.

        if (Oid == OID_TCP_TASK_OFFLOAD)
        {
            //
            // Fail this -if- this driver performs data transformations
            // that can interfere with a lower driver's ability to offload
            // TCP tasks.
            //
            // Status = NDIS_STATUS_NOT_SUPPORTED;
            // break;
            //
        }
        //
        // If the miniport below is unbinding, just fail any request
        //
        NdisAcquireSpinLock(&pAdapt->Lock);
        if (pAdapt->UnbindingInProcess == TRUE)
        {
            NdisReleaseSpinLock(&pAdapt->Lock);
            Status = NDIS_STATUS_FAILURE;
            break;
        }
        NdisReleaseSpinLock(&pAdapt->Lock);
        //
        // All other queries are failed, if the miniport is not at D0,
        //
        if (pAdapt->MPDeviceState > NdisDeviceStateD0) 
        {
            Status = NDIS_STATUS_FAILURE;
            break;
        }

        pAdapt->Request.RequestType = NdisRequestQueryInformation;
        pAdapt->Request.DATA.QUERY_INFORMATION.Oid = Oid;
        pAdapt->Request.DATA.QUERY_INFORMATION.InformationBuffer = InformationBuffer;
        pAdapt->Request.DATA.QUERY_INFORMATION.InformationBufferLength = InformationBufferLength;
        pAdapt->BytesNeeded = BytesNeeded;
        pAdapt->BytesReadOrWritten = BytesWritten;

        //
        // If the miniport below is binding, fail the request
        //
        NdisAcquireSpinLock(&pAdapt->Lock);
            
        if (pAdapt->UnbindingInProcess == TRUE)
        {
            NdisReleaseSpinLock(&pAdapt->Lock);
            Status = NDIS_STATUS_FAILURE;
            break;
        }
        //
        // If the Protocol device state is OFF, mark this request as being 
        // pended. We queue this until the device state is back to D0. 
        //
        if ((pAdapt->PTDeviceState > NdisDeviceStateD0) 
                && (pAdapt->StandingBy == FALSE))
        {
            pAdapt->QueuedRequest = TRUE;
            NdisReleaseSpinLock(&pAdapt->Lock);
            Status = NDIS_STATUS_PENDING;
            break;
        }
        //
        // This is in the process of powering down the system, always fail the request
        // 
        if (pAdapt->StandingBy == TRUE)
        {
            NdisReleaseSpinLock(&pAdapt->Lock);
            Status = NDIS_STATUS_FAILURE;
            break;
        }
        pAdapt->OutstandingRequests = TRUE;
        
        NdisReleaseSpinLock(&pAdapt->Lock);

        //
        // default case, most requests will be passed to the miniport below
        //
        NdisRequest(&Status,
                    pAdapt->BindingHandle,
                    &pAdapt->Request);


        if (Status != NDIS_STATUS_PENDING)
        {
            PtRequestComplete(pAdapt, &pAdapt->Request, Status);
            Status = NDIS_STATUS_PENDING;
        }

    } while (FALSE);

    return(Status);

}


VOID
MPQueryPNPCapabilities(
    IN OUT PADAPT            pAdapt,
    OUT PNDIS_STATUS         pStatus
    )
/*++

Routine Description:

    Postprocess a request for OID_PNP_CAPABILITIES that was forwarded
    down to the underlying miniport, and has been completed by it.

Arguments:

    pAdapt - Pointer to the adapter structure

⌨️ 快捷键说明

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