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