📄 cmd9118.c
字号:
/***************************************************************************
*
* Copyright (C) 2004-2005 SMSC
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
***************************************************************************
* File: cmd9118.c
*/
#ifdef USING_LINT
#include "lint.h"
#else //not USING_LINT
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <linux/delay.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <net/if_arp.h>
#include <arpa/inet.h>
#endif //not USING_LINT
#include "ioctl_118.h"
char *iam=NULL;
typedef enum _bool {
false=0,
true=1
} bool;
typedef struct _COMMAND_DATA {
int hSockFD;
struct ifreq IfReq;
SMSC9118_IOCTL_DATA IoctlData;
} COMMAND_DATA, *PCOMMAND_DATA;
#define SOCKET int
#define INVALID_SOCKET (-1)
#define SOCKET_ERROR (-1)
#define DEFAULT_PORT_NUMBER (11118U)
SOCKET server_sock=INVALID_SOCKET;
typedef struct _FLOW_PARAMS
{
unsigned long MeasuredMaxThroughput;
unsigned long MeasuredMaxPacketCount;
unsigned long MaxThroughput;
unsigned long MaxPacketCount;
unsigned long PacketCost;
unsigned long BurstPeriod;
unsigned long MaxWorkLoad;
unsigned long IntDeas;
} FLOW_PARAMS, * PFLOW_PARAMS;
bool ParseNumber(const char *str,unsigned long *number);
void DisplayUsage(void);
void GetMacAddress(PCOMMAND_DATA commandData);
void SetMacAddress(PCOMMAND_DATA commandData,unsigned long addrh,unsigned long addrl);
void LoadMacAddress(PCOMMAND_DATA commandData);
void SaveMacAddress(PCOMMAND_DATA commandData,unsigned long addrh,unsigned long addrl);
void LanDumpRegs(PCOMMAND_DATA commandData);
void MacDumpRegs(PCOMMAND_DATA commandData);
void DumpEEPROM(PCOMMAND_DATA commandData);
void DumpTemp(PCOMMAND_DATA commandData);
void PhyDumpRegs(PCOMMAND_DATA commandData);
void SetDebugMode(PCOMMAND_DATA commandData,
unsigned long debug_mode);
void SetLinkMode(PCOMMAND_DATA commandData,
unsigned long link_mode);
void SetPowerMode(PCOMMAND_DATA commandData,
unsigned long power_mode);
void GetLinkMode(PCOMMAND_DATA commandData);
void CheckLink(PCOMMAND_DATA commandData);
void GetPowerMode(PCOMMAND_DATA commandData);
void GetFlowParams(PCOMMAND_DATA commandData);
void GetConfiguration(PCOMMAND_DATA commandData);
void ReadByte(PCOMMAND_DATA commandData,unsigned long address);
void ReadWord(PCOMMAND_DATA commandData, unsigned long address);
void ReadDWord(PCOMMAND_DATA commandData,unsigned long address);
void WriteByte(PCOMMAND_DATA commandData,unsigned long address, unsigned long data);
void WriteWord(PCOMMAND_DATA commandData,unsigned long address, unsigned long data);
void WriteDWord(PCOMMAND_DATA commandData,unsigned long address, unsigned long data);
void LanGetReg(PCOMMAND_DATA commandData,unsigned long address);
void LanSetReg(PCOMMAND_DATA commandData, unsigned long address, unsigned long data);
void MacGetReg(PCOMMAND_DATA commandData, unsigned long address);
void MacSetReg(
PCOMMAND_DATA commandData,
unsigned long address, unsigned long data);
void PhyGetReg(PCOMMAND_DATA commandData, unsigned long address);
void PhySetReg(
PCOMMAND_DATA commandData,
unsigned long address, unsigned long data);
bool Initialize(PCOMMAND_DATA commandData,const char *ethName);
bool ReceiveULong(SOCKET sock,unsigned long * pDWord);
bool SendULong(SOCKET sock,unsigned long data);
void process_requests(PCOMMAND_DATA commandData);
void RunServer(PCOMMAND_DATA commandData,unsigned short portNumber);
bool ReceiveFlowParams(SOCKET sock,PFLOW_PARAMS flowParams);
bool SendFlowParams(SOCKET sock,PFLOW_PARAMS flowParams);
void DisplayFlowParams(PFLOW_PARAMS flowParams);
unsigned long ReadThroughput(char * fileName);
void RunTuner(const char *hostName,unsigned short portNum);
bool ParseNumber(const char *str,unsigned long *number) {
if(str==NULL) return false;
if(str[0]==0) return false;
if((str[0]=='0')&&(str[1]=='x')) {
if(sscanf(&(str[2]),"%lx",number)==1) {
return true;
}
}
if(sscanf(str,"%ld",number)==1) {
return true;
}
return false;
}
void DisplayUsage(void) {
printf("usage: %s [-h] [-e adaptername] [-c command] [-a address] [-d data]\n",iam);
printf(" [-H host_address] [-p port_number]\n");
printf(" -h displays this usage information, other options ignored.\n");
printf(" -e specifies the adapter name (eth0,eth1...)\n");
printf(" if not specified then %s will attempt to\n",iam);
printf(" auto detect.\n");
printf(" -c specifies the command code\n");
printf(" SERVER = run in server mode\n");
printf(" may use -p to specify port to listen on\n");
printf(" TUNER = connects remotely to server to run\n");
printf(" automatic flow control tuning.\n");
printf(" use -H to specify host address\n");
printf(" use -p to specify port\n");
printf(" GET_CONFIG = gets internal variables of driver\n");
printf(" DUMP_REGS = dumps the LAN9118 memory mapped registers\n");
printf(" DUMP_MAC = dumps the LAN9118 MAC registers\n");
printf(" DUMP_PHY = dumps the LAN9118 PHY registers\n");
printf(" DUMP_EEPROM = dumps the first 8 bytes of the EEPROM\n");
printf(" DUMP_TEMP = dumps temp data space used for debugging\n");
printf(" GET_MAC = gets MAC address from ADDRH and ADDRL\n");
printf(" SET_MAC = sets MAC address in ADDRH and ADDRL\n");
printf(" -a specifies the value to write to ADDRH\n");
printf(" -d specifies the value to write to ADDRL\n");
printf(" LOAD_MAC = causes the LAN9118 to reload the MAC address\n");
printf(" from the external EEPROM. Also displays it\n");
printf(" SAVE_MAC = writes a MAC address to the EEPROM\n");
printf(" -a specifies the part of the MAC address that would\n");
printf(" appear in ADDRH\n");
printf(" -d specifies the part of the MAC address that would\n");
printf(" appear in ADDRL\n");
printf(" SET_DEBUG = sets the driver's internal debug_mode value\n");
printf(" -d specifies the debug mode\n");
printf(" 0x01, bit 0, enables trace messages\n");
printf(" 0x02, bit 1, enables warning messages\n");
printf(" 0x04, bit 2, enables GPO signals\n");
printf(" NOTE: trace, and warning messages will only show if\n");
printf(" they have been turned on at driver compile time.\n");
printf(" SET_LINK = sets the driver's internal link_mode value\n");
printf(" and also attempts to relink with the new setting\n");
printf(" -d specifies the link mode\n");
printf(" 1 = 10HD, 2 = 10FD, 4 = 100HD, 8 = 100FD\n");
printf(" to specify multiple link modes, add the values\n");
printf(" of each mode you want and use the sum as the link mode\n");
printf(" GET_LINK = gets the driver's internal link_mode value\n");
printf(" Warning!! Power management is not implemented as of version 0.54\n");
printf(" SET_POWER = sets the LAN9118 power mode\n");
printf(" -d specifies the power state\n");
printf(" 0 = D0, 1 = D1, 2 = D2, 3 = D3\n");
printf(" GET_POWER = gets the LAN9118 power mode\n");
printf(" CHECK_LINK = causes the driver to recheck its link status\n");
printf(" Warning!! the following read and write commands may cause\n");
printf(" unpredictable results, including system lock up or crash.\n");
printf(" Use with caution\n");
printf(" READ_REG = reads a value from the LAN9118 Memory Map\n");
printf(" -a specifies offset into LAN9118 Memory Map\n");
printf(" WRITE_REG = writes a value to the LAN9118 Memory Map\n");
printf(" -a specifies offset into LAN9118 Memory Map\n");
printf(" -d specifies data to write in HEX form\n");
printf(" READ_MAC = reads a value from the LAN9118 Mac registers\n");
printf(" -a specifies the Mac register index\n");
printf(" WRITE_MAC = writes a value to the LAN9118 Mac registers\n");
printf(" -a specifies the Mac register index\n");
printf(" -d specifies data to write in HEX form\n");
printf(" READ_PHY = reads a value from the LAN9118 Phy registers\n");
printf(" -a specifies the Phy register index\n");
printf(" WRITE_PHY = writes a value to the LAN9118 Phy registers\n");
printf(" -a specifies the Phy register index\n");
printf(" -d specifies data to write in HEX form\n");
printf(" READ_BYTE = reads a byte from a location in memory\n");
printf(" -a address\n");
printf(" READ_WORD = reads a word from a location in memory\n");
printf(" -a address\n");
printf(" READ_DWORD = reads a dword from a location in memory\n");
printf(" -a address\n");
printf(" WRITE_BYTE = write a byte to a location in memory\n");
printf(" -a address -d data\n");
printf(" WRITE_WORD = write a word to a location in memory\n");
printf(" -a address -d data\n");
printf(" WRITE_DWORD = write a dword to a location in memory\n");
printf(" -a address -d data\n");
printf(" -a specifies the address, index, or offset of a register\n");
printf(" -d specifies the data to write to a register\n");
printf(" can be decimal or hexadecimal\n");
}
void GetMacAddress(PCOMMAND_DATA commandData)
{
if(commandData==NULL) return;
commandData->IfReq.ifr_data=(void *)&(commandData->IoctlData);
commandData->IoctlData.dwSignature=SMSC9118_APP_SIGNATURE;
commandData->IoctlData.dwCommand=COMMAND_GET_MAC_ADDRESS;
ioctl(commandData->hSockFD,SMSC9118_IOCTL,&(commandData->IfReq));
if(commandData->IoctlData.dwSignature==SMSC9118_DRIVER_SIGNATURE) {
printf("Mac Address == 0x%04lX%08lX\n",
commandData->IoctlData.Data[0],
commandData->IoctlData.Data[1]);
} else {
printf("Failed to Get Mac Address\n");
}
}
void SetMacAddress(PCOMMAND_DATA commandData,unsigned long addrh,unsigned long addrl)
{
if(commandData==NULL) return;
commandData->IfReq.ifr_data=(void *)&(commandData->IoctlData);
commandData->IoctlData.dwSignature=SMSC9118_APP_SIGNATURE;
commandData->IoctlData.dwCommand=COMMAND_SET_MAC_ADDRESS;
commandData->IoctlData.Data[0]=addrh;
commandData->IoctlData.Data[1]=addrl;
ioctl(commandData->hSockFD,SMSC9118_IOCTL,&(commandData->IfReq));
if(commandData->IoctlData.dwSignature!=SMSC9118_DRIVER_SIGNATURE) {
printf("Failed to Set Mac Address\n");
}
}
void LoadMacAddress(PCOMMAND_DATA commandData)
{
if(commandData==NULL) return;
commandData->IfReq.ifr_data=(void *)&(commandData->IoctlData);
commandData->IoctlData.dwSignature=SMSC9118_APP_SIGNATURE;
commandData->IoctlData.dwCommand=COMMAND_LOAD_MAC_ADDRESS;
ioctl(commandData->hSockFD,SMSC9118_IOCTL,&(commandData->IfReq));
if(commandData->IoctlData.dwSignature==SMSC9118_DRIVER_SIGNATURE) {
printf("Mac Address == 0x%04lX%08lX\n",
commandData->IoctlData.Data[0],
commandData->IoctlData.Data[1]);
} else {
printf("Failed to Load Mac Address\n");
}
}
void SaveMacAddress(PCOMMAND_DATA commandData,unsigned long addrh,unsigned long addrl)
{
if(commandData==NULL) return;
commandData->IfReq.ifr_data=(void *)&(commandData->IoctlData);
commandData->IoctlData.dwSignature=SMSC9118_APP_SIGNATURE;
commandData->IoctlData.dwCommand=COMMAND_SAVE_MAC_ADDRESS;
commandData->IoctlData.Data[0]=addrh;
commandData->IoctlData.Data[1]=addrl;
ioctl(commandData->hSockFD,SMSC9118_IOCTL,&(commandData->IfReq));
if(commandData->IoctlData.dwSignature!=SMSC9118_DRIVER_SIGNATURE) {
printf("Failed to Save Mac Address\n");
}
}
void LanDumpRegs(PCOMMAND_DATA commandData)
{
if(commandData==NULL) return;
commandData->IfReq.ifr_data=(void *)&(commandData->IoctlData);
commandData->IoctlData.dwSignature=SMSC9118_APP_SIGNATURE;
commandData->IoctlData.dwCommand=COMMAND_DUMP_LAN_REGS;
ioctl(commandData->hSockFD,SMSC9118_IOCTL,&(commandData->IfReq));
if(commandData->IoctlData.dwSignature==SMSC9118_DRIVER_SIGNATURE) {
printf("offset 0x50, ID_REV = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_ID_REV]);
printf("offset 0x54, INT_CFG = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_INT_CFG]);
printf("offset 0x58, INT_STS = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_INT_STS]);
printf("offset 0x5C, INT_EN = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_INT_EN]);
printf("offset 0x64, BYTE_TEST = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_BYTE_TEST]);
printf("offset 0x68, FIFO_INT = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_FIFO_INT]);
printf("offset 0x6C, RX_CFG = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_RX_CFG]);
printf("offset 0x70, TX_CFG = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_TX_CFG]);
printf("offset 0x74, HW_CFG = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_HW_CFG]);
printf("offset 0x78, RX_DP_CTRL = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_RX_DP_CTRL]);
printf("offset 0x7C, RX_FIFO_INF = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_RX_FIFO_INF]);
printf("offset 0x80, TX_FIFO_INF = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_TX_FIFO_INF]);
printf("offset 0x84, PMT_CTRL = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_PMT_CTRL]);
printf("offset 0x88, GPIO_CFG = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_GPIO_CFG]);
printf("offset 0x8C, GPT_CFG = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_GPT_CFG]);
printf("offset 0x90, GPT_CNT = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_GPT_CNT]);
printf("offset 0x94, FPGA_REV = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_FPGA_REV]);
printf("offset 0x98, ENDIAN = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_ENDIAN]);
printf("offset 0x9C, FREE_RUN = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_FREE_RUN]);
printf("offset 0xA0, RX_DROP = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_RX_DROP]);
printf("offset 0xA4, MAC_CSR_CMD = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_MAC_CSR_CMD]);
printf("offset 0xA8, MAC_CSR_DATA = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_MAC_CSR_DATA]);
printf("offset 0xAC, AFC_CFG = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_AFC_CFG]);
printf("offset 0xB0, E2P_CMD = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_E2P_CMD]);
printf("offset 0xB4, E2P_DATA = 0x%08lX\n",commandData->IoctlData.Data[LAN_REG_E2P_DATA]);
} else {
printf("Failed to DUMP registers\n");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -