6ull驱动记录--字符设备驱动开发基本框架

2024-08-30 14:20

本文主要是介绍6ull驱动记录--字符设备驱动开发基本框架,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、字符设备驱动开发

1.1 字符设备

字符设备是Linux驱动中最基本的设备驱动,所谓的字符设备就是按照字节流(一个一个字节)进行读写操作的设备。常见的字符设备有点灯、LED、按键、IIC、SPI、LCD等等。

1.2 字符设备的实现

要实现一个字符设备程序,需要包括驱动模块加载、字符设备、实现的具体操作函数、LICENSE

1.2.1 驱动模块的加载和卸载

函数介绍:

Linux 驱动有两种运行方式:

第一种是将驱动编译进 Linux 内核中,这样当 Linux 内核启动的时候就会自动运行驱动程序。

第二种是将驱动编译成模块(Linux 下模块扩展名为.ko),在Linux 内核启动以后使用“insmod”命令加载驱动模块。(常用)

模块有加载和卸载两种操作,我们在编写驱动的时候需要注册这两种操作函数,模块的加载和卸载注册函数如下

module_init(xxx_init); //注册模块加载函数 
module_exit(xxx_exit); //注册模块卸载函数
说明:

module_init 函数用来向 Linux 内核注册一个模块加载函数,参数 xxx_init 就是需要注册的具体函数,当使用“insmod”命令加载驱动的时候,xxx_init 这个函数就会被调用。

module_exit()函数用来向 Linux 内核注册一个模块卸载函数,参数 xxx_exit 就是需要注册的具体函数,当使用“rmmod”命令卸载具体驱动的时候 xxx_exit 函数就会被调用。

使用:
/* 驱动入口函数 */ 
static int __init xxx_init(void) 
{ 
/* 入口函数具体内容 */ 
return 0; } 
/* 驱动出口函数 */ 
static void __exit xxx_exit(void) 
{ 
/* 出口函数具体内容 */ 
} 
/* 将上面两个函数指定为驱动的入口和出口函数 类似于声明*/ 
module_init(xxx_init); 
module_exit(xxx_exit);

1.2.2 字符设备的注册和注销

函数介绍:

加载驱动模块之后需要注册字符设备和注销字符设备,函数模型:

static inline int register_chrdev(unsigned int major, const char *name, 
const struct file_operations *fops) 
static inline void unregister_chrdev(unsigned int major, const char *name) 

major:主设备号,Linux 下每个设备都有一个设备号,设备号分为主设备号和次设备号两部分。

name:设备名字,指向一串字符串。

fops:结构体 file_operations 类型指针,指向设备的操作函数集合变量。

使用:
static struct file_operations fops_test;
/* 驱动入口函数 */ 
static int __init xxx_init(void) 
{ 
/* 入口函数具体内容 */ int retvalue =0 ;retvalue = register_chrdev(200,"test_dev",fops_test);if(retvalue < 0){//创建失败处理}
return 0; } 
/* 驱动出口函数 */ 
static void __exit xxx_exit(void) 
{ 
/* 出口函数具体内容 */unregister_chrdev(200,"test_dev"); 
} 
/* 将上面两个函数指定为驱动的入口和出口函数 类似于声明*/ 
module_init(xxx_init); 
module_exit(xxx_exit);
设备号说明

Linux 中每个设备都有一个设备号,设备号由主设备号(12位)和次设备号(20位)两部分组成

1、主设备号表示某一个具体的驱动:比如LED、按键设备、adc设备等

2、次设备号表示使用这个驱动的各个设备:比如LED1,LED2、、、等

因此 Linux系统中主设备号范围为 0~4095

当有两个设备,主设备号一样,次设备号不同,会有两个设备节点。

1.2.3 实现设备的具体操作函数-file_operations

函数介绍

file_operations 的结构体是Linux 内核驱动操作函数集合:

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 (*read_iter) (struct kiocb *, struct iov_iter *);ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);int (*iterate) (struct file *, struct dir_context *);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 (*mremap)(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 **, void **);long (*fallocate)(struct file *file, int mode, loff_t offset,loff_t len);void (*show_fdinfo)(struct seq_file *m, struct file *f);
#ifndef CONFIG_MMUunsigned (*mmap_capabilities)(struct file *);
#endif
};
使用

假设应用程序需要通过 read 和 write 这两个函数进行读写操作,所以需要实现 file_operations 中的 read 和 write 这两个函数。


