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

📄 ndisdevice.c

📁 MICREL 网卡驱动 FOR CE 5.0
💻 C
📖 第 1 页 / 共 5 页
字号:
    CheckDescriptors( &pAdapter->m_Hardware.m_RxDescInfo );
    CheckDescriptors( &pAdapter->m_Hardware.m_TxDescInfo );
#endif

    return NDIS_STATUS_SUCCESS;
}  /* AllocateMemory */


/*
    FreeMapRegisters

    Description:
        This local routine frees the mapping registers allocated by
        AllocateMapRegisters().

    Parameters:
        PNDIS_ADAPTER pAdapter
            Pointer to adapter information structure.

    Return (None):
*/

static void FreeMapRegisters (
        PNDIS_ADAPTER pAdapter )
{
    /* free map registers */
    if ( pAdapter->m_cnMapRegAlloc )
    {
        NdisMFreeMapRegisters( pAdapter->m_hAdapter );
        NdisFreeMemory( pAdapter->m_MapReg,
            sizeof( MAP_REG ) * pAdapter->m_cnMapRegAlloc, 0 );
        pAdapter->m_cnMapRegAlloc = 0;
    }

    if ( pAdapter->m_cnPackets )
    {
        NdisFreeMemory( pAdapter->m_PacketArray,
            sizeof( PNDIS_PACKET ) * pAdapter->m_cnPackets, 0 );
        pAdapter->m_cnPackets = 0;
    }
}  /* FreeMapRegisters */


/*
    FreeDescriptors

    Description:
        This local routine frees the software and hardware descriptors
        allocated by AllocateDescriptors().

    Parameters:
        PNDIS_ADAPTER pAdapter
            Pointer to adapter information structure.

    Return (None):
*/

static void FreeDescriptors (
        PNDIS_ADAPTER pAdapter )
{
    PHARDWARE pHardware = &pAdapter->m_Hardware;

    /* reset descriptor */
    pHardware->m_RxDescInfo.phwRing = NULL;
    pHardware->m_TxDescInfo.phwRing = NULL;
    pHardware->m_RxDescInfo.ulRing = 0;
    pHardware->m_TxDescInfo.ulRing = 0;

    /* Free memory */
    if ( pAdapter->m_DescPool.pAllocVirtual )
        NdisMFreeSharedMemory( pAdapter->m_hAdapter,
            pAdapter->m_DescPool.ulAllocSize,
            pAdapter->m_DescPool.bCached,
            pAdapter->m_DescPool.pAllocVirtual,
            pAdapter->m_DescPool.AllocPhysicalAddr );

    /* reset resource pool */
    pAdapter->m_DescPool.ulAllocSize = 0;
    pAdapter->m_DescPool.pAllocVirtual = NULL;

    if ( pHardware->m_RxDescInfo.pRing )
    {
        NdisFreeMemory( pHardware->m_RxDescInfo.pRing,
            sizeof( TDesc ) * pHardware->m_RxDescInfo.cnAlloc, 0 );
        pHardware->m_RxDescInfo.pRing = NULL;
    }
    if ( pHardware->m_TxDescInfo.pRing )
    {
        NdisFreeMemory( pHardware->m_TxDescInfo.pRing,
            sizeof( TDesc ) * pHardware->m_TxDescInfo.cnAlloc, 0 );
        pHardware->m_TxDescInfo.pRing = NULL;
    }
}  /* FreeDescriptors */


/*
    FreeBuffers

    Description:
        This local routine frees the DMA buffers allocated by
        AllocateBuffers().

    Parameters:
        PNDIS_ADAPTER pAdapter
            Pointer to adapter information structure.

        PBUFFER_INFO pBufInfo
            Pointer to DMA buffer information structure.

    Return (None):
*/

static void FreeBuffers (
        PNDIS_ADAPTER pAdapter,
        PBUFFER_INFO  pBufInfo )
{
    int         i;
    PDMA_BUFFER pDma;
    PSHARED_MEM pBufPool = &pBufInfo->MemPool;

    if ( !pBufInfo->BufArray )
        return;

    pDma = pBufInfo->BufArray;
    for ( i = 0; i < pBufInfo->cnAlloc; i++ )
    {
        if ( pDma->pNdisPacket )
        {
            NdisFreePacket( pDma->pNdisPacket );
            pDma->pNdisPacket = NULL;
        }
        if ( pDma->pNdisBuffer )
        {
            /* Need to restore to original length before releasing. */
            NdisAdjustBufferLength( pDma->pNdisBuffer, pDma->ulSize );
            NdisFreeBuffer( pDma->pNdisBuffer );
            pDma->pNdisBuffer = NULL;
        }
        if ( pDma->sm.pAllocVirtual )
        {
            NdisMFreeSharedMemory( pAdapter->m_hAdapter,
                pDma->sm.ulAllocSize,
                pDma->sm.bCached,
                pDma->sm.pAllocVirtual,
                pDma->sm.AllocPhysicalAddr );
        }
        pDma++;
    }
    NdisFreeMemory( pBufInfo->BufArray,
        sizeof( DMA_BUFFER ) * pBufInfo->cnAlloc, 0 );
    pBufInfo->BufArray = NULL;

    if ( pBufPool->pAllocVirtual )
    {
        NdisMFreeSharedMemory( pAdapter->m_hAdapter,
            pBufPool->ulAllocSize,
            pBufPool->bCached,
            pBufPool->pAllocVirtual,
            pBufPool->AllocPhysicalAddr );
        pBufPool->ulAllocSize = 0;
        pBufPool->pAllocVirtual = NULL;
    }
    NdisFreeSpinLock( &pBufInfo->listLock );
}  /* FreeBuffers */


