Home | History | Annotate | Line # | Download | only in usb
if_axe.c revision 1.17.10.2
      1 /*	$NetBSD: if_axe.c,v 1.17.10.2 2007/06/13 03:59:15 itohy 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.17.10.2 2007/06/13 03:59:15 itohy 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/lock.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 #include <dev/usb/usb_ethersubr.h>
    141 
    142 #include <dev/usb/if_axereg.h>
    143 
    144 #ifdef AXE_DEBUG
    145 #define DPRINTF(x)	do { if (axedebug) logprintf x; } while (0)
    146 #define DPRINTFN(n,x)	do { if (axedebug >= (n)) logprintf x; } while (0)
    147 int	axedebug = 0;
    148 #else
    149 #define DPRINTF(x)
    150 #define DPRINTFN(n,x)
    151 #endif
    152 
    153 /*
    154  * Various supported device vendors/products.
    155  */
    156 Static const struct axe_type axe_devs[] = {
    157 	{ { USB_VENDOR_ASIX,		USB_PRODUCT_ASIX_AX88172}, 0 },
    158 	{ { USB_VENDOR_COREGA,		USB_PRODUCT_COREGA_FETHER_USB2_TX }, 0},
    159 	{ { USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DUBE100}, 0 },
    160 	{ { USB_VENDOR_LINKSYS2,	USB_PRODUCT_LINKSYS2_USB200M}, 0 },
    161 	{ { USB_VENDOR_MELCO,		USB_PRODUCT_MELCO_LUAU2KTX}, 0 },
    162 	{ { USB_VENDOR_NETGEAR,		USB_PRODUCT_NETGEAR_FA120}, 0 },
    163 	{ { USB_VENDOR_SITECOM,		USB_PRODUCT_SITECOM_LN029}, 0 },
    164 	{ { USB_VENDOR_SYSTEMTALKS,	USB_PRODUCT_SYSTEMTALKS_SGCX2UL}, 0 },
    165 };
    166 #define axe_lookup(v, p) ((const struct axe_type *)usb_lookup(axe_devs, v, p))
    167 
    168 USB_DECLARE_DRIVER(axe);
    169 
    170 Static int axe_encap(struct axe_softc *, struct mbuf *, int);
    171 Static void axe_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
    172 Static void axe_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
    173 Static void axe_tick(void *);
    174 Static void axe_tick_task(void *);
    175 #if 0
    176 Static void axe_rxstart(struct ifnet *);
    177 #endif
    178 Static void axe_start(struct ifnet *);
    179 Static int axe_ioctl(struct ifnet *, u_long, caddr_t);
    180 Static int axe_init(struct ifnet *);
    181 Static void axe_stop(struct ifnet *, int);
    182 Static void axe_watchdog(struct ifnet *);
    183 Static int axe_miibus_readreg(device_ptr_t, int, int);
    184 Static void axe_miibus_writereg(device_ptr_t, int, int, int);
    185 Static void axe_miibus_statchg(device_ptr_t);
    186 Static int axe_cmd(struct axe_softc *, int, int, int, void *);
    187 Static int axe_ifmedia_upd(struct ifnet *);
    188 Static void axe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
    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 	usb_lockmgr(&sc->axe_mii_lock, LK_EXCLUSIVE, NULL);
    201 }
    202 
    203 Static void
    204 axe_unlock_mii(struct axe_softc *sc)
    205 {
    206 	usb_lockmgr(&sc->axe_mii_lock, LK_RELEASE, NULL);
    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 	if (sc->axe_dying)
    218 		return(0);
    219 
    220 	axe_lock_mii(sc);
    221 	if (AXE_CMD_DIR(cmd))
    222 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    223 	else
    224 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
    225 	req.bRequest = AXE_CMD_CMD(cmd);
    226 	USETW(req.wValue, val);
    227 	USETW(req.wIndex, index);
    228 	USETW(req.wLength, AXE_CMD_LEN(cmd));
    229 
    230 	err = usbd_do_request(sc->axe_udev, &req, buf);
    231 	axe_unlock_mii(sc);
    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 		printf("%s: read PHY failed\n", USBDEVNAME(sc->axe_dev));
    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 		printf("%s: write PHY failed\n", USBDEVNAME(sc->axe_dev));
    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 	err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL);
    321 	if (err) {
    322 		printf("%s: media change failed\n", USBDEVNAME(sc->axe_dev));
    323 		return;
    324 	}
    325 }
    326 
    327 /*
    328  * Set media options.
    329  */
    330 Static int
    331 axe_ifmedia_upd(struct ifnet *ifp)
    332 {
    333         struct axe_softc        *sc = ifp->if_softc;
    334         struct mii_data         *mii = GET_MII(sc);
    335 
    336         sc->axe_link = 0;
    337         if (mii->mii_instance) {
    338                 struct mii_softc        *miisc;
    339                 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
    340                          mii_phy_reset(miisc);
    341         }
    342         mii_mediachg(mii);
    343 
    344         return (0);
    345 }
    346 
    347 /*
    348  * Report current media status.
    349  */
    350 Static void
    351 axe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
    352 {
    353         struct axe_softc        *sc = ifp->if_softc;
    354         struct mii_data         *mii = GET_MII(sc);
    355 
    356         mii_pollstat(mii);
    357         ifmr->ifm_active = mii->mii_media_active;
    358         ifmr->ifm_status = mii->mii_media_status;
    359 }
    360 
    361 Static void
    362 axe_setmulti(struct axe_softc *sc)
    363 {
    364 	struct ifnet		*ifp;
    365 	struct ether_multi *enm;
    366 	struct ether_multistep step;
    367 	u_int32_t		h = 0;
    368 	u_int16_t		rxmode;
    369 	u_int8_t		hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
    370 
    371 	if (sc->axe_dying)
    372 		return;
    373 
    374 	ifp = GET_IFP(sc);
    375 
    376 	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, (void *)&rxmode);
    377 	rxmode = le16toh(rxmode);
    378 
    379 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
    380 	allmulti:
    381 		rxmode |= AXE_RXCMD_ALLMULTI;
    382 		axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
    383 		return;
    384 	} else
    385 		rxmode &= ~AXE_RXCMD_ALLMULTI;
    386 
    387 	/* now program new ones */
    388 #if defined(__NetBSD__)
    389 	ETHER_FIRST_MULTI(step, &sc->axe_ec, enm);
    390 #else
    391 	ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
    392 #endif
    393 	while (enm != NULL) {
    394 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    395 			   ETHER_ADDR_LEN) != 0)
    396 			goto allmulti;
    397 
    398 		h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26;
    399 		hashtbl[h / 8] |= 1 << (h % 8);
    400 		ETHER_NEXT_MULTI(step, enm);
    401 	}
    402 
    403 	ifp->if_flags &= ~IFF_ALLMULTI;
    404 	axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
    405 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
    406 	return;
    407 }
    408 
    409 Static void
    410 axe_reset(struct axe_softc *sc)
    411 {
    412 	if (sc->axe_dying)
    413 		return;
    414 	/* XXX What to reset? */
    415 
    416 	/* Wait a little while for the chip to get its brains in order. */
    417 	DELAY(1000);
    418 	return;
    419 }
    420 
    421 /*
    422  * Probe for a AX88172 chip.
    423  */
    424 USB_MATCH(axe)
    425 {
    426 	USB_MATCH_START(axe, uaa);
    427 
    428 	if (!uaa->iface) {
    429 		return(UMATCH_NONE);
    430 	}
    431 
    432 	return (axe_lookup(uaa->vendor, uaa->product) != NULL ?
    433 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    434 }
    435 
    436 /*
    437  * Attach the interface. Allocate softc structures, do ifmedia
    438  * setup and ethernet/BPF attach.
    439  */
    440 USB_ATTACH(axe)
    441 {
    442 	USB_ATTACH_START(axe, sc, uaa);
    443 	usbd_device_handle dev = uaa->device;
    444 	usbd_status err;
    445 	usb_interface_descriptor_t *id;
    446 	usb_endpoint_descriptor_t *ed;
    447 	struct mii_data	*mii;
    448 	u_char eaddr[ETHER_ADDR_LEN];
    449 	char *devinfop;
    450 	char *devname = USBDEVNAME(sc->axe_dev);
    451 	struct ifnet *ifp;
    452 	int i, s;
    453 
    454 	devinfop = usbd_devinfo_alloc(dev, 0);
    455 	USB_ATTACH_SETUP;
    456 
    457 	err = usbd_set_config_no(dev, AXE_CONFIG_NO, 1);
    458 	if (err) {
    459 		printf("%s: getting interface handle failed\n",
    460 		    USBDEVNAME(sc->axe_dev));
    461                 usbd_devinfo_free(devinfop);
    462 		USB_ATTACH_ERROR_RETURN;
    463 	}
    464 
    465 	usb_init_task(&sc->axe_tick_task, axe_tick_task, sc);
    466 	lockinit(&sc->axe_mii_lock, PZERO, "axemii", 0, LK_CANRECURSE);
    467 	usb_init_task(&sc->axe_stop_task, (void (*)(void *))axe_stop, sc);
    468 
    469 	err = usbd_device2interface_handle(dev, AXE_IFACE_IDX, &sc->axe_iface);
    470 	if (err) {
    471 		printf("%s: getting interface handle failed\n",
    472 		    USBDEVNAME(sc->axe_dev));
    473                 usbd_devinfo_free(devinfop);
    474 		USB_ATTACH_ERROR_RETURN;
    475 	}
    476 
    477 	sc->axe_udev = dev;
    478 	sc->axe_product = uaa->product;
    479 	sc->axe_vendor = uaa->vendor;
    480 
    481 	id = usbd_get_interface_descriptor(sc->axe_iface);
    482 
    483 	printf("%s: %s\n", USBDEVNAME(sc->axe_dev), devinfop);
    484 	usbd_devinfo_free(devinfop);
    485 
    486 	/* Find endpoints. */
    487 	for (i = 0; i < id->bNumEndpoints; i++) {
    488 		ed = usbd_interface2endpoint_descriptor(sc->axe_iface, i);
    489 		if (!ed) {
    490 			printf("%s: couldn't get ep %d\n",
    491 			    USBDEVNAME(sc->axe_dev), i);
    492 			USB_ATTACH_ERROR_RETURN;
    493 		}
    494 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    495 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    496 			sc->axe_ed[AXE_ENDPT_RX] = ed->bEndpointAddress;
    497 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    498 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    499 			sc->axe_ed[AXE_ENDPT_TX] = ed->bEndpointAddress;
    500 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    501 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    502 			sc->axe_ed[AXE_ENDPT_INTR] = ed->bEndpointAddress;
    503 		}
    504 	}
    505 
    506 	s = splnet();
    507 
    508 	/*
    509 	 * Get station address.
    510 	 */
    511 	axe_cmd(sc, AXE_CMD_READ_NODEID, 0, 0, &eaddr);
    512 
    513 	/*
    514 	 * Load IPG values and PHY indexes.
    515 	 */
    516 	axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, (void *)&sc->axe_ipgs);
    517 	axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, (void *)&sc->axe_phyaddrs);
    518 
    519 	/*
    520 	 * Work around broken adapters that appear to lie about
    521 	 * their PHY addresses.
    522 	 */
    523 	sc->axe_phyaddrs[0] = sc->axe_phyaddrs[1] = 0xFF;
    524 
    525 	/*
    526 	 * An ASIX chip was detected. Inform the world.
    527 	 */
    528 	printf("%s: Ethernet address %s\n", USBDEVNAME(sc->axe_dev),
    529 	    ether_sprintf(eaddr));
    530 
    531 	/* Initialize interface info.*/
    532 	ifp = GET_IFP(sc);
    533 	ifp->if_softc = sc;
    534 	strncpy(ifp->if_xname, devname, IFNAMSIZ);
    535 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    536 	ifp->if_ioctl = axe_ioctl;
    537 	ifp->if_start = axe_start;
    538 
    539 	ifp->if_init = axe_init;
    540 	ifp->if_stop = axe_stop;
    541 	ifp->if_watchdog = axe_watchdog;
    542 
    543 /*	ifp->if_baudrate = 10000000; */
    544 /*	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;*/
    545 
    546 	IFQ_SET_READY(&ifp->if_snd);
    547 
    548 	/* Initialize MII/media info. */
    549 	mii = &sc->axe_mii;
    550 	mii->mii_ifp = ifp;
    551 	mii->mii_readreg = axe_miibus_readreg;
    552 	mii->mii_writereg = axe_miibus_writereg;
    553 	mii->mii_statchg = axe_miibus_statchg;
    554 	mii->mii_flags = MIIF_AUTOTSLEEP;
    555 
    556 	ifmedia_init(&mii->mii_media, 0, axe_ifmedia_upd, axe_ifmedia_sts);
    557 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
    558 
    559 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
    560 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
    561 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
    562 	} else
    563 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    564 
    565 	/* Attach the interface. */
    566 	if_attach(ifp);
    567 	Ether_ifattach(ifp, eaddr);
    568 #if NRND > 0
    569 	rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->axe_dev),
    570 	    RND_TYPE_NET, 0);
    571 #endif
    572 
    573 	usb_callout_init(sc->axe_stat_ch);
    574 
    575 	sc->axe_attached = 1;
    576 	splx(s);
    577 
    578 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->axe_udev,
    579 			   USBDEV(sc->axe_dev));
    580 
    581 	USB_ATTACH_SUCCESS_RETURN;
    582 }
    583 
    584 USB_DETACH(axe)
    585 {
    586 	USB_DETACH_START(axe, sc);
    587 	int			s;
    588 	struct ifnet		*ifp = GET_IFP(sc);
    589 
    590 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
    591 
    592 	/* Detached before attached finished, so just bail out. */
    593 	if (!sc->axe_attached)
    594 		return (0);
    595 
    596 	usb_uncallout(sc->axe_stat_ch, axe_tick, sc);
    597 
    598 	sc->axe_dying = 1;
    599 
    600 	ether_ifdetach(ifp);
    601 
    602 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL)
    603 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
    604 	if (sc->axe_ep[AXE_ENDPT_RX] != NULL)
    605 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
    606 	if (sc->axe_ep[AXE_ENDPT_INTR] != NULL)
    607 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
    608 
    609 	/*
    610 	 * Remove any pending tasks.  They cannot be executing because they run
    611 	 * in the same thread as detach.
    612 	 */
    613 	usb_rem_task(sc->axe_udev, &sc->axe_tick_task);
    614 	usb_rem_task(sc->axe_udev, &sc->axe_stop_task);
    615 
    616 	s = splusb();
    617 
    618 	if (--sc->axe_refcnt >= 0) {
    619 		/* Wait for processes to go away */
    620 		usb_detach_wait(USBDEV(sc->axe_dev));
    621 	}
    622 
    623 	if (ifp->if_flags & IFF_RUNNING)
    624 		axe_stop(ifp, 1);
    625 
    626 #if defined(__NetBSD__)
    627 #if NRND > 0
    628 	rnd_detach_source(&sc->rnd_source);
    629 #endif
    630 #endif /* __NetBSD__ */
    631 	mii_detach(&sc->axe_mii, MII_PHY_ANY, MII_OFFSET_ANY);
    632 	ifmedia_delete_instance(&sc->axe_mii.mii_media, IFM_INST_ANY);
    633 	ether_ifdetach(ifp);
    634 	if_detach(ifp);
    635 
    636 #ifdef DIAGNOSTIC
    637 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL ||
    638 	    sc->axe_ep[AXE_ENDPT_RX] != NULL ||
    639 	    sc->axe_ep[AXE_ENDPT_INTR] != NULL)
    640 		printf("%s: detach has active endpoints\n",
    641 		       USBDEVNAME(sc->axe_dev));
    642 #endif
    643 
    644 	sc->axe_attached = 0;
    645 
    646 	if (--sc->axe_refcnt >= 0) {
    647 		/* Wait for processes to go away. */
    648 		usb_detach_wait(USBDEV(sc->axe_dev));
    649 	}
    650 	splx(s);
    651 
    652 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->axe_udev,
    653 			   USBDEV(sc->axe_dev));
    654 
    655 	return (0);
    656 }
    657 
    658 int
    659 axe_activate(device_ptr_t self, enum devact act)
    660 {
    661 	struct axe_softc *sc = (struct axe_softc *)self;
    662 
    663 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
    664 
    665 	switch (act) {
    666 	case DVACT_ACTIVATE:
    667 		return (EOPNOTSUPP);
    668 		break;
    669 
    670 	case DVACT_DEACTIVATE:
    671 		if_deactivate(&sc->axe_ec.ec_if);
    672 		sc->axe_dying = 1;
    673 		break;
    674 	}
    675 	return (0);
    676 }
    677 
    678 #if 0
    679 Static void
    680 axe_rxstart(struct ifnet *ifp)
    681 {
    682 	struct axe_softc	*sc;
    683 	struct ue_chain		*c;
    684 
    685 	sc = ifp->if_softc;
    686 	axe_lock_mii(sc);
    687 	c = &sc->axe_cdata.axe_rx_chain[sc->axe_cdata.axe_rx_prod];
    688 
    689 	if ((c->ue_mbuf = usb_ether_newbuf(NULL)) == NULL) {
    690 		ifp->if_ierrors++;
    691 		axe_unlock_mii(sc);
    692 		return;
    693 	}
    694 
    695 	/* Setup new transfer. */
    696 	(void)usbd_map_buffer_mbuf(c->ue_xfer, c->ue_mbuf);
    697 	usbd_setup_xfer(c->ue_xfer, sc->axe_ep[AXE_ENDPT_RX],
    698 	    c, NULL /* XXX buf */, AXE_BUFSZ, USBD_SHORT_XFER_OK,
    699 	    USBD_NO_TIMEOUT, axe_rxeof);
    700 	usbd_transfer(c->ue_xfer);
    701 	axe_unlock_mii(sc);
    702 
    703 	return;
    704 }
    705 #endif
    706 
    707 /*
    708  * A frame has been uploaded: pass the resulting mbuf chain up to
    709  * the higher level protocols.
    710  */
    711 Static void
    712 axe_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    713 {
    714 	struct axe_softc	*sc;
    715 	struct ue_chain		*c;
    716 	struct ifnet		*ifp;
    717 	struct mbuf		*m;
    718 	u_int32_t		total_len;
    719 	int			s;
    720 
    721 	c = priv;
    722 	sc = (void *)c->ue_dev;
    723 	ifp = GET_IFP(sc);
    724 
    725 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev),__func__));
    726 
    727 	if (sc->axe_dying)
    728 		return;
    729 
    730 	usbd_unmap_buffer(xfer);
    731 
    732 	if (!(ifp->if_flags & IFF_RUNNING))
    733 		return;
    734 
    735 	if (status != USBD_NORMAL_COMPLETION) {
    736 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    737 			return;
    738 		if (usbd_ratecheck(&sc->axe_rx_notice)) {
    739 			printf("%s: usb errors on rx: %s\n",
    740 			    USBDEVNAME(sc->axe_dev), usbd_errstr(status));
    741 		}
    742 		if (status == USBD_STALLED)
    743 			usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_RX]);
    744 		goto done;
    745 	}
    746 
    747 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
    748 
    749 	m = c->ue_mbuf;
    750 
    751 	if (total_len <= sizeof(struct ether_header)) {
    752 		ifp->if_ierrors++;
    753 		goto done;
    754 	}
    755 
    756 	/*
    757 	 * Allocate new mbuf cluster for the next transfer.
    758 	 * If that failed, discard current packet and recycle the mbuf.
    759 	 */
    760 	if ((c->ue_mbuf = usb_ether_newbuf(NULL)) == NULL) {
    761 		printf("%s: no memory for rx list -- packet dropped!\n",
    762 		    USBDEVNAME(sc->axe_dev));
    763 		ifp->if_ierrors++;
    764 		c->ue_mbuf = usb_ether_newbuf(m);
    765 		goto done;
    766 	}
    767 
    768 	ifp->if_ipackets++;
    769 	m->m_pkthdr.rcvif = ifp;
    770 	m->m_pkthdr.len = m->m_len = total_len;
    771 
    772 	/* No errors; receive the packet. */
    773 	total_len -= ETHER_CRC_LEN + 4;
    774 	/* XXX XXX What is it?  Should it be moved several lines above? */
    775 
    776 	s = splnet();
    777 
    778 #if NBPFILTER > 0
    779 	if (ifp->if_bpf)
    780 		BPF_MTAP(ifp, m);
    781 #endif
    782 
    783 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->axe_dev),
    784 		    __func__, m->m_len));
    785 	IF_INPUT(ifp, m);
    786 	splx(s);
    787 
    788  done:
    789 
    790 	/* Setup new transfer. */
    791 	(void)usbd_map_buffer_mbuf(c->ue_xfer, c->ue_mbuf);
    792 	usbd_setup_xfer(xfer, sc->axe_ep[AXE_ENDPT_RX],
    793 	    c, NULL /* XXX buf */, AXE_BUFSZ,
    794 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
    795 	    USBD_NO_TIMEOUT, axe_rxeof);
    796 	usbd_transfer(xfer);
    797 
    798 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->axe_dev),
    799 		    __func__));
    800 	return;
    801 }
    802 
    803 /*
    804  * A frame was downloaded to the chip. It's safe for us to clean up
    805  * the list buffers.
    806  */
    807 
    808 Static void
    809 axe_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
    810     usbd_status status)
    811 {
    812 	struct axe_softc	*sc;
    813 	struct ue_chain		*c;
    814 	struct ifnet		*ifp;
    815 	int			s;
    816 
    817 	c = priv;
    818 	sc = (void *)c->ue_dev;
    819 	ifp = GET_IFP(sc);
    820 
    821 	if (sc->axe_dying)
    822 		return;
    823 
    824 	usbd_unmap_buffer(xfer);
    825 
    826 	s = splnet();
    827 
    828 	if (status != USBD_NORMAL_COMPLETION) {
    829 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
    830 			splx(s);
    831 			return;
    832 		}
    833 		ifp->if_oerrors++;
    834 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->axe_dev),
    835 		    usbd_errstr(status));
    836 		if (status == USBD_STALLED)
    837 			usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_TX]);
    838 		splx(s);
    839 		return;
    840 	}
    841 
    842 	ifp->if_timer = 0;
    843 	ifp->if_flags &= ~IFF_OACTIVE;
    844 
    845 	m_freem(c->ue_mbuf);
    846 	c->ue_mbuf = NULL;
    847 
    848 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    849 		axe_start(ifp);
    850 
    851 	ifp->if_opackets++;
    852 	splx(s);
    853 	return;
    854 }
    855 
    856 Static void
    857 axe_tick(void *xsc)
    858 {
    859 	struct axe_softc *sc = xsc;
    860 
    861 	if (sc == NULL)
    862 		return;
    863 
    864 	DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->axe_dev),
    865 			__func__));
    866 
    867 	if (sc->axe_dying)
    868 		return;
    869 
    870 	/* Perform periodic stuff in process context */
    871 	usb_add_task(sc->axe_udev, &sc->axe_tick_task, USB_TASKQ_DRIVER);
    872 
    873 }
    874 
    875 Static void
    876 axe_tick_task(void *xsc)
    877 {
    878 	int			s;
    879 	struct axe_softc	*sc;
    880 	struct ifnet		*ifp;
    881 	struct mii_data		*mii;
    882 
    883 	sc = xsc;
    884 
    885 	if (sc == NULL)
    886 		return;
    887 
    888 	if (sc->axe_dying)
    889 		return;
    890 
    891 	ifp = GET_IFP(sc);
    892 	mii = GET_MII(sc);
    893 	if (mii == NULL)
    894 		return;
    895 
    896 	s = splnet();
    897 
    898 	mii_tick(mii);
    899 	if (!sc->axe_link) {
    900 		mii_pollstat(mii);
    901 		if (mii->mii_media_status & IFM_ACTIVE &&
    902 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
    903 			DPRINTF(("%s: %s: got link\n",
    904 				 USBDEVNAME(sc->axe_dev), __func__));
    905 			sc->axe_link++;
    906 			if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    907 				   axe_start(ifp);
    908 		}
    909 	}
    910 
    911 	usb_callout(sc->axe_stat_ch, hz, axe_tick, sc);
    912 
    913 	splx(s);
    914 }
    915 
    916 Static int
    917 axe_encap(struct axe_softc *sc, struct mbuf *m, int idx)
    918 {
    919 	struct ue_chain		*c;
    920 	usbd_status		err;
    921 	int			ret;
    922 
    923 	c = &sc->axe_cdata.axe_tx_chain[idx];
    924 
    925 	ret = usb_ether_map_tx_buffer_mbuf(c, m);
    926 	if (ret)
    927 		return (ret);
    928 
    929 	usbd_setup_xfer(c->ue_xfer, sc->axe_ep[AXE_ENDPT_TX],
    930 	    c, NULL /* XXX buf */, m->m_pkthdr.len, USBD_FORCE_SHORT_XFER, 10000,
    931 	    axe_txeof);
    932 
    933 	/* Transmit */
    934 	err = usbd_transfer(c->ue_xfer);
    935 	if (err != USBD_IN_PROGRESS) {
    936 		axe_stop(GET_IFP(sc), 0);
    937 		return(EIO);
    938 	}
    939 
    940 	sc->axe_cdata.axe_tx_cnt++;
    941 
    942 	return(0);
    943 }
    944 
    945 Static void
    946 axe_start(struct ifnet *ifp)
    947 {
    948 	struct axe_softc	*sc;
    949 	struct mbuf		*m_head = NULL;
    950 
    951 	sc = ifp->if_softc;
    952 
    953 	if (!sc->axe_link) {
    954 		return;
    955 	}
    956 
    957 	if (ifp->if_flags & IFF_OACTIVE) {
    958 		return;
    959 	}
    960 
    961 	IFQ_POLL(&ifp->if_snd, m_head);
    962 	if (m_head == NULL) {
    963 		return;
    964 	}
    965 
    966 	if (axe_encap(sc, m_head, 0)) {
    967 		ifp->if_flags |= IFF_OACTIVE;
    968 		return;
    969 	}
    970 
    971 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
    972 
    973 	/*
    974 	 * If there's a BPF listener, bounce a copy of this frame
    975 	 * to him.
    976 	 */
    977 #if NBPFILTER > 0
    978 	 if (ifp->if_bpf)
    979 	 	BPF_MTAP(ifp, m_head);
    980 #endif
    981 
    982 	ifp->if_flags |= IFF_OACTIVE;
    983 
    984 	/*
    985 	 * Set a timeout in case the chip goes out to lunch.
    986 	 */
    987 	ifp->if_timer = 5;
    988 
    989 	return;
    990 }
    991 
    992 Static int
    993 axe_init(struct ifnet *ifp)
    994 {
    995 	struct axe_softc	*sc = ifp->if_softc;
    996 	struct ue_chain		*c;
    997 	usbd_status		err;
    998 	int			rxmode;
    999 	int			i, s;
   1000 
   1001 	if (ifp->if_flags & IFF_RUNNING)
   1002 		return (EIO);
   1003 
   1004 	s = splnet();
   1005 
   1006 	/*
   1007 	 * Cancel pending I/O and free all RX/TX buffers.
   1008 	 */
   1009 	axe_reset(sc);
   1010 
   1011 	/* Set transmitter IPG values */
   1012 	axe_cmd(sc, AXE_CMD_WRITE_IPG0, 0, sc->axe_ipgs[0], NULL);
   1013 	axe_cmd(sc, AXE_CMD_WRITE_IPG1, 0, sc->axe_ipgs[1], NULL);
   1014 	axe_cmd(sc, AXE_CMD_WRITE_IPG2, 0, sc->axe_ipgs[2], NULL);
   1015 
   1016 	/* Enable receiver, set RX mode */
   1017 	rxmode = AXE_RXCMD_UNICAST|AXE_RXCMD_MULTICAST|AXE_RXCMD_ENABLE;
   1018 
   1019 	/* If we want promiscuous mode, set the allframes bit. */
   1020 	if (ifp->if_flags & IFF_PROMISC)
   1021 		rxmode |= AXE_RXCMD_PROMISC;
   1022 
   1023 	if (ifp->if_flags & IFF_BROADCAST)
   1024 		rxmode |= AXE_RXCMD_BROADCAST;
   1025 
   1026 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
   1027 
   1028 	/* Load the multicast filter. */
   1029 	axe_setmulti(sc);
   1030 
   1031 	/* Open RX and TX pipes. */
   1032 	err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_RX],
   1033 	    USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_RX]);
   1034 	if (err) {
   1035 		printf("%s: open rx pipe failed: %s\n",
   1036 		    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1037 		splx(s);
   1038 		return (EIO);
   1039 	}
   1040 
   1041 	err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_TX],
   1042 	    USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_TX]);
   1043 	if (err) {
   1044 		printf("%s: open tx pipe failed: %s\n",
   1045 		    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1046 		splx(s);
   1047 		return (EIO);
   1048 	}
   1049 
   1050 	/* Init RX ring. */
   1051 	if ((i = usb_ether_rx_list_init(USBDEV(sc->axe_dev),
   1052 	    sc->axe_cdata.axe_rx_chain, AXE_RX_LIST_CNT,
   1053 	    sc->axe_udev, sc->axe_ep[AXE_ENDPT_RX]))) {
   1054 		printf("%s: rx list init failed\n", USBDEVNAME(sc->axe_dev));
   1055 		splx(s);
   1056 		return (i);
   1057 	}
   1058 
   1059 	/* Init TX ring. */
   1060 	if ((i = usb_ether_tx_list_init(USBDEV(sc->axe_dev),
   1061 	    sc->axe_cdata.axe_tx_chain, AXE_TX_LIST_CNT,
   1062 	    sc->axe_udev, sc->axe_ep[AXE_ENDPT_TX], NULL))) {
   1063 		printf("%s: tx list init failed\n", USBDEVNAME(sc->axe_dev));
   1064 		splx(s);
   1065 		return (i);
   1066 	}
   1067 
   1068 	/* Start up the receive pipe. */
   1069 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
   1070 		c = &sc->axe_cdata.axe_rx_chain[i];
   1071 		(void)usbd_map_buffer_mbuf(c->ue_xfer, c->ue_mbuf);
   1072 		usbd_setup_xfer(c->ue_xfer, sc->axe_ep[AXE_ENDPT_RX],
   1073 		    c, NULL /* buf */, AXE_BUFSZ,
   1074 		    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, axe_rxeof);
   1075 		usbd_transfer(c->ue_xfer);
   1076 	}
   1077 
   1078 	ifp->if_flags |= IFF_RUNNING;
   1079 	ifp->if_flags &= ~IFF_OACTIVE;
   1080 
   1081 	splx(s);
   1082 
   1083 	usb_callout_init(sc->axe_stat_ch);
   1084 	usb_callout(sc->axe_stat_ch, hz, axe_tick, sc);
   1085 	return (0);
   1086 }
   1087 
   1088 Static int
   1089 axe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   1090 {
   1091 	struct axe_softc	*sc = ifp->if_softc;
   1092 	struct ifreq		*ifr = (struct ifreq *)data;
   1093 #if 0
   1094 	struct ifaddr		*ifa = (struct ifaddr *)data;
   1095 #endif
   1096 	struct mii_data		*mii;
   1097 	u_int16_t		rxmode;
   1098 	int			error = 0;
   1099 
   1100 	switch(cmd) {
   1101 #if 0
   1102 	case SIOCSIFADDR:
   1103 		ifp->if_flags |= IFF_UP;
   1104 		axe_init(ifp);
   1105 
   1106 		switch (ifa->ifa_addr->sa_family) {
   1107 #ifdef INET
   1108 		case AF_INET:
   1109 #if defined(__NetBSD__)
   1110 			arp_ifinit(ifp, ifa);
   1111 #else
   1112 			arp_ifinit(&sc->arpcom, ifa);
   1113 #endif
   1114 			break;
   1115 #endif /* INET */
   1116 		}
   1117 		break;
   1118 
   1119 	case SIOCSIFMTU:
   1120 		if (ifr->ifr_mtu > ETHERMTU)
   1121 			error = EINVAL;
   1122 		else
   1123 			ifp->if_mtu = ifr->ifr_mtu;
   1124 		break;
   1125 #endif
   1126 
   1127 	case SIOCSIFFLAGS:
   1128 		if (ifp->if_flags & IFF_UP) {
   1129 			if (ifp->if_flags & IFF_RUNNING &&
   1130 			    ifp->if_flags & IFF_PROMISC &&
   1131 			    !(sc->axe_if_flags & IFF_PROMISC)) {
   1132 
   1133 				axe_cmd(sc, AXE_CMD_RXCTL_READ,
   1134 					0, 0, (void *)&rxmode);
   1135 				rxmode = le16toh(rxmode) | AXE_RXCMD_PROMISC;
   1136 				axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
   1137 					0, rxmode, NULL);
   1138 
   1139 				axe_setmulti(sc);
   1140 			} else if (ifp->if_flags & IFF_RUNNING &&
   1141 			    !(ifp->if_flags & IFF_PROMISC) &&
   1142 			    sc->axe_if_flags & IFF_PROMISC) {
   1143 				axe_cmd(sc, AXE_CMD_RXCTL_READ,
   1144 					0, 0, (void *)&rxmode);
   1145 				rxmode = le16toh(rxmode) & ~AXE_RXCMD_PROMISC;
   1146 				axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
   1147 					0, rxmode, NULL);
   1148 				axe_setmulti(sc);
   1149 			} else if (!(ifp->if_flags & IFF_RUNNING))
   1150 				axe_init(ifp);
   1151 		} else {
   1152 			if (ifp->if_flags & IFF_RUNNING)
   1153 				axe_stop(ifp, 1);
   1154 		}
   1155 		sc->axe_if_flags = ifp->if_flags;
   1156 		error = 0;
   1157 		break;
   1158 #if 0
   1159 	case SIOCADDMULTI:
   1160 	case SIOCDELMULTI:
   1161 #ifdef __NetBSD__
   1162 		error = (cmd == SIOCADDMULTI) ?
   1163 			ether_addmulti(ifr, &sc->axe_ec) :
   1164 			ether_delmulti(ifr, &sc->axe_ec);
   1165 #else
   1166 		error = (cmd == SIOCADDMULTI) ?
   1167 		    ether_addmulti(ifr, &sc->arpcom) :
   1168 		    ether_delmulti(ifr, &sc->arpcom);
   1169 #endif /* __NetBSD__ */
   1170 		if (error == ENETRESET) {
   1171 			/*
   1172 			 * Multicast list has changed; set the hardware
   1173 			 * filter accordingly.
   1174 			 */
   1175 			if (ifp->if_flags & IFF_RUNNING)
   1176 				axe_setmulti(sc);
   1177 			error = 0;
   1178 		}
   1179 		break;
   1180 #endif
   1181 	case SIOCGIFMEDIA:
   1182 	case SIOCSIFMEDIA:
   1183 		mii = GET_MII(sc);
   1184 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
   1185 		break;
   1186 
   1187 	default:
   1188 		error = ether_ioctl(ifp, cmd, data);
   1189 		if (error == ENETRESET) {
   1190 			/*
   1191 			 * Multicast list has changed; set the hardware
   1192 			 * filter accordingly.
   1193 			 */
   1194 			if (ifp->if_flags & IFF_RUNNING)
   1195 				axe_setmulti(sc);
   1196 			error = 0;
   1197 		}
   1198 		break;
   1199 	}
   1200 
   1201 	return(error);
   1202 }
   1203 
   1204 /*
   1205  * XXX
   1206  * You can't call axe_txeof since the USB transfer has not
   1207  * completed yet.
   1208  */
   1209 Static void
   1210 axe_watchdog(struct ifnet *ifp)
   1211 {
   1212 	struct axe_softc	*sc;
   1213 	struct ue_chain		*c;
   1214 	usbd_status		stat;
   1215 	int			s;
   1216 
   1217 	sc = ifp->if_softc;
   1218 
   1219 	ifp->if_oerrors++;
   1220 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->axe_dev));
   1221 
   1222 	s = splusb();
   1223 	c = &sc->axe_cdata.axe_tx_chain[0];
   1224 	usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &stat);
   1225 	axe_txeof(c->ue_xfer, c, stat);
   1226 
   1227 	if (ifp->if_snd.ifq_head != NULL)
   1228 		axe_start(ifp);
   1229 	splx(s);
   1230 }
   1231 
   1232 /*
   1233  * Stop the adapter and free any mbufs allocated to the
   1234  * RX and TX lists.
   1235  */
   1236 Static void
   1237 axe_stop(struct ifnet *ifp, int disable)
   1238 {
   1239 	struct axe_softc	*sc = ifp->if_softc;
   1240 	usbd_status		err;
   1241 
   1242 	axe_reset(sc);
   1243 
   1244 	ifp = GET_IFP(sc);
   1245 	ifp->if_timer = 0;
   1246 
   1247 	usb_uncallout(sc->axe_stat_ch, axe_tick, sc);
   1248 
   1249 	/* Stop transfers. */
   1250 	if (sc->axe_ep[AXE_ENDPT_RX] != NULL) {
   1251 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
   1252 		if (err) {
   1253 			printf("%s: abort rx pipe failed: %s\n",
   1254 		    	    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1255 		}
   1256 	}
   1257 
   1258 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL) {
   1259 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
   1260 		if (err) {
   1261 			printf("%s: abort tx pipe failed: %s\n",
   1262 		    	    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1263 		}
   1264 	}
   1265 
   1266 	if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) {
   1267 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
   1268 		if (err) {
   1269 			printf("%s: abort intr pipe failed: %s\n",
   1270 		    	    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1271 		}
   1272 	}
   1273 
   1274 	/* Free RX/TX resources. */
   1275 	usb_ether_rx_list_free(sc->axe_cdata.axe_rx_chain, AXE_RX_LIST_CNT);
   1276 	usb_ether_tx_list_free(sc->axe_cdata.axe_tx_chain, AXE_TX_LIST_CNT);
   1277 
   1278 	/* Close pipes. */
   1279 	if (sc->axe_ep[AXE_ENDPT_RX] != NULL) {
   1280 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_RX]);
   1281 		if (err) {
   1282 			printf("%s: close rx pipe failed: %s\n",
   1283 		    	    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1284 		}
   1285 		sc->axe_ep[AXE_ENDPT_RX] = NULL;
   1286 	}
   1287 
   1288 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL) {
   1289 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_TX]);
   1290 		if (err) {
   1291 			printf("%s: close tx pipe failed: %s\n",
   1292 			    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1293 		}
   1294 		sc->axe_ep[AXE_ENDPT_TX] = NULL;
   1295 	}
   1296 
   1297 	if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) {
   1298 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
   1299 		if (err) {
   1300 			printf("%s: close intr pipe failed: %s\n",
   1301 			    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1302 		}
   1303 		sc->axe_ep[AXE_ENDPT_INTR] = NULL;
   1304 	}
   1305 
   1306 	sc->axe_link = 0;
   1307 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1308 }
   1309 
   1310