summaryrefslogtreecommitdiff
path: root/sys/dev
diff options
context:
space:
mode:
authormrg <mrg@NetBSD.org>2019-09-20 10:34:54 +0000
committermrg <mrg@NetBSD.org>2019-09-20 10:34:54 +0000
commitf47e09f401055afd94cc5eedfdb0aa64b9645bab (patch)
tree44721b0969a29f1ab7b474d10bef13c3c9c17866 /sys/dev
parent20ac9e47bf51fd16add0fe8641d00bdbc2f04de5 (diff)
add mos(4) driver for Moschip MCS7730/MCS7830/MCS7832 usb ethernet.
ported from openbsd. usbnet version loses about 40% of code.
Diffstat (limited to 'sys/dev')
-rw-r--r--sys/dev/usb/files.usb17
-rw-r--r--sys/dev/usb/if_mos.c816
-rw-r--r--sys/dev/usb/if_mosreg.h109
-rw-r--r--sys/dev/usb/usbdevices.config4
4 files changed, 938 insertions, 8 deletions
diff --git a/sys/dev/usb/files.usb b/sys/dev/usb/files.usb
index 63c838ba292..5793633a931 100644
--- a/sys/dev/usb/files.usb
+++ b/sys/dev/usb/files.usb
@@ -1,4 +1,4 @@
-# $NetBSD: files.usb,v 1.167 2019/08/23 08:45:25 mrg Exp $
+# $NetBSD: files.usb,v 1.168 2019/09/20 10:34:54 mrg Exp $
#
# Config file and device description for machine-independent USB code.
# Included by ports that need it. Ports that use it must provide
@@ -373,21 +373,26 @@ device axen: arp, ether, ifnet, mii, usbnet
attach axen at usbdevif
file dev/usb/if_axen.c axen
+# Moscom MCS7730, MCS7830, and MCS7832
+device mos: arp, ether, ifnet, mii, mii_phy, usbnet
+attach mos at usbdevif
+file dev/usb/if_mos.c mos
+
# Microchip LAN750x and LAN85xx
device mue: arp, ether, ifnet, mii, mii_phy, usbnet
attach mue at usbdevif
file dev/usb/if_mue.c mue
-# DAVICOM DM9601
-device udav: arp, ether, ifnet, mii, mii_phy, usbnet
-attach udav at usbdevif
-file dev/usb/if_udav.c udav
-
# Atheros AR9170
device otus: arp, ether, firmload, ifnet, wlan
attach otus at usbdevif
file dev/usb/if_otus.c otus
+# DAVICOM DM9601
+device udav: arp, ether, ifnet, mii, mii_phy, usbnet
+attach udav at usbdevif
+file dev/usb/if_udav.c udav
+
# Mobile Broadband Interface Model
device umb: ifnet
attach umb at usbifif
diff --git a/sys/dev/usb/if_mos.c b/sys/dev/usb/if_mos.c
new file mode 100644
index 00000000000..a0c8dea7927
--- /dev/null
+++ b/sys/dev/usb/if_mos.c
@@ -0,0 +1,816 @@
+/* $NetBSD: if_mos.c,v 1.1 2019/09/20 10:34:54 mrg Exp $ */
+/* $OpenBSD: if_mos.c,v 1.40 2019/07/07 06:40:10 kevlo Exp $ */
+
+/*
+ * Copyright (c) 2008 Johann Christian Rode <jcrode@gmx.net>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/*
+ * Copyright (c) 2005, 2006, 2007 Jonathan Gray <jsg@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/*
+ * Copyright (c) 1997, 1998, 1999, 2000-2003
+ * Bill Paul <wpaul@windriver.com>. 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.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Bill Paul.
+ * 4. Neither the name of the author nor the names of any co-contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY Bill Paul 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 Bill Paul OR THE VOICES IN HIS HEAD
+ * 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.
+ */
+
+/*
+ * Moschip MCS7730/MCS7830/MCS7832 USB to Ethernet controller
+ * The datasheet is available at the following URL:
+ * http://www.moschip.com/data/products/MCS7830/Data%20Sheet_7830.pdf
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: if_mos.c,v 1.1 2019/09/20 10:34:54 mrg Exp $");
+
+#include <sys/param.h>
+
+#include <dev/usb/usbnet.h>
+#include <dev/usb/if_mosreg.h>
+
+#define MOS_PAUSE_REWRITES 3
+
+#define MOS_TIMEOUT 1000
+
+#define MOS_RX_LIST_CNT 1
+#define MOS_TX_LIST_CNT 1
+
+/* Maximum size of a fast ethernet frame plus one byte for the status */
+#define MOS_BUFSZ (ETHER_MAX_LEN+1)
+
+/*
+ * USB endpoints.
+ */
+#define MOS_ENDPT_RX 0
+#define MOS_ENDPT_TX 1
+#define MOS_ENDPT_INTR 2
+#define MOS_ENDPT_MAX 3
+
+/*
+ * USB vendor requests.
+ */
+#define MOS_UR_READREG 0x0e
+#define MOS_UR_WRITEREG 0x0d
+
+#define MOS_CONFIG_NO 1
+#define MOS_IFACE_IDX 0
+
+struct mos_type {
+ struct usb_devno mos_dev;
+ u_int16_t mos_flags;
+#define MCS7730 0x0001 /* MCS7730 */
+#define MCS7830 0x0002 /* MCS7830 */
+#define MCS7832 0x0004 /* MCS7832 */
+};
+
+#define MOS_INC(x, y) (x) = (x + 1) % y
+
+#ifdef MOS_DEBUG
+#define DPRINTF(x) do { if (mosdebug) printf x; } while (0)
+#define DPRINTFN(n,x) do { if (mosdebug >= (n)) printf x; } while (0)
+int mosdebug = 0;
+#else
+#define DPRINTF(x) __nothing
+#define DPRINTFN(n,x) __nothing
+#endif
+
+/*
+ * Various supported device vendors/products.
+ */
+const struct mos_type mos_devs[] = {
+ { { USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7730 }, MCS7730 },
+ { { USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7830 }, MCS7830 },
+ { { USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7832 }, MCS7832 },
+ { { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_LN030 }, MCS7830 },
+};
+#define mos_lookup(v, p) ((const struct mos_type *)usb_lookup(mos_devs, v, p))
+
+static int mos_match(device_t, cfdata_t, void *);
+static void mos_attach(device_t, device_t, void *);
+
+CFATTACH_DECL_NEW(mos, sizeof(struct usbnet),
+ mos_match, mos_attach, usbnet_detach, usbnet_activate);
+
+static void mos_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t);
+static unsigned mos_tx_prepare(struct usbnet *, struct mbuf *,
+ struct usbnet_chain *);
+static int mos_ioctl(struct ifnet *, u_long, void *);
+static int mos_init(struct ifnet *);
+static void mos_chip_init(struct usbnet *);
+static void mos_stop(struct ifnet *ifp, int disable);
+static int mos_mii_read_reg(struct usbnet *, int, int, uint16_t *);
+static int mos_mii_write_reg(struct usbnet *, int, int, uint16_t);
+static void mos_mii_statchg(struct ifnet *);
+static void mos_reset(struct usbnet *);
+
+static int mos_reg_read_1(struct usbnet *, int);
+static int mos_reg_read_2(struct usbnet *, int);
+static int mos_reg_write_1(struct usbnet *, int, int);
+static int mos_reg_write_2(struct usbnet *, int, int);
+static int mos_readmac(struct usbnet *);
+static int mos_writemac(struct usbnet *);
+static int mos_write_mcast(struct usbnet *, uint8_t *);
+
+static struct usbnet_ops mos_ops = {
+ .uno_stop = mos_stop,
+ .uno_ioctl = mos_ioctl,
+ .uno_read_reg = mos_mii_read_reg,
+ .uno_write_reg = mos_mii_write_reg,
+ .uno_statchg = mos_mii_statchg,
+ .uno_tx_prepare = mos_tx_prepare,
+ .uno_rx_loop = mos_rx_loop,
+ .uno_init = mos_init,
+};
+
+static int
+mos_reg_read_1(struct usbnet *un, int reg)
+{
+ usb_device_request_t req;
+ usbd_status err;
+ uByte val = 0;
+
+ if (usbnet_isdying(un))
+ return 0;
+
+ req.bmRequestType = UT_READ_VENDOR_DEVICE;
+ req.bRequest = MOS_UR_READREG;
+ USETW(req.wValue, 0);
+ USETW(req.wIndex, reg);
+ USETW(req.wLength, 1);
+
+ err = usbd_do_request(un->un_udev, &req, &val);
+
+ if (err) {
+ aprint_error_dev(un->un_dev, "read reg %x\n", reg);
+ return 0;
+ }
+
+ return val;
+}
+
+static int
+mos_reg_read_2(struct usbnet *un, int reg)
+{
+ usb_device_request_t req;
+ usbd_status err;
+ uWord val;
+
+ if (usbnet_isdying(un))
+ return 0;
+
+ USETW(val,0);
+
+ req.bmRequestType = UT_READ_VENDOR_DEVICE;
+ req.bRequest = MOS_UR_READREG;
+ USETW(req.wValue, 0);
+ USETW(req.wIndex, reg);
+ USETW(req.wLength, 2);
+
+ err = usbd_do_request(un->un_udev, &req, &val);
+
+ if (err) {
+ aprint_error_dev(un->un_dev, "read reg2 %x\n", reg);
+ return 0;
+ }
+
+ return UGETW(val);
+}
+
+static int
+mos_reg_write_1(struct usbnet *un, int reg, int aval)
+{
+ usb_device_request_t req;
+ usbd_status err;
+ uByte val;
+
+ if (usbnet_isdying(un))
+ return 0;
+
+ val = aval;
+
+ req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
+ req.bRequest = MOS_UR_WRITEREG;
+ USETW(req.wValue, 0);
+ USETW(req.wIndex, reg);
+ USETW(req.wLength, 1);
+
+ err = usbd_do_request(un->un_udev, &req, &val);
+
+ if (err)
+ aprint_error_dev(un->un_dev, "write reg %x <- %x\n",
+ reg, aval);
+
+ return 0;
+}
+
+static int
+mos_reg_write_2(struct usbnet *un, int reg, int aval)
+{
+ usb_device_request_t req;
+ usbd_status err;
+ uWord val;
+
+ USETW(val, aval);
+
+ if (usbnet_isdying(un))
+ return EIO;
+
+ req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
+ req.bRequest = MOS_UR_WRITEREG;
+ USETW(req.wValue, 0);
+ USETW(req.wIndex, reg);
+ USETW(req.wLength, 2);
+
+ err = usbd_do_request(un->un_udev, &req, &val);
+
+ if (err)
+ aprint_error_dev(un->un_dev, "write reg2 %x <- %x\n",
+ reg, aval);
+
+ return 0;
+}
+
+static int
+mos_readmac(struct usbnet *un)
+{
+ usb_device_request_t req;
+ usbd_status err;
+
+ if (usbnet_isdying(un))
+ return 0;
+
+ req.bmRequestType = UT_READ_VENDOR_DEVICE;
+ req.bRequest = MOS_UR_READREG;
+ USETW(req.wValue, 0);
+ USETW(req.wIndex, MOS_MAC);
+ USETW(req.wLength, ETHER_ADDR_LEN);
+
+ err = usbd_do_request(un->un_udev, &req, un->un_eaddr);
+
+ if (err)
+ aprint_error_dev(un->un_dev, "%s: failed", __func__);
+
+ return err;
+}
+
+static int
+mos_writemac(struct usbnet *un)
+{
+ usb_device_request_t req;
+ usbd_status err;
+
+ if (usbnet_isdying(un))
+ return 0;
+
+ req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
+ req.bRequest = MOS_UR_WRITEREG;
+ USETW(req.wValue, 0);
+ USETW(req.wIndex, MOS_MAC);
+ USETW(req.wLength, ETHER_ADDR_LEN);
+
+ err = usbd_do_request(un->un_udev, &req, un->un_eaddr);
+
+ if (err)
+ aprint_error_dev(un->un_dev, "%s: failed", __func__);
+
+ return 0;
+}
+
+static int
+mos_write_mcast(struct usbnet *un, uint8_t *hashtbl)
+{
+ usb_device_request_t req;
+ usbd_status err;
+
+ if (usbnet_isdying(un))
+ return EIO;
+
+ req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
+ req.bRequest = MOS_UR_WRITEREG;
+ USETW(req.wValue, 0);
+ USETW(req.wIndex, MOS_MCAST_TABLE);
+ USETW(req.wLength, 8);
+
+ err = usbd_do_request(un->un_udev, &req, hashtbl);
+
+ if (err) {
+ aprint_error_dev(un->un_dev, "%s: failed", __func__);
+ return(-1);
+ }
+
+ return 0;
+}
+
+static int
+mos_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val)
+{
+ int i, res;
+
+ mos_reg_write_2(un, MOS_PHY_DATA, 0);
+ mos_reg_write_1(un, MOS_PHY_CTL, (phy & MOS_PHYCTL_PHYADDR) |
+ MOS_PHYCTL_READ);
+ mos_reg_write_1(un, MOS_PHY_STS, (reg & MOS_PHYSTS_PHYREG) |
+ MOS_PHYSTS_PENDING);
+
+ for (i = 0; i < MOS_TIMEOUT; i++) {
+ if (mos_reg_read_1(un, MOS_PHY_STS) & MOS_PHYSTS_READY)
+ break;
+ }
+ if (i == MOS_TIMEOUT) {
+ aprint_error_dev(un->un_dev, "read PHY failed\n");
+ return EIO;
+ }
+
+ res = mos_reg_read_2(un, MOS_PHY_DATA);
+ *val = res;
+
+ DPRINTFN(10,("%s: %s: phy %d reg %d val %u\n",
+ device_xname(un->un_dev), __func__, phy, reg, res));
+
+ return 0;
+}
+
+static int
+mos_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val)
+{
+ int i;
+
+ DPRINTFN(10,("%s: %s: phy %d reg %d val %u\n",
+ device_xname(un->un_dev), __func__, phy, reg, val));
+
+ mos_reg_write_2(un, MOS_PHY_DATA, val);
+ mos_reg_write_1(un, MOS_PHY_CTL, (phy & MOS_PHYCTL_PHYADDR) |
+ MOS_PHYCTL_WRITE);
+ mos_reg_write_1(un, MOS_PHY_STS, (reg & MOS_PHYSTS_PHYREG) |
+ MOS_PHYSTS_PENDING);
+
+ for (i = 0; i < MOS_TIMEOUT; i++) {
+ if (mos_reg_read_1(un, MOS_PHY_STS) & MOS_PHYSTS_READY)
+ break;
+ }
+ if (i == MOS_TIMEOUT) {
+ aprint_error_dev(un->un_dev, "write PHY failed\n");
+ return EIO;
+ }
+
+ return 0;
+}
+
+void
+mos_mii_statchg(struct ifnet *ifp)
+{
+ struct usbnet * const un = ifp->if_softc;
+ struct mii_data * const mii = usbnet_mii(un);
+ int val, err;
+
+ if (usbnet_isdying(un))
+ return;
+
+ DPRINTFN(10,("%s: %s: enter\n", device_xname(un->un_dev), __func__));
+
+ usbnet_lock_mii(un);
+
+ /* disable RX, TX prior to changing FDX, SPEEDSEL */
+ val = mos_reg_read_1(un, MOS_CTL);
+ val &= ~(MOS_CTL_TX_ENB | MOS_CTL_RX_ENB);
+ mos_reg_write_1(un, MOS_CTL, val);
+
+ /* reset register which counts dropped frames */
+ mos_reg_write_1(un, MOS_FRAME_DROP_CNT, 0);
+
+ if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
+ val |= MOS_CTL_FDX_ENB;
+ else
+ val &= ~(MOS_CTL_FDX_ENB);
+
+ if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
+ (IFM_ACTIVE | IFM_AVALID)) {
+ switch (IFM_SUBTYPE(mii->mii_media_active)) {
+ case IFM_100_TX:
+ val |= MOS_CTL_SPEEDSEL;
+ break;
+ case IFM_10_T:
+ val &= ~(MOS_CTL_SPEEDSEL);
+ break;
+ }
+ usbnet_set_link(un, true);
+ }
+
+ /* re-enable TX, RX */
+ val |= (MOS_CTL_TX_ENB | MOS_CTL_RX_ENB);
+ err = mos_reg_write_1(un, MOS_CTL, val);
+ usbnet_unlock_mii(un);
+
+ if (err)
+ aprint_error_dev(un->un_dev, "media change failed\n");
+}
+
+static void
+mos_setiff_locked(struct usbnet *un)
+{
+ struct ifnet *ifp = usbnet_ifp(un);
+ struct ethercom *ec = usbnet_ec(un);
+ struct ether_multi *enm;
+ struct ether_multistep step;
+ u_int32_t h = 0;
+ u_int8_t rxmode, hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
+
+ if (usbnet_isdying(un))
+ return;
+
+ rxmode = mos_reg_read_1(un, MOS_CTL);
+ rxmode &= ~(MOS_CTL_ALLMULTI | MOS_CTL_RX_PROMISC);
+
+ if (ifp->if_flags & IFF_PROMISC || ec->ec_multicnt > 0) {
+allmulti:
+ ETHER_LOCK(ec);
+ ec->ec_flags |= ETHER_F_ALLMULTI;
+ ETHER_UNLOCK(ec);
+ rxmode |= MOS_CTL_ALLMULTI;
+ if (ifp->if_flags & IFF_PROMISC)
+ rxmode |= MOS_CTL_RX_PROMISC;
+ } else {
+ /* now program new ones */
+ ETHER_LOCK(ec);
+ ec->ec_flags &= ~ETHER_F_ALLMULTI;
+
+ ETHER_FIRST_MULTI(step, ec, enm);
+ while (enm != NULL) {
+ if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
+ ETHER_ADDR_LEN)) {
+ memset(hashtbl, 0, sizeof(hashtbl));
+ ETHER_UNLOCK(ec);
+ goto allmulti;
+ }
+ h = ether_crc32_be(enm->enm_addrlo,
+ ETHER_ADDR_LEN) >> 26;
+ hashtbl[h / 8] |= 1 << (h % 8);
+
+ ETHER_NEXT_MULTI(step, enm);
+ }
+ ETHER_UNLOCK(ec);
+ }
+
+ /*
+ * The datasheet claims broadcast frames were always accepted
+ * regardless of filter settings. But the hardware seems to
+ * filter broadcast frames, so pass them explicitly.
+ */
+ h = ether_crc32_be(etherbroadcastaddr, ETHER_ADDR_LEN) >> 26;
+ hashtbl[h / 8] |= 1 << (h % 8);
+
+ mos_write_mcast(un, hashtbl);
+ mos_reg_write_1(un, MOS_CTL, rxmode);
+}
+
+static void
+mos_setiff(struct usbnet *un)
+{
+ usbnet_lock_mii(un);
+ mos_setiff_locked(un);
+ usbnet_unlock_mii(un);
+}
+
+static void
+mos_reset(struct usbnet *un)
+{
+ u_int8_t ctl;
+
+ if (usbnet_isdying(un))
+ return;
+
+ ctl = mos_reg_read_1(un, MOS_CTL);
+ ctl &= ~(MOS_CTL_RX_PROMISC | MOS_CTL_ALLMULTI | MOS_CTL_TX_ENB |
+ MOS_CTL_RX_ENB);
+ /* Disable RX, TX, promiscuous and allmulticast mode */
+ mos_reg_write_1(un, MOS_CTL, ctl);
+
+ /* Reset frame drop counter register to zero */
+ mos_reg_write_1(un, MOS_FRAME_DROP_CNT, 0);
+
+ /* Wait a little while for the chip to get its brains in order. */
+ DELAY(1000);
+}
+
+void
+mos_chip_init(struct usbnet *un)
+{
+ int i;
+
+ /*
+ * Rev.C devices have a pause threshold register which needs to be set
+ * at startup.
+ */
+ if (mos_reg_read_1(un, MOS_PAUSE_TRHD) != -1) {
+ for (i = 0; i < MOS_PAUSE_REWRITES; i++)
+ mos_reg_write_1(un, MOS_PAUSE_TRHD, 0);
+ }
+}
+
+/*
+ * Probe for a MCS7x30 chip.
+ */
+static int
+mos_match(device_t parent, cfdata_t match, void *aux)
+{
+ struct usb_attach_arg *uaa = aux;
+
+ return (mos_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
+ UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
+}
+
+/*
+ * Attach the interface.
+ */
+static void
+mos_attach(device_t parent, device_t self, void *aux)
+{
+ USBNET_MII_DECL_DEFAULT(unm);
+ struct usbnet * un = device_private(self);
+ struct usb_attach_arg *uaa = aux;
+ struct usbd_device *dev = uaa->uaa_device;
+ usbd_status err;
+ usb_interface_descriptor_t *id;
+ usb_endpoint_descriptor_t *ed;
+ char *devinfop;
+ int i;
+
+ aprint_naive("\n");
+ aprint_normal("\n");
+ devinfop = usbd_devinfo_alloc(dev, 0);
+ aprint_normal_dev(self, "%s\n", devinfop);
+ usbd_devinfo_free(devinfop);
+
+ un->un_dev = self;
+ un->un_udev = dev;
+ un->un_sc = un;
+ un->un_ops = &mos_ops;
+ un->un_rx_xfer_flags = USBD_SHORT_XFER_OK;
+ un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
+ un->un_rx_list_cnt = MOS_RX_LIST_CNT;
+ un->un_tx_list_cnt = MOS_TX_LIST_CNT;
+ un->un_rx_bufsz = un->un_tx_bufsz = MOS_BUFSZ;
+
+ err = usbd_set_config_no(dev, MOS_CONFIG_NO, 1);
+ if (err) {
+ aprint_error_dev(self, "failed to set configuration"
+ ", err=%s\n", usbd_errstr(err));
+ return;
+ }
+
+ err = usbd_device2interface_handle(dev, MOS_IFACE_IDX, &un->un_iface);
+ if (err) {
+ aprint_error_dev(self, "failed getting interface handle"
+ ", err=%s\n", usbd_errstr(err));
+ return;
+ }
+
+ un->un_flags = mos_lookup(uaa->uaa_vendor, uaa->uaa_product)->mos_flags;
+
+ id = usbd_get_interface_descriptor(un->un_iface);
+
+ /* Find endpoints. */
+ for (i = 0; i < id->bNumEndpoints; i++) {
+ ed = usbd_interface2endpoint_descriptor(un->un_iface, i);
+ if (!ed) {
+ aprint_error_dev(self, "couldn't get ep %d\n", i);
+ return;
+ }
+ if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
+ UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
+ un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress;
+ } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
+ UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
+ un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress;
+ } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
+ UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
+ un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress;
+ }
+ }
+
+ if (un->un_flags & MCS7730)
+ aprint_normal_dev(self, "MCS7730\n");
+ else if (un->un_flags & MCS7830)
+ aprint_normal_dev(self, "MCS7830\n");
+ else if (un->un_flags & MCS7832)
+ aprint_normal_dev(self, "MCS7832\n");
+
+ /* Set these up now for register access. */
+ usbnet_attach(un, "mosdet");
+
+ mos_chip_init(un);
+
+ /*
+ * Read MAC address, inform the world.
+ */
+ err = mos_readmac(un);
+ if (err) {
+ aprint_error_dev(self, "couldn't read MAC address\n");
+ return;
+ }
+
+ struct ifnet *ifp = usbnet_ifp(un);
+ ifp->if_capabilities = ETHERCAP_VLAN_MTU;
+
+ usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST,
+ 0, &unm);
+}
+
+/*
+ * A frame has been uploaded: pass the resulting mbuf chain up to
+ * the higher level protocols.
+ */
+void
+mos_rx_loop(struct usbnet * un, struct usbnet_chain *c, uint32_t total_len)
+{
+ struct ifnet *ifp = usbnet_ifp(un);
+ uint8_t *buf = c->unc_buf;
+ u_int8_t rxstat;
+ u_int16_t pktlen = 0;
+
+ DPRINTFN(5,("%s: %s: enter len %u\n",
+ device_xname(un->un_dev), __func__, total_len));
+
+ if (total_len <= 1)
+ return;
+
+ /* evaluate status byte at the end */
+ pktlen = total_len - 1;
+ if (pktlen > un->un_rx_bufsz) {
+ ifp->if_ierrors++;
+ return;
+ }
+ rxstat = buf[pktlen] & MOS_RXSTS_MASK;
+
+ if (rxstat != MOS_RXSTS_VALID) {
+ DPRINTF(("%s: erroneous frame received: ",
+ device_xname(un->un_dev)));
+ if (rxstat & MOS_RXSTS_SHORT_FRAME)
+ DPRINTF(("frame size less than 64 bytes\n"));
+ if (rxstat & MOS_RXSTS_LARGE_FRAME)
+ DPRINTF(("frame size larger than 1532 bytes\n"));
+ if (rxstat & MOS_RXSTS_CRC_ERROR)
+ DPRINTF(("CRC error\n"));
+ if (rxstat & MOS_RXSTS_ALIGN_ERROR)
+ DPRINTF(("alignment error\n"));
+ ifp->if_ierrors++;
+ return;
+ }
+
+ if (pktlen < sizeof(struct ether_header) ) {
+ ifp->if_ierrors++;
+ return;
+ }
+
+ usbnet_enqueue(un, c->unc_buf, pktlen, 0, 0, 0);
+}
+
+static unsigned
+mos_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
+{
+ int length;
+
+ usbnet_isowned_tx(un);
+
+ if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz)
+ return 0;
+
+ m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf);
+ length = m->m_pkthdr.len;
+
+ DPRINTFN(5,("%s: %s: len %u\n",
+ device_xname(un->un_dev), __func__, length));
+
+ return length;
+}
+
+static int
+mos_init_locked(struct ifnet *ifp)
+{
+ struct usbnet * const un = ifp->if_softc;
+ u_int8_t rxmode;
+ unsigned char ipgs[2];
+
+ if (usbnet_isdying(un))
+ return EIO;
+
+ /* Cancel pending I/O */
+ usbnet_stop(un, ifp, 1);
+
+ usbnet_lock_mii_un_locked(un);
+
+ /* Reset the ethernet interface. */
+ mos_reset(un);
+
+ /* Write MAC address. */
+ mos_writemac(un);
+
+ /* Read and set transmitter IPG values */
+ ipgs[0] = mos_reg_read_1(un, MOS_IPG0);
+ ipgs[1] = mos_reg_read_1(un, MOS_IPG1);
+ mos_reg_write_1(un, MOS_IPG0, ipgs[0]);
+ mos_reg_write_1(un, MOS_IPG1, ipgs[1]);
+
+ /* Program promiscuous mode and multicast filters. */
+ mos_setiff_locked(un);
+
+ /* Enable receiver and transmitter, bridge controls speed/duplex mode */
+ rxmode = mos_reg_read_1(un, MOS_CTL);
+ rxmode |= MOS_CTL_RX_ENB | MOS_CTL_TX_ENB | MOS_CTL_BS_ENB;
+ rxmode &= ~(MOS_CTL_SLEEP);
+ mos_reg_write_1(un, MOS_CTL, rxmode);
+
+ usbnet_unlock_mii_un_locked(un);
+
+ return usbnet_init_rx_tx(un);
+}
+
+static int
+mos_init(struct ifnet *ifp)
+{
+ struct usbnet * const un = ifp->if_softc;
+
+ usbnet_lock(un);
+ int ret = mos_init_locked(ifp);
+ usbnet_unlock(un);
+
+ return ret;
+}
+
+static int
+mos_ioctl(struct ifnet *ifp, u_long cmd, void *data)
+{
+ struct usbnet * const un = ifp->if_softc;
+
+ switch (cmd) {
+ case SIOCADDMULTI:
+ case SIOCDELMULTI:
+ mos_setiff(un);
+ break;
+ default:
+ break;
+ }
+
+ return 0;
+}
+
+void
+mos_stop(struct ifnet *ifp, int disable)
+{
+ struct usbnet * const un = ifp->if_softc;
+
+ usbnet_lock_mii_un_locked(un);
+ mos_reset(un);
+ usbnet_unlock_mii_un_locked(un);
+}
diff --git a/sys/dev/usb/if_mosreg.h b/sys/dev/usb/if_mosreg.h
new file mode 100644
index 00000000000..6594e68a0ac
--- /dev/null
+++ b/sys/dev/usb/if_mosreg.h
@@ -0,0 +1,109 @@
+/* $NetBSD: if_mosreg.h,v 1.1 2019/09/20 10:34:54 mrg Exp $ */
+/* $OpenBSD: if_mosreg.h,v 1.7 2013/04/15 09:23:01 mglocker Exp $ */
+
+/*
+ * Copyright (c) 2008 Johann Christian Rode <jcrode@gmx.net>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/*
+ * Copyright (c) 1997, 1998, 1999, 2000-2003
+ * Bill Paul <wpaul@windriver.com>. 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.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Ravikanth.
+ * 4. Neither the name of the author nor the names of any co-contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY Bill Paul 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 Bill Paul, THE VOICES IN HIS HEAD OR
+ * THE 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.
+ *
+ */
+
+/*
+ * Register definitions for the Moschip MCS7x30 ethernet controller.
+ */
+#define MOS_MCAST_TABLE 0x00
+#define MOS_IPG0 0x08
+#define MOS_IPG1 0x09
+#define MOS_PHY_DATA0 0x0a
+#define MOS_PHY_DATA1 0x0b
+#define MOS_PHY_CTL 0x0c
+#define MOS_PHY_STS 0x0d
+#define MOS_PHY_DATA MOS_PHY_DATA0
+#define MOS_CTL 0x0e
+#define MOS_MAC0 0x0f
+#define MOS_MAC1 0x10
+#define MOS_MAC2 0x11
+#define MOS_MAC3 0x12
+#define MOS_MAC4 0x13
+#define MOS_MAC5 0x14
+#define MOS_MAC MOS_MAC0
+/* apparently only available on hardware rev. C */
+#define MOS_FRAME_DROP_CNT 0x15
+#define MOS_PAUSE_TRHD 0x16
+
+#define MOS_PHYCTL_PHYADDR 0x1f
+#define MOS_PHYCTL_WRITE 0x20
+#define MOS_PHYCTL_READ 0x40
+
+#define MOS_PHYSTS_PHYREG 0x1f
+#define MOS_PHYSTS_READY 0x40
+#define MOS_PHYSTS_PENDING 0x80
+
+#define MOS_CTL_RX_PROMISC 0x01
+#define MOS_CTL_ALLMULTI 0x02
+#define MOS_CTL_SLEEP 0x04
+#define MOS_CTL_TX_ENB 0x08
+/*
+ * The documentation calls this bit 'reserved', but in the FreeBSD driver
+ * provided by the vendor, this enables the receiver.
+ */
+#define MOS_CTL_RX_ENB 0x10
+#define MOS_CTL_FDX_ENB 0x20
+/* 0 = 10 Mbps, 1 = 100 Mbps */
+#define MOS_CTL_SPEEDSEL 0x40
+/* 0 = PHY controls speed/duplex mode, 1 = bridge controls speed/duplex mode */
+#define MOS_CTL_BS_ENB 0x80
+
+#define MOS_RXSTS_SHORT_FRAME 0x01
+#define MOS_RXSTS_LENGTH_ERROR 0x02
+#define MOS_RXSTS_ALIGN_ERROR 0x04
+#define MOS_RXSTS_CRC_ERROR 0x08
+#define MOS_RXSTS_LARGE_FRAME 0x10
+#define MOS_RXSTS_VALID 0x20
+/*
+ * The EtherType field of an Ethernet frame can contain values other than
+ * the frame length, hence length errors are ignored.
+ */
+#define MOS_RXSTS_MASK 0x3d
diff --git a/sys/dev/usb/usbdevices.config b/sys/dev/usb/usbdevices.config
index 5a8487ba493..ca9c232b845 100644
--- a/sys/dev/usb/usbdevices.config
+++ b/sys/dev/usb/usbdevices.config
@@ -1,4 +1,4 @@
-# $NetBSD: usbdevices.config,v 1.35 2019/02/06 11:58:32 rin Exp $
+# $NetBSD: usbdevices.config,v 1.36 2019/09/20 10:34:54 mrg Exp $
#
# This file contains all USB related configuration.
# It is suitable for inclusion in a kernel config(5) file.
@@ -126,7 +126,7 @@ axen* at uhub? port ? # ASIX AX88178a/AX88179 based adapters
cdce* at uhub? port ? # CDC, Ethernet Networking Control Model
cue* at uhub? port ? # CATC USB-EL1201A based adapters
kue* at uhub? port ? # Kawasaki LSI KL5KUSB101B based adapters
-#mos* at uhub? port ? # Moschip MCS7730/MCS7830/MCS7832 based adapters
+mos* at uhub? port ? # Moschip MCS7730/MCS7830/MCS7832 based adapters
mue* at uhub? port ? # Microchip LAN75xx/LAN78xx based adapters
udav* at uhub? port ? # Davicom DM9601 based adapters
#umb* at uhub? port ? # Mobile Broadband Interface Model (EXPERIMENTAL)