Home | History | Annotate | Line # | Download | only in usb
if_udav.c revision 1.43.4.14
      1  1.43.4.14     skrll /*	$NetBSD: if_udav.c,v 1.43.4.14 2017/02/05 13:40:46 skrll Exp $	*/
      2        1.1    itojun /*	$nabe: if_udav.c,v 1.3 2003/08/21 16:57:19 nabe Exp $	*/
      3       1.37       mrg 
      4        1.1    itojun /*
      5        1.1    itojun  * Copyright (c) 2003
      6        1.1    itojun  *     Shingo WATANABE <nabe (at) nabechan.org>.  All rights reserved.
      7        1.1    itojun  *
      8        1.1    itojun  * Redistribution and use in source and binary forms, with or without
      9        1.1    itojun  * modification, are permitted provided that the following conditions
     10        1.1    itojun  * are met:
     11        1.1    itojun  * 1. Redistributions of source code must retain the above copyright
     12        1.1    itojun  *    notice, this list of conditions and the following disclaimer.
     13        1.1    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14        1.1    itojun  *    notice, this list of conditions and the following disclaimer in the
     15        1.1    itojun  *    documentation and/or other materials provided with the distribution.
     16        1.2   tsutsui  * 3. Neither the name of the author nor the names of any co-contributors
     17        1.1    itojun  *    may be used to endorse or promote products derived from this software
     18        1.1    itojun  *    without specific prior written permission.
     19        1.1    itojun  *
     20        1.1    itojun  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     21        1.1    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22        1.1    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23        1.1    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     24        1.1    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25        1.1    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26        1.1    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27        1.1    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28        1.1    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29        1.1    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30        1.1    itojun  * SUCH DAMAGE.
     31        1.1    itojun  *
     32        1.1    itojun  */
     33        1.1    itojun 
     34        1.1    itojun /*
     35        1.1    itojun  * DM9601(DAVICOM USB to Ethernet MAC Controller with Integrated 10/100 PHY)
     36        1.1    itojun  * The spec can be found at the following url.
     37        1.1    itojun  *   http://www.davicom.com.tw/big5/download/Data%20Sheet/DM9601-DS-F01-062202s.pdf
     38        1.1    itojun  */
     39        1.1    itojun 
     40        1.1    itojun /*
     41        1.1    itojun  * TODO:
     42        1.1    itojun  *	Interrupt Endpoint support
     43        1.1    itojun  *	External PHYs
     44        1.1    itojun  *	powerhook() support?
     45        1.1    itojun  */
     46        1.1    itojun 
     47        1.1    itojun #include <sys/cdefs.h>
     48  1.43.4.14     skrll __KERNEL_RCSID(0, "$NetBSD: if_udav.c,v 1.43.4.14 2017/02/05 13:40:46 skrll Exp $");
     49        1.1    itojun 
     50       1.40  christos #ifdef _KERNEL_OPT
     51        1.1    itojun #include "opt_inet.h"
     52  1.43.4.11     skrll #include "opt_usb.h"
     53       1.40  christos #endif
     54        1.1    itojun 
     55        1.1    itojun #include <sys/param.h>
     56        1.1    itojun #include <sys/systm.h>
     57       1.19        ad #include <sys/mutex.h>
     58        1.1    itojun #include <sys/mbuf.h>
     59        1.1    itojun #include <sys/kernel.h>
     60        1.1    itojun #include <sys/socket.h>
     61        1.1    itojun #include <sys/device.h>
     62   1.43.4.6     skrll #include <sys/rndsource.h>
     63        1.1    itojun 
     64        1.1    itojun #include <net/if.h>
     65        1.1    itojun #include <net/if_arp.h>
     66        1.1    itojun #include <net/if_dl.h>
     67        1.1    itojun #include <net/if_media.h>
     68        1.1    itojun 
     69        1.1    itojun #include <net/bpf.h>
     70        1.1    itojun 
     71        1.1    itojun #include <net/if_ether.h>
     72        1.1    itojun #ifdef INET
     73        1.1    itojun #include <netinet/in.h>
     74        1.1    itojun #include <netinet/if_inarp.h>
     75        1.1    itojun #endif
     76        1.1    itojun 
     77        1.1    itojun #include <dev/mii/mii.h>
     78        1.1    itojun #include <dev/mii/miivar.h>
     79        1.1    itojun 
     80        1.1    itojun #include <dev/usb/usb.h>
     81        1.1    itojun #include <dev/usb/usbdi.h>
     82        1.1    itojun #include <dev/usb/usbdi_util.h>
     83        1.1    itojun #include <dev/usb/usbdevs.h>
     84        1.1    itojun 
     85        1.1    itojun #include <dev/usb/if_udavreg.h>
     86        1.1    itojun 
     87        1.1    itojun 
     88        1.1    itojun /* Function declarations */
     89  1.43.4.10     skrll int	udav_match(device_t, cfdata_t, void *);
     90  1.43.4.10     skrll void	udav_attach(device_t, device_t, void *);
     91  1.43.4.10     skrll int	udav_detach(device_t, int);
     92  1.43.4.10     skrll int	udav_activate(device_t, enum devact);
     93       1.31    dyoung extern struct cfdriver udav_cd;
     94  1.43.4.10     skrll CFATTACH_DECL_NEW(udav, sizeof(struct udav_softc), udav_match, udav_attach,
     95  1.43.4.10     skrll     udav_detach, udav_activate);
     96        1.1    itojun 
     97        1.1    itojun Static int udav_openpipes(struct udav_softc *);
     98        1.1    itojun Static int udav_rx_list_init(struct udav_softc *);
     99  1.43.4.12     skrll Static void udav_rx_list_free(struct udav_softc *);
    100        1.1    itojun Static int udav_tx_list_init(struct udav_softc *);
    101  1.43.4.12     skrll Static void udav_tx_list_free(struct udav_softc *);
    102        1.1    itojun Static int udav_newbuf(struct udav_softc *, struct udav_chain *, struct mbuf *);
    103        1.1    itojun Static void udav_start(struct ifnet *);
    104  1.43.4.12     skrll Static void udav_start_locked(struct ifnet *);
    105        1.1    itojun Static int udav_send(struct udav_softc *, struct mbuf *, int);
    106   1.43.4.4     skrll Static void udav_txeof(struct usbd_xfer *, void *, usbd_status);
    107   1.43.4.4     skrll Static void udav_rxeof(struct usbd_xfer *, void *, usbd_status);
    108        1.1    itojun Static void udav_tick(void *);
    109        1.1    itojun Static void udav_tick_task(void *);
    110       1.16  christos Static int udav_ioctl(struct ifnet *, u_long, void *);
    111        1.1    itojun Static void udav_stop_task(struct udav_softc *);
    112        1.1    itojun Static void udav_stop(struct ifnet *, int);
    113  1.43.4.12     skrll Static void udav_stop_locked(struct ifnet *, int);
    114        1.1    itojun Static void udav_watchdog(struct ifnet *);
    115        1.1    itojun Static int udav_ifmedia_change(struct ifnet *);
    116        1.1    itojun Static void udav_ifmedia_status(struct ifnet *, struct ifmediareq *);
    117        1.1    itojun Static void udav_lock_mii(struct udav_softc *);
    118        1.1    itojun Static void udav_unlock_mii(struct udav_softc *);
    119       1.31    dyoung Static int udav_miibus_readreg(device_t, int, int);
    120       1.31    dyoung Static void udav_miibus_writereg(device_t, int, int, int);
    121       1.38      matt Static void udav_miibus_statchg(struct ifnet *);
    122        1.1    itojun Static int udav_init(struct ifnet *);
    123  1.43.4.12     skrll Static int udav_init_locked(struct ifnet *);
    124        1.1    itojun Static void udav_setmulti(struct udav_softc *);
    125        1.1    itojun Static void udav_reset(struct udav_softc *);
    126        1.1    itojun 
    127        1.1    itojun Static int udav_csr_read(struct udav_softc *, int, void *, int);
    128        1.1    itojun Static int udav_csr_write(struct udav_softc *, int, void *, int);
    129        1.1    itojun Static int udav_csr_read1(struct udav_softc *, int);
    130        1.1    itojun Static int udav_csr_write1(struct udav_softc *, int, unsigned char);
    131        1.1    itojun 
    132        1.1    itojun #if 0
    133        1.1    itojun Static int udav_mem_read(struct udav_softc *, int, void *, int);
    134        1.1    itojun Static int udav_mem_write(struct udav_softc *, int, void *, int);
    135        1.1    itojun Static int udav_mem_write1(struct udav_softc *, int, unsigned char);
    136        1.1    itojun #endif
    137        1.1    itojun 
    138        1.1    itojun /* Macros */
    139        1.1    itojun #ifdef UDAV_DEBUG
    140       1.31    dyoung #define DPRINTF(x)	if (udavdebug) printf x
    141       1.31    dyoung #define DPRINTFN(n,x)	if (udavdebug >= (n)) printf x
    142        1.1    itojun int udavdebug = 0;
    143        1.1    itojun #else
    144        1.1    itojun #define DPRINTF(x)
    145        1.1    itojun #define DPRINTFN(n,x)
    146        1.1    itojun #endif
    147        1.1    itojun 
    148        1.1    itojun #define	UDAV_SETBIT(sc, reg, x)	\
    149        1.1    itojun 	udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) | (x))
    150        1.1    itojun 
    151        1.1    itojun #define	UDAV_CLRBIT(sc, reg, x)	\
    152        1.1    itojun 	udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) & ~(x))
    153        1.1    itojun 
    154        1.1    itojun static const struct udav_type {
    155        1.1    itojun 	struct usb_devno udav_dev;
    156   1.43.4.1     skrll 	uint16_t udav_flags;
    157        1.1    itojun #define UDAV_EXT_PHY	0x0001
    158       1.42  jakllsch #define UDAV_NO_PHY	0x0002
    159        1.1    itojun } udav_devs [] = {
    160        1.1    itojun 	/* Corega USB-TXC */
    161        1.1    itojun 	{{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXC }, 0},
    162       1.18   xtraeme 	/* ShanTou ST268 USB NIC */
    163       1.18   xtraeme 	{{ USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ST268_USB_NIC }, 0},
    164       1.22  drochner 	/* ShanTou ADM8515 */
    165       1.22  drochner 	{{ USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ADM8515 }, 0},
    166       1.30  jmcneill 	/* SUNRISING SR9600 */
    167       1.30  jmcneill 	{{ USB_VENDOR_SUNRISING, USB_PRODUCT_SUNRISING_SR9600 }, 0 },
    168       1.42  jakllsch 	/* SUNRISING QF9700 */
    169       1.42  jakllsch 	{{ USB_VENDOR_SUNRISING, USB_PRODUCT_SUNRISING_QF9700 }, UDAV_NO_PHY },
    170       1.35   mbalmer 	/* QUAN DM9601 */
    171       1.35   mbalmer 	{{USB_VENDOR_QUAN, USB_PRODUCT_QUAN_DM9601 }, 0},
    172        1.1    itojun #if 0
    173        1.1    itojun 	/* DAVICOM DM9601 Generic? */
    174        1.1    itojun 	/*  XXX: The following ids was obtained from the data sheet. */
    175        1.1    itojun 	{{ 0x0a46, 0x9601 }, 0},
    176        1.1    itojun #endif
    177        1.1    itojun };
    178        1.7  christos #define udav_lookup(v, p) ((const struct udav_type *)usb_lookup(udav_devs, v, p))
    179        1.1    itojun 
    180        1.1    itojun 
    181        1.1    itojun /* Probe */
    182       1.40  christos int
    183       1.31    dyoung udav_match(device_t parent, cfdata_t match, void *aux)
    184        1.1    itojun {
    185       1.31    dyoung 	struct usb_attach_arg *uaa = aux;
    186        1.1    itojun 
    187   1.43.4.5     skrll 	return udav_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    188   1.43.4.3     skrll 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    189        1.1    itojun }
    190        1.1    itojun 
    191        1.1    itojun /* Attach */
    192       1.40  christos void
    193       1.31    dyoung udav_attach(device_t parent, device_t self, void *aux)
    194        1.1    itojun {
    195       1.31    dyoung 	struct udav_softc *sc = device_private(self);
    196       1.31    dyoung 	struct usb_attach_arg *uaa = aux;
    197   1.43.4.5     skrll 	struct usbd_device *dev = uaa->uaa_device;
    198   1.43.4.4     skrll 	struct usbd_interface *iface;
    199        1.1    itojun 	usbd_status err;
    200        1.1    itojun 	usb_interface_descriptor_t *id;
    201        1.1    itojun 	usb_endpoint_descriptor_t *ed;
    202        1.6  augustss 	char *devinfop;
    203        1.1    itojun 	struct ifnet *ifp;
    204        1.1    itojun 	struct mii_data *mii;
    205        1.1    itojun 	u_char eaddr[ETHER_ADDR_LEN];
    206  1.43.4.12     skrll 	int i;
    207        1.1    itojun 
    208       1.24      cube 	sc->sc_dev = self;
    209       1.24      cube 
    210       1.26    plunky 	aprint_naive("\n");
    211       1.26    plunky 	aprint_normal("\n");
    212       1.26    plunky 
    213        1.6  augustss 	devinfop = usbd_devinfo_alloc(dev, 0);
    214       1.24      cube 	aprint_normal_dev(self, "%s\n", devinfop);
    215        1.6  augustss 	usbd_devinfo_free(devinfop);
    216        1.1    itojun 
    217        1.1    itojun 	/* Move the device into the configured state. */
    218       1.22  drochner 	err = usbd_set_config_no(dev, UDAV_CONFIG_NO, 1); /* idx 0 */
    219        1.1    itojun 	if (err) {
    220       1.39     skrll 		aprint_error_dev(self, "failed to set configuration"
    221       1.39     skrll 		    ", err=%s\n", usbd_errstr(err));
    222        1.1    itojun 		goto bad;
    223        1.1    itojun 	}
    224        1.1    itojun 
    225       1.41  jmcneill 	usb_init_task(&sc->sc_tick_task, udav_tick_task, sc, 0);
    226       1.19        ad 	mutex_init(&sc->sc_mii_lock, MUTEX_DEFAULT, IPL_NONE);
    227  1.43.4.12     skrll 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    228  1.43.4.12     skrll 	mutex_init(&sc->sc_txlock, MUTEX_DEFAULT, IPL_SOFTUSB);
    229  1.43.4.12     skrll 	mutex_init(&sc->sc_rxlock, MUTEX_DEFAULT, IPL_SOFTUSB);
    230  1.43.4.10     skrll 	usb_init_task(&sc->sc_stop_task, (void (*)(void *))udav_stop_task, sc,
    231  1.43.4.10     skrll 	    0);
    232        1.1    itojun 
    233        1.1    itojun 	/* get control interface */
    234        1.1    itojun 	err = usbd_device2interface_handle(dev, UDAV_IFACE_INDEX, &iface);
    235        1.1    itojun 	if (err) {
    236       1.24      cube 		aprint_error_dev(self, "failed to get interface, err=%s\n",
    237        1.1    itojun 		       usbd_errstr(err));
    238        1.1    itojun 		goto bad;
    239        1.1    itojun 	}
    240        1.1    itojun 
    241        1.1    itojun 	sc->sc_udev = dev;
    242        1.1    itojun 	sc->sc_ctl_iface = iface;
    243  1.43.4.10     skrll 	sc->sc_flags = udav_lookup(uaa->uaa_vendor,
    244  1.43.4.10     skrll 	    uaa->uaa_product)->udav_flags;
    245        1.1    itojun 
    246        1.1    itojun 	/* get interface descriptor */
    247        1.1    itojun 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
    248        1.1    itojun 
    249        1.1    itojun 	/* find endpoints */
    250        1.1    itojun 	sc->sc_bulkin_no = sc->sc_bulkout_no = sc->sc_intrin_no = -1;
    251        1.1    itojun 	for (i = 0; i < id->bNumEndpoints; i++) {
    252        1.1    itojun 		ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
    253        1.1    itojun 		if (ed == NULL) {
    254       1.24      cube 			aprint_error_dev(self, "couldn't get endpoint %d\n", i);
    255        1.1    itojun 			goto bad;
    256        1.1    itojun 		}
    257        1.1    itojun 		if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
    258        1.1    itojun 		    UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
    259        1.1    itojun 			sc->sc_bulkin_no = ed->bEndpointAddress; /* RX */
    260        1.1    itojun 		else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
    261        1.1    itojun 			 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT)
    262        1.1    itojun 			sc->sc_bulkout_no = ed->bEndpointAddress; /* TX */
    263        1.1    itojun 		else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT &&
    264        1.1    itojun 			 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
    265        1.1    itojun 			sc->sc_intrin_no = ed->bEndpointAddress; /* Status */
    266        1.1    itojun 	}
    267        1.1    itojun 
    268        1.1    itojun 	if (sc->sc_bulkin_no == -1 || sc->sc_bulkout_no == -1 ||
    269        1.1    itojun 	    sc->sc_intrin_no == -1) {
    270       1.24      cube 		aprint_error_dev(self, "missing endpoint\n");
    271        1.1    itojun 		goto bad;
    272        1.1    itojun 	}
    273        1.1    itojun 
    274        1.1    itojun 	/* reset the adapter */
    275        1.1    itojun 	udav_reset(sc);
    276        1.1    itojun 
    277        1.1    itojun 	/* Get Ethernet Address */
    278        1.1    itojun 	err = udav_csr_read(sc, UDAV_PAR, (void *)eaddr, ETHER_ADDR_LEN);
    279        1.1    itojun 	if (err) {
    280       1.24      cube 		aprint_error_dev(self, "read MAC address failed\n");
    281        1.1    itojun 		goto bad;
    282        1.1    itojun 	}
    283        1.1    itojun 
    284        1.1    itojun 	/* Print Ethernet Address */
    285       1.24      cube 	aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
    286        1.1    itojun 
    287        1.9       wiz 	/* initialize interface information */
    288        1.1    itojun 	ifp = GET_IFP(sc);
    289        1.1    itojun 	ifp->if_softc = sc;
    290        1.1    itojun 	ifp->if_mtu = ETHERMTU;
    291  1.43.4.14     skrll 	strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
    292        1.1    itojun 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    293  1.43.4.12     skrll 	ifp->if_extflags = IFEF_START_MPSAFE;
    294        1.1    itojun 	ifp->if_start = udav_start;
    295        1.1    itojun 	ifp->if_ioctl = udav_ioctl;
    296        1.1    itojun 	ifp->if_watchdog = udav_watchdog;
    297        1.1    itojun 	ifp->if_init = udav_init;
    298        1.1    itojun 	ifp->if_stop = udav_stop;
    299        1.1    itojun 
    300        1.1    itojun 	IFQ_SET_READY(&ifp->if_snd);
    301        1.1    itojun 
    302       1.42  jakllsch 	if (ISSET(sc->sc_flags, UDAV_NO_PHY)) {
    303       1.42  jakllsch 		sc->sc_link = 1;
    304       1.42  jakllsch 		goto skipmii;
    305       1.42  jakllsch 	}
    306       1.42  jakllsch 
    307        1.1    itojun 	/*
    308        1.1    itojun 	 * Do ifmedia setup.
    309        1.1    itojun 	 */
    310        1.1    itojun 	mii = &sc->sc_mii;
    311        1.1    itojun 	mii->mii_ifp = ifp;
    312        1.1    itojun 	mii->mii_readreg = udav_miibus_readreg;
    313        1.1    itojun 	mii->mii_writereg = udav_miibus_writereg;
    314        1.1    itojun 	mii->mii_statchg = udav_miibus_statchg;
    315        1.1    itojun 	mii->mii_flags = MIIF_AUTOTSLEEP;
    316       1.21    dyoung 	sc->sc_ec.ec_mii = mii;
    317        1.1    itojun 	ifmedia_init(&mii->mii_media, 0,
    318        1.1    itojun 		     udav_ifmedia_change, udav_ifmedia_status);
    319        1.1    itojun 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
    320        1.1    itojun 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
    321        1.1    itojun 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
    322        1.1    itojun 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
    323        1.1    itojun 	} else
    324        1.1    itojun 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    325        1.1    itojun 
    326       1.42  jakllsch skipmii:
    327        1.1    itojun 	/* attach the interface */
    328  1.43.4.12     skrll 	if_initialize(ifp);
    329  1.43.4.12     skrll 	sc->sc_ipq = if_percpuq_create(&sc->sc_ec.ec_if);
    330       1.31    dyoung 	ether_ifattach(ifp, eaddr);
    331  1.43.4.12     skrll 	if_register(ifp);
    332        1.1    itojun 
    333       1.24      cube 	rnd_attach_source(&sc->rnd_source, device_xname(self),
    334       1.43       tls 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
    335        1.1    itojun 
    336       1.31    dyoung 	callout_init(&sc->sc_stat_ch, 0);
    337        1.1    itojun 	sc->sc_attached = 1;
    338        1.1    itojun 
    339       1.31    dyoung 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, sc->sc_dev);
    340        1.1    itojun 
    341       1.31    dyoung 	return;
    342        1.1    itojun 
    343        1.1    itojun  bad:
    344        1.1    itojun 	sc->sc_dying = 1;
    345       1.31    dyoung 	return;
    346        1.1    itojun }
    347        1.1    itojun 
    348        1.1    itojun /* detach */
    349       1.40  christos int
    350       1.31    dyoung udav_detach(device_t self, int flags)
    351        1.1    itojun {
    352       1.31    dyoung 	struct udav_softc *sc = device_private(self);
    353        1.1    itojun 	struct ifnet *ifp = GET_IFP(sc);
    354        1.1    itojun 	int s;
    355        1.1    itojun 
    356       1.31    dyoung 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    357        1.1    itojun 
    358        1.1    itojun 	/* Detached before attached finished */
    359        1.1    itojun 	if (!sc->sc_attached)
    360   1.43.4.3     skrll 		return 0;
    361        1.1    itojun 
    362       1.31    dyoung 	callout_stop(&sc->sc_stat_ch);
    363        1.1    itojun 
    364        1.1    itojun 	/* Remove any pending tasks */
    365        1.1    itojun 	usb_rem_task(sc->sc_udev, &sc->sc_tick_task);
    366        1.1    itojun 	usb_rem_task(sc->sc_udev, &sc->sc_stop_task);
    367        1.1    itojun 
    368        1.1    itojun 	s = splusb();
    369        1.1    itojun 
    370        1.1    itojun 	if (--sc->sc_refcnt >= 0) {
    371        1.1    itojun 		/* Wait for processes to go away */
    372       1.36       mrg 		usb_detach_waitold(sc->sc_dev);
    373        1.1    itojun 	}
    374        1.1    itojun 	if (ifp->if_flags & IFF_RUNNING)
    375        1.1    itojun 		udav_stop(GET_IFP(sc), 1);
    376        1.1    itojun 
    377        1.1    itojun 	rnd_detach_source(&sc->rnd_source);
    378        1.1    itojun 	mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
    379        1.1    itojun 	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
    380        1.1    itojun 	ether_ifdetach(ifp);
    381        1.1    itojun 	if_detach(ifp);
    382        1.1    itojun 
    383        1.1    itojun #ifdef DIAGNOSTIC
    384        1.1    itojun 	if (sc->sc_pipe_tx != NULL)
    385       1.24      cube 		aprint_debug_dev(self, "detach has active tx endpoint.\n");
    386        1.1    itojun 	if (sc->sc_pipe_rx != NULL)
    387       1.24      cube 		aprint_debug_dev(self, "detach has active rx endpoint.\n");
    388        1.1    itojun 	if (sc->sc_pipe_intr != NULL)
    389       1.24      cube 		aprint_debug_dev(self, "detach has active intr endpoint.\n");
    390        1.1    itojun #endif
    391        1.1    itojun 	sc->sc_attached = 0;
    392        1.1    itojun 
    393        1.1    itojun 	splx(s);
    394        1.1    itojun 
    395  1.43.4.10     skrll 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    396        1.1    itojun 
    397       1.19        ad 	mutex_destroy(&sc->sc_mii_lock);
    398       1.19        ad 
    399  1.43.4.13     skrll 	mutex_destroy(&sc->sc_txlock);
    400  1.43.4.13     skrll 	mutex_destroy(&sc->sc_rxlock);
    401  1.43.4.13     skrll 	mutex_destroy(&sc->sc_lock);
    402  1.43.4.13     skrll 
    403   1.43.4.3     skrll 	return 0;
    404        1.1    itojun }
    405        1.1    itojun 
    406        1.1    itojun #if 0
    407        1.1    itojun /* read memory */
    408        1.1    itojun Static int
    409        1.1    itojun udav_mem_read(struct udav_softc *sc, int offset, void *buf, int len)
    410        1.1    itojun {
    411        1.1    itojun 	usb_device_request_t req;
    412        1.1    itojun 	usbd_status err;
    413        1.1    itojun 
    414        1.1    itojun 	if (sc == NULL)
    415   1.43.4.3     skrll 		return 0;
    416        1.1    itojun 
    417        1.1    itojun 	DPRINTFN(0x200,
    418       1.31    dyoung 		("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    419        1.1    itojun 
    420        1.1    itojun 	if (sc->sc_dying)
    421   1.43.4.3     skrll 		return 0;
    422        1.1    itojun 
    423        1.1    itojun 	offset &= 0xffff;
    424        1.1    itojun 	len &= 0xff;
    425        1.1    itojun 
    426        1.1    itojun 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    427        1.1    itojun 	req.bRequest = UDAV_REQ_MEM_READ;
    428        1.1    itojun 	USETW(req.wValue, 0x0000);
    429        1.1    itojun 	USETW(req.wIndex, offset);
    430        1.1    itojun 	USETW(req.wLength, len);
    431        1.1    itojun 
    432        1.1    itojun 	sc->sc_refcnt++;
    433        1.1    itojun 	err = usbd_do_request(sc->sc_udev, &req, buf);
    434        1.1    itojun 	if (--sc->sc_refcnt < 0)
    435       1.36       mrg 		usb_detach_wakeupold(sc->sc_dev);
    436        1.1    itojun 	if (err) {
    437        1.1    itojun 		DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
    438       1.31    dyoung 			 device_xname(sc->sc_dev), __func__, offset, err));
    439        1.1    itojun 	}
    440        1.1    itojun 
    441   1.43.4.3     skrll 	return err;
    442        1.1    itojun }
    443        1.1    itojun 
    444        1.1    itojun /* write memory */
    445        1.1    itojun Static int
    446        1.1    itojun udav_mem_write(struct udav_softc *sc, int offset, void *buf, int len)
    447        1.1    itojun {
    448        1.1    itojun 	usb_device_request_t req;
    449        1.1    itojun 	usbd_status err;
    450        1.1    itojun 
    451        1.1    itojun 	if (sc == NULL)
    452   1.43.4.3     skrll 		return 0;
    453        1.1    itojun 
    454        1.1    itojun 	DPRINTFN(0x200,
    455       1.31    dyoung 		("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    456        1.1    itojun 
    457        1.1    itojun 	if (sc->sc_dying)
    458   1.43.4.3     skrll 		return 0;
    459        1.1    itojun 
    460        1.1    itojun 	offset &= 0xffff;
    461        1.1    itojun 	len &= 0xff;
    462        1.1    itojun 
    463        1.1    itojun 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    464        1.1    itojun 	req.bRequest = UDAV_REQ_MEM_WRITE;
    465        1.1    itojun 	USETW(req.wValue, 0x0000);
    466        1.1    itojun 	USETW(req.wIndex, offset);
    467        1.1    itojun 	USETW(req.wLength, len);
    468        1.1    itojun 
    469        1.1    itojun 	sc->sc_refcnt++;
    470        1.1    itojun 	err = usbd_do_request(sc->sc_udev, &req, buf);
    471        1.1    itojun 	if (--sc->sc_refcnt < 0)
    472       1.36       mrg 		usb_detach_wakeupold(sc->sc_dev);
    473        1.1    itojun 	if (err) {
    474        1.1    itojun 		DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
    475       1.31    dyoung 			 device_xname(sc->sc_dev), __func__, offset, err));
    476        1.1    itojun 	}
    477        1.1    itojun 
    478   1.43.4.3     skrll 	return err;
    479        1.1    itojun }
    480        1.1    itojun 
    481        1.1    itojun /* write memory */
    482        1.1    itojun Static int
    483        1.1    itojun udav_mem_write1(struct udav_softc *sc, int offset, unsigned char ch)
    484        1.1    itojun {
    485        1.1    itojun 	usb_device_request_t req;
    486        1.1    itojun 	usbd_status err;
    487        1.1    itojun 
    488        1.1    itojun 	if (sc == NULL)
    489   1.43.4.3     skrll 		return 0;
    490        1.1    itojun 
    491        1.1    itojun 	DPRINTFN(0x200,
    492       1.31    dyoung 		("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    493        1.1    itojun 
    494        1.1    itojun 	if (sc->sc_dying)
    495   1.43.4.3     skrll 		return 0;
    496        1.1    itojun 
    497        1.1    itojun 	offset &= 0xffff;
    498        1.1    itojun 
    499        1.1    itojun 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    500        1.1    itojun 	req.bRequest = UDAV_REQ_MEM_WRITE1;
    501        1.1    itojun 	USETW(req.wValue, ch);
    502        1.1    itojun 	USETW(req.wIndex, offset);
    503        1.1    itojun 	USETW(req.wLength, 0x0000);
    504        1.1    itojun 
    505        1.1    itojun 	sc->sc_refcnt++;
    506        1.1    itojun 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    507        1.1    itojun 	if (--sc->sc_refcnt < 0)
    508       1.36       mrg 		usb_detach_wakeupold(sc->sc_dev);
    509        1.1    itojun 	if (err) {
    510        1.1    itojun 		DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
    511       1.31    dyoung 			 device_xname(sc->sc_dev), __func__, offset, err));
    512        1.1    itojun 	}
    513        1.1    itojun 
    514   1.43.4.3     skrll 	return err;
    515        1.1    itojun }
    516        1.1    itojun #endif
    517        1.1    itojun 
    518        1.1    itojun /* read register(s) */
    519        1.1    itojun Static int
    520        1.1    itojun udav_csr_read(struct udav_softc *sc, int offset, void *buf, int len)
    521        1.1    itojun {
    522        1.1    itojun 	usb_device_request_t req;
    523        1.1    itojun 	usbd_status err;
    524        1.1    itojun 
    525        1.1    itojun 	if (sc == NULL)
    526   1.43.4.3     skrll 		return 0;
    527        1.1    itojun 
    528        1.1    itojun 	DPRINTFN(0x200,
    529       1.31    dyoung 		("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    530        1.1    itojun 
    531        1.1    itojun 	if (sc->sc_dying)
    532   1.43.4.3     skrll 		return 0;
    533        1.1    itojun 
    534        1.1    itojun 	offset &= 0xff;
    535        1.1    itojun 	len &= 0xff;
    536        1.1    itojun 
    537        1.1    itojun 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
    538        1.1    itojun 	req.bRequest = UDAV_REQ_REG_READ;
    539        1.1    itojun 	USETW(req.wValue, 0x0000);
    540        1.1    itojun 	USETW(req.wIndex, offset);
    541        1.1    itojun 	USETW(req.wLength, len);
    542        1.1    itojun 
    543        1.1    itojun 	sc->sc_refcnt++;
    544        1.1    itojun 	err = usbd_do_request(sc->sc_udev, &req, buf);
    545        1.1    itojun 	if (--sc->sc_refcnt < 0)
    546       1.36       mrg 		usb_detach_wakeupold(sc->sc_dev);
    547        1.1    itojun 	if (err) {
    548        1.1    itojun 		DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
    549       1.31    dyoung 			 device_xname(sc->sc_dev), __func__, offset, err));
    550        1.1    itojun 	}
    551        1.1    itojun 
    552   1.43.4.3     skrll 	return err;
    553        1.1    itojun }
    554        1.1    itojun 
    555        1.1    itojun /* write register(s) */
    556        1.1    itojun Static int
    557        1.1    itojun udav_csr_write(struct udav_softc *sc, int offset, void *buf, int len)
    558        1.1    itojun {
    559        1.1    itojun 	usb_device_request_t req;
    560        1.1    itojun 	usbd_status err;
    561        1.1    itojun 
    562        1.1    itojun 	if (sc == NULL)
    563   1.43.4.3     skrll 		return 0;
    564        1.1    itojun 
    565        1.1    itojun 	DPRINTFN(0x200,
    566       1.31    dyoung 		("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    567        1.1    itojun 
    568        1.1    itojun 	if (sc->sc_dying)
    569   1.43.4.3     skrll 		return 0;
    570        1.1    itojun 
    571        1.1    itojun 	offset &= 0xff;
    572        1.1    itojun 	len &= 0xff;
    573        1.1    itojun 
    574        1.1    itojun 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    575        1.1    itojun 	req.bRequest = UDAV_REQ_REG_WRITE;
    576        1.1    itojun 	USETW(req.wValue, 0x0000);
    577        1.1    itojun 	USETW(req.wIndex, offset);
    578        1.1    itojun 	USETW(req.wLength, len);
    579        1.1    itojun 
    580        1.1    itojun 	sc->sc_refcnt++;
    581        1.1    itojun 	err = usbd_do_request(sc->sc_udev, &req, buf);
    582        1.1    itojun 	if (--sc->sc_refcnt < 0)
    583       1.36       mrg 		usb_detach_wakeupold(sc->sc_dev);
    584        1.1    itojun 	if (err) {
    585        1.1    itojun 		DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
    586       1.31    dyoung 			 device_xname(sc->sc_dev), __func__, offset, err));
    587        1.1    itojun 	}
    588        1.1    itojun 
    589   1.43.4.3     skrll 	return err;
    590        1.1    itojun }
    591        1.1    itojun 
    592        1.1    itojun Static int
    593        1.1    itojun udav_csr_read1(struct udav_softc *sc, int offset)
    594        1.1    itojun {
    595   1.43.4.1     skrll 	uint8_t val = 0;
    596        1.5     perry 
    597        1.1    itojun 	if (sc == NULL)
    598   1.43.4.3     skrll 		return 0;
    599        1.1    itojun 
    600        1.1    itojun 	DPRINTFN(0x200,
    601       1.31    dyoung 		("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    602        1.1    itojun 
    603        1.1    itojun 	if (sc->sc_dying)
    604   1.43.4.3     skrll 		return 0;
    605        1.1    itojun 
    606   1.43.4.3     skrll 	return udav_csr_read(sc, offset, &val, 1) ? 0 : val;
    607        1.1    itojun }
    608        1.1    itojun 
    609        1.1    itojun /* write a register */
    610        1.1    itojun Static int
    611        1.1    itojun udav_csr_write1(struct udav_softc *sc, int offset, unsigned char ch)
    612        1.1    itojun {
    613        1.1    itojun 	usb_device_request_t req;
    614        1.1    itojun 	usbd_status err;
    615        1.1    itojun 
    616        1.1    itojun 	if (sc == NULL)
    617   1.43.4.3     skrll 		return 0;
    618        1.1    itojun 
    619        1.1    itojun 	DPRINTFN(0x200,
    620       1.31    dyoung 		("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    621        1.1    itojun 
    622        1.1    itojun 	if (sc->sc_dying)
    623   1.43.4.3     skrll 		return 0;
    624        1.1    itojun 
    625        1.1    itojun 	offset &= 0xff;
    626        1.1    itojun 
    627        1.1    itojun 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    628        1.1    itojun 	req.bRequest = UDAV_REQ_REG_WRITE1;
    629        1.1    itojun 	USETW(req.wValue, ch);
    630        1.1    itojun 	USETW(req.wIndex, offset);
    631        1.1    itojun 	USETW(req.wLength, 0x0000);
    632        1.1    itojun 
    633        1.1    itojun 	sc->sc_refcnt++;
    634        1.1    itojun 	err = usbd_do_request(sc->sc_udev, &req, NULL);
    635        1.1    itojun 	if (--sc->sc_refcnt < 0)
    636       1.36       mrg 		usb_detach_wakeupold(sc->sc_dev);
    637        1.1    itojun 	if (err) {
    638        1.1    itojun 		DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
    639       1.31    dyoung 			 device_xname(sc->sc_dev), __func__, offset, err));
    640        1.1    itojun 	}
    641        1.1    itojun 
    642   1.43.4.3     skrll 	return err;
    643        1.1    itojun }
    644        1.1    itojun 
    645        1.1    itojun Static int
    646        1.1    itojun udav_init(struct ifnet *ifp)
    647        1.1    itojun {
    648        1.1    itojun 	struct udav_softc *sc = ifp->if_softc;
    649  1.43.4.12     skrll 
    650  1.43.4.12     skrll 	mutex_enter(&sc->sc_lock);
    651  1.43.4.12     skrll 	int ret = udav_init_locked(ifp);
    652  1.43.4.12     skrll 	mutex_exit(&sc->sc_lock);
    653  1.43.4.12     skrll 
    654  1.43.4.12     skrll 	return ret;
    655  1.43.4.12     skrll }
    656  1.43.4.12     skrll 
    657  1.43.4.12     skrll Static int
    658  1.43.4.12     skrll udav_init_locked(struct ifnet *ifp)
    659  1.43.4.12     skrll {
    660  1.43.4.12     skrll 	struct udav_softc *sc = ifp->if_softc;
    661        1.1    itojun 	struct mii_data *mii = GET_MII(sc);
    662       1.20    dyoung 	uint8_t eaddr[ETHER_ADDR_LEN];
    663  1.43.4.12     skrll 	int rc;
    664        1.1    itojun 
    665       1.31    dyoung 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    666        1.1    itojun 
    667        1.1    itojun 	if (sc->sc_dying)
    668   1.43.4.3     skrll 		return EIO;
    669        1.1    itojun 
    670        1.1    itojun 	/* Cancel pending I/O and free all TX/RX buffers */
    671  1.43.4.12     skrll 	udav_stop_locked(ifp, 1);
    672        1.1    itojun 
    673       1.20    dyoung 	memcpy(eaddr, CLLADDR(ifp->if_sadl), sizeof(eaddr));
    674        1.1    itojun 	udav_csr_write(sc, UDAV_PAR, eaddr, ETHER_ADDR_LEN);
    675        1.1    itojun 
    676        1.1    itojun 	/* Initialize network control register */
    677        1.1    itojun 	/*  Disable loopback  */
    678        1.1    itojun 	UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_LBK0 | UDAV_NCR_LBK1);
    679        1.1    itojun 
    680        1.1    itojun 	/* Initialize RX control register */
    681        1.1    itojun 	UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_DIS_LONG | UDAV_RCR_DIS_CRC);
    682        1.1    itojun 
    683        1.1    itojun 	/* If we want promiscuous mode, accept all physical frames. */
    684        1.1    itojun 	if (ifp->if_flags & IFF_PROMISC)
    685        1.1    itojun 		UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
    686        1.1    itojun 	else
    687        1.1    itojun 		UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
    688        1.1    itojun 
    689        1.1    itojun 	/* Load the multicast filter */
    690        1.1    itojun 	udav_setmulti(sc);
    691        1.1    itojun 
    692        1.1    itojun 	/* Enable RX */
    693        1.1    itojun 	UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_RXEN);
    694        1.1    itojun 
    695        1.1    itojun 	/* clear POWER_DOWN state of internal PHY */
    696        1.1    itojun 	UDAV_SETBIT(sc, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0);
    697        1.1    itojun 	UDAV_CLRBIT(sc, UDAV_GPR, UDAV_GPR_GEPIO0);
    698        1.1    itojun 
    699       1.21    dyoung 	if ((rc = mii_mediachg(mii)) == ENXIO)
    700       1.21    dyoung 		rc = 0;
    701       1.21    dyoung 	else if (rc != 0)
    702  1.43.4.12     skrll 		return rc;
    703        1.1    itojun 
    704        1.1    itojun 	if (sc->sc_pipe_tx == NULL || sc->sc_pipe_rx == NULL) {
    705        1.1    itojun 		if (udav_openpipes(sc)) {
    706   1.43.4.3     skrll 			return EIO;
    707        1.1    itojun 		}
    708        1.1    itojun 	}
    709        1.1    itojun 
    710   1.43.4.7     skrll 	/* Initialize transmit ring */
    711   1.43.4.7     skrll 	if (udav_tx_list_init(sc)) {
    712   1.43.4.7     skrll 		printf("%s: tx list init failed\n", device_xname(sc->sc_dev));
    713  1.43.4.12     skrll 		goto fail;
    714   1.43.4.7     skrll 	}
    715   1.43.4.7     skrll 
    716   1.43.4.7     skrll 	/* Initialize receive ring */
    717   1.43.4.7     skrll 	if (udav_rx_list_init(sc)) {
    718   1.43.4.7     skrll 		printf("%s: rx list init failed\n", device_xname(sc->sc_dev));
    719  1.43.4.12     skrll 		goto fail1;
    720   1.43.4.7     skrll 	}
    721   1.43.4.7     skrll 
    722   1.43.4.7     skrll 	/* Start up the receive pipe. */
    723   1.43.4.7     skrll 	for (size_t i = 0; i < UDAV_RX_LIST_CNT; i++) {
    724   1.43.4.7     skrll 		struct udav_chain *c = &sc->sc_cdata.udav_rx_chain[i];
    725   1.43.4.7     skrll 		usbd_setup_xfer(c->udav_xfer, c, c->udav_buf, UDAV_BUFSZ,
    726   1.43.4.7     skrll 		    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, udav_rxeof);
    727   1.43.4.7     skrll 		(void)usbd_transfer(c->udav_xfer);
    728   1.43.4.7     skrll 		DPRINTF(("%s: %s: start read\n", device_xname(sc->sc_dev),
    729   1.43.4.7     skrll 			 __func__));
    730   1.43.4.7     skrll 	}
    731   1.43.4.7     skrll 
    732        1.1    itojun 	ifp->if_flags |= IFF_RUNNING;
    733        1.1    itojun 	ifp->if_flags &= ~IFF_OACTIVE;
    734        1.1    itojun 
    735       1.31    dyoung 	callout_reset(&sc->sc_stat_ch, hz, udav_tick, sc);
    736        1.1    itojun 
    737       1.21    dyoung 	return rc;
    738  1.43.4.12     skrll fail1:
    739  1.43.4.12     skrll 	udav_tx_list_free(sc);
    740  1.43.4.12     skrll fail:
    741  1.43.4.12     skrll 	return EIO;
    742        1.1    itojun }
    743        1.1    itojun 
    744        1.1    itojun Static void
    745        1.1    itojun udav_reset(struct udav_softc *sc)
    746        1.1    itojun {
    747        1.1    itojun 	int i;
    748        1.1    itojun 
    749       1.31    dyoung 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    750        1.1    itojun 
    751        1.1    itojun 	if (sc->sc_dying)
    752        1.1    itojun 		return;
    753        1.1    itojun 
    754        1.1    itojun 	/* Select PHY */
    755        1.1    itojun #if 1
    756        1.1    itojun 	/*
    757        1.1    itojun 	 * XXX: force select internal phy.
    758        1.1    itojun 	 *	external phy routines are not tested.
    759        1.1    itojun 	 */
    760        1.1    itojun 	UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
    761        1.1    itojun #else
    762        1.1    itojun 	if (sc->sc_flags & UDAV_EXT_PHY) {
    763        1.1    itojun 		UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
    764        1.1    itojun 	} else {
    765        1.1    itojun 		UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
    766        1.1    itojun 	}
    767        1.1    itojun #endif
    768        1.1    itojun 
    769        1.1    itojun 	UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_RST);
    770        1.1    itojun 
    771        1.1    itojun 	for (i = 0; i < UDAV_TX_TIMEOUT; i++) {
    772        1.1    itojun 		if (!(udav_csr_read1(sc, UDAV_NCR) & UDAV_NCR_RST))
    773        1.1    itojun 			break;
    774        1.1    itojun 		delay(10);	/* XXX */
    775        1.1    itojun 	}
    776        1.1    itojun 	delay(10000);		/* XXX */
    777        1.1    itojun }
    778        1.1    itojun 
    779        1.1    itojun int
    780       1.31    dyoung udav_activate(device_t self, enum devact act)
    781        1.1    itojun {
    782       1.24      cube 	struct udav_softc *sc = device_private(self);
    783        1.1    itojun 
    784       1.31    dyoung 	DPRINTF(("%s: %s: enter, act=%d\n", device_xname(sc->sc_dev),
    785        1.1    itojun 		 __func__, act));
    786        1.1    itojun 	switch (act) {
    787        1.1    itojun 	case DVACT_DEACTIVATE:
    788        1.1    itojun 		if_deactivate(&sc->sc_ec.ec_if);
    789        1.1    itojun 		sc->sc_dying = 1;
    790       1.27    dyoung 		return 0;
    791       1.27    dyoung 	default:
    792       1.27    dyoung 		return EOPNOTSUPP;
    793        1.1    itojun 	}
    794        1.1    itojun }
    795        1.1    itojun 
    796        1.1    itojun #define UDAV_BITS	6
    797        1.1    itojun 
    798        1.1    itojun #define UDAV_CALCHASH(addr) \
    799        1.1    itojun 	(ether_crc32_le((addr), ETHER_ADDR_LEN) & ((1 << UDAV_BITS) - 1))
    800        1.1    itojun 
    801        1.1    itojun Static void
    802        1.1    itojun udav_setmulti(struct udav_softc *sc)
    803        1.1    itojun {
    804        1.1    itojun 	struct ifnet *ifp;
    805        1.1    itojun 	struct ether_multi *enm;
    806        1.1    itojun 	struct ether_multistep step;
    807   1.43.4.1     skrll 	uint8_t hashes[8];
    808        1.1    itojun 	int h = 0;
    809        1.1    itojun 
    810       1.31    dyoung 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    811        1.1    itojun 
    812        1.1    itojun 	if (sc->sc_dying)
    813        1.1    itojun 		return;
    814        1.1    itojun 
    815        1.1    itojun 	ifp = GET_IFP(sc);
    816        1.1    itojun 
    817       1.42  jakllsch 	if (ISSET(sc->sc_flags, UDAV_NO_PHY)) {
    818       1.42  jakllsch 		UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
    819       1.42  jakllsch 		UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_PRMSC);
    820       1.42  jakllsch 		return;
    821       1.42  jakllsch 	}
    822       1.42  jakllsch 
    823        1.1    itojun 	if (ifp->if_flags & IFF_PROMISC) {
    824        1.1    itojun 		UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
    825        1.1    itojun 		return;
    826        1.1    itojun 	} else if (ifp->if_flags & IFF_ALLMULTI) {
    827        1.1    itojun 	allmulti:
    828        1.1    itojun 		ifp->if_flags |= IFF_ALLMULTI;
    829        1.1    itojun 		UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
    830        1.1    itojun 		UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_PRMSC);
    831        1.1    itojun 		return;
    832        1.1    itojun 	}
    833        1.1    itojun 
    834        1.1    itojun 	/* first, zot all the existing hash bits */
    835        1.1    itojun 	memset(hashes, 0x00, sizeof(hashes));
    836        1.1    itojun 	hashes[7] |= 0x80;	/* broadcast address */
    837        1.1    itojun 	udav_csr_write(sc, UDAV_MAR, hashes, sizeof(hashes));
    838        1.1    itojun 
    839        1.1    itojun 	/* now program new ones */
    840        1.1    itojun 	ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
    841        1.1    itojun 	while (enm != NULL) {
    842        1.1    itojun 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    843        1.1    itojun 			   ETHER_ADDR_LEN) != 0)
    844        1.1    itojun 			goto allmulti;
    845        1.1    itojun 
    846        1.1    itojun 		h = UDAV_CALCHASH(enm->enm_addrlo);
    847        1.1    itojun 		hashes[h>>3] |= 1 << (h & 0x7);
    848        1.1    itojun 		ETHER_NEXT_MULTI(step, enm);
    849        1.1    itojun 	}
    850        1.1    itojun 
    851        1.1    itojun 	/* disable all multicast */
    852        1.1    itojun 	ifp->if_flags &= ~IFF_ALLMULTI;
    853        1.1    itojun 	UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
    854        1.1    itojun 
    855        1.1    itojun 	/* write hash value to the register */
    856        1.1    itojun 	udav_csr_write(sc, UDAV_MAR, hashes, sizeof(hashes));
    857        1.1    itojun }
    858        1.1    itojun 
    859        1.1    itojun Static int
    860        1.1    itojun udav_openpipes(struct udav_softc *sc)
    861        1.1    itojun {
    862        1.1    itojun 	usbd_status err;
    863        1.1    itojun 	int error = 0;
    864        1.1    itojun 
    865        1.1    itojun 	if (sc->sc_dying)
    866   1.43.4.3     skrll 		return EIO;
    867        1.1    itojun 
    868        1.1    itojun 	sc->sc_refcnt++;
    869        1.1    itojun 
    870        1.1    itojun 	/* Open RX pipe */
    871        1.1    itojun 	err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkin_no,
    872        1.1    itojun 			     USBD_EXCLUSIVE_USE, &sc->sc_pipe_rx);
    873        1.1    itojun 	if (err) {
    874        1.1    itojun 		printf("%s: open rx pipe failed: %s\n",
    875       1.31    dyoung 		       device_xname(sc->sc_dev), usbd_errstr(err));
    876        1.1    itojun 		error = EIO;
    877        1.1    itojun 		goto done;
    878        1.1    itojun 	}
    879        1.1    itojun 
    880        1.1    itojun 	/* Open TX pipe */
    881        1.1    itojun 	err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkout_no,
    882        1.1    itojun 			     USBD_EXCLUSIVE_USE, &sc->sc_pipe_tx);
    883        1.1    itojun 	if (err) {
    884  1.43.4.12     skrll 		usbd_close_pipe(sc->sc_pipe_rx);
    885        1.1    itojun 		printf("%s: open tx pipe failed: %s\n",
    886       1.31    dyoung 		       device_xname(sc->sc_dev), usbd_errstr(err));
    887        1.1    itojun 		error = EIO;
    888        1.1    itojun 		goto done;
    889        1.1    itojun 	}
    890        1.1    itojun 
    891        1.1    itojun #if 0
    892        1.1    itojun 	/* XXX: interrupt endpoint is not yet supported */
    893        1.1    itojun 	/* Open Interrupt pipe */
    894        1.1    itojun 	err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_intrin_no,
    895        1.1    itojun 				  USBD_EXCLUSIVE_USE, &sc->sc_pipe_intr, sc,
    896        1.1    itojun 				  &sc->sc_cdata.udav_ibuf, UDAV_INTR_PKGLEN,
    897       1.15  drochner 				  udav_intr, USBD_DEFAULT_INTERVAL);
    898        1.1    itojun 	if (err) {
    899        1.1    itojun 		printf("%s: open intr pipe failed: %s\n",
    900       1.31    dyoung 		       device_xname(sc->sc_dev), usbd_errstr(err));
    901        1.1    itojun 		error = EIO;
    902        1.1    itojun 		goto done;
    903        1.1    itojun 	}
    904        1.1    itojun #endif
    905        1.1    itojun  done:
    906        1.1    itojun 	if (--sc->sc_refcnt < 0)
    907       1.36       mrg 		usb_detach_wakeupold(sc->sc_dev);
    908        1.1    itojun 
    909   1.43.4.3     skrll 	return error;
    910        1.1    itojun }
    911        1.1    itojun 
    912        1.1    itojun Static int
    913        1.1    itojun udav_newbuf(struct udav_softc *sc, struct udav_chain *c, struct mbuf *m)
    914        1.1    itojun {
    915        1.1    itojun 	struct mbuf *m_new = NULL;
    916        1.1    itojun 
    917       1.31    dyoung 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    918        1.1    itojun 
    919        1.1    itojun 	if (m == NULL) {
    920        1.1    itojun 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    921        1.1    itojun 		if (m_new == NULL) {
    922        1.1    itojun 			printf("%s: no memory for rx list "
    923       1.31    dyoung 			       "-- packet dropped!\n", device_xname(sc->sc_dev));
    924   1.43.4.3     skrll 			return ENOBUFS;
    925        1.1    itojun 		}
    926        1.1    itojun 		MCLGET(m_new, M_DONTWAIT);
    927        1.1    itojun 		if (!(m_new->m_flags & M_EXT)) {
    928        1.1    itojun 			printf("%s: no memory for rx list "
    929       1.31    dyoung 			       "-- packet dropped!\n", device_xname(sc->sc_dev));
    930        1.1    itojun 			m_freem(m_new);
    931   1.43.4.3     skrll 			return ENOBUFS;
    932        1.1    itojun 		}
    933        1.1    itojun 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    934        1.1    itojun 	} else {
    935        1.1    itojun 		m_new = m;
    936        1.1    itojun 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    937        1.1    itojun 		m_new->m_data = m_new->m_ext.ext_buf;
    938        1.1    itojun 	}
    939        1.1    itojun 
    940        1.1    itojun 	m_adj(m_new, ETHER_ALIGN);
    941        1.1    itojun 	c->udav_mbuf = m_new;
    942        1.1    itojun 
    943   1.43.4.3     skrll 	return 0;
    944        1.1    itojun }
    945        1.1    itojun 
    946        1.1    itojun 
    947        1.1    itojun Static int
    948        1.1    itojun udav_rx_list_init(struct udav_softc *sc)
    949        1.1    itojun {
    950        1.1    itojun 	struct udav_cdata *cd;
    951        1.1    itojun 	struct udav_chain *c;
    952        1.1    itojun 	int i;
    953        1.1    itojun 
    954       1.31    dyoung 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    955        1.1    itojun 
    956        1.1    itojun 	cd = &sc->sc_cdata;
    957        1.1    itojun 	for (i = 0; i < UDAV_RX_LIST_CNT; i++) {
    958        1.1    itojun 		c = &cd->udav_rx_chain[i];
    959        1.1    itojun 		c->udav_sc = sc;
    960        1.1    itojun 		c->udav_idx = i;
    961        1.1    itojun 		if (udav_newbuf(sc, c, NULL) == ENOBUFS)
    962   1.43.4.3     skrll 			return ENOBUFS;
    963        1.1    itojun 		if (c->udav_xfer == NULL) {
    964  1.43.4.10     skrll 			int error = usbd_create_xfer(sc->sc_pipe_rx,
    965  1.43.4.10     skrll 			    UDAV_BUFSZ, USBD_SHORT_XFER_OK, 0, &c->udav_xfer);
    966   1.43.4.7     skrll 			if (error)
    967   1.43.4.7     skrll 				return error;
    968   1.43.4.7     skrll 			c->udav_buf = usbd_get_buffer(c->udav_xfer);
    969        1.1    itojun 		}
    970        1.1    itojun 	}
    971        1.1    itojun 
    972   1.43.4.3     skrll 	return 0;
    973        1.1    itojun }
    974        1.1    itojun 
    975  1.43.4.12     skrll Static void
    976  1.43.4.12     skrll udav_rx_list_free(struct udav_softc *sc)
    977  1.43.4.12     skrll {
    978  1.43.4.12     skrll 	for (int i = 0; i < UDAV_RX_LIST_CNT; i++) {
    979  1.43.4.12     skrll 		if (sc->sc_cdata.udav_rx_chain[i].udav_mbuf != NULL) {
    980  1.43.4.12     skrll 			m_freem(sc->sc_cdata.udav_rx_chain[i].udav_mbuf);
    981  1.43.4.12     skrll 			sc->sc_cdata.udav_rx_chain[i].udav_mbuf = NULL;
    982  1.43.4.12     skrll 		}
    983  1.43.4.12     skrll 		if (sc->sc_cdata.udav_rx_chain[i].udav_xfer != NULL) {
    984  1.43.4.12     skrll 			usbd_destroy_xfer(sc->sc_cdata.udav_rx_chain[i].udav_xfer);
    985  1.43.4.12     skrll 			sc->sc_cdata.udav_rx_chain[i].udav_xfer = NULL;
    986  1.43.4.12     skrll 		}
    987  1.43.4.12     skrll 	}
    988  1.43.4.12     skrll }
    989  1.43.4.12     skrll 
    990        1.1    itojun Static int
    991        1.1    itojun udav_tx_list_init(struct udav_softc *sc)
    992        1.1    itojun {
    993        1.1    itojun 	struct udav_cdata *cd;
    994        1.1    itojun 	struct udav_chain *c;
    995        1.1    itojun 	int i;
    996        1.1    itojun 
    997       1.31    dyoung 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    998        1.1    itojun 
    999        1.1    itojun 	cd = &sc->sc_cdata;
   1000        1.1    itojun 	for (i = 0; i < UDAV_TX_LIST_CNT; i++) {
   1001        1.1    itojun 		c = &cd->udav_tx_chain[i];
   1002        1.1    itojun 		c->udav_sc = sc;
   1003        1.1    itojun 		c->udav_idx = i;
   1004        1.1    itojun 		c->udav_mbuf = NULL;
   1005        1.1    itojun 		if (c->udav_xfer == NULL) {
   1006   1.43.4.7     skrll 			int error = usbd_create_xfer(sc->sc_pipe_tx, UDAV_BUFSZ,
   1007   1.43.4.7     skrll 			    USBD_FORCE_SHORT_XFER, 0, &c->udav_xfer);
   1008   1.43.4.7     skrll 			if (error)
   1009   1.43.4.7     skrll 				return error;
   1010   1.43.4.7     skrll 			c->udav_buf = usbd_get_buffer(c->udav_xfer);
   1011        1.1    itojun 		}
   1012        1.1    itojun 	}
   1013        1.1    itojun 
   1014   1.43.4.3     skrll 	return 0;
   1015        1.1    itojun }
   1016        1.1    itojun 
   1017        1.1    itojun Static void
   1018  1.43.4.12     skrll udav_tx_list_free(struct udav_softc *sc)
   1019  1.43.4.12     skrll {
   1020  1.43.4.12     skrll 	for (int i = 0; i < UDAV_TX_LIST_CNT; i++) {
   1021  1.43.4.12     skrll 		if (sc->sc_cdata.udav_tx_chain[i].udav_mbuf != NULL) {
   1022  1.43.4.12     skrll 			m_freem(sc->sc_cdata.udav_tx_chain[i].udav_mbuf);
   1023  1.43.4.12     skrll 			sc->sc_cdata.udav_tx_chain[i].udav_mbuf = NULL;
   1024  1.43.4.12     skrll 		}
   1025  1.43.4.12     skrll 		if (sc->sc_cdata.udav_tx_chain[i].udav_xfer != NULL) {
   1026  1.43.4.12     skrll 			usbd_destroy_xfer(sc->sc_cdata.udav_tx_chain[i].udav_xfer);
   1027  1.43.4.12     skrll 			sc->sc_cdata.udav_tx_chain[i].udav_xfer = NULL;
   1028  1.43.4.12     skrll 		}
   1029  1.43.4.12     skrll 	}
   1030  1.43.4.12     skrll }
   1031  1.43.4.12     skrll 
   1032  1.43.4.12     skrll Static void
   1033        1.1    itojun udav_start(struct ifnet *ifp)
   1034        1.1    itojun {
   1035  1.43.4.12     skrll 	struct udav_softc * const sc = ifp->if_softc;
   1036  1.43.4.12     skrll 	KASSERT(ifp->if_extflags & IFEF_START_MPSAFE);
   1037  1.43.4.12     skrll 
   1038  1.43.4.12     skrll 	mutex_enter(&sc->sc_txlock);
   1039  1.43.4.12     skrll 	udav_start_locked(ifp);
   1040  1.43.4.12     skrll 	mutex_exit(&sc->sc_txlock);
   1041  1.43.4.12     skrll }
   1042  1.43.4.12     skrll 
   1043  1.43.4.12     skrll Static void
   1044  1.43.4.12     skrll udav_start_locked(struct ifnet *ifp)
   1045  1.43.4.12     skrll {
   1046        1.1    itojun 	struct udav_softc *sc = ifp->if_softc;
   1047        1.1    itojun 	struct mbuf *m_head = NULL;
   1048        1.1    itojun 
   1049       1.31    dyoung 	DPRINTF(("%s: %s: enter, link=%d\n", device_xname(sc->sc_dev),
   1050        1.1    itojun 		 __func__, sc->sc_link));
   1051        1.1    itojun 
   1052        1.1    itojun 	if (sc->sc_dying)
   1053        1.1    itojun 		return;
   1054        1.1    itojun 
   1055        1.1    itojun 	if (!sc->sc_link)
   1056        1.1    itojun 		return;
   1057        1.1    itojun 
   1058        1.1    itojun 	if (ifp->if_flags & IFF_OACTIVE)
   1059        1.1    itojun 		return;
   1060        1.1    itojun 
   1061        1.1    itojun 	IFQ_POLL(&ifp->if_snd, m_head);
   1062        1.1    itojun 	if (m_head == NULL)
   1063        1.1    itojun 		return;
   1064        1.1    itojun 
   1065        1.1    itojun 	if (udav_send(sc, m_head, 0)) {
   1066        1.1    itojun 		return;
   1067        1.1    itojun 	}
   1068        1.1    itojun 
   1069        1.1    itojun 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
   1070        1.1    itojun 
   1071       1.29     joerg 	bpf_mtap(ifp, m_head);
   1072        1.1    itojun 
   1073        1.1    itojun 	ifp->if_flags |= IFF_OACTIVE;
   1074        1.1    itojun 
   1075        1.1    itojun 	/* Set a timeout in case the chip goes out to lunch. */
   1076        1.1    itojun 	ifp->if_timer = 5;
   1077        1.1    itojun }
   1078        1.1    itojun 
   1079        1.1    itojun Static int
   1080        1.1    itojun udav_send(struct udav_softc *sc, struct mbuf *m, int idx)
   1081        1.1    itojun {
   1082        1.1    itojun 	int total_len;
   1083        1.1    itojun 	struct udav_chain *c;
   1084        1.1    itojun 	usbd_status err;
   1085        1.1    itojun 
   1086       1.31    dyoung 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
   1087        1.1    itojun 
   1088        1.1    itojun 	c = &sc->sc_cdata.udav_tx_chain[idx];
   1089        1.1    itojun 
   1090        1.1    itojun 	/* Copy the mbuf data into a contiguous buffer */
   1091        1.1    itojun 	/*  first 2 bytes are packet length */
   1092        1.1    itojun 	m_copydata(m, 0, m->m_pkthdr.len, c->udav_buf + 2);
   1093        1.1    itojun 	c->udav_mbuf = m;
   1094        1.1    itojun 	total_len = m->m_pkthdr.len;
   1095        1.1    itojun 	if (total_len < UDAV_MIN_FRAME_LEN) {
   1096        1.1    itojun 		memset(c->udav_buf + 2 + total_len, 0,
   1097        1.1    itojun 		    UDAV_MIN_FRAME_LEN - total_len);
   1098        1.1    itojun 		total_len = UDAV_MIN_FRAME_LEN;
   1099        1.1    itojun 	}
   1100        1.1    itojun 
   1101        1.1    itojun 	/* Frame length is specified in the first 2bytes of the buffer */
   1102   1.43.4.1     skrll 	c->udav_buf[0] = (uint8_t)total_len;
   1103   1.43.4.1     skrll 	c->udav_buf[1] = (uint8_t)(total_len >> 8);
   1104        1.1    itojun 	total_len += 2;
   1105        1.1    itojun 
   1106   1.43.4.7     skrll 	usbd_setup_xfer(c->udav_xfer, c, c->udav_buf, total_len,
   1107   1.43.4.7     skrll 	    USBD_FORCE_SHORT_XFER, UDAV_TX_TIMEOUT, udav_txeof);
   1108        1.1    itojun 
   1109        1.1    itojun 	/* Transmit */
   1110        1.1    itojun 	sc->sc_refcnt++;
   1111        1.1    itojun 	err = usbd_transfer(c->udav_xfer);
   1112        1.1    itojun 	if (--sc->sc_refcnt < 0)
   1113       1.36       mrg 		usb_detach_wakeupold(sc->sc_dev);
   1114        1.1    itojun 	if (err != USBD_IN_PROGRESS) {
   1115       1.31    dyoung 		printf("%s: udav_send error=%s\n", device_xname(sc->sc_dev),
   1116        1.1    itojun 		       usbd_errstr(err));
   1117        1.1    itojun 		/* Stop the interface */
   1118       1.13     joerg 		usb_add_task(sc->sc_udev, &sc->sc_stop_task,
   1119       1.13     joerg 		    USB_TASKQ_DRIVER);
   1120   1.43.4.3     skrll 		return EIO;
   1121        1.1    itojun 	}
   1122        1.1    itojun 
   1123       1.31    dyoung 	DPRINTF(("%s: %s: send %d bytes\n", device_xname(sc->sc_dev),
   1124        1.1    itojun 		 __func__, total_len));
   1125        1.1    itojun 
   1126        1.1    itojun 	sc->sc_cdata.udav_tx_cnt++;
   1127        1.1    itojun 
   1128   1.43.4.3     skrll 	return 0;
   1129        1.1    itojun }
   1130        1.1    itojun 
   1131        1.1    itojun Static void
   1132   1.43.4.4     skrll udav_txeof(struct usbd_xfer *xfer, void *priv,
   1133       1.12  christos     usbd_status status)
   1134        1.1    itojun {
   1135        1.1    itojun 	struct udav_chain *c = priv;
   1136        1.1    itojun 	struct udav_softc *sc = c->udav_sc;
   1137        1.1    itojun 	struct ifnet *ifp = GET_IFP(sc);
   1138        1.1    itojun 	int s;
   1139        1.1    itojun 
   1140        1.1    itojun 	if (sc->sc_dying)
   1141        1.1    itojun 		return;
   1142        1.1    itojun 
   1143        1.1    itojun 	s = splnet();
   1144        1.1    itojun 
   1145       1.31    dyoung 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
   1146        1.1    itojun 
   1147        1.1    itojun 	ifp->if_timer = 0;
   1148        1.1    itojun 	ifp->if_flags &= ~IFF_OACTIVE;
   1149        1.1    itojun 
   1150        1.1    itojun 	if (status != USBD_NORMAL_COMPLETION) {
   1151        1.1    itojun 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1152        1.1    itojun 			splx(s);
   1153        1.1    itojun 			return;
   1154        1.1    itojun 		}
   1155        1.1    itojun 		ifp->if_oerrors++;
   1156       1.31    dyoung 		printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev),
   1157        1.1    itojun 		       usbd_errstr(status));
   1158        1.1    itojun 		if (status == USBD_STALLED) {
   1159        1.1    itojun 			sc->sc_refcnt++;
   1160        1.8  augustss 			usbd_clear_endpoint_stall_async(sc->sc_pipe_tx);
   1161        1.1    itojun 			if (--sc->sc_refcnt < 0)
   1162       1.36       mrg 				usb_detach_wakeupold(sc->sc_dev);
   1163        1.1    itojun 		}
   1164        1.1    itojun 		splx(s);
   1165        1.1    itojun 		return;
   1166        1.1    itojun 	}
   1167        1.1    itojun 
   1168        1.1    itojun 	ifp->if_opackets++;
   1169        1.1    itojun 
   1170        1.1    itojun 	m_freem(c->udav_mbuf);
   1171        1.1    itojun 	c->udav_mbuf = NULL;
   1172        1.1    itojun 
   1173        1.1    itojun 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1174        1.1    itojun 		udav_start(ifp);
   1175        1.1    itojun 
   1176        1.1    itojun 	splx(s);
   1177        1.1    itojun }
   1178        1.1    itojun 
   1179        1.1    itojun Static void
   1180   1.43.4.4     skrll udav_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
   1181        1.1    itojun {
   1182        1.1    itojun 	struct udav_chain *c = priv;
   1183        1.1    itojun 	struct udav_softc *sc = c->udav_sc;
   1184        1.1    itojun 	struct ifnet *ifp = GET_IFP(sc);
   1185        1.1    itojun 	struct mbuf *m;
   1186   1.43.4.1     skrll 	uint32_t total_len;
   1187   1.43.4.1     skrll 	uint8_t *pktstat;
   1188        1.1    itojun 
   1189       1.31    dyoung 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
   1190        1.1    itojun 
   1191        1.1    itojun 	if (sc->sc_dying)
   1192        1.1    itojun 		return;
   1193        1.1    itojun 
   1194        1.1    itojun 	if (status != USBD_NORMAL_COMPLETION) {
   1195        1.1    itojun 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   1196        1.1    itojun 			return;
   1197        1.1    itojun 		sc->sc_rx_errs++;
   1198        1.1    itojun 		if (usbd_ratecheck(&sc->sc_rx_notice)) {
   1199        1.1    itojun 			printf("%s: %u usb errors on rx: %s\n",
   1200       1.31    dyoung 			       device_xname(sc->sc_dev), sc->sc_rx_errs,
   1201        1.1    itojun 			       usbd_errstr(status));
   1202        1.1    itojun 			sc->sc_rx_errs = 0;
   1203        1.1    itojun 		}
   1204        1.1    itojun 		if (status == USBD_STALLED) {
   1205        1.1    itojun 			sc->sc_refcnt++;
   1206        1.8  augustss 			usbd_clear_endpoint_stall_async(sc->sc_pipe_rx);
   1207        1.1    itojun 			if (--sc->sc_refcnt < 0)
   1208       1.36       mrg 				usb_detach_wakeupold(sc->sc_dev);
   1209        1.1    itojun 		}
   1210        1.1    itojun 		goto done;
   1211        1.1    itojun 	}
   1212        1.1    itojun 
   1213        1.1    itojun 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
   1214        1.1    itojun 
   1215        1.1    itojun 	/* copy data to mbuf */
   1216        1.1    itojun 	m = c->udav_mbuf;
   1217        1.1    itojun 	memcpy(mtod(m, char *), c->udav_buf, total_len);
   1218        1.1    itojun 
   1219        1.1    itojun 	/* first byte in received data */
   1220   1.43.4.1     skrll 	pktstat = mtod(m, uint8_t *);
   1221   1.43.4.1     skrll 	m_adj(m, sizeof(uint8_t));
   1222       1.31    dyoung 	DPRINTF(("%s: RX Status: 0x%02x\n", device_xname(sc->sc_dev),
   1223       1.10  christos 				*pktstat));
   1224        1.1    itojun 
   1225   1.43.4.1     skrll 	total_len = UGETW(mtod(m, uint8_t *));
   1226   1.43.4.1     skrll 	m_adj(m, sizeof(uint16_t));
   1227        1.1    itojun 
   1228        1.1    itojun 	if (*pktstat & UDAV_RSR_LCS) {
   1229        1.1    itojun 		ifp->if_collisions++;
   1230        1.1    itojun 		goto done;
   1231        1.1    itojun 	}
   1232        1.1    itojun 
   1233        1.1    itojun 	if (total_len < sizeof(struct ether_header) ||
   1234        1.1    itojun 	    *pktstat & UDAV_RSR_ERR) {
   1235        1.1    itojun 		ifp->if_ierrors++;
   1236        1.1    itojun 		goto done;
   1237        1.1    itojun 	}
   1238        1.1    itojun 
   1239        1.1    itojun 	total_len -= ETHER_CRC_LEN;
   1240        1.1    itojun 
   1241        1.1    itojun 	m->m_pkthdr.len = m->m_len = total_len;
   1242  1.43.4.10     skrll 	m_set_rcvif(m, ifp);
   1243        1.1    itojun 
   1244        1.1    itojun 	if (udav_newbuf(sc, c, NULL) == ENOBUFS) {
   1245        1.1    itojun 		ifp->if_ierrors++;
   1246  1.43.4.12     skrll 		goto done;
   1247        1.1    itojun 	}
   1248        1.1    itojun 
   1249       1.31    dyoung 	DPRINTF(("%s: %s: deliver %d\n", device_xname(sc->sc_dev),
   1250        1.1    itojun 		 __func__, m->m_len));
   1251  1.43.4.12     skrll 	if_percpuq_enqueue(sc->sc_ipq, (m));
   1252        1.1    itojun 
   1253        1.1    itojun  done:
   1254        1.1    itojun 	/* Setup new transfer */
   1255   1.43.4.7     skrll 	usbd_setup_xfer(xfer, c, c->udav_buf, UDAV_BUFSZ, USBD_SHORT_XFER_OK,
   1256   1.43.4.7     skrll 	    USBD_NO_TIMEOUT, udav_rxeof);
   1257        1.1    itojun 	sc->sc_refcnt++;
   1258        1.1    itojun 	usbd_transfer(xfer);
   1259        1.1    itojun 	if (--sc->sc_refcnt < 0)
   1260       1.36       mrg 		usb_detach_wakeupold(sc->sc_dev);
   1261        1.1    itojun 
   1262       1.31    dyoung 	DPRINTF(("%s: %s: start rx\n", device_xname(sc->sc_dev), __func__));
   1263        1.1    itojun }
   1264        1.1    itojun 
   1265        1.1    itojun #if 0
   1266       1.25    cegger Static void udav_intr(void)
   1267        1.1    itojun {
   1268        1.1    itojun }
   1269        1.1    itojun #endif
   1270        1.1    itojun 
   1271        1.1    itojun Static int
   1272       1.16  christos udav_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1273        1.1    itojun {
   1274        1.1    itojun 	struct udav_softc *sc = ifp->if_softc;
   1275        1.1    itojun 	int s, error = 0;
   1276        1.1    itojun 
   1277       1.31    dyoung 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
   1278        1.1    itojun 
   1279        1.1    itojun 	if (sc->sc_dying)
   1280   1.43.4.3     skrll 		return EIO;
   1281        1.1    itojun 
   1282        1.1    itojun 	s = splnet();
   1283        1.1    itojun 
   1284       1.21    dyoung 	error = ether_ioctl(ifp, cmd, data);
   1285       1.21    dyoung 	if (error == ENETRESET) {
   1286       1.21    dyoung 		if (ifp->if_flags & IFF_RUNNING)
   1287       1.21    dyoung 			udav_setmulti(sc);
   1288       1.21    dyoung 		error = 0;
   1289        1.1    itojun 	}
   1290        1.1    itojun 
   1291        1.1    itojun 	splx(s);
   1292        1.1    itojun 
   1293   1.43.4.3     skrll 	return error;
   1294        1.1    itojun }
   1295        1.1    itojun 
   1296        1.1    itojun Static void
   1297        1.1    itojun udav_watchdog(struct ifnet *ifp)
   1298        1.1    itojun {
   1299        1.1    itojun 	struct udav_softc *sc = ifp->if_softc;
   1300        1.1    itojun 	struct udav_chain *c;
   1301        1.1    itojun 	usbd_status stat;
   1302        1.1    itojun 	int s;
   1303        1.1    itojun 
   1304       1.31    dyoung 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
   1305        1.1    itojun 
   1306        1.1    itojun 	ifp->if_oerrors++;
   1307       1.31    dyoung 	printf("%s: watchdog timeout\n", device_xname(sc->sc_dev));
   1308        1.1    itojun 
   1309        1.1    itojun 	s = splusb();
   1310        1.1    itojun 	c = &sc->sc_cdata.udav_tx_chain[0];
   1311        1.1    itojun 	usbd_get_xfer_status(c->udav_xfer, NULL, NULL, NULL, &stat);
   1312        1.1    itojun 	udav_txeof(c->udav_xfer, c, stat);
   1313        1.1    itojun 
   1314        1.1    itojun 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1315        1.1    itojun 		udav_start(ifp);
   1316        1.1    itojun 	splx(s);
   1317        1.1    itojun }
   1318        1.1    itojun 
   1319        1.1    itojun Static void
   1320        1.1    itojun udav_stop_task(struct udav_softc *sc)
   1321        1.1    itojun {
   1322        1.1    itojun 	udav_stop(GET_IFP(sc), 1);
   1323        1.1    itojun }
   1324        1.1    itojun 
   1325        1.1    itojun Static void
   1326       1.14  christos udav_stop(struct ifnet *ifp, int disable)
   1327        1.1    itojun {
   1328  1.43.4.12     skrll 	struct udav_softc * const sc = ifp->if_softc;
   1329  1.43.4.12     skrll 
   1330  1.43.4.12     skrll 	mutex_enter(&sc->sc_lock);
   1331  1.43.4.12     skrll 	udav_stop_locked(ifp, disable);
   1332  1.43.4.12     skrll 	mutex_exit(&sc->sc_lock);
   1333  1.43.4.12     skrll }
   1334  1.43.4.12     skrll 
   1335  1.43.4.12     skrll /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */
   1336  1.43.4.12     skrll Static void
   1337  1.43.4.12     skrll udav_stop_locked(struct ifnet *ifp, int disable)
   1338  1.43.4.12     skrll {
   1339        1.1    itojun 	struct udav_softc *sc = ifp->if_softc;
   1340        1.1    itojun 	usbd_status err;
   1341        1.1    itojun 
   1342       1.31    dyoung 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
   1343        1.1    itojun 
   1344        1.1    itojun 	ifp->if_timer = 0;
   1345        1.1    itojun 
   1346  1.43.4.12     skrll //	udav_reset(sc);
   1347        1.1    itojun 
   1348       1.31    dyoung 	callout_stop(&sc->sc_stat_ch);
   1349        1.1    itojun 
   1350        1.1    itojun 	/* Stop transfers */
   1351        1.1    itojun 	/* RX endpoint */
   1352        1.1    itojun 	if (sc->sc_pipe_rx != NULL) {
   1353        1.1    itojun 		err = usbd_abort_pipe(sc->sc_pipe_rx);
   1354        1.1    itojun 		if (err)
   1355        1.1    itojun 			printf("%s: abort rx pipe failed: %s\n",
   1356       1.31    dyoung 			       device_xname(sc->sc_dev), usbd_errstr(err));
   1357        1.1    itojun 	}
   1358        1.1    itojun 
   1359        1.1    itojun 	/* TX endpoint */
   1360        1.1    itojun 	if (sc->sc_pipe_tx != NULL) {
   1361        1.1    itojun 		err = usbd_abort_pipe(sc->sc_pipe_tx);
   1362        1.1    itojun 		if (err)
   1363        1.1    itojun 			printf("%s: abort tx pipe failed: %s\n",
   1364       1.31    dyoung 			       device_xname(sc->sc_dev), usbd_errstr(err));
   1365        1.1    itojun 	}
   1366        1.1    itojun 
   1367        1.1    itojun #if 0
   1368        1.1    itojun 	/* XXX: Interrupt endpoint is not yet supported!! */
   1369        1.1    itojun 	/* Interrupt endpoint */
   1370        1.1    itojun 	if (sc->sc_pipe_intr != NULL) {
   1371        1.1    itojun 		err = usbd_abort_pipe(sc->sc_pipe_intr);
   1372        1.1    itojun 		if (err)
   1373        1.1    itojun 			printf("%s: abort intr pipe failed: %s\n",
   1374       1.31    dyoung 			       device_xname(sc->sc_dev), usbd_errstr(err));
   1375        1.1    itojun 		err = usbd_close_pipe(sc->sc_pipe_intr);
   1376        1.1    itojun 		if (err)
   1377        1.1    itojun 			printf("%s: close intr pipe failed: %s\n",
   1378       1.31    dyoung 			       device_xname(sc->sc_dev), usbd_errstr(err));
   1379        1.1    itojun 		sc->sc_pipe_intr = NULL;
   1380        1.1    itojun 	}
   1381        1.1    itojun #endif
   1382        1.1    itojun 
   1383  1.43.4.12     skrll 	udav_rx_list_free(sc);
   1384        1.1    itojun 
   1385  1.43.4.12     skrll 	udav_tx_list_free(sc);
   1386        1.1    itojun 
   1387   1.43.4.8     skrll 	/* Close pipes */
   1388   1.43.4.8     skrll 	/* RX endpoint */
   1389   1.43.4.8     skrll 	if (sc->sc_pipe_rx != NULL) {
   1390   1.43.4.8     skrll 		err = usbd_close_pipe(sc->sc_pipe_rx);
   1391   1.43.4.8     skrll 		if (err)
   1392   1.43.4.8     skrll 			printf("%s: close rx pipe failed: %s\n",
   1393   1.43.4.8     skrll 			       device_xname(sc->sc_dev), usbd_errstr(err));
   1394   1.43.4.8     skrll 		sc->sc_pipe_rx = NULL;
   1395   1.43.4.8     skrll 	}
   1396   1.43.4.8     skrll 
   1397   1.43.4.8     skrll 	/* TX endpoint */
   1398   1.43.4.8     skrll 	if (sc->sc_pipe_tx != NULL) {
   1399   1.43.4.8     skrll 		err = usbd_close_pipe(sc->sc_pipe_tx);
   1400   1.43.4.8     skrll 		if (err)
   1401   1.43.4.8     skrll 			printf("%s: close tx pipe failed: %s\n",
   1402   1.43.4.8     skrll 			       device_xname(sc->sc_dev), usbd_errstr(err));
   1403   1.43.4.8     skrll 		sc->sc_pipe_tx = NULL;
   1404   1.43.4.8     skrll 	}
   1405   1.43.4.8     skrll 
   1406       1.42  jakllsch 	if (!ISSET(sc->sc_flags, UDAV_NO_PHY))
   1407       1.42  jakllsch 		sc->sc_link = 0;
   1408        1.1    itojun 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1409        1.1    itojun }
   1410        1.1    itojun 
   1411        1.1    itojun /* Set media options */
   1412        1.1    itojun Static int
   1413        1.1    itojun udav_ifmedia_change(struct ifnet *ifp)
   1414        1.1    itojun {
   1415        1.1    itojun 	struct udav_softc *sc = ifp->if_softc;
   1416        1.1    itojun 	struct mii_data *mii = GET_MII(sc);
   1417       1.21    dyoung 	int rc;
   1418        1.1    itojun 
   1419       1.31    dyoung 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
   1420        1.1    itojun 
   1421        1.1    itojun 	if (sc->sc_dying)
   1422   1.43.4.3     skrll 		return 0;
   1423        1.1    itojun 
   1424        1.1    itojun 	sc->sc_link = 0;
   1425       1.21    dyoung 	if ((rc = mii_mediachg(mii)) == ENXIO)
   1426       1.21    dyoung 		return 0;
   1427       1.21    dyoung 	return rc;
   1428        1.1    itojun }
   1429        1.1    itojun 
   1430        1.1    itojun /* Report current media status. */
   1431        1.1    itojun Static void
   1432        1.1    itojun udav_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
   1433        1.1    itojun {
   1434        1.1    itojun 	struct udav_softc *sc = ifp->if_softc;
   1435        1.1    itojun 
   1436       1.31    dyoung 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
   1437        1.1    itojun 
   1438        1.1    itojun 	if (sc->sc_dying)
   1439        1.1    itojun 		return;
   1440        1.1    itojun 
   1441       1.21    dyoung 	ether_mediastatus(ifp, ifmr);
   1442        1.1    itojun }
   1443        1.1    itojun 
   1444        1.1    itojun Static void
   1445        1.1    itojun udav_tick(void *xsc)
   1446        1.1    itojun {
   1447        1.1    itojun 	struct udav_softc *sc = xsc;
   1448        1.1    itojun 
   1449        1.1    itojun 	if (sc == NULL)
   1450        1.1    itojun 		return;
   1451        1.1    itojun 
   1452       1.31    dyoung 	DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
   1453        1.1    itojun 			__func__));
   1454        1.1    itojun 
   1455        1.1    itojun 	if (sc->sc_dying)
   1456        1.1    itojun 		return;
   1457        1.1    itojun 
   1458        1.1    itojun 	/* Perform periodic stuff in process context */
   1459       1.13     joerg 	usb_add_task(sc->sc_udev, &sc->sc_tick_task,
   1460       1.13     joerg 	    USB_TASKQ_DRIVER);
   1461        1.1    itojun }
   1462        1.1    itojun 
   1463        1.1    itojun Static void
   1464        1.1    itojun udav_tick_task(void *xsc)
   1465        1.1    itojun {
   1466        1.1    itojun 	struct udav_softc *sc = xsc;
   1467        1.1    itojun 	struct ifnet *ifp;
   1468        1.1    itojun 	struct mii_data *mii;
   1469        1.1    itojun 	int s;
   1470        1.1    itojun 
   1471        1.1    itojun 	if (sc == NULL)
   1472        1.1    itojun 		return;
   1473        1.1    itojun 
   1474       1.31    dyoung 	DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
   1475        1.1    itojun 			__func__));
   1476        1.1    itojun 
   1477        1.1    itojun 	if (sc->sc_dying)
   1478        1.1    itojun 		return;
   1479        1.1    itojun 
   1480        1.1    itojun 	ifp = GET_IFP(sc);
   1481        1.1    itojun 	mii = GET_MII(sc);
   1482        1.1    itojun 
   1483        1.1    itojun 	if (mii == NULL)
   1484        1.1    itojun 		return;
   1485        1.1    itojun 
   1486        1.1    itojun 	s = splnet();
   1487        1.1    itojun 
   1488        1.1    itojun 	mii_tick(mii);
   1489        1.1    itojun 	if (!sc->sc_link) {
   1490        1.1    itojun 		mii_pollstat(mii);
   1491        1.1    itojun 		if (mii->mii_media_status & IFM_ACTIVE &&
   1492        1.1    itojun 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
   1493        1.1    itojun 			DPRINTF(("%s: %s: got link\n",
   1494       1.31    dyoung 				 device_xname(sc->sc_dev), __func__));
   1495        1.1    itojun 			sc->sc_link++;
   1496        1.1    itojun 			if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1497        1.1    itojun 				   udav_start(ifp);
   1498        1.1    itojun 		}
   1499        1.1    itojun 	}
   1500        1.1    itojun 
   1501       1.31    dyoung 	callout_reset(&sc->sc_stat_ch, hz, udav_tick, sc);
   1502        1.1    itojun 
   1503        1.1    itojun 	splx(s);
   1504        1.1    itojun }
   1505        1.1    itojun 
   1506        1.1    itojun /* Get exclusive access to the MII registers */
   1507        1.1    itojun Static void
   1508        1.1    itojun udav_lock_mii(struct udav_softc *sc)
   1509        1.1    itojun {
   1510       1.31    dyoung 	DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
   1511        1.1    itojun 			__func__));
   1512        1.1    itojun 
   1513        1.1    itojun 	sc->sc_refcnt++;
   1514       1.19        ad 	mutex_enter(&sc->sc_mii_lock);
   1515        1.1    itojun }
   1516        1.1    itojun 
   1517        1.1    itojun Static void
   1518        1.1    itojun udav_unlock_mii(struct udav_softc *sc)
   1519        1.1    itojun {
   1520       1.31    dyoung 	DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
   1521        1.1    itojun 		       __func__));
   1522        1.1    itojun 
   1523       1.19        ad 	mutex_exit(&sc->sc_mii_lock);
   1524        1.1    itojun 	if (--sc->sc_refcnt < 0)
   1525       1.36       mrg 		usb_detach_wakeupold(sc->sc_dev);
   1526        1.1    itojun }
   1527        1.1    itojun 
   1528        1.1    itojun Static int
   1529       1.31    dyoung udav_miibus_readreg(device_t dev, int phy, int reg)
   1530        1.1    itojun {
   1531        1.1    itojun 	struct udav_softc *sc;
   1532   1.43.4.1     skrll 	uint8_t val[2];
   1533   1.43.4.1     skrll 	uint16_t data16;
   1534        1.1    itojun 
   1535        1.1    itojun 	if (dev == NULL)
   1536   1.43.4.3     skrll 		return 0;
   1537        1.1    itojun 
   1538       1.31    dyoung 	sc = device_private(dev);
   1539        1.1    itojun 
   1540        1.1    itojun 	DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n",
   1541       1.31    dyoung 		 device_xname(sc->sc_dev), __func__, phy, reg));
   1542        1.1    itojun 
   1543        1.1    itojun 	if (sc->sc_dying) {
   1544        1.1    itojun #ifdef DIAGNOSTIC
   1545       1.31    dyoung 		printf("%s: %s: dying\n", device_xname(sc->sc_dev),
   1546        1.1    itojun 		       __func__);
   1547        1.1    itojun #endif
   1548   1.43.4.3     skrll 		return 0;
   1549        1.1    itojun 	}
   1550        1.1    itojun 
   1551        1.1    itojun 	/* XXX: one PHY only for the internal PHY */
   1552        1.1    itojun 	if (phy != 0) {
   1553        1.1    itojun 		DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
   1554       1.31    dyoung 			 device_xname(sc->sc_dev), __func__, phy));
   1555   1.43.4.3     skrll 		return 0;
   1556        1.1    itojun 	}
   1557        1.1    itojun 
   1558        1.1    itojun 	udav_lock_mii(sc);
   1559        1.1    itojun 
   1560        1.1    itojun 	/* select internal PHY and set PHY register address */
   1561        1.1    itojun 	udav_csr_write1(sc, UDAV_EPAR,
   1562        1.1    itojun 			UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
   1563        1.1    itojun 
   1564        1.1    itojun 	/* select PHY operation and start read command */
   1565        1.1    itojun 	udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR);
   1566        1.1    itojun 
   1567        1.1    itojun 	/* XXX: should be wait? */
   1568        1.1    itojun 
   1569        1.1    itojun 	/* end read command */
   1570        1.1    itojun 	UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRR);
   1571        1.1    itojun 
   1572        1.1    itojun 	/* retrieve the result from data registers */
   1573        1.1    itojun 	udav_csr_read(sc, UDAV_EPDRL, val, 2);
   1574        1.1    itojun 
   1575        1.1    itojun 	udav_unlock_mii(sc);
   1576        1.1    itojun 
   1577        1.1    itojun 	data16 = val[0] | (val[1] << 8);
   1578        1.1    itojun 
   1579        1.1    itojun 	DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04x\n",
   1580       1.31    dyoung 		 device_xname(sc->sc_dev), __func__, phy, reg, data16));
   1581        1.1    itojun 
   1582   1.43.4.3     skrll 	return data16;
   1583        1.1    itojun }
   1584        1.1    itojun 
   1585        1.1    itojun Static void
   1586       1.31    dyoung udav_miibus_writereg(device_t dev, int phy, int reg, int data)
   1587        1.1    itojun {
   1588        1.1    itojun 	struct udav_softc *sc;
   1589   1.43.4.1     skrll 	uint8_t val[2];
   1590        1.1    itojun 
   1591        1.1    itojun 	if (dev == NULL)
   1592        1.1    itojun 		return;
   1593        1.1    itojun 
   1594       1.31    dyoung 	sc = device_private(dev);
   1595        1.1    itojun 
   1596        1.1    itojun 	DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n",
   1597       1.31    dyoung 		 device_xname(sc->sc_dev), __func__, phy, reg, data));
   1598        1.1    itojun 
   1599        1.1    itojun 	if (sc->sc_dying) {
   1600        1.1    itojun #ifdef DIAGNOSTIC
   1601       1.31    dyoung 		printf("%s: %s: dying\n", device_xname(sc->sc_dev),
   1602        1.1    itojun 		       __func__);
   1603        1.1    itojun #endif
   1604        1.1    itojun 		return;
   1605        1.1    itojun 	}
   1606        1.1    itojun 
   1607        1.1    itojun 	/* XXX: one PHY only for the internal PHY */
   1608        1.1    itojun 	if (phy != 0) {
   1609        1.1    itojun 		DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
   1610       1.31    dyoung 			 device_xname(sc->sc_dev), __func__, phy));
   1611        1.1    itojun 		return;
   1612        1.1    itojun 	}
   1613        1.1    itojun 
   1614        1.1    itojun 	udav_lock_mii(sc);
   1615        1.1    itojun 
   1616        1.1    itojun 	/* select internal PHY and set PHY register address */
   1617        1.1    itojun 	udav_csr_write1(sc, UDAV_EPAR,
   1618        1.1    itojun 			UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
   1619        1.1    itojun 
   1620        1.1    itojun 	/* put the value to the data registers */
   1621        1.1    itojun 	val[0] = data & 0xff;
   1622        1.1    itojun 	val[1] = (data >> 8) & 0xff;
   1623        1.1    itojun 	udav_csr_write(sc, UDAV_EPDRL, val, 2);
   1624        1.1    itojun 
   1625        1.1    itojun 	/* select PHY operation and start write command */
   1626        1.1    itojun 	udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW);
   1627        1.1    itojun 
   1628        1.1    itojun 	/* XXX: should be wait? */
   1629        1.1    itojun 
   1630        1.1    itojun 	/* end write command */
   1631        1.1    itojun 	UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRW);
   1632        1.1    itojun 
   1633        1.1    itojun 	udav_unlock_mii(sc);
   1634        1.1    itojun 
   1635        1.1    itojun 	return;
   1636        1.1    itojun }
   1637        1.1    itojun 
   1638        1.1    itojun Static void
   1639       1.38      matt udav_miibus_statchg(struct ifnet *ifp)
   1640        1.1    itojun {
   1641        1.1    itojun #ifdef UDAV_DEBUG
   1642        1.1    itojun 
   1643       1.38      matt 	if (ifp == NULL)
   1644        1.1    itojun 		return;
   1645        1.1    itojun 
   1646       1.38      matt 	DPRINTF(("%s: %s: enter\n", ifp->if_xname, __func__));
   1647        1.1    itojun #endif
   1648        1.1    itojun 	/* Nothing to do */
   1649        1.1    itojun }
   1650