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