-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtual_fan.c
More file actions
195 lines (165 loc) · 5.88 KB
/
virtual_fan.c
File metadata and controls
195 lines (165 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <linux/module.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/err.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
// 1. 必须定义宏
#define NUM_FANS 3
// 2. 结构体成员必须定义为数组
struct virtual_fan_data {
long pwm_value[NUM_FANS]; // 数组:保存每个风扇的 PWM
long enabled[NUM_FANS]; // 数组:保存每个风扇的使能状态
long fan_speed[NUM_FANS]; // 数组:保存来自 Go 的真实 RPM
};
// 属性文件的显示函数
static ssize_t virtual_fan_marker_show(struct device *dev, struct device_attribute *attr, char *buf) {
return snprintf(buf, PAGE_SIZE, "vFanByTk\n");
}
// 创建一个 sysfs 属性
static DEVICE_ATTR(marker, 0444, virtual_fan_marker_show, NULL);
// 属性可见性逻辑
static umode_t virtual_fan_is_visible(const void *data, enum hwmon_sensor_types type,
u32 attr, int channel) {
// 基础越界检查
if (channel >= NUM_FANS) return 0;
if (type == hwmon_pwm) {
switch (attr) {
case hwmon_pwm_input:
case hwmon_pwm_enable:
case hwmon_pwm_mode:
return 0644;
default: return 0;
}
} else if (type == hwmon_fan) {
if (attr == hwmon_fan_input) {
return 0644; // 允许 Go 写入 RPM 数据
}
}
return 0;
}
// 读取函数:利用 channel 索引
static int virtual_fan_read(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long *val) {
struct virtual_fan_data *data = dev_get_drvdata(dev);
if (channel < 0 || channel >= NUM_FANS) return -EINVAL;
if (type == hwmon_fan && attr == hwmon_fan_input) {
*val = data->fan_speed[channel];
return 0;
}
if (type == hwmon_pwm) {
if (attr == hwmon_pwm_input) {
*val = data->pwm_value[channel];
return 0;
}
if (attr == hwmon_pwm_enable) {
*val = data->enabled[channel];
return 0;
}
}
return -EOPNOTSUPP;
}
// 写入函数:利用 channel 索引
static int virtual_fan_write(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long val) {
struct virtual_fan_data *data = dev_get_drvdata(dev);
if (channel < 0 || channel >= NUM_FANS) return -EINVAL;
if (type == hwmon_fan && attr == hwmon_fan_input) {
data->fan_speed[channel] = val; // 接收来自 Go 的 RPM
return 0;
}
if (type == hwmon_pwm) {
switch (attr) {
case hwmon_pwm_enable:
if (val != 0 && val != 1) return -EINVAL;
data->enabled[channel] = val;
return 0;
case hwmon_pwm_input:
if (!data->enabled[channel]) return -EACCES;
if (val < 0 || val > 255) return -EINVAL;
data->pwm_value[channel] = val;
return 0;
}
}
return -EINVAL;
}
static const struct hwmon_ops virtual_fan_hwmon_ops = {
.is_visible = virtual_fan_is_visible,
.read = virtual_fan_read,
.write = virtual_fan_write,
};
// 3. 定义 3 个通道的信息
static const struct hwmon_channel_info *virtual_fan_info[] = {
HWMON_CHANNEL_INFO(pwm,
HWMON_PWM_INPUT | HWMON_PWM_ENABLE | HWMON_PWM_MODE, // ch 0
HWMON_PWM_INPUT | HWMON_PWM_ENABLE | HWMON_PWM_MODE, // ch 1
HWMON_PWM_INPUT | HWMON_PWM_ENABLE | HWMON_PWM_MODE),// ch 2
HWMON_CHANNEL_INFO(fan,
HWMON_F_INPUT, // ch 0
HWMON_F_INPUT, // ch 1
HWMON_F_INPUT), // ch 2
NULL
};
static const struct hwmon_chip_info virtual_fan_chip_info = {
.ops = &virtual_fan_hwmon_ops,
.info = virtual_fan_info,
};
// 基础 Probe 函数
static int virtual_fan_probe(struct platform_device *pdev) {
struct device *hwmon_dev;
struct virtual_fan_data *data;
int ret;
int i;
data = devm_kzalloc(&pdev->dev, sizeof(struct virtual_fan_data), GFP_KERNEL);
if (!data) return -ENOMEM;
// 4. 初始化所有通道的默认值
for (i = 0; i < NUM_FANS; i++) {
data->pwm_value[i] = 100;
data->enabled[i] = 1;
data->fan_speed[i] = 0;
}
hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev, "virtual_pwm_fan",
data, &virtual_fan_chip_info, NULL);
if (IS_ERR(hwmon_dev)) return PTR_ERR(hwmon_dev);
platform_set_drvdata(pdev, data);
// 创建 sysfs 属性文件
ret = device_create_file(&pdev->dev, &dev_attr_marker);
if (ret) {
pr_err("Virtual Fan: Failed to create sysfs attribute\n");
} else {
pr_info("Virtual Fan: Sysfs attribute created successfully\n");
}
return 0;
}
static struct platform_driver virtual_fan_driver = {
.driver = { .name = "virtual_fan_driver" },
.probe = virtual_fan_probe,
};
static struct platform_device *v_pdev;
static int __init virtual_fan_init(void) {
int ret;
pr_info("Virtual Fan: Module loading...\n");
ret = platform_driver_register(&virtual_fan_driver);
if (ret) {
pr_err("Virtual Fan: Failed to register driver\n");
return ret;
}
v_pdev = platform_device_register_simple("virtual_fan_driver", -1, NULL, 0);
if (IS_ERR(v_pdev)) {
pr_err("Virtual Fan: Failed to register device\n");
platform_driver_unregister(&virtual_fan_driver);
return PTR_ERR(v_pdev);
}
pr_info("Virtual Fan: Device registered successfully!\n");
return 0;
}
static void __exit virtual_fan_exit(void) {
platform_device_unregister(v_pdev);
platform_driver_unregister(&virtual_fan_driver);
}
module_init(virtual_fan_init);
module_exit(virtual_fan_exit);
MODULE_LICENSE("GPL");