2 * Copyright (c) 2001-2003, Shunsuke Akiyama <akiyama@FreeBSD.org>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * Copyright (c) 1997, 1998, 1999, 2000
28 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by Bill Paul.
41 * 4. Neither the name of the author nor the names of any co-contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
49 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
55 * THE POSSIBILITY OF SUCH DAMAGE.
57 * $FreeBSD: src/sys/dev/usb/if_rue.c,v 1.14 2004/06/09 14:34:03 naddy Exp $
58 * $DragonFly: src/sys/dev/netif/rue/if_rue.c,v 1.6 2006/09/05 00:55:41 dillon Exp $
62 * RealTek RTL8150 USB to fast ethernet controller driver.
63 * Datasheet is available from
64 * ftp://ftp.realtek.com.tw/lancard/data_sheet/8150/.
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/sockio.h>
71 #include <sys/malloc.h>
72 #include <sys/kernel.h>
73 #include <sys/module.h>
74 #include <sys/socket.h>
75 #include <sys/sysctl.h>
78 #include <net/if_arp.h>
79 #include <net/ethernet.h>
80 #include <net/if_dl.h>
81 #include <net/if_media.h>
82 #include <net/ifq_var.h>
87 #include <machine/bus.h>
89 #include <bus/usb/usb.h>
90 #include <bus/usb/usbdi.h>
91 #include <bus/usb/usbdi_util.h>
92 #include <bus/usb/usbdivar.h>
93 #include <bus/usb/usbdevs.h>
94 #include <bus/usb/usb_ethersubr.h>
96 #include <dev/netif/mii_layer/mii.h>
97 #include <dev/netif/mii_layer/miivar.h>
99 #include <dev/netif/rue/if_ruereg.h>
101 #include "miibus_if.h"
104 SYSCTL_NODE(_hw, OID_AUTO, rue, CTLFLAG_RW, 0, "USB rue");
106 Static int rue_debug = 0;
107 SYSCTL_INT(_hw_rue, OID_AUTO, debug, CTLFLAG_RW, &rue_debug, 0,
110 /* XXX DPRINTF/DPRINTFN can be used only after rue_attach() */
111 #define DPRINTFN(n, x) do { if (rue_debug > (n)) if_printf x; } while (0)
113 #define DPRINTFN(n, x)
115 #define DPRINTF(x) DPRINTFN(0, x)
118 * Various supported device vendors/products.
121 Static struct rue_type rue_devs[] = {
122 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX },
123 { USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_USBKR100 },
127 Static int rue_match(device_ptr_t);
128 Static int rue_attach(device_ptr_t);
129 Static int rue_detach(device_ptr_t);
131 Static int rue_tx_list_init(struct rue_softc *);
132 Static int rue_rx_list_init(struct rue_softc *);
133 Static int rue_newbuf(struct rue_softc *, struct rue_chain *, struct mbuf *);
134 Static int rue_encap(struct rue_softc *, struct mbuf *, int);
136 Static void rue_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
138 Static void rue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
139 Static void rue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
140 Static void rue_tick(void *);
141 Static void rue_rxstart(struct ifnet *);
142 Static void rue_start(struct ifnet *);
143 Static int rue_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
144 Static void rue_init(void *);
145 Static void rue_stop(struct rue_softc *);
146 Static void rue_watchdog(struct ifnet *);
147 Static void rue_shutdown(device_ptr_t);
148 Static int rue_ifmedia_upd(struct ifnet *);
149 Static void rue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
151 Static int rue_miibus_readreg(device_ptr_t, int, int);
152 Static int rue_miibus_writereg(device_ptr_t, int, int, int);
153 Static void rue_miibus_statchg(device_ptr_t);
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, miibus, 1, 1, 1);
198 #define RUE_SETBIT(sc, reg, x) \
199 rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) | (x))
201 #define RUE_CLRBIT(sc, reg, x) \
202 rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) & ~(x))
204 #define RUE_SETBIT_2(sc, reg, x) \
205 rue_csr_write_2(sc, reg, rue_csr_read_2(sc, reg) | (x))
207 #define RUE_CLRBIT_2(sc, reg, x) \
208 rue_csr_write_2(sc, reg, rue_csr_read_2(sc, reg) & ~(x))
211 rue_read_mem(struct rue_softc *sc, u_int16_t addr, void *buf, u_int16_t len)
213 usb_device_request_t req;
221 req.bmRequestType = UT_READ_VENDOR_DEVICE;
222 req.bRequest = UR_SET_ADDRESS;
223 USETW(req.wValue, addr);
224 USETW(req.wIndex, 0);
225 USETW(req.wLength, len);
227 err = usbd_do_request(sc->rue_udev, &req, buf);
232 if_printf(&sc->arpcom.ac_if, "control pipe read failed: %s\n",
241 rue_write_mem(struct rue_softc *sc, u_int16_t addr, void *buf, u_int16_t len)
243 usb_device_request_t req;
251 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
252 req.bRequest = UR_SET_ADDRESS;
253 USETW(req.wValue, addr);
254 USETW(req.wIndex, 0);
255 USETW(req.wLength, len);
257 err = usbd_do_request(sc->rue_udev, &req, buf);
262 if_printf(&sc->arpcom.ac_if, "control pipe write failed: %s\n",
271 rue_csr_read_1(struct rue_softc *sc, int reg)
276 err = rue_read_mem(sc, reg, &val, 1);
285 rue_csr_read_2(struct rue_softc *sc, int reg)
292 err = rue_read_mem(sc, reg, &w, 2);
302 rue_csr_write_1(struct rue_softc *sc, int reg, u_int8_t val)
306 err = rue_write_mem(sc, reg, &val, 1);
315 rue_csr_write_2(struct rue_softc *sc, int reg, u_int16_t val)
321 err = rue_write_mem(sc, reg, &w, 2);
330 rue_csr_write_4(struct rue_softc *sc, int reg, u_int32_t val)
336 err = rue_write_mem(sc, reg, &dw, 4);
345 rue_miibus_readreg(device_ptr_t dev, int phy, int reg)
347 struct rue_softc *sc = USBGETSOFTC(dev);
351 if (phy != 0) /* RTL8150 supports PHY == 0, only */
375 if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) {
376 rval = rue_csr_read_1(sc, reg);
379 if_printf(&sc->arpcom.ac_if, "bad phy register\n");
383 rval = rue_csr_read_2(sc, ruereg);
389 rue_miibus_writereg(device_ptr_t dev, int phy, int reg, int data)
391 struct rue_softc *sc = USBGETSOFTC(dev);
394 if (phy != 0) /* RTL8150 supports PHY == 0, only */
418 if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) {
419 rue_csr_write_1(sc, reg, data);
422 if_printf(&sc->arpcom.ac_if, "bad phy register\n");
425 rue_csr_write_2(sc, ruereg, data);
431 rue_miibus_statchg(device_ptr_t dev)
436 * Program the 64-bit multicast hash filter.
440 rue_setmulti(struct rue_softc *sc)
444 u_int32_t hashes[2] = { 0, 0 };
445 struct ifmultiaddr *ifma;
449 ifp = &sc->arpcom.ac_if;
451 rxcfg = rue_csr_read_2(sc, RUE_RCR);
453 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
454 rxcfg |= (RUE_RCR_AAM | RUE_RCR_AAP);
455 rxcfg &= ~RUE_RCR_AM;
456 rue_csr_write_2(sc, RUE_RCR, rxcfg);
457 rue_csr_write_4(sc, RUE_MAR0, 0xFFFFFFFF);
458 rue_csr_write_4(sc, RUE_MAR4, 0xFFFFFFFF);
462 /* first, zot all the existing hash bits */
463 rue_csr_write_4(sc, RUE_MAR0, 0);
464 rue_csr_write_4(sc, RUE_MAR4, 0);
466 /* now program new ones */
467 LIST_FOREACH (ifma, &ifp->if_multiaddrs, ifma_link) {
468 if (ifma->ifma_addr->sa_family != AF_LINK)
470 h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
471 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
473 hashes[0] |= (1 << h);
475 hashes[1] |= (1 << (h - 32));
482 rxcfg &= ~RUE_RCR_AM;
484 rxcfg &= ~(RUE_RCR_AAM | RUE_RCR_AAP);
486 rue_csr_write_2(sc, RUE_RCR, rxcfg);
487 rue_csr_write_4(sc, RUE_MAR0, hashes[0]);
488 rue_csr_write_4(sc, RUE_MAR4, hashes[1]);
492 rue_reset(struct rue_softc *sc)
496 rue_csr_write_1(sc, RUE_CR, RUE_CR_SOFT_RST);
498 for (i = 0; i < RUE_TIMEOUT; i++) {
500 if (!(rue_csr_read_1(sc, RUE_CR) & RUE_CR_SOFT_RST))
503 if (i == RUE_TIMEOUT)
504 if_printf(&sc->arpcom.ac_if, "reset never completed!\n");
510 * Probe for a RTL8150 chip.
515 USB_MATCH_START(rue, uaa);
518 if (uaa->iface == NULL)
519 return (UMATCH_NONE);
523 if (uaa->vendor == t->rue_vid &&
524 uaa->product == t->rue_did) {
525 return (UMATCH_VENDOR_PRODUCT);
530 return (UMATCH_NONE);
534 * Attach the interface. Allocate softc structures, do ifmedia
535 * setup and ethernet/BPF attach.
540 USB_ATTACH_START(rue, sc, uaa);
542 uint8_t eaddr[ETHER_ADDR_LEN];
544 usb_interface_descriptor_t *id;
545 usb_endpoint_descriptor_t *ed;
548 sc->rue_udev = uaa->device;
550 if (usbd_set_config_no(sc->rue_udev, RUE_CONFIG_NO, 0)) {
551 device_printf(self, "setting config no %d failed\n",
553 USB_ATTACH_ERROR_RETURN;
556 if (usbd_device2interface_handle(uaa->device, RUE_IFACE_IDX,
558 device_printf(self, "getting interface handle failed\n");
559 USB_ATTACH_ERROR_RETURN;
562 id = usbd_get_interface_descriptor(sc->rue_iface);
564 usbd_devinfo(uaa->device, 0, devinfo);
565 device_set_desc_copy(self, devinfo);
566 device_printf(self, "%s\n", devinfo);
569 for (i = 0; i < id->bNumEndpoints; i++) {
570 ed = usbd_interface2endpoint_descriptor(sc->rue_iface, i);
572 device_printf(self, "couldn't get ep %d\n", i);
573 USB_ATTACH_ERROR_RETURN;
575 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
576 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
577 sc->rue_ed[RUE_ENDPT_RX] = ed->bEndpointAddress;
578 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
579 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
580 sc->rue_ed[RUE_ENDPT_TX] = ed->bEndpointAddress;
581 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
582 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
583 sc->rue_ed[RUE_ENDPT_INTR] = ed->bEndpointAddress;
587 ifp = &sc->arpcom.ac_if;
588 if_initname(ifp, device_get_name(self), device_get_unit(self));
590 /* Reset the adapter */
593 /* Get station address from the EEPROM */
594 if (rue_read_mem(sc, RUE_EEPROM_IDR0, eaddr, ETHER_ADDR_LEN)) {
595 device_printf(self, "couldn't get station address\n");
596 USB_ATTACH_ERROR_RETURN;
600 ifp->if_mtu = ETHERMTU;
601 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
602 ifp->if_ioctl = rue_ioctl;
603 ifp->if_start = rue_start;
604 ifp->if_watchdog = rue_watchdog;
605 ifp->if_init = rue_init;
606 ifp->if_baudrate = 10000000;
607 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
608 ifq_set_ready(&ifp->if_snd);
611 if (mii_phy_probe(self, &sc->rue_miibus,
612 rue_ifmedia_upd, rue_ifmedia_sts)) {
613 device_printf(self, "MII without any PHY!\n");
614 USB_ATTACH_ERROR_RETURN;
617 /* Call MI attach routine */
618 ether_ifattach(ifp, eaddr, NULL);
620 callout_init(&sc->rue_stat_ch);
623 usb_register_netisr();
625 USB_ATTACH_SUCCESS_RETURN;
629 rue_detach(device_ptr_t dev)
631 struct rue_softc *sc;
634 sc = device_get_softc(dev);
636 ifp = &sc->arpcom.ac_if;
639 callout_stop(&sc->rue_stat_ch);
643 if (sc->rue_ep[RUE_ENDPT_TX] != NULL)
644 usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_TX]);
645 if (sc->rue_ep[RUE_ENDPT_RX] != NULL)
646 usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_RX]);
648 if (sc->rue_ep[RUE_ENDPT_INTR] != NULL)
649 usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_INTR]);
658 * Initialize an RX descriptor and attach an MBUF cluster.
662 rue_newbuf(struct rue_softc *sc, struct rue_chain *c, struct mbuf *m)
664 struct mbuf *m_new = NULL;
667 m_new = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
669 if_printf(&sc->arpcom.ac_if, "no memory for rx list "
670 "-- packet dropped!\n");
673 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
676 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
677 m_new->m_data = m_new->m_ext.ext_buf;
680 m_adj(m_new, ETHER_ALIGN);
687 rue_rx_list_init(struct rue_softc *sc)
689 struct rue_cdata *cd;
694 for (i = 0; i < RUE_RX_LIST_CNT; i++) {
695 c = &cd->rue_rx_chain[i];
698 if (rue_newbuf(sc, c, NULL) == ENOBUFS)
700 if (c->rue_xfer == NULL) {
701 c->rue_xfer = usbd_alloc_xfer(sc->rue_udev);
702 if (c->rue_xfer == NULL)
711 rue_tx_list_init(struct rue_softc *sc)
713 struct rue_cdata *cd;
718 for (i = 0; i < RUE_TX_LIST_CNT; i++) {
719 c = &cd->rue_tx_chain[i];
723 if (c->rue_xfer == NULL) {
724 c->rue_xfer = usbd_alloc_xfer(sc->rue_udev);
725 if (c->rue_xfer == NULL)
728 c->rue_buf = kmalloc(RUE_BUFSZ, M_USBDEV, M_WAITOK);
736 rue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
738 struct rue_softc *sc = priv;
740 struct rue_intrpkt *p;
743 ifp = &sc->arpcom.ac_if;
745 if (!(ifp->if_flags & IFF_RUNNING)) {
750 if (status != USBD_NORMAL_COMPLETION) {
751 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
755 if_printf(ifp, "usb error on intr: %s\n", usbd_errstr(status));
756 if (status == USBD_STALLED)
757 usbd_clear_endpoint_stall(sc->rue_ep[RUE_ENDPT_INTR]);
762 usbd_get_xfer_status(xfer, NULL, (void **)&p, NULL, NULL);
764 ifp->if_ierrors += p->rue_rxlost_cnt;
765 ifp->if_ierrors += p->rue_crcerr_cnt;
766 ifp->if_collisions += p->rue_col_cnt;
773 rue_rxstart(struct ifnet *ifp)
775 struct rue_softc *sc;
780 c = &sc->rue_cdata.rue_rx_chain[sc->rue_cdata.rue_rx_prod];
782 if (rue_newbuf(sc, c, NULL) == ENOBUFS) {
788 /* Setup new transfer. */
789 usbd_setup_xfer(c->rue_xfer, sc->rue_ep[RUE_ENDPT_RX],
790 c, mtod(c->rue_mbuf, char *), RUE_BUFSZ, USBD_SHORT_XFER_OK,
791 USBD_NO_TIMEOUT, rue_rxeof);
792 usbd_transfer(c->rue_xfer);
798 * A frame has been uploaded: pass the resulting mbuf chain up to
799 * the higher level protocols.
803 rue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
805 struct rue_chain *c = priv;
806 struct rue_softc *sc = c->rue_sc;
815 ifp = &sc->arpcom.ac_if;
817 if (!(ifp->if_flags & IFF_RUNNING)) {
822 if (status != USBD_NORMAL_COMPLETION) {
823 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
827 if (usbd_ratecheck(&sc->rue_rx_notice)) {
828 if_printf(ifp, "usb error on rx: %s\n",
829 usbd_errstr(status));
831 if (status == USBD_STALLED)
832 usbd_clear_endpoint_stall(sc->rue_ep[RUE_ENDPT_RX]);
836 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
838 if (total_len <= ETHER_CRC_LEN) {
844 bcopy(mtod(m, char *) + total_len - 4, (char *)&r, sizeof (r));
846 /* Check recieve packet was valid or not */
847 if ((r.rue_rxstat & RUE_RXSTAT_VALID) == 0) {
852 /* No errors; receive the packet. */
853 total_len -= ETHER_CRC_LEN;
856 m->m_pkthdr.rcvif = ifp;
857 m->m_pkthdr.len = m->m_len = total_len;
859 /* Put the packet on the special USB input queue. */
867 /* Setup new transfer. */
868 usbd_setup_xfer(xfer, sc->rue_ep[RUE_ENDPT_RX],
869 c, mtod(c->rue_mbuf, char *), RUE_BUFSZ,
870 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rue_rxeof);
876 * A frame was downloaded to the chip. It's safe for us to clean up
881 rue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
883 struct rue_chain *c = priv;
884 struct rue_softc *sc = c->rue_sc;
890 ifp = &sc->arpcom.ac_if;
892 if (status != USBD_NORMAL_COMPLETION) {
893 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
897 if_printf(ifp, "usb error on tx: %s\n", usbd_errstr(status));
898 if (status == USBD_STALLED)
899 usbd_clear_endpoint_stall(sc->rue_ep[RUE_ENDPT_TX]);
905 ifp->if_flags &= ~IFF_OACTIVE;
906 usbd_get_xfer_status(c->rue_xfer, NULL, NULL, NULL, &err);
908 if (c->rue_mbuf != NULL) {
909 m_freem(c->rue_mbuf);
918 if (!ifq_is_empty(&ifp->if_snd))
927 struct rue_softc *sc = xsc;
929 struct mii_data *mii;
936 ifp = &sc->arpcom.ac_if;
944 if (!sc->rue_link && mii->mii_media_status & IFM_ACTIVE &&
945 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
947 if (!ifq_is_empty(&ifp->if_snd))
951 callout_reset(&sc->rue_stat_ch, hz, rue_tick, sc);
957 rue_encap(struct rue_softc *sc, struct mbuf *m, int idx)
963 c = &sc->rue_cdata.rue_tx_chain[idx];
966 * Copy the mbuf data into a contiguous buffer
968 m_copydata(m, 0, m->m_pkthdr.len, c->rue_buf);
971 total_len = m->m_pkthdr.len;
974 * This is an undocumented behavior.
975 * RTL8150 chip doesn't send frame length smaller than
976 * RUE_MIN_FRAMELEN (60) byte packet.
978 if (total_len < RUE_MIN_FRAMELEN)
979 total_len = RUE_MIN_FRAMELEN;
981 usbd_setup_xfer(c->rue_xfer, sc->rue_ep[RUE_ENDPT_TX],
982 c, c->rue_buf, total_len, USBD_FORCE_SHORT_XFER,
986 err = usbd_transfer(c->rue_xfer);
987 if (err != USBD_IN_PROGRESS) {
992 sc->rue_cdata.rue_tx_cnt++;
998 rue_start(struct ifnet *ifp)
1000 struct rue_softc *sc = ifp->if_softc;
1001 struct mbuf *m_head = NULL;
1005 if (!sc->rue_link) {
1010 if (ifp->if_flags & IFF_OACTIVE) {
1015 m_head = ifq_poll(&ifp->if_snd);
1016 if (m_head == NULL) {
1021 if (rue_encap(sc, m_head, 0)) {
1022 ifp->if_flags |= IFF_OACTIVE;
1026 ifq_dequeue(&ifp->if_snd, m_head);
1029 * If there's a BPF listener, bounce a copy of this frame
1032 BPF_MTAP(ifp, m_head);
1034 ifp->if_flags |= IFF_OACTIVE;
1037 * Set a timeout in case the chip goes out to lunch.
1047 struct rue_softc *sc = xsc;
1048 struct ifnet *ifp = &sc->arpcom.ac_if;
1049 struct mii_data *mii = GET_MII(sc);
1050 struct rue_chain *c;
1057 if (ifp->if_flags & IFF_RUNNING) {
1063 * Cancel pending I/O and free all RX/TX buffers.
1067 /* Set MAC address */
1068 rue_write_mem(sc, RUE_IDR0, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
1071 if (rue_tx_list_init(sc) == ENOBUFS) {
1072 if_printf(ifp, "tx list init failed\n");
1078 if (rue_rx_list_init(sc) == ENOBUFS) {
1079 if_printf(ifp, "rx list init failed\n");
1084 #ifdef RUE_INTR_PIPE
1085 sc->rue_cdata.rue_ibuf = kmalloc(RUE_INTR_PKTLEN, M_USBDEV, M_WAITOK);
1089 * Set the initial TX and RX configuration.
1091 rue_csr_write_1(sc, RUE_TCR, RUE_TCR_CONFIG);
1093 rxcfg = RUE_RCR_CONFIG;
1095 /* Set capture broadcast bit to capture broadcast frames. */
1096 if (ifp->if_flags & IFF_BROADCAST)
1097 rxcfg |= RUE_RCR_AB;
1099 rxcfg &= ~RUE_RCR_AB;
1101 /* If we want promiscuous mode, set the allframes bit. */
1102 if (ifp->if_flags & IFF_PROMISC)
1103 rxcfg |= RUE_RCR_AAP;
1105 rxcfg &= ~RUE_RCR_AAP;
1107 rue_csr_write_2(sc, RUE_RCR, rxcfg);
1109 /* Load the multicast filter. */
1112 /* Enable RX and TX */
1113 rue_csr_write_1(sc, RUE_CR, (RUE_CR_TE | RUE_CR_RE | RUE_CR_EP3CLREN));
1117 /* Open RX and TX pipes. */
1118 err = usbd_open_pipe(sc->rue_iface, sc->rue_ed[RUE_ENDPT_RX],
1119 USBD_EXCLUSIVE_USE, &sc->rue_ep[RUE_ENDPT_RX]);
1121 if_printf(ifp, "open rx pipe failed: %s\n", usbd_errstr(err));
1125 err = usbd_open_pipe(sc->rue_iface, sc->rue_ed[RUE_ENDPT_TX],
1126 USBD_EXCLUSIVE_USE, &sc->rue_ep[RUE_ENDPT_TX]);
1128 if_printf(ifp, "open tx pipe failed: %s\n", usbd_errstr(err));
1133 #ifdef RUE_INTR_PIPE
1134 err = usbd_open_pipe_intr(sc->rue_iface, sc->rue_ed[RUE_ENDPT_INTR],
1136 &sc->rue_ep[RUE_ENDPT_INTR], sc,
1137 sc->rue_cdata.rue_ibuf, RUE_INTR_PKTLEN,
1138 rue_intr, RUE_INTR_INTERVAL);
1140 if_printf(ifp, "open intr pipe failed: %s\n", usbd_errstr(err));
1146 /* Start up the receive pipe. */
1147 for (i = 0; i < RUE_RX_LIST_CNT; i++) {
1148 c = &sc->rue_cdata.rue_rx_chain[i];
1149 usbd_setup_xfer(c->rue_xfer, sc->rue_ep[RUE_ENDPT_RX],
1150 c, mtod(c->rue_mbuf, char *), RUE_BUFSZ,
1151 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, rue_rxeof);
1152 usbd_transfer(c->rue_xfer);
1155 ifp->if_flags |= IFF_RUNNING;
1156 ifp->if_flags &= ~IFF_OACTIVE;
1158 callout_reset(&sc->rue_stat_ch, hz, rue_tick, sc);
1164 * Set media options.
1168 rue_ifmedia_upd(struct ifnet *ifp)
1170 struct rue_softc *sc = ifp->if_softc;
1171 struct mii_data *mii = GET_MII(sc);
1174 if (mii->mii_instance) {
1175 struct mii_softc *miisc;
1176 LIST_FOREACH (miisc, &mii->mii_phys, mii_list)
1177 mii_phy_reset(miisc);
1185 * Report current media status.
1189 rue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1191 struct rue_softc *sc = ifp->if_softc;
1192 struct mii_data *mii = GET_MII(sc);
1195 ifmr->ifm_active = mii->mii_media_active;
1196 ifmr->ifm_status = mii->mii_media_status;
1200 rue_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
1202 struct rue_softc *sc = ifp->if_softc;
1203 struct ifreq *ifr = (struct ifreq *)data;
1204 struct mii_data *mii;
1211 if (ifp->if_flags & IFF_UP) {
1212 if (ifp->if_flags & IFF_RUNNING &&
1213 ifp->if_flags & IFF_PROMISC &&
1214 !(sc->rue_if_flags & IFF_PROMISC)) {
1215 RUE_SETBIT_2(sc, RUE_RCR,
1216 (RUE_RCR_AAM | RUE_RCR_AAP));
1218 } else if (ifp->if_flags & IFF_RUNNING &&
1219 !(ifp->if_flags & IFF_PROMISC) &&
1220 sc->rue_if_flags & IFF_PROMISC) {
1221 RUE_CLRBIT_2(sc, RUE_RCR,
1222 (RUE_RCR_AAM | RUE_RCR_AAP));
1224 } else if (!(ifp->if_flags & IFF_RUNNING))
1227 if (ifp->if_flags & IFF_RUNNING)
1230 sc->rue_if_flags = ifp->if_flags;
1241 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1244 error = ether_ioctl(ifp, command, data);
1254 rue_watchdog(struct ifnet *ifp)
1256 struct rue_softc *sc = ifp->if_softc;
1257 struct rue_chain *c;
1263 if_printf(ifp, "watchdog timeout\n");
1265 c = &sc->rue_cdata.rue_tx_chain[0];
1266 usbd_get_xfer_status(c->rue_xfer, NULL, NULL, NULL, &stat);
1267 rue_txeof(c->rue_xfer, c, stat);
1269 if (!ifq_is_empty(&ifp->if_snd))
1276 * Stop the adapter and free any mbufs allocated to the
1281 rue_stop(struct rue_softc *sc)
1289 ifp = &sc->arpcom.ac_if;
1292 rue_csr_write_1(sc, RUE_CR, 0x00);
1295 callout_stop(&sc->rue_stat_ch);
1297 /* Stop transfers. */
1298 if (sc->rue_ep[RUE_ENDPT_RX] != NULL) {
1299 err = usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_RX]);
1301 if_printf(ifp, "abort rx pipe failed: %s\n",
1304 err = usbd_close_pipe(sc->rue_ep[RUE_ENDPT_RX]);
1306 if_printf(ifp, "close rx pipe failed: %s\n",
1309 sc->rue_ep[RUE_ENDPT_RX] = NULL;
1312 if (sc->rue_ep[RUE_ENDPT_TX] != NULL) {
1313 err = usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_TX]);
1315 if_printf(ifp, "abort tx pipe failed: %s\n",
1318 err = usbd_close_pipe(sc->rue_ep[RUE_ENDPT_TX]);
1320 if_printf(ifp, "close tx pipe failed: %s\n",
1323 sc->rue_ep[RUE_ENDPT_TX] = NULL;
1326 #ifdef RUE_INTR_PIPE
1327 if (sc->rue_ep[RUE_ENDPT_INTR] != NULL) {
1328 err = usbd_abort_pipe(sc->rue_ep[RUE_ENDPT_INTR]);
1330 if_printf(ifp, "abort intr pipe failed: %s\n",
1333 err = usbd_close_pipe(sc->rue_ep[RUE_ENDPT_INTR]);
1335 if_printf(ifp, "close intr pipe failed: %s\n",
1338 sc->rue_ep[RUE_ENDPT_INTR] = NULL;
1342 /* Free RX resources. */
1343 for (i = 0; i < RUE_RX_LIST_CNT; i++) {
1344 if (sc->rue_cdata.rue_rx_chain[i].rue_buf != NULL) {
1345 kfree(sc->rue_cdata.rue_rx_chain[i].rue_buf, M_USBDEV);
1346 sc->rue_cdata.rue_rx_chain[i].rue_buf = NULL;
1348 if (sc->rue_cdata.rue_rx_chain[i].rue_mbuf != NULL) {
1349 m_freem(sc->rue_cdata.rue_rx_chain[i].rue_mbuf);
1350 sc->rue_cdata.rue_rx_chain[i].rue_mbuf = NULL;
1352 if (sc->rue_cdata.rue_rx_chain[i].rue_xfer != NULL) {
1353 usbd_free_xfer(sc->rue_cdata.rue_rx_chain[i].rue_xfer);
1354 sc->rue_cdata.rue_rx_chain[i].rue_xfer = NULL;
1358 /* Free TX resources. */
1359 for (i = 0; i < RUE_TX_LIST_CNT; i++) {
1360 if (sc->rue_cdata.rue_tx_chain[i].rue_buf != NULL) {
1361 kfree(sc->rue_cdata.rue_tx_chain[i].rue_buf, M_USBDEV);
1362 sc->rue_cdata.rue_tx_chain[i].rue_buf = NULL;
1364 if (sc->rue_cdata.rue_tx_chain[i].rue_mbuf != NULL) {
1365 m_freem(sc->rue_cdata.rue_tx_chain[i].rue_mbuf);
1366 sc->rue_cdata.rue_tx_chain[i].rue_mbuf = NULL;
1368 if (sc->rue_cdata.rue_tx_chain[i].rue_xfer != NULL) {
1369 usbd_free_xfer(sc->rue_cdata.rue_tx_chain[i].rue_xfer);
1370 sc->rue_cdata.rue_tx_chain[i].rue_xfer = NULL;
1374 #ifdef RUE_INTR_PIPE
1375 if (sc->rue_cdata.rue_ibuf != NULL) {
1376 kfree(sc->rue_cdata.rue_ibuf, M_USBDEV);
1377 sc->rue_cdata.rue_ibuf = NULL;
1383 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1389 * Stop all chip I/O so that the kernel's probe routines don't
1390 * get confused by errant DMAs when rebooting.
1394 rue_shutdown(device_ptr_t dev)
1396 struct rue_softc *sc;
1398 sc = device_get_softc(dev);