📄 lan91c111_init.c
字号:
/*
*
* Copyright (c) Standard MicroSystems Corporation. All Rights Reserved.
*
* LAN91C111 Driver for Windows CE .NET
*
* Revision History
*_______________________________________________________________________________
* Author Date Version Description
*_______________________________________________________________________________
* Pramod Bhardwaj 6/18/2002 0.1 Beta Release
* Pramod Bhardwaj 7/15/2002 1.0 Release
* Pramod Bhardwaj 1/22/2003 1.1 Removed some platform dependencies
* Pramod Bhardwaj 4/15/2003 2.0 Added support for alloc interrupt
*_______________________________________________________________________________
*
*Description:
* Contains all the initialization functions required for the driver.
*
*
*/
#include <Ndis.h>
#include <PKFuncs.h>
#include "LAN91C111_Adapter.H"
#define PXA250_LAN91C111_ADDR 0x17400000
extern NDIS_PHYSICAL_ADDRESS HighestAcceptedMax;
extern VOID LAN91C111_MiniPortHandleInterrupt (IN NDIS_HANDLE AdapterContext);
NDIS_STATUS GetRegistrySettings(MINIPORT_ADAPTER *, NDIS_HANDLE);
BOOLEAN AdapterVerify(MINIPORT_ADAPTER *);
BOOLEAN AdapterReset(MINIPORT_ADAPTER *);
void BackOut(MINIPORT_ADAPTER *Adapter);
BOOLEAN EstablishLink(MINIPORT_ADAPTER *Adapter);
/*
Function Name : LAN91C111_MiniportInitialize
Description :
Called by the NDIS Wrapper to initialize the adapter.
0. Verify the Adapter v/s the driver
1. Create and initilize the adapter structure
2. Read and load the registry settings
3. Initialize the chip
4. Establish the link
Parameters :
PNDIS_STATUS OpenErrorStatus - Additional error status, if return value is error
PUINT MediumIndex - specifies the medium type the driver or its network adapter uses
PNDIS_MEDIUM MediumArray - Specifies an array of NdisMediumXXX values from which
MiniportInitialize selects one that its network adapter supports
or that the driver supports as an interface to higher-level drivers.
UINT MediumArraySize - Specifies the number of elements at MediumArray
NDIS_HANDLE AdapterHandle - Specifies a handle identifying the miniport抯 network adapter,
which is assigned by the NDIS library
NDIS_HANDLE ConfigurationContext - Specifies a handle used only during initialization for
calls to NdisXXX configuration and initialization functions
Return Value :
NDIS_STATUS Status
*/
NDIS_STATUS LAN91C111_MiniportInitialize(
PNDIS_STATUS OpenErrorStatus,
PUINT MediumIndex,
PNDIS_MEDIUM MediumArray,
UINT MediumArraySize,
NDIS_HANDLE AdapterHandle,
NDIS_HANDLE ConfigurationContext
)
{
NDIS_STATUS Status=NDIS_STATUS_SUCCESS;
UINT ArrayIndex;
PMINIPORT_ADAPTER Adapter;
USHORT temp;
LPVOID lpIOBase;
BOOL RetVal;
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C111 ==> MiniportInitialize \r\n")));
//Check for the supported 802.3 media
for(ArrayIndex = 0; ArrayIndex < MediumArraySize; ArrayIndex++)
if(MediumArray[ArrayIndex] == NdisMedium802_3) break;
if(ArrayIndex == MediumArraySize)
{
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C111 : ERROR - No Supported Media types \r\n")));
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C111 <== MiniportInitialize \r\n")));
return(NDIS_STATUS_UNSUPPORTED_MEDIA);
}
*MediumIndex = ArrayIndex;
//Allocate memory for the adapter structure
temp = MINIPORT_ADAPTER_SIZE;
Status = NdisAllocateMemory((PVOID *) &Adapter, MINIPORT_ADAPTER_SIZE, 0, HighestAcceptedMax);
if(Status != NDIS_STATUS_SUCCESS)
{
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C111: ERROR - No Memory for Adapter Structure!\r\n")));
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C111 <== MiniPort Initialize\r\n")));
return(NDIS_STATUS_RESOURCES);
}
NdisZeroMemory(Adapter, MINIPORT_ADAPTER_SIZE); //Clean it up
Adapter->AdapterHandle = AdapterHandle;
Adapter->State = INITIALIZING_STATE;
Adapter->LookAheadBuffer = NULL;
Adapter->IsInterruptSet =
Adapter->IsPortRegistered = FALSE;
//Allocate the TX Buffer memory
Status = NdisAllocateMemory((PVOID *) &Adapter->TxBuffer,
MAX_FRAME_SIZE,
0,
HighestAcceptedMax);
if(Status != NDIS_STATUS_SUCCESS)
{
RETAILMSG(1, (TEXT("LAN91C111:ERROR : Can't Allocate Tx Buffer!\r\n")));
NdisFreeMemory(Adapter, MINIPORT_ADAPTER_SIZE, 0);
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C111:<== MiniPort Initialize FAILED !!\r\n")));
return(NDIS_STATUS_RESOURCES);
}
else
{
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C111:Allocated Tx Buffer Successfully!\r\n")));
}
//Get the adapter information from the registry
Status = GetRegistrySettings(Adapter, ConfigurationContext);
if(Status != NDIS_STATUS_SUCCESS)
{
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C111: ERROR - Configure Adapter failed!\r\n")));
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C111 <== MiniPort Initialize\r\n")));
BackOut(Adapter);
return(NDIS_STATUS_FAILURE);
}
//Register the interrupt
NdisMSetAttributes(Adapter->AdapterHandle, (NDIS_HANDLE) Adapter, FALSE, NdisInterfaceInternal);
Status = NdisMRegisterInterrupt(&Adapter->InterruptInfo,
Adapter->AdapterHandle,
Adapter->InterruptLevel,
Adapter->InterruptLevel,
TRUE,
FALSE,
NdisInterruptLatched
);
if(Status != NDIS_STATUS_SUCCESS)
{
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C111 : ERROR - Can't Attach to Interrupt!\r\n")));
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C111:<== MiniPort Initialize\r\n")));
BackOut(Adapter);
return(Status);
}
else
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C111:Interrupt Registered !!! \r\n")));
Adapter->IsInterruptSet = TRUE;
#if 0
//Register the IOBase address. Modify this code according to the platform
Status = NdisMRegisterIoPortRange((PVOID *) &(Adapter->IOBase),
Adapter->AdapterHandle,
Adapter->IOBase,
16);
if(Status != NDIS_STATUS_SUCCESS)
{
RETAILMSG(1, (TEXT("LAN91C9111 : ERROR - Can't Register I/O Port Range!\r\n")));
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C9111 <== MiniPort Initialize\r\n")));
BackOut(Adapter);
return(NDIS_STATUS_RESOURCES);
}
#else
#pragma message( "-- WARNING: Change the IO BASE ADDRESS for VirtualAlloc/VirtualCopy functions in LAN91C111_Init.C" )
lpIOBase = VirtualAlloc(0, 0x1000, MEM_RESERVE, PAGE_NOACCESS);
RetVal = VirtualCopy(lpIOBase, (LPVOID)(PXA250_LAN91C111_ADDR/256), 0x1000, PAGE_PHYSICAL | PAGE_READWRITE | PAGE_NOCACHE);
if (RetVal == TRUE)
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C111: VirtualAlloc/Copy Succeeded...........\r\n")));
else
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C111:ERROR : VirtualAlloc/Copy Failed...........\r\n")));
Adapter->IOBase = (UINT)lpIOBase + 0x300;
#endif
Adapter->IsPortRegistered = TRUE;
//Allocate the Adapter Interface Memory.
Status = NdisAllocateMemory((PVOID *) &Adapter->LookAheadBuffer,
LOOK_AHEAD_BUFFER_SIZE,
0,
HighestAcceptedMax);
if(Status != NDIS_STATUS_SUCCESS)
{
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C111:ERROR : Can't Allocate LookAhead Buffer!\r\n")));
BackOut(Adapter);
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C111:<== MiniPort Initialize\r\n")));
return(NDIS_STATUS_FAILURE);
}
else
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C111:Allocated LookAhead Buffer Successfully!\r\n")));
if(!AdapterVerify(Adapter))
{
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C111:Adapter failed to Verify!\r\n")));
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C111:<== MiniPort Initialize\r\n")));
return(NDIS_STATUS_FAILURE);
}
//Disable all interrupts until the initzing is completed
NdisRawWritePortUshort( Adapter->IOBase + BANK_SELECT, 2 );
NdisRawWritePortUshort( Adapter->IOBase + BANK2_INT_STS, 0 );
//Check for the MAC Address
//If Adapter->MACAddress != NULL then we need to write the new mac address to the LAN91C111 register
//Else read the LAN91C111 register and store the mac addr. in Adapter->MACAddress
if (Adapter->MACAddress[0] == 0 &&
Adapter->MACAddress[1] == 0 &&
Adapter->MACAddress[2] == 0 &&
Adapter->MACAddress[3] == 0 &&
Adapter->MACAddress[4] == 0 &&
Adapter->MACAddress[5] == 0 )
{
//Read the MAC address from the register
NdisRawWritePortUshort(Adapter->IOBase + BANK_SELECT, 1);
NdisRawReadPortUshort(Adapter->IOBase + BANK1_IA0, &temp);
Adapter->MACAddress[1] = (UCHAR) (temp >> 8);
Adapter->MACAddress[0] = (UCHAR) (temp & 0xFF);
NdisRawReadPortUshort(Adapter->IOBase + BANK1_IA2, &temp);
Adapter->MACAddress[3] = (UCHAR) (temp >> 8);
Adapter->MACAddress[2] = (UCHAR) (temp & 0xFF);
NdisRawReadPortUshort(Adapter->IOBase + BANK1_IA4, &temp);
Adapter->MACAddress[5] = (UCHAR) (temp >> 8);
Adapter->MACAddress[4] = (UCHAR) (temp & 0xFF);
}
else
{
//Write the new MAC address to the register
NdisRawWritePortUshort(Adapter->IOBase + BANK_SELECT, 1);
temp = Adapter->MACAddress[1];
temp <<= 8;
temp |= Adapter->MACAddress[0];
NdisRawWritePortUshort(Adapter->IOBase + BANK1_IA0, temp);
temp = Adapter->MACAddress[3];
temp <<= 8;
temp |= Adapter->MACAddress[2];
NdisRawWritePortUshort(Adapter->IOBase + BANK1_IA2, temp);
temp = Adapter->MACAddress[5];
temp <<= 8;
temp |= Adapter->MACAddress[4];
NdisRawWritePortUshort(Adapter->IOBase + BANK1_IA4, temp);
}
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C111: MAC Address - %02X-%02X-%02X-%02X-%02X-%02X\r\n"),
Adapter->MACAddress[0],
Adapter->MACAddress[1],
Adapter->MACAddress[2],
Adapter->MACAddress[3],
Adapter->MACAddress[4],
Adapter->MACAddress[5]
));
//Reset the card
if(!AdapterReset(Adapter))
{
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C111: ERROR Can't Reset the Adapter!\r\n")));
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C111:<== MiniPort Initialize\r\n")));
BackOut(Adapter);
return(NDIS_STATUS_FAILURE);
}
//Enable the interrupts !!
NdisRawWritePortUshort(Adapter->IOBase + BANK_SELECT,(USHORT) 2);
NdisRawWritePortUshort( Adapter->IOBase + BANK2_INT_STS,(USHORT)(ENABLED_INTS << 8));
PrintDebugMsg(ZONE_INIT, (TEXT("LAN91C111 <== MiniportInitialize \r\n")));
//Ready
Adapter->State = NORMAL_STATE;
return NDIS_STATUS_SUCCESS;
}
/*
Function Name : GetRegistrySettings
Description :
Reads the registry values, and loads them into the adapter structure
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -