Source-Changes-HG archive

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

[src-draft/trunk]: src/sys/dev/usb Convert urtwn(4) to usbwifi(9)



details:   https://anonhg.NetBSD.org/src-all/rev/ade913d038f0
branches:  trunk
changeset: 1025486:ade913d038f0
user:      Martin Husemann <martin%NetBSD.org@localhost>
date:      Fri Sep 03 17:53:09 2021 +0200

description:
Convert urtwn(4) to usbwifi(9)

diffstat:

 sys/dev/usb/files.usb     |     2 +-
 sys/dev/usb/if_urtwn.c    |  1533 +++++++++++---------------------------------
 sys/dev/usb/if_urtwnvar.h |    22 +-
 3 files changed, 389 insertions(+), 1168 deletions(-)

diffs (truncated from 2968 to 300 lines):

diff -r 8114e22895e4 -r ade913d038f0 sys/dev/usb/files.usb
--- a/sys/dev/usb/files.usb     Fri Sep 03 17:52:14 2021 +0200
+++ b/sys/dev/usb/files.usb     Fri Sep 03 17:53:09 2021 +0200
@@ -519,7 +519,7 @@
 file   dev/usb/if_urndis.c             urndis
 
 # Realtek RTL8188CU/RTL8192CU
-device urtwn: ether, ifnet, arp, wlan, firmload
+device urtwn: ether, ifnet, arp, wlan, firmload, usbwifi
 attach urtwn at usbdevif
 file   dev/usb/if_urtwn.c              urtwn
 
diff -r 8114e22895e4 -r ade913d038f0 sys/dev/usb/if_urtwn.c
--- a/sys/dev/usb/if_urtwn.c    Fri Sep 03 17:52:14 2021 +0200
+++ b/sys/dev/usb/if_urtwn.c    Fri Sep 03 17:53:09 2021 +0200
@@ -79,30 +79,18 @@
 #include <netinet/ip.h>
 #include <netinet/if_inarp.h>
 
-#include <net80211/ieee80211_netbsd.h>
-#include <net80211/ieee80211_var.h>
 #include <net80211/ieee80211_radiotap.h>
 #include <net80211/ieee80211_regdomain.h>
 
 #include <dev/firmload.h>
 
-#include <dev/usb/usb.h>
-#include <dev/usb/usbdi.h>
-#include <dev/usb/usbdivar.h>
-#include <dev/usb/usbdi_util.h>
-#include <dev/usb/usbdevs.h>
+#include <dev/usb/usbwifi.h>
 
 #include <dev/ic/rtwnreg.h>
 #include <dev/ic/rtwn_data.h>
 #include <dev/usb/if_urtwnreg.h>
 #include <dev/usb/if_urtwnvar.h>
 
-/*
- * The sc_write_mtx locking is to prevent sequences of writes from
- * being intermingled with each other. I don't know if this is really
- * needed.  I have added it just to be on the safe side.
- */
-
 #ifdef URTWN_DEBUG
 #define DBG_INIT       __BIT(0)
 #define DBG_FN         __BIT(1)
@@ -229,20 +217,29 @@
 static int     urtwn_match(device_t, cfdata_t, void *);
 static void    urtwn_attach(device_t, device_t, void *);
 static int     urtwn_detach(device_t, int);
