public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Greg KH <gregkh@suse.de>
To: linux-kernel@vger.kernel.org, sensors@Stimpy.netroedge.com
Cc: khali@linux-fr.org
Subject: [PATCH] I2C: pcf8574 doesn't need a lock
Date: Fri, 1 Apr 2005 12:04:14 -0800	[thread overview]
Message-ID: <11123858541382@kroah.com> (raw)
In-Reply-To: <20050401200324.GA12854@kroah.com>

ChangeSet 1.2339, 2005/04/01 11:48:28-08:00, khali@linux-fr.org

[PATCH] I2C: pcf8574 doesn't need a lock

While investigating the i2c chips drivers that were not properly
locking, we found that the pcf8574 driver does the exact contrary. It
uses a lock where it's not needed.

While we were there, we did some additional cleanups to the driver:
1* Merge pcf8574_update_client() in show_read(), as it was the only user
and the function became trivial once the locking was removed.
2* Add a validity check on values provided by user-space.

Aurelien Jarno tested the modified code for confirmation and it worked
just fine.

Signed-off-by: Jean Delvare <khali@linux-fr.org>
Signed-off-by: Aurelien Jarno <aurelien@aurel32.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>


 drivers/i2c/chips/pcf8574.c |   27 +++++++++------------------
 1 files changed, 9 insertions(+), 18 deletions(-)


diff -Nru a/drivers/i2c/chips/pcf8574.c b/drivers/i2c/chips/pcf8574.c
--- a/drivers/i2c/chips/pcf8574.c	2005-04-01 11:53:38 -08:00
+++ b/drivers/i2c/chips/pcf8574.c	2005-04-01 11:53:38 -08:00
@@ -56,7 +56,6 @@
 /* Each client has this additional data */
 struct pcf8574_data {
 	struct i2c_client client;
-	struct semaphore update_lock;
 
 	u8 read, write;			/* Register values */
 };
@@ -65,7 +64,6 @@
 static int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind);
 static int pcf8574_detach_client(struct i2c_client *client);
 static void pcf8574_init_client(struct i2c_client *client);
-static struct pcf8574_data *pcf8574_update_client(struct device *dev);
 
 /* This is the driver that will be inserted */
 static struct i2c_driver pcf8574_driver = {
@@ -80,7 +78,9 @@
 /* following are the sysfs callback functions */
 static ssize_t show_read(struct device *dev, char *buf)
 {
-	struct pcf8574_data *data = pcf8574_update_client(dev);
+	struct i2c_client *client = to_i2c_client(dev);
+	struct pcf8574_data *data = i2c_get_clientdata(client);
+	data->read = i2c_smbus_read_byte(client); 
 	return sprintf(buf, "%u\n", data->read);
 }
 
@@ -97,7 +97,12 @@
 {
 	struct i2c_client *client = to_i2c_client(dev);
 	struct pcf8574_data *data = i2c_get_clientdata(client);
-	data->write = simple_strtoul(buf, NULL, 10);
+	unsigned long val = simple_strtoul(buf, NULL, 10);
+
+	if (val > 0xff)
+		return -EINVAL;
+
+	data->write = val;
 	i2c_smbus_write_byte(client, data->write);
 	return count;
 }
@@ -157,7 +162,6 @@
 
 	/* Fill in the remaining client fields and put it into the global list */
 	strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
-	init_MUTEX(&data->update_lock);
 
 	/* Tell the I2C layer a new client has arrived */
 	if ((err = i2c_attach_client(new_client)))
@@ -200,19 +204,6 @@
 	struct pcf8574_data *data = i2c_get_clientdata(client);
 	data->write = PCF8574_INIT;
 	i2c_smbus_write_byte(client, data->write);
-}
-
-static struct pcf8574_data *pcf8574_update_client(struct device *dev)
-{
-	struct i2c_client *client = to_i2c_client(dev);
-	struct pcf8574_data *data = i2c_get_clientdata(client);
-
-	down(&data->update_lock);
-	dev_dbg(&client->dev, "Starting pcf8574 update\n");
-	data->read = i2c_smbus_read_byte(client); 
-	up(&data->update_lock);
-	
-	return data;
 }
 
 static int __init pcf8574_init(void)


  reply	other threads:[~2005-04-01 20:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-01 20:03 [BK PATCH] More I2C patches for 2.6.12-rc1 Greg KH
2005-04-01 20:04 ` Greg KH [this message]
2005-04-01 20:04   ` [PATCH] I2C: Move functionality handling from i2c-core to i2c.h Greg KH
2005-04-01 20:04     ` [PATCH] I2C: Fix a common race condition in hardware monitoring Greg KH

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=11123858541382@kroah.com \
    --to=gregkh@suse.de \
    --cc=greg@kroah.com \
    --cc=khali@linux-fr.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sensors@Stimpy.netroedge.com \
    /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