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