Home | History | Annotate | Line # | Download | only in usb
if_aue.c revision 1.4
      1 /*	$NetBSD: if_aue.c,v 1.4 2000/01/16 15:12:41 augustss Exp $	*/
      2 /*
      3  * Copyright (c) 1997, 1998, 1999, 2000
      4  *	Bill Paul <wpaul (at) ee.columbia.edu>.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Bill Paul.
     17  * 4. Neither the name of the author nor the names of any co-contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     31  * THE POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  * $FreeBSD: src/sys/dev/usb/if_aue.c,v 1.11 2000/01/14 01:36:14 wpaul Exp $
     34  */
     35 
     36 /*
     37  * ADMtek AN986 Pegasus USB to ethernet driver. Datasheet is available
     38  * from http://www.admtek.com.tw.
     39  *
     40  * Written by Bill Paul <wpaul (at) ee.columbia.edu>
     41  * Electrical Engineering Department
     42  * Columbia University, New York City
     43  */
     44 
     45 /*
     46  * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet
     47  * support: the control endpoint for reading/writing registers, burst
     48  * read endpoint for packet reception, burst write for packet transmission
     49  * and one for "interrupts." The chip uses the same RX filter scheme
     50  * as the other ADMtek ethernet parts: one perfect filter entry for the
     51  * the station address and a 64-bit multicast hash table. The chip supports
     52  * both MII and HomePNA attachments.
     53  *
     54  * Since the maximum data transfer speed of USB is supposed to be 12Mbps,
     55  * you're never really going to get 100Mbps speeds from this device. I
     56  * think the idea is to allow the device to connect to 10 or 100Mbps
     57  * networks, not necessarily to provide 100Mbps performance. Also, since
     58  * the controller uses an external PHY chip, it's possible that board
     59  * designers might simply choose a 10Mbps PHY.
     60  *
     61  * Registers are accessed using usbd_do_request(). Packet transfers are
     62  * done using usbd_transfer() and friends.
     63  */
     64 
     65 /*
     66  * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
     67  */
     68 
     69 /*
     70  * TODO:
     71  * better error messages from rxstat
     72  * split out if_auevar.h
     73  * add thread to avoid register reads from interrupt context
     74  * more error checks
     75  * investigate short rx problem
     76  */
     77 
     78 #if defined(__NetBSD__) || defined(__OpenBSD__)
     79 #include "opt_inet.h"
     80 #include "opt_ns.h"
     81 #include "bpfilter.h"
     82 #include "rnd.h"
     83 #endif
     84 
     85 #include <sys/param.h>
     86 #include <sys/systm.h>
     87 #include <sys/sockio.h>
     88 #include <sys/mbuf.h>
     89 #include <sys/malloc.h>
     90 #include <sys/kernel.h>
     91 #include <sys/socket.h>
     92 
     93 #if defined(__FreeBSD__)
     94 
     95 #include <net/ethernet.h>
     96 #include <machine/clock.h>	/* for DELAY */
     97 #include <sys/bus.h>
     98 /* "controller miibus0" required.  See GENERIC if you get errors here. */
     99 #include "miibus_if.h"
    100 
    101 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    102 
    103 #include <sys/device.h>
    104 
    105 #endif
    106 
    107 #include <net/if.h>
    108 #include <net/if_arp.h>
    109 #include <net/if_dl.h>
    110 #include <net/if_media.h>
    111 
    112 #if defined(__NetBSD__) || defined(__OpenBSD__)
    113 #include <net/if_ether.h>
    114 
    115 #define bpf_mtap(ifp, m) bpf_tap((ifp)->if_bpf, mtod((m), caddr_t), (m)->m_len)
    116 
    117 #endif
    118 
    119 #if defined(__FreeBSD__) || NBPFILTER > 0
    120 #include <net/bpf.h>
    121 #endif
    122 
    123 #if defined(__NetBSD__) || defined(__OpenBSD__)
    124 #ifdef INET
    125 #include <netinet/in.h>
    126 #include <netinet/if_inarp.h>
    127 #endif
    128 
    129 #ifdef NS
    130 #include <netns/ns.h>
    131 #include <netns/ns_if.h>
    132 #endif
    133 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    134 
    135 #include <dev/mii/mii.h>
    136 #include <dev/mii/miivar.h>
    137 
    138 #include <dev/usb/usb.h>
    139 #include <dev/usb/usbdi.h>
    140 #include <dev/usb/usbdi_util.h>
    141 #include <dev/usb/usbdevs.h>
    142 
    143 #ifdef __FreeBSD__
    144 #include <dev/usb/usb_ethersubr.h>
    145 #endif
    146 
    147 #include <dev/usb/if_auereg.h>
    148 
    149 #define AUE_DEBUG
    150 
    151 #ifdef AUE_DEBUG
    152 #define DPRINTF(x)	if (auedebug) logprintf x
    153 #define DPRINTFN(n,x)	if (auedebug >= (n)) logprintf x
    154 int	auedebug = 1;
    155 #else
    156 #define DPRINTF(x)
    157 #define DPRINTFN(n,x)
    158 #endif
    159 
    160 int aue_cutoff = AUE_CUTOFF;
    161 #undef AUE_CUTOFF
    162 #define AUE_CUTOFF aue_cutoff
    163 
    164 /*
    165  * Various supported device vendors/types and their names.
    166  */
    167 static struct aue_type aue_devs[] = {
    168 	{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100 },
    169 	{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX },
    170 	{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX },
    171 	{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS },
    172 	{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX },
    173 	{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA },
    174 	{ USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB },
    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_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
    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
    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
    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 		u_int16_t               auxmode;
    550 		auxmode = aue_miibus_readreg(dev, 0, 0x1b);
    551 		aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
    552 	}
    553 }
    554 
    555 #define AUE_POLY	0xEDB88320
    556 #define AUE_BITS	6
    557 
    558 static u_int32_t
    559 aue_crc(addr)
    560 	caddr_t			addr;
    561 {
    562 	u_int32_t		idx, bit, data, crc;
    563 
    564 	/* Compute CRC for the address value. */
    565 	crc = 0xFFFFFFFF; /* initial value */
    566 
    567 	for (idx = 0; idx < 6; idx++) {
    568 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
    569 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0);
    570 	}
    571 
    572 	return (crc & ((1 << AUE_BITS) - 1));
    573 }
    574 
    575 static void
    576 aue_setmulti(sc)
    577 	struct aue_softc	*sc;
    578 {
    579 	struct ifnet		*ifp;
    580 #if defined(__FreeBSD__)
    581 	struct ifmultiaddr	*ifma;
    582 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    583 	struct ether_multi	*enm;
    584 	struct ether_multistep	step;
    585 #endif
    586 	u_int32_t		h = 0, i;
    587 
    588 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
    589 
    590 	ifp = GET_IFP(sc);
    591 
    592 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
    593 		AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
    594 		return;
    595 	}
    596 
    597 	AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
    598 
    599 	/* first, zot all the existing hash bits */
    600 	for (i = 0; i < 8; i++)
    601 		csr_write_1(sc, AUE_MAR0 + i, 0);
    602 
    603 	/* now program new ones */
    604 #if defined(__FreeBSD__)
    605 	for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
    606 	    ifma = ifma->ifma_link.le_next) {
    607 		if (ifma->ifma_addr->sa_family != AF_LINK)
    608 			continue;
    609 		h = aue_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
    610 		AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0xF));
    611 	}
    612 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    613 	ETHER_FIRST_MULTI(step, &sc->aue_ec, enm);
    614 	while (enm != NULL) {
    615 #if 1
    616 		if (memcmp(enm->enm_addrlo,
    617 			   enm->enm_addrhi, ETHER_ADDR_LEN) != 0) {
    618 			ifp->if_flags |= IFF_ALLMULTI;
    619 			AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
    620 			return;
    621 		}
    622 #endif
    623 		h = aue_crc(enm->enm_addrlo);
    624 		AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0xF));
    625 		ETHER_NEXT_MULTI(step, enm);
    626 	}
    627 #endif
    628 
    629 	return;
    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 		csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1);
    666 		csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1|
    667 			AUE_GPIO_OUT0);
    668 	}
    669 
    670 	/* Wait a little while for the chip to get its brains in order. */
    671 	DELAY(10000);		/* XXX */
    672 	return;
    673 }
    674 
    675 /*
    676  * Probe for a Pegasus chip.
    677  */
    678 USB_MATCH(aue)
    679 {
    680 	USB_MATCH_START(aue, uaa);
    681 	struct aue_type			*t;
    682 
    683 	if (uaa->iface != NULL)
    684 		return (UMATCH_NONE);
    685 
    686 	for (t = aue_devs; t->aue_vid != 0; t++)
    687 		if (uaa->vendor == t->aue_vid && uaa->product == t->aue_did)
    688 			return (UMATCH_VENDOR_PRODUCT);
    689 
    690 	return (UMATCH_NONE);
    691 }
    692 
    693 /*
    694  * Attach the interface. Allocate softc structures, do ifmedia
    695  * setup and ethernet/BPF attach.
    696  */
    697 USB_ATTACH(aue)
    698 {
    699 	USB_ATTACH_START(aue, sc, uaa);
    700 	char			devinfo[1024];
    701 	int			s;
    702 	u_char			eaddr[ETHER_ADDR_LEN];
    703 	struct ifnet		*ifp;
    704 	struct mii_data		*mii;
    705 	usbd_device_handle	dev = uaa->device;
    706 	usbd_interface_handle	iface;
    707 	usb_interface_descriptor_t	*id;
    708 	usb_endpoint_descriptor_t	*ed;
    709 	usbd_status		err;
    710 	int			i;
    711 
    712 #ifdef __FreeBSD__
    713 	bzero(sc, sizeof(struct aue_softc));
    714 #endif
    715 
    716 	DPRINTFN(5,(" : uan_attach: sc=%p", sc));
    717 
    718 	usbd_devinfo(dev, 0, devinfo);
    719 	USB_ATTACH_SETUP;
    720 	printf("%s: %s\n", USBDEVNAME(sc->aue_dev), devinfo);
    721 
    722 	err = usbd_set_config_no(dev, AUE_CONFIG_NO, 0);
    723 	if (err) {
    724 		printf("%s: setting config no failed\n",
    725 		    USBDEVNAME(sc->aue_dev));
    726 		USB_ATTACH_ERROR_RETURN;
    727 	}
    728 
    729 	err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &iface);
    730 	if (err) {
    731 		printf("%s: getting interface handle failed\n",
    732 		    USBDEVNAME(sc->aue_dev));
    733 		USB_ATTACH_ERROR_RETURN;
    734 	}
    735 
    736 	sc->aue_udev = dev;
    737 	sc->aue_iface = iface;
    738 	sc->aue_product = uaa->product;
    739 	sc->aue_vendor = uaa->vendor;
    740 
    741 	id = usbd_get_interface_descriptor(iface);
    742 
    743 	/* Find endpoints. */
    744 	for (i = 0; i < id->bNumEndpoints; i++) {
    745 		ed = usbd_interface2endpoint_descriptor(iface, i);
    746 		if (!ed) {
    747 			printf("%s: couldn't get endpoint descriptor %d\n",
    748 			    USBDEVNAME(sc->aue_dev), i);
    749 			USB_ATTACH_ERROR_RETURN;
    750 		}
    751 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    752 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    753 			sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
    754 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    755 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    756 			sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
    757 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    758 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
    759 			sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
    760 		}
    761 	}
    762 
    763 	if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 ||
    764 	    sc->aue_ed[AUE_ENDPT_INTR] == 0) {
    765 		printf("%s: missing endpoint\n", USBDEVNAME(sc->aue_dev));
    766 		USB_ATTACH_ERROR_RETURN;
    767 	}
    768 
    769 
    770 	s = splimp();
    771 
    772 	/* Reset the adapter. */
    773 	aue_reset(sc);
    774 
    775 	/*
    776 	 * Get station address from the EEPROM.
    777 	 */
    778 	aue_read_mac(sc, eaddr);
    779 
    780 	/*
    781 	 * A Pegasus chip was detected. Inform the world.
    782 	 */
    783 #if defined(__FreeBSD__)
    784 	printf("%s: Ethernet address: %6D\n", USBDEVNAME(sc->aue_dev),
    785 	    eaddr, ":");
    786 
    787 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
    788 
    789 	ifp = &sc->arpcom.ac_if;
    790 	ifp->if_softc = sc;
    791 	ifp->if_unit = sc->aue_unit;
    792 	ifp->if_name = "aue";
    793 	ifp->if_mtu = ETHERMTU;
    794 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    795 	ifp->if_ioctl = aue_ioctl;
    796 	ifp->if_output = ether_output;
    797 	ifp->if_start = aue_start;
    798 	ifp->if_watchdog = aue_watchdog;
    799 	ifp->if_init = aue_init;
    800 	ifp->if_baudrate = 10000000;
    801 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
    802 
    803 	/*
    804 	 * Do MII setup.
    805 	 * NOTE: Doing this causes child devices to be attached to us,
    806 	 * which we would normally disconnect at in the detach routine
    807 	 * using device_delete_child(). However the USB code is set up
    808 	 * such that when this driver is removed, all childred devices
    809 	 * are removed as well. In effect, the USB code ends up detaching
    810 	 * all of our children for us, so we don't have to do is ourselves
    811 	 * in aue_detach(). It's important to point this out since if
    812 	 * we *do* try to detach the child devices ourselves, we will
    813 	 * end up getting the children deleted twice, which will crash
    814 	 * the system.
    815 	 */
    816 	if (mii_phy_probe(self, &sc->aue_miibus,
    817 	    aue_ifmedia_upd, aue_ifmedia_sts)) {
    818 		printf("%s: MII without any PHY!\n", USBDEVNAME(sc->aue_dev));
    819 		splx(s);
    820 		USB_ATTACH_ERROR_RETURN;
    821 	}
    822 
    823 	aue_qdat.ifp = ifp;
    824 	aue_qdat.if_rxstart = aue_rxstart;
    825 
    826 	/*
    827 	 * Call MI attach routines.
    828 	 */
    829 	if_attach(ifp);
    830 	ether_ifattach(ifp);
    831 	callout_handle_init(&sc->aue_stat_ch);
    832 	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
    833 
    834 	usb_register_netisr();
    835 
    836 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    837 
    838 	printf("%s: Ethernet address %s\n", USBDEVNAME(sc->aue_dev),
    839 	    ether_sprintf(eaddr));
    840 
    841 	/* Initialize interface info.*/
    842 	ifp = &sc->aue_ec.ec_if;
    843 	ifp->if_softc = sc;
    844 	ifp->if_mtu = ETHERMTU;
    845 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    846 	ifp->if_ioctl = aue_ioctl;
    847 	ifp->if_start = aue_start;
    848 	ifp->if_watchdog = aue_watchdog;
    849 	ifp->if_baudrate = 10000000;
    850 	strncpy(ifp->if_xname, USBDEVNAME(sc->aue_dev), IFNAMSIZ);
    851 
    852 	/* Initialize MII/media info. */
    853 	mii = &sc->aue_mii;
    854 	mii->mii_ifp = ifp;
    855 	mii->mii_readreg = aue_miibus_readreg;
    856 	mii->mii_writereg = aue_miibus_writereg;
    857 	mii->mii_statchg = aue_miibus_statchg;
    858 	ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, aue_ifmedia_sts);
    859 	mii_phy_probe(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY);
    860 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
    861 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
    862 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
    863 	} else
    864 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    865 
    866 	/* Attach the interface. */
    867 	if_attach(ifp);
    868 	ether_ifattach(ifp, eaddr);
    869 
    870 #if NBPFILTER > 0
    871 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
    872 		  sizeof(struct ether_header));
    873 #endif
    874 #if RND > 0
    875 	rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->aue_dev),
    876 	    RND_TYPE_NET, 0);
    877 #endif
    878 
    879 #endif /* __NetBSD__ */
    880 
    881 	splx(s);
    882 
    883 	USB_ATTACH_SUCCESS_RETURN;
    884 }
    885 
    886 USB_DETACH(aue)
    887 {
    888 	USB_DETACH_START(aue, sc);
    889 #if defined(__FreeBSD__)
    890 	struct ifnet		*ifp;
    891 	int			s;
    892 
    893 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
    894 
    895 	s = splusb();
    896 
    897 	ifp = &sc->arpcom.ac_if;
    898 
    899 	usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
    900 	if_detach(ifp);
    901 
    902 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL)
    903 		usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
    904 	if (sc->aue_ep[AUE_ENDPT_RX] != NULL)
    905 		usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
    906 	if (sc->aue_ep[AUE_ENDPT_INTR] != NULL)
    907 		usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
    908 
    909 	splx(s);
    910 
    911 	return (0);
    912 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    913 	sc = sc;		/* XXX use sc */
    914 	/* XXX deallocate */
    915 
    916 #ifdef notyet
    917 	/*
    918 	 * Our softc is about to go away, so drop our refernce
    919 	 * to the ifnet.
    920 	 */
    921 	if_delref(sc->aue_ec.ec_if);
    922 	return (0);
    923 #else
    924 	return (EBUSY);
    925 #endif
    926 
    927 #endif
    928 }
    929 
    930 #if defined(__NetBSD__) || defined(__OpenBSD__)
    931 int
    932 aue_activate(self, act)
    933 	device_ptr_t self;
    934 	enum devact act;
    935 {
    936 	struct aue_softc *sc = (struct aue_softc *)self;
    937 
    938 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
    939 
    940 	switch (act) {
    941 	case DVACT_ACTIVATE:
    942 		return (EOPNOTSUPP);
    943 		break;
    944 
    945 	case DVACT_DEACTIVATE:
    946 #ifdef notyet
    947 		/* First, kill off the interface. */
    948 		if_detach(sc->aue_ec.ec_if);
    949 #endif
    950 		sc->aue_dying = 1;
    951 		break;
    952 	}
    953 	return (0);
    954 }
    955 #endif /* __NetBSD__ || __OpenBSD__ */
    956 
    957 /*
    958  * Initialize an RX descriptor and attach an MBUF cluster.
    959  */
    960 static int
    961 aue_newbuf(sc, c, m)
    962 	struct aue_softc	*sc;
    963 	struct aue_chain	*c;
    964 	struct mbuf		*m;
    965 {
    966 	struct mbuf		*m_new = NULL;
    967 
    968 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
    969 
    970 	if (m == NULL) {
    971 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    972 		if (m_new == NULL) {
    973 			printf("%s: no memory for rx list "
    974 			    "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
    975 			return (ENOBUFS);
    976 		}
    977 
    978 		MCLGET(m_new, M_DONTWAIT);
    979 		if (!(m_new->m_flags & M_EXT)) {
    980 			printf("%s: no memory for rx list "
    981 			    "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
    982 			m_freem(m_new);
    983 			return (ENOBUFS);
    984 		}
    985 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    986 	} else {
    987 		m_new = m;
    988 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    989 		m_new->m_data = m_new->m_ext.ext_buf;
    990 	}
    991 
    992 	m_adj(m_new, ETHER_ALIGN);
    993 	c->aue_mbuf = m_new;
    994 
    995 	return (0);
    996 }
    997 
    998 static int
    999 aue_rx_list_init(sc)
   1000 	struct aue_softc	*sc;
   1001 {
   1002 	struct aue_cdata	*cd;
   1003 	struct aue_chain	*c;
   1004 	int			i;
   1005 
   1006 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1007 
   1008 	cd = &sc->aue_cdata;
   1009 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
   1010 		c = &cd->aue_rx_chain[i];
   1011 		c->aue_sc = sc;
   1012 		c->aue_idx = i;
   1013 		c->aue_accum = 0;
   1014 		if (aue_newbuf(sc, c, NULL) == ENOBUFS)
   1015 			return (ENOBUFS);
   1016 		if (c->aue_xfer == NULL) {
   1017 			c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
   1018 			if (c->aue_xfer == NULL)
   1019 				return (ENOBUFS);
   1020 			c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
   1021 			if (c->aue_buf == NULL)
   1022 				return (ENOBUFS); /* XXX free xfer */
   1023 		}
   1024 	}
   1025 
   1026 	return (0);
   1027 }
   1028 
   1029 static int
   1030 aue_tx_list_init(sc)
   1031 	struct aue_softc	*sc;
   1032 {
   1033 	struct aue_cdata	*cd;
   1034 	struct aue_chain	*c;
   1035 	int			i;
   1036 
   1037 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1038 
   1039 	cd = &sc->aue_cdata;
   1040 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
   1041 		c = &cd->aue_tx_chain[i];
   1042 		c->aue_sc = sc;
   1043 		c->aue_idx = i;
   1044 		c->aue_mbuf = NULL;
   1045 		if (c->aue_xfer == NULL) {
   1046 			c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
   1047 			if (c->aue_xfer == NULL)
   1048 				return (ENOBUFS);
   1049 			c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
   1050 			if (c->aue_buf == NULL)
   1051 				return (ENOBUFS);
   1052 		}
   1053 	}
   1054 
   1055 	return (0);
   1056 }
   1057 
   1058 static void
   1059 aue_intr(xfer, priv, status)
   1060 	usbd_xfer_handle	xfer;
   1061 	usbd_private_handle	priv;
   1062 	usbd_status		status;
   1063 {
   1064 	struct aue_softc	*sc = priv;
   1065 	struct ifnet		*ifp = GET_IFP(sc);
   1066 	struct aue_intrpkt	*p = &sc->aue_cdata.aue_ibuf;
   1067 
   1068 	DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
   1069 
   1070 	if (!(ifp->if_flags & IFF_RUNNING))
   1071 		return;
   1072 
   1073 	if (status != USBD_NORMAL_COMPLETION) {
   1074 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1075 			return;
   1076 		}
   1077 		printf("%s: usb error on intr: %s\n", USBDEVNAME(sc->aue_dev),
   1078 		    usbd_errstr(status));
   1079 		if (status == USBD_STALLED)
   1080 			usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
   1081 		return;
   1082 	}
   1083 
   1084 	if (p->aue_txstat0)
   1085 		ifp->if_oerrors++;
   1086 
   1087 	if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
   1088 		ifp->if_collisions++;
   1089 }
   1090 
   1091 #if defined(__FreeBSD__)
   1092 static void
   1093 aue_rxstart(ifp)
   1094 	struct ifnet		*ifp;
   1095 {
   1096 	struct aue_softc	*sc;
   1097 	struct aue_chain	*c;
   1098 
   1099 	sc = ifp->if_softc;
   1100 	c = &sc->aue_cdata.aue_rx_chain[sc->aue_cdata.aue_rx_prod];
   1101 
   1102 	if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
   1103 		ifp->if_ierrors++;
   1104 		return;
   1105 	}
   1106 
   1107 	/* Setup new transfer. */
   1108 	usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
   1109 	    c, mtod(c->aue_mbuf, char *), AUE_CUTOFF, USBD_SHORT_XFER_OK,
   1110 	    USBD_NO_TIMEOUT, aue_rxeof);
   1111 	usbd_transfer(c->aue_xfer);
   1112 
   1113 	return;
   1114 }
   1115 #endif
   1116 
   1117 /*
   1118  * A frame has been uploaded: pass the resulting mbuf chain up to
   1119  * the higher level protocols.
   1120  *
   1121  * Grrr. Receiving transfers larger than about 1152 bytes sometimes
   1122  * doesn't work. We get an incomplete frame. In order to avoid
   1123  * this, we queue up RX transfers that are shorter than a full sized
   1124  * frame. If the received frame is larger than our transfer size,
   1125  * we snag the rest of the data using a second transfer. Does this
   1126  * hurt performance? Yes. But after fighting with this stupid thing
   1127  * for three days, I'm willing to settle. I'd rather have reliable
   1128  * receive performance that fast but spotty performance.
   1129  */
   1130 static void
   1131 aue_rxeof(xfer, priv, status)
   1132 	usbd_xfer_handle	xfer;
   1133 	usbd_private_handle	priv;
   1134 	usbd_status		status;
   1135 {
   1136 	struct aue_chain	*c = priv;
   1137 	struct aue_softc	*sc = c->aue_sc;
   1138 	struct ifnet		*ifp = GET_IFP(sc);
   1139 	struct mbuf		*m;
   1140 	u_int32_t		total_len;
   1141 	struct aue_rxpkt	r;
   1142 #if defined(__NetBSD__) || defined(__OpenBSD__)
   1143 	int			s;
   1144 #endif
   1145 
   1146 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
   1147 
   1148 	if (!(ifp->if_flags & IFF_RUNNING))
   1149 		return;
   1150 
   1151 	if (status != USBD_NORMAL_COMPLETION) {
   1152 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   1153 			return;
   1154 		printf("%s: usb error on rx: %s\n", USBDEVNAME(sc->aue_dev),
   1155 		    usbd_errstr(status));
   1156 		if (status == USBD_STALLED)
   1157 			usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
   1158 		goto done;
   1159 	}
   1160 
   1161 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
   1162 
   1163 	/* XXX copy data to mbuf */
   1164 	memcpy(mtod(c->aue_mbuf, char*) + c->aue_accum, c->aue_buf, total_len);
   1165 
   1166 	/*
   1167 	 * See if we've already accumulated some data from
   1168 	 * a previous transfer.
   1169 	 */
   1170 	if (c->aue_accum) {
   1171 		total_len += c->aue_accum;
   1172 		c->aue_accum = 0;
   1173 	}
   1174 
   1175 	if (total_len <= 4 + ETHER_CRC_LEN) {
   1176 		ifp->if_ierrors++;
   1177 		goto done;
   1178 	}
   1179 
   1180 	m = c->aue_mbuf;
   1181 	memcpy(&r, mtod(m, char *) + total_len - 4, sizeof(r));
   1182 
   1183 	/* Turn off all the non-error bits in the rx status word. */
   1184 	r.aue_rxstat &= AUE_RXSTAT_MASK;
   1185 
   1186 	/*
   1187 	 * Check to see if this is just the first chunk of a
   1188 	 * split transfer. We really need a more reliable way
   1189 	 * to detect this.
   1190 	 */
   1191 	if (UGETW(r.aue_pktlen) != total_len && total_len == AUE_CUTOFF) {
   1192 		c->aue_accum = AUE_CUTOFF;
   1193 		usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
   1194 		    c, c->aue_buf,
   1195 		    AUE_CUTOFF, USBD_SHORT_XFER_OK,
   1196 		    USBD_NO_TIMEOUT, aue_rxeof);
   1197 		DPRINTFN(5,("%s: %s: extra rx\n", USBDEVNAME(sc->aue_dev),
   1198 			    __FUNCTION__));
   1199 		usbd_transfer(xfer);
   1200 		return;
   1201 	}
   1202 
   1203 	if (r.aue_rxstat) {
   1204 		ifp->if_ierrors++;
   1205 		goto done;
   1206 	}
   1207 
   1208 	/* No errors; receive the packet. */
   1209 	total_len -= ETHER_CRC_LEN + 4;
   1210 	m->m_pkthdr.len = m->m_len = total_len;
   1211 	ifp->if_ipackets++;
   1212 
   1213 #if defined(__FreeBSD__)
   1214 	/* Put the packet on the special USB input queue. */
   1215 	usb_ether_input(m);
   1216 	return;
   1217 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1218 	m->m_pkthdr.rcvif = ifp;
   1219 
   1220 	s = splimp();
   1221 
   1222 	/* XXX ugly */
   1223 	if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
   1224 		ifp->if_ierrors++;
   1225 		goto done1;
   1226 	}
   1227 
   1228 	/*
   1229 	 * Handle BPF listeners. Let the BPF user see the packet, but
   1230 	 * don't pass it up to the ether_input() layer unless it's
   1231 	 * a broadcast packet, multicast packet, matches our ethernet
   1232 	 * address or the interface is in promiscuous mode.
   1233 	 */
   1234 	if (ifp->if_bpf) {
   1235 		struct ether_header *eh = mtod(m, struct ether_header *);
   1236 		bpf_mtap(ifp, m);
   1237 		if ((ifp->if_flags & IFF_PROMISC) &&
   1238 		    memcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
   1239 			   ETHER_ADDR_LEN) &&
   1240 		    !(eh->ether_dhost[0] & 1)) {
   1241 			m_freem(m);
   1242 			goto done1;
   1243 		}
   1244 	}
   1245 
   1246 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->aue_dev),
   1247 		    __FUNCTION__, m->m_len));
   1248 	(*ifp->if_input)(ifp, m);
   1249  done1:
   1250 	splx(s);
   1251 #endif
   1252 
   1253  done:
   1254 
   1255 	/* Setup new transfer. */
   1256 	usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
   1257 	    c, c->aue_buf, AUE_CUTOFF,
   1258 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
   1259 	    USBD_NO_TIMEOUT, aue_rxeof);
   1260 	usbd_transfer(xfer);
   1261 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->aue_dev),
   1262 		    __FUNCTION__));
   1263 }
   1264 
   1265 /*
   1266  * A frame was downloaded to the chip. It's safe for us to clean up
   1267  * the list buffers.
   1268  */
   1269 
   1270 static void
   1271 aue_txeof(xfer, priv, status)
   1272 	usbd_xfer_handle	xfer;
   1273 	usbd_private_handle	priv;
   1274 	usbd_status		status;
   1275 {
   1276 	struct aue_chain	*c = priv;
   1277 	struct aue_softc	*sc = c->aue_sc;
   1278 	struct ifnet		*ifp = GET_IFP(sc);
   1279 	int			s;
   1280 
   1281 	s = splimp();
   1282 
   1283 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->aue_dev),
   1284 		    __FUNCTION__, status));
   1285 
   1286 	if (status != USBD_NORMAL_COMPLETION) {
   1287 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1288 			splx(s);
   1289 			return;
   1290 		}
   1291 		ifp->if_oerrors++;
   1292 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->aue_dev),
   1293 		    usbd_errstr(status));
   1294 		if (status == USBD_STALLED)
   1295 			usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]);
   1296 		splx(s);
   1297 		return;
   1298 	}
   1299 
   1300 	ifp->if_timer = 0;
   1301 	ifp->if_flags &= ~IFF_OACTIVE;
   1302 
   1303 	ifp->if_opackets++;
   1304 
   1305 #if defined(__FreeBSD__)
   1306 	c->aue_mbuf->m_pkthdr.rcvif = ifp;
   1307 	usb_tx_done(c->aue_mbuf);
   1308   	c->aue_mbuf = NULL;
   1309 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1310 	m_freem(c->aue_mbuf);
   1311 	c->aue_mbuf = NULL;
   1312 
   1313 	if (ifp->if_snd.ifq_head != NULL)
   1314 		aue_start(ifp);
   1315 #endif
   1316 
   1317 	splx(s);
   1318 }
   1319 
   1320 static void
   1321 aue_tick(xsc)
   1322 	void			*xsc;
   1323 {
   1324 	struct aue_softc	*sc = xsc;
   1325 	struct ifnet		*ifp;
   1326 	struct mii_data		*mii;
   1327 	int			s;
   1328 
   1329 	DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
   1330 
   1331 	if (sc == NULL)
   1332 		return;
   1333 
   1334 	ifp = GET_IFP(sc);
   1335 	mii = GET_MII(sc);
   1336 	if (mii == NULL)
   1337 		return;
   1338 
   1339 	s = splimp();
   1340 
   1341 	mii_tick(mii);
   1342 	if (!sc->aue_link) {
   1343 		mii_pollstat(mii);
   1344 		if (mii->mii_media_status & IFM_ACTIVE &&
   1345 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
   1346 			DPRINTFN(2,("%s: %s: got link\n",
   1347 				    USBDEVNAME(sc->aue_dev),__FUNCTION__));
   1348 			sc->aue_link++;
   1349 			if (ifp->if_snd.ifq_head != NULL)
   1350 				aue_start(ifp);
   1351 		}
   1352 	}
   1353 
   1354 	usb_timeout(aue_tick, sc, hz, sc->aue_stat_ch);
   1355 
   1356 	splx(s);
   1357 
   1358 	return;
   1359 }
   1360 
   1361 static int
   1362 aue_send(sc, m, idx)
   1363 	struct aue_softc	*sc;
   1364 	struct mbuf		*m;
   1365 	int			idx;
   1366 {
   1367 	int			total_len;
   1368 	struct aue_chain	*c;
   1369 	usbd_status		err;
   1370 
   1371 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
   1372 
   1373 	c = &sc->aue_cdata.aue_tx_chain[idx];
   1374 
   1375 	/*
   1376 	 * Copy the mbuf data into a contiguous buffer, leaving two
   1377 	 * bytes at the beginning to hold the frame length.
   1378 	 */
   1379 	m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
   1380 	c->aue_mbuf = m;
   1381 
   1382 	/*
   1383 	 * The ADMtek documentation says that the packet length is
   1384 	 * supposed to be specified in the first two bytes of the
   1385 	 * transfer, however it actually seems to ignore this info
   1386 	 * and base the frame size on the bulk transfer length.
   1387 	 */
   1388 	c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
   1389 	c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 3) & 0xE0;
   1390 	total_len = m->m_pkthdr.len + 2;
   1391 
   1392 	usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
   1393 	    c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
   1394 	    AUE_TX_TIMEOUT, aue_txeof);
   1395 
   1396 	/* Transmit */
   1397 	err = usbd_transfer(c->aue_xfer);
   1398 	if (err != USBD_IN_PROGRESS) {
   1399 		aue_stop(sc);
   1400 		return (EIO);
   1401 	}
   1402 	DPRINTFN(5,("%s: %s: send %d bytes\n", USBDEVNAME(sc->aue_dev),
   1403 		    __FUNCTION__, total_len));
   1404 
   1405 	sc->aue_cdata.aue_tx_cnt++;
   1406 
   1407 	return (0);
   1408 }
   1409 
   1410 static void
   1411 aue_start(ifp)
   1412 	struct ifnet		*ifp;
   1413 {
   1414 	struct aue_softc	*sc = ifp->if_softc;
   1415 	struct mbuf		*m_head = NULL;
   1416 
   1417 	DPRINTFN(5,("%s: %s: enter, link=%d\n", USBDEVNAME(sc->aue_dev),
   1418 		    __FUNCTION__, sc->aue_link));
   1419 
   1420 	if (!sc->aue_link)
   1421 		return;
   1422 
   1423 	if (ifp->if_flags & IFF_OACTIVE)
   1424 		return;
   1425 
   1426 	IF_DEQUEUE(&ifp->if_snd, m_head);
   1427 	if (m_head == NULL)
   1428 		return;
   1429 
   1430 	if (aue_send(sc, m_head, 0)) {
   1431 		IF_PREPEND(&ifp->if_snd, m_head);
   1432 		ifp->if_flags |= IFF_OACTIVE;
   1433 		return;
   1434 	}
   1435 
   1436 	/*
   1437 	 * If there's a BPF listener, bounce a copy of this frame
   1438 	 * to him.
   1439 	 */
   1440 	if (ifp->if_bpf)
   1441 		bpf_mtap(ifp, m_head);
   1442 
   1443 	ifp->if_flags |= IFF_OACTIVE;
   1444 
   1445 	/*
   1446 	 * Set a timeout in case the chip goes out to lunch.
   1447 	 */
   1448 	ifp->if_timer = 5;
   1449 
   1450 	return;
   1451 }
   1452 
   1453 static void
   1454 aue_init(xsc)
   1455 	void			*xsc;
   1456 {
   1457 	struct aue_softc	*sc = xsc;
   1458 	struct ifnet		*ifp = GET_IFP(sc);
   1459 	struct mii_data		*mii = GET_MII(sc);
   1460 	struct aue_chain	*c;
   1461 	usbd_status		err;
   1462 	int			i, s;
   1463 	u_char			*eaddr;
   1464 
   1465 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1466 
   1467 	if (ifp->if_flags & IFF_RUNNING)
   1468 		return;
   1469 
   1470 	s = splimp();
   1471 
   1472 	/*
   1473 	 * Cancel pending I/O and free all RX/TX buffers.
   1474 	 */
   1475 	aue_reset(sc);
   1476 
   1477 #if defined(__FreeBSD__)
   1478 	eaddr = sc->arpcom.ac_enaddr;
   1479 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1480 	eaddr = LLADDR(ifp->if_sadl);
   1481 #endif
   1482 	for (i = 0; i < ETHER_ADDR_LEN; i++)
   1483 		csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
   1484 
   1485 	 /* If we want promiscuous mode, set the allframes bit. */
   1486 	if (ifp->if_flags & IFF_PROMISC)
   1487 		AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1488 	else
   1489 		AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1490 
   1491 	/* Init TX ring. */
   1492 	if (aue_tx_list_init(sc) == ENOBUFS) {
   1493 		printf("%s: tx list init failed\n", USBDEVNAME(sc->aue_dev));
   1494 		splx(s);
   1495 		return;
   1496 	}
   1497 
   1498 	/* Init RX ring. */
   1499 	if (aue_rx_list_init(sc) == ENOBUFS) {
   1500 		printf("%s: rx list init failed\n", USBDEVNAME(sc->aue_dev));
   1501 		splx(s);
   1502 		return;
   1503 	}
   1504 
   1505 	/* Load the multicast filter. */
   1506 	aue_setmulti(sc);
   1507 
   1508 	/* Enable RX and TX */
   1509 	csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND|AUE_CTL0_RX_ENB);
   1510 	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
   1511 	AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
   1512 
   1513 	mii_mediachg(mii);
   1514 
   1515 	if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
   1516 	/* Open RX and TX pipes. */
   1517 	err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
   1518 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
   1519 	if (err) {
   1520 		printf("%s: open rx pipe failed: %s\n",
   1521 		    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1522 		splx(s);
   1523 		return;
   1524 	}
   1525 	usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
   1526 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
   1527 	if (err) {
   1528 		printf("%s: open tx pipe failed: %s\n",
   1529 		    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1530 		splx(s);
   1531 		return;
   1532 	}
   1533 	err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
   1534 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
   1535 	    &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr);
   1536 	if (err) {
   1537 		printf("%s: open intr pipe failed: %s\n",
   1538 		    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1539 		splx(s);
   1540 		return;
   1541 	}
   1542 
   1543 	/* Start up the receive pipe. */
   1544 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
   1545 		c = &sc->aue_cdata.aue_rx_chain[i];
   1546 		usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
   1547 		    c, c->aue_buf, AUE_CUTOFF,
   1548 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
   1549 		    aue_rxeof);
   1550 		usbd_transfer(c->aue_xfer);
   1551 		DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->aue_dev),
   1552 			    __FUNCTION__));
   1553 
   1554 	}
   1555 	}
   1556 
   1557 	ifp->if_flags |= IFF_RUNNING;
   1558 	ifp->if_flags &= ~IFF_OACTIVE;
   1559 
   1560 	splx(s);
   1561 
   1562 	usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
   1563 	usb_timeout(aue_tick, sc, hz, sc->aue_stat_ch);
   1564 
   1565 	return;
   1566 }
   1567 
   1568 /*
   1569  * Set media options.
   1570  */
   1571 static int
   1572 aue_ifmedia_upd(ifp)
   1573 	struct ifnet		*ifp;
   1574 {
   1575 	struct aue_softc	*sc = ifp->if_softc;
   1576 	struct mii_data		*mii = GET_MII(sc);
   1577 
   1578 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1579 
   1580 	sc->aue_link = 0;
   1581 	if (mii->mii_instance) {
   1582 		struct mii_softc	*miisc;
   1583 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
   1584 		    miisc = LIST_NEXT(miisc, mii_list))
   1585 			 mii_phy_reset(miisc);
   1586 	}
   1587 	mii_mediachg(mii);
   1588 
   1589 	return (0);
   1590 }
   1591 
   1592 /*
   1593  * Report current media status.
   1594  */
   1595 static void
   1596 aue_ifmedia_sts(ifp, ifmr)
   1597 	struct ifnet		*ifp;
   1598 	struct ifmediareq	*ifmr;
   1599 {
   1600 	struct aue_softc	*sc = ifp->if_softc;
   1601 	struct mii_data		*mii = GET_MII(sc);
   1602 
   1603 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1604 
   1605 	mii_pollstat(mii);
   1606 	ifmr->ifm_active = mii->mii_media_active;
   1607 	ifmr->ifm_status = mii->mii_media_status;
   1608 
   1609 	return;
   1610 }
   1611 
   1612 static int
   1613 aue_ioctl(ifp, command, data)
   1614 	struct ifnet		*ifp;
   1615 	u_long			command;
   1616 	caddr_t			data;
   1617 {
   1618 	struct aue_softc	*sc = ifp->if_softc;
   1619 #if defined(__NetBSD__) || defined(__OpenBSD__)
   1620 	struct ifaddr 		*ifa = (struct ifaddr *)data;
   1621 #endif
   1622 	struct ifreq		*ifr = (struct ifreq *)data;
   1623 	struct mii_data		*mii;
   1624 	int			s, error = 0;
   1625 
   1626 	s = splimp();
   1627 
   1628 	switch(command) {
   1629 #if defined(__FreeBSD__)
   1630 	case SIOCSIFADDR:
   1631 	case SIOCGIFADDR:
   1632 	case SIOCSIFMTU:
   1633 		error = ether_ioctl(ifp, command, data);
   1634 		break;
   1635 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1636 	case SIOCSIFADDR:
   1637 		ifp->if_flags |= IFF_UP;
   1638 		aue_init(sc);
   1639 
   1640 		switch (ifa->ifa_addr->sa_family) {
   1641 #ifdef INET
   1642 		case AF_INET:
   1643 			arp_ifinit(ifp, ifa);
   1644 			break;
   1645 #endif /* INET */
   1646 #ifdef NS
   1647 		case AF_NS:
   1648 		    {
   1649 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1650 
   1651 			if (ns_nullhost(*ina))
   1652 				ina->x_hsot = *(union ns_host *)
   1653 					LLADDR(ifp->if_sadl);
   1654 			else
   1655 				memcpy(LLADDR(ifp->if_sadl),
   1656 				       ina->x_host.c_host,
   1657 				       ifp->if_addrlen);
   1658 			break;
   1659 		    }
   1660 #endif /* NS */
   1661 		}
   1662 		break;
   1663 
   1664 	case SIOCSIFMTU:
   1665 		if (ifr->ifr_mtu > ETHERMTU)
   1666 			error = EINVAL;
   1667 		else
   1668 			ifp->if_mtu = ifr->ifr_mtu;
   1669 		break;
   1670 
   1671 #endif
   1672 	case SIOCSIFFLAGS:
   1673 		if (ifp->if_flags & IFF_UP) {
   1674 			if (ifp->if_flags & IFF_RUNNING &&
   1675 			    ifp->if_flags & IFF_PROMISC &&
   1676 			    !(sc->aue_if_flags & IFF_PROMISC)) {
   1677 				AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1678 			} else if (ifp->if_flags & IFF_RUNNING &&
   1679 			    !(ifp->if_flags & IFF_PROMISC) &&
   1680 			    sc->aue_if_flags & IFF_PROMISC) {
   1681 				AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1682 			} else if (!(ifp->if_flags & IFF_RUNNING))
   1683 				aue_init(sc);
   1684 		} else {
   1685 			if (ifp->if_flags & IFF_RUNNING)
   1686 				aue_stop(sc);
   1687 		}
   1688 		sc->aue_if_flags = ifp->if_flags;
   1689 		error = 0;
   1690 		break;
   1691 	case SIOCADDMULTI:
   1692 	case SIOCDELMULTI:
   1693 		aue_setmulti(sc);
   1694 		error = 0;
   1695 		break;
   1696 	case SIOCGIFMEDIA:
   1697 	case SIOCSIFMEDIA:
   1698 		mii = GET_MII(sc);
   1699 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
   1700 		break;
   1701 	default:
   1702 		error = EINVAL;
   1703 		break;
   1704 	}
   1705 
   1706 	splx(s);
   1707 
   1708 	return (error);
   1709 }
   1710 
   1711 static void
   1712 aue_watchdog(ifp)
   1713 	struct ifnet		*ifp;
   1714 {
   1715 	struct aue_softc	*sc = ifp->if_softc;
   1716 
   1717 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1718 
   1719 	ifp->if_oerrors++;
   1720 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->aue_dev));
   1721 
   1722 	/*
   1723 	 * The polling business is a kludge to avoid allowing the
   1724 	 * USB code to call tsleep() in usbd_delay_ms(), which will
   1725 	 * kill us since the watchdog routine is invoked from
   1726 	 * interrupt context.
   1727 	 */
   1728 	usbd_set_polling(sc->aue_udev, 1);
   1729 	aue_stop(sc);
   1730 	aue_init(sc);
   1731 	usbd_set_polling(sc->aue_udev, 0);
   1732 
   1733 	if (ifp->if_snd.ifq_head != NULL)
   1734 		aue_start(ifp);
   1735 
   1736 	return;
   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 	return;
   1833 }
   1834 
   1835 #ifdef __FreeBSD__
   1836 /*
   1837  * Stop all chip I/O so that the kernel's probe routines don't
   1838  * get confused by errant DMAs when rebooting.
   1839  */
   1840 static void
   1841 aue_shutdown(dev)
   1842 	device_ptr_t		dev;
   1843 {
   1844 	struct aue_softc	*sc = USBGETSOFTC(dev);
   1845 
   1846 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1847 
   1848 	aue_reset(sc);
   1849 	aue_stop(sc);
   1850 
   1851 	return;
   1852 }
   1853 #endif
   1854