1 /* atp.c: Attached (pocket) ethernet adapter driver for linux. */
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
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;
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.
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
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
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
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
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
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>
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,};
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. */
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. */
187 /* This code, written by wwc@super.org, resets the adapter every
188 TIMED_CHECKER ticks. This recovers from an unknown error which
190 #define TIMED_CHECKER (HZ/4)
192 #include <linux/timer.h>
193 static void atp_timed_checker(unsigned long ignored);
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);
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. */
237 for (port = ports; *port; port++) {
239 outb(0x57, ioaddr + PAR_DATA);
240 if (inb(ioaddr + PAR_DATA) != 0x57)
242 if (atp_probe1(ioaddr) == 0)
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;
256 outb(0xff, ioaddr + PAR_DATA);
257 /* Save the original value of the Control register, in case we guessed
259 saved_ctrl_reg = inb(ioaddr + PAR_CONTROL);
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
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));
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);
281 status = read_nibble(ioaddr, CMR1);
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));
290 if ((status & 0x78) != 0x08) {
291 /* The pocket adapter probe failed, restore the control register. */
292 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
295 status = read_nibble(ioaddr, CMR2_h);
296 if ((status & 0x78) != 0x10) {
297 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
301 dev = alloc_etherdev(sizeof(struct net_local));
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. */
313 else if (ioaddr == 0x378)
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. */
327 printk(KERN_INFO "%s" KERN_INFO "%s", versionA, versionB);
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. */
345 dev->if_port = xcvr[0];
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);
366 lp->next_module = root_atp_dev;
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;
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)
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.
399 * CLK: ______| |___| |
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);
415 if (read_nibble(ioaddr, PROM_DATA) & EE_DATA_READ)
418 write_reg_high(ioaddr, PROM_CMD, EE_CLK_LOW & ~EE_CS);
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);
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);
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);
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;
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);
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)
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);
515 write_byte_mode0(ioaddr, *packet++);
516 } while (--length > pad_len) ;
518 write_byte_mode0(ioaddr, 0);
519 } while (--length > 0) ;
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);
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++);
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"
550 np->stats.tx_errors++;
551 /* Try to restart the adapter. */
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;
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 */
584 lp->tx_unit_busy = 1;
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;
597 /* The typical workload of the driver:
598 Handle the network interface interrupts. */
599 static irqreturn_t atp_interrupt(int irq, void *dev_instance)
601 struct net_device *dev = (struct net_device *)dev_instance;
602 struct net_local *lp;
604 static int num_tx_since_rx;
605 int boguscount = max_interrupt_work;
609 printk(KERN_ERR "ATP_interrupt(): irq %d for unknown device.\n", irq);
612 ioaddr = dev->base_addr;
613 lp = netdev_priv(dev);
615 spin_lock(&lp->lock);
617 /* Disable additional spurious interrupts. */
618 outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
620 /* The adapter's output is currently the IRQ line, switch it to data. */
621 write_reg(ioaddr, CMR2, CMR2_NULL);
622 write_reg(ioaddr, IMR, 0);
624 if (net_debug > 5) printk(KERN_DEBUG "%s: In interrupt ", dev->name);
625 while (--boguscount > 0) {
626 int status = read_nibble(ioaddr, ISR);
627 if (net_debug > 5) printk("loop status %02x..", status);
629 if (status & (ISR_RxOK<<3)) {
631 write_reg(ioaddr, ISR, ISR_RxOK); /* Clear the Rx interrupt. */
633 int read_status = read_nibble(ioaddr, CMR1);
635 printk("handling Rx packet %02x..", read_status);
636 /* We acknowledged the normal Rx interrupt, so if the interrupt
637 is still outstanding we must have a Rx error. */
638 if (read_status & (CMR1_IRQ << 3)) { /* Overrun. */
639 lp->stats.rx_over_errors++;
640 /* Set to no-accept mode long enough to remove a packet. */
641 write_reg_high(ioaddr, CMR2, CMR2h_OFF);
643 /* Clear the interrupt and return to normal Rx mode. */
644 write_reg_high(ioaddr, ISR, ISRh_RxErr);
645 write_reg_high(ioaddr, CMR2, lp->addr_mode);
646 } else if ((read_status & (CMR1_BufEnb << 3)) == 0) {
651 } while (--boguscount > 0);
652 } else if (status & ((ISR_TxErr + ISR_TxOK)<<3)) {
654 if (net_debug > 6) printk("handling Tx done..");
655 /* Clear the Tx interrupt. We should check for too many failures
656 and reinitialize the adapter. */
657 write_reg(ioaddr, ISR, ISR_TxErr + ISR_TxOK);
658 if (status & (ISR_TxErr<<3)) {
659 lp->stats.collisions++;
660 if (++lp->re_tx > 15) {
661 lp->stats.tx_aborted_errors++;
665 /* Attempt to retransmit. */
666 if (net_debug > 6) printk("attempting to ReTx");
667 write_reg(ioaddr, CMR1, CMR1_ReXmit + CMR1_Xmit);
669 /* Finish up the transmit. */
670 lp->stats.tx_packets++;
671 lp->pac_cnt_in_tx_buf--;
672 if ( lp->saved_tx_size) {
673 trigger_send(ioaddr, lp->saved_tx_size);
674 lp->saved_tx_size = 0;
677 lp->tx_unit_busy = 0;
678 netif_wake_queue(dev); /* Inform upper layers. */
681 } else if (num_tx_since_rx > 8
682 && time_after(jiffies, dev->last_rx + HZ)) {
684 printk(KERN_DEBUG "%s: Missed packet? No Rx after %d Tx and "
685 "%ld jiffies status %02x CMR1 %02x.\n", dev->name,
686 num_tx_since_rx, jiffies - dev->last_rx, status,
687 (read_nibble(ioaddr, CMR1) >> 3) & 15);
688 lp->stats.rx_missed_errors++;
696 /* This following code fixes a rare (and very difficult to track down)
697 problem where the adapter forgets its ethernet address. */
700 for (i = 0; i < 6; i++)
701 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
702 #if 0 && defined(TIMED_CHECKER)
703 mod_timer(&lp->timer, jiffies + TIMED_CHECKER);
707 /* Tell the adapter that it can go back to using the output line as IRQ. */
708 write_reg(ioaddr, CMR2, CMR2_IRQOUT);
709 /* Enable the physical interrupt line, which is sure to be low until.. */
710 outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
711 /* .. we enable the interrupt sources. */
712 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
713 write_reg_high(ioaddr, IMR, ISRh_RxErr); /* Hmmm, really needed? */
715 spin_unlock(&lp->lock);
717 if (net_debug > 5) printk("exiting interrupt.\n");
718 return IRQ_RETVAL(handled);
722 /* This following code fixes a rare (and very difficult to track down)
723 problem where the adapter forgets its ethernet address. */
724 static void atp_timed_checker(unsigned long data)
726 struct net_device *dev = (struct net_device *)data;
727 long ioaddr = dev->base_addr;
728 struct net_local *lp = netdev_priv(dev);
729 int tickssofar = jiffies - lp->last_rx_time;
732 spin_lock(&lp->lock);
733 if (tickssofar > 2*HZ) {
735 for (i = 0; i < 6; i++)
736 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
737 lp->last_rx_time = jiffies;
739 for (i = 0; i < 6; i++)
740 if (read_cmd_byte(ioaddr, PAR0 + i) != atp_timed_dev->dev_addr[i])
742 struct net_local *lp = netdev_priv(atp_timed_dev);
743 write_reg_byte(ioaddr, PAR0 + i, atp_timed_dev->dev_addr[i]);
745 lp->stats.tx_errors++;
747 lp->stats.tx_dropped++;
749 lp->stats.collisions++;
751 lp->stats.rx_errors++;
755 spin_unlock(&lp->lock);
756 lp->timer.expires = jiffies + TIMED_CHECKER;
757 add_timer(&lp->timer);
761 /* We have a good packet(s), get it/them out of the buffers. */
762 static void net_rx(struct net_device *dev)
764 struct net_local *lp = netdev_priv(dev);
765 long ioaddr = dev->base_addr;
766 struct rx_header rx_head;
768 /* Process the received packet. */
769 outb(EOC+MAR, ioaddr + PAR_DATA);
770 read_block(ioaddr, 8, (unsigned char*)&rx_head, dev->if_port);
772 printk(KERN_DEBUG " rx_count %04x %04x %04x %04x..", rx_head.pad,
773 rx_head.rx_count, rx_head.rx_status, rx_head.cur_addr);
774 if ((rx_head.rx_status & 0x77) != 0x01) {
775 lp->stats.rx_errors++;
776 if (rx_head.rx_status & 0x0004) lp->stats.rx_frame_errors++;
777 else if (rx_head.rx_status & 0x0002) lp->stats.rx_crc_errors++;
779 printk(KERN_DEBUG "%s: Unknown ATP Rx error %04x.\n",
780 dev->name, rx_head.rx_status);
781 if (rx_head.rx_status & 0x0020) {
782 lp->stats.rx_fifo_errors++;
783 write_reg_high(ioaddr, CMR1, CMR1h_TxENABLE);
784 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
785 } else if (rx_head.rx_status & 0x0050)
789 /* Malloc up new buffer. The "-4" omits the FCS (CRC). */
790 int pkt_len = (rx_head.rx_count & 0x7ff) - 4;
793 skb = dev_alloc_skb(pkt_len + 2);
795 printk(KERN_ERR "%s: Memory squeeze, dropping packet.\n",
797 lp->stats.rx_dropped++;
802 skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
803 read_block(ioaddr, pkt_len, skb_put(skb,pkt_len), dev->if_port);
804 skb->protocol = eth_type_trans(skb, dev);
806 dev->last_rx = jiffies;
807 lp->stats.rx_packets++;
808 lp->stats.rx_bytes += pkt_len;
811 write_reg(ioaddr, CMR1, CMR1_NextPkt);
812 lp->last_rx_time = jiffies;
816 static void read_block(long ioaddr, int length, unsigned char *p, int data_mode)
819 if (data_mode <= 3) { /* Mode 0 or 1 */
820 outb(Ctrl_LNibRead, ioaddr + PAR_CONTROL);
821 outb(length == 8 ? RdAddr | HNib | MAR : RdAddr | MAR,
823 if (data_mode <= 1) { /* Mode 0 or 1 */
824 do *p++ = read_byte_mode0(ioaddr); while (--length > 0);
825 } else /* Mode 2 or 3 */
826 do *p++ = read_byte_mode2(ioaddr); while (--length > 0);
827 } else if (data_mode <= 5)
828 do *p++ = read_byte_mode4(ioaddr); while (--length > 0);
830 do *p++ = read_byte_mode6(ioaddr); while (--length > 0);
832 outb(EOC+HNib+MAR, ioaddr + PAR_DATA);
833 outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
836 /* The inverse routine to net_open(). */
838 net_close(struct net_device *dev)
840 struct net_local *lp = netdev_priv(dev);
841 long ioaddr = dev->base_addr;
843 netif_stop_queue(dev);
845 del_timer_sync(&lp->timer);
847 /* Flush the Tx and disable Rx here. */
848 lp->addr_mode = CMR2h_OFF;
849 write_reg_high(ioaddr, CMR2, CMR2h_OFF);
851 /* Free the IRQ line. */
852 outb(0x00, ioaddr + PAR_CONTROL);
853 free_irq(dev->irq, dev);
855 /* Reset the ethernet hardware and activate the printer pass-through. */
856 write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX);
860 /* Get the current statistics. This may be called with the card open or
862 static struct net_device_stats *
863 net_get_stats(struct net_device *dev)
865 struct net_local *lp = netdev_priv(dev);
870 * Set or clear the multicast filter for this adapter.
873 static void set_rx_mode_8002(struct net_device *dev)
875 struct net_local *lp = netdev_priv(dev);
876 long ioaddr = dev->base_addr;
878 if ( dev->mc_count > 0 || (dev->flags & (IFF_ALLMULTI|IFF_PROMISC))) {
879 /* We must make the kernel realise we had to move
880 * into promisc mode or we start all out war on
883 dev->flags|=IFF_PROMISC;
884 lp->addr_mode = CMR2h_PROMISC;
886 lp->addr_mode = CMR2h_Normal;
887 write_reg_high(ioaddr, CMR2, lp->addr_mode);
890 static void set_rx_mode_8012(struct net_device *dev)
892 struct net_local *lp = netdev_priv(dev);
893 long ioaddr = dev->base_addr;
894 unsigned char new_mode, mc_filter[8]; /* Multicast hash filter */
897 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
898 new_mode = CMR2h_PROMISC;
899 } else if ((dev->mc_count > 1000) || (dev->flags & IFF_ALLMULTI)) {
900 /* Too many to filter perfectly -- accept all multicasts. */
901 memset(mc_filter, 0xff, sizeof(mc_filter));
902 new_mode = CMR2h_Normal;
904 struct dev_mc_list *mclist;
906 memset(mc_filter, 0, sizeof(mc_filter));
907 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
908 i++, mclist = mclist->next)
910 int filterbit = ether_crc_le(ETH_ALEN, mclist->dmi_addr) & 0x3f;
911 mc_filter[filterbit >> 5] |= 1 << (filterbit & 31);
913 new_mode = CMR2h_Normal;
915 lp->addr_mode = new_mode;
916 write_reg(ioaddr, CMR2, CMR2_IRQOUT | 0x04); /* Switch to page 1. */
917 for (i = 0; i < 8; i++)
918 write_reg_byte(ioaddr, i, mc_filter[i]);
919 if (net_debug > 2 || 1) {
921 printk(KERN_DEBUG "%s: Mode %d, setting multicast filter to",
922 dev->name, lp->addr_mode);
923 for (i = 0; i < 8; i++)
924 printk(" %2.2x", mc_filter[i]);
928 write_reg_high(ioaddr, CMR2, lp->addr_mode);
929 write_reg(ioaddr, CMR2, CMR2_IRQOUT); /* Switch back to page 0 */
932 static int __init atp_init_module(void) {
933 if (debug) /* Emit version even if no cards detected. */
934 printk(KERN_INFO "%s" KERN_INFO "%s", versionA, versionB);
938 static void __exit atp_cleanup_module(void) {
939 struct net_device *next_dev;
941 while (root_atp_dev) {
942 next_dev = ((struct net_local *)root_atp_dev->priv)->next_module;
943 unregister_netdev(root_atp_dev);
944 /* No need to release_region(), since we never snarf it. */
945 free_netdev(root_atp_dev);
946 root_atp_dev = next_dev;
950 module_init(atp_init_module);
951 module_exit(atp_cleanup_module);