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