/*======================================================================

    A PCMCIA ethernet driver for NS8390-based cards

    This driver supports the D-Link DE-650 and Linksys EthernetCard
    cards, the newer D-Link and Linksys combo cards, Accton EN2212
    cards, the RPTI EP400, and the PreMax PE-200 in non-shared-memory
    mode, and the IBM Credit Card Adapter, the NE4100, the Thomas
    Conrad ethernet card, and the Kingston KNE-PCM/x in shared-memory
    mode.  It will also handle the Socket EA card in either mode.

    Written by David Hinds, dhinds@allegro.stanford.edu

    The network driver code is based on Donald Becker's NE2000 code:

    Written 1992,1993 by Donald Becker.
    Copyright 1993 United States Government as represented by the
    Director, National Security Agency.  This software may be used and
    distributed according to the terms of the GNU Public License,
    incorporated herein by reference.
    Donald Becker may be reached at becker@cesdis1.gsfc.nasa.gov

    Based also on Keith Moore's changes to Don Becker's code, for IBM
    CCAE support.  Drivers merged back together, and shared-memory
    Socket EA support added, by Ken Raeburn, September 1995.

======================================================================*/

#include <pcmcia/config.h>
#include <pcmcia/k_compat.h>

#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/ptrace.h>
#include <linux/malloc.h>
#include <linux/string.h>
#include <linux/timer.h>
#include <linux/delay.h>
#include <asm/io.h>
#include <asm/system.h>

#include <linux/netdevice.h>
#include <drivers/net/8390.h>

#include <pcmcia/version.h>
#include <pcmcia/cs_types.h>
#include <pcmcia/cs.h>
#include <pcmcia/cistpl.h>
#include <pcmcia/ciscode.h>
#include <pcmcia/ds.h>
#include <pcmcia/cisreg.h>

#define PCNET_CMD	0x00
#define PCNET_DATAPORT	0x10	/* NatSemi-defined port window offset. */
#define PCNET_RESET	0x1f	/* Issue a read to reset, a write to clear. */
#define PCNET_MISC	0x18	/* For IBM CCAE and Socket EA cards */

#define PCNET_START_PG	0x40	/* First page of TX buffer */
#define PCNET_STOP_PG	0x80	/* Last page +1 of RX ring */

/* Socket EA cards have a larger packet buffer */
#define SOCKET_START_PG	0x01
#define SOCKET_STOP_PG	0xff

#define PCNET_RDC_TIMEOUT 0x02	/* Max wait in jiffies for Tx RDC */

static char *if_names[] = { "Auto", "10baseT", "10base2"};

#ifdef PCMCIA_DEBUG
static int pc_debug = PCMCIA_DEBUG;
MODULE_PARM(pc_debug, "i");
#define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
static char *version =
"pcnet_cs.c 1.52 1997/06/01 05:03:16 (David Hinds)";
#else
#define DEBUG(n, args...)
#endif

/*====================================================================*/

/* Parameters that can be set with 'insmod' */

/* Bit map of interrupts to choose from */
static u_long irq_mask = 0xdeb8;

/* Transceiver type, for Socket EA and IBM CC cards. */
static int if_port = 1;

/* Use 64K packet buffer, for Socket EA cards. */
static int use_big_buf = 1;

/* Shared memory speed, in ns */
static int mem_speed = 0;

/* Insert a pause in block_output after sending a packet */
static int delay_output = 0;

/* Length of delay, in microseconds */
static int delay_time = 4;

/* Use shared memory or polled IO?  Default is to use hw_info */
static int use_shmem = -1;

/* Ugh!  Let the user hardwire the hardware address for queer cards */
static int hw_addr[6] = { 0, /* ... */ };

MODULE_PARM(irq_mask, "i");
MODULE_PARM(if_port, "i");
MODULE_PARM(use_big_buf, "i");
MODULE_PARM(mem_speed, "i");
MODULE_PARM(delay_output, "i");
MODULE_PARM(delay_time, "i");
MODULE_PARM(use_shmem, "i");
MODULE_PARM(hw_addr, "6i");

/*====================================================================*/

static void pcnet_config(dev_link_t *link);
static void pcnet_release(u_long arg);
static int pcnet_event(event_t event, int priority,
		       event_callback_args_t *args);

static int pcnet_open(struct device *dev);
static int pcnet_close(struct device *dev);

static void pcnet_reset_8390(struct device *dev);

static int set_config(struct device *dev, struct ifmap *map);

static int setup_shmem_window(dev_link_t *link, int start_pg,
			      int stop_pg, int cm_offset);
static int setup_dma_config(dev_link_t *link, int start_pg,
			    int stop_pg);

static dev_info_t dev_info = "pcnet_cs";

static dev_link_t *pcnet_attach(void);
static void pcnet_detach(dev_link_t *);

static dev_link_t *dev_list;

/*====================================================================*/

typedef struct hw_info_t {
    char	*id;
    u_long	offset;
    u_char	a0, a1, a2;
    u_long	flags;
} hw_info_t;

#define DELAY_OUTPUT	0x01
#define HAS_MISC_REG	0x02
#define USE_SHMEM	0x04
#define USE_BIG_BUF	0x08
#define HAS_IBM_MISC	0x10
#define IS_MULTIFN	0x20

