📄 cs89x0.c
字号:
/* cs89x0.c: A Crystal Semiconductor CS89[02]0 driver for linux. *//* Written 1996 by Russell Nelson, with reference to skeleton.c written 1993-1994 by Donald Becker. This software may be used and distributed according to the terms of the GNU Public License, incorporated herein by reference. The author may be reached at nelson@crynwr.com, Crynwr Software, 521 Pleasant Valley Rd., Potsdam, NY 13676 Changelog: Mike Cruse : mcruse@cti-ltd.com : Changes for Linux 2.0 compatibility. : Added dev_id parameter in net_interrupt(), : request_irq() and free_irq(). Just NULL for now. Mike Cruse : Added MOD_INC_USE_COUNT and MOD_DEC_USE_COUNT macros : in net_open() and net_close() so kerneld would know : that the module is in use and wouldn't eject the : driver prematurely. Mike Cruse : Rewrote init_module() and cleanup_module using 8390.c : as an example. Disabled autoprobing in init_module(), : not a good thing to do to other devices while Linux : is running from all accounts. Russ Nelson : Jul 13 1998. Added RxOnly DMA support. Melody Lee : Aug 10 1999. Changes for Linux 2.2.5 compatibility. email: ethernet@crystal.cirrus.com*/static char *version ="cs89x0.c:v2.0 12/27/96 Russell Nelson <nelson@crynwr.com>";/* ======================= configure the driver here ======================= *//* we can do some optimizations that *require* that the Packet Page be mapped into memory, but you have to change the following line to get them. We automatically use memory for bulk transfer if the Packet Page is found to have a memory map. */#define REQUIRE_MEMORY 0/* we can use a DMA buffer to receiving packets. It takes more memory. */#define ALLOW_DMA 0/* use 0 for production, 1 for verification, >2 for debug */#ifndef NET_DEBUG#define NET_DEBUG 1#endif/* ======================= end of configuration ======================= */#ifdef MODULE#include <linux/module.h>#include <linux/version.h>#else#define MOD_INC_USE_COUNT#define MOD_DEC_USE_COUNT#endif#define PRINTK(x) printk x/* Sources: Crynwr packet driver epktisa. Crystal Semiconductor data sheets.*/#include <linux/kernel.h>#include <linux/sched.h>#include <linux/types.h>#include <linux/fcntl.h>#include <linux/interrupt.h>#include <linux/ptrace.h>#include <linux/ioport.h>#include <linux/in.h>#include <linux/malloc.h>#include <linux/string.h>#include <asm/system.h>#include <asm/bitops.h>#include <asm/io.h>#include <asm/dma.h>#include <linux/errno.h>#include <linux/init.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/skbuff.h>#include "cs89x0.h"/* First, a few definitions that the brave might change. *//* A zero-terminated list of I/O addresses to be probed. */static unsigned int netcard_portlist[] __initdata = { 0x300, 0x320, 0x340, 0x360, 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0, 0};static unsigned int net_debug = NET_DEBUG;/* The number of low I/O ports used by the ethercard. */#define NETCARD_IO_EXTENT 16/* we allow the user to override various values normally set in the EEPROM */#define FORCE_RJ45 0x0001 /* pick one of these three */#define FORCE_AUI 0x0002#define FORCE_BNC 0x0004#define FORCE_AUTO 0x0010 /* pick one of these three */#define FORCE_HALF 0x0020#define FORCE_FULL 0x0030/* Information that need to be kept for each board. */struct net_local { struct net_device_stats stats; int chip_type; /* one of: CS8900, CS8920, CS8920M */ char chip_revision; /* revision letter of the chip ('A'...) */ int send_cmd; /* the proper send command: TX_NOW, TX_AFTER_381, or TX_AFTER_ALL */ int auto_neg_cnf; /* auto-negotiation word from EEPROM */ int adapter_cnf; /* adapter configuration from EEPROM */ int isa_config; /* ISA configuration from EEPROM */ int irq_map; /* IRQ map from EEPROM */ int rx_mode; /* what mode are we in? 0, RX_MULTCAST_ACCEPT, or RX_ALL_ACCEPT */ int curr_rx_cfg; /* a copy of PP_RxCFG */ int linectl; /* either 0 or LOW_RX_SQUELCH, depending on configuration. */ int send_underrun; /* keep track of how many underruns in a row we get */ int force; /* force various values; see FORCE* above. */ struct sk_buff *Gskb; /* pointer to TX buff waiting for Ready4Tx*/#if ALLOW_DMA int dma; /* DMA channel */ int dmasize; /* 16 or 64 */ unsigned char *dma_buff; /* points to the beginning of the buffer */ unsigned char *end_dma_buff; /* points to the end of the buffer */ unsigned char *rx_dma_ptr; /* points to the next packet */#endif};/* Index to functions, as function prototypes. */extern int cs89x0_probe(struct device *dev);static int cs89x0_probe1(struct device *dev, int ioaddr);static int net_open(struct device *dev);static int net_send_packet(struct sk_buff *skb, struct device *dev);static void net_interrupt(int irq, void *dev_id, struct pt_regs *regs);static void set_multicast_list(struct device *dev);static void net_rx(struct device *dev);static int net_close(struct device *dev);static struct net_device_stats *net_get_stats(struct device *dev);static void reset_chip(struct device *dev);static int get_eeprom_data(struct device *dev, int off, int len, int *buffer);static int get_eeprom_cksum(int off, int len, int *buffer);static int set_mac_address(struct device *dev, void *addr);static void get_dma_channel(struct device *dev);static void count_rx_errors(int status, struct net_local *lp);/* Example routines you must write ;->. */#define tx_done(dev) 1/* Check for a network adaptor of this type, and return '0' iff one exists. If dev->base_addr == 0, probe all likely locations. If dev->base_addr == 1, always return failure. If dev->base_addr == 2, allocate space for the device and return success (detachable devices only). */#ifdef HAVE_DEVLIST/* Support for a alternate probe manager, which will eliminate the boilerplate below. */struct netdev_entry netcard_drv ={"netcard", cs89x0_probe1, NETCARD_IO_EXTENT, netcard_portlist};#else__initfunc(intcs89x0_probe(struct device *dev)){ int i; int base_addr = dev ? dev->base_addr : 0; if (base_addr > 0x1ff) /* Check a single specified location. */ return cs89x0_probe1(dev, base_addr); else if (base_addr != 0) /* Don't probe at all. */ return ENXIO; for (i = 0; netcard_portlist[i]; i++) { int ioaddr = netcard_portlist[i]; if (check_region(ioaddr, NETCARD_IO_EXTENT)) continue; if (cs89x0_probe1(dev, ioaddr) == 0) return 0; } printk(KERN_NOTICE "cs89x0: no cs8900 or cs8920 detected. Be sure to disable PnP with SETUP\n"); return ENODEV;}#endifstatic int inlinereadreg_io(struct device *dev, int portno){ outw(portno, dev->base_addr + ADD_PORT); return inw(dev->base_addr + DATA_PORT);}static void inlinewritereg_io(struct device *dev, int portno, int value){ outw(portno, dev->base_addr + ADD_PORT); outw(value, dev->base_addr + DATA_PORT);}static int inlinereadreg(struct device *dev, int portno){#if REQUIRE_MEMORY return readw(dev->mem_start + portno);#else outw(portno, dev->base_addr + ADD_PORT); return inw(dev->base_addr + DATA_PORT);#endif}static void inlinewritereg(struct device *dev, int portno, int value){#if REQUIRE_MEMORY writew(value, dev->mem_start+portno);#else outw(portno, dev->base_addr + ADD_PORT); outw(value, dev->base_addr + DATA_PORT);#endif}static int inlinereadword(struct device *dev, int portno){#if REQUIRE_MEMORY return readw(dev->mem_start + portno);#else return inw(dev->base_addr + portno);#endif}static void inlinewriteword(struct device *dev, int portno, int value){#if REQUIRE_MEMORY writew(value, dev->mem_start + portno);#else outw(value, dev->base_addr + portno);#endif}__initfunc(intwait_eeprom_ready(struct device *dev)){ int timeout = jiffies; /* check to see if the EEPROM is ready, a timeout is used - just in case EEPROM is ready when SI_BUSY in the PP_SelfST is clear */ while(readreg_io(dev, PP_SelfST) & SI_BUSY) if (jiffies - timeout >= 40) return -1; return 0;}__initfunc(intget_eeprom_data(struct device *dev, int off, int len, int *buffer)){ int i; if (net_debug > 3) printk(KERN_DEBUG "EEPROM data from %x for %x:\n",off,len); for (i = 0; i < len; i++) { if (wait_eeprom_ready(dev) < 0) return -1; /* Now send the EEPROM read command and EEPROM location to read */ writereg_io(dev, PP_EECMD, (off + i) | EEPROM_READ_CMD); if (wait_eeprom_ready(dev) < 0) return -1; buffer[i] = readreg_io(dev, PP_EEData); if (net_debug > 3) printk(KERN_DEBUG "%04x ", buffer[i]); } if (net_debug > 3) printk(KERN_DEBUG "\n"); return 0;}__initfunc(intget_eeprom_cksum(int off, int len, int *buffer)){ int i, cksum; cksum = 0; for (i = 0; i < len; i++) cksum += buffer[i]; cksum &= 0xffff; if (cksum == 0) return 0; return -1;}/* This is the real probe routine. Linux has a history of friendly device probes on the ISA bus. A good device probes avoids doing writes, and verifies that the correct device exists and functions. */__initfunc(static int cs89x0_probe1(struct device *dev, int ioaddr)){ struct net_local *lp; static unsigned version_printed = 0; int i; unsigned rev_type = 0; int eeprom_buff[CHKSUM_LEN]; int tmpID; /* Initialize the device structure. */ if (dev->priv == NULL) { dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL); memset(dev->priv, 0, sizeof(struct net_local)); } lp = (struct net_local *)dev->priv; /* if they give us an odd I/O address, then do ONE write to the address port, to get it back to address zero, where we expect to find the EISA signature word. */ if (ioaddr & 1) { ioaddr &= ~ 1; if ((inw(ioaddr + ADD_PORT) & ADD_MASK) != ADD_SIG) return ENODEV; outw(PP_ChipID, ioaddr + ADD_PORT); } if (inw(ioaddr + DATA_PORT) != CHIP_EISA_ID_SIG) return ENODEV; /* Fill in the 'dev' fields. */ dev->base_addr = ioaddr; /* get the chip type */ rev_type = readreg_io(dev, PRODUCT_ID_ADD); lp->chip_type = rev_type &~ REVISON_BITS; lp->chip_revision = ((rev_type & REVISON_BITS) >> 8) + 'A'; /* Check the chip type and revision in order to set the correct send command CS8920 revision C and CS8900 revision F can use the faster send. */ lp->send_cmd = TX_AFTER_381; if (lp->chip_type == CS8900 && lp->chip_revision >= 'F') lp->send_cmd = TX_NOW; if (lp->chip_type != CS8900 && lp->chip_revision >= 'C') lp->send_cmd = TX_NOW; if (net_debug && version_printed++ == 0) printk(KERN_INFO "%s\n", version); printk(KERN_INFO "%s: cs89%c0%s rev %c found at %#3lx", dev->name, lp->chip_type==CS8900?'0':'2', lp->chip_type==CS8920M?"M":"", lp->chip_revision, dev->base_addr); reset_chip(dev); if ((readreg_io(dev, PP_SelfST) & EEPROM_PRESENT) == 0) { printk("\n" KERN_WARNING "cs89x0: No EEPROM, relying on command line....\n"); } else if (get_eeprom_data(dev, START_EEPROM_DATA,CHKSUM_LEN,eeprom_buff) < 0) { printk("\n" KERN_WARNING "cs89x0: EEPROM read failed, relying on command line.\n"); } else if (get_eeprom_cksum(START_EEPROM_DATA,CHKSUM_LEN,eeprom_buff) < 0) { printk("\n" KERN_WARNING "cs89x0: EEPROM checksum bad, relying on command line\n"); } else { lp->auto_neg_cnf = eeprom_buff[AUTO_NEG_CNF_OFFSET/2]; lp->adapter_cnf = eeprom_buff[ADAPTER_CNF_OFFSET/2]; lp->isa_config = eeprom_buff[ISA_CNF_OFFSET/2];#if REQUIRE_MEMORY if (dev->mem_start == 0) { dev->mem_start = eeprom_buff[PACKET_PAGE_OFFSET/2] << 8; }#endif /* eeprom_buff has 32-bit ints, so we can't just memcpy it */ for (i = 0; i < ETH_ALEN/2; i++) { dev->dev_addr[i*2] = eeprom_buff[i]; dev->dev_addr[i*2+1] = eeprom_buff[i] >> 8; } } /* allow them to force multiple transceivers. If they force multiple, autosense */ { int count = 0; if (lp->force & FORCE_RJ45) {lp->adapter_cnf |= A_CNF_10B_T; count++; } if (lp->force & FORCE_AUI) {lp->adapter_cnf |= A_CNF_AUI; count++; } if (lp->force & FORCE_BNC) {lp->adapter_cnf |= A_CNF_10B_2; count++; } if (count > 1) lp->adapter_cnf |= A_CNF_MEDIA_AUTO; else if (lp->force & FORCE_RJ45) lp->adapter_cnf |= A_CNF_MEDIA_10B_T; else if (lp->force & FORCE_AUI) lp->adapter_cnf |= A_CNF_MEDIA_AUI; else if (lp->force & FORCE_BNC) lp->adapter_cnf |= A_CNF_MEDIA_10B_2; } /* We don't let you set dc-dc polarity or low RX squelch from the command line: add it here */ /* We don't let you set the IMM bit from the command line: add it to lp->auto_neg_cnf here */ /* we don't set the Ethernet address on the command line. Use ifconfig IFACE hw ether AABBCCDDEEFF */ /* For lp->isa_config, we use int=. We don't allow stream transfer from the command line */#if REQUIRE_MEMORY if (dev->mem_start) { dev->mem_end = dev->mem_start + 0x10000; printk(" memory %5lx,", dev->mem_start); if (lp->chip_type == CS8900) { writereg_io(dev, PP_CS8900_ISAMemB, dev->mem_start); writereg_io(dev, PP_CS8900_ISAMemB+2, dev->mem_start >>16); } else { writereg_io(dev, PP_CS8920_ISAMemB, (dev->mem_start & 0xff00) | (dev->mem_start >> 16 & 0xff)); } writereg_io(dev, PP_BusCTL, readreg_io(dev, PP_BusCTL)|MEMORY_ON); tmpID=readw(dev->mem_start); if ( (tmpID & 0xffff) != 0x630e) { printk("cs89x0: %lx isn't our memory!", dev->mem_start); dev->mem_start = 0; } } if (dev->mem_start == 0) { printk(" has no memory map -- not installed\n"); return ENODEV; }#endif printk(" media %s%s%s", (lp->adapter_cnf & A_CNF_10B_T)?"RJ-45,":"", (lp->adapter_cnf & A_CNF_AUI)?"AUI,":"", (lp->adapter_cnf & A_CNF_10B_2)?"BNC,":""); lp->irq_map = 0xffff; /* If this is a CS8900 then no pnp soft */ if (lp->chip_type != CS8900 && /* Check if the ISA IRQ has been set */ (i = readreg(dev, PP_CS8920_ISAINT) & 0xff, (i != 0 && i < CS8920_NO_INTS))) { if (!dev->irq) dev->irq = i; } else { i = lp->isa_config & INT_NO_MASK;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -