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