upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / drivers / net / ewrk3.c
1 /*  ewrk3.c: A DIGITAL EtherWORKS 3 ethernet driver for Linux.
2
3    Written 1994 by David C. Davies.
4
5    Copyright 1994 Digital Equipment Corporation.
6
7    This software may be used and distributed according to the terms of
8    the GNU General Public License, incorporated herein by reference.
9
10    This driver is written for the Digital Equipment Corporation series
11    of EtherWORKS ethernet cards:
12
13    DE203 Turbo (BNC)
14    DE204 Turbo (TP)
15    DE205 Turbo (TP BNC)
16
17    The driver has been tested on a relatively busy  network using the DE205
18    card and benchmarked with 'ttcp': it transferred 16M  of data at 975kB/s
19    (7.8Mb/s) to a DECstation 5000/200.
20
21    The author may be reached at davies@maniac.ultranet.com.
22
23    =========================================================================
24    This driver has been written  substantially  from scratch, although  its
25    inheritance of style and stack interface from 'depca.c' and in turn from
26    Donald Becker's 'lance.c' should be obvious.
27
28    The  DE203/4/5 boards  all  use a new proprietary   chip in place of the
29    LANCE chip used in prior cards  (DEPCA, DE100, DE200/1/2, DE210, DE422).
30    Use the depca.c driver in the standard distribution  for the LANCE based
31    cards from DIGITAL; this driver will not work with them.
32
33    The DE203/4/5 cards have 2  main modes: shared memory  and I/O only. I/O
34    only makes  all the card accesses through  I/O transactions and  no high
35    (shared)  memory is used. This  mode provides a >48% performance penalty
36    and  is deprecated in this  driver,  although allowed to provide initial
37    setup when hardstrapped.
38
39    The shared memory mode comes in 3 flavours: 2kB, 32kB and 64kB. There is
40    no point in using any mode other than the 2kB  mode - their performances
41    are virtually identical, although the driver has  been tested in the 2kB
42    and 32kB modes. I would suggest you uncomment the line:
43
44    FORCE_2K_MODE;
45
46    to allow the driver to configure the card as a  2kB card at your current
47    base  address, thus leaving more  room to clutter  your  system box with
48    other memory hungry boards.
49
50    As many ISA  and EISA cards  can be supported  under this driver  as you
51    wish, limited primarily  by the available IRQ lines,  rather than by the
52    available I/O addresses  (24 ISA,  16 EISA).   I have  checked different
53    configurations of  multiple  depca cards and  ewrk3 cards  and have  not
54    found a problem yet (provided you have at least depca.c v0.38) ...
55
56    The board IRQ setting   must be at  an unused  IRQ which is  auto-probed
57    using  Donald  Becker's autoprobe  routines.   All  these cards   are at
58    {5,10,11,15}.
59
60    No 16MB memory  limitation should exist with this  driver as DMA is  not
61    used and the common memory area is in low memory on the network card (my
62    current system has 20MB and I've not had problems yet).
63
64    The ability to load  this driver as a  loadable module has been included
65    and used  extensively during the  driver development (to save those long
66    reboot sequences). To utilise this ability, you have to do 8 things:
67
68    0) have a copy of the loadable modules code installed on your system.
69    1) copy ewrk3.c from the  /linux/drivers/net directory to your favourite
70    temporary directory.
71    2) edit the  source code near  line 1898 to reflect  the I/O address and
72    IRQ you're using.
73    3) compile  ewrk3.c, but include -DMODULE in  the command line to ensure
74    that the correct bits are compiled (see end of source code).
75    4) if you are wanting to add a new  card, goto 5. Otherwise, recompile a
76    kernel with the ewrk3 configuration turned off and reboot.
77    5) insmod ewrk3.o
78    [Alan Cox: Changed this so you can insmod ewrk3.o irq=x io=y]
79    [Adam Kropelin: now accepts irq=x1,x2 io=y1,y2 for multiple cards]
80    6) run the net startup bits for your new eth?? interface manually
81    (usually /etc/rc.inet[12] at boot time).
82    7) enjoy!
83
84    Note that autoprobing is not allowed in loadable modules - the system is
85    already up and running and you're messing with interrupts.
86
87    To unload a module, turn off the associated interface
88    'ifconfig eth?? down' then 'rmmod ewrk3'.
89
90    Promiscuous   mode has been  turned  off  in this driver,   but  all the
91    multicast  address bits  have been   turned on. This  improved the  send
92    performance on a busy network by about 13%.
93
94    Ioctl's have now been provided (primarily because  I wanted to grab some
95    packet size statistics). They  are patterned after 'plipconfig.c' from a
96    suggestion by Alan Cox.  Using these  ioctls, you can enable promiscuous
97    mode, add/delete multicast  addresses, change the hardware address,  get
98    packet size distribution statistics and muck around with the control and
99    status register. I'll add others if and when the need arises.
100
101    TO DO:
102    ------
103
104
105    Revision History
106    ----------------
107
108    Version   Date        Description
109
110    0.1     26-aug-94   Initial writing. ALPHA code release.
111    0.11    31-aug-94   Fixed: 2k mode memory base calc.,
112    LeMAC version calc.,
113    IRQ vector assignments during autoprobe.
114    0.12    31-aug-94   Tested working on LeMAC2 (DE20[345]-AC) card.
115    Fixed up MCA hash table algorithm.
116    0.20     4-sep-94   Added IOCTL functionality.
117    0.21    14-sep-94   Added I/O mode.
118    0.21axp 15-sep-94   Special version for ALPHA AXP Linux V1.0.
119    0.22    16-sep-94   Added more IOCTLs & tidied up.
120    0.23    21-sep-94   Added transmit cut through.
121    0.24    31-oct-94   Added uid checks in some ioctls.
122    0.30     1-nov-94   BETA code release.
123    0.31     5-dec-94   Added check/allocate region code.
124    0.32    16-jan-95   Broadcast packet fix.
125    0.33    10-Feb-95   Fix recognition bug reported by <bkm@star.rl.ac.uk>.
126    0.40    27-Dec-95   Rationalise MODULE and autoprobe code.
127    Rewrite for portability & updated.
128    ALPHA support from <jestabro@amt.tay1.dec.com>
129    Added verify_area() calls in ewrk3_ioctl() from
130    suggestion by <heiko@colossus.escape.de>.
131    Add new multicasting code.
132    0.41    20-Jan-96   Fix IRQ set up problem reported by
133    <kenneth@bbs.sas.ntu.ac.sg>.
134    0.42    22-Apr-96   Fix alloc_device() bug <jari@markkus2.fimr.fi>
135    0.43    16-Aug-96   Update alloc_device() to conform to de4x5.c
136    0.44    08-Nov-01   use library crc32 functions <Matt_Domsch@dell.com>
137    0.45    19-Jul-02   fix unaligned access on alpha <martin@bruli.net>
138    0.46    10-Oct-02   Multiple NIC support when module <akropel1@rochester.rr.com>
139    0.47    18-Oct-02   ethtool support <akropel1@rochester.rr.com>
140    0.48    18-Oct-02   cli/sti removal for 2.5 <vda@port.imtp.ilyichevsk.odessa.ua>
141    ioctl locking, signature search cleanup <akropel1@rochester.rr.com>
142
143    =========================================================================
144  */
145
146 #include <linux/module.h>
147 #include <linux/kernel.h>
148 #include <linux/string.h>
149 #include <linux/errno.h>
150 #include <linux/ioport.h>
151 #include <linux/slab.h>
152 #include <linux/interrupt.h>
153 #include <linux/delay.h>
154 #include <linux/init.h>
155 #include <linux/crc32.h>
156 #include <linux/netdevice.h>
157 #include <linux/etherdevice.h>
158 #include <linux/skbuff.h>
159 #include <linux/ethtool.h>
160 #include <linux/time.h>
161 #include <linux/types.h>
162 #include <linux/unistd.h>
163 #include <linux/ctype.h>
164 #include <linux/bitops.h>
165
166 #include <asm/io.h>
167 #include <asm/dma.h>
168 #include <asm/uaccess.h>
169
170 #include "ewrk3.h"
171
172 #define DRV_NAME        "ewrk3"
173 #define DRV_VERSION     "0.48"
174
175 static char version[] __initdata =
176 DRV_NAME ":v" DRV_VERSION " 2002/10/18 davies@maniac.ultranet.com\n";
177
178 #ifdef EWRK3_DEBUG
179 static int ewrk3_debug = EWRK3_DEBUG;
180 #else
181 static int ewrk3_debug = 1;
182 #endif
183
184 #define EWRK3_NDA 0xffe0        /* No Device Address */
185
186 #define PROBE_LENGTH    32
187 #define ETH_PROM_SIG    0xAA5500FFUL
188
189 #ifndef EWRK3_SIGNATURE
190 #define EWRK3_SIGNATURE {"DE203","DE204","DE205",""}
191 #define EWRK3_STRLEN 8
192 #endif
193
194 #ifndef EWRK3_RAM_BASE_ADDRESSES
195 #define EWRK3_RAM_BASE_ADDRESSES {0xc0000,0xd0000,0x00000}
196 #endif
197
198 /*
199    ** Sets up the I/O area for the autoprobe.
200  */
201 #define EWRK3_IO_BASE 0x100     /* Start address for probe search */
202 #define EWRK3_IOP_INC 0x20      /* I/O address increment */
203 #define EWRK3_TOTAL_SIZE 0x20   /* required I/O address length */
204
205 /* If you change this, remember to also change MODULE_PARM array limits */
206 #ifndef MAX_NUM_EWRK3S
207 #define MAX_NUM_EWRK3S 21
208 #endif
209
210 #ifndef EWRK3_EISA_IO_PORTS
211 #define EWRK3_EISA_IO_PORTS 0x0c00      /* I/O port base address, slot 0 */
212 #endif
213
214 #ifndef MAX_EISA_SLOTS
215 #define MAX_EISA_SLOTS 16
216 #define EISA_SLOT_INC 0x1000
217 #endif
218
219 #define QUEUE_PKT_TIMEOUT (1*HZ)        /* Jiffies */
220
221 /*
222    ** EtherWORKS 3 shared memory window sizes
223  */
224 #define IO_ONLY         0x00
225 #define SHMEM_2K        0x800
226 #define SHMEM_32K       0x8000
227 #define SHMEM_64K       0x10000
228
229 /*
230    ** EtherWORKS 3 IRQ ENABLE/DISABLE
231  */
232 #define ENABLE_IRQs { \
233   icr |= lp->irq_mask;\
234   outb(icr, EWRK3_ICR);                     /* Enable the IRQs */\
235 }
236
237 #define DISABLE_IRQs { \
238   icr = inb(EWRK3_ICR);\
239   icr &= ~lp->irq_mask;\
240   outb(icr, EWRK3_ICR);                     /* Disable the IRQs */\
241 }
242
243 /*
244    ** EtherWORKS 3 START/STOP
245  */
246 #define START_EWRK3 { \
247   csr = inb(EWRK3_CSR);\
248   csr &= ~(CSR_TXD|CSR_RXD);\
249   outb(csr, EWRK3_CSR);                     /* Enable the TX and/or RX */\
250 }
251
252 #define STOP_EWRK3 { \
253   csr = (CSR_TXD|CSR_RXD);\
254   outb(csr, EWRK3_CSR);                     /* Disable the TX and/or RX */\
255 }
256
257 /*
258    ** The EtherWORKS 3 private structure
259  */
260 #define EWRK3_PKT_STAT_SZ 16
261 #define EWRK3_PKT_BIN_SZ  128   /* Should be >=100 unless you
262                                    increase EWRK3_PKT_STAT_SZ */
263
264 struct ewrk3_stats {
265         u32 bins[EWRK3_PKT_STAT_SZ];
266         u32 unicast;
267         u32 multicast;
268         u32 broadcast;
269         u32 excessive_collisions;
270         u32 tx_underruns;
271         u32 excessive_underruns;
272 };
273
274 struct ewrk3_private {
275         char adapter_name[80];  /* Name exported to /proc/ioports */
276         u_long shmem_base;      /* Shared memory start address */
277         u_long shmem_length;    /* Shared memory window length */
278         struct net_device_stats stats;  /* Public stats */
279         struct ewrk3_stats pktStats; /* Private stats counters */
280         u_char irq_mask;        /* Adapter IRQ mask bits */
281         u_char mPage;           /* Maximum 2kB Page number */
282         u_char lemac;           /* Chip rev. level */
283         u_char hard_strapped;   /* Don't allow a full open */
284         u_char txc;             /* Transmit cut through */
285         u_char *mctbl;          /* Pointer to the multicast table */
286         u_char led_mask;        /* Used to reserve LED access for ethtool */
287         spinlock_t hw_lock;
288 };
289
290 /*
291    ** Force the EtherWORKS 3 card to be in 2kB MODE
292  */
293 #define FORCE_2K_MODE { \
294   shmem_length = SHMEM_2K;\
295   outb(((mem_start - 0x80000) >> 11), EWRK3_MBR);\
296 }
297
298 /*
299    ** Public Functions
300  */
301 static int ewrk3_open(struct net_device *dev);
302 static int ewrk3_queue_pkt(struct sk_buff *skb, struct net_device *dev);
303 static irqreturn_t ewrk3_interrupt(int irq, void *dev_id, struct pt_regs *regs);
304 static int ewrk3_close(struct net_device *dev);
305 static struct net_device_stats *ewrk3_get_stats(struct net_device *dev);
306 static void set_multicast_list(struct net_device *dev);
307 static int ewrk3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
308 static struct ethtool_ops ethtool_ops_203;
309 static struct ethtool_ops ethtool_ops;
310
311 /*
312    ** Private functions
313  */
314 static int ewrk3_hw_init(struct net_device *dev, u_long iobase);
315 static void ewrk3_init(struct net_device *dev);
316 static int ewrk3_rx(struct net_device *dev);
317 static int ewrk3_tx(struct net_device *dev);
318 static void ewrk3_timeout(struct net_device *dev);
319
320 static void EthwrkSignature(char *name, char *eeprom_image);
321 static int DevicePresent(u_long iobase);
322 static void SetMulticastFilter(struct net_device *dev);
323 static int EISA_signature(char *name, s32 eisa_id);
324
325 static int Read_EEPROM(u_long iobase, u_char eaddr);
326 static int Write_EEPROM(short data, u_long iobase, u_char eaddr);
327 static u_char get_hw_addr(struct net_device *dev, u_char * eeprom_image, char chipType);
328
329 static int ewrk3_probe1(struct net_device *dev, u_long iobase, int irq);
330 static int isa_probe(struct net_device *dev, u_long iobase);
331 static int eisa_probe(struct net_device *dev, u_long iobase);
332
333 static u_char irq[MAX_NUM_EWRK3S+1] = {5, 0, 10, 3, 11, 9, 15, 12};
334
335 static char name[EWRK3_STRLEN + 1];
336 static int num_ewrks3s;
337
338 /*
339    ** Miscellaneous defines...
340  */
341 #define INIT_EWRK3 {\
342     outb(EEPROM_INIT, EWRK3_IOPR);\
343     mdelay(1);\
344 }
345
346 #ifndef MODULE
347 struct net_device * __init ewrk3_probe(int unit)
348 {
349         struct net_device *dev = alloc_etherdev(sizeof(struct ewrk3_private));
350         int err;
351
352         if (!dev)
353                 return ERR_PTR(-ENOMEM);
354
355         if (unit >= 0) {
356                 sprintf(dev->name, "eth%d", unit);
357                 netdev_boot_setup_check(dev);
358         }
359         SET_MODULE_OWNER(dev);
360
361         err = ewrk3_probe1(dev, dev->base_addr, dev->irq);
362         if (err) 
363                 goto out;
364         return dev;
365 out:
366         free_netdev(dev);
367         return ERR_PTR(err);
368         
369 }
370 #endif
371
372 static int __init ewrk3_probe1(struct net_device *dev, u_long iobase, int irq)
373 {
374         int err;
375
376         dev->base_addr = iobase;
377         dev->irq = irq;
378
379         /* Address PROM pattern */
380         err = isa_probe(dev, iobase);
381         if (err != 0) 
382                 err = eisa_probe(dev, iobase);
383
384         if (err)
385                 return err;
386
387         err = register_netdev(dev);
388         if (err)
389                 release_region(dev->base_addr, EWRK3_TOTAL_SIZE);
390
391         return err;
392 }
393
394 static int __init 
395 ewrk3_hw_init(struct net_device *dev, u_long iobase)
396 {
397         struct ewrk3_private *lp;
398         int i, status = 0;
399         u_long mem_start, shmem_length;
400         u_char cr, cmr, icr, nicsr, lemac, hard_strapped = 0;
401         u_char eeprom_image[EEPROM_MAX], chksum, eisa_cr = 0;
402
403         /*
404         ** Stop the EWRK3. Enable the DBR ROM. Disable interrupts and remote boot.
405         ** This also disables the EISA_ENABLE bit in the EISA Control Register.
406          */
407         if (iobase > 0x400)
408                 eisa_cr = inb(EISA_CR);
409         INIT_EWRK3;
410
411         nicsr = inb(EWRK3_CSR);
412
413         icr = inb(EWRK3_ICR);
414         icr &= 0x70;
415         outb(icr, EWRK3_ICR);   /* Disable all the IRQs */
416
417         if (nicsr == (CSR_TXD | CSR_RXD))
418                 return -ENXIO;
419
420
421         /* Check that the EEPROM is alive and well and not living on Pluto... */
422         for (chksum = 0, i = 0; i < EEPROM_MAX; i += 2) {
423                 union {
424                         short val;
425                         char c[2];
426                 } tmp;
427
428                 tmp.val = (short) Read_EEPROM(iobase, (i >> 1));
429                 eeprom_image[i] = tmp.c[0];
430                 eeprom_image[i + 1] = tmp.c[1];
431                 chksum += eeprom_image[i] + eeprom_image[i + 1];
432         }
433
434         if (chksum != 0) {      /* Bad EEPROM Data! */
435                 printk("%s: Device has a bad on-board EEPROM.\n", dev->name);
436                 return -ENXIO;
437         }
438         
439         EthwrkSignature(name, eeprom_image);
440         if (*name == '\0') 
441                 return -ENXIO;
442
443         dev->base_addr = iobase;
444                                 
445         if (iobase > 0x400) {
446                 outb(eisa_cr, EISA_CR);         /* Rewrite the EISA CR */
447         }
448         lemac = eeprom_image[EEPROM_CHIPVER];
449         cmr = inb(EWRK3_CMR);
450         
451         if (((lemac == LeMAC) && ((cmr & CMR_NO_EEPROM) != CMR_NO_EEPROM)) ||
452             ((lemac == LeMAC2) && !(cmr & CMR_HS))) {
453                 printk("%s: %s at %#4lx", dev->name, name, iobase);
454                 hard_strapped = 1;
455         } else if ((iobase & 0x0fff) == EWRK3_EISA_IO_PORTS) {
456                 /* EISA slot address */
457                 printk("%s: %s at %#4lx (EISA slot %ld)",
458                        dev->name, name, iobase, ((iobase >> 12) & 0x0f));
459         } else {        /* ISA port address */
460                 printk("%s: %s at %#4lx", dev->name, name, iobase);
461         }
462
463         printk(", h/w address ");
464         if (lemac != LeMAC2)
465                 DevicePresent(iobase);  /* need after EWRK3_INIT */
466         status = get_hw_addr(dev, eeprom_image, lemac);
467         for (i = 0; i < ETH_ALEN - 1; i++) {    /* get the ethernet addr. */
468                 printk("%2.2x:", dev->dev_addr[i]);
469         }
470         printk("%2.2x,\n", dev->dev_addr[i]);
471         
472         if (status) {
473                 printk("      which has an EEPROM CRC error.\n");
474                 return -ENXIO;
475         }
476
477         if (lemac == LeMAC2) {  /* Special LeMAC2 CMR things */
478                 cmr &= ~(CMR_RA | CMR_WB | CMR_LINK | CMR_POLARITY | CMR_0WS);
479                 if (eeprom_image[EEPROM_MISC0] & READ_AHEAD)
480                         cmr |= CMR_RA;
481                 if (eeprom_image[EEPROM_MISC0] & WRITE_BEHIND)
482                         cmr |= CMR_WB;
483                 if (eeprom_image[EEPROM_NETMAN0] & NETMAN_POL)
484                         cmr |= CMR_POLARITY;
485                 if (eeprom_image[EEPROM_NETMAN0] & NETMAN_LINK)
486                         cmr |= CMR_LINK;
487                 if (eeprom_image[EEPROM_MISC0] & _0WS_ENA)
488                         cmr |= CMR_0WS;
489         }
490         if (eeprom_image[EEPROM_SETUP] & SETUP_DRAM)
491                 cmr |= CMR_DRAM;
492         outb(cmr, EWRK3_CMR);
493         
494         cr = inb(EWRK3_CR);     /* Set up the Control Register */
495         cr |= eeprom_image[EEPROM_SETUP] & SETUP_APD;
496         if (cr & SETUP_APD)
497                 cr |= eeprom_image[EEPROM_SETUP] & SETUP_PS;
498         cr |= eeprom_image[EEPROM_MISC0] & FAST_BUS;
499         cr |= eeprom_image[EEPROM_MISC0] & ENA_16;
500         outb(cr, EWRK3_CR);
501
502         /*
503         ** Determine the base address and window length for the EWRK3
504         ** RAM from the memory base register.
505         */
506         mem_start = inb(EWRK3_MBR);
507         shmem_length = 0;
508         if (mem_start != 0) {
509                 if ((mem_start >= 0x0a) && (mem_start <= 0x0f)) {
510                         mem_start *= SHMEM_64K;
511                         shmem_length = SHMEM_64K;
512                 } else if ((mem_start >= 0x14) && (mem_start <= 0x1f)) {
513                         mem_start *= SHMEM_32K;
514                         shmem_length = SHMEM_32K;
515                 } else if ((mem_start >= 0x40) && (mem_start <= 0xff)) {
516                         mem_start = mem_start * SHMEM_2K + 0x80000;
517                         shmem_length = SHMEM_2K;
518                 } else {
519                         return -ENXIO;
520                 }
521         }
522         /*
523         ** See the top of this source code for comments about
524         ** uncommenting this line.
525         */
526 /*          FORCE_2K_MODE; */
527         
528         if (hard_strapped) {
529                 printk("      is hard strapped.\n");
530         } else if (mem_start) {
531                 printk("      has a %dk RAM window", (int) (shmem_length >> 10));
532                 printk(" at 0x%.5lx", mem_start);
533         } else {
534                 printk("      is in I/O only mode");
535         }
536
537         lp = netdev_priv(dev);
538         lp->shmem_base = mem_start;
539         lp->shmem_length = shmem_length;
540         lp->lemac = lemac;
541         lp->hard_strapped = hard_strapped;
542         lp->led_mask = CR_LED;
543         spin_lock_init(&lp->hw_lock);
544         
545         lp->mPage = 64;
546         if (cmr & CMR_DRAM)
547                 lp->mPage <<= 1;        /* 2 DRAMS on module */
548         
549         sprintf(lp->adapter_name, "%s (%s)", name, dev->name);
550         
551         lp->irq_mask = ICR_TNEM | ICR_TXDM | ICR_RNEM | ICR_RXDM;
552         
553         if (!hard_strapped) {
554                 /*
555                 ** Enable EWRK3 board interrupts for autoprobing
556                 */
557                 icr |= ICR_IE;  /* Enable interrupts */
558                 outb(icr, EWRK3_ICR);
559                 
560                 /* The DMA channel may be passed in on this parameter. */
561                 dev->dma = 0;
562                 
563                 /* To auto-IRQ we enable the initialization-done and DMA err,
564                    interrupts. For now we will always get a DMA error. */
565                 if (dev->irq < 2) {
566 #ifndef MODULE
567                         u_char irqnum;
568                         unsigned long irq_mask;
569                         
570
571                         irq_mask = probe_irq_on();
572                         
573                         /*
574                         ** Trigger a TNE interrupt.
575                         */
576                         icr |= ICR_TNEM;
577                         outb(1, EWRK3_TDQ);     /* Write to the TX done queue */
578                         outb(icr, EWRK3_ICR);   /* Unmask the TXD interrupt */
579                         
580                         irqnum = irq[((icr & IRQ_SEL) >> 4)];
581                         
582                         mdelay(20);
583                         dev->irq = probe_irq_off(irq_mask);
584                         if ((dev->irq) && (irqnum == dev->irq)) {
585                                 printk(" and uses IRQ%d.\n", dev->irq);
586                         } else {
587                                 if (!dev->irq) {
588                                         printk(" and failed to detect IRQ line.\n");
589                                 } else if ((irqnum == 1) && (lemac == LeMAC2)) {
590                                         printk(" and an illegal IRQ line detected.\n");
591                                 } else {
592                                         printk(", but incorrect IRQ line detected.\n");
593                                 }
594                                 return -ENXIO;
595                         }
596
597                         DISABLE_IRQs;   /* Mask all interrupts */
598
599 #endif                          /* MODULE */
600                 } else {
601                         printk(" and requires IRQ%d.\n", dev->irq);
602                 }
603         }
604
605         if (ewrk3_debug > 1) {
606                 printk(version);
607         }
608         /* The EWRK3-specific entries in the device structure. */
609         dev->open = ewrk3_open;
610         dev->hard_start_xmit = ewrk3_queue_pkt;
611         dev->stop = ewrk3_close;
612         dev->get_stats = ewrk3_get_stats;
613         dev->set_multicast_list = set_multicast_list;
614         dev->do_ioctl = ewrk3_ioctl;
615         if (lp->adapter_name[4] == '3')
616                 SET_ETHTOOL_OPS(dev, &ethtool_ops_203);
617         else
618                 SET_ETHTOOL_OPS(dev, &ethtool_ops);
619         dev->tx_timeout = ewrk3_timeout;
620         dev->watchdog_timeo = QUEUE_PKT_TIMEOUT;
621         
622         dev->mem_start = 0;
623
624         return 0;
625 }
626 \f
627
628 static int ewrk3_open(struct net_device *dev)
629 {
630         struct ewrk3_private *lp = netdev_priv(dev);
631         u_long iobase = dev->base_addr;
632         int i, status = 0;
633         u_char icr, csr;
634
635         /*
636            ** Stop the TX and RX...
637          */
638         STOP_EWRK3;
639
640         if (!lp->hard_strapped) {
641                 if (request_irq(dev->irq, (void *) ewrk3_interrupt, 0, "ewrk3", dev)) {
642                         printk("ewrk3_open(): Requested IRQ%d is busy\n", dev->irq);
643                         status = -EAGAIN;
644                 } else {
645
646                         /*
647                            ** Re-initialize the EWRK3...
648                          */
649                         ewrk3_init(dev);
650
651                         if (ewrk3_debug > 1) {
652                                 printk("%s: ewrk3 open with irq %d\n", dev->name, dev->irq);
653                                 printk("  physical address: ");
654                                 for (i = 0; i < 5; i++) {
655                                         printk("%2.2x:", (u_char) dev->dev_addr[i]);
656                                 }
657                                 printk("%2.2x\n", (u_char) dev->dev_addr[i]);
658                                 if (lp->shmem_length == 0) {
659                                         printk("  no shared memory, I/O only mode\n");
660                                 } else {
661                                         printk("  start of shared memory: 0x%08lx\n", lp->shmem_base);
662                                         printk("  window length: 0x%04lx\n", lp->shmem_length);
663                                 }
664                                 printk("  # of DRAMS: %d\n", ((inb(EWRK3_CMR) & 0x02) ? 2 : 1));
665                                 printk("  csr:  0x%02x\n", inb(EWRK3_CSR));
666                                 printk("  cr:   0x%02x\n", inb(EWRK3_CR));
667                                 printk("  icr:  0x%02x\n", inb(EWRK3_ICR));
668                                 printk("  cmr:  0x%02x\n", inb(EWRK3_CMR));
669                                 printk("  fmqc: 0x%02x\n", inb(EWRK3_FMQC));
670                         }
671                         netif_start_queue(dev);
672                         /*
673                            ** Unmask EWRK3 board interrupts
674                          */
675                         icr = inb(EWRK3_ICR);
676                         ENABLE_IRQs;
677
678                 }
679         } else {
680                 printk(KERN_ERR "%s: ewrk3 available for hard strapped set up only.\n", dev->name);
681                 printk(KERN_ERR "      Run the 'ewrk3setup' utility or remove the hard straps.\n");
682                 return -EINVAL;
683         }
684
685         return status;
686 }
687
688 /*
689    ** Initialize the EtherWORKS 3 operating conditions
690  */
691 static void ewrk3_init(struct net_device *dev)
692 {
693         struct ewrk3_private *lp = netdev_priv(dev);
694         u_char csr, page;
695         u_long iobase = dev->base_addr;
696         int i;
697
698         /*
699            ** Enable any multicasts
700          */
701         set_multicast_list(dev);
702
703         /*
704         ** Set hardware MAC address. Address is initialized from the EEPROM
705         ** during startup but may have since been changed by the user.
706         */
707         for (i=0; i<ETH_ALEN; i++)
708                 outb(dev->dev_addr[i], EWRK3_PAR0 + i);
709
710         /*
711            ** Clean out any remaining entries in all the queues here
712          */
713         while (inb(EWRK3_TQ));
714         while (inb(EWRK3_TDQ));
715         while (inb(EWRK3_RQ));
716         while (inb(EWRK3_FMQ));
717
718         /*
719            ** Write a clean free memory queue
720          */
721         for (page = 1; page < lp->mPage; page++) {      /* Write the free page numbers */
722                 outb(page, EWRK3_FMQ);  /* to the Free Memory Queue */
723         }
724
725         START_EWRK3;            /* Enable the TX and/or RX */
726 }
727
728 /*
729  *  Transmit timeout
730  */
731  
732 static void ewrk3_timeout(struct net_device *dev)
733 {
734         struct ewrk3_private *lp = netdev_priv(dev);
735         u_char icr, csr;
736         u_long iobase = dev->base_addr;
737         
738         if (!lp->hard_strapped) 
739         {
740                 printk(KERN_WARNING"%s: transmit timed/locked out, status %04x, resetting.\n",
741                        dev->name, inb(EWRK3_CSR));
742
743                 /*
744                    ** Mask all board interrupts
745                  */
746                 DISABLE_IRQs;
747
748                 /*
749                    ** Stop the TX and RX...
750                  */
751                 STOP_EWRK3;
752
753                 ewrk3_init(dev);
754
755                 /*
756                    ** Unmask EWRK3 board interrupts
757                  */
758                 ENABLE_IRQs;
759
760                 dev->trans_start = jiffies;
761                 netif_wake_queue(dev);
762         }
763 }
764
765 /*
766    ** Writes a socket buffer to the free page queue
767  */
768 static int ewrk3_queue_pkt (struct sk_buff *skb, struct net_device *dev)
769 {
770         struct ewrk3_private *lp = netdev_priv(dev);
771         u_long iobase = dev->base_addr;
772         u_long buf = 0;
773         u_char icr;
774         u_char page;
775
776         spin_lock_irq (&lp->hw_lock);
777         DISABLE_IRQs;
778
779         /* if no resources available, exit, request packet be queued */
780         if (inb (EWRK3_FMQC) == 0) {
781                 printk (KERN_WARNING "%s: ewrk3_queue_pkt(): No free resources...\n",
782                         dev->name);
783                 printk (KERN_WARNING "%s: ewrk3_queue_pkt(): CSR: %02x ICR: %02x FMQC: %02x\n",
784                         dev->name, inb (EWRK3_CSR), inb (EWRK3_ICR),
785                         inb (EWRK3_FMQC));
786                 goto err_out;
787         }
788
789         /*
790          ** Get a free page from the FMQ
791          */
792         if ((page = inb (EWRK3_FMQ)) >= lp->mPage) {
793                 printk ("ewrk3_queue_pkt(): Invalid free memory page (%d).\n",
794                      (u_char) page);
795                 goto err_out;
796         }
797
798
799         /*
800          ** Set up shared memory window and pointer into the window
801          */
802         if (lp->shmem_length == IO_ONLY) {
803                 outb (page, EWRK3_IOPR);
804         } else if (lp->shmem_length == SHMEM_2K) {
805                 buf = lp->shmem_base;
806                 outb (page, EWRK3_MPR);
807         } else if (lp->shmem_length == SHMEM_32K) {
808                 buf = ((((short) page << 11) & 0x7800) + lp->shmem_base);
809                 outb ((page >> 4), EWRK3_MPR);
810         } else if (lp->shmem_length == SHMEM_64K) {
811                 buf = ((((short) page << 11) & 0xf800) + lp->shmem_base);
812                 outb ((page >> 5), EWRK3_MPR);
813         } else {
814                 printk (KERN_ERR "%s: Oops - your private data area is hosed!\n",
815                         dev->name);
816                 BUG ();
817         }
818
819         /*
820          ** Set up the buffer control structures and copy the data from
821          ** the socket buffer to the shared memory .
822          */
823         if (lp->shmem_length == IO_ONLY) {
824                 int i;
825                 u_char *p = skb->data;
826                 outb ((char) (TCR_QMODE | TCR_PAD | TCR_IFC), EWRK3_DATA);
827                 outb ((char) (skb->len & 0xff), EWRK3_DATA);
828                 outb ((char) ((skb->len >> 8) & 0xff), EWRK3_DATA);
829                 outb ((char) 0x04, EWRK3_DATA);
830                 for (i = 0; i < skb->len; i++) {
831                         outb (*p++, EWRK3_DATA);
832                 }
833                 outb (page, EWRK3_TQ);  /* Start sending pkt */
834         } else {
835                 isa_writeb ((char) (TCR_QMODE | TCR_PAD | TCR_IFC), buf);       /* ctrl byte */
836                 buf += 1;
837                 isa_writeb ((char) (skb->len & 0xff), buf);     /* length (16 bit xfer) */
838                 buf += 1;
839                 if (lp->txc) {
840                         isa_writeb ((char)
841                                     (((skb->len >> 8) & 0xff) | XCT), buf);
842                         buf += 1;
843                         isa_writeb (0x04, buf); /* index byte */
844                         buf += 1;
845                         isa_writeb (0x00, (buf + skb->len));    /* Write the XCT flag */
846                         isa_memcpy_toio (buf, skb->data, PRELOAD);      /* Write PRELOAD bytes */
847                         outb (page, EWRK3_TQ);  /* Start sending pkt */
848                         isa_memcpy_toio (buf + PRELOAD,
849                                          skb->data + PRELOAD,
850                                          skb->len - PRELOAD);
851                         isa_writeb (0xff, (buf + skb->len));    /* Write the XCT flag */
852                 } else {
853                         isa_writeb ((char)
854                                     ((skb->len >> 8) & 0xff), buf);
855                         buf += 1;
856                         isa_writeb (0x04, buf); /* index byte */
857                         buf += 1;
858                         isa_memcpy_toio (buf, skb->data, skb->len);     /* Write data bytes */
859                         outb (page, EWRK3_TQ);  /* Start sending pkt */
860                 }
861         }
862
863         ENABLE_IRQs;
864         spin_unlock_irq (&lp->hw_lock);
865
866         lp->stats.tx_bytes += skb->len;
867         dev->trans_start = jiffies;
868         dev_kfree_skb (skb);
869
870         /* Check for free resources: stop Tx queue if there are none */
871         if (inb (EWRK3_FMQC) == 0)
872                 netif_stop_queue (dev);
873
874         return 0;
875
876 err_out:
877         ENABLE_IRQs;
878         spin_unlock_irq (&lp->hw_lock);
879         return 1;
880 }
881
882 /*
883    ** The EWRK3 interrupt handler.
884  */
885 static irqreturn_t ewrk3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
886 {
887         struct net_device *dev = dev_id;
888         struct ewrk3_private *lp;
889         u_long iobase;
890         u_char icr, cr, csr;
891
892         lp = netdev_priv(dev);
893         iobase = dev->base_addr;
894
895         /* get the interrupt information */
896         csr = inb(EWRK3_CSR);
897
898         /*
899          ** Mask the EWRK3 board interrupts and turn on the LED
900          */
901         spin_lock(&lp->hw_lock);
902         DISABLE_IRQs;
903
904         cr = inb(EWRK3_CR);
905         cr |= lp->led_mask;
906         outb(cr, EWRK3_CR);
907
908         if (csr & CSR_RNE)      /* Rx interrupt (packet[s] arrived) */
909                 ewrk3_rx(dev);
910
911         if (csr & CSR_TNE)      /* Tx interrupt (packet sent) */
912                 ewrk3_tx(dev);
913
914         /*
915          ** Now deal with the TX/RX disable flags. These are set when there
916          ** are no more resources. If resources free up then enable these
917          ** interrupts, otherwise mask them - failure to do this will result
918          ** in the system hanging in an interrupt loop.
919          */
920         if (inb(EWRK3_FMQC)) {  /* any resources available? */
921                 lp->irq_mask |= ICR_TXDM | ICR_RXDM;    /* enable the interrupt source */
922                 csr &= ~(CSR_TXD | CSR_RXD);    /* ensure restart of a stalled TX or RX */
923                 outb(csr, EWRK3_CSR);
924                 netif_wake_queue(dev);
925         } else {
926                 lp->irq_mask &= ~(ICR_TXDM | ICR_RXDM);         /* disable the interrupt source */
927         }
928
929         /* Unmask the EWRK3 board interrupts and turn off the LED */
930         cr &= ~(lp->led_mask);
931         outb(cr, EWRK3_CR);
932         ENABLE_IRQs;
933         spin_unlock(&lp->hw_lock);
934         return IRQ_HANDLED;
935 }
936
937 /* Called with lp->hw_lock held */
938 static int ewrk3_rx(struct net_device *dev)
939 {
940         struct ewrk3_private *lp = netdev_priv(dev);
941         u_long iobase = dev->base_addr;
942         int i, status = 0;
943         u_char page;
944         u_long buf = 0;
945
946         while (inb(EWRK3_RQC) && !status) {     /* Whilst there's incoming data */
947                 if ((page = inb(EWRK3_RQ)) < lp->mPage) {       /* Get next entry's buffer page */
948                         /*
949                            ** Set up shared memory window and pointer into the window
950                          */
951                         if (lp->shmem_length == IO_ONLY) {
952                                 outb(page, EWRK3_IOPR);
953                         } else if (lp->shmem_length == SHMEM_2K) {
954                                 buf = lp->shmem_base;
955                                 outb(page, EWRK3_MPR);
956                         } else if (lp->shmem_length == SHMEM_32K) {
957                                 buf = ((((short) page << 11) & 0x7800) + lp->shmem_base);
958                                 outb((page >> 4), EWRK3_MPR);
959                         } else if (lp->shmem_length == SHMEM_64K) {
960                                 buf = ((((short) page << 11) & 0xf800) + lp->shmem_base);
961                                 outb((page >> 5), EWRK3_MPR);
962                         } else {
963                                 status = -1;
964                                 printk("%s: Oops - your private data area is hosed!\n", dev->name);
965                         }
966
967                         if (!status) {
968                                 char rx_status;
969                                 int pkt_len;
970
971                                 if (lp->shmem_length == IO_ONLY) {
972                                         rx_status = inb(EWRK3_DATA);
973                                         pkt_len = inb(EWRK3_DATA);
974                                         pkt_len |= ((u_short) inb(EWRK3_DATA) << 8);
975                                 } else {
976                                         rx_status = isa_readb(buf);
977                                         buf += 1;
978                                         pkt_len = isa_readw(buf);
979                                         buf += 3;
980                                 }
981
982                                 if (!(rx_status & R_ROK)) {     /* There was an error. */
983                                         lp->stats.rx_errors++;  /* Update the error stats. */
984                                         if (rx_status & R_DBE)
985                                                 lp->stats.rx_frame_errors++;
986                                         if (rx_status & R_CRC)
987                                                 lp->stats.rx_crc_errors++;
988                                         if (rx_status & R_PLL)
989                                                 lp->stats.rx_fifo_errors++;
990                                 } else {
991                                         struct sk_buff *skb;
992
993                                         if ((skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
994                                                 unsigned char *p;
995                                                 skb->dev = dev;
996                                                 skb_reserve(skb, 2);    /* Align to 16 bytes */
997                                                 p = skb_put(skb, pkt_len);
998
999                                                 if (lp->shmem_length == IO_ONLY) {
1000                                                         *p = inb(EWRK3_DATA);   /* dummy read */
1001                                                         for (i = 0; i < pkt_len; i++) {
1002                                                                 *p++ = inb(EWRK3_DATA);
1003                                                         }
1004                                                 } else {
1005                                                         isa_memcpy_fromio(p, buf, pkt_len);
1006                                                 }
1007
1008                                                 for (i = 1; i < EWRK3_PKT_STAT_SZ - 1; i++) {
1009                                                         if (pkt_len < i * EWRK3_PKT_BIN_SZ) {
1010                                                                 lp->pktStats.bins[i]++;
1011                                                                 i = EWRK3_PKT_STAT_SZ;
1012                                                         }
1013                                                 }
1014                                                 p = skb->data;  /* Look at the dest addr */
1015                                                 if (p[0] & 0x01) {      /* Multicast/Broadcast */
1016                                                         if ((*(s16 *) & p[0] == -1) && (*(s16 *) & p[2] == -1) && (*(s16 *) & p[4] == -1)) {
1017                                                                 lp->pktStats.broadcast++;
1018                                                         } else {
1019                                                                 lp->pktStats.multicast++;
1020                                                         }
1021                                                 } else if ((*(s16 *) & p[0] == *(s16 *) & dev->dev_addr[0]) &&
1022                                                            (*(s16 *) & p[2] == *(s16 *) & dev->dev_addr[2]) &&
1023                                                            (*(s16 *) & p[4] == *(s16 *) & dev->dev_addr[4])) {
1024                                                         lp->pktStats.unicast++;
1025                                                 }
1026                                                 lp->pktStats.bins[0]++;         /* Duplicates stats.rx_packets */
1027                                                 if (lp->pktStats.bins[0] == 0) {        /* Reset counters */
1028                                                         memset(&lp->pktStats, 0, sizeof(lp->pktStats));
1029                                                 }
1030                                                 /*
1031                                                    ** Notify the upper protocol layers that there is another
1032                                                    ** packet to handle
1033                                                  */
1034                                                 skb->protocol = eth_type_trans(skb, dev);
1035                                                 netif_rx(skb);
1036
1037                                                 /*
1038                                                    ** Update stats
1039                                                  */
1040                                                 dev->last_rx = jiffies;
1041                                                 lp->stats.rx_packets++;
1042                                                 lp->stats.rx_bytes += pkt_len;
1043                                         } else {
1044                                                 printk("%s: Insufficient memory; nuking packet.\n", dev->name);
1045                                                 lp->stats.rx_dropped++;         /* Really, deferred. */
1046                                                 break;
1047                                         }
1048                                 }
1049                         }
1050                         /*
1051                            ** Return the received buffer to the free memory queue
1052                          */
1053                         outb(page, EWRK3_FMQ);
1054                 } else {
1055                         printk("ewrk3_rx(): Illegal page number, page %d\n", page);
1056                         printk("ewrk3_rx(): CSR: %02x ICR: %02x FMQC: %02x\n", inb(EWRK3_CSR), inb(EWRK3_ICR), inb(EWRK3_FMQC));
1057                 }
1058         }
1059         return status;
1060 }
1061
1062 /*
1063 ** Buffer sent - check for TX buffer errors.
1064 ** Called with lp->hw_lock held
1065 */
1066 static int ewrk3_tx(struct net_device *dev)
1067 {
1068         struct ewrk3_private *lp = netdev_priv(dev);
1069         u_long iobase = dev->base_addr;
1070         u_char tx_status;
1071
1072         while ((tx_status = inb(EWRK3_TDQ)) > 0) {      /* Whilst there's old buffers */
1073                 if (tx_status & T_VSTS) {       /* The status is valid */
1074                         if (tx_status & T_TXE) {
1075                                 lp->stats.tx_errors++;
1076                                 if (tx_status & T_NCL)
1077                                         lp->stats.tx_carrier_errors++;
1078                                 if (tx_status & T_LCL)
1079                                         lp->stats.tx_window_errors++;
1080                                 if (tx_status & T_CTU) {
1081                                         if ((tx_status & T_COLL) ^ T_XUR) {
1082                                                 lp->pktStats.tx_underruns++;
1083                                         } else {
1084                                                 lp->pktStats.excessive_underruns++;
1085                                         }
1086                                 } else if (tx_status & T_COLL) {
1087                                         if ((tx_status & T_COLL) ^ T_XCOLL) {
1088                                                 lp->stats.collisions++;
1089                                         } else {
1090                                                 lp->pktStats.excessive_collisions++;
1091                                         }
1092                                 }
1093                         } else {
1094                                 lp->stats.tx_packets++;
1095                         }
1096                 }
1097         }
1098
1099         return 0;
1100 }
1101
1102 static int ewrk3_close(struct net_device *dev)
1103 {
1104         struct ewrk3_private *lp = netdev_priv(dev);
1105         u_long iobase = dev->base_addr;
1106         u_char icr, csr;
1107
1108         netif_stop_queue(dev);
1109         
1110         if (ewrk3_debug > 1) {
1111                 printk("%s: Shutting down ethercard, status was %2.2x.\n",
1112                        dev->name, inb(EWRK3_CSR));
1113         }
1114         /*
1115            ** We stop the EWRK3 here... mask interrupts and stop TX & RX
1116          */
1117         DISABLE_IRQs;
1118
1119         STOP_EWRK3;
1120
1121         /*
1122            ** Clean out the TX and RX queues here (note that one entry
1123            ** may get added to either the TXD or RX queues if the TX or RX
1124            ** just starts processing a packet before the STOP_EWRK3 command
1125            ** is received. This will be flushed in the ewrk3_open() call).
1126          */
1127         while (inb(EWRK3_TQ));
1128         while (inb(EWRK3_TDQ));
1129         while (inb(EWRK3_RQ));
1130
1131         if (!lp->hard_strapped) {
1132                 free_irq(dev->irq, dev);
1133         }
1134         return 0;
1135 }
1136
1137 static struct net_device_stats *ewrk3_get_stats(struct net_device *dev)
1138 {
1139         struct ewrk3_private *lp = netdev_priv(dev);
1140
1141         /* Null body since there is no framing error counter */
1142         return &lp->stats;
1143 }
1144
1145 /*
1146    ** Set or clear the multicast filter for this adapter.
1147  */
1148 static void set_multicast_list(struct net_device *dev)
1149 {
1150         struct ewrk3_private *lp = netdev_priv(dev);
1151         u_long iobase = dev->base_addr;
1152         u_char csr;
1153
1154         csr = inb(EWRK3_CSR);
1155
1156         if (lp->shmem_length == IO_ONLY) {
1157                 lp->mctbl = (char *) PAGE0_HTE;
1158         } else {
1159                 lp->mctbl = (char *) (lp->shmem_base + PAGE0_HTE);
1160         }
1161
1162         csr &= ~(CSR_PME | CSR_MCE);
1163         if (dev->flags & IFF_PROMISC) {         /* set promiscuous mode */
1164                 csr |= CSR_PME;
1165                 outb(csr, EWRK3_CSR);
1166         } else {
1167                 SetMulticastFilter(dev);
1168                 csr |= CSR_MCE;
1169                 outb(csr, EWRK3_CSR);
1170         }
1171 }
1172
1173 /*
1174    ** Calculate the hash code and update the logical address filter
1175    ** from a list of ethernet multicast addresses.
1176    ** Little endian crc one liner from Matt Thomas, DEC.
1177    **
1178    ** Note that when clearing the table, the broadcast bit must remain asserted
1179    ** to receive broadcast messages.
1180  */
1181 static void SetMulticastFilter(struct net_device *dev)
1182 {
1183         struct ewrk3_private *lp = netdev_priv(dev);
1184         struct dev_mc_list *dmi = dev->mc_list;
1185         u_long iobase = dev->base_addr;
1186         int i;
1187         char *addrs, bit, byte;
1188         short *p = (short *) lp->mctbl;
1189         u16 hashcode;
1190         u32 crc;
1191
1192         spin_lock_irq(&lp->hw_lock);
1193
1194         if (lp->shmem_length == IO_ONLY) {
1195                 outb(0, EWRK3_IOPR);
1196                 outw(EEPROM_OFFSET(lp->mctbl), EWRK3_PIR1);
1197         } else {
1198                 outb(0, EWRK3_MPR);
1199         }
1200
1201         if (dev->flags & IFF_ALLMULTI) {
1202                 for (i = 0; i < (HASH_TABLE_LEN >> 3); i++) {
1203                         if (lp->shmem_length == IO_ONLY) {
1204                                 outb(0xff, EWRK3_DATA);
1205                         } else {        /* memset didn't work here */
1206                                 isa_writew(0xffff, (int) p);
1207                                 p++;
1208                                 i++;
1209                         }
1210                 }
1211         } else {
1212                 /* Clear table except for broadcast bit */
1213                 if (lp->shmem_length == IO_ONLY) {
1214                         for (i = 0; i < (HASH_TABLE_LEN >> 4) - 1; i++) {
1215                                 outb(0x00, EWRK3_DATA);
1216                         }
1217                         outb(0x80, EWRK3_DATA);
1218                         i++;    /* insert the broadcast bit */
1219                         for (; i < (HASH_TABLE_LEN >> 3); i++) {
1220                                 outb(0x00, EWRK3_DATA);
1221                         }
1222                 } else {
1223                         isa_memset_io((int) lp->mctbl, 0, (HASH_TABLE_LEN >> 3));
1224                         isa_writeb(0x80, (int) (lp->mctbl + (HASH_TABLE_LEN >> 4) - 1));
1225                 }
1226
1227                 /* Update table */
1228                 for (i = 0; i < dev->mc_count; i++) {   /* for each address in the list */
1229                         addrs = dmi->dmi_addr;
1230                         dmi = dmi->next;
1231                         if ((*addrs & 0x01) == 1) {     /* multicast address? */
1232                                 crc = ether_crc_le(ETH_ALEN, addrs);
1233                                 hashcode = crc & ((1 << 9) - 1);        /* hashcode is 9 LSb of CRC */
1234
1235                                 byte = hashcode >> 3;   /* bit[3-8] -> byte in filter */
1236                                 bit = 1 << (hashcode & 0x07);   /* bit[0-2] -> bit in byte */
1237
1238                                 if (lp->shmem_length == IO_ONLY) {
1239                                         u_char tmp;
1240
1241                                         outw((short) ((long) lp->mctbl) + byte, EWRK3_PIR1);
1242                                         tmp = inb(EWRK3_DATA);
1243                                         tmp |= bit;
1244                                         outw((short) ((long) lp->mctbl) + byte, EWRK3_PIR1);
1245                                         outb(tmp, EWRK3_DATA);
1246                                 } else {
1247                                         isa_writeb(isa_readb((int)(lp->mctbl + byte)) | bit, (int)(lp->mctbl + byte));
1248                                 }
1249                         }
1250                 }
1251         }
1252
1253         spin_unlock_irq(&lp->hw_lock);
1254 }
1255
1256 /*
1257    ** ISA bus I/O device probe
1258  */
1259 static int __init isa_probe(struct net_device *dev, u_long ioaddr)
1260 {
1261         int i = num_ewrks3s, maxSlots;
1262         int ret = -ENODEV;
1263
1264         u_long iobase;
1265
1266         if (ioaddr >= 0x400)
1267                 goto out;
1268
1269         if (ioaddr == 0) {      /* Autoprobing */
1270                 iobase = EWRK3_IO_BASE;         /* Get the first slot address */
1271                 maxSlots = 24;
1272         } else {                /* Probe a specific location */
1273                 iobase = ioaddr;
1274                 maxSlots = i + 1;
1275         }
1276
1277         for (; (i < maxSlots) && (dev != NULL);
1278              iobase += EWRK3_IOP_INC, i++)
1279         {
1280                 if (request_region(iobase, EWRK3_TOTAL_SIZE, DRV_NAME)) {
1281                         if (DevicePresent(iobase) == 0) {
1282                                 int irq = dev->irq;
1283                                 ret = ewrk3_hw_init(dev, iobase);
1284                                 if (!ret)
1285                                         break;
1286                                 dev->irq = irq;
1287                         }
1288                         release_region(iobase, EWRK3_TOTAL_SIZE);
1289                 }
1290         }
1291  out:
1292
1293         return ret;
1294 }
1295
1296 /*
1297    ** EISA bus I/O device probe. Probe from slot 1 since slot 0 is usually
1298    ** the motherboard.
1299  */
1300 static int __init eisa_probe(struct net_device *dev, u_long ioaddr)
1301 {
1302         int i, maxSlots;
1303         u_long iobase;
1304         int ret = -ENODEV;
1305
1306         if (ioaddr < 0x1000)
1307                 goto out;
1308
1309         if (ioaddr == 0) {      /* Autoprobing */
1310                 iobase = EISA_SLOT_INC;         /* Get the first slot address */
1311                 i = 1;
1312                 maxSlots = MAX_EISA_SLOTS;
1313         } else {                /* Probe a specific location */
1314                 iobase = ioaddr;
1315                 i = (ioaddr >> 12);
1316                 maxSlots = i + 1;
1317         }
1318
1319         for (i = 1; (i < maxSlots) && (dev != NULL); i++, iobase += EISA_SLOT_INC) {
1320                 if (EISA_signature(name, EISA_ID) == 0) {
1321                         if (request_region(iobase, EWRK3_TOTAL_SIZE, DRV_NAME) &&
1322                             DevicePresent(iobase) == 0) {
1323                                 int irq = dev->irq;
1324                                 ret = ewrk3_hw_init(dev, iobase);
1325                                 if (!ret)
1326                                         break;
1327                                 dev->irq = irq;
1328                         }
1329                         release_region(iobase, EWRK3_TOTAL_SIZE);
1330                 }
1331         }
1332
1333  out:
1334         return ret;
1335 }
1336
1337
1338 /*
1339    ** Read the EWRK3 EEPROM using this routine
1340  */
1341 static int Read_EEPROM(u_long iobase, u_char eaddr)
1342 {
1343         int i;
1344
1345         outb((eaddr & 0x3f), EWRK3_PIR1);       /* set up 6 bits of address info */
1346         outb(EEPROM_RD, EWRK3_IOPR);    /* issue read command */
1347         for (i = 0; i < 5000; i++)
1348                 inb(EWRK3_CSR); /* wait 1msec */
1349
1350         return inw(EWRK3_EPROM1);       /* 16 bits data return */
1351 }
1352
1353 /*
1354    ** Write the EWRK3 EEPROM using this routine
1355  */
1356 static int Write_EEPROM(short data, u_long iobase, u_char eaddr)
1357 {
1358         int i;
1359
1360         outb(EEPROM_WR_EN, EWRK3_IOPR);         /* issue write enable command */
1361         for (i = 0; i < 5000; i++)
1362                 inb(EWRK3_CSR); /* wait 1msec */
1363         outw(data, EWRK3_EPROM1);       /* write data to register */
1364         outb((eaddr & 0x3f), EWRK3_PIR1);       /* set up 6 bits of address info */
1365         outb(EEPROM_WR, EWRK3_IOPR);    /* issue write command */
1366         for (i = 0; i < 75000; i++)
1367                 inb(EWRK3_CSR); /* wait 15msec */
1368         outb(EEPROM_WR_DIS, EWRK3_IOPR);        /* issue write disable command */
1369         for (i = 0; i < 5000; i++)
1370                 inb(EWRK3_CSR); /* wait 1msec */
1371
1372         return 0;
1373 }
1374
1375 /*
1376    ** Look for a particular board name in the on-board EEPROM.
1377  */
1378 static void __init EthwrkSignature(char *name, char *eeprom_image)
1379 {
1380         int i;
1381         char *signatures[] = EWRK3_SIGNATURE;
1382
1383         for (i=0; *signatures[i] != '\0'; i++)
1384                 if( !strncmp(eeprom_image+EEPROM_PNAME7, signatures[i], strlen(signatures[i])) )
1385                         break;
1386
1387         if (*signatures[i] != '\0') {
1388                 memcpy(name, eeprom_image+EEPROM_PNAME7, EWRK3_STRLEN);
1389                 name[EWRK3_STRLEN] = '\0';
1390         } else
1391                 name[0] = '\0';
1392
1393         return;
1394 }
1395
1396 /*
1397    ** Look for a special sequence in the Ethernet station address PROM that
1398    ** is common across all EWRK3 products.
1399    **
1400    ** Search the Ethernet address ROM for the signature. Since the ROM address
1401    ** counter can start at an arbitrary point, the search must include the entire
1402    ** probe sequence length plus the (length_of_the_signature - 1).
1403    ** Stop the search IMMEDIATELY after the signature is found so that the
1404    ** PROM address counter is correctly positioned at the start of the
1405    ** ethernet address for later read out.
1406  */
1407
1408 static int __init DevicePresent(u_long iobase)
1409 {
1410         union {
1411                 struct {
1412                         u32 a;
1413                         u32 b;
1414                 } llsig;
1415                 char Sig[sizeof(u32) << 1];
1416         }
1417         dev;
1418         short sigLength;
1419         char data;
1420         int i, j, status = 0;
1421
1422         dev.llsig.a = ETH_PROM_SIG;
1423         dev.llsig.b = ETH_PROM_SIG;
1424         sigLength = sizeof(u32) << 1;
1425
1426         for (i = 0, j = 0; j < sigLength && i < PROBE_LENGTH + sigLength - 1; i++) {
1427                 data = inb(EWRK3_APROM);
1428                 if (dev.Sig[j] == data) {       /* track signature */
1429                         j++;
1430                 } else {        /* lost signature; begin search again */
1431                         if (data == dev.Sig[0]) {
1432                                 j = 1;
1433                         } else {
1434                                 j = 0;
1435                         }
1436                 }
1437         }
1438
1439         if (j != sigLength) {
1440                 status = -ENODEV;       /* search failed */
1441         }
1442         return status;
1443 }
1444
1445 static u_char __init get_hw_addr(struct net_device *dev, u_char * eeprom_image, char chipType)
1446 {
1447         int i, j, k;
1448         u_short chksum;
1449         u_char crc, lfsr, sd, status = 0;
1450         u_long iobase = dev->base_addr;
1451         u16 tmp;
1452
1453         if (chipType == LeMAC2) {
1454                 for (crc = 0x6a, j = 0; j < ETH_ALEN; j++) {
1455                         sd = dev->dev_addr[j] = eeprom_image[EEPROM_PADDR0 + j];
1456                         outb(dev->dev_addr[j], EWRK3_PAR0 + j);
1457                         for (k = 0; k < 8; k++, sd >>= 1) {
1458                                 lfsr = ((((crc & 0x02) >> 1) ^ (crc & 0x01)) ^ (sd & 0x01)) << 7;
1459                                 crc = (crc >> 1) + lfsr;
1460                         }
1461                 }
1462                 if (crc != eeprom_image[EEPROM_PA_CRC])
1463                         status = -1;
1464         } else {
1465                 for (i = 0, k = 0; i < ETH_ALEN;) {
1466                         k <<= 1;
1467                         if (k > 0xffff)
1468                                 k -= 0xffff;
1469
1470                         k += (u_char) (tmp = inb(EWRK3_APROM));
1471                         dev->dev_addr[i] = (u_char) tmp;
1472                         outb(dev->dev_addr[i], EWRK3_PAR0 + i);
1473                         i++;
1474                         k += (u_short) ((tmp = inb(EWRK3_APROM)) << 8);
1475                         dev->dev_addr[i] = (u_char) tmp;
1476                         outb(dev->dev_addr[i], EWRK3_PAR0 + i);
1477                         i++;
1478
1479                         if (k > 0xffff)
1480                                 k -= 0xffff;
1481                 }
1482                 if (k == 0xffff)
1483                         k = 0;
1484                 chksum = inb(EWRK3_APROM);
1485                 chksum |= (inb(EWRK3_APROM) << 8);
1486                 if (k != chksum)
1487                         status = -1;
1488         }
1489
1490         return status;
1491 }
1492
1493 /*
1494    ** Look for a particular board name in the EISA configuration space
1495  */
1496 static int __init EISA_signature(char *name, s32 eisa_id)
1497 {
1498         u_long i;
1499         char *signatures[] = EWRK3_SIGNATURE;
1500         char ManCode[EWRK3_STRLEN];
1501         union {
1502                 s32 ID;
1503                 char Id[4];
1504         } Eisa;
1505         int status = 0;
1506
1507         *name = '\0';
1508         for (i = 0; i < 4; i++) {
1509                 Eisa.Id[i] = inb(eisa_id + i);
1510         }
1511
1512         ManCode[0] = (((Eisa.Id[0] >> 2) & 0x1f) + 0x40);
1513         ManCode[1] = (((Eisa.Id[1] & 0xe0) >> 5) + ((Eisa.Id[0] & 0x03) << 3) + 0x40);
1514         ManCode[2] = (((Eisa.Id[2] >> 4) & 0x0f) + 0x30);
1515         ManCode[3] = ((Eisa.Id[2] & 0x0f) + 0x30);
1516         ManCode[4] = (((Eisa.Id[3] >> 4) & 0x0f) + 0x30);
1517         ManCode[5] = '\0';
1518
1519         for (i = 0; (*signatures[i] != '\0') && (*name == '\0'); i++) {
1520                 if (strstr(ManCode, signatures[i]) != NULL) {
1521                         strcpy(name, ManCode);
1522                         status = 1;
1523                 }
1524         }
1525
1526         return status;          /* return the device name string */
1527 }
1528
1529 static void ewrk3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1530 {
1531         int fwrev = Read_EEPROM(dev->base_addr, EEPROM_REVLVL);
1532
1533         strcpy(info->driver, DRV_NAME);
1534         strcpy(info->version, DRV_VERSION);
1535         sprintf(info->fw_version, "%d", fwrev);
1536         strcpy(info->bus_info, "N/A");
1537         info->eedump_len = EEPROM_MAX;
1538 }
1539
1540 static int ewrk3_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1541 {
1542         struct ewrk3_private *lp = netdev_priv(dev);
1543         unsigned long iobase = dev->base_addr;
1544         u8 cr = inb(EWRK3_CR);
1545
1546         switch (lp->adapter_name[4]) {
1547         case '3': /* DE203 */
1548                 ecmd->supported = SUPPORTED_BNC;
1549                 ecmd->port = PORT_BNC;
1550                 break;
1551
1552         case '4': /* DE204 */
1553                 ecmd->supported = SUPPORTED_TP;
1554                 ecmd->port = PORT_TP;
1555                 break;
1556
1557         case '5': /* DE205 */
1558                 ecmd->supported = SUPPORTED_TP | SUPPORTED_BNC | SUPPORTED_AUI;
1559                 ecmd->autoneg = !(cr & CR_APD);
1560                 /*
1561                 ** Port is only valid if autoneg is disabled
1562                 ** and even then we don't know if AUI is jumpered.
1563                 */
1564                 if (!ecmd->autoneg)
1565                         ecmd->port = (cr & CR_PSEL) ? PORT_BNC : PORT_TP;
1566                 break;
1567         }
1568
1569         ecmd->supported |= SUPPORTED_10baseT_Half;
1570         ecmd->speed = SPEED_10;
1571         ecmd->duplex = DUPLEX_HALF;
1572         return 0;
1573 }
1574
1575 static int ewrk3_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1576 {
1577         struct ewrk3_private *lp = netdev_priv(dev);
1578         unsigned long iobase = dev->base_addr;
1579         unsigned long flags;
1580         u8 cr;
1581
1582         /* DE205 is the only card with anything to set */
1583         if (lp->adapter_name[4] != '5')
1584                 return -EOPNOTSUPP;
1585
1586         /* Sanity-check parameters */
1587         if (ecmd->speed != SPEED_10)
1588                 return -EINVAL;
1589         if (ecmd->port != PORT_TP && ecmd->port != PORT_BNC)
1590                 return -EINVAL; /* AUI is not software-selectable */
1591         if (ecmd->transceiver != XCVR_INTERNAL)
1592                 return -EINVAL;
1593         if (ecmd->duplex != DUPLEX_HALF)
1594                 return -EINVAL;
1595         if (ecmd->phy_address != 0)
1596                 return -EINVAL;
1597
1598         spin_lock_irqsave(&lp->hw_lock, flags);
1599         cr = inb(EWRK3_CR);
1600
1601         /* If Autoneg is set, change to Auto Port mode */
1602         /* Otherwise, disable Auto Port and set port explicitly */
1603         if (ecmd->autoneg) {
1604                 cr &= ~CR_APD;
1605         } else {
1606                 cr |= CR_APD;
1607                 if (ecmd->port == PORT_TP)
1608                         cr &= ~CR_PSEL;         /* Force TP */
1609                 else
1610                         cr |= CR_PSEL;          /* Force BNC */
1611         }
1612
1613         /* Commit the changes */
1614         outb(cr, EWRK3_CR);
1615         spin_unlock_irqrestore(&lp->hw_lock, flags);
1616         return 0;
1617 }
1618
1619 static u32 ewrk3_get_link(struct net_device *dev)
1620 {
1621         unsigned long iobase = dev->base_addr;
1622         u8 cmr = inb(EWRK3_CMR);
1623         /* DE203 has BNC only and link status does not apply */
1624         /* On DE204 this is always valid since TP is the only port. */
1625         /* On DE205 this reflects TP status even if BNC or AUI is selected. */
1626         return !(cmr & CMR_LINK);
1627 }
1628
1629 static int ewrk3_phys_id(struct net_device *dev, u32 data)
1630 {
1631         struct ewrk3_private *lp = netdev_priv(dev);
1632         unsigned long iobase = dev->base_addr;
1633         unsigned long flags;
1634         u8 cr;
1635         int count;
1636
1637         /* Toggle LED 4x per second */
1638         count = data << 2;
1639
1640         spin_lock_irqsave(&lp->hw_lock, flags);
1641
1642         /* Bail if a PHYS_ID is already in progress */
1643         if (lp->led_mask == 0) {
1644                 spin_unlock_irqrestore(&lp->hw_lock, flags);
1645                 return -EBUSY;
1646         }
1647
1648         /* Prevent ISR from twiddling the LED */
1649         lp->led_mask = 0;
1650
1651         while (count--) {
1652                 /* Toggle the LED */
1653                 cr = inb(EWRK3_CR);
1654                 outb(cr ^ CR_LED, EWRK3_CR);
1655
1656                 /* Wait a little while */
1657                 spin_unlock_irqrestore(&lp->hw_lock, flags);
1658                 set_current_state(TASK_UNINTERRUPTIBLE);
1659                 schedule_timeout(HZ>>2);
1660                 spin_lock_irqsave(&lp->hw_lock, flags);
1661
1662                 /* Exit if we got a signal */
1663                 if (signal_pending(current))
1664                         break;
1665         }
1666
1667         lp->led_mask = CR_LED;
1668         cr = inb(EWRK3_CR);
1669         outb(cr & ~CR_LED, EWRK3_CR);
1670         spin_unlock_irqrestore(&lp->hw_lock, flags);
1671         return signal_pending(current) ? -ERESTARTSYS : 0;
1672 }
1673
1674 static struct ethtool_ops ethtool_ops_203 = {
1675         .get_drvinfo = ewrk3_get_drvinfo,
1676         .get_settings = ewrk3_get_settings,
1677         .set_settings = ewrk3_set_settings,
1678         .phys_id = ewrk3_phys_id,
1679 };
1680
1681 static struct ethtool_ops ethtool_ops = {
1682         .get_drvinfo = ewrk3_get_drvinfo,
1683         .get_settings = ewrk3_get_settings,
1684         .set_settings = ewrk3_set_settings,
1685         .get_link = ewrk3_get_link,
1686         .phys_id = ewrk3_phys_id,
1687 };
1688
1689 /*
1690    ** Perform IOCTL call functions here. Some are privileged operations and the
1691    ** effective uid is checked in those cases.
1692  */
1693 static int ewrk3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1694 {
1695         struct ewrk3_private *lp = netdev_priv(dev);
1696         struct ewrk3_ioctl *ioc = (struct ewrk3_ioctl *) &rq->ifr_ifru;
1697         u_long iobase = dev->base_addr;
1698         int i, j, status = 0;
1699         u_char csr;
1700         unsigned long flags;
1701         union ewrk3_addr {
1702                 u_char addr[HASH_TABLE_LEN * ETH_ALEN];
1703                 u_short val[(HASH_TABLE_LEN * ETH_ALEN) >> 1];
1704         };
1705         
1706         union ewrk3_addr *tmp;
1707
1708         /* All we handle are private IOCTLs */
1709         if (cmd != EWRK3IOCTL)
1710                 return -EOPNOTSUPP;
1711
1712         tmp = kmalloc(sizeof(union ewrk3_addr), GFP_KERNEL);
1713         if(tmp==NULL)
1714                 return -ENOMEM;
1715
1716         switch (ioc->cmd) {
1717         case EWRK3_GET_HWADDR:  /* Get the hardware address */
1718                 for (i = 0; i < ETH_ALEN; i++) {
1719                         tmp->addr[i] = dev->dev_addr[i];
1720                 }
1721                 ioc->len = ETH_ALEN;
1722                 if (copy_to_user(ioc->data, tmp->addr, ioc->len))
1723                         status = -EFAULT;
1724                 break;
1725                 
1726         case EWRK3_SET_HWADDR:  /* Set the hardware address */
1727                 if (capable(CAP_NET_ADMIN)) {
1728                         spin_lock_irqsave(&lp->hw_lock, flags);
1729                         csr = inb(EWRK3_CSR);
1730                         csr |= (CSR_TXD | CSR_RXD);
1731                         outb(csr, EWRK3_CSR);   /* Disable the TX and RX */
1732                         spin_unlock_irqrestore(&lp->hw_lock, flags);
1733
1734                         if (copy_from_user(tmp->addr, ioc->data, ETH_ALEN)) {
1735                                 status = -EFAULT;
1736                                 break;
1737                         }
1738                         spin_lock_irqsave(&lp->hw_lock, flags);
1739                         for (i = 0; i < ETH_ALEN; i++) {
1740                                 dev->dev_addr[i] = tmp->addr[i];
1741                                 outb(tmp->addr[i], EWRK3_PAR0 + i);
1742                         }
1743
1744                         csr = inb(EWRK3_CSR);
1745                         csr &= ~(CSR_TXD | CSR_RXD);    /* Enable the TX and RX */
1746                         outb(csr, EWRK3_CSR);
1747                         spin_unlock_irqrestore(&lp->hw_lock, flags);
1748                 } else {
1749                         status = -EPERM;
1750                 }
1751
1752                 break;
1753         case EWRK3_SET_PROM:    /* Set Promiscuous Mode */
1754                 if (capable(CAP_NET_ADMIN)) {
1755                         spin_lock_irqsave(&lp->hw_lock, flags);
1756                         csr = inb(EWRK3_CSR);
1757                         csr |= CSR_PME;
1758                         csr &= ~CSR_MCE;
1759                         outb(csr, EWRK3_CSR);
1760                         spin_unlock_irqrestore(&lp->hw_lock, flags);
1761                 } else {
1762                         status = -EPERM;
1763                 }
1764
1765                 break;
1766         case EWRK3_CLR_PROM:    /* Clear Promiscuous Mode */
1767                 if (capable(CAP_NET_ADMIN)) {
1768                         spin_lock_irqsave(&lp->hw_lock, flags);
1769                         csr = inb(EWRK3_CSR);
1770                         csr &= ~CSR_PME;
1771                         outb(csr, EWRK3_CSR);
1772                         spin_unlock_irqrestore(&lp->hw_lock, flags);
1773                 } else {
1774                         status = -EPERM;
1775                 }
1776
1777                 break;
1778         case EWRK3_GET_MCA:     /* Get the multicast address table */
1779                 spin_lock_irqsave(&lp->hw_lock, flags);
1780                 if (lp->shmem_length == IO_ONLY) {
1781                         outb(0, EWRK3_IOPR);
1782                         outw(PAGE0_HTE, EWRK3_PIR1);
1783                         for (i = 0; i < (HASH_TABLE_LEN >> 3); i++) {
1784                                 tmp->addr[i] = inb(EWRK3_DATA);
1785                         }
1786                 } else {
1787                         outb(0, EWRK3_MPR);
1788                         isa_memcpy_fromio(tmp->addr, lp->shmem_base + PAGE0_HTE, (HASH_TABLE_LEN >> 3));
1789                 }
1790                 spin_unlock_irqrestore(&lp->hw_lock, flags);
1791
1792                 ioc->len = (HASH_TABLE_LEN >> 3);
1793                 if (copy_to_user(ioc->data, tmp->addr, ioc->len))
1794                         status = -EFAULT;
1795
1796                 break;
1797         case EWRK3_SET_MCA:     /* Set a multicast address */
1798                 if (capable(CAP_NET_ADMIN)) {
1799                         if (ioc->len > 1024)
1800                         {
1801                                 status = -EINVAL;
1802                                 break;
1803                         }
1804                         if (copy_from_user(tmp->addr, ioc->data, ETH_ALEN * ioc->len)) {
1805                                 status = -EFAULT;
1806                                 break;
1807                         }
1808                         set_multicast_list(dev);
1809                 } else {
1810                         status = -EPERM;
1811                 }
1812
1813                 break;
1814         case EWRK3_CLR_MCA:     /* Clear all multicast addresses */
1815                 if (capable(CAP_NET_ADMIN)) {
1816                         set_multicast_list(dev);
1817                 } else {
1818                         status = -EPERM;
1819                 }
1820
1821                 break;
1822         case EWRK3_MCA_EN:      /* Enable multicast addressing */
1823                 if (capable(CAP_NET_ADMIN)) {
1824                         spin_lock_irqsave(&lp->hw_lock, flags);
1825                         csr = inb(EWRK3_CSR);
1826                         csr |= CSR_MCE;
1827                         csr &= ~CSR_PME;
1828                         outb(csr, EWRK3_CSR);
1829                         spin_unlock_irqrestore(&lp->hw_lock, flags);
1830                 } else {
1831                         status = -EPERM;
1832                 }
1833
1834                 break;
1835         case EWRK3_GET_STATS: { /* Get the driver statistics */
1836                 struct ewrk3_stats *tmp_stats =
1837                         kmalloc(sizeof(lp->pktStats), GFP_KERNEL);
1838                 if (!tmp_stats) {
1839                         status = -ENOMEM;
1840                         break;
1841                 }
1842
1843                 spin_lock_irqsave(&lp->hw_lock, flags);
1844                 memcpy(tmp_stats, &lp->pktStats, sizeof(lp->pktStats));
1845                 spin_unlock_irqrestore(&lp->hw_lock, flags);
1846
1847                 ioc->len = sizeof(lp->pktStats);
1848                 if (copy_to_user(ioc->data, tmp_stats, sizeof(lp->pktStats)))
1849                         status = -EFAULT;
1850                 kfree(tmp_stats);
1851                 break;
1852         }
1853         case EWRK3_CLR_STATS:   /* Zero out the driver statistics */
1854                 if (capable(CAP_NET_ADMIN)) {
1855                         spin_lock_irqsave(&lp->hw_lock, flags);
1856                         memset(&lp->pktStats, 0, sizeof(lp->pktStats));
1857                         spin_unlock_irqrestore(&lp->hw_lock,flags);
1858                 } else {
1859                         status = -EPERM;
1860                 }
1861
1862                 break;
1863         case EWRK3_GET_CSR:     /* Get the CSR Register contents */
1864                 tmp->addr[0] = inb(EWRK3_CSR);
1865                 ioc->len = 1;
1866                 if (copy_to_user(ioc->data, tmp->addr, ioc->len))
1867                         status = -EFAULT;
1868                 break;
1869         case EWRK3_SET_CSR:     /* Set the CSR Register contents */
1870                 if (capable(CAP_NET_ADMIN)) {
1871                         if (copy_from_user(tmp->addr, ioc->data, 1)) {
1872                                 status = -EFAULT;
1873                                 break;
1874                         }
1875                         outb(tmp->addr[0], EWRK3_CSR);
1876                 } else {
1877                         status = -EPERM;
1878                 }
1879
1880                 break;
1881         case EWRK3_GET_EEPROM:  /* Get the EEPROM contents */
1882                 if (capable(CAP_NET_ADMIN)) {
1883                         for (i = 0; i < (EEPROM_MAX >> 1); i++) {
1884                                 tmp->val[i] = (short) Read_EEPROM(iobase, i);
1885                         }
1886                         i = EEPROM_MAX;
1887                         tmp->addr[i++] = inb(EWRK3_CMR);                /* Config/Management Reg. */
1888                         for (j = 0; j < ETH_ALEN; j++) {
1889                                 tmp->addr[i++] = inb(EWRK3_PAR0 + j);
1890                         }
1891                         ioc->len = EEPROM_MAX + 1 + ETH_ALEN;
1892                         if (copy_to_user(ioc->data, tmp->addr, ioc->len))
1893                                 status = -EFAULT;
1894                 } else {
1895                         status = -EPERM;
1896                 }
1897
1898                 break;
1899         case EWRK3_SET_EEPROM:  /* Set the EEPROM contents */
1900                 if (capable(CAP_NET_ADMIN)) {
1901                         if (copy_from_user(tmp->addr, ioc->data, EEPROM_MAX)) {
1902                                 status = -EFAULT;
1903                                 break;
1904                         }
1905                         for (i = 0; i < (EEPROM_MAX >> 1); i++) {
1906                                 Write_EEPROM(tmp->val[i], iobase, i);
1907                         }
1908                 } else {
1909                         status = -EPERM;
1910                 }
1911
1912                 break;
1913         case EWRK3_GET_CMR:     /* Get the CMR Register contents */
1914                 tmp->addr[0] = inb(EWRK3_CMR);
1915                 ioc->len = 1;
1916                 if (copy_to_user(ioc->data, tmp->addr, ioc->len))
1917                         status = -EFAULT;
1918                 break;
1919         case EWRK3_SET_TX_CUT_THRU:     /* Set TX cut through mode */
1920                 if (capable(CAP_NET_ADMIN)) {
1921                         lp->txc = 1;
1922                 } else {
1923                         status = -EPERM;
1924                 }
1925
1926                 break;
1927         case EWRK3_CLR_TX_CUT_THRU:     /* Clear TX cut through mode */
1928                 if (capable(CAP_NET_ADMIN)) {
1929                         lp->txc = 0;
1930                 } else {
1931                         status = -EPERM;
1932                 }
1933
1934                 break;
1935         default:
1936                 status = -EOPNOTSUPP;
1937         }
1938         kfree(tmp);
1939         return status;
1940 }
1941
1942 #ifdef MODULE
1943 static struct net_device *ewrk3_devs[MAX_NUM_EWRK3S];
1944 static int ndevs;
1945 static int io[MAX_NUM_EWRK3S+1] = { 0x300, 0, };
1946
1947 /* '21' below should really be 'MAX_NUM_EWRK3S' */
1948 MODULE_PARM(io, "0-21i");
1949 MODULE_PARM(irq, "0-21i");
1950 MODULE_PARM_DESC(io, "EtherWORKS 3 I/O base address(es)");
1951 MODULE_PARM_DESC(irq, "EtherWORKS 3 IRQ number(s)");
1952
1953 static __exit void ewrk3_exit_module(void)
1954 {
1955         int i;
1956
1957         for( i=0; i<ndevs; i++ ) {
1958                 unregister_netdev(ewrk3_devs[i]);
1959                 release_region(ewrk3_devs[i]->base_addr, EWRK3_TOTAL_SIZE);
1960                 free_netdev(ewrk3_devs[i]);
1961                 ewrk3_devs[i] = NULL;
1962         }
1963 }
1964
1965 static __init int ewrk3_init_module(void)
1966 {
1967         int i=0;
1968
1969         while( io[i] && irq[i] ) {
1970                 struct net_device *dev
1971                         = alloc_etherdev(sizeof(struct ewrk3_private));
1972
1973                 if (!dev)
1974                         break;
1975
1976                 if (ewrk3_probe1(dev, io[i], irq[i]) != 0) {
1977                         free_netdev(dev);
1978                         break;
1979                 }
1980
1981                 ewrk3_devs[ndevs++] = dev;
1982                 i++;
1983         }
1984
1985         return ndevs ? 0 : -EIO;
1986 }
1987
1988
1989 /* Hack for breakage in new module stuff */
1990 module_exit(ewrk3_exit_module);
1991 module_init(ewrk3_init_module);
1992 #endif                          /* MODULE */
1993 MODULE_LICENSE("GPL");
1994
1995 \f
1996
1997 /*
1998  * Local variables:
1999  *  compile-command: "gcc -D__KERNEL__ -I/linux/include -Wall -Wstrict-prototypes -fomit-frame-pointer -fno-strength-reduce -malign-loops=2 -malign-jumps=2 -malign-functions=2 -O2 -m486 -c ewrk3.c"
2000  *
2001  *  compile-command: "gcc -D__KERNEL__ -DMODULE -I/linux/include -Wall -Wstrict-prototypes -fomit-frame-pointer -fno-strength-reduce -malign-loops=2 -malign-jumps=2 -malign-functions=2 -O2 -m486 -c ewrk3.c"
2002  * End:
2003  */