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