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

📄 ndisdriver.c

📁 MICREL 网卡驱动 FOR CE 5.0
💻 C
📖 第 1 页 / 共 3 页
字号:
/* ---------------------------------------------------------------------------
          Copyright (c) 2003-2006 Micrel, Inc.  All rights reserved.
   ---------------------------------------------------------------------------

    NdisDriver.c - NDIS driver functions.

    Author      Date        Description
    THa         12/01/03    Created file.
    THa         06/27/05    Updated for version 0.1.5.
    THa         09/29/05    Changed descriptor structure.
    THa         02/23/06    Updated for WinCE 5.0.
    THa         03/14/06    Release 1.0.0.
    THa         06/02/06    Report statistics from MIB counters.
    THa         07/27/06    Version 1.4 supports NDIS 5.
   ---------------------------------------------------------------------------
*/


#include "target.h"
#include "hardware.h"
#include "NdisDevice.h"
#include "NdisOid.h"


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

#if defined( DEF_KS8842 )
#define DRV_NAME     "KSZ8842"
#elif defined( DEF_KS8841 )
#define DRV_NAME     "KSZ8841"
#else
#error Either DEF_KS8841 or DEF_KS8842 has to be defined.
#endif
#define BUS_NAME     "PCI"
#define DRV_VERSION  "1.0.4"
#define DRV_RELDATE  "Jul 27, 2006"

#ifdef DBG
static char version[] =
    "Micrel " DRV_NAME " " BUS_NAME " " DRV_VERSION " (" DRV_RELDATE ")";
#endif

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

#ifdef UNDER_CE

#if DBG
DBGPARAM dpCurSettings = {
    TEXT( NDIS_NAME ),
    {
        TEXT( "Error" ),
        TEXT( "Warning" ),
        TEXT( "Func" ),
        TEXT( "Init" ),
        TEXT( "Intr" ),
        TEXT( "Rx" ),
        TEXT( "Tx" ),
        TEXT( "Link" ),
        TEXT( "Undefined" ),
        TEXT( "Undefined" ),
        TEXT( "Undefined" ),
        TEXT( "Undefined" ),
        TEXT( "Undefined" ),
        TEXT( "Undefined" ),
        TEXT( "Undefined" ),
        TEXT( "Undefined" )
    },
    ZONE_INIT_MASK | ZONE_ERROR_MASK
};
#endif


BOOL __stdcall
DllEntry (
    HANDLE hDLL,
    DWORD  dwReason,
    LPVOID lpReserved )
{
    switch ( dwReason )
    {
        case DLL_PROCESS_ATTACH:
            DEBUGREGISTER( hDLL );
            DEBUGMSG( ZONE_INIT, ( TEXT( "KS884X: DLL_PROCESS_ATTACH\r\n" )));
            DisableThreadLibraryCalls(( HMODULE ) hDLL );
            break;
        case DLL_PROCESS_DETACH:
            DEBUGMSG( ZONE_INIT, ( TEXT( "KS884X: DLL_PROCESS_DETACH\r\n" )));
            break;
    }
    return TRUE;
}

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

#define MIN_DETECT_KEY  40
#define MAX_DETECT_KEY  99


typedef struct _REG_VALUE_DESCR {
    LPWSTR val_name;
    DWORD  val_type;
    PBYTE  val_data;
} REG_VALUE_DESCR, *PREG_VALUE_DESCR;


// Values for [HKEY_LOCAL_MACHINE\Drivers\PCMCIA\Detect\40]
REG_VALUE_DESCR DetectKeyValues[] = {
    ( TEXT( "Dll" )), REG_SZ, ( PBYTE )( TEXT( "NdisDriver.DLL" )),
    ( TEXT( "Entry" )), REG_SZ, ( PBYTE )( TEXT( "DetectNdisDevice" )),
    NULL, 0, NULL
};

// Values for [HKEY_LOCAL_MACHINE\Drivers\PCMCIA\NdisDriver]
REG_VALUE_DESCR PcmKeyValues[] = {
    ( TEXT( "Dll" )), REG_SZ, ( PBYTE )( TEXT( "ndis.dll" )),
    ( TEXT( "Prefix" )), REG_SZ, ( PBYTE )( TEXT( "NDS" )),
    ( TEXT( "Miniport" )), REG_SZ, ( PBYTE )( TEXT( "NdisDriver" )),
    NULL, 0, NULL
};

