• 嵌入式Linux驱动开发基础知识(一)——hello驱动程序开发


    一、怎么编写驱动程序

    linux下编写驱动程序有一套完整的模板,具体包含以下内容:

    ① 确定主设备号,也可以让内核分配
    ② 定义自己的file_operations结构体
    ③ 实现对应的drv_open/drv_read/drv_write等函数,填入file_operations结构体
    ④ 把file_operations结构体告诉内核:register_chrdev
    ⑤ 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数
    ⑥ 有入口函数就应该有出口函数:卸载驱动程序时,出口函数调用unregister_chrdev
    ⑦ 其他完善:提供设备信息,自动创建设备节点:class_create, device_create
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    注解:

    • 主设备号类似于ID一样,主设备号可以自己分配也可以由内核分配(给0就表示由内核分配)
    • file_operations为linux驱动的文件句柄,这个结构体包含了一些常用的参数: 读写模式、open函数、read函数、write函数等等,开发者需要重定义这些函数。
    • 实现drv_open/drv_read/drv_write函数,实现驱动的打开,读写功能。
    • register_chrdev函数将file_operations告诉内核,这个函数是联系内核和驱动的关键函数
    • 驱动程序写好了,谁来调用呢?这个需要一个入口函数,入口函数的作用是当安装驱动程序时,就会去调用这个入口函数。入口函数需要调用register_chrdev函数
    • 出口函数:卸载驱动程序时,也同样会进入一个函数,那么这个函数就叫出口函数,出口函数调用unregister_chrdev
    • 除了以上信息,还需要提供设备信息,自动创建设备节点等等相关的代码。

    二、驱动程序、应用程序、Makefile文件

    1. 驱动程序

    hello_drv.c

    /*所需要的头文件参考drivers下的misc.c文件*/
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    /*open、read、write、close函数声明*/
    static int 		hello_drv_open	   	(struct inode *, 	struct file *);
    static ssize_t 	hello_drv_read 	(struct file *, 	char __user *, 			size_t, loff_t *);
    static ssize_t 	hello_drv_write (struct file *, 	const char __user *, 	size_t, loff_t *);
    static int 		hello_drv_close (struct inode *, 	struct file *);
    
    
    /* 1.确定主设备号,也可以让内核分配 */
    static int 			major = 0;					// 主设备号:给0时表示让内核进行设置
    static char 		kernel_buf[1024] = {0};		// 内核buf,用于存储应用层传递给驱动的数据
    static struct class *hello_class;				// 驱动类
    
    
    #define MIN(a, b) (a < b ? a : b)		
    
    /* 2. 定义自己的file_operations结构体 */
    static const struct file_operations hello_drv = {
    	.owner	 = THIS_MODULE,
    	.open    = hello_drv_open,
    	.read    = hello_drv_read,
    	.write   = hello_drv_write,
    	.release = hello_drv_close,
    };
    
    /* 3. 实现对应的drv_open/drv_read/drv_write等函数,填入file_operations结构体 */
    int 	hello_drv_open	   	(struct inode *node, struct file *file)
    {
    	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    
    	return 0;
    }
    
    ssize_t hello_drv_read 	(struct file *file, char __user *buf, size_t size, loff_t *offset)
    {
    	int err = 0;
    	
    	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    
    	// 应用层需要读取内核数据需要通过copy_to_user
    	err = copy_to_user(buf, kernel_buf, MIN(1024, size));
    
    	return MIN(1024, size);
    }
    
    ssize_t hello_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
    {
    	int err = 0;
    
    	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    
    	// 应用层需要写入内核数据需要通过copy_from_user
    	err = copy_from_user(kernel_buf, buf, MIN(1024, size));
    
    	return MIN(1024, size);
    }
    
    int 	hello_drv_close (struct inode *node, struct file *file)
    {
    	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    
    	return 0;
    }
    
    /* 4. 把file_operations结构体告诉内核:注册程序 */
    /* 5. 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数 */
    static int __init hello_init(void)
    {
    	int err = 0;
    
    	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    	
    	// 将hello驱动注册进内核中, hello为驱动在内核中的名称
    	major = register_chrdev(major, "hello", &hello_drv);			  /* /dev/hello */
    
    	/*创建类和设备:先创建类,后创建设备*/
    	hello_class = class_create(THIS_MODULE, "hello");
    	err = PTR_ERR(hello_class);
    	if (IS_ERR(hello_class))
    	{
    		// 如果创建类不成功,则需要卸载驱动
    		unregister_chrdev(major, "hello");
    		return -1;
    	}
    
    	// 创建设备
    	device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello"); /* /dev/hello */
    
    	return 0;
    }
    
    /* 6. 有入口函数就应该有出口函数:卸载驱动程序时,出口函数调用unregister_chrdev */
    static void __exit hello_exit(void)
    {
    	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    
    	/*销毁类和设备:先销毁设备,后销毁类*/
    	device_destroy(hello_class, MKDEV(major, 0));
    	class_destroy(hello_class);
    
    	// 将hello驱动卸载出内核中
    	unregister_chrdev(major, "hello");
    }
    
    /* 7. 其他完善:提供设备信息,自动创建设备节点:class_create, device_create */
    // 将hello_init和hello_exit修饰为入口函数和出口函数
    module_init(hello_init);
    module_exit(hello_exit);
    // 驱动程序需要遵循GPL开源协议,否则无法运行
    MODULE_LICENSE("GPL");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126

    2. 应用程序

    hello_drv_test.c

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    /*
     * ./hello_drv_test -w abc
     * ./hello_drv_test -r
     */
    int main(int argc, char **argv)
    {
    	int fd;
    	char buf[1024];
    	int len;
    	
    	/* 1. 判断参数 */
    	if (argc < 2) 
    	{
    		/*参数不对时,提示用户怎么使用*/
    		printf("Usage: %s -w \n", argv[0]);
    		printf("       %s -r\n", argv[0]);
    		return -1;
    	}
    
    	/* 2. 打开文件 */
    	fd = open("/dev/hello", O_RDWR);
    	if (fd == -1)
    	{
    		/*打开文件失败*/
    		printf("can not open file /dev/hello\n");
    		return -1;
    	}
    
    	/* 3. 写文件或读文件 */
    	if ((0 == strcmp(argv[1], "-w")) && (argc == 3))
    	{
    		len = strlen(argv[2]) + 1;
    		len = len < 1024 ? len : 1024;
    		write(fd, argv[2], len);
    	}
    	else
    	{
    		len = read(fd, buf, 1024);		
    		buf[1023] = '\0';
    		printf("APP read : %s\n", buf);
    	}
    	
    	close(fd);
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    3. Makefile文件

    # 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
    # 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
    # 2.1 ARCH,          比如: export ARCH=arm64
    # 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
    # 2.3 PATH,          比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin 
    # 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
    #       请参考各开发板的高级用户使用手册
    
    # KERN_DIR = /home/book/100ask_roc-rk3399-pc/linux-4.4
    KERN_DIR = /home/book/100ask_imx6ull-sdk/Linux-4.9.88
    
    all:
    	make -C $(KERN_DIR) M=`pwd` modules 
    	$(CROSS_COMPILE)gcc -o hello_drv_test hello_drv_test.c 
    
    clean:
    	make -C $(KERN_DIR) M=`pwd` modules clean
    	rm -rf modules.order
    	rm -f hello_drv_test
    
    obj-m	+= hello_drv.o
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    三、编译及运行

    1. 虚拟机(IP: 192.168.5.11)
    	1.1 编译驱动 进入相应的驱动文件夹,然后运行make就可以了 驱动和应用文件会产生相应的ko文件和可执行文件( hello_drv.ko  hello_drv_test)
    	1.2 将驱动产生的文件复制到网络挂载文件夹上: cp hello_drv.ko  hello_drv_test /home/book/nfs_rootfs/
    2. 开发板(IP:192.168.5.9)
    	2.1 首先使用ifconfig查看ip地址是否为192.168.5.9,如果不是则需要使用 ifconfig eth0 192.168.5.9
    	2.2 将开发板挂载到虚拟机的文件夹,运行以下命令 mount -t nfs -o nolock,vers=3 192.168.5.11:/home/book/nfs_rootfs /mnt
    	2.3 查看/mnt目录下是否有hello_drv.ko  hello_drv_test文件 cd /mnt/
    		ls -l
    	2.4 安装驱动,运行如下命令: insmod hello_drv.ko
    	2.5 查看是否安装驱动成功,运行如下命令: 列出字符和块设备的主设备号,以及分配到这些设备号的设备名称: cat /proc/devices  会出现类似:240 hello 的信息, 240表示主设备号, hello为驱动的名字
    	 	显示已载入系统的模块:lsmod   
    	 	会出现: hello_drv       3744   0   
    	 	hello_drv:驱动文件名称 3744:文件大小 0:正在使用数量
    	 	查看是否为设备名:(/dev/驱动名字)
    	 	ls /dev/hello -l 显示: crw------- 1 root root 240, 0 Jan  1 00:14 /dev/hello 权限                 主设备号     时间      设备名称
    	2.6 测试驱动(这一块要根据应用程序的用法进行使用) 运行: ./hello_drv_test -w "I am success!"
    		运行: ./hello_drv_test -r
    		显示:APP read : I am success!
    	2.7 卸载驱动 rmmod hello_drv
    		运行后可以使用lsmod 查看是否还存在hello_drv驱动了
    	2.8 显示调试信息 在运行过程中默认是不显示调试信息的,需要使用命令: dmesg单独查看
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    ElasticSearch综合练习题,ES为8版本,使用Kibana运行语句
    这个合租室友真的没有一点公德心,还好他搬走了
    SystemVerilog Assertions应用指南 第一章(1.27章节 “within”运算符)
    MiniConda在Ubuntu终端的安装
    React组件复用
    Linux实践学习的一些归纳
    redis的主从复制,哨兵和cluster集群
    pta团队天题题-阅览室(c++)
    【LeetCode】11. 盛最多水的容器
    阿里云物联网MQTT对接
  • 原文地址:https://blog.csdn.net/FourLeafCloverLLLS/article/details/126831114