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