SYSFS中属性文件的建立

1、device中建立属性文件

(1)函数调用关系:

/**************************************************************/

device_create_file

    sysfs_create_file

/*************************************************************/

(2)相关的数据结构:

 

1 struct attribute {
2     const char        *name;           //  属性文件的名字 
3     struct module        *owner;       //  属性文件的所有者
4     mode_t            mode;
5 #ifdef CONFIG_DEBUG_LOCK_ALLOC
6     struct lock_class_key    *key;
7     struct lock_class_key    skey;
8 #endif
9 };

 

 

 

1 struct device_attribute {
2     struct attribute    attr;                                //  内置的attribute 结构体
3     ssize_t (*show)(struct device *dev, struct device_attribute *attr,   //  属性文件的show方法(也就是读)
4             char *buf);
5     ssize_t (*store)(struct device *dev, struct device_attribute *attr,  //   属性文件的store方法(也就是写)
6              const char *buf, size_t count);
7 };

 

 

(3) 相关的宏定义

/**************************************************************/

#define DEVICE_ATTR(_name, _mode, _show, _store)   \

   struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)

 

#define __ATTR(_name,_mode,_show,_store) { \

   .attr = {.name = __stringify(_name), .mode = _mode }, \

   .show = _show,

   .store = _store,

/*************************************************************/

由此可知这个宏定义就是定义了一个 device_attribute 类型的变量并进行了初始化。

总结:我们如果需要给device添加属性文件,那么我们可以通过device_create_file函数去添加,我们需要提供相应的device属性文件描述信息,

也就是一个device_attribute 结构体变量。

 

2、class建立属性文件

(1)函数调用关系

/**********************************************************/

class_create_file

    sysfs_create_file

/***********************************************************/

(2)相关的数据结构

 

1 struct class_attribute {
2     struct attribute attr;              //  内置的  attribute  结构体变量
3     ssize_t (*show)(struct class *class, struct class_attribute *attr,      //   属性文件的show操作方法
4             char *buf);
5     ssize_t (*store)(struct class *class, struct class_attribute *attr,     //   属性文件的store操作方法
6             const char *buf, size_t count);
7 };

 

 

(3)相关的宏定义

/******************************************************************************/

#define CLASS_ATTR(_name, _mode, _show, _store) \

 

   struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store)

/******************************************************************************/

 

3、bus建立属性文件

(1)函数调用关系

/**********************************************************/

bus_create_file

    sysfs_create_file

/*********************************************************/

(2)相关的数据结构

1 struct bus_attribute {
2     struct attribute    attr;
3     ssize_t (*show)(struct bus_type *bus, char *buf);
4     ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
5 };

 

(3)相关的宏定义

#define BUS_ATTR(_name, _mode, _show, _store) \

  struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)

 

4、一次建立多个属性文件的方法

上面说的那些函数一次只能建立一个属性文件,其实在内核中还提供了另一个可以一次建立多个属性文件的函数,所以既然是建立多个,那么我们肯定是需要提供相应的数组。

(1)一次建立多个device属性

static int device_add_attributes(struct device *dev, struct device_attribute *attrs)

 

(2)一次建立多个bus属性文件

static int device_add_attrs(struct bus_type *bus, struct device *dev) 

 

/*************************************************************************************/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
sysfs_create_group函数用于在/sys目录下创建一个设备文件属性组。它需要传入一个kobject对象和一个属性组结构体,用于定义设备文件属性。 kobject对象表示设备对象,在Linux内核用于表示各种设备。属性组结构体包含了设备文件属性及其对应的操作函数。 具体使用方法如下: 1. 首先,需要定义一个属性结构体,其包含了设备文件属性及其对应的操作函数。例如: ```c static ssize_t show_attr(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { // 读取属性值 // 将属性值拷贝到buf return sprintf(buf, "%s\n", attr->attr.name); } static ssize_t store_attr(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { // 将buf的值写入属性 return count; } static struct kobj_attribute my_attr = __ATTR(my_attr_name, 0666, show_attr, store_attr); ``` 2. 创建一个kobject对象,并将其初始化。例如: ```c struct kobject *my_kobj; my_kobj = kobject_create_and_add("my_kobject", kernel_kobj); if (!my_kobj) return -ENOMEM; ``` 3. 调用sysfs_create_group函数创建设备文件属性组。例如: ```c int ret; struct attribute_group attr_group; attr_group.attrs = &my_attr.attr; attr_group.name = "my_attr_group"; ret = sysfs_create_group(my_kobj, &attr_group); if (ret) kobject_put(my_kobj); ``` 以上就是使用sysfs_create_group函数创建设备文件属性组的简单示例。注意,创建设备文件及其属性组需要在Linux内核驱动进行,具体使用方法会根据具体需求而有所不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值