of: Add support for reading a u32 from a multi-value property.
[deliverable/linux.git] / drivers / of / base.c
CommitLineData
97e873e5
SR
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 *
e91edcf5
GL
12 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
13 * Grant Likely.
97e873e5
SR
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 */
611cad72 20#include <linux/ctype.h>
97e873e5
SR
21#include <linux/module.h>
22#include <linux/of.h>
581b605a 23#include <linux/spinlock.h>
5a0e3ad6 24#include <linux/slab.h>
a9f2f63a 25#include <linux/proc_fs.h>
581b605a 26
ced4eec9 27#include "of_private.h"
611cad72 28
ced4eec9 29LIST_HEAD(aliases_lookup);
611cad72 30
465aac6d
RD
31struct device_node *of_allnodes;
32EXPORT_SYMBOL(of_allnodes);
fc0bdae4 33struct device_node *of_chosen;
611cad72
SG
34struct device_node *of_aliases;
35
ced4eec9 36DEFINE_MUTEX(of_aliases_mutex);
1ef4d424 37
581b605a
SR
38/* use when traversing tree through the allnext, child, sibling,
39 * or parent members of struct device_node.
40 */
d6d3c4e6 41DEFINE_RAW_SPINLOCK(devtree_lock);
97e873e5
SR
42
43int of_n_addr_cells(struct device_node *np)
44{
a9fadeef 45 const __be32 *ip;
97e873e5
SR
46
47 do {
48 if (np->parent)
49 np = np->parent;
50 ip = of_get_property(np, "#address-cells", NULL);
51 if (ip)
33714881 52 return be32_to_cpup(ip);
97e873e5
SR
53 } while (np->parent);
54 /* No #address-cells property for the root node */
55 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
56}
57EXPORT_SYMBOL(of_n_addr_cells);
58
59int of_n_size_cells(struct device_node *np)
60{
a9fadeef 61 const __be32 *ip;
97e873e5
SR
62
63 do {
64 if (np->parent)
65 np = np->parent;
66 ip = of_get_property(np, "#size-cells", NULL);
67 if (ip)
33714881 68 return be32_to_cpup(ip);
97e873e5
SR
69 } while (np->parent);
70 /* No #size-cells property for the root node */
71 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
72}
73EXPORT_SYMBOL(of_n_size_cells);
74
0f22dd39 75#if defined(CONFIG_OF_DYNAMIC)
923f7e30
GL
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
80 *
81 * Returns node.
82 */
83struct device_node *of_node_get(struct device_node *node)
84{
85 if (node)
86 kref_get(&node->kref);
87 return node;
88}
89EXPORT_SYMBOL(of_node_get);
90
91static inline struct device_node *kref_to_device_node(struct kref *kref)
92{
93 return container_of(kref, struct device_node, kref);
94}
95
96/**
97 * of_node_release - release a dynamically allocated node
98 * @kref: kref element of the node to be released
99 *
100 * In of_node_put() this function is passed to kref_put()
101 * as the destructor.
102 */
103static void of_node_release(struct kref *kref)
104{
105 struct device_node *node = kref_to_device_node(kref);
106 struct property *prop = node->properties;
107
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;
114 }
115
116 if (!of_node_check_flag(node, OF_DYNAMIC))
117 return;
118
119 while (prop) {
120 struct property *next = prop->next;
121 kfree(prop->name);
122 kfree(prop->value);
123 kfree(prop);
124 prop = next;
125
126 if (!prop) {
127 prop = node->deadprops;
128 node->deadprops = NULL;
129 }
130 }
131 kfree(node->full_name);
132 kfree(node->data);
133 kfree(node);
134}
135
136/**
137 * of_node_put - Decrement refcount of a node
138 * @node: Node to dec refcount, NULL is supported to
139 * simplify writing of callers
140 *
141 */
142void of_node_put(struct device_node *node)
143{
144 if (node)
145 kref_put(&node->kref, of_node_release);
146}
147EXPORT_SYMBOL(of_node_put);
0f22dd39 148#endif /* CONFIG_OF_DYNAMIC */
923f7e30 149
28d0e36b
TG
150static struct property *__of_find_property(const struct device_node *np,
151 const char *name, int *lenp)
581b605a
SR
152{
153 struct property *pp;
154
64e4566f
TT
155 if (!np)
156 return NULL;
157
a3a7cab1 158 for (pp = np->properties; pp; pp = pp->next) {
581b605a 159 if (of_prop_cmp(pp->name, name) == 0) {
a3a7cab1 160 if (lenp)
581b605a
SR
161 *lenp = pp->length;
162 break;
163 }
164 }
28d0e36b
TG
165
166 return pp;
167}
168
169struct property *of_find_property(const struct device_node *np,
170 const char *name,
171 int *lenp)
172{
173 struct property *pp;
d6d3c4e6 174 unsigned long flags;
28d0e36b 175
d6d3c4e6 176 raw_spin_lock_irqsave(&devtree_lock, flags);
28d0e36b 177 pp = __of_find_property(np, name, lenp);
d6d3c4e6 178 raw_spin_unlock_irqrestore(&devtree_lock, flags);
581b605a
SR
179
180 return pp;
181}
182EXPORT_SYMBOL(of_find_property);
183
e91edcf5
GL
184/**
185 * of_find_all_nodes - Get next node in global list
186 * @prev: Previous node or NULL to start iteration
187 * of_node_put() will be called on it
188 *
189 * Returns a node pointer with refcount incremented, use
190 * of_node_put() on it when done.
191 */
192struct device_node *of_find_all_nodes(struct device_node *prev)
193{
194 struct device_node *np;
195
d6d3c4e6 196 raw_spin_lock(&devtree_lock);
465aac6d 197 np = prev ? prev->allnext : of_allnodes;
e91edcf5
GL
198 for (; np != NULL; np = np->allnext)
199 if (of_node_get(np))
200 break;
201 of_node_put(prev);
d6d3c4e6 202 raw_spin_unlock(&devtree_lock);
e91edcf5
GL
203 return np;
204}
205EXPORT_SYMBOL(of_find_all_nodes);
206
28d0e36b
TG
207/*
208 * Find a property with a given name for a given node
209 * and return the value.
210 */
211static const void *__of_get_property(const struct device_node *np,
212 const char *name, int *lenp)
213{
214 struct property *pp = __of_find_property(np, name, lenp);
215
216 return pp ? pp->value : NULL;
217}
218
97e873e5
SR
219/*
220 * Find a property with a given name for a given node
221 * and return the value.
222 */
223const void *of_get_property(const struct device_node *np, const char *name,
28d0e36b 224 int *lenp)
97e873e5
SR
225{
226 struct property *pp = of_find_property(np, name, lenp);
227
228 return pp ? pp->value : NULL;
229}
230EXPORT_SYMBOL(of_get_property);
0081cbc3
SR
231
232/** Checks if the given "compat" string matches one of the strings in
233 * the device's "compatible" property
234 */
28d0e36b
TG
235static int __of_device_is_compatible(const struct device_node *device,
236 const char *compat)
0081cbc3
SR
237{
238 const char* cp;
239 int cplen, l;
240
28d0e36b 241 cp = __of_get_property(device, "compatible", &cplen);
0081cbc3
SR
242 if (cp == NULL)
243 return 0;
244 while (cplen > 0) {
245 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
246 return 1;
247 l = strlen(cp) + 1;
248 cp += l;
249 cplen -= l;
250 }
251
252 return 0;
253}
28d0e36b
TG
254
255/** Checks if the given "compat" string matches one of the strings in
256 * the device's "compatible" property
257 */
258int of_device_is_compatible(const struct device_node *device,
259 const char *compat)
260{
d6d3c4e6 261 unsigned long flags;
28d0e36b
TG
262 int res;
263
d6d3c4e6 264 raw_spin_lock_irqsave(&devtree_lock, flags);
28d0e36b 265 res = __of_device_is_compatible(device, compat);
d6d3c4e6 266 raw_spin_unlock_irqrestore(&devtree_lock, flags);
28d0e36b
TG
267 return res;
268}
0081cbc3 269EXPORT_SYMBOL(of_device_is_compatible);
e679c5f4 270
1f43cfb9 271/**
71a157e8 272 * of_machine_is_compatible - Test root of device tree for a given compatible value
1f43cfb9
GL
273 * @compat: compatible string to look for in root node's compatible property.
274 *
275 * Returns true if the root node has the given value in its
276 * compatible property.
277 */
71a157e8 278int of_machine_is_compatible(const char *compat)
1f43cfb9
GL
279{
280 struct device_node *root;
281 int rc = 0;
282
283 root = of_find_node_by_path("/");
284 if (root) {
285 rc = of_device_is_compatible(root, compat);
286 of_node_put(root);
287 }
288 return rc;
289}
71a157e8 290EXPORT_SYMBOL(of_machine_is_compatible);
1f43cfb9 291
834d97d4 292/**
c31a0c05 293 * __of_device_is_available - check if a device is available for use
834d97d4 294 *
c31a0c05 295 * @device: Node to check for availability, with locks already held
834d97d4
JB
296 *
297 * Returns 1 if the status property is absent or set to "okay" or "ok",
298 * 0 otherwise
299 */
c31a0c05 300static int __of_device_is_available(const struct device_node *device)
834d97d4
JB
301{
302 const char *status;
303 int statlen;
304
c31a0c05 305 status = __of_get_property(device, "status", &statlen);
834d97d4
JB
306 if (status == NULL)
307 return 1;
308
309 if (statlen > 0) {
310 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
311 return 1;
312 }
313
314 return 0;
315}
c31a0c05
SW
316
317/**
318 * of_device_is_available - check if a device is available for use
319 *
320 * @device: Node to check for availability
321 *
322 * Returns 1 if the status property is absent or set to "okay" or "ok",
323 * 0 otherwise
324 */
325int of_device_is_available(const struct device_node *device)
326{
327 unsigned long flags;
328 int res;
329
330 raw_spin_lock_irqsave(&devtree_lock, flags);
331 res = __of_device_is_available(device);
332 raw_spin_unlock_irqrestore(&devtree_lock, flags);
333 return res;
334
335}
834d97d4
JB
336EXPORT_SYMBOL(of_device_is_available);
337
e679c5f4
SR
338/**
339 * of_get_parent - Get a node's parent if any
340 * @node: Node to get parent
341 *
342 * Returns a node pointer with refcount incremented, use
343 * of_node_put() on it when done.
344 */
345struct device_node *of_get_parent(const struct device_node *node)
346{
347 struct device_node *np;
d6d3c4e6 348 unsigned long flags;
e679c5f4
SR
349
350 if (!node)
351 return NULL;
352
d6d3c4e6 353 raw_spin_lock_irqsave(&devtree_lock, flags);
e679c5f4 354 np = of_node_get(node->parent);
d6d3c4e6 355 raw_spin_unlock_irqrestore(&devtree_lock, flags);
e679c5f4
SR
356 return np;
357}
358EXPORT_SYMBOL(of_get_parent);
d1cd355a 359
f4eb0107
ME
360/**
361 * of_get_next_parent - Iterate to a node's parent
362 * @node: Node to get parent of
363 *
364 * This is like of_get_parent() except that it drops the
365 * refcount on the passed node, making it suitable for iterating
366 * through a node's parents.
367 *
368 * Returns a node pointer with refcount incremented, use
369 * of_node_put() on it when done.
370 */
371struct device_node *of_get_next_parent(struct device_node *node)
372{
373 struct device_node *parent;
d6d3c4e6 374 unsigned long flags;
f4eb0107
ME
375
376 if (!node)
377 return NULL;
378
d6d3c4e6 379 raw_spin_lock_irqsave(&devtree_lock, flags);
f4eb0107
ME
380 parent = of_node_get(node->parent);
381 of_node_put(node);
d6d3c4e6 382 raw_spin_unlock_irqrestore(&devtree_lock, flags);
f4eb0107
ME
383 return parent;
384}
385
d1cd355a
SR
386/**
387 * of_get_next_child - Iterate a node childs
388 * @node: parent node
389 * @prev: previous child of the parent node, or NULL to get first
390 *
391 * Returns a node pointer with refcount incremented, use
392 * of_node_put() on it when done.
393 */
394struct device_node *of_get_next_child(const struct device_node *node,
395 struct device_node *prev)
396{
397 struct device_node *next;
d6d3c4e6 398 unsigned long flags;
d1cd355a 399
d6d3c4e6 400 raw_spin_lock_irqsave(&devtree_lock, flags);
d1cd355a
SR
401 next = prev ? prev->sibling : node->child;
402 for (; next; next = next->sibling)
403 if (of_node_get(next))
404 break;
405 of_node_put(prev);
d6d3c4e6 406 raw_spin_unlock_irqrestore(&devtree_lock, flags);
d1cd355a
SR
407 return next;
408}
409EXPORT_SYMBOL(of_get_next_child);
1ef4d424 410
3296193d
TT
411/**
412 * of_get_next_available_child - Find the next available child node
413 * @node: parent node
414 * @prev: previous child of the parent node, or NULL to get first
415 *
416 * This function is like of_get_next_child(), except that it
417 * automatically skips any disabled nodes (i.e. status = "disabled").
418 */
419struct device_node *of_get_next_available_child(const struct device_node *node,
420 struct device_node *prev)
421{
422 struct device_node *next;
423
d6d3c4e6 424 raw_spin_lock(&devtree_lock);
3296193d
TT
425 next = prev ? prev->sibling : node->child;
426 for (; next; next = next->sibling) {
c31a0c05 427 if (!__of_device_is_available(next))
3296193d
TT
428 continue;
429 if (of_node_get(next))
430 break;
431 }
432 of_node_put(prev);
d6d3c4e6 433 raw_spin_unlock(&devtree_lock);
3296193d
TT
434 return next;
435}
436EXPORT_SYMBOL(of_get_next_available_child);
437
9c19761a
SK
438/**
439 * of_get_child_by_name - Find the child node by name for a given parent
440 * @node: parent node
441 * @name: child name to look for.
442 *
443 * This function looks for child node for given matching name
444 *
445 * Returns a node pointer if found, with refcount incremented, use
446 * of_node_put() on it when done.
447 * Returns NULL if node is not found.
448 */
449struct device_node *of_get_child_by_name(const struct device_node *node,
450 const char *name)
451{
452 struct device_node *child;
453
454 for_each_child_of_node(node, child)
455 if (child->name && (of_node_cmp(child->name, name) == 0))
456 break;
457 return child;
458}
459EXPORT_SYMBOL(of_get_child_by_name);
460
1ef4d424
SR
461/**
462 * of_find_node_by_path - Find a node matching a full OF path
463 * @path: The full path to match
464 *
465 * Returns a node pointer with refcount incremented, use
466 * of_node_put() on it when done.
467 */
468struct device_node *of_find_node_by_path(const char *path)
469{
465aac6d 470 struct device_node *np = of_allnodes;
d6d3c4e6 471 unsigned long flags;
1ef4d424 472
d6d3c4e6 473 raw_spin_lock_irqsave(&devtree_lock, flags);
1ef4d424
SR
474 for (; np; np = np->allnext) {
475 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
476 && of_node_get(np))
477 break;
478 }
d6d3c4e6 479 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1ef4d424
SR
480 return np;
481}
482EXPORT_SYMBOL(of_find_node_by_path);
483
484/**
485 * of_find_node_by_name - Find a node by its "name" property
486 * @from: The node to start searching from or NULL, the node
487 * you pass will not be searched, only the next one
488 * will; typically, you pass what the previous call
489 * returned. of_node_put() will be called on it
490 * @name: The name string to match against
491 *
492 * Returns a node pointer with refcount incremented, use
493 * of_node_put() on it when done.
494 */
495struct device_node *of_find_node_by_name(struct device_node *from,
496 const char *name)
497{
498 struct device_node *np;
d6d3c4e6 499 unsigned long flags;
1ef4d424 500
d6d3c4e6 501 raw_spin_lock_irqsave(&devtree_lock, flags);
465aac6d 502 np = from ? from->allnext : of_allnodes;
1ef4d424
SR
503 for (; np; np = np->allnext)
504 if (np->name && (of_node_cmp(np->name, name) == 0)
505 && of_node_get(np))
506 break;
507 of_node_put(from);
d6d3c4e6 508 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1ef4d424
SR
509 return np;
510}
511EXPORT_SYMBOL(of_find_node_by_name);
512
513/**
514 * of_find_node_by_type - Find a node by its "device_type" property
515 * @from: The node to start searching from, or NULL to start searching
516 * the entire device tree. The node you pass will not be
517 * searched, only the next one will; typically, you pass
518 * what the previous call returned. of_node_put() will be
519 * called on from for you.
520 * @type: The type string to match against
521 *
522 * Returns a node pointer with refcount incremented, use
523 * of_node_put() on it when done.
524 */
525struct device_node *of_find_node_by_type(struct device_node *from,
526 const char *type)
527{
528 struct device_node *np;
d6d3c4e6 529 unsigned long flags;
1ef4d424 530
d6d3c4e6 531 raw_spin_lock_irqsave(&devtree_lock, flags);
465aac6d 532 np = from ? from->allnext : of_allnodes;
1ef4d424
SR
533 for (; np; np = np->allnext)
534 if (np->type && (of_node_cmp(np->type, type) == 0)
535 && of_node_get(np))
536 break;
537 of_node_put(from);
d6d3c4e6 538 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1ef4d424
SR
539 return np;
540}
541EXPORT_SYMBOL(of_find_node_by_type);
542
543/**
544 * of_find_compatible_node - Find a node based on type and one of the
545 * tokens in its "compatible" property
546 * @from: The node to start searching from or NULL, the node
547 * you pass will not be searched, only the next one
548 * will; typically, you pass what the previous call
549 * returned. of_node_put() will be called on it
550 * @type: The type string to match "device_type" or NULL to ignore
551 * @compatible: The string to match to one of the tokens in the device
552 * "compatible" list.
553 *
554 * Returns a node pointer with refcount incremented, use
555 * of_node_put() on it when done.
556 */
557struct device_node *of_find_compatible_node(struct device_node *from,
558 const char *type, const char *compatible)
559{
560 struct device_node *np;
d6d3c4e6 561 unsigned long flags;
1ef4d424 562
d6d3c4e6 563 raw_spin_lock_irqsave(&devtree_lock, flags);
465aac6d 564 np = from ? from->allnext : of_allnodes;
1ef4d424
SR
565 for (; np; np = np->allnext) {
566 if (type
567 && !(np->type && (of_node_cmp(np->type, type) == 0)))
568 continue;
28d0e36b
TG
569 if (__of_device_is_compatible(np, compatible) &&
570 of_node_get(np))
1ef4d424
SR
571 break;
572 }
573 of_node_put(from);
d6d3c4e6 574 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1ef4d424
SR
575 return np;
576}
577EXPORT_SYMBOL(of_find_compatible_node);
283029d1 578
1e291b14
ME
579/**
580 * of_find_node_with_property - Find a node which has a property with
581 * the given name.
582 * @from: The node to start searching from or NULL, the node
583 * you pass will not be searched, only the next one
584 * will; typically, you pass what the previous call
585 * returned. of_node_put() will be called on it
586 * @prop_name: The name of the property to look for.
587 *
588 * Returns a node pointer with refcount incremented, use
589 * of_node_put() on it when done.
590 */
591struct device_node *of_find_node_with_property(struct device_node *from,
592 const char *prop_name)
593{
594 struct device_node *np;
595 struct property *pp;
d6d3c4e6 596 unsigned long flags;
1e291b14 597
d6d3c4e6 598 raw_spin_lock_irqsave(&devtree_lock, flags);
465aac6d 599 np = from ? from->allnext : of_allnodes;
1e291b14 600 for (; np; np = np->allnext) {
a3a7cab1 601 for (pp = np->properties; pp; pp = pp->next) {
1e291b14
ME
602 if (of_prop_cmp(pp->name, prop_name) == 0) {
603 of_node_get(np);
604 goto out;
605 }
606 }
607 }
608out:
609 of_node_put(from);
d6d3c4e6 610 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1e291b14
ME
611 return np;
612}
613EXPORT_SYMBOL(of_find_node_with_property);
614
28d0e36b
TG
615static
616const struct of_device_id *__of_match_node(const struct of_device_id *matches,
617 const struct device_node *node)
283029d1 618{
a52f07ec
GL
619 if (!matches)
620 return NULL;
621
283029d1
GL
622 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
623 int match = 1;
624 if (matches->name[0])
625 match &= node->name
626 && !strcmp(matches->name, node->name);
627 if (matches->type[0])
628 match &= node->type
629 && !strcmp(matches->type, node->type);
bc51b0c2 630 if (matches->compatible[0])
28d0e36b
TG
631 match &= __of_device_is_compatible(node,
632 matches->compatible);
bc51b0c2 633 if (match)
283029d1
GL
634 return matches;
635 matches++;
636 }
637 return NULL;
638}
28d0e36b
TG
639
640/**
641 * of_match_node - Tell if an device_node has a matching of_match structure
642 * @matches: array of of device match structures to search in
643 * @node: the of device structure to match against
644 *
645 * Low level utility function used by device matching.
646 */
647const struct of_device_id *of_match_node(const struct of_device_id *matches,
648 const struct device_node *node)
649{
650 const struct of_device_id *match;
d6d3c4e6 651 unsigned long flags;
28d0e36b 652
d6d3c4e6 653 raw_spin_lock_irqsave(&devtree_lock, flags);
28d0e36b 654 match = __of_match_node(matches, node);
d6d3c4e6 655 raw_spin_unlock_irqrestore(&devtree_lock, flags);
28d0e36b
TG
656 return match;
657}
283029d1
GL
658EXPORT_SYMBOL(of_match_node);
659
660/**
50c8af4c
SW
661 * of_find_matching_node_and_match - Find a node based on an of_device_id
662 * match table.
283029d1
GL
663 * @from: The node to start searching from or NULL, the node
664 * you pass will not be searched, only the next one
665 * will; typically, you pass what the previous call
666 * returned. of_node_put() will be called on it
667 * @matches: array of of device match structures to search in
50c8af4c 668 * @match Updated to point at the matches entry which matched
283029d1
GL
669 *
670 * Returns a node pointer with refcount incremented, use
671 * of_node_put() on it when done.
672 */
50c8af4c
SW
673struct device_node *of_find_matching_node_and_match(struct device_node *from,
674 const struct of_device_id *matches,
675 const struct of_device_id **match)
283029d1
GL
676{
677 struct device_node *np;
dc71bcf1 678 const struct of_device_id *m;
d6d3c4e6 679 unsigned long flags;
283029d1 680
50c8af4c
SW
681 if (match)
682 *match = NULL;
683
d6d3c4e6 684 raw_spin_lock_irqsave(&devtree_lock, flags);
465aac6d 685 np = from ? from->allnext : of_allnodes;
283029d1 686 for (; np; np = np->allnext) {
28d0e36b 687 m = __of_match_node(matches, np);
dc71bcf1 688 if (m && of_node_get(np)) {
50c8af4c 689 if (match)
dc71bcf1 690 *match = m;
283029d1 691 break;
50c8af4c 692 }
283029d1
GL
693 }
694 of_node_put(from);
d6d3c4e6 695 raw_spin_unlock_irqrestore(&devtree_lock, flags);
283029d1
GL
696 return np;
697}
80c2022e 698EXPORT_SYMBOL(of_find_matching_node_and_match);
3f07af49 699
3f07af49
GL
700/**
701 * of_modalias_node - Lookup appropriate modalias for a device node
702 * @node: pointer to a device tree node
703 * @modalias: Pointer to buffer that modalias value will be copied into
704 * @len: Length of modalias value
705 *
2ffe8c5f
GL
706 * Based on the value of the compatible property, this routine will attempt
707 * to choose an appropriate modalias value for a particular device tree node.
708 * It does this by stripping the manufacturer prefix (as delimited by a ',')
709 * from the first entry in the compatible list property.
3f07af49 710 *
2ffe8c5f 711 * This routine returns 0 on success, <0 on failure.
3f07af49
GL
712 */
713int of_modalias_node(struct device_node *node, char *modalias, int len)
714{
2ffe8c5f
GL
715 const char *compatible, *p;
716 int cplen;
3f07af49
GL
717
718 compatible = of_get_property(node, "compatible", &cplen);
2ffe8c5f 719 if (!compatible || strlen(compatible) > cplen)
3f07af49 720 return -ENODEV;
3f07af49 721 p = strchr(compatible, ',');
2ffe8c5f 722 strlcpy(modalias, p ? p + 1 : compatible, len);
3f07af49
GL
723 return 0;
724}
725EXPORT_SYMBOL_GPL(of_modalias_node);
726
89751a7c
JK
727/**
728 * of_find_node_by_phandle - Find a node given a phandle
729 * @handle: phandle of the node to find
730 *
731 * Returns a node pointer with refcount incremented, use
732 * of_node_put() on it when done.
733 */
734struct device_node *of_find_node_by_phandle(phandle handle)
735{
736 struct device_node *np;
737
d6d3c4e6 738 raw_spin_lock(&devtree_lock);
465aac6d 739 for (np = of_allnodes; np; np = np->allnext)
89751a7c
JK
740 if (np->phandle == handle)
741 break;
742 of_node_get(np);
d6d3c4e6 743 raw_spin_unlock(&devtree_lock);
89751a7c
JK
744 return np;
745}
746EXPORT_SYMBOL(of_find_node_by_phandle);
747
3daf3726
TP
748/**
749 * of_property_read_u32_index - Find and read a u32 from a multi-value property.
750 *
751 * @np: device node from which the property value is to be read.
752 * @propname: name of the property to be searched.
753 * @index: index of the u32 in the list of values
754 * @out_value: pointer to return value, modified only if no error.
755 *
756 * Search for a property in a device node and read nth 32-bit value from
757 * it. Returns 0 on success, -EINVAL if the property does not exist,
758 * -ENODATA if property does not have a value, and -EOVERFLOW if the
759 * property data isn't large enough.
760 *
761 * The out_value is modified only if a valid u32 value can be decoded.
762 */
763int of_property_read_u32_index(const struct device_node *np,
764 const char *propname,
765 u32 index, u32 *out_value)
766{
767 struct property *prop = of_find_property(np, propname, NULL);
768
769 if (!prop)
770 return -EINVAL;
771 if (!prop->value)
772 return -ENODATA;
773 if (((index + 1) * sizeof(*out_value)) > prop->length)
774 return -EOVERFLOW;
775
776 *out_value = be32_to_cpup(((__be32 *)prop->value) + index);
777 return 0;
778}
779EXPORT_SYMBOL_GPL(of_property_read_u32_index);
780
be193249
VK
781/**
782 * of_property_read_u8_array - Find and read an array of u8 from a property.
783 *
784 * @np: device node from which the property value is to be read.
785 * @propname: name of the property to be searched.
786 * @out_value: pointer to return value, modified only if return value is 0.
787 * @sz: number of array elements to read
788 *
789 * Search for a property in a device node and read 8-bit value(s) 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.
793 *
794 * dts entry of array should be like:
795 * property = /bits/ 8 <0x50 0x60 0x70>;
796 *
797 * The out_value is modified only if a valid u8 value can be decoded.
798 */
799int of_property_read_u8_array(const struct device_node *np,
800 const char *propname, u8 *out_values, size_t sz)
801{
802 struct property *prop = of_find_property(np, propname, NULL);
803 const u8 *val;
804
805 if (!prop)
806 return -EINVAL;
807 if (!prop->value)
808 return -ENODATA;
809 if ((sz * sizeof(*out_values)) > prop->length)
810 return -EOVERFLOW;
811
812 val = prop->value;
813 while (sz--)
814 *out_values++ = *val++;
815 return 0;
816}
817EXPORT_SYMBOL_GPL(of_property_read_u8_array);
818
819/**
820 * of_property_read_u16_array - Find and read an array of u16 from a property.
821 *
822 * @np: device node from which the property value is to be read.
823 * @propname: name of the property to be searched.
824 * @out_value: pointer to return value, modified only if return value is 0.
825 * @sz: number of array elements to read
826 *
827 * Search for a property in a device node and read 16-bit value(s) from
828 * it. Returns 0 on success, -EINVAL if the property does not exist,
829 * -ENODATA if property does not have a value, and -EOVERFLOW if the
830 * property data isn't large enough.
831 *
832 * dts entry of array should be like:
833 * property = /bits/ 16 <0x5000 0x6000 0x7000>;
834 *
835 * The out_value is modified only if a valid u16 value can be decoded.
836 */
837int of_property_read_u16_array(const struct device_node *np,
838 const char *propname, u16 *out_values, size_t sz)
839{
840 struct property *prop = of_find_property(np, propname, NULL);
841 const __be16 *val;
842
843 if (!prop)
844 return -EINVAL;
845 if (!prop->value)
846 return -ENODATA;
847 if ((sz * sizeof(*out_values)) > prop->length)
848 return -EOVERFLOW;
849
850 val = prop->value;
851 while (sz--)
852 *out_values++ = be16_to_cpup(val++);
853 return 0;
854}
855EXPORT_SYMBOL_GPL(of_property_read_u16_array);
856
a3b85363 857/**
0e373639
RH
858 * of_property_read_u32_array - Find and read an array of 32 bit integers
859 * from a property.
860 *
a3b85363
TA
861 * @np: device node from which the property value is to be read.
862 * @propname: name of the property to be searched.
863 * @out_value: pointer to return value, modified only if return value is 0.
be193249 864 * @sz: number of array elements to read
a3b85363 865 *
0e373639 866 * Search for a property in a device node and read 32-bit value(s) from
a3b85363
TA
867 * it. Returns 0 on success, -EINVAL if the property does not exist,
868 * -ENODATA if property does not have a value, and -EOVERFLOW if the
869 * property data isn't large enough.
870 *
871 * The out_value is modified only if a valid u32 value can be decoded.
872 */
aac285c6
JI
873int of_property_read_u32_array(const struct device_node *np,
874 const char *propname, u32 *out_values,
875 size_t sz)
a3b85363
TA
876{
877 struct property *prop = of_find_property(np, propname, NULL);
0e373639 878 const __be32 *val;
a3b85363
TA
879
880 if (!prop)
881 return -EINVAL;
882 if (!prop->value)
883 return -ENODATA;
0e373639 884 if ((sz * sizeof(*out_values)) > prop->length)
a3b85363 885 return -EOVERFLOW;
0e373639
RH
886
887 val = prop->value;
888 while (sz--)
889 *out_values++ = be32_to_cpup(val++);
a3b85363
TA
890 return 0;
891}
0e373639 892EXPORT_SYMBOL_GPL(of_property_read_u32_array);
a3b85363 893
4cd7f7a3
JI
894/**
895 * of_property_read_u64 - Find and read a 64 bit integer from a property
896 * @np: device node from which the property value is to be read.
897 * @propname: name of the property to be searched.
898 * @out_value: pointer to return value, modified only if return value is 0.
899 *
900 * Search for a property in a device node and read a 64-bit value from
901 * it. Returns 0 on success, -EINVAL if the property does not exist,
902 * -ENODATA if property does not have a value, and -EOVERFLOW if the
903 * property data isn't large enough.
904 *
905 * The out_value is modified only if a valid u64 value can be decoded.
906 */
907int of_property_read_u64(const struct device_node *np, const char *propname,
908 u64 *out_value)
909{
910 struct property *prop = of_find_property(np, propname, NULL);
911
912 if (!prop)
913 return -EINVAL;
914 if (!prop->value)
915 return -ENODATA;
916 if (sizeof(*out_value) > prop->length)
917 return -EOVERFLOW;
918 *out_value = of_read_number(prop->value, 2);
919 return 0;
920}
921EXPORT_SYMBOL_GPL(of_property_read_u64);
922
a3b85363
TA
923/**
924 * of_property_read_string - Find and read a string from a property
925 * @np: device node from which the property value is to be read.
926 * @propname: name of the property to be searched.
927 * @out_string: pointer to null terminated return string, modified only if
928 * return value is 0.
929 *
930 * Search for a property in a device tree node and retrieve a null
931 * terminated string value (pointer to data, not a copy). Returns 0 on
932 * success, -EINVAL if the property does not exist, -ENODATA if property
933 * does not have a value, and -EILSEQ if the string is not null-terminated
934 * within the length of the property data.
935 *
936 * The out_string pointer is modified only if a valid string can be decoded.
937 */
aac285c6 938int of_property_read_string(struct device_node *np, const char *propname,
f09bc831 939 const char **out_string)
a3b85363
TA
940{
941 struct property *prop = of_find_property(np, propname, NULL);
942 if (!prop)
943 return -EINVAL;
944 if (!prop->value)
945 return -ENODATA;
946 if (strnlen(prop->value, prop->length) >= prop->length)
947 return -EILSEQ;
948 *out_string = prop->value;
949 return 0;
950}
951EXPORT_SYMBOL_GPL(of_property_read_string);
952
4fcd15a0
BC
953/**
954 * of_property_read_string_index - Find and read a string from a multiple
955 * strings property.
956 * @np: device node from which the property value is to be read.
957 * @propname: name of the property to be searched.
958 * @index: index of the string in the list of strings
959 * @out_string: pointer to null terminated return string, modified only if
960 * return value is 0.
961 *
962 * Search for a property in a device tree node and retrieve a null
963 * terminated string value (pointer to data, not a copy) in the list of strings
964 * contained in that property.
965 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
966 * property does not have a value, and -EILSEQ if the string is not
967 * null-terminated within the length of the property data.
968 *
969 * The out_string pointer is modified only if a valid string can be decoded.
970 */
971int of_property_read_string_index(struct device_node *np, const char *propname,
972 int index, const char **output)
973{
974 struct property *prop = of_find_property(np, propname, NULL);
975 int i = 0;
976 size_t l = 0, total = 0;
977 const char *p;
978
979 if (!prop)
980 return -EINVAL;
981 if (!prop->value)
982 return -ENODATA;
983 if (strnlen(prop->value, prop->length) >= prop->length)
984 return -EILSEQ;
985
986 p = prop->value;
987
988 for (i = 0; total < prop->length; total += l, p += l) {
989 l = strlen(p) + 1;
88af7f58 990 if (i++ == index) {
4fcd15a0
BC
991 *output = p;
992 return 0;
993 }
994 }
995 return -ENODATA;
996}
997EXPORT_SYMBOL_GPL(of_property_read_string_index);
998
7aff0fe3
GL
999/**
1000 * of_property_match_string() - Find string in a list and return index
1001 * @np: pointer to node containing string list property
1002 * @propname: string list property name
1003 * @string: pointer to string to search for in string list
1004 *
1005 * This function searches a string list property and returns the index
1006 * of a specific string value.
1007 */
1008int of_property_match_string(struct device_node *np, const char *propname,
1009 const char *string)
1010{
1011 struct property *prop = of_find_property(np, propname, NULL);
1012 size_t l;
1013 int i;
1014 const char *p, *end;
1015
1016 if (!prop)
1017 return -EINVAL;
1018 if (!prop->value)
1019 return -ENODATA;
1020
1021 p = prop->value;
1022 end = p + prop->length;
1023
1024 for (i = 0; p < end; i++, p += l) {
1025 l = strlen(p) + 1;
1026 if (p + l > end)
1027 return -EILSEQ;
1028 pr_debug("comparing %s with %s\n", string, p);
1029 if (strcmp(string, p) == 0)
1030 return i; /* Found it; return index */
1031 }
1032 return -ENODATA;
1033}
1034EXPORT_SYMBOL_GPL(of_property_match_string);
4fcd15a0
BC
1035
1036/**
1037 * of_property_count_strings - Find and return the number of strings from a
1038 * multiple strings property.
1039 * @np: device node from which the property value is to be read.
1040 * @propname: name of the property to be searched.
1041 *
1042 * Search for a property in a device tree node and retrieve the number of null
1043 * terminated string contain in it. Returns the number of strings on
1044 * success, -EINVAL if the property does not exist, -ENODATA if property
1045 * does not have a value, and -EILSEQ if the string is not null-terminated
1046 * within the length of the property data.
1047 */
1048int of_property_count_strings(struct device_node *np, const char *propname)
1049{
1050 struct property *prop = of_find_property(np, propname, NULL);
1051 int i = 0;
1052 size_t l = 0, total = 0;
1053 const char *p;
1054
1055 if (!prop)
1056 return -EINVAL;
1057 if (!prop->value)
1058 return -ENODATA;
1059 if (strnlen(prop->value, prop->length) >= prop->length)
1060 return -EILSEQ;
1061
1062 p = prop->value;
1063
88af7f58 1064 for (i = 0; total < prop->length; total += l, p += l, i++)
4fcd15a0 1065 l = strlen(p) + 1;
88af7f58 1066
4fcd15a0
BC
1067 return i;
1068}
1069EXPORT_SYMBOL_GPL(of_property_count_strings);
1070
739649c5
GL
1071/**
1072 * of_parse_phandle - Resolve a phandle property to a device_node pointer
1073 * @np: Pointer to device node holding phandle property
1074 * @phandle_name: Name of property holding a phandle value
1075 * @index: For properties holding a table of phandles, this is the index into
1076 * the table
1077 *
1078 * Returns the device_node pointer with refcount incremented. Use
1079 * of_node_put() on it when done.
1080 */
b8fbdc42
ST
1081struct device_node *of_parse_phandle(const struct device_node *np,
1082 const char *phandle_name, int index)
739649c5 1083{
9a6b2e58 1084 const __be32 *phandle;
739649c5
GL
1085 int size;
1086
1087 phandle = of_get_property(np, phandle_name, &size);
1088 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
1089 return NULL;
1090
9a6b2e58 1091 return of_find_node_by_phandle(be32_to_cpup(phandle + index));
739649c5
GL
1092}
1093EXPORT_SYMBOL(of_parse_phandle);
1094
64b60e09 1095/**
15c9a0ac 1096 * of_parse_phandle_with_args() - Find a node pointed by phandle in a list
64b60e09
AV
1097 * @np: pointer to a device tree node containing a list
1098 * @list_name: property name that contains a list
1099 * @cells_name: property name that specifies phandles' arguments count
1100 * @index: index of a phandle to parse out
15c9a0ac 1101 * @out_args: optional pointer to output arguments structure (will be filled)
64b60e09
AV
1102 *
1103 * This function is useful to parse lists of phandles and their arguments.
15c9a0ac
GL
1104 * Returns 0 on success and fills out_args, on error returns appropriate
1105 * errno value.
1106 *
1107 * Caller is responsible to call of_node_put() on the returned out_args->node
1108 * pointer.
64b60e09
AV
1109 *
1110 * Example:
1111 *
1112 * phandle1: node1 {
1113 * #list-cells = <2>;
1114 * }
1115 *
1116 * phandle2: node2 {
1117 * #list-cells = <1>;
1118 * }
1119 *
1120 * node3 {
1121 * list = <&phandle1 1 2 &phandle2 3>;
1122 * }
1123 *
1124 * To get a device_node of the `node2' node you may call this:
15c9a0ac 1125 * of_parse_phandle_with_args(node3, "list", "#list-cells", 1, &args);
64b60e09 1126 */
bd69f73f
GL
1127static int __of_parse_phandle_with_args(const struct device_node *np,
1128 const char *list_name,
1129 const char *cells_name, int index,
1130 struct of_phandle_args *out_args)
64b60e09 1131{
15c9a0ac 1132 const __be32 *list, *list_end;
23ce04c0 1133 int rc = 0, size, cur_index = 0;
15c9a0ac 1134 uint32_t count = 0;
64b60e09 1135 struct device_node *node = NULL;
15c9a0ac 1136 phandle phandle;
64b60e09 1137
15c9a0ac 1138 /* Retrieve the phandle list property */
64b60e09 1139 list = of_get_property(np, list_name, &size);
15c9a0ac 1140 if (!list)
1af4c7f1 1141 return -ENOENT;
64b60e09
AV
1142 list_end = list + size / sizeof(*list);
1143
15c9a0ac 1144 /* Loop over the phandles until all the requested entry is found */
64b60e09 1145 while (list < list_end) {
23ce04c0 1146 rc = -EINVAL;
15c9a0ac 1147 count = 0;
64b60e09 1148
15c9a0ac
GL
1149 /*
1150 * If phandle is 0, then it is an empty entry with no
1151 * arguments. Skip forward to the next entry.
1152 */
9a6b2e58 1153 phandle = be32_to_cpup(list++);
15c9a0ac
GL
1154 if (phandle) {
1155 /*
1156 * Find the provider node and parse the #*-cells
1157 * property to determine the argument length
1158 */
1159 node = of_find_node_by_phandle(phandle);
1160 if (!node) {
1161 pr_err("%s: could not find phandle\n",
1162 np->full_name);
23ce04c0 1163 goto err;
15c9a0ac
GL
1164 }
1165 if (of_property_read_u32(node, cells_name, &count)) {
1166 pr_err("%s: could not get %s for %s\n",
1167 np->full_name, cells_name,
1168 node->full_name);
23ce04c0 1169 goto err;
15c9a0ac 1170 }
64b60e09 1171
15c9a0ac
GL
1172 /*
1173 * Make sure that the arguments actually fit in the
1174 * remaining property data length
1175 */
1176 if (list + count > list_end) {
1177 pr_err("%s: arguments longer than property\n",
1178 np->full_name);
23ce04c0 1179 goto err;
15c9a0ac 1180 }
64b60e09
AV
1181 }
1182
15c9a0ac
GL
1183 /*
1184 * All of the error cases above bail out of the loop, so at
1185 * this point, the parsing is successful. If the requested
1186 * index matches, then fill the out_args structure and return,
1187 * or return -ENOENT for an empty entry.
1188 */
23ce04c0 1189 rc = -ENOENT;
15c9a0ac
GL
1190 if (cur_index == index) {
1191 if (!phandle)
23ce04c0 1192 goto err;
15c9a0ac
GL
1193
1194 if (out_args) {
1195 int i;
1196 if (WARN_ON(count > MAX_PHANDLE_ARGS))
1197 count = MAX_PHANDLE_ARGS;
1198 out_args->np = node;
1199 out_args->args_count = count;
1200 for (i = 0; i < count; i++)
1201 out_args->args[i] = be32_to_cpup(list++);
1202 }
23ce04c0
GL
1203
1204 /* Found it! return success */
1205 if (node)
1206 of_node_put(node);
15c9a0ac 1207 return 0;
64b60e09 1208 }
64b60e09
AV
1209
1210 of_node_put(node);
1211 node = NULL;
15c9a0ac 1212 list += count;
64b60e09
AV
1213 cur_index++;
1214 }
1215
23ce04c0
GL
1216 /*
1217 * Unlock node before returning result; will be one of:
1218 * -ENOENT : index is for empty phandle
1219 * -EINVAL : parsing error on data
bd69f73f 1220 * [1..n] : Number of phandle (count mode; when index = -1)
23ce04c0 1221 */
bd69f73f 1222 rc = index < 0 ? cur_index : -ENOENT;
23ce04c0 1223 err:
15c9a0ac
GL
1224 if (node)
1225 of_node_put(node);
23ce04c0 1226 return rc;
64b60e09 1227}
bd69f73f
GL
1228
1229int of_parse_phandle_with_args(const struct device_node *np, const char *list_name,
1230 const char *cells_name, int index,
1231 struct of_phandle_args *out_args)
1232{
1233 if (index < 0)
1234 return -EINVAL;
1235 return __of_parse_phandle_with_args(np, list_name, cells_name, index, out_args);
1236}
15c9a0ac 1237EXPORT_SYMBOL(of_parse_phandle_with_args);
02af11b0 1238
bd69f73f
GL
1239/**
1240 * of_count_phandle_with_args() - Find the number of phandles references in a property
1241 * @np: pointer to a device tree node containing a list
1242 * @list_name: property name that contains a list
1243 * @cells_name: property name that specifies phandles' arguments count
1244 *
1245 * Returns the number of phandle + argument tuples within a property. It
1246 * is a typical pattern to encode a list of phandle and variable
1247 * arguments into a single property. The number of arguments is encoded
1248 * by a property in the phandle-target node. For example, a gpios
1249 * property would contain a list of GPIO specifies consisting of a
1250 * phandle and 1 or more arguments. The number of arguments are
1251 * determined by the #gpio-cells property in the node pointed to by the
1252 * phandle.
1253 */
1254int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
1255 const char *cells_name)
1256{
1257 return __of_parse_phandle_with_args(np, list_name, cells_name, -1, NULL);
1258}
1259EXPORT_SYMBOL(of_count_phandle_with_args);
1260
1cf3d8b3
NF
1261#if defined(CONFIG_OF_DYNAMIC)
1262static int of_property_notify(int action, struct device_node *np,
1263 struct property *prop)
1264{
1265 struct of_prop_reconfig pr;
1266
1267 pr.dn = np;
1268 pr.prop = prop;
1269 return of_reconfig_notify(action, &pr);
1270}
1271#else
1272static int of_property_notify(int action, struct device_node *np,
1273 struct property *prop)
1274{
1275 return 0;
1276}
1277#endif
1278
02af11b0 1279/**
79d1c712 1280 * of_add_property - Add a property to a node
02af11b0 1281 */
79d1c712 1282int of_add_property(struct device_node *np, struct property *prop)
02af11b0
GL
1283{
1284 struct property **next;
1285 unsigned long flags;
1cf3d8b3
NF
1286 int rc;
1287
1288 rc = of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop);
1289 if (rc)
1290 return rc;
02af11b0
GL
1291
1292 prop->next = NULL;
d6d3c4e6 1293 raw_spin_lock_irqsave(&devtree_lock, flags);
02af11b0
GL
1294 next = &np->properties;
1295 while (*next) {
1296 if (strcmp(prop->name, (*next)->name) == 0) {
1297 /* duplicate ! don't insert it */
d6d3c4e6 1298 raw_spin_unlock_irqrestore(&devtree_lock, flags);
02af11b0
GL
1299 return -1;
1300 }
1301 next = &(*next)->next;
1302 }
1303 *next = prop;
d6d3c4e6 1304 raw_spin_unlock_irqrestore(&devtree_lock, flags);
02af11b0
GL
1305
1306#ifdef CONFIG_PROC_DEVICETREE
1307 /* try to add to proc as well if it was initialized */
1308 if (np->pde)
1309 proc_device_tree_add_prop(np->pde, prop);
1310#endif /* CONFIG_PROC_DEVICETREE */
1311
1312 return 0;
1313}
1314
1315/**
79d1c712 1316 * of_remove_property - Remove a property from a node.
02af11b0
GL
1317 *
1318 * Note that we don't actually remove it, since we have given out
1319 * who-knows-how-many pointers to the data using get-property.
1320 * Instead we just move the property to the "dead properties"
1321 * list, so it won't be found any more.
1322 */
79d1c712 1323int of_remove_property(struct device_node *np, struct property *prop)
02af11b0
GL
1324{
1325 struct property **next;
1326 unsigned long flags;
1327 int found = 0;
1cf3d8b3
NF
1328 int rc;
1329
1330 rc = of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop);
1331 if (rc)
1332 return rc;
02af11b0 1333
d6d3c4e6 1334 raw_spin_lock_irqsave(&devtree_lock, flags);
02af11b0
GL
1335 next = &np->properties;
1336 while (*next) {
1337 if (*next == prop) {
1338 /* found the node */
1339 *next = prop->next;
1340 prop->next = np->deadprops;
1341 np->deadprops = prop;
1342 found = 1;
1343 break;
1344 }
1345 next = &(*next)->next;
1346 }
d6d3c4e6 1347 raw_spin_unlock_irqrestore(&devtree_lock, flags);
02af11b0
GL
1348
1349 if (!found)
1350 return -ENODEV;
1351
1352#ifdef CONFIG_PROC_DEVICETREE
1353 /* try to remove the proc node as well */
1354 if (np->pde)
1355 proc_device_tree_remove_prop(np->pde, prop);
1356#endif /* CONFIG_PROC_DEVICETREE */
1357
1358 return 0;
1359}
1360
1361/*
79d1c712 1362 * of_update_property - Update a property in a node, if the property does
475d0094 1363 * not exist, add it.
02af11b0
GL
1364 *
1365 * Note that we don't actually remove it, since we have given out
1366 * who-knows-how-many pointers to the data using get-property.
1367 * Instead we just move the property to the "dead properties" list,
1368 * and add the new property to the property list
1369 */
79d1c712 1370int of_update_property(struct device_node *np, struct property *newprop)
02af11b0 1371{
475d0094 1372 struct property **next, *oldprop;
02af11b0 1373 unsigned long flags;
1cf3d8b3
NF
1374 int rc, found = 0;
1375
1376 rc = of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop);
1377 if (rc)
1378 return rc;
02af11b0 1379
475d0094
DA
1380 if (!newprop->name)
1381 return -EINVAL;
1382
1383 oldprop = of_find_property(np, newprop->name, NULL);
1384 if (!oldprop)
79d1c712 1385 return of_add_property(np, newprop);
475d0094 1386
d6d3c4e6 1387 raw_spin_lock_irqsave(&devtree_lock, flags);
02af11b0
GL
1388 next = &np->properties;
1389 while (*next) {
1390 if (*next == oldprop) {
1391 /* found the node */
1392 newprop->next = oldprop->next;
1393 *next = newprop;
1394 oldprop->next = np->deadprops;
1395 np->deadprops = oldprop;
1396 found = 1;
1397 break;
1398 }
1399 next = &(*next)->next;
1400 }
d6d3c4e6 1401 raw_spin_unlock_irqrestore(&devtree_lock, flags);
02af11b0
GL
1402
1403 if (!found)
1404 return -ENODEV;
1405
1406#ifdef CONFIG_PROC_DEVICETREE
1407 /* try to add to proc as well if it was initialized */
1408 if (np->pde)
1409 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1410#endif /* CONFIG_PROC_DEVICETREE */
1411
1412 return 0;
1413}
fcdeb7fe
GL
1414
1415#if defined(CONFIG_OF_DYNAMIC)
1416/*
1417 * Support for dynamic device trees.
1418 *
1419 * On some platforms, the device tree can be manipulated at runtime.
1420 * The routines in this section support adding, removing and changing
1421 * device tree nodes.
1422 */
1423
1cf3d8b3
NF
1424static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
1425
1426int of_reconfig_notifier_register(struct notifier_block *nb)
1427{
1428 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
1429}
1a9bd454 1430EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
1cf3d8b3
NF
1431
1432int of_reconfig_notifier_unregister(struct notifier_block *nb)
1433{
1434 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
1435}
1a9bd454 1436EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
1cf3d8b3
NF
1437
1438int of_reconfig_notify(unsigned long action, void *p)
1439{
1440 int rc;
1441
1442 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
1443 return notifier_to_errno(rc);
1444}
1445
e81b3295
NF
1446#ifdef CONFIG_PROC_DEVICETREE
1447static void of_add_proc_dt_entry(struct device_node *dn)
1448{
1449 struct proc_dir_entry *ent;
1450
1451 ent = proc_mkdir(strrchr(dn->full_name, '/') + 1, dn->parent->pde);
1452 if (ent)
1453 proc_device_tree_add_node(dn, ent);
1454}
1455#else
1456static void of_add_proc_dt_entry(struct device_node *dn)
1457{
1458 return;
1459}
1460#endif
1461
fcdeb7fe
GL
1462/**
1463 * of_attach_node - Plug a device node into the tree and global list.
1464 */
1cf3d8b3 1465int of_attach_node(struct device_node *np)
fcdeb7fe
GL
1466{
1467 unsigned long flags;
1cf3d8b3
NF
1468 int rc;
1469
1470 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
1471 if (rc)
1472 return rc;
fcdeb7fe 1473
d6d3c4e6 1474 raw_spin_lock_irqsave(&devtree_lock, flags);
fcdeb7fe 1475 np->sibling = np->parent->child;
465aac6d 1476 np->allnext = of_allnodes;
fcdeb7fe 1477 np->parent->child = np;
465aac6d 1478 of_allnodes = np;
d6d3c4e6 1479 raw_spin_unlock_irqrestore(&devtree_lock, flags);
e81b3295
NF
1480
1481 of_add_proc_dt_entry(np);
1cf3d8b3 1482 return 0;
fcdeb7fe
GL
1483}
1484
e81b3295
NF
1485#ifdef CONFIG_PROC_DEVICETREE
1486static void of_remove_proc_dt_entry(struct device_node *dn)
1487{
1488 struct device_node *parent = dn->parent;
1489 struct property *prop = dn->properties;
1490
1491 while (prop) {
1492 remove_proc_entry(prop->name, dn->pde);
1493 prop = prop->next;
1494 }
1495
1496 if (dn->pde)
1497 remove_proc_entry(dn->pde->name, parent->pde);
1498}
1499#else
1500static void of_remove_proc_dt_entry(struct device_node *dn)
1501{
1502 return;
1503}
1504#endif
1505
fcdeb7fe
GL
1506/**
1507 * of_detach_node - "Unplug" a node from the device tree.
1508 *
1509 * The caller must hold a reference to the node. The memory associated with
1510 * the node is not freed until its refcount goes to zero.
1511 */
1cf3d8b3 1512int of_detach_node(struct device_node *np)
fcdeb7fe
GL
1513{
1514 struct device_node *parent;
1515 unsigned long flags;
1cf3d8b3
NF
1516 int rc = 0;
1517
1518 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
1519 if (rc)
1520 return rc;
fcdeb7fe 1521
d6d3c4e6 1522 raw_spin_lock_irqsave(&devtree_lock, flags);
fcdeb7fe 1523
e81b3295
NF
1524 if (of_node_check_flag(np, OF_DETACHED)) {
1525 /* someone already detached it */
d6d3c4e6 1526 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1cf3d8b3 1527 return rc;
e81b3295
NF
1528 }
1529
fcdeb7fe 1530 parent = np->parent;
e81b3295 1531 if (!parent) {
d6d3c4e6 1532 raw_spin_unlock_irqrestore(&devtree_lock, flags);
1cf3d8b3 1533 return rc;
e81b3295 1534 }
fcdeb7fe 1535
465aac6d
RD
1536 if (of_allnodes == np)
1537 of_allnodes = np->allnext;
fcdeb7fe
GL
1538 else {
1539 struct device_node *prev;
465aac6d 1540 for (prev = of_allnodes;
fcdeb7fe
GL
1541 prev->allnext != np;
1542 prev = prev->allnext)
1543 ;
1544 prev->allnext = np->allnext;
1545 }
1546
1547 if (parent->child == np)
1548 parent->child = np->sibling;
1549 else {
1550 struct device_node *prevsib;
1551 for (prevsib = np->parent->child;
1552 prevsib->sibling != np;
1553 prevsib = prevsib->sibling)
1554 ;
1555 prevsib->sibling = np->sibling;
1556 }
1557
1558 of_node_set_flag(np, OF_DETACHED);
d6d3c4e6 1559 raw_spin_unlock_irqrestore(&devtree_lock, flags);
e81b3295
NF
1560
1561 of_remove_proc_dt_entry(np);
1cf3d8b3 1562 return rc;
fcdeb7fe
GL
1563}
1564#endif /* defined(CONFIG_OF_DYNAMIC) */
1565
611cad72
SG
1566static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1567 int id, const char *stem, int stem_len)
1568{
1569 ap->np = np;
1570 ap->id = id;
1571 strncpy(ap->stem, stem, stem_len);
1572 ap->stem[stem_len] = 0;
1573 list_add_tail(&ap->link, &aliases_lookup);
1574 pr_debug("adding DT alias:%s: stem=%s id=%i node=%s\n",
74a7f084 1575 ap->alias, ap->stem, ap->id, of_node_full_name(np));
611cad72
SG
1576}
1577
1578/**
1579 * of_alias_scan - Scan all properties of 'aliases' node
1580 *
1581 * The function scans all the properties of 'aliases' node and populate
1582 * the the global lookup table with the properties. It returns the
1583 * number of alias_prop found, or error code in error case.
1584 *
1585 * @dt_alloc: An allocator that provides a virtual address to memory
1586 * for the resulting tree
1587 */
1588void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1589{
1590 struct property *pp;
1591
1592 of_chosen = of_find_node_by_path("/chosen");
1593 if (of_chosen == NULL)
1594 of_chosen = of_find_node_by_path("/chosen@0");
1595 of_aliases = of_find_node_by_path("/aliases");
1596 if (!of_aliases)
1597 return;
1598
8af0da93 1599 for_each_property_of_node(of_aliases, pp) {
611cad72
SG
1600 const char *start = pp->name;
1601 const char *end = start + strlen(start);
1602 struct device_node *np;
1603 struct alias_prop *ap;
1604 int id, len;
1605
1606 /* Skip those we do not want to proceed */
1607 if (!strcmp(pp->name, "name") ||
1608 !strcmp(pp->name, "phandle") ||
1609 !strcmp(pp->name, "linux,phandle"))
1610 continue;
1611
1612 np = of_find_node_by_path(pp->value);
1613 if (!np)
1614 continue;
1615
1616 /* walk the alias backwards to extract the id and work out
1617 * the 'stem' string */
1618 while (isdigit(*(end-1)) && end > start)
1619 end--;
1620 len = end - start;
1621
1622 if (kstrtoint(end, 10, &id) < 0)
1623 continue;
1624
1625 /* Allocate an alias_prop with enough space for the stem */
1626 ap = dt_alloc(sizeof(*ap) + len + 1, 4);
1627 if (!ap)
1628 continue;
1629 ap->alias = start;
1630 of_alias_add(ap, np, id, start, len);
1631 }
1632}
1633
1634/**
1635 * of_alias_get_id - Get alias id for the given device_node
1636 * @np: Pointer to the given device_node
1637 * @stem: Alias stem of the given device_node
1638 *
1639 * The function travels the lookup table to get alias id for the given
1640 * device_node and alias stem. It returns the alias id if find it.
1641 */
1642int of_alias_get_id(struct device_node *np, const char *stem)
1643{
1644 struct alias_prop *app;
1645 int id = -ENODEV;
1646
1647 mutex_lock(&of_aliases_mutex);
1648 list_for_each_entry(app, &aliases_lookup, link) {
1649 if (strcmp(app->stem, stem) != 0)
1650 continue;
1651
1652 if (np == app->np) {
1653 id = app->id;
1654 break;
1655 }
1656 }
1657 mutex_unlock(&of_aliases_mutex);
1658
1659 return id;
1660}
1661EXPORT_SYMBOL_GPL(of_alias_get_id);
c541adc6
SW
1662
1663const __be32 *of_prop_next_u32(struct property *prop, const __be32 *cur,
1664 u32 *pu)
1665{
1666 const void *curv = cur;
1667
1668 if (!prop)
1669 return NULL;
1670
1671 if (!cur) {
1672 curv = prop->value;
1673 goto out_val;
1674 }
1675
1676 curv += sizeof(*cur);
1677 if (curv >= prop->value + prop->length)
1678 return NULL;
1679
1680out_val:
1681 *pu = be32_to_cpup(curv);
1682 return curv;
1683}
1684EXPORT_SYMBOL_GPL(of_prop_next_u32);
1685
1686const char *of_prop_next_string(struct property *prop, const char *cur)
1687{
1688 const void *curv = cur;
1689
1690 if (!prop)
1691 return NULL;
1692
1693 if (!cur)
1694 return prop->value;
1695
1696 curv += strlen(cur) + 1;
1697 if (curv >= prop->value + prop->length)
1698 return NULL;
1699
1700 return curv;
1701}
1702EXPORT_SYMBOL_GPL(of_prop_next_string);
This page took 0.502231 seconds and 5 git commands to generate.