-static int     urtwn_activate(device_t, enum devact);
+static void    urtwn_rx_loop(struct usbwifi *, struct usbwifi_chain *,
+                   uint32_t);
+static unsigned        urtwn_tx_prepare(struct usbwifi *,
+                               struct usbwifi_chain *,
+                               uint8_t qid);
+static int     urtwn_init(struct usbwifi *);
+static void    urtwn_stop(struct usbwifi *);
+
 
 CFATTACH_DECL_NEW(urtwn, sizeof(struct urtwn_softc), urtwn_match,
-    urtwn_attach, urtwn_detach, urtwn_activate);
-
-static int     urtwn_open_pipes(struct urtwn_softc *);
-static void    urtwn_close_pipes(struct urtwn_softc *);
-static int     urtwn_alloc_rx_list(struct urtwn_softc *);
-static void    urtwn_free_rx_list(struct urtwn_softc *);
-static int     urtwn_alloc_tx_list(struct urtwn_softc *);
-static void    urtwn_free_tx_list(struct urtwn_softc *);
+    urtwn_attach, urtwn_detach, usbwifi_activate);
+
+static const struct usbwifi_ops urtwn_ops = {
+       .uwo_rx_loop = urtwn_rx_loop,
+       .uwo_tx_prepare = urtwn_tx_prepare,
+       .uwo_init = urtwn_init,
+       .uwo_stop = urtwn_stop,
+};
+
+static int     urtwn_configure_pipes(struct urtwn_softc*, int*, int*);
 static void    urtwn_task(void *);
 static void    urtwn_do_async(struct urtwn_softc *,
-                   void (*)(struct urtwn_softc *, void *), void *, int);
+                       void (*)(struct urtwn_softc *, void *), void *, int);
 static void    urtwn_wait_async(struct urtwn_softc *);
 static int     urtwn_write_region_1(struct urtwn_softc *, uint16_t, uint8_t *,
                    int);
@@ -290,13 +287,6 @@
 static int8_t  urtwn_get_rssi(struct urtwn_softc *, int, void *);
 static int8_t  urtwn_r88e_get_rssi(struct urtwn_softc *, int, void *);
 static void    urtwn_rx_frame(struct urtwn_softc *, uint8_t *, int);
-static void    urtwn_rxeof(struct usbd_xfer *, void *, usbd_status);
-static void    urtwn_txeof(struct usbd_xfer *, void *, usbd_status);
-static int     urtwn_tx(struct urtwn_softc *, struct mbuf *,
-                   struct ieee80211_node *, struct urtwn_tx_data *);
-static struct urtwn_tx_data *
-               urtwn_get_tx_data(struct urtwn_softc *, size_t);
-static void    urtwn_start(struct urtwn_softc *);
 static void    urtwn_watchdog(void*);
 static int     urtwn_r92c_power_on(struct urtwn_softc *);
 static int     urtwn_r92e_power_on(struct urtwn_softc *);
@@ -325,9 +315,6 @@
 static void    urtwn_iq_calib(struct urtwn_softc *, bool);
 static void    urtwn_lc_calib(struct urtwn_softc *);
 static void    urtwn_temp_calib(struct urtwn_softc *);
-static int     urtwn_init(struct urtwn_softc *);
-static void    urtwn_stop(struct urtwn_softc *);
-static void    urtwn_chip_stop(struct urtwn_softc *);
 static void    urtwn_newassoc(struct ieee80211_node *, int);
 static void    urtwn_delay_ms(struct urtwn_softc *, int ms);
 /* Functions for wifi refresh */
@@ -337,17 +324,11 @@
                    const uint8_t [IEEE80211_ADDR_LEN],
                    const uint8_t [IEEE80211_ADDR_LEN]);
 static void    urtwn_vap_delete(struct ieee80211vap *);
-// static int  urtwn_ioctl(struct ifnet *, u_long, void *);
-static void    urtwn_parent(struct ieee80211com *);
 static void    urtwn_get_radiocaps(struct ieee80211com *, int, int *,
                    struct ieee80211_channel chans[]);
 static void    urtwn_scan_start(struct ieee80211com *);
 static void    urtwn_scan_end(struct ieee80211com *);
 static void    urtwn_set_channel(struct ieee80211com *);
-static int     urtwn_transmit(struct ieee80211com *, struct mbuf *);
-static int     urtwn_raw_xmit(struct ieee80211_node *, struct mbuf *,
-                   const struct ieee80211_bpf_params *);
-// static int  urtwn_send_mgmt(struct ieee80211_node *, int, int);
 static void    urtwn_update_mcast(struct ieee80211com *);
 
 /* Aliases. */
@@ -382,26 +363,30 @@
 }
 
 static void
