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