Skip to content

Commit 56a5066

Browse files
hkallweitWolfram Sang
authored andcommitted
i2c: Replace list-based mechanism for handling auto-detected clients
So far a list is used to track auto-detected clients per driver. The same functionality can be achieved much simpler by flagging auto-detected clients. Two notes regarding the usage of driver_for_each_device: In our case it can't fail, however the function is annotated __must_check. So a little workaround is needed to avoid a compiler warning. Then we may remove nodes from the list over which we iterate. This is safe, see the explanation at the beginning of lib/klist.c. Signed-off-by: Heiner Kallweit <[email protected]> [wsa: fixed description of the new flag] Signed-off-by: Wolfram Sang <[email protected]>
1 parent 60be800 commit 56a5066

File tree

2 files changed

+17
-38
lines changed

2 files changed

+17
-38
lines changed

drivers/i2c/i2c-core-base.c

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,23 +1696,6 @@ int i2c_add_numbered_adapter(struct i2c_adapter *adap)
16961696
}
16971697
EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
16981698

1699-
static void i2c_do_del_adapter(struct i2c_driver *driver,
1700-
struct i2c_adapter *adapter)
1701-
{
1702-
struct i2c_client *client, *_n;
1703-
1704-
/* Remove the devices we created ourselves as the result of hardware
1705-
* probing (using a driver's detect method) */
1706-
list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1707-
if (client->adapter == adapter) {
1708-
dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1709-
client->name, client->addr);
1710-
list_del(&client->detected);
1711-
i2c_unregister_device(client);
1712-
}
1713-
}
1714-
}
1715-
17161699
static int __unregister_client(struct device *dev, void *dummy)
17171700
{
17181701
struct i2c_client *client = i2c_verify_client(dev);
@@ -1728,12 +1711,6 @@ static int __unregister_dummy(struct device *dev, void *dummy)
17281711
return 0;
17291712
}
17301713

1731-
static int __process_removed_adapter(struct device_driver *d, void *data)
1732-
{
1733-
i2c_do_del_adapter(to_i2c_driver(d), data);
1734-
return 0;
1735-
}
1736-
17371714
/**
17381715
* i2c_del_adapter - unregister I2C adapter
17391716
* @adap: the adapter being unregistered
@@ -1757,11 +1734,6 @@ void i2c_del_adapter(struct i2c_adapter *adap)
17571734
}
17581735

17591736
i2c_acpi_remove_space_handler(adap);
1760-
/* Tell drivers about this removal */
1761-
mutex_lock(&core_lock);
1762-
bus_for_each_drv(&i2c_bus_type, NULL, adap,
1763-
__process_removed_adapter);
1764-
mutex_unlock(&core_lock);
17651737

17661738
/* Remove devices instantiated from sysfs */
17671739
mutex_lock_nested(&adap->userspace_clients_lock,
@@ -1780,8 +1752,10 @@ void i2c_del_adapter(struct i2c_adapter *adap)
17801752
* we can't remove the dummy devices during the first pass: they
17811753
* could have been instantiated by real devices wishing to clean
17821754
* them up properly, so we give them a chance to do that first. */
1755+
mutex_lock(&core_lock);
17831756
device_for_each_child(&adap->dev, NULL, __unregister_client);
17841757
device_for_each_child(&adap->dev, NULL, __unregister_dummy);
1758+
mutex_unlock(&core_lock);
17851759

17861760
/* device name is gone after device_unregister */
17871761
dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
@@ -2001,7 +1975,6 @@ int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
20011975
/* add the driver to the list of i2c drivers in the driver core */
20021976
driver->driver.owner = owner;
20031977
driver->driver.bus = &i2c_bus_type;
2004-
INIT_LIST_HEAD(&driver->clients);
20051978

20061979
/* When registration returns, the driver core
20071980
* will have called probe() for all matching-but-unbound devices.
@@ -2019,10 +1992,13 @@ int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
20191992
}
20201993
EXPORT_SYMBOL(i2c_register_driver);
20211994

2022-
static int __process_removed_driver(struct device *dev, void *data)
1995+
static int __i2c_unregister_detected_client(struct device *dev, void *argp)
20231996
{
2024-
if (dev->type == &i2c_adapter_type)
2025-
i2c_do_del_adapter(data, to_i2c_adapter(dev));
1997+
struct i2c_client *client = i2c_verify_client(dev);
1998+
1999+
if (client && client->flags & I2C_CLIENT_AUTO)
2000+
i2c_unregister_device(client);
2001+
20262002
return 0;
20272003
}
20282004

@@ -2033,7 +2009,12 @@ static int __process_removed_driver(struct device *dev, void *data)
20332009
*/
20342010
void i2c_del_driver(struct i2c_driver *driver)
20352011
{
2036-
i2c_for_each_dev(driver, __process_removed_driver);
2012+
mutex_lock(&core_lock);
2013+
/* Satisfy __must_check, function can't fail */
2014+
if (driver_for_each_device(&driver->driver, NULL, NULL,
2015+
__i2c_unregister_detected_client)) {
2016+
}
2017+
mutex_unlock(&core_lock);
20372018

20382019
driver_unregister(&driver->driver);
20392020
pr_debug("driver [%s] unregistered\n", driver->driver.name);
@@ -2460,6 +2441,7 @@ static int i2c_detect_address(struct i2c_client *temp_client,
24602441
/* Finally call the custom detection function */
24612442
memset(&info, 0, sizeof(struct i2c_board_info));
24622443
info.addr = addr;
2444+
info.flags = I2C_CLIENT_AUTO;
24632445
err = driver->detect(temp_client, &info);
24642446
if (err) {
24652447
/* -ENODEV is returned if the detection fails. We catch it
@@ -2486,9 +2468,7 @@ static int i2c_detect_address(struct i2c_client *temp_client,
24862468
dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
24872469
info.type, info.addr);
24882470
client = i2c_new_client_device(adapter, &info);
2489-
if (!IS_ERR(client))
2490-
list_add_tail(&client->detected, &driver->clients);
2491-
else
2471+
if (IS_ERR(client))
24922472
dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
24932473
info.type, info.addr);
24942474
}

include/linux/i2c.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ 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)
248247
* @flags: A bitmask of flags defined in &enum i2c_driver_flags
249248
*
250249
* The driver.owner field should be set to the module owner of this driver.
@@ -299,7 +298,6 @@ struct i2c_driver {
299298
/* Device detection callback for automatic device creation */
300299
int (*detect)(struct i2c_client *client, struct i2c_board_info *info);
301300
const unsigned short *address_list;
302-
struct list_head clients;
303301

304302
u32 flags;
305303
};
@@ -334,6 +332,7 @@ struct i2c_client {
334332
#define I2C_CLIENT_SLAVE 0x20 /* we are the slave */
335333
#define I2C_CLIENT_HOST_NOTIFY 0x40 /* We want to use I2C host notify */
336334
#define I2C_CLIENT_WAKE 0x80 /* for board_info; true iff can wake */
335+
#define I2C_CLIENT_AUTO 0x100 /* client was auto-detected */
337336
#define I2C_CLIENT_SCCB 0x9000 /* Use Omnivision SCCB protocol */
338337
/* Must match I2C_M_STOP|IGNORE_NAK */
339338

0 commit comments

Comments
 (0)