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