linux kernel --- sysfs文件系统

sysfs是Linux内核中一种内存文件系统,用于实现内核空间与用户空间的交互。本文主要介绍了sysfs的几个关键结构体,如struct attribute、struct device_attribute和struct attribute_group,以及sysfs_create_file和sysfs_create_groups()函数的使用。通过DEVICE_ATTR宏创建设备属性,并通过show和store函数实现属性文件的读写操作。在设备、总线和驱动创建sysfs节点的示例中,详细阐述了如何在/sys目录下创建并操作设备属性。
摘要由CSDN通过智能技术生成

sysfs 作为内核中的一种内存文件系统,现阶段很多子系统、设备驱动程序都使用sysfs实现内核空间和用户空间的交互。其挂载在/sys下。用户通过对设备节点的操作,进入到kernel空间,通过属性文件的读写操作(show store),操作硬件。

在内核中,sysfs的属性一般都是由_ATTR系列的宏来实现。对设备的使用 DEVICE_ATTR ,对总线使用 BUS_ATTR ,对驱动使用 DRIVER_ATTR ,对类别(class)使用 CLASS_ATTR。

以下源码分析是在kernel4.14上 。

一、 几个结构体

sysfs_create_file 里的传参是 struct attribute 结构体
sysfs_create_group 里的传参是 struct attribute_group 结构体

struct attribute {
   
	const char		*name;
	umode_t			mode;
 	#ifdef CONFIG_DEBUG_LOCK_ALLOC
	bool			ignore_lockdep:1;
	struct lock_class_key	*key;
	struct lock_class_key	skey;
 	#endif
};
struct attribute_group {
   
	const char		*name;
	umode_t			(*is_visible)(struct kobject *, struct attribute *, int);
	umode_t			(*is_bin_visible)(struct kobject *,  struct bin_attribute *, int);
	struct attribute	**attrs;
	struct bin_attribute	**bin_attrs;
};

struct attribute 和 struct attribute_group 区别:attribute_group 里可以有好几组 attribute 。

struct device_attribute {
   
	struct attribute	attr;
	ssize_t (*show)(struct device *dev, struct device_attribute *attr,
			char *buf);
	ssize_t (*store)(struct device *dev, struct device_attribute *attr,
			 const char *buf, size_t count);
};

struct device_attribute 是自定义decice属性结构,读写该属性的方法 show 和 store 会和自定义的属性方法关联起来。

二、 sysfs_create_file

static inline int __must_check sysfs_create_file(struct kobject *kobj,
  						 const struct attribute *attr)</
  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值