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