【Linux】【驱动】应用层和驱动层传输数据

【Linux】【驱动】应用层和驱动层传输数据

  • 绪论
    • [1.如果我在应用层使用系统0 对设备节点进行打开,关闭,读写等操作会发生什么呢?](#1.如果我在应用层使用系统0 对设备节点进行打开,关闭,读写等操作会发生什么呢?)
  • [2 我们的应用层和内核层是不能直接进行数据传输的](#2 我们的应用层和内核层是不能直接进行数据传输的)
  • [3 驱动部分的代码](#3 驱动部分的代码)
  • [4 应用代码](#4 应用代码)
  • [5 编译以及运行代码](#5 编译以及运行代码)

绪论

Linux一切皆文件!

文件对应的操作有打开,关闭,读写设备节点对应的操作有打开,关闭,读写

1.如果我在应用层使用系统0 对设备节点进行打开,关闭,读写等操作会发生什么呢?

当我们在应用层 read 设备节点的时候,就会触发我们驱动里面read 这个函数

c 复制代码
ssize t (*read) (struct file *, char  user *, size t, loff t *);

当我们在应用层 write 设备节点的时候,就会触发我们驱动里面 write 这个函数

c 复制代码
ssize t (*write) (struct file *, const char  user *, size t, loff t *);
c 复制代码
unsigned int (*poll) (struct file *, struct poll table struct *);
c 复制代码
long (*unlocked ioctl) (struct file *, unsigned int, unsigned long);
c 复制代码
int (*open) (struct inode *, struct file *);
c 复制代码
int (*release) (struct inode *, struct file *);

2 我们的应用层和内核层是不能直接进行数据传输的

如下两个代码实现了数据的交互

c 复制代码
static inline long copy from user(void *to, const void user * from, unsigned long n)
static inline long copy to user(voiduser *to, const void *from, unsigned long n)

3 驱动部分的代码

c 复制代码
#include <linux/init.h>
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>	
#include <linux/uaccess.h>


int misc_open(struct inode *inode, struct file *file)
{
	printk("misc_open\n");

	return 0;
}

int misc_release(struct inode *inode, struct file *file)
{
	printk("misc_release\n");
	return 0;

}

ssize_t misc_read(struct file *file,char __user *ubuf,size_t size,loff_t *loff_t)
{
	char kbuf[512] = "haha";

	if(copy_to_user(ubuf,kbuf,strlen(kbuf))!=0)
	{
		printk("error copying\n");
		return -1;
	}

	return 0;

}

ssize_t misc_write(struct file *file,const char __user *ubuf,size_t size,loff_t *loff_t)
{

	char kbuf[512] = {0};	
	if(copy_from_user(kbuf,ubuf,size)!= 0)
	{
		printk("misc_write error\n");
		return -1;

	}
	printk("kbuf = %s\n",kbuf);
	return 0;

}

struct file_operations misc_fops = {
	.owner	= THIS_MODULE,
	.open	= misc_open,
	.release = misc_release,
	.read	= misc_read,
	.write	= misc_write
};


struct miscdevice misc_dev = 
{
	.minor = MISC_DYNAMIC_MINOR,
	.name = "hello_misc",
	.fops = &misc_fops
};


//drivers for init
static int misc_init(void)
{
	int ret = 0;
	ret = misc_register(&misc_dev);
	if(ret<0) 
	{
		printk("misc_register is failed\n");
		return -1;
	}
	printk("misc registe is succeed \n");
	return 0;
}


//drivers for exit 
static void misc_exit(void)
{
//
	misc_deregister(&misc_dev);

	printk("misc exit \n");

}

module_init(misc_init);
module_exit(misc_exit);

MODULE_LICENSE("GPL");

4 应用代码

write就是实现了write的功能

read 函数就是实现了read的功能

c 复制代码
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"


/*
 * @description		: main主程序
 * @param - argc 	: argv数组元素个数
 * @param - argv 	: 具体参数
 * @return 			: 0 成功;其他 失败
 */
int main(int argc, char *argv[])
{
	int fd;
	char buf[64] = "12345";

	//fd = open(argv[1], O_RDONLY);
	fd = open("/dev/hello_misc", O_RDWR);

	if(fd < 0)
	{
		perror("open error");
		return fd;

	}
	//write(fd,buf,sizeof(buf));
	//printf("buf is %s\n",buf);
	write(fd,buf,sizeof(buf));
	close(fd);
	return 0;
}

5 编译以及运行代码

编译app代码

Bash 复制代码
arm-buildroot-linux-gnueabihf-gcc -o miscApp miscApp.c

挂载nfs盘

bash 复制代码
mount -t nfs -o nolock,vers=3 192.168.5.15:/home/book/nfs_rootfs /mnt

移动到mnt目录

bash 复制代码
cd /mnt

删除文件

bash 复制代码
rm -f + chrdevbase.ko

移动代码

bash 复制代码
cp miscApp /home/book/nfs_rootfs/

允许printk输出

bash 复制代码
echo "7 4 1 7"> /proc/sys/kernel/printk

安装驱动

bash 复制代码
insmod chrdevbase.ko

列出驱动

bash 复制代码
lsmod

删除驱动

bash 复制代码
remmod

运行代码

bash 复制代码
./miscApp
相关推荐
少妇的美梦14 小时前
logstash教程
运维
chen94515 小时前
k8s集群部署vector日志采集器
运维
chen94515 小时前
aws ec2部署harbor,使用s3存储
运维
轻松Ai享生活19 小时前
5 节课深入学习Linux Cgroups
linux
christine-rr19 小时前
linux常用命令(4)——压缩命令
linux·服务器·redis
三坛海会大神55520 小时前
LVS与Keepalived详解(二)LVS负载均衡实现实操
linux·负载均衡·lvs
東雪蓮☆20 小时前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs
qq_2642208920 小时前
LVS负载均衡群集和LVS+Keepalived群集
运维·负载均衡·lvs
乌萨奇也要立志学C++20 小时前
【Linux】进程概念(二):进程查看与 fork 初探
linux·运维·服务器
雨落Liy20 小时前
Nginx 从入门到进阶:反向代理、负载均衡与高性能实战指南
运维·nginx·负载均衡