xirc2ps_cs.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:62k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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