zoukankan      html  css  js  c++  java
  • 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) 

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

  • 相关阅读:
    边工作边刷题:70天一遍leetcode: day 3
    边工作边刷题:70天一遍leetcode: day 3
    边工作边刷题:70天一遍leetcode: day 4
    边工作边刷题:70天一遍leetcode: day 4
    边工作边刷题:70天一遍leetcode: day 4
    javascript和jquery 获取触发事件的元素
    javascript 柯里化
    惰性函数
    IE6和IE7的line-height和现代浏览器不一致的问题
    img图片之间有空隙的问题
  • 原文地址:https://www.cnblogs.com/deng-tao/p/6367585.html
Copyright © 2011-2022 走看看