Home | History | Annotate | Line # | Download | only in usb
if_aue.c revision 1.132.2.1
      1 /*	$NetBSD: if_aue.c,v 1.132.2.1 2017/04/05 19:54:19 snj Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997, 1998, 1999, 2000
      5  *	Bill Paul <wpaul (at) ee.columbia.edu>.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Bill Paul.
     18  * 4. Neither the name of the author nor the names of any co-contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32  * THE POSSIBILITY OF SUCH DAMAGE.
     33  *
     34  * $FreeBSD: src/sys/dev/usb/if_aue.c,v 1.11 2000/01/14 01:36:14 wpaul Exp $
     35  */
     36 
     37 /*
     38  * ADMtek AN986 Pegasus and AN8511 Pegasus II USB to ethernet driver.
     39  * Datasheet is available from http://www.admtek.com.tw.
     40  *
     41  * Written by Bill Paul <wpaul (at) ee.columbia.edu>
     42  * Electrical Engineering Department
     43  * Columbia University, New York City
     44  */
     45 
     46 /*
     47  * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet
     48  * support: the control endpoint for reading/writing registers, burst
     49  * read endpoint for packet reception, burst write for packet transmission
     50  * and one for "interrupts." The chip uses the same RX filter scheme
     51  * as the other ADMtek ethernet parts: one perfect filter entry for the
     52  * the station address and a 64-bit multicast hash table. The chip supports
     53  * both MII and HomePNA attachments.
     54  *
     55  * Since the maximum data transfer speed of USB is supposed to be 12Mbps,
     56  * you're never really going to get 100Mbps speeds from this device. I
     57  * think the idea is to allow the device to connect to 10 or 100Mbps
     58  * networks, not necessarily to provide 100Mbps performance. Also, since
     59  * the controller uses an external PHY chip, it's possible that board
     60  * designers might simply choose a 10Mbps PHY.
     61  *
     62  * Registers are accessed using usbd_do_request(). Packet transfers are
     63  * done using usbd_transfer() and friends.
     64  */
     65 
     66 /*
     67  * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
     68  */
     69 
     70 /*
     71  * TODO:
     72  * better error messages from rxstat
     73  * split out if_auevar.h
     74  * more error checks
     75  * investigate short rx problem
     76  * proper cleanup on errors
     77  */
     78 
     79 #include <sys/cdefs.h>
     80 __KERNEL_RCSID(0, "$NetBSD: if_aue.c,v 1.132.2.1 2017/04/05 19:54:19 snj Exp $");
     81 
     82 #ifdef _KERNEL_OPT
     83 #include "opt_usb.h"
     84 #include "opt_inet.h"
     85 #endif
     86 
     87 #include <sys/param.h>
     88 #include <sys/systm.h>
     89 #include <sys/sockio.h>
     90 #include <sys/mutex.h>
     91 #include <sys/mbuf.h>
     92 #include <sys/kernel.h>
     93 #include <sys/socket.h>
     94 #include <sys/device.h>
     95 #include <sys/rnd.h>
     96 
     97 #include <net/if.h>
     98 #include <net/if_arp.h>
     99 #include <net/if_dl.h>
    100 #include <net/if_media.h>
    101 
    102 #include <net/bpf.h>
    103 
    104 #include <net/if_ether.h>
    105 #ifdef INET
    106 #include <netinet/in.h>
    107 #include <netinet/if_inarp.h>
    108 #endif
    109 
    110 
    111 
    112 #include <dev/mii/mii.h>
    113 #include <dev/mii/miivar.h>
    114 
    115 #include <dev/usb/usb.h>
    116 #include <dev/usb/usbdi.h>
    117 #include <dev/usb/usbdi_util.h>
    118 #include <dev/usb/usbdevs.h>
    119 
    120 #include <sys/condvar.h>
    121 #include <sys/kthread.h>
    122 
    123 #include <dev/usb/if_auereg.h>
    124 
    125 #ifdef AUE_DEBUG
    126 #define DPRINTF(x)	if (auedebug) printf x
    127 #define DPRINTFN(n,x)	if (auedebug >= (n)) printf x
    128 int	auedebug = 0;
    129 #else
    130 #define DPRINTF(x)
    131 #define DPRINTFN(n,x)
    132 #endif
    133 
    134 /*
    135  * Various supported device vendors/products.
    136  */
    137 struct aue_type {
    138 	struct usb_devno	aue_dev;
    139 	uint16_t		aue_flags;
    140 #define LSYS	0x0001		/* use Linksys reset */
    141 #define PNA	0x0002		/* has Home PNA */
    142 #define PII	0x0004		/* Pegasus II chip */
    143 };
    144 
    145 Static const struct aue_type aue_devs[] = {
    146  {{ USB_VENDOR_3COM,		USB_PRODUCT_3COM_3C460B},	  PII },
    147  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX1},	  PNA|PII },
    148  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX2},	  PII },
    149  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_UFE1000},	  LSYS },
    150  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX4},	  PNA },
    151  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX5},	  PNA },
    152  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX6},	  PII },
    153  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX7},	  PII },
    154  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX8},	  PII },
    155  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX9},	  PNA },
    156  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX10},	  0 },
    157  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_DSB650TX_PNA}, 0 },
    158  {{ USB_VENDOR_ACCTON,		USB_PRODUCT_ACCTON_USB320_EC},	  0 },
    159  {{ USB_VENDOR_ACCTON,		USB_PRODUCT_ACCTON_SS1001},	  PII },
    160  {{ USB_VENDOR_ADMTEK,		USB_PRODUCT_ADMTEK_PEGASUS},	  PNA },
    161  {{ USB_VENDOR_ADMTEK,		USB_PRODUCT_ADMTEK_PEGASUSII},	  PII },
    162  {{ USB_VENDOR_ADMTEK,		USB_PRODUCT_ADMTEK_PEGASUSII_2},  PII },
    163  {{ USB_VENDOR_ADMTEK,		USB_PRODUCT_ADMTEK_PEGASUSII_3},  PII },
    164  {{ USB_VENDOR_AEI,		USB_PRODUCT_AEI_USBTOLAN},	  PII },
    165  {{ USB_VENDOR_BELKIN,		USB_PRODUCT_BELKIN_USB2LAN},	  PII },
    166  {{ USB_VENDOR_BILLIONTON,	USB_PRODUCT_BILLIONTON_USB100},	  0 },
    167  {{ USB_VENDOR_BILLIONTON,	USB_PRODUCT_BILLIONTON_USBLP100}, PNA },
    168  {{ USB_VENDOR_BILLIONTON,	USB_PRODUCT_BILLIONTON_USBEL100}, 0 },
    169  {{ USB_VENDOR_BILLIONTON,	USB_PRODUCT_BILLIONTON_USBE100},  PII },
    170  {{ USB_VENDOR_COMPAQ,		USB_PRODUCT_COMPAQ_HNE200},       PII },
    171  {{ USB_VENDOR_COREGA,		USB_PRODUCT_COREGA_FETHER_USB_TX}, 0 },
    172  {{ USB_VENDOR_COREGA,		USB_PRODUCT_COREGA_FETHER_USB_TXS},PII },
    173  {{ USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DSB650TX4},	  LSYS|PII },
    174  {{ USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DSB650TX1},	  LSYS },
    175  {{ USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DSB650TX},	  LSYS },
    176  {{ USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DSB650TX_PNA},  PNA },
    177  {{ USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DSB650TX3},	  LSYS|PII },
    178  {{ USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DSB650TX2},	  LSYS|PII },
    179  {{ USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DSB650},	  0 },
    180  {{ USB_VENDOR_ELECOM,		USB_PRODUCT_ELECOM_LDUSBTX0},	  0 },
    181  {{ USB_VENDOR_ELECOM,		USB_PRODUCT_ELECOM_LDUSBTX1},	  LSYS },
    182  {{ USB_VENDOR_ELECOM,		USB_PRODUCT_ELECOM_LDUSBTX2},	  0 },
    183  {{ USB_VENDOR_ELECOM,		USB_PRODUCT_ELECOM_LDUSBTX3},	  LSYS },
    184  {{ USB_VENDOR_ELECOM,		USB_PRODUCT_ELECOM_LDUSBLTX},	  PII },
    185  {{ USB_VENDOR_ELSA,		USB_PRODUCT_ELSA_USB2ETHERNET},	  0 },
    186  {{ USB_VENDOR_HAWKING,		USB_PRODUCT_HAWKING_UF100},	  PII },
    187  {{ USB_VENDOR_HP,		USB_PRODUCT_HP_HN210E},		  PII },
    188  {{ USB_VENDOR_IODATA,		USB_PRODUCT_IODATA_USBETTX},	  0 },
    189  {{ USB_VENDOR_IODATA,		USB_PRODUCT_IODATA_USBETTXS},	  PII },
    190  {{ USB_VENDOR_IODATA,		USB_PRODUCT_IODATA_ETXUS2},	  PII },
    191  {{ USB_VENDOR_KINGSTON,	USB_PRODUCT_KINGSTON_KNU101TX},   0 },
    192  {{ USB_VENDOR_LINKSYS,		USB_PRODUCT_LINKSYS_USB10TX1},	  LSYS|PII },
    193  {{ USB_VENDOR_LINKSYS,		USB_PRODUCT_LINKSYS_USB10T},	  LSYS },
    194  {{ USB_VENDOR_LINKSYS,		USB_PRODUCT_LINKSYS_USB100TX},	  LSYS },
    195  {{ USB_VENDOR_LINKSYS,		USB_PRODUCT_LINKSYS_USB100H1},	  LSYS|PNA },
    196  {{ USB_VENDOR_LINKSYS,		USB_PRODUCT_LINKSYS_USB10TA},	  LSYS },
    197  {{ USB_VENDOR_LINKSYS,		USB_PRODUCT_LINKSYS_USB10TX2},	  LSYS|PII },
    198  {{ USB_VENDOR_MELCO, 		USB_PRODUCT_MELCO_LUATX1}, 	  0 },
    199  {{ USB_VENDOR_MELCO, 		USB_PRODUCT_MELCO_LUATX5}, 	  0 },
    200  {{ USB_VENDOR_MELCO, 		USB_PRODUCT_MELCO_LUA2TX5}, 	  PII },
    201  {{ USB_VENDOR_MICROSOFT,	USB_PRODUCT_MICROSOFT_MN110},	  PII },
    202  {{ USB_VENDOR_NETGEAR,         USB_PRODUCT_NETGEAR_FA101},       PII },
    203  {{ USB_VENDOR_SIEMENS,		USB_PRODUCT_SIEMENS_SPEEDSTREAM}, PII },
    204  {{ USB_VENDOR_SMARTBRIDGES,	USB_PRODUCT_SMARTBRIDGES_SMARTNIC},PII },
    205  {{ USB_VENDOR_SMC,		USB_PRODUCT_SMC_2202USB},	  0 },
    206  {{ USB_VENDOR_SMC,		USB_PRODUCT_SMC_2206USB},	  PII },
    207  {{ USB_VENDOR_SOHOWARE,	USB_PRODUCT_SOHOWARE_NUB100},	  0 },
    208 };
    209 #define aue_lookup(v, p) ((const struct aue_type *)usb_lookup(aue_devs, v, p))
    210 
    211 int aue_match(device_t, cfdata_t, void *);
    212 void aue_attach(device_t, device_t, void *);
    213 int aue_detach(device_t, int);
    214 int aue_activate(device_t, enum devact);
    215 extern struct cfdriver aue_cd;
    216 CFATTACH_DECL_NEW(aue, sizeof(struct aue_softc), aue_match, aue_attach,
    217     aue_detach, aue_activate);
    218 
    219 Static void aue_reset_pegasus_II(struct aue_softc *);
    220 Static int aue_tx_list_init(struct aue_softc *);
    221 Static void aue_tx_list_free(struct aue_softc *);
    222 Static int aue_rx_list_init(struct aue_softc *);
    223 Static void aue_rx_list_free(struct aue_softc *);
    224 Static int aue_newbuf(struct aue_softc *, struct aue_chain *, struct mbuf *);
    225 Static int aue_send(struct aue_softc *, struct mbuf *, int);
    226 Static void aue_intr(struct usbd_xfer *, void *, usbd_status);
    227 Static void aue_rxeof(struct usbd_xfer *, void *, usbd_status);
    228 Static void aue_txeof(struct usbd_xfer *, void *, usbd_status);
    229 Static void aue_tick(void *);
    230 Static void aue_tick_task(void *);
    231 Static void aue_start(struct ifnet *);
    232 Static void aue_start_locked(struct ifnet *);
    233 Static int aue_ioctl(struct ifnet *, u_long, void *);
    234 Static int aue_ifflags_cb(struct ethercom *);
    235 Static int aue_init(struct ifnet *);
    236 Static int aue_init_locked(struct ifnet *);
    237 Static void aue_stop(struct ifnet *, int);
    238 Static void aue_stop_locked(struct ifnet *, int);
    239 Static void aue_watchdog(struct ifnet *);
    240 Static int aue_ifmedia_upd(struct ifnet *);
    241 
    242 Static int aue_eeprom_getword(struct aue_softc *, int);
    243 Static void aue_read_mac(struct aue_softc *, u_char *);
    244 Static int aue_miibus_readreg(device_t, int, int);
    245 Static void aue_miibus_writereg(device_t, int, int, int);
    246 Static void aue_miibus_statchg(struct ifnet *);
    247 
    248 Static void aue_lock_mii(struct aue_softc *);
    249 Static void aue_unlock_mii(struct aue_softc *);
    250 
    251 Static void aue_setmulti(struct aue_softc *);
    252 Static uint32_t aue_crc(void *);
    253 Static void aue_reset(struct aue_softc *);
    254 
    255 Static int aue_csr_read_1(struct aue_softc *, int);
    256 Static int aue_csr_write_1(struct aue_softc *, int, int);
    257 Static int aue_csr_read_2(struct aue_softc *, int);
    258 Static int aue_csr_write_2(struct aue_softc *, int, int);
    259 
    260 #define AUE_SETBIT(sc, reg, x)				\
    261 	aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x))
    262 
    263 #define AUE_CLRBIT(sc, reg, x)				\
    264 	aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x))
    265 
    266 Static int
    267 aue_csr_read_1(struct aue_softc *sc, int reg)
    268 {
    269 	usb_device_request_t	req;
    270 	usbd_status		err;
    271 	uByte			val = 0;
    272 
    273 	if (sc->aue_dying)
    274 		return 0;
    275 
    276 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    277 	req.bRequest = AUE_UR_READREG;
    278 	USETW(req.wValue, 0);
    279 	USETW(req.wIndex, reg);
    280 	USETW(req.wLength, 1);
    281 
    282 	err = usbd_do_request(sc->aue_udev, &req, &val);
    283 
    284 	if (err) {
    285 		DPRINTF(("%s: aue_csr_read_1: reg=0x%x err=%s\n",
    286 		    device_xname(sc->aue_dev), reg, usbd_errstr(err)));
    287 		return 0;
    288 	}
    289 
    290 	return val;
    291 }
    292 
    293 Static int
    294 aue_csr_read_2(struct aue_softc *sc, int reg)
    295 {
    296 	usb_device_request_t	req;
    297 	usbd_status		err;
    298 	uWord			val;
    299 
    300 	if (sc->aue_dying)
    301 		return 0;
    302 
    303 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    304 	req.bRequest = AUE_UR_READREG;
    305 	USETW(req.wValue, 0);
    306 	USETW(req.wIndex, reg);
    307 	USETW(req.wLength, 2);
    308 
    309 	err = usbd_do_request(sc->aue_udev, &req, &val);
    310 
    311 	if (err) {
    312 		DPRINTF(("%s: aue_csr_read_2: reg=0x%x err=%s\n",
    313 		    device_xname(sc->aue_dev), reg, usbd_errstr(err)));
    314 		return 0;
    315 	}
    316 
    317 	return UGETW(val);
    318 }
    319 
    320 Static int
    321 aue_csr_write_1(struct aue_softc *sc, int reg, int aval)
    322 {
    323 	usb_device_request_t	req;
    324 	usbd_status		err;
    325 	uByte			val;
    326 
    327 	if (sc->aue_dying)
    328 		return 0;
    329 
    330 	val = aval;
    331 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    332 	req.bRequest = AUE_UR_WRITEREG;
    333 	USETW(req.wValue, val);
    334 	USETW(req.wIndex, reg);
    335 	USETW(req.wLength, 1);
    336 
    337 	err = usbd_do_request(sc->aue_udev, &req, &val);
    338 
    339 	if (err) {
    340 		DPRINTF(("%s: aue_csr_write_1: reg=0x%x err=%s\n",
    341 		    device_xname(sc->aue_dev), reg, usbd_errstr(err)));
    342 		return -1;
    343 	}
    344 
    345 	return 0;
    346 }
    347 
    348 Static int
    349 aue_csr_write_2(struct aue_softc *sc, int reg, int aval)
    350 {
    351 	usb_device_request_t	req;
    352 	usbd_status		err;
    353 	uWord			val;
    354 
    355 	if (sc->aue_dying)
    356 		return 0;
    357 
    358 	USETW(val, aval);
    359 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    360 	req.bRequest = AUE_UR_WRITEREG;
    361 	USETW(req.wValue, aval);
    362 	USETW(req.wIndex, reg);
    363 	USETW(req.wLength, 2);
    364 
    365 	err = usbd_do_request(sc->aue_udev, &req, &val);
    366 
    367 	if (err) {
    368 		DPRINTF(("%s: aue_csr_write_2: reg=0x%x err=%s\n",
    369 		    device_xname(sc->aue_dev), reg, usbd_errstr(err)));
    370 		return -1;
    371 	}
    372 
    373 	return 0;
    374 }
    375 
    376 /*
    377  * Read a word of data stored in the EEPROM at address 'addr.'
    378  */
    379 Static int
    380 aue_eeprom_getword(struct aue_softc *sc, int addr)
    381 {
    382 	int		i;
    383 
    384 	aue_csr_write_1(sc, AUE_EE_REG, addr);
    385 	aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
    386 
    387 	for (i = 0; i < AUE_TIMEOUT; i++) {
    388 		if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
    389 			break;
    390 	}
    391 
    392 	if (i == AUE_TIMEOUT) {
    393 		printf("%s: EEPROM read timed out\n",
    394 		    device_xname(sc->aue_dev));
    395 	}
    396 
    397 	return aue_csr_read_2(sc, AUE_EE_DATA);
    398 }
    399 
    400 /*
    401  * Read the MAC from the EEPROM.  It's at offset 0.
    402  */
    403 Static void
    404 aue_read_mac(struct aue_softc *sc, u_char *dest)
    405 {
    406 	int			i;
    407 	int			off = 0;
    408 	int			word;
    409 
    410 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
    411 
    412 	for (i = 0; i < 3; i++) {
    413 		word = aue_eeprom_getword(sc, off + i);
    414 		dest[2 * i] = (u_char)word;
    415 		dest[2 * i + 1] = (u_char)(word >> 8);
    416 	}
    417 }
    418 
    419 /* Get exclusive access to the MII registers */
    420 Static void
    421 aue_lock_mii(struct aue_softc *sc)
    422 {
    423 	sc->aue_refcnt++;
    424 	mutex_enter(&sc->aue_mii_lock);
    425 }
    426 
    427 Static void
    428 aue_unlock_mii(struct aue_softc *sc)
    429 {
    430 	mutex_exit(&sc->aue_mii_lock);
    431 	if (--sc->aue_refcnt < 0)
    432 		usb_detach_wakeupold(sc->aue_dev);
    433 }
    434 
    435 Static int
    436 aue_miibus_readreg(device_t dev, int phy, int reg)
    437 {
    438 	struct aue_softc *sc = device_private(dev);
    439 	int			i;
    440 	uint16_t		val;
    441 
    442 	if (sc->aue_dying) {
    443 #ifdef DIAGNOSTIC
    444 		printf("%s: dying\n", device_xname(sc->aue_dev));
    445 #endif
    446 		return 0;
    447 	}
    448 
    449 #if 0
    450 	/*
    451 	 * The Am79C901 HomePNA PHY actually contains
    452 	 * two transceivers: a 1Mbps HomePNA PHY and a
    453 	 * 10Mbps full/half duplex ethernet PHY with
    454 	 * NWAY autoneg. However in the ADMtek adapter,
    455 	 * only the 1Mbps PHY is actually connected to
    456 	 * anything, so we ignore the 10Mbps one. It
    457 	 * happens to be configured for MII address 3,
    458 	 * so we filter that out.
    459 	 */
    460 	if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
    461 	    sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
    462 		if (phy == 3)
    463 			return 0;
    464 	}
    465 #endif
    466 
    467 	aue_lock_mii(sc);
    468 	aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
    469 	aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
    470 
    471 	for (i = 0; i < AUE_TIMEOUT; i++) {
    472 		if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
    473 			break;
    474 	}
    475 
    476 	if (i == AUE_TIMEOUT) {
    477 		printf("%s: MII read timed out\n", device_xname(sc->aue_dev));
    478 	}
    479 
    480 	val = aue_csr_read_2(sc, AUE_PHY_DATA);
    481 
    482 	DPRINTFN(11,("%s: %s: phy=%d reg=%d => 0x%04x\n",
    483 	    device_xname(sc->aue_dev), __func__, phy, reg, val));
    484 
    485 	aue_unlock_mii(sc);
    486 	return val;
    487 }
    488 
    489 Static void
    490 aue_miibus_writereg(device_t dev, int phy, int reg, int data)
    491 {
    492 	struct aue_softc *sc = device_private(dev);
    493 	int			i;
    494 
    495 #if 0
    496 	if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
    497 	    sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
    498 		if (phy == 3)
    499 			return;
    500 	}
    501 #endif
    502 
    503 	DPRINTFN(11,("%s: %s: phy=%d reg=%d data=0x%04x\n",
    504 	    device_xname(sc->aue_dev), __func__, phy, reg, data));
    505 
    506 	aue_lock_mii(sc);
    507 	aue_csr_write_2(sc, AUE_PHY_DATA, data);
    508 	aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
    509 	aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
    510 
    511 	for (i = 0; i < AUE_TIMEOUT; i++) {
    512 		if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
    513 			break;
    514 	}
    515 
    516 	if (i == AUE_TIMEOUT) {
    517 		printf("%s: MII read timed out\n", device_xname(sc->aue_dev));
    518 	}
    519 	aue_unlock_mii(sc);
    520 }
    521 
    522 Static void
    523 aue_miibus_statchg(struct ifnet *ifp)
    524 {
    525 	struct aue_softc *sc = ifp->if_softc;
    526 	struct mii_data	*mii = GET_MII(sc);
    527 
    528 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
    529 
    530 	aue_lock_mii(sc);
    531 	AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
    532 
    533 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
    534 		AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
    535 	} else {
    536 		AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
    537 	}
    538 
    539 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
    540 		AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
    541 	else
    542 		AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
    543 
    544 	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
    545 	aue_unlock_mii(sc);
    546 
    547 	/*
    548 	 * Set the LED modes on the LinkSys adapter.
    549 	 * This turns on the 'dual link LED' bin in the auxmode
    550 	 * register of the Broadcom PHY.
    551 	 */
    552 	if (!sc->aue_dying && (sc->aue_flags & LSYS)) {
    553 		uint16_t auxmode;
    554 		auxmode = aue_miibus_readreg(sc->aue_dev, 0, 0x1b);
    555 		aue_miibus_writereg(sc->aue_dev, 0, 0x1b, auxmode | 0x04);
    556 	}
    557 	DPRINTFN(5,("%s: %s: exit\n", device_xname(sc->aue_dev), __func__));
    558 }
    559 
    560 #define AUE_POLY	0xEDB88320
    561 #define AUE_BITS	6
    562 
    563 Static uint32_t
    564 aue_crc(void *addrv)
    565 {
    566 	uint32_t		idx, bit, data, crc;
    567 	char *addr = addrv;
    568 
    569 	/* Compute CRC for the address value. */
    570 	crc = 0xFFFFFFFF; /* initial value */
    571 
    572 	for (idx = 0; idx < 6; idx++) {
    573 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
    574 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0);
    575 	}
    576 
    577 	return crc & ((1 << AUE_BITS) - 1);
    578 }
    579 
    580 Static void
    581 aue_setmulti(struct aue_softc *sc)
    582 {
    583 	struct ifnet		*ifp;
    584 	struct ether_multi	*enm;
    585 	struct ether_multistep	step;
    586 	uint32_t		h = 0, i;
    587 
    588 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
    589 
    590 	ifp = GET_IFP(sc);
    591 
    592 	if (ifp->if_flags & IFF_PROMISC) {
    593 allmulti:
    594 		ifp->if_flags |= IFF_ALLMULTI;
    595 		AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
    596 		return;
    597 	}
    598 
    599 	AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
    600 
    601 	/* first, zot all the existing hash bits */
    602 	for (i = 0; i < 8; i++)
    603 		aue_csr_write_1(sc, AUE_MAR0 + i, 0);
    604 
    605 	/* now program new ones */
    606 	ETHER_FIRST_MULTI(step, &sc->aue_ec, enm);
    607 	while (enm != NULL) {
    608 		if (memcmp(enm->enm_addrlo,
    609 		    enm->enm_addrhi, ETHER_ADDR_LEN) != 0)
    610 			goto allmulti;
    611 
    612 		h = aue_crc(enm->enm_addrlo);
    613 		AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0x7));
    614 		ETHER_NEXT_MULTI(step, enm);
    615 	}
    616 
    617 	ifp->if_flags &= ~IFF_ALLMULTI;
    618 }
    619 
    620 Static void
    621 aue_reset_pegasus_II(struct aue_softc *sc)
    622 {
    623 	/* Magic constants taken from Linux driver. */
    624 	aue_csr_write_1(sc, AUE_REG_1D, 0);
    625 	aue_csr_write_1(sc, AUE_REG_7B, 2);
    626 #if 0
    627 	if ((sc->aue_flags & HAS_HOME_PNA) && mii_mode)
    628 		aue_csr_write_1(sc, AUE_REG_81, 6);
    629 	else
    630 #endif
    631 		aue_csr_write_1(sc, AUE_REG_81, 2);
    632 }
    633 
    634 Static void
    635 aue_reset(struct aue_softc *sc)
    636 {
    637 	int		i;
    638 
    639 	DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
    640 
    641 	AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
    642 
    643 	for (i = 0; i < AUE_TIMEOUT; i++) {
    644 		if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
    645 			break;
    646 	}
    647 
    648 	if (i == AUE_TIMEOUT)
    649 		printf("%s: reset failed\n", device_xname(sc->aue_dev));
    650 
    651 #if 0
    652 	/* XXX what is mii_mode supposed to be */
    653 	if (sc->aue_mii_mode && (sc->aue_flags & PNA))
    654 		aue_csr_write_1(sc, AUE_GPIO1, 0x34);
    655 	else
    656 		aue_csr_write_1(sc, AUE_GPIO1, 0x26);
    657 #endif
    658 
    659 	/*
    660 	 * The PHY(s) attached to the Pegasus chip may be held
    661 	 * in reset until we flip on the GPIO outputs. Make sure
    662 	 * to set the GPIO pins high so that the PHY(s) will
    663 	 * be enabled.
    664 	 *
    665 	 * Note: We force all of the GPIO pins low first, *then*
    666 	 * enable the ones we want.
    667 	 */
    668 	if (sc->aue_flags & LSYS) {
    669 		/* Grrr. LinkSys has to be different from everyone else. */
    670 		aue_csr_write_1(sc, AUE_GPIO0,
    671 		    AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
    672 	} else {
    673 		aue_csr_write_1(sc, AUE_GPIO0,
    674 		    AUE_GPIO_OUT0 | AUE_GPIO_SEL0);
    675 	}
    676 	aue_csr_write_1(sc, AUE_GPIO0,
    677 	    AUE_GPIO_OUT0 | AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
    678 
    679 	if (sc->aue_flags & PII)
    680 		aue_reset_pegasus_II(sc);
    681 
    682 	/* Wait a little while for the chip to get its brains in order. */
    683 	delay(10000);		/* XXX */
    684 }
    685 
    686 /*
    687  * Probe for a Pegasus chip.
    688  */
    689 int
    690 aue_match(device_t parent, cfdata_t match, void *aux)
    691 {
    692 	struct usb_attach_arg *uaa = aux;
    693 
    694 	/*
    695 	 * Some manufacturers use the same vendor and product id for
    696 	 * different devices. We need to sanity check the DeviceClass
    697 	 * in this case
    698 	 * Currently known guilty products:
    699 	 * 0x050d/0x0121 Belkin Bluetooth and USB2LAN
    700 	 *
    701 	 * If this turns out to be more common, we could use a quirk
    702 	 * table.
    703 	 */
    704 	if (uaa->uaa_vendor == USB_VENDOR_BELKIN &&
    705 		uaa->uaa_product == USB_PRODUCT_BELKIN_USB2LAN) {
    706 		usb_device_descriptor_t *dd;
    707 
    708 		dd = usbd_get_device_descriptor(uaa->uaa_device);
    709 		if (dd != NULL &&
    710 			dd->bDeviceClass != UDCLASS_IN_INTERFACE)
    711 			return UMATCH_NONE;
    712 	}
    713 
    714 	return aue_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    715 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    716 }
    717 
    718 /*
    719  * Attach the interface. Allocate softc structures, do ifmedia
    720  * setup and ethernet/BPF attach.
    721  */
    722 void
    723 aue_attach(device_t parent, device_t self, void *aux)
    724 {
    725 	struct aue_softc *sc = device_private(self);
    726 	struct usb_attach_arg *uaa = aux;
    727 	char			*devinfop;
    728 	u_char			eaddr[ETHER_ADDR_LEN];
    729 	struct ifnet		*ifp;
    730 	struct mii_data		*mii;
    731 	struct usbd_device	*dev = uaa->uaa_device;
    732 	struct usbd_interface	*iface;
    733 	usbd_status		err;
    734 	usb_interface_descriptor_t	*id;
    735 	usb_endpoint_descriptor_t	*ed;
    736 	int			i;
    737 
    738 	DPRINTFN(5,(" : aue_attach: sc=%p", sc));
    739 
    740 	sc->aue_dev = self;
    741 
    742 	aprint_naive("\n");
    743 	aprint_normal("\n");
    744 
    745 	devinfop = usbd_devinfo_alloc(uaa->uaa_device, 0);
    746 	aprint_normal_dev(self, "%s\n", devinfop);
    747 	usbd_devinfo_free(devinfop);
    748 
    749 	err = usbd_set_config_no(dev, AUE_CONFIG_NO, 1);
    750 	if (err) {
    751 		aprint_error_dev(self, "failed to set configuration"
    752 		    ", err=%s\n", usbd_errstr(err));
    753 		return;
    754 	}
    755 
    756 	usb_init_task(&sc->aue_tick_task, aue_tick_task, sc, 0);
    757 	usb_init_task(&sc->aue_stop_task, (void (*)(void *))aue_stop, sc, 0);
    758 	mutex_init(&sc->aue_mii_lock, MUTEX_DEFAULT, IPL_NONE);
    759 	mutex_init(&sc->aue_lock, MUTEX_DEFAULT, IPL_NONE);
    760 	mutex_init(&sc->aue_txlock, MUTEX_DEFAULT, IPL_SOFTUSB);
    761 	mutex_init(&sc->aue_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB);
    762 
    763 	err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &iface);
    764 	if (err) {
    765 		aprint_error_dev(self, "getting interface handle failed\n");
    766 		return;
    767 	}
    768 	sc->aue_closing = 0;
    769 
    770 	mutex_init(&sc->aue_mcmtx, MUTEX_DRIVER, IPL_NET);
    771 	cv_init(&sc->aue_closemc, "auemccl");
    772 
    773 	if (err) {
    774 		aprint_error_dev(self,
    775 		    "creating multicast configuration thread\n");
    776 		return;
    777 	}
    778 	sc->aue_flags = aue_lookup(uaa->uaa_vendor,
    779 	    uaa->uaa_product)->aue_flags;
    780 
    781 	sc->aue_udev = dev;
    782 	sc->aue_iface = iface;
    783 	sc->aue_product = uaa->uaa_product;
    784 	sc->aue_vendor = uaa->uaa_vendor;
    785 
    786 	id = usbd_get_interface_descriptor(iface);
    787 
    788 	/* Find endpoints. */
    789 	for (i = 0; i < id->bNumEndpoints; i++) {
    790 		ed = usbd_interface2endpoint_descriptor(iface, i);
    791 		if (ed == NULL) {
    792 			aprint_error_dev(self,
    793 			    "couldn't get endpoint descriptor %d\n", i);
    794 			return;
    795 		}
    796 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    797 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    798 			sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
    799 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    800 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    801 			sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
    802 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    803 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    804 			sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
    805 		}
    806 	}
    807 
    808 	if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 ||
    809 	    sc->aue_ed[AUE_ENDPT_INTR] == 0) {
    810 		aprint_error_dev(self, "missing endpoint\n");
    811 		return;
    812 	}
    813 
    814 	/* Reset the adapter. */
    815 	aue_reset(sc);
    816 
    817 	/*
    818 	 * Get station address from the EEPROM.
    819 	 */
    820 	aue_read_mac(sc, eaddr);
    821 
    822 	/*
    823 	 * A Pegasus chip was detected. Inform the world.
    824 	 */
    825 	ifp = GET_IFP(sc);
    826 	aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
    827 
    828 	/* Initialize interface info.*/
    829 	ifp->if_softc = sc;
    830 	ifp->if_mtu = ETHERMTU;
    831 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    832 	ifp->if_init = aue_init;
    833 	ifp->if_ioctl = aue_ioctl;
    834 	ifp->if_start = aue_start;
    835 	ifp->if_watchdog = aue_watchdog;
    836 	strncpy(ifp->if_xname, device_xname(sc->aue_dev), IFNAMSIZ);
    837 
    838 	IFQ_SET_READY(&ifp->if_snd);
    839 
    840 	/* Initialize MII/media info. */
    841 	mii = &sc->aue_mii;
    842 	mii->mii_ifp = ifp;
    843 	mii->mii_readreg = aue_miibus_readreg;
    844 	mii->mii_writereg = aue_miibus_writereg;
    845 	mii->mii_statchg = aue_miibus_statchg;
    846 	mii->mii_flags = MIIF_AUTOTSLEEP;
    847 	sc->aue_ec.ec_mii = mii;
    848 	ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, ether_mediastatus);
    849 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
    850 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
    851 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
    852 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
    853 	} else
    854 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    855 
    856 	/* Attach the interface. */
    857 	if_attach(ifp);
    858 	ether_set_ifflags_cb(&sc->aue_ec, aue_ifflags_cb);
    859 	ether_ifattach(ifp, eaddr);
    860 	rnd_attach_source(&sc->rnd_source, device_xname(sc->aue_dev),
    861 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
    862 
    863 	callout_init(&(sc->aue_stat_ch), 0);
    864 
    865 	sc->aue_attached = 1;
    866 
    867 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->aue_udev, sc->aue_dev);
    868 
    869 	return;
    870 }
    871 
    872 int
    873 aue_detach(device_t self, int flags)
    874 {
    875 	struct aue_softc *sc = device_private(self);
    876 	struct ifnet		*ifp = GET_IFP(sc);
    877 	int			s;
    878 
    879 	DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
    880 
    881 	if (!sc->aue_attached) {
    882 		/* Detached before attached finished, so just bail out. */
    883 		return 0;
    884 	}
    885 
    886 	callout_stop(&sc->aue_stat_ch);
    887 	/*
    888 	 * Remove any pending tasks.  They cannot be executing because they run
    889 	 * in the same thread as detach.
    890 	 */
    891 	usb_rem_task(sc->aue_udev, &sc->aue_tick_task);
    892 	usb_rem_task(sc->aue_udev, &sc->aue_stop_task);
    893 
    894 	sc->aue_closing = 1;
    895 
    896 	mutex_enter(&sc->aue_mcmtx);
    897 	cv_wait(&sc->aue_closemc,&sc->aue_mcmtx);
    898 	mutex_exit(&sc->aue_mcmtx);
    899 
    900 	mutex_destroy(&sc->aue_mcmtx);
    901 	cv_destroy(&sc->aue_closemc);
    902 
    903 	s = splusb();
    904 
    905 	if (ifp->if_flags & IFF_RUNNING)
    906 		aue_stop(ifp, 1);
    907 
    908 	rnd_detach_source(&sc->rnd_source);
    909 	mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY);
    910 	ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY);
    911 	ether_ifdetach(ifp);
    912 
    913 	if_detach(ifp);
    914 
    915 #ifdef DIAGNOSTIC
    916 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL ||
    917 	    sc->aue_ep[AUE_ENDPT_RX] != NULL ||
    918 	    sc->aue_ep[AUE_ENDPT_INTR] != NULL)
    919 		aprint_error_dev(self, "detach has active endpoints\n");
    920 #endif
    921 
    922 	sc->aue_attached = 0;
    923 
    924 	if (--sc->aue_refcnt >= 0) {
    925 		/* Wait for processes to go away. */
    926 		usb_detach_waitold(sc->aue_dev);
    927 	}
    928 	splx(s);
    929 
    930 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->aue_udev, sc->aue_dev);
    931 
    932 	mutex_destroy(&sc->aue_mii_lock);
    933 	mutex_destroy(&sc->aue_lock);
    934 	mutex_destroy(&sc->aue_txlock);
    935 	mutex_destroy(&sc->aue_rxlock);
    936 #if 0
    937 	mutex_destroy(&sc->wkmtx);
    938 #endif
    939 	return 0;
    940 }
    941 
    942 int
    943 aue_activate(device_t self, enum devact act)
    944 {
    945 	struct aue_softc *sc = device_private(self);
    946 
    947 	DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
    948 
    949 	switch (act) {
    950 	case DVACT_DEACTIVATE:
    951 		if_deactivate(&sc->aue_ec.ec_if);
    952 		sc->aue_dying = 1;
    953 		return 0;
    954 	default:
    955 		return EOPNOTSUPP;
    956 	}
    957 }
    958 
    959 /*
    960  * Initialize an RX descriptor and attach an MBUF cluster.
    961  */
    962 Static int
    963 aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m)
    964 {
    965 	struct mbuf		*m_new = NULL;
    966 
    967 	DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
    968 
    969 	if (m == NULL) {
    970 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    971 		if (m_new == NULL) {
    972 			aprint_error_dev(sc->aue_dev, "no memory for rx list "
    973 			    "-- packet dropped!\n");
    974 			return ENOBUFS;
    975 		}
    976 
    977 		MCLGET(m_new, M_DONTWAIT);
    978 		if (!(m_new->m_flags & M_EXT)) {
    979 			aprint_error_dev(sc->aue_dev, "no memory for rx "
    980 			    "list -- packet dropped!\n");
    981 			m_freem(m_new);
    982 			return ENOBUFS;
    983 		}
    984 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    985 	} else {
    986 		m_new = m;
    987 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    988 		m_new->m_data = m_new->m_ext.ext_buf;
    989 	}
    990 
    991 	m_adj(m_new, ETHER_ALIGN);
    992 	c->aue_mbuf = m_new;
    993 
    994 	return 0;
    995 }
    996 
    997 Static int
    998 aue_rx_list_init(struct aue_softc *sc)
    999 {
   1000 	struct aue_cdata	*cd;
   1001 	struct aue_chain	*c;
   1002 	int			i;
   1003 
   1004 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
   1005 
   1006 	cd = &sc->aue_cdata;
   1007 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
   1008 		c = &cd->aue_rx_chain[i];
   1009 		c->aue_sc = sc;
   1010 		c->aue_idx = i;
   1011 		if (aue_newbuf(sc, c, NULL) == ENOBUFS)
   1012 			return ENOBUFS;
   1013 		if (c->aue_xfer == NULL) {
   1014 			int err = usbd_create_xfer(sc->aue_ep[AUE_ENDPT_RX],
   1015 			    AUE_BUFSZ, USBD_SHORT_XFER_OK, 0, &c->aue_xfer);
   1016 			if (err) {
   1017 				return err;
   1018 			}
   1019 			c->aue_buf = usbd_get_buffer(c->aue_xfer);
   1020 		}
   1021 	}
   1022 
   1023 	return 0;
   1024 }
   1025 
   1026 Static void
   1027 aue_rx_list_free(struct aue_softc *sc)
   1028 {
   1029 	/* Free RX resources. */
   1030 	for (int i = 0; i < AUE_RX_LIST_CNT; i++) {
   1031 		if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
   1032 			m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
   1033 			sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
   1034 		}
   1035 		if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
   1036 			usbd_destroy_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
   1037 			sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
   1038 		}
   1039 	}
   1040 }
   1041 
   1042 Static int
   1043 aue_tx_list_init(struct aue_softc *sc)
   1044 {
   1045 	struct aue_cdata	*cd;
   1046 	struct aue_chain	*c;
   1047 	int			i;
   1048 
   1049 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
   1050 
   1051 	cd = &sc->aue_cdata;
   1052 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
   1053 		c = &cd->aue_tx_chain[i];
   1054 		c->aue_sc = sc;
   1055 		c->aue_idx = i;
   1056 		c->aue_mbuf = NULL;
   1057 		if (c->aue_xfer == NULL) {
   1058 			int err = usbd_create_xfer(sc->aue_ep[AUE_ENDPT_TX],
   1059 			    AUE_BUFSZ, USBD_FORCE_SHORT_XFER, 0, &c->aue_xfer);
   1060 			if (err) {
   1061 				return err;
   1062 			}
   1063 			c->aue_buf = usbd_get_buffer(c->aue_xfer);
   1064 		}
   1065 	}
   1066 
   1067 	return 0;
   1068 }
   1069 
   1070 Static void
   1071 aue_tx_list_free(struct aue_softc *sc)
   1072 {
   1073 	/* Free TX resources. */
   1074 	for (int i = 0; i < AUE_TX_LIST_CNT; i++) {
   1075 		if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
   1076 			m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
   1077 			sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
   1078 		}
   1079 		if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
   1080 			usbd_destroy_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
   1081 			sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
   1082 		}
   1083 	}
   1084 }
   1085 
   1086 Static void
   1087 aue_intr(struct usbd_xfer *xfer, void *priv,
   1088     usbd_status status)
   1089 {
   1090 	struct aue_softc	*sc = priv;
   1091 	struct ifnet		*ifp = GET_IFP(sc);
   1092 	struct aue_intrpkt	*p = &sc->aue_cdata.aue_ibuf;
   1093 
   1094 	DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
   1095 
   1096 	if (sc->aue_dying)
   1097 		return;
   1098 
   1099 	if (!(ifp->if_flags & IFF_RUNNING))
   1100 		return;
   1101 
   1102 	if (status != USBD_NORMAL_COMPLETION) {
   1103 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1104 			return;
   1105 		}
   1106 		sc->aue_intr_errs++;
   1107 		if (usbd_ratecheck(&sc->aue_rx_notice)) {
   1108 			aprint_debug_dev(sc->aue_dev,
   1109 			    "%u usb errors on intr: %s\n", sc->aue_intr_errs,
   1110 			    usbd_errstr(status));
   1111 			sc->aue_intr_errs = 0;
   1112 		}
   1113 		if (status == USBD_STALLED)
   1114 			usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]);
   1115 		return;
   1116 	}
   1117 
   1118 	if (p->aue_txstat0)
   1119 		ifp->if_oerrors++;
   1120 
   1121 	if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
   1122 		ifp->if_collisions++;
   1123 }
   1124 
   1125 /*
   1126  * A frame has been uploaded: pass the resulting mbuf chain up to
   1127  * the higher level protocols.
   1128  */
   1129 Static void
   1130 aue_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
   1131 {
   1132 	struct aue_chain	*c = priv;
   1133 	struct aue_softc	*sc = c->aue_sc;
   1134 	struct ifnet		*ifp = GET_IFP(sc);
   1135 	struct mbuf		*m;
   1136 	uint32_t		total_len;
   1137 	struct aue_rxpkt	r;
   1138 	int			s;
   1139 
   1140 	DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
   1141 
   1142 	if (sc->aue_dying)
   1143 		return;
   1144 
   1145 	if (!(ifp->if_flags & IFF_RUNNING))
   1146 		return;
   1147 
   1148 	if (status != USBD_NORMAL_COMPLETION) {
   1149 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   1150 			return;
   1151 		sc->aue_rx_errs++;
   1152 		if (usbd_ratecheck(&sc->aue_rx_notice)) {
   1153 			aprint_error_dev(sc->aue_dev,
   1154 			    "%u usb errors on rx: %s\n", sc->aue_rx_errs,
   1155 			    usbd_errstr(status));
   1156 			sc->aue_rx_errs = 0;
   1157 		}
   1158 		if (status == USBD_STALLED)
   1159 			usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]);
   1160 		goto done;
   1161 	}
   1162 
   1163 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
   1164 
   1165 	memcpy(mtod(c->aue_mbuf, char *), c->aue_buf, total_len);
   1166 
   1167 	if (total_len <= 4 + ETHER_CRC_LEN) {
   1168 		ifp->if_ierrors++;
   1169 		goto done;
   1170 	}
   1171 
   1172 	memcpy(&r, c->aue_buf + total_len - 4, sizeof(r));
   1173 
   1174 	/* Turn off all the non-error bits in the rx status word. */
   1175 	r.aue_rxstat &= AUE_RXSTAT_MASK;
   1176 	if (r.aue_rxstat) {
   1177 		ifp->if_ierrors++;
   1178 		goto done;
   1179 	}
   1180 
   1181 	/* No errors; receive the packet. */
   1182 	m = c->aue_mbuf;
   1183 	total_len -= ETHER_CRC_LEN + 4;
   1184 	m->m_pkthdr.len = m->m_len = total_len;
   1185 	ifp->if_ipackets++;
   1186 
   1187 	m->m_pkthdr.rcvif = ifp;
   1188 
   1189 	s = splnet();
   1190 
   1191 	/* XXX ugly */
   1192 	if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
   1193 		ifp->if_ierrors++;
   1194 		goto done1;
   1195 	}
   1196 
   1197 	/*
   1198 	 * Handle BPF listeners. Let the BPF user see the packet, but
   1199 	 * don't pass it up to the ether_input() layer unless it's
   1200 	 * a broadcast packet, multicast packet, matches our ethernet
   1201 	 * address or the interface is in promiscuous mode.
   1202 	 */
   1203 	bpf_mtap(ifp, m);
   1204 
   1205 	DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->aue_dev),
   1206 		    __func__, m->m_len));
   1207 	(*(ifp)->if_input)((ifp), (m));
   1208  done1:
   1209 	splx(s);
   1210 
   1211  done:
   1212 
   1213 	/* Setup new transfer. */
   1214 	usbd_setup_xfer(xfer, c, c->aue_buf, AUE_BUFSZ,
   1215 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof);
   1216 	usbd_transfer(xfer);
   1217 
   1218 	DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->aue_dev),
   1219 		    __func__));
   1220 }
   1221 
   1222 /*
   1223  * A frame was downloaded to the chip. It's safe for us to clean up
   1224  * the list buffers.
   1225  */
   1226 
   1227 Static void
   1228 aue_txeof(struct usbd_xfer *xfer, void *priv,
   1229     usbd_status status)
   1230 {
   1231 	struct aue_chain	*c = priv;
   1232 	struct aue_softc	*sc = c->aue_sc;
   1233 	struct ifnet		*ifp = GET_IFP(sc);
   1234 	int			s;
   1235 
   1236 	if (sc->aue_dying)
   1237 		return;
   1238 
   1239 	s = splnet();
   1240 
   1241 	DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->aue_dev),
   1242 		    __func__, status));
   1243 
   1244 	ifp->if_timer = 0;
   1245 	ifp->if_flags &= ~IFF_OACTIVE;
   1246 
   1247 	if (status != USBD_NORMAL_COMPLETION) {
   1248 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1249 			splx(s);
   1250 			return;
   1251 		}
   1252 		ifp->if_oerrors++;
   1253 		aprint_error_dev(sc->aue_dev, "usb error on tx: %s\n",
   1254 		    usbd_errstr(status));
   1255 		if (status == USBD_STALLED)
   1256 			usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_TX]);
   1257 		splx(s);
   1258 		return;
   1259 	}
   1260 
   1261 	ifp->if_opackets++;
   1262 
   1263 	m_freem(c->aue_mbuf);
   1264 	c->aue_mbuf = NULL;
   1265 
   1266 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1267 		aue_start(ifp);
   1268 
   1269 	splx(s);
   1270 }
   1271 
   1272 Static void
   1273 aue_tick(void *xsc)
   1274 {
   1275 	struct aue_softc	*sc = xsc;
   1276 
   1277 	DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
   1278 
   1279 	if (sc == NULL)
   1280 		return;
   1281 
   1282 	if (sc->aue_dying)
   1283 		return;
   1284 
   1285 	/* Perform periodic stuff in process context. */
   1286 	usb_add_task(sc->aue_udev, &sc->aue_tick_task, USB_TASKQ_DRIVER);
   1287 }
   1288 
   1289 Static void
   1290 aue_tick_task(void *xsc)
   1291 {
   1292 	struct aue_softc	*sc = xsc;
   1293 	struct ifnet		*ifp;
   1294 	struct mii_data		*mii;
   1295 	int			s;
   1296 
   1297 	DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
   1298 
   1299 	if (sc->aue_dying)
   1300 		return;
   1301 
   1302 	ifp = GET_IFP(sc);
   1303 	mii = GET_MII(sc);
   1304 	if (mii == NULL)
   1305 		return;
   1306 
   1307 	s = splnet();
   1308 
   1309 	mii_tick(mii);
   1310 	if (!sc->aue_link) {
   1311 		mii_pollstat(mii); /* XXX FreeBSD has removed this call */
   1312 		if (mii->mii_media_status & IFM_ACTIVE &&
   1313 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
   1314 			DPRINTFN(2,("%s: %s: got link\n",
   1315 			    device_xname(sc->aue_dev), __func__));
   1316 			sc->aue_link++;
   1317 			if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1318 				aue_start(ifp);
   1319 		}
   1320 	}
   1321 
   1322 	callout_reset(&(sc->aue_stat_ch), (hz), (aue_tick), (sc));
   1323 
   1324 	splx(s);
   1325 }
   1326 
   1327 Static int
   1328 aue_send(struct aue_softc *sc, struct mbuf *m, int idx)
   1329 {
   1330 	int			total_len;
   1331 	struct aue_chain	*c;
   1332 	usbd_status		err;
   1333 
   1334 	DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
   1335 
   1336 	c = &sc->aue_cdata.aue_tx_chain[idx];
   1337 
   1338 	/*
   1339 	 * Copy the mbuf data into a contiguous buffer, leaving two
   1340 	 * bytes at the beginning to hold the frame length.
   1341 	 */
   1342 	m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
   1343 	c->aue_mbuf = m;
   1344 
   1345 	/*
   1346 	 * The ADMtek documentation says that the packet length is
   1347 	 * supposed to be specified in the first two bytes of the
   1348 	 * transfer, however it actually seems to ignore this info
   1349 	 * and base the frame size on the bulk transfer length.
   1350 	 */
   1351 	c->aue_buf[0] = (uint8_t)m->m_pkthdr.len;
   1352 	c->aue_buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
   1353 	total_len = m->m_pkthdr.len + 2;
   1354 
   1355 	usbd_setup_xfer(c->aue_xfer, c, c->aue_buf, total_len,
   1356 	    USBD_FORCE_SHORT_XFER, AUE_TX_TIMEOUT, aue_txeof);
   1357 
   1358 	/* Transmit */
   1359 	err = usbd_transfer(c->aue_xfer);
   1360 	if (err != USBD_IN_PROGRESS) {
   1361 		aprint_error_dev(sc->aue_dev, "aue_send error=%s\n",
   1362 		       usbd_errstr(err));
   1363 		/* Stop the interface from process context. */
   1364 		usb_add_task(sc->aue_udev, &sc->aue_stop_task,
   1365 		    USB_TASKQ_DRIVER);
   1366 		return EIO;
   1367 	}
   1368 	DPRINTFN(5,("%s: %s: send %d bytes\n", device_xname(sc->aue_dev),
   1369 		    __func__, total_len));
   1370 
   1371 	sc->aue_cdata.aue_tx_cnt++;
   1372 
   1373 	return 0;
   1374 }
   1375 
   1376 Static void
   1377 aue_start(struct ifnet *ifp)
   1378 {
   1379 	struct aue_softc *sc = ifp->if_softc;
   1380 
   1381 	mutex_enter(&sc->aue_txlock);
   1382 	aue_start_locked(ifp);
   1383 	mutex_exit(&sc->aue_txlock);
   1384 }
   1385 
   1386 Static void
   1387 aue_start_locked(struct ifnet *ifp)
   1388 {
   1389 	struct aue_softc	*sc = ifp->if_softc;
   1390 	struct mbuf		*m_head = NULL;
   1391 
   1392 	DPRINTFN(5,("%s: %s: enter, link=%d\n", device_xname(sc->aue_dev),
   1393 		    __func__, sc->aue_link));
   1394 
   1395 	if (sc->aue_dying)
   1396 		return;
   1397 
   1398 	if (!sc->aue_link)
   1399 		return;
   1400 
   1401 	if (ifp->if_flags & IFF_OACTIVE)
   1402 		return;
   1403 
   1404 	IFQ_POLL(&ifp->if_snd, m_head);
   1405 	if (m_head == NULL)
   1406 		return;
   1407 
   1408 	if (aue_send(sc, m_head, 0)) {
   1409 		return;
   1410 	}
   1411 
   1412 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
   1413 
   1414 	/*
   1415 	 * If there's a BPF listener, bounce a copy of this frame
   1416 	 * to him.
   1417 	 */
   1418 	bpf_mtap(ifp, m_head);
   1419 
   1420 	ifp->if_flags |= IFF_OACTIVE;
   1421 
   1422 	/*
   1423 	 * Set a timeout in case the chip goes out to lunch.
   1424 	 */
   1425 	ifp->if_timer = 5;
   1426 }
   1427 
   1428 Static int
   1429 aue_init(struct ifnet *ifp)
   1430 {
   1431 	struct aue_softc	*sc = ifp->if_softc;
   1432 
   1433 	mutex_enter(&sc->aue_lock);
   1434 	int ret = aue_init_locked(ifp);
   1435 	mutex_exit(&sc->aue_lock);
   1436 
   1437 	return ret;
   1438 }
   1439 
   1440 Static int
   1441 aue_init_locked(struct ifnet *ifp)
   1442 {
   1443 	struct aue_softc	*sc = ifp->if_softc;
   1444 	struct mii_data		*mii = GET_MII(sc);
   1445 	int			i;
   1446 	const u_char		*eaddr;
   1447 	usbd_status		err;
   1448 
   1449 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
   1450 
   1451 	if (sc->aue_dying)
   1452 		return EIO;
   1453 
   1454 	aue_stop_locked(ifp, 1);
   1455 
   1456 	/*
   1457 	 * Cancel pending I/O and free all RX/TX buffers.
   1458 	 */
   1459 	aue_reset(sc);
   1460 
   1461 	eaddr = CLLADDR(ifp->if_sadl);
   1462 	for (i = 0; i < ETHER_ADDR_LEN; i++)
   1463 		aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
   1464 
   1465 	 /* If we want promiscuous mode, set the allframes bit. */
   1466 	if (ifp->if_flags & IFF_PROMISC)
   1467 		AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1468 	else
   1469 		AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1470 
   1471 	if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
   1472 		/* Open RX and TX pipes. */
   1473 		err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
   1474 		USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
   1475 		if (err) {
   1476 			aprint_error_dev(sc->aue_dev, "open rx pipe failed: %s\n",
   1477 			usbd_errstr(err));
   1478 			goto fail;
   1479 		}
   1480 		err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
   1481 		USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
   1482 		if (err) {
   1483 			aprint_error_dev(sc->aue_dev, "open tx pipe failed: %s\n",
   1484 			usbd_errstr(err));
   1485 			goto fail1;
   1486 		}
   1487 		err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
   1488 			USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
   1489 			&sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
   1490 			AUE_INTR_INTERVAL);
   1491 		if (err) {
   1492 			aprint_error_dev(sc->aue_dev, "open intr pipe failed: %s\n",
   1493 			usbd_errstr(err));
   1494 			goto fail2;
   1495 		}
   1496 	}
   1497 	/* Init TX ring. */
   1498 	if (aue_tx_list_init(sc)) {
   1499 		aprint_error_dev(sc->aue_dev, "tx list init failed\n");
   1500 		goto fail3;
   1501 	}
   1502 
   1503 	/* Init RX ring. */
   1504 	if (aue_rx_list_init(sc)) {
   1505 		aprint_error_dev(sc->aue_dev, "rx list init failed\n");
   1506 		goto fail4;
   1507 	}
   1508 
   1509 	/* Start up the receive pipe. */
   1510 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
   1511 		struct aue_chain *c = &sc->aue_cdata.aue_rx_chain[i];
   1512 
   1513 		usbd_setup_xfer(c->aue_xfer, c, c->aue_buf, AUE_BUFSZ,
   1514 		    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof);
   1515 		(void)usbd_transfer(c->aue_xfer); /* XXX */
   1516 		DPRINTFN(5,("%s: %s: start read\n", device_xname(sc->aue_dev),
   1517 			    __func__));
   1518 
   1519 	}
   1520 
   1521 	/* Load the multicast filter. */
   1522 	aue_setmulti(sc);
   1523 
   1524 	/* Enable RX and TX */
   1525 	aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
   1526 	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
   1527 	AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
   1528 
   1529 	mii_mediachg(mii);
   1530 
   1531 	ifp->if_flags |= IFF_RUNNING;
   1532 	ifp->if_flags &= ~IFF_OACTIVE;
   1533 
   1534 	callout_reset(&(sc->aue_stat_ch), (hz), (aue_tick), (sc));
   1535 	return 0;
   1536 
   1537 fail4:
   1538 	aue_tx_list_free(sc);
   1539 fail3:
   1540 	usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
   1541 fail2:
   1542 	usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
   1543 fail1:
   1544 	usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
   1545 fail:
   1546 	return EIO;
   1547 }
   1548 
   1549 /*
   1550  * Set media options.
   1551  */
   1552 Static int
   1553 aue_ifmedia_upd(struct ifnet *ifp)
   1554 {
   1555 	struct aue_softc	*sc = ifp->if_softc;
   1556 	struct mii_data		*mii = GET_MII(sc);
   1557 	int rc;
   1558 
   1559 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
   1560 
   1561 	if (sc->aue_dying)
   1562 		return 0;
   1563 
   1564 	sc->aue_link = 0;
   1565 
   1566 	if ((rc = mii_mediachg(mii)) == ENXIO)
   1567 		return 0;
   1568 	return rc;
   1569 }
   1570 
   1571 Static int
   1572 aue_ioctl(struct ifnet *ifp, u_long command, void *data)
   1573 {
   1574 	struct aue_softc	*sc = ifp->if_softc;
   1575 	int			s, error = 0;
   1576 
   1577 	if (sc->aue_dying)
   1578 		return EIO;
   1579 
   1580 	s = splnet();
   1581 	error = ether_ioctl(ifp, command, data);
   1582 	splx(s);
   1583 
   1584 	return error;
   1585 }
   1586 
   1587 Static int
   1588 aue_ifflags_cb(struct ethercom *ec)
   1589 {
   1590 	struct ifnet *ifp = &ec->ec_if;
   1591 	struct aue_softc *sc = ifp->if_softc;
   1592 
   1593 	mutex_enter(&sc->aue_lock);
   1594 
   1595 	if (ifp->if_flags & IFF_UP) {
   1596 		if (ifp->if_flags & IFF_RUNNING &&
   1597 		    ifp->if_flags & IFF_PROMISC &&
   1598 		    !(sc->aue_if_flags & IFF_PROMISC)) {
   1599 			AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1600 		} else if (ifp->if_flags & IFF_RUNNING &&
   1601 		    !(ifp->if_flags & IFF_PROMISC) &&
   1602 		    sc->aue_if_flags & IFF_PROMISC) {
   1603 			AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1604 		}
   1605 	}
   1606 	sc->aue_if_flags = ifp->if_flags;
   1607 	aue_setmulti(sc);
   1608 	mutex_exit(&sc->aue_lock);
   1609 
   1610 	return 0;
   1611 }
   1612 
   1613 Static void
   1614 aue_watchdog(struct ifnet *ifp)
   1615 {
   1616 	struct aue_softc	*sc = ifp->if_softc;
   1617 	struct aue_chain	*c;
   1618 	usbd_status		stat;
   1619 	int			s;
   1620 
   1621 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
   1622 
   1623 	ifp->if_oerrors++;
   1624 	aprint_error_dev(sc->aue_dev, "watchdog timeout\n");
   1625 
   1626 	s = splusb();
   1627 	c = &sc->aue_cdata.aue_tx_chain[0];
   1628 	usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat);
   1629 	aue_txeof(c->aue_xfer, c, stat);
   1630 
   1631 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1632 		aue_start(ifp);
   1633 	splx(s);
   1634 }
   1635 
   1636 /*
   1637  * Stop the adapter and free any mbufs allocated to the
   1638  * RX and TX lists.
   1639  */
   1640 Static void
   1641 aue_stop(struct ifnet *ifp, int disable)
   1642 {
   1643 	struct aue_softc *sc = ifp->if_softc;
   1644 
   1645 	mutex_enter(&sc->aue_lock);
   1646 	aue_stop_locked(ifp, disable);
   1647 	mutex_exit(&sc->aue_lock);
   1648 }
   1649 
   1650 Static void
   1651 aue_stop_locked(struct ifnet *ifp, int disable)
   1652 {
   1653 	usbd_status		err;
   1654 	struct aue_softc	*sc = ifp->if_softc;
   1655 
   1656 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
   1657 
   1658 	ifp->if_timer = 0;
   1659 
   1660 	aue_csr_write_1(sc, AUE_CTL0, 0);
   1661 	aue_csr_write_1(sc, AUE_CTL1, 0);
   1662 	aue_reset(sc);
   1663 	callout_stop(&sc->aue_stat_ch);
   1664 
   1665 	/* Stop transfers. */
   1666 	if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
   1667 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
   1668 		if (err) {
   1669 			printf("%s: abort rx pipe failed: %s\n",
   1670 			    device_xname(sc->aue_dev), usbd_errstr(err));
   1671 		}
   1672 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
   1673 		if (err) {
   1674 			printf("%s: close rx pipe failed: %s\n",
   1675 			    device_xname(sc->aue_dev), usbd_errstr(err));
   1676 		}
   1677 		sc->aue_ep[AUE_ENDPT_RX] = NULL;
   1678 	}
   1679 
   1680 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
   1681 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
   1682 		if (err) {
   1683 			printf("%s: abort tx pipe failed: %s\n",
   1684 			    device_xname(sc->aue_dev), usbd_errstr(err));
   1685 		}
   1686 	}
   1687 
   1688 	if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
   1689 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
   1690 		if (err) {
   1691 			printf("%s: abort intr pipe failed: %s\n",
   1692 			    device_xname(sc->aue_dev), usbd_errstr(err));
   1693 		}
   1694 	}
   1695 
   1696 	aue_rx_list_free(sc);
   1697 
   1698 	aue_tx_list_free(sc);
   1699 
   1700 	/* Close pipes */
   1701 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
   1702 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
   1703 		if (err) {
   1704 			printf("%s: close tx pipe failed: %s\n",
   1705 			    device_xname(sc->aue_dev), usbd_errstr(err));
   1706 		}
   1707 		sc->aue_ep[AUE_ENDPT_TX] = NULL;
   1708 	}
   1709 
   1710 	if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
   1711 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
   1712 		if (err) {
   1713 			printf("%s: close intr pipe failed: %s\n",
   1714 			    device_xname(sc->aue_dev), usbd_errstr(err));
   1715 		}
   1716 		sc->aue_ep[AUE_ENDPT_INTR] = NULL;
   1717 	}
   1718 
   1719 	sc->aue_link = 0;
   1720 
   1721 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1722 }
   1723 
   1724