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