file_operations填充
1、open、read、write、release 参考
ssize_t miscread (struct file *file, char __user *buf, size_t size, loff_t *loff)
{
printk("enter miscread success \n");
return 0;
}
ssize_t miscwrite (struct file *file, const char __user *buf, size_t size, loff_t *loff)
{
printk("enter miscwrite success \n");
return 0;
}
int miscrelease (struct inode *inode, struct file *file)
{
printk("enter miscrelease success \n");
return 0;
}
int miscopen (struct inode *inode, struct file *file)
{
printk("enter miscopen success \n");
return 0;
}
const struct file_operations mymisc_fops={
.owner = THIS_MODULE,
.open = miscopen,
.read = miscread,
.write = miscwrite,
.release = miscrelease
};
2、应用层和内核层数据传递
应用层和内核层是不能直接进行数据传输,需要借助如下函数:
头文件:
#include <linux/uaccess.h>
函数
static inline long copy_from_user(void *to, const void __user * from, unsigned long n)
static inline long copy_to_user(void __user *to, const void *from, unsigned long n)
调用read时,相当于读内核,此时调用的函数为copy_to_user
调用write时,相当于写内核,此时调用的函数为copy_from_user
未完待续。。。。
备注:file_operation 源码:
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 (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
int (*readdir) (struct file *, void *, filldir_t);
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 (*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 **);
long (*fallocate)(struct file *file, int mode, loff_t offset,
loff_t len);
int (*show_fdinfo)(struct seq_file *m, struct file *f);
/* get_lower_file is for stackable file system */
struct file* (*get_lower_file)(struct file *f);
};