]>
Commit | Line | Data |
---|---|---|
6afc0dc3 GL |
1 | /* |
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 | */ | |
24 | struct device_node *of_node_get(struct device_node *node) | |
25 | { | |
26 | if (node) | |
27 | kobject_get(&node->kobj); | |
28 | return node; | |
29 | } | |
30 | EXPORT_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 | */ | |
37 | void of_node_put(struct device_node *node) | |
38 | { | |
39 | if (node) | |
40 | kobject_put(&node->kobj); | |
41 | } | |
42 | EXPORT_SYMBOL(of_node_put); | |
43 | ||
8a2b22a2 | 44 | void __of_detach_node_sysfs(struct device_node *np) |
6afc0dc3 GL |
45 | { |
46 | struct property *pp; | |
47 | ||
ef69d740 GM |
48 | if (!IS_ENABLED(CONFIG_SYSFS)) |
49 | return; | |
50 | ||
6afc0dc3 | 51 | BUG_ON(!of_node_is_initialized(np)); |
8a2b22a2 GL |
52 | if (!of_kset) |
53 | return; | |
6afc0dc3 GL |
54 | |
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 | ||
66 | static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain); | |
67 | ||
68 | int of_reconfig_notifier_register(struct notifier_block *nb) | |
69 | { | |
70 | return blocking_notifier_chain_register(&of_reconfig_chain, nb); | |
71 | } | |
72 | EXPORT_SYMBOL_GPL(of_reconfig_notifier_register); | |
73 | ||
74 | int of_reconfig_notifier_unregister(struct notifier_block *nb) | |
75 | { | |
76 | return blocking_notifier_chain_unregister(&of_reconfig_chain, nb); | |
77 | } | |
78 | EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister); | |
79 | ||
80 | int of_reconfig_notify(unsigned long action, void *p) | |
81 | { | |
82 | int rc; | |
83 | ||
84 | rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p); | |
85 | return notifier_to_errno(rc); | |
86 | } | |
87 | ||
88 | int of_property_notify(int action, struct device_node *np, | |
259092a3 | 89 | struct property *prop, struct property *oldprop) |
6afc0dc3 GL |
90 | { |
91 | struct of_prop_reconfig pr; | |
92 | ||
93 | /* only call notifiers if the node is attached */ | |
94 | if (!of_node_is_attached(np)) | |
95 | return 0; | |
96 | ||
97 | pr.dn = np; | |
98 | pr.prop = prop; | |
259092a3 | 99 | pr.old_prop = oldprop; |
6afc0dc3 GL |
100 | return of_reconfig_notify(action, &pr); |
101 | } | |
102 | ||
d8c50088 PA |
103 | void __of_attach_node(struct device_node *np) |
104 | { | |
a25095d4 GL |
105 | const __be32 *phandle; |
106 | int sz; | |
107 | ||
108 | np->name = __of_get_property(np, "name", NULL) ? : "<NULL>"; | |
109 | np->type = __of_get_property(np, "device_type", NULL) ? : "<NULL>"; | |
110 | ||
111 | phandle = __of_get_property(np, "phandle", &sz); | |
112 | if (!phandle) | |
113 | phandle = __of_get_property(np, "linux,phandle", &sz); | |
114 | if (IS_ENABLED(PPC_PSERIES) && !phandle) | |
115 | phandle = __of_get_property(np, "ibm,phandle", &sz); | |
116 | np->phandle = (phandle && (sz >= 4)) ? be32_to_cpup(phandle) : 0; | |
117 | ||
6162dbe4 | 118 | np->child = NULL; |
d8c50088 | 119 | np->sibling = np->parent->child; |
d8c50088 PA |
120 | np->parent->child = np; |
121 | of_node_clear_flag(np, OF_DETACHED); | |
122 | } | |
123 | ||
6afc0dc3 GL |
124 | /** |
125 | * of_attach_node() - Plug a device node into the tree and global list. | |
126 | */ | |
127 | int of_attach_node(struct device_node *np) | |
128 | { | |
129 | unsigned long flags; | |
6afc0dc3 | 130 | |
8a2b22a2 | 131 | mutex_lock(&of_mutex); |
6afc0dc3 | 132 | raw_spin_lock_irqsave(&devtree_lock, flags); |
d8c50088 | 133 | __of_attach_node(np); |
6afc0dc3 GL |
134 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
135 | ||
8a2b22a2 GL |
136 | __of_attach_node_sysfs(np); |
137 | mutex_unlock(&of_mutex); | |
259092a3 GL |
138 | |
139 | of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np); | |
140 | ||
6afc0dc3 GL |
141 | return 0; |
142 | } | |
143 | ||
d8c50088 | 144 | void __of_detach_node(struct device_node *np) |
6afc0dc3 GL |
145 | { |
146 | struct device_node *parent; | |
6afc0dc3 | 147 | |
d8c50088 PA |
148 | if (WARN_ON(of_node_check_flag(np, OF_DETACHED))) |
149 | return; | |
6afc0dc3 GL |
150 | |
151 | parent = np->parent; | |
d8c50088 PA |
152 | if (WARN_ON(!parent)) |
153 | return; | |
6afc0dc3 | 154 | |
6afc0dc3 GL |
155 | if (parent->child == np) |
156 | parent->child = np->sibling; | |
157 | else { | |
158 | struct device_node *prevsib; | |
159 | for (prevsib = np->parent->child; | |
160 | prevsib->sibling != np; | |
161 | prevsib = prevsib->sibling) | |
162 | ; | |
163 | prevsib->sibling = np->sibling; | |
164 | } | |
165 | ||
166 | of_node_set_flag(np, OF_DETACHED); | |
d8c50088 PA |
167 | } |
168 | ||
169 | /** | |
170 | * of_detach_node() - "Unplug" a node from the device tree. | |
171 | * | |
172 | * The caller must hold a reference to the node. The memory associated with | |
173 | * the node is not freed until its refcount goes to zero. | |
174 | */ | |
175 | int of_detach_node(struct device_node *np) | |
176 | { | |
177 | unsigned long flags; | |
178 | int rc = 0; | |
179 | ||
8a2b22a2 | 180 | mutex_lock(&of_mutex); |
d8c50088 PA |
181 | raw_spin_lock_irqsave(&devtree_lock, flags); |
182 | __of_detach_node(np); | |
6afc0dc3 GL |
183 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
184 | ||
8a2b22a2 GL |
185 | __of_detach_node_sysfs(np); |
186 | mutex_unlock(&of_mutex); | |
259092a3 GL |
187 | |
188 | of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np); | |
189 | ||
6afc0dc3 GL |
190 | return rc; |
191 | } | |
192 | ||
193 | /** | |
194 | * of_node_release() - release a dynamically allocated node | |
195 | * @kref: kref element of the node to be released | |
196 | * | |
197 | * In of_node_put() this function is passed to kref_put() as the destructor. | |
198 | */ | |
199 | void of_node_release(struct kobject *kobj) | |
200 | { | |
201 | struct device_node *node = kobj_to_device_node(kobj); | |
202 | struct property *prop = node->properties; | |
203 | ||
204 | /* We should never be releasing nodes that haven't been detached. */ | |
205 | if (!of_node_check_flag(node, OF_DETACHED)) { | |
206 | pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name); | |
207 | dump_stack(); | |
208 | return; | |
209 | } | |
210 | ||
211 | if (!of_node_check_flag(node, OF_DYNAMIC)) | |
212 | return; | |
213 | ||
214 | while (prop) { | |
215 | struct property *next = prop->next; | |
216 | kfree(prop->name); | |
217 | kfree(prop->value); | |
218 | kfree(prop); | |
219 | prop = next; | |
220 | ||
221 | if (!prop) { | |
222 | prop = node->deadprops; | |
223 | node->deadprops = NULL; | |
224 | } | |
225 | } | |
226 | kfree(node->full_name); | |
227 | kfree(node->data); | |
228 | kfree(node); | |
229 | } | |
69843396 PA |
230 | |
231 | /** | |
232 | * __of_prop_dup - Copy a property dynamically. | |
233 | * @prop: Property to copy | |
234 | * @allocflags: Allocation flags (typically pass GFP_KERNEL) | |
235 | * | |
236 | * Copy a property by dynamically allocating the memory of both the | |
27b3383a | 237 | * property structure and the property name & contents. The property's |
69843396 PA |
238 | * flags have the OF_DYNAMIC bit set so that we can differentiate between |
239 | * dynamically allocated properties and not. | |
240 | * Returns the newly allocated property or NULL on out of memory error. | |
241 | */ | |
242 | struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags) | |
243 | { | |
244 | struct property *new; | |
245 | ||
246 | new = kzalloc(sizeof(*new), allocflags); | |
247 | if (!new) | |
248 | return NULL; | |
249 | ||
250 | /* | |
251 | * NOTE: There is no check for zero length value. | |
b6ae5dc5 | 252 | * In case of a boolean property, this will allocate a value |
69843396 PA |
253 | * of zero bytes. We do this to work around the use |
254 | * of of_get_property() calls on boolean values. | |
255 | */ | |
256 | new->name = kstrdup(prop->name, allocflags); | |
257 | new->value = kmemdup(prop->value, prop->length, allocflags); | |
258 | new->length = prop->length; | |
259 | if (!new->name || !new->value) | |
260 | goto err_free; | |
261 | ||
262 | /* mark the property as dynamic */ | |
263 | of_property_set_flag(new, OF_DYNAMIC); | |
264 | ||
265 | return new; | |
266 | ||
267 | err_free: | |
268 | kfree(new->name); | |
269 | kfree(new->value); | |
270 | kfree(new); | |
271 | return NULL; | |
272 | } | |
273 | ||
274 | /** | |
e5179581 GL |
275 | * __of_node_dup() - Duplicate or create an empty device node dynamically. |
276 | * @fmt: Format string (plus vargs) for new full name of the device node | |
69843396 | 277 | * |
e5179581 GL |
278 | * Create an device tree node, either by duplicating an empty node or by allocating |
279 | * an empty one suitable for further modification. The node data are | |
280 | * dynamically allocated and all the node flags have the OF_DYNAMIC & | |
281 | * OF_DETACHED bits set. Returns the newly allocated node or NULL on out of | |
282 | * memory error. | |
69843396 | 283 | */ |
e5179581 | 284 | struct device_node *__of_node_dup(const struct device_node *np, const char *fmt, ...) |
69843396 | 285 | { |
ef8bbd73 | 286 | va_list vargs; |
69843396 PA |
287 | struct device_node *node; |
288 | ||
ef8bbd73 | 289 | node = kzalloc(sizeof(*node), GFP_KERNEL); |
69843396 PA |
290 | if (!node) |
291 | return NULL; | |
ef8bbd73 GL |
292 | va_start(vargs, fmt); |
293 | node->full_name = kvasprintf(GFP_KERNEL, fmt, vargs); | |
294 | va_end(vargs); | |
e5179581 GL |
295 | if (!node->full_name) { |
296 | kfree(node); | |
297 | return NULL; | |
298 | } | |
69843396 | 299 | |
ef8bbd73 GL |
300 | of_node_set_flag(node, OF_DYNAMIC); |
301 | of_node_set_flag(node, OF_DETACHED); | |
69843396 PA |
302 | of_node_init(node); |
303 | ||
e5179581 GL |
304 | /* Iterate over and duplicate all properties */ |
305 | if (np) { | |
306 | struct property *pp, *new_pp; | |
307 | for_each_property_of_node(np, pp) { | |
308 | new_pp = __of_prop_dup(pp, GFP_KERNEL); | |
309 | if (!new_pp) | |
310 | goto err_prop; | |
311 | if (__of_add_property(node, new_pp)) { | |
312 | kfree(new_pp->name); | |
313 | kfree(new_pp->value); | |
314 | kfree(new_pp); | |
315 | goto err_prop; | |
316 | } | |
317 | } | |
318 | } | |
69843396 PA |
319 | return node; |
320 | ||
e5179581 GL |
321 | err_prop: |
322 | of_node_put(node); /* Frees the node and properties */ | |
69843396 PA |
323 | return NULL; |
324 | } | |
201c910b PA |
325 | |
326 | static void __of_changeset_entry_destroy(struct of_changeset_entry *ce) | |
327 | { | |
328 | of_node_put(ce->np); | |
329 | list_del(&ce->node); | |
330 | kfree(ce); | |
331 | } | |
332 | ||
333 | #ifdef DEBUG | |
334 | static void __of_changeset_entry_dump(struct of_changeset_entry *ce) | |
335 | { | |
336 | switch (ce->action) { | |
337 | case OF_RECONFIG_ADD_PROPERTY: | |
338 | pr_debug("%p: %s %s/%s\n", | |
339 | ce, "ADD_PROPERTY ", ce->np->full_name, | |
340 | ce->prop->name); | |
341 | break; | |
342 | case OF_RECONFIG_REMOVE_PROPERTY: | |
343 | pr_debug("%p: %s %s/%s\n", | |
344 | ce, "REMOVE_PROPERTY", ce->np->full_name, | |
345 | ce->prop->name); | |
346 | break; | |
347 | case OF_RECONFIG_UPDATE_PROPERTY: | |
348 | pr_debug("%p: %s %s/%s\n", | |
349 | ce, "UPDATE_PROPERTY", ce->np->full_name, | |
350 | ce->prop->name); | |
351 | break; | |
352 | case OF_RECONFIG_ATTACH_NODE: | |
353 | pr_debug("%p: %s %s\n", | |
354 | ce, "ATTACH_NODE ", ce->np->full_name); | |
355 | break; | |
356 | case OF_RECONFIG_DETACH_NODE: | |
357 | pr_debug("%p: %s %s\n", | |
358 | ce, "DETACH_NODE ", ce->np->full_name); | |
359 | break; | |
360 | } | |
361 | } | |
362 | #else | |
363 | static inline void __of_changeset_entry_dump(struct of_changeset_entry *ce) | |
364 | { | |
365 | /* empty */ | |
366 | } | |
367 | #endif | |
368 | ||
369 | static void __of_changeset_entry_invert(struct of_changeset_entry *ce, | |
370 | struct of_changeset_entry *rce) | |
371 | { | |
372 | memcpy(rce, ce, sizeof(*rce)); | |
373 | ||
374 | switch (ce->action) { | |
375 | case OF_RECONFIG_ATTACH_NODE: | |
376 | rce->action = OF_RECONFIG_DETACH_NODE; | |
377 | break; | |
378 | case OF_RECONFIG_DETACH_NODE: | |
379 | rce->action = OF_RECONFIG_ATTACH_NODE; | |
380 | break; | |
381 | case OF_RECONFIG_ADD_PROPERTY: | |
382 | rce->action = OF_RECONFIG_REMOVE_PROPERTY; | |
383 | break; | |
384 | case OF_RECONFIG_REMOVE_PROPERTY: | |
385 | rce->action = OF_RECONFIG_ADD_PROPERTY; | |
386 | break; | |
387 | case OF_RECONFIG_UPDATE_PROPERTY: | |
388 | rce->old_prop = ce->prop; | |
389 | rce->prop = ce->old_prop; | |
390 | break; | |
391 | } | |
392 | } | |
393 | ||
394 | static void __of_changeset_entry_notify(struct of_changeset_entry *ce, bool revert) | |
395 | { | |
396 | struct of_changeset_entry ce_inverted; | |
397 | int ret; | |
398 | ||
399 | if (revert) { | |
400 | __of_changeset_entry_invert(ce, &ce_inverted); | |
401 | ce = &ce_inverted; | |
402 | } | |
403 | ||
404 | switch (ce->action) { | |
405 | case OF_RECONFIG_ATTACH_NODE: | |
406 | case OF_RECONFIG_DETACH_NODE: | |
407 | ret = of_reconfig_notify(ce->action, ce->np); | |
408 | break; | |
409 | case OF_RECONFIG_ADD_PROPERTY: | |
410 | case OF_RECONFIG_REMOVE_PROPERTY: | |
411 | case OF_RECONFIG_UPDATE_PROPERTY: | |
412 | ret = of_property_notify(ce->action, ce->np, ce->prop, ce->old_prop); | |
413 | break; | |
414 | default: | |
415 | pr_err("%s: invalid devicetree changeset action: %i\n", __func__, | |
416 | (int)ce->action); | |
417 | return; | |
418 | } | |
419 | ||
420 | if (ret) | |
421 | pr_err("%s: notifier error @%s\n", __func__, ce->np->full_name); | |
422 | } | |
423 | ||
424 | static int __of_changeset_entry_apply(struct of_changeset_entry *ce) | |
425 | { | |
426 | struct property *old_prop, **propp; | |
427 | unsigned long flags; | |
428 | int ret = 0; | |
429 | ||
430 | __of_changeset_entry_dump(ce); | |
431 | ||
432 | raw_spin_lock_irqsave(&devtree_lock, flags); | |
433 | switch (ce->action) { | |
434 | case OF_RECONFIG_ATTACH_NODE: | |
435 | __of_attach_node(ce->np); | |
436 | break; | |
437 | case OF_RECONFIG_DETACH_NODE: | |
438 | __of_detach_node(ce->np); | |
439 | break; | |
440 | case OF_RECONFIG_ADD_PROPERTY: | |
441 | /* If the property is in deadprops then it must be removed */ | |
442 | for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) { | |
443 | if (*propp == ce->prop) { | |
444 | *propp = ce->prop->next; | |
445 | ce->prop->next = NULL; | |
446 | break; | |
447 | } | |
448 | } | |
449 | ||
450 | ret = __of_add_property(ce->np, ce->prop); | |
451 | if (ret) { | |
452 | pr_err("%s: add_property failed @%s/%s\n", | |
453 | __func__, ce->np->full_name, | |
454 | ce->prop->name); | |
455 | break; | |
456 | } | |
457 | break; | |
458 | case OF_RECONFIG_REMOVE_PROPERTY: | |
459 | ret = __of_remove_property(ce->np, ce->prop); | |
460 | if (ret) { | |
461 | pr_err("%s: remove_property failed @%s/%s\n", | |
462 | __func__, ce->np->full_name, | |
463 | ce->prop->name); | |
464 | break; | |
465 | } | |
466 | break; | |
467 | ||
468 | case OF_RECONFIG_UPDATE_PROPERTY: | |
469 | /* If the property is in deadprops then it must be removed */ | |
470 | for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) { | |
471 | if (*propp == ce->prop) { | |
472 | *propp = ce->prop->next; | |
473 | ce->prop->next = NULL; | |
474 | break; | |
475 | } | |
476 | } | |
477 | ||
478 | ret = __of_update_property(ce->np, ce->prop, &old_prop); | |
479 | if (ret) { | |
480 | pr_err("%s: update_property failed @%s/%s\n", | |
481 | __func__, ce->np->full_name, | |
482 | ce->prop->name); | |
483 | break; | |
484 | } | |
485 | break; | |
486 | default: | |
487 | ret = -EINVAL; | |
488 | } | |
489 | raw_spin_unlock_irqrestore(&devtree_lock, flags); | |
490 | ||
491 | if (ret) | |
492 | return ret; | |
493 | ||
494 | switch (ce->action) { | |
495 | case OF_RECONFIG_ATTACH_NODE: | |
496 | __of_attach_node_sysfs(ce->np); | |
497 | break; | |
498 | case OF_RECONFIG_DETACH_NODE: | |
499 | __of_detach_node_sysfs(ce->np); | |
500 | break; | |
501 | case OF_RECONFIG_ADD_PROPERTY: | |
502 | /* ignore duplicate names */ | |
503 | __of_add_property_sysfs(ce->np, ce->prop); | |
504 | break; | |
505 | case OF_RECONFIG_REMOVE_PROPERTY: | |
506 | __of_remove_property_sysfs(ce->np, ce->prop); | |
507 | break; | |
508 | case OF_RECONFIG_UPDATE_PROPERTY: | |
509 | __of_update_property_sysfs(ce->np, ce->prop, ce->old_prop); | |
510 | break; | |
511 | } | |
512 | ||
513 | return 0; | |
514 | } | |
515 | ||
516 | static inline int __of_changeset_entry_revert(struct of_changeset_entry *ce) | |
517 | { | |
518 | struct of_changeset_entry ce_inverted; | |
519 | ||
520 | __of_changeset_entry_invert(ce, &ce_inverted); | |
521 | return __of_changeset_entry_apply(&ce_inverted); | |
522 | } | |
523 | ||
524 | /** | |
525 | * of_changeset_init - Initialize a changeset for use | |
526 | * | |
527 | * @ocs: changeset pointer | |
528 | * | |
529 | * Initialize a changeset structure | |
530 | */ | |
531 | void of_changeset_init(struct of_changeset *ocs) | |
532 | { | |
533 | memset(ocs, 0, sizeof(*ocs)); | |
534 | INIT_LIST_HEAD(&ocs->entries); | |
535 | } | |
536 | ||
537 | /** | |
538 | * of_changeset_destroy - Destroy a changeset | |
539 | * | |
540 | * @ocs: changeset pointer | |
541 | * | |
542 | * Destroys a changeset. Note that if a changeset is applied, | |
543 | * its changes to the tree cannot be reverted. | |
544 | */ | |
545 | void of_changeset_destroy(struct of_changeset *ocs) | |
546 | { | |
547 | struct of_changeset_entry *ce, *cen; | |
548 | ||
549 | list_for_each_entry_safe_reverse(ce, cen, &ocs->entries, node) | |
550 | __of_changeset_entry_destroy(ce); | |
551 | } | |
552 | ||
553 | /** | |
554 | * of_changeset_apply - Applies a changeset | |
555 | * | |
556 | * @ocs: changeset pointer | |
557 | * | |
558 | * Applies a changeset to the live tree. | |
559 | * Any side-effects of live tree state changes are applied here on | |
560 | * sucess, like creation/destruction of devices and side-effects | |
561 | * like creation of sysfs properties and directories. | |
562 | * Returns 0 on success, a negative error value in case of an error. | |
563 | * On error the partially applied effects are reverted. | |
564 | */ | |
565 | int of_changeset_apply(struct of_changeset *ocs) | |
566 | { | |
567 | struct of_changeset_entry *ce; | |
568 | int ret; | |
569 | ||
570 | /* perform the rest of the work */ | |
571 | pr_debug("of_changeset: applying...\n"); | |
572 | list_for_each_entry(ce, &ocs->entries, node) { | |
573 | ret = __of_changeset_entry_apply(ce); | |
574 | if (ret) { | |
575 | pr_err("%s: Error applying changeset (%d)\n", __func__, ret); | |
576 | list_for_each_entry_continue_reverse(ce, &ocs->entries, node) | |
577 | __of_changeset_entry_revert(ce); | |
578 | return ret; | |
579 | } | |
580 | } | |
581 | pr_debug("of_changeset: applied, emitting notifiers.\n"); | |
582 | ||
583 | /* drop the global lock while emitting notifiers */ | |
584 | mutex_unlock(&of_mutex); | |
585 | list_for_each_entry(ce, &ocs->entries, node) | |
586 | __of_changeset_entry_notify(ce, 0); | |
587 | mutex_lock(&of_mutex); | |
588 | pr_debug("of_changeset: notifiers sent.\n"); | |
589 | ||
590 | return 0; | |
591 | } | |
592 | ||
593 | /** | |
594 | * of_changeset_revert - Reverts an applied changeset | |
595 | * | |
596 | * @ocs: changeset pointer | |
597 | * | |
598 | * Reverts a changeset returning the state of the tree to what it | |
599 | * was before the application. | |
600 | * Any side-effects like creation/destruction of devices and | |
601 | * removal of sysfs properties and directories are applied. | |
602 | * Returns 0 on success, a negative error value in case of an error. | |
603 | */ | |
604 | int of_changeset_revert(struct of_changeset *ocs) | |
605 | { | |
606 | struct of_changeset_entry *ce; | |
607 | int ret; | |
608 | ||
609 | pr_debug("of_changeset: reverting...\n"); | |
610 | list_for_each_entry_reverse(ce, &ocs->entries, node) { | |
611 | ret = __of_changeset_entry_revert(ce); | |
612 | if (ret) { | |
613 | pr_err("%s: Error reverting changeset (%d)\n", __func__, ret); | |
614 | list_for_each_entry_continue(ce, &ocs->entries, node) | |
615 | __of_changeset_entry_apply(ce); | |
616 | return ret; | |
617 | } | |
618 | } | |
619 | pr_debug("of_changeset: reverted, emitting notifiers.\n"); | |
620 | ||
621 | /* drop the global lock while emitting notifiers */ | |
622 | mutex_unlock(&of_mutex); | |
623 | list_for_each_entry_reverse(ce, &ocs->entries, node) | |
624 | __of_changeset_entry_notify(ce, 1); | |
625 | mutex_lock(&of_mutex); | |
626 | pr_debug("of_changeset: notifiers sent.\n"); | |
627 | ||
628 | return 0; | |
629 | } | |
630 | ||
631 | /** | |
632 | * of_changeset_action - Perform a changeset action | |
633 | * | |
634 | * @ocs: changeset pointer | |
635 | * @action: action to perform | |
636 | * @np: Pointer to device node | |
637 | * @prop: Pointer to property | |
638 | * | |
639 | * On action being one of: | |
640 | * + OF_RECONFIG_ATTACH_NODE | |
641 | * + OF_RECONFIG_DETACH_NODE, | |
642 | * + OF_RECONFIG_ADD_PROPERTY | |
643 | * + OF_RECONFIG_REMOVE_PROPERTY, | |
644 | * + OF_RECONFIG_UPDATE_PROPERTY | |
645 | * Returns 0 on success, a negative error value in case of an error. | |
646 | */ | |
647 | int of_changeset_action(struct of_changeset *ocs, unsigned long action, | |
648 | struct device_node *np, struct property *prop) | |
649 | { | |
650 | struct of_changeset_entry *ce; | |
651 | ||
652 | ce = kzalloc(sizeof(*ce), GFP_KERNEL); | |
653 | if (!ce) { | |
654 | pr_err("%s: Failed to allocate\n", __func__); | |
655 | return -ENOMEM; | |
656 | } | |
657 | /* get a reference to the node */ | |
658 | ce->action = action; | |
659 | ce->np = of_node_get(np); | |
660 | ce->prop = prop; | |
661 | ||
662 | if (action == OF_RECONFIG_UPDATE_PROPERTY && prop) | |
663 | ce->old_prop = of_find_property(np, prop->name, NULL); | |
664 | ||
665 | /* add it to the list */ | |
666 | list_add_tail(&ce->node, &ocs->entries); | |
667 | return 0; | |
668 | } |