linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] [POWERPC] of: add alias helper functions.
@ 2008-02-04 16:16 Grant Likely
  2008-02-05  4:21 ` Stephen Rothwell
  0 siblings, 1 reply; 2+ messages in thread
From: Grant Likely @ 2008-02-04 16:16 UTC (permalink / raw)
  To: linuxppc-dev, sfr, david, davem

From: Grant Likely <grant.likely@secretlab.ca>

Add helper functions for translating back and forth between alias
properties and device tree nodes.

Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
---

 drivers/of/base.c  |   80 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/of.h |    2 +
 2 files changed, 82 insertions(+), 0 deletions(-)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index b306fef..54a7f2e 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -331,3 +331,83 @@ struct device_node *of_find_matching_node(struct device_node *from,
 	return np;
 }
 EXPORT_SYMBOL(of_find_matching_node);
+
+/**
+ *	of_find_node_by_alias - Find a node from an alias name
+ *	@alias:		Alias to decode
+ *
+ *	Returns a node pointer with refcount incremented.  Use of_node_put
+ *	on it when done.
+ */
+struct device_node *of_find_node_by_alias(const char *alias)
+{
+	struct device_node *np, *alias_np;
+	const char *path;
+
+	np = NULL;
+
+	/* First decode the alias into a path */
+	alias_np = of_find_node_by_path("/aliases");
+	if (!alias_np)
+		return NULL;
+
+	path = of_get_property(alias_np, alias, NULL);
+	if (!path)
+		goto exit;
+
+	/* Next find the node pointed to by the alias */
+	np = of_find_node_by_path(path);
+
+ exit:
+	of_node_put(alias_np);
+	return np;
+}
+
+/**
+ * of_node_alias - Return the alias for a node
+ * @np		Pointer to device node
+ * @prefix	Prefix string; if provided then this function will only
+ * 		match on properties which have the given prefix.
+ *
+ * returns the alias property name for the given node without the prefix
+ */
+const char *of_node_alias(struct device_node *np, const char *prefix)
+{
+	struct device_node *alias_np, *test_np;
+	struct property *pp;
+	int prefix_len;
+	const char *alias;
+
+	prefix_len = 0;
+	if (prefix)
+		prefix_len = strlen(prefix);
+
+	/* First decode the alias into a path */
+	alias_np = of_find_node_by_path("/aliases");
+	if (!alias_np)
+		return NULL;
+
+	/* Loop over the aliases looking for a match */
+	alias = NULL;
+	for (pp = alias_np->properties; pp != 0; pp = pp->next) {
+		/* Skip properties which don't begin with the prefix */
+		if (prefix && (strncmp(pp->name, prefix, prefix_len) != 0))
+			continue;
+
+		/* Skip properties which aren't a NULL terminated string */
+		if (memchr(pp->value, 0, pp->length) == NULL)
+			continue;
+
+		/* Find out what node the property points to and see if it
+		 * matches.  If so then we've found our alias */
+		test_np = of_find_node_by_path(pp->value);
+		if (test_np == np)
+			alias = pp->name + prefix_len;
+		of_node_put(test_np);
+		if (alias)
+			break;
+	}
+
+	of_node_put(alias_np);
+	return alias;
+}
diff --git a/include/linux/of.h b/include/linux/of.h
index b5f33ef..aae1570 100644
--- a/include/linux/of.h
+++ b/include/linux/of.h
@@ -68,5 +68,7 @@ extern int of_n_addr_cells(struct device_node *np);
 extern int of_n_size_cells(struct device_node *np);
 extern const struct of_device_id *of_match_node(
 	const struct of_device_id *matches, const struct device_node *node);
+extern struct device_node *of_find_node_by_alias(const char *alias);
+extern const char *of_node_alias(struct device_node *np, const char *prefix);
 
 #endif /* _LINUX_OF_H */

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] [POWERPC] of: add alias helper functions.
  2008-02-04 16:16 [PATCH] [POWERPC] of: add alias helper functions Grant Likely
@ 2008-02-05  4:21 ` Stephen Rothwell
  0 siblings, 0 replies; 2+ messages in thread
From: Stephen Rothwell @ 2008-02-05  4:21 UTC (permalink / raw)
  To: Grant Likely; +Cc: linuxppc-dev, davem, david

[-- Attachment #1: Type: text/plain, Size: 1409 bytes --]

Hi Grant,

On Mon, 04 Feb 2008 09:16:08 -0700 Grant Likely <grant.likely@secretlab.ca> wrote:
>
> From: Grant Likely <grant.likely@secretlab.ca>
> 
> Add helper functions for translating back and forth between alias
> properties and device tree nodes.

Do you have a use for this yet (I assume you do - it would be nice to
have a reason in the changelog)?

Overall looks ok, just a few comments?

Dave (Miller) is this useful for Sparc?

> +struct device_node *of_find_node_by_alias(const char *alias)
> +{
> +	struct device_node *np, *alias_np;
> +	const char *path;
> +
> +	np = NULL;

	struct device_node *np = NULL;
	struct device_node *alias_np;

> +const char *of_node_alias(struct device_node *np, const char *prefix)

> +	/* Loop over the aliases looking for a match */
> +	alias = NULL;
> +	for (pp = alias_np->properties; pp != 0; pp = pp->next) {
                                              ^
Use NULL for pointers (or just test "pp").

> +		if (test_np == np)
> +			alias = pp->name + prefix_len;
> +		of_node_put(test_np);
> +		if (alias)
> +			break;

This could be:
		of_node_put(test_np);
		if (test_np == np) {
			alias = pp->name + prefix_len;
			break;
		}

As you can still test for pointer equality after dropping the ref count.

-- 
Cheers,
Stephen Rothwell                    sfr@canb.auug.org.au
http://www.canb.auug.org.au/~sfr/

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-02-05  4:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-02-04 16:16 [PATCH] [POWERPC] of: add alias helper functions Grant Likely
2008-02-05  4:21 ` Stephen Rothwell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).