2 * Self tests for device tree subsystem
5 #define pr_fmt(fmt) "### %s(): " fmt, __func__
9 #include <linux/errno.h>
10 #include <linux/module.h>
12 #include <linux/list.h>
13 #include <linux/mutex.h>
14 #include <linux/slab.h>
15 #include <linux/device.h>
17 static bool selftest_passed
= true;
18 #define selftest(result, fmt, ...) { \
19 selftest_passed &= (result); \
21 pr_err("FAIL %s:%i " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
24 static void __init
of_selftest_parse_phandle_with_args(void)
26 struct device_node
*np
;
27 struct of_phandle_args args
;
29 bool passed_all
= true;
32 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
34 pr_err("missing testcase data\n");
38 for (i
= 0; i
< 7; i
++) {
40 rc
= of_parse_phandle_with_args(np
, "phandle-list",
41 "#phandle-cells", i
, &args
);
43 /* Test the values from tests-phandle.dtsi */
47 passed
&= (args
.args_count
== 1);
48 passed
&= (args
.args
[0] == (i
+ 1));
52 passed
&= (args
.args_count
== 2);
53 passed
&= (args
.args
[0] == (i
+ 1));
54 passed
&= (args
.args
[1] == 0);
57 passed
&= (rc
== -ENOENT
);
61 passed
&= (args
.args_count
== 3);
62 passed
&= (args
.args
[0] == (i
+ 1));
63 passed
&= (args
.args
[1] == 4);
64 passed
&= (args
.args
[2] == 3);
68 passed
&= (args
.args_count
== 2);
69 passed
&= (args
.args
[0] == (i
+ 1));
70 passed
&= (args
.args
[1] == 100);
74 passed
&= (args
.args_count
== 0);
78 passed
&= (args
.args_count
== 1);
79 passed
&= (args
.args
[0] == (i
+ 1));
82 passed
&= (rc
== -EINVAL
);
90 pr_err("index %i - data error on node %s rc=%i regs=[",
91 i
, args
.np
->full_name
, rc
);
92 for (j
= 0; j
< args
.args_count
; j
++)
93 printk(" %i", args
.args
[j
]);
100 /* Check for missing list property */
101 rc
= of_parse_phandle_with_args(np
, "phandle-list-missing",
102 "#phandle-cells", 0, &args
);
103 passed_all
&= (rc
== -EINVAL
);
105 /* Check for missing cells property */
106 rc
= of_parse_phandle_with_args(np
, "phandle-list",
107 "#phandle-cells-missing", 0, &args
);
108 passed_all
&= (rc
== -EINVAL
);
110 /* Check for bad phandle in list */
111 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-phandle",
112 "#phandle-cells", 0, &args
);
113 passed_all
&= (rc
== -EINVAL
);
115 /* Check for incorrectly formed argument list */
116 rc
= of_parse_phandle_with_args(np
, "phandle-list-bad-args",
117 "#phandle-cells", 1, &args
);
118 passed_all
&= (rc
== -EINVAL
);
120 pr_info("end - %s\n", passed_all
? "PASS" : "FAIL");
123 static void __init
of_selftest_property_match_string(void)
125 struct device_node
*np
;
129 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
131 pr_err("No testcase data in device tree\n");
135 rc
= of_property_match_string(np
, "phandle-list-names", "first");
136 selftest(rc
== 0, "first expected:0 got:%i\n", rc
);
137 rc
= of_property_match_string(np
, "phandle-list-names", "second");
138 selftest(rc
== 1, "second expected:0 got:%i\n", rc
);
139 rc
= of_property_match_string(np
, "phandle-list-names", "third");
140 selftest(rc
== 2, "third expected:0 got:%i\n", rc
);
141 rc
= of_property_match_string(np
, "phandle-list-names", "fourth");
142 selftest(rc
== -ENODATA
, "unmatched string; rc=%i", rc
);
143 rc
= of_property_match_string(np
, "missing-property", "blah");
144 selftest(rc
== -EINVAL
, "missing property; rc=%i", rc
);
145 rc
= of_property_match_string(np
, "empty-property", "blah");
146 selftest(rc
== -ENODATA
, "empty property; rc=%i", rc
);
147 rc
= of_property_match_string(np
, "unterminated-string", "blah");
148 selftest(rc
== -EILSEQ
, "unterminated string; rc=%i", rc
);
151 static int __init
of_selftest(void)
153 struct device_node
*np
;
155 np
= of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
157 pr_info("No testcase data in device tree; not running tests\n");
162 pr_info("start of selftest - you will see error messages\n");
163 of_selftest_parse_phandle_with_args();
164 of_selftest_property_match_string();
165 pr_info("end of selftest - %s\n", selftest_passed
? "PASS" : "FAIL");
168 late_initcall(of_selftest
);