当前位置: 首页 > news >正文

3.用户程序与驱动交互

驱动程序请使用第二章https://blog.csdn.net/chenhequanlalala/article/details/140034424

用户app与驱动交互最常见的做法是insmod驱动后,生成一个设备节点,app通过open,read等系统调用去操作这个设备节点,这里先用mknode命令调试。

mknod 设备名 设备类型(b块设备/c字符设备/p管道) 主设备号 次设备号

mknod /dev/hello c 240 0

 使用mknode后生成了/dev/hello节点,写入数据到hello节点中,查看dmesg的输出发现调用了驱动的open write release

echo 1 > /dev/hello

[  802.771723] /home/book/nfs_rootfs/drivers_projects/01_hello_drv/hello_drv.c hello_open 48
[  802.773196] /home/book/nfs_rootfs/drivers_projects/01_hello_drv/hello_drv.c hello_write 40
[  802.773285] /home/book/nfs_rootfs/drivers_projects/01_hello_drv/hello_drv.c hello_release 56
 

 这里写一个C程序来读写这个设备节点

#include "linux/string.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>/*
读数据 ./hello_test xxx(设备节点名字)
写数据 ./hello_test xxx(设备节点名字) string
*/int main(int argc, char **argv)
{int fd;int len;char buf[1024];if(argc < 2){printf("Usage :\n");printf("%s <dev> [str]\n", argv[0]);return -1;}//openfd = open(argv[1], O_RDWR);if(fd < 0){printf("open %s failed\n", argv[1]);return -1;}//readif(argc == 2){len = read(fd, buf, sizeof(buf));printf("%s\n", buf);}//writeelse if(argc == 3){len = write(fd, argv[2], strlen(argv[2]));}else{printf("Too many parameters\n");}close(fd);
}

分别调用 ./hello_test /dev/hello 123 和 ./hello_test /dev/hello 后,查看dmesg输出显示

[ 2770.434595] /home/book/nfs_rootfs/drivers_projects/01_hello_drv/hello_drv.c hello_open 48
[ 2770.434664] /home/book/nfs_rootfs/drivers_projects/01_hello_drv/hello_drv.c hello_write 40
[ 2770.434705] /home/book/nfs_rootfs/drivers_projects/01_hello_drv/hello_drv.c hello_release 56
[ 2772.388372] /home/book/nfs_rootfs/drivers_projects/01_hello_drv/hello_drv.c hello_open 48
[ 2772.388439] /home/book/nfs_rootfs/drivers_projects/01_hello_drv/hello_drv.c hello_read 32
[ 2772.389257] /home/book/nfs_rootfs/drivers_projects/01_hello_drv/hello_drv.c hello_release 56

 app的open write read release都一一对应上了。

这里的./hello_test /dev/hello其实没有读到数据,因为驱动程序中并没有和app交互数据。

[root@100ask:/mnt/drivers_projects/01_hello_drv]# ./hello_test /dev/hello
read size = 1024 data = 
 

 app与驱动交换数据通过copy_from_user和copy_to_user来实现

copy_to_user(void __user *to, const void *from, unsigned long n);
copy_from_user(void *to, const void __user *from, unsigned long n);

在驱动程序中添加一个buf用来保存用户的数据,用copy_from_user保存数据,用copy_to_user读取数据。