// Values for [HKEY_LOCAL_MACHINE\Comm\NdisDriver]
REG_VALUE_DESCR CommKeyValues[] = {
    ( TEXT( "DisplayName" )), REG_SZ, ( PBYTE )
        ( TEXT( "KS8841 Ethernet Driver" )),
    ( TEXT( "Group" )), REG_SZ, ( PBYTE )( TEXT( "NDIS" )),
    ( TEXT( "ImagePath" )), REG_SZ, ( PBYTE )( TEXT( "ks8841.dll" )),
    NULL, 0, NULL
};

// Values for [HKEY_LOCAL_MACHINE\Comm\NdisDriver\Linkage]
REG_VALUE_DESCR LinkageKeyValues[] = {
    ( TEXT( "Route" )), REG_MULTI_SZ, ( PBYTE )( TEXT( "KS88411" )),
    NULL, 0, NULL
};

// Values for [HKEY_LOCAL_MACHINE\Comm\NdisDriver1\Parms]
REG_VALUE_DESCR ParmKeyValues[] = {
    ( TEXT( "BusNumber" )), REG_DWORD, ( PBYTE ) 0,
    ( TEXT( "BusType" )), REG_DWORD, ( PBYTE ) 8,
    ( TEXT( "InterruptNumber" )), REG_DWORD, ( PBYTE ) 9,
    ( TEXT( "IoBaseAddress" )), REG_DWORD, ( PBYTE ) 0x300,
    ( TEXT( "IoLen" )), REG_DWORD, ( PBYTE ) 0x10,
    ( TEXT( "CableType" )), REG_DWORD, ( PBYTE ) 1,
    ( TEXT( "EarlyTransmit" )), REG_DWORD, ( PBYTE ) 0,
    ( TEXT( "FullDuplex" )), REG_DWORD, ( PBYTE ) 0,
    ( TEXT( "LinkIntegrity" )), REG_DWORD, ( PBYTE ) 1,
    NULL, 0, NULL
};

PREG_VALUE_DESCR Values[] = {
#if 0
    PcmKeyValues,
#endif
    CommKeyValues,
    CommKeyValues,
    ParmKeyValues,
    LinkageKeyValues
};

LPWSTR KeyNames[] = {
#if 0
    ( TEXT( "Drivers\\PCMCIA\\KS8841" )),
#endif
    ( TEXT( "Comm\\KS8841" )),
    ( TEXT( "Comm\\KS88411" )),
    ( TEXT( "Comm\\KS88411\\Parms" )),
    ( TEXT( "Comm\\KS8841\\Linkage" ))
};


//
// Add the specified key and its values to the registry under HKEY_LOCAL_MACHINE
//
// NOTE: This function only supports REG_MULTI_SZ strings with one item.
//
BOOL AddKeyValues (
    LPWSTR           KeyName,
    PREG_VALUE_DESCR Vals )
{
    HKEY             hKey;
    DWORD            Status;
    DWORD            dwDisp;
    DWORD            dwVal;
    PREG_VALUE_DESCR pValue;
    DWORD            ValLen;
    PBYTE            pVal;
    LPWSTR           pStr;

    /* create specifed key */
    Status = RegCreateKeyEx( HKEY_LOCAL_MACHINE, KeyName, 0, NULL,
        REG_OPTION_NON_VOLATILE, 0, NULL, &hKey, &dwDisp );

    if ( Status != ERROR_SUCCESS ) {
        return FALSE;
    }

    /* created the key, now put the value in */
    pValue = Vals;
    while ( pValue->val_name ) {
        switch ( pValue->val_type ) {
            case REG_DWORD:
                pVal = ( PBYTE ) &dwVal;
                dwVal = ( DWORD ) pValue->val_data;
                ValLen = sizeof( DWORD );
                break;

            case REG_SZ:
                pVal = ( PBYTE ) pValue->val_data;
                ValLen = ( wcslen(( LPWSTR ) pVal ) + 1 ) * sizeof( WCHAR );
                break;

            case REG_MULTI_SZ:
                dwVal = wcslen(( LPWSTR ) pValue->val_data );
                ValLen = ( dwVal + 2 ) * sizeof( WCHAR );
                pVal = ( PBYTE ) LocalAlloc( LPTR, ValLen );
                if ( pVal == NULL ) {
                    goto akv_fail;
                }
                wcscpy(( LPWSTR ) pVal, ( LPWSTR ) pValue->val_data );
                pStr = ( LPWSTR ) pVal + dwVal;
                pStr[ 1 ] = 0;
                break;
        }
        Status = RegSetValueEx( hKey, pValue->val_name, 0, pValue->val_type,
            pVal, ValLen );
        if ( pValue->val_type == REG_MULTI_SZ ) {
            LocalFree( pVal );
        }

akv_fail:
        if ( Status != ERROR_SUCCESS ) {
            RegCloseKey( hKey );
            return FALSE;
        }

        /* get next value */
        pValue++;
    }

    /* close the key */
    RegCloseKey( hKey );
    return TRUE;
}   // AddKeyValues


