]> git.hungrycats.org Git - linux/commitdiff
[PATCH] ppc64: New Open Firmware device tree API, from Nathan Lynch
authorAndrew Morton <akpm@osdl.org>
Mon, 19 Jan 2004 02:10:48 +0000 (18:10 -0800)
committerLinus Torvalds <torvalds@home.osdl.org>
Mon, 19 Jan 2004 02:10:48 +0000 (18:10 -0800)
From: Anton Blanchard <anton@samba.org>

This is an adaptation of the new Open Firmware device tree traversal API from
ppc32, originally written by Benjamin Herrenschmidt.  This patch is against
2.6.0-test3, but should apply ok to the latest 2.5 ameslab tree.

These functions are meant to be SMP-safe alternatives to the current set of
query/traversal routines (find_devices, find_type_devices, et al).

arch/ppc64/kernel/prom.c
include/asm-ppc64/prom.h

index 31ba21d12fc1e893db6cbc9e990ecc2588dea487..35152838093208f9a974368c7c1fc5d228484263 100644 (file)
@@ -2000,6 +2000,185 @@ find_path_device(const char *path)
        return NULL;
 }
 
+/*******
+ *
+ * New implementation of the OF "find" APIs, return a refcounted
+ * object, call of_node_put() when done. Currently, still lacks
+ * locking as old implementation, this is being done for ppc64.
+ *
+ * Note that property management will need some locking as well,
+ * this isn't dealt with yet.
+ *
+ *******/
+
+/**
+ *     of_find_node_by_name - Find a node by its "name" property
+ *     @from:  The node to start searching from or NULL, the node
+ *             you pass will not be searched, only the next one
+ *             will; typically, you pass what the previous call
+ *             returned. of_node_put() will be called on it
+ *     @name:  The name string to match against
+ *
+ *     Returns a node pointer with refcount incremented, use
+ *     of_node_put() on it when done.
+ */
+struct device_node *of_find_node_by_name(struct device_node *from,
+       const char *name)
+{
+       struct device_node *np = from ? from->allnext : allnodes;
+
+       for (; np != 0; np = np->allnext)
+               if (np->name != 0 && strcasecmp(np->name, name) == 0)
+                       break;
+       if (from)
+               of_node_put(from);
+       return of_node_get(np);
+}
+
+/**
+ *     of_find_node_by_type - Find a node by its "device_type" property
+ *     @from:  The node to start searching from or NULL, the node
+ *             you pass will not be searched, only the next one
+ *             will; typically, you pass what the previous call
+ *             returned. of_node_put() will be called on it
+ *     @name:  The type string to match against
+ *
+ *     Returns a node pointer with refcount incremented, use
+ *     of_node_put() on it when done.
+ */
+struct device_node *of_find_node_by_type(struct device_node *from,
+       const char *type)
+{
+       struct device_node *np = from ? from->allnext : allnodes;
+
+       for (; np != 0; np = np->allnext)
+               if (np->type != 0 && strcasecmp(np->type, type) == 0)
+                       break;
+       if (from)
+               of_node_put(from);
+       return of_node_get(np);
+}
+
+/**
+ *     of_find_compatible_node - Find a node based on type and one of the
+ *                                tokens in its "compatible" property
+ *     @from:          The node to start searching from or NULL, the node
+ *                     you pass will not be searched, only the next one
+ *                     will; typically, you pass what the previous call
+ *                     returned. of_node_put() will be called on it
+ *     @type:          The type string to match "device_type" or NULL to ignore
+ *     @compatible:    The string to match to one of the tokens in the device
+ *                     "compatible" list.
+ *
+ *     Returns a node pointer with refcount incremented, use
+ *     of_node_put() on it when done.
+ */
+struct device_node *of_find_compatible_node(struct device_node *from,
+       const char *type, const char *compatible)
+{
+       struct device_node *np = from ? from->allnext : allnodes;
+
+       for (; np != 0; np = np->allnext) {
+               if (type != NULL
+                   && !(np->type != 0 && strcasecmp(np->type, type) == 0))
+                       continue;
+               if (device_is_compatible(np, compatible))
+                       break;
+       }
+       if (from)
+               of_node_put(from);
+       return of_node_get(np);
+}
+
+/**
+ *     of_find_node_by_path - Find a node matching a full OF path
+ *     @path:  The full path to match
+ *
+ *     Returns a node pointer with refcount incremented, use
+ *     of_node_put() on it when done.
+ */
+struct device_node *of_find_node_by_path(const char *path)
+{
+       struct device_node *np = allnodes;
+
+       for (; np != 0; np = np->allnext)
+               if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
+                       break;
+       return of_node_get(np);
+}
+
+/**
+ *     of_find_all_nodes - Get next node in global list
+ *     @prev:  Previous node or NULL to start iteration
+ *             of_node_put() will be called on it
+ *
+ *     Returns a node pointer with refcount incremented, use
+ *     of_node_put() on it when done.
+ */
+struct device_node *of_find_all_nodes(struct device_node *prev)
+{
+       struct device_node *np = prev ? prev->allnext : allnodes;
+
+       if (prev)
+               of_node_put(prev);
+       return of_node_get(np);
+}
+
+/**
+ *     of_get_parent - Get a node's parent if any
+ *     @node:  Node to get parent
+ *
+ *     Returns a node pointer with refcount incremented, use
+ *     of_node_put() on it when done.
+ */
+struct device_node *of_get_parent(const struct device_node *node)
+{
+       return node ? of_node_get(node->parent) : NULL;
+}
+
+/**
+ *     of_get_next_child - Iterate a node childs
+ *     @node:  parent node
+ *     @prev:  previous child of the parent node, or NULL to get first
+ *
+ *     Returns a node pointer with refcount incremented, use
+ *     of_node_put() on it when done.
+ */
+struct device_node *of_get_next_child(const struct device_node *node,
+       struct device_node *prev)
+{
+       struct device_node *next = prev ? prev->sibling : node->child;
+
+       for (; next != 0; next = next->sibling)
+               if (of_node_get(next))
+                       break;
+       if (prev)
+               of_node_put(prev);
+       return next;
+}
+
+/**
+ *     of_node_get - Increment refcount of a node
+ *     @node:  Node to inc refcount, NULL is supported to
+ *             simplify writing of callers
+ *
+ *     Returns the node itself or NULL if gone.
+ */
+struct device_node *of_node_get(struct device_node *node)
+{
+       return node;
+}
+
+/**
+ *     of_node_put - Decrement refcount of a node
+ *     @node:  Node to dec refcount, NULL is supported to
+ *             simplify writing of callers
+ *
+ */
+void of_node_put(struct device_node *node)
+{
+}
+
 /*
  * Find the device_node with a given phandle.
  */
