of: typo fix in __of_prop_dup()
[pandora-kernel.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/cpu.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 #include <linux/of_graph.h>
25 #include <linux/spinlock.h>
26 #include <linux/slab.h>
27 #include <linux/string.h>
28 #include <linux/proc_fs.h>
29
30 #include "of_private.h"
31
32 LIST_HEAD(aliases_lookup);
33
34 struct device_node *of_allnodes;
35 EXPORT_SYMBOL(of_allnodes);
36 struct device_node *of_chosen;
37 struct device_node *of_aliases;
38 static struct device_node *of_stdout;
39
40 struct kset *of_kset;
41
42 /*
43  * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
44  * This mutex must be held whenever modifications are being made to the
45  * device tree. The of_{attach,detach}_node() and
46  * of_{add,remove,update}_property() helpers make sure this happens.
47  */
48 DEFINE_MUTEX(of_mutex);
49
50 /* use when traversing tree through the allnext, child, sibling,
51  * or parent members of struct device_node.
52  */
53 DEFINE_RAW_SPINLOCK(devtree_lock);
54
55 int of_n_addr_cells(struct device_node *np)
56 {
57         const __be32 *ip;
58
59         do {
60                 if (np->parent)
61                         np = np->parent;
62                 ip = of_get_property(np, "#address-cells", NULL);
63                 if (ip)
64                         return be32_to_cpup(ip);
65         } while (np->parent);
66         /* No #address-cells property for the root node */
67         return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
68 }
69 EXPORT_SYMBOL(of_n_addr_cells);
70
71 int of_n_size_cells(struct device_node *np)
72 {
73         const __be32 *ip;
74
75         do {
76                 if (np->parent)
77                         np = np->parent;
78                 ip = of_get_property(np, "#size-cells", NULL);
79                 if (ip)
80                         return be32_to_cpup(ip);
81         } while (np->parent);
82         /* No #size-cells property for the root node */
83         return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
84 }
85 EXPORT_SYMBOL(of_n_size_cells);
86
87 #ifdef CONFIG_NUMA
88 int __weak of_node_to_nid(struct device_node *np)
89 {
90         return numa_node_id();
91 }
92 #endif
93
94 #ifndef CONFIG_OF_DYNAMIC
95 static void of_node_release(struct kobject *kobj)
96 {
97         /* Without CONFIG_OF_DYNAMIC, no nodes gets freed */
98 }
99 #endif /* CONFIG_OF_DYNAMIC */
100
101 struct kobj_type of_node_ktype = {
102         .release = of_node_release,
103 };
104
105 static ssize_t of_node_property_read(struct file *filp, struct kobject *kobj,
106                                 struct bin_attribute *bin_attr, char *buf,
107                                 loff_t offset, size_t count)
108 {
109         struct property *pp = container_of(bin_attr, struct property, attr);
110         return memory_read_from_buffer(buf, count, &offset, pp->value, pp->length);
111 }
112
113 static const char *safe_name(struct kobject *kobj, const char *orig_name)
114 {
115         const char *name = orig_name;
116         struct kernfs_node *kn;
117         int i = 0;
118
119         /* don't be a hero. After 16 tries give up */
120         while (i < 16 && (kn = sysfs_get_dirent(kobj->sd, name))) {
121                 sysfs_put(kn);
122                 if (name != orig_name)
123                         kfree(name);
124                 name = kasprintf(GFP_KERNEL, "%s#%i", orig_name, ++i);
125         }
126
127         if (name != orig_name)
128                 pr_warn("device-tree: Duplicate name in %s, renamed to \"%s\"\n",
129                         kobject_name(kobj), name);
130         return name;
131 }
132
133 int __of_add_property_sysfs(struct device_node *np, struct property *pp)
134 {
135         int rc;
136
137         /* Important: Don't leak passwords */
138         bool secure = strncmp(pp->name, "security-", 9) == 0;
139
140         if (!of_kset || !of_node_is_attached(np))
141                 return 0;
142
143         sysfs_bin_attr_init(&pp->attr);
144         pp->attr.attr.name = safe_name(&np->kobj, pp->name);
145         pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
146         pp->attr.size = secure ? 0 : pp->length;
147         pp->attr.read = of_node_property_read;
148
149         rc = sysfs_create_bin_file(&np->kobj, &pp->attr);
150         WARN(rc, "error adding attribute %s to node %s\n", pp->name, np->full_name);
151         return rc;
152 }
153
154 int __of_attach_node_sysfs(struct device_node *np)
155 {
156         const char *name;
157         struct property *pp;
158         int rc;
159
160         if (!of_kset)
161                 return 0;
162
163         np->kobj.kset = of_kset;
164         if (!np->parent) {
165                 /* Nodes without parents are new top level trees */
166                 rc = kobject_add(&np->kobj, NULL, "%s",
167                                  safe_name(&of_kset->kobj, "base"));
168         } else {
169                 name = safe_name(&np->parent->kobj, kbasename(np->full_name));
170                 if (!name || !name[0])
171                         return -EINVAL;
172
173                 rc = kobject_add(&np->kobj, &np->parent->kobj, "%s", name);
174         }
175         if (rc)
176                 return rc;
177
178         for_each_property_of_node(np, pp)
179                 __of_add_property_sysfs(np, pp);
180
181         return 0;
182 }
183
184 static int __init of_init(void)
185 {
186         struct device_node *np;
187
188         /* Create the kset, and register existing nodes */
189         mutex_lock(&of_mutex);
190         of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
191         if (!of_kset) {
192                 mutex_unlock(&of_mutex);
193                 return -ENOMEM;
194         }
195         for_each_of_allnodes(np)
196                 __of_attach_node_sysfs(np);
197         mutex_unlock(&of_mutex);
198
199         /* Symlink in /proc as required by userspace ABI */
200         if (of_allnodes)
201                 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
202
203         return 0;
204 }
205 core_initcall(of_init);
206
207 static struct property *__of_find_property(const struct device_node *np,
208                                            const char *name, int *lenp)
209 {
210         struct property *pp;
211
212         if (!np)
213                 return NULL;
214
215         for (pp = np->properties; pp; pp = pp->next) {
216                 if (of_prop_cmp(pp->name, name) == 0) {
217                         if (lenp)
218                                 *lenp = pp->length;
219                         break;
220                 }
221         }
222
223         return pp;
224 }
225
226 struct property *of_find_property(const struct device_node *np,
227                                   const char *name,
228                                   int *lenp)
229 {
230         struct property *pp;
231         unsigned long flags;
232
233         raw_spin_lock_irqsave(&devtree_lock, flags);
234         pp = __of_find_property(np, name, lenp);
235         raw_spin_unlock_irqrestore(&devtree_lock, flags);
236
237         return pp;
238 }
239 EXPORT_SYMBOL(of_find_property);
240
241 /**
242  * of_find_all_nodes - Get next node in global list
243  * @prev:       Previous node or NULL to start iteration
244  *              of_node_put() will be called on it
245  *
246  * Returns a node pointer with refcount incremented, use
247  * of_node_put() on it when done.
248  */
249 struct device_node *of_find_all_nodes(struct device_node *prev)
250 {
251         struct device_node *np;
252         unsigned long flags;
253
254         raw_spin_lock_irqsave(&devtree_lock, flags);
255         np = prev ? prev->allnext : of_allnodes;
256         for (; np != NULL; np = np->allnext)
257                 if (of_node_get(np))
258                         break;
259         of_node_put(prev);
260         raw_spin_unlock_irqrestore(&devtree_lock, flags);
261         return np;
262 }
263 EXPORT_SYMBOL(of_find_all_nodes);
264
265 /*
266  * Find a property with a given name for a given node
267  * and return the value.
268  */
269 const void *__of_get_property(const struct device_node *np,
270                               const char *name, int *lenp)
271 {
272         struct property *pp = __of_find_property(np, name, lenp);
273
274         return pp ? pp->value : NULL;
275 }
276
277 /*
278  * Find a property with a given name for a given node
279  * and return the value.
280  */
281 const void *of_get_property(const struct device_node *np, const char *name,
282                             int *lenp)
283 {
284         struct property *pp = of_find_property(np, name, lenp);
285
286         return pp ? pp->value : NULL;
287 }
288 EXPORT_SYMBOL(of_get_property);
289
290 /*
291  * arch_match_cpu_phys_id - Match the given logical CPU and physical id
292  *
293  * @cpu: logical cpu index of a core/thread
294  * @phys_id: physical identifier of a core/thread
295  *
296  * CPU logical to physical index mapping is architecture specific.
297  * However this __weak function provides a default match of physical
298  * id to logical cpu index. phys_id provided here is usually values read
299  * from the device tree which must match the hardware internal registers.
300  *
301  * Returns true if the physical identifier and the logical cpu index
302  * correspond to the same core/thread, false otherwise.
303  */
304 bool __weak arch_match_cpu_phys_id(int cpu, u64 phys_id)
305 {
306         return (u32)phys_id == cpu;
307 }
308
309 /**
310  * Checks if the given "prop_name" property holds the physical id of the
311  * core/thread corresponding to the logical cpu 'cpu'. If 'thread' is not
312  * NULL, local thread number within the core is returned in it.
313  */
314 static bool __of_find_n_match_cpu_property(struct device_node *cpun,
315                         const char *prop_name, int cpu, unsigned int *thread)
316 {
317         const __be32 *cell;
318         int ac, prop_len, tid;
319         u64 hwid;
320
321         ac = of_n_addr_cells(cpun);
322         cell = of_get_property(cpun, prop_name, &prop_len);
323         if (!cell || !ac)
324                 return false;
325         prop_len /= sizeof(*cell) * ac;
326         for (tid = 0; tid < prop_len; tid++) {
327                 hwid = of_read_number(cell, ac);
328                 if (arch_match_cpu_phys_id(cpu, hwid)) {
329                         if (thread)
330                                 *thread = tid;
331                         return true;
332                 }
333                 cell += ac;
334         }
335         return false;
336 }
337
338 /*
339  * arch_find_n_match_cpu_physical_id - See if the given device node is
340  * for the cpu corresponding to logical cpu 'cpu'.  Return true if so,
341  * else false.  If 'thread' is non-NULL, the local thread number within the
342  * core is returned in it.
343  */
344 bool __weak arch_find_n_match_cpu_physical_id(struct device_node *cpun,
345                                               int cpu, unsigned int *thread)
346 {
347         /* Check for non-standard "ibm,ppc-interrupt-server#s" property
348          * for thread ids on PowerPC. If it doesn't exist fallback to
349          * standard "reg" property.
350          */
351         if (IS_ENABLED(CONFIG_PPC) &&
352             __of_find_n_match_cpu_property(cpun,
353                                            "ibm,ppc-interrupt-server#s",
354                                            cpu, thread))
355                 return true;
356
357         if (__of_find_n_match_cpu_property(cpun, "reg", cpu, thread))
358                 return true;
359
360         return false;
361 }
362
363 /**
364  * of_get_cpu_node - Get device node associated with the given logical CPU
365  *
366  * @cpu: CPU number(logical index) for which device node is required
367  * @thread: if not NULL, local thread number within the physical core is
368  *          returned
369  *
370  * The main purpose of this function is to retrieve the device node for the
371  * given logical CPU index. It should be used to initialize the of_node in
372  * cpu device. Once of_node in cpu device is populated, all the further
373  * references can use that instead.
374  *
375  * CPU logical to physical index mapping is architecture specific and is built
376  * before booting secondary cores. This function uses arch_match_cpu_phys_id
377  * which can be overridden by architecture specific implementation.
378  *
379  * Returns a node pointer for the logical cpu if found, else NULL.
380  */
381 struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
382 {
383         struct device_node *cpun;
384
385         for_each_node_by_type(cpun, "cpu") {
386                 if (arch_find_n_match_cpu_physical_id(cpun, cpu, thread))
387                         return cpun;
388         }
389         return NULL;
390 }
391 EXPORT_SYMBOL(of_get_cpu_node);
392
393 /**
394  * __of_device_is_compatible() - Check if the node matches given constraints
395  * @device: pointer to node
396  * @compat: required compatible string, NULL or "" for any match
397  * @type: required device_type value, NULL or "" for any match
398  * @name: required node name, NULL or "" for any match
399  *
400  * Checks if the given @compat, @type and @name strings match the
401  * properties of the given @device. A constraints can be skipped by
402  * passing NULL or an empty string as the constraint.
403  *
404  * Returns 0 for no match, and a positive integer on match. The return
405  * value is a relative score with larger values indicating better
406  * matches. The score is weighted for the most specific compatible value
407  * to get the highest score. Matching type is next, followed by matching
408  * name. Practically speaking, this results in the following priority
409  * order for matches:
410  *
411  * 1. specific compatible && type && name
412  * 2. specific compatible && type
413  * 3. specific compatible && name
414  * 4. specific compatible
415  * 5. general compatible && type && name
416  * 6. general compatible && type
417  * 7. general compatible && name
418  * 8. general compatible
419  * 9. type && name
420  * 10. type
421  * 11. name
422  */
423 static int __of_device_is_compatible(const struct device_node *device,
424                                      const char *compat, const char *type, const char *name)
425 {
426         struct property *prop;
427         const char *cp;
428         int index = 0, score = 0;
429
430         /* Compatible match has highest priority */
431         if (compat && compat[0]) {
432                 prop = __of_find_property(device, "compatible", NULL);
433                 for (cp = of_prop_next_string(prop, NULL); cp;
434                      cp = of_prop_next_string(prop, cp), index++) {
435                         if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
436                                 score = INT_MAX/2 - (index << 2);
437                                 break;
438                         }
439                 }
440                 if (!score)
441                         return 0;
442         }
443
444         /* Matching type is better than matching name */
445         if (type && type[0]) {
446                 if (!device->type || of_node_cmp(type, device->type))
447                         return 0;
448                 score += 2;
449         }
450
451         /* Matching name is a bit better than not */
452         if (name && name[0]) {
453                 if (!device->name || of_node_cmp(name, device->name))
454                         return 0;
455                 score++;
456         }
457
458         return score;
459 }
460
461 /** Checks if the given "compat" string matches one of the strings in
462  * the device's "compatible" property
463  */
464 int of_device_is_compatible(const struct device_node *device,
465                 const char *compat)
466 {
467         unsigned long flags;
468         int res;
469
470         raw_spin_lock_irqsave(&devtree_lock, flags);
471         res = __of_device_is_compatible(device, compat, NULL, NULL);
472         raw_spin_unlock_irqrestore(&devtree_lock, flags);
473         return res;
474 }
475 EXPORT_SYMBOL(of_device_is_compatible);
476
477 /**
478  * of_machine_is_compatible - Test root of device tree for a given compatible value
479  * @compat: compatible string to look for in root node's compatible property.
480  *
481  * Returns true if the root node has the given value in its
482  * compatible property.
483  */
484 int of_machine_is_compatible(const char *compat)
485 {
486         struct device_node *root;
487         int rc = 0;
488
489         root = of_find_node_by_path("/");
490         if (root) {
491                 rc = of_device_is_compatible(root, compat);
492                 of_node_put(root);
493         }
494         return rc;
495 }
496 EXPORT_SYMBOL(of_machine_is_compatible);
497
498 /**
499  *  __of_device_is_available - check if a device is available for use
500  *
501  *  @device: Node to check for availability, with locks already held
502  *
503  *  Returns 1 if the status property is absent or set to "okay" or "ok",
504  *  0 otherwise
505  */
506 static int __of_device_is_available(const struct device_node *device)
507 {
508         const char *status;
509         int statlen;
510
511         if (!device)
512                 return 0;
513
514         status = __of_get_property(device, "status", &statlen);
515         if (status == NULL)
516                 return 1;
517
518         if (statlen > 0) {
519                 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
520                         return 1;
521         }
522
523         return 0;
524 }
525
526 /**
527  *  of_device_is_available - check if a device is available for use
528  *
529  *  @device: Node to check for availability
530  *
531  *  Returns 1 if the status property is absent or set to "okay" or "ok",
532  *  0 otherwise
533  */
534 int of_device_is_available(const struct device_node *device)
535 {
536         unsigned long flags;
537         int res;
538
539         raw_spin_lock_irqsave(&devtree_lock, flags);
540         res = __of_device_is_available(device);
541         raw_spin_unlock_irqrestore(&devtree_lock, flags);
542         return res;
543
544 }
545 EXPORT_SYMBOL(of_device_is_available);
546
547 /**
548  *      of_get_parent - Get a node's parent if any
549  *      @node:  Node to get parent
550  *
551  *      Returns a node pointer with refcount incremented, use
552  *      of_node_put() on it when done.
553  */
554 struct device_node *of_get_parent(const struct device_node *node)
555 {
556         struct device_node *np;
557         unsigned long flags;
558
559         if (!node)
560                 return NULL;
561
562         raw_spin_lock_irqsave(&devtree_lock, flags);
563         np = of_node_get(node->parent);
564         raw_spin_unlock_irqrestore(&devtree_lock, flags);
565         return np;
566 }
567 EXPORT_SYMBOL(of_get_parent);
568
569 /**
570  *      of_get_next_parent - Iterate to a node's parent
571  *      @node:  Node to get parent of
572  *
573  *      This is like of_get_parent() except that it drops the
574  *      refcount on the passed node, making it suitable for iterating
575  *      through a node's parents.
576  *
577  *      Returns a node pointer with refcount incremented, use
578  *      of_node_put() on it when done.
579  */
580 struct device_node *of_get_next_parent(struct device_node *node)
581 {
582         struct device_node *parent;
583         unsigned long flags;
584
585         if (!node)
586                 return NULL;
587
588         raw_spin_lock_irqsave(&devtree_lock, flags);
589         parent = of_node_get(node->parent);
590         of_node_put(node);
591         raw_spin_unlock_irqrestore(&devtree_lock, flags);
592         return parent;
593 }
594 EXPORT_SYMBOL(of_get_next_parent);
595
596 static struct device_node *__of_get_next_child(const struct device_node *node,
597                                                 struct device_node *prev)
598 {
599         struct device_node *next;
600
601         if (!node)
602                 return NULL;
603
604         next = prev ? prev->sibling : node->child;
605         for (; next; next = next->sibling)
606                 if (of_node_get(next))
607                         break;
608         of_node_put(prev);
609         return next;
610 }
611 #define __for_each_child_of_node(parent, child) \
612         for (child = __of_get_next_child(parent, NULL); child != NULL; \
613              child = __of_get_next_child(parent, child))
614
615 /**
616  *      of_get_next_child - Iterate a node childs
617  *      @node:  parent node
618  *      @prev:  previous child of the parent node, or NULL to get first
619  *
620  *      Returns a node pointer with refcount incremented, use
621  *      of_node_put() on it when done.
622  */
623 struct device_node *of_get_next_child(const struct device_node *node,
624         struct device_node *prev)
625 {
626         struct device_node *next;
627         unsigned long flags;
628
629         raw_spin_lock_irqsave(&devtree_lock, flags);
630         next = __of_get_next_child(node, prev);
631         raw_spin_unlock_irqrestore(&devtree_lock, flags);
632         return next;
633 }
634 EXPORT_SYMBOL(of_get_next_child);
635
636 /**
637  *      of_get_next_available_child - Find the next available child node
638  *      @node:  parent node
639  *      @prev:  previous child of the parent node, or NULL to get first
640  *
641  *      This function is like of_get_next_child(), except that it
642  *      automatically skips any disabled nodes (i.e. status = "disabled").
643  */
644 struct device_node *of_get_next_available_child(const struct device_node *node,
645         struct device_node *prev)
646 {
647         struct device_node *next;
648         unsigned long flags;
649
650         if (!node)
651                 return NULL;
652
653         raw_spin_lock_irqsave(&devtree_lock, flags);
654         next = prev ? prev->sibling : node->child;
655         for (; next; next = next->sibling) {
656                 if (!__of_device_is_available(next))
657                         continue;
658                 if (of_node_get(next))
659                         break;
660         }
661         of_node_put(prev);
662         raw_spin_unlock_irqrestore(&devtree_lock, flags);
663         return next;
664 }
665 EXPORT_SYMBOL(of_get_next_available_child);
666
667 /**
668  *      of_get_child_by_name - Find the child node by name for a given parent
669  *      @node:  parent node
670  *      @name:  child name to look for.
671  *
672  *      This function looks for child node for given matching name
673  *
674  *      Returns a node pointer if found, with refcount incremented, use
675  *      of_node_put() on it when done.
676  *      Returns NULL if node is not found.
677  */
678 struct device_node *of_get_child_by_name(const struct device_node *node,
679                                 const char *name)
680 {
681         struct device_node *child;
682
683         for_each_child_of_node(node, child)
684                 if (child->name && (of_node_cmp(child->name, name) == 0))
685                         break;
686         return child;
687 }
688 EXPORT_SYMBOL(of_get_child_by_name);
689
690 static struct device_node *__of_find_node_by_path(struct device_node *parent,
691                                                 const char *path)
692 {
693         struct device_node *child;
694         int len = strchrnul(path, '/') - path;
695
696         if (!len)
697                 return NULL;
698
699         __for_each_child_of_node(parent, child) {
700                 const char *name = strrchr(child->full_name, '/');
701                 if (WARN(!name, "malformed device_node %s\n", child->full_name))
702                         continue;
703                 name++;
704                 if (strncmp(path, name, len) == 0 && (strlen(name) == len))
705                         return child;
706         }
707         return NULL;
708 }
709
710 /**
711  *      of_find_node_by_path - Find a node matching a full OF path
712  *      @path: Either the full path to match, or if the path does not
713  *             start with '/', the name of a property of the /aliases
714  *             node (an alias).  In the case of an alias, the node
715  *             matching the alias' value will be returned.
716  *
717  *      Valid paths:
718  *              /foo/bar        Full path
719  *              foo             Valid alias
720  *              foo/bar         Valid alias + relative path
721  *
722  *      Returns a node pointer with refcount incremented, use
723  *      of_node_put() on it when done.
724  */
725 struct device_node *of_find_node_by_path(const char *path)
726 {
727         struct device_node *np = NULL;
728         struct property *pp;
729         unsigned long flags;
730
731         if (strcmp(path, "/") == 0)
732                 return of_node_get(of_allnodes);
733
734         /* The path could begin with an alias */
735         if (*path != '/') {
736                 char *p = strchrnul(path, '/');
737                 int len = p - path;
738
739                 /* of_aliases must not be NULL */
740                 if (!of_aliases)
741                         return NULL;
742
743                 for_each_property_of_node(of_aliases, pp) {
744                         if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
745                                 np = of_find_node_by_path(pp->value);
746                                 break;
747                         }
748                 }
749                 if (!np)
750                         return NULL;
751                 path = p;
752         }
753
754         /* Step down the tree matching path components */
755         raw_spin_lock_irqsave(&devtree_lock, flags);
756         if (!np)
757                 np = of_node_get(of_allnodes);
758         while (np && *path == '/') {
759                 path++; /* Increment past '/' delimiter */
760                 np = __of_find_node_by_path(np, path);
761                 path = strchrnul(path, '/');
762         }
763         raw_spin_unlock_irqrestore(&devtree_lock, flags);
764         return np;
765 }
766 EXPORT_SYMBOL(of_find_node_by_path);
767
768 /**
769  *      of_find_node_by_name - Find a node by its "name" property
770  *      @from:  The node to start searching from or NULL, the node
771  *              you pass will not be searched, only the next one
772  *              will; typically, you pass what the previous call
773  *              returned. of_node_put() will be called on it
774  *      @name:  The name string to match against
775  *
776  *      Returns a node pointer with refcount incremented, use
777  *      of_node_put() on it when done.
778  */
779 struct device_node *of_find_node_by_name(struct device_node *from,
780         const char *name)
781 {
782         struct device_node *np;
783         unsigned long flags;
784
785         raw_spin_lock_irqsave(&devtree_lock, flags);
786         np = from ? from->allnext : of_allnodes;
787         for (; np; np = np->allnext)
788                 if (np->name && (of_node_cmp(np->name, name) == 0)
789                     && of_node_get(np))
790                         break;
791         of_node_put(from);
792         raw_spin_unlock_irqrestore(&devtree_lock, flags);
793         return np;
794 }
795 EXPORT_SYMBOL(of_find_node_by_name);
796
797 /**
798  *      of_find_node_by_type - Find a node by its "device_type" property
799  *      @from:  The node to start searching from, or NULL to start searching
800  *              the entire device tree. The node you pass will not be
801  *              searched, only the next one will; typically, you pass
802  *              what the previous call returned. of_node_put() will be
803  *              called on from for you.
804  *      @type:  The type string to match against
805  *
806  *      Returns a node pointer with refcount incremented, use
807  *      of_node_put() on it when done.
808  */
809 struct device_node *of_find_node_by_type(struct device_node *from,
810         const char *type)
811 {
812         struct device_node *np;
813         unsigned long flags;
814
815         raw_spin_lock_irqsave(&devtree_lock, flags);
816         np = from ? from->allnext : of_allnodes;
817         for (; np; np = np->allnext)
818                 if (np->type && (of_node_cmp(np->type, type) == 0)
819                     && of_node_get(np))
820                         break;
821         of_node_put(from);
822         raw_spin_unlock_irqrestore(&devtree_lock, flags);
823         return np;
824 }
825 EXPORT_SYMBOL(of_find_node_by_type);
826
827 /**
828  *      of_find_compatible_node - Find a node based on type and one of the
829  *                                tokens in its "compatible" property
830  *      @from:          The node to start searching from or NULL, the node
831  *                      you pass will not be searched, only the next one
832  *                      will; typically, you pass what the previous call
833  *                      returned. of_node_put() will be called on it
834  *      @type:          The type string to match "device_type" or NULL to ignore
835  *      @compatible:    The string to match to one of the tokens in the device
836  *                      "compatible" list.
837  *
838  *      Returns a node pointer with refcount incremented, use
839  *      of_node_put() on it when done.
840  */
841 struct device_node *of_find_compatible_node(struct device_node *from,
842         const char *type, const char *compatible)
843 {
844         struct device_node *np;
845         unsigned long flags;
846
847         raw_spin_lock_irqsave(&devtree_lock, flags);
848         np = from ? from->allnext : of_allnodes;
849         for (; np; np = np->allnext) {
850                 if (__of_device_is_compatible(np, compatible, type, NULL) &&
851                     of_node_get(np))
852                         break;
853         }
854         of_node_put(from);
855         raw_spin_unlock_irqrestore(&devtree_lock, flags);
856         return np;
857 }
858 EXPORT_SYMBOL(of_find_compatible_node);
859
860 /**
861  *      of_find_node_with_property - Find a node which has a property with
862  *                                   the given name.
863  *      @from:          The node to start searching from or NULL, the node
864  *                      you pass will not be searched, only the next one
865  *                      will; typically, you pass what the previous call
866  *                      returned. of_node_put() will be called on it
867  *      @prop_name:     The name of the property to look for.
868  *
869  *      Returns a node pointer with refcount incremented, use
870  *      of_node_put() on it when done.
871  */
872 struct device_node *of_find_node_with_property(struct device_node *from,
873         const char *prop_name)
874 {
875         struct device_node *np;
876         struct property *pp;
877         unsigned long flags;
878
879         raw_spin_lock_irqsave(&devtree_lock, flags);
880         np = from ? from->allnext : of_allnodes;
881         for (; np; np = np->allnext) {
882                 for (pp = np->properties; pp; pp = pp->next) {
883                         if (of_prop_cmp(pp->name, prop_name) == 0) {
884                                 of_node_get(np);
885                                 goto out;
886                         }
887                 }
888         }
889 out:
890         of_node_put(from);
891         raw_spin_unlock_irqrestore(&devtree_lock, flags);
892         return np;
893 }
894 EXPORT_SYMBOL(of_find_node_with_property);
895
896 static
897 const struct of_device_id *__of_match_node(const struct of_device_id *matches,
898                                            const struct device_node *node)
899 {
900         const struct of_device_id *best_match = NULL;
901         int score, best_score = 0;
902
903         if (!matches)
904                 return NULL;
905
906         for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
907                 score = __of_device_is_compatible(node, matches->compatible,
908                                                   matches->type, matches->name);
909                 if (score > best_score) {
910                         best_match = matches;
911                         best_score = score;
912                 }
913         }
914
915         return best_match;
916 }
917
918 /**
919  * of_match_node - Tell if an device_node has a matching of_match structure
920  *      @matches:       array of of device match structures to search in
921  *      @node:          the of device structure to match against
922  *
923  *      Low level utility function used by device matching.
924  */
925 const struct of_device_id *of_match_node(const struct of_device_id *matches,
926                                          const struct device_node *node)
927 {
928         const struct of_device_id *match;
929         unsigned long flags;
930
931         raw_spin_lock_irqsave(&devtree_lock, flags);
932         match = __of_match_node(matches, node);
933         raw_spin_unlock_irqrestore(&devtree_lock, flags);
934         return match;
935 }
936 EXPORT_SYMBOL(of_match_node);
937
938 /**
939  *      of_find_matching_node_and_match - Find a node based on an of_device_id
940  *                                        match table.
941  *      @from:          The node to start searching from or NULL, the node
942  *                      you pass will not be searched, only the next one
943  *                      will; typically, you pass what the previous call
944  *                      returned. of_node_put() will be called on it
945  *      @matches:       array of of device match structures to search in
946  *      @match          Updated to point at the matches entry which matched
947  *
948  *      Returns a node pointer with refcount incremented, use
949  *      of_node_put() on it when done.
950  */
951 struct device_node *of_find_matching_node_and_match(struct device_node *from,
952                                         const struct of_device_id *matches,
953                                         const struct of_device_id **match)
954 {
955         struct device_node *np;
956         const struct of_device_id *m;
957         unsigned long flags;
958
959         if (match)
960                 *match = NULL;
961
962         raw_spin_lock_irqsave(&devtree_lock, flags);
963         np = from ? from->allnext : of_allnodes;
964         for (; np; np = np->allnext) {
965                 m = __of_match_node(matches, np);
966                 if (m && of_node_get(np)) {
967                         if (match)
968                                 *match = m;
969                         break;
970                 }
971         }
972         of_node_put(from);
973         raw_spin_unlock_irqrestore(&devtree_lock, flags);
974         return np;
975 }
976 EXPORT_SYMBOL(of_find_matching_node_and_match);
977
978 /**
979  * of_modalias_node - Lookup appropriate modalias for a device node
980  * @node:       pointer to a device tree node
981  * @modalias:   Pointer to buffer that modalias value will be copied into
982  * @len:        Length of modalias value
983  *
984  * Based on the value of the compatible property, this routine will attempt
985  * to choose an appropriate modalias value for a particular device tree node.
986  * It does this by stripping the manufacturer prefix (as delimited by a ',')
987  * from the first entry in the compatible list property.
988  *
989  * This routine returns 0 on success, <0 on failure.
990  */
991 int of_modalias_node(struct device_node *node, char *modalias, int len)
992 {
993         const char *compatible, *p;
994         int cplen;
995
996         compatible = of_get_property(node, "compatible", &cplen);
997         if (!compatible || strlen(compatible) > cplen)
998                 return -ENODEV;
999         p = strchr(compatible, ',');
1000         strlcpy(modalias, p ? p + 1 : compatible, len);
1001         return 0;
1002 }
1003 EXPORT_SYMBOL_GPL(of_modalias_node);
1004
1005 /**
1006  * of_find_node_by_phandle - Find a node given a phandle
1007  * @handle:     phandle of the node to find
1008  *
1009  * Returns a node pointer with refcount incremented, use
1010  * of_node_put() on it when done.
1011  */
1012 struct device_node *of_find_node_by_phandle(phandle handle)
1013 {
1014         struct device_node *np;
1015         unsigned long flags;
1016
1017         raw_spin_lock_irqsave(&devtree_lock, flags);
1018         for (np = of_allnodes; np; np = np->allnext)
1019                 if (np->phandle == handle)
1020                         break;
1021         of_node_get(np);
1022         raw_spin_unlock_irqrestore(&devtree_lock, flags);
1023         return np;
1024 }
1025 EXPORT_SYMBOL(of_find_node_by_phandle);
1026
1027 /**
1028  * of_property_count_elems_of_size - Count the number of elements in a property
1029  *
1030  * @np:         device node from which the property value is to be read.
1031  * @propname:   name of the property to be searched.
1032  * @elem_size:  size of the individual element
1033  *
1034  * Search for a property in a device node and count the number of elements of
1035  * size elem_size in it. Returns number of elements on sucess, -EINVAL if the
1036  * property does not exist or its length does not match a multiple of elem_size
1037  * and -ENODATA if the property does not have a value.
1038  */
1039 int of_property_count_elems_of_size(const struct device_node *np,
1040                                 const char *propname, int elem_size)
1041 {
1042         struct property *prop = of_find_property(np, propname, NULL);
1043
1044         if (!prop)
1045                 return -EINVAL;
1046         if (!prop->value)
1047                 return -ENODATA;
1048
1049         if (prop->length % elem_size != 0) {
1050                 pr_err("size of %s in node %s is not a multiple of %d\n",
1051                        propname, np->full_name, elem_size);
1052                 return -EINVAL;
1053         }
1054
1055         return prop->length / elem_size;
1056 }
1057 EXPORT_SYMBOL_GPL(of_property_count_elems_of_size);
1058
1059 /**
1060  * of_find_property_value_of_size
1061  *
1062  * @np:         device node from which the property value is to be read.
1063  * @propname:   name of the property to be searched.
1064  * @len:        requested length of property value
1065  *
1066  * Search for a property in a device node and valid the requested size.
1067  * Returns the property value on success, -EINVAL if the property does not
1068  *  exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
1069  * property data isn't large enough.
1070  *
1071  */
1072 static void *of_find_property_value_of_size(const struct device_node *np,
1073                         const char *propname, u32 len)
1074 {
1075         struct property *prop = of_find_property(np, propname, NULL);
1076
1077         if (!prop)
1078                 return ERR_PTR(-EINVAL);
1079         if (!prop->value)
1080                 return ERR_PTR(-ENODATA);
1081         if (len > prop->length)
1082                 return ERR_PTR(-EOVERFLOW);
1083
1084         return prop->value;
1085 }
1086
1087 /**
1088  * of_property_read_u32_index - Find and read a u32 from a multi-value property.
1089  *
1090  * @np:         device node from which the property value is to be read.
1091  * @propname:   name of the property to be searched.
1092  * @index:      index of the u32 in the list of values
1093  * @out_value:  pointer to return value, modified only if no error.
1094  *
1095  * Search for a property in a device node and read nth 32-bit value from
1096  * it. Returns 0 on success, -EINVAL if the property does not exist,
1097  * -ENODATA if property does not have a value, and -EOVERFLOW if the
1098  * property data isn't large enough.
1099  *
1100  * The out_value is modified only if a valid u32 value can be decoded.
1101  */
1102 int of_property_read_u32_index(const struct device_node *np,
1103                                        const char *propname,
1104                                        u32 index, u32 *out_value)
1105 {
1106         const u32 *val = of_find_property_value_of_size(np, propname,
1107                                         ((index + 1) * sizeof(*out_value)));
1108
1109         if (IS_ERR(val))
1110                 return PTR_ERR(val);
1111
1112         *out_value = be32_to_cpup(((__be32 *)val) + index);
1113         return 0;
1114 }
1115 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
1116
1117 /**
1118  * of_property_read_u8_array - Find and read an array of u8 from a property.
1119  *
1120  * @np:         device node from which the property value is to be read.
1121  * @propname:   name of the property to be searched.
1122  * @out_values: pointer to return value, modified only if return value is 0.
1123  * @sz:         number of array elements to read
1124  *
1125  * Search for a property in a device node and read 8-bit value(s) from
1126  * it. Returns 0 on success, -EINVAL if the property does not exist,
1127  * -ENODATA if property does not have a value, and -EOVERFLOW if the
1128  * property data isn't large enough.
1129  *
1130  * dts entry of array should be like:
1131  *      property = /bits/ 8 <0x50 0x60 0x70>;
1132  *
1133  * The out_values is modified only if a valid u8 value can be decoded.
1134  */
1135 int of_property_read_u8_array(const struct device_node *np,
1136                         const char *propname, u8 *out_values, size_t sz)
1137 {
1138         const u8 *val = of_find_property_value_of_size(np, propname,
1139                                                 (sz * sizeof(*out_values)));
1140
1141         if (IS_ERR(val))
1142                 return PTR_ERR(val);
1143
1144         while (sz--)
1145                 *out_values++ = *val++;
1146         return 0;
1147 }
1148 EXPORT_SYMBOL_GPL(of_property_read_u8_array);
1149
1150 /**
1151  * of_property_read_u16_array - Find and read an array of u16 from a property.
1152  *
1153  * @np:         device node from which the property value is to be read.
1154  * @propname:   name of the property to be searched.
1155  * @out_values: pointer to return value, modified only if return value is 0.
1156  * @sz:         number of array elements to read
1157  *
1158  * Search for a property in a device node and read 16-bit value(s) from
1159  * it. Returns 0 on success, -EINVAL if the property does not exist,
1160  * -ENODATA if property does not have a value, and -EOVERFLOW if the
1161  * property data isn't large enough.
1162  *
1163  * dts entry of array should be like:
1164  *      property = /bits/ 16 <0x5000 0x6000 0x7000>;
1165  *
1166  * The out_values is modified only if a valid u16 value can be decoded.
1167  */
1168 int of_property_read_u16_array(const struct device_node *np,
1169                         const char *propname, u16 *out_values, size_t sz)
1170 {
1171         const __be16 *val = of_find_property_value_of_size(np, propname,
1172                                                 (sz * sizeof(*out_values)));
1173
1174         if (IS_ERR(val))
1175                 return PTR_ERR(val);
1176
1177         while (sz--)
1178                 *out_values++ = be16_to_cpup(val++);
1179         return 0;
1180 }
1181 EXPORT_SYMBOL_GPL(of_property_read_u16_array);
1182
1183 /**
1184  * of_property_read_u32_array - Find and read an array of 32 bit integers
1185  * from a property.
1186  *
1187  * @np:         device node from which the property value is to be read.
1188  * @propname:   name of the property to be searched.
1189  * @out_values: pointer to return value, modified only if return value is 0.
1190  * @sz:         number of array elements to read
1191  *
1192  * Search for a property in a device node and read 32-bit value(s) from
1193  * it. Returns 0 on success, -EINVAL if the property does not exist,
1194  * -ENODATA if property does not have a value, and -EOVERFLOW if the
1195  * property data isn't large enough.
1196  *
1197  * The out_values is modified only if a valid u32 value can be decoded.
1198  */
1199 int of_property_read_u32_array(const struct device_node *np,
1200                                const char *propname, u32 *out_values,
1201                                size_t sz)
1202 {
1203         const __be32 *val = of_find_property_value_of_size(np, propname,
1204                                                 (sz * sizeof(*out_values)));
1205
1206         if (IS_ERR(val))
1207                 return PTR_ERR(val);
1208
1209         while (sz--)
1210                 *out_values++ = be32_to_cpup(val++);
1211         return 0;
1212 }
1213 EXPORT_SYMBOL_GPL(of_property_read_u32_array);
1214
1215 /**
1216  * of_property_read_u64 - Find and read a 64 bit integer from a property
1217  * @np:         device node from which the property value is to be read.
1218  * @propname:   name of the property to be searched.
1219  * @out_value:  pointer to return value, modified only if return value is 0.
1220  *
1221  * Search for a property in a device node and read a 64-bit value from
1222  * it. Returns 0 on success, -EINVAL if the property does not exist,
1223  * -ENODATA if property does not have a value, and -EOVERFLOW if the
1224  * property data isn't large enough.
1225  *
1226  * The out_value is modified only if a valid u64 value can be decoded.
1227  */
1228 int of_property_read_u64(const struct device_node *np, const char *propname,
1229                          u64 *out_value)
1230 {
1231         const __be32 *val = of_find_property_value_of_size(np, propname,
1232                                                 sizeof(*out_value));
1233
1234         if (IS_ERR(val))
1235                 return PTR_ERR(val);
1236
1237         *out_value = of_read_number(val, 2);
1238         return 0;
1239 }
1240 EXPORT_SYMBOL_GPL(of_property_read_u64);
1241
1242 /**
1243  * of_property_read_string - Find and read a string from a property
1244  * @np:         device node from which the property value is to be read.
1245  * @propname:   name of the property to be searched.
1246  * @out_string: pointer to null terminated return string, modified only if
1247  *              return value is 0.
1248  *
1249  * Search for a property in a device tree node and retrieve a null
1250  * terminated string value (pointer to data, not a copy). Returns 0 on
1251  * success, -EINVAL if the property does not exist, -ENODATA if property
1252  * does not have a value, and -EILSEQ if the string is not null-terminated
1253  * within the length of the property data.
1254  *
1255  * The out_string pointer is modified only if a valid string can be decoded.
1256  */
1257 int of_property_read_string(struct device_node *np, const char *propname,
1258                                 const char **out_string)
1259 {
1260         struct property *prop = of_find_property(np, propname, NULL);
1261         if (!prop)
1262                 return -EINVAL;
1263         if (!prop->value)
1264                 return -ENODATA;
1265         if (strnlen(prop->value, prop->length) >= prop->length)
1266                 return -EILSEQ;
1267         *out_string = prop->value;
1268         return 0;
1269 }
1270 EXPORT_SYMBOL_GPL(of_property_read_string);
1271
1272 /**
1273  * of_property_read_string_index - Find and read a string from a multiple
1274  * strings property.
1275  * @np:         device node from which the property value is to be read.
1276  * @propname:   name of the property to be searched.
1277  * @index:      index of the string in the list of strings
1278  * @out_string: pointer to null terminated return string, modified only if
1279  *              return value is 0.
1280  *
1281  * Search for a property in a device tree node and retrieve a null
1282  * terminated string value (pointer to data, not a copy) in the list of strings
1283  * contained in that property.
1284  * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
1285  * property does not have a value, and -EILSEQ if the string is not
1286  * null-terminated within the length of the property data.
1287  *
1288  * The out_string pointer is modified only if a valid string can be decoded.
1289  */
1290 int of_property_read_string_index(struct device_node *np, const char *propname,
1291                                   int index, const char **output)
1292 {
1293         struct property *prop = of_find_property(np, propname, NULL);
1294         int i = 0;
1295         size_t l = 0, total = 0;
1296         const char *p;
1297
1298         if (!prop)
1299                 return -EINVAL;
1300         if (!prop->value)
1301                 return -ENODATA;
1302         if (strnlen(prop->value, prop->length) >= prop->length)
1303                 return -EILSEQ;
1304
1305         p = prop->value;
1306
1307         for (i = 0; total < prop->length; total += l, p += l) {
1308                 l = strlen(p) + 1;
1309                 if (i++ == index) {
1310                         *output = p;
1311                         return 0;
1312                 }
1313         }
1314         return -ENODATA;
1315 }
1316 EXPORT_SYMBOL_GPL(of_property_read_string_index);
1317
1318 /**
1319  * of_property_match_string() - Find string in a list and return index
1320  * @np: pointer to node containing string list property
1321  * @propname: string list property name
1322  * @string: pointer to string to search for in string list
1323  *
1324  * This function searches a string list property and returns the index
1325  * of a specific string value.
1326  */
1327 int of_property_match_string(struct device_node *np, const char *propname,
1328                              const char *string)
1329 {
1330         struct property *prop = of_find_property(np, propname, NULL);
1331         size_t l;
1332         int i;
1333         const char *p, *end;
1334
1335         if (!prop)
1336                 return -EINVAL;
1337         if (!prop->value)
1338                 return -ENODATA;
1339
1340         p = prop->value;
1341         end = p + prop->length;
1342
1343         for (i = 0; p < end; i++, p += l) {
1344                 l = strlen(p) + 1;
1345                 if (p + l > end)
1346                         return -EILSEQ;
1347                 pr_debug("comparing %s with %s\n", string, p);
1348                 if (strcmp(string, p) == 0)
1349                         return i; /* Found it; return index */
1350         }
1351         return -ENODATA;
1352 }
1353 EXPORT_SYMBOL_GPL(of_property_match_string);
1354
1355 /**
1356  * of_property_count_strings - Find and return the number of strings from a
1357  * multiple strings property.
1358  * @np:         device node from which the property value is to be read.
1359  * @propname:   name of the property to be searched.
1360  *
1361  * Search for a property in a device tree node and retrieve the number of null
1362  * terminated string contain in it. Returns the number of strings on
1363  * success, -EINVAL if the property does not exist, -ENODATA if property
1364  * does not have a value, and -EILSEQ if the string is not null-terminated
1365  * within the length of the property data.
1366  */
1367 int of_property_count_strings(struct device_node *np, const char *propname)
1368 {
1369         struct property *prop = of_find_property(np, propname, NULL);
1370         int i = 0;
1371         size_t l = 0, total = 0;
1372         const char *p;
1373
1374         if (!prop)
1375                 return -EINVAL;
1376         if (!prop->value)
1377                 return -ENODATA;
1378         if (strnlen(prop->value, prop->length) >= prop->length)
1379                 return -EILSEQ;
1380
1381         p = prop->value;
1382
1383         for (i = 0; total < prop->length; total += l, p += l, i++)
1384                 l = strlen(p) + 1;
1385
1386         return i;
1387 }
1388 EXPORT_SYMBOL_GPL(of_property_count_strings);
1389
1390 void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
1391 {
1392         int i;
1393         printk("%s %s", msg, of_node_full_name(args->np));
1394         for (i = 0; i < args->args_count; i++)
1395                 printk(i ? ",%08x" : ":%08x", args->args[i]);
1396         printk("\n");
1397 }
1398
1399 static int __of_parse_phandle_with_args(const struct device_node *np,
1400                                         const char *list_name,
1401                                         const char *cells_name,
1402                                         int cell_count, int index,
1403                                         struct of_phandle_args *out_args)
1404 {
1405         const __be32 *list, *list_end;
1406         int rc = 0, size, cur_index = 0;
1407         uint32_t count = 0;
1408         struct device_node *node = NULL;
1409         phandle phandle;
1410
1411         /* Retrieve the phandle list property */
1412         list = of_get_property(np, list_name, &size);
1413         if (!list)
1414                 return -ENOENT;
1415         list_end = list + size / sizeof(*list);
1416
1417         /* Loop over the phandles until all the requested entry is found */
1418         while (list < list_end) {
1419                 rc = -EINVAL;
1420                 count = 0;
1421
1422                 /*
1423                  * If phandle is 0, then it is an empty entry with no
1424                  * arguments.  Skip forward to the next entry.
1425                  */
1426                 phandle = be32_to_cpup(list++);
1427                 if (phandle) {
1428                         /*
1429                          * Find the provider node and parse the #*-cells
1430                          * property to determine the argument length.
1431                          *
1432                          * This is not needed if the cell count is hard-coded
1433                          * (i.e. cells_name not set, but cell_count is set),
1434                          * except when we're going to return the found node
1435                          * below.
1436                          */
1437                         if (cells_name || cur_index == index) {
1438                                 node = of_find_node_by_phandle(phandle);
1439                                 if (!node) {
1440                                         pr_err("%s: could not find phandle\n",
1441                                                 np->full_name);
1442                                         goto err;
1443                                 }
1444                         }
1445
1446                         if (cells_name) {
1447                                 if (of_property_read_u32(node, cells_name,
1448                                                          &count)) {
1449                                         pr_err("%s: could not get %s for %s\n",
1450                                                 np->full_name, cells_name,
1451                                                 node->full_name);
1452                                         goto err;
1453                                 }
1454                         } else {
1455                                 count = cell_count;
1456                         }
1457
1458                         /*
1459                          * Make sure that the arguments actually fit in the
1460                          * remaining property data length
1461                          */
1462                         if (list + count > list_end) {
1463                                 pr_err("%s: arguments longer than property\n",
1464                                          np->full_name);
1465                                 goto err;
1466                         }
1467                 }
1468
1469                 /*
1470                  * All of the error cases above bail out of the loop, so at
1471                  * this point, the parsing is successful. If the requested
1472                  * index matches, then fill the out_args structure and return,
1473                  * or return -ENOENT for an empty entry.
1474                  */
1475                 rc = -ENOENT;
1476                 if (cur_index == index) {
1477                         if (!phandle)
1478                                 goto err;
1479
1480                         if (out_args) {
1481                                 int i;
1482                                 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1483                                         count = MAX_PHANDLE_ARGS;
1484                                 out_args->np = node;
1485                                 out_args->args_count = count;
1486                                 for (i = 0; i < count; i++)
1487                                         out_args->args[i] = be32_to_cpup(list++);
1488                         } else {
1489                                 of_node_put(node);
1490                         }
1491
1492                         /* Found it! return success */
1493                         return 0;
1494                 }
1495
1496                 of_node_put(node);
1497                 node = NULL;
1498                 list += count;
1499                 cur_index++;
1500         }
1501
1502         /*
1503          * Unlock node before returning result; will be one of:
1504          * -ENOENT : index is for empty phandle
1505          * -EINVAL : parsing error on data
1506          * [1..n]  : Number of phandle (count mode; when index = -1)
1507          */
1508         rc = index < 0 ? cur_index : -ENOENT;
1509  err:
1510         if (node)
1511                 of_node_put(node);
1512         return rc;
1513 }
1514
1515 /**
1516  * of_parse_phandle - Resolve a phandle property to a device_node pointer
1517  * @np: Pointer to device node holding phandle property
1518  * @phandle_name: Name of property holding a phandle value
1519  * @index: For properties holding a table of phandles, this is the index into
1520  *         the table
1521  *
1522  * Returns the device_node pointer with refcount incremented.  Use
1523  * of_node_put() on it when done.
1524  */
1525 struct device_node *of_parse_phandle(const struct device_node *np,
1526                                      const char *phandle_name, int index)
1527 {
1528         struct of_phandle_args args;
1529
1530         if (index < 0)
1531                 return NULL;
1532
1533         if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0,
1534                                          index, &args))
1535                 return NULL;
1536
1537         return args.np;
1538 }
1539 EXPORT_SYMBOL(of_parse_phandle);
1540
1541 /**
1542  * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1543  * @np:         pointer to a device tree node containing a list
1544  * @list_name:  property name that contains a list
1545  * @cells_name: property name that specifies phandles' arguments count
1546  * @index:      index of a phandle to parse out
1547  * @out_args:   optional pointer to output arguments structure (will be filled)
1548  *
1549  * This function is useful to parse lists of phandles and their arguments.
1550  * Returns 0 on success and fills out_args, on error returns appropriate
1551  * errno value.
1552  *
1553  * Caller is responsible to call of_node_put() on the returned out_args->node
1554  * pointer.
1555  *
1556  * Example:
1557  *
1558  * phandle1: node1 {
1559  *      #list-cells = <2>;
1560  * }
1561  *
1562  * phandle2: node2 {
1563  *      #list-cells = <1>;
1564  * }
1565  *
1566  * node3 {
1567  *      list = <&phandle1 1 2 &phandle2 3>;
1568  * }
1569  *
1570  * To get a device_node of the `node2' node you may call this:
1571  * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1572  */
1573 int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1574                                 const char *cells_name, int index,
1575                                 struct of_phandle_args *out_args)
1576 {
1577         if (index < 0)
1578                 return -EINVAL;
1579         return __of_parse_phandle_with_args(np, list_name, cells_name, 0,
1580                                             index, out_args);
1581 }
1582 EXPORT_SYMBOL(of_parse_phandle_with_args);
1583
1584 /**
1585  * of_parse_phandle_with_fixed_args() - Find a node pointed by phandle in a list
1586  * @np:         pointer to a device tree node containing a list
1587  * @list_name:  property name that contains a list
1588  * @cell_count: number of argument cells following the phandle
1589  * @index:      index of a phandle to parse out
1590  * @out_args:   optional pointer to output arguments structure (will be filled)
1591  *
1592  * This function is useful to parse lists of phandles and their arguments.
1593  * Returns 0 on success and fills out_args, on error returns appropriate
1594  * errno value.
1595  *
1596  * Caller is responsible to call of_node_put() on the returned out_args->node
1597  * pointer.
1598  *
1599  * Example:
1600  *
1601  * phandle1: node1 {
1602  * }
1603  *
1604  * phandle2: node2 {
1605  * }
1606  *
1607  * node3 {
1608  *      list = <&phandle1 0 2 &phandle2 2 3>;
1609  * }
1610  *
1611  * To get a device_node of the `node2' node you may call this:
1612  * of_parse_phandle_with_fixed_args(node3, "list", 2, 1, &args);
1613  */
1614 int of_parse_phandle_with_fixed_args(const struct device_node *np,
1615                                 const char *list_name, int cell_count,
1616                                 int index, struct of_phandle_args *out_args)
1617 {
1618         if (index < 0)
1619                 return -EINVAL;
1620         return __of_parse_phandle_with_args(np, list_name, NULL, cell_count,
1621                                            index, out_args);
1622 }
1623 EXPORT_SYMBOL(of_parse_phandle_with_fixed_args);
1624
1625 /**
1626  * of_count_phandle_with_args() - Find the number of phandles references in a property
1627  * @np:         pointer to a device tree node containing a list
1628  * @list_name:  property name that contains a list
1629  * @cells_name: property name that specifies phandles' arguments count
1630  *
1631  * Returns the number of phandle + argument tuples within a property. It
1632  * is a typical pattern to encode a list of phandle and variable
1633  * arguments into a single property. The number of arguments is encoded
1634  * by a property in the phandle-target node. For example, a gpios
1635  * property would contain a list of GPIO specifies consisting of a
1636  * phandle and 1 or more arguments. The number of arguments are
1637  * determined by the #gpio-cells property in the node pointed to by the
1638  * phandle.
1639  */
1640 int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1641                                 const char *cells_name)
1642 {
1643         return __of_parse_phandle_with_args(np, list_name, cells_name, 0, -1,
1644                                             NULL);
1645 }
1646 EXPORT_SYMBOL(of_count_phandle_with_args);
1647
1648 /**
1649  * __of_add_property - Add a property to a node without lock operations
1650  */
1651 int __of_add_property(struct device_node *np, struct property *prop)
1652 {
1653         struct property **next;
1654
1655         prop->next = NULL;
1656         next = &np->properties;
1657         while (*next) {
1658                 if (strcmp(prop->name, (*next)->name) == 0)
1659                         /* duplicate ! don't insert it */
1660                         return -EEXIST;
1661
1662                 next = &(*next)->next;
1663         }
1664         *next = prop;
1665
1666         return 0;
1667 }
1668
1669 /**
1670  * of_add_property - Add a property to a node
1671  */
1672 int of_add_property(struct device_node *np, struct property *prop)
1673 {
1674         unsigned long flags;
1675         int rc;
1676
1677         mutex_lock(&of_mutex);
1678
1679         raw_spin_lock_irqsave(&devtree_lock, flags);
1680         rc = __of_add_property(np, prop);
1681         raw_spin_unlock_irqrestore(&devtree_lock, flags);
1682
1683         if (!rc)
1684                 __of_add_property_sysfs(np, prop);
1685
1686         mutex_unlock(&of_mutex);
1687
1688         if (!rc)
1689                 of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
1690
1691         return rc;
1692 }
1693
1694 int __of_remove_property(struct device_node *np, struct property *prop)
1695 {
1696         struct property **next;
1697
1698         for (next = &np->properties; *next; next = &(*next)->next) {
1699                 if (*next == prop)
1700                         break;
1701         }
1702         if (*next == NULL)
1703                 return -ENODEV;
1704
1705         /* found the node */
1706         *next = prop->next;
1707         prop->next = np->deadprops;
1708         np->deadprops = prop;
1709
1710         return 0;
1711 }
1712
1713 void __of_remove_property_sysfs(struct device_node *np, struct property *prop)
1714 {
1715         /* at early boot, bail here and defer setup to of_init() */
1716         if (of_kset && of_node_is_attached(np))
1717                 sysfs_remove_bin_file(&np->kobj, &prop->attr);
1718 }
1719
1720 /**
1721  * of_remove_property - Remove a property from a node.
1722  *
1723  * Note that we don't actually remove it, since we have given out
1724  * who-knows-how-many pointers to the data using get-property.
1725  * Instead we just move the property to the "dead properties"
1726  * list, so it won't be found any more.
1727  */
1728 int of_remove_property(struct device_node *np, struct property *prop)
1729 {
1730         unsigned long flags;
1731         int rc;
1732
1733         mutex_lock(&of_mutex);
1734
1735         raw_spin_lock_irqsave(&devtree_lock, flags);
1736         rc = __of_remove_property(np, prop);
1737         raw_spin_unlock_irqrestore(&devtree_lock, flags);
1738
1739         if (!rc)
1740                 __of_remove_property_sysfs(np, prop);
1741
1742         mutex_unlock(&of_mutex);
1743
1744         if (!rc)
1745                 of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
1746
1747         return rc;
1748 }
1749
1750 int __of_update_property(struct device_node *np, struct property *newprop,
1751                 struct property **oldpropp)
1752 {
1753         struct property **next, *oldprop;
1754
1755         for (next = &np->properties; *next; next = &(*next)->next) {
1756                 if (of_prop_cmp((*next)->name, newprop->name) == 0)
1757                         break;
1758         }
1759         *oldpropp = oldprop = *next;
1760
1761         if (oldprop) {
1762                 /* replace the node */
1763                 newprop->next = oldprop->next;
1764                 *next = newprop;
1765                 oldprop->next = np->deadprops;
1766                 np->deadprops = oldprop;
1767         } else {
1768                 /* new node */
1769                 newprop->next = NULL;
1770                 *next = newprop;
1771         }
1772
1773         return 0;
1774 }
1775
1776 void __of_update_property_sysfs(struct device_node *np, struct property *newprop,
1777                 struct property *oldprop)
1778 {
1779         /* At early boot, bail out and defer setup to of_init() */
1780         if (!of_kset)
1781                 return;
1782
1783         if (oldprop)
1784                 sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
1785         __of_add_property_sysfs(np, newprop);
1786 }
1787
1788 /*
1789  * of_update_property - Update a property in a node, if the property does
1790  * not exist, add it.
1791  *
1792  * Note that we don't actually remove it, since we have given out
1793  * who-knows-how-many pointers to the data using get-property.
1794  * Instead we just move the property to the "dead properties" list,
1795  * and add the new property to the property list
1796  */
1797 int of_update_property(struct device_node *np, struct property *newprop)
1798 {
1799         struct property *oldprop;
1800         unsigned long flags;
1801         int rc;
1802
1803         if (!newprop->name)
1804                 return -EINVAL;
1805
1806         mutex_lock(&of_mutex);
1807
1808         raw_spin_lock_irqsave(&devtree_lock, flags);
1809         rc = __of_update_property(np, newprop, &oldprop);
1810         raw_spin_unlock_irqrestore(&devtree_lock, flags);
1811
1812         if (!rc)
1813                 __of_update_property_sysfs(np, newprop, oldprop);
1814
1815         mutex_unlock(&of_mutex);
1816
1817         if (!rc)
1818                 of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
1819
1820         return rc;
1821 }
1822
1823 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1824                          int id, const char *stem, int stem_len)
1825 {
1826         ap->np = np;
1827         ap->id = id;
1828         strncpy(ap->stem, stem, stem_len);
1829         ap->stem[stem_len] = 0;
1830         list_add_tail(&ap->link, &aliases_lookup);
1831         pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1832                  ap->alias, ap->stem, ap->id, of_node_full_name(np));
1833 }
1834
1835 /**
1836  * of_alias_scan - Scan all properties of 'aliases' node
1837  *
1838  * The function scans all the properties of 'aliases' node and populate
1839  * the the global lookup table with the properties.  It returns the
1840  * number of alias_prop found, or error code in error case.
1841  *
1842  * @dt_alloc:   An allocator that provides a virtual address to memory
1843  *              for the resulting tree
1844  */
1845 void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1846 {
1847         struct property *pp;
1848
1849         of_chosen = of_find_node_by_path("/chosen");
1850         if (of_chosen == NULL)
1851                 of_chosen = of_find_node_by_path("/chosen@0");
1852
1853         if (of_chosen) {
1854                 const char *name = of_get_property(of_chosen, "stdout-path", NULL);
1855                 if (!name)
1856                         name = of_get_property(of_chosen, "linux,stdout-path", NULL);
1857                 if (name)
1858                         of_stdout = of_find_node_by_path(name);
1859         }
1860
1861         of_aliases = of_find_node_by_path("/aliases");
1862         if (!of_aliases)
1863                 return;
1864
1865         for_each_property_of_node(of_aliases, pp) {
1866                 const char *start = pp->name;
1867                 const char *end = start + strlen(start);
1868                 struct device_node *np;
1869                 struct alias_prop *ap;
1870                 int id, len;
1871
1872                 /* Skip those we do not want to proceed */
1873                 if (!strcmp(pp->name, "name") ||
1874                     !strcmp(pp->name, "phandle") ||
1875                     !strcmp(pp->name, "linux,phandle"))
1876                         continue;
1877
1878                 np = of_find_node_by_path(pp->value);
1879                 if (!np)
1880                         continue;
1881
1882                 /* walk the alias backwards to extract the id and work out
1883                  * the 'stem' string */
1884                 while (isdigit(*(end-1)) && end > start)
1885                         end--;
1886                 len = end - start;
1887
1888                 if (kstrtoint(end, 10, &id) < 0)
1889                         continue;
1890
1891                 /* Allocate an alias_prop with enough space for the stem */
1892                 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1893                 if (!ap)
1894                         continue;
1895                 memset(ap, 0, sizeof(*ap) + len + 1);
1896                 ap->alias = start;
1897                 of_alias_add(ap, np, id, start, len);
1898         }
1899 }
1900
1901 /**
1902  * of_alias_get_id - Get alias id for the given device_node
1903  * @np:         Pointer to the given device_node
1904  * @stem:       Alias stem of the given device_node
1905  *
1906  * The function travels the lookup table to get the alias id for the given
1907  * device_node and alias stem.  It returns the alias id if found.
1908  */
1909 int of_alias_get_id(struct device_node *np, const char *stem)
1910 {
1911         struct alias_prop *app;
1912         int id = -ENODEV;
1913
1914         mutex_lock(&of_mutex);
1915         list_for_each_entry(app, &aliases_lookup, link) {
1916                 if (strcmp(app->stem, stem) != 0)
1917                         continue;
1918
1919                 if (np == app->np) {
1920                         id = app->id;
1921                         break;
1922                 }
1923         }
1924         mutex_unlock(&of_mutex);
1925
1926         return id;
1927 }
1928 EXPORT_SYMBOL_GPL(of_alias_get_id);
1929
1930 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1931                                u32 *pu)
1932 {
1933         const void *curv = cur;
1934
1935         if (!prop)
1936                 return NULL;
1937
1938         if (!cur) {
1939                 curv = prop->value;
1940                 goto out_val;
1941         }
1942
1943         curv += sizeof(*cur);
1944         if (curv >= prop->value + prop->length)
1945                 return NULL;
1946
1947 out_val:
1948         *pu = be32_to_cpup(curv);
1949         return curv;
1950 }
1951 EXPORT_SYMBOL_GPL(of_prop_next_u32);
1952
1953 const char *of_prop_next_string(struct property *prop, const char *cur)
1954 {
1955         const void *curv = cur;
1956
1957         if (!prop)
1958                 return NULL;
1959
1960         if (!cur)
1961                 return prop->value;
1962
1963         curv += strlen(cur) + 1;
1964         if (curv >= prop->value + prop->length)
1965                 return NULL;
1966
1967         return curv;
1968 }
1969 EXPORT_SYMBOL_GPL(of_prop_next_string);
1970
1971 /**
1972  * of_device_is_stdout_path - check if a device node matches the
1973  *                            linux,stdout-path property
1974  *
1975  * Check if this device node matches the linux,stdout-path property
1976  * in the chosen node. return true if yes, false otherwise.
1977  */
1978 int of_device_is_stdout_path(struct device_node *dn)
1979 {
1980         if (!of_stdout)
1981                 return false;
1982
1983         return of_stdout == dn;
1984 }
1985 EXPORT_SYMBOL_GPL(of_device_is_stdout_path);
1986
1987 /**
1988  *      of_find_next_cache_node - Find a node's subsidiary cache
1989  *      @np:    node of type "cpu" or "cache"
1990  *
1991  *      Returns a node pointer with refcount incremented, use
1992  *      of_node_put() on it when done.  Caller should hold a reference
1993  *      to np.
1994  */
1995 struct device_node *of_find_next_cache_node(const struct device_node *np)
1996 {
1997         struct device_node *child;
1998         const phandle *handle;
1999
2000         handle = of_get_property(np, "l2-cache", NULL);
2001         if (!handle)
2002                 handle = of_get_property(np, "next-level-cache", NULL);
2003
2004         if (handle)
2005                 return of_find_node_by_phandle(be32_to_cpup(handle));
2006
2007         /* OF on pmac has nodes instead of properties named "l2-cache"
2008          * beneath CPU nodes.
2009          */
2010         if (!strcmp(np->type, "cpu"))
2011                 for_each_child_of_node(np, child)
2012                         if (!strcmp(child->type, "cache"))
2013                                 return child;
2014
2015         return NULL;
2016 }
2017
2018 /**
2019  * of_graph_parse_endpoint() - parse common endpoint node properties
2020  * @node: pointer to endpoint device_node
2021  * @endpoint: pointer to the OF endpoint data structure
2022  *
2023  * The caller should hold a reference to @node.
2024  */
2025 int of_graph_parse_endpoint(const struct device_node *node,
2026                             struct of_endpoint *endpoint)
2027 {
2028         struct device_node *port_node = of_get_parent(node);
2029
2030         WARN_ONCE(!port_node, "%s(): endpoint %s has no parent node\n",
2031                   __func__, node->full_name);
2032
2033         memset(endpoint, 0, sizeof(*endpoint));
2034
2035         endpoint->local_node = node;
2036         /*
2037          * It doesn't matter whether the two calls below succeed.
2038          * If they don't then the default value 0 is used.
2039          */
2040         of_property_read_u32(port_node, "reg", &endpoint->port);
2041         of_property_read_u32(node, "reg", &endpoint->id);
2042
2043         of_node_put(port_node);
2044
2045         return 0;
2046 }
2047 EXPORT_SYMBOL(of_graph_parse_endpoint);
2048
2049 /**
2050  * of_graph_get_next_endpoint() - get next endpoint node
2051  * @parent: pointer to the parent device node
2052  * @prev: previous endpoint node, or NULL to get first
2053  *
2054  * Return: An 'endpoint' node pointer with refcount incremented. Refcount
2055  * of the passed @prev node is not decremented, the caller have to use
2056  * of_node_put() on it when done.
2057  */
2058 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
2059                                         struct device_node *prev)
2060 {
2061         struct device_node *endpoint;
2062         struct device_node *port;
2063
2064         if (!parent)
2065                 return NULL;
2066
2067         /*
2068          * Start by locating the port node. If no previous endpoint is specified
2069          * search for the first port node, otherwise get the previous endpoint
2070          * parent port node.
2071          */
2072         if (!prev) {
2073                 struct device_node *node;
2074
2075                 node = of_get_child_by_name(parent, "ports");
2076                 if (node)
2077                         parent = node;
2078
2079                 port = of_get_child_by_name(parent, "port");
2080                 of_node_put(node);
2081
2082                 if (!port) {
2083                         pr_err("%s(): no port node found in %s\n",
2084                                __func__, parent->full_name);
2085                         return NULL;
2086                 }
2087         } else {
2088                 port = of_get_parent(prev);
2089                 if (WARN_ONCE(!port, "%s(): endpoint %s has no parent node\n",
2090                               __func__, prev->full_name))
2091                         return NULL;
2092
2093                 /*
2094                  * Avoid dropping prev node refcount to 0 when getting the next
2095                  * child below.
2096                  */
2097                 of_node_get(prev);
2098         }
2099
2100         while (1) {
2101                 /*
2102                  * Now that we have a port node, get the next endpoint by
2103                  * getting the next child. If the previous endpoint is NULL this
2104                  * will return the first child.
2105                  */
2106                 endpoint = of_get_next_child(port, prev);
2107                 if (endpoint) {
2108                         of_node_put(port);
2109                         return endpoint;
2110                 }
2111
2112                 /* No more endpoints under this port, try the next one. */
2113                 prev = NULL;
2114
2115                 do {
2116                         port = of_get_next_child(parent, port);
2117                         if (!port)
2118                                 return NULL;
2119                 } while (of_node_cmp(port->name, "port"));
2120         }
2121 }
2122 EXPORT_SYMBOL(of_graph_get_next_endpoint);
2123
2124 /**
2125  * of_graph_get_remote_port_parent() - get remote port's parent node
2126  * @node: pointer to a local endpoint device_node
2127  *
2128  * Return: Remote device node associated with remote endpoint node linked
2129  *         to @node. Use of_node_put() on it when done.
2130  */
2131 struct device_node *of_graph_get_remote_port_parent(
2132                                const struct device_node *node)
2133 {
2134         struct device_node *np;
2135         unsigned int depth;
2136
2137         /* Get remote endpoint node. */
2138         np = of_parse_phandle(node, "remote-endpoint", 0);
2139
2140         /* Walk 3 levels up only if there is 'ports' node. */
2141         for (depth = 3; depth && np; depth--) {
2142                 np = of_get_next_parent(np);
2143                 if (depth == 2 && of_node_cmp(np->name, "ports"))
2144                         break;
2145         }
2146         return np;
2147 }
2148 EXPORT_SYMBOL(of_graph_get_remote_port_parent);
2149
2150 /**
2151  * of_graph_get_remote_port() - get remote port node
2152  * @node: pointer to a local endpoint device_node
2153  *
2154  * Return: Remote port node associated with remote endpoint node linked
2155  *         to @node. Use of_node_put() on it when done.
2156  */
2157 struct device_node *of_graph_get_remote_port(const struct device_node *node)
2158 {
2159         struct device_node *np;
2160
2161         /* Get remote endpoint node. */
2162         np = of_parse_phandle(node, "remote-endpoint", 0);
2163         if (!np)
2164                 return NULL;
2165         return of_get_next_parent(np);
2166 }
2167 EXPORT_SYMBOL(of_graph_get_remote_port);