Home | History | Annotate | Line # | Download | only in usb
if_udav.c revision 1.21
      1 /*	$NetBSD: if_udav.c,v 1.21 2008/01/19 22:10:20 dyoung 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.21 2008/01/19 22:10:20 dyoung 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/mutex.h>
     56 #include <sys/mbuf.h>
     57 #include <sys/kernel.h>
     58 #include <sys/socket.h>
     59 #include <sys/device.h>
     60 #if NRND > 0
     61 #include <sys/rnd.h>
     62 #endif
     63 
     64 #include <net/if.h>
     65 #include <net/if_arp.h>
     66 #include <net/if_dl.h>
     67 #include <net/if_media.h>
     68 
     69 #if NBPFILTER > 0
     70 #include <net/bpf.h>
     71 #endif
     72 #define	BPF_MTAP(ifp, m)	bpf_mtap((ifp)->if_bpf, (m))
     73 
     74 #include <net/if_ether.h>
     75 #ifdef INET
     76 #include <netinet/in.h>
     77 #include <netinet/if_inarp.h>
     78 #endif
     79 
     80 #include <dev/mii/mii.h>
     81 #include <dev/mii/miivar.h>
     82 
     83 #include <dev/usb/usb.h>
     84 #include <dev/usb/usbdi.h>
     85 #include <dev/usb/usbdi_util.h>
     86 #include <dev/usb/usbdevs.h>
     87 
     88 #include <dev/usb/if_udavreg.h>
     89 
     90 
     91 /* Function declarations */
     92 USB_DECLARE_DRIVER(udav);
     93 
     94 Static int udav_openpipes(struct udav_softc *);
     95 Static int udav_rx_list_init(struct udav_softc *);
     96 Static int udav_tx_list_init(struct udav_softc *);
     97 Static int udav_newbuf(struct udav_softc *, struct udav_chain *, struct mbuf *);
     98 Static void udav_start(struct ifnet *);
     99 Static int udav_send(struct udav_softc *, struct mbuf *, int);
    100 Static void udav_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
    101 Static void udav_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
    102 Static void udav_tick(void *);
    103 Static void udav_tick_task(void *);
    104 Static int udav_ioctl(struct ifnet *, u_long, void *);
    105 Static void udav_stop_task(struct udav_softc *);
    106 Static void udav_stop(struct ifnet *, int);
    107 Static void udav_watchdog(struct ifnet *);
    108 Static int udav_ifmedia_change(struct ifnet *);
    109 Static void udav_ifmedia_status(struct ifnet *, struct ifmediareq *);
    110 Static void udav_lock_mii(struct udav_softc *);
    111 Static void udav_unlock_mii(struct udav_softc *);
    112 Static int udav_miibus_readreg(device_ptr_t, int, int);
    113 Static void udav_miibus_writereg(device_ptr_t, int, int, int);
    114 Static void udav_miibus_statchg(device_ptr_t);
    115 Static int udav_init(struct ifnet *);
    116 Static void udav_setmulti(struct udav_softc *);
    117 Static void udav_reset(struct udav_softc *);
    118 
    119 Static int udav_csr_read(struct udav_softc *, int, void *, int);
    120 Static int udav_csr_write(struct udav_softc *, int, void *, int);
    121 Static int udav_csr_read1(struct udav_softc *, int);
    122 Static int udav_csr_write1(struct udav_softc *, int, unsigned char);
    123 
    124 #if 0
    125 Static int udav_mem_read(struct udav_softc *, int, void *, int);
    126 Static int udav_mem_write(struct udav_softc *, int, void *, int);
    127 Static int udav_mem_write1(struct udav_softc *, int, unsigned char);
    128 #endif
    129 
    130 /* Macros */
    131 #ifdef UDAV_DEBUG
    132 #define DPRINTF(x)	if (udavdebug) logprintf x
    133 #define DPRINTFN(n,x)	if (udavdebug >= (n)) logprintf x
    134 int udavdebug = 0;
    135 #else
    136 #define DPRINTF(x)
    137 #define DPRINTFN(n,x)
    138 #endif
    139 
    140 #define	UDAV_SETBIT(sc, reg, x)	\
    141 	udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) | (x))
    142 
    143 #define	UDAV_CLRBIT(sc, reg, x)	\
    144 	udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) & ~(x))
    145 
    146 static const struct udav_type {
    147 	struct usb_devno udav_dev;
    148 	u_int16_t udav_flags;
    149 #define UDAV_EXT_PHY	0x0001
    150 } udav_devs [] = {
    151 	/* Corega USB-TXC */
    152 	{{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXC }, 0},
    153 	/* ShanTou ST268 USB NIC */
    154 	{{ USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ST268_USB_NIC }, 0},
    155 #if 0
    156 	/* DAVICOM DM9601 Generic? */
    157 	/*  XXX: The following ids was obtained from the data sheet. */
    158 	{{ 0x0a46, 0x9601 }, 0},
    159 #endif
    160 };
    161 #define udav_lookup(v, p) ((const struct udav_type *)usb_lookup(udav_devs, v, p))
    162 
    163 
    164 /* Probe */
    165 USB_MATCH(udav)
    166 {
    167 	USB_MATCH_START(udav, uaa);
    168 
    169 	return (udav_lookup(uaa->vendor, uaa->product) != NULL ?
    170 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    171 }
    172 
    173 /* Attach */
    174 USB_ATTACH(udav)
    175 {
    176 	USB_ATTACH_START(udav, sc, uaa);
    177 	usbd_device_handle dev = uaa->device;
    178 	usbd_interface_handle iface;
    179 	usbd_status err;
    180 	usb_interface_descriptor_t *id;
    181 	usb_endpoint_descriptor_t *ed;
    182 	char *devinfop;
    183 	char *devname = USBDEVNAME(sc->sc_dev);
    184 	struct ifnet *ifp;
    185 	struct mii_data *mii;
    186 	u_char eaddr[ETHER_ADDR_LEN];
    187 	int i, s;
    188 
    189 	devinfop = usbd_devinfo_alloc(dev, 0);
    190 	USB_ATTACH_SETUP;
    191 	printf("%s: %s\n", devname, devinfop);
    192 	usbd_devinfo_free(devinfop);
    193 
    194 	/* Move the device into the configured state. */
    195 	err = usbd_set_config_no(dev, UDAV_CONFIG_NO, 1);
    196 	if (err) {
    197 		printf("%s: setting config no failed\n", devname);
    198 		goto bad;
    199 	}
    200 
    201 	usb_init_task(&sc->sc_tick_task, udav_tick_task, sc);
    202 	mutex_init(&sc->sc_mii_lock, MUTEX_DEFAULT, IPL_NONE);
    203 	usb_init_task(&sc->sc_stop_task, (void (*)(void *)) udav_stop_task, sc);
    204 
    205 	/* get control interface */
    206 	err = usbd_device2interface_handle(dev, UDAV_IFACE_INDEX, &iface);
    207 	if (err) {
    208 		printf("%s: failed to get interface, err=%s\n", devname,
    209 		       usbd_errstr(err));
    210 		goto bad;
    211 	}
    212 
    213 	sc->sc_udev = dev;
    214 	sc->sc_ctl_iface = iface;
    215 	sc->sc_flags = udav_lookup(uaa->vendor, uaa->product)->udav_flags;
    216 
    217 	/* get interface descriptor */
    218 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
    219 
    220 	/* find endpoints */
    221 	sc->sc_bulkin_no = sc->sc_bulkout_no = sc->sc_intrin_no = -1;
    222 	for (i = 0; i < id->bNumEndpoints; i++) {
    223 		ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
    224 		if (ed == NULL) {
    225 			printf("%s: couldn't get endpoint %d\n", devname, i);
    226 			goto bad;
    227 		}
    228 		if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
    229 		    UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
    230 			sc->sc_bulkin_no = ed->bEndpointAddress; /* RX */
    231 		else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
    232 			 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT)
    233 			sc->sc_bulkout_no = ed->bEndpointAddress; /* TX */
    234 		else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT &&
    235 			 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
    236 			sc->sc_intrin_no = ed->bEndpointAddress; /* Status */
    237 	}
    238 
    239 	if (sc->sc_bulkin_no == -1 || sc->sc_bulkout_no == -1 ||
    240 	    sc->sc_intrin_no == -1) {
    241 		printf("%s: missing endpoint\n", devname);
    242 		goto bad;
    243 	}
    244 
    245 	s = splnet();
    246 
    247 	/* reset the adapter */
    248 	udav_reset(sc);
    249 
    250 	/* Get Ethernet Address */
    251 	err = udav_csr_read(sc, UDAV_PAR, (void *)eaddr, ETHER_ADDR_LEN);
    252 	if (err) {
    253 		printf("%s: read MAC address failed\n", devname);
    254 		splx(s);
    255 		goto bad;
    256 	}
    257 
    258 	/* Print Ethernet Address */
    259 	printf("%s: Ethernet address %s\n", devname, ether_sprintf(eaddr));
    260 
    261 	/* initialize interface information */
    262 	ifp = GET_IFP(sc);
    263 	ifp->if_softc = sc;
    264 	ifp->if_mtu = ETHERMTU;
    265 	strncpy(ifp->if_xname, devname, IFNAMSIZ);
    266 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    267 	ifp->if_start = udav_start;
    268 	ifp->if_ioctl = udav_ioctl;
    269 	ifp->if_watchdog = udav_watchdog;
    270 	ifp->if_init = udav_init;
    271 	ifp->if_stop = udav_stop;
    272 
    273 	IFQ_SET_READY(&ifp->if_snd);
    274 
    275 	/*
    276 	 * Do ifmedia setup.
    277 	 */
    278 	mii = &sc->sc_mii;
    279 	mii->mii_ifp = ifp;
    280 	mii->mii_readreg = udav_miibus_readreg;
    281 	mii->mii_writereg = udav_miibus_writereg;
    282 	mii->mii_statchg = udav_miibus_statchg;
    283 	mii->mii_flags = MIIF_AUTOTSLEEP;
    284 	sc->sc_ec.ec_mii = mii;
    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(GET_IFP(sc), 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 	mutex_destroy(&sc->sc_mii_lock);
    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 	uint8_t eaddr[ETHER_ADDR_LEN];
    619 	int rc, s;
    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 	memcpy(eaddr, CLLADDR(ifp->if_sadl), sizeof(eaddr));
    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 	/* Initialize transmit ring */
    648 	if (udav_tx_list_init(sc) == ENOBUFS) {
    649 		printf("%s: tx list init failed\n", USBDEVNAME(sc->sc_dev));
    650 		splx(s);
    651 		return (EIO);
    652 	}
    653 
    654 	/* Initialize receive ring */
    655 	if (udav_rx_list_init(sc) == ENOBUFS) {
    656 		printf("%s: rx list init failed\n", USBDEVNAME(sc->sc_dev));
    657 		splx(s);
    658 		return (EIO);
    659 	}
    660 
    661 	/* Load the multicast filter */
    662 	udav_setmulti(sc);
    663 
    664 	/* Enable RX */
    665 	UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_RXEN);
    666 
    667 	/* clear POWER_DOWN state of internal PHY */
    668 	UDAV_SETBIT(sc, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0);
    669 	UDAV_CLRBIT(sc, UDAV_GPR, UDAV_GPR_GEPIO0);
    670 
    671 	if ((rc = mii_mediachg(mii)) == ENXIO)
    672 		rc = 0;
    673 	else if (rc != 0)
    674 		goto out;
    675 
    676 	if (sc->sc_pipe_tx == NULL || sc->sc_pipe_rx == NULL) {
    677 		if (udav_openpipes(sc)) {
    678 			splx(s);
    679 			return (EIO);
    680 		}
    681 	}
    682 
    683 	ifp->if_flags |= IFF_RUNNING;
    684 	ifp->if_flags &= ~IFF_OACTIVE;
    685 
    686 	usb_callout(sc->sc_stat_ch, hz, udav_tick, sc);
    687 
    688 out:
    689 	splx(s);
    690 	return rc;
    691 }
    692 
    693 Static void
    694 udav_reset(struct udav_softc *sc)
    695 {
    696 	int i;
    697 
    698 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
    699 
    700 	if (sc->sc_dying)
    701 		return;
    702 
    703 	/* Select PHY */
    704 #if 1
    705 	/*
    706 	 * XXX: force select internal phy.
    707 	 *	external phy routines are not tested.
    708 	 */
    709 	UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
    710 #else
    711 	if (sc->sc_flags & UDAV_EXT_PHY) {
    712 		UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
    713 	} else {
    714 		UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
    715 	}
    716 #endif
    717 
    718 	UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_RST);
    719 
    720 	for (i = 0; i < UDAV_TX_TIMEOUT; i++) {
    721 		if (!(udav_csr_read1(sc, UDAV_NCR) & UDAV_NCR_RST))
    722 			break;
    723 		delay(10);	/* XXX */
    724 	}
    725 	delay(10000);		/* XXX */
    726 }
    727 
    728 int
    729 udav_activate(device_ptr_t self, enum devact act)
    730 {
    731 	struct udav_softc *sc = (struct udav_softc *)self;
    732 
    733 	DPRINTF(("%s: %s: enter, act=%d\n", USBDEVNAME(sc->sc_dev),
    734 		 __func__, act));
    735 	switch (act) {
    736 	case DVACT_ACTIVATE:
    737 		return (EOPNOTSUPP);
    738 		break;
    739 
    740 	case DVACT_DEACTIVATE:
    741 		if_deactivate(&sc->sc_ec.ec_if);
    742 		sc->sc_dying = 1;
    743 		break;
    744 	}
    745 	return (0);
    746 }
    747 
    748 #define UDAV_BITS	6
    749 
    750 #define UDAV_CALCHASH(addr) \
    751 	(ether_crc32_le((addr), ETHER_ADDR_LEN) & ((1 << UDAV_BITS) - 1))
    752 
    753 Static void
    754 udav_setmulti(struct udav_softc *sc)
    755 {
    756 	struct ifnet *ifp;
    757 	struct ether_multi *enm;
    758 	struct ether_multistep step;
    759 	u_int8_t hashes[8];
    760 	int h = 0;
    761 
    762 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
    763 
    764 	if (sc->sc_dying)
    765 		return;
    766 
    767 	ifp = GET_IFP(sc);
    768 
    769 	if (ifp->if_flags & IFF_PROMISC) {
    770 		UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
    771 		return;
    772 	} else if (ifp->if_flags & IFF_ALLMULTI) {
    773 	allmulti:
    774 		ifp->if_flags |= IFF_ALLMULTI;
    775 		UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
    776 		UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_PRMSC);
    777 		return;
    778 	}
    779 
    780 	/* first, zot all the existing hash bits */
    781 	memset(hashes, 0x00, sizeof(hashes));
    782 	hashes[7] |= 0x80;	/* broadcast address */
    783 	udav_csr_write(sc, UDAV_MAR, hashes, sizeof(hashes));
    784 
    785 	/* now program new ones */
    786 	ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
    787 	while (enm != NULL) {
    788 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    789 			   ETHER_ADDR_LEN) != 0)
    790 			goto allmulti;
    791 
    792 		h = UDAV_CALCHASH(enm->enm_addrlo);
    793 		hashes[h>>3] |= 1 << (h & 0x7);
    794 		ETHER_NEXT_MULTI(step, enm);
    795 	}
    796 
    797 	/* disable all multicast */
    798 	ifp->if_flags &= ~IFF_ALLMULTI;
    799 	UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
    800 
    801 	/* write hash value to the register */
    802 	udav_csr_write(sc, UDAV_MAR, hashes, sizeof(hashes));
    803 }
    804 
    805 Static int
    806 udav_openpipes(struct udav_softc *sc)
    807 {
    808 	struct udav_chain *c;
    809 	usbd_status err;
    810 	int i;
    811 	int error = 0;
    812 
    813 	if (sc->sc_dying)
    814 		return (EIO);
    815 
    816 	sc->sc_refcnt++;
    817 
    818 	/* Open RX pipe */
    819 	err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkin_no,
    820 			     USBD_EXCLUSIVE_USE, &sc->sc_pipe_rx);
    821 	if (err) {
    822 		printf("%s: open rx pipe failed: %s\n",
    823 		       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    824 		error = EIO;
    825 		goto done;
    826 	}
    827 
    828 	/* Open TX pipe */
    829 	err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkout_no,
    830 			     USBD_EXCLUSIVE_USE, &sc->sc_pipe_tx);
    831 	if (err) {
    832 		printf("%s: open tx pipe failed: %s\n",
    833 		       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    834 		error = EIO;
    835 		goto done;
    836 	}
    837 
    838 #if 0
    839 	/* XXX: interrupt endpoint is not yet supported */
    840 	/* Open Interrupt pipe */
    841 	err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_intrin_no,
    842 				  USBD_EXCLUSIVE_USE, &sc->sc_pipe_intr, sc,
    843 				  &sc->sc_cdata.udav_ibuf, UDAV_INTR_PKGLEN,
    844 				  udav_intr, USBD_DEFAULT_INTERVAL);
    845 	if (err) {
    846 		printf("%s: open intr pipe failed: %s\n",
    847 		       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    848 		error = EIO;
    849 		goto done;
    850 	}
    851 #endif
    852 
    853 
    854 	/* Start up the receive pipe. */
    855 	for (i = 0; i < UDAV_RX_LIST_CNT; i++) {
    856 		c = &sc->sc_cdata.udav_rx_chain[i];
    857 		usbd_setup_xfer(c->udav_xfer, sc->sc_pipe_rx,
    858 				c, c->udav_buf, UDAV_BUFSZ,
    859 				USBD_SHORT_XFER_OK | USBD_NO_COPY,
    860 				USBD_NO_TIMEOUT, udav_rxeof);
    861 		(void)usbd_transfer(c->udav_xfer);
    862 		DPRINTF(("%s: %s: start read\n", USBDEVNAME(sc->sc_dev),
    863 			 __func__));
    864 	}
    865 
    866  done:
    867 	if (--sc->sc_refcnt < 0)
    868 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    869 
    870 	return (error);
    871 }
    872 
    873 Static int
    874 udav_newbuf(struct udav_softc *sc, struct udav_chain *c, struct mbuf *m)
    875 {
    876 	struct mbuf *m_new = NULL;
    877 
    878 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
    879 
    880 	if (m == NULL) {
    881 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    882 		if (m_new == NULL) {
    883 			printf("%s: no memory for rx list "
    884 			       "-- packet dropped!\n", USBDEVNAME(sc->sc_dev));
    885 			return (ENOBUFS);
    886 		}
    887 		MCLGET(m_new, M_DONTWAIT);
    888 		if (!(m_new->m_flags & M_EXT)) {
    889 			printf("%s: no memory for rx list "
    890 			       "-- packet dropped!\n", USBDEVNAME(sc->sc_dev));
    891 			m_freem(m_new);
    892 			return (ENOBUFS);
    893 		}
    894 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    895 	} else {
    896 		m_new = m;
    897 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    898 		m_new->m_data = m_new->m_ext.ext_buf;
    899 	}
    900 
    901 	m_adj(m_new, ETHER_ALIGN);
    902 	c->udav_mbuf = m_new;
    903 
    904 	return (0);
    905 }
    906 
    907 
    908 Static int
    909 udav_rx_list_init(struct udav_softc *sc)
    910 {
    911 	struct udav_cdata *cd;
    912 	struct udav_chain *c;
    913 	int i;
    914 
    915 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
    916 
    917 	cd = &sc->sc_cdata;
    918 	for (i = 0; i < UDAV_RX_LIST_CNT; i++) {
    919 		c = &cd->udav_rx_chain[i];
    920 		c->udav_sc = sc;
    921 		c->udav_idx = i;
    922 		if (udav_newbuf(sc, c, NULL) == ENOBUFS)
    923 			return (ENOBUFS);
    924 		if (c->udav_xfer == NULL) {
    925 			c->udav_xfer = usbd_alloc_xfer(sc->sc_udev);
    926 			if (c->udav_xfer == NULL)
    927 				return (ENOBUFS);
    928 			c->udav_buf = usbd_alloc_buffer(c->udav_xfer, UDAV_BUFSZ);
    929 			if (c->udav_buf == NULL) {
    930 				usbd_free_xfer(c->udav_xfer);
    931 				return (ENOBUFS);
    932 			}
    933 		}
    934 	}
    935 
    936 	return (0);
    937 }
    938 
    939 Static int
    940 udav_tx_list_init(struct udav_softc *sc)
    941 {
    942 	struct udav_cdata *cd;
    943 	struct udav_chain *c;
    944 	int i;
    945 
    946 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
    947 
    948 	cd = &sc->sc_cdata;
    949 	for (i = 0; i < UDAV_TX_LIST_CNT; i++) {
    950 		c = &cd->udav_tx_chain[i];
    951 		c->udav_sc = sc;
    952 		c->udav_idx = i;
    953 		c->udav_mbuf = NULL;
    954 		if (c->udav_xfer == NULL) {
    955 			c->udav_xfer = usbd_alloc_xfer(sc->sc_udev);
    956 			if (c->udav_xfer == NULL)
    957 				return (ENOBUFS);
    958 			c->udav_buf = usbd_alloc_buffer(c->udav_xfer, UDAV_BUFSZ);
    959 			if (c->udav_buf == NULL) {
    960 				usbd_free_xfer(c->udav_xfer);
    961 				return (ENOBUFS);
    962 			}
    963 		}
    964 	}
    965 
    966 	return (0);
    967 }
    968 
    969 Static void
    970 udav_start(struct ifnet *ifp)
    971 {
    972 	struct udav_softc *sc = ifp->if_softc;
    973 	struct mbuf *m_head = NULL;
    974 
    975 	DPRINTF(("%s: %s: enter, link=%d\n", USBDEVNAME(sc->sc_dev),
    976 		 __func__, sc->sc_link));
    977 
    978 	if (sc->sc_dying)
    979 		return;
    980 
    981 	if (!sc->sc_link)
    982 		return;
    983 
    984 	if (ifp->if_flags & IFF_OACTIVE)
    985 		return;
    986 
    987 	IFQ_POLL(&ifp->if_snd, m_head);
    988 	if (m_head == NULL)
    989 		return;
    990 
    991 	if (udav_send(sc, m_head, 0)) {
    992 		ifp->if_flags |= IFF_OACTIVE;
    993 		return;
    994 	}
    995 
    996 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
    997 
    998 #if NBPFILTER > 0
    999 	if (ifp->if_bpf)
   1000 		bpf_mtap(ifp->if_bpf, m_head);
   1001 #endif
   1002 
   1003 	ifp->if_flags |= IFF_OACTIVE;
   1004 
   1005 	/* Set a timeout in case the chip goes out to lunch. */
   1006 	ifp->if_timer = 5;
   1007 }
   1008 
   1009 Static int
   1010 udav_send(struct udav_softc *sc, struct mbuf *m, int idx)
   1011 {
   1012 	int total_len;
   1013 	struct udav_chain *c;
   1014 	usbd_status err;
   1015 
   1016 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__func__));
   1017 
   1018 	c = &sc->sc_cdata.udav_tx_chain[idx];
   1019 
   1020 	/* Copy the mbuf data into a contiguous buffer */
   1021 	/*  first 2 bytes are packet length */
   1022 	m_copydata(m, 0, m->m_pkthdr.len, c->udav_buf + 2);
   1023 	c->udav_mbuf = m;
   1024 	total_len = m->m_pkthdr.len;
   1025 	if (total_len < UDAV_MIN_FRAME_LEN) {
   1026 		memset(c->udav_buf + 2 + total_len, 0,
   1027 		    UDAV_MIN_FRAME_LEN - total_len);
   1028 		total_len = UDAV_MIN_FRAME_LEN;
   1029 	}
   1030 
   1031 	/* Frame length is specified in the first 2bytes of the buffer */
   1032 	c->udav_buf[0] = (u_int8_t)total_len;
   1033 	c->udav_buf[1] = (u_int8_t)(total_len >> 8);
   1034 	total_len += 2;
   1035 
   1036 	usbd_setup_xfer(c->udav_xfer, sc->sc_pipe_tx, c, c->udav_buf, total_len,
   1037 			USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
   1038 			UDAV_TX_TIMEOUT, udav_txeof);
   1039 
   1040 	/* Transmit */
   1041 	sc->sc_refcnt++;
   1042 	err = usbd_transfer(c->udav_xfer);
   1043 	if (--sc->sc_refcnt < 0)
   1044 		usb_detach_wakeup(USBDEV(sc->sc_dev));
   1045 	if (err != USBD_IN_PROGRESS) {
   1046 		printf("%s: udav_send error=%s\n", USBDEVNAME(sc->sc_dev),
   1047 		       usbd_errstr(err));
   1048 		/* Stop the interface */
   1049 		usb_add_task(sc->sc_udev, &sc->sc_stop_task,
   1050 		    USB_TASKQ_DRIVER);
   1051 		return (EIO);
   1052 	}
   1053 
   1054 	DPRINTF(("%s: %s: send %d bytes\n", USBDEVNAME(sc->sc_dev),
   1055 		 __func__, total_len));
   1056 
   1057 	sc->sc_cdata.udav_tx_cnt++;
   1058 
   1059 	return (0);
   1060 }
   1061 
   1062 Static void
   1063 udav_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
   1064     usbd_status status)
   1065 {
   1066 	struct udav_chain *c = priv;
   1067 	struct udav_softc *sc = c->udav_sc;
   1068 	struct ifnet *ifp = GET_IFP(sc);
   1069 	int s;
   1070 
   1071 	if (sc->sc_dying)
   1072 		return;
   1073 
   1074 	s = splnet();
   1075 
   1076 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
   1077 
   1078 	ifp->if_timer = 0;
   1079 	ifp->if_flags &= ~IFF_OACTIVE;
   1080 
   1081 	if (status != USBD_NORMAL_COMPLETION) {
   1082 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
   1083 			splx(s);
   1084 			return;
   1085 		}
   1086 		ifp->if_oerrors++;
   1087 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->sc_dev),
   1088 		       usbd_errstr(status));
   1089 		if (status == USBD_STALLED) {
   1090 			sc->sc_refcnt++;
   1091 			usbd_clear_endpoint_stall_async(sc->sc_pipe_tx);
   1092 			if (--sc->sc_refcnt < 0)
   1093 				usb_detach_wakeup(USBDEV(sc->sc_dev));
   1094 		}
   1095 		splx(s);
   1096 		return;
   1097 	}
   1098 
   1099 	ifp->if_opackets++;
   1100 
   1101 	m_freem(c->udav_mbuf);
   1102 	c->udav_mbuf = NULL;
   1103 
   1104 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1105 		udav_start(ifp);
   1106 
   1107 	splx(s);
   1108 }
   1109 
   1110 Static void
   1111 udav_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
   1112 {
   1113 	struct udav_chain *c = priv;
   1114 	struct udav_softc *sc = c->udav_sc;
   1115 	struct ifnet *ifp = GET_IFP(sc);
   1116 	struct mbuf *m;
   1117 	u_int32_t total_len;
   1118 	u_int8_t *pktstat;
   1119 	int s;
   1120 
   1121 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__func__));
   1122 
   1123 	if (sc->sc_dying)
   1124 		return;
   1125 
   1126 	if (status != USBD_NORMAL_COMPLETION) {
   1127 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   1128 			return;
   1129 		sc->sc_rx_errs++;
   1130 		if (usbd_ratecheck(&sc->sc_rx_notice)) {
   1131 			printf("%s: %u usb errors on rx: %s\n",
   1132 			       USBDEVNAME(sc->sc_dev), sc->sc_rx_errs,
   1133 			       usbd_errstr(status));
   1134 			sc->sc_rx_errs = 0;
   1135 		}
   1136 		if (status == USBD_STALLED) {
   1137 			sc->sc_refcnt++;
   1138 			usbd_clear_endpoint_stall_async(sc->sc_pipe_rx);
   1139 			if (--sc->sc_refcnt < 0)
   1140 				usb_detach_wakeup(USBDEV(sc->sc_dev));
   1141 		}
   1142 		goto done;
   1143 	}
   1144 
   1145 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
   1146 
   1147 	/* copy data to mbuf */
   1148 	m = c->udav_mbuf;
   1149 	memcpy(mtod(m, char *), c->udav_buf, total_len);
   1150 
   1151 	/* first byte in received data */
   1152 	pktstat = mtod(m, u_int8_t *);
   1153 	m_adj(m, sizeof(u_int8_t));
   1154 	DPRINTF(("%s: RX Status: 0x%02x\n", USBDEVNAME(sc->sc_dev),
   1155 				*pktstat));
   1156 
   1157 	total_len = UGETW(mtod(m, u_int8_t *));
   1158 	m_adj(m, sizeof(u_int16_t));
   1159 
   1160 	if (*pktstat & UDAV_RSR_LCS) {
   1161 		ifp->if_collisions++;
   1162 		goto done;
   1163 	}
   1164 
   1165 	if (total_len < sizeof(struct ether_header) ||
   1166 	    *pktstat & UDAV_RSR_ERR) {
   1167 		ifp->if_ierrors++;
   1168 		goto done;
   1169 	}
   1170 
   1171 	ifp->if_ipackets++;
   1172 	total_len -= ETHER_CRC_LEN;
   1173 
   1174 	m->m_pkthdr.len = m->m_len = total_len;
   1175 	m->m_pkthdr.rcvif = ifp;
   1176 
   1177 	s = splnet();
   1178 
   1179 	if (udav_newbuf(sc, c, NULL) == ENOBUFS) {
   1180 		ifp->if_ierrors++;
   1181 		goto done1;
   1182 	}
   1183 
   1184 #if NBPFILTER > 0
   1185 	if (ifp->if_bpf)
   1186 		BPF_MTAP(ifp, m);
   1187 #endif
   1188 
   1189 	DPRINTF(("%s: %s: deliver %d\n", USBDEVNAME(sc->sc_dev),
   1190 		 __func__, m->m_len));
   1191 	IF_INPUT(ifp, m);
   1192 
   1193  done1:
   1194 	splx(s);
   1195 
   1196  done:
   1197 	/* Setup new transfer */
   1198 	usbd_setup_xfer(xfer, sc->sc_pipe_rx, c, c->udav_buf, UDAV_BUFSZ,
   1199 			USBD_SHORT_XFER_OK | USBD_NO_COPY,
   1200 			USBD_NO_TIMEOUT, udav_rxeof);
   1201 	sc->sc_refcnt++;
   1202 	usbd_transfer(xfer);
   1203 	if (--sc->sc_refcnt < 0)
   1204 		usb_detach_wakeup(USBDEV(sc->sc_dev));
   1205 
   1206 	DPRINTF(("%s: %s: start rx\n", USBDEVNAME(sc->sc_dev), __func__));
   1207 }
   1208 
   1209 #if 0
   1210 Static void udav_intr()
   1211 {
   1212 }
   1213 #endif
   1214 
   1215 Static int
   1216 udav_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1217 {
   1218 	struct udav_softc *sc = ifp->if_softc;
   1219 	int s, error = 0;
   1220 
   1221 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
   1222 
   1223 	if (sc->sc_dying)
   1224 		return (EIO);
   1225 
   1226 	s = splnet();
   1227 
   1228 	error = ether_ioctl(ifp, cmd, data);
   1229 	if (error == ENETRESET) {
   1230 		if (ifp->if_flags & IFF_RUNNING)
   1231 			udav_setmulti(sc);
   1232 		error = 0;
   1233 	}
   1234 
   1235 	splx(s);
   1236 
   1237 	return (error);
   1238 }
   1239 
   1240 Static void
   1241 udav_watchdog(struct ifnet *ifp)
   1242 {
   1243 	struct udav_softc *sc = ifp->if_softc;
   1244 	struct udav_chain *c;
   1245 	usbd_status stat;
   1246 	int s;
   1247 
   1248 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
   1249 
   1250 	ifp->if_oerrors++;
   1251 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->sc_dev));
   1252 
   1253 	s = splusb();
   1254 	c = &sc->sc_cdata.udav_tx_chain[0];
   1255 	usbd_get_xfer_status(c->udav_xfer, NULL, NULL, NULL, &stat);
   1256 	udav_txeof(c->udav_xfer, c, stat);
   1257 
   1258 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1259 		udav_start(ifp);
   1260 	splx(s);
   1261 }
   1262 
   1263 Static void
   1264 udav_stop_task(struct udav_softc *sc)
   1265 {
   1266 	udav_stop(GET_IFP(sc), 1);
   1267 }
   1268 
   1269 /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */
   1270 Static void
   1271 udav_stop(struct ifnet *ifp, int disable)
   1272 {
   1273 	struct udav_softc *sc = ifp->if_softc;
   1274 	usbd_status err;
   1275 	int i;
   1276 
   1277 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
   1278 
   1279 	ifp->if_timer = 0;
   1280 
   1281 	udav_reset(sc);
   1282 
   1283 	usb_uncallout(sc->sc_stat_ch, udav_tick, sc);
   1284 
   1285 	/* Stop transfers */
   1286 	/* RX endpoint */
   1287 	if (sc->sc_pipe_rx != NULL) {
   1288 		err = usbd_abort_pipe(sc->sc_pipe_rx);
   1289 		if (err)
   1290 			printf("%s: abort rx pipe failed: %s\n",
   1291 			       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
   1292 		err = usbd_close_pipe(sc->sc_pipe_rx);
   1293 		if (err)
   1294 			printf("%s: close rx pipe failed: %s\n",
   1295 			       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
   1296 		sc->sc_pipe_rx = NULL;
   1297 	}
   1298 
   1299 	/* TX endpoint */
   1300 	if (sc->sc_pipe_tx != NULL) {
   1301 		err = usbd_abort_pipe(sc->sc_pipe_tx);
   1302 		if (err)
   1303 			printf("%s: abort tx pipe failed: %s\n",
   1304 			       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
   1305 		err = usbd_close_pipe(sc->sc_pipe_tx);
   1306 		if (err)
   1307 			printf("%s: close tx pipe failed: %s\n",
   1308 			       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
   1309 		sc->sc_pipe_tx = NULL;
   1310 	}
   1311 
   1312 #if 0
   1313 	/* XXX: Interrupt endpoint is not yet supported!! */
   1314 	/* Interrupt endpoint */
   1315 	if (sc->sc_pipe_intr != NULL) {
   1316 		err = usbd_abort_pipe(sc->sc_pipe_intr);
   1317 		if (err)
   1318 			printf("%s: abort intr pipe failed: %s\n",
   1319 			       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
   1320 		err = usbd_close_pipe(sc->sc_pipe_intr);
   1321 		if (err)
   1322 			printf("%s: close intr pipe failed: %s\n",
   1323 			       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
   1324 		sc->sc_pipe_intr = NULL;
   1325 	}
   1326 #endif
   1327 
   1328 	/* Free RX resources. */
   1329 	for (i = 0; i < UDAV_RX_LIST_CNT; i++) {
   1330 		if (sc->sc_cdata.udav_rx_chain[i].udav_mbuf != NULL) {
   1331 			m_freem(sc->sc_cdata.udav_rx_chain[i].udav_mbuf);
   1332 			sc->sc_cdata.udav_rx_chain[i].udav_mbuf = NULL;
   1333 		}
   1334 		if (sc->sc_cdata.udav_rx_chain[i].udav_xfer != NULL) {
   1335 			usbd_free_xfer(sc->sc_cdata.udav_rx_chain[i].udav_xfer);
   1336 			sc->sc_cdata.udav_rx_chain[i].udav_xfer = NULL;
   1337 		}
   1338 	}
   1339 
   1340 	/* Free TX resources. */
   1341 	for (i = 0; i < UDAV_TX_LIST_CNT; i++) {
   1342 		if (sc->sc_cdata.udav_tx_chain[i].udav_mbuf != NULL) {
   1343 			m_freem(sc->sc_cdata.udav_tx_chain[i].udav_mbuf);
   1344 			sc->sc_cdata.udav_tx_chain[i].udav_mbuf = NULL;
   1345 		}
   1346 		if (sc->sc_cdata.udav_tx_chain[i].udav_xfer != NULL) {
   1347 			usbd_free_xfer(sc->sc_cdata.udav_tx_chain[i].udav_xfer);
   1348 			sc->sc_cdata.udav_tx_chain[i].udav_xfer = NULL;
   1349 		}
   1350 	}
   1351 
   1352 	sc->sc_link = 0;
   1353 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1354 }
   1355 
   1356 /* Set media options */
   1357 Static int
   1358 udav_ifmedia_change(struct ifnet *ifp)
   1359 {
   1360 	struct udav_softc *sc = ifp->if_softc;
   1361 	struct mii_data *mii = GET_MII(sc);
   1362 	int rc;
   1363 
   1364 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
   1365 
   1366 	if (sc->sc_dying)
   1367 		return (0);
   1368 
   1369 	sc->sc_link = 0;
   1370 	if ((rc = mii_mediachg(mii)) == ENXIO)
   1371 		return 0;
   1372 	return rc;
   1373 }
   1374 
   1375 /* Report current media status. */
   1376 Static void
   1377 udav_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
   1378 {
   1379 	struct udav_softc *sc = ifp->if_softc;
   1380 
   1381 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
   1382 
   1383 	if (sc->sc_dying)
   1384 		return;
   1385 
   1386 	ether_mediastatus(ifp, ifmr);
   1387 }
   1388 
   1389 Static void
   1390 udav_tick(void *xsc)
   1391 {
   1392 	struct udav_softc *sc = xsc;
   1393 
   1394 	if (sc == NULL)
   1395 		return;
   1396 
   1397 	DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
   1398 			__func__));
   1399 
   1400 	if (sc->sc_dying)
   1401 		return;
   1402 
   1403 	/* Perform periodic stuff in process context */
   1404 	usb_add_task(sc->sc_udev, &sc->sc_tick_task,
   1405 	    USB_TASKQ_DRIVER);
   1406 }
   1407 
   1408 Static void
   1409 udav_tick_task(void *xsc)
   1410 {
   1411 	struct udav_softc *sc = xsc;
   1412 	struct ifnet *ifp;
   1413 	struct mii_data *mii;
   1414 	int s;
   1415 
   1416 	if (sc == NULL)
   1417 		return;
   1418 
   1419 	DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
   1420 			__func__));
   1421 
   1422 	if (sc->sc_dying)
   1423 		return;
   1424 
   1425 	ifp = GET_IFP(sc);
   1426 	mii = GET_MII(sc);
   1427 
   1428 	if (mii == NULL)
   1429 		return;
   1430 
   1431 	s = splnet();
   1432 
   1433 	mii_tick(mii);
   1434 	if (!sc->sc_link) {
   1435 		mii_pollstat(mii);
   1436 		if (mii->mii_media_status & IFM_ACTIVE &&
   1437 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
   1438 			DPRINTF(("%s: %s: got link\n",
   1439 				 USBDEVNAME(sc->sc_dev), __func__));
   1440 			sc->sc_link++;
   1441 			if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1442 				   udav_start(ifp);
   1443 		}
   1444 	}
   1445 
   1446 	usb_callout(sc->sc_stat_ch, hz, udav_tick, sc);
   1447 
   1448 	splx(s);
   1449 }
   1450 
   1451 /* Get exclusive access to the MII registers */
   1452 Static void
   1453 udav_lock_mii(struct udav_softc *sc)
   1454 {
   1455 	DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
   1456 			__func__));
   1457 
   1458 	sc->sc_refcnt++;
   1459 	mutex_enter(&sc->sc_mii_lock);
   1460 }
   1461 
   1462 Static void
   1463 udav_unlock_mii(struct udav_softc *sc)
   1464 {
   1465 	DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
   1466 		       __func__));
   1467 
   1468 	mutex_exit(&sc->sc_mii_lock);
   1469 	if (--sc->sc_refcnt < 0)
   1470 		usb_detach_wakeup(USBDEV(sc->sc_dev));
   1471 }
   1472 
   1473 Static int
   1474 udav_miibus_readreg(device_ptr_t dev, int phy, int reg)
   1475 {
   1476 	struct udav_softc *sc;
   1477 	u_int8_t val[2];
   1478 	u_int16_t data16;
   1479 
   1480 	if (dev == NULL)
   1481 		return (0);
   1482 
   1483 	sc = USBGETSOFTC(dev);
   1484 
   1485 	DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n",
   1486 		 USBDEVNAME(sc->sc_dev), __func__, phy, reg));
   1487 
   1488 	if (sc->sc_dying) {
   1489 #ifdef DIAGNOSTIC
   1490 		printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev),
   1491 		       __func__);
   1492 #endif
   1493 		return (0);
   1494 	}
   1495 
   1496 	/* XXX: one PHY only for the internal PHY */
   1497 	if (phy != 0) {
   1498 		DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
   1499 			 USBDEVNAME(sc->sc_dev), __func__, phy));
   1500 		return (0);
   1501 	}
   1502 
   1503 	udav_lock_mii(sc);
   1504 
   1505 	/* select internal PHY and set PHY register address */
   1506 	udav_csr_write1(sc, UDAV_EPAR,
   1507 			UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
   1508 
   1509 	/* select PHY operation and start read command */
   1510 	udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR);
   1511 
   1512 	/* XXX: should be wait? */
   1513 
   1514 	/* end read command */
   1515 	UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRR);
   1516 
   1517 	/* retrieve the result from data registers */
   1518 	udav_csr_read(sc, UDAV_EPDRL, val, 2);
   1519 
   1520 	udav_unlock_mii(sc);
   1521 
   1522 	data16 = val[0] | (val[1] << 8);
   1523 
   1524 	DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04x\n",
   1525 		 USBDEVNAME(sc->sc_dev), __func__, phy, reg, data16));
   1526 
   1527 	return (data16);
   1528 }
   1529 
   1530 Static void
   1531 udav_miibus_writereg(device_ptr_t dev, int phy, int reg, int data)
   1532 {
   1533 	struct udav_softc *sc;
   1534 	u_int8_t val[2];
   1535 
   1536 	if (dev == NULL)
   1537 		return;
   1538 
   1539 	sc = USBGETSOFTC(dev);
   1540 
   1541 	DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n",
   1542 		 USBDEVNAME(sc->sc_dev), __func__, phy, reg, data));
   1543 
   1544 	if (sc->sc_dying) {
   1545 #ifdef DIAGNOSTIC
   1546 		printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev),
   1547 		       __func__);
   1548 #endif
   1549 		return;
   1550 	}
   1551 
   1552 	/* XXX: one PHY only for the internal PHY */
   1553 	if (phy != 0) {
   1554 		DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
   1555 			 USBDEVNAME(sc->sc_dev), __func__, phy));
   1556 		return;
   1557 	}
   1558 
   1559 	udav_lock_mii(sc);
   1560 
   1561 	/* select internal PHY and set PHY register address */
   1562 	udav_csr_write1(sc, UDAV_EPAR,
   1563 			UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
   1564 
   1565 	/* put the value to the data registers */
   1566 	val[0] = data & 0xff;
   1567 	val[1] = (data >> 8) & 0xff;
   1568 	udav_csr_write(sc, UDAV_EPDRL, val, 2);
   1569 
   1570 	/* select PHY operation and start write command */
   1571 	udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW);
   1572 
   1573 	/* XXX: should be wait? */
   1574 
   1575 	/* end write command */
   1576 	UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRW);
   1577 
   1578 	udav_unlock_mii(sc);
   1579 
   1580 	return;
   1581 }
   1582 
   1583 Static void
   1584 udav_miibus_statchg(device_ptr_t dev)
   1585 {
   1586 #ifdef UDAV_DEBUG
   1587 	struct udav_softc *sc;
   1588 
   1589 	if (dev == NULL)
   1590 		return;
   1591 
   1592 	sc = USBGETSOFTC(dev);
   1593 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
   1594 #endif
   1595 	/* Nothing to do */
   1596 }
   1597