]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/of/dynamic.c
of: Use vargs in __of_node_alloc
[karo-tx-linux.git] / drivers / of / dynamic.c
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
44 void __of_detach_node_sysfs(struct device_node *np)
45 {
46         struct property *pp;
47
48         if (!IS_ENABLED(CONFIG_SYSFS))
49                 return;
50
51         BUG_ON(!of_node_is_initialized(np));
52         if (!of_kset)
53                 return;
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,
89                        struct property *prop, struct property *oldprop)
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;
99         pr.old_prop = oldprop;
100         return of_reconfig_notify(action, &pr);
101 }
102
103 void __of_attach_node(struct device_node *np)
104 {
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
118         np->child = NULL;
119         np->sibling = np->parent->child;
120         np->parent->child = np;
121         of_node_clear_flag(np, OF_DETACHED);
122 }
123
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;
130
131         mutex_lock(&of_mutex);
132         raw_spin_lock_irqsave(&devtree_lock, flags);
133         __of_attach_node(np);
134         raw_spin_unlock_irqrestore(&devtree_lock, flags);
135
136         __of_attach_node_sysfs(np);
137         mutex_unlock(&of_mutex);
138
139         of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, np);
140
141         return 0;
142 }
143
144 void __of_detach_node(struct device_node *np)
145 {
146         struct device_node *parent;
147
148         if (WARN_ON(of_node_check_flag(np, OF_DETACHED)))
149                 return;
150
151         parent = np->parent;
152         if (WARN_ON(!parent))
153                 return;
154
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);
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
180         mutex_lock(&of_mutex);
181         raw_spin_lock_irqsave(&devtree_lock, flags);
182         __of_detach_node(np);
183         raw_spin_unlock_irqrestore(&devtree_lock, flags);
184
185         __of_detach_node_sysfs(np);
186         mutex_unlock(&of_mutex);
187
188         of_reconfig_notify(OF_RECONFIG_DETACH_NODE, np);
189
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 }
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
237  * property structure and the property name & contents. The property's
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.
252          * In case of a boolean property, this will allocate a value
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 /**
275  * __of_node_alloc() - Create an empty device node dynamically.
276  * @full_name:  Full name of the new device node
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  */
283 struct device_node *__of_node_alloc(const char *fmt, ...)
284 {
285         va_list vargs;
286         struct device_node *node;
287
288         node = kzalloc(sizeof(*node), GFP_KERNEL);
289         if (!node)
290                 return NULL;
291         va_start(vargs, fmt);
292         node->full_name = kvasprintf(GFP_KERNEL, fmt, vargs);
293         va_end(vargs);
294         if (!node->full_name)
295                 goto err_free;
296
297         of_node_set_flag(node, OF_DYNAMIC);
298         of_node_set_flag(node, OF_DETACHED);
299         of_node_init(node);
300
301         return node;
302
303  err_free:
304         kfree(node);
305         return NULL;
306 }
307
308 static void __of_changeset_entry_destroy(struct of_changeset_entry *ce)
309 {
310         of_node_put(ce->np);
311         list_del(&ce->node);
312         kfree(ce);
313 }
314
315 #ifdef DEBUG
316 static void __of_changeset_entry_dump(struct of_changeset_entry *ce)
317 {
318         switch (ce->action) {
319         case OF_RECONFIG_ADD_PROPERTY:
320                 pr_debug("%p: %s %s/%s\n",
321                         ce, "ADD_PROPERTY   ", ce->np->full_name,
322                         ce->prop->name);
323                 break;
324         case OF_RECONFIG_REMOVE_PROPERTY:
325                 pr_debug("%p: %s %s/%s\n",
326                         ce, "REMOVE_PROPERTY", ce->np->full_name,
327                         ce->prop->name);
328                 break;
329         case OF_RECONFIG_UPDATE_PROPERTY:
330                 pr_debug("%p: %s %s/%s\n",
331                         ce, "UPDATE_PROPERTY", ce->np->full_name,
332                         ce->prop->name);
333                 break;
334         case OF_RECONFIG_ATTACH_NODE:
335                 pr_debug("%p: %s %s\n",
336                         ce, "ATTACH_NODE    ", ce->np->full_name);
337                 break;
338         case OF_RECONFIG_DETACH_NODE:
339                 pr_debug("%p: %s %s\n",
340                         ce, "DETACH_NODE    ", ce->np->full_name);
341                 break;
342         }
343 }
344 #else
345 static inline void __of_changeset_entry_dump(struct of_changeset_entry *ce)
346 {
347         /* empty */
348 }
349 #endif
350
351 static void __of_changeset_entry_invert(struct of_changeset_entry *ce,
352                                           struct of_changeset_entry *rce)
353 {
354         memcpy(rce, ce, sizeof(*rce));
355
356         switch (ce->action) {
357         case OF_RECONFIG_ATTACH_NODE:
358                 rce->action = OF_RECONFIG_DETACH_NODE;
359                 break;
360         case OF_RECONFIG_DETACH_NODE:
361                 rce->action = OF_RECONFIG_ATTACH_NODE;
362                 break;
363         case OF_RECONFIG_ADD_PROPERTY:
364                 rce->action = OF_RECONFIG_REMOVE_PROPERTY;
365                 break;
366         case OF_RECONFIG_REMOVE_PROPERTY:
367                 rce->action = OF_RECONFIG_ADD_PROPERTY;
368                 break;
369         case OF_RECONFIG_UPDATE_PROPERTY:
370                 rce->old_prop = ce->prop;
371                 rce->prop = ce->old_prop;
372                 break;
373         }
374 }
375
376 static void __of_changeset_entry_notify(struct of_changeset_entry *ce, bool revert)
377 {
378         struct of_changeset_entry ce_inverted;
379         int ret;
380
381         if (revert) {
382                 __of_changeset_entry_invert(ce, &ce_inverted);
383                 ce = &ce_inverted;
384         }
385
386         switch (ce->action) {
387         case OF_RECONFIG_ATTACH_NODE:
388         case OF_RECONFIG_DETACH_NODE:
389                 ret = of_reconfig_notify(ce->action, ce->np);
390                 break;
391         case OF_RECONFIG_ADD_PROPERTY:
392         case OF_RECONFIG_REMOVE_PROPERTY:
393         case OF_RECONFIG_UPDATE_PROPERTY:
394                 ret = of_property_notify(ce->action, ce->np, ce->prop, ce->old_prop);
395                 break;
396         default:
397                 pr_err("%s: invalid devicetree changeset action: %i\n", __func__,
398                         (int)ce->action);
399                 return;
400         }
401
402         if (ret)
403                 pr_err("%s: notifier error @%s\n", __func__, ce->np->full_name);
404 }
405
406 static int __of_changeset_entry_apply(struct of_changeset_entry *ce)
407 {
408         struct property *old_prop, **propp;
409         unsigned long flags;
410         int ret = 0;
411
412         __of_changeset_entry_dump(ce);
413
414         raw_spin_lock_irqsave(&devtree_lock, flags);
415         switch (ce->action) {
416         case OF_RECONFIG_ATTACH_NODE:
417                 __of_attach_node(ce->np);
418                 break;
419         case OF_RECONFIG_DETACH_NODE:
420                 __of_detach_node(ce->np);
421                 break;
422         case OF_RECONFIG_ADD_PROPERTY:
423                 /* If the property is in deadprops then it must be removed */
424                 for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
425                         if (*propp == ce->prop) {
426                                 *propp = ce->prop->next;
427                                 ce->prop->next = NULL;
428                                 break;
429                         }
430                 }
431
432                 ret = __of_add_property(ce->np, ce->prop);
433                 if (ret) {
434                         pr_err("%s: add_property failed @%s/%s\n",
435                                 __func__, ce->np->full_name,
436                                 ce->prop->name);
437                         break;
438                 }
439                 break;
440         case OF_RECONFIG_REMOVE_PROPERTY:
441                 ret = __of_remove_property(ce->np, ce->prop);
442                 if (ret) {
443                         pr_err("%s: remove_property failed @%s/%s\n",
444                                 __func__, ce->np->full_name,
445                                 ce->prop->name);
446                         break;
447                 }
448                 break;
449
450         case OF_RECONFIG_UPDATE_PROPERTY:
451                 /* If the property is in deadprops then it must be removed */
452                 for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
453                         if (*propp == ce->prop) {
454                                 *propp = ce->prop->next;
455                                 ce->prop->next = NULL;
456                                 break;
457                         }
458                 }
459
460                 ret = __of_update_property(ce->np, ce->prop, &old_prop);
461                 if (ret) {
462                         pr_err("%s: update_property failed @%s/%s\n",
463                                 __func__, ce->np->full_name,
464                                 ce->prop->name);
465                         break;
466                 }
467                 break;
468         default:
469                 ret = -EINVAL;
470         }
471         raw_spin_unlock_irqrestore(&devtree_lock, flags);
472
473         if (ret)
474                 return ret;
475
476         switch (ce->action) {
477         case OF_RECONFIG_ATTACH_NODE:
478                 __of_attach_node_sysfs(ce->np);
479                 break;
480         case OF_RECONFIG_DETACH_NODE:
481                 __of_detach_node_sysfs(ce->np);
482                 break;
483         case OF_RECONFIG_ADD_PROPERTY:
484                 /* ignore duplicate names */
485                 __of_add_property_sysfs(ce->np, ce->prop);
486                 break;
487         case OF_RECONFIG_REMOVE_PROPERTY:
488                 __of_remove_property_sysfs(ce->np, ce->prop);
489                 break;
490         case OF_RECONFIG_UPDATE_PROPERTY:
491                 __of_update_property_sysfs(ce->np, ce->prop, ce->old_prop);
492                 break;
493         }
494
495         return 0;
496 }
497
498 static inline int __of_changeset_entry_revert(struct of_changeset_entry *ce)
499 {
500         struct of_changeset_entry ce_inverted;
501
502         __of_changeset_entry_invert(ce, &ce_inverted);
503         return __of_changeset_entry_apply(&ce_inverted);
504 }
505
506 /**
507  * of_changeset_init - Initialize a changeset for use
508  *
509  * @ocs:        changeset pointer
510  *
511  * Initialize a changeset structure
512  */
513 void of_changeset_init(struct of_changeset *ocs)
514 {
515         memset(ocs, 0, sizeof(*ocs));
516         INIT_LIST_HEAD(&ocs->entries);
517 }
518
519 /**
520  * of_changeset_destroy - Destroy a changeset
521  *
522  * @ocs:        changeset pointer
523  *
524  * Destroys a changeset. Note that if a changeset is applied,
525  * its changes to the tree cannot be reverted.
526  */
527 void of_changeset_destroy(struct of_changeset *ocs)
528 {
529         struct of_changeset_entry *ce, *cen;
530
531         list_for_each_entry_safe_reverse(ce, cen, &ocs->entries, node)
532                 __of_changeset_entry_destroy(ce);
533 }
534
535 /**
536  * of_changeset_apply - Applies a changeset
537  *
538  * @ocs:        changeset pointer
539  *
540  * Applies a changeset to the live tree.
541  * Any side-effects of live tree state changes are applied here on
542  * sucess, like creation/destruction of devices and side-effects
543  * like creation of sysfs properties and directories.
544  * Returns 0 on success, a negative error value in case of an error.
545  * On error the partially applied effects are reverted.
546  */
547 int of_changeset_apply(struct of_changeset *ocs)
548 {
549         struct of_changeset_entry *ce;
550         int ret;
551
552         /* perform the rest of the work */
553         pr_debug("of_changeset: applying...\n");
554         list_for_each_entry(ce, &ocs->entries, node) {
555                 ret = __of_changeset_entry_apply(ce);
556                 if (ret) {
557                         pr_err("%s: Error applying changeset (%d)\n", __func__, ret);
558                         list_for_each_entry_continue_reverse(ce, &ocs->entries, node)
559                                 __of_changeset_entry_revert(ce);
560                         return ret;
561                 }
562         }
563         pr_debug("of_changeset: applied, emitting notifiers.\n");
564
565         /* drop the global lock while emitting notifiers */
566         mutex_unlock(&of_mutex);
567         list_for_each_entry(ce, &ocs->entries, node)
568                 __of_changeset_entry_notify(ce, 0);
569         mutex_lock(&of_mutex);
570         pr_debug("of_changeset: notifiers sent.\n");
571
572         return 0;
573 }
574
575 /**
576  * of_changeset_revert - Reverts an applied changeset
577  *
578  * @ocs:        changeset pointer
579  *
580  * Reverts a changeset returning the state of the tree to what it
581  * was before the application.
582  * Any side-effects like creation/destruction of devices and
583  * removal of sysfs properties and directories are applied.
584  * Returns 0 on success, a negative error value in case of an error.
585  */
586 int of_changeset_revert(struct of_changeset *ocs)
587 {
588         struct of_changeset_entry *ce;
589         int ret;
590
591         pr_debug("of_changeset: reverting...\n");
592         list_for_each_entry_reverse(ce, &ocs->entries, node) {
593                 ret = __of_changeset_entry_revert(ce);
594                 if (ret) {
595                         pr_err("%s: Error reverting changeset (%d)\n", __func__, ret);
596                         list_for_each_entry_continue(ce, &ocs->entries, node)
597                                 __of_changeset_entry_apply(ce);
598                         return ret;
599                 }
600         }
601         pr_debug("of_changeset: reverted, emitting notifiers.\n");
602
603         /* drop the global lock while emitting notifiers */
604         mutex_unlock(&of_mutex);
605         list_for_each_entry_reverse(ce, &ocs->entries, node)
606                 __of_changeset_entry_notify(ce, 1);
607         mutex_lock(&of_mutex);
608         pr_debug("of_changeset: notifiers sent.\n");
609
610         return 0;
611 }
612
613 /**
614  * of_changeset_action - Perform a changeset action
615  *
616  * @ocs:        changeset pointer
617  * @action:     action to perform
618  * @np:         Pointer to device node
619  * @prop:       Pointer to property
620  *
621  * On action being one of:
622  * + OF_RECONFIG_ATTACH_NODE
623  * + OF_RECONFIG_DETACH_NODE,
624  * + OF_RECONFIG_ADD_PROPERTY
625  * + OF_RECONFIG_REMOVE_PROPERTY,
626  * + OF_RECONFIG_UPDATE_PROPERTY
627  * Returns 0 on success, a negative error value in case of an error.
628  */
629 int of_changeset_action(struct of_changeset *ocs, unsigned long action,
630                 struct device_node *np, struct property *prop)
631 {
632         struct of_changeset_entry *ce;
633
634         ce = kzalloc(sizeof(*ce), GFP_KERNEL);
635         if (!ce) {
636                 pr_err("%s: Failed to allocate\n", __func__);
637                 return -ENOMEM;
638         }
639         /* get a reference to the node */
640         ce->action = action;
641         ce->np = of_node_get(np);
642         ce->prop = prop;
643
644         if (action == OF_RECONFIG_UPDATE_PROPERTY && prop)
645                 ce->old_prop = of_find_property(np, prop->name, NULL);
646
647         /* add it to the list */
648         list_add_tail(&ce->node, &ocs->entries);
649         return 0;
650 }