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