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