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