分享

linux驱动开发第1讲:带你编写一个最简单的字符设备驱动

 山峰云绕 2022-04-19
https://www.toutiao.com/article/6848065724905161228/?log_from=803ee86960ffa_1650342534396

关注“技术简说”,一步一步教你开发linux内核和驱动。

hello world!是广大程序员入门一门新语言的第一步。

今天,我们来看一个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驱动开发第1讲:带你编写一个最简单的字符设备驱动

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

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/sched.h>
#include <linux/slab.h>

#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\r\n");
    return 0;
}

ssize_t hello_write(struct file *f, const char __user *u, size_t s, loff_t *l)
{
    printk(KERN_EMERG"hello_write\r\n");
    return 0;
}
ssize_t hello_read(struct file *f, char __user *u, size_t s, loff_t *l)
{
    printk(KERN_EMERG"hello_read\r\n");      
    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 \n"); 
    }else {
    printk(KERN_EMERG"register_chrdev_region error n");
        return ERROR;
    }
    printk(KERN_EMERG" hello driver init \n");
    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驱动开发第1讲:带你编写一个最简单的字符设备驱动

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

ifneq ($(KERNELRELEASE),)
obj-m := helloDev.o
else
PWD := $(shell pwd)
KDIR:= /lib/modules/4.4.0-31-generic/build
#KDIR := /lib/modules/`uname -r`/build
all:
	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驱动开发第1讲:带你编写一个最简单的字符设备驱动

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

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

linux驱动开发第1讲:带你编写一个最简单的字符设备驱动

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

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

linux驱动开发第1讲:带你编写一个最简单的字符设备驱动

helloDriver加载成功,打印出了:

[11837.379638] register_chrdev_region ok

[11837.379642] hello driver init

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

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

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

linux驱动开发第1讲:带你编写一个最简单的字符设备驱动

本节来看驱动的测试。

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

#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/select.h>


#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\r\n",fd);
    if(-1 == fd) {
      	perror("open file error\r\n");
		return -1;
    }	
	else {
		printf("open successe\r\n");
	}
    
    w_len = write(fd,buf, DATA_NUM);
    r_len = read(fd, buf, DATA_NUM);
    printf("%d %d\r\n", w_len, r_len);
    printf("%s\r\n",buf);

    return 0;
}

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

linux驱动开发第1讲:带你编写一个最简单的字符设备驱动

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

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

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

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

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

root@ubuntu:/home/jinxin/drivers/helloDev# 

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

linux驱动开发第1讲:带你编写一个最简单的字符设备驱动

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

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

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

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

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

针对以上可能的问题,敬请期待《linux内核驱动第2讲》,我会一一回答以上问题。

关注“技术简说”,一步一步教你开发linux内核和驱动。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多