From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 1/3] base: class: add convenience helpers for iteration
Date: Mon, 14 Apr 2025 09:05:22 +0200 [thread overview]
Message-ID: <20250414070524.2784496-1-a.fatoum@pengutronix.de> (raw)
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, ð_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
next reply other threads:[~2025-04-14 7:06 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-14 7:05 Ahmad Fatoum [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250414070524.2784496-1-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox