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