Linux驱动入门——几个简单的Linux 模块
模块一:
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE ("GPL");
int init_module (void)
{
printk (KERN_INFO "Hello world\n");
return 0;
}
void cleanup_module (void)
{
printk (KERN_INFO "Goodbye world\n");
}
///////////////////////////////////////////////////////////
编写相关的makefile文件
ifeq ($(KERNELRELEASE),)
#KERNELDIR ?= /home/lht/kernel2.6/linux-2.6.14
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
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
.PHONY: modules modules_install clean
else
obj-m := hello.o
endif
makefile 文件的编写相对较复杂一些!但要自己尝试,逐渐攻破,为以后的开发做准备!
因为模块是要添加到内核只能过去的,因此,在权限方面应该是超级用户 : root
相关的编译命令如下:
首先:make
其次:通过insmod命令将模块添加到内核中
#insmod hello.ko //ko是模块的后缀。
再次:通过lsmod命令查看是否添加模块到内核中
#lsmod | grep hello
最后: 通过rmmod删除添加的模块
#rmmod hello
结束后,查看源码,适当修改,掌握实验内容。
一下的几个模块只需改hello.c源文件,不需要修改makefile文件,同样的方式编译即可。
模块二:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
MODULE_LICENSE ("GPL");
static int __init hello_2_init (void)
{
printk (KERN_INFO "Hello world\n");
return 0;
}
static void __exit hello_2_exit (void)
{
printk (KERN_INFO "Goodbye world\n");
}
module_init (hello_2_init);
module_exit (hello_2_exit);
模块三:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#define DRIVER_AUTHOR "Foo bar"
#define DRIVER_DESC "A sample driver"
MODULE_LICENSE ("GPL");
MODULE_AUTHOR (DRIVER_AUTHOR);
MODULE_DESCRIPTION (DRIVER_DESC);
MODULE_SUPPORTED_DEVICE ("TestDevice");
static int __init hello_2_init (void)
{
printk (KERN_INFO "Hello world\n");
return 0;
}
static void __exit hello_2_exit (void)
{
printk (KERN_INFO "Goodbye world\n");
}
module_init (hello_2_init);
module_exit (hello_2_exit);
模块四:
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>
#define DRIVER_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 int myshort = 1;
static int myint = 420;
static long int mylong = 9999;
static char *mystring = "blah";
static int array[2]= {-1, -1};
static int arr_argc = 0;
module_param (myshort, short, 0000);
MODULE_PARM_DESC (myshort, "A short integer");
module_param (myint, int, 0000);
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 a short integer: %hd\n", myshort);
printk (KERN_INFO "myint is an integer: %d\n", myint);
printk (KERN_INFO "mylong is a long integer: %ld\n", mylong);
printk (KERN_INFO "mystring is a string: %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 %d arguments in array\n", arr_argc);
return 0;
}
static void __exit hello_2_exit (void)
{
printk (KERN_INFO "hello driver cleaned up\n");
}
module_init (hello_2_init);
module_exit (hello_2_exit);
从上面的模块内容可以看出,源文件一直在改变,并且在功能上完善。
以上几个实例仅供参考。