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