blob: c875787fa3948df67972310f81064fc53b0607e0 [file] [log] [blame]
Grant Likely6afc0dc2014-06-26 15:40:48 +01001/*
2 * Support for dynamic device trees.
3 *
4 * On some platforms, the device tree can be manipulated at runtime.
5 * The routines in this section support adding, removing and changing
6 * device tree nodes.
7 */
8
9#include <linux/of.h>
10#include <linux/spinlock.h>
11#include <linux/slab.h>
12#include <linux/string.h>
13#include <linux/proc_fs.h>
14
15#include "of_private.h"
16
17/**
18 * of_node_get() - Increment refcount of a node
19 * @node: Node to inc refcount, NULL is supported to simplify writing of
20 * callers
21 *
22 * Returns node.
23 */
24struct device_node *of_node_get(struct device_node *node)
25{
26 if (node)
27 kobject_get(&node->kobj);
28 return node;
29}
30EXPORT_SYMBOL(of_node_get);
31
32/**
33 * of_node_put() - Decrement refcount of a node
34 * @node: Node to dec refcount, NULL is supported to simplify writing of
35 * callers
36 */
37void of_node_put(struct device_node *node)
38{
39 if (node)
40 kobject_put(&node->kobj);
41}
42EXPORT_SYMBOL(of_node_put);
43
Grant Likely8a2b22a2014-07-23 17:05:06 -060044void __of_detach_node_sysfs(struct device_node *np)
Grant Likely6afc0dc2014-06-26 15:40:48 +010045{
46 struct property *pp;
47
48 BUG_ON(!of_node_is_initialized(np));
Grant Likely8a2b22a2014-07-23 17:05:06 -060049 if (!of_kset)
50 return;
Grant Likely6afc0dc2014-06-26 15:40:48 +010051
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);
57 }
58
59 /* finally remove the kobj_init ref */
60 of_node_put(np);
61}
62
63static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
64
65int of_reconfig_notifier_register(struct notifier_block *nb)
66{
67 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
68}
69EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
70
71int of_reconfig_notifier_unregister(struct notifier_block *nb)
72{
73 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
74}
75EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
76
77int of_reconfig_notify(unsigned long action, void *p)
78{
79 int rc;
80
81 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
82 return notifier_to_errno(rc);
83}
84
85int of_property_notify(int action, struct device_node *np,
86 struct property *prop)
87{
88 struct of_prop_reconfig pr;
89
90 /* only call notifiers if the node is attached */
91 if (!of_node_is_attached(np))
92 return 0;
93
94 pr.dn = np;
95 pr.prop = prop;
96 return of_reconfig_notify(action, &pr);
97}
98
Pantelis Antonioud8c50082014-07-04 19:58:46 +030099void __of_attach_node(struct device_node *np)
100{
101 np->sibling = np->parent->child;
102 np->allnext = np->parent->allnext;
103 np->parent->allnext = np;
104 np->parent->child = np;
105 of_node_clear_flag(np, OF_DETACHED);
106}
107
Grant Likely6afc0dc2014-06-26 15:40:48 +0100108/**
109 * of_attach_node() - Plug a device node into the tree and global list.
110 */
111int of_attach_node(struct device_node *np)
112{
113 unsigned long flags;
114 int rc;
115
116 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
117 if (rc)
118 return rc;
119
Grant Likely8a2b22a2014-07-23 17:05:06 -0600120 mutex_lock(&of_mutex);
Grant Likely6afc0dc2014-06-26 15:40:48 +0100121 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antonioud8c50082014-07-04 19:58:46 +0300122 __of_attach_node(np);
Grant Likely6afc0dc2014-06-26 15:40:48 +0100123 raw_spin_unlock_irqrestore(&devtree_lock, flags);
124
Grant Likely8a2b22a2014-07-23 17:05:06 -0600125 __of_attach_node_sysfs(np);
126 mutex_unlock(&of_mutex);
Grant Likely6afc0dc2014-06-26 15:40:48 +0100127 return 0;
128}
129
Pantelis Antonioud8c50082014-07-04 19:58:46 +0300130void __of_detach_node(struct device_node *np)
Grant Likely6afc0dc2014-06-26 15:40:48 +0100131{
132 struct device_node *parent;
Grant Likely6afc0dc2014-06-26 15:40:48 +0100133
Pantelis Antonioud8c50082014-07-04 19:58:46 +0300134 if (WARN_ON(of_node_check_flag(np, OF_DETACHED)))
135 return;
Grant Likely6afc0dc2014-06-26 15:40:48 +0100136
137 parent = np->parent;
Pantelis Antonioud8c50082014-07-04 19:58:46 +0300138 if (WARN_ON(!parent))
139 return;
Grant Likely6afc0dc2014-06-26 15:40:48 +0100140
141 if (of_allnodes == np)
142 of_allnodes = np->allnext;
143 else {
144 struct device_node *prev;
145 for (prev = of_allnodes;
146 prev->allnext != np;
147 prev = prev->allnext)
148 ;
149 prev->allnext = np->allnext;
150 }
151
152 if (parent->child == np)
153 parent->child = np->sibling;
154 else {
155 struct device_node *prevsib;
156 for (prevsib = np->parent->child;
157 prevsib->sibling != np;
158 prevsib = prevsib->sibling)
159 ;
160 prevsib->sibling = np->sibling;
161 }
162
163 of_node_set_flag(np, OF_DETACHED);
Pantelis Antonioud8c50082014-07-04 19:58:46 +0300164}
165
166/**
167 * of_detach_node() - "Unplug" a node from the device tree.
168 *
169 * The caller must hold a reference to the node. The memory associated with
170 * the node is not freed until its refcount goes to zero.
171 */
172int of_detach_node(struct device_node *np)
173{
174 unsigned long flags;
175 int rc = 0;
176
177 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
178 if (rc)
179 return rc;
180
Grant Likely8a2b22a2014-07-23 17:05:06 -0600181 mutex_lock(&of_mutex);
Pantelis Antonioud8c50082014-07-04 19:58:46 +0300182 raw_spin_lock_irqsave(&devtree_lock, flags);
183 __of_detach_node(np);
Grant Likely6afc0dc2014-06-26 15:40:48 +0100184 raw_spin_unlock_irqrestore(&devtree_lock, flags);
185
Grant Likely8a2b22a2014-07-23 17:05:06 -0600186 __of_detach_node_sysfs(np);
187 mutex_unlock(&of_mutex);
Grant Likely6afc0dc2014-06-26 15:40:48 +0100188 return rc;
189}
190
191/**
192 * of_node_release() - release a dynamically allocated node
193 * @kref: kref element of the node to be released
194 *
195 * In of_node_put() this function is passed to kref_put() as the destructor.
196 */
197void of_node_release(struct kobject *kobj)
198{
199 struct device_node *node = kobj_to_device_node(kobj);
200 struct property *prop = node->properties;
201
202 /* We should never be releasing nodes that haven't been detached. */
203 if (!of_node_check_flag(node, OF_DETACHED)) {
204 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
205 dump_stack();
206 return;
207 }
208
209 if (!of_node_check_flag(node, OF_DYNAMIC))
210 return;
211
212 while (prop) {
213 struct property *next = prop->next;
214 kfree(prop->name);
215 kfree(prop->value);
216 kfree(prop);
217 prop = next;
218
219 if (!prop) {
220 prop = node->deadprops;
221 node->deadprops = NULL;
222 }
223 }
224 kfree(node->full_name);
225 kfree(node->data);
226 kfree(node);
227}
Pantelis Antoniou69843392014-07-04 19:58:47 +0300228
229/**
230 * __of_prop_dup - Copy a property dynamically.
231 * @prop: Property to copy
232 * @allocflags: Allocation flags (typically pass GFP_KERNEL)
233 *
234 * Copy a property by dynamically allocating the memory of both the
235 * property stucture and the property name & contents. The property's
236 * flags have the OF_DYNAMIC bit set so that we can differentiate between
237 * dynamically allocated properties and not.
238 * Returns the newly allocated property or NULL on out of memory error.
239 */
240struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags)
241{
242 struct property *new;
243
244 new = kzalloc(sizeof(*new), allocflags);
245 if (!new)
246 return NULL;
247
248 /*
249 * NOTE: There is no check for zero length value.
250 * In case of a boolean property This will allocate a value
251 * of zero bytes. We do this to work around the use
252 * of of_get_property() calls on boolean values.
253 */
254 new->name = kstrdup(prop->name, allocflags);
255 new->value = kmemdup(prop->value, prop->length, allocflags);
256 new->length = prop->length;
257 if (!new->name || !new->value)
258 goto err_free;
259
260 /* mark the property as dynamic */
261 of_property_set_flag(new, OF_DYNAMIC);
262
263 return new;
264
265 err_free:
266 kfree(new->name);
267 kfree(new->value);
268 kfree(new);
269 return NULL;
270}
271
272/**
273 * __of_node_alloc() - Create an empty device node dynamically.
274 * @full_name: Full name of the new device node
275 * @allocflags: Allocation flags (typically pass GFP_KERNEL)
276 *
277 * Create an empty device tree node, suitable for further modification.
278 * The node data are dynamically allocated and all the node flags
279 * have the OF_DYNAMIC & OF_DETACHED bits set.
280 * Returns the newly allocated node or NULL on out of memory error.
281 */
282struct device_node *__of_node_alloc(const char *full_name, gfp_t allocflags)
283{
284 struct device_node *node;
285
286 node = kzalloc(sizeof(*node), allocflags);
287 if (!node)
288 return NULL;
289
290 node->full_name = kstrdup(full_name, allocflags);
291 of_node_set_flag(node, OF_DYNAMIC);
292 of_node_set_flag(node, OF_DETACHED);
293 if (!node->full_name)
294 goto err_free;
295
296 of_node_init(node);
297
298 return node;
299
300 err_free:
301 kfree(node->full_name);
302 kfree(node);
303 return NULL;
304}