if_cnw.c revision 1.47       1 /*	$NetBSD: if_cnw.c,v 1.47 2009/03/14 15:36:20 dsl 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.47 2009/03/14 15:36:20 dsl 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(sc, cmd, count, arg1, arg2)
    295 	struct cnw_softc *sc;
    296 	int cmd, count, arg1, arg2;
    297 {
    298 	int ptr = sc->sc_memoff + CNW_EREG_CB;
    299 
    300 	if (wait_WOC(sc, 0)) {
    301 		printf("%s: wedged when issuing cmd 0x%x\n",
    302 		    device_xname(&sc->sc_dev), cmd);
    303 		/*
    304 		 * We'll continue anyway, as that's probably the best
    305 		 * thing we can do; at least the user knows there's a
    306 		 * problem, and can reset the interface with ifconfig
    307 		 * down/up.
    308 		 */
    309 	}
    310 
    311 	bus_space_write_1(sc->sc_memt, sc->sc_memh, ptr, cmd);
    312 	if (count > 0) {
    313 		bus_space_write_1(sc->sc_memt, sc->sc_memh, ptr + 1, arg1);
    314 		if (count > 1)
    315 			bus_space_write_1(sc->sc_memt, sc->sc_memh,
    316 			    ptr + 2, arg2);
    317 	}
    318 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
    319 	    ptr + count + 1, CNW_CMD_EOC);
    320 	return (0);
    321 }
    322 #define CNW_CMD0(sc, cmd) \
    323     do { cnw_cmd(sc, cmd, 0, 0, 0); } while (0)
    324 #define CNW_CMD1(sc, cmd, arg1)	\
    325     do { cnw_cmd(sc, cmd, 1, arg1 , 0); } while (0)
    326 #define CNW_CMD2(sc, cmd, arg1, arg2) \
    327     do { cnw_cmd(sc, cmd, 2, arg1, arg2); } while (0)
    328 
    329 /* ---------------------------------------------------------------- */
    330 
    331 /*
    332  * Reset the hardware.
    333  */
    334 void
    335 cnw_reset(struct cnw_softc *sc)
    336 {
    337 #ifdef CNW_DEBUG
    338 	if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    339 		printf("%s: resetting\n", device_xname(&sc->sc_dev));
    340 #endif
    341 	wait_WOC(sc, 0);
    342 #ifndef MEMORY_MAPPED
    343 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, CNW_REG_PMR, CNW_PMR_RESET);
    344 #else
    345 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
    346 	    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_PMR, CNW_PMR_RESET);
    347 #endif
    348 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
    349 	    sc->sc_memoff + CNW_EREG_ASCC, CNW_ASR_WOC);
    350 #ifndef MEMORY_MAPPED
    351 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, CNW_REG_PMR, 0);
    352 #else
    353 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
    354 	    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_PMR, 0);
    355 #endif
    356 }
    357 
    358 
    359 /*
    360  * Initialize the card.
    361  */
    362 void
    363 cnw_init(struct cnw_softc *sc)
    364 {
    365 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    366 	const u_int8_t rxmode =
    367 	    CNW_RXCONF_RXENA | CNW_RXCONF_BCAST | CNW_RXCONF_AMP;
    368 
    369 	/* Reset the card */
    370 	cnw_reset(sc);
    371 
    372 	/* Issue a NOP to check the card */
    373 	CNW_CMD0(sc, CNW_CMD_NOP);
    374 
    375 	/* Set up receive configuration */
    376 	CNW_CMD1(sc, CNW_CMD_SRC,
    377 	    rxmode | ((ifp->if_flags & IFF_PROMISC) ? CNW_RXCONF_PRO : 0));
    378 
    379 	/* Set up transmit configuration */
    380 	CNW_CMD1(sc, CNW_CMD_STC, CNW_TXCONF_TXENA);
    381 
    382 	/* Set domain */
    383 	CNW_CMD2(sc, CNW_CMD_SMD, sc->sc_domain, sc->sc_domain >> 8);
    384 
    385 	/* Set scramble key */
    386 	CNW_CMD2(sc, CNW_CMD_SSK, sc->sc_skey, sc->sc_skey >> 8);
    387 
    388 	/* Enable interrupts */
    389 	WAIT_WOC(sc);
    390 #ifndef MEMORY_MAPPED
    391 	bus_space_write_1(sc->sc_iot, sc->sc_ioh,
    392 	    CNW_REG_IMR, CNW_IMR_IENA | CNW_IMR_RFU1);
    393 #else
    394 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
    395 	    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_IMR,
    396 	    CNW_IMR_IENA | CNW_IMR_RFU1);
    397 #endif
    398 
    399 	/* Enable receiver */
    400 	CNW_CMD0(sc, CNW_CMD_ER);
    401 
    402 	/* "Set the IENA bit in COR" */
    403 	WAIT_WOC(sc);
    404 #ifndef MEMORY_MAPPED
    405 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, CNW_REG_COR,
    406 	    CNW_COR_IENA | CNW_COR_LVLREQ);
    407 #else
    408 	bus_space_write_1(sc->sc_memt, sc->sc_memh,
    409 	    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_COR,
    410 	    CNW_COR_IENA | CNW_COR_LVLREQ);
    411 #endif
    412 }
    413 
    414 
    415 /*
    416  * Enable and initialize the card.
    417  */
    418 int
    419 cnw_enable(struct cnw_softc *sc)
    420 {
    421 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    422 
    423 	if ((ifp->if_flags & IFF_RUNNING) != 0)
    424 		return (0);
    425 
    426 	sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_NET, cnw_intr, sc);
    427 	if (sc->sc_ih == NULL) {
    428 		aprint_error_dev(&sc->sc_dev, "couldn't establish interrupt handler\n");
    429 		return (EIO);
    430 	}
    431 	if (pcmcia_function_enable(sc->sc_pf) != 0) {
    432 		aprint_error_dev(&sc->sc_dev, "couldn't enable card\n");
    433 		return (EIO);
    434 	}
    435 	sc->sc_resource |= CNW_RES_PCIC;
    436 	cnw_init(sc);
    437 	ifp->if_flags &= ~IFF_OACTIVE;
    438 	ifp->if_flags |= IFF_RUNNING;
    439 	return (0);
    440 }
    441 
    442 
    443 /*
    444  * Stop and disable the card.
    445  */
    446 void
    447 cnw_disable(struct cnw_softc *sc)
    448 {
    449 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    450 
    451 	if ((ifp->if_flags & IFF_RUNNING) == 0)
    452 		return;
    453 
    454 	pcmcia_function_disable(sc->sc_pf);
    455 	sc->sc_resource &= ~CNW_RES_PCIC;
    456 	pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
    457 	ifp->if_flags &= ~IFF_RUNNING;
    458 	ifp->if_timer = 0;
    459 }
    460 
    461 
    462 /*
    463  * Match the hardware we handle.
    464  */
    465 int
    466 cnw_match(struct device *parent, struct cfdata *match,
    467     void *aux)
    468 {
    469 	struct pcmcia_attach_args *pa = aux;
    470 
    471 	if (pa->manufacturer == PCMCIA_VENDOR_XIRCOM &&
    472 	    pa->product == PCMCIA_PRODUCT_XIRCOM_CNW_801)
    473 		return 1;
    474 	if (pa->manufacturer == PCMCIA_VENDOR_XIRCOM &&
    475 	    pa->product == PCMCIA_PRODUCT_XIRCOM_CNW_802)
    476 		return 1;
    477 	return 0;
    478 }
    479 
    480 
    481 /*
    482  * Attach the card.
    483  */
    484 void
    485 cnw_attach(struct device  *parent, struct device *self, void *aux)
    486 {
    487 	struct cnw_softc *sc = (void *) self;
    488 	struct pcmcia_attach_args *pa = aux;
    489 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    490 	u_int8_t macaddr[ETHER_ADDR_LEN];
    491 	int i;
    492 	bus_size_t memsize;
    493 
    494 	sc->sc_resource = 0;
    495 
    496 	/* Enable the card */
    497 	sc->sc_pf = pa->pf;
    498 	pcmcia_function_init(sc->sc_pf, SIMPLEQ_FIRST(&sc->sc_pf->cfe_head));
    499 	if (pcmcia_function_enable(sc->sc_pf)) {
    500 		aprint_error_dev(self, "function enable failed\n");
    501 		return;
    502 	}
    503 	sc->sc_resource |= CNW_RES_PCIC;
    504 
    505 	/* Map I/O register and "memory" */
    506 #ifndef MEMORY_MAPPED
    507 	if (pcmcia_io_alloc(sc->sc_pf, 0, CNW_IO_SIZE, CNW_IO_SIZE,
    508 	    &sc->sc_pcioh) != 0) {
    509 		aprint_error_dev(self, "can't allocate i/o space\n");
    510 		goto fail;
    511 	}
    512 	if (pcmcia_io_map(sc->sc_pf, PCMCIA_WIDTH_IO16, &sc->sc_pcioh,
    513 	    &sc->sc_iowin) != 0) {
    514 		aprint_error_dev(self, "can't map i/o space\n");
    515 		pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
    516 		goto fail;
    517 	}
    518 	sc->sc_iot = sc->sc_pcioh.iot;
    519 	sc->sc_ioh = sc->sc_pcioh.ioh;
    520 	sc->sc_resource |= CNW_RES_IO;
    521 #endif
    522 #ifndef MEMORY_MAPPED
    523 	memsize = CNW_MEM_SIZE;
    524 #else
    525 	memsize = CNW_MEM_SIZE + CNW_IOM_SIZE;
    526 #endif
    527 	if (pcmcia_mem_alloc(sc->sc_pf, memsize, &sc->sc_pcmemh) != 0) {
    528 		aprint_error_dev(self, "can't allocate memory\n");
    529 		goto fail;
    530 	}
    531 	if (pcmcia_mem_map(sc->sc_pf, PCMCIA_WIDTH_MEM8|PCMCIA_MEM_COMMON,
    532 	    CNW_MEM_ADDR, memsize, &sc->sc_pcmemh, &sc->sc_memoff,
    533 	    &sc->sc_memwin) != 0) {
    534 		aprint_error_dev(self, "can't map memory\n");
    535 		pcmcia_mem_free(sc->sc_pf, &sc->sc_pcmemh);
    536 		goto fail;
    537 	}
    538 	sc->sc_memt = sc->sc_pcmemh.memt;
    539 	sc->sc_memh = sc->sc_pcmemh.memh;
    540 	sc->sc_resource |= CNW_RES_MEM;
    541 
    542 	/* Finish setup of softc */
    543 	sc->sc_domain = cnw_domain;
    544 	sc->sc_skey = cnw_skey;
    545 
    546 	/* Get MAC address */
    547 	cnw_reset(sc);
    548 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    549 		macaddr[i] = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    550 		    sc->sc_memoff + CNW_EREG_PA + i);
    551 	printf("%s: address %s\n", device_xname(&sc->sc_dev),
    552 	    ether_sprintf(macaddr));
    553 
    554 	/* Set up ifnet structure */
    555 	strlcpy(ifp->if_xname, device_xname(&sc->sc_dev), IFNAMSIZ);
    556 	ifp->if_softc = sc;
    557 	ifp->if_start = cnw_start;
    558 	ifp->if_ioctl = cnw_ioctl;
    559 	ifp->if_watchdog = cnw_watchdog;
    560 	ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX |
    561 	    IFF_NOTRAILERS;
    562 	IFQ_SET_READY(&ifp->if_snd);
    563 
    564 	/* Attach the interface */
    565 	if_attach(ifp);
    566 	ether_ifattach(ifp, macaddr);
    567 
    568 	sc->sc_resource |= CNW_RES_NET;
    569 
    570 	ifp->if_baudrate = IF_Mbps(1);
    571 
    572 	/* Disable the card now, and turn it on when the interface goes up */
    573 	pcmcia_function_disable(sc->sc_pf);
    574 	sc->sc_resource &= ~CNW_RES_PCIC;
    575 	return;
    576 
    577 fail:
    578 #ifndef MEMORY_MAPPED
    579 	if ((sc->sc_resource & CNW_RES_IO) != 0) {
    580 		pcmcia_io_unmap(sc->sc_pf, sc->sc_iowin);
    581 		pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
    582 		sc->sc_resource &= ~CNW_RES_IO;
    583 	}
    584 #endif
    585 	if ((sc->sc_resource & CNW_RES_PCIC) != 0) {
    586 		pcmcia_function_disable(sc->sc_pf);
    587 		sc->sc_resource &= ~CNW_RES_PCIC;
    588 	}
    589 }
    590 
    591 /*
    592  * Start outputting on the interface.
    593  */
    594 void
    595 cnw_start(struct ifnet *ifp)
    596 {
    597 	struct cnw_softc *sc = ifp->if_softc;
    598 	struct mbuf *m0;
    599 	int lif;
    600 	int asr;
    601 #ifdef ONE_AT_A_TIME
    602 	struct timeval now;
    603 #endif
    604 
    605 #ifdef CNW_DEBUG
    606 	if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    607 		printf("%s: cnw_start\n", ifp->if_xname);
    608 	if (ifp->if_flags & IFF_OACTIVE)
    609 		printf("%s: cnw_start reentered\n", ifp->if_xname);
    610 #endif
    611 
    612 	ifp->if_flags |= IFF_OACTIVE;
    613 
    614 	for (;;) {
    615 #ifdef ONE_AT_A_TIME
    616 		microtime(&now);
    617 		now.tv_sec -= sc->sc_txlast.tv_sec;
    618 		now.tv_usec -= sc->sc_txlast.tv_usec;
    619 		if (now.tv_usec < 0) {
    620 			now.tv_usec += 1000000;
    621 			now.tv_sec--;
    622 		}
    623 
    624 		/*
    625 		 * Don't ship this packet out until the last
    626 		 * packet has left the building.
    627 		 * If we have not tried to send a packet for 1/5
    628 		 * a second then we assume we lost an interrupt,
    629 		 * lets go on and send the next packet anyhow.
    630 		 *
    631 		 * I suppose we could check to see if it is okay
    632 		 * to put additional packets on the card (beyond
    633 		 * the one already waiting to be sent) but I don't
    634 		 * think we would get any improvement in speed as
    635 		 * we should have ample time to put the next packet
    636 		 * on while this one is going out.
    637 		 */
    638 		if (sc->sc_active && now.tv_sec == 0 && now.tv_usec < 200000)
    639 			break;
    640 #endif
    641 
    642 		/* Make sure the link integrity field is on */
    643 		WAIT_WOC(sc);
    644 		lif = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    645 		    sc->sc_memoff + CNW_EREG_LIF);
    646 		if (lif == 0) {
    647 #ifdef CNW_DEBUG
    648 			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    649 				printf("%s: link integrity %d\n", ifp->if_xname, lif);
    650 #endif
    651 			break;
    652 		}
    653 
    654 		/* Is there any buffer space available on the card? */
    655 		WAIT_WOC(sc);
    656 #ifndef MEMORY_MAPPED
    657 		asr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, CNW_REG_ASR);
    658 #else
    659 		asr = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    660 		    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_ASR);
    661 #endif
    662 		if (!(asr & CNW_ASR_TXBA)) {
    663 #ifdef CNW_DEBUG
    664 			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    665 				printf("%s: no buffer space\n", ifp->if_xname);
    666 #endif
    667 			break;
    668 		}
    669 
    670 		sc->sc_stats.nws_tx++;
    671 
    672 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    673 		if (m0 == 0)
    674 			break;
    675 
    676 #if NBPFILTER > 0
    677 		if (ifp->if_bpf)
    678 			bpf_mtap(ifp->if_bpf, m0);
    679 #endif
    680 
    681 		cnw_transmit(sc, m0);
    682 		++ifp->if_opackets;
    683 		ifp->if_timer = 3; /* start watchdog timer */
    684 
    685 		microtime(&sc->sc_txlast);
    686 		sc->sc_active = 1;
    687 	}
    688 
    689 	ifp->if_flags &= ~IFF_OACTIVE;
    690 }
    691 
    692 /*
    693  * Transmit a packet.
    694  */
    695 void
    696 cnw_transmit(struct cnw_softc *sc, struct mbuf *m0)
    697 {
    698 	int buffer, bufsize, bufoffset, bufptr, bufspace, len, mbytes, n;
    699 	struct mbuf *m;
    700 	u_int8_t *mptr;
    701 
    702 	/* Get buffer info from card */
    703 	buffer = read16(sc, CNW_EREG_TDP);
    704 	bufsize = read16(sc, CNW_EREG_TDP + 2);
    705 	bufoffset = read16(sc, CNW_EREG_TDP + 4);
    706 #ifdef CNW_DEBUG
    707 	if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    708 		printf("%s: cnw_transmit b=0x%x s=%d o=0x%x\n",
    709 		    device_xname(&sc->sc_dev), buffer, bufsize, bufoffset);
    710 #endif
    711 
    712 	/* Copy data from mbuf chain to card buffers */
    713 	bufptr = sc->sc_memoff + buffer + bufoffset;
    714 	bufspace = bufsize;
    715 	len = 0;
    716 	for (m = m0; m; ) {
    717 		mptr = mtod(m, u_int8_t *);
    718 		mbytes = m->m_len;
    719 		len += mbytes;
    720 		while (mbytes > 0) {
    721 			if (bufspace == 0) {
    722 				buffer = read16(sc, buffer);
    723 				bufptr = sc->sc_memoff + buffer + bufoffset;
    724 				bufspace = bufsize;
    725 #ifdef CNW_DEBUG
    726 				if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    727 					printf("%s:   next buffer @0x%x\n",
    728 					    device_xname(&sc->sc_dev), buffer);
    729 #endif
    730 			}
    731 			n = mbytes <= bufspace ? mbytes : bufspace;
    732 			bus_space_write_region_1(sc->sc_memt, sc->sc_memh,
    733 			    bufptr, mptr, n);
    734 			bufptr += n;
    735 			bufspace -= n;
    736 			mptr += n;
    737 			mbytes -= n;
    738 		}
    739 		MFREE(m, m0);
    740 		m = m0;
    741 	}
    742 
    743 	/* Issue transmit command */
    744 	CNW_CMD2(sc, CNW_CMD_TL, len, len >> 8);
    745 }
    746 
    747 
    748 /*
    749  * Pull a packet from the card into an mbuf chain.
    750  */
    751 struct mbuf *
    752 cnw_read(struct cnw_softc *sc)
    753 {
    754 	struct mbuf *m, *top, **mp;
    755 	int totbytes, buffer, bufbytes, bufptr, mbytes, n;
    756 	u_int8_t *mptr;
    757 
    758 	WAIT_WOC(sc);
    759 	totbytes = read16(sc, CNW_EREG_RDP);
    760 #ifdef CNW_DEBUG
    761 	if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    762 		printf("%s: recv %d bytes\n", device_xname(&sc->sc_dev), totbytes);
    763 #endif
    764 	buffer = CNW_EREG_RDP + 2;
    765 	bufbytes = 0;
    766 	bufptr = 0; /* XXX make gcc happy */
    767 
    768 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    769 	if (m == 0)
    770 		return (0);
    771 	m->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
    772 	m->m_pkthdr.len = totbytes;
    773 	mbytes = MHLEN;
    774 	top = 0;
    775 	mp = ⊤
    776 
    777 	while (totbytes > 0) {
    778 		if (top) {
    779 			MGET(m, M_DONTWAIT, MT_DATA);
    780 			if (m == 0) {
    781 				m_freem(top);
    782 				return (0);
    783 			}
    784 			mbytes = MLEN;
    785 		}
    786 		if (totbytes >= MINCLSIZE) {
    787 			MCLGET(m, M_DONTWAIT);
    788 			if ((m->m_flags & M_EXT) == 0) {
    789 				m_free(m);
    790 				m_freem(top);
    791 				return (0);
    792 			}
    793 			mbytes = MCLBYTES;
    794 		}
    795 		if (!top) {
    796 			int pad = ALIGN(sizeof(struct ether_header)) -
    797 			    sizeof(struct ether_header);
    798 			m->m_data += pad;
    799 			mbytes -= pad;
    800 		}
    801 		mptr = mtod(m, u_int8_t *);
    802 		mbytes = m->m_len = min(totbytes, mbytes);
    803 		totbytes -= mbytes;
    804 		while (mbytes > 0) {
    805 			if (bufbytes == 0) {
    806 				buffer = read16(sc, buffer);
    807 				bufbytes = read16(sc, buffer + 2);
    808 				bufptr = sc->sc_memoff + buffer +
    809 				    read16(sc, buffer + 4);
    810 #ifdef CNW_DEBUG
    811 				if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
    812 					printf("%s:   %d bytes @0x%x+0x%lx\n",
    813 					    device_xname(&sc->sc_dev), bufbytes,
    814 					    buffer, (u_long)(bufptr - buffer -
    815 					    sc->sc_memoff));
    816 #endif
    817 			}
    818 			n = mbytes <= bufbytes ? mbytes : bufbytes;
    819 			bus_space_read_region_1(sc->sc_memt, sc->sc_memh,
    820 			    bufptr, mptr, n);
    821 			bufbytes -= n;
    822 			bufptr += n;
    823 			mbytes -= n;
    824 			mptr += n;
    825 		}
    826 		*mp = m;
    827 		mp = &m->m_next;
    828 	}
    829 
    830 	return (top);
    831 }
    832 
    833 
    834 /*
    835  * Handle received packets.
    836  */
    837 void
    838 cnw_recv(struct cnw_softc *sc)
    839 {
    840 	int rser;
    841 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    842 	struct mbuf *m;
    843 
    844 	for (;;) {
    845 		WAIT_WOC(sc);
    846 		rser = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    847 		    sc->sc_memoff + CNW_EREG_RSER);
    848 		if (!(rser & CNW_RSER_RXAVAIL))
    849 			return;
    850 
    851 		/* Pull packet off card */
    852 		m = cnw_read(sc);
    853 
    854 		/* Acknowledge packet */
    855 		CNW_CMD0(sc, CNW_CMD_SRP);
    856 
    857 		/* Did we manage to get the packet from the interface? */
    858 		if (m == 0) {
    859 			++ifp->if_ierrors;
    860 			return;
    861 		}
    862 		++ifp->if_ipackets;
    863 
    864 #if NBPFILTER > 0
    865 		if (ifp->if_bpf)
    866 			bpf_mtap(ifp->if_bpf, m);
    867 #endif
    868 
    869 		/* Pass the packet up. */
    870 		(*ifp->if_input)(ifp, m);
    871 	}
    872 }
    873 
    874 
    875 /*
    876  * Interrupt handler.
    877  */
    878 int
    879 cnw_intr(void *arg)
    880 {
    881 	struct cnw_softc *sc = arg;
    882 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    883 	int ret, status, rser, tser;
    884 
    885 	if ((sc->sc_ethercom.ec_if.if_flags & IFF_RUNNING) == 0 ||
    886 	    !device_is_active(&sc->sc_dev))
    887 		return (0);
    888 	ifp->if_timer = 0;	/* stop watchdog timer */
    889 
    890 	ret = 0;
    891 	for (;;) {
    892 		WAIT_WOC(sc);
    893 #ifndef MEMORY_MAPPED
    894 		status = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
    895 		    CNW_REG_CCSR);
    896 #else
    897 		status = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    898 		    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_CCSR);
    899 #endif
    900 		if (!(status & 0x02)) {
    901 			if (ret == 0)
    902 				printf("%s: spurious interrupt\n",
    903 				    device_xname(&sc->sc_dev));
    904 			return (ret);
    905 		}
    906 		ret = 1;
    907 #ifndef MEMORY_MAPPED
    908 		status = bus_space_read_1(sc->sc_iot, sc->sc_ioh, CNW_REG_ASR);
    909 #else
    910 		status = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    911 		    sc->sc_memoff + CNW_IOM_OFF + CNW_REG_ASR);
    912 #endif
    913 
    914 		/* Anything to receive? */
    915 		if (status & CNW_ASR_RXRDY) {
    916 			sc->sc_stats.nws_rx++;
    917 			cnw_recv(sc);
    918 		}
    919 
    920 		/* Receive error */
    921 		if (status & CNW_ASR_RXERR) {
    922 			/*
    923 			 * I get a *lot* of spurious receive errors
    924 			 * (many per second), even when the interface
    925 			 * is quiescent, so we don't increment
    926 			 * if_ierrors here.
    927 			 */
    928 			rser = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    929 			    sc->sc_memoff + CNW_EREG_RSER);
    930 
    931 			/* RX statistics */
    932 			sc->sc_stats.nws_rxerr++;
    933 			if (rser & CNW_RSER_RXBIG)
    934 				sc->sc_stats.nws_rxframe++;
    935 			if (rser & CNW_RSER_RXCRC)
    936 				sc->sc_stats.nws_rxcrcerror++;
    937 			if (rser & CNW_RSER_RXOVERRUN)
    938 				sc->sc_stats.nws_rxoverrun++;
    939 			if (rser & CNW_RSER_RXOVERFLOW)
    940 				sc->sc_stats.nws_rxoverflow++;
    941 			if (rser & CNW_RSER_RXERR)
    942 				sc->sc_stats.nws_rxerrors++;
    943 			if (rser & CNW_RSER_RXAVAIL)
    944 				sc->sc_stats.nws_rxavail++;
    945 
    946 			/* Clear error bits in RSER */
    947 			WAIT_WOC(sc);
    948 			bus_space_write_1(sc->sc_memt, sc->sc_memh,
    949 			    sc->sc_memoff + CNW_EREG_RSERW,
    950 			    CNW_RSER_RXERR |
    951 			    (rser & (CNW_RSER_RXCRC | CNW_RSER_RXBIG)));
    952 			/* Clear RXERR in ASR */
    953 			WAIT_WOC(sc);
    954 			bus_space_write_1(sc->sc_memt, sc->sc_memh,
    955 			    sc->sc_memoff + CNW_EREG_ASCC, CNW_ASR_RXERR);
    956 		}
    957 
    958 		/* Transmit done */
    959 		if (status & CNW_ASR_TXDN) {
    960 			tser = bus_space_read_1(sc->sc_memt, sc->sc_memh,
    961 						CNW_EREG_TSER);
    962 
    963 			/* TX statistics */
    964 			if (tser & CNW_TSER_TXERR)
    965 				sc->sc_stats.nws_txerrors++;
    966 			if (tser & CNW_TSER_TXNOAP)
    967 				sc->sc_stats.nws_txlostcd++;
    968 			if (tser & CNW_TSER_TXGU)
    969 				sc->sc_stats.nws_txabort++;
    970 
    971 			if (tser & CNW_TSER_TXOK) {
    972 				sc->sc_stats.nws_txokay++;
    973 				sc->sc_stats.nws_txretries[status & 0xf]++;
    974 				WAIT_WOC(sc);
    975 				bus_space_write_1(sc->sc_memt, sc->sc_memh,
    976 				    sc->sc_memoff + CNW_EREG_TSERW,
    977 				    CNW_TSER_TXOK | CNW_TSER_RTRY);
    978 			}
    979 
    980 			if (tser & CNW_TSER_ERROR) {
    981 				++ifp->if_oerrors;
    982 				WAIT_WOC(sc);
    983 				bus_space_write_1(sc->sc_memt, sc->sc_memh,
    984 				    sc->sc_memoff + CNW_EREG_TSERW,
    985 				    (tser & CNW_TSER_ERROR) |
    986 				    CNW_TSER_RTRY);
    987 			}
    988 
    989 			sc->sc_active = 0;
    990 			ifp->if_flags &= ~IFF_OACTIVE;
    991 
    992 			/* Continue to send packets from the queue */
    993 			cnw_start(&sc->sc_ethercom.ec_if);
    994 		}
    995 
    996 	}
    997 }
    998 
    999 
   1000 /*
   1001  * Handle device ioctls.
   1002  */
   1003 int
   1004 cnw_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1005 {
   1006 	struct cnw_softc *sc = ifp->if_softc;
   1007 	struct ifaddr *ifa = (struct ifaddr *)data;
   1008 	struct ifreq *ifr = (struct ifreq *)data;
   1009 	int s, error = 0;
   1010 	struct lwp *l = curlwp;	/*XXX*/
   1011 
   1012 	switch (cmd) {
   1013 	case SIOCINITIFADDR:
   1014 	case SIOCSIFFLAGS:
   1015 	case SIOCADDMULTI:
   1016 	case SIOCDELMULTI:
   1017 	case SIOCGCNWDOMAIN:
   1018 	case SIOCGCNWSTATS:
   1019 		break;
   1020 	case SIOCSCNWDOMAIN:
   1021 	case SIOCSCNWKEY:
   1022 	case SIOCGCNWSTATUS:
   1023 		error = kauth_authorize_generic(l->l_cred,
   1024 		    KAUTH_GENERIC_ISSUSER, NULL);
   1025 		if (error)
   1026 			return (error);
   1027 		break;
   1028 	default:
   1029 		return (EINVAL);
   1030 	}
   1031 
   1032 	s = splnet();
   1033 
   1034 	switch (cmd) {
   1035 
   1036 	case SIOCINITIFADDR:
   1037 		if (!(ifp->if_flags & IFF_RUNNING) &&
   1038 		    (error = cnw_enable(sc)) != 0)
   1039 			break;
   1040 		ifp->if_flags |= IFF_UP;
   1041 		cnw_init(sc);
   1042 		switch (ifa->ifa_addr->sa_family) {
   1043 #ifdef INET
   1044 		case AF_INET:
   1045 			arp_ifinit(&sc->sc_ethercom.ec_if, ifa);
   1046 			break;
   1047 #endif
   1048 		default:
   1049 			break;
   1050 		}
   1051 		break;
   1052 
   1053 	case SIOCSIFFLAGS:
   1054 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1055 			break;
   1056 		/* XXX re-use ether_ioctl() */
   1057 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
   1058 		case IFF_RUNNING:
   1059 			/*
   1060 			 * The interface is marked down and it is running, so
   1061 			 * stop it.
   1062 			 */
   1063 			cnw_disable(sc);
   1064 			break;
   1065 		case IFF_UP:
   1066 			/*
   1067 			 * The interface is marked up and it is stopped, so
   1068 			 * start it.
   1069 			 */
   1070 			error = cnw_enable(sc);
   1071 			break;
   1072 		default:
   1073 			/* IFF_PROMISC may be changed */
   1074 			cnw_init(sc);
   1075 			break;
   1076 		}
   1077 		break;
   1078 
   1079 	case SIOCADDMULTI:
   1080 	case SIOCDELMULTI:
   1081 		/* Update our multicast list. */
   1082 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
   1083 			if (ifp->if_flags & IFF_RUNNING)
   1084 				cnw_init(sc);
   1085 			error = 0;
   1086 		}
   1087 		break;
   1088 
   1089 	case SIOCGCNWDOMAIN:
   1090 		ifr->ifr_domain = sc->sc_domain;
   1091 		break;
   1092 
   1093 	case SIOCSCNWDOMAIN:
   1094 		error = cnw_setdomain(sc, ifr->ifr_domain);
   1095 		break;
   1096 
   1097 	case SIOCSCNWKEY:
   1098 		error = cnw_setkey(sc, ifr->ifr_key);
   1099 		break;
   1100 
   1101 	case SIOCGCNWSTATUS:
   1102 		if ((ifp->if_flags & IFF_RUNNING) == 0)
   1103 			break;
   1104 		bus_space_read_region_1(sc->sc_memt, sc->sc_memh,
   1105 		    sc->sc_memoff + CNW_EREG_CB,
   1106 		    ((struct cnwstatus *)data)->data,
   1107 		    sizeof(((struct cnwstatus *)data)->data));
   1108 		break;
   1109 
   1110 	case SIOCGCNWSTATS:
   1111 		memcpy((void *)&(((struct cnwistats *)data)->stats),
   1112 		    (void *)&sc->sc_stats, sizeof(struct cnwstats));
   1113 			break;
   1114 
   1115 	default:
   1116 		error = ether_ioctl(ifp, cmd, data);
   1117 		break;
   1118 	}
   1119 
   1120 	splx(s);
   1121 	return (error);
   1122 }
   1123 
   1124 
   1125 /*
   1126  * Device timeout/watchdog routine. Entered if the device neglects to
   1127  * generate an interrupt after a transmit has been started on it.
   1128  */
   1129 void
   1130 cnw_watchdog(struct ifnet *ifp)
   1131 {
   1132 	struct cnw_softc *sc = ifp->if_softc;
   1133 
   1134 	printf("%s: device timeout; card reset\n", device_xname(&sc->sc_dev));
   1135 	++ifp->if_oerrors;
   1136 	cnw_init(sc);
   1137 }
   1138 
   1139 int
   1140 cnw_setdomain(struct cnw_softc *sc, int domain)
   1141 {
   1142 	int s;
   1143 
   1144 	if (domain & ~0x1ff)
   1145 		return EINVAL;
   1146 
   1147 	s = splnet();
   1148 	CNW_CMD2(sc, CNW_CMD_SMD, domain, domain >> 8);
   1149 	splx(s);
   1150 
   1151 	sc->sc_domain = domain;
   1152 	return 0;
   1153 }
   1154 
   1155 int
   1156 cnw_setkey(struct cnw_softc *sc, int key)
   1157 {
   1158 	int s;
   1159 
   1160 	if (key & ~0xffff)
   1161 		return EINVAL;
   1162 
   1163 	s = splnet();
   1164 	CNW_CMD2(sc, CNW_CMD_SSK, key, key >> 8);
   1165 	splx(s);
   1166 
   1167 	sc->sc_skey = key;
   1168 	return 0;
   1169 }
   1170 
   1171 int
   1172 cnw_activate(struct device *self, enum devact act)
   1173 {
   1174 	struct cnw_softc *sc = (struct cnw_softc *)self;
   1175 	int rv = 0, s;
   1176 
   1177 	s = splnet();
   1178 	switch (act) {
   1179 	case DVACT_ACTIVATE:
   1180 		rv = EOPNOTSUPP;
   1181 		break;
   1182 
   1183 	case DVACT_DEACTIVATE:
   1184 		if_deactivate(&sc->sc_ethercom.ec_if);
   1185 		break;
   1186 	}
   1187 	splx(s);
   1188 	return (rv);
   1189 }
   1190 
   1191 int
   1192 cnw_detach(struct device *self, int flags)
   1193 {
   1194 	struct cnw_softc *sc = (struct cnw_softc *)self;
   1195 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1196 
   1197 	/* cnw_disable() checks IFF_RUNNING */
   1198 	cnw_disable(sc);
   1199 
   1200 	if ((sc->sc_resource & CNW_RES_NET) != 0) {
   1201 		ether_ifdetach(ifp);
   1202 		if_detach(ifp);
   1203 	}
   1204 
   1205 #ifndef MEMORY_MAPPED
   1206 	/* unmap and free our i/o windows */
   1207 	if ((sc->sc_resource & CNW_RES_IO) != 0) {
   1208 		pcmcia_io_unmap(sc->sc_pf, sc->sc_iowin);
   1209 		pcmcia_io_free(sc->sc_pf, &sc->sc_pcioh);
   1210 	}
   1211 #endif
   1212 
   1213 	/* unmap and free our memory windows */
   1214 	if ((sc->sc_resource & CNW_RES_MEM) != 0) {
   1215 		pcmcia_mem_unmap(sc->sc_pf, sc->sc_memwin);
   1216 		pcmcia_mem_free(sc->sc_pf, &sc->sc_pcmemh);
   1217 	}
   1218 
   1219 	return (0);
   1220 }
   1221