Repository navigation
Expand file tree
/
Copy pathacer_battery_wmi.c
More file actions
258 lines (233 loc) · 7.89 KB
/
Copy pathacer_battery_wmi.c
File metadata and controls
258 lines (233 loc) · 7.89 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
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
#include <linux/acpi.h>
#include <linux/delay.h>
#include <linux/dmi.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/platform_device.h>
#include <linux/pm.h>
#include <linux/version.h>
MODULE_DESCRIPTION("Support Acer Battery Health Control");
MODULE_AUTHOR("Copyright 2021 maxc@stateoftheart.pw");
MODULE_LICENSE("GPL v2");
#define ACER_BATTERY_GUID "79772EC5-04B1-4BFD-843C-61E7F77B6CC9"
MODULE_ALIAS("wmi:79772EC5-04B1-4BFD-843C-61E7F77B6CC9");
struct SetBatteryControlIn {
u8 uBatteryNo;
u8 uFunctionMask;
u8 uFunctionStatus;
u8 uReserved[5];
} __attribute__((packed));
struct SetBatteryControlOut {
u16 uReturn;
u16 uReserved;
} __attribute__((packed));
struct GetBatteryControlIn {
u8 uBatteryNo;
u8 uFunctionQuery;
u16 uReserved;
} __attribute__((packed));
struct GetBatteryControlOut {
u8 uFunctionList;
u8 uReturn[2];
u8 uFunctionStatus[5];
} __attribute__((packed));
static struct platform_driver platform_driver;
static struct platform_device *platform_device;
/*
* Cached health mode, re-applied after resume because the EC may drop the
* setting over a sleep cycle. 0xFF means "unknown", set on first read, write
* or probe.
*/
static u8 health_mode_val = 0xFF;
/* Serializes WMI access and health_mode_val updates. */
static DEFINE_MUTEX(acer_battery_lock);
static acpi_status acer_battery_wmi_exec_method(u8 val) {
acpi_status status;
union acpi_object *obj;
struct SetBatteryControlIn in;
struct SetBatteryControlOut *out;
in.uBatteryNo = 1;
in.uFunctionMask = 1;
in.uFunctionStatus = val;
in.uReserved[0] = 0;
in.uReserved[1] = 0;
in.uReserved[2] = 0;
in.uReserved[3] = 0;
in.uReserved[4] = 0;
struct acpi_buffer input = {sizeof(struct SetBatteryControlIn), &in};
struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
status = wmi_evaluate_method(ACER_BATTERY_GUID, 0, 21, &input, &output);
if (ACPI_FAILURE(status)) {
pr_err("Error setting battery health status: %s\n",
acpi_format_exception(status));
return AE_ERROR;
}
obj = output.pointer;
if (!obj || obj->type != ACPI_TYPE_BUFFER || obj->buffer.length != 4) {
kfree(output.pointer);
pr_err("Unexpected output format setting battery health status, buffer "
"length:%d\n",
obj ? obj->buffer.length : 0);
return AE_ERROR;
}
out = (struct SetBatteryControlOut *)(obj->buffer.pointer);
if (out->uReturn != 0) {
pr_err("Firmware rejected battery health mode %d, ret was 0x%x\n", val,
out->uReturn);
kfree(output.pointer);
return AE_ERROR;
}
printk("Setting battery health status success! Status was %d ret was 0x%x "
"(%d)\n",
status, out->uReturn, out->uReturn);
kfree(output.pointer);
return AE_OK;
}
static int acer_battery_wmi_query_health_status(u8 *health_status) {
acpi_status status;
union acpi_object *obj;
struct GetBatteryControlIn in;
struct GetBatteryControlOut *out;
int ret = -ENODEV;
in.uBatteryNo = 1;
in.uFunctionQuery = 1;
in.uReserved = 0;
struct acpi_buffer input = {sizeof(struct GetBatteryControlIn), &in};
struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
status = wmi_evaluate_method(ACER_BATTERY_GUID, 0, 20, &input, &output);
if (ACPI_FAILURE(status)) {
pr_err("Error getting battery health status: %s\n",
acpi_format_exception(status));
return -ENODEV;
}
obj = output.pointer;
if (!obj || obj->type != ACPI_TYPE_BUFFER || obj->buffer.length != 8) {
pr_err("Unexpected output format getting battery health status, buffer "
"length:%d\n",
obj ? obj->buffer.length : 0);
goto out;
}
out = (struct GetBatteryControlOut *)(obj->buffer.pointer);
if (out->uReturn[0] > 0 || out->uReturn[1] > 0)
goto out;
switch (out->uFunctionList) {
case 1:
case 3:
*health_status = out->uFunctionStatus[0] > 0;
ret = 0;
break;
case 2:
/* Only calibration mode is available, no battery health control. */
default:
break;
}
out:
kfree(output.pointer);
if (ret == 0)
printk("Getting battery health status success!\n");
return ret;
}
static ssize_t acer_battery_show_save_mode(struct device *dev,
struct device_attribute *attr,
char *buf) {
u8 healthStatus;
int ret;
mutex_lock(&acer_battery_lock);
ret = acer_battery_wmi_query_health_status(&healthStatus);
if (ret == 0)
health_mode_val = healthStatus;
mutex_unlock(&acer_battery_lock);
if (ret != 0)
return -ENODEV;
return sprintf(buf, "%d\n", healthStatus);
}
static ssize_t acer_battery_store_save_mode(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count) {
u8 val;
if (sscanf(buf, "%hhd", &val) != 1)
return -EINVAL;
if ((val != 0) && (val != 1))
return -EINVAL;
mutex_lock(&acer_battery_lock);
if (acer_battery_wmi_exec_method(val) != AE_OK) {
mutex_unlock(&acer_battery_lock);
return -ENODEV;
}
health_mode_val = val;
mutex_unlock(&acer_battery_lock);
return count;
}
struct device_attribute acer_battery_health =
__ATTR(health_mode, 0664, acer_battery_show_save_mode,
acer_battery_store_save_mode);
static struct attribute *acer_battery_attrs[] = {&acer_battery_health.attr,
NULL};
static const struct attribute_group platform_attribute_group = {
.name = "acer_battery", .attrs = acer_battery_attrs};
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 11, 0)
static void acer_battery_wmi_remove(struct platform_device *device) {
#else
static int acer_battery_wmi_remove(struct platform_device *device) {
#endif
sysfs_remove_group(&platform_device->dev.kobj, &platform_attribute_group);
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 11, 0)
return 0;
#endif
}
static int __init acer_battery_wmi_probe(struct platform_device *pdev) {
u8 health_status;
int ret;
if (!wmi_has_guid(ACER_BATTERY_GUID)) {
pr_err("no acer battery WMI guid\n");
return -ENODEV;
}
/* Cache the current health mode so it can be restored on resume. */
mutex_lock(&acer_battery_lock);
ret = acer_battery_wmi_query_health_status(&health_status);
if (ret == 0)
health_mode_val = health_status;
mutex_unlock(&acer_battery_lock);
return sysfs_create_group(&pdev->dev.kobj, &platform_attribute_group);
}
static int acer_battery_wmi_resume(struct device *dev) {
/*
* The EC may not keep the health mode over a sleep cycle, re-apply it.
* Registered as .resume, .thaw and .restore to also cover hibernation.
*/
mutex_lock(&acer_battery_lock);
if (health_mode_val != 0xFF) {
if (acer_battery_wmi_exec_method(health_mode_val) != AE_OK)
pr_err("Failed to re-apply battery health mode after resume\n");
else
printk("Battery health mode %d re-applied after resume\n",
health_mode_val);
}
mutex_unlock(&acer_battery_lock);
return 0;
}
static const struct dev_pm_ops acer_battery_wmi_pm_ops = {
.resume = acer_battery_wmi_resume,
.thaw = acer_battery_wmi_resume,
.restore = acer_battery_wmi_resume};
static int __init acer_battery_wmi_init(void) {
platform_driver.remove = acer_battery_wmi_remove;
platform_driver.driver.owner = THIS_MODULE;
platform_driver.driver.name = "acer_battery_wmi";
platform_driver.driver.pm = &acer_battery_wmi_pm_ops;
platform_device = platform_create_bundle(
&platform_driver, acer_battery_wmi_probe, NULL, 0, NULL, 0);
if (IS_ERR(platform_device))
return PTR_ERR(platform_device);
pr_info("Acer battery WMI driver loaded\n");
return 0;
}
static void __exit acer_battery_wmi_exit(void) {
platform_device_unregister(platform_device);
platform_driver_unregister(&platform_driver);
pr_info("Acer battery WMI driver unloaded\n");
}
module_init(acer_battery_wmi_init);
module_exit(acer_battery_wmi_exit);