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

📄 miniport.c

📁 James Antognini和Tom Divine提供的PASSTHRU的编成实例。
💻 C
📖 第 1 页 / 共 5 页
字号:
/*++

Copyright (c) 1992-2000  Microsoft Corporation

Module Name:

    miniport.c

Abstract:

    Ndis Intermediate Miniport driver sample. This is a passthru driver.

Author:

Environment:


Revision History:


--*/


#include "precomp.h"
#pragma hdrstop
#include "PktHdr.h"                                   // ja, 28.09.2003.
#include "WMIFunc.h"                                  // ja, 28.09.2003.

#define JADrvNm             "JApassthru"              // ja, 04.10.2003.
#define JADrvRtnName        "miniport"                // ja, 04.10.2003.
#define JADrvRtnVer         "1.03"                    // ja, 04.10.2003.

#define CompDateTimeStr "dd mmm yyyy hh:mm:ss"        // ja, 29.11.2003.
                                                    
char DrvCompileInfo[sizeof(CompDateTimeStr)+1];       // ja, 29.11.2003.

#pragma comment(exestr,                               /* ja, 04.10.2003. */  \
                JADrvNm " " JADrvRtnName " v" JADrvRtnVer " compiled on " __DATE__ " at " __TIME__)

#define CUSTOM_DRIVER_SET_MIN   0x1                   // ja, 28.09.2003.
#define CUSTOM_DRIVER_SET_MAX   0xFFFFFF              

#define NIC_NUM_CUSTOM_GUIDS    2                                                                                      
                                                                                                                       
#ifndef NDIS51                                                                                                         
                                                                                                                       
static const NDIS_GUID NICGuidList[NIC_NUM_CUSTOM_GUIDS] =                                                             
  {                                                                                                                    
                                                                                                                       
    { // {5635DE7F-44E4-4dd6-B2A8-2A2A888A49B3}                                                  
     PassthruStatisticsGuid,                                                                                           
     OID_CUSTOM_DRIVER_STATISTICS,
     sizeof(ULONG),                                                                                                    
     (fNDIS_GUID_TO_OID )                                                                                              
    },                                                                                                                 
    { // {5635DE81-44E4-4dd6-B2A8-2A2A888A49B3} example of an array query                                              
     PassthruIPAddrArrayGuid,                                                                                          
     OID_CUSTOM_ARRAY,                                                                                                 
     sizeof(ULONG),                                   // Size is size of each element in the array.                    
     (fNDIS_GUID_TO_OID | fNDIS_GUID_ARRAY )                                                                           
    }                                                                                                                  
  };                                                                                                                   
                                                                                                                       
#else                                                                                                                  
                                                                                                                       
//                                                                                                                     
// Support for the fNDIS_GUID_ALLOW_READ flag has been added in WinXP for                                              
// both 5.0 and 5.1 miniports                                                                                          
//                                                                                                                     
static const NDIS_GUID NICGuidList[NIC_NUM_CUSTOM_GUIDS] =                                                             
  {                                                                                                                    
    { // {5635DE7F-44E4-4dd6-B2A8-2A2A888A49B3}                                                 
     PassthruStatisticsGuid,                                                                                           
     OID_CUSTOM_DRIVER_STATISTICS,
     sizeof(ULONG),                                                                                                    
     fNDIS_GUID_TO_OID | fNDIS_GUID_ARRAY | fNDIS_GUID_ALLOW_READ                                               
    },                                                                                                                 
    { // {5635DE81-44E4-4dd6-B2A8-2A2A888A49B3} example of an array query                                              
     PassthruIPAddrArrayGuid,                                                                                          
     OID_CUSTOM_ARRAY,                                                                                                 
     sizeof(ULONG),                                   // Size is size of each element in the array.                    
     // setting fNDIS_GUID_ALLOW_READ flag means that we allow any                                                     
     // user to query this value.                                                                                      
     fNDIS_GUID_TO_OID | fNDIS_GUID_ARRAY | fNDIS_GUID_ALLOW_READ | fNDIS_GUID_ALLOW_WRITE                              
    }                                                                                                                  
  };                                                                                                                   
                                                                                                                       
#endif                                                                                                                 

NDIS_STATUS
MPInitialize(
    OUT PNDIS_STATUS             OpenErrorStatus,
    OUT PUINT                    SelectedMediumIndex,
    IN  PNDIS_MEDIUM             MediumArray,
    IN  UINT                     MediumArraySize,
    IN  NDIS_HANDLE              MiniportAdapterHandle,
    IN  NDIS_HANDLE              WrapperConfigurationContext
    )
