RFC: for_each_u32/string_property_value

Stephen Warren swarren at nvidia.com
Thu Aug 25 09:02:33 EST 2011


Grant & all,

I was thinking of some macros for <linux/of.h> that iterate over all the
values in a property that's a list/array of values in a device-tree:

foo = <1 2 3 4>;
foo = "one", "two", "three", "four";

for_each_u32_property_value(node, "foo", value)
	printf("Value is: %u\n", value);
for_each_string_property_value(node, "foo", value)
	printf("Value is: %s\n", value);

Something like those below. Do these look OK, or are they a hugely hacky
abuse of the pre-processor?

(Alternatively, these could be inlines functions, but then would need to
emulate closures)

/*
const struct device_node *np;
const char *propname;
u32 value;
int tmplen;
const __be32 *tmpptr;
*/
#define for_each_u32_property_value(dn, prop_name, value, tmplen, tmpptr) \
	for (tmpptr = of_find_property(dn, prop_name, &tmplen); \
	     value = tmplen ? be32_to_cpup(*tmpptr) : 0, tmplen; \
	     tmpptr++, tmplen--)

/*
const struct device_node *np;
const char *propname;
const char *value;
int tmplen;
const char *tmpafter;
*/
#define for_each_string_property_value(dn, prop_name, value, tmplen, tmpafter) \
	for (value = of_find_property(dn, prop_name, &tmplen), \
	     tmpafter = value + tmplen, tmpafter[-1] = '\0'; \
	     value < tmpafter; \
	     value += strlen(value) + 1)

(Note I haven't even compile-tested these yet)

-- 
nvpublic



More information about the devicetree-discuss mailing list