Home | History | Annotate | Line # | Download | only in usb
if_aue.c revision 1.35
      1 /*	$NetBSD: if_aue.c,v 1.35 2000/03/29 18:24:52 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 	strncpy(ifp->if_xname, USBDEVNAME(sc->aue_dev), IFNAMSIZ);
    894 
    895 	/* Initialize MII/media info. */
    896 	mii = &sc->aue_mii;
    897 	mii->mii_ifp = ifp;
    898 	mii->mii_readreg = aue_miibus_readreg;
    899 	mii->mii_writereg = aue_miibus_writereg;
    900 	mii->mii_statchg = aue_miibus_statchg;
    901 	ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, aue_ifmedia_sts);
    902 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
    903 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
    904 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
    905 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
    906 	} else
    907 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    908 
    909 	/* Attach the interface. */
    910 	if_attach(ifp);
    911 	Ether_ifattach(ifp, eaddr);
    912 
    913 #if NBPFILTER > 0
    914 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
    915 		  sizeof(struct ether_header));
    916 #endif
    917 #if NRND > 0
    918 	rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->aue_dev),
    919 	    RND_TYPE_NET, 0);
    920 #endif
    921 
    922 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    923 
    924 	usb_callout_init(sc->aue_stat_ch);
    925 
    926 	sc->aue_attached = 1;
    927 	splx(s);
    928 
    929 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->aue_udev,
    930 			   USBDEV(sc->aue_dev));
    931 
    932 	USB_ATTACH_SUCCESS_RETURN;
    933 }
    934 
    935 USB_DETACH(aue)
    936 {
    937 	USB_DETACH_START(aue, sc);
    938 	struct ifnet		*ifp = GET_IFP(sc);
    939 	int			s;
    940 
    941 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
    942 
    943 	s = splusb();
    944 
    945 	usb_uncallout(sc->aue_stat_ch, aue_tick, sc);
    946 
    947 	if (!sc->aue_attached) {
    948 		/* Detached before attached finished, so just bail out. */
    949 		splx(s);
    950 		return (0);
    951 	}
    952 
    953 	if (ifp->if_flags & IFF_RUNNING)
    954 		aue_stop(sc);
    955 
    956 #if defined(__NetBSD__)
    957 #if NRND > 0
    958 	rnd_detach_source(&sc->rnd_source);
    959 #endif
    960 	mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY);
    961 	ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY);
    962 #if NBPFILTER > 0
    963 	bpfdetach(ifp);
    964 #endif
    965 	ether_ifdetach(ifp);
    966 #endif /* __NetBSD__ */
    967 
    968 	if_detach(ifp);
    969 
    970 #ifdef DIAGNOSTIC
    971 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL ||
    972 	    sc->aue_ep[AUE_ENDPT_RX] != NULL ||
    973 	    sc->aue_ep[AUE_ENDPT_INTR] != NULL)
    974 		printf("%s: detach has active endpoints\n",
    975 		       USBDEVNAME(sc->aue_dev));
    976 #endif
    977 
    978 	sc->aue_attached = 0;
    979 	splx(s);
    980 
    981 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->aue_udev,
    982 			   USBDEV(sc->aue_dev));
    983 
    984 	return (0);
    985 }
    986 
    987 #if defined(__NetBSD__) || defined(__OpenBSD__)
    988 int
    989 aue_activate(self, act)
    990 	device_ptr_t self;
    991 	enum devact act;
    992 {
    993 	struct aue_softc *sc = (struct aue_softc *)self;
    994 
    995 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
    996 
    997 	switch (act) {
    998 	case DVACT_ACTIVATE:
    999 		return (EOPNOTSUPP);
   1000 		break;
   1001 
   1002 	case DVACT_DEACTIVATE:
   1003 		if_deactivate(&sc->aue_ec.ec_if);
   1004 		sc->aue_dying = 1;
   1005 		break;
   1006 	}
   1007 	return (0);
   1008 }
   1009 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1010 
   1011 /*
   1012  * Initialize an RX descriptor and attach an MBUF cluster.
   1013  */
   1014 Static int
   1015 aue_newbuf(sc, c, m)
   1016 	struct aue_softc	*sc;
   1017 	struct aue_chain	*c;
   1018 	struct mbuf		*m;
   1019 {
   1020 	struct mbuf		*m_new = NULL;
   1021 
   1022 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
   1023 
   1024 	if (m == NULL) {
   1025 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
   1026 		if (m_new == NULL) {
   1027 			printf("%s: no memory for rx list "
   1028 			    "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
   1029 			return (ENOBUFS);
   1030 		}
   1031 
   1032 		MCLGET(m_new, M_DONTWAIT);
   1033 		if (!(m_new->m_flags & M_EXT)) {
   1034 			printf("%s: no memory for rx list "
   1035 			    "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
   1036 			m_freem(m_new);
   1037 			return (ENOBUFS);
   1038 		}
   1039 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
   1040 	} else {
   1041 		m_new = m;
   1042 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
   1043 		m_new->m_data = m_new->m_ext.ext_buf;
   1044 	}
   1045 
   1046 	m_adj(m_new, ETHER_ALIGN);
   1047 	c->aue_mbuf = m_new;
   1048 
   1049 	return (0);
   1050 }
   1051 
   1052 Static int
   1053 aue_rx_list_init(sc)
   1054 	struct aue_softc	*sc;
   1055 {
   1056 	struct aue_cdata	*cd;
   1057 	struct aue_chain	*c;
   1058 	int			i;
   1059 
   1060 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1061 
   1062 	cd = &sc->aue_cdata;
   1063 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
   1064 		c = &cd->aue_rx_chain[i];
   1065 		c->aue_sc = sc;
   1066 		c->aue_idx = i;
   1067 		if (aue_newbuf(sc, c, NULL) == ENOBUFS)
   1068 			return (ENOBUFS);
   1069 		if (c->aue_xfer == NULL) {
   1070 			c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
   1071 			if (c->aue_xfer == NULL)
   1072 				return (ENOBUFS);
   1073 			c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
   1074 			if (c->aue_buf == NULL)
   1075 				return (ENOBUFS); /* XXX free xfer */
   1076 		}
   1077 	}
   1078 
   1079 	return (0);
   1080 }
   1081 
   1082 Static int
   1083 aue_tx_list_init(sc)
   1084 	struct aue_softc	*sc;
   1085 {
   1086 	struct aue_cdata	*cd;
   1087 	struct aue_chain	*c;
   1088 	int			i;
   1089 
   1090 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1091 
   1092 	cd = &sc->aue_cdata;
   1093 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
   1094 		c = &cd->aue_tx_chain[i];
   1095 		c->aue_sc = sc;
   1096 		c->aue_idx = i;
   1097 		c->aue_mbuf = NULL;
   1098 		if (c->aue_xfer == NULL) {
   1099 			c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
   1100 			if (c->aue_xfer == NULL)
   1101 				return (ENOBUFS);
   1102 			c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
   1103 			if (c->aue_buf == NULL)
   1104 				return (ENOBUFS);
   1105 		}
   1106 	}
   1107 
   1108 	return (0);
   1109 }
   1110 
   1111 Static void
   1112 aue_intr(xfer, priv, status)
   1113 	usbd_xfer_handle	xfer;
   1114 	usbd_private_handle	priv;
   1115 	usbd_status		status;
   1116 {
   1117 	struct aue_softc	*sc = priv;
   1118 	struct ifnet		*ifp = GET_IFP(sc);
   1119 	struct aue_intrpkt	*p = &sc->aue_cdata.aue_ibuf;
   1120 
   1121 	DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
   1122 
   1123 	if (sc->aue_dying)
   1124 		return;
   1125 
   1126 	if (!(ifp->if_flags & IFF_RUNNING))
   1127 		return;
   1128 
   1129 	if (status != USBD_NORMAL_COMPLETION) {
   1130 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1131 			return;
   1132 		}
   1133 		printf("%s: usb error on intr: %s\n", USBDEVNAME(sc->aue_dev),
   1134 		    usbd_errstr(status));
   1135 		if (status == USBD_STALLED)
   1136 			usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
   1137 		return;
   1138 	}
   1139 
   1140 	if (p->aue_txstat0)
   1141 		ifp->if_oerrors++;
   1142 
   1143 	if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
   1144 		ifp->if_collisions++;
   1145 }
   1146 
   1147 #if defined(__FreeBSD__)
   1148 Static void
   1149 aue_rxstart(ifp)
   1150 	struct ifnet		*ifp;
   1151 {
   1152 	struct aue_softc	*sc;
   1153 	struct aue_chain	*c;
   1154 
   1155 	sc = ifp->if_softc;
   1156 	c = &sc->aue_cdata.aue_rx_chain[sc->aue_cdata.aue_rx_prod];
   1157 
   1158 	if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
   1159 		ifp->if_ierrors++;
   1160 		return;
   1161 	}
   1162 
   1163 	/* Setup new transfer. */
   1164 	usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
   1165 	    c, mtod(c->aue_mbuf, char *), AUE_BUFSZ, USBD_SHORT_XFER_OK,
   1166 	    USBD_NO_TIMEOUT, aue_rxeof);
   1167 	usbd_transfer(c->aue_xfer);
   1168 }
   1169 #endif
   1170 
   1171 /*
   1172  * A frame has been uploaded: pass the resulting mbuf chain up to
   1173  * the higher level protocols.
   1174  *
   1175  * Grrr. Receiving transfers larger than about 1152 bytes sometimes
   1176  * doesn't work. We get an incomplete frame. In order to avoid
   1177  * this, we queue up RX transfers that are shorter than a full sized
   1178  * frame. If the received frame is larger than our transfer size,
   1179  * we snag the rest of the data using a second transfer. Does this
   1180  * hurt performance? Yes. But after fighting with this stupid thing
   1181  * for three days, I'm willing to settle. I'd rather have reliable
   1182  * receive performance that fast but spotty performance.
   1183  */
   1184 Static void
   1185 aue_rxeof(xfer, priv, status)
   1186 	usbd_xfer_handle	xfer;
   1187 	usbd_private_handle	priv;
   1188 	usbd_status		status;
   1189 {
   1190 	struct aue_chain	*c = priv;
   1191 	struct aue_softc	*sc = c->aue_sc;
   1192 	struct ifnet		*ifp = GET_IFP(sc);
   1193 	struct mbuf		*m;
   1194 	u_int32_t		total_len;
   1195 	struct aue_rxpkt	r;
   1196 #if defined(__NetBSD__) || defined(__OpenBSD__)
   1197 	int			s;
   1198 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1199 
   1200 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
   1201 
   1202 	if (sc->aue_dying)
   1203 		return;
   1204 
   1205 	if (!(ifp->if_flags & IFF_RUNNING))
   1206 		return;
   1207 
   1208 	if (status != USBD_NORMAL_COMPLETION) {
   1209 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   1210 			return;
   1211 		sc->aue_rx_errs++;
   1212 		if (usbd_ratecheck(&sc->aue_rx_notice)) {
   1213 			printf("%s: %u usb errors on rx: %s\n",
   1214 			    USBDEVNAME(sc->aue_dev), sc->aue_rx_errs,
   1215 			    usbd_errstr(status));
   1216 			sc->aue_rx_errs = 0;
   1217 		}
   1218 		if (status == USBD_STALLED)
   1219 			usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
   1220 		goto done;
   1221 	}
   1222 
   1223 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
   1224 
   1225 	memcpy(mtod(c->aue_mbuf, char*), c->aue_buf, total_len);
   1226 
   1227 	if (total_len <= 4 + ETHER_CRC_LEN) {
   1228 		ifp->if_ierrors++;
   1229 		goto done;
   1230 	}
   1231 
   1232 	memcpy(&r, c->aue_buf + total_len - 4, sizeof(r));
   1233 
   1234 	/* Turn off all the non-error bits in the rx status word. */
   1235 	r.aue_rxstat &= AUE_RXSTAT_MASK;
   1236 	if (r.aue_rxstat) {
   1237 		ifp->if_ierrors++;
   1238 		goto done;
   1239 	}
   1240 
   1241 	/* No errors; receive the packet. */
   1242 	m = c->aue_mbuf;
   1243 	total_len -= ETHER_CRC_LEN + 4;
   1244 	m->m_pkthdr.len = m->m_len = total_len;
   1245 	ifp->if_ipackets++;
   1246 
   1247 #if defined(__FreeBSD__)
   1248 	m->m_pkthdr.rcvif = (struct ifnet *)&kue_qdat;
   1249 	/* Put the packet on the special USB input queue. */
   1250 	usb_ether_input(m);
   1251 
   1252 	return;
   1253 
   1254 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1255 	m->m_pkthdr.rcvif = ifp;
   1256 
   1257 	s = splimp();
   1258 
   1259 	/* XXX ugly */
   1260 	if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
   1261 		ifp->if_ierrors++;
   1262 		goto done1;
   1263 	}
   1264 
   1265 #if NBPFILTER > 0
   1266 	/*
   1267 	 * Handle BPF listeners. Let the BPF user see the packet, but
   1268 	 * don't pass it up to the ether_input() layer unless it's
   1269 	 * a broadcast packet, multicast packet, matches our ethernet
   1270 	 * address or the interface is in promiscuous mode.
   1271 	 */
   1272 	if (ifp->if_bpf) {
   1273 		struct ether_header *eh = mtod(m, struct ether_header *);
   1274 		BPF_MTAP(ifp, m);
   1275 #if defined(__NetBSD__)
   1276 		if ((ifp->if_flags & IFF_PROMISC) &&
   1277 		    memcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
   1278 			   ETHER_ADDR_LEN) &&
   1279 		    !(eh->ether_dhost[0] & 1)) {
   1280 			m_freem(m);
   1281 			goto done1;
   1282 		}
   1283 #endif
   1284 	}
   1285 #endif
   1286 
   1287 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->aue_dev),
   1288 		    __FUNCTION__, m->m_len));
   1289 	IF_INPUT(ifp, m);
   1290  done1:
   1291 	splx(s);
   1292 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1293 
   1294  done:
   1295 
   1296 	/* Setup new transfer. */
   1297 	usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
   1298 	    c, c->aue_buf, AUE_BUFSZ,
   1299 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
   1300 	    USBD_NO_TIMEOUT, aue_rxeof);
   1301 	usbd_transfer(xfer);
   1302 
   1303 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->aue_dev),
   1304 		    __FUNCTION__));
   1305 }
   1306 
   1307 /*
   1308  * A frame was downloaded to the chip. It's safe for us to clean up
   1309  * the list buffers.
   1310  */
   1311 
   1312 Static void
   1313 aue_txeof(xfer, priv, status)
   1314 	usbd_xfer_handle	xfer;
   1315 	usbd_private_handle	priv;
   1316 	usbd_status		status;
   1317 {
   1318 	struct aue_chain	*c = priv;
   1319 	struct aue_softc	*sc = c->aue_sc;
   1320 	struct ifnet		*ifp = GET_IFP(sc);
   1321 	int			s;
   1322 
   1323 	if (sc->aue_dying)
   1324 		return;
   1325 
   1326 	s = splimp();
   1327 
   1328 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->aue_dev),
   1329 		    __FUNCTION__, status));
   1330 
   1331 	ifp->if_timer = 0;
   1332 	ifp->if_flags &= ~IFF_OACTIVE;
   1333 
   1334 	if (status != USBD_NORMAL_COMPLETION) {
   1335 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1336 			splx(s);
   1337 			return;
   1338 		}
   1339 		ifp->if_oerrors++;
   1340 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->aue_dev),
   1341 		    usbd_errstr(status));
   1342 		if (status == USBD_STALLED)
   1343 			usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]);
   1344 		splx(s);
   1345 		return;
   1346 	}
   1347 
   1348 	ifp->if_opackets++;
   1349 
   1350 #if defined(__FreeBSD__)
   1351 	c->aue_mbuf->m_pkthdr.rcvif = ifp;
   1352 	usb_tx_done(c->aue_mbuf);
   1353   	c->aue_mbuf = NULL;
   1354 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1355 	m_freem(c->aue_mbuf);
   1356 	c->aue_mbuf = NULL;
   1357 
   1358 	if (ifp->if_snd.ifq_head != NULL)
   1359 		aue_start(ifp);
   1360 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1361 
   1362 	splx(s);
   1363 }
   1364 
   1365 Static void
   1366 aue_tick(xsc)
   1367 	void			*xsc;
   1368 {
   1369 	struct aue_softc	*sc = xsc;
   1370 	struct ifnet		*ifp;
   1371 	struct mii_data		*mii;
   1372 	int			s;
   1373 
   1374 	DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
   1375 
   1376 	if (sc == NULL)
   1377 		return;
   1378 
   1379 	if (sc->aue_dying)
   1380 		return;
   1381 
   1382 	ifp = GET_IFP(sc);
   1383 	mii = GET_MII(sc);
   1384 	if (mii == NULL)
   1385 		return;
   1386 
   1387 	s = splimp();
   1388 
   1389 	mii_tick(mii);
   1390 	if (!sc->aue_link) {
   1391 		mii_pollstat(mii);
   1392 		if (mii->mii_media_status & IFM_ACTIVE &&
   1393 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
   1394 			DPRINTFN(2,("%s: %s: got link\n",
   1395 				    USBDEVNAME(sc->aue_dev),__FUNCTION__));
   1396 			sc->aue_link++;
   1397 			if (ifp->if_snd.ifq_head != NULL)
   1398 				aue_start(ifp);
   1399 		}
   1400 	}
   1401 
   1402 	usb_callout(sc->aue_stat_ch, hz, aue_tick, sc);
   1403 
   1404 	splx(s);
   1405 }
   1406 
   1407 Static int
   1408 aue_send(sc, m, idx)
   1409 	struct aue_softc	*sc;
   1410 	struct mbuf		*m;
   1411 	int			idx;
   1412 {
   1413 	int			total_len;
   1414 	struct aue_chain	*c;
   1415 	usbd_status		err;
   1416 
   1417 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
   1418 
   1419 	c = &sc->aue_cdata.aue_tx_chain[idx];
   1420 
   1421 	/*
   1422 	 * Copy the mbuf data into a contiguous buffer, leaving two
   1423 	 * bytes at the beginning to hold the frame length.
   1424 	 */
   1425 	m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
   1426 	c->aue_mbuf = m;
   1427 
   1428 	/*
   1429 	 * The ADMtek documentation says that the packet length is
   1430 	 * supposed to be specified in the first two bytes of the
   1431 	 * transfer, however it actually seems to ignore this info
   1432 	 * and base the frame size on the bulk transfer length.
   1433 	 */
   1434 	c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
   1435 	c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
   1436 	total_len = m->m_pkthdr.len + 2;
   1437 
   1438 	usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
   1439 	    c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
   1440 	    AUE_TX_TIMEOUT, aue_txeof);
   1441 
   1442 	/* Transmit */
   1443 	err = usbd_transfer(c->aue_xfer);
   1444 	if (err != USBD_IN_PROGRESS) {
   1445 		aue_stop(sc);
   1446 		return (EIO);
   1447 	}
   1448 	DPRINTFN(5,("%s: %s: send %d bytes\n", USBDEVNAME(sc->aue_dev),
   1449 		    __FUNCTION__, total_len));
   1450 
   1451 	sc->aue_cdata.aue_tx_cnt++;
   1452 
   1453 	return (0);
   1454 }
   1455 
   1456 Static void
   1457 aue_start(ifp)
   1458 	struct ifnet		*ifp;
   1459 {
   1460 	struct aue_softc	*sc = ifp->if_softc;
   1461 	struct mbuf		*m_head = NULL;
   1462 
   1463 	DPRINTFN(5,("%s: %s: enter, link=%d\n", USBDEVNAME(sc->aue_dev),
   1464 		    __FUNCTION__, sc->aue_link));
   1465 
   1466 	if (sc->aue_dying)
   1467 		return;
   1468 
   1469 	if (!sc->aue_link)
   1470 		return;
   1471 
   1472 	if (ifp->if_flags & IFF_OACTIVE)
   1473 		return;
   1474 
   1475 	IF_DEQUEUE(&ifp->if_snd, m_head);
   1476 	if (m_head == NULL)
   1477 		return;
   1478 
   1479 	if (aue_send(sc, m_head, 0)) {
   1480 		IF_PREPEND(&ifp->if_snd, m_head);
   1481 		ifp->if_flags |= IFF_OACTIVE;
   1482 		return;
   1483 	}
   1484 
   1485 #if NBPFILTER > 0
   1486 	/*
   1487 	 * If there's a BPF listener, bounce a copy of this frame
   1488 	 * to him.
   1489 	 */
   1490 	if (ifp->if_bpf)
   1491 		BPF_MTAP(ifp, m_head);
   1492 #endif
   1493 
   1494 	ifp->if_flags |= IFF_OACTIVE;
   1495 
   1496 	/*
   1497 	 * Set a timeout in case the chip goes out to lunch.
   1498 	 */
   1499 	ifp->if_timer = 5;
   1500 }
   1501 
   1502 Static void
   1503 aue_init(xsc)
   1504 	void			*xsc;
   1505 {
   1506 	struct aue_softc	*sc = xsc;
   1507 	struct ifnet		*ifp = GET_IFP(sc);
   1508 	struct mii_data		*mii = GET_MII(sc);
   1509 	int			i, s;
   1510 	u_char			*eaddr;
   1511 
   1512 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1513 
   1514 	if (sc->aue_dying)
   1515 		return;
   1516 
   1517 	if (ifp->if_flags & IFF_RUNNING)
   1518 		return;
   1519 
   1520 	s = splimp();
   1521 
   1522 	/*
   1523 	 * Cancel pending I/O and free all RX/TX buffers.
   1524 	 */
   1525 	aue_reset(sc);
   1526 
   1527 #if defined(__FreeBSD__) || defined(__OpenBSD__)
   1528 	eaddr = sc->arpcom.ac_enaddr;
   1529 #elif defined(__NetBSD__)
   1530 	eaddr = LLADDR(ifp->if_sadl);
   1531 #endif /* defined(__NetBSD__) */
   1532 	for (i = 0; i < ETHER_ADDR_LEN; i++)
   1533 		aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
   1534 
   1535 	 /* If we want promiscuous mode, set the allframes bit. */
   1536 	if (ifp->if_flags & IFF_PROMISC)
   1537 		AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1538 	else
   1539 		AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1540 
   1541 	/* Init TX ring. */
   1542 	if (aue_tx_list_init(sc) == ENOBUFS) {
   1543 		printf("%s: tx list init failed\n", USBDEVNAME(sc->aue_dev));
   1544 		splx(s);
   1545 		return;
   1546 	}
   1547 
   1548 	/* Init RX ring. */
   1549 	if (aue_rx_list_init(sc) == ENOBUFS) {
   1550 		printf("%s: rx list init failed\n", USBDEVNAME(sc->aue_dev));
   1551 		splx(s);
   1552 		return;
   1553 	}
   1554 
   1555 	/* Load the multicast filter. */
   1556 	aue_setmulti(sc);
   1557 
   1558 	/* Enable RX and TX */
   1559 	aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
   1560 	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
   1561 	AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
   1562 
   1563 	mii_mediachg(mii);
   1564 
   1565 	if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
   1566 		if (aue_openpipes(sc)) {
   1567 			splx(s);
   1568 			return;
   1569 		}
   1570 	}
   1571 
   1572 	ifp->if_flags |= IFF_RUNNING;
   1573 	ifp->if_flags &= ~IFF_OACTIVE;
   1574 
   1575 	splx(s);
   1576 
   1577 	usb_callout(sc->aue_stat_ch, hz, aue_tick, sc);
   1578 }
   1579 
   1580 Static int
   1581 aue_openpipes(sc)
   1582 	struct aue_softc	*sc;
   1583 {
   1584 	struct aue_chain	*c;
   1585 	usbd_status		err;
   1586 	int i;
   1587 
   1588 	/* Open RX and TX pipes. */
   1589 	err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
   1590 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
   1591 	if (err) {
   1592 		printf("%s: open rx pipe failed: %s\n",
   1593 		    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1594 		return (EIO);
   1595 	}
   1596 	usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
   1597 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
   1598 	if (err) {
   1599 		printf("%s: open tx pipe failed: %s\n",
   1600 		    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1601 		return (EIO);
   1602 	}
   1603 	err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
   1604 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
   1605 	    &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
   1606 	    AUE_INTR_INTERVAL);
   1607 	if (err) {
   1608 		printf("%s: open intr pipe failed: %s\n",
   1609 		    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1610 		return (EIO);
   1611 	}
   1612 
   1613 	/* Start up the receive pipe. */
   1614 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
   1615 		c = &sc->aue_cdata.aue_rx_chain[i];
   1616 		usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
   1617 		    c, c->aue_buf, AUE_BUFSZ,
   1618 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
   1619 		    aue_rxeof);
   1620 		(void)usbd_transfer(c->aue_xfer); /* XXX */
   1621 		DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->aue_dev),
   1622 			    __FUNCTION__));
   1623 
   1624 	}
   1625 	return (0);
   1626 }
   1627 
   1628 /*
   1629  * Set media options.
   1630  */
   1631 Static int
   1632 aue_ifmedia_upd(ifp)
   1633 	struct ifnet		*ifp;
   1634 {
   1635 	struct aue_softc	*sc = ifp->if_softc;
   1636 	struct mii_data		*mii = GET_MII(sc);
   1637 
   1638 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1639 
   1640 	if (sc->aue_dying)
   1641 		return (0);
   1642 
   1643 	sc->aue_link = 0;
   1644 	if (mii->mii_instance) {
   1645 		struct mii_softc	*miisc;
   1646 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
   1647 		    miisc = LIST_NEXT(miisc, mii_list))
   1648 			 mii_phy_reset(miisc);
   1649 	}
   1650 	mii_mediachg(mii);
   1651 
   1652 	return (0);
   1653 }
   1654 
   1655 /*
   1656  * Report current media status.
   1657  */
   1658 Static void
   1659 aue_ifmedia_sts(ifp, ifmr)
   1660 	struct ifnet		*ifp;
   1661 	struct ifmediareq	*ifmr;
   1662 {
   1663 	struct aue_softc	*sc = ifp->if_softc;
   1664 	struct mii_data		*mii = GET_MII(sc);
   1665 
   1666 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1667 
   1668 	mii_pollstat(mii);
   1669 	ifmr->ifm_active = mii->mii_media_active;
   1670 	ifmr->ifm_status = mii->mii_media_status;
   1671 }
   1672 
   1673 Static int
   1674 aue_ioctl(ifp, command, data)
   1675 	struct ifnet		*ifp;
   1676 	u_long			command;
   1677 	caddr_t			data;
   1678 {
   1679 	struct aue_softc	*sc = ifp->if_softc;
   1680 #if defined(__NetBSD__) || defined(__OpenBSD__)
   1681 	struct ifaddr 		*ifa = (struct ifaddr *)data;
   1682 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1683 	struct ifreq		*ifr = (struct ifreq *)data;
   1684 	struct mii_data		*mii;
   1685 	int			s, error = 0;
   1686 
   1687 	if (sc->aue_dying)
   1688 		return (EIO);
   1689 
   1690 	s = splimp();
   1691 
   1692 	switch(command) {
   1693 #if defined(__FreeBSD__)
   1694 	case SIOCSIFADDR:
   1695 	case SIOCGIFADDR:
   1696 	case SIOCSIFMTU:
   1697 		error = ether_ioctl(ifp, command, data);
   1698 		break;
   1699 #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1700 	case SIOCSIFADDR:
   1701 		ifp->if_flags |= IFF_UP;
   1702 		aue_init(sc);
   1703 
   1704 		switch (ifa->ifa_addr->sa_family) {
   1705 #ifdef INET
   1706 		case AF_INET:
   1707 #if defined(__NetBSD__)
   1708 			arp_ifinit(ifp, ifa);
   1709 #else
   1710 			arp_ifinit(&sc->arpcom, ifa);
   1711 #endif
   1712 			break;
   1713 #endif /* INET */
   1714 #ifdef NS
   1715 		case AF_NS:
   1716 		    {
   1717 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1718 
   1719 			if (ns_nullhost(*ina))
   1720 				ina->x_host = *(union ns_host *)
   1721 					LLADDR(ifp->if_sadl);
   1722 			else
   1723 				memcpy(LLADDR(ifp->if_sadl),
   1724 				       ina->x_host.c_host,
   1725 				       ifp->if_addrlen);
   1726 			break;
   1727 		    }
   1728 #endif /* NS */
   1729 		}
   1730 		break;
   1731 
   1732 	case SIOCSIFMTU:
   1733 		if (ifr->ifr_mtu > ETHERMTU)
   1734 			error = EINVAL;
   1735 		else
   1736 			ifp->if_mtu = ifr->ifr_mtu;
   1737 		break;
   1738 
   1739 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
   1740 	case SIOCSIFFLAGS:
   1741 		if (ifp->if_flags & IFF_UP) {
   1742 			if (ifp->if_flags & IFF_RUNNING &&
   1743 			    ifp->if_flags & IFF_PROMISC &&
   1744 			    !(sc->aue_if_flags & IFF_PROMISC)) {
   1745 				AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1746 			} else if (ifp->if_flags & IFF_RUNNING &&
   1747 			    !(ifp->if_flags & IFF_PROMISC) &&
   1748 			    sc->aue_if_flags & IFF_PROMISC) {
   1749 				AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
   1750 			} else if (!(ifp->if_flags & IFF_RUNNING))
   1751 				aue_init(sc);
   1752 		} else {
   1753 			if (ifp->if_flags & IFF_RUNNING)
   1754 				aue_stop(sc);
   1755 		}
   1756 		sc->aue_if_flags = ifp->if_flags;
   1757 		error = 0;
   1758 		break;
   1759 	case SIOCADDMULTI:
   1760 	case SIOCDELMULTI:
   1761 		aue_setmulti(sc);
   1762 		error = 0;
   1763 		break;
   1764 	case SIOCGIFMEDIA:
   1765 	case SIOCSIFMEDIA:
   1766 		mii = GET_MII(sc);
   1767 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
   1768 		break;
   1769 	default:
   1770 		error = EINVAL;
   1771 		break;
   1772 	}
   1773 
   1774 	splx(s);
   1775 
   1776 	return (error);
   1777 }
   1778 
   1779 Static void
   1780 aue_watchdog(ifp)
   1781 	struct ifnet		*ifp;
   1782 {
   1783 	struct aue_softc	*sc = ifp->if_softc;
   1784 
   1785 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1786 
   1787 	ifp->if_oerrors++;
   1788 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->aue_dev));
   1789 
   1790 	/*
   1791 	 * The polling business is a kludge to avoid allowing the
   1792 	 * USB code to call tsleep() in usbd_delay_ms(), which will
   1793 	 * kill us since the watchdog routine is invoked from
   1794 	 * interrupt context.
   1795 	 */
   1796 	usbd_set_polling(sc->aue_udev, 1);
   1797 	aue_stop(sc);
   1798 	aue_init(sc);
   1799 	usbd_set_polling(sc->aue_udev, 0);
   1800 
   1801 	if (ifp->if_snd.ifq_head != NULL)
   1802 		aue_start(ifp);
   1803 }
   1804 
   1805 /*
   1806  * Stop the adapter and free any mbufs allocated to the
   1807  * RX and TX lists.
   1808  */
   1809 Static void
   1810 aue_stop(sc)
   1811 	struct aue_softc	*sc;
   1812 {
   1813 	usbd_status		err;
   1814 	struct ifnet		*ifp;
   1815 	int			i;
   1816 
   1817 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1818 
   1819 	ifp = GET_IFP(sc);
   1820 	ifp->if_timer = 0;
   1821 
   1822 	aue_csr_write_1(sc, AUE_CTL0, 0);
   1823 	aue_csr_write_1(sc, AUE_CTL1, 0);
   1824 	aue_reset(sc);
   1825 	usb_uncallout(sc->aue_stat_ch, aue_tick, sc);
   1826 
   1827 	/* Stop transfers. */
   1828 	if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
   1829 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
   1830 		if (err) {
   1831 			printf("%s: abort rx pipe failed: %s\n",
   1832 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1833 		}
   1834 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
   1835 		if (err) {
   1836 			printf("%s: close rx pipe failed: %s\n",
   1837 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1838 		}
   1839 		sc->aue_ep[AUE_ENDPT_RX] = NULL;
   1840 	}
   1841 
   1842 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
   1843 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
   1844 		if (err) {
   1845 			printf("%s: abort tx pipe failed: %s\n",
   1846 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1847 		}
   1848 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
   1849 		if (err) {
   1850 			printf("%s: close tx pipe failed: %s\n",
   1851 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1852 		}
   1853 		sc->aue_ep[AUE_ENDPT_TX] = NULL;
   1854 	}
   1855 
   1856 	if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
   1857 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
   1858 		if (err) {
   1859 			printf("%s: abort intr pipe failed: %s\n",
   1860 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1861 		}
   1862 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
   1863 		if (err) {
   1864 			printf("%s: close intr pipe failed: %s\n",
   1865 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
   1866 		}
   1867 		sc->aue_ep[AUE_ENDPT_INTR] = NULL;
   1868 	}
   1869 
   1870 	/* Free RX resources. */
   1871 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
   1872 		if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
   1873 			m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
   1874 			sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
   1875 		}
   1876 		if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
   1877 			usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
   1878 			sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
   1879 		}
   1880 	}
   1881 
   1882 	/* Free TX resources. */
   1883 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
   1884 		if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
   1885 			m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
   1886 			sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
   1887 		}
   1888 		if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
   1889 			usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
   1890 			sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
   1891 		}
   1892 	}
   1893 
   1894 	sc->aue_link = 0;
   1895 
   1896 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1897 }
   1898 
   1899 #ifdef __FreeBSD__
   1900 /*
   1901  * Stop all chip I/O so that the kernel's probe routines don't
   1902  * get confused by errant DMAs when rebooting.
   1903  */
   1904 Static void
   1905 aue_shutdown(dev)
   1906 	device_ptr_t		dev;
   1907 {
   1908 	struct aue_softc	*sc = USBGETSOFTC(dev);
   1909 
   1910 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
   1911 
   1912 	aue_reset(sc);
   1913 	aue_stop(sc);
   1914 }
   1915 #endif
   1916