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