Home | History | Annotate | Line # | Download | only in usb
if_axe.c revision 1.28
      1 /*	$NetBSD: if_axe.c,v 1.28 2009/09/04 18:10:08 dyoung Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997, 1998, 1999, 2000-2003
      5  *	Bill Paul <wpaul (at) windriver.com>.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Bill Paul.
     18  * 4. Neither the name of the author nor the names of any co-contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32  * THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * ASIX Electronics AX88172 USB 2.0 ethernet driver. Used in the
     37  * LinkSys USB200M and various other adapters.
     38  *
     39  * Manuals available from:
     40  * http://www.asix.com.tw/datasheet/mac/Ax88172.PDF
     41  * (also http://people.freebsd.org/~wpaul/ASIX/Ax88172.PDF)
     42  * Note: you need the manual for the AX88170 chip (USB 1.x ethernet
     43  * controller) to find the definitions for the RX control register.
     44  * http://www.asix.com.tw/datasheet/mac/Ax88170.PDF
     45  *
     46  * Written by Bill Paul <wpaul (at) windriver.com>
     47  * Senior Engineer
     48  * Wind River Systems
     49  */
     50 
     51 /*
     52  * The AX88172 provides USB ethernet supports at 10 and 100Mbps.
     53  * It uses an external PHY (reference designs use a RealTek chip),
     54  * and has a 64-bit multicast hash filter. There is some information
     55  * missing from the manual which one needs to know in order to make
     56  * the chip function:
     57  *
     58  * - You must set bit 7 in the RX control register, otherwise the
     59  *   chip won't receive any packets.
     60  * - You must initialize all 3 IPG registers, or you won't be able
     61  *   to send any packets.
     62  *
     63  * Note that this device appears to only support loading the station
     64  * address via autload from the EEPROM (i.e. there's no way to manaully
     65  * set it).
     66  *
     67  * (Adam Weinberger wanted me to name this driver if_gir.c.)
     68  */
     69 
     70 /*
     71  * Ported to OpenBSD 3/28/2004 by Greg Taleck <taleck (at) oz.net>
     72  * with bits and pieces from the aue and url drivers.
     73  */
     74 
     75 #include <sys/cdefs.h>
     76 __KERNEL_RCSID(0, "$NetBSD: if_axe.c,v 1.28 2009/09/04 18:10:08 dyoung Exp $");
     77 
     78 #if defined(__NetBSD__)
     79 #include "opt_inet.h"
     80 #include "rnd.h"
     81 #endif
     82 
     83 #include "bpfilter.h"
     84 
     85 #include <sys/param.h>
     86 #include <sys/systm.h>
     87 #include <sys/sockio.h>
     88 #include <sys/mutex.h>
     89 #include <sys/mbuf.h>
     90 #include <sys/kernel.h>
     91 #if defined(__OpenBSD__)
     92 #include <sys/proc.h>
     93 #endif
     94 #include <sys/socket.h>
     95 
     96 #include <sys/device.h>
     97 #if NRND > 0
     98 #include <sys/rnd.h>
     99 #endif
    100 
    101 #include <net/if.h>
    102 #if defined(__NetBSD__)
    103 #include <net/if_arp.h>
    104 #endif
    105 #include <net/if_dl.h>
    106 #include <net/if_media.h>
    107 
    108 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m))
    109 
    110 #if NBPFILTER > 0
    111 #include <net/bpf.h>
    112 #endif
    113 
    114 #if defined(__NetBSD__)
    115 #include <net/if_ether.h>
    116 #ifdef INET
    117 #include <netinet/in.h>
    118 #include <netinet/if_inarp.h>
    119 #endif
    120 #endif /* defined(__NetBSD__) */
    121 
    122 #if defined(__OpenBSD__)
    123 #ifdef INET
    124 #include <netinet/in.h>
    125 #include <netinet/in_systm.h>
    126 #include <netinet/in_var.h>
    127 #include <netinet/ip.h>
    128 #include <netinet/if_ether.h>
    129 #endif
    130 #endif /* defined(__OpenBSD__) */
    131 
    132 
    133 #include <dev/mii/mii.h>
    134 #include <dev/mii/miivar.h>
    135 
    136 #include <dev/usb/usb.h>
    137 #include <dev/usb/usbdi.h>
    138 #include <dev/usb/usbdi_util.h>
    139 #include <dev/usb/usbdevs.h>
    140 
    141 #include <dev/usb/if_axereg.h>
    142 
    143 #ifdef AXE_DEBUG
    144 #define DPRINTF(x)	do { if (axedebug) logprintf x; } while (0)
    145 #define DPRINTFN(n,x)	do { if (axedebug >= (n)) logprintf x; } while (0)
    146 int	axedebug = 0;
    147 #else
    148 #define DPRINTF(x)
    149 #define DPRINTFN(n,x)
    150 #endif
    151 
    152 /*
    153  * Various supported device vendors/products.
    154  */
    155 Static const struct axe_type axe_devs[] = {
    156 	{ { USB_VENDOR_ASIX,		USB_PRODUCT_ASIX_AX88172}, 0 },
    157 	{ { USB_VENDOR_COREGA,		USB_PRODUCT_COREGA_FETHER_USB2_TX }, 0},
    158 	{ { USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DUBE100}, 0 },
    159 	{ { USB_VENDOR_LINKSYS2,	USB_PRODUCT_LINKSYS2_USB200M}, 0 },
    160 	{ { USB_VENDOR_MELCO,		USB_PRODUCT_MELCO_LUAU2KTX}, 0 },
    161 	{ { USB_VENDOR_NETGEAR,		USB_PRODUCT_NETGEAR_FA120}, 0 },
    162 	{ { USB_VENDOR_SITECOM,		USB_PRODUCT_SITECOM_LN029}, 0 },
    163 	{ { USB_VENDOR_SYSTEMTALKS,	USB_PRODUCT_SYSTEMTALKS_SGCX2UL}, 0 },
    164 };
    165 #define axe_lookup(v, p) ((const struct axe_type *)usb_lookup(axe_devs, v, p))
    166 
    167 int axe_match(device_t, cfdata_t, void *);
    168 void axe_attach(device_t, device_t, void *);
    169 int axe_detach(device_t, int);
    170 int axe_activate(device_t, enum devact);
    171 extern struct cfdriver axe_cd;
    172 CFATTACH_DECL_NEW(axe, sizeof(struct axe_softc), axe_match, axe_attach,
    173     axe_detach, axe_activate);
    174 
    175 Static int axe_tx_list_init(struct axe_softc *);
    176 Static int axe_rx_list_init(struct axe_softc *);
    177 Static int axe_newbuf(struct axe_softc *, struct axe_chain *, struct mbuf *);
    178 Static int axe_encap(struct axe_softc *, struct mbuf *, int);
    179 Static void axe_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
    180 Static void axe_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
    181 Static void axe_tick(void *);
    182 Static void axe_tick_task(void *);
    183 #if 0
    184 Static void axe_rxstart(struct ifnet *);
    185 #endif
    186 Static void axe_start(struct ifnet *);
    187 Static int axe_ioctl(struct ifnet *, u_long, void *);
    188 Static void axe_init(void *);
    189 Static void axe_stop(struct axe_softc *);
    190 Static void axe_watchdog(struct ifnet *);
    191 Static int axe_miibus_readreg(device_t, int, int);
    192 Static void axe_miibus_writereg(device_t, int, int, int);
    193 Static void axe_miibus_statchg(device_t);
    194 Static int axe_cmd(struct axe_softc *, int, int, int, void *);
    195 Static void axe_reset(struct axe_softc *sc);
    196 
    197 Static void axe_setmulti(struct axe_softc *);
    198 Static void axe_lock_mii(struct axe_softc *sc);
    199 Static void axe_unlock_mii(struct axe_softc *sc);
    200 
    201 /* Get exclusive access to the MII registers */
    202 Static void
    203 axe_lock_mii(struct axe_softc *sc)
    204 {
    205 	sc->axe_refcnt++;
    206 	mutex_enter(&sc->axe_mii_lock);
    207 }
    208 
    209 Static void
    210 axe_unlock_mii(struct axe_softc *sc)
    211 {
    212 	mutex_exit(&sc->axe_mii_lock);
    213 	if (--sc->axe_refcnt < 0)
    214 		usb_detach_wakeup((sc->axe_dev));
    215 }
    216 
    217 Static int
    218 axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
    219 {
    220 	usb_device_request_t	req;
    221 	usbd_status		err;
    222 
    223 	KASSERT(mutex_owned(&sc->axe_mii_lock));
    224 
    225 	if (sc->axe_dying)
    226 		return(0);
    227 
    228 	if (AXE_CMD_DIR(cmd))
    229 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    230 	else
    231 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
    232 	req.bRequest = AXE_CMD_CMD(cmd);
    233 	USETW(req.wValue, val);
    234 	USETW(req.wIndex, index);
    235 	USETW(req.wLength, AXE_CMD_LEN(cmd));
    236 
    237 	err = usbd_do_request(sc->axe_udev, &req, buf);
    238 
    239 	if (err)
    240 		return(-1);
    241 
    242 	return(0);
    243 }
    244 
    245 Static int
    246 axe_miibus_readreg(device_t dev, int phy, int reg)
    247 {
    248 	struct axe_softc *sc = device_private(dev);
    249 	usbd_status		err;
    250 	u_int16_t		val;
    251 
    252 	if (sc->axe_dying) {
    253 		DPRINTF(("axe: dying\n"));
    254 		return(0);
    255 	}
    256 
    257 #ifdef notdef
    258 	/*
    259 	 * The chip tells us the MII address of any supported
    260 	 * PHYs attached to the chip, so only read from those.
    261 	 */
    262 
    263 	if (sc->axe_phyaddrs[0] != AXE_NOPHY && phy != sc->axe_phyaddrs[0])
    264 		return (0);
    265 
    266 	if (sc->axe_phyaddrs[1] != AXE_NOPHY && phy != sc->axe_phyaddrs[1])
    267 		return (0);
    268 #endif
    269 	if (sc->axe_phyaddrs[0] != 0xFF && sc->axe_phyaddrs[0] != phy)
    270 		return (0);
    271 
    272 	val = 0;
    273 
    274 	axe_lock_mii(sc);
    275 	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
    276 	err = axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, (void *)&val);
    277 	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
    278 	axe_unlock_mii(sc);
    279 
    280 	if (err) {
    281 		aprint_error_dev(sc->axe_dev, "read PHY failed\n");
    282 		return(-1);
    283 	}
    284 
    285 	if (val)
    286 		sc->axe_phyaddrs[0] = phy;
    287 
    288 	return (le16toh(val));
    289 }
    290 
    291 Static void
    292 axe_miibus_writereg(device_t dev, int phy, int reg, int aval)
    293 {
    294 	struct axe_softc *sc = device_private(dev);
    295 	usbd_status		err;
    296 	u_int16_t		val;
    297 
    298 	if (sc->axe_dying)
    299 		return;
    300 
    301 	val = htole16(aval);
    302 	axe_lock_mii(sc);
    303 	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
    304 	err = axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, (void *)&val);
    305 	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
    306 	axe_unlock_mii(sc);
    307 
    308 	if (err) {
    309 		aprint_error_dev(sc->axe_dev, "write PHY failed\n");
    310 		return;
    311 	}
    312 }
    313 
    314 Static void
    315 axe_miibus_statchg(device_t dev)
    316 {
    317 	struct axe_softc *sc = device_private(dev);
    318 	struct mii_data		*mii = GET_MII(sc);
    319 	int val, err;
    320 
    321 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
    322 		val = AXE_MEDIA_FULL_DUPLEX;
    323 	else
    324 		val = 0;
    325 	DPRINTF(("axe_miibus_statchg: val=0x%x\n", val));
    326 	axe_lock_mii(sc);
    327 	err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL);
    328 	axe_unlock_mii(sc);
    329 	if (err) {
    330 		aprint_error_dev(sc->axe_dev, "media change failed\n");
    331 		return;
    332 	}
    333 }
    334 
    335 Static void
    336 axe_setmulti(struct axe_softc *sc)
    337 {
    338 	struct ifnet		*ifp;
    339 	struct ether_multi *enm;
    340 	struct ether_multistep step;
    341 	u_int32_t		h = 0;
    342 	u_int16_t		rxmode;
    343 	u_int8_t		hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
    344 
    345 	if (sc->axe_dying)
    346 		return;
    347 
    348 	ifp = GET_IFP(sc);
    349 
    350 	axe_lock_mii(sc);
    351 	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, (void *)&rxmode);
    352 	rxmode = le16toh(rxmode);
    353 
    354 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
    355 	allmulti:
    356 		rxmode |= AXE_RXCMD_ALLMULTI;
    357 		axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
    358 		return;
    359 	} else
    360 		rxmode &= ~AXE_RXCMD_ALLMULTI;
    361 
    362 	/* now program new ones */
    363 #if defined(__NetBSD__)
    364 	ETHER_FIRST_MULTI(step, &sc->axe_ec, enm);
    365 #else
    366 	ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
    367 #endif
    368 	while (enm != NULL) {
    369 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    370 			   ETHER_ADDR_LEN) != 0)
    371 			goto allmulti;
    372 
    373 		h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26;
    374 		hashtbl[h / 8] |= 1 << (h % 8);
    375 		ETHER_NEXT_MULTI(step, enm);
    376 	}
    377 
    378 	ifp->if_flags &= ~IFF_ALLMULTI;
    379 	axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
    380 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
    381 	axe_unlock_mii(sc);
    382 	return;
    383 }
    384 
    385 Static void
    386 axe_reset(struct axe_softc *sc)
    387 {
    388 	if (sc->axe_dying)
    389 		return;
    390 	/* XXX What to reset? */
    391 
    392 	/* Wait a little while for the chip to get its brains in order. */
    393 	DELAY(1000);
    394 	return;
    395 }
    396 
    397 /*
    398  * Probe for a AX88172 chip.
    399  */
    400 int
    401 axe_match(device_t parent, cfdata_t match, void *aux)
    402 {
    403 	struct usb_attach_arg *uaa = aux;
    404 
    405 	return (axe_lookup(uaa->vendor, uaa->product) != NULL ?
    406 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    407 }
    408 
    409 /*
    410  * Attach the interface. Allocate softc structures, do ifmedia
    411  * setup and ethernet/BPF attach.
    412  */
    413 void
    414 axe_attach(device_t parent, device_t self, void *aux)
    415 {
    416 	struct axe_softc *sc = device_private(self);
    417 	struct usb_attach_arg *uaa = aux;
    418 	usbd_device_handle dev = uaa->device;
    419 	usbd_status err;
    420 	usb_interface_descriptor_t *id;
    421 	usb_endpoint_descriptor_t *ed;
    422 	struct mii_data	*mii;
    423 	u_char eaddr[ETHER_ADDR_LEN];
    424 	char *devinfop;
    425 	const char *devname = device_xname(self);
    426 	struct ifnet *ifp;
    427 	int i, s;
    428 
    429 	devinfop = usbd_devinfo_alloc(dev, 0);
    430 	aprint_naive("\n");
    431 	aprint_normal("\n");
    432 	sc->axe_dev = self;
    433 
    434 	err = usbd_set_config_no(dev, AXE_CONFIG_NO, 1);
    435 	if (err) {
    436 		aprint_error_dev(self, "getting interface handle failed\n");
    437 		usbd_devinfo_free(devinfop);
    438 		return;
    439 	}
    440 
    441 	usb_init_task(&sc->axe_tick_task, axe_tick_task, sc);
    442 	mutex_init(&sc->axe_mii_lock, MUTEX_DEFAULT, IPL_NONE);
    443 	usb_init_task(&sc->axe_stop_task, (void (*)(void *))axe_stop, sc);
    444 
    445 	err = usbd_device2interface_handle(dev, AXE_IFACE_IDX, &sc->axe_iface);
    446 	if (err) {
    447 		aprint_error_dev(self, "getting interface handle failed\n");
    448 		usbd_devinfo_free(devinfop);
    449 		return;
    450 	}
    451 
    452 	sc->axe_udev = dev;
    453 	sc->axe_product = uaa->product;
    454 	sc->axe_vendor = uaa->vendor;
    455 
    456 	id = usbd_get_interface_descriptor(sc->axe_iface);
    457 
    458 	aprint_normal_dev(self, "%s\n", devinfop);
    459 	usbd_devinfo_free(devinfop);
    460 
    461 	/* Find endpoints. */
    462 	for (i = 0; i < id->bNumEndpoints; i++) {
    463 		ed = usbd_interface2endpoint_descriptor(sc->axe_iface, i);
    464 		if (!ed) {
    465 			aprint_error_dev(self, "couldn't get ep %d\n", i);
    466 			return;
    467 		}
    468 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    469 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    470 			sc->axe_ed[AXE_ENDPT_RX] = ed->bEndpointAddress;
    471 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    472 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    473 			sc->axe_ed[AXE_ENDPT_TX] = ed->bEndpointAddress;
    474 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    475 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    476 			sc->axe_ed[AXE_ENDPT_INTR] = ed->bEndpointAddress;
    477 		}
    478 	}
    479 
    480 	s = splnet();
    481 
    482 	/*
    483 	 * Get station address.
    484 	 */
    485 	axe_lock_mii(sc);
    486 	axe_cmd(sc, AXE_CMD_READ_NODEID, 0, 0, &eaddr);
    487 
    488 	/*
    489 	 * Load IPG values and PHY indexes.
    490 	 */
    491 	axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, (void *)&sc->axe_ipgs);
    492 	axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, (void *)&sc->axe_phyaddrs);
    493 	axe_unlock_mii(sc);
    494 
    495 	/*
    496 	 * Work around broken adapters that appear to lie about
    497 	 * their PHY addresses.
    498 	 */
    499 	sc->axe_phyaddrs[0] = sc->axe_phyaddrs[1] = 0xFF;
    500 
    501 	/*
    502 	 * An ASIX chip was detected. Inform the world.
    503 	 */
    504 	aprint_normal_dev(self, "Ethernet address %s\n",
    505 	    ether_sprintf(eaddr));
    506 
    507 	/* Initialize interface info.*/
    508 	ifp = GET_IFP(sc);
    509 	ifp->if_softc = sc;
    510 	strncpy(ifp->if_xname, devname, IFNAMSIZ);
    511 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    512 	ifp->if_ioctl = axe_ioctl;
    513 	ifp->if_start = axe_start;
    514 
    515 	ifp->if_watchdog = axe_watchdog;
    516 
    517 /*	ifp->if_baudrate = 10000000; */
    518 /*	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;*/
    519 
    520 	IFQ_SET_READY(&ifp->if_snd);
    521 
    522 	/* Initialize MII/media info. */
    523 	mii = &sc->axe_mii;
    524 	mii->mii_ifp = ifp;
    525 	mii->mii_readreg = axe_miibus_readreg;
    526 	mii->mii_writereg = axe_miibus_writereg;
    527 	mii->mii_statchg = axe_miibus_statchg;
    528 	mii->mii_flags = MIIF_AUTOTSLEEP;
    529 
    530 	sc->axe_ec.ec_mii = mii;
    531 	ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
    532 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
    533 
    534 	if (LIST_EMPTY(&mii->mii_phys)) {
    535 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
    536 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
    537 	} else
    538 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    539 
    540 	/* Attach the interface. */
    541 	if_attach(ifp);
    542 	ether_ifattach(ifp, eaddr);
    543 #if NRND > 0
    544 	rnd_attach_source(&sc->rnd_source, device_xname(sc->axe_dev),
    545 	    RND_TYPE_NET, 0);
    546 #endif
    547 
    548 	callout_init(&(sc->axe_stat_ch), 0);
    549 
    550 	sc->axe_attached = 1;
    551 	splx(s);
    552 
    553 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->axe_udev, sc->axe_dev);
    554 
    555 	return;
    556 }
    557 
    558 int
    559 axe_detach(device_t self, int flags)
    560 {
    561 	struct axe_softc *sc = device_private(self);
    562 	int			s;
    563 	struct ifnet		*ifp = GET_IFP(sc);
    564 
    565 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
    566 
    567 	/* Detached before attached finished, so just bail out. */
    568 	if (!sc->axe_attached)
    569 		return (0);
    570 
    571 	callout_stop(&(sc->axe_stat_ch));
    572 
    573 	sc->axe_dying = 1;
    574 
    575 	ether_ifdetach(ifp);
    576 
    577 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL)
    578 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
    579 	if (sc->axe_ep[AXE_ENDPT_RX] != NULL)
    580 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
    581 	if (sc->axe_ep[AXE_ENDPT_INTR] != NULL)
    582 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
    583 
    584 	/*
    585 	 * Remove any pending tasks.  They cannot be executing because they run
    586 	 * in the same thread as detach.
    587 	 */
    588 	usb_rem_task(sc->axe_udev, &sc->axe_tick_task);
    589 	usb_rem_task(sc->axe_udev, &sc->axe_stop_task);
    590 
    591 	s = splusb();
    592 
    593 	if (--sc->axe_refcnt >= 0) {
    594 		/* Wait for processes to go away */
    595 		usb_detach_wait((sc->axe_dev));
    596 	}
    597 
    598 	if (ifp->if_flags & IFF_RUNNING)
    599 		axe_stop(sc);
    600 
    601 #if defined(__NetBSD__)
    602 #if NRND > 0
    603 	rnd_detach_source(&sc->rnd_source);
    604 #endif
    605 #endif /* __NetBSD__ */
    606 	mii_detach(&sc->axe_mii, MII_PHY_ANY, MII_OFFSET_ANY);
    607 	ifmedia_delete_instance(&sc->axe_mii.mii_media, IFM_INST_ANY);
    608 	ether_ifdetach(ifp);
    609 	if_detach(ifp);
    610 
    611 #ifdef DIAGNOSTIC
    612 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL ||
    613 	    sc->axe_ep[AXE_ENDPT_RX] != NULL ||
    614 	    sc->axe_ep[AXE_ENDPT_INTR] != NULL)
    615 		aprint_debug_dev(self, "detach has active endpoints\n");
    616 #endif
    617 
    618 	sc->axe_attached = 0;
    619 
    620 	if (--sc->axe_refcnt >= 0) {
    621 		/* Wait for processes to go away. */
    622 		usb_detach_wait((sc->axe_dev));
    623 	}
    624 	splx(s);
    625 
    626 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->axe_udev, sc->axe_dev);
    627 
    628 	return (0);
    629 }
    630 
    631 int
    632 axe_activate(device_t self, enum devact act)
    633 {
    634 	struct axe_softc *sc = device_private(self);
    635 
    636 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
    637 
    638 	switch (act) {
    639 	case DVACT_ACTIVATE:
    640 		return (EOPNOTSUPP);
    641 		break;
    642 
    643 	case DVACT_DEACTIVATE:
    644 		if_deactivate(&sc->axe_ec.ec_if);
    645 		sc->axe_dying = 1;
    646 		break;
    647 	}
    648 	return (0);
    649 }
    650 
    651 /*
    652  * Initialize an RX descriptor and attach an MBUF cluster.
    653  */
    654 Static int
    655 axe_newbuf(struct axe_softc *sc, struct axe_chain *c, struct mbuf *m)
    656 {
    657 	struct mbuf		*m_new = NULL;
    658 
    659 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev),__func__));
    660 
    661 	if (m == NULL) {
    662 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    663 		if (m_new == NULL) {
    664 			aprint_error_dev(sc->axe_dev, "no memory for rx list "
    665 			    "-- packet dropped!\n");
    666 			return (ENOBUFS);
    667 		}
    668 
    669 		MCLGET(m_new, M_DONTWAIT);
    670 		if (!(m_new->m_flags & M_EXT)) {
    671 			aprint_error_dev(sc->axe_dev, "no memory for rx list "
    672 			    "-- packet dropped!\n");
    673 			m_freem(m_new);
    674 			return (ENOBUFS);
    675 		}
    676 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    677 	} else {
    678 		m_new = m;
    679 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    680 		m_new->m_data = m_new->m_ext.ext_buf;
    681 	}
    682 
    683 	m_adj(m_new, ETHER_ALIGN);
    684 	c->axe_mbuf = m_new;
    685 
    686 	return (0);
    687 }
    688 
    689 Static int
    690 axe_rx_list_init(struct axe_softc *sc)
    691 {
    692 	struct axe_cdata *cd;
    693 	struct axe_chain *c;
    694 	int i;
    695 
    696 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
    697 
    698 	cd = &sc->axe_cdata;
    699 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
    700 		c = &cd->axe_rx_chain[i];
    701 		c->axe_sc = sc;
    702 		c->axe_idx = i;
    703 		if (axe_newbuf(sc, c, NULL) == ENOBUFS)
    704 			return (ENOBUFS);
    705 		if (c->axe_xfer == NULL) {
    706 			c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
    707 			if (c->axe_xfer == NULL)
    708 				return (ENOBUFS);
    709 			c->axe_buf = usbd_alloc_buffer(c->axe_xfer, AXE_BUFSZ);
    710 			if (c->axe_buf == NULL) {
    711 				usbd_free_xfer(c->axe_xfer);
    712 				return (ENOBUFS);
    713 			}
    714 		}
    715 	}
    716 
    717 	return (0);
    718 }
    719 
    720 Static int
    721 axe_tx_list_init(struct axe_softc *sc)
    722 {
    723 	struct axe_cdata *cd;
    724 	struct axe_chain *c;
    725 	int i;
    726 
    727 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
    728 
    729 	cd = &sc->axe_cdata;
    730 	for (i = 0; i < AXE_TX_LIST_CNT; i++) {
    731 		c = &cd->axe_tx_chain[i];
    732 		c->axe_sc = sc;
    733 		c->axe_idx = i;
    734 		c->axe_mbuf = NULL;
    735 		if (c->axe_xfer == NULL) {
    736 			c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
    737 			if (c->axe_xfer == NULL)
    738 				return (ENOBUFS);
    739 			c->axe_buf = usbd_alloc_buffer(c->axe_xfer, AXE_BUFSZ);
    740 			if (c->axe_buf == NULL) {
    741 				usbd_free_xfer(c->axe_xfer);
    742 				return (ENOBUFS);
    743 			}
    744 		}
    745 	}
    746 
    747 	return (0);
    748 }
    749 
    750 #if 0
    751 Static void
    752 axe_rxstart(struct ifnet *ifp)
    753 {
    754 	struct axe_softc	*sc;
    755 	struct axe_chain	*c;
    756 
    757 	sc = ifp->if_softc;
    758 	axe_lock_mii(sc);
    759 	c = &sc->axe_cdata.axe_rx_chain[sc->axe_cdata.axe_rx_prod];
    760 
    761 	if (axe_newbuf(sc, c, NULL) == ENOBUFS) {
    762 		ifp->if_ierrors++;
    763 		axe_unlock_mii(sc);
    764 		return;
    765 	}
    766 
    767 	/* Setup new transfer. */
    768 	usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
    769 	    c, mtod(c->axe_mbuf, char *), AXE_BUFSZ, USBD_SHORT_XFER_OK,
    770 	    USBD_NO_TIMEOUT, axe_rxeof);
    771 	usbd_transfer(c->axe_xfer);
    772 	axe_unlock_mii(sc);
    773 
    774 	return;
    775 }
    776 #endif
    777 
    778 /*
    779  * A frame has been uploaded: pass the resulting mbuf chain up to
    780  * the higher level protocols.
    781  */
    782 Static void
    783 axe_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    784 {
    785 	struct axe_softc	*sc;
    786 	struct axe_chain	*c;
    787 	struct ifnet		*ifp;
    788 	struct mbuf		*m;
    789 	u_int32_t		total_len;
    790 	int			s;
    791 
    792 	c = priv;
    793 	sc = c->axe_sc;
    794 	ifp = GET_IFP(sc);
    795 
    796 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev),__func__));
    797 
    798 	if (sc->axe_dying)
    799 		return;
    800 
    801 	if (!(ifp->if_flags & IFF_RUNNING))
    802 		return;
    803 
    804 	if (status != USBD_NORMAL_COMPLETION) {
    805 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    806 			return;
    807 		if (usbd_ratecheck(&sc->axe_rx_notice)) {
    808 			printf("%s: usb errors on rx: %s\n",
    809 			    device_xname(sc->axe_dev), usbd_errstr(status));
    810 		}
    811 		if (status == USBD_STALLED)
    812 			usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_RX]);
    813 		goto done;
    814 	}
    815 
    816 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
    817 
    818 	m = c->axe_mbuf;
    819 
    820 	if (total_len <= sizeof(struct ether_header)) {
    821 		ifp->if_ierrors++;
    822 		goto done;
    823 	}
    824 
    825 	ifp->if_ipackets++;
    826 	m->m_pkthdr.rcvif = ifp;
    827 	m->m_pkthdr.len = m->m_len = total_len;
    828 
    829 
    830 	memcpy(mtod(c->axe_mbuf, char *), c->axe_buf, total_len);
    831 
    832 	/* No errors; receive the packet. */
    833 	total_len -= ETHER_CRC_LEN + 4;
    834 
    835 	s = splnet();
    836 
    837 	/* XXX ugly */
    838 	if (axe_newbuf(sc, c, NULL) == ENOBUFS) {
    839 		ifp->if_ierrors++;
    840 		goto done1;
    841 	}
    842 
    843 #if NBPFILTER > 0
    844 	if (ifp->if_bpf)
    845 		BPF_MTAP(ifp, m);
    846 #endif
    847 
    848 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->axe_dev),
    849 		    __func__, m->m_len));
    850 	(*(ifp)->if_input)((ifp), (m));
    851  done1:
    852 	splx(s);
    853 
    854  done:
    855 
    856 	/* Setup new transfer. */
    857 	usbd_setup_xfer(xfer, sc->axe_ep[AXE_ENDPT_RX],
    858 	    c, c->axe_buf, AXE_BUFSZ,
    859 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
    860 	    USBD_NO_TIMEOUT, axe_rxeof);
    861 	usbd_transfer(xfer);
    862 
    863 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->axe_dev),
    864 		    __func__));
    865 	return;
    866 }
    867 
    868 /*
    869  * A frame was downloaded to the chip. It's safe for us to clean up
    870  * the list buffers.
    871  */
    872 
    873 Static void
    874 axe_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
    875     usbd_status status)
    876 {
    877 	struct axe_softc	*sc;
    878 	struct axe_chain	*c;
    879 	struct ifnet		*ifp;
    880 	int			s;
    881 
    882 	c = priv;
    883 	sc = c->axe_sc;
    884 	ifp = GET_IFP(sc);
    885 
    886 	if (sc->axe_dying)
    887 		return;
    888 
    889 	s = splnet();
    890 
    891 	if (status != USBD_NORMAL_COMPLETION) {
    892 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
    893 			splx(s);
    894 			return;
    895 		}
    896 		ifp->if_oerrors++;
    897 		printf("%s: usb error on tx: %s\n", device_xname(sc->axe_dev),
    898 		    usbd_errstr(status));
    899 		if (status == USBD_STALLED)
    900 			usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_TX]);
    901 		splx(s);
    902 		return;
    903 	}
    904 
    905 	ifp->if_timer = 0;
    906 	ifp->if_flags &= ~IFF_OACTIVE;
    907 
    908 	m_freem(c->axe_mbuf);
    909 	c->axe_mbuf = NULL;
    910 
    911 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    912 		axe_start(ifp);
    913 
    914 	ifp->if_opackets++;
    915 	splx(s);
    916 	return;
    917 }
    918 
    919 Static void
    920 axe_tick(void *xsc)
    921 {
    922 	struct axe_softc *sc = xsc;
    923 
    924 	if (sc == NULL)
    925 		return;
    926 
    927 	DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->axe_dev),
    928 			__func__));
    929 
    930 	if (sc->axe_dying)
    931 		return;
    932 
    933 	/* Perform periodic stuff in process context */
    934 	usb_add_task(sc->axe_udev, &sc->axe_tick_task, USB_TASKQ_DRIVER);
    935 
    936 }
    937 
    938 Static void
    939 axe_tick_task(void *xsc)
    940 {
    941 	int			s;
    942 	struct axe_softc	*sc;
    943 	struct ifnet		*ifp;
    944 	struct mii_data		*mii;
    945 
    946 	sc = xsc;
    947 
    948 	if (sc == NULL)
    949 		return;
    950 
    951 	if (sc->axe_dying)
    952 		return;
    953 
    954 	ifp = GET_IFP(sc);
    955 	mii = GET_MII(sc);
    956 	if (mii == NULL)
    957 		return;
    958 
    959 	s = splnet();
    960 
    961 	mii_tick(mii);
    962 
    963 	callout_reset(&(sc->axe_stat_ch), (hz), (axe_tick), (sc));
    964 
    965 	splx(s);
    966 }
    967 
    968 Static int
    969 axe_encap(struct axe_softc *sc, struct mbuf *m, int idx)
    970 {
    971 	struct axe_chain	*c;
    972 	usbd_status		err;
    973 
    974 	c = &sc->axe_cdata.axe_tx_chain[idx];
    975 
    976 	/*
    977 	 * Copy the mbuf data into a contiguous buffer, leaving two
    978 	 * bytes at the beginning to hold the frame length.
    979 	 */
    980 	m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf);
    981 	c->axe_mbuf = m;
    982 
    983 	usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_TX],
    984 	    c, c->axe_buf, m->m_pkthdr.len, USBD_FORCE_SHORT_XFER, 10000,
    985 	    axe_txeof);
    986 
    987 	/* Transmit */
    988 	err = usbd_transfer(c->axe_xfer);
    989 	if (err != USBD_IN_PROGRESS) {
    990 		axe_stop(sc);
    991 		return(EIO);
    992 	}
    993 
    994 	sc->axe_cdata.axe_tx_cnt++;
    995 
    996 	return(0);
    997 }
    998 
    999 Static void
   1000 axe_start(struct ifnet *ifp)
   1001 {
   1002 	struct axe_softc	*sc;
   1003 	struct mbuf		*m_head = NULL;
   1004 
   1005 	sc = ifp->if_softc;
   1006 
   1007 	if ((ifp->if_flags & (IFF_OACTIVE|IFF_RUNNING)) != IFF_RUNNING)
   1008 		return;
   1009 
   1010 	IF_DEQUEUE(&ifp->if_snd, m_head);
   1011 	if (m_head == NULL) {
   1012 		return;
   1013 	}
   1014 
   1015 	if (axe_encap(sc, m_head, 0)) {
   1016 		IF_PREPEND(&ifp->if_snd, m_head);
   1017 		ifp->if_flags |= IFF_OACTIVE;
   1018 		return;
   1019 	}
   1020 
   1021 	/*
   1022 	 * If there's a BPF listener, bounce a copy of this frame
   1023 	 * to him.
   1024 	 */
   1025 #if NBPFILTER > 0
   1026 	 if (ifp->if_bpf)
   1027 	 	BPF_MTAP(ifp, m_head);
   1028 #endif
   1029 
   1030 	ifp->if_flags |= IFF_OACTIVE;
   1031 
   1032 	/*
   1033 	 * Set a timeout in case the chip goes out to lunch.
   1034 	 */
   1035 	ifp->if_timer = 5;
   1036 
   1037 	return;
   1038 }
   1039 
   1040 Static void
   1041 axe_init(void *xsc)
   1042 {
   1043 	struct axe_softc	*sc = xsc;
   1044 	struct ifnet		*ifp = GET_IFP(sc);
   1045 	struct axe_chain	*c;
   1046 	usbd_status		err;
   1047 	int			rxmode;
   1048 	int			i, s;
   1049 
   1050 	if (ifp->if_flags & IFF_RUNNING)
   1051 		return;
   1052 
   1053 	s = splnet();
   1054 
   1055 	/*
   1056 	 * Cancel pending I/O and free all RX/TX buffers.
   1057 	 */
   1058 	axe_reset(sc);
   1059 
   1060 	/* Enable RX logic. */
   1061 
   1062 	/* Init RX ring. */
   1063 	if (axe_rx_list_init(sc) == ENOBUFS) {
   1064 		printf("%s: rx list init failed\n", device_xname(sc->axe_dev));
   1065 		splx(s);
   1066 		return;
   1067 	}
   1068 
   1069 	/* Init TX ring. */
   1070 	if (axe_tx_list_init(sc) == ENOBUFS) {
   1071 		printf("%s: tx list init failed\n", device_xname(sc->axe_dev));
   1072 		splx(s);
   1073 		return;
   1074 	}
   1075 
   1076 	/* Set transmitter IPG values */
   1077 	axe_lock_mii(sc);
   1078 	axe_cmd(sc, AXE_CMD_WRITE_IPG0, 0, sc->axe_ipgs[0], NULL);
   1079 	axe_cmd(sc, AXE_CMD_WRITE_IPG1, 0, sc->axe_ipgs[1], NULL);
   1080 	axe_cmd(sc, AXE_CMD_WRITE_IPG2, 0, sc->axe_ipgs[2], NULL);
   1081 
   1082 	/* Enable receiver, set RX mode */
   1083 	rxmode = AXE_RXCMD_UNICAST|AXE_RXCMD_MULTICAST|AXE_RXCMD_ENABLE;
   1084 
   1085 	/* If we want promiscuous mode, set the allframes bit. */
   1086 	if (ifp->if_flags & IFF_PROMISC)
   1087 		rxmode |= AXE_RXCMD_PROMISC;
   1088 
   1089 	if (ifp->if_flags & IFF_BROADCAST)
   1090 		rxmode |= AXE_RXCMD_BROADCAST;
   1091 
   1092 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
   1093 	axe_unlock_mii(sc);
   1094 
   1095 	/* Load the multicast filter. */
   1096 	axe_setmulti(sc);
   1097 
   1098 	/* Open RX and TX pipes. */
   1099 	err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_RX],
   1100 	    USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_RX]);
   1101 	if (err) {
   1102 		printf("%s: open rx pipe failed: %s\n",
   1103 		    device_xname(sc->axe_dev), usbd_errstr(err));
   1104 		splx(s);
   1105 		return;
   1106 	}
   1107 
   1108 	err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_TX],
   1109 	    USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_TX]);
   1110 	if (err) {
   1111 		printf("%s: open tx pipe failed: %s\n",
   1112 		    device_xname(sc->axe_dev), usbd_errstr(err));
   1113 		splx(s);
   1114 		return;
   1115 	}
   1116 
   1117 	/* Start up the receive pipe. */
   1118 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
   1119 		c = &sc->axe_cdata.axe_rx_chain[i];
   1120 		usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
   1121 		    c, mtod(c->axe_mbuf, char *), AXE_BUFSZ,
   1122 		    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, axe_rxeof);
   1123 		usbd_transfer(c->axe_xfer);
   1124 	}
   1125 
   1126 	ifp->if_flags |= IFF_RUNNING;
   1127 	ifp->if_flags &= ~IFF_OACTIVE;
   1128 
   1129 	splx(s);
   1130 
   1131 	callout_init(&(sc->axe_stat_ch), 0);
   1132 	callout_reset(&(sc->axe_stat_ch), (hz), (axe_tick), (sc));
   1133 	return;
   1134 }
   1135 
   1136 Static int
   1137 axe_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1138 {
   1139 	struct axe_softc	*sc = ifp->if_softc;
   1140 	struct ifreq		*ifr = (struct ifreq *)data;
   1141 	struct ifaddr		*ifa = (struct ifaddr *)data;
   1142 	u_int16_t		rxmode;
   1143 	int			error = 0;
   1144 
   1145 	switch(cmd) {
   1146 	case SIOCINITIFADDR:
   1147 		ifp->if_flags |= IFF_UP;
   1148 		axe_init(sc);
   1149 
   1150 		switch (ifa->ifa_addr->sa_family) {
   1151 #ifdef INET
   1152 		case AF_INET:
   1153 #if defined(__NetBSD__)
   1154 			arp_ifinit(ifp, ifa);
   1155 #else
   1156 			arp_ifinit(&sc->arpcom, ifa);
   1157 #endif
   1158 			break;
   1159 #endif /* INET */
   1160 		}
   1161 		break;
   1162 
   1163 	case SIOCSIFMTU:
   1164 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU)
   1165 			error = EINVAL;
   1166 		else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
   1167 			error = 0;
   1168 		break;
   1169 
   1170 	case SIOCSIFFLAGS:
   1171 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1172 			break;
   1173 		if (ifp->if_flags & IFF_UP) {
   1174 			if (ifp->if_flags & IFF_RUNNING &&
   1175 			    ifp->if_flags & IFF_PROMISC &&
   1176 			    !(sc->axe_if_flags & IFF_PROMISC)) {
   1177 
   1178 				axe_lock_mii(sc);
   1179 				axe_cmd(sc, AXE_CMD_RXCTL_READ,
   1180 					0, 0, (void *)&rxmode);
   1181 				rxmode = le16toh(rxmode) | AXE_RXCMD_PROMISC;
   1182 				axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
   1183 					0, rxmode, NULL);
   1184 				axe_unlock_mii(sc);
   1185 
   1186 				axe_setmulti(sc);
   1187 			} else if (ifp->if_flags & IFF_RUNNING &&
   1188 			    !(ifp->if_flags & IFF_PROMISC) &&
   1189 			    sc->axe_if_flags & IFF_PROMISC) {
   1190 				axe_lock_mii(sc);
   1191 				axe_cmd(sc, AXE_CMD_RXCTL_READ,
   1192 					0, 0, (void *)&rxmode);
   1193 				rxmode = le16toh(rxmode) & ~AXE_RXCMD_PROMISC;
   1194 				axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
   1195 					0, rxmode, NULL);
   1196 				axe_unlock_mii(sc);
   1197 				axe_setmulti(sc);
   1198 			} else if (!(ifp->if_flags & IFF_RUNNING))
   1199 				axe_init(sc);
   1200 		} else {
   1201 			if (ifp->if_flags & IFF_RUNNING)
   1202 				axe_stop(sc);
   1203 		}
   1204 		sc->axe_if_flags = ifp->if_flags;
   1205 		error = 0;
   1206 		break;
   1207 	case SIOCADDMULTI:
   1208 	case SIOCDELMULTI:
   1209 	case SIOCGIFMEDIA:
   1210 	case SIOCSIFMEDIA:
   1211 		error = ether_ioctl(ifp, cmd, data);
   1212 		if (error == ENETRESET) {
   1213 			/*
   1214 			 * Multicast list has changed; set the hardware
   1215 			 * filter accordingly.
   1216 			 */
   1217 			if (ifp->if_flags & IFF_RUNNING)
   1218 				axe_setmulti(sc);
   1219 			error = 0;
   1220 		}
   1221 		break;
   1222 	default:
   1223 		error = ether_ioctl(ifp, cmd, data);
   1224 		break;
   1225 	}
   1226 
   1227 	return(error);
   1228 }
   1229 
   1230 /*
   1231  * XXX
   1232  * You can't call axe_txeof since the USB transfer has not
   1233  * completed yet.
   1234  */
   1235 Static void
   1236 axe_watchdog(struct ifnet *ifp)
   1237 {
   1238 	struct axe_softc	*sc;
   1239 	struct axe_chain	*c;
   1240 	usbd_status		stat;
   1241 	int			s;
   1242 
   1243 	sc = ifp->if_softc;
   1244 
   1245 	ifp->if_oerrors++;
   1246 	printf("%s: watchdog timeout\n", device_xname(sc->axe_dev));
   1247 
   1248 	s = splusb();
   1249 	c = &sc->axe_cdata.axe_tx_chain[0];
   1250 	usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &stat);
   1251 	axe_txeof(c->axe_xfer, c, stat);
   1252 
   1253 	if (ifp->if_snd.ifq_head != NULL)
   1254 		axe_start(ifp);
   1255 	splx(s);
   1256 }
   1257 
   1258 /*
   1259  * Stop the adapter and free any mbufs allocated to the
   1260  * RX and TX lists.
   1261  */
   1262 Static void
   1263 axe_stop(struct axe_softc *sc)
   1264 {
   1265 	usbd_status		err;
   1266 	struct ifnet		*ifp;
   1267 	int			i;
   1268 
   1269 	axe_reset(sc);
   1270 
   1271 	ifp = GET_IFP(sc);
   1272 	ifp->if_timer = 0;
   1273 
   1274 	callout_stop(&(sc->axe_stat_ch));
   1275 
   1276 	/* Stop transfers. */
   1277 	if (sc->axe_ep[AXE_ENDPT_RX] != NULL) {
   1278 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
   1279 		if (err) {
   1280 			printf("%s: abort rx pipe failed: %s\n",
   1281 			    device_xname(sc->axe_dev), usbd_errstr(err));
   1282 		}
   1283 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_RX]);
   1284 		if (err) {
   1285 			printf("%s: close rx pipe failed: %s\n",
   1286 			    device_xname(sc->axe_dev), usbd_errstr(err));
   1287 		}
   1288 		sc->axe_ep[AXE_ENDPT_RX] = NULL;
   1289 	}
   1290 
   1291 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL) {
   1292 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
   1293 		if (err) {
   1294 			printf("%s: abort tx pipe failed: %s\n",
   1295 			    device_xname(sc->axe_dev), usbd_errstr(err));
   1296 		}
   1297 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_TX]);
   1298 		if (err) {
   1299 			printf("%s: close tx pipe failed: %s\n",
   1300 			    device_xname(sc->axe_dev), usbd_errstr(err));
   1301 		}
   1302 		sc->axe_ep[AXE_ENDPT_TX] = NULL;
   1303 	}
   1304 
   1305 	if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) {
   1306 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
   1307 		if (err) {
   1308 			printf("%s: abort intr pipe failed: %s\n",
   1309 			    device_xname(sc->axe_dev), usbd_errstr(err));
   1310 		}
   1311 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
   1312 		if (err) {
   1313 			printf("%s: close intr pipe failed: %s\n",
   1314 			    device_xname(sc->axe_dev), usbd_errstr(err));
   1315 		}
   1316 		sc->axe_ep[AXE_ENDPT_INTR] = NULL;
   1317 	}
   1318 
   1319 	/* Free RX resources. */
   1320 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
   1321 		if (sc->axe_cdata.axe_rx_chain[i].axe_mbuf != NULL) {
   1322 			m_freem(sc->axe_cdata.axe_rx_chain[i].axe_mbuf);
   1323 			sc->axe_cdata.axe_rx_chain[i].axe_mbuf = NULL;
   1324 		}
   1325 		if (sc->axe_cdata.axe_rx_chain[i].axe_xfer != NULL) {
   1326 			usbd_free_xfer(sc->axe_cdata.axe_rx_chain[i].axe_xfer);
   1327 			sc->axe_cdata.axe_rx_chain[i].axe_xfer = NULL;
   1328 		}
   1329 	}
   1330 
   1331 	/* Free TX resources. */
   1332 	for (i = 0; i < AXE_TX_LIST_CNT; i++) {
   1333 		if (sc->axe_cdata.axe_tx_chain[i].axe_mbuf != NULL) {
   1334 			m_freem(sc->axe_cdata.axe_tx_chain[i].axe_mbuf);
   1335 			sc->axe_cdata.axe_tx_chain[i].axe_mbuf = NULL;
   1336 		}
   1337 		if (sc->axe_cdata.axe_tx_chain[i].axe_xfer != NULL) {
   1338 			usbd_free_xfer(sc->axe_cdata.axe_tx_chain[i].axe_xfer);
   1339 			sc->axe_cdata.axe_tx_chain[i].axe_xfer = NULL;
   1340 		}
   1341 	}
   1342 
   1343 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1344 }
   1345 
   1346