Linux 4.16.11
[linux/fpc-iii.git] / drivers / base / test / test_async_driver_probe.c
blobe7f145d662f06a9f313c92e322a73fa5a5eebfa2
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2014 Google, Inc.
4 */
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/delay.h>
9 #include <linux/init.h>
10 #include <linux/hrtimer.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/time.h>
15 #define TEST_PROBE_DELAY (5 * 1000) /* 5 sec */
16 #define TEST_PROBE_THRESHOLD (TEST_PROBE_DELAY / 2)
18 static int test_probe(struct platform_device *pdev)
20 dev_info(&pdev->dev, "sleeping for %d msecs in probe\n",
21 TEST_PROBE_DELAY);
22 msleep(TEST_PROBE_DELAY);
23 dev_info(&pdev->dev, "done sleeping\n");
25 return 0;
28 static struct platform_driver async_driver = {
29 .driver = {
30 .name = "test_async_driver",
31 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
33 .probe = test_probe,
36 static struct platform_driver sync_driver = {
37 .driver = {
38 .name = "test_sync_driver",
39 .probe_type = PROBE_FORCE_SYNCHRONOUS,
41 .probe = test_probe,
44 static struct platform_device *async_dev_1, *async_dev_2;
45 static struct platform_device *sync_dev_1;
47 static int __init test_async_probe_init(void)
49 ktime_t calltime, delta;
50 unsigned long long duration;
51 int error;
53 pr_info("registering first asynchronous device...\n");
55 async_dev_1 = platform_device_register_simple("test_async_driver", 1,
56 NULL, 0);
57 if (IS_ERR(async_dev_1)) {
58 error = PTR_ERR(async_dev_1);
59 pr_err("failed to create async_dev_1: %d\n", error);
60 return error;
63 pr_info("registering asynchronous driver...\n");
64 calltime = ktime_get();
65 error = platform_driver_register(&async_driver);
66 if (error) {
67 pr_err("Failed to register async_driver: %d\n", error);
68 goto err_unregister_async_dev_1;
71 delta = ktime_sub(ktime_get(), calltime);
72 duration = (unsigned long long) ktime_to_ms(delta);
73 pr_info("registration took %lld msecs\n", duration);
74 if (duration > TEST_PROBE_THRESHOLD) {
75 pr_err("test failed: probe took too long\n");
76 error = -ETIMEDOUT;
77 goto err_unregister_async_driver;
80 pr_info("registering second asynchronous device...\n");
81 calltime = ktime_get();
82 async_dev_2 = platform_device_register_simple("test_async_driver", 2,
83 NULL, 0);
84 if (IS_ERR(async_dev_2)) {
85 error = PTR_ERR(async_dev_2);
86 pr_err("failed to create async_dev_2: %d\n", error);
87 goto err_unregister_async_driver;
90 delta = ktime_sub(ktime_get(), calltime);
91 duration = (unsigned long long) ktime_to_ms(delta);
92 pr_info("registration took %lld msecs\n", duration);
93 if (duration > TEST_PROBE_THRESHOLD) {
94 pr_err("test failed: probe took too long\n");
95 error = -ETIMEDOUT;
96 goto err_unregister_async_dev_2;
99 pr_info("registering synchronous driver...\n");
101 error = platform_driver_register(&sync_driver);
102 if (error) {
103 pr_err("Failed to register async_driver: %d\n", error);
104 goto err_unregister_async_dev_2;
107 pr_info("registering synchronous device...\n");
108 calltime = ktime_get();
109 sync_dev_1 = platform_device_register_simple("test_sync_driver", 1,
110 NULL, 0);
111 if (IS_ERR(sync_dev_1)) {
112 error = PTR_ERR(sync_dev_1);
113 pr_err("failed to create sync_dev_1: %d\n", error);
114 goto err_unregister_sync_driver;
117 delta = ktime_sub(ktime_get(), calltime);
118 duration = (unsigned long long) ktime_to_ms(delta);
119 pr_info("registration took %lld msecs\n", duration);
120 if (duration < TEST_PROBE_THRESHOLD) {
121 pr_err("test failed: probe was too quick\n");
122 error = -ETIMEDOUT;
123 goto err_unregister_sync_dev_1;
126 pr_info("completed successfully");
128 return 0;
130 err_unregister_sync_dev_1:
131 platform_device_unregister(sync_dev_1);
133 err_unregister_sync_driver:
134 platform_driver_unregister(&sync_driver);
136 err_unregister_async_dev_2:
137 platform_device_unregister(async_dev_2);
139 err_unregister_async_driver:
140 platform_driver_unregister(&async_driver);
142 err_unregister_async_dev_1:
143 platform_device_unregister(async_dev_1);
145 return error;
147 module_init(test_async_probe_init);
149 static void __exit test_async_probe_exit(void)
151 platform_driver_unregister(&async_driver);
152 platform_driver_unregister(&sync_driver);
153 platform_device_unregister(async_dev_1);
154 platform_device_unregister(async_dev_2);
155 platform_device_unregister(sync_dev_1);
157 module_exit(test_async_probe_exit);
159 MODULE_DESCRIPTION("Test module for asynchronous driver probing");
160 MODULE_AUTHOR("Dmitry Torokhov <dtor@chromium.org>");
161 MODULE_LICENSE("GPL");