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