Home | History | Annotate | Line # | Download | only in usb
if_aue.c revision 1.47
      1 /*	$NetBSD: if_aue.c,v 1.47 2000/10/24 14:53:59 augustss Exp $	*/
      2 /*
      3  * Copyright (c) 1997, 1998, 1999, 2000
      4  *	Bill Paul <wpaul (at) ee.columbia.edu>.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Bill Paul.
     17  * 4. Neither the name of the author nor the names of any co-contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     31  * THE POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  * $FreeBSD: src/sys/dev/usb/if_aue.c,v 1.11 2000/01/14 01:36:14 wpaul Exp $
     34  */
     35 
     36 /*
     37  * ADMtek AN986 Pegasus USB to ethernet driver. Datasheet is available
     38  * from http://www.admtek.com.tw.
     39  *
     40  * Written by Bill Paul <wpaul (at) ee.columbia.edu>
     41  * Electrical Engineering Department
     42  * Columbia University, New York City
     43  */
     44 
     45 /*
     46  * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet
     47  * support: the control endpoint for reading/writing registers, burst
     48  * read endpoint for packet reception, burst write for packet transmission
     49  * and one for "interrupts." The chip uses the same RX filter scheme
     50  * as the other ADMtek ethernet parts: one perfect filter entry for the
     51  * the station address and a 64-bit multicast hash table. The chip supports
     52  * both MII and HomePNA attachments.
     53  *
     54  * Since the maximum data transfer speed of USB is supposed to be 12Mbps,
     55  * you're never really going to get 100Mbps speeds from this device. I
     56  * think the idea is to allow the device to connect to 10 or 100Mbps
     57  * networks, not necessarily to provide 100Mbps performance. Also, since
     58  * the controller uses an external PHY chip, it's possible that board
     59  * designers might simply choose a 10Mbps PHY.
     60  *
     61  * Registers are accessed using usbd_do_request(). Packet transfers are
     62  * done using usbd_transfer() and friends.
     63  */
     64 
     65 /*
     66  * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
     67  */
     68 
     69 /*
     70  * TODO:
     71  * better error messages from rxstat
     72  * split out if_auevar.h
     73  * add thread to avoid register reads from interrupt context
     74  * more error checks
     75  * investigate short rx problem
     76  * proper cleanup on errors
     77  */
     78 
     79 #if defined(__NetBSD__)
     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 
    906 #if NBPFILTER > 0
    907 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
    908 		  sizeof(struct ether_header));
    909 #endif
    910 #if NRND > 0
    911 	rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->aue_dev),
    912 	    RND_TYPE_NET, 0);
    913 #endif
    914 
    915 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    916 
    917 	usb_callout_init(sc->aue_stat_ch);
    918 
    919 	sc->aue_attached = 1;
    920 	splx(s);
    921 
    922 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->aue_udev,
    923 			   USBDEV(sc->aue_dev));
    924 
    925 	USB_ATTACH_SUCCESS_RETURN;
    926 }
    927 
    928 USB_DETACH(aue)
    929 {
    930 	USB_DETACH_START(aue, sc);
    931 	struct ifnet		*ifp = GET_IFP(sc);
    932 	int			s;
    933 
    934 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
    935 
    936 	s = splusb();
    937 
    938 	usb_uncallout(sc->aue_stat_ch, aue_tick, sc);
    939 
    940 	if (!sc->aue_attached) {
    941 		/* Detached before attached finished, so just bail out. */
    942 		splx(s);
    943 		return (0);
    944 	}
    945 
    946 	if (ifp->if_flags & IFF_RUNNING)
    947 		aue_stop(sc);
    948 
    949 #if defined(__NetBSD__)
    950 #if NRND > 0
    951 	rnd_detach_source(&sc->rnd_source);
    952 #endif
    953 	mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY);
    954 	ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY);
    955 #if NBPFILTER > 0
    956 	bpfdetach(ifp);
    957 #endif
    958 	ether_ifdetach(ifp);
    959 #endif /* __NetBSD__ */
    960 
    961 	if_detach(ifp);
    962 
    963 #ifdef DIAGNOSTIC
    964 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL ||
    965 	    sc->aue_ep[AUE_ENDPT_RX] != NULL ||
    966 	    sc->aue_ep[AUE_ENDPT_INTR] != NULL)
    967 		printf("%s: detach has active endpoints\n",
    968 		       USBDEVNAME(sc->aue_dev));
    969 #endif
    970 
    971 	sc->aue_attached = 0;
    972 	splx(s);
    973 
    974 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->aue_udev,
    975 			   USBDEV(sc->aue_dev));
    976 
    977 	return (0);
    978 }
    979 
    980 #if defined(__NetBSD__) || defined(__OpenBSD__)
    981 int
    982 aue_activate(device_ptr_t self, enum devact act)
    983 {
    984 	struct aue_softc *sc = (struct aue_softc *)self;
    985 
    986 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
    987 
    988 	switch (act) {
    989 	case DVACT_ACTIVATE:
    990 		return (EOPNOTSUPP);
    991 		break;
    992 
    993 	case DVACT_DEACTIVATE:
    994 		if_deactivate(&sc->aue_ec.ec_if);
    995 		sc->aue_dying = 1;
    996 		break;
    997 	}
    998 	return (0);
    999 }
   1000 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1001 
   1002 /*
   1003  * Initialize an RX descriptor and attach an MBUF cluster.
   1004  */
   1005 Static int
   1006 aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m)
   1007 {
   1008 	struct mbuf		*m_new = NULL;
   1009 
   1010 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
   1011 
   1012 	if (m == NULL) {
   1013 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
   1014 		if (m_new == NULL) {
   1015 			printf("%s: no memory for rx list "
   1016 			    "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
   1017 			return (ENOBUFS);
   1018 		}
   1019 
   1020 		MCLGET(m_new, M_DONTWAIT);
   1021 		if (!(m_new->m_flags & M_EXT)) {
   1022 			printf("%s: no memory for rx list "
   1023 			    "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
   1024 			m_freem(m_new);
   1025 			return (ENOBUFS);
   1026 		}
   1027 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
   1028 	} else {
   1029 		m_new = m;
   1030 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
   1031 		m_new->m_data = m_new->m_ext.ext_buf;
   1032 	}
   1033 
   1034 	m_adj(m_new, ETHER_ALIGN);
   1035 	c->aue_mbuf = m_new;
   1036 
   1037 	return (0);
   1038 }
   1039 
   1040 Static int
   1041 aue_rx_list_init(struct aue_softc *sc)
   1042 {
   1043 	struct aue_cdata	*cd;
   1044 	struct aue_chain	*c;
   1045 	int			i;
   1046 
   1047 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1048 
   1049 	cd = &sc->aue_cdata;
   1050 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
   1051 		c = &cd->aue_rx_chain[i];
   1052 		c->aue_sc = sc;
   1053 		c->aue_idx = i;
   1054 		if (aue_newbuf(sc, c, NULL) == ENOBUFS)
   1055 			return (ENOBUFS);
   1056 		if (c->aue_xfer == NULL) {
   1057 			c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
   1058 			if (c->aue_xfer == NULL)
   1059 				return (ENOBUFS);
   1060 			c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
   1061 			if (c->aue_buf == NULL)
   1062 				return (ENOBUFS); /* XXX free xfer */
   1063 		}
   1064 	}
   1065 
   1066 	return (0);
   1067 }
   1068 
   1069 Static int
   1070 aue_tx_list_init(struct aue_softc *sc)
   1071 {
   1072 	struct aue_cdata	*cd;
   1073 	struct aue_chain	*c;
   1074 	int			i;
   1075 
   1076 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1077 
   1078 	cd = &sc->aue_cdata;
   1079 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
   1080 		c = &cd->aue_tx_chain[i];
   1081 		c->aue_sc = sc;
   1082 		c->aue_idx = i;
   1083 		c->aue_mbuf = NULL;
   1084 		if (c->aue_xfer == NULL) {
   1085 			c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
   1086 			if (c->aue_xfer == NULL)
   1087 				return (ENOBUFS);
   1088 			c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
   1089 			if (c->aue_buf == NULL)
   1090 				return (ENOBUFS);
   1091 		}
   1092 	}
   1093 
   1094 	return (0);
   1095 }
   1096 
   1097 Static void
   1098 aue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
   1099 {
   1100 	struct aue_softc	*sc = priv;
   1101 	struct ifnet		*ifp = GET_IFP(sc);
   1102 	struct aue_intrpkt	*p = &sc->aue_cdata.aue_ibuf;
   1103 
   1104 	DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
   1105 
   1106 	if (sc->aue_dying)
   1107 		return;
   1108 
   1109 	if (!(ifp->if_flags & IFF_RUNNING))
   1110 		return;
   1111 
   1112 	if (status != USBD_NORMAL_COMPLETION) {
   1113 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1114 			return;
   1115 		}
   1116 		sc->aue_intr_errs++;
   1117 		if (usbd_ratecheck(&sc->aue_rx_notice)) {
   1118 			printf("%s: %u usb errors on intr: %s\n",
   1119 			    USBDEVNAME(sc->aue_dev), sc->aue_rx_errs,
   1120 			    usbd_errstr(status));
   1121 			sc->aue_intr_errs = 0;
   1122 		}
   1123 		if (status == USBD_STALLED)
   1124 			usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
   1125 		return;
   1126 	}
   1127 
   1128 	if (p->aue_txstat0)
   1129 		ifp->if_oerrors++;
   1130 
   1131 	if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
   1132 		ifp->if_collisions++;
   1133 }
   1134 
   1135 #if defined(__FreeBSD__)
   1136 Static void
   1137 aue_rxstart(struct ifnet *ifp)
   1138 {
   1139 	struct aue_softc	*sc;
   1140 	struct aue_chain	*c;
   1141 
   1142 	sc = ifp->if_softc;
   1143 	c = &sc->aue_cdata.aue_rx_chain[sc->aue_cdata.aue_rx_prod];
   1144 
   1145 	if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
   1146 		ifp->if_ierrors++;
   1147 		return;
   1148 	}
   1149 
   1150 	/* Setup new transfer. */
   1151 	usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
   1152 	    c, mtod(c->aue_mbuf, char *), AUE_BUFSZ, USBD_SHORT_XFER_OK,
   1153 	    USBD_NO_TIMEOUT, aue_rxeof);
   1154 	usbd_transfer(c->aue_xfer);
   1155 }
   1156 #endif
   1157 
   1158 /*
   1159  * A frame has been uploaded: pass the resulting mbuf chain up to
   1160  * the higher level protocols.
   1161  */
   1162 Static void
   1163 aue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
   1164 {
   1165 	struct aue_chain	*c = priv;
   1166 	struct aue_softc	*sc = c->aue_sc;
   1167 	struct ifnet		*ifp = GET_IFP(sc);
   1168 	struct mbuf		*m;
   1169 	u_int32_t		total_len;
   1170 	struct aue_rxpkt	r;
   1171 #if defined(__NetBSD__) || defined(__OpenBSD__)
   1172 	int			s;
   1173 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1174 
   1175 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
   1176 
   1177 	if (sc->aue_dying)
   1178 		return;
   1179 
   1180 	if (!(ifp->if_flags & IFF_RUNNING))
   1181 		return;
   1182 
   1183 	if (status != USBD_NORMAL_COMPLETION) {
   1184 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   1185 			return;
   1186 		sc->aue_rx_errs++;
   1187 		if (usbd_ratecheck(&sc->aue_rx_notice)) {
   1188 			printf("%s: %u usb errors on rx: %s\n",
   1189 			    USBDEVNAME(sc->aue_dev), sc->aue_rx_errs,
   1190 			    usbd_errstr(status));
   1191 			sc->aue_rx_errs = 0;
   1192 		}
   1193 		if (status == USBD_STALLED)
   1194 			usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
   1195 		goto done;
   1196 	}
   1197 
   1198 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
   1199 
   1200 	memcpy(mtod(c->aue_mbuf, char*), c->aue_buf, total_len);
   1201 
   1202 	if (total_len <= 4 + ETHER_CRC_LEN) {
   1203 		ifp->if_ierrors++;
   1204 		goto done;
   1205 	}
   1206 
   1207 	memcpy(&r, c->aue_buf + total_len - 4, sizeof(r));
   1208 
   1209 	/* Turn off all the non-error bits in the rx status word. */
   1210 	r.aue_rxstat &= AUE_RXSTAT_MASK;
   1211 	if (r.aue_rxstat) {
   1212 		ifp->if_ierrors++;
   1213 		goto done;
   1214 	}
   1215 
   1216 	/* No errors; receive the packet. */
   1217 	m = c->aue_mbuf;
   1218 	total_len -= ETHER_CRC_LEN + 4;
   1219 	m->m_pkthdr.len = m->m_len = total_len;
   1220 	ifp->if_ipackets++;
   1221 
   1222 #if defined(__FreeBSD__)
   1223 	m->m_pkthdr.rcvif = (struct ifnet *)&kue_qdat;
   1224 	/* Put the packet on the special USB input queue. */
   1225 	usb_ether_input(m);
   1226 
   1227 	return;
   1228 
   1229 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1230 	m->m_pkthdr.rcvif = ifp;
   1231 
   1232 	s = splimp();
   1233 
   1234 	/* XXX ugly */
   1235 	if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
   1236 		ifp->if_ierrors++;
   1237 		goto done1;
   1238 	}
   1239 
   1240 #if NBPFILTER > 0
   1241 	/*
   1242 	 * Handle BPF listeners. Let the BPF user see the packet, but
   1243 	 * don't pass it up to the ether_input() layer unless it's
   1244 	 * a broadcast packet, multicast packet, matches our ethernet
   1245 	 * address or the interface is in promiscuous mode.
   1246 	 */
   1247 	if (ifp->if_bpf)
   1248 		BPF_MTAP(ifp, m);
   1249 #endif
   1250 
   1251 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->aue_dev),
   1252 		    __FUNCTION__, m->m_len));
   1253 	IF_INPUT(ifp, m);
   1254  done1:
   1255 	splx(s);
   1256 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1257 
   1258  done:
   1259 
   1260 	/* Setup new transfer. */
   1261 	usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
   1262 	    c, c->aue_buf, AUE_BUFSZ,
   1263 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
   1264 	    USBD_NO_TIMEOUT, aue_rxeof);
   1265 	usbd_transfer(xfer);
   1266 
   1267 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->aue_dev),
   1268 		    __FUNCTION__));
   1269 }
   1270 
   1271 /*
   1272  * A frame was downloaded to the chip. It's safe for us to clean up
   1273  * the list buffers.
   1274  */
   1275 
   1276 Static void
   1277 aue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
   1278 {
   1279 	struct aue_chain	*c = priv;
   1280 	struct aue_softc	*sc = c->aue_sc;
   1281 	struct ifnet		*ifp = GET_IFP(sc);
   1282 	int			s;
   1283 
   1284 	if (sc->aue_dying)
   1285 		return;
   1286 
   1287 	s = splimp();
   1288 
   1289 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->aue_dev),
   1290 		    __FUNCTION__, status));
   1291 
   1292 	ifp->if_timer = 0;
   1293 	ifp->if_flags &= ~IFF_OACTIVE;
   1294 
   1295 	if (status != USBD_NORMAL_COMPLETION) {
   1296 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1297 			splx(s);
   1298 			return;
   1299 		}
   1300 		ifp->if_oerrors++;
   1301 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->aue_dev),
   1302 		    usbd_errstr(status));
   1303 		if (status == USBD_STALLED)
   1304 			usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]);
   1305 		splx(s);
   1306 		return;
   1307 	}
   1308 
   1309 	ifp->if_opackets++;
   1310 
   1311 #if defined(__FreeBSD__)
   1312 	c->aue_mbuf->m_pkthdr.rcvif = ifp;
   1313 	usb_tx_done(c->aue_mbuf);
   1314   	c->aue_mbuf = NULL;
   1315 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1316 	m_freem(c->aue_mbuf);
   1317 	c->aue_mbuf = NULL;
   1318 
   1319 	if (ifp->if_snd.ifq_head != NULL)
   1320 		aue_start(ifp);
   1321 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1322 
   1323 	splx(s);
   1324 }
   1325 
   1326 Static void
   1327 aue_tick(void *xsc)
   1328 {
   1329 	struct aue_softc	*sc = xsc;
   1330 	struct ifnet		*ifp;
   1331 	struct mii_data		*mii;
   1332 	int			s;
   1333 
   1334 	DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
   1335 
   1336 	if (sc == NULL)
   1337 		return;
   1338 
   1339 	if (sc->aue_dying)
   1340 		return;
   1341 
   1342 	ifp = GET_IFP(sc);
   1343 	mii = GET_MII(sc);
   1344 	if (mii == NULL)
   1345 		return;
   1346 
   1347 	s = splimp();
   1348 
   1349 	mii_tick(mii);
   1350 	if (!sc->aue_link) {
   1351 		mii_pollstat(mii);
   1352 		if (mii->mii_media_status & IFM_ACTIVE &&
   1353 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
   1354 			DPRINTFN(2,("%s: %s: got link\n",
   1355 				    USBDEVNAME(sc->aue_dev),__FUNCTION__));
   1356 			sc->aue_link++;
   1357 			if (ifp->if_snd.ifq_head != NULL)
   1358 				aue_start(ifp);
   1359 		}
   1360 	}
   1361 
   1362 	usb_callout(sc->aue_stat_ch, hz, aue_tick, sc);
   1363 
   1364 	splx(s);
   1365 }
   1366 
   1367 Static int
   1368 aue_send(struct aue_softc *sc, struct mbuf *m, int idx)
   1369 {
   1370 	int			total_len;
   1371 	struct aue_chain	*c;
   1372 	usbd_status		err;
   1373 
   1374 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
   1375 
   1376 	c = &sc->aue_cdata.aue_tx_chain[idx];
   1377 
   1378 	/*
   1379 	 * Copy the mbuf data into a contiguous buffer, leaving two
   1380 	 * bytes at the beginning to hold the frame length.
   1381 	 */
   1382 	m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
   1383 	c->aue_mbuf = m;
   1384 
   1385 	/*
   1386 	 * The ADMtek documentation says that the packet length is
   1387 	 * supposed to be specified in the first two bytes of the
   1388 	 * transfer, however it actually seems to ignore this info
   1389 	 * and base the frame size on the bulk transfer length.
   1390 	 */
   1391 	c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
   1392 	c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
   1393 	total_len = m->m_pkthdr.len + 2;
   1394 
   1395 	usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
   1396 	    c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
   1397 	    AUE_TX_TIMEOUT, aue_txeof);
   1398 
   1399 	/* Transmit */
   1400 	err = usbd_transfer(c->aue_xfer);
   1401 	if (err != USBD_IN_PROGRESS) {
   1402 		printf("%s: aue_send error=%s\n", USBDEVNAME(sc->aue_dev),
   1403 		       usbd_errstr(err));
   1404 		aue_stop(sc);
   1405 		return (EIO);
   1406 	}
   1407 	DPRINTFN(5,("%s: %s: send %d bytes\n", USBDEVNAME(sc->aue_dev),
   1408 		    __FUNCTION__, total_len));
   1409 
   1410 	sc->aue_cdata.aue_tx_cnt++;
   1411 
   1412 	return (0);
   1413 }
   1414 
   1415 Static void
   1416 aue_start(struct ifnet *ifp)
   1417 {
   1418 	struct aue_softc	*sc = ifp->if_softc;
   1419 	struct mbuf		*m_head = NULL;
   1420 
   1421 	DPRINTFN(5,("%s: %s: enter, link=%d\n", USBDEVNAME(sc->aue_dev),
   1422 		    __FUNCTION__, sc->aue_link));
   1423 
   1424 	if (sc->aue_dying)
   1425 		return;
   1426 
   1427 	if (!sc->aue_link)
   1428 		return;
   1429 
   1430 	if (ifp->if_flags & IFF_OACTIVE)
   1431 		return;
   1432 
   1433 	IF_DEQUEUE(&ifp->if_snd, m_head);
   1434 	if (m_head == NULL)
   1435 		return;
   1436 
   1437 	if (aue_send(sc, m_head, 0)) {
   1438 		IF_PREPEND(&ifp->if_snd, m_head);
   1439 		ifp->if_flags |= IFF_OACTIVE;
   1440 		return;
   1441 	}
   1442 
   1443 #if NBPFILTER > 0
   1444 	/*
   1445 	 * If there's a BPF listener, bounce a copy of this frame
   1446 	 * to him.
   1447 	 */
   1448 	if (ifp->if_bpf)
   1449 		BPF_MTAP(ifp, m_head);
   1450 #endif
   1451 
   1452 	ifp->if_flags |= IFF_OACTIVE;
   1453 
   1454 	/*
   1455 	 * Set a timeout in case the chip goes out to lunch.
   1456 	 */
   1457 	ifp->if_timer = 5;
   1458 }
   1459 
   1460 Static void
   1461 aue_init(void *xsc)
   1462 {
   1463 	struct aue_softc	*sc = xsc;
   1464 	struct ifnet		*ifp = GET_IFP(sc);
   1465 	struct mii_data		*mii = GET_MII(sc);
   1466 	int			i, s;
   1467 	u_char			*eaddr;
   1468 
   1469 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1470 
   1471 	if (sc->aue_dying)
   1472 		return;
   1473 
   1474 	if (ifp->if_flags & IFF_RUNNING)
   1475 		return;
   1476 
   1477 	s = splimp();
   1478 
   1479 	/*
   1480 	 * Cancel pending I/O and free all RX/TX buffers.
   1481 	 */
   1482 	aue_reset(sc);
   1483 
   1484 #if defined(__FreeBSD__) || defined(__OpenBSD__)
   1485 	eaddr = sc->arpcom.ac_enaddr;
   1486 #elif defined(__NetBSD__)
   1487 	eaddr = LLADDR(ifp->if_sadl);
   1488 #endif /* defined(__NetBSD__) */
   1489 	for (i = 0; i < ETHER_ADDR_LEN; i++)
   1490 		aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
   1491 
   1492 	 /* If we want promiscuous mode, set the allframes bit. */
   1493 	if (ifp->if_flags & IFF_PROMISC)
   1494 		AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1495 	else
   1496 		AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1497 
   1498 	/* Init TX ring. */
   1499 	if (aue_tx_list_init(sc) == ENOBUFS) {
   1500 		printf("%s: tx list init failed\n", USBDEVNAME(sc->aue_dev));
   1501 		splx(s);
   1502 		return;
   1503 	}
   1504 
   1505 	/* Init RX ring. */
   1506 	if (aue_rx_list_init(sc) == ENOBUFS) {
   1507 		printf("%s: rx list init failed\n", USBDEVNAME(sc->aue_dev));
   1508 		splx(s);
   1509 		return;
   1510 	}
   1511 
   1512 	/* Load the multicast filter. */
   1513 	aue_setmulti(sc);
   1514 
   1515 	/* Enable RX and TX */
   1516 	aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
   1517 	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
   1518 	AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
   1519 
   1520 	mii_mediachg(mii);
   1521 
   1522 	if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
   1523 		if (aue_openpipes(sc)) {
   1524 			splx(s);
   1525 			return;
   1526 		}
   1527 	}
   1528 
   1529 	ifp->if_flags |= IFF_RUNNING;
   1530 	ifp->if_flags &= ~IFF_OACTIVE;
   1531 
   1532 	splx(s);
   1533 
   1534 	usb_callout(sc->aue_stat_ch, hz, aue_tick, sc);
   1535 }
   1536 
   1537 Static int
   1538 aue_openpipes(struct aue_softc *sc)
   1539 {
   1540 	struct aue_chain	*c;
   1541 	usbd_status		err;
   1542 	int i;
   1543 
   1544 	/* Open RX and TX pipes. */
   1545 	err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
   1546 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
   1547 	if (err) {
   1548 		printf("%s: open rx pipe failed: %s\n",
   1549 		    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1550 		return (EIO);
   1551 	}
   1552 	err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
   1553 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
   1554 	if (err) {
   1555 		printf("%s: open tx pipe failed: %s\n",
   1556 		    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1557 		return (EIO);
   1558 	}
   1559 	err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
   1560 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
   1561 	    &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
   1562 	    AUE_INTR_INTERVAL);
   1563 	if (err) {
   1564 		printf("%s: open intr pipe failed: %s\n",
   1565 		    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1566 		return (EIO);
   1567 	}
   1568 
   1569 	/* Start up the receive pipe. */
   1570 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
   1571 		c = &sc->aue_cdata.aue_rx_chain[i];
   1572 		usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
   1573 		    c, c->aue_buf, AUE_BUFSZ,
   1574 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
   1575 		    aue_rxeof);
   1576 		(void)usbd_transfer(c->aue_xfer); /* XXX */
   1577 		DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->aue_dev),
   1578 			    __FUNCTION__));
   1579 
   1580 	}
   1581 	return (0);
   1582 }
   1583 
   1584 /*
   1585  * Set media options.
   1586  */
   1587 Static int
   1588 aue_ifmedia_upd(struct ifnet *ifp)
   1589 {
   1590 	struct aue_softc	*sc = ifp->if_softc;
   1591 	struct mii_data		*mii = GET_MII(sc);
   1592 
   1593 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1594 
   1595 	if (sc->aue_dying)
   1596 		return (0);
   1597 
   1598 	sc->aue_link = 0;
   1599 	if (mii->mii_instance) {
   1600 		struct mii_softc	*miisc;
   1601 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
   1602 		    miisc = LIST_NEXT(miisc, mii_list))
   1603 			 mii_phy_reset(miisc);
   1604 	}
   1605 	mii_mediachg(mii);
   1606 
   1607 	return (0);
   1608 }
   1609 
   1610 /*
   1611  * Report current media status.
   1612  */
   1613 Static void
   1614 aue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
   1615 {
   1616 	struct aue_softc	*sc = ifp->if_softc;
   1617 	struct mii_data		*mii = GET_MII(sc);
   1618 
   1619 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1620 
   1621 	mii_pollstat(mii);
   1622 	ifmr->ifm_active = mii->mii_media_active;
   1623 	ifmr->ifm_status = mii->mii_media_status;
   1624 }
   1625 
   1626 Static int
   1627 aue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
   1628 {
   1629 	struct aue_softc	*sc = ifp->if_softc;
   1630 #if defined(__NetBSD__) || defined(__OpenBSD__)
   1631 	struct ifaddr 		*ifa = (struct ifaddr *)data;
   1632 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1633 	struct ifreq		*ifr = (struct ifreq *)data;
   1634 	struct mii_data		*mii;
   1635 	int			s, error = 0;
   1636 
   1637 	if (sc->aue_dying)
   1638 		return (EIO);
   1639 
   1640 	s = splimp();
   1641 
   1642 	switch(command) {
   1643 #if defined(__FreeBSD__)
   1644 	case SIOCSIFADDR:
   1645 	case SIOCGIFADDR:
   1646 	case SIOCSIFMTU:
   1647 		error = ether_ioctl(ifp, command, data);
   1648 		break;
   1649 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1650 	case SIOCSIFADDR:
   1651 		ifp->if_flags |= IFF_UP;
   1652 		aue_init(sc);
   1653 
   1654 		switch (ifa->ifa_addr->sa_family) {
   1655 #ifdef INET
   1656 		case AF_INET:
   1657 #if defined(__NetBSD__)
   1658 			arp_ifinit(ifp, ifa);
   1659 #else
   1660 			arp_ifinit(&sc->arpcom, ifa);
   1661 #endif
   1662 			break;
   1663 #endif /* INET */
   1664 #ifdef NS
   1665 		case AF_NS:
   1666 		    {
   1667 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1668 
   1669 			if (ns_nullhost(*ina))
   1670 				ina->x_host = *(union ns_host *)
   1671 					LLADDR(ifp->if_sadl);
   1672 			else
   1673 				memcpy(LLADDR(ifp->if_sadl),
   1674 				       ina->x_host.c_host,
   1675 				       ifp->if_addrlen);
   1676 			break;
   1677 		    }
   1678 #endif /* NS */
   1679 		}
   1680 		break;
   1681 
   1682 	case SIOCSIFMTU:
   1683 		if (ifr->ifr_mtu > ETHERMTU)
   1684 			error = EINVAL;
   1685 		else
   1686 			ifp->if_mtu = ifr->ifr_mtu;
   1687 		break;
   1688 
   1689 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1690 	case SIOCSIFFLAGS:
   1691 		if (ifp->if_flags & IFF_UP) {
   1692 			if (ifp->if_flags & IFF_RUNNING &&
   1693 			    ifp->if_flags & IFF_PROMISC &&
   1694 			    !(sc->aue_if_flags & IFF_PROMISC)) {
   1695 				AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1696 			} else if (ifp->if_flags & IFF_RUNNING &&
   1697 			    !(ifp->if_flags & IFF_PROMISC) &&
   1698 			    sc->aue_if_flags & IFF_PROMISC) {
   1699 				AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1700 			} else if (!(ifp->if_flags & IFF_RUNNING))
   1701 				aue_init(sc);
   1702 		} else {
   1703 			if (ifp->if_flags & IFF_RUNNING)
   1704 				aue_stop(sc);
   1705 		}
   1706 		sc->aue_if_flags = ifp->if_flags;
   1707 		error = 0;
   1708 		break;
   1709 	case SIOCADDMULTI:
   1710 	case SIOCDELMULTI:
   1711 #if defined(__NetBSD__) || defined(__OpenBSD__)
   1712 		error = (command == SIOCADDMULTI) ?
   1713 			ether_addmulti(ifr, &sc->aue_ec) :
   1714 			ether_delmulti(ifr, &sc->aue_ec);
   1715 		if (error == ENETRESET) {
   1716 			aue_init(sc);
   1717 		}
   1718 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1719 		aue_setmulti(sc);
   1720 		error = 0;
   1721 		break;
   1722 	case SIOCGIFMEDIA:
   1723 	case SIOCSIFMEDIA:
   1724 		mii = GET_MII(sc);
   1725 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
   1726 		break;
   1727 	default:
   1728 		error = EINVAL;
   1729 		break;
   1730 	}
   1731 
   1732 	splx(s);
   1733 
   1734 	return (error);
   1735 }
   1736 
   1737 Static void
   1738 aue_watchdog(struct ifnet *ifp)
   1739 {
   1740 	struct aue_softc	*sc = ifp->if_softc;
   1741 
   1742 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1743 
   1744 	ifp->if_oerrors++;
   1745 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->aue_dev));
   1746 
   1747 	/*
   1748 	 * The polling business is a kludge to avoid allowing the
   1749 	 * USB code to call tsleep() in usbd_delay_ms(), which will
   1750 	 * kill us since the watchdog routine is invoked from
   1751 	 * interrupt context.
   1752 	 */
   1753 	usbd_set_polling(sc->aue_udev, 1);
   1754 	aue_stop(sc);
   1755 	aue_init(sc);
   1756 	usbd_set_polling(sc->aue_udev, 0);
   1757 
   1758 	if (ifp->if_snd.ifq_head != NULL)
   1759 		aue_start(ifp);
   1760 }
   1761 
   1762 /*
   1763  * Stop the adapter and free any mbufs allocated to the
   1764  * RX and TX lists.
   1765  */
   1766 Static void
   1767 aue_stop(struct aue_softc *sc)
   1768 {
   1769 	usbd_status		err;
   1770 	struct ifnet		*ifp;
   1771 	int			i;
   1772 
   1773 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1774 
   1775 	ifp = GET_IFP(sc);
   1776 	ifp->if_timer = 0;
   1777 
   1778 	aue_csr_write_1(sc, AUE_CTL0, 0);
   1779 	aue_csr_write_1(sc, AUE_CTL1, 0);
   1780 	aue_reset(sc);
   1781 	usb_uncallout(sc->aue_stat_ch, aue_tick, sc);
   1782 
   1783 	/* Stop transfers. */
   1784 	if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
   1785 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
   1786 		if (err) {
   1787 			printf("%s: abort rx pipe failed: %s\n",
   1788 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1789 		}
   1790 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
   1791 		if (err) {
   1792 			printf("%s: close rx pipe failed: %s\n",
   1793 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1794 		}
   1795 		sc->aue_ep[AUE_ENDPT_RX] = NULL;
   1796 	}
   1797 
   1798 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
   1799 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
   1800 		if (err) {
   1801 			printf("%s: abort tx pipe failed: %s\n",
   1802 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1803 		}
   1804 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
   1805 		if (err) {
   1806 			printf("%s: close tx pipe failed: %s\n",
   1807 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1808 		}
   1809 		sc->aue_ep[AUE_ENDPT_TX] = NULL;
   1810 	}
   1811 
   1812 	if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
   1813 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
   1814 		if (err) {
   1815 			printf("%s: abort intr pipe failed: %s\n",
   1816 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1817 		}
   1818 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
   1819 		if (err) {
   1820 			printf("%s: close intr pipe failed: %s\n",
   1821 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1822 		}
   1823 		sc->aue_ep[AUE_ENDPT_INTR] = NULL;
   1824 	}
   1825 
   1826 	/* Free RX resources. */
   1827 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
   1828 		if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
   1829 			m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
   1830 			sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
   1831 		}
   1832 		if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
   1833 			usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
   1834 			sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
   1835 		}
   1836 	}
   1837 
   1838 	/* Free TX resources. */
   1839 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
   1840 		if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
   1841 			m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
   1842 			sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
   1843 		}
   1844 		if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
   1845 			usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
   1846 			sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
   1847 		}
   1848 	}
   1849 
   1850 	sc->aue_link = 0;
   1851 
   1852 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1853 }
   1854 
   1855 #ifdef __FreeBSD__
   1856 /*
   1857  * Stop all chip I/O so that the kernel's probe routines don't
   1858  * get confused by errant DMAs when rebooting.
   1859  */
   1860 Static void
   1861 aue_shutdown(device_ptr_t dev)
   1862 {
   1863 	struct aue_softc	*sc = USBGETSOFTC(dev);
   1864 
   1865 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1866 
   1867 	aue_reset(sc);
   1868 	aue_stop(sc);
   1869 }
   1870 #endif
   1871