linux驱动文件私有数据(字符设备基础二)

编程入门 行业动态 更新时间:2024-10-28 06:27:26

linux驱动文件私有数据(<a href=https://www.elefans.com/category/jswz/34/1771063.html style=字符设备基础二)"/>

linux驱动文件私有数据(字符设备基础二)

  编写linux驱动程序时,通常在驱动开发中会为设备定义相关的设备结构体,将硬件属性的描述信息全部放在该结构体中
  Linux 中并没有明确规定要使用文件私有数据,但是在 linux 驱动源码中,广泛使用了文件私有数据,这是 Linux 驱动遵循的“潜规则”,实际上也体现了Linux 面向对象的思想。structfile 结构体中专门为用户留了一个域用于定义私有数据。结构体内容如下所示:

struct file {union {struct llist_node	fu_llist;struct rcu_head 	fu_rcuhead;} f_u;struct path		f_path;struct inode		*f_inode;	/* cached value */const struct file_operations	*f_op;/** Protects f_ep_links, f_flags.* Must not be taken from IRQ context.*/spinlock_t		f_lock;enum rw_hint		f_write_hint;atomic_long_t		f_count;unsigned int 		f_flags;fmode_t			f_mode;struct mutex		f_pos_lock;loff_t			f_pos;struct fown_struct	f_owner;const struct cred	*f_cred;struct file_ra_state	f_ra;u64			f_version;
#ifdef CONFIG_SECURITYvoid			*f_security;
#endif/* needed for tty driver, and maybe others */void			*private_data;#ifdef CONFIG_EPOLL/* Used by fs/eventpoll.c to link all the hooks to this file */struct list_head	f_ep_links;struct list_head	f_tfile_llink;
#endif /* #ifdef CONFIG_EPOLL */struct address_space	*f_mapping;errseq_t		f_wb_err;
}

  文件私有数据的概念在 Linux 驱动中有着非常广泛的应用,文件私有数据就是将私有数据private_data 指向设备结构体。通过它可以将私有数据一路从open 函数带到read, write函数层层传入。一般是在 open 的时候赋值,read、write 时使用。open 函数中私有数据的使用如下所示

struct device_test dev1;
static int cdev_test_open(struct inode *inode,struct file *file)
{file->private_data=&dev1;return 0;
};

  在上述代码中,定义了一个设备结构体 dev1,然后在 open 函数中,将私有数据private_data指向了设备结构体 dev1。我们可以在 read write 函数中通过 private_data 访问设备结构体,如下所示:

static ssize_t cdev_test_write(struct file *file,const char _user *buf, size_t size,loff_t *off_t)
{struct device_test *test_dev=(struct device_test *)file->private_data;return 0;
}

更多推荐

linux驱动文件私有数据(字符设备基础二)

本文发布于:2023-12-03 21:05:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1657638.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符   文件   基础   数据   设备

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!