public inbox for linux-i2c@vger.kernel.org
 help / color / mirror / Atom feed
From: Danielle Costantino <danielle.costantino-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
To: Wolfram Sang <wsa-z923LK4zBo2bacvFa/9K2g@public.gmane.org>,
	linux-i2c-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Subject: [PATCH] i2c: mux: pca9541: add return value check on register writes
Date: Sat, 29 Nov 2014 14:45:47 -0800	[thread overview]
Message-ID: <547A4C9B.5080705@gmail.com> (raw)

i2c: mux: pca9541: add return value check on register writes

Without this check there are conditions when the device was unable to
acquire mastership but returns that it succeeded.

also convert to use devm memory allocation functions

Signed-off-by: Danielle Costantino <danielle.costantino-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
---
diff --git a/drivers/i2c/muxes/i2c-mux-pca9541.c 
b/drivers/i2c/muxes/i2c-mux-pca9541.c
index cb77277..0a029e9 100644
--- a/drivers/i2c/muxes/i2c-mux-pca9541.c
+++ b/drivers/i2c/muxes/i2c-mux-pca9541.c
@@ -52,9 +52,9 @@
  #define PCA9541_CTL_TESTON    (1 << 6)
  #define PCA9541_CTL_NTESTON    (1 << 7)

-#define PCA9541_ISTAT_INTIN    (1 << 0)
+#define PCA9541_ISTAT_INTIN        (1 << 0)
  #define PCA9541_ISTAT_BUSINIT    (1 << 1)
-#define PCA9541_ISTAT_BUSOK    (1 << 2)
+#define PCA9541_ISTAT_BUSOK        (1 << 2)
  #define PCA9541_ISTAT_BUSLOST    (1 << 3)
  #define PCA9541_ISTAT_MYTEST    (1 << 6)
  #define PCA9541_ISTAT_NMYTEST    (1 << 7)
@@ -65,7 +65,7 @@
  #define busoff(x)    (!((x) & BUSON) || ((x) & BUSON) == BUSON)

  /* arbitration timeouts, in jiffies */
-#define ARB_TIMEOUT    (HZ / 8)    /* 125 ms until forcing bus ownership */
+#define ARB_TIMEOUT        (HZ / 8)    /* 125 ms until forcing bus 
ownership */
  #define ARB2_TIMEOUT    (HZ / 4)    /* 250 ms until acquisition failure */

  /* arbitration retry delays, in us */
@@ -219,6 +219,7 @@
  {
      struct pca9541 *data = i2c_get_clientdata(client);
      int reg;
+    int ret = 0;

      reg = pca9541_reg_read(client, PCA9541_CONTROL);
      if (reg < 0)
@@ -237,7 +238,7 @@
               * Other master did not request ownership,
               * or arbitration timeout expired. Take the bus.
               */
-            pca9541_reg_write(client,
+            ret = pca9541_reg_write(client,
                        PCA9541_CONTROL,
                        pca9541_control[reg & 0x0f]
                        | PCA9541_CTL_NTESTON);
@@ -255,11 +256,11 @@
           * Reset NTESTON and BUSINIT, then return success.
           */
          if (reg & (PCA9541_CTL_NTESTON | PCA9541_CTL_BUSINIT))
-            pca9541_reg_write(client,
+            ret = pca9541_reg_write(client,
                        PCA9541_CONTROL,
                        reg & ~(PCA9541_CTL_NTESTON
                            | PCA9541_CTL_BUSINIT));
-        return 1;
+        return ret < 0 ? ret : 1;
      } else {
          /*
           * Other master owns the bus.
@@ -269,7 +270,7 @@
          data->select_timeout = SELECT_DELAY_LONG;
          if (time_is_before_eq_jiffies(data->arb_timeout)) {
              /* Time is up, take the bus and reset it. */
-            pca9541_reg_write(client,
+            ret = pca9541_reg_write(client,
                        PCA9541_CONTROL,
                        pca9541_control[reg & 0x0f]
                        | PCA9541_CTL_BUSINIT
@@ -277,12 +278,12 @@
          } else {
              /* Request bus ownership if needed */
              if (!(reg & PCA9541_CTL_NTESTON))
-                pca9541_reg_write(client,
+                ret = pca9541_reg_write(client,
                            PCA9541_CONTROL,
                            reg | PCA9541_CTL_NTESTON);
          }
      }
-    return 0;
+    return ret < 0 ? ret : 0;
  }

  static int pca9541_select_chan(struct i2c_adapter *adap, void *client, 
u32 chan)
@@ -331,7 +332,7 @@
      if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE_DATA))
          goto err;

-    data = kzalloc(sizeof(struct pca9541), GFP_KERNEL);
+    data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
      if (!data) {
          ret = -ENOMEM;
          goto err;
@@ -368,7 +369,7 @@
      return 0;

  exit_free:
-    kfree(data);
+    devm_kfree(&client->dev, data);
  err:
      return ret;
  }
@@ -379,7 +380,6 @@

      i2c_del_mux_adapter(data->mux_adap);

-    kfree(data);
      return 0;
  }

             reply	other threads:[~2014-11-29 22:45 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-29 22:45 Danielle Costantino [this message]
     [not found] ` <547A4C9B.5080705-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2014-12-01 17:33   ` [PATCH] i2c: mux: pca9541: add return value check on register writes Wolfram Sang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=547A4C9B.5080705@gmail.com \
    --to=danielle.costantino-re5jqeeqqe8avxtiumwx3w@public.gmane.org \
    --cc=linux-i2c-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=wsa-z923LK4zBo2bacvFa/9K2g@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox