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 axe(4) and ure(4) to usbnet.



details:   https://anonhg.NetBSD.org/src/rev/a6634ccc61d2
branches:  trunk
changeset: 964486:a6634ccc61d2
user:      mrg <mrg%NetBSD.org@localhost>
date:      Sun Aug 04 09:03:46 2019 +0000

description:
convert axe(4) and ure(4) to usbnet.

axe loses 838 lines (37%) and ure loses 716 lines (36%).

diffstat:

 sys/dev/usb/if_axe.c    |  1252 +++++++--------------------------------------
 sys/dev/usb/if_ure.c    |  1267 +++++++++++-----------------------------------
 sys/dev/usb/if_urevar.h |    55 +-
 3 files changed, 510 insertions(+), 2064 deletions(-)

diffs (truncated from 3784 to 300 lines):

diff -r 2e2a83fba971 -r a6634ccc61d2 sys/dev/usb/if_axe.c
--- a/sys/dev/usb/if_axe.c      Sun Aug 04 08:59:13 2019 +0000
+++ b/sys/dev/usb/if_axe.c      Sun Aug 04 09:03:46 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_axe.c,v 1.103 2019/08/01 01:19:21 mrg Exp $ */
+/*     $NetBSD: if_axe.c,v 1.104 2019/08/04 09:03:46 mrg Exp $ */
 /*     $OpenBSD: if_axe.c,v 1.137 2016/04/13 11:03:37 mpi Exp $ */
 
 /*
@@ -87,44 +87,22 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_axe.c,v 1.103 2019/08/01 01:19:21 mrg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_axe.c,v 1.104 2019/08/04 09:03:46 mrg Exp $");
 
 #ifdef _KERNEL_OPT
-#include "opt_inet.h"
 #include "opt_usb.h"
 #include "opt_net_mpsafe.h"
 #endif
 
 #include <sys/param.h>
-#include <sys/bus.h>
-#include <sys/device.h>
 #include <sys/kernel.h>
-#include <sys/mbuf.h>
 #include <sys/module.h>
-#include <sys/mutex.h>
 #include <sys/socket.h>
 #include <sys/sockio.h>
 #include <sys/systm.h>
 
-#include <sys/rndsource.h>
-
-#include <net/if.h>
-#include <net/if_dl.h>
-#include <net/if_ether.h>
-#include <net/if_media.h>
-
-#include <net/bpf.h>
-
-#include <dev/mii/mii.h>
-#include <dev/mii/miivar.h>
-
-#include <dev/usb/usb.h>
+#include <dev/usb/usbnet.h>
 #include <dev/usb/usbhist.h>
-#include <dev/usb/usbdi.h>
-#include <dev/usb/usbdi_util.h>
-#include <dev/usb/usbdivar.h>
-#include <dev/usb/usbdevs.h>
-
 #include <dev/usb/if_axereg.h>
 
 struct axe_type {
@@ -132,32 +110,9 @@
        uint16_t                axe_flags;
 };
 
-struct axe_softc;
-
-struct axe_chain {
-       struct axe_softc        *axe_sc;
-       struct usbd_xfer        *axe_xfer;
-       uint8_t                 *axe_buf;
-};
+struct axe_softc {
+       struct usbnet           axe_un;
 
-struct axe_cdata {
-       struct axe_chain        axe_tx_chain[AXE_TX_LIST_CNT];
-       struct axe_chain        axe_rx_chain[AXE_RX_LIST_CNT];
-       int                     axe_tx_prod;
-       int                     axe_tx_cnt;
-};
-
-struct axe_softc {
-       device_t axe_dev;
-       struct ethercom         axe_ec;
-       struct mii_data         axe_mii;
-       krndsource_t    rnd_source;
-       struct usbd_device *    axe_udev;
-       struct usbd_interface * axe_iface;
-
-       uint16_t                axe_vendor;
-       uint16_t                axe_product;
-       uint16_t                axe_timer;
        uint32_t                axe_flags;      /* copied from axe_type */
 #define AX178          __BIT(0)        /* AX88178 */
 #define AX772          __BIT(1)        /* AX88772 */
