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>
89 #if __FreeBSD_version < 500000
90 #include <machine/clock.h>
93 #include <dev/usb/usb.h>
94 #include <dev/usb/usbdi.h>
95 #include <dev/usb/usbdi_util.h>
96 #include <dev/usb/usbdivar.h>
98 #include <dev/usb/usb_ethersubr.h>
100 #include <dev/mii/mii.h>
101 #include <dev/mii/miivar.h>
103 #include <dev/usb/if_ruereg.h>
105 /* "controller miibus0" required. See GENERIC if you get errors here. */
106 #include "miibus_if.h"
109 Static int ruedebug = 0;
110 SYSCTL_NODE(_hw_usb, OID_AUTO, rue, CTLFLAG_RW, 0, "USB rue");
111 SYSCTL_INT(_hw_usb_rue, OID_AUTO, debug, CTLFLAG_RW,
112 &ruedebug, 0, "rue debug level");
114 #define DPRINTFN(n, x) do { \
115 if (ruedebug > (n)) \
119 #define DPRINTFN(n, x)
121 #define DPRINTF(x) DPRINTFN(0, x)
124 * Various supported device vendors/products.
127 Static struct rue_type rue_devs[] = {
128 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX },
129 { USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_USBKR100 },
133 Static int rue_match(device_ptr_t);
134 Static int rue_attach(device_ptr_t);
135 Static int rue_detach(device_ptr_t);
137 Static int rue_encap(struct rue_softc *, struct mbuf *, int);
139 Static void rue_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
141 Static void rue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
142 Static void rue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
143 Static void rue_tick(void *);
144 Static void rue_rxstart(struct ifnet *);
145 Static void rue_start(struct ifnet *);
146 Static int rue_ioctl(struct ifnet *, u_long, caddr_t);
147 Static void rue_init(void *);
148 Static void rue_stop(struct rue_softc *);
149 Static void rue_watchdog(struct ifnet *);
150 Static void rue_shutdown(device_ptr_t);
151 Static int rue_ifmedia_upd(struct ifnet *);
152 Static void rue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
154 Static int rue_miibus_readreg(device_ptr_t, int, int);
155 Static int rue_miibus_writereg(device_ptr_t, int, int, int);
156 Static void rue_miibus_statchg(device_ptr_t);
158 Static void rue_setmulti(struct rue_softc *);
159 Static void rue_reset(struct rue_softc *);
161 Static int rue_read_mem(struct rue_softc *, u_int16_t, void *, u_int16_t);
162 Static int rue_write_mem(struct rue_softc *, u_int16_t, void *, u_int16_t);
163 Static int rue_csr_read_1(struct rue_softc *, int);
164 Static int rue_csr_write_1(struct rue_softc *, int, u_int8_t);
165 Static int rue_csr_read_2(struct rue_softc *, int);
166 Static int rue_csr_write_2(struct rue_softc *, int, u_int16_t);
167 Static int rue_csr_write_4(struct rue_softc *, int, u_int32_t);
169 Static device_method_t rue_methods[] = {
170 /* Device interface */
171 DEVMETHOD(device_probe, rue_match),
172 DEVMETHOD(device_attach, rue_attach),
173 DEVMETHOD(device_detach, rue_detach),
174 DEVMETHOD(device_shutdown, rue_shutdown),
177 DEVMETHOD(bus_print_child, bus_generic_print_child),
178 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
181 DEVMETHOD(miibus_readreg, rue_miibus_readreg),
182 DEVMETHOD(miibus_writereg, rue_miibus_writereg),
183 DEVMETHOD(miibus_statchg, rue_miibus_statchg),
188 Static driver_t rue_driver = {
191 sizeof(struct rue_softc)
194 Static devclass_t rue_devclass;
196 DRIVER_MODULE(rue, uhub, rue_driver, rue_devclass, usbd_driver_load, 0);
197 DRIVER_MODULE(miibus, rue, miibus_driver, miibus_devclass, 0, 0);
198 MODULE_DEPEND(rue, usb, 1, 1, 1);
199 MODULE_DEPEND(rue, ether, 1, 1, 1);
200 MODULE_DEPEND(rue, miibus, 1, 1, 1);
202 #define RUE_SETBIT(sc, reg, x) \
203 rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) | (x))
205 #define RUE_CLRBIT(sc, reg, x) \
206 rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) & ~(x))
208 #define RUE_SETBIT_2(sc, reg, x) \
209 rue_csr_write_2(sc, reg, rue_csr_read_2(sc, reg) | (x))
211 #define RUE_CLRBIT_2(sc, reg, x) \
212 rue_csr_write_2(sc, reg, rue_csr_read_2(sc, reg) & ~(x))
215 rue_read_mem(struct rue_softc *sc, u_int16_t addr, void *buf, u_int16_t len)
217 usb_device_request_t req;
225 req.bmRequestType = UT_READ_VENDOR_DEVICE;
226 req.bRequest = UR_SET_ADDRESS;
227 USETW(req.wValue, addr);
228 USETW(req.wIndex, 0);
229 USETW(req.wLength, len);
231 err = usbd_do_request(sc->rue_udev, &req, buf);
236 printf("rue%d: control pipe read failed: %s\n",
237 sc->rue_unit, usbd_errstr(err));
245 rue_write_mem(struct rue_softc *sc, u_int16_t addr, void *buf, u_int16_t len)
247 usb_device_request_t req;
255 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
256 req.bRequest = UR_SET_ADDRESS;
257 USETW(req.wValue, addr);
258 USETW(req.wIndex, 0);
259 USETW(req.wLength, len);
261 err = usbd_do_request(sc->rue_udev, &req, buf);
266 printf("rue%d: control pipe write failed: %s\n",
267 sc->rue_unit, usbd_errstr(err));
275 rue_csr_read_1(struct rue_softc *sc, int reg)
280 err = rue_read_mem(sc, reg, &val, 1);
289 rue_csr_read_2(struct rue_softc *sc, int reg)
296 err = rue_read_mem(sc, reg, &w, 2);
306 rue_csr_write_1(struct rue_softc *sc, int reg, u_int8_t val)
310 err = rue_write_mem(sc, reg, &val, 1);
319 rue_csr_write_2(struct rue_softc *sc, int reg, u_int16_t val)
325 err = rue_write_mem(sc, reg, &w, 2);
334 rue_csr_write_4(struct rue_softc *sc, int reg, u_int32_t val)
340 err = rue_write_mem(sc, reg, &dw, 4);
349 rue_miibus_readreg(device_ptr_t dev, int phy, int reg)
351 struct rue_softc *sc = USBGETSOFTC(dev);
355 if (phy != 0) /* RTL8150 supports PHY == 0, only */
379 if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) {
380 rval = rue_csr_read_1(sc, reg);
383 printf("rue%d: bad phy register\n", sc->rue_unit);
387 rval = rue_csr_read_2(sc, ruereg);
393 rue_miibus_writereg(device_ptr_t dev, int phy, int reg, int data)
395 struct rue_softc *sc = USBGETSOFTC(dev);
398 if (phy != 0) /* RTL8150 supports PHY == 0, only */
422 if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) {
423 rue_csr_write_1(sc, reg, data);
426 printf("rue%d: bad phy register\n", sc->rue_unit);
429 rue_csr_write_2(sc, ruereg, data);
435 rue_miibus_statchg(device_ptr_t dev)
438 * When the code below is enabled the card starts doing weird
439 * things after link going from UP to DOWN and back UP.
441 * Looks like some of register writes below messes up PHY
444 * No visible regressions were found after commenting this code
445 * out, so that disable it for good.
448 struct rue_softc *sc = USBGETSOFTC(dev);
449 struct mii_data *mii = GET_MII(sc);
452 RUE_CLRBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE));
454 bmcr = rue_csr_read_2(sc, RUE_BMCR);
456 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX)
457 bmcr |= RUE_BMCR_SPD_SET;
459 bmcr &= ~RUE_BMCR_SPD_SET;
461 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
462 bmcr |= RUE_BMCR_DUPLEX;
464 bmcr &= ~RUE_BMCR_DUPLEX;
466 rue_csr_write_2(sc, RUE_BMCR, bmcr);
468 RUE_SETBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE));
473 * Program the 64-bit multicast hash filter.
477 rue_setmulti(struct rue_softc *sc)
481 u_int32_t hashes[2] = { 0, 0 };
482 struct ifmultiaddr *ifma;
488 rxcfg = rue_csr_read_2(sc, RUE_RCR);
490 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
491 rxcfg |= (RUE_RCR_AAM | RUE_RCR_AAP);
492 rxcfg &= ~RUE_RCR_AM;
493 rue_csr_write_2(sc, RUE_RCR, rxcfg);
494 rue_csr_write_4(sc, RUE_MAR0, 0xFFFFFFFF);
495 rue_csr_write_4(sc, RUE_MAR4, 0xFFFFFFFF);
499 /* first, zot all the existing hash bits */
500 rue_csr_write_4(sc, RUE_MAR0, 0);
501 rue_csr_write_4(sc, RUE_MAR4, 0);
503 /* now program new ones */
505 #if __FreeBSD_version >= 500000
506 TAILQ_FOREACH (ifma, &ifp->if_multiaddrs, ifma_link)
508 LIST_FOREACH (ifma, &ifp->if_multiaddrs, ifma_link)
511 if (ifma->ifma_addr->sa_family != AF_LINK)
513 h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
514 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
516 hashes[0] |= (1 << h);
518 hashes[1] |= (1 << (h - 32));
526 rxcfg &= ~RUE_RCR_AM;
528 rxcfg &= ~(RUE_RCR_AAM | RUE_RCR_AAP);
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]);
536 rue_reset(struct rue_softc *sc)
540 rue_csr_write_1(sc, RUE_CR, RUE_CR_SOFT_RST);
542 for (i = 0; i < RUE_TIMEOUT; i++) {
544 if (!(rue_csr_read_1(sc, RUE_CR) & RUE_CR_SOFT_RST))
547 if (i == RUE_TIMEOUT)
548 printf("rue%d: reset never completed!\n", sc->rue_unit);
554 * Probe for a RTL8150 chip.
559 USB_MATCH_START(rue, uaa);
562 if (uaa->iface == NULL)
563 return (UMATCH_NONE);
567 if (uaa->vendor == t->rue_vid &&
568 uaa->product == t->rue_did) {
569 return (UMATCH_VENDOR_PRODUCT);
574 return (UMATCH_NONE);
578 * Attach the interface. Allocate softc structures, do ifmedia
579 * setup and ethernet/BPF attach.
584 USB_ATTACH_START(rue, sc, uaa);
586 u_char eaddr[ETHER_ADDR_LEN];
588 usbd_interface_handle iface;
590 usb_interface_descriptor_t *id;
591 usb_endpoint_descriptor_t *ed;
595 devinfo = malloc(1024, M_USBDEV, M_WAITOK);
597 bzero(sc, sizeof (struct rue_softc));
598 usbd_devinfo(uaa->device, 0, devinfo);
601 sc->rue_udev = uaa->device;
602 sc->rue_unit = device_get_unit(self);
604 if (usbd_set_config_no(sc->rue_udev, RUE_CONFIG_NO, 0)) {
605 printf("rue%d: getting interface handle failed\n",
610 err = usbd_device2interface_handle(uaa->device, RUE_IFACE_IDX, &iface);
612 printf("rue%d: getting interface handle failed\n",
617 sc->rue_iface = iface;
621 if (uaa->vendor == t->rue_vid &&
622 uaa->product == t->rue_did) {
629 id = usbd_get_interface_descriptor(sc->rue_iface);
631 usbd_devinfo(uaa->device, 0, devinfo);
632 device_set_desc_copy(self, devinfo);
633 printf("%s: %s\n", USBDEVNAME(self), devinfo);
636 for (i = 0; i < id->bNumEndpoints; i++) {
637 ed = usbd_interface2endpoint_descriptor(iface, i);
639 printf("rue%d: couldn't get ep %d\n", sc->rue_unit, i);
642 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
643 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
644 sc->rue_ed[RUE_ENDPT_RX] = ed->bEndpointAddress;
645 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
646 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
647 sc->rue_ed[RUE_ENDPT_TX] = ed->bEndpointAddress;
648 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
649 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
650 sc->rue_ed[RUE_ENDPT_INTR] = ed->bEndpointAddress;
654 #if __FreeBSD_version >= 500000
655 mtx_init(&sc->rue_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
656 MTX_DEF | MTX_RECURSE);
660 /* Reset the adapter */
663 /* Get station address from the EEPROM */
664 err = rue_read_mem(sc, RUE_EEPROM_IDR0,
665 (caddr_t)&eaddr, ETHER_ADDR_LEN);
667 printf("rue%d: couldn't get station address\n", sc->rue_unit);
671 ifp = sc->rue_ifp = if_alloc(IFT_ETHER);
673 printf("rue%d: can not if_alloc()\n", sc->rue_unit);
677 if_initname(ifp, "rue", sc->rue_unit);
678 ifp->if_mtu = ETHERMTU;
679 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST |
681 ifp->if_ioctl = rue_ioctl;
682 ifp->if_start = rue_start;
683 ifp->if_watchdog = rue_watchdog;
684 ifp->if_init = rue_init;
685 ifp->if_baudrate = 10000000;
686 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
689 if (mii_phy_probe(self, &sc->rue_miibus,
690 rue_ifmedia_upd, rue_ifmedia_sts)) {
691 printf("rue%d: MII without any PHY!\n", sc->rue_unit);
695 sc->rue_qdat.ifp = ifp;
696 sc->rue_qdat.if_rxstart = rue_rxstart;
698 /* Call MI attach routine */
699 #if __FreeBSD_version >= 500000
700 ether_ifattach(ifp, eaddr);
702 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
704 callout_handle_init(&sc->rue_stat_ch);
705 usb_register_netisr();
709 free(devinfo, M_USBDEV);
710 USB_ATTACH_SUCCESS_RETURN;
716 #if __FreeBSD_version >= 500000
717 mtx_destroy(&sc->rue_mtx);
720 free(devinfo, M_USBDEV);
721 USB_ATTACH_ERROR_RETURN;
725 rue_detach(device_ptr_t dev)
727 struct rue_softc *sc;
730 sc = device_get_softc(dev);
735 untimeout(rue_tick, sc, sc->rue_stat_ch);
736 #if __FreeBSD_version >= 500000
739 ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
742 if (sc->rue_ep[RUE_ENDPT_TX] != NULL)
743 usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_TX]);
744 if (sc->rue_ep[RUE_ENDPT_RX] != NULL)
745 usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_RX]);
747 if (sc->rue_ep[RUE_ENDPT_INTR] != NULL)
748 usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_INTR]);
752 #if __FreeBSD_version >= 500000
753 mtx_destroy(&sc->rue_mtx);
761 rue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
763 struct rue_softc *sc = priv;
765 struct rue_intrpkt *p;
770 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
775 if (status != USBD_NORMAL_COMPLETION) {
776 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
780 printf("rue%d: usb error on intr: %s\n", sc->rue_unit,
781 usbd_errstr(status));
782 if (status == USBD_STALLED)
783 usbd_clear_endpoint_stall(sc->rue_ep[RUE_ENDPT_INTR]);
788 usbd_get_xfer_status(xfer, NULL, (void **)&p, NULL, NULL);
790 ifp->if_ierrors += p->rue_rxlost_cnt;
791 ifp->if_ierrors += p->rue_crcerr_cnt;
792 ifp->if_collisions += p->rue_col_cnt;
799 rue_rxstart(struct ifnet *ifp)
801 struct rue_softc *sc;
806 c = &sc->rue_cdata.ue_rx_chain[sc->rue_cdata.ue_rx_prod];
808 c->ue_mbuf = usb_ether_newbuf();
809 if (c->ue_mbuf == NULL) {
810 printf("%s: no memory for rx list "
811 "-- packet dropped!\n", USBDEVNAME(sc->rue_dev));
817 /* Setup new transfer. */
818 usbd_setup_xfer(c->ue_xfer, sc->rue_ep[RUE_ENDPT_RX],
819 c, mtod(c->ue_mbuf, char *), UE_BUFSZ, USBD_SHORT_XFER_OK,
820 USBD_NO_TIMEOUT, rue_rxeof);
821 usbd_transfer(c->ue_xfer);
827 * A frame has been uploaded: pass the resulting mbuf chain up to
828 * the higher level protocols.
832 rue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
834 struct ue_chain *c = priv;
835 struct rue_softc *sc = c->ue_sc;
846 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
851 if (status != USBD_NORMAL_COMPLETION) {
852 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
856 if (usbd_ratecheck(&sc->rue_rx_notice))
857 printf("rue%d: usb error on rx: %s\n", sc->rue_unit,
858 usbd_errstr(status));
859 if (status == USBD_STALLED)
860 usbd_clear_endpoint_stall(sc->rue_ep[RUE_ENDPT_RX]);
864 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
866 if (total_len <= ETHER_CRC_LEN) {
872 bcopy(mtod(m, char *) + total_len - 4, (char *)&r, sizeof (r));
874 /* Check recieve packet was valid or not */
875 if ((r.rue_rxstat & RUE_RXSTAT_VALID) == 0) {
880 /* No errors; receive the packet. */
881 total_len -= ETHER_CRC_LEN;
884 m->m_pkthdr.rcvif = (void *)&sc->rue_qdat;
885 m->m_pkthdr.len = m->m_len = total_len;
887 /* Put the packet on the special USB input queue. */
894 /* Setup new transfer. */
895 usbd_setup_xfer(xfer, sc->rue_ep[RUE_ENDPT_RX],
896 c, mtod(c->ue_mbuf, char *), UE_BUFSZ,
897 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rue_rxeof);
903 * A frame was downloaded to the chip. It's safe for us to clean up
908 rue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
910 struct ue_chain *c = priv;
911 struct rue_softc *sc = c->ue_sc;
919 if (status != USBD_NORMAL_COMPLETION) {
920 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
924 printf("rue%d: usb error on tx: %s\n", sc->rue_unit,
925 usbd_errstr(status));
926 if (status == USBD_STALLED)
927 usbd_clear_endpoint_stall(sc->rue_ep[RUE_ENDPT_TX]);
933 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
934 usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &err);
936 if (c->ue_mbuf != NULL) {
937 c->ue_mbuf->m_pkthdr.rcvif = ifp;
938 usb_tx_done(c->ue_mbuf);
953 struct rue_softc *sc = xsc;
955 struct mii_data *mii;
970 if (!sc->rue_link && mii->mii_media_status & IFM_ACTIVE &&
971 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
973 if (ifp->if_snd.ifq_head != NULL)
977 sc->rue_stat_ch = timeout(rue_tick, sc, hz);
983 rue_encap(struct rue_softc *sc, struct mbuf *m, int idx)
989 c = &sc->rue_cdata.ue_tx_chain[idx];
992 * Copy the mbuf data into a contiguous buffer
994 m_copydata(m, 0, m->m_pkthdr.len, c->ue_buf);
997 total_len = m->m_pkthdr.len;
1000 * This is an undocumented behavior.
1001 * RTL8150 chip doesn't send frame length smaller than
1002 * RUE_MIN_FRAMELEN (60) byte packet.
1004 if (total_len < RUE_MIN_FRAMELEN)
1005 total_len = RUE_MIN_FRAMELEN;
1007 usbd_setup_xfer(c->ue_xfer, sc->rue_ep[RUE_ENDPT_TX],
1008 c, c->ue_buf, total_len, USBD_FORCE_SHORT_XFER,
1012 err = usbd_transfer(c->ue_xfer);
1013 if (err != USBD_IN_PROGRESS) {
1018 sc->rue_cdata.ue_tx_cnt++;
1024 rue_start(struct ifnet *ifp)
1026 struct rue_softc *sc = ifp->if_softc;
1027 struct mbuf *m_head = NULL;
1031 if (!sc->rue_link) {
1036 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
1041 IF_DEQUEUE(&ifp->if_snd, m_head);
1042 if (m_head == NULL) {
1047 if (rue_encap(sc, m_head, 0)) {
1048 IF_PREPEND(&ifp->if_snd, m_head);
1049 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1055 * If there's a BPF listener, bounce a copy of this frame
1058 BPF_MTAP(ifp, m_head);
1060 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1063 * Set a timeout in case the chip goes out to lunch.
1073 struct rue_softc *sc = xsc;
1074 struct ifnet *ifp = sc->rue_ifp;
1075 struct mii_data *mii = GET_MII(sc);
1083 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1089 * Cancel pending I/O and free all RX/TX buffers.
1093 /* Set MAC address */
1094 rue_write_mem(sc, RUE_IDR0, IFP2ENADDR(sc->rue_ifp),
1098 if (usb_ether_tx_list_init(sc, &sc->rue_cdata,
1099 sc->rue_udev) == ENOBUFS) {
1100 printf("rue%d: tx list init failed\n", sc->rue_unit);
1106 if (usb_ether_rx_list_init(sc, &sc->rue_cdata,
1107 sc->rue_udev) == ENOBUFS) {
1108 printf("rue%d: rx list init failed\n", sc->rue_unit);
1113 #ifdef RUE_INTR_PIPE
1114 sc->rue_cdata.ue_ibuf = malloc(RUE_INTR_PKTLEN, M_USBDEV, M_NOWAIT);
1118 * Set the initial TX and RX configuration.
1120 rue_csr_write_1(sc, RUE_TCR, RUE_TCR_CONFIG);
1122 rxcfg = RUE_RCR_CONFIG;
1124 /* Set capture broadcast bit to capture broadcast frames. */
1125 if (ifp->if_flags & IFF_BROADCAST)
1126 rxcfg |= RUE_RCR_AB;
1128 rxcfg &= ~RUE_RCR_AB;
1130 /* If we want promiscuous mode, set the allframes bit. */
1131 if (ifp->if_flags & IFF_PROMISC)
1132 rxcfg |= RUE_RCR_AAP;
1134 rxcfg &= ~RUE_RCR_AAP;
1136 rue_csr_write_2(sc, RUE_RCR, rxcfg);
1138 /* Load the multicast filter. */
1141 /* Enable RX and TX */
1142 rue_csr_write_1(sc, RUE_CR, (RUE_CR_TE | RUE_CR_RE | RUE_CR_EP3CLREN));
1146 /* Open RX and TX pipes. */
1147 err = usbd_open_pipe(sc->rue_iface, sc->rue_ed[RUE_ENDPT_RX],
1148 USBD_EXCLUSIVE_USE, &sc->rue_ep[RUE_ENDPT_RX]);
1150 printf("rue%d: open rx pipe failed: %s\n",
1151 sc->rue_unit, usbd_errstr(err));
1155 err = usbd_open_pipe(sc->rue_iface, sc->rue_ed[RUE_ENDPT_TX],
1156 USBD_EXCLUSIVE_USE, &sc->rue_ep[RUE_ENDPT_TX]);
1158 printf("rue%d: open tx pipe failed: %s\n",
1159 sc->rue_unit, usbd_errstr(err));
1164 #ifdef RUE_INTR_PIPE
1165 err = usbd_open_pipe_intr(sc->rue_iface, sc->rue_ed[RUE_ENDPT_INTR],
1167 &sc->rue_ep[RUE_ENDPT_INTR], sc,
1168 sc->rue_cdata.ue_ibuf, RUE_INTR_PKTLEN,
1169 rue_intr, RUE_INTR_INTERVAL);
1171 printf("rue%d: open intr pipe failed: %s\n",
1172 sc->rue_unit, usbd_errstr(err));
1178 /* Start up the receive pipe. */
1179 for (i = 0; i < UE_RX_LIST_CNT; i++) {
1180 c = &sc->rue_cdata.ue_rx_chain[i];
1181 usbd_setup_xfer(c->ue_xfer, sc->rue_ep[RUE_ENDPT_RX],
1182 c, mtod(c->ue_mbuf, char *), UE_BUFSZ,
1183 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rue_rxeof);
1184 usbd_transfer(c->ue_xfer);
1187 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1188 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1190 sc->rue_stat_ch = timeout(rue_tick, sc, hz);
1196 * Set media options.
1200 rue_ifmedia_upd(struct ifnet *ifp)
1202 struct rue_softc *sc = ifp->if_softc;
1203 struct mii_data *mii = GET_MII(sc);
1206 if (mii->mii_instance) {
1207 struct mii_softc *miisc;
1208 LIST_FOREACH (miisc, &mii->mii_phys, mii_list)
1209 mii_phy_reset(miisc);
1217 * Report current media status.
1221 rue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1223 struct rue_softc *sc = ifp->if_softc;
1224 struct mii_data *mii = GET_MII(sc);
1227 ifmr->ifm_active = mii->mii_media_active;
1228 ifmr->ifm_status = mii->mii_media_status;
1232 rue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1234 struct rue_softc *sc = ifp->if_softc;
1235 struct ifreq *ifr = (struct ifreq *)data;
1236 struct mii_data *mii;
1243 if (ifp->if_flags & IFF_UP) {
1244 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1245 ifp->if_flags & IFF_PROMISC &&
1246 !(sc->rue_if_flags & IFF_PROMISC)) {
1247 RUE_SETBIT_2(sc, RUE_RCR,
1248 (RUE_RCR_AAM | RUE_RCR_AAP));
1250 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1251 !(ifp->if_flags & IFF_PROMISC) &&
1252 sc->rue_if_flags & IFF_PROMISC) {
1253 RUE_CLRBIT_2(sc, RUE_RCR,
1254 (RUE_RCR_AAM | RUE_RCR_AAP));
1256 } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1259 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1262 sc->rue_if_flags = ifp->if_flags;
1273 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1276 error = ether_ioctl(ifp, command, data);
1286 rue_watchdog(struct ifnet *ifp)
1288 struct rue_softc *sc = ifp->if_softc;
1295 printf("rue%d: watchdog timeout\n", sc->rue_unit);
1297 c = &sc->rue_cdata.ue_tx_chain[0];
1298 usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &stat);
1299 rue_txeof(c->ue_xfer, c, stat);
1301 if (ifp->if_snd.ifq_head != NULL)
1308 * Stop the adapter and free any mbufs allocated to the
1313 rue_stop(struct rue_softc *sc)
1323 rue_csr_write_1(sc, RUE_CR, 0x00);
1326 untimeout(rue_tick, sc, sc->rue_stat_ch);
1328 /* Stop transfers. */
1329 if (sc->rue_ep[RUE_ENDPT_RX] != NULL) {
1330 err = usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_RX]);
1332 printf("rue%d: abort rx pipe failed: %s\n",
1333 sc->rue_unit, usbd_errstr(err));
1335 err = usbd_close_pipe(sc->rue_ep[RUE_ENDPT_RX]);
1337 printf("rue%d: close rx pipe failed: %s\n",
1338 sc->rue_unit, usbd_errstr(err));
1340 sc->rue_ep[RUE_ENDPT_RX] = NULL;
1343 if (sc->rue_ep[RUE_ENDPT_TX] != NULL) {
1344 err = usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_TX]);
1346 printf("rue%d: abort tx pipe failed: %s\n",
1347 sc->rue_unit, usbd_errstr(err));
1349 err = usbd_close_pipe(sc->rue_ep[RUE_ENDPT_TX]);
1351 printf("rue%d: close tx pipe failed: %s\n",
1352 sc->rue_unit, usbd_errstr(err));
1354 sc->rue_ep[RUE_ENDPT_TX] = NULL;
1357 #ifdef RUE_INTR_PIPE
1358 if (sc->rue_ep[RUE_ENDPT_INTR] != NULL) {
1359 err = usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_INTR]);
1361 printf("rue%d: abort intr pipe failed: %s\n",
1362 sc->rue_unit, usbd_errstr(err));
1364 err = usbd_close_pipe(sc->rue_ep[RUE_ENDPT_INTR]);
1366 printf("rue%d: close intr pipe failed: %s\n",
1367 sc->rue_unit, usbd_errstr(err));
1369 sc->rue_ep[RUE_ENDPT_INTR] = NULL;
1373 /* Free RX resources. */
1374 usb_ether_rx_list_free(&sc->rue_cdata);
1375 /* Free TX resources. */
1376 usb_ether_tx_list_free(&sc->rue_cdata);
1378 #ifdef RUE_INTR_PIPE
1379 free(sc->rue_cdata.ue_ibuf, M_USBDEV);
1380 sc->rue_cdata.ue_ibuf = NULL;
1385 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1391 * Stop all chip I/O so that the kernel's probe routines don't
1392 * get confused by errant DMAs when rebooting.
1396 rue_shutdown(device_ptr_t dev)
1398 struct rue_softc *sc;
1400 sc = device_get_softc(dev);