Home | History | Annotate | Line # | Download | only in usb
if_axe.c revision 1.11
      1 /*	$NetBSD: if_axe.c,v 1.11 2005/11/10 12:05:01 augustss Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997, 1998, 1999, 2000-2003
      5  *	Bill Paul <wpaul (at) windriver.com>.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Bill Paul.
     18  * 4. Neither the name of the author nor the names of any co-contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32  * THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * ASIX Electronics AX88172 USB 2.0 ethernet driver. Used in the
     37  * LinkSys USB200M and various other adapters.
     38  *
     39  * Manuals available from:
     40  * http://www.asix.com.tw/datasheet/mac/Ax88172.PDF
     41  * (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.11 2005/11/10 12:05:01 augustss Exp $");
     77 
     78 #if defined(__NetBSD__)
     79 #include "opt_inet.h"
     80 #include "opt_ns.h"
     81 #include "rnd.h"
     82 #endif
     83 
     84 #include "bpfilter.h"
     85 
     86 #include <sys/param.h>
     87 #include <sys/systm.h>
     88 #include <sys/sockio.h>
     89 #include <sys/lock.h>
     90 #include <sys/mbuf.h>
     91 #include <sys/kernel.h>
     92 #if defined(__OpenBSD__)
     93 #include <sys/proc.h>
     94 #endif
     95 #include <sys/socket.h>
     96 
     97 #include <sys/device.h>
     98 #if NRND > 0
     99 #include <sys/rnd.h>
    100 #endif
    101 
    102 #include <net/if.h>
    103 #if defined(__NetBSD__)
    104 #include <net/if_arp.h>
    105 #endif
    106 #include <net/if_dl.h>
    107 #include <net/if_media.h>
    108 
    109 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m))
    110 
    111 #if NBPFILTER > 0
    112 #include <net/bpf.h>
    113 #endif
    114 
    115 #if defined(__NetBSD__)
    116 #include <net/if_ether.h>
    117 #ifdef INET
    118 #include <netinet/in.h>
    119 #include <netinet/if_inarp.h>
    120 #endif
    121 #endif /* defined(__NetBSD__) */
    122 
    123 #if defined(__OpenBSD__)
    124 #ifdef INET
    125 #include <netinet/in.h>
    126 #include <netinet/in_systm.h>
    127 #include <netinet/in_var.h>
    128 #include <netinet/ip.h>
    129 #include <netinet/if_ether.h>
    130 #endif
    131 #endif /* defined(__OpenBSD__) */
    132 
    133 #ifdef NS
    134 #include <netns/ns.h>
    135 #include <netns/ns_if.h>
    136 #endif
    137 
    138 #include <dev/mii/mii.h>
    139 #include <dev/mii/miivar.h>
    140 
    141 #include <dev/usb/usb.h>
    142 #include <dev/usb/usbdi.h>
    143 #include <dev/usb/usbdi_util.h>
    144 #include <dev/usb/usbdevs.h>
    145 
    146 #include <dev/usb/if_axereg.h>
    147 
    148 #ifdef AXE_DEBUG
    149 #define DPRINTF(x)	do { if (axedebug) logprintf x; } while (0)
    150 #define DPRINTFN(n,x)	do { if (axedebug >= (n)) logprintf x; } while (0)
    151 int	axedebug = 0;
    152 #else
    153 #define DPRINTF(x)
    154 #define DPRINTFN(n,x)
    155 #endif
    156 
    157 /*
    158  * Various supported device vendors/products.
    159  */
    160 Static const struct axe_type axe_devs[] = {
    161 	{ { USB_VENDOR_ASIX,		USB_PRODUCT_ASIX_AX88172}, 0 },
    162 	{ { USB_VENDOR_COREGA,		USB_PRODUCT_COREGA_FETHER_USB2_TX }, 0},
    163 	{ { USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DUBE100}, 0 },
    164 	{ { USB_VENDOR_LINKSYS2,	USB_PRODUCT_LINKSYS2_USB200M}, 0 },
    165 	{ { USB_VENDOR_MELCO,		USB_PRODUCT_MELCO_LUAU2KTX}, 0 },
    166 	{ { USB_VENDOR_NETGEAR,		USB_PRODUCT_NETGEAR_FA120}, 0 },
    167 	{ { USB_VENDOR_SITECOM,		USB_PRODUCT_SITECOM_LN029}, 0 },
    168 	{ { USB_VENDOR_SYSTEMTALKS,	USB_PRODUCT_SYSTEMTALKS_SGCX2UL}, 0 },
    169 };
    170 #define axe_lookup(v, p) ((const struct axe_type *)usb_lookup(axe_devs, v, p))
    171 
    172 USB_DECLARE_DRIVER(axe);
    173 
    174 Static int axe_tx_list_init(struct axe_softc *);
    175 Static int axe_rx_list_init(struct axe_softc *);
    176 Static int axe_newbuf(struct axe_softc *, struct axe_chain *, struct mbuf *);
    177 Static int axe_encap(struct axe_softc *, struct mbuf *, int);
    178 Static void axe_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
    179 Static void axe_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
    180 Static void axe_tick(void *);
    181 Static void axe_tick_task(void *);
    182 #if 0
    183 Static void axe_rxstart(struct ifnet *);
    184 #endif
    185 Static void axe_start(struct ifnet *);
    186 Static int axe_ioctl(struct ifnet *, u_long, caddr_t);
    187 Static void axe_init(void *);
    188 Static void axe_stop(struct axe_softc *);
    189 Static void axe_watchdog(struct ifnet *);
    190 Static int axe_miibus_readreg(device_ptr_t, int, int);
    191 Static void axe_miibus_writereg(device_ptr_t, int, int, int);
    192 Static void axe_miibus_statchg(device_ptr_t);
    193 Static int axe_cmd(struct axe_softc *, int, int, int, void *);
    194 Static int axe_ifmedia_upd(struct ifnet *);
    195 Static void axe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
    196 Static void axe_reset(struct axe_softc *sc);
    197 
    198 Static void axe_setmulti(struct axe_softc *);
    199 Static void axe_lock_mii(struct axe_softc *sc);
    200 Static void axe_unlock_mii(struct axe_softc *sc);
    201 
    202 /* Get exclusive access to the MII registers */
    203 Static void
    204 axe_lock_mii(struct axe_softc *sc)
    205 {
    206 	sc->axe_refcnt++;
    207 	usb_lockmgr(&sc->axe_mii_lock, LK_EXCLUSIVE, NULL);
    208 }
    209 
    210 Static void
    211 axe_unlock_mii(struct axe_softc *sc)
    212 {
    213 	usb_lockmgr(&sc->axe_mii_lock, LK_RELEASE, NULL);
    214 	if (--sc->axe_refcnt < 0)
    215 		usb_detach_wakeup(USBDEV(sc->axe_dev));
    216 }
    217 
    218 Static int
    219 axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
    220 {
    221 	usb_device_request_t	req;
    222 	usbd_status		err;
    223 
    224 	if (sc->axe_dying)
    225 		return(0);
    226 
    227 	axe_lock_mii(sc);
    228 	if (AXE_CMD_DIR(cmd))
    229 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    230 	else
    231 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
    232 	req.bRequest = AXE_CMD_CMD(cmd);
    233 	USETW(req.wValue, val);
    234 	USETW(req.wIndex, index);
    235 	USETW(req.wLength, AXE_CMD_LEN(cmd));
    236 
    237 	err = usbd_do_request(sc->axe_udev, &req, buf);
    238 	axe_unlock_mii(sc);
    239 
    240 	if (err)
    241 		return(-1);
    242 
    243 	return(0);
    244 }
    245 
    246 Static int
    247 axe_miibus_readreg(device_ptr_t dev, int phy, int reg)
    248 {
    249 	struct axe_softc	*sc = USBGETSOFTC(dev);
    250 	usbd_status		err;
    251 	u_int16_t		val;
    252 
    253 	if (sc->axe_dying) {
    254 		DPRINTF(("axe: dying\n"));
    255 		return(0);
    256 	}
    257 
    258 #ifdef notdef
    259 	/*
    260 	 * The chip tells us the MII address of any supported
    261 	 * PHYs attached to the chip, so only read from those.
    262 	 */
    263 
    264 	if (sc->axe_phyaddrs[0] != AXE_NOPHY && phy != sc->axe_phyaddrs[0])
    265 		return (0);
    266 
    267 	if (sc->axe_phyaddrs[1] != AXE_NOPHY && phy != sc->axe_phyaddrs[1])
    268 		return (0);
    269 #endif
    270 	if (sc->axe_phyaddrs[0] != 0xFF && sc->axe_phyaddrs[0] != phy)
    271 		return (0);
    272 
    273 	val = 0;
    274 
    275 	axe_lock_mii(sc);
    276 	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
    277 	err = axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, (void *)&val);
    278 	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
    279 	axe_unlock_mii(sc);
    280 
    281 	if (err) {
    282 		printf("%s: read PHY failed\n", USBDEVNAME(sc->axe_dev));
    283 		return(-1);
    284 	}
    285 
    286 	if (val)
    287 		sc->axe_phyaddrs[0] = phy;
    288 
    289 	return (le16toh(val));
    290 }
    291 
    292 Static void
    293 axe_miibus_writereg(device_ptr_t dev, int phy, int reg, int aval)
    294 {
    295 	struct axe_softc	*sc = USBGETSOFTC(dev);
    296 	usbd_status		err;
    297 	u_int16_t		val;
    298 
    299 	if (sc->axe_dying)
    300 		return;
    301 
    302 	val = htole16(aval);
    303 	axe_lock_mii(sc);
    304 	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
    305 	err = axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, (void *)&val);
    306 	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
    307 	axe_unlock_mii(sc);
    308 
    309 	if (err) {
    310 		printf("%s: write PHY failed\n", USBDEVNAME(sc->axe_dev));
    311 		return;
    312 	}
    313 }
    314 
    315 Static void
    316 axe_miibus_statchg(device_ptr_t dev)
    317 {
    318 	struct axe_softc	*sc = USBGETSOFTC(dev);
    319 	struct mii_data		*mii = GET_MII(sc);
    320 	int val, err;
    321 
    322 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
    323 		val = AXE_MEDIA_FULL_DUPLEX;
    324 	else
    325 		val = 0;
    326 	DPRINTF(("axe_miibus_statchg: val=0x%x\n", val));
    327 	err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL);
    328 	if (err) {
    329 		printf("%s: media change failed\n", USBDEVNAME(sc->axe_dev));
    330 		return;
    331 	}
    332 }
    333 
    334 /*
    335  * Set media options.
    336  */
    337 Static int
    338 axe_ifmedia_upd(struct ifnet *ifp)
    339 {
    340         struct axe_softc        *sc = ifp->if_softc;
    341         struct mii_data         *mii = GET_MII(sc);
    342 
    343         sc->axe_link = 0;
    344         if (mii->mii_instance) {
    345                 struct mii_softc        *miisc;
    346                 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
    347                          mii_phy_reset(miisc);
    348         }
    349         mii_mediachg(mii);
    350 
    351         return (0);
    352 }
    353 
    354 /*
    355  * Report current media status.
    356  */
    357 Static void
    358 axe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
    359 {
    360         struct axe_softc        *sc = ifp->if_softc;
    361         struct mii_data         *mii = GET_MII(sc);
    362 
    363         mii_pollstat(mii);
    364         ifmr->ifm_active = mii->mii_media_active;
    365         ifmr->ifm_status = mii->mii_media_status;
    366 }
    367 
    368 Static void
    369 axe_setmulti(struct axe_softc *sc)
    370 {
    371 	struct ifnet		*ifp;
    372 	struct ether_multi *enm;
    373 	struct ether_multistep step;
    374 	u_int32_t		h = 0;
    375 	u_int16_t		rxmode;
    376 	u_int8_t		hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
    377 
    378 	if (sc->axe_dying)
    379 		return;
    380 
    381 	ifp = GET_IFP(sc);
    382 
    383 	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, (void *)&rxmode);
    384 	rxmode = le16toh(rxmode);
    385 
    386 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
    387 	allmulti:
    388 		rxmode |= AXE_RXCMD_ALLMULTI;
    389 		axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
    390 		return;
    391 	} else
    392 		rxmode &= ~AXE_RXCMD_ALLMULTI;
    393 
    394 	/* now program new ones */
    395 #if defined(__NetBSD__)
    396 	ETHER_FIRST_MULTI(step, &sc->axe_ec, enm);
    397 #else
    398 	ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
    399 #endif
    400 	while (enm != NULL) {
    401 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    402 			   ETHER_ADDR_LEN) != 0)
    403 			goto allmulti;
    404 
    405 		h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26;
    406 		hashtbl[h / 8] |= 1 << (h % 8);
    407 		ETHER_NEXT_MULTI(step, enm);
    408 	}
    409 
    410 	ifp->if_flags &= ~IFF_ALLMULTI;
    411 	axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
    412 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
    413 	return;
    414 }
    415 
    416 Static void
    417 axe_reset(struct axe_softc *sc)
    418 {
    419 	if (sc->axe_dying)
    420 		return;
    421 	/* XXX What to reset? */
    422 
    423 	/* Wait a little while for the chip to get its brains in order. */
    424 	DELAY(1000);
    425 	return;
    426 }
    427 
    428 /*
    429  * Probe for a AX88172 chip.
    430  */
    431 USB_MATCH(axe)
    432 {
    433 	USB_MATCH_START(axe, uaa);
    434 
    435 	if (!uaa->iface) {
    436 		return(UMATCH_NONE);
    437 	}
    438 
    439 	return (axe_lookup(uaa->vendor, uaa->product) != NULL ?
    440 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    441 }
    442 
    443 /*
    444  * Attach the interface. Allocate softc structures, do ifmedia
    445  * setup and ethernet/BPF attach.
    446  */
    447 USB_ATTACH(axe)
    448 {
    449 	USB_ATTACH_START(axe, sc, uaa);
    450 	usbd_device_handle dev = uaa->device;
    451 	usbd_status err;
    452 	usb_interface_descriptor_t *id;
    453 	usb_endpoint_descriptor_t *ed;
    454 	struct mii_data	*mii;
    455 	u_char eaddr[ETHER_ADDR_LEN];
    456 	char *devinfop;
    457 	char *devname = USBDEVNAME(sc->axe_dev);
    458 	struct ifnet *ifp;
    459 	int i, s;
    460 
    461 	devinfop = usbd_devinfo_alloc(dev, 0);
    462 	USB_ATTACH_SETUP;
    463 
    464 	err = usbd_set_config_no(dev, AXE_CONFIG_NO, 1);
    465 	if (err) {
    466 		printf("%s: getting interface handle failed\n",
    467 		    USBDEVNAME(sc->axe_dev));
    468 		USB_ATTACH_ERROR_RETURN;
    469 	}
    470 
    471 	usb_init_task(&sc->axe_tick_task, axe_tick_task, sc);
    472 	lockinit(&sc->axe_mii_lock, PZERO, "axemii", 0, LK_CANRECURSE);
    473 	usb_init_task(&sc->axe_stop_task, (void (*)(void *))axe_stop, sc);
    474 
    475 	err = usbd_device2interface_handle(dev, AXE_IFACE_IDX, &sc->axe_iface);
    476 	if (err) {
    477 		printf("%s: getting interface handle failed\n",
    478 		    USBDEVNAME(sc->axe_dev));
    479 		USB_ATTACH_ERROR_RETURN;
    480 	}
    481 
    482 	sc->axe_udev = dev;
    483 	sc->axe_product = uaa->product;
    484 	sc->axe_vendor = uaa->vendor;
    485 
    486 	id = usbd_get_interface_descriptor(sc->axe_iface);
    487 
    488 	printf("%s: %s\n", USBDEVNAME(sc->axe_dev), devinfop);
    489 	usbd_devinfo_free(devinfop);
    490 
    491 	/* Find endpoints. */
    492 	for (i = 0; i < id->bNumEndpoints; i++) {
    493 		ed = usbd_interface2endpoint_descriptor(sc->axe_iface, i);
    494 		if (!ed) {
    495 			printf("%s: couldn't get ep %d\n",
    496 			    USBDEVNAME(sc->axe_dev), i);
    497 			USB_ATTACH_ERROR_RETURN;
    498 		}
    499 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    500 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    501 			sc->axe_ed[AXE_ENDPT_RX] = ed->bEndpointAddress;
    502 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    503 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    504 			sc->axe_ed[AXE_ENDPT_TX] = ed->bEndpointAddress;
    505 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    506 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    507 			sc->axe_ed[AXE_ENDPT_INTR] = ed->bEndpointAddress;
    508 		}
    509 	}
    510 
    511 	s = splnet();
    512 
    513 	/*
    514 	 * Get station address.
    515 	 */
    516 	axe_cmd(sc, AXE_CMD_READ_NODEID, 0, 0, &eaddr);
    517 
    518 	/*
    519 	 * Load IPG values and PHY indexes.
    520 	 */
    521 	axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, (void *)&sc->axe_ipgs);
    522 	axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, (void *)&sc->axe_phyaddrs);
    523 
    524 	/*
    525 	 * Work around broken adapters that appear to lie about
    526 	 * their PHY addresses.
    527 	 */
    528 	sc->axe_phyaddrs[0] = sc->axe_phyaddrs[1] = 0xFF;
    529 
    530 	/*
    531 	 * An ASIX chip was detected. Inform the world.
    532 	 */
    533 	printf("%s: Ethernet address %s\n", USBDEVNAME(sc->axe_dev),
    534 	    ether_sprintf(eaddr));
    535 
    536 	/* Initialize interface info.*/
    537 	ifp = GET_IFP(sc);
    538 	ifp->if_softc = sc;
    539 	strncpy(ifp->if_xname, devname, IFNAMSIZ);
    540 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    541 	ifp->if_ioctl = axe_ioctl;
    542 	ifp->if_start = axe_start;
    543 
    544 	ifp->if_watchdog = axe_watchdog;
    545 
    546 /*	ifp->if_baudrate = 10000000; */
    547 /*	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;*/
    548 
    549 	IFQ_SET_READY(&ifp->if_snd);
    550 
    551 	/* Initialize MII/media info. */
    552 	mii = &sc->axe_mii;
    553 	mii->mii_ifp = ifp;
    554 	mii->mii_readreg = axe_miibus_readreg;
    555 	mii->mii_writereg = axe_miibus_writereg;
    556 	mii->mii_statchg = axe_miibus_statchg;
    557 	mii->mii_flags = MIIF_AUTOTSLEEP;
    558 
    559 	ifmedia_init(&mii->mii_media, 0, axe_ifmedia_upd, axe_ifmedia_sts);
    560 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
    561 
    562 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
    563 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
    564 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
    565 	} else
    566 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    567 
    568 	/* Attach the interface. */
    569 	if_attach(ifp);
    570 	Ether_ifattach(ifp, eaddr);
    571 #if NRND > 0
    572 	rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->axe_dev),
    573 	    RND_TYPE_NET, 0);
    574 #endif
    575 
    576 	usb_callout_init(sc->axe_stat_ch);
    577 
    578 	sc->axe_attached = 1;
    579 	splx(s);
    580 
    581 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->axe_udev,
    582 			   USBDEV(sc->axe_dev));
    583 
    584 	USB_ATTACH_SUCCESS_RETURN;
    585 }
    586 
    587 USB_DETACH(axe)
    588 {
    589 	USB_DETACH_START(axe, sc);
    590 	int			s;
    591 	struct ifnet		*ifp = GET_IFP(sc);
    592 
    593 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
    594 
    595 	/* Detached before attached finished, so just bail out. */
    596 	if (!sc->axe_attached)
    597 		return (0);
    598 
    599 	usb_uncallout(sc->axe_stat_ch, axe_tick, sc);
    600 
    601 	sc->axe_dying = 1;
    602 
    603 	ether_ifdetach(ifp);
    604 
    605 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL)
    606 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
    607 	if (sc->axe_ep[AXE_ENDPT_RX] != NULL)
    608 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
    609 	if (sc->axe_ep[AXE_ENDPT_INTR] != NULL)
    610 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
    611 
    612 	/*
    613 	 * Remove any pending tasks.  They cannot be executing because they run
    614 	 * in the same thread as detach.
    615 	 */
    616 	usb_rem_task(sc->axe_udev, &sc->axe_tick_task);
    617 	usb_rem_task(sc->axe_udev, &sc->axe_stop_task);
    618 
    619 	s = splusb();
    620 
    621 	if (--sc->axe_refcnt >= 0) {
    622 		/* Wait for processes to go away */
    623 		usb_detach_wait(USBDEV(sc->axe_dev));
    624 	}
    625 
    626 	if (ifp->if_flags & IFF_RUNNING)
    627 		axe_stop(sc);
    628 
    629 #if defined(__NetBSD__)
    630 #if NRND > 0
    631 	rnd_detach_source(&sc->rnd_source);
    632 #endif
    633 #endif /* __NetBSD__ */
    634 	mii_detach(&sc->axe_mii, MII_PHY_ANY, MII_OFFSET_ANY);
    635 	ifmedia_delete_instance(&sc->axe_mii.mii_media, IFM_INST_ANY);
    636 	ether_ifdetach(ifp);
    637 	if_detach(ifp);
    638 
    639 #ifdef DIAGNOSTIC
    640 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL ||
    641 	    sc->axe_ep[AXE_ENDPT_RX] != NULL ||
    642 	    sc->axe_ep[AXE_ENDPT_INTR] != NULL)
    643 		printf("%s: detach has active endpoints\n",
    644 		       USBDEVNAME(sc->axe_dev));
    645 #endif
    646 
    647 	sc->axe_attached = 0;
    648 
    649 	if (--sc->axe_refcnt >= 0) {
    650 		/* Wait for processes to go away. */
    651 		usb_detach_wait(USBDEV(sc->axe_dev));
    652 	}
    653 	splx(s);
    654 
    655 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->axe_udev,
    656 			   USBDEV(sc->axe_dev));
    657 
    658 	return (0);
    659 }
    660 
    661 int
    662 axe_activate(device_ptr_t self, enum devact act)
    663 {
    664 	struct axe_softc *sc = (struct axe_softc *)self;
    665 
    666 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
    667 
    668 	switch (act) {
    669 	case DVACT_ACTIVATE:
    670 		return (EOPNOTSUPP);
    671 		break;
    672 
    673 	case DVACT_DEACTIVATE:
    674 		if_deactivate(&sc->axe_ec.ec_if);
    675 		sc->axe_dying = 1;
    676 		break;
    677 	}
    678 	return (0);
    679 }
    680 
    681 /*
    682  * Initialize an RX descriptor and attach an MBUF cluster.
    683  */
    684 Static int
    685 axe_newbuf(struct axe_softc *sc, struct axe_chain *c, struct mbuf *m)
    686 {
    687 	struct mbuf		*m_new = NULL;
    688 
    689 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev),__func__));
    690 
    691 	if (m == NULL) {
    692 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    693 		if (m_new == NULL) {
    694 			printf("%s: no memory for rx list "
    695 			    "-- packet dropped!\n", USBDEVNAME(sc->axe_dev));
    696 			return (ENOBUFS);
    697 		}
    698 
    699 		MCLGET(m_new, M_DONTWAIT);
    700 		if (!(m_new->m_flags & M_EXT)) {
    701 			printf("%s: no memory for rx list "
    702 			    "-- packet dropped!\n", USBDEVNAME(sc->axe_dev));
    703 			m_freem(m_new);
    704 			return (ENOBUFS);
    705 		}
    706 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    707 	} else {
    708 		m_new = m;
    709 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    710 		m_new->m_data = m_new->m_ext.ext_buf;
    711 	}
    712 
    713 	m_adj(m_new, ETHER_ALIGN);
    714 	c->axe_mbuf = m_new;
    715 
    716 	return (0);
    717 }
    718 
    719 Static int
    720 axe_rx_list_init(struct axe_softc *sc)
    721 {
    722 	struct axe_cdata *cd;
    723 	struct axe_chain *c;
    724 	int i;
    725 
    726 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
    727 
    728 	cd = &sc->axe_cdata;
    729 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
    730 		c = &cd->axe_rx_chain[i];
    731 		c->axe_sc = sc;
    732 		c->axe_idx = i;
    733 		if (axe_newbuf(sc, c, NULL) == ENOBUFS)
    734 			return (ENOBUFS);
    735 		if (c->axe_xfer == NULL) {
    736 			c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
    737 			if (c->axe_xfer == NULL)
    738 				return (ENOBUFS);
    739 			c->axe_buf = usbd_alloc_buffer(c->axe_xfer, AXE_BUFSZ);
    740 			if (c->axe_buf == NULL) {
    741 				usbd_free_xfer(c->axe_xfer);
    742 				return (ENOBUFS);
    743 			}
    744 		}
    745 	}
    746 
    747 	return (0);
    748 }
    749 
    750 Static int
    751 axe_tx_list_init(struct axe_softc *sc)
    752 {
    753 	struct axe_cdata *cd;
    754 	struct axe_chain *c;
    755 	int i;
    756 
    757 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
    758 
    759 	cd = &sc->axe_cdata;
    760 	for (i = 0; i < AXE_TX_LIST_CNT; i++) {
    761 		c = &cd->axe_tx_chain[i];
    762 		c->axe_sc = sc;
    763 		c->axe_idx = i;
    764 		c->axe_mbuf = NULL;
    765 		if (c->axe_xfer == NULL) {
    766 			c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
    767 			if (c->axe_xfer == NULL)
    768 				return (ENOBUFS);
    769 			c->axe_buf = usbd_alloc_buffer(c->axe_xfer, AXE_BUFSZ);
    770 			if (c->axe_buf == NULL) {
    771 				usbd_free_xfer(c->axe_xfer);
    772 				return (ENOBUFS);
    773 			}
    774 		}
    775 	}
    776 
    777 	return (0);
    778 }
    779 
    780 #if 0
    781 Static void
    782 axe_rxstart(struct ifnet *ifp)
    783 {
    784 	struct axe_softc	*sc;
    785 	struct axe_chain	*c;
    786 
    787 	sc = ifp->if_softc;
    788 	axe_lock_mii(sc);
    789 	c = &sc->axe_cdata.axe_rx_chain[sc->axe_cdata.axe_rx_prod];
    790 
    791 	if (axe_newbuf(sc, c, NULL) == ENOBUFS) {
    792 		ifp->if_ierrors++;
    793 		axe_unlock_mii(sc);
    794 		return;
    795 	}
    796 
    797 	/* Setup new transfer. */
    798 	usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
    799 	    c, mtod(c->axe_mbuf, char *), AXE_BUFSZ, USBD_SHORT_XFER_OK,
    800 	    USBD_NO_TIMEOUT, axe_rxeof);
    801 	usbd_transfer(c->axe_xfer);
    802 	axe_unlock_mii(sc);
    803 
    804 	return;
    805 }
    806 #endif
    807 
    808 /*
    809  * A frame has been uploaded: pass the resulting mbuf chain up to
    810  * the higher level protocols.
    811  */
    812 Static void
    813 axe_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    814 {
    815 	struct axe_softc	*sc;
    816 	struct axe_chain	*c;
    817 	struct ifnet		*ifp;
    818 	struct mbuf		*m;
    819 	u_int32_t		total_len;
    820 	int			s;
    821 
    822 	c = priv;
    823 	sc = c->axe_sc;
    824 	ifp = GET_IFP(sc);
    825 
    826 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev),__func__));
    827 
    828 	if (sc->axe_dying)
    829 		return;
    830 
    831 	if (!(ifp->if_flags & IFF_RUNNING))
    832 		return;
    833 
    834 	if (status != USBD_NORMAL_COMPLETION) {
    835 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    836 			return;
    837 		if (usbd_ratecheck(&sc->axe_rx_notice)) {
    838 			printf("%s: usb errors on rx: %s\n",
    839 			    USBDEVNAME(sc->axe_dev), usbd_errstr(status));
    840 		}
    841 		if (status == USBD_STALLED)
    842 			usbd_clear_endpoint_stall(sc->axe_ep[AXE_ENDPT_RX]);
    843 		goto done;
    844 	}
    845 
    846 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
    847 
    848 	m = c->axe_mbuf;
    849 
    850 	if (total_len <= sizeof(struct ether_header)) {
    851 		ifp->if_ierrors++;
    852 		goto done;
    853 	}
    854 
    855 	ifp->if_ipackets++;
    856 	m->m_pkthdr.rcvif = ifp;
    857 	m->m_pkthdr.len = m->m_len = total_len;
    858 
    859 
    860 	memcpy(mtod(c->axe_mbuf, char *), c->axe_buf, total_len);
    861 
    862 	/* No errors; receive the packet. */
    863 	total_len -= ETHER_CRC_LEN + 4;
    864 
    865 	s = splnet();
    866 
    867 	/* XXX ugly */
    868 	if (axe_newbuf(sc, c, NULL) == ENOBUFS) {
    869 		ifp->if_ierrors++;
    870 		goto done1;
    871 	}
    872 
    873 #if NBPFILTER > 0
    874 	if (ifp->if_bpf)
    875 		BPF_MTAP(ifp, m);
    876 #endif
    877 
    878 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->axe_dev),
    879 		    __func__, m->m_len));
    880 	IF_INPUT(ifp, m);
    881  done1:
    882 	splx(s);
    883 
    884  done:
    885 
    886 	/* Setup new transfer. */
    887 	usbd_setup_xfer(xfer, sc->axe_ep[AXE_ENDPT_RX],
    888 	    c, c->axe_buf, AXE_BUFSZ,
    889 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
    890 	    USBD_NO_TIMEOUT, axe_rxeof);
    891 	usbd_transfer(xfer);
    892 
    893 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->axe_dev),
    894 		    __func__));
    895 	return;
    896 }
    897 
    898 /*
    899  * A frame was downloaded to the chip. It's safe for us to clean up
    900  * the list buffers.
    901  */
    902 
    903 Static void
    904 axe_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, 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(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);
    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_cmd(sc, AXE_CMD_WRITE_IPG0, 0, sc->axe_ipgs[0], NULL);
   1123 	axe_cmd(sc, AXE_CMD_WRITE_IPG1, 0, sc->axe_ipgs[1], NULL);
   1124 	axe_cmd(sc, AXE_CMD_WRITE_IPG2, 0, sc->axe_ipgs[2], NULL);
   1125 
   1126 	/* Enable receiver, set RX mode */
   1127 	rxmode = AXE_RXCMD_UNICAST|AXE_RXCMD_MULTICAST|AXE_RXCMD_ENABLE;
   1128 
   1129 	/* If we want promiscuous mode, set the allframes bit. */
   1130 	if (ifp->if_flags & IFF_PROMISC)
   1131 		rxmode |= AXE_RXCMD_PROMISC;
   1132 
   1133 	if (ifp->if_flags & IFF_BROADCAST)
   1134 		rxmode |= AXE_RXCMD_BROADCAST;
   1135 
   1136 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
   1137 
   1138 	/* Load the multicast filter. */
   1139 	axe_setmulti(sc);
   1140 
   1141 	/* Open RX and TX pipes. */
   1142 	err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_RX],
   1143 	    USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_RX]);
   1144 	if (err) {
   1145 		printf("%s: open rx pipe failed: %s\n",
   1146 		    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1147 		splx(s);
   1148 		return;
   1149 	}
   1150 
   1151 	err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_TX],
   1152 	    USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_TX]);
   1153 	if (err) {
   1154 		printf("%s: open tx pipe failed: %s\n",
   1155 		    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1156 		splx(s);
   1157 		return;
   1158 	}
   1159 
   1160 	/* Start up the receive pipe. */
   1161 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
   1162 		c = &sc->axe_cdata.axe_rx_chain[i];
   1163 		usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
   1164 		    c, mtod(c->axe_mbuf, char *), AXE_BUFSZ,
   1165 		    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, axe_rxeof);
   1166 		usbd_transfer(c->axe_xfer);
   1167 	}
   1168 
   1169 	ifp->if_flags |= IFF_RUNNING;
   1170 	ifp->if_flags &= ~IFF_OACTIVE;
   1171 
   1172 	splx(s);
   1173 
   1174 	usb_callout_init(sc->axe_stat_ch);
   1175 	usb_callout(sc->axe_stat_ch, hz, axe_tick, sc);
   1176 	return;
   1177 }
   1178 
   1179 Static int
   1180 axe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   1181 {
   1182 	struct axe_softc	*sc = ifp->if_softc;
   1183 	struct ifreq		*ifr = (struct ifreq *)data;
   1184 	struct ifaddr		*ifa = (struct ifaddr *)data;
   1185 	struct mii_data		*mii;
   1186 	u_int16_t		rxmode;
   1187 	int			error = 0;
   1188 
   1189 	switch(cmd) {
   1190 	case SIOCSIFADDR:
   1191 		ifp->if_flags |= IFF_UP;
   1192 		axe_init(sc);
   1193 
   1194 		switch (ifa->ifa_addr->sa_family) {
   1195 #ifdef INET
   1196 		case AF_INET:
   1197 #if defined(__NetBSD__)
   1198 			arp_ifinit(ifp, ifa);
   1199 #else
   1200 			arp_ifinit(&sc->arpcom, ifa);
   1201 #endif
   1202 			break;
   1203 #endif /* INET */
   1204 #ifdef NS
   1205 		case AF_NS:
   1206 		    {
   1207 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1208 
   1209 			if (ns_nullhost(*ina))
   1210 				ina->x_host = *(union ns_host *)
   1211 					LLADDR(ifp->if_sadl);
   1212 			else
   1213 				memcpy(LLADDR(ifp->if_sadl),
   1214 				       ina->x_host.c_host,
   1215 				       ifp->if_addrlen);
   1216 			break;
   1217 		    }
   1218 #endif /* NS */
   1219 		}
   1220 		break;
   1221 
   1222 	case SIOCSIFMTU:
   1223 		if (ifr->ifr_mtu > ETHERMTU)
   1224 			error = EINVAL;
   1225 		else
   1226 			ifp->if_mtu = ifr->ifr_mtu;
   1227 		break;
   1228 
   1229 	case SIOCSIFFLAGS:
   1230 		if (ifp->if_flags & IFF_UP) {
   1231 			if (ifp->if_flags & IFF_RUNNING &&
   1232 			    ifp->if_flags & IFF_PROMISC &&
   1233 			    !(sc->axe_if_flags & IFF_PROMISC)) {
   1234 
   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 
   1241 				axe_setmulti(sc);
   1242 			} else if (ifp->if_flags & IFF_RUNNING &&
   1243 			    !(ifp->if_flags & IFF_PROMISC) &&
   1244 			    sc->axe_if_flags & IFF_PROMISC) {
   1245 				axe_cmd(sc, AXE_CMD_RXCTL_READ,
   1246 					0, 0, (void *)&rxmode);
   1247 				rxmode = le16toh(rxmode) & ~AXE_RXCMD_PROMISC;
   1248 				axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
   1249 					0, rxmode, NULL);
   1250 				axe_setmulti(sc);
   1251 			} else if (!(ifp->if_flags & IFF_RUNNING))
   1252 				axe_init(sc);
   1253 		} else {
   1254 			if (ifp->if_flags & IFF_RUNNING)
   1255 				axe_stop(sc);
   1256 		}
   1257 		sc->axe_if_flags = ifp->if_flags;
   1258 		error = 0;
   1259 		break;
   1260 	case SIOCADDMULTI:
   1261 	case SIOCDELMULTI:
   1262 #ifdef __NetBSD__
   1263 		error = (cmd == SIOCADDMULTI) ?
   1264 			ether_addmulti(ifr, &sc->axe_ec) :
   1265 			ether_delmulti(ifr, &sc->axe_ec);
   1266 #else
   1267 		error = (cmd == SIOCADDMULTI) ?
   1268 		    ether_addmulti(ifr, &sc->arpcom) :
   1269 		    ether_delmulti(ifr, &sc->arpcom);
   1270 #endif /* __NetBSD__ */
   1271 		if (error == ENETRESET) {
   1272 			/*
   1273 			 * Multicast list has changed; set the hardware
   1274 			 * filter accordingly.
   1275 			 */
   1276 			if (ifp->if_flags & IFF_RUNNING)
   1277 				axe_setmulti(sc);
   1278 			error = 0;
   1279 		}
   1280 		break;
   1281 	case SIOCGIFMEDIA:
   1282 	case SIOCSIFMEDIA:
   1283 		mii = GET_MII(sc);
   1284 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
   1285 		break;
   1286 
   1287 	default:
   1288 		error = EINVAL;
   1289 		break;
   1290 	}
   1291 
   1292 	return(error);
   1293 }
   1294 
   1295 /*
   1296  * XXX
   1297  * You can't call axe_txeof since the USB transfer has not
   1298  * completed yet.
   1299  */
   1300 Static void
   1301 axe_watchdog(struct ifnet *ifp)
   1302 {
   1303 	struct axe_softc	*sc;
   1304 	struct axe_chain	*c;
   1305 	usbd_status		stat;
   1306 	int			s;
   1307 
   1308 	sc = ifp->if_softc;
   1309 
   1310 	ifp->if_oerrors++;
   1311 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->axe_dev));
   1312 
   1313 	s = splusb();
   1314 	c = &sc->axe_cdata.axe_tx_chain[0];
   1315 	usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &stat);
   1316 	axe_txeof(c->axe_xfer, c, stat);
   1317 
   1318 	if (ifp->if_snd.ifq_head != NULL)
   1319 		axe_start(ifp);
   1320 	splx(s);
   1321 }
   1322 
   1323 /*
   1324  * Stop the adapter and free any mbufs allocated to the
   1325  * RX and TX lists.
   1326  */
   1327 Static void
   1328 axe_stop(struct axe_softc *sc)
   1329 {
   1330 	usbd_status		err;
   1331 	struct ifnet		*ifp;
   1332 	int			i;
   1333 
   1334 	axe_reset(sc);
   1335 
   1336 	ifp = GET_IFP(sc);
   1337 	ifp->if_timer = 0;
   1338 
   1339 	usb_uncallout(sc->axe_stat_ch, axe_tick, sc);
   1340 
   1341 	/* Stop transfers. */
   1342 	if (sc->axe_ep[AXE_ENDPT_RX] != NULL) {
   1343 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
   1344 		if (err) {
   1345 			printf("%s: abort rx pipe failed: %s\n",
   1346 		    	    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1347 		}
   1348 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_RX]);
   1349 		if (err) {
   1350 			printf("%s: close rx pipe failed: %s\n",
   1351 		    	    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1352 		}
   1353 		sc->axe_ep[AXE_ENDPT_RX] = NULL;
   1354 	}
   1355 
   1356 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL) {
   1357 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
   1358 		if (err) {
   1359 			printf("%s: abort tx pipe failed: %s\n",
   1360 		    	    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1361 		}
   1362 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_TX]);
   1363 		if (err) {
   1364 			printf("%s: close tx pipe failed: %s\n",
   1365 			    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1366 		}
   1367 		sc->axe_ep[AXE_ENDPT_TX] = NULL;
   1368 	}
   1369 
   1370 	if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) {
   1371 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
   1372 		if (err) {
   1373 			printf("%s: abort intr pipe failed: %s\n",
   1374 		    	    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1375 		}
   1376 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
   1377 		if (err) {
   1378 			printf("%s: close intr pipe failed: %s\n",
   1379 			    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1380 		}
   1381 		sc->axe_ep[AXE_ENDPT_INTR] = NULL;
   1382 	}
   1383 
   1384 	/* Free RX resources. */
   1385 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
   1386 		if (sc->axe_cdata.axe_rx_chain[i].axe_mbuf != NULL) {
   1387 			m_freem(sc->axe_cdata.axe_rx_chain[i].axe_mbuf);
   1388 			sc->axe_cdata.axe_rx_chain[i].axe_mbuf = NULL;
   1389 		}
   1390 		if (sc->axe_cdata.axe_rx_chain[i].axe_xfer != NULL) {
   1391 			usbd_free_xfer(sc->axe_cdata.axe_rx_chain[i].axe_xfer);
   1392 			sc->axe_cdata.axe_rx_chain[i].axe_xfer = NULL;
   1393 		}
   1394 	}
   1395 
   1396 	/* Free TX resources. */
   1397 	for (i = 0; i < AXE_TX_LIST_CNT; i++) {
   1398 		if (sc->axe_cdata.axe_tx_chain[i].axe_mbuf != NULL) {
   1399 			m_freem(sc->axe_cdata.axe_tx_chain[i].axe_mbuf);
   1400 			sc->axe_cdata.axe_tx_chain[i].axe_mbuf = NULL;
   1401 		}
   1402 		if (sc->axe_cdata.axe_tx_chain[i].axe_xfer != NULL) {
   1403 			usbd_free_xfer(sc->axe_cdata.axe_tx_chain[i].axe_xfer);
   1404 			sc->axe_cdata.axe_tx_chain[i].axe_xfer = NULL;
   1405 		}
   1406 	}
   1407 
   1408 	sc->axe_link = 0;
   1409 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1410 }
   1411 
   1412