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