当前位置: 首页 > news >正文

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

目录

      • struct device
      • sysfs 相关API
        • 使用默认目录
        • 自定义目录
        • 创建和销毁 sysfs 节点
        • attribute 初始化宏
        • 属性的读写函数
      • device 类型中sysfs 接口
      • 参考 demo

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_ALLOCbool			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、storestatic 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,  // 假设已定义 barNULL,                // 必须以 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/kernelkobj_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);}
}

相关文章:

  • MySQL进阶之索引(1)索引结构分类语法和SQL性能分析
  • xcode中project.pbxproj点开为空白问题
  • Windows Server 2019--12 活动目录(Active Directory,AD)
  • Linux 系统可视化管理工具
  • 微机原理与接口技术,期末冲刺复习资料(六)
  • 【无标题】NP完全问题的拓扑对偶统一解法 ——四色问题到P=NP的普适框架
  • Contos7yum停服
  • spider分享--图片
  • 时间同步技术在电力系统中的应用二
  • 讲一件Java虚拟线程
  • 英一真题阅读单词笔记 09年
  • 第三章支线六 ·数据幻域 · 状态管理与数据流
  • 面向异构系统的多面体编译优化关键技术研究——李颖颖博士
  • 2025武汉考研形势分析,趋势、挑战与应对策略
  • 【实习总结】C++ 通过pugi::xml库对xml文件进行操作
  • Spring Boot + MyBatis Plus 项目中,entity和 XML 映射文件的查找机制
  • CSS“多列布局”
  • 从代码学习深度强学习 - Dyna-Q 算法 PyTorch版
  • SnapViewer:解决PyTorch官方内存工具卡死问题,实现高效可视化
  • 一站式了解单例模式
  • 室内设计公司网站设计/站长工具中文
  • 吕梁网站建设kuyiso/友情链接源码
  • 哪家做网站做的好/友情链接网站
  • 网站建设项目管理/郑州seo外包服务
  • 哪个网站做首饰批发好/百度网盘app官网下载
  • 苹果自带建设网站/成都电脑培训班零基础