2 * Support for dynamic device trees.
4 * On some platforms, the device tree can be manipulated at runtime.
5 * The routines in this section support adding, removing and changing
10 #include <linux/spinlock.h>
11 #include <linux/slab.h>
12 #include <linux/string.h>
13 #include <linux/proc_fs.h>
15 #include "of_private.h"
18 * of_node_get() - Increment refcount of a node
19 * @node: Node to inc refcount, NULL is supported to simplify writing of
24 struct device_node *of_node_get(struct device_node *node)
27 kobject_get(&node->kobj);
30 EXPORT_SYMBOL(of_node_get);
33 * of_node_put() - Decrement refcount of a node
34 * @node: Node to dec refcount, NULL is supported to simplify writing of
37 void of_node_put(struct device_node *node)
40 kobject_put(&node->kobj);
42 EXPORT_SYMBOL(of_node_put);
44 static void of_node_remove(struct device_node *np)
48 BUG_ON(!of_node_is_initialized(np));
50 /* only remove properties if on sysfs */
51 if (of_node_is_attached(np)) {
52 for_each_property_of_node(np, pp)
53 sysfs_remove_bin_file(&np->kobj, &pp->attr);
54 kobject_del(&np->kobj);
57 /* finally remove the kobj_init ref */
61 static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
63 int of_reconfig_notifier_register(struct notifier_block *nb)
65 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
67 EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
69 int of_reconfig_notifier_unregister(struct notifier_block *nb)
71 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
73 EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
75 int of_reconfig_notify(unsigned long action, void *p)
79 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
80 return notifier_to_errno(rc);
83 int of_property_notify(int action, struct device_node *np,
84 struct property *prop)
86 struct of_prop_reconfig pr;
88 /* only call notifiers if the node is attached */
89 if (!of_node_is_attached(np))
94 return of_reconfig_notify(action, &pr);
98 * of_attach_node() - Plug a device node into the tree and global list.
100 int of_attach_node(struct device_node *np)
105 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
109 raw_spin_lock_irqsave(&devtree_lock, flags);
110 np->sibling = np->parent->child;
111 np->allnext = np->parent->allnext;
112 np->parent->allnext = np;
113 np->parent->child = np;
114 of_node_clear_flag(np, OF_DETACHED);
115 raw_spin_unlock_irqrestore(&devtree_lock, flags);
122 * of_detach_node() - "Unplug" a node from the device tree.
124 * The caller must hold a reference to the node. The memory associated with
125 * the node is not freed until its refcount goes to zero.
127 int of_detach_node(struct device_node *np)
129 struct device_node *parent;
133 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
137 raw_spin_lock_irqsave(&devtree_lock, flags);
139 if (of_node_check_flag(np, OF_DETACHED)) {
140 /* someone already detached it */
141 raw_spin_unlock_irqrestore(&devtree_lock, flags);
147 raw_spin_unlock_irqrestore(&devtree_lock, flags);
151 if (of_allnodes == np)
152 of_allnodes = np->allnext;
154 struct device_node *prev;
155 for (prev = of_allnodes;
157 prev = prev->allnext)
159 prev->allnext = np->allnext;
162 if (parent->child == np)
163 parent->child = np->sibling;
165 struct device_node *prevsib;
166 for (prevsib = np->parent->child;
167 prevsib->sibling != np;
168 prevsib = prevsib->sibling)
170 prevsib->sibling = np->sibling;
173 of_node_set_flag(np, OF_DETACHED);
174 raw_spin_unlock_irqrestore(&devtree_lock, flags);
181 * of_node_release() - release a dynamically allocated node
182 * @kref: kref element of the node to be released
184 * In of_node_put() this function is passed to kref_put() as the destructor.
186 void of_node_release(struct kobject *kobj)
188 struct device_node *node = kobj_to_device_node(kobj);
189 struct property *prop = node->properties;
191 /* We should never be releasing nodes that haven't been detached. */
192 if (!of_node_check_flag(node, OF_DETACHED)) {
193 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
198 if (!of_node_check_flag(node, OF_DYNAMIC))
202 struct property *next = prop->next;
209 prop = node->deadprops;
210 node->deadprops = NULL;
213 kfree(node->full_name);