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