static hw_info_t hw_info[] = {
    { "Accton EN2212", 0x0ff0, 0x00, 0x00, 0xe8, DELAY_OUTPUT },
    { "Allied Telesis LA-PCM", 0x0ff0, 0x00, 0x00, 0xf4, USE_SHMEM },
    { "APEX MultiCard", 0x03f4, 0x00, 0x20, 0xe5, 0 },
    { "ASANTE FriendlyNet", 0x4910, 0x00, 0x00, 0x94,
      DELAY_OUTPUT | HAS_IBM_MISC },
    { "Danpex EN-6200P2", 0x0110, 0x00, 0x40, 0xc7, 0 },
    { "DataTrek NetCard", 0x0ff0, 0x00, 0x20, 0xe8, 0 },
    { "Dayna CommuniCard E", 0x0110, 0x00, 0x80, 0x19, 0 },
    { "D-Link DE-650", 0x0040, 0x00, 0x80, 0xc8, 0 },
    { "EP-210 Ethernet", 0x0110, 0x00, 0x40, 0x33, 0 },
    { "EP4000 Ethernet", 0x01c0, 0x00, 0x00, 0xb4, 0 },
    { "Epson EEN10B", 0x0ff0, 0x00, 0x00, 0x48,
      USE_SHMEM | HAS_MISC_REG | HAS_IBM_MISC },
    { "ELECOM Laneed LD-CDWA", 0xb8, 0x08, 0x00, 0x42, 0 },
    { "Grey Cell GCS2220", 0x0000, 0x00, 0x47, 0x43, 0 },
    { "Hypertec Ethernet", 0x01c0, 0x00, 0x40, 0x4c, 0 },
    { "IBM CCAE", 0x0ff0, 0x08, 0x00, 0x5a,
      USE_SHMEM | HAS_MISC_REG | HAS_IBM_MISC },
    { "IBM CCAE", 0x0ff0, 0x00, 0x04, 0xac,
      USE_SHMEM | HAS_MISC_REG | HAS_IBM_MISC },
    { "IBM CCAE", 0x0ff0, 0x00, 0x06, 0x29,
      USE_SHMEM | HAS_MISC_REG | HAS_IBM_MISC },
    { "IBM FME", 0x0374, 0x08, 0x00, 0x5a,
      USE_SHMEM | HAS_MISC_REG | HAS_IBM_MISC },
    { "IBM FME", 0x0374, 0x00, 0x04, 0xac,
      USE_SHMEM | HAS_MISC_REG | HAS_IBM_MISC },
    { "I-O DATA PCLA/T", 0x0ff0, 0x00, 0xa0, 0xb0, 0 },
    { "Katron PE-520", 0x0110, 0x00, 0x40, 0xf6, 0 },
    { "Kingston KNE-PCM/x", 0x0ff0, 0x00, 0xc0, 0xf0,
      USE_SHMEM | HAS_MISC_REG | HAS_IBM_MISC },
    { "Kingston KNE-PCM/x", 0x0ff0, 0xe2, 0x0c, 0x0f,
      USE_SHMEM | HAS_MISC_REG | HAS_IBM_MISC },
    { "Kingston KNE-PC2", 0x0180, 0x00, 0xc0, 0xf0, 0 },
    { "Longshine LCS-8534", 0, 0x08, 0x00, 0x00, 0 },
    { "Maxtech PCN2000", 0x5000, 0x00, 0x00, 0xe8, 0 },
    { "NDC Instant-Link", 0x003a, 0x00, 0x80, 0xc6, 0 },
    { "NE2000 Compatible", 0x0ff0, 0x00, 0xa0, 0x0c, 0 },
    { "Network General Sniffer", 0x0ff0, 0x00, 0x00, 0x65,
      USE_SHMEM | HAS_MISC_REG | HAS_IBM_MISC },
    { "Panasonic VEL211", 0x0ff0, 0x00, 0x80, 0x45, 
      USE_SHMEM | HAS_MISC_REG | HAS_IBM_MISC },
    { "PreMax PE-200", 0x07f0, 0x00, 0x20, 0xe0, 0 },
    { "RPTI EP400", 0x0110, 0x00, 0x40, 0x95, 0 },
    { "SCM Ethernet", 0x0ff0, 0x00, 0x20, 0xcb, 0 },
    { "Socket EA", 0x4000, 0x00, 0xc0, 0x1b,
      DELAY_OUTPUT | HAS_MISC_REG | USE_BIG_BUF },
    { "Volktek NPL-402CT", 0x0060, 0x00, 0x40, 0x05, 0 }
};

#define NR_INFO		(sizeof(hw_info)/sizeof(hw_info_t))

static hw_info_t default_info =
{ "Unknown NE2000 Clone", 0x00, 0x00, 0x00, 0 };

typedef struct pcnet_dev_t {
    struct device	dev;
    dev_node_t		node;
    u_long		flags;
} pcnet_dev_t;

/*====================================================================*/

static void cs_error(int func, int ret)
{
    CardServices(ReportError, dev_info, (void *)func, (void *)ret);
}

/*======================================================================

    We never need to do anything when a pcnet device is "initialized"
    by the net software, because we only register already-found cards.

======================================================================*/

static int pcnet_init(struct device *dev)
{
    return 0;
}

/*======================================================================

    pcnet_attach() creates an "instance" of the driver, allocating
    local data structures for one device.  The device is registered
    with Card Services.

======================================================================*/

static dev_link_t *pcnet_attach(void)
{
    client_reg_t client_reg;
    dev_link_t *link;
    pcnet_dev_t *info;
    struct device *dev;
    int ret;

    DEBUG(0, "pcnet_attach()\n");

    /* Create new ethernet device */
    link = kmalloc(sizeof(struct dev_link_t), GFP_KERNEL);
    memset(link, 0, sizeof(struct dev_link_t));
    link->release.function = &pcnet_release;
    link->release.data = (u_long)link;
    link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
    link->io.NumPorts1 = link->io.NumPorts2 = 16;
    link->io.Attributes2 = IO_DATA_PATH_WIDTH_16;
    link->io.IOAddrLines = 5;
    link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
    link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
    link->irq.IRQInfo2 = irq_mask;
    link->conf.Attributes = CONF_ENABLE_IRQ;
    link->conf.Vcc = 50;
    link->conf.IntType = INT_MEMORY_AND_IO;

    info = kmalloc(sizeof(struct pcnet_dev_t), GFP_KERNEL);
    memset(info, 0, sizeof(struct pcnet_dev_t));
    dev = &info->dev;
    ethdev_init(dev);
    dev->name = info->node.dev_name;
    dev->init = &pcnet_init;
    dev->open = &pcnet_open;
    dev->stop = &pcnet_close;
    dev->set_config = &set_config;
    dev->tbusy = 1;
    link->priv = info;

    /* Register with Card Services */
    link->next = dev_list;
    dev_list = link;
    client_reg.dev_info = &dev_info;
    client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
    client_reg.EventMask =
	CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
	CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
	CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
    client_reg.event_handler = &pcnet_event;
    client_reg.Version = 0x0210;
    client_reg.event_callback_args.client_data = link;
    ret = CardServices(RegisterClient, &link->handle, &client_reg);
    if (ret != 0) {
	cs_error(RegisterClient, ret);
	pcnet_detach(link);
	return NULL;
    }

    return link;
} /* pcnet_attach */

