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