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