1、为什么要加
我们在编写驱动的时候,尤其是字符设备驱动,往往给这个驱动编写一些应用程序,我们最常用的两个就是read和write。
在Linux里面一切皆文件,所以我们可以通过read或write这个文件来操作这个驱动
read
fd:对应VFS层的file;当前进程打开一个文件以后,这个fd就会唯一的分配给它,如果再重新打开一次的话,fd也会不同。后续如果我们要操作这个文件,我们只需要填充整个fd即可,内核会根据这个fd找到这个file
buf:读取数据存放的缓冲区;一定是用户空间的一个地址
count:缓冲区大小;驱动就不会多发送一些字节给你
返回值:实际读取字节的个数
write
fd:对应VFS层的file;当前进程打开一个文件以后,这个fd就会唯一的分配给它,如果再重新打开一次的话,fd也会不同。后续如果我们要操作这个文件,我们只需要填充整个fd即可,内核会根据这个fd找到这个file
buf:要写入的数据存放的缓冲区
count:缓冲区大小;
返回值:实际写入字节的个数
2、详解
那我们内核里面是哪两个接口函数与其对应呢?
copy_to_user的原型:
copy_from_usr原型:
3、示例代码
驱动:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
static int major = 237;
static int minor = 0;
static dev_t devno;
static struct cdev cdev;
struct device *class_dev = NULL;
struct class *cls;
static int hello_open (struct inode *inode, struct file *filep)
{
printk("hello_open()\n");
return 0;
}
static int hello_release (struct inode *inode, struct file *filep)
{
printk("hello_release()\n");
return 0;
}
#define KMAX_LEN 32
char kbuf[KMAX_LEN+1] = "kernel";
//read(fd,buff,40);
static ssize_t hello_read (struct file *filep, char __user *buf, size_t size, loff_t *pos)
{
int error;
if(size > strlen(kbuf))
{
size = strlen(kbuf);
}
if(copy_to_user(buf,kbuf, size))
{
error = -EFAULT;
return error;
}
return size;
}
//write(fd,buff,40);
static ssize_t hello_write (struct file *filep, const char __user *buf, size_t size, loff_t *pos)
{
int error;
if(size > KMAX_LEN)
{
size = KMAX_LEN;
}
memset(kbuf,0,sizeof(kbuf));
if(copy_from_use(kbuf, buf, size))
{
error = -EFAULT;
return error;
}
printk("%s\n",kbuf);
return size;
}
static struct file_operations hello_ops =
{
.open = hello_open,
.release = hello_release,
.read = hello_read,
.write = hello_write,
};
static int hello_init(void)
{
int result;
int error;
printk("hello_init \n");
result = register_chrdev( major, "hello", &hello_ops);
if(result < 0)
{
printk("register_chrdev fail \n");
return result;
}
cls = class_create(THIS_MODULE, "hellocls");
if (IS_ERR(cls)) {
printk(KERN_ERR "class_create() failed for cls\n");
result = PTR_ERR(cls);
goto out_err_1;
}
devno = MKDEV(major, minor);
class_dev = device_create(cls, NULL, devno, NULL, "hellodev");
if (IS_ERR(class_dev)) {
result = PTR_ERR(class_dev);
goto out_err_2;
}
return 0;
out_err_2:
class_destroy(cls);
out_err_1:
unregister_chrdev(major,"hello");
return result;
}
static void hello_exit(void)
{
printk("hello_exit \n");
device_destroy(cls, devno);
class_destroy(cls);
unregister_chrdev(major,"hello");
return;
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
//proc/devices
应用程序:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
main()
{
int fd;
int len;
char buf[64]={0};
char buf2[64+1]="peng";
fd = open("/dev/hellodev",O_RDWR);
if(fd<0)
{
perror("open fail \n");
return;
}
len = read(fd,buf,64);
buf[len]='\0';
printf("read:%s len = %d\n",buf,len);
len = write(fd,buf2,strlen(buf2));
printf("len = %d\n",len);
close(fd);
}