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