Source-Changes-HG archive

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

[src/trunk]: src/sys/dev/i2c simplify the code and remove unused (it is in CV...



details:   https://anonhg.NetBSD.org/src/rev/42b3b24e0ed1
branches:  trunk
changeset: 1025562:42b3b24e0ed1
user:      christos <christos%NetBSD.org@localhost>
date:      Fri Nov 12 22:16:27 2021 +0000

description:
simplify the code and remove unused (it is in CVS anyway). Try to merge
duplicate code. Follow KNF. Brad, please test!

diffstat:

 sys/dev/i2c/sht3x.c |  1185 ++++++++++++++++----------------------------------
 1 files changed, 375 insertions(+), 810 deletions(-)

diffs (truncated from 1558 to 300 lines):

diff -r a98c19151427 -r 42b3b24e0ed1 sys/dev/i2c/sht3x.c
--- a/sys/dev/i2c/sht3x.c       Fri Nov 12 22:02:49 2021 +0000
+++ b/sys/dev/i2c/sht3x.c       Fri Nov 12 22:16:27 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sht3x.c,v 1.1 2021/11/06 13:34:40 brad Exp $   */
+/*     $NetBSD: sht3x.c,v 1.2 2021/11/12 22:16:27 christos Exp $       */
 
 /*
  * Copyright (c) 2021 Brad Spencer <brad%anduin.eldar.org@localhost>
@@ -17,7 +17,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sht3x.c,v 1.1 2021/11/06 13:34:40 brad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sht3x.c,v 1.2 2021/11/12 22:16:27 christos Exp $");
 
 /*
   Driver for the Sensirion SHT30/SHT31/SHT35
@@ -52,17 +52,6 @@
 static void    sht3x_attach(device_t, device_t, void *);
 static int     sht3x_detach(device_t, int);
 static void    sht3x_refresh(struct sysmon_envsys *, envsys_data_t *);
-/* The chip that I had would not allow the limits to actually be set
- * for reasons which are not obvious.  The chip took the command just
- * fine, but a read back of the limit registers showed that no change
- * was made, so disable limits for now.
- */
-#ifdef __did_not_work
-static void    sht3x_get_limits(struct sysmon_envsys *, envsys_data_t *,
-                                sysmon_envsys_lim_t *, uint32_t *);
-static void    sht3x_set_limits(struct sysmon_envsys *, envsys_data_t *,
-                                sysmon_envsys_lim_t *, uint32_t *);
-#endif
 static int     sht3x_verify_sysctl(SYSCTLFN_ARGS);
 static int     sht3x_verify_sysctl_heateron(SYSCTLFN_ARGS);
 static int     sht3x_verify_sysctl_modes(SYSCTLFN_ARGS);
@@ -338,8 +327,8 @@
        if (! have_bus) {
                error = iic_acquire_bus(sc->sc_tag, 0);
                if (error) {
-                       DPRINTF(sc, 2, ("%s: Could not acquire iic bus for breaking %d\n",
-                           device_xname(sc->sc_dev), error));
+                       DPRINTF(sc, 2, ("%s: Could not acquire iic bus for "
+                           "breaking %d\n", device_xname(sc->sc_dev), error));
                        goto out;
                }
        }
@@ -348,13 +337,13 @@
                DPRINTF(sc, 2, ("%s: Error breaking: %d\n",
                    device_xname(sc->sc_dev), error));
        }
- out:
+out:
        if (! have_bus) {
                iic_release_bus(sc->sc_tag, 0);
        }
 
        sc->sc_isperiodic = false;
-       strlcpy(sc->sc_mode,"single-shot",SHT3X_MODE_NAME);
+       strlcpy(sc->sc_mode, "single-shot", SHT3X_MODE_NAME);
 
        return error;
 }
