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