Date: Tue, 1 Dec 2015 05:12:13 +0000 (UTC) From: Kevin Lo <kevlo@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r291557 - in head: share/man/man4 sys/conf sys/dev/usb sys/dev/usb/net sys/modules/usb sys/modules/usb/ure Message-ID: <201512010512.tB15CDgV007432@repo.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: kevlo Date: Tue Dec 1 05:12:13 2015 New Revision: 291557 URL: https://svnweb.freebsd.org/changeset/base/291557 Log: Add initial support for RTL8152 USB Fast Ethernet. RTL8152 supports IPv4/IPv6 checksum offloading and VLAN tag insertion/stripping. Since uether doesn't provide a way to announce driver specific offload capabilities to upper stack, checksum offloading support needs more work and will be done in the future. Special thanks to Hayes Wang from RealTek who gave input. Added: head/share/man/man4/ure.4 (contents, props changed) head/sys/dev/usb/net/if_ure.c (contents, props changed) head/sys/dev/usb/net/if_urereg.h (contents, props changed) head/sys/modules/usb/ure/ head/sys/modules/usb/ure/Makefile (contents, props changed) Modified: head/share/man/man4/Makefile head/share/man/man4/miibus.4 head/sys/conf/NOTES head/sys/conf/files head/sys/dev/usb/usbdevs head/sys/modules/usb/Makefile Modified: head/share/man/man4/Makefile ============================================================================== --- head/share/man/man4/Makefile Tue Dec 1 04:15:34 2015 (r291556) +++ head/share/man/man4/Makefile Tue Dec 1 05:12:13 2015 (r291557) @@ -521,6 +521,7 @@ MAN= aac.4 \ tws.4 \ tx.4 \ txp.4 \ + ure.4 \ vale.4 \ vga.4 \ vge.4 \ @@ -710,6 +711,7 @@ MLINKS+=tl.4 if_tl.4 MLINKS+=tun.4 if_tun.4 MLINKS+=tx.4 if_tx.4 MLINKS+=txp.4 if_txp.4 +MLINKS+=ure.4 if_ure.4 MLINKS+=vge.4 if_vge.4 MLINKS+=vlan.4 if_vlan.4 MLINKS+=vxlan.4 if_vxlan.4 Modified: head/share/man/man4/miibus.4 ============================================================================== --- head/share/man/man4/miibus.4 Tue Dec 1 04:15:34 2015 (r291556) +++ head/share/man/man4/miibus.4 Tue Dec 1 05:12:13 2015 (r291557) @@ -8,7 +8,7 @@ .\" .\" $FreeBSD$ .\" -.Dd January 5, 2015 +.Dd December 1, 2015 .Dt MIIBUS 4 .Os .Sh NAME @@ -117,6 +117,8 @@ Texas Instruments ThunderLAN SMC EtherPower II (83c170) .It Xr udav 4 Davicom DM9601 USB Ethernet +.It Xr ure 4 +RealTek RTL8152 USB To Fast Ethernet .It Xr vge 4 VIA VT612x PCI Gigabit Ethernet .It Xr vr 4 @@ -177,6 +179,7 @@ but as a result are not well behaved new .Xr tl 4 , .Xr tx 4 , .Xr udav 4 , +.Xr ure 4 , .Xr vge 4 , .Xr vr 4 , .Xr vte 4 , Added: head/share/man/man4/ure.4 ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/share/man/man4/ure.4 Tue Dec 1 05:12:13 2015 (r291557) @@ -0,0 +1,121 @@ +.\" +.\" Copyright (c) 2015 Kevin Lo <kevlo@FreeBSD.org> +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd December 1, 2015 +.Dt URE 4 +.Os +.Sh NAME +.Nm ure +.Nd "RealTek RTL8152 USB to Fast Ethernet controller driver" +.Sh SYNOPSIS +To compile this driver into the kernel, +place the following lines in your +kernel configuration file: +.Bd -ragged -offset indent +.Cd "device uhci" +.Cd "device ohci" +.Cd "device usb" +.Cd "device miibus" +.Cd "device uether" +.Cd "device ure" +.Ed +.Pp +Alternatively, to load the driver as a +module at boot time, place the following line in +.Xr loader.conf 5 : +.Bd -literal -offset indent +if_ure_load="YES" +.Ed +.Sh DESCRIPTION +The +.Nm +driver provides support for USB Ethernet adapters based on the RealTek +RTL8152 USB to Fast Ethernet controller chip. +.Pp +The RTL8152 contains an integrated Fast Ethernet MAC, which supports +both 10 and 100Mbps speeds in either full or half duplex. +.Pp +The +.Nm +driver supports the following media types: +.Bl -tag -width ".Cm 10baseT/UTP" +.It Cm autoselect +Enable auto selection of the media type and options. +The user can manually override +the auto selected mode by adding media options to the +.Pa /etc/rc.conf +file. +.It Cm 10baseT/UTP +Set 10Mbps operation. +The +.Cm mediaopt +option can also be used to select either +.Cm full-duplex +or +.Cm half-duplex +modes. +.It Cm 100baseTX +Set 100Mbps (Fast Ethernet) operation. +The +.Cm mediaopt +option can also be used to select either +.Cm full-duplex +or +.Cm half-duplex +modes. +.El +.Pp +The +.Nm +driver supports the following media options: +.Bl -tag -width ".Cm 10baseT/UTP" +.It Cm full-duplex +Force full duplex operation. +.It Cm half-duplex +Force half duplex operation. +.El +.Pp +For more information on configuring this device, see +.Xr ifconfig 8 . +.Sh DIAGNOSTICS +.Bl -diag +.It "ure%d: watchdog timeout" +A packet was queued for transmission and a transmit command was +issued, however the device failed to acknowledge the transmission +before a timeout expired. +.El +.Sh SEE ALSO +.Xr arp 4 , +.Xr miibus 4 , +.Xr netintro 4 , +.Xr ng_ether 4 , +.Xr ifconfig 8 +.Sh AUTHORS +The +.Nm +driver was written by +.An Kevin Lo Aq Mt kevlo@FreeBSD.org . Modified: head/sys/conf/NOTES ============================================================================== --- head/sys/conf/NOTES Tue Dec 1 04:15:34 2015 (r291556) +++ head/sys/conf/NOTES Tue Dec 1 05:12:13 2015 (r291557) @@ -2740,6 +2740,9 @@ device rue # Davicom DM9601E USB to fast ethernet. Supports the Corega FEther USB-TXC. device udav # +# RealTek RTL8152 USB to fast ethernet. +device ure +# # Moschip MCS7730/MCS7840 USB to fast ethernet. Supports the Sitecom LN030. device mos # Modified: head/sys/conf/files ============================================================================== --- head/sys/conf/files Tue Dec 1 04:15:34 2015 (r291556) +++ head/sys/conf/files Tue Dec 1 05:12:13 2015 (r291557) @@ -2612,12 +2612,13 @@ dev/usb/net/if_mos.c optional mos dev/usb/net/if_rue.c optional rue dev/usb/net/if_smsc.c optional smsc dev/usb/net/if_udav.c optional udav +dev/usb/net/if_ure.c optional ure dev/usb/net/if_usie.c optional usie dev/usb/net/if_urndis.c optional urndis dev/usb/net/ruephy.c optional rue dev/usb/net/usb_ethernet.c optional uether | aue | axe | axge | cdce | \ cue | ipheth | kue | mos | rue | \ - smsc | udav | urndis + smsc | udav | ure | urndis dev/usb/net/uhso.c optional uhso # # USB WLAN drivers Added: head/sys/dev/usb/net/if_ure.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sys/dev/usb/net/if_ure.c Tue Dec 1 05:12:13 2015 (r291557) @@ -0,0 +1,1070 @@ +/*- + * Copyright (c) 2015 Kevin Lo <kevlo@FreeBSD.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/bus.h> +#include <sys/condvar.h> +#include <sys/kernel.h> +#include <sys/lock.h> +#include <sys/module.h> +#include <sys/mutex.h> +#include <sys/socket.h> +#include <sys/sysctl.h> +#include <sys/unistd.h> + +#include <net/if.h> +#include <net/if_var.h> + +#include <dev/usb/usb.h> +#include <dev/usb/usbdi.h> +#include <dev/usb/usbdi_util.h> +#include "usbdevs.h" + +#define USB_DEBUG_VAR ure_debug +#include <dev/usb/usb_debug.h> +#include <dev/usb/usb_process.h> + +#include <dev/usb/net/usb_ethernet.h> +#include <dev/usb/net/if_urereg.h> + +#ifdef USB_DEBUG +static int ure_debug = 0; + +static SYSCTL_NODE(_hw_usb, OID_AUTO, ure, CTLFLAG_RW, 0, "USB ure"); +SYSCTL_INT(_hw_usb_ure, OID_AUTO, debug, CTLFLAG_RWTUN, &ure_debug, 0, + "Debug level"); +#endif + +/* + * Various supported device vendors/products. + */ +static const STRUCT_USB_HOST_ID ure_devs[] = { +#define URE_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) } + URE_DEV(REALTEK, RTL8152), +#undef URE_DEV +}; + +static device_probe_t ure_probe; +static device_attach_t ure_attach; +static device_detach_t ure_detach; + +static usb_callback_t ure_bulk_read_callback; +static usb_callback_t ure_bulk_write_callback; + +static miibus_readreg_t ure_miibus_readreg; +static miibus_writereg_t ure_miibus_writereg; +static miibus_statchg_t ure_miibus_statchg; + +static uether_fn_t ure_attach_post; +static uether_fn_t ure_init; +static uether_fn_t ure_stop; +static uether_fn_t ure_start; +static uether_fn_t ure_tick; +static uether_fn_t ure_setmulti; +static uether_fn_t ure_setpromisc; + +static int ure_ctl(struct ure_softc *, uint8_t, uint16_t, uint16_t, + void *, int); +static int ure_read_mem(struct ure_softc *, uint16_t, uint16_t, void *, + int); +static int ure_write_mem(struct ure_softc *, uint16_t, uint16_t, void *, + int); +static uint8_t ure_read_1(struct ure_softc *, uint16_t, uint16_t); +static uint16_t ure_read_2(struct ure_softc *, uint16_t, uint16_t); +static uint32_t ure_read_4(struct ure_softc *, uint16_t, uint16_t); +static int ure_write_1(struct ure_softc *, uint16_t, uint16_t, uint32_t); +static int ure_write_2(struct ure_softc *, uint16_t, uint16_t, uint32_t); +static int ure_write_4(struct ure_softc *, uint16_t, uint16_t, uint32_t); +static uint16_t ure_ocp_reg_read(struct ure_softc *, uint16_t); +static void ure_ocp_reg_write(struct ure_softc *, uint16_t, uint16_t); + +static void ure_read_chipver(struct ure_softc *); +static int ure_attach_post_sub(struct usb_ether *); +static void ure_reset(struct ure_softc *); +static int ure_ifmedia_upd(struct ifnet *); +static void ure_ifmedia_sts(struct ifnet *, struct ifmediareq *); +static int ure_ioctl(struct ifnet *, u_long, caddr_t); +static void ure_rtl8152_init(struct ure_softc *); +static void ure_disable_teredo(struct ure_softc *); +static void ure_init_fifo(struct ure_softc *); + +static const struct usb_config ure_config[URE_N_TRANSFER] = { + [URE_BULK_DT_WR] = { + .type = UE_BULK, + .endpoint = UE_ADDR_ANY, + .direction = UE_DIR_OUT, + .bufsize = MCLBYTES, + .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, + .callback = ure_bulk_write_callback, + .timeout = 10000, /* 10 seconds */ + }, + [URE_BULK_DT_RD] = { + .type = UE_BULK, + .endpoint = UE_ADDR_ANY, + .direction = UE_DIR_IN, + .bufsize = MCLBYTES, + .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, + .callback = ure_bulk_read_callback, + .timeout = 0, /* no timeout */ + }, +}; + +static device_method_t ure_methods[] = { + /* Device interface. */ + DEVMETHOD(device_probe, ure_probe), + DEVMETHOD(device_attach, ure_attach), + DEVMETHOD(device_detach, ure_detach), + + /* MII interface. */ + DEVMETHOD(miibus_readreg, ure_miibus_readreg), + DEVMETHOD(miibus_writereg, ure_miibus_writereg), + DEVMETHOD(miibus_statchg, ure_miibus_statchg), + + DEVMETHOD_END +}; + +static driver_t ure_driver = { + .name = "ure", + .methods = ure_methods, + .size = sizeof(struct ure_softc), +}; + +static devclass_t ure_devclass; + +DRIVER_MODULE(ure, uhub, ure_driver, ure_devclass, NULL, NULL); +DRIVER_MODULE(miibus, ure, miibus_driver, miibus_devclass, NULL, NULL); +MODULE_DEPEND(ure, uether, 1, 1, 1); +MODULE_DEPEND(ure, usb, 1, 1, 1); +MODULE_DEPEND(ure, ether, 1, 1, 1); +MODULE_DEPEND(ure, miibus, 1, 1, 1); +MODULE_VERSION(ure, 1); + +static const struct usb_ether_methods ure_ue_methods = { + .ue_attach_post = ure_attach_post, + .ue_attach_post_sub = ure_attach_post_sub, + .ue_start = ure_start, + .ue_init = ure_init, + .ue_stop = ure_stop, + .ue_tick = ure_tick, + .ue_setmulti = ure_setmulti, + .ue_setpromisc = ure_setpromisc, + .ue_mii_upd = ure_ifmedia_upd, + .ue_mii_sts = ure_ifmedia_sts, +}; + +static int +ure_ctl(struct ure_softc *sc, uint8_t rw, uint16_t val, uint16_t index, + void *buf, int len) +{ + struct usb_device_request req; + + URE_LOCK_ASSERT(sc, MA_OWNED); + + if (rw == URE_CTL_WRITE) + req.bmRequestType = UT_WRITE_VENDOR_DEVICE; + else + req.bmRequestType = UT_READ_VENDOR_DEVICE; + req.bRequest = UR_SET_ADDRESS; + USETW(req.wValue, val); + USETW(req.wIndex, index); + USETW(req.wLength, len); + + return (uether_do_request(&sc->sc_ue, &req, buf, 1000)); +} + +static int +ure_read_mem(struct ure_softc *sc, uint16_t addr, uint16_t index, + void *buf, int len) +{ + + return (ure_ctl(sc, URE_CTL_READ, addr, index, buf, len)); +} + +static int +ure_write_mem(struct ure_softc *sc, uint16_t addr, uint16_t index, + void *buf, int len) +{ + + return (ure_ctl(sc, URE_CTL_WRITE, addr, index, buf, len)); +} + +static uint8_t +ure_read_1(struct ure_softc *sc, uint16_t reg, uint16_t index) +{ + uint32_t val; + uint8_t temp[4]; + uint8_t shift; + + shift = (reg & 3) << 3; + reg &= ~3; + + ure_read_mem(sc, reg, index, &temp, 4); + val = UGETDW(temp); + val >>= shift; + + return (val & 0xff); +} + +static uint16_t +ure_read_2(struct ure_softc *sc, uint16_t reg, uint16_t index) +{ + uint32_t val; + uint8_t temp[4]; + uint8_t shift; + + shift = (reg & 2) << 3; + reg &= ~3; + + ure_read_mem(sc, reg, index, &temp, 4); + val = UGETDW(temp); + val >>= shift; + + return (val & 0xffff); +} + +static uint32_t +ure_read_4(struct ure_softc *sc, uint16_t reg, uint16_t index) +{ + uint8_t temp[4]; + + ure_read_mem(sc, reg, index, &temp, 4); + return (UGETDW(temp)); +} + +static int +ure_write_1(struct ure_softc *sc, uint16_t reg, uint16_t index, uint32_t val) +{ + uint16_t byen; + uint8_t temp[4]; + uint8_t shift; + + byen = URE_BYTE_EN_BYTE; + shift = reg & 3; + val &= 0xff; + + if (reg & 3) { + byen <<= shift; + val <<= (shift << 3); + reg &= ~3; + } + + USETDW(temp, val); + return (ure_write_mem(sc, reg, index | byen, &temp, 4)); +} + +static int +ure_write_2(struct ure_softc *sc, uint16_t reg, uint16_t index, uint32_t val) +{ + uint16_t byen; + uint8_t temp[4]; + uint8_t shift; + + byen = URE_BYTE_EN_WORD; + shift = reg & 2; + val &= 0xffff; + + if (reg & 2) { + byen <<= shift; + val <<= (shift << 3); + reg &= ~3; + } + + USETDW(temp, val); + return (ure_write_mem(sc, reg, index | byen, &temp, 4)); +} + +static int +ure_write_4(struct ure_softc *sc, uint16_t reg, uint16_t index, uint32_t val) +{ + uint8_t temp[4]; + + USETDW(temp, val); + return (ure_write_mem(sc, reg, index | URE_BYTE_EN_DWORD, &temp, 4)); +} + +static uint16_t +ure_ocp_reg_read(struct ure_softc *sc, uint16_t addr) +{ + uint16_t reg; + + ure_write_2(sc, URE_PLA_OCP_GPHY_BASE, URE_MCU_TYPE_PLA, addr & 0xf000); + reg = (addr & 0x0fff) | 0xb000; + + return (ure_read_2(sc, reg, URE_MCU_TYPE_PLA)); +} + +static void +ure_ocp_reg_write(struct ure_softc *sc, uint16_t addr, uint16_t data) +{ + uint16_t reg; + + ure_write_2(sc, URE_PLA_OCP_GPHY_BASE, URE_MCU_TYPE_PLA, addr & 0xf000); + reg = (addr & 0x0fff) | 0xb000; + + ure_write_2(sc, reg, URE_MCU_TYPE_PLA, data); +} + +static int +ure_miibus_readreg(device_t dev, int phy, int reg) +{ + struct ure_softc *sc; + uint16_t val; + int locked; + + sc = device_get_softc(dev); + locked = mtx_owned(&sc->sc_mtx); + if (!locked) + URE_LOCK(sc); + + val = ure_ocp_reg_read(sc, URE_OCP_BASE_MII + reg * 2); + + if (!locked) + URE_UNLOCK(sc); + return (val); +} + +static int +ure_miibus_writereg(device_t dev, int phy, int reg, int val) +{ + struct ure_softc *sc; + int locked; + + sc = device_get_softc(dev); + if (sc->sc_phyno != phy) + return (0); + + locked = mtx_owned(&sc->sc_mtx); + if (!locked) + URE_LOCK(sc); + + ure_ocp_reg_write(sc, URE_OCP_BASE_MII + reg * 2, val); + + if (!locked) + URE_UNLOCK(sc); + return (0); +} + +static void +ure_miibus_statchg(device_t dev) +{ + struct ure_softc *sc; + struct mii_data *mii; + struct ifnet *ifp; + int locked; + + sc = device_get_softc(dev); + mii = GET_MII(sc); + locked = mtx_owned(&sc->sc_mtx); + if (!locked) + URE_LOCK(sc); + + ifp = uether_getifp(&sc->sc_ue); + if (mii == NULL || ifp == NULL || + (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) + goto done; + + sc->sc_flags &= ~URE_FLAG_LINK; + if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) == + (IFM_ACTIVE | IFM_AVALID)) { + switch (IFM_SUBTYPE(mii->mii_media_active)) { + case IFM_10_T: + case IFM_100_TX: + sc->sc_flags |= URE_FLAG_LINK; + break; + default: + break; + } + } + + /* Lost link, do nothing. */ + if ((sc->sc_flags & URE_FLAG_LINK) == 0) + goto done; +done: + if (!locked) + URE_UNLOCK(sc); +} + +/* + * Probe for a RTL8152 chip. + */ +static int +ure_probe(device_t dev) +{ + struct usb_attach_arg *uaa; + + uaa = device_get_ivars(dev);; + if (uaa->usb_mode != USB_MODE_HOST) + return (ENXIO); + if (uaa->info.bConfigIndex != URE_CONFIG_IDX) + return (ENXIO); + if (uaa->info.bIfaceIndex != URE_IFACE_IDX) + return (ENXIO); + + return (usbd_lookup_id_by_uaa(ure_devs, sizeof(ure_devs), uaa)); +} + +/* + * Attach the interface. Allocate softc structures, do ifmedia + * setup and ethernet/BPF attach. + */ +static int +ure_attach(device_t dev) +{ + struct usb_attach_arg *uaa = device_get_ivars(dev); + struct ure_softc *sc = device_get_softc(dev); + struct usb_ether *ue = &sc->sc_ue; + uint8_t iface_index; + int error; + + device_set_usb_desc(dev); + mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF); + + iface_index = URE_IFACE_IDX; + error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer, + ure_config, URE_N_TRANSFER, sc, &sc->sc_mtx); + if (error != 0) { + device_printf(dev, "allocating USB transfers failed\n"); + goto detach; + } + + ue->ue_sc = sc; + ue->ue_dev = dev; + ue->ue_udev = uaa->device; + ue->ue_mtx = &sc->sc_mtx; + ue->ue_methods = &ure_ue_methods; + + error = uether_ifattach(ue); + if (error != 0) { + device_printf(dev, "could not attach interface\n"); + goto detach; + } + return (0); /* success */ + +detach: + ure_detach(dev); + return (ENXIO); /* failure */ +} + +static int +ure_detach(device_t dev) +{ + struct ure_softc *sc = device_get_softc(dev); + struct usb_ether *ue = &sc->sc_ue; + + usbd_transfer_unsetup(sc->sc_xfer, URE_N_TRANSFER); + uether_ifdetach(ue); + mtx_destroy(&sc->sc_mtx); + + return (0); +} + +static void +ure_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) +{ + struct ure_softc *sc = usbd_xfer_softc(xfer); + struct usb_ether *ue = &sc->sc_ue; + struct ifnet *ifp = uether_getifp(ue); + struct usb_page_cache *pc; + struct ure_rxpkt pkt; + int actlen, len; + + usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); + + switch (USB_GET_STATE(xfer)) { + case USB_ST_TRANSFERRED: + if (actlen < (int)(sizeof(pkt))) { + if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); + goto tr_setup; + } + pc = usbd_xfer_get_frame(xfer, 0); + usbd_copy_out(pc, 0, &pkt, sizeof(pkt)); + len = le32toh(pkt.ure_pktlen) & URE_RXPKT_LEN_MASK; + len -= ETHER_CRC_LEN; + if (actlen < (int)(len + sizeof(pkt))) { + if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); + goto tr_setup; + } + + uether_rxbuf(ue, pc, sizeof(pkt), len); + /* FALLTHROUGH */ + case USB_ST_SETUP: +tr_setup: + usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); + usbd_transfer_submit(xfer); + uether_rxflush(ue); + return; + + default: /* Error */ + DPRINTF("bulk read error, %s\n", + usbd_errstr(error)); + + if (error != USB_ERR_CANCELLED) { + /* try to clear stall first */ + usbd_xfer_set_stall(xfer); + goto tr_setup; + } + return; + } +} + +static void +ure_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) +{ + struct ure_softc *sc = usbd_xfer_softc(xfer); + struct ifnet *ifp = uether_getifp(&sc->sc_ue); + struct usb_page_cache *pc; + struct mbuf *m; + struct ure_txpkt txpkt; + int len, pos; + + switch (USB_GET_STATE(xfer)) { + case USB_ST_TRANSFERRED: + DPRINTFN(11, "transfer complete\n"); + ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; + /* FALLTHROUGH */ + case USB_ST_SETUP: +tr_setup: + if ((sc->sc_flags & URE_FLAG_LINK) == 0 || + (ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) { + /* + * don't send anything if there is no link ! + */ + return; + } + IFQ_DRV_DEQUEUE(&ifp->if_snd, m); + if (m == NULL) + break; + pos = 0; + len = m->m_pkthdr.len; + pc = usbd_xfer_get_frame(xfer, 0); + memset(&txpkt, 0, sizeof(txpkt)); + txpkt.ure_pktlen = htole32((len & URE_TXPKT_LEN_MASK) | + URE_TKPKT_TX_FS | URE_TKPKT_TX_LS); + usbd_copy_in(pc, pos, &txpkt, sizeof(txpkt)); + pos += sizeof(txpkt); + usbd_m_copy_in(pc, pos, m, 0, m->m_pkthdr.len); + pos += m->m_pkthdr.len; + + if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); + + /* + * If there's a BPF listener, bounce a copy + * of this frame to him. + */ + BPF_MTAP(ifp, m); + + m_freem(m); + + /* Set frame length. */ + usbd_xfer_set_frame_len(xfer, 0, pos); + + usbd_transfer_submit(xfer); + ifp->if_drv_flags |= IFF_DRV_OACTIVE; + return; + default: /* Error */ + DPRINTFN(11, "transfer error, %s\n", + usbd_errstr(error)); + + if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); + ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; + + if (error != USB_ERR_CANCELLED) { + /* try to clear stall first */ + usbd_xfer_set_stall(xfer); + goto tr_setup; + } + return; + } +} + +static void +ure_read_chipver(struct ure_softc *sc) +{ + uint16_t ver; + + ver = ure_read_2(sc, URE_PLA_TCR1, URE_MCU_TYPE_PLA) & URE_VERSION_MASK; + switch (ver) { + case 0x4c00: + sc->sc_chip |= URE_CHIP_VER_4C00; + break; + case 0x4c10: + sc->sc_chip |= URE_CHIP_VER_4C10; + break; + default: + device_printf(sc->sc_ue.ue_dev, + "unknown version 0x%04x\n", ver); + break; + } +} + +static void +ure_attach_post(struct usb_ether *ue) +{ + struct ure_softc *sc = uether_getsc(ue); + + sc->sc_phyno = 0; + + /* Determine the chip version. */ + ure_read_chipver(sc); + + /* Initialize controller and get station address. */ + ure_rtl8152_init(sc); + + if (sc->sc_chip & URE_CHIP_VER_4C00) + ure_read_mem(sc, URE_PLA_IDR, URE_MCU_TYPE_PLA, + ue->ue_eaddr, 8); + else + ure_read_mem(sc, URE_PLA_BACKUP, URE_MCU_TYPE_PLA, + ue->ue_eaddr, 8); +} + +static int +ure_attach_post_sub(struct usb_ether *ue) +{ + struct ure_softc *sc; + struct ifnet *ifp; + int error; + + sc = uether_getsc(ue); + ifp = ue->ue_ifp; + ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; + ifp->if_start = uether_start; + ifp->if_ioctl = ure_ioctl; + ifp->if_init = uether_init; + IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); + ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; + IFQ_SET_READY(&ifp->if_snd); + + mtx_lock(&Giant); + error = mii_attach(ue->ue_dev, &ue->ue_miibus, ifp, + uether_ifmedia_upd, ue->ue_methods->ue_mii_sts, + BMSR_DEFCAPMASK, sc->sc_phyno, MII_OFFSET_ANY, 0); + mtx_unlock(&Giant); + + return (error); +} + +static void +ure_init(struct usb_ether *ue) +{ + struct ure_softc *sc = uether_getsc(ue); + struct ifnet *ifp = uether_getifp(ue); + uint32_t rxmode; + + URE_LOCK_ASSERT(sc, MA_OWNED); + + if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) + return; + + /* Cancel pending I/O. */ + ure_stop(ue); + + ure_reset(sc); + + /* Set MAC address. */ + ure_write_mem(sc, URE_PLA_IDR, URE_MCU_TYPE_PLA | URE_BYTE_EN_SIX_BYTES, + IF_LLADDR(ifp), 8); + + /* Reset the packet filter. */ + ure_write_2(sc, URE_PLA_FMC, URE_MCU_TYPE_PLA, + ure_read_2(sc, URE_PLA_FMC, URE_MCU_TYPE_PLA) & + ~URE_FMC_FCR_MCU_EN); + ure_write_2(sc, URE_PLA_FMC, URE_MCU_TYPE_PLA, + ure_read_2(sc, URE_PLA_FMC, URE_MCU_TYPE_PLA) | + URE_FMC_FCR_MCU_EN); + + /* Enable transmit and receive. */ + ure_write_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA, + ure_read_1(sc, URE_PLA_CR, URE_MCU_TYPE_PLA) | URE_CR_RE | + URE_CR_TE); + + ure_write_2(sc, URE_PLA_MISC_1, URE_MCU_TYPE_PLA, + ure_read_2(sc, URE_PLA_MISC_1, URE_MCU_TYPE_PLA) & + ~URE_RXDY_GATED_EN); + + /* Set Rx mode. */ + rxmode = URE_RCR_APM; + + /* If we want promiscuous mode, set the allframes bit. */ + if (ifp->if_flags & IFF_PROMISC) + rxmode |= URE_RCR_AAP; + + if (ifp->if_flags & IFF_BROADCAST) + rxmode |= URE_RCR_AB; + + ure_write_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA, rxmode); + + /* Load the multicast filter. */ + ure_setmulti(ue); + + usbd_xfer_set_stall(sc->sc_xfer[URE_BULK_DT_WR]); + + /* Indicate we are up and running. */ + ifp->if_drv_flags |= IFF_DRV_RUNNING; + + /* Switch to selected media. */ + ure_ifmedia_upd(ifp); +} + +static void +ure_tick(struct usb_ether *ue) +{ + struct ure_softc *sc = uether_getsc(ue); + struct mii_data *mii = GET_MII(sc); + + URE_LOCK_ASSERT(sc, MA_OWNED); + + mii_tick(mii); + if ((sc->sc_flags & URE_FLAG_LINK) == 0 + && mii->mii_media_status & IFM_ACTIVE && + IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { + sc->sc_flags |= URE_FLAG_LINK; + ure_start(ue); + } +} + +static void +ure_setpromisc(struct usb_ether *ue) +{ + struct ure_softc *sc = uether_getsc(ue); + struct ifnet *ifp = uether_getifp(ue); + uint32_t rxmode; + + rxmode = ure_read_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA); + + if (ifp->if_flags & IFF_PROMISC) + rxmode |= URE_RCR_AAP; + else + rxmode &= ~URE_RCR_AAP; + + ure_write_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA, rxmode); + + ure_setmulti(ue); +} + +/* + * Program the 64-bit multicast hash filter. + */ +static void +ure_setmulti(struct usb_ether *ue) +{ + struct ure_softc *sc = uether_getsc(ue); + struct ifnet *ifp = uether_getifp(ue); + struct ifmultiaddr *ifma; + uint32_t h, rxmode; + uint32_t hashes[2] = { 0, 0 }; + + URE_LOCK_ASSERT(sc, MA_OWNED); + + rxmode = ure_read_4(sc, URE_PLA_RCR, URE_MCU_TYPE_PLA); + if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) { + if (ifp->if_flags & IFF_PROMISC) + rxmode |= URE_RCR_AAP; + rxmode |= URE_RCR_AM; + hashes[0] = hashes[1] = 0xffffffff; + goto done; + } + + if_maddr_rlock(ifp); + TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { + if (ifma->ifma_addr->sa_family != AF_LINK) + continue; *** DIFF OUTPUT TRUNCATED AT 1000 LINES ***
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201512010512.tB15CDgV007432>