#include "asm/uaccess.h"
#include "linux/scatterlist.h"
#include "linux/types.h"
#include <linux/mm.h>
#include <linux/miscdevice.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/mman.h>
#include <linux/random.h>
#include <linux/init.h>
#include <linux/raw.h>
#include <linux/tty.h>
#include <linux/capability.h>
#include <linux/ptrace.h>
#include <linux/device.h>
#include <linux/highmem.h>
#include <linux/backing-dev.h>
#include <linux/shmem_fs.h>
#include <linux/splice.h>
#include <linux/pfn.h>
#include <linux/export.h>
#include <linux/io.h>
#include <linux/uio.h>
#include <linux/module.h>#include <linux/uaccess.h>#define DEVICE_NAME "hello_device"
static int major;
#define hello_buf_size 100
static unsigned char hello_buf[hello_buf_size];//ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
//参数含义依次为 要读取的文件指针 用户保存数据的buf 读取数据大小 文件内容的偏移量
static ssize_t hello_read (struct file * filp, char __user *buf, size_t size, loff_t *offset)
{unsigned long len = size > hello_buf_size ? hello_buf_size : size;copy_to_user(buf, hello_buf, len);printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);return len;
}//ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
//参数含义依次为 要写入的文件指针 用户要写入的数据buf 写入数据大小 文件内容的偏移量
static ssize_t hello_write (struct file *filp, const char __user *buf, size_t size, loff_t *offset)
{unsigned long len = size > hello_buf_size ? hello_buf_size : size;copy_from_user(hello_buf, buf, len);printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);return len;
}//int (*open) (struct inode *, struct file *);
//参数含义依次为 文件索引节点 文件指针
static int hello_open (struct inode *node, struct file *filp)
{printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);return 0;
}//int (*release) (struct inode *, struct file *);
//参数含义依次为 文件索引节点 文件指针
static int hello_release (struct inode *node, struct file *filp)
{printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);return 0; 
}/*构建file_operations结构体*/
static const struct file_operations hello_fops = {.owner      = THIS_MODULE,.read		= hello_read,.write		= hello_write,.open		= hello_open,.release    = hello_release,
};/*init函数,实现register_chrdev*/
static int __init hello_init(void)
{//数含义依次为 主设备号,如果为0,内核会自动分配一个可用的。设备名,会在/proc/devices中显示。 file_operations结构体//注册成功就返回主设备号major = register_chrdev(0, DEVICE_NAME, &hello_fops);printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);return 0;
}/*exit函数unregister_chrdev*/
static void __exit hello_exit(void)
{//数含义依次为 主设备号 设备名unregister_chrdev(major, DEVICE_NAME);printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
}module_init(hello_init);
module_exit(hello_exit);
//遵循GPL协议
MODULE_LICENSE("GPL");

重新insmod驱动后,执行测试程序

[root@100ask:/mnt/drivers_projects/01_hello_drv]# ./hello_test /dev/hello 123456
[root@100ask:/mnt/drivers_projects/01_hello_drv]# ./hello_test /dev/hello       
read size = 100 data = 123456
[root@100ask:/mnt/drivers_projects/01_hello_drv]# ./hello_test /dev/hello hello
[root@100ask:/mnt/drivers_projects/01_hello_drv]# ./hello_test /dev/hello 
read size = 100 data = hello6
 

 这里第二次输出hello6,是因为copy_from_user只是写入指定长度的数据,第一次写入的123456只覆盖了前5字节。

相关文章:

3.用户程序与驱动交互

驱动程序请使用第二章https://blog.csdn.net/chenhequanlalala/article/details/140034424 用户app与驱动交互最常见的做法是insmod驱动后&#xff0c;生成一个设备节点&#xff0c;app通过open&#xff0c;read等系统调用去操作这个设备节点&#xff0c;这里先用mknode命令调…...

尽量不写一行if...elseif...写出高质量可持续迭代的项目代码

背景 无论是前端代码还是后端代码&#xff0c;都存在着定位困难&#xff0c;不好抽离&#xff0c;改造困难的问题&#xff0c;造成代码开发越来越慢&#xff0c;此外因为代码耦合较高&#xff0c;总是出现改了一处地方&#xff0c;然后影响其他地方&#xff0c;要么就是要修改…...

xcrun: error: unable to find utility “simctl“, not a developer tool or in PATH

目录 前言 一、问题详情 二、解决方案 1.确认Xcode已安装 2.安装Xcode命令行工具 3.指定正确的开发者目录 4. 确认命令行工具路径 5. 更新PATH环境变量 前言 今天使用cocoapods更新私有库的时候&#xff0c;遇到了"xcrun: error: unable to find utility &…...

【linux高级IO(一)】理解五种IO模型

&#x1f493;博主CSDN主页:杭电码农-NEO&#x1f493;   ⏩专栏分类:Linux从入门到精通⏪   &#x1f69a;代码仓库:NEO的学习日记&#x1f69a;   &#x1f339;关注我&#x1faf5;带你学更多操作系统知识   &#x1f51d;&#x1f51d; Linux高级IO 1. 前言2. 重谈对…...

前端引用vue/element/echarts资源等引用方法Blob下载HTML

前端引用下载vue/element/echarts资源等引用方法 功能需求 需求是在HTML页面中集成Vue.js、Element Plus&#xff08;Element UI的Vue 3版本&#xff09;、ECharts等前端资源&#xff0c;使用Blob下载HTML。 解决方案概述 直接访问线上CDN地址&#xff1a;简单直接&#xff0c…...

昇思MindSpore学习笔记2-01 LLM原理和实践 --基于 MindSpore 实现 BERT 对话情绪识别

摘要&#xff1a; 通过识别BERT对话情绪状态的实例&#xff0c;展现在昇思MindSpore AI框架中大语言模型的原理和实际使用方法、步骤。 一、环境配置 %%capture captured_output # 实验环境已经预装了mindspore2.2.14&#xff0c;如需更换mindspore版本&#xff0c;可更改下…...

