unittest.c (c95baf12f5077419db01313ab61c2aac007d40cd) unittest.c (f4056e705b2ef7f123a188f6aee23ade70e7d793)
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Self tests for device tree subsystem
4 */
5
6#define pr_fmt(fmt) "### dt-test ### " fmt
7
8#include <linux/memblock.h>

--- 10 unchanged lines hidden (view full) ---

19#include <linux/list.h>
20#include <linux/mutex.h>
21#include <linux/slab.h>
22#include <linux/device.h>
23#include <linux/platform_device.h>
24
25#include <linux/i2c.h>
26#include <linux/i2c-mux.h>
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Self tests for device tree subsystem
4 */
5
6#define pr_fmt(fmt) "### dt-test ### " fmt
7
8#include <linux/memblock.h>

--- 10 unchanged lines hidden (view full) ---

19#include <linux/list.h>
20#include <linux/mutex.h>
21#include <linux/slab.h>
22#include <linux/device.h>
23#include <linux/platform_device.h>
24
25#include <linux/i2c.h>
26#include <linux/i2c-mux.h>
27#include <linux/gpio/driver.h>
27
28#include <linux/bitops.h>
29
30#include "of_private.h"
31
32static struct unittest_results {
33 int passed;
34 int failed;

--- 6 unchanged lines hidden (view full) ---

41 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
42 } else { \
43 unittest_results.passed++; \
44 pr_debug("pass %s():%i\n", __func__, __LINE__); \
45 } \
46 failed; \
47})
48
28
29#include <linux/bitops.h>
30
31#include "of_private.h"
32
33static struct unittest_results {
34 int passed;
35 int failed;

--- 6 unchanged lines hidden (view full) ---

42 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
43 } else { \
44 unittest_results.passed++; \
45 pr_debug("pass %s():%i\n", __func__, __LINE__); \
46 } \
47 failed; \
48})
49
50/*
51 * Expected message may have a message level other than KERN_INFO.
52 * Print the expected message only if the current loglevel will allow
53 * the actual message to print.
54 */
55#define EXPECT_BEGIN(level, fmt, ...) \
56 printk(level pr_fmt("EXPECT \\ : ") fmt, ##__VA_ARGS__)
57
58#define EXPECT_END(level, fmt, ...) \
59 printk(level pr_fmt("EXPECT / : ") fmt, ##__VA_ARGS__)
60
61struct unittest_gpio_dev {
62 struct gpio_chip chip;
63};
64
65static int unittest_gpio_chip_request_count;
66static int unittest_gpio_probe_count;
67static int unittest_gpio_probe_pass_count;
68
69static int unittest_gpio_chip_request(struct gpio_chip *chip, unsigned int offset)
70{
71 unittest_gpio_chip_request_count++;
72
73 pr_debug("%s(): %s %d %d\n", __func__, chip->label, offset,
74 unittest_gpio_chip_request_count);
75 return 0;
76}
77
78static int unittest_gpio_probe(struct platform_device *pdev)
79{
80 struct unittest_gpio_dev *devptr;
81 int ret;
82
83 unittest_gpio_probe_count++;
84
85 devptr = kzalloc(sizeof(*devptr), GFP_KERNEL);
86 if (!devptr)
87 return -ENOMEM;
88
89 platform_set_drvdata(pdev, devptr);
90
91 devptr->chip.of_node = pdev->dev.of_node;
92 devptr->chip.label = "of-unittest-gpio";
93 devptr->chip.base = -1; /* dynamic allocation */
94 devptr->chip.ngpio = 5;
95 devptr->chip.request = unittest_gpio_chip_request;
96
97 ret = gpiochip_add_data(&devptr->chip, NULL);
98
99 unittest(!ret,
100 "gpiochip_add_data() for node @%pOF failed, ret = %d\n", devptr->chip.of_node, ret);
101
102 if (!ret)
103 unittest_gpio_probe_pass_count++;
104 return ret;
105}
106
107static int unittest_gpio_remove(struct platform_device *pdev)
108{
109 struct unittest_gpio_dev *gdev = platform_get_drvdata(pdev);
110 struct device *dev = &pdev->dev;
111 struct device_node *np = pdev->dev.of_node;
112
113 dev_dbg(dev, "%s for node @%pOF\n", __func__, np);
114
115 if (!gdev)
116 return -EINVAL;
117
118 if (gdev->chip.base != -1)
119 gpiochip_remove(&gdev->chip);
120
121 platform_set_drvdata(pdev, NULL);
122 kfree(pdev);
123
124 return 0;
125}
126
127static const struct of_device_id unittest_gpio_id[] = {
128 { .compatible = "unittest-gpio", },
129 {}
130};
131
132static struct platform_driver unittest_gpio_driver = {
133 .probe = unittest_gpio_probe,
134 .remove = unittest_gpio_remove,
135 .driver = {
136 .name = "unittest-gpio",
137 .of_match_table = of_match_ptr(unittest_gpio_id),
138 },
139};
140
49static void __init of_unittest_find_node_by_name(void)
50{
51 struct device_node *np;
52 const char *options, *name;
53
54 np = of_find_node_by_path("/testcase-data");
55 name = kasprintf(GFP_KERNEL, "%pOF", np);
56 unittest(np && !strcmp("/testcase-data", name),

--- 2121 unchanged lines hidden (view full) ---

2178
2179#else
2180
2181static inline void of_unittest_overlay_i2c_14(void) { }
2182static inline void of_unittest_overlay_i2c_15(void) { }
2183
2184#endif
2185
141static void __init of_unittest_find_node_by_name(void)
142{
143 struct device_node *np;
144 const char *options, *name;
145
146 np = of_find_node_by_path("/testcase-data");
147 name = kasprintf(GFP_KERNEL, "%pOF", np);
148 unittest(np && !strcmp("/testcase-data", name),

--- 2121 unchanged lines hidden (view full) ---

2270
2271#else
2272
2273static inline void of_unittest_overlay_i2c_14(void) { }
2274static inline void of_unittest_overlay_i2c_15(void) { }
2275
2276#endif
2277
2278static void __init of_unittest_overlay_gpio(void)
2279{
2280 int chip_request_count;
2281 int probe_pass_count;
2282 int ret;
2283
2284 /*
2285 * tests: apply overlays before registering driver
2286 * Similar to installing a driver as a module, the
2287 * driver is registered after applying the overlays.
2288 *
2289 * - apply overlay_gpio_01
2290 * - apply overlay_gpio_02a
2291 * - apply overlay_gpio_02b
2292 * - register driver
2293 *
2294 * register driver will result in
2295 * - probe and processing gpio hog for overlay_gpio_01
2296 * - probe for overlay_gpio_02a
2297 * - processing gpio for overlay_gpio_02b
2298 */
2299
2300 probe_pass_count = unittest_gpio_probe_pass_count;
2301 chip_request_count = unittest_gpio_chip_request_count;
2302
2303 /*
2304 * overlay_gpio_01 contains gpio node and child gpio hog node
2305 * overlay_gpio_02a contains gpio node
2306 * overlay_gpio_02b contains child gpio hog node
2307 */
2308
2309 unittest(overlay_data_apply("overlay_gpio_01", NULL),
2310 "Adding overlay 'overlay_gpio_01' failed\n");
2311
2312 unittest(overlay_data_apply("overlay_gpio_02a", NULL),
2313 "Adding overlay 'overlay_gpio_02a' failed\n");
2314
2315 unittest(overlay_data_apply("overlay_gpio_02b", NULL),
2316 "Adding overlay 'overlay_gpio_02b' failed\n");
2317
2318 /*
2319 * messages are the result of the probes, after the
2320 * driver is registered
2321 */
2322
2323 EXPECT_BEGIN(KERN_INFO,
2324 "GPIO line <<int>> (line-B-input) hogged as input\n");
2325
2326 EXPECT_BEGIN(KERN_INFO,
2327 "GPIO line <<int>> (line-A-input) hogged as input\n");
2328
2329 ret = platform_driver_register(&unittest_gpio_driver);
2330 if (unittest(ret == 0, "could not register unittest gpio driver\n"))
2331 return;
2332
2333 EXPECT_END(KERN_INFO,
2334 "GPIO line <<int>> (line-A-input) hogged as input\n");
2335 EXPECT_END(KERN_INFO,
2336 "GPIO line <<int>> (line-B-input) hogged as input\n");
2337
2338 unittest(probe_pass_count + 2 == unittest_gpio_probe_pass_count,
2339 "unittest_gpio_probe() failed or not called\n");
2340
2341 unittest(chip_request_count + 2 == unittest_gpio_chip_request_count,
2342 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
2343 unittest_gpio_chip_request_count - chip_request_count);
2344
2345 /*
2346 * tests: apply overlays after registering driver
2347 *
2348 * Similar to a driver built-in to the kernel, the
2349 * driver is registered before applying the overlays.
2350 *
2351 * overlay_gpio_03 contains gpio node and child gpio hog node
2352 *
2353 * - apply overlay_gpio_03
2354 *
2355 * apply overlay will result in
2356 * - probe and processing gpio hog.
2357 */
2358
2359 probe_pass_count = unittest_gpio_probe_pass_count;
2360 chip_request_count = unittest_gpio_chip_request_count;
2361
2362 EXPECT_BEGIN(KERN_INFO,
2363 "GPIO line <<int>> (line-D-input) hogged as input\n");
2364
2365 /* overlay_gpio_03 contains gpio node and child gpio hog node */
2366
2367 unittest(overlay_data_apply("overlay_gpio_03", NULL),
2368 "Adding overlay 'overlay_gpio_03' failed\n");
2369
2370 EXPECT_END(KERN_INFO,
2371 "GPIO line <<int>> (line-D-input) hogged as input\n");
2372
2373 unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
2374 "unittest_gpio_probe() failed or not called\n");
2375
2376 unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
2377 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
2378 unittest_gpio_chip_request_count - chip_request_count);
2379
2380 /*
2381 * overlay_gpio_04a contains gpio node
2382 *
2383 * - apply overlay_gpio_04a
2384 *
2385 * apply the overlay will result in
2386 * - probe for overlay_gpio_04a
2387 */
2388
2389 probe_pass_count = unittest_gpio_probe_pass_count;
2390 chip_request_count = unittest_gpio_chip_request_count;
2391
2392 /* overlay_gpio_04a contains gpio node */
2393
2394 unittest(overlay_data_apply("overlay_gpio_04a", NULL),
2395 "Adding overlay 'overlay_gpio_04a' failed\n");
2396
2397 unittest(probe_pass_count + 1 == unittest_gpio_probe_pass_count,
2398 "unittest_gpio_probe() failed or not called\n");
2399
2400 /*
2401 * overlay_gpio_04b contains child gpio hog node
2402 *
2403 * - apply overlay_gpio_04b
2404 *
2405 * apply the overlay will result in
2406 * - processing gpio for overlay_gpio_04b
2407 */
2408
2409 EXPECT_BEGIN(KERN_INFO,
2410 "GPIO line <<int>> (line-C-input) hogged as input\n");
2411
2412 /* overlay_gpio_04b contains child gpio hog node */
2413
2414 unittest(overlay_data_apply("overlay_gpio_04b", NULL),
2415 "Adding overlay 'overlay_gpio_04b' failed\n");
2416
2417 EXPECT_END(KERN_INFO,
2418 "GPIO line <<int>> (line-C-input) hogged as input\n");
2419
2420 unittest(chip_request_count + 1 == unittest_gpio_chip_request_count,
2421 "unittest_gpio_chip_request() called %d times (expected 1 time)\n",
2422 unittest_gpio_chip_request_count - chip_request_count);
2423}
2424
2186static void __init of_unittest_overlay(void)
2187{
2188 struct device_node *bus_np = NULL;
2189
2190 if (platform_driver_register(&unittest_driver)) {
2191 unittest(0, "could not register unittest driver\n");
2192 goto out;
2193 }

--- 43 unchanged lines hidden (view full) ---

2237 of_unittest_overlay_i2c_12();
2238 of_unittest_overlay_i2c_13();
2239 of_unittest_overlay_i2c_14();
2240 of_unittest_overlay_i2c_15();
2241
2242 of_unittest_overlay_i2c_cleanup();
2243#endif
2244
2425static void __init of_unittest_overlay(void)
2426{
2427 struct device_node *bus_np = NULL;
2428
2429 if (platform_driver_register(&unittest_driver)) {
2430 unittest(0, "could not register unittest driver\n");
2431 goto out;
2432 }

--- 43 unchanged lines hidden (view full) ---

2476 of_unittest_overlay_i2c_12();
2477 of_unittest_overlay_i2c_13();
2478 of_unittest_overlay_i2c_14();
2479 of_unittest_overlay_i2c_15();
2480
2481 of_unittest_overlay_i2c_cleanup();
2482#endif
2483
2484 of_unittest_overlay_gpio();
2485
2245 of_unittest_destroy_tracked_overlays();
2246
2247out:
2248 of_node_put(bus_np);
2249}
2250
2251#else
2252static inline void __init of_unittest_overlay(void) { }

--- 37 unchanged lines hidden (view full) ---

2290OVERLAY_INFO_EXTERN(overlay_7);
2291OVERLAY_INFO_EXTERN(overlay_8);
2292OVERLAY_INFO_EXTERN(overlay_9);
2293OVERLAY_INFO_EXTERN(overlay_10);
2294OVERLAY_INFO_EXTERN(overlay_11);
2295OVERLAY_INFO_EXTERN(overlay_12);
2296OVERLAY_INFO_EXTERN(overlay_13);
2297OVERLAY_INFO_EXTERN(overlay_15);
2486 of_unittest_destroy_tracked_overlays();
2487
2488out:
2489 of_node_put(bus_np);
2490}
2491
2492#else
2493static inline void __init of_unittest_overlay(void) { }

--- 37 unchanged lines hidden (view full) ---

2531OVERLAY_INFO_EXTERN(overlay_7);
2532OVERLAY_INFO_EXTERN(overlay_8);
2533OVERLAY_INFO_EXTERN(overlay_9);
2534OVERLAY_INFO_EXTERN(overlay_10);
2535OVERLAY_INFO_EXTERN(overlay_11);
2536OVERLAY_INFO_EXTERN(overlay_12);
2537OVERLAY_INFO_EXTERN(overlay_13);
2538OVERLAY_INFO_EXTERN(overlay_15);
2539OVERLAY_INFO_EXTERN(overlay_gpio_01);
2540OVERLAY_INFO_EXTERN(overlay_gpio_02a);
2541OVERLAY_INFO_EXTERN(overlay_gpio_02b);
2542OVERLAY_INFO_EXTERN(overlay_gpio_03);
2543OVERLAY_INFO_EXTERN(overlay_gpio_04a);
2544OVERLAY_INFO_EXTERN(overlay_gpio_04b);
2298OVERLAY_INFO_EXTERN(overlay_bad_add_dup_node);
2299OVERLAY_INFO_EXTERN(overlay_bad_add_dup_prop);
2300OVERLAY_INFO_EXTERN(overlay_bad_phandle);
2301OVERLAY_INFO_EXTERN(overlay_bad_symbol);
2302
2303/* entries found by name */
2304static struct overlay_info overlays[] = {
2305 OVERLAY_INFO(overlay_base, -9999),

--- 8 unchanged lines hidden (view full) ---

2314 OVERLAY_INFO(overlay_7, 0),
2315 OVERLAY_INFO(overlay_8, 0),
2316 OVERLAY_INFO(overlay_9, 0),
2317 OVERLAY_INFO(overlay_10, 0),
2318 OVERLAY_INFO(overlay_11, 0),
2319 OVERLAY_INFO(overlay_12, 0),
2320 OVERLAY_INFO(overlay_13, 0),
2321 OVERLAY_INFO(overlay_15, 0),
2545OVERLAY_INFO_EXTERN(overlay_bad_add_dup_node);
2546OVERLAY_INFO_EXTERN(overlay_bad_add_dup_prop);
2547OVERLAY_INFO_EXTERN(overlay_bad_phandle);
2548OVERLAY_INFO_EXTERN(overlay_bad_symbol);
2549
2550/* entries found by name */
2551static struct overlay_info overlays[] = {
2552 OVERLAY_INFO(overlay_base, -9999),

--- 8 unchanged lines hidden (view full) ---

2561 OVERLAY_INFO(overlay_7, 0),
2562 OVERLAY_INFO(overlay_8, 0),
2563 OVERLAY_INFO(overlay_9, 0),
2564 OVERLAY_INFO(overlay_10, 0),
2565 OVERLAY_INFO(overlay_11, 0),
2566 OVERLAY_INFO(overlay_12, 0),
2567 OVERLAY_INFO(overlay_13, 0),
2568 OVERLAY_INFO(overlay_15, 0),
2569 OVERLAY_INFO(overlay_gpio_01, 0),
2570 OVERLAY_INFO(overlay_gpio_02a, 0),
2571 OVERLAY_INFO(overlay_gpio_02b, 0),
2572 OVERLAY_INFO(overlay_gpio_03, 0),
2573 OVERLAY_INFO(overlay_gpio_04a, 0),
2574 OVERLAY_INFO(overlay_gpio_04b, 0),
2322 OVERLAY_INFO(overlay_bad_add_dup_node, -EINVAL),
2323 OVERLAY_INFO(overlay_bad_add_dup_prop, -EINVAL),
2324 OVERLAY_INFO(overlay_bad_phandle, -EINVAL),
2325 OVERLAY_INFO(overlay_bad_symbol, -EINVAL),
2326 /* end marker */
2327 {.dtb_begin = NULL, .dtb_end = NULL, .expected_result = 0, .name = NULL}
2328};
2329

--- 337 unchanged lines hidden ---
2575 OVERLAY_INFO(overlay_bad_add_dup_node, -EINVAL),
2576 OVERLAY_INFO(overlay_bad_add_dup_prop, -EINVAL),
2577 OVERLAY_INFO(overlay_bad_phandle, -EINVAL),
2578 OVERLAY_INFO(overlay_bad_symbol, -EINVAL),
2579 /* end marker */
2580 {.dtb_begin = NULL, .dtb_end = NULL, .expected_result = 0, .name = NULL}
2581};
2582

--- 337 unchanged lines hidden ---