从零开始写linux字符设备驱动程序(三)(基于友善之臂tiny4412开发板)

简介: 这一节,我们再来看看新的知识点,这一次,我们将进一步完善这个字符设备的驱动程序。首先,将上一节的代码做下修改:#include #include #include #include #include #include #include //创建一个字符设备struct c...

这一节,我们再来看看新的知识点,这一次,我们将进一步完善这个字符设备的驱动程序。

首先,将上一节的代码做下修改:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/slab.h>

//创建一个字符设备
struct char_dev
{
    struct cdev c_dev ;
    dev_t dev_num ;
    char buf[1024];  
};

int my_open()
{
    printk("cdev open");	
}

int my_close()
{
    printk("cdev del");
}

struct file_operations my_ops = {
	.open = my_open,
	.release = my_close ,
};

struct char_dev *test_dev ;
static int __init  cdev_test_init(void)
{
	int ret ;
	//1、给字符设备结构分配内存
	test_dev = kmalloc(sizeof(*test_dev),GFP_KERNEL);
	if(!test_dev){
	   ret = -ENOMEM ;
	   goto malloc_dev_fair;
	}
	//2、申请设备号并注册字符设备
	ret = alloc_chrdev_region(&test_dev->dev_num,1,1,"test_dev");
	if(ret < 0){
	   goto alloc_chrdev_fair ;
	}
	//3、初始化字符设备
	cdev_init(&test_dev->dev_num , &my_ops);
	//4、添加一个字符设备
	ret = cdev_add(&test_dev->c_dev,test_dev->dev_num,1);	
	if(ret < 0){
	   goto cdev_add_fair;
	}
	my_open();
	return 0 ;
	cdev_add_fair:
	return ret ;
	malloc_dev_fair :
	return ret  ;
	alloc_chrdev_fair :
	return ret ;
}

static int __exit cdev_test_exit(void)
{
	//删除设备
	cdev_del(&test_dev->c_dev);
	//注销驱动-->后面写1表示从dev_no开始连续一个
	unregister_chrdev_region(test_dev->dev_num,1);
	return 0 ;
}


module_init(cdev_test_init);
module_exit(cdev_test_exit);
MODULE_LICENSE("GPL");
在代码中,我们要实现一个虚拟的字符设备,这个设备很简单,只不过更加丰富了。

我们首先创建一个字符设备,用一个结构体char_dev来表示。

对结构体分配内存,然后申请设备号并注册,最后初始化,再将这个字符设备加到内核里去,一旦这些操作成功后,将调用my_open函数。

这就是一个字符设备的最基本构成。

上节我们已经说过alloc_chrdev_region这个函数的作用。

那么这节多了file_operations这个结构体,它的功能是什么?

当一个字符设备被注册后,我们随即就要来操作这个字符设备,open  , read , write , close等操作。

如下代码:

struct file_operations {
	struct module *owner;
	loff_t (*llseek) (struct file *, loff_t, int);
	ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
	ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
	ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
	ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
	int (*readdir) (struct file *, void *, filldir_t);
	unsigned int (*poll) (struct file *, struct poll_table_struct *);
	long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
	long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
	int (*mmap) (struct file *, struct vm_area_struct *);
	int (*open) (struct inode *, struct file *);
	int (*flush) (struct file *, fl_owner_t id);
	int (*release) (struct inode *, struct file *);
	int (*fsync) (struct file *, loff_t, loff_t, int datasync);
	int (*aio_fsync) (struct kiocb *, int datasync);
	int (*fasync) (int, struct file *, int);
	int (*lock) (struct file *, int, struct file_lock *);
	ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
	unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
	int (*check_flags)(int);
	int (*flock) (struct file *, int, struct file_lock *);
	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
	ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
	int (*setlease)(struct file *, long, struct file_lock **);
	long (*fallocate)(struct file *file, int mode, loff_t offset,
			  loff_t len);
};
那么内核是如何去识别相应的函数呢?

是通过系统调用

在上层应用程序,打个比方。

通过open()打印相应的设备,那么syscall函数就会通过系统调用号识别到内核态里的函数,进而调用到我们这里实现的my_open,这就是内核态和用户态相互沟通的方式。


这里我就不去写相应的应用程序了,以前也写过了,我就直接将open函数调用放在init函数,随着字符设备注册并执行。

这样将zImage下载到开发板上,串口上也是可以打印cdev_open的。

不知道怎么用应用程序去读写设备的可以参考以下文章:

http://blog.csdn.net/morixinguan/article/details/50619675


接下来看看本节使用的函数:

void cdev_init(struct cdev *, const struct file_operations *);

int cdev_add(struct cdev *, dev_t, unsigned);
void cdev_del(struct cdev *);

static __always_inline void *kmalloc(size_t size, gfp_t flags);

留心的小伙伴会发现,在exit函数中,我没有对内存进行释放,这里是故意这么做的,为了提醒粗心的伙伴,在内核中,分配的内存一定要释放的。

释放调用函数:

void kfree(const void *objp)





目录
相关文章
|
28天前
|
Shell Linux C语言
【Shell 命令集合 设备管理 】Linux 创建设备文件 MAKEDEV命令 使用指南
【Shell 命令集合 设备管理 】Linux 创建设备文件 MAKEDEV命令 使用指南
33 0
|
28天前
|
监控 Linux Shell
【Shell 命令集合 网络通讯 】Linux管理终端设备的登录过程 getty命令 使用指南
【Shell 命令集合 网络通讯 】Linux管理终端设备的登录过程 getty命令 使用指南
32 0
|
28天前
|
监控 Linux Shell
【Shell 命令集合 磁盘维护 】Linux 交换分区的特殊文件或设备 swapon命令使用指南
【Shell 命令集合 磁盘维护 】Linux 交换分区的特殊文件或设备 swapon命令使用指南
38 1
|
28天前
|
安全 Shell Linux
【Shell 命令集合 网络通讯 】Linux 打开终端设备 mingetty命令 使用指南
【Shell 命令集合 网络通讯 】Linux 打开终端设备 mingetty命令 使用指南
38 0
|
11天前
|
网络协议 Linux SDN
虚拟网络设备与Linux网络协议栈
在现代计算环境中,虚拟网络设备在实现灵活的网络配置和隔离方面发挥了至关重要的作用🔧,特别是在容器化和虚拟化技术广泛应用的今天🌐。而Linux网络协议栈则是操作系统处理网络通信的核心💻,它支持广泛的协议和网络服务🌍,确保数据正确地在网络中传输。本文将深入分析虚拟网络设备与Linux网络协议栈的关联,揭示它们如何共同工作以支持复杂的网络需求。
|
12天前
|
存储 缓存 固态存储
Linux设备全览:从字符到块,揭秘每种设备的秘密
在Linux的世界里,设备是构成系统的基础,它们使得计算机能够与外界互动。Linux设备可以大致分为几种类型,每种类型都有其独特的特性和用途。🌌让我们一起探索这些设备类型及其特性。
|
28天前
|
Shell Linux C语言
【Shell 命令集合 系统设置 】Linux 配置鼠标设备的相关设置 mouseconfig命令 使用指南
【Shell 命令集合 系统设置 】Linux 配置鼠标设备的相关设置 mouseconfig命令 使用指南
34 0
|
28天前
|
存储 编解码 Shell
【Shell 命令集合 系统设置 】⭐Linux 设置和调整帧缓冲设备 fbset命令 使用指南
【Shell 命令集合 系统设置 】⭐Linux 设置和调整帧缓冲设备 fbset命令 使用指南
28 0
|
28天前
|
存储 安全 Linux
【Shell 命令集合 设备管理 】Linux 更改根文件系统的设备号 rdev命令 使用指南
【Shell 命令集合 设备管理 】Linux 更改根文件系统的设备号 rdev命令 使用指南
29 0
|
Linux
手把手教你写第一个Linux驱动程序
说到写第一个linux内核驱动程序,再简单不过了。以前也写过,这次将做一个图形化的整理。 我这里以我的开发环境tiny4412为主,我将在这上面写第一个驱动程序。 首先,我在driver/目录下创建yangyx目录, 在目录下新建一个hello.c和一个Makefile hello.c内容如下: hello.c包含的头文件就类似咱们写标准C一样的头文件。
1203 0