2 * Procedures for creating, accessing and interpreting the device tree.
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
7 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8 * {engebret|bergner}@us.ibm.com
10 * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
12 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
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.
20 #include <linux/ctype.h>
21 #include <linux/module.h>
23 #include <linux/spinlock.h>
24 #include <linux/slab.h>
25 #include <linux/proc_fs.h>
27 #include "of_private.h"
29 LIST_HEAD(aliases_lookup);
31 struct device_node *of_allnodes;
32 EXPORT_SYMBOL(of_allnodes);
33 struct device_node *of_chosen;
34 struct device_node *of_aliases;
35 static struct device_node *of_stdout;
37 DEFINE_MUTEX(of_aliases_mutex);
39 /* use when traversing tree through the allnext, child, sibling,
40 * or parent members of struct device_node.
42 DEFINE_RAW_SPINLOCK(devtree_lock);
44 int of_n_addr_cells(struct device_node *np)
51 ip = of_get_property(np, "#address-cells", NULL);
53 return be32_to_cpup(ip);
55 /* No #address-cells property for the root node */
56 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
58 EXPORT_SYMBOL(of_n_addr_cells);
60 int of_n_size_cells(struct device_node *np)
67 ip = of_get_property(np, "#size-cells", NULL);
69 return be32_to_cpup(ip);
71 /* No #size-cells property for the root node */
72 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
74 EXPORT_SYMBOL(of_n_size_cells);
76 #if defined(CONFIG_OF_DYNAMIC)
78 * of_node_get - Increment refcount of a node
79 * @node: Node to inc refcount, NULL is supported to
80 * simplify writing of callers
84 struct device_node *of_node_get(struct device_node *node)
87 kref_get(&node->kref);
90 EXPORT_SYMBOL(of_node_get);
92 static inline struct device_node *kref_to_device_node(struct kref *kref)
94 return container_of(kref, struct device_node, kref);
98 * of_node_release - release a dynamically allocated node
99 * @kref: kref element of the node to be released
101 * In of_node_put() this function is passed to kref_put()
104 static void of_node_release(struct kref *kref)
106 struct device_node *node = kref_to_device_node(kref);
107 struct property *prop = node->properties;
109 /* We should never be releasing nodes that haven't been detached. */
110 if (!of_node_check_flag(node, OF_DETACHED)) {
111 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
113 kref_init(&node->kref);
117 if (!of_node_check_flag(node, OF_DYNAMIC))
121 struct property *next = prop->next;
128 prop = node->deadprops;
129 node->deadprops = NULL;
132 kfree(node->full_name);
138 * of_node_put - Decrement refcount of a node
139 * @node: Node to dec refcount, NULL is supported to
140 * simplify writing of callers
143 void of_node_put(struct device_node *node)
146 kref_put(&node->kref, of_node_release);
148 EXPORT_SYMBOL(of_node_put);
149 #endif /* CONFIG_OF_DYNAMIC */
151 static struct property *__of_find_property(const struct device_node *np,
152 const char *name, int *lenp)
159 for (pp = np->properties; pp; pp = pp->next) {
160 if (of_prop_cmp(pp->name, name) == 0) {
170 struct property *of_find_property(const struct device_node *np,
177 raw_spin_lock_irqsave(&devtree_lock, flags);
178 pp = __of_find_property(np, name, lenp);
179 raw_spin_unlock_irqrestore(&devtree_lock, flags);
183 EXPORT_SYMBOL(of_find_property);
186 * of_find_all_nodes - Get next node in global list
187 * @prev: Previous node or NULL to start iteration
188 * of_node_put() will be called on it
190 * Returns a node pointer with refcount incremented, use
191 * of_node_put() on it when done.
193 struct device_node *of_find_all_nodes(struct device_node *prev)
195 struct device_node *np;
198 raw_spin_lock_irqsave(&devtree_lock, flags);
199 np = prev ? prev->allnext : of_allnodes;
200 for (; np != NULL; np = np->allnext)
204 raw_spin_unlock_irqrestore(&devtree_lock, flags);
207 EXPORT_SYMBOL(of_find_all_nodes);
210 * Find a property with a given name for a given node
211 * and return the value.
213 static const void *__of_get_property(const struct device_node *np,
214 const char *name, int *lenp)
216 struct property *pp = __of_find_property(np, name, lenp);
218 return pp ? pp->value : NULL;
222 * Find a property with a given name for a given node
223 * and return the value.
225 const void *of_get_property(const struct device_node *np, const char *name,
228 struct property *pp = of_find_property(np, name, lenp);
230 return pp ? pp->value : NULL;
232 EXPORT_SYMBOL(of_get_property);
234 /** Checks if the given "compat" string matches one of the strings in
235 * the device's "compatible" property
237 static int __of_device_is_compatible(const struct device_node *device,
243 cp = __of_get_property(device, "compatible", &cplen);
247 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
257 /** Checks if the given "compat" string matches one of the strings in
258 * the device's "compatible" property
260 int of_device_is_compatible(const struct device_node *device,
266 raw_spin_lock_irqsave(&devtree_lock, flags);
267 res = __of_device_is_compatible(device, compat);
268 raw_spin_unlock_irqrestore(&devtree_lock, flags);
271 EXPORT_SYMBOL(of_device_is_compatible);
274 * of_machine_is_compatible - Test root of device tree for a given compatible value
275 * @compat: compatible string to look for in root node's compatible property.
277 * Returns true if the root node has the given value in its
278 * compatible property.
280 int of_machine_is_compatible(const char *compat)
282 struct device_node *root;
285 root = of_find_node_by_path("/");
287 rc = of_device_is_compatible(root, compat);
292 EXPORT_SYMBOL(of_machine_is_compatible);
295 * __of_device_is_available - check if a device is available for use
297 * @device: Node to check for availability, with locks already held
299 * Returns 1 if the status property is absent or set to "okay" or "ok",
302 static int __of_device_is_available(const struct device_node *device)
307 status = __of_get_property(device, "status", &statlen);
312 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
320 * of_device_is_available - check if a device is available for use
322 * @device: Node to check for availability
324 * Returns 1 if the status property is absent or set to "okay" or "ok",
327 int of_device_is_available(const struct device_node *device)
332 raw_spin_lock_irqsave(&devtree_lock, flags);
333 res = __of_device_is_available(device);
334 raw_spin_unlock_irqrestore(&devtree_lock, flags);
338 EXPORT_SYMBOL(of_device_is_available);
341 * of_get_parent - Get a node's parent if any
342 * @node: Node to get parent
344 * Returns a node pointer with refcount incremented, use
345 * of_node_put() on it when done.
347 struct device_node *of_get_parent(const struct device_node *node)
349 struct device_node *np;
355 raw_spin_lock_irqsave(&devtree_lock, flags);
356 np = of_node_get(node->parent);
357 raw_spin_unlock_irqrestore(&devtree_lock, flags);
360 EXPORT_SYMBOL(of_get_parent);
363 * of_get_next_parent - Iterate to a node's parent
364 * @node: Node to get parent of
366 * This is like of_get_parent() except that it drops the
367 * refcount on the passed node, making it suitable for iterating
368 * through a node's parents.
370 * Returns a node pointer with refcount incremented, use
371 * of_node_put() on it when done.
373 struct device_node *of_get_next_parent(struct device_node *node)
375 struct device_node *parent;
381 raw_spin_lock_irqsave(&devtree_lock, flags);
382 parent = of_node_get(node->parent);
384 raw_spin_unlock_irqrestore(&devtree_lock, flags);
387 EXPORT_SYMBOL(of_get_next_parent);
390 * of_get_next_child - Iterate a node childs
392 * @prev: previous child of the parent node, or NULL to get first
394 * Returns a node pointer with refcount incremented, use
395 * of_node_put() on it when done.
397 struct device_node *of_get_next_child(const struct device_node *node,
398 struct device_node *prev)
400 struct device_node *next;
403 raw_spin_lock_irqsave(&devtree_lock, flags);
404 next = prev ? prev->sibling : node->child;
405 for (; next; next = next->sibling)
406 if (of_node_get(next))
409 raw_spin_unlock_irqrestore(&devtree_lock, flags);
412 EXPORT_SYMBOL(of_get_next_child);
415 * of_get_next_available_child - Find the next available child node
417 * @prev: previous child of the parent node, or NULL to get first
419 * This function is like of_get_next_child(), except that it
420 * automatically skips any disabled nodes (i.e. status = "disabled").
422 struct device_node *of_get_next_available_child(const struct device_node *node,
423 struct device_node *prev)
425 struct device_node *next;
428 raw_spin_lock_irqsave(&devtree_lock, flags);
429 next = prev ? prev->sibling : node->child;
430 for (; next; next = next->sibling) {
431 if (!__of_device_is_available(next))
433 if (of_node_get(next))
437 raw_spin_unlock_irqrestore(&devtree_lock, flags);
440 EXPORT_SYMBOL(of_get_next_available_child);
443 * of_get_child_by_name - Find the child node by name for a given parent
445 * @name: child name to look for.
447 * This function looks for child node for given matching name
449 * Returns a node pointer if found, with refcount incremented, use
450 * of_node_put() on it when done.
451 * Returns NULL if node is not found.
453 struct device_node *of_get_child_by_name(const struct device_node *node,
456 struct device_node *child;
458 for_each_child_of_node(node, child)
459 if (child->name && (of_node_cmp(child->name, name) == 0))
463 EXPORT_SYMBOL(of_get_child_by_name);
466 * of_find_node_by_path - Find a node matching a full OF path
467 * @path: The full path to match
469 * Returns a node pointer with refcount incremented, use
470 * of_node_put() on it when done.
472 struct device_node *of_find_node_by_path(const char *path)
474 struct device_node *np = of_allnodes;
477 raw_spin_lock_irqsave(&devtree_lock, flags);
478 for (; np; np = np->allnext) {
479 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
483 raw_spin_unlock_irqrestore(&devtree_lock, flags);
486 EXPORT_SYMBOL(of_find_node_by_path);
489 * of_find_node_by_name - Find a node by its "name" property
490 * @from: The node to start searching from or NULL, the node
491 * you pass will not be searched, only the next one
492 * will; typically, you pass what the previous call
493 * returned. of_node_put() will be called on it
494 * @name: The name string to match against
496 * Returns a node pointer with refcount incremented, use
497 * of_node_put() on it when done.
499 struct device_node *of_find_node_by_name(struct device_node *from,
502 struct device_node *np;
505 raw_spin_lock_irqsave(&devtree_lock, flags);
506 np = from ? from->allnext : of_allnodes;
507 for (; np; np = np->allnext)
508 if (np->name && (of_node_cmp(np->name, name) == 0)
512 raw_spin_unlock_irqrestore(&devtree_lock, flags);
515 EXPORT_SYMBOL(of_find_node_by_name);
518 * of_find_node_by_type - Find a node by its "device_type" property
519 * @from: The node to start searching from, or NULL to start searching
520 * the entire device tree. The node you pass will not be
521 * searched, only the next one will; typically, you pass
522 * what the previous call returned. of_node_put() will be
523 * called on from for you.
524 * @type: The type string to match against
526 * Returns a node pointer with refcount incremented, use
527 * of_node_put() on it when done.
529 struct device_node *of_find_node_by_type(struct device_node *from,
532 struct device_node *np;
535 raw_spin_lock_irqsave(&devtree_lock, flags);
536 np = from ? from->allnext : of_allnodes;
537 for (; np; np = np->allnext)
538 if (np->type && (of_node_cmp(np->type, type) == 0)
542 raw_spin_unlock_irqrestore(&devtree_lock, flags);
545 EXPORT_SYMBOL(of_find_node_by_type);
548 * of_find_compatible_node - Find a node based on type and one of the
549 * tokens in its "compatible" property
550 * @from: The node to start searching from or NULL, the node
551 * you pass will not be searched, only the next one
552 * will; typically, you pass what the previous call
553 * returned. of_node_put() will be called on it
554 * @type: The type string to match "device_type" or NULL to ignore
555 * @compatible: The string to match to one of the tokens in the device
558 * Returns a node pointer with refcount incremented, use
559 * of_node_put() on it when done.
561 struct device_node *of_find_compatible_node(struct device_node *from,
562 const char *type, const char *compatible)
564 struct device_node *np;
567 raw_spin_lock_irqsave(&devtree_lock, flags);
568 np = from ? from->allnext : of_allnodes;
569 for (; np; np = np->allnext) {
571 && !(np->type && (of_node_cmp(np->type, type) == 0)))
573 if (__of_device_is_compatible(np, compatible) &&
578 raw_spin_unlock_irqrestore(&devtree_lock, flags);
581 EXPORT_SYMBOL(of_find_compatible_node);
584 * of_find_node_with_property - Find a node which has a property with
586 * @from: The node to start searching from or NULL, the node
587 * you pass will not be searched, only the next one
588 * will; typically, you pass what the previous call
589 * returned. of_node_put() will be called on it
590 * @prop_name: The name of the property to look for.
592 * Returns a node pointer with refcount incremented, use
593 * of_node_put() on it when done.
595 struct device_node *of_find_node_with_property(struct device_node *from,
596 const char *prop_name)
598 struct device_node *np;
602 raw_spin_lock_irqsave(&devtree_lock, flags);
603 np = from ? from->allnext : of_allnodes;
604 for (; np; np = np->allnext) {
605 for (pp = np->properties; pp; pp = pp->next) {
606 if (of_prop_cmp(pp->name, prop_name) == 0) {
614 raw_spin_unlock_irqrestore(&devtree_lock, flags);
617 EXPORT_SYMBOL(of_find_node_with_property);
620 const struct of_device_id *__of_match_node(const struct of_device_id *matches,
621 const struct device_node *node)
626 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
628 if (matches->name[0])
630 && !strcmp(matches->name, node->name);
631 if (matches->type[0])
633 && !strcmp(matches->type, node->type);
634 if (matches->compatible[0])
635 match &= __of_device_is_compatible(node,
636 matches->compatible);
645 * of_match_node - Tell if an device_node has a matching of_match structure
646 * @matches: array of of device match structures to search in
647 * @node: the of device structure to match against
649 * Low level utility function used by device matching.
651 const struct of_device_id *of_match_node(const struct of_device_id *matches,
652 const struct device_node *node)
654 const struct of_device_id *match;
657 raw_spin_lock_irqsave(&devtree_lock, flags);
658 match = __of_match_node(matches, node);
659 raw_spin_unlock_irqrestore(&devtree_lock, flags);
662 EXPORT_SYMBOL(of_match_node);
665 * of_find_matching_node_and_match - Find a node based on an of_device_id
667 * @from: The node to start searching from or NULL, the node
668 * you pass will not be searched, only the next one
669 * will; typically, you pass what the previous call
670 * returned. of_node_put() will be called on it
671 * @matches: array of of device match structures to search in
672 * @match Updated to point at the matches entry which matched
674 * Returns a node pointer with refcount incremented, use
675 * of_node_put() on it when done.
677 struct device_node *of_find_matching_node_and_match(struct device_node *from,
678 const struct of_device_id *matches,
679 const struct of_device_id **match)
681 struct device_node *np;
682 const struct of_device_id *m;
688 raw_spin_lock_irqsave(&devtree_lock, flags);
689 np = from ? from->allnext : of_allnodes;
690 for (; np; np = np->allnext) {
691 m = __of_match_node(matches, np);
692 if (m && of_node_get(np)) {
699 raw_spin_unlock_irqrestore(&devtree_lock, flags);
702 EXPORT_SYMBOL(of_find_matching_node_and_match);
705 * of_modalias_node - Lookup appropriate modalias for a device node
706 * @node: pointer to a device tree node
707 * @modalias: Pointer to buffer that modalias value will be copied into
708 * @len: Length of modalias value
710 * Based on the value of the compatible property, this routine will attempt
711 * to choose an appropriate modalias value for a particular device tree node.
712 * It does this by stripping the manufacturer prefix (as delimited by a ',')
713 * from the first entry in the compatible list property.
715 * This routine returns 0 on success, <0 on failure.
717 int of_modalias_node(struct device_node *node, char *modalias, int len)
719 const char *compatible, *p;
722 compatible = of_get_property(node, "compatible", &cplen);
723 if (!compatible || strlen(compatible) > cplen)
725 p = strchr(compatible, ',');
726 strlcpy(modalias, p ? p + 1 : compatible, len);
729 EXPORT_SYMBOL_GPL(of_modalias_node);
732 * of_find_node_by_phandle - Find a node given a phandle
733 * @handle: phandle of the node to find
735 * Returns a node pointer with refcount incremented, use
736 * of_node_put() on it when done.
738 struct device_node *of_find_node_by_phandle(phandle handle)
740 struct device_node *np;
743 raw_spin_lock_irqsave(&devtree_lock, flags);
744 for (np = of_allnodes; np; np = np->allnext)
745 if (np->phandle == handle)
748 raw_spin_unlock_irqrestore(&devtree_lock, flags);
751 EXPORT_SYMBOL(of_find_node_by_phandle);
754 * of_find_property_value_of_size
756 * @np: device node from which the property value is to be read.
757 * @propname: name of the property to be searched.
758 * @len: requested length of property value
760 * Search for a property in a device node and valid the requested size.
761 * Returns the property value on success, -EINVAL if the property does not
762 * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
763 * property data isn't large enough.
766 static void *of_find_property_value_of_size(const struct device_node *np,
767 const char *propname, u32 len)
769 struct property *prop = of_find_property(np, propname, NULL);
772 return ERR_PTR(-EINVAL);
774 return ERR_PTR(-ENODATA);
775 if (len > prop->length)
776 return ERR_PTR(-EOVERFLOW);
782 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
784 * @np: device node from which the property value is to be read.
785 * @propname: name of the property to be searched.
786 * @index: index of the u32 in the list of values
787 * @out_value: pointer to return value, modified only if no error.
789 * Search for a property in a device node and read nth 32-bit value from
790 * it. Returns 0 on success, -EINVAL if the property does not exist,
791 * -ENODATA if property does not have a value, and -EOVERFLOW if the
792 * property data isn't large enough.
794 * The out_value is modified only if a valid u32 value can be decoded.
796 int of_property_read_u32_index(const struct device_node *np,
797 const char *propname,
798 u32 index, u32 *out_value)
800 const u32 *val = of_find_property_value_of_size(np, propname,
801 ((index + 1) * sizeof(*out_value)));
806 *out_value = be32_to_cpup(((__be32 *)val) + index);
809 EXPORT_SYMBOL_GPL(of_property_read_u32_index);
812 * of_property_read_u8_array - Find and read an array of u8 from a property.
814 * @np: device node from which the property value is to be read.
815 * @propname: name of the property to be searched.
816 * @out_values: pointer to return value, modified only if return value is 0.
817 * @sz: number of array elements to read
819 * Search for a property in a device node and read 8-bit value(s) from
820 * it. Returns 0 on success, -EINVAL if the property does not exist,
821 * -ENODATA if property does not have a value, and -EOVERFLOW if the
822 * property data isn't large enough.
824 * dts entry of array should be like:
825 * property = /bits/ 8 <0x50 0x60 0x70>;
827 * The out_values is modified only if a valid u8 value can be decoded.
829 int of_property_read_u8_array(const struct device_node *np,
830 const char *propname, u8 *out_values, size_t sz)
832 const u8 *val = of_find_property_value_of_size(np, propname,
833 (sz * sizeof(*out_values)));
839 *out_values++ = *val++;
842 EXPORT_SYMBOL_GPL(of_property_read_u8_array);
845 * of_property_read_u16_array - Find and read an array of u16 from a property.
847 * @np: device node from which the property value is to be read.
848 * @propname: name of the property to be searched.
849 * @out_values: pointer to return value, modified only if return value is 0.
850 * @sz: number of array elements to read
852 * Search for a property in a device node and read 16-bit value(s) from
853 * it. Returns 0 on success, -EINVAL if the property does not exist,
854 * -ENODATA if property does not have a value, and -EOVERFLOW if the
855 * property data isn't large enough.
857 * dts entry of array should be like:
858 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
860 * The out_values is modified only if a valid u16 value can be decoded.
862 int of_property_read_u16_array(const struct device_node *np,
863 const char *propname, u16 *out_values, size_t sz)
865 const __be16 *val = of_find_property_value_of_size(np, propname,
866 (sz * sizeof(*out_values)));
872 *out_values++ = be16_to_cpup(val++);
875 EXPORT_SYMBOL_GPL(of_property_read_u16_array);
878 * of_property_read_u32_array - Find and read an array of 32 bit integers
881 * @np: device node from which the property value is to be read.
882 * @propname: name of the property to be searched.
883 * @out_values: pointer to return value, modified only if return value is 0.
884 * @sz: number of array elements to read
886 * Search for a property in a device node and read 32-bit value(s) from
887 * it. Returns 0 on success, -EINVAL if the property does not exist,
888 * -ENODATA if property does not have a value, and -EOVERFLOW if the
889 * property data isn't large enough.
891 * The out_values is modified only if a valid u32 value can be decoded.
893 int of_property_read_u32_array(const struct device_node *np,
894 const char *propname, u32 *out_values,
897 const __be32 *val = of_find_property_value_of_size(np, propname,
898 (sz * sizeof(*out_values)));
904 *out_values++ = be32_to_cpup(val++);
907 EXPORT_SYMBOL_GPL(of_property_read_u32_array);
910 * of_property_read_u64 - Find and read a 64 bit integer from a property
911 * @np: device node from which the property value is to be read.
912 * @propname: name of the property to be searched.
913 * @out_value: pointer to return value, modified only if return value is 0.
915 * Search for a property in a device node and read a 64-bit value from
916 * it. Returns 0 on success, -EINVAL if the property does not exist,
917 * -ENODATA if property does not have a value, and -EOVERFLOW if the
918 * property data isn't large enough.
920 * The out_value is modified only if a valid u64 value can be decoded.
922 int of_property_read_u64(const struct device_node *np, const char *propname,
925 const __be32 *val = of_find_property_value_of_size(np, propname,
931 *out_value = of_read_number(val, 2);
934 EXPORT_SYMBOL_GPL(of_property_read_u64);
937 * of_property_read_string - Find and read a string from a property
938 * @np: device node from which the property value is to be read.
939 * @propname: name of the property to be searched.
940 * @out_string: pointer to null terminated return string, modified only if
943 * Search for a property in a device tree node and retrieve a null
944 * terminated string value (pointer to data, not a copy). Returns 0 on
945 * success, -EINVAL if the property does not exist, -ENODATA if property
946 * does not have a value, and -EILSEQ if the string is not null-terminated
947 * within the length of the property data.
949 * The out_string pointer is modified only if a valid string can be decoded.
951 int of_property_read_string(struct device_node *np, const char *propname,
952 const char **out_string)
954 struct property *prop = of_find_property(np, propname, NULL);
959 if (strnlen(prop->value, prop->length) >= prop->length)
961 *out_string = prop->value;
964 EXPORT_SYMBOL_GPL(of_property_read_string);
967 * of_property_read_string_index - Find and read a string from a multiple
969 * @np: device node from which the property value is to be read.
970 * @propname: name of the property to be searched.
971 * @index: index of the string in the list of strings
972 * @out_string: pointer to null terminated return string, modified only if
975 * Search for a property in a device tree node and retrieve a null
976 * terminated string value (pointer to data, not a copy) in the list of strings
977 * contained in that property.
978 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
979 * property does not have a value, and -EILSEQ if the string is not
980 * null-terminated within the length of the property data.
982 * The out_string pointer is modified only if a valid string can be decoded.
984 int of_property_read_string_index(struct device_node *np, const char *propname,
985 int index, const char **output)
987 struct property *prop = of_find_property(np, propname, NULL);
989 size_t l = 0, total = 0;
996 if (strnlen(prop->value, prop->length) >= prop->length)
1001 for (i = 0; total < prop->length; total += l, p += l) {
1010 EXPORT_SYMBOL_GPL(of_property_read_string_index);
1013 * of_property_match_string() - Find string in a list and return index
1014 * @np: pointer to node containing string list property
1015 * @propname: string list property name
1016 * @string: pointer to string to search for in string list
1018 * This function searches a string list property and returns the index
1019 * of a specific string value.
1021 int of_property_match_string(struct device_node *np, const char *propname,
1024 struct property *prop = of_find_property(np, propname, NULL);
1027 const char *p, *end;
1035 end = p + prop->length;
1037 for (i = 0; p < end; i++, p += l) {
1041 pr_debug("comparing %s with %s\n", string, p);
1042 if (strcmp(string, p) == 0)
1043 return i; /* Found it; return index */
1047 EXPORT_SYMBOL_GPL(of_property_match_string);
1050 * of_property_count_strings - Find and return the number of strings from a
1051 * multiple strings property.
1052 * @np: device node from which the property value is to be read.
1053 * @propname: name of the property to be searched.
1055 * Search for a property in a device tree node and retrieve the number of null
1056 * terminated string contain in it. Returns the number of strings on
1057 * success, -EINVAL if the property does not exist, -ENODATA if property
1058 * does not have a value, and -EILSEQ if the string is not null-terminated
1059 * within the length of the property data.
1061 int of_property_count_strings(struct device_node *np, const char *propname)
1063 struct property *prop = of_find_property(np, propname, NULL);
1065 size_t l = 0, total = 0;
1072 if (strnlen(prop->value, prop->length) >= prop->length)
1077 for (i = 0; total < prop->length; total += l, p += l, i++)
1082 EXPORT_SYMBOL_GPL(of_property_count_strings);
1085 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1086 * @np: Pointer to device node holding phandle property
1087 * @phandle_name: Name of property holding a phandle value
1088 * @index: For properties holding a table of phandles, this is the index into
1091 * Returns the device_node pointer with refcount incremented. Use
1092 * of_node_put() on it when done.
1094 struct device_node *of_parse_phandle(const struct device_node *np,
1095 const char *phandle_name, int index)
1097 const __be32 *phandle;
1100 phandle = of_get_property(np, phandle_name, &size);
1101 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
1104 return of_find_node_by_phandle(be32_to_cpup(phandle + index));
1106 EXPORT_SYMBOL(of_parse_phandle);
1109 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
1110 * @np: pointer to a device tree node containing a list
1111 * @list_name: property name that contains a list
1112 * @cells_name: property name that specifies phandles' arguments count
1113 * @index: index of a phandle to parse out
1114 * @out_args: optional pointer to output arguments structure (will be filled)
1116 * This function is useful to parse lists of phandles and their arguments.
1117 * Returns 0 on success and fills out_args, on error returns appropriate
1120 * Caller is responsible to call of_node_put() on the returned out_args->node
1126 * #list-cells = <2>;
1130 * #list-cells = <1>;
1134 * list = <&phandle1 1 2 &phandle2 3>;
1137 * To get a device_node of the `node2' node you may call this:
1138 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1140 static int __of_parse_phandle_with_args(const struct device_node *np,
1141 const char *list_name,
1142 const char *cells_name, int index,
1143 struct of_phandle_args *out_args)
1145 const __be32 *list, *list_end;
1146 int rc = 0, size, cur_index = 0;
1148 struct device_node *node = NULL;
1151 /* Retrieve the phandle list property */
1152 list = of_get_property(np, list_name, &size);
1155 list_end = list + size / sizeof(*list);
1157 /* Loop over the phandles until all the requested entry is found */
1158 while (list < list_end) {
1163 * If phandle is 0, then it is an empty entry with no
1164 * arguments. Skip forward to the next entry.
1166 phandle = be32_to_cpup(list++);
1169 * Find the provider node and parse the #*-cells
1170 * property to determine the argument length
1172 node = of_find_node_by_phandle(phandle);
1174 pr_err("%s: could not find phandle\n",
1178 if (of_property_read_u32(node, cells_name, &count)) {
1179 pr_err("%s: could not get %s for %s\n",
1180 np->full_name, cells_name,
1186 * Make sure that the arguments actually fit in the
1187 * remaining property data length
1189 if (list + count > list_end) {
1190 pr_err("%s: arguments longer than property\n",
1197 * All of the error cases above bail out of the loop, so at
1198 * this point, the parsing is successful. If the requested
1199 * index matches, then fill the out_args structure and return,
1200 * or return -ENOENT for an empty entry.
1203 if (cur_index == index) {
1209 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1210 count = MAX_PHANDLE_ARGS;
1211 out_args->np = node;
1212 out_args->args_count = count;
1213 for (i = 0; i < count; i++)
1214 out_args->args[i] = be32_to_cpup(list++);
1219 /* Found it! return success */
1230 * Unlock node before returning result; will be one of:
1231 * -ENOENT : index is for empty phandle
1232 * -EINVAL : parsing error on data
1233 * [1..n] : Number of phandle (count mode; when index = -1)
1235 rc = index < 0 ? cur_index : -ENOENT;
1242 int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1243 const char *cells_name, int index,
1244 struct of_phandle_args *out_args)
1248 return __of_parse_phandle_with_args(np, list_name, cells_name, index, out_args);
1250 EXPORT_SYMBOL(of_parse_phandle_with_args);
1253 * of_count_phandle_with_args() - Find the number of phandles references in a property
1254 * @np: pointer to a device tree node containing a list
1255 * @list_name: property name that contains a list
1256 * @cells_name: property name that specifies phandles' arguments count
1258 * Returns the number of phandle + argument tuples within a property. It
1259 * is a typical pattern to encode a list of phandle and variable
1260 * arguments into a single property. The number of arguments is encoded
1261 * by a property in the phandle-target node. For example, a gpios
1262 * property would contain a list of GPIO specifies consisting of a
1263 * phandle and 1 or more arguments. The number of arguments are
1264 * determined by the #gpio-cells property in the node pointed to by the
1267 int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1268 const char *cells_name)
1270 return __of_parse_phandle_with_args(np, list_name, cells_name, -1, NULL);
1272 EXPORT_SYMBOL(of_count_phandle_with_args);
1274 #if defined(CONFIG_OF_DYNAMIC)
1275 static int of_property_notify(int action, struct device_node *np,
1276 struct property *prop)
1278 struct of_prop_reconfig pr;
1282 return of_reconfig_notify(action, &pr);
1285 static int of_property_notify(int action, struct device_node *np,
1286 struct property *prop)
1293 * of_add_property - Add a property to a node
1295 int of_add_property(struct device_node *np, struct property *prop)
1297 struct property **next;
1298 unsigned long flags;
1301 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1306 raw_spin_lock_irqsave(&devtree_lock, flags);
1307 next = &np->properties;
1309 if (strcmp(prop->name, (*next)->name) == 0) {
1310 /* duplicate ! don't insert it */
1311 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1314 next = &(*next)->next;
1317 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1319 #ifdef CONFIG_PROC_DEVICETREE
1320 /* try to add to proc as well if it was initialized */
1322 proc_device_tree_add_prop(np->pde, prop);
1323 #endif /* CONFIG_PROC_DEVICETREE */
1329 * of_remove_property - Remove a property from a node.
1331 * Note that we don't actually remove it, since we have given out
1332 * who-knows-how-many pointers to the data using get-property.
1333 * Instead we just move the property to the "dead properties"
1334 * list, so it won't be found any more.
1336 int of_remove_property(struct device_node *np, struct property *prop)
1338 struct property **next;
1339 unsigned long flags;
1343 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1347 raw_spin_lock_irqsave(&devtree_lock, flags);
1348 next = &np->properties;
1350 if (*next == prop) {
1351 /* found the node */
1353 prop->next = np->deadprops;
1354 np->deadprops = prop;
1358 next = &(*next)->next;
1360 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1365 #ifdef CONFIG_PROC_DEVICETREE
1366 /* try to remove the proc node as well */
1368 proc_device_tree_remove_prop(np->pde, prop);
1369 #endif /* CONFIG_PROC_DEVICETREE */
1375 * of_update_property - Update a property in a node, if the property does
1376 * not exist, add it.
1378 * Note that we don't actually remove it, since we have given out
1379 * who-knows-how-many pointers to the data using get-property.
1380 * Instead we just move the property to the "dead properties" list,
1381 * and add the new property to the property list
1383 int of_update_property(struct device_node *np, struct property *newprop)
1385 struct property **next, *oldprop;
1386 unsigned long flags;
1389 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1396 oldprop = of_find_property(np, newprop->name, NULL);
1398 return of_add_property(np, newprop);
1400 raw_spin_lock_irqsave(&devtree_lock, flags);
1401 next = &np->properties;
1403 if (*next == oldprop) {
1404 /* found the node */
1405 newprop->next = oldprop->next;
1407 oldprop->next = np->deadprops;
1408 np->deadprops = oldprop;
1412 next = &(*next)->next;
1414 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1419 #ifdef CONFIG_PROC_DEVICETREE
1420 /* try to add to proc as well if it was initialized */
1422 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1423 #endif /* CONFIG_PROC_DEVICETREE */
1428 #if defined(CONFIG_OF_DYNAMIC)
1430 * Support for dynamic device trees.
1432 * On some platforms, the device tree can be manipulated at runtime.
1433 * The routines in this section support adding, removing and changing
1434 * device tree nodes.
1437 static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1439 int of_reconfig_notifier_register(struct notifier_block *nb)
1441 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1443 EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
1445 int of_reconfig_notifier_unregister(struct notifier_block *nb)
1447 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1449 EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
1451 int of_reconfig_notify(unsigned long action, void *p)
1455 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1456 return notifier_to_errno(rc);
1459 #ifdef CONFIG_PROC_DEVICETREE
1460 static void of_add_proc_dt_entry(struct device_node *dn)
1462 struct proc_dir_entry *ent;
1464 ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
1466 proc_device_tree_add_node(dn, ent);
1469 static void of_add_proc_dt_entry(struct device_node *dn)
1476 * of_attach_node - Plug a device node into the tree and global list.
1478 int of_attach_node(struct device_node *np)
1480 unsigned long flags;
1483 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1487 raw_spin_lock_irqsave(&devtree_lock, flags);
1488 np->sibling = np->parent->child;
1489 np->allnext = of_allnodes;
1490 np->parent->child = np;
1492 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1494 of_add_proc_dt_entry(np);
1498 #ifdef CONFIG_PROC_DEVICETREE
1499 static void of_remove_proc_dt_entry(struct device_node *dn)
1501 proc_remove(dn->pde);
1504 static void of_remove_proc_dt_entry(struct device_node *dn)
1511 * of_detach_node - "Unplug" a node from the device tree.
1513 * The caller must hold a reference to the node. The memory associated with
1514 * the node is not freed until its refcount goes to zero.
1516 int of_detach_node(struct device_node *np)
1518 struct device_node *parent;
1519 unsigned long flags;
1522 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1526 raw_spin_lock_irqsave(&devtree_lock, flags);
1528 if (of_node_check_flag(np, OF_DETACHED)) {
1529 /* someone already detached it */
1530 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1534 parent = np->parent;
1536 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1540 if (of_allnodes == np)
1541 of_allnodes = np->allnext;
1543 struct device_node *prev;
1544 for (prev = of_allnodes;
1545 prev->allnext != np;
1546 prev = prev->allnext)
1548 prev->allnext = np->allnext;
1551 if (parent->child == np)
1552 parent->child = np->sibling;
1554 struct device_node *prevsib;
1555 for (prevsib = np->parent->child;
1556 prevsib->sibling != np;
1557 prevsib = prevsib->sibling)
1559 prevsib->sibling = np->sibling;
1562 of_node_set_flag(np, OF_DETACHED);
1563 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1565 of_remove_proc_dt_entry(np);
1568 #endif /* defined(CONFIG_OF_DYNAMIC) */
1570 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1571 int id, const char *stem, int stem_len)
1575 strncpy(ap->stem, stem, stem_len);
1576 ap->stem[stem_len] = 0;
1577 list_add_tail(&ap->link, &aliases_lookup);
1578 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1579 ap->alias, ap->stem, ap->id, of_node_full_name(np));
1583 * of_alias_scan - Scan all properties of 'aliases' node
1585 * The function scans all the properties of 'aliases' node and populate
1586 * the the global lookup table with the properties. It returns the
1587 * number of alias_prop found, or error code in error case.
1589 * @dt_alloc: An allocator that provides a virtual address to memory
1590 * for the resulting tree
1592 void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1594 struct property *pp;
1596 of_chosen = of_find_node_by_path("/chosen");
1597 if (of_chosen == NULL)
1598 of_chosen = of_find_node_by_path("/chosen@0");
1603 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
1605 of_stdout = of_find_node_by_path(name);
1608 of_aliases = of_find_node_by_path("/aliases");
1612 for_each_property_of_node(of_aliases, pp) {
1613 const char *start = pp->name;
1614 const char *end = start + strlen(start);
1615 struct device_node *np;
1616 struct alias_prop *ap;
1619 /* Skip those we do not want to proceed */
1620 if (!strcmp(pp->name, "name") ||
1621 !strcmp(pp->name, "phandle") ||
1622 !strcmp(pp->name, "linux,phandle"))
1625 np = of_find_node_by_path(pp->value);
1629 /* walk the alias backwards to extract the id and work out
1630 * the 'stem' string */
1631 while (isdigit(*(end-1)) && end > start)
1635 if (kstrtoint(end, 10, &id) < 0)
1638 /* Allocate an alias_prop with enough space for the stem */
1639 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1643 of_alias_add(ap, np, id, start, len);
1648 * of_alias_get_id - Get alias id for the given device_node
1649 * @np: Pointer to the given device_node
1650 * @stem: Alias stem of the given device_node
1652 * The function travels the lookup table to get alias id for the given
1653 * device_node and alias stem. It returns the alias id if find it.
1655 int of_alias_get_id(struct device_node *np, const char *stem)
1657 struct alias_prop *app;
1660 mutex_lock(&of_aliases_mutex);
1661 list_for_each_entry(app, &aliases_lookup, link) {
1662 if (strcmp(app->stem, stem) != 0)
1665 if (np == app->np) {
1670 mutex_unlock(&of_aliases_mutex);
1674 EXPORT_SYMBOL_GPL(of_alias_get_id);
1676 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1679 const void *curv = cur;
1689 curv += sizeof(*cur);
1690 if (curv >= prop->value + prop->length)
1694 *pu = be32_to_cpup(curv);
1697 EXPORT_SYMBOL_GPL(of_prop_next_u32);
1699 const char *of_prop_next_string(struct property *prop, const char *cur)
1701 const void *curv = cur;
1709 curv += strlen(cur) + 1;
1710 if (curv >= prop->value + prop->length)
1715 EXPORT_SYMBOL_GPL(of_prop_next_string);
1718 * of_device_is_stdout_path - check if a device node matches the
1719 * linux,stdout-path property
1721 * Check if this device node matches the linux,stdout-path property
1722 * in the chosen node. return true if yes, false otherwise.
1724 int of_device_is_stdout_path(struct device_node *dn)
1729 return of_stdout == dn;
1731 EXPORT_SYMBOL_GPL(of_device_is_stdout_path);