/*++

Routine Description:

    This is the initialize handler which gets called as a result of
    the BindAdapter handler calling NdisIMInitializeDeviceInstanceEx.
    The context parameter which we pass there is the adapter structure
    which we retrieve here.

    Arguments:

    OpenErrorStatus            Not used by us.
    SelectedMediumIndex        Place-holder for what media we are using
    MediumArray                Array of ndis media passed down to us to pick from
    MediumArraySize            Size of the array
    MiniportAdapterHandle    The handle NDIS uses to refer to us
    WrapperConfigurationContext    For use by NdisOpenConfiguration

Return Value:

    NDIS_STATUS_SUCCESS unless something goes wrong

--*/
{
    UINT            i;
    PADAPT          pAdapt;
    NDIS_STATUS     Status = NDIS_STATUS_FAILURE;
    NDIS_MEDIUM     Medium;

    UNREFERENCED_PARAMETER(WrapperConfigurationContext);
    
    do
    {

        // Information output.  ja, 29.11.2003.                                                                                                              

        char                     static DateCompiledBase[] = __DATE__,
                                        TimeCompiledBase[] = " "__TIME__;
        char                            DateCompiled[] =  // Build date in preferred (dd mmm yyyy) format.
                                          {DateCompiledBase[4], DateCompiledBase[5], DateCompiledBase[6],
                                           DateCompiledBase[0], DateCompiledBase[1], DateCompiledBase[2], DateCompiledBase[3],
                                           DateCompiledBase[7], DateCompiledBase[8], DateCompiledBase[9], DateCompiledBase[10],
                                           0x0
                                          };

        if (' '==DateCompiled[0])
          strcpy(DrvCompileInfo, DateCompiled+1);
        else
          strcpy(DrvCompileInfo, DateCompiled+0);

        strcat(DrvCompileInfo, TimeCompiledBase);

        DBGPRINT((JADrvNm "." JADrvRtnName " v" JADrvRtnVer " (compiled %s)\n", DrvCompileInfo));

        // End information output.  ja, 29.11.2003.
     
        //
        // Start off by retrieving our adapter context and storing
        // the Miniport handle in it.
        //
        pAdapt = NdisIMGetDeviceContext(MiniportAdapterHandle);
        pAdapt->MiniportHandle = MiniportAdapterHandle;

        DBGPRINT(("==> Miniport Initialize:  Adapt %p\n", pAdapt));

        //
        // Usually we export the medium type of the adapter below as our
        // virtual miniport's medium type. However if the adapter below us
        // is a WAN device, then we claim to be of medium type 802.3.
        //
        Medium = pAdapt->Medium;

        if (Medium == NdisMediumWan)
        {
            Medium = NdisMedium802_3;
        }

        for (i = 0; i < MediumArraySize; i++)
        {
            if (MediumArray[i] == Medium)
            {
                *SelectedMediumIndex = i;
                break;
            }
        }

        if (i == MediumArraySize)
        {
            Status = NDIS_STATUS_UNSUPPORTED_MEDIA;
            break;
        }


        //
        // Set the attributes now. NDIS_ATTRIBUTE_DESERIALIZE enables us
        // to make up-calls to NDIS without having to call NdisIMSwitchToMiniport
        // or NdisIMQueueCallBack. This also forces us to protect our data using
        // spinlocks where appropriate. Also in this case NDIS does not queue
        // packets on our behalf. Since this is a very simple pass-thru
        // miniport, we do not have a need to protect anything. However in
        // a general case there will be a need to use per-adapter spin-locks
        // for the packet queues at the very least.
        //
        NdisMSetAttributesEx(MiniportAdapterHandle,
                             pAdapt,
                             0,                                        // CheckForHangTimeInSeconds
                             NDIS_ATTRIBUTE_IGNORE_PACKET_TIMEOUT    |
                                NDIS_ATTRIBUTE_IGNORE_REQUEST_TIMEOUT|
                                NDIS_ATTRIBUTE_INTERMEDIATE_DRIVER |
                                NDIS_ATTRIBUTE_DESERIALIZE |
                                NDIS_ATTRIBUTE_NO_HALT_ON_SUSPEND,
                             0);

        //
        // Initialize LastIndicatedStatus to be NDIS_STATUS_MEDIA_CONNECT
        //
        pAdapt->LastIndicatedStatus = NDIS_STATUS_MEDIA_CONNECT;
        
        //
        // Initialize the power states for both the lower binding (PTDeviceState)
        // and our miniport edge to Powered On.
        //
        pAdapt->MPDeviceState = NdisDeviceStateD0;
        pAdapt->PTDeviceState = NdisDeviceStateD0;

        //
        // Add this adapter to the global pAdapt List
        //
        NdisAcquireSpinLock(&GlobalLock);

        pAdapt->Next = pAdaptList;
        pAdaptList = pAdapt;

        NdisReleaseSpinLock(&GlobalLock);
        
        //
        // Create an ioctl interface
        //
        (VOID)PtRegisterDevice();

        // Set up empty IP-address array.             // ja, 28.09.2003.

        NdisInitializeReadWriteLock(&pAdapt->IPAddrArrLock);

        Status =
          PassthruWMISetAddrArray(
                                  pAdapt,
                                  0,                  // No elements.
                                  NULL                // No structure of elements.
                                 );

        if (NDIS_STATUS_SUCCESS!=Status)
          {
           DBGPRINT(("MPInitialize():  Failed inPassthruWMISetAddrArray(), status 0x%08x!\n", Status));
           Status = NDIS_STATUS_RESOURCES;
          }

        Status = NDIS_STATUS_SUCCESS;
    }
    while (FALSE);

    //
    // If we had received an UnbindAdapter notification on the underlying
    // adapter, we would have blocked that thread waiting for the IM Init
    // process to complete. Wake up any such thread.
    //
    ASSERT(pAdapt->MiniportInitPending == TRUE);
    pAdapt->MiniportInitPending = FALSE;
    NdisSetEvent(&pAdapt->MiniportInitEvent);

    DBGPRINT(("<== Miniport Initialize: Adapt %p, Status %x\n", pAdapt, Status));

    *OpenErrorStatus = Status;
    
    return Status;
}


