sysfs 属性能否在 Linux 设备驱动程序中采用非数值?

Can a sysfs attribute take a non numeric value in Linux Device Driver?

我正在开发一个 Linux 设备驱动程序,我必须使用 sysfs 接口将一串字符传递给它。 sysfs 属性可以接受字符串形式的数据(类似于 echo "somedata" > sysfs_interface )吗?

我已经在上面实现了它,它似乎工作正常,但我想确定这是有效的(内核社区可以接受)。

Can the sysfs attributes accepts the data in a string form ...

是的。
实际上,当您使用 echo 时,这就是 sysfs 接受的内容。当您使用 echo 0 时,输出是两个字节,0x30(数字零的 ASCII 代码)和 0x0A(换行符)。

例如GPIO LED接口使用关键字来报告和select触发器。

# cat /sys/class/leds/d8/trigger
none nand-disk mmc0 timer [heartbeat] gpio

(括号中的关键字表示当前selection,心跳定时器。)

# echo none > /sys/class/leds/d8/trigger
# cat /sys/class/leds/d8/trigger
[none] nand-disk mmc0 timer heartbeat gpio

... (something like echo "somedata" > sysfs_interface )

您甚至不需要使用引号。
参见上面将 LED 触发器设置为 none.

的示例

附录

these are the custom interfaces ...

不,这是主线。

... but what about the one provided by the subsystem?

权威答案来自Linux Documentation/filesystems/sysfs.txt

Attributes should be ASCII text files, preferably with only one value
per file.