patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / net / pcmcia / pcnet_cs.c
1 /*======================================================================
2
3     A PCMCIA ethernet driver for NS8390-based cards
4
5     This driver supports the D-Link DE-650 and Linksys EthernetCard
6     cards, the newer D-Link and Linksys combo cards, Accton EN2212
7     cards, the RPTI EP400, and the PreMax PE-200 in non-shared-memory
8     mode, and the IBM Credit Card Adapter, the NE4100, the Thomas
9     Conrad ethernet card, and the Kingston KNE-PCM/x in shared-memory
10     mode.  It will also handle the Socket EA card in either mode.
11
12     Copyright (C) 1999 David A. Hinds -- dahinds@users.sourceforge.net
13
14     pcnet_cs.c 1.153 2003/11/09 18:53:09
15     
16     The network driver code is based on Donald Becker's NE2000 code:
17
18     Written 1992,1993 by Donald Becker.
19     Copyright 1993 United States Government as represented by the
20     Director, National Security Agency.  This software may be used and
21     distributed according to the terms of the GNU General Public License,
22     incorporated herein by reference.
23     Donald Becker may be reached at becker@scyld.com
24
25     Based also on Keith Moore's changes to Don Becker's code, for IBM
26     CCAE support.  Drivers merged back together, and shared-memory
27     Socket EA support added, by Ken Raeburn, September 1995.
28
29 ======================================================================*/
30
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/init.h>
34 #include <linux/ptrace.h>
35 #include <linux/slab.h>
36 #include <linux/string.h>
37 #include <linux/timer.h>
38 #include <linux/delay.h>
39 #include <linux/ethtool.h>
40 #include <linux/netdevice.h>
41 #include <../drivers/net/8390.h>
42
43 #include <pcmcia/version.h>
44 #include <pcmcia/cs_types.h>
45 #include <pcmcia/cs.h>
46 #include <pcmcia/cistpl.h>
47 #include <pcmcia/ciscode.h>
48 #include <pcmcia/ds.h>
49 #include <pcmcia/cisreg.h>
50
51 #include <asm/io.h>
52 #include <asm/system.h>
53 #include <asm/byteorder.h>
54 #include <asm/uaccess.h>
55
56 #define PCNET_CMD       0x00
57 #define PCNET_DATAPORT  0x10    /* NatSemi-defined port window offset. */
58 #define PCNET_RESET     0x1f    /* Issue a read to reset, a write to clear. */
59 #define PCNET_MISC      0x18    /* For IBM CCAE and Socket EA cards */
60
61 #define PCNET_START_PG  0x40    /* First page of TX buffer */
62 #define PCNET_STOP_PG   0x80    /* Last page +1 of RX ring */
63
64 /* Socket EA cards have a larger packet buffer */
65 #define SOCKET_START_PG 0x01
66 #define SOCKET_STOP_PG  0xff
67
68 #define PCNET_RDC_TIMEOUT (2*HZ/100)    /* Max wait in jiffies for Tx RDC */
69
70 static char *if_names[] = { "auto", "10baseT", "10base2"};
71
72 #ifdef PCMCIA_DEBUG
73 static int pc_debug = PCMCIA_DEBUG;
74 MODULE_PARM(pc_debug, "i");
75 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
76 static char *version =
77 "pcnet_cs.c 1.153 2003/11/09 18:53:09 (David Hinds)";
78 #else
79 #define DEBUG(n, args...)
80 #endif
81
82 /*====================================================================*/
83
84 /* Module parameters */
85
86 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
87 MODULE_DESCRIPTION("NE2000 compatible PCMCIA ethernet driver");
88 MODULE_LICENSE("GPL");
89
90 #define INT_MODULE_PARM(n, v) static int n = v; MODULE_PARM(n, "i")
91
92 /* Bit map of interrupts to choose from */
93 INT_MODULE_PARM(irq_mask,       0xdeb8);
94 static int irq_list[4] = { -1 };
95 MODULE_PARM(irq_list, "1-4i");
96
97 INT_MODULE_PARM(if_port,        1);     /* Transceiver type */
98 INT_MODULE_PARM(use_big_buf,    1);     /* use 64K packet buffer? */
99 INT_MODULE_PARM(mem_speed,      0);     /* shared mem speed, in ns */
100 INT_MODULE_PARM(delay_output,   0);     /* pause after xmit? */
101 INT_MODULE_PARM(delay_time,     4);     /* in usec */
102 INT_MODULE_PARM(use_shmem,      -1);    /* use shared memory? */
103 INT_MODULE_PARM(full_duplex,    0);     /* full duplex? */
104
105 /* Ugh!  Let the user hardwire the hardware address for queer cards */
106 static int hw_addr[6] = { 0, /* ... */ };
107 MODULE_PARM(hw_addr, "6i");
108
109 /*====================================================================*/
110
111 static void mii_phy_probe(struct net_device *dev);
112 static void pcnet_config(dev_link_t *link);
113 static void pcnet_release(dev_link_t *link);
114 static int pcnet_event(event_t event, int priority,
115                        event_callback_args_t *args);
116 static int pcnet_open(struct net_device *dev);
117 static int pcnet_close(struct net_device *dev);
118 static int ei_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
119 static struct ethtool_ops netdev_ethtool_ops;
120 static irqreturn_t ei_irq_wrapper(int irq, void *dev_id, struct pt_regs *regs);
121 static void ei_watchdog(u_long arg);
122 static void pcnet_reset_8390(struct net_device *dev);
123 static int set_config(struct net_device *dev, struct ifmap *map);
124 static int setup_shmem_window(dev_link_t *link, int start_pg,
125                               int stop_pg, int cm_offset);
126 static int setup_dma_config(dev_link_t *link, int start_pg,
127                             int stop_pg);
128
129 static dev_link_t *pcnet_attach(void);
130 static void pcnet_detach(dev_link_t *);
131
132 static dev_info_t dev_info = "pcnet_cs";
133 static dev_link_t *dev_list;
134
135 /*====================================================================*/
136
137 typedef struct hw_info_t {
138     u_int       offset;
139     u_char      a0, a1, a2;
140     u_int       flags;
141 } hw_info_t;
142
143 #define DELAY_OUTPUT    0x01
144 #define HAS_MISC_REG    0x02
145 #define USE_BIG_BUF     0x04
146 #define HAS_IBM_MISC    0x08
147 #define IS_DL10019      0x10
148 #define IS_DL10022      0x20
149 #define HAS_MII         0x40
150 #define USE_SHMEM       0x80    /* autodetected */
151
152 #define AM79C9XX_HOME_PHY       0x00006B90  /* HomePNA PHY */
153 #define AM79C9XX_ETH_PHY        0x00006B70  /* 10baseT PHY */
154 #define MII_PHYID_REV_MASK      0xfffffff0
155 #define MII_PHYID_REG1          0x02
156 #define MII_PHYID_REG2          0x03
157
158 static hw_info_t hw_info[] = {
159     { /* Accton EN2212 */ 0x0ff0, 0x00, 0x00, 0xe8, DELAY_OUTPUT }, 
160     { /* Allied Telesis LA-PCM */ 0x0ff0, 0x00, 0x00, 0xf4, 0 },
161     { /* APEX MultiCard */ 0x03f4, 0x00, 0x20, 0xe5, 0 },
162     { /* ASANTE FriendlyNet */ 0x4910, 0x00, 0x00, 0x94,
163       DELAY_OUTPUT | HAS_IBM_MISC },
164     { /* Danpex EN-6200P2 */ 0x0110, 0x00, 0x40, 0xc7, 0 },
165     { /* DataTrek NetCard */ 0x0ff0, 0x00, 0x20, 0xe8, 0 },
166     { /* Dayna CommuniCard E */ 0x0110, 0x00, 0x80, 0x19, 0 },
167     { /* D-Link DE-650 */ 0x0040, 0x00, 0x80, 0xc8, 0 },
168     { /* EP-210 Ethernet */ 0x0110, 0x00, 0x40, 0x33, 0 },
169     { /* EP4000 Ethernet */ 0x01c0, 0x00, 0x00, 0xb4, 0 },
170     { /* Epson EEN10B */ 0x0ff0, 0x00, 0x00, 0x48,
171       HAS_MISC_REG | HAS_IBM_MISC },
172     { /* ELECOM Laneed LD-CDWA */ 0xb8, 0x08, 0x00, 0x42, 0 },
173     { /* Hypertec Ethernet */ 0x01c0, 0x00, 0x40, 0x4c, 0 },
174     { /* IBM CCAE */ 0x0ff0, 0x08, 0x00, 0x5a,
175       HAS_MISC_REG | HAS_IBM_MISC },
176     { /* IBM CCAE */ 0x0ff0, 0x00, 0x04, 0xac,
177       HAS_MISC_REG | HAS_IBM_MISC },
178     { /* IBM CCAE */ 0x0ff0, 0x00, 0x06, 0x29,
179       HAS_MISC_REG | HAS_IBM_MISC },
180     { /* IBM FME */ 0x0374, 0x08, 0x00, 0x5a,
181       HAS_MISC_REG | HAS_IBM_MISC },
182     { /* IBM FME */ 0x0374, 0x00, 0x04, 0xac,
183       HAS_MISC_REG | HAS_IBM_MISC },
184     { /* Kansai KLA-PCM/T */ 0x0ff0, 0x00, 0x60, 0x87,
185       HAS_MISC_REG | HAS_IBM_MISC },
186     { /* NSC DP83903 */ 0x0374, 0x08, 0x00, 0x17,
187       HAS_MISC_REG | HAS_IBM_MISC },
188     { /* NSC DP83903 */ 0x0374, 0x00, 0xc0, 0xa8,
189       HAS_MISC_REG | HAS_IBM_MISC },
190     { /* NSC DP83903 */ 0x0374, 0x00, 0xa0, 0xb0,
191       HAS_MISC_REG | HAS_IBM_MISC },
192     { /* NSC DP83903 */ 0x0198, 0x00, 0x20, 0xe0,
193       HAS_MISC_REG | HAS_IBM_MISC },
194     { /* I-O DATA PCLA/T */ 0x0ff0, 0x00, 0xa0, 0xb0, 0 },
195     { /* Katron PE-520 */ 0x0110, 0x00, 0x40, 0xf6, 0 },
196     { /* Kingston KNE-PCM/x */ 0x0ff0, 0x00, 0xc0, 0xf0,
197       HAS_MISC_REG | HAS_IBM_MISC },
198     { /* Kingston KNE-PCM/x */ 0x0ff0, 0xe2, 0x0c, 0x0f,
199       HAS_MISC_REG | HAS_IBM_MISC },
200     { /* Kingston KNE-PC2 */ 0x0180, 0x00, 0xc0, 0xf0, 0 },
201     { /* Maxtech PCN2000 */ 0x5000, 0x00, 0x00, 0xe8, 0 },
202     { /* NDC Instant-Link */ 0x003a, 0x00, 0x80, 0xc6, 0 },
203     { /* NE2000 Compatible */ 0x0ff0, 0x00, 0xa0, 0x0c, 0 },
204     { /* Network General Sniffer */ 0x0ff0, 0x00, 0x00, 0x65,
205       HAS_MISC_REG | HAS_IBM_MISC },
206     { /* Panasonic VEL211 */ 0x0ff0, 0x00, 0x80, 0x45, 
207       HAS_MISC_REG | HAS_IBM_MISC },
208     { /* PreMax PE-200 */ 0x07f0, 0x00, 0x20, 0xe0, 0 },
209     { /* RPTI EP400 */ 0x0110, 0x00, 0x40, 0x95, 0 },
210     { /* SCM Ethernet */ 0x0ff0, 0x00, 0x20, 0xcb, 0 },
211     { /* Socket EA */ 0x4000, 0x00, 0xc0, 0x1b,
212       DELAY_OUTPUT | HAS_MISC_REG | USE_BIG_BUF },
213     { /* Socket LP-E CF+ */ 0x01c0, 0x00, 0xc0, 0x1b, 0 },
214     { /* SuperSocket RE450T */ 0x0110, 0x00, 0xe0, 0x98, 0 },
215     { /* Volktek NPL-402CT */ 0x0060, 0x00, 0x40, 0x05, 0 },
216     { /* NEC PC-9801N-J12 */ 0x0ff0, 0x00, 0x00, 0x4c, 0 },
217     { /* PCMCIA Technology OEM */ 0x01c8, 0x00, 0xa0, 0x0c, 0 }
218 };
219
220 #define NR_INFO         (sizeof(hw_info)/sizeof(hw_info_t))
221
222 static hw_info_t default_info = { 0, 0, 0, 0, 0 };
223 static hw_info_t dl10019_info = { 0, 0, 0, 0, IS_DL10019|HAS_MII };
224 static hw_info_t dl10022_info = { 0, 0, 0, 0, IS_DL10022|HAS_MII };
225
226 typedef struct pcnet_dev_t {
227     dev_link_t          link;
228     dev_node_t          node;
229     u_int               flags;
230     caddr_t             base;
231     struct timer_list   watchdog;
232     int                 stale, fast_poll;
233     u_char              phy_id;
234     u_char              eth_phy, pna_phy;
235     u_short             link_status;
236     u_long              mii_reset;
237 } pcnet_dev_t;
238
239 static inline pcnet_dev_t *PRIV(struct net_device *dev)
240 {
241         char *p = netdev_priv(dev);
242         return (pcnet_dev_t *)(p + sizeof(struct ei_device));
243 }
244
245 /*======================================================================
246
247     pcnet_attach() creates an "instance" of the driver, allocating
248     local data structures for one device.  The device is registered
249     with Card Services.
250
251 ======================================================================*/
252
253 static dev_link_t *pcnet_attach(void)
254 {
255     pcnet_dev_t *info;
256     dev_link_t *link;
257     struct net_device *dev;
258     client_reg_t client_reg;
259     int i, ret;
260
261     DEBUG(0, "pcnet_attach()\n");
262
263     /* Create new ethernet device */
264     dev = __alloc_ei_netdev(sizeof(pcnet_dev_t));
265     if (!dev) return NULL;
266     info = PRIV(dev);
267     link = &info->link;
268     link->priv = dev;
269
270     link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
271     link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
272     if (irq_list[0] == -1)
273         link->irq.IRQInfo2 = irq_mask;
274     else
275         for (i = 0; i < 4; i++)
276             link->irq.IRQInfo2 |= 1 << irq_list[i];
277     link->conf.Attributes = CONF_ENABLE_IRQ;
278     link->conf.IntType = INT_MEMORY_AND_IO;
279
280     SET_MODULE_OWNER(dev);
281     dev->open = &pcnet_open;
282     dev->stop = &pcnet_close;
283     dev->set_config = &set_config;
284
285     /* Register with Card Services */
286     link->next = dev_list;
287     dev_list = link;
288     client_reg.dev_info = &dev_info;
289     client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
290     client_reg.EventMask =
291         CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
292         CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
293         CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
294     client_reg.event_handler = &pcnet_event;
295     client_reg.Version = 0x0210;
296     client_reg.event_callback_args.client_data = link;
297     ret = pcmcia_register_client(&link->handle, &client_reg);
298     if (ret != CS_SUCCESS) {
299         cs_error(link->handle, RegisterClient, ret);
300         pcnet_detach(link);
301         return NULL;
302     }
303
304     return link;
305 } /* pcnet_attach */
306
307 /*======================================================================
308
309     This deletes a driver "instance".  The device is de-registered
310     with Card Services.  If it has been released, all local data
311     structures are freed.  Otherwise, the structures will be freed
312     when the device is released.
313
314 ======================================================================*/
315
316 static void pcnet_detach(dev_link_t *link)
317 {
318     struct net_device *dev = link->priv;
319     dev_link_t **linkp;
320
321     DEBUG(0, "pcnet_detach(0x%p)\n", link);
322
323     /* Locate device structure */
324     for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
325         if (*linkp == link) break;
326     if (*linkp == NULL)
327         return;
328
329     if (link->state & DEV_CONFIG)
330         pcnet_release(link);
331
332     if (link->handle)
333         pcmcia_deregister_client(link->handle);
334
335     /* Unlink device structure, free bits */
336     *linkp = link->next;
337     if (link->dev)
338         unregister_netdev(dev);
339     free_netdev(dev);
340 } /* pcnet_detach */
341
342 /*======================================================================
343
344     This probes for a card's hardware address, for card types that
345     encode this information in their CIS.
346
347 ======================================================================*/
348
349 static hw_info_t *get_hwinfo(dev_link_t *link)
350 {
351     struct net_device *dev = link->priv;
352     win_req_t req;
353     memreq_t mem;
354     u_char *base, *virt;
355     int i, j;
356
357     /* Allocate a small memory window */
358     req.Attributes = WIN_DATA_WIDTH_8|WIN_MEMORY_TYPE_AM|WIN_ENABLE;
359     req.Base = 0; req.Size = 0;
360     req.AccessSpeed = 0;
361     i = pcmcia_request_window(&link->handle, &req, &link->win);
362     if (i != CS_SUCCESS) {
363         cs_error(link->handle, RequestWindow, i);
364         return NULL;
365     }
366
367     virt = ioremap(req.Base, req.Size);
368     mem.Page = 0;
369     for (i = 0; i < NR_INFO; i++) {
370         mem.CardOffset = hw_info[i].offset & ~(req.Size-1);
371         pcmcia_map_mem_page(link->win, &mem);
372         base = &virt[hw_info[i].offset & (req.Size-1)];
373         if ((readb(base+0) == hw_info[i].a0) &&
374             (readb(base+2) == hw_info[i].a1) &&
375             (readb(base+4) == hw_info[i].a2))
376             break;
377     }
378     if (i < NR_INFO) {
379         for (j = 0; j < 6; j++)
380             dev->dev_addr[j] = readb(base + (j<<1));
381     }
382     
383     iounmap(virt);
384     j = pcmcia_release_window(link->win);
385     if (j != CS_SUCCESS)
386         cs_error(link->handle, ReleaseWindow, j);
387     return (i < NR_INFO) ? hw_info+i : NULL;
388 } /* get_hwinfo */
389
390 /*======================================================================
391
392     This probes for a card's hardware address by reading the PROM.
393     It checks the address against a list of known types, then falls
394     back to a simple NE2000 clone signature check.
395
396 ======================================================================*/
397
398 static hw_info_t *get_prom(dev_link_t *link)
399 {
400     struct net_device *dev = link->priv;
401     ioaddr_t ioaddr = dev->base_addr;
402     u_char prom[32];
403     int i, j;
404
405     /* This is lifted straight from drivers/net/ne.c */
406     struct {
407         u_char value, offset;
408     } program_seq[] = {
409         {E8390_NODMA+E8390_PAGE0+E8390_STOP, E8390_CMD}, /* Select page 0*/
410         {0x48,  EN0_DCFG},      /* Set byte-wide (0x48) access. */
411         {0x00,  EN0_RCNTLO},    /* Clear the count regs. */
412         {0x00,  EN0_RCNTHI},
413         {0x00,  EN0_IMR},       /* Mask completion irq. */
414         {0xFF,  EN0_ISR},
415         {E8390_RXOFF, EN0_RXCR},        /* 0x20  Set to monitor */
416         {E8390_TXOFF, EN0_TXCR},        /* 0x02  and loopback mode. */
417         {32,    EN0_RCNTLO},
418         {0x00,  EN0_RCNTHI},
419         {0x00,  EN0_RSARLO},    /* DMA starting at 0x0000. */
420         {0x00,  EN0_RSARHI},
421         {E8390_RREAD+E8390_START, E8390_CMD},
422     };
423
424     pcnet_reset_8390(dev);
425     mdelay(10);
426
427     for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++)
428         outb_p(program_seq[i].value, ioaddr + program_seq[i].offset);
429
430     for (i = 0; i < 32; i++)
431         prom[i] = inb(ioaddr + PCNET_DATAPORT);
432     for (i = 0; i < NR_INFO; i++) {
433         if ((prom[0] == hw_info[i].a0) &&
434             (prom[2] == hw_info[i].a1) &&
435             (prom[4] == hw_info[i].a2))
436             break;
437     }
438     if ((i < NR_INFO) || ((prom[28] == 0x57) && (prom[30] == 0x57))) {
439         for (j = 0; j < 6; j++)
440             dev->dev_addr[j] = prom[j<<1];
441         return (i < NR_INFO) ? hw_info+i : &default_info;
442     }
443     return NULL;
444 } /* get_prom */
445
446 /*======================================================================
447
448     For DL10019 based cards, like the Linksys EtherFast
449
450 ======================================================================*/
451
452 static hw_info_t *get_dl10019(dev_link_t *link)
453 {
454     struct net_device *dev = link->priv;
455     int i;
456     u_char sum;
457
458     for (sum = 0, i = 0x14; i < 0x1c; i++)
459         sum += inb_p(dev->base_addr + i);
460     if (sum != 0xff)
461         return NULL;
462     for (i = 0; i < 6; i++)
463         dev->dev_addr[i] = inb_p(dev->base_addr + 0x14 + i);
464     i = inb(dev->base_addr + 0x1f);
465     return ((i == 0x91)||(i == 0x99)) ? &dl10022_info : &dl10019_info;
466 }
467
468 /*======================================================================
469
470     For Asix AX88190 based cards
471
472 ======================================================================*/
473
474 static hw_info_t *get_ax88190(dev_link_t *link)
475 {
476     struct net_device *dev = link->priv;
477     ioaddr_t ioaddr = dev->base_addr;
478     int i, j;
479
480     /* Not much of a test, but the alternatives are messy */
481     if (link->conf.ConfigBase != 0x03c0)
482         return NULL;
483
484     outb_p(0x01, ioaddr + EN0_DCFG);    /* Set word-wide access. */
485     outb_p(0x00, ioaddr + EN0_RSARLO);  /* DMA starting at 0x0400. */
486     outb_p(0x04, ioaddr + EN0_RSARHI);
487     outb_p(E8390_RREAD+E8390_START, ioaddr + E8390_CMD);
488
489     for (i = 0; i < 6; i += 2) {
490         j = inw(ioaddr + PCNET_DATAPORT);
491         dev->dev_addr[i] = j & 0xff;
492         dev->dev_addr[i+1] = j >> 8;
493     }
494     printk(KERN_NOTICE "pcnet_cs: this is an AX88190 card!\n");
495     printk(KERN_NOTICE "pcnet_cs: use axnet_cs instead.\n");
496     return NULL;
497 }
498
499 /*======================================================================
500
501     This should be totally unnecessary... but when we can't figure
502     out the hardware address any other way, we'll let the user hard
503     wire it when the module is initialized.
504
505 ======================================================================*/
506
507 static hw_info_t *get_hwired(dev_link_t *link)
508 {
509     struct net_device *dev = link->priv;
510     int i;
511
512     for (i = 0; i < 6; i++)
513         if (hw_addr[i] != 0) break;
514     if (i == 6)
515         return NULL;
516
517     for (i = 0; i < 6; i++)
518         dev->dev_addr[i] = hw_addr[i];
519
520     return &default_info;
521 } /* get_hwired */
522
523 /*======================================================================
524
525     pcnet_config() is scheduled to run after a CARD_INSERTION event
526     is received, to configure the PCMCIA socket, and to make the
527     ethernet device available to the system.
528
529 ======================================================================*/
530
531 #define CS_CHECK(fn, ret) \
532 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
533
534 static int try_io_port(dev_link_t *link)
535 {
536     int j, ret;
537     if (link->io.NumPorts1 == 32) {
538         link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
539         if (link->io.NumPorts2 > 0) {
540             /* for master/slave multifunction cards */
541             link->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
542             link->irq.Attributes = 
543                 IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
544         }
545     } else {
546         /* This should be two 16-port windows */
547         link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
548         link->io.Attributes2 = IO_DATA_PATH_WIDTH_16;
549     }
550     if (link->io.BasePort1 == 0) {
551         link->io.IOAddrLines = 16;
552         for (j = 0; j < 0x400; j += 0x20) {
553             link->io.BasePort1 = j ^ 0x300;
554             link->io.BasePort2 = (j ^ 0x300) + 0x10;
555             ret = pcmcia_request_io(link->handle, &link->io);
556             if (ret == CS_SUCCESS) return ret;
557         }
558         return ret;
559     } else {
560         return pcmcia_request_io(link->handle, &link->io);
561     }
562 }
563
564 static void pcnet_config(dev_link_t *link)
565 {
566     client_handle_t handle = link->handle;
567     struct net_device *dev = link->priv;
568     pcnet_dev_t *info = PRIV(dev);
569     tuple_t tuple;
570     cisparse_t parse;
571     int i, last_ret, last_fn, start_pg, stop_pg, cm_offset;
572     int manfid = 0, prodid = 0, has_shmem = 0;
573     u_short buf[64];
574     config_info_t conf;
575     hw_info_t *hw_info;
576
577     DEBUG(0, "pcnet_config(0x%p)\n", link);
578
579     tuple.Attributes = 0;
580     tuple.TupleData = (cisdata_t *)buf;
581     tuple.TupleDataMax = sizeof(buf);
582     tuple.TupleOffset = 0;
583     tuple.DesiredTuple = CISTPL_CONFIG;
584     CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
585     CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
586     CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
587     link->conf.ConfigBase = parse.config.base;
588     link->conf.Present = parse.config.rmask[0];
589
590     /* Configure card */
591     link->state |= DEV_CONFIG;
592
593     /* Look up current Vcc */
594     CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
595     link->conf.Vcc = conf.Vcc;
596
597     tuple.DesiredTuple = CISTPL_MANFID;
598     tuple.Attributes = TUPLE_RETURN_COMMON;
599     if ((pcmcia_get_first_tuple(handle, &tuple) == CS_SUCCESS) &&
600         (pcmcia_get_tuple_data(handle, &tuple) == CS_SUCCESS)) {
601         manfid = le16_to_cpu(buf[0]);
602         prodid = le16_to_cpu(buf[1]);
603     }
604     
605     tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
606     tuple.Attributes = 0;
607     CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
608     while (last_ret == CS_SUCCESS) {
609         cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
610         cistpl_io_t *io = &(parse.cftable_entry.io);
611         
612         if (pcmcia_get_tuple_data(handle, &tuple) != 0 ||
613                         pcmcia_parse_tuple(handle, &tuple, &parse) != 0 ||
614                         cfg->index == 0 || cfg->io.nwin == 0)
615                 goto next_entry;
616         
617         link->conf.ConfigIndex = cfg->index;
618         /* For multifunction cards, by convention, we configure the
619            network function with window 0, and serial with window 1 */
620         if (io->nwin > 1) {
621             i = (io->win[1].len > io->win[0].len);
622             link->io.BasePort2 = io->win[1-i].base;
623             link->io.NumPorts2 = io->win[1-i].len;
624         } else {
625             i = link->io.NumPorts2 = 0;
626         }
627         has_shmem = ((cfg->mem.nwin == 1) &&
628                      (cfg->mem.win[0].len >= 0x4000));
629         link->io.BasePort1 = io->win[i].base;
630         link->io.NumPorts1 = io->win[i].len;
631         link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
632         if (link->io.NumPorts1 + link->io.NumPorts2 >= 32) {
633             last_ret = try_io_port(link);
634             if (last_ret == CS_SUCCESS) break;
635         }
636     next_entry:
637         last_ret = pcmcia_get_next_tuple(handle, &tuple);
638     }
639     if (last_ret != CS_SUCCESS) {
640         cs_error(handle, RequestIO, last_ret);
641         goto failed;
642     }
643
644     CS_CHECK(RequestIRQ, pcmcia_request_irq(handle, &link->irq));
645     
646     if (link->io.NumPorts2 == 8) {
647         link->conf.Attributes |= CONF_ENABLE_SPKR;
648         link->conf.Status = CCSR_AUDIO_ENA;
649     }
650     if ((manfid == MANFID_IBM) &&
651         (prodid == PRODID_IBM_HOME_AND_AWAY))
652         link->conf.ConfigIndex |= 0x10;
653     
654     CS_CHECK(RequestConfiguration, pcmcia_request_configuration(handle, &link->conf));
655     dev->irq = link->irq.AssignedIRQ;
656     dev->base_addr = link->io.BasePort1;
657     if (info->flags & HAS_MISC_REG) {
658         if ((if_port == 1) || (if_port == 2))
659             dev->if_port = if_port;
660         else
661             printk(KERN_NOTICE "pcnet_cs: invalid if_port requested\n");
662     } else {
663         dev->if_port = 0;
664     }
665
666     hw_info = get_hwinfo(link);
667     if (hw_info == NULL)
668         hw_info = get_prom(link);
669     if (hw_info == NULL)
670         hw_info = get_dl10019(link);
671     if (hw_info == NULL)
672         hw_info = get_ax88190(link);
673     if (hw_info == NULL)
674         hw_info = get_hwired(link);
675     
676     if (hw_info == NULL) {
677         printk(KERN_NOTICE "pcnet_cs: unable to read hardware net"
678                " address for io base %#3lx\n", dev->base_addr);
679         goto failed;
680     }
681
682     info->flags = hw_info->flags;
683     /* Check for user overrides */
684     info->flags |= (delay_output) ? DELAY_OUTPUT : 0;
685     if ((manfid == MANFID_SOCKET) &&
686         ((prodid == PRODID_SOCKET_LPE) ||
687          (prodid == PRODID_SOCKET_LPE_CF) ||
688          (prodid == PRODID_SOCKET_EIO)))
689         info->flags &= ~USE_BIG_BUF;
690     if (!use_big_buf)
691         info->flags &= ~USE_BIG_BUF;
692     
693     if (info->flags & USE_BIG_BUF) {
694         start_pg = SOCKET_START_PG;
695         stop_pg = SOCKET_STOP_PG;
696         cm_offset = 0x10000;
697     } else {
698         start_pg = PCNET_START_PG;
699         stop_pg = PCNET_STOP_PG;
700         cm_offset = 0;
701     }
702
703     /* has_shmem is ignored if use_shmem != -1 */
704     if ((use_shmem == 0) || (!has_shmem && (use_shmem == -1)) ||
705         (setup_shmem_window(link, start_pg, stop_pg, cm_offset) != 0))
706         setup_dma_config(link, start_pg, stop_pg);
707
708     ei_status.name = "NE2000";
709     ei_status.word16 = 1;
710     ei_status.reset_8390 = &pcnet_reset_8390;
711
712     SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
713
714     if (info->flags & (IS_DL10019|IS_DL10022)) {
715         u_char id = inb(dev->base_addr + 0x1a);
716         dev->do_ioctl = &ei_ioctl;
717         mii_phy_probe(dev);
718         if ((id == 0x30) && !info->pna_phy && (info->eth_phy == 4))
719             info->eth_phy = 0;
720     }
721
722     link->dev = &info->node;
723     link->state &= ~DEV_CONFIG_PENDING;
724
725 #ifdef CONFIG_NET_POLL_CONTROLLER
726     dev->poll_controller = ei_poll;
727 #endif
728
729     if (register_netdev(dev) != 0) {
730         printk(KERN_NOTICE "pcnet_cs: register_netdev() failed\n");
731         link->dev = NULL;
732         goto failed;
733     }
734
735     strcpy(info->node.dev_name, dev->name);
736
737     if (info->flags & (IS_DL10019|IS_DL10022)) {
738         u_char id = inb(dev->base_addr + 0x1a);
739         printk(KERN_INFO "%s: NE2000 (DL100%d rev %02x): ",
740                dev->name, ((info->flags & IS_DL10022) ? 22 : 19), id);
741         if (info->pna_phy)
742             printk("PNA, ");
743     } else {
744         printk(KERN_INFO "%s: NE2000 Compatible: ", dev->name);
745     }
746     printk("io %#3lx, irq %d,", dev->base_addr, dev->irq);
747     if (info->flags & USE_SHMEM)
748         printk (" mem %#5lx,", dev->mem_start);
749     if (info->flags & HAS_MISC_REG)
750         printk(" %s xcvr,", if_names[dev->if_port]);
751     printk(" hw_addr ");
752     for (i = 0; i < 6; i++)
753         printk("%02X%s", dev->dev_addr[i], ((i<5) ? ":" : "\n"));
754     return;
755
756 cs_failed:
757     cs_error(link->handle, last_fn, last_ret);
758 failed:
759     pcnet_release(link);
760     link->state &= ~DEV_CONFIG_PENDING;
761     return;
762 } /* pcnet_config */
763
764 /*======================================================================
765
766     After a card is removed, pcnet_release() will unregister the net
767     device, and release the PCMCIA configuration.  If the device is
768     still open, this will be postponed until it is closed.
769
770 ======================================================================*/
771
772 static void pcnet_release(dev_link_t *link)
773 {
774     pcnet_dev_t *info = PRIV(link->priv);
775
776     DEBUG(0, "pcnet_release(0x%p)\n", link);
777
778     if (info->flags & USE_SHMEM) {
779         iounmap(info->base);
780         pcmcia_release_window(link->win);
781     }
782     pcmcia_release_configuration(link->handle);
783     pcmcia_release_io(link->handle, &link->io);
784     pcmcia_release_irq(link->handle, &link->irq);
785
786     link->state &= ~DEV_CONFIG;
787 }
788
789 /*======================================================================
790
791     The card status event handler.  Mostly, this schedules other
792     stuff to run after an event is received.  A CARD_REMOVAL event
793     also sets some flags to discourage the net drivers from trying
794     to talk to the card any more.
795
796 ======================================================================*/
797
798 static int pcnet_event(event_t event, int priority,
799                        event_callback_args_t *args)
800 {
801     dev_link_t *link = args->client_data;
802     struct net_device *dev = link->priv;
803
804     DEBUG(2, "pcnet_event(0x%06x)\n", event);
805
806     switch (event) {
807     case CS_EVENT_CARD_REMOVAL:
808         link->state &= ~DEV_PRESENT;
809         if (link->state & DEV_CONFIG) {
810             netif_device_detach(dev);
811             pcnet_release(link);
812         }
813         break;
814     case CS_EVENT_CARD_INSERTION:
815         link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
816         pcnet_config(link);
817         break;
818     case CS_EVENT_PM_SUSPEND:
819         link->state |= DEV_SUSPEND;
820         /* Fall through... */
821     case CS_EVENT_RESET_PHYSICAL:
822         if (link->state & DEV_CONFIG) {
823             if (link->open)
824                 netif_device_detach(dev);
825             pcmcia_release_configuration(link->handle);
826         }
827         break;
828     case CS_EVENT_PM_RESUME:
829         link->state &= ~DEV_SUSPEND;
830         /* Fall through... */
831     case CS_EVENT_CARD_RESET:
832         if (link->state & DEV_CONFIG) {
833             pcmcia_request_configuration(link->handle, &link->conf);
834             if (link->open) {
835                 pcnet_reset_8390(dev);
836                 NS8390_init(dev, 1);
837                 netif_device_attach(dev);
838             }
839         }
840         break;
841     }
842     return 0;
843 } /* pcnet_event */
844
845 /*======================================================================
846
847     MII interface support for DL10019 and DL10022 based cards
848
849     On the DL10019, the MII IO direction bit is 0x10; on the DL10022
850     it is 0x20.  Setting both bits seems to work on both card types.
851
852 ======================================================================*/
853
854 #define DLINK_GPIO              0x1c
855 #define DLINK_DIAG              0x1d
856 #define DLINK_EEPROM            0x1e
857
858 #define MDIO_SHIFT_CLK          0x80
859 #define MDIO_DATA_OUT           0x40
860 #define MDIO_DIR_WRITE          0x30
861 #define MDIO_DATA_WRITE0        (MDIO_DIR_WRITE)
862 #define MDIO_DATA_WRITE1        (MDIO_DIR_WRITE | MDIO_DATA_OUT)
863 #define MDIO_DATA_READ          0x10
864 #define MDIO_MASK               0x0f
865
866 static void mdio_sync(ioaddr_t addr)
867 {
868     int bits, mask = inb(addr) & MDIO_MASK;
869     for (bits = 0; bits < 32; bits++) {
870         outb(mask | MDIO_DATA_WRITE1, addr);
871         outb(mask | MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, addr);
872     }
873 }
874
875 static int mdio_read(ioaddr_t addr, int phy_id, int loc)
876 {
877     u_int cmd = (0x06<<10)|(phy_id<<5)|loc;
878     int i, retval = 0, mask = inb(addr) & MDIO_MASK;
879
880     mdio_sync(addr);
881     for (i = 13; i >= 0; i--) {
882         int dat = (cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
883         outb(mask | dat, addr);
884         outb(mask | dat | MDIO_SHIFT_CLK, addr);
885     }
886     for (i = 19; i > 0; i--) {
887         outb(mask, addr);
888         retval = (retval << 1) | ((inb(addr) & MDIO_DATA_READ) != 0);
889         outb(mask | MDIO_SHIFT_CLK, addr);
890     }
891     return (retval>>1) & 0xffff;
892 }
893
894 static void mdio_write(ioaddr_t addr, int phy_id, int loc, int value)
895 {
896     u_int cmd = (0x05<<28)|(phy_id<<23)|(loc<<18)|(1<<17)|value;
897     int i, mask = inb(addr) & MDIO_MASK;
898
899     mdio_sync(addr);
900     for (i = 31; i >= 0; i--) {
901         int dat = (cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
902         outb(mask | dat, addr);
903         outb(mask | dat | MDIO_SHIFT_CLK, addr);
904     }
905     for (i = 1; i >= 0; i--) {
906         outb(mask, addr);
907         outb(mask | MDIO_SHIFT_CLK, addr);
908     }
909 }
910
911 static void mdio_reset(ioaddr_t addr, int phy_id)
912 {
913     outb_p(0x08, addr);
914     outb_p(0x0c, addr);
915     outb_p(0x08, addr);
916     outb_p(0x0c, addr);
917     outb_p(0x00, addr);
918 }
919
920 /*======================================================================
921
922     EEPROM access routines for DL10019 and DL10022 based cards
923
924 ======================================================================*/
925
926 #define EE_EEP          0x40
927 #define EE_ASIC         0x10
928 #define EE_CS           0x08
929 #define EE_CK           0x04
930 #define EE_DO           0x02
931 #define EE_DI           0x01
932 #define EE_ADOT         0x01    /* DataOut for ASIC */
933 #define EE_READ_CMD     0x06
934
935 #define DL19FDUPLX      0x0400  /* DL10019 Full duplex mode */
936
937 static int read_eeprom(ioaddr_t ioaddr, int location)
938 {
939     int i, retval = 0;
940     ioaddr_t ee_addr = ioaddr + DLINK_EEPROM;
941     int read_cmd = location | (EE_READ_CMD << 8);
942
943     outb(0, ee_addr);
944     outb(EE_EEP|EE_CS, ee_addr);
945
946     /* Shift the read command bits out. */
947     for (i = 10; i >= 0; i--) {
948         short dataval = (read_cmd & (1 << i)) ? EE_DO : 0;
949         outb_p(EE_EEP|EE_CS|dataval, ee_addr);
950         outb_p(EE_EEP|EE_CS|dataval|EE_CK, ee_addr);
951     }
952     outb(EE_EEP|EE_CS, ee_addr);
953
954     for (i = 16; i > 0; i--) {
955         outb_p(EE_EEP|EE_CS | EE_CK, ee_addr);
956         retval = (retval << 1) | ((inb(ee_addr) & EE_DI) ? 1 : 0);
957         outb_p(EE_EEP|EE_CS, ee_addr);
958     }
959
960     /* Terminate the EEPROM access. */
961     outb(0, ee_addr);
962     return retval;
963 }
964
965 /*
966     The internal ASIC registers can be changed by EEPROM READ access
967     with EE_ASIC bit set.
968     In ASIC mode, EE_ADOT is used to output the data to the ASIC.
969 */
970
971 static void write_asic(ioaddr_t ioaddr, int location, short asic_data)
972 {
973         int i;
974         ioaddr_t ee_addr = ioaddr + DLINK_EEPROM;
975         short dataval;
976         int read_cmd = location | (EE_READ_CMD << 8);
977
978         asic_data |= read_eeprom(ioaddr, location);
979
980         outb(0, ee_addr);
981         outb(EE_ASIC|EE_CS|EE_DI, ee_addr);
982
983         read_cmd = read_cmd >> 1;
984
985         /* Shift the read command bits out. */
986         for (i = 9; i >= 0; i--) {
987                 dataval = (read_cmd & (1 << i)) ? EE_DO : 0;
988                 outb_p(EE_ASIC|EE_CS|EE_DI|dataval, ee_addr);
989                 outb_p(EE_ASIC|EE_CS|EE_DI|dataval|EE_CK, ee_addr);
990                 outb_p(EE_ASIC|EE_CS|EE_DI|dataval, ee_addr);
991         }
992         // sync
993         outb(EE_ASIC|EE_CS, ee_addr);
994         outb(EE_ASIC|EE_CS|EE_CK, ee_addr);
995         outb(EE_ASIC|EE_CS, ee_addr);
996
997         for (i = 15; i >= 0; i--) {
998                 dataval = (asic_data & (1 << i)) ? EE_ADOT : 0;
999                 outb_p(EE_ASIC|EE_CS|dataval, ee_addr);
1000                 outb_p(EE_ASIC|EE_CS|dataval|EE_CK, ee_addr);
1001                 outb_p(EE_ASIC|EE_CS|dataval, ee_addr);
1002         }
1003
1004         /* Terminate the ASIC access. */
1005         outb(EE_ASIC|EE_DI, ee_addr);
1006         outb(EE_ASIC|EE_DI| EE_CK, ee_addr);
1007         outb(EE_ASIC|EE_DI, ee_addr);
1008
1009         outb(0, ee_addr);
1010 }
1011
1012 /*====================================================================*/
1013
1014 static void set_misc_reg(struct net_device *dev)
1015 {
1016     ioaddr_t nic_base = dev->base_addr;
1017     pcnet_dev_t *info = PRIV(dev);
1018     u_char tmp;
1019     
1020     if (info->flags & HAS_MISC_REG) {
1021         tmp = inb_p(nic_base + PCNET_MISC) & ~3;
1022         if (dev->if_port == 2)
1023             tmp |= 1;
1024         if (info->flags & USE_BIG_BUF)
1025             tmp |= 2;
1026         if (info->flags & HAS_IBM_MISC)
1027             tmp |= 8;
1028         outb_p(tmp, nic_base + PCNET_MISC);
1029     }
1030     if (info->flags & IS_DL10022) {
1031         if (info->flags & HAS_MII) {
1032             mdio_reset(nic_base + DLINK_GPIO, info->eth_phy);
1033             /* Restart MII autonegotiation */
1034             mdio_write(nic_base + DLINK_GPIO, info->eth_phy, 0, 0x0000);
1035             mdio_write(nic_base + DLINK_GPIO, info->eth_phy, 0, 0x1200);
1036             info->mii_reset = jiffies;
1037         } else {
1038             outb(full_duplex ? 4 : 0, nic_base + DLINK_DIAG);
1039         }
1040     }
1041 }
1042
1043 /*====================================================================*/
1044
1045 static void mii_phy_probe(struct net_device *dev)
1046 {
1047     pcnet_dev_t *info = PRIV(dev);
1048     ioaddr_t mii_addr = dev->base_addr + DLINK_GPIO;
1049     int i;
1050     u_int tmp, phyid;
1051
1052     for (i = 31; i >= 0; i--) {
1053         tmp = mdio_read(mii_addr, i, 1);
1054         if ((tmp == 0) || (tmp == 0xffff))
1055             continue;
1056         tmp = mdio_read(mii_addr, i, MII_PHYID_REG1);
1057         phyid = tmp << 16;
1058         phyid |= mdio_read(mii_addr, i, MII_PHYID_REG2);
1059         phyid &= MII_PHYID_REV_MASK;
1060         DEBUG(0, "%s: MII at %d is 0x%08x\n", dev->name, i, phyid);
1061         if (phyid == AM79C9XX_HOME_PHY) {
1062             info->pna_phy = i;
1063         } else if (phyid != AM79C9XX_ETH_PHY) {
1064             info->eth_phy = i;
1065         }
1066     }
1067 }
1068
1069 static int pcnet_open(struct net_device *dev)
1070 {
1071     pcnet_dev_t *info = PRIV(dev);
1072     dev_link_t *link = &info->link;
1073     
1074     DEBUG(2, "pcnet_open('%s')\n", dev->name);
1075
1076     if (!DEV_OK(link))
1077         return -ENODEV;
1078
1079     link->open++;
1080
1081     set_misc_reg(dev);
1082     request_irq(dev->irq, ei_irq_wrapper, SA_SHIRQ, dev_info, dev);
1083
1084     info->phy_id = info->eth_phy;
1085     info->link_status = 0x00;
1086     init_timer(&info->watchdog);
1087     info->watchdog.function = &ei_watchdog;
1088     info->watchdog.data = (u_long)dev;
1089     info->watchdog.expires = jiffies + HZ;
1090     add_timer(&info->watchdog);
1091
1092     return ei_open(dev);
1093 } /* pcnet_open */
1094
1095 /*====================================================================*/
1096
1097 static int pcnet_close(struct net_device *dev)
1098 {
1099     pcnet_dev_t *info = PRIV(dev);
1100     dev_link_t *link = &info->link;
1101
1102     DEBUG(2, "pcnet_close('%s')\n", dev->name);
1103
1104     ei_close(dev);
1105     free_irq(dev->irq, dev);
1106     
1107     link->open--;
1108     netif_stop_queue(dev);
1109     del_timer_sync(&info->watchdog);
1110
1111     return 0;
1112 } /* pcnet_close */
1113
1114 /*======================================================================
1115
1116     Hard reset the card.  This used to pause for the same period that
1117     a 8390 reset command required, but that shouldn't be necessary.
1118
1119 ======================================================================*/
1120
1121 static void pcnet_reset_8390(struct net_device *dev)
1122 {
1123     ioaddr_t nic_base = dev->base_addr;
1124     int i;
1125
1126     ei_status.txing = ei_status.dmaing = 0;
1127
1128     outb_p(E8390_NODMA+E8390_PAGE0+E8390_STOP, nic_base + E8390_CMD);
1129
1130     outb(inb(nic_base + PCNET_RESET), nic_base + PCNET_RESET);
1131
1132     for (i = 0; i < 100; i++) {
1133         if ((inb_p(nic_base+EN0_ISR) & ENISR_RESET) != 0)
1134             break;
1135         udelay(100);
1136     }
1137     outb_p(ENISR_RESET, nic_base + EN0_ISR); /* Ack intr. */
1138     
1139     if (i == 100)
1140         printk(KERN_ERR "%s: pcnet_reset_8390() did not complete.\n",
1141                dev->name);
1142     set_misc_reg(dev);
1143     
1144 } /* pcnet_reset_8390 */
1145
1146 /*====================================================================*/
1147
1148 static int set_config(struct net_device *dev, struct ifmap *map)
1149 {
1150     pcnet_dev_t *info = PRIV(dev);
1151     if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
1152         if (!(info->flags & HAS_MISC_REG))
1153             return -EOPNOTSUPP;
1154         else if ((map->port < 1) || (map->port > 2))
1155             return -EINVAL;
1156         dev->if_port = map->port;
1157         printk(KERN_INFO "%s: switched to %s port\n",
1158                dev->name, if_names[dev->if_port]);
1159         NS8390_init(dev, 1);
1160     }
1161     return 0;
1162 }
1163
1164 /*====================================================================*/
1165
1166 static irqreturn_t ei_irq_wrapper(int irq, void *dev_id, struct pt_regs *regs)
1167 {
1168     struct net_device *dev = dev_id;
1169     pcnet_dev_t *info = PRIV(dev);
1170     irqreturn_t ret = ei_interrupt(irq, dev_id, regs);
1171
1172     if (ret == IRQ_HANDLED)
1173             info->stale = 0;
1174     return ret;
1175 }
1176
1177 static void ei_watchdog(u_long arg)
1178 {
1179     struct net_device *dev = (struct net_device *)arg;
1180     pcnet_dev_t *info = PRIV(dev);
1181     ioaddr_t nic_base = dev->base_addr;
1182     ioaddr_t mii_addr = nic_base + DLINK_GPIO;
1183     u_short link;
1184
1185     if (!netif_device_present(dev)) goto reschedule;
1186
1187     /* Check for pending interrupt with expired latency timer: with
1188        this, we can limp along even if the interrupt is blocked */
1189     outb_p(E8390_NODMA+E8390_PAGE0, nic_base + E8390_CMD);
1190     if (info->stale++ && (inb_p(nic_base + EN0_ISR) & ENISR_ALL)) {
1191         if (!info->fast_poll)
1192             printk(KERN_INFO "%s: interrupt(s) dropped!\n", dev->name);
1193         ei_irq_wrapper(dev->irq, dev, NULL);
1194         info->fast_poll = HZ;
1195     }
1196     if (info->fast_poll) {
1197         info->fast_poll--;
1198         info->watchdog.expires = jiffies + 1;
1199         add_timer(&info->watchdog);
1200         return;
1201     }
1202
1203     if (!(info->flags & HAS_MII))
1204         goto reschedule;
1205
1206     mdio_read(mii_addr, info->phy_id, 1);
1207     link = mdio_read(mii_addr, info->phy_id, 1);
1208     if (!link || (link == 0xffff)) {
1209         if (info->eth_phy) {
1210             info->phy_id = info->eth_phy = 0;
1211         } else {
1212             printk(KERN_INFO "%s: MII is missing!\n", dev->name);
1213             info->flags &= ~HAS_MII;
1214         }
1215         goto reschedule;
1216     }
1217
1218     link &= 0x0004;
1219     if (link != info->link_status) {
1220         u_short p = mdio_read(mii_addr, info->phy_id, 5);
1221         printk(KERN_INFO "%s: %s link beat\n", dev->name,
1222                (link) ? "found" : "lost");
1223         if (link && (info->flags & IS_DL10022)) {
1224             /* Disable collision detection on full duplex links */
1225             outb((p & 0x0140) ? 4 : 0, nic_base + DLINK_DIAG);
1226         } else if (link && (info->flags & IS_DL10019)) {
1227             /* Disable collision detection on full duplex links */
1228             write_asic(dev->base_addr, 4, (p & 0x140) ? DL19FDUPLX : 0);
1229         }
1230         if (link) {
1231             if (info->phy_id == info->eth_phy) {
1232                 if (p)
1233                     printk(KERN_INFO "%s: autonegotiation complete: "
1234                            "%sbaseT-%cD selected\n", dev->name,
1235                            ((p & 0x0180) ? "100" : "10"),
1236                            ((p & 0x0140) ? 'F' : 'H'));
1237                 else
1238                     printk(KERN_INFO "%s: link partner did not "
1239                            "autonegotiate\n", dev->name);
1240             }
1241             NS8390_init(dev, 1);
1242         }
1243         info->link_status = link;
1244     }
1245     if (info->pna_phy && time_after(jiffies, info->mii_reset + 6*HZ)) {
1246         link = mdio_read(mii_addr, info->eth_phy, 1) & 0x0004;
1247         if (((info->phy_id == info->pna_phy) && link) ||
1248             ((info->phy_id != info->pna_phy) && !link)) {
1249             /* isolate this MII and try flipping to the other one */
1250             mdio_write(mii_addr, info->phy_id, 0, 0x0400);
1251             info->phy_id ^= info->pna_phy ^ info->eth_phy;
1252             printk(KERN_INFO "%s: switched to %s transceiver\n", dev->name,
1253                    (info->phy_id == info->eth_phy) ? "ethernet" : "PNA");
1254             mdio_write(mii_addr, info->phy_id, 0,
1255                        (info->phy_id == info->eth_phy) ? 0x1000 : 0);
1256             info->link_status = 0;
1257             info->mii_reset = jiffies;
1258         }
1259     }
1260
1261 reschedule:
1262     info->watchdog.expires = jiffies + HZ;
1263     add_timer(&info->watchdog);
1264 }
1265
1266 /*====================================================================*/
1267
1268 static void netdev_get_drvinfo(struct net_device *dev,
1269                                struct ethtool_drvinfo *info)
1270 {
1271         strcpy(info->driver, "pcnet_cs");
1272 }
1273
1274 static struct ethtool_ops netdev_ethtool_ops = {
1275         .get_drvinfo            = netdev_get_drvinfo,
1276 };
1277
1278 /*====================================================================*/
1279
1280
1281 static int ei_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1282 {
1283     pcnet_dev_t *info = PRIV(dev);
1284     u16 *data = (u16 *)&rq->ifr_ifru;
1285     ioaddr_t mii_addr = dev->base_addr + DLINK_GPIO;
1286     switch (cmd) {
1287     case SIOCGMIIPHY:
1288         data[0] = info->phy_id;
1289     case SIOCGMIIREG:           /* Read MII PHY register. */
1290         data[3] = mdio_read(mii_addr, data[0], data[1] & 0x1f);
1291         return 0;
1292     case SIOCSMIIREG:           /* Write MII PHY register. */
1293         if (!capable(CAP_NET_ADMIN))
1294             return -EPERM;
1295         mdio_write(mii_addr, data[0], data[1] & 0x1f, data[2]);
1296         return 0;
1297     }
1298     return -EOPNOTSUPP;
1299 }
1300
1301 /*====================================================================*/
1302
1303 static void dma_get_8390_hdr(struct net_device *dev,
1304                              struct e8390_pkt_hdr *hdr,
1305                              int ring_page)
1306 {
1307     ioaddr_t nic_base = dev->base_addr;
1308
1309     if (ei_status.dmaing) {
1310         printk(KERN_NOTICE "%s: DMAing conflict in dma_block_input."
1311                "[DMAstat:%1x][irqlock:%1x]\n",
1312                dev->name, ei_status.dmaing, ei_status.irqlock);
1313         return;
1314     }
1315     
1316     ei_status.dmaing |= 0x01;
1317     outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base + PCNET_CMD);
1318     outb_p(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
1319     outb_p(0, nic_base + EN0_RCNTHI);
1320     outb_p(0, nic_base + EN0_RSARLO);           /* On page boundary */
1321     outb_p(ring_page, nic_base + EN0_RSARHI);
1322     outb_p(E8390_RREAD+E8390_START, nic_base + PCNET_CMD);
1323
1324     insw(nic_base + PCNET_DATAPORT, hdr,
1325             sizeof(struct e8390_pkt_hdr)>>1);
1326     /* Fix for big endian systems */
1327     hdr->count = le16_to_cpu(hdr->count);
1328
1329     outb_p(ENISR_RDC, nic_base + EN0_ISR);      /* Ack intr. */
1330     ei_status.dmaing &= ~0x01;
1331 }
1332
1333 /*====================================================================*/
1334
1335 static void dma_block_input(struct net_device *dev, int count,
1336                             struct sk_buff *skb, int ring_offset)
1337 {
1338     ioaddr_t nic_base = dev->base_addr;
1339     int xfer_count = count;
1340     char *buf = skb->data;
1341
1342 #ifdef PCMCIA_DEBUG
1343     if ((ei_debug > 4) && (count != 4))
1344         printk(KERN_DEBUG "%s: [bi=%d]\n", dev->name, count+4);
1345 #endif
1346     if (ei_status.dmaing) {
1347         printk(KERN_NOTICE "%s: DMAing conflict in dma_block_input."
1348                "[DMAstat:%1x][irqlock:%1x]\n",
1349                dev->name, ei_status.dmaing, ei_status.irqlock);
1350         return;
1351     }
1352     ei_status.dmaing |= 0x01;
1353     outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base + PCNET_CMD);
1354     outb_p(count & 0xff, nic_base + EN0_RCNTLO);
1355     outb_p(count >> 8, nic_base + EN0_RCNTHI);
1356     outb_p(ring_offset & 0xff, nic_base + EN0_RSARLO);
1357     outb_p(ring_offset >> 8, nic_base + EN0_RSARHI);
1358     outb_p(E8390_RREAD+E8390_START, nic_base + PCNET_CMD);
1359
1360     insw(nic_base + PCNET_DATAPORT,buf,count>>1);
1361     if (count & 0x01)
1362         buf[count-1] = inb(nic_base + PCNET_DATAPORT), xfer_count++;
1363
1364     /* This was for the ALPHA version only, but enough people have
1365        encountering problems that it is still here. */
1366 #ifdef PCMCIA_DEBUG
1367     if (ei_debug > 4) {         /* DMA termination address check... */
1368         int addr, tries = 20;
1369         do {
1370             /* DON'T check for 'inb_p(EN0_ISR) & ENISR_RDC' here
1371                -- it's broken for Rx on some cards! */
1372             int high = inb_p(nic_base + EN0_RSARHI);
1373             int low = inb_p(nic_base + EN0_RSARLO);
1374             addr = (high << 8) + low;
1375             if (((ring_offset + xfer_count) & 0xff) == (addr & 0xff))
1376                 break;
1377         } while (--tries > 0);
1378         if (tries <= 0)
1379             printk(KERN_NOTICE "%s: RX transfer address mismatch,"
1380                    "%#4.4x (expected) vs. %#4.4x (actual).\n",
1381                    dev->name, ring_offset + xfer_count, addr);
1382     }
1383 #endif
1384     outb_p(ENISR_RDC, nic_base + EN0_ISR);      /* Ack intr. */
1385     ei_status.dmaing &= ~0x01;
1386 } /* dma_block_input */
1387
1388 /*====================================================================*/
1389
1390 static void dma_block_output(struct net_device *dev, int count,
1391                              const u_char *buf, const int start_page)
1392 {
1393     ioaddr_t nic_base = dev->base_addr;
1394     pcnet_dev_t *info = PRIV(dev);
1395 #ifdef PCMCIA_DEBUG
1396     int retries = 0;
1397 #endif
1398     u_long dma_start;
1399
1400 #ifdef PCMCIA_DEBUG
1401     if (ei_debug > 4)
1402         printk(KERN_DEBUG "%s: [bo=%d]\n", dev->name, count);
1403 #endif
1404
1405     /* Round the count up for word writes.  Do we need to do this?
1406        What effect will an odd byte count have on the 8390?
1407        I should check someday. */
1408     if (count & 0x01)
1409         count++;
1410     if (ei_status.dmaing) {
1411         printk(KERN_NOTICE "%s: DMAing conflict in dma_block_output."
1412                "[DMAstat:%1x][irqlock:%1x]\n",
1413                dev->name, ei_status.dmaing, ei_status.irqlock);
1414         return;
1415     }
1416     ei_status.dmaing |= 0x01;
1417     /* We should already be in page 0, but to be safe... */
1418     outb_p(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base+PCNET_CMD);
1419
1420 #ifdef PCMCIA_DEBUG
1421   retry:
1422 #endif
1423
1424     outb_p(ENISR_RDC, nic_base + EN0_ISR);
1425
1426     /* Now the normal output. */
1427     outb_p(count & 0xff, nic_base + EN0_RCNTLO);
1428     outb_p(count >> 8,   nic_base + EN0_RCNTHI);
1429     outb_p(0x00, nic_base + EN0_RSARLO);
1430     outb_p(start_page, nic_base + EN0_RSARHI);
1431
1432     outb_p(E8390_RWRITE+E8390_START, nic_base + PCNET_CMD);
1433     outsw(nic_base + PCNET_DATAPORT, buf, count>>1);
1434
1435     dma_start = jiffies;
1436
1437 #ifdef PCMCIA_DEBUG
1438     /* This was for the ALPHA version only, but enough people have
1439        encountering problems that it is still here. */
1440     if (ei_debug > 4) { /* DMA termination address check... */
1441         int addr, tries = 20;
1442         do {
1443             int high = inb_p(nic_base + EN0_RSARHI);
1444             int low = inb_p(nic_base + EN0_RSARLO);
1445             addr = (high << 8) + low;
1446             if ((start_page << 8) + count == addr)
1447                 break;
1448         } while (--tries > 0);
1449         if (tries <= 0) {
1450             printk(KERN_NOTICE "%s: Tx packet transfer address mismatch,"
1451                    "%#4.4x (expected) vs. %#4.4x (actual).\n",
1452                    dev->name, (start_page << 8) + count, addr);
1453             if (retries++ == 0)
1454                 goto retry;
1455         }
1456     }
1457 #endif
1458
1459     while ((inb_p(nic_base + EN0_ISR) & ENISR_RDC) == 0)
1460         if (time_after(jiffies, dma_start + PCNET_RDC_TIMEOUT)) {
1461             printk(KERN_NOTICE "%s: timeout waiting for Tx RDC.\n",
1462                    dev->name);
1463             pcnet_reset_8390(dev);
1464             NS8390_init(dev, 1);
1465             break;
1466         }
1467
1468     outb_p(ENISR_RDC, nic_base + EN0_ISR);      /* Ack intr. */
1469     if (info->flags & DELAY_OUTPUT)
1470         udelay((long)delay_time);
1471     ei_status.dmaing &= ~0x01;
1472 }
1473
1474 /*====================================================================*/
1475
1476 static int setup_dma_config(dev_link_t *link, int start_pg,
1477                             int stop_pg)
1478 {
1479     struct net_device *dev = link->priv;
1480
1481     ei_status.tx_start_page = start_pg;
1482     ei_status.rx_start_page = start_pg + TX_PAGES;
1483     ei_status.stop_page = stop_pg;
1484
1485     /* set up block i/o functions */
1486     ei_status.get_8390_hdr = &dma_get_8390_hdr;
1487     ei_status.block_input = &dma_block_input;
1488     ei_status.block_output = &dma_block_output;
1489
1490     return 0;
1491 }
1492
1493 /*====================================================================*/
1494
1495 static void copyin(u_char *dest, u_char *src, int c)
1496 {
1497     u_short *d = (u_short *)dest, *s = (u_short *)src;
1498     int odd;
1499
1500     if (c <= 0)
1501         return;
1502     odd = (c & 1); c >>= 1;
1503
1504     if (c) {
1505         do { *d++ = __raw_readw(s++); } while (--c);
1506     }
1507     /* get last byte by fetching a word and masking */
1508     if (odd)
1509         *((u_char *)d) = readw(s) & 0xff;
1510 }
1511
1512 static void copyout(u_char *dest, const u_char *src, int c)
1513 {
1514     u_short *d = (u_short *)dest, *s = (u_short *)src;
1515     int odd;
1516
1517     if (c <= 0)
1518         return;
1519     odd = (c & 1); c >>= 1;
1520
1521     if (c) {
1522         do { __raw_writew(*s++, d++); } while (--c);
1523     }
1524     /* copy last byte doing a read-modify-write */
1525     if (odd)
1526         writew((readw(d) & 0xff00) | *(u_char *)s, d);
1527 }
1528
1529 /*====================================================================*/
1530
1531 static void shmem_get_8390_hdr(struct net_device *dev,
1532                                struct e8390_pkt_hdr *hdr,
1533                                int ring_page)
1534 {
1535     void *xfer_start = (void *)(ei_status.rmem_start + (ring_page << 8)
1536                                 - (ei_status.rx_start_page << 8));
1537     
1538     copyin((void *)hdr, xfer_start, sizeof(struct e8390_pkt_hdr));
1539     /* Fix for big endian systems */
1540     hdr->count = le16_to_cpu(hdr->count);
1541 }
1542
1543 /*====================================================================*/
1544
1545 static void shmem_block_input(struct net_device *dev, int count,
1546                               struct sk_buff *skb, int ring_offset)
1547 {
1548     void *xfer_start = (void *)(ei_status.rmem_start + ring_offset
1549                                 - (ei_status.rx_start_page << 8));
1550     char *buf = skb->data;
1551     
1552     if (xfer_start + count > (void *)ei_status.rmem_end) {
1553         /* We must wrap the input move. */
1554         int semi_count = (void*)ei_status.rmem_end - xfer_start;
1555         copyin(buf, xfer_start, semi_count);
1556         buf += semi_count;
1557         ring_offset = ei_status.rx_start_page << 8;
1558         xfer_start = (void *)ei_status.rmem_start;
1559         count -= semi_count;
1560     }
1561     copyin(buf, xfer_start, count);
1562 }
1563
1564 /*====================================================================*/
1565
1566 static void shmem_block_output(struct net_device *dev, int count,
1567                                const u_char *buf, const int start_page)
1568 {
1569     void *shmem = (void *)dev->mem_start + (start_page << 8);
1570     shmem -= ei_status.tx_start_page << 8;
1571     copyout(shmem, buf, count);
1572 }
1573
1574 /*====================================================================*/
1575
1576 static int setup_shmem_window(dev_link_t *link, int start_pg,
1577                               int stop_pg, int cm_offset)
1578 {
1579     struct net_device *dev = link->priv;
1580     pcnet_dev_t *info = PRIV(dev);
1581     win_req_t req;
1582     memreq_t mem;
1583     int i, window_size, offset, last_ret, last_fn;
1584
1585     window_size = (stop_pg - start_pg) << 8;
1586     if (window_size > 32 * 1024)
1587         window_size = 32 * 1024;
1588
1589     /* Make sure it's a power of two.  */
1590     while ((window_size & (window_size - 1)) != 0)
1591         window_size += window_size & ~(window_size - 1);
1592
1593     /* Allocate a memory window */
1594     req.Attributes = WIN_DATA_WIDTH_16|WIN_MEMORY_TYPE_CM|WIN_ENABLE;
1595     req.Attributes |= WIN_USE_WAIT;
1596     req.Base = 0; req.Size = window_size;
1597     req.AccessSpeed = mem_speed;
1598     CS_CHECK(RequestWindow, pcmcia_request_window(&link->handle, &req, &link->win));
1599
1600     mem.CardOffset = (start_pg << 8) + cm_offset;
1601     offset = mem.CardOffset % window_size;
1602     mem.CardOffset -= offset;
1603     mem.Page = 0;
1604     CS_CHECK(MapMemPage, pcmcia_map_mem_page(link->win, &mem));
1605
1606     /* Try scribbling on the buffer */
1607     info->base = ioremap(req.Base, window_size);
1608     for (i = 0; i < (TX_PAGES<<8); i += 2)
1609         __raw_writew((i>>1), info->base+offset+i);
1610     udelay(100);
1611     for (i = 0; i < (TX_PAGES<<8); i += 2)
1612         if (__raw_readw(info->base+offset+i) != (i>>1)) break;
1613     pcnet_reset_8390(dev);
1614     if (i != (TX_PAGES<<8)) {
1615         iounmap(info->base);
1616         pcmcia_release_window(link->win);
1617         info->base = NULL; link->win = NULL;
1618         goto failed;
1619     }
1620     
1621     dev->mem_start = (u_long)info->base + offset;
1622     ei_status.rmem_start = dev->mem_start + (TX_PAGES<<8);
1623     dev->mem_end = ei_status.rmem_end = (u_long)info->base + req.Size;
1624
1625     ei_status.tx_start_page = start_pg;
1626     ei_status.rx_start_page = start_pg + TX_PAGES;
1627     ei_status.stop_page = start_pg + ((req.Size - offset) >> 8);
1628
1629     /* set up block i/o functions */
1630     ei_status.get_8390_hdr = &shmem_get_8390_hdr;
1631     ei_status.block_input = &shmem_block_input;
1632     ei_status.block_output = &shmem_block_output;
1633
1634     info->flags |= USE_SHMEM;
1635     return 0;
1636
1637 cs_failed:
1638     cs_error(link->handle, last_fn, last_ret);
1639 failed:
1640     return 1;
1641 }
1642
1643 /*====================================================================*/
1644
1645 static struct pcmcia_driver pcnet_driver = {
1646         .drv            = {
1647                 .name   = "pcnet_cs",
1648         },
1649         .attach         = pcnet_attach,
1650         .detach         = pcnet_detach,
1651         .owner          = THIS_MODULE,
1652 };
1653
1654 static int __init init_pcnet_cs(void)
1655 {
1656     return pcmcia_register_driver(&pcnet_driver);
1657 }
1658
1659 static void __exit exit_pcnet_cs(void)
1660 {
1661     DEBUG(0, "pcnet_cs: unloading\n");
1662     pcmcia_unregister_driver(&pcnet_driver);
1663     while (dev_list != NULL)
1664         pcnet_detach(dev_list);
1665 }
1666
1667 module_init(init_pcnet_cs);
1668 module_exit(exit_pcnet_cs);