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.13 2008/05/14 11:59:21 sephe 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>
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/ifq_var.h>
87 #include <bus/usb/usb.h>
88 #include <bus/usb/usbdi.h>
89 #include <bus/usb/usbdi_util.h>
90 #include <bus/usb/usbdivar.h>
91 #include <bus/usb/usb_ethersubr.h>
93 #include <dev/netif/mii_layer/mii.h>
94 #include <dev/netif/mii_layer/miivar.h>
96 #include <dev/netif/rue/if_ruereg.h>
98 #include "miibus_if.h"
101 SYSCTL_NODE(_hw
, OID_AUTO
, rue
, CTLFLAG_RW
, 0, "USB rue");
103 static int rue_debug
= 0;
104 SYSCTL_INT(_hw_rue
, OID_AUTO
, debug
, CTLFLAG_RW
, &rue_debug
, 0,
107 /* XXX DPRINTF/DPRINTFN can be used only after rue_attach() */
108 #define DPRINTFN(n, x) do { if (rue_debug > (n)) if_printf x; } while (0)
110 #define DPRINTFN(n, x)
112 #define DPRINTF(x) DPRINTFN(0, x)
115 * Various supported device vendors/products.
118 static const struct usb_devno rue_devs
[] = {
119 { USB_DEVICE(0x0411, 0x0012) }, /* Melco LUA-KTX*/
120 { USB_DEVICE(0x0bda, 0x8150) }, /* Realtek USBKR100 (GREEN HOUSE) */
123 static int rue_match(device_t
);
124 static int rue_attach(device_t
);
125 static int rue_detach(device_t
);
127 static int rue_tx_list_init(struct rue_softc
*);
128 static int rue_rx_list_init(struct rue_softc
*);
129 static int rue_newbuf(struct rue_softc
*, struct rue_chain
*, struct mbuf
*);
130 static int rue_encap(struct rue_softc
*, struct mbuf
*, int);
132 static void rue_intr(usbd_xfer_handle
, usbd_private_handle
, usbd_status
);
134 static void rue_rxeof(usbd_xfer_handle
, usbd_private_handle
, usbd_status
);
135 static void rue_txeof(usbd_xfer_handle
, usbd_private_handle
, usbd_status
);
136 static void rue_tick(void *);
137 static void rue_rxstart(struct ifnet
*);
138 static void rue_start(struct ifnet
*);
139 static int rue_ioctl(struct ifnet
*, u_long
, caddr_t
, struct ucred
*);
140 static void rue_init(void *);
141 static void rue_stop(struct rue_softc
*);
142 static void rue_watchdog(struct ifnet
*);
143 static void rue_shutdown(device_t
);
144 static int rue_ifmedia_upd(struct ifnet
*);
145 static void rue_ifmedia_sts(struct ifnet
*, struct ifmediareq
*);
147 static int rue_miibus_readreg(device_t
, int, int);
148 static int rue_miibus_writereg(device_t
, int, int, int);
149 static void rue_miibus_statchg(device_t
);
151 static void rue_setmulti(struct rue_softc
*);
152 static void rue_reset(struct rue_softc
*);
154 static int rue_read_mem(struct rue_softc
*, u_int16_t
, void *, u_int16_t
);
155 static int rue_write_mem(struct rue_softc
*, u_int16_t
, void *, u_int16_t
);
156 static int rue_csr_read_1(struct rue_softc
*, int);
157 static int rue_csr_write_1(struct rue_softc
*, int, u_int8_t
);
158 static int rue_csr_read_2(struct rue_softc
*, int);
159 static int rue_csr_write_2(struct rue_softc
*, int, u_int16_t
);
160 static int rue_csr_write_4(struct rue_softc
*, int, u_int32_t
);
162 static device_method_t rue_methods
[] = {
163 /* Device interface */
164 DEVMETHOD(device_probe
, rue_match
),
165 DEVMETHOD(device_attach
, rue_attach
),
166 DEVMETHOD(device_detach
, rue_detach
),
167 DEVMETHOD(device_shutdown
, rue_shutdown
),
170 DEVMETHOD(bus_print_child
, bus_generic_print_child
),
171 DEVMETHOD(bus_driver_added
, bus_generic_driver_added
),
174 DEVMETHOD(miibus_readreg
, rue_miibus_readreg
),
175 DEVMETHOD(miibus_writereg
, rue_miibus_writereg
),
176 DEVMETHOD(miibus_statchg
, rue_miibus_statchg
),
181 static driver_t rue_driver
= {
184 sizeof(struct rue_softc
)
187 static devclass_t rue_devclass
;
189 DRIVER_MODULE(rue
, uhub
, rue_driver
, rue_devclass
, usbd_driver_load
, 0);
190 DRIVER_MODULE(miibus
, rue
, miibus_driver
, miibus_devclass
, 0, 0);
191 MODULE_DEPEND(rue
, usb
, 1, 1, 1);
192 MODULE_DEPEND(rue
, miibus
, 1, 1, 1);
194 #define RUE_SETBIT(sc, reg, x) \
195 rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) | (x))
197 #define RUE_CLRBIT(sc, reg, x) \
198 rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) & ~(x))
200 #define RUE_SETBIT_2(sc, reg, x) \
201 rue_csr_write_2(sc, reg, rue_csr_read_2(sc, reg) | (x))
203 #define RUE_CLRBIT_2(sc, reg, x) \
204 rue_csr_write_2(sc, reg, rue_csr_read_2(sc, reg) & ~(x))
207 rue_read_mem(struct rue_softc
*sc
, u_int16_t addr
, void *buf
, u_int16_t len
)
209 usb_device_request_t req
;
217 req
.bmRequestType
= UT_READ_VENDOR_DEVICE
;
218 req
.bRequest
= UR_SET_ADDRESS
;
219 USETW(req
.wValue
, addr
);
220 USETW(req
.wIndex
, 0);
221 USETW(req
.wLength
, len
);
223 err
= usbd_do_request(sc
->rue_udev
, &req
, buf
);
228 if_printf(&sc
->arpcom
.ac_if
, "control pipe read failed: %s\n",
237 rue_write_mem(struct rue_softc
*sc
, u_int16_t addr
, void *buf
, u_int16_t len
)
239 usb_device_request_t req
;
247 req
.bmRequestType
= UT_WRITE_VENDOR_DEVICE
;
248 req
.bRequest
= UR_SET_ADDRESS
;
249 USETW(req
.wValue
, addr
);
250 USETW(req
.wIndex
, 0);
251 USETW(req
.wLength
, len
);
253 err
= usbd_do_request(sc
->rue_udev
, &req
, buf
);
258 if_printf(&sc
->arpcom
.ac_if
, "control pipe write failed: %s\n",
267 rue_csr_read_1(struct rue_softc
*sc
, int reg
)
272 err
= rue_read_mem(sc
, reg
, &val
, 1);
281 rue_csr_read_2(struct rue_softc
*sc
, int reg
)
288 err
= rue_read_mem(sc
, reg
, &w
, 2);
298 rue_csr_write_1(struct rue_softc
*sc
, int reg
, u_int8_t val
)
302 err
= rue_write_mem(sc
, reg
, &val
, 1);
311 rue_csr_write_2(struct rue_softc
*sc
, int reg
, u_int16_t val
)
317 err
= rue_write_mem(sc
, reg
, &w
, 2);
326 rue_csr_write_4(struct rue_softc
*sc
, int reg
, u_int32_t val
)
332 err
= rue_write_mem(sc
, reg
, &dw
, 4);
341 rue_miibus_readreg(device_t dev
, int phy
, int reg
)
343 struct rue_softc
*sc
= device_get_softc(dev
);
347 if (phy
!= 0) /* RTL8150 supports PHY == 0, only */
371 if (RUE_REG_MIN
<= reg
&& reg
<= RUE_REG_MAX
) {
372 rval
= rue_csr_read_1(sc
, reg
);
375 if_printf(&sc
->arpcom
.ac_if
, "bad phy register\n");
379 rval
= rue_csr_read_2(sc
, ruereg
);
385 rue_miibus_writereg(device_t dev
, int phy
, int reg
, int data
)
387 struct rue_softc
*sc
= device_get_softc(dev
);
390 if (phy
!= 0) /* RTL8150 supports PHY == 0, only */
414 if (RUE_REG_MIN
<= reg
&& reg
<= RUE_REG_MAX
) {
415 rue_csr_write_1(sc
, reg
, data
);
418 if_printf(&sc
->arpcom
.ac_if
, "bad phy register\n");
421 rue_csr_write_2(sc
, ruereg
, data
);
427 rue_miibus_statchg(device_t dev
)
432 * Program the 64-bit multicast hash filter.
436 rue_setmulti(struct rue_softc
*sc
)
440 u_int32_t hashes
[2] = { 0, 0 };
441 struct ifmultiaddr
*ifma
;
445 ifp
= &sc
->arpcom
.ac_if
;
447 rxcfg
= rue_csr_read_2(sc
, RUE_RCR
);
449 if (ifp
->if_flags
& IFF_ALLMULTI
|| ifp
->if_flags
& IFF_PROMISC
) {
450 rxcfg
|= (RUE_RCR_AAM
| RUE_RCR_AAP
);
451 rxcfg
&= ~RUE_RCR_AM
;
452 rue_csr_write_2(sc
, RUE_RCR
, rxcfg
);
453 rue_csr_write_4(sc
, RUE_MAR0
, 0xFFFFFFFF);
454 rue_csr_write_4(sc
, RUE_MAR4
, 0xFFFFFFFF);
458 /* first, zot all the existing hash bits */
459 rue_csr_write_4(sc
, RUE_MAR0
, 0);
460 rue_csr_write_4(sc
, RUE_MAR4
, 0);
462 /* now program new ones */
463 LIST_FOREACH (ifma
, &ifp
->if_multiaddrs
, ifma_link
) {
464 if (ifma
->ifma_addr
->sa_family
!= AF_LINK
)
466 h
= ether_crc32_be(LLADDR((struct sockaddr_dl
*)
467 ifma
->ifma_addr
), ETHER_ADDR_LEN
) >> 26;
469 hashes
[0] |= (1 << h
);
471 hashes
[1] |= (1 << (h
- 32));
478 rxcfg
&= ~RUE_RCR_AM
;
480 rxcfg
&= ~(RUE_RCR_AAM
| RUE_RCR_AAP
);
482 rue_csr_write_2(sc
, RUE_RCR
, rxcfg
);
483 rue_csr_write_4(sc
, RUE_MAR0
, hashes
[0]);
484 rue_csr_write_4(sc
, RUE_MAR4
, hashes
[1]);
488 rue_reset(struct rue_softc
*sc
)
492 rue_csr_write_1(sc
, RUE_CR
, RUE_CR_SOFT_RST
);
494 for (i
= 0; i
< RUE_TIMEOUT
; i
++) {
496 if (!(rue_csr_read_1(sc
, RUE_CR
) & RUE_CR_SOFT_RST
))
499 if (i
== RUE_TIMEOUT
)
500 if_printf(&sc
->arpcom
.ac_if
, "reset never completed!\n");
506 * Probe for a RTL8150 chip.
510 rue_match(device_t self
)
512 struct usb_attach_arg
*uaa
= device_get_ivars(self
);
514 if (uaa
->iface
== NULL
)
515 return (UMATCH_NONE
);
517 return (usb_lookup(rue_devs
, uaa
->vendor
, uaa
->product
) != NULL
?
518 UMATCH_VENDOR_PRODUCT
: UMATCH_NONE
);
522 * Attach the interface. Allocate softc structures, do ifmedia
523 * setup and ethernet/BPF attach.
527 rue_attach(device_t self
)
529 struct rue_softc
*sc
= device_get_softc(self
);
530 struct usb_attach_arg
*uaa
= device_get_ivars(self
);
531 uint8_t eaddr
[ETHER_ADDR_LEN
];
533 usb_interface_descriptor_t
*id
;
534 usb_endpoint_descriptor_t
*ed
;
537 sc
->rue_udev
= uaa
->device
;
539 if (usbd_set_config_no(sc
->rue_udev
, RUE_CONFIG_NO
, 0)) {
540 device_printf(self
, "setting config no %d failed\n",
545 if (usbd_device2interface_handle(uaa
->device
, RUE_IFACE_IDX
,
547 device_printf(self
, "getting interface handle failed\n");
551 id
= usbd_get_interface_descriptor(sc
->rue_iface
);
554 for (i
= 0; i
< id
->bNumEndpoints
; i
++) {
555 ed
= usbd_interface2endpoint_descriptor(sc
->rue_iface
, i
);
557 device_printf(self
, "couldn't get ep %d\n", i
);
560 if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_IN
&&
561 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_BULK
) {
562 sc
->rue_ed
[RUE_ENDPT_RX
] = ed
->bEndpointAddress
;
563 } else if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_OUT
&&
564 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_BULK
) {
565 sc
->rue_ed
[RUE_ENDPT_TX
] = ed
->bEndpointAddress
;
566 } else if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_IN
&&
567 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_INTERRUPT
) {
568 sc
->rue_ed
[RUE_ENDPT_INTR
] = ed
->bEndpointAddress
;
572 ifp
= &sc
->arpcom
.ac_if
;
573 if_initname(ifp
, device_get_name(self
), device_get_unit(self
));
575 /* Reset the adapter */
578 /* Get station address from the EEPROM */
579 if (rue_read_mem(sc
, RUE_EEPROM_IDR0
, eaddr
, ETHER_ADDR_LEN
)) {
580 device_printf(self
, "couldn't get station address\n");
585 ifp
->if_mtu
= ETHERMTU
;
586 ifp
->if_flags
= IFF_BROADCAST
| IFF_SIMPLEX
| IFF_MULTICAST
;
587 ifp
->if_ioctl
= rue_ioctl
;
588 ifp
->if_start
= rue_start
;
589 ifp
->if_watchdog
= rue_watchdog
;
590 ifp
->if_init
= rue_init
;
591 ifp
->if_baudrate
= 10000000;
592 ifq_set_maxlen(&ifp
->if_snd
, IFQ_MAXLEN
);
593 ifq_set_ready(&ifp
->if_snd
);
596 if (mii_phy_probe(self
, &sc
->rue_miibus
,
597 rue_ifmedia_upd
, rue_ifmedia_sts
)) {
598 device_printf(self
, "MII without any PHY!\n");
602 /* Call MI attach routine */
603 ether_ifattach(ifp
, eaddr
, NULL
);
605 callout_init(&sc
->rue_stat_ch
);
608 usb_register_netisr();
614 rue_detach(device_t dev
)
616 struct rue_softc
*sc
;
619 sc
= device_get_softc(dev
);
621 ifp
= &sc
->arpcom
.ac_if
;
624 callout_stop(&sc
->rue_stat_ch
);
628 if (sc
->rue_ep
[RUE_ENDPT_TX
] != NULL
)
629 usbd_abort_pipe(sc
->rue_ep
[RUE_ENDPT_TX
]);
630 if (sc
->rue_ep
[RUE_ENDPT_RX
] != NULL
)
631 usbd_abort_pipe(sc
->rue_ep
[RUE_ENDPT_RX
]);
633 if (sc
->rue_ep
[RUE_ENDPT_INTR
] != NULL
)
634 usbd_abort_pipe(sc
->rue_ep
[RUE_ENDPT_INTR
]);
643 * Initialize an RX descriptor and attach an MBUF cluster.
647 rue_newbuf(struct rue_softc
*sc
, struct rue_chain
*c
, struct mbuf
*m
)
649 struct mbuf
*m_new
= NULL
;
652 m_new
= m_getcl(MB_DONTWAIT
, MT_DATA
, M_PKTHDR
);
654 if_printf(&sc
->arpcom
.ac_if
, "no memory for rx list "
655 "-- packet dropped!\n");
658 m_new
->m_len
= m_new
->m_pkthdr
.len
= MCLBYTES
;
661 m_new
->m_len
= m_new
->m_pkthdr
.len
= MCLBYTES
;
662 m_new
->m_data
= m_new
->m_ext
.ext_buf
;
665 m_adj(m_new
, ETHER_ALIGN
);
672 rue_rx_list_init(struct rue_softc
*sc
)
674 struct rue_cdata
*cd
;
679 for (i
= 0; i
< RUE_RX_LIST_CNT
; i
++) {
680 c
= &cd
->rue_rx_chain
[i
];
683 if (rue_newbuf(sc
, c
, NULL
) == ENOBUFS
)
685 if (c
->rue_xfer
== NULL
) {
686 c
->rue_xfer
= usbd_alloc_xfer(sc
->rue_udev
);
687 if (c
->rue_xfer
== NULL
)
696 rue_tx_list_init(struct rue_softc
*sc
)
698 struct rue_cdata
*cd
;
703 for (i
= 0; i
< RUE_TX_LIST_CNT
; i
++) {
704 c
= &cd
->rue_tx_chain
[i
];
708 if (c
->rue_xfer
== NULL
) {
709 c
->rue_xfer
= usbd_alloc_xfer(sc
->rue_udev
);
710 if (c
->rue_xfer
== NULL
)
713 c
->rue_buf
= kmalloc(RUE_BUFSZ
, M_USBDEV
, M_WAITOK
);
721 rue_intr(usbd_xfer_handle xfer
, usbd_private_handle priv
, usbd_status status
)
723 struct rue_softc
*sc
= priv
;
725 struct rue_intrpkt
*p
;
728 ifp
= &sc
->arpcom
.ac_if
;
730 if (!(ifp
->if_flags
& IFF_RUNNING
)) {
735 if (status
!= USBD_NORMAL_COMPLETION
) {
736 if (status
== USBD_NOT_STARTED
|| status
== USBD_CANCELLED
) {
740 if_printf(ifp
, "usb error on intr: %s\n", usbd_errstr(status
));
741 if (status
== USBD_STALLED
)
742 usbd_clear_endpoint_stall(sc
->rue_ep
[RUE_ENDPT_INTR
]);
747 usbd_get_xfer_status(xfer
, NULL
, (void **)&p
, NULL
, NULL
);
749 ifp
->if_ierrors
+= p
->rue_rxlost_cnt
;
750 ifp
->if_ierrors
+= p
->rue_crcerr_cnt
;
751 ifp
->if_collisions
+= p
->rue_col_cnt
;
758 rue_rxstart(struct ifnet
*ifp
)
760 struct rue_softc
*sc
;
765 c
= &sc
->rue_cdata
.rue_rx_chain
[sc
->rue_cdata
.rue_rx_prod
];
767 if (rue_newbuf(sc
, c
, NULL
) == ENOBUFS
) {
773 /* Setup new transfer. */
774 usbd_setup_xfer(c
->rue_xfer
, sc
->rue_ep
[RUE_ENDPT_RX
],
775 c
, mtod(c
->rue_mbuf
, char *), RUE_BUFSZ
, USBD_SHORT_XFER_OK
,
776 USBD_NO_TIMEOUT
, rue_rxeof
);
777 usbd_transfer(c
->rue_xfer
);
783 * A frame has been uploaded: pass the resulting mbuf chain up to
784 * the higher level protocols.
788 rue_rxeof(usbd_xfer_handle xfer
, usbd_private_handle priv
, usbd_status status
)
790 struct rue_chain
*c
= priv
;
791 struct rue_softc
*sc
= c
->rue_sc
;
800 ifp
= &sc
->arpcom
.ac_if
;
802 if (!(ifp
->if_flags
& IFF_RUNNING
)) {
807 if (status
!= USBD_NORMAL_COMPLETION
) {
808 if (status
== USBD_NOT_STARTED
|| status
== USBD_CANCELLED
) {
812 if (usbd_ratecheck(&sc
->rue_rx_notice
)) {
813 if_printf(ifp
, "usb error on rx: %s\n",
814 usbd_errstr(status
));
816 if (status
== USBD_STALLED
)
817 usbd_clear_endpoint_stall(sc
->rue_ep
[RUE_ENDPT_RX
]);
821 usbd_get_xfer_status(xfer
, NULL
, NULL
, &total_len
, NULL
);
823 if (total_len
<= ETHER_CRC_LEN
) {
829 bcopy(mtod(m
, char *) + total_len
- 4, (char *)&r
, sizeof (r
));
831 /* Check recieve packet was valid or not */
832 if ((r
.rue_rxstat
& RUE_RXSTAT_VALID
) == 0) {
837 /* No errors; receive the packet. */
838 total_len
-= ETHER_CRC_LEN
;
841 m
->m_pkthdr
.rcvif
= ifp
;
842 m
->m_pkthdr
.len
= m
->m_len
= total_len
;
844 /* Put the packet on the special USB input queue. */
852 /* Setup new transfer. */
853 usbd_setup_xfer(xfer
, sc
->rue_ep
[RUE_ENDPT_RX
],
854 c
, mtod(c
->rue_mbuf
, char *), RUE_BUFSZ
,
855 USBD_SHORT_XFER_OK
, USBD_NO_TIMEOUT
, rue_rxeof
);
861 * A frame was downloaded to the chip. It's safe for us to clean up
866 rue_txeof(usbd_xfer_handle xfer
, usbd_private_handle priv
, usbd_status status
)
868 struct rue_chain
*c
= priv
;
869 struct rue_softc
*sc
= c
->rue_sc
;
875 ifp
= &sc
->arpcom
.ac_if
;
877 if (status
!= USBD_NORMAL_COMPLETION
) {
878 if (status
== USBD_NOT_STARTED
|| status
== USBD_CANCELLED
) {
882 if_printf(ifp
, "usb error on tx: %s\n", usbd_errstr(status
));
883 if (status
== USBD_STALLED
)
884 usbd_clear_endpoint_stall(sc
->rue_ep
[RUE_ENDPT_TX
]);
890 ifp
->if_flags
&= ~IFF_OACTIVE
;
891 usbd_get_xfer_status(c
->rue_xfer
, NULL
, NULL
, NULL
, &err
);
893 if (c
->rue_mbuf
!= NULL
) {
894 m_freem(c
->rue_mbuf
);
903 if (!ifq_is_empty(&ifp
->if_snd
))
912 struct rue_softc
*sc
= xsc
;
914 struct mii_data
*mii
;
921 ifp
= &sc
->arpcom
.ac_if
;
929 if (!sc
->rue_link
&& mii
->mii_media_status
& IFM_ACTIVE
&&
930 IFM_SUBTYPE(mii
->mii_media_active
) != IFM_NONE
) {
932 if (!ifq_is_empty(&ifp
->if_snd
))
936 callout_reset(&sc
->rue_stat_ch
, hz
, rue_tick
, sc
);
942 rue_encap(struct rue_softc
*sc
, struct mbuf
*m
, int idx
)
948 c
= &sc
->rue_cdata
.rue_tx_chain
[idx
];
951 * Copy the mbuf data into a contiguous buffer
953 m_copydata(m
, 0, m
->m_pkthdr
.len
, c
->rue_buf
);
956 total_len
= m
->m_pkthdr
.len
;
959 * This is an undocumented behavior.
960 * RTL8150 chip doesn't send frame length smaller than
961 * RUE_MIN_FRAMELEN (60) byte packet.
963 if (total_len
< RUE_MIN_FRAMELEN
)
964 total_len
= RUE_MIN_FRAMELEN
;
966 usbd_setup_xfer(c
->rue_xfer
, sc
->rue_ep
[RUE_ENDPT_TX
],
967 c
, c
->rue_buf
, total_len
, USBD_FORCE_SHORT_XFER
,
971 err
= usbd_transfer(c
->rue_xfer
);
972 if (err
!= USBD_IN_PROGRESS
) {
977 sc
->rue_cdata
.rue_tx_cnt
++;
983 rue_start(struct ifnet
*ifp
)
985 struct rue_softc
*sc
= ifp
->if_softc
;
986 struct mbuf
*m_head
= NULL
;
991 ifq_purge(&ifp
->if_snd
);
996 if (ifp
->if_flags
& IFF_OACTIVE
) {
1001 m_head
= ifq_dequeue(&ifp
->if_snd
, NULL
);
1002 if (m_head
== NULL
) {
1007 if (rue_encap(sc
, m_head
, 0)) {
1008 /* rue_encap() will free m_head, if we reach here */
1009 ifp
->if_flags
|= IFF_OACTIVE
;
1015 * If there's a BPF listener, bounce a copy of this frame
1018 BPF_MTAP(ifp
, m_head
);
1020 ifp
->if_flags
|= IFF_OACTIVE
;
1023 * Set a timeout in case the chip goes out to lunch.
1033 struct rue_softc
*sc
= xsc
;
1034 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
1035 struct mii_data
*mii
= GET_MII(sc
);
1036 struct rue_chain
*c
;
1043 if (ifp
->if_flags
& IFF_RUNNING
) {
1049 * Cancel pending I/O and free all RX/TX buffers.
1053 /* Set MAC address */
1054 rue_write_mem(sc
, RUE_IDR0
, sc
->arpcom
.ac_enaddr
, ETHER_ADDR_LEN
);
1057 if (rue_tx_list_init(sc
) == ENOBUFS
) {
1058 if_printf(ifp
, "tx list init failed\n");
1064 if (rue_rx_list_init(sc
) == ENOBUFS
) {
1065 if_printf(ifp
, "rx list init failed\n");
1070 #ifdef RUE_INTR_PIPE
1071 sc
->rue_cdata
.rue_ibuf
= kmalloc(RUE_INTR_PKTLEN
, M_USBDEV
, M_WAITOK
);
1075 * Set the initial TX and RX configuration.
1077 rue_csr_write_1(sc
, RUE_TCR
, RUE_TCR_CONFIG
);
1079 rxcfg
= RUE_RCR_CONFIG
;
1081 /* Set capture broadcast bit to capture broadcast frames. */
1082 if (ifp
->if_flags
& IFF_BROADCAST
)
1083 rxcfg
|= RUE_RCR_AB
;
1085 rxcfg
&= ~RUE_RCR_AB
;
1087 /* If we want promiscuous mode, set the allframes bit. */
1088 if (ifp
->if_flags
& IFF_PROMISC
)
1089 rxcfg
|= RUE_RCR_AAP
;
1091 rxcfg
&= ~RUE_RCR_AAP
;
1093 rue_csr_write_2(sc
, RUE_RCR
, rxcfg
);
1095 /* Load the multicast filter. */
1098 /* Enable RX and TX */
1099 rue_csr_write_1(sc
, RUE_CR
, (RUE_CR_TE
| RUE_CR_RE
| RUE_CR_EP3CLREN
));
1103 /* Open RX and TX pipes. */
1104 err
= usbd_open_pipe(sc
->rue_iface
, sc
->rue_ed
[RUE_ENDPT_RX
],
1105 USBD_EXCLUSIVE_USE
, &sc
->rue_ep
[RUE_ENDPT_RX
]);
1107 if_printf(ifp
, "open rx pipe failed: %s\n", usbd_errstr(err
));
1111 err
= usbd_open_pipe(sc
->rue_iface
, sc
->rue_ed
[RUE_ENDPT_TX
],
1112 USBD_EXCLUSIVE_USE
, &sc
->rue_ep
[RUE_ENDPT_TX
]);
1114 if_printf(ifp
, "open tx pipe failed: %s\n", usbd_errstr(err
));
1119 #ifdef RUE_INTR_PIPE
1120 err
= usbd_open_pipe_intr(sc
->rue_iface
, sc
->rue_ed
[RUE_ENDPT_INTR
],
1122 &sc
->rue_ep
[RUE_ENDPT_INTR
], sc
,
1123 sc
->rue_cdata
.rue_ibuf
, RUE_INTR_PKTLEN
,
1124 rue_intr
, RUE_INTR_INTERVAL
);
1126 if_printf(ifp
, "open intr pipe failed: %s\n", usbd_errstr(err
));
1132 /* Start up the receive pipe. */
1133 for (i
= 0; i
< RUE_RX_LIST_CNT
; i
++) {
1134 c
= &sc
->rue_cdata
.rue_rx_chain
[i
];
1135 usbd_setup_xfer(c
->rue_xfer
, sc
->rue_ep
[RUE_ENDPT_RX
],
1136 c
, mtod(c
->rue_mbuf
, char *), RUE_BUFSZ
,
1137 USBD_SHORT_XFER_OK
, USBD_NO_TIMEOUT
, rue_rxeof
);
1138 usbd_transfer(c
->rue_xfer
);
1141 ifp
->if_flags
|= IFF_RUNNING
;
1142 ifp
->if_flags
&= ~IFF_OACTIVE
;
1144 callout_reset(&sc
->rue_stat_ch
, hz
, rue_tick
, sc
);
1150 * Set media options.
1154 rue_ifmedia_upd(struct ifnet
*ifp
)
1156 struct rue_softc
*sc
= ifp
->if_softc
;
1157 struct mii_data
*mii
= GET_MII(sc
);
1160 if (mii
->mii_instance
) {
1161 struct mii_softc
*miisc
;
1162 LIST_FOREACH (miisc
, &mii
->mii_phys
, mii_list
)
1163 mii_phy_reset(miisc
);
1171 * Report current media status.
1175 rue_ifmedia_sts(struct ifnet
*ifp
, struct ifmediareq
*ifmr
)
1177 struct rue_softc
*sc
= ifp
->if_softc
;
1178 struct mii_data
*mii
= GET_MII(sc
);
1181 ifmr
->ifm_active
= mii
->mii_media_active
;
1182 ifmr
->ifm_status
= mii
->mii_media_status
;
1186 rue_ioctl(struct ifnet
*ifp
, u_long command
, caddr_t data
, struct ucred
*cr
)
1188 struct rue_softc
*sc
= ifp
->if_softc
;
1189 struct ifreq
*ifr
= (struct ifreq
*)data
;
1190 struct mii_data
*mii
;
1197 if (ifp
->if_flags
& IFF_UP
) {
1198 if (ifp
->if_flags
& IFF_RUNNING
&&
1199 ifp
->if_flags
& IFF_PROMISC
&&
1200 !(sc
->rue_if_flags
& IFF_PROMISC
)) {
1201 RUE_SETBIT_2(sc
, RUE_RCR
,
1202 (RUE_RCR_AAM
| RUE_RCR_AAP
));
1204 } else if (ifp
->if_flags
& IFF_RUNNING
&&
1205 !(ifp
->if_flags
& IFF_PROMISC
) &&
1206 sc
->rue_if_flags
& IFF_PROMISC
) {
1207 RUE_CLRBIT_2(sc
, RUE_RCR
,
1208 (RUE_RCR_AAM
| RUE_RCR_AAP
));
1210 } else if (!(ifp
->if_flags
& IFF_RUNNING
))
1213 if (ifp
->if_flags
& IFF_RUNNING
)
1216 sc
->rue_if_flags
= ifp
->if_flags
;
1227 error
= ifmedia_ioctl(ifp
, ifr
, &mii
->mii_media
, command
);
1230 error
= ether_ioctl(ifp
, command
, data
);
1240 rue_watchdog(struct ifnet
*ifp
)
1242 struct rue_softc
*sc
= ifp
->if_softc
;
1243 struct rue_chain
*c
;
1249 if_printf(ifp
, "watchdog timeout\n");
1251 c
= &sc
->rue_cdata
.rue_tx_chain
[0];
1252 usbd_get_xfer_status(c
->rue_xfer
, NULL
, NULL
, NULL
, &stat
);
1253 rue_txeof(c
->rue_xfer
, c
, stat
);
1255 if (!ifq_is_empty(&ifp
->if_snd
))
1262 * Stop the adapter and free any mbufs allocated to the
1267 rue_stop(struct rue_softc
*sc
)
1275 ifp
= &sc
->arpcom
.ac_if
;
1278 rue_csr_write_1(sc
, RUE_CR
, 0x00);
1281 callout_stop(&sc
->rue_stat_ch
);
1283 /* Stop transfers. */
1284 if (sc
->rue_ep
[RUE_ENDPT_RX
] != NULL
) {
1285 err
= usbd_abort_pipe(sc
->rue_ep
[RUE_ENDPT_RX
]);
1287 if_printf(ifp
, "abort rx pipe failed: %s\n",
1290 err
= usbd_close_pipe(sc
->rue_ep
[RUE_ENDPT_RX
]);
1292 if_printf(ifp
, "close rx pipe failed: %s\n",
1295 sc
->rue_ep
[RUE_ENDPT_RX
] = NULL
;
1298 if (sc
->rue_ep
[RUE_ENDPT_TX
] != NULL
) {
1299 err
= usbd_abort_pipe(sc
->rue_ep
[RUE_ENDPT_TX
]);
1301 if_printf(ifp
, "abort tx pipe failed: %s\n",
1304 err
= usbd_close_pipe(sc
->rue_ep
[RUE_ENDPT_TX
]);
1306 if_printf(ifp
, "close tx pipe failed: %s\n",
1309 sc
->rue_ep
[RUE_ENDPT_TX
] = NULL
;
1312 #ifdef RUE_INTR_PIPE
1313 if (sc
->rue_ep
[RUE_ENDPT_INTR
] != NULL
) {
1314 err
= usbd_abort_pipe(sc
->rue_ep
[RUE_ENDPT_INTR
]);
1316 if_printf(ifp
, "abort intr pipe failed: %s\n",
1319 err
= usbd_close_pipe(sc
->rue_ep
[RUE_ENDPT_INTR
]);
1321 if_printf(ifp
, "close intr pipe failed: %s\n",
1324 sc
->rue_ep
[RUE_ENDPT_INTR
] = NULL
;
1328 /* Free RX resources. */
1329 for (i
= 0; i
< RUE_RX_LIST_CNT
; i
++) {
1330 if (sc
->rue_cdata
.rue_rx_chain
[i
].rue_buf
!= NULL
) {
1331 kfree(sc
->rue_cdata
.rue_rx_chain
[i
].rue_buf
, M_USBDEV
);
1332 sc
->rue_cdata
.rue_rx_chain
[i
].rue_buf
= NULL
;
1334 if (sc
->rue_cdata
.rue_rx_chain
[i
].rue_mbuf
!= NULL
) {
1335 m_freem(sc
->rue_cdata
.rue_rx_chain
[i
].rue_mbuf
);
1336 sc
->rue_cdata
.rue_rx_chain
[i
].rue_mbuf
= NULL
;
1338 if (sc
->rue_cdata
.rue_rx_chain
[i
].rue_xfer
!= NULL
) {
1339 usbd_free_xfer(sc
->rue_cdata
.rue_rx_chain
[i
].rue_xfer
);
1340 sc
->rue_cdata
.rue_rx_chain
[i
].rue_xfer
= NULL
;
1344 /* Free TX resources. */
1345 for (i
= 0; i
< RUE_TX_LIST_CNT
; i
++) {
1346 if (sc
->rue_cdata
.rue_tx_chain
[i
].rue_buf
!= NULL
) {
1347 kfree(sc
->rue_cdata
.rue_tx_chain
[i
].rue_buf
, M_USBDEV
);
1348 sc
->rue_cdata
.rue_tx_chain
[i
].rue_buf
= NULL
;
1350 if (sc
->rue_cdata
.rue_tx_chain
[i
].rue_mbuf
!= NULL
) {
1351 m_freem(sc
->rue_cdata
.rue_tx_chain
[i
].rue_mbuf
);
1352 sc
->rue_cdata
.rue_tx_chain
[i
].rue_mbuf
= NULL
;
1354 if (sc
->rue_cdata
.rue_tx_chain
[i
].rue_xfer
!= NULL
) {
1355 usbd_free_xfer(sc
->rue_cdata
.rue_tx_chain
[i
].rue_xfer
);
1356 sc
->rue_cdata
.rue_tx_chain
[i
].rue_xfer
= NULL
;
1360 #ifdef RUE_INTR_PIPE
1361 if (sc
->rue_cdata
.rue_ibuf
!= NULL
) {
1362 kfree(sc
->rue_cdata
.rue_ibuf
, M_USBDEV
);
1363 sc
->rue_cdata
.rue_ibuf
= NULL
;
1369 ifp
->if_flags
&= ~(IFF_RUNNING
| IFF_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
);