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

相关文章

一文详解Python如何开发游戏

《一文详解Python如何开发游戏》Python是一种非常流行的编程语言,也可以用来开发游戏模组,:本文主要介绍Python如何开发游戏的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、python简介二、Python 开发 2D 游戏的优劣势优势缺点三、Python 开发 3D

Redis 基本数据类型和使用详解

《Redis基本数据类型和使用详解》String是Redis最基本的数据类型,一个键对应一个值,它的功能十分强大,可以存储字符串、整数、浮点数等多种数据格式,本文给大家介绍Redis基本数据类型和... 目录一、Redis 入门介绍二、Redis 的五大基本数据类型2.1 String 类型2.2 Hash

基于Python开发Windows自动更新控制工具

《基于Python开发Windows自动更新控制工具》在当今数字化时代,操作系统更新已成为计算机维护的重要组成部分,本文介绍一款基于Python和PyQt5的Windows自动更新控制工具,有需要的可... 目录设计原理与技术实现系统架构概述数学建模工具界面完整代码实现技术深度分析多层级控制理论服务层控制注

Java Instrumentation从概念到基本用法详解

《JavaInstrumentation从概念到基本用法详解》JavaInstrumentation是java.lang.instrument包提供的API,允许开发者在类被JVM加载时对其进行修改... 目录一、什么是 Java Instrumentation主要用途二、核心概念1. Java Agent

Java中的分布式系统开发基于 Zookeeper 与 Dubbo 的应用案例解析

《Java中的分布式系统开发基于Zookeeper与Dubbo的应用案例解析》本文将通过实际案例,带你走进基于Zookeeper与Dubbo的分布式系统开发,本文通过实例代码给大家介绍的非常详... 目录Java 中的分布式系统开发基于 Zookeeper 与 Dubbo 的应用案例一、分布式系统中的挑战二

Java 缓存框架 Caffeine 应用场景解析

《Java缓存框架Caffeine应用场景解析》文章介绍Caffeine作为高性能Java本地缓存框架,基于W-TinyLFU算法,支持异步加载、灵活过期策略、内存安全机制及统计监控,重点解析其... 目录一、Caffeine 简介1. 框架概述1.1 Caffeine的核心优势二、Caffeine 基础2

docker编写java的jar完整步骤记录

《docker编写java的jar完整步骤记录》在平常的开发工作中,我们经常需要部署项目,开发测试完成后,最关键的一步就是部署,:本文主要介绍docker编写java的jar的相关资料,文中通过代... 目录all-docker/生成Docker打包部署文件配置服务A的Dockerfile (a/Docke

基于Go语言开发一个 IP 归属地查询接口工具

《基于Go语言开发一个IP归属地查询接口工具》在日常开发中,IP地址归属地查询是一个常见需求,本文将带大家使用Go语言快速开发一个IP归属地查询接口服务,有需要的小伙伴可以了解下... 目录功能目标技术栈项目结构核心代码(main.go)使用方法扩展功能总结在日常开发中,IP 地址归属地查询是一个常见需求:

录音功能在哪里? 电脑手机等设备打开录音功能的技巧

《录音功能在哪里?电脑手机等设备打开录音功能的技巧》很多时候我们需要使用录音功能,电脑和手机这些常用设备怎么使用录音功能呢?下面我们就来看看详细的教程... 我们在会议讨论、采访记录、课堂学习、灵感创作、法律取证、重要对话时,都可能有录音需求,便于留存关键信息。下面分享一下如何在电脑端和手机端上找到录音功能

Kotlin 协程之Channel的概念和基本使用详解

《Kotlin协程之Channel的概念和基本使用详解》文章介绍协程在复杂场景中使用Channel进行数据传递与控制,涵盖创建参数、缓冲策略、操作方式及异常处理,适用于持续数据流、多协程协作等,需注... 目录前言launch / async 适合的场景Channel 的概念和基本使用概念Channel 的