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