public inbox for linux-i2c@vger.kernel.org
 help / color / mirror / Atom feed
From: Benjamin Tissoires <benjamin.tissoires-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
To: Jiri Kosina <jkosina-AlSwsSmVLrQ@public.gmane.org>,
	Jean Delvare <khali-PUYAD+kWke1g9hUCZPvPmw@public.gmane.org>,
	linux-input-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	linux-i2c-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Cc: Benjamin Tissoires
	<benjamin.tissoires-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
Subject: [PATCH] HID: i2c-hid: add mutex protecting open/close race
Date: Wed, 12 Dec 2012 17:12:16 +0100	[thread overview]
Message-ID: <1355328736-31418-1-git-send-email-benjamin.tissoires@gmail.com> (raw)

We should not enter close function while someone else is in open.
This mutex prevents this race.

There is also no need to override the ret value with -EIO in case of
a failure of i2c_hid_set_power.

Signed-off-by: Benjamin Tissoires <benjamin.tissoires-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
---

Hi Jiri, hi Jean,

this is the last patch that came from the last review. Sorry I took so long to
do it, but I thought it would have raised other problems. In the end, the
changes are pretty straightforward, and it would be great to have this one
in 3.8-rc0 too.
Anyway, Jiri, the choice is yours.

Cheers,
Benjamin

 drivers/hid/i2c-hid/i2c-hid.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c
index 6e1774c..74dfc33 100644
--- a/drivers/hid/i2c-hid/i2c-hid.c
+++ b/drivers/hid/i2c-hid/i2c-hid.c
@@ -117,6 +117,8 @@ static const struct i2c_hid_cmd hid_set_power_cmd =	{ I2C_HID_CMD(0x08) };
  * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) };
  */
 
+static DEFINE_MUTEX(i2c_hid_open_mut);
+
 /* The main device structure */
 struct i2c_hid {
 	struct i2c_client	*client;	/* i2c client */
@@ -641,17 +643,20 @@ static int i2c_hid_open(struct hid_device *hid)
 {
 	struct i2c_client *client = hid->driver_data;
 	struct i2c_hid *ihid = i2c_get_clientdata(client);
-	int ret;
+	int ret = 0;
 
+	mutex_lock(&i2c_hid_open_mut);
 	if (!hid->open++) {
 		ret = i2c_hid_set_power(client, I2C_HID_PWR_ON);
 		if (ret) {
 			hid->open--;
-			return -EIO;
+			goto done;
 		}
 		set_bit(I2C_HID_STARTED, &ihid->flags);
 	}
-	return 0;
+done:
+	mutex_unlock(&i2c_hid_open_mut);
+	return ret;
 }
 
 static void i2c_hid_close(struct hid_device *hid)
@@ -663,12 +668,14 @@ static void i2c_hid_close(struct hid_device *hid)
 	 * data acquistion due to a resumption we no longer
 	 * care about
 	 */
+	mutex_lock(&i2c_hid_open_mut);
 	if (!--hid->open) {
 		clear_bit(I2C_HID_STARTED, &ihid->flags);
 
 		/* Save some power */
 		i2c_hid_set_power(client, I2C_HID_PWR_SLEEP);
 	}
+	mutex_unlock(&i2c_hid_open_mut);
 }
 
 static int i2c_hid_power(struct hid_device *hid, int lvl)
-- 
1.8.0.2

             reply	other threads:[~2012-12-12 16:12 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-12 16:12 Benjamin Tissoires [this message]
2012-12-12 16:34 ` [PATCH] HID: i2c-hid: add mutex protecting open/close race Jean Delvare
     [not found]   ` <20121212173444.471d5cc6-R0o5gVi9kd7kN2dkZ6Wm7A@public.gmane.org>
2012-12-12 16:54     ` Benjamin Tissoires

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=1355328736-31418-1-git-send-email-benjamin.tissoires@gmail.com \
    --to=benjamin.tissoires-re5jqeeqqe8avxtiumwx3w@public.gmane.org \
    --cc=jkosina-AlSwsSmVLrQ@public.gmane.org \
    --cc=khali-PUYAD+kWke1g9hUCZPvPmw@public.gmane.org \
    --cc=linux-i2c-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=linux-input-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@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