of: Output devicetree alias names in uevent
[linux-2.6/btrfs-unstable.git] / drivers / of / base.c
blobd1bb50719ed7aa16ca57647d2745976c79c2f087
1 /*
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
13 * Grant Likely.
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>
22 #include <linux/of.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;
36 DEFINE_MUTEX(of_aliases_mutex);
38 /* use when traversing tree through the allnext, child, sibling,
39 * or parent members of struct device_node.
41 DEFINE_RWLOCK(devtree_lock);
43 int of_n_addr_cells(struct device_node *np)
45 const __be32 *ip;
47 do {
48 if (np->parent)
49 np = np->parent;
50 ip = of_get_property(np, "#address-cells", NULL);
51 if (ip)
52 return be32_to_cpup(ip);
53 } while (np->parent);
54 /* No #address-cells property for the root node */
55 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
57 EXPORT_SYMBOL(of_n_addr_cells);
59 int of_n_size_cells(struct device_node *np)
61 const __be32 *ip;
63 do {
64 if (np->parent)
65 np = np->parent;
66 ip = of_get_property(np, "#size-cells", NULL);
67 if (ip)
68 return be32_to_cpup(ip);
69 } while (np->parent);
70 /* No #size-cells property for the root node */
71 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
73 EXPORT_SYMBOL(of_n_size_cells);
75 #if defined(CONFIG_OF_DYNAMIC)
76 /**
77 * of_node_get - Increment refcount of a node
78 * @node: Node to inc refcount, NULL is supported to
79 * simplify writing of callers
81 * Returns node.
83 struct device_node *of_node_get(struct device_node *node)
85 if (node)
86 kref_get(&node->kref);
87 return node;
89 EXPORT_SYMBOL(of_node_get);
91 static inline struct device_node *kref_to_device_node(struct kref *kref)
93 return container_of(kref, struct device_node, kref);
96 /**
97 * of_node_release - release a dynamically allocated node
98 * @kref: kref element of the node to be released
100 * In of_node_put() this function is passed to kref_put()
101 * as the destructor.
103 static void of_node_release(struct kref *kref)
105 struct device_node *node = kref_to_device_node(kref);
106 struct property *prop = node->properties;
108 /* We should never be releasing nodes that haven't been detached. */
109 if (!of_node_check_flag(node, OF_DETACHED)) {
110 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
111 dump_stack();
112 kref_init(&node->kref);
113 return;
116 if (!of_node_check_flag(node, OF_DYNAMIC))
117 return;
119 while (prop) {
120 struct property *next = prop->next;
121 kfree(prop->name);
122 kfree(prop->value);
123 kfree(prop);
124 prop = next;
126 if (!prop) {
127 prop = node->deadprops;
128 node->deadprops = NULL;
131 kfree(node->full_name);
132 kfree(node->data);
133 kfree(node);
137 * of_node_put - Decrement refcount of a node
138 * @node: Node to dec refcount, NULL is supported to
139 * simplify writing of callers
142 void of_node_put(struct device_node *node)
144 if (node)
145 kref_put(&node->kref, of_node_release);
147 EXPORT_SYMBOL(of_node_put);
148 #endif /* CONFIG_OF_DYNAMIC */
150 struct property *of_find_property(const struct device_node *np,
151 const char *name,
152 int *lenp)
154 struct property *pp;
156 if (!np)
157 return NULL;
159 read_lock(&devtree_lock);
160 for (pp = np->properties; pp; pp = pp->next) {
161 if (of_prop_cmp(pp->name, name) == 0) {
162 if (lenp)
163 *lenp = pp->length;
164 break;
167 read_unlock(&devtree_lock);
169 return pp;
171 EXPORT_SYMBOL(of_find_property);
174 * of_find_all_nodes - Get next node in global list
175 * @prev: Previous node or NULL to start iteration
176 * of_node_put() will be called on it
178 * Returns a node pointer with refcount incremented, use
179 * of_node_put() on it when done.
181 struct device_node *of_find_all_nodes(struct device_node *prev)
183 struct device_node *np;
185 read_lock(&devtree_lock);
186 np = prev ? prev->allnext : of_allnodes;
187 for (; np != NULL; np = np->allnext)
188 if (of_node_get(np))
189 break;
190 of_node_put(prev);
191 read_unlock(&devtree_lock);
192 return np;
194 EXPORT_SYMBOL(of_find_all_nodes);
197 * Find a property with a given name for a given node
198 * and return the value.
200 const void *of_get_property(const struct device_node *np, const char *name,
201 int *lenp)
203 struct property *pp = of_find_property(np, name, lenp);
205 return pp ? pp->value : NULL;
207 EXPORT_SYMBOL(of_get_property);
209 /** Checks if the given "compat" string matches one of the strings in
210 * the device's "compatible" property
212 int of_device_is_compatible(const struct device_node *device,
213 const char *compat)
215 const char* cp;
216 int cplen, l;
218 cp = of_get_property(device, "compatible", &cplen);
219 if (cp == NULL)
220 return 0;
221 while (cplen > 0) {
222 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
223 return 1;
224 l = strlen(cp) + 1;
225 cp += l;
226 cplen -= l;
229 return 0;
231 EXPORT_SYMBOL(of_device_is_compatible);
234 * of_machine_is_compatible - Test root of device tree for a given compatible value
235 * @compat: compatible string to look for in root node's compatible property.
237 * Returns true if the root node has the given value in its
238 * compatible property.
240 int of_machine_is_compatible(const char *compat)
242 struct device_node *root;
243 int rc = 0;
245 root = of_find_node_by_path("/");
246 if (root) {
247 rc = of_device_is_compatible(root, compat);
248 of_node_put(root);
250 return rc;
252 EXPORT_SYMBOL(of_machine_is_compatible);
255 * of_device_is_available - check if a device is available for use
257 * @device: Node to check for availability
259 * Returns 1 if the status property is absent or set to "okay" or "ok",
260 * 0 otherwise
262 int of_device_is_available(const struct device_node *device)
264 const char *status;
265 int statlen;
267 status = of_get_property(device, "status", &statlen);
268 if (status == NULL)
269 return 1;
271 if (statlen > 0) {
272 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
273 return 1;
276 return 0;
278 EXPORT_SYMBOL(of_device_is_available);
281 * of_get_parent - Get a node's parent if any
282 * @node: Node to get parent
284 * Returns a node pointer with refcount incremented, use
285 * of_node_put() on it when done.
287 struct device_node *of_get_parent(const struct device_node *node)
289 struct device_node *np;
291 if (!node)
292 return NULL;
294 read_lock(&devtree_lock);
295 np = of_node_get(node->parent);
296 read_unlock(&devtree_lock);
297 return np;
299 EXPORT_SYMBOL(of_get_parent);
302 * of_get_next_parent - Iterate to a node's parent
303 * @node: Node to get parent of
305 * This is like of_get_parent() except that it drops the
306 * refcount on the passed node, making it suitable for iterating
307 * through a node's parents.
309 * Returns a node pointer with refcount incremented, use
310 * of_node_put() on it when done.
312 struct device_node *of_get_next_parent(struct device_node *node)
314 struct device_node *parent;
316 if (!node)
317 return NULL;
319 read_lock(&devtree_lock);
320 parent = of_node_get(node->parent);
321 of_node_put(node);
322 read_unlock(&devtree_lock);
323 return parent;
327 * of_get_next_child - Iterate a node childs
328 * @node: parent node
329 * @prev: previous child of the parent node, or NULL to get first
331 * Returns a node pointer with refcount incremented, use
332 * of_node_put() on it when done.
334 struct device_node *of_get_next_child(const struct device_node *node,
335 struct device_node *prev)
337 struct device_node *next;
339 read_lock(&devtree_lock);
340 next = prev ? prev->sibling : node->child;
341 for (; next; next = next->sibling)
342 if (of_node_get(next))
343 break;
344 of_node_put(prev);
345 read_unlock(&devtree_lock);
346 return next;
348 EXPORT_SYMBOL(of_get_next_child);
351 * of_get_next_available_child - Find the next available child node
352 * @node: parent node
353 * @prev: previous child of the parent node, or NULL to get first
355 * This function is like of_get_next_child(), except that it
356 * automatically skips any disabled nodes (i.e. status = "disabled").
358 struct device_node *of_get_next_available_child(const struct device_node *node,
359 struct device_node *prev)
361 struct device_node *next;
363 read_lock(&devtree_lock);
364 next = prev ? prev->sibling : node->child;
365 for (; next; next = next->sibling) {
366 if (!of_device_is_available(next))
367 continue;
368 if (of_node_get(next))
369 break;
371 of_node_put(prev);
372 read_unlock(&devtree_lock);
373 return next;
375 EXPORT_SYMBOL(of_get_next_available_child);
378 * of_get_child_by_name - Find the child node by name for a given parent
379 * @node: parent node
380 * @name: child name to look for.
382 * This function looks for child node for given matching name
384 * Returns a node pointer if found, with refcount incremented, use
385 * of_node_put() on it when done.
386 * Returns NULL if node is not found.
388 struct device_node *of_get_child_by_name(const struct device_node *node,
389 const char *name)
391 struct device_node *child;
393 for_each_child_of_node(node, child)
394 if (child->name && (of_node_cmp(child->name, name) == 0))
395 break;
396 return child;
398 EXPORT_SYMBOL(of_get_child_by_name);
401 * of_find_node_by_path - Find a node matching a full OF path
402 * @path: The full path to match
404 * Returns a node pointer with refcount incremented, use
405 * of_node_put() on it when done.
407 struct device_node *of_find_node_by_path(const char *path)
409 struct device_node *np = of_allnodes;
411 read_lock(&devtree_lock);
412 for (; np; np = np->allnext) {
413 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
414 && of_node_get(np))
415 break;
417 read_unlock(&devtree_lock);
418 return np;
420 EXPORT_SYMBOL(of_find_node_by_path);
423 * of_find_node_by_name - Find a node by its "name" property
424 * @from: The node to start searching from or NULL, the node
425 * you pass will not be searched, only the next one
426 * will; typically, you pass what the previous call
427 * returned. of_node_put() will be called on it
428 * @name: The name string to match against
430 * Returns a node pointer with refcount incremented, use
431 * of_node_put() on it when done.
433 struct device_node *of_find_node_by_name(struct device_node *from,
434 const char *name)
436 struct device_node *np;
438 read_lock(&devtree_lock);
439 np = from ? from->allnext : of_allnodes;
440 for (; np; np = np->allnext)
441 if (np->name && (of_node_cmp(np->name, name) == 0)
442 && of_node_get(np))
443 break;
444 of_node_put(from);
445 read_unlock(&devtree_lock);
446 return np;
448 EXPORT_SYMBOL(of_find_node_by_name);
451 * of_find_node_by_type - Find a node by its "device_type" property
452 * @from: The node to start searching from, or NULL to start searching
453 * the entire device tree. The node you pass will not be
454 * searched, only the next one will; typically, you pass
455 * what the previous call returned. of_node_put() will be
456 * called on from for you.
457 * @type: The type string to match against
459 * Returns a node pointer with refcount incremented, use
460 * of_node_put() on it when done.
462 struct device_node *of_find_node_by_type(struct device_node *from,
463 const char *type)
465 struct device_node *np;
467 read_lock(&devtree_lock);
468 np = from ? from->allnext : of_allnodes;
469 for (; np; np = np->allnext)
470 if (np->type && (of_node_cmp(np->type, type) == 0)
471 && of_node_get(np))
472 break;
473 of_node_put(from);
474 read_unlock(&devtree_lock);
475 return np;
477 EXPORT_SYMBOL(of_find_node_by_type);
480 * of_find_compatible_node - Find a node based on type and one of the
481 * tokens in its "compatible" property
482 * @from: The node to start searching from or NULL, the node
483 * you pass will not be searched, only the next one
484 * will; typically, you pass what the previous call
485 * returned. of_node_put() will be called on it
486 * @type: The type string to match "device_type" or NULL to ignore
487 * @compatible: The string to match to one of the tokens in the device
488 * "compatible" list.
490 * Returns a node pointer with refcount incremented, use
491 * of_node_put() on it when done.
493 struct device_node *of_find_compatible_node(struct device_node *from,
494 const char *type, const char *compatible)
496 struct device_node *np;
498 read_lock(&devtree_lock);
499 np = from ? from->allnext : of_allnodes;
500 for (; np; np = np->allnext) {
501 if (type
502 && !(np->type && (of_node_cmp(np->type, type) == 0)))
503 continue;
504 if (of_device_is_compatible(np, compatible) && of_node_get(np))
505 break;
507 of_node_put(from);
508 read_unlock(&devtree_lock);
509 return np;
511 EXPORT_SYMBOL(of_find_compatible_node);
514 * of_find_node_with_property - Find a node which has a property with
515 * the given name.
516 * @from: The node to start searching from or NULL, the node
517 * you pass will not be searched, only the next one
518 * will; typically, you pass what the previous call
519 * returned. of_node_put() will be called on it
520 * @prop_name: The name of the property to look for.
522 * Returns a node pointer with refcount incremented, use
523 * of_node_put() on it when done.
525 struct device_node *of_find_node_with_property(struct device_node *from,
526 const char *prop_name)
528 struct device_node *np;
529 struct property *pp;
531 read_lock(&devtree_lock);
532 np = from ? from->allnext : of_allnodes;
533 for (; np; np = np->allnext) {
534 for (pp = np->properties; pp; pp = pp->next) {
535 if (of_prop_cmp(pp->name, prop_name) == 0) {
536 of_node_get(np);
537 goto out;
541 out:
542 of_node_put(from);
543 read_unlock(&devtree_lock);
544 return np;
546 EXPORT_SYMBOL(of_find_node_with_property);
549 * of_match_node - Tell if an device_node has a matching of_match structure
550 * @matches: array of of device match structures to search in
551 * @node: the of device structure to match against
553 * Low level utility function used by device matching.
555 const struct of_device_id *of_match_node(const struct of_device_id *matches,
556 const struct device_node *node)
558 if (!matches)
559 return NULL;
561 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
562 int match = 1;
563 if (matches->name[0])
564 match &= node->name
565 && !strcmp(matches->name, node->name);
566 if (matches->type[0])
567 match &= node->type
568 && !strcmp(matches->type, node->type);
569 if (matches->compatible[0])
570 match &= of_device_is_compatible(node,
571 matches->compatible);
572 if (match)
573 return matches;
574 matches++;
576 return NULL;
578 EXPORT_SYMBOL(of_match_node);
581 * of_find_matching_node_and_match - Find a node based on an of_device_id
582 * match table.
583 * @from: The node to start searching from or NULL, the node
584 * you pass will not be searched, only the next one
585 * will; typically, you pass what the previous call
586 * returned. of_node_put() will be called on it
587 * @matches: array of of device match structures to search in
588 * @match Updated to point at the matches entry which matched
590 * Returns a node pointer with refcount incremented, use
591 * of_node_put() on it when done.
593 struct device_node *of_find_matching_node_and_match(struct device_node *from,
594 const struct of_device_id *matches,
595 const struct of_device_id **match)
597 struct device_node *np;
599 if (match)
600 *match = NULL;
602 read_lock(&devtree_lock);
603 np = from ? from->allnext : of_allnodes;
604 for (; np; np = np->allnext) {
605 if (of_match_node(matches, np) && of_node_get(np)) {
606 if (match)
607 *match = matches;
608 break;
611 of_node_put(from);
612 read_unlock(&devtree_lock);
613 return np;
615 EXPORT_SYMBOL(of_find_matching_node_and_match);
618 * of_modalias_node - Lookup appropriate modalias for a device node
619 * @node: pointer to a device tree node
620 * @modalias: Pointer to buffer that modalias value will be copied into
621 * @len: Length of modalias value
623 * Based on the value of the compatible property, this routine will attempt
624 * to choose an appropriate modalias value for a particular device tree node.
625 * It does this by stripping the manufacturer prefix (as delimited by a ',')
626 * from the first entry in the compatible list property.
628 * This routine returns 0 on success, <0 on failure.
630 int of_modalias_node(struct device_node *node, char *modalias, int len)
632 const char *compatible, *p;
633 int cplen;
635 compatible = of_get_property(node, "compatible", &cplen);
636 if (!compatible || strlen(compatible) > cplen)
637 return -ENODEV;
638 p = strchr(compatible, ',');
639 strlcpy(modalias, p ? p + 1 : compatible, len);
640 return 0;
642 EXPORT_SYMBOL_GPL(of_modalias_node);
645 * of_find_node_by_phandle - Find a node given a phandle
646 * @handle: phandle of the node to find
648 * Returns a node pointer with refcount incremented, use
649 * of_node_put() on it when done.
651 struct device_node *of_find_node_by_phandle(phandle handle)
653 struct device_node *np;
655 read_lock(&devtree_lock);
656 for (np = of_allnodes; np; np = np->allnext)
657 if (np->phandle == handle)
658 break;
659 of_node_get(np);
660 read_unlock(&devtree_lock);
661 return np;
663 EXPORT_SYMBOL(of_find_node_by_phandle);
666 * of_property_read_u8_array - Find and read an array of u8 from a property.
668 * @np: device node from which the property value is to be read.
669 * @propname: name of the property to be searched.
670 * @out_value: pointer to return value, modified only if return value is 0.
671 * @sz: number of array elements to read
673 * Search for a property in a device node and read 8-bit value(s) from
674 * it. Returns 0 on success, -EINVAL if the property does not exist,
675 * -ENODATA if property does not have a value, and -EOVERFLOW if the
676 * property data isn't large enough.
678 * dts entry of array should be like:
679 * property = /bits/ 8 <0x50 0x60 0x70>;
681 * The out_value is modified only if a valid u8 value can be decoded.
683 int of_property_read_u8_array(const struct device_node *np,
684 const char *propname, u8 *out_values, size_t sz)
686 struct property *prop = of_find_property(np, propname, NULL);
687 const u8 *val;
689 if (!prop)
690 return -EINVAL;
691 if (!prop->value)
692 return -ENODATA;
693 if ((sz * sizeof(*out_values)) > prop->length)
694 return -EOVERFLOW;
696 val = prop->value;
697 while (sz--)
698 *out_values++ = *val++;
699 return 0;
701 EXPORT_SYMBOL_GPL(of_property_read_u8_array);
704 * of_property_read_u16_array - Find and read an array of u16 from a property.
706 * @np: device node from which the property value is to be read.
707 * @propname: name of the property to be searched.
708 * @out_value: pointer to return value, modified only if return value is 0.
709 * @sz: number of array elements to read
711 * Search for a property in a device node and read 16-bit value(s) from
712 * it. Returns 0 on success, -EINVAL if the property does not exist,
713 * -ENODATA if property does not have a value, and -EOVERFLOW if the
714 * property data isn't large enough.
716 * dts entry of array should be like:
717 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
719 * The out_value is modified only if a valid u16 value can be decoded.
721 int of_property_read_u16_array(const struct device_node *np,
722 const char *propname, u16 *out_values, size_t sz)
724 struct property *prop = of_find_property(np, propname, NULL);
725 const __be16 *val;
727 if (!prop)
728 return -EINVAL;
729 if (!prop->value)
730 return -ENODATA;
731 if ((sz * sizeof(*out_values)) > prop->length)
732 return -EOVERFLOW;
734 val = prop->value;
735 while (sz--)
736 *out_values++ = be16_to_cpup(val++);
737 return 0;
739 EXPORT_SYMBOL_GPL(of_property_read_u16_array);
742 * of_property_read_u32_array - Find and read an array of 32 bit integers
743 * from a property.
745 * @np: device node from which the property value is to be read.
746 * @propname: name of the property to be searched.
747 * @out_value: pointer to return value, modified only if return value is 0.
748 * @sz: number of array elements to read
750 * Search for a property in a device node and read 32-bit value(s) from
751 * it. Returns 0 on success, -EINVAL if the property does not exist,
752 * -ENODATA if property does not have a value, and -EOVERFLOW if the
753 * property data isn't large enough.
755 * The out_value is modified only if a valid u32 value can be decoded.
757 int of_property_read_u32_array(const struct device_node *np,
758 const char *propname, u32 *out_values,
759 size_t sz)
761 struct property *prop = of_find_property(np, propname, NULL);
762 const __be32 *val;
764 if (!prop)
765 return -EINVAL;
766 if (!prop->value)
767 return -ENODATA;
768 if ((sz * sizeof(*out_values)) > prop->length)
769 return -EOVERFLOW;
771 val = prop->value;
772 while (sz--)
773 *out_values++ = be32_to_cpup(val++);
774 return 0;
776 EXPORT_SYMBOL_GPL(of_property_read_u32_array);
779 * of_property_read_u64 - Find and read a 64 bit integer from a property
780 * @np: device node from which the property value is to be read.
781 * @propname: name of the property to be searched.
782 * @out_value: pointer to return value, modified only if return value is 0.
784 * Search for a property in a device node and read a 64-bit value from
785 * it. Returns 0 on success, -EINVAL if the property does not exist,
786 * -ENODATA if property does not have a value, and -EOVERFLOW if the
787 * property data isn't large enough.
789 * The out_value is modified only if a valid u64 value can be decoded.
791 int of_property_read_u64(const struct device_node *np, const char *propname,
792 u64 *out_value)
794 struct property *prop = of_find_property(np, propname, NULL);
796 if (!prop)
797 return -EINVAL;
798 if (!prop->value)
799 return -ENODATA;
800 if (sizeof(*out_value) > prop->length)
801 return -EOVERFLOW;
802 *out_value = of_read_number(prop->value, 2);
803 return 0;
805 EXPORT_SYMBOL_GPL(of_property_read_u64);
808 * of_property_read_string - Find and read a string from a property
809 * @np: device node from which the property value is to be read.
810 * @propname: name of the property to be searched.
811 * @out_string: pointer to null terminated return string, modified only if
812 * return value is 0.
814 * Search for a property in a device tree node and retrieve a null
815 * terminated string value (pointer to data, not a copy). Returns 0 on
816 * success, -EINVAL if the property does not exist, -ENODATA if property
817 * does not have a value, and -EILSEQ if the string is not null-terminated
818 * within the length of the property data.
820 * The out_string pointer is modified only if a valid string can be decoded.
822 int of_property_read_string(struct device_node *np, const char *propname,
823 const char **out_string)
825 struct property *prop = of_find_property(np, propname, NULL);
826 if (!prop)
827 return -EINVAL;
828 if (!prop->value)
829 return -ENODATA;
830 if (strnlen(prop->value, prop->length) >= prop->length)
831 return -EILSEQ;
832 *out_string = prop->value;
833 return 0;
835 EXPORT_SYMBOL_GPL(of_property_read_string);
838 * of_property_read_string_index - Find and read a string from a multiple
839 * strings property.
840 * @np: device node from which the property value is to be read.
841 * @propname: name of the property to be searched.
842 * @index: index of the string in the list of strings
843 * @out_string: pointer to null terminated return string, modified only if
844 * return value is 0.
846 * Search for a property in a device tree node and retrieve a null
847 * terminated string value (pointer to data, not a copy) in the list of strings
848 * contained in that property.
849 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
850 * property does not have a value, and -EILSEQ if the string is not
851 * null-terminated within the length of the property data.
853 * The out_string pointer is modified only if a valid string can be decoded.
855 int of_property_read_string_index(struct device_node *np, const char *propname,
856 int index, const char **output)
858 struct property *prop = of_find_property(np, propname, NULL);
859 int i = 0;
860 size_t l = 0, total = 0;
861 const char *p;
863 if (!prop)
864 return -EINVAL;
865 if (!prop->value)
866 return -ENODATA;
867 if (strnlen(prop->value, prop->length) >= prop->length)
868 return -EILSEQ;
870 p = prop->value;
872 for (i = 0; total < prop->length; total += l, p += l) {
873 l = strlen(p) + 1;
874 if (i++ == index) {
875 *output = p;
876 return 0;
879 return -ENODATA;
881 EXPORT_SYMBOL_GPL(of_property_read_string_index);
884 * of_property_match_string() - Find string in a list and return index
885 * @np: pointer to node containing string list property
886 * @propname: string list property name
887 * @string: pointer to string to search for in string list
889 * This function searches a string list property and returns the index
890 * of a specific string value.
892 int of_property_match_string(struct device_node *np, const char *propname,
893 const char *string)
895 struct property *prop = of_find_property(np, propname, NULL);
896 size_t l;
897 int i;
898 const char *p, *end;
900 if (!prop)
901 return -EINVAL;
902 if (!prop->value)
903 return -ENODATA;
905 p = prop->value;
906 end = p + prop->length;
908 for (i = 0; p < end; i++, p += l) {
909 l = strlen(p) + 1;
910 if (p + l > end)
911 return -EILSEQ;
912 pr_debug("comparing %s with %s\n", string, p);
913 if (strcmp(string, p) == 0)
914 return i; /* Found it; return index */
916 return -ENODATA;
918 EXPORT_SYMBOL_GPL(of_property_match_string);
921 * of_property_count_strings - Find and return the number of strings from a
922 * multiple strings property.
923 * @np: device node from which the property value is to be read.
924 * @propname: name of the property to be searched.
926 * Search for a property in a device tree node and retrieve the number of null
927 * terminated string contain in it. Returns the number of strings on
928 * success, -EINVAL if the property does not exist, -ENODATA if property
929 * does not have a value, and -EILSEQ if the string is not null-terminated
930 * within the length of the property data.
932 int of_property_count_strings(struct device_node *np, const char *propname)
934 struct property *prop = of_find_property(np, propname, NULL);
935 int i = 0;
936 size_t l = 0, total = 0;
937 const char *p;
939 if (!prop)
940 return -EINVAL;
941 if (!prop->value)
942 return -ENODATA;
943 if (strnlen(prop->value, prop->length) >= prop->length)
944 return -EILSEQ;
946 p = prop->value;
948 for (i = 0; total < prop->length; total += l, p += l, i++)
949 l = strlen(p) + 1;
951 return i;
953 EXPORT_SYMBOL_GPL(of_property_count_strings);
956 * of_parse_phandle - Resolve a phandle property to a device_node pointer
957 * @np: Pointer to device node holding phandle property
958 * @phandle_name: Name of property holding a phandle value
959 * @index: For properties holding a table of phandles, this is the index into
960 * the table
962 * Returns the device_node pointer with refcount incremented. Use
963 * of_node_put() on it when done.
965 struct device_node *of_parse_phandle(const struct device_node *np,
966 const char *phandle_name, int index)
968 const __be32 *phandle;
969 int size;
971 phandle = of_get_property(np, phandle_name, &size);
972 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
973 return NULL;
975 return of_find_node_by_phandle(be32_to_cpup(phandle + index));
977 EXPORT_SYMBOL(of_parse_phandle);
980 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
981 * @np: pointer to a device tree node containing a list
982 * @list_name: property name that contains a list
983 * @cells_name: property name that specifies phandles' arguments count
984 * @index: index of a phandle to parse out
985 * @out_args: optional pointer to output arguments structure (will be filled)
987 * This function is useful to parse lists of phandles and their arguments.
988 * Returns 0 on success and fills out_args, on error returns appropriate
989 * errno value.
991 * Caller is responsible to call of_node_put() on the returned out_args->node
992 * pointer.
994 * Example:
996 * phandle1: node1 {
997 * #list-cells = <2>;
1000 * phandle2: node2 {
1001 * #list-cells = <1>;
1004 * node3 {
1005 * list = <&phandle1 1 2 &phandle2 3>;
1008 * To get a device_node of the `node2' node you may call this:
1009 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
1011 int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1012 const char *cells_name, int index,
1013 struct of_phandle_args *out_args)
1015 const __be32 *list, *list_end;
1016 int size, cur_index = 0;
1017 uint32_t count = 0;
1018 struct device_node *node = NULL;
1019 phandle phandle;
1021 /* Retrieve the phandle list property */
1022 list = of_get_property(np, list_name, &size);
1023 if (!list)
1024 return -ENOENT;
1025 list_end = list + size / sizeof(*list);
1027 /* Loop over the phandles until all the requested entry is found */
1028 while (list < list_end) {
1029 count = 0;
1032 * If phandle is 0, then it is an empty entry with no
1033 * arguments. Skip forward to the next entry.
1035 phandle = be32_to_cpup(list++);
1036 if (phandle) {
1038 * Find the provider node and parse the #*-cells
1039 * property to determine the argument length
1041 node = of_find_node_by_phandle(phandle);
1042 if (!node) {
1043 pr_err("%s: could not find phandle\n",
1044 np->full_name);
1045 break;
1047 if (of_property_read_u32(node, cells_name, &count)) {
1048 pr_err("%s: could not get %s for %s\n",
1049 np->full_name, cells_name,
1050 node->full_name);
1051 break;
1055 * Make sure that the arguments actually fit in the
1056 * remaining property data length
1058 if (list + count > list_end) {
1059 pr_err("%s: arguments longer than property\n",
1060 np->full_name);
1061 break;
1066 * All of the error cases above bail out of the loop, so at
1067 * this point, the parsing is successful. If the requested
1068 * index matches, then fill the out_args structure and return,
1069 * or return -ENOENT for an empty entry.
1071 if (cur_index == index) {
1072 if (!phandle)
1073 return -ENOENT;
1075 if (out_args) {
1076 int i;
1077 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1078 count = MAX_PHANDLE_ARGS;
1079 out_args->np = node;
1080 out_args->args_count = count;
1081 for (i = 0; i < count; i++)
1082 out_args->args[i] = be32_to_cpup(list++);
1084 return 0;
1087 of_node_put(node);
1088 node = NULL;
1089 list += count;
1090 cur_index++;
1093 /* Loop exited without finding a valid entry; return an error */
1094 if (node)
1095 of_node_put(node);
1096 return -EINVAL;
1098 EXPORT_SYMBOL(of_parse_phandle_with_args);
1100 #if defined(CONFIG_OF_DYNAMIC)
1101 static int of_property_notify(int action, struct device_node *np,
1102 struct property *prop)
1104 struct of_prop_reconfig pr;
1106 pr.dn = np;
1107 pr.prop = prop;
1108 return of_reconfig_notify(action, &pr);
1110 #else
1111 static int of_property_notify(int action, struct device_node *np,
1112 struct property *prop)
1114 return 0;
1116 #endif
1119 * of_add_property - Add a property to a node
1121 int of_add_property(struct device_node *np, struct property *prop)
1123 struct property **next;
1124 unsigned long flags;
1125 int rc;
1127 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1128 if (rc)
1129 return rc;
1131 prop->next = NULL;
1132 write_lock_irqsave(&devtree_lock, flags);
1133 next = &np->properties;
1134 while (*next) {
1135 if (strcmp(prop->name, (*next)->name) == 0) {
1136 /* duplicate ! don't insert it */
1137 write_unlock_irqrestore(&devtree_lock, flags);
1138 return -1;
1140 next = &(*next)->next;
1142 *next = prop;
1143 write_unlock_irqrestore(&devtree_lock, flags);
1145 #ifdef CONFIG_PROC_DEVICETREE
1146 /* try to add to proc as well if it was initialized */
1147 if (np->pde)
1148 proc_device_tree_add_prop(np->pde, prop);
1149 #endif /* CONFIG_PROC_DEVICETREE */
1151 return 0;
1155 * of_remove_property - Remove a property from a node.
1157 * Note that we don't actually remove it, since we have given out
1158 * who-knows-how-many pointers to the data using get-property.
1159 * Instead we just move the property to the "dead properties"
1160 * list, so it won't be found any more.
1162 int of_remove_property(struct device_node *np, struct property *prop)
1164 struct property **next;
1165 unsigned long flags;
1166 int found = 0;
1167 int rc;
1169 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1170 if (rc)
1171 return rc;
1173 write_lock_irqsave(&devtree_lock, flags);
1174 next = &np->properties;
1175 while (*next) {
1176 if (*next == prop) {
1177 /* found the node */
1178 *next = prop->next;
1179 prop->next = np->deadprops;
1180 np->deadprops = prop;
1181 found = 1;
1182 break;
1184 next = &(*next)->next;
1186 write_unlock_irqrestore(&devtree_lock, flags);
1188 if (!found)
1189 return -ENODEV;
1191 #ifdef CONFIG_PROC_DEVICETREE
1192 /* try to remove the proc node as well */
1193 if (np->pde)
1194 proc_device_tree_remove_prop(np->pde, prop);
1195 #endif /* CONFIG_PROC_DEVICETREE */
1197 return 0;
1201 * of_update_property - Update a property in a node, if the property does
1202 * not exist, add it.
1204 * Note that we don't actually remove it, since we have given out
1205 * who-knows-how-many pointers to the data using get-property.
1206 * Instead we just move the property to the "dead properties" list,
1207 * and add the new property to the property list
1209 int of_update_property(struct device_node *np, struct property *newprop)
1211 struct property **next, *oldprop;
1212 unsigned long flags;
1213 int rc, found = 0;
1215 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1216 if (rc)
1217 return rc;
1219 if (!newprop->name)
1220 return -EINVAL;
1222 oldprop = of_find_property(np, newprop->name, NULL);
1223 if (!oldprop)
1224 return of_add_property(np, newprop);
1226 write_lock_irqsave(&devtree_lock, flags);
1227 next = &np->properties;
1228 while (*next) {
1229 if (*next == oldprop) {
1230 /* found the node */
1231 newprop->next = oldprop->next;
1232 *next = newprop;
1233 oldprop->next = np->deadprops;
1234 np->deadprops = oldprop;
1235 found = 1;
1236 break;
1238 next = &(*next)->next;
1240 write_unlock_irqrestore(&devtree_lock, flags);
1242 if (!found)
1243 return -ENODEV;
1245 #ifdef CONFIG_PROC_DEVICETREE
1246 /* try to add to proc as well if it was initialized */
1247 if (np->pde)
1248 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1249 #endif /* CONFIG_PROC_DEVICETREE */
1251 return 0;
1254 #if defined(CONFIG_OF_DYNAMIC)
1256 * Support for dynamic device trees.
1258 * On some platforms, the device tree can be manipulated at runtime.
1259 * The routines in this section support adding, removing and changing
1260 * device tree nodes.
1263 static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1265 int of_reconfig_notifier_register(struct notifier_block *nb)
1267 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1269 EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
1271 int of_reconfig_notifier_unregister(struct notifier_block *nb)
1273 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1275 EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
1277 int of_reconfig_notify(unsigned long action, void *p)
1279 int rc;
1281 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1282 return notifier_to_errno(rc);
1285 #ifdef CONFIG_PROC_DEVICETREE
1286 static void of_add_proc_dt_entry(struct device_node *dn)
1288 struct proc_dir_entry *ent;
1290 ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
1291 if (ent)
1292 proc_device_tree_add_node(dn, ent);
1294 #else
1295 static void of_add_proc_dt_entry(struct device_node *dn)
1297 return;
1299 #endif
1302 * of_attach_node - Plug a device node into the tree and global list.
1304 int of_attach_node(struct device_node *np)
1306 unsigned long flags;
1307 int rc;
1309 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1310 if (rc)
1311 return rc;
1313 write_lock_irqsave(&devtree_lock, flags);
1314 np->sibling = np->parent->child;
1315 np->allnext = of_allnodes;
1316 np->parent->child = np;
1317 of_allnodes = np;
1318 write_unlock_irqrestore(&devtree_lock, flags);
1320 of_add_proc_dt_entry(np);
1321 return 0;
1324 #ifdef CONFIG_PROC_DEVICETREE
1325 static void of_remove_proc_dt_entry(struct device_node *dn)
1327 struct device_node *parent = dn->parent;
1328 struct property *prop = dn->properties;
1330 while (prop) {
1331 remove_proc_entry(prop->name, dn->pde);
1332 prop = prop->next;
1335 if (dn->pde)
1336 remove_proc_entry(dn->pde->name, parent->pde);
1338 #else
1339 static void of_remove_proc_dt_entry(struct device_node *dn)
1341 return;
1343 #endif
1346 * of_detach_node - "Unplug" a node from the device tree.
1348 * The caller must hold a reference to the node. The memory associated with
1349 * the node is not freed until its refcount goes to zero.
1351 int of_detach_node(struct device_node *np)
1353 struct device_node *parent;
1354 unsigned long flags;
1355 int rc = 0;
1357 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1358 if (rc)
1359 return rc;
1361 write_lock_irqsave(&devtree_lock, flags);
1363 if (of_node_check_flag(np, OF_DETACHED)) {
1364 /* someone already detached it */
1365 write_unlock_irqrestore(&devtree_lock, flags);
1366 return rc;
1369 parent = np->parent;
1370 if (!parent) {
1371 write_unlock_irqrestore(&devtree_lock, flags);
1372 return rc;
1375 if (of_allnodes == np)
1376 of_allnodes = np->allnext;
1377 else {
1378 struct device_node *prev;
1379 for (prev = of_allnodes;
1380 prev->allnext != np;
1381 prev = prev->allnext)
1383 prev->allnext = np->allnext;
1386 if (parent->child == np)
1387 parent->child = np->sibling;
1388 else {
1389 struct device_node *prevsib;
1390 for (prevsib = np->parent->child;
1391 prevsib->sibling != np;
1392 prevsib = prevsib->sibling)
1394 prevsib->sibling = np->sibling;
1397 of_node_set_flag(np, OF_DETACHED);
1398 write_unlock_irqrestore(&devtree_lock, flags);
1400 of_remove_proc_dt_entry(np);
1401 return rc;
1403 #endif /* defined(CONFIG_OF_DYNAMIC) */
1405 static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1406 int id, const char *stem, int stem_len)
1408 ap->np = np;
1409 ap->id = id;
1410 strncpy(ap->stem, stem, stem_len);
1411 ap->stem[stem_len] = 0;
1412 list_add_tail(&ap->link, &aliases_lookup);
1413 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
1414 ap->alias, ap->stem, ap->id, of_node_full_name(np));
1418 * of_alias_scan - Scan all properties of 'aliases' node
1420 * The function scans all the properties of 'aliases' node and populate
1421 * the the global lookup table with the properties. It returns the
1422 * number of alias_prop found, or error code in error case.
1424 * @dt_alloc: An allocator that provides a virtual address to memory
1425 * for the resulting tree
1427 void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1429 struct property *pp;
1431 of_chosen = of_find_node_by_path("/chosen");
1432 if (of_chosen == NULL)
1433 of_chosen = of_find_node_by_path("/chosen@0");
1434 of_aliases = of_find_node_by_path("/aliases");
1435 if (!of_aliases)
1436 return;
1438 for_each_property_of_node(of_aliases, pp) {
1439 const char *start = pp->name;
1440 const char *end = start + strlen(start);
1441 struct device_node *np;
1442 struct alias_prop *ap;
1443 int id, len;
1445 /* Skip those we do not want to proceed */
1446 if (!strcmp(pp->name, "name") ||
1447 !strcmp(pp->name, "phandle") ||
1448 !strcmp(pp->name, "linux,phandle"))
1449 continue;
1451 np = of_find_node_by_path(pp->value);
1452 if (!np)
1453 continue;
1455 /* walk the alias backwards to extract the id and work out
1456 * the 'stem' string */
1457 while (isdigit(*(end-1)) && end > start)
1458 end--;
1459 len = end - start;
1461 if (kstrtoint(end, 10, &id) < 0)
1462 continue;
1464 /* Allocate an alias_prop with enough space for the stem */
1465 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1466 if (!ap)
1467 continue;
1468 ap->alias = start;
1469 of_alias_add(ap, np, id, start, len);
1474 * of_alias_get_id - Get alias id for the given device_node
1475 * @np: Pointer to the given device_node
1476 * @stem: Alias stem of the given device_node
1478 * The function travels the lookup table to get alias id for the given
1479 * device_node and alias stem. It returns the alias id if find it.
1481 int of_alias_get_id(struct device_node *np, const char *stem)
1483 struct alias_prop *app;
1484 int id = -ENODEV;
1486 mutex_lock(&of_aliases_mutex);
1487 list_for_each_entry(app, &aliases_lookup, link) {
1488 if (strcmp(app->stem, stem) != 0)
1489 continue;
1491 if (np == app->np) {
1492 id = app->id;
1493 break;
1496 mutex_unlock(&of_aliases_mutex);
1498 return id;
1500 EXPORT_SYMBOL_GPL(of_alias_get_id);
1502 const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1503 u32 *pu)
1505 const void *curv = cur;
1507 if (!prop)
1508 return NULL;
1510 if (!cur) {
1511 curv = prop->value;
1512 goto out_val;
1515 curv += sizeof(*cur);
1516 if (curv >= prop->value + prop->length)
1517 return NULL;
1519 out_val:
1520 *pu = be32_to_cpup(curv);
1521 return curv;
1523 EXPORT_SYMBOL_GPL(of_prop_next_u32);
1525 const char *of_prop_next_string(struct property *prop, const char *cur)
1527 const void *curv = cur;
1529 if (!prop)
1530 return NULL;
1532 if (!cur)
1533 return prop->value;
1535 curv += strlen(cur) + 1;
1536 if (curv >= prop->value + prop->length)
1537 return NULL;
1539 return curv;
1541 EXPORT_SYMBOL_GPL(of_prop_next_string);