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