linux-i2c.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Rodolfo Giometti <giometti-k2GhghHVRtY@public.gmane.org>
To: linux-i2c-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Cc: Jean Delvare <khali-PUYAD+kWke1g9hUCZPvPmw@public.gmane.org>,
	David Brownell <david-b-yBeKhBN/0LDR7s880joybQ@public.gmane.org>,
	Kumar Gala
	<galak-XVmvHMARGAS8U2dJNN8I7kB+6BGkLq7r@public.gmane.org>,
	Peter Korsgaard <jacmet-OfajU3CKLf1/SzgSGea1oA@public.gmane.org>,
	Rodolfo Giometti <giometti-k2GhghHVRtY@public.gmane.org>
Subject: [PATCH 4/6] i2c: free adapters (de)registration from i2c "core_lock" mutex.
Date: Thu,  5 Feb 2009 14:36:11 +0100	[thread overview]
Message-ID: <1233840973-13268-5-git-send-email-giometti@linux.it> (raw)
In-Reply-To: <1233840973-13268-4-git-send-email-giometti-k2GhghHVRtY@public.gmane.org>

The adapters (de)registration are now protectd by using a dedicated
mutex instead of using global "core_lock" one. This allow multiple
(and recursive) (de)registration of several adapters (or i2c
multiplexers acting as i2c virtual adapters).

Signed-off-by: Rodolfo Giometti <giometti-k2GhghHVRtY@public.gmane.org>
---
 drivers/i2c/i2c-core.c |   57 +++++++++++++++++++++++------------------------
 1 files changed, 28 insertions(+), 29 deletions(-)

diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index 5d3646f..4b088ef 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -40,6 +40,7 @@
 
 
 static DEFINE_MUTEX(core_lock);
+static DEFINE_MUTEX(idr_adapter_lock);
 static DEFINE_IDR(i2c_adapter_idr);
 
 #define is_newstyle_driver(d) ((d)->probe || (d)->remove || (d)->detect)
