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