/* 打开设备 */ 
static int chrtest_open(struct inode *inode, struct file *filp) 
{ /* 用户实现具体功能 */ return 0; } /* 从设备读取 */ 
static ssize_t chrtest_read(struct file *filp, char __user *buf,  size_t cnt, loff_t *offt) 
{ /* 用户实现具体功能 */ return 0; } /* 向设备写数据 */ static ssize_t chrtest_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt) { /* 用户实现具体功能 */ return 0; } /* 关闭/释放设备 */ static int chrtest_release(struct inode *inode, struct file *filp) { /* 用户实现具体功能 */  return 0; } static struct file_operations test_fops = { .owner = THIS_MODULE,  .open = chrtest_open, .read = chrtest_read, .write = chrtest_write, .release = chrtest_release, 
}; 
/* 驱动入口函数 */ 
static int __init xxx_init(void) 
{ /* 入口函数具体内容 */ int retvalue = 0; /* 注册字符设备驱动 */ retvalue = register_chrdev(200, "chrtest", &test_fops); if(retvalue < 0){ /* 字符设备注册失败,自行处理 */ } return 0; 
} 
/* 驱动出口函数 */ 
static void __exit xxx_exit(void) 
{ /* 注销字符设备驱动 */ unregister_chrdev(200, "chrtest"); } /* 将上面两个函数指定为驱动的入口和出口函数 */ module_init(xxx_init); module_exit(xxx_exit); 

1.2.4 添加 LICENSE 和作者信息

在文章最后加入 LICENSE 信息和作者信息

LICENSE 是必须添加的,否则的话编译的时候会报错(脏内核)

作者信息可以添加也可以不添加

MODULE_LICENSE("GPL") //添加模块 LICENSE 信息 
MODULE_AUTHOR("zhaorming") //添加模块作者信息 

2、整体

static struct file_operations test_fops;
/* 打开设备 */ 
static int chrtest_open(struct inode *inode, struct file *filp) 
{ /* 用户实现具体功能 */ return 0; } /* 从设备读取 */ 
static ssize_t chrtest_read(struct file *filp, char __user *buf,  size_t cnt, loff_t *offt) 
{ /* 用户实现具体功能 */ return 0; } /* 向设备写数据 */ static ssize_t chrtest_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt) { /* 用户实现具体功能 */ return 0; } /* 关闭/释放设备 */ static int chrtest_release(struct inode *inode, struct file *filp) { /* 用户实现具体功能 */  return 0; } static struct file_operations test_fops = { .owner = THIS_MODULE,  .open = chrtest_open, .read = chrtest_read, .write = chrtest_write, .release = chrtest_release, 
}; 
/* 驱动入口函数 */ 
static int __init xxx_init(void) 
{ /* 入口函数具体内容 */ int retvalue = 0; /* 注册字符设备驱动 */ retvalue = register_chrdev(200, "chrtest", &test_fops); if(retvalue < 0){ /* 字符设备注册失败,自行处理 */ } return 0; 
} 
/* 驱动出口函数 */ 
static void __exit xxx_exit(void) 
{ /* 注销字符设备驱动 */ unregister_chrdev(200, "chrtest"); } /* 将上面两个函数指定为驱动的入口和出口函数 */ module_init(xxx_init); module_exit(xxx_exit); MODULE_LICENSE("GPL") //添加模块 LICENSE 信息 MODULE_AUTHOR("zhaorming") //添加模块作者信息 

这篇关于6ull驱动记录--字符设备驱动开发基本框架的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1121064

相关文章

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

mysql中insert into的基本用法和一些示例

《mysql中insertinto的基本用法和一些示例》INSERTINTO用于向MySQL表插入新行,支持单行/多行及部分列插入,下面给大家介绍mysql中insertinto的基本用法和一些示例... 目录基本语法插入单行数据插入多行数据插入部分列的数据插入默认值注意事项在mysql中,INSERT I

Python UV安装、升级、卸载详细步骤记录

《PythonUV安装、升级、卸载详细步骤记录》:本文主要介绍PythonUV安装、升级、卸载的详细步骤,uv是Astral推出的下一代Python包与项目管理器,主打单一可执行文件、极致性能... 目录安装检查升级设置自动补全卸载UV 命令总结 官方文档详见:https://docs.astral.sh/

mapstruct中的@Mapper注解的基本用法

《mapstruct中的@Mapper注解的基本用法》在MapStruct中,@Mapper注解是核心注解之一,用于标记一个接口或抽象类为MapStruct的映射器(Mapper),本文给大家介绍ma... 目录1. 基本用法2. 常用属性3. 高级用法4. 注意事项5. 总结6. 编译异常处理在MapSt

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

使用Python开发一个现代化屏幕取色器

《使用Python开发一个现代化屏幕取色器》在UI设计、网页开发等场景中,颜色拾取是高频需求,:本文主要介绍如何使用Python开发一个现代化屏幕取色器,有需要的小伙伴可以参考一下... 目录一、项目概述二、核心功能解析2.1 实时颜色追踪2.2 智能颜色显示三、效果展示四、实现步骤详解4.1 环境配置4.

MyBatis ResultMap 的基本用法示例详解

《MyBatisResultMap的基本用法示例详解》在MyBatis中,resultMap用于定义数据库查询结果到Java对象属性的映射关系,本文给大家介绍MyBatisResultMap的基本... 目录MyBATis 中的 resultMap1. resultMap 的基本语法2. 简单的 resul

统一返回JsonResult踩坑的记录

《统一返回JsonResult踩坑的记录》:本文主要介绍统一返回JsonResult踩坑的记录,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录统一返回jsonResult踩坑定义了一个统一返回类在使用时,JsonResult没有get/set方法时响应总结统一返回

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件