/*======================================================================

    This deletes a driver "instance".  The device is de-registered
    with Card Services.  If it has been released, all local data
    structures are freed.  Otherwise, the structures will be freed
    when the device is released.

======================================================================*/

static void pcnet_detach(dev_link_t *link)
{
    dev_link_t **linkp;
    long flags;

    DEBUG(0, "pcnet_detach(0x%p)\n", link);

    /* Locate device structure */
    for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
	if (*linkp == link) break;
    if (*linkp == NULL)
	return;

    save_flags(flags);
    cli();
    if (link->state & DEV_RELEASE_PENDING) {
	del_timer(&link->release);
	link->state &= ~DEV_RELEASE_PENDING;
    }
    restore_flags(flags);

    if (link->state & DEV_CONFIG) {
	pcnet_release((u_long)link);
	if (link->state & DEV_STALE_CONFIG) {
	    link->state |= DEV_STALE_LINK;
	    return;
	}
    }

    if (link->handle)
	CardServices(DeregisterClient, link->handle);

    /* Unlink device structure, free bits */
    *linkp = link->next;
    if (link->priv) {
	struct device *dev = link->priv;
	if (dev->priv)
	    kfree_s(dev->priv, sizeof(struct ei_device));
	kfree_s(dev, sizeof(struct pcnet_dev_t));
    }
    kfree_s(link, sizeof(struct dev_link_t));

} /* pcnet_detach */

/*======================================================================

    This probes for a card's hardware address, for card types that
    encode this information in their CIS.

======================================================================*/

static hw_info_t *get_hwinfo(dev_link_t *link)
{
    struct device *dev = link->priv;
    win_req_t req;
    memreq_t mem;
    u_char *base;
    int i, j;

    /* Allocate a 4K memory window */
    req.Attributes = WIN_DATA_WIDTH_8|WIN_MEMORY_TYPE_AM|WIN_ENABLE;
    req.Base = NULL;
    req.Size = 0x1000;
    req.AccessSpeed = 0;
    link->win = (window_handle_t)link->handle;
    i = CardServices(RequestWindow, &link->win, &req);
    if (i != 0) {
	cs_error(RequestWindow, i);
	return NULL;
    }

    mem.Page = 0;
    for (i = 0; i < NR_INFO; i++) {
	mem.CardOffset = hw_info[i].offset & ~0x0fff;
	CardServices(MapMemPage, link->win, &mem);
	base = &req.Base[hw_info[i].offset & 0x0fff];
	if ((readb(base+0) == hw_info[i].a0) &&
	    (readb(base+2) == hw_info[i].a1) &&
	    (readb(base+4) == hw_info[i].a2))
	    break;
    }
    if (i < NR_INFO) {
	for (j = 0; j < 6; j++)
	    dev->dev_addr[j] = readb(base + (j<<1));
	j = CardServices(ReleaseWindow, link->win);
	if (j != 0)
	    cs_error(ReleaseWindow, j);
	return hw_info+i;
    }
    else {
	i = CardServices(ReleaseWindow, link->win);
	if (i != 0)
	    cs_error(ReleaseWindow, i);
	return NULL;
    }
} /* get_hwinfo */

/*======================================================================

    This probes for a card's hardware address by reading the PROM.
    It checks the address against a list of known types, then falls
    back to a simple NE2000 clone signature check.

======================================================================*/

static hw_info_t *get_prom(dev_link_t *link)
{
    struct device *dev = link->priv;
    unsigned char prom[32];
    int i, j, ioaddr;

    /* This is lifted straight from drivers/net/ne.c */
    struct {
	unsigned char value, offset;
    } program_seq[] = {
	{E8390_NODMA+E8390_PAGE0+E8390_STOP, E8390_CMD}, /* Select page 0*/
	{0x48,	EN0_DCFG},	/* Set byte-wide (0x48) access. */
	{0x00,	EN0_RCNTLO},	/* Clear the count regs. */
	{0x00,	EN0_RCNTHI},
	{0x00,	EN0_IMR},	/* Mask completion irq. */
	{0xFF,	EN0_ISR},
	{E8390_RXOFF, EN0_RXCR},	/* 0x20  Set to monitor */
	{E8390_TXOFF, EN0_TXCR},	/* 0x02  and loopback mode. */
	{32,	EN0_RCNTLO},
	{0x00,	EN0_RCNTHI},
	{0x00,	EN0_RSARLO},	/* DMA starting at 0x0000. */
	{0x00,	EN0_RSARHI},
	{E8390_RREAD+E8390_START, E8390_CMD},
    };

    ioaddr = dev->base_addr;

    pcnet_reset_8390(dev);
    udelay(10000);

    for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++)
	outb_p(program_seq[i].value, ioaddr + program_seq[i].offset);

    for (i = 0; i < 32; i++)
	prom[i] = inb(ioaddr + PCNET_DATAPORT);
    for (i = 0; i < NR_INFO; i++) {
	if ((prom[0] == hw_info[i].a0) &&
	    (prom[2] == hw_info[i].a1) &&
	    (prom[4] == hw_info[i].a2))
	    break;
    }
    if (i < NR_INFO) {
	for (j = 0; j < 6; j++)
	    dev->dev_addr[j] = prom[j<<1];
	return hw_info+i;
    }
    if ((prom[28] == 0x57) && (prom[30] == 0x57)) {
	for (j = 0; j < 6; j++)
	    dev->dev_addr[j] = prom[j<<1];
	return &default_info;
    }
    return NULL;
} /* get_prom */

