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