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