@@ -438,7 +439,7 @@ static int i2c_do_add_adapter(struct device_driver *d, void *data)
 
 static int i2c_register_adapter(struct i2c_adapter *adap)
 {
-	int res = 0, dummy;
+	int res, dummy;
 
 	/* Can't register until after driver model init */
 	if (unlikely(WARN_ON(!i2c_bus_type.p)))
@@ -448,8 +449,6 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
 	mutex_init(&adap->clist_lock);
 	INIT_LIST_HEAD(&adap->clients);
 
-	mutex_lock(&core_lock);
-
 	/* Add the adapter to the driver core.
 	 * If the parent pointer is not set up,
 	 * we add this adapter to the host bus.
@@ -463,8 +462,13 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
 	adap->dev.release = &i2c_adapter_dev_release;
 	adap->dev.class = &i2c_adapter_class;
 	res = device_register(&adap->dev);
-	if (res)
-		goto out_list;
+	if (res) {
+		mutex_lock(&idr_adapter_lock);
+		idr_remove(&i2c_adapter_idr, adap->nr);
+		mutex_unlock(&idr_adapter_lock);
+
+		return res;
+	}
 
 	dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
 
@@ -476,13 +480,7 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
 	dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
 				 i2c_do_add_adapter);
 
-out_unlock:
-	mutex_unlock(&core_lock);
-	return res;
-
-out_list:
-	idr_remove(&i2c_adapter_idr, adap->nr);
-	goto out_unlock;
+	return 0;
 }
 
 /**
@@ -506,11 +504,11 @@ retry:
 	if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
 		return -ENOMEM;
 
-	mutex_lock(&core_lock);
+	mutex_lock(&idr_adapter_lock);
 	/* "above" here means "above or equal to", sigh */
 	res = idr_get_new_above(&i2c_adapter_idr, adapter,
 				__i2c_first_dynamic_bus_num, &id);
-	mutex_unlock(&core_lock);
+	mutex_unlock(&idr_adapter_lock);
 
 	if (res < 0) {
 		if (res == -EAGAIN)
@@ -555,7 +553,7 @@ retry:
 	if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
 		return -ENOMEM;
 
-	mutex_lock(&core_lock);
+	mutex_lock(&idr_adapter_lock);
 	/* "above" here means "above or equal to", sigh;
 	 * we need the "equal to" result to force the result
 	 */
@@ -564,7 +562,7 @@ retry:
 		status = -EBUSY;
 		idr_remove(&i2c_adapter_idr, id);
 	}
-	mutex_unlock(&core_lock);
+	mutex_unlock(&idr_adapter_lock);
 	if (status == -EAGAIN)
 		goto retry;
 
@@ -612,19 +610,25 @@ static int i2c_do_del_adapter(struct device_driver *d, void *data)
  */
 int i2c_del_adapter(struct i2c_adapter *adap)
 {
+	struct i2c_adapter *ptr;
 	struct i2c_client *client, *_n;
-	int res = 0, dummy;
+	int dummy;
 
-	mutex_lock(&core_lock);
+	mutex_lock(&idr_adapter_lock);
 
 	/* First make sure that this adapter was ever added */
-	if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
+	ptr = idr_find(&i2c_adapter_idr, adap->nr);
+	if (ptr != adap) {
 		pr_debug("i2c-core: attempting to delete unregistered "
 			 "adapter [%s]\n", adap->name);
-		res = -EINVAL;
-		goto out_unlock;
+		mutex_unlock(&idr_adapter_lock);
+		return -EINVAL;
 	}
 
+	/* ... then remove adapter ID */
+	idr_remove(&i2c_adapter_idr, adap->nr);
+	mutex_unlock(&idr_adapter_lock);
+
 	/* Tell drivers about this removal */
 	dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
 			       i2c_do_del_adapter);
@@ -655,18 +659,13 @@ int i2c_del_adapter(struct i2c_adapter *adap)
 	/* wait for sysfs to drop all references */
 	wait_for_completion(&adap->dev_released);
 
-	/* free bus id */
-	idr_remove(&i2c_adapter_idr, adap->nr);
-
 	dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
 
 	/* Clear the device structure in case this adapter is ever going to be
 	   added again */
 	memset(&adap->dev, 0, sizeof(adap->dev));
 
- out_unlock:
-	mutex_unlock(&core_lock);
-	return res;
+	return 0;
 }
 EXPORT_SYMBOL(i2c_del_adapter);
 
@@ -1497,12 +1496,12 @@ struct i2c_adapter* i2c_get_adapter(int id)
 {
 	struct i2c_adapter *adapter;
 
-	mutex_lock(&core_lock);
+	mutex_lock(&idr_adapter_lock);
 	adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
 	if (adapter && !try_module_get(adapter->owner))
 		adapter = NULL;
 
-	mutex_unlock(&core_lock);
+	mutex_unlock(&idr_adapter_lock);
 	return adapter;
 }
 EXPORT_SYMBOL(i2c_get_adapter);
