Source-Changes-HG archive

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

[src/trunk]: src/sys/dev/i2c - fix clang compilation: add "%s" to format string



details:   https://anonhg.NetBSD.org/src/rev/1eebd6776039
branches:  trunk
changeset: 1024366:1eebd6776039
user:      christos <christos%NetBSD.org@localhost>
date:      Wed Oct 20 17:52:44 2021 +0000

description:
- fix clang compilation: add "%s" to format string
- comma is followed by space
- KNF multi-line comments
- fold long lines
- early returns, fixes a missed iic_release_bus() on error.
- foo == false -> !foo

diffstat:

 sys/dev/i2c/sgp40.c |  265 ++++++++++++++++++++++++++++-----------------------
 1 files changed, 147 insertions(+), 118 deletions(-)

diffs (truncated from 431 to 300 lines):

diff -r a814439bc999 -r 1eebd6776039 sys/dev/i2c/sgp40.c
--- a/sys/dev/i2c/sgp40.c       Wed Oct 20 17:30:28 2021 +0000
+++ b/sys/dev/i2c/sgp40.c       Wed Oct 20 17:52:44 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sgp40.c,v 1.1 2021/10/14 13:54:46 brad Exp $   */
+/*     $NetBSD: sgp40.c,v 1.2 2021/10/20 17:52:44 christos Exp $       */
 
 /*
  * Copyright (c) 2021 Brad Spencer <brad%anduin.eldar.org@localhost>
@@ -17,7 +17,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sgp40.c,v 1.1 2021/10/14 13:54:46 brad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sgp40.c,v 1.2 2021/10/20 17:52:44 christos Exp $");
 
 /*
   Driver for the Sensirion SGP40 MOx gas sensor for air quality
@@ -42,7 +42,8 @@
 #include <dev/i2c/sensirion_voc_algorithm.h>
 
 static uint8_t         sgp40_crc(uint8_t *, size_t);
-static int      sgp40_cmdr(struct sgp40_sc *, uint16_t, uint8_t *, uint8_t, uint8_t *, size_t);
+static int      sgp40_cmdr(struct sgp40_sc *, uint16_t, uint8_t *, uint8_t,
+    uint8_t *, size_t);
 static int     sgp40_poke(i2c_tag_t, i2c_addr_t, bool);
 static int     sgp40_match(device_t, cfdata_t, void *);
 static void    sgp40_attach(device_t, device_t, void *);
@@ -110,9 +111,10 @@
 
        VocAlgorithm_init(&voc_algorithm_params);
 
-       while (sc->sc_stopping == false) {
-               rv = cv_timedwait(&sc->sc_condvar, &sc->sc_threadmutex, mstohz(1000));
-               if (rv == EWOULDBLOCK && sc->sc_stopping == false) {
+       while (!sc->sc_stopping) {
+               rv = cv_timedwait(&sc->sc_condvar, &sc->sc_threadmutex,
+                   mstohz(1000));
+               if (rv == EWOULDBLOCK && !sc->sc_stopping) {
                        sgp40_take_measurement(sc,&voc_algorithm_params);
                }
        }
@@ -138,35 +140,37 @@
        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 heater off in stop thread: %d\n",
+               DPRINTF(sc, 2, ("%s: Could not acquire iic bus for heater off "
+                   "in stop thread: %d\n", device_xname(sc->sc_dev), error));
+               goto out;
+       }
+       error = sgp40_cmdr(sc, SGP40_HEATER_OFF, NULL, 0, NULL, 0);
+       if (error) {
+               DPRINTF(sc, 2, ("%s: Error turning heater off: %d\n",
                    device_xname(sc->sc_dev), error));
-       } else {
-               error = sgp40_cmdr(sc, SGP40_HEATER_OFF,NULL,0,NULL,0);
-               if (error) {
-                       DPRINTF(sc, 2, ("%s: Error turning heater off: %d\n",
-                           device_xname(sc->sc_dev), error));
-               }
-               iic_release_bus(sc->sc_tag, 0);
        }
+out:
+       iic_release_bus(sc->sc_tag, 0);
        mutex_exit(&sc->sc_mutex);
 }
 
 static int
 sgp40_compute_temp_comp(int unconverted)
 {
-       /* The published algorithm for this conversion is:
-          (temp_in_celcius + 45) * 65535 / 175
-
-          However, this did not exactly yield the results that
-          the example in the data sheet, so something a little
-          different was done.
-
-          (temp_in_celcius + 45) * 65536 / 175
-
-          This was also scaled up by 10^2 and then scaled back to
-          preserve some percision.  37449 is simply (65536 * 100) / 175
-          and rounded.
-       */
+       /*
+        * The published algorithm for this conversion is:
+        * (temp_in_celcius + 45) * 65535 / 175
+        *
+        * However, this did not exactly yield the results that
+        * the example in the data sheet, so something a little
+        * different was done.
+        *
+        * (temp_in_celcius + 45) * 65536 / 175
+        *
+        * This was also scaled up by 10^2 and then scaled back to
+        * preserve some percision.  37449 is simply (65536 * 100) / 175
+        * and rounded.
+        */
 
        return (((unconverted + 45) * 100) * 37449) / 10000;
 }
@@ -176,19 +180,20 @@
 {
        int q;
 
-       /* The published algorithm for this conversion is:
-          %rh * 65535 / 100
-
-          However, this did not exactly yield the results that
-          the example in the data sheet, so something a little
-          different was done.
-
-          %rh * 65536 / 100
-
-          This was also scaled up by 10^2 and then scaled back to
-          preserve some percision.  The value is also latched to 65535
-          as an upper limit.
-       */
+       /*
+        * The published algorithm for this conversion is:
+        * %rh * 65535 / 100
+        *
+        * However, this did not exactly yield the results that
+        * the example in the data sheet, so something a little
+        * different was done.
+        *
+        * %rh * 65536 / 100
+        *
+        * This was also scaled up by 10^2 and then scaled back to
+        * preserve some percision.  The value is also latched to 65535
+        * as an upper limit.
+        */
 
        q = ((unconverted * 100) * 65536) / 10000;
        if (q > 65535)
@@ -218,49 +223,55 @@
 
        args[0] = convertedrh >> 8;
        args[1] = convertedrh & 0x00ff;
-       args[2] = sgp40_crc(&args[0],2);
+       args[2] = sgp40_crc(&args[0], 2);
        args[3] = convertedtemp >> 8;
        args[4] = convertedtemp & 0x00ff;
-       args[5] = sgp40_crc(&args[3],2);
+       args[5] = sgp40_crc(&args[3], 2);
 
-       /* The VOC algoritm has a black out time when it first starts to run
-          and does not return any indicator that is going on, so voc_index
-          in that case would be 0..  however, that is also a valid response
-          otherwise, although an unlikely one.
-       */
+       /*
+        * The VOC algoritm has a black out time when it first starts to run
+        * and does not return any indicator that is going on, so voc_index
+        * in that case would be 0..  however, that is also a valid response
+        * otherwise, although an unlikely one.
+        */
        error = iic_acquire_bus(sc->sc_tag, 0);
        if (error) {
-               DPRINTF(sc, 2, ("%s: Could not acquire iic bus for take measurement: %d\n",
-                   device_xname(sc->sc_dev), error));
+               DPRINTF(sc, 2, ("%s: Could not acquire iic bus for take "
+                   "measurement: %d\n", device_xname(sc->sc_dev), error));
                sc->sc_voc = 0;
                sc->sc_vocvalid = false;
-       } else {
-               error = sgp40_cmdr(sc, SGP40_MEASURE_RAW, args, 6, buf, 3);
-               iic_release_bus(sc->sc_tag, 0);
-               if (error == 0) {
-                       crc = sgp40_crc(&buf[0],2);
-                       DPRINTF(sc, 2, ("%s: Raw ticks and crc: %02x%02x %02x %02x\n",
-                           device_xname(sc->sc_dev), buf[0], buf[1], buf[2],crc));
-                       if (buf[2] == crc) {
-                               rawmeasurement = buf[0] << 8;
-                               rawmeasurement |= buf[1];
-                               VocAlgorithm_process(params, rawmeasurement, &voc_index);
-                               DPRINTF(sc, 2, ("%s: VOC index: %d\n",
-                                   device_xname(sc->sc_dev), voc_index));
-                               sc->sc_voc = voc_index;
-                               sc->sc_vocvalid = true;
-                       } else {
-                               sc->sc_voc = 0;
-                               sc->sc_vocvalid = false;
-                       }
-               } else {
-                       DPRINTF(sc, 2, ("%s: Failed to get measurement %d\n",
-                           device_xname(sc->sc_dev), error));
-                       sc->sc_voc = 0;
-                       sc->sc_vocvalid = false;
-               }
+               goto out;
+       }
+
+       error = sgp40_cmdr(sc, SGP40_MEASURE_RAW, args, 6, buf, 3);
+       iic_release_bus(sc->sc_tag, 0);
+       if (error) {
+               DPRINTF(sc, 2, ("%s: Failed to get measurement %d\n",
+                   device_xname(sc->sc_dev), error));
+               goto out;
        }
 
+       crc = sgp40_crc(&buf[0], 2);
+       DPRINTF(sc, 2, ("%s: Raw ticks and crc: %02x%02x %02x "
+           "%02x\n", device_xname(sc->sc_dev), buf[0], buf[1],
+           buf[2], crc));
+       if (buf[2] != crc)
+               goto out;
+
+       rawmeasurement = buf[0] << 8;
+       rawmeasurement |= buf[1];
+       VocAlgorithm_process(params, rawmeasurement,
+           &voc_index);
+       DPRINTF(sc, 2, ("%s: VOC index: %d\n",
+           device_xname(sc->sc_dev), voc_index));
+       sc->sc_voc = voc_index;
+       sc->sc_vocvalid = true;
+
+       mutex_exit(&sc->sc_mutex);
+       return;
+out:
+       sc->sc_voc = 0;
+       sc->sc_vocvalid = false;
        mutex_exit(&sc->sc_mutex);
 }
 
