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