Source-Changes-HG archive

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

[src/trunk]: src/sys/dev/usb Use modern interfaces and kill tsleep/wakeup. F...



details:   https://anonhg.NetBSD.org/src/rev/473974a67946
branches:  trunk
changeset: 467144:473974a67946
user:      skrll <skrll%NetBSD.org@localhost>
date:      Wed Jan 15 08:20:13 2020 +0000

description:
Use modern interfaces and kill tsleep/wakeup.  From nick-nhusb branch.

diffstat:

 sys/dev/usb/if_otus.c     |  52 +++++++++++++++++++++++-----------------------
 sys/dev/usb/if_otusvar.h  |   4 ++-
 sys/dev/usb/if_upgt.c     |  19 ++++++++++++----
 sys/dev/usb/if_upgtvar.h  |   3 +-
 sys/dev/usb/if_urtwn.c    |  12 +++++++---
 sys/dev/usb/if_urtwnvar.h |   3 +-
 sys/dev/usb/if_zyd.c      |  31 ++++++++++++++++-----------
 sys/dev/usb/if_zydreg.h   |   5 +++-
 8 files changed, 77 insertions(+), 52 deletions(-)

diffs (truncated from 497 to 300 lines):

diff -r 06eb140c73ec -r 473974a67946 sys/dev/usb/if_otus.c
--- a/sys/dev/usb/if_otus.c     Wed Jan 15 08:13:20 2020 +0000
+++ b/sys/dev/usb/if_otus.c     Wed Jan 15 08:20:13 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_otus.c,v 1.39 2019/09/14 12:37:34 maxv Exp $        */
+/*     $NetBSD: if_otus.c,v 1.40 2020/01/15 08:20:13 skrll Exp $       */
 /*     $OpenBSD: if_otus.c,v 1.18 2010/08/27 17:08:00 jsg Exp $        */
 
 /*-
@@ -23,7 +23,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_otus.c,v 1.39 2019/09/14 12:37:34 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_otus.c,v 1.40 2020/01/15 08:20:13 skrll Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -630,6 +630,8 @@
        aprint_normal_dev(sc->sc_dev, "%s\n", devinfop);
        usbd_devinfo_free(devinfop);
 
+       cv_init(&sc->sc_task_cv, "otustsk");
+       cv_init(&sc->sc_cmd_cv, "otuscmd");
        mutex_init(&sc->sc_cmd_mtx,   MUTEX_DEFAULT, IPL_NONE);
        mutex_init(&sc->sc_task_mtx,  MUTEX_DEFAULT, IPL_NET);
        mutex_init(&sc->sc_tx_mtx,    MUTEX_DEFAULT, IPL_NONE);
@@ -678,8 +680,10 @@
 
        DPRINTFN(DBG_FN, sc, "\n");
 
+       mutex_spin_enter(&sc->sc_task_mtx);
        while (sc->sc_cmdq.queued > 0)
-               tsleep(&sc->sc_cmdq, 0, "sc_cmdq", 0);
+               cv_wait(&sc->sc_task_cv, &sc->sc_task_mtx);
+       mutex_spin_exit(&sc->sc_task_mtx);
 }
 
 Static int
@@ -720,6 +724,9 @@
        mutex_destroy(&sc->sc_tx_mtx);
        mutex_destroy(&sc->sc_task_mtx);
        mutex_destroy(&sc->sc_cmd_mtx);
+       cv_destroy(&sc->sc_task_cv);
+       cv_destroy(&sc->sc_cmd_cv);
+
        return 0;
 }
 
@@ -1242,34 +1249,29 @@
        struct otus_softc *sc;
        struct otus_host_cmd_ring *ring;
        struct otus_host_cmd *cmd;
-       int s;
 
        sc = arg;
 
        DPRINTFN(DBG_FN, sc, "\n");
 
        /* Process host commands. */
-       s = splusb();
        mutex_spin_enter(&sc->sc_task_mtx);
        ring = &sc->sc_cmdq;
        while (ring->next != ring->cur) {
                cmd = &ring->cmd[ring->next];
                mutex_spin_exit(&sc->sc_task_mtx);
-               splx(s);
 
                /* Callback. */
                DPRINTFN(DBG_CMD, sc, "cb=%p queued=%d\n", cmd->cb,
                    ring->queued);
                cmd->cb(sc, cmd->data);
 
-               s = splusb();
                mutex_spin_enter(&sc->sc_task_mtx);
                ring->queued--;
                ring->next = (ring->next + 1) % OTUS_HOST_CMD_RING_COUNT;
        }
+       cv_signal(&sc->sc_task_cv);
        mutex_spin_exit(&sc->sc_task_mtx);
-       wakeup(ring);
-       splx(s);
 }
 
 Static void
