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