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