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
740 ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
743 if (sc->rue_ep[RUE_ENDPT_TX] != NULL)
744 usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_TX]);
745 if (sc->rue_ep[RUE_ENDPT_RX] != NULL)
746 usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_RX]);
748 if (sc->rue_ep[RUE_ENDPT_INTR] != NULL)
749 usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_INTR]);
753 #if __FreeBSD_version >= 500000
754 mtx_destroy(&sc->rue_mtx);
762 rue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
764 struct rue_softc *sc = priv;
766 struct rue_intrpkt *p;
771 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
776 if (status != USBD_NORMAL_COMPLETION) {
777 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
781 printf("rue%d: usb error on intr: %s\n", sc->rue_unit,
782 usbd_errstr(status));
783 if (status == USBD_STALLED)
784 usbd_clear_endpoint_stall(sc->rue_ep[RUE_ENDPT_INTR]);
789 usbd_get_xfer_status(xfer, NULL, (void **)&p, NULL, NULL);
791 ifp->if_ierrors += p->rue_rxlost_cnt;
792 ifp->if_ierrors += p->rue_crcerr_cnt;
793 ifp->if_collisions += p->rue_col_cnt;
800 rue_rxstart(struct ifnet *ifp)
802 struct rue_softc *sc;
807 c = &sc->rue_cdata.ue_rx_chain[sc->rue_cdata.ue_rx_prod];
809 c->ue_mbuf = usb_ether_newbuf();
810 if (c->ue_mbuf == NULL) {
811 printf("%s: no memory for rx list "
812 "-- packet dropped!\n", USBDEVNAME(sc->rue_dev));
818 /* Setup new transfer. */
819 usbd_setup_xfer(c->ue_xfer, sc->rue_ep[RUE_ENDPT_RX],
820 c, mtod(c->ue_mbuf, char *), UE_BUFSZ, USBD_SHORT_XFER_OK,
821 USBD_NO_TIMEOUT, rue_rxeof);
822 usbd_transfer(c->ue_xfer);
828 * A frame has been uploaded: pass the resulting mbuf chain up to
829 * the higher level protocols.
833 rue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
835 struct ue_chain *c = priv;
836 struct rue_softc *sc = c->ue_sc;
847 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
852 if (status != USBD_NORMAL_COMPLETION) {
853 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
857 if (usbd_ratecheck(&sc->rue_rx_notice))
858 printf("rue%d: usb error on rx: %s\n", sc->rue_unit,
859 usbd_errstr(status));
860 if (status == USBD_STALLED)
861 usbd_clear_endpoint_stall(sc->rue_ep[RUE_ENDPT_RX]);
865 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
867 if (total_len <= ETHER_CRC_LEN) {
873 bcopy(mtod(m, char *) + total_len - 4, (char *)&r, sizeof (r));
875 /* Check recieve packet was valid or not */
876 if ((r.rue_rxstat & RUE_RXSTAT_VALID) == 0) {
881 /* No errors; receive the packet. */
882 total_len -= ETHER_CRC_LEN;
885 m->m_pkthdr.rcvif = (void *)&sc->rue_qdat;
886 m->m_pkthdr.len = m->m_len = total_len;
888 /* Put the packet on the special USB input queue. */
895 /* Setup new transfer. */
896 usbd_setup_xfer(xfer, sc->rue_ep[RUE_ENDPT_RX],
897 c, mtod(c->ue_mbuf, char *), UE_BUFSZ,
898 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rue_rxeof);
904 * A frame was downloaded to the chip. It's safe for us to clean up
909 rue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
911 struct ue_chain *c = priv;
912 struct rue_softc *sc = c->ue_sc;
920 if (status != USBD_NORMAL_COMPLETION) {
921 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
925 printf("rue%d: usb error on tx: %s\n", sc->rue_unit,
926 usbd_errstr(status));
927 if (status == USBD_STALLED)
928 usbd_clear_endpoint_stall(sc->rue_ep[RUE_ENDPT_TX]);
934 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
935 usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &err);
937 if (c->ue_mbuf != NULL) {
938 c->ue_mbuf->m_pkthdr.rcvif = ifp;
939 usb_tx_done(c->ue_mbuf);
954 struct rue_softc *sc = xsc;
956 struct mii_data *mii;
971 if (!sc->rue_link && mii->mii_media_status & IFM_ACTIVE &&
972 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
974 if (ifp->if_snd.ifq_head != NULL)
978 sc->rue_stat_ch = timeout(rue_tick, sc, hz);
984 rue_encap(struct rue_softc *sc, struct mbuf *m, int idx)
990 c = &sc->rue_cdata.ue_tx_chain[idx];
993 * Copy the mbuf data into a contiguous buffer
995 m_copydata(m, 0, m->m_pkthdr.len, c->ue_buf);
998 total_len = m->m_pkthdr.len;
1001 * This is an undocumented behavior.
1002 * RTL8150 chip doesn't send frame length smaller than
1003 * RUE_MIN_FRAMELEN (60) byte packet.
1005 if (total_len < RUE_MIN_FRAMELEN)
1006 total_len = RUE_MIN_FRAMELEN;
1008 usbd_setup_xfer(c->ue_xfer, sc->rue_ep[RUE_ENDPT_TX],
1009 c, c->ue_buf, total_len, USBD_FORCE_SHORT_XFER,
1013 err = usbd_transfer(c->ue_xfer);
1014 if (err != USBD_IN_PROGRESS) {
1019 sc->rue_cdata.ue_tx_cnt++;
1025 rue_start(struct ifnet *ifp)
1027 struct rue_softc *sc = ifp->if_softc;
1028 struct mbuf *m_head = NULL;
1032 if (!sc->rue_link) {
1037 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) {
1042 IF_DEQUEUE(&ifp->if_snd, m_head);
1043 if (m_head == NULL) {
1048 if (rue_encap(sc, m_head, 0)) {
1049 IF_PREPEND(&ifp->if_snd, m_head);
1050 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1056 * If there's a BPF listener, bounce a copy of this frame
1059 BPF_MTAP(ifp, m_head);
1061 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1064 * Set a timeout in case the chip goes out to lunch.
1074 struct rue_softc *sc = xsc;
1075 struct ifnet *ifp = sc->rue_ifp;
1076 struct mii_data *mii = GET_MII(sc);
1084 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1090 * Cancel pending I/O and free all RX/TX buffers.
1094 /* Set MAC address */
1095 rue_write_mem(sc, RUE_IDR0, IFP2ENADDR(sc->rue_ifp),
1099 if (usb_ether_tx_list_init(sc, &sc->rue_cdata,
1100 sc->rue_udev) == ENOBUFS) {
1101 printf("rue%d: tx list init failed\n", sc->rue_unit);
1107 if (usb_ether_rx_list_init(sc, &sc->rue_cdata,
1108 sc->rue_udev) == ENOBUFS) {
1109 printf("rue%d: rx list init failed\n", sc->rue_unit);
1114 #ifdef RUE_INTR_PIPE
1115 sc->rue_cdata.ue_ibuf = malloc(RUE_INTR_PKTLEN, M_USBDEV, M_NOWAIT);
1119 * Set the initial TX and RX configuration.
1121 rue_csr_write_1(sc, RUE_TCR, RUE_TCR_CONFIG);
1123 rxcfg = RUE_RCR_CONFIG;
1125 /* Set capture broadcast bit to capture broadcast frames. */
1126 if (ifp->if_flags & IFF_BROADCAST)
1127 rxcfg |= RUE_RCR_AB;
1129 rxcfg &= ~RUE_RCR_AB;
1131 /* If we want promiscuous mode, set the allframes bit. */
1132 if (ifp->if_flags & IFF_PROMISC)
1133 rxcfg |= RUE_RCR_AAP;
1135 rxcfg &= ~RUE_RCR_AAP;
1137 rue_csr_write_2(sc, RUE_RCR, rxcfg);
1139 /* Load the multicast filter. */
1142 /* Enable RX and TX */
1143 rue_csr_write_1(sc, RUE_CR, (RUE_CR_TE | RUE_CR_RE | RUE_CR_EP3CLREN));
1147 /* Open RX and TX pipes. */
1148 err = usbd_open_pipe(sc->rue_iface, sc->rue_ed[RUE_ENDPT_RX],
1149 USBD_EXCLUSIVE_USE, &sc->rue_ep[RUE_ENDPT_RX]);
1151 printf("rue%d: open rx pipe failed: %s\n",
1152 sc->rue_unit, usbd_errstr(err));
1156 err = usbd_open_pipe(sc->rue_iface, sc->rue_ed[RUE_ENDPT_TX],
1157 USBD_EXCLUSIVE_USE, &sc->rue_ep[RUE_ENDPT_TX]);
1159 printf("rue%d: open tx pipe failed: %s\n",
1160 sc->rue_unit, usbd_errstr(err));
1165 #ifdef RUE_INTR_PIPE
1166 err = usbd_open_pipe_intr(sc->rue_iface, sc->rue_ed[RUE_ENDPT_INTR],
1168 &sc->rue_ep[RUE_ENDPT_INTR], sc,
1169 sc->rue_cdata.ue_ibuf, RUE_INTR_PKTLEN,
1170 rue_intr, RUE_INTR_INTERVAL);
1172 printf("rue%d: open intr pipe failed: %s\n",
1173 sc->rue_unit, usbd_errstr(err));
1179 /* Start up the receive pipe. */
1180 for (i = 0; i < UE_RX_LIST_CNT; i++) {
1181 c = &sc->rue_cdata.ue_rx_chain[i];
1182 usbd_setup_xfer(c->ue_xfer, sc->rue_ep[RUE_ENDPT_RX],
1183 c, mtod(c->ue_mbuf, char *), UE_BUFSZ,
1184 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rue_rxeof);
1185 usbd_transfer(c->ue_xfer);
1188 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1189 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1191 sc->rue_stat_ch = timeout(rue_tick, sc, hz);
1197 * Set media options.
1201 rue_ifmedia_upd(struct ifnet *ifp)
1203 struct rue_softc *sc = ifp->if_softc;
1204 struct mii_data *mii = GET_MII(sc);
1207 if (mii->mii_instance) {
1208 struct mii_softc *miisc;
1209 LIST_FOREACH (miisc, &mii->mii_phys, mii_list)
1210 mii_phy_reset(miisc);
1218 * Report current media status.
1222 rue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1224 struct rue_softc *sc = ifp->if_softc;
1225 struct mii_data *mii = GET_MII(sc);
1228 ifmr->ifm_active = mii->mii_media_active;
1229 ifmr->ifm_status = mii->mii_media_status;
1233 rue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1235 struct rue_softc *sc = ifp->if_softc;
1236 struct ifreq *ifr = (struct ifreq *)data;
1237 struct mii_data *mii;
1244 if (ifp->if_flags & IFF_UP) {
1245 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1246 ifp->if_flags & IFF_PROMISC &&
1247 !(sc->rue_if_flags & IFF_PROMISC)) {
1248 RUE_SETBIT_2(sc, RUE_RCR,
1249 (RUE_RCR_AAM | RUE_RCR_AAP));
1251 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1252 !(ifp->if_flags & IFF_PROMISC) &&
1253 sc->rue_if_flags & IFF_PROMISC) {
1254 RUE_CLRBIT_2(sc, RUE_RCR,
1255 (RUE_RCR_AAM | RUE_RCR_AAP));
1257 } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1260 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1263 sc->rue_if_flags = ifp->if_flags;
1274 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1277 error = ether_ioctl(ifp, command, data);
1287 rue_watchdog(struct ifnet *ifp)
1289 struct rue_softc *sc = ifp->if_softc;
1296 printf("rue%d: watchdog timeout\n", sc->rue_unit);
1298 c = &sc->rue_cdata.ue_tx_chain[0];
1299 usbd_get_xfer_status(c->ue_xfer, NULL, NULL, NULL, &stat);
1300 rue_txeof(c->ue_xfer, c, stat);
1302 if (ifp->if_snd.ifq_head != NULL)
1309 * Stop the adapter and free any mbufs allocated to the
1314 rue_stop(struct rue_softc *sc)
1324 rue_csr_write_1(sc, RUE_CR, 0x00);
1327 untimeout(rue_tick, sc, sc->rue_stat_ch);
1329 /* Stop transfers. */
1330 if (sc->rue_ep[RUE_ENDPT_RX] != NULL) {
1331 err = usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_RX]);
1333 printf("rue%d: abort rx pipe failed: %s\n",
1334 sc->rue_unit, usbd_errstr(err));
1336 err = usbd_close_pipe(sc->rue_ep[RUE_ENDPT_RX]);
1338 printf("rue%d: close rx pipe failed: %s\n",
1339 sc->rue_unit, usbd_errstr(err));
1341 sc->rue_ep[RUE_ENDPT_RX] = NULL;
1344 if (sc->rue_ep[RUE_ENDPT_TX] != NULL) {
1345 err = usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_TX]);
1347 printf("rue%d: abort tx pipe failed: %s\n",
1348 sc->rue_unit, usbd_errstr(err));
1350 err = usbd_close_pipe(sc->rue_ep[RUE_ENDPT_TX]);
1352 printf("rue%d: close tx pipe failed: %s\n",
1353 sc->rue_unit, usbd_errstr(err));
1355 sc->rue_ep[RUE_ENDPT_TX] = NULL;
1358 #ifdef RUE_INTR_PIPE
1359 if (sc->rue_ep[RUE_ENDPT_INTR] != NULL) {
1360 err = usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_INTR]);
1362 printf("rue%d: abort intr pipe failed: %s\n",
1363 sc->rue_unit, usbd_errstr(err));
1365 err = usbd_close_pipe(sc->rue_ep[RUE_ENDPT_INTR]);
1367 printf("rue%d: close intr pipe failed: %s\n",
1368 sc->rue_unit, usbd_errstr(err));
1370 sc->rue_ep[RUE_ENDPT_INTR] = NULL;
1374 /* Free RX resources. */
1375 usb_ether_rx_list_free(&sc->rue_cdata);
1376 /* Free TX resources. */
1377 usb_ether_tx_list_free(&sc->rue_cdata);
1379 #ifdef RUE_INTR_PIPE
1380 free(sc->rue_cdata.ue_ibuf, M_USBDEV);
1381 sc->rue_cdata.ue_ibuf = NULL;
1386 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1392 * Stop all chip I/O so that the kernel's probe routines don't
1393 * get confused by errant DMAs when rebooting.
1397 rue_shutdown(device_ptr_t dev)
1399 struct rue_softc *sc;
1401 sc = device_get_softc(dev);