From d43259c553cbadb9d82ae7da566c98d39043d410 Mon Sep 17 00:00:00 2001 From: Jim Huang Date: Sun, 8 Aug 2021 00:29:24 +0800 Subject: [PATCH] Drop the deprecated init_module() and cleanup_module() --- examples/bottomhalf.c | 13 +++++++++---- examples/chardev.c | 9 +++++---- examples/chardev2.c | 32 ++++++++++++-------------------- examples/devicemodel.c | 6 +++--- examples/hello-1.c | 4 +--- examples/intrpt.c | 7 +++++-- examples/procfs1.c | 7 +++++-- examples/procfs2.c | 15 +++++---------- examples/procfs3.c | 8 ++++++-- examples/procfs4.c | 13 +++++++------ examples/sched.c | 7 +++++-- examples/sleep.c | 19 +++++++------------ 12 files changed, 70 insertions(+), 70 deletions(-) diff --git a/examples/bottomhalf.c b/examples/bottomhalf.c index 64d9873..4fa4e34 100644 --- a/examples/bottomhalf.c +++ b/examples/bottomhalf.c @@ -22,8 +22,10 @@ static struct gpio leds[] = {{4, GPIOF_OUT_INIT_LOW, "LED 1"}}; /* Define GPIOs for BUTTONS Change the numbers for the GPIO on your board. */ -static struct gpio buttons[] = {{17, GPIOF_IN, "LED 1 ON BUTTON"}, - {18, GPIOF_IN, "LED 1 OFF BUTTON"}}; +static struct gpio buttons[] = { + {17, GPIOF_IN, "LED 1 ON BUTTON"}, + {18, GPIOF_IN, "LED 1 OFF BUTTON"}, +}; /* Tasklet containing some non-trivial amount of processing */ static void bottomhalf_tasklet_fn(unsigned long data) @@ -53,7 +55,7 @@ static irqreturn_t button_isr(int irq, void *data) return IRQ_HANDLED; } -int init_module() +static int __init bottomhalf_init(void) { int ret = 0; @@ -133,7 +135,7 @@ fail1: return ret; } -void cleanup_module() +static void __exit bottomhalf_exit(void) { int i; @@ -152,5 +154,8 @@ void cleanup_module() gpio_free_array(buttons, ARRAY_SIZE(buttons)); } +module_init(bottomhalf_init); +module_exit(bottomhalf_exit); + MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Interrupt with top and bottom half"); diff --git a/examples/chardev.c b/examples/chardev.c index b844703..096a4bb 100644 --- a/examples/chardev.c +++ b/examples/chardev.c @@ -40,8 +40,7 @@ static struct file_operations chardev_fops = { .release = device_release, }; -/* This function is called when the module is loaded. */ -int init_module(void) +static int __init chardev_init(void) { major = register_chrdev(0, DEVICE_NAME, &chardev_fops); @@ -60,8 +59,7 @@ int init_module(void) return SUCCESS; } -/* This function is called when the module is unloaded. */ -void cleanup_module(void) +static void __exit chardev_exit(void) { device_destroy(cls, MKDEV(major, 0)); class_destroy(cls); @@ -145,4 +143,7 @@ static ssize_t device_write(struct file *filp, return -EINVAL; } +module_init(chardev_init); +module_exit(chardev_exit); + MODULE_LICENSE("GPL"); diff --git a/examples/chardev2.c b/examples/chardev2.c index 8e26003..013e55f 100644 --- a/examples/chardev2.c +++ b/examples/chardev2.c @@ -8,7 +8,7 @@ #include #include #include -#include /* We're doing kernel work */ +#include /* We are doing kernel work */ #include /* Specifically, a module */ #include @@ -229,20 +229,13 @@ struct file_operations Fops = { .release = device_release, /* a.k.a. close */ }; -/* - * Initialize the module - Register the character device - */ -int init_module() +/* Initialize the module - Register the character device */ +static int __init chardev2_init(void) { - int ret_val; - /* - * Register the character device (atleast try) - */ - ret_val = register_chrdev(MAJOR_NUM, DEVICE_NAME, &Fops); + /* Register the character device (atleast try) */ + int ret_val = register_chrdev(MAJOR_NUM, DEVICE_NAME, &Fops); - /* - * Negative values signify an error - */ + /* Negative values signify an error */ if (ret_val < 0) { pr_alert("%s failed with %d\n", "Sorry, registering the character device ", ret_val); @@ -259,18 +252,17 @@ int init_module() return 0; } -/* - * Cleanup - unregister the appropriate file from /proc - */ -void cleanup_module() +/* Cleanup - unregister the appropriate file from /proc */ +static void __exit chardev2_exit(void) { device_destroy(cls, MKDEV(Major, 0)); class_destroy(cls); - /* - * Unregister the device - */ + /* Unregister the device */ unregister_chrdev(Major, DEVICE_NAME); } +module_init(chardev2_init); +module_exit(chardev2_exit); + MODULE_LICENSE("GPL"); diff --git a/examples/devicemodel.c b/examples/devicemodel.c index 6cef830..07d30ec 100644 --- a/examples/devicemodel.c +++ b/examples/devicemodel.c @@ -92,8 +92,8 @@ static void devicemodel_exit(void) platform_driver_unregister(&devicemodel_driver); } -MODULE_LICENSE("GPL"); -MODULE_DESCRIPTION("Linux Device Model example"); - module_init(devicemodel_init); module_exit(devicemodel_exit); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Linux Device Model example"); diff --git a/examples/hello-1.c b/examples/hello-1.c index a96438b..e4fb93a 100644 --- a/examples/hello-1.c +++ b/examples/hello-1.c @@ -8,9 +8,7 @@ int init_module(void) { pr_info("Hello world 1.\n"); - /* - * A non 0 return means init_module failed; module can't be loaded. - */ + /* A non 0 return means init_module failed; module can't be loaded. */ return 0; } diff --git a/examples/intrpt.c b/examples/intrpt.c index 019a465..a506837 100644 --- a/examples/intrpt.c +++ b/examples/intrpt.c @@ -39,7 +39,7 @@ static irqreturn_t button_isr(int irq, void *data) return IRQ_HANDLED; } -int init_module() +static int __init intrpt_init(void) { int ret = 0; @@ -119,7 +119,7 @@ fail1: return ret; } -void cleanup_module() +static void __exit intrpt_exit(void) { int i; @@ -138,5 +138,8 @@ void cleanup_module() gpio_free_array(buttons, ARRAY_SIZE(buttons)); } +module_init(intrpt_init); +module_exit(intrpt_exit); + MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Handle some GPIO interrupts"); diff --git a/examples/procfs1.c b/examples/procfs1.c index 001d50b..2f627cf 100644 --- a/examples/procfs1.c +++ b/examples/procfs1.c @@ -47,7 +47,7 @@ static const struct file_operations proc_file_fops = { }; #endif -int init_module() +static int __init procfs1_init(void) { Our_Proc_File = proc_create(procfs_name, 0644, NULL, &proc_file_fops); if (NULL == Our_Proc_File) { @@ -60,10 +60,13 @@ int init_module() return 0; } -void cleanup_module() +static void __exit procfs1_exit(void) { proc_remove(Our_Proc_File); pr_info("/proc/%s removed\n", procfs_name); } +module_init(procfs1_init); +module_exit(procfs1_exit); + MODULE_LICENSE("GPL"); diff --git a/examples/procfs2.c b/examples/procfs2.c index 43ff27f..dcb08a1 100644 --- a/examples/procfs2.c +++ b/examples/procfs2.c @@ -90,11 +90,7 @@ static const struct file_operations proc_file_fops = { }; #endif -/** - *This function is called when the module is loaded - * - */ -int init_module() +static int __init procfs2_init(void) { Our_Proc_File = proc_create(PROCFS_NAME, 0644, NULL, &proc_file_fops); if (NULL == Our_Proc_File) { @@ -107,14 +103,13 @@ int init_module() return 0; } -/** - *This function is called when the module is unloaded - * - */ -void cleanup_module() +static void __exit procfs2_exit(void) { proc_remove(Our_Proc_File); pr_info("/proc/%s removed\n", PROCFS_NAME); } +module_init(procfs2_init); +module_exit(procfs2_exit); + MODULE_LICENSE("GPL"); diff --git a/examples/procfs3.c b/examples/procfs3.c index 0906bec..4a6e3bb 100644 --- a/examples/procfs3.c +++ b/examples/procfs3.c @@ -78,7 +78,7 @@ static const struct file_operations File_Ops_4_Our_Proc_File = { }; #endif -int init_module() +static int __init procfs3_init(void) { Our_Proc_File = proc_create(PROCFS_ENTRY_FILENAME, 0644, NULL, &File_Ops_4_Our_Proc_File); @@ -94,10 +94,14 @@ int init_module() pr_debug("/proc/%s created\n", PROCFS_ENTRY_FILENAME); return 0; } -void cleanup_module() + +static void __exit procfs3_exit(void) { remove_proc_entry(PROCFS_ENTRY_FILENAME, NULL); pr_debug("/proc/%s removed\n", PROCFS_ENTRY_FILENAME); } +module_init(procfs3_init); +module_exit(procfs3_exit); + MODULE_LICENSE("GPL"); diff --git a/examples/procfs4.c b/examples/procfs4.c index f9b27db..6761697 100644 --- a/examples/procfs4.c +++ b/examples/procfs4.c @@ -15,8 +15,6 @@ #define PROC_NAME "iter" -MODULE_LICENSE("GPL"); - /* This function is called at the beginning of a sequence. * ie, when: * - the /proc file is read (first time) @@ -93,8 +91,7 @@ static const struct file_operations my_file_ops = { }; #endif -/* This function is called when the module is loaded. */ -int init_module(void) +static int __init procfs4_init(void) { struct proc_dir_entry *entry; @@ -108,9 +105,13 @@ int init_module(void) return 0; } -/* This function is called when the module is unloaded. */ -void cleanup_module(void) +static void __exit procfs4_exit(void) { remove_proc_entry(PROC_NAME, NULL); pr_debug("/proc/%s removed\n", PROC_NAME); } + +module_init(procfs4_init); +module_exit(procfs4_exit); + +MODULE_LICENSE("GPL"); diff --git a/examples/sched.c b/examples/sched.c index a6a7bcd..5d208d6 100644 --- a/examples/sched.c +++ b/examples/sched.c @@ -13,7 +13,7 @@ static void work_handler(struct work_struct *data) pr_info("work handler function.\n"); } -int init_module() +static int __init sched_init(void) { queue = alloc_workqueue("HELLOWORLD", WQ_UNBOUND, 1); INIT_WORK(&work, work_handler); @@ -22,10 +22,13 @@ int init_module() return 0; } -void cleanup_module() +static void __exit sched_exit(void) { destroy_workqueue(queue); } +module_init(sched_init); +module_exit(sched_exit); + MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Workqueue example"); diff --git a/examples/sleep.c b/examples/sleep.c index b14ab99..365c601 100644 --- a/examples/sleep.c +++ b/examples/sleep.c @@ -240,15 +240,8 @@ static const struct file_operations File_Ops_4_Our_Proc_File = { }; #endif -/* - * Module initialization and cleanup - */ - -/* - * Initialize the module - register the proc file - */ - -int init_module() +/* Initialize the module - register the proc file */ +static int __init sleep_init(void) { Our_Proc_File = proc_create(PROC_ENTRY_FILENAME, 0644, NULL, &File_Ops_4_Our_Proc_File); @@ -265,16 +258,18 @@ int init_module() return 0; } -/* - * Cleanup - unregister our file from /proc. This could get dangerous if +/* Cleanup - unregister our file from /proc. This could get dangerous if * there are still processes waiting in WaitQ, because they are inside our * open function, which will get unloaded. I'll explain how to avoid removal * of a kernel module in such a case in chapter 10. */ -void cleanup_module() +static void __exit sleep_exit(void) { remove_proc_entry(PROC_ENTRY_FILENAME, NULL); pr_debug("/proc/%s removed\n", PROC_ENTRY_FILENAME); } +module_init(sleep_init); +module_exit(sleep_exit); + MODULE_LICENSE("GPL");