-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpdev.c
293 lines (215 loc) · 6.47 KB
/
pdev.c
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// SPDX-License-Identifier: GPL-2.0
#include "pr.h"
#include <linux/bug.h>
#include <linux/device.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include "util.h"
#include "ec.h"
#include "features.h"
#include "misc.h"
#include "pdev.h"
/* ========================================================================== */
struct platform_device *qc71_platform_dev;
/* ========================================================================== */
static ssize_t fan_reduced_duty_cycle_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
int status = ec_read_byte(BIOS_CTRL_3_ADDR);
if (status < 0)
return status;
return sprintf(buf, "%d\n", !!(status & BIOS_CTRL_3_FAN_REDUCED_DUTY_CYCLE));
}
static ssize_t fan_reduced_duty_cycle_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
int status;
bool value;
if (kstrtobool(buf, &value))
return -EINVAL;
status = ec_read_byte(BIOS_CTRL_3_ADDR);
if (status < 0)
return status;
status = SET_BIT(status, BIOS_CTRL_3_FAN_REDUCED_DUTY_CYCLE, value);
status = ec_write_byte(BIOS_CTRL_3_ADDR, status);
if (status < 0)
return status;
return count;
}
static ssize_t fan_always_on_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
int status = ec_read_byte(BIOS_CTRL_3_ADDR);
if (status < 0)
return status;
return sprintf(buf, "%d\n", !!(status & BIOS_CTRL_3_FAN_ALWAYS_ON));
}
static ssize_t fan_always_on_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
int status;
bool value;
if (kstrtobool(buf, &value))
return -EINVAL;
status = ec_read_byte(BIOS_CTRL_3_ADDR);
if (status < 0)
return status;
status = SET_BIT(status, BIOS_CTRL_3_FAN_ALWAYS_ON, value);
status = ec_write_byte(BIOS_CTRL_3_ADDR, status);
if (status < 0)
return status;
return count;
}
static ssize_t fn_lock_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
int status = qc71_fn_lock_get_state();
if (status < 0)
return status;
return sprintf(buf, "%d\n", status);
}
static ssize_t fn_lock_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
int status;
bool value;
if (kstrtobool(buf, &value))
return -EINVAL;
status = qc71_fn_lock_set_state(value);
if (status < 0)
return status;
return count;
}
static ssize_t fn_lock_switch_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
int status = ec_read_byte(AP_BIOS_BYTE_ADDR);
if (status < 0)
return status;
return sprintf(buf, "%d\n", !!(status & AP_BIOS_BYTE_FN_LOCK_SWITCH));
}
static ssize_t fn_lock_switch_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
int status;
bool value;
if (kstrtobool(buf, &value))
return -EINVAL;
status = ec_read_byte(AP_BIOS_BYTE_ADDR);
if (status < 0)
return status;
status = SET_BIT(status, AP_BIOS_BYTE_FN_LOCK_SWITCH, value);
status = ec_write_byte(AP_BIOS_BYTE_ADDR, status);
if (status < 0)
return status;
return count;
}
static ssize_t manual_control_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
int status = ec_read_byte(CTRL_1_ADDR);
if (status < 0)
return status;
return sprintf(buf, "%d\n", !!(status & CTRL_1_MANUAL_MODE));
}
static ssize_t manual_control_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
int status;
bool value;
if (kstrtobool(buf, &value))
return -EINVAL;
status = ec_read_byte(CTRL_1_ADDR);
if (status < 0)
return status;
status = SET_BIT(status, CTRL_1_MANUAL_MODE, value);
status = ec_write_byte(CTRL_1_ADDR, status);
if (status < 0)
return status;
return count;
}
static ssize_t super_key_lock_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
int status = ec_read_byte(STATUS_1_ADDR);
if (status < 0)
return status;
return sprintf(buf, "%d\n", !!(status & STATUS_1_SUPER_KEY_LOCK));
}
static ssize_t super_key_lock_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
int status;
bool value;
if (kstrtobool(buf, &value))
return -EINVAL;
status = ec_read_byte(STATUS_1_ADDR);
if (status < 0)
return status;
if (value != !!(status & STATUS_1_SUPER_KEY_LOCK)) {
status = ec_write_byte(TRIGGER_1_ADDR, TRIGGER_1_SUPER_KEY_LOCK);
if (status < 0)
return status;
}
return count;
}
/* ========================================================================== */
static DEVICE_ATTR_RW(fn_lock);
static DEVICE_ATTR_RW(fn_lock_switch);
static DEVICE_ATTR_RW(fan_always_on);
static DEVICE_ATTR_RW(fan_reduced_duty_cycle);
static DEVICE_ATTR_RW(manual_control);
static DEVICE_ATTR_RW(super_key_lock);
static struct attribute *qc71_laptop_attrs[] = {
&dev_attr_fn_lock.attr,
&dev_attr_fn_lock_switch.attr,
&dev_attr_fan_always_on.attr,
&dev_attr_fan_reduced_duty_cycle.attr,
&dev_attr_manual_control.attr,
&dev_attr_super_key_lock.attr,
NULL
};
/* ========================================================================== */
static umode_t qc71_laptop_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
{
bool ok = false;
if (attr == &dev_attr_fn_lock.attr || attr == &dev_attr_fn_lock_switch.attr)
ok = qc71_features.fn_lock;
else if (attr == &dev_attr_fan_always_on.attr || attr == &dev_attr_fan_reduced_duty_cycle.attr)
ok = qc71_features.fan_extras;
else if (attr == &dev_attr_manual_control.attr)
ok = true;
else if (attr == &dev_attr_super_key_lock.attr)
ok = qc71_features.super_key_lock;
return ok ? attr->mode : 0;
}
/* ========================================================================== */
static const struct attribute_group qc71_laptop_group = {
.is_visible = qc71_laptop_attr_is_visible,
.attrs = qc71_laptop_attrs,
};
static const struct attribute_group *qc71_laptop_groups[] = {
&qc71_laptop_group,
NULL
};
/* ========================================================================== */
int __init qc71_pdev_setup(void)
{
int err;
qc71_platform_dev = platform_device_alloc(KBUILD_MODNAME, PLATFORM_DEVID_NONE);
if (!qc71_platform_dev)
return -ENOMEM;
qc71_platform_dev->dev.groups = qc71_laptop_groups;
err = platform_device_add(qc71_platform_dev);
if (err) {
platform_device_put(qc71_platform_dev);
qc71_platform_dev = NULL;
}
return err;
}
void qc71_pdev_cleanup(void)
{
/* checks for IS_ERR_OR_NULL() */
platform_device_unregister(qc71_platform_dev);
}