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