Skip to content

Commit 74b5161

Browse files
committed
Merge tag 'i2c-for-6.14-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux
Pull i2c reverts from Wolfram Sang: "It turned out the new mechanism for handling created devices does not handle all muxing cases. Revert the changes to give a proper solution more time" * tag 'i2c-for-6.14-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux: Revert "i2c: Replace list-based mechanism for handling auto-detected clients" Revert "i2c: Replace list-based mechanism for handling userspace-created clients"
2 parents 595ab66 + 3bfa08f commit 74b5161

File tree

2 files changed

+83
-40
lines changed

2 files changed

+83
-40
lines changed

drivers/i2c/i2c-core-base.c

Lines changed: 75 additions & 38 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)
@@ -1700,6 +1704,23 @@ int i2c_add_numbered_adapter(struct i2c_adapter *adap)
17001704
}
17011705
EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
17021706

1707+
static void i2c_do_del_adapter(struct i2c_driver *driver,
1708+
struct i2c_adapter *adapter)
1709+
{
1710+
struct i2c_client *client, *_n;
1711+
1712+
/* Remove the devices we created ourselves as the result of hardware
1713+
* probing (using a driver's detect method) */
1714+
list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1715+
if (client->adapter == adapter) {
1716+
dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1717+
client->name, client->addr);
1718+
list_del(&client->detected);
1719+
i2c_unregister_device(client);
1720+
}
1721+
}
1722+
}
1723+
17031724
static int __unregister_client(struct device *dev, void *dummy)
17041725
{
17051726
struct i2c_client *client = i2c_verify_client(dev);
@@ -1715,6 +1736,12 @@ static int __unregister_dummy(struct device *dev, void *dummy)
17151736
return 0;
17161737
}
17171738

1739+
static int __process_removed_adapter(struct device_driver *d, void *data)
1740+
{
1741+
i2c_do_del_adapter(to_i2c_driver(d), data);
1742+
return 0;
1743+
}
1744+
17181745
/**
17191746
* i2c_del_adapter - unregister I2C adapter
17201747
* @adap: the adapter being unregistered
@@ -1726,6 +1753,7 @@ static int __unregister_dummy(struct device *dev, void *dummy)
17261753
void i2c_del_adapter(struct i2c_adapter *adap)
17271754
{
17281755
struct i2c_adapter *found;
1756+
struct i2c_client *client, *next;
17291757

17301758
/* First make sure that this adapter was ever added */
17311759
mutex_lock(&core_lock);
@@ -1737,16 +1765,31 @@ void i2c_del_adapter(struct i2c_adapter *adap)
17371765
}
17381766

17391767
i2c_acpi_remove_space_handler(adap);
1768+
/* Tell drivers about this removal */
1769+
mutex_lock(&core_lock);
1770+
bus_for_each_drv(&i2c_bus_type, NULL, adap,
1771+
__process_removed_adapter);
1772+
mutex_unlock(&core_lock);
1773+
1774+
/* Remove devices instantiated from sysfs */
1775+
mutex_lock_nested(&adap->userspace_clients_lock,
1776+
i2c_adapter_depth(adap));
1777+
list_for_each_entry_safe(client, next, &adap->userspace_clients,
1778+
detected) {
1779+
dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1780+
client->addr);
1781+
list_del(&client->detected);
1782+
i2c_unregister_device(client);
1783+
}
1784+
mutex_unlock(&adap->userspace_clients_lock);
17401785

17411786
/* Detach any active clients. This can't fail, thus we do not
17421787
* check the returned value. This is a two-pass process, because
17431788
* we can't remove the dummy devices during the first pass: they
17441789
* could have been instantiated by real devices wishing to clean
17451790
* them up properly, so we give them a chance to do that first. */
1746-
mutex_lock(&core_lock);
17471791
device_for_each_child(&adap->dev, NULL, __unregister_client);
17481792
device_for_each_child(&adap->dev, NULL, __unregister_dummy);
1749-
mutex_unlock(&core_lock);
17501793

17511794
/* device name is gone after device_unregister */
17521795
dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
@@ -1966,6 +2009,7 @@ int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
19662009
/* add the driver to the list of i2c drivers in the driver core */
19672010
driver->driver.owner = owner;
19682011
driver->driver.bus = &i2c_bus_type;
2012+
INIT_LIST_HEAD(&driver->clients);
19692013

