2 * Self tests for device tree subsystem
5 #define pr_fmt(fmt) "### dt-test ### " fmt
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, ...) { \
20 pr_err("FAIL %s:%i " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
21 selftest_passed = false; \
23 pr_info("pass %s:%i\n", __FILE__, __LINE__); \
27 static void __init of_selftest_parse_phandle_with_args(void)
29 struct device_node *np;
30 struct of_phandle_args args;
33 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
35 pr_err("missing testcase data\n");
39 for (i = 0; i < 8; i++) {
41 rc = of_parse_phandle_with_args(np, "phandle-list",
42 "#phandle-cells", i, &args);
44 /* Test the values from tests-phandle.dtsi */
48 passed &= (args.args_count == 1);
49 passed &= (args.args[0] == (i + 1));
53 passed &= (args.args_count == 2);
54 passed &= (args.args[0] == (i + 1));
55 passed &= (args.args[1] == 0);
58 passed &= (rc == -ENOENT);
62 passed &= (args.args_count == 3);
63 passed &= (args.args[0] == (i + 1));
64 passed &= (args.args[1] == 4);
65 passed &= (args.args[2] == 3);
69 passed &= (args.args_count == 2);
70 passed &= (args.args[0] == (i + 1));
71 passed &= (args.args[1] == 100);
75 passed &= (args.args_count == 0);
79 passed &= (args.args_count == 1);
80 passed &= (args.args[0] == (i + 1));
83 passed &= (rc == -ENOENT);
89 selftest(passed, "index %i - data error on node %s rc=%i\n",
90 i, args.np->full_name, rc);
93 /* Check for missing list property */
94 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
95 "#phandle-cells", 0, &args);
96 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
98 /* Check for missing cells property */
99 rc = of_parse_phandle_with_args(np, "phandle-list",
100 "#phandle-cells-missing", 0, &args);
101 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
103 /* Check for bad phandle in list */
104 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
105 "#phandle-cells", 0, &args);
106 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
108 /* Check for incorrectly formed argument list */
109 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
110 "#phandle-cells", 1, &args);
111 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
114 static void __init of_selftest_property_match_string(void)
116 struct device_node *np;
120 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
122 pr_err("No testcase data in device tree\n");
126 rc = of_property_match_string(np, "phandle-list-names", "first");
127 selftest(rc == 0, "first expected:0 got:%i\n", rc);
128 rc = of_property_match_string(np, "phandle-list-names", "second");
129 selftest(rc == 1, "second expected:0 got:%i\n", rc);
130 rc = of_property_match_string(np, "phandle-list-names", "third");
131 selftest(rc == 2, "third expected:0 got:%i\n", rc);
132 rc = of_property_match_string(np, "phandle-list-names", "fourth");
133 selftest(rc == -ENODATA, "unmatched string; rc=%i", rc);
134 rc = of_property_match_string(np, "missing-property", "blah");
135 selftest(rc == -EINVAL, "missing property; rc=%i", rc);
136 rc = of_property_match_string(np, "empty-property", "blah");
137 selftest(rc == -ENODATA, "empty property; rc=%i", rc);
138 rc = of_property_match_string(np, "unterminated-string", "blah");
139 selftest(rc == -EILSEQ, "unterminated string; rc=%i", rc);
142 static int __init of_selftest(void)
144 struct device_node *np;
146 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
148 pr_info("No testcase data in device tree; not running tests\n");
153 pr_info("start of selftest - you will see error messages\n");
154 of_selftest_parse_phandle_with_args();
155 of_selftest_property_match_string();
156 pr_info("end of selftest - %s\n", selftest_passed ? "PASS" : "FAIL");
159 late_initcall(of_selftest);