@@ -166,40 +121,11 @@
 #define        AXSTD_FRAME     __BIT(12)
 #define        AXCSUM_FRAME    __BIT(13)
 
-       int                     axe_ed[AXE_ENDPT_MAX];
-       struct usbd_pipe *      axe_ep[AXE_ENDPT_MAX];
-       int                     axe_if_flags;
-       int                     axe_phyno;
-       struct axe_cdata        axe_cdata;
-       struct callout          axe_stat_ch;
-
-       uint8_t                 axe_enaddr[ETHER_ADDR_LEN];
-
-       int                     axe_refcnt;
-       bool                    axe_dying;
-       bool                    axe_stopping;
-       bool                    axe_attached;
-
-       struct usb_task         axe_tick_task;
-
-       kmutex_t                axe_lock;
-       kmutex_t                axe_mii_lock;
-       kmutex_t                axe_rxlock;
-       kmutex_t                axe_txlock;
-       kcondvar_t              axe_detachcv;
-
-       int                     axe_link;
-
        uint8_t                 axe_ipgs[3];
        uint8_t                 axe_phyaddrs[2];
        uint16_t                sc_pwrcfg;
        uint16_t                sc_lenmask;
 
-       struct timeval          axe_rx_notice;
-       struct timeval          axe_tx_notice;
-       int                     axe_bufsz;
-
-#define sc_if  axe_ec.ec_if
 };
 
 #define        AXE_IS_178_FAMILY(sc)                                             \
@@ -331,97 +257,34 @@
 
 int    axe_match(device_t, cfdata_t, void *);
 void   axe_attach(device_t, device_t, void *);
-int    axe_detach(device_t, int);
-int    axe_activate(device_t, devact_t);
 
 CFATTACH_DECL_NEW(axe, sizeof(struct axe_softc),
-       axe_match, axe_attach, axe_detach, axe_activate);
+       axe_match, axe_attach, usbnet_detach, usbnet_activate);
 
-static int     axe_tx_list_init(struct axe_softc *);
-static int     axe_rx_list_init(struct axe_softc *);
-static int     axe_encap(struct axe_softc *, struct mbuf *, int);
-static void    axe_rxeof(struct usbd_xfer *, void *, usbd_status);
-static void    axe_txeof(struct usbd_xfer *, void *, usbd_status);
-static void    axe_tick(void *);
-static void    axe_tick_task(void *);
-static void    axe_start(struct ifnet *);
-static void    axe_start_locked(struct ifnet *);
-static int     axe_ioctl(struct ifnet *, u_long, void *);
+static void    axe_rx_loop_cb(struct usbnet *, struct usbd_xfer *,
+                              struct usbnet_chain *, uint32_t);
+static unsigned axe_tx_prepare_cb(struct usbnet *, struct mbuf *,
+                                 struct usbnet_chain *);
 static int     axe_init(struct ifnet *);
-static int     axe_init_locked(struct ifnet *);
-static void    axe_stop(struct ifnet *, int);
-static void    axe_stop_locked(struct ifnet *, int);
-static void    axe_watchdog(struct ifnet *);
-static int     axe_miibus_readreg(device_t, int, int, uint16_t *);
-static int     axe_miibus_readreg_locked(device_t, int, int, uint16_t *);
-static int     axe_miibus_writereg(device_t, int, int, uint16_t);
-static int     axe_miibus_writereg_locked(device_t, int, int, uint16_t);
-static void    axe_miibus_statchg(struct ifnet *);
-static int     axe_cmd(struct axe_softc *, int, int, int, void *);
-static void    axe_reset(struct axe_softc *);
-
-static void    axe_setmulti(struct axe_softc *);
-static void    axe_setmulti_locked(struct axe_softc *);
-static void    axe_lock_mii(struct axe_softc *);
-static void    axe_unlock_mii(struct axe_softc *);
+static void    axe_stop_cb(struct ifnet *, int);
+static int     axe_ioctl_cb(struct ifnet *, u_long, void *);
 
 static void    axe_ax88178_init(struct axe_softc *);
 static void    axe_ax88772_init(struct axe_softc *);
 static void    axe_ax88772a_init(struct axe_softc *);
 static void    axe_ax88772b_init(struct axe_softc *);
 
