mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 1/3] of: define macro for maximum of_device_is_compatible return value
Date: Tue,  5 Dec 2023 12:36:16 +0100	[thread overview]
Message-ID: <20231205113618.3966168-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20231205113618.3966168-1-a.fatoum@pengutronix.de>

of_device_is_compatible() will return zero on mismatch and a positive
value up to and including (INT_MAX / 2) on match. Knowledge of the upper
bound can be useful to callers, because if inside a loop a maximum score
was achieved, there's usually no point in continuing the search.

Therefore add a macro for INT_MAX / 2 and document it for potential
users.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/of/base.c | 6 ++++--
 include/of.h      | 4 ++++
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 463db1058ae9..8a42fcee1d40 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -546,7 +546,9 @@ struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
 EXPORT_SYMBOL(of_get_cpu_node);
 
 /** Checks if the given "compat" string matches one of the strings in
- * the device's "compatible" property
+ * the device's "compatible" property. Returns 0 on mismatch and a
+ * positive score on match with the maximum being OF_DEVICE_COMPATIBLE_MAX_SCORE,
+ * which is only returned if the first compatible matched.
  */
 int of_device_is_compatible(const struct device_node *device,
 		const char *compat)
@@ -559,7 +561,7 @@ int of_device_is_compatible(const struct device_node *device,
 	for (cp = of_prop_next_string(prop, NULL); cp;
 	     cp = of_prop_next_string(prop, cp), index++) {
 		if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
-			score = INT_MAX/2 - (index << 2);
+			score = OF_DEVICE_COMPATIBLE_MAX_SCORE - (index << 2);
 			break;
 		}
 	}
diff --git a/include/of.h b/include/of.h
index 7d1df462d8cf..19b8e7c038a4 100644
--- a/include/of.h
+++ b/include/of.h
@@ -5,6 +5,7 @@
 #include <fdt.h>
 #include <errno.h>
 #include <linux/types.h>
+#include <linux/limits.h>
 #include <linux/list.h>
 #include <linux/err.h>
 #include <asm/byteorder.h>
@@ -124,6 +125,9 @@ int of_fixup_reserved_memory(struct device_node *node, void *data);
 
 struct cdev;
 
+/* Maximum score returned by of_device_is_compatible() */
+#define OF_DEVICE_COMPATIBLE_MAX_SCORE	(INT_MAX / 2)
+
 #ifdef CONFIG_OFTREE
 extern struct device_node *of_read_file(const char *filename);
 extern struct of_reserve_map *of_get_reserve_map(void);
-- 
2.39.2




  reply	other threads:[~2023-12-05 11:37 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-05 11:36 [PATCH 0/3] FIT: match best configuration when multiple are compatible Ahmad Fatoum
2023-12-05 11:36 ` Ahmad Fatoum [this message]
2023-12-05 11:36 ` [PATCH 2/3] of: early exit of_match_node if no better matches are possible Ahmad Fatoum
2023-12-05 11:36 ` [PATCH 3/3] FIT: match best configuration when multiple are compatible Ahmad Fatoum
2023-12-13  6:44 ` [PATCH 0/3] " 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=20231205113618.3966168-2-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