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