blob: 710b53bfac6d9478b86aed67e537bc06ee1cbc6d [file] [log] [blame]
Stephen Rothwell97e873e2007-05-01 16:26:07 +10001/*
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 *
Grant Likelye91edcf2009-10-15 10:58:09 -060012 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
13 * Grant Likely.
Stephen Rothwell97e873e2007-05-01 16:26:07 +100014 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
19 */
20#include <linux/module.h>
21#include <linux/of.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100022#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Jeremy Kerra9f2f632010-02-01 21:34:14 -070024#include <linux/proc_fs.h>
Stephen Rothwell581b6052007-04-24 16:46:53 +100025
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100026struct device_node *allnodes;
Grant Likelyfc0bdae2010-02-14 07:13:55 -070027struct device_node *of_chosen;
Stephen Rothwell1ef4d422007-04-24 17:57:33 +100028
Stephen Rothwell581b6052007-04-24 16:46:53 +100029/* use when traversing tree through the allnext, child, sibling,
30 * or parent members of struct device_node.
31 */
32DEFINE_RWLOCK(devtree_lock);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100033
34int of_n_addr_cells(struct device_node *np)
35{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060036 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100037
38 do {
39 if (np->parent)
40 np = np->parent;
41 ip = of_get_property(np, "#address-cells", NULL);
42 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070043 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100044 } while (np->parent);
45 /* No #address-cells property for the root node */
46 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
47}
48EXPORT_SYMBOL(of_n_addr_cells);
49
50int of_n_size_cells(struct device_node *np)
51{
Jeremy Kerra9fadee2010-10-10 21:24:10 -060052 const __be32 *ip;
Stephen Rothwell97e873e2007-05-01 16:26:07 +100053
54 do {
55 if (np->parent)
56 np = np->parent;
57 ip = of_get_property(np, "#size-cells", NULL);
58 if (ip)
Jeremy Kerr33714882010-01-30 01:45:26 -070059 return be32_to_cpup(ip);
Stephen Rothwell97e873e2007-05-01 16:26:07 +100060 } while (np->parent);
61 /* No #size-cells property for the root node */
62 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
63}
64EXPORT_SYMBOL(of_n_size_cells);
65
Grant Likely923f7e32010-01-28 13:52:53 -070066#if !defined(CONFIG_SPARC) /* SPARC doesn't do ref counting (yet) */
67/**
68 * of_node_get - Increment refcount of a node
69 * @node: Node to inc refcount, NULL is supported to
70 * simplify writing of callers
71 *
72 * Returns node.
73 */
74struct device_node *of_node_get(struct device_node *node)
75{
76 if (node)
77 kref_get(&node->kref);
78 return node;
79}
80EXPORT_SYMBOL(of_node_get);
81
82static inline struct device_node *kref_to_device_node(struct kref *kref)
83{
84 return container_of(kref, struct device_node, kref);
85}
86
87/**
88 * of_node_release - release a dynamically allocated node
89 * @kref: kref element of the node to be released
90 *
91 * In of_node_put() this function is passed to kref_put()
92 * as the destructor.
93 */
94static void of_node_release(struct kref *kref)
95{
96 struct device_node *node = kref_to_device_node(kref);
97 struct property *prop = node->properties;
98
99 /* We should never be releasing nodes that haven't been detached. */
100 if (!of_node_check_flag(node, OF_DETACHED)) {
101 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
102 dump_stack();
103 kref_init(&node->kref);
104 return;
105 }
106
107 if (!of_node_check_flag(node, OF_DYNAMIC))
108 return;
109
110 while (prop) {
111 struct property *next = prop->next;
112 kfree(prop->name);
113 kfree(prop->value);
114 kfree(prop);
115 prop = next;
116
117 if (!prop) {
118 prop = node->deadprops;
119 node->deadprops = NULL;
120 }
121 }
122 kfree(node->full_name);
123 kfree(node->data);
124 kfree(node);
125}
126
127/**
128 * of_node_put - Decrement refcount of a node
129 * @node: Node to dec refcount, NULL is supported to
130 * simplify writing of callers
131 *
132 */
133void of_node_put(struct device_node *node)
134{
135 if (node)
136 kref_put(&node->kref, of_node_release);
137}
138EXPORT_SYMBOL(of_node_put);
139#endif /* !CONFIG_SPARC */
140
Stephen Rothwell581b6052007-04-24 16:46:53 +1000141struct property *of_find_property(const struct device_node *np,
142 const char *name,
143 int *lenp)
144{
145 struct property *pp;
146
Timur Tabi64e45662008-05-08 05:19:59 +1000147 if (!np)
148 return NULL;
149
Stephen Rothwell581b6052007-04-24 16:46:53 +1000150 read_lock(&devtree_lock);
151 for (pp = np->properties; pp != 0; pp = pp->next) {
152 if (of_prop_cmp(pp->name, name) == 0) {
153 if (lenp != 0)
154 *lenp = pp->length;
155 break;
156 }
157 }
158 read_unlock(&devtree_lock);
159
160 return pp;
161}
162EXPORT_SYMBOL(of_find_property);
163
Grant Likelye91edcf2009-10-15 10:58:09 -0600164/**
165 * of_find_all_nodes - Get next node in global list
166 * @prev: Previous node or NULL to start iteration
167 * of_node_put() will be called on it
168 *
169 * Returns a node pointer with refcount incremented, use
170 * of_node_put() on it when done.
171 */
172struct device_node *of_find_all_nodes(struct device_node *prev)
173{
174 struct device_node *np;
175
176 read_lock(&devtree_lock);
177 np = prev ? prev->allnext : allnodes;
178 for (; np != NULL; np = np->allnext)
179 if (of_node_get(np))
180 break;
181 of_node_put(prev);
182 read_unlock(&devtree_lock);
183 return np;
184}
185EXPORT_SYMBOL(of_find_all_nodes);
186
Stephen Rothwell97e873e2007-05-01 16:26:07 +1000187/*
188 * Find a property with a given name for a given node
189 * and return the value.
190 */
191const void *of_get_property(const struct device_node *np, const char *name,
192 int *lenp)
193{
194 struct property *pp = of_find_property(np, name, lenp);
195
196 return pp ? pp->value : NULL;
197}
198EXPORT_SYMBOL(of_get_property);
Stephen Rothwell0081cbc2007-05-01 16:29:19 +1000199
200/** Checks if the given "compat" string matches one of the strings in
201 * the device's "compatible" property
202 */
203int of_device_is_compatible(const struct device_node *device,
204 const char *compat)
205{
206 const char* cp;
207 int cplen, l;
208
209 cp = of_get_property(device, "compatible", &cplen);
210 if (cp == NULL)
211 return 0;
212 while (cplen > 0) {
213 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
214 return 1;
215 l = strlen(cp) + 1;
216 cp += l;
217 cplen -= l;
218 }
219
220 return 0;
221}
222EXPORT_SYMBOL(of_device_is_compatible);
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000223
224/**
Grant Likely71a157e2010-02-01 21:34:14 -0700225 * of_machine_is_compatible - Test root of device tree for a given compatible value
Grant Likely1f43cfb2010-01-28 13:47:25 -0700226 * @compat: compatible string to look for in root node's compatible property.
227 *
228 * Returns true if the root node has the given value in its
229 * compatible property.
230 */
Grant Likely71a157e2010-02-01 21:34:14 -0700231int of_machine_is_compatible(const char *compat)
Grant Likely1f43cfb2010-01-28 13:47:25 -0700232{
233 struct device_node *root;
234 int rc = 0;
235
236 root = of_find_node_by_path("/");
237 if (root) {
238 rc = of_device_is_compatible(root, compat);
239 of_node_put(root);
240 }
241 return rc;
242}
Grant Likely71a157e2010-02-01 21:34:14 -0700243EXPORT_SYMBOL(of_machine_is_compatible);
Grant Likely1f43cfb2010-01-28 13:47:25 -0700244
245/**
Josh Boyer834d97d2008-03-27 00:33:14 +1100246 * of_device_is_available - check if a device is available for use
247 *
248 * @device: Node to check for availability
249 *
250 * Returns 1 if the status property is absent or set to "okay" or "ok",
251 * 0 otherwise
252 */
253int of_device_is_available(const struct device_node *device)
254{
255 const char *status;
256 int statlen;
257
258 status = of_get_property(device, "status", &statlen);
259 if (status == NULL)
260 return 1;
261
262 if (statlen > 0) {
263 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
264 return 1;
265 }
266
267 return 0;
268}
269EXPORT_SYMBOL(of_device_is_available);
270
271/**
Stephen Rothwelle679c5f2007-04-24 17:16:16 +1000272 * of_get_parent - Get a node's parent if any
273 * @node: Node to get parent
274 *
275 * Returns a node pointer with refcount incremented, use
276 * of_node_put() on it when done.
277 */
278struct device_node *of_get_parent(const struct device_node *node)
279{
280 struct device_node *np;
281
282 if (!node)
283 return NULL;
284
285 read_lock(&devtree_lock);
286 np = of_node_get(node->parent);
287 read_unlock(&devtree_lock);
288 return np;
289}
290EXPORT_SYMBOL(of_get_parent);
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000291
292/**
Michael Ellermanf4eb0102007-10-26 16:54:31 +1000293 * of_get_next_parent - Iterate to a node's parent
294 * @node: Node to get parent of
295 *
296 * This is like of_get_parent() except that it drops the
297 * refcount on the passed node, making it suitable for iterating
298 * through a node's parents.
299 *
300 * Returns a node pointer with refcount incremented, use
301 * of_node_put() on it when done.
302 */
303struct device_node *of_get_next_parent(struct device_node *node)
304{
305 struct device_node *parent;
306
307 if (!node)
308 return NULL;
309
310 read_lock(&devtree_lock);
311 parent = of_node_get(node->parent);
312 of_node_put(node);
313 read_unlock(&devtree_lock);
314 return parent;
315}
316
317/**
Stephen Rothwelld1cd3552007-04-24 17:21:29 +1000318 * of_get_next_child - Iterate a node childs
319 * @node: parent node
320 * @prev: previous child of the parent node, or NULL to get first
321 *
322 * Returns a node pointer with refcount incremented, use
323 * of_node_put() on it when done.
324 */
325struct device_node *of_get_next_child(const struct device_node *node,
326 struct device_node *prev)
327{
328 struct device_node *next;
329
330 read_lock(&devtree_lock);
331 next = prev ? prev->sibling : node->child;
332 for (; next; next = next->sibling)
333 if (of_node_get(next))
334 break;
335 of_node_put(prev);
336 read_unlock(&devtree_lock);
337 return next;
338}
339EXPORT_SYMBOL(of_get_next_child);
Stephen Rothwell1ef4d422007-04-24 17:57:33 +1000340
341/**
342 * of_find_node_by_path - Find a node matching a full OF path
343 * @path: The full path to match
344 *
345 * Returns a node pointer with refcount incremented, use
346 * of_node_put() on it when done.
347 */
348struct device_node *of_find_node_by_path(const char *path)
349{
350 struct device_node *np = allnodes;
351
352 read_lock(&devtree_lock);
353 for (; np; np = np->allnext) {
354 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
355 && of_node_get(np))
356 break;
357 }
358 read_unlock(&devtree_lock);
359 return np;
360}
361EXPORT_SYMBOL(of_find_node_by_path);
362
363/**
364 * of_find_node_by_name - Find a node by its "name" property
365 * @from: The node to start searching from or NULL, the node
366 * you pass will not be searched, only the next one
367 * will; typically, you pass what the previous call
368 * returned. of_node_put() will be called on it
369 * @name: The name string to match against
370 *
371 * Returns a node pointer with refcount incremented, use
372 * of_node_put() on it when done.
373 */
374struct device_node *of_find_node_by_name(struct device_node *from,
375 const char *name)
376{
377 struct device_node *np;
378
379 read_lock(&devtree_lock);
380 np = from ? from->allnext : allnodes;
381 for (; np; np = np->allnext)
382 if (np->name && (of_node_cmp(np->name, name) == 0)
383 && of_node_get(np))
384 break;
385 of_node_put(from);
386 read_unlock(&devtree_lock);
387 return np;
388}
389EXPORT_SYMBOL(of_find_node_by_name);
390
391/**
392 * of_find_node_by_type - Find a node by its "device_type" property
393 * @from: The node to start searching from, or NULL to start searching
394 * the entire device tree. The node you pass will not be
395 * searched, only the next one will; typically, you pass
396 * what the previous call returned. of_node_put() will be
397 * called on from for you.
398 * @type: The type string to match against
399 *
400 * Returns a node pointer with refcount incremented, use
401 * of_node_put() on it when done.
402 */
403struct device_node *of_find_node_by_type(struct device_node *from,
404 const char *type)
405{
406 struct device_node *np;
407
408 read_lock(&devtree_lock);
409 np = from ? from->allnext : allnodes;
410 for (; np; np = np->allnext)
411 if (np->type && (of_node_cmp(np->type, type) == 0)
412 && of_node_get(np))
413 break;
414 of_node_put(from);
415 read_unlock(&devtree_lock);
416 return np;
417}
418EXPORT_SYMBOL(of_find_node_by_type);
419
420/**
421 * of_find_compatible_node - Find a node based on type and one of the
422 * tokens in its "compatible" property
423 * @from: The node to start searching from or NULL, the node
424 * you pass will not be searched, only the next one
425 * will; typically, you pass what the previous call
426 * returned. of_node_put() will be called on it
427 * @type: The type string to match "device_type" or NULL to ignore
428 * @compatible: The string to match to one of the tokens in the device
429 * "compatible" list.
430 *
431 * Returns a node pointer with refcount incremented, use
432 * of_node_put() on it when done.
433 */
434struct device_node *of_find_compatible_node(struct device_node *from,
435 const char *type, const char *compatible)
436{
437 struct device_node *np;
438
439 read_lock(&devtree_lock);
440 np = from ? from->allnext : allnodes;
441 for (; np; np = np->allnext) {
442 if (type
443 && !(np->type && (of_node_cmp(np->type, type) == 0)))
444 continue;
445 if (of_device_is_compatible(np, compatible) && of_node_get(np))
446 break;
447 }
448 of_node_put(from);
449 read_unlock(&devtree_lock);
450 return np;
451}
452EXPORT_SYMBOL(of_find_compatible_node);
Grant Likely283029d2008-01-09 06:20:40 +1100453
454/**
Michael Ellerman1e291b12008-11-12 18:54:42 +0000455 * of_find_node_with_property - Find a node which has a property with
456 * the given name.
457 * @from: The node to start searching from or NULL, the node
458 * you pass will not be searched, only the next one
459 * will; typically, you pass what the previous call
460 * returned. of_node_put() will be called on it
461 * @prop_name: The name of the property to look for.
462 *
463 * Returns a node pointer with refcount incremented, use
464 * of_node_put() on it when done.
465 */
466struct device_node *of_find_node_with_property(struct device_node *from,
467 const char *prop_name)
468{
469 struct device_node *np;
470 struct property *pp;
471
472 read_lock(&devtree_lock);
473 np = from ? from->allnext : allnodes;
474 for (; np; np = np->allnext) {
475 for (pp = np->properties; pp != 0; pp = pp->next) {
476 if (of_prop_cmp(pp->name, prop_name) == 0) {
477 of_node_get(np);
478 goto out;
479 }
480 }
481 }
482out:
483 of_node_put(from);
484 read_unlock(&devtree_lock);
485 return np;
486}
487EXPORT_SYMBOL(of_find_node_with_property);
488
489/**
Grant Likely283029d2008-01-09 06:20:40 +1100490 * of_match_node - Tell if an device_node has a matching of_match structure
491 * @matches: array of of device match structures to search in
492 * @node: the of device structure to match against
493 *
494 * Low level utility function used by device matching.
495 */
496const struct of_device_id *of_match_node(const struct of_device_id *matches,
497 const struct device_node *node)
498{
499 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
500 int match = 1;
501 if (matches->name[0])
502 match &= node->name
503 && !strcmp(matches->name, node->name);
504 if (matches->type[0])
505 match &= node->type
506 && !strcmp(matches->type, node->type);
507 if (matches->compatible[0])
508 match &= of_device_is_compatible(node,
509 matches->compatible);
510 if (match)
511 return matches;
512 matches++;
513 }
514 return NULL;
515}
516EXPORT_SYMBOL(of_match_node);
517
518/**
519 * of_find_matching_node - Find a node based on an of_device_id match
520 * table.
521 * @from: The node to start searching from or NULL, the node
522 * you pass will not be searched, only the next one
523 * will; typically, you pass what the previous call
524 * returned. of_node_put() will be called on it
525 * @matches: array of of device match structures to search in
526 *
527 * Returns a node pointer with refcount incremented, use
528 * of_node_put() on it when done.
529 */
530struct device_node *of_find_matching_node(struct device_node *from,
531 const struct of_device_id *matches)
532{
533 struct device_node *np;
534
535 read_lock(&devtree_lock);
536 np = from ? from->allnext : allnodes;
537 for (; np; np = np->allnext) {
538 if (of_match_node(matches, np) && of_node_get(np))
539 break;
540 }
541 of_node_put(from);
542 read_unlock(&devtree_lock);
543 return np;
544}
545EXPORT_SYMBOL(of_find_matching_node);
Grant Likely3f07af42008-07-25 22:25:13 -0400546
547/**
Grant Likely3f07af42008-07-25 22:25:13 -0400548 * of_modalias_node - Lookup appropriate modalias for a device node
549 * @node: pointer to a device tree node
550 * @modalias: Pointer to buffer that modalias value will be copied into
551 * @len: Length of modalias value
552 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600553 * Based on the value of the compatible property, this routine will attempt
554 * to choose an appropriate modalias value for a particular device tree node.
555 * It does this by stripping the manufacturer prefix (as delimited by a ',')
556 * from the first entry in the compatible list property.
Grant Likely3f07af42008-07-25 22:25:13 -0400557 *
Grant Likely2ffe8c52010-06-08 07:48:19 -0600558 * This routine returns 0 on success, <0 on failure.
Grant Likely3f07af42008-07-25 22:25:13 -0400559 */
560int of_modalias_node(struct device_node *node, char *modalias, int len)
561{
Grant Likely2ffe8c52010-06-08 07:48:19 -0600562 const char *compatible, *p;
563 int cplen;
Grant Likely3f07af42008-07-25 22:25:13 -0400564
565 compatible = of_get_property(node, "compatible", &cplen);
Grant Likely2ffe8c52010-06-08 07:48:19 -0600566 if (!compatible || strlen(compatible) > cplen)
Grant Likely3f07af42008-07-25 22:25:13 -0400567 return -ENODEV;
Grant Likely3f07af42008-07-25 22:25:13 -0400568 p = strchr(compatible, ',');
Grant Likely2ffe8c52010-06-08 07:48:19 -0600569 strlcpy(modalias, p ? p + 1 : compatible, len);
Grant Likely3f07af42008-07-25 22:25:13 -0400570 return 0;
571}
572EXPORT_SYMBOL_GPL(of_modalias_node);
573
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000574/**
Jeremy Kerr89751a72010-02-01 21:34:11 -0700575 * of_find_node_by_phandle - Find a node given a phandle
576 * @handle: phandle of the node to find
577 *
578 * Returns a node pointer with refcount incremented, use
579 * of_node_put() on it when done.
580 */
581struct device_node *of_find_node_by_phandle(phandle handle)
582{
583 struct device_node *np;
584
585 read_lock(&devtree_lock);
586 for (np = allnodes; np; np = np->allnext)
587 if (np->phandle == handle)
588 break;
589 of_node_get(np);
590 read_unlock(&devtree_lock);
591 return np;
592}
593EXPORT_SYMBOL(of_find_node_by_phandle);
594
595/**
Grant Likely739649c2009-04-25 12:52:40 +0000596 * of_parse_phandle - Resolve a phandle property to a device_node pointer
597 * @np: Pointer to device node holding phandle property
598 * @phandle_name: Name of property holding a phandle value
599 * @index: For properties holding a table of phandles, this is the index into
600 * the table
601 *
602 * Returns the device_node pointer with refcount incremented. Use
603 * of_node_put() on it when done.
604 */
605struct device_node *
606of_parse_phandle(struct device_node *np, const char *phandle_name, int index)
607{
Grant Likely9a6b2e52010-07-23 01:48:25 -0600608 const __be32 *phandle;
Grant Likely739649c2009-04-25 12:52:40 +0000609 int size;
610
611 phandle = of_get_property(np, phandle_name, &size);
612 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
613 return NULL;
614
Grant Likely9a6b2e52010-07-23 01:48:25 -0600615 return of_find_node_by_phandle(be32_to_cpup(phandle + index));
Grant Likely739649c2009-04-25 12:52:40 +0000616}
617EXPORT_SYMBOL(of_parse_phandle);
618
619/**
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000620 * of_parse_phandles_with_args - Find a node pointed by phandle in a list
621 * @np: pointer to a device tree node containing a list
622 * @list_name: property name that contains a list
623 * @cells_name: property name that specifies phandles' arguments count
624 * @index: index of a phandle to parse out
Anton Vorontsov7736a3d2008-12-05 08:15:46 +0000625 * @out_node: optional pointer to device_node struct pointer (will be filled)
626 * @out_args: optional pointer to arguments pointer (will be filled)
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000627 *
628 * This function is useful to parse lists of phandles and their arguments.
629 * Returns 0 on success and fills out_node and out_args, on error returns
630 * appropriate errno value.
631 *
632 * Example:
633 *
634 * phandle1: node1 {
635 * #list-cells = <2>;
636 * }
637 *
638 * phandle2: node2 {
639 * #list-cells = <1>;
640 * }
641 *
642 * node3 {
643 * list = <&phandle1 1 2 &phandle2 3>;
644 * }
645 *
646 * To get a device_node of the `node2' node you may call this:
647 * of_parse_phandles_with_args(node3, "list", "#list-cells", 2, &node2, &args);
648 */
649int of_parse_phandles_with_args(struct device_node *np, const char *list_name,
650 const char *cells_name, int index,
651 struct device_node **out_node,
652 const void **out_args)
653{
654 int ret = -EINVAL;
Jeremy Kerr33714882010-01-30 01:45:26 -0700655 const __be32 *list;
656 const __be32 *list_end;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000657 int size;
658 int cur_index = 0;
659 struct device_node *node = NULL;
Anton Vorontsov7736a3d2008-12-05 08:15:46 +0000660 const void *args = NULL;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000661
662 list = of_get_property(np, list_name, &size);
663 if (!list) {
664 ret = -ENOENT;
665 goto err0;
666 }
667 list_end = list + size / sizeof(*list);
668
669 while (list < list_end) {
Jeremy Kerr33714882010-01-30 01:45:26 -0700670 const __be32 *cells;
Grant Likely9a6b2e52010-07-23 01:48:25 -0600671 phandle phandle;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000672
Grant Likely9a6b2e52010-07-23 01:48:25 -0600673 phandle = be32_to_cpup(list++);
Anton Vorontsovc1bb7c62008-12-05 08:15:39 +0000674 args = list;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000675
676 /* one cell hole in the list = <>; */
Grant Likely9a6b2e52010-07-23 01:48:25 -0600677 if (!phandle)
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000678 goto next;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000679
Grant Likely9a6b2e52010-07-23 01:48:25 -0600680 node = of_find_node_by_phandle(phandle);
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000681 if (!node) {
682 pr_debug("%s: could not find phandle\n",
683 np->full_name);
684 goto err0;
685 }
686
687 cells = of_get_property(node, cells_name, &size);
688 if (!cells || size != sizeof(*cells)) {
689 pr_debug("%s: could not get %s for %s\n",
690 np->full_name, cells_name, node->full_name);
691 goto err1;
692 }
693
Jeremy Kerr33714882010-01-30 01:45:26 -0700694 list += be32_to_cpup(cells);
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000695 if (list > list_end) {
696 pr_debug("%s: insufficient arguments length\n",
697 np->full_name);
698 goto err1;
699 }
700next:
701 if (cur_index == index)
702 break;
703
704 of_node_put(node);
705 node = NULL;
Anton Vorontsov7736a3d2008-12-05 08:15:46 +0000706 args = NULL;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000707 cur_index++;
708 }
709
710 if (!node) {
Anton Vorontsov7736a3d2008-12-05 08:15:46 +0000711 /*
712 * args w/o node indicates that the loop above has stopped at
713 * the 'hole' cell. Report this differently.
714 */
715 if (args)
716 ret = -EEXIST;
717 else
718 ret = -ENOENT;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000719 goto err0;
720 }
721
Anton Vorontsov7736a3d2008-12-05 08:15:46 +0000722 if (out_node)
723 *out_node = node;
724 if (out_args)
725 *out_args = args;
Anton Vorontsov64b60e02008-10-10 04:43:17 +0000726
727 return 0;
728err1:
729 of_node_put(node);
730err0:
731 pr_debug("%s failed with status %d\n", __func__, ret);
732 return ret;
733}
734EXPORT_SYMBOL(of_parse_phandles_with_args);
Grant Likely02af11b2009-11-23 20:16:45 -0700735
736/**
737 * prom_add_property - Add a property to a node
738 */
739int prom_add_property(struct device_node *np, struct property *prop)
740{
741 struct property **next;
742 unsigned long flags;
743
744 prop->next = NULL;
745 write_lock_irqsave(&devtree_lock, flags);
746 next = &np->properties;
747 while (*next) {
748 if (strcmp(prop->name, (*next)->name) == 0) {
749 /* duplicate ! don't insert it */
750 write_unlock_irqrestore(&devtree_lock, flags);
751 return -1;
752 }
753 next = &(*next)->next;
754 }
755 *next = prop;
756 write_unlock_irqrestore(&devtree_lock, flags);
757
758#ifdef CONFIG_PROC_DEVICETREE
759 /* try to add to proc as well if it was initialized */
760 if (np->pde)
761 proc_device_tree_add_prop(np->pde, prop);
762#endif /* CONFIG_PROC_DEVICETREE */
763
764 return 0;
765}
766
767/**
768 * prom_remove_property - Remove a property from a node.
769 *
770 * Note that we don't actually remove it, since we have given out
771 * who-knows-how-many pointers to the data using get-property.
772 * Instead we just move the property to the "dead properties"
773 * list, so it won't be found any more.
774 */
775int prom_remove_property(struct device_node *np, struct property *prop)
776{
777 struct property **next;
778 unsigned long flags;
779 int found = 0;
780
781 write_lock_irqsave(&devtree_lock, flags);
782 next = &np->properties;
783 while (*next) {
784 if (*next == prop) {
785 /* found the node */
786 *next = prop->next;
787 prop->next = np->deadprops;
788 np->deadprops = prop;
789 found = 1;
790 break;
791 }
792 next = &(*next)->next;
793 }
794 write_unlock_irqrestore(&devtree_lock, flags);
795
796 if (!found)
797 return -ENODEV;
798
799#ifdef CONFIG_PROC_DEVICETREE
800 /* try to remove the proc node as well */
801 if (np->pde)
802 proc_device_tree_remove_prop(np->pde, prop);
803#endif /* CONFIG_PROC_DEVICETREE */
804
805 return 0;
806}
807
808/*
809 * prom_update_property - Update a property in a node.
810 *
811 * Note that we don't actually remove it, since we have given out
812 * who-knows-how-many pointers to the data using get-property.
813 * Instead we just move the property to the "dead properties" list,
814 * and add the new property to the property list
815 */
816int prom_update_property(struct device_node *np,
817 struct property *newprop,
818 struct property *oldprop)
819{
820 struct property **next;
821 unsigned long flags;
822 int found = 0;
823
824 write_lock_irqsave(&devtree_lock, flags);
825 next = &np->properties;
826 while (*next) {
827 if (*next == oldprop) {
828 /* found the node */
829 newprop->next = oldprop->next;
830 *next = newprop;
831 oldprop->next = np->deadprops;
832 np->deadprops = oldprop;
833 found = 1;
834 break;
835 }
836 next = &(*next)->next;
837 }
838 write_unlock_irqrestore(&devtree_lock, flags);
839
840 if (!found)
841 return -ENODEV;
842
843#ifdef CONFIG_PROC_DEVICETREE
844 /* try to add to proc as well if it was initialized */
845 if (np->pde)
846 proc_device_tree_update_prop(np->pde, newprop, oldprop);
847#endif /* CONFIG_PROC_DEVICETREE */
848
849 return 0;
850}
Grant Likelyfcdeb7f2010-01-29 05:04:33 -0700851
852#if defined(CONFIG_OF_DYNAMIC)
853/*
854 * Support for dynamic device trees.
855 *
856 * On some platforms, the device tree can be manipulated at runtime.
857 * The routines in this section support adding, removing and changing
858 * device tree nodes.
859 */
860
861/**
862 * of_attach_node - Plug a device node into the tree and global list.
863 */
864void of_attach_node(struct device_node *np)
865{
866 unsigned long flags;
867
868 write_lock_irqsave(&devtree_lock, flags);
869 np->sibling = np->parent->child;
870 np->allnext = allnodes;
871 np->parent->child = np;
872 allnodes = np;
873 write_unlock_irqrestore(&devtree_lock, flags);
874}
875
876/**
877 * of_detach_node - "Unplug" a node from the device tree.
878 *
879 * The caller must hold a reference to the node. The memory associated with
880 * the node is not freed until its refcount goes to zero.
881 */
882void of_detach_node(struct device_node *np)
883{
884 struct device_node *parent;
885 unsigned long flags;
886
887 write_lock_irqsave(&devtree_lock, flags);
888
889 parent = np->parent;
890 if (!parent)
891 goto out_unlock;
892
893 if (allnodes == np)
894 allnodes = np->allnext;
895 else {
896 struct device_node *prev;
897 for (prev = allnodes;
898 prev->allnext != np;
899 prev = prev->allnext)
900 ;
901 prev->allnext = np->allnext;
902 }
903
904 if (parent->child == np)
905 parent->child = np->sibling;
906 else {
907 struct device_node *prevsib;
908 for (prevsib = np->parent->child;
909 prevsib->sibling != np;
910 prevsib = prevsib->sibling)
911 ;
912 prevsib->sibling = np->sibling;
913 }
914
915 of_node_set_flag(np, OF_DETACHED);
916
917out_unlock:
918 write_unlock_irqrestore(&devtree_lock, flags);
919}
920#endif /* defined(CONFIG_OF_DYNAMIC) */
921