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