绪论
Linux一切皆文件!
文件对应的操作有打开,关闭,读写设备节点对应的操作有打开,关闭,读写
1.如果我在应用层使用系统0 对设备节点进行打开,关闭,读写等操作会发生什么呢?
当我们在应用层 read 设备节点的时候,就会触发我们驱动里面read 这个函数
ssize t (*read) (struct file *, char user *, size t, loff t *);
当我们在应用层 write 设备节点的时候,就会触发我们驱动里面 write 这个函数
ssize t (*write) (struct file *, const char user *, size t, loff t *);
unsigned int (*poll) (struct file *, struct poll table struct *);
long (*unlocked ioctl) (struct file *, unsigned int, unsigned long);
int (*open) (struct inode *, struct file *);
int (*release) (struct inode *, struct file *);
2 我们的应用层和内核层是不能直接进行数据传输的
如下两个代码实现了数据的交互
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 驱动部分的代码
#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的功能
#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代码
arm-buildroot-linux-gnueabihf-gcc -o miscApp miscApp.c
挂载nfs盘
mount -t nfs -o nolock,vers=3 192.168.5.15:/home/book/nfs_rootfs /mnt
移动到mnt目录
cd /mnt
删除文件
rm -f + chrdevbase.ko
移动代码
cp miscApp /home/book/nfs_rootfs/
允许printk输出
echo "7 4 1 7"> /proc/sys/kernel/printk
安装驱动
insmod chrdevbase.ko
列出驱动
lsmod
删除驱动
rmmod
运行代码文章来源:https://www.toymoban.com/news/detail-652990.html
./miscApp
文章来源地址https://www.toymoban.com/news/detail-652990.html
到了这里,关于【Linux】【驱动】应用层和驱动层传输数据的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!