Skip to content

Commit fae3cd0

Browse files
mochel@digitalimplant.orggregkh
authored andcommitted
[PATCH] Add driver_for_each_device().
Now there's an iterator for accessing each device bound to a driver. Signed-off-by: Patrick Mochel <mochel@digitalimplant.org> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de> Index: linux-2.6.12-rc2/drivers/base/driver.c ===================================================================
1 parent 07e4a3e commit fae3cd0

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

‎drivers/base/driver.c‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,41 @@
1818
#define to_dev(node) container_of(node, struct device, driver_list)
1919
#define to_drv(obj) container_of(obj, struct device_driver, kobj)
2020

21+
22+
/**
23+
* driver_for_each_device - Iterator for devices bound to a driver.
24+
* @drv: Driver we're iterating.
25+
* @data: Data to pass to the callback.
26+
* @fn: Function to call for each device.
27+
*
28+
* Take the bus's rwsem and iterate over the @drv's list of devices,
29+
* calling @fn for each one.
30+
*/
31+
32+
int driver_for_each_device(struct device_driver * drv, struct device * start,
33+
void * data, int (*fn)(struct device *, void *))
34+
{
35+
struct list_head * head;
36+
struct device * dev;
37+
int error = 0;
38+
39+
down_read(&drv->bus->subsys.rwsem);
40+
head = &drv->devices;
41+
dev = list_prepare_entry(start, head, driver_list);
42+
list_for_each_entry_continue(dev, head, driver_list) {
43+
get_device(dev);
44+
error = fn(dev, data);
45+
put_device(dev);
46+
if (error)
47+
break;
48+
}
49+
up_read(&drv->bus->subsys.rwsem);
50+
return error;
51+
}
52+
53+
EXPORT_SYMBOL(driver_for_each_device);
54+
55+
2156
/**
2257
* driver_create_file - create sysfs file for driver.
2358
* @drv: driver.

‎include/linux/device.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ struct driver_attribute driver_attr_##_name = __ATTR(_name,_mode,_show,_store)
136136
extern int driver_create_file(struct device_driver *, struct driver_attribute *);
137137
extern void driver_remove_file(struct device_driver *, struct driver_attribute *);
138138

139+
extern int driver_for_each_device(struct device_driver * drv, struct device * start,
140+
void * data, int (*fn)(struct device *, void *));
141+
139142

140143
/*
141144
* device classes

0 commit comments

Comments
 (0)