linux驱动模型中的 DEVICE_ATTR 宏分析

来源:互联网 发布:程序员鼓励师是真的吗 编辑:程序博客网 时间:2024/05/20 05:27

 

定义于 "include/linux/device.h"  文件中:

 

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

而 "kernel/include/linux/sysfs.h" 也定义了 __ATTR , 这个宏被多个设备模型的宏使用到了。

 


/**
 * Use these macros to make defining attributes easier. See include/linux/device.h
 * for examples..
 */

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

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

#define __ATTR_NULL { .attr = { .name = NULL } }

#define attr_name(_attr) (_attr).attr.name

 

sysfs.h 也定义了 struct attribute

/* FIXME
 * The *owner field is no longer used.
 * x86 tree has been cleaned up. The owner
 * attribute is still left for other arches.
 */
struct attribute {
        const char              *name;
        struct module           *owner;
        mode_t                  mode;
#ifdef CONFIG_DEBUG_LOCK_ALLOC
        struct lock_class_key   *key;
        struct lock_class_key   skey;
#endif
};

 

 

struct device_attribute  也 定义于 "include/linux/device.h"  文件中:

 

/* 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);
};

 

所以, DEVICE_ATTR 这个宏实际上是定义并初始化了一个 名叫 “dev_attr_##_name”的 struct device_attribute 类型的结构体。

 

关于mode_t这个类型

include/linux/types.h:

        typedef __kernel_mode_t            mode_t;


arch/arm/include/asm/posix_types.h

        typedef unsigned short                __kernel_mode_t;

实际上 mode_t 这个类型,就是  unsigned short

include/asm-generic/posix_types.h

            typedef unsigned int   __kernel_mode_t; // 因为我们的平台是arm架构的所以,用的应该是“arch/arm/include/asm/posix_types.h” 这个文件对__kernel_mode_t的定义。

include/asm-generic/types.h

      typedef unsigned short umode_t; 

arch/arm/include/asm/types.h

      typedef unsigned short umode_t;

 

 

原创粉丝点击