@@ -1278,12 +1280,10 @@
 {
        struct otus_host_cmd_ring *ring;
        struct otus_host_cmd *cmd;
-       int s;
+       bool sched = false;
 
        DPRINTFN(DBG_FN, sc, "cb=%p\n", cb);
 
-
-       s = splusb();
        mutex_spin_enter(&sc->sc_task_mtx);
        ring = &sc->sc_cmdq;
        cmd = &ring->cmd[ring->cur];
@@ -1294,13 +1294,12 @@
 
        /* If there is no pending command already, schedule a task. */
        if (++ring->queued == 1) {
-               mutex_spin_exit(&sc->sc_task_mtx);
-               usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
+               sched = true;
        }
-       else
-               mutex_spin_exit(&sc->sc_task_mtx);
-       wakeup(ring);
-       splx(s);
+       cv_signal(&sc->sc_task_cv);
+       mutex_spin_exit(&sc->sc_task_mtx);
+       if (sched)
+               usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
 }
 
 Static int
@@ -1401,7 +1400,7 @@
 {
        struct otus_tx_cmd *cmd;
        struct ar_cmd_hdr *hdr;
-       int s, xferlen, error;
+       int xferlen, error;
 
        DPRINTFN(DBG_FN, sc, "\n");
 
@@ -1427,14 +1426,12 @@
        DPRINTFN(DBG_CMD, sc, "sending command code=0x%02x len=%d token=%d\n",
            code, ilen, hdr->token);
 
-       s = splusb();
        cmd->odata = odata;
        cmd->done = 0;
        usbd_setup_xfer(cmd->xfer, cmd, cmd->buf, xferlen,
            USBD_FORCE_SHORT_XFER, OTUS_CMD_TIMEOUT, NULL);
        error = usbd_sync_transfer(cmd->xfer);
        if (error != 0) {
-               splx(s);
                mutex_exit(&sc->sc_cmd_mtx);
 #if defined(DIAGNOSTIC) || defined(OTUS_DEBUG) /* XXX: kill some noise */
                aprint_error_dev(sc->sc_dev,
@@ -1444,9 +1441,8 @@
                return EIO;
        }
        if (!cmd->done)
-               error = tsleep(cmd, PCATCH, "otuscmd", hz);
+               error = cv_timedwait_sig(&sc->sc_cmd_cv, &sc->sc_cmd_mtx, hz);
        cmd->odata = NULL;      /* In case answer is received too late. */
-       splx(s);
        mutex_exit(&sc->sc_cmd_mtx);
        if (error != 0) {
                aprint_error_dev(sc->sc_dev,
@@ -1501,7 +1497,7 @@
        DPRINTFN(DBG_FN, DBG_NO_SC, "\n");
 
        on = malloc(sizeof(*on), M_DEVBUF, M_NOWAIT | M_ZERO);
-       return &on->ni;
+       return on ? &on->ni : NULL;
 }
 
 Static int
@@ -1647,14 +1643,18 @@
        if ((hdr->code & 0xc0) != 0xc0) {
                DPRINTFN(DBG_RX, sc, "received reply code=0x%02x len=%d token=%d\n",
                    hdr->code, hdr->len, hdr->token);
+               mutex_enter(&sc->sc_cmd_mtx);
                cmd = &sc->sc_tx_cmd;
-               if (__predict_false(hdr->token != cmd->token))
+               if (__predict_false(hdr->token != cmd->token)) {
+                       mutex_exit(&sc->sc_cmd_mtx);
                        return;
+               }
                /* Copy answer into caller's supplied buffer. */
                if (cmd->odata != NULL)
                        memcpy(cmd->odata, &hdr[1], hdr->len);
                cmd->done = 1;
-               wakeup(cmd);
+               cv_signal(&sc->sc_cmd_cv);
+               mutex_exit(&sc->sc_cmd_mtx);
                return;
        }
 
diff -r 06eb140c73ec -r 473974a67946 sys/dev/usb/if_otusvar.h
--- a/sys/dev/usb/if_otusvar.h  Wed Jan 15 08:13:20 2020 +0000
+++ b/sys/dev/usb/if_otusvar.h  Wed Jan 15 08:20:13 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_otusvar.h,v 1.10 2019/10/05 23:27:20 mrg Exp $      */
+/*     $NetBSD: if_otusvar.h,v 1.11 2020/01/15 08:20:13 skrll Exp $    */
 /*     $OpenBSD: if_otusreg.h,v 1.6 2009/04/06 18:17:01 damien Exp $   */
 
 /*-
@@ -199,6 +199,8 @@
        unsigned int                    sc_write_idx;
        uint32_t                        sc_led_state;
 
+       kcondvar_t                      sc_task_cv;
+       kcondvar_t                      sc_cmd_cv;
        kmutex_t                        sc_cmd_mtx;
        kmutex_t                        sc_task_mtx;
        kmutex_t                        sc_write_mtx;
diff -r 06eb140c73ec -r 473974a67946 sys/dev/usb/if_upgt.c
--- a/sys/dev/usb/if_upgt.c     Wed Jan 15 08:13:20 2020 +0000
+++ b/sys/dev/usb/if_upgt.c     Wed Jan 15 08:20:13 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_upgt.c,v 1.26 2019/09/14 12:53:24 maxv Exp $        */
+/*     $NetBSD: if_upgt.c,v 1.27 2020/01/15 08:20:13 skrll Exp $       */
 /*     $OpenBSD: if_upgt.c,v 1.49 2010/04/20 22:05:43 tedu Exp $ */
 
 /*
@@ -18,7 +18,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_upgt.c,v 1.26 2019/09/14 12:53:24 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_upgt.c,v 1.27 2020/01/15 08:20:13 skrll Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -1017,7 +1017,11 @@
                            "could not transmit EEPROM data URB\n");
                        return EIO;
                }
-               if (tsleep(sc, 0, "eeprom_request", UPGT_USB_TIMEOUT)) {
+
+               mutex_enter(&sc->sc_mtx);
+               int res = cv_timedwait(&sc->sc_cv, &sc->sc_mtx, UPGT_USB_TIMEOUT);
+               mutex_exit(&sc->sc_mtx);
+               if (res) {
                        aprint_error_dev(sc->sc_dev,
                            "timeout while waiting for EEPROM data\n");
                        return EIO;
@@ -1792,12 +1796,15 @@
                DPRINTF(2, "%s: received EEPROM block (offset=%d, len=%d)\n",
                        device_xname(sc->sc_dev), eeprom_offset, eeprom_len);
 
+               mutex_enter(&sc->sc_mtx);
                memcpy(sc->sc_eeprom + eeprom_offset,
                    data_rx->buf + sizeof(struct upgt_lmac_eeprom) + 4,
                    eeprom_len);
 
-               /* EEPROM data has arrived in time, wakeup tsleep() */
-               wakeup(sc);
+               /* EEPROM data has arrived in time, wakeup upgt_eeprom_read */
+               /* Note eeprom data arrived */
+               cv_broadcast(&sc->sc_cv);
+               mutex_exit(&sc->sc_mtx);
        } else
        if (h1_type == UPGT_H1_TYPE_CTRL &&
            h2_type == UPGT_H2_TYPE_TX_DONE) {
@@ -2322,6 +2329,7 @@
 
        data_cmd->buf = usbd_get_buffer(data_cmd->xfer);
 
+       cv_init(&sc->sc_cv, "upgteeprom");
        mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);
 
        return 0;
@@ -2368,6 +2376,7 @@
        }
 
        mutex_destroy(&sc->sc_mtx);
+       cv_destroy(&sc->sc_cv);
 }
 
 static int
