Source-Changes-HG archive

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

[src/trunk]: src/sys/dev/i2c - No need to use I2C_F_POLL here.



details:   https://anonhg.NetBSD.org/src/rev/103165227ba6
branches:  trunk
changeset: 466813:103165227ba6
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Thu Jan 02 17:40:27 2020 +0000

description:
- No need to use I2C_F_POLL here.
- Correctly propagate errors up the stack.

diffstat:

 sys/dev/i2c/x1226.c |  90 ++++++++++++++++++++++++----------------------------
 1 files changed, 42 insertions(+), 48 deletions(-)

diffs (214 lines):

diff -r 4e7dfbf9ae32 -r 103165227ba6 sys/dev/i2c/x1226.c
--- a/sys/dev/i2c/x1226.c       Thu Jan 02 17:17:36 2020 +0000
+++ b/sys/dev/i2c/x1226.c       Thu Jan 02 17:40:27 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: x1226.c,v 1.21 2018/06/16 21:22:13 thorpej Exp $       */
+/*     $NetBSD: x1226.c,v 1.22 2020/01/02 17:40:27 thorpej Exp $       */
 
 /*
  * Copyright (c) 2003 Shigeyuki Fukushima.
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: x1226.c,v 1.21 2018/06/16 21:22:13 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x1226.c,v 1.22 2020/01/02 17:40:27 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -252,14 +252,17 @@
        struct xrtc_softc *sc = ch->cookie;
        struct clock_ymdhms dt, check;
        int retries;
+       int error;
 
        memset(&dt, 0, sizeof(dt));
        memset(&check, 0, sizeof(check));
 
        retries = 5;
        do {
-               xrtc_clock_read(sc, &dt);
-               xrtc_clock_read(sc, &check);
+               if ((error = xrtc_clock_read(sc, &dt)) == 0)
+                       error = xrtc_clock_read(sc, &check);
+               if (error)
+                       return error;
        } while (memcmp(&dt, &check, sizeof(check)) != 0 && --retries);
 
        tv->tv_sec = clock_ymdhms_to_secs(&dt);
@@ -276,10 +279,7 @@
 
        clock_secs_to_ymdhms(tv->tv_sec, &dt);
 
-       if (xrtc_clock_write(sc, &dt) == 0)
-               return (-1);
-
-       return (0);
+       return xrtc_clock_write(sc, &dt);
 }
 
 static int
@@ -287,11 +287,12 @@
 {
        int i = 0;
        u_int8_t bcd[X1226_REG_RTC_SIZE], cmdbuf[2];
+       int error;
 
-       if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
+       if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
                aprint_error_dev(sc->sc_dev,
                    "xrtc_clock_read: failed to acquire I2C bus\n");
-               return (0);
+               return (error);
        }
 
        /* Read each RTC register in order */
@@ -300,20 +301,20 @@
                cmdbuf[0] = (addr >> 8) & 0xff;
                cmdbuf[1] = addr & 0xff;
 
-               if (iic_exec(sc->sc_tag,
+               if ((error = iic_exec(sc->sc_tag,
                        I2C_OP_READ_WITH_STOP,
                        sc->sc_address, cmdbuf, 2,
-                       &bcd[i], 1, I2C_F_POLL)) {
-                       iic_release_bus(sc->sc_tag, I2C_F_POLL);
+                       &bcd[i], 1, 0)) != 0) {
+                       iic_release_bus(sc->sc_tag, 0);
                        aprint_error_dev(sc->sc_dev,
                            "xrtc_clock_read: failed to read rtc "
                                "at 0x%x\n", i);
-                       return (0);
+                       return (error);
                }
        }
 
        /* Done with I2C */
-       iic_release_bus(sc->sc_tag, I2C_F_POLL);
+       iic_release_bus(sc->sc_tag, 0);
 
        /*
         * Convert the X1226's register bcd values
@@ -343,7 +344,7 @@
        dt->dt_year += bcdtobin(bcd[X1226_REG_Y2K - X1226_REG_RTC_BASE]
                        & X1226_REG_Y2K_MASK) * 100;
 
-       return (1);
+       return (0);
 }
 
 static int
@@ -351,6 +352,7 @@
 {
        int i = 0, addr;
        u_int8_t bcd[X1226_REG_RTC_SIZE], cmdbuf[3];
+       int error, error2;
 
        /*
         * Convert our time to bcd values
@@ -365,10 +367,10 @@
        bcd[X1226_REG_YR - X1226_REG_RTC_BASE] = bintobcd(dt->dt_year % 100);
        bcd[X1226_REG_Y2K - X1226_REG_RTC_BASE] = bintobcd(dt->dt_year / 100);
 
-       if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
+       if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
                aprint_error_dev(sc->sc_dev,
                    "xrtc_clock_write: failed to acquire I2C bus\n");
-               return (0);
+               return (error);
        }
 
        /* Unlock register: Write Enable Latch */
