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