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