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