1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/module.h>
21#include <linux/of.h>
22#include <linux/spinlock.h>
23#include <linux/slab.h>
24#include <linux/proc_fs.h>
25
26struct device_node *allnodes;
27struct device_node *of_chosen;
28
29
30
31
32DEFINE_RWLOCK(devtree_lock);
33
34int of_n_addr_cells(struct device_node *np)
35{
36 const __be32 *ip;
37
38 do {
39 if (np->parent)
40 np = np->parent;
41 ip = of_get_property(np, "#address-cells", NULL);
42 if (ip)
43 return be32_to_cpup(ip);
44 } while (np->parent);
45
46 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
47}
48EXPORT_SYMBOL(of_n_addr_cells);
49
50int of_n_size_cells(struct device_node *np)
51{
52 const __be32 *ip;
53
54 do {
55 if (np->parent)
56 np = np->parent;
57 ip = of_get_property(np, "#size-cells", NULL);
58 if (ip)
59 return be32_to_cpup(ip);
60 } while (np->parent);
61
62 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
63}
64EXPORT_SYMBOL(of_n_size_cells);
65
66#if !defined(CONFIG_SPARC)
67
68
69
70
71
72
73
74struct device_node *of_node_get(struct device_node *node)
75{
76 if (node)
77 kref_get(&node->kref);
78 return node;
79}
80EXPORT_SYMBOL(of_node_get);
81
82static inline struct device_node *kref_to_device_node(struct kref *kref)
83{
84 return container_of(kref, struct device_node, kref);
85}
86
87
88
89
90
91
92
93
94static void of_node_release(struct kref *kref)
95{
96 struct device_node *node = kref_to_device_node(kref);
97 struct property *prop = node->properties;
98
99
100 if (!of_node_check_flag(node, OF_DETACHED)) {
101 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
102 dump_stack();
103 kref_init(&node->kref);
104 return;
105 }
106
107 if (!of_node_check_flag(node, OF_DYNAMIC))
108 return;
109
110 while (prop) {
111 struct property *next = prop->next;
112 kfree(prop->name);
113 kfree(prop->value);
114 kfree(prop);
115 prop = next;
116
117 if (!prop) {
118 prop = node->deadprops;
119 node->deadprops = NULL;
120 }
121 }
122 kfree(node->full_name);
123 kfree(node->data);
124 kfree(node);
125}
126
127
128
129
130
131
132
133void of_node_put(struct device_node *node)
134{
135 if (node)
136 kref_put(&node->kref, of_node_release);
137}
138EXPORT_SYMBOL(of_node_put);
139#endif
140
141struct property *of_find_property(const struct device_node *np,
142 const char *name,
143 int *lenp)
144{
145 struct property *pp;
146
147 if (!np)
148 return NULL;
149
150 read_lock(&devtree_lock);
151 for (pp = np->properties; pp != 0; pp = pp->next) {
152 if (of_prop_cmp(pp->name, name) == 0) {
153 if (lenp != 0)
154 *lenp = pp->length;
155 break;
156 }
157 }
158 read_unlock(&devtree_lock);
159
160 return pp;
161}
162EXPORT_SYMBOL(of_find_property);
163
164
165
166
167
168
169
170
171
172struct device_node *of_find_all_nodes(struct device_node *prev)
173{
174 struct device_node *np;
175
176 read_lock(&devtree_lock);
177 np = prev ? prev->allnext : allnodes;
178 for (; np != NULL; np = np->allnext)
179 if (of_node_get(np))
180 break;
181 of_node_put(prev);
182 read_unlock(&devtree_lock);
183 return np;
184}
185EXPORT_SYMBOL(of_find_all_nodes);
186
187
188
189
190
191const void *of_get_property(const struct device_node *np, const char *name,
192 int *lenp)
193{
194 struct property *pp = of_find_property(np, name, lenp);
195
196 return pp ? pp->value : NULL;
197}
198EXPORT_SYMBOL(of_get_property);
199
200
201
202
203int of_device_is_compatible(const struct device_node *device,
204 const char *compat)
205{
206 const char* cp;
207 int cplen, l;
208
209 cp = of_get_property(device, "compatible", &cplen);
210 if (cp == NULL)
211 return 0;
212 while (cplen > 0) {
213 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
214 return 1;
215 l = strlen(cp) + 1;
216 cp += l;
217 cplen -= l;
218 }
219
220 return 0;
221}
222EXPORT_SYMBOL(of_device_is_compatible);
223
224
225
226
227
228
229
230
231int of_machine_is_compatible(const char *compat)
232{
233 struct device_node *root;
234 int rc = 0;
235
236 root = of_find_node_by_path("/");
237 if (root) {
238 rc = of_device_is_compatible(root, compat);
239 of_node_put(root);
240 }
241 return rc;
242}
243EXPORT_SYMBOL(of_machine_is_compatible);
244
245
246
247
248
249
250
251
252
253int of_device_is_available(const struct device_node *device)
254{
255 const char *status;
256 int statlen;
257
258 status = of_get_property(device, "status", &statlen);
259 if (status == NULL)
260 return 1;
261
262 if (statlen > 0) {
263 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
264 return 1;
265 }
266
267 return 0;
268}
269EXPORT_SYMBOL(of_device_is_available);
270
271
272
273
274
275
276
277
278struct device_node *of_get_parent(const struct device_node *node)
279{
280 struct device_node *np;
281
282 if (!node)
283 return NULL;
284
285 read_lock(&devtree_lock);
286 np = of_node_get(node->parent);
287 read_unlock(&devtree_lock);
288 return np;
289}
290EXPORT_SYMBOL(of_get_parent);
291
292
293
294
295
296
297
298
299
300
301
302
303struct device_node *of_get_next_parent(struct device_node *node)
304{
305 struct device_node *parent;
306
307 if (!node)
308 return NULL;
309
310 read_lock(&devtree_lock);
311 parent = of_node_get(node->parent);
312 of_node_put(node);
313 read_unlock(&devtree_lock);
314 return parent;
315}
316
317
318
319
320
321
322
323
324
325struct device_node *of_get_next_child(const struct device_node *node,
326 struct device_node *prev)
327{
328 struct device_node *next;
329
330 read_lock(&devtree_lock);
331 next = prev ? prev->sibling : node->child;
332 for (; next; next = next->sibling)
333 if (of_node_get(next))
334 break;
335 of_node_put(prev);
336 read_unlock(&devtree_lock);
337 return next;
338}
339EXPORT_SYMBOL(of_get_next_child);
340
341
342
343
344
345
346
347
348struct device_node *of_find_node_by_path(const char *path)
349{
350 struct device_node *np = allnodes;
351
352 read_lock(&devtree_lock);
353 for (; np; np = np->allnext) {
354 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
355 && of_node_get(np))
356 break;
357 }
358 read_unlock(&devtree_lock);
359 return np;
360}
361EXPORT_SYMBOL(of_find_node_by_path);
362
363
364
365
366
367
368
369
370
371
372
373
374struct device_node *of_find_node_by_name(struct device_node *from,
375 const char *name)
376{
377 struct device_node *np;
378
379 read_lock(&devtree_lock);
380 np = from ? from->allnext : allnodes;
381 for (; np; np = np->allnext)
382 if (np->name && (of_node_cmp(np->name, name) == 0)
383 && of_node_get(np))
384 break;
385 of_node_put(from);
386 read_unlock(&devtree_lock);
387 return np;
388}
389EXPORT_SYMBOL(of_find_node_by_name);
390
391
392
393
394
395
396
397
398
399
400
401
402
403struct device_node *of_find_node_by_type(struct device_node *from,
404 const char *type)
405{
406 struct device_node *np;
407
408 read_lock(&devtree_lock);
409 np = from ? from->allnext : allnodes;
410 for (; np; np = np->allnext)
411 if (np->type && (of_node_cmp(np->type, type) == 0)
412 && of_node_get(np))
413 break;
414 of_node_put(from);
415 read_unlock(&devtree_lock);
416 return np;
417}
418EXPORT_SYMBOL(of_find_node_by_type);
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434struct device_node *of_find_compatible_node(struct device_node *from,
435 const char *type, const char *compatible)
436{
437 struct device_node *np;
438
439 read_lock(&devtree_lock);
440 np = from ? from->allnext : allnodes;
441 for (; np; np = np->allnext) {
442 if (type
443 && !(np->type && (of_node_cmp(np->type, type) == 0)))
444 continue;
445 if (of_device_is_compatible(np, compatible) && of_node_get(np))
446 break;
447 }
448 of_node_put(from);
449 read_unlock(&devtree_lock);
450 return np;
451}
452EXPORT_SYMBOL(of_find_compatible_node);
453
454
455
456
457
458
459
460
461
462
463
464
465
466struct device_node *of_find_node_with_property(struct device_node *from,
467 const char *prop_name)
468{
469 struct device_node *np;
470 struct property *pp;
471
472 read_lock(&devtree_lock);
473 np = from ? from->allnext : allnodes;
474 for (; np; np = np->allnext) {
475 for (pp = np->properties; pp != 0; pp = pp->next) {
476 if (of_prop_cmp(pp->name, prop_name) == 0) {
477 of_node_get(np);
478 goto out;
479 }
480 }
481 }
482out:
483 of_node_put(from);
484 read_unlock(&devtree_lock);
485 return np;
486}
487EXPORT_SYMBOL(of_find_node_with_property);
488
489
490
491
492
493
494
495
496const struct of_device_id *of_match_node(const struct of_device_id *matches,
497 const struct device_node *node)
498{
499 if (!matches)
500 return NULL;
501
502 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
503 int match = 1;
504 if (matches->name[0])
505 match &= node->name
506 && !strcmp(matches->name, node->name);
507 if (matches->type[0])
508 match &= node->type
509 && !strcmp(matches->type, node->type);
510 if (matches->compatible[0])
511 match &= of_device_is_compatible(node,
512 matches->compatible);
513 if (match)
514 return matches;
515 matches++;
516 }
517 return NULL;
518}
519EXPORT_SYMBOL(of_match_node);
520
521
522
523
524
525
526
527
528
529
530
531
532
533struct device_node *of_find_matching_node(struct device_node *from,
534 const struct of_device_id *matches)
535{
536 struct device_node *np;
537
538 read_lock(&devtree_lock);
539 np = from ? from->allnext : allnodes;
540 for (; np; np = np->allnext) {
541 if (of_match_node(matches, np) && of_node_get(np))
542 break;
543 }
544 of_node_put(from);
545 read_unlock(&devtree_lock);
546 return np;
547}
548EXPORT_SYMBOL(of_find_matching_node);
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563int of_modalias_node(struct device_node *node, char *modalias, int len)
564{
565 const char *compatible, *p;
566 int cplen;
567
568 compatible = of_get_property(node, "compatible", &cplen);
569 if (!compatible || strlen(compatible) > cplen)
570 return -ENODEV;
571 p = strchr(compatible, ',');
572 strlcpy(modalias, p ? p + 1 : compatible, len);
573 return 0;
574}
575EXPORT_SYMBOL_GPL(of_modalias_node);
576
577
578
579
580
581
582
583
584struct device_node *of_find_node_by_phandle(phandle handle)
585{
586 struct device_node *np;
587
588 read_lock(&devtree_lock);
589 for (np = allnodes; np; np = np->allnext)
590 if (np->phandle == handle)
591 break;
592 of_node_get(np);
593 read_unlock(&devtree_lock);
594 return np;
595}
596EXPORT_SYMBOL(of_find_node_by_phandle);
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613int of_property_read_u32_array(const struct device_node *np,
614 const char *propname, u32 *out_values,
615 size_t sz)
616{
617 struct property *prop = of_find_property(np, propname, NULL);
618 const __be32 *val;
619
620 if (!prop)
621 return -EINVAL;
622 if (!prop->value)
623 return -ENODATA;
624 if ((sz * sizeof(*out_values)) > prop->length)
625 return -EOVERFLOW;
626
627 val = prop->value;
628 while (sz--)
629 *out_values++ = be32_to_cpup(val++);
630 return 0;
631}
632EXPORT_SYMBOL_GPL(of_property_read_u32_array);
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649int of_property_read_string(struct device_node *np, const char *propname,
650 const char **out_string)
651{
652 struct property *prop = of_find_property(np, propname, NULL);
653 if (!prop)
654 return -EINVAL;
655 if (!prop->value)
656 return -ENODATA;
657 if (strnlen(prop->value, prop->length) >= prop->length)
658 return -EILSEQ;
659 *out_string = prop->value;
660 return 0;
661}
662EXPORT_SYMBOL_GPL(of_property_read_string);
663
664
665
666
667
668
669
670
671
672
673
674struct device_node *
675of_parse_phandle(struct device_node *np, const char *phandle_name, int index)
676{
677 const __be32 *phandle;
678 int size;
679
680 phandle = of_get_property(np, phandle_name, &size);
681 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
682 return NULL;
683
684 return of_find_node_by_phandle(be32_to_cpup(phandle + index));
685}
686EXPORT_SYMBOL(of_parse_phandle);
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718int of_parse_phandles_with_args(struct device_node *np, const char *list_name,
719 const char *cells_name, int index,
720 struct device_node **out_node,
721 const void **out_args)
722{
723 int ret = -EINVAL;
724 const __be32 *list;
725 const __be32 *list_end;
726 int size;
727 int cur_index = 0;
728 struct device_node *node = NULL;
729 const void *args = NULL;
730
731 list = of_get_property(np, list_name, &size);
732 if (!list) {
733 ret = -ENOENT;
734 goto err0;
735 }
736 list_end = list + size / sizeof(*list);
737
738 while (list < list_end) {
739 const __be32 *cells;
740 phandle phandle;
741
742 phandle = be32_to_cpup(list++);
743 args = list;
744
745
746 if (!phandle)
747 goto next;
748
749 node = of_find_node_by_phandle(phandle);
750 if (!node) {
751 pr_debug("%s: could not find phandle\n",
752 np->full_name);
753 goto err0;
754 }
755
756 cells = of_get_property(node, cells_name, &size);
757 if (!cells || size != sizeof(*cells)) {
758 pr_debug("%s: could not get %s for %s\n",
759 np->full_name, cells_name, node->full_name);
760 goto err1;
761 }
762
763 list += be32_to_cpup(cells);
764 if (list > list_end) {
765 pr_debug("%s: insufficient arguments length\n",
766 np->full_name);
767 goto err1;
768 }
769next:
770 if (cur_index == index)
771 break;
772
773 of_node_put(node);
774 node = NULL;
775 args = NULL;
776 cur_index++;
777 }
778
779 if (!node) {
780
781
782
783
784 if (args)
785 ret = -EEXIST;
786 else
787 ret = -ENOENT;
788 goto err0;
789 }
790
791 if (out_node)
792 *out_node = node;
793 if (out_args)
794 *out_args = args;
795
796 return 0;
797err1:
798 of_node_put(node);
799err0:
800 pr_debug("%s failed with status %d\n", __func__, ret);
801 return ret;
802}
803EXPORT_SYMBOL(of_parse_phandles_with_args);
804
805
806
807
808int prom_add_property(struct device_node *np, struct property *prop)
809{
810 struct property **next;
811 unsigned long flags;
812
813 prop->next = NULL;
814 write_lock_irqsave(&devtree_lock, flags);
815 next = &np->properties;
816 while (*next) {
817 if (strcmp(prop->name, (*next)->name) == 0) {
818
819 write_unlock_irqrestore(&devtree_lock, flags);
820 return -1;
821 }
822 next = &(*next)->next;
823 }
824 *next = prop;
825 write_unlock_irqrestore(&devtree_lock, flags);
826
827#ifdef CONFIG_PROC_DEVICETREE
828
829 if (np->pde)
830 proc_device_tree_add_prop(np->pde, prop);
831#endif
832
833 return 0;
834}
835
836
837
838
839
840
841
842
843
844int prom_remove_property(struct device_node *np, struct property *prop)
845{
846 struct property **next;
847 unsigned long flags;
848 int found = 0;
849
850 write_lock_irqsave(&devtree_lock, flags);
851 next = &np->properties;
852 while (*next) {
853 if (*next == prop) {
854
855 *next = prop->next;
856 prop->next = np->deadprops;
857 np->deadprops = prop;
858 found = 1;
859 break;
860 }
861 next = &(*next)->next;
862 }
863 write_unlock_irqrestore(&devtree_lock, flags);
864
865 if (!found)
866 return -ENODEV;
867
868#ifdef CONFIG_PROC_DEVICETREE
869
870 if (np->pde)
871 proc_device_tree_remove_prop(np->pde, prop);
872#endif
873
874 return 0;
875}
876
877
878
879
880
881
882
883
884
885int prom_update_property(struct device_node *np,
886 struct property *newprop,
887 struct property *oldprop)
888{
889 struct property **next;
890 unsigned long flags;
891 int found = 0;
892
893 write_lock_irqsave(&devtree_lock, flags);
894 next = &np->properties;
895 while (*next) {
896 if (*next == oldprop) {
897
898 newprop->next = oldprop->next;
899 *next = newprop;
900 oldprop->next = np->deadprops;
901 np->deadprops = oldprop;
902 found = 1;
903 break;
904 }
905 next = &(*next)->next;
906 }
907 write_unlock_irqrestore(&devtree_lock, flags);
908
909 if (!found)
910 return -ENODEV;
911
912#ifdef CONFIG_PROC_DEVICETREE
913
914 if (np->pde)
915 proc_device_tree_update_prop(np->pde, newprop, oldprop);
916#endif
917
918 return 0;
919}
920
921#if defined(CONFIG_OF_DYNAMIC)
922
923
924
925
926
927
928
929
930
931
932
933void of_attach_node(struct device_node *np)
934{
935 unsigned long flags;
936
937 write_lock_irqsave(&devtree_lock, flags);
938 np->sibling = np->parent->child;
939 np->allnext = allnodes;
940 np->parent->child = np;
941 allnodes = np;
942 write_unlock_irqrestore(&devtree_lock, flags);
943}
944
945
946
947
948
949
950
951void of_detach_node(struct device_node *np)
952{
953 struct device_node *parent;
954 unsigned long flags;
955
956 write_lock_irqsave(&devtree_lock, flags);
957
958 parent = np->parent;
959 if (!parent)
960 goto out_unlock;
961
962 if (allnodes == np)
963 allnodes = np->allnext;
964 else {
965 struct device_node *prev;
966 for (prev = allnodes;
967 prev->allnext != np;
968 prev = prev->allnext)
969 ;
970 prev->allnext = np->allnext;
971 }
972
973 if (parent->child == np)
974 parent->child = np->sibling;
975 else {
976 struct device_node *prevsib;
977 for (prevsib = np->parent->child;
978 prevsib->sibling != np;
979 prevsib = prevsib->sibling)
980 ;
981 prevsib->sibling = np->sibling;
982 }
983
984 of_node_set_flag(np, OF_DETACHED);
985
986out_unlock:
987 write_unlock_irqrestore(&devtree_lock, flags);
988}
989#endif
990
991