]>
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 | ||
00aa3720 GL |
80 | #ifdef DEBUG |
81 | const 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 | ||
f5242e5a | 90 | int of_reconfig_notify(unsigned long action, struct of_reconfig_data *p) |
6afc0dc3 GL |
91 | { |
92 | int rc; | |
00aa3720 | 93 | #ifdef DEBUG |
f5242e5a | 94 | struct of_reconfig_data *pr = p; |
00aa3720 GL |
95 | |
96 | 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], | |
f5242e5a | 100 | pr->dn->full_name); |
00aa3720 GL |
101 | 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; | |
6afc0dc3 | 108 | |
00aa3720 GL |
109 | } |
110 | #endif | |
6afc0dc3 GL |
111 | rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p); |
112 | return notifier_to_errno(rc); | |
113 | } | |
114 | ||
b53a2340 PA |
115 | /* |
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 | */ | |
f5242e5a | 124 | int of_reconfig_get_state_change(unsigned long action, struct of_reconfig_data *pr) |
b53a2340 | 125 | { |
f5242e5a | 126 | struct property *prop, *old_prop = NULL; |
b53a2340 PA |
127 | int is_status, status_state, old_status_state, prev_state, new_state; |
128 | ||
129 | /* figure out if a device should be created or destroyed */ | |
b53a2340 PA |
130 | switch (action) { |
131 | case OF_RECONFIG_ATTACH_NODE: | |
132 | case OF_RECONFIG_DETACH_NODE: | |
f5242e5a | 133 | prop = of_find_property(pr->dn, "status", NULL); |
b53a2340 PA |
134 | break; |
135 | case OF_RECONFIG_ADD_PROPERTY: | |
136 | case OF_RECONFIG_REMOVE_PROPERTY: | |
b53a2340 PA |
137 | prop = pr->prop; |
138 | break; | |
139 | case OF_RECONFIG_UPDATE_PROPERTY: | |
b53a2340 PA |
140 | 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 | } | |
200 | EXPORT_SYMBOL_GPL(of_reconfig_get_state_change); | |
201 | ||
6afc0dc3 | 202 | int of_property_notify(int action, struct device_node *np, |
259092a3 | 203 | struct property *prop, struct property *oldprop) |
6afc0dc3 | 204 | { |
f5242e5a | 205 | struct of_reconfig_data pr; |
6afc0dc3 GL |
206 | |
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; | |
259092a3 | 213 | pr.old_prop = oldprop; |
6afc0dc3 GL |
214 | return of_reconfig_notify(action, &pr); |
215 | } | |
216 | ||
d8c50088 PA |
217 | void __of_attach_node(struct device_node *np) |
218 | { | |
a25095d4 GL |
219 | 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); | |
f76502aa | 228 | if (IS_ENABLED(CONFIG_PPC_PSERIES) && !phandle) |
a25095d4 GL |
229 | phandle = __of_get_property(np, "ibm,phandle", &sz); |
230 | np->phandle = (phandle && (sz >= 4)) ? be32_to_cpup(phandle) : 0; | |
231 | ||
6162dbe4 | 232 | np->child = NULL; |
d8c50088 | 233 | np->sibling = np->parent->child; |
d8c50088 PA |
234 | np->parent->child = np; |
235 | of_node_clear_flag(np, OF_DETACHED); | |
236 | } | |
237 | ||
6afc0dc3 GL |
238 | /** |
239 | * of_attach_node() - Plug a device node into the tree and global list. | |
240 | */ | |
241 | int of_attach_node(struct device_node *np) | |
242 | { | |
f5242e5a | 243 | struct of_reconfig_data rd; |
6afc0dc3 | 244 | unsigned long flags; |
6afc0dc3 | 245 | |
f5242e5a GL |
246 | memset(&rd, 0, sizeof(rd)); |
247 | rd.dn = np; | |
248 | ||
8a2b22a2 | 249 | mutex_lock(&of_mutex); |
6afc0dc3 | 250 | raw_spin_lock_irqsave(&devtree_lock, flags); |
d8c50088 | 251 | __of_attach_node(np); |
6afc0dc3 GL |
252 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
253 | ||
8a2b22a2 GL |
254 | __of_attach_node_sysfs(np); |
255 | mutex_unlock(&of_mutex); | |
259092a3 | 256 | |
f5242e5a | 257 | of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, &rd); |
259092a3 | 258 | |
6afc0dc3 GL |
259 | return 0; |
260 | } | |
261 | ||
d8c50088 | 262 | void __of_detach_node(struct device_node *np) |
6afc0dc3 GL |
263 | { |
264 | struct device_node *parent; | |
6afc0dc3 | 265 | |
d8c50088 PA |
266 | if (WARN_ON(of_node_check_flag(np, OF_DETACHED))) |
267 | return; | |
6afc0dc3 GL |
268 | |
269 | parent = np->parent; | |
d8c50088 PA |
270 | if (WARN_ON(!parent)) |
271 | return; | |
6afc0dc3 | 272 | |
6afc0dc3 GL |
273 | 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); | |
d8c50088 PA |
285 | } |
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 | */ | |
293 | int of_detach_node(struct device_node *np) | |
294 | { | |
f5242e5a | 295 | struct of_reconfig_data rd; |
d8c50088 PA |
296 | unsigned long flags; |
297 | int rc = 0; | |
298 | ||
f5242e5a GL |
299 | memset(&rd, 0, sizeof(rd)); |
300 | rd.dn = np; | |
301 | ||
8a2b22a2 | 302 | mutex_lock(&of_mutex); |
d8c50088 PA |
303 | raw_spin_lock_irqsave(&devtree_lock, flags); |
304 | __of_detach_node(np); | |
6afc0dc3 GL |
305 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
306 | ||
8a2b22a2 GL |
307 | __of_detach_node_sysfs(np); |
308 | mutex_unlock(&of_mutex); | |
259092a3 | 309 | |
f5242e5a | 310 | of_reconfig_notify(OF_RECONFIG_DETACH_NODE, &rd); |
259092a3 | 311 | |
6afc0dc3 GL |
312 | return rc; |
313 | } | |
bb91f923 | 314 | EXPORT_SYMBOL_GPL(of_detach_node); |
6afc0dc3 GL |
315 | |
316 | /** | |
317 | * of_node_release() - release a dynamically allocated node | |
318 | * @kref: kref element of the node to be released | |
319 | * | |
320 | * In of_node_put() this function is passed to kref_put() as the destructor. | |
321 | */ | |
322 | void of_node_release(struct kobject *kobj) | |
323 | { | |
324 | struct device_node *node = kobj_to_device_node(kobj); | |
325 | struct property *prop = node->properties; | |
326 | ||
327 | /* We should never be releasing nodes that haven't been detached. */ | |
328 | if (!of_node_check_flag(node, OF_DETACHED)) { | |
329 | pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name); | |
330 | dump_stack(); | |
331 | return; | |
332 | } | |
333 | ||
334 | if (!of_node_check_flag(node, OF_DYNAMIC)) | |
335 | return; | |
336 | ||
337 | while (prop) { | |
338 | struct property *next = prop->next; | |
339 | kfree(prop->name); | |
340 | kfree(prop->value); | |
341 | kfree(prop); | |
342 | prop = next; | |
343 | ||
344 | if (!prop) { | |
345 | prop = node->deadprops; | |
346 | node->deadprops = NULL; | |
347 | } | |
348 | } | |
349 | kfree(node->full_name); | |
350 | kfree(node->data); | |
351 | kfree(node); | |
352 | } | |
69843396 PA |
353 | |
354 | /** | |
355 | * __of_prop_dup - Copy a property dynamically. | |
356 | * @prop: Property to copy | |
357 | * @allocflags: Allocation flags (typically pass GFP_KERNEL) | |
358 | * | |
359 | * Copy a property by dynamically allocating the memory of both the | |
27b3383a | 360 | * property structure and the property name & contents. The property's |
69843396 PA |
361 | * flags have the OF_DYNAMIC bit set so that we can differentiate between |
362 | * dynamically allocated properties and not. | |
363 | * Returns the newly allocated property or NULL on out of memory error. | |
364 | */ | |
365 | struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags) | |
366 | { | |
367 | struct property *new; | |
368 | ||
369 | new = kzalloc(sizeof(*new), allocflags); | |
370 | if (!new) | |
371 | return NULL; | |
372 | ||
373 | /* | |
374 | * NOTE: There is no check for zero length value. | |
b6ae5dc5 | 375 | * In case of a boolean property, this will allocate a value |
69843396 PA |
376 | * of zero bytes. We do this to work around the use |
377 | * of of_get_property() calls on boolean values. | |
378 | */ | |
379 | new->name = kstrdup(prop->name, allocflags); | |
380 | new->value = kmemdup(prop->value, prop->length, allocflags); | |
381 | new->length = prop->length; | |
382 | if (!new->name || !new->value) | |
383 | goto err_free; | |
384 | ||
385 | /* mark the property as dynamic */ | |
386 | of_property_set_flag(new, OF_DYNAMIC); | |
387 | ||
388 | return new; | |
389 | ||
390 | err_free: | |
391 | kfree(new->name); | |
392 | kfree(new->value); | |
393 | kfree(new); | |
394 | return NULL; | |
395 | } | |
396 | ||
397 | /** | |
e5179581 GL |
398 | * __of_node_dup() - Duplicate or create an empty device node dynamically. |
399 | * @fmt: Format string (plus vargs) for new full name of the device node | |
69843396 | 400 | * |
e5179581 GL |
401 | * Create an device tree node, either by duplicating an empty node or by allocating |
402 | * an empty one suitable for further modification. The node data are | |
403 | * dynamically allocated and all the node flags have the OF_DYNAMIC & | |
404 | * OF_DETACHED bits set. Returns the newly allocated node or NULL on out of | |
405 | * memory error. | |
69843396 | 406 | */ |
e5179581 | 407 | struct device_node *__of_node_dup(const struct device_node *np, const char *fmt, ...) |
69843396 | 408 | { |
ef8bbd73 | 409 | va_list vargs; |
69843396 PA |
410 | struct device_node *node; |
411 | ||
ef8bbd73 | 412 | node = kzalloc(sizeof(*node), GFP_KERNEL); |
69843396 PA |
413 | if (!node) |
414 | return NULL; | |
ef8bbd73 GL |
415 | va_start(vargs, fmt); |
416 | node->full_name = kvasprintf(GFP_KERNEL, fmt, vargs); | |
417 | va_end(vargs); | |
e5179581 GL |
418 | if (!node->full_name) { |
419 | kfree(node); | |
420 | return NULL; | |
421 | } | |
69843396 | 422 | |
ef8bbd73 GL |
423 | of_node_set_flag(node, OF_DYNAMIC); |
424 | of_node_set_flag(node, OF_DETACHED); | |
69843396 PA |
425 | of_node_init(node); |
426 | ||
e5179581 GL |
427 | /* Iterate over and duplicate all properties */ |
428 | if (np) { | |
429 | struct property *pp, *new_pp; | |
430 | for_each_property_of_node(np, pp) { | |
431 | new_pp = __of_prop_dup(pp, GFP_KERNEL); | |
432 | if (!new_pp) | |
433 | goto err_prop; | |
434 | if (__of_add_property(node, new_pp)) { | |
435 | kfree(new_pp->name); | |
436 | kfree(new_pp->value); | |
437 | kfree(new_pp); | |
438 | goto err_prop; | |
439 | } | |
440 | } | |
441 | } | |
69843396 PA |
442 | return node; |
443 | ||
e5179581 GL |
444 | err_prop: |
445 | of_node_put(node); /* Frees the node and properties */ | |
69843396 PA |
446 | return NULL; |
447 | } | |
201c910b PA |
448 | |
449 | static void __of_changeset_entry_destroy(struct of_changeset_entry *ce) | |
450 | { | |
451 | of_node_put(ce->np); | |
452 | list_del(&ce->node); | |
453 | kfree(ce); | |
454 | } | |
455 | ||
456 | #ifdef DEBUG | |
457 | static void __of_changeset_entry_dump(struct of_changeset_entry *ce) | |
458 | { | |
459 | switch (ce->action) { | |
460 | case OF_RECONFIG_ADD_PROPERTY: | |
201c910b | 461 | case OF_RECONFIG_REMOVE_PROPERTY: |
201c910b | 462 | case OF_RECONFIG_UPDATE_PROPERTY: |
00aa3720 GL |
463 | pr_debug("of/cset<%p> %-15s %s/%s\n", ce, action_names[ce->action], |
464 | ce->np->full_name, ce->prop->name); | |
201c910b PA |
465 | break; |
466 | case OF_RECONFIG_ATTACH_NODE: | |
201c910b | 467 | case OF_RECONFIG_DETACH_NODE: |
00aa3720 GL |
468 | pr_debug("of/cset<%p> %-15s %s\n", ce, action_names[ce->action], |
469 | ce->np->full_name); | |
201c910b PA |
470 | break; |
471 | } | |
472 | } | |
473 | #else | |
474 | static inline void __of_changeset_entry_dump(struct of_changeset_entry *ce) | |
475 | { | |
476 | /* empty */ | |
477 | } | |
478 | #endif | |
479 | ||
480 | static void __of_changeset_entry_invert(struct of_changeset_entry *ce, | |
481 | struct of_changeset_entry *rce) | |
482 | { | |
483 | memcpy(rce, ce, sizeof(*rce)); | |
484 | ||
485 | switch (ce->action) { | |
486 | case OF_RECONFIG_ATTACH_NODE: | |
487 | rce->action = OF_RECONFIG_DETACH_NODE; | |
488 | break; | |
489 | case OF_RECONFIG_DETACH_NODE: | |
490 | rce->action = OF_RECONFIG_ATTACH_NODE; | |
491 | break; | |
492 | case OF_RECONFIG_ADD_PROPERTY: | |
493 | rce->action = OF_RECONFIG_REMOVE_PROPERTY; | |
494 | break; | |
495 | case OF_RECONFIG_REMOVE_PROPERTY: | |
496 | rce->action = OF_RECONFIG_ADD_PROPERTY; | |
497 | break; | |
498 | case OF_RECONFIG_UPDATE_PROPERTY: | |
499 | rce->old_prop = ce->prop; | |
500 | rce->prop = ce->old_prop; | |
501 | break; | |
502 | } | |
503 | } | |
504 | ||
505 | static void __of_changeset_entry_notify(struct of_changeset_entry *ce, bool revert) | |
506 | { | |
f5242e5a | 507 | struct of_reconfig_data rd; |
201c910b PA |
508 | struct of_changeset_entry ce_inverted; |
509 | int ret; | |
510 | ||
511 | if (revert) { | |
512 | __of_changeset_entry_invert(ce, &ce_inverted); | |
513 | ce = &ce_inverted; | |
514 | } | |
515 | ||
516 | switch (ce->action) { | |
517 | case OF_RECONFIG_ATTACH_NODE: | |
518 | case OF_RECONFIG_DETACH_NODE: | |
f5242e5a GL |
519 | memset(&rd, 0, sizeof(rd)); |
520 | rd.dn = ce->np; | |
521 | ret = of_reconfig_notify(ce->action, &rd); | |
201c910b PA |
522 | break; |
523 | case OF_RECONFIG_ADD_PROPERTY: | |
524 | case OF_RECONFIG_REMOVE_PROPERTY: | |
525 | case OF_RECONFIG_UPDATE_PROPERTY: | |
526 | ret = of_property_notify(ce->action, ce->np, ce->prop, ce->old_prop); | |
527 | break; | |
528 | default: | |
529 | pr_err("%s: invalid devicetree changeset action: %i\n", __func__, | |
530 | (int)ce->action); | |
531 | return; | |
532 | } | |
533 | ||
534 | if (ret) | |
535 | pr_err("%s: notifier error @%s\n", __func__, ce->np->full_name); | |
536 | } | |
537 | ||
538 | static int __of_changeset_entry_apply(struct of_changeset_entry *ce) | |
539 | { | |
540 | struct property *old_prop, **propp; | |
541 | unsigned long flags; | |
542 | int ret = 0; | |
543 | ||
544 | __of_changeset_entry_dump(ce); | |
545 | ||
546 | raw_spin_lock_irqsave(&devtree_lock, flags); | |
547 | switch (ce->action) { | |
548 | case OF_RECONFIG_ATTACH_NODE: | |
549 | __of_attach_node(ce->np); | |
550 | break; | |
551 | case OF_RECONFIG_DETACH_NODE: | |
552 | __of_detach_node(ce->np); | |
553 | break; | |
554 | case OF_RECONFIG_ADD_PROPERTY: | |
555 | /* If the property is in deadprops then it must be removed */ | |
556 | for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) { | |
557 | if (*propp == ce->prop) { | |
558 | *propp = ce->prop->next; | |
559 | ce->prop->next = NULL; | |
560 | break; | |
561 | } | |
562 | } | |
563 | ||
564 | ret = __of_add_property(ce->np, ce->prop); | |
565 | if (ret) { | |
566 | pr_err("%s: add_property failed @%s/%s\n", | |
567 | __func__, ce->np->full_name, | |
568 | ce->prop->name); | |
569 | break; | |
570 | } | |
571 | break; | |
572 | case OF_RECONFIG_REMOVE_PROPERTY: | |
573 | ret = __of_remove_property(ce->np, ce->prop); | |
574 | if (ret) { | |
575 | pr_err("%s: remove_property failed @%s/%s\n", | |
576 | __func__, ce->np->full_name, | |
577 | ce->prop->name); | |
578 | break; | |
579 | } | |
580 | break; | |
581 | ||
582 | case OF_RECONFIG_UPDATE_PROPERTY: | |
583 | /* If the property is in deadprops then it must be removed */ | |
584 | for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) { | |
585 | if (*propp == ce->prop) { | |
586 | *propp = ce->prop->next; | |
587 | ce->prop->next = NULL; | |
588 | break; | |
589 | } | |
590 | } | |
591 | ||
592 | ret = __of_update_property(ce->np, ce->prop, &old_prop); | |
593 | if (ret) { | |
594 | pr_err("%s: update_property failed @%s/%s\n", | |
595 | __func__, ce->np->full_name, | |
596 | ce->prop->name); | |
597 | break; | |
598 | } | |
599 | break; | |
600 | default: | |
601 | ret = -EINVAL; | |
602 | } | |
603 | raw_spin_unlock_irqrestore(&devtree_lock, flags); | |
604 | ||
605 | if (ret) | |
606 | return ret; | |
607 | ||
608 | switch (ce->action) { | |
609 | case OF_RECONFIG_ATTACH_NODE: | |
610 | __of_attach_node_sysfs(ce->np); | |
611 | break; | |
612 | case OF_RECONFIG_DETACH_NODE: | |
613 | __of_detach_node_sysfs(ce->np); | |
614 | break; | |
615 | case OF_RECONFIG_ADD_PROPERTY: | |
616 | /* ignore duplicate names */ | |
617 | __of_add_property_sysfs(ce->np, ce->prop); | |
618 | break; | |
619 | case OF_RECONFIG_REMOVE_PROPERTY: | |
620 | __of_remove_property_sysfs(ce->np, ce->prop); | |
621 | break; | |
622 | case OF_RECONFIG_UPDATE_PROPERTY: | |
623 | __of_update_property_sysfs(ce->np, ce->prop, ce->old_prop); | |
624 | break; | |
625 | } | |
626 | ||
627 | return 0; | |
628 | } | |
629 | ||
630 | static inline int __of_changeset_entry_revert(struct of_changeset_entry *ce) | |
631 | { | |
632 | struct of_changeset_entry ce_inverted; | |
633 | ||
634 | __of_changeset_entry_invert(ce, &ce_inverted); | |
635 | return __of_changeset_entry_apply(&ce_inverted); | |
636 | } | |
637 | ||
638 | /** | |
639 | * of_changeset_init - Initialize a changeset for use | |
640 | * | |
641 | * @ocs: changeset pointer | |
642 | * | |
643 | * Initialize a changeset structure | |
644 | */ | |
645 | void of_changeset_init(struct of_changeset *ocs) | |
646 | { | |
647 | memset(ocs, 0, sizeof(*ocs)); | |
648 | INIT_LIST_HEAD(&ocs->entries); | |
649 | } | |
18322377 | 650 | EXPORT_SYMBOL_GPL(of_changeset_init); |
201c910b PA |
651 | |
652 | /** | |
653 | * of_changeset_destroy - Destroy a changeset | |
654 | * | |
655 | * @ocs: changeset pointer | |
656 | * | |
657 | * Destroys a changeset. Note that if a changeset is applied, | |
658 | * its changes to the tree cannot be reverted. | |
659 | */ | |
660 | void of_changeset_destroy(struct of_changeset *ocs) | |
661 | { | |
662 | struct of_changeset_entry *ce, *cen; | |
663 | ||
664 | list_for_each_entry_safe_reverse(ce, cen, &ocs->entries, node) | |
665 | __of_changeset_entry_destroy(ce); | |
666 | } | |
18322377 | 667 | EXPORT_SYMBOL_GPL(of_changeset_destroy); |
201c910b | 668 | |
18322377 | 669 | int __of_changeset_apply(struct of_changeset *ocs) |
201c910b PA |
670 | { |
671 | struct of_changeset_entry *ce; | |
672 | int ret; | |
673 | ||
674 | /* perform the rest of the work */ | |
675 | pr_debug("of_changeset: applying...\n"); | |
676 | list_for_each_entry(ce, &ocs->entries, node) { | |
677 | ret = __of_changeset_entry_apply(ce); | |
678 | if (ret) { | |
679 | pr_err("%s: Error applying changeset (%d)\n", __func__, ret); | |
680 | list_for_each_entry_continue_reverse(ce, &ocs->entries, node) | |
681 | __of_changeset_entry_revert(ce); | |
682 | return ret; | |
683 | } | |
684 | } | |
685 | pr_debug("of_changeset: applied, emitting notifiers.\n"); | |
686 | ||
687 | /* drop the global lock while emitting notifiers */ | |
688 | mutex_unlock(&of_mutex); | |
689 | list_for_each_entry(ce, &ocs->entries, node) | |
690 | __of_changeset_entry_notify(ce, 0); | |
691 | mutex_lock(&of_mutex); | |
692 | pr_debug("of_changeset: notifiers sent.\n"); | |
693 | ||
694 | return 0; | |
695 | } | |
696 | ||
697 | /** | |
18322377 | 698 | * of_changeset_apply - Applies a changeset |
201c910b PA |
699 | * |
700 | * @ocs: changeset pointer | |
701 | * | |
18322377 GS |
702 | * Applies a changeset to the live tree. |
703 | * Any side-effects of live tree state changes are applied here on | |
704 | * success, like creation/destruction of devices and side-effects | |
705 | * like creation of sysfs properties and directories. | |
201c910b | 706 | * Returns 0 on success, a negative error value in case of an error. |
18322377 | 707 | * On error the partially applied effects are reverted. |
201c910b | 708 | */ |
18322377 GS |
709 | int of_changeset_apply(struct of_changeset *ocs) |
710 | { | |
711 | int ret; | |
712 | ||
713 | mutex_lock(&of_mutex); | |
714 | ret = __of_changeset_apply(ocs); | |
715 | mutex_unlock(&of_mutex); | |
716 | ||
717 | return ret; | |
718 | } | |
719 | EXPORT_SYMBOL_GPL(of_changeset_apply); | |
720 | ||
721 | int __of_changeset_revert(struct of_changeset *ocs) | |
201c910b PA |
722 | { |
723 | struct of_changeset_entry *ce; | |
724 | int ret; | |
725 | ||
726 | pr_debug("of_changeset: reverting...\n"); | |
727 | list_for_each_entry_reverse(ce, &ocs->entries, node) { | |
728 | ret = __of_changeset_entry_revert(ce); | |
729 | if (ret) { | |
730 | pr_err("%s: Error reverting changeset (%d)\n", __func__, ret); | |
731 | list_for_each_entry_continue(ce, &ocs->entries, node) | |
732 | __of_changeset_entry_apply(ce); | |
733 | return ret; | |
734 | } | |
735 | } | |
736 | pr_debug("of_changeset: reverted, emitting notifiers.\n"); | |
737 | ||
738 | /* drop the global lock while emitting notifiers */ | |
739 | mutex_unlock(&of_mutex); | |
740 | list_for_each_entry_reverse(ce, &ocs->entries, node) | |
741 | __of_changeset_entry_notify(ce, 1); | |
742 | mutex_lock(&of_mutex); | |
743 | pr_debug("of_changeset: notifiers sent.\n"); | |
744 | ||
745 | return 0; | |
746 | } | |
747 | ||
18322377 GS |
748 | /** |
749 | * of_changeset_revert - Reverts an applied changeset | |
750 | * | |
751 | * @ocs: changeset pointer | |
752 | * | |
753 | * Reverts a changeset returning the state of the tree to what it | |
754 | * was before the application. | |
755 | * Any side-effects like creation/destruction of devices and | |
756 | * removal of sysfs properties and directories are applied. | |
757 | * Returns 0 on success, a negative error value in case of an error. | |
758 | */ | |
759 | int of_changeset_revert(struct of_changeset *ocs) | |
760 | { | |
761 | int ret; | |
762 | ||
763 | mutex_lock(&of_mutex); | |
764 | ret = __of_changeset_revert(ocs); | |
765 | mutex_unlock(&of_mutex); | |
766 | ||
767 | return ret; | |
768 | } | |
769 | EXPORT_SYMBOL_GPL(of_changeset_revert); | |
770 | ||
201c910b PA |
771 | /** |
772 | * of_changeset_action - Perform a changeset action | |
773 | * | |
774 | * @ocs: changeset pointer | |
775 | * @action: action to perform | |
776 | * @np: Pointer to device node | |
777 | * @prop: Pointer to property | |
778 | * | |
779 | * On action being one of: | |
780 | * + OF_RECONFIG_ATTACH_NODE | |
781 | * + OF_RECONFIG_DETACH_NODE, | |
782 | * + OF_RECONFIG_ADD_PROPERTY | |
783 | * + OF_RECONFIG_REMOVE_PROPERTY, | |
784 | * + OF_RECONFIG_UPDATE_PROPERTY | |
785 | * Returns 0 on success, a negative error value in case of an error. | |
786 | */ | |
787 | int of_changeset_action(struct of_changeset *ocs, unsigned long action, | |
788 | struct device_node *np, struct property *prop) | |
789 | { | |
790 | struct of_changeset_entry *ce; | |
791 | ||
792 | ce = kzalloc(sizeof(*ce), GFP_KERNEL); | |
793 | if (!ce) { | |
794 | pr_err("%s: Failed to allocate\n", __func__); | |
795 | return -ENOMEM; | |
796 | } | |
797 | /* get a reference to the node */ | |
798 | ce->action = action; | |
799 | ce->np = of_node_get(np); | |
800 | ce->prop = prop; | |
801 | ||
802 | if (action == OF_RECONFIG_UPDATE_PROPERTY && prop) | |
803 | ce->old_prop = of_find_property(np, prop->name, NULL); | |
804 | ||
805 | /* add it to the list */ | |
806 | list_add_tail(&ce->node, &ocs->entries); | |
807 | return 0; | |
808 | } | |
18322377 | 809 | EXPORT_SYMBOL_GPL(of_changeset_action); |