Source-Changes-HG archive

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

[src/trunk]: src/sys/dev/usb port kue(4) to usbnet(9). interesting notes:



details:   https://anonhg.NetBSD.org/src/rev/8952623b1e76
branches:  trunk
changeset: 964773:8952623b1e76
user:      mrg <mrg%NetBSD.org@localhost>
date:      Fri Aug 16 08:51:09 2019 +0000

description:
port kue(4) to usbnet(9).  interesting notes:
- move KUE_RXFILT_PROMISC setting into kue_setiff() from kue_init()
  to avoid multiple setting KUE_CMD_SET_PKT_FILTER reg multiple times
- software-only constructs moved from if_kuereg.h into if_kue.c
- kue is the first (umb(4) will need it to, i think) to have its own
  autoconf detach routine remain
- un_tx_xfer_flags is 0 here, not USBD_FORCE_SHORT_XFER
- fix a potential data exposure (but probably not without a USB
  protocol tap).  kue needs the transfers to be 64-byte aligned, and
  while i doubt it sends more than the frame provided, were sending
  random kernel data (whatever was the 0-63 bytes to alignment) to
  the device.

diffstat says:
2 files changed, 189 insertions(+), 739 deletions(-)

diffstat:

 sys/dev/usb/if_kue.c    |  898 ++++++++++-------------------------------------
 sys/dev/usb/if_kuereg.h |   66 +---
 2 files changed, 207 insertions(+), 757 deletions(-)

diffs (truncated from 1296 to 300 lines):

diff -r a76892e5b1d6 -r 8952623b1e76 sys/dev/usb/if_kue.c
--- a/sys/dev/usb/if_kue.c      Fri Aug 16 08:38:21 2019 +0000
+++ b/sys/dev/usb/if_kue.c      Fri Aug 16 08:51:09 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_kue.c,v 1.97 2019/08/01 00:10:22 mrg Exp $  */
+/*     $NetBSD: if_kue.c,v 1.98 2019/08/16 08:51:09 mrg Exp $  */
 
 /*
  * Copyright (c) 1997, 1998, 1999, 2000
@@ -71,7 +71,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_kue.c,v 1.97 2019/08/01 00:10:22 mrg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_kue.c,v 1.98 2019/08/16 08:51:09 mrg Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -79,33 +79,15 @@
 #endif
 
 #include <sys/param.h>
-#include <sys/systm.h>
-#include <sys/sockio.h>
-#include <sys/mbuf.h>
 #include <sys/kmem.h>
-#include <sys/kernel.h>
-#include <sys/socket.h>
-#include <sys/device.h>
-#include <sys/proc.h>
-#include <sys/rndsource.h>
 
-#include <net/if.h>
-#include <net/if_arp.h>
-#include <net/if_dl.h>
-#include <net/bpf.h>
-#include <net/if_ether.h>
+#include <dev/usb/usbnet.h>
 
 #ifdef INET
 #include <netinet/in.h>
 #include <netinet/if_inarp.h>
 #endif
 
-#include <dev/usb/usb.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_kuereg.h>
 #include <dev/usb/kue_fw.h>
 
@@ -118,6 +100,28 @@
 #define DPRINTFN(n, x)
 #endif
 
+struct kue_type {
+       uint16_t                kue_vid;
+       uint16_t                kue_did;
+};
+
+struct kue_softc {
+       struct usbnet           kue_un;
+
+       struct kue_ether_desc   kue_desc;
+       uint16_t                kue_rxfilt;
+       uint8_t                 *kue_mcfilters;
+};
+
+#define KUE_MCFILT(x, y)       \
+       (uint8_t *)&(sc->kue_mcfilters[y * ETHER_ADDR_LEN])
+
+#define KUE_BUFSZ              1536
+#define KUE_MIN_FRAMELEN       60
+
+#define KUE_RX_LIST_CNT                1
+#define KUE_TX_LIST_CNT                1
+
 /*
  * Various supported device vendors/products.
  */
@@ -160,37 +164,36 @@
 int kue_match(device_t, cfdata_t, void *);
 void kue_attach(device_t, device_t, void *);
 int kue_detach(device_t, int);
