driver core: Add helper device_find_child_by_name()
authorHeikki Krogerus <heikki.krogerus@linux.intel.com>
Fri, 31 May 2019 14:15:37 +0000 (17:15 +0300)
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>
Mon, 3 Jun 2019 08:55:38 +0000 (10:55 +0200)
It looks like the child device is often matched with a name.
This introduces a helper that does it automatically.

Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Tested-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
drivers/base/core.c
include/linux/device.h

index fd7511e04e62233eac98fec28fd5e90a1a111210..b4c64528f13ccab74f5933b89bf718a88e490045 100644 (file)
@@ -2474,6 +2474,34 @@ struct device *device_find_child(struct device *parent, void *data,
 }
 EXPORT_SYMBOL_GPL(device_find_child);
 
+/**
+ * device_find_child_by_name - device iterator for locating a child device.
+ * @parent: parent struct device
+ * @name: name of the child device
+ *
+ * This is similar to the device_find_child() function above, but it
+ * returns a reference to a device that has the name @name.
+ *
+ * NOTE: you will need to drop the reference with put_device() after use.
+ */
+struct device *device_find_child_by_name(struct device *parent,
+                                        const char *name)
+{
+       struct klist_iter i;
+       struct device *child;
+
+       if (!parent)
+               return NULL;
+
+       klist_iter_init(&parent->p->klist_children, &i);
+       while ((child = next_device(&i)))
+               if (!strcmp(dev_name(child), name) && get_device(child))
+                       break;
+       klist_iter_exit(&i);
+       return child;
+}
+EXPORT_SYMBOL_GPL(device_find_child_by_name);
+
 int __init devices_init(void)
 {
        devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
index e85264fb66161408aee2815001d8d5159ab04ac1..5489a759e1c5aa7d6eee5cb5c823cb4f33109224 100644 (file)
@@ -1250,6 +1250,8 @@ extern int device_for_each_child_reverse(struct device *dev, void *data,
                     int (*fn)(struct device *dev, void *data));
 extern struct device *device_find_child(struct device *dev, void *data,
                                int (*match)(struct device *dev, void *data));
+extern struct device *device_find_child_by_name(struct device *parent,
+                                               const char *name);
 extern int device_rename(struct device *dev, const char *new_name);
 extern int device_move(struct device *dev, struct device *new_parent,
                       enum dpm_order dpm_order);