i2c: Replace list-based mechanism for handling userspace-created clients
authorHeiner Kallweit <hkallweit1@gmail.com>
Fri, 1 Nov 2024 22:11:39 +0000 (23:11 +0100)
committerWolfram Sang <wsa+renesas@sang-engineering.com>
Fri, 3 Jan 2025 12:22:54 +0000 (13:22 +0100)
Similar to the list of auto-detected clients, we can also replace the
list of userspace-created clients with flagging such client devices.

Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
[wsa: fixed description of the new flag; reordered new code in
'device_store' to have single exit point; fixed whitespace errors;
 folded cleanup patch into this one]
Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
drivers/i2c/i2c-core-base.c
include/linux/i2c.h

index 42c62839de0caf5e602d71f96d8cdfdb2f6021fc..bd90a6084fc0d632794ce8233304b3fb7dfc0452 100644 (file)
@@ -1293,14 +1293,12 @@ new_device_store(struct device *dev, struct device_attribute *attr,
                info.flags |= I2C_CLIENT_SLAVE;
        }
 
+       info.flags |= I2C_CLIENT_USER;
+
        client = i2c_new_client_device(adap, &info);
        if (IS_ERR(client))
                return PTR_ERR(client);
 
-       /* Keep track of the added device */
-       mutex_lock(&adap->userspace_clients_lock);
-       list_add_tail(&client->detected, &adap->userspace_clients);
-       mutex_unlock(&adap->userspace_clients_lock);
        dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
                 info.type, info.addr);
 
@@ -1308,6 +1306,15 @@ new_device_store(struct device *dev, struct device_attribute *attr,
 }
 static DEVICE_ATTR_WO(new_device);
 
+static int __i2c_find_user_addr(struct device *dev, void *addrp)
+{
+       struct i2c_client *client = i2c_verify_client(dev);
+       unsigned short addr = *(unsigned short *)addrp;
+
+       return client && client->flags & I2C_CLIENT_USER &&
+              i2c_encode_flags_to_addr(client) == addr;
+}
+
 /*
  * And of course let the users delete the devices they instantiated, if
  * they got it wrong. This interface can only be used to delete devices
@@ -1322,7 +1329,7 @@ delete_device_store(struct device *dev, struct device_attribute *attr,
                    const char *buf, size_t count)
 {
        struct i2c_adapter *adap = to_i2c_adapter(dev);
-       struct i2c_client *client, *next;
+       struct device *child_dev;
        unsigned short addr;
        char end;
        int res;
@@ -1338,28 +1345,19 @@ delete_device_store(struct device *dev, struct device_attribute *attr,
                return -EINVAL;
        }
 
+       mutex_lock(&core_lock);
        /* Make sure the device was added through sysfs */
-       res = -ENOENT;
-       mutex_lock_nested(&adap->userspace_clients_lock,
-                         i2c_adapter_depth(adap));
-       list_for_each_entry_safe(client, next, &adap->userspace_clients,
-                                detected) {
-               if (i2c_encode_flags_to_addr(client) == addr) {
-                       dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
-                                "delete_device", client->name, client->addr);
-
-                       list_del(&client->detected);
-                       i2c_unregister_device(client);
-                       res = count;
-                       break;
-               }
+       child_dev = device_find_child(&adap->dev, &addr, __i2c_find_user_addr);
+       if (child_dev) {
+               i2c_unregister_device(i2c_verify_client(child_dev));
+               put_device(child_dev);
+       } else {
+               dev_err(dev, "Can't find userspace-created device at %#x\n", addr);
+               count = -ENOENT;
        }
-       mutex_unlock(&adap->userspace_clients_lock);
+       mutex_unlock(&core_lock);
 
-       if (res < 0)
-               dev_err(dev, "%s: Can't find device in list\n",
-                       "delete_device");
-       return res;
+       return count;
 }
 static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
                                  delete_device_store);
@@ -1530,8 +1528,6 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
        adap->locked_flags = 0;
        rt_mutex_init(&adap->bus_lock);
        rt_mutex_init(&adap->mux_lock);
-       mutex_init(&adap->userspace_clients_lock);
-       INIT_LIST_HEAD(&adap->userspace_clients);
 
        /* Set default timeout to 1 second if not already set */
        if (adap->timeout == 0)
@@ -1722,7 +1718,6 @@ static int __unregister_dummy(struct device *dev, void *dummy)
 void i2c_del_adapter(struct i2c_adapter *adap)
 {
        struct i2c_adapter *found;
-       struct i2c_client *client, *next;
 
        /* First make sure that this adapter was ever added */
        mutex_lock(&core_lock);
@@ -1735,18 +1730,6 @@ void i2c_del_adapter(struct i2c_adapter *adap)
 
        i2c_acpi_remove_space_handler(adap);
 
-       /* Remove devices instantiated from sysfs */
-       mutex_lock_nested(&adap->userspace_clients_lock,
-                         i2c_adapter_depth(adap));
-       list_for_each_entry_safe(client, next, &adap->userspace_clients,
-                                detected) {
-               dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
-                       client->addr);
-               list_del(&client->detected);
-               i2c_unregister_device(client);
-       }
-       mutex_unlock(&adap->userspace_clients_lock);
-
        /* Detach any active clients. This can't fail, thus we do not
         * check the returned value. This is a two-pass process, because
         * we can't remove the dummy devices during the first pass: they
index 072a62244e14a8b412b49386703d532834c569a2..66fb3d6cf686c71f624ac50d63e2f2e3daf84a60 100644 (file)
@@ -313,8 +313,6 @@ struct i2c_driver {
  * @dev: Driver model device node for the slave.
  * @init_irq: IRQ that was set at initialization
  * @irq: indicates the IRQ generated by this device (if any)
- * @detected: member of an i2c_driver.clients list or i2c-core's
- *     userspace_devices list
  * @slave_cb: Callback when I2C slave mode of an adapter is used. The adapter
  *     calls it to pass on slave events to the slave driver.
  * @devres_group_id: id of the devres group that will be created for resources
@@ -333,6 +331,7 @@ struct i2c_client {
 #define I2C_CLIENT_HOST_NOTIFY 0x40    /* We want to use I2C host notify */
 #define I2C_CLIENT_WAKE                0x80    /* for board_info; true iff can wake */
 #define I2C_CLIENT_AUTO                0x100   /* client was auto-detected */
+#define I2C_CLIENT_USER                0x200   /* client was userspace-created */
 #define I2C_CLIENT_SCCB                0x9000  /* Use Omnivision SCCB protocol */
                                        /* Must match I2C_M_STOP|IGNORE_NAK */
 
@@ -344,7 +343,6 @@ struct i2c_client {
        struct device dev;              /* the device structure         */
        int init_irq;                   /* irq set at initialization    */
        int irq;                        /* irq issued by device         */
-       struct list_head detected;
 #if IS_ENABLED(CONFIG_I2C_SLAVE)
        i2c_slave_cb_t slave_cb;        /* callback for slave mode      */
 #endif
@@ -750,9 +748,6 @@ struct i2c_adapter {
        char name[48];
        struct completion dev_released;
 
-       struct mutex userspace_clients_lock;
-       struct list_head userspace_clients;
-
        struct i2c_bus_recovery_info *bus_recovery_info;
        const struct i2c_adapter_quirks *quirks;