blob: 53826b84e0ec6d46d3699705f46216070a471867 [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
Gaurav Minochaef69d742014-09-05 09:56:13 -070048 if (!IS_ENABLED(CONFIG_SYSFS))
49 return;
50
Grant Likely6afc0dc2014-06-26 15:40:48 +010051 BUG_ON(!of_node_is_initialized(np));
Grant Likely8a2b22a2014-07-23 17:05:06 -060052 if (!of_kset)
53 return;
Grant Likely6afc0dc2014-06-26 15:40:48 +010054
55 /* only remove properties if on sysfs */
56 if (of_node_is_attached(np)) {
57 for_each_property_of_node(np, pp)
58 sysfs_remove_bin_file(&np->kobj, &pp->attr);
59 kobject_del(&np->kobj);
60 }
61
62 /* finally remove the kobj_init ref */
63 of_node_put(np);
64}
65
66static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
67
68int of_reconfig_notifier_register(struct notifier_block *nb)
69{
70 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
71}
72EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
73
74int of_reconfig_notifier_unregister(struct notifier_block *nb)
75{
76 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
77}
78EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
79
Grant Likely00aa3722014-11-14 14:34:55 +000080#ifdef DEBUG
81const char *action_names[] = {
82 [OF_RECONFIG_ATTACH_NODE] = "ATTACH_NODE",
83 [OF_RECONFIG_DETACH_NODE] = "DETACH_NODE",
84 [OF_RECONFIG_ADD_PROPERTY] = "ADD_PROPERTY",
85 [OF_RECONFIG_REMOVE_PROPERTY] = "REMOVE_PROPERTY",
86 [OF_RECONFIG_UPDATE_PROPERTY] = "UPDATE_PROPERTY",
87};
88#endif
89
Grant Likelyf5242e52014-11-24 17:58:01 +000090int of_reconfig_notify(unsigned long action, struct of_reconfig_data *p)
Grant Likely6afc0dc2014-06-26 15:40:48 +010091{
92 int rc;
Grant Likely00aa3722014-11-14 14:34:55 +000093#ifdef DEBUG
Grant Likelyf5242e52014-11-24 17:58:01 +000094 struct of_reconfig_data *pr = p;
Grant Likely6afc0dc2014-06-26 15:40:48 +010095
Grant Likely00aa3722014-11-14 14:34:55 +000096 switch (action) {
97 case OF_RECONFIG_ATTACH_NODE:
98 case OF_RECONFIG_DETACH_NODE:
99 pr_debug("of/notify %-15s %s\n", action_names[action],
Grant Likelyf5242e52014-11-24 17:58:01 +0000100 pr->dn->full_name);
Grant Likely00aa3722014-11-14 14:34:55 +0000101 break;
102 case OF_RECONFIG_ADD_PROPERTY:
103 case OF_RECONFIG_REMOVE_PROPERTY:
104 case OF_RECONFIG_UPDATE_PROPERTY:
105 pr_debug("of/notify %-15s %s:%s\n", action_names[action],
106 pr->dn->full_name, pr->prop->name);
107 break;
108
109 }
110#endif
Grant Likely6afc0dc2014-06-26 15:40:48 +0100111 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
112 return notifier_to_errno(rc);
113}
114
Pantelis Antonioub53a2342014-10-28 22:33:53 +0200115/*
116 * of_reconfig_get_state_change() - Returns new state of device
117 * @action - action of the of notifier
118 * @arg - argument of the of notifier
119 *
120 * Returns the new state of a device based on the notifier used.
121 * Returns 0 on device going from enabled to disabled, 1 on device
122 * going from disabled to enabled and -1 on no change.
123 */
Grant Likelyf5242e52014-11-24 17:58:01 +0000124int of_reconfig_get_state_change(unsigned long action, struct of_reconfig_data *pr)
Pantelis Antonioub53a2342014-10-28 22:33:53 +0200125{
Grant Likelyf5242e52014-11-24 17:58:01 +0000126 struct property *prop, *old_prop = NULL;
Pantelis Antonioub53a2342014-10-28 22:33:53 +0200127 int is_status, status_state, old_status_state, prev_state, new_state;
128
129 /* figure out if a device should be created or destroyed */
Pantelis Antonioub53a2342014-10-28 22:33:53 +0200130 switch (action) {
131 case OF_RECONFIG_ATTACH_NODE:
132 case OF_RECONFIG_DETACH_NODE:
Grant Likelyf5242e52014-11-24 17:58:01 +0000133 prop = of_find_property(pr->dn, "status", NULL);
Pantelis Antonioub53a2342014-10-28 22:33:53 +0200134 break;
135 case OF_RECONFIG_ADD_PROPERTY:
136 case OF_RECONFIG_REMOVE_PROPERTY:
Pantelis Antonioub53a2342014-10-28 22:33:53 +0200137 prop = pr->prop;
138 break;
139 case OF_RECONFIG_UPDATE_PROPERTY:
Pantelis Antonioub53a2342014-10-28 22:33:53 +0200140 prop = pr->prop;
141 old_prop = pr->old_prop;
142 break;
143 default:
144 return OF_RECONFIG_NO_CHANGE;
145 }
146
147 is_status = 0;
148 status_state = -1;
149 old_status_state = -1;
150 prev_state = -1;
151 new_state = -1;
152
153 if (prop && !strcmp(prop->name, "status")) {
154 is_status = 1;
155 status_state = !strcmp(prop->value, "okay") ||
156 !strcmp(prop->value, "ok");
157 if (old_prop)
158 old_status_state = !strcmp(old_prop->value, "okay") ||
159 !strcmp(old_prop->value, "ok");
160 }
161
162 switch (action) {
163 case OF_RECONFIG_ATTACH_NODE:
164 prev_state = 0;
165 /* -1 & 0 status either missing or okay */
166 new_state = status_state != 0;
167 break;
168 case OF_RECONFIG_DETACH_NODE:
169 /* -1 & 0 status either missing or okay */
170 prev_state = status_state != 0;
171 new_state = 0;
172 break;
173 case OF_RECONFIG_ADD_PROPERTY:
174 if (is_status) {
175 /* no status property -> enabled (legacy) */
176 prev_state = 1;
177 new_state = status_state;
178 }
179 break;
180 case OF_RECONFIG_REMOVE_PROPERTY:
181 if (is_status) {
182 prev_state = status_state;
183 /* no status property -> enabled (legacy) */
184 new_state = 1;
185 }
186 break;
187 case OF_RECONFIG_UPDATE_PROPERTY:
188 if (is_status) {
189 prev_state = old_status_state != 0;
190 new_state = status_state != 0;
191 }
192 break;
193 }
194
195 if (prev_state == new_state)
196 return OF_RECONFIG_NO_CHANGE;
197
198 return new_state ? OF_RECONFIG_CHANGE_ADD : OF_RECONFIG_CHANGE_REMOVE;
199}
200EXPORT_SYMBOL_GPL(of_reconfig_get_state_change);
201
Grant Likely6afc0dc2014-06-26 15:40:48 +0100202int of_property_notify(int action, struct device_node *np,
Grant Likely259092a2014-07-16 12:48:23 -0600203 struct property *prop, struct property *oldprop)
Grant Likely6afc0dc2014-06-26 15:40:48 +0100204{
Grant Likelyf5242e52014-11-24 17:58:01 +0000205 struct of_reconfig_data pr;
Grant Likely6afc0dc2014-06-26 15:40:48 +0100206
207 /* only call notifiers if the node is attached */
208 if (!of_node_is_attached(np))
209 return 0;
210
211 pr.dn = np;
212 pr.prop = prop;
Grant Likely259092a2014-07-16 12:48:23 -0600213 pr.old_prop = oldprop;
Grant Likely6afc0dc2014-06-26 15:40:48 +0100214 return of_reconfig_notify(action, &pr);
215}
216
Pantelis Antonioud8c50082014-07-04 19:58:46 +0300217void __of_attach_node(struct device_node *np)
218{
Grant Likelya25095d2014-07-15 23:25:43 -0600219 const __be32 *phandle;
220 int sz;
221
222 np->name = __of_get_property(np, "name", NULL) ? : "<NULL>";
223 np->type = __of_get_property(np, "device_type", NULL) ? : "<NULL>";
224
225 phandle = __of_get_property(np, "phandle", &sz);
226 if (!phandle)
227 phandle = __of_get_property(np, "linux,phandle", &sz);
Geert Uytterhoevenf76502a2015-06-04 11:34:41 +0200228 if (IS_ENABLED(CONFIG_PPC_PSERIES) && !phandle)
Grant Likelya25095d2014-07-15 23:25:43 -0600229 phandle = __of_get_property(np, "ibm,phandle", &sz);
230 np->phandle = (phandle && (sz >= 4)) ? be32_to_cpup(phandle) : 0;
231
Grant Likely6162dbe2014-07-16 08:48:46 -0600232 np->child = NULL;
Pantelis Antonioud8c50082014-07-04 19:58:46 +0300233 np->sibling = np->parent->child;
Pantelis Antonioud8c50082014-07-04 19:58:46 +0300234 np->parent->child = np;
235 of_node_clear_flag(np, OF_DETACHED);
236}
237
Grant Likely6afc0dc2014-06-26 15:40:48 +0100238/**
239 * of_attach_node() - Plug a device node into the tree and global list.
240 */
241int of_attach_node(struct device_node *np)
242{
Grant Likelyf5242e52014-11-24 17:58:01 +0000243 struct of_reconfig_data rd;
Grant Likely6afc0dc2014-06-26 15:40:48 +0100244 unsigned long flags;
Grant Likely6afc0dc2014-06-26 15:40:48 +0100245
Grant Likelyf5242e52014-11-24 17:58:01 +0000246 memset(&rd, 0, sizeof(rd));
247 rd.dn = np;
248
Grant Likely8a2b22a2014-07-23 17:05:06 -0600249 mutex_lock(&of_mutex);
Grant Likely6afc0dc2014-06-26 15:40:48 +0100250 raw_spin_lock_irqsave(&devtree_lock, flags);
Pantelis Antonioud8c50082014-07-04 19:58:46 +0300251 __of_attach_node(np);
Grant Likely6afc0dc2014-06-26 15:40:48 +0100252 raw_spin_unlock_irqrestore(&devtree_lock, flags);
253
Grant Likely8a2b22a2014-07-23 17:05:06 -0600254 __of_attach_node_sysfs(np);
255 mutex_unlock(&of_mutex);
Grant Likely259092a2014-07-16 12:48:23 -0600256
Grant Likelyf5242e52014-11-24 17:58:01 +0000257 of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, &rd);
Grant Likely259092a2014-07-16 12:48:23 -0600258
Grant Likely6afc0dc2014-06-26 15:40:48 +0100259 return 0;
260}
261
Pantelis Antonioud8c50082014-07-04 19:58:46 +0300262void __of_detach_node(struct device_node *np)
Grant Likely6afc0dc2014-06-26 15:40:48 +0100263{
264 struct device_node *parent;
Grant Likely6afc0dc2014-06-26 15:40:48 +0100265
Pantelis Antonioud8c50082014-07-04 19:58:46 +0300266 if (WARN_ON(of_node_check_flag(np, OF_DETACHED)))
267 return;
Grant Likely6afc0dc2014-06-26 15:40:48 +0100268
269 parent = np->parent;
Pantelis Antonioud8c50082014-07-04 19:58:46 +0300270 if (WARN_ON(!parent))
271 return;
Grant Likely6afc0dc2014-06-26 15:40:48 +0100272
Grant Likely6afc0dc2014-06-26 15:40:48 +0100273 if (parent->child == np)
274 parent->child = np->sibling;
275 else {
276 struct device_node *prevsib;
277 for (prevsib = np->parent->child;
278 prevsib->sibling != np;
279 prevsib = prevsib->sibling)
280 ;
281 prevsib->sibling = np->sibling;
282 }
283
284 of_node_set_flag(np, OF_DETACHED);
Pantelis Antonioud8c50082014-07-04 19:58:46 +0300285}
286
287/**
288 * of_detach_node() - "Unplug" a node from the device tree.
289 *
290 * The caller must hold a reference to the node. The memory associated with
291 * the node is not freed until its refcount goes to zero.
292 */
293int of_detach_node(struct device_node *np)
294{
Grant Likelyf5242e52014-11-24 17:58:01 +0000295 struct of_reconfig_data rd;
Pantelis Antonioud8c50082014-07-04 19:58:46 +0300296 unsigned long flags;
297 int rc = 0;
298
Grant Likelyf5242e52014-11-24 17:58:01 +0000299 memset(&rd, 0, sizeof(rd));
300 rd.dn = np;
301
Grant Likely8a2b22a2014-07-23 17:05:06 -0600302 mutex_lock(&of_mutex);
Pantelis Antonioud8c50082014-07-04 19:58:46 +0300303 raw_spin_lock_irqsave(&devtree_lock, flags);
304 __of_detach_node(np);
Grant Likely6afc0dc2014-06-26 15:40:48 +0100305 raw_spin_unlock_irqrestore(&devtree_lock, flags);
306
Grant Likely8a2b22a2014-07-23 17:05:06 -0600307 __of_detach_node_sysfs(np);
308 mutex_unlock(&of_mutex);
Grant Likely259092a2014-07-16 12:48:23 -0600309
Grant Likelyf5242e52014-11-24 17:58:01 +0000310 of_reconfig_notify(OF_RECONFIG_DETACH_NODE, &rd);
Grant Likely259092a2014-07-16 12:48:23 -0600311
Grant Likely6afc0dc2014-06-26 15:40:48 +0100312 return rc;
313}
314
315/**
316 * of_node_release() - release a dynamically allocated node
317 * @kref: kref element of the node to be released
318 *
319 * In of_node_put() this function is passed to kref_put() as the destructor.
320 */
321void of_node_release(struct kobject *kobj)
322{
323 struct device_node *node = kobj_to_device_node(kobj);
324 struct property *prop = node->properties;
325
326 /* We should never be releasing nodes that haven't been detached. */
327 if (!of_node_check_flag(node, OF_DETACHED)) {
328 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
329 dump_stack();
330 return;
331 }
332
333 if (!of_node_check_flag(node, OF_DYNAMIC))
334 return;
335
336 while (prop) {
337 struct property *next = prop->next;
338 kfree(prop->name);
339 kfree(prop->value);
340 kfree(prop);
341 prop = next;
342
343 if (!prop) {
344 prop = node->deadprops;
345 node->deadprops = NULL;
346 }
347 }
348 kfree(node->full_name);
349 kfree(node->data);
350 kfree(node);
351}
Pantelis Antoniou69843392014-07-04 19:58:47 +0300352
353/**
354 * __of_prop_dup - Copy a property dynamically.
355 * @prop: Property to copy
356 * @allocflags: Allocation flags (typically pass GFP_KERNEL)
357 *
358 * Copy a property by dynamically allocating the memory of both the
Geert Uytterhoeven27b33832014-10-22 11:49:01 +0200359 * property structure and the property name & contents. The property's
Pantelis Antoniou69843392014-07-04 19:58:47 +0300360 * flags have the OF_DYNAMIC bit set so that we can differentiate between
361 * dynamically allocated properties and not.
362 * Returns the newly allocated property or NULL on out of memory error.
363 */
364struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags)
365{
366 struct property *new;
367
368 new = kzalloc(sizeof(*new), allocflags);
369 if (!new)
370 return NULL;
371
372 /*
373 * NOTE: There is no check for zero length value.
Grant Likelyb6ae5dc2014-07-26 10:58:43 -0600374 * In case of a boolean property, this will allocate a value
Pantelis Antoniou69843392014-07-04 19:58:47 +0300375 * of zero bytes. We do this to work around the use
376 * of of_get_property() calls on boolean values.
377 */
378 new->name = kstrdup(prop->name, allocflags);
379 new->value = kmemdup(prop->value, prop->length, allocflags);
380 new->length = prop->length;
381 if (!new->name || !new->value)
382 goto err_free;
383
384 /* mark the property as dynamic */
385 of_property_set_flag(new, OF_DYNAMIC);
386
387 return new;
388
389 err_free:
390 kfree(new->name);
391 kfree(new->value);
392 kfree(new);
393 return NULL;
394}
395
396/**
Grant Likelye5179582014-11-17 22:31:32 +0000397 * __of_node_dup() - Duplicate or create an empty device node dynamically.
398 * @fmt: Format string (plus vargs) for new full name of the device node
Pantelis Antoniou69843392014-07-04 19:58:47 +0300399 *
Grant Likelye5179582014-11-17 22:31:32 +0000400 * Create an device tree node, either by duplicating an empty node or by allocating
401 * an empty one suitable for further modification. The node data are
402 * dynamically allocated and all the node flags have the OF_DYNAMIC &
403 * OF_DETACHED bits set. Returns the newly allocated node or NULL on out of
404 * memory error.
Pantelis Antoniou69843392014-07-04 19:58:47 +0300405 */
Grant Likelye5179582014-11-17 22:31:32 +0000406struct device_node *__of_node_dup(const struct device_node *np, const char *fmt, ...)
Pantelis Antoniou69843392014-07-04 19:58:47 +0300407{
Grant Likelyef8bbd72014-11-14 15:33:07 +0000408 va_list vargs;
Pantelis Antoniou69843392014-07-04 19:58:47 +0300409 struct device_node *node;
410
Grant Likelyef8bbd72014-11-14 15:33:07 +0000411 node = kzalloc(sizeof(*node), GFP_KERNEL);
Pantelis Antoniou69843392014-07-04 19:58:47 +0300412 if (!node)
413 return NULL;
Grant Likelyef8bbd72014-11-14 15:33:07 +0000414 va_start(vargs, fmt);
415 node->full_name = kvasprintf(GFP_KERNEL, fmt, vargs);
416 va_end(vargs);
Grant Likelye5179582014-11-17 22:31:32 +0000417 if (!node->full_name) {
418 kfree(node);
419 return NULL;
420 }
Pantelis Antoniou69843392014-07-04 19:58:47 +0300421
Grant Likelyef8bbd72014-11-14 15:33:07 +0000422 of_node_set_flag(node, OF_DYNAMIC);
423 of_node_set_flag(node, OF_DETACHED);
Pantelis Antoniou69843392014-07-04 19:58:47 +0300424 of_node_init(node);
425
Grant Likelye5179582014-11-17 22:31:32 +0000426 /* Iterate over and duplicate all properties */
427 if (np) {
428 struct property *pp, *new_pp;
429 for_each_property_of_node(np, pp) {
430 new_pp = __of_prop_dup(pp, GFP_KERNEL);
431 if (!new_pp)
432 goto err_prop;
433 if (__of_add_property(node, new_pp)) {
434 kfree(new_pp->name);
435 kfree(new_pp->value);
436 kfree(new_pp);
437 goto err_prop;
438 }
439 }
440 }
Pantelis Antoniou69843392014-07-04 19:58:47 +0300441 return node;
442
Grant Likelye5179582014-11-17 22:31:32 +0000443 err_prop:
444 of_node_put(node); /* Frees the node and properties */
Pantelis Antoniou69843392014-07-04 19:58:47 +0300445 return NULL;
446}
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300447
448static void __of_changeset_entry_destroy(struct of_changeset_entry *ce)
449{
450 of_node_put(ce->np);
451 list_del(&ce->node);
452 kfree(ce);
453}
454
455#ifdef DEBUG
456static void __of_changeset_entry_dump(struct of_changeset_entry *ce)
457{
458 switch (ce->action) {
459 case OF_RECONFIG_ADD_PROPERTY:
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300460 case OF_RECONFIG_REMOVE_PROPERTY:
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300461 case OF_RECONFIG_UPDATE_PROPERTY:
Grant Likely00aa3722014-11-14 14:34:55 +0000462 pr_debug("of/cset<%p> %-15s %s/%s\n", ce, action_names[ce->action],
463 ce->np->full_name, ce->prop->name);
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300464 break;
465 case OF_RECONFIG_ATTACH_NODE:
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300466 case OF_RECONFIG_DETACH_NODE:
Grant Likely00aa3722014-11-14 14:34:55 +0000467 pr_debug("of/cset<%p> %-15s %s\n", ce, action_names[ce->action],
468 ce->np->full_name);
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300469 break;
470 }
471}
472#else
473static inline void __of_changeset_entry_dump(struct of_changeset_entry *ce)
474{
475 /* empty */
476}
477#endif
478
479static void __of_changeset_entry_invert(struct of_changeset_entry *ce,
480 struct of_changeset_entry *rce)
481{
482 memcpy(rce, ce, sizeof(*rce));
483
484 switch (ce->action) {
485 case OF_RECONFIG_ATTACH_NODE:
486 rce->action = OF_RECONFIG_DETACH_NODE;
487 break;
488 case OF_RECONFIG_DETACH_NODE:
489 rce->action = OF_RECONFIG_ATTACH_NODE;
490 break;
491 case OF_RECONFIG_ADD_PROPERTY:
492 rce->action = OF_RECONFIG_REMOVE_PROPERTY;
493 break;
494 case OF_RECONFIG_REMOVE_PROPERTY:
495 rce->action = OF_RECONFIG_ADD_PROPERTY;
496 break;
497 case OF_RECONFIG_UPDATE_PROPERTY:
498 rce->old_prop = ce->prop;
499 rce->prop = ce->old_prop;
500 break;
501 }
502}
503
504static void __of_changeset_entry_notify(struct of_changeset_entry *ce, bool revert)
505{
Grant Likelyf5242e52014-11-24 17:58:01 +0000506 struct of_reconfig_data rd;
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300507 struct of_changeset_entry ce_inverted;
508 int ret;
509
510 if (revert) {
511 __of_changeset_entry_invert(ce, &ce_inverted);
512 ce = &ce_inverted;
513 }
514
515 switch (ce->action) {
516 case OF_RECONFIG_ATTACH_NODE:
517 case OF_RECONFIG_DETACH_NODE:
Grant Likelyf5242e52014-11-24 17:58:01 +0000518 memset(&rd, 0, sizeof(rd));
519 rd.dn = ce->np;
520 ret = of_reconfig_notify(ce->action, &rd);
Pantelis Antoniou201c9102014-07-04 19:58:49 +0300521 break;
522 case OF_RECONFIG_ADD_PROPERTY:
523 case OF_RECONFIG_REMOVE_PROPERTY:
524 case OF_RECONFIG_UPDATE_PROPERTY:
525 ret = of_property_notify(ce->action, ce->np, ce->prop, ce->old_prop);
526 break;
527 default:
528 pr_err("%s: invalid devicetree changeset action: %i\n", __func__,
529 (int)ce->action);
530 return;
531 }
532
533 if (ret)
534 pr_err("%s: notifier error @%s\n", __func__, ce->np->full_name);
535}
536
537static int __of_changeset_entry_apply(struct of_changeset_entry *ce)
538{
539 struct property *old_prop, **propp;
540 unsigned long flags;
541 int ret = 0;
542
543 __of_changeset_entry_dump(ce);
544
545 raw_spin_lock_irqsave(&devtree_lock, flags);
546 switch (ce->action) {
547 case OF_RECONFIG_ATTACH_NODE:
548 __of_attach_node(ce->np);
549 break;
550 case OF_RECONFIG_DETACH_NODE:
551 __of_detach_node(ce->np);
552 break;
553 case OF_RECONFIG_ADD_PROPERTY:
554 /* If the property is in deadprops then it must be removed */
555 for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
556 if (*propp == ce->prop) {
557 *propp = ce->prop->next;
558 ce->prop->next = NULL;
559 break;
560 }
561 }
562
563 ret = __of_add_property(ce->np, ce->prop);
564 if (ret) {
565 pr_err("%s: add_property failed @%s/%s\n",
566 __func__, ce->np->full_name,
567 ce->prop->name);
568 break;
569 }
570 break;
571 case OF_RECONFIG_REMOVE_PROPERTY:
572 ret = __of_remove_property(ce->np, ce->prop);
573 if (ret) {
574 pr_err("%s: remove_property failed @%s/%s\n",
575 __func__, ce->np->full_name,
576 ce->prop->name);
577 break;
578 }
579 break;
580
581 case OF_RECONFIG_UPDATE_PROPERTY:
582 /* If the property is in deadprops then it must be removed */
583 for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
584 if (*propp == ce->prop) {
585 *propp = ce->prop->next;
586 ce->prop->next = NULL;
587 break;
588 }
589 }
590
591 ret = __of_update_property(ce->np, ce->prop, &old_prop);
592 if (ret) {
593 pr_err("%s: update_property failed @%s/%s\n",
594 __func__, ce->np->full_name,
595 ce->prop->name);
596 break;
597 }
598 break;
599 default:
600 ret = -EINVAL;
601 }
602 raw_spin_unlock_irqrestore(&devtree_lock, flags);
603
604 if (ret)
605 return ret;
606
607 switch (ce->action) {
608 case OF_RECONFIG_ATTACH_NODE:
609 __of_attach_node_sysfs(ce->np);
610 break;
611 case OF_RECONFIG_DETACH_NODE:
612 __of_detach_node_sysfs(ce->np);
613 break;
614 case OF_RECONFIG_ADD_PROPERTY:
615 /* ignore duplicate names */
616 __of_add_property_sysfs(ce->np, ce->prop);
617 break;
618 case OF_RECONFIG_REMOVE_PROPERTY:
619 __of_remove_property_sysfs(ce->np, ce->prop);
620 break;
621 case OF_RECONFIG_UPDATE_PROPERTY:
622 __of_update_property_sysfs(ce->np, ce->prop, ce->old_prop);
623 break;
624 }
625
626 return 0;
627}
628
629static inline int __of_changeset_entry_revert(struct of_changeset_entry *ce)
630{
631 struct of_changeset_entry ce_inverted;
632
633 __of_changeset_entry_invert(ce, &ce_inverted);
634 return __of_changeset_entry_apply(&ce_inverted);
635}
636
637/**
638 * of_changeset_init - Initialize a changeset for use
639 *
640 * @ocs: changeset pointer
641 *
642 * Initialize a changeset structure
643 */
644void of_changeset_init(struct of_changeset *ocs)
645{
646 memset(ocs, 0, sizeof(*ocs));
647 INIT_LIST_HEAD(&ocs->entries);
648}
649
650/**
651 * of_changeset_destroy - Destroy a changeset
652 *
653 * @ocs: changeset pointer
654 *
655 * Destroys a changeset. Note that if a changeset is applied,
656 * its changes to the tree cannot be reverted.
657 */
658void of_changeset_destroy(struct of_changeset *ocs)
659{
660 struct of_changeset_entry *ce, *cen;
661
662 list_for_each_entry_safe_reverse(ce, cen, &ocs->entries, node)
663 __of_changeset_entry_destroy(ce);
664}
665
666/**
667 * of_changeset_apply - Applies a changeset
668 *
669 * @ocs: changeset pointer
670 *
671 * Applies a changeset to the live tree.
672 * Any side-effects of live tree state changes are applied here on
673 * sucess, like creation/destruction of devices and side-effects
674 * like creation of sysfs properties and directories.
675 * Returns 0 on success, a negative error value in case of an error.
676 * On error the partially applied effects are reverted.
677 */
678int of_changeset_apply(struct of_changeset *ocs)
679{
680 struct of_changeset_entry *ce;
681 int ret;
682
683 /* perform the rest of the work */
684 pr_debug("of_changeset: applying...\n");
685 list_for_each_entry(ce, &ocs->entries, node) {
686 ret = __of_changeset_entry_apply(ce);
687 if (ret) {
688 pr_err("%s: Error applying changeset (%d)\n", __func__, ret);
689 list_for_each_entry_continue_reverse(ce, &ocs->entries, node)
690 __of_changeset_entry_revert(ce);
691 return ret;
692 }
693 }
694 pr_debug("of_changeset: applied, emitting notifiers.\n");
695
696 /* drop the global lock while emitting notifiers */
697 mutex_unlock(&of_mutex);
698 list_for_each_entry(ce, &ocs->entries, node)
699 __of_changeset_entry_notify(ce, 0);
700 mutex_lock(&of_mutex);
701 pr_debug("of_changeset: notifiers sent.\n");
702
703 return 0;
704}
705
706/**
707 * of_changeset_revert - Reverts an applied changeset
708 *
709 * @ocs: changeset pointer
710 *
711 * Reverts a changeset returning the state of the tree to what it
712 * was before the application.
713 * Any side-effects like creation/destruction of devices and
714 * removal of sysfs properties and directories are applied.
715 * Returns 0 on success, a negative error value in case of an error.
716 */
717int of_changeset_revert(struct of_changeset *ocs)
718{
719 struct of_changeset_entry *ce;
720 int ret;
721
722 pr_debug("of_changeset: reverting...\n");
723 list_for_each_entry_reverse(ce, &ocs->entries, node) {
724 ret = __of_changeset_entry_revert(ce);
725 if (ret) {
726 pr_err("%s: Error reverting changeset (%d)\n", __func__, ret);
727 list_for_each_entry_continue(ce, &ocs->entries, node)
728 __of_changeset_entry_apply(ce);
729 return ret;
730 }
731 }
732 pr_debug("of_changeset: reverted, emitting notifiers.\n");
733
734 /* drop the global lock while emitting notifiers */
735 mutex_unlock(&of_mutex);
736 list_for_each_entry_reverse(ce, &ocs->entries, node)
737 __of_changeset_entry_notify(ce, 1);
738 mutex_lock(&of_mutex);
739 pr_debug("of_changeset: notifiers sent.\n");
740
741 return 0;
742}
743
744/**
745 * of_changeset_action - Perform a changeset action
746 *
747 * @ocs: changeset pointer
748 * @action: action to perform
749 * @np: Pointer to device node
750 * @prop: Pointer to property
751 *
752 * On action being one of:
753 * + OF_RECONFIG_ATTACH_NODE
754 * + OF_RECONFIG_DETACH_NODE,
755 * + OF_RECONFIG_ADD_PROPERTY
756 * + OF_RECONFIG_REMOVE_PROPERTY,
757 * + OF_RECONFIG_UPDATE_PROPERTY
758 * Returns 0 on success, a negative error value in case of an error.
759 */
760int of_changeset_action(struct of_changeset *ocs, unsigned long action,
761 struct device_node *np, struct property *prop)
762{
763 struct of_changeset_entry *ce;
764
765 ce = kzalloc(sizeof(*ce), GFP_KERNEL);
766 if (!ce) {
767 pr_err("%s: Failed to allocate\n", __func__);
768 return -ENOMEM;
769 }
770 /* get a reference to the node */
771 ce->action = action;
772 ce->np = of_node_get(np);
773 ce->prop = prop;
774
775 if (action == OF_RECONFIG_UPDATE_PROPERTY && prop)
776 ce->old_prop = of_find_property(np, prop->name, NULL);
777
778 /* add it to the list */
779 list_add_tail(&ce->node, &ocs->entries);
780 return 0;
781}