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

📄 de620.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *	de620.c $Revision: 1.40 $ BETA * * *	Linux driver for the D-Link DE-620 Ethernet pocket adapter. * *	Portions (C) Copyright 1993, 1994 by Bjorn Ekwall <bj0rn@blox.se> * *	Based on adapter information gathered from DOS packetdriver *	sources from D-Link Inc:  (Special thanks to Henry Ngai of D-Link.) *		Portions (C) Copyright D-Link SYSTEM Inc. 1991, 1992 *		Copyright, 1988, Russell Nelson, Crynwr Software * *	Adapted to the sample network driver core for linux, *	written by: Donald Becker <becker@super.org> *		(Now at <becker@cesdis.gsfc.nasa.gov> * *	Valuable assistance from: *		J. Joshua Kopper <kopper@rtsg.mot.com> *		Olav Kvittem <Olav.Kvittem@uninett.no> *		Germano Caronni <caronni@nessie.cs.id.ethz.ch> *		Jeremy Fitzhardinge <jeremy@suite.sw.oz.au> * *****************************************************************************//* *	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, 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., 675 Mass Ave, Cambridge, MA 02139, USA. * *****************************************************************************/static const char *version =	"de620.c: $Revision: 1.40 $,  Bjorn Ekwall <bj0rn@blox.se>\n";/*********************************************************************** * * "Tuning" section. * * Compile-time options: (see below for descriptions) * -DDE620_IO=0x378	(lpt1) * -DDE620_IRQ=7	(lpt1) * -DDE602_DEBUG=... * -DSHUTDOWN_WHEN_LOST * -DCOUNT_LOOPS * -DLOWSPEED * -DREAD_DELAY * -DWRITE_DELAY *//* * This driver assumes that the printer port is a "normal", * dumb, uni-directional port! * If your port is "fancy" in any way, please try to set it to "normal" * with your BIOS setup.  I have no access to machines with bi-directional * ports, so I can't test such a driver :-( * (Yes, I _know_ it is possible to use DE620 with bidirectional ports...) * * There are some clones of DE620 out there, with different names. * If the current driver does not recognize a clone, try to change * the following #define to: * * #define DE620_CLONE 1 */#define DE620_CLONE 0/* * If the adapter has problems with high speeds, enable this #define * otherwise full printerport speed will be attempted. * * You can tune the READ_DELAY/WRITE_DELAY below if you enable LOWSPEED *#define LOWSPEED */#ifndef READ_DELAY#define READ_DELAY 100	/* adapter internal read delay in 100ns units */#endif#ifndef WRITE_DELAY#define WRITE_DELAY 100	/* adapter internal write delay in 100ns units */#endif/* * Enable this #define if you want the adapter to do a "ifconfig down" on * itself when we have detected that something is possibly wrong with it. * The default behaviour is to retry with "adapter_init()" until success. * This should be used for debugging purposes only. *#define SHUTDOWN_WHEN_LOST *//* * Enable debugging by "-DDE620_DEBUG=3" when compiling, * OR by enabling the following #define * * use 0 for production, 1 for verification, >2 for debug *#define DE620_DEBUG 3 */#ifdef LOWSPEED/* * Enable this #define if you want to see debugging output that show how long * we have to wait before the DE-620 is ready for the next read/write/command. *#define COUNT_LOOPS */#endif#include <linux/module.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/types.h>#include <linux/fcntl.h>#include <linux/string.h>#include <linux/interrupt.h>#include <linux/ioport.h>#include <asm/io.h>#include <linux/in.h>#include <linux/ptrace.h>#include <asm/system.h>#include <linux/errno.h>#include <linux/init.h>#include <linux/inet.h>#include <linux/netdevice.h>#include <linux/etherdevice.h>#include <linux/skbuff.h>/* Constant definitions for the DE-620 registers, commands and bits */#include "de620.h"typedef unsigned char byte;/******************************************************* *                                                     * * Definition of D-Link DE-620 Ethernet Pocket adapter * * See also "de620.h"                                  * *                                                     * *******************************************************/#ifndef DE620_IO /* Compile-time configurable */#define DE620_IO 0x378#endif#ifndef DE620_IRQ /* Compile-time configurable */#define DE620_IRQ	7#endif#define DATA_PORT	(dev->base_addr)#define STATUS_PORT	(dev->base_addr + 1)#define COMMAND_PORT	(dev->base_addr + 2)#define RUNT 60		/* Too small Ethernet packet */#define GIANT 1514	/* largest legal size packet, no fcs */#ifdef DE620_DEBUG /* Compile-time configurable */#define PRINTK(x) if (de620_debug >= 2) printk x#else#define DE620_DEBUG 0#define PRINTK(x) /**/#endif/* * Force media with insmod: *	insmod de620.o bnc=1 * or *	insmod de620.o utp=1 * * Force io and/or irq with insmod: *	insmod de620.o io=0x378 irq=7 * * Make a clone skip the Ethernet-address range check: *	insmod de620.o clone=1 */static int bnc = 0;static int utp = 0;static int io  = DE620_IO;static int irq = DE620_IRQ;static int clone = DE620_CLONE;static unsigned int de620_debug = DE620_DEBUG;MODULE_PARM(bnc, "i");MODULE_PARM(utp, "i");MODULE_PARM(io, "i");MODULE_PARM(irq, "i");MODULE_PARM(clone, "i");MODULE_PARM(de620_debug, "i");/*********************************************** *                                             * * Index to functions, as function prototypes. * *                                             * ***********************************************//* * Routines used internally. (See also "convenience macros.. below") *//* Put in the device structure. */static int	de620_open(struct net_device *);static int	de620_close(struct net_device *);static struct	net_device_stats *get_stats(struct net_device *);static void	de620_set_multicast_list(struct net_device *);static int	de620_start_xmit(struct sk_buff *, struct net_device *);/* Dispatch from interrupts. */static void	de620_interrupt(int, void *, struct pt_regs *);static int	de620_rx_intr(struct net_device *);/* Initialization */static int	adapter_init(struct net_device *);int		de620_probe(struct net_device *);static int	read_eeprom(struct net_device *);/* * D-Link driver variables: */#define SCR_DEF NIBBLEMODE |INTON | SLEEP | AUTOTX#define	TCR_DEF RXPB			/* not used: | TXSUCINT | T16INT */#define DE620_RX_START_PAGE 12		/* 12 pages (=3k) reserved for tx */#define DEF_NIC_CMD IRQEN | ICEN | DS1static volatile byte	NIC_Cmd;static volatile byte	next_rx_page;static byte		first_rx_page;static byte		last_rx_page;static byte		EIPRegister;static struct nic {	byte	NodeID[6];	byte	RAM_Size;	byte	Model;	byte	Media;	byte	SCR;} nic_data;/********************************************************** *                                                        * * Convenience macros/functions for D-Link DE-620 adapter * *                                                        * **********************************************************/#define de620_tx_buffs(dd) (inb(STATUS_PORT) & (TXBF0 | TXBF1))#define de620_flip_ds(dd) NIC_Cmd ^= DS0 | DS1; outb(NIC_Cmd, COMMAND_PORT);/* Check for ready-status, and return a nibble (high 4 bits) for data input */#ifdef COUNT_LOOPSstatic int tot_cnt;#endifstatic inline bytede620_ready(struct net_device *dev){	byte value;	register short int cnt = 0;	while ((((value = inb(STATUS_PORT)) & READY) == 0) && (cnt <= 1000))		++cnt;#ifdef COUNT_LOOPS	tot_cnt += cnt;#endif	return value & 0xf0; /* nibble */}static inline voidde620_send_command(struct net_device *dev, byte cmd){	de620_ready(dev);	if (cmd == W_DUMMY)		outb(NIC_Cmd, COMMAND_PORT);	outb(cmd, DATA_PORT);	outb(NIC_Cmd ^ CS0, COMMAND_PORT);	de620_ready(dev);	outb(NIC_Cmd, COMMAND_PORT);}static inline voidde620_put_byte(struct net_device *dev, byte value){	/* The de620_ready() makes 7 loops, on the average, on a DX2/66 */	de620_ready(dev);	outb(value, DATA_PORT);	de620_flip_ds(dev);}static inline bytede620_read_byte(struct net_device *dev){	byte value;	/* The de620_ready() makes 7 loops, on the average, on a DX2/66 */	value = de620_ready(dev); /* High nibble */	de620_flip_ds(dev);	value |= de620_ready(dev) >> 4; /* Low nibble */	return value;}static inline voidde620_write_block(struct net_device *dev, byte *buffer, int count){#ifndef LOWSPEED	byte uflip = NIC_Cmd ^ (DS0 | DS1);	byte dflip = NIC_Cmd;#else /* LOWSPEED */#ifdef COUNT_LOOPS	int bytes = count;#endif /* COUNT_LOOPS */#endif /* LOWSPEED */#ifdef LOWSPEED#ifdef COUNT_LOOPS	tot_cnt = 0;#endif /* COUNT_LOOPS */	/* No further optimization useful, the limit is in the adapter. */	for ( ; count > 0; --count, ++buffer) {		de620_put_byte(dev,*buffer);	}	de620_send_command(dev,W_DUMMY);#ifdef COUNT_LOOPS	/* trial debug output: loops per byte in de620_ready() */	printk("WRITE(%d)\n", tot_cnt/((bytes?bytes:1)));#endif /* COUNT_LOOPS */#else /* not LOWSPEED */	for ( ; count > 0; count -=2) {		outb(*buffer++, DATA_PORT);		outb(uflip, COMMAND_PORT);		outb(*buffer++, DATA_PORT);		outb(dflip, COMMAND_PORT);	}	de620_send_command(dev,W_DUMMY);#endif /* LOWSPEED */}static inline voidde620_read_block(struct net_device *dev, byte *data, int count){#ifndef LOWSPEED	byte value;	byte uflip = NIC_Cmd ^ (DS0 | DS1);	byte dflip = NIC_Cmd;#else /* LOWSPEED */#ifdef COUNT_LOOPS	int bytes = count;	tot_cnt = 0;#endif /* COUNT_LOOPS */#endif /* LOWSPEED */#ifdef LOWSPEED	/* No further optimization useful, the limit is in the adapter. */	while (count-- > 0) {		*data++ = de620_read_byte(dev);		de620_flip_ds(dev);	}#ifdef COUNT_LOOPS	/* trial debug output: loops per byte in de620_ready() */	printk("READ(%d)\n", tot_cnt/(2*(bytes?bytes:1)));#endif /* COUNT_LOOPS */#else /* not LOWSPEED */	while (count-- > 0) {		value = inb(STATUS_PORT) & 0xf0; /* High nibble */		outb(uflip, COMMAND_PORT);		*data++ = value | inb(STATUS_PORT) >> 4; /* Low nibble */		outb(dflip , COMMAND_PORT);	}#endif /* LOWSPEED */}static inline voidde620_set_delay(struct net_device *dev){	de620_ready(dev);	outb(W_DFR, DATA_PORT);	outb(NIC_Cmd ^ CS0, COMMAND_PORT);	de620_ready(dev);#ifdef LOWSPEED	outb(WRITE_DELAY, DATA_PORT);#else	outb(0, DATA_PORT);#endif	de620_flip_ds(dev);	de620_ready(dev);#ifdef LOWSPEED	outb(READ_DELAY, DATA_PORT);#else	outb(0, DATA_PORT);#endif	de620_flip_ds(dev);}static inline voidde620_set_register(struct net_device *dev, byte reg, byte value){	de620_ready(dev);	outb(reg, DATA_PORT);	outb(NIC_Cmd ^ CS0, COMMAND_PORT);	de620_put_byte(dev, value);}static inline bytede620_get_register(struct net_device *dev, byte reg){	byte value;	de620_send_command(dev,reg);	value = de620_read_byte(dev);	de620_send_command(dev,W_DUMMY);	return value;}/********************************************************************* * * Open/initialize the board. * * This routine should set everything up anew at each open, even * registers that "should" only need to be set once at boot, so that * there is a non-reboot way to recover if something goes wrong. * */static int de620_open(struct net_device *dev){	int ret = request_irq(dev->irq, de620_interrupt, 0, dev->name, dev);	if (ret) {		printk (KERN_ERR "%s: unable to get IRQ %d\n", dev->name, dev->irq);		return ret;	}	if (adapter_init(dev))		return -EIO;	netif_start_queue(dev);	return 0;}/************************************************ * * The inverse routine to de620_open(). * */static int de620_close(struct net_device *dev){	netif_stop_queue(dev);	/* disable recv */	de620_set_register(dev, W_TCR, RXOFF);	free_irq(dev->irq, dev);	return 0;}/********************************************* * * Return current statistics * */static struct net_device_stats *get_stats(struct net_device *dev){	return (struct net_device_stats *)(dev->priv);}/********************************************* * * Set or clear the multicast filter for this adaptor. * (no real multicast implemented for the DE-620, but she can be promiscuous...) * */static void de620_set_multicast_list(struct net_device *dev){	if (dev->mc_count || dev->flags&(IFF_ALLMULTI|IFF_PROMISC))	{ /* Enable promiscuous mode */		/*		 *	We must make the kernel realise we had to move		 *	into promisc mode or we start all out war on		 *	the cable. - AC		 */		dev->flags|=IFF_PROMISC;		de620_set_register(dev, W_TCR, (TCR_DEF & ~RXPBM) | RXALL);	}	else	{ /* Disable promiscuous mode, use normal mode */		de620_set_register(dev, W_TCR, TCR_DEF);	}}/******************************************************* *	 * Handle timeouts on transmit */ static void de620_timeout(struct net_device *dev){	printk("%s: transmit timed out, %s?\n",

⌨️ 快捷键说明

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