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