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