-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfeatures.c
193 lines (144 loc) · 3.77 KB
/
features.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
// SPDX-License-Identifier: GPL-2.0
#include "pr.h"
#include <linux/ctype.h>
#include <linux/dmi.h>
#include <linux/init.h>
#include <linux/mod_devicetable.h>
#include <linux/slab.h>
#include <linux/string.h>
#include "ec.h"
#include "features.h"
/* ========================================================================== */
struct oem_string_walker_data {
char *value;
int index;
};
/* ========================================================================== */
static const struct dmi_system_id qc71_dmi_table[] __initconst = {
{
.matches = {
DMI_MATCH(DMI_BOARD_NAME, "LAPQC71"),
{ }
}
},
{
/* https://avell.com.br/avell-a60-muv-295765 */
.matches = {
DMI_EXACT_MATCH(DMI_CHASSIS_VENDOR, "Avell High Performance"),
DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "A60 MUV"),
{ }
}
},
{ }
};
/* ========================================================================== */
struct qc71_features_struct qc71_features;
/* ========================================================================== */
static void __init oem_string_walker(const struct dmi_header *dm, void *ptr)
{
int i, count;
const uint8_t *s;
struct oem_string_walker_data *data = ptr;
if (dm->type != 11 || dm->length < 5 || !IS_ERR_OR_NULL(data->value))
return;
count = *(uint8_t *)(dm + 1);
if (data->index >= count)
return;
i = 0;
s = ((uint8_t *)dm) + dm->length;
while (i++ < data->index && *s)
s += strlen(s) + 1;
data->value = kstrdup(s, GFP_KERNEL);
if (!data->value)
data->value = ERR_PTR(-ENOMEM);
}
static char * __init read_oem_string(int index)
{
struct oem_string_walker_data d = {.value = ERR_PTR(-ENOENT),
.index = index};
int err = dmi_walk(oem_string_walker, &d);
if (err) {
if (!IS_ERR_OR_NULL(d.value))
kfree(d.value);
return ERR_PTR(err);
}
return d.value;
}
/* QCCFL357.0062.2020.0313.1530 -> 62 */
static int __pure __init parse_bios_version(const char *str)
{
const char *p = strchr(str, '.'), *p2;
int bios_version;
if (!p)
return -EINVAL;
p2 = strchr(p + 1, '.');
if (!p2)
return -EINVAL;
p += 1;
bios_version = 0;
while (p != p2) {
if (!isdigit(*p))
return -EINVAL;
bios_version = 10 * bios_version + *p - '0';
p += 1;
}
return bios_version;
}
static int __init check_features_ec(void)
{
int err = ec_read_byte(SUPPORT_1_ADDR);
if (err >= 0) {
qc71_features.super_key_lock = !!(err & SUPPORT_1_SUPER_KEY_LOCK);
qc71_features.lightbar = !!(err & SUPPORT_1_LIGHTBAR);
qc71_features.fan_boost = !!(err & SUPPORT_1_FAN_BOOST);
} else {
pr_warn("failed to query support_1 byte: %d\n", err);
}
return err;
}
static int __init check_features_bios(void)
{
const char *bios_version_str;
int bios_version;
if (!dmi_check_system(qc71_dmi_table)) {
pr_warn("no DMI match\n");
return -ENODEV;
}
bios_version_str = dmi_get_system_info(DMI_BIOS_VERSION);
if (!bios_version_str) {
pr_warn("failed to get BIOS version DMI string\n");
return -ENOENT;
}
pr_info("BIOS version string: '%s'\n", bios_version_str);
bios_version = parse_bios_version(bios_version_str);
if (bios_version < 0) {
pr_warn("cannot parse BIOS version\n");
return -EINVAL;
}
pr_info("BIOS version: %04d\n", bios_version);
if (bios_version >= 114) {
const char *s = read_oem_string(18);
size_t s_len;
if (IS_ERR(s))
return PTR_ERR(s);
s_len = strlen(s);
pr_info("OEM_STRING(18) = '%s'\n", s);
/* if it is entirely spaces */
if (strspn(s, " ") == s_len) {
qc71_features.fn_lock = true;
qc71_features.batt_charge_limit = true;
qc71_features.fan_extras = true;
} else if (s_len > 0) {
/* TODO */
pr_warn("cannot extract supported features");
}
kfree(s);
}
return 0;
}
int __init qc71_check_features(void)
{
(void) check_features_ec();
(void) check_features_bios();
return 0;
}