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