Home | History | Annotate | Line # | Download | only in usb
if_aue.c revision 1.36
      1 /*	$NetBSD: if_aue.c,v 1.36 2000/03/30 00:18:17 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 		printf("%s: usb error on intr: %s\n", USBDEVNAME(sc->aue_dev),
   1137 		    usbd_errstr(status));
   1138 		if (status == USBD_STALLED)
   1139 			usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
   1140 		return;
   1141 	}
   1142 
   1143 	if (p->aue_txstat0)
   1144 		ifp->if_oerrors++;
   1145 
   1146 	if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
   1147 		ifp->if_collisions++;
   1148 }
   1149 
   1150 #if defined(__FreeBSD__)
   1151 Static void
   1152 aue_rxstart(ifp)
   1153 	struct ifnet		*ifp;
   1154 {
   1155 	struct aue_softc	*sc;
   1156 	struct aue_chain	*c;
   1157 
   1158 	sc = ifp->if_softc;
   1159 	c = &sc->aue_cdata.aue_rx_chain[sc->aue_cdata.aue_rx_prod];
   1160 
   1161 	if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
   1162 		ifp->if_ierrors++;
   1163 		return;
   1164 	}
   1165 
   1166 	/* Setup new transfer. */
   1167 	usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
   1168 	    c, mtod(c->aue_mbuf, char *), AUE_BUFSZ, USBD_SHORT_XFER_OK,
   1169 	    USBD_NO_TIMEOUT, aue_rxeof);
   1170 	usbd_transfer(c->aue_xfer);
   1171 }
   1172 #endif
   1173 
   1174 /*
   1175  * A frame has been uploaded: pass the resulting mbuf chain up to
   1176  * the higher level protocols.
   1177  *
   1178  * Grrr. Receiving transfers larger than about 1152 bytes sometimes
   1179  * doesn't work. We get an incomplete frame. In order to avoid
   1180  * this, we queue up RX transfers that are shorter than a full sized
   1181  * frame. If the received frame is larger than our transfer size,
   1182  * we snag the rest of the data using a second transfer. Does this
   1183  * hurt performance? Yes. But after fighting with this stupid thing
   1184  * for three days, I'm willing to settle. I'd rather have reliable
   1185  * receive performance that fast but spotty performance.
   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 		aue_stop(sc);
   1451 		return (EIO);
   1452 	}
   1453 	DPRINTFN(5,("%s: %s: send %d bytes\n", USBDEVNAME(sc->aue_dev),
   1454 		    __FUNCTION__, total_len));
   1455 
   1456 	sc->aue_cdata.aue_tx_cnt++;
   1457 
   1458 	return (0);
   1459 }
   1460 
   1461 Static void
   1462 aue_start(ifp)
   1463 	struct ifnet		*ifp;
   1464 {
   1465 	struct aue_softc	*sc = ifp->if_softc;
   1466 	struct mbuf		*m_head = NULL;
   1467 
   1468 	DPRINTFN(5,("%s: %s: enter, link=%d\n", USBDEVNAME(sc->aue_dev),
   1469 		    __FUNCTION__, sc->aue_link));
   1470 
   1471 	if (sc->aue_dying)
   1472 		return;
   1473 
   1474 	if (!sc->aue_link)
   1475 		return;
   1476 
   1477 	if (ifp->if_flags & IFF_OACTIVE)
   1478 		return;
   1479 
   1480 	IF_DEQUEUE(&ifp->if_snd, m_head);
   1481 	if (m_head == NULL)
   1482 		return;
   1483 
   1484 	if (aue_send(sc, m_head, 0)) {
   1485 		IF_PREPEND(&ifp->if_snd, m_head);
   1486 		ifp->if_flags |= IFF_OACTIVE;
   1487 		return;
   1488 	}
   1489 
   1490 #if NBPFILTER > 0
   1491 	/*
   1492 	 * If there's a BPF listener, bounce a copy of this frame
   1493 	 * to him.
   1494 	 */
   1495 	if (ifp->if_bpf)
   1496 		BPF_MTAP(ifp, m_head);
   1497 #endif
   1498 
   1499 	ifp->if_flags |= IFF_OACTIVE;
   1500 
   1501 	/*
   1502 	 * Set a timeout in case the chip goes out to lunch.
   1503 	 */
   1504 	ifp->if_timer = 5;
   1505 }
   1506 
   1507 Static void
   1508 aue_init(xsc)
   1509 	void			*xsc;
   1510 {
   1511 	struct aue_softc	*sc = xsc;
   1512 	struct ifnet		*ifp = GET_IFP(sc);
   1513 	struct mii_data		*mii = GET_MII(sc);
   1514 	int			i, s;
   1515 	u_char			*eaddr;
   1516 
   1517 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1518 
   1519 	if (sc->aue_dying)
   1520 		return;
   1521 
   1522 	if (ifp->if_flags & IFF_RUNNING)
   1523 		return;
   1524 
   1525 	s = splimp();
   1526 
   1527 	/*
   1528 	 * Cancel pending I/O and free all RX/TX buffers.
   1529 	 */
   1530 	aue_reset(sc);
   1531 
   1532 #if defined(__FreeBSD__) || defined(__OpenBSD__)
   1533 	eaddr = sc->arpcom.ac_enaddr;
   1534 #elif defined(__NetBSD__)
   1535 	eaddr = LLADDR(ifp->if_sadl);
   1536 #endif /* defined(__NetBSD__) */
   1537 	for (i = 0; i < ETHER_ADDR_LEN; i++)
   1538 		aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
   1539 
   1540 	 /* If we want promiscuous mode, set the allframes bit. */
   1541 	if (ifp->if_flags & IFF_PROMISC)
   1542 		AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1543 	else
   1544 		AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1545 
   1546 	/* Init TX ring. */
   1547 	if (aue_tx_list_init(sc) == ENOBUFS) {
   1548 		printf("%s: tx list init failed\n", USBDEVNAME(sc->aue_dev));
   1549 		splx(s);
   1550 		return;
   1551 	}
   1552 
   1553 	/* Init RX ring. */
   1554 	if (aue_rx_list_init(sc) == ENOBUFS) {
   1555 		printf("%s: rx list init failed\n", USBDEVNAME(sc->aue_dev));
   1556 		splx(s);
   1557 		return;
   1558 	}
   1559 
   1560 	/* Load the multicast filter. */
   1561 	aue_setmulti(sc);
   1562 
   1563 	/* Enable RX and TX */
   1564 	aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
   1565 	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
   1566 	AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
   1567 
   1568 	mii_mediachg(mii);
   1569 
   1570 	if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
   1571 		if (aue_openpipes(sc)) {
   1572 			splx(s);
   1573 			return;
   1574 		}
   1575 	}
   1576 
   1577 	ifp->if_flags |= IFF_RUNNING;
   1578 	ifp->if_flags &= ~IFF_OACTIVE;
   1579 
   1580 	splx(s);
   1581 
   1582 	usb_callout(sc->aue_stat_ch, hz, aue_tick, sc);
   1583 }
   1584 
   1585 Static int
   1586 aue_openpipes(sc)
   1587 	struct aue_softc	*sc;
   1588 {
   1589 	struct aue_chain	*c;
   1590 	usbd_status		err;
   1591 	int i;
   1592 
   1593 	/* Open RX and TX pipes. */
   1594 	err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
   1595 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
   1596 	if (err) {
   1597 		printf("%s: open rx pipe failed: %s\n",
   1598 		    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1599 		return (EIO);
   1600 	}
   1601 	usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
   1602 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
   1603 	if (err) {
   1604 		printf("%s: open tx pipe failed: %s\n",
   1605 		    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1606 		return (EIO);
   1607 	}
   1608 	err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
   1609 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
   1610 	    &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
   1611 	    AUE_INTR_INTERVAL);
   1612 	if (err) {
   1613 		printf("%s: open intr pipe failed: %s\n",
   1614 		    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1615 		return (EIO);
   1616 	}
   1617 
   1618 	/* Start up the receive pipe. */
   1619 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
   1620 		c = &sc->aue_cdata.aue_rx_chain[i];
   1621 		usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
   1622 		    c, c->aue_buf, AUE_BUFSZ,
   1623 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
   1624 		    aue_rxeof);
   1625 		(void)usbd_transfer(c->aue_xfer); /* XXX */
   1626 		DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->aue_dev),
   1627 			    __FUNCTION__));
   1628 
   1629 	}
   1630 	return (0);
   1631 }
   1632 
   1633 /*
   1634  * Set media options.
   1635  */
   1636 Static int
   1637 aue_ifmedia_upd(ifp)
   1638 	struct ifnet		*ifp;
   1639 {
   1640 	struct aue_softc	*sc = ifp->if_softc;
   1641 	struct mii_data		*mii = GET_MII(sc);
   1642 
   1643 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1644 
   1645 	if (sc->aue_dying)
   1646 		return (0);
   1647 
   1648 	sc->aue_link = 0;
   1649 	if (mii->mii_instance) {
   1650 		struct mii_softc	*miisc;
   1651 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
   1652 		    miisc = LIST_NEXT(miisc, mii_list))
   1653 			 mii_phy_reset(miisc);
   1654 	}
   1655 	mii_mediachg(mii);
   1656 
   1657 	return (0);
   1658 }
   1659 
   1660 /*
   1661  * Report current media status.
   1662  */
   1663 Static void
   1664 aue_ifmedia_sts(ifp, ifmr)
   1665 	struct ifnet		*ifp;
   1666 	struct ifmediareq	*ifmr;
   1667 {
   1668 	struct aue_softc	*sc = ifp->if_softc;
   1669 	struct mii_data		*mii = GET_MII(sc);
   1670 
   1671 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1672 
   1673 	mii_pollstat(mii);
   1674 	ifmr->ifm_active = mii->mii_media_active;
   1675 	ifmr->ifm_status = mii->mii_media_status;
   1676 }
   1677 
   1678 Static int
   1679 aue_ioctl(ifp, command, data)
   1680 	struct ifnet		*ifp;
   1681 	u_long			command;
   1682 	caddr_t			data;
   1683 {
   1684 	struct aue_softc	*sc = ifp->if_softc;
   1685 #if defined(__NetBSD__) || defined(__OpenBSD__)
   1686 	struct ifaddr 		*ifa = (struct ifaddr *)data;
   1687 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1688 	struct ifreq		*ifr = (struct ifreq *)data;
   1689 	struct mii_data		*mii;
   1690 	int			s, error = 0;
   1691 
   1692 	if (sc->aue_dying)
   1693 		return (EIO);
   1694 
   1695 	s = splimp();
   1696 
   1697 	switch(command) {
   1698 #if defined(__FreeBSD__)
   1699 	case SIOCSIFADDR:
   1700 	case SIOCGIFADDR:
   1701 	case SIOCSIFMTU:
   1702 		error = ether_ioctl(ifp, command, data);
   1703 		break;
   1704 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1705 	case SIOCSIFADDR:
   1706 		ifp->if_flags |= IFF_UP;
   1707 		aue_init(sc);
   1708 
   1709 		switch (ifa->ifa_addr->sa_family) {
   1710 #ifdef INET
   1711 		case AF_INET:
   1712 #if defined(__NetBSD__)
   1713 			arp_ifinit(ifp, ifa);
   1714 #else
   1715 			arp_ifinit(&sc->arpcom, ifa);
   1716 #endif
   1717 			break;
   1718 #endif /* INET */
   1719 #ifdef NS
   1720 		case AF_NS:
   1721 		    {
   1722 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1723 
   1724 			if (ns_nullhost(*ina))
   1725 				ina->x_host = *(union ns_host *)
   1726 					LLADDR(ifp->if_sadl);
   1727 			else
   1728 				memcpy(LLADDR(ifp->if_sadl),
   1729 				       ina->x_host.c_host,
   1730 				       ifp->if_addrlen);
   1731 			break;
   1732 		    }
   1733 #endif /* NS */
   1734 		}
   1735 		break;
   1736 
   1737 	case SIOCSIFMTU:
   1738 		if (ifr->ifr_mtu > ETHERMTU)
   1739 			error = EINVAL;
   1740 		else
   1741 			ifp->if_mtu = ifr->ifr_mtu;
   1742 		break;
   1743 
   1744 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1745 	case SIOCSIFFLAGS:
   1746 		if (ifp->if_flags & IFF_UP) {
   1747 			if (ifp->if_flags & IFF_RUNNING &&
   1748 			    ifp->if_flags & IFF_PROMISC &&
   1749 			    !(sc->aue_if_flags & IFF_PROMISC)) {
   1750 				AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1751 			} else if (ifp->if_flags & IFF_RUNNING &&
   1752 			    !(ifp->if_flags & IFF_PROMISC) &&
   1753 			    sc->aue_if_flags & IFF_PROMISC) {
   1754 				AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1755 			} else if (!(ifp->if_flags & IFF_RUNNING))
   1756 				aue_init(sc);
   1757 		} else {
   1758 			if (ifp->if_flags & IFF_RUNNING)
   1759 				aue_stop(sc);
   1760 		}
   1761 		sc->aue_if_flags = ifp->if_flags;
   1762 		error = 0;
   1763 		break;
   1764 	case SIOCADDMULTI:
   1765 	case SIOCDELMULTI:
   1766 		aue_setmulti(sc);
   1767 		error = 0;
   1768 		break;
   1769 	case SIOCGIFMEDIA:
   1770 	case SIOCSIFMEDIA:
   1771 		mii = GET_MII(sc);
   1772 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
   1773 		break;
   1774 	default:
   1775 		error = EINVAL;
   1776 		break;
   1777 	}
   1778 
   1779 	splx(s);
   1780 
   1781 	return (error);
   1782 }
   1783 
   1784 Static void
   1785 aue_watchdog(ifp)
   1786 	struct ifnet		*ifp;
   1787 {
   1788 	struct aue_softc	*sc = ifp->if_softc;
   1789 
   1790 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1791 
   1792 	ifp->if_oerrors++;
   1793 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->aue_dev));
   1794 
   1795 	/*
   1796 	 * The polling business is a kludge to avoid allowing the
   1797 	 * USB code to call tsleep() in usbd_delay_ms(), which will
   1798 	 * kill us since the watchdog routine is invoked from
   1799 	 * interrupt context.
   1800 	 */
   1801 	usbd_set_polling(sc->aue_udev, 1);
   1802 	aue_stop(sc);
   1803 	aue_init(sc);
   1804 	usbd_set_polling(sc->aue_udev, 0);
   1805 
   1806 	if (ifp->if_snd.ifq_head != NULL)
   1807 		aue_start(ifp);
   1808 }
   1809 
   1810 /*
   1811  * Stop the adapter and free any mbufs allocated to the
   1812  * RX and TX lists.
   1813  */
   1814 Static void
   1815 aue_stop(sc)
   1816 	struct aue_softc	*sc;
   1817 {
   1818 	usbd_status		err;
   1819 	struct ifnet		*ifp;
   1820 	int			i;
   1821 
   1822 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1823 
   1824 	ifp = GET_IFP(sc);
   1825 	ifp->if_timer = 0;
   1826 
   1827 	aue_csr_write_1(sc, AUE_CTL0, 0);
   1828 	aue_csr_write_1(sc, AUE_CTL1, 0);
   1829 	aue_reset(sc);
   1830 	usb_uncallout(sc->aue_stat_ch, aue_tick, sc);
   1831 
   1832 	/* Stop transfers. */
   1833 	if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
   1834 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
   1835 		if (err) {
   1836 			printf("%s: abort rx pipe failed: %s\n",
   1837 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1838 		}
   1839 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
   1840 		if (err) {
   1841 			printf("%s: close rx pipe failed: %s\n",
   1842 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1843 		}
   1844 		sc->aue_ep[AUE_ENDPT_RX] = NULL;
   1845 	}
   1846 
   1847 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
   1848 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
   1849 		if (err) {
   1850 			printf("%s: abort tx pipe failed: %s\n",
   1851 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1852 		}
   1853 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
   1854 		if (err) {
   1855 			printf("%s: close tx pipe failed: %s\n",
   1856 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1857 		}
   1858 		sc->aue_ep[AUE_ENDPT_TX] = NULL;
   1859 	}
   1860 
   1861 	if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
   1862 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
   1863 		if (err) {
   1864 			printf("%s: abort intr pipe failed: %s\n",
   1865 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1866 		}
   1867 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
   1868 		if (err) {
   1869 			printf("%s: close intr pipe failed: %s\n",
   1870 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1871 		}
   1872 		sc->aue_ep[AUE_ENDPT_INTR] = NULL;
   1873 	}
   1874 
   1875 	/* Free RX resources. */
   1876 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
   1877 		if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
   1878 			m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
   1879 			sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
   1880 		}
   1881 		if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
   1882 			usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
   1883 			sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
   1884 		}
   1885 	}
   1886 
   1887 	/* Free TX resources. */
   1888 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
   1889 		if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
   1890 			m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
   1891 			sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
   1892 		}
   1893 		if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
   1894 			usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
   1895 			sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
   1896 		}
   1897 	}
   1898 
   1899 	sc->aue_link = 0;
   1900 
   1901 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1902 }
   1903 
   1904 #ifdef __FreeBSD__
   1905 /*
   1906  * Stop all chip I/O so that the kernel's probe routines don't
   1907  * get confused by errant DMAs when rebooting.
   1908  */
   1909 Static void
   1910 aue_shutdown(dev)
   1911 	device_ptr_t		dev;
   1912 {
   1913 	struct aue_softc	*sc = USBGETSOFTC(dev);
   1914 
   1915 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1916 
   1917 	aue_reset(sc);
   1918 	aue_stop(sc);
   1919 }
   1920 #endif
   1921