-urtwn_attach(device_t parent, device_t self, void *aux)
+urtwn_attach( device_t parent, device_t self, void *aux)
 {
        struct urtwn_softc *sc = device_private(self);
-       struct ieee80211com *ic = &sc->sc_ic;
+       struct ieee80211com *ic = &sc->sc_uw.uw_ic;
        struct usb_attach_arg *uaa = aux;
        char *devinfop;
        const struct urtwn_dev *dev;
        usb_device_request_t req;
        // NNN loop below size_t i;
-       int error;
-
-       sc->sc_dev = self;
-       sc->sc_udev = uaa->uaa_device;
+       int error, num_rx, num_tx;
 
        /*
-        * Driver Send queue, separate from the (multiple) VAPs
-        * if send queue
+        * Generic usbwifi(9) setup
         */
-       sc->sc_sendq.ifq_maxlen = ifqmaxlen;
-       IFQ_LOCK_INIT(&sc->sc_sendq);
+       sc->sc_uw.uw_sc = sc;
+       sc->sc_uw.uw_dev = self;
+       sc->sc_uw.uw_udev = uaa->uaa_device;
+       sc->sc_uw.uw_ops = &urtwn_ops;
+       sc->sc_uw.uw_rx_xfer_flags = USBD_SHORT_XFER_OK;
+       sc->sc_uw.uw_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
+       sc->sc_uw.uw_rx_bufsz = URTWN_RXBUFSZ;  /* size of a single buffer */
+       sc->sc_uw.uw_tx_bufsz = URTWN_TXBUFSZ;  /* for RX/TX */
+       sc->sc_uw.uw_tx_list_cnt = URTWN_TX_LIST_COUNT; /* max RX/TX buffers */
+       sc->sc_uw.uw_rx_list_cnt = URTWN_RX_LIST_COUNT; /* in the rings */
 
        dev = urtwn_lookup(urtwn_devs, uaa->uaa_vendor, uaa->uaa_product);
        if (dev != NULL && ISSET(dev->flags, FLAG_RTL8188E))
@@ -412,9 +397,10 @@
        aprint_naive("\n");
        aprint_normal("\n");
 
-       DPRINTFN(DBG_FN, ("%s: %s\n", device_xname(sc->sc_dev), __func__));
-
-       devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
+       DPRINTFN(DBG_FN, ("%s: %s\n", device_xname(sc->sc_uw.uw_dev),
+           __func__));
+
+       devinfop = usbd_devinfo_alloc(sc->sc_uw.uw_udev, 0);
        aprint_normal_dev(self, "%s\n", devinfop);
        usbd_devinfo_free(devinfop);
 
@@ -424,15 +410,13 @@
        USETW(req.wIndex, UHF_PORT_SUSPEND);
        USETW(req.wLength, 0);
 
-       (void) usbd_do_request(sc->sc_udev, &req, 0);
+       (void) usbd_do_request(sc->sc_uw.uw_udev, &req, 0);
 
        mutex_init(&sc->sc_task_mtx, MUTEX_DEFAULT, IPL_NET);
-       mutex_init(&sc->sc_tx_mtx, MUTEX_DEFAULT, IPL_SOFTNET);
-       mutex_init(&sc->sc_rx_mtx, MUTEX_DEFAULT, IPL_SOFTNET);
-       mutex_init(&sc->sc_fwcmd_mtx, MUTEX_DEFAULT, IPL_NONE);
-       mutex_init(&sc->sc_write_mtx, MUTEX_DEFAULT, IPL_NONE);
-
-       mutex_enter(&sc->sc_write_mtx);
+
+       usbwifi_attach(&sc->sc_uw, "urtwndet");
+
+       usbwifi_lock_core(&sc->sc_uw);
        usb_init_task(&sc->sc_task, urtwn_task, sc, 0);
 
        /* NNN make these callouts use a vap ... in vap create??? */
@@ -441,7 +425,7 @@
        callout_init(&sc->sc_watchdog_to, CALLOUT_MPSAFE);
        callout_setfunc(&sc->sc_watchdog_to, urtwn_watchdog, sc);
 
-       error = usbd_set_config_no(sc->sc_udev, 1, 0);
+       error = usbd_set_config_no(sc->sc_uw.uw_udev, 1, 0);
        if (error != 0) {
                aprint_error_dev(self, "failed to set configuration"
                    ", err=%s\n", usbd_errstr(error));
@@ -449,7 +433,8 @@
        }
 
        /* Get the first interface handle. */
-       error = usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface);
+       error = usbd_device2interface_handle(sc->sc_uw.uw_udev, 0,
+           &sc->sc_uw.uw_iface);
        if (error != 0) {
                aprint_error_dev(self, "could not get interface handle\n");
                goto fail;
@@ -488,22 +473,15 @@
            "8188CUS", sc->ntxchains, sc->nrxchains,
            ether_sprintf(ic->ic_macaddr));
 
