of: use __be32 for cell value accessors
[GitHub/mt8127/android_kernel_alcatel_ttab.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 */
20#include <linux/module.h>
21#include <linux/of.h>
581b605a 22#include <linux/spinlock.h>
a9f2f63a 23#include <linux/proc_fs.h>
581b605a 24
1ef4d424
SR
25struct device_node *allnodes;
26
581b605a
SR
27/* use when traversing tree through the allnext, child, sibling,
28 * or parent members of struct device_node.
29 */
30DEFINE_RWLOCK(devtree_lock);
97e873e5
SR
31
32int of_n_addr_cells(struct device_node *np)
33{
34 const int *ip;
35
36 do {
37 if (np->parent)
38 np = np->parent;
39 ip = of_get_property(np, "#address-cells", NULL);
40 if (ip)
41 return *ip;
42 } while (np->parent);
43 /* No #address-cells property for the root node */
44 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
45}
46EXPORT_SYMBOL(of_n_addr_cells);
47
48int of_n_size_cells(struct device_node *np)
49{
50 const int *ip;
51
52 do {
53 if (np->parent)
54 np = np->parent;
55 ip = of_get_property(np, "#size-cells", NULL);
56 if (ip)
57 return *ip;
58 } while (np->parent);
59 /* No #size-cells property for the root node */
60 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
61}
62EXPORT_SYMBOL(of_n_size_cells);
63
923f7e30
GL
64#if !defined(CONFIG_SPARC) /* SPARC doesn't do ref counting (yet) */
65/**
66 * of_node_get - Increment refcount of a node
67 * @node: Node to inc refcount, NULL is supported to
68 * simplify writing of callers
69 *
70 * Returns node.
71 */
72struct device_node *of_node_get(struct device_node *node)
73{
74 if (node)
75 kref_get(&node->kref);
76 return node;
77}
78EXPORT_SYMBOL(of_node_get);
79
80static inline struct device_node *kref_to_device_node(struct kref *kref)
81{
82 return container_of(kref, struct device_node, kref);
83}
84
85/**
86 * of_node_release - release a dynamically allocated node
87 * @kref: kref element of the node to be released
88 *
89 * In of_node_put() this function is passed to kref_put()
90 * as the destructor.
91 */
92static void of_node_release(struct kref *kref)
93{
94 struct device_node *node = kref_to_device_node(kref);
95 struct property *prop = node->properties;
96
97 /* We should never be releasing nodes that haven't been detached. */
98 if (!of_node_check_flag(node, OF_DETACHED)) {
99 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
100 dump_stack();
101 kref_init(&node->kref);
102 return;
103 }
104
105 if (!of_node_check_flag(node, OF_DYNAMIC))
106 return;
107
108 while (prop) {
109 struct property *next = prop->next;
110 kfree(prop->name);
111 kfree(prop->value);
112 kfree(prop);
113 prop = next;
114
115 if (!prop) {
116 prop = node->deadprops;
117 node->deadprops = NULL;
118 }
119 }
120 kfree(node->full_name);
121 kfree(node->data);
122 kfree(node);
123}
124
125/**
126 * of_node_put - Decrement refcount of a node
127 * @node: Node to dec refcount, NULL is supported to
128 * simplify writing of callers
129 *
130 */
131void of_node_put(struct device_node *node)
132{
133 if (node)
134 kref_put(&node->kref, of_node_release);
135}
136EXPORT_SYMBOL(of_node_put);
137#endif /* !CONFIG_SPARC */
138
581b605a
SR
139struct property *of_find_property(const struct device_node *np,
140 const char *name,
141 int *lenp)
142{
143 struct property *pp;
144
64e4566f
TT
145 if (!np)
146 return NULL;
147
581b605a
SR
148 read_lock(&devtree_lock);
149 for (pp = np->properties; pp != 0; pp = pp->next) {
150 if (of_prop_cmp(pp->name, name) == 0) {
151 if (lenp != 0)
152 *lenp = pp->length;
153 break;
154 }
155 }
156 read_unlock(&devtree_lock);
157
158 return pp;
159}
160EXPORT_SYMBOL(of_find_property);
161
e91edcf5
GL
162/**
163 * of_find_all_nodes - Get next node in global list
164 * @prev: Previous node or NULL to start iteration
165 * of_node_put() will be called on it
166 *
167 * Returns a node pointer with refcount incremented, use
168 * of_node_put() on it when done.
169 */
170struct device_node *of_find_all_nodes(struct device_node *prev)
171{
172 struct device_node *np;
173
174 read_lock(&devtree_lock);
175 np = prev ? prev->allnext : allnodes;
176 for (; np != NULL; np = np->allnext)
177 if (of_node_get(np))
178 break;
179 of_node_put(prev);
180 read_unlock(&devtree_lock);
181 return np;
182}
183EXPORT_SYMBOL(of_find_all_nodes);
184
97e873e5
SR
185/*
186 * Find a property with a given name for a given node
187 * and return the value.
188 */
189const void *of_get_property(const struct device_node *np, const char *name,
190 int *lenp)
191{
192 struct property *pp = of_find_property(np, name, lenp);
193
194 return pp ? pp->value : NULL;
195}
196EXPORT_SYMBOL(of_get_property);
0081cbc3
SR
197
198/** Checks if the given "compat" string matches one of the strings in
199 * the device's "compatible" property
200 */
201int of_device_is_compatible(const struct device_node *device,
202 const char *compat)
203{
204 const char* cp;
205 int cplen, l;
206
207 cp = of_get_property(device, "compatible", &cplen);
208 if (cp == NULL)
209 return 0;
210 while (cplen > 0) {
211 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
212 return 1;
213 l = strlen(cp) + 1;
214 cp += l;
215 cplen -= l;
216 }
217
218 return 0;
219}
220EXPORT_SYMBOL(of_device_is_compatible);
e679c5f4 221
1f43cfb9 222/**
71a157e8 223 * of_machine_is_compatible - Test root of device tree for a given compatible value
1f43cfb9
GL
224 * @compat: compatible string to look for in root node's compatible property.
225 *
226 * Returns true if the root node has the given value in its
227 * compatible property.
228 */
71a157e8 229int of_machine_is_compatible(const char *compat)
1f43cfb9
GL
230{
231 struct device_node *root;
232 int rc = 0;
233
234 root = of_find_node_by_path("/");
235 if (root) {
236 rc = of_device_is_compatible(root, compat);
237 of_node_put(root);
238 }
239 return rc;
240}
71a157e8 241EXPORT_SYMBOL(of_machine_is_compatible);
1f43cfb9 242
834d97d4
JB
243/**
244 * of_device_is_available - check if a device is available for use
245 *
246 * @device: Node to check for availability
247 *
248 * Returns 1 if the status property is absent or set to "okay" or "ok",
249 * 0 otherwise
250 */
251int of_device_is_available(const struct device_node *device)
252{
253 const char *status;
254 int statlen;
255
256 status = of_get_property(device, "status", &statlen);
257 if (status == NULL)
258 return 1;
259
260 if (statlen > 0) {
261 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
262 return 1;
263 }
264
265 return 0;
266}
267EXPORT_SYMBOL(of_device_is_available);
268
e679c5f4
SR
269/**
270 * of_get_parent - Get a node's parent if any
271 * @node: Node to get parent
272 *
273 * Returns a node pointer with refcount incremented, use
274 * of_node_put() on it when done.
275 */
276struct device_node *of_get_parent(const struct device_node *node)
277{
278 struct device_node *np;
279
280 if (!node)
281 return NULL;
282
283 read_lock(&devtree_lock);
284 np = of_node_get(node->parent);
285 read_unlock(&devtree_lock);
286 return np;
287}
288EXPORT_SYMBOL(of_get_parent);
d1cd355a 289
f4eb0107
ME
290/**
291 * of_get_next_parent - Iterate to a node's parent
292 * @node: Node to get parent of
293 *
294 * This is like of_get_parent() except that it drops the
295 * refcount on the passed node, making it suitable for iterating
296 * through a node's parents.
297 *
298 * Returns a node pointer with refcount incremented, use
299 * of_node_put() on it when done.
300 */
301struct device_node *of_get_next_parent(struct device_node *node)
302{
303 struct device_node *parent;
304
305 if (!node)
306 return NULL;
307
308 read_lock(&devtree_lock);
309 parent = of_node_get(node->parent);
310 of_node_put(node);
311 read_unlock(&devtree_lock);
312 return parent;
313}
314
d1cd355a
SR
315/**
316 * of_get_next_child - Iterate a node childs
317 * @node: parent node
318 * @prev: previous child of the parent node, or NULL to get first
319 *
320 * Returns a node pointer with refcount incremented, use
321 * of_node_put() on it when done.
322 */
323struct device_node *of_get_next_child(const struct device_node *node,
324 struct device_node *prev)
325{
326 struct device_node *next;
327
328 read_lock(&devtree_lock);
329 next = prev ? prev->sibling : node->child;
330 for (; next; next = next->sibling)
331 if (of_node_get(next))
332 break;
333 of_node_put(prev);
334 read_unlock(&devtree_lock);
335 return next;
336}
337EXPORT_SYMBOL(of_get_next_child);
1ef4d424
SR
338
339/**
340 * of_find_node_by_path - Find a node matching a full OF path
341 * @path: The full path to match
342 *
343 * Returns a node pointer with refcount incremented, use
344 * of_node_put() on it when done.
345 */
346struct device_node *of_find_node_by_path(const char *path)
347{
348 struct device_node *np = allnodes;
349
350 read_lock(&devtree_lock);
351 for (; np; np = np->allnext) {
352 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
353 && of_node_get(np))
354 break;
355 }
356 read_unlock(&devtree_lock);
357 return np;
358}
359EXPORT_SYMBOL(of_find_node_by_path);
360
361/**
362 * of_find_node_by_name - Find a node by its "name" property
363 * @from: The node to start searching from or NULL, the node
364 * you pass will not be searched, only the next one
365 * will; typically, you pass what the previous call
366 * returned. of_node_put() will be called on it
367 * @name: The name string to match against
368 *
369 * Returns a node pointer with refcount incremented, use
370 * of_node_put() on it when done.
371 */
372struct device_node *of_find_node_by_name(struct device_node *from,
373 const char *name)
374{
375 struct device_node *np;
376
377 read_lock(&devtree_lock);
378 np = from ? from->allnext : allnodes;
379 for (; np; np = np->allnext)
380 if (np->name && (of_node_cmp(np->name, name) == 0)
381 && of_node_get(np))
382 break;
383 of_node_put(from);
384 read_unlock(&devtree_lock);
385 return np;
386}
387EXPORT_SYMBOL(of_find_node_by_name);
388
389/**
390 * of_find_node_by_type - Find a node by its "device_type" property
391 * @from: The node to start searching from, or NULL to start searching
392 * the entire device tree. The node you pass will not be
393 * searched, only the next one will; typically, you pass
394 * what the previous call returned. of_node_put() will be
395 * called on from for you.
396 * @type: The type string to match against
397 *
398 * Returns a node pointer with refcount incremented, use
399 * of_node_put() on it when done.
400 */
401struct device_node *of_find_node_by_type(struct device_node *from,
402 const char *type)
403{
404 struct device_node *np;
405
406 read_lock(&devtree_lock);
407 np = from ? from->allnext : allnodes;
408 for (; np; np = np->allnext)
409 if (np->type && (of_node_cmp(np->type, type) == 0)
410 && of_node_get(np))
411 break;
412 of_node_put(from);
413 read_unlock(&devtree_lock);
414 return np;
415}
416EXPORT_SYMBOL(of_find_node_by_type);
417
418/**
419 * of_find_compatible_node - Find a node based on type and one of the
420 * tokens in its "compatible" property
421 * @from: The node to start searching from or NULL, the node
422 * you pass will not be searched, only the next one
423 * will; typically, you pass what the previous call
424 * returned. of_node_put() will be called on it
425 * @type: The type string to match "device_type" or NULL to ignore
426 * @compatible: The string to match to one of the tokens in the device
427 * "compatible" list.
428 *
429 * Returns a node pointer with refcount incremented, use
430 * of_node_put() on it when done.
431 */
432struct device_node *of_find_compatible_node(struct device_node *from,
433 const char *type, const char *compatible)
434{
435 struct device_node *np;
436
437 read_lock(&devtree_lock);
438 np = from ? from->allnext : allnodes;
439 for (; np; np = np->allnext) {
440 if (type
441 && !(np->type && (of_node_cmp(np->type, type) == 0)))
442 continue;
443 if (of_device_is_compatible(np, compatible) && of_node_get(np))
444 break;
445 }
446 of_node_put(from);
447 read_unlock(&devtree_lock);
448 return np;
449}
450EXPORT_SYMBOL(of_find_compatible_node);
283029d1 451
1e291b14
ME
452/**
453 * of_find_node_with_property - Find a node which has a property with
454 * the given name.
455 * @from: The node to start searching from or NULL, the node
456 * you pass will not be searched, only the next one
457 * will; typically, you pass what the previous call
458 * returned. of_node_put() will be called on it
459 * @prop_name: The name of the property to look for.
460 *
461 * Returns a node pointer with refcount incremented, use
462 * of_node_put() on it when done.
463 */
464struct device_node *of_find_node_with_property(struct device_node *from,
465 const char *prop_name)
466{
467 struct device_node *np;
468 struct property *pp;
469
470 read_lock(&devtree_lock);
471 np = from ? from->allnext : allnodes;
472 for (; np; np = np->allnext) {
473 for (pp = np->properties; pp != 0; pp = pp->next) {
474 if (of_prop_cmp(pp->name, prop_name) == 0) {
475 of_node_get(np);
476 goto out;
477 }
478 }
479 }
480out:
481 of_node_put(from);
482 read_unlock(&devtree_lock);
483 return np;
484}
485EXPORT_SYMBOL(of_find_node_with_property);
486
283029d1
GL
487/**
488 * of_match_node - Tell if an device_node has a matching of_match structure
489 * @matches: array of of device match structures to search in
490 * @node: the of device structure to match against
491 *
492 * Low level utility function used by device matching.
493 */
494const struct of_device_id *of_match_node(const struct of_device_id *matches,
495 const struct device_node *node)
496{
497 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
498 int match = 1;
499 if (matches->name[0])
500 match &= node->name
501 && !strcmp(matches->name, node->name);
502 if (matches->type[0])
503 match &= node->type
504 && !strcmp(matches->type, node->type);
505 if (matches->compatible[0])
506 match &= of_device_is_compatible(node,
507 matches->compatible);
508 if (match)
509 return matches;
510 matches++;
511 }
512 return NULL;
513}
514EXPORT_SYMBOL(of_match_node);
515
516/**
517 * of_find_matching_node - Find a node based on an of_device_id match
518 * table.
519 * @from: The node to start searching from or NULL, the node
520 * you pass will not be searched, only the next one
521 * will; typically, you pass what the previous call
522 * returned. of_node_put() will be called on it
523 * @matches: array of of device match structures to search in
524 *
525 * Returns a node pointer with refcount incremented, use
526 * of_node_put() on it when done.
527 */
528struct device_node *of_find_matching_node(struct device_node *from,
529 const struct of_device_id *matches)
530{
531 struct device_node *np;
532
533 read_lock(&devtree_lock);
534 np = from ? from->allnext : allnodes;
535 for (; np; np = np->allnext) {
536 if (of_match_node(matches, np) && of_node_get(np))
537 break;
538 }
539 of_node_put(from);
540 read_unlock(&devtree_lock);
541 return np;
542}
543EXPORT_SYMBOL(of_find_matching_node);
3f07af49
GL
544
545/**
546 * of_modalias_table: Table of explicit compatible ==> modalias mappings
547 *
548 * This table allows particulare compatible property values to be mapped
549 * to modalias strings. This is useful for busses which do not directly
550 * understand the OF device tree but are populated based on data contained
551 * within the device tree. SPI and I2C are the two current users of this
552 * table.
553 *
554 * In most cases, devices do not need to be listed in this table because
555 * the modalias value can be derived directly from the compatible table.
556 * However, if for any reason a value cannot be derived, then this table
557 * provides a method to override the implicit derivation.
558 *
559 * At the moment, a single table is used for all bus types because it is
560 * assumed that the data size is small and that the compatible values
561 * should already be distinct enough to differentiate between SPI, I2C
562 * and other devices.
563 */
564struct of_modalias_table {
565 char *of_device;
566 char *modalias;
567};
568static struct of_modalias_table of_modalias_table[] = {
4c3ed7d6 569 { "fsl,mcu-mpc8349emitx", "mcu-mpc8349emitx" },
3f1c6ebf 570 { "mmc-spi-slot", "mmc_spi" },
3f07af49
GL
571};
572
573/**
574 * of_modalias_node - Lookup appropriate modalias for a device node
575 * @node: pointer to a device tree node
576 * @modalias: Pointer to buffer that modalias value will be copied into
577 * @len: Length of modalias value
578 *
579 * Based on the value of the compatible property, this routine will determine
58f467ce
GL
580 * an appropriate modalias value for a particular device tree node. Two
581 * separate methods are attempted to derive a modalias value.
3f07af49
GL
582 *
583 * First method is to lookup the compatible value in of_modalias_table.
58f467ce
GL
584 * Second is to strip off the manufacturer prefix from the first
585 * compatible entry and use the remainder as modalias
3f07af49
GL
586 *
587 * This routine returns 0 on success
588 */
589int of_modalias_node(struct device_node *node, char *modalias, int len)
590{
591 int i, cplen;
592 const char *compatible;
593 const char *p;
594
595 /* 1. search for exception list entry */
596 for (i = 0; i < ARRAY_SIZE(of_modalias_table); i++) {
597 compatible = of_modalias_table[i].of_device;
598 if (!of_device_is_compatible(node, compatible))
599 continue;
600 strlcpy(modalias, of_modalias_table[i].modalias, len);
601 return 0;
602 }
603
604 compatible = of_get_property(node, "compatible", &cplen);
605 if (!compatible)
606 return -ENODEV;
607
58f467ce 608 /* 2. take first compatible entry and strip manufacturer */
3f07af49
GL
609 p = strchr(compatible, ',');
610 if (!p)
611 return -ENODEV;
612 p++;
613 strlcpy(modalias, p, len);
614 return 0;
615}
616EXPORT_SYMBOL_GPL(of_modalias_node);
617
89751a7c
JK
618/**
619 * of_find_node_by_phandle - Find a node given a phandle
620 * @handle: phandle of the node to find
621 *
622 * Returns a node pointer with refcount incremented, use
623 * of_node_put() on it when done.
624 */
625struct device_node *of_find_node_by_phandle(phandle handle)
626{
627 struct device_node *np;
628
629 read_lock(&devtree_lock);
630 for (np = allnodes; np; np = np->allnext)
631 if (np->phandle == handle)
632 break;
633 of_node_get(np);
634 read_unlock(&devtree_lock);
635 return np;
636}
637EXPORT_SYMBOL(of_find_node_by_phandle);
638
739649c5
GL
639/**
640 * of_parse_phandle - Resolve a phandle property to a device_node pointer
641 * @np: Pointer to device node holding phandle property
642 * @phandle_name: Name of property holding a phandle value
643 * @index: For properties holding a table of phandles, this is the index into
644 * the table
645 *
646 * Returns the device_node pointer with refcount incremented. Use
647 * of_node_put() on it when done.
648 */
649struct device_node *
650of_parse_phandle(struct device_node *np, const char *phandle_name, int index)
651{
652 const phandle *phandle;
653 int size;
654
655 phandle = of_get_property(np, phandle_name, &size);
656 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
657 return NULL;
658
659 return of_find_node_by_phandle(phandle[index]);
660}
661EXPORT_SYMBOL(of_parse_phandle);
662
64b60e09
AV
663/**
664 * of_parse_phandles_with_args - Find a node pointed by phandle in a list
665 * @np: pointer to a device tree node containing a list
666 * @list_name: property name that contains a list
667 * @cells_name: property name that specifies phandles' arguments count
668 * @index: index of a phandle to parse out
7736a3db
AV
669 * @out_node: optional pointer to device_node struct pointer (will be filled)
670 * @out_args: optional pointer to arguments pointer (will be filled)
64b60e09
AV
671 *
672 * This function is useful to parse lists of phandles and their arguments.
673 * Returns 0 on success and fills out_node and out_args, on error returns
674 * appropriate errno value.
675 *
676 * Example:
677 *
678 * phandle1: node1 {
679 * #list-cells = <2>;
680 * }
681 *
682 * phandle2: node2 {
683 * #list-cells = <1>;
684 * }
685 *
686 * node3 {
687 * list = <&phandle1 1 2 &phandle2 3>;
688 * }
689 *
690 * To get a device_node of the `node2' node you may call this:
691 * of_parse_phandles_with_args(node3, "list", "#list-cells", 2, &node2, &args);
692 */
693int of_parse_phandles_with_args(struct device_node *np, const char *list_name,
694 const char *cells_name, int index,
695 struct device_node **out_node,
696 const void **out_args)
697{
698 int ret = -EINVAL;
699 const u32 *list;
700 const u32 *list_end;
701 int size;
702 int cur_index = 0;
703 struct device_node *node = NULL;
7736a3db 704 const void *args = NULL;
64b60e09
AV
705
706 list = of_get_property(np, list_name, &size);
707 if (!list) {
708 ret = -ENOENT;
709 goto err0;
710 }
711 list_end = list + size / sizeof(*list);
712
713 while (list < list_end) {
714 const u32 *cells;
715 const phandle *phandle;
716
c1bb7c6d
AV
717 phandle = list++;
718 args = list;
64b60e09
AV
719
720 /* one cell hole in the list = <>; */
c1bb7c6d 721 if (!*phandle)
64b60e09 722 goto next;
64b60e09
AV
723
724 node = of_find_node_by_phandle(*phandle);
725 if (!node) {
726 pr_debug("%s: could not find phandle\n",
727 np->full_name);
728 goto err0;
729 }
730
731 cells = of_get_property(node, cells_name, &size);
732 if (!cells || size != sizeof(*cells)) {
733 pr_debug("%s: could not get %s for %s\n",
734 np->full_name, cells_name, node->full_name);
735 goto err1;
736 }
737
c1bb7c6d 738 list += *cells;
64b60e09
AV
739 if (list > list_end) {
740 pr_debug("%s: insufficient arguments length\n",
741 np->full_name);
742 goto err1;
743 }
744next:
745 if (cur_index == index)
746 break;
747
748 of_node_put(node);
749 node = NULL;
7736a3db 750 args = NULL;
64b60e09
AV
751 cur_index++;
752 }
753
754 if (!node) {
7736a3db
AV
755 /*
756 * args w/o node indicates that the loop above has stopped at
757 * the 'hole' cell. Report this differently.
758 */
759 if (args)
760 ret = -EEXIST;
761 else
762 ret = -ENOENT;
64b60e09
AV
763 goto err0;
764 }
765
7736a3db
AV
766 if (out_node)
767 *out_node = node;
768 if (out_args)
769 *out_args = args;
64b60e09
AV
770
771 return 0;
772err1:
773 of_node_put(node);
774err0:
775 pr_debug("%s failed with status %d\n", __func__, ret);
776 return ret;
777}
778EXPORT_SYMBOL(of_parse_phandles_with_args);
02af11b0
GL
779
780/**
781 * prom_add_property - Add a property to a node
782 */
783int prom_add_property(struct device_node *np, struct property *prop)
784{
785 struct property **next;
786 unsigned long flags;
787
788 prop->next = NULL;
789 write_lock_irqsave(&devtree_lock, flags);
790 next = &np->properties;
791 while (*next) {
792 if (strcmp(prop->name, (*next)->name) == 0) {
793 /* duplicate ! don't insert it */
794 write_unlock_irqrestore(&devtree_lock, flags);
795 return -1;
796 }
797 next = &(*next)->next;
798 }
799 *next = prop;
800 write_unlock_irqrestore(&devtree_lock, flags);
801
802#ifdef CONFIG_PROC_DEVICETREE
803 /* try to add to proc as well if it was initialized */
804 if (np->pde)
805 proc_device_tree_add_prop(np->pde, prop);
806#endif /* CONFIG_PROC_DEVICETREE */
807
808 return 0;
809}
810
811/**
812 * prom_remove_property - Remove a property from a node.
813 *
814 * Note that we don't actually remove it, since we have given out
815 * who-knows-how-many pointers to the data using get-property.
816 * Instead we just move the property to the "dead properties"
817 * list, so it won't be found any more.
818 */
819int prom_remove_property(struct device_node *np, struct property *prop)
820{
821 struct property **next;
822 unsigned long flags;
823 int found = 0;
824
825 write_lock_irqsave(&devtree_lock, flags);
826 next = &np->properties;
827 while (*next) {
828 if (*next == prop) {
829 /* found the node */
830 *next = prop->next;
831 prop->next = np->deadprops;
832 np->deadprops = prop;
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 remove the proc node as well */
845 if (np->pde)
846 proc_device_tree_remove_prop(np->pde, prop);
847#endif /* CONFIG_PROC_DEVICETREE */
848
849 return 0;
850}
851
852/*
853 * prom_update_property - Update a property in a node.
854 *
855 * Note that we don't actually remove it, since we have given out
856 * who-knows-how-many pointers to the data using get-property.
857 * Instead we just move the property to the "dead properties" list,
858 * and add the new property to the property list
859 */
860int prom_update_property(struct device_node *np,
861 struct property *newprop,
862 struct property *oldprop)
863{
864 struct property **next;
865 unsigned long flags;
866 int found = 0;
867
868 write_lock_irqsave(&devtree_lock, flags);
869 next = &np->properties;
870 while (*next) {
871 if (*next == oldprop) {
872 /* found the node */
873 newprop->next = oldprop->next;
874 *next = newprop;
875 oldprop->next = np->deadprops;
876 np->deadprops = oldprop;
877 found = 1;
878 break;
879 }
880 next = &(*next)->next;
881 }
882 write_unlock_irqrestore(&devtree_lock, flags);
883
884 if (!found)
885 return -ENODEV;
886
887#ifdef CONFIG_PROC_DEVICETREE
888 /* try to add to proc as well if it was initialized */
889 if (np->pde)
890 proc_device_tree_update_prop(np->pde, newprop, oldprop);
891#endif /* CONFIG_PROC_DEVICETREE */
892
893 return 0;
894}
fcdeb7fe
GL
895
896#if defined(CONFIG_OF_DYNAMIC)
897/*
898 * Support for dynamic device trees.
899 *
900 * On some platforms, the device tree can be manipulated at runtime.
901 * The routines in this section support adding, removing and changing
902 * device tree nodes.
903 */
904
905/**
906 * of_attach_node - Plug a device node into the tree and global list.
907 */
908void of_attach_node(struct device_node *np)
909{
910 unsigned long flags;
911
912 write_lock_irqsave(&devtree_lock, flags);
913 np->sibling = np->parent->child;
914 np->allnext = allnodes;
915 np->parent->child = np;
916 allnodes = np;
917 write_unlock_irqrestore(&devtree_lock, flags);
918}
919
920/**
921 * of_detach_node - "Unplug" a node from the device tree.
922 *
923 * The caller must hold a reference to the node. The memory associated with
924 * the node is not freed until its refcount goes to zero.
925 */
926void of_detach_node(struct device_node *np)
927{
928 struct device_node *parent;
929 unsigned long flags;
930
931 write_lock_irqsave(&devtree_lock, flags);
932
933 parent = np->parent;
934 if (!parent)
935 goto out_unlock;
936
937 if (allnodes == np)
938 allnodes = np->allnext;
939 else {
940 struct device_node *prev;
941 for (prev = allnodes;
942 prev->allnext != np;
943 prev = prev->allnext)
944 ;
945 prev->allnext = np->allnext;
946 }
947
948 if (parent->child == np)
949 parent->child = np->sibling;
950 else {
951 struct device_node *prevsib;
952 for (prevsib = np->parent->child;
953 prevsib->sibling != np;
954 prevsib = prevsib->sibling)
955 ;
956 prevsib->sibling = np->sibling;
957 }
958
959 of_node_set_flag(np, OF_DETACHED);
960
961out_unlock:
962 write_unlock_irqrestore(&devtree_lock, flags);
963}
964#endif /* defined(CONFIG_OF_DYNAMIC) */
965