/*
    FreeMemory

    Description:
        This local routine frees all the resources allocated by
        AllocateMemory().

    Parameters:
        PNDIS_ADAPTER pAdapter
            Pointer to adapter information structure.

    Return (None):
*/

VOID FreeMemory (
        PNDIS_ADAPTER pAdapter )
{
    /* Free transmit buffers. */
    FreeBuffers( pAdapter, &pAdapter->m_TxBufInfo );

    /* Free receive buffers */
    FreeBuffers( pAdapter, &pAdapter->m_RxBufInfo );

    /* Free map registers. */
    FreeMapRegisters( pAdapter );

    /* Free descriptors. */
    FreeDescriptors( pAdapter );

    /* Free packet pool handle. */
    if ( pAdapter->m_hPacketPool )
    {
        NdisFreePacketPool( pAdapter->m_hPacketPool );
        pAdapter->m_hPacketPool = 0;
    }

    /* Free buffer pool handle. */
    if ( pAdapter->m_hBufferPool )
    {
        NdisFreeBufferPool( pAdapter->m_hBufferPool );
        pAdapter->m_hBufferPool = 0;
    }
}  /* FreeMemory */

/* -------------------------------------------------------------------------- */

/*
    InitAdapter

    Description:
        This function initializes the adapter when the adapter is started.  It
        calls ReadConfiguration() to get all information required to start the
        adapter.

    Parameters:
        PNDIS_ADAPTER pAdapter
            Pointer to adapter information structure.

        NDIS_HANDLE hAdapter
            Handle to adapter context containing adapter information.

        NDIS_HANDLE hConfiguration
            Handle to adapter configuration.

    Return (NDIS_STATUS):
        NDIS_STATUS_SUCCESS if successful; otherwise an error code indicating
        failure.
*/

NDIS_STATUS InitAdapter (
        PNDIS_ADAPTER pAdapter,
    IN  NDIS_HANDLE   hAdapter,
    IN  NDIS_HANDLE   hConfiguration )
{
    NDIS_STATUS nsStatus;

    // Save the adapter handle furnished by NDIS.
    pAdapter->m_hAdapter = hAdapter;

    nsStatus = ReadConfiguration( pAdapter, hConfiguration );
    if ( !NT_SUCCESS( nsStatus ) )
    {
        return( nsStatus );
    }

    /* Need to set bus master else NdisMAllocateSharedMemory will not succeed.
    */
#if (NDISVER < 51)
    NdisMSetAttributes( pAdapter->m_hAdapter, ( NDIS_HANDLE ) pAdapter, TRUE,
        pAdapter->m_BusType );

#else
    NdisMSetAttributesEx( pAdapter->m_hAdapter, ( NDIS_HANDLE ) pAdapter, 0,
        NDIS_ATTRIBUTE_SURPRISE_REMOVE_OK | NDIS_ATTRIBUTE_BUS_MASTER,
        pAdapter->m_BusType );
#endif

    if ( !NT_SUCCESS( AllocateMemory( pAdapter )) )
    {
        return NDIS_STATUS_FAILURE;
    }

    return NDIS_STATUS_SUCCESS;
}  // InitAdapter


/*
    SetupAdapter

    Description:
        This function setups the adapter when the adapter is started.

    Parameters:
        PNDIS_ADAPTER pAdapter
            Pointer to adapter information structure.

    Return (NDIS_STATUS):
        NDIS_STATUS_SUCCESS if successful; otherwise an error code indicating
        failure.
*/

