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