@@ -340,7 +351,8 @@
        }
 
        if (r == -1) {
-               panic("Bad command look up in cmd delay: cmd: %d\n",cmd);
+               panic("sgp40: Bad command look up in cmd delay: cmd: %d\n",
+                   cmd);
        }
 
        return r;
@@ -357,37 +369,47 @@
        cmd16 = cmd[0] << 8;
        cmd16 = cmd16 | cmd[1];
 
-       error = iic_exec(tag,I2C_OP_WRITE_WITH_STOP,addr,cmd,clen,NULL,0,0);
+       error = iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, cmd, clen, NULL, 0,
+           0);
+       if (error)
+               return error;
 
-       /* Every command returns something except for turning the heater off
-          and the general soft reset which returns nothing.  */
-       if (error == 0 && cmd16 != SGP40_HEATER_OFF) {
-               /* Every command has a particular delay for how long
-                  it typically takes and the max time it will take. */
-               cmddelay = sgp40_cmddelay(cmd16);
-               delay(cmddelay);
+       /* 
+        * Every command returns something except for turning the heater off
+        * and the general soft reset which returns nothing.
+        */
+       if (cmd16 == SGP40_HEATER_OFF)
+               return 0;
+       /*
+        * Every command has a particular delay for how long
+        * it typically takes and the max time it will take.
+        */
+       cmddelay = sgp40_cmddelay(cmd16);
+       delay(cmddelay);
 
-               for (int aint = 0; aint < readattempts; aint++) {
-                       error = iic_exec(tag,I2C_OP_READ_WITH_STOP,addr,NULL,0,buf,blen,0);
-                       if (error == 0)
-                               break;
-                       delay(1000);
-               }
+       for (int aint = 0; aint < readattempts; aint++) {
+               error = iic_exec(tag, I2C_OP_READ_WITH_STOP, addr, NULL, 0,
+                   buf, blen, 0);
+               if (error == 0)
+                       break;
+               delay(1000);
        }
 
        return error;
 }
 
 static int
-sgp40_cmdr(struct sgp40_sc *sc, uint16_t cmd, uint8_t *extraargs, uint8_t argslen, uint8_t *buf, size_t blen)
+sgp40_cmdr(struct sgp40_sc *sc, uint16_t cmd, uint8_t *extraargs,
+    uint8_t argslen, uint8_t *buf, size_t blen)
 {
        uint8_t fullcmd[8];
        uint8_t cmdlen;
        int n;
 
-       /* The biggest documented command + arguments is 8 uint8_t bytes long. */
-       /* Catch anything that ties to have an arglen more than 6 */
-
+       /*
+        * The biggest documented command + arguments is 8 uint8_t bytes long.
+        * Catch anything that ties to have an arglen more than 6
+        */
        KASSERT(argslen <= 6);
 



Home | Main Index | Thread Index | Old Index