NDIS_STATUS SetupAdapter (
    PNDIS_ADAPTER pAdapter )
{
    PHARDWARE pHardware = &pAdapter->m_Hardware;
    UCHAR     bPromiscuous;

    if ( !AcquireAdapter( pAdapter, FALSE ) )
    {
        return NDIS_STATUS_ADAPTER_NOT_FOUND;
    }
    if ( !HardwareInitialize( pHardware )  ||  !HardwareReset( pHardware ) )
    {
        NdisWriteErrorLogEntry( pAdapter->m_hAdapter,
            NDIS_ERROR_CODE_UNSUPPORTED_CONFIGURATION, 1, 0x10560100 );
        ReleaseAdapter( pAdapter );
        return NDIS_STATUS_ADAPTER_NOT_FOUND;
    }

    HardwareDisable( pHardware );

#if 0
    HardwareGetVersion( pHardware );
#endif

    // Read the Ethernet address.
    if ( !HardwareReadAddress( pHardware ) )
    {
        NdisWriteErrorLogEntry( pAdapter->m_hAdapter,
            NDIS_ERROR_CODE_UNSUPPORTED_CONFIGURATION, 1, 0x10560101 );
        ReleaseAdapter( pAdapter );
        return NDIS_STATUS_ADAPTER_NOT_FOUND;
    }

    pHardware->m_ulHardwareState = MediaStateDisconnected;

    /* Initialize to invalid value so that link detection is done. */
    pHardware->m_PortInfo[ MAIN_PORT ].bLinkPartner = 0xFF;
    pHardware->m_PortInfo[ MAIN_PORT ].ulHardwareState =
        MediaStateDisconnected;

#ifdef DEF_KS8842
    pHardware->m_PortInfo[ OTHER_PORT ].bLinkPartner = 0xFF;
    pHardware->m_PortInfo[ OTHER_PORT ].ulHardwareState =
        MediaStateDisconnected;
#endif

    HardwareSetup( pHardware );
    HardwareSwitchSetup( pHardware );
    HardwareSetDescriptorBase( pHardware,
        pHardware->m_TxDescInfo.ulRing,
        pHardware->m_RxDescInfo.ulRing );

    if ( ( pAdapter->m_ulPacketFilter &
            ( NDIS_PACKET_TYPE_PROMISCUOUS | NDIS_PACKET_TYPE_ALL_LOCAL )) )
        bPromiscuous = TRUE;
    else
        bPromiscuous = FALSE;
    if ( !HardwareSetPromiscuous( pHardware, bPromiscuous ) )
    {
        ReleaseAdapter( pAdapter );
        return NDIS_STATUS_ADAPTER_NOT_FOUND;
    }

    pAdapter->m_ulNdisMediaState = pAdapter->m_Hardware.m_ulHardwareState;

    PortInitCounters( pHardware, MAIN_PORT );

#if defined( DEF_KS8842 )  &&  !defined( TWO_NETWORK_INTERFACE )
    PortInitCounters( pHardware, OTHER_PORT );
#endif
    PortInitCounters( pHardware, HOST_PORT );

    HardwareSetupInterrupt( pHardware );
    HardwareEnable( pHardware );

#ifdef DBG
DbgPrint( "Setup completed"NEWLINE );
#endif

#if 0
    HardwareGetInfo( pHardware );
#endif

    HardwareEnableInterrupt( pHardware );
    ReleaseAdapter( pAdapter );

#if (NDISVER >= 50)
    /* Specifies the lowest device power state from which the
       miniport's NIC can signal a wake-up on receipt of a Magic
       Packet.  (A Magic Packet is a packet that contains 16 contiguous
       copies of the receiving NIC's Ethernet address.)
    */
    pAdapter->PNP_Capabilities.WakeUpCapabilities.MinMagicPacketWakeUp =
        NdisDeviceStateUnspecified;
    pAdapter->PNP_Capabilities.WakeUpCapabilities.MinMagicPacketWakeUp =
        NdisDeviceStateD3;

    /* Specifies the lowest device power state from which the
       miniport's NIC can signal a wake-up event on receipt of a
       network frame that contains a pattern specified by the protocol
       driver.
    */
    pAdapter->PNP_Capabilities.WakeUpCapabilities.MinPatternWakeUp =
        NdisDeviceStateUnspecified;
    pAdapter->PNP_Capabilities.WakeUpCapabilities.MinPatternWakeUp =
        NdisDeviceStateD3;

    /* Specifies the lowest device power state from which the
       miniport's NIC can signal a wake-up event in response to a link
       change (the connection or disconnection of the NIC's network
       cable).
    */
    pAdapter->PNP_Capabilities.WakeUpCapabilities.MinLinkChangeWakeUp =
        NdisDeviceStateUnspecified;
    pAdapter->PNP_Capabilities.WakeUpCapabilities.MinLinkChangeWakeUp =
        NdisDeviceStateD3;

    pAdapter->PNP_Capabilities.Flags = NDIS_DEVICE_WAKE_UP_ENABLE;
#if 1
    pAdapter->PNP_Capabilities.Flags |=
        NDIS_DEVICE_WAKE_ON_PATTERN_MATCH_ENABLE;
    pAdapter->PNP_Capabilities.Flags |=
        NDIS_DEVICE_WAKE_ON_MAGIC_PACKET_ENABLE;
#endif

    pAdapter->CurrentPowerState = pAdapter->NextPowerState =
        NdisDeviceStateD0;

#if 0
    pAdapter->m_ulRxChecksum = pAdapter->m_ulTxChecksum =
        TASK_OFFLOAD_CHECKSUM;
#endif
#endif

    phw = pHardware;

    return NDIS_STATUS_SUCCESS;
}  // SetupAdapter


/*
    ShutdownAdapter

    Description:
        This routine shutdowns the adapter when the driver is unloaded.

    Parameters:
        PNDIS_ADAPTER pAdapter
            Pointer to adapter information structure.

    Return (None):
*/

void ShutdownAdapter (
    PNDIS_ADAPTER pAdapter )
{
    PHARDWARE pHardware = &pAdapter->m_Hardware;

#ifndef UNDER_CE

⌨️ 快捷键说明

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