/*======================================================================

    This should be totally unnecessary... but when we can't figure
    out the hardware address any other way, we'll let the user hard
    wire it when the module is initialized.

======================================================================*/

static hw_info_t *get_hwired(dev_link_t *link)
{
    struct device *dev = link->priv;
    int i;

    for (i = 0; i < 6; i++)
	if (hw_addr[i] != 0) break;
    if (i == 6)
	return NULL;

    for (i = 0; i < 6; i++)
	dev->dev_addr[i] = hw_addr[i];

    return &default_info;
} /* get_hwired */

/*======================================================================

    pcnet_config() is scheduled to run after a CARD_INSERTION event
    is received, to configure the PCMCIA socket, and to make the
    ethernet device available to the system.

======================================================================*/

#define CS_EXIT_TEST(ret, svc, label) \
    if (ret != CS_SUCCESS) { cs_error(svc, ret); goto label; }

static void pcnet_config(dev_link_t *link)
{
    client_handle_t handle;
    tuple_t tuple;
    cisparse_t parse;
    pcnet_dev_t *info;
    struct device *dev;
    int i, j, start_pg, stop_pg, cm_offset;
    int manfid = 0, multi = 0;
    u_short buf[64];
    hw_info_t *hw_info;

    handle = link->handle;
    info = link->priv;
    dev = &info->dev;

    DEBUG(0, "pcnet_config(0x%p)\n", link);

    tuple.Attributes = 0;
    tuple.DesiredTuple = CISTPL_CONFIG;
    i = CardServices(GetFirstTuple, handle, &tuple);
    CS_EXIT_TEST(i, GetFirstTuple, config_failed);
    tuple.TupleData = (cisdata_t *)buf;
    tuple.TupleDataMax = sizeof(buf);
    tuple.TupleOffset = 0;
    i = CardServices(GetTupleData, handle, &tuple);
    CS_EXIT_TEST(i, GetTupleData, config_failed);
    i = CardServices(ParseTuple, handle, &tuple, &parse);
    CS_EXIT_TEST(i, ParseTuple, config_failed);
    link->conf.ConfigBase = parse.config.base;
    link->conf.Present = parse.config.rmask[0];

    /* Configure card */
    link->state |= DEV_CONFIG;

    /* Is this a the IBM Home and Away Card? */
    tuple.DesiredTuple = CISTPL_MANFID;
    tuple.Attributes = TUPLE_RETURN_COMMON;
    if ((CardServices(GetFirstTuple, handle, &tuple) == CS_SUCCESS) &&
 	(CardServices(GetTupleData, handle, &tuple) == CS_SUCCESS)) {
	manfid = buf[0];
 	if ((manfid == MANFID_IBM) &&
	    (buf[1] == PRODID_IBM_HOME_AND_AWAY)) {
	    multi = 1;
	    link->io.NumPorts1 = 32;
	    link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
	    link->io.NumPorts2 = 0;
	}
 	if ((manfid == MANFID_LINKSYS) &&
	    (buf[1] == PRODID_LINKSYS_PCMLM28)) {
	    multi = 1;
	    link->io.NumPorts1 = 32;
	    link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
	    link->io.NumPorts2 = 8;
	    link->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
	    link->irq.Attributes =
		IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
	}
    }
    
    tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
    tuple.Attributes = 0;
    i = CardServices(GetFirstTuple, handle, &tuple);
    while (i == CS_SUCCESS) {
	i = CardServices(GetTupleData, handle, &tuple);
	if (i != CS_SUCCESS) break;
	i = CardServices(ParseTuple, handle, &tuple, &parse);
	if (i == CS_SUCCESS) {
	    link->conf.ConfigIndex = parse.cftable_entry.index;
	    link->io.BasePort1 = parse.cftable_entry.io.win[0].base;
	    if (link->io.BasePort1 != 0) {
		if (parse.cftable_entry.io.nwin > 1)
		    link->io.BasePort2 = parse.cftable_entry.io.win[1].base;
		else
		    link->io.BasePort2 = link->io.BasePort1 + 0x10;
		i = CardServices(RequestIO, link->handle, &link->io);
	    } else {
		for (j = 0; j < 0x400; j += 0x20) {
		    link->io.BasePort1 = j ^ 0x300;
		    link->io.BasePort2 = (j ^ 0x300) + 0x10;
		    i = CardServices(RequestIO, link->handle, &link->io);
		    if (i == CS_SUCCESS) break;
		}
	    }
	    if (i == CS_SUCCESS) break;
	}
	i = CardServices(GetNextTuple, handle, &tuple);
    }
    if ((i != CS_SUCCESS) && !multi) {
	link->conf.ConfigIndex = 1;
	for (j = 0x100; j < 0x400; j += 0x20) {
	    link->io.BasePort1 = j;
	    link->io.BasePort2 = j+0x10;
	    i = CardServices(RequestIO, link->handle, &link->io);
	    if (i == CS_SUCCESS) break;
	}
	CS_EXIT_TEST(i, RequestIO, config_failed);
    }
    
    i = CardServices(RequestIRQ, link->handle, &link->irq);
    CS_EXIT_TEST(i, RequestIRQ, config_failed);
    if (multi) {
	link->conf.Attributes |= CONF_ENABLE_SPKR;
	link->conf.Status = CCSR_AUDIO_ENA;
    }
    if (multi && (manfid == MANFID_IBM))
	/* For IBM Home and Away Card */
	link->conf.ConfigIndex |= 0x10;
    i = CardServices(RequestConfiguration, link->handle, &link->conf);
    CS_EXIT_TEST(i, RequestConfiguration, config_failed);
    dev->irq = link->irq.AssignedIRQ;
    dev->base_addr = link->io.BasePort1;
    if ((if_port == 1) || (if_port == 2))
	dev->if_port = if_port;
    else
	printk(KERN_NOTICE "pcnet_cs: invalid if_port requested\n");
    dev->tbusy = 0;
    i = register_netdev(dev);
    if (i != 0) {
	printk(KERN_NOTICE "pcnet_cs: register_netdev() failed\n");
	goto config_failed;
    }

    hw_info = get_hwinfo(link);
    if (hw_info == NULL)
	hw_info = get_prom(link);
    if (hw_info == NULL)
	hw_info = get_hwired(link);
    
    if (hw_info == NULL) {
	printk(KERN_NOTICE "pcnet_cs: unable to read hardware net address\n");
	goto config_undo;
    }

    info->flags = hw_info->flags | (multi ? IS_MULTIFN : 0);
    /* Check for user overrides */
    if (delay_output)
	info->flags |= DELAY_OUTPUT;
    if (!use_big_buf)
	info->flags &= ~USE_BIG_BUF;
    if (use_shmem != -1) {
	if (use_shmem)
	    info->flags |= USE_SHMEM;
	else
	    info->flags &= ~USE_SHMEM;
    }
    
    if (info->flags & USE_BIG_BUF) {
	start_pg = SOCKET_START_PG;
	stop_pg = SOCKET_STOP_PG;
	cm_offset = 0x10000;
    }
    else {
	start_pg = PCNET_START_PG;
	stop_pg = PCNET_STOP_PG;
	cm_offset = 0;
    }

    if (info->flags & USE_SHMEM)
	i = setup_shmem_window(link, start_pg, stop_pg, cm_offset);
    else
	i = setup_dma_config(link, start_pg, stop_pg);
    if (i != 0)
	goto config_undo;

    ei_status.name = hw_info->id;
    ei_status.word16 = 1;
    ei_status.reset_8390 = &pcnet_reset_8390;

    link->dev = &info->node;
    link->state &= ~DEV_CONFIG_PENDING;

    printk(KERN_INFO "%s: %s, port %#3lx, irq %d,",
	   dev->name, hw_info->id, dev->base_addr, dev->irq);
    if (info->flags & USE_SHMEM)
	printk (" mem %#5lx,", dev->mem_start);
    if (info->flags & HAS_MISC_REG)
	printk(" %s port,", if_names[dev->if_port]);
    for (i = 0; i < ETHER_ADDR_LEN; i++)
	printk(" %02X", dev->dev_addr[i]);
    printk("\n");
    return;

config_undo:
    unregister_netdev(dev);
config_failed:    /* CS_EXIT_TEST() calls jump to here... */
    pcnet_release((u_long)link);

} /* pcnet_config */

