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