Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Self tests for device tree subsystem |
| 3 | */ |
| 4 | |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 5 | #define pr_fmt(fmt) "### dt-test ### " fmt |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 6 | |
| 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 | |
| 17 | static bool selftest_passed = true; |
| 18 | #define selftest(result, fmt, ...) { \ |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 19 | if (!(result)) { \ |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 20 | pr_err("FAIL %s:%i " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \ |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 21 | selftest_passed = false; \ |
| 22 | } else { \ |
| 23 | pr_info("pass %s:%i\n", __FILE__, __LINE__); \ |
| 24 | } \ |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | static void __init of_selftest_parse_phandle_with_args(void) |
| 28 | { |
| 29 | struct device_node *np; |
| 30 | struct of_phandle_args args; |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 31 | int i, rc; |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 32 | |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 33 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); |
| 34 | if (!np) { |
| 35 | pr_err("missing testcase data\n"); |
| 36 | return; |
| 37 | } |
| 38 | |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame^] | 39 | rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells"); |
| 40 | selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc); |
| 41 | |
Grant Likely | f7f951c | 2013-02-12 17:41:22 +0000 | [diff] [blame] | 42 | for (i = 0; i < 8; i++) { |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 43 | bool passed = true; |
| 44 | rc = of_parse_phandle_with_args(np, "phandle-list", |
| 45 | "#phandle-cells", i, &args); |
| 46 | |
| 47 | /* Test the values from tests-phandle.dtsi */ |
| 48 | switch (i) { |
| 49 | case 0: |
| 50 | passed &= !rc; |
| 51 | passed &= (args.args_count == 1); |
| 52 | passed &= (args.args[0] == (i + 1)); |
| 53 | break; |
| 54 | case 1: |
| 55 | passed &= !rc; |
| 56 | passed &= (args.args_count == 2); |
| 57 | passed &= (args.args[0] == (i + 1)); |
| 58 | passed &= (args.args[1] == 0); |
| 59 | break; |
| 60 | case 2: |
| 61 | passed &= (rc == -ENOENT); |
| 62 | break; |
| 63 | case 3: |
| 64 | passed &= !rc; |
| 65 | passed &= (args.args_count == 3); |
| 66 | passed &= (args.args[0] == (i + 1)); |
| 67 | passed &= (args.args[1] == 4); |
| 68 | passed &= (args.args[2] == 3); |
| 69 | break; |
| 70 | case 4: |
| 71 | passed &= !rc; |
| 72 | passed &= (args.args_count == 2); |
| 73 | passed &= (args.args[0] == (i + 1)); |
| 74 | passed &= (args.args[1] == 100); |
| 75 | break; |
| 76 | case 5: |
| 77 | passed &= !rc; |
| 78 | passed &= (args.args_count == 0); |
| 79 | break; |
| 80 | case 6: |
| 81 | passed &= !rc; |
| 82 | passed &= (args.args_count == 1); |
| 83 | passed &= (args.args[0] == (i + 1)); |
| 84 | break; |
| 85 | case 7: |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 86 | passed &= (rc == -ENOENT); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 87 | break; |
| 88 | default: |
| 89 | passed = false; |
| 90 | } |
| 91 | |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 92 | selftest(passed, "index %i - data error on node %s rc=%i\n", |
| 93 | i, args.np->full_name, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /* Check for missing list property */ |
| 97 | rc = of_parse_phandle_with_args(np, "phandle-list-missing", |
| 98 | "#phandle-cells", 0, &args); |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 99 | selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame^] | 100 | rc = of_count_phandle_with_args(np, "phandle-list-missing", |
| 101 | "#phandle-cells"); |
| 102 | selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 103 | |
| 104 | /* Check for missing cells property */ |
| 105 | rc = of_parse_phandle_with_args(np, "phandle-list", |
| 106 | "#phandle-cells-missing", 0, &args); |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 107 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame^] | 108 | rc = of_count_phandle_with_args(np, "phandle-list", |
| 109 | "#phandle-cells-missing"); |
| 110 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 111 | |
| 112 | /* Check for bad phandle in list */ |
| 113 | rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle", |
| 114 | "#phandle-cells", 0, &args); |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 115 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame^] | 116 | rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle", |
| 117 | "#phandle-cells"); |
| 118 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 119 | |
| 120 | /* Check for incorrectly formed argument list */ |
| 121 | rc = of_parse_phandle_with_args(np, "phandle-list-bad-args", |
| 122 | "#phandle-cells", 1, &args); |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 123 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame^] | 124 | rc = of_count_phandle_with_args(np, "phandle-list-bad-args", |
| 125 | "#phandle-cells"); |
| 126 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 129 | static void __init of_selftest_property_match_string(void) |
| 130 | { |
| 131 | struct device_node *np; |
| 132 | int rc; |
| 133 | |
| 134 | pr_info("start\n"); |
| 135 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); |
| 136 | if (!np) { |
| 137 | pr_err("No testcase data in device tree\n"); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | rc = of_property_match_string(np, "phandle-list-names", "first"); |
| 142 | selftest(rc == 0, "first expected:0 got:%i\n", rc); |
| 143 | rc = of_property_match_string(np, "phandle-list-names", "second"); |
| 144 | selftest(rc == 1, "second expected:0 got:%i\n", rc); |
| 145 | rc = of_property_match_string(np, "phandle-list-names", "third"); |
| 146 | selftest(rc == 2, "third expected:0 got:%i\n", rc); |
| 147 | rc = of_property_match_string(np, "phandle-list-names", "fourth"); |
| 148 | selftest(rc == -ENODATA, "unmatched string; rc=%i", rc); |
| 149 | rc = of_property_match_string(np, "missing-property", "blah"); |
| 150 | selftest(rc == -EINVAL, "missing property; rc=%i", rc); |
| 151 | rc = of_property_match_string(np, "empty-property", "blah"); |
| 152 | selftest(rc == -ENODATA, "empty property; rc=%i", rc); |
| 153 | rc = of_property_match_string(np, "unterminated-string", "blah"); |
| 154 | selftest(rc == -EILSEQ, "unterminated string; rc=%i", rc); |
| 155 | } |
| 156 | |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 157 | static int __init of_selftest(void) |
| 158 | { |
| 159 | struct device_node *np; |
| 160 | |
| 161 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); |
| 162 | if (!np) { |
| 163 | pr_info("No testcase data in device tree; not running tests\n"); |
| 164 | return 0; |
| 165 | } |
| 166 | of_node_put(np); |
| 167 | |
| 168 | pr_info("start of selftest - you will see error messages\n"); |
| 169 | of_selftest_parse_phandle_with_args(); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 170 | of_selftest_property_match_string(); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 171 | pr_info("end of selftest - %s\n", selftest_passed ? "PASS" : "FAIL"); |
| 172 | return 0; |
| 173 | } |
| 174 | late_initcall(of_selftest); |