/*======================================================================

    After a card is removed, pcnet_release() will unregister the net
    device, and release the PCMCIA configuration.  If the device is
    still open, this will be postponed until it is closed.

======================================================================*/

static void pcnet_release(u_long arg)
{
    dev_link_t *link = (dev_link_t *)arg;
    pcnet_dev_t *info = link->priv;

    DEBUG(0, "pcnet_release(0x%p)\n", link);

    if (link->open) {
	DEBUG(1, "pcnet_cs: release postponed, '%s' still open\n",
	      info->node.dev_name);
	link->state |= DEV_STALE_CONFIG;
	return;
    }

    if (link->dev)
	unregister_netdev(&info->dev);
    link->dev = NULL;

    if (info->flags & USE_SHMEM)
	CardServices(ReleaseWindow, link->win);
    CardServices(ReleaseConfiguration, link->handle);
    CardServices(ReleaseIO, link->handle, &link->io);
    CardServices(ReleaseIRQ, link->handle, &link->irq);
    if (info->dev.irq != 0)
	irq2dev_map[info->dev.irq] = NULL;

    link->state &= ~(DEV_CONFIG | DEV_RELEASE_PENDING);
    if (link->state & DEV_STALE_LINK)
	pcnet_detach(link);

} /* pcnet_release */

/*======================================================================

    The card status event handler.  Mostly, this schedules other
    stuff to run after an event is received.  A CARD_REMOVAL event
    also sets some flags to discourage the net drivers from trying
    to talk to the card any more.

======================================================================*/

static int pcnet_event(event_t event, int priority,
		       event_callback_args_t *args)
{
    dev_link_t *link = args->client_data;
    pcnet_dev_t *info = link->priv;

    DEBUG(2, "pcnet_event(0x%06x)\n", event);

    switch (event) {
    case CS_EVENT_CARD_REMOVAL:
	link->state &= ~DEV_PRESENT;
	if (link->state & DEV_CONFIG) {
	    info->dev.tbusy = 1; info->dev.start = 0;
	    link->release.expires = RUN_AT(HZ/20);
	    link->state |= DEV_RELEASE_PENDING;
	    add_timer(&link->release);
	}
	break;
    case CS_EVENT_CARD_INSERTION:
	link->state |= DEV_PRESENT;
	pcnet_config(link);
	break;
    case CS_EVENT_PM_SUSPEND:
	link->state |= DEV_SUSPEND;
	/* Fall through... */
    case CS_EVENT_RESET_PHYSICAL:
	if (link->state & DEV_CONFIG) {
	    if (link->open) {
		info->dev.tbusy = 1; info->dev.start = 0;
	    }
	    CardServices(ReleaseConfiguration, link->handle);
	}
	break;
    case CS_EVENT_PM_RESUME:
	link->state &= ~DEV_SUSPEND;
	/* Fall through... */
    case CS_EVENT_CARD_RESET:
	if (link->state & DEV_CONFIG) {
	    CardServices(RequestConfiguration, link->handle, &link->conf);
	    if (link->open) {
		pcnet_reset_8390(&info->dev);
		NS8390_init(&info->dev, 1);
		info->dev.tbusy = 0; info->dev.start = 1;
	    }
	}
	break;
    }
    return 0;
} /* pcnet_event */

