2 * Copyright (C) 2014 Google, Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/delay.h>
17 #include <linux/init.h>
18 #include <linux/hrtimer.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/time.h>
23 #define TEST_PROBE_DELAY (5 * 1000) /* 5 sec */
24 #define TEST_PROBE_THRESHOLD (TEST_PROBE_DELAY / 2)
26 static int test_probe(struct platform_device
*pdev
)
28 dev_info(&pdev
->dev
, "sleeping for %d msecs in probe\n",
30 msleep(TEST_PROBE_DELAY
);
31 dev_info(&pdev
->dev
, "done sleeping\n");
36 static struct platform_driver async_driver
= {
38 .name
= "test_async_driver",
39 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
44 static struct platform_driver sync_driver
= {
46 .name
= "test_sync_driver",
47 .probe_type
= PROBE_FORCE_SYNCHRONOUS
,
52 static struct platform_device
*async_dev_1
, *async_dev_2
;
53 static struct platform_device
*sync_dev_1
;
55 static int __init
test_async_probe_init(void)
57 ktime_t calltime
, delta
;
58 unsigned long long duration
;
61 pr_info("registering first asynchronous device...\n");
63 async_dev_1
= platform_device_register_simple("test_async_driver", 1,
65 if (IS_ERR(async_dev_1
)) {
66 error
= PTR_ERR(async_dev_1
);
67 pr_err("failed to create async_dev_1: %d", error
);
71 pr_info("registering asynchronous driver...\n");
72 calltime
= ktime_get();
73 error
= platform_driver_register(&async_driver
);
75 pr_err("Failed to register async_driver: %d\n", error
);
76 goto err_unregister_async_dev_1
;
79 delta
= ktime_sub(ktime_get(), calltime
);
80 duration
= (unsigned long long) ktime_to_ms(delta
);
81 pr_info("registration took %lld msecs\n", duration
);
82 if (duration
> TEST_PROBE_THRESHOLD
) {
83 pr_err("test failed: probe took too long\n");
85 goto err_unregister_async_driver
;
88 pr_info("registering second asynchronous device...\n");
89 calltime
= ktime_get();
90 async_dev_2
= platform_device_register_simple("test_async_driver", 2,
92 if (IS_ERR(async_dev_2
)) {
93 error
= PTR_ERR(async_dev_2
);
94 pr_err("failed to create async_dev_2: %d", error
);
95 goto err_unregister_async_driver
;
98 delta
= ktime_sub(ktime_get(), calltime
);
99 duration
= (unsigned long long) ktime_to_ms(delta
);
100 pr_info("registration took %lld msecs\n", duration
);
101 if (duration
> TEST_PROBE_THRESHOLD
) {
102 pr_err("test failed: probe took too long\n");
104 goto err_unregister_async_dev_2
;
107 pr_info("registering synchronous driver...\n");
109 error
= platform_driver_register(&sync_driver
);
111 pr_err("Failed to register async_driver: %d\n", error
);
112 goto err_unregister_async_dev_2
;
115 pr_info("registering synchronous device...\n");
116 calltime
= ktime_get();
117 sync_dev_1
= platform_device_register_simple("test_sync_driver", 1,
119 if (IS_ERR(sync_dev_1
)) {
120 error
= PTR_ERR(sync_dev_1
);
121 pr_err("failed to create sync_dev_1: %d", error
);
122 goto err_unregister_sync_driver
;
125 delta
= ktime_sub(ktime_get(), calltime
);
126 duration
= (unsigned long long) ktime_to_ms(delta
);
127 pr_info("registration took %lld msecs\n", duration
);
128 if (duration
< TEST_PROBE_THRESHOLD
) {
129 pr_err("test failed: probe was too quick\n");
131 goto err_unregister_sync_dev_1
;
134 pr_info("completed successfully");
138 err_unregister_sync_dev_1
:
139 platform_device_unregister(sync_dev_1
);
141 err_unregister_sync_driver
:
142 platform_driver_unregister(&sync_driver
);
144 err_unregister_async_dev_2
:
145 platform_device_unregister(async_dev_2
);
147 err_unregister_async_driver
:
148 platform_driver_unregister(&async_driver
);
150 err_unregister_async_dev_1
:
151 platform_device_unregister(async_dev_1
);
155 module_init(test_async_probe_init
);
157 static void __exit
test_async_probe_exit(void)
159 platform_driver_unregister(&async_driver
);
160 platform_driver_unregister(&sync_driver
);
161 platform_device_unregister(async_dev_1
);
162 platform_device_unregister(async_dev_2
);
163 platform_device_unregister(sync_dev_1
);
165 module_exit(test_async_probe_exit
);
167 MODULE_DESCRIPTION("Test module for asynchronous driver probing");
168 MODULE_AUTHOR("Dmitry Torokhov <dtor@chromium.org>");
169 MODULE_LICENSE("GPL");