File: /usr/src/linux/drivers/net/atp.c

1     /* atp.c: Attached (pocket) ethernet adapter driver for linux. */
2     /*
3     	This is a driver for commonly OEM pocket (parallel port)
4     	ethernet adapters based on the Realtek RTL8002 and RTL8012 chips.
5     
6     	Written 1993-2000 by Donald Becker.
7     
8     	This software may be used and distributed according to the terms of
9     	the GNU General Public License (GPL), incorporated herein by reference.
10     	Drivers based on or derived from this code fall under the GPL and must
11     	retain the authorship, copyright and license notice.  This file is not
12     	a complete program and may only be used when the entire operating
13     	system is licensed under the GPL.
14     
15     	Copyright 1993 United States Government as represented by the Director,
16     	National Security Agency.  Copyright 1994-2000 retained by the original
17     	author, Donald Becker. The timer-based reset code was supplied in 1995
18     	by Bill Carlson, wwc@super.org.
19     
20     	The author may be reached as becker@scyld.com, or C/O
21     	Scyld Computing Corporation
22     	410 Severn Ave., Suite 210
23     	Annapolis MD 21403
24     
25     	Support information and updates available at
26     	http://www.scyld.com/network/atp.html
27     
28     
29     	Modular support/softnet added by Alan Cox.
30     
31     */
32     
33     static const char versionA[] =
34     "atp.c:v1.09 8/9/2000 Donald Becker <becker@scyld.com>\n";
35     static const char versionB[] =
36     "  http://www.scyld.com/network/atp.html\n";
37     
38     /* The user-configurable values.
39        These may be modified when a driver module is loaded.*/
40     
41     static int debug = 1; 			/* 1 normal messages, 0 quiet .. 7 verbose. */
42     #define net_debug debug
43     
44     /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
45     static int max_interrupt_work = 15;
46     
47     #define NUM_UNITS 2
48     /* The standard set of ISA module parameters. */
49     static int io[NUM_UNITS];
50     static int irq[NUM_UNITS];
51     static int xcvr[NUM_UNITS]; 			/* The data transfer mode. */
52     
53     /* Operational parameters that are set at compile time. */
54     
55     /* Time in jiffies before concluding the transmitter is hung. */
56     #define TX_TIMEOUT  (400*HZ/1000)
57     
58     /*
59     	This file is a device driver for the RealTek (aka AT-Lan-Tec) pocket
60     	ethernet adapter.  This is a common low-cost OEM pocket ethernet
61     	adapter, sold under many names.
62     
63       Sources:
64     	This driver was written from the packet driver assembly code provided by
65     	Vincent Bono of AT-Lan-Tec.	 Ever try to figure out how a complicated
66     	device works just from the assembly code?  It ain't pretty.  The following
67     	description is written based on guesses and writing lots of special-purpose
68     	code to test my theorized operation.
69     
70     	In 1997 Realtek made available the documentation for the second generation
71     	RTL8012 chip, which has lead to several driver improvements.
72     	  http://www.realtek.com.tw/cn/cn.html
73     
74     					Theory of Operation
75     
76     	The RTL8002 adapter seems to be built around a custom spin of the SEEQ
77     	controller core.  It probably has a 16K or 64K internal packet buffer, of
78     	which the first 4K is devoted to transmit and the rest to receive.
79     	The controller maintains the queue of received packet and the packet buffer
80     	access pointer internally, with only 'reset to beginning' and 'skip to next
81     	packet' commands visible.  The transmit packet queue holds two (or more?)
82     	packets: both 'retransmit this packet' (due to collision) and 'transmit next
83     	packet' commands must be started by hand.
84     
85     	The station address is stored in a standard bit-serial EEPROM which must be
86     	read (ughh) by the device driver.  (Provisions have been made for
87     	substituting a 74S288 PROM, but I haven't gotten reports of any models
88     	using it.)  Unlike built-in devices, a pocket adapter can temporarily lose
89     	power without indication to the device driver.  The major effect is that
90     	the station address, receive filter (promiscuous, etc.) and transceiver
91     	must be reset.
92     
93     	The controller itself has 16 registers, some of which use only the lower
94     	bits.  The registers are read and written 4 bits at a time.  The four bit
95     	register address is presented on the data lines along with a few additional
96     	timing and control bits.  The data is then read from status port or written
97     	to the data port.
98     
99     	Correction: the controller has two banks of 16 registers.  The second
100     	bank contains only the multicast filter table (now used) and the EEPROM
101     	access registers.
102     
103     	Since the bulk data transfer of the actual packets through the slow
104     	parallel port dominates the driver's running time, four distinct data
105     	(non-register) transfer modes are provided by the adapter, two in each
106     	direction.  In the first mode timing for the nibble transfers is
107     	provided through the data port.  In the second mode the same timing is
108     	provided through the control port.  In either case the data is read from
109     	the status port and written to the data port, just as it is accessing
110     	registers.
111     
112     	In addition to the basic data transfer methods, several more are modes are
113     	created by adding some delay by doing multiple reads of the data to allow
114     	it to stabilize.  This delay seems to be needed on most machines.
115     
116     	The data transfer mode is stored in the 'dev->if_port' field.  Its default
117     	value is '4'.  It may be overridden at boot-time using the third parameter
118     	to the "ether=..." initialization.
119     
120     	The header file <atp.h> provides inline functions that encapsulate the
121     	register and data access methods.  These functions are hand-tuned to
122     	generate reasonable object code.  This header file also documents my
123     	interpretations of the device registers.
124     */
125     
126     #include <linux/kernel.h>
127     #include <linux/module.h>
128     #include <linux/sched.h>
129     #include <linux/types.h>
130     #include <linux/fcntl.h>
131     #include <linux/interrupt.h>
132     #include <linux/ptrace.h>
133     #include <linux/ioport.h>
134     #include <linux/in.h>
135     #include <linux/slab.h>
136     #include <linux/string.h>
137     #include <asm/system.h>
138     #include <asm/bitops.h>
139     #include <asm/io.h>
140     #include <asm/dma.h>
141     #include <linux/errno.h>
142     #include <linux/init.h>
143     
144     #include <linux/netdevice.h>
145     #include <linux/etherdevice.h>
146     #include <linux/skbuff.h>
147     #include <linux/spinlock.h>
148     #include <linux/delay.h>
149     
150     #include "atp.h"
151     
152     MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
153     MODULE_DESCRIPTION("RealTek RTL8002/8012 parallel port Ethernet driver");
154     MODULE_PARM(max_interrupt_work, "i");
155     MODULE_PARM(debug, "i");
156     MODULE_PARM(io, "1-" __MODULE_STRING(NUM_UNITS) "i");
157     MODULE_PARM(irq, "1-" __MODULE_STRING(NUM_UNITS) "i");
158     MODULE_PARM(xcvr, "1-" __MODULE_STRING(NUM_UNITS) "i");
159     MODULE_PARM_DESC(max_interrupt_work, "ATP maximum events handled per interrupt");
160     MODULE_PARM_DESC(debug, "ATP debug level (0-7)");
161     MODULE_PARM_DESC(io, "ATP I/O base address(es)");
162     MODULE_PARM_DESC(irq, "ATP IRQ number(s)");
163     MODULE_PARM_DESC(xcvr, "ATP tranceiver(s) (0=internal, 1=external)");
164     
165     #define RUN_AT(x) (jiffies + (x))
166     
167     /* The number of low I/O ports used by the ethercard. */
168     #define ETHERCARD_TOTAL_SIZE	3
169     
170     /* Sequence to switch an 8012 from printer mux to ethernet mode. */
171     static char mux_8012[] = { 0xff, 0xf7, 0xff, 0xfb, 0xf3, 0xfb, 0xff, 0xf7,};
172     
173     struct net_local {
174         spinlock_t lock;
175         struct net_device *next_module;
176         struct net_device_stats stats;
177         struct timer_list timer;	/* Media selection timer. */
178         long last_rx_time;		/* Last Rx, in jiffies, to handle Rx hang. */
179         int saved_tx_size;
180         unsigned int tx_unit_busy:1;
181         unsigned char re_tx,	/* Number of packet retransmissions. */
182     		addr_mode,		/* Current Rx filter e.g. promiscuous, etc. */
183     		pac_cnt_in_tx_buf,
184     		chip_type;
185     };
186     
187     /* This code, written by wwc@super.org, resets the adapter every
188        TIMED_CHECKER ticks.  This recovers from an unknown error which
189        hangs the device. */
190     #define TIMED_CHECKER (HZ/4)
191     #ifdef TIMED_CHECKER
192     #include <linux/timer.h>
193     static void atp_timed_checker(unsigned long ignored);
194     #endif
195     
196     /* Index to functions, as function prototypes. */
197     
198     static int atp_probe1(struct net_device *dev, long ioaddr);
199     static void get_node_ID(struct net_device *dev);
200     static unsigned short eeprom_op(long ioaddr, unsigned int cmd);
201     static int net_open(struct net_device *dev);
202     static void hardware_init(struct net_device *dev);
203     static void write_packet(long ioaddr, int length, unsigned char *packet, int mode);
204     static void trigger_send(long ioaddr, int length);
205     static int	atp_send_packet(struct sk_buff *skb, struct net_device *dev);
206     static void atp_interrupt(int irq, void *dev_id, struct pt_regs *regs);
207     static void net_rx(struct net_device *dev);
208     static void read_block(long ioaddr, int length, unsigned char *buffer, int data_mode);
209     static int net_close(struct net_device *dev);
210     static struct net_device_stats *net_get_stats(struct net_device *dev);
211     static void set_rx_mode_8002(struct net_device *dev);
212     static void set_rx_mode_8012(struct net_device *dev);
213     static void tx_timeout(struct net_device *dev);
214     
215     
216     /* A list of all installed ATP devices, for removing the driver module. */
217     static struct net_device *root_atp_dev;
218     
219     /* Check for a network adapter of this type, and return '0' iff one exists.
220        If dev->base_addr == 0, probe all likely locations.
221        If dev->base_addr == 1, always return failure.
222        If dev->base_addr == 2, allocate space for the device and return success
223        (detachable devices only).
224        */
225     static int __init atp_init(struct net_device *dev)
226     {
227     	int *port, ports[] = {0x378, 0x278, 0x3bc, 0};
228     	int base_addr = dev ? dev->base_addr : io[0];
229     
230     	if (base_addr > 0x1ff)		/* Check a single specified location. */
231     		return atp_probe1(dev, base_addr);
232     	else if (base_addr == 1)	/* Don't probe at all. */
233     		return -ENXIO;
234     
235     	for (port = ports; *port; port++) {
236     		long ioaddr = *port;
237     		outb(0x57, ioaddr + PAR_DATA);
238     		if (inb(ioaddr + PAR_DATA) != 0x57)
239     			continue;
240     		if (atp_probe1(dev, ioaddr) == 0)
241     			return 0;
242     	}
243     
244     	return -ENODEV;
245     }
246     
247     static int __init atp_probe1(struct net_device *dev, long ioaddr)
248     {
249     	struct net_local *lp;
250     	int saved_ctrl_reg, status, i;
251     
252     	outb(0xff, ioaddr + PAR_DATA);
253     	/* Save the original value of the Control register, in case we guessed
254     	   wrong. */
255     	saved_ctrl_reg = inb(ioaddr + PAR_CONTROL);
256     	if (net_debug > 3)
257     		printk("atp: Control register was %#2.2x.\n", saved_ctrl_reg);
258     	/* IRQEN=0, SLCTB=high INITB=high, AUTOFDB=high, STBB=high. */
259     	outb(0x04, ioaddr + PAR_CONTROL);
260     #ifndef final_version
261     	if (net_debug > 3) {
262     		/* Turn off the printer multiplexer on the 8012. */
263     		for (i = 0; i < 8; i++)
264     			outb(mux_8012[i], ioaddr + PAR_DATA);
265     		write_reg(ioaddr, MODSEL, 0x00);
266     		printk("atp: Registers are ");
267     		for (i = 0; i < 32; i++)
268     			printk(" %2.2x", read_nibble(ioaddr, i));
269     		printk(".\n");
270     	}
271     #endif
272     	/* Turn off the printer multiplexer on the 8012. */
273     	for (i = 0; i < 8; i++)
274     		outb(mux_8012[i], ioaddr + PAR_DATA);
275     	write_reg_high(ioaddr, CMR1, CMR1h_RESET);
276     	/* udelay() here? */
277     	status = read_nibble(ioaddr, CMR1);
278     
279     	if (net_debug > 3) {
280     		printk(KERN_DEBUG "atp: Status nibble was %#2.2x..", status);
281     		for (i = 0; i < 32; i++)
282     			printk(" %2.2x", read_nibble(ioaddr, i));
283     		printk("\n");
284     	}
285     
286     	if ((status & 0x78) != 0x08) {
287     		/* The pocket adapter probe failed, restore the control register. */
288     		outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
289     		return -ENODEV;
290     	}
291     	status = read_nibble(ioaddr, CMR2_h);
292     	if ((status & 0x78) != 0x10) {
293     		outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
294     		return -ENODEV;
295     	}
296     
297     	dev = init_etherdev(dev, sizeof(struct net_local));
298     	if (!dev)
299     		return -ENOMEM;
300     	SET_MODULE_OWNER(dev);
301     
302     	/* Find the IRQ used by triggering an interrupt. */
303     	write_reg_byte(ioaddr, CMR2, 0x01);			/* No accept mode, IRQ out. */
304     	write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);	/* Enable Tx and Rx. */
305     
306     	/* Omit autoIRQ routine for now. Use "table lookup" instead.  Uhgggh. */
307     	if (irq[0])
308     		dev->irq = irq[0];
309     	else if (ioaddr == 0x378)
310     		dev->irq = 7;
311     	else
312     		dev->irq = 5;
313     	write_reg_high(ioaddr, CMR1, CMR1h_TxRxOFF); /* Disable Tx and Rx units. */
314     	write_reg(ioaddr, CMR2, CMR2_NULL);
315     
316     	dev->base_addr = ioaddr;
317     
318     	/* Read the station address PROM.  */
319     	get_node_ID(dev);
320     
321     #ifndef MODULE
322     	if (net_debug)
323     		printk(KERN_INFO "%s" KERN_INFO "%s", versionA, versionB);
324     #endif
325     
326     	printk(KERN_NOTICE "%s: Pocket adapter found at %#3lx, IRQ %d, SAPROM "
327     		   "%02X:%02X:%02X:%02X:%02X:%02X.\n", dev->name, dev->base_addr,
328     		   dev->irq, dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
329     		   dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
330     
331     	/* Reset the ethernet hardware and activate the printer pass-through. */
332         write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX);
333     
334     	/* Initialize the device structure. */
335     	ether_setup(dev);
336     	if (dev->priv == NULL)
337     		dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
338     	if (dev->priv == NULL)
339     		return -ENOMEM;
340     	memset(dev->priv, 0, sizeof(struct net_local));
341     
342     	lp = (struct net_local *)dev->priv;
343     	lp->chip_type = RTL8002;
344     	lp->addr_mode = CMR2h_Normal;
345     	spin_lock_init(&lp->lock);
346     
347     	lp->next_module = root_atp_dev;
348     	root_atp_dev = dev;
349     
350     	/* For the ATP adapter the "if_port" is really the data transfer mode. */
351     	if (xcvr[0])
352     		dev->if_port = xcvr[0];
353     	else
354     		dev->if_port = (dev->mem_start & 0xf) ? (dev->mem_start & 0x7) : 4;
355     	if (dev->mem_end & 0xf)
356     		net_debug = dev->mem_end & 7;
357     
358     	dev->open		= net_open;
359     	dev->stop		= net_close;
360     	dev->hard_start_xmit	= atp_send_packet;
361     	dev->get_stats		= net_get_stats;
362     	dev->set_multicast_list =
363     	  lp->chip_type == RTL8002 ? &set_rx_mode_8002 : &set_rx_mode_8012;
364     	dev->tx_timeout		= tx_timeout;
365     	dev->watchdog_timeo	= TX_TIMEOUT;
366     
367     	return 0;
368     }
369     
370     /* Read the station address PROM, usually a word-wide EEPROM. */
371     static void __init get_node_ID(struct net_device *dev)
372     {
373     	long ioaddr = dev->base_addr;
374     	int sa_offset = 0;
375     	int i;
376     
377     	write_reg(ioaddr, CMR2, CMR2_EEPROM);	  /* Point to the EEPROM control registers. */
378     
379     	/* Some adapters have the station address at offset 15 instead of offset
380     	   zero.  Check for it, and fix it if needed. */
381     	if (eeprom_op(ioaddr, EE_READ(0)) == 0xffff)
382     		sa_offset = 15;
383     
384     	for (i = 0; i < 3; i++)
385     		((u16 *)dev->dev_addr)[i] =
386     			be16_to_cpu(eeprom_op(ioaddr, EE_READ(sa_offset + i)));
387     
388     	write_reg(ioaddr, CMR2, CMR2_NULL);
389     }
390     
391     /*
392       An EEPROM read command starts by shifting out 0x60+address, and then
393       shifting in the serial data. See the NatSemi databook for details.
394      *		   ________________
395      * CS : __|
396      *			   ___	   ___
397      * CLK: ______|	  |___|	  |
398      *		 __ _______ _______
399      * DI :	 __X_______X_______X
400      * DO :	 _________X_______X
401      */
402     
403     static unsigned short __init eeprom_op(long ioaddr, unsigned int cmd)
404     {
405     	unsigned eedata_out = 0;
406     	int num_bits = EE_CMD_SIZE;
407     
408     	while (--num_bits >= 0) {
409     		char outval = test_bit(num_bits, &cmd) ? EE_DATA_WRITE : 0;
410     		write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_LOW);
411     		write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_HIGH);
412     		eedata_out <<= 1;
413     		if (read_nibble(ioaddr, PROM_DATA) & EE_DATA_READ)
414     			eedata_out++;
415     	}
416     	write_reg_high(ioaddr, PROM_CMD, EE_CLK_LOW & ~EE_CS);
417     	return eedata_out;
418     }
419     
420     
421     /* Open/initialize the board.  This is called (in the current kernel)
422        sometime after booting when the 'ifconfig' program is run.
423     
424        This routine sets everything up anew at each open, even
425        registers that "should" only need to be set once at boot, so that
426        there is non-reboot way to recover if something goes wrong.
427     
428        This is an attachable device: if there is no dev->priv entry then it wasn't
429        probed for at boot-time, and we need to probe for it again.
430        */
431     static int net_open(struct net_device *dev)
432     {
433     	struct net_local *lp = (struct net_local *)dev->priv;
434     	int ret;
435     
436     	/* The interrupt line is turned off (tri-stated) when the device isn't in
437     	   use.  That's especially important for "attached" interfaces where the
438     	   port or interrupt may be shared. */
439     	ret = request_irq(dev->irq, &atp_interrupt, 0, dev->name, dev);
440     	if (ret)
441     		return ret;
442     
443     	hardware_init(dev);
444     
445     	init_timer(&lp->timer);
446     	lp->timer.expires = RUN_AT(TIMED_CHECKER);
447     	lp->timer.data = (unsigned long)dev;
448     	lp->timer.function = &atp_timed_checker;    /* timer handler */
449     	add_timer(&lp->timer);
450     
451     	netif_start_queue(dev);
452     	return 0;
453     }
454     
455     /* This routine resets the hardware.  We initialize everything, assuming that
456        the hardware may have been temporarily detached. */
457     static void hardware_init(struct net_device *dev)
458     {
459     	struct net_local *lp = (struct net_local *)dev->priv;
460     	long ioaddr = dev->base_addr;
461         int i;
462     
463     	/* Turn off the printer multiplexer on the 8012. */
464     	for (i = 0; i < 8; i++)
465     		outb(mux_8012[i], ioaddr + PAR_DATA);
466     	write_reg_high(ioaddr, CMR1, CMR1h_RESET);
467     
468         for (i = 0; i < 6; i++)
469     		write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
470     
471     	write_reg_high(ioaddr, CMR2, lp->addr_mode);
472     
473     	if (net_debug > 2) {
474     		printk(KERN_DEBUG "%s: Reset: current Rx mode %d.\n", dev->name,
475     			   (read_nibble(ioaddr, CMR2_h) >> 3) & 0x0f);
476     	}
477     
478         write_reg(ioaddr, CMR2, CMR2_IRQOUT);
479         write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
480     
481     	/* Enable the interrupt line from the serial port. */
482     	outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
483     
484     	/* Unmask the interesting interrupts. */
485         write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
486         write_reg_high(ioaddr, IMR, ISRh_RxErr);
487     
488     	lp->tx_unit_busy = 0;
489         lp->pac_cnt_in_tx_buf = 0;
490     	lp->saved_tx_size = 0;
491     }
492     
493     static void trigger_send(long ioaddr, int length)
494     {
495     	write_reg_byte(ioaddr, TxCNT0, length & 0xff);
496     	write_reg(ioaddr, TxCNT1, length >> 8);
497     	write_reg(ioaddr, CMR1, CMR1_Xmit);
498     }
499     
500     static void write_packet(long ioaddr, int length, unsigned char *packet, int data_mode)
501     {
502         length = (length + 1) & ~1;		/* Round up to word length. */
503         outb(EOC+MAR, ioaddr + PAR_DATA);
504         if ((data_mode & 1) == 0) {
505     		/* Write the packet out, starting with the write addr. */
506     		outb(WrAddr+MAR, ioaddr + PAR_DATA);
507     		do {
508     			write_byte_mode0(ioaddr, *packet++);
509     		} while (--length > 0) ;
510         } else {
511     		/* Write the packet out in slow mode. */
512     		unsigned char outbyte = *packet++;
513     
514     		outb(Ctrl_LNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
515     		outb(WrAddr+MAR, ioaddr + PAR_DATA);
516     
517     		outb((outbyte & 0x0f)|0x40, ioaddr + PAR_DATA);
518     		outb(outbyte & 0x0f, ioaddr + PAR_DATA);
519     		outbyte >>= 4;
520     		outb(outbyte & 0x0f, ioaddr + PAR_DATA);
521     		outb(Ctrl_HNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
522     		while (--length > 0)
523     			write_byte_mode1(ioaddr, *packet++);
524         }
525         /* Terminate the Tx frame.  End of write: ECB. */
526         outb(0xff, ioaddr + PAR_DATA);
527         outb(Ctrl_HNibWrite | Ctrl_SelData | Ctrl_IRQEN, ioaddr + PAR_CONTROL);
528     }
529     
530     static void tx_timeout(struct net_device *dev)
531     {
532     	struct net_local *np = (struct net_local *)dev->priv;
533     	long ioaddr = dev->base_addr;
534     
535     	printk(KERN_WARNING "%s: Transmit timed out, %s?\n", dev->name,
536     		   inb(ioaddr + PAR_CONTROL) & 0x10 ? "network cable problem"
537     		   :  "IRQ conflict");
538     	np->stats.tx_errors++;
539     	/* Try to restart the adapter. */
540     	hardware_init(dev);
541     	dev->trans_start = jiffies;
542     	netif_wake_queue(dev);
543     	np->stats.tx_errors++;
544     }
545     
546     static int atp_send_packet(struct sk_buff *skb, struct net_device *dev)
547     {
548     	struct net_local *lp = (struct net_local *)dev->priv;
549     	long ioaddr = dev->base_addr;
550     	int length;
551     	long flags;
552     
553     	length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
554     
555     	netif_stop_queue(dev);
556     
557     	/* Disable interrupts by writing 0x00 to the Interrupt Mask Register.
558     	   This sequence must not be interrupted by an incoming packet. */
559     
560     	spin_lock_irqsave(&lp->lock, flags);
561     	write_reg(ioaddr, IMR, 0);
562     	write_reg_high(ioaddr, IMR, 0);
563     	spin_unlock_irqrestore(&lp->lock, flags);
564     
565     	write_packet(ioaddr, length, skb->data, dev->if_port);
566     
567     	lp->pac_cnt_in_tx_buf++;
568     	if (lp->tx_unit_busy == 0) {
569     		trigger_send(ioaddr, length);
570     		lp->saved_tx_size = 0; 				/* Redundant */
571     		lp->re_tx = 0;
572     		lp->tx_unit_busy = 1;
573     	} else
574     		lp->saved_tx_size = length;
575     	/* Re-enable the LPT interrupts. */
576     	write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
577     	write_reg_high(ioaddr, IMR, ISRh_RxErr);
578     
579     	dev->trans_start = jiffies;
580     	dev_kfree_skb (skb);
581     	return 0;
582     }
583     
584     
585     /* The typical workload of the driver:
586        Handle the network interface interrupts. */
587     static void atp_interrupt(int irq, void *dev_instance, struct pt_regs * regs)
588     {
589     	struct net_device *dev = (struct net_device *)dev_instance;
590     	struct net_local *lp;
591     	long ioaddr;
592     	static int num_tx_since_rx;
593     	int boguscount = max_interrupt_work;
594     
595     	if (dev == NULL) {
596     		printk(KERN_ERR "ATP_interrupt(): irq %d for unknown device.\n", irq);
597     		return;
598     	}
599     	ioaddr = dev->base_addr;
600     	lp = (struct net_local *)dev->priv;
601     
602     	spin_lock(&lp->lock);
603     
604     	/* Disable additional spurious interrupts. */
605     	outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
606     
607     	/* The adapter's output is currently the IRQ line, switch it to data. */
608     	write_reg(ioaddr, CMR2, CMR2_NULL);
609     	write_reg(ioaddr, IMR, 0);
610     
611     	if (net_debug > 5) printk(KERN_DEBUG "%s: In interrupt ", dev->name);
612         while (--boguscount > 0) {
613     		int status = read_nibble(ioaddr, ISR);
614     		if (net_debug > 5) printk("loop status %02x..", status);
615     
616     		if (status & (ISR_RxOK<<3)) {
617     			write_reg(ioaddr, ISR, ISR_RxOK); /* Clear the Rx interrupt. */
618     			do {
619     				int read_status = read_nibble(ioaddr, CMR1);
620     				if (net_debug > 6)
621     					printk("handling Rx packet %02x..", read_status);
622     				/* We acknowledged the normal Rx interrupt, so if the interrupt
623     				   is still outstanding we must have a Rx error. */
624     				if (read_status & (CMR1_IRQ << 3)) { /* Overrun. */
625     					lp->stats.rx_over_errors++;
626     					/* Set to no-accept mode long enough to remove a packet. */
627     					write_reg_high(ioaddr, CMR2, CMR2h_OFF);
628     					net_rx(dev);
629     					/* Clear the interrupt and return to normal Rx mode. */
630     					write_reg_high(ioaddr, ISR, ISRh_RxErr);
631     					write_reg_high(ioaddr, CMR2, lp->addr_mode);
632     				} else if ((read_status & (CMR1_BufEnb << 3)) == 0) {
633     					net_rx(dev);
634     					num_tx_since_rx = 0;
635     				} else
636     					break;
637     			} while (--boguscount > 0);
638     		} else if (status & ((ISR_TxErr + ISR_TxOK)<<3)) {
639     			if (net_debug > 6)  printk("handling Tx done..");
640     			/* Clear the Tx interrupt.  We should check for too many failures
641     			   and reinitialize the adapter. */
642     			write_reg(ioaddr, ISR, ISR_TxErr + ISR_TxOK);
643     			if (status & (ISR_TxErr<<3)) {
644     				lp->stats.collisions++;
645     				if (++lp->re_tx > 15) {
646     					lp->stats.tx_aborted_errors++;
647     					hardware_init(dev);
648     					break;
649     				}
650     				/* Attempt to retransmit. */
651     				if (net_debug > 6)  printk("attempting to ReTx");
652     				write_reg(ioaddr, CMR1, CMR1_ReXmit + CMR1_Xmit);
653     			} else {
654     				/* Finish up the transmit. */
655     				lp->stats.tx_packets++;
656     				lp->pac_cnt_in_tx_buf--;
657     				if ( lp->saved_tx_size) {
658     					trigger_send(ioaddr, lp->saved_tx_size);
659     					lp->saved_tx_size = 0;
660     					lp->re_tx = 0;
661     				} else
662     					lp->tx_unit_busy = 0;
663     				netif_wake_queue(dev);	/* Inform upper layers. */
664     			}
665     			num_tx_since_rx++;
666     		} else if (num_tx_since_rx > 8
667     				   && jiffies > dev->last_rx + HZ) {
668     			if (net_debug > 2)
669     				printk(KERN_DEBUG "%s: Missed packet? No Rx after %d Tx and "
670     					   "%ld jiffies status %02x  CMR1 %02x.\n", dev->name,
671     					   num_tx_since_rx, jiffies - dev->last_rx, status,
672     					   (read_nibble(ioaddr, CMR1) >> 3) & 15);
673     			lp->stats.rx_missed_errors++;
674     			hardware_init(dev);
675     			num_tx_since_rx = 0;
676     			break;
677     		} else
678     			break;
679         }
680     
681     	/* This following code fixes a rare (and very difficult to track down)
682     	   problem where the adapter forgets its ethernet address. */
683     	{
684     		int i;
685     		for (i = 0; i < 6; i++)
686     			write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
687     #if 0 && defined(TIMED_CHECKER)
688     		mod_timer(&lp->timer, RUN_AT(TIMED_CHECKER));
689     #endif
690     	}
691     
692     	/* Tell the adapter that it can go back to using the output line as IRQ. */
693         write_reg(ioaddr, CMR2, CMR2_IRQOUT);
694     	/* Enable the physical interrupt line, which is sure to be low until.. */
695     	outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
696     	/* .. we enable the interrupt sources. */
697     	write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
698     	write_reg_high(ioaddr, IMR, ISRh_RxErr); 			/* Hmmm, really needed? */
699     
700     	spin_unlock(&lp->lock);
701     
702     	if (net_debug > 5) printk("exiting interrupt.\n");
703     	return;
704     }
705     
706     #ifdef TIMED_CHECKER
707     /* This following code fixes a rare (and very difficult to track down)
708        problem where the adapter forgets its ethernet address. */
709     static void atp_timed_checker(unsigned long data)
710     {
711     	struct net_device *dev = (struct net_device *)data;
712     	long ioaddr = dev->base_addr;
713     	struct net_local *lp = (struct net_local *)dev->priv;
714     	int tickssofar = jiffies - lp->last_rx_time;
715     	int i;
716     
717     	spin_lock(&lp->lock);
718     	if (tickssofar > 2*HZ) {
719     #if 1
720     		for (i = 0; i < 6; i++)
721     			write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
722     		lp->last_rx_time = jiffies;
723     #else
724     		for (i = 0; i < 6; i++)
725     			if (read_cmd_byte(ioaddr, PAR0 + i) != atp_timed_dev->dev_addr[i])
726     				{
727     			struct net_local *lp = (struct net_local *)atp_timed_dev->priv;
728     			write_reg_byte(ioaddr, PAR0 + i, atp_timed_dev->dev_addr[i]);
729     			if (i == 2)
730     			  lp->stats.tx_errors++;
731     			else if (i == 3)
732     			  lp->stats.tx_dropped++;
733     			else if (i == 4)
734     			  lp->stats.collisions++;
735     			else
736     			  lp->stats.rx_errors++;
737     		  }
738     #endif
739     	}
740     	spin_unlock(&lp->lock);
741     	lp->timer.expires = RUN_AT(TIMED_CHECKER);
742     	add_timer(&lp->timer);
743     }
744     #endif
745     
746     /* We have a good packet(s), get it/them out of the buffers. */
747     static void net_rx(struct net_device *dev)
748     {
749     	struct net_local *lp = (struct net_local *)dev->priv;
750     	long ioaddr = dev->base_addr;
751     	struct rx_header rx_head;
752     
753     	/* Process the received packet. */
754     	outb(EOC+MAR, ioaddr + PAR_DATA);
755     	read_block(ioaddr, 8, (unsigned char*)&rx_head, dev->if_port);
756     	if (net_debug > 5)
757     		printk(KERN_DEBUG " rx_count %04x %04x %04x %04x..", rx_head.pad,
758     			   rx_head.rx_count, rx_head.rx_status, rx_head.cur_addr);
759     	if ((rx_head.rx_status & 0x77) != 0x01) {
760     		lp->stats.rx_errors++;
761     		if (rx_head.rx_status & 0x0004) lp->stats.rx_frame_errors++;
762     		else if (rx_head.rx_status & 0x0002) lp->stats.rx_crc_errors++;
763     		if (net_debug > 3)
764     			printk(KERN_DEBUG "%s: Unknown ATP Rx error %04x.\n",
765     				   dev->name, rx_head.rx_status);
766     		if  (rx_head.rx_status & 0x0020) {
767     			lp->stats.rx_fifo_errors++;
768     			write_reg_high(ioaddr, CMR1, CMR1h_TxENABLE);
769     			write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
770     		} else if (rx_head.rx_status & 0x0050)
771     			hardware_init(dev);
772     		return;
773     	} else {
774     		/* Malloc up new buffer. The "-4" omits the FCS (CRC). */
775     		int pkt_len = (rx_head.rx_count & 0x7ff) - 4;
776     		struct sk_buff *skb;
777     
778     		skb = dev_alloc_skb(pkt_len + 2);
779     		if (skb == NULL) {
780     			printk(KERN_ERR "%s: Memory squeeze, dropping packet.\n",
781     				   dev->name);
782     			lp->stats.rx_dropped++;
783     			goto done;
784     		}
785     		skb->dev = dev;
786     
787     		skb_reserve(skb, 2);	/* Align IP on 16 byte boundaries */
788     		read_block(ioaddr, pkt_len, skb_put(skb,pkt_len), dev->if_port);
789     		skb->protocol = eth_type_trans(skb, dev);
790     		netif_rx(skb);
791     		dev->last_rx = jiffies;
792     		lp->stats.rx_packets++;
793     		lp->stats.rx_bytes += pkt_len;
794     	}
795      done:
796     	write_reg(ioaddr, CMR1, CMR1_NextPkt);
797     	lp->last_rx_time = jiffies;
798     	return;
799     }
800     
801     static void read_block(long ioaddr, int length, unsigned char *p, int data_mode)
802     {
803     
804     	if (data_mode <= 3) { /* Mode 0 or 1 */
805     		outb(Ctrl_LNibRead, ioaddr + PAR_CONTROL);
806     		outb(length == 8  ?  RdAddr | HNib | MAR  :  RdAddr | MAR,
807     			 ioaddr + PAR_DATA);
808     		if (data_mode <= 1) { /* Mode 0 or 1 */
809     			do  *p++ = read_byte_mode0(ioaddr);  while (--length > 0);
810     		} else	/* Mode 2 or 3 */
811     			do  *p++ = read_byte_mode2(ioaddr);  while (--length > 0);
812     	} else if (data_mode <= 5)
813     		do      *p++ = read_byte_mode4(ioaddr);  while (--length > 0);
814     	else
815     		do      *p++ = read_byte_mode6(ioaddr);  while (--length > 0);
816     
817         outb(EOC+HNib+MAR, ioaddr + PAR_DATA);
818     	outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
819     }
820     
821     /* The inverse routine to net_open(). */
822     static int
823     net_close(struct net_device *dev)
824     {
825     	struct net_local *lp = (struct net_local *)dev->priv;
826     	long ioaddr = dev->base_addr;
827     
828     	netif_stop_queue(dev);
829     
830     	del_timer_sync(&lp->timer);
831     
832     	/* Flush the Tx and disable Rx here. */
833     	lp->addr_mode = CMR2h_OFF;
834     	write_reg_high(ioaddr, CMR2, CMR2h_OFF);
835     
836     	/* Free the IRQ line. */
837     	outb(0x00, ioaddr + PAR_CONTROL);
838     	free_irq(dev->irq, dev);
839     
840     	/* Reset the ethernet hardware and activate the printer pass-through. */
841     	write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX);
842     	return 0;
843     }
844     
845     /* Get the current statistics.	This may be called with the card open or
846        closed. */
847     static struct net_device_stats *
848     net_get_stats(struct net_device *dev)
849     {
850     	struct net_local *lp = (struct net_local *)dev->priv;
851     	return &lp->stats;
852     }
853     
854     /*
855      *	Set or clear the multicast filter for this adapter.
856      */
857     
858     /* The little-endian AUTODIN32 ethernet CRC calculation.
859        This is common code and should be moved to net/core/crc.c */
860     static unsigned const ethernet_polynomial_le = 0xedb88320U;
861     static inline unsigned ether_crc_le(int length, unsigned char *data)
862     {
863         unsigned int crc = 0xffffffff;	/* Initial value. */
864         while(--length >= 0) {
865     		unsigned char current_octet = *data++;
866     		int bit;
867     		for (bit = 8; --bit >= 0; current_octet >>= 1) {
868     			if ((crc ^ current_octet) & 1) {
869     				crc >>= 1;
870     				crc ^= ethernet_polynomial_le;
871     			} else
872     				crc >>= 1;
873     		}
874         }
875         return crc;
876     }
877     
878     static void set_rx_mode_8002(struct net_device *dev)
879     {
880     	struct net_local *lp = (struct net_local *)dev->priv;
881     	long ioaddr = dev->base_addr;
882     
883     	if ( dev->mc_count > 0 || (dev->flags & (IFF_ALLMULTI|IFF_PROMISC))) {
884     		/* We must make the kernel realise we had to move
885     		 *	into promisc mode or we start all out war on
886     		 *	the cable. - AC
887     		 */
888     		dev->flags|=IFF_PROMISC;
889     		lp->addr_mode = CMR2h_PROMISC;
890     	} else
891     		lp->addr_mode = CMR2h_Normal;
892     	write_reg_high(ioaddr, CMR2, lp->addr_mode);
893     }
894     
895     static void set_rx_mode_8012(struct net_device *dev)
896     {
897     	struct net_local *lp = (struct net_local *)dev->priv;
898     	long ioaddr = dev->base_addr;
899     	unsigned char new_mode, mc_filter[8]; /* Multicast hash filter */
900     	int i;
901     
902     	if (dev->flags & IFF_PROMISC) {			/* Set promiscuous. */
903     		new_mode = CMR2h_PROMISC;
904     	} else if ((dev->mc_count > 1000)  ||  (dev->flags & IFF_ALLMULTI)) {
905     		/* Too many to filter perfectly -- accept all multicasts. */
906     		memset(mc_filter, 0xff, sizeof(mc_filter));
907     		new_mode = CMR2h_Normal;
908     	} else {
909     		struct dev_mc_list *mclist;
910     
911     		memset(mc_filter, 0, sizeof(mc_filter));
912     		for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
913     			 i++, mclist = mclist->next)
914     			set_bit(ether_crc_le(ETH_ALEN, mclist->dmi_addr) & 0x3f,
915     					mc_filter);
916     		new_mode = CMR2h_Normal;
917     	}
918     	lp->addr_mode = new_mode;
919         write_reg(ioaddr, CMR2, CMR2_IRQOUT | 0x04); /* Switch to page 1. */
920         for (i = 0; i < 8; i++)
921     		write_reg_byte(ioaddr, i, mc_filter[i]);
922     	if (net_debug > 2 || 1) {
923     		lp->addr_mode = 1;
924     		printk(KERN_DEBUG "%s: Mode %d, setting multicast filter to",
925     			   dev->name, lp->addr_mode);
926     		for (i = 0; i < 8; i++)
927     			printk(" %2.2x", mc_filter[i]);
928     		printk(".\n");
929     	}
930     
931     	write_reg_high(ioaddr, CMR2, lp->addr_mode);
932         write_reg(ioaddr, CMR2, CMR2_IRQOUT); /* Switch back to page 0 */
933     }
934     
935     static int __init atp_init_module(void) {
936     	if (debug)					/* Emit version even if no cards detected. */
937     		printk(KERN_INFO "%s" KERN_INFO "%s", versionA, versionB);
938     	return atp_init(NULL);
939     }
940     
941     static void __exit atp_cleanup_module(void) {
942     	struct net_device *next_dev;
943     
944     	while (root_atp_dev) {
945     		next_dev = ((struct net_local *)root_atp_dev->priv)->next_module;
946     		unregister_netdev(root_atp_dev);
947     		/* No need to release_region(), since we never snarf it. */
948     		kfree(root_atp_dev);
949     		root_atp_dev = next_dev;
950     	}
951     }
952     
953     module_init(atp_init_module);
954     module_exit(atp_cleanup_module);
955