标签:des style color io 使用 strong ar for sp
Linux设备驱动辩编写基础
一、linux中的驱动是以模块的形式存在的,这样就大大提高了驱动的灵活性,linux内核模块的程序结构如下:
l 模块加载函数(必须):module_init()
l 模块卸载函数(必须):module_exit()
l 模块许可证声明(必须):MODULE_LECENSE(“GPL”)
l 模块参数(可选):module_param(a,int,0400)
l 模块到处符号(可选):EXPORT_SYMBOL_GPL(func)
l 模块作者声明等其他信息(可选):MODULE_AUTHOR(“name”);
对于模块参数需要使用下面方式进行操作:
module_patam(a,int,04000);
MODULE_PARAM_DESC(a,”description”);
#insmod module.ko a=100
导出符号的作用:例如一个模块mod1中定义一个函数func1;在另外一个模块mod2中定义一个函数func2,func2调用func1。
在模块mod1中,EXPORT_SYMBOL(func1);
在模块mod2中,extern intfunc1();
就可以在mod2中调用func1了。
一个例子:
#include<linux/module.h>
#include<linux/moduleparam.h>
#include<linux/kernel.h>
#include<linux/init.h>
#include<linux/stat.h>
#defineDRIVER_AUTHOR "Foobar"
#define DRIVER_DESC "A sample driver"
MODULE_LICENSE("GPL");
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_SUPPORTED_DEVICE("TestDevice");
static short intmyshort = 1;
static int myint= 420;
static long intmylong = 9999;
static char*mystring = "blah";
static intarray[2]= {-1, -1};
static intarr_argc = 0;
module_param(myshort, short, 0400);
MODULE_PARM_DESC(myshort, "A short integer");
module_param(myint, int, 0400);
MODULE_PARM_DESC(myint, "An integer");
module_param (mylong,long, 0000);
MODULE_PARM_DESC(mylong, "A long integer");
module_param(mystring, charp, 0000);
MODULE_PARM_DESC(mystring, "A character string");
module_param_array(array, int, &arr_argc, 0000);
//module_param_array(array, int, arr_argc, 0000); //for kernel<2.6.10
MODULE_PARM_DESC(array, "An array of integers");
static int__init hello_2_init (void)
{
int i;
printk (KERN_INFO "myshort is ashort integer: %hd\n", myshort);
printk (KERN_INFO "myint is aninteger: %d\n", myint);
printk (KERN_INFO "mylong is along integer: %ld\n", mylong);
printk (KERN_INFO "mystring is astring: %s\n\n", mystring);
for (i=0; i<arr_argc; i++)
printk (KERN_INFO"array[%d] = %d\n",i, array[i]);
printk (KERN_INFO "Got %darguments in array\n", arr_argc);
return 0;
}
static void__exit hello_2_exit (void)
{
printk (KERN_INFO "hello drivercleaned up\n");
}
module_init(hello_2_init);
module_exit(hello_2_exit);
二、驱动模块的编译
编译需注意:内核源码路径必须是配置好的切编译过的
ifeq($(KERNELRELEASE),)
#KERNELDIR ?=/home/lht/kernel2.6/linux-2.6.14
KERNELDIR ?=/lib/modules/$(shell uname -r)/build
PWD := $(shellpwd)
modules:
$(MAKE)-C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE)-C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko*.mod.c .tmp_versions modules* Module*
.PHONY:modules modules_install clean
else
obj-m := hello.o
endif
标签:des style color io 使用 strong ar for sp
原文地址:http://blog.csdn.net/hbuxiaofei/article/details/38984655