Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

driver core: Introduce device_iter_t for device iterating APIs

There are several for_each APIs which has parameter with type below:
int (*fn)(struct device *dev, void *data)
They iterate over various device lists and call @fn() for each device
with caller provided data @*data, and they usually need to modify @*data.

Give the type an dedicated typedef with advantages shown below:
typedef int (*device_iter_t)(struct device *dev, void *data)

- Shorter API declarations and definitions
- Prevent further for_each APIs from using bad parameter type

So introduce device_iter_t and apply it to various existing APIs below:
bus_for_each_dev()
(class|driver)_for_each_device()
device_for_each_child(_reverse|_reverse_from)().

Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
Link: https://lore.kernel.org/r/20250105-class_fix-v6-7-3a2f1768d4d4@quicinc.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Zijun Hu and committed by
Greg Kroah-Hartman
767b74e0 523c6b3e

+17 -14
+1 -1
drivers/base/bus.c
··· 354 354 * count in the supplied callback. 355 355 */ 356 356 int bus_for_each_dev(const struct bus_type *bus, struct device *start, 357 - void *data, int (*fn)(struct device *, void *)) 357 + void *data, device_iter_t fn) 358 358 { 359 359 struct subsys_private *sp = bus_to_subsys(bus); 360 360 struct klist_iter i;
+1 -1
drivers/base/class.c
··· 402 402 * code. There's no locking restriction. 403 403 */ 404 404 int class_for_each_device(const struct class *class, const struct device *start, 405 - void *data, int (*fn)(struct device *, void *)) 405 + void *data, device_iter_t fn) 406 406 { 407 407 struct subsys_private *sp = class_to_subsys(class); 408 408 struct class_dev_iter iter;
+3 -3
drivers/base/core.c
··· 3980 3980 * other than 0, we break out and return that value. 3981 3981 */ 3982 3982 int device_for_each_child(struct device *parent, void *data, 3983 - int (*fn)(struct device *dev, void *data)) 3983 + device_iter_t fn) 3984 3984 { 3985 3985 struct klist_iter i; 3986 3986 struct device *child; ··· 4010 4010 * other than 0, we break out and return that value. 4011 4011 */ 4012 4012 int device_for_each_child_reverse(struct device *parent, void *data, 4013 - int (*fn)(struct device *dev, void *data)) 4013 + device_iter_t fn) 4014 4014 { 4015 4015 struct klist_iter i; 4016 4016 struct device *child; ··· 4044 4044 */ 4045 4045 int device_for_each_child_reverse_from(struct device *parent, 4046 4046 struct device *from, void *data, 4047 - int (*fn)(struct device *, void *)) 4047 + device_iter_t fn) 4048 4048 { 4049 4049 struct klist_iter i; 4050 4050 struct device *child;
+1 -1
drivers/base/driver.c
··· 115 115 * Iterate over the @drv's list of devices calling @fn for each one. 116 116 */ 117 117 int driver_for_each_device(struct device_driver *drv, struct device *start, 118 - void *data, int (*fn)(struct device *, void *)) 118 + void *data, device_iter_t fn) 119 119 { 120 120 struct klist_iter i; 121 121 struct device *dev;
+3 -3
include/linux/device.h
··· 1075 1075 DEFINE_FREE(device_del, struct device *, if (_T) device_del(_T)) 1076 1076 1077 1077 int device_for_each_child(struct device *parent, void *data, 1078 - int (*fn)(struct device *dev, void *data)); 1078 + device_iter_t fn); 1079 1079 int device_for_each_child_reverse(struct device *parent, void *data, 1080 - int (*fn)(struct device *dev, void *data)); 1080 + device_iter_t fn); 1081 1081 int device_for_each_child_reverse_from(struct device *parent, 1082 1082 struct device *from, void *data, 1083 - int (*fn)(struct device *, void *)); 1083 + device_iter_t fn); 1084 1084 struct device *device_find_child(struct device *parent, const void *data, 1085 1085 device_match_t match); 1086 1086 struct device *device_find_child_by_name(struct device *parent,
+5 -2
include/linux/device/bus.h
··· 139 139 int device_match_acpi_handle(struct device *dev, const void *handle); 140 140 int device_match_any(struct device *dev, const void *unused); 141 141 142 + /* Device iterating function type for various driver core for_each APIs */ 143 + typedef int (*device_iter_t)(struct device *dev, void *data); 144 + 142 145 /* iterator helpers for buses */ 143 - int bus_for_each_dev(const struct bus_type *bus, struct device *start, void *data, 144 - int (*fn)(struct device *dev, void *data)); 146 + int bus_for_each_dev(const struct bus_type *bus, struct device *start, 147 + void *data, device_iter_t fn); 145 148 struct device *bus_find_device(const struct bus_type *bus, struct device *start, 146 149 const void *data, device_match_t match); 147 150 /**
+2 -2
include/linux/device/class.h
··· 92 92 struct device *class_dev_iter_next(struct class_dev_iter *iter); 93 93 void class_dev_iter_exit(struct class_dev_iter *iter); 94 94 95 - int class_for_each_device(const struct class *class, const struct device *start, void *data, 96 - int (*fn)(struct device *dev, void *data)); 95 + int class_for_each_device(const struct class *class, const struct device *start, 96 + void *data, device_iter_t fn); 97 97 struct device *class_find_device(const struct class *class, const struct device *start, 98 98 const void *data, device_match_t match); 99 99
+1 -1
include/linux/device/driver.h
··· 154 154 int driver_set_override(struct device *dev, const char **override, 155 155 const char *s, size_t len); 156 156 int __must_check driver_for_each_device(struct device_driver *drv, struct device *start, 157 - void *data, int (*fn)(struct device *dev, void *)); 157 + void *data, device_iter_t fn); 158 158 struct device *driver_find_device(const struct device_driver *drv, 159 159 struct device *start, const void *data, 160 160 device_match_t match);