Linux 内核学习(10) --- Linux sysfs 节点创建

struct device

系统中的任一设备在设备模型中都由一个 device 对象描述,其对应的数据结构 struct device

struct device {
	struct kobject kobj;
	struct device		*parent;
	struct device_private	*p;
	const char		*init_name; /* initial name of the device */
	const struct device_type *type;
	struct bus_type	*bus;		/* type of bus device is on */
	struct device_driver *driver;	/* which driver has allocated this device */
	void		*platform_data;	/* Platform specific data, device core doesn't touch it */
	void		*driver_data;	/* Driver data, set and get with dev_set_drvdata/dev_get_drvdata */
	....
	struct dev_links_info	links;
	struct dev_pm_info	power;
	struct dev_pm_domain	*pm_domain;
	const struct dma_map_ops *dma_ops;
	u64		*dma_mask;	/* dma mask (if dma'able device) */
	u64		coherent_dma_mask;
	u64		bus_dma_mask;	/* upstream dma_mask constraint */
	unsigned long	dma_pfn_offset;
	struct device_dma_parameters *dma_parms;
	......
};

device 内嵌一个kobject 对象,用于实现引用计数的管理并且通过它实现 sysfs 的层级结构
driver 域指向管理该设备的驱动程序对象,driver_data 是提供给驱动程序的数据
bus 域描述设备连接的总线类型

内核提供了相应的函数,操作 device 对象:
device_reigster 函数将一个新的 device 对象插入设备模型,并且自动在 /sys/devices 下创建一个新的目录
device_unregister 完成相反的操作
device_reigster 中最终是调用 kobject_add 这些底层函数,将 device 中包含的 kobject 注册到整个 kobject 的层级结构中

sysfs 相关API

使用默认目录

sysfs 默认挂载在 /sys/ 目录下,对于内核态是 kobject,对于用户态,映射在总线(bus) 对应的目录下,一般的设备驱动,我们使用的是 platform 虚拟总线,因此会映射在 /sys/platform/device/xxx
设备驱动创建时,就会创建该目录,因此不需要手动创建
bus/devices 下创建文件:

static inline int sysfs_create_files(struct kobject *kobj, const struct attribute * const *attr)
自定义目录

/sys 目录下创建自定义目录,创建目录使用下面的接口:

struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
{
	struct kobject *kobj;
	int retval;

	kobj = kobject_create();
	if (!kobj)
		return NULL;

	retval = kobject_add(kobj, parent, "%s", name);
	if (retval) {
		pr_warn("%s: kobject_add error: %d\n", __func__, retval);
		kobject_put(kobj);
		kobj = NULL;
	}
	return kobj;
}

name 表示目录名称
parent 表示父目录,NULL 为默认在 /sys 目录下创建

创建和销毁 sysfs 节点

内核 kobject 中的 attribute 映射到用户态就是文件,attribute 分为下面三类结构:

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 bin_attribute {
	struct attribute	attr;
	size_t			size;
	void			*private;
	ssize_t (*read)(struct file *, struct kobject *, struct bin_attribute *,
		char *, loff_t, size_t);
	ssize_t (*write)(struct file *, struct kobject *, struct bin_attribute *,
		 char *, loff_t, size_t);
	int (*mmap)(struct file *, struct kobject *, struct bin_attribute *attr,
		struct vm_area_struct *vma);
};
  • struct attribute :属性的通用结构,其他部分在使用时可以将 struct attribute 嵌入大到更大的属性结构中
  • struct bin_attribute:为二进制属性专门设计,在 sysfs 中表现为二进制文件,大多数设备参数的映射
  • struct attribute_group:提供一组属性的集合,集中管理 attributebin_attribute

sysfs 提供了下面的接口,将属性关联到对应的 kobject 上:

static inline int sysfs_create_files(struct kobject *kobj, const struct attribute * const *attr)
static inline int sysfs_create_file_ns(struct kobject *kobj, const struct attribute *attr, const void *ns)
int __must_check sysfs_create_bin_file(struct kobject *kobj, const struct bin_attribute *attr);
int __must_check sysfs_create_group(struct kobject *kobj, const struct attribute_group *grp);

struct attribute 包含了一个 mode 属性,即文件的访问权限,可读,可写,可执行,可以用已经定义的宏表示
(S_IWUSR | S_IRUGO),也可以用数字表示(0600)
宏的含义如下
S_IRUSR:用户读权限
S_IWUSR:用户写权限
S_IRGRP:用户组读权限
S_IWGRP:用户组写权限
S_IROTH:其他组都权限
S_IWOTH:其他组写权限

attribute 初始化宏

Linux 内核提供了一组宏定义来初始化上面的 struct attibute 结构

#define __ATTR(_name, _mode, _show, _store) {				\
	.attr = {.name = __stringify(_name),				\
		 .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },		\
	.show	= _show,						\
	.store	= _store,						\
}

#define __ATTR_PREALLOC(_name, _mode, _show, _store) {			\
	.attr = {.name = __stringify(_name),				\
		 .mode = SYSFS_PREALLOC | VERIFY_OCTAL_PERMISSIONS(_mode) },\
	.show	= _show,						\
	.store	= _store,						\
}

#define __ATTR_RO(_name) {						\
	.attr	= { .name = __stringify(_name), .mode = 0444 },		\
	.show	= _name##_show,						\
}

