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