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