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