#define __ATTR_RO_MODE(_name, _mode) {					\
	.attr	= { .name = __stringify(_name),				\
		    .mode = VERIFY_OCTAL_PERMISSIONS(_mode) },		\
	.show	= _name##_show,						\
}

#define __ATTR_WO(_name) {						\
	.attr	= { .name = __stringify(_name), .mode = 0200 },		\
	.store	= _name##_store,					\
}
#define __ATTR_RW(_name) __ATTR(_name, 0644, _name##_show, _name##_store)
#define __ATTR_NULL { .attr = { .name = NULL } }
属性的读写函数
struct kobj_attribute {
	struct attribute attr;
	ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
			char *buf);
	ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
			 const char *buf, size_t count);
};
  • show 函数在读操作时被调用,它会拷贝 attr 提供的属性值到 buffer 指定的缓冲区中,缓冲区的大小为 PAGE_SIZE 字节,如果读取成功,则返回实际写入 buffer 的字节数,如果失败,返回负的错误码
  • store 函数在写操作时被调用,它会从 buffer 中读取 size 大小的字节,并将其保存在 attr 所表示的属性结构体变量中,如果成功,则将返回实际从 buffer 中读取的字节数,如果失败,返回负的错误码

device 类型中sysfs 接口

/* interface for exporting device attributes */
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);
};

#define DEVICE_ATTR(_name, _mode, _show, _store) \
	struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
#define DEVICE_ATTR_PREALLOC(_name, _mode, _show, _store) \
	struct device_attribute dev_attr_##_name = \
		__ATTR_PREALLOC(_name, _mode, _show, _store)
#define DEVICE_ATTR_RW(_name) \
	struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
#define DEVICE_ATTR_RO(_name) \
	struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
#define DEVICE_ATTR_WO(_name) \
	struct device_attribute dev_attr_##_name = __ATTR_WO(_name)

extern int device_create_file(struct device *device, const struct device_attribute *entry);
extern void device_remove_file(struct device *dev, const struct device_attribute *attr);

struct device 结构中包含了 kobject 对象,相应的 include/linux/device.h 包含了 struct device_attribute 结构体,使用 device_create_file 接口,就可以向 /sys/platform/device 目录下添加对应的文件

DEVICE_ATTR 开头的宏是对 __ATTR 宏的进一步封装
可以看到,DEVICE_ATTR 宏将结构体定义功能也实现了,直接定义了 dev_attr_##_name 为名称的 device_attribute 类型,
使用,使用 device_create_file 创建 sysfs demo 如下:

static ssize_t show_foo(struct device *dev, struct device_attribute *attr, char *buf) {
    return sprintf(buf, "foo value: 42\n");
}

static ssize_t store_foo(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) {
    int value;
    if (kstrtoint(buf, 10, &value) != 0)
        return -EINVAL;
    printk(KERN_INFO "Set foo to %d\n", value);
    return count;
}

// 实际是定义了 dev_attr_foo 类型为 struct device_attribute
static DEVICE_ATTR(foo, 0644, show_foo, store_foo);  // 名称、权限、show、store

static ssize_t bar_show(struct device *dev, struct device_attribute *attr, char *buf) {
    return sprintf(buf, "bar value: 42\n");
}
// 实际是定义了 dev_attr_bar 类型为 struct device_attribute
static DEVICE_ATTR_RO(bar);

static struct attribute *demo_attrs[] = {
    &dev_attr_foo.attr,  // 注意这里是 .attr
    &dev_attr_bar.attr,  // 假设已定义 bar
    NULL,                // 必须以 NULL 结束
};

static struct attribute_group my_attr_group = {
    .name = "demo_group",  // 可选:在 sysfs 中创建子目录
    .attrs = demo_attrs,   // struct attribute* 类型的数组
};

static int mdrv_probe(struct platform_device *pdev) {
    ret = sysfs_create_group(&pdev->dev.kobj, &my_attr_group);
    if (ret) {
        dev_err(&pdev->dev, "Failed to create sysfs group\n");
        return ret;
    }
    return 0;
}

参考 demo

补充一个使用标志 sysfs 接口 API 的使用例子:

struct kobject *kobj_ref;
volatile int sysfs_store_value = 20;

static ssize_t sysfs_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) {
    return sprintf(buf, "%d", sysfs_store_value);
}

static ssize_t sysfs_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) {
    sscanf(buf, "%d", &sysfs_store_value);
    return count;
}

//struct kobj_attribute demo_attr = __ATTR(sysfs_store_value, 0660, sysfs_show, sysfs_store);
struct kobj_attribute demo_attr = __ATTR_RO(sysfs);
//struct kobj_attribute demo_attr = __ATTR_WO(sysfs);
//struct kobj_attribute demo_attr = __ATTR_RW(sysfs);

static int __init demo_mdrv_init(void) {
	// create sysfs node /from /sys/kernel
	kobj_ref = kobject_create_and_add("mdrv_sysfs", kernel_kobj);
	ret = sysfs_create_file(kobj_ref, &demo_attr.attr);
	if(ret) {
	    printk(KERN_ERR "sysfs create file failed.\n");
	    kobject_put(kobj_ref);
	    sysfs_remove_file(kernel_kobj, &demo_attr.attr);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值