-int kue_activate(device_t, enum devact);
 
 CFATTACH_DECL_NEW(kue, sizeof(struct kue_softc), kue_match, kue_attach,
-    kue_detach, kue_activate);
+    kue_detach, usbnet_activate);
+
+static void kue_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t);
+static unsigned kue_tx_prepare(struct usbnet *, struct mbuf *,
+                              struct usbnet_chain *);
+static int kue_ioctl_cb(struct ifnet *, u_long, void *);
+static int kue_init(struct ifnet *);
 
-static int kue_tx_list_init(struct kue_softc *);
-static int kue_rx_list_init(struct kue_softc *);
-static int kue_send(struct kue_softc *, struct mbuf *, int);
-static int kue_open_pipes(struct kue_softc *);
-static void kue_rxeof(struct usbd_xfer *, void *, usbd_status);
-static void kue_txeof(struct usbd_xfer *, void *, usbd_status);
-static void kue_start(struct ifnet *);
-static int kue_ioctl(struct ifnet *, u_long, void *);
-static void kue_init(void *);
-static void kue_stop(struct kue_softc *);
-static void kue_watchdog(struct ifnet *);
+static struct usbnet_ops kue_ops = {
+       .uno_ioctl = kue_ioctl_cb,
+       .uno_tx_prepare = kue_tx_prepare,
+       .uno_rx_loop = kue_rx_loop,
+       .uno_init = kue_init,
+};
 
-static void kue_setmulti(struct kue_softc *);
-static void kue_reset(struct kue_softc *);
+static void kue_setiff(struct usbnet *);
+static void kue_reset(struct usbnet *);
 
-static usbd_status kue_ctl(struct kue_softc *, int, uint8_t,
+static usbd_status kue_ctl(struct usbnet *, int, uint8_t,
                           uint16_t, void *, uint32_t);
-static usbd_status kue_setword(struct kue_softc *, uint8_t, uint16_t);
-static int kue_load_fw(struct kue_softc *);
+static int kue_load_fw(struct usbnet *);
 
 static usbd_status
-kue_setword(struct kue_softc *sc, uint8_t breq, uint16_t word)
+kue_setword(struct usbnet *un, uint8_t breq, uint16_t word)
 {
        usb_device_request_t    req;
 
-       DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->kue_dev),__func__));
+       DPRINTFN(10,("%s: %s: enter\n", device_xname(un->un_dev),__func__));
 
        req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
        req.bRequest = breq;
@@ -198,16 +201,16 @@
        USETW(req.wIndex, 0);
        USETW(req.wLength, 0);
 
-       return usbd_do_request(sc->kue_udev, &req, NULL);
+       return usbd_do_request(un->un_udev, &req, NULL);
 }
 
 static usbd_status
-kue_ctl(struct kue_softc *sc, int rw, uint8_t breq, uint16_t val,
+kue_ctl(struct usbnet *un, int rw, uint8_t breq, uint16_t val,
        void *data, uint32_t len)
 {
        usb_device_request_t    req;
 
-       DPRINTFN(10,("%s: %s: enter, len=%d\n", device_xname(sc->kue_dev),
+       DPRINTFN(10,("%s: %s: enter, len=%d\n", device_xname(un->un_dev),
                     __func__, len));
 
        if (rw == KUE_CTL_WRITE)
@@ -220,16 +223,16 @@
        USETW(req.wIndex, 0);
        USETW(req.wLength, len);
 
-       return usbd_do_request(sc->kue_udev, &req, data);
+       return usbd_do_request(un->un_udev, &req, data);
 }
 
 static int
-kue_load_fw(struct kue_softc *sc)
+kue_load_fw(struct usbnet *un)
 {
        usb_device_descriptor_t dd;
        usbd_status             err;
 
-       DPRINTFN(1,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__));
+       DPRINTFN(1,("%s: %s: enter\n", device_xname(un->un_dev), __func__));
 
        /*
         * First, check if we even need to load the firmware.
@@ -245,54 +248,54 @@
         * it's probed while the firmware is still loaded and
         * running.
         */
-       if (usbd_get_device_desc(sc->kue_udev, &dd))
+       if (usbd_get_device_desc(un->un_udev, &dd))
                return EIO;
        if (UGETW(dd.bcdDevice) == KUE_WARM_REV) {
                printf("%s: warm boot, no firmware download\n",
-                      device_xname(sc->kue_dev));
+                      device_xname(un->un_dev));
                return 0;
        }
 
        printf("%s: cold boot, downloading firmware\n",
-              device_xname(sc->kue_dev));
+              device_xname(un->un_dev));
 
        /* Load code segment */
        DPRINTFN(1,("%s: kue_load_fw: download code_seg\n",
-                   device_xname(sc->kue_dev)));
+                   device_xname(un->un_dev)));
        /*XXXUNCONST*/
