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