index 9e8825036073ef278395969ec8b14366c8f4f22a..7299fc2a73c2eeba096e5fb0c038822626e5f516 100644 (file)
@@ -172,19 +172,36 @@ extern struct prom_t prom;
 
 extern int boot_cpuid;
 
-/* Prototypes */
-extern void abort(void);
-extern unsigned long prom_init(unsigned long, unsigned long, unsigned long,
-    unsigned long, unsigned long);
-extern void prom_print(const char *msg);
-extern void relocate_nodes(void);
-extern void finish_device_tree(void);
+/* OBSOLETE: Old stlye node lookup */
 extern struct device_node *find_devices(const char *name);
 extern struct device_node *find_type_devices(const char *type);
 extern struct device_node *find_path_device(const char *path);
 extern struct device_node *find_compatible_devices(const char *type,
                                                   const char *compat);
 extern struct device_node *find_all_nodes(void);
+
+/* New style node lookup */
+extern struct device_node *of_find_node_by_name(struct device_node *from,
+       const char *name);
+extern struct device_node *of_find_node_by_type(struct device_node *from,
+       const char *type);
+extern struct device_node *of_find_compatible_node(struct device_node *from,
+       const char *type, const char *compat);
+extern struct device_node *of_find_node_by_path(const char *path);
+extern struct device_node *of_find_all_nodes(struct device_node *prev);
+extern struct device_node *of_get_parent(const struct device_node *node);
+extern struct device_node *of_get_next_child(const struct device_node *node,
+                                            struct device_node *prev);
+extern struct device_node *of_node_get(struct device_node *node);
+extern void of_node_put(struct device_node *node);
+
+/* Other Prototypes */
+extern void abort(void);
+extern unsigned long prom_init(unsigned long, unsigned long, unsigned long,
+       unsigned long, unsigned long);
+extern void prom_print(const char *msg);
+extern void relocate_nodes(void);
+extern void finish_device_tree(void);
 extern int device_is_compatible(struct device_node *device, const char *);
 extern int machine_is_compatible(const char *compat);
 extern unsigned char *get_property(struct device_node *node, const char *name,