Home | History | Annotate | Line # | Download | only in usb
if_aue.c revision 1.132.4.9
      1 /*	$NetBSD: if_aue.c,v 1.132.4.9 2015/10/06 21:32:15 skrll 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  * add thread to avoid register reads from interrupt context
     75  * more error checks
     76  * investigate short rx problem
     77  * proper cleanup on errors
     78  */
     79 
     80 #include <sys/cdefs.h>
     81 __KERNEL_RCSID(0, "$NetBSD: if_aue.c,v 1.132.4.9 2015/10/06 21:32:15 skrll Exp $");
     82 
     83 #ifdef _KERNEL_OPT
     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/rndsource.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_multithread(void *);
    220 
    221 Static void aue_reset_pegasus_II(struct aue_softc *);
    222 Static int aue_tx_list_init(struct aue_softc *);
    223 Static int aue_rx_list_init(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 int aue_ioctl(struct ifnet *, u_long, void *);
    233 Static void aue_init(void *);
    234 Static void aue_stop(struct aue_softc *);
    235 Static void aue_watchdog(struct ifnet *);
    236 Static int aue_openpipes(struct aue_softc *);
    237 Static int aue_ifmedia_upd(struct ifnet *);
    238 
    239 Static int aue_eeprom_getword(struct aue_softc *, int);
    240 Static void aue_read_mac(struct aue_softc *, u_char *);
    241 Static int aue_miibus_readreg(device_t, int, int);
    242 Static void aue_miibus_writereg(device_t, int, int, int);
    243 Static void aue_miibus_statchg(struct ifnet *);
    244 
    245 Static void aue_lock_mii(struct aue_softc *);
    246 Static void aue_unlock_mii(struct aue_softc *);
    247 
    248 Static void aue_setmulti(struct aue_softc *);
    249 Static uint32_t aue_crc(void *);
    250 Static void aue_reset(struct aue_softc *);
    251 
    252 Static int aue_csr_read_1(struct aue_softc *, int);
    253 Static int aue_csr_write_1(struct aue_softc *, int, int);
    254 Static int aue_csr_read_2(struct aue_softc *, int);
    255 Static int aue_csr_write_2(struct aue_softc *, int, int);
    256 
    257 #define AUE_SETBIT(sc, reg, x)				\
    258 	aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x))
    259 
    260 #define AUE_CLRBIT(sc, reg, x)				\
    261 	aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x))
    262 
    263 Static int
    264 aue_csr_read_1(struct aue_softc *sc, int reg)
    265 {
    266 	usb_device_request_t	req;
    267 	usbd_status		err;
    268 	uByte			val = 0;
    269 
    270 	if (sc->aue_dying)
    271 		return 0;
    272 
    273 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    274 	req.bRequest = AUE_UR_READREG;
    275 	USETW(req.wValue, 0);
    276 	USETW(req.wIndex, reg);
    277 	USETW(req.wLength, 1);
    278 
    279 	err = usbd_do_request(sc->aue_udev, &req, &val);
    280 
    281 	if (err) {
    282 		DPRINTF(("%s: aue_csr_read_1: reg=0x%x err=%s\n",
    283 		    device_xname(sc->aue_dev), reg, usbd_errstr(err)));
    284 		return 0;
    285 	}
    286 
    287 	return val;
    288 }
    289 
    290 Static int
    291 aue_csr_read_2(struct aue_softc *sc, int reg)
    292 {
    293 	usb_device_request_t	req;
    294 	usbd_status		err;
    295 	uWord			val;
    296 
    297 	if (sc->aue_dying)
    298 		return 0;
    299 
    300 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    301 	req.bRequest = AUE_UR_READREG;
    302 	USETW(req.wValue, 0);
    303 	USETW(req.wIndex, reg);
    304 	USETW(req.wLength, 2);
    305 
    306 	err = usbd_do_request(sc->aue_udev, &req, &val);
    307 
    308 	if (err) {
    309 		DPRINTF(("%s: aue_csr_read_2: reg=0x%x err=%s\n",
    310 		    device_xname(sc->aue_dev), reg, usbd_errstr(err)));
    311 		return 0;
    312 	}
    313 
    314 	return UGETW(val);
    315 }
    316 
    317 Static int
    318 aue_csr_write_1(struct aue_softc *sc, int reg, int aval)
    319 {
    320 	usb_device_request_t	req;
    321 	usbd_status		err;
    322 	uByte			val;
    323 
    324 	if (sc->aue_dying)
    325 		return 0;
    326 
    327 	val = aval;
    328 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    329 	req.bRequest = AUE_UR_WRITEREG;
    330 	USETW(req.wValue, val);
    331 	USETW(req.wIndex, reg);
    332 	USETW(req.wLength, 1);
    333 
    334 	err = usbd_do_request(sc->aue_udev, &req, &val);
    335 
    336 	if (err) {
    337 		DPRINTF(("%s: aue_csr_write_1: reg=0x%x err=%s\n",
    338 		    device_xname(sc->aue_dev), reg, usbd_errstr(err)));
    339 		return -1;
    340 	}
    341 
    342 	return 0;
    343 }
    344 
    345 Static int
    346 aue_csr_write_2(struct aue_softc *sc, int reg, int aval)
    347 {
    348 	usb_device_request_t	req;
    349 	usbd_status		err;
    350 	uWord			val;
    351 
    352 	if (sc->aue_dying)
    353 		return 0;
    354 
    355 	USETW(val, aval);
    356 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    357 	req.bRequest = AUE_UR_WRITEREG;
    358 	USETW(req.wValue, aval);
    359 	USETW(req.wIndex, reg);
    360 	USETW(req.wLength, 2);
    361 
    362 	err = usbd_do_request(sc->aue_udev, &req, &val);
    363 
    364 	if (err) {
    365 		DPRINTF(("%s: aue_csr_write_2: reg=0x%x err=%s\n",
    366 		    device_xname(sc->aue_dev), reg, usbd_errstr(err)));
    367 		return -1;
    368 	}
    369 
    370 	return 0;
    371 }
    372 
    373 /*
    374  * Read a word of data stored in the EEPROM at address 'addr.'
    375  */
    376 Static int
    377 aue_eeprom_getword(struct aue_softc *sc, int addr)
    378 {
    379 	int		i;
    380 
    381 	aue_csr_write_1(sc, AUE_EE_REG, addr);
    382 	aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
    383 
    384 	for (i = 0; i < AUE_TIMEOUT; i++) {
    385 		if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
    386 			break;
    387 	}
    388 
    389 	if (i == AUE_TIMEOUT) {
    390 		printf("%s: EEPROM read timed out\n",
    391 		    device_xname(sc->aue_dev));
    392 	}
    393 
    394 	return aue_csr_read_2(sc, AUE_EE_DATA);
    395 }
    396 
    397 /*
    398  * Read the MAC from the EEPROM.  It's at offset 0.
    399  */
    400 Static void
    401 aue_read_mac(struct aue_softc *sc, u_char *dest)
    402 {
    403 	int			i;
    404 	int			off = 0;
    405 	int			word;
    406 
    407 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
    408 
    409 	for (i = 0; i < 3; i++) {
    410 		word = aue_eeprom_getword(sc, off + i);
    411 		dest[2 * i] = (u_char)word;
    412 		dest[2 * i + 1] = (u_char)(word >> 8);
    413 	}
    414 }
    415 
    416 /* Get exclusive access to the MII registers */
    417 Static void
    418 aue_lock_mii(struct aue_softc *sc)
    419 {
    420 	sc->aue_refcnt++;
    421 	mutex_enter(&sc->aue_mii_lock);
    422 }
    423 
    424 Static void
    425 aue_unlock_mii(struct aue_softc *sc)
    426 {
    427 	mutex_exit(&sc->aue_mii_lock);
    428 	if (--sc->aue_refcnt < 0)
    429 		usb_detach_wakeupold(sc->aue_dev);
    430 }
    431 
    432 Static int
    433 aue_miibus_readreg(device_t dev, int phy, int reg)
    434 {
    435 	struct aue_softc *sc = device_private(dev);
    436 	int			i;
    437 	uint16_t		val;
    438 
    439 	if (sc->aue_dying) {
    440 #ifdef DIAGNOSTIC
    441 		printf("%s: dying\n", device_xname(sc->aue_dev));
    442 #endif
    443 		return 0;
    444 	}
    445 
    446 #if 0
    447 	/*
    448 	 * The Am79C901 HomePNA PHY actually contains
    449 	 * two transceivers: a 1Mbps HomePNA PHY and a
    450 	 * 10Mbps full/half duplex ethernet PHY with
    451 	 * NWAY autoneg. However in the ADMtek adapter,
    452 	 * only the 1Mbps PHY is actually connected to
    453 	 * anything, so we ignore the 10Mbps one. It
    454 	 * happens to be configured for MII address 3,
    455 	 * so we filter that out.
    456 	 */
    457 	if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
    458 	    sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
    459 		if (phy == 3)
    460 			return 0;
    461 	}
    462 #endif
    463 
    464 	aue_lock_mii(sc);
    465 	aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
    466 	aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
    467 
    468 	for (i = 0; i < AUE_TIMEOUT; i++) {
    469 		if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
    470 			break;
    471 	}
    472 
    473 	if (i == AUE_TIMEOUT) {
    474 		printf("%s: MII read timed out\n", device_xname(sc->aue_dev));
    475 	}
    476 
    477 	val = aue_csr_read_2(sc, AUE_PHY_DATA);
    478 
    479 	DPRINTFN(11,("%s: %s: phy=%d reg=%d => 0x%04x\n",
    480 	    device_xname(sc->aue_dev), __func__, phy, reg, val));
    481 
    482 	aue_unlock_mii(sc);
    483 	return val;
    484 }
    485 
    486 Static void
    487 aue_miibus_writereg(device_t dev, int phy, int reg, int data)
    488 {
    489 	struct aue_softc *sc = device_private(dev);
    490 	int			i;
    491 
    492 #if 0
    493 	if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
    494 	    sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
    495 		if (phy == 3)
    496 			return;
    497 	}
    498 #endif
    499 
    500 	DPRINTFN(11,("%s: %s: phy=%d reg=%d data=0x%04x\n",
    501 	    device_xname(sc->aue_dev), __func__, phy, reg, data));
    502 
    503 	aue_lock_mii(sc);
    504 	aue_csr_write_2(sc, AUE_PHY_DATA, data);
    505 	aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
    506 	aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
    507 
    508 	for (i = 0; i < AUE_TIMEOUT; i++) {
    509 		if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
    510 			break;
    511 	}
    512 
    513 	if (i == AUE_TIMEOUT) {
    514 		printf("%s: MII read timed out\n", device_xname(sc->aue_dev));
    515 	}
    516 	aue_unlock_mii(sc);
    517 }
    518 
    519 Static void
    520 aue_miibus_statchg(struct ifnet *ifp)
    521 {
    522 	struct aue_softc *sc = ifp->if_softc;
    523 	struct mii_data	*mii = GET_MII(sc);
    524 
    525 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
    526 
    527 	aue_lock_mii(sc);
    528 	AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
    529 
    530 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
    531 		AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
    532 	} else {
    533 		AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
    534 	}
    535 
    536 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
    537 		AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
    538 	else
    539 		AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
    540 
    541 	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
    542 	aue_unlock_mii(sc);
    543 
    544 	/*
    545 	 * Set the LED modes on the LinkSys adapter.
    546 	 * This turns on the 'dual link LED' bin in the auxmode
    547 	 * register of the Broadcom PHY.
    548 	 */
    549 	if (!sc->aue_dying && (sc->aue_flags & LSYS)) {
    550 		uint16_t auxmode;
    551 		auxmode = aue_miibus_readreg(sc->aue_dev, 0, 0x1b);
    552 		aue_miibus_writereg(sc->aue_dev, 0, 0x1b, auxmode | 0x04);
    553 	}
    554 	DPRINTFN(5,("%s: %s: exit\n", device_xname(sc->aue_dev), __func__));
    555 }
    556 
    557 #define AUE_POLY	0xEDB88320
    558 #define AUE_BITS	6
    559 
    560 Static uint32_t
    561 aue_crc(void *addrv)
    562 {
    563 	uint32_t		idx, bit, data, crc;
    564 	char *addr = addrv;
    565 
    566 	/* Compute CRC for the address value. */
    567 	crc = 0xFFFFFFFF; /* initial value */
    568 
    569 	for (idx = 0; idx < 6; idx++) {
    570 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
    571 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0);
    572 	}
    573 
    574 	return crc & ((1 << AUE_BITS) - 1);
    575 }
    576 
    577 Static void
    578 aue_setmulti(struct aue_softc *sc)
    579 {
    580 	struct ifnet		*ifp;
    581 	struct ether_multi	*enm;
    582 	struct ether_multistep	step;
    583 	uint32_t		h = 0, i;
    584 
    585 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
    586 
    587 	ifp = GET_IFP(sc);
    588 
    589 	if (ifp->if_flags & IFF_PROMISC) {
    590 allmulti:
    591 		ifp->if_flags |= IFF_ALLMULTI;
    592 		AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
    593 		return;
    594 	}
    595 
    596 	AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
    597 
    598 	/* first, zot all the existing hash bits */
    599 	for (i = 0; i < 8; i++)
    600 		aue_csr_write_1(sc, AUE_MAR0 + i, 0);
    601 
    602 	/* now program new ones */
    603 	ETHER_FIRST_MULTI(step, &sc->aue_ec, enm);
    604 	while (enm != NULL) {
    605 		if (memcmp(enm->enm_addrlo,
    606 		    enm->enm_addrhi, ETHER_ADDR_LEN) != 0)
    607 			goto allmulti;
    608 
    609 		h = aue_crc(enm->enm_addrlo);
    610 		AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0x7));
    611 		ETHER_NEXT_MULTI(step, enm);
    612 	}
    613 
    614 	ifp->if_flags &= ~IFF_ALLMULTI;
    615 }
    616 
    617 Static void
    618 aue_reset_pegasus_II(struct aue_softc *sc)
    619 {
    620 	/* Magic constants taken from Linux driver. */
    621 	aue_csr_write_1(sc, AUE_REG_1D, 0);
    622 	aue_csr_write_1(sc, AUE_REG_7B, 2);
    623 #if 0
    624 	if ((sc->aue_flags & HAS_HOME_PNA) && mii_mode)
    625 		aue_csr_write_1(sc, AUE_REG_81, 6);
    626 	else
    627 #endif
    628 		aue_csr_write_1(sc, AUE_REG_81, 2);
    629 }
    630 
    631 Static void
    632 aue_reset(struct aue_softc *sc)
    633 {
    634 	int		i;
    635 
    636 	DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
    637 
    638 	AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
    639 
    640 	for (i = 0; i < AUE_TIMEOUT; i++) {
    641 		if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
    642 			break;
    643 	}
    644 
    645 	if (i == AUE_TIMEOUT)
    646 		printf("%s: reset failed\n", device_xname(sc->aue_dev));
    647 
    648 #if 0
    649 	/* XXX what is mii_mode supposed to be */
    650 	if (sc->aue_mii_mode && (sc->aue_flags & PNA))
    651 		aue_csr_write_1(sc, AUE_GPIO1, 0x34);
    652 	else
    653 		aue_csr_write_1(sc, AUE_GPIO1, 0x26);
    654 #endif
    655 
    656 	/*
    657 	 * The PHY(s) attached to the Pegasus chip may be held
    658 	 * in reset until we flip on the GPIO outputs. Make sure
    659 	 * to set the GPIO pins high so that the PHY(s) will
    660 	 * be enabled.
    661 	 *
    662 	 * Note: We force all of the GPIO pins low first, *then*
    663 	 * enable the ones we want.
    664   	 */
    665 	if (sc->aue_flags & LSYS) {
    666 		/* Grrr. LinkSys has to be different from everyone else. */
    667 		aue_csr_write_1(sc, AUE_GPIO0,
    668 		    AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
    669 	} else {
    670 		aue_csr_write_1(sc, AUE_GPIO0,
    671 		    AUE_GPIO_OUT0 | AUE_GPIO_SEL0);
    672 	}
    673   	aue_csr_write_1(sc, AUE_GPIO0,
    674 	    AUE_GPIO_OUT0 | AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
    675 
    676 	if (sc->aue_flags & PII)
    677 		aue_reset_pegasus_II(sc);
    678 
    679 	/* Wait a little while for the chip to get its brains in order. */
    680 	delay(10000);		/* XXX */
    681 }
    682 
    683 /*
    684  * Probe for a Pegasus chip.
    685  */
    686 int
    687 aue_match(device_t parent, cfdata_t match, void *aux)
    688 {
    689 	struct usb_attach_arg *uaa = aux;
    690 
    691 	/*
    692 	 * Some manufacturers use the same vendor and product id for
    693 	 * different devices. We need to sanity check the DeviceClass
    694 	 * in this case
    695 	 * Currently known guilty products:
    696 	 * 0x050d/0x0121 Belkin Bluetooth and USB2LAN
    697 	 *
    698 	 * If this turns out to be more common, we could use a quirk
    699 	 * table.
    700 	 */
    701 	if (uaa->uaa_vendor == USB_VENDOR_BELKIN &&
    702 		uaa->uaa_product == USB_PRODUCT_BELKIN_USB2LAN) {
    703 		usb_device_descriptor_t *dd;
    704 
    705 		dd = usbd_get_device_descriptor(uaa->uaa_device);
    706 		if (dd != NULL &&
    707 			dd->bDeviceClass != UDCLASS_IN_INTERFACE)
    708 			return UMATCH_NONE;
    709 	}
    710 
    711 	return aue_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    712 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    713 }
    714 
    715 /*
    716  * Attach the interface. Allocate softc structures, do ifmedia
    717  * setup and ethernet/BPF attach.
    718  */
    719 void
    720 aue_attach(device_t parent, device_t self, void *aux)
    721 {
    722 	struct aue_softc *sc = device_private(self);
    723 	struct usb_attach_arg *uaa = aux;
    724 	char			*devinfop;
    725 	int			s;
    726 	u_char			eaddr[ETHER_ADDR_LEN];
    727 	struct ifnet		*ifp;
    728 	struct mii_data		*mii;
    729 	struct usbd_device	*dev = uaa->uaa_device;
    730 	struct usbd_interface	*iface;
    731 	usbd_status		err;
    732 	usb_interface_descriptor_t	*id;
    733 	usb_endpoint_descriptor_t	*ed;
    734 	int			i;
    735 
    736 	DPRINTFN(5,(" : aue_attach: sc=%p", sc));
    737 
    738 	sc->aue_dev = self;
    739 
    740 	aprint_naive("\n");
    741 	aprint_normal("\n");
    742 
    743 	devinfop = usbd_devinfo_alloc(uaa->uaa_device, 0);
    744 	aprint_normal_dev(self, "%s\n", devinfop);
    745 	usbd_devinfo_free(devinfop);
    746 
    747 	err = usbd_set_config_no(dev, AUE_CONFIG_NO, 1);
    748 	if (err) {
    749 		aprint_error_dev(self, "failed to set configuration"
    750 		    ", err=%s\n", usbd_errstr(err));
    751 		return;
    752 	}
    753 
    754 	usb_init_task(&sc->aue_tick_task, aue_tick_task, sc, 0);
    755 	usb_init_task(&sc->aue_stop_task, (void (*)(void *))aue_stop, sc, 0);
    756 	mutex_init(&sc->aue_mii_lock, MUTEX_DEFAULT, IPL_NONE);
    757 
    758 	err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &iface);
    759 	if (err) {
    760 		aprint_error_dev(self, "getting interface handle failed\n");
    761 		return;
    762 	}
    763 	sc->aue_closing = 0;
    764 
    765 	mutex_init(&sc->aue_mcmtx, MUTEX_DRIVER, IPL_NET);
    766 	cv_init(&sc->aue_domc, "auemc");
    767 	cv_init(&sc->aue_closemc, "auemccl");
    768 
    769 	err = kthread_create(PRI_NONE, 0, NULL,
    770 		aue_multithread, sc, &sc->aue_thread,
    771 		"%s-mc", device_xname(sc->aue_dev));
    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, uaa->uaa_product)->aue_flags;
    779 
    780 	sc->aue_udev = dev;
    781 	sc->aue_iface = iface;
    782 	sc->aue_product = uaa->uaa_product;
    783 	sc->aue_vendor = uaa->uaa_vendor;
    784 
    785 	id = usbd_get_interface_descriptor(iface);
    786 
    787 	/* Find endpoints. */
    788 	for (i = 0; i < id->bNumEndpoints; i++) {
    789 		ed = usbd_interface2endpoint_descriptor(iface, i);
    790 		if (ed == NULL) {
    791 			aprint_error_dev(self,
    792 			    "couldn't get endpoint descriptor %d\n", i);
    793 			return;
    794 		}
    795 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    796 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    797 			sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
    798 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    799 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    800 			sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
    801 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    802 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    803 			sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
    804 		}
    805 	}
    806 
    807 	if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 ||
    808 	    sc->aue_ed[AUE_ENDPT_INTR] == 0) {
    809 		aprint_error_dev(self, "missing endpoint\n");
    810 		return;
    811 	}
    812 
    813 
    814 	s = splnet();
    815 
    816 	/* Reset the adapter. */
    817 	aue_reset(sc);
    818 
    819 	/*
    820 	 * Get station address from the EEPROM.
    821 	 */
    822 	aue_read_mac(sc, eaddr);
    823 
    824 	/*
    825 	 * A Pegasus chip was detected. Inform the world.
    826 	 */
    827 	ifp = GET_IFP(sc);
    828 	aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
    829 
    830 	/* Initialize interface info.*/
    831 	ifp->if_softc = sc;
    832 	ifp->if_mtu = ETHERMTU;
    833 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    834 	ifp->if_ioctl = aue_ioctl;
    835 	ifp->if_start = aue_start;
    836 	ifp->if_watchdog = aue_watchdog;
    837 	strncpy(ifp->if_xname, device_xname(sc->aue_dev), IFNAMSIZ);
    838 
    839 	IFQ_SET_READY(&ifp->if_snd);
    840 
    841 	/* Initialize MII/media info. */
    842 	mii = &sc->aue_mii;
    843 	mii->mii_ifp = ifp;
    844 	mii->mii_readreg = aue_miibus_readreg;
    845 	mii->mii_writereg = aue_miibus_writereg;
    846 	mii->mii_statchg = aue_miibus_statchg;
    847 	mii->mii_flags = MIIF_AUTOTSLEEP;
    848 	sc->aue_ec.ec_mii = mii;
    849 	ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, ether_mediastatus);
    850 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
    851 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
    852 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
    853 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
    854 	} else
    855 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    856 
    857 	/* Attach the interface. */
    858 	if_attach(ifp);
    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 	splx(s);
    867 
    868 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->aue_udev, sc->aue_dev);
    869 
    870 	return;
    871 }
    872 
    873 int
    874 aue_detach(device_t self, int flags)
    875 {
    876 	struct aue_softc *sc = device_private(self);
    877 	struct ifnet		*ifp = GET_IFP(sc);
    878 	int			s;
    879 
    880 	DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
    881 
    882 	if (!sc->aue_attached) {
    883 		/* Detached before attached finished, so just bail out. */
    884 		return 0;
    885 	}
    886 
    887 	callout_stop(&sc->aue_stat_ch);
    888 	/*
    889 	 * Remove any pending tasks.  They cannot be executing because they run
    890 	 * in the same thread as detach.
    891 	 */
    892 	usb_rem_task(sc->aue_udev, &sc->aue_tick_task);
    893 	usb_rem_task(sc->aue_udev, &sc->aue_stop_task);
    894 
    895 	sc->aue_closing = 1;
    896 	cv_signal(&sc->aue_domc);
    897 
    898 	mutex_enter(&sc->aue_mcmtx);
    899 	cv_wait(&sc->aue_closemc,&sc->aue_mcmtx);
    900 	mutex_exit(&sc->aue_mcmtx);
    901 
    902 	mutex_destroy(&sc->aue_mcmtx);
    903 	cv_destroy(&sc->aue_domc);
    904 	cv_destroy(&sc->aue_closemc);
    905 
    906 	s = splusb();
    907 
    908 	if (ifp->if_flags & IFF_RUNNING)
    909 		aue_stop(sc);
    910 
    911 	rnd_detach_source(&sc->rnd_source);
    912 	mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY);
    913 	ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY);
    914 	ether_ifdetach(ifp);
    915 
    916 	if_detach(ifp);
    917 
    918 #ifdef DIAGNOSTIC
    919 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL ||
    920 	    sc->aue_ep[AUE_ENDPT_RX] != NULL ||
    921 	    sc->aue_ep[AUE_ENDPT_INTR] != NULL)
    922 		aprint_error_dev(self, "detach has active endpoints\n");
    923 #endif
    924 
    925 	sc->aue_attached = 0;
    926 
    927 	if (--sc->aue_refcnt >= 0) {
    928 		/* Wait for processes to go away. */
    929 		usb_detach_waitold(sc->aue_dev);
    930 	}
    931 	splx(s);
    932 
    933 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->aue_udev, sc->aue_dev);
    934 
    935 	mutex_destroy(&sc->aue_mii_lock);
    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 int
   1027 aue_tx_list_init(struct aue_softc *sc)
   1028 {
   1029 	struct aue_cdata	*cd;
   1030 	struct aue_chain	*c;
   1031 	int			i;
   1032 
   1033 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
   1034 
   1035 	cd = &sc->aue_cdata;
   1036 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
   1037 		c = &cd->aue_tx_chain[i];
   1038 		c->aue_sc = sc;
   1039 		c->aue_idx = i;
   1040 		c->aue_mbuf = NULL;
   1041 		if (c->aue_xfer == NULL) {
   1042 			int err = usbd_create_xfer(sc->aue_ep[AUE_ENDPT_TX],
   1043 			    AUE_BUFSZ, USBD_FORCE_SHORT_XFER, 0, &c->aue_xfer);
   1044 			if (err) {
   1045 				return err;
   1046 			}
   1047 			c->aue_buf = usbd_get_buffer(c->aue_xfer);
   1048 		}
   1049 	}
   1050 
   1051 	return 0;
   1052 }
   1053 
   1054 Static void
   1055 aue_intr(struct usbd_xfer *xfer, void *priv,
   1056     usbd_status status)
   1057 {
   1058 	struct aue_softc	*sc = priv;
   1059 	struct ifnet		*ifp = GET_IFP(sc);
   1060 	struct aue_intrpkt	*p = &sc->aue_cdata.aue_ibuf;
   1061 
   1062 	DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
   1063 
   1064 	if (sc->aue_dying)
   1065 		return;
   1066 
   1067 	if (!(ifp->if_flags & IFF_RUNNING))
   1068 		return;
   1069 
   1070 	if (status != USBD_NORMAL_COMPLETION) {
   1071 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1072 			return;
   1073 		}
   1074 		sc->aue_intr_errs++;
   1075 		if (usbd_ratecheck(&sc->aue_rx_notice)) {
   1076 			aprint_debug_dev(sc->aue_dev,
   1077 			    "%u usb errors on intr: %s\n", sc->aue_intr_errs,
   1078 			    usbd_errstr(status));
   1079 			sc->aue_intr_errs = 0;
   1080 		}
   1081 		if (status == USBD_STALLED)
   1082 			usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]);
   1083 		return;
   1084 	}
   1085 
   1086 	if (p->aue_txstat0)
   1087 		ifp->if_oerrors++;
   1088 
   1089 	if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
   1090 		ifp->if_collisions++;
   1091 }
   1092 
   1093 /*
   1094  * A frame has been uploaded: pass the resulting mbuf chain up to
   1095  * the higher level protocols.
   1096  */
   1097 Static void
   1098 aue_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
   1099 {
   1100 	struct aue_chain	*c = priv;
   1101 	struct aue_softc	*sc = c->aue_sc;
   1102 	struct ifnet		*ifp = GET_IFP(sc);
   1103 	struct mbuf		*m;
   1104 	uint32_t		total_len;
   1105 	struct aue_rxpkt	r;
   1106 	int			s;
   1107 
   1108 	DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
   1109 
   1110 	if (sc->aue_dying)
   1111 		return;
   1112 
   1113 	if (!(ifp->if_flags & IFF_RUNNING))
   1114 		return;
   1115 
   1116 	if (status != USBD_NORMAL_COMPLETION) {
   1117 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   1118 			return;
   1119 		sc->aue_rx_errs++;
   1120 		if (usbd_ratecheck(&sc->aue_rx_notice)) {
   1121 			aprint_error_dev(sc->aue_dev,
   1122 			    "%u usb errors on rx: %s\n", sc->aue_rx_errs,
   1123 			    usbd_errstr(status));
   1124 			sc->aue_rx_errs = 0;
   1125 		}
   1126 		if (status == USBD_STALLED)
   1127 			usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]);
   1128 		goto done;
   1129 	}
   1130 
   1131 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
   1132 
   1133 	memcpy(mtod(c->aue_mbuf, char *), c->aue_buf, total_len);
   1134 
   1135 	if (total_len <= 4 + ETHER_CRC_LEN) {
   1136 		ifp->if_ierrors++;
   1137 		goto done;
   1138 	}
   1139 
   1140 	memcpy(&r, c->aue_buf + total_len - 4, sizeof(r));
   1141 
   1142 	/* Turn off all the non-error bits in the rx status word. */
   1143 	r.aue_rxstat &= AUE_RXSTAT_MASK;
   1144 	if (r.aue_rxstat) {
   1145 		ifp->if_ierrors++;
   1146 		goto done;
   1147 	}
   1148 
   1149 	/* No errors; receive the packet. */
   1150 	m = c->aue_mbuf;
   1151 	total_len -= ETHER_CRC_LEN + 4;
   1152 	m->m_pkthdr.len = m->m_len = total_len;
   1153 	ifp->if_ipackets++;
   1154 
   1155 	m->m_pkthdr.rcvif = ifp;
   1156 
   1157 	s = splnet();
   1158 
   1159 	/* XXX ugly */
   1160 	if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
   1161 		ifp->if_ierrors++;
   1162 		goto done1;
   1163 	}
   1164 
   1165 	/*
   1166 	 * Handle BPF listeners. Let the BPF user see the packet, but
   1167 	 * don't pass it up to the ether_input() layer unless it's
   1168 	 * a broadcast packet, multicast packet, matches our ethernet
   1169 	 * address or the interface is in promiscuous mode.
   1170 	 */
   1171 	bpf_mtap(ifp, m);
   1172 
   1173 	DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->aue_dev),
   1174 		    __func__, m->m_len));
   1175 	(*(ifp)->if_input)((ifp), (m));
   1176  done1:
   1177 	splx(s);
   1178 
   1179  done:
   1180 
   1181 	/* Setup new transfer. */
   1182 	usbd_setup_xfer(xfer, c, c->aue_buf, AUE_BUFSZ,
   1183 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof);
   1184 	usbd_transfer(xfer);
   1185 
   1186 	DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->aue_dev),
   1187 		    __func__));
   1188 }
   1189 
   1190 /*
   1191  * A frame was downloaded to the chip. It's safe for us to clean up
   1192  * the list buffers.
   1193  */
   1194 
   1195 Static void
   1196 aue_txeof(struct usbd_xfer *xfer, void *priv,
   1197     usbd_status status)
   1198 {
   1199 	struct aue_chain	*c = priv;
   1200 	struct aue_softc	*sc = c->aue_sc;
   1201 	struct ifnet		*ifp = GET_IFP(sc);
   1202 	int			s;
   1203 
   1204 	if (sc->aue_dying)
   1205 		return;
   1206 
   1207 	s = splnet();
   1208 
   1209 	DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->aue_dev),
   1210 		    __func__, status));
   1211 
   1212 	ifp->if_timer = 0;
   1213 	ifp->if_flags &= ~IFF_OACTIVE;
   1214 
   1215 	if (status != USBD_NORMAL_COMPLETION) {
   1216 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1217 			splx(s);
   1218 			return;
   1219 		}
   1220 		ifp->if_oerrors++;
   1221 		aprint_error_dev(sc->aue_dev, "usb error on tx: %s\n",
   1222 		    usbd_errstr(status));
   1223 		if (status == USBD_STALLED)
   1224 			usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_TX]);
   1225 		splx(s);
   1226 		return;
   1227 	}
   1228 
   1229 	ifp->if_opackets++;
   1230 
   1231 	m_freem(c->aue_mbuf);
   1232 	c->aue_mbuf = NULL;
   1233 
   1234 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1235 		aue_start(ifp);
   1236 
   1237 	splx(s);
   1238 }
   1239 
   1240 Static void
   1241 aue_tick(void *xsc)
   1242 {
   1243 	struct aue_softc	*sc = xsc;
   1244 
   1245 	DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
   1246 
   1247 	if (sc == NULL)
   1248 		return;
   1249 
   1250 	if (sc->aue_dying)
   1251 		return;
   1252 
   1253 	/* Perform periodic stuff in process context. */
   1254 	usb_add_task(sc->aue_udev, &sc->aue_tick_task, USB_TASKQ_DRIVER);
   1255 }
   1256 
   1257 Static void
   1258 aue_tick_task(void *xsc)
   1259 {
   1260 	struct aue_softc	*sc = xsc;
   1261 	struct ifnet		*ifp;
   1262 	struct mii_data		*mii;
   1263 	int			s;
   1264 
   1265 	DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
   1266 
   1267 	if (sc->aue_dying)
   1268 		return;
   1269 
   1270 	ifp = GET_IFP(sc);
   1271 	mii = GET_MII(sc);
   1272 	if (mii == NULL)
   1273 		return;
   1274 
   1275 	s = splnet();
   1276 
   1277 	mii_tick(mii);
   1278 	if (!sc->aue_link) {
   1279 		mii_pollstat(mii); /* XXX FreeBSD has removed this call */
   1280 		if (mii->mii_media_status & IFM_ACTIVE &&
   1281 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
   1282 			DPRINTFN(2,("%s: %s: got link\n",
   1283 			    device_xname(sc->aue_dev), __func__));
   1284 			sc->aue_link++;
   1285 			if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1286 				aue_start(ifp);
   1287 		}
   1288 	}
   1289 
   1290 	callout_reset(&(sc->aue_stat_ch), (hz), (aue_tick), (sc));
   1291 
   1292 	splx(s);
   1293 }
   1294 
   1295 Static int
   1296 aue_send(struct aue_softc *sc, struct mbuf *m, int idx)
   1297 {
   1298 	int			total_len;
   1299 	struct aue_chain	*c;
   1300 	usbd_status		err;
   1301 
   1302 	DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
   1303 
   1304 	c = &sc->aue_cdata.aue_tx_chain[idx];
   1305 
   1306 	/*
   1307 	 * Copy the mbuf data into a contiguous buffer, leaving two
   1308 	 * bytes at the beginning to hold the frame length.
   1309 	 */
   1310 	m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
   1311 	c->aue_mbuf = m;
   1312 
   1313 	/*
   1314 	 * The ADMtek documentation says that the packet length is
   1315 	 * supposed to be specified in the first two bytes of the
   1316 	 * transfer, however it actually seems to ignore this info
   1317 	 * and base the frame size on the bulk transfer length.
   1318 	 */
   1319 	c->aue_buf[0] = (uint8_t)m->m_pkthdr.len;
   1320 	c->aue_buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
   1321 	total_len = m->m_pkthdr.len + 2;
   1322 
   1323 	usbd_setup_xfer(c->aue_xfer, c, c->aue_buf, total_len,
   1324 	    USBD_FORCE_SHORT_XFER, AUE_TX_TIMEOUT, aue_txeof);
   1325 
   1326 	/* Transmit */
   1327 	err = usbd_transfer(c->aue_xfer);
   1328 	if (err != USBD_IN_PROGRESS) {
   1329 		aprint_error_dev(sc->aue_dev, "aue_send error=%s\n",
   1330 		       usbd_errstr(err));
   1331 		/* Stop the interface from process context. */
   1332 		usb_add_task(sc->aue_udev, &sc->aue_stop_task,
   1333 		    USB_TASKQ_DRIVER);
   1334 		return EIO;
   1335 	}
   1336 	DPRINTFN(5,("%s: %s: send %d bytes\n", device_xname(sc->aue_dev),
   1337 		    __func__, total_len));
   1338 
   1339 	sc->aue_cdata.aue_tx_cnt++;
   1340 
   1341 	return 0;
   1342 }
   1343 
   1344 Static void
   1345 aue_start(struct ifnet *ifp)
   1346 {
   1347 	struct aue_softc	*sc = ifp->if_softc;
   1348 	struct mbuf		*m_head = NULL;
   1349 
   1350 	DPRINTFN(5,("%s: %s: enter, link=%d\n", device_xname(sc->aue_dev),
   1351 		    __func__, sc->aue_link));
   1352 
   1353 	if (sc->aue_dying)
   1354 		return;
   1355 
   1356 	if (!sc->aue_link)
   1357 		return;
   1358 
   1359 	if (ifp->if_flags & IFF_OACTIVE)
   1360 		return;
   1361 
   1362 	IFQ_POLL(&ifp->if_snd, m_head);
   1363 	if (m_head == NULL)
   1364 		return;
   1365 
   1366 	if (aue_send(sc, m_head, 0)) {
   1367 		ifp->if_flags |= IFF_OACTIVE;
   1368 		return;
   1369 	}
   1370 
   1371 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
   1372 
   1373 	/*
   1374 	 * If there's a BPF listener, bounce a copy of this frame
   1375 	 * to him.
   1376 	 */
   1377 	bpf_mtap(ifp, m_head);
   1378 
   1379 	ifp->if_flags |= IFF_OACTIVE;
   1380 
   1381 	/*
   1382 	 * Set a timeout in case the chip goes out to lunch.
   1383 	 */
   1384 	ifp->if_timer = 5;
   1385 }
   1386 
   1387 Static void
   1388 aue_init(void *xsc)
   1389 {
   1390 	struct aue_softc	*sc = xsc;
   1391 	struct ifnet		*ifp = GET_IFP(sc);
   1392 	struct mii_data		*mii = GET_MII(sc);
   1393 	int			i, s;
   1394 	const u_char		*eaddr;
   1395 
   1396 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
   1397 
   1398 	if (sc->aue_dying)
   1399 		return;
   1400 
   1401 	if (ifp->if_flags & IFF_RUNNING)
   1402 		return;
   1403 
   1404 	s = splnet();
   1405 
   1406 	/*
   1407 	 * Cancel pending I/O and free all RX/TX buffers.
   1408 	 */
   1409 	aue_reset(sc);
   1410 
   1411 	eaddr = CLLADDR(ifp->if_sadl);
   1412 	for (i = 0; i < ETHER_ADDR_LEN; i++)
   1413 		aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
   1414 
   1415 	 /* If we want promiscuous mode, set the allframes bit. */
   1416 	if (ifp->if_flags & IFF_PROMISC)
   1417 		AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1418 	else
   1419 		AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1420 
   1421 	if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
   1422 		if (aue_openpipes(sc)) {
   1423 			splx(s);
   1424 			return;
   1425 		}
   1426 	}
   1427 	/* Init TX ring. */
   1428 	if (aue_tx_list_init(sc)) {
   1429 		aprint_error_dev(sc->aue_dev, "tx list init failed\n");
   1430 		splx(s);
   1431 		return;
   1432 	}
   1433 
   1434 	/* Init RX ring. */
   1435 	if (aue_rx_list_init(sc)) {
   1436 		aprint_error_dev(sc->aue_dev, "rx list init failed\n");
   1437 		splx(s);
   1438 		return;
   1439 	}
   1440 
   1441 	/* Start up the receive pipe. */
   1442 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
   1443 		struct aue_chain *c = &sc->aue_cdata.aue_rx_chain[i];
   1444 
   1445 		usbd_setup_xfer(c->aue_xfer, c, c->aue_buf, AUE_BUFSZ,
   1446 		    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof);
   1447 		(void)usbd_transfer(c->aue_xfer); /* XXX */
   1448 		DPRINTFN(5,("%s: %s: start read\n", device_xname(sc->aue_dev),
   1449 			    __func__));
   1450 
   1451 	}
   1452 
   1453 	/* Load the multicast filter. */
   1454 	aue_setmulti(sc);
   1455 
   1456 	/* Enable RX and TX */
   1457 	aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
   1458 	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
   1459 	AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
   1460 
   1461 	mii_mediachg(mii);
   1462 
   1463 	ifp->if_flags |= IFF_RUNNING;
   1464 	ifp->if_flags &= ~IFF_OACTIVE;
   1465 
   1466 	splx(s);
   1467 
   1468 	callout_reset(&(sc->aue_stat_ch), (hz), (aue_tick), (sc));
   1469 }
   1470 
   1471 Static int
   1472 aue_openpipes(struct aue_softc *sc)
   1473 {
   1474 	usbd_status		err;
   1475 
   1476 	/* Open RX and TX pipes. */
   1477 	err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
   1478 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
   1479 	if (err) {
   1480 		aprint_error_dev(sc->aue_dev, "open rx pipe failed: %s\n",
   1481 		    usbd_errstr(err));
   1482 		return EIO;
   1483 	}
   1484 	err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
   1485 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
   1486 	if (err) {
   1487 		aprint_error_dev(sc->aue_dev, "open tx pipe failed: %s\n",
   1488 		    usbd_errstr(err));
   1489 		return EIO;
   1490 	}
   1491 	err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
   1492 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
   1493 	    &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
   1494 	    AUE_INTR_INTERVAL);
   1495 	if (err) {
   1496 		aprint_error_dev(sc->aue_dev, "open intr pipe failed: %s\n",
   1497 		    usbd_errstr(err));
   1498 		return EIO;
   1499 	}
   1500 
   1501 	return 0;
   1502 }
   1503 
   1504 /*
   1505  * Set media options.
   1506  */
   1507 Static int
   1508 aue_ifmedia_upd(struct ifnet *ifp)
   1509 {
   1510 	struct aue_softc	*sc = ifp->if_softc;
   1511 	struct mii_data		*mii = GET_MII(sc);
   1512 	int rc;
   1513 
   1514 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
   1515 
   1516 	if (sc->aue_dying)
   1517 		return 0;
   1518 
   1519 	sc->aue_link = 0;
   1520 
   1521 	if ((rc = mii_mediachg(mii)) == ENXIO)
   1522 		return 0;
   1523 	return rc;
   1524 }
   1525 
   1526 Static int
   1527 aue_ioctl(struct ifnet *ifp, u_long command, void *data)
   1528 {
   1529 	struct aue_softc	*sc = ifp->if_softc;
   1530 	struct ifaddr 		*ifa = (struct ifaddr *)data;
   1531 	struct ifreq		*ifr = (struct ifreq *)data;
   1532 	int			s, error = 0;
   1533 
   1534 	if (sc->aue_dying)
   1535 		return EIO;
   1536 
   1537 	s = splnet();
   1538 
   1539 	switch(command) {
   1540 	case SIOCINITIFADDR:
   1541 		ifp->if_flags |= IFF_UP;
   1542 		aue_init(sc);
   1543 
   1544 		switch (ifa->ifa_addr->sa_family) {
   1545 #ifdef INET
   1546 		case AF_INET:
   1547 			arp_ifinit(ifp, ifa);
   1548 			break;
   1549 #endif /* INET */
   1550 		}
   1551 		break;
   1552 
   1553 	case SIOCSIFMTU:
   1554 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU)
   1555 			error = EINVAL;
   1556 		else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET)
   1557 			error = 0;
   1558 		break;
   1559 
   1560 	case SIOCSIFFLAGS:
   1561 		if ((error = ifioctl_common(ifp, command, data)) != 0)
   1562 			break;
   1563 		if (ifp->if_flags & IFF_UP) {
   1564 			if (ifp->if_flags & IFF_RUNNING &&
   1565 			    ifp->if_flags & IFF_PROMISC &&
   1566 			    !(sc->aue_if_flags & IFF_PROMISC)) {
   1567 				AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1568 			} else if (ifp->if_flags & IFF_RUNNING &&
   1569 			    !(ifp->if_flags & IFF_PROMISC) &&
   1570 			    sc->aue_if_flags & IFF_PROMISC) {
   1571 				AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1572 			} else if (!(ifp->if_flags & IFF_RUNNING))
   1573 				aue_init(sc);
   1574 		} else {
   1575 			if (ifp->if_flags & IFF_RUNNING)
   1576 				aue_stop(sc);
   1577 		}
   1578 		sc->aue_if_flags = ifp->if_flags;
   1579 		error = 0;
   1580 		break;
   1581 	case SIOCADDMULTI:
   1582 	case SIOCDELMULTI:
   1583 	case SIOCGIFMEDIA:
   1584 	case SIOCSIFMEDIA:
   1585 		if ((error = ether_ioctl(ifp, command, data)) == ENETRESET) {
   1586 			if (ifp->if_flags & IFF_RUNNING) {
   1587 				cv_signal(&sc->aue_domc);
   1588 			}
   1589 			error = 0;
   1590 		}
   1591 		break;
   1592 	default:
   1593 		error = ether_ioctl(ifp, command, data);
   1594 		break;
   1595 	}
   1596 
   1597 	splx(s);
   1598 
   1599 	return error;
   1600 }
   1601 
   1602 Static void
   1603 aue_watchdog(struct ifnet *ifp)
   1604 {
   1605 	struct aue_softc	*sc = ifp->if_softc;
   1606 	struct aue_chain	*c;
   1607 	usbd_status		stat;
   1608 	int			s;
   1609 
   1610 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
   1611 
   1612 	ifp->if_oerrors++;
   1613 	aprint_error_dev(sc->aue_dev, "watchdog timeout\n");
   1614 
   1615 	s = splusb();
   1616 	c = &sc->aue_cdata.aue_tx_chain[0];
   1617 	usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat);
   1618 	aue_txeof(c->aue_xfer, c, stat);
   1619 
   1620 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1621 		aue_start(ifp);
   1622 	splx(s);
   1623 }
   1624 
   1625 /*
   1626  * Stop the adapter and free any mbufs allocated to the
   1627  * RX and TX lists.
   1628  */
   1629 Static void
   1630 aue_stop(struct aue_softc *sc)
   1631 {
   1632 	usbd_status		err;
   1633 	struct ifnet		*ifp;
   1634 	int			i;
   1635 
   1636 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
   1637 
   1638 	ifp = GET_IFP(sc);
   1639 	ifp->if_timer = 0;
   1640 
   1641 	aue_csr_write_1(sc, AUE_CTL0, 0);
   1642 	aue_csr_write_1(sc, AUE_CTL1, 0);
   1643 	aue_reset(sc);
   1644 	callout_stop(&sc->aue_stat_ch);
   1645 
   1646 	/* Stop transfers. */
   1647 	if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
   1648 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
   1649 		if (err) {
   1650 			printf("%s: abort rx pipe failed: %s\n",
   1651 			    device_xname(sc->aue_dev), usbd_errstr(err));
   1652 		}
   1653 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
   1654 		if (err) {
   1655 			printf("%s: close rx pipe failed: %s\n",
   1656 			    device_xname(sc->aue_dev), usbd_errstr(err));
   1657 		}
   1658 		sc->aue_ep[AUE_ENDPT_RX] = NULL;
   1659 	}
   1660 
   1661 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
   1662 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
   1663 		if (err) {
   1664 			printf("%s: abort tx pipe failed: %s\n",
   1665 			    device_xname(sc->aue_dev), usbd_errstr(err));
   1666 		}
   1667 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
   1668 		if (err) {
   1669 			printf("%s: close tx pipe failed: %s\n",
   1670 			    device_xname(sc->aue_dev), usbd_errstr(err));
   1671 		}
   1672 		sc->aue_ep[AUE_ENDPT_TX] = NULL;
   1673 	}
   1674 
   1675 	if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
   1676 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
   1677 		if (err) {
   1678 			printf("%s: abort intr pipe failed: %s\n",
   1679 			    device_xname(sc->aue_dev), usbd_errstr(err));
   1680 		}
   1681 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
   1682 		if (err) {
   1683 			printf("%s: close intr pipe failed: %s\n",
   1684 			    device_xname(sc->aue_dev), usbd_errstr(err));
   1685 		}
   1686 		sc->aue_ep[AUE_ENDPT_INTR] = NULL;
   1687 	}
   1688 
   1689 	/* Free RX resources. */
   1690 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
   1691 		if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
   1692 			m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
   1693 			sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
   1694 		}
   1695 		if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
   1696 			usbd_destroy_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
   1697 			sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
   1698 		}
   1699 	}
   1700 
   1701 	/* Free TX resources. */
   1702 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
   1703 		if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
   1704 			m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
   1705 			sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
   1706 		}
   1707 		if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
   1708 			usbd_destroy_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
   1709 			sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
   1710 		}
   1711 	}
   1712 
   1713 	sc->aue_link = 0;
   1714 
   1715 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1716 }
   1717 
   1718 Static void
   1719 aue_multithread(void *arg)
   1720 {
   1721 	struct aue_softc *sc;
   1722 	int s;
   1723 
   1724 	sc = (struct aue_softc *)arg;
   1725 
   1726 	while (1) {
   1727 		mutex_enter(&sc->aue_mcmtx);
   1728 		cv_wait(&sc->aue_domc,&sc->aue_mcmtx);
   1729 		mutex_exit(&sc->aue_mcmtx);
   1730 
   1731 		if (sc->aue_closing)
   1732 			break;
   1733 
   1734 		s = splnet();
   1735 		aue_init(sc);
   1736 		/* XXX called by aue_init, but rc ifconfig hangs without it: */
   1737 		aue_setmulti(sc);
   1738 		splx(s);
   1739 	}
   1740 
   1741 	cv_signal(&sc->aue_closemc);
   1742 
   1743 	kthread_exit(0);
   1744 }
   1745