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