Home | History | Annotate | Line # | Download | only in usb
if_aue.c revision 1.10
      1 /*	$NetBSD: if_aue.c,v 1.10 2000/01/16 23:18:56 augustss Exp $	*/
      2 /*
      3  * Copyright (c) 1997, 1998, 1999, 2000
      4  *	Bill Paul <wpaul (at) ee.columbia.edu>.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Bill Paul.
     17  * 4. Neither the name of the author nor the names of any co-contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     31  * THE POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  * $FreeBSD: src/sys/dev/usb/if_aue.c,v 1.11 2000/01/14 01:36:14 wpaul Exp $
     34  */
     35 
     36 /*
     37  * ADMtek AN986 Pegasus USB to ethernet driver. Datasheet is available
     38  * from http://www.admtek.com.tw.
     39  *
     40  * Written by Bill Paul <wpaul (at) ee.columbia.edu>
     41  * Electrical Engineering Department
     42  * Columbia University, New York City
     43  */
     44 
     45 /*
     46  * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet
     47  * support: the control endpoint for reading/writing registers, burst
     48  * read endpoint for packet reception, burst write for packet transmission
     49  * and one for "interrupts." The chip uses the same RX filter scheme
     50  * as the other ADMtek ethernet parts: one perfect filter entry for the
     51  * the station address and a 64-bit multicast hash table. The chip supports
     52  * both MII and HomePNA attachments.
     53  *
     54  * Since the maximum data transfer speed of USB is supposed to be 12Mbps,
     55  * you're never really going to get 100Mbps speeds from this device. I
     56  * think the idea is to allow the device to connect to 10 or 100Mbps
     57  * networks, not necessarily to provide 100Mbps performance. Also, since
     58  * the controller uses an external PHY chip, it's possible that board
     59  * designers might simply choose a 10Mbps PHY.
     60  *
     61  * Registers are accessed using usbd_do_request(). Packet transfers are
     62  * done using usbd_transfer() and friends.
     63  */
     64 
     65 /*
     66  * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
     67  */
     68 
     69 /*
     70  * TODO:
     71  * better error messages from rxstat
     72  * split out if_auevar.h
     73  * add thread to avoid register reads from interrupt context
     74  * more error checks
     75  * investigate short rx problem
     76  * proper cleanup on errors
     77  */
     78 
     79 #if defined(__NetBSD__) || defined(__OpenBSD__)
     80 #include "opt_inet.h"
     81 #include "opt_ns.h"
     82 #include "bpfilter.h"
     83 #include "rnd.h"
     84 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
     85 
     86 #include <sys/param.h>
     87 #include <sys/systm.h>
     88 #include <sys/sockio.h>
     89 #include <sys/mbuf.h>
     90 #include <sys/malloc.h>
     91 #include <sys/kernel.h>
     92 #include <sys/socket.h>
     93 
     94 #if defined(__FreeBSD__)
     95 
     96 #include <net/ethernet.h>
     97 #include <machine/clock.h>	/* for DELAY */
     98 #include <sys/bus.h>
     99 /* "controller miibus0" required.  See GENERIC if you get errors here. */
    100 #include "miibus_if.h"
    101 
    102 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    103 
    104 #include <sys/device.h>
    105 
    106 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    107 
    108 #include <net/if.h>
    109 #include <net/if_arp.h>
    110 #include <net/if_dl.h>
    111 #include <net/if_media.h>
    112 
    113 #if defined(__NetBSD__) || defined(__OpenBSD__)
    114 #include <net/if_ether.h>
    115 
    116 #define bpf_mtap(ifp, m) bpf_tap((ifp)->if_bpf, mtod((m), caddr_t), (m)->m_len)
    117 
    118 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    119 
    120 #if defined(__FreeBSD__) || NBPFILTER > 0
    121 #include <net/bpf.h>
    122 #endif
    123 
    124 #if defined(__NetBSD__) || defined(__OpenBSD__)
    125 #ifdef INET
    126 #include <netinet/in.h>
    127 #include <netinet/if_inarp.h>
    128 #endif
    129 
    130 #ifdef NS
    131 #include <netns/ns.h>
    132 #include <netns/ns_if.h>
    133 #endif
    134 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    135 
    136 #include <dev/mii/mii.h>
    137 #include <dev/mii/miivar.h>
    138 
    139 #include <dev/usb/usb.h>
    140 #include <dev/usb/usbdi.h>
    141 #include <dev/usb/usbdi_util.h>
    142 #include <dev/usb/usbdevs.h>
    143 
    144 #ifdef __FreeBSD__
    145 #include <dev/usb/usb_ethersubr.h>
    146 #endif
    147 
    148 #include <dev/usb/if_auereg.h>
    149 
    150 #ifdef AUE_DEBUG
    151 #define DPRINTF(x)	if (auedebug) logprintf x
    152 #define DPRINTFN(n,x)	if (auedebug >= (n)) logprintf x
    153 int	auedebug = 0;
    154 #else
    155 #define DPRINTF(x)
    156 #define DPRINTFN(n,x)
    157 #endif
    158 
    159 /*
    160  * Various supported device vendors/products.
    161  */
    162 static struct aue_type aue_devs[] = {
    163 	{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100 },
    164 	{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX },
    165 	{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX },
    166 	{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS },
    167 	{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX },
    168 	{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA },
    169 	{ USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB },
    170 	{ 0, 0 }
    171 };
    172 
    173 USB_DECLARE_DRIVER(aue);
    174 
    175 static int aue_tx_list_init	__P((struct aue_softc *));
    176 static int aue_rx_list_init	__P((struct aue_softc *));
    177 static int aue_newbuf		__P((struct aue_softc *, struct aue_chain *,
    178 				    struct mbuf *));
    179 static int aue_send		__P((struct aue_softc *, struct mbuf *, int));
    180 static void aue_intr		__P((usbd_xfer_handle,
    181 				    usbd_private_handle, usbd_status));
    182 static void aue_rxeof		__P((usbd_xfer_handle,
    183 				    usbd_private_handle, usbd_status));
    184 static void aue_txeof		__P((usbd_xfer_handle,
    185 				    usbd_private_handle, usbd_status));
    186 static void aue_tick		__P((void *));
    187 static void aue_start		__P((struct ifnet *));
    188 static int aue_ioctl		__P((struct ifnet *, u_long, caddr_t));
    189 static void aue_init		__P((void *));
    190 static void aue_stop		__P((struct aue_softc *));
    191 static void aue_watchdog	__P((struct ifnet *));
    192 #ifdef __FreeBSD__
    193 static void aue_shutdown	__P((device_ptr_t));
    194 #endif
    195 static int aue_ifmedia_upd	__P((struct ifnet *));
    196 static void aue_ifmedia_sts	__P((struct ifnet *, struct ifmediareq *));
    197 
    198 static int aue_eeprom_getword	__P((struct aue_softc *, int));
    199 static void aue_read_mac	__P((struct aue_softc *, u_char *));
    200 static int aue_miibus_readreg	__P((device_ptr_t, int, int));
    201 #if defined(__FreeBSD__)
    202 static int aue_miibus_writereg	__P((device_ptr_t, int, int, int));
    203 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    204 static void aue_miibus_writereg	__P((device_ptr_t, int, int, int));
    205 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    206 static void aue_miibus_statchg	__P((device_ptr_t));
    207 
    208 static void aue_setmulti	__P((struct aue_softc *));
    209 static u_int32_t aue_crc	__P((caddr_t));
    210 static void aue_reset		__P((struct aue_softc *));
    211 
    212 static int csr_read_1		__P((struct aue_softc *, int));
    213 static int csr_write_1		__P((struct aue_softc *, int, int));
    214 static int csr_read_2		__P((struct aue_softc *, int));
    215 static int csr_write_2		__P((struct aue_softc *, int, int));
    216 
    217 #if defined(__FreeBSD__)
    218 #if !defined(lint)
    219 static const char rcsid[] =
    220   "$FreeBSD: src/sys/dev/usb/if_aue.c,v 1.11 2000/01/14 01:36:14 wpaul Exp $";
    221 #endif
    222 
    223 static void aue_rxstart		__P((struct ifnet *));
    224 
    225 static struct usb_qdat aue_qdat;
    226 
    227 static device_method_t aue_methods[] = {
    228 	/* Device interface */
    229 	DEVMETHOD(device_probe,		aue_match),
    230 	DEVMETHOD(device_attach,	aue_attach),
    231 	DEVMETHOD(device_detach,	aue_detach),
    232 	DEVMETHOD(device_shutdown,	aue_shutdown),
    233 
    234 	/* bus interface */
    235 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
    236 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
    237 
    238 	/* MII interface */
    239 	DEVMETHOD(miibus_readreg,	aue_miibus_readreg),
    240 	DEVMETHOD(miibus_writereg,	aue_miibus_writereg),
    241 	DEVMETHOD(miibus_statchg,	aue_miibus_statchg),
    242 
    243 	{ 0, 0 }
    244 };
    245 
    246 static driver_t aue_driver = {
    247 	"aue",
    248 	aue_methods,
    249 	sizeof(struct aue_softc)
    250 };
    251 
    252 static devclass_t aue_devclass;
    253 
    254 DRIVER_MODULE(if_aue, uhub, aue_driver, aue_devclass, usbd_driver_load, 0);
    255 DRIVER_MODULE(miibus, aue, miibus_driver, miibus_devclass, 0, 0);
    256 
    257 #endif /* __FreeBSD__ */
    258 
    259 #define AUE_DO_REQUEST(dev, req, data) usbd_do_request_flags(dev, req, data, USBD_NO_TSLEEP, NULL)
    260 
    261 #define AUE_SETBIT(sc, reg, x)				\
    262 	csr_write_1(sc, reg, csr_read_1(sc, reg) | (x))
    263 
    264 #define AUE_CLRBIT(sc, reg, x)				\
    265 	csr_write_1(sc, reg, csr_read_1(sc, reg) & ~(x))
    266 
    267 static int
    268 csr_read_1(sc, reg)
    269 	struct aue_softc	*sc;
    270 	int			reg;
    271 {
    272 	usb_device_request_t	req;
    273 	usbd_status		err;
    274 	uByte			val = 0;
    275 	int			s;
    276 
    277 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    278 	req.bRequest = AUE_UR_READREG;
    279 	USETW(req.wValue, 0);
    280 	USETW(req.wIndex, reg);
    281 	USETW(req.wLength, 1);
    282 
    283 	s = splusb();
    284 	err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
    285 	splx(s);
    286 
    287 	if (err)
    288 		return (0);
    289 
    290 	return (val);
    291 }
    292 
    293 static int
    294 csr_read_2(sc, reg)
    295 	struct aue_softc	*sc;
    296 	int			reg;
    297 {
    298 	usb_device_request_t	req;
    299 	usbd_status		err;
    300 	uWord			val;
    301 	int			s;
    302 
    303 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    304 	req.bRequest = AUE_UR_READREG;
    305 	USETW(req.wValue, 0);
    306 	USETW(req.wIndex, reg);
    307 	USETW(req.wLength, 2);
    308 
    309 	s = splusb();
    310 	err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
    311 	splx(s);
    312 
    313 	if (err)
    314 		return (0);
    315 
    316 	return (UGETW(val));
    317 }
    318 
    319 static int
    320 csr_write_1(sc, reg, aval)
    321 	struct aue_softc	*sc;
    322 	int			reg, aval;
    323 {
    324 	usb_device_request_t	req;
    325 	usbd_status		err;
    326 	int			s;
    327 	uByte			val;
    328 
    329 	val = aval;
    330 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    331 	req.bRequest = AUE_UR_WRITEREG;
    332 	USETW(req.wValue, val);
    333 	USETW(req.wIndex, reg);
    334 	USETW(req.wLength, 1);
    335 
    336 	s = splusb();
    337 	err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
    338 	splx(s);
    339 
    340 	if (err)
    341 		return (-1);
    342 
    343 	return (0);
    344 }
    345 
    346 static int
    347 csr_write_2(sc, reg, aval)
    348 	struct aue_softc	*sc;
    349 	int			reg, aval;
    350 {
    351 	usb_device_request_t	req;
    352 	usbd_status		err;
    353 	int			s;
    354 	uWord			val;
    355 
    356 	USETW(val, aval);
    357 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    358 	req.bRequest = AUE_UR_WRITEREG;
    359 	USETW(req.wValue, aval);
    360 	USETW(req.wIndex, reg);
    361 	USETW(req.wLength, 2);
    362 
    363 	s = splusb();
    364 	err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
    365 	splx(s);
    366 
    367 	if (err)
    368 		return (-1);
    369 
    370 	return (0);
    371 }
    372 
    373 /*
    374  * Read a word of data stored in the EEPROM at address 'addr.'
    375  */
    376 static int
    377 aue_eeprom_getword(sc, addr)
    378 	struct aue_softc	*sc;
    379 	int			addr;
    380 {
    381 	int		i;
    382 
    383 	csr_write_1(sc, AUE_EE_REG, addr);
    384 	csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
    385 
    386 	for (i = 0; i < AUE_TIMEOUT; i++) {
    387 		if (csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
    388 			break;
    389 	}
    390 
    391 	if (i == AUE_TIMEOUT) {
    392 		printf("%s: EEPROM read timed out\n",
    393 		    USBDEVNAME(sc->aue_dev));
    394 	}
    395 
    396 	return (csr_read_2(sc, AUE_EE_DATA));
    397 }
    398 
    399 /*
    400  * Read the MAC from the EEPROM.  It's at offset 0.
    401  */
    402 static void
    403 aue_read_mac(sc, dest)
    404 	struct aue_softc	*sc;
    405 	u_char			*dest;
    406 {
    407 	int			i;
    408 	int			off = 0;
    409 	int			word;
    410 
    411 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
    412 
    413 	for (i = 0; i < 3; i++) {
    414 		word = aue_eeprom_getword(sc, off + i);
    415 		dest[2 * i] = (u_char)word;
    416 		dest[2 * i + 1] = (u_char)(word >> 8);
    417 	}
    418 }
    419 
    420 static int
    421 aue_miibus_readreg(dev, phy, reg)
    422 	device_ptr_t		dev;
    423 	int			phy, reg;
    424 {
    425 	struct aue_softc	*sc = USBGETSOFTC(dev);
    426 	int			i;
    427 	u_int16_t		val;
    428 
    429 	/*
    430 	 * The Am79C901 HomePNA PHY actually contains
    431 	 * two transceivers: a 1Mbps HomePNA PHY and a
    432 	 * 10Mbps full/half duplex ethernet PHY with
    433 	 * NWAY autoneg. However in the ADMtek adapter,
    434 	 * only the 1Mbps PHY is actually connected to
    435 	 * anything, so we ignore the 10Mbps one. It
    436 	 * happens to be configured for MII address 3,
    437 	 * so we filter that out.
    438 	 */
    439 	if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
    440 	    sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
    441 		if (phy != 1)
    442 			return (0);
    443 	}
    444 
    445 	csr_write_1(sc, AUE_PHY_ADDR, phy);
    446 	csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
    447 
    448 	for (i = 0; i < AUE_TIMEOUT; i++) {
    449 		if (csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
    450 			break;
    451 	}
    452 
    453 	if (i == AUE_TIMEOUT) {
    454 		printf("%s: MII read timed out\n",
    455 		    USBDEVNAME(sc->aue_dev));
    456 	}
    457 
    458 	val = csr_read_2(sc, AUE_PHY_DATA);
    459 
    460 	DPRINTFN(11,("%s: %s: phy=%d reg=%d => 0x%04x\n",
    461 		     USBDEVNAME(sc->aue_dev), __FUNCTION__, phy, reg, val));
    462 
    463 	return (val);
    464 }
    465 
    466 #if defined(__FreeBSD__)
    467 static int
    468 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    469 static void
    470 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    471 aue_miibus_writereg(dev, phy, reg, data)
    472 	device_ptr_t		dev;
    473 	int			phy, reg, data;
    474 {
    475 	struct aue_softc	*sc = USBGETSOFTC(dev);
    476 	int			i;
    477 
    478 	if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
    479 	    sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
    480 		if (phy == 3)
    481 #if defined(__FreeBSD__)
    482 			return (0);
    483 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    484 			return;
    485 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    486 	}
    487 
    488 	DPRINTFN(11,("%s: %s: phy=%d reg=%d data=0x%04x\n",
    489 		     USBDEVNAME(sc->aue_dev), __FUNCTION__, phy, reg, data));
    490 
    491 	csr_write_2(sc, AUE_PHY_DATA, data);
    492 	csr_write_1(sc, AUE_PHY_ADDR, phy);
    493 	csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
    494 
    495 	for (i = 0; i < AUE_TIMEOUT; i++) {
    496 		if (csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
    497 			break;
    498 	}
    499 
    500 	if (i == AUE_TIMEOUT) {
    501 		printf("%s: MII read timed out\n",
    502 		    USBDEVNAME(sc->aue_dev));
    503 	}
    504 
    505 #if defined(__FreeBSD__)
    506 	return (0);
    507 #endif
    508 }
    509 
    510 static void
    511 aue_miibus_statchg(dev)
    512 	device_ptr_t		dev;
    513 {
    514 	struct aue_softc	*sc = USBGETSOFTC(dev);
    515 	struct mii_data		*mii = GET_MII(sc);
    516 	struct ifnet		*ifp = GET_IFP(sc);
    517 
    518 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
    519 
    520 	AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
    521 
    522 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
    523 		AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
    524 		ifp->if_baudrate = 100000000;
    525 	} else {
    526 		AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
    527 		ifp->if_baudrate = 10000000;
    528 	}
    529 
    530 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
    531 		AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
    532 	else
    533 		AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
    534 
    535 	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
    536 
    537 	/*
    538 	 * Set the LED modes on the LinkSys adapter.
    539 	 * This turns on the 'dual link LED' bin in the auxmode
    540 	 * register of the Broadcom PHY.
    541 	 */
    542 	if (sc->aue_vendor == USB_VENDOR_LINKSYS &&
    543 	    sc->aue_product == USB_PRODUCT_LINKSYS_USB100TX) {
    544 		u_int16_t               auxmode;
    545 		auxmode = aue_miibus_readreg(dev, 0, 0x1b);
    546 		aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
    547 	}
    548 }
    549 
    550 #define AUE_POLY	0xEDB88320
    551 #define AUE_BITS	6
    552 
    553 static u_int32_t
    554 aue_crc(addr)
    555 	caddr_t			addr;
    556 {
    557 	u_int32_t		idx, bit, data, crc;
    558 
    559 	/* Compute CRC for the address value. */
    560 	crc = 0xFFFFFFFF; /* initial value */
    561 
    562 	for (idx = 0; idx < 6; idx++) {
    563 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
    564 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0);
    565 	}
    566 
    567 	return (crc & ((1 << AUE_BITS) - 1));
    568 }
    569 
    570 static void
    571 aue_setmulti(sc)
    572 	struct aue_softc	*sc;
    573 {
    574 	struct ifnet		*ifp;
    575 #if defined(__FreeBSD__)
    576 	struct ifmultiaddr	*ifma;
    577 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    578 	struct ether_multi	*enm;
    579 	struct ether_multistep	step;
    580 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    581 	u_int32_t		h = 0, i;
    582 
    583 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
    584 
    585 	ifp = GET_IFP(sc);
    586 
    587 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
    588 		AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
    589 		return;
    590 	}
    591 
    592 	AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
    593 
    594 	/* first, zot all the existing hash bits */
    595 	for (i = 0; i < 8; i++)
    596 		csr_write_1(sc, AUE_MAR0 + i, 0);
    597 
    598 	/* now program new ones */
    599 #if defined(__FreeBSD__)
    600 	for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
    601 	    ifma = ifma->ifma_link.le_next) {
    602 		if (ifma->ifma_addr->sa_family != AF_LINK)
    603 			continue;
    604 		h = aue_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
    605 		AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0xF));
    606 	}
    607 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    608 	ETHER_FIRST_MULTI(step, &sc->aue_ec, enm);
    609 	while (enm != NULL) {
    610 #if 1
    611 		if (memcmp(enm->enm_addrlo,
    612 			   enm->enm_addrhi, ETHER_ADDR_LEN) != 0) {
    613 			ifp->if_flags |= IFF_ALLMULTI;
    614 			AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
    615 			return;
    616 		}
    617 #endif
    618 		h = aue_crc(enm->enm_addrlo);
    619 		AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0xF));
    620 		ETHER_NEXT_MULTI(step, enm);
    621 	}
    622 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    623 }
    624 
    625 static void
    626 aue_reset(sc)
    627 	struct aue_softc	*sc;
    628 {
    629 	int		i;
    630 
    631 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
    632 
    633 	AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
    634 
    635 	for (i = 0; i < AUE_TIMEOUT; i++) {
    636 		if (!(csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
    637 			break;
    638 	}
    639 
    640 	if (i == AUE_TIMEOUT)
    641 		printf("%s: reset failed\n", USBDEVNAME(sc->aue_dev));
    642 
    643 	/*
    644 	 * The PHY(s) attached to the Pegasus chip may be held
    645 	 * in reset until we flip on the GPIO outputs. Make sure
    646 	 * to set the GPIO pins high so that the PHY(s) will
    647 	 * be enabled.
    648 	 *
    649 	 * Note: We force all of the GPIO pins low first, *then*
    650 	 * enable the ones we want.
    651   	 */
    652 	csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0);
    653   	csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0|AUE_GPIO_SEL1);
    654 
    655 	/* Grrr. LinkSys has to be different from everyone else. */
    656 	if (sc->aue_vendor == USB_VENDOR_LINKSYS &&
    657 	    sc->aue_product == USB_PRODUCT_LINKSYS_USB100TX) {
    658 		csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1);
    659 		csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1|
    660 			AUE_GPIO_OUT0);
    661 	}
    662 
    663 	/* Wait a little while for the chip to get its brains in order. */
    664 	DELAY(10000);		/* XXX */
    665 }
    666 
    667 /*
    668  * Probe for a Pegasus chip.
    669  */
    670 USB_MATCH(aue)
    671 {
    672 	USB_MATCH_START(aue, uaa);
    673 	struct aue_type			*t;
    674 
    675 	if (uaa->iface != NULL)
    676 		return (UMATCH_NONE);
    677 
    678 	for (t = aue_devs; t->aue_vid != 0; t++)
    679 		if (uaa->vendor == t->aue_vid && uaa->product == t->aue_did)
    680 			return (UMATCH_VENDOR_PRODUCT);
    681 
    682 	return (UMATCH_NONE);
    683 }
    684 
    685 /*
    686  * Attach the interface. Allocate softc structures, do ifmedia
    687  * setup and ethernet/BPF attach.
    688  */
    689 USB_ATTACH(aue)
    690 {
    691 	USB_ATTACH_START(aue, sc, uaa);
    692 	char			devinfo[1024];
    693 	int			s;
    694 	u_char			eaddr[ETHER_ADDR_LEN];
    695 	struct ifnet		*ifp;
    696 	struct mii_data		*mii;
    697 	usbd_device_handle	dev = uaa->device;
    698 	usbd_interface_handle	iface;
    699 	usbd_status		err;
    700 	usb_interface_descriptor_t	*id;
    701 	usb_endpoint_descriptor_t	*ed;
    702 	int			i;
    703 
    704 #ifdef __FreeBSD__
    705 	bzero(sc, sizeof(struct aue_softc));
    706 #endif
    707 
    708 	DPRINTFN(5,(" : aue_attach: sc=%p", sc));
    709 
    710 	usbd_devinfo(dev, 0, devinfo);
    711 	USB_ATTACH_SETUP;
    712 	printf("%s: %s\n", USBDEVNAME(sc->aue_dev), devinfo);
    713 
    714 	err = usbd_set_config_no(dev, AUE_CONFIG_NO, 0);
    715 	if (err) {
    716 		printf("%s: setting config no failed\n",
    717 		    USBDEVNAME(sc->aue_dev));
    718 		USB_ATTACH_ERROR_RETURN;
    719 	}
    720 
    721 	err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &iface);
    722 	if (err) {
    723 		printf("%s: getting interface handle failed\n",
    724 		    USBDEVNAME(sc->aue_dev));
    725 		USB_ATTACH_ERROR_RETURN;
    726 	}
    727 
    728 	sc->aue_udev = dev;
    729 	sc->aue_iface = iface;
    730 	sc->aue_product = uaa->product;
    731 	sc->aue_vendor = uaa->vendor;
    732 
    733 	id = usbd_get_interface_descriptor(iface);
    734 
    735 	/* Find endpoints. */
    736 	for (i = 0; i < id->bNumEndpoints; i++) {
    737 		ed = usbd_interface2endpoint_descriptor(iface, i);
    738 		if (!ed) {
    739 			printf("%s: couldn't get endpoint descriptor %d\n",
    740 			    USBDEVNAME(sc->aue_dev), i);
    741 			USB_ATTACH_ERROR_RETURN;
    742 		}
    743 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    744 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    745 			sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
    746 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    747 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    748 			sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
    749 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    750 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
    751 			sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
    752 		}
    753 	}
    754 
    755 	if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 ||
    756 	    sc->aue_ed[AUE_ENDPT_INTR] == 0) {
    757 		printf("%s: missing endpoint\n", USBDEVNAME(sc->aue_dev));
    758 		USB_ATTACH_ERROR_RETURN;
    759 	}
    760 
    761 
    762 	s = splimp();
    763 
    764 	/* Reset the adapter. */
    765 	aue_reset(sc);
    766 
    767 	/*
    768 	 * Get station address from the EEPROM.
    769 	 */
    770 	aue_read_mac(sc, eaddr);
    771 
    772 	/*
    773 	 * A Pegasus chip was detected. Inform the world.
    774 	 */
    775 #if defined(__FreeBSD__)
    776 	printf("%s: Ethernet address: %6D\n", USBDEVNAME(sc->aue_dev),
    777 	    eaddr, ":");
    778 
    779 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
    780 
    781 	ifp = &sc->arpcom.ac_if;
    782 	ifp->if_softc = sc;
    783 	ifp->if_unit = sc->aue_unit;
    784 	ifp->if_name = "aue";
    785 	ifp->if_mtu = ETHERMTU;
    786 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    787 	ifp->if_ioctl = aue_ioctl;
    788 	ifp->if_output = ether_output;
    789 	ifp->if_start = aue_start;
    790 	ifp->if_watchdog = aue_watchdog;
    791 	ifp->if_init = aue_init;
    792 	ifp->if_baudrate = 10000000;
    793 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
    794 
    795 	/*
    796 	 * Do MII setup.
    797 	 * NOTE: Doing this causes child devices to be attached to us,
    798 	 * which we would normally disconnect at in the detach routine
    799 	 * using device_delete_child(). However the USB code is set up
    800 	 * such that when this driver is removed, all childred devices
    801 	 * are removed as well. In effect, the USB code ends up detaching
    802 	 * all of our children for us, so we don't have to do is ourselves
    803 	 * in aue_detach(). It's important to point this out since if
    804 	 * we *do* try to detach the child devices ourselves, we will
    805 	 * end up getting the children deleted twice, which will crash
    806 	 * the system.
    807 	 */
    808 	if (mii_phy_probe(self, &sc->aue_miibus,
    809 	    aue_ifmedia_upd, aue_ifmedia_sts)) {
    810 		printf("%s: MII without any PHY!\n", USBDEVNAME(sc->aue_dev));
    811 		splx(s);
    812 		USB_ATTACH_ERROR_RETURN;
    813 	}
    814 
    815 	aue_qdat.ifp = ifp;
    816 	aue_qdat.if_rxstart = aue_rxstart;
    817 
    818 	/*
    819 	 * Call MI attach routines.
    820 	 */
    821 	if_attach(ifp);
    822 	ether_ifattach(ifp);
    823 	callout_handle_init(&sc->aue_stat_ch);
    824 	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
    825 
    826 	usb_register_netisr();
    827 
    828 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    829 
    830 	printf("%s: Ethernet address %s\n", USBDEVNAME(sc->aue_dev),
    831 	    ether_sprintf(eaddr));
    832 
    833 	/* Initialize interface info.*/
    834 	ifp = &sc->aue_ec.ec_if;
    835 	ifp->if_softc = sc;
    836 	ifp->if_mtu = ETHERMTU;
    837 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    838 	ifp->if_ioctl = aue_ioctl;
    839 	ifp->if_start = aue_start;
    840 	ifp->if_watchdog = aue_watchdog;
    841 	ifp->if_baudrate = 10000000;
    842 	strncpy(ifp->if_xname, USBDEVNAME(sc->aue_dev), IFNAMSIZ);
    843 
    844 	/* Initialize MII/media info. */
    845 	mii = &sc->aue_mii;
    846 	mii->mii_ifp = ifp;
    847 	mii->mii_readreg = aue_miibus_readreg;
    848 	mii->mii_writereg = aue_miibus_writereg;
    849 	mii->mii_statchg = aue_miibus_statchg;
    850 	ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, aue_ifmedia_sts);
    851 	mii_phy_probe(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY);
    852 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
    853 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
    854 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
    855 	} else
    856 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    857 
    858 	/* Attach the interface. */
    859 	if_attach(ifp);
    860 	ether_ifattach(ifp, eaddr);
    861 
    862 #if NBPFILTER > 0
    863 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
    864 		  sizeof(struct ether_header));
    865 #endif
    866 #if RND > 0
    867 	rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->aue_dev),
    868 	    RND_TYPE_NET, 0);
    869 #endif
    870 
    871 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    872 
    873 	splx(s);
    874 
    875 	USB_ATTACH_SUCCESS_RETURN;
    876 }
    877 
    878 USB_DETACH(aue)
    879 {
    880 	USB_DETACH_START(aue, sc);
    881 #if defined(__FreeBSD__)
    882 	struct ifnet		*ifp;
    883 	int			s;
    884 
    885 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
    886 
    887 	s = splusb();
    888 
    889 	ifp = &sc->arpcom.ac_if;
    890 
    891 	usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
    892 	if_detach(ifp);
    893 
    894 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL)
    895 		usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
    896 	if (sc->aue_ep[AUE_ENDPT_RX] != NULL)
    897 		usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
    898 	if (sc->aue_ep[AUE_ENDPT_INTR] != NULL)
    899 		usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
    900 
    901 	splx(s);
    902 
    903 	return (0);
    904 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    905 	sc = sc;		/* XXX use sc */
    906 	/* XXX deallocate */
    907 
    908 #ifdef notyet
    909 	/*
    910 	 * Our softc is about to go away, so drop our refernce
    911 	 * to the ifnet.
    912 	 */
    913 	if_delref(sc->aue_ec.ec_if);
    914 	return (0);
    915 #else
    916 	return (EBUSY);
    917 #endif
    918 
    919 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    920 }
    921 
    922 #if defined(__NetBSD__) || defined(__OpenBSD__)
    923 int
    924 aue_activate(self, act)
    925 	device_ptr_t self;
    926 	enum devact act;
    927 {
    928 	struct aue_softc *sc = (struct aue_softc *)self;
    929 
    930 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
    931 
    932 	switch (act) {
    933 	case DVACT_ACTIVATE:
    934 		return (EOPNOTSUPP);
    935 		break;
    936 
    937 	case DVACT_DEACTIVATE:
    938 #ifdef notyet
    939 		/* First, kill off the interface. */
    940 		if_detach(sc->aue_ec.ec_if);
    941 #endif
    942 		sc->aue_dying = 1;
    943 		break;
    944 	}
    945 	return (0);
    946 }
    947 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    948 
    949 /*
    950  * Initialize an RX descriptor and attach an MBUF cluster.
    951  */
    952 static int
    953 aue_newbuf(sc, c, m)
    954 	struct aue_softc	*sc;
    955 	struct aue_chain	*c;
    956 	struct mbuf		*m;
    957 {
    958 	struct mbuf		*m_new = NULL;
    959 
    960 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
    961 
    962 	if (m == NULL) {
    963 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    964 		if (m_new == NULL) {
    965 			printf("%s: no memory for rx list "
    966 			    "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
    967 			return (ENOBUFS);
    968 		}
    969 
    970 		MCLGET(m_new, M_DONTWAIT);
    971 		if (!(m_new->m_flags & M_EXT)) {
    972 			printf("%s: no memory for rx list "
    973 			    "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
    974 			m_freem(m_new);
    975 			return (ENOBUFS);
    976 		}
    977 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    978 	} else {
    979 		m_new = m;
    980 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    981 		m_new->m_data = m_new->m_ext.ext_buf;
    982 	}
    983 
    984 	m_adj(m_new, ETHER_ALIGN);
    985 	c->aue_mbuf = m_new;
    986 
    987 	return (0);
    988 }
    989 
    990 static int
    991 aue_rx_list_init(sc)
    992 	struct aue_softc	*sc;
    993 {
    994 	struct aue_cdata	*cd;
    995 	struct aue_chain	*c;
    996 	int			i;
    997 
    998 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
    999 
   1000 	cd = &sc->aue_cdata;
   1001 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
   1002 		c = &cd->aue_rx_chain[i];
   1003 		c->aue_sc = sc;
   1004 		c->aue_idx = i;
   1005 		c->aue_accum = 0;
   1006 		if (aue_newbuf(sc, c, NULL) == ENOBUFS)
   1007 			return (ENOBUFS);
   1008 		if (c->aue_xfer == NULL) {
   1009 			c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
   1010 			if (c->aue_xfer == NULL)
   1011 				return (ENOBUFS);
   1012 			c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
   1013 			if (c->aue_buf == NULL)
   1014 				return (ENOBUFS); /* XXX free xfer */
   1015 		}
   1016 	}
   1017 
   1018 	return (0);
   1019 }
   1020 
   1021 static int
   1022 aue_tx_list_init(sc)
   1023 	struct aue_softc	*sc;
   1024 {
   1025 	struct aue_cdata	*cd;
   1026 	struct aue_chain	*c;
   1027 	int			i;
   1028 
   1029 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1030 
   1031 	cd = &sc->aue_cdata;
   1032 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
   1033 		c = &cd->aue_tx_chain[i];
   1034 		c->aue_sc = sc;
   1035 		c->aue_idx = i;
   1036 		c->aue_mbuf = NULL;
   1037 		if (c->aue_xfer == NULL) {
   1038 			c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
   1039 			if (c->aue_xfer == NULL)
   1040 				return (ENOBUFS);
   1041 			c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
   1042 			if (c->aue_buf == NULL)
   1043 				return (ENOBUFS);
   1044 		}
   1045 	}
   1046 
   1047 	return (0);
   1048 }
   1049 
   1050 static void
   1051 aue_intr(xfer, priv, status)
   1052 	usbd_xfer_handle	xfer;
   1053 	usbd_private_handle	priv;
   1054 	usbd_status		status;
   1055 {
   1056 	struct aue_softc	*sc = priv;
   1057 	struct ifnet		*ifp = GET_IFP(sc);
   1058 	struct aue_intrpkt	*p = &sc->aue_cdata.aue_ibuf;
   1059 
   1060 	DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
   1061 
   1062 	if (!(ifp->if_flags & IFF_RUNNING))
   1063 		return;
   1064 
   1065 	if (status != USBD_NORMAL_COMPLETION) {
   1066 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1067 			return;
   1068 		}
   1069 		printf("%s: usb error on intr: %s\n", USBDEVNAME(sc->aue_dev),
   1070 		    usbd_errstr(status));
   1071 		if (status == USBD_STALLED)
   1072 			usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
   1073 		return;
   1074 	}
   1075 
   1076 	if (p->aue_txstat0)
   1077 		ifp->if_oerrors++;
   1078 
   1079 	if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
   1080 		ifp->if_collisions++;
   1081 }
   1082 
   1083 #if defined(__FreeBSD__)
   1084 static void
   1085 aue_rxstart(ifp)
   1086 	struct ifnet		*ifp;
   1087 {
   1088 	struct aue_softc	*sc;
   1089 	struct aue_chain	*c;
   1090 
   1091 	sc = ifp->if_softc;
   1092 	c = &sc->aue_cdata.aue_rx_chain[sc->aue_cdata.aue_rx_prod];
   1093 
   1094 	if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
   1095 		ifp->if_ierrors++;
   1096 		return;
   1097 	}
   1098 
   1099 	/* Setup new transfer. */
   1100 	usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
   1101 	    c, mtod(c->aue_mbuf, char *), AUE_CUTOFF, USBD_SHORT_XFER_OK,
   1102 	    USBD_NO_TIMEOUT, aue_rxeof);
   1103 	usbd_transfer(c->aue_xfer);
   1104 }
   1105 #endif
   1106 
   1107 /*
   1108  * A frame has been uploaded: pass the resulting mbuf chain up to
   1109  * the higher level protocols.
   1110  *
   1111  * Grrr. Receiving transfers larger than about 1152 bytes sometimes
   1112  * doesn't work. We get an incomplete frame. In order to avoid
   1113  * this, we queue up RX transfers that are shorter than a full sized
   1114  * frame. If the received frame is larger than our transfer size,
   1115  * we snag the rest of the data using a second transfer. Does this
   1116  * hurt performance? Yes. But after fighting with this stupid thing
   1117  * for three days, I'm willing to settle. I'd rather have reliable
   1118  * receive performance that fast but spotty performance.
   1119  */
   1120 static void
   1121 aue_rxeof(xfer, priv, status)
   1122 	usbd_xfer_handle	xfer;
   1123 	usbd_private_handle	priv;
   1124 	usbd_status		status;
   1125 {
   1126 	struct aue_chain	*c = priv;
   1127 	struct aue_softc	*sc = c->aue_sc;
   1128 	struct ifnet		*ifp = GET_IFP(sc);
   1129 	struct mbuf		*m;
   1130 	u_int32_t		total_len;
   1131 	struct aue_rxpkt	r;
   1132 #if defined(__NetBSD__) || defined(__OpenBSD__)
   1133 	int			s;
   1134 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1135 
   1136 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
   1137 
   1138 	if (!(ifp->if_flags & IFF_RUNNING))
   1139 		return;
   1140 
   1141 	if (status != USBD_NORMAL_COMPLETION) {
   1142 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   1143 			return;
   1144 		printf("%s: usb error on rx: %s\n", USBDEVNAME(sc->aue_dev),
   1145 		    usbd_errstr(status));
   1146 		if (status == USBD_STALLED)
   1147 			usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
   1148 		goto done;
   1149 	}
   1150 
   1151 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
   1152 
   1153 	/* XXX copy data to mbuf */
   1154 	memcpy(mtod(c->aue_mbuf, char*) + c->aue_accum, c->aue_buf, total_len);
   1155 
   1156 	/*
   1157 	 * See if we've already accumulated some data from
   1158 	 * a previous transfer.
   1159 	 */
   1160 	if (c->aue_accum) {
   1161 		total_len += c->aue_accum;
   1162 		c->aue_accum = 0;
   1163 	}
   1164 
   1165 	if (total_len <= 4 + ETHER_CRC_LEN) {
   1166 		ifp->if_ierrors++;
   1167 		goto done;
   1168 	}
   1169 
   1170 	m = c->aue_mbuf;
   1171 	memcpy(&r, mtod(m, char *) + total_len - 4, sizeof(r));
   1172 
   1173 	/* Turn off all the non-error bits in the rx status word. */
   1174 	r.aue_rxstat &= AUE_RXSTAT_MASK;
   1175 
   1176 	/*
   1177 	 * Check to see if this is just the first chunk of a
   1178 	 * split transfer. We really need a more reliable way
   1179 	 * to detect this.
   1180 	 */
   1181 	if (UGETW(r.aue_pktlen) != total_len && total_len == AUE_CUTOFF) {
   1182 		c->aue_accum = AUE_CUTOFF;
   1183 		usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
   1184 		    c, c->aue_buf,
   1185 		    AUE_CUTOFF, USBD_SHORT_XFER_OK,
   1186 		    USBD_NO_TIMEOUT, aue_rxeof);
   1187 		DPRINTFN(5,("%s: %s: extra rx\n", USBDEVNAME(sc->aue_dev),
   1188 			    __FUNCTION__));
   1189 		usbd_transfer(xfer);
   1190 		return;
   1191 	}
   1192 
   1193 	if (r.aue_rxstat) {
   1194 		ifp->if_ierrors++;
   1195 		goto done;
   1196 	}
   1197 
   1198 	/* No errors; receive the packet. */
   1199 	total_len -= ETHER_CRC_LEN + 4;
   1200 	m->m_pkthdr.len = m->m_len = total_len;
   1201 	ifp->if_ipackets++;
   1202 
   1203 #if defined(__FreeBSD__)
   1204 	m->m_pkthdr.rcvif = (struct ifnet *)&kue_qdat;
   1205 	/* Put the packet on the special USB input queue. */
   1206 	usb_ether_input(m);
   1207 
   1208 	return;
   1209 
   1210 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1211 	m->m_pkthdr.rcvif = ifp;
   1212 
   1213 	s = splimp();
   1214 
   1215 	/* XXX ugly */
   1216 	if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
   1217 		ifp->if_ierrors++;
   1218 		goto done1;
   1219 	}
   1220 
   1221 	/*
   1222 	 * Handle BPF listeners. Let the BPF user see the packet, but
   1223 	 * don't pass it up to the ether_input() layer unless it's
   1224 	 * a broadcast packet, multicast packet, matches our ethernet
   1225 	 * address or the interface is in promiscuous mode.
   1226 	 */
   1227 	if (ifp->if_bpf) {
   1228 		struct ether_header *eh = mtod(m, struct ether_header *);
   1229 		bpf_mtap(ifp, m);
   1230 		if ((ifp->if_flags & IFF_PROMISC) &&
   1231 		    memcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
   1232 			   ETHER_ADDR_LEN) &&
   1233 		    !(eh->ether_dhost[0] & 1)) {
   1234 			m_freem(m);
   1235 			goto done1;
   1236 		}
   1237 	}
   1238 
   1239 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->aue_dev),
   1240 		    __FUNCTION__, m->m_len));
   1241 	(*ifp->if_input)(ifp, m);
   1242  done1:
   1243 	splx(s);
   1244 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1245 
   1246  done:
   1247 
   1248 	/* Setup new transfer. */
   1249 	usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
   1250 	    c, c->aue_buf, AUE_CUTOFF,
   1251 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
   1252 	    USBD_NO_TIMEOUT, aue_rxeof);
   1253 	usbd_transfer(xfer);
   1254 
   1255 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->aue_dev),
   1256 		    __FUNCTION__));
   1257 }
   1258 
   1259 /*
   1260  * A frame was downloaded to the chip. It's safe for us to clean up
   1261  * the list buffers.
   1262  */
   1263 
   1264 static void
   1265 aue_txeof(xfer, priv, status)
   1266 	usbd_xfer_handle	xfer;
   1267 	usbd_private_handle	priv;
   1268 	usbd_status		status;
   1269 {
   1270 	struct aue_chain	*c = priv;
   1271 	struct aue_softc	*sc = c->aue_sc;
   1272 	struct ifnet		*ifp = GET_IFP(sc);
   1273 	int			s;
   1274 
   1275 	s = splimp();
   1276 
   1277 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->aue_dev),
   1278 		    __FUNCTION__, status));
   1279 
   1280 	ifp->if_timer = 0;
   1281 	ifp->if_flags &= ~IFF_OACTIVE;
   1282 
   1283 	if (status != USBD_NORMAL_COMPLETION) {
   1284 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1285 			splx(s);
   1286 			return;
   1287 		}
   1288 		ifp->if_oerrors++;
   1289 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->aue_dev),
   1290 		    usbd_errstr(status));
   1291 		if (status == USBD_STALLED)
   1292 			usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]);
   1293 		splx(s);
   1294 		return;
   1295 	}
   1296 
   1297 	ifp->if_opackets++;
   1298 
   1299 #if defined(__FreeBSD__)
   1300 	c->aue_mbuf->m_pkthdr.rcvif = ifp;
   1301 	usb_tx_done(c->aue_mbuf);
   1302   	c->aue_mbuf = NULL;
   1303 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1304 	m_freem(c->aue_mbuf);
   1305 	c->aue_mbuf = NULL;
   1306 
   1307 	if (ifp->if_snd.ifq_head != NULL)
   1308 		aue_start(ifp);
   1309 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1310 
   1311 	splx(s);
   1312 }
   1313 
   1314 static void
   1315 aue_tick(xsc)
   1316 	void			*xsc;
   1317 {
   1318 	struct aue_softc	*sc = xsc;
   1319 	struct ifnet		*ifp;
   1320 	struct mii_data		*mii;
   1321 	int			s;
   1322 
   1323 	DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
   1324 
   1325 	if (sc == NULL)
   1326 		return;
   1327 
   1328 	ifp = GET_IFP(sc);
   1329 	mii = GET_MII(sc);
   1330 	if (mii == NULL)
   1331 		return;
   1332 
   1333 	s = splimp();
   1334 
   1335 	mii_tick(mii);
   1336 	if (!sc->aue_link) {
   1337 		mii_pollstat(mii);
   1338 		if (mii->mii_media_status & IFM_ACTIVE &&
   1339 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
   1340 			DPRINTFN(2,("%s: %s: got link\n",
   1341 				    USBDEVNAME(sc->aue_dev),__FUNCTION__));
   1342 			sc->aue_link++;
   1343 			if (ifp->if_snd.ifq_head != NULL)
   1344 				aue_start(ifp);
   1345 		}
   1346 	}
   1347 
   1348 	usb_timeout(aue_tick, sc, hz, sc->aue_stat_ch);
   1349 
   1350 	splx(s);
   1351 }
   1352 
   1353 static int
   1354 aue_send(sc, m, idx)
   1355 	struct aue_softc	*sc;
   1356 	struct mbuf		*m;
   1357 	int			idx;
   1358 {
   1359 	int			total_len;
   1360 	struct aue_chain	*c;
   1361 	usbd_status		err;
   1362 
   1363 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
   1364 
   1365 	c = &sc->aue_cdata.aue_tx_chain[idx];
   1366 
   1367 	/*
   1368 	 * Copy the mbuf data into a contiguous buffer, leaving two
   1369 	 * bytes at the beginning to hold the frame length.
   1370 	 */
   1371 	m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
   1372 	c->aue_mbuf = m;
   1373 
   1374 	/*
   1375 	 * The ADMtek documentation says that the packet length is
   1376 	 * supposed to be specified in the first two bytes of the
   1377 	 * transfer, however it actually seems to ignore this info
   1378 	 * and base the frame size on the bulk transfer length.
   1379 	 */
   1380 	c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
   1381 	c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 3) & 0xE0;
   1382 	total_len = m->m_pkthdr.len + 2;
   1383 
   1384 	usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
   1385 	    c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
   1386 	    AUE_TX_TIMEOUT, aue_txeof);
   1387 
   1388 	/* Transmit */
   1389 	err = usbd_transfer(c->aue_xfer);
   1390 	if (err != USBD_IN_PROGRESS) {
   1391 		aue_stop(sc);
   1392 		return (EIO);
   1393 	}
   1394 	DPRINTFN(5,("%s: %s: send %d bytes\n", USBDEVNAME(sc->aue_dev),
   1395 		    __FUNCTION__, total_len));
   1396 
   1397 	sc->aue_cdata.aue_tx_cnt++;
   1398 
   1399 	return (0);
   1400 }
   1401 
   1402 static void
   1403 aue_start(ifp)
   1404 	struct ifnet		*ifp;
   1405 {
   1406 	struct aue_softc	*sc = ifp->if_softc;
   1407 	struct mbuf		*m_head = NULL;
   1408 
   1409 	DPRINTFN(5,("%s: %s: enter, link=%d\n", USBDEVNAME(sc->aue_dev),
   1410 		    __FUNCTION__, sc->aue_link));
   1411 
   1412 	if (!sc->aue_link)
   1413 		return;
   1414 
   1415 	if (ifp->if_flags & IFF_OACTIVE)
   1416 		return;
   1417 
   1418 	IF_DEQUEUE(&ifp->if_snd, m_head);
   1419 	if (m_head == NULL)
   1420 		return;
   1421 
   1422 	if (aue_send(sc, m_head, 0)) {
   1423 		IF_PREPEND(&ifp->if_snd, m_head);
   1424 		ifp->if_flags |= IFF_OACTIVE;
   1425 		return;
   1426 	}
   1427 
   1428 	/*
   1429 	 * If there's a BPF listener, bounce a copy of this frame
   1430 	 * to him.
   1431 	 */
   1432 	if (ifp->if_bpf)
   1433 		bpf_mtap(ifp, m_head);
   1434 
   1435 	ifp->if_flags |= IFF_OACTIVE;
   1436 
   1437 	/*
   1438 	 * Set a timeout in case the chip goes out to lunch.
   1439 	 */
   1440 	ifp->if_timer = 5;
   1441 }
   1442 
   1443 static void
   1444 aue_init(xsc)
   1445 	void			*xsc;
   1446 {
   1447 	struct aue_softc	*sc = xsc;
   1448 	struct ifnet		*ifp = GET_IFP(sc);
   1449 	struct mii_data		*mii = GET_MII(sc);
   1450 	struct aue_chain	*c;
   1451 	usbd_status		err;
   1452 	int			i, s;
   1453 	u_char			*eaddr;
   1454 
   1455 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1456 
   1457 	if (ifp->if_flags & IFF_RUNNING)
   1458 		return;
   1459 
   1460 	s = splimp();
   1461 
   1462 	/*
   1463 	 * Cancel pending I/O and free all RX/TX buffers.
   1464 	 */
   1465 	aue_reset(sc);
   1466 
   1467 #if defined(__FreeBSD__)
   1468 	eaddr = sc->arpcom.ac_enaddr;
   1469 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1470 	eaddr = LLADDR(ifp->if_sadl);
   1471 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1472 	for (i = 0; i < ETHER_ADDR_LEN; i++)
   1473 		csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
   1474 
   1475 	 /* If we want promiscuous mode, set the allframes bit. */
   1476 	if (ifp->if_flags & IFF_PROMISC)
   1477 		AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1478 	else
   1479 		AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1480 
   1481 	/* Init TX ring. */
   1482 	if (aue_tx_list_init(sc) == ENOBUFS) {
   1483 		printf("%s: tx list init failed\n", USBDEVNAME(sc->aue_dev));
   1484 		splx(s);
   1485 		return;
   1486 	}
   1487 
   1488 	/* Init RX ring. */
   1489 	if (aue_rx_list_init(sc) == ENOBUFS) {
   1490 		printf("%s: rx list init failed\n", USBDEVNAME(sc->aue_dev));
   1491 		splx(s);
   1492 		return;
   1493 	}
   1494 
   1495 	/* Load the multicast filter. */
   1496 	aue_setmulti(sc);
   1497 
   1498 	/* Enable RX and TX */
   1499 	csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND|AUE_CTL0_RX_ENB);
   1500 	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
   1501 	AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
   1502 
   1503 	mii_mediachg(mii);
   1504 
   1505 	if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
   1506 	/* Open RX and TX pipes. */
   1507 	err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
   1508 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
   1509 	if (err) {
   1510 		printf("%s: open rx pipe failed: %s\n",
   1511 		    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1512 		splx(s);
   1513 		return;
   1514 	}
   1515 	usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
   1516 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
   1517 	if (err) {
   1518 		printf("%s: open tx pipe failed: %s\n",
   1519 		    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1520 		splx(s);
   1521 		return;
   1522 	}
   1523 	err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
   1524 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
   1525 	    &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr);
   1526 	if (err) {
   1527 		printf("%s: open intr pipe failed: %s\n",
   1528 		    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1529 		splx(s);
   1530 		return;
   1531 	}
   1532 
   1533 	/* Start up the receive pipe. */
   1534 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
   1535 		c = &sc->aue_cdata.aue_rx_chain[i];
   1536 		usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
   1537 		    c, c->aue_buf, AUE_CUTOFF,
   1538 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
   1539 		    aue_rxeof);
   1540 		usbd_transfer(c->aue_xfer);
   1541 		DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->aue_dev),
   1542 			    __FUNCTION__));
   1543 
   1544 	}
   1545 	}
   1546 
   1547 	ifp->if_flags |= IFF_RUNNING;
   1548 	ifp->if_flags &= ~IFF_OACTIVE;
   1549 
   1550 	splx(s);
   1551 
   1552 	usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
   1553 	usb_timeout(aue_tick, sc, hz, sc->aue_stat_ch);
   1554 }
   1555 
   1556 /*
   1557  * Set media options.
   1558  */
   1559 static int
   1560 aue_ifmedia_upd(ifp)
   1561 	struct ifnet		*ifp;
   1562 {
   1563 	struct aue_softc	*sc = ifp->if_softc;
   1564 	struct mii_data		*mii = GET_MII(sc);
   1565 
   1566 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1567 
   1568 	sc->aue_link = 0;
   1569 	if (mii->mii_instance) {
   1570 		struct mii_softc	*miisc;
   1571 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
   1572 		    miisc = LIST_NEXT(miisc, mii_list))
   1573 			 mii_phy_reset(miisc);
   1574 	}
   1575 	mii_mediachg(mii);
   1576 
   1577 	return (0);
   1578 }
   1579 
   1580 /*
   1581  * Report current media status.
   1582  */
   1583 static void
   1584 aue_ifmedia_sts(ifp, ifmr)
   1585 	struct ifnet		*ifp;
   1586 	struct ifmediareq	*ifmr;
   1587 {
   1588 	struct aue_softc	*sc = ifp->if_softc;
   1589 	struct mii_data		*mii = GET_MII(sc);
   1590 
   1591 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1592 
   1593 	mii_pollstat(mii);
   1594 	ifmr->ifm_active = mii->mii_media_active;
   1595 	ifmr->ifm_status = mii->mii_media_status;
   1596 }
   1597 
   1598 static int
   1599 aue_ioctl(ifp, command, data)
   1600 	struct ifnet		*ifp;
   1601 	u_long			command;
   1602 	caddr_t			data;
   1603 {
   1604 	struct aue_softc	*sc = ifp->if_softc;
   1605 #if defined(__NetBSD__) || defined(__OpenBSD__)
   1606 	struct ifaddr 		*ifa = (struct ifaddr *)data;
   1607 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1608 	struct ifreq		*ifr = (struct ifreq *)data;
   1609 	struct mii_data		*mii;
   1610 	int			s, error = 0;
   1611 
   1612 	s = splimp();
   1613 
   1614 	switch(command) {
   1615 #if defined(__FreeBSD__)
   1616 	case SIOCSIFADDR:
   1617 	case SIOCGIFADDR:
   1618 	case SIOCSIFMTU:
   1619 		error = ether_ioctl(ifp, command, data);
   1620 		break;
   1621 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1622 	case SIOCSIFADDR:
   1623 		ifp->if_flags |= IFF_UP;
   1624 		aue_init(sc);
   1625 
   1626 		switch (ifa->ifa_addr->sa_family) {
   1627 #ifdef INET
   1628 		case AF_INET:
   1629 			arp_ifinit(ifp, ifa);
   1630 			break;
   1631 #endif /* INET */
   1632 #ifdef NS
   1633 		case AF_NS:
   1634 		    {
   1635 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1636 
   1637 			if (ns_nullhost(*ina))
   1638 				ina->x_host = *(union ns_host *)
   1639 					LLADDR(ifp->if_sadl);
   1640 			else
   1641 				memcpy(LLADDR(ifp->if_sadl),
   1642 				       ina->x_host.c_host,
   1643 				       ifp->if_addrlen);
   1644 			break;
   1645 		    }
   1646 #endif /* NS */
   1647 		}
   1648 		break;
   1649 
   1650 	case SIOCSIFMTU:
   1651 		if (ifr->ifr_mtu > ETHERMTU)
   1652 			error = EINVAL;
   1653 		else
   1654 			ifp->if_mtu = ifr->ifr_mtu;
   1655 		break;
   1656 
   1657 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1658 	case SIOCSIFFLAGS:
   1659 		if (ifp->if_flags & IFF_UP) {
   1660 			if (ifp->if_flags & IFF_RUNNING &&
   1661 			    ifp->if_flags & IFF_PROMISC &&
   1662 			    !(sc->aue_if_flags & IFF_PROMISC)) {
   1663 				AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1664 			} else if (ifp->if_flags & IFF_RUNNING &&
   1665 			    !(ifp->if_flags & IFF_PROMISC) &&
   1666 			    sc->aue_if_flags & IFF_PROMISC) {
   1667 				AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1668 			} else if (!(ifp->if_flags & IFF_RUNNING))
   1669 				aue_init(sc);
   1670 		} else {
   1671 			if (ifp->if_flags & IFF_RUNNING)
   1672 				aue_stop(sc);
   1673 		}
   1674 		sc->aue_if_flags = ifp->if_flags;
   1675 		error = 0;
   1676 		break;
   1677 	case SIOCADDMULTI:
   1678 	case SIOCDELMULTI:
   1679 		aue_setmulti(sc);
   1680 		error = 0;
   1681 		break;
   1682 	case SIOCGIFMEDIA:
   1683 	case SIOCSIFMEDIA:
   1684 		mii = GET_MII(sc);
   1685 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
   1686 		break;
   1687 	default:
   1688 		error = EINVAL;
   1689 		break;
   1690 	}
   1691 
   1692 	splx(s);
   1693 
   1694 	return (error);
   1695 }
   1696 
   1697 static void
   1698 aue_watchdog(ifp)
   1699 	struct ifnet		*ifp;
   1700 {
   1701 	struct aue_softc	*sc = ifp->if_softc;
   1702 
   1703 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1704 
   1705 	ifp->if_oerrors++;
   1706 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->aue_dev));
   1707 
   1708 	/*
   1709 	 * The polling business is a kludge to avoid allowing the
   1710 	 * USB code to call tsleep() in usbd_delay_ms(), which will
   1711 	 * kill us since the watchdog routine is invoked from
   1712 	 * interrupt context.
   1713 	 */
   1714 	usbd_set_polling(sc->aue_udev, 1);
   1715 	aue_stop(sc);
   1716 	aue_init(sc);
   1717 	usbd_set_polling(sc->aue_udev, 0);
   1718 
   1719 	if (ifp->if_snd.ifq_head != NULL)
   1720 		aue_start(ifp);
   1721 }
   1722 
   1723 /*
   1724  * Stop the adapter and free any mbufs allocated to the
   1725  * RX and TX lists.
   1726  */
   1727 static void
   1728 aue_stop(sc)
   1729 	struct aue_softc	*sc;
   1730 {
   1731 	usbd_status		err;
   1732 	struct ifnet		*ifp;
   1733 	int			i;
   1734 
   1735 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1736 
   1737 	ifp = GET_IFP(sc);
   1738 	ifp->if_timer = 0;
   1739 
   1740 	csr_write_1(sc, AUE_CTL0, 0);
   1741 	csr_write_1(sc, AUE_CTL1, 0);
   1742 	aue_reset(sc);
   1743 	usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
   1744 
   1745 	/* Stop transfers. */
   1746 	if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
   1747 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
   1748 		if (err) {
   1749 			printf("%s: abort rx pipe failed: %s\n",
   1750 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1751 		}
   1752 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
   1753 		if (err) {
   1754 			printf("%s: close rx pipe failed: %s\n",
   1755 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1756 		}
   1757 		sc->aue_ep[AUE_ENDPT_RX] = NULL;
   1758 	}
   1759 
   1760 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
   1761 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
   1762 		if (err) {
   1763 			printf("%s: abort tx pipe failed: %s\n",
   1764 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1765 		}
   1766 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
   1767 		if (err) {
   1768 			printf("%s: close tx pipe failed: %s\n",
   1769 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1770 		}
   1771 		sc->aue_ep[AUE_ENDPT_TX] = NULL;
   1772 	}
   1773 
   1774 	if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
   1775 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
   1776 		if (err) {
   1777 			printf("%s: abort intr pipe failed: %s\n",
   1778 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1779 		}
   1780 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
   1781 		if (err) {
   1782 			printf("%s: close intr pipe failed: %s\n",
   1783 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1784 		}
   1785 		sc->aue_ep[AUE_ENDPT_INTR] = NULL;
   1786 	}
   1787 
   1788 	/* Free RX resources. */
   1789 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
   1790 		if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
   1791 			m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
   1792 			sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
   1793 		}
   1794 		if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
   1795 			usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
   1796 			sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
   1797 		}
   1798 	}
   1799 
   1800 	/* Free TX resources. */
   1801 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
   1802 		if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
   1803 			m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
   1804 			sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
   1805 		}
   1806 		if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
   1807 			usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
   1808 			sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
   1809 		}
   1810 	}
   1811 
   1812 	sc->aue_link = 0;
   1813 
   1814 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1815 }
   1816 
   1817 #ifdef __FreeBSD__
   1818 /*
   1819  * Stop all chip I/O so that the kernel's probe routines don't
   1820  * get confused by errant DMAs when rebooting.
   1821  */
   1822 static void
   1823 aue_shutdown(dev)
   1824 	device_ptr_t		dev;
   1825 {
   1826 	struct aue_softc	*sc = USBGETSOFTC(dev);
   1827 
   1828 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1829 
   1830 	aue_reset(sc);
   1831 	aue_stop(sc);
   1832 }
   1833 #endif
   1834