-- 
1.5.6.3

  parent reply	other threads:[~2009-02-05 13:36 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-02-05 13:36 i2c bus multiplexing (Version 2) Rodolfo Giometti
     [not found] ` <1233840973-13268-1-git-send-email-giometti-k2GhghHVRtY@public.gmane.org>
2009-02-05 13:36   ` [PATCH 1/6] i2c: put driver_unregister() out of core_lock protection Rodolfo Giometti
     [not found]     ` <1233840973-13268-2-git-send-email-giometti-k2GhghHVRtY@public.gmane.org>
2009-02-05 13:36       ` [PATCH 2/6] i2c: use rwsem instead of mutex Rodolfo Giometti
     [not found]         ` <1233840973-13268-3-git-send-email-giometti-k2GhghHVRtY@public.gmane.org>
2009-02-05 13:36           ` [PATCH 3/6] i2c: ignore active clients detaching during adapter removal Rodolfo Giometti
     [not found]             ` <1233840973-13268-4-git-send-email-giometti-k2GhghHVRtY@public.gmane.org>
2009-02-05 13:36               ` Rodolfo Giometti [this message]
     [not found]                 ` <1233840973-13268-5-git-send-email-giometti-k2GhghHVRtY@public.gmane.org>
2009-02-05 13:36                   ` [PATCH 5/6] i2c: Multiplexed I2C bus core support Rodolfo Giometti
     [not found]                     ` <1233840973-13268-6-git-send-email-giometti-k2GhghHVRtY@public.gmane.org>
2009-02-05 13:36                       ` [PATCH 6/6] i2c: driver for PCA954x I2C multiplexer series Rodolfo Giometti
2009-02-06  0:02               ` [PATCH 3/6] i2c: ignore active clients detaching during adapter removal David Brownell
     [not found]                 ` <200902051602.44036.david-b-yBeKhBN/0LDR7s880joybQ@public.gmane.org>
2009-02-06 10:22                   ` Rodolfo Giometti
     [not found]                     ` <20090206102220.GF28554-AVVDYK/kqiJWk0Htik3J/w@public.gmane.org>
2009-02-06 12:15                       ` Jean Delvare
     [not found]                         ` <20090206131520.232b53c3-ig7AzVSIIG7kN2dkZ6Wm7A@public.gmane.org>
2009-02-06 12:59                           ` Rodolfo Giometti
     [not found]                             ` <20090206125907.GA8581-AVVDYK/kqiJWk0Htik3J/w@public.gmane.org>
2009-02-06 21:15                               ` Jean Delvare
     [not found]                                 ` <20090206221519.5fc3d2b9-ig7AzVSIIG7kN2dkZ6Wm7A@public.gmane.org>
2009-02-09 13:06                                   ` Rodolfo Giometti
     [not found]                                     ` <20090209130642.GV7975-AVVDYK/kqiJWk0Htik3J/w@public.gmane.org>
2009-02-10 11:08                                       ` Jean Delvare
     [not found]                                         ` <20090210120839.23592e38-ig7AzVSIIG7kN2dkZ6Wm7A@public.gmane.org>
2009-02-18 16:48                                           ` Rodolfo Giometti
     [not found]                                             ` <20090218164812.GA11217-AVVDYK/kqiJWk0Htik3J/w@public.gmane.org>
2009-05-28  7:50                                               ` Jean Delvare
     [not found]                                                 ` <20090528095054.03fc2df3-ig7AzVSIIG7kN2dkZ6Wm7A@public.gmane.org>
2009-05-28  7:58                                                   ` Rodolfo Giometti
     [not found]                                                     ` <20090528075835.GH20243-AVVDYK/kqiJWk0Htik3J/w@public.gmane.org>
2009-05-28  8:22                                                       ` Jean Delvare
2009-05-29  7:28                                                   ` Rodolfo Giometti
     [not found]                                                     ` <20090529072835.GA4575-AVVDYK/kqiJWk0Htik3J/w@public.gmane.org>
2009-05-29  7:46                                                       ` Jean Delvare
2009-05-28 11:50           ` [PATCH 2/6] i2c: use rwsem instead of mutex Jean Delvare
2009-12-11 22:41 ` i2c bus multiplexing (Version 2) Muralidharan Karicheri
     [not found]   ` <loom.20091211T234046-786-eS7Uydv5nfjZ+VzJOa5vwg@public.gmane.org>
2010-04-16 11:18     ` Jean Delvare

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=1233840973-13268-5-git-send-email-giometti@linux.it \
    --to=giometti-k2ghghhvrty@public.gmane.org \
    --cc=david-b-yBeKhBN/0LDR7s880joybQ@public.gmane.org \
    --cc=galak-XVmvHMARGAS8U2dJNN8I7kB+6BGkLq7r@public.gmane.org \
    --cc=jacmet-OfajU3CKLf1/SzgSGea1oA@public.gmane.org \
    --cc=khali-PUYAD+kWke1g9hUCZPvPmw@public.gmane.org \
    --cc=linux-i2c-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;
as well as URLs for NNTP newsgroup(s).