Source-Changes-HG archive

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

[src/trunk]: src/sys/dev/i2c Expand code covered by sc->sc_mtx, to make sure ...



details:   https://anonhg.NetBSD.org/src/rev/101cbd4c9533
branches:  trunk
changeset: 836188:101cbd4c9533
user:      bouyer <bouyer%NetBSD.org@localhost>
date:      Mon Oct 01 09:39:20 2018 +0000

description:
Expand code covered by sc->sc_mtx, to make sure an interrupt would not be
handled before the cv_timedwait_sig() call, or while polling.
Seems to fix "sunxitwi0: send STOP failed" messages frequently seen related
to axp20x0 sensors.

diffstat:

 sys/dev/i2c/gttwsi_core.c |  31 +++++++++++++++++++------------
 1 files changed, 19 insertions(+), 12 deletions(-)

diffs (84 lines):

diff -r a3e47d0a98fc -r 101cbd4c9533 sys/dev/i2c/gttwsi_core.c
--- a/sys/dev/i2c/gttwsi_core.c Mon Oct 01 08:23:53 2018 +0000
+++ b/sys/dev/i2c/gttwsi_core.c Mon Oct 01 09:39:20 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gttwsi_core.c,v 1.7 2018/06/18 12:42:29 jakllsch Exp $ */
+/*     $NetBSD: gttwsi_core.c,v 1.8 2018/10/01 09:39:20 bouyer Exp $   */
 /*
  * Copyright (c) 2008 Eiji Kawauchi.
  * All rights reserved.
@@ -66,7 +66,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: gttwsi_core.c,v 1.7 2018/06/18 12:42:29 jakllsch Exp $");
+__KERNEL_RCSID(0, "$NetBSD: gttwsi_core.c,v 1.8 2018/10/01 09:39:20 bouyer Exp $");
 #include "locators.h"
 
 #include <sys/param.h>
@@ -192,15 +192,15 @@
        struct gttwsi_softc *sc = arg;
        uint32_t val;
 
+       mutex_enter(&sc->sc_mtx);
        val = gttwsi_read_4(sc, TWSI_CONTROL);
        if (val & CONTROL_IFLG) {
                gttwsi_write_4(sc, TWSI_CONTROL, val & ~CONTROL_INTEN);
-               mutex_enter(&sc->sc_mtx);
                cv_broadcast(&sc->sc_cv);
                mutex_exit(&sc->sc_mtx);
-
                return 1;       /* handled */
        }
+       mutex_exit(&sc->sc_mtx);
        return 0;
 }
 
@@ -377,6 +377,7 @@
                control |= CONTROL_INTEN;
        if (sc->sc_iflg_rwc)
                control |= CONTROL_IFLG;
+       mutex_enter(&sc->sc_mtx);
        gttwsi_write_4(sc, TWSI_CONTROL, control | CONTROL_TWSIEN);
 
        timo = 0;
@@ -385,17 +386,21 @@
                if (control & CONTROL_IFLG)
                        break;
                if (!(flags & I2C_F_POLL)) {
-                       mutex_enter(&sc->sc_mtx);
                        error = cv_timedwait_sig(&sc->sc_cv, &sc->sc_mtx, hz);
-                       mutex_exit(&sc->sc_mtx);
                        if (error)
-                               return error;
+                               break;
+               } else {
+                       DELAY(TWSI_RETRY_DELAY);
+                       if (timo++ > 1000000)   /* 1sec */
+                               break;
                }
-               DELAY(TWSI_RETRY_DELAY);
-               if (timo++ > 1000000)   /* 1sec */
-                       break;
        }
-
+       if ((control & CONTROL_IFLG) == 0) {
+               aprint_error_dev(sc->sc_dev,
+                   "gttwsi_wait(): timeout, control=0x%x\n", control);
+               error = EWOULDBLOCK;
+               goto end;
+       }
        status = gttwsi_read_4(sc, TWSI_STATUS);
        if (status != expect) {
                /*
@@ -408,7 +413,9 @@
                        aprint_error_dev(sc->sc_dev,
                            "unexpected status 0x%x: expect 0x%x\n", status,
                            expect);
-               return EIO;
+               error = EIO;
        }
+end:
+       mutex_exit(&sc->sc_mtx);
        return error;
 }



Home | Main Index | Thread Index | Old Index