Linux 驱动开发基础知识——Hello驱动程序(一)

个人名片:

🦁作者简介:一名喜欢分享和记录学习的在校大学生

🐯个人主页:妄北y

🐧个人QQ:2061314755

🐻个人邮箱:2061314755@qq.com

🦉个人WeChat:Vir2021GKBS

🐼本文由妄北y原创,首发CSDN 🎊🎊🎊

🐨座右铭:大多数人想要改造这个世界,但却罕有人想改造自己。

专栏导航:

妄北y系列专栏导航:

C/C++的基础算法:C/C++是一种常用的编程语言,可以用于实现各种算法,这里我们对一些基础算法进行了详细的介绍与分享。🎇🎇🎇

C/C++刷题库:分享一些关于编程的练习基础题,也会后续加入一系列的算法题,分享自己的解题思路和方法。🥰🥰🥰

计算机网络:对计算机网络的基础知识框架有一个简单的学习与认识,对计算机网络中常见的题型进行一个总结与归纳。🍾🍾🍾

QT基础入门学习:对QT的基础图形化页面设计进行了一个简单的学习与认识,利用QT的基础知识进行了翻金币小游戏的制作🤹🤹🤹

Linux基础编程:初步认识什么是Linux,为什么学Linux,安装环境,进行基础命令的学习,入门级的shell编程。🍻🍻🍻

Linux的系统编程+网络编程:IO编程、进程、线程、进程间通讯(包括管道、信号、信号量、共享内存等)网络编程主要就是socket,poll,epoll,以及对TCP/IP的理解,同时要学会高并发式服务器的编写。🙌🙌🙌

Linux应用开发基础开发:分享Linux的基本概念、命令行操作、文件系统、用户和权限管理等,网络编程相关知识,TCP/IP 协议、套接字(Socket)编程等,可以实现网络通信功能。💐💐💐

Linux项目开发:Linux基础知识的实践,做项目是最锻炼能力的一个学习方法,这里我们会学习到一些简单基础的项目开发与应用,而且都是毕业设计级别的哦。🤸🤸🤸

非常期待和您一起在这个小小的互联网世界里共同探索、学习和成长。💝💝💝 ✨✨ 欢迎订阅本专栏 ✨✨

文章介绍:

🎉 本篇文章对Linux驱动基础学习的相关知识进行分享!🥳🥳🥳

如果您觉得文章不错,期待你的一键三连哦,你的鼓励是我创作动力的源泉,让我们一起加油,一起奔跑,让我们顶峰相见!!!💪💪💪

🎁感谢大家点赞👍收藏⭐评论✍️

一、Hello 驱动(不涉及硬件操作)

我们选用的内核都是 4.x 版本,操作都是类似的:

rk3399 linux 4.4.154

rk3288 linux 4.4.154

imx6ul linux 4.9.88

am3358 linux 4.9.168

1.如何编写驱动程序

2.APP 打开的文件在内核中如何表示

APP 打开文件时,可以得到一个整数,这个整数被称为文件句柄 。对于 APP 的每一个文件句柄,在内核里面都有一个"**struct file"**与之对应。

可以猜测,我们使用 open 打开文件时,传入的 flags、mode 等参数会被记录在内核中对应的struct file 结构体里(f_flags、f_mode)

bash 复制代码
int open(const char *pathname, int flags, mode_t mode);

去读写文件时,文件的当前偏移地址也会保存在 struct file 结构体的 f_pos 成员里。

3. 打开字符设备节点时,内核中也有对应的 struct file

注意这个结构体中的结构体:struct file_operations *f_op,这是由驱动程序提供的。

结构体 struct file_operations 的定义如下:

二、编写代码

1.写驱动程序

参考driver/char 中的程序,包含头文件,写框架,传输数据:

(1)驱动中实现 open, read, write, release,APP 调用这些函数时,都打印内核信息

(2)APP 调用 write 函数时,传入的数据保存在驱动中

(3)APP 调用 read 函数时,把驱动中保存的数据返回给 APP

2.hello_drv.c:

cpp 复制代码
#include <linux/module.h>

#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>

/* 1. 确定主设备号                                                                 */
static int major = 0;
static char kernel_buf[1024];
static struct class *hello_class;


#define MIN(a, b) (a < b ? a : b)

/* 3. 实现对应的open/read/write等函数,填入file_operations结构体                   */
static ssize_t hello_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	int err;
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	err = copy_to_user(buf, kernel_buf, MIN(1024, size));
	return MIN(1024, size);
}

static ssize_t hello_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
	int err;
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	err = copy_from_user(kernel_buf, buf, MIN(1024, size));
	return MIN(1024, size);
}

static int hello_drv_open (struct inode *node, struct file *file)
{
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}

static int hello_drv_close (struct inode *node, struct file *file)
{
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}

/* 2. 定义自己的file_operations结构体                                              */
static struct file_operations hello_drv = {
	.owner	 = THIS_MODULE,
	.open    = hello_drv_open,
	.read    = hello_drv_read,
	.write   = hello_drv_write,
	.release = hello_drv_close,
};

