mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/3] base: class: add convenience helpers for iteration
@ 2025-04-14  7:05 Ahmad Fatoum
  2025-04-14  7:05 ` [PATCH 2/3] mux: port Linux multiplexer framework Ahmad Fatoum
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2025-04-14  7:05 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

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@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, &eth_class.devices, dev.class_list)
+#define for_each_netdev(netdev) \
+	class_for_each_container_of_device(&eth_class, netdev, dev)
 
 extern struct class eth_class;
 
-- 
2.39.5




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-04-14 11:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-04-14  7:05 [PATCH 1/3] base: class: add convenience helpers for iteration Ahmad Fatoum
2025-04-14  7:05 ` [PATCH 2/3] mux: port Linux multiplexer framework Ahmad Fatoum
2025-04-14  7:05 ` [PATCH 3/3] drivers: port Linux gpio-mux driver Ahmad Fatoum
2025-04-14 11:33 ` [PATCH 1/3] base: class: add convenience helpers for iteration Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox