785e9cc1b20797ab0925b0c3cd16ef0f1f17026b
[safe/jmp/linux-2.6] / drivers / of / base.c
1 /*
2  * Procedures for creating, accessing and interpreting the device tree.
3  *
4  * Paul Mackerras       August 1996.
5  * Copyright (C) 1996-2005 Paul Mackerras.
6  *
7  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8  *    {engebret|bergner}@us.ibm.com
9  *
10  *  Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
11  *
12  *  Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
13  *  Grant Likely.
14  *
15  *      This program is free software; you can redistribute it and/or
16  *      modify it under the terms of the GNU General Public License
17  *      as published by the Free Software Foundation; either version
18  *      2 of the License, or (at your option) any later version.
19  */
20 #include <linux/module.h>
21 #include <linux/of.h>
22 #include <linux/spinlock.h>
23
24 struct device_node *allnodes;
25
26 /* use when traversing tree through the allnext, child, sibling,
27  * or parent members of struct device_node.
28  */
29 DEFINE_RWLOCK(devtree_lock);
30
31 int of_n_addr_cells(struct device_node *np)
32 {
33         const int *ip;
34
35         do {
36                 if (np->parent)
37                         np = np->parent;
38                 ip = of_get_property(np, "#address-cells", NULL);
39                 if (ip)
40                         return *ip;
41         } while (np->parent);
42         /* No #address-cells property for the root node */
43         return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
44 }
45 EXPORT_SYMBOL(of_n_addr_cells);
46
47 int of_n_size_cells(struct device_node *np)
48 {
49         const int *ip;
50
51         do {
52                 if (np->parent)
53                         np = np->parent;
54                 ip = of_get_property(np, "#size-cells", NULL);
55                 if (ip)
56                         return *ip;
57         } while (np->parent);
58         /* No #size-cells property for the root node */
59         return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
60 }
61 EXPORT_SYMBOL(of_n_size_cells);
62
63 #if !defined(CONFIG_SPARC)   /* SPARC doesn't do ref counting (yet) */
64 /**
65  *      of_node_get - Increment refcount of a node
66  *      @node:  Node to inc refcount, NULL is supported to
67  *              simplify writing of callers
68  *
69  *      Returns node.
70  */
71 struct device_node *of_node_get(struct device_node *node)
72 {
73         if (node)
74                 kref_get(&node->kref);
75         return node;
76 }
77 EXPORT_SYMBOL(of_node_get);
78
79 static inline struct device_node *kref_to_device_node(struct kref *kref)
80 {
81         return container_of(kref, struct device_node, kref);
82 }
83
84 /**
85  *      of_node_release - release a dynamically allocated node
86  *      @kref:  kref element of the node to be released
87  *
88  *      In of_node_put() this function is passed to kref_put()
89  *      as the destructor.
90  */
91 static void of_node_release(struct kref *kref)
92 {
93         struct device_node *node = kref_to_device_node(kref);
94         struct property *prop = node->properties;
95
96         /* We should never be releasing nodes that haven't been detached. */
97         if (!of_node_check_flag(node, OF_DETACHED)) {
98                 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
99                 dump_stack();
100                 kref_init(&node->kref);
101                 return;
102         }
103
104         if (!of_node_check_flag(node, OF_DYNAMIC))
105                 return;
106
107         while (prop) {
108                 struct property *next = prop->next;
109                 kfree(prop->name);
110                 kfree(prop->value);
111                 kfree(prop);
112                 prop = next;
113
114                 if (!prop) {
115                         prop = node->deadprops;
116                         node->deadprops = NULL;
117                 }
118         }
119         kfree(node->full_name);
120         kfree(node->data);
121         kfree(node);
122 }
123
124 /**
125  *      of_node_put - Decrement refcount of a node
126  *      @node:  Node to dec refcount, NULL is supported to
127  *              simplify writing of callers
128  *
129  */
130 void of_node_put(struct device_node *node)
131 {
132         if (node)
133                 kref_put(&node->kref, of_node_release);
134 }
135 EXPORT_SYMBOL(of_node_put);
136 #endif /* !CONFIG_SPARC */
137
138 struct property *of_find_property(const struct device_node *np,
139                                   const char *name,
140                                   int *lenp)
141 {
142         struct property *pp;
143
144         if (!np)
145                 return NULL;
146
147         read_lock(&devtree_lock);
148         for (pp = np->properties; pp != 0; pp = pp->next) {
149                 if (of_prop_cmp(pp->name, name) == 0) {
150                         if (lenp != 0)
151                                 *lenp = pp->length;
152                         break;
153                 }
154         }
155         read_unlock(&devtree_lock);
156
157         return pp;
158 }
159 EXPORT_SYMBOL(of_find_property);
160
161 /**
162  * of_find_all_nodes - Get next node in global list
163  * @prev:       Previous node or NULL to start iteration
164  *              of_node_put() will be called on it
165  *
166  * Returns a node pointer with refcount incremented, use
167  * of_node_put() on it when done.
168  */
169 struct device_node *of_find_all_nodes(struct device_node *prev)
170 {
171         struct device_node *np;
172
173         read_lock(&devtree_lock);
174         np = prev ? prev->allnext : allnodes;
175         for (; np != NULL; np = np->allnext)
176                 if (of_node_get(np))
177                         break;
178         of_node_put(prev);
179         read_unlock(&devtree_lock);
180         return np;
181 }
182 EXPORT_SYMBOL(of_find_all_nodes);
183
184 /*
185  * Find a property with a given name for a given node
186  * and return the value.
187  */
188 const void *of_get_property(const struct device_node *np, const char *name,
189                          int *lenp)
190 {
191         struct property *pp = of_find_property(np, name, lenp);
192
193         return pp ? pp->value : NULL;
194 }
195 EXPORT_SYMBOL(of_get_property);
196
197 /** Checks if the given "compat" string matches one of the strings in
198  * the device's "compatible" property
199  */
200 int of_device_is_compatible(const struct device_node *device,
201                 const char *compat)
202 {
203         const char* cp;
204         int cplen, l;
205
206         cp = of_get_property(device, "compatible", &cplen);
207         if (cp == NULL)
208                 return 0;
209         while (cplen > 0) {
210                 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
211                         return 1;
212                 l = strlen(cp) + 1;
213                 cp += l;
214                 cplen -= l;
215         }
216
217         return 0;
218 }
219 EXPORT_SYMBOL(of_device_is_compatible);
220
221 /**
222  * machine_is_compatible - Test root of device tree for a given compatible value
223  * @compat: compatible string to look for in root node's compatible property.
224  *
225  * Returns true if the root node has the given value in its
226  * compatible property.
227  */
228 int machine_is_compatible(const char *compat)
229 {
230         struct device_node *root;
231         int rc = 0;
232
233         root = of_find_node_by_path("/");
234         if (root) {
235                 rc = of_device_is_compatible(root, compat);
236                 of_node_put(root);
237         }
238         return rc;
239 }
240 EXPORT_SYMBOL(machine_is_compatible);
241
242 /**
243  *  of_device_is_available - check if a device is available for use
244  *
245  *  @device: Node to check for availability
246  *
247  *  Returns 1 if the status property is absent or set to "okay" or "ok",
248  *  0 otherwise
249  */
250 int of_device_is_available(const struct device_node *device)
251 {
252         const char *status;
253         int statlen;
254
255         status = of_get_property(device, "status", &statlen);
256         if (status == NULL)
257                 return 1;
258
259         if (statlen > 0) {
260                 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
261                         return 1;
262         }
263
264         return 0;
265 }
266 EXPORT_SYMBOL(of_device_is_available);
267
268 /**
269  *      of_get_parent - Get a node's parent if any
270  *      @node:  Node to get parent
271  *
272  *      Returns a node pointer with refcount incremented, use
273  *      of_node_put() on it when done.
274  */
275 struct device_node *of_get_parent(const struct device_node *node)
276 {
277         struct device_node *np;
278
279         if (!node)
280                 return NULL;
281
282         read_lock(&devtree_lock);
283         np = of_node_get(node->parent);
284         read_unlock(&devtree_lock);
285         return np;
286 }
287 EXPORT_SYMBOL(of_get_parent);
288
289 /**
290  *      of_get_next_parent - Iterate to a node's parent
291  *      @node:  Node to get parent of
292  *
293  *      This is like of_get_parent() except that it drops the
294  *      refcount on the passed node, making it suitable for iterating
295  *      through a node's parents.
296  *
297  *      Returns a node pointer with refcount incremented, use
298  *      of_node_put() on it when done.
299  */
300 struct device_node *of_get_next_parent(struct device_node *node)
301 {
302         struct device_node *parent;
303
304         if (!node)
305                 return NULL;
306
307         read_lock(&devtree_lock);
308         parent = of_node_get(node->parent);
309         of_node_put(node);
310         read_unlock(&devtree_lock);
311         return parent;
312 }
313
314 /**
315  *      of_get_next_child - Iterate a node childs
316  *      @node:  parent node
317  *      @prev:  previous child of the parent node, or NULL to get first
318  *
319  *      Returns a node pointer with refcount incremented, use
320  *      of_node_put() on it when done.
321  */
322 struct device_node *of_get_next_child(const struct device_node *node,
323         struct device_node *prev)
324 {
325         struct device_node *next;
326
327         read_lock(&devtree_lock);
328         next = prev ? prev->sibling : node->child;
329         for (; next; next = next->sibling)
330                 if (of_node_get(next))
331                         break;
332         of_node_put(prev);
333         read_unlock(&devtree_lock);
334         return next;
335 }
336 EXPORT_SYMBOL(of_get_next_child);
337
338 /**
339  *      of_find_node_by_path - Find a node matching a full OF path
340  *      @path:  The full path to match
341  *
342  *      Returns a node pointer with refcount incremented, use
343  *      of_node_put() on it when done.
344  */
345 struct device_node *of_find_node_by_path(const char *path)
346 {
347         struct device_node *np = allnodes;
348
349         read_lock(&devtree_lock);
350         for (; np; np = np->allnext) {
351                 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
352                     && of_node_get(np))
353                         break;
354         }
355         read_unlock(&devtree_lock);
356         return np;
357 }
358 EXPORT_SYMBOL(of_find_node_by_path);
359
360 /**
361  *      of_find_node_by_name - Find a node by its "name" property
362  *      @from:  The node to start searching from or NULL, the node
363  *              you pass will not be searched, only the next one
364  *              will; typically, you pass what the previous call
365  *              returned. of_node_put() will be called on it
366  *      @name:  The name string to match against
367  *
368  *      Returns a node pointer with refcount incremented, use
369  *      of_node_put() on it when done.
370  */
371 struct device_node *of_find_node_by_name(struct device_node *from,
372         const char *name)
373 {
374         struct device_node *np;
375
376         read_lock(&devtree_lock);
377         np = from ? from->allnext : allnodes;
378         for (; np; np = np->allnext)
379                 if (np->name && (of_node_cmp(np->name, name) == 0)
380                     && of_node_get(np))
381                         break;
382         of_node_put(from);
383         read_unlock(&devtree_lock);
384         return np;
385 }
386 EXPORT_SYMBOL(of_find_node_by_name);
387
388 /**
389  *      of_find_node_by_type - Find a node by its "device_type" property
390  *      @from:  The node to start searching from, or NULL to start searching
391  *              the entire device tree. The node you pass will not be
392  *              searched, only the next one will; typically, you pass
393  *              what the previous call returned. of_node_put() will be
394  *              called on from for you.
395  *      @type:  The type string to match against
396  *
397  *      Returns a node pointer with refcount incremented, use
398  *      of_node_put() on it when done.
399  */
400 struct device_node *of_find_node_by_type(struct device_node *from,
401         const char *type)
402 {
403         struct device_node *np;
404
405         read_lock(&devtree_lock);
406         np = from ? from->allnext : allnodes;
407         for (; np; np = np->allnext)
408                 if (np->type && (of_node_cmp(np->type, type) == 0)
409                     && of_node_get(np))
410                         break;
411         of_node_put(from);
412         read_unlock(&devtree_lock);
413         return np;
414 }
415 EXPORT_SYMBOL(of_find_node_by_type);
416
417 /**
418  *      of_find_compatible_node - Find a node based on type and one of the
419  *                                tokens in its "compatible" property
420  *      @from:          The node to start searching from or NULL, the node
421  *                      you pass will not be searched, only the next one
422  *                      will; typically, you pass what the previous call
423  *                      returned. of_node_put() will be called on it
424  *      @type:          The type string to match "device_type" or NULL to ignore
425  *      @compatible:    The string to match to one of the tokens in the device
426  *                      "compatible" list.
427  *
428  *      Returns a node pointer with refcount incremented, use
429  *      of_node_put() on it when done.
430  */
431 struct device_node *of_find_compatible_node(struct device_node *from,
432         const char *type, const char *compatible)
433 {
434         struct device_node *np;
435
436         read_lock(&devtree_lock);
437         np = from ? from->allnext : allnodes;
438         for (; np; np = np->allnext) {
439                 if (type
440                     && !(np->type && (of_node_cmp(np->type, type) == 0)))
441                         continue;
442                 if (of_device_is_compatible(np, compatible) && of_node_get(np))
443                         break;
444         }
445         of_node_put(from);
446         read_unlock(&devtree_lock);
447         return np;
448 }
449 EXPORT_SYMBOL(of_find_compatible_node);
450
451 /**
452  *      of_find_node_with_property - Find a node which has a property with
453  *                                   the given name.
454  *      @from:          The node to start searching from or NULL, the node
455  *                      you pass will not be searched, only the next one
456  *                      will; typically, you pass what the previous call
457  *                      returned. of_node_put() will be called on it
458  *      @prop_name:     The name of the property to look for.
459  *
460  *      Returns a node pointer with refcount incremented, use
461  *      of_node_put() on it when done.
462  */
463 struct device_node *of_find_node_with_property(struct device_node *from,
464         const char *prop_name)
465 {
466         struct device_node *np;
467         struct property *pp;
468
469         read_lock(&devtree_lock);
470         np = from ? from->allnext : allnodes;
471         for (; np; np = np->allnext) {
472                 for (pp = np->properties; pp != 0; pp = pp->next) {
473                         if (of_prop_cmp(pp->name, prop_name) == 0) {
474                                 of_node_get(np);
475                                 goto out;
476                         }
477                 }
478         }
479 out:
480         of_node_put(from);
481         read_unlock(&devtree_lock);
482         return np;
483 }
484 EXPORT_SYMBOL(of_find_node_with_property);
485
486 /**
487  * of_match_node - Tell if an device_node has a matching of_match structure
488  *      @matches:       array of of device match structures to search in
489  *      @node:          the of device structure to match against
490  *
491  *      Low level utility function used by device matching.
492  */
493 const struct of_device_id *of_match_node(const struct of_device_id *matches,
494                                          const struct device_node *node)
495 {
496         while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
497                 int match = 1;
498                 if (matches->name[0])
499                         match &= node->name
500                                 && !strcmp(matches->name, node->name);
501                 if (matches->type[0])
502                         match &= node->type
503                                 && !strcmp(matches->type, node->type);
504                 if (matches->compatible[0])
505                         match &= of_device_is_compatible(node,
506                                                 matches->compatible);
507                 if (match)
508                         return matches;
509                 matches++;
510         }
511         return NULL;
512 }
513 EXPORT_SYMBOL(of_match_node);
514
515 /**
516  *      of_find_matching_node - Find a node based on an of_device_id match
517  *                              table.
518  *      @from:          The node to start searching from or NULL, the node
519  *                      you pass will not be searched, only the next one
520  *                      will; typically, you pass what the previous call
521  *                      returned. of_node_put() will be called on it
522  *      @matches:       array of of device match structures to search in
523  *
524  *      Returns a node pointer with refcount incremented, use
525  *      of_node_put() on it when done.
526  */
527 struct device_node *of_find_matching_node(struct device_node *from,
528                                           const struct of_device_id *matches)
529 {
530         struct device_node *np;
531
532         read_lock(&devtree_lock);
533         np = from ? from->allnext : allnodes;
534         for (; np; np = np->allnext) {
535                 if (of_match_node(matches, np) && of_node_get(np))
536                         break;
537         }
538         of_node_put(from);
539         read_unlock(&devtree_lock);
540         return np;
541 }
542 EXPORT_SYMBOL(of_find_matching_node);
543
544 /**
545  * of_modalias_table: Table of explicit compatible ==> modalias mappings
546  *
547  * This table allows particulare compatible property values to be mapped
548  * to modalias strings.  This is useful for busses which do not directly
549  * understand the OF device tree but are populated based on data contained
550  * within the device tree.  SPI and I2C are the two current users of this
551  * table.
552  *
553  * In most cases, devices do not need to be listed in this table because
554  * the modalias value can be derived directly from the compatible table.
555  * However, if for any reason a value cannot be derived, then this table
556  * provides a method to override the implicit derivation.
557  *
558  * At the moment, a single table is used for all bus types because it is
559  * assumed that the data size is small and that the compatible values
560  * should already be distinct enough to differentiate between SPI, I2C
561  * and other devices.
562  */
563 struct of_modalias_table {
564         char *of_device;
565         char *modalias;
566 };
567 static struct of_modalias_table of_modalias_table[] = {
568         { "fsl,mcu-mpc8349emitx", "mcu-mpc8349emitx" },
569         { "mmc-spi-slot", "mmc_spi" },
570 };
571
572 /**
573  * of_modalias_node - Lookup appropriate modalias for a device node
574  * @node:       pointer to a device tree node
575  * @modalias:   Pointer to buffer that modalias value will be copied into
576  * @len:        Length of modalias value
577  *
578  * Based on the value of the compatible property, this routine will determine
579  * an appropriate modalias value for a particular device tree node.  Two
580  * separate methods are attempted to derive a modalias value.
581  *
582  * First method is to lookup the compatible value in of_modalias_table.
583  * Second is to strip off the manufacturer prefix from the first
584  * compatible entry and use the remainder as modalias
585  *
586  * This routine returns 0 on success
587  */
588 int of_modalias_node(struct device_node *node, char *modalias, int len)
589 {
590         int i, cplen;
591         const char *compatible;
592         const char *p;
593
594         /* 1. search for exception list entry */
595         for (i = 0; i < ARRAY_SIZE(of_modalias_table); i++) {
596                 compatible = of_modalias_table[i].of_device;
597                 if (!of_device_is_compatible(node, compatible))
598                         continue;
599                 strlcpy(modalias, of_modalias_table[i].modalias, len);
600                 return 0;
601         }
602
603         compatible = of_get_property(node, "compatible", &cplen);
604         if (!compatible)
605                 return -ENODEV;
606
607         /* 2. take first compatible entry and strip manufacturer */
608         p = strchr(compatible, ',');
609         if (!p)
610                 return -ENODEV;
611         p++;
612         strlcpy(modalias, p, len);
613         return 0;
614 }
615 EXPORT_SYMBOL_GPL(of_modalias_node);
616
617 /**
618  * of_find_node_by_phandle - Find a node given a phandle
619  * @handle:     phandle of the node to find
620  *
621  * Returns a node pointer with refcount incremented, use
622  * of_node_put() on it when done.
623  */
624 struct device_node *of_find_node_by_phandle(phandle handle)
625 {
626         struct device_node *np;
627
628         read_lock(&devtree_lock);
629         for (np = allnodes; np; np = np->allnext)
630                 if (np->phandle == handle)
631                         break;
632         of_node_get(np);
633         read_unlock(&devtree_lock);
634         return np;
635 }
636 EXPORT_SYMBOL(of_find_node_by_phandle);
637
638 /**
639  * of_parse_phandle - Resolve a phandle property to a device_node pointer
640  * @np: Pointer to device node holding phandle property
641  * @phandle_name: Name of property holding a phandle value
642  * @index: For properties holding a table of phandles, this is the index into
643  *         the table
644  *
645  * Returns the device_node pointer with refcount incremented.  Use
646  * of_node_put() on it when done.
647  */
648 struct device_node *
649 of_parse_phandle(struct device_node *np, const char *phandle_name, int index)
650 {
651         const phandle *phandle;
652         int size;
653
654         phandle = of_get_property(np, phandle_name, &size);
655         if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
656                 return NULL;
657
658         return of_find_node_by_phandle(phandle[index]);
659 }
660 EXPORT_SYMBOL(of_parse_phandle);
661
662 /**
663  * of_parse_phandles_with_args - Find a node pointed by phandle in a list
664  * @np:         pointer to a device tree node containing a list
665  * @list_name:  property name that contains a list
666  * @cells_name: property name that specifies phandles' arguments count
667  * @index:      index of a phandle to parse out
668  * @out_node:   optional pointer to device_node struct pointer (will be filled)
669  * @out_args:   optional pointer to arguments pointer (will be filled)
670  *
671  * This function is useful to parse lists of phandles and their arguments.
672  * Returns 0 on success and fills out_node and out_args, on error returns
673  * appropriate errno value.
674  *
675  * Example:
676  *
677  * phandle1: node1 {
678  *      #list-cells = <2>;
679  * }
680  *
681  * phandle2: node2 {
682  *      #list-cells = <1>;
683  * }
684  *
685  * node3 {
686  *      list = <&phandle1 1 2 &phandle2 3>;
687  * }
688  *
689  * To get a device_node of the `node2' node you may call this:
690  * of_parse_phandles_with_args(node3, "list", "#list-cells", 2, &node2, &args);
691  */
692 int of_parse_phandles_with_args(struct device_node *np, const char *list_name,
693                                 const char *cells_name, int index,
694                                 struct device_node **out_node,
695                                 const void **out_args)
696 {
697         int ret = -EINVAL;
698         const u32 *list;
699         const u32 *list_end;
700         int size;
701         int cur_index = 0;
702         struct device_node *node = NULL;
703         const void *args = NULL;
704
705         list = of_get_property(np, list_name, &size);
706         if (!list) {
707                 ret = -ENOENT;
708                 goto err0;
709         }
710         list_end = list + size / sizeof(*list);
711
712         while (list < list_end) {
713                 const u32 *cells;
714                 const phandle *phandle;
715
716                 phandle = list++;
717                 args = list;
718
719                 /* one cell hole in the list = <>; */
720                 if (!*phandle)
721                         goto next;
722
723                 node = of_find_node_by_phandle(*phandle);
724                 if (!node) {
725                         pr_debug("%s: could not find phandle\n",
726                                  np->full_name);
727                         goto err0;
728                 }
729
730                 cells = of_get_property(node, cells_name, &size);
731                 if (!cells || size != sizeof(*cells)) {
732                         pr_debug("%s: could not get %s for %s\n",
733                                  np->full_name, cells_name, node->full_name);
734                         goto err1;
735                 }
736
737                 list += *cells;
738                 if (list > list_end) {
739                         pr_debug("%s: insufficient arguments length\n",
740                                  np->full_name);
741                         goto err1;
742                 }
743 next:
744                 if (cur_index == index)
745                         break;
746
747                 of_node_put(node);
748                 node = NULL;
749                 args = NULL;
750                 cur_index++;
751         }
752
753         if (!node) {
754                 /*
755                  * args w/o node indicates that the loop above has stopped at
756                  * the 'hole' cell. Report this differently.
757                  */
758                 if (args)
759                         ret = -EEXIST;
760                 else
761                         ret = -ENOENT;
762                 goto err0;
763         }
764
765         if (out_node)
766                 *out_node = node;
767         if (out_args)
768                 *out_args = args;
769
770         return 0;
771 err1:
772         of_node_put(node);
773 err0:
774         pr_debug("%s failed with status %d\n", __func__, ret);
775         return ret;
776 }
777 EXPORT_SYMBOL(of_parse_phandles_with_args);
778
779 /**
780  * prom_add_property - Add a property to a node
781  */
782 int prom_add_property(struct device_node *np, struct property *prop)
783 {
784         struct property **next;
785         unsigned long flags;
786
787         prop->next = NULL;
788         write_lock_irqsave(&devtree_lock, flags);
789         next = &np->properties;
790         while (*next) {
791                 if (strcmp(prop->name, (*next)->name) == 0) {
792                         /* duplicate ! don't insert it */
793                         write_unlock_irqrestore(&devtree_lock, flags);
794                         return -1;
795                 }
796                 next = &(*next)->next;
797         }
798         *next = prop;
799         write_unlock_irqrestore(&devtree_lock, flags);
800
801 #ifdef CONFIG_PROC_DEVICETREE
802         /* try to add to proc as well if it was initialized */
803         if (np->pde)
804                 proc_device_tree_add_prop(np->pde, prop);
805 #endif /* CONFIG_PROC_DEVICETREE */
806
807         return 0;
808 }
809
810 /**
811  * prom_remove_property - Remove a property from a node.
812  *
813  * Note that we don't actually remove it, since we have given out
814  * who-knows-how-many pointers to the data using get-property.
815  * Instead we just move the property to the "dead properties"
816  * list, so it won't be found any more.
817  */
818 int prom_remove_property(struct device_node *np, struct property *prop)
819 {
820         struct property **next;
821         unsigned long flags;
822         int found = 0;
823
824         write_lock_irqsave(&devtree_lock, flags);
825         next = &np->properties;
826         while (*next) {
827                 if (*next == prop) {
828                         /* found the node */
829                         *next = prop->next;
830                         prop->next = np->deadprops;
831                         np->deadprops = prop;
832                         found = 1;
833                         break;
834                 }
835                 next = &(*next)->next;
836         }
837         write_unlock_irqrestore(&devtree_lock, flags);
838
839         if (!found)
840                 return -ENODEV;
841
842 #ifdef CONFIG_PROC_DEVICETREE
843         /* try to remove the proc node as well */
844         if (np->pde)
845                 proc_device_tree_remove_prop(np->pde, prop);
846 #endif /* CONFIG_PROC_DEVICETREE */
847
848         return 0;
849 }
850
851 /*
852  * prom_update_property - Update a property in a node.
853  *
854  * Note that we don't actually remove it, since we have given out
855  * who-knows-how-many pointers to the data using get-property.
856  * Instead we just move the property to the "dead properties" list,
857  * and add the new property to the property list
858  */
859 int prom_update_property(struct device_node *np,
860                          struct property *newprop,
861                          struct property *oldprop)
862 {
863         struct property **next;
864         unsigned long flags;
865         int found = 0;
866
867         write_lock_irqsave(&devtree_lock, flags);
868         next = &np->properties;
869         while (*next) {
870                 if (*next == oldprop) {
871                         /* found the node */
872                         newprop->next = oldprop->next;
873                         *next = newprop;
874                         oldprop->next = np->deadprops;
875                         np->deadprops = oldprop;
876                         found = 1;
877                         break;
878                 }
879                 next = &(*next)->next;
880         }
881         write_unlock_irqrestore(&devtree_lock, flags);
882
883         if (!found)
884                 return -ENODEV;
885
886 #ifdef CONFIG_PROC_DEVICETREE
887         /* try to add to proc as well if it was initialized */
888         if (np->pde)
889                 proc_device_tree_update_prop(np->pde, newprop, oldprop);
890 #endif /* CONFIG_PROC_DEVICETREE */
891
892         return 0;
893 }
894
895 #if defined(CONFIG_OF_DYNAMIC)
896 /*
897  * Support for dynamic device trees.
898  *
899  * On some platforms, the device tree can be manipulated at runtime.
900  * The routines in this section support adding, removing and changing
901  * device tree nodes.
902  */
903
904 /**
905  * of_attach_node - Plug a device node into the tree and global list.
906  */
907 void of_attach_node(struct device_node *np)
908 {
909         unsigned long flags;
910
911         write_lock_irqsave(&devtree_lock, flags);
912         np->sibling = np->parent->child;
913         np->allnext = allnodes;
914         np->parent->child = np;
915         allnodes = np;
916         write_unlock_irqrestore(&devtree_lock, flags);
917 }
918
919 /**
920  * of_detach_node - "Unplug" a node from the device tree.
921  *
922  * The caller must hold a reference to the node.  The memory associated with
923  * the node is not freed until its refcount goes to zero.
924  */
925 void of_detach_node(struct device_node *np)
926 {
927         struct device_node *parent;
928         unsigned long flags;
929
930         write_lock_irqsave(&devtree_lock, flags);
931
932         parent = np->parent;
933         if (!parent)
934                 goto out_unlock;
935
936         if (allnodes == np)
937                 allnodes = np->allnext;
938         else {
939                 struct device_node *prev;
940                 for (prev = allnodes;
941                      prev->allnext != np;
942                      prev = prev->allnext)
943                         ;
944                 prev->allnext = np->allnext;
945         }
946
947         if (parent->child == np)
948                 parent->child = np->sibling;
949         else {
950                 struct device_node *prevsib;
951                 for (prevsib = np->parent->child;
952                      prevsib->sibling != np;
953                      prevsib = prevsib->sibling)
954                         ;
955                 prevsib->sibling = np->sibling;
956         }
957
958         of_node_set_flag(np, OF_DETACHED);
959
960 out_unlock:
961         write_unlock_irqrestore(&devtree_lock, flags);
962 }
963 #endif /* defined(CONFIG_OF_DYNAMIC) */
964