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