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