From: Johan Hovold <johan@kernel.org>
To: Andi Shyti <andi.shyti@kernel.org>
Cc: Wolfram Sang <wsa+renesas@sang-engineering.com>,
linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org,
Johan Hovold <johan@kernel.org>
Subject: [PATCH v2 3/3] i2c: dev: clean up registration
Date: Thu, 27 Aug 2026 12:20:43 +0200 [thread overview]
Message-ID: <20260827102043.673273-4-johan@kernel.org> (raw)
In-Reply-To: <20260827102043.673273-1-johan@kernel.org>
Drop the get_free_i2c_dev() and put_i2c_dev() helpers and do all setup
and teardown directly in i2cdev_attach_adapter() and
i2cdev_detach_adapter() for consistency and to make the logic clearer.
Note that the device list is only used at detach so the i2c-dev can be
added after registering the class device.
Signed-off-by: Johan Hovold <johan@kernel.org>
---
drivers/i2c/i2c-dev.c | 69 ++++++++++++++++++-------------------------
1 file changed, 29 insertions(+), 40 deletions(-)
diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c
index 49c05f2eb223..9ad145f67fc1 100644
--- a/drivers/i2c/i2c-dev.c
+++ b/drivers/i2c/i2c-dev.c
@@ -73,41 +73,6 @@ static struct i2c_dev *i2c_dev_get_by_minor(unsigned index)
return i2c_dev;
}
-static struct i2c_dev *get_free_i2c_dev(struct i2c_adapter *adap)
-{
- struct i2c_dev *i2c_dev;
-
- if (adap->nr >= I2C_MINORS) {
- pr_err("Out of device minors (%d)\n", adap->nr);
- return ERR_PTR(-ENODEV);
- }
-
- i2c_dev = kzalloc_obj(*i2c_dev);
- if (!i2c_dev)
- return ERR_PTR(-ENOMEM);
- i2c_dev->adap = adap;
-
- spin_lock(&i2c_dev_list_lock);
- list_add_tail(&i2c_dev->list, &i2c_dev_list);
- spin_unlock(&i2c_dev_list_lock);
- return i2c_dev;
-}
-
-static void put_i2c_dev(struct i2c_dev *i2c_dev, bool del_cdev)
-{
- spin_lock(&i2c_dev_list_lock);
- list_del(&i2c_dev->list);
- spin_unlock(&i2c_dev_list_lock);
- if (del_cdev)
- cdev_device_del(&i2c_dev->cdev, &i2c_dev->dev);
-
- scoped_guard(rwsem_write, &i2c_dev->rwsem) {
- i2c_dev->adap = NULL;
- }
-
- put_device(&i2c_dev->dev);
-}
-
static ssize_t name_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
@@ -724,11 +689,17 @@ static int i2cdev_attach_adapter(struct device *dev)
return NOTIFY_DONE;
adap = to_i2c_adapter(dev);
- i2c_dev = get_free_i2c_dev(adap);
- if (IS_ERR(i2c_dev))
+ if (adap->nr >= I2C_MINORS) {
+ pr_err("Out of device minors (%d)\n", adap->nr);
+ return NOTIFY_DONE;
+ }
+
+ i2c_dev = kzalloc_obj(*i2c_dev);
+ if (!i2c_dev)
return NOTIFY_DONE;
init_rwsem(&i2c_dev->rwsem);
+ i2c_dev->adap = adap;
cdev_init(&i2c_dev->cdev, &i2cdev_fops);
i2c_dev->cdev.owner = adap->owner;
@@ -745,13 +716,21 @@ static int i2cdev_attach_adapter(struct device *dev)
res = cdev_device_add(&i2c_dev->cdev, &i2c_dev->dev);
if (res)
- goto err_put_i2c_dev;
+ goto err_clear_adap;
+
+ spin_lock(&i2c_dev_list_lock);
+ list_add_tail(&i2c_dev->list, &i2c_dev_list);
+ spin_unlock(&i2c_dev_list_lock);
pr_debug("adapter [%s] registered as minor %d\n", adap->name, adap->nr);
return NOTIFY_OK;
+err_clear_adap:
+ scoped_guard(rwsem_write, &i2c_dev->rwsem) {
+ i2c_dev->adap = NULL;
+ }
err_put_i2c_dev:
- put_i2c_dev(i2c_dev, false);
+ put_device(&i2c_dev->dev);
return NOTIFY_DONE;
}
@@ -768,7 +747,17 @@ static int i2cdev_detach_adapter(struct device *dev)
if (!i2c_dev) /* attach_adapter must have failed */
return NOTIFY_DONE;
- put_i2c_dev(i2c_dev, true);
+ spin_lock(&i2c_dev_list_lock);
+ list_del(&i2c_dev->list);
+ spin_unlock(&i2c_dev_list_lock);
+
+ cdev_device_del(&i2c_dev->cdev, &i2c_dev->dev);
+
+ scoped_guard(rwsem_write, &i2c_dev->rwsem) {
+ i2c_dev->adap = NULL;
+ }
+
+ put_device(&i2c_dev->dev);
pr_debug("adapter [%s] unregistered\n", adap->name);
return NOTIFY_OK;
--
2.54.0
prev parent reply other threads:[~2026-08-27 10:22 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-27 10:20 [PATCH v2 0/3] i2c: dev: fix blocked adapter deregistration Johan Hovold
2026-08-27 10:20 ` [PATCH v2 1/3] " Johan Hovold
2026-08-27 10:20 ` [PATCH v2 2/3] i2c: dev: drop unnecessary sysfs device lookup Johan Hovold
2026-08-27 10:20 ` Johan Hovold [this message]
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=20260827102043.673273-4-johan@kernel.org \
--to=johan@kernel.org \
--cc=andi.shyti@kernel.org \
--cc=linux-i2c@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=wsa+renesas@sang-engineering.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