Home | History | Annotate | Line # | Download | only in usb
if_axe.c revision 1.17.10.1
      1 /*	$NetBSD: if_axe.c,v 1.17.10.1 2007/05/22 14:57:36 itohy 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.17.10.1 2007/05/22 14:57:36 itohy 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/lock.h>
     89 #include <sys/mbuf.h>
     90 #include <sys/kernel.h>
     91 #if defined(__OpenBSD__)
     92 #include <sys/proc.h>
     93 #endif
     94 #include <sys/socket.h>
     95 
     96 #include <sys/device.h>
     97 #if NRND > 0
     98 #include <sys/rnd.h>
     99 #endif
    100 
    101 #include <net/if.h>
    102 #if defined(__NetBSD__)
    103 #include <net/if_arp.h>
    104 #endif
    105 #include <net/if_dl.h>
    106 #include <net/if_media.h>
    107 
    108 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m))
    109 
    110 #if NBPFILTER > 0
    111 #include <net/bpf.h>
    112 #endif
    113 
    114 #if defined(__NetBSD__)
    115 #include <net/if_ether.h>
    116 #ifdef INET
    117 #include <netinet/in.h>
    118 #include <netinet/if_inarp.h>
    119 #endif
    120 #endif /* defined(__NetBSD__) */
    121 
    122 #if defined(__OpenBSD__)
    123 #ifdef INET
    124 #include <netinet/in.h>
    125 #include <netinet/in_systm.h>
    126 #include <netinet/in_var.h>
    127 #include <netinet/ip.h>
    128 #include <netinet/if_ether.h>
    129 #endif
    130 #endif /* defined(__OpenBSD__) */
    131 
    132 
    133 #include <dev/mii/mii.h>
    134 #include <dev/mii/miivar.h>
    135 
    136 #include <dev/usb/usb.h>
    137 #include <dev/usb/usbdi.h>
    138 #include <dev/usb/usbdi_util.h>
    139 #include <dev/usb/usbdevs.h>
    140 
    141 #include <dev/usb/if_axereg.h>
    142 
    143 #ifdef AXE_DEBUG
    144 #define DPRINTF(x)	do { if (axedebug) logprintf x; } while (0)
    145 #define DPRINTFN(n,x)	do { if (axedebug >= (n)) logprintf x; } while (0)
    146 int	axedebug = 0;
    147 #else
    148 #define DPRINTF(x)
    149 #define DPRINTFN(n,x)
    150 #endif
    151 
    152 /*
    153  * Various supported device vendors/products.
    154  */
    155 Static const struct axe_type axe_devs[] = {
    156 	{ { USB_VENDOR_ASIX,		USB_PRODUCT_ASIX_AX88172}, 0 },
    157 	{ { USB_VENDOR_COREGA,		USB_PRODUCT_COREGA_FETHER_USB2_TX }, 0},
    158 	{ { USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DUBE100}, 0 },
    159 	{ { USB_VENDOR_LINKSYS2,	USB_PRODUCT_LINKSYS2_USB200M}, 0 },
    160 	{ { USB_VENDOR_MELCO,		USB_PRODUCT_MELCO_LUAU2KTX}, 0 },
    161 	{ { USB_VENDOR_NETGEAR,		USB_PRODUCT_NETGEAR_FA120}, 0 },
    162 	{ { USB_VENDOR_SITECOM,		USB_PRODUCT_SITECOM_LN029}, 0 },
    163 	{ { USB_VENDOR_SYSTEMTALKS,	USB_PRODUCT_SYSTEMTALKS_SGCX2UL}, 0 },
    164 };
    165 #define axe_lookup(v, p) ((const struct axe_type *)usb_lookup(axe_devs, v, p))
    166 
    167 USB_DECLARE_DRIVER(axe);
    168 
    169 Static int axe_tx_list_init(struct axe_softc *);
    170 Static int axe_rx_list_init(struct axe_softc *);
    171 Static int axe_newbuf(struct axe_softc *, struct axe_chain *, struct mbuf *);
    172 Static int axe_encap(struct axe_softc *, struct mbuf *, int);
    173 Static void axe_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
    174 Static void axe_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
    175 Static void axe_tick(void *);
    176 Static void axe_tick_task(void *);
    177 #if 0
    178 Static void axe_rxstart(struct ifnet *);
    179 #endif
    180 Static void axe_start(struct ifnet *);
    181 Static int axe_ioctl(struct ifnet *, u_long, caddr_t);
    182 Static void axe_init(void *);
    183 Static void axe_stop(struct axe_softc *);
    184 Static void axe_watchdog(struct ifnet *);
    185 Static int axe_miibus_readreg(device_ptr_t, int, int);
    186 Static void axe_miibus_writereg(device_ptr_t, int, int, int);
    187 Static void axe_miibus_statchg(device_ptr_t);
    188 Static int axe_cmd(struct axe_softc *, int, int, int, void *);
    189 Static int axe_ifmedia_upd(struct ifnet *);
    190 Static void axe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
    191 Static void axe_reset(struct axe_softc *sc);
    192 
    193 Static void axe_setmulti(struct axe_softc *);
    194 Static void axe_lock_mii(struct axe_softc *sc);
    195 Static void axe_unlock_mii(struct axe_softc *sc);
    196 
    197 /* Get exclusive access to the MII registers */
    198 Static void
    199 axe_lock_mii(struct axe_softc *sc)
    200 {
    201 	sc->axe_refcnt++;
    202 	usb_lockmgr(&sc->axe_mii_lock, LK_EXCLUSIVE, NULL);
    203 }
    204 
    205 Static void
    206 axe_unlock_mii(struct axe_softc *sc)
    207 {
    208 	usb_lockmgr(&sc->axe_mii_lock, LK_RELEASE, NULL);
    209 	if (--sc->axe_refcnt < 0)
    210 		usb_detach_wakeup(USBDEV(sc->axe_dev));
    211 }
    212 
    213 Static int
    214 axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
    215 {
    216 	usb_device_request_t	req;
    217 	usbd_status		err;
    218 
    219 	if (sc->axe_dying)
    220 		return(0);
    221 
    222 	axe_lock_mii(sc);
    223 	if (AXE_CMD_DIR(cmd))
    224 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    225 	else
    226 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
    227 	req.bRequest = AXE_CMD_CMD(cmd);
    228 	USETW(req.wValue, val);
    229 	USETW(req.wIndex, index);
    230 	USETW(req.wLength, AXE_CMD_LEN(cmd));
    231 
    232 	err = usbd_do_request(sc->axe_udev, &req, buf);
    233 	axe_unlock_mii(sc);
    234 
    235 	if (err)
    236 		return(-1);
    237 
    238 	return(0);
    239 }
    240 
    241 Static int
    242 axe_miibus_readreg(device_ptr_t dev, int phy, int reg)
    243 {
    244 	struct axe_softc	*sc = USBGETSOFTC(dev);
    245 	usbd_status		err;
    246 	u_int16_t		val;
    247 
    248 	if (sc->axe_dying) {
    249 		DPRINTF(("axe: dying\n"));
    250 		return(0);
    251 	}
    252 
    253 #ifdef notdef
    254 	/*
    255 	 * The chip tells us the MII address of any supported
    256 	 * PHYs attached to the chip, so only read from those.
    257 	 */
    258 
    259 	if (sc->axe_phyaddrs[0] != AXE_NOPHY && phy != sc->axe_phyaddrs[0])
    260 		return (0);
    261 
    262 	if (sc->axe_phyaddrs[1] != AXE_NOPHY && phy != sc->axe_phyaddrs[1])
    263 		return (0);
    264 #endif
    265 	if (sc->axe_phyaddrs[0] != 0xFF && sc->axe_phyaddrs[0] != phy)
    266 		return (0);
    267 
    268 	val = 0;
    269 
    270 	axe_lock_mii(sc);
    271 	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
    272 	err = axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, (void *)&val);
    273 	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
    274 	axe_unlock_mii(sc);
    275 
    276 	if (err) {
    277 		printf("%s: read PHY failed\n", USBDEVNAME(sc->axe_dev));
    278 		return(-1);
    279 	}
    280 
    281 	if (val)
    282 		sc->axe_phyaddrs[0] = phy;
    283 
    284 	return (le16toh(val));
    285 }
    286 
    287 Static void
    288 axe_miibus_writereg(device_ptr_t dev, int phy, int reg, int aval)
    289 {
    290 	struct axe_softc	*sc = USBGETSOFTC(dev);
    291 	usbd_status		err;
    292 	u_int16_t		val;
    293 
    294 	if (sc->axe_dying)
    295 		return;
    296 
    297 	val = htole16(aval);
    298 	axe_lock_mii(sc);
    299 	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
    300 	err = axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, (void *)&val);
    301 	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
    302 	axe_unlock_mii(sc);
    303 
    304 	if (err) {
    305 		printf("%s: write PHY failed\n", USBDEVNAME(sc->axe_dev));
    306 		return;
    307 	}
    308 }
    309 
    310 Static void
    311 axe_miibus_statchg(device_ptr_t dev)
    312 {
    313 	struct axe_softc	*sc = USBGETSOFTC(dev);
    314 	struct mii_data		*mii = GET_MII(sc);
    315 	int val, err;
    316 
    317 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
    318 		val = AXE_MEDIA_FULL_DUPLEX;
    319 	else
    320 		val = 0;
    321 	DPRINTF(("axe_miibus_statchg: val=0x%x\n", val));
    322 	err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL);
    323 	if (err) {
    324 		printf("%s: media change failed\n", USBDEVNAME(sc->axe_dev));
    325 		return;
    326 	}
    327 }
    328 
    329 /*
    330  * Set media options.
    331  */
    332 Static int
    333 axe_ifmedia_upd(struct ifnet *ifp)
    334 {
    335         struct axe_softc        *sc = ifp->if_softc;
    336         struct mii_data         *mii = GET_MII(sc);
    337 
    338         sc->axe_link = 0;
    339         if (mii->mii_instance) {
    340                 struct mii_softc        *miisc;
    341                 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
    342                          mii_phy_reset(miisc);
    343         }
    344         mii_mediachg(mii);
    345 
    346         return (0);
    347 }
    348 
    349 /*
    350  * Report current media status.
    351  */
    352 Static void
    353 axe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
    354 {
    355         struct axe_softc        *sc = ifp->if_softc;
    356         struct mii_data         *mii = GET_MII(sc);
    357 
    358         mii_pollstat(mii);
    359         ifmr->ifm_active = mii->mii_media_active;
    360         ifmr->ifm_status = mii->mii_media_status;
    361 }
    362 
    363 Static void
    364 axe_setmulti(struct axe_softc *sc)
    365 {
    366 	struct ifnet		*ifp;
    367 	struct ether_multi *enm;
    368 	struct ether_multistep step;
    369 	u_int32_t		h = 0;
    370 	u_int16_t		rxmode;
    371 	u_int8_t		hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
    372 
    373 	if (sc->axe_dying)
    374 		return;
    375 
    376 	ifp = GET_IFP(sc);
    377 
    378 	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, (void *)&rxmode);
    379 	rxmode = le16toh(rxmode);
    380 
    381 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
    382 	allmulti:
    383 		rxmode |= AXE_RXCMD_ALLMULTI;
    384 		axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
    385 		return;
    386 	} else
    387 		rxmode &= ~AXE_RXCMD_ALLMULTI;
    388 
    389 	/* now program new ones */
    390 #if defined(__NetBSD__)
    391 	ETHER_FIRST_MULTI(step, &sc->axe_ec, enm);
    392 #else
    393 	ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
    394 #endif
    395 	while (enm != NULL) {
    396 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    397 			   ETHER_ADDR_LEN) != 0)
    398 			goto allmulti;
    399 
    400 		h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26;
    401 		hashtbl[h / 8] |= 1 << (h % 8);
    402 		ETHER_NEXT_MULTI(step, enm);
    403 	}
    404 
    405 	ifp->if_flags &= ~IFF_ALLMULTI;
    406 	axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
    407 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
    408 	return;
    409 }
    410 
    411 Static void
    412 axe_reset(struct axe_softc *sc)
    413 {
    414 	if (sc->axe_dying)
    415 		return;
    416 	/* XXX What to reset? */
    417 
    418 	/* Wait a little while for the chip to get its brains in order. */
    419 	DELAY(1000);
    420 	return;
    421 }
    422 
    423 /*
    424  * Probe for a AX88172 chip.
    425  */
    426 USB_MATCH(axe)
    427 {
    428 	USB_MATCH_START(axe, uaa);
    429 
    430 	if (!uaa->iface) {
    431 		return(UMATCH_NONE);
    432 	}
    433 
    434 	return (axe_lookup(uaa->vendor, uaa->product) != NULL ?
    435 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    436 }
    437 
    438 /*
    439  * Attach the interface. Allocate softc structures, do ifmedia
    440  * setup and ethernet/BPF attach.
    441  */
    442 USB_ATTACH(axe)
    443 {
    444 	USB_ATTACH_START(axe, sc, uaa);
    445 	usbd_device_handle dev = uaa->device;
    446 	usbd_status err;
    447 	usb_interface_descriptor_t *id;
    448 	usb_endpoint_descriptor_t *ed;
    449 	struct mii_data	*mii;
    450 	u_char eaddr[ETHER_ADDR_LEN];
    451 	char *devinfop;
    452 	char *devname = USBDEVNAME(sc->axe_dev);
    453 	struct ifnet *ifp;
    454 	int i, s;
    455 
    456 	devinfop = usbd_devinfo_alloc(dev, 0);
    457 	USB_ATTACH_SETUP;
    458 
    459 	err = usbd_set_config_no(dev, AXE_CONFIG_NO, 1);
    460 	if (err) {
    461 		printf("%s: getting interface handle failed\n",
    462 		    USBDEVNAME(sc->axe_dev));
    463                 usbd_devinfo_free(devinfop);
    464 		USB_ATTACH_ERROR_RETURN;
    465 	}
    466 
    467 	usb_init_task(&sc->axe_tick_task, axe_tick_task, sc);
    468 	lockinit(&sc->axe_mii_lock, PZERO, "axemii", 0, LK_CANRECURSE);
    469 	usb_init_task(&sc->axe_stop_task, (void (*)(void *))axe_stop, sc);
    470 
    471 	err = usbd_device2interface_handle(dev, AXE_IFACE_IDX, &sc->axe_iface);
    472 	if (err) {
    473 		printf("%s: getting interface handle failed\n",
    474 		    USBDEVNAME(sc->axe_dev));
    475                 usbd_devinfo_free(devinfop);
    476 		USB_ATTACH_ERROR_RETURN;
    477 	}
    478 
    479 	sc->axe_udev = dev;
    480 	sc->axe_product = uaa->product;
    481 	sc->axe_vendor = uaa->vendor;
    482 
    483 	id = usbd_get_interface_descriptor(sc->axe_iface);
    484 
    485 	printf("%s: %s\n", USBDEVNAME(sc->axe_dev), devinfop);
    486 	usbd_devinfo_free(devinfop);
    487 
    488 	/* Find endpoints. */
    489 	for (i = 0; i < id->bNumEndpoints; i++) {
    490 		ed = usbd_interface2endpoint_descriptor(sc->axe_iface, i);
    491 		if (!ed) {
    492 			printf("%s: couldn't get ep %d\n",
    493 			    USBDEVNAME(sc->axe_dev), i);
    494 			USB_ATTACH_ERROR_RETURN;
    495 		}
    496 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    497 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    498 			sc->axe_ed[AXE_ENDPT_RX] = ed->bEndpointAddress;
    499 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    500 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    501 			sc->axe_ed[AXE_ENDPT_TX] = ed->bEndpointAddress;
    502 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    503 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    504 			sc->axe_ed[AXE_ENDPT_INTR] = ed->bEndpointAddress;
    505 		}
    506 	}
    507 
    508 	s = splnet();
    509 
    510 	/*
    511 	 * Get station address.
    512 	 */
    513 	axe_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 			    sc->axe_ep[AXE_ENDPT_RX]);
    735 			if (c->axe_xfer == NULL)
    736 				return (ENOBUFS);
    737 			c->axe_buf = usbd_alloc_buffer(c->axe_xfer, AXE_BUFSZ);
    738 			if (c->axe_buf == NULL) {
    739 				usbd_free_xfer(c->axe_xfer);
    740 				return (ENOBUFS);
    741 			}
    742 		}
    743 	}
    744 
    745 	return (0);
    746 }
    747 
    748 Static int
    749 axe_tx_list_init(struct axe_softc *sc)
    750 {
    751 	struct axe_cdata *cd;
    752 	struct axe_chain *c;
    753 	int i;
    754 
    755 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
    756 
    757 	cd = &sc->axe_cdata;
    758 	for (i = 0; i < AXE_TX_LIST_CNT; i++) {
    759 		c = &cd->axe_tx_chain[i];
    760 		c->axe_sc = sc;
    761 		c->axe_idx = i;
    762 		c->axe_mbuf = NULL;
    763 		if (c->axe_xfer == NULL) {
    764 			c->axe_xfer = usbd_alloc_xfer(sc->axe_udev,
    765 			    sc->axe_ep[AXE_ENDPT_TX]);
    766 			if (c->axe_xfer == NULL)
    767 				return (ENOBUFS);
    768 			c->axe_buf = usbd_alloc_buffer(c->axe_xfer, AXE_BUFSZ);
    769 			if (c->axe_buf == NULL) {
    770 				usbd_free_xfer(c->axe_xfer);
    771 				return (ENOBUFS);
    772 			}
    773 		}
    774 	}
    775 
    776 	return (0);
    777 }
    778 
    779 #if 0
    780 Static void
    781 axe_rxstart(struct ifnet *ifp)
    782 {
    783 	struct axe_softc	*sc;
    784 	struct axe_chain	*c;
    785 
    786 	sc = ifp->if_softc;
    787 	axe_lock_mii(sc);
    788 	c = &sc->axe_cdata.axe_rx_chain[sc->axe_cdata.axe_rx_prod];
    789 
    790 	if (axe_newbuf(sc, c, NULL) == ENOBUFS) {
    791 		ifp->if_ierrors++;
    792 		axe_unlock_mii(sc);
    793 		return;
    794 	}
    795 
    796 	/* Setup new transfer. */
    797 	usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
    798 	    c, mtod(c->axe_mbuf, char *), AXE_BUFSZ, USBD_SHORT_XFER_OK,
    799 	    USBD_NO_TIMEOUT, axe_rxeof);
    800 	usbd_transfer(c->axe_xfer);
    801 	axe_unlock_mii(sc);
    802 
    803 	return;
    804 }
    805 #endif
    806 
    807 /*
    808  * A frame has been uploaded: pass the resulting mbuf chain up to
    809  * the higher level protocols.
    810  */
    811 Static void
    812 axe_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
    813 {
    814 	struct axe_softc	*sc;
    815 	struct axe_chain	*c;
    816 	struct ifnet		*ifp;
    817 	struct mbuf		*m;
    818 	u_int32_t		total_len;
    819 	int			s;
    820 
    821 	c = priv;
    822 	sc = c->axe_sc;
    823 	ifp = GET_IFP(sc);
    824 
    825 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev),__func__));
    826 
    827 	if (sc->axe_dying)
    828 		return;
    829 
    830 	if (!(ifp->if_flags & IFF_RUNNING))
    831 		return;
    832 
    833 	if (status != USBD_NORMAL_COMPLETION) {
    834 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
    835 			return;
    836 		if (usbd_ratecheck(&sc->axe_rx_notice)) {
    837 			printf("%s: usb errors on rx: %s\n",
    838 			    USBDEVNAME(sc->axe_dev), usbd_errstr(status));
    839 		}
    840 		if (status == USBD_STALLED)
    841 			usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_RX]);
    842 		goto done;
    843 	}
    844 
    845 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
    846 
    847 	m = c->axe_mbuf;
    848 
    849 	if (total_len <= sizeof(struct ether_header)) {
    850 		ifp->if_ierrors++;
    851 		goto done;
    852 	}
    853 
    854 	ifp->if_ipackets++;
    855 	m->m_pkthdr.rcvif = ifp;
    856 	m->m_pkthdr.len = m->m_len = total_len;
    857 
    858 
    859 	memcpy(mtod(c->axe_mbuf, char *), c->axe_buf, total_len);
    860 
    861 	/* No errors; receive the packet. */
    862 	total_len -= ETHER_CRC_LEN + 4;
    863 
    864 	s = splnet();
    865 
    866 	/* XXX ugly */
    867 	if (axe_newbuf(sc, c, NULL) == ENOBUFS) {
    868 		ifp->if_ierrors++;
    869 		goto done1;
    870 	}
    871 
    872 #if NBPFILTER > 0
    873 	if (ifp->if_bpf)
    874 		BPF_MTAP(ifp, m);
    875 #endif
    876 
    877 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->axe_dev),
    878 		    __func__, m->m_len));
    879 	IF_INPUT(ifp, m);
    880  done1:
    881 	splx(s);
    882 
    883  done:
    884 
    885 	/* Setup new transfer. */
    886 	usbd_setup_xfer(xfer, sc->axe_ep[AXE_ENDPT_RX],
    887 	    c, c->axe_buf, AXE_BUFSZ,
    888 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
    889 	    USBD_NO_TIMEOUT, axe_rxeof);
    890 	usbd_transfer(xfer);
    891 
    892 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->axe_dev),
    893 		    __func__));
    894 	return;
    895 }
    896 
    897 /*
    898  * A frame was downloaded to the chip. It's safe for us to clean up
    899  * the list buffers.
    900  */
    901 
    902 Static void
    903 axe_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
    904     usbd_status status)
    905 {
    906 	struct axe_softc	*sc;
    907 	struct axe_chain	*c;
    908 	struct ifnet		*ifp;
    909 	int			s;
    910 
    911 	c = priv;
    912 	sc = c->axe_sc;
    913 	ifp = GET_IFP(sc);
    914 
    915 	if (sc->axe_dying)
    916 		return;
    917 
    918 	s = splnet();
    919 
    920 	if (status != USBD_NORMAL_COMPLETION) {
    921 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
    922 			splx(s);
    923 			return;
    924 		}
    925 		ifp->if_oerrors++;
    926 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->axe_dev),
    927 		    usbd_errstr(status));
    928 		if (status == USBD_STALLED)
    929 			usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_TX]);
    930 		splx(s);
    931 		return;
    932 	}
    933 
    934 	ifp->if_timer = 0;
    935 	ifp->if_flags &= ~IFF_OACTIVE;
    936 
    937 	m_freem(c->axe_mbuf);
    938 	c->axe_mbuf = NULL;
    939 
    940 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    941 		axe_start(ifp);
    942 
    943 	ifp->if_opackets++;
    944 	splx(s);
    945 	return;
    946 }
    947 
    948 Static void
    949 axe_tick(void *xsc)
    950 {
    951 	struct axe_softc *sc = xsc;
    952 
    953 	if (sc == NULL)
    954 		return;
    955 
    956 	DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->axe_dev),
    957 			__func__));
    958 
    959 	if (sc->axe_dying)
    960 		return;
    961 
    962 	/* Perform periodic stuff in process context */
    963 	usb_add_task(sc->axe_udev, &sc->axe_tick_task, USB_TASKQ_DRIVER);
    964 
    965 }
    966 
    967 Static void
    968 axe_tick_task(void *xsc)
    969 {
    970 	int			s;
    971 	struct axe_softc	*sc;
    972 	struct ifnet		*ifp;
    973 	struct mii_data		*mii;
    974 
    975 	sc = xsc;
    976 
    977 	if (sc == NULL)
    978 		return;
    979 
    980 	if (sc->axe_dying)
    981 		return;
    982 
    983 	ifp = GET_IFP(sc);
    984 	mii = GET_MII(sc);
    985 	if (mii == NULL)
    986 		return;
    987 
    988 	s = splnet();
    989 
    990 	mii_tick(mii);
    991 	if (!sc->axe_link) {
    992 		mii_pollstat(mii);
    993 		if (mii->mii_media_status & IFM_ACTIVE &&
    994 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
    995 			DPRINTF(("%s: %s: got link\n",
    996 				 USBDEVNAME(sc->axe_dev), __func__));
    997 			sc->axe_link++;
    998 			if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    999 				   axe_start(ifp);
   1000 		}
   1001 	}
   1002 
   1003 	usb_callout(sc->axe_stat_ch, hz, axe_tick, sc);
   1004 
   1005 	splx(s);
   1006 }
   1007 
   1008 Static int
   1009 axe_encap(struct axe_softc *sc, struct mbuf *m, int idx)
   1010 {
   1011 	struct axe_chain	*c;
   1012 	usbd_status		err;
   1013 
   1014 	c = &sc->axe_cdata.axe_tx_chain[idx];
   1015 
   1016 	/*
   1017 	 * Copy the mbuf data into a contiguous buffer, leaving two
   1018 	 * bytes at the beginning to hold the frame length.
   1019 	 */
   1020 	m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf);
   1021 	c->axe_mbuf = m;
   1022 
   1023 	usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_TX],
   1024 	    c, c->axe_buf, m->m_pkthdr.len, USBD_FORCE_SHORT_XFER, 10000,
   1025 	    axe_txeof);
   1026 
   1027 	/* Transmit */
   1028 	err = usbd_transfer(c->axe_xfer);
   1029 	if (err != USBD_IN_PROGRESS) {
   1030 		axe_stop(sc);
   1031 		return(EIO);
   1032 	}
   1033 
   1034 	sc->axe_cdata.axe_tx_cnt++;
   1035 
   1036 	return(0);
   1037 }
   1038 
   1039 Static void
   1040 axe_start(struct ifnet *ifp)
   1041 {
   1042 	struct axe_softc	*sc;
   1043 	struct mbuf		*m_head = NULL;
   1044 
   1045 	sc = ifp->if_softc;
   1046 
   1047 	if (!sc->axe_link) {
   1048 		return;
   1049 	}
   1050 
   1051 	if (ifp->if_flags & IFF_OACTIVE) {
   1052 		return;
   1053 	}
   1054 
   1055 	IF_DEQUEUE(&ifp->if_snd, m_head);
   1056 	if (m_head == NULL) {
   1057 		return;
   1058 	}
   1059 
   1060 	if (axe_encap(sc, m_head, 0)) {
   1061 		IF_PREPEND(&ifp->if_snd, m_head);
   1062 		ifp->if_flags |= IFF_OACTIVE;
   1063 		return;
   1064 	}
   1065 
   1066 	/*
   1067 	 * If there's a BPF listener, bounce a copy of this frame
   1068 	 * to him.
   1069 	 */
   1070 #if NBPFILTER > 0
   1071 	 if (ifp->if_bpf)
   1072 	 	BPF_MTAP(ifp, m_head);
   1073 #endif
   1074 
   1075 	ifp->if_flags |= IFF_OACTIVE;
   1076 
   1077 	/*
   1078 	 * Set a timeout in case the chip goes out to lunch.
   1079 	 */
   1080 	ifp->if_timer = 5;
   1081 
   1082 	return;
   1083 }
   1084 
   1085 Static void
   1086 axe_init(void *xsc)
   1087 {
   1088 	struct axe_softc	*sc = xsc;
   1089 	struct ifnet		*ifp = GET_IFP(sc);
   1090 	struct axe_chain	*c;
   1091 	usbd_status		err;
   1092 	int			rxmode;
   1093 	int			i, s;
   1094 
   1095 	if (ifp->if_flags & IFF_RUNNING)
   1096 		return;
   1097 
   1098 	s = splnet();
   1099 
   1100 	/*
   1101 	 * Cancel pending I/O and free all RX/TX buffers.
   1102 	 */
   1103 	axe_reset(sc);
   1104 
   1105 	/* Enable RX logic. */
   1106 
   1107 	/* Init RX ring. */
   1108 	if (axe_rx_list_init(sc) == ENOBUFS) {
   1109 		printf("%s: rx list init failed\n", USBDEVNAME(sc->axe_dev));
   1110 		splx(s);
   1111 		return;
   1112 	}
   1113 
   1114 	/* Init TX ring. */
   1115 	if (axe_tx_list_init(sc) == ENOBUFS) {
   1116 		printf("%s: tx list init failed\n", USBDEVNAME(sc->axe_dev));
   1117 		splx(s);
   1118 		return;
   1119 	}
   1120 
   1121 	/* Set transmitter IPG values */
   1122 	axe_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 		}
   1205 		break;
   1206 
   1207 	case SIOCSIFMTU:
   1208 		if (ifr->ifr_mtu > ETHERMTU)
   1209 			error = EINVAL;
   1210 		else
   1211 			ifp->if_mtu = ifr->ifr_mtu;
   1212 		break;
   1213 
   1214 	case SIOCSIFFLAGS:
   1215 		if (ifp->if_flags & IFF_UP) {
   1216 			if (ifp->if_flags & IFF_RUNNING &&
   1217 			    ifp->if_flags & IFF_PROMISC &&
   1218 			    !(sc->axe_if_flags & IFF_PROMISC)) {
   1219 
   1220 				axe_cmd(sc, AXE_CMD_RXCTL_READ,
   1221 					0, 0, (void *)&rxmode);
   1222 				rxmode = le16toh(rxmode) | AXE_RXCMD_PROMISC;
   1223 				axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
   1224 					0, rxmode, NULL);
   1225 
   1226 				axe_setmulti(sc);
   1227 			} else if (ifp->if_flags & IFF_RUNNING &&
   1228 			    !(ifp->if_flags & IFF_PROMISC) &&
   1229 			    sc->axe_if_flags & IFF_PROMISC) {
   1230 				axe_cmd(sc, AXE_CMD_RXCTL_READ,
   1231 					0, 0, (void *)&rxmode);
   1232 				rxmode = le16toh(rxmode) & ~AXE_RXCMD_PROMISC;
   1233 				axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
   1234 					0, rxmode, NULL);
   1235 				axe_setmulti(sc);
   1236 			} else if (!(ifp->if_flags & IFF_RUNNING))
   1237 				axe_init(sc);
   1238 		} else {
   1239 			if (ifp->if_flags & IFF_RUNNING)
   1240 				axe_stop(sc);
   1241 		}
   1242 		sc->axe_if_flags = ifp->if_flags;
   1243 		error = 0;
   1244 		break;
   1245 	case SIOCADDMULTI:
   1246 	case SIOCDELMULTI:
   1247 #ifdef __NetBSD__
   1248 		error = (cmd == SIOCADDMULTI) ?
   1249 			ether_addmulti(ifr, &sc->axe_ec) :
   1250 			ether_delmulti(ifr, &sc->axe_ec);
   1251 #else
   1252 		error = (cmd == SIOCADDMULTI) ?
   1253 		    ether_addmulti(ifr, &sc->arpcom) :
   1254 		    ether_delmulti(ifr, &sc->arpcom);
   1255 #endif /* __NetBSD__ */
   1256 		if (error == ENETRESET) {
   1257 			/*
   1258 			 * Multicast list has changed; set the hardware
   1259 			 * filter accordingly.
   1260 			 */
   1261 			if (ifp->if_flags & IFF_RUNNING)
   1262 				axe_setmulti(sc);
   1263 			error = 0;
   1264 		}
   1265 		break;
   1266 	case SIOCGIFMEDIA:
   1267 	case SIOCSIFMEDIA:
   1268 		mii = GET_MII(sc);
   1269 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
   1270 		break;
   1271 
   1272 	default:
   1273 		error = EINVAL;
   1274 		break;
   1275 	}
   1276 
   1277 	return(error);
   1278 }
   1279 
   1280 /*
   1281  * XXX
   1282  * You can't call axe_txeof since the USB transfer has not
   1283  * completed yet.
   1284  */
   1285 Static void
   1286 axe_watchdog(struct ifnet *ifp)
   1287 {
   1288 	struct axe_softc	*sc;
   1289 	struct axe_chain	*c;
   1290 	usbd_status		stat;
   1291 	int			s;
   1292 
   1293 	sc = ifp->if_softc;
   1294 
   1295 	ifp->if_oerrors++;
   1296 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->axe_dev));
   1297 
   1298 	s = splusb();
   1299 	c = &sc->axe_cdata.axe_tx_chain[0];
   1300 	usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &stat);
   1301 	axe_txeof(c->axe_xfer, c, stat);
   1302 
   1303 	if (ifp->if_snd.ifq_head != NULL)
   1304 		axe_start(ifp);
   1305 	splx(s);
   1306 }
   1307 
   1308 /*
   1309  * Stop the adapter and free any mbufs allocated to the
   1310  * RX and TX lists.
   1311  */
   1312 Static void
   1313 axe_stop(struct axe_softc *sc)
   1314 {
   1315 	usbd_status		err;
   1316 	struct ifnet		*ifp;
   1317 	int			i;
   1318 
   1319 	axe_reset(sc);
   1320 
   1321 	ifp = GET_IFP(sc);
   1322 	ifp->if_timer = 0;
   1323 
   1324 	usb_uncallout(sc->axe_stat_ch, axe_tick, sc);
   1325 
   1326 	/* Stop transfers. */
   1327 	if (sc->axe_ep[AXE_ENDPT_RX] != NULL) {
   1328 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
   1329 		if (err) {
   1330 			printf("%s: abort rx pipe failed: %s\n",
   1331 		    	    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1332 		}
   1333 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_RX]);
   1334 		if (err) {
   1335 			printf("%s: close rx pipe failed: %s\n",
   1336 		    	    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1337 		}
   1338 		sc->axe_ep[AXE_ENDPT_RX] = NULL;
   1339 	}
   1340 
   1341 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL) {
   1342 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
   1343 		if (err) {
   1344 			printf("%s: abort tx pipe failed: %s\n",
   1345 		    	    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1346 		}
   1347 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_TX]);
   1348 		if (err) {
   1349 			printf("%s: close tx pipe failed: %s\n",
   1350 			    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1351 		}
   1352 		sc->axe_ep[AXE_ENDPT_TX] = NULL;
   1353 	}
   1354 
   1355 	if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) {
   1356 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
   1357 		if (err) {
   1358 			printf("%s: abort intr pipe failed: %s\n",
   1359 		    	    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1360 		}
   1361 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
   1362 		if (err) {
   1363 			printf("%s: close intr pipe failed: %s\n",
   1364 			    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
   1365 		}
   1366 		sc->axe_ep[AXE_ENDPT_INTR] = NULL;
   1367 	}
   1368 
   1369 	/* Free RX resources. */
   1370 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
   1371 		if (sc->axe_cdata.axe_rx_chain[i].axe_mbuf != NULL) {
   1372 			m_freem(sc->axe_cdata.axe_rx_chain[i].axe_mbuf);
   1373 			sc->axe_cdata.axe_rx_chain[i].axe_mbuf = NULL;
   1374 		}
   1375 		if (sc->axe_cdata.axe_rx_chain[i].axe_xfer != NULL) {
   1376 			usbd_free_xfer(sc->axe_cdata.axe_rx_chain[i].axe_xfer);
   1377 			sc->axe_cdata.axe_rx_chain[i].axe_xfer = NULL;
   1378 		}
   1379 	}
   1380 
   1381 	/* Free TX resources. */
   1382 	for (i = 0; i < AXE_TX_LIST_CNT; i++) {
   1383 		if (sc->axe_cdata.axe_tx_chain[i].axe_mbuf != NULL) {
   1384 			m_freem(sc->axe_cdata.axe_tx_chain[i].axe_mbuf);
   1385 			sc->axe_cdata.axe_tx_chain[i].axe_mbuf = NULL;
   1386 		}
   1387 		if (sc->axe_cdata.axe_tx_chain[i].axe_xfer != NULL) {
   1388 			usbd_free_xfer(sc->axe_cdata.axe_tx_chain[i].axe_xfer);
   1389 			sc->axe_cdata.axe_tx_chain[i].axe_xfer = NULL;
   1390 		}
   1391 	}
   1392 
   1393 	sc->axe_link = 0;
   1394 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1395 }
   1396 
   1397