-/* Get exclusive access to the MII registers */
-static void
-axe_lock_mii(struct axe_softc *sc)
-{
-
-       mutex_enter(&sc->axe_lock);
-       sc->axe_refcnt++;
-       mutex_exit(&sc->axe_lock);
-
-       mutex_enter(&sc->axe_mii_lock);
-}
-
-static void
-axe_lock_mii_sc_locked(struct axe_softc *sc)
-{
-       KASSERT(mutex_owned(&sc->axe_lock));
-
-       sc->axe_refcnt++;
-       mutex_enter(&sc->axe_mii_lock);
-}
-
-static void
-axe_unlock_mii(struct axe_softc *sc)
-{
-
-       mutex_exit(&sc->axe_mii_lock);
-       mutex_enter(&sc->axe_lock);
-       if (--sc->axe_refcnt < 0)
-               cv_broadcast(&sc->axe_detachcv);
-       mutex_exit(&sc->axe_lock);
-}
-
-static void
-axe_unlock_mii_sc_locked(struct axe_softc *sc)
-{
-       KASSERT(mutex_owned(&sc->axe_lock));
-
-       mutex_exit(&sc->axe_mii_lock);
-       if (--sc->axe_refcnt < 0)
-               cv_broadcast(&sc->axe_detachcv);
-}
-
-static int
+static usbd_status
 axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
 {
        AXEHIST_FUNC(); AXEHIST_CALLED();
+       struct usbnet * const un = &sc->axe_un;
        usb_device_request_t req;
        usbd_status err;
 
-       KASSERT(mutex_owned(&sc->axe_mii_lock));
+       usbnet_isowned_mii(un);
 
-       if (sc->axe_dying)
+       if (un->un_dying)
                return -1;
 
        DPRINTFN(20, "cmd %#jx index %#jx val %#jx", cmd, index, val, 0);
@@ -435,30 +298,21 @@
        USETW(req.wIndex, index);
        USETW(req.wLength, AXE_CMD_LEN(cmd));
 
-       err = usbd_do_request(sc->axe_udev, &req, buf);
-
-       if (err) {
+       err = usbd_do_request(un->un_udev, &req, buf);
+       if (err)
                DPRINTF("cmd %jd err %jd", cmd, err, 0, 0);
-               return -1;
-       }
-       return 0;
+
+       return err;
 }
 
-static int
-axe_miibus_readreg_locked(device_t dev, int phy, int reg, uint16_t *val)
+static usbd_status
+axe_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val)
 {
        AXEHIST_FUNC(); AXEHIST_CALLED();
-       struct axe_softc *sc = device_private(dev);
+       struct axe_softc * const sc = usbnet_softc(un);
        usbd_status err;
        uint16_t data;
 
-       mutex_enter(&sc->axe_lock);
-       if (sc->axe_dying || sc->axe_phyno != phy) {
-               mutex_exit(&sc->axe_lock);
-               return -1;
-       }
-       mutex_exit(&sc->axe_lock);
-
        DPRINTFN(30, "phy 0x%jx reg 0x%jx\n", phy, reg, 0, 0);
 
        axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
@@ -467,7 +321,7 @@
        axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
 
        if (err) {
-               aprint_error_dev(sc->axe_dev, "read PHY failed\n");
+               aprint_error_dev(un->un_dev, "read PHY failed\n");
                return err;
        }
 
@@ -484,83 +338,40 @@
 
        DPRINTFN(30, "phy 0x%jx reg 0x%jx val %#jx", phy, reg, *val, 0);
 
-       return 0;
-}
-
-static int
-axe_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val)
-{
-       struct axe_softc *sc = device_private(dev);
-       int rv;



Home | Main Index | Thread Index | Old Index