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