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