[RFC PATCH 08/12] drivers: base: introduce bus_remove_device_by_name()

Enrico Weigelt, metux IT consult
2021-02-08T22:26:39Z

Introduce a helper for detaching a named device from bus and
unregistering it. This is helpful eg. if some board specific driver
needs to remove an unwanted device that had been probed via firmware,
but should be handled differently.

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
---
drivers/base/bus.c | 16 ++++++++++++++++
include/linux/device/bus.h | 9 +++++++++
2 files changed, 25 insertions(+)

diff --git a/drivers/base/bus.c b/drivers/base/bus.c
index a9c23ecebc7c..450d3ed6cf1f 100644
--- a/drivers/base/bus.c
+++ b/drivers/base/bus.c
@@ -178,6 +178,22 @@ static const struct kset_uevent_ops bus_uevent_ops = {

static struct kset *bus_kset;

+int bus_unregister_device_by_name(struct bus_type *bus, const char *name)
+{
+ struct device *dev;
+
+ dev = bus_find_device_by_name(bus, NULL, name);
+ if (!dev)
+ return -ENOENT;
+
+ device_driver_detach(dev);
+ device_unregister(dev);
+ put_device(dev);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(bus_unregister_device_by_name);
+
/* Manually detach a device from its associated driver. */
static ssize_t unbind_store(struct device_driver *drv, const char *buf,
size_t count)
diff --git a/include/linux/device/bus.h b/include/linux/device/bus.h
index 1ea5e1d1545b..36a1dae26c95 100644
--- a/include/linux/device/bus.h
+++ b/include/linux/device/bus.h
@@ -253,6 +253,15 @@ int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
void bus_sort_breadthfirst(struct bus_type *bus,
int (*compare)(const struct device *a,
const struct device *b));
+
+/**
+ * bus_unregister_device_by_name - remove device by bus id from specific bus
+ * and unregister it from device core
+ * @bus: bus type
+ * @name: name of the device to remove
+ */
+int bus_unregister_device_by_name(struct bus_type *bus, const char *name);
+
/*
* Bus notifiers: Get notified of addition/removal of devices
* and binding/unbinding of drivers to devices.
--
2.11.0