1
2
3
4
5
6
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
19
20
21
22
23
24struct device_node *of_node_get(struct device_node *node)
25{
26 if (node)
27 kobject_get(&node->kobj);
28 return node;
29}
30EXPORT_SYMBOL(of_node_get);
31
32
33
34
35
36
37void of_node_put(struct device_node *node)
38{
39 if (node)
40 kobject_put(&node->kobj);
41}
42EXPORT_SYMBOL(of_node_put);
43
44void __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
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
63 of_node_put(np);
64}
65
66static BLOCKING_NOTIFIER_HEAD(of_reconfig_chain);
67
68int of_reconfig_notifier_register(struct notifier_block *nb)
69{
70 return blocking_notifier_chain_register(&of_reconfig_chain, nb);
71}
72EXPORT_SYMBOL_GPL(of_reconfig_notifier_register);
73
74int of_reconfig_notifier_unregister(struct notifier_block *nb)
75{
76 return blocking_notifier_chain_unregister(&of_reconfig_chain, nb);
77}
78EXPORT_SYMBOL_GPL(of_reconfig_notifier_unregister);
79
80#ifdef DEBUG
81const char *action_names[] = {
82 [OF_RECONFIG_ATTACH_NODE] = "ATTACH_NODE",
83 [OF_RECONFIG_DETACH_NODE] = "DETACH_NODE",
84 [OF_RECONFIG_ADD_PROPERTY] = "ADD_PROPERTY",
85 [OF_RECONFIG_REMOVE_PROPERTY] = "REMOVE_PROPERTY",
86 [OF_RECONFIG_UPDATE_PROPERTY] = "UPDATE_PROPERTY",
87};
88#endif
89
90int of_reconfig_notify(unsigned long action, struct of_reconfig_data *p)
91{
92 int rc;
93#ifdef DEBUG
94 struct of_reconfig_data *pr = p;
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],
100 pr->dn->full_name);
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;
108
109 }
110#endif
111 rc = blocking_notifier_call_chain(&of_reconfig_chain, action, p);
112 return notifier_to_errno(rc);
113}
114
115
116
117
118
119
120
121
122
123
124int of_reconfig_get_state_change(unsigned long action, struct of_reconfig_data *pr)
125{
126 struct property *prop, *old_prop = NULL;
127 int is_status, status_state, old_status_state, prev_state, new_state;
128
129
130 switch (action) {
131 case OF_RECONFIG_ATTACH_NODE:
132 case OF_RECONFIG_DETACH_NODE:
133 prop = of_find_property(pr->dn, "status", NULL);
134 break;
135 case OF_RECONFIG_ADD_PROPERTY:
136 case OF_RECONFIG_REMOVE_PROPERTY:
137 prop = pr->prop;
138 break;
139 case OF_RECONFIG_UPDATE_PROPERTY:
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
166 new_state = status_state != 0;
167 break;
168 case OF_RECONFIG_DETACH_NODE:
169
170 prev_state = status_state != 0;
171 new_state = 0;
172 break;
173 case OF_RECONFIG_ADD_PROPERTY:
174 if (is_status) {
175
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
184 new_state = 1;
185 }
186 break;
187 case OF_RECONFIG_UPDATE_PROPERTY:
188 if (is_status) {
189 prev_state = old_status_state != 0;
190 new_state = status_state != 0;
191 }
192 break;
193 }
194
195 if (prev_state == new_state)
196 return OF_RECONFIG_NO_CHANGE;
197
198 return new_state ? OF_RECONFIG_CHANGE_ADD : OF_RECONFIG_CHANGE_REMOVE;
199}
200EXPORT_SYMBOL_GPL(of_reconfig_get_state_change);
201
202int of_property_notify(int action, struct device_node *np,
203 struct property *prop, struct property *oldprop)
204{
205 struct of_reconfig_data pr;
206
207
208 if (!of_node_is_attached(np))
209 return 0;
210
211 pr.dn = np;
212 pr.prop = prop;
213 pr.old_prop = oldprop;
214 return of_reconfig_notify(action, &pr);
215}
216
217void __of_attach_node(struct device_node *np)
218{
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);
228 if (IS_ENABLED(CONFIG_PPC_PSERIES) && !phandle)
229 phandle = __of_get_property(np, "ibm,phandle", &sz);
230 np->phandle = (phandle && (sz >= 4)) ? be32_to_cpup(phandle) : 0;
231
232 np->child = NULL;
233 np->sibling = np->parent->child;
234 np->parent->child = np;
235 of_node_clear_flag(np, OF_DETACHED);
236}
237
238
239
240
241int of_attach_node(struct device_node *np)
242{
243 struct of_reconfig_data rd;
244 unsigned long flags;
245
246 memset(&rd, 0, sizeof(rd));
247 rd.dn = np;
248
249 mutex_lock(&of_mutex);
250 raw_spin_lock_irqsave(&devtree_lock, flags);
251 __of_attach_node(np);
252 raw_spin_unlock_irqrestore(&devtree_lock, flags);
253
254 __of_attach_node_sysfs(np);
255 mutex_unlock(&of_mutex);
256
257 of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, &rd);
258
259 return 0;
260}
261
262void __of_detach_node(struct device_node *np)
263{
264 struct device_node *parent;
265
266 if (WARN_ON(of_node_check_flag(np, OF_DETACHED)))
267 return;
268
269 parent = np->parent;
270 if (WARN_ON(!parent))
271 return;
272
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);
285}
286
287
288
289
290
291
292
293int of_detach_node(struct device_node *np)
294{
295 struct of_reconfig_data rd;
296 unsigned long flags;
297 int rc = 0;
298
299 memset(&rd, 0, sizeof(rd));
300 rd.dn = np;
301
302 mutex_lock(&of_mutex);
303 raw_spin_lock_irqsave(&devtree_lock, flags);
304 __of_detach_node(np);
305 raw_spin_unlock_irqrestore(&devtree_lock, flags);
306
307 __of_detach_node_sysfs(np);
308 mutex_unlock(&of_mutex);
309
310 of_reconfig_notify(OF_RECONFIG_DETACH_NODE, &rd);
311
312 return rc;
313}
314EXPORT_SYMBOL_GPL(of_detach_node);
315
316
317
318
319
320
321
322void 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
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}
353
354
355
356
357
358
359
360
361
362
363
364
365struct 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
375
376
377
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
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
398
399
400
401
402
403
404
405
406
407struct device_node *__of_node_dup(const struct device_node *np, const char *fmt, ...)
408{
409 va_list vargs;
410 struct device_node *node;
411
412 node = kzalloc(sizeof(*node), GFP_KERNEL);
413 if (!node)
414 return NULL;
415 va_start(vargs, fmt);
416 node->full_name = kvasprintf(GFP_KERNEL, fmt, vargs);
417 va_end(vargs);
418 if (!node->full_name) {
419 kfree(node);
420 return NULL;
421 }
422
423 of_node_set_flag(node, OF_DYNAMIC);
424 of_node_set_flag(node, OF_DETACHED);
425 of_node_init(node);
426
427
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 }
442 return node;
443
444 err_prop:
445 of_node_put(node);
446 return NULL;
447}
448
449static 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
457static void __of_changeset_entry_dump(struct of_changeset_entry *ce)
458{
459 switch (ce->action) {
460 case OF_RECONFIG_ADD_PROPERTY:
461 case OF_RECONFIG_REMOVE_PROPERTY:
462 case OF_RECONFIG_UPDATE_PROPERTY:
463 pr_debug("of/cset<%p> %-15s %s/%s\n", ce, action_names[ce->action],
464 ce->np->full_name, ce->prop->name);
465 break;
466 case OF_RECONFIG_ATTACH_NODE:
467 case OF_RECONFIG_DETACH_NODE:
468 pr_debug("of/cset<%p> %-15s %s\n", ce, action_names[ce->action],
469 ce->np->full_name);
470 break;
471 }
472}
473#else
474static inline void __of_changeset_entry_dump(struct of_changeset_entry *ce)
475{
476
477}
478#endif
479
480static 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
502 if (!rce->prop) {
503 rce->action = OF_RECONFIG_REMOVE_PROPERTY;
504 rce->prop = ce->prop;
505 }
506 break;
507 }
508}
509
510static void __of_changeset_entry_notify(struct of_changeset_entry *ce, bool revert)
511{
512 struct of_reconfig_data rd;
513 struct of_changeset_entry ce_inverted;
514 int ret;
515
516 if (revert) {
517 __of_changeset_entry_invert(ce, &ce_inverted);
518 ce = &ce_inverted;
519 }
520
521 switch (ce->action) {
522 case OF_RECONFIG_ATTACH_NODE:
523 case OF_RECONFIG_DETACH_NODE:
524 memset(&rd, 0, sizeof(rd));
525 rd.dn = ce->np;
526 ret = of_reconfig_notify(ce->action, &rd);
527 break;
528 case OF_RECONFIG_ADD_PROPERTY:
529 case OF_RECONFIG_REMOVE_PROPERTY:
530 case OF_RECONFIG_UPDATE_PROPERTY:
531 ret = of_property_notify(ce->action, ce->np, ce->prop, ce->old_prop);
532 break;
533 default:
534 pr_err("%s: invalid devicetree changeset action: %i\n", __func__,
535 (int)ce->action);
536 return;
537 }
538
539 if (ret)
540 pr_err("%s: notifier error @%s\n", __func__, ce->np->full_name);
541}
542
543static int __of_changeset_entry_apply(struct of_changeset_entry *ce)
544{
545 struct property *old_prop, **propp;
546 unsigned long flags;
547 int ret = 0;
548
549 __of_changeset_entry_dump(ce);
550
551 raw_spin_lock_irqsave(&devtree_lock, flags);
552 switch (ce->action) {
553 case OF_RECONFIG_ATTACH_NODE:
554 __of_attach_node(ce->np);
555 break;
556 case OF_RECONFIG_DETACH_NODE:
557 __of_detach_node(ce->np);
558 break;
559 case OF_RECONFIG_ADD_PROPERTY:
560
561 for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
562 if (*propp == ce->prop) {
563 *propp = ce->prop->next;
564 ce->prop->next = NULL;
565 break;
566 }
567 }
568
569 ret = __of_add_property(ce->np, ce->prop);
570 if (ret) {
571 pr_err("%s: add_property failed @%s/%s\n",
572 __func__, ce->np->full_name,
573 ce->prop->name);
574 break;
575 }
576 break;
577 case OF_RECONFIG_REMOVE_PROPERTY:
578 ret = __of_remove_property(ce->np, ce->prop);
579 if (ret) {
580 pr_err("%s: remove_property failed @%s/%s\n",
581 __func__, ce->np->full_name,
582 ce->prop->name);
583 break;
584 }
585 break;
586
587 case OF_RECONFIG_UPDATE_PROPERTY:
588
589 for (propp = &ce->np->deadprops; *propp; propp = &(*propp)->next) {
590 if (*propp == ce->prop) {
591 *propp = ce->prop->next;
592 ce->prop->next = NULL;
593 break;
594 }
595 }
596
597 ret = __of_update_property(ce->np, ce->prop, &old_prop);
598 if (ret) {
599 pr_err("%s: update_property failed @%s/%s\n",
600 __func__, ce->np->full_name,
601 ce->prop->name);
602 break;
603 }
604 break;
605 default:
606 ret = -EINVAL;
607 }
608 raw_spin_unlock_irqrestore(&devtree_lock, flags);
609
610 if (ret)
611 return ret;
612
613 switch (ce->action) {
614 case OF_RECONFIG_ATTACH_NODE:
615 __of_attach_node_sysfs(ce->np);
616 break;
617 case OF_RECONFIG_DETACH_NODE:
618 __of_detach_node_sysfs(ce->np);
619 break;
620 case OF_RECONFIG_ADD_PROPERTY:
621
622 __of_add_property_sysfs(ce->np, ce->prop);
623 break;
624 case OF_RECONFIG_REMOVE_PROPERTY:
625 __of_remove_property_sysfs(ce->np, ce->prop);
626 break;
627 case OF_RECONFIG_UPDATE_PROPERTY:
628 __of_update_property_sysfs(ce->np, ce->prop, ce->old_prop);
629 break;
630 }
631
632 return 0;
633}
634
635static inline int __of_changeset_entry_revert(struct of_changeset_entry *ce)
636{
637 struct of_changeset_entry ce_inverted;
638
639 __of_changeset_entry_invert(ce, &ce_inverted);
640 return __of_changeset_entry_apply(&ce_inverted);
641}
642
643
644
645
646
647
648
649
650void of_changeset_init(struct of_changeset *ocs)
651{
652 memset(ocs, 0, sizeof(*ocs));
653 INIT_LIST_HEAD(&ocs->entries);
654}
655EXPORT_SYMBOL_GPL(of_changeset_init);
656
657
658
659
660
661
662
663
664
665void of_changeset_destroy(struct of_changeset *ocs)
666{
667 struct of_changeset_entry *ce, *cen;
668
669 list_for_each_entry_safe_reverse(ce, cen, &ocs->entries, node)
670 __of_changeset_entry_destroy(ce);
671}
672EXPORT_SYMBOL_GPL(of_changeset_destroy);
673
674int __of_changeset_apply(struct of_changeset *ocs)
675{
676 struct of_changeset_entry *ce;
677 int ret;
678
679
680 pr_debug("of_changeset: applying...\n");
681 list_for_each_entry(ce, &ocs->entries, node) {
682 ret = __of_changeset_entry_apply(ce);
683 if (ret) {
684 pr_err("%s: Error applying changeset (%d)\n", __func__, ret);
685 list_for_each_entry_continue_reverse(ce, &ocs->entries, node)
686 __of_changeset_entry_revert(ce);
687 return ret;
688 }
689 }
690 pr_debug("of_changeset: applied, emitting notifiers.\n");
691
692
693 mutex_unlock(&of_mutex);
694 list_for_each_entry(ce, &ocs->entries, node)
695 __of_changeset_entry_notify(ce, 0);
696 mutex_lock(&of_mutex);
697 pr_debug("of_changeset: notifiers sent.\n");
698
699 return 0;
700}
701
702
703
704
705
706
707
708
709
710
711
712
713
714int of_changeset_apply(struct of_changeset *ocs)
715{
716 int ret;
717
718 mutex_lock(&of_mutex);
719 ret = __of_changeset_apply(ocs);
720 mutex_unlock(&of_mutex);
721
722 return ret;
723}
724EXPORT_SYMBOL_GPL(of_changeset_apply);
725
726int __of_changeset_revert(struct of_changeset *ocs)
727{
728 struct of_changeset_entry *ce;
729 int ret;
730
731 pr_debug("of_changeset: reverting...\n");
732 list_for_each_entry_reverse(ce, &ocs->entries, node) {
733 ret = __of_changeset_entry_revert(ce);
734 if (ret) {
735 pr_err("%s: Error reverting changeset (%d)\n", __func__, ret);
736 list_for_each_entry_continue(ce, &ocs->entries, node)
737 __of_changeset_entry_apply(ce);
738 return ret;
739 }
740 }
741 pr_debug("of_changeset: reverted, emitting notifiers.\n");
742
743
744 mutex_unlock(&of_mutex);
745 list_for_each_entry_reverse(ce, &ocs->entries, node)
746 __of_changeset_entry_notify(ce, 1);
747 mutex_lock(&of_mutex);
748 pr_debug("of_changeset: notifiers sent.\n");
749
750 return 0;
751}
752
753
754
755
756
757
758
759
760
761
762
763
764int of_changeset_revert(struct of_changeset *ocs)
765{
766 int ret;
767
768 mutex_lock(&of_mutex);
769 ret = __of_changeset_revert(ocs);
770 mutex_unlock(&of_mutex);
771
772 return ret;
773}
774EXPORT_SYMBOL_GPL(of_changeset_revert);
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792int of_changeset_action(struct of_changeset *ocs, unsigned long action,
793 struct device_node *np, struct property *prop)
794{
795 struct of_changeset_entry *ce;
796
797 ce = kzalloc(sizeof(*ce), GFP_KERNEL);
798 if (!ce) {
799 pr_err("%s: Failed to allocate\n", __func__);
800 return -ENOMEM;
801 }
802
803 ce->action = action;
804 ce->np = of_node_get(np);
805 ce->prop = prop;
806
807 if (action == OF_RECONFIG_UPDATE_PROPERTY && prop)
808 ce->old_prop = of_find_property(np, prop->name, NULL);
809
810
811 list_add_tail(&ce->node, &ocs->entries);
812 return 0;
813}
814EXPORT_SYMBOL_GPL(of_changeset_action);
815