@@ -362,38 +351,35 @@
 static int
 sht3x_get_status_register(void *aux, uint16_t *reg, bool have_bus)
 {
-       struct sht3x_sc *sc;
-       sc = aux;
+       struct sht3x_sc *sc = aux;
        uint8_t buf[3];
-       int error = 0;
+       int error;
 
        if (! have_bus) {
                error = iic_acquire_bus(sc->sc_tag, 0);
                if (error) {
-                       DPRINTF(sc, 2, ("%s: Could not acquire iic bus for getting status %d\n",
-                           device_xname(sc->sc_dev), error));
-                       goto out;
+                       DPRINTF(sc, 2, ("%s: Could not acquire iic bus for "
+                           "getting status %d\n", device_xname(sc->sc_dev),
+                           error));
+                       return error;
                }
        }
        error = sht3x_cmdr(sc, SHT3X_GET_STATUS_REGISTER, buf, 3);
        if (error) {
                DPRINTF(sc, 2, ("%s: Error getting status: %d\n",
                    device_xname(sc->sc_dev), error));
-       }
- out:
-       if (! have_bus) {
-               iic_release_bus(sc->sc_tag, 0);
+               goto out;
        }
 
-       if (!error) {
-               uint8_t c;
-
-               c = sht3x_crc(&buf[0],2);
-               if (c == buf[2]) {
-                       *reg = buf[0] << 8 | buf[1];
-               } else {
-                       error = EINVAL;
-               }
+       uint8_t c = sht3x_crc(&buf[0], 2);
+       if (c == buf[2]) {
+               *reg = buf[0] << 8 | buf[1];
+       } else {
+               error = EINVAL;
+       }
+out:
+       if (! have_bus) {
+               iic_release_bus(sc->sc_tag, 0);
        }
 
        return error;
@@ -402,16 +388,16 @@
 static int
 sht3x_clear_status_register(void *aux, bool have_bus)
 {
-       struct sht3x_sc *sc;
-       sc = aux;
-       int error = 0;
+       struct sht3x_sc *sc = aux;
+       int error;
 
        if (! have_bus) {
                error = iic_acquire_bus(sc->sc_tag, 0);
                if (error) {
-                       DPRINTF(sc, 2, ("%s: Could not acquire iic bus for clearing status %d\n",
-                           device_xname(sc->sc_dev), error));
-                       goto out;
+                       DPRINTF(sc, 2, ("%s: Could not acquire iic bus for "
+                           "clearing status %d\n", device_xname(sc->sc_dev),
+                           error));
+                       return error;
                }
        }
        error = sht3x_cmdr(sc, SHT3X_CLEAR_STATUS_REGISTER, NULL, 0);
@@ -419,7 +405,6 @@
                DPRINTF(sc, 2, ("%s: Error clear status register: %d\n",
                    device_xname(sc->sc_dev), error));
        }
- out:
        if (! have_bus) {
                iic_release_bus(sc->sc_tag, 0);
        }
@@ -438,16 +423,18 @@
 
        while (!sc->sc_stopping && !sc->sc_dying) {
                if (sc->sc_initperiodic) {
-                       error = sht3x_init_periodic_measurement(sc,&sdelay);
+                       error = sht3x_init_periodic_measurement(sc, &sdelay);
                        if (error) {
-                               DPRINTF(sc, 2, ("%s: Error initing periodic measurement "
-                                   "in thread: %d\n", device_xname(sc->sc_dev), error));
+                               DPRINTF(sc, 2, ("%s: Error initing periodic "
+                                   "measurement in thread: %d\n",
+                                   device_xname(sc->sc_dev), error));
                        }
                        sc->sc_initperiodic = false;
                }
                rv = cv_timedwait(&sc->sc_condvar, &sc->sc_threadmutex,
                    mstohz(sdelay));
-               if (rv == EWOULDBLOCK && !sc->sc_stopping && !sc->sc_initperiodic && !sc->sc_dying) {
+               if (rv == EWOULDBLOCK && !sc->sc_stopping &&
+                   !sc->sc_initperiodic && !sc->sc_dying) {
                        sht3x_take_periodic_measurement(sc);
                }
        }
@@ -458,14 +445,17 @@
 int
 sht3x_init_periodic_measurement(void *aux, int *sdelay)
 {
-       struct sht3x_sc *sc;
-       sc = aux;
-       int i,error = 0;
-       uint16_t r = 0;
+       struct sht3x_sc *sc = aux;
+       size_t i;
+       int error;
+       uint16_t r;
 
        for (i = 0; i < __arraycount(sht3x_periodic_rate); i++) {
-               if (strncmp(sc->sc_repeatability,sht3x_periodic_rate[i].repeatability,SHT3X_REP_NAME) == 0 &&
-                   strncmp(sc->sc_periodic_rate, sht3x_periodic_rate[i].rate,SHT3X_RATE_NAME) == 0) {
+               if (strncmp(sc->sc_repeatability,
+                   sht3x_periodic_rate[i].repeatability, SHT3X_REP_NAME) == 0 &&
+                   strncmp(sc->sc_periodic_rate, sht3x_periodic_rate[i].rate,
+                   SHT3X_RATE_NAME) == 0)
+               {
                        r = sht3x_periodic_rate[i].cmd;
                        *sdelay = sht3x_periodic_rate[i].sdelay;
                        break;
@@ -473,123 +463,138 @@
        }
 
        if (i == __arraycount(sht3x_periodic_rate)) {
-               error = 1;
                *sdelay = 100;
+               return ENODEV;
        }
 
        DPRINTF(sc, 2, ("%s: Would init with: %x\n",
            device_xname(sc->sc_dev), r));
 
-       if (error == 0) {
-               mutex_enter(&sc->sc_mutex);
+       mutex_enter(&sc->sc_mutex);
 
-               error = iic_acquire_bus(sc->sc_tag, 0);
-               if (error) {
-                       DPRINTF(sc, 2, ("%s: Could not acquire iic bus for initing: "
-                           " %d\n", device_xname(sc->sc_dev), error));
-               } else {
-                       error = sht3x_take_break(sc,true);
-                       if (error) {
-                               DPRINTF(sc, 2, ("%s: Could not acquire iic bus for initing: "
-                                   " %d\n", device_xname(sc->sc_dev), error));
-                       }
-
-                       error = sht3x_cmdr(sc, r, NULL, 0);
-                       if (error) {
-                               DPRINTF(sc, 2, ("%s: Error sending periodic measurement command: %d\n",
-                                   device_xname(sc->sc_dev), error));
-                       }
-                       iic_release_bus(sc->sc_tag, 0);
-                       sc->sc_isperiodic = true;
-                       strlcpy(sc->sc_mode,"periodic",SHT3X_MODE_NAME);
-               }
-               mutex_exit(&sc->sc_mutex);
+       error = iic_acquire_bus(sc->sc_tag, 0);
+       if (error) {
+               DPRINTF(sc, 2, ("%s: Could not acquire iic bus for initing: "
+                   " %d\n", device_xname(sc->sc_dev), error));
+               goto out;
        }
 
+       error = sht3x_take_break(sc, true);
+       if (error) {
+           DPRINTF(sc, 2, ("%s: Could not acquire iic bus for initing: "
+               " %d\n", device_xname(sc->sc_dev), error));
+           goto out;
+       }
+
+       error = sht3x_cmdr(sc, r, NULL, 0);
+       if (error) {
+               DPRINTF(sc, 2,
+                   ("%s: Error sending periodic measurement command: %d\n",
+                   device_xname(sc->sc_dev), error));
+               goto out;
+       }
+
+       sc->sc_isperiodic = true;
+       strlcpy(sc->sc_mode, "periodic", SHT3X_MODE_NAME);
+
+out:
+       iic_release_bus(sc->sc_tag, 0);
+       mutex_exit(&sc->sc_mutex);
        return error;
 }
 
 static void
 sht3x_take_periodic_measurement(void *aux)
 {
-       struct sht3x_sc *sc;
-       sc = aux;
-       int error = 0, data_error = 0;
-       uint8_t rawbuf[6];
+       struct sht3x_sc *sc = aux;
+       int error;
        struct sht3x_read_q *pp;
+       uint8_t rawbuf[MAX(sizeof(sc->sc_pbuffer), sizeof(pp->measurement))];
+       uint16_t status_reg;
 
        mutex_enter(&sc->sc_mutex);
        error = iic_acquire_bus(sc->sc_tag, 0);
        if (error) {
                DPRINTF(sc, 2, ("%s: Could not acquire iic bus for getting "
                    "periodic data: %d\n", device_xname(sc->sc_dev), error));
-       } else {
-               uint16_t status_reg;
+               goto out;
+       }
+
+       error = sht3x_get_status_register(sc, &status_reg, true);
+       if (error) {
+               DPRINTF(sc, 2,
+                   ("%s: Error getting status register periodic: %d\n",
+                   device_xname(sc->sc_dev), error));
+               goto err;
+       }
 
-               error = sht3x_get_status_register(sc, &status_reg, true);
-               if (error) {
-                       DPRINTF(sc, 2, ("%s: Error getting status register periodic: %d\n",
-                           device_xname(sc->sc_dev), error));
-               } else {



Home | Main Index | Thread Index | Old Index