Переглянути джерело

Drop the deprecated init_module() and cleanup_module()

Jim Huang 3 роки тому
батько
коміт
d43259c553

+ 9 - 4
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");

+ 5 - 4
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");

+ 12 - 20
examples/chardev2.c

@@ -8,7 +8,7 @@
 #include <linux/fs.h>
 #include <linux/init.h>
 #include <linux/irq.h>
-#include <linux/kernel.h> /* We're doing kernel work */
+#include <linux/kernel.h> /* We are doing kernel work */
 #include <linux/module.h> /* Specifically, a module */
 #include <linux/poll.h>
 
@@ -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");

+ 3 - 3
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");

+ 1 - 3
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;
 }
 

+ 5 - 2
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");

+ 5 - 2
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");

+ 5 - 10
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");

+ 6 - 2
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");

+ 7 - 6
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");

+ 5 - 2
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");

+ 7 - 12
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");