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