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