Home | History | Annotate | Line # | Download | only in usb
if_aue.c revision 1.110.10.2
      1 /*	$NetBSD: if_aue.c,v 1.110.10.2 2009/09/16 13:37:57 yamt 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.110.10.2 2009/09/16 13:37:57 yamt 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 	devinfop = usbd_devinfo_alloc(uaa->device, 0);
    746 	aprint_naive("\n");
    747 	aprint_normal("\n");
    748 	aprint_normal_dev(self, "%s\n", devinfop);
    749 	usbd_devinfo_free(devinfop);
    750 
    751 	err = usbd_set_config_no(dev, AUE_CONFIG_NO, 1);
    752 	if (err) {
    753 		aprint_error_dev(self, "setting config no failed\n");
    754 		return;
    755 	}
    756 
    757 	usb_init_task(&sc->aue_tick_task, aue_tick_task, sc);
    758 	usb_init_task(&sc->aue_stop_task, (void (*)(void *))aue_stop, sc);
    759 	mutex_init(&sc->aue_mii_lock, MUTEX_DEFAULT, IPL_NONE);
    760 
    761 	err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &iface);
    762 	if (err) {
    763 		aprint_error_dev(self, "getting interface handle failed\n");
    764 		return;
    765 	}
    766 	sc->aue_closing = 0;
    767 
    768 	mutex_init(&sc->aue_mcmtx, MUTEX_DRIVER, IPL_NET);
    769 	cv_init(&sc->aue_domc, "auemc");
    770 	cv_init(&sc->aue_closemc, "auemccl");
    771 
    772 	err = kthread_create(PRI_NONE, 0, NULL,
    773 		aue_multithread, sc, &sc->aue_thread,
    774 		"%s-mc", device_xname(sc->aue_dev));
    775 
    776 	if (err) {
    777 		aprint_error_dev(self,
    778 		    "creating multicast configuration thread\n");
    779 		return;
    780 	}
    781 	sc->aue_flags = aue_lookup(uaa->vendor, uaa->product)->aue_flags;
    782 
    783 	sc->aue_udev = dev;
    784 	sc->aue_iface = iface;
    785 	sc->aue_product = uaa->product;
    786 	sc->aue_vendor = uaa->vendor;
    787 
    788 	id = usbd_get_interface_descriptor(iface);
    789 
    790 	/* Find endpoints. */
    791 	for (i = 0; i < id->bNumEndpoints; i++) {
    792 		ed = usbd_interface2endpoint_descriptor(iface, i);
    793 		if (ed == NULL) {
    794 			aprint_error_dev(self,
    795 			    "couldn't get endpoint descriptor %d\n", i);
    796 			return;
    797 		}
    798 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    799 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    800 			sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
    801 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    802 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    803 			sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
    804 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    805 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    806 			sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
    807 		}
    808 	}
    809 
    810 	if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 ||
    811 	    sc->aue_ed[AUE_ENDPT_INTR] == 0) {
    812 		aprint_error_dev(self, "missing endpoint\n");
    813 		return;
    814 	}
    815 
    816 
    817 	s = splnet();
    818 
    819 	/* Reset the adapter. */
    820 	aue_reset(sc);
    821 
    822 	/*
    823 	 * Get station address from the EEPROM.
    824 	 */
    825 	aue_read_mac(sc, eaddr);
    826 
    827 	/*
    828 	 * A Pegasus chip was detected. Inform the world.
    829 	 */
    830 	ifp = GET_IFP(sc);
    831 	aprint_error_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
    832 
    833 	/* Initialize interface info.*/
    834 	ifp->if_softc = sc;
    835 	ifp->if_mtu = ETHERMTU;
    836 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    837 	ifp->if_ioctl = aue_ioctl;
    838 	ifp->if_start = aue_start;
    839 	ifp->if_watchdog = aue_watchdog;
    840 	strncpy(ifp->if_xname, device_xname(sc->aue_dev), IFNAMSIZ);
    841 
    842 	IFQ_SET_READY(&ifp->if_snd);
    843 
    844 	/* Initialize MII/media info. */
    845 	mii = &sc->aue_mii;
    846 	mii->mii_ifp = ifp;
    847 	mii->mii_readreg = aue_miibus_readreg;
    848 	mii->mii_writereg = aue_miibus_writereg;
    849 	mii->mii_statchg = aue_miibus_statchg;
    850 	mii->mii_flags = MIIF_AUTOTSLEEP;
    851 	sc->aue_ec.ec_mii = mii;
    852 	ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, ether_mediastatus);
    853 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
    854 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
    855 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
    856 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
    857 	} else
    858 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    859 
    860 	/* Attach the interface. */
    861 	if_attach(ifp);
    862 	ether_ifattach(ifp, eaddr);
    863 #if NRND > 0
    864 	rnd_attach_source(&sc->rnd_source, device_xname(sc->aue_dev),
    865 	    RND_TYPE_NET, 0);
    866 #endif
    867 
    868 	callout_init(&(sc->aue_stat_ch), 0);
    869 
    870 	sc->aue_attached = 1;
    871 	splx(s);
    872 
    873 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->aue_udev, sc->aue_dev);
    874 
    875 	return;
    876 }
    877 
    878 int
    879 aue_detach(device_t self, int flags)
    880 {
    881 	struct aue_softc *sc = device_private(self);
    882 	struct ifnet		*ifp = GET_IFP(sc);
    883 	int			s;
    884 
    885 	DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
    886 
    887 	if (!sc->aue_attached) {
    888 		/* Detached before attached finished, so just bail out. */
    889 		return (0);
    890 	}
    891 
    892 	callout_stop(&(sc->aue_stat_ch));
    893 	/*
    894 	 * Remove any pending tasks.  They cannot be executing because they run
    895 	 * in the same thread as detach.
    896 	 */
    897 	usb_rem_task(sc->aue_udev, &sc->aue_tick_task);
    898 	usb_rem_task(sc->aue_udev, &sc->aue_stop_task);
    899 
    900 	sc->aue_closing = 1;
    901 	cv_signal(&sc->aue_domc);
    902 
    903 	mutex_enter(&sc->aue_mcmtx);
    904 	cv_wait(&sc->aue_closemc,&sc->aue_mcmtx);
    905 	mutex_exit(&sc->aue_mcmtx);
    906 
    907 	mutex_destroy(&sc->aue_mcmtx);
    908 	cv_destroy(&sc->aue_domc);
    909 	cv_destroy(&sc->aue_closemc);
    910 
    911 	s = splusb();
    912 
    913 	if (ifp->if_flags & IFF_RUNNING)
    914 		aue_stop(sc);
    915 
    916 #if NRND > 0
    917 	rnd_detach_source(&sc->rnd_source);
    918 #endif
    919 	mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY);
    920 	ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY);
    921 	ether_ifdetach(ifp);
    922 
    923 	if_detach(ifp);
    924 
    925 #ifdef DIAGNOSTIC
    926 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL ||
    927 	    sc->aue_ep[AUE_ENDPT_RX] != NULL ||
    928 	    sc->aue_ep[AUE_ENDPT_INTR] != NULL)
    929 		aprint_error_dev(self, "detach has active endpoints\n");
    930 #endif
    931 
    932 	sc->aue_attached = 0;
    933 
    934 	if (--sc->aue_refcnt >= 0) {
    935 		/* Wait for processes to go away. */
    936 		usb_detach_wait((sc->aue_dev));
    937 	}
    938 	splx(s);
    939 
    940 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->aue_udev, sc->aue_dev);
    941 
    942 	mutex_destroy(&sc->aue_mii_lock);
    943 #if 0
    944 	mutex_destroy(&sc->wkmtx);
    945 #endif
    946 	return (0);
    947 }
    948 
    949 int
    950 aue_activate(device_t self, enum devact act)
    951 {
    952 	struct aue_softc *sc = device_private(self);
    953 
    954 	DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
    955 
    956 	switch (act) {
    957 	case DVACT_ACTIVATE:
    958 		return (EOPNOTSUPP);
    959 		break;
    960 
    961 	case DVACT_DEACTIVATE:
    962 		if_deactivate(&sc->aue_ec.ec_if);
    963 		sc->aue_dying = 1;
    964 		break;
    965 	}
    966 	return (0);
    967 }
    968 
    969 /*
    970  * Initialize an RX descriptor and attach an MBUF cluster.
    971  */
    972 Static int
    973 aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m)
    974 {
    975 	struct mbuf		*m_new = NULL;
    976 
    977 	DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
    978 
    979 	if (m == NULL) {
    980 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    981 		if (m_new == NULL) {
    982 			aprint_error_dev(sc->aue_dev, "no memory for rx list "
    983 			    "-- packet dropped!\n");
    984 			return (ENOBUFS);
    985 		}
    986 
    987 		MCLGET(m_new, M_DONTWAIT);
    988 		if (!(m_new->m_flags & M_EXT)) {
    989 			aprint_error_dev(sc->aue_dev, "no memory for rx "
    990 			    "list -- packet dropped!\n");
    991 			m_freem(m_new);
    992 			return (ENOBUFS);
    993 		}
    994 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    995 	} else {
    996 		m_new = m;
    997 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    998 		m_new->m_data = m_new->m_ext.ext_buf;
    999 	}
   1000 
   1001 	m_adj(m_new, ETHER_ALIGN);
   1002 	c->aue_mbuf = m_new;
   1003 
   1004 	return (0);
   1005 }
   1006 
   1007 Static int
   1008 aue_rx_list_init(struct aue_softc *sc)
   1009 {
   1010 	struct aue_cdata	*cd;
   1011 	struct aue_chain	*c;
   1012 	int			i;
   1013 
   1014 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
   1015 
   1016 	cd = &sc->aue_cdata;
   1017 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
   1018 		c = &cd->aue_rx_chain[i];
   1019 		c->aue_sc = sc;
   1020 		c->aue_idx = i;
   1021 		if (aue_newbuf(sc, c, NULL) == ENOBUFS)
   1022 			return (ENOBUFS);
   1023 		if (c->aue_xfer == NULL) {
   1024 			c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
   1025 			if (c->aue_xfer == NULL)
   1026 				return (ENOBUFS);
   1027 			c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
   1028 			if (c->aue_buf == NULL)
   1029 				return (ENOBUFS); /* XXX free xfer */
   1030 		}
   1031 	}
   1032 
   1033 	return (0);
   1034 }
   1035 
   1036 Static int
   1037 aue_tx_list_init(struct aue_softc *sc)
   1038 {
   1039 	struct aue_cdata	*cd;
   1040 	struct aue_chain	*c;
   1041 	int			i;
   1042 
   1043 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
   1044 
   1045 	cd = &sc->aue_cdata;
   1046 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
   1047 		c = &cd->aue_tx_chain[i];
   1048 		c->aue_sc = sc;
   1049 		c->aue_idx = i;
   1050 		c->aue_mbuf = NULL;
   1051 		if (c->aue_xfer == NULL) {
   1052 			c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
   1053 			if (c->aue_xfer == NULL)
   1054 				return (ENOBUFS);
   1055 			c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
   1056 			if (c->aue_buf == NULL)
   1057 				return (ENOBUFS);
   1058 		}
   1059 	}
   1060 
   1061 	return (0);
   1062 }
   1063 
   1064 Static void
   1065 aue_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
   1066     usbd_status status)
   1067 {
   1068 	struct aue_softc	*sc = priv;
   1069 	struct ifnet		*ifp = GET_IFP(sc);
   1070 	struct aue_intrpkt	*p = &sc->aue_cdata.aue_ibuf;
   1071 
   1072 	DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
   1073 
   1074 	if (sc->aue_dying)
   1075 		return;
   1076 
   1077 	if (!(ifp->if_flags & IFF_RUNNING))
   1078 		return;
   1079 
   1080 	if (status != USBD_NORMAL_COMPLETION) {
   1081 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1082 			return;
   1083 		}
   1084 		sc->aue_intr_errs++;
   1085 		if (usbd_ratecheck(&sc->aue_rx_notice)) {
   1086 			aprint_error_dev(sc->aue_dev,
   1087 			    "%u usb errors on intr: %s\n", sc->aue_intr_errs,
   1088 			    usbd_errstr(status));
   1089 			sc->aue_intr_errs = 0;
   1090 		}
   1091 		if (status == USBD_STALLED)
   1092 			usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]);
   1093 		return;
   1094 	}
   1095 
   1096 	if (p->aue_txstat0)
   1097 		ifp->if_oerrors++;
   1098 
   1099 	if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
   1100 		ifp->if_collisions++;
   1101 }
   1102 
   1103 /*
   1104  * A frame has been uploaded: pass the resulting mbuf chain up to
   1105  * the higher level protocols.
   1106  */
   1107 Static void
   1108 aue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
   1109 {
   1110 	struct aue_chain	*c = priv;
   1111 	struct aue_softc	*sc = c->aue_sc;
   1112 	struct ifnet		*ifp = GET_IFP(sc);
   1113 	struct mbuf		*m;
   1114 	u_int32_t		total_len;
   1115 	struct aue_rxpkt	r;
   1116 	int			s;
   1117 
   1118 	DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
   1119 
   1120 	if (sc->aue_dying)
   1121 		return;
   1122 
   1123 	if (!(ifp->if_flags & IFF_RUNNING))
   1124 		return;
   1125 
   1126 	if (status != USBD_NORMAL_COMPLETION) {
   1127 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   1128 			return;
   1129 		sc->aue_rx_errs++;
   1130 		if (usbd_ratecheck(&sc->aue_rx_notice)) {
   1131 			aprint_error_dev(sc->aue_dev,
   1132 			    "%u usb errors on rx: %s\n", sc->aue_rx_errs,
   1133 			    usbd_errstr(status));
   1134 			sc->aue_rx_errs = 0;
   1135 		}
   1136 		if (status == USBD_STALLED)
   1137 			usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]);
   1138 		goto done;
   1139 	}
   1140 
   1141 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
   1142 
   1143 	memcpy(mtod(c->aue_mbuf, char *), c->aue_buf, total_len);
   1144 
   1145 	if (total_len <= 4 + ETHER_CRC_LEN) {
   1146 		ifp->if_ierrors++;
   1147 		goto done;
   1148 	}
   1149 
   1150 	memcpy(&r, c->aue_buf + total_len - 4, sizeof(r));
   1151 
   1152 	/* Turn off all the non-error bits in the rx status word. */
   1153 	r.aue_rxstat &= AUE_RXSTAT_MASK;
   1154 	if (r.aue_rxstat) {
   1155 		ifp->if_ierrors++;
   1156 		goto done;
   1157 	}
   1158 
   1159 	/* No errors; receive the packet. */
   1160 	m = c->aue_mbuf;
   1161 	total_len -= ETHER_CRC_LEN + 4;
   1162 	m->m_pkthdr.len = m->m_len = total_len;
   1163 	ifp->if_ipackets++;
   1164 
   1165 	m->m_pkthdr.rcvif = ifp;
   1166 
   1167 	s = splnet();
   1168 
   1169 	/* XXX ugly */
   1170 	if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
   1171 		ifp->if_ierrors++;
   1172 		goto done1;
   1173 	}
   1174 
   1175 #if NBPFILTER > 0
   1176 	/*
   1177 	 * Handle BPF listeners. Let the BPF user see the packet, but
   1178 	 * don't pass it up to the ether_input() layer unless it's
   1179 	 * a broadcast packet, multicast packet, matches our ethernet
   1180 	 * address or the interface is in promiscuous mode.
   1181 	 */
   1182 	if (ifp->if_bpf)
   1183 		BPF_MTAP(ifp, m);
   1184 #endif
   1185 
   1186 	DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->aue_dev),
   1187 		    __func__, m->m_len));
   1188 	(*(ifp)->if_input)((ifp), (m));
   1189  done1:
   1190 	splx(s);
   1191 
   1192  done:
   1193 
   1194 	/* Setup new transfer. */
   1195 	usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
   1196 	    c, c->aue_buf, AUE_BUFSZ,
   1197 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
   1198 	    USBD_NO_TIMEOUT, aue_rxeof);
   1199 	usbd_transfer(xfer);
   1200 
   1201 	DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->aue_dev),
   1202 		    __func__));
   1203 }
   1204 
   1205 /*
   1206  * A frame was downloaded to the chip. It's safe for us to clean up
   1207  * the list buffers.
   1208  */
   1209 
   1210 Static void
   1211 aue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
   1212     usbd_status status)
   1213 {
   1214 	struct aue_chain	*c = priv;
   1215 	struct aue_softc	*sc = c->aue_sc;
   1216 	struct ifnet		*ifp = GET_IFP(sc);
   1217 	int			s;
   1218 
   1219 	if (sc->aue_dying)
   1220 		return;
   1221 
   1222 	s = splnet();
   1223 
   1224 	DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->aue_dev),
   1225 		    __func__, status));
   1226 
   1227 	ifp->if_timer = 0;
   1228 	ifp->if_flags &= ~IFF_OACTIVE;
   1229 
   1230 	if (status != USBD_NORMAL_COMPLETION) {
   1231 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1232 			splx(s);
   1233 			return;
   1234 		}
   1235 		ifp->if_oerrors++;
   1236 		aprint_error_dev(sc->aue_dev, "usb error on tx: %s\n",
   1237 		    usbd_errstr(status));
   1238 		if (status == USBD_STALLED)
   1239 			usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_TX]);
   1240 		splx(s);
   1241 		return;
   1242 	}
   1243 
   1244 	ifp->if_opackets++;
   1245 
   1246 	m_freem(c->aue_mbuf);
   1247 	c->aue_mbuf = NULL;
   1248 
   1249 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1250 		aue_start(ifp);
   1251 
   1252 	splx(s);
   1253 }
   1254 
   1255 Static void
   1256 aue_tick(void *xsc)
   1257 {
   1258 	struct aue_softc	*sc = xsc;
   1259 
   1260 	DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
   1261 
   1262 	if (sc == NULL)
   1263 		return;
   1264 
   1265 	if (sc->aue_dying)
   1266 		return;
   1267 
   1268 	/* Perform periodic stuff in process context. */
   1269 	usb_add_task(sc->aue_udev, &sc->aue_tick_task, USB_TASKQ_DRIVER);
   1270 }
   1271 
   1272 Static void
   1273 aue_tick_task(void *xsc)
   1274 {
   1275 	struct aue_softc	*sc = xsc;
   1276 	struct ifnet		*ifp;
   1277 	struct mii_data		*mii;
   1278 	int			s;
   1279 
   1280 	DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
   1281 
   1282 	if (sc->aue_dying)
   1283 		return;
   1284 
   1285 	ifp = GET_IFP(sc);
   1286 	mii = GET_MII(sc);
   1287 	if (mii == NULL)
   1288 		return;
   1289 
   1290 	s = splnet();
   1291 
   1292 	mii_tick(mii);
   1293 	if (!sc->aue_link) {
   1294 		mii_pollstat(mii); /* XXX FreeBSD has removed this call */
   1295 		if (mii->mii_media_status & IFM_ACTIVE &&
   1296 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
   1297 			DPRINTFN(2,("%s: %s: got link\n",
   1298 			    device_xname(sc->aue_dev), __func__));
   1299 			sc->aue_link++;
   1300 			if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1301 				aue_start(ifp);
   1302 		}
   1303 	}
   1304 
   1305 	callout_reset(&(sc->aue_stat_ch), (hz), (aue_tick), (sc));
   1306 
   1307 	splx(s);
   1308 }
   1309 
   1310 Static int
   1311 aue_send(struct aue_softc *sc, struct mbuf *m, int idx)
   1312 {
   1313 	int			total_len;
   1314 	struct aue_chain	*c;
   1315 	usbd_status		err;
   1316 
   1317 	DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
   1318 
   1319 	c = &sc->aue_cdata.aue_tx_chain[idx];
   1320 
   1321 	/*
   1322 	 * Copy the mbuf data into a contiguous buffer, leaving two
   1323 	 * bytes at the beginning to hold the frame length.
   1324 	 */
   1325 	m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
   1326 	c->aue_mbuf = m;
   1327 
   1328 	/*
   1329 	 * The ADMtek documentation says that the packet length is
   1330 	 * supposed to be specified in the first two bytes of the
   1331 	 * transfer, however it actually seems to ignore this info
   1332 	 * and base the frame size on the bulk transfer length.
   1333 	 */
   1334 	c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
   1335 	c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
   1336 	total_len = m->m_pkthdr.len + 2;
   1337 
   1338 	usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
   1339 	    c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
   1340 	    AUE_TX_TIMEOUT, aue_txeof);
   1341 
   1342 	/* Transmit */
   1343 	err = usbd_transfer(c->aue_xfer);
   1344 	if (err != USBD_IN_PROGRESS) {
   1345 		aprint_error_dev(sc->aue_dev, "aue_send error=%s\n",
   1346 		       usbd_errstr(err));
   1347 		/* Stop the interface from process context. */
   1348 		usb_add_task(sc->aue_udev, &sc->aue_stop_task,
   1349 		    USB_TASKQ_DRIVER);
   1350 		return (EIO);
   1351 	}
   1352 	DPRINTFN(5,("%s: %s: send %d bytes\n", device_xname(sc->aue_dev),
   1353 		    __func__, total_len));
   1354 
   1355 	sc->aue_cdata.aue_tx_cnt++;
   1356 
   1357 	return (0);
   1358 }
   1359 
   1360 Static void
   1361 aue_start(struct ifnet *ifp)
   1362 {
   1363 	struct aue_softc	*sc = ifp->if_softc;
   1364 	struct mbuf		*m_head = NULL;
   1365 
   1366 	DPRINTFN(5,("%s: %s: enter, link=%d\n", device_xname(sc->aue_dev),
   1367 		    __func__, sc->aue_link));
   1368 
   1369 	if (sc->aue_dying)
   1370 		return;
   1371 
   1372 	if (!sc->aue_link)
   1373 		return;
   1374 
   1375 	if (ifp->if_flags & IFF_OACTIVE)
   1376 		return;
   1377 
   1378 	IFQ_POLL(&ifp->if_snd, m_head);
   1379 	if (m_head == NULL)
   1380 		return;
   1381 
   1382 	if (aue_send(sc, m_head, 0)) {
   1383 		ifp->if_flags |= IFF_OACTIVE;
   1384 		return;
   1385 	}
   1386 
   1387 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
   1388 
   1389 #if NBPFILTER > 0
   1390 	/*
   1391 	 * If there's a BPF listener, bounce a copy of this frame
   1392 	 * to him.
   1393 	 */
   1394 	if (ifp->if_bpf)
   1395 		BPF_MTAP(ifp, m_head);
   1396 #endif
   1397 
   1398 	ifp->if_flags |= IFF_OACTIVE;
   1399 
   1400 	/*
   1401 	 * Set a timeout in case the chip goes out to lunch.
   1402 	 */
   1403 	ifp->if_timer = 5;
   1404 }
   1405 
   1406 Static void
   1407 aue_init(void *xsc)
   1408 {
   1409 	struct aue_softc	*sc = xsc;
   1410 	struct ifnet		*ifp = GET_IFP(sc);
   1411 	struct mii_data		*mii = GET_MII(sc);
   1412 	int			i, s;
   1413 	const u_char		*eaddr;
   1414 
   1415 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
   1416 
   1417 	if (sc->aue_dying)
   1418 		return;
   1419 
   1420 	if (ifp->if_flags & IFF_RUNNING)
   1421 		return;
   1422 
   1423 	s = splnet();
   1424 
   1425 	/*
   1426 	 * Cancel pending I/O and free all RX/TX buffers.
   1427 	 */
   1428 	aue_reset(sc);
   1429 
   1430 	eaddr = CLLADDR(ifp->if_sadl);
   1431 	for (i = 0; i < ETHER_ADDR_LEN; i++)
   1432 		aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
   1433 
   1434 	 /* If we want promiscuous mode, set the allframes bit. */
   1435 	if (ifp->if_flags & IFF_PROMISC)
   1436 		AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1437 	else
   1438 		AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1439 
   1440 	/* Init TX ring. */
   1441 	if (aue_tx_list_init(sc) == ENOBUFS) {
   1442 		aprint_error_dev(sc->aue_dev, "tx list init failed\n");
   1443 		splx(s);
   1444 		return;
   1445 	}
   1446 
   1447 	/* Init RX ring. */
   1448 	if (aue_rx_list_init(sc) == ENOBUFS) {
   1449 		aprint_error_dev(sc->aue_dev, "rx list init failed\n");
   1450 		splx(s);
   1451 		return;
   1452 	}
   1453 
   1454 	/* Load the multicast filter. */
   1455 	aue_setmulti(sc);
   1456 
   1457 	/* Enable RX and TX */
   1458 	aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
   1459 	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
   1460 	AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
   1461 
   1462 	mii_mediachg(mii);
   1463 
   1464 	if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
   1465 		if (aue_openpipes(sc)) {
   1466 			splx(s);
   1467 			return;
   1468 		}
   1469 	}
   1470 
   1471 	ifp->if_flags |= IFF_RUNNING;
   1472 	ifp->if_flags &= ~IFF_OACTIVE;
   1473 
   1474 	splx(s);
   1475 
   1476 	callout_reset(&(sc->aue_stat_ch), (hz), (aue_tick), (sc));
   1477 }
   1478 
   1479 Static int
   1480 aue_openpipes(struct aue_softc *sc)
   1481 {
   1482 	struct aue_chain	*c;
   1483 	usbd_status		err;
   1484 	int i;
   1485 
   1486 	/* Open RX and TX pipes. */
   1487 	err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
   1488 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
   1489 	if (err) {
   1490 		aprint_error_dev(sc->aue_dev, "open rx pipe failed: %s\n",
   1491 		    usbd_errstr(err));
   1492 		return (EIO);
   1493 	}
   1494 	err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
   1495 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
   1496 	if (err) {
   1497 		aprint_error_dev(sc->aue_dev, "open tx pipe failed: %s\n",
   1498 		    usbd_errstr(err));
   1499 		return (EIO);
   1500 	}
   1501 	err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
   1502 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
   1503 	    &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
   1504 	    AUE_INTR_INTERVAL);
   1505 	if (err) {
   1506 		aprint_error_dev(sc->aue_dev, "open intr pipe failed: %s\n",
   1507 		    usbd_errstr(err));
   1508 		return (EIO);
   1509 	}
   1510 
   1511 	/* Start up the receive pipe. */
   1512 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
   1513 		c = &sc->aue_cdata.aue_rx_chain[i];
   1514 		usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
   1515 		    c, c->aue_buf, AUE_BUFSZ,
   1516 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
   1517 		    aue_rxeof);
   1518 		(void)usbd_transfer(c->aue_xfer); /* XXX */
   1519 		DPRINTFN(5,("%s: %s: start read\n", device_xname(sc->aue_dev),
   1520 			    __func__));
   1521 
   1522 	}
   1523 	return (0);
   1524 }
   1525 
   1526 /*
   1527  * Set media options.
   1528  */
   1529 Static int
   1530 aue_ifmedia_upd(struct ifnet *ifp)
   1531 {
   1532 	struct aue_softc	*sc = ifp->if_softc;
   1533 	struct mii_data		*mii = GET_MII(sc);
   1534 	int rc;
   1535 
   1536 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
   1537 
   1538 	if (sc->aue_dying)
   1539 		return (0);
   1540 
   1541 	sc->aue_link = 0;
   1542 
   1543 	if ((rc = mii_mediachg(mii)) == ENXIO)
   1544 		return 0;
   1545 	return rc;
   1546 }
   1547 
   1548 Static int
   1549 aue_ioctl(struct ifnet *ifp, u_long command, void *data)
   1550 {
   1551 	struct aue_softc	*sc = ifp->if_softc;
   1552 	struct ifaddr 		*ifa = (struct ifaddr *)data;
   1553 	struct ifreq		*ifr = (struct ifreq *)data;
   1554 	int			s, error = 0;
   1555 
   1556 	if (sc->aue_dying)
   1557 		return (EIO);
   1558 
   1559 	s = splnet();
   1560 
   1561 	switch(command) {
   1562 	case SIOCINITIFADDR:
   1563 		ifp->if_flags |= IFF_UP;
   1564 		aue_init(sc);
   1565 
   1566 		switch (ifa->ifa_addr->sa_family) {
   1567 #ifdef INET
   1568 		case AF_INET:
   1569 			arp_ifinit(ifp, ifa);
   1570 			break;
   1571 #endif /* INET */
   1572 		}
   1573 		break;
   1574 
   1575 	case SIOCSIFMTU:
   1576 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU)
   1577 			error = EINVAL;
   1578 		else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET)
   1579 			error = 0;
   1580 		break;
   1581 
   1582 	case SIOCSIFFLAGS:
   1583 		if ((error = ifioctl_common(ifp, command, data)) != 0)
   1584 			break;
   1585 		if (ifp->if_flags & IFF_UP) {
   1586 			if (ifp->if_flags & IFF_RUNNING &&
   1587 			    ifp->if_flags & IFF_PROMISC &&
   1588 			    !(sc->aue_if_flags & IFF_PROMISC)) {
   1589 				AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1590 			} else if (ifp->if_flags & IFF_RUNNING &&
   1591 			    !(ifp->if_flags & IFF_PROMISC) &&
   1592 			    sc->aue_if_flags & IFF_PROMISC) {
   1593 				AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1594 			} else if (!(ifp->if_flags & IFF_RUNNING))
   1595 				aue_init(sc);
   1596 		} else {
   1597 			if (ifp->if_flags & IFF_RUNNING)
   1598 				aue_stop(sc);
   1599 		}
   1600 		sc->aue_if_flags = ifp->if_flags;
   1601 		error = 0;
   1602 		break;
   1603 	case SIOCADDMULTI:
   1604 	case SIOCDELMULTI:
   1605 	case SIOCGIFMEDIA:
   1606 	case SIOCSIFMEDIA:
   1607 		if ((error = ether_ioctl(ifp, command, data)) == ENETRESET) {
   1608 			if (ifp->if_flags & IFF_RUNNING) {
   1609 				cv_signal(&sc->aue_domc);
   1610 			}
   1611 			error = 0;
   1612 		}
   1613 		break;
   1614 	default:
   1615 		error = ether_ioctl(ifp, command, data);
   1616 		break;
   1617 	}
   1618 
   1619 	splx(s);
   1620 
   1621 	return (error);
   1622 }
   1623 
   1624 Static void
   1625 aue_watchdog(struct ifnet *ifp)
   1626 {
   1627 	struct aue_softc	*sc = ifp->if_softc;
   1628 	struct aue_chain	*c;
   1629 	usbd_status		stat;
   1630 	int			s;
   1631 
   1632 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
   1633 
   1634 	ifp->if_oerrors++;
   1635 	aprint_error_dev(sc->aue_dev, "watchdog timeout\n");
   1636 
   1637 	s = splusb();
   1638 	c = &sc->aue_cdata.aue_tx_chain[0];
   1639 	usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat);
   1640 	aue_txeof(c->aue_xfer, c, stat);
   1641 
   1642 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1643 		aue_start(ifp);
   1644 	splx(s);
   1645 }
   1646 
   1647 /*
   1648  * Stop the adapter and free any mbufs allocated to the
   1649  * RX and TX lists.
   1650  */
   1651 Static void
   1652 aue_stop(struct aue_softc *sc)
   1653 {
   1654 	usbd_status		err;
   1655 	struct ifnet		*ifp;
   1656 	int			i;
   1657 
   1658 	DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
   1659 
   1660 	ifp = GET_IFP(sc);
   1661 	ifp->if_timer = 0;
   1662 
   1663 	aue_csr_write_1(sc, AUE_CTL0, 0);
   1664 	aue_csr_write_1(sc, AUE_CTL1, 0);
   1665 	aue_reset(sc);
   1666 	callout_stop(&(sc->aue_stat_ch));
   1667 
   1668 	/* Stop transfers. */
   1669 	if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
   1670 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
   1671 		if (err) {
   1672 			printf("%s: abort rx pipe failed: %s\n",
   1673 			    device_xname(sc->aue_dev), usbd_errstr(err));
   1674 		}
   1675 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
   1676 		if (err) {
   1677 			printf("%s: close rx pipe failed: %s\n",
   1678 			    device_xname(sc->aue_dev), usbd_errstr(err));
   1679 		}
   1680 		sc->aue_ep[AUE_ENDPT_RX] = NULL;
   1681 	}
   1682 
   1683 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
   1684 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
   1685 		if (err) {
   1686 			printf("%s: abort tx pipe failed: %s\n",
   1687 			    device_xname(sc->aue_dev), usbd_errstr(err));
   1688 		}
   1689 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
   1690 		if (err) {
   1691 			printf("%s: close tx pipe failed: %s\n",
   1692 			    device_xname(sc->aue_dev), usbd_errstr(err));
   1693 		}
   1694 		sc->aue_ep[AUE_ENDPT_TX] = NULL;
   1695 	}
   1696 
   1697 	if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
   1698 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
   1699 		if (err) {
   1700 			printf("%s: abort intr pipe failed: %s\n",
   1701 			    device_xname(sc->aue_dev), usbd_errstr(err));
   1702 		}
   1703 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
   1704 		if (err) {
   1705 			printf("%s: close intr pipe failed: %s\n",
   1706 			    device_xname(sc->aue_dev), usbd_errstr(err));
   1707 		}
   1708 		sc->aue_ep[AUE_ENDPT_INTR] = NULL;
   1709 	}
   1710 
   1711 	/* Free RX resources. */
   1712 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
   1713 		if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
   1714 			m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
   1715 			sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
   1716 		}
   1717 		if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
   1718 			usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
   1719 			sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
   1720 		}
   1721 	}
   1722 
   1723 	/* Free TX resources. */
   1724 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
   1725 		if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
   1726 			m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
   1727 			sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
   1728 		}
   1729 		if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
   1730 			usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
   1731 			sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
   1732 		}
   1733 	}
   1734 
   1735 	sc->aue_link = 0;
   1736 
   1737 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1738 }
   1739 
   1740 Static void
   1741 aue_multithread(void *arg)
   1742 {
   1743 	struct aue_softc *sc;
   1744 	int s;
   1745 
   1746 	sc = (struct aue_softc *)arg;
   1747 
   1748 	while (1) {
   1749 		mutex_enter(&sc->aue_mcmtx);
   1750 		cv_wait(&sc->aue_domc,&sc->aue_mcmtx);
   1751 		mutex_exit(&sc->aue_mcmtx);
   1752 
   1753 		if (sc->aue_closing)
   1754 			break;
   1755 
   1756 		s = splnet();
   1757 		aue_init(sc);
   1758 		/* XXX called by aue_init, but rc ifconfig hangs without it: */
   1759 		aue_setmulti(sc);
   1760 		splx(s);
   1761 	}
   1762 
   1763 	cv_signal(&sc->aue_closemc);
   1764 
   1765 	kthread_exit(0);
   1766 }
   1767