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