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