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