-       err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
+       err = kue_ctl(un, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
            0, __UNCONST(kue_code_seg), sizeof(kue_code_seg));
        if (err) {
                printf("%s: failed to load code segment: %s\n",
-                   device_xname(sc->kue_dev), usbd_errstr(err));
+                   device_xname(un->un_dev), usbd_errstr(err));
                        return EIO;
        }
 
        /* Load fixup segment */
        DPRINTFN(1,("%s: kue_load_fw: download fix_seg\n",
-                   device_xname(sc->kue_dev)));
+                   device_xname(un->un_dev)));
        /*XXXUNCONST*/
-       err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
+       err = kue_ctl(un, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
            0, __UNCONST(kue_fix_seg), sizeof(kue_fix_seg));
        if (err) {
                printf("%s: failed to load fixup segment: %s\n",
-                   device_xname(sc->kue_dev), usbd_errstr(err));
+                   device_xname(un->un_dev), usbd_errstr(err));
                        return EIO;
        }
 
        /* Send trigger command. */
        DPRINTFN(1,("%s: kue_load_fw: download trig_seg\n",
-                   device_xname(sc->kue_dev)));
+                   device_xname(un->un_dev)));
        /*XXXUNCONST*/
-       err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
+       err = kue_ctl(un, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
            0, __UNCONST(kue_trig_seg), sizeof(kue_trig_seg));
        if (err) {
                printf("%s: failed to load trigger segment: %s\n",
-                   device_xname(sc->kue_dev), usbd_errstr(err));
+                   device_xname(un->un_dev), usbd_errstr(err));
                        return EIO;
        }
 
-       usbd_delay_ms(sc->kue_udev, 10);
+       usbd_delay_ms(un->un_udev, 10);
 
        /*
         * Reload device descriptor.
@@ -302,37 +305,44 @@
         * code. This confuses the quirk mechanism, which is
         * dependent on the revision data.
         */
-       (void)usbd_reload_device_desc(sc->kue_udev);
+       (void)usbd_reload_device_desc(un->un_udev);
 
-       DPRINTFN(1,("%s: %s: done\n", device_xname(sc->kue_dev), __func__));
+       DPRINTFN(1,("%s: %s: done\n", device_xname(un->un_dev), __func__));
 
        /* Reset the adapter. */
-       kue_reset(sc);
+       kue_reset(un);
 
        return 0;
 }
 
 static void
-kue_setmulti(struct kue_softc *sc)
+kue_setiff(struct usbnet *un)
 {
-       struct ethercom         *ec = &sc->kue_ec;
-       struct ifnet            *ifp = GET_IFP(sc);
+       struct ethercom *       ec = usbnet_ec(un);
+       struct kue_softc *      sc = usbnet_softc(un);
+       struct ifnet * const    ifp = usbnet_ifp(un);
        struct ether_multi      *enm;
        struct ether_multistep  step;
        int                     i;
 
-       DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->kue_dev), __func__));
+       DPRINTFN(5,("%s: %s: enter\n", device_xname(un->un_dev), __func__));
+
+        /* If we want promiscuous mode, set the allframes bit. */
+       if (ifp->if_flags & IFF_PROMISC)
+               sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
+       else
+               sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
 
        if (ifp->if_flags & IFF_PROMISC) {
 allmulti:
                ifp->if_flags |= IFF_ALLMULTI;
-               sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
+               sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI|KUE_RXFILT_PROMISC;
                sc->kue_rxfilt &= ~KUE_RXFILT_MULTICAST;
-               kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
+               kue_setword(un, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
                return;
        }
 
-       sc->kue_rxfilt &= ~KUE_RXFILT_ALLMULTI;
+       sc->kue_rxfilt &= ~(KUE_RXFILT_ALLMULTI|KUE_RXFILT_PROMISC);
 



Home | Main Index | Thread Index | Old Index