Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/usb Convert udav(4) to usbnet. Based on a diff from...



details:   https://anonhg.NetBSD.org/src/rev/d9c10fbe1f08
branches:  trunk
changeset: 463091:d9c10fbe1f08
user:      skrll <skrll%NetBSD.org@localhost>
date:      Wed Aug 07 19:21:48 2019 +0000

description:
Convert udav(4) to usbnet.  Based on a diff from mrg@

diffstat:

 sys/dev/usb/files.usb    |     4 +-
 sys/dev/usb/if_udav.c    |  1328 +++++++++++----------------------------------
 sys/dev/usb/if_udavreg.h |    60 +--
 3 files changed, 339 insertions(+), 1053 deletions(-)

diffs (truncated from 1827 to 300 lines):

diff -r 8b734c91d2d1 -r d9c10fbe1f08 sys/dev/usb/files.usb
--- a/sys/dev/usb/files.usb     Wed Aug 07 19:16:36 2019 +0000
+++ b/sys/dev/usb/files.usb     Wed Aug 07 19:21:48 2019 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.usb,v 1.160 2019/08/07 07:25:09 skrll Exp $
+#      $NetBSD: files.usb,v 1.161 2019/08/07 19:21:48 skrll Exp $
 #
 # Config file and device description for machine-independent USB code.
 # Included by ports that need it.  Ports that use it must provide
@@ -378,7 +378,7 @@
 file   dev/usb/if_mue.c                mue
 
 # DAVICOM DM9601
-device udav: arp, ether, ifnet, mii, mii_phy
+device udav: arp, ether, ifnet, mii, mii_phy, usbnet
 attach udav at usbdevif
 file   dev/usb/if_udav.c               udav
 
diff -r 8b734c91d2d1 -r d9c10fbe1f08 sys/dev/usb/if_udav.c
--- a/sys/dev/usb/if_udav.c     Wed Aug 07 19:16:36 2019 +0000
+++ b/sys/dev/usb/if_udav.c     Wed Aug 07 19:21:48 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_udav.c,v 1.60 2019/08/01 00:10:22 mrg Exp $ */
+/*     $NetBSD: if_udav.c,v 1.61 2019/08/07 19:21:48 skrll Exp $       */
 /*     $nabe: if_udav.c,v 1.3 2003/08/21 16:57:19 nabe Exp $   */
 
 /*
@@ -45,95 +45,64 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_udav.c,v 1.60 2019/08/01 00:10:22 mrg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_udav.c,v 1.61 2019/08/07 19:21:48 skrll Exp $");
 
 #ifdef _KERNEL_OPT
-#include "opt_inet.h"
 #include "opt_usb.h"
 #endif
 
 #include <sys/param.h>
 #include <sys/systm.h>
-#include <sys/mutex.h>
-#include <sys/mbuf.h>
+#include <sys/module.h>
 #include <sys/kernel.h>
-#include <sys/socket.h>
-#include <sys/device.h>
-#include <sys/rndsource.h>
-
-#include <net/if.h>
-#include <net/if_arp.h>
-#include <net/if_dl.h>
-#include <net/if_media.h>
-
-#include <net/bpf.h>
 
-#include <net/if_ether.h>
-#ifdef INET
-#include <netinet/in.h>
-#include <netinet/if_inarp.h>
-#endif
-
-#include <dev/mii/mii.h>
-#include <dev/mii/miivar.h>
-
-#include <dev/usb/usb.h>
-#include <dev/usb/usbdi.h>
-#include <dev/usb/usbdi_util.h>
-#include <dev/usb/usbdevs.h>
-
+#include <dev/usb/usbnet.h>
 #include <dev/usb/if_udavreg.h>
 
+struct udav_softc {
+       struct usbnet           sc_un;
+       uint16_t                sc_flags;
+};
 
 /* Function declarations */
 int    udav_match(device_t, cfdata_t, void *);
 void   udav_attach(device_t, device_t, void *);
-int    udav_detach(device_t, int);
-int    udav_activate(device_t, enum devact);
 
 CFATTACH_DECL_NEW(udav, sizeof(struct udav_softc), udav_match, udav_attach,
-    udav_detach, udav_activate);
+    usbnet_detach, usbnet_activate);
+
+static void udav_chip_init(struct usbnet *);
 
