Welcome 微信登录

首页 / 操作系统 / Linux / Linux下Driver开发

Linux下的驱动程序也没有听上去的那么难实现,我们可以看一下helloworld这个例子就完全可以了解它的编写的方式!我们还是先看一个这个例子,helloworld1)编写helloworld.c,内容如下:---------------- code start---------------------------#include <linux/module.h>//与module相关的信息
#include <linux/kernel.h>
#include <linux/init.h>      //与init相关的函数static int __init hellokernel_init(void)
{
        printk(KERN_INFO "Hello kernel! ");
        return 0;
}static void __exit hellokernel_exit(void)
{
        printk(KERN_INFO "Exit kernel! ");
}
module_init(hellokernel_init);
module_exit(hellokernel_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("xxxx");---------------- code end---------------------------2)编写Makefile---------------- code start---------------------------obj-m := helloworld.oPWD       := $(shell pwd)all:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modulesclean:
        rm -rf *.o *~ core .*.cmd *.mod.c ./tmp_version---------------- code end---------------------------3)执行make编译成功之后会生成相应有ko文件,也就是我们想要的驱动了4)驱动程序的相关操作      a)查看ko模块的信息 modinfo      b)插入模块 insmod helloworld.ko      c)卸载模块 rmmod helloworld      d)还有一个modprobe功能,以后介绍!5)查看驱动的打印信息      使用dmesg可以查看在驱动的相关打印信息!       现在有例子是会有如下的打印内容:---------------------log start----------------------------[27520.195551] Exit kernel!
[27948.531569] Hello kernel!---------------------log end----------------------------