uniapp实现图片懒加载 封装组件

想要的效果就是窗口滑动到哪里&#xff0c;哪里的图片进行展示 主要原理使用IntersectionObserver <template><view><image error"HandlerError" :style"imgStyle" :src"imageSrc" :id"randomId" :mode"mode&quo…...

持续交付:自动化测试与发布流程的变革

目录 前言1. 持续交付的概念1.1 持续交付的定义1.2 持续交付的核心原则 2. 持续交付的优势2.1 提高交付速度2.2 提高软件质量2.3 降低发布风险2.4 提高团队协作 3. 实施持续交付的步骤3.1 构建自动化测试体系3.1.1 单元测试3.1.2 集成测试3.1.3 功能测试3.1.4 性能测试 3.2 构建…...

VBA常用的字符串内置函数

前言 在VBA程序中&#xff0c;常用的内置函数可以按照功能分为字符串函数、数字函数、转换函数等等&#xff0c;本节主要会介绍常用的字符串的内置函数&#xff0c;包括Len()、Left()、Mid()、Right()、Split()、String()、StrConV()等。 本节的练习数据表以下表为例&#xff…...

大数据面试题之Spark(7)

目录 Spark实现wordcount Spark Streaming怎么实现数据持久化保存? Spark SQL读取文件&#xff0c;内存不够使用&#xff0c;如何处理? Spark的lazy体现在哪里? Spark中的并行度等于什么 Spark运行时并行度的设署 Spark SQL的数据倾斜 Spark的exactly-once Spark的…...

AI绘画 Stable Diffusion图像的脸部细节控制——采样器全解析

大家好&#xff0c;我是画画的小强 我们在运用AI绘画 Stable Diffusion 这一功能强大的AI绘图工具时&#xff0c;我们往往会发现自己对提示词的使用还不够充分。在这种情形下&#xff0c;我们应当如何调整自己的策略&#xff0c;以便更加精确、全面地塑造出理想的人物形象呢&a…...

liunx离线安装Firefox

在Linux系统中离线安装Firefox浏览器&#xff0c;您需要先从Mozilla的官方网站下载Firefox的安装包&#xff0c;然后通过终端进行安装。以下是详细的步骤&#xff1a; 准备工作 下载Firefox安装包&#xff1a; 首先&#xff0c;在一台可以上网的电脑上访问Firefox官方下载页面…...

UNet进行病理图像分割

数据集链接&#xff1a;https://pan.baidu.com/s/1IBe_P0AyHgZC39NqzOxZhA?pwdnztc 提取码&#xff1a;nztc UNet模型 import torch import torch.nn as nnclass conv_block(nn.Module):def __init__(self, ch_in, ch_out):super(conv_block, self).__init__()self.conv nn…...

初二数学基础差从哪开始补?附深度解析!

有时候&#xff0c;当你推不开一扇门的时候&#xff0c;不要着急&#xff0c;试着反方向拉一下&#xff0c;或者横向拉一下。下面是小偏整理的初二数学基础差从哪开始补2021年&#xff0c;感谢您的每一次阅读。   初二数学基础差从哪开始补2021年   第一个问题是很多同学都…...

【C语言】return 关键字

在C语言中&#xff0c;return是一个关键字&#xff0c;用于从函数中返回值或者结束函数的执行。它是函数的重要组成部分&#xff0c;负责将函数的计算结果返回给调用者&#xff0c;并可以提前终止函数的执行。 主要用途和原理&#xff1a; 返回值给调用者&#xff1a; 当函数执…...

华为机试HJ13句子逆序

华为机试HJ13句子逆序 题目&#xff1a; 将一个英文语句以单词为单位逆序排放。例如“I am a boy”&#xff0c;逆序排放后为“boy a am I”所有单词之间用一个空格隔开&#xff0c;语句中除了英文字母外&#xff0c;不再包含其他字符 想法&#xff1a; 将输入的字符串通过…...

代码随想录day40 动态规划(5)

52. 携带研究材料&#xff08;第七期模拟笔试&#xff09; (kamacoder.com) 完全背包&#xff0c;可重复放入物品&#xff0c;需要用一维滚动数组从前往后遍历。 由于第0个物品和后面物品的转移方程没有区别&#xff0c;可以不额外初始化dp数组&#xff0c;直接用元素全0的d…...

FFmpeg 命令行 音视频格式转换

&#x1f4da;&#xff1a;FFmpeg 提供了丰富的命令行选项和功能&#xff0c;可以用来处理音视频文件、流媒体等&#xff0c;掌握命令行的使用&#xff0c;可以有效提高工作效率。 目录 一、视频转换和格式转换 &#x1f535; 将视频文件转换为另一种格式 &#x1f535; 指定…...

