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