Home | History | Annotate | Line # | Download | only in usb
if_axe.c revision 1.26
      1 /*	$NetBSD: if_axe.c,v 1.26 2008/11/07 00:20:12 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.26 2008/11/07 00:20:12 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 USB_DECLARE_DRIVER(axe);
    168 
    169 Static int axe_tx_list_init(struct axe_softc *);
    170 Static int axe_rx_list_init(struct axe_softc *);
    171 Static int axe_newbuf(struct axe_softc *, struct axe_chain *, struct mbuf *);
    172 Static int axe_encap(struct axe_softc *, struct mbuf *, int);
    173 Static void axe_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
    174 Static void axe_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
    175 Static void axe_tick(void *);
    176 Static void axe_tick_task(void *);
    177 #if 0
    178 Static void axe_rxstart(struct ifnet *);
    179 #endif
    180 Static void axe_start(struct ifnet *);
    181 Static int axe_ioctl(struct ifnet *, u_long, void *);
    182 Static void axe_init(void *);
    183 Static void axe_stop(struct axe_softc *);
    184 Static void axe_watchdog(struct ifnet *);
    185 Static int axe_miibus_readreg(device_ptr_t, int, int);
    186 Static void axe_miibus_writereg(device_ptr_t, int, int, int);
    187 Static void axe_miibus_statchg(device_ptr_t);
    188 Static int axe_cmd(struct axe_softc *, int, int, int, void *);
    189 Static void axe_reset(struct axe_softc *sc);
    190 
    191 Static void axe_setmulti(struct axe_softc *);
    192 Static void axe_lock_mii(struct axe_softc *sc);
    193 Static void axe_unlock_mii(struct axe_softc *sc);
    194 
    195 /* Get exclusive access to the MII registers */
    196 Static void
    197 axe_lock_mii(struct axe_softc *sc)
    198 {
    199 	sc->axe_refcnt++;
    200 	mutex_enter(&sc->axe_mii_lock);
    201 }
    202 
    203 Static void
    204 axe_unlock_mii(struct axe_softc *sc)
    205 {
    206 	mutex_exit(&sc->axe_mii_lock);
    207 	if (--sc->axe_refcnt < 0)
    208 		usb_detach_wakeup(USBDEV(sc->axe_dev));
    209 }
    210 
    211 Static int
    212 axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
    213 {
    214 	usb_device_request_t	req;
    215 	usbd_status		err;
    216 
    217 	KASSERT(mutex_owned(&sc->axe_mii_lock));
    218 
    219 	if (sc->axe_dying)
    220 		return(0);
    221 
    222 	if (AXE_CMD_DIR(cmd))
    223 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    224 	else
    225 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
    226 	req.bRequest = AXE_CMD_CMD(cmd);
    227 	USETW(req.wValue, val);
    228 	USETW(req.wIndex, index);
    229 	USETW(req.wLength, AXE_CMD_LEN(cmd));
    230 
    231 	err = usbd_do_request(sc->axe_udev, &req, buf);
    232 
    233 	if (err)
    234 		return(-1);
    235 
    236 	return(0);
    237 }
    238 
    239 Static int
    240 axe_miibus_readreg(device_ptr_t dev, int phy, int reg)
    241 {
    242 	struct axe_softc	*sc = USBGETSOFTC(dev);
    243 	usbd_status		err;
    244 	u_int16_t		val;
    245 
    246 	if (sc->axe_dying) {
    247 		DPRINTF(("axe: dying\n"));
    248 		return(0);
    249 	}
    250 
    251 #ifdef notdef
    252 	/*
    253 	 * The chip tells us the MII address of any supported
    254 	 * PHYs attached to the chip, so only read from those.
    255 	 */
    256 
    257 	if (sc->axe_phyaddrs[0] != AXE_NOPHY && phy != sc->axe_phyaddrs[0])
    258 		return (0);
    259 
    260 	if (sc->axe_phyaddrs[1] != AXE_NOPHY && phy != sc->axe_phyaddrs[1])
    261 		return (0);
    262 #endif
    263 	if (sc->axe_phyaddrs[0] != 0xFF && sc->axe_phyaddrs[0] != phy)
    264 		return (0);
    265 
    266 	val = 0;
    267 
    268 	axe_lock_mii(sc);
    269 	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
    270 	err = axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, (void *)&val);
    271 	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
    272 	axe_unlock_mii(sc);
    273 
    274 	if (err) {
    275 		aprint_error_dev(sc->axe_dev, "read PHY failed\n");
    276 		return(-1);
    277 	}
    278 
    279 	if (val)
    280 		sc->axe_phyaddrs[0] = phy;
    281 
    282 	return (le16toh(val));
    283 }
    284 
    285 Static void
    286 axe_miibus_writereg(device_ptr_t dev, int phy, int reg, int aval)
    287 {
    288 	struct axe_softc	*sc = USBGETSOFTC(dev);
    289 	usbd_status		err;
    290 	u_int16_t		val;
    291 
    292 	if (sc->axe_dying)
    293 		return;
    294 
    295 	val = htole16(aval);
    296 	axe_lock_mii(sc);
    297 	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
    298 	err = axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, (void *)&val);
    299 	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
    300 	axe_unlock_mii(sc);
    301 
    302 	if (err) {
    303 		aprint_error_dev(sc->axe_dev, "write PHY failed\n");
    304 		return;
    305 	}
    306 }
    307 
    308 Static void
    309 axe_miibus_statchg(device_ptr_t dev)
    310 {
    311 	struct axe_softc	*sc = USBGETSOFTC(dev);
    312 	struct mii_data		*mii = GET_MII(sc);
    313 	int val, err;
    314 
    315 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
    316 		val = AXE_MEDIA_FULL_DUPLEX;
    317 	else
    318 		val = 0;
    319 	DPRINTF(("axe_miibus_statchg: val=0x%x\n", val));
    320 	axe_lock_mii(sc);
    321 	err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL);
    322 	axe_unlock_mii(sc);
    323 	if (err) {
    324 		aprint_error_dev(sc->axe_dev, "media change failed\n");
    325 		return;
    326 	}
    327 }
    328 
    329 Static void
    330 axe_setmulti(struct axe_softc *sc)
    331 {
    332 	struct ifnet		*ifp;
    333 	struct ether_multi *enm;
    334 	struct ether_multistep step;
    335 	u_int32_t		h = 0;
    336 	u_int16_t		rxmode;
    337 	u_int8_t		hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
    338 
    339 	if (sc->axe_dying)
    340 		return;
    341 
    342 	ifp = GET_IFP(sc);
    343 
    344 	axe_lock_mii(sc);
    345 	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, (void *)&rxmode);
    346 	rxmode = le16toh(rxmode);
    347 
    348 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
    349 	allmulti:
    350 		rxmode |= AXE_RXCMD_ALLMULTI;
    351 		axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
    352 		return;
    353 	} else
    354 		rxmode &= ~AXE_RXCMD_ALLMULTI;
    355 
    356 	/* now program new ones */
    357 #if defined(__NetBSD__)
    358 	ETHER_FIRST_MULTI(step, &sc->axe_ec, enm);
    359 #else
    360 	ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
    361 #endif
    362 	while (enm != NULL) {
    363 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    364 			   ETHER_ADDR_LEN) != 0)
    365 			goto allmulti;
    366 
    367 		h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26;
    368 		hashtbl[h / 8] |= 1 << (h % 8);
    369 		ETHER_NEXT_MULTI(step, enm);
    370 	}
    371 
    372 	ifp->if_flags &= ~IFF_ALLMULTI;
    373 	axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
    374 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
    375 	axe_unlock_mii(sc);
    376 	return;
    377 }
    378 
    379 Static void
    380 axe_reset(struct axe_softc *sc)
    381 {
    382 	if (sc->axe_dying)
    383 		return;
    384 	/* XXX What to reset? */
    385 
    386 	/* Wait a little while for the chip to get its brains in order. */
    387 	DELAY(1000);
    388 	return;
    389 }
    390 
    391 /*
    392  * Probe for a AX88172 chip.
    393  */
    394 USB_MATCH(axe)
    395 {
    396 	USB_MATCH_START(axe, uaa);
    397 
    398 	return (axe_lookup(uaa->vendor, uaa->product) != NULL ?
    399 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    400 }
    401 
    402 /*
    403  * Attach the interface. Allocate softc structures, do ifmedia
    404  * setup and ethernet/BPF attach.
    405  */
    406 USB_ATTACH(axe)
    407 {
    408 	USB_ATTACH_START(axe, sc, uaa);
    409 	usbd_device_handle dev = uaa->device;
    410 	usbd_status err;
    411 	usb_interface_descriptor_t *id;
    412 	usb_endpoint_descriptor_t *ed;
    413 	struct mii_data	*mii;
    414 	u_char eaddr[ETHER_ADDR_LEN];
    415 	char *devinfop;
    416 	const char *devname = device_xname(self);
    417 	struct ifnet *ifp;
    418 	int i, s;
    419 
    420 	devinfop = usbd_devinfo_alloc(dev, 0);
    421 	USB_ATTACH_SETUP;
    422 	sc->axe_dev = self;
    423 
    424 	err = usbd_set_config_no(dev, AXE_CONFIG_NO, 1);
    425 	if (err) {
    426 		aprint_error_dev(self, "getting interface handle failed\n");
    427                 usbd_devinfo_free(devinfop);
    428 		USB_ATTACH_ERROR_RETURN;
    429 	}
    430 
    431 	usb_init_task(&sc->axe_tick_task, axe_tick_task, sc);
    432 	mutex_init(&sc->axe_mii_lock, MUTEX_DEFAULT, IPL_NONE);
    433 	usb_init_task(&sc->axe_stop_task, (void (*)(void *))axe_stop, sc);
    434 
    435 	err = usbd_device2interface_handle(dev, AXE_IFACE_IDX, &sc->axe_iface);
    436 	if (err) {
    437 		aprint_error_dev(self, "getting interface handle failed\n");
    438                 usbd_devinfo_free(devinfop);
    439 		USB_ATTACH_ERROR_RETURN;
    440 	}
    441 
    442 	sc->axe_udev = dev;
    443 	sc->axe_product = uaa->product;
    444 	sc->axe_vendor = uaa->vendor;
    445 
    446 	id = usbd_get_interface_descriptor(sc->axe_iface);
    447 
    448 	aprint_normal_dev(self, "%s\n", devinfop);
    449 	usbd_devinfo_free(devinfop);
    450 
    451 	/* Find endpoints. */
    452 	for (i = 0; i < id->bNumEndpoints; i++) {
    453 		ed = usbd_interface2endpoint_descriptor(sc->axe_iface, i);
    454 		if (!ed) {
    455 			aprint_error_dev(self, "couldn't get ep %d\n", i);
    456 			USB_ATTACH_ERROR_RETURN;
    457 		}
    458 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    459 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    460 			sc->axe_ed[AXE_ENDPT_RX] = ed->bEndpointAddress;
    461 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    462 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    463 			sc->axe_ed[AXE_ENDPT_TX] = ed->bEndpointAddress;
    464 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    465 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    466 			sc->axe_ed[AXE_ENDPT_INTR] = ed->bEndpointAddress;
    467 		}
    468 	}
    469 
    470 	s = splnet();
    471 
    472 	/*
    473 	 * Get station address.
    474 	 */
    475 	axe_lock_mii(sc);
    476 	axe_cmd(sc, AXE_CMD_READ_NODEID, 0, 0, &eaddr);
    477 
    478 	/*
    479 	 * Load IPG values and PHY indexes.
    480 	 */
    481 	axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, (void *)&sc->axe_ipgs);
    482 	axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, (void *)&sc->axe_phyaddrs);
    483 	axe_unlock_mii(sc);
    484 
    485 	/*
    486 	 * Work around broken adapters that appear to lie about
    487 	 * their PHY addresses.
    488 	 */
    489 	sc->axe_phyaddrs[0] = sc->axe_phyaddrs[1] = 0xFF;
    490 
    491 	/*
    492 	 * An ASIX chip was detected. Inform the world.
    493 	 */
    494 	aprint_normal_dev(self, "Ethernet address %s\n",
    495 	    ether_sprintf(eaddr));
    496 
    497 	/* Initialize interface info.*/
    498 	ifp = GET_IFP(sc);
    499 	ifp->if_softc = sc;
    500 	strncpy(ifp->if_xname, devname, IFNAMSIZ);
    501 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    502 	ifp->if_ioctl = axe_ioctl;
    503 	ifp->if_start = axe_start;
    504 
    505 	ifp->if_watchdog = axe_watchdog;
    506 
    507 /*	ifp->if_baudrate = 10000000; */
    508 /*	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;*/
    509 
    510 	IFQ_SET_READY(&ifp->if_snd);
    511 
    512 	/* Initialize MII/media info. */
    513 	mii = &sc->axe_mii;
    514 	mii->mii_ifp = ifp;
    515 	mii->mii_readreg = axe_miibus_readreg;
    516 	mii->mii_writereg = axe_miibus_writereg;
    517 	mii->mii_statchg = axe_miibus_statchg;
    518 	mii->mii_flags = MIIF_AUTOTSLEEP;
    519 
    520 	sc->axe_ec.ec_mii = mii;
    521 	ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
    522 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
    523 
    524 	if (LIST_EMPTY(&mii->mii_phys)) {
    525 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
    526 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
    527 	} else
    528 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    529 
    530 	/* Attach the interface. */
    531 	if_attach(ifp);
    532 	Ether_ifattach(ifp, eaddr);
    533 #if NRND > 0
    534 	rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->axe_dev),
    535 	    RND_TYPE_NET, 0);
    536 #endif
    537 
    538 	usb_callout_init(sc->axe_stat_ch);
    539 
    540 	sc->axe_attached = 1;
    541 	splx(s);
    542 
    543 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->axe_udev,
    544 			   USBDEV(sc->axe_dev));
    545 
    546 	USB_ATTACH_SUCCESS_RETURN;
    547 }
    548 
    549 USB_DETACH(axe)
    550 {
    551 	USB_DETACH_START(axe, sc);
    552 	int			s;
    553 	struct ifnet		*ifp = GET_IFP(sc);
    554 
    555 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
    556 
    557 	/* Detached before attached finished, so just bail out. */
    558 	if (!sc->axe_attached)
    559 		return (0);
    560 
    561 	usb_uncallout(sc->axe_stat_ch, axe_tick, sc);
    562 
    563 	sc->axe_dying = 1;
    564 
    565 	ether_ifdetach(ifp);
    566 
    567 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL)
    568 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
    569 	if (sc->axe_ep[AXE_ENDPT_RX] != NULL)
    570 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
    571 	if (sc->axe_ep[AXE_ENDPT_INTR] != NULL)
    572 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
    573 
    574 	/*
    575 	 * Remove any pending tasks.  They cannot be executing because they run
    576 	 * in the same thread as detach.
    577 	 */
    578 	usb_rem_task(sc->axe_udev, &sc->axe_tick_task);
    579 	usb_rem_task(sc->axe_udev, &sc->axe_stop_task);
    580 
    581 	s = splusb();
    582 
    583 	if (--sc->axe_refcnt >= 0) {
    584 		/* Wait for processes to go away */
    585 		usb_detach_wait(USBDEV(sc->axe_dev));
    586 	}
    587 
    588 	if (ifp->if_flags & IFF_RUNNING)
    589 		axe_stop(sc);
    590 
    591 #if defined(__NetBSD__)
    592 #if NRND > 0
    593 	rnd_detach_source(&sc->rnd_source);
    594 #endif
    595 #endif /* __NetBSD__ */
    596 	mii_detach(&sc->axe_mii, MII_PHY_ANY, MII_OFFSET_ANY);
    597 	ifmedia_delete_instance(&sc->axe_mii.mii_media, IFM_INST_ANY);
    598 	ether_ifdetach(ifp);
    599 	if_detach(ifp);
    600 
    601 #ifdef DIAGNOSTIC
    602 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL ||
    603 	    sc->axe_ep[AXE_ENDPT_RX] != NULL ||
    604 	    sc->axe_ep[AXE_ENDPT_INTR] != NULL)
    605 		aprint_debug_dev(self, "detach has active endpoints\n");
    606 #endif
    607 
    608 	sc->axe_attached = 0;
    609 
    610 	if (--sc->axe_refcnt >= 0) {
    611 		/* Wait for processes to go away. */
    612 		usb_detach_wait(USBDEV(sc->axe_dev));
    613 	}
    614 	splx(s);
    615 
    616 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->axe_udev,
    617 			   USBDEV(sc->axe_dev));
    618 
    619 	return (0);
    620 }
    621 
    622 int
    623 axe_activate(device_ptr_t self, enum devact act)
    624 {
    625 	struct axe_softc *sc = device_private(self);
    626 
    627 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
    628 
    629 	switch (act) {
    630 	case DVACT_ACTIVATE:
    631 		return (EOPNOTSUPP);
    632 		break;
    633 
    634 	case DVACT_DEACTIVATE:
    635 		if_deactivate(&sc->axe_ec.ec_if);
    636 		sc->axe_dying = 1;
    637 		break;
    638 	}
    639 	return (0);
    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 			    USBDEVNAME(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 #if NBPFILTER > 0
    835 	if (ifp->if_bpf)
    836 		BPF_MTAP(ifp, m);
    837 #endif
    838 
    839 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->axe_dev),
    840 		    __func__, m->m_len));
    841 	IF_INPUT(ifp, m);
    842  done1:
    843 	splx(s);
    844 
    845  done:
    846 
    847 	/* Setup new transfer. */
    848 	usbd_setup_xfer(xfer, sc->axe_ep[AXE_ENDPT_RX],
    849 	    c, c->axe_buf, AXE_BUFSZ,
    850 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
    851 	    USBD_NO_TIMEOUT, axe_rxeof);
    852 	usbd_transfer(xfer);
    853 
    854 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->axe_dev),
    855 		    __func__));
    856 	return;
    857 }
    858 
    859 /*
    860  * A frame was downloaded to the chip. It's safe for us to clean up
    861  * the list buffers.
    862  */
    863 
    864 Static void
    865 axe_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
    866     usbd_status status)
    867 {
    868 	struct axe_softc	*sc;
    869 	struct axe_chain	*c;
    870 	struct ifnet		*ifp;
    871 	int			s;
    872 
    873 	c = priv;
    874 	sc = c->axe_sc;
    875 	ifp = GET_IFP(sc);
    876 
    877 	if (sc->axe_dying)
    878 		return;
    879 
    880 	s = splnet();
    881 
    882 	if (status != USBD_NORMAL_COMPLETION) {
    883 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
    884 			splx(s);
    885 			return;
    886 		}
    887 		ifp->if_oerrors++;
    888 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->axe_dev),
    889 		    usbd_errstr(status));
    890 		if (status == USBD_STALLED)
    891 			usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_TX]);
    892 		splx(s);
    893 		return;
    894 	}
    895 
    896 	ifp->if_timer = 0;
    897 	ifp->if_flags &= ~IFF_OACTIVE;
    898 
    899 	m_freem(c->axe_mbuf);
    900 	c->axe_mbuf = NULL;
    901 
    902 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    903 		axe_start(ifp);
    904 
    905 	ifp->if_opackets++;
    906 	splx(s);
    907 	return;
    908 }
    909 
    910 Static void
    911 axe_tick(void *xsc)
    912 {
    913 	struct axe_softc *sc = xsc;
    914 
    915 	if (sc == NULL)
    916 		return;
    917 
    918 	DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->axe_dev),
    919 			__func__));
    920 
    921 	if (sc->axe_dying)
    922 		return;
    923 
    924 	/* Perform periodic stuff in process context */
    925 	usb_add_task(sc->axe_udev, &sc->axe_tick_task, USB_TASKQ_DRIVER);
    926 
    927 }
    928 
    929 Static void
    930 axe_tick_task(void *xsc)
    931 {
    932 	int			s;
    933 	struct axe_softc	*sc;
    934 	struct ifnet		*ifp;
    935 	struct mii_data		*mii;
    936 
    937 	sc = xsc;
    938 
    939 	if (sc == NULL)
    940 		return;
    941 
    942 	if (sc->axe_dying)
    943 		return;
    944 
    945 	ifp = GET_IFP(sc);
    946 	mii = GET_MII(sc);
    947 	if (mii == NULL)
    948 		return;
    949 
    950 	s = splnet();
    951 
    952 	mii_tick(mii);
    953 
    954 	usb_callout(sc->axe_stat_ch, hz, axe_tick, sc);
    955 
    956 	splx(s);
    957 }
    958 
    959 Static int
    960 axe_encap(struct axe_softc *sc, struct mbuf *m, int idx)
    961 {
    962 	struct axe_chain	*c;
    963 	usbd_status		err;
    964 
    965 	c = &sc->axe_cdata.axe_tx_chain[idx];
    966 
    967 	/*
    968 	 * Copy the mbuf data into a contiguous buffer, leaving two
    969 	 * bytes at the beginning to hold the frame length.
    970 	 */
    971 	m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf);
    972 	c->axe_mbuf = m;
    973 
    974 	usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_TX],
    975 	    c, c->axe_buf, m->m_pkthdr.len, USBD_FORCE_SHORT_XFER, 10000,
    976 	    axe_txeof);
    977 
    978 	/* Transmit */
    979 	err = usbd_transfer(c->axe_xfer);
    980 	if (err != USBD_IN_PROGRESS) {
    981 		axe_stop(sc);
    982 		return(EIO);
    983 	}
    984 
    985 	sc->axe_cdata.axe_tx_cnt++;
    986 
    987 	return(0);
    988 }
    989 
    990 Static void
    991 axe_start(struct ifnet *ifp)
    992 {
    993 	struct axe_softc	*sc;
    994 	struct mbuf		*m_head = NULL;
    995 
    996 	sc = ifp->if_softc;
    997 
    998 	if ((ifp->if_flags & (IFF_OACTIVE|IFF_RUNNING)) != IFF_RUNNING)
    999 		return;
   1000 
   1001 	IF_DEQUEUE(&ifp->if_snd, m_head);
   1002 	if (m_head == NULL) {
   1003 		return;
   1004 	}
   1005 
   1006 	if (axe_encap(sc, m_head, 0)) {
   1007 		IF_PREPEND(&ifp->if_snd, m_head);
   1008 		ifp->if_flags |= IFF_OACTIVE;
   1009 		return;
   1010 	}
   1011 
   1012 	/*
   1013 	 * If there's a BPF listener, bounce a copy of this frame
   1014 	 * to him.
   1015 	 */
   1016 #if NBPFILTER > 0
   1017 	 if (ifp->if_bpf)
   1018 	 	BPF_MTAP(ifp, m_head);
   1019 #endif
   1020 
   1021 	ifp->if_flags |= IFF_OACTIVE;
   1022 
   1023 	/*
   1024 	 * Set a timeout in case the chip goes out to lunch.
   1025 	 */
   1026 	ifp->if_timer = 5;
   1027 
   1028 	return;
   1029 }
   1030 
   1031 Static void
   1032 axe_init(void *xsc)
   1033 {
   1034 	struct axe_softc	*sc = xsc;
   1035 	struct ifnet		*ifp = GET_IFP(sc);
   1036 	struct axe_chain	*c;
   1037 	usbd_status		err;
   1038 	int			rxmode;
   1039 	int			i, s;
   1040 
   1041 	if (ifp->if_flags & IFF_RUNNING)
   1042 		return;
   1043 
   1044 	s = splnet();
   1045 
   1046 	/*
   1047 	 * Cancel pending I/O and free all RX/TX buffers.
   1048 	 */
   1049 	axe_reset(sc);
   1050 
   1051 	/* Enable RX logic. */
   1052 
   1053 	/* Init RX ring. */
   1054 	if (axe_rx_list_init(sc) == ENOBUFS) {
   1055 		printf("%s: rx list init failed\n", USBDEVNAME(sc->axe_dev));
   1056 		splx(s);
   1057 		return;
   1058 	}
   1059 
   1060 	/* Init TX ring. */
   1061 	if (axe_tx_list_init(sc) == ENOBUFS) {
   1062 		printf("%s: tx list init failed\n", USBDEVNAME(sc->axe_dev));
   1063 		splx(s);
   1064 		return;
   1065 	}
   1066 
   1067 	/* Set transmitter IPG values */
   1068 	axe_lock_mii(sc);
   1069 	axe_cmd(sc, AXE_CMD_WRITE_IPG0, 0, sc->axe_ipgs[0], NULL);
   1070 	axe_cmd(sc, AXE_CMD_WRITE_IPG1, 0, sc->axe_ipgs[1], NULL);
   1071 	axe_cmd(sc, AXE_CMD_WRITE_IPG2, 0, sc->axe_ipgs[2], NULL);
   1072 
   1073 	/* Enable receiver, set RX mode */
   1074 	rxmode = AXE_RXCMD_UNICAST|AXE_RXCMD_MULTICAST|AXE_RXCMD_ENABLE;
   1075 
   1076 	/* If we want promiscuous mode, set the allframes bit. */
   1077 	if (ifp->if_flags & IFF_PROMISC)
   1078 		rxmode |= AXE_RXCMD_PROMISC;
   1079 
   1080 	if (ifp->if_flags & IFF_BROADCAST)
   1081 		rxmode |= AXE_RXCMD_BROADCAST;
   1082 
   1083 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
   1084 	axe_unlock_mii(sc);
   1085 
   1086 	/* Load the multicast filter. */
   1087 	axe_setmulti(sc);
   1088 
   1089 	/* Open RX and TX pipes. */
   1090 	err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_RX],
   1091 	    USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_RX]);
   1092 	if (err) {
   1093 		printf("%s: open rx pipe failed: %s\n",
   1094 		    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1095 		splx(s);
   1096 		return;
   1097 	}
   1098 
   1099 	err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_TX],
   1100 	    USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_TX]);
   1101 	if (err) {
   1102 		printf("%s: open tx pipe failed: %s\n",
   1103 		    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1104 		splx(s);
   1105 		return;
   1106 	}
   1107 
   1108 	/* Start up the receive pipe. */
   1109 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
   1110 		c = &sc->axe_cdata.axe_rx_chain[i];
   1111 		usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
   1112 		    c, mtod(c->axe_mbuf, char *), AXE_BUFSZ,
   1113 		    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, axe_rxeof);
   1114 		usbd_transfer(c->axe_xfer);
   1115 	}
   1116 
   1117 	ifp->if_flags |= IFF_RUNNING;
   1118 	ifp->if_flags &= ~IFF_OACTIVE;
   1119 
   1120 	splx(s);
   1121 
   1122 	usb_callout_init(sc->axe_stat_ch);
   1123 	usb_callout(sc->axe_stat_ch, hz, axe_tick, sc);
   1124 	return;
   1125 }
   1126 
   1127 Static int
   1128 axe_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1129 {
   1130 	struct axe_softc	*sc = ifp->if_softc;
   1131 	struct ifreq		*ifr = (struct ifreq *)data;
   1132 	struct ifaddr		*ifa = (struct ifaddr *)data;
   1133 	u_int16_t		rxmode;
   1134 	int			error = 0;
   1135 
   1136 	switch(cmd) {
   1137 	case SIOCINITIFADDR:
   1138 		ifp->if_flags |= IFF_UP;
   1139 		axe_init(sc);
   1140 
   1141 		switch (ifa->ifa_addr->sa_family) {
   1142 #ifdef INET
   1143 		case AF_INET:
   1144 #if defined(__NetBSD__)
   1145 			arp_ifinit(ifp, ifa);
   1146 #else
   1147 			arp_ifinit(&sc->arpcom, ifa);
   1148 #endif
   1149 			break;
   1150 #endif /* INET */
   1151 		}
   1152 		break;
   1153 
   1154 	case SIOCSIFMTU:
   1155 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU)
   1156 			error = EINVAL;
   1157 		else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
   1158 			error = 0;
   1159 		break;
   1160 
   1161 	case SIOCSIFFLAGS:
   1162 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1163 			break;
   1164 		if (ifp->if_flags & IFF_UP) {
   1165 			if (ifp->if_flags & IFF_RUNNING &&
   1166 			    ifp->if_flags & IFF_PROMISC &&
   1167 			    !(sc->axe_if_flags & IFF_PROMISC)) {
   1168 
   1169 				axe_lock_mii(sc);
   1170 				axe_cmd(sc, AXE_CMD_RXCTL_READ,
   1171 					0, 0, (void *)&rxmode);
   1172 				rxmode = le16toh(rxmode) | AXE_RXCMD_PROMISC;
   1173 				axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
   1174 					0, rxmode, NULL);
   1175 				axe_unlock_mii(sc);
   1176 
   1177 				axe_setmulti(sc);
   1178 			} else if (ifp->if_flags & IFF_RUNNING &&
   1179 			    !(ifp->if_flags & IFF_PROMISC) &&
   1180 			    sc->axe_if_flags & IFF_PROMISC) {
   1181 				axe_lock_mii(sc);
   1182 				axe_cmd(sc, AXE_CMD_RXCTL_READ,
   1183 					0, 0, (void *)&rxmode);
   1184 				rxmode = le16toh(rxmode) & ~AXE_RXCMD_PROMISC;
   1185 				axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
   1186 					0, rxmode, NULL);
   1187 				axe_unlock_mii(sc);
   1188 				axe_setmulti(sc);
   1189 			} else if (!(ifp->if_flags & IFF_RUNNING))
   1190 				axe_init(sc);
   1191 		} else {
   1192 			if (ifp->if_flags & IFF_RUNNING)
   1193 				axe_stop(sc);
   1194 		}
   1195 		sc->axe_if_flags = ifp->if_flags;
   1196 		error = 0;
   1197 		break;
   1198 	case SIOCADDMULTI:
   1199 	case SIOCDELMULTI:
   1200 	case SIOCGIFMEDIA:
   1201 	case SIOCSIFMEDIA:
   1202 		error = ether_ioctl(ifp, cmd, data);
   1203 		if (error == ENETRESET) {
   1204 			/*
   1205 			 * Multicast list has changed; set the hardware
   1206 			 * filter accordingly.
   1207 			 */
   1208 			if (ifp->if_flags & IFF_RUNNING)
   1209 				axe_setmulti(sc);
   1210 			error = 0;
   1211 		}
   1212 		break;
   1213 	default:
   1214 		error = ether_ioctl(ifp, cmd, data);
   1215 		break;
   1216 	}
   1217 
   1218 	return(error);
   1219 }
   1220 
   1221 /*
   1222  * XXX
   1223  * You can't call axe_txeof since the USB transfer has not
   1224  * completed yet.
   1225  */
   1226 Static void
   1227 axe_watchdog(struct ifnet *ifp)
   1228 {
   1229 	struct axe_softc	*sc;
   1230 	struct axe_chain	*c;
   1231 	usbd_status		stat;
   1232 	int			s;
   1233 
   1234 	sc = ifp->if_softc;
   1235 
   1236 	ifp->if_oerrors++;
   1237 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->axe_dev));
   1238 
   1239 	s = splusb();
   1240 	c = &sc->axe_cdata.axe_tx_chain[0];
   1241 	usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &stat);
   1242 	axe_txeof(c->axe_xfer, c, stat);
   1243 
   1244 	if (ifp->if_snd.ifq_head != NULL)
   1245 		axe_start(ifp);
   1246 	splx(s);
   1247 }
   1248 
   1249 /*
   1250  * Stop the adapter and free any mbufs allocated to the
   1251  * RX and TX lists.
   1252  */
   1253 Static void
   1254 axe_stop(struct axe_softc *sc)
   1255 {
   1256 	usbd_status		err;
   1257 	struct ifnet		*ifp;
   1258 	int			i;
   1259 
   1260 	axe_reset(sc);
   1261 
   1262 	ifp = GET_IFP(sc);
   1263 	ifp->if_timer = 0;
   1264 
   1265 	usb_uncallout(sc->axe_stat_ch, axe_tick, sc);
   1266 
   1267 	/* Stop transfers. */
   1268 	if (sc->axe_ep[AXE_ENDPT_RX] != NULL) {
   1269 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
   1270 		if (err) {
   1271 			printf("%s: abort rx pipe failed: %s\n",
   1272 		    	    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1273 		}
   1274 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_RX]);
   1275 		if (err) {
   1276 			printf("%s: close rx pipe failed: %s\n",
   1277 		    	    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1278 		}
   1279 		sc->axe_ep[AXE_ENDPT_RX] = NULL;
   1280 	}
   1281 
   1282 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL) {
   1283 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
   1284 		if (err) {
   1285 			printf("%s: abort tx pipe failed: %s\n",
   1286 		    	    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1287 		}
   1288 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_TX]);
   1289 		if (err) {
   1290 			printf("%s: close tx pipe failed: %s\n",
   1291 			    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1292 		}
   1293 		sc->axe_ep[AXE_ENDPT_TX] = NULL;
   1294 	}
   1295 
   1296 	if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) {
   1297 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
   1298 		if (err) {
   1299 			printf("%s: abort intr pipe failed: %s\n",
   1300 		    	    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1301 		}
   1302 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
   1303 		if (err) {
   1304 			printf("%s: close intr pipe failed: %s\n",
   1305 			    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1306 		}
   1307 		sc->axe_ep[AXE_ENDPT_INTR] = NULL;
   1308 	}
   1309 
   1310 	/* Free RX resources. */
   1311 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
   1312 		if (sc->axe_cdata.axe_rx_chain[i].axe_mbuf != NULL) {
   1313 			m_freem(sc->axe_cdata.axe_rx_chain[i].axe_mbuf);
   1314 			sc->axe_cdata.axe_rx_chain[i].axe_mbuf = NULL;
   1315 		}
   1316 		if (sc->axe_cdata.axe_rx_chain[i].axe_xfer != NULL) {
   1317 			usbd_free_xfer(sc->axe_cdata.axe_rx_chain[i].axe_xfer);
   1318 			sc->axe_cdata.axe_rx_chain[i].axe_xfer = NULL;
   1319 		}
   1320 	}
   1321 
   1322 	/* Free TX resources. */
   1323 	for (i = 0; i < AXE_TX_LIST_CNT; i++) {
   1324 		if (sc->axe_cdata.axe_tx_chain[i].axe_mbuf != NULL) {
   1325 			m_freem(sc->axe_cdata.axe_tx_chain[i].axe_mbuf);
   1326 			sc->axe_cdata.axe_tx_chain[i].axe_mbuf = NULL;
   1327 		}
   1328 		if (sc->axe_cdata.axe_tx_chain[i].axe_xfer != NULL) {
   1329 			usbd_free_xfer(sc->axe_cdata.axe_tx_chain[i].axe_xfer);
   1330 			sc->axe_cdata.axe_tx_chain[i].axe_xfer = NULL;
   1331 		}
   1332 	}
   1333 
   1334 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1335 }
   1336 
   1337