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