[PATCH] pcmcia: move event handler
[safe/jmp/linux-2.6] / 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.Version = 0x0210;
623     client_reg.event_callback_args.client_data = link;
624     if ((err = pcmcia_register_client(&link->handle, &client_reg))) {
625         cs_error(link->handle, RegisterClient, err);
626         xirc2ps_detach(link);
627         return NULL;
628     }
629
630     return link;
631 } /* xirc2ps_attach */
632
633 /****************
634  *  This deletes a driver "instance".  The device is de-registered
635  *  with Card Services.  If it has been released, all local data
636  *  structures are freed.  Otherwise, the structures will be freed
637  *  when the device is released.
638  */
639
640 static void
641 xirc2ps_detach(dev_link_t * link)
642 {
643     struct net_device *dev = link->priv;
644     dev_link_t **linkp;
645
646     DEBUG(0, "detach(0x%p)\n", link);
647
648     /* Locate device structure */
649     for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
650         if (*linkp == link)
651             break;
652     if (!*linkp) {
653         DEBUG(0, "detach(0x%p): dev_link lost\n", link);
654         return;
655     }
656
657     if (link->dev)
658         unregister_netdev(dev);
659
660     /*
661      * If the device is currently configured and active, we won't
662      * actually delete it yet.  Instead, it is marked so that when
663      * the release() function is called, that will trigger a proper
664      * detach().
665      */
666     if (link->state & DEV_CONFIG)
667         xirc2ps_release(link);
668
669     /* Break the link with Card Services */
670     if (link->handle)
671         pcmcia_deregister_client(link->handle);
672
673     /* Unlink device structure, free it */
674     *linkp = link->next;
675     free_netdev(dev);
676 } /* xirc2ps_detach */
677
678 /****************
679  * Detect the type of the card. s is the buffer with the data of tuple 0x20
680  * Returns: 0 := not supported
681  *                     mediaid=11 and prodid=47
682  * Media-Id bits:
683  *  Ethernet        0x01
684  *  Tokenring       0x02
685  *  Arcnet          0x04
686  *  Wireless        0x08
687  *  Modem           0x10
688  *  GSM only        0x20
689  * Prod-Id bits:
690  *  Pocket          0x10
691  *  External        0x20
692  *  Creditcard      0x40
693  *  Cardbus         0x80
694  *
695  */
696 static int
697 set_card_type(dev_link_t *link, const void *s)
698 {
699     struct net_device *dev = link->priv;
700     local_info_t *local = netdev_priv(dev);
701   #ifdef PCMCIA_DEBUG
702     unsigned cisrev = ((const unsigned char *)s)[2];
703   #endif
704     unsigned mediaid= ((const unsigned char *)s)[3];
705     unsigned prodid = ((const unsigned char *)s)[4];
706
707     DEBUG(0, "cisrev=%02x mediaid=%02x prodid=%02x\n",
708           cisrev, mediaid, prodid);
709
710     local->mohawk = 0;
711     local->dingo = 0;
712     local->modem = 0;
713     local->card_type = XIR_UNKNOWN;
714     if (!(prodid & 0x40)) {
715         printk(KNOT_XIRC "Ooops: Not a creditcard\n");
716         return 0;
717     }
718     if (!(mediaid & 0x01)) {
719         printk(KNOT_XIRC "Not an Ethernet card\n");
720         return 0;
721     }
722     if (mediaid & 0x10) {
723         local->modem = 1;
724         switch(prodid & 15) {
725           case 1: local->card_type = XIR_CEM   ; break;
726           case 2: local->card_type = XIR_CEM2  ; break;
727           case 3: local->card_type = XIR_CEM3  ; break;
728           case 4: local->card_type = XIR_CEM33 ; break;
729           case 5: local->card_type = XIR_CEM56M;
730                   local->mohawk = 1;
731                   break;
732           case 6:
733           case 7: /* 7 is the RealPort 10/56 */
734                   local->card_type = XIR_CEM56 ;
735                   local->mohawk = 1;
736                   local->dingo = 1;
737                   break;
738         }
739     } else {
740         switch(prodid & 15) {
741           case 1: local->card_type = has_ce2_string(link)? XIR_CE2 : XIR_CE ;
742                   break;
743           case 2: local->card_type = XIR_CE2; break;
744           case 3: local->card_type = XIR_CE3;
745                   local->mohawk = 1;
746                   break;
747         }
748     }
749     if (local->card_type == XIR_CE || local->card_type == XIR_CEM) {
750         printk(KNOT_XIRC "Sorry, this is an old CE card\n");
751         return 0;
752     }
753     if (local->card_type == XIR_UNKNOWN)
754         printk(KNOT_XIRC "unknown card (mediaid=%02x prodid=%02x)\n",
755                mediaid, prodid);
756
757     return 1;
758 }
759
760 /****************
761  * There are some CE2 cards out which claim to be a CE card.
762  * This function looks for a "CE2" in the 3rd version field.
763  * Returns: true if this is a CE2
764  */
765 static int
766 has_ce2_string(dev_link_t * link)
767 {
768     client_handle_t handle = link->handle;
769     tuple_t tuple;
770     cisparse_t parse;
771     u_char buf[256];
772
773     tuple.Attributes = 0;
774     tuple.TupleData = buf;
775     tuple.TupleDataMax = 254;
776     tuple.TupleOffset = 0;
777     tuple.DesiredTuple = CISTPL_VERS_1;
778     if (!first_tuple(handle, &tuple, &parse) && parse.version_1.ns > 2) {
779         if (strstr(parse.version_1.str + parse.version_1.ofs[2], "CE2"))
780             return 1;
781     }
782     return 0;
783 }
784
785 /****************
786  * xirc2ps_config() is scheduled to run after a CARD_INSERTION event
787  * is received, to configure the PCMCIA socket, and to make the
788  * ethernet device available to the system.
789  */
790 static void
791 xirc2ps_config(dev_link_t * link)
792 {
793     client_handle_t handle = link->handle;
794     struct net_device *dev = link->priv;
795     local_info_t *local = netdev_priv(dev);
796     tuple_t tuple;
797     cisparse_t parse;
798     kio_addr_t ioaddr;
799     int err, i;
800     u_char buf[64];
801     cistpl_lan_node_id_t *node_id = (cistpl_lan_node_id_t*)parse.funce.data;
802     cistpl_cftable_entry_t *cf = &parse.cftable_entry;
803
804     local->dingo_ccr = NULL;
805
806     DEBUG(0, "config(0x%p)\n", link);
807
808     /*
809      * This reads the card's CONFIG tuple to find its configuration
810      * registers.
811      */
812     tuple.Attributes = 0;
813     tuple.TupleData = buf;
814     tuple.TupleDataMax = 64;
815     tuple.TupleOffset = 0;
816
817     /* Is this a valid  card */
818     tuple.DesiredTuple = CISTPL_MANFID;
819     if ((err=first_tuple(handle, &tuple, &parse))) {
820         printk(KNOT_XIRC "manfid not found in CIS\n");
821         goto failure;
822     }
823
824     switch(parse.manfid.manf) {
825       case MANFID_XIRCOM:
826         local->manf_str = "Xircom";
827         break;
828       case MANFID_ACCTON:
829         local->manf_str = "Accton";
830         break;
831       case MANFID_COMPAQ:
832       case MANFID_COMPAQ2:
833         local->manf_str = "Compaq";
834         break;
835       case MANFID_INTEL:
836         local->manf_str = "Intel";
837         break;
838       case MANFID_TOSHIBA:
839         local->manf_str = "Toshiba";
840         break;
841       default:
842         printk(KNOT_XIRC "Unknown Card Manufacturer ID: 0x%04x\n",
843                (unsigned)parse.manfid.manf);
844         goto failure;
845     }
846     DEBUG(0, "found %s card\n", local->manf_str);
847
848     if (!set_card_type(link, buf)) {
849         printk(KNOT_XIRC "this card is not supported\n");
850         goto failure;
851     }
852
853     /* get configuration stuff */
854     tuple.DesiredTuple = CISTPL_CONFIG;
855     if ((err=first_tuple(handle, &tuple, &parse)))
856         goto cis_error;
857     link->conf.ConfigBase = parse.config.base;
858     link->conf.Present =    parse.config.rmask[0];
859
860     /* get the ethernet address from the CIS */
861     tuple.DesiredTuple = CISTPL_FUNCE;
862     for (err = first_tuple(handle, &tuple, &parse); !err;
863                              err = next_tuple(handle, &tuple, &parse)) {
864         /* Once I saw two CISTPL_FUNCE_LAN_NODE_ID entries:
865          * the first one with a length of zero the second correct -
866          * so I skip all entries with length 0 */
867         if (parse.funce.type == CISTPL_FUNCE_LAN_NODE_ID
868             && ((cistpl_lan_node_id_t *)parse.funce.data)->nb)
869             break;
870     }
871     if (err) { /* not found: try to get the node-id from tuple 0x89 */
872         tuple.DesiredTuple = 0x89;  /* data layout looks like tuple 0x22 */
873         if ((err = pcmcia_get_first_tuple(handle, &tuple)) == 0 &&
874                 (err = pcmcia_get_tuple_data(handle, &tuple)) == 0) {
875             if (tuple.TupleDataLen == 8 && *buf == CISTPL_FUNCE_LAN_NODE_ID)
876                 memcpy(&parse, buf, 8);
877             else
878                 err = -1;
879         }
880     }
881     if (err) { /* another try   (James Lehmer's CE2 version 4.1)*/
882         tuple.DesiredTuple = CISTPL_FUNCE;
883         for (err = first_tuple(handle, &tuple, &parse); !err;
884                                  err = next_tuple(handle, &tuple, &parse)) {
885             if (parse.funce.type == 0x02 && parse.funce.data[0] == 1
886                 && parse.funce.data[1] == 6 && tuple.TupleDataLen == 13) {
887                 buf[1] = 4;
888                 memcpy(&parse, buf+1, 8);
889                 break;
890             }
891         }
892     }
893     if (err) {
894         printk(KNOT_XIRC "node-id not found in CIS\n");
895         goto failure;
896     }
897     node_id = (cistpl_lan_node_id_t *)parse.funce.data;
898     if (node_id->nb != 6) {
899         printk(KNOT_XIRC "malformed node-id in CIS\n");
900         goto failure;
901     }
902     for (i=0; i < 6; i++)
903         dev->dev_addr[i] = node_id->id[i];
904
905     /* Configure card */
906     link->state |= DEV_CONFIG;
907
908     link->io.IOAddrLines =10;
909     link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
910     link->irq.Attributes = IRQ_HANDLE_PRESENT;
911     link->irq.IRQInfo1 = IRQ_LEVEL_ID;
912     if (local->modem) {
913         int pass;
914
915         if (do_sound) {
916             link->conf.Attributes |= CONF_ENABLE_SPKR;
917             link->conf.Status |= CCSR_AUDIO_ENA;
918         }
919         link->irq.Attributes |= IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED ;
920         link->io.NumPorts2 = 8;
921         link->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
922         if (local->dingo) {
923             /* Take the Modem IO port from the CIS and scan for a free
924              * Ethernet port */
925             link->io.NumPorts1 = 16; /* no Mako stuff anymore */
926             tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
927             for (err = first_tuple(handle, &tuple, &parse); !err;
928                                  err = next_tuple(handle, &tuple, &parse)) {
929                 if (cf->io.nwin > 0  &&  (cf->io.win[0].base & 0xf) == 8) {
930                     for (ioaddr = 0x300; ioaddr < 0x400; ioaddr += 0x10) {
931                         link->conf.ConfigIndex = cf->index ;
932                         link->io.BasePort2 = cf->io.win[0].base;
933                         link->io.BasePort1 = ioaddr;
934                         if (!(err=pcmcia_request_io(link->handle, &link->io)))
935                             goto port_found;
936                     }
937                 }
938             }
939         } else {
940             link->io.NumPorts1 = 18;
941             /* We do 2 passes here: The first one uses the regular mapping and
942              * the second tries again, thereby considering that the 32 ports are
943              * mirrored every 32 bytes. Actually we use a mirrored port for
944              * the Mako if (on the first pass) the COR bit 5 is set.
945              */
946             for (pass=0; pass < 2; pass++) {
947                 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
948                 for (err = first_tuple(handle, &tuple, &parse); !err;
949                                      err = next_tuple(handle, &tuple, &parse)){
950                     if (cf->io.nwin > 0  &&  (cf->io.win[0].base & 0xf) == 8){
951                         link->conf.ConfigIndex = cf->index ;
952                         link->io.BasePort2 = cf->io.win[0].base;
953                         link->io.BasePort1 = link->io.BasePort2
954                                     + (pass ? (cf->index & 0x20 ? -24:8)
955                                             : (cf->index & 0x20 ?   8:-24));
956                         if (!(err=pcmcia_request_io(link->handle, &link->io)))
957                             goto port_found;
958                     }
959                 }
960             }
961             /* if special option:
962              * try to configure as Ethernet only.
963              * .... */
964         }
965         printk(KNOT_XIRC "no ports available\n");
966     } else {
967         link->irq.Attributes |= IRQ_TYPE_EXCLUSIVE;
968         link->io.NumPorts1 = 16;
969         for (ioaddr = 0x300; ioaddr < 0x400; ioaddr += 0x10) {
970             link->io.BasePort1 = ioaddr;
971             if (!(err=pcmcia_request_io(link->handle, &link->io)))
972                 goto port_found;
973         }
974         link->io.BasePort1 = 0; /* let CS decide */
975         if ((err=pcmcia_request_io(link->handle, &link->io))) {
976             cs_error(link->handle, RequestIO, err);
977             goto config_error;
978         }
979     }
980   port_found:
981     if (err)
982          goto config_error;
983
984     /****************
985      * Now allocate an interrupt line.  Note that this does not
986      * actually assign a handler to the interrupt.
987      */
988     if ((err=pcmcia_request_irq(link->handle, &link->irq))) {
989         cs_error(link->handle, RequestIRQ, err);
990         goto config_error;
991     }
992
993     /****************
994      * This actually configures the PCMCIA socket -- setting up
995      * the I/O windows and the interrupt mapping.
996      */
997     if ((err=pcmcia_request_configuration(link->handle, &link->conf))) {
998         cs_error(link->handle, RequestConfiguration, err);
999         goto config_error;
1000     }
1001
1002     if (local->dingo) {
1003         conf_reg_t reg;
1004         win_req_t req;
1005         memreq_t mem;
1006
1007         /* Reset the modem's BAR to the correct value
1008          * This is necessary because in the RequestConfiguration call,
1009          * the base address of the ethernet port (BasePort1) is written
1010          * to the BAR registers of the modem.
1011          */
1012         reg.Action = CS_WRITE;
1013         reg.Offset = CISREG_IOBASE_0;
1014         reg.Value = link->io.BasePort2 & 0xff;
1015         if ((err = pcmcia_access_configuration_register(link->handle, &reg))) {
1016             cs_error(link->handle, AccessConfigurationRegister, err);
1017             goto config_error;
1018         }
1019         reg.Action = CS_WRITE;
1020         reg.Offset = CISREG_IOBASE_1;
1021         reg.Value = (link->io.BasePort2 >> 8) & 0xff;
1022         if ((err = pcmcia_access_configuration_register(link->handle, &reg))) {
1023             cs_error(link->handle, AccessConfigurationRegister, err);
1024             goto config_error;
1025         }
1026
1027         /* There is no config entry for the Ethernet part which
1028          * is at 0x0800. So we allocate a window into the attribute
1029          * memory and write direct to the CIS registers
1030          */
1031         req.Attributes = WIN_DATA_WIDTH_8|WIN_MEMORY_TYPE_AM|WIN_ENABLE;
1032         req.Base = req.Size = 0;
1033         req.AccessSpeed = 0;
1034         if ((err = pcmcia_request_window(&link->handle, &req, &link->win))) {
1035             cs_error(link->handle, RequestWindow, err);
1036             goto config_error;
1037         }
1038         local->dingo_ccr = ioremap(req.Base,0x1000) + 0x0800;
1039         mem.CardOffset = 0x0;
1040         mem.Page = 0;
1041         if ((err = pcmcia_map_mem_page(link->win, &mem))) {
1042             cs_error(link->handle, MapMemPage, err);
1043             goto config_error;
1044         }
1045
1046         /* Setup the CCRs; there are no infos in the CIS about the Ethernet
1047          * part.
1048          */
1049         writeb(0x47, local->dingo_ccr + CISREG_COR);
1050         ioaddr = link->io.BasePort1;
1051         writeb(ioaddr & 0xff      , local->dingo_ccr + CISREG_IOBASE_0);
1052         writeb((ioaddr >> 8)&0xff , local->dingo_ccr + CISREG_IOBASE_1);
1053
1054       #if 0
1055         {
1056             u_char tmp;
1057             printk(KERN_INFO "ECOR:");
1058             for (i=0; i < 7; i++) {
1059                 tmp = readb(local->dingo_ccr + i*2);
1060                 printk(" %02x", tmp);
1061             }
1062             printk("\n");
1063             printk(KERN_INFO "DCOR:");
1064             for (i=0; i < 4; i++) {
1065                 tmp = readb(local->dingo_ccr + 0x20 + i*2);
1066                 printk(" %02x", tmp);
1067             }
1068             printk("\n");
1069             printk(KERN_INFO "SCOR:");
1070             for (i=0; i < 10; i++) {
1071                 tmp = readb(local->dingo_ccr + 0x40 + i*2);
1072                 printk(" %02x", tmp);
1073             }
1074             printk("\n");
1075         }
1076       #endif
1077
1078         writeb(0x01, local->dingo_ccr + 0x20);
1079         writeb(0x0c, local->dingo_ccr + 0x22);
1080         writeb(0x00, local->dingo_ccr + 0x24);
1081         writeb(0x00, local->dingo_ccr + 0x26);
1082         writeb(0x00, local->dingo_ccr + 0x28);
1083     }
1084
1085     /* The if_port symbol can be set when the module is loaded */
1086     local->probe_port=0;
1087     if (!if_port) {
1088         local->probe_port = dev->if_port = 1;
1089     } else if ((if_port >= 1 && if_port <= 2) ||
1090                (local->mohawk && if_port==4))
1091         dev->if_port = if_port;
1092     else
1093         printk(KNOT_XIRC "invalid if_port requested\n");
1094
1095     /* we can now register the device with the net subsystem */
1096     dev->irq = link->irq.AssignedIRQ;
1097     dev->base_addr = link->io.BasePort1;
1098
1099     if (local->dingo)
1100         do_reset(dev, 1); /* a kludge to make the cem56 work */
1101
1102     link->dev = &local->node;
1103     link->state &= ~DEV_CONFIG_PENDING;
1104     SET_NETDEV_DEV(dev, &handle_to_dev(handle));
1105
1106     if ((err=register_netdev(dev))) {
1107         printk(KNOT_XIRC "register_netdev() failed\n");
1108         link->dev = NULL;
1109         goto config_error;
1110     }
1111
1112     strcpy(local->node.dev_name, dev->name);
1113
1114     /* give some infos about the hardware */
1115     printk(KERN_INFO "%s: %s: port %#3lx, irq %d, hwaddr",
1116          dev->name, local->manf_str,(u_long)dev->base_addr, (int)dev->irq);
1117     for (i = 0; i < 6; i++)
1118         printk("%c%02X", i?':':' ', dev->dev_addr[i]);
1119     printk("\n");
1120
1121     return;
1122
1123   config_error:
1124     link->state &= ~DEV_CONFIG_PENDING;
1125     xirc2ps_release(link);
1126     return;
1127
1128   cis_error:
1129     printk(KNOT_XIRC "unable to parse CIS\n");
1130   failure:
1131     link->state &= ~DEV_CONFIG_PENDING;
1132 } /* xirc2ps_config */
1133
1134 /****************
1135  * After a card is removed, xirc2ps_release() will unregister the net
1136  * device, and release the PCMCIA configuration.  If the device is
1137  * still open, this will be postponed until it is closed.
1138  */
1139 static void
1140 xirc2ps_release(dev_link_t *link)
1141 {
1142
1143     DEBUG(0, "release(0x%p)\n", link);
1144
1145     if (link->win) {
1146         struct net_device *dev = link->priv;
1147         local_info_t *local = netdev_priv(dev);
1148         if (local->dingo)
1149             iounmap(local->dingo_ccr - 0x0800);
1150         pcmcia_release_window(link->win);
1151     }
1152     pcmcia_release_configuration(link->handle);
1153     pcmcia_release_io(link->handle, &link->io);
1154     pcmcia_release_irq(link->handle, &link->irq);
1155     link->state &= ~DEV_CONFIG;
1156
1157 } /* xirc2ps_release */
1158
1159 /*====================================================================*/
1160
1161 /****************
1162  * The card status event handler.  Mostly, this schedules other
1163  * stuff to run after an event is received.  A CARD_REMOVAL event
1164  * also sets some flags to discourage the net drivers from trying
1165  * to talk to the card any more.
1166  *
1167  * When a CARD_REMOVAL event is received, we immediately set a flag
1168  * to block future accesses to this device.  All the functions that
1169  * actually access the device should check this flag to make sure
1170  * the card is still present.
1171  */
1172
1173 static int
1174 xirc2ps_event(event_t event, int priority,
1175               event_callback_args_t * args)
1176 {
1177     dev_link_t *link = args->client_data;
1178     struct net_device *dev = link->priv;
1179
1180     DEBUG(0, "event(%d)\n", (int)event);
1181
1182     switch (event) {
1183     case CS_EVENT_REGISTRATION_COMPLETE:
1184         DEBUG(0, "registration complete\n");
1185         break;
1186     case CS_EVENT_CARD_REMOVAL:
1187         link->state &= ~DEV_PRESENT;
1188         if (link->state & DEV_CONFIG)
1189             netif_device_detach(dev);
1190         break;
1191     case CS_EVENT_CARD_INSERTION:
1192         link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
1193         xirc2ps_config(link);
1194         break;
1195     case CS_EVENT_PM_SUSPEND:
1196         link->state |= DEV_SUSPEND;
1197         /* Fall through... */
1198     case CS_EVENT_RESET_PHYSICAL:
1199         if (link->state & DEV_CONFIG) {
1200             if (link->open) {
1201                 netif_device_detach(dev);
1202                 do_powerdown(dev);
1203             }
1204             pcmcia_release_configuration(link->handle);
1205         }
1206         break;
1207     case CS_EVENT_PM_RESUME:
1208         link->state &= ~DEV_SUSPEND;
1209         /* Fall through... */
1210     case CS_EVENT_CARD_RESET:
1211         if (link->state & DEV_CONFIG) {
1212             pcmcia_request_configuration(link->handle, &link->conf);
1213             if (link->open) {
1214                 do_reset(dev,1);
1215                 netif_device_attach(dev);
1216             }
1217         }
1218         break;
1219     }
1220     return 0;
1221 } /* xirc2ps_event */
1222
1223 /*====================================================================*/
1224
1225 /****************
1226  * This is the Interrupt service route.
1227  */
1228 static irqreturn_t
1229 xirc2ps_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1230 {
1231     struct net_device *dev = (struct net_device *)dev_id;
1232     local_info_t *lp = netdev_priv(dev);
1233     kio_addr_t ioaddr;
1234     u_char saved_page;
1235     unsigned bytes_rcvd;
1236     unsigned int_status, eth_status, rx_status, tx_status;
1237     unsigned rsr, pktlen;
1238     ulong start_ticks = jiffies; /* fixme: jiffies rollover every 497 days
1239                                   * is this something to worry about?
1240                                   * -- on a laptop?
1241                                   */
1242
1243     if (!netif_device_present(dev))
1244         return IRQ_HANDLED;
1245
1246     ioaddr = dev->base_addr;
1247     if (lp->mohawk) { /* must disable the interrupt */
1248         PutByte(XIRCREG_CR, 0);
1249     }
1250
1251     DEBUG(6, "%s: interrupt %d at %#x.\n", dev->name, irq, ioaddr);
1252
1253     saved_page = GetByte(XIRCREG_PR);
1254     /* Read the ISR to see whats the cause for the interrupt.
1255      * This also clears the interrupt flags on CE2 cards
1256      */
1257     int_status = GetByte(XIRCREG_ISR);
1258     bytes_rcvd = 0;
1259   loop_entry:
1260     if (int_status == 0xff) { /* card may be ejected */
1261         DEBUG(3, "%s: interrupt %d for dead card\n", dev->name, irq);
1262         goto leave;
1263     }
1264     eth_status = GetByte(XIRCREG_ESR);
1265
1266     SelectPage(0x40);
1267     rx_status  = GetByte(XIRCREG40_RXST0);
1268     PutByte(XIRCREG40_RXST0, (~rx_status & 0xff));
1269     tx_status = GetByte(XIRCREG40_TXST0);
1270     tx_status |= GetByte(XIRCREG40_TXST1) << 8;
1271     PutByte(XIRCREG40_TXST0, 0);
1272     PutByte(XIRCREG40_TXST1, 0);
1273
1274     DEBUG(3, "%s: ISR=%#2.2x ESR=%#2.2x RSR=%#2.2x TSR=%#4.4x\n",
1275           dev->name, int_status, eth_status, rx_status, tx_status);
1276
1277     /***** receive section ******/
1278     SelectPage(0);
1279     while (eth_status & FullPktRcvd) {
1280         rsr = GetByte(XIRCREG0_RSR);
1281         if (bytes_rcvd > maxrx_bytes && (rsr & PktRxOk)) {
1282             /* too many bytes received during this int, drop the rest of the
1283              * packets */
1284             lp->stats.rx_dropped++;
1285             DEBUG(2, "%s: RX drop, too much done\n", dev->name);
1286         } else if (rsr & PktRxOk) {
1287             struct sk_buff *skb;
1288
1289             pktlen = GetWord(XIRCREG0_RBC);
1290             bytes_rcvd += pktlen;
1291
1292             DEBUG(5, "rsr=%#02x packet_length=%u\n", rsr, pktlen);
1293
1294             skb = dev_alloc_skb(pktlen+3); /* 1 extra so we can use insw */
1295             if (!skb) {
1296                 printk(KNOT_XIRC "low memory, packet dropped (size=%u)\n",
1297                        pktlen);
1298                 lp->stats.rx_dropped++;
1299             } else { /* okay get the packet */
1300                 skb_reserve(skb, 2);
1301                 if (lp->silicon == 0 ) { /* work around a hardware bug */
1302                     unsigned rhsa; /* receive start address */
1303
1304                     SelectPage(5);
1305                     rhsa = GetWord(XIRCREG5_RHSA0);
1306                     SelectPage(0);
1307                     rhsa += 3; /* skip control infos */
1308                     if (rhsa >= 0x8000)
1309                         rhsa = 0;
1310                     if (rhsa + pktlen > 0x8000) {
1311                         unsigned i;
1312                         u_char *buf = skb_put(skb, pktlen);
1313                         for (i=0; i < pktlen ; i++, rhsa++) {
1314                             buf[i] = GetByte(XIRCREG_EDP);
1315                             if (rhsa == 0x8000) {
1316                                 rhsa = 0;
1317                                 i--;
1318                             }
1319                         }
1320                     } else {
1321                         insw(ioaddr+XIRCREG_EDP,
1322                                 skb_put(skb, pktlen), (pktlen+1)>>1);
1323                     }
1324                 }
1325               #if 0
1326                 else if (lp->mohawk) {
1327                     /* To use this 32 bit access we should use
1328                      * a manual optimized loop
1329                      * Also the words are swapped, we can get more
1330                      * performance by using 32 bit access and swapping
1331                      * the words in a register. Will need this for cardbus
1332                      *
1333                      * Note: don't forget to change the ALLOC_SKB to .. +3
1334                      */
1335                     unsigned i;
1336                     u_long *p = skb_put(skb, pktlen);
1337                     register u_long a;
1338                     kio_addr_t edpreg = ioaddr+XIRCREG_EDP-2;
1339                     for (i=0; i < len ; i += 4, p++) {
1340                         a = inl(edpreg);
1341                         __asm__("rorl $16,%0\n\t"
1342                                 :"=q" (a)
1343                                 : "0" (a));
1344                         *p = a;
1345                     }
1346                 }
1347               #endif
1348                 else {
1349                     insw(ioaddr+XIRCREG_EDP, skb_put(skb, pktlen),
1350                             (pktlen+1)>>1);
1351                 }
1352                 skb->protocol = eth_type_trans(skb, dev);
1353                 skb->dev = dev;
1354                 netif_rx(skb);
1355                 dev->last_rx = jiffies;
1356                 lp->stats.rx_packets++;
1357                 lp->stats.rx_bytes += pktlen;
1358                 if (!(rsr & PhyPkt))
1359                     lp->stats.multicast++;
1360             }
1361         } else { /* bad packet */
1362             DEBUG(5, "rsr=%#02x\n", rsr);
1363         }
1364         if (rsr & PktTooLong) {
1365             lp->stats.rx_frame_errors++;
1366             DEBUG(3, "%s: Packet too long\n", dev->name);
1367         }
1368         if (rsr & CRCErr) {
1369             lp->stats.rx_crc_errors++;
1370             DEBUG(3, "%s: CRC error\n", dev->name);
1371         }
1372         if (rsr & AlignErr) {
1373             lp->stats.rx_fifo_errors++; /* okay ? */
1374             DEBUG(3, "%s: Alignment error\n", dev->name);
1375         }
1376
1377         /* clear the received/dropped/error packet */
1378         PutWord(XIRCREG0_DO, 0x8000); /* issue cmd: skip_rx_packet */
1379
1380         /* get the new ethernet status */
1381         eth_status = GetByte(XIRCREG_ESR);
1382     }
1383     if (rx_status & 0x10) { /* Receive overrun */
1384         lp->stats.rx_over_errors++;
1385         PutByte(XIRCREG_CR, ClearRxOvrun);
1386         DEBUG(3, "receive overrun cleared\n");
1387     }
1388
1389     /***** transmit section ******/
1390     if (int_status & PktTxed) {
1391         unsigned n, nn;
1392
1393         n = lp->last_ptr_value;
1394         nn = GetByte(XIRCREG0_PTR);
1395         lp->last_ptr_value = nn;
1396         if (nn < n) /* rollover */
1397             lp->stats.tx_packets += 256 - n;
1398         else if (n == nn) { /* happens sometimes - don't know why */
1399             DEBUG(0, "PTR not changed?\n");
1400         } else
1401             lp->stats.tx_packets += lp->last_ptr_value - n;
1402         netif_wake_queue(dev);
1403     }
1404     if (tx_status & 0x0002) {   /* Execessive collissions */
1405         DEBUG(0, "tx restarted due to execssive collissions\n");
1406         PutByte(XIRCREG_CR, RestartTx);  /* restart transmitter process */
1407     }
1408     if (tx_status & 0x0040)
1409         lp->stats.tx_aborted_errors++;
1410
1411     /* recalculate our work chunk so that we limit the duration of this
1412      * ISR to about 1/10 of a second.
1413      * Calculate only if we received a reasonable amount of bytes.
1414      */
1415     if (bytes_rcvd > 1000) {
1416         u_long duration = jiffies - start_ticks;
1417
1418         if (duration >= HZ/10) { /* if more than about 1/10 second */
1419             maxrx_bytes = (bytes_rcvd * (HZ/10)) / duration;
1420             if (maxrx_bytes < 2000)
1421                 maxrx_bytes = 2000;
1422             else if (maxrx_bytes > 22000)
1423                 maxrx_bytes = 22000;
1424             DEBUG(1, "set maxrx=%u (rcvd=%u ticks=%lu)\n",
1425                   maxrx_bytes, bytes_rcvd, duration);
1426         } else if (!duration && maxrx_bytes < 22000) {
1427             /* now much faster */
1428             maxrx_bytes += 2000;
1429             if (maxrx_bytes > 22000)
1430                 maxrx_bytes = 22000;
1431             DEBUG(1, "set maxrx=%u\n", maxrx_bytes);
1432         }
1433     }
1434
1435   leave:
1436     if (lockup_hack) {
1437         if (int_status != 0xff && (int_status = GetByte(XIRCREG_ISR)) != 0)
1438             goto loop_entry;
1439     }
1440     SelectPage(saved_page);
1441     PutByte(XIRCREG_CR, EnableIntr);  /* re-enable interrupts */
1442     /* Instead of dropping packets during a receive, we could
1443      * force an interrupt with this command:
1444      *    PutByte(XIRCREG_CR, EnableIntr|ForceIntr);
1445      */
1446     return IRQ_HANDLED;
1447 } /* xirc2ps_interrupt */
1448
1449 /*====================================================================*/
1450
1451 static void
1452 do_tx_timeout(struct net_device *dev)
1453 {
1454     local_info_t *lp = netdev_priv(dev);
1455     printk(KERN_NOTICE "%s: transmit timed out\n", dev->name);
1456     lp->stats.tx_errors++;
1457     /* reset the card */
1458     do_reset(dev,1);
1459     dev->trans_start = jiffies;
1460     netif_wake_queue(dev);
1461 }
1462
1463 static int
1464 do_start_xmit(struct sk_buff *skb, struct net_device *dev)
1465 {
1466     local_info_t *lp = netdev_priv(dev);
1467     kio_addr_t ioaddr = dev->base_addr;
1468     int okay;
1469     unsigned freespace;
1470     unsigned pktlen = skb? skb->len : 0;
1471
1472     DEBUG(1, "do_start_xmit(skb=%p, dev=%p) len=%u\n",
1473           skb, dev, pktlen);
1474
1475
1476     /* adjust the packet length to min. required
1477      * and hope that the buffer is large enough
1478      * to provide some random data.
1479      * fixme: For Mohawk we can change this by sending
1480      * a larger packetlen than we actually have; the chip will
1481      * pad this in his buffer with random bytes
1482      */
1483     if (pktlen < ETH_ZLEN)
1484     {
1485         skb = skb_padto(skb, ETH_ZLEN);
1486         if (skb == NULL)
1487                 return 0;
1488         pktlen = ETH_ZLEN;
1489     }
1490
1491     netif_stop_queue(dev);
1492     SelectPage(0);
1493     PutWord(XIRCREG0_TRS, (u_short)pktlen+2);
1494     freespace = GetWord(XIRCREG0_TSO);
1495     okay = freespace & 0x8000;
1496     freespace &= 0x7fff;
1497     /* TRS doesn't work - (indeed it is eliminated with sil-rev 1) */
1498     okay = pktlen +2 < freespace;
1499     DEBUG(2 + (okay ? 2 : 0), "%s: avail. tx space=%u%s\n",
1500           dev->name, freespace, okay ? " (okay)":" (not enough)");
1501     if (!okay) { /* not enough space */
1502         return 1;  /* upper layer may decide to requeue this packet */
1503     }
1504     /* send the packet */
1505     PutWord(XIRCREG_EDP, (u_short)pktlen);
1506     outsw(ioaddr+XIRCREG_EDP, skb->data, pktlen>>1);
1507     if (pktlen & 1)
1508         PutByte(XIRCREG_EDP, skb->data[pktlen-1]);
1509
1510     if (lp->mohawk)
1511         PutByte(XIRCREG_CR, TransmitPacket|EnableIntr);
1512
1513     dev_kfree_skb (skb);
1514     dev->trans_start = jiffies;
1515     lp->stats.tx_bytes += pktlen;
1516     netif_start_queue(dev);
1517     return 0;
1518 }
1519
1520 static struct net_device_stats *
1521 do_get_stats(struct net_device *dev)
1522 {
1523     local_info_t *lp = netdev_priv(dev);
1524
1525     /*  lp->stats.rx_missed_errors = GetByte(?) */
1526     return &lp->stats;
1527 }
1528
1529 /****************
1530  * Set all addresses: This first one is the individual address,
1531  * the next 9 addresses are taken from the multicast list and
1532  * the rest is filled with the individual address.
1533  */
1534 static void
1535 set_addresses(struct net_device *dev)
1536 {
1537     kio_addr_t ioaddr = dev->base_addr;
1538     local_info_t *lp = netdev_priv(dev);
1539     struct dev_mc_list *dmi = dev->mc_list;
1540     char *addr;
1541     int i,j,k,n;
1542
1543     SelectPage(k=0x50);
1544     for (i=0,j=8,n=0; ; i++, j++) {
1545         if (i > 5) {
1546             if (++n > 9)
1547                 break;
1548             i = 0;
1549         }
1550         if (j > 15) {
1551             j = 8;
1552             k++;
1553             SelectPage(k);
1554         }
1555
1556         if (n && n <= dev->mc_count && dmi) {
1557             addr = dmi->dmi_addr;
1558             dmi = dmi->next;
1559         } else
1560             addr = dev->dev_addr;
1561
1562         if (lp->mohawk)
1563             PutByte(j, addr[5-i]);
1564         else
1565             PutByte(j, addr[i]);
1566     }
1567     SelectPage(0);
1568 }
1569
1570 /****************
1571  * Set or clear the multicast filter for this adaptor.
1572  * We can filter up to 9 addresses, if more are requested we set
1573  * multicast promiscuous mode.
1574  */
1575
1576 static void
1577 set_multicast_list(struct net_device *dev)
1578 {
1579     kio_addr_t ioaddr = dev->base_addr;
1580
1581     SelectPage(0x42);
1582     if (dev->flags & IFF_PROMISC) { /* snoop */
1583         PutByte(XIRCREG42_SWC1, 0x06); /* set MPE and PME */
1584     } else if (dev->mc_count > 9 || (dev->flags & IFF_ALLMULTI)) {
1585         PutByte(XIRCREG42_SWC1, 0x06); /* set MPE */
1586     } else if (dev->mc_count) {
1587         /* the chip can filter 9 addresses perfectly */
1588         PutByte(XIRCREG42_SWC1, 0x00);
1589         SelectPage(0x40);
1590         PutByte(XIRCREG40_CMD0, Offline);
1591         set_addresses(dev);
1592         SelectPage(0x40);
1593         PutByte(XIRCREG40_CMD0, EnableRecv | Online);
1594     } else { /* standard usage */
1595         PutByte(XIRCREG42_SWC1, 0x00);
1596     }
1597     SelectPage(0);
1598 }
1599
1600 static int
1601 do_config(struct net_device *dev, struct ifmap *map)
1602 {
1603     local_info_t *local = netdev_priv(dev);
1604
1605     DEBUG(0, "do_config(%p)\n", dev);
1606     if (map->port != 255 && map->port != dev->if_port) {
1607         if (map->port > 4)
1608             return -EINVAL;
1609         if (!map->port) {
1610             local->probe_port = 1;
1611             dev->if_port = 1;
1612         } else {
1613             local->probe_port = 0;
1614             dev->if_port = map->port;
1615         }
1616         printk(KERN_INFO "%s: switching to %s port\n",
1617                dev->name, if_names[dev->if_port]);
1618         do_reset(dev,1);  /* not the fine way :-) */
1619     }
1620     return 0;
1621 }
1622
1623 /****************
1624  * Open the driver
1625  */
1626 static int
1627 do_open(struct net_device *dev)
1628 {
1629     local_info_t *lp = netdev_priv(dev);
1630     dev_link_t *link = &lp->link;
1631
1632     DEBUG(0, "do_open(%p)\n", dev);
1633
1634     /* Check that the PCMCIA card is still here. */
1635     /* Physical device present signature. */
1636     if (!DEV_OK(link))
1637         return -ENODEV;
1638
1639     /* okay */
1640     link->open++;
1641
1642     netif_start_queue(dev);
1643     do_reset(dev,1);
1644
1645     return 0;
1646 }
1647
1648 static void netdev_get_drvinfo(struct net_device *dev,
1649                                struct ethtool_drvinfo *info)
1650 {
1651         strcpy(info->driver, "xirc2ps_cs");
1652         sprintf(info->bus_info, "PCMCIA 0x%lx", dev->base_addr);
1653 }
1654
1655 static struct ethtool_ops netdev_ethtool_ops = {
1656         .get_drvinfo            = netdev_get_drvinfo,
1657 };
1658
1659 static int
1660 do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1661 {
1662     local_info_t *local = netdev_priv(dev);
1663     kio_addr_t ioaddr = dev->base_addr;
1664     u16 *data = (u16 *)&rq->ifr_ifru;
1665
1666     DEBUG(1, "%s: ioctl(%-.6s, %#04x) %04x %04x %04x %04x\n",
1667           dev->name, rq->ifr_ifrn.ifrn_name, cmd,
1668           data[0], data[1], data[2], data[3]);
1669
1670     if (!local->mohawk)
1671         return -EOPNOTSUPP;
1672
1673     switch(cmd) {
1674       case SIOCGMIIPHY:         /* Get the address of the PHY in use. */
1675         data[0] = 0;            /* we have only this address */
1676         /* fall trough */
1677       case SIOCGMIIREG:         /* Read the specified MII register. */
1678         data[3] = mii_rd(ioaddr, data[0] & 0x1f, data[1] & 0x1f);
1679         break;
1680       case SIOCSMIIREG:         /* Write the specified MII register */
1681         if (!capable(CAP_NET_ADMIN))
1682             return -EPERM;
1683         mii_wr(ioaddr, data[0] & 0x1f, data[1] & 0x1f, data[2], 16);
1684         break;
1685       default:
1686         return -EOPNOTSUPP;
1687     }
1688     return 0;
1689 }
1690
1691 static void
1692 hardreset(struct net_device *dev)
1693 {
1694     local_info_t *local = netdev_priv(dev);
1695     kio_addr_t ioaddr = dev->base_addr;
1696
1697     SelectPage(4);
1698     udelay(1);
1699     PutByte(XIRCREG4_GPR1, 0);       /* clear bit 0: power down */
1700     msleep(40);                              /* wait 40 msec */
1701     if (local->mohawk)
1702         PutByte(XIRCREG4_GPR1, 1);       /* set bit 0: power up */
1703     else
1704         PutByte(XIRCREG4_GPR1, 1 | 4);   /* set bit 0: power up, bit 2: AIC */
1705     msleep(20);                      /* wait 20 msec */
1706 }
1707
1708 static void
1709 do_reset(struct net_device *dev, int full)
1710 {
1711     local_info_t *local = netdev_priv(dev);
1712     kio_addr_t ioaddr = dev->base_addr;
1713     unsigned value;
1714
1715     DEBUG(0, "%s: do_reset(%p,%d)\n", dev? dev->name:"eth?", dev, full);
1716
1717     hardreset(dev);
1718     PutByte(XIRCREG_CR, SoftReset); /* set */
1719     msleep(20);                      /* wait 20 msec */
1720     PutByte(XIRCREG_CR, 0);          /* clear */
1721     msleep(40);                      /* wait 40 msec */
1722     if (local->mohawk) {
1723         SelectPage(4);
1724         /* set pin GP1 and GP2 to output  (0x0c)
1725          * set GP1 to low to power up the ML6692 (0x00)
1726          * set GP2 to high to power up the 10Mhz chip  (0x02)
1727          */
1728         PutByte(XIRCREG4_GPR0, 0x0e);
1729     }
1730
1731     /* give the circuits some time to power up */
1732     msleep(500);                        /* about 500ms */
1733
1734     local->last_ptr_value = 0;
1735     local->silicon = local->mohawk ? (GetByte(XIRCREG4_BOV) & 0x70) >> 4
1736                                    : (GetByte(XIRCREG4_BOV) & 0x30) >> 4;
1737
1738     if (local->probe_port) {
1739         if (!local->mohawk) {
1740             SelectPage(4);
1741             PutByte(XIRCREG4_GPR0, 4);
1742             local->probe_port = 0;
1743         }
1744     } else if (dev->if_port == 2) { /* enable 10Base2 */
1745         SelectPage(0x42);
1746         PutByte(XIRCREG42_SWC1, 0xC0);
1747     } else { /* enable 10BaseT */
1748         SelectPage(0x42);
1749         PutByte(XIRCREG42_SWC1, 0x80);
1750     }
1751     msleep(40);                      /* wait 40 msec to let it complete */
1752
1753   #ifdef PCMCIA_DEBUG
1754     if (pc_debug) {
1755         SelectPage(0);
1756         value = GetByte(XIRCREG_ESR);    /* read the ESR */
1757         printk(KERN_DEBUG "%s: ESR is: %#02x\n", dev->name, value);
1758     }
1759   #endif
1760
1761     /* setup the ECR */
1762     SelectPage(1);
1763     PutByte(XIRCREG1_IMR0, 0xff); /* allow all ints */
1764     PutByte(XIRCREG1_IMR1, 1    ); /* and Set TxUnderrunDetect */
1765     value = GetByte(XIRCREG1_ECR);
1766   #if 0
1767     if (local->mohawk)
1768         value |= DisableLinkPulse;
1769     PutByte(XIRCREG1_ECR, value);
1770   #endif
1771     DEBUG(0, "%s: ECR is: %#02x\n", dev->name, value);
1772
1773     SelectPage(0x42);
1774     PutByte(XIRCREG42_SWC0, 0x20); /* disable source insertion */
1775
1776     if (local->silicon != 1) {
1777         /* set the local memory dividing line.
1778          * The comments in the sample code say that this is only
1779          * settable with the scipper version 2 which is revision 0.
1780          * Always for CE3 cards
1781          */
1782         SelectPage(2);
1783         PutWord(XIRCREG2_RBS, 0x2000);
1784     }
1785
1786     if (full)
1787         set_addresses(dev);
1788
1789     /* Hardware workaround:
1790      * The receive byte pointer after reset is off by 1 so we need
1791      * to move the offset pointer back to 0.
1792      */
1793     SelectPage(0);
1794     PutWord(XIRCREG0_DO, 0x2000); /* change offset command, off=0 */
1795
1796     /* setup MAC IMRs and clear status registers */
1797     SelectPage(0x40);                /* Bit 7 ... bit 0 */
1798     PutByte(XIRCREG40_RMASK0, 0xff); /* ROK, RAB, rsv, RO, CRC, AE, PTL, MP */
1799     PutByte(XIRCREG40_TMASK0, 0xff); /* TOK, TAB, SQE, LL, TU, JAB, EXC, CRS */
1800     PutByte(XIRCREG40_TMASK1, 0xb0); /* rsv, rsv, PTD, EXT, rsv,rsv,rsv, rsv*/
1801     PutByte(XIRCREG40_RXST0,  0x00); /* ROK, RAB, REN, RO, CRC, AE, PTL, MP */
1802     PutByte(XIRCREG40_TXST0,  0x00); /* TOK, TAB, SQE, LL, TU, JAB, EXC, CRS */
1803     PutByte(XIRCREG40_TXST1,  0x00); /* TEN, rsv, PTD, EXT, retry_counter:4  */
1804
1805     if (full && local->mohawk && init_mii(dev)) {
1806         if (dev->if_port == 4 || local->dingo || local->new_mii) {
1807             printk(KERN_INFO "%s: MII selected\n", dev->name);
1808             SelectPage(2);
1809             PutByte(XIRCREG2_MSR, GetByte(XIRCREG2_MSR) | 0x08);
1810             msleep(20);
1811         } else {
1812             printk(KERN_INFO "%s: MII detected; using 10mbs\n",
1813                    dev->name);
1814             SelectPage(0x42);
1815             if (dev->if_port == 2) /* enable 10Base2 */
1816                 PutByte(XIRCREG42_SWC1, 0xC0);
1817             else  /* enable 10BaseT */
1818                 PutByte(XIRCREG42_SWC1, 0x80);
1819             msleep(40);                 /* wait 40 msec to let it complete */
1820         }
1821         if (full_duplex)
1822             PutByte(XIRCREG1_ECR, GetByte(XIRCREG1_ECR | FullDuplex));
1823     } else {  /* No MII */
1824         SelectPage(0);
1825         value = GetByte(XIRCREG_ESR);    /* read the ESR */
1826         dev->if_port = (value & MediaSelect) ? 1 : 2;
1827     }
1828
1829     /* configure the LEDs */
1830     SelectPage(2);
1831     if (dev->if_port == 1 || dev->if_port == 4) /* TP: Link and Activity */
1832         PutByte(XIRCREG2_LED, 0x3b);
1833     else                              /* Coax: Not-Collision and Activity */
1834         PutByte(XIRCREG2_LED, 0x3a);
1835
1836     if (local->dingo)
1837         PutByte(0x0b, 0x04); /* 100 Mbit LED */
1838
1839     /* enable receiver and put the mac online */
1840     if (full) {
1841         SelectPage(0x40);
1842         PutByte(XIRCREG40_CMD0, EnableRecv | Online);
1843     }
1844
1845     /* setup Ethernet IMR and enable interrupts */
1846     SelectPage(1);
1847     PutByte(XIRCREG1_IMR0, 0xff);
1848     udelay(1);
1849     SelectPage(0);
1850     PutByte(XIRCREG_CR, EnableIntr);
1851     if (local->modem && !local->dingo) { /* do some magic */
1852         if (!(GetByte(0x10) & 0x01))
1853             PutByte(0x10, 0x11); /* unmask master-int bit */
1854     }
1855
1856     if (full)
1857         printk(KERN_INFO "%s: media %s, silicon revision %d\n",
1858                dev->name, if_names[dev->if_port], local->silicon);
1859     /* We should switch back to page 0 to avoid a bug in revision 0
1860      * where regs with offset below 8 can't be read after an access
1861      * to the MAC registers */
1862     SelectPage(0);
1863 }
1864
1865 /****************
1866  * Initialize the Media-Independent-Interface
1867  * Returns: True if we have a good MII
1868  */
1869 static int
1870 init_mii(struct net_device *dev)
1871 {
1872     local_info_t *local = netdev_priv(dev);
1873     kio_addr_t ioaddr = dev->base_addr;
1874     unsigned control, status, linkpartner;
1875     int i;
1876
1877     if (if_port == 4 || if_port == 1) { /* force 100BaseT or 10BaseT */
1878         dev->if_port = if_port;
1879         local->probe_port = 0;
1880         return 1;
1881     }
1882
1883     status = mii_rd(ioaddr,  0, 1);
1884     if ((status & 0xff00) != 0x7800)
1885         return 0; /* No MII */
1886
1887     local->new_mii = (mii_rd(ioaddr, 0, 2) != 0xffff);
1888     
1889     if (local->probe_port)
1890         control = 0x1000; /* auto neg */
1891     else if (dev->if_port == 4)
1892         control = 0x2000; /* no auto neg, 100mbs mode */
1893     else
1894         control = 0x0000; /* no auto neg, 10mbs mode */
1895     mii_wr(ioaddr,  0, 0, control, 16);
1896     udelay(100);
1897     control = mii_rd(ioaddr, 0, 0);
1898
1899     if (control & 0x0400) {
1900         printk(KERN_NOTICE "%s can't take PHY out of isolation mode\n",
1901                dev->name);
1902         local->probe_port = 0;
1903         return 0;
1904     }
1905
1906     if (local->probe_port) {
1907         /* according to the DP83840A specs the auto negotiation process
1908          * may take up to 3.5 sec, so we use this also for our ML6692
1909          * Fixme: Better to use a timer here!
1910          */
1911         for (i=0; i < 35; i++) {
1912             msleep(100);         /* wait 100 msec */
1913             status = mii_rd(ioaddr,  0, 1);
1914             if ((status & 0x0020) && (status & 0x0004))
1915                 break;
1916         }
1917
1918         if (!(status & 0x0020)) {
1919             printk(KERN_INFO "%s: autonegotiation failed;"
1920                    " using 10mbs\n", dev->name);
1921             if (!local->new_mii) {
1922                 control = 0x0000;
1923                 mii_wr(ioaddr,  0, 0, control, 16);
1924                 udelay(100);
1925                 SelectPage(0);
1926                 dev->if_port = (GetByte(XIRCREG_ESR) & MediaSelect) ? 1 : 2;
1927             }
1928         } else {
1929             linkpartner = mii_rd(ioaddr, 0, 5);
1930             printk(KERN_INFO "%s: MII link partner: %04x\n",
1931                    dev->name, linkpartner);
1932             if (linkpartner & 0x0080) {
1933                 dev->if_port = 4;
1934             } else
1935                 dev->if_port = 1;
1936         }
1937     }
1938
1939     return 1;
1940 }
1941
1942 static void
1943 do_powerdown(struct net_device *dev)
1944 {
1945
1946     kio_addr_t ioaddr = dev->base_addr;
1947
1948     DEBUG(0, "do_powerdown(%p)\n", dev);
1949
1950     SelectPage(4);
1951     PutByte(XIRCREG4_GPR1, 0);       /* clear bit 0: power down */
1952     SelectPage(0);
1953 }
1954
1955 static int
1956 do_stop(struct net_device *dev)
1957 {
1958     kio_addr_t ioaddr = dev->base_addr;
1959     local_info_t *lp = netdev_priv(dev);
1960     dev_link_t *link = &lp->link;
1961
1962     DEBUG(0, "do_stop(%p)\n", dev);
1963
1964     if (!link)
1965         return -ENODEV;
1966
1967     netif_stop_queue(dev);
1968
1969     SelectPage(0);
1970     PutByte(XIRCREG_CR, 0);  /* disable interrupts */
1971     SelectPage(0x01);
1972     PutByte(XIRCREG1_IMR0, 0x00); /* forbid all ints */
1973     SelectPage(4);
1974     PutByte(XIRCREG4_GPR1, 0);  /* clear bit 0: power down */
1975     SelectPage(0);
1976
1977     link->open--;
1978     return 0;
1979 }
1980
1981 static struct pcmcia_device_id xirc2ps_ids[] = {
1982         PCMCIA_PFC_DEVICE_MANF_CARD(0, 0x0089, 0x110a),
1983         PCMCIA_PFC_DEVICE_MANF_CARD(0, 0x0138, 0x110a),
1984         PCMCIA_PFC_DEVICE_PROD_ID13(0, "Xircom", "CEM28", 0x2e3ee845, 0x0ea978ea),
1985         PCMCIA_PFC_DEVICE_PROD_ID13(0, "Xircom", "CEM33", 0x2e3ee845, 0x80609023),
1986         PCMCIA_PFC_DEVICE_PROD_ID13(0, "Xircom", "CEM56", 0x2e3ee845, 0xa650c32a),
1987         PCMCIA_PFC_DEVICE_PROD_ID13(0, "Xircom", "REM10", 0x2e3ee845, 0x76df1d29),
1988         PCMCIA_PFC_DEVICE_PROD_ID13(0, "Xircom", "XEM5600", 0x2e3ee845, 0xf1403719),
1989         PCMCIA_PFC_DEVICE_PROD_ID12(0, "Xircom", "CreditCard Ethernet", 0x2e3ee845, 0xc0e778c2),
1990         PCMCIA_DEVICE_MANF_CARD(0x01bf, 0x010a),
1991         PCMCIA_DEVICE_PROD_ID13("Toshiba Information Systems", "TPCENET", 0x1b3b94fe, 0xf381c1a2),
1992         PCMCIA_DEVICE_PROD_ID13("Xircom", "CE3-10/100", 0x2e3ee845, 0x0ec0ac37),
1993         PCMCIA_DEVICE_PROD_ID13("Xircom", "PS-CE2-10", 0x2e3ee845, 0x947d9073),
1994         PCMCIA_DEVICE_PROD_ID13("Xircom", "R2E-100BTX", 0x2e3ee845, 0x2464a6e3),
1995         PCMCIA_DEVICE_PROD_ID13("Xircom", "RE-10", 0x2e3ee845, 0x3e08d609),
1996         PCMCIA_DEVICE_PROD_ID13("Xircom", "XE2000", 0x2e3ee845, 0xf7188e46),
1997         PCMCIA_DEVICE_PROD_ID12("Compaq", "Ethernet LAN Card", 0x54f7c49c, 0x9fd2f0a2),
1998         PCMCIA_DEVICE_PROD_ID12("Compaq", "Netelligent 10/100 PC Card", 0x54f7c49c, 0xefe96769),
1999         PCMCIA_DEVICE_PROD_ID12("Intel", "EtherExpress(TM) PRO/100 PC Card Mobile Adapter16", 0x816cc815, 0x174397db),
2000         PCMCIA_DEVICE_PROD_ID12("Toshiba", "10/100 Ethernet PC Card", 0x44a09d9c, 0xb44deecf),
2001         /* also matches CFE-10 cards! */
2002         /* PCMCIA_DEVICE_MANF_CARD(0x0105, 0x010a), */
2003         PCMCIA_DEVICE_NULL,
2004 };
2005 MODULE_DEVICE_TABLE(pcmcia, xirc2ps_ids);
2006
2007
2008 static struct pcmcia_driver xirc2ps_cs_driver = {
2009         .owner          = THIS_MODULE,
2010         .drv            = {
2011                 .name   = "xirc2ps_cs",
2012         },
2013         .attach         = xirc2ps_attach,
2014         .event          = xirc2ps_event,
2015         .detach         = xirc2ps_detach,
2016         .id_table       = xirc2ps_ids,
2017 };
2018
2019 static int __init
2020 init_xirc2ps_cs(void)
2021 {
2022         return pcmcia_register_driver(&xirc2ps_cs_driver);
2023 }
2024
2025 static void __exit
2026 exit_xirc2ps_cs(void)
2027 {
2028         pcmcia_unregister_driver(&xirc2ps_cs_driver);
2029         BUG_ON(dev_list != NULL);
2030 }
2031
2032 module_init(init_xirc2ps_cs);
2033 module_exit(exit_xirc2ps_cs);
2034
2035 #ifndef MODULE
2036 static int __init setup_xirc2ps_cs(char *str)
2037 {
2038         /* if_port, full_duplex, do_sound, lockup_hack
2039          */
2040         int ints[10] = { -1 };
2041
2042         str = get_options(str, 9, ints);
2043
2044 #define MAYBE_SET(X,Y) if (ints[0] >= Y && ints[Y] != -1) { X = ints[Y]; }
2045         MAYBE_SET(if_port, 3);
2046         MAYBE_SET(full_duplex, 4);
2047         MAYBE_SET(do_sound, 5);
2048         MAYBE_SET(lockup_hack, 6);
2049 #undef  MAYBE_SET
2050
2051         return 0;
2052 }
2053
2054 __setup("xirc2ps_cs=", setup_xirc2ps_cs);
2055 #endif