Import 2.4.0-test4
[davej-history.git] / drivers / net / atp.c
blob096dd03c5511f0ee05424418d709a02decca62c7
1 /* atp.c: Attached (pocket) ethernet adapter driver for linux. */
2 /*
3 This is a driver for a commonly OEMed pocket (parallel port)
4 ethernet adapter.
6 Written 1993,1994,1995 by Donald Becker.
8 Copyright 1993 United States Government as represented by the
9 Director, National Security Agency.
11 This software may be used and distributed according to the terms
12 of the GNU Public License, incorporated herein by reference.
14 The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O
15 Center of Excellence in Space Data and Information Sciences
16 Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771
18 The timer-based reset code was written by Bill Carlson, wwc@super.org.
20 Modular support/softnet added by Alan Cox.
23 static const char *version =
24 "atp.c:v1.01 1/18/95 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
27 This file is a device driver for the RealTek (aka AT-Lan-Tec) pocket
28 ethernet adapter. This is a common low-cost OEM pocket ethernet
29 adapter, sold under many names.
31 Sources:
32 This driver was written from the packet driver assembly code provided by
33 Vincent Bono of AT-Lan-Tec. Ever try to figure out how a complicated
34 device works just from the assembly code? It ain't pretty. The following
35 description is written based on guesses and writing lots of special-purpose
36 code to test my theorized operation.
38 Theory of Operation
40 The RTL8002 adapter seems to be built around a custom spin of the SEEQ
41 controller core. It probably has a 16K or 64K internal packet buffer, of
42 which the first 4K is devoted to transmit and the rest to receive.
43 The controller maintains the queue of received packet and the packet buffer
44 access pointer internally, with only 'reset to beginning' and 'skip to next
45 packet' commands visible. The transmit packet queue holds two (or more?)
46 packets: both 'retransmit this packet' (due to collision) and 'transmit next
47 packet' commands must be started by hand.
49 The station address is stored in a standard bit-serial EEPROM which must be
50 read (ughh) by the device driver. (Provisions have been made for
51 substituting a 74S288 PROM, but I haven't gotten reports of any models
52 using it.) Unlike built-in devices, a pocket adapter can temporarily lose
53 power without indication to the device driver. The major effect is that
54 the station address, receive filter (promiscuous, etc.) and transceiver
55 must be reset.
57 The controller itself has 16 registers, some of which use only the lower
58 bits. The registers are read and written 4 bits at a time. The four bit
59 register address is presented on the data lines along with a few additional
60 timing and control bits. The data is then read from status port or written
61 to the data port.
63 Since the bulk data transfer of the actual packets through the slow
64 parallel port dominates the driver's running time, four distinct data
65 (non-register) transfer modes are provided by the adapter, two in each
66 direction. In the first mode timing for the nibble transfers is
67 provided through the data port. In the second mode the same timing is
68 provided through the control port. In either case the data is read from
69 the status port and written to the data port, just as it is accessing
70 registers.
72 In addition to the basic data transfer methods, several more are modes are
73 created by adding some delay by doing multiple reads of the data to allow
74 it to stabilize. This delay seems to be needed on most machines.
76 The data transfer mode is stored in the 'dev->if_port' field. Its default
77 value is '4'. It may be overridden at boot-time using the third parameter
78 to the "ether=..." initialization.
80 The header file <atp.h> provides inline functions that encapsulate the
81 register and data access methods. These functions are hand-tuned to
82 generate reasonable object code. This header file also documents my
83 interpretations of the device registers.
86 #include <linux/kernel.h>
87 #include <linux/module.h>
88 #include <linux/sched.h>
89 #include <linux/types.h>
90 #include <linux/fcntl.h>
91 #include <linux/interrupt.h>
92 #include <linux/ptrace.h>
93 #include <linux/ioport.h>
94 #include <linux/in.h>
95 #include <linux/malloc.h>
96 #include <linux/string.h>
97 #include <asm/system.h>
98 #include <asm/bitops.h>
99 #include <asm/io.h>
100 #include <asm/dma.h>
101 #include <linux/errno.h>
102 #include <linux/init.h>
104 #include <linux/netdevice.h>
105 #include <linux/etherdevice.h>
106 #include <linux/skbuff.h>
107 #include <linux/spinlock.h>
109 #include "atp.h"
111 /* use 0 for production, 1 for verification, >2 for debug */
112 #ifndef NET_DEBUG
113 #define NET_DEBUG 1
114 #endif
115 static unsigned int net_debug = NET_DEBUG;
117 /* The number of low I/O ports used by the ethercard. */
118 #define ETHERCARD_TOTAL_SIZE 3
120 /* This code, written by wwc@super.org, resets the adapter every
121 TIMED_CHECKER ticks. This recovers from an unknown error which
122 hangs the device. */
123 #define TIMED_CHECKER (HZ/4)
124 #ifdef TIMED_CHECKER
125 #include <linux/timer.h>
126 static void atp_timed_checker(unsigned long ignored);
127 static struct net_device *atp_timed_dev;
128 static struct timer_list atp_timer = { function: atp_timed_checker };
129 #endif
131 /* Index to functions, as function prototypes. */
133 static int atp_probe1(struct net_device *dev, short ioaddr);
134 static void get_node_ID(struct net_device *dev);
135 static unsigned short eeprom_op(short ioaddr, unsigned int cmd);
136 static int net_open(struct net_device *dev);
137 static void hardware_init(struct net_device *dev);
138 static void tx_timeout(struct net_device *dev);
139 static void write_packet(short ioaddr, int length, unsigned char *packet, int mode);
140 static void trigger_send(short ioaddr, int length);
141 static int net_send_packet(struct sk_buff *skb, struct net_device *dev);
142 static void net_interrupt(int irq, void *dev_id, struct pt_regs *regs);
143 static void net_rx(struct net_device *dev);
144 static void read_block(short ioaddr, int length, unsigned char *buffer, int data_mode);
145 static int net_close(struct net_device *dev);
146 static struct net_device_stats *net_get_stats(struct net_device *dev);
147 static void set_multicast_list(struct net_device *dev);
150 /* Check for a network adapter of this type, and return '0' iff one exists.
151 If dev->base_addr == 0, probe all likely locations.
152 If dev->base_addr == 1, always return failure.
153 If dev->base_addr == 2, allocate space for the device and return success
154 (detachable devices only).
157 int __init atp_init(struct net_device *dev)
159 int *port, ports[] = {0x378, 0x278, 0x3bc, 0};
160 int base_addr = dev->base_addr;
162 if (base_addr > 0x1ff) /* Check a single specified location. */
163 return atp_probe1(dev, base_addr);
164 else if (base_addr == 1) /* Don't probe at all. */
165 return -ENXIO;
167 for (port = ports; *port; port++) {
168 int ioaddr = *port;
169 outb(0x57, ioaddr + PAR_DATA);
170 if (inb(ioaddr + PAR_DATA) != 0x57)
171 continue;
172 if (atp_probe1(dev, ioaddr) == 0)
173 return 0;
176 return -ENODEV;
179 static int __init atp_probe1(struct net_device *dev, short ioaddr)
181 int saved_ctrl_reg, status;
183 outb(0xff, ioaddr + PAR_DATA);
184 /* Save the original value of the Control register, in case we guessed
185 wrong. */
186 saved_ctrl_reg = inb(ioaddr + PAR_CONTROL);
187 /* IRQEN=0, SLCTB=high INITB=high, AUTOFDB=high, STBB=high. */
188 outb(0x04, ioaddr + PAR_CONTROL);
189 write_reg_high(ioaddr, CMR1, CMR1h_RESET);
190 udelay(100);
191 status = read_nibble(ioaddr, CMR1);
193 if ((status & 0x78) != 0x08) {
194 /* The pocket adapter probe failed, restore the control register. */
195 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
196 return 1;
198 status = read_nibble(ioaddr, CMR2_h);
199 if ((status & 0x78) != 0x10) {
200 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
201 return 1;
203 /* Find the IRQ used by triggering an interrupt. */
204 write_reg_byte(ioaddr, CMR2, 0x01); /* No accept mode, IRQ out. */
205 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE); /* Enable Tx and Rx. */
207 /* Omit autoIRQ routine for now. Use "table lookup" instead. Uhgggh. */
208 if (ioaddr == 0x378)
209 dev->irq = 7;
210 else
211 dev->irq = 5;
212 write_reg_high(ioaddr, CMR1, CMR1h_TxRxOFF); /* Disable Tx and Rx units. */
213 write_reg(ioaddr, CMR2, CMR2_NULL);
215 dev->base_addr = ioaddr;
217 /* Read the station address PROM. */
218 get_node_ID(dev);
220 printk("%s: Pocket adapter found at %#3lx, IRQ %d, SAPROM "
221 "%02X:%02X:%02X:%02X:%02X:%02X.\n", dev->name, dev->base_addr,
222 dev->irq, dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
223 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
225 /* Leave the hardware in a reset state. */
226 write_reg_high(ioaddr, CMR1, CMR1h_RESET);
228 if (net_debug)
229 printk(version);
231 /* Initialize the device structure. */
232 ether_setup(dev);
233 dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
234 if (dev->priv == NULL)
235 return -ENOMEM;
236 memset(dev->priv, 0, sizeof(struct net_local));
238 struct net_local *lp = (struct net_local *)dev->priv;
239 lp->addr_mode = CMR2h_Normal;
240 spin_lock_init(&lp->lock);
243 /* For the ATP adapter the "if_port" is really the data transfer mode. */
244 dev->if_port = (dev->mem_start & 0xf) ? dev->mem_start & 0x7 : 4;
245 if (dev->mem_end & 0xf)
246 net_debug = dev->mem_end & 7;
248 dev->open = net_open;
249 dev->stop = net_close;
250 dev->hard_start_xmit = net_send_packet;
251 dev->get_stats = net_get_stats;
252 dev->set_multicast_list = set_multicast_list;
253 dev->tx_timeout = tx_timeout;
254 dev->watchdog_timeo = HZ/20;
256 #ifdef TIMED_CHECKER
257 del_timer(&atp_timer);
258 atp_timer.expires = jiffies + TIMED_CHECKER;
259 atp_timed_dev = dev;
260 add_timer(&atp_timer);
261 #endif
262 return 0;
265 /* Read the station address PROM, usually a word-wide EEPROM. */
266 static void __init get_node_ID(struct net_device *dev)
268 short ioaddr = dev->base_addr;
269 int sa_offset = 0;
270 int i;
272 write_reg(ioaddr, CMR2, CMR2_EEPROM); /* Point to the EEPROM control registers. */
274 /* Some adapters have the station address at offset 15 instead of offset
275 zero. Check for it, and fix it if needed. */
276 if (eeprom_op(ioaddr, EE_READ(0)) == 0xffff)
277 sa_offset = 15;
279 for (i = 0; i < 3; i++)
280 ((unsigned short *)dev->dev_addr)[i] =
281 ntohs(eeprom_op(ioaddr, EE_READ(sa_offset + i)));
283 write_reg(ioaddr, CMR2, CMR2_NULL);
287 An EEPROM read command starts by shifting out 0x60+address, and then
288 shifting in the serial data. See the NatSemi databook for details.
289 * ________________
290 * CS : __|
291 * ___ ___
292 * CLK: ______| |___| |
293 * __ _______ _______
294 * DI : __X_______X_______X
295 * DO : _________X_______X
298 static unsigned short __init eeprom_op(short ioaddr, unsigned int cmd)
300 unsigned eedata_out = 0;
301 int num_bits = EE_CMD_SIZE;
303 while (--num_bits >= 0) {
304 char outval = test_bit(num_bits, &cmd) ? EE_DATA_WRITE : 0;
305 write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_LOW);
306 udelay(5);
307 write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_HIGH);
308 eedata_out <<= 1;
309 if (read_nibble(ioaddr, PROM_DATA) & EE_DATA_READ)
310 eedata_out++;
311 udelay(5);
313 write_reg_high(ioaddr, PROM_CMD, EE_CLK_LOW & ~EE_CS);
314 return eedata_out;
318 /* Open/initialize the board. This is called (in the current kernel)
319 sometime after booting when the 'ifconfig' program is run.
321 This routine sets everything up anew at each open, even
322 registers that "should" only need to be set once at boot, so that
323 there is non-reboot way to recover if something goes wrong.
325 This is an attachable device: if there is no dev->priv entry then it wasn't
326 probed for at boot-time, and we need to probe for it again.
328 static int net_open(struct net_device *dev)
331 /* The interrupt line is turned off (tri-stated) when the device isn't in
332 use. That's especially important for "attached" interfaces where the
333 port or interrupt may be shared. */
335 if (request_irq(dev->irq, &net_interrupt, 0, "ATP", dev)) {
336 return -EAGAIN;
338 hardware_init(dev);
339 netif_start_queue(dev);
340 return 0;
343 /* This routine resets the hardware. We initialize everything, assuming that
344 the hardware may have been temporarily detached. */
345 static void hardware_init(struct net_device *dev)
347 struct net_local *lp = (struct net_local *)dev->priv;
348 int ioaddr = dev->base_addr;
349 int i;
351 write_reg_high(ioaddr, CMR1, CMR1h_RESET);
353 for (i = 0; i < 6; i++)
354 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
356 write_reg_high(ioaddr, CMR2, lp->addr_mode);
358 if (net_debug > 2) {
359 printk("%s: Reset: current Rx mode %d.\n", dev->name,
360 (read_nibble(ioaddr, CMR2_h) >> 3) & 0x0f);
363 write_reg(ioaddr, CMR2, CMR2_IRQOUT);
364 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
366 /* Enable the interrupt line from the serial port. */
367 outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
369 /* Unmask the interesting interrupts. */
370 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
371 write_reg_high(ioaddr, IMR, ISRh_RxErr);
373 lp->tx_unit_busy = 0;
374 lp->pac_cnt_in_tx_buf = 0;
375 lp->saved_tx_size = 0;
378 static void trigger_send(short ioaddr, int length)
380 write_reg_byte(ioaddr, TxCNT0, length & 0xff);
381 write_reg(ioaddr, TxCNT1, length >> 8);
382 write_reg(ioaddr, CMR1, CMR1_Xmit);
385 static void write_packet(short ioaddr, int length, unsigned char *packet, int data_mode)
387 length = (length + 1) & ~1; /* Round up to word length. */
388 outb(EOC+MAR, ioaddr + PAR_DATA);
389 if ((data_mode & 1) == 0) {
390 /* Write the packet out, starting with the write addr. */
391 outb(WrAddr+MAR, ioaddr + PAR_DATA);
392 do {
393 write_byte_mode0(ioaddr, *packet++);
394 } while (--length > 0) ;
395 } else {
396 /* Write the packet out in slow mode. */
397 unsigned char outbyte = *packet++;
399 outb(Ctrl_LNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
400 outb(WrAddr+MAR, ioaddr + PAR_DATA);
402 outb((outbyte & 0x0f)|0x40, ioaddr + PAR_DATA);
403 outb(outbyte & 0x0f, ioaddr + PAR_DATA);
404 outbyte >>= 4;
405 outb(outbyte & 0x0f, ioaddr + PAR_DATA);
406 outb(Ctrl_HNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
407 while (--length > 0)
408 write_byte_mode1(ioaddr, *packet++);
410 /* Terminate the Tx frame. End of write: ECB. */
411 outb(0xff, ioaddr + PAR_DATA);
412 outb(Ctrl_HNibWrite | Ctrl_SelData | Ctrl_IRQEN, ioaddr + PAR_CONTROL);
415 static void tx_timeout(struct net_device *dev)
417 struct net_local *lp = (struct net_local *)dev->priv;
418 int ioaddr = dev->base_addr;
419 /* If we get here, some higher level has decided we are broken. */
420 printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
421 inb(ioaddr + PAR_CONTROL) & 0x10 ? "network cable problem"
422 : "IRQ conflict");
423 lp->stats.tx_errors++;
424 /* Try to restart the adapter. */
425 hardware_init(dev);
426 dev->trans_start = jiffies;
427 netif_wake_queue(dev);
430 static int net_send_packet(struct sk_buff *skb, struct net_device *dev)
432 struct net_local *lp = (struct net_local *)dev->priv;
433 int ioaddr = dev->base_addr;
434 short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
435 unsigned char *buf = skb->data;
436 unsigned long flags;
438 netif_stop_queue(dev);
440 /* Disable interrupts by writing 0x00 to the Interrupt Mask Register.
441 This sequence must not be interrupted by an incoming packet. */
443 spin_lock_irqsave(&lp->lock, flags);
444 write_reg(ioaddr, IMR, 0);
445 write_reg_high(ioaddr, IMR, 0);
446 spin_unlock_irqrestore(&lp->lock, flags);
448 write_packet(ioaddr, length, buf, dev->if_port);
450 lp->pac_cnt_in_tx_buf++;
451 if (lp->tx_unit_busy == 0) {
452 trigger_send(ioaddr, length);
453 lp->saved_tx_size = 0; /* Redundant */
454 lp->re_tx = 0;
455 lp->tx_unit_busy = 1;
456 } else
457 lp->saved_tx_size = length;
459 dev->trans_start = jiffies;
460 /* Re-enable the LPT interrupts. */
461 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
462 write_reg_high(ioaddr, IMR, ISRh_RxErr);
463 dev_kfree_skb (skb);
464 return 0;
467 /* The typical workload of the driver:
468 Handle the network interface interrupts. */
469 static void
470 net_interrupt(int irq, void *dev_id, struct pt_regs * regs)
472 struct net_device *dev = dev_id;
473 struct net_local *lp;
474 int ioaddr, status, boguscount = 20;
475 static int num_tx_since_rx = 0;
477 ioaddr = dev->base_addr;
478 lp = (struct net_local *)dev->priv;
480 spin_lock(&lp->lock);
482 /* Disable additional spurious interrupts. */
483 outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
485 /* The adapter's output is currently the IRQ line, switch it to data. */
486 write_reg(ioaddr, CMR2, CMR2_NULL);
487 write_reg(ioaddr, IMR, 0);
489 if (net_debug > 5)
490 printk("%s: In interrupt ", dev->name);
492 while (--boguscount > 0)
494 status = read_nibble(ioaddr, ISR);
495 if (net_debug > 5)
496 printk("loop status %02x..", status);
498 if (status & (ISR_RxOK<<3)) {
499 write_reg(ioaddr, ISR, ISR_RxOK); /* Clear the Rx interrupt. */
500 do {
501 int read_status = read_nibble(ioaddr, CMR1);
502 if (net_debug > 6)
503 printk("handling Rx packet %02x..", read_status);
504 /* We acknowledged the normal Rx interrupt, so if the interrupt
505 is still outstanding we must have a Rx error. */
506 if (read_status & (CMR1_IRQ << 3)) { /* Overrun. */
507 lp->stats.rx_over_errors++;
508 /* Set to no-accept mode long enough to remove a packet. */
509 write_reg_high(ioaddr, CMR2, CMR2h_OFF);
510 net_rx(dev);
511 /* Clear the interrupt and return to normal Rx mode. */
512 write_reg_high(ioaddr, ISR, ISRh_RxErr);
513 write_reg_high(ioaddr, CMR2, lp->addr_mode);
514 } else if ((read_status & (CMR1_BufEnb << 3)) == 0) {
515 net_rx(dev);
516 dev->last_rx = jiffies;
517 num_tx_since_rx = 0;
518 } else
519 break;
520 } while (--boguscount > 0);
521 } else if (status & ((ISR_TxErr + ISR_TxOK)<<3)) {
522 if (net_debug > 6) printk("handling Tx done..");
523 /* Clear the Tx interrupt. We should check for too many failures
524 and reinitialize the adapter. */
525 write_reg(ioaddr, ISR, ISR_TxErr + ISR_TxOK);
526 if (status & (ISR_TxErr<<3)) {
527 lp->stats.collisions++;
528 if (++lp->re_tx > 15) {
529 lp->stats.tx_aborted_errors++;
530 hardware_init(dev);
531 break;
533 /* Attempt to retransmit. */
534 if (net_debug > 6)
535 printk("attempting to ReTx");
536 write_reg(ioaddr, CMR1, CMR1_ReXmit + CMR1_Xmit);
537 } else {
538 /* Finish up the transmit. */
539 lp->stats.tx_packets++;
540 lp->pac_cnt_in_tx_buf--;
541 if ( lp->saved_tx_size) {
542 trigger_send(ioaddr, lp->saved_tx_size);
543 lp->saved_tx_size = 0;
544 lp->re_tx = 0;
545 } else
546 lp->tx_unit_busy = 0;
547 netif_wake_queue(dev); /* Inform upper layers. */
549 num_tx_since_rx++;
550 } else if (num_tx_since_rx > 8
551 && jiffies - dev->last_rx > 100) {
552 if (net_debug > 2)
553 printk("%s: Missed packet? No Rx after %d Tx and %ld jiffies"
554 " status %02x CMR1 %02x.\n", dev->name,
555 num_tx_since_rx, jiffies - dev->last_rx, status,
556 (read_nibble(ioaddr, CMR1) >> 3) & 15);
557 lp->stats.rx_missed_errors++;
558 hardware_init(dev);
559 num_tx_since_rx = 0;
560 break;
561 } else
562 break;
565 /* This following code fixes a rare (and very difficult to track down)
566 problem where the adapter forgets its ethernet address. */
568 int i;
569 for (i = 0; i < 6; i++)
570 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
571 #ifdef TIMED_CHECKER
572 mod_timer(&atp_timer, jiffies+TIMED_CHECKER);
573 #endif
576 /* Tell the adapter that it can go back to using the output line as IRQ. */
577 write_reg(ioaddr, CMR2, CMR2_IRQOUT);
578 /* Enable the physical interrupt line, which is sure to be low until.. */
579 outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
580 /* .. we enable the interrupt sources. */
581 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
582 write_reg_high(ioaddr, IMR, ISRh_RxErr); /* Hmmm, really needed? */
584 if (net_debug > 5)
585 printk("exiting interrupt.\n");
587 spin_unlock(&lp->lock);
588 return;
591 #ifdef TIMED_CHECKER
592 /* This following code fixes a rare (and very difficult to track down)
593 problem where the adapter forgets its ethernet address. */
594 static void atp_timed_checker(unsigned long ignored)
596 int i;
597 struct net_local *lp = (struct net_local *)atp_timed_dev->priv;
598 int ioaddr = atp_timed_dev->base_addr;
600 spin_lock(&lp->lock);
601 for (i = 0; i < 6; i++)
602 write_reg_byte(ioaddr, PAR0 + i, atp_timed_dev->dev_addr[i]);
603 spin_unlock(&lp->lock);
604 mod_timer(&atp_timer, jiffies+TIMED_CHECKER);
606 #endif
608 /* We have a good packet(s), get it/them out of the buffers. */
609 static void net_rx(struct net_device *dev)
611 struct net_local *lp = (struct net_local *)dev->priv;
612 int ioaddr = dev->base_addr;
613 #ifdef notdef
614 ushort header[4];
615 #else
616 struct rx_header rx_head;
617 #endif
619 /* Process the received packet. */
620 outb(EOC+MAR, ioaddr + PAR_DATA);
621 read_block(ioaddr, 8, (unsigned char*)&rx_head, dev->if_port);
622 if (net_debug > 5)
623 printk(" rx_count %04x %04x %04x %04x..", rx_head.pad,
624 rx_head.rx_count, rx_head.rx_status, rx_head.cur_addr);
625 if ((rx_head.rx_status & 0x77) != 0x01) {
626 lp->stats.rx_errors++;
627 /* Ackkk! I don't have any documentation on what the error bits mean!
628 The best I can do is slap the device around a bit. */
629 if (net_debug > 3) printk("%s: Unknown ATP Rx error %04x.\n",
630 dev->name, rx_head.rx_status);
631 hardware_init(dev);
632 return;
633 } else {
634 /* Malloc up new buffer. */
635 int pkt_len = (rx_head.rx_count & 0x7ff) - 4; /* The "-4" is omits the FCS (CRC). */
636 struct sk_buff *skb;
638 skb = dev_alloc_skb(pkt_len);
639 if (skb == NULL) {
640 printk("%s: Memory squeeze, dropping packet.\n", dev->name);
641 lp->stats.rx_dropped++;
642 goto done;
644 skb->dev = dev;
646 read_block(ioaddr, pkt_len, skb_put(skb,pkt_len), dev->if_port);
648 if (net_debug > 6) {
649 unsigned char *data = skb->data;
650 printk(" data %02x%02x%02x %02x%02x%02x %02x%02x%02x"
651 "%02x%02x%02x %02x%02x..",
652 data[0], data[1], data[2], data[3], data[4], data[5],
653 data[6], data[7], data[8], data[9], data[10], data[11],
654 data[12], data[13]);
657 skb->protocol=eth_type_trans(skb,dev);
658 netif_rx(skb);
659 lp->stats.rx_packets++;
661 done:
662 write_reg(ioaddr, CMR1, CMR1_NextPkt);
663 return;
666 static void read_block(short ioaddr, int length, unsigned char *p, int data_mode)
669 if (data_mode <= 3) { /* Mode 0 or 1 */
670 outb(Ctrl_LNibRead, ioaddr + PAR_CONTROL);
671 outb(length == 8 ? RdAddr | HNib | MAR : RdAddr | MAR,
672 ioaddr + PAR_DATA);
673 if (data_mode <= 1) { /* Mode 0 or 1 */
674 do *p++ = read_byte_mode0(ioaddr); while (--length > 0);
675 } else /* Mode 2 or 3 */
676 do *p++ = read_byte_mode2(ioaddr); while (--length > 0);
677 } else if (data_mode <= 5)
678 do *p++ = read_byte_mode4(ioaddr); while (--length > 0);
679 else
680 do *p++ = read_byte_mode6(ioaddr); while (--length > 0);
682 outb(EOC+HNib+MAR, ioaddr + PAR_DATA);
683 outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
686 /* The inverse routine to net_open(). */
687 static int net_close(struct net_device *dev)
689 struct net_local *lp = (struct net_local *)dev->priv;
690 int ioaddr = dev->base_addr;
692 netif_stop_queue(dev);
694 /* Flush the Tx and disable Rx here. */
695 lp->addr_mode = CMR2h_OFF;
696 write_reg_high(ioaddr, CMR2, CMR2h_OFF);
698 /* Free the IRQ line. */
699 outb(0x00, ioaddr + PAR_CONTROL);
700 free_irq(dev->irq, dev);
702 /* Leave the hardware in a reset state. */
703 write_reg_high(ioaddr, CMR1, CMR1h_RESET);
705 return 0;
708 /* Get the current statistics. This may be called with the card open or
709 closed. */
710 static struct net_device_stats *net_get_stats(struct net_device *dev)
712 struct net_local *lp = (struct net_local *)dev->priv;
713 return &lp->stats;
717 * Set or clear the multicast filter for this adapter.
720 static void set_multicast_list(struct net_device *dev)
722 struct net_local *lp = (struct net_local *)dev->priv;
723 short ioaddr = dev->base_addr;
724 int num_addrs=dev->mc_count;
726 if(dev->flags&(IFF_ALLMULTI|IFF_PROMISC))
727 num_addrs=1;
729 * We must make the kernel realise we had to move
730 * into promisc mode or we start all out war on
731 * the cable. - AC
733 if(num_addrs)
734 dev->flags|=IFF_PROMISC;
735 lp->addr_mode = num_addrs ? CMR2h_PROMISC : CMR2h_Normal;
736 write_reg_high(ioaddr, CMR2, lp->addr_mode);
740 * Local variables:
741 * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c atp.c"
742 * version-control: t
743 * kept-new-versions: 5
744 * tab-width: 4
745 * End:
748 #ifdef MODULE
750 static int io = 0;
751 static struct net_device atp_dev = {
752 "", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, atp_init };
754 MODULE_PARM(io, "I/O port of the pocket adapter");
756 int init_module(void)
758 atp_dev.base_addr = io;
759 if (register_netdev(&atp_dev) != 0)
760 return -EIO;
761 return 0;
764 void cleanup_module(void)
766 unregister_netdev(&atp_dev);
769 #endif