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