HWMON Sysfs ABI、属性单位与告警
1. 规则
HWMON ABI 是文本十进制整数,每次 read 返回一个值。文件名和单位由 Documentation/hwmon/sysfs-interface.rst 规定。
2. 单位
| 类型 | 典型文件 | 单位 |
|---|---|---|
| 温度 | tempN_input |
millidegree Celsius |
| 电压 | inN_input |
millivolt |
| 电流 | currN_input |
milliampere |
| 功率 | powerN_input/average |
microwatt |
| 能量 | energyN_input |
microjoule |
| 湿度 | humidityN_input |
milli-percent |
| 风扇 | fanN_input |
RPM |
| PWM | pwmN |
通常 0..255 |
| alarm/fault | *_alarm |
0/1 |
驱动必须在内核中完成寄存器比例、符号和单位换算。
3. Channel
用户文件从 1 编号:
1 | temp1_input |
hwmon_ops 的 channel 参数从 0 编号。Core使用每类 attribute base 将二者转换。
4. 温度
常见:
- input;
- min/max/crit/emergency;
- hysteresis;
- alarm/fault;
- offset;
- lowest/highest/reset_history;
- type;
- label。
阈值是硬件阈值还是软件缓存由驱动和芯片决定。
5. 电力
inN_*:电压;currN_*:电流;powerN_input:瞬时功率;powerN_average:平均功率;powerN_cap:功率限制;energyN_input:积分能量。
shunt monitor 的结果依赖板级 shunt resistor 参数,DTS/平台数据错误会造成成比例的错误。
6. 风扇
fanN_input;- min/max/target;
- pulses;
- alarm/fault;
pwmN;pwmN_enable;pwmN_mode。
pwm_enable 数值语义按通用 ABI和驱动能力,不可假设所有驱动的自动控制模式相同。
7. 可写性
is_visible() 决定 mode。即使文件存在并可写,write() 仍应验证:
- range;
- channel;
- hardware capability;
- state/mode;
- conversion;
- rounding。
超范围通常返回 -EINVAL,通信失败返回底层 errno。
8. Alarm 与事件
alarm 文件通常是轮询读取。支持主动通知的驱动可调用:
1 | hwmon_notify_event(dev, type, attr, channel) |
Core 执行:
sysfs_notify();KOBJ_CHANGEuevent,环境变量NAME=<attribute>;- 若为温度则通知 thermal zone。
它不保证所有传感器都有硬件 IRQ。
9. 稳定识别
不要使用固定 /sys/class/hwmon/hwmon0。应:
- 读取
name; - 解析 device symlink;
- 使用 libsensors label/config;
- 必要时 udev 创建稳定 symlink。
10. 错误值
读取失败应返回 errno,不应伪造 0。0°C、0 RPM、0 mV 都可能是真实值;监控程序必须区分 read error、alarm 和物理 0。
正在加载留言…