From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 3/3] of: implement new of_property_sprintf
Date: Thu, 9 Dec 2021 11:57:08 +0100 [thread overview]
Message-ID: <20211209105708.3517684-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20211209105708.3517684-1-a.fatoum@pengutronix.de>
Board code may compute values for device tree properties and write them
as strings. Make this easier by adding a of_property_write_string
variant that does formatted output. This also saves an allocation,
because asprintf buffer is reused.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
drivers/of/base.c | 57 ++++++++++++++++++++++++++++++++++++++---------
include/of.h | 2 ++
2 files changed, 49 insertions(+), 10 deletions(-)
diff --git a/drivers/of/base.c b/drivers/of/base.c
index 065265ec9756..321022f2b391 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -2157,6 +2157,21 @@ struct device_node *of_new_node(struct device_node *parent, const char *name)
return node;
}
+static struct property *__of_new_property(struct device_node *node, const char *name,
+ void *data, int len)
+{
+ struct property *prop;
+
+ prop = xzalloc(sizeof(*prop));
+ prop->name = xstrdup(name);
+ prop->length = len;
+ prop->value = data;
+
+ list_add_tail(&prop->list, &node->properties);
+
+ return prop;
+}
+
/**
* of_new_property - Add a new property to a node
* @node: device node to which the property is added
@@ -2172,19 +2187,13 @@ struct device_node *of_new_node(struct device_node *parent, const char *name)
struct property *of_new_property(struct device_node *node, const char *name,
const void *data, int len)
{
- struct property *prop;
-
- prop = xzalloc(sizeof(*prop));
- prop->name = xstrdup(name);
- prop->length = len;
- prop->value = xzalloc(len);
+ char *buf;
+ buf = xzalloc(len);
if (data)
- memcpy(prop->value, data, len);
+ memcpy(buf, data, len);
- list_add_tail(&prop->list, &node->properties);
-
- return prop;
+ return __of_new_property(node, name, buf, len);
}
/**
@@ -2256,6 +2265,34 @@ int of_set_property(struct device_node *np, const char *name, const void *val, i
return 0;
}
+int of_property_sprintf(struct device_node *np,
+ const char *propname, const char *fmt, ...)
+{
+ struct property *pp;
+ struct va_format vaf;
+ char *buf = NULL;
+ va_list args;
+ int len;
+
+ if (!np)
+ return -ENOENT;
+
+ va_start(args, fmt);
+ vaf.fmt = fmt;
+ vaf.va = &args;
+ len = asprintf(&buf, "%pV", &vaf);
+ va_end(args);
+
+ if (len < 0)
+ return -ENOMEM;
+
+ pp = of_find_property(np, propname, NULL);
+ of_delete_property(pp);
+
+ __of_new_property(np, propname, buf, len);
+ return len;
+}
+
static int mem_bank_num;
int of_add_memory(struct device_node *node, bool dump)
diff --git a/include/of.h b/include/of.h
index 1b7392cdb3b9..e5df4cab4afc 100644
--- a/include/of.h
+++ b/include/of.h
@@ -241,6 +241,8 @@ extern int of_property_write_string(struct device_node *np, const char *propname
const char *value);
extern int of_property_write_strings(struct device_node *np, const char *propname,
...) __attribute__((__sentinel__));
+int of_property_sprintf(struct device_node *np, const char *propname, const char *fmt, ...)
+ __attribute__ ((format(__printf__, 3, 4)));
extern struct device_node *of_parse_phandle(const struct device_node *np,
const char *phandle_name,
--
2.30.2
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next prev parent reply other threads:[~2021-12-09 10:58 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-09 10:57 [PATCH 1/3] vsprintf: add support for printing raw buffers as hex (%*ph) Ahmad Fatoum
2021-12-09 10:57 ` [PATCH 2/3] common: add new PRINTF_FULL option Ahmad Fatoum
2021-12-09 10:57 ` Ahmad Fatoum [this message]
2021-12-15 20:16 ` [PATCH] fixup! of: implement new of_property_sprintf Ahmad Fatoum
2021-12-13 22:28 ` [PATCH 1/3] vsprintf: add support for printing raw buffers as hex (%*ph) Sascha Hauer
2021-12-14 6:57 ` Ahmad Fatoum
2021-12-14 7:27 ` Sascha Hauer
2022-01-03 10:40 ` Ahmad Fatoum
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=20211209105708.3517684-3-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