Back to home page

OSCL-LXR

 
 

    


0001 /*======================================================================
0002     fmvj18x_cs.c 2.8 2002/03/23
0003 
0004     A fmvj18x (and its compatibles) PCMCIA client driver
0005 
0006     Contributed by Shingo Fujimoto, shingo@flab.fujitsu.co.jp
0007 
0008     TDK LAK-CD021 and CONTEC C-NET(PC)C support added by 
0009     Nobuhiro Katayama, kata-n@po.iijnet.or.jp
0010 
0011     The PCMCIA client code is based on code written by David Hinds.
0012     Network code is based on the "FMV-18x driver" by Yutaka TAMIYA
0013     but is actually largely Donald Becker's AT1700 driver, which
0014     carries the following attribution:
0015 
0016     Written 1993-94 by Donald Becker.
0017 
0018     Copyright 1993 United States Government as represented by the
0019     Director, National Security Agency.
0020     
0021     This software may be used and distributed according to the terms
0022     of the GNU General Public License, incorporated herein by reference.
0023     
0024     The author may be reached as becker@scyld.com, or C/O
0025     Scyld Computing Corporation
0026     410 Severn Ave., Suite 210
0027     Annapolis MD 21403
0028    
0029 ======================================================================*/
0030 
0031 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0032 
0033 #define DRV_NAME    "fmvj18x_cs"
0034 #define DRV_VERSION "2.9"
0035 
0036 #include <linux/module.h>
0037 #include <linux/kernel.h>
0038 #include <linux/ptrace.h>
0039 #include <linux/slab.h>
0040 #include <linux/string.h>
0041 #include <linux/timer.h>
0042 #include <linux/interrupt.h>
0043 #include <linux/in.h>
0044 #include <linux/delay.h>
0045 #include <linux/ethtool.h>
0046 #include <linux/netdevice.h>
0047 #include <linux/etherdevice.h>
0048 #include <linux/skbuff.h>
0049 #include <linux/if_arp.h>
0050 #include <linux/ioport.h>
0051 #include <linux/crc32.h>
0052 
0053 #include <pcmcia/cistpl.h>
0054 #include <pcmcia/ciscode.h>
0055 #include <pcmcia/ds.h>
0056 
0057 #include <linux/uaccess.h>
0058 #include <asm/io.h>
0059 
0060 /*====================================================================*/
0061 
0062 /* Module parameters */
0063 
0064 MODULE_DESCRIPTION("fmvj18x and compatible PCMCIA ethernet driver");
0065 MODULE_LICENSE("GPL");
0066 
0067 #define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0)
0068 
0069 /* SRAM configuration */
0070 /* 0:4KB*2 TX buffer   else:8KB*2 TX buffer */
0071 INT_MODULE_PARM(sram_config, 0);
0072 
0073 
0074 /*====================================================================*/
0075 /*
0076     PCMCIA event handlers
0077  */
0078 static int fmvj18x_config(struct pcmcia_device *link);
0079 static int fmvj18x_get_hwinfo(struct pcmcia_device *link, u_char *node_id);
0080 static int fmvj18x_setup_mfc(struct pcmcia_device *link);
0081 static void fmvj18x_release(struct pcmcia_device *link);
0082 static void fmvj18x_detach(struct pcmcia_device *p_dev);
0083 
0084 /*
0085     LAN controller(MBH86960A) specific routines
0086  */
0087 static int fjn_config(struct net_device *dev, struct ifmap *map);
0088 static int fjn_open(struct net_device *dev);
0089 static int fjn_close(struct net_device *dev);
0090 static netdev_tx_t fjn_start_xmit(struct sk_buff *skb,
0091                     struct net_device *dev);
0092 static irqreturn_t fjn_interrupt(int irq, void *dev_id);
0093 static void fjn_rx(struct net_device *dev);
0094 static void fjn_reset(struct net_device *dev);
0095 static void set_rx_mode(struct net_device *dev);
0096 static void fjn_tx_timeout(struct net_device *dev, unsigned int txqueue);
0097 static const struct ethtool_ops netdev_ethtool_ops;
0098 
0099 /*
0100     card type
0101  */
0102 enum cardtype { MBH10302, MBH10304, TDK, CONTEC, LA501, UNGERMANN,
0103            XXX10304, NEC, KME
0104 };
0105 
0106 /*
0107     driver specific data structure
0108 */
0109 struct local_info {
0110     struct pcmcia_device    *p_dev;
0111     long open_time;
0112     uint tx_started:1;
0113     uint tx_queue;
0114     u_short tx_queue_len;
0115     enum cardtype cardtype;
0116     u_short sent;
0117     u_char __iomem *base;
0118 };
0119 
0120 #define MC_FILTERBREAK 64
0121 
0122 /*====================================================================*/
0123 /* 
0124     ioport offset from the base address 
0125  */
0126 #define TX_STATUS               0 /* transmit status register */
0127 #define RX_STATUS               1 /* receive status register */
0128 #define TX_INTR                 2 /* transmit interrupt mask register */
0129 #define RX_INTR                 3 /* receive interrupt mask register */
0130 #define TX_MODE                 4 /* transmit mode register */
0131 #define RX_MODE                 5 /* receive mode register */
0132 #define CONFIG_0                6 /* configuration register 0 */
0133 #define CONFIG_1                7 /* configuration register 1 */
0134 
0135 #define NODE_ID                 8 /* node ID register            (bank 0) */
0136 #define MAR_ADR                 8 /* multicast address registers (bank 1) */
0137 
0138 #define DATAPORT                8 /* buffer mem port registers   (bank 2) */
0139 #define TX_START               10 /* transmit start register */
0140 #define COL_CTRL               11 /* 16 collision control register */
0141 #define BMPR12                 12 /* reserved */
0142 #define BMPR13                 13 /* reserved */
0143 #define RX_SKIP                14 /* skip received packet register */
0144 
0145 #define LAN_CTRL               16 /* LAN card control register */
0146 
0147 #define MAC_ID               0x1a /* hardware address */
0148 #define UNGERMANN_MAC_ID     0x18 /* UNGERMANN-BASS hardware address */
0149 
0150 /* 
0151     control bits 
0152  */
0153 #define ENA_TMT_OK           0x80
0154 #define ENA_TMT_REC          0x20
0155 #define ENA_COL              0x04
0156 #define ENA_16_COL           0x02
0157 #define ENA_TBUS_ERR         0x01
0158 
0159 #define ENA_PKT_RDY          0x80
0160 #define ENA_BUS_ERR          0x40
0161 #define ENA_LEN_ERR          0x08
0162 #define ENA_ALG_ERR          0x04
0163 #define ENA_CRC_ERR          0x02
0164 #define ENA_OVR_FLO          0x01
0165 
0166 /* flags */
0167 #define F_TMT_RDY            0x80 /* can accept new packet */
0168 #define F_NET_BSY            0x40 /* carrier is detected */
0169 #define F_TMT_OK             0x20 /* send packet successfully */
0170 #define F_SRT_PKT            0x10 /* short packet error */
0171 #define F_COL_ERR            0x04 /* collision error */
0172 #define F_16_COL             0x02 /* 16 collision error */
0173 #define F_TBUS_ERR           0x01 /* bus read error */
0174 
0175 #define F_PKT_RDY            0x80 /* packet(s) in buffer */
0176 #define F_BUS_ERR            0x40 /* bus read error */
0177 #define F_LEN_ERR            0x08 /* short packet */
0178 #define F_ALG_ERR            0x04 /* frame error */
0179 #define F_CRC_ERR            0x02 /* CRC error */
0180 #define F_OVR_FLO            0x01 /* overflow error */
0181 
0182 #define F_BUF_EMP            0x40 /* receive buffer is empty */
0183 
0184 #define F_SKP_PKT            0x05 /* drop packet in buffer */
0185 
0186 /* default bitmaps */
0187 #define D_TX_INTR  ( ENA_TMT_OK )
0188 #define D_RX_INTR  ( ENA_PKT_RDY | ENA_LEN_ERR \
0189            | ENA_ALG_ERR | ENA_CRC_ERR | ENA_OVR_FLO )
0190 #define TX_STAT_M  ( F_TMT_RDY )
0191 #define RX_STAT_M  ( F_PKT_RDY | F_LEN_ERR \
0192                    | F_ALG_ERR | F_CRC_ERR | F_OVR_FLO )
0193 
0194 /* commands */
0195 #define D_TX_MODE            0x06 /* no tests, detect carrier */
0196 #define ID_MATCHED           0x02 /* (RX_MODE) */
0197 #define RECV_ALL             0x03 /* (RX_MODE) */
0198 #define CONFIG0_DFL          0x5a /* 16bit bus, 4K x 2 Tx queues */
0199 #define CONFIG0_DFL_1        0x5e /* 16bit bus, 8K x 2 Tx queues */
0200 #define CONFIG0_RST          0xda /* Data Link Controller off (CONFIG_0) */
0201 #define CONFIG0_RST_1        0xde /* Data Link Controller off (CONFIG_0) */
0202 #define BANK_0               0xa0 /* bank 0 (CONFIG_1) */
0203 #define BANK_1               0xa4 /* bank 1 (CONFIG_1) */
0204 #define BANK_2               0xa8 /* bank 2 (CONFIG_1) */
0205 #define CHIP_OFF             0x80 /* contrl chip power off (CONFIG_1) */
0206 #define DO_TX                0x80 /* do transmit packet */
0207 #define SEND_PKT             0x81 /* send a packet */
0208 #define AUTO_MODE            0x07 /* Auto skip packet on 16 col detected */
0209 #define MANU_MODE            0x03 /* Stop and skip packet on 16 col */
0210 #define TDK_AUTO_MODE        0x47 /* Auto skip packet on 16 col detected */
0211 #define TDK_MANU_MODE        0x43 /* Stop and skip packet on 16 col */
0212 #define INTR_OFF             0x0d /* LAN controller ignores interrupts */
0213 #define INTR_ON              0x1d /* LAN controller will catch interrupts */
0214 
0215 #define TX_TIMEOUT      ((400*HZ)/1000)
0216 
0217 #define BANK_0U              0x20 /* bank 0 (CONFIG_1) */
0218 #define BANK_1U              0x24 /* bank 1 (CONFIG_1) */
0219 #define BANK_2U              0x28 /* bank 2 (CONFIG_1) */
0220 
0221 static const struct net_device_ops fjn_netdev_ops = {
0222     .ndo_open       = fjn_open,
0223     .ndo_stop       = fjn_close,
0224     .ndo_start_xmit     = fjn_start_xmit,
0225     .ndo_tx_timeout     = fjn_tx_timeout,
0226     .ndo_set_config     = fjn_config,
0227     .ndo_set_rx_mode    = set_rx_mode,
0228     .ndo_set_mac_address    = eth_mac_addr,
0229     .ndo_validate_addr  = eth_validate_addr,
0230 };
0231 
0232 static int fmvj18x_probe(struct pcmcia_device *link)
0233 {
0234     struct local_info *lp;
0235     struct net_device *dev;
0236 
0237     dev_dbg(&link->dev, "fmvj18x_attach()\n");
0238 
0239     /* Make up a FMVJ18x specific data structure */
0240     dev = alloc_etherdev(sizeof(struct local_info));
0241     if (!dev)
0242     return -ENOMEM;
0243     lp = netdev_priv(dev);
0244     link->priv = dev;
0245     lp->p_dev = link;
0246     lp->base = NULL;
0247 
0248     /* The io structure describes IO port mapping */
0249     link->resource[0]->end = 32;
0250     link->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
0251 
0252     /* General socket configuration */
0253     link->config_flags |= CONF_ENABLE_IRQ;
0254 
0255     dev->netdev_ops = &fjn_netdev_ops;
0256     dev->watchdog_timeo = TX_TIMEOUT;
0257 
0258     dev->ethtool_ops = &netdev_ethtool_ops;
0259 
0260     return fmvj18x_config(link);
0261 } /* fmvj18x_attach */
0262 
0263 /*====================================================================*/
0264 
0265 static void fmvj18x_detach(struct pcmcia_device *link)
0266 {
0267     struct net_device *dev = link->priv;
0268 
0269     dev_dbg(&link->dev, "fmvj18x_detach\n");
0270 
0271     unregister_netdev(dev);
0272 
0273     fmvj18x_release(link);
0274 
0275     free_netdev(dev);
0276 } /* fmvj18x_detach */
0277 
0278 /*====================================================================*/
0279 
0280 static int mfc_try_io_port(struct pcmcia_device *link)
0281 {
0282     int i, ret;
0283     static const unsigned int serial_base[5] =
0284     { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
0285 
0286     for (i = 0; i < 5; i++) {
0287     link->resource[1]->start = serial_base[i];
0288     link->resource[1]->flags |= IO_DATA_PATH_WIDTH_8;
0289     if (link->resource[1]->start == 0) {
0290         link->resource[1]->end = 0;
0291         pr_notice("out of resource for serial\n");
0292     }
0293     ret = pcmcia_request_io(link);
0294     if (ret == 0)
0295         return ret;
0296     }
0297     return ret;
0298 }
0299 
0300 static int ungermann_try_io_port(struct pcmcia_device *link)
0301 {
0302     int ret;
0303     unsigned int ioaddr;
0304     /*
0305     Ungermann-Bass Access/CARD accepts 0x300,0x320,0x340,0x360
0306     0x380,0x3c0 only for ioport.
0307     */
0308     for (ioaddr = 0x300; ioaddr < 0x3e0; ioaddr += 0x20) {
0309     link->resource[0]->start = ioaddr;
0310     ret = pcmcia_request_io(link);
0311     if (ret == 0) {
0312         /* calculate ConfigIndex value */
0313         link->config_index =
0314         ((link->resource[0]->start & 0x0f0) >> 3) | 0x22;
0315         return ret;
0316     }
0317     }
0318     return ret; /* RequestIO failed */
0319 }
0320 
0321 static int fmvj18x_ioprobe(struct pcmcia_device *p_dev, void *priv_data)
0322 {
0323     return 0; /* strange, but that's what the code did already before... */
0324 }
0325 
0326 static int fmvj18x_config(struct pcmcia_device *link)
0327 {
0328     struct net_device *dev = link->priv;
0329     struct local_info *lp = netdev_priv(dev);
0330     int i, ret;
0331     unsigned int ioaddr;
0332     enum cardtype cardtype;
0333     char *card_name = "unknown";
0334     u8 *buf;
0335     size_t len;
0336     u_char buggybuf[32];
0337     u8 addr[ETH_ALEN];
0338 
0339     dev_dbg(&link->dev, "fmvj18x_config\n");
0340 
0341     link->io_lines = 5;
0342 
0343     len = pcmcia_get_tuple(link, CISTPL_FUNCE, &buf);
0344     kfree(buf);
0345 
0346     if (len) {
0347     /* Yes, I have CISTPL_FUNCE. Let's check CISTPL_MANFID */
0348     ret = pcmcia_loop_config(link, fmvj18x_ioprobe, NULL);
0349     if (ret != 0)
0350         goto failed;
0351 
0352     switch (link->manf_id) {
0353     case MANFID_TDK:
0354         cardtype = TDK;
0355         if (link->card_id == PRODID_TDK_GN3410 ||
0356         link->card_id == PRODID_TDK_NP9610 ||
0357         link->card_id == PRODID_TDK_MN3200) {
0358         /* MultiFunction Card */
0359         link->config_base = 0x800;
0360         link->config_index = 0x47;
0361         link->resource[1]->end = 8;
0362         }
0363         break;
0364     case MANFID_NEC:
0365         cardtype = NEC; /* MultiFunction Card */
0366         link->config_base = 0x800;
0367         link->config_index = 0x47;
0368         link->resource[1]->end = 8;
0369         break;
0370     case MANFID_KME:
0371         cardtype = KME; /* MultiFunction Card */
0372         link->config_base = 0x800;
0373         link->config_index = 0x47;
0374         link->resource[1]->end = 8;
0375         break;
0376     case MANFID_CONTEC:
0377         cardtype = CONTEC;
0378         break;
0379     case MANFID_FUJITSU:
0380         if (link->config_base == 0x0fe0)
0381         cardtype = MBH10302;
0382         else if (link->card_id == PRODID_FUJITSU_MBH10302) 
0383                 /* RATOC REX-5588/9822/4886's PRODID are 0004(=MBH10302),
0384                    but these are MBH10304 based card. */ 
0385         cardtype = MBH10304;
0386         else if (link->card_id == PRODID_FUJITSU_MBH10304)
0387         cardtype = MBH10304;
0388         else
0389         cardtype = LA501;
0390         break;
0391     default:
0392         cardtype = MBH10304;
0393     }
0394     } else {
0395     /* old type card */
0396     switch (link->manf_id) {
0397     case MANFID_FUJITSU:
0398         if (link->card_id == PRODID_FUJITSU_MBH10304) {
0399         cardtype = XXX10304;    /* MBH10304 with buggy CIS */
0400         link->config_index = 0x20;
0401         } else {
0402         cardtype = MBH10302;    /* NextCom NC5310, etc. */
0403         link->config_index = 1;
0404         }
0405         break;
0406     case MANFID_UNGERMANN:
0407         cardtype = UNGERMANN;
0408         break;
0409     default:
0410         cardtype = MBH10302;
0411         link->config_index = 1;
0412     }
0413     }
0414 
0415     if (link->resource[1]->end != 0) {
0416     ret = mfc_try_io_port(link);
0417     if (ret != 0) goto failed;
0418     } else if (cardtype == UNGERMANN) {
0419     ret = ungermann_try_io_port(link);
0420     if (ret != 0) goto failed;
0421     } else { 
0422         ret = pcmcia_request_io(link);
0423         if (ret)
0424             goto failed;
0425     }
0426     ret = pcmcia_request_irq(link, fjn_interrupt);
0427     if (ret)
0428         goto failed;
0429     ret = pcmcia_enable_device(link);
0430     if (ret)
0431         goto failed;
0432 
0433     dev->irq = link->irq;
0434     dev->base_addr = link->resource[0]->start;
0435 
0436     if (resource_size(link->resource[1]) != 0) {
0437     ret = fmvj18x_setup_mfc(link);
0438     if (ret != 0) goto failed;
0439     }
0440 
0441     ioaddr = dev->base_addr;
0442 
0443     /* Reset controller */
0444     if (sram_config == 0) 
0445     outb(CONFIG0_RST, ioaddr + CONFIG_0);
0446     else
0447     outb(CONFIG0_RST_1, ioaddr + CONFIG_0);
0448 
0449     /* Power On chip and select bank 0 */
0450     if (cardtype == MBH10302)
0451     outb(BANK_0, ioaddr + CONFIG_1);
0452     else
0453     outb(BANK_0U, ioaddr + CONFIG_1);
0454     
0455     /* Set hardware address */
0456     switch (cardtype) {
0457     case MBH10304:
0458     case TDK:
0459     case LA501:
0460     case CONTEC:
0461     case NEC:
0462     case KME:
0463     if (cardtype == MBH10304) {
0464         card_name = "FMV-J182";
0465 
0466         len = pcmcia_get_tuple(link, CISTPL_FUNCE, &buf);
0467         if (len < 11) {
0468             kfree(buf);
0469             goto failed;
0470         }
0471         /* Read MACID from CIS */
0472         eth_hw_addr_set(dev, &buf[5]);
0473         kfree(buf);
0474     } else {
0475         if (pcmcia_get_mac_from_cis(link, dev))
0476         goto failed;
0477         if( cardtype == TDK ) {
0478         card_name = "TDK LAK-CD021";
0479         } else if( cardtype == LA501 ) {
0480         card_name = "LA501";
0481         } else if( cardtype == NEC ) {
0482         card_name = "PK-UG-J001";
0483         } else if( cardtype == KME ) {
0484         card_name = "Panasonic";
0485         } else {
0486         card_name = "C-NET(PC)C";
0487         }
0488     }
0489     break;
0490     case UNGERMANN:
0491     /* Read MACID from register */
0492     for (i = 0; i < 6; i++) 
0493         addr[i] = inb(ioaddr + UNGERMANN_MAC_ID + i);
0494     eth_hw_addr_set(dev, addr);
0495     card_name = "Access/CARD";
0496     break;
0497     case XXX10304:
0498     /* Read MACID from Buggy CIS */
0499     if (fmvj18x_get_hwinfo(link, buggybuf) == -1) {
0500         pr_notice("unable to read hardware net address\n");
0501         goto failed;
0502     }
0503     eth_hw_addr_set(dev, buggybuf);
0504     card_name = "FMV-J182";
0505     break;
0506     case MBH10302:
0507     default:
0508     /* Read MACID from register */
0509     for (i = 0; i < 6; i++) 
0510         addr[i] = inb(ioaddr + MAC_ID + i);
0511     eth_hw_addr_set(dev, addr);
0512     card_name = "FMV-J181";
0513     break;
0514     }
0515 
0516     lp->cardtype = cardtype;
0517     SET_NETDEV_DEV(dev, &link->dev);
0518 
0519     if (register_netdev(dev) != 0) {
0520     pr_notice("register_netdev() failed\n");
0521     goto failed;
0522     }
0523 
0524     /* print current configuration */
0525     netdev_info(dev, "%s, sram %s, port %#3lx, irq %d, hw_addr %pM\n",
0526         card_name, sram_config == 0 ? "4K TX*2" : "8K TX*2",
0527         dev->base_addr, dev->irq, dev->dev_addr);
0528 
0529     return 0;
0530     
0531 failed:
0532     fmvj18x_release(link);
0533     return -ENODEV;
0534 } /* fmvj18x_config */
0535 /*====================================================================*/
0536 
0537 static int fmvj18x_get_hwinfo(struct pcmcia_device *link, u_char *node_id)
0538 {
0539     u_char __iomem *base;
0540     int i, j;
0541 
0542     /* Allocate a small memory window */
0543     link->resource[2]->flags |= WIN_DATA_WIDTH_8|WIN_MEMORY_TYPE_AM|WIN_ENABLE;
0544     link->resource[2]->start = 0; link->resource[2]->end = 0;
0545     i = pcmcia_request_window(link, link->resource[2], 0);
0546     if (i != 0)
0547     return -1;
0548 
0549     base = ioremap(link->resource[2]->start, resource_size(link->resource[2]));
0550     if (!base) {
0551     pcmcia_release_window(link, link->resource[2]);
0552     return -1;
0553     }
0554 
0555     pcmcia_map_mem_page(link, link->resource[2], 0);
0556 
0557     /*
0558      *  MBH10304 CISTPL_FUNCE_LAN_NODE_ID format
0559      *  22 0d xx xx xx 04 06 yy yy yy yy yy yy ff
0560      *  'xx' is garbage.
0561      *  'yy' is MAC address.
0562     */ 
0563     for (i = 0; i < 0x200; i++) {
0564     if (readb(base+i*2) == 0x22) {  
0565         if (readb(base+(i-1)*2) == 0xff &&
0566             readb(base+(i+5)*2) == 0x04 &&
0567             readb(base+(i+6)*2) == 0x06 &&
0568             readb(base+(i+13)*2) == 0xff)
0569             break;
0570     }
0571     }
0572 
0573     if (i != 0x200) {
0574     for (j = 0 ; j < 6; j++,i++) {
0575         node_id[j] = readb(base+(i+7)*2);
0576     }
0577     }
0578 
0579     iounmap(base);
0580     j = pcmcia_release_window(link, link->resource[2]);
0581     return (i != 0x200) ? 0 : -1;
0582 
0583 } /* fmvj18x_get_hwinfo */
0584 /*====================================================================*/
0585 
0586 static int fmvj18x_setup_mfc(struct pcmcia_device *link)
0587 {
0588     int i;
0589     struct net_device *dev = link->priv;
0590     unsigned int ioaddr;
0591     struct local_info *lp = netdev_priv(dev);
0592 
0593     /* Allocate a small memory window */
0594     link->resource[3]->flags = WIN_DATA_WIDTH_8|WIN_MEMORY_TYPE_AM|WIN_ENABLE;
0595     link->resource[3]->start = link->resource[3]->end = 0;
0596     i = pcmcia_request_window(link, link->resource[3], 0);
0597     if (i != 0)
0598     return -1;
0599 
0600     lp->base = ioremap(link->resource[3]->start,
0601                resource_size(link->resource[3]));
0602     if (lp->base == NULL) {
0603     netdev_notice(dev, "ioremap failed\n");
0604     return -1;
0605     }
0606 
0607     i = pcmcia_map_mem_page(link, link->resource[3], 0);
0608     if (i != 0) {
0609     iounmap(lp->base);
0610     lp->base = NULL;
0611     return -1;
0612     }
0613     
0614     ioaddr = dev->base_addr;
0615     writeb(0x47, lp->base+0x800);   /* Config Option Register of LAN */
0616     writeb(0x0,  lp->base+0x802);   /* Config and Status Register */
0617 
0618     writeb(ioaddr & 0xff, lp->base+0x80a);    /* I/O Base(Low) of LAN */
0619     writeb((ioaddr >> 8) & 0xff, lp->base+0x80c); /* I/O Base(High) of LAN */
0620    
0621     writeb(0x45, lp->base+0x820);   /* Config Option Register of Modem */
0622     writeb(0x8,  lp->base+0x822);   /* Config and Status Register */
0623 
0624     return 0;
0625 
0626 }
0627 /*====================================================================*/
0628 
0629 static void fmvj18x_release(struct pcmcia_device *link)
0630 {
0631 
0632     struct net_device *dev = link->priv;
0633     struct local_info *lp = netdev_priv(dev);
0634     u_char __iomem *tmp;
0635 
0636     dev_dbg(&link->dev, "fmvj18x_release\n");
0637 
0638     if (lp->base != NULL) {
0639     tmp = lp->base;
0640     lp->base = NULL;    /* set NULL before iounmap */
0641     iounmap(tmp);
0642     }
0643 
0644     pcmcia_disable_device(link);
0645 
0646 }
0647 
0648 static int fmvj18x_suspend(struct pcmcia_device *link)
0649 {
0650     struct net_device *dev = link->priv;
0651 
0652     if (link->open)
0653         netif_device_detach(dev);
0654 
0655     return 0;
0656 }
0657 
0658 static int fmvj18x_resume(struct pcmcia_device *link)
0659 {
0660     struct net_device *dev = link->priv;
0661 
0662     if (link->open) {
0663         fjn_reset(dev);
0664         netif_device_attach(dev);
0665     }
0666 
0667     return 0;
0668 }
0669 
0670 /*====================================================================*/
0671 
0672 static const struct pcmcia_device_id fmvj18x_ids[] = {
0673     PCMCIA_DEVICE_MANF_CARD(0x0004, 0x0004),
0674     PCMCIA_DEVICE_PROD_ID12("EAGLE Technology", "NE200 ETHERNET LAN MBH10302 04", 0x528c88c4, 0x74f91e59),
0675     PCMCIA_DEVICE_PROD_ID12("Eiger Labs,Inc", "EPX-10BT PC Card Ethernet 10BT", 0x53af556e, 0x877f9922),
0676     PCMCIA_DEVICE_PROD_ID12("Eiger labs,Inc.", "EPX-10BT PC Card Ethernet 10BT", 0xf47e6c66, 0x877f9922),
0677     PCMCIA_DEVICE_PROD_ID12("FUJITSU", "LAN Card(FMV-J182)", 0x6ee5a3d8, 0x5baf31db),
0678     PCMCIA_DEVICE_PROD_ID12("FUJITSU", "MBH10308", 0x6ee5a3d8, 0x3f04875e),
0679     PCMCIA_DEVICE_PROD_ID12("FUJITSU TOWA", "LA501", 0xb8451188, 0x12939ba2),
0680     PCMCIA_DEVICE_PROD_ID12("HITACHI", "HT-4840-11", 0xf4f43949, 0x773910f4),
0681     PCMCIA_DEVICE_PROD_ID12("NextComK.K.", "NC5310B Ver1.0       ", 0x8cef4d3a, 0x075fc7b6),
0682     PCMCIA_DEVICE_PROD_ID12("NextComK.K.", "NC5310 Ver1.0        ", 0x8cef4d3a, 0xbccf43e6),
0683     PCMCIA_DEVICE_PROD_ID12("RATOC System Inc.", "10BASE_T CARD R280", 0x85c10e17, 0xd9413666),
0684     PCMCIA_DEVICE_PROD_ID12("TDK", "LAC-CD02x", 0x1eae9475, 0x8fa0ee70),
0685     PCMCIA_DEVICE_PROD_ID12("TDK", "LAC-CF010", 0x1eae9475, 0x7683bc9a),
0686     PCMCIA_DEVICE_PROD_ID1("CONTEC Co.,Ltd.", 0x58d8fee2),
0687     PCMCIA_DEVICE_PROD_ID1("PCMCIA LAN MBH10304  ES", 0x2599f454),
0688     PCMCIA_DEVICE_PROD_ID1("PCMCIA MBH10302", 0x8f4005da),
0689     PCMCIA_DEVICE_PROD_ID1("UBKK,V2.0", 0x90888080),
0690     PCMCIA_PFC_DEVICE_PROD_ID12(0, "TDK", "GlobalNetworker 3410/3412", 0x1eae9475, 0xd9a93bed),
0691     PCMCIA_PFC_DEVICE_PROD_ID12(0, "NEC", "PK-UG-J001" ,0x18df0ba0 ,0x831b1064),
0692     PCMCIA_PFC_DEVICE_MANF_CARD(0, 0x0105, 0x0d0a),
0693     PCMCIA_PFC_DEVICE_MANF_CARD(0, 0x0105, 0x0e0a),
0694     PCMCIA_PFC_DEVICE_MANF_CARD(0, 0x0032, 0x0e01),
0695     PCMCIA_PFC_DEVICE_MANF_CARD(0, 0x0032, 0x0a05),
0696     PCMCIA_PFC_DEVICE_MANF_CARD(0, 0x0032, 0x0b05),
0697     PCMCIA_PFC_DEVICE_MANF_CARD(0, 0x0032, 0x1101),
0698     PCMCIA_DEVICE_NULL,
0699 };
0700 MODULE_DEVICE_TABLE(pcmcia, fmvj18x_ids);
0701 
0702 static struct pcmcia_driver fmvj18x_cs_driver = {
0703     .owner      = THIS_MODULE,
0704     .name       = "fmvj18x_cs",
0705     .probe      = fmvj18x_probe,
0706     .remove     = fmvj18x_detach,
0707     .id_table       = fmvj18x_ids,
0708     .suspend    = fmvj18x_suspend,
0709     .resume     = fmvj18x_resume,
0710 };
0711 module_pcmcia_driver(fmvj18x_cs_driver);
0712 
0713 /*====================================================================*/
0714 
0715 static irqreturn_t fjn_interrupt(int dummy, void *dev_id)
0716 {
0717     struct net_device *dev = dev_id;
0718     struct local_info *lp = netdev_priv(dev);
0719     unsigned int ioaddr;
0720     unsigned short tx_stat, rx_stat;
0721 
0722     ioaddr = dev->base_addr;
0723 
0724     /* avoid multiple interrupts */
0725     outw(0x0000, ioaddr + TX_INTR);
0726 
0727     /* wait for a while */
0728     udelay(1);
0729 
0730     /* get status */
0731     tx_stat = inb(ioaddr + TX_STATUS);
0732     rx_stat = inb(ioaddr + RX_STATUS);
0733 
0734     /* clear status */
0735     outb(tx_stat, ioaddr + TX_STATUS);
0736     outb(rx_stat, ioaddr + RX_STATUS);
0737     
0738     pr_debug("%s: interrupt, rx_status %02x.\n", dev->name, rx_stat);
0739     pr_debug("               tx_status %02x.\n", tx_stat);
0740     
0741     if (rx_stat || (inb(ioaddr + RX_MODE) & F_BUF_EMP) == 0) {
0742     /* there is packet(s) in rx buffer */
0743     fjn_rx(dev);
0744     }
0745     if (tx_stat & F_TMT_RDY) {
0746     dev->stats.tx_packets += lp->sent ;
0747         lp->sent = 0 ;
0748     if (lp->tx_queue) {
0749         outb(DO_TX | lp->tx_queue, ioaddr + TX_START);
0750         lp->sent = lp->tx_queue ;
0751         lp->tx_queue = 0;
0752         lp->tx_queue_len = 0;
0753         netif_trans_update(dev);
0754     } else {
0755         lp->tx_started = 0;
0756     }
0757     netif_wake_queue(dev);
0758     }
0759     pr_debug("%s: exiting interrupt,\n", dev->name);
0760     pr_debug("    tx_status %02x, rx_status %02x.\n", tx_stat, rx_stat);
0761 
0762     outb(D_TX_INTR, ioaddr + TX_INTR);
0763     outb(D_RX_INTR, ioaddr + RX_INTR);
0764 
0765     if (lp->base != NULL) {
0766     /* Ack interrupt for multifunction card */
0767     writeb(0x01, lp->base+0x802);
0768     writeb(0x09, lp->base+0x822);
0769     }
0770 
0771     return IRQ_HANDLED;
0772 
0773 } /* fjn_interrupt */
0774 
0775 /*====================================================================*/
0776 
0777 static void fjn_tx_timeout(struct net_device *dev, unsigned int txqueue)
0778 {
0779     struct local_info *lp = netdev_priv(dev);
0780     unsigned int ioaddr = dev->base_addr;
0781 
0782     netdev_notice(dev, "transmit timed out with status %04x, %s?\n",
0783           htons(inw(ioaddr + TX_STATUS)),
0784           inb(ioaddr + TX_STATUS) & F_TMT_RDY
0785           ? "IRQ conflict" : "network cable problem");
0786     netdev_notice(dev, "timeout registers: %04x %04x %04x "
0787           "%04x %04x %04x %04x %04x.\n",
0788           htons(inw(ioaddr + 0)), htons(inw(ioaddr + 2)),
0789           htons(inw(ioaddr + 4)), htons(inw(ioaddr + 6)),
0790           htons(inw(ioaddr + 8)), htons(inw(ioaddr + 10)),
0791           htons(inw(ioaddr + 12)), htons(inw(ioaddr + 14)));
0792     dev->stats.tx_errors++;
0793     /* ToDo: We should try to restart the adaptor... */
0794     local_irq_disable();
0795     fjn_reset(dev);
0796 
0797     lp->tx_started = 0;
0798     lp->tx_queue = 0;
0799     lp->tx_queue_len = 0;
0800     lp->sent = 0;
0801     lp->open_time = jiffies;
0802     local_irq_enable();
0803     netif_wake_queue(dev);
0804 }
0805 
0806 static netdev_tx_t fjn_start_xmit(struct sk_buff *skb,
0807                     struct net_device *dev)
0808 {
0809     struct local_info *lp = netdev_priv(dev);
0810     unsigned int ioaddr = dev->base_addr;
0811     short length = skb->len;
0812     
0813     if (length < ETH_ZLEN)
0814     {
0815     if (skb_padto(skb, ETH_ZLEN))
0816         return NETDEV_TX_OK;
0817     length = ETH_ZLEN;
0818     }
0819 
0820     netif_stop_queue(dev);
0821 
0822     {
0823     unsigned char *buf = skb->data;
0824 
0825     if (length > ETH_FRAME_LEN) {
0826         netdev_notice(dev, "Attempting to send a large packet (%d bytes)\n",
0827               length);
0828         return NETDEV_TX_BUSY;
0829     }
0830 
0831     netdev_dbg(dev, "Transmitting a packet of length %lu\n",
0832            (unsigned long)skb->len);
0833     dev->stats.tx_bytes += skb->len;
0834 
0835     /* Disable both interrupts. */
0836     outw(0x0000, ioaddr + TX_INTR);
0837 
0838     /* wait for a while */
0839     udelay(1);
0840 
0841     outw(length, ioaddr + DATAPORT);
0842     outsw(ioaddr + DATAPORT, buf, (length + 1) >> 1);
0843 
0844     lp->tx_queue++;
0845     lp->tx_queue_len += ((length+3) & ~1);
0846 
0847     if (lp->tx_started == 0) {
0848         /* If the Tx is idle, always trigger a transmit. */
0849         outb(DO_TX | lp->tx_queue, ioaddr + TX_START);
0850         lp->sent = lp->tx_queue ;
0851         lp->tx_queue = 0;
0852         lp->tx_queue_len = 0;
0853         lp->tx_started = 1;
0854         netif_start_queue(dev);
0855     } else {
0856         if( sram_config == 0 ) {
0857         if (lp->tx_queue_len < (4096 - (ETH_FRAME_LEN +2)) )
0858             /* Yes, there is room for one more packet. */
0859             netif_start_queue(dev);
0860         } else {
0861         if (lp->tx_queue_len < (8192 - (ETH_FRAME_LEN +2)) && 
0862                         lp->tx_queue < 127 )
0863             /* Yes, there is room for one more packet. */
0864             netif_start_queue(dev);
0865         }
0866     }
0867 
0868     /* Re-enable interrupts */
0869     outb(D_TX_INTR, ioaddr + TX_INTR);
0870     outb(D_RX_INTR, ioaddr + RX_INTR);
0871     }
0872     dev_kfree_skb (skb);
0873 
0874     return NETDEV_TX_OK;
0875 } /* fjn_start_xmit */
0876 
0877 /*====================================================================*/
0878 
0879 static void fjn_reset(struct net_device *dev)
0880 {
0881     struct local_info *lp = netdev_priv(dev);
0882     unsigned int ioaddr = dev->base_addr;
0883     int i;
0884 
0885     netdev_dbg(dev, "fjn_reset() called\n");
0886 
0887     /* Reset controller */
0888     if( sram_config == 0 ) 
0889     outb(CONFIG0_RST, ioaddr + CONFIG_0);
0890     else
0891     outb(CONFIG0_RST_1, ioaddr + CONFIG_0);
0892 
0893     /* Power On chip and select bank 0 */
0894     if (lp->cardtype == MBH10302)
0895     outb(BANK_0, ioaddr + CONFIG_1);
0896     else
0897     outb(BANK_0U, ioaddr + CONFIG_1);
0898 
0899     /* Set Tx modes */
0900     outb(D_TX_MODE, ioaddr + TX_MODE);
0901     /* set Rx modes */
0902     outb(ID_MATCHED, ioaddr + RX_MODE);
0903 
0904     /* Set hardware address */
0905     for (i = 0; i < 6; i++) 
0906         outb(dev->dev_addr[i], ioaddr + NODE_ID + i);
0907 
0908     /* (re)initialize the multicast table */
0909     set_rx_mode(dev);
0910 
0911     /* Switch to bank 2 (runtime mode) */
0912     if (lp->cardtype == MBH10302)
0913     outb(BANK_2, ioaddr + CONFIG_1);
0914     else
0915     outb(BANK_2U, ioaddr + CONFIG_1);
0916 
0917     /* set 16col ctrl bits */
0918     if( lp->cardtype == TDK || lp->cardtype == CONTEC) 
0919         outb(TDK_AUTO_MODE, ioaddr + COL_CTRL);
0920     else
0921         outb(AUTO_MODE, ioaddr + COL_CTRL);
0922 
0923     /* clear Reserved Regs */
0924     outb(0x00, ioaddr + BMPR12);
0925     outb(0x00, ioaddr + BMPR13);
0926 
0927     /* reset Skip packet reg. */
0928     outb(0x01, ioaddr + RX_SKIP);
0929 
0930     /* Enable Tx and Rx */
0931     if( sram_config == 0 )
0932     outb(CONFIG0_DFL, ioaddr + CONFIG_0);
0933     else
0934     outb(CONFIG0_DFL_1, ioaddr + CONFIG_0);
0935 
0936     /* Init receive pointer ? */
0937     inw(ioaddr + DATAPORT);
0938     inw(ioaddr + DATAPORT);
0939 
0940     /* Clear all status */
0941     outb(0xff, ioaddr + TX_STATUS);
0942     outb(0xff, ioaddr + RX_STATUS);
0943 
0944     if (lp->cardtype == MBH10302)
0945     outb(INTR_OFF, ioaddr + LAN_CTRL);
0946 
0947     /* Turn on Rx interrupts */
0948     outb(D_TX_INTR, ioaddr + TX_INTR);
0949     outb(D_RX_INTR, ioaddr + RX_INTR);
0950 
0951     /* Turn on interrupts from LAN card controller */
0952     if (lp->cardtype == MBH10302)
0953     outb(INTR_ON, ioaddr + LAN_CTRL);
0954 } /* fjn_reset */
0955 
0956 /*====================================================================*/
0957 
0958 static void fjn_rx(struct net_device *dev)
0959 {
0960     unsigned int ioaddr = dev->base_addr;
0961     int boguscount = 10;    /* 5 -> 10: by agy 19940922 */
0962 
0963     pr_debug("%s: in rx_packet(), rx_status %02x.\n",
0964       dev->name, inb(ioaddr + RX_STATUS));
0965 
0966     while ((inb(ioaddr + RX_MODE) & F_BUF_EMP) == 0) {
0967     u_short status = inw(ioaddr + DATAPORT);
0968 
0969     netdev_dbg(dev, "Rxing packet mode %02x status %04x.\n",
0970            inb(ioaddr + RX_MODE), status);
0971 #ifndef final_version
0972     if (status == 0) {
0973         outb(F_SKP_PKT, ioaddr + RX_SKIP);
0974         break;
0975     }
0976 #endif
0977     if ((status & 0xF0) != 0x20) {  /* There was an error. */
0978         dev->stats.rx_errors++;
0979         if (status & F_LEN_ERR) dev->stats.rx_length_errors++;
0980         if (status & F_ALG_ERR) dev->stats.rx_frame_errors++;
0981         if (status & F_CRC_ERR) dev->stats.rx_crc_errors++;
0982         if (status & F_OVR_FLO) dev->stats.rx_over_errors++;
0983     } else {
0984         u_short pkt_len = inw(ioaddr + DATAPORT);
0985         /* Malloc up new buffer. */
0986         struct sk_buff *skb;
0987 
0988         if (pkt_len > 1550) {
0989         netdev_notice(dev, "The FMV-18x claimed a very large packet, size %d\n",
0990                   pkt_len);
0991         outb(F_SKP_PKT, ioaddr + RX_SKIP);
0992         dev->stats.rx_errors++;
0993         break;
0994         }
0995         skb = netdev_alloc_skb(dev, pkt_len + 2);
0996         if (skb == NULL) {
0997         outb(F_SKP_PKT, ioaddr + RX_SKIP);
0998         dev->stats.rx_dropped++;
0999         break;
1000         }
1001 
1002         skb_reserve(skb, 2);
1003         insw(ioaddr + DATAPORT, skb_put(skb, pkt_len),
1004          (pkt_len + 1) >> 1);
1005         skb->protocol = eth_type_trans(skb, dev);
1006 
1007         {
1008         int i;
1009         pr_debug("%s: Rxed packet of length %d: ",
1010             dev->name, pkt_len);
1011         for (i = 0; i < 14; i++)
1012             pr_debug(" %02x", skb->data[i]);
1013         pr_debug(".\n");
1014         }
1015 
1016         netif_rx(skb);
1017         dev->stats.rx_packets++;
1018         dev->stats.rx_bytes += pkt_len;
1019     }
1020     if (--boguscount <= 0)
1021         break;
1022     }
1023 
1024     /* If any worth-while packets have been received, dev_rint()
1025        has done a netif_wake_queue() for us and will work on them
1026        when we get to the bottom-half routine. */
1027 /*
1028     if (lp->cardtype != TDK) {
1029     int i;
1030     for (i = 0; i < 20; i++) {
1031         if ((inb(ioaddr + RX_MODE) & F_BUF_EMP) == F_BUF_EMP)
1032         break;
1033         (void)inw(ioaddr + DATAPORT);  /+ dummy status read +/
1034         outb(F_SKP_PKT, ioaddr + RX_SKIP);
1035     }
1036 
1037     if (i > 0)
1038         pr_debug("%s: Exint Rx packet with mode %02x after "
1039           "%d ticks.\n", dev->name, inb(ioaddr + RX_MODE), i);
1040     }
1041 */
1042 } /* fjn_rx */
1043 
1044 /*====================================================================*/
1045 
1046 static void netdev_get_drvinfo(struct net_device *dev,
1047                    struct ethtool_drvinfo *info)
1048 {
1049     strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1050     strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1051     snprintf(info->bus_info, sizeof(info->bus_info),
1052         "PCMCIA 0x%lx", dev->base_addr);
1053 }
1054 
1055 static const struct ethtool_ops netdev_ethtool_ops = {
1056     .get_drvinfo        = netdev_get_drvinfo,
1057 };
1058 
1059 static int fjn_config(struct net_device *dev, struct ifmap *map){
1060     return 0;
1061 }
1062 
1063 static int fjn_open(struct net_device *dev)
1064 {
1065     struct local_info *lp = netdev_priv(dev);
1066     struct pcmcia_device *link = lp->p_dev;
1067 
1068     pr_debug("fjn_open('%s').\n", dev->name);
1069 
1070     if (!pcmcia_dev_present(link))
1071     return -ENODEV;
1072     
1073     link->open++;
1074     
1075     fjn_reset(dev);
1076     
1077     lp->tx_started = 0;
1078     lp->tx_queue = 0;
1079     lp->tx_queue_len = 0;
1080     lp->open_time = jiffies;
1081     netif_start_queue(dev);
1082     
1083     return 0;
1084 } /* fjn_open */
1085 
1086 /*====================================================================*/
1087 
1088 static int fjn_close(struct net_device *dev)
1089 {
1090     struct local_info *lp = netdev_priv(dev);
1091     struct pcmcia_device *link = lp->p_dev;
1092     unsigned int ioaddr = dev->base_addr;
1093 
1094     pr_debug("fjn_close('%s').\n", dev->name);
1095 
1096     lp->open_time = 0;
1097     netif_stop_queue(dev);
1098 
1099     /* Set configuration register 0 to disable Tx and Rx. */
1100     if( sram_config == 0 ) 
1101     outb(CONFIG0_RST ,ioaddr + CONFIG_0);
1102     else
1103     outb(CONFIG0_RST_1 ,ioaddr + CONFIG_0);
1104 
1105     /* Update the statistics -- ToDo. */
1106 
1107     /* Power-down the chip.  Green, green, green! */
1108     outb(CHIP_OFF ,ioaddr + CONFIG_1);
1109 
1110     /* Set the ethernet adaptor disable IRQ */
1111     if (lp->cardtype == MBH10302)
1112     outb(INTR_OFF, ioaddr + LAN_CTRL);
1113 
1114     link->open--;
1115 
1116     return 0;
1117 } /* fjn_close */
1118 
1119 /*====================================================================*/
1120 
1121 /*
1122   Set the multicast/promiscuous mode for this adaptor.
1123 */
1124 
1125 static void set_rx_mode(struct net_device *dev)
1126 {
1127     unsigned int ioaddr = dev->base_addr;
1128     u_char mc_filter[8];         /* Multicast hash filter */
1129     u_long flags;
1130     int i;
1131     
1132     int saved_bank;
1133     int saved_config_0 = inb(ioaddr + CONFIG_0);
1134      
1135     local_irq_save(flags); 
1136 
1137     /* Disable Tx and Rx */
1138     if (sram_config == 0) 
1139     outb(CONFIG0_RST, ioaddr + CONFIG_0);
1140     else
1141     outb(CONFIG0_RST_1, ioaddr + CONFIG_0);
1142 
1143     if (dev->flags & IFF_PROMISC) {
1144     memset(mc_filter, 0xff, sizeof(mc_filter));
1145     outb(3, ioaddr + RX_MODE);  /* Enable promiscuous mode */
1146     } else if (netdev_mc_count(dev) > MC_FILTERBREAK ||
1147            (dev->flags & IFF_ALLMULTI)) {
1148     /* Too many to filter perfectly -- accept all multicasts. */
1149     memset(mc_filter, 0xff, sizeof(mc_filter));
1150     outb(2, ioaddr + RX_MODE);  /* Use normal mode. */
1151     } else if (netdev_mc_empty(dev)) {
1152     memset(mc_filter, 0x00, sizeof(mc_filter));
1153     outb(1, ioaddr + RX_MODE);  /* Ignore almost all multicasts. */
1154     } else {
1155     struct netdev_hw_addr *ha;
1156 
1157     memset(mc_filter, 0, sizeof(mc_filter));
1158     netdev_for_each_mc_addr(ha, dev) {
1159         unsigned int bit = ether_crc_le(ETH_ALEN, ha->addr) >> 26;
1160         mc_filter[bit >> 3] |= (1 << (bit & 7));
1161     }
1162     outb(2, ioaddr + RX_MODE);  /* Use normal mode. */
1163     }
1164 
1165     /* Switch to bank 1 and set the multicast table. */
1166     saved_bank = inb(ioaddr + CONFIG_1);
1167     outb(0xe4, ioaddr + CONFIG_1);
1168 
1169     for (i = 0; i < 8; i++)
1170     outb(mc_filter[i], ioaddr + MAR_ADR + i);
1171     outb(saved_bank, ioaddr + CONFIG_1);
1172 
1173     outb(saved_config_0, ioaddr + CONFIG_0);
1174 
1175     local_irq_restore(flags);
1176 }