]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/of/base.c
media: v4l2-core: Migration from upstream
[sojka/nv-tegra/linux-3.10.git] / 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/ctype.h>
21 #include <linux/module.h>
22 #include <linux/of.h>
23 #include <linux/of_graph.h>
24 #include <linux/spinlock.h>
25 #include <linux/slab.h>
26 #include <linux/proc_fs.h>
27
28 #include "of_private.h"
29
30 LIST_HEAD(aliases_lookup);
31
32 struct device_node *of_allnodes;
33 EXPORT_SYMBOL(of_allnodes);
34 struct device_node *of_chosen;
35 struct device_node *of_aliases;
36
37 DEFINE_MUTEX(of_aliases_mutex);
38
39 /* use when traversing tree through the allnext, child, sibling,
40  * or parent members of struct device_node.
41  */
42 DEFINE_RAW_SPINLOCK(devtree_lock);
43
44 int of_n_addr_cells(struct device_node *np)
45 {
46         const __be32 *ip;
47
48         do {
49                 if (np->parent)
50                         np = np->parent;
51                 ip = of_get_property(np, "#address-cells", NULL);
52                 if (ip)
53                         return be32_to_cpup(ip);
54         } while (np->parent);
55         /* No #address-cells property for the root node */
56         return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
57 }
58 EXPORT_SYMBOL(of_n_addr_cells);
59
60 int of_n_size_cells(struct device_node *np)
61 {
62         const __be32 *ip;
63
64         do {
65                 if (np->parent)
66                         np = np->parent;
67                 ip = of_get_property(np, "#size-cells", NULL);
68                 if (ip)
69                         return be32_to_cpup(ip);
70         } while (np->parent);
71         /* No #size-cells property for the root node */
72         return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
73 }
74 EXPORT_SYMBOL(of_n_size_cells);
75
76 #if defined(CONFIG_OF_DYNAMIC)
77 /**
78  *      of_node_get - Increment refcount of a node
79  *      @node:  Node to inc refcount, NULL is supported to
80  *              simplify writing of callers
81  *
82  *      Returns node.
83  */
84 struct device_node *of_node_get(struct device_node *node)
85 {
86         if (node)
87                 kref_get(&node->kref);
88         return node;
89 }
90 EXPORT_SYMBOL(of_node_get);
91
92 static inline struct device_node *kref_to_device_node(struct kref *kref)
93 {
94         return container_of(kref, struct device_node, kref);
95 }
96
97 /**
98  *      of_node_release - release a dynamically allocated node
99  *      @kref:  kref element of the node to be released
100  *
101  *      In of_node_put() this function is passed to kref_put()
102  *      as the destructor.
103  */
104 static void of_node_release(struct kref *kref)
105 {
106         struct device_node *node = kref_to_device_node(kref);
107         struct property *prop = node->properties;
108
109         /* We should never be releasing nodes that haven't been detached. */
110         if (!of_node_check_flag(node, OF_DETACHED)) {
111                 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
112                 dump_stack();
113                 kref_init(&node->kref);
114                 return;
115         }
116
117         if (!of_node_check_flag(node, OF_DYNAMIC))
118                 return;
119
120         while (prop) {
121                 struct property *next = prop->next;
122                 kfree(prop->name);
123                 kfree(prop->value);
124                 kfree(prop);
125                 prop = next;
126
127                 if (!prop) {
128                         prop = node->deadprops;
129                         node->deadprops = NULL;
130                 }
131         }
132         kfree(node->full_name);
133         kfree(node->data);
134         kfree(node);
135 }
136
137 /**
138  *      of_node_put - Decrement refcount of a node
139  *      @node:  Node to dec refcount, NULL is supported to
140  *              simplify writing of callers
141  *
142  */
143 void of_node_put(struct device_node *node)
144 {
145         if (node)
146                 kref_put(&node->kref, of_node_release);
147 }
148 EXPORT_SYMBOL(of_node_put);
149 #endif /* CONFIG_OF_DYNAMIC */
150
151 static struct property *__of_find_property(const struct device_node *np,
152                                            const char *name, int *lenp)
153 {
154         struct property *pp;
155
156         if (!np)
157                 return NULL;
158
159         for (pp = np->properties; pp; pp = pp->next) {
160                 if (of_prop_cmp(pp->name, name) == 0) {
161                         if (lenp)
162                                 *lenp = pp->length;
163                         break;
164                 }
165         }
166
167         return pp;
168 }
169
170 struct property *of_find_property(const struct device_node *np,
171                                   const char *name,
172                                   int *lenp)
173 {
174         struct property *pp;
175         unsigned long flags;
176
177         raw_spin_lock_irqsave(&devtree_lock, flags);
178         pp = __of_find_property(np, name, lenp);
179         raw_spin_unlock_irqrestore(&devtree_lock, flags);
180
181         return pp;
182 }
183 EXPORT_SYMBOL(of_find_property);
184
185 /**
186  * of_find_all_nodes - Get next node in global list
187  * @prev:       Previous node or NULL to start iteration
188  *              of_node_put() will be called on it
189  *
190  * Returns a node pointer with refcount incremented, use
191  * of_node_put() on it when done.
192  */
193 struct device_node *of_find_all_nodes(struct device_node *prev)
194 {
195         struct device_node *np;
196         unsigned long flags;
197
198         raw_spin_lock_irqsave(&devtree_lock, flags);
199         np = prev ? prev->allnext : of_allnodes;
200         for (; np != NULL; np = np->allnext)
201                 if (of_node_get(np))
202                         break;
203         of_node_put(prev);
204         raw_spin_unlock_irqrestore(&devtree_lock, flags);
205         return np;
206 }
207 EXPORT_SYMBOL(of_find_all_nodes);
208
209 /*
210  * Find a property with a given name for a given node
211  * and return the value.
212  */
213 static const void *__of_get_property(const struct device_node *np,
214                                      const char *name, int *lenp)
215 {
216         struct property *pp = __of_find_property(np, name, lenp);
217
218         return pp ? pp->value : NULL;
219 }
220
221 /*
222  * Find a property with a given name for a given node
223  * and return the value.
224  */
225 const void *of_get_property(const struct device_node *np, const char *name,
226                             int *lenp)
227 {
228         struct property *pp = of_find_property(np, name, lenp);
229
230         return pp ? pp->value : NULL;
231 }
232 EXPORT_SYMBOL(of_get_property);
233
234 /** Checks if the given "compat" string matches one of the strings in
235  * the device's "compatible" property
236  */
237 static int __of_device_is_compatible(const struct device_node *device,
238                                      const char *compat)
239 {
240         const char* cp;
241         int cplen, l;
242
243         cp = __of_get_property(device, "compatible", &cplen);
244         if (cp == NULL)
245                 return 0;
246         while (cplen > 0) {
247                 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
248                         return 1;
249                 l = strlen(cp) + 1;
250                 cp += l;
251                 cplen -= l;
252         }
253
254         return 0;
255 }
256
257 /** Checks if the given "compat" string matches one of the strings in
258  * the device's "compatible" property
259  */
260 int of_device_is_compatible(const struct device_node *device,
261                 const char *compat)
262 {
263         unsigned long flags;
264         int res;
265
266         raw_spin_lock_irqsave(&devtree_lock, flags);
267         res = __of_device_is_compatible(device, compat);
268         raw_spin_unlock_irqrestore(&devtree_lock, flags);
269         return res;
270 }
271 EXPORT_SYMBOL(of_device_is_compatible);
272
273 /**
274  * of_machine_is_compatible - Test root of device tree for a given compatible value
275  * @compat: compatible string to look for in root node's compatible property.
276  *
277  * Returns true if the root node has the given value in its
278  * compatible property.
279  */
280 int of_machine_is_compatible(const char *compat)
281 {
282         struct device_node *root;
283         int rc = 0;
284
285         root = of_find_node_by_path("/");
286         if (root) {
287                 rc = of_device_is_compatible(root, compat);
288                 of_node_put(root);
289         }
290         return rc;
291 }
292 EXPORT_SYMBOL(of_machine_is_compatible);
293
294 /**
295  *  __of_device_is_available - check if a device is available for use
296  *
297  *  @device: Node to check for availability, with locks already held
298  *
299  *  Returns 1 if the status property is absent or set to "okay" or "ok",
300  *  0 otherwise
301  */
302 static int __of_device_is_available(const struct device_node *device)
303 {
304         const char *status;
305         int statlen;
306
307         status = __of_get_property(device, "status", &statlen);
308         if (status == NULL)
309                 return 1;
310
311         if (statlen > 0) {
312                 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
313                         return 1;
314         }
315
316         return 0;
317 }
318
319 /**
320  *  of_device_is_available - check if a device is available for use
321  *
322  *  @device: Node to check for availability
323  *
324  *  Returns 1 if the status property is absent or set to "okay" or "ok",
325  *  0 otherwise
326  */
327 int of_device_is_available(const struct device_node *device)
328 {
329         unsigned long flags;
330         int res;
331
332         raw_spin_lock_irqsave(&devtree_lock, flags);
333         res = __of_device_is_available(device);
334         raw_spin_unlock_irqrestore(&devtree_lock, flags);
335         return res;
336
337 }
338 EXPORT_SYMBOL(of_device_is_available);
339
340 /**
341  *      of_get_parent - Get a node's parent if any
342  *      @node:  Node to get parent
343  *
344  *      Returns a node pointer with refcount incremented, use
345  *      of_node_put() on it when done.
346  */
347 struct device_node *of_get_parent(const struct device_node *node)
348 {
349         struct device_node *np;
350         unsigned long flags;
351
352         if (!node)
353                 return NULL;
354
355         raw_spin_lock_irqsave(&devtree_lock, flags);
356         np = of_node_get(node->parent);
357         raw_spin_unlock_irqrestore(&devtree_lock, flags);
358         return np;
359 }
360 EXPORT_SYMBOL(of_get_parent);
361
362 /**
363  *      of_get_next_parent - Iterate to a node's parent
364  *      @node:  Node to get parent of
365  *
366  *      This is like of_get_parent() except that it drops the
367  *      refcount on the passed node, making it suitable for iterating
368  *      through a node's parents.
369  *
370  *      Returns a node pointer with refcount incremented, use
371  *      of_node_put() on it when done.
372  */
373 struct device_node *of_get_next_parent(struct device_node *node)
374 {
375         struct device_node *parent;
376         unsigned long flags;
377
378         if (!node)
379                 return NULL;
380
381         raw_spin_lock_irqsave(&devtree_lock, flags);
382         parent = of_node_get(node->parent);
383         of_node_put(node);
384         raw_spin_unlock_irqrestore(&devtree_lock, flags);
385         return parent;
386 }
387 EXPORT_SYMBOL(of_get_next_parent);
388
389 /**
390  *      of_get_next_child - Iterate a node childs
391  *      @node:  parent node
392  *      @prev:  previous child of the parent node, or NULL to get first
393  *
394  *      Returns a node pointer with refcount incremented, use
395  *      of_node_put() on it when done.
396  */
397 struct device_node *of_get_next_child(const struct device_node *node,
398         struct device_node *prev)
399 {
400         struct device_node *next;
401         unsigned long flags;
402
403         raw_spin_lock_irqsave(&devtree_lock, flags);
404         next = prev ? prev->sibling : node->child;
405         for (; next; next = next->sibling)
406                 if (of_node_get(next))
407                         break;
408         of_node_put(prev);
409         raw_spin_unlock_irqrestore(&devtree_lock, flags);
410         return next;
411 }
412 EXPORT_SYMBOL(of_get_next_child);
413
414 /**
415  *      of_get_next_available_child - Find the next available child node
416  *      @node:  parent node
417  *      @prev:  previous child of the parent node, or NULL to get first
418  *
419  *      This function is like of_get_next_child(), except that it
420  *      automatically skips any disabled nodes (i.e. status = "disabled").
421  */
422 struct device_node *of_get_next_available_child(const struct device_node *node,
423         struct device_node *prev)
424 {
425         struct device_node *next;
426         unsigned long flags;
427
428         raw_spin_lock_irqsave(&devtree_lock, flags);
429         next = prev ? prev->sibling : node->child;
430         for (; next; next = next->sibling) {
431                 if (!__of_device_is_available(next))
432                         continue;
433                 if (of_node_get(next))
434                         break;
435         }
436         of_node_put(prev);
437         raw_spin_unlock_irqrestore(&devtree_lock, flags);
438         return next;
439 }
440 EXPORT_SYMBOL(of_get_next_available_child);
441
442 /**
443  *      of_get_child_by_name - Find the child node by name for a given parent
444  *      @node:  parent node
445  *      @name:  child name to look for.
446  *
447  *      This function looks for child node for given matching name
448  *
449  *      Returns a node pointer if found, with refcount incremented, use
450  *      of_node_put() on it when done.
451  *      Returns NULL if node is not found.
452  */
453 struct device_node *of_get_child_by_name(const struct device_node *node,
454                                 const char *name)
455 {
456         struct device_node *child;
457
458         for_each_child_of_node(node, child)
459                 if (child->name && (of_node_cmp(child->name, name) == 0))
460                         break;
461         return child;
462 }
463 EXPORT_SYMBOL(of_get_child_by_name);
464
465 /**
466  *      of_find_node_by_path - Find a node matching a full OF path
467  *      @path:  The full path to match
468  *
469  *      Returns a node pointer with refcount incremented, use
470  *      of_node_put() on it when done.
471  */
472 struct device_node *of_find_node_by_path(const char *path)
473 {
474         struct device_node *np = of_allnodes;
475         unsigned long flags;
476
477         raw_spin_lock_irqsave(&devtree_lock, flags);
478         for (; np; np = np->allnext) {
479                 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
480                     && of_node_get(np))
481                         break;
482         }
483         raw_spin_unlock_irqrestore(&devtree_lock, flags);
484         return np;
485 }
486 EXPORT_SYMBOL(of_find_node_by_path);
487
488 /**
489  *      of_find_node_by_name - Find a node by its "name" property
490  *      @from:  The node to start searching from or NULL, the node
491  *              you pass will not be searched, only the next one
492  *              will; typically, you pass what the previous call
493  *              returned. of_node_put() will be called on it
494  *      @name:  The name string to match against
495  *
496  *      Returns a node pointer with refcount incremented, use
497  *      of_node_put() on it when done.
498  */
499 struct device_node *of_find_node_by_name(struct device_node *from,
500         const char *name)
501 {
502         struct device_node *np;
503         unsigned long flags;
504
505         raw_spin_lock_irqsave(&devtree_lock, flags);
506         np = from ? from->allnext : of_allnodes;
507         for (; np; np = np->allnext)
508                 if (np->name && (of_node_cmp(np->name, name) == 0)
509                     && of_node_get(np))
510                         break;
511         of_node_put(from);
512         raw_spin_unlock_irqrestore(&devtree_lock, flags);
513         return np;
514 }
515 EXPORT_SYMBOL(of_find_node_by_name);
516
517 /**
518  *      of_find_node_by_type - Find a node by its "device_type" property
519  *      @from:  The node to start searching from, or NULL to start searching
520  *              the entire device tree. The node you pass will not be
521  *              searched, only the next one will; typically, you pass
522  *              what the previous call returned. of_node_put() will be
523  *              called on from for you.
524  *      @type:  The type string to match against
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_node_by_type(struct device_node *from,
530         const char *type)
531 {
532         struct device_node *np;
533         unsigned long flags;
534
535         raw_spin_lock_irqsave(&devtree_lock, flags);
536         np = from ? from->allnext : of_allnodes;
537         for (; np; np = np->allnext)
538                 if (np->type && (of_node_cmp(np->type, type) == 0)
539                     && of_node_get(np))
540                         break;
541         of_node_put(from);
542         raw_spin_unlock_irqrestore(&devtree_lock, flags);
543         return np;
544 }
545 EXPORT_SYMBOL(of_find_node_by_type);
546
547 /**
548  *      of_find_compatible_node - Find a node based on type and one of the
549  *                                tokens in its "compatible" property
550  *      @from:          The node to start searching from or NULL, the node
551  *                      you pass will not be searched, only the next one
552  *                      will; typically, you pass what the previous call
553  *                      returned. of_node_put() will be called on it
554  *      @type:          The type string to match "device_type" or NULL to ignore
555  *      @compatible:    The string to match to one of the tokens in the device
556  *                      "compatible" list.
557  *
558  *      Returns a node pointer with refcount incremented, use
559  *      of_node_put() on it when done.
560  */
561 struct device_node *of_find_compatible_node(struct device_node *from,
562         const char *type, const char *compatible)
563 {
564         struct device_node *np;
565         unsigned long flags;
566
567         raw_spin_lock_irqsave(&devtree_lock, flags);
568         np = from ? from->allnext : of_allnodes;
569         for (; np; np = np->allnext) {
570                 if (type
571                     && !(np->type && (of_node_cmp(np->type, type) == 0)))
572                         continue;
573                 if (__of_device_is_compatible(np, compatible) &&
574                     of_node_get(np))
575                         break;
576         }
577         of_node_put(from);
578         raw_spin_unlock_irqrestore(&devtree_lock, flags);
579         return np;
580 }
581 EXPORT_SYMBOL(of_find_compatible_node);
582
583 /**
584  *      of_find_node_with_property - Find a node which has a property with
585  *                                   the given name.
586  *      @from:          The node to start searching from or NULL, the node
587  *                      you pass will not be searched, only the next one
588  *                      will; typically, you pass what the previous call
589  *                      returned. of_node_put() will be called on it
590  *      @prop_name:     The name of the property to look for.
591  *
592  *      Returns a node pointer with refcount incremented, use
593  *      of_node_put() on it when done.
594  */
595 struct device_node *of_find_node_with_property(struct device_node *from,
596         const char *prop_name)
597 {
598         struct device_node *np;
599         struct property *pp;
600         unsigned long flags;
601
602         raw_spin_lock_irqsave(&devtree_lock, flags);
603         np = from ? from->allnext : of_allnodes;
604         for (; np; np = np->allnext) {
605                 for (pp = np->properties; pp; pp = pp->next) {
606                         if (of_prop_cmp(pp->name, prop_name) == 0) {
607                                 of_node_get(np);
608                                 goto out;
609                         }
610                 }
611         }
612 out:
613         of_node_put(from);
614         raw_spin_unlock_irqrestore(&devtree_lock, flags);
615         return np;
616 }
617 EXPORT_SYMBOL(of_find_node_with_property);
618
619 static
620 const struct of_device_id *__of_match_node(const struct of_device_id *matches,
621                                            const struct device_node *node)
622 {
623         if (!matches)
624                 return NULL;
625
626         while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
627                 int match = 1;
628                 if (matches->name[0])
629                         match &= node->name
630                                 && !strcmp(matches->name, node->name);
631                 if (matches->type[0])
632                         match &= node->type
633                                 && !strcmp(matches->type, node->type);
634                 if (matches->compatible[0])
635                         match &= __of_device_is_compatible(node,
636                                                            matches->compatible);
637                 if (match)
638                         return matches;
639                 matches++;
640         }
641         return NULL;
642 }
643
644 /**
645  * of_match_node - Tell if an device_node has a matching of_match structure
646  *      @matches:       array of of device match structures to search in
647  *      @node:          the of device structure to match against
648  *
649  *      Low level utility function used by device matching.
650  */
651 const struct of_device_id *of_match_node(const struct of_device_id *matches,
652                                          const struct device_node *node)
653 {
654         const struct of_device_id *match;
655         unsigned long flags;
656
657         raw_spin_lock_irqsave(&devtree_lock, flags);
658         match = __of_match_node(matches, node);
659         raw_spin_unlock_irqrestore(&devtree_lock, flags);
660         return match;
661 }
662 EXPORT_SYMBOL(of_match_node);
663
664 /**
665  *      of_find_matching_node_and_match - Find a node based on an of_device_id
666  *                                        match table.
667  *      @from:          The node to start searching from or NULL, the node
668  *                      you pass will not be searched, only the next one
669  *                      will; typically, you pass what the previous call
670  *                      returned. of_node_put() will be called on it
671  *      @matches:       array of of device match structures to search in
672  *      @match          Updated to point at the matches entry which matched
673  *
674  *      Returns a node pointer with refcount incremented, use
675  *      of_node_put() on it when done.
676  */
677 struct device_node *of_find_matching_node_and_match(struct device_node *from,
678                                         const struct of_device_id *matches,
679                                         const struct of_device_id **match)
680 {
681         struct device_node *np;
682         const struct of_device_id *m;
683         unsigned long flags;
684
685         if (match)
686                 *match = NULL;
687
688         raw_spin_lock_irqsave(&devtree_lock, flags);
689         np = from ? from->allnext : of_allnodes;
690         for (; np; np = np->allnext) {
691                 m = __of_match_node(matches, np);
692                 if (m && of_node_get(np)) {
693                         if (match)
694                                 *match = m;
695                         break;
696                 }
697         }
698         of_node_put(from);
699         raw_spin_unlock_irqrestore(&devtree_lock, flags);
700         return np;
701 }
702 EXPORT_SYMBOL(of_find_matching_node_and_match);
703
704 /**
705  * of_modalias_node - Lookup appropriate modalias for a device node
706  * @node:       pointer to a device tree node
707  * @modalias:   Pointer to buffer that modalias value will be copied into
708  * @len:        Length of modalias value
709  *
710  * Based on the value of the compatible property, this routine will attempt
711  * to choose an appropriate modalias value for a particular device tree node.
712  * It does this by stripping the manufacturer prefix (as delimited by a ',')
713  * from the first entry in the compatible list property.
714  *
715  * This routine returns 0 on success, <0 on failure.
716  */
717 int of_modalias_node(struct device_node *node, char *modalias, int len)
718 {
719         const char *compatible, *p;
720         int cplen;
721
722         compatible = of_get_property(node, "compatible", &cplen);
723         if (!compatible || strlen(compatible) > cplen)
724                 return -ENODEV;
725         p = strchr(compatible, ',');
726         strlcpy(modalias, p ? p + 1 : compatible, len);
727         return 0;
728 }
729 EXPORT_SYMBOL_GPL(of_modalias_node);
730
731 /**
732  * of_find_node_by_phandle - Find a node given a phandle
733  * @handle:     phandle of the node to find
734  *
735  * Returns a node pointer with refcount incremented, use
736  * of_node_put() on it when done.
737  */
738 struct device_node *of_find_node_by_phandle(phandle handle)
739 {
740         struct device_node *np;
741         unsigned long flags;
742
743         raw_spin_lock_irqsave(&devtree_lock, flags);
744         for (np = of_allnodes; np; np = np->allnext)
745                 if (np->phandle == handle)
746                         break;
747         of_node_get(np);
748         raw_spin_unlock_irqrestore(&devtree_lock, flags);
749         return np;
750 }
751 EXPORT_SYMBOL(of_find_node_by_phandle);
752
753 /**
754  * of_find_property_value_of_size
755  *
756  * @np:         device node from which the property value is to be read.
757  * @propname:   name of the property to be searched.
758  * @len:        requested length of property value
759  *
760  * Search for a property in a device node and valid the requested size.
761  * Returns the property value on success, -EINVAL if the property does not
762  *  exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
763  * property data isn't large enough.
764  *
765  */
766 static void *of_find_property_value_of_size(const struct device_node *np,
767                         const char *propname, u32 len)
768 {
769         struct property *prop = of_find_property(np, propname, NULL);
770
771         if (!prop)
772                 return ERR_PTR(-EINVAL);
773         if (!prop->value)
774                 return ERR_PTR(-ENODATA);
775         if (len > prop->length)
776                 return ERR_PTR(-EOVERFLOW);
777
778         return prop->value;
779 }
780
781 /**
782  * of_property_read_u32_index - Find and read a u32 from a multi-value property.
783  *
784  * @np:         device node from which the property value is to be read.
785  * @propname:   name of the property to be searched.
786  * @index:      index of the u32 in the list of values
787  * @out_value:  pointer to return value, modified only if no error.
788  *
789  * Search for a property in a device node and read nth 32-bit value from
790  * it. Returns 0 on success, -EINVAL if the property does not exist,
791  * -ENODATA if property does not have a value, and -EOVERFLOW if the
792  * property data isn't large enough.
793  *
794  * The out_value is modified only if a valid u32 value can be decoded.
795  */
796 int of_property_read_u32_index(const struct device_node *np,
797                                        const char *propname,
798                                        u32 index, u32 *out_value)
799 {
800         const u32 *val = of_find_property_value_of_size(np, propname,
801                                         ((index + 1) * sizeof(*out_value)));
802
803         if (IS_ERR(val))
804                 return PTR_ERR(val);
805
806         *out_value = be32_to_cpup(((__be32 *)val) + index);
807         return 0;
808 }
809 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
810
811 /**
812  * of_property_read_u8_array - Find and read an array of u8 from a property.
813  *
814  * @np:         device node from which the property value is to be read.
815  * @propname:   name of the property to be searched.
816  * @out_value:  pointer to return value, modified only if return value is 0.
817  * @sz:         number of array elements to read
818  *
819  * Search for a property in a device node and read 8-bit value(s) from
820  * it. Returns 0 on success, -EINVAL if the property does not exist,
821  * -ENODATA if property does not have a value, and -EOVERFLOW if the
822  * property data isn't large enough.
823  *
824  * dts entry of array should be like:
825  *      property = /bits/ 8 <0x50 0x60 0x70>;
826  *
827  * The out_value is modified only if a valid u8 value can be decoded.
828  */
829 int of_property_read_u8_array(const struct device_node *np,
830                         const char *propname, u8 *out_values, size_t sz)
831 {
832         const u8 *val = of_find_property_value_of_size(np, propname,
833                                                 (sz * sizeof(*out_values)));
834
835         if (IS_ERR(val))
836                 return PTR_ERR(val);
837
838         while (sz--)
839                 *out_values++ = *val++;
840         return 0;
841 }
842 EXPORT_SYMBOL_GPL(of_property_read_u8_array);
843
844 /**
845  * of_property_read_u16_array - Find and read an array of u16 from a property.
846  *
847  * @np:         device node from which the property value is to be read.
848  * @propname:   name of the property to be searched.
849  * @out_value:  pointer to return value, modified only if return value is 0.
850  * @sz:         number of array elements to read
851  *
852  * Search for a property in a device node and read 16-bit value(s) from
853  * it. Returns 0 on success, -EINVAL if the property does not exist,
854  * -ENODATA if property does not have a value, and -EOVERFLOW if the
855  * property data isn't large enough.
856  *
857  * dts entry of array should be like:
858  *      property = /bits/ 16 <0x5000 0x6000 0x7000>;
859  *
860  * The out_value is modified only if a valid u16 value can be decoded.
861  */
862 int of_property_read_u16_array(const struct device_node *np,
863                         const char *propname, u16 *out_values, size_t sz)
864 {
865         const __be16 *val = of_find_property_value_of_size(np, propname,
866                                                 (sz * sizeof(*out_values)));
867
868         if (IS_ERR(val))
869                 return PTR_ERR(val);
870
871         while (sz--)
872                 *out_values++ = be16_to_cpup(val++);
873         return 0;
874 }
875 EXPORT_SYMBOL_GPL(of_property_read_u16_array);
876
877 /**
878  * of_property_read_u32_array - Find and read an array of 32 bit integers
879  * from a property.
880  *
881  * @np:         device node from which the property value is to be read.
882  * @propname:   name of the property to be searched.
883  * @out_value:  pointer to return value, modified only if return value is 0.
884  * @sz:         number of array elements to read
885  *
886  * Search for a property in a device node and read 32-bit value(s) from
887  * it. Returns 0 on success, -EINVAL if the property does not exist,
888  * -ENODATA if property does not have a value, and -EOVERFLOW if the
889  * property data isn't large enough.
890  *
891  * The out_value is modified only if a valid u32 value can be decoded.
892  */
893 int of_property_read_u32_array(const struct device_node *np,
894                                const char *propname, u32 *out_values,
895                                size_t sz)
896 {
897         const __be32 *val = of_find_property_value_of_size(np, propname,
898                                                 (sz * sizeof(*out_values)));
899
900         if (IS_ERR(val))
901                 return PTR_ERR(val);
902
903         while (sz--)
904                 *out_values++ = be32_to_cpup(val++);
905         return 0;
906 }
907 EXPORT_SYMBOL_GPL(of_property_read_u32_array);
908
909 /**
910  * of_property_read_u64 - Find and read a 64 bit integer from a property
911  * @np:         device node from which the property value is to be read.
912  * @propname:   name of the property to be searched.
913  * @out_value:  pointer to return value, modified only if return value is 0.
914  *
915  * Search for a property in a device node and read a 64-bit value from
916  * it. Returns 0 on success, -EINVAL if the property does not exist,
917  * -ENODATA if property does not have a value, and -EOVERFLOW if the
918  * property data isn't large enough.
919  *
920  * The out_value is modified only if a valid u64 value can be decoded.
921  */
922 int of_property_read_u64(const struct device_node *np, const char *propname,
923                          u64 *out_value)
924 {
925         const __be32 *val = of_find_property_value_of_size(np, propname,
926                                                 sizeof(*out_value));
927
928         if (IS_ERR(val))
929                 return PTR_ERR(val);
930
931         *out_value = of_read_number(val, 2);
932         return 0;
933 }
934 EXPORT_SYMBOL_GPL(of_property_read_u64);
935
936 /**
937  * of_property_read_string - Find and read a string from a property
938  * @np:         device node from which the property value is to be read.
939  * @propname:   name of the property to be searched.
940  * @out_string: pointer to null terminated return string, modified only if
941  *              return value is 0.
942  *
943  * Search for a property in a device tree node and retrieve a null
944  * terminated string value (pointer to data, not a copy). Returns 0 on
945  * success, -EINVAL if the property does not exist, -ENODATA if property
946  * does not have a value, and -EILSEQ if the string is not null-terminated
947  * within the length of the property data.
948  *
949  * The out_string pointer is modified only if a valid string can be decoded.
950  */
951 int of_property_read_string(struct device_node *np, const char *propname,
952                                 const char **out_string)
953 {
954         struct property *prop = of_find_property(np, propname, NULL);
955         if (!prop)
956                 return -EINVAL;
957         if (!prop->value)
958                 return -ENODATA;
959         if (strnlen(prop->value, prop->length) >= prop->length)
960                 return -EILSEQ;
961         *out_string = prop->value;
962         return 0;
963 }
964 EXPORT_SYMBOL_GPL(of_property_read_string);
965
966 /**
967  * of_property_match_string() - Find string in a list and return index
968  * @np: pointer to node containing string list property
969  * @propname: string list property name
970  * @string: pointer to string to search for in string list
971  *
972  * This function searches a string list property and returns the index
973  * of a specific string value.
974  */
975 int of_property_match_string(struct device_node *np, const char *propname,
976                              const char *string)
977 {
978         struct property *prop = of_find_property(np, propname, NULL);
979         size_t l;
980         int i;
981         const char *p, *end;
982
983         if (!prop)
984                 return -EINVAL;
985         if (!prop->value)
986                 return -ENODATA;
987
988         p = prop->value;
989         end = p + prop->length;
990
991         for (i = 0; p < end; i++, p += l) {
992                 l = strnlen(p, end - p) + 1;
993                 if (p + l > end)
994                         return -EILSEQ;
995                 pr_debug("comparing %s with %s\n", string, p);
996                 if (strcmp(string, p) == 0)
997                         return i; /* Found it; return index */
998         }
999         return -ENODATA;
1000 }
1001 EXPORT_SYMBOL_GPL(of_property_match_string);
1002
1003 /**
1004  * of_property_read_string_util() - Utility helper for parsing string properties
1005  * @np:         device node from which the property value is to be read.
1006  * @propname:   name of the property to be searched.
1007  * @out_strs:   output array of string pointers.
1008  * @sz:         number of array elements to read.
1009  * @skip:       Number of strings to skip over at beginning of list.
1010  *
1011  * Don't call this function directly. It is a utility helper for the
1012  * of_property_read_string*() family of functions.
1013  */
1014 int of_property_read_string_helper(struct device_node *np, const char *propname,
1015                                    const char **out_strs, size_t sz, int skip)
1016 {
1017         struct property *prop = of_find_property(np, propname, NULL);
1018         int l = 0, i = 0;
1019         const char *p, *end;
1020
1021         if (!prop)
1022                 return -EINVAL;
1023         if (!prop->value)
1024                 return -ENODATA;
1025         p = prop->value;
1026         end = p + prop->length;
1027
1028         for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) {
1029                 l = strnlen(p, end - p) + 1;
1030                 if (p + l > end)
1031                         return -EILSEQ;
1032                 if (out_strs && i >= skip)
1033                         *out_strs++ = p;
1034         }
1035         i -= skip;
1036         return i <= 0 ? -ENODATA : i;
1037 }
1038 EXPORT_SYMBOL_GPL(of_property_read_string_helper);
1039
1040 /**
1041  * of_parse_phandle - Resolve a phandle property to a device_node pointer
1042  * @np: Pointer to device node holding phandle property
1043  * @phandle_name: Name of property holding a phandle value
1044  * @index: For properties holding a table of phandles, this is the index into
1045  *         the table
1046  *
1047  * Returns the device_node pointer with refcount incremented.  Use
1048  * of_node_put() on it when done.
1049  */
1050 struct device_node *of_parse_phandle(const struct device_node *np,
1051                                      const char *phandle_name, int index)
1052 {
1053         const __be32 *phandle;
1054         int size;
1055
1056         phandle = of_get_property(np, phandle_name, &size);
1057         if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
1058                 return NULL;
1059
1060         return of_find_node_by_phandle(be32_to_cpup(phandle + index));
1061 }
1062 EXPORT_SYMBOL(of_parse_phandle);
1063
1064 /**
1065  * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1066  * @np:         pointer to a device tree node containing a list
1067  * @list_name:  property name that contains a list
1068  * @cells_name: property name that specifies phandles' arguments count
1069  * @index:      index of a phandle to parse out
1070  * @out_args:   optional pointer to output arguments structure (will be filled)
1071  *
1072  * This function is useful to parse lists of phandles and their arguments.
1073  * Returns 0 on success and fills out_args, on error returns appropriate
1074  * errno value.
1075  *
1076  * Caller is responsible to call of_node_put() on the returned out_args->node
1077  * pointer.
1078  *
1079  * Example:
1080  *
1081  * phandle1: node1 {
1082  *      #list-cells = <2>;
1083  * }
1084  *
1085  * phandle2: node2 {
1086  *      #list-cells = <1>;
1087  * }
1088  *
1089  * node3 {
1090  *      list = <&phandle1 1 2 &phandle2 3>;
1091  * }
1092  *
1093  * To get a device_node of the `node2' node you may call this:
1094  * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1095  */
1096 static int __of_parse_phandle_with_args(const struct device_node *np,
1097                                         const char *list_name,
1098                                         const char *cells_name, int index,
1099                                         struct of_phandle_args *out_args)
1100 {
1101         const __be32 *list, *list_end;
1102         int rc = 0, size, cur_index = 0;
1103         uint32_t count = 0;
1104         struct device_node *node = NULL;
1105         phandle phandle;
1106
1107         /* Retrieve the phandle list property */
1108         list = of_get_property(np, list_name, &size);
1109         if (!list)
1110                 return -ENOENT;
1111         list_end = list + size / sizeof(*list);
1112
1113         /* Loop over the phandles until all the requested entry is found */
1114         while (list < list_end) {
1115                 rc = -EINVAL;
1116                 count = 0;
1117
1118                 /*
1119                  * If phandle is 0, then it is an empty entry with no
1120                  * arguments.  Skip forward to the next entry.
1121                  */
1122                 phandle = be32_to_cpup(list++);
1123                 if (phandle) {
1124                         /*
1125                          * Find the provider node and parse the #*-cells
1126                          * property to determine the argument length
1127                          */
1128                         node = of_find_node_by_phandle(phandle);
1129                         if (!node) {
1130                                 pr_err("%s: could not find phandle\n",
1131                                          np->full_name);
1132                                 goto err;
1133                         }
1134                         if (of_property_read_u32(node, cells_name, &count)) {
1135                                 pr_err("%s: could not get %s for %s\n",
1136                                          np->full_name, cells_name,
1137                                          node->full_name);
1138                                 goto err;
1139                         }
1140
1141                         /*
1142                          * Make sure that the arguments actually fit in the
1143                          * remaining property data length
1144                          */
1145                         if (list + count > list_end) {
1146                                 pr_err("%s: arguments longer than property\n",
1147                                          np->full_name);
1148                                 goto err;
1149                         }
1150                 }
1151
1152                 /*
1153                  * All of the error cases above bail out of the loop, so at
1154                  * this point, the parsing is successful. If the requested
1155                  * index matches, then fill the out_args structure and return,
1156                  * or return -ENOENT for an empty entry.
1157                  */
1158                 rc = -ENOENT;
1159                 if (cur_index == index) {
1160                         if (!phandle)
1161                                 goto err;
1162
1163                         if (out_args) {
1164                                 int i;
1165                                 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1166                                         count = MAX_PHANDLE_ARGS;
1167                                 out_args->np = node;
1168                                 out_args->args_count = count;
1169                                 for (i = 0; i < count; i++)
1170                                         out_args->args[i] = be32_to_cpup(list++);
1171                         } else {
1172                                 of_node_put(node);
1173                         }
1174
1175                         /* Found it! return success */
1176                         return 0;
1177                 }
1178
1179                 of_node_put(node);
1180                 node = NULL;
1181                 list += count;
1182                 cur_index++;
1183         }
1184
1185         /*
1186          * Unlock node before returning result; will be one of:
1187          * -ENOENT : index is for empty phandle
1188          * -EINVAL : parsing error on data
1189          * [1..n]  : Number of phandle (count mode; when index = -1)
1190          */
1191         rc = index < 0 ? cur_index : -ENOENT;
1192  err:
1193         if (node)
1194                 of_node_put(node);
1195         return rc;
1196 }
1197
1198 int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1199                                 const char *cells_name, int index,
1200                                 struct of_phandle_args *out_args)
1201 {
1202         if (index < 0)
1203                 return -EINVAL;
1204         return __of_parse_phandle_with_args(np, list_name, cells_name, index, out_args);
1205 }
1206 EXPORT_SYMBOL(of_parse_phandle_with_args);
1207
1208 void of_phandle_iter_next(struct of_phandle_iter *iter)
1209 {
1210         struct device_node *dn;
1211         int i, count;
1212
1213         if (!iter->cur || (iter->cur >= iter->end))
1214                 goto err_out;
1215
1216         dn = of_find_node_by_phandle(be32_to_cpup(iter->cur++));
1217         if (!dn)
1218                 goto err_out;
1219
1220         if (iter->cells_name) {
1221                 if (of_property_read_u32(dn, iter->cells_name, &count))
1222                         goto err_out;
1223         } else {
1224                 count =  iter->cell_count;
1225         }
1226
1227         iter->out_args.np = dn;
1228         iter->out_args.args_count = count;
1229         for (i = 0; i < count; i++)
1230                 iter->out_args.args[i] = be32_to_cpup(iter->cur++);
1231
1232         return;
1233
1234 err_out:
1235         iter->cur = NULL;
1236 }
1237 EXPORT_SYMBOL_GPL(of_phandle_iter_next);
1238
1239 void of_phandle_iter_start(struct of_phandle_iter *iter,
1240                            const struct device_node *np,
1241                            const char *list_name, const char *cells_name,
1242                            int cell_count)
1243 {
1244         int bytes;
1245
1246         iter->cur = of_get_property(np, list_name, &bytes);
1247         if (!iter->cur)
1248                 return;
1249         iter->end = iter->cur;
1250         if (bytes)
1251                 iter->end += bytes / sizeof(*iter->cur);
1252         iter->cells_name = cells_name;
1253         iter->cell_count = cell_count;
1254         of_phandle_iter_next(iter);
1255 }
1256 EXPORT_SYMBOL_GPL(of_phandle_iter_start);
1257
1258
1259 /**
1260  * of_count_phandle_with_args() - Find the number of phandles references in a property
1261  * @np:         pointer to a device tree node containing a list
1262  * @list_name:  property name that contains a list
1263  * @cells_name: property name that specifies phandles' arguments count
1264  *
1265  * Returns the number of phandle + argument tuples within a property. It
1266  * is a typical pattern to encode a list of phandle and variable
1267  * arguments into a single property. The number of arguments is encoded
1268  * by a property in the phandle-target node. For example, a gpios
1269  * property would contain a list of GPIO specifies consisting of a
1270  * phandle and 1 or more arguments. The number of arguments are
1271  * determined by the #gpio-cells property in the node pointed to by the
1272  * phandle.
1273  */
1274 int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1275                                 const char *cells_name)
1276 {
1277         return __of_parse_phandle_with_args(np, list_name, cells_name, -1, NULL);
1278 }
1279 EXPORT_SYMBOL(of_count_phandle_with_args);
1280
1281 #if defined(CONFIG_OF_DYNAMIC)
1282 static int of_property_notify(int action, struct device_node *np,
1283                               struct property *prop)
1284 {
1285         struct of_prop_reconfig pr;
1286
1287         pr.dn = np;
1288         pr.prop = prop;
1289         return of_reconfig_notify(action, &pr);
1290 }
1291 #else
1292 static int of_property_notify(int action, struct device_node *np,
1293                               struct property *prop)
1294 {
1295         return 0;
1296 }
1297 #endif
1298
1299 /**
1300  * of_add_property - Add a property to a node
1301  */
1302 int of_add_property(struct device_node *np, struct property *prop)
1303 {
1304         struct property **next;
1305         unsigned long flags;
1306         int rc;
1307
1308         rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1309         if (rc)
1310                 return rc;
1311
1312         prop->next = NULL;
1313         raw_spin_lock_irqsave(&devtree_lock, flags);
1314         next = &np->properties;
1315         while (*next) {
1316                 if (strcmp(prop->name, (*next)->name) == 0) {
1317                         /* duplicate ! don't insert it */
1318                         raw_spin_unlock_irqrestore(&devtree_lock, flags);
1319                         return -1;
1320                 }
1321                 next = &(*next)->next;
1322         }
1323         *next = prop;
1324         raw_spin_unlock_irqrestore(&devtree_lock, flags);
1325
1326 #ifdef CONFIG_PROC_DEVICETREE
1327         /* try to add to proc as well if it was initialized */
1328         if (np->pde)
1329                 proc_device_tree_add_prop(np->pde, prop);
1330 #endif /* CONFIG_PROC_DEVICETREE */
1331
1332         return 0;
1333 }
1334
1335 /**
1336  * of_remove_property - Remove a property from a node.
1337  *
1338  * Note that we don't actually remove it, since we have given out
1339  * who-knows-how-many pointers to the data using get-property.
1340  * Instead we just move the property to the "dead properties"
1341  * list, so it won't be found any more.
1342  */
1343 int of_remove_property(struct device_node *np, struct property *prop)
1344 {
1345         struct property **next;
1346         unsigned long flags;
1347         int found = 0;
1348         int rc;
1349
1350         rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1351         if (rc)
1352                 return rc;
1353
1354         raw_spin_lock_irqsave(&devtree_lock, flags);
1355         next = &np->properties;
1356         while (*next) {
1357                 if (*next == prop) {
1358                         /* found the node */
1359                         *next = prop->next;
1360                         prop->next = np->deadprops;
1361                         np->deadprops = prop;
1362                         found = 1;
1363                         break;
1364                 }
1365                 next = &(*next)->next;
1366         }
1367         raw_spin_unlock_irqrestore(&devtree_lock, flags);
1368
1369         if (!found)
1370                 return -ENODEV;
1371
1372 #ifdef CONFIG_PROC_DEVICETREE
1373         /* try to remove the proc node as well */
1374         if (np->pde)
1375                 proc_device_tree_remove_prop(np->pde, prop);
1376 #endif /* CONFIG_PROC_DEVICETREE */
1377
1378         return 0;
1379 }
1380
1381 /*
1382  * of_update_property - Update a property in a node, if the property does
1383  * not exist, add it.
1384  *
1385  * Note that we don't actually remove it, since we have given out
1386  * who-knows-how-many pointers to the data using get-property.
1387  * Instead we just move the property to the "dead properties" list,
1388  * and add the new property to the property list
1389  */
1390 int of_update_property(struct device_node *np, struct property *newprop)
1391 {
1392         struct property **next, *oldprop;
1393         unsigned long flags;
1394         int rc, found = 0;
1395
1396         rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1397         if (rc)
1398                 return rc;
1399
1400         if (!newprop->name)
1401                 return -EINVAL;
1402
1403         oldprop = of_find_property(np, newprop->name, NULL);
1404         if (!oldprop)
1405                 return of_add_property(np, newprop);
1406
1407         raw_spin_lock_irqsave(&devtree_lock, flags);
1408         next = &np->properties;
1409         while (*next) {
1410                 if (*next == oldprop) {
1411                         /* found the node */
1412                         newprop->next = oldprop->next;
1413                         *next = newprop;
1414                         oldprop->next = np->deadprops;
1415                         np->deadprops = oldprop;
1416                         found = 1;
1417                         break;
1418                 }
1419                 next = &(*next)->next;
1420         }
1421         raw_spin_unlock_irqrestore(&devtree_lock, flags);
1422
1423         if (!found)
1424                 return -ENODEV;
1425
1426 #ifdef CONFIG_PROC_DEVICETREE
1427         /* try to add to proc as well if it was initialized */
1428         if (np->pde)
1429                 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1430 #endif /* CONFIG_PROC_DEVICETREE */
1431
1432         return 0;
1433 }
1434
1435 #if defined(CONFIG_OF_DYNAMIC)
1436 /*
1437  * Support for dynamic device trees.
1438  *
1439  * On some platforms, the device tree can be manipulated at runtime.
1440  * The routines in this section support adding, removing and changing
1441  * device tree nodes.
1442  */
1443
1444 static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1445
1446 int of_reconfig_notifier_register(struct notifier_block *nb)
1447 {
1448         return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1449 }
1450 EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
1451
1452 int of_reconfig_notifier_unregister(struct notifier_block *nb)
1453 {
1454         return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1455 }
1456 EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
1457
1458 int of_reconfig_notify(unsigned long action, void *p)
1459 {
1460         int rc;
1461
1462         rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1463         return notifier_to_errno(rc);
1464 }
1465
1466 #ifdef CONFIG_PROC_DEVICETREE
1467 static void of_add_proc_dt_entry(struct device_node *dn)
1468 {
1469         struct proc_dir_entry *ent;
1470
1471         ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
1472         if (ent)
1473                 proc_device_tree_add_node(dn, ent);
1474 }
1475 #else
1476 static void of_add_proc_dt_entry(struct device_node *dn)
1477 {
1478         return;
1479 }
1480 #endif
1481
1482 /**
1483  * of_attach_node - Plug a device node into the tree and global list.
1484  */
1485 int of_attach_node(struct device_node *np)
1486 {
1487         unsigned long flags;
1488         int rc;
1489
1490         rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1491         if (rc)
1492                 return rc;
1493
1494         raw_spin_lock_irqsave(&devtree_lock, flags);
1495         np->sibling = np->parent->child;
1496         np->allnext = of_allnodes;
1497         np->parent->child = np;
1498         of_allnodes = np;
1499         raw_spin_unlock_irqrestore(&devtree_lock, flags);
1500
1501         of_add_proc_dt_entry(np);
1502         return 0;
1503 }
1504
1505 #ifdef CONFIG_PROC_DEVICETREE
1506 static void of_remove_proc_dt_entry(struct device_node *dn)
1507 {
1508         proc_remove(dn->pde);
1509 }
1510 #else
1511 static void of_remove_proc_dt_entry(struct device_node *dn)
1512 {
1513         return;
1514 }
1515 #endif
1516
1517 /**
1518  * of_detach_node - "Unplug" a node from the device tree.
1519  *
1520  * The caller must hold a reference to the node.  The memory associated with
1521  * the node is not freed until its refcount goes to zero.
1522  */
1523 int of_detach_node(struct device_node *np)
1524 {
1525         struct device_node *parent;
1526         unsigned long flags;
1527         int rc = 0;
1528
1529         rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1530         if (rc)
1531                 return rc;
1532
1533         raw_spin_lock_irqsave(&devtree_lock, flags);
1534
1535         if (of_node_check_flag(np, OF_DETACHED)) {
1536                 /* someone already detached it */
1537                 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1538                 return rc;
1539         }
1540
1541         parent = np->parent;
1542         if (!parent) {
1543                 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1544                 return rc;
1545         }
1546
1547         if (of_allnodes == np)
1548                 of_allnodes = np->allnext;
1549         else {
1550                 struct device_node *prev;
1551                 for (prev = of_allnodes;
1552                      prev->allnext != np;
1553                      prev = prev->allnext)
1554                         ;
1555                 prev->allnext = np->allnext;
1556         }
1557
1558         if (parent->child == np)
1559                 parent->child = np->sibling;
1560         else {
1561                 struct device_node *prevsib;
1562                 for (prevsib = np->parent->child;
1563                      prevsib->sibling != np;
1564                      prevsib = prevsib->sibling)
1565                         ;
1566                 prevsib->sibling = np->sibling;
1567         }
1568
1569         of_node_set_flag(np, OF_DETACHED);
1570         raw_spin_unlock_irqrestore(&devtree_lock, flags);
1571
1572         of_remove_proc_dt_entry(np);
1573         return rc;
1574 }
1575 #endif /* defined(CONFIG_OF_DYNAMIC) */
1576
1577 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1578                          int id, const char *stem, int stem_len)
1579 {
1580         ap->np = np;
1581         ap->id = id;
1582         strncpy(ap->stem, stem, stem_len);
1583         ap->stem[stem_len] = 0;
1584         list_add_tail(&ap->link, &aliases_lookup);
1585         pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1586                  ap->alias, ap->stem, ap->id, of_node_full_name(np));
1587 }
1588
1589 /**
1590  * of_alias_scan - Scan all properties of 'aliases' node
1591  *
1592  * The function scans all the properties of 'aliases' node and populate
1593  * the the global lookup table with the properties.  It returns the
1594  * number of alias_prop found, or error code in error case.
1595  *
1596  * @dt_alloc:   An allocator that provides a virtual address to memory
1597  *              for the resulting tree
1598  */
1599 void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1600 {
1601         struct property *pp;
1602
1603         of_chosen = of_find_node_by_path("/chosen");
1604         if (of_chosen == NULL)
1605                 of_chosen = of_find_node_by_path("/chosen@0");
1606         of_aliases = of_find_node_by_path("/aliases");
1607         if (!of_aliases)
1608                 return;
1609
1610         for_each_property_of_node(of_aliases, pp) {
1611                 const char *start = pp->name;
1612                 const char *end = start + strlen(start);
1613                 struct device_node *np;
1614                 struct alias_prop *ap;
1615                 int id, len;
1616
1617                 /* Skip those we do not want to proceed */
1618                 if (!strcmp(pp->name, "name") ||
1619                     !strcmp(pp->name, "phandle") ||
1620                     !strcmp(pp->name, "linux,phandle"))
1621                         continue;
1622
1623                 np = of_find_node_by_path(pp->value);
1624                 if (!np)
1625                         continue;
1626
1627                 /* walk the alias backwards to extract the id and work out
1628                  * the 'stem' string */
1629                 while (isdigit(*(end-1)) && end > start)
1630                         end--;
1631                 len = end - start;
1632
1633                 if (kstrtoint(end, 10, &id) < 0)
1634                         continue;
1635
1636                 /* Allocate an alias_prop with enough space for the stem */
1637                 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1638                 if (!ap)
1639                         continue;
1640                 memset(ap, 0, sizeof(*ap) + len + 1);
1641                 ap->alias = start;
1642                 of_alias_add(ap, np, id, start, len);
1643         }
1644 }
1645
1646 /**
1647  * of_alias_get_id - Get alias id for the given device_node
1648  * @np:         Pointer to the given device_node
1649  * @stem:       Alias stem of the given device_node
1650  *
1651  * The function travels the lookup table to get alias id for the given
1652  * device_node and alias stem.  It returns the alias id if find it.
1653  */
1654 int of_alias_get_id(struct device_node *np, const char *stem)
1655 {
1656         struct alias_prop *app;
1657         int id = -ENODEV;
1658
1659         mutex_lock(&of_aliases_mutex);
1660         list_for_each_entry(app, &aliases_lookup, link) {
1661                 if (strcmp(app->stem, stem) != 0)
1662                         continue;
1663
1664                 if (np == app->np) {
1665                         id = app->id;
1666                         break;
1667                 }
1668         }
1669         mutex_unlock(&of_aliases_mutex);
1670
1671         return id;
1672 }
1673 EXPORT_SYMBOL_GPL(of_alias_get_id);
1674
1675 /**
1676  * of_alias_get_max_id - Get maximim alias id for the given stem
1677  * @stem:       Alias stem of the given device_node
1678  *
1679  * The function travels the lookup table to get maximum alias id for
1680  * the given alias stem.  It returns the maximum alias id if find it.
1681  */
1682 int of_alias_get_max_id(const char *stem)
1683 {
1684         struct alias_prop *app;
1685         int id = -ENODEV;
1686
1687         mutex_lock(&of_aliases_mutex);
1688         list_for_each_entry(app, &aliases_lookup, link) {
1689                 if (strcmp(app->stem, stem) != 0)
1690                         continue;
1691
1692                 id = (app->id > id) ? app->id : id;
1693         }
1694         mutex_unlock(&of_aliases_mutex);
1695
1696         return id;
1697 }
1698 EXPORT_SYMBOL_GPL(of_alias_get_max_id);
1699
1700 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1701                                u32 *pu)
1702 {
1703         const void *curv = cur;
1704
1705         if (!prop)
1706                 return NULL;
1707
1708         if (!cur) {
1709                 curv = prop->value;
1710                 goto out_val;
1711         }
1712
1713         curv += sizeof(*cur);
1714         if (curv >= prop->value + prop->length)
1715                 return NULL;
1716
1717 out_val:
1718         *pu = be32_to_cpup(curv);
1719         return curv;
1720 }
1721 EXPORT_SYMBOL_GPL(of_prop_next_u32);
1722
1723 const char *of_prop_next_string(struct property *prop, const char *cur)
1724 {
1725         const void *curv = cur;
1726
1727         if (!prop)
1728                 return NULL;
1729
1730         if (!cur)
1731                 return prop->value;
1732
1733         curv += strlen(cur) + 1;
1734         if (curv >= prop->value + prop->length)
1735                 return NULL;
1736
1737         return curv;
1738 }
1739 EXPORT_SYMBOL_GPL(of_prop_next_string);
1740
1741
1742 /**
1743  * of_graph_parse_endpoint() - parse common endpoint node properties
1744  * @node: pointer to endpoint device_node
1745  * @endpoint: pointer to the OF endpoint data structure
1746  *
1747  * The caller should hold a reference to @node.
1748  */
1749 int of_graph_parse_endpoint(const struct device_node *node,
1750                             struct of_endpoint *endpoint)
1751 {
1752         struct device_node *port_node = of_get_parent(node);
1753
1754         WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
1755                   __func__, node->full_name);
1756
1757         memset(endpoint, 0, sizeof(*endpoint));
1758
1759         endpoint->local_node = node;
1760         /*
1761          * It doesn't matter whether the two calls below succeed.
1762          * If they don't then the default value 0 is used.
1763          */
1764         of_property_read_u32(port_node, "reg", &endpoint->port);
1765         of_property_read_u32(node, "reg", &endpoint->id);
1766
1767         of_node_put(port_node);
1768
1769         return 0;
1770 }
1771 EXPORT_SYMBOL(of_graph_parse_endpoint);
1772
1773 /**
1774  * of_property_read_u64_array - Find and read an array of 64 bit integers
1775  * from a property.
1776  *
1777  * @np:         device node from which the property value is to be read.
1778  * @propname:   name of the property to be searched.
1779  * @out_values: pointer to return value, modified only if return value is 0.
1780  * @sz:         number of array elements to read
1781  *
1782  * Search for a property in a device node and read 64-bit value(s) from
1783  * it. Returns 0 on success, -EINVAL if the property does not exist,
1784  * -ENODATA if property does not have a value, and -EOVERFLOW if the
1785  * property data isn't large enough.
1786  *
1787  * The out_values is modified only if a valid u64 value can be decoded.
1788  */
1789 int of_property_read_u64_array(const struct device_node *np,
1790                                const char *propname, u64 *out_values,
1791                                size_t sz)
1792 {
1793         const __be32 *val = of_find_property_value_of_size(np, propname,
1794                                                 (sz * sizeof(*out_values)));
1795
1796         if (IS_ERR(val))
1797                 return PTR_ERR(val);
1798
1799         while (sz--) {
1800                 *out_values++ = of_read_number(val, 2);
1801                 val += 2;
1802         }
1803         return 0;
1804 }
1805 EXPORT_SYMBOL_GPL(of_property_read_u64_array);
1806
1807
1808 /**
1809  * of_graph_get_next_endpoint() - get next endpoint node
1810  * @parent: pointer to the parent device node
1811  * @prev: previous endpoint node, or NULL to get first
1812  *
1813  * Return: An 'endpoint' node pointer with refcount incremented. Refcount
1814  * of the passed @prev node is decremented.
1815  */
1816 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
1817                                         struct device_node *prev)
1818 {
1819         struct device_node *endpoint;
1820         struct device_node *port;
1821
1822         if (!parent)
1823                 return NULL;
1824
1825         /*
1826          * Start by locating the port node. If no previous endpoint is specified
1827          * search for the first port node, otherwise get the previous endpoint
1828          * parent port node.
1829          */
1830         if (!prev) {
1831                 struct device_node *node;
1832
1833                 node = of_get_child_by_name(parent, "ports");
1834                 if (node)
1835                         parent = node;
1836
1837                 port = of_get_child_by_name(parent, "port");
1838                 of_node_put(node);
1839
1840                 if (!port) {
1841                         pr_err("%s(): no port node found in %s\n",
1842                                __func__, parent->full_name);
1843                         return NULL;
1844                 }
1845         } else {
1846                 port = of_get_parent(prev);
1847                 if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
1848                               __func__, prev->full_name))
1849                         return NULL;
1850         }
1851
1852         while (1) {
1853                 /*
1854                  * Now that we have a port node, get the next endpoint by
1855                  * getting the next child. If the previous endpoint is NULL this
1856                  * will return the first child.
1857                  */
1858                 endpoint = of_get_next_child(port, prev);
1859                 if (endpoint) {
1860                         of_node_put(port);
1861                         return endpoint;
1862                 }
1863
1864                 /* No more endpoints under this port, try the next one. */
1865                 prev = NULL;
1866
1867                 do {
1868                         port = of_get_next_child(parent, port);
1869                         if (!port)
1870                                 return NULL;
1871                 } while (of_node_cmp(port->name, "port"));
1872         }
1873 }
1874 EXPORT_SYMBOL(of_graph_get_next_endpoint);
1875
1876 /**
1877  * of_graph_get_remote_port_parent() - get remote port's parent node
1878  * @node: pointer to a local endpoint device_node
1879  *
1880  * Return: Remote device node associated with remote endpoint node linked
1881  *         to @node. Use of_node_put() on it when done.
1882  */
1883 struct device_node *of_graph_get_remote_port_parent(
1884                                const struct device_node *node)
1885 {
1886         struct device_node *np;
1887         unsigned int depth;
1888
1889         /* Get remote endpoint node. */
1890         np = of_parse_phandle(node, "remote-endpoint", 0);
1891
1892         /* Walk 3 levels up only if there is 'ports' node. */
1893         for (depth = 3; depth && np; depth--) {
1894                 np = of_get_next_parent(np);
1895                 if (depth == 2 && of_node_cmp(np->name, "ports"))
1896                         break;
1897         }
1898         return np;
1899 }
1900 EXPORT_SYMBOL(of_graph_get_remote_port_parent);