Home | History | Annotate | Line # | Download | only in usb
if_url.c revision 1.61
      1 /*	$NetBSD: if_url.c,v 1.61 2019/01/22 03:42:28 msaitoh 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.61 2019/01/22 03:42:28 msaitoh 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, uint16_t *);
    117 Static int url_int_miibus_writereg(device_t, int, int, uint16_t);
    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 	    NULL);
    361 	usb_rem_task_wait(sc->sc_udev, &sc->sc_stop_task, USB_TASKQ_DRIVER,
    362 	    NULL);
    363 
    364 	s = splusb();
    365 
    366 	if (--sc->sc_refcnt >= 0) {
    367 		/* Wait for processes to go away */
    368 		usb_detach_waitold(sc->sc_dev);
    369 	}
    370 
    371 	if (ifp->if_flags & IFF_RUNNING)
    372 		url_stop(GET_IFP(sc), 1);
    373 
    374 	rnd_detach_source(&sc->rnd_source);
    375 	mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
    376 	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
    377 	ether_ifdetach(ifp);
    378 	if_detach(ifp);
    379 
    380 #ifdef DIAGNOSTIC
    381 	if (sc->sc_pipe_tx != NULL)
    382 		aprint_debug_dev(self, "detach has active tx endpoint.\n");
    383 	if (sc->sc_pipe_rx != NULL)
    384 		aprint_debug_dev(self, "detach has active rx endpoint.\n");
    385 	if (sc->sc_pipe_intr != NULL)
    386 		aprint_debug_dev(self, "detach has active intr endpoint.\n");
    387 #endif
    388 
    389 	sc->sc_attached = 0;
    390 
    391 	splx(s);
    392 
    393 	rw_destroy(&sc->sc_mii_rwlock);
    394 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    395 
    396 	return 0;
    397 }
    398 
    399 /* read/write memory */
    400 Static int
    401 url_mem(struct url_softc *sc, int cmd, int offset, void *buf, int len)
    402 {
    403 	usb_device_request_t req;
    404 	usbd_status err;
    405 
    406 	if (sc == NULL)
    407 		return 0;
    408 
    409 	DPRINTFN(0x200,
    410 		("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    411 
    412 	if (sc->sc_dying)
    413 		return 0;
    414 
    415 	if (cmd == URL_CMD_READMEM)
    416 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
    417 	else
    418 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
    419 	req.bRequest = URL_REQ_MEM;
    420 	USETW(req.wValue, offset);
    421 	USETW(req.wIndex, 0x0000);
    422 	USETW(req.wLength, len);
    423 
    424 	sc->sc_refcnt++;
    425 	err = usbd_do_request(sc->sc_udev, &req, buf);
    426 	if (--sc->sc_refcnt < 0)
    427 		usb_detach_wakeupold(sc->sc_dev);
    428 	if (err) {
    429 		DPRINTF(("%s: url_mem(): %s failed. off=%04x, err=%d\n",
    430 			 device_xname(sc->sc_dev),
    431 			 cmd == URL_CMD_READMEM ? "read" : "write",
    432 			 offset, err));
    433 	}
    434 
    435 	return err;
    436 }
    437 
    438 /* read 1byte from register */
    439 Static int
    440 url_csr_read_1(struct url_softc *sc, int reg)
    441 {
    442 	uint8_t val = 0;
    443 
    444 	DPRINTFN(0x100,
    445 		 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    446 
    447 	if (sc->sc_dying)
    448 		return 0;
    449 
    450 	return url_mem(sc, URL_CMD_READMEM, reg, &val, 1) ? 0 : val;
    451 }
    452 
    453 /* read 2bytes from register */
    454 Static int
    455 url_csr_read_2(struct url_softc *sc, int reg)
    456 {
    457 	uWord val;
    458 
    459 	DPRINTFN(0x100,
    460 		 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    461 
    462 	if (sc->sc_dying)
    463 		return 0;
    464 
    465 	USETW(val, 0);
    466 	return url_mem(sc, URL_CMD_READMEM, reg, &val, 2) ? 0 : UGETW(val);
    467 }
    468 
    469 /* write 1byte to register */
    470 Static int
    471 url_csr_write_1(struct url_softc *sc, int reg, int aval)
    472 {
    473 	uint8_t val = aval;
    474 
    475 	DPRINTFN(0x100,
    476 		 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    477 
    478 	if (sc->sc_dying)
    479 		return 0;
    480 
    481 	return url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 1) ? -1 : 0;
    482 }
    483 
    484 /* write 2bytes to register */
    485 Static int
    486 url_csr_write_2(struct url_softc *sc, int reg, int aval)
    487 {
    488 	uWord val;
    489 
    490 	DPRINTFN(0x100,
    491 		 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    492 
    493 	USETW(val, aval);
    494 
    495 	if (sc->sc_dying)
    496 		return 0;
    497 
    498 	return url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 2) ? -1 : 0;
    499 }
    500 
    501 /* write 4bytes to register */
    502 Static int
    503 url_csr_write_4(struct url_softc *sc, int reg, int aval)
    504 {
    505 	uDWord val;
    506 
    507 	DPRINTFN(0x100,
    508 		 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    509 
    510 	USETDW(val, aval);
    511 
    512 	if (sc->sc_dying)
    513 		return 0;
    514 
    515 	return url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 4) ? -1 : 0;
    516 }
    517 
    518 Static int
    519 url_init(struct ifnet *ifp)
    520 {
    521 	struct url_softc *sc = ifp->if_softc;
    522 	struct mii_data *mii = GET_MII(sc);
    523 	const u_char *eaddr;
    524 	int i, rc, s;
    525 
    526 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    527 
    528 	if (sc->sc_dying)
    529 		return EIO;
    530 
    531 	s = splnet();
    532 
    533 	/* Cancel pending I/O and free all TX/RX buffers */
    534 	url_stop(ifp, 1);
    535 
    536 	eaddr = CLLADDR(ifp->if_sadl);
    537 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    538 		url_csr_write_1(sc, URL_IDR0 + i, eaddr[i]);
    539 
    540 	/* Init transmission control register */
    541 	URL_CLRBIT(sc, URL_TCR,
    542 		   URL_TCR_TXRR1 | URL_TCR_TXRR0 |
    543 		   URL_TCR_IFG1 | URL_TCR_IFG0 |
    544 		   URL_TCR_NOCRC);
    545 
    546 	/* Init receive control register */
    547 	URL_SETBIT2(sc, URL_RCR, URL_RCR_TAIL | URL_RCR_AD);
    548 	if (ifp->if_flags & IFF_BROADCAST)
    549 		URL_SETBIT2(sc, URL_RCR, URL_RCR_AB);
    550 	else
    551 		URL_CLRBIT2(sc, URL_RCR, URL_RCR_AB);
    552 
    553 	/* If we want promiscuous mode, accept all physical frames. */
    554 	if (ifp->if_flags & IFF_PROMISC)
    555 		URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP);
    556 	else
    557 		URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP);
    558 
    559 
    560 	/* Load the multicast filter */
    561 	url_setmulti(sc);
    562 
    563 	/* Enable RX and TX */
    564 	URL_SETBIT(sc, URL_CR, URL_CR_TE | URL_CR_RE);
    565 
    566 	if ((rc = mii_mediachg(mii)) == ENXIO)
    567 		rc = 0;
    568 	else if (rc != 0)
    569 		goto out;
    570 
    571 	if (sc->sc_pipe_tx == NULL || sc->sc_pipe_rx == NULL) {
    572 		if (url_openpipes(sc)) {
    573 			splx(s);
    574 			return EIO;
    575 		}
    576 	}
    577 	/* Initialize transmit ring */
    578 	if (url_tx_list_init(sc)) {
    579 		printf("%s: tx list init failed\n", device_xname(sc->sc_dev));
    580 		splx(s);
    581 		return EIO;
    582 	}
    583 
    584 	/* Initialize receive ring */
    585 	if (url_rx_list_init(sc)) {
    586 		printf("%s: rx list init failed\n", device_xname(sc->sc_dev));
    587 		splx(s);
    588 		return EIO;
    589 	}
    590 	/* Start up the receive pipe. */
    591 	for (i = 0; i < URL_RX_LIST_CNT; i++) {
    592 		struct url_chain *c = &sc->sc_cdata.url_rx_chain[i];
    593 
    594 		usbd_setup_xfer(c->url_xfer, c, c->url_buf, URL_BUFSZ,
    595 		    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, url_rxeof);
    596 		(void)usbd_transfer(c->url_xfer);
    597 		DPRINTF(("%s: %s: start read\n", device_xname(sc->sc_dev),
    598 			 __func__));
    599 	}
    600 
    601 	ifp->if_flags |= IFF_RUNNING;
    602 	ifp->if_flags &= ~IFF_OACTIVE;
    603 
    604 	callout_reset(&sc->sc_stat_ch, hz, url_tick, sc);
    605 
    606 out:
    607 	splx(s);
    608 	return rc;
    609 }
    610 
    611 Static void
    612 url_reset(struct url_softc *sc)
    613 {
    614 	int i;
    615 
    616 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    617 
    618 	if (sc->sc_dying)
    619 		return;
    620 
    621 	URL_SETBIT(sc, URL_CR, URL_CR_SOFT_RST);
    622 
    623 	for (i = 0; i < URL_TX_TIMEOUT; i++) {
    624 		if (!(url_csr_read_1(sc, URL_CR) & URL_CR_SOFT_RST))
    625 			break;
    626 		delay(10);	/* XXX */
    627 	}
    628 
    629 	delay(10000);		/* XXX */
    630 }
    631 
    632 int
    633 url_activate(device_t self, enum devact act)
    634 {
    635 	struct url_softc *sc = device_private(self);
    636 
    637 	DPRINTF(("%s: %s: enter, act=%d\n", device_xname(sc->sc_dev),
    638 		 __func__, act));
    639 
    640 	switch (act) {
    641 	case DVACT_DEACTIVATE:
    642 		if_deactivate(&sc->sc_ec.ec_if);
    643 		sc->sc_dying = 1;
    644 		return 0;
    645 	default:
    646 		return EOPNOTSUPP;
    647 	}
    648 }
    649 
    650 #define url_calchash(addr) (ether_crc32_be((addr), ETHER_ADDR_LEN) >> 26)
    651 
    652 
    653 Static void
    654 url_setmulti(struct url_softc *sc)
    655 {
    656 	struct ifnet *ifp;
    657 	struct ether_multi *enm;
    658 	struct ether_multistep step;
    659 	uint32_t hashes[2] = { 0, 0 };
    660 	int h = 0;
    661 	int mcnt = 0;
    662 
    663 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    664 
    665 	if (sc->sc_dying)
    666 		return;
    667 
    668 	ifp = GET_IFP(sc);
    669 
    670 	if (ifp->if_flags & IFF_PROMISC) {
    671 		URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP);
    672 		return;
    673 	} else if (ifp->if_flags & IFF_ALLMULTI) {
    674 	allmulti:
    675 		ifp->if_flags |= IFF_ALLMULTI;
    676 		URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM);
    677 		URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAP);
    678 		return;
    679 	}
    680 
    681 	/* first, zot all the existing hash bits */
    682 	url_csr_write_4(sc, URL_MAR0, 0);
    683 	url_csr_write_4(sc, URL_MAR4, 0);
    684 
    685 	/* now program new ones */
    686 	ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
    687 	while (enm != NULL) {
    688 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    689 			   ETHER_ADDR_LEN) != 0)
    690 			goto allmulti;
    691 
    692 		h = url_calchash(enm->enm_addrlo);
    693 		if (h < 32)
    694 			hashes[0] |= (1 << h);
    695 		else
    696 			hashes[1] |= (1 << (h -32));
    697 		mcnt++;
    698 		ETHER_NEXT_MULTI(step, enm);
    699 	}
    700 
    701 	ifp->if_flags &= ~IFF_ALLMULTI;
    702 
    703 	URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP);
    704 
    705 	if (mcnt){
    706 		URL_SETBIT2(sc, URL_RCR, URL_RCR_AM);
    707 	} else {
    708 		URL_CLRBIT2(sc, URL_RCR, URL_RCR_AM);
    709 	}
    710 	url_csr_write_4(sc, URL_MAR0, hashes[0]);
    711 	url_csr_write_4(sc, URL_MAR4, hashes[1]);
    712 }
    713 
    714 Static int
    715 url_openpipes(struct url_softc *sc)
    716 {
    717 	usbd_status err;
    718 	int error = 0;
    719 
    720 	if (sc->sc_dying)
    721 		return EIO;
    722 
    723 	sc->sc_refcnt++;
    724 
    725 	/* Open RX pipe */
    726 	err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkin_no,
    727 			     USBD_EXCLUSIVE_USE, &sc->sc_pipe_rx);
    728 	if (err) {
    729 		printf("%s: open rx pipe failed: %s\n",
    730 		       device_xname(sc->sc_dev), usbd_errstr(err));
    731 		error = EIO;
    732 		goto done;
    733 	}
    734 
    735 	/* Open TX pipe */
    736 	err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkout_no,
    737 			     USBD_EXCLUSIVE_USE, &sc->sc_pipe_tx);
    738 	if (err) {
    739 		printf("%s: open tx pipe failed: %s\n",
    740 		       device_xname(sc->sc_dev), usbd_errstr(err));
    741 		error = EIO;
    742 		goto done;
    743 	}
    744 
    745 #if 0
    746 	/* XXX: interrupt endpoint is not yet supported */
    747 	/* Open Interrupt pipe */
    748 	err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_intrin_no,
    749 				  USBD_EXCLUSIVE_USE, &sc->sc_pipe_intr, sc,
    750 				  &sc->sc_cdata.url_ibuf, URL_INTR_PKGLEN,
    751 				  url_intr, USBD_DEFAULT_INTERVAL);
    752 	if (err) {
    753 		printf("%s: open intr pipe failed: %s\n",
    754 		       device_xname(sc->sc_dev), usbd_errstr(err));
    755 		error = EIO;
    756 		goto done;
    757 	}
    758 #endif
    759 
    760  done:
    761 	if (--sc->sc_refcnt < 0)
    762 		usb_detach_wakeupold(sc->sc_dev);
    763 
    764 	return error;
    765 }
    766 
    767 Static int
    768 url_newbuf(struct url_softc *sc, struct url_chain *c, struct mbuf *m)
    769 {
    770 	struct mbuf *m_new = NULL;
    771 
    772 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    773 
    774 	if (m == NULL) {
    775 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    776 		if (m_new == NULL) {
    777 			printf("%s: no memory for rx list "
    778 			       "-- packet dropped!\n", device_xname(sc->sc_dev));
    779 			return ENOBUFS;
    780 		}
    781 		MCLGET(m_new, M_DONTWAIT);
    782 		if (!(m_new->m_flags & M_EXT)) {
    783 			printf("%s: no memory for rx list "
    784 			       "-- packet dropped!\n", device_xname(sc->sc_dev));
    785 			m_freem(m_new);
    786 			return ENOBUFS;
    787 		}
    788 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    789 	} else {
    790 		m_new = m;
    791 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    792 		m_new->m_data = m_new->m_ext.ext_buf;
    793 	}
    794 
    795 	m_adj(m_new, ETHER_ALIGN);
    796 	c->url_mbuf = m_new;
    797 
    798 	return 0;
    799 }
    800 
    801 
    802 Static int
    803 url_rx_list_init(struct url_softc *sc)
    804 {
    805 	struct url_cdata *cd;
    806 	struct url_chain *c;
    807 	int i;
    808 
    809 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    810 
    811 	cd = &sc->sc_cdata;
    812 	for (i = 0; i < URL_RX_LIST_CNT; i++) {
    813 		c = &cd->url_rx_chain[i];
    814 		c->url_sc = sc;
    815 		c->url_idx = i;
    816 		if (url_newbuf(sc, c, NULL) == ENOBUFS)
    817 			return ENOBUFS;
    818 		if (c->url_xfer == NULL) {
    819 			int error = usbd_create_xfer(sc->sc_pipe_rx, URL_BUFSZ,
    820 			    0, 0, &c->url_xfer);
    821 			if (error)
    822 				return error;
    823 			c->url_buf = usbd_get_buffer(c->url_xfer);
    824 		}
    825 	}
    826 
    827 	return 0;
    828 }
    829 
    830 Static int
    831 url_tx_list_init(struct url_softc *sc)
    832 {
    833 	struct url_cdata *cd;
    834 	struct url_chain *c;
    835 	int i;
    836 
    837 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    838 
    839 	cd = &sc->sc_cdata;
    840 	for (i = 0; i < URL_TX_LIST_CNT; i++) {
    841 		c = &cd->url_tx_chain[i];
    842 		c->url_sc = sc;
    843 		c->url_idx = i;
    844 		c->url_mbuf = NULL;
    845 		if (c->url_xfer == NULL) {
    846 			int error = usbd_create_xfer(sc->sc_pipe_tx, URL_BUFSZ,
    847 			    USBD_FORCE_SHORT_XFER, 0, &c->url_xfer);
    848 			if (error)
    849 				return error;
    850 			c->url_buf = usbd_get_buffer(c->url_xfer);
    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, BPF_D_OUT);
    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,c, c->url_buf, total_len,
    916 	    USBD_FORCE_SHORT_XFER, URL_TX_TIMEOUT, url_txeof);
    917 
    918 	/* Transmit */
    919 	sc->sc_refcnt++;
    920 	err = usbd_transfer(c->url_xfer);
    921 	if (--sc->sc_refcnt < 0)
    922 		usb_detach_wakeupold(sc->sc_dev);
    923 	if (err != USBD_IN_PROGRESS) {
    924 		printf("%s: url_send error=%s\n", device_xname(sc->sc_dev),
    925 		       usbd_errstr(err));
    926 		/* Stop the interface */
    927 		usb_add_task(sc->sc_udev, &sc->sc_stop_task,
    928 		    USB_TASKQ_DRIVER);
    929 		return EIO;
    930 	}
    931 
    932 	DPRINTF(("%s: %s: send %d bytes\n", device_xname(sc->sc_dev),
    933 		 __func__, total_len));
    934 
    935 	sc->sc_cdata.url_tx_cnt++;
    936 
    937 	return 0;
    938 }
    939 
    940 Static void
    941 url_txeof(struct usbd_xfer *xfer, void *priv,
    942     usbd_status status)
    943 {
    944 	struct url_chain *c = priv;
    945 	struct url_softc *sc = c->url_sc;
    946 	struct ifnet *ifp = GET_IFP(sc);
    947 	int s;
    948 
    949 	if (sc->sc_dying)
    950 		return;
    951 
    952 	s = splnet();
    953 
    954 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
    955 
    956 	ifp->if_timer = 0;
    957 	ifp->if_flags &= ~IFF_OACTIVE;
    958 
    959 	if (status != USBD_NORMAL_COMPLETION) {
    960 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
    961 			splx(s);
    962 			return;
    963 		}
    964 		ifp->if_oerrors++;
    965 		printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev),
    966 		       usbd_errstr(status));
    967 		if (status == USBD_STALLED) {
    968 			sc->sc_refcnt++;
    969 			usbd_clear_endpoint_stall_async(sc->sc_pipe_tx);
    970 			if (--sc->sc_refcnt < 0)
    971 				usb_detach_wakeupold(sc->sc_dev);
    972 		}
    973 		splx(s);
    974 		return;
    975 	}
    976 
    977 	ifp->if_opackets++;
    978 
    979 	m_freem(c->url_mbuf);
    980 	c->url_mbuf = NULL;
    981 
    982 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    983 		url_start(ifp);
    984 
    985 	splx(s);
    986 }
    987 
    988 Static void
    989 url_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
    990 {
    991 	struct url_chain *c = priv;
    992 	struct url_softc *sc = c->url_sc;
    993 	struct ifnet *ifp = GET_IFP(sc);
    994 	struct mbuf *m;
    995 	uint32_t total_len;
    996 	url_rxhdr_t rxhdr;
    997 	int s;
    998 
    999 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
   1000 
   1001 	if (sc->sc_dying)
   1002 		return;
   1003 
   1004 	if (status != USBD_NORMAL_COMPLETION) {
   1005 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
   1006 			return;
   1007 		sc->sc_rx_errs++;
   1008 		if (usbd_ratecheck(&sc->sc_rx_notice)) {
   1009 			printf("%s: %u usb errors on rx: %s\n",
   1010 			       device_xname(sc->sc_dev), sc->sc_rx_errs,
   1011 			       usbd_errstr(status));
   1012 			sc->sc_rx_errs = 0;
   1013 		}
   1014 		if (status == USBD_STALLED) {
   1015 			sc->sc_refcnt++;
   1016 			usbd_clear_endpoint_stall_async(sc->sc_pipe_rx);
   1017 			if (--sc->sc_refcnt < 0)
   1018 				usb_detach_wakeupold(sc->sc_dev);
   1019 		}
   1020 		goto done;
   1021 	}
   1022 
   1023 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
   1024 
   1025 	memcpy(mtod(c->url_mbuf, char *), c->url_buf, total_len);
   1026 
   1027 	if (total_len <= ETHER_CRC_LEN) {
   1028 		ifp->if_ierrors++;
   1029 		goto done;
   1030 	}
   1031 
   1032 	memcpy(&rxhdr, c->url_buf + total_len - ETHER_CRC_LEN, sizeof(rxhdr));
   1033 
   1034 	DPRINTF(("%s: RX Status: %dbytes%s%s%s%s packets\n",
   1035 		 device_xname(sc->sc_dev),
   1036 		 UGETW(rxhdr) & URL_RXHDR_BYTEC_MASK,
   1037 		 UGETW(rxhdr) & URL_RXHDR_VALID_MASK ? ", Valid" : "",
   1038 		 UGETW(rxhdr) & URL_RXHDR_RUNTPKT_MASK ? ", Runt" : "",
   1039 		 UGETW(rxhdr) & URL_RXHDR_PHYPKT_MASK ? ", Physical match" : "",
   1040 		 UGETW(rxhdr) & URL_RXHDR_MCASTPKT_MASK ? ", Multicast" : ""));
   1041 
   1042 	if ((UGETW(rxhdr) & URL_RXHDR_VALID_MASK) == 0) {
   1043 		ifp->if_ierrors++;
   1044 		goto done;
   1045 	}
   1046 
   1047 	total_len -= ETHER_CRC_LEN;
   1048 
   1049 	m = c->url_mbuf;
   1050 	m->m_pkthdr.len = m->m_len = total_len;
   1051 	m_set_rcvif(m, ifp);
   1052 
   1053 	s = splnet();
   1054 
   1055 	if (url_newbuf(sc, c, NULL) == ENOBUFS) {
   1056 		ifp->if_ierrors++;
   1057 		goto done1;
   1058 	}
   1059 
   1060 	DPRINTF(("%s: %s: deliver %d\n", device_xname(sc->sc_dev),
   1061 		 __func__, m->m_len));
   1062 	if_percpuq_enqueue((ifp)->if_percpuq, (m));
   1063 
   1064  done1:
   1065 	splx(s);
   1066 
   1067  done:
   1068 	/* Setup new transfer */
   1069 	usbd_setup_xfer(xfer, c, c->url_buf, URL_BUFSZ, USBD_SHORT_XFER_OK,
   1070 	    USBD_NO_TIMEOUT, url_rxeof);
   1071 	sc->sc_refcnt++;
   1072 	usbd_transfer(xfer);
   1073 	if (--sc->sc_refcnt < 0)
   1074 		usb_detach_wakeupold(sc->sc_dev);
   1075 
   1076 	DPRINTF(("%s: %s: start rx\n", device_xname(sc->sc_dev), __func__));
   1077 }
   1078 
   1079 #if 0
   1080 Static void url_intr(void)
   1081 {
   1082 }
   1083 #endif
   1084 
   1085 Static int
   1086 url_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1087 {
   1088 	struct url_softc *sc = ifp->if_softc;
   1089 	int s, error = 0;
   1090 
   1091 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
   1092 
   1093 	if (sc->sc_dying)
   1094 		return EIO;
   1095 
   1096 	s = splnet();
   1097 
   1098 	error = ether_ioctl(ifp, cmd, data);
   1099 	if (error == ENETRESET) {
   1100 		if (ifp->if_flags & IFF_RUNNING)
   1101 			url_setmulti(sc);
   1102 		error = 0;
   1103 	}
   1104 
   1105 	splx(s);
   1106 
   1107 	return error;
   1108 }
   1109 
   1110 Static void
   1111 url_watchdog(struct ifnet *ifp)
   1112 {
   1113 	struct url_softc *sc = ifp->if_softc;
   1114 	struct url_chain *c;
   1115 	usbd_status stat;
   1116 	int s;
   1117 
   1118 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
   1119 
   1120 	ifp->if_oerrors++;
   1121 	printf("%s: watchdog timeout\n", device_xname(sc->sc_dev));
   1122 
   1123 	s = splusb();
   1124 	c = &sc->sc_cdata.url_tx_chain[0];
   1125 	usbd_get_xfer_status(c->url_xfer, NULL, NULL, NULL, &stat);
   1126 	url_txeof(c->url_xfer, c, stat);
   1127 
   1128 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1129 		url_start(ifp);
   1130 	splx(s);
   1131 }
   1132 
   1133 Static void
   1134 url_stop_task(struct url_softc *sc)
   1135 {
   1136 	url_stop(GET_IFP(sc), 1);
   1137 }
   1138 
   1139 /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */
   1140 Static void
   1141 url_stop(struct ifnet *ifp, int disable)
   1142 {
   1143 	struct url_softc *sc = ifp->if_softc;
   1144 	usbd_status err;
   1145 	int i;
   1146 
   1147 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
   1148 
   1149 	ifp->if_timer = 0;
   1150 
   1151 	url_reset(sc);
   1152 
   1153 	callout_stop(&sc->sc_stat_ch);
   1154 
   1155 	/* Stop transfers */
   1156 	/* RX endpoint */
   1157 	if (sc->sc_pipe_rx != NULL) {
   1158 		err = usbd_abort_pipe(sc->sc_pipe_rx);
   1159 		if (err)
   1160 			printf("%s: abort rx pipe failed: %s\n",
   1161 			       device_xname(sc->sc_dev), usbd_errstr(err));
   1162 	}
   1163 
   1164 	/* TX endpoint */
   1165 	if (sc->sc_pipe_tx != NULL) {
   1166 		err = usbd_abort_pipe(sc->sc_pipe_tx);
   1167 		if (err)
   1168 			printf("%s: abort tx pipe failed: %s\n",
   1169 			       device_xname(sc->sc_dev), usbd_errstr(err));
   1170 	}
   1171 
   1172 #if 0
   1173 	/* XXX: Interrupt endpoint is not yet supported!! */
   1174 	/* Interrupt endpoint */
   1175 	if (sc->sc_pipe_intr != NULL) {
   1176 		err = usbd_abort_pipe(sc->sc_pipe_intr);
   1177 		if (err)
   1178 			printf("%s: abort intr pipe failed: %s\n",
   1179 			       device_xname(sc->sc_dev), usbd_errstr(err));
   1180 		err = usbd_close_pipe(sc->sc_pipe_intr);
   1181 		if (err)
   1182 			printf("%s: close intr pipe failed: %s\n",
   1183 			       device_xname(sc->sc_dev), usbd_errstr(err));
   1184 		sc->sc_pipe_intr = NULL;
   1185 	}
   1186 #endif
   1187 
   1188 	/* Free RX resources. */
   1189 	for (i = 0; i < URL_RX_LIST_CNT; i++) {
   1190 		if (sc->sc_cdata.url_rx_chain[i].url_mbuf != NULL) {
   1191 			m_freem(sc->sc_cdata.url_rx_chain[i].url_mbuf);
   1192 			sc->sc_cdata.url_rx_chain[i].url_mbuf = NULL;
   1193 		}
   1194 		if (sc->sc_cdata.url_rx_chain[i].url_xfer != NULL) {
   1195 			usbd_destroy_xfer(sc->sc_cdata.url_rx_chain[i].url_xfer);
   1196 			sc->sc_cdata.url_rx_chain[i].url_xfer = NULL;
   1197 		}
   1198 	}
   1199 
   1200 	/* Free TX resources. */
   1201 	for (i = 0; i < URL_TX_LIST_CNT; i++) {
   1202 		if (sc->sc_cdata.url_tx_chain[i].url_mbuf != NULL) {
   1203 			m_freem(sc->sc_cdata.url_tx_chain[i].url_mbuf);
   1204 			sc->sc_cdata.url_tx_chain[i].url_mbuf = NULL;
   1205 		}
   1206 		if (sc->sc_cdata.url_tx_chain[i].url_xfer != NULL) {
   1207 			usbd_destroy_xfer(sc->sc_cdata.url_tx_chain[i].url_xfer);
   1208 			sc->sc_cdata.url_tx_chain[i].url_xfer = NULL;
   1209 		}
   1210 	}
   1211 
   1212 	/* Close pipes */
   1213 	/* RX endpoint */
   1214 	if (sc->sc_pipe_rx != NULL) {
   1215 		err = usbd_close_pipe(sc->sc_pipe_rx);
   1216 		if (err)
   1217 			printf("%s: close rx pipe failed: %s\n",
   1218 			       device_xname(sc->sc_dev), usbd_errstr(err));
   1219 		sc->sc_pipe_rx = NULL;
   1220 	}
   1221 
   1222 	/* TX endpoint */
   1223 	if (sc->sc_pipe_tx != NULL) {
   1224 		err = usbd_close_pipe(sc->sc_pipe_tx);
   1225 		if (err)
   1226 			printf("%s: close tx pipe failed: %s\n",
   1227 			       device_xname(sc->sc_dev), usbd_errstr(err));
   1228 		sc->sc_pipe_tx = NULL;
   1229 	}
   1230 
   1231 	sc->sc_link = 0;
   1232 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1233 }
   1234 
   1235 /* Set media options */
   1236 Static int
   1237 url_ifmedia_change(struct ifnet *ifp)
   1238 {
   1239 	struct url_softc *sc = ifp->if_softc;
   1240 	struct mii_data *mii = GET_MII(sc);
   1241 	int rc;
   1242 
   1243 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
   1244 
   1245 	if (sc->sc_dying)
   1246 		return 0;
   1247 
   1248 	sc->sc_link = 0;
   1249 	if ((rc = mii_mediachg(mii)) == ENXIO)
   1250 		return 0;
   1251 	return rc;
   1252 }
   1253 
   1254 /* Report current media status. */
   1255 Static void
   1256 url_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
   1257 {
   1258 	struct url_softc *sc = ifp->if_softc;
   1259 
   1260 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
   1261 
   1262 	if (sc->sc_dying)
   1263 		return;
   1264 
   1265 	ether_mediastatus(ifp, ifmr);
   1266 }
   1267 
   1268 Static void
   1269 url_tick(void *xsc)
   1270 {
   1271 	struct url_softc *sc = xsc;
   1272 
   1273 	if (sc == NULL)
   1274 		return;
   1275 
   1276 	DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
   1277 			__func__));
   1278 
   1279 	if (sc->sc_dying)
   1280 		return;
   1281 
   1282 	/* Perform periodic stuff in process context */
   1283 	usb_add_task(sc->sc_udev, &sc->sc_tick_task, USB_TASKQ_DRIVER);
   1284 }
   1285 
   1286 Static void
   1287 url_tick_task(void *xsc)
   1288 {
   1289 	struct url_softc *sc = xsc;
   1290 	struct ifnet *ifp;
   1291 	struct mii_data *mii;
   1292 	int s;
   1293 
   1294 	if (sc == NULL)
   1295 		return;
   1296 
   1297 	DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
   1298 			__func__));
   1299 
   1300 	if (sc->sc_dying)
   1301 		return;
   1302 
   1303 	ifp = GET_IFP(sc);
   1304 	mii = GET_MII(sc);
   1305 
   1306 	if (mii == NULL)
   1307 		return;
   1308 
   1309 	s = splnet();
   1310 
   1311 	mii_tick(mii);
   1312 	if (!sc->sc_link) {
   1313 		mii_pollstat(mii);
   1314 		if (mii->mii_media_status & IFM_ACTIVE &&
   1315 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
   1316 			DPRINTF(("%s: %s: got link\n",
   1317 				 device_xname(sc->sc_dev), __func__));
   1318 			sc->sc_link++;
   1319 			if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   1320 				   url_start(ifp);
   1321 		}
   1322 	}
   1323 
   1324 	callout_reset(&sc->sc_stat_ch, hz, url_tick, sc);
   1325 
   1326 	splx(s);
   1327 }
   1328 
   1329 /* Get exclusive access to the MII registers */
   1330 Static void
   1331 url_lock_mii(struct url_softc *sc)
   1332 {
   1333 	DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
   1334 			__func__));
   1335 
   1336 	sc->sc_refcnt++;
   1337 	rw_enter(&sc->sc_mii_rwlock, RW_WRITER);
   1338 }
   1339 
   1340 Static void
   1341 url_unlock_mii(struct url_softc *sc)
   1342 {
   1343 	DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
   1344 		       __func__));
   1345 
   1346 	rw_exit(&sc->sc_mii_rwlock);
   1347 	if (--sc->sc_refcnt < 0)
   1348 		usb_detach_wakeupold(sc->sc_dev);
   1349 }
   1350 
   1351 Static int
   1352 url_int_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val)
   1353 {
   1354 	struct url_softc *sc;
   1355 	uint16_t data;
   1356 	int rv = 0;
   1357 
   1358 	if (dev == NULL)
   1359 		return 0;
   1360 
   1361 	sc = device_private(dev);
   1362 
   1363 	DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n",
   1364 		 device_xname(sc->sc_dev), __func__, phy, reg));
   1365 
   1366 	if (sc->sc_dying) {
   1367 #ifdef DIAGNOSTIC
   1368 		printf("%s: %s: dying\n", device_xname(sc->sc_dev),
   1369 		       __func__);
   1370 #endif
   1371 		return -1;
   1372 	}
   1373 
   1374 	/* XXX: one PHY only for the RTL8150 internal PHY */
   1375 	if (phy != 0) {
   1376 		DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
   1377 			 device_xname(sc->sc_dev), __func__, phy));
   1378 		return -1;
   1379 	}
   1380 
   1381 	url_lock_mii(sc);
   1382 
   1383 	switch (reg) {
   1384 	case MII_BMCR:		/* Control Register */
   1385 		reg = URL_BMCR;
   1386 		break;
   1387 	case MII_BMSR:		/* Status Register */
   1388 		reg = URL_BMSR;
   1389 		break;
   1390 	case MII_PHYIDR1:
   1391 	case MII_PHYIDR2:
   1392 		*val = 0;
   1393 		goto R_DONE;
   1394 		break;
   1395 	case MII_ANAR:		/* Autonegotiation advertisement */
   1396 		reg = URL_ANAR;
   1397 		break;
   1398 	case MII_ANLPAR:	/* Autonegotiation link partner abilities */
   1399 		reg = URL_ANLP;
   1400 		break;
   1401 	case URLPHY_MSR:	/* Media Status Register */
   1402 		reg = URL_MSR;
   1403 		break;
   1404 	default:
   1405 		printf("%s: %s: bad register %04x\n",
   1406 		       device_xname(sc->sc_dev), __func__, reg);
   1407 		rv = -1;
   1408 		goto R_DONE;
   1409 		break;
   1410 	}
   1411 
   1412 	if (reg == URL_MSR)
   1413 		data = url_csr_read_1(sc, reg);
   1414 	else
   1415 		data = url_csr_read_2(sc, reg);
   1416 	*val = data;
   1417 
   1418  R_DONE:
   1419 	DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04hx\n",
   1420 		 device_xname(sc->sc_dev), __func__, phy, reg, *val));
   1421 
   1422 	url_unlock_mii(sc);
   1423 	return rv;
   1424 }
   1425 
   1426 Static int
   1427 url_int_miibus_writereg(device_t dev, int phy, int reg, uint16_t val)
   1428 {
   1429 	struct url_softc *sc;
   1430 	int rv = 0;
   1431 
   1432 	if (dev == NULL)
   1433 		return -1;
   1434 
   1435 	sc = device_private(dev);
   1436 
   1437 	DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x val=0x%04hx\n",
   1438 		 device_xname(sc->sc_dev), __func__, phy, reg, val));
   1439 
   1440 	if (sc->sc_dying) {
   1441 #ifdef DIAGNOSTIC
   1442 		printf("%s: %s: dying\n", device_xname(sc->sc_dev),
   1443 		       __func__);
   1444 #endif
   1445 		return -1;
   1446 	}
   1447 
   1448 	/* XXX: one PHY only for the RTL8150 internal PHY */
   1449 	if (phy != 0) {
   1450 		DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
   1451 			 device_xname(sc->sc_dev), __func__, phy));
   1452 		return -1;
   1453 	}
   1454 
   1455 	url_lock_mii(sc);
   1456 
   1457 	switch (reg) {
   1458 	case MII_BMCR:		/* Control Register */
   1459 		reg = URL_BMCR;
   1460 		break;
   1461 	case MII_BMSR:		/* Status Register */
   1462 		reg = URL_BMSR;
   1463 		break;
   1464 	case MII_PHYIDR1:
   1465 	case MII_PHYIDR2:
   1466 		goto W_DONE;
   1467 		break;
   1468 	case MII_ANAR:		/* Autonegotiation advertisement */
   1469 		reg = URL_ANAR;
   1470 		break;
   1471 	case MII_ANLPAR:	/* Autonegotiation link partner abilities */
   1472 		reg = URL_ANLP;
   1473 		break;
   1474 	case URLPHY_MSR:	/* Media Status Register */
   1475 		reg = URL_MSR;
   1476 		break;
   1477 	default:
   1478 		printf("%s: %s: bad register %04x\n",
   1479 		       device_xname(sc->sc_dev), __func__, reg);
   1480 		rv = -1;
   1481 		goto W_DONE;
   1482 		break;
   1483 	}
   1484 
   1485 	if (reg == URL_MSR)
   1486 		url_csr_write_1(sc, reg, val);
   1487 	else
   1488 		url_csr_write_2(sc, reg, val);
   1489  W_DONE:
   1490 
   1491 	url_unlock_mii(sc);
   1492 	return rv;
   1493 }
   1494 
   1495 Static void
   1496 url_miibus_statchg(struct ifnet *ifp)
   1497 {
   1498 #ifdef URL_DEBUG
   1499 	if (ifp == NULL)
   1500 		return;
   1501 
   1502 	DPRINTF(("%s: %s: enter\n", ifp->if_xname, __func__));
   1503 #endif
   1504 	/* Nothing to do */
   1505 }
   1506 
   1507 #if 0
   1508 /*
   1509  * external PHYs support, but not test.
   1510  */
   1511 Static int
   1512 url_ext_miibus_redreg(device_t dev, int phy, int reg)
   1513 {
   1514 	struct url_softc *sc = device_private(dev);
   1515 	uint16_t val;
   1516 
   1517 	DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x\n",
   1518 		 device_xname(sc->sc_dev), __func__, phy, reg));
   1519 
   1520 	if (sc->sc_dying) {
   1521 #ifdef DIAGNOSTIC
   1522 		printf("%s: %s: dying\n", device_xname(sc->sc_dev),
   1523 		       __func__);
   1524 #endif
   1525 		return 0;
   1526 	}
   1527 
   1528 	url_lock_mii(sc);
   1529 
   1530 	url_csr_write_1(sc, URL_PHYADD, phy & URL_PHYADD_MASK);
   1531 	/*
   1532 	 * RTL8150L will initiate a MII management data transaction
   1533 	 * if PHYCNT_OWN bit is set 1 by software. After transaction,
   1534 	 * this bit is auto cleared by TRL8150L.
   1535 	 */
   1536 	url_csr_write_1(sc, URL_PHYCNT,
   1537 			(reg | URL_PHYCNT_PHYOWN) & ~URL_PHYCNT_RWCR);
   1538 	for (i = 0; i < URL_TIMEOUT; i++) {
   1539 		if ((url_csr_read_1(sc, URL_PHYCNT) & URL_PHYCNT_PHYOWN) == 0)
   1540 			break;
   1541 	}
   1542 	if (i == URL_TIMEOUT) {
   1543 		printf("%s: MII read timed out\n", device_xname(sc->sc_dev));
   1544 	}
   1545 
   1546 	val = url_csr_read_2(sc, URL_PHYDAT);
   1547 
   1548 	DPRINTF(("%s: %s: phy=%d reg=0x%04x => 0x%04x\n",
   1549 		 device_xname(sc->sc_dev), __func__, phy, reg, val));
   1550 
   1551 	url_unlock_mii(sc);
   1552 	return val;
   1553 }
   1554 
   1555 Static void
   1556 url_ext_miibus_writereg(device_t dev, int phy, int reg, int data)
   1557 {
   1558 	struct url_softc *sc = device_private(dev);
   1559 
   1560 	DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n",
   1561 		 device_xname(sc->sc_dev), __func__, phy, reg, data));
   1562 
   1563 	if (sc->sc_dying) {
   1564 #ifdef DIAGNOSTIC
   1565 		printf("%s: %s: dying\n", device_xname(sc->sc_dev),
   1566 		       __func__);
   1567 #endif
   1568 		return;
   1569 	}
   1570 
   1571 	url_lock_mii(sc);
   1572 
   1573 	url_csr_write_2(sc, URL_PHYDAT, data);
   1574 	url_csr_write_1(sc, URL_PHYADD, phy);
   1575 	url_csr_write_1(sc, URL_PHYCNT, reg | URL_PHYCNT_RWCR);	/* Write */
   1576 
   1577 	for (i=0; i < URL_TIMEOUT; i++) {
   1578 		if (url_csr_read_1(sc, URL_PHYCNT) & URL_PHYCNT_PHYOWN)
   1579 			break;
   1580 	}
   1581 
   1582 	if (i == URL_TIMEOUT) {
   1583 		printf("%s: MII write timed out\n",
   1584 		       device_xname(sc->sc_dev));
   1585 	}
   1586 
   1587 	url_unlock_mii(sc);
   1588 	return;
   1589 }
   1590 #endif
   1591