建立设备属性文件(device_create_file)

博客介绍了设备属性文件相关操作,包括使用device_create_file在/sys/devices/xxx/目录下创建文件、device_remove_file移除文件,还提及DEVICE_ATTR宏创建属性结构体及初始化device_attribute结构体的宏。同时说明了创建流程,如实例化参数、创建和删除属性文件。

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

1. 涉及的内容

1)  int device_create_file(struct device *, struct device_attribute *);  在/sys/devices/xxx/目录下创建device属性文件

2) void device_remove_file(struct device *, struct device_attribute *); 移除/sys/devices/xxx/目录下的device属性文件

3) DEVICE_ATTR宏创建一个名为dev_attr_##_name的属性结构体,dev_attr_##_name用于device_create_file

#include </linux/device.h>

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

4) 初始化device_attribute结构体的宏

#include </linux/sysfs.h>

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

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

2、创建流程

a、DEVICE_ATTR里面的_name, _mode, _show, _store四个参数实例化。

_name表示名字,在sys中代表为属性

_mode表示属性操作的权限,比如0664

_show表示属性的读取函数,在用户空间读取属性值

_store表示属性的写入函数,在用户空间写入属性值

b、属性结构体准备完毕,device_create_file创建属性文件

c、device_remove_file删除属性文件

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

`device_create_file()`函数是在设备驱动程序中创建一个sysfs文件节点的函数。在Linux内核中,sysfs是一个虚拟文件系统,它允许内核和用户空间之间进行通信。sysfs文件系统中的每一个文件都是一个内核对象的属性,可以通过读写这些属性来控制和监视内核对象的状态。 `device_create_file()`函数需要三个参数:设备结构体(struct device *)、指向属性结构体(struct attribute *)的指针,以及sysfs文件节点名字的字符串。 以下是使用`device_create_file()`函数创建设备文件的示例代码: ```c #include <linux/device.h> static struct attribute my_attr = { .name = "my_attribute", .mode = S_IRUSR | S_IWUSR, }; static ssize_t my_attribute_show(struct device *dev, struct device_attribute *attr, char *buf) { // 读取属性值并将其写入到缓冲区buf中 } static ssize_t my_attribute_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { // 将从buf中读取的属性值写入到设备中 } static DEVICE_ATTR(my_attribute, S_IRUSR | S_IWUSR, my_attribute_show, my_attribute_store); static int my_device_probe(struct platform_device *pdev) { // 创建设备节点 device_create_file(&pdev->dev, &dev_attr_my_attribute); return 0; } static int my_device_remove(struct platform_device *pdev) { // 删除设备节点 device_remove_file(&pdev->dev, &dev_attr_my_attribute); return 0; } ``` 在上面的代码中,我们定义了一个名为`my_attr`的属性结构体,并使用`DEVICE_ATTR()`宏将其转换为设备属性。然后,在设备的probe函数中使用`device_create_file()`函数创建设备文件`my_attribute`,在设备的remove函数中使用`device_remove_file()`函数删除该文件。在设备属性的`show`和`store`函数中,我们可以实现对设备属性的读写操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值