/*====================================================================*/

static void set_misc_reg(struct device *dev)
{
    int nic_base = dev->base_addr;
    pcnet_dev_t *info = (pcnet_dev_t *)dev;
    u_char tmp;
    
    if (info->flags & HAS_MISC_REG) {
	tmp = inb_p(nic_base + PCNET_MISC) & ~3;
	if (dev->if_port == 2)
	    tmp |= 1;
	if (info->flags & USE_BIG_BUF)
	    tmp |= 2;
	if (info->flags & HAS_IBM_MISC)
	    tmp |= 8;
	outb_p(tmp, nic_base + PCNET_MISC);
    }
}

/*====================================================================*/

static int pcnet_open(struct device *dev)
{
    pcnet_dev_t *info = (pcnet_dev_t *)dev;
    dev_link_t *link;
    
    DEBUG(2, "pcnet_open('%s')\n", dev->name);

    for (link = dev_list; link; link = link->next)
	if (link->priv == dev) break;
    if (!DEV_OK(link))
	return -ENODEV;

    link->open++;
    MOD_INC_USE_COUNT;

    set_misc_reg(dev);
    REQUEST_IRQ(dev->irq, ei_interrupt,
		((info->flags & IS_MULTIFN) ? SA_SHIRQ : 0),
		dev_info, dev_info);
    return ei_open(dev);
} /* pcnet_open */

/*====================================================================*/

static int pcnet_close(struct device *dev)
{
    dev_link_t *link;

    DEBUG(2, "pcnet_close('%s')\n", dev->name);

    for (link = dev_list; link; link = link->next)
	if (link->priv == dev) break;
    if (link == NULL)
	return -ENODEV;

    FREE_IRQ(dev->irq, dev_info);
    
    link->open--; dev->start = 0;
    if (link->state & DEV_STALE_CONFIG) {
	link->release.expires = RUN_AT(HZ/20);
	link->state |= DEV_RELEASE_PENDING;
	add_timer(&link->release);
    }

    MOD_DEC_USE_COUNT;

    return 0;
} /* pcnet_close */

/*======================================================================

    Hard reset the card.  This used to pause for the same period that
    a 8390 reset command required, but that shouldn't be necessary.

======================================================================*/

static void pcnet_reset_8390(struct device *dev)
{
    int nic_base = dev->base_addr;
    int i;

    ei_status.txing = ei_status.dmaing = 0;

    outb(inb(nic_base + PCNET_RESET), nic_base + PCNET_RESET);

    for (i = 0; i < 100; i++) {
	if ((inb_p(nic_base+EN0_ISR) & ENISR_RESET) != 0)
	    break;
	udelay(100L);
    }
    outb_p(ENISR_RESET, nic_base + EN0_ISR); /* Ack intr. */
    
    if (i == 100)
	printk(KERN_ERR "%s: pcnet_reset_8390() did not complete.\n",
	       dev->name);
    set_misc_reg(dev);
    
} /* pcnet_reset_8390 */

/* ======================================================================= */

static int set_config(struct device *dev, struct ifmap *map)
{
    pcnet_dev_t *info = (pcnet_dev_t *)dev;
    if (!(info->flags & HAS_MISC_REG)) {
	printk(KERN_NOTICE "%s: transceiver selection not "
	       "implemented\n", dev->name);
	return -EINVAL;
    }
    if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
	if ((map->port == 1) || (map->port == 2)) {
	    dev->if_port = map->port;
	    printk(KERN_INFO "%s: switched to %s port\n",
		   dev->name, if_names[dev->if_port]);
	}
	else
	    return -EINVAL;
    }
    return 0;
}

/* ======================================================================= */

#ifdef GET_8390_HDR
static void dma_get_8390_hdr(struct device *dev,
			     struct e8390_pkt_hdr *hdr,
			     int ring_page)
{
    int nic_base = dev->base_addr;

    if (ei_status.dmaing) {
	printk(KERN_NOTICE "%s: DMAing conflict in dma_block_input."
	       "[DMAstat:%1x][irqlock:%1x][intr:%d]\n",
	       dev->name, ei_status.dmaing, ei_status.irqlock,
	       dev->interrupt);
	return;
    }
    
    ei_status.dmaing |= 0x01;
    outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base + PCNET_CMD);
    outb_p(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
    outb_p(0, nic_base + EN0_RCNTHI);
    outb_p(0, nic_base + EN0_RSARLO);		/* On page boundary */
    outb_p(ring_page, nic_base + EN0_RSARHI);
    outb_p(E8390_RREAD+E8390_START, nic_base + PCNET_CMD);

    insw(nic_base + PCNET_DATAPORT, hdr,
	 sizeof(struct e8390_pkt_hdr)>>1);

    outb_p(ENISR_RDC, nic_base + EN0_ISR);	/* Ack intr. */
    ei_status.dmaing &= ~0x01;
}
#endif /* GET_8390_HDR */

/* ======================================================================= */

#ifdef GET_8390_HDR
static void dma_block_input(struct device *dev, int count,
			    struct sk_buff *skb, int ring_offset)
#else
static int dma_block_input(struct device *dev, int count,
			   char *buf, int ring_offset)
