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