ALSA: hda - Fix beep frequency on IDT 92HD73xx and 92HD71Bxx codecs
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / of / base.c
blobb5ad9740d8b21a6eb6b3e3f3bd1586e8a5645426
1 /*
2 * Procedures for creating, accessing and interpreting the device tree.
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
7 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8 * {engebret|bergner}@us.ibm.com
10 * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
12 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
13 * Grant Likely.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
20 #include <linux/module.h>
21 #include <linux/of.h>
22 #include <linux/spinlock.h>
23 #include <linux/slab.h>
24 #include <linux/proc_fs.h>
26 struct device_node *allnodes;
27 struct device_node *of_chosen;
29 /* use when traversing tree through the allnext, child, sibling,
30 * or parent members of struct device_node.
32 DEFINE_RWLOCK(devtree_lock);
34 int of_n_addr_cells(struct device_node *np)
36 const int *ip;
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;
48 EXPORT_SYMBOL(of_n_addr_cells);
50 int of_n_size_cells(struct device_node *np)
52 const int *ip;
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;
64 EXPORT_SYMBOL(of_n_size_cells);
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
72 * Returns node.
74 struct device_node *of_node_get(struct device_node *node)
76 if (node)
77 kref_get(&node->kref);
78 return node;
80 EXPORT_SYMBOL(of_node_get);
82 static inline struct device_node *kref_to_device_node(struct kref *kref)
84 return container_of(kref, struct device_node, kref);
87 /**
88 * of_node_release - release a dynamically allocated node
89 * @kref: kref element of the node to be released
91 * In of_node_put() this function is passed to kref_put()
92 * as the destructor.
94 static void of_node_release(struct kref *kref)
96 struct device_node *node = kref_to_device_node(kref);
97 struct property *prop = node->properties;
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;
107 if (!of_node_check_flag(node, OF_DYNAMIC))
108 return;
110 while (prop) {
111 struct property *next = prop->next;
112 kfree(prop->name);
113 kfree(prop->value);
114 kfree(prop);
115 prop = next;
117 if (!prop) {
118 prop = node->deadprops;
119 node->deadprops = NULL;
122 kfree(node->full_name);
123 kfree(node->data);
124 kfree(node);
128 * of_node_put - Decrement refcount of a node
129 * @node: Node to dec refcount, NULL is supported to
130 * simplify writing of callers
133 void of_node_put(struct device_node *node)
135 if (node)
136 kref_put(&node->kref, of_node_release);
138 EXPORT_SYMBOL(of_node_put);
139 #endif /* !CONFIG_SPARC */
141 struct property *of_find_property(const struct device_node *np,
142 const char *name,
143 int *lenp)
145 struct property *pp;
147 if (!np)
148 return NULL;
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;
158 read_unlock(&devtree_lock);
160 return pp;
162 EXPORT_SYMBOL(of_find_property);
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
169 * Returns a node pointer with refcount incremented, use
170 * of_node_put() on it when done.
172 struct device_node *of_find_all_nodes(struct device_node *prev)
174 struct device_node *np;
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;
185 EXPORT_SYMBOL(of_find_all_nodes);
188 * Find a property with a given name for a given node
189 * and return the value.
191 const void *of_get_property(const struct device_node *np, const char *name,
192 int *lenp)
194 struct property *pp = of_find_property(np, name, lenp);
196 return pp ? pp->value : NULL;
198 EXPORT_SYMBOL(of_get_property);
200 /** Checks if the given "compat" string matches one of the strings in
201 * the device's "compatible" property
203 int of_device_is_compatible(const struct device_node *device,
204 const char *compat)
206 const char* cp;
207 int cplen, l;
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;
220 return 0;
222 EXPORT_SYMBOL(of_device_is_compatible);
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.
228 * Returns true if the root node has the given value in its
229 * compatible property.
231 int of_machine_is_compatible(const char *compat)
233 struct device_node *root;
234 int rc = 0;
236 root = of_find_node_by_path("/");
237 if (root) {
238 rc = of_device_is_compatible(root, compat);
239 of_node_put(root);
241 return rc;
243 EXPORT_SYMBOL(of_machine_is_compatible);
246 * of_device_is_available - check if a device is available for use
248 * @device: Node to check for availability
250 * Returns 1 if the status property is absent or set to "okay" or "ok",
251 * 0 otherwise
253 int of_device_is_available(const struct device_node *device)
255 const char *status;
256 int statlen;
258 status = of_get_property(device, "status", &statlen);
259 if (status == NULL)
260 return 1;
262 if (statlen > 0) {
263 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
264 return 1;
267 return 0;
269 EXPORT_SYMBOL(of_device_is_available);
272 * of_get_parent - Get a node's parent if any
273 * @node: Node to get parent
275 * Returns a node pointer with refcount incremented, use
276 * of_node_put() on it when done.
278 struct device_node *of_get_parent(const struct device_node *node)
280 struct device_node *np;
282 if (!node)
283 return NULL;
285 read_lock(&devtree_lock);
286 np = of_node_get(node->parent);
287 read_unlock(&devtree_lock);
288 return np;
290 EXPORT_SYMBOL(of_get_parent);
293 * of_get_next_parent - Iterate to a node's parent
294 * @node: Node to get parent of
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.
300 * Returns a node pointer with refcount incremented, use
301 * of_node_put() on it when done.
303 struct device_node *of_get_next_parent(struct device_node *node)
305 struct device_node *parent;
307 if (!node)
308 return NULL;
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;
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
322 * Returns a node pointer with refcount incremented, use
323 * of_node_put() on it when done.
325 struct device_node *of_get_next_child(const struct device_node *node,
326 struct device_node *prev)
328 struct device_node *next;
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;
339 EXPORT_SYMBOL(of_get_next_child);
342 * of_find_node_by_path - Find a node matching a full OF path
343 * @path: The full path to match
345 * Returns a node pointer with refcount incremented, use
346 * of_node_put() on it when done.
348 struct device_node *of_find_node_by_path(const char *path)
350 struct device_node *np = allnodes;
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;
358 read_unlock(&devtree_lock);
359 return np;
361 EXPORT_SYMBOL(of_find_node_by_path);
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
371 * Returns a node pointer with refcount incremented, use
372 * of_node_put() on it when done.
374 struct device_node *of_find_node_by_name(struct device_node *from,
375 const char *name)
377 struct device_node *np;
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;
389 EXPORT_SYMBOL(of_find_node_by_name);
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
400 * Returns a node pointer with refcount incremented, use
401 * of_node_put() on it when done.
403 struct device_node *of_find_node_by_type(struct device_node *from,
404 const char *type)
406 struct device_node *np;
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;
418 EXPORT_SYMBOL(of_find_node_by_type);
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.
431 * Returns a node pointer with refcount incremented, use
432 * of_node_put() on it when done.
434 struct device_node *of_find_compatible_node(struct device_node *from,
435 const char *type, const char *compatible)
437 struct device_node *np;
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;
448 of_node_put(from);
449 read_unlock(&devtree_lock);
450 return np;
452 EXPORT_SYMBOL(of_find_compatible_node);
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.
463 * Returns a node pointer with refcount incremented, use
464 * of_node_put() on it when done.
466 struct device_node *of_find_node_with_property(struct device_node *from,
467 const char *prop_name)
469 struct device_node *np;
470 struct property *pp;
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;
482 out:
483 of_node_put(from);
484 read_unlock(&devtree_lock);
485 return np;
487 EXPORT_SYMBOL(of_find_node_with_property);
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
494 * Low level utility function used by device matching.
496 const struct of_device_id *of_match_node(const struct of_device_id *matches,
497 const struct device_node *node)
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++;
514 return NULL;
516 EXPORT_SYMBOL(of_match_node);
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
527 * Returns a node pointer with refcount incremented, use
528 * of_node_put() on it when done.
530 struct device_node *of_find_matching_node(struct device_node *from,
531 const struct of_device_id *matches)
533 struct device_node *np;
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;
541 of_node_put(from);
542 read_unlock(&devtree_lock);
543 return np;
545 EXPORT_SYMBOL(of_find_matching_node);
548 * of_modalias_table: Table of explicit compatible ==> modalias mappings
550 * This table allows particulare compatible property values to be mapped
551 * to modalias strings. This is useful for busses which do not directly
552 * understand the OF device tree but are populated based on data contained
553 * within the device tree. SPI and I2C are the two current users of this
554 * table.
556 * In most cases, devices do not need to be listed in this table because
557 * the modalias value can be derived directly from the compatible table.
558 * However, if for any reason a value cannot be derived, then this table
559 * provides a method to override the implicit derivation.
561 * At the moment, a single table is used for all bus types because it is
562 * assumed that the data size is small and that the compatible values
563 * should already be distinct enough to differentiate between SPI, I2C
564 * and other devices.
566 struct of_modalias_table {
567 char *of_device;
568 char *modalias;
570 static struct of_modalias_table of_modalias_table[] = {
571 { "fsl,mcu-mpc8349emitx", "mcu-mpc8349emitx" },
572 { "mmc-spi-slot", "mmc_spi" },
576 * of_modalias_node - Lookup appropriate modalias for a device node
577 * @node: pointer to a device tree node
578 * @modalias: Pointer to buffer that modalias value will be copied into
579 * @len: Length of modalias value
581 * Based on the value of the compatible property, this routine will determine
582 * an appropriate modalias value for a particular device tree node. Two
583 * separate methods are attempted to derive a modalias value.
585 * First method is to lookup the compatible value in of_modalias_table.
586 * Second is to strip off the manufacturer prefix from the first
587 * compatible entry and use the remainder as modalias
589 * This routine returns 0 on success
591 int of_modalias_node(struct device_node *node, char *modalias, int len)
593 int i, cplen;
594 const char *compatible;
595 const char *p;
597 /* 1. search for exception list entry */
598 for (i = 0; i < ARRAY_SIZE(of_modalias_table); i++) {
599 compatible = of_modalias_table[i].of_device;
600 if (!of_device_is_compatible(node, compatible))
601 continue;
602 strlcpy(modalias, of_modalias_table[i].modalias, len);
603 return 0;
606 compatible = of_get_property(node, "compatible", &cplen);
607 if (!compatible)
608 return -ENODEV;
610 /* 2. take first compatible entry and strip manufacturer */
611 p = strchr(compatible, ',');
612 if (!p)
613 return -ENODEV;
614 p++;
615 strlcpy(modalias, p, len);
616 return 0;
618 EXPORT_SYMBOL_GPL(of_modalias_node);
621 * of_find_node_by_phandle - Find a node given a phandle
622 * @handle: phandle of the node to find
624 * Returns a node pointer with refcount incremented, use
625 * of_node_put() on it when done.
627 struct device_node *of_find_node_by_phandle(phandle handle)
629 struct device_node *np;
631 read_lock(&devtree_lock);
632 for (np = allnodes; np; np = np->allnext)
633 if (np->phandle == handle)
634 break;
635 of_node_get(np);
636 read_unlock(&devtree_lock);
637 return np;
639 EXPORT_SYMBOL(of_find_node_by_phandle);
642 * of_parse_phandle - Resolve a phandle property to a device_node pointer
643 * @np: Pointer to device node holding phandle property
644 * @phandle_name: Name of property holding a phandle value
645 * @index: For properties holding a table of phandles, this is the index into
646 * the table
648 * Returns the device_node pointer with refcount incremented. Use
649 * of_node_put() on it when done.
651 struct device_node *
652 of_parse_phandle(struct device_node *np, const char *phandle_name, int index)
654 const phandle *phandle;
655 int size;
657 phandle = of_get_property(np, phandle_name, &size);
658 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
659 return NULL;
661 return of_find_node_by_phandle(phandle[index]);
663 EXPORT_SYMBOL(of_parse_phandle);
666 * of_parse_phandles_with_args - Find a node pointed by phandle in a list
667 * @np: pointer to a device tree node containing a list
668 * @list_name: property name that contains a list
669 * @cells_name: property name that specifies phandles' arguments count
670 * @index: index of a phandle to parse out
671 * @out_node: optional pointer to device_node struct pointer (will be filled)
672 * @out_args: optional pointer to arguments pointer (will be filled)
674 * This function is useful to parse lists of phandles and their arguments.
675 * Returns 0 on success and fills out_node and out_args, on error returns
676 * appropriate errno value.
678 * Example:
680 * phandle1: node1 {
681 * #list-cells = <2>;
684 * phandle2: node2 {
685 * #list-cells = <1>;
688 * node3 {
689 * list = <&phandle1 1 2 &phandle2 3>;
692 * To get a device_node of the `node2' node you may call this:
693 * of_parse_phandles_with_args(node3, "list", "#list-cells", 2, &node2, &args);
695 int of_parse_phandles_with_args(struct device_node *np, const char *list_name,
696 const char *cells_name, int index,
697 struct device_node **out_node,
698 const void **out_args)
700 int ret = -EINVAL;
701 const __be32 *list;
702 const __be32 *list_end;
703 int size;
704 int cur_index = 0;
705 struct device_node *node = NULL;
706 const void *args = NULL;
708 list = of_get_property(np, list_name, &size);
709 if (!list) {
710 ret = -ENOENT;
711 goto err0;
713 list_end = list + size / sizeof(*list);
715 while (list < list_end) {
716 const __be32 *cells;
717 const phandle *phandle;
719 phandle = list++;
720 args = list;
722 /* one cell hole in the list = <>; */
723 if (!*phandle)
724 goto next;
726 node = of_find_node_by_phandle(*phandle);
727 if (!node) {
728 pr_debug("%s: could not find phandle\n",
729 np->full_name);
730 goto err0;
733 cells = of_get_property(node, cells_name, &size);
734 if (!cells || size != sizeof(*cells)) {
735 pr_debug("%s: could not get %s for %s\n",
736 np->full_name, cells_name, node->full_name);
737 goto err1;
740 list += be32_to_cpup(cells);
741 if (list > list_end) {
742 pr_debug("%s: insufficient arguments length\n",
743 np->full_name);
744 goto err1;
746 next:
747 if (cur_index == index)
748 break;
750 of_node_put(node);
751 node = NULL;
752 args = NULL;
753 cur_index++;
756 if (!node) {
758 * args w/o node indicates that the loop above has stopped at
759 * the 'hole' cell. Report this differently.
761 if (args)
762 ret = -EEXIST;
763 else
764 ret = -ENOENT;
765 goto err0;
768 if (out_node)
769 *out_node = node;
770 if (out_args)
771 *out_args = args;
773 return 0;
774 err1:
775 of_node_put(node);
776 err0:
777 pr_debug("%s failed with status %d\n", __func__, ret);
778 return ret;
780 EXPORT_SYMBOL(of_parse_phandles_with_args);
783 * prom_add_property - Add a property to a node
785 int prom_add_property(struct device_node *np, struct property *prop)
787 struct property **next;
788 unsigned long flags;
790 prop->next = NULL;
791 write_lock_irqsave(&devtree_lock, flags);
792 next = &np->properties;
793 while (*next) {
794 if (strcmp(prop->name, (*next)->name) == 0) {
795 /* duplicate ! don't insert it */
796 write_unlock_irqrestore(&devtree_lock, flags);
797 return -1;
799 next = &(*next)->next;
801 *next = prop;
802 write_unlock_irqrestore(&devtree_lock, flags);
804 #ifdef CONFIG_PROC_DEVICETREE
805 /* try to add to proc as well if it was initialized */
806 if (np->pde)
807 proc_device_tree_add_prop(np->pde, prop);
808 #endif /* CONFIG_PROC_DEVICETREE */
810 return 0;
814 * prom_remove_property - Remove a property from a node.
816 * Note that we don't actually remove it, since we have given out
817 * who-knows-how-many pointers to the data using get-property.
818 * Instead we just move the property to the "dead properties"
819 * list, so it won't be found any more.
821 int prom_remove_property(struct device_node *np, struct property *prop)
823 struct property **next;
824 unsigned long flags;
825 int found = 0;
827 write_lock_irqsave(&devtree_lock, flags);
828 next = &np->properties;
829 while (*next) {
830 if (*next == prop) {
831 /* found the node */
832 *next = prop->next;
833 prop->next = np->deadprops;
834 np->deadprops = prop;
835 found = 1;
836 break;
838 next = &(*next)->next;
840 write_unlock_irqrestore(&devtree_lock, flags);
842 if (!found)
843 return -ENODEV;
845 #ifdef CONFIG_PROC_DEVICETREE
846 /* try to remove the proc node as well */
847 if (np->pde)
848 proc_device_tree_remove_prop(np->pde, prop);
849 #endif /* CONFIG_PROC_DEVICETREE */
851 return 0;
855 * prom_update_property - Update a property in a node.
857 * Note that we don't actually remove it, since we have given out
858 * who-knows-how-many pointers to the data using get-property.
859 * Instead we just move the property to the "dead properties" list,
860 * and add the new property to the property list
862 int prom_update_property(struct device_node *np,
863 struct property *newprop,
864 struct property *oldprop)
866 struct property **next;
867 unsigned long flags;
868 int found = 0;
870 write_lock_irqsave(&devtree_lock, flags);
871 next = &np->properties;
872 while (*next) {
873 if (*next == oldprop) {
874 /* found the node */
875 newprop->next = oldprop->next;
876 *next = newprop;
877 oldprop->next = np->deadprops;
878 np->deadprops = oldprop;
879 found = 1;
880 break;
882 next = &(*next)->next;
884 write_unlock_irqrestore(&devtree_lock, flags);
886 if (!found)
887 return -ENODEV;
889 #ifdef CONFIG_PROC_DEVICETREE
890 /* try to add to proc as well if it was initialized */
891 if (np->pde)
892 proc_device_tree_update_prop(np->pde, newprop, oldprop);
893 #endif /* CONFIG_PROC_DEVICETREE */
895 return 0;
898 #if defined(CONFIG_OF_DYNAMIC)
900 * Support for dynamic device trees.
902 * On some platforms, the device tree can be manipulated at runtime.
903 * The routines in this section support adding, removing and changing
904 * device tree nodes.
908 * of_attach_node - Plug a device node into the tree and global list.
910 void of_attach_node(struct device_node *np)
912 unsigned long flags;
914 write_lock_irqsave(&devtree_lock, flags);
915 np->sibling = np->parent->child;
916 np->allnext = allnodes;
917 np->parent->child = np;
918 allnodes = np;
919 write_unlock_irqrestore(&devtree_lock, flags);
923 * of_detach_node - "Unplug" a node from the device tree.
925 * The caller must hold a reference to the node. The memory associated with
926 * the node is not freed until its refcount goes to zero.
928 void of_detach_node(struct device_node *np)
930 struct device_node *parent;
931 unsigned long flags;
933 write_lock_irqsave(&devtree_lock, flags);
935 parent = np->parent;
936 if (!parent)
937 goto out_unlock;
939 if (allnodes == np)
940 allnodes = np->allnext;
941 else {
942 struct device_node *prev;
943 for (prev = allnodes;
944 prev->allnext != np;
945 prev = prev->allnext)
947 prev->allnext = np->allnext;
950 if (parent->child == np)
951 parent->child = np->sibling;
952 else {
953 struct device_node *prevsib;
954 for (prevsib = np->parent->child;
955 prevsib->sibling != np;
956 prevsib = prevsib->sibling)
958 prevsib->sibling = np->sibling;
961 of_node_set_flag(np, OF_DETACHED);
963 out_unlock:
964 write_unlock_irqrestore(&devtree_lock, flags);
966 #endif /* defined(CONFIG_OF_DYNAMIC) */