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