#ifdef AUTO_DETECT
//
// Find an unused Detect key number and return the registry path name for it.
//
// Return NULL for failure
//
LPWSTR FindDetectKey ( VOID )
{
    HKEY   hDetectKey;
    DWORD  dwDisp;
    DWORD  dwKeyNum;
    DWORD  Status;
    WCHAR* pKeyName;
    LPWSTR pKeyNum;

    pKeyName = ( WCHAR* ) LocalAlloc( LPTR, sizeof( WCHAR ) * 255 );
    if ( pKeyName == NULL ) {
        return NULL;
    }

    wcscpy( pKeyName, ( TEXT( "Drivers\\PCMCIA\\Detect\\" )));
    pKeyNum = pKeyName + wcslen( pKeyName );

    //
    // Find a detect number and create the detect key.
    //
    for ( dwKeyNum = MIN_DETECT_KEY; dwKeyNum < MAX_DETECT_KEY; dwKeyNum++ ) {
        wsprintf( pKeyNum, ( TEXT( "%02d" )), dwKeyNum );
        Status = RegCreateKeyEx( HKEY_LOCAL_MACHINE, pKeyName, 0, NULL,
            REG_OPTION_NON_VOLATILE, 0, NULL, &hDetectKey, &dwDisp );
        if ( Status == ERROR_SUCCESS ) {
            RegCloseKey( hDetectKey );
            if ( dwDisp == REG_CREATED_NEW_KEY ) {
                return pKeyName;
            }
        }
    }
    LocalFree( pKeyName );
    return NULL;
}  // FindDetectKey


//
// IsInstalled - Search the detect function list in the registry to see if
// we've already been installed.
//
// Return TRUE if this driver's detect function is listed.
//
BOOL IsInstalled ( VOID )
{
    HKEY  hDetectKey;
    HKEY  hDetectNum;
    DWORD dwKeyNum;
    DWORD Status;
    WCHAR KeyNum[ 8 ];
    WCHAR EntryName[ 32 ];
    BOOL  bret;
    DWORD dwLen;
    DWORD dwType;

    Status = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
        TEXT( "Drivers\\PCMCIA\\Detect" ), REG_OPTION_NON_VOLATILE, 0,
        &hDetectKey );
    if ( Status != ERROR_SUCCESS ) {
        DEBUGMSG( ZONE_INIT,
            ( TEXT( "KS884X: IsInstalled RegOpenKeyEx failed %d\r\n" ),
            Status ));
        return FALSE;
    }

    bret = FALSE;
    for ( dwKeyNum = MIN_DETECT_KEY;
            ( bret == FALSE )  &&  ( dwKeyNum < MAX_DETECT_KEY );
            dwKeyNum++ ) {
        wsprintf( KeyNum, ( TEXT( "%02d" )), dwKeyNum );
        Status = RegOpenKeyEx( hDetectKey, KeyNum, REG_OPTION_NON_VOLATILE, 0,
            &hDetectNum );
        if ( Status == ERROR_SUCCESS ) {
            Status = RegQueryValueEx( hDetectNum, TEXT( "Entry" ), 0, &dwType,
                ( PUCHAR ) EntryName, &dwLen );
            if ( Status == ERROR_SUCCESS ) {
                if ( !( wcscmp( EntryName, TEXT( "DetectNdisDevice" ))) ) {
                    bret = TRUE;
                }
            }
            RegCloseKey( hDetectNum );
        }
    }
    RegCloseKey( hDetectKey );
    return bret;
}  // IsInstalled
#endif


//
// Install_Driver function for the NDIS miniport driver.
//
// This function sets up the registry keys and values required to install this
// DLL as a Windows CE plug and play driver.
//
// Input:
//
// LPWSTR lpPnpId - The device's plug and play identifier string.  An install
// function can use lpPnpId to set up a key
// HKEY_LOCAL_MACHINE\Drivers\PCMCIA\<lpPnpId> under the assumption that the
// user will continue to use the same device that generates the same plug and
// play id string.  If there is a general detection method for the card, then
// lpPnpId can be ignored and a detection function can be registered under
// HKEY_LOCAL_MACHINE\Drivers\PCMCIA\Detect.
//
// LPWSTR lpRegPath - Buffer to contain the newly installed driver's device key
// under HKEY_LOCAL_MACHINE in the registry.  Windows CE will attempt to load
// the the newly installed device driver upon completion of its
// Install_Driver function.

⌨️ 快捷键说明

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