DRIVER_ATTR and DEVICE_ATTR

本文介绍了Linux中设备属性和驱动程序属性的概念及其使用方法。通过DEVICE_ATTR和DRIVER_ATTR宏定义属性,并利用sysfs文件系统导出这些属性,使它们可以被应用程序读取和修改。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Driver Attributes
~~~~~~~~~~~~~~~~~
struct driver_attribute {
        struct attribute        attr;
        ssize_t (*show)(struct device_driver *driver, char *buf);
        ssize_t (*store)(struct device_driver *, const char * buf, size_t count);
};

Device drivers can export attributes via their sysfs directories.
Drivers can declare attributes using a DRIVER_ATTR macro that works
identically to the DEVICE_ATTR macro.

Example:

DRIVER_ATTR(debug,0644,show_debug,store_debug);

This is equivalent to declaring:

struct driver_attribute driver_attr_debug;

This can then be used to add and remove the attribute from the
driver's directory using:

int driver_create_file(struct device_driver *, const struct driver_attribute *);
void driver_remove_file(struct device_driver *, const struct driver_attribute *);


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

Attributes of devices can be exported via drivers using a simple
procfs-like interface.

Please see Documentation/filesystems/sysfs.txt for more information
on how sysfs works.

Attributes are declared using a macro called DEVICE_ATTR:

#define DEVICE_ATTR(name,mode,show,store)

Example:

DEVICE_ATTR(power,0644,show_power,store_power);

This declares a structure of type struct device_attribute named
'dev_attr_power'. This can then be added and removed to the device's
directory using:

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

Example:

device_create_file(dev,&dev_attr_power);
device_remove_file(dev,&dev_attr_power);

The file name will be 'power' with a mode of 0644 (-rw-r--r--).
device_attr是一个用于Linux设备驱动中的宏,主要用于定义设备属性。通过device_attr宏定义的属性可以在用户空间通过sysfs文件系统进行访问和操作。具体来说,device_attr宏用于创建一个struct device_attribute结构体,该结构体包含了属性的名称、权限以及读写函数。 device_attr的作用主要体现在以下几个方面: 1. **定义设备属性**:通过device_attr宏,可以在驱动中定义设备的属性,这些属性可以通过sysfs文件系统进行访问和操作。 2. **提供用户空间接口**:通过sysfs文件系统,用户空间程序可以读取或写入设备的属性,从而实现与设备的交互。 3. **简化驱动开发**:使用device_attr宏可以简化设备属性的定义和管理,减少驱动开发的工作量。 以下是一个简单的示例,展示了如何使用device_attr宏定义一个设备属性: ```c #include <linux/device.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> static ssize_t my_attr_show(struct device *dev, struct device_attribute *attr, char *buf) { return sprintf(buf, "Hello, World!\n"); } static ssize_t my_attr_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { printk(KERN_INFO "Received data: %s\n", buf); return count; } static DEVICE_ATTR(my_attr, 0644, my_attr_show, my_attr_store); static int __init my_driver_init(void) { int ret; struct device *dev; // 假设有一个设备结构体 dev ret = device_create_file(dev, &dev_attr_my_attr); if (ret < 0) { printk(KERN_ERR "Failed to create sysfs entry\n"); return ret; } printk(KERN_INFO "Driver initialized\n"); return 0; } static void __exit my_driver_exit(void) { struct device *dev; // 假设有一个设备结构体 dev device_remove_file(dev, &dev_attr_my_attr); printk(KERN_INFO "Driver exited\n"); } module_init(my_driver_init); module_exit(my_driver_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("A simple example of device_attr usage"); ``` 在这个示例中,我们定义了一个设备属性`my_attr`,并实现了其读写函数`my_attr_show`和`my_attr_store`。通过`device_create_file`函数将该属性注册到设备上,使其可以通过sysfs文件系统进行访问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值