diff options
author | Ahmad Fatoum <a.fatoum@pengutronix.de> | 2020-09-30 14:53:01 +0200 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2020-10-02 06:09:59 +0200 |
commit | 09cbc3fbdab5f3118062ceeefb0c1b043a75b3fb (patch) | |
tree | 8a1191179d41869146e25b03ec00ee0e4d2d36d2 /drivers | |
parent | f91786df4b06af94a2bc59a8290e84668aa49358 (diff) | |
download | barebox-09cbc3fbdab5f3118062ceeefb0c1b043a75b3fb.tar.gz barebox-09cbc3fbdab5f3118062ceeefb0c1b043a75b3fb.tar.xz |
of: implement of_property_write_strings for multiple strings
The current way to set a property with multiple values (e.g. compatible
strings) is to have
char properties[] = "st,stm32mp157c-dk2\0st,stm32mp157";
of_set_property(np, "compatible", properties, sizeof(properties), 1);
Add a new helper to make this easier at the cost of one runtime
reallocation:
of_property_write_strings(np, "compatible,
"st,stm32mp157c-dk2", "st,stm32mp157", NULL);
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/of/base.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/drivers/of/base.c b/drivers/of/base.c index 0a2632f963..63cc2e586c 100644 --- a/drivers/of/base.c +++ b/drivers/of/base.c @@ -23,6 +23,7 @@ #include <memory.h> #include <linux/sizes.h> #include <of_graph.h> +#include <string.h> #include <linux/ctype.h> #include <linux/amba/bus.h> #include <linux/err.h> @@ -1183,6 +1184,53 @@ int of_property_write_u64_array(struct device_node *np, } /** + * of_property_write_strings - Write strings to a property. If + * the property does not exist, it will be created and appended to the given + * device node. + * + * @np: device node to which the property value is to be written. + * @propname: name of the property to be written. + * @...: pointers to strings to write + * + * Search for a property in a device node and write a string to + * it. If the property does not exist, it will be created and appended to + * the device node. Returns 0 on success, -ENOMEM if the property or array + * of elements cannot be created, -EINVAL if no strings specified. + */ +int of_property_write_strings(struct device_node *np, + const char *propname, ...) +{ + const char *val; + char *buf = NULL, *next; + size_t len = 0; + va_list ap; + int ret = 0; + + va_start(ap, propname); + for (val = va_arg(ap, char *); val; val = va_arg(ap, char *)) + len += strlen(val) + 1; + va_end(ap); + + if (!len) + return -EINVAL; + + buf = malloc(len); + if (!buf) + return -ENOMEM; + + next = buf; + + va_start(ap, propname); + for (val = va_arg(ap, char *); val; val = va_arg(ap, char *)) + next = stpcpy(next, val) + 1; + va_end(ap); + + ret = of_set_property(np, propname, buf, len, 1); + free(buf); + return ret; +} + +/** * of_property_write_string - Write a string to a property. If * the property does not exist, it will be created and appended to the given * device node. |