#endif
{
    int nic_base = dev->base_addr;
    int xfer_count = count;
#ifdef GET_8390_HDR
    char *buf = skb->data;
#endif

#ifdef PCMCIA_DEBUG
    if ((ei_debug > 4) && (count != 4))
	printk(KERN_DEBUG "%s: [bi=%d]\n", dev->name, count+4);
#endif
    if (ei_status.dmaing) {
	printk(KERN_NOTICE "%s: DMAing conflict in dma_block_input."
	       "[DMAstat:%1x][irqlock:%1x][intr:%d]\n",
	       dev->name, ei_status.dmaing, ei_status.irqlock,
	       dev->interrupt);
#ifdef GET_8390_HDR
	return;
#else
	return 0;
#endif
    }
    ei_status.dmaing |= 0x01;
    outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base + PCNET_CMD);
    outb_p(count & 0xff, nic_base + EN0_RCNTLO);
    outb_p(count >> 8, nic_base + EN0_RCNTHI);
    outb_p(ring_offset & 0xff, nic_base + EN0_RSARLO);
    outb_p(ring_offset >> 8, nic_base + EN0_RSARHI);
    outb_p(E8390_RREAD+E8390_START, nic_base + PCNET_CMD);

    insw(nic_base + PCNET_DATAPORT,buf,count>>1);
    if (count & 0x01)
	buf[count-1] = inb(nic_base + PCNET_DATAPORT), xfer_count++;

    /* This was for the ALPHA version only, but enough people have
       encountering problems that it is still here. */
#ifdef PCMCIA_DEBUG
    if (ei_debug > 4) {		/* DMA termination address check... */
	int addr, tries = 20;
	do {
	    /* DON'T check for 'inb_p(EN0_ISR) & ENISR_RDC' here
	       -- it's broken for Rx on some cards! */
	    int high = inb_p(nic_base + EN0_RSARHI);
	    int low = inb_p(nic_base + EN0_RSARLO);
	    addr = (high << 8) + low;
	    if (((ring_offset + xfer_count) & 0xff) == (addr & 0xff))
		break;
	} while (--tries > 0);
	if (tries <= 0)
	    printk(KERN_NOTICE "%s: RX transfer address mismatch,"
		   "%#4.4x (expected) vs. %#4.4x (actual).\n",
		   dev->name, ring_offset + xfer_count, addr);
    }
#endif
    outb_p(ENISR_RDC, nic_base + EN0_ISR);	/* Ack intr. */
    ei_status.dmaing &= ~0x01;
#ifndef GET_8390_HDR
    return ring_offset + count;
#endif
} /* dma_block_input */

/*====================================================================*/

static void dma_block_output(struct device *dev, int count,
			     const unsigned char *buf,
			     const int start_page)
{
    int nic_base = dev->base_addr;
    pcnet_dev_t *info = (pcnet_dev_t *)dev;
#ifdef PCMCIA_DEBUG
    int retries = 0;
#endif
    u_long dma_start;

#ifdef PCMCIA_DEBUG
    if (ei_debug > 4)
	printk(KERN_DEBUG "%s: [bo=%d]\n", dev->name, count);
#endif

    /* Round the count up for word writes.  Do we need to do this?
       What effect will an odd byte count have on the 8390?
       I should check someday. */
    if (count & 0x01)
	count++;
    if (ei_status.dmaing) {
	printk(KERN_NOTICE "%s: DMAing conflict in dma_block_output."
	       "[DMAstat:%1x][irqlock:%1x][intr:%d]\n",
	       dev->name, ei_status.dmaing, ei_status.irqlock,
	       dev->interrupt);
	return;
    }
    ei_status.dmaing |= 0x01;
    /* We should already be in page 0, but to be safe... */
    outb_p(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base+PCNET_CMD);

#ifdef PCMCIA_DEBUG
  retry:
#endif

    outb_p(ENISR_RDC, nic_base + EN0_ISR);

    /* Now the normal output. */
    outb_p(count & 0xff, nic_base + EN0_RCNTLO);
    outb_p(count >> 8,   nic_base + EN0_RCNTHI);
    outb_p(0x00, nic_base + EN0_RSARLO);
    outb_p(start_page, nic_base + EN0_RSARHI);

    outb_p(E8390_RWRITE+E8390_START, nic_base + PCNET_CMD);
    outsw(nic_base + PCNET_DATAPORT, buf, count>>1);

    dma_start = jiffies;

#ifdef PCMCIA_DEBUG
    /* This was for the ALPHA version only, but enough people have
       encountering problems that it is still here. */
    if (ei_debug > 4) {	/* DMA termination address check... */
	int addr, tries = 20;
	do {
	    int high = inb_p(nic_base + EN0_RSARHI);
	    int low = inb_p(nic_base + EN0_RSARLO);
	    addr = (high << 8) + low;
	    if ((start_page << 8) + count == addr)
		break;
	} while (--tries > 0);
	if (tries <= 0) {
	    printk(KERN_NOTICE "%s: Tx packet transfer address mismatch,"
		   "%#4.4x (expected) vs. %#4.4x (actual).\n",
		   dev->name, (start_page << 8) + count, addr);
	    if (retries++ == 0)
		goto retry;
	}
    }
#endif

    while ((inb_p(nic_base + EN0_ISR) & ENISR_RDC) == 0)
	if (jiffies - dma_start > PCNET_RDC_TIMEOUT) {
	    printk(KERN_NOTICE "%s: timeout waiting for Tx RDC.\n",
		   dev->name);
	    pcnet_reset_8390(dev);
	    NS8390_init(dev, 1);
	    break;
	}

    outb_p(ENISR_RDC, nic_base + EN0_ISR);	/* Ack intr. */
    if (info->flags & DELAY_OUTPUT)
	udelay((long)delay_time);
    ei_status.dmaing &= ~0x01;
}

/*====================================================================*/

static int setup_dma_config(dev_link_t *link, int start_pg,
			    int stop_pg)
{
    struct device *dev = link->priv;

    ei_status.tx_start_page = start_pg;
    ei_status.rx_start_page = start_pg + TX_PAGES;
    ei_status.stop_page = stop_pg;

    /* set up block i/o functions */
#ifdef GET_8390_HDR
    ei_status.get_8390_hdr = &dma_get_8390_hdr;
#endif
    ei_status.block_input = &dma_block_input;
    ei_status.block_output = &dma_block_output;

    return 0;
}

