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