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