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