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