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