vserver 2.0 rc7
[linux-2.6.git] / drivers / net / pcmcia / xirc2ps_cs.c
1 /* [xirc2ps_cs.c wk 03.11.99] (1.40 1999/11/18 00:06:03)
2  * Xircom CreditCard Ethernet Adapter IIps driver
3  * Xircom Realport 10/100 (RE-100) driver 
4  *
5  * This driver supports various Xircom CreditCard Ethernet adapters
6  * including the CE2, CE IIps, RE-10, CEM28, CEM33, CE33, CEM56,
7  * CE3-100, CE3B, RE-100, REM10BT, and REM56G-100.
8  *
9  * 2000-09-24 <psheer@icon.co.za> The Xircom CE3B-100 may not
10  * autodetect the media properly. In this case use the
11  * if_port=1 (for 10BaseT) or if_port=4 (for 100BaseT) options
12  * to force the media type.
13  * 
14  * Written originally by Werner Koch based on David Hinds' skeleton of the
15  * PCMCIA driver.
16  *
17  * Copyright (c) 1997,1998 Werner Koch (dd9jn)
18  *
19  * This driver is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 2 of the License, or
22  * (at your option) any later version.
23  *
24  * It is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program; if not, write to the Free Software
31  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
32  *
33  *
34  * ALTERNATIVELY, this driver may be distributed under the terms of
35  * the following license, in which case the provisions of this license
36  * are required INSTEAD OF the GNU General Public License.  (This clause
37  * is necessary due to a potential bad interaction between the GPL and
38  * the restrictions contained in a BSD-style copyright.)
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, and the entire permission notice in its entirety,
45  *    including the disclaimer of warranties.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. The name of the author may not be used to endorse or promote
50  *    products derived from this software without specific prior
51  *    written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
54  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
55  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
56  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
57  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
58  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
59  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
61  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
63  * OF THE POSSIBILITY OF SUCH DAMAGE.
64  */
65
66 #include <linux/module.h>
67 #include <linux/kernel.h>
68 #include <linux/init.h>
69 #include <linux/ptrace.h>
70 #include <linux/slab.h>
71 #include <linux/string.h>
72 #include <linux/timer.h>
73 #include <linux/interrupt.h>
74 #include <linux/in.h>
75 #include <linux/delay.h>
76 #include <linux/ethtool.h>
77 #include <linux/netdevice.h>
78 #include <linux/etherdevice.h>
79 #include <linux/skbuff.h>
80 #include <linux/if_arp.h>
81 #include <linux/ioport.h>
82 #include <linux/bitops.h>
83
84 #include <pcmcia/version.h>
85 #include <pcmcia/cs_types.h>
86 #include <pcmcia/cs.h>
87 #include <pcmcia/cistpl.h>
88 #include <pcmcia/cisreg.h>
89 #include <pcmcia/ciscode.h>
90
91 #include <asm/io.h>
92 #include <asm/system.h>
93 #include <asm/uaccess.h>
94
95 #ifndef MANFID_COMPAQ
96   #define MANFID_COMPAQ            0x0138
97   #define MANFID_COMPAQ2           0x0183  /* is this correct? */
98 #endif
99
100 #include <pcmcia/ds.h>
101
102 /* Time in jiffies before concluding Tx hung */
103 #define TX_TIMEOUT      ((400*HZ)/1000)
104
105 /****************
106  * Some constants used to access the hardware
107  */
108
109 /* Register offsets and value constans */
110 #define XIRCREG_CR  0   /* Command register (wr) */
111 enum xirc_cr {
112     TransmitPacket = 0x01,
113     SoftReset = 0x02,
114     EnableIntr = 0x04,
115     ForceIntr  = 0x08,
116     ClearTxFIFO = 0x10,
117     ClearRxOvrun = 0x20,
118     RestartTx    = 0x40
119 };
120 #define XIRCREG_ESR 0   /* Ethernet status register (rd) */
121 enum xirc_esr {
122     FullPktRcvd = 0x01, /* full packet in receive buffer */
123     PktRejected = 0x04, /* a packet has been rejected */
124     TxPktPend = 0x08,   /* TX Packet Pending */
125     IncorPolarity = 0x10,
126     MediaSelect = 0x20  /* set if TP, clear if AUI */
127 };
128 #define XIRCREG_PR  1   /* Page Register select */
129 #define XIRCREG_EDP 4   /* Ethernet Data Port Register */
130 #define XIRCREG_ISR 6   /* Ethernet Interrupt Status Register */
131 enum xirc_isr {
132     TxBufOvr = 0x01,    /* TX Buffer Overflow */
133     PktTxed  = 0x02,    /* Packet Transmitted */
134     MACIntr  = 0x04,    /* MAC Interrupt occurred */
135     TxResGrant = 0x08,  /* Tx Reservation Granted */
136     RxFullPkt = 0x20,   /* Rx Full Packet */
137     RxPktRej  = 0x40,   /* Rx Packet Rejected */
138     ForcedIntr= 0x80    /* Forced Interrupt */
139 };
140 #define XIRCREG1_IMR0 12 /* Ethernet Interrupt Mask Register (on page 1)*/
141 #define XIRCREG1_IMR1 13
142 #define XIRCREG0_TSO  8  /* Transmit Space Open Register (on page 0)*/
143 #define XIRCREG0_TRS  10 /* Transmit reservation Size Register (page 0)*/
144 #define XIRCREG0_DO   12 /* Data Offset Register (page 0) (wr) */
145 #define XIRCREG0_RSR  12 /* Receive Status Register (page 0) (rd) */
146 enum xirc_rsr {
147     PhyPkt = 0x01,      /* set:physical packet, clear: multicast packet */
148     BrdcstPkt = 0x02,   /* set if it is a broadcast packet */
149     PktTooLong = 0x04,  /* set if packet length > 1518 */
150     AlignErr = 0x10,    /* incorrect CRC and last octet not complete */
151     CRCErr = 0x20,      /* incorrect CRC and last octet is complete */
152     PktRxOk = 0x80      /* received ok */
153 };
154 #define XIRCREG0_PTR 13 /* packets transmitted register (rd) */
155 #define XIRCREG0_RBC 14 /* receive byte count regsister (rd) */
156 #define XIRCREG1_ECR 14 /* ethernet configurationn register */
157 enum xirc_ecr {
158     FullDuplex = 0x04,  /* enable full duplex mode */
159     LongTPMode = 0x08,  /* adjust for longer lengths of TP cable */
160     DisablePolCor = 0x10,/* disable auto polarity correction */
161     DisableLinkPulse = 0x20, /* disable link pulse generation */
162     DisableAutoTx = 0x40, /* disable auto-transmit */
163 };
164 #define XIRCREG2_RBS 8  /* receive buffer start register */
165 #define XIRCREG2_LED 10 /* LED Configuration register */
166 /* values for the leds:    Bits 2-0 for led 1
167  *  0 disabled             Bits 5-3 for led 2
168  *  1 collision
169  *  2 noncollision
170  *  3 link_detected
171  *  4 incor_polarity
172  *  5 jabber
173  *  6 auto_assertion
174  *  7 rx_tx_activity
175  */
176 #define XIRCREG2_MSR 12 /* Mohawk specific register */
177
178 #define XIRCREG4_GPR0 8 /* General Purpose Register 0 */
179 #define XIRCREG4_GPR1 9 /* General Purpose Register 1 */
180 #define XIRCREG2_GPR2 13 /* General Purpose Register 2 (page2!)*/
181 #define XIRCREG4_BOV 10 /* Bonding Version Register */
182 #define XIRCREG4_LMA 12 /* Local Memory Address Register */
183 #define XIRCREG4_LMD 14 /* Local Memory Data Port */
184 /* MAC register can only by accessed with 8 bit operations */
185 #define XIRCREG40_CMD0 8    /* Command Register (wr) */
186 enum xirc_cmd {             /* Commands */
187     Transmit = 0x01,
188     EnableRecv = 0x04,
189     DisableRecv = 0x08,
190     Abort = 0x10,
191     Online = 0x20,
192     IntrAck = 0x40,
193     Offline = 0x80
194 };
195 #define XIRCREG5_RHSA0  10  /* Rx Host Start Address */
196 #define XIRCREG40_RXST0 9   /* Receive Status Register */
197 #define XIRCREG40_TXST0 11  /* Transmit Status Register 0 */
198 #define XIRCREG40_TXST1 12  /* Transmit Status Register 10 */
199 #define XIRCREG40_RMASK0 13  /* Receive Mask Register */
200 #define XIRCREG40_TMASK0 14  /* Transmit Mask Register 0 */
201 #define XIRCREG40_TMASK1 15  /* Transmit Mask Register 0 */
202 #define XIRCREG42_SWC0  8   /* Software Configuration 0 */
203 #define XIRCREG42_SWC1  9   /* Software Configuration 1 */
204 #define XIRCREG42_BOC   10  /* Back-Off Configuration */
205 #define XIRCREG44_TDR0  8   /* Time Domain Reflectometry 0 */
206 #define XIRCREG44_TDR1  9   /* Time Domain Reflectometry 1 */
207 #define XIRCREG44_RXBC_LO 10 /* Rx Byte Count 0 (rd) */
208 #define XIRCREG44_RXBC_HI 11 /* Rx Byte Count 1 (rd) */
209 #define XIRCREG45_REV    15 /* Revision Register (rd) */
210 #define XIRCREG50_IA    8   /* Individual Address (8-13) */
211
212 static char *if_names[] = { "Auto", "10BaseT", "10Base2", "AUI", "100BaseT" };
213
214 /****************
215  * All the PCMCIA modules use PCMCIA_DEBUG to control debugging.  If
216  * you do not define PCMCIA_DEBUG at all, all the debug code will be
217  * left out.  If you compile with PCMCIA_DEBUG=0, the debug code will
218  * be present but disabled -- but it can then be enabled for specific
219  * modules at load time with a 'pc_debug=#' option to insmod.
220  */
221 #ifdef PCMCIA_DEBUG
222 static int pc_debug = PCMCIA_DEBUG;
223 module_param(pc_debug, int, 0);
224 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KDBG_XIRC args)
225 #else
226 #define DEBUG(n, args...)
227 #endif
228
229 #define KDBG_XIRC KERN_DEBUG   "xirc2ps_cs: "
230 #define KERR_XIRC KERN_ERR     "xirc2ps_cs: "
231 #define KWRN_XIRC KERN_WARNING "xirc2ps_cs: "
232 #define KNOT_XIRC KERN_NOTICE  "xirc2ps_cs: "
233 #define KINF_XIRC KERN_INFO    "xirc2ps_cs: "
234
235 /* card types */
236 #define XIR_UNKNOWN  0  /* unknown: not supported */
237 #define XIR_CE       1  /* (prodid 1) different hardware: not supported */
238 #define XIR_CE2      2  /* (prodid 2) */
239 #define XIR_CE3      3  /* (prodid 3) */
240 #define XIR_CEM      4  /* (prodid 1) different hardware: not supported */
241 #define XIR_CEM2     5  /* (prodid 2) */
242 #define XIR_CEM3     6  /* (prodid 3) */
243 #define XIR_CEM33    7  /* (prodid 4) */
244 #define XIR_CEM56M   8  /* (prodid 5) */
245 #define XIR_CEM56    9  /* (prodid 6) */
246 #define XIR_CM28    10  /* (prodid 3) modem only: not supported here */
247 #define XIR_CM33    11  /* (prodid 4) modem only: not supported here */
248 #define XIR_CM56    12  /* (prodid 5) modem only: not supported here */
249 #define XIR_CG      13  /* (prodid 1) GSM modem only: not supported */
250 #define XIR_CBE     14  /* (prodid 1) cardbus ethernet: not supported */
251 /*====================================================================*/
252
253 /* Module parameters */
254
255 MODULE_DESCRIPTION("Xircom PCMCIA ethernet driver");
256 MODULE_LICENSE("Dual MPL/GPL");
257
258 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0)
259
260 INT_MODULE_PARM(if_port,        0);
261 INT_MODULE_PARM(full_duplex,    0);
262 INT_MODULE_PARM(do_sound,       1);
263 INT_MODULE_PARM(lockup_hack,    0);  /* anti lockup hack */
264
265 /*====================================================================*/
266
267 /* We do not process more than these number of bytes during one
268  * interrupt. (Of course we receive complete packets, so this is not
269  * an exact value).
270  * Something between 2000..22000; first value gives best interrupt latency,
271  * the second enables the usage of the complete on-chip buffer. We use the
272  * high value as the initial value.
273  */
274 static unsigned maxrx_bytes = 22000;
275
276 /* MII management prototypes */
277 static void mii_idle(kio_addr_t ioaddr);
278 static void mii_putbit(kio_addr_t ioaddr, unsigned data);
279 static int  mii_getbit(kio_addr_t ioaddr);
280 static void mii_wbits(kio_addr_t ioaddr, unsigned data, int len);
281 static unsigned mii_rd(kio_addr_t ioaddr, u_char phyaddr, u_char phyreg);
282 static void mii_wr(kio_addr_t ioaddr, u_char phyaddr, u_char phyreg,
283                    unsigned data, int len);
284
285 /*
286  * The event() function is this driver's Card Services event handler.
287  * It will be called by Card Services when an appropriate card status
288  * event is received.  The config() and release() entry points are
289  * used to configure or release a socket, in response to card insertion
290  * and ejection events.  They are invoked from the event handler.
291  */
292
293 static int has_ce2_string(dev_link_t * link);
294 static void xirc2ps_config(dev_link_t * link);
295 static void xirc2ps_release(dev_link_t * link);
296 static int xirc2ps_event(event_t event, int priority,
297                          event_callback_args_t * args);
298
299 /****************
300  * The attach() and detach() entry points are used to create and destroy
301  * "instances" of the driver, where each instance represents everything
302  * needed to manage one actual PCMCIA card.
303  */
304
305 static dev_link_t *xirc2ps_attach(void);
306 static void xirc2ps_detach(dev_link_t *);
307
308 /****************
309  * You'll also need to prototype all the functions that will actually
310  * be used to talk to your device.  See 'pcmem_cs' for a good example
311  * of a fully self-sufficient driver; the other drivers rely more or
312  * less on other parts of the kernel.
313  */
314
315 static irqreturn_t xirc2ps_interrupt(int irq, void *dev_id, struct pt_regs *regs);
316
317 /*
318  * The dev_info variable is the "key" that is used to match up this
319  * device driver with appropriate cards, through the card configuration
320  * database.
321  */
322
323 static dev_info_t dev_info = "xirc2ps_cs";
324
325 /****************
326  * A linked list of "instances" of the device.  Each actual
327  * PCMCIA card corresponds to one device instance, and is described
328  * by one dev_link_t structure (defined in ds.h).
329  *
330  * You may not want to use a linked list for this -- for example, the
331  * memory card driver uses an array of dev_link_t pointers, where minor
332  * device numbers are used to derive the corresponding array index.
333  */
334
335 static dev_link_t *dev_list;
336
337 /****************
338  * A dev_link_t structure has fields for most things that are needed
339  * to keep track of a socket, but there will usually be some device
340  * specific information that also needs to be kept track of.  The
341  * 'priv' pointer in a dev_link_t structure can be used to point to
342  * a device-specific private data structure, like this.
343  *
344  * A driver needs to provide a dev_node_t structure for each device
345  * on a card.  In some cases, there is only one device per card (for
346  * example, ethernet cards, modems).  In other cases, there may be
347  * many actual or logical devices (SCSI adapters, memory cards with
348  * multiple partitions).  The dev_node_t structures need to be kept
349  * in a linked list starting at the 'dev' field of a dev_link_t
350  * structure.  We allocate them in the card's private data structure,
351  * because they generally can't be allocated dynamically.
352  */
353
354 typedef struct local_info_t {
355     dev_link_t link;
356     dev_node_t node;
357     struct net_device_stats stats;
358     int card_type;
359     int probe_port;
360     int silicon; /* silicon revision. 0=old CE2, 1=Scipper, 4=Mohawk */
361     int mohawk;  /* a CE3 type card */
362     int dingo;   /* a CEM56 type card */
363     int new_mii; /* has full 10baseT/100baseT MII */
364     int modem;   /* is a multi function card (i.e with a modem) */
365     void __iomem *dingo_ccr; /* only used for CEM56 cards */
366     unsigned last_ptr_value; /* last packets transmitted value */
367     const char *manf_str;
368 } local_info_t;
369
370 /****************
371  * Some more prototypes
372  */
373 static int do_start_xmit(struct sk_buff *skb, struct net_device *dev);
374 static void do_tx_timeout(struct net_device *dev);
375 static struct net_device_stats *do_get_stats(struct net_device *dev);
376 static void set_addresses(struct net_device *dev);
377 static void set_multicast_list(struct net_device *dev);
378 static int set_card_type(dev_link_t *link, const void *s);
379 static int do_config(struct net_device *dev, struct ifmap *map);
380 static int do_open(struct net_device *dev);
381 static int do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
382 static struct ethtool_ops netdev_ethtool_ops;
383 static void hardreset(struct net_device *dev);
384 static void do_reset(struct net_device *dev, int full);
385 static int init_mii(struct net_device *dev);
386 static void do_powerdown(struct net_device *dev);
387 static int do_stop(struct net_device *dev);
388
389 /*=============== Helper functions =========================*/
390 static int
391 first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
392 {
393         int err;
394
395         if ((err = pcmcia_get_first_tuple(handle, tuple)) == 0 &&
396                         (err = pcmcia_get_tuple_data(handle, tuple)) == 0)
397                 err = pcmcia_parse_tuple(handle, tuple, parse);
398         return err;
399 }
400
401 static int
402 next_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
403 {
404         int err;
405
406         if ((err = pcmcia_get_next_tuple(handle, tuple)) == 0 &&
407                         (err = pcmcia_get_tuple_data(handle, tuple)) == 0)
408                 err = pcmcia_parse_tuple(handle, tuple, parse);
409         return err;
410 }
411
412 #define SelectPage(pgnr)   outb((pgnr), ioaddr + XIRCREG_PR)
413 #define GetByte(reg)       ((unsigned)inb(ioaddr + (reg)))
414 #define GetWord(reg)       ((unsigned)inw(ioaddr + (reg)))
415 #define PutByte(reg,value) outb((value), ioaddr+(reg))
416 #define PutWord(reg,value) outw((value), ioaddr+(reg))
417
418 /*====== Functions used for debugging =================================*/
419 #if defined(PCMCIA_DEBUG) && 0 /* reading regs may change system status */
420 static void
421 PrintRegisters(struct net_device *dev)
422 {
423     kio_addr_t ioaddr = dev->base_addr;
424
425     if (pc_debug > 1) {
426         int i, page;
427
428         printk(KDBG_XIRC "Register  common: ");
429         for (i = 0; i < 8; i++)
430             printk(" %2.2x", GetByte(i));
431         printk("\n");
432         for (page = 0; page <= 8; page++) {
433             printk(KDBG_XIRC "Register page %2x: ", page);
434             SelectPage(page);
435             for (i = 8; i < 16; i++)
436                 printk(" %2.2x", GetByte(i));
437             printk("\n");
438         }
439         for (page=0x40 ; page <= 0x5f; page++) {
440             if (page == 0x43 || (page >= 0x46 && page <= 0x4f)
441                 || (page >= 0x51 && page <=0x5e))
442                 continue;
443             printk(KDBG_XIRC "Register page %2x: ", page);
444             SelectPage(page);
445             for (i = 8; i < 16; i++)
446                 printk(" %2.2x", GetByte(i));
447             printk("\n");
448         }
449     }
450 }
451 #endif /* PCMCIA_DEBUG */
452
453 /*============== MII Management functions ===============*/
454
455 /****************
456  * Turn around for read
457  */
458 static void
459 mii_idle(kio_addr_t ioaddr)
460 {
461     PutByte(XIRCREG2_GPR2, 0x04|0); /* drive MDCK low */
462     udelay(1);
463     PutByte(XIRCREG2_GPR2, 0x04|1); /* and drive MDCK high */
464     udelay(1);
465 }
466
467 /****************
468  * Write a bit to MDI/O
469  */
470 static void
471 mii_putbit(kio_addr_t ioaddr, unsigned data)
472 {
473   #if 1
474     if (data) {
475         PutByte(XIRCREG2_GPR2, 0x0c|2|0); /* set MDIO */
476         udelay(1);
477         PutByte(XIRCREG2_GPR2, 0x0c|2|1); /* and drive MDCK high */
478         udelay(1);
479     } else {
480         PutByte(XIRCREG2_GPR2, 0x0c|0|0); /* clear MDIO */
481         udelay(1);
482         PutByte(XIRCREG2_GPR2, 0x0c|0|1); /* and drive MDCK high */
483         udelay(1);
484     }
485   #else
486     if (data) {
487         PutWord(XIRCREG2_GPR2-1, 0x0e0e);
488         udelay(1);
489         PutWord(XIRCREG2_GPR2-1, 0x0f0f);
490         udelay(1);
491     } else {
492         PutWord(XIRCREG2_GPR2-1, 0x0c0c);
493         udelay(1);
494         PutWord(XIRCREG2_GPR2-1, 0x0d0d);
495         udelay(1);
496     }
497   #endif
498 }
499
500 /****************
501  * Get a bit from MDI/O
502  */
503 static int
504 mii_getbit(kio_addr_t ioaddr)
505 {
506     unsigned d;
507
508     PutByte(XIRCREG2_GPR2, 4|0); /* drive MDCK low */
509     udelay(1);
510     d = GetByte(XIRCREG2_GPR2); /* read MDIO */
511     PutByte(XIRCREG2_GPR2, 4|1); /* drive MDCK high again */
512     udelay(1);
513     return d & 0x20; /* read MDIO */
514 }
515
516 static void
517 mii_wbits(kio_addr_t ioaddr, unsigned data, int len)
518 {
519     unsigned m = 1 << (len-1);
520     for (; m; m >>= 1)
521         mii_putbit(ioaddr, data & m);
522 }
523
524 static unsigned
525 mii_rd(kio_addr_t ioaddr,       u_char phyaddr, u_char phyreg)
526 {
527     int i;
528     unsigned data=0, m;
529
530     SelectPage(2);
531     for (i=0; i < 32; i++)              /* 32 bit preamble */
532         mii_putbit(ioaddr, 1);
533     mii_wbits(ioaddr, 0x06, 4);         /* Start and opcode for read */
534     mii_wbits(ioaddr, phyaddr, 5);      /* PHY address to be accessed */
535     mii_wbits(ioaddr, phyreg, 5);       /* PHY register to read */
536     mii_idle(ioaddr);                   /* turn around */
537     mii_getbit(ioaddr);
538
539     for (m = 1<<15; m; m >>= 1)
540         if (mii_getbit(ioaddr))
541             data |= m;
542     mii_idle(ioaddr);
543     return data;
544 }
545
546 static void
547 mii_wr(kio_addr_t ioaddr, u_char phyaddr, u_char phyreg, unsigned data, int len)
548 {
549     int i;
550
551     SelectPage(2);
552     for (i=0; i < 32; i++)              /* 32 bit preamble */
553         mii_putbit(ioaddr, 1);
554     mii_wbits(ioaddr, 0x05, 4);         /* Start and opcode for write */
555     mii_wbits(ioaddr, phyaddr, 5);      /* PHY address to be accessed */
556     mii_wbits(ioaddr, phyreg, 5);       /* PHY Register to write */
557     mii_putbit(ioaddr, 1);              /* turn around */
558     mii_putbit(ioaddr, 0);
559     mii_wbits(ioaddr, data, len);       /* And write the data */
560     mii_idle(ioaddr);
561 }
562
563 /*============= Main bulk of functions  =========================*/
564
565 /****************
566  * xirc2ps_attach() creates an "instance" of the driver, allocating
567  * local data structures for one device.  The device is registered
568  * with Card Services.
569  *
570  * The dev_link structure is initialized, but we don't actually
571  * configure the card at this point -- we wait until we receive a
572  * card insertion event.
573  */
574
575 static dev_link_t *
576 xirc2ps_attach(void)
577 {
578     client_reg_t client_reg;
579     dev_link_t *link;
580     struct net_device *dev;
581     local_info_t *local;
582     int err;
583
584     DEBUG(0, "attach()\n");
585
586     /* Allocate the device structure */
587     dev = alloc_etherdev(sizeof(local_info_t));
588     if (!dev)
589             return NULL;
590     local = netdev_priv(dev);
591     link = &local->link;
592     link->priv = dev;
593
594     /* General socket configuration */
595     link->conf.Attributes = CONF_ENABLE_IRQ;
596     link->conf.Vcc = 50;
597     link->conf.IntType = INT_MEMORY_AND_IO;
598     link->conf.ConfigIndex = 1;
599     link->conf.Present = PRESENT_OPTION;
600     link->irq.Handler = xirc2ps_interrupt;
601     link->irq.Instance = dev;
602
603     /* Fill in card specific entries */
604     SET_MODULE_OWNER(dev);
605     dev->hard_start_xmit = &do_start_xmit;
606     dev->set_config = &do_config;
607     dev->get_stats = &do_get_stats;
608     dev->do_ioctl = &do_ioctl;
609     SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
610     dev->set_multicast_list = &set_multicast_list;
611     dev->open = &do_open;
612     dev->stop = &do_stop;
613 #ifdef HAVE_TX_TIMEOUT
614     dev->tx_timeout = do_tx_timeout;
615     dev->watchdog_timeo = TX_TIMEOUT;
616 #endif
617
618     /* Register with Card Services */
619     link->next = dev_list;
620     dev_list = link;
621     client_reg.dev_info = &dev_info;
622     client_reg.EventMask =
623         CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
624         CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
625         CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
626     client_reg.event_handler = &xirc2ps_event;
627     client_reg.Version = 0x0210;
628     client_reg.event_callback_args.client_data = link;
629     if ((err = pcmcia_register_client(&link->handle, &client_reg))) {
630         cs_error(link->handle, RegisterClient, err);
631         xirc2ps_detach(link);
632         return NULL;
633     }
634
635     return link;
636 } /* xirc2ps_attach */
637
638 /****************
639  *  This deletes a driver "instance".  The device is de-registered
640  *  with Card Services.  If it has been released, all local data
641  *  structures are freed.  Otherwise, the structures will be freed
642  *  when the device is released.
643  */
644
645 static void
646 xirc2ps_detach(dev_link_t * link)
647 {
648     struct net_device *dev = link->priv;
649     dev_link_t **linkp;
650
651     DEBUG(0, "detach(0x%p)\n", link);
652
653     /* Locate device structure */
654     for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
655         if (*linkp == link)
656             break;
657     if (!*linkp) {
658         DEBUG(0, "detach(0x%p): dev_link lost\n", link);
659         return;
660     }
661
662     if (link->dev)
663         unregister_netdev(dev);
664
665     /*
666      * If the device is currently configured and active, we won't
667      * actually delete it yet.  Instead, it is marked so that when
668      * the release() function is called, that will trigger a proper
669      * detach().
670      */
671     if (link->state & DEV_CONFIG)
672         xirc2ps_release(link);
673
674     /* Break the link with Card Services */
675     if (link->handle)
676         pcmcia_deregister_client(link->handle);
677
678     /* Unlink device structure, free it */
679     *linkp = link->next;
680     free_netdev(dev);
681 } /* xirc2ps_detach */
682
683 /****************
684  * Detect the type of the card. s is the buffer with the data of tuple 0x20
685  * Returns: 0 := not supported
686  *                     mediaid=11 and prodid=47
687  * Media-Id bits:
688  *  Ethernet        0x01
689  *  Tokenring       0x02
690  *  Arcnet          0x04
691  *  Wireless        0x08
692  *  Modem           0x10
693  *  GSM only        0x20
694  * Prod-Id bits:
695  *  Pocket          0x10
696  *  External        0x20
697  *  Creditcard      0x40
698  *  Cardbus         0x80
699  *
700  */
701 static int
702 set_card_type(dev_link_t *link, const void *s)
703 {
704     struct net_device *dev = link->priv;
705     local_info_t *local = netdev_priv(dev);
706   #ifdef PCMCIA_DEBUG
707     unsigned cisrev = ((const unsigned char *)s)[2];
708   #endif
709     unsigned mediaid= ((const unsigned char *)s)[3];
710     unsigned prodid = ((const unsigned char *)s)[4];
711
712     DEBUG(0, "cisrev=%02x mediaid=%02x prodid=%02x\n",
713           cisrev, mediaid, prodid);
714
715     local->mohawk = 0;
716     local->dingo = 0;
717     local->modem = 0;
718     local->card_type = XIR_UNKNOWN;
719     if (!(prodid & 0x40)) {
720         printk(KNOT_XIRC "Ooops: Not a creditcard\n");
721         return 0;
722     }
723     if (!(mediaid & 0x01)) {
724         printk(KNOT_XIRC "Not an Ethernet card\n");
725         return 0;
726     }
727     if (mediaid & 0x10) {
728         local->modem = 1;
729         switch(prodid & 15) {
730           case 1: local->card_type = XIR_CEM   ; break;
731           case 2: local->card_type = XIR_CEM2  ; break;
732           case 3: local->card_type = XIR_CEM3  ; break;
733           case 4: local->card_type = XIR_CEM33 ; break;
734           case 5: local->card_type = XIR_CEM56M;
735                   local->mohawk = 1;
736                   break;
737           case 6:
738           case 7: /* 7 is the RealPort 10/56 */
739                   local->card_type = XIR_CEM56 ;
740                   local->mohawk = 1;
741                   local->dingo = 1;
742                   break;
743         }
744     } else {
745         switch(prodid & 15) {
746           case 1: local->card_type = has_ce2_string(link)? XIR_CE2 : XIR_CE ;
747                   break;
748           case 2: local->card_type = XIR_CE2; break;
749           case 3: local->card_type = XIR_CE3;
750                   local->mohawk = 1;
751                   break;
752         }
753     }
754     if (local->card_type == XIR_CE || local->card_type == XIR_CEM) {
755         printk(KNOT_XIRC "Sorry, this is an old CE card\n");
756         return 0;
757     }
758     if (local->card_type == XIR_UNKNOWN)
759         printk(KNOT_XIRC "unknown card (mediaid=%02x prodid=%02x)\n",
760                mediaid, prodid);
761
762     return 1;
763 }
764
765 /****************
766  * There are some CE2 cards out which claim to be a CE card.
767  * This function looks for a "CE2" in the 3rd version field.
768  * Returns: true if this is a CE2
769  */
770 static int
771 has_ce2_string(dev_link_t * link)
772 {
773     client_handle_t handle = link->handle;
774     tuple_t tuple;
775     cisparse_t parse;
776     u_char buf[256];
777
778     tuple.Attributes = 0;
779     tuple.TupleData = buf;
780     tuple.TupleDataMax = 254;
781     tuple.TupleOffset = 0;
782     tuple.DesiredTuple = CISTPL_VERS_1;
783     if (!first_tuple(handle, &tuple, &parse) && parse.version_1.ns > 2) {
784         if (strstr(parse.version_1.str + parse.version_1.ofs[2], "CE2"))
785             return 1;
786     }
787     return 0;
788 }
789
790 /****************
791  * xirc2ps_config() is scheduled to run after a CARD_INSERTION event
792  * is received, to configure the PCMCIA socket, and to make the
793  * ethernet device available to the system.
794  */
795 static void
796 xirc2ps_config(dev_link_t * link)
797 {
798     client_handle_t handle = link->handle;
799     struct net_device *dev = link->priv;
800     local_info_t *local = netdev_priv(dev);
801     tuple_t tuple;
802     cisparse_t parse;
803     kio_addr_t ioaddr;
804     int err, i;
805     u_char buf[64];
806     cistpl_lan_node_id_t *node_id = (cistpl_lan_node_id_t*)parse.funce.data;
807     cistpl_cftable_entry_t *cf = &parse.cftable_entry;
808
809     local->dingo_ccr = NULL;
810
811     DEBUG(0, "config(0x%p)\n", link);
812
813     /*
814      * This reads the card's CONFIG tuple to find its configuration
815      * registers.
816      */
817     tuple.Attributes = 0;
818     tuple.TupleData = buf;
819     tuple.TupleDataMax = 64;
820     tuple.TupleOffset = 0;
821
822     /* Is this a valid  card */
823     tuple.DesiredTuple = CISTPL_MANFID;
824     if ((err=first_tuple(handle, &tuple, &parse))) {
825         printk(KNOT_XIRC "manfid not found in CIS\n");
826         goto failure;
827     }
828
829     switch(parse.manfid.manf) {
830       case MANFID_XIRCOM:
831         local->manf_str = "Xircom";
832         break;
833       case MANFID_ACCTON:
834         local->manf_str = "Accton";
835         break;
836       case MANFID_COMPAQ:
837       case MANFID_COMPAQ2:
838         local->manf_str = "Compaq";
839         break;
840       case MANFID_INTEL:
841         local->manf_str = "Intel";
842         break;
843       case MANFID_TOSHIBA:
844         local->manf_str = "Toshiba";
845         break;
846       default:
847         printk(KNOT_XIRC "Unknown Card Manufacturer ID: 0x%04x\n",
848                (unsigned)parse.manfid.manf);
849         goto failure;
850     }
851     DEBUG(0, "found %s card\n", local->manf_str);
852
853     if (!set_card_type(link, buf)) {
854         printk(KNOT_XIRC "this card is not supported\n");
855         goto failure;
856     }
857
858     /* get configuration stuff */
859     tuple.DesiredTuple = CISTPL_CONFIG;
860     if ((err=first_tuple(handle, &tuple, &parse)))
861         goto cis_error;
862     link->conf.ConfigBase = parse.config.base;
863     link->conf.Present =    parse.config.rmask[0];
864
865     /* get the ethernet address from the CIS */
866     tuple.DesiredTuple = CISTPL_FUNCE;
867     for (err = first_tuple(handle, &tuple, &parse); !err;
868                              err = next_tuple(handle, &tuple, &parse)) {
869         /* Once I saw two CISTPL_FUNCE_LAN_NODE_ID entries:
870          * the first one with a length of zero the second correct -
871          * so I skip all entries with length 0 */
872         if (parse.funce.type == CISTPL_FUNCE_LAN_NODE_ID
873             && ((cistpl_lan_node_id_t *)parse.funce.data)->nb)
874             break;
875     }
876     if (err) { /* not found: try to get the node-id from tuple 0x89 */
877         tuple.DesiredTuple = 0x89;  /* data layout looks like tuple 0x22 */
878         if ((err = pcmcia_get_first_tuple(handle, &tuple)) == 0 &&
879                 (err = pcmcia_get_tuple_data(handle, &tuple)) == 0) {
880             if (tuple.TupleDataLen == 8 && *buf == CISTPL_FUNCE_LAN_NODE_ID)
881                 memcpy(&parse, buf, 8);
882             else
883                 err = -1;
884         }
885     }
886     if (err) { /* another try   (James Lehmer's CE2 version 4.1)*/
887         tuple.DesiredTuple = CISTPL_FUNCE;
888         for (err = first_tuple(handle, &tuple, &parse); !err;
889                                  err = next_tuple(handle, &tuple, &parse)) {
890             if (parse.funce.type == 0x02 && parse.funce.data[0] == 1
891                 && parse.funce.data[1] == 6 && tuple.TupleDataLen == 13) {
892                 buf[1] = 4;
893                 memcpy(&parse, buf+1, 8);
894                 break;
895             }
896         }
897     }
898     if (err) {
899         printk(KNOT_XIRC "node-id not found in CIS\n");
900         goto failure;
901     }
902     node_id = (cistpl_lan_node_id_t *)parse.funce.data;
903     if (node_id->nb != 6) {
904         printk(KNOT_XIRC "malformed node-id in CIS\n");
905         goto failure;
906     }
907     for (i=0; i < 6; i++)
908         dev->dev_addr[i] = node_id->id[i];
909
910     /* Configure card */
911     link->state |= DEV_CONFIG;
912
913     link->io.IOAddrLines =10;
914     link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
915     link->irq.Attributes = IRQ_HANDLE_PRESENT;
916     link->irq.IRQInfo1 = IRQ_LEVEL_ID;
917     if (local->modem) {
918         int pass;
919
920         if (do_sound) {
921             link->conf.Attributes |= CONF_ENABLE_SPKR;
922             link->conf.Status |= CCSR_AUDIO_ENA;
923         }
924         link->irq.Attributes |= IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED ;
925         link->io.NumPorts2 = 8;
926         link->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
927         if (local->dingo) {
928             /* Take the Modem IO port from the CIS and scan for a free
929              * Ethernet port */
930             link->io.NumPorts1 = 16; /* no Mako stuff anymore */
931             tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
932             for (err = first_tuple(handle, &tuple, &parse); !err;
933                                  err = next_tuple(handle, &tuple, &parse)) {
934                 if (cf->io.nwin > 0  &&  (cf->io.win[0].base & 0xf) == 8) {
935                     for (ioaddr = 0x300; ioaddr < 0x400; ioaddr += 0x10) {
936                         link->conf.ConfigIndex = cf->index ;
937                         link->io.BasePort2 = cf->io.win[0].base;
938                         link->io.BasePort1 = ioaddr;
939                         if (!(err=pcmcia_request_io(link->handle, &link->io)))
940                             goto port_found;
941                     }
942                 }
943             }
944         } else {
945             link->io.NumPorts1 = 18;
946             /* We do 2 passes here: The first one uses the regular mapping and
947              * the second tries again, thereby considering that the 32 ports are
948              * mirrored every 32 bytes. Actually we use a mirrored port for
949              * the Mako if (on the first pass) the COR bit 5 is set.
950              */
951             for (pass=0; pass < 2; pass++) {
952                 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
953                 for (err = first_tuple(handle, &tuple, &parse); !err;
954                                      err = next_tuple(handle, &tuple, &parse)){
955                     if (cf->io.nwin > 0  &&  (cf->io.win[0].base & 0xf) == 8){
956                         link->conf.ConfigIndex = cf->index ;
957                         link->io.BasePort2 = cf->io.win[0].base;
958                         link->io.BasePort1 = link->io.BasePort2
959                                     + (pass ? (cf->index & 0x20 ? -24:8)
960                                             : (cf->index & 0x20 ?   8:-24));
961                         if (!(err=pcmcia_request_io(link->handle, &link->io)))
962                             goto port_found;
963                     }
964                 }
965             }
966             /* if special option:
967              * try to configure as Ethernet only.
968              * .... */
969         }
970         printk(KNOT_XIRC "no ports available\n");
971     } else {
972         link->irq.Attributes |= IRQ_TYPE_EXCLUSIVE;
973         link->io.NumPorts1 = 16;
974         for (ioaddr = 0x300; ioaddr < 0x400; ioaddr += 0x10) {
975             link->io.BasePort1 = ioaddr;
976             if (!(err=pcmcia_request_io(link->handle, &link->io)))
977                 goto port_found;
978         }
979         link->io.BasePort1 = 0; /* let CS decide */
980         if ((err=pcmcia_request_io(link->handle, &link->io))) {
981             cs_error(link->handle, RequestIO, err);
982             goto config_error;
983         }
984     }
985   port_found:
986     if (err)
987          goto config_error;
988
989     /****************
990      * Now allocate an interrupt line.  Note that this does not
991      * actually assign a handler to the interrupt.
992      */
993     if ((err=pcmcia_request_irq(link->handle, &link->irq))) {
994         cs_error(link->handle, RequestIRQ, err);
995         goto config_error;
996     }
997
998     /****************
999      * This actually configures the PCMCIA socket -- setting up
1000      * the I/O windows and the interrupt mapping.
1001      */
1002     if ((err=pcmcia_request_configuration(link->handle, &link->conf))) {
1003         cs_error(link->handle, RequestConfiguration, err);
1004         goto config_error;
1005     }
1006
1007     if (local->dingo) {
1008         conf_reg_t reg;
1009         win_req_t req;
1010         memreq_t mem;
1011
1012         /* Reset the modem's BAR to the correct value
1013          * This is necessary because in the RequestConfiguration call,
1014          * the base address of the ethernet port (BasePort1) is written
1015          * to the BAR registers of the modem.
1016          */
1017         reg.Action = CS_WRITE;
1018         reg.Offset = CISREG_IOBASE_0;
1019         reg.Value = link->io.BasePort2 & 0xff;
1020         if ((err = pcmcia_access_configuration_register(link->handle, &reg))) {
1021             cs_error(link->handle, AccessConfigurationRegister, err);
1022             goto config_error;
1023         }
1024         reg.Action = CS_WRITE;
1025         reg.Offset = CISREG_IOBASE_1;
1026         reg.Value = (link->io.BasePort2 >> 8) & 0xff;
1027         if ((err = pcmcia_access_configuration_register(link->handle, &reg))) {
1028             cs_error(link->handle, AccessConfigurationRegister, err);
1029             goto config_error;
1030         }
1031
1032         /* There is no config entry for the Ethernet part which
1033          * is at 0x0800. So we allocate a window into the attribute
1034          * memory and write direct to the CIS registers
1035          */
1036         req.Attributes = WIN_DATA_WIDTH_8|WIN_MEMORY_TYPE_AM|WIN_ENABLE;
1037         req.Base = req.Size = 0;
1038         req.AccessSpeed = 0;
1039         if ((err = pcmcia_request_window(&link->handle, &req, &link->win))) {
1040             cs_error(link->handle, RequestWindow, err);
1041             goto config_error;
1042         }
1043         local->dingo_ccr = ioremap(req.Base,0x1000) + 0x0800;
1044         mem.CardOffset = 0x0;
1045         mem.Page = 0;
1046         if ((err = pcmcia_map_mem_page(link->win, &mem))) {
1047             cs_error(link->handle, MapMemPage, err);
1048             goto config_error;
1049         }
1050
1051         /* Setup the CCRs; there are no infos in the CIS about the Ethernet
1052          * part.
1053          */
1054         writeb(0x47, local->dingo_ccr + CISREG_COR);
1055         ioaddr = link->io.BasePort1;
1056         writeb(ioaddr & 0xff      , local->dingo_ccr + CISREG_IOBASE_0);
1057         writeb((ioaddr >> 8)&0xff , local->dingo_ccr + CISREG_IOBASE_1);
1058
1059       #if 0
1060         {
1061             u_char tmp;
1062             printk(KERN_INFO "ECOR:");
1063             for (i=0; i < 7; i++) {
1064                 tmp = readb(local->dingo_ccr + i*2);
1065                 printk(" %02x", tmp);
1066             }
1067             printk("\n");
1068             printk(KERN_INFO "DCOR:");
1069             for (i=0; i < 4; i++) {
1070                 tmp = readb(local->dingo_ccr + 0x20 + i*2);
1071                 printk(" %02x", tmp);
1072             }
1073             printk("\n");
1074             printk(KERN_INFO "SCOR:");
1075             for (i=0; i < 10; i++) {
1076                 tmp = readb(local->dingo_ccr + 0x40 + i*2);
1077                 printk(" %02x", tmp);
1078             }
1079             printk("\n");
1080         }
1081       #endif
1082
1083         writeb(0x01, local->dingo_ccr + 0x20);
1084         writeb(0x0c, local->dingo_ccr + 0x22);
1085         writeb(0x00, local->dingo_ccr + 0x24);
1086         writeb(0x00, local->dingo_ccr + 0x26);
1087         writeb(0x00, local->dingo_ccr + 0x28);
1088     }
1089
1090     /* The if_port symbol can be set when the module is loaded */
1091     local->probe_port=0;
1092     if (!if_port) {
1093         local->probe_port = dev->if_port = 1;
1094     } else if ((if_port >= 1 && if_port <= 2) ||
1095                (local->mohawk && if_port==4))
1096         dev->if_port = if_port;
1097     else
1098         printk(KNOT_XIRC "invalid if_port requested\n");
1099
1100     /* we can now register the device with the net subsystem */
1101     dev->irq = link->irq.AssignedIRQ;
1102     dev->base_addr = link->io.BasePort1;
1103
1104     if (local->dingo)
1105         do_reset(dev, 1); /* a kludge to make the cem56 work */
1106
1107     link->dev = &local->node;
1108     link->state &= ~DEV_CONFIG_PENDING;
1109     SET_NETDEV_DEV(dev, &handle_to_dev(handle));
1110
1111     if ((err=register_netdev(dev))) {
1112         printk(KNOT_XIRC "register_netdev() failed\n");
1113         link->dev = NULL;
1114         goto config_error;
1115     }
1116
1117     strcpy(local->node.dev_name, dev->name);
1118
1119     /* give some infos about the hardware */
1120     printk(KERN_INFO "%s: %s: port %#3lx, irq %d, hwaddr",
1121          dev->name, local->manf_str,(u_long)dev->base_addr, (int)dev->irq);
1122     for (i = 0; i < 6; i++)
1123         printk("%c%02X", i?':':' ', dev->dev_addr[i]);
1124     printk("\n");
1125
1126     return;
1127
1128   config_error:
1129     link->state &= ~DEV_CONFIG_PENDING;
1130     xirc2ps_release(link);
1131     return;
1132
1133   cis_error:
1134     printk(KNOT_XIRC "unable to parse CIS\n");
1135   failure:
1136     link->state &= ~DEV_CONFIG_PENDING;
1137 } /* xirc2ps_config */
1138
1139 /****************
1140  * After a card is removed, xirc2ps_release() will unregister the net
1141  * device, and release the PCMCIA configuration.  If the device is
1142  * still open, this will be postponed until it is closed.
1143  */
1144 static void
1145 xirc2ps_release(dev_link_t *link)
1146 {
1147
1148     DEBUG(0, "release(0x%p)\n", link);
1149
1150     if (link->win) {
1151         struct net_device *dev = link->priv;
1152         local_info_t *local = netdev_priv(dev);
1153         if (local->dingo)
1154             iounmap(local->dingo_ccr - 0x0800);
1155         pcmcia_release_window(link->win);
1156     }
1157     pcmcia_release_configuration(link->handle);
1158     pcmcia_release_io(link->handle, &link->io);
1159     pcmcia_release_irq(link->handle, &link->irq);
1160     link->state &= ~DEV_CONFIG;
1161
1162 } /* xirc2ps_release */
1163
1164 /*====================================================================*/
1165
1166 /****************
1167  * The card status event handler.  Mostly, this schedules other
1168  * stuff to run after an event is received.  A CARD_REMOVAL event
1169  * also sets some flags to discourage the net drivers from trying
1170  * to talk to the card any more.
1171  *
1172  * When a CARD_REMOVAL event is received, we immediately set a flag
1173  * to block future accesses to this device.  All the functions that
1174  * actually access the device should check this flag to make sure
1175  * the card is still present.
1176  */
1177
1178 static int
1179 xirc2ps_event(event_t event, int priority,
1180               event_callback_args_t * args)
1181 {
1182     dev_link_t *link = args->client_data;
1183     struct net_device *dev = link->priv;
1184
1185     DEBUG(0, "event(%d)\n", (int)event);
1186
1187     switch (event) {
1188     case CS_EVENT_REGISTRATION_COMPLETE:
1189         DEBUG(0, "registration complete\n");
1190         break;
1191     case CS_EVENT_CARD_REMOVAL:
1192         link->state &= ~DEV_PRESENT;
1193         if (link->state & DEV_CONFIG)
1194             netif_device_detach(dev);
1195         break;
1196     case CS_EVENT_CARD_INSERTION:
1197         link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
1198         xirc2ps_config(link);
1199         break;
1200     case CS_EVENT_PM_SUSPEND:
1201         link->state |= DEV_SUSPEND;
1202         /* Fall through... */
1203     case CS_EVENT_RESET_PHYSICAL:
1204         if (link->state & DEV_CONFIG) {
1205             if (link->open) {
1206                 netif_device_detach(dev);
1207                 do_powerdown(dev);
1208             }
1209             pcmcia_release_configuration(link->handle);
1210         }
1211         break;
1212     case CS_EVENT_PM_RESUME:
1213         link->state &= ~DEV_SUSPEND;
1214         /* Fall through... */
1215     case CS_EVENT_CARD_RESET:
1216         if (link->state & DEV_CONFIG) {
1217             pcmcia_request_configuration(link->handle, &link->conf);
1218             if (link->open) {
1219                 do_reset(dev,1);
1220                 netif_device_attach(dev);
1221             }
1222         }
1223         break;
1224     }
1225     return 0;
1226 } /* xirc2ps_event */
1227
1228 /*====================================================================*/
1229
1230 /****************
1231  * This is the Interrupt service route.
1232  */
1233 static irqreturn_t
1234 xirc2ps_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1235 {
1236     struct net_device *dev = (struct net_device *)dev_id;
1237     local_info_t *lp = netdev_priv(dev);
1238     kio_addr_t ioaddr;
1239     u_char saved_page;
1240     unsigned bytes_rcvd;
1241     unsigned int_status, eth_status, rx_status, tx_status;
1242     unsigned rsr, pktlen;
1243     ulong start_ticks = jiffies; /* fixme: jiffies rollover every 497 days
1244                                   * is this something to worry about?
1245                                   * -- on a laptop?
1246                                   */
1247
1248     if (!netif_device_present(dev))
1249         return IRQ_HANDLED;
1250
1251     ioaddr = dev->base_addr;
1252     if (lp->mohawk) { /* must disable the interrupt */
1253         PutByte(XIRCREG_CR, 0);
1254     }
1255
1256     DEBUG(6, "%s: interrupt %d at %#x.\n", dev->name, irq, ioaddr);
1257
1258     saved_page = GetByte(XIRCREG_PR);
1259     /* Read the ISR to see whats the cause for the interrupt.
1260      * This also clears the interrupt flags on CE2 cards
1261      */
1262     int_status = GetByte(XIRCREG_ISR);
1263     bytes_rcvd = 0;
1264   loop_entry:
1265     if (int_status == 0xff) { /* card may be ejected */
1266         DEBUG(3, "%s: interrupt %d for dead card\n", dev->name, irq);
1267         goto leave;
1268     }
1269     eth_status = GetByte(XIRCREG_ESR);
1270
1271     SelectPage(0x40);
1272     rx_status  = GetByte(XIRCREG40_RXST0);
1273     PutByte(XIRCREG40_RXST0, (~rx_status & 0xff));
1274     tx_status = GetByte(XIRCREG40_TXST0);
1275     tx_status |= GetByte(XIRCREG40_TXST1) << 8;
1276     PutByte(XIRCREG40_TXST0, 0);
1277     PutByte(XIRCREG40_TXST1, 0);
1278
1279     DEBUG(3, "%s: ISR=%#2.2x ESR=%#2.2x RSR=%#2.2x TSR=%#4.4x\n",
1280           dev->name, int_status, eth_status, rx_status, tx_status);
1281
1282     /***** receive section ******/
1283     SelectPage(0);
1284     while (eth_status & FullPktRcvd) {
1285         rsr = GetByte(XIRCREG0_RSR);
1286         if (bytes_rcvd > maxrx_bytes && (rsr & PktRxOk)) {
1287             /* too many bytes received during this int, drop the rest of the
1288              * packets */
1289             lp->stats.rx_dropped++;
1290             DEBUG(2, "%s: RX drop, too much done\n", dev->name);
1291         } else if (rsr & PktRxOk) {
1292             struct sk_buff *skb;
1293
1294             pktlen = GetWord(XIRCREG0_RBC);
1295             bytes_rcvd += pktlen;
1296
1297             DEBUG(5, "rsr=%#02x packet_length=%u\n", rsr, pktlen);
1298
1299             skb = dev_alloc_skb(pktlen+3); /* 1 extra so we can use insw */
1300             if (!skb) {
1301                 printk(KNOT_XIRC "low memory, packet dropped (size=%u)\n",
1302                        pktlen);
1303                 lp->stats.rx_dropped++;
1304             } else { /* okay get the packet */
1305                 skb_reserve(skb, 2);
1306                 if (lp->silicon == 0 ) { /* work around a hardware bug */
1307                     unsigned rhsa; /* receive start address */
1308
1309                     SelectPage(5);
1310                     rhsa = GetWord(XIRCREG5_RHSA0);
1311                     SelectPage(0);
1312                     rhsa += 3; /* skip control infos */
1313                     if (rhsa >= 0x8000)
1314                         rhsa = 0;
1315                     if (rhsa + pktlen > 0x8000) {
1316                         unsigned i;
1317                         u_char *buf = skb_put(skb, pktlen);
1318                         for (i=0; i < pktlen ; i++, rhsa++) {
1319                             buf[i] = GetByte(XIRCREG_EDP);
1320                             if (rhsa == 0x8000) {
1321                                 rhsa = 0;
1322                                 i--;
1323                             }
1324                         }
1325                     } else {
1326                         insw(ioaddr+XIRCREG_EDP,
1327                                 skb_put(skb, pktlen), (pktlen+1)>>1);
1328                     }
1329                 }
1330               #if 0
1331                 else if (lp->mohawk) {
1332                     /* To use this 32 bit access we should use
1333                      * a manual optimized loop
1334                      * Also the words are swapped, we can get more
1335                      * performance by using 32 bit access and swapping
1336                      * the words in a register. Will need this for cardbus
1337                      *
1338                      * Note: don't forget to change the ALLOC_SKB to .. +3
1339                      */
1340                     unsigned i;
1341                     u_long *p = skb_put(skb, pktlen);
1342                     register u_long a;
1343                     kio_addr_t edpreg = ioaddr+XIRCREG_EDP-2;
1344                     for (i=0; i < len ; i += 4, p++) {
1345                         a = inl(edpreg);
1346                         __asm__("rorl $16,%0\n\t"
1347                                 :"=q" (a)
1348                                 : "0" (a));
1349                         *p = a;
1350                     }
1351                 }
1352               #endif
1353                 else {
1354                     insw(ioaddr+XIRCREG_EDP, skb_put(skb, pktlen),
1355                             (pktlen+1)>>1);
1356                 }
1357                 skb->protocol = eth_type_trans(skb, dev);
1358                 skb->dev = dev;
1359                 netif_rx(skb);
1360                 dev->last_rx = jiffies;
1361                 lp->stats.rx_packets++;
1362                 lp->stats.rx_bytes += pktlen;
1363                 if (!(rsr & PhyPkt))
1364                     lp->stats.multicast++;
1365             }
1366         } else { /* bad packet */
1367             DEBUG(5, "rsr=%#02x\n", rsr);
1368         }
1369         if (rsr & PktTooLong) {
1370             lp->stats.rx_frame_errors++;
1371             DEBUG(3, "%s: Packet too long\n", dev->name);
1372         }
1373         if (rsr & CRCErr) {
1374             lp->stats.rx_crc_errors++;
1375             DEBUG(3, "%s: CRC error\n", dev->name);
1376         }
1377         if (rsr & AlignErr) {
1378             lp->stats.rx_fifo_errors++; /* okay ? */
1379             DEBUG(3, "%s: Alignment error\n", dev->name);
1380         }
1381
1382         /* clear the received/dropped/error packet */
1383         PutWord(XIRCREG0_DO, 0x8000); /* issue cmd: skip_rx_packet */
1384
1385         /* get the new ethernet status */
1386         eth_status = GetByte(XIRCREG_ESR);
1387     }
1388     if (rx_status & 0x10) { /* Receive overrun */
1389         lp->stats.rx_over_errors++;
1390         PutByte(XIRCREG_CR, ClearRxOvrun);
1391         DEBUG(3, "receive overrun cleared\n");
1392     }
1393
1394     /***** transmit section ******/
1395     if (int_status & PktTxed) {
1396         unsigned n, nn;
1397
1398         n = lp->last_ptr_value;
1399         nn = GetByte(XIRCREG0_PTR);
1400         lp->last_ptr_value = nn;
1401         if (nn < n) /* rollover */
1402             lp->stats.tx_packets += 256 - n;
1403         else if (n == nn) { /* happens sometimes - don't know why */
1404             DEBUG(0, "PTR not changed?\n");
1405         } else
1406             lp->stats.tx_packets += lp->last_ptr_value - n;
1407         netif_wake_queue(dev);
1408     }
1409     if (tx_status & 0x0002) {   /* Execessive collissions */
1410         DEBUG(0, "tx restarted due to execssive collissions\n");
1411         PutByte(XIRCREG_CR, RestartTx);  /* restart transmitter process */
1412     }
1413     if (tx_status & 0x0040)
1414         lp->stats.tx_aborted_errors++;
1415
1416     /* recalculate our work chunk so that we limit the duration of this
1417      * ISR to about 1/10 of a second.
1418      * Calculate only if we received a reasonable amount of bytes.
1419      */
1420     if (bytes_rcvd > 1000) {
1421         u_long duration = jiffies - start_ticks;
1422
1423         if (duration >= HZ/10) { /* if more than about 1/10 second */
1424             maxrx_bytes = (bytes_rcvd * (HZ/10)) / duration;
1425             if (maxrx_bytes < 2000)
1426                 maxrx_bytes = 2000;
1427             else if (maxrx_bytes > 22000)
1428                 maxrx_bytes = 22000;
1429             DEBUG(1, "set maxrx=%u (rcvd=%u ticks=%lu)\n",
1430                   maxrx_bytes, bytes_rcvd, duration);
1431         } else if (!duration && maxrx_bytes < 22000) {
1432             /* now much faster */
1433             maxrx_bytes += 2000;
1434             if (maxrx_bytes > 22000)
1435                 maxrx_bytes = 22000;
1436             DEBUG(1, "set maxrx=%u\n", maxrx_bytes);
1437         }
1438     }
1439
1440   leave:
1441     if (lockup_hack) {
1442         if (int_status != 0xff && (int_status = GetByte(XIRCREG_ISR)) != 0)
1443             goto loop_entry;
1444     }
1445     SelectPage(saved_page);
1446     PutByte(XIRCREG_CR, EnableIntr);  /* re-enable interrupts */
1447     /* Instead of dropping packets during a receive, we could
1448      * force an interrupt with this command:
1449      *    PutByte(XIRCREG_CR, EnableIntr|ForceIntr);
1450      */
1451     return IRQ_HANDLED;
1452 } /* xirc2ps_interrupt */
1453
1454 /*====================================================================*/
1455
1456 static void
1457 do_tx_timeout(struct net_device *dev)
1458 {
1459     local_info_t *lp = netdev_priv(dev);
1460     printk(KERN_NOTICE "%s: transmit timed out\n", dev->name);
1461     lp->stats.tx_errors++;
1462     /* reset the card */
1463     do_reset(dev,1);
1464     dev->trans_start = jiffies;
1465     netif_wake_queue(dev);
1466 }
1467
1468 static int
1469 do_start_xmit(struct sk_buff *skb, struct net_device *dev)
1470 {
1471     local_info_t *lp = netdev_priv(dev);
1472     kio_addr_t ioaddr = dev->base_addr;
1473     int okay;
1474     unsigned freespace;
1475     unsigned pktlen = skb? skb->len : 0;
1476
1477     DEBUG(1, "do_start_xmit(skb=%p, dev=%p) len=%u\n",
1478           skb, dev, pktlen);
1479
1480
1481     /* adjust the packet length to min. required
1482      * and hope that the buffer is large enough
1483      * to provide some random data.
1484      * fixme: For Mohawk we can change this by sending
1485      * a larger packetlen than we actually have; the chip will
1486      * pad this in his buffer with random bytes
1487      */
1488     if (pktlen < ETH_ZLEN)
1489     {
1490         skb = skb_padto(skb, ETH_ZLEN);
1491         if (skb == NULL)
1492                 return 0;
1493         pktlen = ETH_ZLEN;
1494     }
1495
1496     netif_stop_queue(dev);
1497     SelectPage(0);
1498     PutWord(XIRCREG0_TRS, (u_short)pktlen+2);
1499     freespace = GetWord(XIRCREG0_TSO);
1500     okay = freespace & 0x8000;
1501     freespace &= 0x7fff;
1502     /* TRS doesn't work - (indeed it is eliminated with sil-rev 1) */
1503     okay = pktlen +2 < freespace;
1504     DEBUG(2 + (okay ? 2 : 0), "%s: avail. tx space=%u%s\n",
1505           dev->name, freespace, okay ? " (okay)":" (not enough)");
1506     if (!okay) { /* not enough space */
1507         return 1;  /* upper layer may decide to requeue this packet */
1508     }
1509     /* send the packet */
1510     PutWord(XIRCREG_EDP, (u_short)pktlen);
1511     outsw(ioaddr+XIRCREG_EDP, skb->data, pktlen>>1);
1512     if (pktlen & 1)
1513         PutByte(XIRCREG_EDP, skb->data[pktlen-1]);
1514
1515     if (lp->mohawk)
1516         PutByte(XIRCREG_CR, TransmitPacket|EnableIntr);
1517
1518     dev_kfree_skb (skb);
1519     dev->trans_start = jiffies;
1520     lp->stats.tx_bytes += pktlen;
1521     netif_start_queue(dev);
1522     return 0;
1523 }
1524
1525 static struct net_device_stats *
1526 do_get_stats(struct net_device *dev)
1527 {
1528     local_info_t *lp = netdev_priv(dev);
1529
1530     /*  lp->stats.rx_missed_errors = GetByte(?) */
1531     return &lp->stats;
1532 }
1533
1534 /****************
1535  * Set all addresses: This first one is the individual address,
1536  * the next 9 addresses are taken from the multicast list and
1537  * the rest is filled with the individual address.
1538  */
1539 static void
1540 set_addresses(struct net_device *dev)
1541 {
1542     kio_addr_t ioaddr = dev->base_addr;
1543     local_info_t *lp = netdev_priv(dev);
1544     struct dev_mc_list *dmi = dev->mc_list;
1545     char *addr;
1546     int i,j,k,n;
1547
1548     SelectPage(k=0x50);
1549     for (i=0,j=8,n=0; ; i++, j++) {
1550         if (i > 5) {
1551             if (++n > 9)
1552                 break;
1553             i = 0;
1554         }
1555         if (j > 15) {
1556             j = 8;
1557             k++;
1558             SelectPage(k);
1559         }
1560
1561         if (n && n <= dev->mc_count && dmi) {
1562             addr = dmi->dmi_addr;
1563             dmi = dmi->next;
1564         } else
1565             addr = dev->dev_addr;
1566
1567         if (lp->mohawk)
1568             PutByte(j, addr[5-i]);
1569         else
1570             PutByte(j, addr[i]);
1571     }
1572     SelectPage(0);
1573 }
1574
1575 /****************
1576  * Set or clear the multicast filter for this adaptor.
1577  * We can filter up to 9 addresses, if more are requested we set
1578  * multicast promiscuous mode.
1579  */
1580
1581 static void
1582 set_multicast_list(struct net_device *dev)
1583 {
1584     kio_addr_t ioaddr = dev->base_addr;
1585
1586     SelectPage(0x42);
1587     if (dev->flags & IFF_PROMISC) { /* snoop */
1588         PutByte(XIRCREG42_SWC1, 0x06); /* set MPE and PME */
1589     } else if (dev->mc_count > 9 || (dev->flags & IFF_ALLMULTI)) {
1590         PutByte(XIRCREG42_SWC1, 0x06); /* set MPE */
1591     } else if (dev->mc_count) {
1592         /* the chip can filter 9 addresses perfectly */
1593         PutByte(XIRCREG42_SWC1, 0x00);
1594         SelectPage(0x40);
1595         PutByte(XIRCREG40_CMD0, Offline);
1596         set_addresses(dev);
1597         SelectPage(0x40);
1598         PutByte(XIRCREG40_CMD0, EnableRecv | Online);
1599     } else { /* standard usage */
1600         PutByte(XIRCREG42_SWC1, 0x00);
1601     }
1602     SelectPage(0);
1603 }
1604
1605 static int
1606 do_config(struct net_device *dev, struct ifmap *map)
1607 {
1608     local_info_t *local = netdev_priv(dev);
1609
1610     DEBUG(0, "do_config(%p)\n", dev);
1611     if (map->port != 255 && map->port != dev->if_port) {
1612         if (map->port > 4)
1613             return -EINVAL;
1614         if (!map->port) {
1615             local->probe_port = 1;
1616             dev->if_port = 1;
1617         } else {
1618             local->probe_port = 0;
1619             dev->if_port = map->port;
1620         }
1621         printk(KERN_INFO "%s: switching to %s port\n",
1622                dev->name, if_names[dev->if_port]);
1623         do_reset(dev,1);  /* not the fine way :-) */
1624     }
1625     return 0;
1626 }
1627
1628 /****************
1629  * Open the driver
1630  */
1631 static int
1632 do_open(struct net_device *dev)
1633 {
1634     local_info_t *lp = netdev_priv(dev);
1635     dev_link_t *link = &lp->link;
1636
1637     DEBUG(0, "do_open(%p)\n", dev);
1638
1639     /* Check that the PCMCIA card is still here. */
1640     /* Physical device present signature. */
1641     if (!DEV_OK(link))
1642         return -ENODEV;
1643
1644     /* okay */
1645     link->open++;
1646
1647     netif_start_queue(dev);
1648     do_reset(dev,1);
1649
1650     return 0;
1651 }
1652
1653 static void netdev_get_drvinfo(struct net_device *dev,
1654                                struct ethtool_drvinfo *info)
1655 {
1656         strcpy(info->driver, "xirc2ps_cs");
1657         sprintf(info->bus_info, "PCMCIA 0x%lx", dev->base_addr);
1658 }
1659
1660 static struct ethtool_ops netdev_ethtool_ops = {
1661         .get_drvinfo            = netdev_get_drvinfo,
1662 };
1663
1664 static int
1665 do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1666 {
1667     local_info_t *local = netdev_priv(dev);
1668     kio_addr_t ioaddr = dev->base_addr;
1669     u16 *data = (u16 *)&rq->ifr_ifru;
1670
1671     DEBUG(1, "%s: ioctl(%-.6s, %#04x) %04x %04x %04x %04x\n",
1672           dev->name, rq->ifr_ifrn.ifrn_name, cmd,
1673           data[0], data[1], data[2], data[3]);
1674
1675     if (!local->mohawk)
1676         return -EOPNOTSUPP;
1677
1678     switch(cmd) {
1679       case SIOCGMIIPHY:         /* Get the address of the PHY in use. */
1680         data[0] = 0;            /* we have only this address */
1681         /* fall trough */
1682       case SIOCGMIIREG:         /* Read the specified MII register. */
1683         data[3] = mii_rd(ioaddr, data[0] & 0x1f, data[1] & 0x1f);
1684         break;
1685       case SIOCSMIIREG:         /* Write the specified MII register */
1686         if (!capable(CAP_NET_ADMIN))
1687             return -EPERM;
1688         mii_wr(ioaddr, data[0] & 0x1f, data[1] & 0x1f, data[2], 16);
1689         break;
1690       default:
1691         return -EOPNOTSUPP;
1692     }
1693     return 0;
1694 }
1695
1696 static void
1697 hardreset(struct net_device *dev)
1698 {
1699     local_info_t *local = netdev_priv(dev);
1700     kio_addr_t ioaddr = dev->base_addr;
1701
1702     SelectPage(4);
1703     udelay(1);
1704     PutByte(XIRCREG4_GPR1, 0);       /* clear bit 0: power down */
1705     msleep(40);                              /* wait 40 msec */
1706     if (local->mohawk)
1707         PutByte(XIRCREG4_GPR1, 1);       /* set bit 0: power up */
1708     else
1709         PutByte(XIRCREG4_GPR1, 1 | 4);   /* set bit 0: power up, bit 2: AIC */
1710     msleep(20);                      /* wait 20 msec */
1711 }
1712
1713 static void
1714 do_reset(struct net_device *dev, int full)
1715 {
1716     local_info_t *local = netdev_priv(dev);
1717     kio_addr_t ioaddr = dev->base_addr;
1718     unsigned value;
1719
1720     DEBUG(0, "%s: do_reset(%p,%d)\n", dev? dev->name:"eth?", dev, full);
1721
1722     hardreset(dev);
1723     PutByte(XIRCREG_CR, SoftReset); /* set */
1724     msleep(20);                      /* wait 20 msec */
1725     PutByte(XIRCREG_CR, 0);          /* clear */
1726     msleep(40);                      /* wait 40 msec */
1727     if (local->mohawk) {
1728         SelectPage(4);
1729         /* set pin GP1 and GP2 to output  (0x0c)
1730          * set GP1 to low to power up the ML6692 (0x00)
1731          * set GP2 to high to power up the 10Mhz chip  (0x02)
1732          */
1733         PutByte(XIRCREG4_GPR0, 0x0e);
1734     }
1735
1736     /* give the circuits some time to power up */
1737     msleep(500);                        /* about 500ms */
1738
1739     local->last_ptr_value = 0;
1740     local->silicon = local->mohawk ? (GetByte(XIRCREG4_BOV) & 0x70) >> 4
1741                                    : (GetByte(XIRCREG4_BOV) & 0x30) >> 4;
1742
1743     if (local->probe_port) {
1744         if (!local->mohawk) {
1745             SelectPage(4);
1746             PutByte(XIRCREG4_GPR0, 4);
1747             local->probe_port = 0;
1748         }
1749     } else if (dev->if_port == 2) { /* enable 10Base2 */
1750         SelectPage(0x42);
1751         PutByte(XIRCREG42_SWC1, 0xC0);
1752     } else { /* enable 10BaseT */
1753         SelectPage(0x42);
1754         PutByte(XIRCREG42_SWC1, 0x80);
1755     }
1756     msleep(40);                      /* wait 40 msec to let it complete */
1757
1758   #ifdef PCMCIA_DEBUG
1759     if (pc_debug) {
1760         SelectPage(0);
1761         value = GetByte(XIRCREG_ESR);    /* read the ESR */
1762         printk(KERN_DEBUG "%s: ESR is: %#02x\n", dev->name, value);
1763     }
1764   #endif
1765
1766     /* setup the ECR */
1767     SelectPage(1);
1768     PutByte(XIRCREG1_IMR0, 0xff); /* allow all ints */
1769     PutByte(XIRCREG1_IMR1, 1    ); /* and Set TxUnderrunDetect */
1770     value = GetByte(XIRCREG1_ECR);
1771   #if 0
1772     if (local->mohawk)
1773         value |= DisableLinkPulse;
1774     PutByte(XIRCREG1_ECR, value);
1775   #endif
1776     DEBUG(0, "%s: ECR is: %#02x\n", dev->name, value);
1777
1778     SelectPage(0x42);
1779     PutByte(XIRCREG42_SWC0, 0x20); /* disable source insertion */
1780
1781     if (local->silicon != 1) {
1782         /* set the local memory dividing line.
1783          * The comments in the sample code say that this is only
1784          * settable with the scipper version 2 which is revision 0.
1785          * Always for CE3 cards
1786          */
1787         SelectPage(2);
1788         PutWord(XIRCREG2_RBS, 0x2000);
1789     }
1790
1791     if (full)
1792         set_addresses(dev);
1793
1794     /* Hardware workaround:
1795      * The receive byte pointer after reset is off by 1 so we need
1796      * to move the offset pointer back to 0.
1797      */
1798     SelectPage(0);
1799     PutWord(XIRCREG0_DO, 0x2000); /* change offset command, off=0 */
1800
1801     /* setup MAC IMRs and clear status registers */
1802     SelectPage(0x40);                /* Bit 7 ... bit 0 */
1803     PutByte(XIRCREG40_RMASK0, 0xff); /* ROK, RAB, rsv, RO, CRC, AE, PTL, MP */
1804     PutByte(XIRCREG40_TMASK0, 0xff); /* TOK, TAB, SQE, LL, TU, JAB, EXC, CRS */
1805     PutByte(XIRCREG40_TMASK1, 0xb0); /* rsv, rsv, PTD, EXT, rsv,rsv,rsv, rsv*/
1806     PutByte(XIRCREG40_RXST0,  0x00); /* ROK, RAB, REN, RO, CRC, AE, PTL, MP */
1807     PutByte(XIRCREG40_TXST0,  0x00); /* TOK, TAB, SQE, LL, TU, JAB, EXC, CRS */
1808     PutByte(XIRCREG40_TXST1,  0x00); /* TEN, rsv, PTD, EXT, retry_counter:4  */
1809
1810     if (full && local->mohawk && init_mii(dev)) {
1811         if (dev->if_port == 4 || local->dingo || local->new_mii) {
1812             printk(KERN_INFO "%s: MII selected\n", dev->name);
1813             SelectPage(2);
1814             PutByte(XIRCREG2_MSR, GetByte(XIRCREG2_MSR) | 0x08);
1815             msleep(20);
1816         } else {
1817             printk(KERN_INFO "%s: MII detected; using 10mbs\n",
1818                    dev->name);
1819             SelectPage(0x42);
1820             if (dev->if_port == 2) /* enable 10Base2 */
1821                 PutByte(XIRCREG42_SWC1, 0xC0);
1822             else  /* enable 10BaseT */
1823                 PutByte(XIRCREG42_SWC1, 0x80);
1824             msleep(40);                 /* wait 40 msec to let it complete */
1825         }
1826         if (full_duplex)
1827             PutByte(XIRCREG1_ECR, GetByte(XIRCREG1_ECR | FullDuplex));
1828     } else {  /* No MII */
1829         SelectPage(0);
1830         value = GetByte(XIRCREG_ESR);    /* read the ESR */
1831         dev->if_port = (value & MediaSelect) ? 1 : 2;
1832     }
1833
1834     /* configure the LEDs */
1835     SelectPage(2);
1836     if (dev->if_port == 1 || dev->if_port == 4) /* TP: Link and Activity */
1837         PutByte(XIRCREG2_LED, 0x3b);
1838     else                              /* Coax: Not-Collision and Activity */
1839         PutByte(XIRCREG2_LED, 0x3a);
1840
1841     if (local->dingo)
1842         PutByte(0x0b, 0x04); /* 100 Mbit LED */
1843
1844     /* enable receiver and put the mac online */
1845     if (full) {
1846         SelectPage(0x40);
1847         PutByte(XIRCREG40_CMD0, EnableRecv | Online);
1848     }
1849
1850     /* setup Ethernet IMR and enable interrupts */
1851     SelectPage(1);
1852     PutByte(XIRCREG1_IMR0, 0xff);
1853     udelay(1);
1854     SelectPage(0);
1855     PutByte(XIRCREG_CR, EnableIntr);
1856     if (local->modem && !local->dingo) { /* do some magic */
1857         if (!(GetByte(0x10) & 0x01))
1858             PutByte(0x10, 0x11); /* unmask master-int bit */
1859     }
1860
1861     if (full)
1862         printk(KERN_INFO "%s: media %s, silicon revision %d\n",
1863                dev->name, if_names[dev->if_port], local->silicon);
1864     /* We should switch back to page 0 to avoid a bug in revision 0
1865      * where regs with offset below 8 can't be read after an access
1866      * to the MAC registers */
1867     SelectPage(0);
1868 }
1869
1870 /****************
1871  * Initialize the Media-Independent-Interface
1872  * Returns: True if we have a good MII
1873  */
1874 static int
1875 init_mii(struct net_device *dev)
1876 {
1877     local_info_t *local = netdev_priv(dev);
1878     kio_addr_t ioaddr = dev->base_addr;
1879     unsigned control, status, linkpartner;
1880     int i;
1881
1882     if (if_port == 4 || if_port == 1) { /* force 100BaseT or 10BaseT */
1883         dev->if_port = if_port;
1884         local->probe_port = 0;
1885         return 1;
1886     }
1887
1888     status = mii_rd(ioaddr,  0, 1);
1889     if ((status & 0xff00) != 0x7800)
1890         return 0; /* No MII */
1891
1892     local->new_mii = (mii_rd(ioaddr, 0, 2) != 0xffff);
1893     
1894     if (local->probe_port)
1895         control = 0x1000; /* auto neg */
1896     else if (dev->if_port == 4)
1897         control = 0x2000; /* no auto neg, 100mbs mode */
1898     else
1899         control = 0x0000; /* no auto neg, 10mbs mode */
1900     mii_wr(ioaddr,  0, 0, control, 16);
1901     udelay(100);
1902     control = mii_rd(ioaddr, 0, 0);
1903
1904     if (control & 0x0400) {
1905         printk(KERN_NOTICE "%s can't take PHY out of isolation mode\n",
1906                dev->name);
1907         local->probe_port = 0;
1908         return 0;
1909     }
1910
1911     if (local->probe_port) {
1912         /* according to the DP83840A specs the auto negotiation process
1913          * may take up to 3.5 sec, so we use this also for our ML6692
1914          * Fixme: Better to use a timer here!
1915          */
1916         for (i=0; i < 35; i++) {
1917             msleep(100);         /* wait 100 msec */
1918             status = mii_rd(ioaddr,  0, 1);
1919             if ((status & 0x0020) && (status & 0x0004))
1920                 break;
1921         }
1922
1923         if (!(status & 0x0020)) {
1924             printk(KERN_INFO "%s: autonegotiation failed;"
1925                    " using 10mbs\n", dev->name);
1926             if (!local->new_mii) {
1927                 control = 0x0000;
1928                 mii_wr(ioaddr,  0, 0, control, 16);
1929                 udelay(100);
1930                 SelectPage(0);
1931                 dev->if_port = (GetByte(XIRCREG_ESR) & MediaSelect) ? 1 : 2;
1932             }
1933         } else {
1934             linkpartner = mii_rd(ioaddr, 0, 5);
1935             printk(KERN_INFO "%s: MII link partner: %04x\n",
1936                    dev->name, linkpartner);
1937             if (linkpartner & 0x0080) {
1938                 dev->if_port = 4;
1939             } else
1940                 dev->if_port = 1;
1941         }
1942     }
1943
1944     return 1;
1945 }
1946
1947 static void
1948 do_powerdown(struct net_device *dev)
1949 {
1950
1951     kio_addr_t ioaddr = dev->base_addr;
1952
1953     DEBUG(0, "do_powerdown(%p)\n", dev);
1954
1955     SelectPage(4);
1956     PutByte(XIRCREG4_GPR1, 0);       /* clear bit 0: power down */
1957     SelectPage(0);
1958 }
1959
1960 static int
1961 do_stop(struct net_device *dev)
1962 {
1963     kio_addr_t ioaddr = dev->base_addr;
1964     local_info_t *lp = netdev_priv(dev);
1965     dev_link_t *link = &lp->link;
1966
1967     DEBUG(0, "do_stop(%p)\n", dev);
1968
1969     if (!link)
1970         return -ENODEV;
1971
1972     netif_stop_queue(dev);
1973
1974     SelectPage(0);
1975     PutByte(XIRCREG_CR, 0);  /* disable interrupts */
1976     SelectPage(0x01);
1977     PutByte(XIRCREG1_IMR0, 0x00); /* forbid all ints */
1978     SelectPage(4);
1979     PutByte(XIRCREG4_GPR1, 0);  /* clear bit 0: power down */
1980     SelectPage(0);
1981
1982     link->open--;
1983     return 0;
1984 }
1985
1986 static struct pcmcia_driver xirc2ps_cs_driver = {
1987         .owner          = THIS_MODULE,
1988         .drv            = {
1989                 .name   = "xirc2ps_cs",
1990         },
1991         .attach         = xirc2ps_attach,
1992         .detach         = xirc2ps_detach,
1993 };
1994
1995 static int __init
1996 init_xirc2ps_cs(void)
1997 {
1998         return pcmcia_register_driver(&xirc2ps_cs_driver);
1999 }
2000
2001 static void __exit
2002 exit_xirc2ps_cs(void)
2003 {
2004         pcmcia_unregister_driver(&xirc2ps_cs_driver);
2005         BUG_ON(dev_list != NULL);
2006 }
2007
2008 module_init(init_xirc2ps_cs);
2009 module_exit(exit_xirc2ps_cs);
2010
2011 #ifndef MODULE
2012 static int __init setup_xirc2ps_cs(char *str)
2013 {
2014         /* if_port, full_duplex, do_sound, lockup_hack
2015          */
2016         int ints[10] = { -1 };
2017
2018         str = get_options(str, 9, ints);
2019
2020 #define MAYBE_SET(X,Y) if (ints[0] >= Y && ints[Y] != -1) { X = ints[Y]; }
2021         MAYBE_SET(if_port, 3);
2022         MAYBE_SET(full_duplex, 4);
2023         MAYBE_SET(do_sound, 5);
2024         MAYBE_SET(lockup_hack, 6);
2025 #undef  MAYBE_SET
2026
2027         return 0;
2028 }
2029
2030 __setup("xirc2ps_cs=", setup_xirc2ps_cs);
2031 #endif