2021-07-22 11:25:32 +08:00
|
|
|
/*
|
2021-08-08 01:24:59 +08:00
|
|
|
* completions.c
|
2021-07-22 11:25:32 +08:00
|
|
|
*/
|
2021-07-22 06:58:13 +08:00
|
|
|
#include <linux/completion.h>
|
2023-02-22 23:43:27 +08:00
|
|
|
#include <linux/err.h> /* for IS_ERR() */
|
2021-07-22 06:35:24 +08:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/kthread.h>
|
2021-07-22 06:58:13 +08:00
|
|
|
#include <linux/module.h>
|
2023-02-23 18:41:23 +08:00
|
|
|
#include <linux/printk.h>
|
2023-03-14 07:59:42 +08:00
|
|
|
#include <linux/version.h>
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2023-09-27 04:37:55 +08:00
|
|
|
static struct completion crank_comp;
|
|
|
|
static struct completion flywheel_comp;
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2021-07-22 06:58:13 +08:00
|
|
|
static int machine_crank_thread(void *arg)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
|
|
|
pr_info("Turn the crank\n");
|
|
|
|
|
2023-09-27 04:37:55 +08:00
|
|
|
complete_all(&crank_comp);
|
2023-03-14 07:59:42 +08:00
|
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 17, 0)
|
2023-09-27 04:37:55 +08:00
|
|
|
kthread_complete_and_exit(&crank_comp, 0);
|
2023-03-14 07:59:42 +08:00
|
|
|
#else
|
2023-09-27 04:37:55 +08:00
|
|
|
complete_and_exit(&crank_comp, 0);
|
2023-03-14 07:59:42 +08:00
|
|
|
#endif
|
2021-07-22 06:35:24 +08:00
|
|
|
}
|
|
|
|
|
2021-07-22 06:58:13 +08:00
|
|
|
static int machine_flywheel_spinup_thread(void *arg)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
2023-09-27 04:37:55 +08:00
|
|
|
wait_for_completion(&crank_comp);
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
pr_info("Flywheel spins up\n");
|
|
|
|
|
2023-09-27 04:37:55 +08:00
|
|
|
complete_all(&flywheel_comp);
|
2023-03-14 07:59:42 +08:00
|
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 17, 0)
|
2023-09-27 04:37:55 +08:00
|
|
|
kthread_complete_and_exit(&flywheel_comp, 0);
|
2023-03-14 07:59:42 +08:00
|
|
|
#else
|
2023-09-27 04:37:55 +08:00
|
|
|
complete_and_exit(&flywheel_comp, 0);
|
2023-03-14 07:59:42 +08:00
|
|
|
#endif
|
2021-07-22 06:35:24 +08:00
|
|
|
}
|
|
|
|
|
2023-07-05 09:44:21 +08:00
|
|
|
static int __init completions_init(void)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
2021-07-22 06:58:13 +08:00
|
|
|
struct task_struct *crank_thread;
|
|
|
|
struct task_struct *flywheel_thread;
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
pr_info("completions example\n");
|
|
|
|
|
2023-09-27 04:37:55 +08:00
|
|
|
init_completion(&crank_comp);
|
|
|
|
init_completion(&flywheel_comp);
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2021-07-22 06:58:13 +08:00
|
|
|
crank_thread = kthread_create(machine_crank_thread, NULL, "KThread Crank");
|
2021-07-22 06:35:24 +08:00
|
|
|
if (IS_ERR(crank_thread))
|
|
|
|
goto ERROR_THREAD_1;
|
|
|
|
|
2021-07-22 06:58:13 +08:00
|
|
|
flywheel_thread = kthread_create(machine_flywheel_spinup_thread, NULL,
|
|
|
|
"KThread Flywheel");
|
2021-07-22 06:35:24 +08:00
|
|
|
if (IS_ERR(flywheel_thread))
|
|
|
|
goto ERROR_THREAD_2;
|
|
|
|
|
|
|
|
wake_up_process(flywheel_thread);
|
|
|
|
wake_up_process(crank_thread);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ERROR_THREAD_2:
|
|
|
|
kthread_stop(crank_thread);
|
|
|
|
ERROR_THREAD_1:
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-07-05 09:44:21 +08:00
|
|
|
static void __exit completions_exit(void)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
2023-09-27 04:37:55 +08:00
|
|
|
wait_for_completion(&crank_comp);
|
|
|
|
wait_for_completion(&flywheel_comp);
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
pr_info("completions exit\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(completions_init);
|
|
|
|
module_exit(completions_exit);
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("Completions example");
|
|
|
|
MODULE_LICENSE("GPL");
|