@@ -376,13 +378,13 @@
        cmdbuf[0] = ((addr >> 8) & 0xff);
        cmdbuf[1] = (addr & 0xff);
        cmdbuf[2] = X1226_FLAG_SR_WEL;
-       if (iic_exec(sc->sc_tag,
+       if ((error = iic_exec(sc->sc_tag,
                I2C_OP_WRITE_WITH_STOP,
-               sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0) != 0) {
-               iic_release_bus(sc->sc_tag, I2C_F_POLL);
+               sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0)) != 0) {
+               iic_release_bus(sc->sc_tag, 0);
                aprint_error_dev(sc->sc_dev, "xrtc_clock_write: "
                        "failed to write-unlock status register(WEL=1)\n");
-               return (0);
+               return (error);
        }
 
        /* Unlock register: Register Write Enable Latch */
@@ -390,13 +392,13 @@
        cmdbuf[0] = ((addr >> 8) & 0xff);
        cmdbuf[1] = (addr & 0xff);
        cmdbuf[2] = X1226_FLAG_SR_WEL | X1226_FLAG_SR_RWEL;
-       if (iic_exec(sc->sc_tag,
+       if ((error = iic_exec(sc->sc_tag,
                I2C_OP_WRITE_WITH_STOP,
-               sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0) != 0) {
-               iic_release_bus(sc->sc_tag, I2C_F_POLL);
+               sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0)) != 0) {
+               iic_release_bus(sc->sc_tag, 0);
                aprint_error_dev(sc->sc_dev, "xrtc_clock_write: "
                        "failed to write-unlock status register(RWEL=1)\n");
-               return (0);
+               return (error);
        }
 
        /* Write each RTC register in reverse order */
@@ -404,42 +406,34 @@
                addr = i + X1226_REG_RTC_BASE;
                cmdbuf[0] = ((addr >> 8) & 0xff);
                cmdbuf[1] = (addr & 0xff);
-               if (iic_exec(sc->sc_tag,
+               if ((error = iic_exec(sc->sc_tag,
                        I2C_OP_WRITE_WITH_STOP,
                        sc->sc_address, cmdbuf, 2,
-                       &bcd[i], 1, I2C_F_POLL)) {
+                       &bcd[i], 1, 0)) != 0) {
 
-                       /* Lock register: WEL/RWEL off */
-                       addr = X1226_REG_SR;
-                       cmdbuf[0] = ((addr >> 8) & 0xff);
-                       cmdbuf[1] = (addr & 0xff);
-                       cmdbuf[2] = 0;
-                       iic_exec(sc->sc_tag,
-                               I2C_OP_WRITE_WITH_STOP,
-                               sc->sc_address, cmdbuf, 2,
-                               &cmdbuf[2], 1, 0);
+                       aprint_error_dev(sc->sc_dev,
+                           "xrtc_clock_write: failed to write rtc at 0x%x\n",
+                           i);
 
-                       iic_release_bus(sc->sc_tag, I2C_F_POLL);
-                       aprint_error_dev(sc->sc_dev, "xrtc_clock_write: failed to write rtc "
-                               "at 0x%x\n", i);
-                       return (0);
+                       goto write_lock_rtc;
                }
        }
 
+ write_lock_rtc:
        /* Lock register: WEL/RWEL off */
        addr = X1226_REG_SR;
        cmdbuf[0] = ((addr >> 8) & 0xff);
        cmdbuf[1] = (addr & 0xff);
        cmdbuf[2] = 0;
-       if (iic_exec(sc->sc_tag,
-               I2C_OP_WRITE_WITH_STOP,
-               sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0) != 0) {
-               iic_release_bus(sc->sc_tag, I2C_F_POLL);
+       if ((error2 = iic_exec(sc->sc_tag,
+                       I2C_OP_WRITE_WITH_STOP,
+                       sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0)) != 0) {
+               iic_release_bus(sc->sc_tag, 0);
                aprint_error_dev(sc->sc_dev, "xrtc_clock_write: "
                        "failed to write-lock status register\n");
-               return (0);
+               return (error ? error : error2);
        }
 
-       iic_release_bus(sc->sc_tag, I2C_F_POLL);
-       return (1);
+       iic_release_bus(sc->sc_tag, 0);
+       return (error);
 }



Home | Main Index | Thread Index | Old Index