Skip to content

Commit dad9bb0

Browse files
Heikki Krogerusrafaeljw
authored andcommitted
driver core: Add helper device_find_child_by_name()
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>
1 parent b06184a commit dad9bb0

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

drivers/base/core.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2474,6 +2474,34 @@ struct device *device_find_child(struct device *parent, void *data,
24742474
}
24752475
EXPORT_SYMBOL_GPL(device_find_child);
24762476

2477+
/**
2478+
* device_find_child_by_name - device iterator for locating a child device.
2479+
* @parent: parent struct device
2480+
* @name: name of the child device
2481+
*
2482+
* This is similar to the device_find_child() function above, but it
2483+
* returns a reference to a device that has the name @name.
2484+
*
2485+
* NOTE: you will need to drop the reference with put_device() after use.
2486+
*/
2487+
struct device *device_find_child_by_name(struct device *parent,
2488+
const char *name)
2489+
{
2490+
struct klist_iter i;
2491+
struct device *child;
2492+
2493+
if (!parent)
2494+
return NULL;
2495+
2496+
klist_iter_init(&parent->p->klist_children, &i);
2497+
while ((child = next_device(&i)))
2498+
if (!strcmp(dev_name(child), name) && get_device(child))
2499+
break;
2500+
klist_iter_exit(&i);
2501+
return child;
2502+
}
2503+
EXPORT_SYMBOL_GPL(device_find_child_by_name);
2504+
24772505
int __init devices_init(void)
24782506
{
24792507
devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);

include/linux/device.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,8 @@ extern int device_for_each_child_reverse(struct device *dev, void *data,
12501250
int (*fn)(struct device *dev, void *data));
12511251
extern struct device *device_find_child(struct device *dev, void *data,
12521252
int (*match)(struct device *dev, void *data));
1253+
extern struct device *device_find_child_by_name(struct device *parent,
1254+
const char *name);
12531255
extern int device_rename(struct device *dev, const char *new_name);
12541256
extern int device_move(struct device *dev, struct device *new_parent,
12551257
enum dpm_order dpm_order);

0 commit comments

Comments
 (0)