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