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