blob: b96d831009875e379178b21ebb4ebbd4ddda4ccd [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{
Grant Likely6162dbe2014-07-16 08:48:46 -0600101 np->child = NULL;
Pantelis Antonioud8c50082014-07-04 19:58:46 +0300102 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);
107}
108
Grant Likely6afc0dc2014-06-26 15:40:48 +0100109/**
110 * of_attach_node() - Plug a device node into the tree and global list.
111 */
112int of_attach_node(struct device_node *np)
113{
114 unsigned long flags;
115 int rc;
116
117 rc = of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
118 if (rc)
119 return rc;
120
Grant Likely8a2b22a2014-07-23 17:05:06 -0600121 mutex_lock(&of_mutex);
Grant Likely6afc0dc2014-06-26 15:40:48 +0100122 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antonioud8c50082014-07-04 19:58:46 +0300123 __of_attach_node(np);
Grant Likely6afc0dc2014-06-26 15:40:48 +0100124 raw_spin_unlock_irqrestore(&devtree_lock, flags);
125
Grant Likely8a2b22a2014-07-23 17:05:06 -0600126 __of_attach_node_sysfs(np);
127 mutex_unlock(&of_mutex);
Grant Likely6afc0dc2014-06-26 15:40:48 +0100128 return 0;
129}
130
Pantelis Antonioud8c50082014-07-04 19:58:46 +0300131void __of_detach_node(struct device_node *np)
Grant Likely6afc0dc2014-06-26 15:40:48 +0100132{
133 struct device_node *parent;
Grant Likely6afc0dc2014-06-26 15:40:48 +0100134
Pantelis Antonioud8c50082014-07-04 19:58:46 +0300135 if (WARN_ON(of_node_check_flag(np, OF_DETACHED)))
136 return;
Grant Likely6afc0dc2014-06-26 15:40:48 +0100137
138 parent = np->parent;
Pantelis Antonioud8c50082014-07-04 19:58:46 +0300139 if (WARN_ON(!parent))
140 return;
Grant Likely6afc0dc2014-06-26 15:40:48 +0100141
142 if (of_allnodes == np)
143 of_allnodes = np->allnext;
144 else {
145 struct device_node *prev;
146 for (prev = of_allnodes;
147 prev->allnext != np;
148 prev = prev->allnext)
149 ;
150 prev->allnext = np->allnext;
151 }
152
153 if (parent->child == np)
154 parent->child = np->sibling;
155 else {
156 struct device_node *prevsib;
157 for (prevsib = np->parent->child;
158 prevsib->sibling != np;
159 prevsib = prevsib->sibling)
160 ;
161 prevsib->sibling = np->sibling;
162 }
163
164 of_node_set_flag(np, OF_DETACHED);
Pantelis Antonioud8c50082014-07-04 19:58:46 +0300165}
166
167/**
168 * of_detach_node() - "Unplug" a node from the device tree.
169 *
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.
172 */
173int of_detach_node(struct device_node *np)
174{
175 unsigned long flags;
176 int rc = 0;
177
178 rc = of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
179 if (rc)
180 return rc;
181
Grant Likely8a2b22a2014-07-23 17:05:06 -0600182 mutex_lock(&of_mutex);
Pantelis Antonioud8c50082014-07-04 19:58:46 +0300183 raw_spin_lock_irqsave(&devtree_lock, flags);
184 __of_detach_node(np);
Grant Likely6afc0dc2014-06-26 15:40:48 +0100185 raw_spin_unlock_irqrestore(&devtree_lock, flags);
186
Grant Likely8a2b22a2014-07-23 17:05:06 -0600187 __of_detach_node_sysfs(np);
188 mutex_unlock(&of_mutex);
Grant Likely6afc0dc2014-06-26 15:40:48 +0100189 return rc;
190}
191
192/**
193 * of_node_release() - release a dynamically allocated node
194 * @kref: kref element of the node to be released
195 *
196 * In of_node_put() this function is passed to kref_put() as the destructor.
197 */
198void of_node_release(struct kobject *kobj)
199{
200 struct device_node *node = kobj_to_device_node(kobj);
201 struct property *prop = node->properties;
202
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);
206 dump_stack();
207 return;
208 }
209
210 if (!of_node_check_flag(node, OF_DYNAMIC))
211 return;
212
213 while (prop) {
214 struct property *next = prop->next;
215 kfree(prop->name);
216 kfree(prop->value);
217 kfree(prop);
218 prop = next;
219
220 if (!prop) {
221 prop = node->deadprops;
222 node->deadprops = NULL;
223 }
224 }
225 kfree(node->full_name);
226 kfree(node->data);
227 kfree(node);
228}
Pantelis Antoniou69843392014-07-04 19:58:47 +0300229
230/**
231 * __of_prop_dup - Copy a property dynamically.
232 * @prop: Property to copy
233 * @allocflags: Allocation flags (typically pass GFP_KERNEL)
234 *
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.
240 */
241struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags)
242{
243 struct property *new;
244
245 new = kzalloc(sizeof(*new), allocflags);
246 if (!new)
247 return NULL;
248
249 /*
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.
254 */
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)
259 goto err_free;
260
261 /* mark the property as dynamic */
262 of_property_set_flag(new, OF_DYNAMIC);
263
264 return new;
265
266 err_free:
267 kfree(new->name);
268 kfree(new->value);
269 kfree(new);
270 return NULL;
271}
272
273/**
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)
277 *
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.
282 */
283struct device_node *__of_node_alloc(const char *full_name, gfp_t allocflags)
284{
285 struct device_node *node;
286
287 node = kzalloc(sizeof(*node), allocflags);
288 if (!node)
289 return NULL;
290
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)
295 goto err_free;
296
297 of_node_init(node);
298
299 return node;
300
301 err_free:
302 kfree(node->full_name);
303 kfree(node);
304 return NULL;
305}