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