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