Skip to content

Commit aae1cf3

Browse files
dlechProject516
authored andcommitted
i2c: expose adapter probe and remove probed functions
Publicly expose functions for detecting devices and removing detected devices. Currently an i2c adapter only probes/detects new devices when the adapter is added or when a new i2c driver is added. This adds a new function, i2c_adapter_probe(), that allows detecting sensors at any time. Furthermore, the function i2c_adapter_remove_probed() is added to remove the devices added by i2c_adapter_probe() at any time. The intended use of these functions is for LEGO MINDSTORMS sensors. These are hot-plugable I2C devices. When a sensor is connected, i2c_adapter_probe() is called to automatically configure the sensor. When the sensor is disconnected, i2c_adapter_remove_probed() is called to remove the automatically configured device. Signed-off-by: David Lechner <[email protected]>
1 parent b0c9351 commit aae1cf3

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

drivers/i2c/i2c-core-base.c

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,6 +1505,23 @@ int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr)
15051505
}
15061506
EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify);
15071507

1508+
/**
1509+
* i2c_adapter_probe - probe adapter for clients
1510+
* @adap: The i2c adapter.
1511+
*
1512+
* This calls the detect function of each driver that matches the class of the
1513+
* adapter. This function is called when an adapter is added, so there is no
1514+
* need to call this manually, unless you have hot-plugable i2c devices that
1515+
* are connected after the adapter is created.
1516+
*/
1517+
void i2c_adapter_probe(struct i2c_adapter *adap)
1518+
{
1519+
mutex_lock(&core_lock);
1520+
bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
1521+
mutex_unlock(&core_lock);
1522+
}
1523+
EXPORT_SYMBOL(i2c_adapter_probe);
1524+
15081525
static int i2c_register_adapter(struct i2c_adapter *adap)
15091526
{
15101527
int res = -EINVAL;
@@ -1587,9 +1604,7 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
15871604
i2c_scan_static_board_info(adap);
15881605

15891606
/* Notify drivers */
1590-
mutex_lock(&core_lock);
1591-
bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
1592-
mutex_unlock(&core_lock);
1607+
i2c_adapter_probe(adap);
15931608

15941609
return 0;
15951610

@@ -1735,6 +1750,25 @@ static int __process_removed_adapter(struct device_driver *d, void *data)
17351750
return 0;
17361751
}
17371752

1753+
/**
1754+
* i2c_adapter_remove_probed - Remove clients that were automatically detected.
1755+
* @adap: The i2c adapter.
1756+
*
1757+
* This removes all i2c clients from this adapter that were added via driver
1758+
* detect() functions. This function is called when an adapter is removed, so
1759+
* there is no need to call this manually, unless you have hot-plugable i2c
1760+
* devices that are disconnected before the adapter is destroyed.
1761+
*
1762+
* Any clients that were added manually (e.g. via sysfs) are not removed.
1763+
*/
1764+
void i2c_adapter_remove_probed(struct i2c_adapter *adap)
1765+
{
1766+
mutex_lock(&core_lock);
1767+
bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_removed_adapter);
1768+
mutex_unlock(&core_lock);
1769+
}
1770+
EXPORT_SYMBOL(i2c_adapter_remove_probed);
1771+
17381772
/**
17391773
* i2c_del_adapter - unregister I2C adapter
17401774
* @adap: the adapter being unregistered
@@ -1759,10 +1793,7 @@ void i2c_del_adapter(struct i2c_adapter *adap)
17591793

17601794
i2c_acpi_remove_space_handler(adap);
17611795
/* Tell drivers about this removal */
1762-
mutex_lock(&core_lock);
1763-
bus_for_each_drv(&i2c_bus_type, NULL, adap,
1764-
__process_removed_adapter);
1765-
mutex_unlock(&core_lock);
1796+
i2c_adapter_remove_probed(adap);
17661797

17671798
/* Remove devices instantiated from sysfs */
17681799
mutex_lock_nested(&adap->userspace_clients_lock,

include/linux/i2c.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,9 @@ int devm_i2c_add_adapter(struct device *dev, struct i2c_adapter *adapter);
890890
void i2c_del_adapter(struct i2c_adapter *adap);
891891
int i2c_add_numbered_adapter(struct i2c_adapter *adap);
892892

893+
extern void i2c_adapter_probe(struct i2c_adapter *adap);
894+
extern void i2c_adapter_remove_probed(struct i2c_adapter *adap);
895+
893896
int i2c_register_driver(struct module *owner, struct i2c_driver *driver);
894897
void i2c_del_driver(struct i2c_driver *driver);
895898

0 commit comments

Comments
 (0)