* add p cc
[mascara-docs.git] / i386 / linux / linux-0.99 / drivers / net / atp.c
blobd86dc82235af3bc07bb2befc0852d0061e139843
1 /* atp.c: Attached (pocket) ethernet adaptor driver for linux. */
2 /*
3 Written 1993 by Donald Becker.
4 Copyright 1993 United States Government as represented by the Director,
5 National Security Agency. This software may only be used and distributed
6 according to the terms of the GNU Public License as modified by SRC,
7 incorported herein by reference.
9 The author may be reached as becker@super.org or
10 C/O Supercomputing Research Ctr., 17100 Science Dr., Bowie MD 20715
14 static char *version =
15 "atp.c:v0.03 1/19/94 Donald Becker (becker@super.org)\n";
18 This file is a device driver for the RealTek (aka AT-Lan-Tec) pocket
19 ethernet adaptor. This is a common low-cost OEM pocket ethernet
20 adaptor, sold under many names.
22 Sources:
23 This driver was written from the packet driver assembly code provided by
24 Vincent Bono of AT-Lan-Tec. Ever try to figure out how a complicated
25 device works just from the assembly code? It ain't pretty. The following
26 description is written based on guesses and writing lots of special-purpose
27 code to test my theorized operation.
29 Theory of Operation
31 The RTL8002 adaptor seems to be built around a custom spin of the SEEQ
32 controller core. It probably has a 16K or 64K internal packet buffer, of
33 which the first 4K is devoted to transmit and the rest to receive.
34 The controller maintains the queue of received packet and the packet buffer
35 access pointer internally, with only 'reset to beginning' and 'skip to next
36 packet' commands visible. The transmit packet queue holds two (or more?)
37 packets: both 'retransmit this packet' (due to collision) and 'transmit next
38 packet' commands must be started by hand.
40 The station address is stored in a standard bit-serial EEPROM which must be
41 read (ughh) by the device driver. (Provisions have been made for
42 substituting a 74S288 PROM, but I haven't gotten reports of any models
43 using it.) Unlike built-in devices, a pocket adaptor can temporarily lose
44 power without indication to the device driver. The major effect is that
45 the station address, receive filter (promiscuous, etc.) and transceiver
46 must be reset.
48 The controller itself has 16 registers, some of which use only the lower
49 bits. The registers are read and written 4 bits at a time. The four bit
50 register address is presented on the data lines along with a few additional
51 timing and control bits. The data is then read from status port or written
52 to the data port.
54 Since the bulk data transfer of the actual packets through the slow
55 parallel port dominates the driver's running time, four distinct data
56 (non-register) transfer modes are provided by the adaptor, two in each
57 direction. In the first mode timing for the nibble transfers is
58 provided through the data port. In the second mode the same timing is
59 provided through the control port. In either case the data is read from
60 the status port and written to the data port, just as it is accessing
61 registers.
63 In addition to the basic data transfer methods, several more are modes are
64 created by adding some delay by doing multiple reads of the data to allow
65 it to stabilize. This delay seems to be needed on most machines.
67 The data transfer mode is stored in the 'dev->if_port' field. Its default
68 value is '4'. It may be overriden at boot-time using the third parameter
69 to the "ether=..." initialization.
71 The header file <atp.h> provides inline functions that encapsulate the
72 register and data access methods. These functions are hand-tuned to
73 generate reasonable object code. This header file also documents my
74 interpretations of the device registers.
77 #include <linux/config.h> /* Used only to override default values. */
78 #include <linux/kernel.h>
79 #include <linux/sched.h>
80 #include <linux/types.h>
81 #include <linux/fcntl.h>
82 #include <linux/interrupt.h>
83 #include <linux/ptrace.h>
84 #include <linux/ioport.h>
85 #include <linux/in.h>
86 #include <linux/malloc.h>
87 #include <linux/string.h>
88 #include <asm/system.h>
89 #include <asm/bitops.h>
90 #include <asm/io.h>
91 #include <asm/dma.h>
92 #include <errno.h>
94 #include "dev.h"
95 #include "eth.h"
96 #include "skbuff.h"
97 #include "arp.h"
99 #include "atp.h"
101 /* Compatibility definitions for earlier kernel versions. */
102 #ifndef HAVE_AUTOIRQ
103 /* From auto_irq.c, in ioport.h for later versions. */
104 extern void autoirq_setup(int waittime);
105 extern int autoirq_report(int waittime);
106 /* The map from IRQ number (as passed to the interrupt handler) to
107 'struct device'. */
108 extern struct device *irq2dev_map[16];
109 #endif
111 #ifndef HAVE_ALLOC_SKB
112 #define alloc_skb(size, priority) (struct sk_buff *) kmalloc(size,priority)
113 #define kfree_skbmem(addr, size) kfree_s(addr,size);
114 #endif
116 #ifndef HAVE_PORTRESERVE
117 #define check_region(ioaddr, size) 0
118 #define snarf_region(ioaddr, size); do ; while (0)
119 #endif
121 /* use 0 for production, 1 for verification, >2 for debug */
122 #ifndef NET_DEBUG
123 #define NET_DEBUG 4
124 #endif
125 static unsigned int net_debug = NET_DEBUG;
127 /* The number of low I/O ports used by the ethercard. */
128 #define ETHERCARD_TOTAL_SIZE 3
130 /* Index to functions, as function prototypes. */
132 extern int atp_probe(struct device *dev);
134 static int atp_probe1(struct device *dev, short ioaddr);
135 static void init_dev(struct device *dev);
136 static void get_node_ID(struct device *dev);
137 static unsigned short eeprom_op(short ioaddr, unsigned int cmd);
138 static int net_open(struct device *dev);
139 static void hardware_init(struct device *dev);
140 static void write_packet(short ioaddr, int length, unsigned char *packet, int mode);
141 static void trigger_send(short ioaddr, int length);
142 static int net_send_packet(struct sk_buff *skb, struct device *dev);
143 static void net_interrupt(int reg_ptr);
144 static void net_rx(struct device *dev);
145 static void read_block(short ioaddr, int length, unsigned char *buffer, int data_mode);
146 static int net_close(struct device *dev);
147 static struct enet_statistics *net_get_stats(struct device *dev);
148 #ifdef HAVE_MULTICAST
149 static void set_multicast_list(struct device *dev, int num_addrs, void *addrs);
150 #endif
153 /* Check for a network adaptor of this type, and return '0' iff one exists.
154 If dev->base_addr == 0, probe all likely locations.
155 If dev->base_addr == 1, always return failure.
156 If dev->base_addr == 2, alloate space for the device and return success
157 (detachable devices only).
160 atp_init(struct device *dev)
162 int *port, ports[] = {0x378, 0x278, 0x3bc, 0};
163 int base_addr = dev->base_addr;
165 if (base_addr > 0x1ff) /* Check a single specified location. */
166 return atp_probe1(dev, base_addr);
167 else if (base_addr == 1) /* Don't probe at all. */
168 return ENXIO;
170 for (port = ports; *port; port++) {
171 int ioaddr = *port;
172 outb(0x57, ioaddr + PAR_DATA);
173 if (inb(ioaddr + PAR_DATA) != 0x57)
174 continue;
175 if (atp_probe1(dev, ioaddr) == 0)
176 return 0;
179 return ENODEV;
182 static int atp_probe1(struct device *dev, short ioaddr)
184 int saved_ctrl_reg, status;
186 outb(0xff, ioaddr + PAR_DATA);
187 /* Save the original value of the Control register, in case we guessed
188 wrong. */
189 saved_ctrl_reg = inb(ioaddr + PAR_CONTROL);
190 /* IRQEN=0, SLCTB=high INITB=high, AUTOFDB=high, STBB=high. */
191 outb(0x04, ioaddr + PAR_CONTROL);
192 write_reg_high(ioaddr, CMR1, CMR1h_RESET);
193 eeprom_delay(2048);
194 status = read_nibble(ioaddr, CMR1);
196 if ((status & 0x78) != 0x08) {
197 /* The pocket adaptor probe failed, restore the control register. */
198 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
199 return 1;
201 status = read_nibble(ioaddr, CMR2_h);
202 if ((status & 0x78) != 0x10) {
203 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
204 return 1;
206 /* Find the IRQ used by triggering an interrupt. */
207 write_reg_byte(ioaddr, CMR2, 0x01); /* No accept mode, IRQ out. */
208 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE); /* Enable Tx and Rx. */
210 /* Omit autoIRQ routine for now. Use "table lookup" instead. Uhgggh. */
211 if (ioaddr == 0x378)
212 dev->irq = 7;
213 else
214 dev->irq = 5;
215 write_reg_high(ioaddr, CMR1, CMR1h_TxRxOFF); /* Diable Tx and Rx units. */
216 write_reg(ioaddr, CMR2, CMR2_NULL);
218 dev->base_addr = ioaddr;
220 /* Read the station address PROM. */
221 get_node_ID(dev);
223 printk("%s: Pocket adaptor found at %#3x, IRQ %d, SAPROM "
224 "%02X:%02X:%02X:%02X:%02X:%02X.\n", dev->name, dev->base_addr,
225 dev->irq, dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
226 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
228 /* Leave the hardware in a reset state. */
229 write_reg_high(ioaddr, CMR1, CMR1h_RESET);
231 if (net_debug)
232 printk(version);
234 /* Initialize the device structure. */
235 init_dev(dev);
236 dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
237 memset(dev->priv, 0, sizeof(struct net_local));
241 struct net_local *lp = (struct net_local *)dev->priv;
242 lp->addr_mode = CMR2h_Normal;
245 /* For the ATP adaptor the "if_port" is really the data transfer mode. */
246 dev->if_port = (dev->mem_start & 0xf) ? dev->mem_start & 0x7 : 4;
247 if (dev->mem_end & 0xf)
248 net_debug = dev->mem_end & 7;
250 dev->open = net_open;
251 dev->stop = net_close;
252 dev->hard_start_xmit = net_send_packet;
253 dev->get_stats = net_get_stats;
254 #ifdef HAVE_MULTICAST
255 dev->set_multicast_list = &set_multicast_list;
256 #endif
258 return 0;
261 /* Fill in the fields of the device structure with ethernet-generic values.
262 This should be in a common file instead of per-driver. */
263 static void init_dev(struct device *dev)
265 int i;
267 for (i = 0; i < DEV_NUMBUFFS; i++)
268 dev->buffs[i] = NULL;
270 dev->hard_header = eth_header;
271 dev->add_arp = eth_add_arp;
272 dev->queue_xmit = dev_queue_xmit;
273 dev->rebuild_header = eth_rebuild_header;
274 dev->type_trans = eth_type_trans;
276 dev->type = ARPHRD_ETHER;
277 dev->hard_header_len = ETH_HLEN;
278 dev->mtu = 1500; /* eth_mtu */
279 dev->addr_len = ETH_ALEN;
280 for (i = 0; i < ETH_ALEN; i++) {
281 dev->broadcast[i]=0xff;
284 /* New-style flags. */
285 dev->flags = IFF_BROADCAST;
286 dev->family = AF_INET;
287 dev->pa_addr = 0;
288 dev->pa_brdaddr = 0;
289 dev->pa_mask = 0;
290 dev->pa_alen = sizeof(unsigned long);
293 /* Read the station address PROM, usually a word-wide EEPROM. */
294 static void get_node_ID(struct device *dev)
296 short ioaddr = dev->base_addr;
297 int sa_offset = 0;
298 int i;
300 write_reg(ioaddr, CMR2, CMR2_EEPROM); /* Point to the EEPROM control registers. */
302 /* Some adaptors have the station address at offset 15 instead of offset
303 zero. Check for it, and fix it if needed. */
304 if (eeprom_op(ioaddr, EE_READ(0)) == 0xffff)
305 sa_offset = 15;
307 for (i = 0; i < 3; i++)
308 ((unsigned short *)dev->dev_addr)[i] =
309 ntohs(eeprom_op(ioaddr, EE_READ(sa_offset + i)));
311 write_reg(ioaddr, CMR2, CMR2_NULL);
315 An EEPROM read command starts by shifting out 0x60+address, and then
316 shifting in the serial data. See the NatSemi databook for details.
317 * ________________
318 * CS : __|
319 * ___ ___
320 * CLK: ______| |___| |
321 * __ _______ _______
322 * DI : __X_______X_______X
323 * DO : _________X_______X
326 static unsigned short eeprom_op(short ioaddr, unsigned int cmd)
328 unsigned eedata_out = 0;
329 int num_bits = EE_CMD_SIZE;
331 while (--num_bits >= 0) {
332 char outval = test_bit(num_bits, &cmd) ? EE_DATA_WRITE : 0;
333 write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_LOW);
334 eeprom_delay(5);
335 write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_HIGH);
336 eedata_out <<= 1;
337 if (read_nibble(ioaddr, PROM_DATA) & EE_DATA_READ)
338 eedata_out++;
339 eeprom_delay(5);
341 write_reg_high(ioaddr, PROM_CMD, EE_CLK_LOW & ~EE_CS);
342 return eedata_out;
346 /* Open/initialize the board. This is called (in the current kernel)
347 sometime after booting when the 'ifconfig' program is run.
349 This routine sets everything up anew at each open, even
350 registers that "should" only need to be set once at boot, so that
351 there is non-reboot way to recover if something goes wrong.
353 This is an attachable device: if there is no dev->priv entry then it wasn't
354 probed for at boot-time, and we need to probe for it again.
356 static int net_open(struct device *dev)
359 /* The interrupt line is turned off (tri-stated) when the device isn't in
360 use. That's especially important for "attached" interfaces where the
361 port or interrupt may be shared. */
362 if (irq2dev_map[dev->irq] != 0
363 || (irq2dev_map[dev->irq] = dev) == 0
364 || request_irq(dev->irq, &net_interrupt)) {
365 return -EAGAIN;
368 hardware_init(dev);
369 dev->start = 1;
370 return 0;
373 /* This routine resets the hardware. We initialize everything, assuming that
374 the hardware may have been temporarily detacted. */
375 static void hardware_init(struct device *dev)
377 struct net_local *lp = (struct net_local *)dev->priv;
378 int ioaddr = dev->base_addr;
379 int i;
381 write_reg_high(ioaddr, CMR1, CMR1h_RESET);
383 for (i = 0; i < 6; i++)
384 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
386 write_reg_high(ioaddr, CMR2, lp->addr_mode);
388 if (net_debug > 2) {
389 printk("%s: Reset: current Rx mode %d.\n", dev->name,
390 (read_nibble(ioaddr, CMR2_h) >> 3) & 0x0f);
393 write_reg(ioaddr, CMR2, CMR2_IRQOUT);
394 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
396 /* Enable the interrupt line from the serial port. */
397 outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
399 /* Unmask the interesting interrupts. */
400 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
401 write_reg_high(ioaddr, IMR, ISRh_RxErr);
403 lp->tx_unit_busy = 0;
404 lp->pac_cnt_in_tx_buf = 0;
405 lp->saved_tx_size = 0;
407 dev->tbusy = 0;
408 dev->interrupt = 0;
411 static void trigger_send(short ioaddr, int length)
413 write_reg_byte(ioaddr, TxCNT0, length & 0xff);
414 write_reg(ioaddr, TxCNT1, length >> 8);
415 write_reg(ioaddr, CMR1, CMR1_Xmit);
418 static void write_packet(short ioaddr, int length, unsigned char *packet, int data_mode)
420 length = (length + 1) & ~1; /* Round up to word length. */
421 outb(EOC+MAR, ioaddr + PAR_DATA);
422 if ((data_mode & 1) == 0) {
423 /* Write the packet out, starting with the write addr. */
424 outb(WrAddr+MAR, ioaddr + PAR_DATA);
425 do {
426 write_byte_mode0(ioaddr, *packet++);
427 } while (--length > 0) ;
428 } else {
429 /* Write the packet out in slow mode. */
430 unsigned char outbyte = *packet++;
432 outb(Ctrl_LNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
433 outb(WrAddr+MAR, ioaddr + PAR_DATA);
435 outb((outbyte & 0x0f)|0x40, ioaddr + PAR_DATA);
436 outb(outbyte & 0x0f, ioaddr + PAR_DATA);
437 outbyte >>= 4;
438 outb(outbyte & 0x0f, ioaddr + PAR_DATA);
439 outb(Ctrl_HNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
440 while (--length > 0)
441 write_byte_mode1(ioaddr, *packet++);
443 /* Terminate the Tx frame. End of write: ECB. */
444 outb(0xff, ioaddr + PAR_DATA);
445 outb(Ctrl_HNibWrite | Ctrl_SelData | Ctrl_IRQEN, ioaddr + PAR_CONTROL);
448 static int
449 net_send_packet(struct sk_buff *skb, struct device *dev)
451 struct net_local *lp = (struct net_local *)dev->priv;
452 int ioaddr = dev->base_addr;
454 if (dev->tbusy) {
455 /* If we get here, some higher level has decided we are broken.
456 There should really be a "kick me" function call instead. */
457 int tickssofar = jiffies - dev->trans_start;
458 if (tickssofar < 5)
459 return 1;
460 printk("%s: transmit timed out, %s?\n", dev->name,
461 inb(ioaddr + PAR_CONTROL) & 0x10 ? "network cable problem"
462 : "IRQ conflict");
463 lp->stats.tx_errors++;
464 /* Try to restart the adaptor. */
465 hardware_init(dev);
466 dev->tbusy=0;
467 dev->trans_start = jiffies;
470 /* If some higher layer thinks we've missed an tx-done interrupt
471 we are passed NULL. Caution: dev_tint() handles the cli()/sti()
472 itself. */
473 if (skb == NULL) {
474 dev_tint(dev);
475 return 0;
478 /* For ethernet, fill in the header. This should really be done by a
479 higher level, rather than duplicated for each ethernet adaptor. */
480 if (!skb->arp && dev->rebuild_header(skb->data, dev)) {
481 skb->dev = dev;
482 arp_queue (skb);
483 return 0;
485 skb->arp=1;
487 /* Block a timer-based transmit from overlapping. This could better be
488 done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
489 if (set_bit(0, (void*)&dev->tbusy) != 0)
490 printk("%s: Transmitter access conflict.\n", dev->name);
491 else {
492 short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
493 unsigned char *buf = skb->data;
494 int flags;
496 /* Disable interrupts by writing 0x00 to the Interrupt Mask Register.
497 This sequence must not be interrupted by an incoming packet. */
498 save_flags(flags);
499 cli();
500 write_reg(ioaddr, IMR, 0);
501 write_reg_high(ioaddr, IMR, 0);
502 restore_flags(flags);
504 write_packet(ioaddr, length, buf, dev->if_port);
506 lp->pac_cnt_in_tx_buf++;
507 if (lp->tx_unit_busy == 0) {
508 trigger_send(ioaddr, length);
509 lp->saved_tx_size = 0; /* Redundent */
510 lp->re_tx = 0;
511 lp->tx_unit_busy = 1;
512 } else
513 lp->saved_tx_size = length;
515 dev->trans_start = jiffies;
516 /* Re-enable the LPT interrupts. */
517 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
518 write_reg_high(ioaddr, IMR, ISRh_RxErr);
521 if (skb->free)
522 kfree_skb (skb, FREE_WRITE);
524 return 0;
527 /* The typical workload of the driver:
528 Handle the network interface interrupts. */
529 static void
530 net_interrupt(int reg_ptr)
532 int irq = -(((struct pt_regs *)reg_ptr)->orig_eax+2);
533 struct device *dev = (struct device *)(irq2dev_map[irq]);
534 struct net_local *lp;
535 int ioaddr, status, boguscount = 20;
536 static int num_tx_since_rx = 0;
538 if (dev == NULL) {
539 printk ("ATP_interrupt(): irq %d for unknown device.\n", irq);
540 return;
542 dev->interrupt = 1;
544 ioaddr = dev->base_addr;
545 lp = (struct net_local *)dev->priv;
547 /* Disable additional spurious interrupts. */
548 outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
550 /* The adaptor's output is currently the IRQ line, switch it to data. */
551 write_reg(ioaddr, CMR2, CMR2_NULL);
552 write_reg(ioaddr, IMR, 0);
554 if (net_debug > 5) printk("%s: In interrupt ", dev->name);
555 while (--boguscount > 0) {
556 status = read_nibble(ioaddr, ISR);
557 if (net_debug > 5) printk("loop status %02x..", status);
559 if (status & (ISR_RxOK<<3)) {
560 write_reg(ioaddr, ISR, ISR_RxOK); /* Clear the Rx interrupt. */
561 do {
562 int read_status = read_nibble(ioaddr, CMR1);
563 if (net_debug > 6)
564 printk("handling Rx packet %02x..", read_status);
565 /* We acknowledged the normal Rx interrupt, so if the interrupt
566 is still outstanding we must have a Rx error. */
567 if (read_status & (CMR1_IRQ << 3)) { /* Overrun. */
568 lp->stats.rx_over_errors++;
569 /* Set to no-accept mode long enough to remove a packet. */
570 write_reg_high(ioaddr, CMR2, CMR2h_OFF);
571 net_rx(dev);
572 /* Clear the interrupt and return to normal Rx mode. */
573 write_reg_high(ioaddr, ISR, ISRh_RxErr);
574 write_reg_high(ioaddr, CMR2, lp->addr_mode);
575 } else if ((read_status & (CMR1_BufEnb << 3)) == 0) {
576 net_rx(dev);
577 dev->last_rx = jiffies;
578 num_tx_since_rx = 0;
579 } else
580 break;
581 } while (--boguscount > 0);
582 } else if (status & ((ISR_TxErr + ISR_TxOK)<<3)) {
583 if (net_debug > 6) printk("handling Tx done..");
584 /* Clear the Tx interrupt. We should check for too many failures
585 and reinitialize the adaptor. */
586 write_reg(ioaddr, ISR, ISR_TxErr + ISR_TxOK);
587 if (status & (ISR_TxErr<<3)) {
588 lp->stats.collisions++;
589 if (++lp->re_tx > 15) {
590 lp->stats.tx_aborted_errors++;
591 hardware_init(dev);
592 break;
594 /* Attempt to retransmit. */
595 if (net_debug > 6) printk("attempting to ReTx");
596 write_reg(ioaddr, CMR1, CMR1_ReXmit + CMR1_Xmit);
597 } else {
598 /* Finish up the transmit. */
599 lp->stats.tx_packets++;
600 lp->pac_cnt_in_tx_buf--;
601 if ( lp->saved_tx_size) {
602 trigger_send(ioaddr, lp->saved_tx_size);
603 lp->saved_tx_size = 0;
604 lp->re_tx = 0;
605 } else
606 lp->tx_unit_busy = 0;
607 dev->tbusy = 0;
608 mark_bh(INET_BH); /* Inform upper layers. */
610 num_tx_since_rx++;
611 } else if (num_tx_since_rx > 8
612 && jiffies > dev->last_rx + 100) {
613 if (net_debug > 2)
614 printk("%s: Missed packet? No Rx after %d Tx and %d jiffies"
615 " status %02x CMR1 %02x.\n", dev->name,
616 num_tx_since_rx, jiffies - dev->last_rx, status,
617 (read_nibble(ioaddr, CMR1) >> 3) & 15);
618 lp->stats.rx_missed_errors++;
619 hardware_init(dev);
620 num_tx_since_rx = 0;
621 break;
622 } else
623 break;
627 int i;
628 for (i = 0; i < 6; i++)
629 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
632 /* Tell the adaptor that it can go back to using the output line as IRQ. */
633 write_reg(ioaddr, CMR2, CMR2_IRQOUT);
634 /* Enable the physical interrupt line, which is sure to be low until.. */
635 outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
636 /* .. we enable the interrupt sources. */
637 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
638 write_reg_high(ioaddr, IMR, ISRh_RxErr); /* Hmmm, really needed? */
640 if (net_debug > 5) printk("exiting interrupt.\n");
642 dev->interrupt = 0;
644 return;
647 /* We have a good packet(s), get it/them out of the buffers. */
648 static void net_rx(struct device *dev)
650 struct net_local *lp = (struct net_local *)dev->priv;
651 int ioaddr = dev->base_addr;
652 #ifdef notdef
653 ushort header[4];
654 #else
655 struct rx_header rx_head;
656 #endif
658 /* Process the received packet. */
659 outb(EOC+MAR, ioaddr + PAR_DATA);
660 read_block(ioaddr, 8, (unsigned char*)&rx_head, dev->if_port);
661 if (net_debug > 5)
662 printk(" rx_count %04x %04x %04x %04x..", rx_head.pad,
663 rx_head.rx_count, rx_head.rx_status, rx_head.cur_addr);
664 if ((rx_head.rx_status & 0x77) != 0x01) {
665 lp->stats.rx_errors++;
666 /* Ackkk! I don't have any documentation on what the error bits mean!
667 The best I can do is slap the device around a bit. */
668 if (net_debug > 3) printk("%s: Unknown ATP Rx error %04x.\n",
669 dev->name, rx_head.rx_status);
670 hardware_init(dev);
671 return;
672 } else {
673 /* Malloc up new buffer. */
674 int pkt_len = (rx_head.rx_count & 0x7ff) - 4; /* The "-4" is omits the FCS (CRC). */
675 int sksize = sizeof(struct sk_buff) + pkt_len;
676 struct sk_buff *skb;
678 skb = alloc_skb(sksize, GFP_ATOMIC);
679 if (skb == NULL) {
680 printk("%s: Memory squeeze, dropping packet.\n", dev->name);
681 lp->stats.rx_dropped++;
682 goto done;
684 skb->mem_len = sksize;
685 skb->mem_addr = skb;
686 skb->len = pkt_len;
687 skb->dev = dev;
689 /* 'skb->data' points to the start of sk_buff data area. */
690 read_block(ioaddr, pkt_len, skb->data, dev->if_port);
692 if (net_debug > 6) {
693 unsigned char *data = skb->data;
694 printk(" data %02x%02x%02x %02x%02x%02x %02x%02x%02x %02x%02x%02x %02x%02x..",
695 data[0], data[1], data[2], data[3], data[4], data[5],
696 data[6], data[7], data[8], data[9], data[10], data[11],
697 data[12], data[13]);
700 #ifdef HAVE_NETIF_RX
701 netif_rx(skb);
702 #else
703 skb->lock = 0;
704 if (dev_rint((unsigned char*)skb, pkt_len, IN_SKBUFF, dev) != 0) {
705 kfree_s(skb, sksize);
706 lp->stats.rx_dropped++;
707 break;
709 #endif
710 lp->stats.rx_packets++;
712 done:
713 write_reg(ioaddr, CMR1, CMR1_NextPkt);
714 return;
717 static void read_block(short ioaddr, int length, unsigned char *p, int data_mode)
720 if (data_mode <= 3) { /* Mode 0 or 1 */
721 outb(Ctrl_LNibRead, ioaddr + PAR_CONTROL);
722 outb(length == 8 ? RdAddr | HNib | MAR : RdAddr | MAR,
723 ioaddr + PAR_DATA);
724 if (data_mode <= 1) { /* Mode 0 or 1 */
725 do *p++ = read_byte_mode0(ioaddr); while (--length > 0);
726 } else /* Mode 2 or 3 */
727 do *p++ = read_byte_mode2(ioaddr); while (--length > 0);
728 } else if (data_mode <= 5)
729 do *p++ = read_byte_mode4(ioaddr); while (--length > 0);
730 else
731 do *p++ = read_byte_mode6(ioaddr); while (--length > 0);
733 outb(EOC+HNib+MAR, ioaddr + PAR_DATA);
734 outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
737 /* The inverse routine to net_open(). */
738 static int
739 net_close(struct device *dev)
741 struct net_local *lp = (struct net_local *)dev->priv;
742 int ioaddr = dev->base_addr;
744 dev->tbusy = 1;
745 dev->start = 0;
747 /* Flush the Tx and disable Rx here. */
748 lp->addr_mode = CMR2h_OFF;
749 write_reg_high(ioaddr, CMR2, CMR2h_OFF);
751 /* Free the IRQ line. */
752 outb(0x00, ioaddr + PAR_CONTROL);
753 free_irq(dev->irq);
754 irq2dev_map[dev->irq] = 0;
756 /* Leave the hardware in a reset state. */
757 write_reg_high(ioaddr, CMR1, CMR1h_RESET);
759 return 0;
762 /* Get the current statistics. This may be called with the card open or
763 closed. */
764 static struct enet_statistics *
765 net_get_stats(struct device *dev)
767 struct net_local *lp = (struct net_local *)dev->priv;
768 return &lp->stats;
771 #ifdef HAVE_MULTICAST
772 /* Set or clear the multicast filter for this adaptor.
773 num_addrs == -1 Promiscuous mode, receive all packets
774 num_addrs == 0 Normal mode, clear multicast list
775 num_addrs > 0 Multicast mode, receive normal and MC packets, and do
776 best-effort filtering.
778 static void
779 set_multicast_list(struct device *dev, int num_addrs, void *addrs)
781 struct net_local *lp = (struct net_local *)dev->priv;
782 short ioaddr = dev->base_addr;
783 lp->addr_mode = num_addrs ? CMR2h_PROMISC : CMR2h_Normal;
784 write_reg_high(ioaddr, CMR2, lp->addr_mode);
786 #endif
789 * Local variables:
790 * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c atp.c"
791 * version-control: t
792 * kept-new-versions: 5
793 * tab-width: 4
794 * End: