Skip to content

Commit c4d3dfd

Browse files
author
Wolfram Sang
committed
Revert "i2c: Replace list-based mechanism for handling userspace-created clients"
This reverts commit 3cfe39b. Mux handling is not sufficiently implemented. It needs more time. Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
1 parent 2014c95 commit c4d3dfd

File tree

2 files changed

+45
-23
lines changed

2 files changed

+45
-23
lines changed

drivers/i2c/i2c-core-base.c

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,28 +1300,21 @@ new_device_store(struct device *dev, struct device_attribute *attr,
13001300
info.flags |= I2C_CLIENT_SLAVE;
13011301
}
13021302

1303-
info.flags |= I2C_CLIENT_USER;
1304-
13051303
client = i2c_new_client_device(adap, &info);
13061304
if (IS_ERR(client))
13071305
return PTR_ERR(client);
13081306

1307+
/* Keep track of the added device */
1308+
mutex_lock(&adap->userspace_clients_lock);
1309+
list_add_tail(&client->detected, &adap->userspace_clients);
1310+
mutex_unlock(&adap->userspace_clients_lock);
13091311
dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
13101312
info.type, info.addr);
13111313

13121314
return count;
13131315
}
13141316
static DEVICE_ATTR_WO(new_device);
13151317

1316-
static int __i2c_find_user_addr(struct device *dev, const void *addrp)
1317-
{
1318-
struct i2c_client *client = i2c_verify_client(dev);
1319-
unsigned short addr = *(unsigned short *)addrp;
1320-
1321-
return client && client->flags & I2C_CLIENT_USER &&
1322-
i2c_encode_flags_to_addr(client) == addr;
1323-
}
1324-
13251318
/*
13261319
* And of course let the users delete the devices they instantiated, if
13271320
* they got it wrong. This interface can only be used to delete devices
@@ -1336,7 +1329,7 @@ delete_device_store(struct device *dev, struct device_attribute *attr,
13361329
const char *buf, size_t count)
13371330
{
13381331
struct i2c_adapter *adap = to_i2c_adapter(dev);
1339-
struct device *child_dev;
1332+
struct i2c_client *client, *next;
13401333
unsigned short addr;
13411334
char end;
13421335
int res;
@@ -1352,19 +1345,28 @@ delete_device_store(struct device *dev, struct device_attribute *attr,
13521345
return -EINVAL;
13531346
}
13541347

1355-
mutex_lock(&core_lock);
13561348
/* Make sure the device was added through sysfs */
1357-
child_dev = device_find_child(&adap->dev, &addr, __i2c_find_user_addr);
1358-
if (child_dev) {
1359-
i2c_unregister_device(i2c_verify_client(child_dev));
1360-
put_device(child_dev);
1361-
} else {
1362-
dev_err(dev, "Can't find userspace-created device at %#x\n", addr);
1363-
count = -ENOENT;
1349+
res = -ENOENT;
1350+
mutex_lock_nested(&adap->userspace_clients_lock,
1351+
i2c_adapter_depth(adap));
1352+
list_for_each_entry_safe(client, next, &adap->userspace_clients,
1353+
detected) {
1354+
if (i2c_encode_flags_to_addr(client) == addr) {
1355+
dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1356+
"delete_device", client->name, client->addr);
1357+
1358+
list_del(&client->detected);
1359+
i2c_unregister_device(client);
1360+
res = count;
1361+
break;
1362+
}
13641363
}
1365-
mutex_unlock(&core_lock);
1364+
mutex_unlock(&adap->userspace_clients_lock);
13661365

1367-
return count;
1366+
if (res < 0)
1367+
dev_err(dev, "%s: Can't find device in list\n",
1368+
"delete_device");
1369+
return res;
13681370
}
13691371
static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
13701372
delete_device_store);
@@ -1535,6 +1537,8 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
15351537
adap->locked_flags = 0;
15361538
rt_mutex_init(&adap->bus_lock);
15371539
rt_mutex_init(&adap->mux_lock);
1540+
mutex_init(&adap->userspace_clients_lock);
1541+
INIT_LIST_HEAD(&adap->userspace_clients);
15381542

15391543
/* Set default timeout to 1 second if not already set */
15401544
if (adap->timeout == 0)
@@ -1726,6 +1730,7 @@ static int __unregister_dummy(struct device *dev, void *dummy)
17261730
void i2c_del_adapter(struct i2c_adapter *adap)
17271731
{
17281732
struct i2c_adapter *found;
1733+
struct i2c_client *client, *next;
17291734

17301735
/* First make sure that this adapter was ever added */
17311736
mutex_lock(&core_lock);
@@ -1738,6 +1743,18 @@ void i2c_del_adapter(struct i2c_adapter *adap)
17381743

17391744
i2c_acpi_remove_space_handler(adap);
17401745

1746+
/* Remove devices instantiated from sysfs */
1747+
mutex_lock_nested(&adap->userspace_clients_lock,
1748+
i2c_adapter_depth(adap));
1749+
list_for_each_entry_safe(client, next, &adap->userspace_clients,
1750+
detected) {
1751+
dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1752+
client->addr);
1753+
list_del(&client->detected);
1754+
i2c_unregister_device(client);
1755+
}
1756+
mutex_unlock(&adap->userspace_clients_lock);
1757+
17411758
/* Detach any active clients. This can't fail, thus we do not
17421759
* check the returned value. This is a two-pass process, because
17431760
* we can't remove the dummy devices during the first pass: they

include/linux/i2c.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ struct i2c_driver {
313313
* @dev: Driver model device node for the slave.
314314
* @init_irq: IRQ that was set at initialization
315315
* @irq: indicates the IRQ generated by this device (if any)
316+
* @detected: member of an i2c_driver.clients list or i2c-core's
317+
* userspace_devices list
316318
* @slave_cb: Callback when I2C slave mode of an adapter is used. The adapter
317319
* calls it to pass on slave events to the slave driver.
318320
* @devres_group_id: id of the devres group that will be created for resources
@@ -333,7 +335,6 @@ struct i2c_client {
333335
#define I2C_CLIENT_HOST_NOTIFY 0x40 /* We want to use I2C host notify */
334336
#define I2C_CLIENT_WAKE 0x80 /* for board_info; true iff can wake */
335337
#define I2C_CLIENT_AUTO 0x100 /* client was auto-detected */
336-
#define I2C_CLIENT_USER 0x200 /* client was userspace-created */
337338
#define I2C_CLIENT_SCCB 0x9000 /* Use Omnivision SCCB protocol */
338339
/* Must match I2C_M_STOP|IGNORE_NAK */
339340

@@ -345,6 +346,7 @@ struct i2c_client {
345346
struct device dev; /* the device structure */
346347
int init_irq; /* irq set at initialization */
347348
int irq; /* irq issued by device */
349+
struct list_head detected;
348350
#if IS_ENABLED(CONFIG_I2C_SLAVE)
349351
i2c_slave_cb_t slave_cb; /* callback for slave mode */
350352
#endif
@@ -751,6 +753,9 @@ struct i2c_adapter {
751753
char name[48];
752754
struct completion dev_released;
753755

756+
struct mutex userspace_clients_lock;
757+
struct list_head userspace_clients;
758+
754759
struct i2c_bus_recovery_info *bus_recovery_info;
755760
const struct i2c_adapter_quirks *quirks;
756761

0 commit comments

Comments
 (0)