diff -r 06eb140c73ec -r 473974a67946 sys/dev/usb/if_upgtvar.h
--- a/sys/dev/usb/if_upgtvar.h  Wed Jan 15 08:13:20 2020 +0000
+++ b/sys/dev/usb/if_upgtvar.h  Wed Jan 15 08:20:13 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_upgtvar.h,v 1.4 2019/10/05 23:27:20 mrg Exp $       */
+/*     $NetBSD: if_upgtvar.h,v 1.5 2020/01/15 08:20:13 skrll Exp $     */
 /*     $OpenBSD: if_upgtvar.h,v 1.15 2009/08/10 20:02:19 deraadt Exp $ */
 
 /*
@@ -416,6 +416,7 @@
        struct upgt_data         cmd_data;
        int                      tx_queued;
        kmutex_t                 sc_mtx;
+       kcondvar_t               sc_cv;
 
        uint8_t                  sc_device_type;
        struct ieee80211com      sc_ic;
diff -r 06eb140c73ec -r 473974a67946 sys/dev/usb/if_urtwn.c
--- a/sys/dev/usb/if_urtwn.c    Wed Jan 15 08:13:20 2020 +0000
+++ b/sys/dev/usb/if_urtwn.c    Wed Jan 15 08:20:13 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_urtwn.c,v 1.79 2020/01/15 08:13:20 skrll Exp $      */
+/*     $NetBSD: if_urtwn.c,v 1.80 2020/01/15 08:20:13 skrll Exp $      */
 /*     $OpenBSD: if_urtwn.c,v 1.42 2015/02/10 23:25:46 mpi Exp $       */
 
 /*-
@@ -25,7 +25,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_urtwn.c,v 1.79 2020/01/15 08:13:20 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_urtwn.c,v 1.80 2020/01/15 08:20:13 skrll Exp $");
 
 #ifdef _KERNEL_OPT



Home | Main Index | Thread Index | Old Index