2025/04/15

[Linux] Macro

#define DEVICE_ATTR_RW(_name) \
	struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
#define __ATTR_RW(_name) __ATTR(_name, 0644, _name##_show, _name##_store) 
#define __ATTR(_name, _mode, _show, _store) {				\
	.attr = {.name = __stringify(_name),				\
		 .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },		\
	.show	= _show,						\
	.store	= _store,						\
}
  
#define ATTRIBUTE_GROUPS(_name)					\
static const struct attribute_group _name##_group = {		\
	.attrs = _name##_attrs,					\
}; 
struct attribute_group {
	const char		*name;
	umode_t			(*is_visible)(struct kobject *,
					      struct attribute *, int);
	umode_t			(*is_bin_visible)(struct kobject *,
						  const struct bin_attribute *, int);
	size_t			(*bin_size)(struct kobject *,
					    const struct bin_attribute *,
					    int);
	struct attribute	**attrs;
	union {
		struct bin_attribute		**bin_attrs;
		const struct bin_attribute	*const *bin_attrs_new;
	};
};
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
};
    
CLASS_ATTR_RW(xxx);  <<-- xxx代表你想要的节点名字,可以任意字符内容
||
||
\/
struct class_attribute class_attr_xxx = <<--xxx是宏定义的括号里的内容
{
.attr = {
.name = "xxx", <<--这里就是宏定义的括号里的内容,你想要的节点名字
.mode = VERIFY_OCTAL_PERMISSIONS((S_IWUSR | S_IRUGO))
},
.show = xxx_show, <<--xxx也是宏定义括号里的内容,根据你括号里的内容而变
.store = xxx_store <<--xxx也是宏定义括号里的内容,根据你括号里的内容而变
}; 

沒有留言: