pcmcia: dev_node removal (drivers with unregister_netdev check)
[linux-2.6-block.git] / drivers / net / pcmcia / 3c589_cs.c
1 /*======================================================================
2
3     A PCMCIA ethernet driver for the 3com 3c589 card.
4     
5     Copyright (C) 1999 David A. Hinds -- dahinds@users.sourceforge.net
6
7     3c589_cs.c 1.162 2001/10/13 00:08:50
8
9     The network driver code is based on Donald Becker's 3c589 code:
10     
11     Written 1994 by Donald Becker.
12     Copyright 1993 United States Government as represented by the
13     Director, National Security Agency.  This software may be used and
14     distributed according to the terms of the GNU General Public License,
15     incorporated herein by reference.
16     Donald Becker may be reached at becker@scyld.com
17     
18     Updated for 2.5.x by Alan Cox <alan@lxorguk.ukuu.org.uk>
19
20 ======================================================================*/
21
22 #define DRV_NAME        "3c589_cs"
23 #define DRV_VERSION     "1.162-ac"
24
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/ptrace.h>
29 #include <linux/slab.h>
30 #include <linux/string.h>
31 #include <linux/timer.h>
32 #include <linux/interrupt.h>
33 #include <linux/in.h>
34 #include <linux/delay.h>
35 #include <linux/ethtool.h>
36 #include <linux/netdevice.h>
37 #include <linux/etherdevice.h>
38 #include <linux/skbuff.h>
39 #include <linux/if_arp.h>
40 #include <linux/ioport.h>
41 #include <linux/bitops.h>
42 #include <linux/jiffies.h>
43
44 #include <pcmcia/cs_types.h>
45 #include <pcmcia/cs.h>
46 #include <pcmcia/cistpl.h>
47 #include <pcmcia/cisreg.h>
48 #include <pcmcia/ciscode.h>
49 #include <pcmcia/ds.h>
50
51 #include <asm/uaccess.h>
52 #include <asm/io.h>
53 #include <asm/system.h>
54
55 /* To minimize the size of the driver source I only define operating
56    constants if they are used several times.  You'll need the manual
57    if you want to understand driver details. */
58 /* Offsets from base I/O address. */
59 #define EL3_DATA        0x00
60 #define EL3_TIMER       0x0a
61 #define EL3_CMD         0x0e
62 #define EL3_STATUS      0x0e
63
64 #define EEPROM_READ     0x0080
65 #define EEPROM_BUSY     0x8000
66
67 #define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
68
69 /* The top five bits written to EL3_CMD are a command, the lower
70    11 bits are the parameter, if applicable. */
71 enum c509cmd {
72     TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11,
73     RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11, RxDiscard = 8<<11,
74     TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11,
75     FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11,
76     SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11,
77     SetTxThreshold = 18<<11, SetTxStart = 19<<11, StatsEnable = 21<<11,
78     StatsDisable = 22<<11, StopCoax = 23<<11,
79 };
80
81 enum c509status {
82     IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004,
83     TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
84     IntReq = 0x0040, StatsFull = 0x0080, CmdBusy = 0x1000
85 };
86
87 /* The SetRxFilter command accepts the following classes: */
88 enum RxFilter {
89     RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8
90 };
91
92 /* Register window 1 offsets, the window used in normal operation. */
93 #define TX_FIFO         0x00
94 #define RX_FIFO         0x00
95 #define RX_STATUS       0x08
96 #define TX_STATUS       0x0B
97 #define TX_FREE         0x0C    /* Remaining free bytes in Tx buffer. */
98
99 #define WN0_IRQ         0x08    /* Window 0: Set IRQ line in bits 12-15. */
100 #define WN4_MEDIA       0x0A    /* Window 4: Various transcvr/media bits. */
101 #define MEDIA_TP        0x00C0  /* Enable link beat and jabber for 10baseT. */
102 #define MEDIA_LED       0x0001  /* Enable link light on 3C589E cards. */
103
104 /* Time in jiffies before concluding Tx hung */
105 #define TX_TIMEOUT      ((400*HZ)/1000)
106
107 struct el3_private {
108         struct pcmcia_device    *p_dev;
109     /* For transceiver monitoring */
110     struct timer_list   media;
111     u16                 media_status;
112     u16                 fast_poll;
113     unsigned long       last_irq;
114     spinlock_t          lock;
115 };
116
117 static const char *if_names[] = { "auto", "10baseT", "10base2", "AUI" };
118
119 /*====================================================================*/
120
121 /* Module parameters */
122
123 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
124 MODULE_DESCRIPTION("3Com 3c589 series PCMCIA ethernet driver");
125 MODULE_LICENSE("GPL");
126
127 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0)
128
129 /* Special hook for setting if_port when module is loaded */
130 INT_MODULE_PARM(if_port, 0);
131
132
133 /*====================================================================*/
134
135 static int tc589_config(struct pcmcia_device *link);
136 static void tc589_release(struct pcmcia_device *link);
137
138 static u16 read_eeprom(unsigned int ioaddr, int index);
139 static void tc589_reset(struct net_device *dev);
140 static void media_check(unsigned long arg);
141 static int el3_config(struct net_device *dev, struct ifmap *map);
142 static int el3_open(struct net_device *dev);
143 static netdev_tx_t el3_start_xmit(struct sk_buff *skb,
144                                         struct net_device *dev);
145 static irqreturn_t el3_interrupt(int irq, void *dev_id);
146 static void update_stats(struct net_device *dev);
147 static struct net_device_stats *el3_get_stats(struct net_device *dev);
148 static int el3_rx(struct net_device *dev);
149 static int el3_close(struct net_device *dev);
150 static void el3_tx_timeout(struct net_device *dev);
151 static void set_rx_mode(struct net_device *dev);
152 static void set_multicast_list(struct net_device *dev);
153 static const struct ethtool_ops netdev_ethtool_ops;
154
155 static void tc589_detach(struct pcmcia_device *p_dev);
156
157 /*======================================================================
158
159     tc589_attach() creates an "instance" of the driver, allocating
160     local data structures for one device.  The device is registered
161     with Card Services.
162
163 ======================================================================*/
164
165 static const struct net_device_ops el3_netdev_ops = {
166         .ndo_open               = el3_open,
167         .ndo_stop               = el3_close,
168         .ndo_start_xmit         = el3_start_xmit,
169         .ndo_tx_timeout         = el3_tx_timeout,
170         .ndo_set_config         = el3_config,
171         .ndo_get_stats          = el3_get_stats,
172         .ndo_set_multicast_list = set_multicast_list,
173         .ndo_change_mtu         = eth_change_mtu,
174         .ndo_set_mac_address    = eth_mac_addr,
175         .ndo_validate_addr      = eth_validate_addr,
176 };
177
178 static int tc589_probe(struct pcmcia_device *link)
179 {
180     struct el3_private *lp;
181     struct net_device *dev;
182
183     dev_dbg(&link->dev, "3c589_attach()\n");
184
185     /* Create new ethernet device */
186     dev = alloc_etherdev(sizeof(struct el3_private));
187     if (!dev)
188          return -ENOMEM;
189     lp = netdev_priv(dev);
190     link->priv = dev;
191     lp->p_dev = link;
192
193     spin_lock_init(&lp->lock);
194     link->io.NumPorts1 = 16;
195     link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
196
197     link->conf.Attributes = CONF_ENABLE_IRQ;
198     link->conf.IntType = INT_MEMORY_AND_IO;
199     link->conf.ConfigIndex = 1;
200
201     dev->netdev_ops = &el3_netdev_ops;
202     dev->watchdog_timeo = TX_TIMEOUT;
203
204     SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
205
206     return tc589_config(link);
207 } /* tc589_attach */
208
209 /*======================================================================
210
211     This deletes a driver "instance".  The device is de-registered
212     with Card Services.  If it has been released, all local data
213     structures are freed.  Otherwise, the structures will be freed
214     when the device is released.
215
216 ======================================================================*/
217
218 static void tc589_detach(struct pcmcia_device *link)
219 {
220     struct net_device *dev = link->priv;
221
222     dev_dbg(&link->dev, "3c589_detach\n");
223
224     unregister_netdev(dev);
225
226     tc589_release(link);
227
228     free_netdev(dev);
229 } /* tc589_detach */
230
231 /*======================================================================
232
233     tc589_config() is scheduled to run after a CARD_INSERTION event
234     is received, to configure the PCMCIA socket, and to make the
235     ethernet device available to the system.
236     
237 ======================================================================*/
238
239 static int tc589_config(struct pcmcia_device *link)
240 {
241     struct net_device *dev = link->priv;
242     __be16 *phys_addr;
243     int ret, i, j, multi = 0, fifo;
244     unsigned int ioaddr;
245     char *ram_split[] = {"5:3", "3:1", "1:1", "3:5"};
246     u8 *buf;
247     size_t len;
248     
249     dev_dbg(&link->dev, "3c589_config\n");
250
251     phys_addr = (__be16 *)dev->dev_addr;
252     /* Is this a 3c562? */
253     if (link->manf_id != MANFID_3COM)
254             printk(KERN_INFO "3c589_cs: hmmm, is this really a "
255                    "3Com card??\n");
256     multi = (link->card_id == PRODID_3COM_3C562);
257
258     /* For the 3c562, the base address must be xx00-xx7f */
259     link->io.IOAddrLines = 16;
260     for (i = j = 0; j < 0x400; j += 0x10) {
261         if (multi && (j & 0x80)) continue;
262         link->io.BasePort1 = j ^ 0x300;
263         i = pcmcia_request_io(link, &link->io);
264         if (i == 0)
265                 break;
266     }
267     if (i != 0)
268         goto failed;
269
270     ret = pcmcia_request_irq(link, el3_interrupt);
271     if (ret)
272             goto failed;
273
274     ret = pcmcia_request_configuration(link, &link->conf);
275     if (ret)
276             goto failed;
277         
278     dev->irq = link->irq;
279     dev->base_addr = link->io.BasePort1;
280     ioaddr = dev->base_addr;
281     EL3WINDOW(0);
282
283     /* The 3c589 has an extra EEPROM for configuration info, including
284        the hardware address.  The 3c562 puts the address in the CIS. */
285     len = pcmcia_get_tuple(link, 0x88, &buf);
286     if (buf && len >= 6) {
287             for (i = 0; i < 3; i++)
288                     phys_addr[i] = htons(le16_to_cpu(buf[i*2]));
289             kfree(buf);
290     } else {
291         kfree(buf); /* 0 < len < 6 */
292         for (i = 0; i < 3; i++)
293             phys_addr[i] = htons(read_eeprom(ioaddr, i));
294         if (phys_addr[0] == htons(0x6060)) {
295             printk(KERN_ERR "3c589_cs: IO port conflict at 0x%03lx"
296                    "-0x%03lx\n", dev->base_addr, dev->base_addr+15);
297             goto failed;
298         }
299     }
300
301     /* The address and resource configuration register aren't loaded from
302        the EEPROM and *must* be set to 0 and IRQ3 for the PCMCIA version. */
303     outw(0x3f00, ioaddr + 8);
304     fifo = inl(ioaddr);
305
306     /* The if_port symbol can be set when the module is loaded */
307     if ((if_port >= 0) && (if_port <= 3))
308         dev->if_port = if_port;
309     else
310         printk(KERN_ERR "3c589_cs: invalid if_port requested\n");
311     
312     SET_NETDEV_DEV(dev, &link->dev);
313
314     if (register_netdev(dev) != 0) {
315         printk(KERN_ERR "3c589_cs: register_netdev() failed\n");
316         goto failed;
317     }
318
319     printk(KERN_INFO "%s: 3Com 3c%s, io %#3lx, irq %d, "
320            "hw_addr %pM\n",
321            dev->name, (multi ? "562" : "589"), dev->base_addr, dev->irq,
322            dev->dev_addr);
323     printk(KERN_INFO "  %dK FIFO split %s Rx:Tx, %s xcvr\n",
324            (fifo & 7) ? 32 : 8, ram_split[(fifo >> 16) & 3],
325            if_names[dev->if_port]);
326     return 0;
327
328 failed:
329     tc589_release(link);
330     return -ENODEV;
331 } /* tc589_config */
332
333 /*======================================================================
334
335     After a card is removed, tc589_release() will unregister the net
336     device, and release the PCMCIA configuration.  If the device is
337     still open, this will be postponed until it is closed.
338     
339 ======================================================================*/
340
341 static void tc589_release(struct pcmcia_device *link)
342 {
343         pcmcia_disable_device(link);
344 }
345
346 static int tc589_suspend(struct pcmcia_device *link)
347 {
348         struct net_device *dev = link->priv;
349
350         if (link->open)
351                 netif_device_detach(dev);
352
353         return 0;
354 }
355
356 static int tc589_resume(struct pcmcia_device *link)
357 {
358         struct net_device *dev = link->priv;
359
360         if (link->open) {
361                 tc589_reset(dev);
362                 netif_device_attach(dev);
363         }
364
365         return 0;
366 }
367
368 /*====================================================================*/
369
370 /*
371   Use this for commands that may take time to finish
372 */
373 static void tc589_wait_for_completion(struct net_device *dev, int cmd)
374 {
375     int i = 100;
376     outw(cmd, dev->base_addr + EL3_CMD);
377     while (--i > 0)
378         if (!(inw(dev->base_addr + EL3_STATUS) & 0x1000)) break;
379     if (i == 0)
380         printk(KERN_WARNING "%s: command 0x%04x did not complete!\n",
381                dev->name, cmd);
382 }
383
384 /*
385   Read a word from the EEPROM using the regular EEPROM access register.
386   Assume that we are in register window zero.
387 */
388 static u16 read_eeprom(unsigned int ioaddr, int index)
389 {
390     int i;
391     outw(EEPROM_READ + index, ioaddr + 10);
392     /* Reading the eeprom takes 162 us */
393     for (i = 1620; i >= 0; i--)
394         if ((inw(ioaddr + 10) & EEPROM_BUSY) == 0)
395             break;
396     return inw(ioaddr + 12);
397 }
398
399 /*
400   Set transceiver type, perhaps to something other than what the user
401   specified in dev->if_port.
402 */
403 static void tc589_set_xcvr(struct net_device *dev, int if_port)
404 {
405     struct el3_private *lp = netdev_priv(dev);
406     unsigned int ioaddr = dev->base_addr;
407     
408     EL3WINDOW(0);
409     switch (if_port) {
410     case 0: case 1: outw(0, ioaddr + 6); break;
411     case 2: outw(3<<14, ioaddr + 6); break;
412     case 3: outw(1<<14, ioaddr + 6); break;
413     }
414     /* On PCMCIA, this just turns on the LED */
415     outw((if_port == 2) ? StartCoax : StopCoax, ioaddr + EL3_CMD);
416     /* 10baseT interface, enable link beat and jabber check. */
417     EL3WINDOW(4);
418     outw(MEDIA_LED | ((if_port < 2) ? MEDIA_TP : 0), ioaddr + WN4_MEDIA);
419     EL3WINDOW(1);
420     if (if_port == 2)
421         lp->media_status = ((dev->if_port == 0) ? 0x8000 : 0x4000);
422     else
423         lp->media_status = ((dev->if_port == 0) ? 0x4010 : 0x8800);
424 }
425
426 static void dump_status(struct net_device *dev)
427 {
428     unsigned int ioaddr = dev->base_addr;
429     EL3WINDOW(1);
430     printk(KERN_INFO "  irq status %04x, rx status %04x, tx status "
431            "%02x  tx free %04x\n", inw(ioaddr+EL3_STATUS),
432            inw(ioaddr+RX_STATUS), inb(ioaddr+TX_STATUS),
433            inw(ioaddr+TX_FREE));
434     EL3WINDOW(4);
435     printk(KERN_INFO "  diagnostics: fifo %04x net %04x ethernet %04x"
436            " media %04x\n", inw(ioaddr+0x04), inw(ioaddr+0x06),
437            inw(ioaddr+0x08), inw(ioaddr+0x0a));
438     EL3WINDOW(1);
439 }
440
441 /* Reset and restore all of the 3c589 registers. */
442 static void tc589_reset(struct net_device *dev)
443 {
444     unsigned int ioaddr = dev->base_addr;
445     int i;
446     
447     EL3WINDOW(0);
448     outw(0x0001, ioaddr + 4);                   /* Activate board. */ 
449     outw(0x3f00, ioaddr + 8);                   /* Set the IRQ line. */
450     
451     /* Set the station address in window 2. */
452     EL3WINDOW(2);
453     for (i = 0; i < 6; i++)
454         outb(dev->dev_addr[i], ioaddr + i);
455
456     tc589_set_xcvr(dev, dev->if_port);
457     
458     /* Switch to the stats window, and clear all stats by reading. */
459     outw(StatsDisable, ioaddr + EL3_CMD);
460     EL3WINDOW(6);
461     for (i = 0; i < 9; i++)
462         inb(ioaddr+i);
463     inw(ioaddr + 10);
464     inw(ioaddr + 12);
465     
466     /* Switch to register set 1 for normal use. */
467     EL3WINDOW(1);
468
469     set_rx_mode(dev);
470     outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
471     outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
472     outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
473     /* Allow status bits to be seen. */
474     outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
475     /* Ack all pending events, and set active indicator mask. */
476     outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
477          ioaddr + EL3_CMD);
478     outw(SetIntrEnb | IntLatch | TxAvailable | RxComplete | StatsFull
479          | AdapterFailure, ioaddr + EL3_CMD);
480 }
481
482 static void netdev_get_drvinfo(struct net_device *dev,
483                                struct ethtool_drvinfo *info)
484 {
485         strcpy(info->driver, DRV_NAME);
486         strcpy(info->version, DRV_VERSION);
487         sprintf(info->bus_info, "PCMCIA 0x%lx", dev->base_addr);
488 }
489
490 static const struct ethtool_ops netdev_ethtool_ops = {
491         .get_drvinfo            = netdev_get_drvinfo,
492 };
493
494 static int el3_config(struct net_device *dev, struct ifmap *map)
495 {
496     if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
497         if (map->port <= 3) {
498             dev->if_port = map->port;
499             printk(KERN_INFO "%s: switched to %s port\n",
500                    dev->name, if_names[dev->if_port]);
501             tc589_set_xcvr(dev, dev->if_port);
502         } else
503             return -EINVAL;
504     }
505     return 0;
506 }
507
508 static int el3_open(struct net_device *dev)
509 {
510     struct el3_private *lp = netdev_priv(dev);
511     struct pcmcia_device *link = lp->p_dev;
512     
513     if (!pcmcia_dev_present(link))
514         return -ENODEV;
515
516     link->open++;
517     netif_start_queue(dev);
518     
519     tc589_reset(dev);
520     init_timer(&lp->media);
521     lp->media.function = &media_check;
522     lp->media.data = (unsigned long) dev;
523     lp->media.expires = jiffies + HZ;
524     add_timer(&lp->media);
525
526     dev_dbg(&link->dev, "%s: opened, status %4.4x.\n",
527           dev->name, inw(dev->base_addr + EL3_STATUS));
528     
529     return 0;
530 }
531
532 static void el3_tx_timeout(struct net_device *dev)
533 {
534     unsigned int ioaddr = dev->base_addr;
535     
536     printk(KERN_WARNING "%s: Transmit timed out!\n", dev->name);
537     dump_status(dev);
538     dev->stats.tx_errors++;
539     dev->trans_start = jiffies;
540     /* Issue TX_RESET and TX_START commands. */
541     tc589_wait_for_completion(dev, TxReset);
542     outw(TxEnable, ioaddr + EL3_CMD);
543     netif_wake_queue(dev);
544 }
545
546 static void pop_tx_status(struct net_device *dev)
547 {
548     unsigned int ioaddr = dev->base_addr;
549     int i;
550     
551     /* Clear the Tx status stack. */
552     for (i = 32; i > 0; i--) {
553         u_char tx_status = inb(ioaddr + TX_STATUS);
554         if (!(tx_status & 0x84)) break;
555         /* reset transmitter on jabber error or underrun */
556         if (tx_status & 0x30)
557             tc589_wait_for_completion(dev, TxReset);
558         if (tx_status & 0x38) {
559             pr_debug("%s: transmit error: status 0x%02x\n",
560                   dev->name, tx_status);
561             outw(TxEnable, ioaddr + EL3_CMD);
562             dev->stats.tx_aborted_errors++;
563         }
564         outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
565     }
566 }
567
568 static netdev_tx_t el3_start_xmit(struct sk_buff *skb,
569                                         struct net_device *dev)
570 {
571     unsigned int ioaddr = dev->base_addr;
572     struct el3_private *priv = netdev_priv(dev);
573     unsigned long flags;
574
575     pr_debug("%s: el3_start_xmit(length = %ld) called, "
576           "status %4.4x.\n", dev->name, (long)skb->len,
577           inw(ioaddr + EL3_STATUS));
578
579     spin_lock_irqsave(&priv->lock, flags);    
580
581     dev->stats.tx_bytes += skb->len;
582
583     /* Put out the doubleword header... */
584     outw(skb->len, ioaddr + TX_FIFO);
585     outw(0x00, ioaddr + TX_FIFO);
586     /* ... and the packet rounded to a doubleword. */
587     outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
588
589     dev->trans_start = jiffies;
590     if (inw(ioaddr + TX_FREE) <= 1536) {
591         netif_stop_queue(dev);
592         /* Interrupt us when the FIFO has room for max-sized packet. */
593         outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
594     }
595
596     pop_tx_status(dev);
597     spin_unlock_irqrestore(&priv->lock, flags);    
598     dev_kfree_skb(skb);
599     
600     return NETDEV_TX_OK;
601 }
602
603 /* The EL3 interrupt handler. */
604 static irqreturn_t el3_interrupt(int irq, void *dev_id)
605 {
606     struct net_device *dev = (struct net_device *) dev_id;
607     struct el3_private *lp = netdev_priv(dev);
608     unsigned int ioaddr;
609     __u16 status;
610     int i = 0, handled = 1;
611     
612     if (!netif_device_present(dev))
613         return IRQ_NONE;
614
615     ioaddr = dev->base_addr;
616
617     pr_debug("%s: interrupt, status %4.4x.\n",
618           dev->name, inw(ioaddr + EL3_STATUS));
619
620     spin_lock(&lp->lock);    
621     while ((status = inw(ioaddr + EL3_STATUS)) &
622         (IntLatch | RxComplete | StatsFull)) {
623         if ((status & 0xe000) != 0x2000) {
624             pr_debug("%s: interrupt from dead card\n", dev->name);
625             handled = 0;
626             break;
627         }
628         
629         if (status & RxComplete)
630             el3_rx(dev);
631         
632         if (status & TxAvailable) {
633             pr_debug("    TX room bit was handled.\n");
634             /* There's room in the FIFO for a full-sized packet. */
635             outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
636             netif_wake_queue(dev);
637         }
638         
639         if (status & TxComplete)
640             pop_tx_status(dev);
641
642         if (status & (AdapterFailure | RxEarly | StatsFull)) {
643             /* Handle all uncommon interrupts. */
644             if (status & StatsFull)             /* Empty statistics. */
645                 update_stats(dev);
646             if (status & RxEarly) {             /* Rx early is unused. */
647                 el3_rx(dev);
648                 outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
649             }
650             if (status & AdapterFailure) {
651                 u16 fifo_diag;
652                 EL3WINDOW(4);
653                 fifo_diag = inw(ioaddr + 4);
654                 EL3WINDOW(1);
655                 printk(KERN_WARNING "%s: adapter failure, FIFO diagnostic"
656                        " register %04x.\n", dev->name, fifo_diag);
657                 if (fifo_diag & 0x0400) {
658                     /* Tx overrun */
659                     tc589_wait_for_completion(dev, TxReset);
660                     outw(TxEnable, ioaddr + EL3_CMD);
661                 }
662                 if (fifo_diag & 0x2000) {
663                     /* Rx underrun */
664                     tc589_wait_for_completion(dev, RxReset);
665                     set_rx_mode(dev);
666                     outw(RxEnable, ioaddr + EL3_CMD);
667                 }
668                 outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
669             }
670         }
671         
672         if (++i > 10) {
673             printk(KERN_ERR "%s: infinite loop in interrupt, "
674                    "status %4.4x.\n", dev->name, status);
675             /* Clear all interrupts */
676             outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
677             break;
678         }
679         /* Acknowledge the IRQ. */
680         outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD);
681     }
682
683     lp->last_irq = jiffies;
684     spin_unlock(&lp->lock);    
685     pr_debug("%s: exiting interrupt, status %4.4x.\n",
686           dev->name, inw(ioaddr + EL3_STATUS));
687     return IRQ_RETVAL(handled);
688 }
689
690 static void media_check(unsigned long arg)
691 {
692     struct net_device *dev = (struct net_device *)(arg);
693     struct el3_private *lp = netdev_priv(dev);
694     unsigned int ioaddr = dev->base_addr;
695     u16 media, errs;
696     unsigned long flags;
697
698     if (!netif_device_present(dev)) goto reschedule;
699
700     /* Check for pending interrupt with expired latency timer: with
701        this, we can limp along even if the interrupt is blocked */
702     if ((inw(ioaddr + EL3_STATUS) & IntLatch) &&
703         (inb(ioaddr + EL3_TIMER) == 0xff)) {
704         if (!lp->fast_poll)
705             printk(KERN_WARNING "%s: interrupt(s) dropped!\n", dev->name);
706
707         local_irq_save(flags);
708         el3_interrupt(dev->irq, dev);
709         local_irq_restore(flags);
710
711         lp->fast_poll = HZ;
712     }
713     if (lp->fast_poll) {
714         lp->fast_poll--;
715         lp->media.expires = jiffies + HZ/100;
716         add_timer(&lp->media);
717         return;
718     }
719
720     /* lp->lock guards the EL3 window. Window should always be 1 except
721        when the lock is held */
722     spin_lock_irqsave(&lp->lock, flags);    
723     EL3WINDOW(4);
724     media = inw(ioaddr+WN4_MEDIA) & 0xc810;
725
726     /* Ignore collisions unless we've had no irq's recently */
727     if (time_before(jiffies, lp->last_irq + HZ)) {
728         media &= ~0x0010;
729     } else {
730         /* Try harder to detect carrier errors */
731         EL3WINDOW(6);
732         outw(StatsDisable, ioaddr + EL3_CMD);
733         errs = inb(ioaddr + 0);
734         outw(StatsEnable, ioaddr + EL3_CMD);
735         dev->stats.tx_carrier_errors += errs;
736         if (errs || (lp->media_status & 0x0010)) media |= 0x0010;
737     }
738
739     if (media != lp->media_status) {
740         if ((media & lp->media_status & 0x8000) &&
741             ((lp->media_status ^ media) & 0x0800))
742             printk(KERN_INFO "%s: %s link beat\n", dev->name,
743                    (lp->media_status & 0x0800 ? "lost" : "found"));
744         else if ((media & lp->media_status & 0x4000) &&
745                  ((lp->media_status ^ media) & 0x0010))
746             printk(KERN_INFO "%s: coax cable %s\n", dev->name,
747                    (lp->media_status & 0x0010 ? "ok" : "problem"));
748         if (dev->if_port == 0) {
749             if (media & 0x8000) {
750                 if (media & 0x0800)
751                     printk(KERN_INFO "%s: flipped to 10baseT\n",
752                            dev->name);
753                 else
754                     tc589_set_xcvr(dev, 2);
755             } else if (media & 0x4000) {
756                 if (media & 0x0010)
757                     tc589_set_xcvr(dev, 1);
758                 else
759                     printk(KERN_INFO "%s: flipped to 10base2\n",
760                            dev->name);
761             }
762         }
763         lp->media_status = media;
764     }
765     
766     EL3WINDOW(1);
767     spin_unlock_irqrestore(&lp->lock, flags);    
768
769 reschedule:
770     lp->media.expires = jiffies + HZ;
771     add_timer(&lp->media);
772 }
773
774 static struct net_device_stats *el3_get_stats(struct net_device *dev)
775 {
776     struct el3_private *lp = netdev_priv(dev);
777     unsigned long flags;
778     struct pcmcia_device *link = lp->p_dev;
779
780     if (pcmcia_dev_present(link)) {
781         spin_lock_irqsave(&lp->lock, flags);
782         update_stats(dev);
783         spin_unlock_irqrestore(&lp->lock, flags);
784     }
785     return &dev->stats;
786 }
787
788 /*
789   Update statistics.  We change to register window 6, so this should be run
790   single-threaded if the device is active. This is expected to be a rare
791   operation, and it's simpler for the rest of the driver to assume that
792   window 1 is always valid rather than use a special window-state variable.
793   
794   Caller must hold the lock for this
795 */
796 static void update_stats(struct net_device *dev)
797 {
798     unsigned int ioaddr = dev->base_addr;
799
800     pr_debug("%s: updating the statistics.\n", dev->name);
801     /* Turn off statistics updates while reading. */
802     outw(StatsDisable, ioaddr + EL3_CMD);
803     /* Switch to the stats window, and read everything. */
804     EL3WINDOW(6);
805     dev->stats.tx_carrier_errors        += inb(ioaddr + 0);
806     dev->stats.tx_heartbeat_errors      += inb(ioaddr + 1);
807     /* Multiple collisions. */          inb(ioaddr + 2);
808     dev->stats.collisions               += inb(ioaddr + 3);
809     dev->stats.tx_window_errors         += inb(ioaddr + 4);
810     dev->stats.rx_fifo_errors           += inb(ioaddr + 5);
811     dev->stats.tx_packets               += inb(ioaddr + 6);
812     /* Rx packets   */                  inb(ioaddr + 7);
813     /* Tx deferrals */                  inb(ioaddr + 8);
814     /* Rx octets */                     inw(ioaddr + 10);
815     /* Tx octets */                     inw(ioaddr + 12);
816     
817     /* Back to window 1, and turn statistics back on. */
818     EL3WINDOW(1);
819     outw(StatsEnable, ioaddr + EL3_CMD);
820 }
821
822 static int el3_rx(struct net_device *dev)
823 {
824     unsigned int ioaddr = dev->base_addr;
825     int worklimit = 32;
826     short rx_status;
827     
828     pr_debug("%s: in rx_packet(), status %4.4x, rx_status %4.4x.\n",
829           dev->name, inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
830     while (!((rx_status = inw(ioaddr + RX_STATUS)) & 0x8000) &&
831                     worklimit > 0) {
832         worklimit--;
833         if (rx_status & 0x4000) { /* Error, update stats. */
834             short error = rx_status & 0x3800;
835             dev->stats.rx_errors++;
836             switch (error) {
837             case 0x0000:        dev->stats.rx_over_errors++; break;
838             case 0x0800:        dev->stats.rx_length_errors++; break;
839             case 0x1000:        dev->stats.rx_frame_errors++; break;
840             case 0x1800:        dev->stats.rx_length_errors++; break;
841             case 0x2000:        dev->stats.rx_frame_errors++; break;
842             case 0x2800:        dev->stats.rx_crc_errors++; break;
843             }
844         } else {
845             short pkt_len = rx_status & 0x7ff;
846             struct sk_buff *skb;
847             
848             skb = dev_alloc_skb(pkt_len+5);
849             
850             pr_debug("    Receiving packet size %d status %4.4x.\n",
851                   pkt_len, rx_status);
852             if (skb != NULL) {
853                 skb_reserve(skb, 2);
854                 insl(ioaddr+RX_FIFO, skb_put(skb, pkt_len),
855                         (pkt_len+3)>>2);
856                 skb->protocol = eth_type_trans(skb, dev);
857                 netif_rx(skb);
858                 dev->stats.rx_packets++;
859                 dev->stats.rx_bytes += pkt_len;
860             } else {
861                 pr_debug("%s: couldn't allocate a sk_buff of"
862                       " size %d.\n", dev->name, pkt_len);
863                 dev->stats.rx_dropped++;
864             }
865         }
866         /* Pop the top of the Rx FIFO */
867         tc589_wait_for_completion(dev, RxDiscard);
868     }
869     if (worklimit == 0)
870         printk(KERN_WARNING "%s: too much work in el3_rx!\n", dev->name);
871     return 0;
872 }
873
874 static void set_rx_mode(struct net_device *dev)
875 {
876     unsigned int ioaddr = dev->base_addr;
877     u16 opts = SetRxFilter | RxStation | RxBroadcast;
878
879     if (dev->flags & IFF_PROMISC)
880         opts |= RxMulticast | RxProm;
881     else if (!netdev_mc_empty(dev) || (dev->flags & IFF_ALLMULTI))
882         opts |= RxMulticast;
883     outw(opts, ioaddr + EL3_CMD);
884 }
885
886 static void set_multicast_list(struct net_device *dev)
887 {
888         struct el3_private *priv = netdev_priv(dev);
889         unsigned long flags;
890
891         spin_lock_irqsave(&priv->lock, flags);
892         set_rx_mode(dev);
893         spin_unlock_irqrestore(&priv->lock, flags);
894 }
895
896 static int el3_close(struct net_device *dev)
897 {
898     struct el3_private *lp = netdev_priv(dev);
899     struct pcmcia_device *link = lp->p_dev;
900     unsigned int ioaddr = dev->base_addr;
901     
902     dev_dbg(&link->dev, "%s: shutting down ethercard.\n", dev->name);
903
904     if (pcmcia_dev_present(link)) {
905         /* Turn off statistics ASAP.  We update dev->stats below. */
906         outw(StatsDisable, ioaddr + EL3_CMD);
907         
908         /* Disable the receiver and transmitter. */
909         outw(RxDisable, ioaddr + EL3_CMD);
910         outw(TxDisable, ioaddr + EL3_CMD);
911         
912         if (dev->if_port == 2)
913             /* Turn off thinnet power.  Green! */
914             outw(StopCoax, ioaddr + EL3_CMD);
915         else if (dev->if_port == 1) {
916             /* Disable link beat and jabber */
917             EL3WINDOW(4);
918             outw(0, ioaddr + WN4_MEDIA);
919         }
920         
921         /* Switching back to window 0 disables the IRQ. */
922         EL3WINDOW(0);
923         /* But we explicitly zero the IRQ line select anyway. */
924         outw(0x0f00, ioaddr + WN0_IRQ);
925         
926         /* Check if the card still exists */
927         if ((inw(ioaddr+EL3_STATUS) & 0xe000) == 0x2000)
928             update_stats(dev);
929     }
930
931     link->open--;
932     netif_stop_queue(dev);
933     del_timer_sync(&lp->media);
934     
935     return 0;
936 }
937
938 static struct pcmcia_device_id tc589_ids[] = {
939         PCMCIA_MFC_DEVICE_MANF_CARD(0, 0x0101, 0x0562),
940         PCMCIA_MFC_DEVICE_PROD_ID1(0, "Motorola MARQUIS", 0xf03e4e77),
941         PCMCIA_DEVICE_MANF_CARD(0x0101, 0x0589),
942         PCMCIA_DEVICE_PROD_ID12("Farallon", "ENet", 0x58d93fc4, 0x992c2202),
943         PCMCIA_MFC_DEVICE_CIS_MANF_CARD(0, 0x0101, 0x0035, "cis/3CXEM556.cis"),
944         PCMCIA_MFC_DEVICE_CIS_MANF_CARD(0, 0x0101, 0x003d, "cis/3CXEM556.cis"),
945         PCMCIA_DEVICE_NULL,
946 };
947 MODULE_DEVICE_TABLE(pcmcia, tc589_ids);
948
949 static struct pcmcia_driver tc589_driver = {
950         .owner          = THIS_MODULE,
951         .drv            = {
952                 .name   = "3c589_cs",
953         },
954         .probe          = tc589_probe,
955         .remove         = tc589_detach,
956         .id_table       = tc589_ids,
957         .suspend        = tc589_suspend,
958         .resume         = tc589_resume,
959 };
960
961 static int __init init_tc589(void)
962 {
963         return pcmcia_register_driver(&tc589_driver);
964 }
965
966 static void __exit exit_tc589(void)
967 {
968         pcmcia_unregister_driver(&tc589_driver);
969 }
970
971 module_init(init_tc589);
972 module_exit(exit_tc589);