static int __init initialization_function(void)
{
/*初始化代碼*/
}
module_init(initialization_function);
初始化成功返回0,不然返回錯誤碼。linux
通常以__exit標識申明,如數組
static void __exit cleanup_function(void)
{
/*釋放代碼*/
}
module_exit(cleanup_function);
被直接編譯進內核的模塊的卸載函數會被省略,不編譯進內核,由於模塊被內置了,也就不會被卸載。markdown
static char *book_name = "dissecting Linux Deice Driver";
module_param(book_name, char, S_IRUGO);
static int book_num = 4000;
module_param(book_num, int, S_IRUGO);
「insmod (或modprobe) 模塊名 參數名 = 參數值」,不傳遞則用缺省值,模塊被內置時用bootloader經過bootargs裏設置「模塊名.參數 = 值」給內置模塊傳遞參數。函數
/*====================================================================== A kernel module: book This example is to introduce module params The initial developer of the original code is Baohua Song <author@linuxdriver.cn>. All Rights Reserved. ======================================================================*/
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static char *book_name = "dissecting Linux Device Driver";
static int num = 4000;
static int book_init(void)
{
printk(KERN_INFO " book name:%s\n",book_name);
printk(KERN_INFO " book num:%d\n",num);
return 0;
}
static void book_exit(void)
{
printk(KERN_INFO " Book module exit\n ");
}
module_init(book_init);
module_exit(book_exit);
module_param(num, int, S_IRUGO);
module_param(book_name, charp, S_IRUGO);
MODULE_AUTHOR("Song Baohua, author@linuxdriver.cn");
MODULE_DESCRIPTION("A simple Module for testing module params");
MODULE_VERSION("V1.0");
經過insmod加參數和不加參數實驗,在/var/log/messages文件中查看內核的輸出
在/sys/module/book/parameters目錄下輸入tree查看參數文件節點spa
/*====================================================================== A simple kernel module to introduce export symbol The initial developer of the original code is Baohua Song <author@linuxdriver.cn>. All Rights Reserved. ======================================================================*/
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
int add_integar(int a,int b)
{
return a+b;
}
int sub_integar(int a,int b)
{
return a-b;
}
EXPORT_SYMBOL(add_integar);
EXPORT_SYMBOL(sub_integar);
obj-m := modulename.o
modulename-objs := file1.o file2.o ...