导出模块文件export_symb.c
/*======================================================================
A simple kernel module to introduce export symbol
The initial developer of the original code is Baohua Song
. All Rights Reserved.
======================================================================*/
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
int add_integar(int a,int b)
{
printk(KERN_EMERG " a + b\n");
return a+b;
}
int sub_integar(int a,int b)
{
printk(KERN_EMERG " a - b\n");
return a-b;
}
EXPORT_SYMBOL(add_integar);
EXPORT_SYMBOL(sub_integar);
它的Makefile如下:
ifneq ($(KERNELRELEASE),)
obj-m := export_symb.o
else
KERNELDIR := /home/mxzh/opt2/utuLinuxfor2440V1.5.3/utu-linux_for_s3c2440_V1.5.3
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
clean:
rm *.o *.ko
模块参数文件book.c
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
static char *book_name = "dissecting Linux Device Driver";
static int num = 4000;
static int book_init(void)
{
int a = 0;
//printk(KERN_INFO " book name:%s\n",book_name);
printk(KERN_EMERG " book name:%s\n",book_name);
printk(KERN_EMERG " book num:%d\n",num);
a = add_integar(10,8);
printk(KERN_EMERG "a = %d\n",a);
return 0;
}
static void book_exit(void)
{
int b= 0;
printk(KERN_EMERG " Book module exit\n ");
b = sub_integar(20,18);
printk(KERN_EMERG "b = %d\n",b);
}
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");
它的Makefile文件:
ifneq ($(KERNELRELEASE),)
obj-m := book.o
else
KERNELDIR := /home/mxzh/opt2/utuLinuxfor2440V1.5.3/utu-linux_for_s3c2440_V1.5.3
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
clean:
rm *.o *.ko
板子上测试:
/ $ insmod export_symb.ko
/ $ insmod book.ko
<0> book name:dissecting Linux Device Driver
book num:4000
a + b
a = 18
/ $ rmmod book.ko
Book module exit
<0> a - b
b = 2
传入参数给模块的测试
/ $ insmod book.ko book_name="my book" num=88
book name:my book
book num:88
a + b
a = 18
/ $ rmmod book.ko
Book module exit
<0> a - b
b = 2
这里要注意的是=后面不能有空格,否则会出错,insmod不进去
上面是导出函数,传入参数给内核模块,下面是传入参数给一个内核模块,同时导出该参数给其他模块用。见如下:
/**********export_symb.c************/
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
int add_integar(int a,int b)
{
printk(KERN_EMERG " a + b\n");
return a+b;
}
int sub_integar(int a,int b)
{
printk(KERN_EMERG " a - b\n");
return a-b;
}
EXPORT_SYMBOL(add_integar);
EXPORT_SYMBOL(sub_integar);
/*********************book.c**********/
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
static char *book_name = "dissecting Linux Device Driver";
static int num = 4000;
static int book_init(void)
{
int a = 0;
//printk(KERN_INFO " book name:%s\n",book_name);
printk(KERN_EMERG " book name:%s\n",book_name);
printk(KERN_EMERG " book num:%d\n",num);
a = add_integar(10,8);
printk(KERN_EMERG "a = %d\n",a);
return 0;
}
static void book_exit(void)
{
int b= 0;
printk(KERN_EMERG " Book module exit\n ");
b = sub_integar(20,18);
printk(KERN_EMERG "b = %d\n",b);
}
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");
EXPORT_SYMBOL(num);
EXPORT_SYMBOL(book_name);
/****************hello.c******************/
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
extern int num;
extern char *book_name;
printk("hello,num = %d\n",num);
printk("hello,book_name = %s\n",book_name);
printk(KERN_INFO " Hello World enter\n");
printk(KERN_EMERG " ggggggggggg\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_INFO " Hello World exit....\n ");
printk(KERN_EMERG " kkkkk....\n ");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_AUTHOR("Song Baohua");
MODULE_DESCRIPTION("A simple Hello World Module");
MODULE_ALIAS("a simplest module");
实验如下:
/button $ insmod export_symb.ko
/button $ insmod book.ko book_name="mxzh book" num=88888
book name:mxzh book
book num:88888
export ,bbk_a ...
a + b
a = 18
/button $ insmod hello.ko
hello,num = 88888
hello,book_name = mxzh book
Hello World enter
ggggggggggg
阅读(1606) | 评论(0) | 转发(0) |