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