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