Jmeter使用JSON Extractor提取多个变量

1.当正则不好使时&#xff0c;用json extractor 2.提取多个值时&#xff0c;默认值必填&#xff0c;否则读不到变量...

c++ 设计模式 的课本范例(下)

&#xff08;19&#xff09; 桥接模式 Bridge&#xff0c;不是采用类继承&#xff0c;而是采用类组合&#xff0c;一个类的数据成员是类对象&#xff0c;来扩展类的功能。源码如下&#xff1a; class OS // 操作系统负责绘图 { public:virtual ~OS() {}virtual void draw(cha…...

Linux链表操作全解析

Linux C语言链表深度解析与实战技巧 一、链表基础概念与内核链表优势1.1 为什么使用链表&#xff1f;1.2 Linux 内核链表与用户态链表的区别 二、内核链表结构与宏解析常用宏/函数 三、内核链表的优点四、用户态链表示例五、双向循环链表在内核中的实现优势5.1 插入效率5.2 安全…...

python打卡day49

知识点回顾&#xff1a; 通道注意力模块复习空间注意力模块CBAM的定义 作业&#xff1a;尝试对今天的模型检查参数数目&#xff0c;并用tensorboard查看训练过程 import torch import torch.nn as nn# 定义通道注意力 class ChannelAttention(nn.Module):def __init__(self,…...

微信小程序 - 手机震动

一、界面 <button type"primary" bindtap"shortVibrate">短震动</button> <button type"primary" bindtap"longVibrate">长震动</button> 二、js逻辑代码 注&#xff1a;文档 https://developers.weixin.qq…...

前端开发面试题总结-JavaScript篇(一)

文章目录 JavaScript高频问答一、作用域与闭包1.什么是闭包&#xff08;Closure&#xff09;&#xff1f;闭包有什么应用场景和潜在问题&#xff1f;2.解释 JavaScript 的作用域链&#xff08;Scope Chain&#xff09; 二、原型与继承3.原型链是什么&#xff1f;如何实现继承&a…...

CRMEB 框架中 PHP 上传扩展开发:涵盖本地上传及阿里云 OSS、腾讯云 COS、七牛云

目前已有本地上传、阿里云OSS上传、腾讯云COS上传、七牛云上传扩展 扩展入口文件 文件目录 crmeb\services\upload\Upload.php namespace crmeb\services\upload;use crmeb\basic\BaseManager; use think\facade\Config;/*** Class Upload* package crmeb\services\upload* …...

优选算法第十二讲:队列 + 宽搜 优先级队列

优选算法第十二讲&#xff1a;队列 宽搜 && 优先级队列 1.N叉树的层序遍历2.二叉树的锯齿型层序遍历3.二叉树最大宽度4.在每个树行中找最大值5.优先级队列 -- 最后一块石头的重量6.数据流中的第K大元素7.前K个高频单词8.数据流的中位数 1.N叉树的层序遍历 2.二叉树的锯…...

用机器学习破解新能源领域的“弃风”难题

音乐发烧友深有体会&#xff0c;玩音乐的本质就是玩电网。火电声音偏暖&#xff0c;水电偏冷&#xff0c;风电偏空旷。至于太阳能发的电&#xff0c;则略显朦胧和单薄。 不知你是否有感觉&#xff0c;近两年家里的音响声音越来越冷&#xff0c;听起来越来越单薄&#xff1f; —…...

#Uniapp篇:chrome调试unapp适配

chrome调试设备----使用Android模拟机开发调试移动端页面 Chrome://inspect/#devices MuMu模拟器Edge浏览器&#xff1a;Android原生APP嵌入的H5页面元素定位 chrome://inspect/#devices uniapp单位适配 根路径下 postcss.config.js 需要装这些插件 “postcss”: “^8.5.…...

如何更改默认 Crontab 编辑器 ?

在 Linux 领域中&#xff0c;crontab 是您可能经常遇到的一个术语。这个实用程序在类 unix 操作系统上可用&#xff0c;用于调度在预定义时间和间隔自动执行的任务。这对管理员和高级用户非常有益&#xff0c;允许他们自动执行各种系统任务。 编辑 Crontab 文件通常使用文本编…...

Android写一个捕获全局异常的工具类

项目开发和实际运行过程中难免会遇到异常发生&#xff0c;系统提供了一个可以捕获全局异常的工具Uncaughtexceptionhandler&#xff0c;它是Thread的子类&#xff08;就是package java.lang;里线程的Thread&#xff09;。本文将利用它将设备信息、报错信息以及错误的发生时间都…...