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