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