Home | History | Annotate | Line # | Download | only in pcmcia
if_cnw.c revision 1.11.2.2
      1 /*	$NetBSD: if_cnw.c,v 1.11.2.2 2000/10/17 23:20:53 tv Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Michael Eriksson.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1996, 1997 Berkeley Software Design, Inc.
     41  * All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that this notice is retained,
     45  * the conditions in the following notices are met, and terms applying
     46  * to contributors in the following notices also apply to Berkeley
     47  * Software Design, Inc.
     48  *
     49  * 1. Redistributions of source code must retain the above copyright
     50  *    notice, this list of conditions and the following disclaimer.
     51  * 2. Redistributions in binary form must reproduce the above copyright
     52  *    notice, this list of conditions and the following disclaimer in the
     53  *    documentation and/or other materials provided with the distribution.
     54  * 3. All advertising materials mentioning features or use of this software
     55  *    must display the following acknowledgement:
     56  *      This product includes software developed by
     57  *	Berkeley Software Design, Inc.
     58  * 4. Neither the name of the Berkeley Software Design, Inc. nor the names
     59  *    of its contributors may be used to endorse or promote products derived
     60  *    from this software without specific prior written permission.
     61  *
     62  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN, INC. ``AS IS'' AND
     63  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     64  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     65  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN, INC. BE LIABLE
     66  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     67  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     68  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     69  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     70  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     71  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     72  * SUCH DAMAGE.
     73  *
     74  * Paul Borman, December 1996
     75  *
     76  * This driver is derived from a generic frame work which is
     77  * Copyright(c) 1994,1995,1996
     78  * Yoichi Shinoda, Yoshitaka Tokugawa, WIDE Project, Wildboar Project
     79  * and Foretune.  All rights reserved.
     80  *
     81  * A linux driver was used as the "hardware reference manual" (i.e.,
     82  * to determine registers and a general outline of how the card works)
     83  * That driver is publically available and copyright
     84  *
     85  * John Markus Bjrndalen
     86  * Department of Computer Science
     87  * University of Troms
     88  * Norway
     89  * johnm (at) staff.cs.uit.no, http://www.cs.uit.no/~johnm/
     90  */
     91 
     92 /*
     93  * This is a driver for the Xircom CreditCard Netwave (also known as
     94  * the Netwave Airsurfer) wireless LAN PCMCIA adapter.
     95  *
     96  * When this driver was developed, the Linux Netwave driver was used
     97  * as a hardware manual. That driver is Copyright (c) 1997 University
     98  * of Troms, Norway. It is part of the Linix pcmcia-cs package that
     99  * can be found at
    100  * http://hyper.stanford.edu/HyperNews/get/pcmcia/home.html. The most
    101  * recent version of the pcmcia-cs package when this driver was
    102  * written was 3.0.6.
    103  *
    104  * Unfortunately, a lot of explicit numeric constants were used in the
    105  * Linux driver. I have tried to use symbolic names whenever possible,
    106  * but since I don't have any real hardware documentation, there's
    107  * still one or two "magic numbers" :-(.
    108  *
    109  * Driver limitations: This driver doesn't do multicasting or receiver
    110  * promiscuity, because of missing hardware documentation. I couldn't
    111  * get receiver promiscuity to work, and I haven't even tried
    112  * multicast. Volunteers are welcome, of course :-).
    113  */
    114 
    115 #include "opt_inet.h"
    116 #include "bpfilter.h"
    117 
    118 #include <sys/param.h>
    119 #include <sys/systm.h>
    120 #include <sys/device.h>
    121 #include <sys/socket.h>
    122 #include <sys/mbuf.h>
    123 #include <sys/ioctl.h>
    124 #include <sys/proc.h>
    125 
    126 #include <net/if.h>
    127 
    128 #include <dev/pcmcia/if_cnwreg.h>
    129 #include <dev/pcmcia/if_cnwioctl.h>
    130 
    131 #include <dev/pcmcia/pcmciareg.h>
    132 #include <dev/pcmcia/pcmciavar.h>
    133 #include <dev/pcmcia/pcmciadevs.h>
    134 
    135 #include <net/if_dl.h>
    136 #include <net/if_ether.h>
    137 
    138 #ifdef INET
    139 #include <netinet/in.h>
    140 #include <netinet/in_systm.h>
    141 #include <netinet/in_var.h>
    142 #include <netinet/ip.h>
    143 #include <netinet/if_inarp.h>
    144 #endif
    145 
    146 #if NBPFILTER > 0
    147 #include <net/bpf.h>
    148 #include <net/bpfdesc.h>
    149 #endif
    150 
    151 /*
    152  * Let these be patchable variables, initialized from macros that can
    153  * be set in the kernel config file. Someone with lots of spare time
    154  * could probably write a nice Netwave configuration program to do
    155  * this a little bit more elegantly :-).
    156  */
    157 #ifndef CNW_DOMAIN
    158 #define CNW_DOMAIN	0x100
    159 #endif
    160 int cnw_domain = CNW_DOMAIN;		/* Domain */
    161 #ifndef CNW_SCRAMBLEKEY
    162 #define CNW_SCRAMBLEKEY 0
    163 #endif
    164 int cnw_skey = CNW_SCRAMBLEKEY;		/* Scramble key */
    165 
    166 /*
    167  * The card appears to work much better when we only allow one packet
    168  * "in the air" at a time.  This is done by not allowing another packet
    169  * on the card, even if there is room.  Turning this off will allow the
    170  * driver to stuff packets on the card as soon as a transmit buffer is
    171  * available.  This does increase the number of collisions, though.
    172  * We can que a second packet if there are transmit buffers available,
    173  * but we do not actually send the packet until the last packet has
    174  * been written.
    175  */
    176 #define	ONE_AT_A_TIME
    177 
    178 /*
    179  * Netwave cards choke if we try to use io memory address >= 0x400.
    180  * Even though, CIS tuple does not talk about this.
    181  * Use memory mapped access.
    182  */
    183 #define MEMORY_MAPPED
    184 
    185 int	cnw_match __P((struct device *, struct cfdata *, void *));
    186 void	cnw_attach __P((struct device *, struct device *, void *));
    187 int	cnw_detach __P((struct device *, int));
    188 
    189 int	cnw_activate __P((struct device *, enum devact));
    190 
    191 struct cnw_softc {
    192 	struct device sc_dev;		    /* Device glue (must be first) */
    193 	struct ethercom sc_ethercom;	    /* Ethernet common part */
    194 	int sc_domain;			    /* Netwave domain */
    195 	int sc_skey;			    /* Netwave scramble key */
    196 	struct cnwstats sc_stats;
    197 
    198 	/* PCMCIA-specific stuff */
    199 	struct pcmcia_function *sc_pf;	    /* PCMCIA function */
    200 #ifndef MEMORY_MAPPED
    201 	struct pcmcia_io_handle sc_pcioh;   /* PCMCIA I/O space handle */
    202 	int sc_iowin;			    /*   ...window */
    203 	bus_space_tag_t sc_iot;		    /*   ...bus_space tag */
    204 	bus_space_handle_t sc_ioh;	    /*   ...bus_space handle */
    205 #endif
    206 	struct pcmcia_mem_handle sc_pcmemh; /* PCMCIA memory handle */
    207 	bus_addr_t sc_memoff;		    /*   ...offset */
    208 	int sc_memwin;			    /*   ...window */
    209 	bus_space_tag_t sc_memt;	    /*   ...bus_space tag */
    210 	bus_space_handle_t sc_memh;	    /*   ...bus_space handle */
    211 	void *sc_ih;			    /* Interrupt cookie */
    212 	struct timeval sc_txlast;	    /* When the last xmit was made */
    213 	int sc_active;			    /* Currently xmitting a packet */
    214 
    215 	int sc_resource;		    /* Resources alloc'ed on attach */
    216 #define CNW_RES_PCIC	1
    217 #define CNW_RES_IO	2
    218 #define CNW_RES_MEM	4
    219 #define CNW_RES_NET	8
    220 };
    221 
    222 struct cfattach cnw_ca = {
    223 	sizeof(struct cnw_softc), cnw_match, cnw_attach, cnw_detach,
    224 		cnw_activate
    225 };
    226 
    227 
    228 void cnw_reset __P((struct cnw_softc *));
    229 void cnw_init __P((struct cnw_softc *));
    230 int cnw_enable __P((struct cnw_softc *sc));
    231 void cnw_disable __P((struct cnw_softc *sc));
    232 void cnw_config __P((struct cnw_softc *sc, u_int8_t *));
    233 void cnw_start __P((struct ifnet *));
    234 void cnw_transmit __P((struct cnw_softc *, struct mbuf *));
    235 struct mbuf *cnw_read __P((struct cnw_softc *));
    236 void cnw_recv __P((struct cnw_softc *));
    237 int cnw_intr __P((void *arg));
    238 int cnw_ioctl __P((struct ifnet *, u_long, caddr_t));
    239 void cnw_watchdog __P((struct ifnet *));
    240 static int cnw_setdomain __P((struct cnw_softc *, int));
    241 static int cnw_setkey __P((struct cnw_softc *, int));
    242 
    243 /* ---------------------------------------------------------------- */
    244 
    245 /* Help routines */
    246 static int wait_WOC __P((struct cnw_softc *, int));
    247 static int read16 __P((struct cnw_softc *, int));
    248 static int cnw_cmd __P((struct cnw_softc *, int, int, int, int));
    249 
    250 /*
    251  * Wait until the WOC (Write Operation Complete) bit in the
    252  * ASR (Adapter Status Register) is asserted.
    253  */
    254 static int
    255 wait_WOC(sc, line)
    256 	struct cnw_softc *sc;
    257 	int line;
    258 {
    259 	int i, asr;
    260 
    261 	for (i = 0; i < 5000; i++) {
    262 #ifndef MEMORY_MAPPED
    263 		asr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, CNW_REG_ASR);
    264 #else
    265 		asr = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    266 		    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_ASR);
    267 #endif
    268 		if (asr & CNW_ASR_WOC)
    269 			return (0);
    270 		DELAY(100);
    271 	}
    272 	if (line > 0)
    273 		printf("%s: wedged at line %d\n", sc->sc_dev.dv_xname, line);
    274 	return (1);
    275 }
    276 #define WAIT_WOC(sc) wait_WOC(sc, __LINE__)
    277 
    278 
    279 /*
    280  * Read a 16 bit value from the card.
    281  */
    282 static int
    283 read16(sc, offset)
    284 	struct cnw_softc *sc;
    285 	int offset;
    286 {
    287 	int hi, lo;
    288 	int offs = sc->sc_memoff + offset;
    289 
    290 	/* This could presumably be done more efficient with
    291 	 * bus_space_read_2(), but I don't know anything about the
    292 	 * byte sex guarantees... Besides, this is pretty cheap as
    293 	 * well :-)
    294 	 */
    295 	lo = bus_space_read_1(sc->sc_memt, sc->sc_memh, offs);
    296 	hi = bus_space_read_1(sc->sc_memt, sc->sc_memh, offs + 1);
    297 	return ((hi << 8) | lo);
    298 }
    299 
    300 
    301 /*
    302  * Send a command to the card by writing it to the command buffer.
    303  */
    304 int
    305 cnw_cmd(sc, cmd, count, arg1, arg2)
    306 	struct cnw_softc *sc;
    307 	int cmd, count, arg1, arg2;
    308 {
    309 	int ptr = sc->sc_memoff + CNW_EREG_CB;
    310 
    311 	if (wait_WOC(sc, 0)) {
    312 		printf("%s: wedged when issuing cmd 0x%x\n",
    313 		    sc->sc_dev.dv_xname, cmd);
    314 		/*
    315 		 * We'll continue anyway, as that's probably the best
    316 		 * thing we can do; at least the user knows there's a
    317 		 * problem, and can reset the interface with ifconfig
    318 		 * down/up.
    319 		 */
    320 	}
    321 
    322 	bus_space_write_1(sc->sc_memt, sc->sc_memh, ptr, cmd);
    323 	if (count > 0) {
    324 		bus_space_write_1(sc->sc_memt, sc->sc_memh, ptr + 1, arg1);
    325 		if (count > 1)
    326 			bus_space_write_1(sc->sc_memt, sc->sc_memh,
    327 			    ptr + 2, arg2);
    328 	}
    329 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
    330 	    ptr + count + 1, CNW_CMD_EOC);
    331 	return (0);
    332 }
    333 #define CNW_CMD0(sc, cmd) \
    334     do { cnw_cmd(sc, cmd, 0, 0, 0); } while (0)
    335 #define CNW_CMD1(sc, cmd, arg1)	\
    336     do { cnw_cmd(sc, cmd, 1, arg1 , 0); } while (0)
    337 #define CNW_CMD2(sc, cmd, arg1, arg2) \
    338     do { cnw_cmd(sc, cmd, 2, arg1, arg2); } while (0)
    339 
    340 /* ---------------------------------------------------------------- */
    341 
    342 /*
    343  * Reset the hardware.
    344  */
    345 void
    346 cnw_reset(sc)
    347 	struct cnw_softc *sc;
    348 {
    349 #ifdef CNW_DEBUG
    350 	if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    351 		printf("%s: resetting\n", sc->sc_dev.dv_xname);
    352 #endif
    353 	wait_WOC(sc, 0);
    354 #ifndef MEMORY_MAPPED
    355 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, CNW_REG_PMR, CNW_PMR_RESET);
    356 #else
    357 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
    358 	    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_PMR, CNW_PMR_RESET);
    359 #endif
    360 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
    361 	    sc->sc_memoff + CNW_EREG_ASCC, CNW_ASR_WOC);
    362 #ifndef MEMORY_MAPPED
    363 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, CNW_REG_PMR, 0);
    364 #else
    365 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
    366 	    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_PMR, 0);
    367 #endif
    368 }
    369 
    370 
    371 /*
    372  * Initialize the card.
    373  */
    374 void
    375 cnw_init(sc)
    376 	struct cnw_softc *sc;
    377 {
    378 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    379 	const u_int8_t rxmode =
    380 	    CNW_RXCONF_RXENA | CNW_RXCONF_BCAST | CNW_RXCONF_AMP;
    381 
    382 	/* Reset the card */
    383 	cnw_reset(sc);
    384 
    385 	/* Issue a NOP to check the card */
    386 	CNW_CMD0(sc, CNW_CMD_NOP);
    387 
    388 	/* Set up receive configuration */
    389 	CNW_CMD1(sc, CNW_CMD_SRC,
    390 	    rxmode | ((ifp->if_flags & IFF_PROMISC) ? CNW_RXCONF_PRO : 0));
    391 
    392 	/* Set up transmit configuration */
    393 	CNW_CMD1(sc, CNW_CMD_STC, CNW_TXCONF_TXENA);
    394 
    395 	/* Set domain */
    396 	CNW_CMD2(sc, CNW_CMD_SMD, sc->sc_domain, sc->sc_domain >> 8);
    397 
    398 	/* Set scramble key */
    399 	CNW_CMD2(sc, CNW_CMD_SSK, sc->sc_skey, sc->sc_skey >> 8);
    400 
    401 	/* Enable interrupts */
    402 	WAIT_WOC(sc);
    403 #ifndef MEMORY_MAPPED
    404 	bus_space_write_1(sc->sc_iot, sc->sc_ioh,
    405 	    CNW_REG_IMR, CNW_IMR_IENA | CNW_IMR_RFU1);
    406 #else
    407 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
    408 	    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_IMR,
    409 	    CNW_IMR_IENA | CNW_IMR_RFU1);
    410 #endif
    411 
    412 	/* Enable receiver */
    413 	CNW_CMD0(sc, CNW_CMD_ER);
    414 
    415 	/* "Set the IENA bit in COR" */
    416 	WAIT_WOC(sc);
    417 #ifndef MEMORY_MAPPED
    418 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, CNW_REG_COR,
    419 	    CNW_COR_IENA | CNW_COR_LVLREQ);
    420 #else
    421 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
    422 	    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_COR,
    423 	    CNW_COR_IENA | CNW_COR_LVLREQ);
    424 #endif
    425 }
    426 
    427 
    428 /*
    429  * Enable and initialize the card.
    430  */
    431 int
    432 cnw_enable(sc)
    433 	struct cnw_softc *sc;
    434 {
    435 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    436 
    437 	if ((ifp->if_flags & IFF_RUNNING) != 0)
    438 		return (0);
    439 
    440 	sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_NET, cnw_intr, sc);
    441 	if (sc->sc_ih == NULL) {
    442 		printf("%s: couldn't establish interrupt handler\n",
    443 		    sc->sc_dev.dv_xname);
    444 		return (EIO);
    445 	}
    446 	if (pcmcia_function_enable(sc->sc_pf) != 0) {
    447 		printf("%s: couldn't enable card\n", sc->sc_dev.dv_xname);
    448 		return (EIO);
    449 	}
    450 	sc->sc_resource |= CNW_RES_PCIC;
    451 	cnw_init(sc);
    452 	ifp->if_flags &= ~IFF_OACTIVE;
    453 	ifp->if_flags |= IFF_RUNNING;
    454 	return (0);
    455 }
    456 
    457 
    458 /*
    459  * Stop and disable the card.
    460  */
    461 void
    462 cnw_disable(sc)
    463 	struct cnw_softc *sc;
    464 {
    465 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    466 
    467 	if ((ifp->if_flags & IFF_RUNNING) == 0)
    468 		return;
    469 
    470 	pcmcia_function_disable(sc->sc_pf);
    471 	sc->sc_resource &= ~CNW_RES_PCIC;
    472 	pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    473 	ifp->if_flags &= ~IFF_RUNNING;
    474 	ifp->if_timer = 0;
    475 }
    476 
    477 
    478 /*
    479  * Match the hardware we handle.
    480  */
    481 int
    482 cnw_match(parent, match, aux)
    483 	struct device *parent;
    484 	struct cfdata *match;
    485 	void *aux;
    486 {
    487 	struct pcmcia_attach_args *pa = aux;
    488 
    489 	if (pa->manufacturer == PCMCIA_VENDOR_XIRCOM &&
    490 	    pa->product == PCMCIA_PRODUCT_XIRCOM_CNW_801)
    491 		return 1;
    492 	if (pa->manufacturer == PCMCIA_VENDOR_XIRCOM &&
    493 	    pa->product == PCMCIA_PRODUCT_XIRCOM_CNW_802)
    494 		return 1;
    495 	return 0;
    496 }
    497 
    498 
    499 /*
    500  * Attach the card.
    501  */
    502 void
    503 cnw_attach(parent, self, aux)
    504 	struct device  *parent, *self;
    505 	void           *aux;
    506 {
    507 	struct cnw_softc *sc = (void *) self;
    508 	struct pcmcia_attach_args *pa = aux;
    509 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    510 	u_int8_t macaddr[ETHER_ADDR_LEN];
    511 	int i;
    512 	bus_size_t memsize;
    513 
    514 	sc->sc_resource = 0;
    515 
    516 	/* Enable the card */
    517 	sc->sc_pf = pa->pf;
    518 	pcmcia_function_init(sc->sc_pf, sc->sc_pf->cfe_head.sqh_first);
    519 	if (pcmcia_function_enable(sc->sc_pf)) {
    520 		printf(": function enable failed\n");
    521 		return;
    522 	}
    523 	sc->sc_resource |= CNW_RES_PCIC;
    524 
    525 	/* Map I/O register and "memory" */
    526 #ifndef MEMORY_MAPPED
    527 	if (pcmcia_io_alloc(sc->sc_pf, 0, CNW_IO_SIZE, CNW_IO_SIZE,
    528 	    &sc->sc_pcioh) != 0) {
    529 		printf(": can't allocate i/o space\n");
    530 		goto fail;
    531 	}
    532 	if (pcmcia_io_map(sc->sc_pf, PCMCIA_WIDTH_IO16, 0,
    533 	    CNW_IO_SIZE, &sc->sc_pcioh, &sc->sc_iowin) != 0) {
    534 		printf(": can't map i/o space\n");
    535 		pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
    536 		goto fail;
    537 	}
    538 	sc->sc_iot = sc->sc_pcioh.iot;
    539 	sc->sc_ioh = sc->sc_pcioh.ioh;
    540 	sc->sc_resource |= CNW_RES_IO;
    541 #endif
    542 #ifndef MEMORY_MAPPED
    543 	memsize = CNW_MEM_SIZE;
    544 #else
    545 	memsize = CNW_MEM_SIZE + CNW_IOM_SIZE;
    546 #endif
    547 	if (pcmcia_mem_alloc(sc->sc_pf, memsize, &sc->sc_pcmemh) != 0) {
    548 		printf(": can't allocate memory\n");
    549 		goto fail;
    550 	}
    551 	if (pcmcia_mem_map(sc->sc_pf, PCMCIA_WIDTH_MEM8|PCMCIA_MEM_COMMON,
    552 	    CNW_MEM_ADDR, memsize, &sc->sc_pcmemh, &sc->sc_memoff,
    553 	    &sc->sc_memwin) != 0) {
    554 		printf(": can't map memory\n");
    555 		pcmcia_mem_free(sc->sc_pf, &sc->sc_pcmemh);
    556 		goto fail;
    557 	}
    558 	sc->sc_memt = sc->sc_pcmemh.memt;
    559 	sc->sc_memh = sc->sc_pcmemh.memh;
    560 	sc->sc_resource |= CNW_RES_MEM;
    561 	switch (pa->product) {
    562 	case PCMCIA_PRODUCT_XIRCOM_CNW_801:
    563 		printf(": %s\n", PCMCIA_STR_XIRCOM_CNW_801);
    564 		break;
    565 	case PCMCIA_PRODUCT_XIRCOM_CNW_802:
    566 		printf(": %s\n", PCMCIA_STR_XIRCOM_CNW_802);
    567 		break;
    568 	}
    569 
    570 	/* Finish setup of softc */
    571 	sc->sc_domain = cnw_domain;
    572 	sc->sc_skey = cnw_skey;
    573 
    574 	/* Get MAC address */
    575 	cnw_reset(sc);
    576 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    577 		macaddr[i] = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    578 		    sc->sc_memoff + CNW_EREG_PA + i);
    579 	printf("%s: address %s\n", sc->sc_dev.dv_xname,
    580 	    ether_sprintf(macaddr));
    581 
    582 	/* Set up ifnet structure */
    583 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    584 	ifp->if_softc = sc;
    585 	ifp->if_start = cnw_start;
    586 	ifp->if_ioctl = cnw_ioctl;
    587 	ifp->if_watchdog = cnw_watchdog;
    588 	ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX |
    589 	    IFF_NOTRAILERS;
    590 
    591 	/* Attach the interface */
    592 	if_attach(ifp);
    593 	ether_ifattach(ifp, macaddr);
    594 #if NBPFILTER > 0
    595 	bpfattach(&sc->sc_ethercom.ec_if.if_bpf, ifp, DLT_EN10MB,
    596 	    sizeof(struct ether_header));
    597 #endif
    598 	sc->sc_resource |= CNW_RES_NET;
    599 
    600 	ifp->if_baudrate = IF_Mbps(1);
    601 
    602 	/* Disable the card now, and turn it on when the interface goes up */
    603 	pcmcia_function_disable(sc->sc_pf);
    604 	sc->sc_resource &= ~CNW_RES_PCIC;
    605 	return;
    606 
    607 fail:
    608 #ifndef MEMORY_MAPPED
    609 	if ((sc->sc_resource & CNW_RES_IO) != 0) {
    610 		pcmcia_io_unmap(sc->sc_pf, sc->sc_iowin);
    611 		pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
    612 		sc->sc_resource &= ~CNW_RES_IO;
    613 	}
    614 #endif
    615 	if ((sc->sc_resource & CNW_RES_PCIC) != 0) {
    616 		pcmcia_function_disable(sc->sc_pf);
    617 		sc->sc_resource &= ~CNW_RES_PCIC;
    618 	}
    619 }
    620 
    621 /*
    622  * Start outputting on the interface.
    623  */
    624 void
    625 cnw_start(ifp)
    626 	struct ifnet *ifp;
    627 {
    628 	struct cnw_softc *sc = ifp->if_softc;
    629 	struct mbuf *m0;
    630 	int lif;
    631 	int asr;
    632 #ifdef ONE_AT_A_TIME
    633 	struct timeval now;
    634 #endif
    635 
    636 #ifdef CNW_DEBUG
    637 	if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    638 		printf("%s: cnw_start\n", ifp->if_xname);
    639 	if (ifp->if_flags & IFF_OACTIVE)
    640 		printf("%s: cnw_start reentered\n", ifp->if_xname);
    641 #endif
    642 
    643 	ifp->if_flags |= IFF_OACTIVE;
    644 
    645 	for (;;) {
    646 #ifdef ONE_AT_A_TIME
    647 		microtime(&now);
    648 		now.tv_sec -= sc->sc_txlast.tv_sec;
    649 		now.tv_usec -= sc->sc_txlast.tv_usec;
    650 		if (now.tv_usec < 0) {
    651 			now.tv_usec += 1000000;
    652 			now.tv_sec--;
    653 		}
    654 
    655 		/*
    656 		 * Don't ship this packet out until the last
    657 		 * packet has left the building.
    658 		 * If we have not tried to send a packet for 1/5
    659 		 * a second then we assume we lost an interrupt,
    660 		 * lets go on and send the next packet anyhow.
    661 		 *
    662 		 * I suppose we could check to see if it is okay
    663 		 * to put additional packets on the card (beyond
    664 		 * the one already waiting to be sent) but I don't
    665 		 * think we would get any improvement in speed as
    666 		 * we should have ample time to put the next packet
    667 		 * on while this one is going out.
    668 		 */
    669 		if (sc->sc_active && now.tv_sec == 0 && now.tv_usec < 200000)
    670 			break;
    671 #endif
    672 
    673 		/* Make sure the link integrity field is on */
    674 		WAIT_WOC(sc);
    675 		lif = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    676 		    sc->sc_memoff + CNW_EREG_LIF);
    677 		if (lif == 0) {
    678 #ifdef CNW_DEBUG
    679 			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    680 				printf("%s: link integrity %d\n", lif);
    681 #endif
    682 			break;
    683 		}
    684 
    685 		/* Is there any buffer space available on the card? */
    686 		WAIT_WOC(sc);
    687 #ifndef MEMORY_MAPPED
    688 		asr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, CNW_REG_ASR);
    689 #else
    690 		asr = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    691 		    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_ASR);
    692 #endif
    693 		if (!(asr & CNW_ASR_TXBA)) {
    694 #ifdef CNW_DEBUG
    695 			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    696 				printf("%s: no buffer space\n", ifp->if_xname);
    697 #endif
    698 			break;
    699 		}
    700 
    701 		sc->sc_stats.nws_tx++;
    702 
    703 		IF_DEQUEUE(&ifp->if_snd, m0);
    704 		if (m0 == 0)
    705 			break;
    706 
    707 #if NBPFILTER > 0
    708 		if (ifp->if_bpf)
    709 			bpf_mtap(ifp->if_bpf, m0);
    710 #endif
    711 
    712 		cnw_transmit(sc, m0);
    713 		++ifp->if_opackets;
    714 		ifp->if_timer = 3; /* start watchdog timer */
    715 
    716 		microtime(&sc->sc_txlast);
    717 		sc->sc_active = 1;
    718 	}
    719 
    720 	ifp->if_flags &= ~IFF_OACTIVE;
    721 }
    722 
    723 /*
    724  * Transmit a packet.
    725  */
    726 void
    727 cnw_transmit(sc, m0)
    728 	struct cnw_softc *sc;
    729 	struct mbuf *m0;
    730 {
    731 	int buffer, bufsize, bufoffset, bufptr, bufspace, len, mbytes, n;
    732 	struct mbuf *m;
    733 	u_int8_t *mptr;
    734 
    735 	/* Get buffer info from card */
    736 	buffer = read16(sc, CNW_EREG_TDP);
    737 	bufsize = read16(sc, CNW_EREG_TDP + 2);
    738 	bufoffset = read16(sc, CNW_EREG_TDP + 4);
    739 #ifdef CNW_DEBUG
    740 	if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    741 		printf("%s: cnw_transmit b=0x%x s=%d o=0x%x\n",
    742 		    sc->sc_dev.dv_xname, buffer, bufsize, bufoffset);
    743 #endif
    744 
    745 	/* Copy data from mbuf chain to card buffers */
    746 	bufptr = sc->sc_memoff + buffer + bufoffset;
    747 	bufspace = bufsize;
    748 	len = 0;
    749 	for (m = m0; m; ) {
    750 		mptr = mtod(m, u_int8_t *);
    751 		mbytes = m->m_len;
    752 		len += mbytes;
    753 		while (mbytes > 0) {
    754 			if (bufspace == 0) {
    755 				buffer = read16(sc, buffer);
    756 				bufptr = sc->sc_memoff + buffer + bufoffset;
    757 				bufspace = bufsize;
    758 #ifdef CNW_DEBUG
    759 				if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    760 					printf("%s:   next buffer @0x%x\n",
    761 					    sc->sc_dev.dv_xname, buffer);
    762 #endif
    763 			}
    764 			n = mbytes <= bufspace ? mbytes : bufspace;
    765 			bus_space_write_region_1(sc->sc_memt, sc->sc_memh,
    766 			    bufptr, mptr, n);
    767 			bufptr += n;
    768 			bufspace -= n;
    769 			mptr += n;
    770 			mbytes -= n;
    771 		}
    772 		MFREE(m, m0);
    773 		m = m0;
    774 	}
    775 
    776 	/* Issue transmit command */
    777 	CNW_CMD2(sc, CNW_CMD_TL, len, len >> 8);
    778 }
    779 
    780 
    781 /*
    782  * Pull a packet from the card into an mbuf chain.
    783  */
    784 struct mbuf *
    785 cnw_read(sc)
    786 	struct cnw_softc *sc;
    787 {
    788 	struct mbuf *m, *top, **mp;
    789 	int totbytes, buffer, bufbytes, bufptr, mbytes, n;
    790 	u_int8_t *mptr;
    791 
    792 	WAIT_WOC(sc);
    793 	totbytes = read16(sc, CNW_EREG_RDP);
    794 #ifdef CNW_DEBUG
    795 	if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    796 		printf("%s: recv %d bytes\n", sc->sc_dev.dv_xname, totbytes);
    797 #endif
    798 	buffer = CNW_EREG_RDP + 2;
    799 	bufbytes = 0;
    800 	bufptr = 0; /* XXX make gcc happy */
    801 
    802 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    803 	if (m == 0)
    804 		return (0);
    805 	m->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
    806 	m->m_pkthdr.len = totbytes;
    807 	mbytes = MHLEN;
    808 	top = 0;
    809 	mp = &top;
    810 
    811 	while (totbytes > 0) {
    812 		if (top) {
    813 			MGET(m, M_DONTWAIT, MT_DATA);
    814 			if (m == 0) {
    815 				m_freem(top);
    816 				return (0);
    817 			}
    818 			mbytes = MLEN;
    819 		}
    820 		if (totbytes >= MINCLSIZE) {
    821 			MCLGET(m, M_DONTWAIT);
    822 			if ((m->m_flags & M_EXT) == 0) {
    823 				m_free(m);
    824 				m_freem(top);
    825 				return (0);
    826 			}
    827 			mbytes = MCLBYTES;
    828 		}
    829 		if (!top) {
    830 			int pad = ALIGN(sizeof(struct ether_header)) -
    831 			    sizeof(struct ether_header);
    832 			m->m_data += pad;
    833 			mbytes -= pad;
    834 		}
    835 		mptr = mtod(m, u_int8_t *);
    836 		mbytes = m->m_len = min(totbytes, mbytes);
    837 		totbytes -= mbytes;
    838 		while (mbytes > 0) {
    839 			if (bufbytes == 0) {
    840 				buffer = read16(sc, buffer);
    841 				bufbytes = read16(sc, buffer + 2);
    842 				bufptr = sc->sc_memoff + buffer +
    843 				    read16(sc, buffer + 4);
    844 #ifdef CNW_DEBUG
    845 				if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    846 					printf("%s:   %d bytes @0x%x+0x%x\n",
    847 					    sc->sc_dev.dv_xname, bufbytes,
    848 					    buffer, bufptr - buffer -
    849 					    sc->sc_memoff);
    850 #endif
    851 			}
    852 			n = mbytes <= bufbytes ? mbytes : bufbytes;
    853 			bus_space_read_region_1(sc->sc_memt, sc->sc_memh,
    854 			    bufptr, mptr, n);
    855 			bufbytes -= n;
    856 			bufptr += n;
    857 			mbytes -= n;
    858 			mptr += n;
    859 		}
    860 		*mp = m;
    861 		mp = &m->m_next;
    862 	}
    863 
    864 	return (top);
    865 }
    866 
    867 
    868 /*
    869  * Handle received packets.
    870  */
    871 void
    872 cnw_recv(sc)
    873 	struct cnw_softc *sc;
    874 {
    875 	int rser;
    876 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    877 	struct mbuf *m;
    878 	struct ether_header *eh;
    879 
    880 	for (;;) {
    881 		WAIT_WOC(sc);
    882 		rser = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    883 		    sc->sc_memoff + CNW_EREG_RSER);
    884 		if (!(rser & CNW_RSER_RXAVAIL))
    885 			return;
    886 
    887 		/* Pull packet off card */
    888 		m = cnw_read(sc);
    889 
    890 		/* Acknowledge packet */
    891 		CNW_CMD0(sc, CNW_CMD_SRP);
    892 
    893 		/* Did we manage to get the packet from the interface? */
    894 		if (m == 0) {
    895 			++ifp->if_ierrors;
    896 			return;
    897 		}
    898 		++ifp->if_ipackets;
    899 
    900 #if NBPFILTER > 0
    901 		if (ifp->if_bpf)
    902 			bpf_mtap(ifp->if_bpf, m);
    903 #endif
    904 
    905 		/*
    906 		 * Check that the packet is for us or {multi,broad}cast. Maybe
    907 		 * there's a fool-poof hardware check for this, but I don't
    908 		 * really know...
    909 		 */
    910 		eh = mtod(m, struct ether_header *);
    911 		if ((eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    912 		    bcmp(LLADDR(sc->sc_ethercom.ec_if.if_sadl),
    913 		    eh->ether_dhost, sizeof(eh->ether_dhost)) != 0) {
    914 			m_freem(m);
    915 			continue;
    916 		}
    917 
    918 		/* Pass the packet up. */
    919 		(*ifp->if_input)(ifp, m);
    920 	}
    921 }
    922 
    923 
    924 /*
    925  * Interrupt handler.
    926  */
    927 int
    928 cnw_intr(arg)
    929 	void *arg;
    930 {
    931 	struct cnw_softc *sc = arg;
    932 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    933 	int ret, status, rser, tser;
    934 
    935 	if ((sc->sc_ethercom.ec_if.if_flags & IFF_RUNNING) == 0 ||
    936 	    (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    937 		return (0);
    938 	ifp->if_timer = 0;	/* stop watchdog timer */
    939 
    940 	ret = 0;
    941 	for (;;) {
    942 		WAIT_WOC(sc);
    943 #ifndef MEMORY_MAPPED
    944 		status = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
    945 		    CNW_REG_CCSR);
    946 #else
    947 		status = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    948 		    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_CCSR);
    949 #endif
    950 		if (!(status & 0x02)) {
    951 			if (ret == 0)
    952 				printf("%s: spurious interrupt\n",
    953 				    sc->sc_dev.dv_xname);
    954 			return (ret);
    955 		}
    956 		ret = 1;
    957 #ifndef MEMORY_MAPPED
    958 		status = bus_space_read_1(sc->sc_iot, sc->sc_ioh, CNW_REG_ASR);
    959 #else
    960 		status = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    961 		    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_ASR);
    962 #endif
    963 
    964 		/* Anything to receive? */
    965 		if (status & CNW_ASR_RXRDY) {
    966 			sc->sc_stats.nws_rx++;
    967 			cnw_recv(sc);
    968 		}
    969 
    970 		/* Receive error */
    971 		if (status & CNW_ASR_RXERR) {
    972 			/*
    973 			 * I get a *lot* of spurious receive errors
    974 			 * (many per second), even when the interface
    975 			 * is quiescent, so we don't increment
    976 			 * if_ierrors here.
    977 			 */
    978 			rser = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    979 			    sc->sc_memoff + CNW_EREG_RSER);
    980 
    981 			/* RX statistics */
    982 			sc->sc_stats.nws_rxerr++;
    983 			if (rser & CNW_RSER_RXBIG)
    984 				sc->sc_stats.nws_rxframe++;
    985 			if (rser & CNW_RSER_RXCRC)
    986 				sc->sc_stats.nws_rxcrcerror++;
    987 			if (rser & CNW_RSER_RXOVERRUN)
    988 				sc->sc_stats.nws_rxoverrun++;
    989 			if (rser & CNW_RSER_RXOVERFLOW)
    990 				sc->sc_stats.nws_rxoverflow++;
    991 			if (rser & CNW_RSER_RXERR)
    992 				sc->sc_stats.nws_rxerrors++;
    993 			if (rser & CNW_RSER_RXAVAIL)
    994 				sc->sc_stats.nws_rxavail++;
    995 
    996 			/* Clear error bits in RSER */
    997 			WAIT_WOC(sc);
    998 			bus_space_write_1(sc->sc_memt, sc->sc_memh,
    999 			    sc->sc_memoff + CNW_EREG_RSERW,
   1000 			    CNW_RSER_RXERR |
   1001 			    (rser & (CNW_RSER_RXCRC | CNW_RSER_RXBIG)));
   1002 			/* Clear RXERR in ASR */
   1003 			WAIT_WOC(sc);
   1004 			bus_space_write_1(sc->sc_memt, sc->sc_memh,
   1005 			    sc->sc_memoff + CNW_EREG_ASCC, CNW_ASR_RXERR);
   1006 		}
   1007 
   1008 		/* Transmit done */
   1009 		if (status & CNW_ASR_TXDN) {
   1010 			tser = bus_space_read_1(sc->sc_memt, sc->sc_memh,
   1011 						CNW_EREG_TSER);
   1012 
   1013 			/* TX statistics */
   1014 			if (tser & CNW_TSER_TXERR)
   1015 				sc->sc_stats.nws_txerrors++;
   1016 			if (tser & CNW_TSER_TXNOAP)
   1017 				sc->sc_stats.nws_txlostcd++;
   1018 			if (tser & CNW_TSER_TXGU)
   1019 				sc->sc_stats.nws_txabort++;
   1020 
   1021 			if (tser & CNW_TSER_TXOK) {
   1022 				sc->sc_stats.nws_txokay++;
   1023 				sc->sc_stats.nws_txretries[status & 0xf]++;
   1024 				WAIT_WOC(sc);
   1025 				bus_space_write_1(sc->sc_memt, sc->sc_memh,
   1026 				    sc->sc_memoff + CNW_EREG_TSERW,
   1027 				    CNW_TSER_TXOK | CNW_TSER_RTRY);
   1028 			}
   1029 
   1030 			if (tser & CNW_TSER_ERROR) {
   1031 				++ifp->if_oerrors;
   1032 				WAIT_WOC(sc);
   1033 				bus_space_write_1(sc->sc_memt, sc->sc_memh,
   1034 				    sc->sc_memoff + CNW_EREG_TSERW,
   1035 				    (tser & CNW_TSER_ERROR) |
   1036 				    CNW_TSER_RTRY);
   1037 			}
   1038 
   1039 			sc->sc_active = 0;
   1040 			ifp->if_flags &= ~IFF_OACTIVE;
   1041 
   1042 			/* Continue to send packets from the queue */
   1043 			cnw_start(&sc->sc_ethercom.ec_if);
   1044 		}
   1045 
   1046 	}
   1047 }
   1048 
   1049 
   1050 /*
   1051  * Handle device ioctls.
   1052  */
   1053 int
   1054 cnw_ioctl(ifp, cmd, data)
   1055 	struct ifnet *ifp;
   1056 	u_long cmd;
   1057 	caddr_t data;
   1058 {
   1059 	struct cnw_softc *sc = ifp->if_softc;
   1060 	struct ifaddr *ifa = (struct ifaddr *)data;
   1061 	struct ifreq *ifr = (struct ifreq *)data;
   1062 	int s, error = 0;
   1063 	struct proc *p = curproc;	/*XXX*/
   1064 
   1065 	s = splnet();
   1066 
   1067 	switch (cmd) {
   1068 
   1069 	case SIOCSIFADDR:
   1070 		if (!(ifp->if_flags & IFF_RUNNING) &&
   1071 		    (error = cnw_enable(sc)) != 0)
   1072 			break;
   1073 		ifp->if_flags |= IFF_UP;
   1074 		switch (ifa->ifa_addr->sa_family) {
   1075 #ifdef INET
   1076 		case AF_INET:
   1077 			cnw_init(sc);
   1078 			arp_ifinit(&sc->sc_ethercom.ec_if, ifa);
   1079 			break;
   1080 #endif
   1081 		default:
   1082 			cnw_init(sc);
   1083 			break;
   1084 		}
   1085 		break;
   1086 
   1087 	case SIOCSIFFLAGS:
   1088 		if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == IFF_RUNNING) {
   1089 			/*
   1090 			 * The interface is marked down and it is running, so
   1091 			 * stop it.
   1092 			 */
   1093 			cnw_disable(sc);
   1094 		} else if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == IFF_UP){
   1095 			/*
   1096 			 * The interface is marked up and it is stopped, so
   1097 			 * start it.
   1098 			 */
   1099 			error = cnw_enable(sc);
   1100 		} else {
   1101 			/* IFF_PROMISC may be changed */
   1102 			cnw_init(sc);
   1103 		}
   1104 		break;
   1105 
   1106 	case SIOCADDMULTI:
   1107 	case SIOCDELMULTI:
   1108 		/* Update our multicast list. */
   1109 		error = (cmd == SIOCADDMULTI) ?
   1110 		    ether_addmulti(ifr, &sc->sc_ethercom) :
   1111 		    ether_delmulti(ifr, &sc->sc_ethercom);
   1112 		if (error == ENETRESET || error == 0) {
   1113 			cnw_init(sc);
   1114 			error = 0;
   1115 		}
   1116 		break;
   1117 
   1118 	case SIOCGCNWDOMAIN:
   1119 		((struct ifreq *)data)->ifr_domain = sc->sc_domain;
   1120 		break;
   1121 
   1122 	case SIOCSCNWDOMAIN:
   1123 		error = suser(p->p_ucred, &p->p_acflag);
   1124 		if (error)
   1125 			break;
   1126 		error = cnw_setdomain(sc, ifr->ifr_domain);
   1127 		break;
   1128 
   1129 	case SIOCSCNWKEY:
   1130 		error = suser(p->p_ucred, &p->p_acflag);
   1131 		if (error)
   1132 			break;
   1133 		error = cnw_setkey(sc, ifr->ifr_key);
   1134 		break;
   1135 
   1136 	case SIOCGCNWSTATUS:
   1137 		error = suser(p->p_ucred, &p->p_acflag);
   1138 		if (error)
   1139 			break;
   1140 		if ((ifp->if_flags & IFF_RUNNING) == 0)
   1141 			break;
   1142 		bus_space_read_region_1(sc->sc_memt, sc->sc_memh,
   1143 		    sc->sc_memoff + CNW_EREG_CB,
   1144 		    ((struct cnwstatus *)data)->data,
   1145 		    sizeof(((struct cnwstatus *)data)->data));
   1146 		break;
   1147 
   1148 	case SIOCGCNWSTATS:
   1149 		bcopy((void *)&sc->sc_stats,
   1150 		    (void *)&(((struct cnwistats *)data)->stats),
   1151 		    sizeof(struct cnwstats));
   1152 			break;
   1153 
   1154 	default:
   1155 		error = EINVAL;
   1156 		break;
   1157 	}
   1158 
   1159 	splx(s);
   1160 	return (error);
   1161 }
   1162 
   1163 
   1164 /*
   1165  * Device timeout/watchdog routine. Entered if the device neglects to
   1166  * generate an interrupt after a transmit has been started on it.
   1167  */
   1168 void
   1169 cnw_watchdog(ifp)
   1170 	struct ifnet *ifp;
   1171 {
   1172 	struct cnw_softc *sc = ifp->if_softc;
   1173 
   1174 	printf("%s: device timeout; card reset\n", sc->sc_dev.dv_xname);
   1175 	++ifp->if_oerrors;
   1176 	cnw_init(sc);
   1177 }
   1178 
   1179 int
   1180 cnw_setdomain(sc, domain)
   1181 	struct cnw_softc *sc;
   1182 	int domain;
   1183 {
   1184 	int s;
   1185 
   1186 	if (domain & ~0x1ff)
   1187 		return EINVAL;
   1188 
   1189 	s = splnet();
   1190 	CNW_CMD2(sc, CNW_CMD_SMD, domain, domain >> 8);
   1191 	splx(s);
   1192 
   1193 	sc->sc_domain = domain;
   1194 	return 0;
   1195 }
   1196 
   1197 int
   1198 cnw_setkey(sc, key)
   1199 	struct cnw_softc *sc;
   1200 	int key;
   1201 {
   1202 	int s;
   1203 
   1204 	if (key & ~0xffff)
   1205 		return EINVAL;
   1206 
   1207 	s = splnet();
   1208 	CNW_CMD2(sc, CNW_CMD_SSK, key, key >> 8);
   1209 	splx(s);
   1210 
   1211 	sc->sc_skey = key;
   1212 	return 0;
   1213 }
   1214 
   1215 int
   1216 cnw_activate(self, act)
   1217 	struct device *self;
   1218 	enum devact act;
   1219 {
   1220 	struct cnw_softc *sc = (struct cnw_softc *)self;
   1221 	int rv = 0, s;
   1222 
   1223 	s = splnet();
   1224 	switch (act) {
   1225 	case DVACT_ACTIVATE:
   1226 		rv = EOPNOTSUPP;
   1227 		break;
   1228 
   1229 	case DVACT_DEACTIVATE:
   1230 		if_deactivate(&sc->sc_ethercom.ec_if);
   1231 		break;
   1232 	}
   1233 	splx(s);
   1234 	return (rv);
   1235 }
   1236 
   1237 int
   1238 cnw_detach(self, flags)
   1239 	struct device *self;
   1240 	int flags;
   1241 {
   1242 	struct cnw_softc *sc = (struct cnw_softc *)self;
   1243 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1244 
   1245 	/* cnw_disable() checks IFF_RUNNING */
   1246 	cnw_disable(sc);
   1247 
   1248 	if ((sc->sc_resource & CNW_RES_NET) != 0) {
   1249 #if NBPFILTER > 0
   1250 		bpfdetach(ifp);
   1251 #endif
   1252 		ether_ifdetach(ifp);
   1253 		if_detach(ifp);
   1254 	}
   1255 
   1256 #ifndef MEMORY_MAPPED
   1257 	/* unmap and free our i/o windows */
   1258 	if ((sc->sc_resource & CNW_RES_IO) != 0) {
   1259 		pcmcia_io_unmap(sc->sc_pf, sc->sc_iowin);
   1260 		pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
   1261 	}
   1262 #endif
   1263 
   1264 	/* unmap and free our memory windows */
   1265 	if ((sc->sc_resource & CNW_RES_MEM) != 0) {
   1266 		pcmcia_mem_unmap(sc->sc_pf, sc->sc_memwin);
   1267 		pcmcia_mem_free(sc->sc_pf, &sc->sc_pcmemh);
   1268 	}
   1269 
   1270 	return (0);
   1271 }
   1272