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