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