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