-Static int udav_openpipes(struct udav_softc *);
-Static int udav_rx_list_init(struct udav_softc *);
-Static int udav_tx_list_init(struct udav_softc *);
-Static int udav_newbuf(struct udav_softc *, struct udav_chain *, struct mbuf *);
-Static void udav_start(struct ifnet *);
-Static int udav_send(struct udav_softc *, struct mbuf *, int);
-Static void udav_txeof(struct usbd_xfer *, void *, usbd_status);
-Static void udav_rxeof(struct usbd_xfer *, void *, usbd_status);
-Static void udav_tick(void *);
-Static void udav_tick_task(void *);
-Static int udav_ioctl(struct ifnet *, u_long, void *);
-Static void udav_stop_task(struct udav_softc *);
-Static void udav_stop(struct ifnet *, int);
-Static void udav_watchdog(struct ifnet *);
-Static int udav_ifmedia_change(struct ifnet *);
-Static void udav_lock_mii(struct udav_softc *);
-Static void udav_unlock_mii(struct udav_softc *);
-Static int udav_miibus_readreg(device_t, int, int, uint16_t *);
-Static int udav_miibus_writereg(device_t, int, int, uint16_t);
-Static void udav_miibus_statchg(struct ifnet *);
-Static int udav_init(struct ifnet *);
-Static void udav_setmulti(struct udav_softc *);
-Static void udav_reset(struct udav_softc *);
+static unsigned udav_tx_prepare(struct usbnet *, struct mbuf *,
+                               struct usbnet_chain *);
+static void udav_rxeof_loop(struct usbnet *, struct usbd_xfer *,
+                           struct usbnet_chain *, uint32_t);
+static void udav_stop_cb(struct ifnet *, int);
+static int udav_ioctl_cb(struct ifnet *, u_long, void *);
+static usbd_status udav_mii_read_reg(struct usbnet *, int, int, uint16_t *);
+static usbd_status udav_mii_write_reg(struct usbnet *, int, int, uint16_t);
+static void udav_mii_statchg(struct ifnet *);
+static int udav_init(struct ifnet *);
+static void udav_setiff(struct usbnet *);
+static void udav_setiff_locked(struct usbnet *);
+static void udav_reset(struct usbnet *);
 
-Static int udav_csr_read(struct udav_softc *, int, void *, int);
-Static int udav_csr_write(struct udav_softc *, int, void *, int);
-Static int udav_csr_read1(struct udav_softc *, int);
-Static int udav_csr_write1(struct udav_softc *, int, unsigned char);
+static int udav_csr_read(struct udav_softc *, int, void *, int);
+static int udav_csr_write(struct udav_softc *, int, void *, int);
+static int udav_csr_read1(struct udav_softc *, int);
+static int udav_csr_write1(struct udav_softc *, int, unsigned char);
 
 #if 0
-Static int udav_mem_read(struct udav_softc *, int, void *, int);
-Static int udav_mem_write(struct udav_softc *, int, void *, int);
-Static int udav_mem_write1(struct udav_softc *, int, unsigned char);
+static int udav_mem_read(struct udav_softc *, int, void *, int);
+static int udav_mem_write(struct udav_softc *, int, void *, int);
+static int udav_mem_write1(struct udav_softc *, int, unsigned char);
 #endif
 
 /* Macros */
 #ifdef UDAV_DEBUG
 #define DPRINTF(x)     if (udavdebug) printf x
 #define DPRINTFN(n, x) if (udavdebug >= (n)) printf x
-int udavdebug = 0;
+int udavdebug = 10;
 #else
 #define DPRINTF(x)
 #define DPRINTFN(n, x)
