Merge branch 'highbank/soc' into next/soc
[deliverable/linux.git] / drivers / of / base.c
1 /*
2 * Procedures for creating, accessing and interpreting the device tree.
3 *
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
6 *
7 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8 * {engebret|bergner}@us.ibm.com
9 *
10 * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
11 *
12 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
13 * Grant Likely.
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
19 */
20 #include <linux/module.h>
21 #include <linux/of.h>
22 #include <linux/spinlock.h>
23 #include <linux/slab.h>
24 #include <linux/proc_fs.h>
25
26 struct device_node *allnodes;
27 struct device_node *of_chosen;
28
29 /* use when traversing tree through the allnext, child, sibling,
30 * or parent members of struct device_node.
31 */
32 DEFINE_RWLOCK(devtree_lock);
33
34 int of_n_addr_cells(struct device_node *np)
35 {
36 const __be32 *ip;
37
38 do {
39 if (np->parent)
40 np = np->parent;
41 ip = of_get_property(np, "#address-cells", NULL);
42 if (ip)
43 return be32_to_cpup(ip);
44 } while (np->parent);
45 /* No #address-cells property for the root node */
46 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
47 }
48 EXPORT_SYMBOL(of_n_addr_cells);
49
50 int of_n_size_cells(struct device_node *np)
51 {
52 const __be32 *ip;
53
54 do {
55 if (np->parent)
56 np = np->parent;
57 ip = of_get_property(np, "#size-cells", NULL);
58 if (ip)
59 return be32_to_cpup(ip);
60 } while (np->parent);
61 /* No #size-cells property for the root node */
62 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
63 }
64 EXPORT_SYMBOL(of_n_size_cells);
65
66 #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 */
74 struct device_node *of_node_get(struct device_node *node)
75 {
76 if (node)
77 kref_get(&node->kref);
78 return node;
79 }
80 EXPORT_SYMBOL(of_node_get);
81
82 static 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 */
94 static 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 */
133 void of_node_put(struct device_node *node)
134 {
135 if (node)
136 kref_put(&node->kref, of_node_release);
137 }
138 EXPORT_SYMBOL(of_node_put);
139 #endif /* !CONFIG_SPARC */
140
141 struct property *of_find_property(const struct device_node *np,
142 const char *name,
143 int *lenp)
144 {
145 struct property *pp;
146
147 if (!np)
148 return NULL;
149
150 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 }
162 EXPORT_SYMBOL(of_find_property);
163
164 /**
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 */
172 struct 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 }
185 EXPORT_SYMBOL(of_find_all_nodes);
186
187 /*
188 * Find a property with a given name for a given node
189 * and return the value.
190 */
191 const 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 }
198 EXPORT_SYMBOL(of_get_property);
199
200 /** Checks if the given "compat" string matches one of the strings in
201 * the device's "compatible" property
202 */
203 int 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 }
222 EXPORT_SYMBOL(of_device_is_compatible);
223
224 /**
225 * of_machine_is_compatible - Test root of device tree for a given compatible value
226 * @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 */
231 int of_machine_is_compatible(const char *compat)
232 {
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 }
243 EXPORT_SYMBOL(of_machine_is_compatible);
244
245 /**
246 * 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 */
253 int 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 }
269 EXPORT_SYMBOL(of_device_is_available);
270
271 /**
272 * 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 */
278 struct 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 }
290 EXPORT_SYMBOL(of_get_parent);
291
292 /**
293 * 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 */
303 struct 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 /**
318 * 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 */
325 struct 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 }
339 EXPORT_SYMBOL(of_get_next_child);
340
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 */
348 struct 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 }
361 EXPORT_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 */
374 struct 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 }
389 EXPORT_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 */
403 struct 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 }
418 EXPORT_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 */
434 struct 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 }
452 EXPORT_SYMBOL(of_find_compatible_node);
453
454 /**
455 * 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 */
466 struct 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 }
482 out:
483 of_node_put(from);
484 read_unlock(&devtree_lock);
485 return np;
486 }
487 EXPORT_SYMBOL(of_find_node_with_property);
488
489 /**
490 * 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 */
496 const struct of_device_id *of_match_node(const struct of_device_id *matches,
497 const struct device_node *node)
498 {
499 if (!matches)
500 return NULL;
501
502 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
503 int match = 1;
504 if (matches->name[0])
505 match &= node->name
506 && !strcmp(matches->name, node->name);
507 if (matches->type[0])
508 match &= node->type
509 && !strcmp(matches->type, node->type);
510 if (matches->compatible[0])
511 match &= of_device_is_compatible(node,
512 matches->compatible);
513 if (match)
514 return matches;
515 matches++;
516 }
517 return NULL;
518 }
519 EXPORT_SYMBOL(of_match_node);
520
521 /**
522 * of_find_matching_node - Find a node based on an of_device_id match
523 * table.
524 * @from: The node to start searching from or NULL, the node
525 * you pass will not be searched, only the next one
526 * will; typically, you pass what the previous call
527 * returned. of_node_put() will be called on it
528 * @matches: array of of device match structures to search in
529 *
530 * Returns a node pointer with refcount incremented, use
531 * of_node_put() on it when done.
532 */
533 struct device_node *of_find_matching_node(struct device_node *from,
534 const struct of_device_id *matches)
535 {
536 struct device_node *np;
537
538 read_lock(&devtree_lock);
539 np = from ? from->allnext : allnodes;
540 for (; np; np = np->allnext) {
541 if (of_match_node(matches, np) && of_node_get(np))
542 break;
543 }
544 of_node_put(from);
545 read_unlock(&devtree_lock);
546 return np;
547 }
548 EXPORT_SYMBOL(of_find_matching_node);
549
550 /**
551 * of_modalias_node - Lookup appropriate modalias for a device node
552 * @node: pointer to a device tree node
553 * @modalias: Pointer to buffer that modalias value will be copied into
554 * @len: Length of modalias value
555 *
556 * Based on the value of the compatible property, this routine will attempt
557 * to choose an appropriate modalias value for a particular device tree node.
558 * It does this by stripping the manufacturer prefix (as delimited by a ',')
559 * from the first entry in the compatible list property.
560 *
561 * This routine returns 0 on success, <0 on failure.
562 */
563 int of_modalias_node(struct device_node *node, char *modalias, int len)
564 {
565 const char *compatible, *p;
566 int cplen;
567
568 compatible = of_get_property(node, "compatible", &cplen);
569 if (!compatible || strlen(compatible) > cplen)
570 return -ENODEV;
571 p = strchr(compatible, ',');
572 strlcpy(modalias, p ? p + 1 : compatible, len);
573 return 0;
574 }
575 EXPORT_SYMBOL_GPL(of_modalias_node);
576
577 /**
578 * of_find_node_by_phandle - Find a node given a phandle
579 * @handle: phandle of the node to find
580 *
581 * Returns a node pointer with refcount incremented, use
582 * of_node_put() on it when done.
583 */
584 struct device_node *of_find_node_by_phandle(phandle handle)
585 {
586 struct device_node *np;
587
588 read_lock(&devtree_lock);
589 for (np = allnodes; np; np = np->allnext)
590 if (np->phandle == handle)
591 break;
592 of_node_get(np);
593 read_unlock(&devtree_lock);
594 return np;
595 }
596 EXPORT_SYMBOL(of_find_node_by_phandle);
597
598 /**
599 * of_property_read_u32_array - Find and read an array of 32 bit integers
600 * from a property.
601 *
602 * @np: device node from which the property value is to be read.
603 * @propname: name of the property to be searched.
604 * @out_value: pointer to return value, modified only if return value is 0.
605 *
606 * Search for a property in a device node and read 32-bit value(s) from
607 * it. Returns 0 on success, -EINVAL if the property does not exist,
608 * -ENODATA if property does not have a value, and -EOVERFLOW if the
609 * property data isn't large enough.
610 *
611 * The out_value is modified only if a valid u32 value can be decoded.
612 */
613 int of_property_read_u32_array(const struct device_node *np,
614 const char *propname, u32 *out_values,
615 size_t sz)
616 {
617 struct property *prop = of_find_property(np, propname, NULL);
618 const __be32 *val;
619
620 if (!prop)
621 return -EINVAL;
622 if (!prop->value)
623 return -ENODATA;
624 if ((sz * sizeof(*out_values)) > prop->length)
625 return -EOVERFLOW;
626
627 val = prop->value;
628 while (sz--)
629 *out_values++ = be32_to_cpup(val++);
630 return 0;
631 }
632 EXPORT_SYMBOL_GPL(of_property_read_u32_array);
633
634 /**
635 * of_property_read_string - Find and read a string from a property
636 * @np: device node from which the property value is to be read.
637 * @propname: name of the property to be searched.
638 * @out_string: pointer to null terminated return string, modified only if
639 * return value is 0.
640 *
641 * Search for a property in a device tree node and retrieve a null
642 * terminated string value (pointer to data, not a copy). Returns 0 on
643 * success, -EINVAL if the property does not exist, -ENODATA if property
644 * does not have a value, and -EILSEQ if the string is not null-terminated
645 * within the length of the property data.
646 *
647 * The out_string pointer is modified only if a valid string can be decoded.
648 */
649 int of_property_read_string(struct device_node *np, const char *propname,
650 const char **out_string)
651 {
652 struct property *prop = of_find_property(np, propname, NULL);
653 if (!prop)
654 return -EINVAL;
655 if (!prop->value)
656 return -ENODATA;
657 if (strnlen(prop->value, prop->length) >= prop->length)
658 return -EILSEQ;
659 *out_string = prop->value;
660 return 0;
661 }
662 EXPORT_SYMBOL_GPL(of_property_read_string);
663
664 /**
665 * of_property_read_string_index - Find and read a string from a multiple
666 * strings property.
667 * @np: device node from which the property value is to be read.
668 * @propname: name of the property to be searched.
669 * @index: index of the string in the list of strings
670 * @out_string: pointer to null terminated return string, modified only if
671 * return value is 0.
672 *
673 * Search for a property in a device tree node and retrieve a null
674 * terminated string value (pointer to data, not a copy) in the list of strings
675 * contained in that property.
676 * Returns 0 on success, -EINVAL if the property does not exist, -ENODATA if
677 * property does not have a value, and -EILSEQ if the string is not
678 * null-terminated within the length of the property data.
679 *
680 * The out_string pointer is modified only if a valid string can be decoded.
681 */
682 int of_property_read_string_index(struct device_node *np, const char *propname,
683 int index, const char **output)
684 {
685 struct property *prop = of_find_property(np, propname, NULL);
686 int i = 0;
687 size_t l = 0, total = 0;
688 const char *p;
689
690 if (!prop)
691 return -EINVAL;
692 if (!prop->value)
693 return -ENODATA;
694 if (strnlen(prop->value, prop->length) >= prop->length)
695 return -EILSEQ;
696
697 p = prop->value;
698
699 for (i = 0; total < prop->length; total += l, p += l) {
700 l = strlen(p) + 1;
701 if ((*p != 0) && (i++ == index)) {
702 *output = p;
703 return 0;
704 }
705 }
706 return -ENODATA;
707 }
708 EXPORT_SYMBOL_GPL(of_property_read_string_index);
709
710
711 /**
712 * of_property_count_strings - Find and return the number of strings from a
713 * multiple strings property.
714 * @np: device node from which the property value is to be read.
715 * @propname: name of the property to be searched.
716 *
717 * Search for a property in a device tree node and retrieve the number of null
718 * terminated string contain in it. Returns the number of strings on
719 * success, -EINVAL if the property does not exist, -ENODATA if property
720 * does not have a value, and -EILSEQ if the string is not null-terminated
721 * within the length of the property data.
722 */
723 int of_property_count_strings(struct device_node *np, const char *propname)
724 {
725 struct property *prop = of_find_property(np, propname, NULL);
726 int i = 0;
727 size_t l = 0, total = 0;
728 const char *p;
729
730 if (!prop)
731 return -EINVAL;
732 if (!prop->value)
733 return -ENODATA;
734 if (strnlen(prop->value, prop->length) >= prop->length)
735 return -EILSEQ;
736
737 p = prop->value;
738
739 for (i = 0; total < prop->length; total += l, p += l) {
740 l = strlen(p) + 1;
741 if (*p != 0)
742 i++;
743 }
744 return i;
745 }
746 EXPORT_SYMBOL_GPL(of_property_count_strings);
747
748 /**
749 * of_parse_phandle - Resolve a phandle property to a device_node pointer
750 * @np: Pointer to device node holding phandle property
751 * @phandle_name: Name of property holding a phandle value
752 * @index: For properties holding a table of phandles, this is the index into
753 * the table
754 *
755 * Returns the device_node pointer with refcount incremented. Use
756 * of_node_put() on it when done.
757 */
758 struct device_node *
759 of_parse_phandle(struct device_node *np, const char *phandle_name, int index)
760 {
761 const __be32 *phandle;
762 int size;
763
764 phandle = of_get_property(np, phandle_name, &size);
765 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
766 return NULL;
767
768 return of_find_node_by_phandle(be32_to_cpup(phandle + index));
769 }
770 EXPORT_SYMBOL(of_parse_phandle);
771
772 /**
773 * of_parse_phandles_with_args - Find a node pointed by phandle in a list
774 * @np: pointer to a device tree node containing a list
775 * @list_name: property name that contains a list
776 * @cells_name: property name that specifies phandles' arguments count
777 * @index: index of a phandle to parse out
778 * @out_node: optional pointer to device_node struct pointer (will be filled)
779 * @out_args: optional pointer to arguments pointer (will be filled)
780 *
781 * This function is useful to parse lists of phandles and their arguments.
782 * Returns 0 on success and fills out_node and out_args, on error returns
783 * appropriate errno value.
784 *
785 * Example:
786 *
787 * phandle1: node1 {
788 * #list-cells = <2>;
789 * }
790 *
791 * phandle2: node2 {
792 * #list-cells = <1>;
793 * }
794 *
795 * node3 {
796 * list = <&phandle1 1 2 &phandle2 3>;
797 * }
798 *
799 * To get a device_node of the `node2' node you may call this:
800 * of_parse_phandles_with_args(node3, "list", "#list-cells", 2, &node2, &args);
801 */
802 int of_parse_phandles_with_args(struct device_node *np, const char *list_name,
803 const char *cells_name, int index,
804 struct device_node **out_node,
805 const void **out_args)
806 {
807 int ret = -EINVAL;
808 const __be32 *list;
809 const __be32 *list_end;
810 int size;
811 int cur_index = 0;
812 struct device_node *node = NULL;
813 const void *args = NULL;
814
815 list = of_get_property(np, list_name, &size);
816 if (!list) {
817 ret = -ENOENT;
818 goto err0;
819 }
820 list_end = list + size / sizeof(*list);
821
822 while (list < list_end) {
823 const __be32 *cells;
824 phandle phandle;
825
826 phandle = be32_to_cpup(list++);
827 args = list;
828
829 /* one cell hole in the list = <>; */
830 if (!phandle)
831 goto next;
832
833 node = of_find_node_by_phandle(phandle);
834 if (!node) {
835 pr_debug("%s: could not find phandle\n",
836 np->full_name);
837 goto err0;
838 }
839
840 cells = of_get_property(node, cells_name, &size);
841 if (!cells || size != sizeof(*cells)) {
842 pr_debug("%s: could not get %s for %s\n",
843 np->full_name, cells_name, node->full_name);
844 goto err1;
845 }
846
847 list += be32_to_cpup(cells);
848 if (list > list_end) {
849 pr_debug("%s: insufficient arguments length\n",
850 np->full_name);
851 goto err1;
852 }
853 next:
854 if (cur_index == index)
855 break;
856
857 of_node_put(node);
858 node = NULL;
859 args = NULL;
860 cur_index++;
861 }
862
863 if (!node) {
864 /*
865 * args w/o node indicates that the loop above has stopped at
866 * the 'hole' cell. Report this differently.
867 */
868 if (args)
869 ret = -EEXIST;
870 else
871 ret = -ENOENT;
872 goto err0;
873 }
874
875 if (out_node)
876 *out_node = node;
877 if (out_args)
878 *out_args = args;
879
880 return 0;
881 err1:
882 of_node_put(node);
883 err0:
884 pr_debug("%s failed with status %d\n", __func__, ret);
885 return ret;
886 }
887 EXPORT_SYMBOL(of_parse_phandles_with_args);
888
889 /**
890 * prom_add_property - Add a property to a node
891 */
892 int prom_add_property(struct device_node *np, struct property *prop)
893 {
894 struct property **next;
895 unsigned long flags;
896
897 prop->next = NULL;
898 write_lock_irqsave(&devtree_lock, flags);
899 next = &np->properties;
900 while (*next) {
901 if (strcmp(prop->name, (*next)->name) == 0) {
902 /* duplicate ! don't insert it */
903 write_unlock_irqrestore(&devtree_lock, flags);
904 return -1;
905 }
906 next = &(*next)->next;
907 }
908 *next = prop;
909 write_unlock_irqrestore(&devtree_lock, flags);
910
911 #ifdef CONFIG_PROC_DEVICETREE
912 /* try to add to proc as well if it was initialized */
913 if (np->pde)
914 proc_device_tree_add_prop(np->pde, prop);
915 #endif /* CONFIG_PROC_DEVICETREE */
916
917 return 0;
918 }
919
920 /**
921 * prom_remove_property - Remove a property from a node.
922 *
923 * Note that we don't actually remove it, since we have given out
924 * who-knows-how-many pointers to the data using get-property.
925 * Instead we just move the property to the "dead properties"
926 * list, so it won't be found any more.
927 */
928 int prom_remove_property(struct device_node *np, struct property *prop)
929 {
930 struct property **next;
931 unsigned long flags;
932 int found = 0;
933
934 write_lock_irqsave(&devtree_lock, flags);
935 next = &np->properties;
936 while (*next) {
937 if (*next == prop) {
938 /* found the node */
939 *next = prop->next;
940 prop->next = np->deadprops;
941 np->deadprops = prop;
942 found = 1;
943 break;
944 }
945 next = &(*next)->next;
946 }
947 write_unlock_irqrestore(&devtree_lock, flags);
948
949 if (!found)
950 return -ENODEV;
951
952 #ifdef CONFIG_PROC_DEVICETREE
953 /* try to remove the proc node as well */
954 if (np->pde)
955 proc_device_tree_remove_prop(np->pde, prop);
956 #endif /* CONFIG_PROC_DEVICETREE */
957
958 return 0;
959 }
960
961 /*
962 * prom_update_property - Update a property in a node.
963 *
964 * Note that we don't actually remove it, since we have given out
965 * who-knows-how-many pointers to the data using get-property.
966 * Instead we just move the property to the "dead properties" list,
967 * and add the new property to the property list
968 */
969 int prom_update_property(struct device_node *np,
970 struct property *newprop,
971 struct property *oldprop)
972 {
973 struct property **next;
974 unsigned long flags;
975 int found = 0;
976
977 write_lock_irqsave(&devtree_lock, flags);
978 next = &np->properties;
979 while (*next) {
980 if (*next == oldprop) {
981 /* found the node */
982 newprop->next = oldprop->next;
983 *next = newprop;
984 oldprop->next = np->deadprops;
985 np->deadprops = oldprop;
986 found = 1;
987 break;
988 }
989 next = &(*next)->next;
990 }
991 write_unlock_irqrestore(&devtree_lock, flags);
992
993 if (!found)
994 return -ENODEV;
995
996 #ifdef CONFIG_PROC_DEVICETREE
997 /* try to add to proc as well if it was initialized */
998 if (np->pde)
999 proc_device_tree_update_prop(np->pde, newprop, oldprop);
1000 #endif /* CONFIG_PROC_DEVICETREE */
1001
1002 return 0;
1003 }
1004
1005 #if defined(CONFIG_OF_DYNAMIC)
1006 /*
1007 * Support for dynamic device trees.
1008 *
1009 * On some platforms, the device tree can be manipulated at runtime.
1010 * The routines in this section support adding, removing and changing
1011 * device tree nodes.
1012 */
1013
1014 /**
1015 * of_attach_node - Plug a device node into the tree and global list.
1016 */
1017 void of_attach_node(struct device_node *np)
1018 {
1019 unsigned long flags;
1020
1021 write_lock_irqsave(&devtree_lock, flags);
1022 np->sibling = np->parent->child;
1023 np->allnext = allnodes;
1024 np->parent->child = np;
1025 allnodes = np;
1026 write_unlock_irqrestore(&devtree_lock, flags);
1027 }
1028
1029 /**
1030 * of_detach_node - "Unplug" a node from the device tree.
1031 *
1032 * The caller must hold a reference to the node. The memory associated with
1033 * the node is not freed until its refcount goes to zero.
1034 */
1035 void of_detach_node(struct device_node *np)
1036 {
1037 struct device_node *parent;
1038 unsigned long flags;
1039
1040 write_lock_irqsave(&devtree_lock, flags);
1041
1042 parent = np->parent;
1043 if (!parent)
1044 goto out_unlock;
1045
1046 if (allnodes == np)
1047 allnodes = np->allnext;
1048 else {
1049 struct device_node *prev;
1050 for (prev = allnodes;
1051 prev->allnext != np;
1052 prev = prev->allnext)
1053 ;
1054 prev->allnext = np->allnext;
1055 }
1056
1057 if (parent->child == np)
1058 parent->child = np->sibling;
1059 else {
1060 struct device_node *prevsib;
1061 for (prevsib = np->parent->child;
1062 prevsib->sibling != np;
1063 prevsib = prevsib->sibling)
1064 ;
1065 prevsib->sibling = np->sibling;
1066 }
1067
1068 of_node_set_flag(np, OF_DETACHED);
1069
1070 out_unlock:
1071 write_unlock_irqrestore(&devtree_lock, flags);
1072 }
1073 #endif /* defined(CONFIG_OF_DYNAMIC) */
1074
This page took 0.052552 seconds and 6 git commands to generate.