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

📄 lan9118.c

📁 2410平台vxworks下的lan9118驱动。
💻 C
📖 第 1 页 / 共 4 页
字号:
/* lan9118.c - lan9118 Enhanced Network Driver (END) *//* includes */#include "vxWorks.h"#include "stdlib.h"#include "cacheLib.h"#include "intLib.h"#include "end.h"			/* Common END structures. */#include "endLib.h"#include "lstLib.h"			/* Needed to maintain protocol list. */#include "iv.h"#include "semLib.h"#include "logLib.h"#include "netLib.h"#include "stdio.h"#include "sysLib.h"#include "errno.h"#include "errnoLib.h"#include "memLib.h"#include "iosLib.h"#undef	ETHER_MAP_IP_MULTICAST#include "etherMultiLib.h"		/* multicast stuff. */#include "net/mbuf.h"#include "net/unixLib.h"#include "net/protosw.h"#include "net/systm.h"#include "net/if_subr.h"#include "net/route.h"#include "netinet/if_ether.h"#include "sys/socket.h"#include "sys/ioctl.h"#include "sys/times.h"#include "string.h"/* addition. */#include "lan9118.h"#include "rngLib.h"/* Maximum number of lan9118 chips */#define MAXUNITS    1IMPORT	int endMultiLstCnt (END_OBJ* pEnd);/* defines */#define	DRV_NAME	"lan"#define DRV_NAME_LEN	(sizeof(DRV_NAME) + 1)#define DRV_DESC	"SMSC LAN9118 END driver"#define DRV_DESC_LEN	(sizeof(DRV_DESC) + 1)/* Configuration items */#ifndef ETHERMTU#define ETHERMTU (1500)#endif#define END_BUFSIZ	(ETHERMTU + SIZEOF_ETHERHEADER + 6)#define EH_SIZE		(14)#define END_SPEED_10M	(10000000)	/* 10Mbs */#define END_SPEED_100M	(100000000)	/* 100Mbs */#define END_SPEED	END_SPEED_10M/* A shortcut for getting the hardware address from the MIB II stuff. */#define END_HADDR(pEnd)	((pEnd)->mib2Tbl.ifPhysAddress.phyAddress)#define END_HADDR_LEN(pEnd) ((pEnd)->mib2Tbl.ifPhysAddress.addrLength)/* typedefs *//* The definition of the driver control structure */typedef struct end_device{	END_OBJ			        end;		/* The class we inherit from. */	int			            unit;		/* unit number */	int			            ivec;		/* interrupt vector */	int			            ilevel;		/* interrupt level */	USHORT			        enetAddr[3];	/* ethernet address */	BOOL		            resetting;    int                     do_rx;    SEM_ID                  semid_rx;}END_DEVICE;/* --------------------------------	no. mBlks	no. clBlks	memArea		memSize *//* network mbuf configuration table	---------	----------	-------		------- */M_CL_CONFIG lan9118MclBlkConfig =  {0, 		    0,		    NULL,		0};/* ----------------------------------------	clusterSize	num	memArea		memSize   *//* network cluster pool configuration table -----------	---	-------		--------- */CL_DESC lan9118ClDescTbl [] = 		     {{	0,		    0,	NULL,		0	}};int lan9118ClDescTblNumEnt = (NELEMENTS(lan9118ClDescTbl));#define lan9118_MIN_FBUF	(1536)	/* min first buffer size *//* LOCALS */int dt = 0;int dr = 0;int drc = 0;int drcc = 0;int display_rx_frame = 0;END_DEVICE * p_end_device = (END_DEVICE *)NULL;SEM_ID semid_lan9118_tx_mux;RING_ID ringid_rx;/* forward static functions *//* END Specific interfaces. *//* This is the only externally visible interface. */END_OBJ * 		lan9118Load(char* initString, void* p_v);STATUS	lan9118Start(END_DEVICE* pDrvCtrl);STATUS	lan9118Stop(END_DEVICE* pDrvCtrl);STATUS	lan9118Unload(END_DEVICE* pDrvCtrl);int		lan9118Ioctl(END_DEVICE* pDrvCtrl, int cmd, caddr_t data);STATUS	lan9118Send(END_DEVICE* pDrvCtrl, M_BLK_ID pBuf);STATUS	lan9118MCastAdd(END_DEVICE* pDrvCtrl, char* pAddress);STATUS	lan9118MCastDel(END_DEVICE* pDrvCtrl, char* pAddress);STATUS	lan9118MCastGet(END_DEVICE* pDrvCtrl, MULTI_TABLE* pTable);STATUS	lan9118PollStart(END_DEVICE* pDrvCtrl);STATUS	lan9118PollStop(END_DEVICE* pDrvCtrl);STATUS	lan9118PollSend(END_DEVICE* pDrvCtrl, M_BLK_ID pBuf);STATUS	lan9118PollRcv(END_DEVICE* pDrvCtrl, M_BLK_ID pBuf);void		lan9118Reset(END_DEVICE *pDrvCtrl);void		lan9118Recv(END_DEVICE * pDrvCtrl, char * pNewCluster, int len);void        lan9118RecvInt(END_DEVICE * pDrvCtrl, int count);void		lan9118Config(END_DEVICE *pDrvCtrl);STATUS	lan9118MemInit();void		lan9118Int(END_DEVICE* pDrvCtrl);int lan9118_reg_init(void);U32 lan9118_reg_read(U32 offset);void lan9118_reg_write(U32 offset, U32 src);int lan9118_pin_enable(void);int lan9118_pin_disable(void);STATUS mask32_change_bsp(int* dist32, int num, int len, int src);/* * Declare our function table.  This is static across all device * instances. */NET_FUNCS lan9118FuncTable ={	(FUNCPTR) lan9118Start,	/* Function to start the device. */	(FUNCPTR) lan9118Stop,		/* Function to stop the device. */	(FUNCPTR) lan9118Unload,	/* Unloading function for the driver. */	(FUNCPTR) lan9118Ioctl,	/* Ioctl function for the driver. */	(FUNCPTR) lan9118Send,		/* Send function for the driver. */	(FUNCPTR) lan9118MCastAdd,	/* Multicast add function for the driver. */	(FUNCPTR) lan9118MCastDel,	/* Multicast delete function for the driver. */	(FUNCPTR) lan9118MCastGet,	/* Multicast retrieve function for the driver. */	(FUNCPTR) lan9118PollSend,	/* Polling send function */	(FUNCPTR) lan9118PollRcv,	/* Polling receive function */	endEtherAddressForm,		/* put address info into a NET_BUFFER */	endEtherPacketDataGet,		/* get pointer to data in NET_BUFFER */	endEtherPacketAddrGet	  	/* Get packet addresses. */};/******************************************************************************/typedef struct tag_struct_end_rx_frame{	unsigned char * p_data;	int length;	/* data length */}struct_end_rx_frame;int end_rx_frame_put(int length) /* length...bytes */{	unsigned int * p_u32;	struct_end_rx_frame current_rx_frame;    char * p_buf = (char *)(&current_rx_frame);	int i, temp_int, temp_int2;	if(length > END_BUFSIZ) {		return -1;	} else {        p_u32  = (unsigned int *)netClusterGet (p_end_device->end.pNetPool, p_end_device->end.pNetPool->clTbl[0]);		if(p_u32 == NULL) {			return -1;		}        current_rx_frame.length = length;        current_rx_frame.p_data = (char *)p_u32;        temp_int2 = sizeof(struct_end_rx_frame);        temp_int = 0;        do {            temp_int += rngBufPut(ringid_rx, &(p_buf[temp_int]), temp_int2-temp_int);        } while(temp_int < temp_int2);        /* receive data from lan9118 rx fifo. */        for (i = 0; i < ((length + 3) / 4); i++) {            p_u32[i] = lan9118_reg_read(LAN9118_REG_OFFSET_RX_DATA);        }	}	return 0;}unsigned char * end_rx_frame_get(int * p_length){	struct_end_rx_frame current_frame;    char * p_buf = (char *)(&current_frame);    unsigned char * p_rst;    int temp_int, temp_int2;    temp_int2 = sizeof(struct_end_rx_frame);    temp_int = 0;	/* get current rx frame from the chain. */    do {        temp_int = rngBufGet(ringid_rx, &(p_buf[temp_int]), temp_int2-temp_int);    }while(temp_int < temp_int2);	p_rst = current_frame.p_data;	*p_length = current_frame.length;	return p_rst;}/******************************************************************************/int lan9118_local_delay(int num){    int result = 0;    result = taskDelay(num);    return result;}int lan9118_local_get_rate(){    int result = 0;    result = sysClkRateGet();    return result;}/* * lan9118_mac_busy - test lan9118 mac busy * * This routine test the mac of lan9118 busy, isn't it. * Return: 0, free; *         1, busy. */int lan9118_mac_busy(int timeout){	  while (timeout--) {			if (!(lan9118_reg_read(LAN9118_REG_OFFSET_MAC_CSR_CMD) & 0x80000000)) {				  return 0; /* free. */			}	  }	  return 1;     /* busy */}/* * lan9118_mac_reg_read - read data from mac register of lan9118 * * This routine read data from mac register of lan9118. * Return: unsigned 32bits, the data of mac register. */U32 lan9118_mac_reg_read(U32 mac_suboffset){    U32 result = 0xffffffff;    /* Test previous command complete. */    if (lan9118_mac_busy(1)) {        printf("lan9118_mac_reg_read: previous command not complete!!!\n");        return result;    }    /* Write "read" to mac command register. */    lan9118_reg_write(        LAN9118_REG_OFFSET_MAC_CSR_CMD,        ((mac_suboffset&0x000000ff)|0xc0000000));    /* Test timeout of waiting response from MAC. */    if (lan9118_mac_busy(LAN9118_MAC_TIMEOUT)) {        printf("lan9118_mac_reg_read: timeout waiting for response from MAC!!!\n");        return result;    }    result = lan9118_reg_read(LAN9118_REG_OFFSET_MAC_CSR_DATA);    return result;}/* * lan9118_physical_busy - test lan9118 physical busy * * This routine test the physical of lan9118 busy, isn't it. * Return: 0, free; *         1, busy. */int lan9118_physical_busy(int timeout){    while (timeout--) {        if (!(lan9118_mac_reg_read(LAN9118_REG_SUBOFFSET_MII_ACC)&1)) {            return 0; /* free; */        }    }    return 1;     /* busy; */}/* * lan9118_mac_reg_write - write data to mac register. * * This routine write data to mac register. * Return: 0, fail; *         1, success. */int lan9118_mac_reg_write(U32 mac_suboffset, U32 src){    /* Test previous command complete. */    if (lan9118_mac_busy(1)) {        printf("lan9118_mac_reg_write: previous command not complete!!!\n");        return 0;    }    /* put the source data into the mac data register. */    lan9118_reg_write(LAN9118_REG_OFFSET_MAC_CSR_DATA, src);    /* perform the write operation. */    lan9118_reg_write(        LAN9118_REG_OFFSET_MAC_CSR_CMD,        (mac_suboffset&0x000000ff)|0x80000000        );    /* Test timeout of waiting response from MAC. */    if (lan9118_mac_busy(LAN9118_MAC_TIMEOUT)) {        printf("lan9118_mac_reg_write: timeout waiting for response from MAC!!!\n");        return 0;    }    return 1;}/* * lan9118_phy_reg_read - read data from physical register of lan9118 * * This routine read data from physical register of lan9118. * Return: unsigned 16bits, the data of physical register. */U16 lan9118_phy_reg_read(U32 phy_suboffset){    U16 result = 0xffff;    /* Test MII busy. */    if (lan9118_mac_reg_read(LAN9118_REG_SUBOFFSET_MII_ACC)&1) {        printf("lan9118_phy_reg_read: MII busy!!!\n");        return result;    }    /* Write "read" to mii command register. */    lan9118_mac_reg_write(        LAN9118_REG_SUBOFFSET_MII_ACC,        ((phy_suboffset&0x1f)<<6)|(1<<11)        );    /* Test timeout of waiting response from MII. */    if (lan9118_physical_busy(LAN9118_PHY_TIMEOUT)) {        printf("lan9118_phy_reg_read: timeout waiting for MII command!!!\n");        return result;    }    result = (U16)(lan9118_mac_reg_read(LAN9118_REG_SUBOFFSET_MII_DATA));    return result;}/* * lan9118_phy_reg_write - write data to physical register of lan9118 * * This routine write data to physical register of lan9118. * Return: 0, failed; *         1, success. */int lan9118_phy_reg_write(U32 phy_suboffset, U16 src){    /* Test MII busy. */    if (lan9118_mac_reg_read(LAN9118_REG_SUBOFFSET_MII_ACC)&1) {        printf("lan9118_phy_reg_read: MII busy!!!\n");        return 0;    }    /* Write data to mii data register. */    lan9118_mac_reg_write(        LAN9118_REG_SUBOFFSET_MII_DATA,        (U32)src        );    /* Write "write" to mii command register. */    lan9118_mac_reg_write(        LAN9118_REG_SUBOFFSET_MII_ACC,        ((phy_suboffset&0x1f)<<6)|(1<<11)|2        );    /* Test timeout of waiting response from MII. */    if (lan9118_physical_busy(LAN9118_PHY_TIMEOUT)) {        printf("lan9118_phy_reg_read: timeout waiting for MII command!!!\n");        return 0;    }    return 1;}int lan9118_soft_reset(){    U32 temp_u32;    /* check chip id. */    temp_u32 = lan9118_reg_read(LAN9118_REG_OFFSET_ID_REV);    if ((temp_u32 & LAN9118_REG_ID_REV_ID_MASK)        != LAN9118_REG_ID_REV_VALUE) {        printf("Failed to read the ID of the lan9118 when reset it!!!!\n");        return -1;    }    lan9118_reg_write(LAN9118_REG_OFFSET_HW_CFG, 1);    lan9118_local_delay(lan9118_local_get_rate()/20);    return 0;}/* * lan9118_open - open the lan9118. */int lan9118_open(END_DEVICE * pDrvCtrl){    U32 temp_u32;    int temp_int;    U16 temp_u16;    int click;    click = lan9118_local_get_rate();

⌨️ 快捷键说明

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