Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Self tests for device tree subsystem |
| 4 | */ |
| 5 | |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 6 | #define pr_fmt(fmt) "### dt-test ### " fmt |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 7 | |
Rob Herring | 0fa1c57 | 2018-01-05 15:32:33 -0600 | [diff] [blame] | 8 | #include <linux/bootmem.h> |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 9 | #include <linux/clk.h> |
| 10 | #include <linux/err.h> |
| 11 | #include <linux/errno.h> |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 12 | #include <linux/hashtable.h> |
Frank Rowand | 81d0848f | 2017-04-25 17:09:54 -0700 | [diff] [blame] | 13 | #include <linux/libfdt.h> |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 14 | #include <linux/of.h> |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 15 | #include <linux/of_fdt.h> |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 16 | #include <linux/of_irq.h> |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 17 | #include <linux/of_platform.h> |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 18 | #include <linux/list.h> |
| 19 | #include <linux/mutex.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/device.h> |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 22 | #include <linux/platform_device.h> |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 23 | |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 24 | #include <linux/i2c.h> |
| 25 | #include <linux/i2c-mux.h> |
| 26 | |
Pantelis Antoniou | 492a22a | 2015-04-07 22:23:49 +0300 | [diff] [blame] | 27 | #include <linux/bitops.h> |
| 28 | |
Pantelis Antoniou | 6984339 | 2014-07-04 19:58:47 +0300 | [diff] [blame] | 29 | #include "of_private.h" |
| 30 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 31 | static struct unittest_results { |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 32 | int passed; |
| 33 | int failed; |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 34 | } unittest_results; |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 35 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 36 | #define unittest(result, fmt, ...) ({ \ |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 37 | bool failed = !(result); \ |
| 38 | if (failed) { \ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 39 | unittest_results.failed++; \ |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 40 | pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \ |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 41 | } else { \ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 42 | unittest_results.passed++; \ |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 43 | pr_debug("pass %s():%i\n", __func__, __LINE__); \ |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 44 | } \ |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 45 | failed; \ |
| 46 | }) |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 47 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 48 | static void __init of_unittest_find_node_by_name(void) |
Grant Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 49 | { |
| 50 | struct device_node *np; |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 51 | const char *options, *name; |
Grant Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 52 | |
| 53 | np = of_find_node_by_path("/testcase-data"); |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 54 | name = kasprintf(GFP_KERNEL, "%pOF", np); |
| 55 | unittest(np && !strcmp("/testcase-data", name), |
Grant Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 56 | "find /testcase-data failed\n"); |
| 57 | of_node_put(np); |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 58 | kfree(name); |
Grant Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 59 | |
| 60 | /* Test if trailing '/' works */ |
| 61 | np = of_find_node_by_path("/testcase-data/"); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 62 | unittest(!np, "trailing '/' on /testcase-data/ should fail\n"); |
Grant Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 63 | |
| 64 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 65 | name = kasprintf(GFP_KERNEL, "%pOF", np); |
| 66 | unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name), |
Grant Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 67 | "find /testcase-data/phandle-tests/consumer-a failed\n"); |
| 68 | of_node_put(np); |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 69 | kfree(name); |
Grant Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 70 | |
| 71 | np = of_find_node_by_path("testcase-alias"); |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 72 | name = kasprintf(GFP_KERNEL, "%pOF", np); |
| 73 | unittest(np && !strcmp("/testcase-data", name), |
Grant Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 74 | "find testcase-alias failed\n"); |
| 75 | of_node_put(np); |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 76 | kfree(name); |
Grant Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 77 | |
| 78 | /* Test if trailing '/' works on aliases */ |
| 79 | np = of_find_node_by_path("testcase-alias/"); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 80 | unittest(!np, "trailing '/' on testcase-alias/ should fail\n"); |
Grant Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 81 | |
| 82 | np = of_find_node_by_path("testcase-alias/phandle-tests/consumer-a"); |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 83 | name = kasprintf(GFP_KERNEL, "%pOF", np); |
| 84 | unittest(np && !strcmp("/testcase-data/phandle-tests/consumer-a", name), |
Grant Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 85 | "find testcase-alias/phandle-tests/consumer-a failed\n"); |
| 86 | of_node_put(np); |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 87 | kfree(name); |
Grant Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 88 | |
| 89 | np = of_find_node_by_path("/testcase-data/missing-path"); |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 90 | unittest(!np, "non-existent path returned node %pOF\n", np); |
Grant Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 91 | of_node_put(np); |
| 92 | |
| 93 | np = of_find_node_by_path("missing-alias"); |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 94 | unittest(!np, "non-existent alias returned node %pOF\n", np); |
Grant Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 95 | of_node_put(np); |
| 96 | |
| 97 | np = of_find_node_by_path("testcase-alias/missing-path"); |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 98 | unittest(!np, "non-existent alias with relative path returned node %pOF\n", np); |
Grant Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 99 | of_node_put(np); |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 100 | |
| 101 | np = of_find_node_opts_by_path("/testcase-data:testoption", &options); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 102 | unittest(np && !strcmp("testoption", options), |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 103 | "option path test failed\n"); |
| 104 | of_node_put(np); |
| 105 | |
Peter Hurley | 8cbba1a | 2015-03-06 13:59:59 -0500 | [diff] [blame] | 106 | np = of_find_node_opts_by_path("/testcase-data:test/option", &options); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 107 | unittest(np && !strcmp("test/option", options), |
Peter Hurley | 8cbba1a | 2015-03-06 13:59:59 -0500 | [diff] [blame] | 108 | "option path test, subcase #1 failed\n"); |
| 109 | of_node_put(np); |
| 110 | |
Brian Norris | 5ca1b0d | 2015-03-17 12:30:32 -0700 | [diff] [blame] | 111 | np = of_find_node_opts_by_path("/testcase-data/testcase-device1:test/option", &options); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 112 | unittest(np && !strcmp("test/option", options), |
Brian Norris | 5ca1b0d | 2015-03-17 12:30:32 -0700 | [diff] [blame] | 113 | "option path test, subcase #2 failed\n"); |
| 114 | of_node_put(np); |
| 115 | |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 116 | np = of_find_node_opts_by_path("/testcase-data:testoption", NULL); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 117 | unittest(np, "NULL option path test failed\n"); |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 118 | of_node_put(np); |
| 119 | |
| 120 | np = of_find_node_opts_by_path("testcase-alias:testaliasoption", |
| 121 | &options); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 122 | unittest(np && !strcmp("testaliasoption", options), |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 123 | "option alias path test failed\n"); |
| 124 | of_node_put(np); |
| 125 | |
Peter Hurley | 8cbba1a | 2015-03-06 13:59:59 -0500 | [diff] [blame] | 126 | np = of_find_node_opts_by_path("testcase-alias:test/alias/option", |
| 127 | &options); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 128 | unittest(np && !strcmp("test/alias/option", options), |
Peter Hurley | 8cbba1a | 2015-03-06 13:59:59 -0500 | [diff] [blame] | 129 | "option alias path test, subcase #1 failed\n"); |
| 130 | of_node_put(np); |
| 131 | |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 132 | np = of_find_node_opts_by_path("testcase-alias:testaliasoption", NULL); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 133 | unittest(np, "NULL option alias path test failed\n"); |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 134 | of_node_put(np); |
| 135 | |
| 136 | options = "testoption"; |
| 137 | np = of_find_node_opts_by_path("testcase-alias", &options); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 138 | unittest(np && !options, "option clearing test failed\n"); |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 139 | of_node_put(np); |
| 140 | |
| 141 | options = "testoption"; |
| 142 | np = of_find_node_opts_by_path("/", &options); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 143 | unittest(np && !options, "option clearing root node test failed\n"); |
Leif Lindholm | 75c28c0 | 2014-11-28 11:34:28 +0000 | [diff] [blame] | 144 | of_node_put(np); |
Grant Likely | ae91ff7 | 2014-03-14 13:53:10 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 147 | static void __init of_unittest_dynamic(void) |
Grant Likely | 7e66c5c | 2013-11-15 17:19:09 +0000 | [diff] [blame] | 148 | { |
| 149 | struct device_node *np; |
| 150 | struct property *prop; |
| 151 | |
| 152 | np = of_find_node_by_path("/testcase-data"); |
| 153 | if (!np) { |
| 154 | pr_err("missing testcase data\n"); |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | /* Array of 4 properties for the purpose of testing */ |
| 159 | prop = kzalloc(sizeof(*prop) * 4, GFP_KERNEL); |
| 160 | if (!prop) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 161 | unittest(0, "kzalloc() failed\n"); |
Grant Likely | 7e66c5c | 2013-11-15 17:19:09 +0000 | [diff] [blame] | 162 | return; |
| 163 | } |
| 164 | |
| 165 | /* Add a new property - should pass*/ |
| 166 | prop->name = "new-property"; |
| 167 | prop->value = "new-property-data"; |
| 168 | prop->length = strlen(prop->value); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 169 | unittest(of_add_property(np, prop) == 0, "Adding a new property failed\n"); |
Grant Likely | 7e66c5c | 2013-11-15 17:19:09 +0000 | [diff] [blame] | 170 | |
| 171 | /* Try to add an existing property - should fail */ |
| 172 | prop++; |
| 173 | prop->name = "new-property"; |
| 174 | prop->value = "new-property-data-should-fail"; |
| 175 | prop->length = strlen(prop->value); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 176 | unittest(of_add_property(np, prop) != 0, |
Grant Likely | 7e66c5c | 2013-11-15 17:19:09 +0000 | [diff] [blame] | 177 | "Adding an existing property should have failed\n"); |
| 178 | |
| 179 | /* Try to modify an existing property - should pass */ |
| 180 | prop->value = "modify-property-data-should-pass"; |
| 181 | prop->length = strlen(prop->value); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 182 | unittest(of_update_property(np, prop) == 0, |
Grant Likely | 7e66c5c | 2013-11-15 17:19:09 +0000 | [diff] [blame] | 183 | "Updating an existing property should have passed\n"); |
| 184 | |
| 185 | /* Try to modify non-existent property - should pass*/ |
| 186 | prop++; |
| 187 | prop->name = "modify-property"; |
| 188 | prop->value = "modify-missing-property-data-should-pass"; |
| 189 | prop->length = strlen(prop->value); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 190 | unittest(of_update_property(np, prop) == 0, |
Grant Likely | 7e66c5c | 2013-11-15 17:19:09 +0000 | [diff] [blame] | 191 | "Updating a missing property should have passed\n"); |
| 192 | |
| 193 | /* Remove property - should pass */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 194 | unittest(of_remove_property(np, prop) == 0, |
Grant Likely | 7e66c5c | 2013-11-15 17:19:09 +0000 | [diff] [blame] | 195 | "Removing a property should have passed\n"); |
| 196 | |
| 197 | /* Adding very large property - should pass */ |
| 198 | prop++; |
| 199 | prop->name = "large-property-PAGE_SIZEx8"; |
| 200 | prop->length = PAGE_SIZE * 8; |
| 201 | prop->value = kzalloc(prop->length, GFP_KERNEL); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 202 | unittest(prop->value != NULL, "Unable to allocate large buffer\n"); |
Grant Likely | 7e66c5c | 2013-11-15 17:19:09 +0000 | [diff] [blame] | 203 | if (prop->value) |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 204 | unittest(of_add_property(np, prop) == 0, |
Grant Likely | 7e66c5c | 2013-11-15 17:19:09 +0000 | [diff] [blame] | 205 | "Adding a large property should have passed\n"); |
| 206 | } |
| 207 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 208 | static int __init of_unittest_check_node_linkage(struct device_node *np) |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 209 | { |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 210 | struct device_node *child; |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 211 | int count = 0, rc; |
| 212 | |
| 213 | for_each_child_of_node(np, child) { |
| 214 | if (child->parent != np) { |
| 215 | pr_err("Child node %s links to wrong parent %s\n", |
| 216 | child->name, np->name); |
Julia Lawall | 855ff28 | 2015-10-22 11:02:50 +0200 | [diff] [blame] | 217 | rc = -EINVAL; |
| 218 | goto put_child; |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 219 | } |
| 220 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 221 | rc = of_unittest_check_node_linkage(child); |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 222 | if (rc < 0) |
Julia Lawall | 855ff28 | 2015-10-22 11:02:50 +0200 | [diff] [blame] | 223 | goto put_child; |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 224 | count += rc; |
| 225 | } |
| 226 | |
| 227 | return count + 1; |
Julia Lawall | 855ff28 | 2015-10-22 11:02:50 +0200 | [diff] [blame] | 228 | put_child: |
| 229 | of_node_put(child); |
| 230 | return rc; |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 233 | static void __init of_unittest_check_tree_linkage(void) |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 234 | { |
| 235 | struct device_node *np; |
| 236 | int allnode_count = 0, child_count; |
| 237 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 238 | if (!of_root) |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 239 | return; |
| 240 | |
| 241 | for_each_of_allnodes(np) |
| 242 | allnode_count++; |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 243 | child_count = of_unittest_check_node_linkage(of_root); |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 244 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 245 | unittest(child_count > 0, "Device node data structure is corrupted\n"); |
Grant Likely | a2166ca | 2015-03-29 08:59:58 +0100 | [diff] [blame] | 246 | unittest(child_count == allnode_count, |
Frank Rowand | a6bb121 | 2015-03-13 23:59:01 -0700 | [diff] [blame] | 247 | "allnodes list size (%i) doesn't match sibling lists size (%i)\n", |
| 248 | allnode_count, child_count); |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 249 | pr_debug("allnodes list size (%i); sibling lists size (%i)\n", allnode_count, child_count); |
| 250 | } |
| 251 | |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 252 | static void __init of_unittest_printf_one(struct device_node *np, const char *fmt, |
| 253 | const char *expected) |
| 254 | { |
| 255 | unsigned char buf[strlen(expected)+10]; |
| 256 | int size, i; |
| 257 | |
| 258 | /* Baseline; check conversion with a large size limit */ |
| 259 | memset(buf, 0xff, sizeof(buf)); |
| 260 | size = snprintf(buf, sizeof(buf) - 2, fmt, np); |
| 261 | |
| 262 | /* use strcmp() instead of strncmp() here to be absolutely sure strings match */ |
| 263 | unittest((strcmp(buf, expected) == 0) && (buf[size+1] == 0xff), |
| 264 | "sprintf failed; fmt='%s' expected='%s' rslt='%s'\n", |
| 265 | fmt, expected, buf); |
| 266 | |
| 267 | /* Make sure length limits work */ |
| 268 | size++; |
| 269 | for (i = 0; i < 2; i++, size--) { |
| 270 | /* Clear the buffer, and make sure it works correctly still */ |
| 271 | memset(buf, 0xff, sizeof(buf)); |
| 272 | snprintf(buf, size+1, fmt, np); |
| 273 | unittest(strncmp(buf, expected, size) == 0 && (buf[size+1] == 0xff), |
| 274 | "snprintf failed; size=%i fmt='%s' expected='%s' rslt='%s'\n", |
| 275 | size, fmt, expected, buf); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | static void __init of_unittest_printf(void) |
| 280 | { |
| 281 | struct device_node *np; |
| 282 | const char *full_name = "/testcase-data/platform-tests/test-device@1/dev@100"; |
| 283 | char phandle_str[16] = ""; |
| 284 | |
| 285 | np = of_find_node_by_path(full_name); |
| 286 | if (!np) { |
| 287 | unittest(np, "testcase data missing\n"); |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | num_to_str(phandle_str, sizeof(phandle_str), np->phandle); |
| 292 | |
| 293 | of_unittest_printf_one(np, "%pOF", full_name); |
| 294 | of_unittest_printf_one(np, "%pOFf", full_name); |
| 295 | of_unittest_printf_one(np, "%pOFp", phandle_str); |
| 296 | of_unittest_printf_one(np, "%pOFP", "dev@100"); |
| 297 | of_unittest_printf_one(np, "ABC %pOFP ABC", "ABC dev@100 ABC"); |
| 298 | of_unittest_printf_one(np, "%10pOFP", " dev@100"); |
| 299 | of_unittest_printf_one(np, "%-10pOFP", "dev@100 "); |
| 300 | of_unittest_printf_one(of_root, "%pOFP", "/"); |
| 301 | of_unittest_printf_one(np, "%pOFF", "----"); |
| 302 | of_unittest_printf_one(np, "%pOFPF", "dev@100:----"); |
| 303 | of_unittest_printf_one(np, "%pOFPFPc", "dev@100:----:dev@100:test-sub-device"); |
| 304 | of_unittest_printf_one(np, "%pOFc", "test-sub-device"); |
| 305 | of_unittest_printf_one(np, "%pOFC", |
| 306 | "\"test-sub-device\",\"test-compat2\",\"test-compat3\""); |
| 307 | } |
| 308 | |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 309 | struct node_hash { |
| 310 | struct hlist_node node; |
| 311 | struct device_node *np; |
| 312 | }; |
| 313 | |
Grant Likely | 2118f4b | 2014-10-07 11:30:31 +0100 | [diff] [blame] | 314 | static DEFINE_HASHTABLE(phandle_ht, 8); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 315 | static void __init of_unittest_check_phandles(void) |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 316 | { |
| 317 | struct device_node *np; |
| 318 | struct node_hash *nh; |
| 319 | struct hlist_node *tmp; |
| 320 | int i, dup_count = 0, phandle_count = 0; |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 321 | |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 322 | for_each_of_allnodes(np) { |
| 323 | if (!np->phandle) |
| 324 | continue; |
| 325 | |
Grant Likely | 2118f4b | 2014-10-07 11:30:31 +0100 | [diff] [blame] | 326 | hash_for_each_possible(phandle_ht, nh, node, np->phandle) { |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 327 | if (nh->np->phandle == np->phandle) { |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 328 | pr_info("Duplicate phandle! %i used by %pOF and %pOF\n", |
| 329 | np->phandle, nh->np, np); |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 330 | dup_count++; |
| 331 | break; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | nh = kzalloc(sizeof(*nh), GFP_KERNEL); |
| 336 | if (WARN_ON(!nh)) |
| 337 | return; |
| 338 | |
| 339 | nh->np = np; |
Grant Likely | 2118f4b | 2014-10-07 11:30:31 +0100 | [diff] [blame] | 340 | hash_add(phandle_ht, &nh->node, np->phandle); |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 341 | phandle_count++; |
| 342 | } |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 343 | unittest(dup_count == 0, "Found %i duplicates in %i phandles\n", |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 344 | dup_count, phandle_count); |
| 345 | |
| 346 | /* Clean up */ |
Grant Likely | 2118f4b | 2014-10-07 11:30:31 +0100 | [diff] [blame] | 347 | hash_for_each_safe(phandle_ht, i, tmp, nh, node) { |
Grant Likely | 841ec21 | 2014-10-02 13:09:15 +0100 | [diff] [blame] | 348 | hash_del(&nh->node); |
| 349 | kfree(nh); |
| 350 | } |
| 351 | } |
| 352 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 353 | static void __init of_unittest_parse_phandle_with_args(void) |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 354 | { |
| 355 | struct device_node *np; |
| 356 | struct of_phandle_args args; |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 357 | int i, rc; |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 358 | |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 359 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); |
| 360 | if (!np) { |
| 361 | pr_err("missing testcase data\n"); |
| 362 | return; |
| 363 | } |
| 364 | |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 365 | rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells"); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 366 | unittest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 367 | |
Grant Likely | f7f951c | 2013-02-12 17:41:22 +0000 | [diff] [blame] | 368 | for (i = 0; i < 8; i++) { |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 369 | bool passed = true; |
Frank Rowand | 3db316d | 2015-03-14 00:02:31 -0700 | [diff] [blame] | 370 | |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 371 | rc = of_parse_phandle_with_args(np, "phandle-list", |
| 372 | "#phandle-cells", i, &args); |
| 373 | |
| 374 | /* Test the values from tests-phandle.dtsi */ |
| 375 | switch (i) { |
| 376 | case 0: |
| 377 | passed &= !rc; |
| 378 | passed &= (args.args_count == 1); |
| 379 | passed &= (args.args[0] == (i + 1)); |
| 380 | break; |
| 381 | case 1: |
| 382 | passed &= !rc; |
| 383 | passed &= (args.args_count == 2); |
| 384 | passed &= (args.args[0] == (i + 1)); |
| 385 | passed &= (args.args[1] == 0); |
| 386 | break; |
| 387 | case 2: |
| 388 | passed &= (rc == -ENOENT); |
| 389 | break; |
| 390 | case 3: |
| 391 | passed &= !rc; |
| 392 | passed &= (args.args_count == 3); |
| 393 | passed &= (args.args[0] == (i + 1)); |
| 394 | passed &= (args.args[1] == 4); |
| 395 | passed &= (args.args[2] == 3); |
| 396 | break; |
| 397 | case 4: |
| 398 | passed &= !rc; |
| 399 | passed &= (args.args_count == 2); |
| 400 | passed &= (args.args[0] == (i + 1)); |
| 401 | passed &= (args.args[1] == 100); |
| 402 | break; |
| 403 | case 5: |
| 404 | passed &= !rc; |
| 405 | passed &= (args.args_count == 0); |
| 406 | break; |
| 407 | case 6: |
| 408 | passed &= !rc; |
| 409 | passed &= (args.args_count == 1); |
| 410 | passed &= (args.args[0] == (i + 1)); |
| 411 | break; |
| 412 | case 7: |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 413 | passed &= (rc == -ENOENT); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 414 | break; |
| 415 | default: |
| 416 | passed = false; |
| 417 | } |
| 418 | |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 419 | unittest(passed, "index %i - data error on node %pOF rc=%i\n", |
| 420 | i, args.np, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /* Check for missing list property */ |
| 424 | rc = of_parse_phandle_with_args(np, "phandle-list-missing", |
| 425 | "#phandle-cells", 0, &args); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 426 | unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 427 | rc = of_count_phandle_with_args(np, "phandle-list-missing", |
| 428 | "#phandle-cells"); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 429 | unittest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 430 | |
| 431 | /* Check for missing cells property */ |
| 432 | rc = of_parse_phandle_with_args(np, "phandle-list", |
| 433 | "#phandle-cells-missing", 0, &args); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 434 | unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 435 | rc = of_count_phandle_with_args(np, "phandle-list", |
| 436 | "#phandle-cells-missing"); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 437 | unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 438 | |
| 439 | /* Check for bad phandle in list */ |
| 440 | rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle", |
| 441 | "#phandle-cells", 0, &args); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 442 | unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 443 | rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle", |
| 444 | "#phandle-cells"); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 445 | unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 446 | |
| 447 | /* Check for incorrectly formed argument list */ |
| 448 | rc = of_parse_phandle_with_args(np, "phandle-list-bad-args", |
| 449 | "#phandle-cells", 1, &args); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 450 | unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 451 | rc = of_count_phandle_with_args(np, "phandle-list-bad-args", |
| 452 | "#phandle-cells"); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 453 | unittest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 456 | static void __init of_unittest_property_string(void) |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 457 | { |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 458 | const char *strings[4]; |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 459 | struct device_node *np; |
| 460 | int rc; |
| 461 | |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 462 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); |
| 463 | if (!np) { |
| 464 | pr_err("No testcase data in device tree\n"); |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | rc = of_property_match_string(np, "phandle-list-names", "first"); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 469 | unittest(rc == 0, "first expected:0 got:%i\n", rc); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 470 | rc = of_property_match_string(np, "phandle-list-names", "second"); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 471 | unittest(rc == 1, "second expected:1 got:%i\n", rc); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 472 | rc = of_property_match_string(np, "phandle-list-names", "third"); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 473 | unittest(rc == 2, "third expected:2 got:%i\n", rc); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 474 | rc = of_property_match_string(np, "phandle-list-names", "fourth"); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 475 | unittest(rc == -ENODATA, "unmatched string; rc=%i\n", rc); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 476 | rc = of_property_match_string(np, "missing-property", "blah"); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 477 | unittest(rc == -EINVAL, "missing property; rc=%i\n", rc); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 478 | rc = of_property_match_string(np, "empty-property", "blah"); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 479 | unittest(rc == -ENODATA, "empty property; rc=%i\n", rc); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 480 | rc = of_property_match_string(np, "unterminated-string", "blah"); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 481 | unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 482 | |
| 483 | /* of_property_count_strings() tests */ |
| 484 | rc = of_property_count_strings(np, "string-property"); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 485 | unittest(rc == 1, "Incorrect string count; rc=%i\n", rc); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 486 | rc = of_property_count_strings(np, "phandle-list-names"); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 487 | unittest(rc == 3, "Incorrect string count; rc=%i\n", rc); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 488 | rc = of_property_count_strings(np, "unterminated-string"); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 489 | unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 490 | rc = of_property_count_strings(np, "unterminated-string-list"); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 491 | unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 492 | |
| 493 | /* of_property_read_string_index() tests */ |
| 494 | rc = of_property_read_string_index(np, "string-property", 0, strings); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 495 | unittest(rc == 0 && !strcmp(strings[0], "foobar"), "of_property_read_string_index() failure; rc=%i\n", rc); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 496 | strings[0] = NULL; |
| 497 | rc = of_property_read_string_index(np, "string-property", 1, strings); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 498 | unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 499 | rc = of_property_read_string_index(np, "phandle-list-names", 0, strings); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 500 | unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 501 | rc = of_property_read_string_index(np, "phandle-list-names", 1, strings); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 502 | unittest(rc == 0 && !strcmp(strings[0], "second"), "of_property_read_string_index() failure; rc=%i\n", rc); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 503 | rc = of_property_read_string_index(np, "phandle-list-names", 2, strings); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 504 | unittest(rc == 0 && !strcmp(strings[0], "third"), "of_property_read_string_index() failure; rc=%i\n", rc); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 505 | strings[0] = NULL; |
| 506 | rc = of_property_read_string_index(np, "phandle-list-names", 3, strings); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 507 | unittest(rc == -ENODATA && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 508 | strings[0] = NULL; |
| 509 | rc = of_property_read_string_index(np, "unterminated-string", 0, strings); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 510 | unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 511 | rc = of_property_read_string_index(np, "unterminated-string-list", 0, strings); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 512 | unittest(rc == 0 && !strcmp(strings[0], "first"), "of_property_read_string_index() failure; rc=%i\n", rc); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 513 | strings[0] = NULL; |
| 514 | rc = of_property_read_string_index(np, "unterminated-string-list", 2, strings); /* should fail */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 515 | unittest(rc == -EILSEQ && strings[0] == NULL, "of_property_read_string_index() failure; rc=%i\n", rc); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 516 | strings[1] = NULL; |
| 517 | |
| 518 | /* of_property_read_string_array() tests */ |
| 519 | rc = of_property_read_string_array(np, "string-property", strings, 4); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 520 | unittest(rc == 1, "Incorrect string count; rc=%i\n", rc); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 521 | rc = of_property_read_string_array(np, "phandle-list-names", strings, 4); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 522 | unittest(rc == 3, "Incorrect string count; rc=%i\n", rc); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 523 | rc = of_property_read_string_array(np, "unterminated-string", strings, 4); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 524 | unittest(rc == -EILSEQ, "unterminated string; rc=%i\n", rc); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 525 | /* -- An incorrectly formed string should cause a failure */ |
| 526 | rc = of_property_read_string_array(np, "unterminated-string-list", strings, 4); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 527 | unittest(rc == -EILSEQ, "unterminated string array; rc=%i\n", rc); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 528 | /* -- parsing the correctly formed strings should still work: */ |
| 529 | strings[2] = NULL; |
| 530 | rc = of_property_read_string_array(np, "unterminated-string-list", strings, 2); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 531 | unittest(rc == 2 && strings[2] == NULL, "of_property_read_string_array() failure; rc=%i\n", rc); |
Grant Likely | a87fa1d | 2014-11-03 15:15:35 +0000 | [diff] [blame] | 532 | strings[1] = NULL; |
| 533 | rc = of_property_read_string_array(np, "phandle-list-names", strings, 1); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 534 | unittest(rc == 1 && strings[1] == NULL, "Overwrote end of string array; rc=%i, str='%s'\n", rc, strings[1]); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Pantelis Antoniou | 6984339 | 2014-07-04 19:58:47 +0300 | [diff] [blame] | 537 | #define propcmp(p1, p2) (((p1)->length == (p2)->length) && \ |
| 538 | (p1)->value && (p2)->value && \ |
| 539 | !memcmp((p1)->value, (p2)->value, (p1)->length) && \ |
| 540 | !strcmp((p1)->name, (p2)->name)) |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 541 | static void __init of_unittest_property_copy(void) |
Pantelis Antoniou | 6984339 | 2014-07-04 19:58:47 +0300 | [diff] [blame] | 542 | { |
| 543 | #ifdef CONFIG_OF_DYNAMIC |
| 544 | struct property p1 = { .name = "p1", .length = 0, .value = "" }; |
| 545 | struct property p2 = { .name = "p2", .length = 5, .value = "abcd" }; |
| 546 | struct property *new; |
| 547 | |
| 548 | new = __of_prop_dup(&p1, GFP_KERNEL); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 549 | unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n"); |
Pantelis Antoniou | 6984339 | 2014-07-04 19:58:47 +0300 | [diff] [blame] | 550 | kfree(new->value); |
| 551 | kfree(new->name); |
| 552 | kfree(new); |
| 553 | |
| 554 | new = __of_prop_dup(&p2, GFP_KERNEL); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 555 | unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n"); |
Pantelis Antoniou | 6984339 | 2014-07-04 19:58:47 +0300 | [diff] [blame] | 556 | kfree(new->value); |
| 557 | kfree(new->name); |
| 558 | kfree(new); |
| 559 | #endif |
| 560 | } |
| 561 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 562 | static void __init of_unittest_changeset(void) |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 563 | { |
| 564 | #ifdef CONFIG_OF_DYNAMIC |
| 565 | struct property *ppadd, padd = { .name = "prop-add", .length = 0, .value = "" }; |
| 566 | struct property *ppupdate, pupdate = { .name = "prop-update", .length = 5, .value = "abcd" }; |
| 567 | struct property *ppremove; |
Grant Likely | e517958 | 2014-11-17 22:31:32 +0000 | [diff] [blame] | 568 | struct device_node *n1, *n2, *n21, *nremove, *parent, *np; |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 569 | struct of_changeset chgset; |
| 570 | |
Grant Likely | e517958 | 2014-11-17 22:31:32 +0000 | [diff] [blame] | 571 | n1 = __of_node_dup(NULL, "/testcase-data/changeset/n1"); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 572 | unittest(n1, "testcase setup failure\n"); |
Grant Likely | e517958 | 2014-11-17 22:31:32 +0000 | [diff] [blame] | 573 | n2 = __of_node_dup(NULL, "/testcase-data/changeset/n2"); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 574 | unittest(n2, "testcase setup failure\n"); |
Grant Likely | e517958 | 2014-11-17 22:31:32 +0000 | [diff] [blame] | 575 | n21 = __of_node_dup(NULL, "%s/%s", "/testcase-data/changeset/n2", "n21"); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 576 | unittest(n21, "testcase setup failure %p\n", n21); |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 577 | nremove = of_find_node_by_path("/testcase-data/changeset/node-remove"); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 578 | unittest(nremove, "testcase setup failure\n"); |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 579 | ppadd = __of_prop_dup(&padd, GFP_KERNEL); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 580 | unittest(ppadd, "testcase setup failure\n"); |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 581 | ppupdate = __of_prop_dup(&pupdate, GFP_KERNEL); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 582 | unittest(ppupdate, "testcase setup failure\n"); |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 583 | parent = nremove->parent; |
| 584 | n1->parent = parent; |
| 585 | n2->parent = parent; |
| 586 | n21->parent = n2; |
| 587 | n2->child = n21; |
| 588 | ppremove = of_find_property(parent, "prop-remove", NULL); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 589 | unittest(ppremove, "failed to find removal prop"); |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 590 | |
| 591 | of_changeset_init(&chgset); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 592 | unittest(!of_changeset_attach_node(&chgset, n1), "fail attach n1\n"); |
| 593 | unittest(!of_changeset_attach_node(&chgset, n2), "fail attach n2\n"); |
| 594 | unittest(!of_changeset_detach_node(&chgset, nremove), "fail remove node\n"); |
| 595 | unittest(!of_changeset_attach_node(&chgset, n21), "fail attach n21\n"); |
| 596 | unittest(!of_changeset_add_property(&chgset, parent, ppadd), "fail add prop\n"); |
| 597 | unittest(!of_changeset_update_property(&chgset, parent, ppupdate), "fail update prop\n"); |
| 598 | unittest(!of_changeset_remove_property(&chgset, parent, ppremove), "fail remove prop\n"); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 599 | unittest(!of_changeset_apply(&chgset), "apply failed\n"); |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 600 | |
Grant Likely | e517958 | 2014-11-17 22:31:32 +0000 | [diff] [blame] | 601 | /* Make sure node names are constructed correctly */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 602 | unittest((np = of_find_node_by_path("/testcase-data/changeset/n2/n21")), |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 603 | "'%pOF' not added\n", n21); |
Markus Elfring | c46ca3c | 2014-12-02 13:54:00 +0100 | [diff] [blame] | 604 | of_node_put(np); |
Grant Likely | e517958 | 2014-11-17 22:31:32 +0000 | [diff] [blame] | 605 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 606 | unittest(!of_changeset_revert(&chgset), "revert failed\n"); |
Pantelis Antoniou | 201c910 | 2014-07-04 19:58:49 +0300 | [diff] [blame] | 607 | |
| 608 | of_changeset_destroy(&chgset); |
| 609 | #endif |
| 610 | } |
| 611 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 612 | static void __init of_unittest_parse_interrupts(void) |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 613 | { |
| 614 | struct device_node *np; |
| 615 | struct of_phandle_args args; |
| 616 | int i, rc; |
| 617 | |
| 618 | np = of_find_node_by_path("/testcase-data/interrupts/interrupts0"); |
| 619 | if (!np) { |
| 620 | pr_err("missing testcase data\n"); |
| 621 | return; |
| 622 | } |
| 623 | |
| 624 | for (i = 0; i < 4; i++) { |
| 625 | bool passed = true; |
Frank Rowand | 3db316d | 2015-03-14 00:02:31 -0700 | [diff] [blame] | 626 | |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 627 | args.args_count = 0; |
| 628 | rc = of_irq_parse_one(np, i, &args); |
| 629 | |
| 630 | passed &= !rc; |
| 631 | passed &= (args.args_count == 1); |
| 632 | passed &= (args.args[0] == (i + 1)); |
| 633 | |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 634 | unittest(passed, "index %i - data error on node %pOF rc=%i\n", |
| 635 | i, args.np, rc); |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 636 | } |
| 637 | of_node_put(np); |
| 638 | |
| 639 | np = of_find_node_by_path("/testcase-data/interrupts/interrupts1"); |
| 640 | if (!np) { |
| 641 | pr_err("missing testcase data\n"); |
| 642 | return; |
| 643 | } |
| 644 | |
| 645 | for (i = 0; i < 4; i++) { |
| 646 | bool passed = true; |
Frank Rowand | 3db316d | 2015-03-14 00:02:31 -0700 | [diff] [blame] | 647 | |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 648 | args.args_count = 0; |
| 649 | rc = of_irq_parse_one(np, i, &args); |
| 650 | |
| 651 | /* Test the values from tests-phandle.dtsi */ |
| 652 | switch (i) { |
| 653 | case 0: |
| 654 | passed &= !rc; |
| 655 | passed &= (args.args_count == 1); |
| 656 | passed &= (args.args[0] == 9); |
| 657 | break; |
| 658 | case 1: |
| 659 | passed &= !rc; |
| 660 | passed &= (args.args_count == 3); |
| 661 | passed &= (args.args[0] == 10); |
| 662 | passed &= (args.args[1] == 11); |
| 663 | passed &= (args.args[2] == 12); |
| 664 | break; |
| 665 | case 2: |
| 666 | passed &= !rc; |
| 667 | passed &= (args.args_count == 2); |
| 668 | passed &= (args.args[0] == 13); |
| 669 | passed &= (args.args[1] == 14); |
| 670 | break; |
| 671 | case 3: |
| 672 | passed &= !rc; |
| 673 | passed &= (args.args_count == 2); |
| 674 | passed &= (args.args[0] == 15); |
| 675 | passed &= (args.args[1] == 16); |
| 676 | break; |
| 677 | default: |
| 678 | passed = false; |
| 679 | } |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 680 | unittest(passed, "index %i - data error on node %pOF rc=%i\n", |
| 681 | i, args.np, rc); |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 682 | } |
| 683 | of_node_put(np); |
| 684 | } |
| 685 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 686 | static void __init of_unittest_parse_interrupts_extended(void) |
Grant Likely | 79d9701 | 2013-09-19 16:47:37 -0500 | [diff] [blame] | 687 | { |
| 688 | struct device_node *np; |
| 689 | struct of_phandle_args args; |
| 690 | int i, rc; |
| 691 | |
| 692 | np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0"); |
| 693 | if (!np) { |
| 694 | pr_err("missing testcase data\n"); |
| 695 | return; |
| 696 | } |
| 697 | |
| 698 | for (i = 0; i < 7; i++) { |
| 699 | bool passed = true; |
Frank Rowand | 3db316d | 2015-03-14 00:02:31 -0700 | [diff] [blame] | 700 | |
Grant Likely | 79d9701 | 2013-09-19 16:47:37 -0500 | [diff] [blame] | 701 | rc = of_irq_parse_one(np, i, &args); |
| 702 | |
| 703 | /* Test the values from tests-phandle.dtsi */ |
| 704 | switch (i) { |
| 705 | case 0: |
| 706 | passed &= !rc; |
| 707 | passed &= (args.args_count == 1); |
| 708 | passed &= (args.args[0] == 1); |
| 709 | break; |
| 710 | case 1: |
| 711 | passed &= !rc; |
| 712 | passed &= (args.args_count == 3); |
| 713 | passed &= (args.args[0] == 2); |
| 714 | passed &= (args.args[1] == 3); |
| 715 | passed &= (args.args[2] == 4); |
| 716 | break; |
| 717 | case 2: |
| 718 | passed &= !rc; |
| 719 | passed &= (args.args_count == 2); |
| 720 | passed &= (args.args[0] == 5); |
| 721 | passed &= (args.args[1] == 6); |
| 722 | break; |
| 723 | case 3: |
| 724 | passed &= !rc; |
| 725 | passed &= (args.args_count == 1); |
| 726 | passed &= (args.args[0] == 9); |
| 727 | break; |
| 728 | case 4: |
| 729 | passed &= !rc; |
| 730 | passed &= (args.args_count == 3); |
| 731 | passed &= (args.args[0] == 10); |
| 732 | passed &= (args.args[1] == 11); |
| 733 | passed &= (args.args[2] == 12); |
| 734 | break; |
| 735 | case 5: |
| 736 | passed &= !rc; |
| 737 | passed &= (args.args_count == 2); |
| 738 | passed &= (args.args[0] == 13); |
| 739 | passed &= (args.args[1] == 14); |
| 740 | break; |
| 741 | case 6: |
| 742 | passed &= !rc; |
| 743 | passed &= (args.args_count == 1); |
| 744 | passed &= (args.args[0] == 15); |
| 745 | break; |
| 746 | default: |
| 747 | passed = false; |
| 748 | } |
| 749 | |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 750 | unittest(passed, "index %i - data error on node %pOF rc=%i\n", |
| 751 | i, args.np, rc); |
Grant Likely | 79d9701 | 2013-09-19 16:47:37 -0500 | [diff] [blame] | 752 | } |
| 753 | of_node_put(np); |
| 754 | } |
| 755 | |
Frank Rowand | afaed7a | 2015-03-14 00:00:36 -0700 | [diff] [blame] | 756 | static const struct of_device_id match_node_table[] = { |
Grant Likely | 1f42e5d | 2014-02-18 21:38:55 +0000 | [diff] [blame] | 757 | { .data = "A", .name = "name0", }, /* Name alone is lowest priority */ |
| 758 | { .data = "B", .type = "type1", }, /* followed by type alone */ |
| 759 | |
| 760 | { .data = "Ca", .name = "name2", .type = "type1", }, /* followed by both together */ |
| 761 | { .data = "Cb", .name = "name2", }, /* Only match when type doesn't match */ |
| 762 | { .data = "Cc", .name = "name2", .type = "type2", }, |
| 763 | |
| 764 | { .data = "E", .compatible = "compat3" }, |
| 765 | { .data = "G", .compatible = "compat2", }, |
| 766 | { .data = "H", .compatible = "compat2", .name = "name5", }, |
| 767 | { .data = "I", .compatible = "compat2", .type = "type1", }, |
| 768 | { .data = "J", .compatible = "compat2", .type = "type1", .name = "name8", }, |
| 769 | { .data = "K", .compatible = "compat2", .name = "name9", }, |
| 770 | {} |
| 771 | }; |
| 772 | |
| 773 | static struct { |
| 774 | const char *path; |
| 775 | const char *data; |
| 776 | } match_node_tests[] = { |
| 777 | { .path = "/testcase-data/match-node/name0", .data = "A", }, |
| 778 | { .path = "/testcase-data/match-node/name1", .data = "B", }, |
| 779 | { .path = "/testcase-data/match-node/a/name2", .data = "Ca", }, |
| 780 | { .path = "/testcase-data/match-node/b/name2", .data = "Cb", }, |
| 781 | { .path = "/testcase-data/match-node/c/name2", .data = "Cc", }, |
| 782 | { .path = "/testcase-data/match-node/name3", .data = "E", }, |
| 783 | { .path = "/testcase-data/match-node/name4", .data = "G", }, |
| 784 | { .path = "/testcase-data/match-node/name5", .data = "H", }, |
| 785 | { .path = "/testcase-data/match-node/name6", .data = "G", }, |
| 786 | { .path = "/testcase-data/match-node/name7", .data = "I", }, |
| 787 | { .path = "/testcase-data/match-node/name8", .data = "J", }, |
| 788 | { .path = "/testcase-data/match-node/name9", .data = "K", }, |
| 789 | }; |
| 790 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 791 | static void __init of_unittest_match_node(void) |
Grant Likely | 1f42e5d | 2014-02-18 21:38:55 +0000 | [diff] [blame] | 792 | { |
| 793 | struct device_node *np; |
| 794 | const struct of_device_id *match; |
| 795 | int i; |
| 796 | |
| 797 | for (i = 0; i < ARRAY_SIZE(match_node_tests); i++) { |
| 798 | np = of_find_node_by_path(match_node_tests[i].path); |
| 799 | if (!np) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 800 | unittest(0, "missing testcase node %s\n", |
Grant Likely | 1f42e5d | 2014-02-18 21:38:55 +0000 | [diff] [blame] | 801 | match_node_tests[i].path); |
| 802 | continue; |
| 803 | } |
| 804 | |
| 805 | match = of_match_node(match_node_table, np); |
| 806 | if (!match) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 807 | unittest(0, "%s didn't match anything\n", |
Grant Likely | 1f42e5d | 2014-02-18 21:38:55 +0000 | [diff] [blame] | 808 | match_node_tests[i].path); |
| 809 | continue; |
| 810 | } |
| 811 | |
| 812 | if (strcmp(match->data, match_node_tests[i].data) != 0) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 813 | unittest(0, "%s got wrong match. expected %s, got %s\n", |
Grant Likely | 1f42e5d | 2014-02-18 21:38:55 +0000 | [diff] [blame] | 814 | match_node_tests[i].path, match_node_tests[i].data, |
| 815 | (const char *)match->data); |
| 816 | continue; |
| 817 | } |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 818 | unittest(1, "passed"); |
Grant Likely | 1f42e5d | 2014-02-18 21:38:55 +0000 | [diff] [blame] | 819 | } |
| 820 | } |
| 821 | |
Grant Likely | d2329fb | 2016-01-04 13:13:21 +0100 | [diff] [blame] | 822 | static struct resource test_bus_res = { |
| 823 | .start = 0xfffffff8, |
| 824 | .end = 0xfffffff9, |
| 825 | .flags = IORESOURCE_MEM, |
| 826 | }; |
Grant Likely | 37791b6 | 2015-03-27 20:30:04 -0700 | [diff] [blame] | 827 | static const struct platform_device_info test_bus_info = { |
| 828 | .name = "unittest-bus", |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 829 | }; |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 830 | static void __init of_unittest_platform_populate(void) |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 831 | { |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 832 | int irq, rc; |
| 833 | struct device_node *np, *child, *grandchild; |
Grant Likely | 37791b6 | 2015-03-27 20:30:04 -0700 | [diff] [blame] | 834 | struct platform_device *pdev, *test_bus; |
Frank Rowand | afaed7a | 2015-03-14 00:00:36 -0700 | [diff] [blame] | 835 | const struct of_device_id match[] = { |
Rob Herring | fb2caa5 | 2014-05-13 10:07:54 -0500 | [diff] [blame] | 836 | { .compatible = "test-device", }, |
| 837 | {} |
| 838 | }; |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 839 | |
| 840 | np = of_find_node_by_path("/testcase-data"); |
Kefeng Wang | 146dedb | 2016-06-01 14:53:10 +0800 | [diff] [blame] | 841 | of_platform_default_populate(np, NULL, NULL); |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 842 | |
| 843 | /* Test that a missing irq domain returns -EPROBE_DEFER */ |
| 844 | np = of_find_node_by_path("/testcase-data/testcase-device1"); |
| 845 | pdev = of_find_device_by_node(np); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 846 | unittest(pdev, "device 1 creation failed\n"); |
Rob Herring | 7d1cdc8 | 2014-05-13 10:07:29 -0500 | [diff] [blame] | 847 | |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 848 | irq = platform_get_irq(pdev, 0); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 849 | unittest(irq == -EPROBE_DEFER, "device deferred probe failed - %d\n", irq); |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 850 | |
| 851 | /* Test that a parsing failure does not return -EPROBE_DEFER */ |
| 852 | np = of_find_node_by_path("/testcase-data/testcase-device2"); |
| 853 | pdev = of_find_device_by_node(np); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 854 | unittest(pdev, "device 2 creation failed\n"); |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 855 | irq = platform_get_irq(pdev, 0); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 856 | unittest(irq < 0 && irq != -EPROBE_DEFER, "device parsing error failed - %d\n", irq); |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 857 | |
Frank Rowand | 716e1d4 | 2015-03-13 23:57:40 -0700 | [diff] [blame] | 858 | np = of_find_node_by_path("/testcase-data/platform-tests"); |
Grant Likely | a2166ca | 2015-03-29 08:59:58 +0100 | [diff] [blame] | 859 | unittest(np, "No testcase data in device tree\n"); |
Frank Rowand | 716e1d4 | 2015-03-13 23:57:40 -0700 | [diff] [blame] | 860 | if (!np) |
Rob Herring | fb2caa5 | 2014-05-13 10:07:54 -0500 | [diff] [blame] | 861 | return; |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 862 | |
Grant Likely | 37791b6 | 2015-03-27 20:30:04 -0700 | [diff] [blame] | 863 | test_bus = platform_device_register_full(&test_bus_info); |
| 864 | rc = PTR_ERR_OR_ZERO(test_bus); |
Grant Likely | a2166ca | 2015-03-29 08:59:58 +0100 | [diff] [blame] | 865 | unittest(!rc, "testbus registration failed; rc=%i\n", rc); |
Frank Rowand | 716e1d4 | 2015-03-13 23:57:40 -0700 | [diff] [blame] | 866 | if (rc) |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 867 | return; |
Grant Likely | 37791b6 | 2015-03-27 20:30:04 -0700 | [diff] [blame] | 868 | test_bus->dev.of_node = np; |
Rob Herring | fb2caa5 | 2014-05-13 10:07:54 -0500 | [diff] [blame] | 869 | |
Grant Likely | d2329fb | 2016-01-04 13:13:21 +0100 | [diff] [blame] | 870 | /* |
| 871 | * Add a dummy resource to the test bus node after it is |
| 872 | * registered to catch problems with un-inserted resources. The |
| 873 | * DT code doesn't insert the resources, and it has caused the |
| 874 | * kernel to oops in the past. This makes sure the same bug |
| 875 | * doesn't crop up again. |
| 876 | */ |
| 877 | platform_device_add_resources(test_bus, &test_bus_res, 1); |
| 878 | |
Grant Likely | 37791b6 | 2015-03-27 20:30:04 -0700 | [diff] [blame] | 879 | of_platform_populate(np, match, NULL, &test_bus->dev); |
Rob Herring | fb2caa5 | 2014-05-13 10:07:54 -0500 | [diff] [blame] | 880 | for_each_child_of_node(np, child) { |
Rob Herring | fb2caa5 | 2014-05-13 10:07:54 -0500 | [diff] [blame] | 881 | for_each_child_of_node(child, grandchild) |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 882 | unittest(of_find_device_by_node(grandchild), |
Rob Herring | fb2caa5 | 2014-05-13 10:07:54 -0500 | [diff] [blame] | 883 | "Could not create device for node '%s'\n", |
| 884 | grandchild->name); |
| 885 | } |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 886 | |
Grant Likely | 37791b6 | 2015-03-27 20:30:04 -0700 | [diff] [blame] | 887 | of_platform_depopulate(&test_bus->dev); |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 888 | for_each_child_of_node(np, child) { |
| 889 | for_each_child_of_node(child, grandchild) |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 890 | unittest(!of_find_device_by_node(grandchild), |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 891 | "device didn't get destroyed '%s'\n", |
| 892 | grandchild->name); |
| 893 | } |
| 894 | |
Grant Likely | 37791b6 | 2015-03-27 20:30:04 -0700 | [diff] [blame] | 895 | platform_device_unregister(test_bus); |
Grant Likely | 851da97 | 2014-11-04 13:14:13 +0000 | [diff] [blame] | 896 | of_node_put(np); |
Rob Herring | 82c0f58 | 2014-04-23 17:57:40 -0500 | [diff] [blame] | 897 | } |
| 898 | |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 899 | /** |
| 900 | * update_node_properties - adds the properties |
| 901 | * of np into dup node (present in live tree) and |
| 902 | * updates parent of children of np to dup. |
| 903 | * |
| 904 | * @np: node already present in live tree |
| 905 | * @dup: node present in live tree to be updated |
| 906 | */ |
| 907 | static void update_node_properties(struct device_node *np, |
| 908 | struct device_node *dup) |
| 909 | { |
| 910 | struct property *prop; |
| 911 | struct device_node *child; |
| 912 | |
| 913 | for_each_property_of_node(np, prop) |
| 914 | of_add_property(dup, prop); |
| 915 | |
| 916 | for_each_child_of_node(np, child) |
| 917 | child->parent = dup; |
| 918 | } |
| 919 | |
| 920 | /** |
| 921 | * attach_node_and_children - attaches nodes |
| 922 | * and its children to live tree |
| 923 | * |
| 924 | * @np: Node to attach to live tree |
| 925 | */ |
| 926 | static int attach_node_and_children(struct device_node *np) |
| 927 | { |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 928 | struct device_node *next, *dup, *child; |
Gaurav Minocha | 3ce04b4 | 2015-01-10 23:19:51 -0800 | [diff] [blame] | 929 | unsigned long flags; |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 930 | const char *full_name; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 931 | |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 932 | full_name = kasprintf(GFP_KERNEL, "%pOF", np); |
| 933 | dup = of_find_node_by_path(full_name); |
| 934 | kfree(full_name); |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 935 | if (dup) { |
| 936 | update_node_properties(np, dup); |
| 937 | return 0; |
| 938 | } |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 939 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 940 | child = np->child; |
| 941 | np->child = NULL; |
Gaurav Minocha | 3ce04b4 | 2015-01-10 23:19:51 -0800 | [diff] [blame] | 942 | |
| 943 | mutex_lock(&of_mutex); |
| 944 | raw_spin_lock_irqsave(&devtree_lock, flags); |
| 945 | np->sibling = np->parent->child; |
| 946 | np->parent->child = np; |
| 947 | of_node_clear_flag(np, OF_DETACHED); |
| 948 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
| 949 | |
| 950 | __of_attach_node_sysfs(np); |
| 951 | mutex_unlock(&of_mutex); |
| 952 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 953 | while (child) { |
| 954 | next = child->sibling; |
| 955 | attach_node_and_children(child); |
| 956 | child = next; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | return 0; |
| 960 | } |
| 961 | |
| 962 | /** |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 963 | * unittest_data_add - Reads, copies data from |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 964 | * linked tree and attaches it to the live tree |
| 965 | */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 966 | static int __init unittest_data_add(void) |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 967 | { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 968 | void *unittest_data; |
| 969 | struct device_node *unittest_data_node, *np; |
Frank Rowand | c854711 | 2015-03-14 00:04:24 -0700 | [diff] [blame] | 970 | /* |
| 971 | * __dtb_testcases_begin[] and __dtb_testcases_end[] are magically |
| 972 | * created by cmd_dt_S_dtb in scripts/Makefile.lib |
| 973 | */ |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 974 | extern uint8_t __dtb_testcases_begin[]; |
| 975 | extern uint8_t __dtb_testcases_end[]; |
| 976 | const int size = __dtb_testcases_end - __dtb_testcases_begin; |
Grant Likely | 2eb46da | 2014-10-02 14:36:46 +0100 | [diff] [blame] | 977 | int rc; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 978 | |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 979 | if (!size) { |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 980 | pr_warn("%s: No testcase data to attach; not running tests\n", |
| 981 | __func__); |
| 982 | return -ENODATA; |
| 983 | } |
| 984 | |
| 985 | /* creating copy */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 986 | unittest_data = kmemdup(__dtb_testcases_begin, size, GFP_KERNEL); |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 987 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 988 | if (!unittest_data) { |
| 989 | pr_warn("%s: Failed to allocate memory for unittest_data; " |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 990 | "not running tests\n", __func__); |
| 991 | return -ENOMEM; |
| 992 | } |
Gavin Shan | c426323 | 2016-05-03 23:22:50 +1000 | [diff] [blame] | 993 | of_fdt_unflatten_tree(unittest_data, NULL, &unittest_data_node); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 994 | if (!unittest_data_node) { |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 995 | pr_warn("%s: No tree to attach; not running tests\n", __func__); |
| 996 | return -ENODATA; |
| 997 | } |
Frank Rowand | f948d6d | 2017-10-17 16:36:29 -0700 | [diff] [blame] | 998 | |
| 999 | /* |
| 1000 | * This lock normally encloses of_overlay_apply() as well as |
| 1001 | * of_resolve_phandles(). |
| 1002 | */ |
| 1003 | of_overlay_mutex_lock(); |
| 1004 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1005 | rc = of_resolve_phandles(unittest_data_node); |
Grant Likely | 2eb46da | 2014-10-02 14:36:46 +0100 | [diff] [blame] | 1006 | if (rc) { |
| 1007 | pr_err("%s: Failed to resolve phandles (rc=%i)\n", __func__, rc); |
Frank Rowand | f948d6d | 2017-10-17 16:36:29 -0700 | [diff] [blame] | 1008 | of_overlay_mutex_unlock(); |
Grant Likely | 2eb46da | 2014-10-02 14:36:46 +0100 | [diff] [blame] | 1009 | return -EINVAL; |
| 1010 | } |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 1011 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 1012 | if (!of_root) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1013 | of_root = unittest_data_node; |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 1014 | for_each_of_allnodes(np) |
| 1015 | __of_attach_node_sysfs(np); |
| 1016 | of_aliases = of_find_node_by_path("/aliases"); |
| 1017 | of_chosen = of_find_node_by_path("/chosen"); |
Frank Rowand | f948d6d | 2017-10-17 16:36:29 -0700 | [diff] [blame] | 1018 | of_overlay_mutex_unlock(); |
Gaurav Minocha | b951f9d | 2014-07-26 12:48:50 -0700 | [diff] [blame] | 1019 | return 0; |
| 1020 | } |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 1021 | |
| 1022 | /* attach the sub-tree to live tree */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1023 | np = unittest_data_node->child; |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 1024 | while (np) { |
| 1025 | struct device_node *next = np->sibling; |
Frank Rowand | 3db316d | 2015-03-14 00:02:31 -0700 | [diff] [blame] | 1026 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 1027 | np->parent = of_root; |
| 1028 | attach_node_and_children(np); |
| 1029 | np = next; |
| 1030 | } |
Frank Rowand | f948d6d | 2017-10-17 16:36:29 -0700 | [diff] [blame] | 1031 | |
| 1032 | of_overlay_mutex_unlock(); |
| 1033 | |
Grant Likely | 5063e25 | 2014-10-03 16:28:27 +0100 | [diff] [blame] | 1034 | return 0; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1037 | #ifdef CONFIG_OF_OVERLAY |
| 1038 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1039 | static int unittest_probe(struct platform_device *pdev) |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1040 | { |
| 1041 | struct device *dev = &pdev->dev; |
| 1042 | struct device_node *np = dev->of_node; |
| 1043 | |
| 1044 | if (np == NULL) { |
| 1045 | dev_err(dev, "No OF data for device\n"); |
| 1046 | return -EINVAL; |
| 1047 | |
| 1048 | } |
| 1049 | |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 1050 | dev_dbg(dev, "%s for node @%pOF\n", __func__, np); |
Pantelis Antoniou | 6b1271d | 2014-12-19 14:34:34 +0200 | [diff] [blame] | 1051 | |
| 1052 | of_platform_populate(np, NULL, NULL, &pdev->dev); |
| 1053 | |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1054 | return 0; |
| 1055 | } |
| 1056 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1057 | static int unittest_remove(struct platform_device *pdev) |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1058 | { |
| 1059 | struct device *dev = &pdev->dev; |
| 1060 | struct device_node *np = dev->of_node; |
| 1061 | |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 1062 | dev_dbg(dev, "%s for node @%pOF\n", __func__, np); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1063 | return 0; |
| 1064 | } |
| 1065 | |
Grant Likely | a2166ca | 2015-03-29 08:59:58 +0100 | [diff] [blame] | 1066 | static const struct of_device_id unittest_match[] = { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1067 | { .compatible = "unittest", }, |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1068 | {}, |
| 1069 | }; |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1070 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1071 | static struct platform_driver unittest_driver = { |
| 1072 | .probe = unittest_probe, |
| 1073 | .remove = unittest_remove, |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1074 | .driver = { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1075 | .name = "unittest", |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1076 | .of_match_table = of_match_ptr(unittest_match), |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1077 | }, |
| 1078 | }; |
| 1079 | |
| 1080 | /* get the platform device instantiated at the path */ |
| 1081 | static struct platform_device *of_path_to_platform_device(const char *path) |
| 1082 | { |
| 1083 | struct device_node *np; |
| 1084 | struct platform_device *pdev; |
| 1085 | |
| 1086 | np = of_find_node_by_path(path); |
| 1087 | if (np == NULL) |
| 1088 | return NULL; |
| 1089 | |
| 1090 | pdev = of_find_device_by_node(np); |
| 1091 | of_node_put(np); |
| 1092 | |
| 1093 | return pdev; |
| 1094 | } |
| 1095 | |
| 1096 | /* find out if a platform device exists at that path */ |
| 1097 | static int of_path_platform_device_exists(const char *path) |
| 1098 | { |
| 1099 | struct platform_device *pdev; |
| 1100 | |
| 1101 | pdev = of_path_to_platform_device(path); |
| 1102 | platform_device_put(pdev); |
| 1103 | return pdev != NULL; |
| 1104 | } |
| 1105 | |
Arnd Bergmann | 4252de3 | 2015-03-04 20:49:47 +0100 | [diff] [blame] | 1106 | #if IS_BUILTIN(CONFIG_I2C) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1107 | |
| 1108 | /* get the i2c client device instantiated at the path */ |
| 1109 | static struct i2c_client *of_path_to_i2c_client(const char *path) |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1110 | { |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1111 | struct device_node *np; |
| 1112 | struct i2c_client *client; |
| 1113 | |
| 1114 | np = of_find_node_by_path(path); |
| 1115 | if (np == NULL) |
| 1116 | return NULL; |
| 1117 | |
| 1118 | client = of_find_i2c_device_by_node(np); |
| 1119 | of_node_put(np); |
| 1120 | |
| 1121 | return client; |
| 1122 | } |
| 1123 | |
| 1124 | /* find out if a i2c client device exists at that path */ |
| 1125 | static int of_path_i2c_client_exists(const char *path) |
| 1126 | { |
| 1127 | struct i2c_client *client; |
| 1128 | |
| 1129 | client = of_path_to_i2c_client(path); |
| 1130 | if (client) |
| 1131 | put_device(&client->dev); |
| 1132 | return client != NULL; |
| 1133 | } |
| 1134 | #else |
| 1135 | static int of_path_i2c_client_exists(const char *path) |
| 1136 | { |
| 1137 | return 0; |
| 1138 | } |
| 1139 | #endif |
| 1140 | |
| 1141 | enum overlay_type { |
| 1142 | PDEV_OVERLAY, |
| 1143 | I2C_OVERLAY |
| 1144 | }; |
| 1145 | |
| 1146 | static int of_path_device_type_exists(const char *path, |
| 1147 | enum overlay_type ovtype) |
| 1148 | { |
| 1149 | switch (ovtype) { |
| 1150 | case PDEV_OVERLAY: |
| 1151 | return of_path_platform_device_exists(path); |
| 1152 | case I2C_OVERLAY: |
| 1153 | return of_path_i2c_client_exists(path); |
| 1154 | } |
| 1155 | return 0; |
| 1156 | } |
| 1157 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1158 | static const char *unittest_path(int nr, enum overlay_type ovtype) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1159 | { |
| 1160 | const char *base; |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1161 | static char buf[256]; |
| 1162 | |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1163 | switch (ovtype) { |
| 1164 | case PDEV_OVERLAY: |
| 1165 | base = "/testcase-data/overlay-node/test-bus"; |
| 1166 | break; |
| 1167 | case I2C_OVERLAY: |
| 1168 | base = "/testcase-data/overlay-node/test-bus/i2c-test-bus"; |
| 1169 | break; |
| 1170 | default: |
| 1171 | buf[0] = '\0'; |
| 1172 | return buf; |
| 1173 | } |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1174 | snprintf(buf, sizeof(buf) - 1, "%s/test-unittest%d", base, nr); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1175 | buf[sizeof(buf) - 1] = '\0'; |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1176 | return buf; |
| 1177 | } |
| 1178 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1179 | static int of_unittest_device_exists(int unittest_nr, enum overlay_type ovtype) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1180 | { |
| 1181 | const char *path; |
| 1182 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1183 | path = unittest_path(unittest_nr, ovtype); |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1184 | |
| 1185 | switch (ovtype) { |
| 1186 | case PDEV_OVERLAY: |
| 1187 | return of_path_platform_device_exists(path); |
| 1188 | case I2C_OVERLAY: |
| 1189 | return of_path_i2c_client_exists(path); |
| 1190 | } |
| 1191 | return 0; |
| 1192 | } |
| 1193 | |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1194 | static const char *overlay_path(int nr) |
| 1195 | { |
| 1196 | static char buf[256]; |
| 1197 | |
| 1198 | snprintf(buf, sizeof(buf) - 1, |
| 1199 | "/testcase-data/overlay%d", nr); |
| 1200 | buf[sizeof(buf) - 1] = '\0'; |
| 1201 | |
| 1202 | return buf; |
| 1203 | } |
| 1204 | |
| 1205 | static const char *bus_path = "/testcase-data/overlay-node/test-bus"; |
| 1206 | |
Pantelis Antoniou | 492a22a | 2015-04-07 22:23:49 +0300 | [diff] [blame] | 1207 | /* it is guaranteed that overlay ids are assigned in sequence */ |
| 1208 | #define MAX_UNITTEST_OVERLAYS 256 |
| 1209 | static unsigned long overlay_id_bits[BITS_TO_LONGS(MAX_UNITTEST_OVERLAYS)]; |
| 1210 | static int overlay_first_id = -1; |
| 1211 | |
| 1212 | static void of_unittest_track_overlay(int id) |
| 1213 | { |
| 1214 | if (overlay_first_id < 0) |
| 1215 | overlay_first_id = id; |
| 1216 | id -= overlay_first_id; |
| 1217 | |
| 1218 | /* we shouldn't need that many */ |
| 1219 | BUG_ON(id >= MAX_UNITTEST_OVERLAYS); |
| 1220 | overlay_id_bits[BIT_WORD(id)] |= BIT_MASK(id); |
| 1221 | } |
| 1222 | |
| 1223 | static void of_unittest_untrack_overlay(int id) |
| 1224 | { |
| 1225 | if (overlay_first_id < 0) |
| 1226 | return; |
| 1227 | id -= overlay_first_id; |
| 1228 | BUG_ON(id >= MAX_UNITTEST_OVERLAYS); |
| 1229 | overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id); |
| 1230 | } |
| 1231 | |
| 1232 | static void of_unittest_destroy_tracked_overlays(void) |
| 1233 | { |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 1234 | int id, ret, defers, ovcs_id; |
Pantelis Antoniou | 492a22a | 2015-04-07 22:23:49 +0300 | [diff] [blame] | 1235 | |
| 1236 | if (overlay_first_id < 0) |
| 1237 | return; |
| 1238 | |
| 1239 | /* try until no defers */ |
| 1240 | do { |
| 1241 | defers = 0; |
| 1242 | /* remove in reverse order */ |
| 1243 | for (id = MAX_UNITTEST_OVERLAYS - 1; id >= 0; id--) { |
| 1244 | if (!(overlay_id_bits[BIT_WORD(id)] & BIT_MASK(id))) |
| 1245 | continue; |
| 1246 | |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 1247 | ovcs_id = id + overlay_first_id; |
| 1248 | ret = of_overlay_remove(&ovcs_id); |
Sergey Senozhatsky | 815d74b | 2016-03-02 20:24:49 +0900 | [diff] [blame] | 1249 | if (ret == -ENODEV) { |
| 1250 | pr_warn("%s: no overlay to destroy for #%d\n", |
| 1251 | __func__, id + overlay_first_id); |
| 1252 | continue; |
| 1253 | } |
Pantelis Antoniou | 492a22a | 2015-04-07 22:23:49 +0300 | [diff] [blame] | 1254 | if (ret != 0) { |
| 1255 | defers++; |
| 1256 | pr_warn("%s: overlay destroy failed for #%d\n", |
| 1257 | __func__, id + overlay_first_id); |
| 1258 | continue; |
| 1259 | } |
| 1260 | |
| 1261 | overlay_id_bits[BIT_WORD(id)] &= ~BIT_MASK(id); |
| 1262 | } |
| 1263 | } while (defers > 0); |
| 1264 | } |
| 1265 | |
Alexander Sverdlin | ca7b896 | 2017-01-19 11:06:16 +0100 | [diff] [blame] | 1266 | static int of_unittest_apply_overlay(int overlay_nr, int unittest_nr, |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1267 | int *overlay_id) |
| 1268 | { |
| 1269 | struct device_node *np = NULL; |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 1270 | int ret; |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1271 | |
| 1272 | np = of_find_node_by_path(overlay_path(overlay_nr)); |
| 1273 | if (np == NULL) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1274 | unittest(0, "could not find overlay node @\"%s\"\n", |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1275 | overlay_path(overlay_nr)); |
| 1276 | ret = -EINVAL; |
| 1277 | goto out; |
| 1278 | } |
| 1279 | |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 1280 | *overlay_id = 0; |
| 1281 | ret = of_overlay_apply(np, overlay_id); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1282 | if (ret < 0) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1283 | unittest(0, "could not create overlay from \"%s\"\n", |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1284 | overlay_path(overlay_nr)); |
| 1285 | goto out; |
| 1286 | } |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 1287 | of_unittest_track_overlay(*overlay_id); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1288 | |
| 1289 | ret = 0; |
| 1290 | |
| 1291 | out: |
| 1292 | of_node_put(np); |
| 1293 | |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1294 | return ret; |
| 1295 | } |
| 1296 | |
| 1297 | /* apply an overlay while checking before and after states */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1298 | static int of_unittest_apply_overlay_check(int overlay_nr, int unittest_nr, |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1299 | int before, int after, enum overlay_type ovtype) |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1300 | { |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 1301 | int ret, ovcs_id; |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1302 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1303 | /* unittest device must not be in before state */ |
| 1304 | if (of_unittest_device_exists(unittest_nr, ovtype) != before) { |
| 1305 | unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n", |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1306 | overlay_path(overlay_nr), |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1307 | unittest_path(unittest_nr, ovtype), |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1308 | !before ? "enabled" : "disabled"); |
| 1309 | return -EINVAL; |
| 1310 | } |
| 1311 | |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 1312 | ovcs_id = 0; |
| 1313 | ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1314 | if (ret != 0) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1315 | /* of_unittest_apply_overlay already called unittest() */ |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1316 | return ret; |
| 1317 | } |
| 1318 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1319 | /* unittest device must be to set to after state */ |
| 1320 | if (of_unittest_device_exists(unittest_nr, ovtype) != after) { |
| 1321 | unittest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n", |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1322 | overlay_path(overlay_nr), |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1323 | unittest_path(unittest_nr, ovtype), |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1324 | !after ? "enabled" : "disabled"); |
| 1325 | return -EINVAL; |
| 1326 | } |
| 1327 | |
| 1328 | return 0; |
| 1329 | } |
| 1330 | |
| 1331 | /* apply an overlay and then revert it while checking before, after states */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1332 | static int of_unittest_apply_revert_overlay_check(int overlay_nr, |
| 1333 | int unittest_nr, int before, int after, |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1334 | enum overlay_type ovtype) |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1335 | { |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 1336 | int ret, ovcs_id; |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1337 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1338 | /* unittest device must be in before state */ |
| 1339 | if (of_unittest_device_exists(unittest_nr, ovtype) != before) { |
| 1340 | unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n", |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1341 | overlay_path(overlay_nr), |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1342 | unittest_path(unittest_nr, ovtype), |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1343 | !before ? "enabled" : "disabled"); |
| 1344 | return -EINVAL; |
| 1345 | } |
| 1346 | |
| 1347 | /* apply the overlay */ |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 1348 | ovcs_id = 0; |
| 1349 | ret = of_unittest_apply_overlay(overlay_nr, unittest_nr, &ovcs_id); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1350 | if (ret != 0) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1351 | /* of_unittest_apply_overlay already called unittest() */ |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1352 | return ret; |
| 1353 | } |
| 1354 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1355 | /* unittest device must be in after state */ |
| 1356 | if (of_unittest_device_exists(unittest_nr, ovtype) != after) { |
| 1357 | unittest(0, "overlay @\"%s\" failed to create @\"%s\" %s\n", |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1358 | overlay_path(overlay_nr), |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1359 | unittest_path(unittest_nr, ovtype), |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1360 | !after ? "enabled" : "disabled"); |
| 1361 | return -EINVAL; |
| 1362 | } |
| 1363 | |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 1364 | ret = of_overlay_remove(&ovcs_id); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1365 | if (ret != 0) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1366 | unittest(0, "overlay @\"%s\" failed to be destroyed @\"%s\"\n", |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1367 | overlay_path(overlay_nr), |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1368 | unittest_path(unittest_nr, ovtype)); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1369 | return ret; |
| 1370 | } |
| 1371 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1372 | /* unittest device must be again in before state */ |
| 1373 | if (of_unittest_device_exists(unittest_nr, PDEV_OVERLAY) != before) { |
| 1374 | unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n", |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1375 | overlay_path(overlay_nr), |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1376 | unittest_path(unittest_nr, ovtype), |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1377 | !before ? "enabled" : "disabled"); |
| 1378 | return -EINVAL; |
| 1379 | } |
| 1380 | |
| 1381 | return 0; |
| 1382 | } |
| 1383 | |
| 1384 | /* test activation of device */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1385 | static void of_unittest_overlay_0(void) |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1386 | { |
| 1387 | int ret; |
| 1388 | |
| 1389 | /* device should enable */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1390 | ret = of_unittest_apply_overlay_check(0, 0, 0, 1, PDEV_OVERLAY); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1391 | if (ret != 0) |
| 1392 | return; |
| 1393 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1394 | unittest(1, "overlay test %d passed\n", 0); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | /* test deactivation of device */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1398 | static void of_unittest_overlay_1(void) |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1399 | { |
| 1400 | int ret; |
| 1401 | |
| 1402 | /* device should disable */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1403 | ret = of_unittest_apply_overlay_check(1, 1, 1, 0, PDEV_OVERLAY); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1404 | if (ret != 0) |
| 1405 | return; |
| 1406 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1407 | unittest(1, "overlay test %d passed\n", 1); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | /* test activation of device */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1411 | static void of_unittest_overlay_2(void) |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1412 | { |
| 1413 | int ret; |
| 1414 | |
| 1415 | /* device should enable */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1416 | ret = of_unittest_apply_overlay_check(2, 2, 0, 1, PDEV_OVERLAY); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1417 | if (ret != 0) |
| 1418 | return; |
| 1419 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1420 | unittest(1, "overlay test %d passed\n", 2); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | /* test deactivation of device */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1424 | static void of_unittest_overlay_3(void) |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1425 | { |
| 1426 | int ret; |
| 1427 | |
| 1428 | /* device should disable */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1429 | ret = of_unittest_apply_overlay_check(3, 3, 1, 0, PDEV_OVERLAY); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1430 | if (ret != 0) |
| 1431 | return; |
| 1432 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1433 | unittest(1, "overlay test %d passed\n", 3); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | /* test activation of a full device node */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1437 | static void of_unittest_overlay_4(void) |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1438 | { |
| 1439 | int ret; |
| 1440 | |
| 1441 | /* device should disable */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1442 | ret = of_unittest_apply_overlay_check(4, 4, 0, 1, PDEV_OVERLAY); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1443 | if (ret != 0) |
| 1444 | return; |
| 1445 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1446 | unittest(1, "overlay test %d passed\n", 4); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | /* test overlay apply/revert sequence */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1450 | static void of_unittest_overlay_5(void) |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1451 | { |
| 1452 | int ret; |
| 1453 | |
| 1454 | /* device should disable */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1455 | ret = of_unittest_apply_revert_overlay_check(5, 5, 0, 1, PDEV_OVERLAY); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1456 | if (ret != 0) |
| 1457 | return; |
| 1458 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1459 | unittest(1, "overlay test %d passed\n", 5); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | /* test overlay application in sequence */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1463 | static void of_unittest_overlay_6(void) |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1464 | { |
| 1465 | struct device_node *np; |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 1466 | int ret, i, ov_id[2], ovcs_id; |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1467 | int overlay_nr = 6, unittest_nr = 6; |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1468 | int before = 0, after = 1; |
| 1469 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1470 | /* unittest device must be in before state */ |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1471 | for (i = 0; i < 2; i++) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1472 | if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY) |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1473 | != before) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1474 | unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n", |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1475 | overlay_path(overlay_nr + i), |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1476 | unittest_path(unittest_nr + i, |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1477 | PDEV_OVERLAY), |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1478 | !before ? "enabled" : "disabled"); |
| 1479 | return; |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | /* apply the overlays */ |
| 1484 | for (i = 0; i < 2; i++) { |
| 1485 | |
| 1486 | np = of_find_node_by_path(overlay_path(overlay_nr + i)); |
| 1487 | if (np == NULL) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1488 | unittest(0, "could not find overlay node @\"%s\"\n", |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1489 | overlay_path(overlay_nr + i)); |
| 1490 | return; |
| 1491 | } |
| 1492 | |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 1493 | ovcs_id = 0; |
| 1494 | ret = of_overlay_apply(np, &ovcs_id); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1495 | if (ret < 0) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1496 | unittest(0, "could not create overlay from \"%s\"\n", |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1497 | overlay_path(overlay_nr + i)); |
| 1498 | return; |
| 1499 | } |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 1500 | ov_id[i] = ovcs_id; |
Pantelis Antoniou | 492a22a | 2015-04-07 22:23:49 +0300 | [diff] [blame] | 1501 | of_unittest_track_overlay(ov_id[i]); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | for (i = 0; i < 2; i++) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1505 | /* unittest device must be in after state */ |
| 1506 | if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY) |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1507 | != after) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1508 | unittest(0, "overlay @\"%s\" failed @\"%s\" %s\n", |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1509 | overlay_path(overlay_nr + i), |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1510 | unittest_path(unittest_nr + i, |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1511 | PDEV_OVERLAY), |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1512 | !after ? "enabled" : "disabled"); |
| 1513 | return; |
| 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | for (i = 1; i >= 0; i--) { |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 1518 | ovcs_id = ov_id[i]; |
| 1519 | ret = of_overlay_remove(&ovcs_id); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1520 | if (ret != 0) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1521 | unittest(0, "overlay @\"%s\" failed destroy @\"%s\"\n", |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1522 | overlay_path(overlay_nr + i), |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1523 | unittest_path(unittest_nr + i, |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1524 | PDEV_OVERLAY)); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1525 | return; |
| 1526 | } |
Pantelis Antoniou | 492a22a | 2015-04-07 22:23:49 +0300 | [diff] [blame] | 1527 | of_unittest_untrack_overlay(ov_id[i]); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | for (i = 0; i < 2; i++) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1531 | /* unittest device must be again in before state */ |
| 1532 | if (of_unittest_device_exists(unittest_nr + i, PDEV_OVERLAY) |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1533 | != before) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1534 | unittest(0, "overlay @\"%s\" with device @\"%s\" %s\n", |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1535 | overlay_path(overlay_nr + i), |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1536 | unittest_path(unittest_nr + i, |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1537 | PDEV_OVERLAY), |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1538 | !before ? "enabled" : "disabled"); |
| 1539 | return; |
| 1540 | } |
| 1541 | } |
| 1542 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1543 | unittest(1, "overlay test %d passed\n", 6); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | /* test overlay application in sequence */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1547 | static void of_unittest_overlay_8(void) |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1548 | { |
| 1549 | struct device_node *np; |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 1550 | int ret, i, ov_id[2], ovcs_id; |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1551 | int overlay_nr = 8, unittest_nr = 8; |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1552 | |
| 1553 | /* we don't care about device state in this test */ |
| 1554 | |
| 1555 | /* apply the overlays */ |
| 1556 | for (i = 0; i < 2; i++) { |
| 1557 | |
| 1558 | np = of_find_node_by_path(overlay_path(overlay_nr + i)); |
| 1559 | if (np == NULL) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1560 | unittest(0, "could not find overlay node @\"%s\"\n", |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1561 | overlay_path(overlay_nr + i)); |
| 1562 | return; |
| 1563 | } |
| 1564 | |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 1565 | ovcs_id = 0; |
| 1566 | ret = of_overlay_apply(np, &ovcs_id); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1567 | if (ret < 0) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1568 | unittest(0, "could not create overlay from \"%s\"\n", |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1569 | overlay_path(overlay_nr + i)); |
| 1570 | return; |
| 1571 | } |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 1572 | ov_id[i] = ovcs_id; |
Pantelis Antoniou | 492a22a | 2015-04-07 22:23:49 +0300 | [diff] [blame] | 1573 | of_unittest_track_overlay(ov_id[i]); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | /* now try to remove first overlay (it should fail) */ |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 1577 | ovcs_id = ov_id[0]; |
| 1578 | ret = of_overlay_remove(&ovcs_id); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1579 | if (ret == 0) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1580 | unittest(0, "overlay @\"%s\" was destroyed @\"%s\"\n", |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1581 | overlay_path(overlay_nr + 0), |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1582 | unittest_path(unittest_nr, |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1583 | PDEV_OVERLAY)); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1584 | return; |
| 1585 | } |
| 1586 | |
| 1587 | /* removing them in order should work */ |
| 1588 | for (i = 1; i >= 0; i--) { |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 1589 | ovcs_id = ov_id[i]; |
| 1590 | ret = of_overlay_remove(&ovcs_id); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1591 | if (ret != 0) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1592 | unittest(0, "overlay @\"%s\" not destroyed @\"%s\"\n", |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1593 | overlay_path(overlay_nr + i), |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1594 | unittest_path(unittest_nr, |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1595 | PDEV_OVERLAY)); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1596 | return; |
| 1597 | } |
Pantelis Antoniou | 492a22a | 2015-04-07 22:23:49 +0300 | [diff] [blame] | 1598 | of_unittest_untrack_overlay(ov_id[i]); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1599 | } |
| 1600 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1601 | unittest(1, "overlay test %d passed\n", 8); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1602 | } |
| 1603 | |
Pantelis Antoniou | 6b1271d | 2014-12-19 14:34:34 +0200 | [diff] [blame] | 1604 | /* test insertion of a bus with parent devices */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1605 | static void of_unittest_overlay_10(void) |
Pantelis Antoniou | 6b1271d | 2014-12-19 14:34:34 +0200 | [diff] [blame] | 1606 | { |
| 1607 | int ret; |
| 1608 | char *child_path; |
| 1609 | |
| 1610 | /* device should disable */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1611 | ret = of_unittest_apply_overlay_check(10, 10, 0, 1, PDEV_OVERLAY); |
| 1612 | if (unittest(ret == 0, |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1613 | "overlay test %d failed; overlay application\n", 10)) |
Pantelis Antoniou | 6b1271d | 2014-12-19 14:34:34 +0200 | [diff] [blame] | 1614 | return; |
| 1615 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1616 | child_path = kasprintf(GFP_KERNEL, "%s/test-unittest101", |
| 1617 | unittest_path(10, PDEV_OVERLAY)); |
| 1618 | if (unittest(child_path, "overlay test %d failed; kasprintf\n", 10)) |
Pantelis Antoniou | 6b1271d | 2014-12-19 14:34:34 +0200 | [diff] [blame] | 1619 | return; |
| 1620 | |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1621 | ret = of_path_device_type_exists(child_path, PDEV_OVERLAY); |
Pantelis Antoniou | 6b1271d | 2014-12-19 14:34:34 +0200 | [diff] [blame] | 1622 | kfree(child_path); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1623 | if (unittest(ret, "overlay test %d failed; no child device\n", 10)) |
Pantelis Antoniou | 6b1271d | 2014-12-19 14:34:34 +0200 | [diff] [blame] | 1624 | return; |
| 1625 | } |
| 1626 | |
| 1627 | /* test insertion of a bus with parent devices (and revert) */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1628 | static void of_unittest_overlay_11(void) |
Pantelis Antoniou | 6b1271d | 2014-12-19 14:34:34 +0200 | [diff] [blame] | 1629 | { |
| 1630 | int ret; |
| 1631 | |
| 1632 | /* device should disable */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1633 | ret = of_unittest_apply_revert_overlay_check(11, 11, 0, 1, |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1634 | PDEV_OVERLAY); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1635 | if (unittest(ret == 0, |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1636 | "overlay test %d failed; overlay application\n", 11)) |
Pantelis Antoniou | 6b1271d | 2014-12-19 14:34:34 +0200 | [diff] [blame] | 1637 | return; |
| 1638 | } |
| 1639 | |
Arnd Bergmann | 4252de3 | 2015-03-04 20:49:47 +0100 | [diff] [blame] | 1640 | #if IS_BUILTIN(CONFIG_I2C) && IS_ENABLED(CONFIG_OF_OVERLAY) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1641 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1642 | struct unittest_i2c_bus_data { |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1643 | struct platform_device *pdev; |
| 1644 | struct i2c_adapter adap; |
| 1645 | }; |
| 1646 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1647 | static int unittest_i2c_master_xfer(struct i2c_adapter *adap, |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1648 | struct i2c_msg *msgs, int num) |
| 1649 | { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1650 | struct unittest_i2c_bus_data *std = i2c_get_adapdata(adap); |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1651 | |
| 1652 | (void)std; |
| 1653 | |
| 1654 | return num; |
| 1655 | } |
| 1656 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1657 | static u32 unittest_i2c_functionality(struct i2c_adapter *adap) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1658 | { |
| 1659 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; |
| 1660 | } |
| 1661 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1662 | static const struct i2c_algorithm unittest_i2c_algo = { |
| 1663 | .master_xfer = unittest_i2c_master_xfer, |
| 1664 | .functionality = unittest_i2c_functionality, |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1665 | }; |
| 1666 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1667 | static int unittest_i2c_bus_probe(struct platform_device *pdev) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1668 | { |
| 1669 | struct device *dev = &pdev->dev; |
| 1670 | struct device_node *np = dev->of_node; |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1671 | struct unittest_i2c_bus_data *std; |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1672 | struct i2c_adapter *adap; |
| 1673 | int ret; |
| 1674 | |
| 1675 | if (np == NULL) { |
| 1676 | dev_err(dev, "No OF data for device\n"); |
| 1677 | return -EINVAL; |
| 1678 | |
| 1679 | } |
| 1680 | |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 1681 | dev_dbg(dev, "%s for node @%pOF\n", __func__, np); |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1682 | |
| 1683 | std = devm_kzalloc(dev, sizeof(*std), GFP_KERNEL); |
| 1684 | if (!std) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1685 | dev_err(dev, "Failed to allocate unittest i2c data\n"); |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1686 | return -ENOMEM; |
| 1687 | } |
| 1688 | |
| 1689 | /* link them together */ |
| 1690 | std->pdev = pdev; |
| 1691 | platform_set_drvdata(pdev, std); |
| 1692 | |
| 1693 | adap = &std->adap; |
| 1694 | i2c_set_adapdata(adap, std); |
| 1695 | adap->nr = -1; |
| 1696 | strlcpy(adap->name, pdev->name, sizeof(adap->name)); |
| 1697 | adap->class = I2C_CLASS_DEPRECATED; |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1698 | adap->algo = &unittest_i2c_algo; |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1699 | adap->dev.parent = dev; |
| 1700 | adap->dev.of_node = dev->of_node; |
| 1701 | adap->timeout = 5 * HZ; |
| 1702 | adap->retries = 3; |
| 1703 | |
| 1704 | ret = i2c_add_numbered_adapter(adap); |
| 1705 | if (ret != 0) { |
| 1706 | dev_err(dev, "Failed to add I2C adapter\n"); |
| 1707 | return ret; |
| 1708 | } |
| 1709 | |
| 1710 | return 0; |
| 1711 | } |
| 1712 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1713 | static int unittest_i2c_bus_remove(struct platform_device *pdev) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1714 | { |
| 1715 | struct device *dev = &pdev->dev; |
| 1716 | struct device_node *np = dev->of_node; |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1717 | struct unittest_i2c_bus_data *std = platform_get_drvdata(pdev); |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1718 | |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 1719 | dev_dbg(dev, "%s for node @%pOF\n", __func__, np); |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1720 | i2c_del_adapter(&std->adap); |
| 1721 | |
| 1722 | return 0; |
| 1723 | } |
| 1724 | |
Grant Likely | a2166ca | 2015-03-29 08:59:58 +0100 | [diff] [blame] | 1725 | static const struct of_device_id unittest_i2c_bus_match[] = { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1726 | { .compatible = "unittest-i2c-bus", }, |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1727 | {}, |
| 1728 | }; |
| 1729 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1730 | static struct platform_driver unittest_i2c_bus_driver = { |
| 1731 | .probe = unittest_i2c_bus_probe, |
| 1732 | .remove = unittest_i2c_bus_remove, |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1733 | .driver = { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1734 | .name = "unittest-i2c-bus", |
| 1735 | .of_match_table = of_match_ptr(unittest_i2c_bus_match), |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1736 | }, |
| 1737 | }; |
| 1738 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1739 | static int unittest_i2c_dev_probe(struct i2c_client *client, |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1740 | const struct i2c_device_id *id) |
| 1741 | { |
| 1742 | struct device *dev = &client->dev; |
| 1743 | struct device_node *np = client->dev.of_node; |
| 1744 | |
| 1745 | if (!np) { |
| 1746 | dev_err(dev, "No OF node\n"); |
| 1747 | return -EINVAL; |
| 1748 | } |
| 1749 | |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 1750 | dev_dbg(dev, "%s for node @%pOF\n", __func__, np); |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1751 | |
| 1752 | return 0; |
| 1753 | }; |
| 1754 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1755 | static int unittest_i2c_dev_remove(struct i2c_client *client) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1756 | { |
| 1757 | struct device *dev = &client->dev; |
| 1758 | struct device_node *np = client->dev.of_node; |
| 1759 | |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 1760 | dev_dbg(dev, "%s for node @%pOF\n", __func__, np); |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1761 | return 0; |
| 1762 | } |
| 1763 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1764 | static const struct i2c_device_id unittest_i2c_dev_id[] = { |
| 1765 | { .name = "unittest-i2c-dev" }, |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1766 | { } |
| 1767 | }; |
| 1768 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1769 | static struct i2c_driver unittest_i2c_dev_driver = { |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1770 | .driver = { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1771 | .name = "unittest-i2c-dev", |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1772 | }, |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1773 | .probe = unittest_i2c_dev_probe, |
| 1774 | .remove = unittest_i2c_dev_remove, |
| 1775 | .id_table = unittest_i2c_dev_id, |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1776 | }; |
| 1777 | |
Arnd Bergmann | 4252de3 | 2015-03-04 20:49:47 +0100 | [diff] [blame] | 1778 | #if IS_BUILTIN(CONFIG_I2C_MUX) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1779 | |
Peter Rosin | b6e3b71 | 2016-04-20 08:42:47 +0200 | [diff] [blame] | 1780 | static int unittest_i2c_mux_select_chan(struct i2c_mux_core *muxc, u32 chan) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1781 | { |
| 1782 | return 0; |
| 1783 | } |
| 1784 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1785 | static int unittest_i2c_mux_probe(struct i2c_client *client, |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1786 | const struct i2c_device_id *id) |
| 1787 | { |
Peter Rosin | b6e3b71 | 2016-04-20 08:42:47 +0200 | [diff] [blame] | 1788 | int ret, i, nchans; |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1789 | struct device *dev = &client->dev; |
| 1790 | struct i2c_adapter *adap = to_i2c_adapter(dev->parent); |
| 1791 | struct device_node *np = client->dev.of_node, *child; |
Peter Rosin | b6e3b71 | 2016-04-20 08:42:47 +0200 | [diff] [blame] | 1792 | struct i2c_mux_core *muxc; |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1793 | u32 reg, max_reg; |
| 1794 | |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 1795 | dev_dbg(dev, "%s for node @%pOF\n", __func__, np); |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1796 | |
| 1797 | if (!np) { |
| 1798 | dev_err(dev, "No OF node\n"); |
| 1799 | return -EINVAL; |
| 1800 | } |
| 1801 | |
| 1802 | max_reg = (u32)-1; |
| 1803 | for_each_child_of_node(np, child) { |
| 1804 | ret = of_property_read_u32(child, "reg", ®); |
| 1805 | if (ret) |
| 1806 | continue; |
| 1807 | if (max_reg == (u32)-1 || reg > max_reg) |
| 1808 | max_reg = reg; |
| 1809 | } |
| 1810 | nchans = max_reg == (u32)-1 ? 0 : max_reg + 1; |
| 1811 | if (nchans == 0) { |
| 1812 | dev_err(dev, "No channels\n"); |
| 1813 | return -EINVAL; |
| 1814 | } |
| 1815 | |
Peter Rosin | b6e3b71 | 2016-04-20 08:42:47 +0200 | [diff] [blame] | 1816 | muxc = i2c_mux_alloc(adap, dev, nchans, 0, 0, |
| 1817 | unittest_i2c_mux_select_chan, NULL); |
| 1818 | if (!muxc) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1819 | return -ENOMEM; |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1820 | for (i = 0; i < nchans; i++) { |
Peter Rosin | b6e3b71 | 2016-04-20 08:42:47 +0200 | [diff] [blame] | 1821 | ret = i2c_mux_add_adapter(muxc, 0, i, 0); |
| 1822 | if (ret) { |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1823 | dev_err(dev, "Failed to register mux #%d\n", i); |
Peter Rosin | b6e3b71 | 2016-04-20 08:42:47 +0200 | [diff] [blame] | 1824 | i2c_mux_del_adapters(muxc); |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1825 | return -ENODEV; |
| 1826 | } |
| 1827 | } |
| 1828 | |
Peter Rosin | b6e3b71 | 2016-04-20 08:42:47 +0200 | [diff] [blame] | 1829 | i2c_set_clientdata(client, muxc); |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1830 | |
| 1831 | return 0; |
| 1832 | }; |
| 1833 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1834 | static int unittest_i2c_mux_remove(struct i2c_client *client) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1835 | { |
| 1836 | struct device *dev = &client->dev; |
| 1837 | struct device_node *np = client->dev.of_node; |
Peter Rosin | b6e3b71 | 2016-04-20 08:42:47 +0200 | [diff] [blame] | 1838 | struct i2c_mux_core *muxc = i2c_get_clientdata(client); |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1839 | |
Rob Herring | 0d638a0 | 2017-06-01 15:50:55 -0500 | [diff] [blame] | 1840 | dev_dbg(dev, "%s for node @%pOF\n", __func__, np); |
Peter Rosin | b6e3b71 | 2016-04-20 08:42:47 +0200 | [diff] [blame] | 1841 | i2c_mux_del_adapters(muxc); |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1842 | return 0; |
| 1843 | } |
| 1844 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1845 | static const struct i2c_device_id unittest_i2c_mux_id[] = { |
| 1846 | { .name = "unittest-i2c-mux" }, |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1847 | { } |
| 1848 | }; |
| 1849 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1850 | static struct i2c_driver unittest_i2c_mux_driver = { |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1851 | .driver = { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1852 | .name = "unittest-i2c-mux", |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1853 | }, |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1854 | .probe = unittest_i2c_mux_probe, |
| 1855 | .remove = unittest_i2c_mux_remove, |
| 1856 | .id_table = unittest_i2c_mux_id, |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1857 | }; |
| 1858 | |
| 1859 | #endif |
| 1860 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1861 | static int of_unittest_overlay_i2c_init(void) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1862 | { |
| 1863 | int ret; |
| 1864 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1865 | ret = i2c_add_driver(&unittest_i2c_dev_driver); |
| 1866 | if (unittest(ret == 0, |
| 1867 | "could not register unittest i2c device driver\n")) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1868 | return ret; |
| 1869 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1870 | ret = platform_driver_register(&unittest_i2c_bus_driver); |
| 1871 | if (unittest(ret == 0, |
| 1872 | "could not register unittest i2c bus driver\n")) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1873 | return ret; |
| 1874 | |
Arnd Bergmann | 4252de3 | 2015-03-04 20:49:47 +0100 | [diff] [blame] | 1875 | #if IS_BUILTIN(CONFIG_I2C_MUX) |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1876 | ret = i2c_add_driver(&unittest_i2c_mux_driver); |
| 1877 | if (unittest(ret == 0, |
| 1878 | "could not register unittest i2c mux driver\n")) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1879 | return ret; |
| 1880 | #endif |
| 1881 | |
| 1882 | return 0; |
| 1883 | } |
| 1884 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1885 | static void of_unittest_overlay_i2c_cleanup(void) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1886 | { |
Arnd Bergmann | 4252de3 | 2015-03-04 20:49:47 +0100 | [diff] [blame] | 1887 | #if IS_BUILTIN(CONFIG_I2C_MUX) |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1888 | i2c_del_driver(&unittest_i2c_mux_driver); |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1889 | #endif |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1890 | platform_driver_unregister(&unittest_i2c_bus_driver); |
| 1891 | i2c_del_driver(&unittest_i2c_dev_driver); |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1892 | } |
| 1893 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1894 | static void of_unittest_overlay_i2c_12(void) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1895 | { |
| 1896 | int ret; |
| 1897 | |
| 1898 | /* device should enable */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1899 | ret = of_unittest_apply_overlay_check(12, 12, 0, 1, I2C_OVERLAY); |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1900 | if (ret != 0) |
| 1901 | return; |
| 1902 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1903 | unittest(1, "overlay test %d passed\n", 12); |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1904 | } |
| 1905 | |
| 1906 | /* test deactivation of device */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1907 | static void of_unittest_overlay_i2c_13(void) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1908 | { |
| 1909 | int ret; |
| 1910 | |
| 1911 | /* device should disable */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1912 | ret = of_unittest_apply_overlay_check(13, 13, 1, 0, I2C_OVERLAY); |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1913 | if (ret != 0) |
| 1914 | return; |
| 1915 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1916 | unittest(1, "overlay test %d passed\n", 13); |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1917 | } |
| 1918 | |
| 1919 | /* just check for i2c mux existence */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1920 | static void of_unittest_overlay_i2c_14(void) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1921 | { |
| 1922 | } |
| 1923 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1924 | static void of_unittest_overlay_i2c_15(void) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1925 | { |
| 1926 | int ret; |
| 1927 | |
| 1928 | /* device should enable */ |
Alexander Sverdlin | ca7b896 | 2017-01-19 11:06:16 +0100 | [diff] [blame] | 1929 | ret = of_unittest_apply_overlay_check(15, 15, 0, 1, I2C_OVERLAY); |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1930 | if (ret != 0) |
| 1931 | return; |
| 1932 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1933 | unittest(1, "overlay test %d passed\n", 15); |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | #else |
| 1937 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1938 | static inline void of_unittest_overlay_i2c_14(void) { } |
| 1939 | static inline void of_unittest_overlay_i2c_15(void) { } |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1940 | |
| 1941 | #endif |
| 1942 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1943 | static void __init of_unittest_overlay(void) |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1944 | { |
| 1945 | struct device_node *bus_np = NULL; |
| 1946 | int ret; |
| 1947 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1948 | ret = platform_driver_register(&unittest_driver); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1949 | if (ret != 0) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1950 | unittest(0, "could not register unittest driver\n"); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1951 | goto out; |
| 1952 | } |
| 1953 | |
| 1954 | bus_np = of_find_node_by_path(bus_path); |
| 1955 | if (bus_np == NULL) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1956 | unittest(0, "could not find bus_path \"%s\"\n", bus_path); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1957 | goto out; |
| 1958 | } |
| 1959 | |
Kefeng Wang | 146dedb | 2016-06-01 14:53:10 +0800 | [diff] [blame] | 1960 | ret = of_platform_default_populate(bus_np, NULL, NULL); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1961 | if (ret != 0) { |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1962 | unittest(0, "could not populate bus @ \"%s\"\n", bus_path); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1963 | goto out; |
| 1964 | } |
| 1965 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1966 | if (!of_unittest_device_exists(100, PDEV_OVERLAY)) { |
| 1967 | unittest(0, "could not find unittest0 @ \"%s\"\n", |
| 1968 | unittest_path(100, PDEV_OVERLAY)); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1969 | goto out; |
| 1970 | } |
| 1971 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1972 | if (of_unittest_device_exists(101, PDEV_OVERLAY)) { |
| 1973 | unittest(0, "unittest1 @ \"%s\" should not exist\n", |
| 1974 | unittest_path(101, PDEV_OVERLAY)); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1975 | goto out; |
| 1976 | } |
| 1977 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1978 | unittest(1, "basic infrastructure of overlays passed"); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1979 | |
| 1980 | /* tests in sequence */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1981 | of_unittest_overlay_0(); |
| 1982 | of_unittest_overlay_1(); |
| 1983 | of_unittest_overlay_2(); |
| 1984 | of_unittest_overlay_3(); |
| 1985 | of_unittest_overlay_4(); |
| 1986 | of_unittest_overlay_5(); |
| 1987 | of_unittest_overlay_6(); |
| 1988 | of_unittest_overlay_8(); |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 1989 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1990 | of_unittest_overlay_10(); |
| 1991 | of_unittest_overlay_11(); |
Pantelis Antoniou | 6b1271d | 2014-12-19 14:34:34 +0200 | [diff] [blame] | 1992 | |
Arnd Bergmann | 4252de3 | 2015-03-04 20:49:47 +0100 | [diff] [blame] | 1993 | #if IS_BUILTIN(CONFIG_I2C) |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1994 | if (unittest(of_unittest_overlay_i2c_init() == 0, "i2c init failed\n")) |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 1995 | goto out; |
| 1996 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 1997 | of_unittest_overlay_i2c_12(); |
| 1998 | of_unittest_overlay_i2c_13(); |
| 1999 | of_unittest_overlay_i2c_14(); |
| 2000 | of_unittest_overlay_i2c_15(); |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 2001 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 2002 | of_unittest_overlay_i2c_cleanup(); |
Pantelis Antoniou | d5e7550 | 2015-01-12 19:02:49 +0200 | [diff] [blame] | 2003 | #endif |
| 2004 | |
Pantelis Antoniou | 492a22a | 2015-04-07 22:23:49 +0300 | [diff] [blame] | 2005 | of_unittest_destroy_tracked_overlays(); |
| 2006 | |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 2007 | out: |
| 2008 | of_node_put(bus_np); |
| 2009 | } |
| 2010 | |
| 2011 | #else |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 2012 | static inline void __init of_unittest_overlay(void) { } |
Pantelis Antoniou | 177d271 | 2014-10-28 22:35:59 +0200 | [diff] [blame] | 2013 | #endif |
| 2014 | |
Frank Rowand | 60a0004 | 2017-07-19 09:25:20 -0700 | [diff] [blame] | 2015 | #ifdef CONFIG_OF_OVERLAY |
| 2016 | |
Frank Rowand | 81d0848f | 2017-04-25 17:09:54 -0700 | [diff] [blame] | 2017 | /* |
| 2018 | * __dtb_ot_begin[] and __dtb_ot_end[] are created by cmd_dt_S_dtb |
| 2019 | * in scripts/Makefile.lib |
| 2020 | */ |
| 2021 | |
| 2022 | #define OVERLAY_INFO_EXTERN(name) \ |
| 2023 | extern uint8_t __dtb_##name##_begin[]; \ |
| 2024 | extern uint8_t __dtb_##name##_end[] |
| 2025 | |
| 2026 | #define OVERLAY_INFO(name, expected) \ |
| 2027 | { .dtb_begin = __dtb_##name##_begin, \ |
| 2028 | .dtb_end = __dtb_##name##_end, \ |
| 2029 | .expected_result = expected, \ |
| 2030 | } |
| 2031 | |
| 2032 | struct overlay_info { |
| 2033 | uint8_t *dtb_begin; |
| 2034 | uint8_t *dtb_end; |
| 2035 | void *data; |
| 2036 | struct device_node *np_overlay; |
| 2037 | int expected_result; |
| 2038 | int overlay_id; |
| 2039 | }; |
| 2040 | |
| 2041 | OVERLAY_INFO_EXTERN(overlay_base); |
| 2042 | OVERLAY_INFO_EXTERN(overlay); |
| 2043 | OVERLAY_INFO_EXTERN(overlay_bad_phandle); |
Frank Rowand | 60a0004 | 2017-07-19 09:25:20 -0700 | [diff] [blame] | 2044 | OVERLAY_INFO_EXTERN(overlay_bad_symbol); |
Frank Rowand | 81d0848f | 2017-04-25 17:09:54 -0700 | [diff] [blame] | 2045 | |
| 2046 | /* order of entries is hard-coded into users of overlays[] */ |
| 2047 | static struct overlay_info overlays[] = { |
| 2048 | OVERLAY_INFO(overlay_base, -9999), |
| 2049 | OVERLAY_INFO(overlay, 0), |
| 2050 | OVERLAY_INFO(overlay_bad_phandle, -EINVAL), |
Frank Rowand | 60a0004 | 2017-07-19 09:25:20 -0700 | [diff] [blame] | 2051 | OVERLAY_INFO(overlay_bad_symbol, -EINVAL), |
Frank Rowand | 81d0848f | 2017-04-25 17:09:54 -0700 | [diff] [blame] | 2052 | {} |
| 2053 | }; |
| 2054 | |
| 2055 | static struct device_node *overlay_base_root; |
| 2056 | |
Rob Herring | 0fa1c57 | 2018-01-05 15:32:33 -0600 | [diff] [blame] | 2057 | static void * __init dt_alloc_memory(u64 size, u64 align) |
| 2058 | { |
| 2059 | return memblock_virt_alloc(size, align); |
| 2060 | } |
| 2061 | |
Frank Rowand | 81d0848f | 2017-04-25 17:09:54 -0700 | [diff] [blame] | 2062 | /* |
| 2063 | * Create base device tree for the overlay unittest. |
| 2064 | * |
| 2065 | * This is called from very early boot code. |
| 2066 | * |
| 2067 | * Do as much as possible the same way as done in __unflatten_device_tree |
| 2068 | * and other early boot steps for the normal FDT so that the overlay base |
| 2069 | * unflattened tree will have the same characteristics as the real tree |
| 2070 | * (such as having memory allocated by the early allocator). The goal |
| 2071 | * is to test "the real thing" as much as possible, and test "test setup |
| 2072 | * code" as little as possible. |
| 2073 | * |
| 2074 | * Have to stop before resolving phandles, because that uses kmalloc. |
| 2075 | */ |
| 2076 | void __init unittest_unflatten_overlay_base(void) |
| 2077 | { |
| 2078 | struct overlay_info *info; |
| 2079 | u32 data_size; |
| 2080 | u32 size; |
| 2081 | |
| 2082 | info = &overlays[0]; |
| 2083 | |
| 2084 | if (info->expected_result != -9999) { |
| 2085 | pr_err("No dtb 'overlay_base' to attach\n"); |
| 2086 | return; |
| 2087 | } |
| 2088 | |
| 2089 | data_size = info->dtb_end - info->dtb_begin; |
| 2090 | if (!data_size) { |
| 2091 | pr_err("No dtb 'overlay_base' to attach\n"); |
| 2092 | return; |
| 2093 | } |
| 2094 | |
| 2095 | size = fdt_totalsize(info->dtb_begin); |
| 2096 | if (size != data_size) { |
| 2097 | pr_err("dtb 'overlay_base' header totalsize != actual size"); |
| 2098 | return; |
| 2099 | } |
| 2100 | |
Rob Herring | 0fa1c57 | 2018-01-05 15:32:33 -0600 | [diff] [blame] | 2101 | info->data = dt_alloc_memory(size, roundup_pow_of_two(FDT_V17_SIZE)); |
Frank Rowand | 81d0848f | 2017-04-25 17:09:54 -0700 | [diff] [blame] | 2102 | if (!info->data) { |
| 2103 | pr_err("alloc for dtb 'overlay_base' failed"); |
| 2104 | return; |
| 2105 | } |
| 2106 | |
| 2107 | memcpy(info->data, info->dtb_begin, size); |
| 2108 | |
| 2109 | __unflatten_device_tree(info->data, NULL, &info->np_overlay, |
Rob Herring | 0fa1c57 | 2018-01-05 15:32:33 -0600 | [diff] [blame] | 2110 | dt_alloc_memory, true); |
Frank Rowand | 81d0848f | 2017-04-25 17:09:54 -0700 | [diff] [blame] | 2111 | overlay_base_root = info->np_overlay; |
| 2112 | } |
| 2113 | |
| 2114 | /* |
| 2115 | * The purpose of of_unittest_overlay_data_add is to add an |
| 2116 | * overlay in the normal fashion. This is a test of the whole |
| 2117 | * picture, instead of testing individual elements. |
| 2118 | * |
| 2119 | * A secondary purpose is to be able to verify that the contents of |
| 2120 | * /proc/device-tree/ contains the updated structure and values from |
| 2121 | * the overlay. That must be verified separately in user space. |
| 2122 | * |
| 2123 | * Return 0 on unexpected error. |
| 2124 | */ |
| 2125 | static int __init overlay_data_add(int onum) |
| 2126 | { |
| 2127 | struct overlay_info *info; |
| 2128 | int k; |
| 2129 | int ret; |
| 2130 | u32 size; |
| 2131 | u32 size_from_header; |
| 2132 | |
| 2133 | for (k = 0, info = overlays; info; info++, k++) { |
| 2134 | if (k == onum) |
| 2135 | break; |
| 2136 | } |
| 2137 | if (onum > k) |
| 2138 | return 0; |
| 2139 | |
| 2140 | size = info->dtb_end - info->dtb_begin; |
| 2141 | if (!size) { |
| 2142 | pr_err("no overlay to attach, %d\n", onum); |
| 2143 | ret = 0; |
| 2144 | } |
| 2145 | |
| 2146 | size_from_header = fdt_totalsize(info->dtb_begin); |
| 2147 | if (size_from_header != size) { |
| 2148 | pr_err("overlay header totalsize != actual size, %d", onum); |
| 2149 | return 0; |
| 2150 | } |
| 2151 | |
| 2152 | /* |
| 2153 | * Must create permanent copy of FDT because of_fdt_unflatten_tree() |
| 2154 | * will create pointers to the passed in FDT in the EDT. |
| 2155 | */ |
| 2156 | info->data = kmemdup(info->dtb_begin, size, GFP_KERNEL); |
| 2157 | if (!info->data) { |
| 2158 | pr_err("unable to allocate memory for data, %d\n", onum); |
| 2159 | return 0; |
| 2160 | } |
| 2161 | |
| 2162 | of_fdt_unflatten_tree(info->data, NULL, &info->np_overlay); |
| 2163 | if (!info->np_overlay) { |
| 2164 | pr_err("unable to unflatten overlay, %d\n", onum); |
| 2165 | ret = 0; |
| 2166 | goto out_free_data; |
| 2167 | } |
Frank Rowand | 81d0848f | 2017-04-25 17:09:54 -0700 | [diff] [blame] | 2168 | |
Frank Rowand | 24789c5 | 2017-10-17 16:36:26 -0700 | [diff] [blame] | 2169 | info->overlay_id = 0; |
| 2170 | ret = of_overlay_apply(info->np_overlay, &info->overlay_id); |
Frank Rowand | 81d0848f | 2017-04-25 17:09:54 -0700 | [diff] [blame] | 2171 | if (ret < 0) { |
Frank Rowand | 0290c4c | 2017-10-17 16:36:23 -0700 | [diff] [blame] | 2172 | pr_err("of_overlay_apply() (ret=%d), %d\n", ret, onum); |
Frank Rowand | 81d0848f | 2017-04-25 17:09:54 -0700 | [diff] [blame] | 2173 | goto out_free_np_overlay; |
Frank Rowand | 81d0848f | 2017-04-25 17:09:54 -0700 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | pr_debug("__dtb_overlay_begin applied, overlay id %d\n", ret); |
| 2177 | |
| 2178 | goto out; |
| 2179 | |
| 2180 | out_free_np_overlay: |
| 2181 | /* |
| 2182 | * info->np_overlay is the unflattened device tree |
| 2183 | * It has not been spliced into the live tree. |
| 2184 | */ |
| 2185 | |
| 2186 | /* todo: function to free unflattened device tree */ |
| 2187 | |
| 2188 | out_free_data: |
| 2189 | kfree(info->data); |
| 2190 | |
| 2191 | out: |
| 2192 | return (ret == info->expected_result); |
| 2193 | } |
| 2194 | |
| 2195 | /* |
| 2196 | * The purpose of of_unittest_overlay_high_level is to add an overlay |
| 2197 | * in the normal fashion. This is a test of the whole picture, |
| 2198 | * instead of individual elements. |
| 2199 | * |
| 2200 | * The first part of the function is _not_ normal overlay usage; it is |
| 2201 | * finishing splicing the base overlay device tree into the live tree. |
| 2202 | */ |
| 2203 | static __init void of_unittest_overlay_high_level(void) |
| 2204 | { |
| 2205 | struct device_node *last_sibling; |
| 2206 | struct device_node *np; |
| 2207 | struct device_node *of_symbols; |
| 2208 | struct device_node *overlay_base_symbols; |
| 2209 | struct device_node **pprev; |
| 2210 | struct property *prop; |
| 2211 | int ret; |
| 2212 | |
| 2213 | if (!overlay_base_root) { |
| 2214 | unittest(0, "overlay_base_root not initialized\n"); |
| 2215 | return; |
| 2216 | } |
| 2217 | |
| 2218 | /* |
| 2219 | * Could not fixup phandles in unittest_unflatten_overlay_base() |
| 2220 | * because kmalloc() was not yet available. |
| 2221 | */ |
Frank Rowand | f948d6d | 2017-10-17 16:36:29 -0700 | [diff] [blame] | 2222 | of_overlay_mutex_lock(); |
Frank Rowand | 81d0848f | 2017-04-25 17:09:54 -0700 | [diff] [blame] | 2223 | of_resolve_phandles(overlay_base_root); |
Frank Rowand | f948d6d | 2017-10-17 16:36:29 -0700 | [diff] [blame] | 2224 | of_overlay_mutex_unlock(); |
| 2225 | |
Frank Rowand | 81d0848f | 2017-04-25 17:09:54 -0700 | [diff] [blame] | 2226 | |
| 2227 | /* |
| 2228 | * do not allow overlay_base to duplicate any node already in |
| 2229 | * tree, this greatly simplifies the code |
| 2230 | */ |
| 2231 | |
| 2232 | /* |
| 2233 | * remove overlay_base_root node "__local_fixups", after |
| 2234 | * being used by of_resolve_phandles() |
| 2235 | */ |
| 2236 | pprev = &overlay_base_root->child; |
| 2237 | for (np = overlay_base_root->child; np; np = np->sibling) { |
| 2238 | if (!of_node_cmp(np->name, "__local_fixups__")) { |
| 2239 | *pprev = np->sibling; |
| 2240 | break; |
| 2241 | } |
| 2242 | pprev = &np->sibling; |
| 2243 | } |
| 2244 | |
| 2245 | /* remove overlay_base_root node "__symbols__" if in live tree */ |
| 2246 | of_symbols = of_get_child_by_name(of_root, "__symbols__"); |
| 2247 | if (of_symbols) { |
| 2248 | /* will have to graft properties from node into live tree */ |
| 2249 | pprev = &overlay_base_root->child; |
| 2250 | for (np = overlay_base_root->child; np; np = np->sibling) { |
| 2251 | if (!of_node_cmp(np->name, "__symbols__")) { |
| 2252 | overlay_base_symbols = np; |
| 2253 | *pprev = np->sibling; |
| 2254 | break; |
| 2255 | } |
| 2256 | pprev = &np->sibling; |
| 2257 | } |
| 2258 | } |
| 2259 | |
| 2260 | for (np = overlay_base_root->child; np; np = np->sibling) { |
| 2261 | if (of_get_child_by_name(of_root, np->name)) { |
| 2262 | unittest(0, "illegal node name in overlay_base %s", |
| 2263 | np->name); |
| 2264 | return; |
| 2265 | } |
| 2266 | } |
| 2267 | |
| 2268 | /* |
| 2269 | * overlay 'overlay_base' is not allowed to have root |
| 2270 | * properties, so only need to splice nodes into main device tree. |
| 2271 | * |
| 2272 | * root node of *overlay_base_root will not be freed, it is lost |
| 2273 | * memory. |
| 2274 | */ |
| 2275 | |
| 2276 | for (np = overlay_base_root->child; np; np = np->sibling) |
| 2277 | np->parent = of_root; |
| 2278 | |
| 2279 | mutex_lock(&of_mutex); |
| 2280 | |
Arnd Bergmann | ee320b3 | 2017-04-28 11:44:11 +0200 | [diff] [blame] | 2281 | for (last_sibling = np = of_root->child; np; np = np->sibling) |
Frank Rowand | 81d0848f | 2017-04-25 17:09:54 -0700 | [diff] [blame] | 2282 | last_sibling = np; |
| 2283 | |
| 2284 | if (last_sibling) |
| 2285 | last_sibling->sibling = overlay_base_root->child; |
| 2286 | else |
| 2287 | of_root->child = overlay_base_root->child; |
| 2288 | |
| 2289 | for_each_of_allnodes_from(overlay_base_root, np) |
| 2290 | __of_attach_node_sysfs(np); |
| 2291 | |
| 2292 | if (of_symbols) { |
| 2293 | for_each_property_of_node(overlay_base_symbols, prop) { |
| 2294 | ret = __of_add_property(of_symbols, prop); |
| 2295 | if (ret) { |
| 2296 | unittest(0, |
| 2297 | "duplicate property '%s' in overlay_base node __symbols__", |
| 2298 | prop->name); |
Dan Carpenter | 8756cd1 | 2017-05-03 22:49:50 +0300 | [diff] [blame] | 2299 | goto err_unlock; |
Frank Rowand | 81d0848f | 2017-04-25 17:09:54 -0700 | [diff] [blame] | 2300 | } |
| 2301 | ret = __of_add_property_sysfs(of_symbols, prop); |
| 2302 | if (ret) { |
| 2303 | unittest(0, |
| 2304 | "unable to add property '%s' in overlay_base node __symbols__ to sysfs", |
| 2305 | prop->name); |
Dan Carpenter | 8756cd1 | 2017-05-03 22:49:50 +0300 | [diff] [blame] | 2306 | goto err_unlock; |
Frank Rowand | 81d0848f | 2017-04-25 17:09:54 -0700 | [diff] [blame] | 2307 | } |
| 2308 | } |
| 2309 | } |
| 2310 | |
| 2311 | mutex_unlock(&of_mutex); |
| 2312 | |
| 2313 | |
| 2314 | /* now do the normal overlay usage test */ |
| 2315 | |
| 2316 | unittest(overlay_data_add(1), |
| 2317 | "Adding overlay 'overlay' failed\n"); |
| 2318 | |
| 2319 | unittest(overlay_data_add(2), |
| 2320 | "Adding overlay 'overlay_bad_phandle' failed\n"); |
Frank Rowand | 60a0004 | 2017-07-19 09:25:20 -0700 | [diff] [blame] | 2321 | |
| 2322 | unittest(overlay_data_add(3), |
| 2323 | "Adding overlay 'overlay_bad_symbol' failed\n"); |
| 2324 | |
Dan Carpenter | 8756cd1 | 2017-05-03 22:49:50 +0300 | [diff] [blame] | 2325 | return; |
| 2326 | |
| 2327 | err_unlock: |
| 2328 | mutex_unlock(&of_mutex); |
Frank Rowand | 81d0848f | 2017-04-25 17:09:54 -0700 | [diff] [blame] | 2329 | } |
| 2330 | |
| 2331 | #else |
| 2332 | |
| 2333 | static inline __init void of_unittest_overlay_high_level(void) {} |
| 2334 | |
| 2335 | #endif |
| 2336 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 2337 | static int __init of_unittest(void) |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 2338 | { |
| 2339 | struct device_node *np; |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 2340 | int res; |
| 2341 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 2342 | /* adding data for unittest */ |
| 2343 | res = unittest_data_add(); |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 2344 | if (res) |
| 2345 | return res; |
Grant Likely | 788ec2f | 2014-11-19 17:13:44 +0000 | [diff] [blame] | 2346 | if (!of_aliases) |
| 2347 | of_aliases = of_find_node_by_path("/aliases"); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 2348 | |
| 2349 | np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a"); |
| 2350 | if (!np) { |
| 2351 | pr_info("No testcase data in device tree; not running tests\n"); |
| 2352 | return 0; |
| 2353 | } |
| 2354 | of_node_put(np); |
| 2355 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 2356 | pr_info("start of unittest - you will see error messages\n"); |
| 2357 | of_unittest_check_tree_linkage(); |
| 2358 | of_unittest_check_phandles(); |
| 2359 | of_unittest_find_node_by_name(); |
| 2360 | of_unittest_dynamic(); |
| 2361 | of_unittest_parse_phandle_with_args(); |
Pantelis Antoniou | ce4fecf | 2015-01-21 19:06:14 +0200 | [diff] [blame] | 2362 | of_unittest_printf(); |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 2363 | of_unittest_property_string(); |
| 2364 | of_unittest_property_copy(); |
| 2365 | of_unittest_changeset(); |
| 2366 | of_unittest_parse_interrupts(); |
| 2367 | of_unittest_parse_interrupts_extended(); |
| 2368 | of_unittest_match_node(); |
| 2369 | of_unittest_platform_populate(); |
| 2370 | of_unittest_overlay(); |
Gaurav Minocha | ae9304c | 2014-07-16 23:09:39 -0700 | [diff] [blame] | 2371 | |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 2372 | /* Double check linkage after removing testcase data */ |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 2373 | of_unittest_check_tree_linkage(); |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 2374 | |
Frank Rowand | 81d0848f | 2017-04-25 17:09:54 -0700 | [diff] [blame] | 2375 | of_unittest_overlay_high_level(); |
| 2376 | |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 2377 | pr_info("end of unittest - %i passed, %i failed\n", |
| 2378 | unittest_results.passed, unittest_results.failed); |
Grant Likely | f2051d6 | 2014-10-01 17:40:22 +0100 | [diff] [blame] | 2379 | |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 2380 | return 0; |
| 2381 | } |
Wang Long | 9697a55 | 2015-03-11 08:36:54 +0000 | [diff] [blame] | 2382 | late_initcall(of_unittest); |