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