-       error = urtwn_open_pipes(sc);
+       error = urtwn_configure_pipes(sc, &num_tx, &num_rx);
        if (error != 0) {
-               aprint_error_dev(sc->sc_dev, "could not open pipes\n");
+               aprint_error_dev(sc->sc_uw.uw_dev,
+                   "could not confiugre pipes\n");
                goto fail;
        }
        aprint_normal_dev(self, "%d rx pipe%s, %d tx pipe%s\n",
-           sc->rx_npipe, sc->rx_npipe > 1 ? "s" : "",
-           sc->tx_npipe, sc->tx_npipe > 1 ? "s" : "");
-
-       /*
-        * Setup the 802.11 device.
-        */
-       ic->ic_softc = sc;
-       ic->ic_name = device_xname(self);
-       ic->ic_phytype = IEEE80211_T_OFDM;      /* Not only, but not used. */
-       ic->ic_opmode = IEEE80211_M_STA;        /* Default to BSS mode. */
+           num_rx, num_rx > 1 ? "s" : "",
+           num_tx, num_tx > 1 ? "s" : "");
 
        /* Set device capabilities. */
        ic->ic_caps =
@@ -536,11 +514,6 @@
        ic->ic_cryptocaps = 0;
 #endif
 
-       ic->ic_txstream = sc->ntxchains;
-       ic->ic_rxstream = sc->nrxchains;
-
-       ic->ic_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
-
        /* XXX TODO: setup regdomain if URTW_EPROM_CHANPLAN_BY_HW bit is set.*/
        urtwn_get_radiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
            ic->ic_channels);
@@ -549,62 +522,40 @@
         * Initialize the global (non-VAP specific) structures and create
         * the VAP list.
         */
-       ieee80211_ifattach(ic);
-
-       /* override default methods NNN Need more here? */
+       usbwifi_ic_attach(&sc->sc_uw, num_tx, num_rx,
+            IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
+
+       /* override default methods */
        ic->ic_newassoc = urtwn_newassoc;
        ic->ic_wme.wme_update = urtwn_wme_update;
        ic->ic_vap_create = urtwn_vap_create;
        ic->ic_vap_delete = urtwn_vap_delete;
-       ic->ic_parent = urtwn_parent;
        ic->ic_scan_start = urtwn_scan_start;
        ic->ic_scan_end = urtwn_scan_end;
        ic->ic_getradiocaps = urtwn_get_radiocaps;
        ic->ic_set_channel = urtwn_set_channel;
-       ic->ic_transmit = urtwn_transmit;
-       // ic->ic_send_mgmt = urtwn_send_mgmt;
-       ic->ic_raw_xmit = urtwn_raw_xmit;
        ic->ic_update_mcast = urtwn_update_mcast;
-        
-       sc->sc_rxtap_len = sizeof(sc->sc_rxtapu);
-       sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
+
+       sc->sc_rxtap.wr_ihdr.it_len = htole16(sizeof(sc->sc_rxtapu));
        sc->sc_rxtap.wr_ihdr.it_present = htole32(URTWN_RX_RADIOTAP_PRESENT);
 
        sc->sc_txtap_len = sizeof(sc->sc_txtapu);
        sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
        sc->sc_txtap.wt_ihdr.it_present = htole32(URTWN_TX_RADIOTAP_PRESENT);
 
-       usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);



Home | Main Index | Thread Index | Old Index