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 void __of_detach_node_sysfs(struct device_node *np)
48 BUG_ON(!of_node_is_initialized(np));
52 /* only remove properties if on sysfs */
53 if (of_node_is_attached(np)) {
54 for_each_property_of_node(np, pp)
55 sysfs_remove_bin_file(&np->kobj, &pp->attr);
56 kobject_del(&np->kobj);
59 /* finally remove the kobj_init ref */
63 static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
65 int of_reconfig_notifier_register(struct notifier_block *nb)
67 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
69 EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
71 int of_reconfig_notifier_unregister(struct notifier_block *nb)
73 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
75 EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
77 int of_reconfig_notify(unsigned long action, void *p)
81 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
82 return notifier_to_errno(rc);
85 int of_property_notify(int action, struct device_node *np,
86 struct property *prop)
88 struct of_prop_reconfig pr;
90 /* only call notifiers if the node is attached */
91 if (!of_node_is_attached(np))
96 return of_reconfig_notify(action, &pr);
99 void __of_attach_node(struct device_node *np)
102 np->sibling = np->parent->child;
103 np->allnext = np->parent->allnext;
104 np->parent->allnext = np;
105 np->parent->child = np;
106 of_node_clear_flag(np, OF_DETACHED);
110 * of_attach_node() - Plug a device node into the tree and global list.
112 int of_attach_node(struct device_node *np)
117 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
121 mutex_lock(&of_mutex);
122 raw_spin_lock_irqsave(&devtree_lock, flags);
123 __of_attach_node(np);
124 raw_spin_unlock_irqrestore(&devtree_lock, flags);
126 __of_attach_node_sysfs(np);
127 mutex_unlock(&of_mutex);
131 void __of_detach_node(struct device_node *np)
133 struct device_node *parent;
135 if (WARN_ON(of_node_check_flag(np, OF_DETACHED)))
139 if (WARN_ON(!parent))
142 if (of_allnodes == np)
143 of_allnodes = np->allnext;
145 struct device_node *prev;
146 for (prev = of_allnodes;
148 prev = prev->allnext)
150 prev->allnext = np->allnext;
153 if (parent->child == np)
154 parent->child = np->sibling;
156 struct device_node *prevsib;
157 for (prevsib = np->parent->child;
158 prevsib->sibling != np;
159 prevsib = prevsib->sibling)
161 prevsib->sibling = np->sibling;
164 of_node_set_flag(np, OF_DETACHED);
168 * of_detach_node() - "Unplug" a node from the device tree.
170 * The caller must hold a reference to the node. The memory associated with
171 * the node is not freed until its refcount goes to zero.
173 int of_detach_node(struct device_node *np)
178 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
182 mutex_lock(&of_mutex);
183 raw_spin_lock_irqsave(&devtree_lock, flags);
184 __of_detach_node(np);
185 raw_spin_unlock_irqrestore(&devtree_lock, flags);
187 __of_detach_node_sysfs(np);
188 mutex_unlock(&of_mutex);
193 * of_node_release() - release a dynamically allocated node
194 * @kref: kref element of the node to be released
196 * In of_node_put() this function is passed to kref_put() as the destructor.
198 void of_node_release(struct kobject *kobj)
200 struct device_node *node = kobj_to_device_node(kobj);
201 struct property *prop = node->properties;
203 /* We should never be releasing nodes that haven't been detached. */
204 if (!of_node_check_flag(node, OF_DETACHED)) {
205 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
210 if (!of_node_check_flag(node, OF_DYNAMIC))
214 struct property *next = prop->next;
221 prop = node->deadprops;
222 node->deadprops = NULL;
225 kfree(node->full_name);
231 * __of_prop_dup - Copy a property dynamically.
232 * @prop: Property to copy
233 * @allocflags: Allocation flags (typically pass GFP_KERNEL)
235 * Copy a property by dynamically allocating the memory of both the
236 * property stucture and the property name & contents. The property's
237 * flags have the OF_DYNAMIC bit set so that we can differentiate between
238 * dynamically allocated properties and not.
239 * Returns the newly allocated property or NULL on out of memory error.
241 struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags)
243 struct property *new;
245 new = kzalloc(sizeof(*new), allocflags);
250 * NOTE: There is no check for zero length value.
251 * In case of a boolean property This will allocate a value
252 * of zero bytes. We do this to work around the use
253 * of of_get_property() calls on boolean values.
255 new->name = kstrdup(prop->name, allocflags);
256 new->value = kmemdup(prop->value, prop->length, allocflags);
257 new->length = prop->length;
258 if (!new->name || !new->value)
261 /* mark the property as dynamic */
262 of_property_set_flag(new, OF_DYNAMIC);
274 * __of_node_alloc() - Create an empty device node dynamically.
275 * @full_name: Full name of the new device node
276 * @allocflags: Allocation flags (typically pass GFP_KERNEL)
278 * Create an empty device tree node, suitable for further modification.
279 * The node data are dynamically allocated and all the node flags
280 * have the OF_DYNAMIC & OF_DETACHED bits set.
281 * Returns the newly allocated node or NULL on out of memory error.
283 struct device_node *__of_node_alloc(const char *full_name, gfp_t allocflags)
285 struct device_node *node;
287 node = kzalloc(sizeof(*node), allocflags);
291 node->full_name = kstrdup(full_name, allocflags);
292 of_node_set_flag(node, OF_DYNAMIC);
293 of_node_set_flag(node, OF_DETACHED);
294 if (!node->full_name)
302 kfree(node->full_name);