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