/*====================================================================*/

static void copyin(unsigned char *dest, unsigned char *src, int c)
{
    unsigned short *d = (unsigned short *) dest;
    unsigned short *s = (unsigned short *) src;
    int odd;

    if (c <= 0)
	return;
    odd = (c & 01); c >>= 1;

    if (c) {
	do { *d++ = readw(s++); } while (--c);
    }
    /* get last byte by fetching a word and masking */
    if (odd)
	*((unsigned char *)d) = readw(s) & 0xff;
}

static void copyout(unsigned char *dest, const unsigned char *src, int c)
{
    volatile unsigned short *d = (unsigned short *) dest;
    unsigned short *s = (unsigned short *) src;
    int odd;

    if (c <= 0)
	return;
    odd = (c & 01); c >>= 1;

    if (c) {
	do { writew(*s++, d++); } while (--c);
    }
    /* copy last byte doing a read-modify-write */
    if (odd)
	writew((readw(d) & 0xff00) | *(u_char *)s, d);
}

/*====================================================================*/

#ifdef GET_8390_HDR
static void shmem_get_8390_hdr(struct device *dev,
			       struct e8390_pkt_hdr *hdr,
			       int ring_page)
{
    void *xfer_start = (void *)(dev->rmem_start + (ring_page << 8)
				- (ei_status.rx_start_page << 8));
    
    copyin((void *)hdr, xfer_start, sizeof(struct e8390_pkt_hdr));
}
#endif

/*====================================================================*/

#ifdef GET_8390_HDR
static void shmem_block_input(struct device *dev, int count,
			      struct sk_buff *skb, int ring_offset)
#else
static int shmem_block_input(struct device *dev, int count,
			     char *buf, int ring_offset)
#endif
{
    void *xfer_start = (void *)(dev->rmem_start + ring_offset
				- (ei_status.rx_start_page << 8));
#ifdef GET_8390_HDR
    char *buf = skb->data;
#endif
    
    if (xfer_start + count > (void *)dev->rmem_end) {
	/* We must wrap the input move. */
	int semi_count = (void*)dev->rmem_end - xfer_start;
	copyin(buf, xfer_start, semi_count);
	buf += semi_count;
	ring_offset = ei_status.rx_start_page << 8;
	xfer_start = (void *)dev->rmem_start;
	count -= semi_count;
    }
    copyin(buf, xfer_start, count);
#ifndef GET_8390_HDR
    return ring_offset + count;
#endif
}

/*====================================================================*/

static void shmem_block_output(struct device *dev, int count,
			       const unsigned char *buf,
			       const int start_page)
{
    void *shmem = (void *)dev->mem_start + (start_page << 8);
    shmem -= ei_status.tx_start_page << 8;

    if (ei_debug > 4)
	printk(KERN_DEBUG "[bo=%d @ %x]\n", count, start_page);

    copyout(shmem, buf, count);
}

/*====================================================================*/

static int setup_shmem_window(dev_link_t *link, int start_pg,
			      int stop_pg, int cm_offset)
{
    win_req_t req;
    memreq_t mem;
    int offset = 0, i;
    struct device *dev = link->priv;
    int window_size;

    window_size = (stop_pg - start_pg) << 8;
    if (window_size > 32 * 1024)
	window_size = 32 * 1024;

    /* Make sure it's a power of two.  */
    while ((window_size & (window_size - 1)) != 0)
	window_size += window_size & ~(window_size - 1);

    /* Allocate a memory window */
    req.Attributes = WIN_DATA_WIDTH_16|WIN_MEMORY_TYPE_CM|WIN_ENABLE;
    req.Attributes |= WIN_USE_WAIT;
    req.Base = NULL;
    req.Size = window_size;
    req.AccessSpeed = mem_speed;
    link->win = (window_handle_t)link->handle;
    i = CardServices(RequestWindow, &link->win, &req);
    CS_EXIT_TEST(i, RequestWindow, config_failed);

    mem.CardOffset = (start_pg << 8) + cm_offset;
    offset = mem.CardOffset % window_size;
    mem.CardOffset -= offset;
    mem.Page = 0;
    i = CardServices(MapMemPage, link->win, &mem);
    CS_EXIT_TEST(i, MapMemPage, config_failed);

    dev->mem_start = (u_long)req.Base + offset;
    dev->rmem_start = dev->mem_start + (TX_PAGES<<8);
    dev->mem_end = dev->rmem_end = (u_long)req.Base + req.Size;

    ei_status.tx_start_page = start_pg;
    ei_status.rx_start_page = start_pg + TX_PAGES;
    ei_status.stop_page = start_pg + ((req.Size - offset) >> 8);

    /* set up block i/o functions */
#ifdef GET_8390_HDR
    ei_status.get_8390_hdr = &shmem_get_8390_hdr;
#endif
    ei_status.block_input = &shmem_block_input;
    ei_status.block_output = &shmem_block_output;

    return 0;

config_failed:
    return 1;
}

/*====================================================================*/

int init_module(void)
{
    servinfo_t serv;
    DEBUG(0, "%s\n", version);
    CardServices(GetCardServicesInfo, &serv);
    if (serv.Revision != CS_RELEASE_CODE) {
	printk(KERN_NOTICE "pcnet_cs: Card Services release "
	       "does not match!\n");
	return -1;
    }
    register_pcmcia_driver(&dev_info, &pcnet_attach, &pcnet_detach);
    return 0;
}

void cleanup_module(void)
{
    DEBUG(0, "pcnet_cs: unloading\n");
    unregister_pcmcia_driver(&dev_info);
    while (dev_list != NULL)
	pcnet_detach(dev_list);
}
