DEVICE_ATTR
时间: 2023-06-22 09:25:57 浏览: 173
"DEVICE_ATTR"是Linux内核中的一个宏定义,用于定义一个设备(device)的属性(attribute)。它定义了一个名为"dev_attr_<name>"的静态结构体实例,其中"<name>"是属性的名称,该结构体包含了属性的名称、读取函数和写入函数等信息。
以下是一个示例"DEVICE_ATTR"的定义:
```
#define DEVICE_ATTR(_name, _mode, _show, _store) \
struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
```
其中,参数含义如下:
- "_name":属性名称,类型为字符串;
- "_mode":属性文件的访问权限,包括读/写、读和写权限等;
- "_show":读取属性值的函数,类型为"ssize_t (*show)(struct device *dev, struct device_attribute *attr, char *buf)";
- "_store":写入属性值的函数,类型为"ssize_t (*store)(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)"。
通过使用"DEVICE_ATTR"宏定义,可以方便地为一个设备添加新的属性,并且在sysfs文件系统中创建相应的属性文件,从而实现对设备的属性进行读取和写入操作。
相关问题
DEVICE_ATTR_RW和DEVICE_ATTR区别
### DEVICE_ATTR_RW 和 DEVICE_ATTR 的区别
Linux内核提供了多种宏来简化设备属性的创建过程。`DEVICE_ATTR` 是用于定义只读或可写属性的基础宏[^2]。
对于 `DEVICE_ATTR` 宏而言,其语法结构如下:
```c
#define DEVICE_ATTR(_name, _mode, _show, _store)
```
此宏接受四个参数:属性名称 `_name`、权限模式 `_mode` 以及两个函数指针 `_show` 和 `_store` 分别对应于读取操作和写入操作。当只需要实现单向的数据访问(即要么能读不能写,要么能写不能读),可以仅提供相应的回调函数;如果不需要某个方向的操作,则可以把该位置设为空(NULL)。
而 `DEVICE_ATTR_RW` 则是一个更高层次封装版本,在功能上更加具体化。它专门用来声明既支持读又支持写的双向属性,并自动为用户提供默认的读写接口模板。因此,使用起来更为简便快捷。其实现方式本质上还是依赖于基础版的 `DEVICE_ATTR` 来完成实际工作[^1]。
通过比较可以看出,虽然两者都基于相同的底层机制构建而成,但是面向的应用场景有所不同——前者给予开发者更大的灵活性去定制特定行为,后者则更侧重于快速搭建常规性的读写特性。
#### 示例代码展示
以下是利用上述两种不同类型的宏实例化的简单示例:
```c
// 使用 DEVICE_ATTR 创建一个名为 "example_attr" 的属性,
// 支持读 (read_example_show),也支持写 (write_example_store).
static ssize_t read_example_show(struct device *dev,
struct device_attribute *attr,
char *buf);
static ssize_t write_example_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count);
static DEVICE_ATTR(example_attr, 0644, read_example_show, write_example_store);
// 使用 DEVICE_ATTR_RW 可以达到相同的效果,但编码量减少。
static ssize_t example_rw_show(struct device *dev,
struct device_attribute *attr,
char *buf);
static ssize_t example_rw_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count);
static DEVICE_ATTR_RW(wakealarm); // 实际应用中的例子来自 rtc-sysfs.c 文件
```
为什么用DEVICE_ATTR可以直接echo,DEVICE_ATTR_WO必须echo -n
`DEVICE_ATTR` 和 `DEVICE_ATTR_WO` 是内核中的两种属性类型,它们在使用上略有不同。
`DEVICE_ATTR` 属性是可读写的,因此您可以使用 `echo` 命令来写入值,也可以使用 `cat` 命令来读取值。例如,以下命令可以将值 `1` 写入 `/sys/devices/platform/my_device/my_attr` 文件:
```
echo 1 > /sys/devices/platform/my_device/my_attr
```
`DEVICE_ATTR_WO` 属性是只写的,因此您只能使用 `echo -n` 命令来写入值,而不能使用 `echo` 命令。这是因为 `echo` 命令会在写入值后追加一个换行符,而 `DEVICE_ATTR_WO` 的 `store` 函数不支持换行符。因此,使用 `echo -n` 命令可以确保只写入值本身,而不会附加任何其他字符。
总的来说,使用 `echo` 命令写入属性值时,需要根据属性类型来选择合适的命令和选项。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)