xref: /freebsd/sys/dev/usb/net/if_rue.c (revision 8fc257994d0ce2396196d7a06d50d20c8015f4b7)
1 /*-
2  * Copyright (c) 2001-2003, Shunsuke Akiyama <akiyama@FreeBSD.org>.
3  * Copyright (c) 1997, 1998, 1999, 2000 Bill Paul <wpaul@ee.columbia.edu>.
4  * 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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 /*-
28  * Copyright (c) 1997, 1998, 1999, 2000
29  *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  * 1. Redistributions of source code must retain the above copyright
35  *    notice, this list of conditions and the following disclaimer.
36  * 2. Redistributions in binary form must reproduce the above copyright
37  *    notice, this list of conditions and the following disclaimer in the
38  *    documentation and/or other materials provided with the distribution.
39  * 3. All advertising materials mentioning features or use of this software
40  *    must display the following acknowledgement:
41  *	This product includes software developed by Bill Paul.
42  * 4. Neither the name of the author nor the names of any co-contributors
43  *    may be used to endorse or promote products derived from this software
44  *    without specific prior written permission.
45  *
46  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
50  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
56  * THE POSSIBILITY OF SUCH DAMAGE.
57  */
58 
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
61 
62 /*
63  * RealTek RTL8150 USB to fast ethernet controller driver.
64  * Datasheet is available from
65  * ftp://ftp.realtek.com.tw/lancard/data_sheet/8150/.
66  */
67 
68 #include <sys/stdint.h>
69 #include <sys/stddef.h>
70 #include <sys/param.h>
71 #include <sys/queue.h>
72 #include <sys/types.h>
73 #include <sys/systm.h>
74 #include <sys/kernel.h>
75 #include <sys/bus.h>
76 #include <sys/linker_set.h>
77 #include <sys/module.h>
78 #include <sys/lock.h>
79 #include <sys/mutex.h>
80 #include <sys/condvar.h>
81 #include <sys/sysctl.h>
82 #include <sys/sx.h>
83 #include <sys/unistd.h>
84 #include <sys/callout.h>
85 #include <sys/malloc.h>
86 #include <sys/priv.h>
87 
88 #include <dev/usb/usb.h>
89 #include <dev/usb/usbdi.h>
90 #include <dev/usb/usbdi_util.h>
91 #include "usbdevs.h"
92 
93 #define	USB_DEBUG_VAR rue_debug
94 #include <dev/usb/usb_debug.h>
95 #include <dev/usb/usb_process.h>
96 
97 #include <dev/usb/net/usb_ethernet.h>
98 #include <dev/usb/net/if_ruereg.h>
99 
100 #ifdef USB_DEBUG
101 static int rue_debug = 0;
102 
103 SYSCTL_NODE(_hw_usb, OID_AUTO, rue, CTLFLAG_RW, 0, "USB rue");
104 SYSCTL_INT(_hw_usb_rue, OID_AUTO, debug, CTLFLAG_RW,
105     &rue_debug, 0, "Debug level");
106 #endif
107 
108 /*
109  * Various supported device vendors/products.
110  */
111 
112 static const struct usb_device_id rue_devs[] = {
113 	{USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX, 0)},
114 	{USB_VPI(USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_USBKR100, 0)},
115 	{USB_VPI(USB_VENDOR_OQO, USB_PRODUCT_OQO_ETHER01, 0)},
116 };
117 
118 /* prototypes */
119 
120 static device_probe_t rue_probe;
121 static device_attach_t rue_attach;
122 static device_detach_t rue_detach;
123 
124 static miibus_readreg_t rue_miibus_readreg;
125 static miibus_writereg_t rue_miibus_writereg;
126 static miibus_statchg_t rue_miibus_statchg;
127 
128 static usb_callback_t rue_intr_callback;
129 static usb_callback_t rue_bulk_read_callback;
130 static usb_callback_t rue_bulk_write_callback;
131 
132 static uether_fn_t rue_attach_post;
133 static uether_fn_t rue_init;
134 static uether_fn_t rue_stop;
135 static uether_fn_t rue_start;
136 static uether_fn_t rue_tick;
137 static uether_fn_t rue_setmulti;
138 static uether_fn_t rue_setpromisc;
139 
140 static int	rue_read_mem(struct rue_softc *, uint16_t, void *, int);
141 static int	rue_write_mem(struct rue_softc *, uint16_t, void *, int);
142 static uint8_t	rue_csr_read_1(struct rue_softc *, uint16_t);
143 static uint16_t	rue_csr_read_2(struct rue_softc *, uint16_t);
144 static int	rue_csr_write_1(struct rue_softc *, uint16_t, uint8_t);
145 static int	rue_csr_write_2(struct rue_softc *, uint16_t, uint16_t);
146 static int	rue_csr_write_4(struct rue_softc *, int, uint32_t);
147 
148 static void	rue_reset(struct rue_softc *);
149 static int	rue_ifmedia_upd(struct ifnet *);
150 static void	rue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
151 
152 static const struct usb_config rue_config[RUE_N_TRANSFER] = {
153 
154 	[RUE_BULK_DT_WR] = {
155 		.type = UE_BULK,
156 		.endpoint = UE_ADDR_ANY,
157 		.direction = UE_DIR_OUT,
158 		.bufsize = MCLBYTES,
159 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
160 		.callback = rue_bulk_write_callback,
161 		.timeout = 10000,	/* 10 seconds */
162 	},
163 
164 	[RUE_BULK_DT_RD] = {
165 		.type = UE_BULK,
166 		.endpoint = UE_ADDR_ANY,
167 		.direction = UE_DIR_IN,
168 		.bufsize = (MCLBYTES + 4),
169 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
170 		.callback = rue_bulk_read_callback,
171 		.timeout = 0,	/* no timeout */
172 	},
173 
174 	[RUE_INTR_DT_RD] = {
175 		.type = UE_INTERRUPT,
176 		.endpoint = UE_ADDR_ANY,
177 		.direction = UE_DIR_IN,
178 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
179 		.bufsize = 0,	/* use wMaxPacketSize */
180 		.callback = rue_intr_callback,
181 	},
182 };
183 
184 static device_method_t rue_methods[] = {
185 	/* Device interface */
186 	DEVMETHOD(device_probe, rue_probe),
187 	DEVMETHOD(device_attach, rue_attach),
188 	DEVMETHOD(device_detach, rue_detach),
189 
190 	/* Bus interface */
191 	DEVMETHOD(bus_print_child, bus_generic_print_child),
192 	DEVMETHOD(bus_driver_added, bus_generic_driver_added),
193 
194 	/* MII interface */
195 	DEVMETHOD(miibus_readreg, rue_miibus_readreg),
196 	DEVMETHOD(miibus_writereg, rue_miibus_writereg),
197 	DEVMETHOD(miibus_statchg, rue_miibus_statchg),
198 
199 	{0, 0}
200 };
201 
202 static driver_t rue_driver = {
203 	.name = "rue",
204 	.methods = rue_methods,
205 	.size = sizeof(struct rue_softc),
206 };
207 
208 static devclass_t rue_devclass;
209 
210 DRIVER_MODULE(rue, uhub, rue_driver, rue_devclass, NULL, 0);
211 DRIVER_MODULE(miibus, rue, miibus_driver, miibus_devclass, 0, 0);
212 MODULE_DEPEND(rue, uether, 1, 1, 1);
213 MODULE_DEPEND(rue, usb, 1, 1, 1);
214 MODULE_DEPEND(rue, ether, 1, 1, 1);
215 MODULE_DEPEND(rue, miibus, 1, 1, 1);
216 
217 static const struct usb_ether_methods rue_ue_methods = {
218 	.ue_attach_post = rue_attach_post,
219 	.ue_start = rue_start,
220 	.ue_init = rue_init,
221 	.ue_stop = rue_stop,
222 	.ue_tick = rue_tick,
223 	.ue_setmulti = rue_setmulti,
224 	.ue_setpromisc = rue_setpromisc,
225 	.ue_mii_upd = rue_ifmedia_upd,
226 	.ue_mii_sts = rue_ifmedia_sts,
227 };
228 
229 #define	RUE_SETBIT(sc, reg, x) \
230 	rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) | (x))
231 
232 #define	RUE_CLRBIT(sc, reg, x) \
233 	rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) & ~(x))
234 
235 static int
236 rue_read_mem(struct rue_softc *sc, uint16_t addr, void *buf, int len)
237 {
238 	struct usb_device_request req;
239 
240 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
241 	req.bRequest = UR_SET_ADDRESS;
242 	USETW(req.wValue, addr);
243 	USETW(req.wIndex, 0);
244 	USETW(req.wLength, len);
245 
246 	return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
247 }
248 
249 static int
250 rue_write_mem(struct rue_softc *sc, uint16_t addr, void *buf, int len)
251 {
252 	struct usb_device_request req;
253 
254 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
255 	req.bRequest = UR_SET_ADDRESS;
256 	USETW(req.wValue, addr);
257 	USETW(req.wIndex, 0);
258 	USETW(req.wLength, len);
259 
260 	return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
261 }
262 
263 static uint8_t
264 rue_csr_read_1(struct rue_softc *sc, uint16_t reg)
265 {
266 	uint8_t val;
267 
268 	rue_read_mem(sc, reg, &val, 1);
269 	return (val);
270 }
271 
272 static uint16_t
273 rue_csr_read_2(struct rue_softc *sc, uint16_t reg)
274 {
275 	uint8_t val[2];
276 
277 	rue_read_mem(sc, reg, &val, 2);
278 	return (UGETW(val));
279 }
280 
281 static int
282 rue_csr_write_1(struct rue_softc *sc, uint16_t reg, uint8_t val)
283 {
284 	return (rue_write_mem(sc, reg, &val, 1));
285 }
286 
287 static int
288 rue_csr_write_2(struct rue_softc *sc, uint16_t reg, uint16_t val)
289 {
290 	uint8_t temp[2];
291 
292 	USETW(temp, val);
293 	return (rue_write_mem(sc, reg, &temp, 2));
294 }
295 
296 static int
297 rue_csr_write_4(struct rue_softc *sc, int reg, uint32_t val)
298 {
299 	uint8_t temp[4];
300 
301 	USETDW(temp, val);
302 	return (rue_write_mem(sc, reg, &temp, 4));
303 }
304 
305 static int
306 rue_miibus_readreg(device_t dev, int phy, int reg)
307 {
308 	struct rue_softc *sc = device_get_softc(dev);
309 	uint16_t rval;
310 	uint16_t ruereg;
311 	int locked;
312 
313 	if (phy != 0)		/* RTL8150 supports PHY == 0, only */
314 		return (0);
315 
316 	locked = mtx_owned(&sc->sc_mtx);
317 	if (!locked)
318 		RUE_LOCK(sc);
319 
320 	switch (reg) {
321 	case MII_BMCR:
322 		ruereg = RUE_BMCR;
323 		break;
324 	case MII_BMSR:
325 		ruereg = RUE_BMSR;
326 		break;
327 	case MII_ANAR:
328 		ruereg = RUE_ANAR;
329 		break;
330 	case MII_ANER:
331 		ruereg = RUE_AER;
332 		break;
333 	case MII_ANLPAR:
334 		ruereg = RUE_ANLP;
335 		break;
336 	case MII_PHYIDR1:
337 	case MII_PHYIDR2:
338 		rval = 0;
339 		goto done;
340 	default:
341 		if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) {
342 			rval = rue_csr_read_1(sc, reg);
343 			goto done;
344 		}
345 		device_printf(sc->sc_ue.ue_dev, "bad phy register\n");
346 		rval = 0;
347 		goto done;
348 	}
349 
350 	rval = rue_csr_read_2(sc, ruereg);
351 done:
352 	if (!locked)
353 		RUE_UNLOCK(sc);
354 	return (rval);
355 }
356 
357 static int
358 rue_miibus_writereg(device_t dev, int phy, int reg, int data)
359 {
360 	struct rue_softc *sc = device_get_softc(dev);
361 	uint16_t ruereg;
362 	int locked;
363 
364 	if (phy != 0)		/* RTL8150 supports PHY == 0, only */
365 		return (0);
366 
367 	locked = mtx_owned(&sc->sc_mtx);
368 	if (!locked)
369 		RUE_LOCK(sc);
370 
371 	switch (reg) {
372 	case MII_BMCR:
373 		ruereg = RUE_BMCR;
374 		break;
375 	case MII_BMSR:
376 		ruereg = RUE_BMSR;
377 		break;
378 	case MII_ANAR:
379 		ruereg = RUE_ANAR;
380 		break;
381 	case MII_ANER:
382 		ruereg = RUE_AER;
383 		break;
384 	case MII_ANLPAR:
385 		ruereg = RUE_ANLP;
386 		break;
387 	case MII_PHYIDR1:
388 	case MII_PHYIDR2:
389 		goto done;
390 	default:
391 		if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) {
392 			rue_csr_write_1(sc, reg, data);
393 			goto done;
394 		}
395 		device_printf(sc->sc_ue.ue_dev, " bad phy register\n");
396 		goto done;
397 	}
398 	rue_csr_write_2(sc, ruereg, data);
399 done:
400 	if (!locked)
401 		RUE_UNLOCK(sc);
402 	return (0);
403 }
404 
405 static void
406 rue_miibus_statchg(device_t dev)
407 {
408 	/*
409 	 * When the code below is enabled the card starts doing weird
410 	 * things after link going from UP to DOWN and back UP.
411 	 *
412 	 * Looks like some of register writes below messes up PHY
413 	 * interface.
414 	 *
415 	 * No visible regressions were found after commenting this code
416 	 * out, so that disable it for good.
417 	 */
418 #if 0
419 	struct rue_softc *sc = device_get_softc(dev);
420 	struct mii_data *mii = GET_MII(sc);
421 	uint16_t bmcr;
422 	int locked;
423 
424 	locked = mtx_owned(&sc->sc_mtx);
425 	if (!locked)
426 		RUE_LOCK(sc);
427 
428 	RUE_CLRBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE));
429 
430 	bmcr = rue_csr_read_2(sc, RUE_BMCR);
431 
432 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX)
433 		bmcr |= RUE_BMCR_SPD_SET;
434 	else
435 		bmcr &= ~RUE_BMCR_SPD_SET;
436 
437 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
438 		bmcr |= RUE_BMCR_DUPLEX;
439 	else
440 		bmcr &= ~RUE_BMCR_DUPLEX;
441 
442 	rue_csr_write_2(sc, RUE_BMCR, bmcr);
443 
444 	RUE_SETBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE));
445 
446 	if (!locked)
447 		RUE_UNLOCK(sc);
448 #endif
449 }
450 
451 static void
452 rue_setpromisc(struct usb_ether *ue)
453 {
454 	struct rue_softc *sc = uether_getsc(ue);
455 	struct ifnet *ifp = uether_getifp(ue);
456 
457 	RUE_LOCK_ASSERT(sc, MA_OWNED);
458 
459 	/* If we want promiscuous mode, set the allframes bit. */
460 	if (ifp->if_flags & IFF_PROMISC)
461 		RUE_SETBIT(sc, RUE_RCR, RUE_RCR_AAP);
462 	else
463 		RUE_CLRBIT(sc, RUE_RCR, RUE_RCR_AAP);
464 }
465 
466 /*
467  * Program the 64-bit multicast hash filter.
468  */
469 static void
470 rue_setmulti(struct usb_ether *ue)
471 {
472 	struct rue_softc *sc = uether_getsc(ue);
473 	struct ifnet *ifp = uether_getifp(ue);
474 	uint16_t rxcfg;
475 	int h = 0;
476 	uint32_t hashes[2] = { 0, 0 };
477 	struct ifmultiaddr *ifma;
478 	int mcnt = 0;
479 
480 	RUE_LOCK_ASSERT(sc, MA_OWNED);
481 
482 	rxcfg = rue_csr_read_2(sc, RUE_RCR);
483 
484 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
485 		rxcfg |= (RUE_RCR_AAM | RUE_RCR_AAP);
486 		rxcfg &= ~RUE_RCR_AM;
487 		rue_csr_write_2(sc, RUE_RCR, rxcfg);
488 		rue_csr_write_4(sc, RUE_MAR0, 0xFFFFFFFF);
489 		rue_csr_write_4(sc, RUE_MAR4, 0xFFFFFFFF);
490 		return;
491 	}
492 
493 	/* first, zot all the existing hash bits */
494 	rue_csr_write_4(sc, RUE_MAR0, 0);
495 	rue_csr_write_4(sc, RUE_MAR4, 0);
496 
497 	/* now program new ones */
498 	if_maddr_rlock(ifp);
499 	TAILQ_FOREACH (ifma, &ifp->if_multiaddrs, ifma_link)
500 	{
501 		if (ifma->ifma_addr->sa_family != AF_LINK)
502 			continue;
503 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
504 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
505 		if (h < 32)
506 			hashes[0] |= (1 << h);
507 		else
508 			hashes[1] |= (1 << (h - 32));
509 		mcnt++;
510 	}
511 	if_maddr_runlock(ifp);
512 
513 	if (mcnt)
514 		rxcfg |= RUE_RCR_AM;
515 	else
516 		rxcfg &= ~RUE_RCR_AM;
517 
518 	rxcfg &= ~(RUE_RCR_AAM | RUE_RCR_AAP);
519 
520 	rue_csr_write_2(sc, RUE_RCR, rxcfg);
521 	rue_csr_write_4(sc, RUE_MAR0, hashes[0]);
522 	rue_csr_write_4(sc, RUE_MAR4, hashes[1]);
523 }
524 
525 static void
526 rue_reset(struct rue_softc *sc)
527 {
528 	int i;
529 
530 	rue_csr_write_1(sc, RUE_CR, RUE_CR_SOFT_RST);
531 
532 	for (i = 0; i != RUE_TIMEOUT; i++) {
533 		if (uether_pause(&sc->sc_ue, hz / 1000))
534 			break;
535 		if (!(rue_csr_read_1(sc, RUE_CR) & RUE_CR_SOFT_RST))
536 			break;
537 	}
538 	if (i == RUE_TIMEOUT)
539 		device_printf(sc->sc_ue.ue_dev, "reset never completed\n");
540 
541 	uether_pause(&sc->sc_ue, hz / 100);
542 }
543 
544 static void
545 rue_attach_post(struct usb_ether *ue)
546 {
547 	struct rue_softc *sc = uether_getsc(ue);
548 
549 	/* reset the adapter */
550 	rue_reset(sc);
551 
552 	/* get station address from the EEPROM */
553 	rue_read_mem(sc, RUE_EEPROM_IDR0, ue->ue_eaddr, ETHER_ADDR_LEN);
554 }
555 
556 /*
557  * Probe for a RTL8150 chip.
558  */
559 static int
560 rue_probe(device_t dev)
561 {
562 	struct usb_attach_arg *uaa = device_get_ivars(dev);
563 
564 	if (uaa->usb_mode != USB_MODE_HOST)
565 		return (ENXIO);
566 	if (uaa->info.bConfigIndex != RUE_CONFIG_IDX)
567 		return (ENXIO);
568 	if (uaa->info.bIfaceIndex != RUE_IFACE_IDX)
569 		return (ENXIO);
570 
571 	return (usbd_lookup_id_by_uaa(rue_devs, sizeof(rue_devs), uaa));
572 }
573 
574 /*
575  * Attach the interface. Allocate softc structures, do ifmedia
576  * setup and ethernet/BPF attach.
577  */
578 static int
579 rue_attach(device_t dev)
580 {
581 	struct usb_attach_arg *uaa = device_get_ivars(dev);
582 	struct rue_softc *sc = device_get_softc(dev);
583 	struct usb_ether *ue = &sc->sc_ue;
584 	uint8_t iface_index;
585 	int error;
586 
587 	device_set_usb_desc(dev);
588 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
589 
590 	iface_index = RUE_IFACE_IDX;
591 	error = usbd_transfer_setup(uaa->device, &iface_index,
592 	    sc->sc_xfer, rue_config, RUE_N_TRANSFER,
593 	    sc, &sc->sc_mtx);
594 	if (error) {
595 		device_printf(dev, "allocating USB transfers failed\n");
596 		goto detach;
597 	}
598 
599 	ue->ue_sc = sc;
600 	ue->ue_dev = dev;
601 	ue->ue_udev = uaa->device;
602 	ue->ue_mtx = &sc->sc_mtx;
603 	ue->ue_methods = &rue_ue_methods;
604 
605 	error = uether_ifattach(ue);
606 	if (error) {
607 		device_printf(dev, "could not attach interface\n");
608 		goto detach;
609 	}
610 	return (0);			/* success */
611 
612 detach:
613 	rue_detach(dev);
614 	return (ENXIO);			/* failure */
615 }
616 
617 static int
618 rue_detach(device_t dev)
619 {
620 	struct rue_softc *sc = device_get_softc(dev);
621 	struct usb_ether *ue = &sc->sc_ue;
622 
623 	usbd_transfer_unsetup(sc->sc_xfer, RUE_N_TRANSFER);
624 	uether_ifdetach(ue);
625 	mtx_destroy(&sc->sc_mtx);
626 
627 	return (0);
628 }
629 
630 static void
631 rue_intr_callback(struct usb_xfer *xfer, usb_error_t error)
632 {
633 	struct rue_softc *sc = usbd_xfer_softc(xfer);
634 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
635 	struct rue_intrpkt pkt;
636 	struct usb_page_cache *pc;
637 	int actlen;
638 
639 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
640 
641 	switch (USB_GET_STATE(xfer)) {
642 	case USB_ST_TRANSFERRED:
643 
644 		if (ifp && (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
645 		    actlen >= sizeof(pkt)) {
646 
647 			pc = usbd_xfer_get_frame(xfer, 0);
648 			usbd_copy_out(pc, 0, &pkt, sizeof(pkt));
649 
650 			ifp->if_ierrors += pkt.rue_rxlost_cnt;
651 			ifp->if_ierrors += pkt.rue_crcerr_cnt;
652 			ifp->if_collisions += pkt.rue_col_cnt;
653 		}
654 		/* FALLTHROUGH */
655 	case USB_ST_SETUP:
656 tr_setup:
657 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
658 		usbd_transfer_submit(xfer);
659 		return;
660 
661 	default:			/* Error */
662 		if (error != USB_ERR_CANCELLED) {
663 			/* try to clear stall first */
664 			usbd_xfer_set_stall(xfer);
665 			goto tr_setup;
666 		}
667 		return;
668 	}
669 }
670 
671 static void
672 rue_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
673 {
674 	struct rue_softc *sc = usbd_xfer_softc(xfer);
675 	struct usb_ether *ue = &sc->sc_ue;
676 	struct ifnet *ifp = uether_getifp(ue);
677 	struct usb_page_cache *pc;
678 	uint16_t status;
679 	int actlen;
680 
681 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
682 
683 	switch (USB_GET_STATE(xfer)) {
684 	case USB_ST_TRANSFERRED:
685 
686 		if (actlen < 4) {
687 			ifp->if_ierrors++;
688 			goto tr_setup;
689 		}
690 		pc = usbd_xfer_get_frame(xfer, 0);
691 		usbd_copy_out(pc, actlen - 4, &status, sizeof(status));
692 		actlen -= 4;
693 
694 		/* check recieve packet was valid or not */
695 		status = le16toh(status);
696 		if ((status & RUE_RXSTAT_VALID) == 0) {
697 			ifp->if_ierrors++;
698 			goto tr_setup;
699 		}
700 		uether_rxbuf(ue, pc, 0, actlen);
701 		/* FALLTHROUGH */
702 	case USB_ST_SETUP:
703 tr_setup:
704 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
705 		usbd_transfer_submit(xfer);
706 		uether_rxflush(ue);
707 		return;
708 
709 	default:			/* Error */
710 		DPRINTF("bulk read error, %s\n",
711 		    usbd_errstr(error));
712 
713 		if (error != USB_ERR_CANCELLED) {
714 			/* try to clear stall first */
715 			usbd_xfer_set_stall(xfer);
716 			goto tr_setup;
717 		}
718 		return;
719 	}
720 }
721 
722 static void
723 rue_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
724 {
725 	struct rue_softc *sc = usbd_xfer_softc(xfer);
726 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
727 	struct usb_page_cache *pc;
728 	struct mbuf *m;
729 	int temp_len;
730 
731 	switch (USB_GET_STATE(xfer)) {
732 	case USB_ST_TRANSFERRED:
733 		DPRINTFN(11, "transfer complete\n");
734 		ifp->if_opackets++;
735 
736 		/* FALLTHROUGH */
737 	case USB_ST_SETUP:
738 tr_setup:
739 		if ((sc->sc_flags & RUE_FLAG_LINK) == 0) {
740 			/*
741 			 * don't send anything if there is no link !
742 			 */
743 			return;
744 		}
745 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
746 
747 		if (m == NULL)
748 			return;
749 		if (m->m_pkthdr.len > MCLBYTES)
750 			m->m_pkthdr.len = MCLBYTES;
751 		temp_len = m->m_pkthdr.len;
752 
753 		pc = usbd_xfer_get_frame(xfer, 0);
754 		usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
755 
756 		/*
757 		 * This is an undocumented behavior.
758 		 * RTL8150 chip doesn't send frame length smaller than
759 		 * RUE_MIN_FRAMELEN (60) byte packet.
760 		 */
761 		if (temp_len < RUE_MIN_FRAMELEN) {
762 			usbd_frame_zero(pc, temp_len,
763 			    RUE_MIN_FRAMELEN - temp_len);
764 			temp_len = RUE_MIN_FRAMELEN;
765 		}
766 		usbd_xfer_set_frame_len(xfer, 0, temp_len);
767 
768 		/*
769 		 * if there's a BPF listener, bounce a copy
770 		 * of this frame to him:
771 		 */
772 		BPF_MTAP(ifp, m);
773 
774 		m_freem(m);
775 
776 		usbd_transfer_submit(xfer);
777 
778 		return;
779 
780 	default:			/* Error */
781 		DPRINTFN(11, "transfer error, %s\n",
782 		    usbd_errstr(error));
783 
784 		ifp->if_oerrors++;
785 
786 		if (error != USB_ERR_CANCELLED) {
787 			/* try to clear stall first */
788 			usbd_xfer_set_stall(xfer);
789 			goto tr_setup;
790 		}
791 		return;
792 	}
793 }
794 
795 static void
796 rue_tick(struct usb_ether *ue)
797 {
798 	struct rue_softc *sc = uether_getsc(ue);
799 	struct mii_data *mii = GET_MII(sc);
800 
801 	RUE_LOCK_ASSERT(sc, MA_OWNED);
802 
803 	mii_tick(mii);
804 	if ((sc->sc_flags & RUE_FLAG_LINK) == 0
805 	    && mii->mii_media_status & IFM_ACTIVE &&
806 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
807 		sc->sc_flags |= RUE_FLAG_LINK;
808 		rue_start(ue);
809 	}
810 }
811 
812 static void
813 rue_start(struct usb_ether *ue)
814 {
815 	struct rue_softc *sc = uether_getsc(ue);
816 
817 	/*
818 	 * start the USB transfers, if not already started:
819 	 */
820 	usbd_transfer_start(sc->sc_xfer[RUE_INTR_DT_RD]);
821 	usbd_transfer_start(sc->sc_xfer[RUE_BULK_DT_RD]);
822 	usbd_transfer_start(sc->sc_xfer[RUE_BULK_DT_WR]);
823 }
824 
825 static void
826 rue_init(struct usb_ether *ue)
827 {
828 	struct rue_softc *sc = uether_getsc(ue);
829 	struct ifnet *ifp = uether_getifp(ue);
830 
831 	RUE_LOCK_ASSERT(sc, MA_OWNED);
832 
833 	/*
834 	 * Cancel pending I/O
835 	 */
836 	rue_reset(sc);
837 
838 	/* Set MAC address */
839 	rue_write_mem(sc, RUE_IDR0, IF_LLADDR(ifp), ETHER_ADDR_LEN);
840 
841 	rue_stop(ue);
842 
843 	/*
844 	 * Set the initial TX and RX configuration.
845 	 */
846 	rue_csr_write_1(sc, RUE_TCR, RUE_TCR_CONFIG);
847 	rue_csr_write_2(sc, RUE_RCR, RUE_RCR_CONFIG|RUE_RCR_AB);
848 
849 	/* Load the multicast filter */
850 	rue_setpromisc(ue);
851 	/* Load the multicast filter. */
852 	rue_setmulti(ue);
853 
854 	/* Enable RX and TX */
855 	rue_csr_write_1(sc, RUE_CR, (RUE_CR_TE | RUE_CR_RE | RUE_CR_EP3CLREN));
856 
857 	usbd_xfer_set_stall(sc->sc_xfer[RUE_BULK_DT_WR]);
858 
859 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
860 	rue_start(ue);
861 }
862 
863 /*
864  * Set media options.
865  */
866 static int
867 rue_ifmedia_upd(struct ifnet *ifp)
868 {
869 	struct rue_softc *sc = ifp->if_softc;
870 	struct mii_data *mii = GET_MII(sc);
871 
872 	RUE_LOCK_ASSERT(sc, MA_OWNED);
873 
874         sc->sc_flags &= ~RUE_FLAG_LINK;
875 	if (mii->mii_instance) {
876 		struct mii_softc *miisc;
877 
878 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
879 			mii_phy_reset(miisc);
880 	}
881 	mii_mediachg(mii);
882 	return (0);
883 }
884 
885 /*
886  * Report current media status.
887  */
888 static void
889 rue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
890 {
891 	struct rue_softc *sc = ifp->if_softc;
892 	struct mii_data *mii = GET_MII(sc);
893 
894 	RUE_LOCK(sc);
895 	mii_pollstat(mii);
896 	RUE_UNLOCK(sc);
897 	ifmr->ifm_active = mii->mii_media_active;
898 	ifmr->ifm_status = mii->mii_media_status;
899 }
900 
901 static void
902 rue_stop(struct usb_ether *ue)
903 {
904 	struct rue_softc *sc = uether_getsc(ue);
905 	struct ifnet *ifp = uether_getifp(ue);
906 
907 	RUE_LOCK_ASSERT(sc, MA_OWNED);
908 
909 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
910 	sc->sc_flags &= ~RUE_FLAG_LINK;
911 
912 	/*
913 	 * stop all the transfers, if not already stopped:
914 	 */
915 	usbd_transfer_stop(sc->sc_xfer[RUE_BULK_DT_WR]);
916 	usbd_transfer_stop(sc->sc_xfer[RUE_BULK_DT_RD]);
917 	usbd_transfer_stop(sc->sc_xfer[RUE_INTR_DT_RD]);
918 
919 	rue_csr_write_1(sc, RUE_CR, 0x00);
920 
921 	rue_reset(sc);
922 }
923