blob: cf3c758ee447501e1baed8e4dacf49ae8699677c [file] [log] [blame]
Grant Likely53a42092011-12-12 09:25:57 -07001/*
2 * Self tests for device tree subsystem
3 */
4
Grant Likelycabb7d52013-02-12 21:19:37 +00005#define pr_fmt(fmt) "### dt-test ### " fmt
Grant Likely53a42092011-12-12 09:25:57 -07006
7#include <linux/clk.h>
8#include <linux/err.h>
9#include <linux/errno.h>
10#include <linux/module.h>
11#include <linux/of.h>
12#include <linux/list.h>
13#include <linux/mutex.h>
14#include <linux/slab.h>
15#include <linux/device.h>
16
Pantelis Antoniouf713f5e2014-07-04 19:58:47 +030017#include "of_private.h"
18
19static struct selftest_results {
20 int passed;
21 int failed;
22} selftest_results;
23
Grant Likely53a42092011-12-12 09:25:57 -070024#define selftest(result, fmt, ...) { \
Grant Likelycabb7d52013-02-12 21:19:37 +000025 if (!(result)) { \
Grant Likely53a42092011-12-12 09:25:57 -070026 pr_err("FAIL %s:%i " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
Pantelis Antoniouf713f5e2014-07-04 19:58:47 +030027 selftest_results.passed = false; \
Grant Likelycabb7d52013-02-12 21:19:37 +000028 } else { \
29 pr_info("pass %s:%i\n", __FILE__, __LINE__); \
30 } \
Grant Likely53a42092011-12-12 09:25:57 -070031}
32
33static void __init of_selftest_parse_phandle_with_args(void)
34{
35 struct device_node *np;
36 struct of_phandle_args args;
Grant Likelycabb7d52013-02-12 21:19:37 +000037 int i, rc;
Grant Likely53a42092011-12-12 09:25:57 -070038
Grant Likely53a42092011-12-12 09:25:57 -070039 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
40 if (!np) {
41 pr_err("missing testcase data\n");
42 return;
43 }
44
Grant Likelybd69f732013-02-10 22:57:21 +000045 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
46 selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
47
Grant Likelyf7f951c2013-02-12 17:41:22 +000048 for (i = 0; i < 8; i++) {
Grant Likely53a42092011-12-12 09:25:57 -070049 bool passed = true;
50 rc = of_parse_phandle_with_args(np, "phandle-list",
51 "#phandle-cells", i, &args);
52
53 /* Test the values from tests-phandle.dtsi */
54 switch (i) {
55 case 0:
56 passed &= !rc;
57 passed &= (args.args_count == 1);
58 passed &= (args.args[0] == (i + 1));
59 break;
60 case 1:
61 passed &= !rc;
62 passed &= (args.args_count == 2);
63 passed &= (args.args[0] == (i + 1));
64 passed &= (args.args[1] == 0);
65 break;
66 case 2:
67 passed &= (rc == -ENOENT);
68 break;
69 case 3:
70 passed &= !rc;
71 passed &= (args.args_count == 3);
72 passed &= (args.args[0] == (i + 1));
73 passed &= (args.args[1] == 4);
74 passed &= (args.args[2] == 3);
75 break;
76 case 4:
77 passed &= !rc;
78 passed &= (args.args_count == 2);
79 passed &= (args.args[0] == (i + 1));
80 passed &= (args.args[1] == 100);
81 break;
82 case 5:
83 passed &= !rc;
84 passed &= (args.args_count == 0);
85 break;
86 case 6:
87 passed &= !rc;
88 passed &= (args.args_count == 1);
89 passed &= (args.args[0] == (i + 1));
90 break;
91 case 7:
Grant Likelycabb7d52013-02-12 21:19:37 +000092 passed &= (rc == -ENOENT);
Grant Likely53a42092011-12-12 09:25:57 -070093 break;
94 default:
95 passed = false;
96 }
97
Grant Likelycabb7d52013-02-12 21:19:37 +000098 selftest(passed, "index %i - data error on node %s rc=%i\n",
99 i, args.np->full_name, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700100 }
101
102 /* Check for missing list property */
103 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
104 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000105 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000106 rc = of_count_phandle_with_args(np, "phandle-list-missing",
107 "#phandle-cells");
108 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700109
110 /* Check for missing cells property */
111 rc = of_parse_phandle_with_args(np, "phandle-list",
112 "#phandle-cells-missing", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000113 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000114 rc = of_count_phandle_with_args(np, "phandle-list",
115 "#phandle-cells-missing");
116 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700117
118 /* Check for bad phandle in list */
119 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
120 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000121 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000122 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
123 "#phandle-cells");
124 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700125
126 /* Check for incorrectly formed argument list */
127 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
128 "#phandle-cells", 1, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000129 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likelybd69f732013-02-10 22:57:21 +0000130 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
131 "#phandle-cells");
132 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700133}
134
Grant Likely7aff0fe2011-12-12 09:25:58 -0700135static void __init of_selftest_property_match_string(void)
136{
137 struct device_node *np;
138 int rc;
139
140 pr_info("start\n");
141 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
142 if (!np) {
143 pr_err("No testcase data in device tree\n");
144 return;
145 }
146
147 rc = of_property_match_string(np, "phandle-list-names", "first");
148 selftest(rc == 0, "first expected:0 got:%i\n", rc);
149 rc = of_property_match_string(np, "phandle-list-names", "second");
150 selftest(rc == 1, "second expected:0 got:%i\n", rc);
151 rc = of_property_match_string(np, "phandle-list-names", "third");
152 selftest(rc == 2, "third expected:0 got:%i\n", rc);
153 rc = of_property_match_string(np, "phandle-list-names", "fourth");
154 selftest(rc == -ENODATA, "unmatched string; rc=%i", rc);
155 rc = of_property_match_string(np, "missing-property", "blah");
156 selftest(rc == -EINVAL, "missing property; rc=%i", rc);
157 rc = of_property_match_string(np, "empty-property", "blah");
158 selftest(rc == -ENODATA, "empty property; rc=%i", rc);
159 rc = of_property_match_string(np, "unterminated-string", "blah");
160 selftest(rc == -EILSEQ, "unterminated string; rc=%i", rc);
161}
162
Pantelis Antoniouf713f5e2014-07-04 19:58:47 +0300163#define propcmp(p1, p2) (((p1)->length == (p2)->length) && \
164 (p1)->value && (p2)->value && \
165 !memcmp((p1)->value, (p2)->value, (p1)->length) && \
166 !strcmp((p1)->name, (p2)->name))
167static void __init of_selftest_property_copy(void)
168{
169#ifdef CONFIG_OF_DYNAMIC
170 struct property p1 = { .name = "p1", .length = 0, .value = "" };
171 struct property p2 = { .name = "p2", .length = 5, .value = "abcd" };
172 struct property *new;
173
174 new = __of_prop_dup(&p1, GFP_KERNEL);
175 selftest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
176 kfree(new->value);
177 kfree(new->name);
178 kfree(new);
179
180 new = __of_prop_dup(&p2, GFP_KERNEL);
181 selftest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
182 kfree(new->value);
183 kfree(new->name);
184 kfree(new);
185#endif
186}
187
Grant Likely53a42092011-12-12 09:25:57 -0700188static int __init of_selftest(void)
189{
190 struct device_node *np;
191
192 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
193 if (!np) {
194 pr_info("No testcase data in device tree; not running tests\n");
195 return 0;
196 }
197 of_node_put(np);
198
199 pr_info("start of selftest - you will see error messages\n");
200 of_selftest_parse_phandle_with_args();
Grant Likely7aff0fe2011-12-12 09:25:58 -0700201 of_selftest_property_match_string();
Pantelis Antoniouf713f5e2014-07-04 19:58:47 +0300202 of_selftest_property_copy();
203 pr_info("end of selftest - %i passed, %i failed\n",
204 selftest_results.passed, selftest_results.failed);
Grant Likely53a42092011-12-12 09:25:57 -0700205 return 0;
206}
207late_initcall(of_selftest);