今天,我们来看一个hello驱动,希望这是大家入门linux内核驱动的良好开局。

我的环境是ubuntu 14.04,内核版本 4.4.0-31-generic,本节我会开发一个基于ubuntu 14.04下的最简单的hello驱动,带大家领略驱动的魅力。

开发linux内核驱动需要以下4个步骤:

  1. 编写hello驱动代码
  2. 编写makefile
  3. 编译和加载hello驱动
  4. 编写应用程序测试hello驱动



linux imagemagick支持heic generic linux_linux major头文件


驱动代码如下 helloDev.c,这是一个最小、最简单的驱动,我去掉了其他的不相干代码,尽量让大家能了解驱动本身。

#include #include #include #include #include #include #include #include #define BUFFER_MAX    (10)#define OK            (0)#define ERROR         (-1)struct cdev *gDev;struct file_operations *gFile;dev_t  devNum;unsigned int subDevNum = 1;int reg_major  =  232;    int reg_minor =   0;char *buffer;int flag = 0;int hello_open(struct inode *p, struct file *f){    printk(KERN_EMERG"hello_open");    return 0;}ssize_t hello_write(struct file *f, const char __user *u, size_t s, loff_t *l){    printk(KERN_EMERG"hello_write");    return 0;}ssize_t hello_read(struct file *f, char __user *u, size_t s, loff_t *l){    printk(KERN_EMERG"hello_read");          return 0;}int hello_init(void){        devNum = MKDEV(reg_major, reg_minor);    if(OK == register_chrdev_region(devNum, subDevNum, "helloworld")){        printk(KERN_EMERG"register_chrdev_region ok ");     }else {    printk(KERN_EMERG"register_chrdev_region error n");        return ERROR;    }    printk(KERN_EMERG" hello driver init ");    gDev = kzalloc(sizeof(struct cdev), GFP_KERNEL);    gFile = kzalloc(sizeof(struct file_operations), GFP_KERNEL);    gFile->open = hello_open;    gFile->read = hello_read;    gFile->write = hello_write;    gFile->owner = THIS_MODULE;    cdev_init(gDev, gFile);    cdev_add(gDev, devNum, 3);    return 0;}void __exit hello_exit(void){    cdev_del(gDev);    unregister_chrdev_region(devNum, subDevNum);    return;}module_init(hello_init);module_exit(hello_exit);MODULE_LICENSE("GPL");


linux imagemagick支持heic generic linux_linux open 头文件_02


有了驱动文件之后,我们还需要一个Makefile才能把驱动编译出来:

ifneq ($(KERNELRELEASE),)obj-m := helloDev.oelsePWD := $(shell pwd)KDIR:= /lib/modules/4.4.0-31-generic/build#KDIR := /lib/modules/`uname -r`/buildall:make -C $(KDIR) M=$(PWD)clean:rm -rf *.o *.ko *.mod.c *.symvers *.c~ *~endif

linux应用层程序在编译的时候,需要链接c运行时库和glibc库。那驱动需不需要呢?

驱动也需要,但是驱动不能链接和使用应用层的任何lib库,驱动需要引用内核的头文件和函数。所以,编译的时候需要指定内核源码的地址。为了开发方便,也可以安装内核开发包,之后引用这个内核开发包的目录也可以。本例为:/lib/modules/4.4.0-31-generic/build


linux imagemagick支持heic generic linux_linux major头文件_03


驱动文件和Makefile都有了,那么接下来就可以编译和加载驱动了!

在驱动目录下,执行make进行编译:


linux imagemagick支持heic generic linux_linux major头文件_04


编译出来的驱动文件,名称为:helloDev.ko

接下来把这个驱动加载到内核:


linux imagemagick支持heic generic linux_linux major头文件_05


helloDriver加载成功,打印出了:

[11837.379638] register_chrdev_region ok

[11837.379642] hello driver init

可见,执行insmod的时候,驱动文件里的hello_init被调用了。

那驱动文件里的hello_exit什么时候会被调用呢?

可能聪明的你已经猜到了,那就是执行 rmmod helloDev.ko的时候。


linux imagemagick支持heic generic linux_linux major头文件_06


本节来看驱动的测试。

我们需要编写一个应用层的程序来对hello驱动进行测试:(test.c)

#include #include #include #include #define DATA_NUM    (64)int main(int argc, char *argv[]){    int fd, i;    int r_len, w_len;    fd_set fdset;    char buf[DATA_NUM]="hello world";    memset(buf,0,DATA_NUM);    fd = open("/dev/hello", O_RDWR);printf("%d",fd);    if(-1 == fd) {      perror("open file error");return -1;    }else {printf("open successe");}        w_len = write(fd,buf, DATA_NUM);    r_len = read(fd, buf, DATA_NUM);    printf("%d %d", w_len, r_len);    printf("%s",buf);    return 0;}

编译并执行,发现错误,找不到设备文件:


linux imagemagick支持heic generic linux_linux major头文件_07


这是因为还没有创建hello驱动的设备文件,我们为hello驱动手动创建设备文件:

root@ubuntu:/home/jinxin/drivers/helloDev# mknod /dev/hello c 232 0

备注:这里的232和0要跟驱动文件里定义的主次设备号对应起来!

然后再次执行测试程序,发现成功了:

root@ubuntu:/home/jinxin/drivers/helloDev# ./test 3open successe0 0root@ubuntu:/home/jinxin/drivers/helloDev#

然后再次执行dmesg查看驱动输出,发现驱动里的hell_open, hello_write, hello_read被依次调用了。


linux imagemagick支持heic generic linux_linux major头文件_08


这就是一个完整的、最简单的驱动的开发和测试的流程。

我想大家可能会有几个问题:

1.驱动测试的时候为什么要有设备文件,设备文件的作用是什么?hello驱动的设备文件创建的时候为什么要指定主设备号为232, 此设备号为0?

2.对/dev/hello执行write()调用的时候,怎么就调用到了驱动里的hello_write()里去了?

3.测试程序的read和write的返回值为什么都是0?