19702014
/* When registration returns, the driver core
19712015
* will have called probe() for all matching-but-unbound devices.
@@ -1983,13 +2027,10 @@ int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
19832027
}
19842028
EXPORT_SYMBOL(i2c_register_driver);
19852029

1986-
static int __i2c_unregister_detected_client(struct device *dev, void *argp)
2030+
static int __process_removed_driver(struct device *dev, void *data)
19872031
{
1988-
struct i2c_client *client = i2c_verify_client(dev);
1989-
1990-
if (client && client->flags & I2C_CLIENT_AUTO)
1991-
i2c_unregister_device(client);
1992-
2032+
if (dev->type == &i2c_adapter_type)
2033+
i2c_do_del_adapter(data, to_i2c_adapter(dev));
19932034
return 0;
19942035
}
19952036

@@ -2000,12 +2041,7 @@ static int __i2c_unregister_detected_client(struct device *dev, void *argp)
20002041
*/
20012042
void i2c_del_driver(struct i2c_driver *driver)
20022043
{
2003-
mutex_lock(&core_lock);
2004-
/* Satisfy __must_check, function can't fail */
2005-
if (driver_for_each_device(&driver->driver, NULL, NULL,
2006-
__i2c_unregister_detected_client)) {
2007-
}
2008-
mutex_unlock(&core_lock);
2044+
i2c_for_each_dev(driver, __process_removed_driver);
20092045

20102046
driver_unregister(&driver->driver);
20112047
pr_debug("driver [%s] unregistered\n", driver->driver.name);
@@ -2432,7 +2468,6 @@ static int i2c_detect_address(struct i2c_client *temp_client,
24322468
/* Finally call the custom detection function */
24332469
memset(&info, 0, sizeof(struct i2c_board_info));
24342470
info.addr = addr;
2435-
info.flags = I2C_CLIENT_AUTO;
24362471
err = driver->detect(temp_client, &info);
24372472
if (err) {
24382473
/* -ENODEV is returned if the detection fails. We catch it
@@ -2459,7 +2494,9 @@ static int i2c_detect_address(struct i2c_client *temp_client,
24592494
dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
24602495
info.type, info.addr);
24612496
client = i2c_new_client_device(adapter, &info);
2462-
if (IS_ERR(client))
2497+
if (!IS_ERR(client))
2498+
list_add_tail(&client->detected, &driver->clients);
2499+
else
24632500
dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
24642501
info.type, info.addr);
24652502
}

include/linux/i2c.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ enum i2c_driver_flags {
244244
* @id_table: List of I2C devices supported by this driver
245245
* @detect: Callback for device detection
246246
* @address_list: The I2C addresses to probe (for detect)
247+
* @clients: List of detected clients we created (for i2c-core use only)
247248
* @flags: A bitmask of flags defined in &enum i2c_driver_flags
248249
*
249250
* The driver.owner field should be set to the module owner of this driver.
@@ -298,6 +299,7 @@ struct i2c_driver {
298299
/* Device detection callback for automatic device creation */
299300
int (*detect)(struct i2c_client *client, struct i2c_board_info *info);
300301
const unsigned short *address_list;
302+
struct list_head clients;
301303

302304
u32 flags;
303305
};
@@ -313,6 +315,8 @@ struct i2c_driver {
313315
* @dev: Driver model device node for the slave.
314316
* @init_irq: IRQ that was set at initialization
315317
* @irq: indicates the IRQ generated by this device (if any)
318+
* @detected: member of an i2c_driver.clients list or i2c-core's
319+
* userspace_devices list
316320
* @slave_cb: Callback when I2C slave mode of an adapter is used. The adapter
317321
* calls it to pass on slave events to the slave driver.
318322
* @devres_group_id: id of the devres group that will be created for resources
@@ -332,8 +336,6 @@ struct i2c_client {
332336
#define I2C_CLIENT_SLAVE 0x20 /* we are the slave */
333337
#define I2C_CLIENT_HOST_NOTIFY 0x40 /* We want to use I2C host notify */
334338
#define I2C_CLIENT_WAKE 0x80 /* for board_info; true iff can wake */
335-
#define I2C_CLIENT_AUTO 0x100 /* client was auto-detected */
336-
#define I2C_CLIENT_USER 0x200 /* client was userspace-created */
337339
#define I2C_CLIENT_SCCB 0x9000 /* Use Omnivision SCCB protocol */
338340
/* Must match I2C_M_STOP|IGNORE_NAK */
339341

@@ -345,6 +347,7 @@ struct i2c_client {
345347
struct device dev; /* the device structure */
346348
int init_irq; /* irq set at initialization */
347349
int irq; /* irq issued by device */
350+
struct list_head detected;
348351
#if IS_ENABLED(CONFIG_I2C_SLAVE)
349352
i2c_slave_cb_t slave_cb; /* callback for slave mode */
350353
#endif
@@ -751,6 +754,9 @@ struct i2c_adapter {
751754
char name[48];
752755
struct completion dev_released;
753756

757+
struct mutex userspace_clients_lock;
758+
struct list_head userspace_clients;
759+
754760
struct i2c_bus_recovery_info *bus_recovery_info;
755761
const struct i2c_adapter_quirks *quirks;
756762

0 commit comments

Comments
 (0)