[NET]: Nuke SET_MODULE_OWNER macro.
[safe/jmp/linux-2.6] / 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         _bit abuse fixed up by Alan Cox
31
32 */
33
34 static 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
40 static 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. */
44 static int max_interrupt_work = 15;
45
46 #define NUM_UNITS 2
47 /* The standard set of ISA module parameters. */
48 static int io[NUM_UNITS];
49 static int irq[NUM_UNITS];
50 static 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
150 MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
151 MODULE_DESCRIPTION("RealTek RTL8002/8012 parallel port Ethernet driver");
152 MODULE_LICENSE("GPL");
153
154 module_param(max_interrupt_work, int, 0);
155 module_param(debug, int, 0);
156 module_param_array(io, int, NULL, 0);
157 module_param_array(irq, int, NULL, 0);
158 module_param_array(xcvr, int, NULL, 0);
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 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. */
169 static char mux_8012[] = { 0xff, 0xf7, 0xff, 0xfb, 0xf3, 0xfb, 0xff, 0xf7,};
170
171 struct 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>
191 static void atp_timed_checker(unsigned long ignored);
192 #endif
193
194 /* Index to functions, as function prototypes. */
195
196 static int atp_probe1(long ioaddr);
197 static void get_node_ID(struct net_device *dev);
198 static unsigned short eeprom_op(long ioaddr, unsigned int cmd);
199 static int net_open(struct net_device *dev);
200 static void hardware_init(struct net_device *dev);
201 static void write_packet(long ioaddr, int length, unsigned char *packet, int pad, int mode);
202 static void trigger_send(long ioaddr, int length);
203 static int      atp_send_packet(struct sk_buff *skb, struct net_device *dev);
204 static irqreturn_t atp_interrupt(int irq, void *dev_id);
205 static void net_rx(struct net_device *dev);
206 static void read_block(long ioaddr, int length, unsigned char *buffer, int data_mode);
207 static int net_close(struct net_device *dev);
208 static struct net_device_stats *net_get_stats(struct net_device *dev);
209 static void set_rx_mode_8002(struct net_device *dev);
210 static void set_rx_mode_8012(struct net_device *dev);
211 static void tx_timeout(struct net_device *dev);
212
213
214 /* A list of all installed ATP devices, for removing the driver module. */
215 static struct net_device *root_atp_dev;
216
217 /* Check for a network adapter of this type, and return '0' iff one exists.
218    If dev->base_addr == 0, probe all likely locations.
219    If dev->base_addr == 1, always return failure.
220    If dev->base_addr == 2, allocate space for the device and return success
221    (detachable devices only).
222
223    FIXME: we should use the parport layer for this
224    */
225 static int __init atp_init(void)
226 {
227         int *port, ports[] = {0x378, 0x278, 0x3bc, 0};
228         int base_addr = io[0];
229
230         if (base_addr > 0x1ff)          /* Check a single specified location. */
231                 return atp_probe1(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(ioaddr) == 0)
241                         return 0;
242         }
243
244         return -ENODEV;
245 }
246
247 static int __init atp_probe1(long ioaddr)
248 {
249         struct net_device *dev = NULL;
250         struct net_local *lp;
251         int saved_ctrl_reg, status, i;
252         int res;
253
254         outb(0xff, ioaddr + PAR_DATA);
255         /* Save the original value of the Control register, in case we guessed
256            wrong. */
257         saved_ctrl_reg = inb(ioaddr + PAR_CONTROL);
258         if (net_debug > 3)
259                 printk("atp: Control register was %#2.2x.\n", saved_ctrl_reg);
260         /* IRQEN=0, SLCTB=high INITB=high, AUTOFDB=high, STBB=high. */
261         outb(0x04, ioaddr + PAR_CONTROL);
262 #ifndef final_version
263         if (net_debug > 3) {
264                 /* Turn off the printer multiplexer on the 8012. */
265                 for (i = 0; i < 8; i++)
266                         outb(mux_8012[i], ioaddr + PAR_DATA);
267                 write_reg(ioaddr, MODSEL, 0x00);
268                 printk("atp: Registers are ");
269                 for (i = 0; i < 32; i++)
270                         printk(" %2.2x", read_nibble(ioaddr, i));
271                 printk(".\n");
272         }
273 #endif
274         /* Turn off the printer multiplexer on the 8012. */
275         for (i = 0; i < 8; i++)
276                 outb(mux_8012[i], ioaddr + PAR_DATA);
277         write_reg_high(ioaddr, CMR1, CMR1h_RESET);
278         /* udelay() here? */
279         status = read_nibble(ioaddr, CMR1);
280
281         if (net_debug > 3) {
282                 printk(KERN_DEBUG "atp: Status nibble was %#2.2x..", status);
283                 for (i = 0; i < 32; i++)
284                         printk(" %2.2x", read_nibble(ioaddr, i));
285                 printk("\n");
286         }
287
288         if ((status & 0x78) != 0x08) {
289                 /* The pocket adapter probe failed, restore the control register. */
290                 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
291                 return -ENODEV;
292         }
293         status = read_nibble(ioaddr, CMR2_h);
294         if ((status & 0x78) != 0x10) {
295                 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
296                 return -ENODEV;
297         }
298
299         dev = alloc_etherdev(sizeof(struct net_local));
300         if (!dev)
301                 return -ENOMEM;
302
303         /* Find the IRQ used by triggering an interrupt. */
304         write_reg_byte(ioaddr, CMR2, 0x01);                     /* No accept mode, IRQ out. */
305         write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);  /* Enable Tx and Rx. */
306
307         /* Omit autoIRQ routine for now. Use "table lookup" instead.  Uhgggh. */
308         if (irq[0])
309                 dev->irq = irq[0];
310         else if (ioaddr == 0x378)
311                 dev->irq = 7;
312         else
313                 dev->irq = 5;
314         write_reg_high(ioaddr, CMR1, CMR1h_TxRxOFF); /* Disable Tx and Rx units. */
315         write_reg(ioaddr, CMR2, CMR2_NULL);
316
317         dev->base_addr = ioaddr;
318
319         /* Read the station address PROM.  */
320         get_node_ID(dev);
321
322 #ifndef MODULE
323         if (net_debug)
324                 printk(KERN_INFO "%s", version);
325 #endif
326
327         printk(KERN_NOTICE "%s: Pocket adapter found at %#3lx, IRQ %d, SAPROM "
328                    "%02X:%02X:%02X:%02X:%02X:%02X.\n", dev->name, dev->base_addr,
329                    dev->irq, dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
330                    dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
331
332         /* Reset the ethernet hardware and activate the printer pass-through. */
333         write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX);
334
335         lp = netdev_priv(dev);
336         lp->chip_type = RTL8002;
337         lp->addr_mode = CMR2h_Normal;
338         spin_lock_init(&lp->lock);
339
340         /* For the ATP adapter the "if_port" is really the data transfer mode. */
341         if (xcvr[0])
342                 dev->if_port = xcvr[0];
343         else
344                 dev->if_port = (dev->mem_start & 0xf) ? (dev->mem_start & 0x7) : 4;
345         if (dev->mem_end & 0xf)
346                 net_debug = dev->mem_end & 7;
347
348         dev->open               = net_open;
349         dev->stop               = net_close;
350         dev->hard_start_xmit    = atp_send_packet;
351         dev->get_stats          = net_get_stats;
352         dev->set_multicast_list =
353           lp->chip_type == RTL8002 ? &set_rx_mode_8002 : &set_rx_mode_8012;
354         dev->tx_timeout         = tx_timeout;
355         dev->watchdog_timeo     = TX_TIMEOUT;
356
357         res = register_netdev(dev);
358         if (res) {
359                 free_netdev(dev);
360                 return res;
361         }
362
363         lp->next_module = root_atp_dev;
364         root_atp_dev = dev;
365
366         return 0;
367 }
368
369 /* Read the station address PROM, usually a word-wide EEPROM. */
370 static void __init get_node_ID(struct net_device *dev)
371 {
372         long ioaddr = dev->base_addr;
373         int sa_offset = 0;
374         int i;
375
376         write_reg(ioaddr, CMR2, CMR2_EEPROM);     /* Point to the EEPROM control registers. */
377
378         /* Some adapters have the station address at offset 15 instead of offset
379            zero.  Check for it, and fix it if needed. */
380         if (eeprom_op(ioaddr, EE_READ(0)) == 0xffff)
381                 sa_offset = 15;
382
383         for (i = 0; i < 3; i++)
384                 ((u16 *)dev->dev_addr)[i] =
385                         be16_to_cpu(eeprom_op(ioaddr, EE_READ(sa_offset + i)));
386
387         write_reg(ioaddr, CMR2, CMR2_NULL);
388 }
389
390 /*
391   An EEPROM read command starts by shifting out 0x60+address, and then
392   shifting in the serial data. See the NatSemi databook for details.
393  *                 ________________
394  * CS : __|
395  *                         ___     ___
396  * CLK: ______|   |___|   |
397  *               __ _______ _______
398  * DI :  __X_______X_______X
399  * DO :  _________X_______X
400  */
401
402 static unsigned short __init eeprom_op(long ioaddr, u32 cmd)
403 {
404         unsigned eedata_out = 0;
405         int num_bits = EE_CMD_SIZE;
406
407         while (--num_bits >= 0) {
408                 char outval = (cmd & (1<<num_bits)) ? EE_DATA_WRITE : 0;
409                 write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_LOW);
410                 write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_HIGH);
411                 eedata_out <<= 1;
412                 if (read_nibble(ioaddr, PROM_DATA) & EE_DATA_READ)
413                         eedata_out++;
414         }
415         write_reg_high(ioaddr, PROM_CMD, EE_CLK_LOW & ~EE_CS);
416         return eedata_out;
417 }
418
419
420 /* Open/initialize the board.  This is called (in the current kernel)
421    sometime after booting when the 'ifconfig' program is run.
422
423    This routine sets everything up anew at each open, even
424    registers that "should" only need to be set once at boot, so that
425    there is non-reboot way to recover if something goes wrong.
426
427    This is an attachable device: if there is no dev->priv entry then it wasn't
428    probed for at boot-time, and we need to probe for it again.
429    */
430 static int net_open(struct net_device *dev)
431 {
432         struct net_local *lp = netdev_priv(dev);
433         int ret;
434
435         /* The interrupt line is turned off (tri-stated) when the device isn't in
436            use.  That's especially important for "attached" interfaces where the
437            port or interrupt may be shared. */
438         ret = request_irq(dev->irq, &atp_interrupt, 0, dev->name, dev);
439         if (ret)
440                 return ret;
441
442         hardware_init(dev);
443
444         init_timer(&lp->timer);
445         lp->timer.expires = jiffies + TIMED_CHECKER;
446         lp->timer.data = (unsigned long)dev;
447         lp->timer.function = &atp_timed_checker;    /* timer handler */
448         add_timer(&lp->timer);
449
450         netif_start_queue(dev);
451         return 0;
452 }
453
454 /* This routine resets the hardware.  We initialize everything, assuming that
455    the hardware may have been temporarily detached. */
456 static void hardware_init(struct net_device *dev)
457 {
458         struct net_local *lp = netdev_priv(dev);
459         long ioaddr = dev->base_addr;
460     int i;
461
462         /* Turn off the printer multiplexer on the 8012. */
463         for (i = 0; i < 8; i++)
464                 outb(mux_8012[i], ioaddr + PAR_DATA);
465         write_reg_high(ioaddr, CMR1, CMR1h_RESET);
466
467     for (i = 0; i < 6; i++)
468                 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
469
470         write_reg_high(ioaddr, CMR2, lp->addr_mode);
471
472         if (net_debug > 2) {
473                 printk(KERN_DEBUG "%s: Reset: current Rx mode %d.\n", dev->name,
474                            (read_nibble(ioaddr, CMR2_h) >> 3) & 0x0f);
475         }
476
477     write_reg(ioaddr, CMR2, CMR2_IRQOUT);
478     write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
479
480         /* Enable the interrupt line from the serial port. */
481         outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
482
483         /* Unmask the interesting interrupts. */
484     write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
485     write_reg_high(ioaddr, IMR, ISRh_RxErr);
486
487         lp->tx_unit_busy = 0;
488     lp->pac_cnt_in_tx_buf = 0;
489         lp->saved_tx_size = 0;
490 }
491
492 static void trigger_send(long ioaddr, int length)
493 {
494         write_reg_byte(ioaddr, TxCNT0, length & 0xff);
495         write_reg(ioaddr, TxCNT1, length >> 8);
496         write_reg(ioaddr, CMR1, CMR1_Xmit);
497 }
498
499 static void write_packet(long ioaddr, int length, unsigned char *packet, int pad_len, int data_mode)
500 {
501     if (length & 1)
502     {
503         length++;
504         pad_len++;
505     }
506
507     outb(EOC+MAR, ioaddr + PAR_DATA);
508     if ((data_mode & 1) == 0) {
509                 /* Write the packet out, starting with the write addr. */
510                 outb(WrAddr+MAR, ioaddr + PAR_DATA);
511                 do {
512                         write_byte_mode0(ioaddr, *packet++);
513                 } while (--length > pad_len) ;
514                 do {
515                         write_byte_mode0(ioaddr, 0);
516                 } while (--length > 0) ;
517     } else {
518                 /* Write the packet out in slow mode. */
519                 unsigned char outbyte = *packet++;
520
521                 outb(Ctrl_LNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
522                 outb(WrAddr+MAR, ioaddr + PAR_DATA);
523
524                 outb((outbyte & 0x0f)|0x40, ioaddr + PAR_DATA);
525                 outb(outbyte & 0x0f, ioaddr + PAR_DATA);
526                 outbyte >>= 4;
527                 outb(outbyte & 0x0f, ioaddr + PAR_DATA);
528                 outb(Ctrl_HNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
529                 while (--length > pad_len)
530                         write_byte_mode1(ioaddr, *packet++);
531                 while (--length > 0)
532                         write_byte_mode1(ioaddr, 0);
533     }
534     /* Terminate the Tx frame.  End of write: ECB. */
535     outb(0xff, ioaddr + PAR_DATA);
536     outb(Ctrl_HNibWrite | Ctrl_SelData | Ctrl_IRQEN, ioaddr + PAR_CONTROL);
537 }
538
539 static void tx_timeout(struct net_device *dev)
540 {
541         struct net_local *np = netdev_priv(dev);
542         long ioaddr = dev->base_addr;
543
544         printk(KERN_WARNING "%s: Transmit timed out, %s?\n", dev->name,
545                    inb(ioaddr + PAR_CONTROL) & 0x10 ? "network cable problem"
546                    :  "IRQ conflict");
547         np->stats.tx_errors++;
548         /* Try to restart the adapter. */
549         hardware_init(dev);
550         dev->trans_start = jiffies;
551         netif_wake_queue(dev);
552         np->stats.tx_errors++;
553 }
554
555 static int atp_send_packet(struct sk_buff *skb, struct net_device *dev)
556 {
557         struct net_local *lp = netdev_priv(dev);
558         long ioaddr = dev->base_addr;
559         int length;
560         unsigned long flags;
561
562         length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
563
564         netif_stop_queue(dev);
565
566         /* Disable interrupts by writing 0x00 to the Interrupt Mask Register.
567            This sequence must not be interrupted by an incoming packet. */
568
569         spin_lock_irqsave(&lp->lock, flags);
570         write_reg(ioaddr, IMR, 0);
571         write_reg_high(ioaddr, IMR, 0);
572         spin_unlock_irqrestore(&lp->lock, flags);
573
574         write_packet(ioaddr, length, skb->data, length-skb->len, dev->if_port);
575
576         lp->pac_cnt_in_tx_buf++;
577         if (lp->tx_unit_busy == 0) {
578                 trigger_send(ioaddr, length);
579                 lp->saved_tx_size = 0;                          /* Redundant */
580                 lp->re_tx = 0;
581                 lp->tx_unit_busy = 1;
582         } else
583                 lp->saved_tx_size = length;
584         /* Re-enable the LPT interrupts. */
585         write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
586         write_reg_high(ioaddr, IMR, ISRh_RxErr);
587
588         dev->trans_start = jiffies;
589         dev_kfree_skb (skb);
590         return 0;
591 }
592
593
594 /* The typical workload of the driver:
595    Handle the network interface interrupts. */
596 static irqreturn_t atp_interrupt(int irq, void *dev_instance)
597 {
598         struct net_device *dev = dev_instance;
599         struct net_local *lp;
600         long ioaddr;
601         static int num_tx_since_rx;
602         int boguscount = max_interrupt_work;
603         int handled = 0;
604
605         ioaddr = dev->base_addr;
606         lp = netdev_priv(dev);
607
608         spin_lock(&lp->lock);
609
610         /* Disable additional spurious interrupts. */
611         outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
612
613         /* The adapter's output is currently the IRQ line, switch it to data. */
614         write_reg(ioaddr, CMR2, CMR2_NULL);
615         write_reg(ioaddr, IMR, 0);
616
617         if (net_debug > 5) printk(KERN_DEBUG "%s: In interrupt ", dev->name);
618     while (--boguscount > 0) {
619                 int status = read_nibble(ioaddr, ISR);
620                 if (net_debug > 5) printk("loop status %02x..", status);
621
622                 if (status & (ISR_RxOK<<3)) {
623                         handled = 1;
624                         write_reg(ioaddr, ISR, ISR_RxOK); /* Clear the Rx interrupt. */
625                         do {
626                                 int read_status = read_nibble(ioaddr, CMR1);
627                                 if (net_debug > 6)
628                                         printk("handling Rx packet %02x..", read_status);
629                                 /* We acknowledged the normal Rx interrupt, so if the interrupt
630                                    is still outstanding we must have a Rx error. */
631                                 if (read_status & (CMR1_IRQ << 3)) { /* Overrun. */
632                                         lp->stats.rx_over_errors++;
633                                         /* Set to no-accept mode long enough to remove a packet. */
634                                         write_reg_high(ioaddr, CMR2, CMR2h_OFF);
635                                         net_rx(dev);
636                                         /* Clear the interrupt and return to normal Rx mode. */
637                                         write_reg_high(ioaddr, ISR, ISRh_RxErr);
638                                         write_reg_high(ioaddr, CMR2, lp->addr_mode);
639                                 } else if ((read_status & (CMR1_BufEnb << 3)) == 0) {
640                                         net_rx(dev);
641                                         num_tx_since_rx = 0;
642                                 } else
643                                         break;
644                         } while (--boguscount > 0);
645                 } else if (status & ((ISR_TxErr + ISR_TxOK)<<3)) {
646                         handled = 1;
647                         if (net_debug > 6)  printk("handling Tx done..");
648                         /* Clear the Tx interrupt.  We should check for too many failures
649                            and reinitialize the adapter. */
650                         write_reg(ioaddr, ISR, ISR_TxErr + ISR_TxOK);
651                         if (status & (ISR_TxErr<<3)) {
652                                 lp->stats.collisions++;
653                                 if (++lp->re_tx > 15) {
654                                         lp->stats.tx_aborted_errors++;
655                                         hardware_init(dev);
656                                         break;
657                                 }
658                                 /* Attempt to retransmit. */
659                                 if (net_debug > 6)  printk("attempting to ReTx");
660                                 write_reg(ioaddr, CMR1, CMR1_ReXmit + CMR1_Xmit);
661                         } else {
662                                 /* Finish up the transmit. */
663                                 lp->stats.tx_packets++;
664                                 lp->pac_cnt_in_tx_buf--;
665                                 if ( lp->saved_tx_size) {
666                                         trigger_send(ioaddr, lp->saved_tx_size);
667                                         lp->saved_tx_size = 0;
668                                         lp->re_tx = 0;
669                                 } else
670                                         lp->tx_unit_busy = 0;
671                                 netif_wake_queue(dev);  /* Inform upper layers. */
672                         }
673                         num_tx_since_rx++;
674                 } else if (num_tx_since_rx > 8
675                                    && time_after(jiffies, dev->last_rx + HZ)) {
676                         if (net_debug > 2)
677                                 printk(KERN_DEBUG "%s: Missed packet? No Rx after %d Tx and "
678                                            "%ld jiffies status %02x  CMR1 %02x.\n", dev->name,
679                                            num_tx_since_rx, jiffies - dev->last_rx, status,
680                                            (read_nibble(ioaddr, CMR1) >> 3) & 15);
681                         lp->stats.rx_missed_errors++;
682                         hardware_init(dev);
683                         num_tx_since_rx = 0;
684                         break;
685                 } else
686                         break;
687     }
688
689         /* This following code fixes a rare (and very difficult to track down)
690            problem where the adapter forgets its ethernet address. */
691         {
692                 int i;
693                 for (i = 0; i < 6; i++)
694                         write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
695 #if 0 && defined(TIMED_CHECKER)
696                 mod_timer(&lp->timer, jiffies + TIMED_CHECKER);
697 #endif
698         }
699
700         /* Tell the adapter that it can go back to using the output line as IRQ. */
701     write_reg(ioaddr, CMR2, CMR2_IRQOUT);
702         /* Enable the physical interrupt line, which is sure to be low until.. */
703         outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
704         /* .. we enable the interrupt sources. */
705         write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
706         write_reg_high(ioaddr, IMR, ISRh_RxErr);                        /* Hmmm, really needed? */
707
708         spin_unlock(&lp->lock);
709
710         if (net_debug > 5) printk("exiting interrupt.\n");
711         return IRQ_RETVAL(handled);
712 }
713
714 #ifdef TIMED_CHECKER
715 /* This following code fixes a rare (and very difficult to track down)
716    problem where the adapter forgets its ethernet address. */
717 static void atp_timed_checker(unsigned long data)
718 {
719         struct net_device *dev = (struct net_device *)data;
720         long ioaddr = dev->base_addr;
721         struct net_local *lp = netdev_priv(dev);
722         int tickssofar = jiffies - lp->last_rx_time;
723         int i;
724
725         spin_lock(&lp->lock);
726         if (tickssofar > 2*HZ) {
727 #if 1
728                 for (i = 0; i < 6; i++)
729                         write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
730                 lp->last_rx_time = jiffies;
731 #else
732                 for (i = 0; i < 6; i++)
733                         if (read_cmd_byte(ioaddr, PAR0 + i) != atp_timed_dev->dev_addr[i])
734                                 {
735                         struct net_local *lp = netdev_priv(atp_timed_dev);
736                         write_reg_byte(ioaddr, PAR0 + i, atp_timed_dev->dev_addr[i]);
737                         if (i == 2)
738                           lp->stats.tx_errors++;
739                         else if (i == 3)
740                           lp->stats.tx_dropped++;
741                         else if (i == 4)
742                           lp->stats.collisions++;
743                         else
744                           lp->stats.rx_errors++;
745                   }
746 #endif
747         }
748         spin_unlock(&lp->lock);
749         lp->timer.expires = jiffies + TIMED_CHECKER;
750         add_timer(&lp->timer);
751 }
752 #endif
753
754 /* We have a good packet(s), get it/them out of the buffers. */
755 static void net_rx(struct net_device *dev)
756 {
757         struct net_local *lp = netdev_priv(dev);
758         long ioaddr = dev->base_addr;
759         struct rx_header rx_head;
760
761         /* Process the received packet. */
762         outb(EOC+MAR, ioaddr + PAR_DATA);
763         read_block(ioaddr, 8, (unsigned char*)&rx_head, dev->if_port);
764         if (net_debug > 5)
765                 printk(KERN_DEBUG " rx_count %04x %04x %04x %04x..", rx_head.pad,
766                            rx_head.rx_count, rx_head.rx_status, rx_head.cur_addr);
767         if ((rx_head.rx_status & 0x77) != 0x01) {
768                 lp->stats.rx_errors++;
769                 if (rx_head.rx_status & 0x0004) lp->stats.rx_frame_errors++;
770                 else if (rx_head.rx_status & 0x0002) lp->stats.rx_crc_errors++;
771                 if (net_debug > 3)
772                         printk(KERN_DEBUG "%s: Unknown ATP Rx error %04x.\n",
773                                    dev->name, rx_head.rx_status);
774                 if  (rx_head.rx_status & 0x0020) {
775                         lp->stats.rx_fifo_errors++;
776                         write_reg_high(ioaddr, CMR1, CMR1h_TxENABLE);
777                         write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
778                 } else if (rx_head.rx_status & 0x0050)
779                         hardware_init(dev);
780                 return;
781         } else {
782                 /* Malloc up new buffer. The "-4" omits the FCS (CRC). */
783                 int pkt_len = (rx_head.rx_count & 0x7ff) - 4;
784                 struct sk_buff *skb;
785
786                 skb = dev_alloc_skb(pkt_len + 2);
787                 if (skb == NULL) {
788                         printk(KERN_ERR "%s: Memory squeeze, dropping packet.\n",
789                                    dev->name);
790                         lp->stats.rx_dropped++;
791                         goto done;
792                 }
793
794                 skb_reserve(skb, 2);    /* Align IP on 16 byte boundaries */
795                 read_block(ioaddr, pkt_len, skb_put(skb,pkt_len), dev->if_port);
796                 skb->protocol = eth_type_trans(skb, dev);
797                 netif_rx(skb);
798                 dev->last_rx = jiffies;
799                 lp->stats.rx_packets++;
800                 lp->stats.rx_bytes += pkt_len;
801         }
802  done:
803         write_reg(ioaddr, CMR1, CMR1_NextPkt);
804         lp->last_rx_time = jiffies;
805         return;
806 }
807
808 static void read_block(long ioaddr, int length, unsigned char *p, int data_mode)
809 {
810
811         if (data_mode <= 3) { /* Mode 0 or 1 */
812                 outb(Ctrl_LNibRead, ioaddr + PAR_CONTROL);
813                 outb(length == 8  ?  RdAddr | HNib | MAR  :  RdAddr | MAR,
814                          ioaddr + PAR_DATA);
815                 if (data_mode <= 1) { /* Mode 0 or 1 */
816                         do  *p++ = read_byte_mode0(ioaddr);  while (--length > 0);
817                 } else  /* Mode 2 or 3 */
818                         do  *p++ = read_byte_mode2(ioaddr);  while (--length > 0);
819         } else if (data_mode <= 5)
820                 do      *p++ = read_byte_mode4(ioaddr);  while (--length > 0);
821         else
822                 do      *p++ = read_byte_mode6(ioaddr);  while (--length > 0);
823
824     outb(EOC+HNib+MAR, ioaddr + PAR_DATA);
825         outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
826 }
827
828 /* The inverse routine to net_open(). */
829 static int
830 net_close(struct net_device *dev)
831 {
832         struct net_local *lp = netdev_priv(dev);
833         long ioaddr = dev->base_addr;
834
835         netif_stop_queue(dev);
836
837         del_timer_sync(&lp->timer);
838
839         /* Flush the Tx and disable Rx here. */
840         lp->addr_mode = CMR2h_OFF;
841         write_reg_high(ioaddr, CMR2, CMR2h_OFF);
842
843         /* Free the IRQ line. */
844         outb(0x00, ioaddr + PAR_CONTROL);
845         free_irq(dev->irq, dev);
846
847         /* Reset the ethernet hardware and activate the printer pass-through. */
848         write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX);
849         return 0;
850 }
851
852 /* Get the current statistics.  This may be called with the card open or
853    closed. */
854 static struct net_device_stats *
855 net_get_stats(struct net_device *dev)
856 {
857         struct net_local *lp = netdev_priv(dev);
858         return &lp->stats;
859 }
860
861 /*
862  *      Set or clear the multicast filter for this adapter.
863  */
864
865 static void set_rx_mode_8002(struct net_device *dev)
866 {
867         struct net_local *lp = netdev_priv(dev);
868         long ioaddr = dev->base_addr;
869
870         if ( dev->mc_count > 0 || (dev->flags & (IFF_ALLMULTI|IFF_PROMISC))) {
871                 /* We must make the kernel realise we had to move
872                  *      into promisc mode or we start all out war on
873                  *      the cable. - AC
874                  */
875                 dev->flags|=IFF_PROMISC;
876                 lp->addr_mode = CMR2h_PROMISC;
877         } else
878                 lp->addr_mode = CMR2h_Normal;
879         write_reg_high(ioaddr, CMR2, lp->addr_mode);
880 }
881
882 static void set_rx_mode_8012(struct net_device *dev)
883 {
884         struct net_local *lp = netdev_priv(dev);
885         long ioaddr = dev->base_addr;
886         unsigned char new_mode, mc_filter[8]; /* Multicast hash filter */
887         int i;
888
889         if (dev->flags & IFF_PROMISC) {                 /* Set promiscuous. */
890                 new_mode = CMR2h_PROMISC;
891         } else if ((dev->mc_count > 1000)  ||  (dev->flags & IFF_ALLMULTI)) {
892                 /* Too many to filter perfectly -- accept all multicasts. */
893                 memset(mc_filter, 0xff, sizeof(mc_filter));
894                 new_mode = CMR2h_Normal;
895         } else {
896                 struct dev_mc_list *mclist;
897
898                 memset(mc_filter, 0, sizeof(mc_filter));
899                 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
900                          i++, mclist = mclist->next)
901                 {
902                         int filterbit = ether_crc_le(ETH_ALEN, mclist->dmi_addr) & 0x3f;
903                         mc_filter[filterbit >> 5] |= 1 << (filterbit & 31);
904                 }
905                 new_mode = CMR2h_Normal;
906         }
907         lp->addr_mode = new_mode;
908     write_reg(ioaddr, CMR2, CMR2_IRQOUT | 0x04); /* Switch to page 1. */
909     for (i = 0; i < 8; i++)
910                 write_reg_byte(ioaddr, i, mc_filter[i]);
911         if (net_debug > 2 || 1) {
912                 lp->addr_mode = 1;
913                 printk(KERN_DEBUG "%s: Mode %d, setting multicast filter to",
914                            dev->name, lp->addr_mode);
915                 for (i = 0; i < 8; i++)
916                         printk(" %2.2x", mc_filter[i]);
917                 printk(".\n");
918         }
919
920         write_reg_high(ioaddr, CMR2, lp->addr_mode);
921     write_reg(ioaddr, CMR2, CMR2_IRQOUT); /* Switch back to page 0 */
922 }
923
924 static int __init atp_init_module(void) {
925         if (debug)                                      /* Emit version even if no cards detected. */
926                 printk(KERN_INFO "%s", version);
927         return atp_init();
928 }
929
930 static void __exit atp_cleanup_module(void) {
931         struct net_device *next_dev;
932
933         while (root_atp_dev) {
934                 next_dev = ((struct net_local *)root_atp_dev->priv)->next_module;
935                 unregister_netdev(root_atp_dev);
936                 /* No need to release_region(), since we never snarf it. */
937                 free_netdev(root_atp_dev);
938                 root_atp_dev = next_dev;
939         }
940 }
941
942 module_init(atp_init_module);
943 module_exit(atp_cleanup_module);