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