NDIS_STATUS
MPSend(
    IN NDIS_HANDLE             MiniportAdapterContext,
    IN PNDIS_PACKET            Packet,
    IN UINT                    Flags
    )
/*++

Routine Description:

    Send Packet handler. Either this or our SendPackets (array) handler is called
    based on which one is enabled in our Miniport Characteristics.

Arguments:

    MiniportAdapterContext    Pointer to the adapter
    Packet                    Packet to send
    Flags                     Unused, passed down below

Return Value:

    Return code from NdisSend

--*/
{
    PADAPT              pAdapt = (PADAPT)MiniportAdapterContext;
    NDIS_STATUS         Status;
    PNDIS_PACKET        MyPacket;
    PVOID               MediaSpecificInfo = NULL;
    ULONG               MediaSpecificInfoSize = 0;

    //
    // The driver should fail the send if the virtual miniport is in low 
    // power state
    //
    if (pAdapt->MPDeviceState > NdisDeviceStateD0)
    {
         return NDIS_STATUS_FAILURE;
    }

#ifdef NDIS51
    //
    // Use NDIS 5.1 packet stacking:
    //
    {
        PNDIS_PACKET_STACK        pStack;
        BOOLEAN                   Remaining;

        //
        // Packet stacks: Check if we can use the same packet for sending down.
        //

        pStack = NdisIMGetCurrentPacketStack(Packet, &Remaining);
        if (Remaining)
        {
            //
            // We can reuse "Packet".
            //
            // NOTE: if we needed to keep per-packet information in packets
            // sent down, we can use pStack->IMReserved[].
            //
            ASSERT(pStack);
            //
            // If the below miniport is going to low power state, stop sending down any packet.
            //
            NdisAcquireSpinLock(&pAdapt->Lock);
            if (pAdapt->PTDeviceState > NdisDeviceStateD0)
            {
                NdisReleaseSpinLock(&pAdapt->Lock);
                return NDIS_STATUS_FAILURE;
            }
            pAdapt->OutstandingSends++;
            NdisReleaseSpinLock(&pAdapt->Lock);
            NdisSend(&Status,
                     pAdapt->BindingHandle,
                     Packet);

            if (Status != NDIS_STATUS_PENDING)
            {
                ADAPT_DECR_PENDING_SENDS(pAdapt);
            }

            return(Status);

⌨️ 快捷键说明

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