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