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