2 * Copyright (c) 2001-2003, Shunsuke Akiyama <akiyama@FreeBSD.org>.
3 * Copyright (c) 1997, 1998, 1999, 2000 Bill Paul <wpaul@ee.columbia.edu>.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 * Copyright (c) 1997, 1998, 1999, 2000
29 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
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.
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.
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
63 * RealTek RTL8150 USB to fast ethernet controller driver.
64 * Datasheet is available from
65 * ftp://ftp.realtek.com.tw/lancard/data_sheet/8150/.
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/sockio.h>
72 #include <sys/malloc.h>
73 #include <sys/kernel.h>
74 #include <sys/module.h>
75 #include <sys/socket.h>
76 #include <sys/sysctl.h>
79 #include <net/if_arp.h>
80 #include <net/ethernet.h>
81 #include <net/if_dl.h>
82 #include <net/if_media.h>
83 #include <net/if_types.h>
88 #include <machine/bus.h>
90 #include <dev/usb/usb.h>
91 #include <dev/usb/usbdi.h>
92 #include <dev/usb/usbdi_util.h>
93 #include <dev/usb/usbdivar.h>
95 #include <dev/usb/usb_ethersubr.h>
97 #include <dev/mii/mii.h>
98 #include <dev/mii/miivar.h>
100 #include <dev/usb/if_ruereg.h>
102 /* "device miibus" required. See GENERIC if you get errors here. */
103 #include "miibus_if.h"
106 static int ruedebug = 0;
107 SYSCTL_NODE(_hw_usb, OID_AUTO, rue, CTLFLAG_RW, 0, "USB rue");
108 SYSCTL_INT(_hw_usb_rue, OID_AUTO, debug, CTLFLAG_RW,
109 &ruedebug, 0, "rue debug level");
111 #define DPRINTFN(n, x) do { \
112 if (ruedebug > (n)) \
116 #define DPRINTFN(n, x)
118 #define DPRINTF(x) DPRINTFN(0, x)
121 * Various supported device vendors/products.
124 static struct rue_type rue_devs[] = {
125 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX },
126 { USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_USBKR100 },
130 static device_probe_t rue_match;
131 static device_attach_t rue_attach;
132 static device_detach_t rue_detach;
133 static device_shutdown_t rue_shutdown;
134 static miibus_readreg_t rue_miibus_readreg;
135 static miibus_writereg_t rue_miibus_writereg;
136 static miibus_statchg_t rue_miibus_statchg;
138 static int rue_encap(struct rue_softc *, struct mbuf *, int);
140 static void rue_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
142 static void rue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
143 static void rue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
144 static void rue_tick(void *);
145 static void rue_tick_task(void *);
146 static void rue_rxstart(struct ifnet *);
147 static void rue_start(struct ifnet *);
148 static int rue_ioctl(struct ifnet *, u_long, caddr_t);
149 static void rue_init(void *);
150 static void rue_stop(struct rue_softc *);
151 static void rue_watchdog(struct ifnet *);
152 static int rue_ifmedia_upd(struct ifnet *);
153 static void rue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
155 static void rue_setmulti(struct rue_softc *);
156 static void rue_reset(struct rue_softc *);
158 static int rue_read_mem(struct rue_softc *, u_int16_t, void *, u_int16_t);
159 static int rue_write_mem(struct rue_softc *, u_int16_t, void *, u_int16_t);
160 static int rue_csr_read_1(struct rue_softc *, int);
161 static int rue_csr_write_1(struct rue_softc *, int, u_int8_t);
162 static int rue_csr_read_2(struct rue_softc *, int);
163 static int rue_csr_write_2(struct rue_softc *, int, u_int16_t);
164 static int rue_csr_write_4(struct rue_softc *, int, u_int32_t);
166 static device_method_t rue_methods[] = {
167 /* Device interface */
168 DEVMETHOD(device_probe, rue_match),
169 DEVMETHOD(device_attach, rue_attach),
170 DEVMETHOD(device_detach, rue_detach),
171 DEVMETHOD(device_shutdown, rue_shutdown),
174 DEVMETHOD(bus_print_child, bus_generic_print_child),
175 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
178 DEVMETHOD(miibus_readreg, rue_miibus_readreg),
179 DEVMETHOD(miibus_writereg, rue_miibus_writereg),
180 DEVMETHOD(miibus_statchg, rue_miibus_statchg),
185 static driver_t rue_driver = {
188 sizeof(struct rue_softc)
191 static devclass_t rue_devclass;
193 DRIVER_MODULE(rue, uhub, rue_driver, rue_devclass, usbd_driver_load, 0);
194 DRIVER_MODULE(miibus, rue, miibus_driver, miibus_devclass, 0, 0);
195 MODULE_DEPEND(rue, usb, 1, 1, 1);
196 MODULE_DEPEND(rue, ether, 1, 1, 1);
197 MODULE_DEPEND(rue, miibus, 1, 1, 1);
199 #define RUE_SETBIT(sc, reg, x) \
200 rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) | (x))
202 #define RUE_CLRBIT(sc, reg, x) \
203 rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) & ~(x))
205 #define RUE_SETBIT_2(sc, reg, x) \
206 rue_csr_write_2(sc, reg, rue_csr_read_2(sc, reg) | (x))
208 #define RUE_CLRBIT_2(sc, reg, x) \
209 rue_csr_write_2(sc, reg, rue_csr_read_2(sc, reg) & ~(x))
212 rue_read_mem(struct rue_softc *sc, u_int16_t addr, void *buf, u_int16_t len)
214 usb_device_request_t req;
222 req.bmRequestType = UT_READ_VENDOR_DEVICE;
223 req.bRequest = UR_SET_ADDRESS;
224 USETW(req.wValue, addr);
225 USETW(req.wIndex, 0);
226 USETW(req.wLength, len);
228 err = usbd_do_request(sc->rue_udev, &req, buf);
233 device_printf(sc->rue_dev, "control pipe read failed: %s\n",
242 rue_write_mem(struct rue_softc *sc, u_int16_t addr, void *buf, u_int16_t len)
244 usb_device_request_t req;
252 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
253 req.bRequest = UR_SET_ADDRESS;
254 USETW(req.wValue, addr);
255 USETW(req.wIndex, 0);
256 USETW(req.wLength, len);
258 err = usbd_do_request(sc->rue_udev, &req, buf);
263 device_printf(sc->rue_dev, "control pipe write failed: %s\n",
272 rue_csr_read_1(struct rue_softc *sc, int reg)
277 err = rue_read_mem(sc, reg, &val, 1);
286 rue_csr_read_2(struct rue_softc *sc, int reg)
293 err = rue_read_mem(sc, reg, &w, 2);
303 rue_csr_write_1(struct rue_softc *sc, int reg, u_int8_t val)
307 err = rue_write_mem(sc, reg, &val, 1);
316 rue_csr_write_2(struct rue_softc *sc, int reg, u_int16_t val)
322 err = rue_write_mem(sc, reg, &w, 2);
331 rue_csr_write_4(struct rue_softc *sc, int reg, u_int32_t val)
337 err = rue_write_mem(sc, reg, &dw, 4);
346 rue_miibus_readreg(device_t dev, int phy, int reg)
348 struct rue_softc *sc = device_get_softc(dev);
352 if (phy != 0) /* RTL8150 supports PHY == 0, only */
376 if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) {
377 rval = rue_csr_read_1(sc, reg);
380 device_printf(sc->rue_dev, "bad phy register\n");
384 rval = rue_csr_read_2(sc, ruereg);
390 rue_miibus_writereg(device_t dev, int phy, int reg, int data)
392 struct rue_softc *sc = device_get_softc(dev);
395 if (phy != 0) /* RTL8150 supports PHY == 0, only */
419 if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) {
420 rue_csr_write_1(sc, reg, data);
423 device_printf(sc->rue_dev, "bad phy register\n");
426 rue_csr_write_2(sc, ruereg, data);
432 rue_miibus_statchg(device_t dev)
435 * When the code below is enabled the card starts doing weird
436 * things after link going from UP to DOWN and back UP.
438 * Looks like some of register writes below messes up PHY
441 * No visible regressions were found after commenting this code
442 * out, so that disable it for good.
445 struct rue_softc *sc = device_get_softc(dev);
446 struct mii_data *mii = GET_MII(sc);
449 RUE_CLRBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE));
451 bmcr = rue_csr_read_2(sc, RUE_BMCR);
453 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX)
454 bmcr |= RUE_BMCR_SPD_SET;
456 bmcr &= ~RUE_BMCR_SPD_SET;
458 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
459 bmcr |= RUE_BMCR_DUPLEX;
461 bmcr &= ~RUE_BMCR_DUPLEX;
463 rue_csr_write_2(sc, RUE_BMCR, bmcr);
465 RUE_SETBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE));
470 * Program the 64-bit multicast hash filter.
474 rue_setmulti(struct rue_softc *sc)
478 u_int32_t hashes[2] = { 0, 0 };
479 struct ifmultiaddr *ifma;
485 rxcfg = rue_csr_read_2(sc, RUE_RCR);
487 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
488 rxcfg |= (RUE_RCR_AAM | RUE_RCR_AAP);
489 rxcfg &= ~RUE_RCR_AM;
490 rue_csr_write_2(sc, RUE_RCR, rxcfg);
491 rue_csr_write_4(sc, RUE_MAR0, 0xFFFFFFFF);
492 rue_csr_write_4(sc, RUE_MAR4, 0xFFFFFFFF);
496 /* first, zot all the existing hash bits */
497 rue_csr_write_4(sc, RUE_MAR0, 0);
498 rue_csr_write_4(sc, RUE_MAR4, 0);
500 /* now program new ones */
502 TAILQ_FOREACH (ifma, &ifp->if_multiaddrs, ifma_link)
504 if (ifma->ifma_addr->sa_family != AF_LINK)
506 h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
507 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
509 hashes[0] |= (1 << h);
511 hashes[1] |= (1 << (h - 32));
519 rxcfg &= ~RUE_RCR_AM;
521 rxcfg &= ~(RUE_RCR_AAM | RUE_RCR_AAP);
523 rue_csr_write_2(sc, RUE_RCR, rxcfg);
524 rue_csr_write_4(sc, RUE_MAR0, hashes[0]);
525 rue_csr_write_4(sc, RUE_MAR4, hashes[1]);
529 rue_reset(struct rue_softc *sc)
533 rue_csr_write_1(sc, RUE_CR, RUE_CR_SOFT_RST);
535 for (i = 0; i < RUE_TIMEOUT; i++) {
537 if (!(rue_csr_read_1(sc, RUE_CR) & RUE_CR_SOFT_RST))
540 if (i == RUE_TIMEOUT)
541 device_printf(sc->rue_dev, "reset never completed!\n");
547 * Probe for a RTL8150 chip.
551 rue_match(device_t self)
553 struct usb_attach_arg *uaa = device_get_ivars(self);
556 if (uaa->iface == NULL)
557 return (UMATCH_NONE);
561 if (uaa->vendor == t->rue_vid &&
562 uaa->product == t->rue_did) {
563 return (UMATCH_VENDOR_PRODUCT);
568 return (UMATCH_NONE);
572 * Attach the interface. Allocate softc structures, do ifmedia
573 * setup and ethernet/BPF attach.
577 rue_attach(device_t self)
579 struct rue_softc *sc = device_get_softc(self);
580 struct usb_attach_arg *uaa = device_get_ivars(self);
581 u_char eaddr[ETHER_ADDR_LEN];
583 usbd_interface_handle iface;
585 usb_interface_descriptor_t *id;
586 usb_endpoint_descriptor_t *ed;
591 sc->rue_udev = uaa->device;
593 if (usbd_set_config_no(sc->rue_udev, RUE_CONFIG_NO, 0)) {
594 device_printf(sc->rue_dev, "getting interface handle failed\n");
598 usb_init_task(&sc->rue_tick_task, rue_tick_task, sc);
600 err = usbd_device2interface_handle(uaa->device, RUE_IFACE_IDX, &iface);
602 device_printf(sc->rue_dev, "getting interface handle failed\n");
606 sc->rue_iface = iface;
610 if (uaa->vendor == t->rue_vid &&
611 uaa->product == t->rue_did) {
618 id = usbd_get_interface_descriptor(sc->rue_iface);
621 for (i = 0; i < id->bNumEndpoints; i++) {
622 ed = usbd_interface2endpoint_descriptor(iface, i);
624 device_printf(sc->rue_dev, "couldn't get ep %d\n", i);
627 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
628 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
629 sc->rue_ed[RUE_ENDPT_RX] = ed->bEndpointAddress;
630 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
631 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
632 sc->rue_ed[RUE_ENDPT_TX] = ed->bEndpointAddress;
633 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
634 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
635 sc->rue_ed[RUE_ENDPT_INTR] = ed->bEndpointAddress;
639 mtx_init(&sc->rue_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
640 MTX_DEF | MTX_RECURSE);
643 /* Reset the adapter */
646 /* Get station address from the EEPROM */
647 err = rue_read_mem(sc, RUE_EEPROM_IDR0,
648 (caddr_t)&eaddr, ETHER_ADDR_LEN);
650 device_printf(sc->rue_dev, "couldn't get station address\n");
654 ifp = sc->rue_ifp = if_alloc(IFT_ETHER);
656 device_printf(sc->rue_dev, "can not if_alloc()\n");
660 if_initname(ifp, "rue", device_get_unit(sc->rue_dev));
661 ifp->if_mtu = ETHERMTU;
662 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
664 ifp->if_ioctl = rue_ioctl;
665 ifp->if_start = rue_start;
666 ifp->if_watchdog = rue_watchdog;
667 ifp->if_init = rue_init;
668 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
671 if (mii_phy_probe(self, &sc->rue_miibus,
672 rue_ifmedia_upd, rue_ifmedia_sts)) {
673 device_printf(sc->rue_dev, "MII without any PHY!\n");
677 sc->rue_qdat.ifp = ifp;
678 sc->rue_qdat.if_rxstart = rue_rxstart;
680 /* Call MI attach routine */
681 ether_ifattach(ifp, eaddr);
682 callout_handle_init(&sc->rue_stat_ch);
683 usb_register_netisr();
693 mtx_destroy(&sc->rue_mtx);
699 rue_detach(device_t dev)
701 struct rue_softc *sc;
704 sc = device_get_softc(dev);
709 untimeout(rue_tick, sc, sc->rue_stat_ch);
710 usb_rem_task(sc->rue_udev, &sc->rue_tick_task);
714 if (sc->rue_ep[RUE_ENDPT_TX] != NULL)
715 usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_TX]);
716 if (sc->rue_ep[RUE_ENDPT_RX] != NULL)
717 usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_RX]);
719 if (sc->rue_ep[RUE_ENDPT_INTR] != NULL)
720 usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_INTR]);
724 mtx_destroy(&sc->rue_mtx);
731 rue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
733 struct rue_softc *sc = priv;
735 struct rue_intrpkt *p;
740 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
745 if (status != USBD_NORMAL_COMPLETION) {
746 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
750 device_printf(sc->rue_dev, "usb error on intr: %s\n",
751 usbd_errstr(status));
752 if (status == USBD_STALLED)
753 usbd_clear_endpoint_stall(sc->rue_ep[RUE_ENDPT_INTR]);
758 usbd_get_xfer_status(xfer, NULL, (void **)&p, NULL, NULL);
760 ifp->if_ierrors += p->rue_rxlost_cnt;
761 ifp->if_ierrors += p->rue_crcerr_cnt;
762 ifp->if_collisions += p->rue_col_cnt;
769 rue_rxstart(struct ifnet *ifp)
771 struct rue_softc *sc;
776 c = &sc->rue_cdata.ue_rx_chain[sc->rue_cdata.ue_rx_prod];
778 c->ue_mbuf = usb_ether_newbuf();
779 if (c->ue_mbuf == NULL) {
780 printf("%s: no memory for rx list "
781 "-- packet dropped!\n", device_get_nameunit(sc->rue_dev));
787 /* Setup new transfer. */
788 usbd_setup_xfer(c->ue_xfer, sc->rue_ep[RUE_ENDPT_RX],
789 c, mtod(c->ue_mbuf, char *), UE_BUFSZ, USBD_SHORT_XFER_OK,
790 USBD_NO_TIMEOUT, rue_rxeof);
791 usbd_transfer(c->ue_xfer);
797 * A frame has been uploaded: pass the resulting mbuf chain up to
798 * the higher level protocols.
802 rue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
804 struct ue_chain *c = priv;
805 struct rue_softc *sc = c->ue_sc;
816 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
821 if (status != USBD_NORMAL_COMPLETION) {
822 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
826 if (usbd_ratecheck(&sc->rue_rx_notice))
827 device_printf(sc->rue_dev, "usb error on rx: %s\n",
828 usbd_errstr(status));
829 if (status == USBD_STALLED)
830 usbd_clear_endpoint_stall(sc->rue_ep[RUE_ENDPT_RX]);
834 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
836 if (total_len <= ETHER_CRC_LEN) {
842 bcopy(mtod(m, char *) + total_len - 4, (char *)&r, sizeof (r));
844 /* Check recieve packet was valid or not */
845 if ((r.rue_rxstat & RUE_RXSTAT_VALID) == 0) {
850 /* No errors; receive the packet. */
851 total_len -= ETHER_CRC_LEN;
854 m->m_pkthdr.rcvif = (void *)&sc->rue_qdat;
855 m->m_pkthdr.len = m->m_len = total_len;
857 /* Put the packet on the special USB input queue. */
864 /* Setup new transfer. */
865 usbd_setup_xfer(xfer, sc->rue_ep[RUE_ENDPT_RX],
866 c, mtod(c->ue_mbuf, char *), UE_BUFSZ,
867 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rue_rxeof);
873 * A frame was downloaded to the chip. It's safe for us to clean up
878 rue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
880 struct ue_chain *c = priv;
881 struct rue_softc *sc = c->ue_sc;
889 if (status != USBD_NORMAL_COMPLETION) {
890 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
894 device_printf(sc->rue_dev, "usb error on tx: %s\n",
895 usbd_errstr(status));
896 if (status == USBD_STALLED)
897 usbd_clear_endpoint_stall(sc->rue_ep[RUE_ENDPT_TX]);
903 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
904 usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &err);
906 if (c->ue_mbuf != NULL) {
907 c->ue_mbuf->m_pkthdr.rcvif = ifp;
908 usb_tx_done(c->ue_mbuf);
923 struct rue_softc *sc = xsc;
930 /* Perform periodic stuff in process context */
931 usb_add_task(sc->rue_udev, &sc->rue_tick_task, USB_TASKQ_DRIVER);
935 rue_tick_task(void *xsc)
937 struct rue_softc *sc = xsc;
939 struct mii_data *mii;
954 if (!sc->rue_link && mii->mii_media_status & IFM_ACTIVE &&
955 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
957 if (ifp->if_snd.ifq_head != NULL)
961 sc->rue_stat_ch = timeout(rue_tick, sc, hz);
967 rue_encap(struct rue_softc *sc, struct mbuf *m, int idx)
973 c = &sc->rue_cdata.ue_tx_chain[idx];
976 * Copy the mbuf data into a contiguous buffer
978 m_copydata(m, 0, m->m_pkthdr.len, c->ue_buf);
981 total_len = m->m_pkthdr.len;
984 * This is an undocumented behavior.
985 * RTL8150 chip doesn't send frame length smaller than
986 * RUE_MIN_FRAMELEN (60) byte packet.
988 if (total_len < RUE_MIN_FRAMELEN)
989 total_len = RUE_MIN_FRAMELEN;
991 usbd_setup_xfer(c->ue_xfer, sc->rue_ep[RUE_ENDPT_TX],
992 c, c->ue_buf, total_len, USBD_FORCE_SHORT_XFER,
996 err = usbd_transfer(c->ue_xfer);
997 if (err != USBD_IN_PROGRESS) {
1002 sc->rue_cdata.ue_tx_cnt++;
1008 rue_start(struct ifnet *ifp)
1010 struct rue_softc *sc = ifp->if_softc;
1011 struct mbuf *m_head = NULL;
1015 if (!sc->rue_link) {
1020 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
1025 IF_DEQUEUE(&ifp->if_snd, m_head);
1026 if (m_head == NULL) {
1031 if (rue_encap(sc, m_head, 0)) {
1032 IF_PREPEND(&ifp->if_snd, m_head);
1033 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1039 * If there's a BPF listener, bounce a copy of this frame
1042 BPF_MTAP(ifp, m_head);
1044 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1047 * Set a timeout in case the chip goes out to lunch.
1057 struct rue_softc *sc = xsc;
1058 struct ifnet *ifp = sc->rue_ifp;
1059 struct mii_data *mii = GET_MII(sc);
1067 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1073 * Cancel pending I/O and free all RX/TX buffers.
1077 /* Set MAC address */
1078 rue_write_mem(sc, RUE_IDR0, IF_LLADDR(sc->rue_ifp),
1082 if (usb_ether_tx_list_init(sc, &sc->rue_cdata,
1083 sc->rue_udev) == ENOBUFS) {
1084 device_printf(sc->rue_dev, "tx list init failed\n");
1090 if (usb_ether_rx_list_init(sc, &sc->rue_cdata,
1091 sc->rue_udev) == ENOBUFS) {
1092 device_printf(sc->rue_dev, "rx list init failed\n");
1097 #ifdef RUE_INTR_PIPE
1098 sc->rue_cdata.ue_ibuf = malloc(RUE_INTR_PKTLEN, M_USBDEV, M_NOWAIT);
1102 * Set the initial TX and RX configuration.
1104 rue_csr_write_1(sc, RUE_TCR, RUE_TCR_CONFIG);
1106 rxcfg = RUE_RCR_CONFIG;
1108 /* Set capture broadcast bit to capture broadcast frames. */
1109 if (ifp->if_flags & IFF_BROADCAST)
1110 rxcfg |= RUE_RCR_AB;
1112 rxcfg &= ~RUE_RCR_AB;
1114 /* If we want promiscuous mode, set the allframes bit. */
1115 if (ifp->if_flags & IFF_PROMISC)
1116 rxcfg |= RUE_RCR_AAP;
1118 rxcfg &= ~RUE_RCR_AAP;
1120 rue_csr_write_2(sc, RUE_RCR, rxcfg);
1122 /* Load the multicast filter. */
1125 /* Enable RX and TX */
1126 rue_csr_write_1(sc, RUE_CR, (RUE_CR_TE | RUE_CR_RE | RUE_CR_EP3CLREN));
1130 /* Open RX and TX pipes. */
1131 err = usbd_open_pipe(sc->rue_iface, sc->rue_ed[RUE_ENDPT_RX],
1132 USBD_EXCLUSIVE_USE, &sc->rue_ep[RUE_ENDPT_RX]);
1134 device_printf(sc->rue_dev, "open rx pipe failed: %s\n",
1139 err = usbd_open_pipe(sc->rue_iface, sc->rue_ed[RUE_ENDPT_TX],
1140 USBD_EXCLUSIVE_USE, &sc->rue_ep[RUE_ENDPT_TX]);
1142 device_printf(sc->rue_dev, "open tx pipe failed: %s\n",
1148 #ifdef RUE_INTR_PIPE
1149 err = usbd_open_pipe_intr(sc->rue_iface, sc->rue_ed[RUE_ENDPT_INTR],
1151 &sc->rue_ep[RUE_ENDPT_INTR], sc,
1152 sc->rue_cdata.ue_ibuf, RUE_INTR_PKTLEN,
1153 rue_intr, RUE_INTR_INTERVAL);
1155 device_printf(sc->rue_dev, "open intr pipe failed: %s\n",
1162 /* Start up the receive pipe. */
1163 for (i = 0; i < UE_RX_LIST_CNT; i++) {
1164 c = &sc->rue_cdata.ue_rx_chain[i];
1165 usbd_setup_xfer(c->ue_xfer, sc->rue_ep[RUE_ENDPT_RX],
1166 c, mtod(c->ue_mbuf, char *), UE_BUFSZ,
1167 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rue_rxeof);
1168 usbd_transfer(c->ue_xfer);
1171 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1172 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1174 sc->rue_stat_ch = timeout(rue_tick, sc, hz);
1180 * Set media options.
1184 rue_ifmedia_upd(struct ifnet *ifp)
1186 struct rue_softc *sc = ifp->if_softc;
1187 struct mii_data *mii = GET_MII(sc);
1190 if (mii->mii_instance) {
1191 struct mii_softc *miisc;
1192 LIST_FOREACH (miisc, &mii->mii_phys, mii_list)
1193 mii_phy_reset(miisc);
1201 * Report current media status.
1205 rue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1207 struct rue_softc *sc = ifp->if_softc;
1208 struct mii_data *mii = GET_MII(sc);
1211 ifmr->ifm_active = mii->mii_media_active;
1212 ifmr->ifm_status = mii->mii_media_status;
1216 rue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1218 struct rue_softc *sc = ifp->if_softc;
1219 struct ifreq *ifr = (struct ifreq *)data;
1220 struct mii_data *mii;
1227 if (ifp->if_flags & IFF_UP) {
1228 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1229 ifp->if_flags & IFF_PROMISC &&
1230 !(sc->rue_if_flags & IFF_PROMISC)) {
1231 RUE_SETBIT_2(sc, RUE_RCR,
1232 (RUE_RCR_AAM | RUE_RCR_AAP));
1234 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1235 !(ifp->if_flags & IFF_PROMISC) &&
1236 sc->rue_if_flags & IFF_PROMISC) {
1237 RUE_CLRBIT_2(sc, RUE_RCR,
1238 (RUE_RCR_AAM | RUE_RCR_AAP));
1240 } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1243 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1246 sc->rue_if_flags = ifp->if_flags;
1257 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1260 error = ether_ioctl(ifp, command, data);
1270 rue_watchdog(struct ifnet *ifp)
1272 struct rue_softc *sc = ifp->if_softc;
1279 device_printf(sc->rue_dev, "watchdog timeout\n");
1281 c = &sc->rue_cdata.ue_tx_chain[0];
1282 usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &stat);
1283 rue_txeof(c->ue_xfer, c, stat);
1285 if (ifp->if_snd.ifq_head != NULL)
1292 * Stop the adapter and free any mbufs allocated to the
1297 rue_stop(struct rue_softc *sc)
1307 rue_csr_write_1(sc, RUE_CR, 0x00);
1310 untimeout(rue_tick, sc, sc->rue_stat_ch);
1312 /* Stop transfers. */
1313 if (sc->rue_ep[RUE_ENDPT_RX] != NULL) {
1314 err = usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_RX]);
1316 device_printf(sc->rue_dev, "abort rx pipe failed: %s\n",
1319 err = usbd_close_pipe(sc->rue_ep[RUE_ENDPT_RX]);
1321 device_printf(sc->rue_dev, "close rx pipe failed: %s\n",
1324 sc->rue_ep[RUE_ENDPT_RX] = NULL;
1327 if (sc->rue_ep[RUE_ENDPT_TX] != NULL) {
1328 err = usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_TX]);
1330 device_printf(sc->rue_dev, "abort tx pipe failed: %s\n",
1333 err = usbd_close_pipe(sc->rue_ep[RUE_ENDPT_TX]);
1335 device_printf(sc->rue_dev, "close tx pipe failed: %s\n",
1338 sc->rue_ep[RUE_ENDPT_TX] = NULL;
1341 #ifdef RUE_INTR_PIPE
1342 if (sc->rue_ep[RUE_ENDPT_INTR] != NULL) {
1343 err = usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_INTR]);
1345 device_printf(sc->rue_dev, "abort intr pipe failed: %s\n",
1348 err = usbd_close_pipe(sc->rue_ep[RUE_ENDPT_INTR]);
1350 device_printf(sc->rue_dev, "close intr pipe failed: %s\n",
1353 sc->rue_ep[RUE_ENDPT_INTR] = NULL;
1357 /* Free RX resources. */
1358 usb_ether_rx_list_free(&sc->rue_cdata);
1359 /* Free TX resources. */
1360 usb_ether_tx_list_free(&sc->rue_cdata);
1362 #ifdef RUE_INTR_PIPE
1363 free(sc->rue_cdata.ue_ibuf, M_USBDEV);
1364 sc->rue_cdata.ue_ibuf = NULL;
1369 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1375 * Stop all chip I/O so that the kernel's probe routines don't
1376 * get confused by errant DMAs when rebooting.
1380 rue_shutdown(device_t dev)
1382 struct rue_softc *sc;
1384 sc = device_get_softc(dev);