[PATCH 1/3] base: class: add convenience helpers for iteration
Ahmad Fatoum
a.fatoum at pengutronix.de
Mon Apr 14 00:05:22 PDT 2025
Using list_for_each_entry with a last argument that is a member of a
member gets unwieldy, so let's add macros for use in macro definitions
for:
- iterating through all container of devices
- getting the first device
Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
drivers/rtc/class.c | 3 ++-
drivers/watchdog/wd_core.c | 3 ++-
include/device.h | 6 ++++++
include/net.h | 3 ++-
4 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c
index c09ec3f61ea2..6bbf5b31833f 100644
--- a/drivers/rtc/class.c
+++ b/drivers/rtc/class.c
@@ -10,7 +10,8 @@
#include <linux/rtc.h>
#include <device.h>
-#define for_each_rtc(rtc) list_for_each_entry(rtc, &rtc_class.devices, class_dev.class_list)
+#define for_each_rtc(rtc) \
+ class_for_each_container_of_device(&rtc_class, rtc, class_dev)
DEFINE_DEV_CLASS(rtc_class, "rtc");
diff --git a/drivers/watchdog/wd_core.c b/drivers/watchdog/wd_core.c
index 6e239edf612d..ee0e4ccb109b 100644
--- a/drivers/watchdog/wd_core.c
+++ b/drivers/watchdog/wd_core.c
@@ -11,7 +11,8 @@
#include <watchdog.h>
#include <restart.h>
-#define for_each_watchdog(wd) list_for_each_entry(wd, &watchdog_class.devices, dev.class_list)
+#define for_each_watchdog(wd) \
+ class_for_each_container_of_device(&watchdog_class, wd, dev)
DEFINE_DEV_CLASS(watchdog_class, "watchdog");
diff --git a/include/device.h b/include/device.h
index e0c130551f9a..8841197a0a56 100644
--- a/include/device.h
+++ b/include/device.h
@@ -134,6 +134,12 @@ int class_register_device(struct class *class, struct device *class_dev,
extern struct list_head class_list;
#define class_for_each_device(class, dev) list_for_each_entry((dev), &(class)->devices, class_list)
+#define class_first_device(class) \
+ list_first_entry_or_null(&(class)->devices, struct device, class_list)
+
+#define class_for_each_container_of_device(class, obj, member) \
+ list_for_each_entry((obj), &(class)->devices, member.class_list)
+
#define class_for_each(class) list_for_each_entry((class), &class_list, list)
struct device_alias {
diff --git a/include/net.h b/include/net.h
index a3c466784630..52f6a0fa97ad 100644
--- a/include/net.h
+++ b/include/net.h
@@ -597,7 +597,8 @@ void ifdown_edev(struct eth_device *edev);
int ifdown(const char *name);
void ifdown_all(void);
-#define for_each_netdev(netdev) list_for_each_entry(netdev, ð_class.devices, dev.class_list)
+#define for_each_netdev(netdev) \
+ class_for_each_container_of_device(ð_class, netdev, dev)
extern struct class eth_class;
--
2.39.5
More information about the barebox
mailing list