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.8 2007/06/28 06:32:32 hasso 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/usbdevs.h>
92 #include <bus/usb/usb_ethersubr.h>
94 #include <dev/netif/mii_layer/mii.h>
95 #include <dev/netif/mii_layer/miivar.h>
97 #include <dev/netif/rue/if_ruereg.h>
99 #include "miibus_if.h"
102 SYSCTL_NODE(_hw
, OID_AUTO
, rue
, CTLFLAG_RW
, 0, "USB rue");
104 static int rue_debug
= 0;
105 SYSCTL_INT(_hw_rue
, OID_AUTO
, debug
, CTLFLAG_RW
, &rue_debug
, 0,
108 /* XXX DPRINTF/DPRINTFN can be used only after rue_attach() */
109 #define DPRINTFN(n, x) do { if (rue_debug > (n)) if_printf x; } while (0)
111 #define DPRINTFN(n, x)
113 #define DPRINTF(x) DPRINTFN(0, x)
116 * Various supported device vendors/products.
119 static struct rue_type rue_devs
[] = {
120 { USB_VENDOR_MELCO
, USB_PRODUCT_MELCO_LUAKTX
},
121 { USB_VENDOR_REALTEK
, USB_PRODUCT_REALTEK_USBKR100
},
125 static int rue_match(device_t
);
126 static int rue_attach(device_t
);
127 static int rue_detach(device_t
);
129 static int rue_tx_list_init(struct rue_softc
*);
130 static int rue_rx_list_init(struct rue_softc
*);
131 static int rue_newbuf(struct rue_softc
*, struct rue_chain
*, struct mbuf
*);
132 static int rue_encap(struct rue_softc
*, struct mbuf
*, int);
134 static void rue_intr(usbd_xfer_handle
, usbd_private_handle
, usbd_status
);
136 static void rue_rxeof(usbd_xfer_handle
, usbd_private_handle
, usbd_status
);
137 static void rue_txeof(usbd_xfer_handle
, usbd_private_handle
, usbd_status
);
138 static void rue_tick(void *);
139 static void rue_rxstart(struct ifnet
*);
140 static void rue_start(struct ifnet
*);
141 static int rue_ioctl(struct ifnet
*, u_long
, caddr_t
, struct ucred
*);
142 static void rue_init(void *);
143 static void rue_stop(struct rue_softc
*);
144 static void rue_watchdog(struct ifnet
*);
145 static void rue_shutdown(device_t
);
146 static int rue_ifmedia_upd(struct ifnet
*);
147 static void rue_ifmedia_sts(struct ifnet
*, struct ifmediareq
*);
149 static int rue_miibus_readreg(device_t
, int, int);
150 static int rue_miibus_writereg(device_t
, int, int, int);
151 static void rue_miibus_statchg(device_t
);
153 static void rue_setmulti(struct rue_softc
*);
154 static void rue_reset(struct rue_softc
*);
156 static int rue_read_mem(struct rue_softc
*, u_int16_t
, void *, u_int16_t
);
157 static int rue_write_mem(struct rue_softc
*, u_int16_t
, void *, u_int16_t
);
158 static int rue_csr_read_1(struct rue_softc
*, int);
159 static int rue_csr_write_1(struct rue_softc
*, int, u_int8_t
);
160 static int rue_csr_read_2(struct rue_softc
*, int);
161 static int rue_csr_write_2(struct rue_softc
*, int, u_int16_t
);
162 static int rue_csr_write_4(struct rue_softc
*, int, u_int32_t
);
164 static device_method_t rue_methods
[] = {
165 /* Device interface */
166 DEVMETHOD(device_probe
, rue_match
),
167 DEVMETHOD(device_attach
, rue_attach
),
168 DEVMETHOD(device_detach
, rue_detach
),
169 DEVMETHOD(device_shutdown
, rue_shutdown
),
172 DEVMETHOD(bus_print_child
, bus_generic_print_child
),
173 DEVMETHOD(bus_driver_added
, bus_generic_driver_added
),
176 DEVMETHOD(miibus_readreg
, rue_miibus_readreg
),
177 DEVMETHOD(miibus_writereg
, rue_miibus_writereg
),
178 DEVMETHOD(miibus_statchg
, rue_miibus_statchg
),
183 static driver_t rue_driver
= {
186 sizeof(struct rue_softc
)
189 static devclass_t rue_devclass
;
191 DRIVER_MODULE(rue
, uhub
, rue_driver
, rue_devclass
, usbd_driver_load
, 0);
192 DRIVER_MODULE(miibus
, rue
, miibus_driver
, miibus_devclass
, 0, 0);
193 MODULE_DEPEND(rue
, usb
, 1, 1, 1);
194 MODULE_DEPEND(rue
, miibus
, 1, 1, 1);
196 #define RUE_SETBIT(sc, reg, x) \
197 rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) | (x))
199 #define RUE_CLRBIT(sc, reg, x) \
200 rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) & ~(x))
202 #define RUE_SETBIT_2(sc, reg, x) \
203 rue_csr_write_2(sc, reg, rue_csr_read_2(sc, reg) | (x))
205 #define RUE_CLRBIT_2(sc, reg, x) \
206 rue_csr_write_2(sc, reg, rue_csr_read_2(sc, reg) & ~(x))
209 rue_read_mem(struct rue_softc
*sc
, u_int16_t addr
, void *buf
, u_int16_t len
)
211 usb_device_request_t req
;
219 req
.bmRequestType
= UT_READ_VENDOR_DEVICE
;
220 req
.bRequest
= UR_SET_ADDRESS
;
221 USETW(req
.wValue
, addr
);
222 USETW(req
.wIndex
, 0);
223 USETW(req
.wLength
, len
);
225 err
= usbd_do_request(sc
->rue_udev
, &req
, buf
);
230 if_printf(&sc
->arpcom
.ac_if
, "control pipe read failed: %s\n",
239 rue_write_mem(struct rue_softc
*sc
, u_int16_t addr
, void *buf
, u_int16_t len
)
241 usb_device_request_t req
;
249 req
.bmRequestType
= UT_WRITE_VENDOR_DEVICE
;
250 req
.bRequest
= UR_SET_ADDRESS
;
251 USETW(req
.wValue
, addr
);
252 USETW(req
.wIndex
, 0);
253 USETW(req
.wLength
, len
);
255 err
= usbd_do_request(sc
->rue_udev
, &req
, buf
);
260 if_printf(&sc
->arpcom
.ac_if
, "control pipe write failed: %s\n",
269 rue_csr_read_1(struct rue_softc
*sc
, int reg
)
274 err
= rue_read_mem(sc
, reg
, &val
, 1);
283 rue_csr_read_2(struct rue_softc
*sc
, int reg
)
290 err
= rue_read_mem(sc
, reg
, &w
, 2);
300 rue_csr_write_1(struct rue_softc
*sc
, int reg
, u_int8_t val
)
304 err
= rue_write_mem(sc
, reg
, &val
, 1);
313 rue_csr_write_2(struct rue_softc
*sc
, int reg
, u_int16_t val
)
319 err
= rue_write_mem(sc
, reg
, &w
, 2);
328 rue_csr_write_4(struct rue_softc
*sc
, int reg
, u_int32_t val
)
334 err
= rue_write_mem(sc
, reg
, &dw
, 4);
343 rue_miibus_readreg(device_t dev
, int phy
, int reg
)
345 struct rue_softc
*sc
= device_get_softc(dev
);
349 if (phy
!= 0) /* RTL8150 supports PHY == 0, only */
373 if (RUE_REG_MIN
<= reg
&& reg
<= RUE_REG_MAX
) {
374 rval
= rue_csr_read_1(sc
, reg
);
377 if_printf(&sc
->arpcom
.ac_if
, "bad phy register\n");
381 rval
= rue_csr_read_2(sc
, ruereg
);
387 rue_miibus_writereg(device_t dev
, int phy
, int reg
, int data
)
389 struct rue_softc
*sc
= device_get_softc(dev
);
392 if (phy
!= 0) /* RTL8150 supports PHY == 0, only */
416 if (RUE_REG_MIN
<= reg
&& reg
<= RUE_REG_MAX
) {
417 rue_csr_write_1(sc
, reg
, data
);
420 if_printf(&sc
->arpcom
.ac_if
, "bad phy register\n");
423 rue_csr_write_2(sc
, ruereg
, data
);
429 rue_miibus_statchg(device_t dev
)
434 * Program the 64-bit multicast hash filter.
438 rue_setmulti(struct rue_softc
*sc
)
442 u_int32_t hashes
[2] = { 0, 0 };
443 struct ifmultiaddr
*ifma
;
447 ifp
= &sc
->arpcom
.ac_if
;
449 rxcfg
= rue_csr_read_2(sc
, RUE_RCR
);
451 if (ifp
->if_flags
& IFF_ALLMULTI
|| ifp
->if_flags
& IFF_PROMISC
) {
452 rxcfg
|= (RUE_RCR_AAM
| RUE_RCR_AAP
);
453 rxcfg
&= ~RUE_RCR_AM
;
454 rue_csr_write_2(sc
, RUE_RCR
, rxcfg
);
455 rue_csr_write_4(sc
, RUE_MAR0
, 0xFFFFFFFF);
456 rue_csr_write_4(sc
, RUE_MAR4
, 0xFFFFFFFF);
460 /* first, zot all the existing hash bits */
461 rue_csr_write_4(sc
, RUE_MAR0
, 0);
462 rue_csr_write_4(sc
, RUE_MAR4
, 0);
464 /* now program new ones */
465 LIST_FOREACH (ifma
, &ifp
->if_multiaddrs
, ifma_link
) {
466 if (ifma
->ifma_addr
->sa_family
!= AF_LINK
)
468 h
= ether_crc32_be(LLADDR((struct sockaddr_dl
*)
469 ifma
->ifma_addr
), ETHER_ADDR_LEN
) >> 26;
471 hashes
[0] |= (1 << h
);
473 hashes
[1] |= (1 << (h
- 32));
480 rxcfg
&= ~RUE_RCR_AM
;
482 rxcfg
&= ~(RUE_RCR_AAM
| RUE_RCR_AAP
);
484 rue_csr_write_2(sc
, RUE_RCR
, rxcfg
);
485 rue_csr_write_4(sc
, RUE_MAR0
, hashes
[0]);
486 rue_csr_write_4(sc
, RUE_MAR4
, hashes
[1]);
490 rue_reset(struct rue_softc
*sc
)
494 rue_csr_write_1(sc
, RUE_CR
, RUE_CR_SOFT_RST
);
496 for (i
= 0; i
< RUE_TIMEOUT
; i
++) {
498 if (!(rue_csr_read_1(sc
, RUE_CR
) & RUE_CR_SOFT_RST
))
501 if (i
== RUE_TIMEOUT
)
502 if_printf(&sc
->arpcom
.ac_if
, "reset never completed!\n");
508 * Probe for a RTL8150 chip.
513 USB_MATCH_START(rue
, uaa
);
516 if (uaa
->iface
== NULL
)
517 return (UMATCH_NONE
);
521 if (uaa
->vendor
== t
->rue_vid
&&
522 uaa
->product
== t
->rue_did
) {
523 return (UMATCH_VENDOR_PRODUCT
);
528 return (UMATCH_NONE
);
532 * Attach the interface. Allocate softc structures, do ifmedia
533 * setup and ethernet/BPF attach.
538 USB_ATTACH_START(rue
, sc
, uaa
);
540 uint8_t eaddr
[ETHER_ADDR_LEN
];
542 usb_interface_descriptor_t
*id
;
543 usb_endpoint_descriptor_t
*ed
;
546 sc
->rue_udev
= uaa
->device
;
548 if (usbd_set_config_no(sc
->rue_udev
, RUE_CONFIG_NO
, 0)) {
549 device_printf(self
, "setting config no %d failed\n",
551 USB_ATTACH_ERROR_RETURN
;
554 if (usbd_device2interface_handle(uaa
->device
, RUE_IFACE_IDX
,
556 device_printf(self
, "getting interface handle failed\n");
557 USB_ATTACH_ERROR_RETURN
;
560 id
= usbd_get_interface_descriptor(sc
->rue_iface
);
562 usbd_devinfo(uaa
->device
, 0, devinfo
);
563 device_set_desc_copy(self
, devinfo
);
564 device_printf(self
, "%s\n", devinfo
);
567 for (i
= 0; i
< id
->bNumEndpoints
; i
++) {
568 ed
= usbd_interface2endpoint_descriptor(sc
->rue_iface
, i
);
570 device_printf(self
, "couldn't get ep %d\n", i
);
571 USB_ATTACH_ERROR_RETURN
;
573 if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_IN
&&
574 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_BULK
) {
575 sc
->rue_ed
[RUE_ENDPT_RX
] = ed
->bEndpointAddress
;
576 } else if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_OUT
&&
577 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_BULK
) {
578 sc
->rue_ed
[RUE_ENDPT_TX
] = ed
->bEndpointAddress
;
579 } else if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_IN
&&
580 UE_GET_XFERTYPE(ed
->bmAttributes
) == UE_INTERRUPT
) {
581 sc
->rue_ed
[RUE_ENDPT_INTR
] = ed
->bEndpointAddress
;
585 ifp
= &sc
->arpcom
.ac_if
;
586 if_initname(ifp
, device_get_name(self
), device_get_unit(self
));
588 /* Reset the adapter */
591 /* Get station address from the EEPROM */
592 if (rue_read_mem(sc
, RUE_EEPROM_IDR0
, eaddr
, ETHER_ADDR_LEN
)) {
593 device_printf(self
, "couldn't get station address\n");
594 USB_ATTACH_ERROR_RETURN
;
598 ifp
->if_mtu
= ETHERMTU
;
599 ifp
->if_flags
= IFF_BROADCAST
| IFF_SIMPLEX
| IFF_MULTICAST
;
600 ifp
->if_ioctl
= rue_ioctl
;
601 ifp
->if_start
= rue_start
;
602 ifp
->if_watchdog
= rue_watchdog
;
603 ifp
->if_init
= rue_init
;
604 ifp
->if_baudrate
= 10000000;
605 ifq_set_maxlen(&ifp
->if_snd
, IFQ_MAXLEN
);
606 ifq_set_ready(&ifp
->if_snd
);
609 if (mii_phy_probe(self
, &sc
->rue_miibus
,
610 rue_ifmedia_upd
, rue_ifmedia_sts
)) {
611 device_printf(self
, "MII without any PHY!\n");
612 USB_ATTACH_ERROR_RETURN
;
615 /* Call MI attach routine */
616 ether_ifattach(ifp
, eaddr
, NULL
);
618 callout_init(&sc
->rue_stat_ch
);
621 usb_register_netisr();
623 USB_ATTACH_SUCCESS_RETURN
;
627 rue_detach(device_t dev
)
629 struct rue_softc
*sc
;
632 sc
= device_get_softc(dev
);
634 ifp
= &sc
->arpcom
.ac_if
;
637 callout_stop(&sc
->rue_stat_ch
);
641 if (sc
->rue_ep
[RUE_ENDPT_TX
] != NULL
)
642 usbd_abort_pipe(sc
->rue_ep
[RUE_ENDPT_TX
]);
643 if (sc
->rue_ep
[RUE_ENDPT_RX
] != NULL
)
644 usbd_abort_pipe(sc
->rue_ep
[RUE_ENDPT_RX
]);
646 if (sc
->rue_ep
[RUE_ENDPT_INTR
] != NULL
)
647 usbd_abort_pipe(sc
->rue_ep
[RUE_ENDPT_INTR
]);
656 * Initialize an RX descriptor and attach an MBUF cluster.
660 rue_newbuf(struct rue_softc
*sc
, struct rue_chain
*c
, struct mbuf
*m
)
662 struct mbuf
*m_new
= NULL
;
665 m_new
= m_getcl(MB_DONTWAIT
, MT_DATA
, M_PKTHDR
);
667 if_printf(&sc
->arpcom
.ac_if
, "no memory for rx list "
668 "-- packet dropped!\n");
671 m_new
->m_len
= m_new
->m_pkthdr
.len
= MCLBYTES
;
674 m_new
->m_len
= m_new
->m_pkthdr
.len
= MCLBYTES
;
675 m_new
->m_data
= m_new
->m_ext
.ext_buf
;
678 m_adj(m_new
, ETHER_ALIGN
);
685 rue_rx_list_init(struct rue_softc
*sc
)
687 struct rue_cdata
*cd
;
692 for (i
= 0; i
< RUE_RX_LIST_CNT
; i
++) {
693 c
= &cd
->rue_rx_chain
[i
];
696 if (rue_newbuf(sc
, c
, NULL
) == ENOBUFS
)
698 if (c
->rue_xfer
== NULL
) {
699 c
->rue_xfer
= usbd_alloc_xfer(sc
->rue_udev
);
700 if (c
->rue_xfer
== NULL
)
709 rue_tx_list_init(struct rue_softc
*sc
)
711 struct rue_cdata
*cd
;
716 for (i
= 0; i
< RUE_TX_LIST_CNT
; i
++) {
717 c
= &cd
->rue_tx_chain
[i
];
721 if (c
->rue_xfer
== NULL
) {
722 c
->rue_xfer
= usbd_alloc_xfer(sc
->rue_udev
);
723 if (c
->rue_xfer
== NULL
)
726 c
->rue_buf
= kmalloc(RUE_BUFSZ
, M_USBDEV
, M_WAITOK
);
734 rue_intr(usbd_xfer_handle xfer
, usbd_private_handle priv
, usbd_status status
)
736 struct rue_softc
*sc
= priv
;
738 struct rue_intrpkt
*p
;
741 ifp
= &sc
->arpcom
.ac_if
;
743 if (!(ifp
->if_flags
& IFF_RUNNING
)) {
748 if (status
!= USBD_NORMAL_COMPLETION
) {
749 if (status
== USBD_NOT_STARTED
|| status
== USBD_CANCELLED
) {
753 if_printf(ifp
, "usb error on intr: %s\n", usbd_errstr(status
));
754 if (status
== USBD_STALLED
)
755 usbd_clear_endpoint_stall(sc
->rue_ep
[RUE_ENDPT_INTR
]);
760 usbd_get_xfer_status(xfer
, NULL
, (void **)&p
, NULL
, NULL
);
762 ifp
->if_ierrors
+= p
->rue_rxlost_cnt
;
763 ifp
->if_ierrors
+= p
->rue_crcerr_cnt
;
764 ifp
->if_collisions
+= p
->rue_col_cnt
;
771 rue_rxstart(struct ifnet
*ifp
)
773 struct rue_softc
*sc
;
778 c
= &sc
->rue_cdata
.rue_rx_chain
[sc
->rue_cdata
.rue_rx_prod
];
780 if (rue_newbuf(sc
, c
, NULL
) == ENOBUFS
) {
786 /* Setup new transfer. */
787 usbd_setup_xfer(c
->rue_xfer
, sc
->rue_ep
[RUE_ENDPT_RX
],
788 c
, mtod(c
->rue_mbuf
, char *), RUE_BUFSZ
, USBD_SHORT_XFER_OK
,
789 USBD_NO_TIMEOUT
, rue_rxeof
);
790 usbd_transfer(c
->rue_xfer
);
796 * A frame has been uploaded: pass the resulting mbuf chain up to
797 * the higher level protocols.
801 rue_rxeof(usbd_xfer_handle xfer
, usbd_private_handle priv
, usbd_status status
)
803 struct rue_chain
*c
= priv
;
804 struct rue_softc
*sc
= c
->rue_sc
;
813 ifp
= &sc
->arpcom
.ac_if
;
815 if (!(ifp
->if_flags
& IFF_RUNNING
)) {
820 if (status
!= USBD_NORMAL_COMPLETION
) {
821 if (status
== USBD_NOT_STARTED
|| status
== USBD_CANCELLED
) {
825 if (usbd_ratecheck(&sc
->rue_rx_notice
)) {
826 if_printf(ifp
, "usb error on rx: %s\n",
827 usbd_errstr(status
));
829 if (status
== USBD_STALLED
)
830 usbd_clear_endpoint_stall(sc
->rue_ep
[RUE_ENDPT_RX
]);
834 usbd_get_xfer_status(xfer
, NULL
, NULL
, &total_len
, NULL
);
836 if (total_len
<= ETHER_CRC_LEN
) {
842 bcopy(mtod(m
, char *) + total_len
- 4, (char *)&r
, sizeof (r
));
844 /* Check recieve packet was valid or not */
845 if ((r
.rue_rxstat
& RUE_RXSTAT_VALID
) == 0) {
850 /* No errors; receive the packet. */
851 total_len
-= ETHER_CRC_LEN
;
854 m
->m_pkthdr
.rcvif
= ifp
;
855 m
->m_pkthdr
.len
= m
->m_len
= total_len
;
857 /* Put the packet on the special USB input queue. */
865 /* Setup new transfer. */
866 usbd_setup_xfer(xfer
, sc
->rue_ep
[RUE_ENDPT_RX
],
867 c
, mtod(c
->rue_mbuf
, char *), RUE_BUFSZ
,
868 USBD_SHORT_XFER_OK
, USBD_NO_TIMEOUT
, rue_rxeof
);
874 * A frame was downloaded to the chip. It's safe for us to clean up
879 rue_txeof(usbd_xfer_handle xfer
, usbd_private_handle priv
, usbd_status status
)
881 struct rue_chain
*c
= priv
;
882 struct rue_softc
*sc
= c
->rue_sc
;
888 ifp
= &sc
->arpcom
.ac_if
;
890 if (status
!= USBD_NORMAL_COMPLETION
) {
891 if (status
== USBD_NOT_STARTED
|| status
== USBD_CANCELLED
) {
895 if_printf(ifp
, "usb error on tx: %s\n", usbd_errstr(status
));
896 if (status
== USBD_STALLED
)
897 usbd_clear_endpoint_stall(sc
->rue_ep
[RUE_ENDPT_TX
]);
903 ifp
->if_flags
&= ~IFF_OACTIVE
;
904 usbd_get_xfer_status(c
->rue_xfer
, NULL
, NULL
, NULL
, &err
);
906 if (c
->rue_mbuf
!= NULL
) {
907 m_freem(c
->rue_mbuf
);
916 if (!ifq_is_empty(&ifp
->if_snd
))
925 struct rue_softc
*sc
= xsc
;
927 struct mii_data
*mii
;
934 ifp
= &sc
->arpcom
.ac_if
;
942 if (!sc
->rue_link
&& mii
->mii_media_status
& IFM_ACTIVE
&&
943 IFM_SUBTYPE(mii
->mii_media_active
) != IFM_NONE
) {
945 if (!ifq_is_empty(&ifp
->if_snd
))
949 callout_reset(&sc
->rue_stat_ch
, hz
, rue_tick
, sc
);
955 rue_encap(struct rue_softc
*sc
, struct mbuf
*m
, int idx
)
961 c
= &sc
->rue_cdata
.rue_tx_chain
[idx
];
964 * Copy the mbuf data into a contiguous buffer
966 m_copydata(m
, 0, m
->m_pkthdr
.len
, c
->rue_buf
);
969 total_len
= m
->m_pkthdr
.len
;
972 * This is an undocumented behavior.
973 * RTL8150 chip doesn't send frame length smaller than
974 * RUE_MIN_FRAMELEN (60) byte packet.
976 if (total_len
< RUE_MIN_FRAMELEN
)
977 total_len
= RUE_MIN_FRAMELEN
;
979 usbd_setup_xfer(c
->rue_xfer
, sc
->rue_ep
[RUE_ENDPT_TX
],
980 c
, c
->rue_buf
, total_len
, USBD_FORCE_SHORT_XFER
,
984 err
= usbd_transfer(c
->rue_xfer
);
985 if (err
!= USBD_IN_PROGRESS
) {
990 sc
->rue_cdata
.rue_tx_cnt
++;
996 rue_start(struct ifnet
*ifp
)
998 struct rue_softc
*sc
= ifp
->if_softc
;
999 struct mbuf
*m_head
= NULL
;
1003 if (!sc
->rue_link
) {
1008 if (ifp
->if_flags
& IFF_OACTIVE
) {
1013 m_head
= ifq_poll(&ifp
->if_snd
);
1014 if (m_head
== NULL
) {
1019 if (rue_encap(sc
, m_head
, 0)) {
1020 ifp
->if_flags
|= IFF_OACTIVE
;
1024 ifq_dequeue(&ifp
->if_snd
, m_head
);
1027 * If there's a BPF listener, bounce a copy of this frame
1030 BPF_MTAP(ifp
, m_head
);
1032 ifp
->if_flags
|= IFF_OACTIVE
;
1035 * Set a timeout in case the chip goes out to lunch.
1045 struct rue_softc
*sc
= xsc
;
1046 struct ifnet
*ifp
= &sc
->arpcom
.ac_if
;
1047 struct mii_data
*mii
= GET_MII(sc
);
1048 struct rue_chain
*c
;
1055 if (ifp
->if_flags
& IFF_RUNNING
) {
1061 * Cancel pending I/O and free all RX/TX buffers.
1065 /* Set MAC address */
1066 rue_write_mem(sc
, RUE_IDR0
, sc
->arpcom
.ac_enaddr
, ETHER_ADDR_LEN
);
1069 if (rue_tx_list_init(sc
) == ENOBUFS
) {
1070 if_printf(ifp
, "tx list init failed\n");
1076 if (rue_rx_list_init(sc
) == ENOBUFS
) {
1077 if_printf(ifp
, "rx list init failed\n");
1082 #ifdef RUE_INTR_PIPE
1083 sc
->rue_cdata
.rue_ibuf
= kmalloc(RUE_INTR_PKTLEN
, M_USBDEV
, M_WAITOK
);
1087 * Set the initial TX and RX configuration.
1089 rue_csr_write_1(sc
, RUE_TCR
, RUE_TCR_CONFIG
);
1091 rxcfg
= RUE_RCR_CONFIG
;
1093 /* Set capture broadcast bit to capture broadcast frames. */
1094 if (ifp
->if_flags
& IFF_BROADCAST
)
1095 rxcfg
|= RUE_RCR_AB
;
1097 rxcfg
&= ~RUE_RCR_AB
;
1099 /* If we want promiscuous mode, set the allframes bit. */
1100 if (ifp
->if_flags
& IFF_PROMISC
)
1101 rxcfg
|= RUE_RCR_AAP
;
1103 rxcfg
&= ~RUE_RCR_AAP
;
1105 rue_csr_write_2(sc
, RUE_RCR
, rxcfg
);
1107 /* Load the multicast filter. */
1110 /* Enable RX and TX */
1111 rue_csr_write_1(sc
, RUE_CR
, (RUE_CR_TE
| RUE_CR_RE
| RUE_CR_EP3CLREN
));
1115 /* Open RX and TX pipes. */
1116 err
= usbd_open_pipe(sc
->rue_iface
, sc
->rue_ed
[RUE_ENDPT_RX
],
1117 USBD_EXCLUSIVE_USE
, &sc
->rue_ep
[RUE_ENDPT_RX
]);
1119 if_printf(ifp
, "open rx pipe failed: %s\n", usbd_errstr(err
));
1123 err
= usbd_open_pipe(sc
->rue_iface
, sc
->rue_ed
[RUE_ENDPT_TX
],
1124 USBD_EXCLUSIVE_USE
, &sc
->rue_ep
[RUE_ENDPT_TX
]);
1126 if_printf(ifp
, "open tx pipe failed: %s\n", usbd_errstr(err
));
1131 #ifdef RUE_INTR_PIPE
1132 err
= usbd_open_pipe_intr(sc
->rue_iface
, sc
->rue_ed
[RUE_ENDPT_INTR
],
1134 &sc
->rue_ep
[RUE_ENDPT_INTR
], sc
,
1135 sc
->rue_cdata
.rue_ibuf
, RUE_INTR_PKTLEN
,
1136 rue_intr
, RUE_INTR_INTERVAL
);
1138 if_printf(ifp
, "open intr pipe failed: %s\n", usbd_errstr(err
));
1144 /* Start up the receive pipe. */
1145 for (i
= 0; i
< RUE_RX_LIST_CNT
; i
++) {
1146 c
= &sc
->rue_cdata
.rue_rx_chain
[i
];
1147 usbd_setup_xfer(c
->rue_xfer
, sc
->rue_ep
[RUE_ENDPT_RX
],
1148 c
, mtod(c
->rue_mbuf
, char *), RUE_BUFSZ
,
1149 USBD_SHORT_XFER_OK
, USBD_NO_TIMEOUT
, rue_rxeof
);
1150 usbd_transfer(c
->rue_xfer
);
1153 ifp
->if_flags
|= IFF_RUNNING
;
1154 ifp
->if_flags
&= ~IFF_OACTIVE
;
1156 callout_reset(&sc
->rue_stat_ch
, hz
, rue_tick
, sc
);
1162 * Set media options.
1166 rue_ifmedia_upd(struct ifnet
*ifp
)
1168 struct rue_softc
*sc
= ifp
->if_softc
;
1169 struct mii_data
*mii
= GET_MII(sc
);
1172 if (mii
->mii_instance
) {
1173 struct mii_softc
*miisc
;
1174 LIST_FOREACH (miisc
, &mii
->mii_phys
, mii_list
)
1175 mii_phy_reset(miisc
);
1183 * Report current media status.
1187 rue_ifmedia_sts(struct ifnet
*ifp
, struct ifmediareq
*ifmr
)
1189 struct rue_softc
*sc
= ifp
->if_softc
;
1190 struct mii_data
*mii
= GET_MII(sc
);
1193 ifmr
->ifm_active
= mii
->mii_media_active
;
1194 ifmr
->ifm_status
= mii
->mii_media_status
;
1198 rue_ioctl(struct ifnet
*ifp
, u_long command
, caddr_t data
, struct ucred
*cr
)
1200 struct rue_softc
*sc
= ifp
->if_softc
;
1201 struct ifreq
*ifr
= (struct ifreq
*)data
;
1202 struct mii_data
*mii
;
1209 if (ifp
->if_flags
& IFF_UP
) {
1210 if (ifp
->if_flags
& IFF_RUNNING
&&
1211 ifp
->if_flags
& IFF_PROMISC
&&
1212 !(sc
->rue_if_flags
& IFF_PROMISC
)) {
1213 RUE_SETBIT_2(sc
, RUE_RCR
,
1214 (RUE_RCR_AAM
| RUE_RCR_AAP
));
1216 } else if (ifp
->if_flags
& IFF_RUNNING
&&
1217 !(ifp
->if_flags
& IFF_PROMISC
) &&
1218 sc
->rue_if_flags
& IFF_PROMISC
) {
1219 RUE_CLRBIT_2(sc
, RUE_RCR
,
1220 (RUE_RCR_AAM
| RUE_RCR_AAP
));
1222 } else if (!(ifp
->if_flags
& IFF_RUNNING
))
1225 if (ifp
->if_flags
& IFF_RUNNING
)
1228 sc
->rue_if_flags
= ifp
->if_flags
;
1239 error
= ifmedia_ioctl(ifp
, ifr
, &mii
->mii_media
, command
);
1242 error
= ether_ioctl(ifp
, command
, data
);
1252 rue_watchdog(struct ifnet
*ifp
)
1254 struct rue_softc
*sc
= ifp
->if_softc
;
1255 struct rue_chain
*c
;
1261 if_printf(ifp
, "watchdog timeout\n");
1263 c
= &sc
->rue_cdata
.rue_tx_chain
[0];
1264 usbd_get_xfer_status(c
->rue_xfer
, NULL
, NULL
, NULL
, &stat
);
1265 rue_txeof(c
->rue_xfer
, c
, stat
);
1267 if (!ifq_is_empty(&ifp
->if_snd
))
1274 * Stop the adapter and free any mbufs allocated to the
1279 rue_stop(struct rue_softc
*sc
)
1287 ifp
= &sc
->arpcom
.ac_if
;
1290 rue_csr_write_1(sc
, RUE_CR
, 0x00);
1293 callout_stop(&sc
->rue_stat_ch
);
1295 /* Stop transfers. */
1296 if (sc
->rue_ep
[RUE_ENDPT_RX
] != NULL
) {
1297 err
= usbd_abort_pipe(sc
->rue_ep
[RUE_ENDPT_RX
]);
1299 if_printf(ifp
, "abort rx pipe failed: %s\n",
1302 err
= usbd_close_pipe(sc
->rue_ep
[RUE_ENDPT_RX
]);
1304 if_printf(ifp
, "close rx pipe failed: %s\n",
1307 sc
->rue_ep
[RUE_ENDPT_RX
] = NULL
;
1310 if (sc
->rue_ep
[RUE_ENDPT_TX
] != NULL
) {
1311 err
= usbd_abort_pipe(sc
->rue_ep
[RUE_ENDPT_TX
]);
1313 if_printf(ifp
, "abort tx pipe failed: %s\n",
1316 err
= usbd_close_pipe(sc
->rue_ep
[RUE_ENDPT_TX
]);
1318 if_printf(ifp
, "close tx pipe failed: %s\n",
1321 sc
->rue_ep
[RUE_ENDPT_TX
] = NULL
;
1324 #ifdef RUE_INTR_PIPE
1325 if (sc
->rue_ep
[RUE_ENDPT_INTR
] != NULL
) {
1326 err
= usbd_abort_pipe(sc
->rue_ep
[RUE_ENDPT_INTR
]);
1328 if_printf(ifp
, "abort intr pipe failed: %s\n",
1331 err
= usbd_close_pipe(sc
->rue_ep
[RUE_ENDPT_INTR
]);
1333 if_printf(ifp
, "close intr pipe failed: %s\n",
1336 sc
->rue_ep
[RUE_ENDPT_INTR
] = NULL
;
1340 /* Free RX resources. */
1341 for (i
= 0; i
< RUE_RX_LIST_CNT
; i
++) {
1342 if (sc
->rue_cdata
.rue_rx_chain
[i
].rue_buf
!= NULL
) {
1343 kfree(sc
->rue_cdata
.rue_rx_chain
[i
].rue_buf
, M_USBDEV
);
1344 sc
->rue_cdata
.rue_rx_chain
[i
].rue_buf
= NULL
;
1346 if (sc
->rue_cdata
.rue_rx_chain
[i
].rue_mbuf
!= NULL
) {
1347 m_freem(sc
->rue_cdata
.rue_rx_chain
[i
].rue_mbuf
);
1348 sc
->rue_cdata
.rue_rx_chain
[i
].rue_mbuf
= NULL
;
1350 if (sc
->rue_cdata
.rue_rx_chain
[i
].rue_xfer
!= NULL
) {
1351 usbd_free_xfer(sc
->rue_cdata
.rue_rx_chain
[i
].rue_xfer
);
1352 sc
->rue_cdata
.rue_rx_chain
[i
].rue_xfer
= NULL
;
1356 /* Free TX resources. */
1357 for (i
= 0; i
< RUE_TX_LIST_CNT
; i
++) {
1358 if (sc
->rue_cdata
.rue_tx_chain
[i
].rue_buf
!= NULL
) {
1359 kfree(sc
->rue_cdata
.rue_tx_chain
[i
].rue_buf
, M_USBDEV
);
1360 sc
->rue_cdata
.rue_tx_chain
[i
].rue_buf
= NULL
;
1362 if (sc
->rue_cdata
.rue_tx_chain
[i
].rue_mbuf
!= NULL
) {
1363 m_freem(sc
->rue_cdata
.rue_tx_chain
[i
].rue_mbuf
);
1364 sc
->rue_cdata
.rue_tx_chain
[i
].rue_mbuf
= NULL
;
1366 if (sc
->rue_cdata
.rue_tx_chain
[i
].rue_xfer
!= NULL
) {
1367 usbd_free_xfer(sc
->rue_cdata
.rue_tx_chain
[i
].rue_xfer
);
1368 sc
->rue_cdata
.rue_tx_chain
[i
].rue_xfer
= NULL
;
1372 #ifdef RUE_INTR_PIPE
1373 if (sc
->rue_cdata
.rue_ibuf
!= NULL
) {
1374 kfree(sc
->rue_cdata
.rue_ibuf
, M_USBDEV
);
1375 sc
->rue_cdata
.rue_ibuf
= NULL
;
1381 ifp
->if_flags
&= ~(IFF_RUNNING
| IFF_OACTIVE
);
1387 * Stop all chip I/O so that the kernel's probe routines don't
1388 * get confused by errant DMAs when rebooting.
1392 rue_shutdown(device_t dev
)
1394 struct rue_softc
*sc
;
1396 sc
= device_get_softc(dev
);