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