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