@@ -187,6 +156,7 @@
 udav_attach(device_t parent, device_t self, void *aux)
 {
        struct udav_softc *sc = device_private(self);
+       struct usbnet * const un = &sc->sc_un;
        struct usb_attach_arg *uaa = aux;
        struct usbd_device *dev = uaa->uaa_device;
        struct usbd_interface *iface;
@@ -194,208 +164,114 @@
        usb_interface_descriptor_t *id;
        usb_endpoint_descriptor_t *ed;
        char *devinfop;
-       struct ifnet *ifp;
-       struct mii_data *mii;
-       u_char eaddr[ETHER_ADDR_LEN];
-       int i, s;
+       int i;
 
-       sc->sc_dev = self;
+       /* Switch to usbnet for device_private() */
+       self->dv_private = un;
 
        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 = sc;
+       un->un_stop_cb = udav_stop_cb;
+       un->un_ioctl_cb = udav_ioctl_cb;
+       un->un_read_reg_cb = udav_mii_read_reg;
+       un->un_write_reg_cb = udav_mii_write_reg;
+       un->un_statchg_cb = udav_mii_statchg;
+       un->un_tx_prepare_cb = udav_tx_prepare;
+       un->un_rx_loop_cb = udav_rxeof_loop;
+       un->un_init_cb = udav_init;
+       un->un_rx_xfer_flags = USBD_SHORT_XFER_OK;
+       un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
+
        /* Move the device into the configured state. */
        err = usbd_set_config_no(dev, UDAV_CONFIG_NO, 1); /* idx 0 */
        if (err) {
                aprint_error_dev(self, "failed to set configuration"
                    ", err=%s\n", usbd_errstr(err));
-               goto bad;
+               return;
        }
 
-       usb_init_task(&sc->sc_tick_task, udav_tick_task, sc, 0);
-       mutex_init(&sc->sc_mii_lock, MUTEX_DEFAULT, IPL_NONE);
-       usb_init_task(&sc->sc_stop_task, (void (*)(void *))udav_stop_task, sc,
-           0);
-
        /* get control interface */
        err = usbd_device2interface_handle(dev, UDAV_IFACE_INDEX, &iface);
        if (err) {
                aprint_error_dev(self, "failed to get interface, err=%s\n",
                       usbd_errstr(err));
-               goto bad;
+               return;
        }
 
-       sc->sc_udev = dev;
-       sc->sc_ctl_iface = iface;
+       un->un_iface = iface;
        sc->sc_flags = udav_lookup(uaa->uaa_vendor,
            uaa->uaa_product)->udav_flags;
 
        /* get interface descriptor */
-       id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
+       id = usbd_get_interface_descriptor(un->un_iface);
 
        /* find endpoints */
-       sc->sc_bulkin_no = sc->sc_bulkout_no = sc->sc_intrin_no = -1;
+       un->un_ed[USBNET_ENDPT_RX] = un->un_ed[USBNET_ENDPT_TX] =
+           un->un_ed[USBNET_ENDPT_INTR] = -1;
        for (i = 0; i < id->bNumEndpoints; i++) {
-               ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
+               ed = usbd_interface2endpoint_descriptor(un->un_iface, i);
                if (ed == NULL) {
                        aprint_error_dev(self, "couldn't get endpoint %d\n", i);
-                       goto bad;
+                       return;
                }
                if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
                    UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
-                       sc->sc_bulkin_no = ed->bEndpointAddress; /* RX */
+                       un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress;
                else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
                         UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT)
-                       sc->sc_bulkout_no = ed->bEndpointAddress; /* TX */
+                       un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress;
                else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT &&
                         UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
-                       sc->sc_intrin_no = ed->bEndpointAddress; /* Status */
-       }
-
-       if (sc->sc_bulkin_no == -1 || sc->sc_bulkout_no == -1 ||
-           sc->sc_intrin_no == -1) {
-               aprint_error_dev(self, "missing endpoint\n");
-               goto bad;
+                       un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress;
        }
 
-       s = splnet();
-
-       /* reset the adapter */
-       udav_reset(sc);
-
-       /* Get Ethernet Address */
-       err = udav_csr_read(sc, UDAV_PAR, (void *)eaddr, ETHER_ADDR_LEN);
-       if (err) {
-               aprint_error_dev(self, "read MAC address failed\n");
-               splx(s);
-               goto bad;
-       }
-
-       /* Print Ethernet Address */
-       aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
-
-       /* initialize interface information */
-       ifp = GET_IFP(sc);
-       ifp->if_softc = sc;
-       ifp->if_mtu = ETHERMTU;
-       strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
-       ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
-       ifp->if_start = udav_start;
-       ifp->if_ioctl = udav_ioctl;
-       ifp->if_watchdog = udav_watchdog;
-       ifp->if_init = udav_init;
-       ifp->if_stop = udav_stop;
-
-       IFQ_SET_READY(&ifp->if_snd);
-
-       if (ISSET(sc->sc_flags, UDAV_NO_PHY)) {
-               sc->sc_link = 1;
-               goto skipmii;
+       if (un->un_ed[USBNET_ENDPT_RX] == -1 ||
+           un->un_ed[USBNET_ENDPT_TX] == -1 ||
+           un->un_ed[USBNET_ENDPT_INTR] == -1) {
+               aprint_error_dev(self, "missing endpoint\n");
+               return;



Home | Main Index | Thread Index | Old Index