/* 4. 把file_operations结构体告诉内核:注册驱动程序                                */
/* 5. 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数 */
static int __init hello_init(void)
{
	int err;
	
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	major = register_chrdev(0, "hello", &hello_drv);  /* /dev/hello */


	hello_class = class_create(THIS_MODULE, "hello_class");
	err = PTR_ERR(hello_class);
	if (IS_ERR(hello_class)) {
		printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
		unregister_chrdev(major, "hello");
		return -1;
	}
	
	device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello"); /* /dev/hello */
	
	return 0;
}

/* 6. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数           */
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);
	unregister_chrdev(major, "hello");
}


/* 7. 其他完善:提供设备信息,自动创建设备节点                                     */

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");

参考Linux的内核进行编写程序D:\linux\Linux-4.9.88\Linux-4.9.88\drivers\char(字符驱动程序)

第18~24行:确定主设备号

第19行:主设备号

第20行:将write得到的数据传入数组kernel_buf

第24行:得到数据长度的最小值
第26~53行:实现对应的open/read/write等函数,填入file_operations结构体

第31行:读取kernel_buf中的信息

第39行:将信息写入 kernel_buf
第55~62行: 定义自己的file_operations结构体

在结构体内设置open, read, write, release这四个成员
第66~85行:得有一个入口函数:安装驱动程序时,就会去调用这个入口函数

第82行:创造设备
**第87~94行:**得到一个出口函数:卸载驱动程序时,就会去调用这个出口函数

第92行:销毁设备
第99~102行:提供设备信息,自动创建设备节点

第102行:增加GPL协议

阅读一个驱动程序,从它的入口函数开始 ,第 66 行就是入口函数。它的主要工作就是第 71 行,向内核注册一个 file_operations 结构体:hello_drv, 这就是字符设备驱动程序的核心

file_operations 结构体 hello_drv 在第 56 行定义,里面提供了open/read/write/release成员,应用程序调用 open/read/write/close 时就会导致这些成员函数被调用。

file_operations 结构体 hello_drv 中的成员函数都比较简单,大多数只是打印而已。要注意的是,驱动程序和应用程序之间传递数据要使用copy_from_user/copy_to_user 函数。

3.写测试程序

测试程序要实现写、读功能:

cpp 复制代码
./hello_drv_test -w www.100ask.net // 把字符串"www.100ask.net"发给驱动程序
./hello_drv_test -r // 把驱动中保存的字符串读回来
cpp 复制代码
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

/*
 * ./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 <string>\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;
}

三、测试

编写驱动程序的Makefile

驱动程序中包含了很多头文件,这些头文件来自内核,不同的 ARM 板它的某些头文件可能不同。所以编译驱动程序时,需要指定板子所用的内核的源码路径。、

要编译哪个文件?这也需要指定,设置 obj-m 变量即可,怎么把.c 文件编译为驱动程序.ko?这要借助内核的顶层 Makefile。 本驱动程序的 Makefile 内容如下:

cpp 复制代码
# 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 = 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

先设置好交叉编译工具链,编译好你的板子所用的内核,然后修改 Makefile 指定内核源码路径,最后即可执行 make 命令编译驱动程序和测试程序。

**注意:**我们是在 Ubuntu 中编译程序,但是需要在 ARM 板子上测试。所以需要把程序放到 ARM 板子上。

启动单板后,可以通过 NFS 挂载 Ubuntu 的某个目录,访问该目录中的程序。

bash 复制代码
cp *.ko hello_drv_test ~/nfs_rootfs/

在 ARM 板上测试:

//安装驱动程序

cpp 复制代码
[root@100ask:/mnt]# insmod hello_drv.ko

// 驱动程序会生成设备节点

// 查看测试程序的用法

//往驱动程序中写入字符串

// 从驱动程序中读出字符串

大佬觉得有用的话点个赞 👍🏻 呗。

❤️❤️❤️本人水平有限,如有纰漏,欢迎各位大佬评论批评指正!😄😄😄

💘💘💘如果觉得这篇文对你有帮助的话,也请给个点赞、收藏下吧,非常感谢!👍 👍 👍

🔥🔥🔥任务在无形中完成,价值在无形中升华,让我们一起加油吧!🌙🌙🌙

相关推荐
只对您心动1 小时前
【C高级】有关shell脚本的一些练习
linux·c语言·shell·脚本
lldhsds1 小时前
linux下的分布式Minio部署实践
linux·minio·分布式对象存储
OH五星上将2 小时前
OpenHarmony(鸿蒙南向开发)——小型系统内核(LiteOS-A)【内核通信机制】上
linux·嵌入式硬件·harmonyos·openharmony·鸿蒙开发·liteos-a·鸿蒙内核
爱桥代码的程序媛2 小时前
鸿蒙OpenHarmony【轻量系统内核通信机制(互斥锁)】子系统开发
嵌入式硬件·harmonyos·鸿蒙·openharmony··鸿蒙开发·子系统开发
DC_BLOG3 小时前
IPv6(四)
运维·服务器·网络·ip
shelby_loo3 小时前
通过 Docker 部署 MySQL 服务器
服务器·mysql·docker
ZBzibing3 小时前
[游戏技术]L4D服务器报错解决
服务器·游戏
沈艺强3 小时前
伊犁linux 创建yum 源过程
linux·运维·服务器
拾光师3 小时前
linux命令行快捷键
linux·运维·服务器
Dola_Pan5 小时前
Linux文件IO(二)-文件操作使用详解
java·linux·服务器