]> err.no Git - linux-2.6/blobdiff - arch/powerpc/boot/flatdevtree.c
Merge branch 'hwmon-for-linus' of git://jdelvare.pck.nerim.net/jdelvare-2.6
[linux-2.6] / arch / powerpc / boot / flatdevtree.c
index 88b178a77e875d27869d02626e181630c0801647..b732644788db799f83c6c90921ac14cd5f4e8069 100644 (file)
@@ -820,34 +820,98 @@ int ft_get_prop(struct ft_cxt *cxt, const void *phandle, const char *propname,
        return -1;
 }
 
+void *__ft_find_node_by_prop_value(struct ft_cxt *cxt, void *prev,
+                                   const char *propname, const char *propval,
+                                   unsigned int proplen)
+{
+       struct ft_atom atom;
+       char *p = ft_root_node(cxt);
+       char *next;
+       int past_prev = prev ? 0 : 1;
+       int depth = -1;
+
+       while ((next = ft_next(cxt, p, &atom)) != NULL) {
+               const void *data;
+               unsigned int size;
+
+               switch (atom.tag) {
+               case OF_DT_BEGIN_NODE:
+                       depth++;
+
+                       if (prev == p) {
+                               past_prev = 1;
+                               break;
+                       }
+
+                       if (!past_prev || depth < 1)
+                               break;
+
+                       data = __ft_get_prop(cxt, p, propname, &size);
+                       if (!data || size != proplen)
+                               break;
+                       if (memcmp(data, propval, size))
+                               break;
+
+                       return p;
+
+               case OF_DT_END_NODE:
+                       if (depth-- == 0)
+                               return NULL;
+
+                       break;
+               }
+
+               p = next;
+       }
+
+       return NULL;
+}
+
+void *ft_find_node_by_prop_value(struct ft_cxt *cxt, const void *prev,
+                                 const char *propname, const char *propval,
+                                 int proplen)
+{
+       void *node = NULL;
+
+       if (prev) {
+               node = ft_node_ph2node(cxt, prev);
+
+               if (!node)
+                       return NULL;
+       }
+
+       node = __ft_find_node_by_prop_value(cxt, node, propname,
+                                           propval, proplen);
+       return ft_get_phandle(cxt, node);
+}
+
 int ft_set_prop(struct ft_cxt *cxt, const void *phandle, const char *propname,
                const void *buf, const unsigned int buflen)
 {
        struct ft_atom atom;
        void *node;
        char *p, *next;
-       int nextra, depth;
+       int nextra;
 
        node = ft_node_ph2node(cxt, phandle);
        if (node == NULL)
                return -1;
 
-       depth = 0;
-       p = node;
+       next = ft_next(cxt, node, &atom);
+       if (atom.tag != OF_DT_BEGIN_NODE)
+               /* phandle didn't point to a node */
+               return -1;
+       p = next;
 
        while ((next = ft_next(cxt, p, &atom)) != NULL) {
                switch (atom.tag) {
-               case OF_DT_BEGIN_NODE:
-                       ++depth;
-                       break;
+               case OF_DT_BEGIN_NODE: /* properties must go before subnodes */
                case OF_DT_END_NODE:
-                       if (--depth > 0)
-                               break;
                        /* haven't found the property, insert here */
                        cxt->p = p;
                        return ft_prop(cxt, propname, buf, buflen);
                case OF_DT_PROP:
-                       if ((depth != 1) || strcmp(atom.name, propname))
+                       if (strcmp(atom.name, propname))
                                break;
                        /* found an existing property, overwrite it */
                        nextra = _ALIGN(buflen, 4) - _ALIGN(atom.size, 4);
@@ -896,19 +960,26 @@ int ft_del_prop(struct ft_cxt *cxt, const void *phandle, const char *propname)
        return -1;
 }
 
-void *ft_create_node(struct ft_cxt *cxt, const void *parent, const char *path)
+void *ft_create_node(struct ft_cxt *cxt, const void *parent, const char *name)
 {
        struct ft_atom atom;
        char *p, *next;
        int depth = 0;
 
-       p = ft_root_node(cxt);
+       if (parent) {
+               p = ft_node_ph2node(cxt, parent);
+               if (!p)
+                       return NULL;
+       } else {
+               p = ft_root_node(cxt);
+       }
+
        while ((next = ft_next(cxt, p, &atom)) != NULL) {
                switch (atom.tag) {
                case OF_DT_BEGIN_NODE:
                        ++depth;
-                       if (depth == 1 && strcmp(atom.name, path) == 0)
-                               /* duplicate node path, return error */
+                       if (depth == 1 && strcmp(atom.name, name) == 0)
+                               /* duplicate node name, return error */
                                return NULL;
                        break;
                case OF_DT_END_NODE:
@@ -917,7 +988,7 @@ void *ft_create_node(struct ft_cxt *cxt, const void *parent, const char *path)
                                break;
                        /* end of node, insert here */
                        cxt->p = p;
-                       ft_begin_node(cxt, path);
+                       ft_begin_node(cxt, name);
                        ft_end_node(cxt);
                        return p;
                }