]>
Commit | Line | Data |
---|---|---|
3e0a4e85 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 | 2 | /* |
a5afba16 | 3 | * dell-smm-hwmon.c -- Linux driver for accessing the SMM BIOS on Dell laptops. |
1da177e4 LT |
4 | * |
5 | * Copyright (C) 2001 Massimo Dal Zotto <dz@debian.org> | |
6 | * | |
949a9d70 | 7 | * Hwmon integration: |
7c81c60f | 8 | * Copyright (C) 2011 Jean Delvare <jdelvare@suse.de> |
564132d9 | 9 | * Copyright (C) 2013, 2014 Guenter Roeck <linux@roeck-us.net> |
149ed3d4 | 10 | * Copyright (C) 2014, 2015 Pali Rohár <pali@kernel.org> |
1da177e4 LT |
11 | */ |
12 | ||
60e71aaf GR |
13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
14 | ||
27046a3f | 15 | #include <linux/cpu.h> |
564132d9 | 16 | #include <linux/delay.h> |
1da177e4 LT |
17 | #include <linux/module.h> |
18 | #include <linux/types.h> | |
19 | #include <linux/init.h> | |
20 | #include <linux/proc_fs.h> | |
352f8f8b | 21 | #include <linux/seq_file.h> |
e70c9d5e | 22 | #include <linux/dmi.h> |
7e80d0d0 | 23 | #include <linux/capability.h> |
613655fa | 24 | #include <linux/mutex.h> |
949a9d70 JD |
25 | #include <linux/hwmon.h> |
26 | #include <linux/hwmon-sysfs.h> | |
12186f51 GR |
27 | #include <linux/uaccess.h> |
28 | #include <linux/io.h> | |
f36fdb9f | 29 | #include <linux/sched.h> |
053ea640 | 30 | #include <linux/ctype.h> |
27046a3f | 31 | #include <linux/smp.h> |
1da177e4 LT |
32 | |
33 | #include <linux/i8k.h> | |
34 | ||
1da177e4 LT |
35 | #define I8K_SMM_FN_STATUS 0x0025 |
36 | #define I8K_SMM_POWER_STATUS 0x0069 | |
37 | #define I8K_SMM_SET_FAN 0x01a3 | |
38 | #define I8K_SMM_GET_FAN 0x00a3 | |
39 | #define I8K_SMM_GET_SPEED 0x02a3 | |
f989e554 | 40 | #define I8K_SMM_GET_FAN_TYPE 0x03a3 |
8f21d8e9 | 41 | #define I8K_SMM_GET_NOM_SPEED 0x04a3 |
1da177e4 | 42 | #define I8K_SMM_GET_TEMP 0x10a3 |
5114b474 | 43 | #define I8K_SMM_GET_TEMP_TYPE 0x11a3 |
7e0fa31d DT |
44 | #define I8K_SMM_GET_DELL_SIG1 0xfea3 |
45 | #define I8K_SMM_GET_DELL_SIG2 0xffa3 | |
1da177e4 LT |
46 | |
47 | #define I8K_FAN_MULT 30 | |
8f21d8e9 | 48 | #define I8K_FAN_MAX_RPM 30000 |
1da177e4 LT |
49 | #define I8K_MAX_TEMP 127 |
50 | ||
51 | #define I8K_FN_NONE 0x00 | |
52 | #define I8K_FN_UP 0x01 | |
53 | #define I8K_FN_DOWN 0x02 | |
54 | #define I8K_FN_MUTE 0x04 | |
55 | #define I8K_FN_MASK 0x07 | |
56 | #define I8K_FN_SHIFT 8 | |
57 | ||
58 | #define I8K_POWER_AC 0x05 | |
59 | #define I8K_POWER_BATTERY 0x01 | |
60 | ||
613655fa | 61 | static DEFINE_MUTEX(i8k_mutex); |
e70c9d5e | 62 | static char bios_version[4]; |
7613663c | 63 | static char bios_machineid[16]; |
949a9d70 | 64 | static struct device *i8k_hwmon_dev; |
82ba1d3f | 65 | static u32 i8k_hwmon_flags; |
8f21d8e9 | 66 | static uint i8k_fan_mult = I8K_FAN_MULT; |
7f69fb03 PR |
67 | static uint i8k_pwm_mult; |
68 | static uint i8k_fan_max = I8K_FAN_HIGH; | |
2744d2fd | 69 | static bool disallow_fan_type_call; |
f480ea90 | 70 | static bool disallow_fan_support; |
afe45277 GM |
71 | static unsigned int manual_fan; |
72 | static unsigned int auto_fan; | |
82ba1d3f GR |
73 | |
74 | #define I8K_HWMON_HAVE_TEMP1 (1 << 0) | |
d83c39de GR |
75 | #define I8K_HWMON_HAVE_TEMP2 (1 << 1) |
76 | #define I8K_HWMON_HAVE_TEMP3 (1 << 2) | |
77 | #define I8K_HWMON_HAVE_TEMP4 (1 << 3) | |
1bb46a20 MS |
78 | #define I8K_HWMON_HAVE_TEMP5 (1 << 4) |
79 | #define I8K_HWMON_HAVE_TEMP6 (1 << 5) | |
80 | #define I8K_HWMON_HAVE_TEMP7 (1 << 6) | |
81 | #define I8K_HWMON_HAVE_TEMP8 (1 << 7) | |
82 | #define I8K_HWMON_HAVE_TEMP9 (1 << 8) | |
83 | #define I8K_HWMON_HAVE_TEMP10 (1 << 9) | |
84 | #define I8K_HWMON_HAVE_FAN1 (1 << 10) | |
85 | #define I8K_HWMON_HAVE_FAN2 (1 << 11) | |
86 | #define I8K_HWMON_HAVE_FAN3 (1 << 12) | |
1da177e4 LT |
87 | |
88 | MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)"); | |
149ed3d4 | 89 | MODULE_AUTHOR("Pali Rohár <pali@kernel.org>"); |
039ae585 | 90 | MODULE_DESCRIPTION("Dell laptop SMM BIOS hwmon driver"); |
1da177e4 | 91 | MODULE_LICENSE("GPL"); |
a5afba16 | 92 | MODULE_ALIAS("i8k"); |
1da177e4 | 93 | |
90ab5ee9 | 94 | static bool force; |
1da177e4 LT |
95 | module_param(force, bool, 0); |
96 | MODULE_PARM_DESC(force, "Force loading without checking for supported models"); | |
97 | ||
90ab5ee9 | 98 | static bool ignore_dmi; |
e70c9d5e DT |
99 | module_param(ignore_dmi, bool, 0); |
100 | MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match"); | |
101 | ||
039ae585 | 102 | #if IS_ENABLED(CONFIG_I8K) |
7613663c | 103 | static bool restricted = true; |
1da177e4 | 104 | module_param(restricted, bool, 0); |
7613663c | 105 | MODULE_PARM_DESC(restricted, "Restrict fan control and serial number to CAP_SYS_ADMIN (default: 1)"); |
1da177e4 | 106 | |
90ab5ee9 | 107 | static bool power_status; |
1da177e4 | 108 | module_param(power_status, bool, 0600); |
7613663c | 109 | MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k (default: 0)"); |
039ae585 | 110 | #endif |
1da177e4 | 111 | |
8f21d8e9 | 112 | static uint fan_mult; |
7f69fb03 | 113 | module_param(fan_mult, uint, 0); |
8f21d8e9 | 114 | MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with (default: autodetect)"); |
4ed99a27 | 115 | |
8f21d8e9 | 116 | static uint fan_max; |
7f69fb03 | 117 | module_param(fan_max, uint, 0); |
8f21d8e9 | 118 | MODULE_PARM_DESC(fan_max, "Maximum configurable fan speed (default: autodetect)"); |
81474fc2 | 119 | |
8378b924 | 120 | struct smm_regs { |
dec63ec3 | 121 | unsigned int eax; |
12186f51 GR |
122 | unsigned int ebx __packed; |
123 | unsigned int ecx __packed; | |
124 | unsigned int edx __packed; | |
125 | unsigned int esi __packed; | |
126 | unsigned int edi __packed; | |
8378b924 | 127 | }; |
1da177e4 | 128 | |
1855256c | 129 | static inline const char *i8k_get_dmi_data(int field) |
e70c9d5e | 130 | { |
1855256c | 131 | const char *dmi_data = dmi_get_system_info(field); |
4f005551 DT |
132 | |
133 | return dmi_data && *dmi_data ? dmi_data : "?"; | |
e70c9d5e | 134 | } |
1da177e4 LT |
135 | |
136 | /* | |
137 | * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard. | |
138 | */ | |
27046a3f | 139 | static int i8k_smm_func(void *par) |
1da177e4 | 140 | { |
dec63ec3 | 141 | int rc; |
27046a3f | 142 | struct smm_regs *regs = par; |
dec63ec3 | 143 | int eax = regs->eax; |
f36fdb9f | 144 | |
9d58bec0 PR |
145 | #ifdef DEBUG |
146 | int ebx = regs->ebx; | |
147 | unsigned long duration; | |
148 | ktime_t calltime, delta, rettime; | |
149 | ||
150 | calltime = ktime_get(); | |
151 | #endif | |
152 | ||
f36fdb9f | 153 | /* SMM requires CPU 0 */ |
27046a3f JG |
154 | if (smp_processor_id() != 0) |
155 | return -EBUSY; | |
dec63ec3 | 156 | |
fe04f22f | 157 | #if defined(CONFIG_X86_64) |
22d3243d | 158 | asm volatile("pushq %%rax\n\t" |
fe04f22f BS |
159 | "movl 0(%%rax),%%edx\n\t" |
160 | "pushq %%rdx\n\t" | |
161 | "movl 4(%%rax),%%ebx\n\t" | |
162 | "movl 8(%%rax),%%ecx\n\t" | |
163 | "movl 12(%%rax),%%edx\n\t" | |
164 | "movl 16(%%rax),%%esi\n\t" | |
165 | "movl 20(%%rax),%%edi\n\t" | |
166 | "popq %%rax\n\t" | |
167 | "out %%al,$0xb2\n\t" | |
168 | "out %%al,$0x84\n\t" | |
169 | "xchgq %%rax,(%%rsp)\n\t" | |
170 | "movl %%ebx,4(%%rax)\n\t" | |
171 | "movl %%ecx,8(%%rax)\n\t" | |
172 | "movl %%edx,12(%%rax)\n\t" | |
173 | "movl %%esi,16(%%rax)\n\t" | |
174 | "movl %%edi,20(%%rax)\n\t" | |
175 | "popq %%rdx\n\t" | |
176 | "movl %%edx,0(%%rax)\n\t" | |
bc1f419c LT |
177 | "pushfq\n\t" |
178 | "popq %%rax\n\t" | |
fe04f22f | 179 | "andl $1,%%eax\n" |
12186f51 | 180 | : "=a"(rc) |
fe04f22f BS |
181 | : "a"(regs) |
182 | : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory"); | |
183 | #else | |
22d3243d | 184 | asm volatile("pushl %%eax\n\t" |
dec63ec3 DT |
185 | "movl 0(%%eax),%%edx\n\t" |
186 | "push %%edx\n\t" | |
187 | "movl 4(%%eax),%%ebx\n\t" | |
188 | "movl 8(%%eax),%%ecx\n\t" | |
189 | "movl 12(%%eax),%%edx\n\t" | |
190 | "movl 16(%%eax),%%esi\n\t" | |
191 | "movl 20(%%eax),%%edi\n\t" | |
192 | "popl %%eax\n\t" | |
193 | "out %%al,$0xb2\n\t" | |
194 | "out %%al,$0x84\n\t" | |
195 | "xchgl %%eax,(%%esp)\n\t" | |
196 | "movl %%ebx,4(%%eax)\n\t" | |
197 | "movl %%ecx,8(%%eax)\n\t" | |
198 | "movl %%edx,12(%%eax)\n\t" | |
199 | "movl %%esi,16(%%eax)\n\t" | |
200 | "movl %%edi,20(%%eax)\n\t" | |
201 | "popl %%edx\n\t" | |
202 | "movl %%edx,0(%%eax)\n\t" | |
203 | "lahf\n\t" | |
204 | "shrl $8,%%eax\n\t" | |
6b4e81db | 205 | "andl $1,%%eax\n" |
12186f51 | 206 | : "=a"(rc) |
dec63ec3 DT |
207 | : "a"(regs) |
208 | : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory"); | |
fe04f22f | 209 | #endif |
8378b924 | 210 | if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax) |
f36fdb9f | 211 | rc = -EINVAL; |
dec63ec3 | 212 | |
9d58bec0 PR |
213 | #ifdef DEBUG |
214 | rettime = ktime_get(); | |
215 | delta = ktime_sub(rettime, calltime); | |
216 | duration = ktime_to_ns(delta) >> 10; | |
217 | pr_debug("smm(0x%.4x 0x%.4x) = 0x%.4x (took %7lu usecs)\n", eax, ebx, | |
218 | (rc ? 0xffff : regs->eax & 0xffff), duration); | |
219 | #endif | |
220 | ||
f36fdb9f | 221 | return rc; |
1da177e4 LT |
222 | } |
223 | ||
27046a3f JG |
224 | /* |
225 | * Call the System Management Mode BIOS. | |
226 | */ | |
227 | static int i8k_smm(struct smm_regs *regs) | |
228 | { | |
229 | int ret; | |
230 | ||
231 | get_online_cpus(); | |
232 | ret = smp_call_on_cpu(0, i8k_smm_func, regs, true); | |
233 | put_online_cpus(); | |
234 | ||
235 | return ret; | |
236 | } | |
237 | ||
1da177e4 LT |
238 | /* |
239 | * Read the fan status. | |
240 | */ | |
241 | static int i8k_get_fan_status(int fan) | |
242 | { | |
8378b924 | 243 | struct smm_regs regs = { .eax = I8K_SMM_GET_FAN, }; |
1da177e4 | 244 | |
f480ea90 PR |
245 | if (disallow_fan_support) |
246 | return -EINVAL; | |
247 | ||
dec63ec3 | 248 | regs.ebx = fan & 0xff; |
8378b924 | 249 | return i8k_smm(®s) ? : regs.eax & 0xff; |
1da177e4 LT |
250 | } |
251 | ||
252 | /* | |
253 | * Read the fan speed in RPM. | |
254 | */ | |
255 | static int i8k_get_fan_speed(int fan) | |
256 | { | |
8378b924 | 257 | struct smm_regs regs = { .eax = I8K_SMM_GET_SPEED, }; |
1da177e4 | 258 | |
f480ea90 PR |
259 | if (disallow_fan_support) |
260 | return -EINVAL; | |
261 | ||
dec63ec3 | 262 | regs.ebx = fan & 0xff; |
26d09382 | 263 | return i8k_smm(®s) ? : (regs.eax & 0xffff) * i8k_fan_mult; |
1da177e4 LT |
264 | } |
265 | ||
f989e554 PR |
266 | /* |
267 | * Read the fan type. | |
268 | */ | |
5ce91714 | 269 | static int _i8k_get_fan_type(int fan) |
f989e554 PR |
270 | { |
271 | struct smm_regs regs = { .eax = I8K_SMM_GET_FAN_TYPE, }; | |
272 | ||
f480ea90 | 273 | if (disallow_fan_support || disallow_fan_type_call) |
2744d2fd PR |
274 | return -EINVAL; |
275 | ||
f989e554 PR |
276 | regs.ebx = fan & 0xff; |
277 | return i8k_smm(®s) ? : regs.eax & 0xff; | |
278 | } | |
279 | ||
5ce91714 PR |
280 | static int i8k_get_fan_type(int fan) |
281 | { | |
282 | /* I8K_SMM_GET_FAN_TYPE SMM call is expensive, so cache values */ | |
747bc8b0 | 283 | static int types[3] = { INT_MIN, INT_MIN, INT_MIN }; |
5ce91714 PR |
284 | |
285 | if (types[fan] == INT_MIN) | |
286 | types[fan] = _i8k_get_fan_type(fan); | |
287 | ||
288 | return types[fan]; | |
289 | } | |
290 | ||
8f21d8e9 PR |
291 | /* |
292 | * Read the fan nominal rpm for specific fan speed. | |
293 | */ | |
294 | static int i8k_get_fan_nominal_speed(int fan, int speed) | |
295 | { | |
296 | struct smm_regs regs = { .eax = I8K_SMM_GET_NOM_SPEED, }; | |
297 | ||
f480ea90 PR |
298 | if (disallow_fan_support) |
299 | return -EINVAL; | |
300 | ||
8f21d8e9 PR |
301 | regs.ebx = (fan & 0xff) | (speed << 8); |
302 | return i8k_smm(®s) ? : (regs.eax & 0xffff) * i8k_fan_mult; | |
303 | } | |
304 | ||
afe45277 GM |
305 | /* |
306 | * Enable or disable automatic BIOS fan control support | |
307 | */ | |
308 | static int i8k_enable_fan_auto_mode(bool enable) | |
309 | { | |
310 | struct smm_regs regs = { }; | |
311 | ||
312 | if (disallow_fan_support) | |
313 | return -EINVAL; | |
314 | ||
315 | regs.eax = enable ? auto_fan : manual_fan; | |
316 | return i8k_smm(®s); | |
317 | } | |
318 | ||
1da177e4 LT |
319 | /* |
320 | * Set the fan speed (off, low, high). Returns the new fan status. | |
321 | */ | |
322 | static int i8k_set_fan(int fan, int speed) | |
323 | { | |
8378b924 | 324 | struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, }; |
1da177e4 | 325 | |
f480ea90 PR |
326 | if (disallow_fan_support) |
327 | return -EINVAL; | |
328 | ||
81474fc2 | 329 | speed = (speed < 0) ? 0 : ((speed > i8k_fan_max) ? i8k_fan_max : speed); |
dec63ec3 | 330 | regs.ebx = (fan & 0xff) | (speed << 8); |
1da177e4 | 331 | |
8378b924 | 332 | return i8k_smm(®s) ? : i8k_get_fan_status(fan); |
1da177e4 LT |
333 | } |
334 | ||
5114b474 PR |
335 | static int i8k_get_temp_type(int sensor) |
336 | { | |
337 | struct smm_regs regs = { .eax = I8K_SMM_GET_TEMP_TYPE, }; | |
338 | ||
339 | regs.ebx = sensor & 0xff; | |
340 | return i8k_smm(®s) ? : regs.eax & 0xff; | |
341 | } | |
342 | ||
1da177e4 LT |
343 | /* |
344 | * Read the cpu temperature. | |
345 | */ | |
564132d9 | 346 | static int _i8k_get_temp(int sensor) |
1da177e4 | 347 | { |
564132d9 GR |
348 | struct smm_regs regs = { |
349 | .eax = I8K_SMM_GET_TEMP, | |
350 | .ebx = sensor & 0xff, | |
351 | }; | |
1da177e4 | 352 | |
564132d9 GR |
353 | return i8k_smm(®s) ? : regs.eax & 0xff; |
354 | } | |
8378b924 | 355 | |
564132d9 GR |
356 | static int i8k_get_temp(int sensor) |
357 | { | |
358 | int temp = _i8k_get_temp(sensor); | |
1da177e4 | 359 | |
dec63ec3 DT |
360 | /* |
361 | * Sometimes the temperature sensor returns 0x99, which is out of range. | |
564132d9 | 362 | * In this case we retry (once) before returning an error. |
dec63ec3 DT |
363 | # 1003655137 00000058 00005a4b |
364 | # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees | |
365 | # 1003655139 00000054 00005c52 | |
366 | */ | |
564132d9 GR |
367 | if (temp == 0x99) { |
368 | msleep(100); | |
369 | temp = _i8k_get_temp(sensor); | |
dec63ec3 | 370 | } |
564132d9 GR |
371 | /* |
372 | * Return -ENODATA for all invalid temperatures. | |
373 | * | |
374 | * Known instances are the 0x99 value as seen above as well as | |
375 | * 0xc1 (193), which may be returned when trying to read the GPU | |
376 | * temperature if the system supports a GPU and it is currently | |
377 | * turned off. | |
378 | */ | |
723493ca | 379 | if (temp > I8K_MAX_TEMP) |
83d514d7 | 380 | return -ENODATA; |
1da177e4 | 381 | |
dec63ec3 | 382 | return temp; |
1da177e4 LT |
383 | } |
384 | ||
7e0fa31d | 385 | static int i8k_get_dell_signature(int req_fn) |
1da177e4 | 386 | { |
7e0fa31d | 387 | struct smm_regs regs = { .eax = req_fn, }; |
dec63ec3 | 388 | int rc; |
1da177e4 | 389 | |
12186f51 GR |
390 | rc = i8k_smm(®s); |
391 | if (rc < 0) | |
dec63ec3 | 392 | return rc; |
1da177e4 | 393 | |
8378b924 | 394 | return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1; |
1da177e4 LT |
395 | } |
396 | ||
039ae585 PR |
397 | #if IS_ENABLED(CONFIG_I8K) |
398 | ||
399 | /* | |
400 | * Read the Fn key status. | |
401 | */ | |
402 | static int i8k_get_fn_status(void) | |
403 | { | |
404 | struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, }; | |
405 | int rc; | |
406 | ||
407 | rc = i8k_smm(®s); | |
408 | if (rc < 0) | |
409 | return rc; | |
410 | ||
411 | switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) { | |
412 | case I8K_FN_UP: | |
413 | return I8K_VOL_UP; | |
414 | case I8K_FN_DOWN: | |
415 | return I8K_VOL_DOWN; | |
416 | case I8K_FN_MUTE: | |
417 | return I8K_VOL_MUTE; | |
418 | default: | |
419 | return 0; | |
420 | } | |
421 | } | |
422 | ||
423 | /* | |
424 | * Read the power status. | |
425 | */ | |
426 | static int i8k_get_power_status(void) | |
427 | { | |
428 | struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, }; | |
429 | int rc; | |
430 | ||
431 | rc = i8k_smm(®s); | |
432 | if (rc < 0) | |
433 | return rc; | |
434 | ||
435 | return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY; | |
436 | } | |
437 | ||
438 | /* | |
439 | * Procfs interface | |
440 | */ | |
441 | ||
d79b6f4d FW |
442 | static int |
443 | i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg) | |
1da177e4 | 444 | { |
e70c9d5e | 445 | int val = 0; |
dec63ec3 DT |
446 | int speed; |
447 | unsigned char buff[16]; | |
448 | int __user *argp = (int __user *)arg; | |
449 | ||
450 | if (!argp) | |
451 | return -EINVAL; | |
452 | ||
453 | switch (cmd) { | |
454 | case I8K_BIOS_VERSION: | |
053ea640 PR |
455 | if (!isdigit(bios_version[0]) || !isdigit(bios_version[1]) || |
456 | !isdigit(bios_version[2])) | |
457 | return -EINVAL; | |
458 | ||
e551b152 GR |
459 | val = (bios_version[0] << 16) | |
460 | (bios_version[1] << 8) | bios_version[2]; | |
dec63ec3 DT |
461 | break; |
462 | ||
463 | case I8K_MACHINE_ID: | |
7613663c PR |
464 | if (restricted && !capable(CAP_SYS_ADMIN)) |
465 | return -EPERM; | |
466 | ||
467 | memset(buff, 0, sizeof(buff)); | |
468 | strlcpy(buff, bios_machineid, sizeof(buff)); | |
dec63ec3 DT |
469 | break; |
470 | ||
471 | case I8K_FN_STATUS: | |
472 | val = i8k_get_fn_status(); | |
473 | break; | |
474 | ||
475 | case I8K_POWER_STATUS: | |
476 | val = i8k_get_power_status(); | |
477 | break; | |
478 | ||
479 | case I8K_GET_TEMP: | |
7e0fa31d | 480 | val = i8k_get_temp(0); |
dec63ec3 DT |
481 | break; |
482 | ||
483 | case I8K_GET_SPEED: | |
8378b924 | 484 | if (copy_from_user(&val, argp, sizeof(int))) |
dec63ec3 | 485 | return -EFAULT; |
8378b924 | 486 | |
dec63ec3 DT |
487 | val = i8k_get_fan_speed(val); |
488 | break; | |
1da177e4 | 489 | |
dec63ec3 | 490 | case I8K_GET_FAN: |
8378b924 | 491 | if (copy_from_user(&val, argp, sizeof(int))) |
dec63ec3 | 492 | return -EFAULT; |
8378b924 | 493 | |
dec63ec3 DT |
494 | val = i8k_get_fan_status(val); |
495 | break; | |
1da177e4 | 496 | |
dec63ec3 | 497 | case I8K_SET_FAN: |
8378b924 | 498 | if (restricted && !capable(CAP_SYS_ADMIN)) |
dec63ec3 | 499 | return -EPERM; |
8378b924 DT |
500 | |
501 | if (copy_from_user(&val, argp, sizeof(int))) | |
dec63ec3 | 502 | return -EFAULT; |
8378b924 DT |
503 | |
504 | if (copy_from_user(&speed, argp + 1, sizeof(int))) | |
dec63ec3 | 505 | return -EFAULT; |
8378b924 | 506 | |
dec63ec3 DT |
507 | val = i8k_set_fan(val, speed); |
508 | break; | |
1da177e4 | 509 | |
dec63ec3 DT |
510 | default: |
511 | return -EINVAL; | |
1da177e4 | 512 | } |
1da177e4 | 513 | |
8378b924 | 514 | if (val < 0) |
dec63ec3 | 515 | return val; |
1da177e4 | 516 | |
dec63ec3 DT |
517 | switch (cmd) { |
518 | case I8K_BIOS_VERSION: | |
8378b924 | 519 | if (copy_to_user(argp, &val, 4)) |
dec63ec3 | 520 | return -EFAULT; |
8378b924 | 521 | |
dec63ec3 DT |
522 | break; |
523 | case I8K_MACHINE_ID: | |
8378b924 | 524 | if (copy_to_user(argp, buff, 16)) |
dec63ec3 | 525 | return -EFAULT; |
8378b924 | 526 | |
dec63ec3 DT |
527 | break; |
528 | default: | |
8378b924 | 529 | if (copy_to_user(argp, &val, sizeof(int))) |
dec63ec3 | 530 | return -EFAULT; |
8378b924 | 531 | |
dec63ec3 | 532 | break; |
1da177e4 | 533 | } |
1da177e4 | 534 | |
dec63ec3 | 535 | return 0; |
1da177e4 LT |
536 | } |
537 | ||
d79b6f4d FW |
538 | static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg) |
539 | { | |
540 | long ret; | |
541 | ||
613655fa | 542 | mutex_lock(&i8k_mutex); |
d79b6f4d | 543 | ret = i8k_ioctl_unlocked(fp, cmd, arg); |
613655fa | 544 | mutex_unlock(&i8k_mutex); |
d79b6f4d FW |
545 | |
546 | return ret; | |
547 | } | |
548 | ||
1da177e4 LT |
549 | /* |
550 | * Print the information for /proc/i8k. | |
551 | */ | |
352f8f8b | 552 | static int i8k_proc_show(struct seq_file *seq, void *offset) |
1da177e4 | 553 | { |
352f8f8b | 554 | int fn_key, cpu_temp, ac_power; |
dec63ec3 DT |
555 | int left_fan, right_fan, left_speed, right_speed; |
556 | ||
96de0e25 JE |
557 | cpu_temp = i8k_get_temp(0); /* 11100 µs */ |
558 | left_fan = i8k_get_fan_status(I8K_FAN_LEFT); /* 580 µs */ | |
559 | right_fan = i8k_get_fan_status(I8K_FAN_RIGHT); /* 580 µs */ | |
560 | left_speed = i8k_get_fan_speed(I8K_FAN_LEFT); /* 580 µs */ | |
561 | right_speed = i8k_get_fan_speed(I8K_FAN_RIGHT); /* 580 µs */ | |
562 | fn_key = i8k_get_fn_status(); /* 750 µs */ | |
8378b924 | 563 | if (power_status) |
96de0e25 | 564 | ac_power = i8k_get_power_status(); /* 14700 µs */ |
8378b924 | 565 | else |
dec63ec3 | 566 | ac_power = -1; |
dec63ec3 DT |
567 | |
568 | /* | |
569 | * Info: | |
570 | * | |
571 | * 1) Format version (this will change if format changes) | |
572 | * 2) BIOS version | |
573 | * 3) BIOS machine ID | |
574 | * 4) Cpu temperature | |
575 | * 5) Left fan status | |
576 | * 6) Right fan status | |
577 | * 7) Left fan speed | |
578 | * 8) Right fan speed | |
579 | * 9) AC power | |
580 | * 10) Fn Key status | |
581 | */ | |
3a267d3b JP |
582 | seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n", |
583 | I8K_PROC_FMT, | |
584 | bios_version, | |
7613663c | 585 | (restricted && !capable(CAP_SYS_ADMIN)) ? "-1" : bios_machineid, |
3a267d3b JP |
586 | cpu_temp, |
587 | left_fan, right_fan, left_speed, right_speed, | |
588 | ac_power, fn_key); | |
589 | ||
590 | return 0; | |
1da177e4 LT |
591 | } |
592 | ||
352f8f8b | 593 | static int i8k_open_fs(struct inode *inode, struct file *file) |
1da177e4 | 594 | { |
352f8f8b | 595 | return single_open(file, i8k_proc_show, NULL); |
1da177e4 LT |
596 | } |
597 | ||
97a32539 AD |
598 | static const struct proc_ops i8k_proc_ops = { |
599 | .proc_open = i8k_open_fs, | |
600 | .proc_read = seq_read, | |
601 | .proc_lseek = seq_lseek, | |
602 | .proc_release = single_release, | |
603 | .proc_ioctl = i8k_ioctl, | |
039ae585 PR |
604 | }; |
605 | ||
606 | static void __init i8k_init_procfs(void) | |
607 | { | |
608 | /* Register the proc entry */ | |
97a32539 | 609 | proc_create("i8k", 0, NULL, &i8k_proc_ops); |
039ae585 PR |
610 | } |
611 | ||
612 | static void __exit i8k_exit_procfs(void) | |
613 | { | |
614 | remove_proc_entry("i8k", NULL); | |
615 | } | |
616 | ||
617 | #else | |
618 | ||
619 | static inline void __init i8k_init_procfs(void) | |
620 | { | |
621 | } | |
622 | ||
623 | static inline void __exit i8k_exit_procfs(void) | |
624 | { | |
625 | } | |
626 | ||
627 | #endif | |
949a9d70 JD |
628 | |
629 | /* | |
630 | * Hwmon interface | |
631 | */ | |
632 | ||
ba949ed6 | 633 | static ssize_t i8k_hwmon_temp_label_show(struct device *dev, |
5114b474 PR |
634 | struct device_attribute *devattr, |
635 | char *buf) | |
636 | { | |
637 | static const char * const labels[] = { | |
638 | "CPU", | |
639 | "GPU", | |
640 | "SODIMM", | |
641 | "Other", | |
642 | "Ambient", | |
643 | "Other", | |
644 | }; | |
645 | int index = to_sensor_dev_attr(devattr)->index; | |
646 | int type; | |
647 | ||
648 | type = i8k_get_temp_type(index); | |
649 | if (type < 0) | |
650 | return type; | |
651 | if (type >= ARRAY_SIZE(labels)) | |
652 | type = ARRAY_SIZE(labels) - 1; | |
653 | return sprintf(buf, "%s\n", labels[type]); | |
654 | } | |
655 | ||
ba949ed6 | 656 | static ssize_t i8k_hwmon_temp_show(struct device *dev, |
949a9d70 JD |
657 | struct device_attribute *devattr, |
658 | char *buf) | |
659 | { | |
d83c39de GR |
660 | int index = to_sensor_dev_attr(devattr)->index; |
661 | int temp; | |
949a9d70 | 662 | |
d83c39de GR |
663 | temp = i8k_get_temp(index); |
664 | if (temp < 0) | |
665 | return temp; | |
666 | return sprintf(buf, "%d\n", temp * 1000); | |
949a9d70 JD |
667 | } |
668 | ||
ba949ed6 | 669 | static ssize_t i8k_hwmon_fan_label_show(struct device *dev, |
f989e554 PR |
670 | struct device_attribute *devattr, |
671 | char *buf) | |
672 | { | |
673 | static const char * const labels[] = { | |
674 | "Processor Fan", | |
675 | "Motherboard Fan", | |
676 | "Video Fan", | |
677 | "Power Supply Fan", | |
678 | "Chipset Fan", | |
679 | "Other Fan", | |
680 | }; | |
681 | int index = to_sensor_dev_attr(devattr)->index; | |
682 | bool dock = false; | |
683 | int type; | |
684 | ||
685 | type = i8k_get_fan_type(index); | |
686 | if (type < 0) | |
687 | return type; | |
688 | ||
689 | if (type & 0x10) { | |
690 | dock = true; | |
691 | type &= 0x0F; | |
692 | } | |
693 | ||
694 | if (type >= ARRAY_SIZE(labels)) | |
695 | type = (ARRAY_SIZE(labels) - 1); | |
696 | ||
697 | return sprintf(buf, "%s%s\n", (dock ? "Docking " : ""), labels[type]); | |
698 | } | |
699 | ||
ba949ed6 GR |
700 | static ssize_t i8k_hwmon_fan_show(struct device *dev, |
701 | struct device_attribute *devattr, char *buf) | |
949a9d70 JD |
702 | { |
703 | int index = to_sensor_dev_attr(devattr)->index; | |
704 | int fan_speed; | |
705 | ||
706 | fan_speed = i8k_get_fan_speed(index); | |
707 | if (fan_speed < 0) | |
708 | return fan_speed; | |
709 | return sprintf(buf, "%d\n", fan_speed); | |
710 | } | |
711 | ||
ba949ed6 GR |
712 | static ssize_t i8k_hwmon_pwm_show(struct device *dev, |
713 | struct device_attribute *devattr, char *buf) | |
e7f5f275 GR |
714 | { |
715 | int index = to_sensor_dev_attr(devattr)->index; | |
716 | int status; | |
717 | ||
718 | status = i8k_get_fan_status(index); | |
719 | if (status < 0) | |
720 | return -EIO; | |
81474fc2 | 721 | return sprintf(buf, "%d\n", clamp_val(status * i8k_pwm_mult, 0, 255)); |
e7f5f275 GR |
722 | } |
723 | ||
ba949ed6 GR |
724 | static ssize_t i8k_hwmon_pwm_store(struct device *dev, |
725 | struct device_attribute *attr, | |
726 | const char *buf, size_t count) | |
e7f5f275 GR |
727 | { |
728 | int index = to_sensor_dev_attr(attr)->index; | |
729 | unsigned long val; | |
730 | int err; | |
731 | ||
732 | err = kstrtoul(buf, 10, &val); | |
733 | if (err) | |
734 | return err; | |
81474fc2 | 735 | val = clamp_val(DIV_ROUND_CLOSEST(val, i8k_pwm_mult), 0, i8k_fan_max); |
e7f5f275 GR |
736 | |
737 | mutex_lock(&i8k_mutex); | |
738 | err = i8k_set_fan(index, val); | |
739 | mutex_unlock(&i8k_mutex); | |
740 | ||
741 | return err < 0 ? -EIO : count; | |
742 | } | |
743 | ||
afe45277 GM |
744 | static ssize_t i8k_hwmon_pwm_enable_store(struct device *dev, |
745 | struct device_attribute *attr, | |
746 | const char *buf, size_t count) | |
747 | { | |
748 | int err; | |
749 | bool enable; | |
750 | unsigned long val; | |
751 | ||
752 | if (!auto_fan) | |
753 | return -ENODEV; | |
754 | ||
755 | err = kstrtoul(buf, 10, &val); | |
756 | if (err) | |
757 | return err; | |
758 | ||
759 | if (val == 1) | |
760 | enable = false; | |
761 | else if (val == 2) | |
762 | enable = true; | |
763 | else | |
764 | return -EINVAL; | |
765 | ||
766 | mutex_lock(&i8k_mutex); | |
767 | err = i8k_enable_fan_auto_mode(enable); | |
768 | mutex_unlock(&i8k_mutex); | |
769 | ||
770 | return err ? err : count; | |
771 | } | |
772 | ||
ba949ed6 GR |
773 | static SENSOR_DEVICE_ATTR_RO(temp1_input, i8k_hwmon_temp, 0); |
774 | static SENSOR_DEVICE_ATTR_RO(temp1_label, i8k_hwmon_temp_label, 0); | |
775 | static SENSOR_DEVICE_ATTR_RO(temp2_input, i8k_hwmon_temp, 1); | |
776 | static SENSOR_DEVICE_ATTR_RO(temp2_label, i8k_hwmon_temp_label, 1); | |
777 | static SENSOR_DEVICE_ATTR_RO(temp3_input, i8k_hwmon_temp, 2); | |
778 | static SENSOR_DEVICE_ATTR_RO(temp3_label, i8k_hwmon_temp_label, 2); | |
779 | static SENSOR_DEVICE_ATTR_RO(temp4_input, i8k_hwmon_temp, 3); | |
780 | static SENSOR_DEVICE_ATTR_RO(temp4_label, i8k_hwmon_temp_label, 3); | |
1bb46a20 MS |
781 | static SENSOR_DEVICE_ATTR_RO(temp5_input, i8k_hwmon_temp, 4); |
782 | static SENSOR_DEVICE_ATTR_RO(temp5_label, i8k_hwmon_temp_label, 4); | |
783 | static SENSOR_DEVICE_ATTR_RO(temp6_input, i8k_hwmon_temp, 5); | |
784 | static SENSOR_DEVICE_ATTR_RO(temp6_label, i8k_hwmon_temp_label, 5); | |
785 | static SENSOR_DEVICE_ATTR_RO(temp7_input, i8k_hwmon_temp, 6); | |
786 | static SENSOR_DEVICE_ATTR_RO(temp7_label, i8k_hwmon_temp_label, 6); | |
787 | static SENSOR_DEVICE_ATTR_RO(temp8_input, i8k_hwmon_temp, 7); | |
788 | static SENSOR_DEVICE_ATTR_RO(temp8_label, i8k_hwmon_temp_label, 7); | |
789 | static SENSOR_DEVICE_ATTR_RO(temp9_input, i8k_hwmon_temp, 8); | |
790 | static SENSOR_DEVICE_ATTR_RO(temp9_label, i8k_hwmon_temp_label, 8); | |
791 | static SENSOR_DEVICE_ATTR_RO(temp10_input, i8k_hwmon_temp, 9); | |
792 | static SENSOR_DEVICE_ATTR_RO(temp10_label, i8k_hwmon_temp_label, 9); | |
ba949ed6 GR |
793 | static SENSOR_DEVICE_ATTR_RO(fan1_input, i8k_hwmon_fan, 0); |
794 | static SENSOR_DEVICE_ATTR_RO(fan1_label, i8k_hwmon_fan_label, 0); | |
795 | static SENSOR_DEVICE_ATTR_RW(pwm1, i8k_hwmon_pwm, 0); | |
afe45277 | 796 | static SENSOR_DEVICE_ATTR_WO(pwm1_enable, i8k_hwmon_pwm_enable, 0); |
ba949ed6 GR |
797 | static SENSOR_DEVICE_ATTR_RO(fan2_input, i8k_hwmon_fan, 1); |
798 | static SENSOR_DEVICE_ATTR_RO(fan2_label, i8k_hwmon_fan_label, 1); | |
799 | static SENSOR_DEVICE_ATTR_RW(pwm2, i8k_hwmon_pwm, 1); | |
800 | static SENSOR_DEVICE_ATTR_RO(fan3_input, i8k_hwmon_fan, 2); | |
801 | static SENSOR_DEVICE_ATTR_RO(fan3_label, i8k_hwmon_fan_label, 2); | |
802 | static SENSOR_DEVICE_ATTR_RW(pwm3, i8k_hwmon_pwm, 2); | |
82ba1d3f GR |
803 | |
804 | static struct attribute *i8k_attrs[] = { | |
d83c39de | 805 | &sensor_dev_attr_temp1_input.dev_attr.attr, /* 0 */ |
5114b474 PR |
806 | &sensor_dev_attr_temp1_label.dev_attr.attr, /* 1 */ |
807 | &sensor_dev_attr_temp2_input.dev_attr.attr, /* 2 */ | |
808 | &sensor_dev_attr_temp2_label.dev_attr.attr, /* 3 */ | |
809 | &sensor_dev_attr_temp3_input.dev_attr.attr, /* 4 */ | |
810 | &sensor_dev_attr_temp3_label.dev_attr.attr, /* 5 */ | |
811 | &sensor_dev_attr_temp4_input.dev_attr.attr, /* 6 */ | |
812 | &sensor_dev_attr_temp4_label.dev_attr.attr, /* 7 */ | |
1bb46a20 MS |
813 | &sensor_dev_attr_temp5_input.dev_attr.attr, /* 8 */ |
814 | &sensor_dev_attr_temp5_label.dev_attr.attr, /* 9 */ | |
815 | &sensor_dev_attr_temp6_input.dev_attr.attr, /* 10 */ | |
816 | &sensor_dev_attr_temp6_label.dev_attr.attr, /* 11 */ | |
817 | &sensor_dev_attr_temp7_input.dev_attr.attr, /* 12 */ | |
818 | &sensor_dev_attr_temp7_label.dev_attr.attr, /* 13 */ | |
819 | &sensor_dev_attr_temp8_input.dev_attr.attr, /* 14 */ | |
820 | &sensor_dev_attr_temp8_label.dev_attr.attr, /* 15 */ | |
821 | &sensor_dev_attr_temp9_input.dev_attr.attr, /* 16 */ | |
822 | &sensor_dev_attr_temp9_label.dev_attr.attr, /* 17 */ | |
823 | &sensor_dev_attr_temp10_input.dev_attr.attr, /* 18 */ | |
824 | &sensor_dev_attr_temp10_label.dev_attr.attr, /* 19 */ | |
825 | &sensor_dev_attr_fan1_input.dev_attr.attr, /* 20 */ | |
826 | &sensor_dev_attr_fan1_label.dev_attr.attr, /* 21 */ | |
827 | &sensor_dev_attr_pwm1.dev_attr.attr, /* 22 */ | |
afe45277 GM |
828 | &sensor_dev_attr_pwm1_enable.dev_attr.attr, /* 23 */ |
829 | &sensor_dev_attr_fan2_input.dev_attr.attr, /* 24 */ | |
830 | &sensor_dev_attr_fan2_label.dev_attr.attr, /* 25 */ | |
831 | &sensor_dev_attr_pwm2.dev_attr.attr, /* 26 */ | |
832 | &sensor_dev_attr_fan3_input.dev_attr.attr, /* 27 */ | |
833 | &sensor_dev_attr_fan3_label.dev_attr.attr, /* 28 */ | |
834 | &sensor_dev_attr_pwm3.dev_attr.attr, /* 29 */ | |
82ba1d3f GR |
835 | NULL |
836 | }; | |
949a9d70 | 837 | |
82ba1d3f GR |
838 | static umode_t i8k_is_visible(struct kobject *kobj, struct attribute *attr, |
839 | int index) | |
949a9d70 | 840 | { |
35d470b5 | 841 | if (disallow_fan_support && index >= 20) |
f480ea90 | 842 | return 0; |
2744d2fd | 843 | if (disallow_fan_type_call && |
35d470b5 | 844 | (index == 21 || index == 25 || index == 28)) |
2744d2fd | 845 | return 0; |
5114b474 PR |
846 | if (index >= 0 && index <= 1 && |
847 | !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP1)) | |
82ba1d3f | 848 | return 0; |
5114b474 PR |
849 | if (index >= 2 && index <= 3 && |
850 | !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP2)) | |
d83c39de | 851 | return 0; |
5114b474 PR |
852 | if (index >= 4 && index <= 5 && |
853 | !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP3)) | |
d83c39de | 854 | return 0; |
5114b474 PR |
855 | if (index >= 6 && index <= 7 && |
856 | !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP4)) | |
d83c39de | 857 | return 0; |
1bb46a20 MS |
858 | if (index >= 8 && index <= 9 && |
859 | !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP5)) | |
860 | return 0; | |
861 | if (index >= 10 && index <= 11 && | |
862 | !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP6)) | |
863 | return 0; | |
864 | if (index >= 12 && index <= 13 && | |
865 | !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP7)) | |
866 | return 0; | |
867 | if (index >= 14 && index <= 15 && | |
868 | !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP8)) | |
869 | return 0; | |
870 | if (index >= 16 && index <= 17 && | |
871 | !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP9)) | |
872 | return 0; | |
873 | if (index >= 18 && index <= 19 && | |
874 | !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP10)) | |
875 | return 0; | |
876 | ||
afe45277 | 877 | if (index >= 20 && index <= 23 && |
82ba1d3f GR |
878 | !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN1)) |
879 | return 0; | |
afe45277 | 880 | if (index >= 24 && index <= 26 && |
82ba1d3f GR |
881 | !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN2)) |
882 | return 0; | |
afe45277 | 883 | if (index >= 27 && index <= 29 && |
747bc8b0 PR |
884 | !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN3)) |
885 | return 0; | |
82ba1d3f | 886 | |
afe45277 GM |
887 | if (index == 23 && !auto_fan) |
888 | return 0; | |
889 | ||
82ba1d3f | 890 | return attr->mode; |
949a9d70 JD |
891 | } |
892 | ||
82ba1d3f GR |
893 | static const struct attribute_group i8k_group = { |
894 | .attrs = i8k_attrs, | |
895 | .is_visible = i8k_is_visible, | |
896 | }; | |
897 | __ATTRIBUTE_GROUPS(i8k); | |
898 | ||
949a9d70 JD |
899 | static int __init i8k_init_hwmon(void) |
900 | { | |
901 | int err; | |
902 | ||
82ba1d3f | 903 | i8k_hwmon_flags = 0; |
949a9d70 | 904 | |
672af223 PR |
905 | /* CPU temperature attributes, if temperature type is OK */ |
906 | err = i8k_get_temp_type(0); | |
907 | if (err >= 0) | |
82ba1d3f | 908 | i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP1; |
d83c39de | 909 | /* check for additional temperature sensors */ |
672af223 PR |
910 | err = i8k_get_temp_type(1); |
911 | if (err >= 0) | |
d83c39de | 912 | i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP2; |
672af223 PR |
913 | err = i8k_get_temp_type(2); |
914 | if (err >= 0) | |
d83c39de | 915 | i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP3; |
672af223 PR |
916 | err = i8k_get_temp_type(3); |
917 | if (err >= 0) | |
d83c39de | 918 | i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP4; |
1bb46a20 MS |
919 | err = i8k_get_temp_type(4); |
920 | if (err >= 0) | |
921 | i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP5; | |
922 | err = i8k_get_temp_type(5); | |
923 | if (err >= 0) | |
924 | i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP6; | |
925 | err = i8k_get_temp_type(6); | |
926 | if (err >= 0) | |
927 | i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP7; | |
928 | err = i8k_get_temp_type(7); | |
929 | if (err >= 0) | |
930 | i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP8; | |
931 | err = i8k_get_temp_type(8); | |
932 | if (err >= 0) | |
933 | i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP9; | |
934 | err = i8k_get_temp_type(9); | |
935 | if (err >= 0) | |
936 | i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP10; | |
949a9d70 | 937 | |
5ce91714 PR |
938 | /* First fan attributes, if fan status or type is OK */ |
939 | err = i8k_get_fan_status(0); | |
940 | if (err < 0) | |
941 | err = i8k_get_fan_type(0); | |
82ba1d3f GR |
942 | if (err >= 0) |
943 | i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN1; | |
949a9d70 | 944 | |
5ce91714 PR |
945 | /* Second fan attributes, if fan status or type is OK */ |
946 | err = i8k_get_fan_status(1); | |
947 | if (err < 0) | |
948 | err = i8k_get_fan_type(1); | |
82ba1d3f GR |
949 | if (err >= 0) |
950 | i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN2; | |
949a9d70 | 951 | |
747bc8b0 PR |
952 | /* Third fan attributes, if fan status or type is OK */ |
953 | err = i8k_get_fan_status(2); | |
954 | if (err < 0) | |
955 | err = i8k_get_fan_type(2); | |
956 | if (err >= 0) | |
957 | i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN3; | |
958 | ||
9026cae1 | 959 | i8k_hwmon_dev = hwmon_device_register_with_groups(NULL, "dell_smm", |
039ae585 | 960 | NULL, i8k_groups); |
82ba1d3f GR |
961 | if (IS_ERR(i8k_hwmon_dev)) { |
962 | err = PTR_ERR(i8k_hwmon_dev); | |
963 | i8k_hwmon_dev = NULL; | |
964 | pr_err("hwmon registration failed (%d)\n", err); | |
965 | return err; | |
966 | } | |
949a9d70 | 967 | return 0; |
949a9d70 JD |
968 | } |
969 | ||
81474fc2 | 970 | struct i8k_config_data { |
7f69fb03 PR |
971 | uint fan_mult; |
972 | uint fan_max; | |
81474fc2 GR |
973 | }; |
974 | ||
975 | enum i8k_configs { | |
7b883446 GR |
976 | DELL_LATITUDE_D520, |
977 | DELL_PRECISION_490, | |
81474fc2 | 978 | DELL_STUDIO, |
34ae40f6 | 979 | DELL_XPS, |
81474fc2 GR |
980 | }; |
981 | ||
982 | static const struct i8k_config_data i8k_config_data[] = { | |
7b883446 GR |
983 | [DELL_LATITUDE_D520] = { |
984 | .fan_mult = 1, | |
985 | .fan_max = I8K_FAN_TURBO, | |
986 | }, | |
987 | [DELL_PRECISION_490] = { | |
988 | .fan_mult = 1, | |
989 | .fan_max = I8K_FAN_TURBO, | |
990 | }, | |
81474fc2 GR |
991 | [DELL_STUDIO] = { |
992 | .fan_mult = 1, | |
993 | .fan_max = I8K_FAN_HIGH, | |
994 | }, | |
34ae40f6 | 995 | [DELL_XPS] = { |
81474fc2 GR |
996 | .fan_mult = 1, |
997 | .fan_max = I8K_FAN_HIGH, | |
998 | }, | |
999 | }; | |
1000 | ||
6faadbbb | 1001 | static const struct dmi_system_id i8k_dmi_table[] __initconst = { |
e70c9d5e DT |
1002 | { |
1003 | .ident = "Dell Inspiron", | |
1004 | .matches = { | |
1005 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"), | |
1006 | DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"), | |
1007 | }, | |
1008 | }, | |
1009 | { | |
1010 | .ident = "Dell Latitude", | |
1011 | .matches = { | |
1012 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"), | |
1013 | DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"), | |
1014 | }, | |
1015 | }, | |
7e0fa31d DT |
1016 | { |
1017 | .ident = "Dell Inspiron 2", | |
1018 | .matches = { | |
1019 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
1020 | DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"), | |
1021 | }, | |
1022 | }, | |
7b883446 GR |
1023 | { |
1024 | .ident = "Dell Latitude D520", | |
1025 | .matches = { | |
1026 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
1027 | DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D520"), | |
1028 | }, | |
1029 | .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_D520], | |
1030 | }, | |
7e0fa31d DT |
1031 | { |
1032 | .ident = "Dell Latitude 2", | |
1033 | .matches = { | |
1034 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
1035 | DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"), | |
1036 | }, | |
1037 | }, | |
a9000d03 NW |
1038 | { /* UK Inspiron 6400 */ |
1039 | .ident = "Dell Inspiron 3", | |
1040 | .matches = { | |
1041 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
1042 | DMI_MATCH(DMI_PRODUCT_NAME, "MM061"), | |
1043 | }, | |
1044 | }, | |
48103c52 FS |
1045 | { |
1046 | .ident = "Dell Inspiron 3", | |
1047 | .matches = { | |
1048 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
1049 | DMI_MATCH(DMI_PRODUCT_NAME, "MP061"), | |
1050 | }, | |
1051 | }, | |
7b883446 GR |
1052 | { |
1053 | .ident = "Dell Precision 490", | |
1054 | .matches = { | |
1055 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
1056 | DMI_MATCH(DMI_PRODUCT_NAME, | |
1057 | "Precision WorkStation 490"), | |
1058 | }, | |
1059 | .driver_data = (void *)&i8k_config_data[DELL_PRECISION_490], | |
1060 | }, | |
7ab21a86 AS |
1061 | { |
1062 | .ident = "Dell Precision", | |
1063 | .matches = { | |
1064 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
1065 | DMI_MATCH(DMI_PRODUCT_NAME, "Precision"), | |
1066 | }, | |
1067 | }, | |
bef2a508 FH |
1068 | { |
1069 | .ident = "Dell Vostro", | |
1070 | .matches = { | |
1071 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
1072 | DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"), | |
1073 | }, | |
1074 | }, | |
ff457bde GR |
1075 | { |
1076 | .ident = "Dell Studio", | |
1077 | .matches = { | |
1078 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
1079 | DMI_MATCH(DMI_PRODUCT_NAME, "Studio"), | |
1080 | }, | |
81474fc2 | 1081 | .driver_data = (void *)&i8k_config_data[DELL_STUDIO], |
ff457bde | 1082 | }, |
919a0304 GR |
1083 | { |
1084 | .ident = "Dell XPS M140", | |
1085 | .matches = { | |
1086 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
1087 | DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"), | |
1088 | }, | |
34ae40f6 | 1089 | .driver_data = (void *)&i8k_config_data[DELL_XPS], |
919a0304 | 1090 | }, |
a4811b6c | 1091 | { |
b8a13e5e | 1092 | .ident = "Dell XPS", |
162372b0 MS |
1093 | .matches = { |
1094 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
b8a13e5e | 1095 | DMI_MATCH(DMI_PRODUCT_NAME, "XPS"), |
162372b0 MS |
1096 | }, |
1097 | }, | |
12186f51 | 1098 | { } |
e70c9d5e | 1099 | }; |
1da177e4 | 1100 | |
148b1fda PR |
1101 | MODULE_DEVICE_TABLE(dmi, i8k_dmi_table); |
1102 | ||
2744d2fd PR |
1103 | /* |
1104 | * On some machines once I8K_SMM_GET_FAN_TYPE is issued then CPU fan speed | |
1105 | * randomly going up and down due to bug in Dell SMM or BIOS. Here is blacklist | |
1106 | * of affected Dell machines for which we disallow I8K_SMM_GET_FAN_TYPE call. | |
1107 | * See bug: https://bugzilla.kernel.org/show_bug.cgi?id=100121 | |
1108 | */ | |
6faadbbb | 1109 | static const struct dmi_system_id i8k_blacklist_fan_type_dmi_table[] __initconst = { |
6220f4eb | 1110 | { |
6220f4eb TL |
1111 | .ident = "Dell Studio XPS 8000", |
1112 | .matches = { | |
1113 | DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
1114 | DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8000"), | |
1115 | }, | |
1116 | }, | |
a4b45b25 | 1117 | { |
a4b45b25 PR |
1118 | .ident = "Dell Studio XPS 8100", |
1119 | .matches = { | |
1120 | DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
1121 | DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8100"), | |
1122 | }, | |
1123 | }, | |
2744d2fd PR |
1124 | { |
1125 | .ident = "Dell Inspiron 580", | |
1126 | .matches = { | |
1127 | DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
1128 | DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 580 "), | |
1129 | }, | |
1130 | }, | |
a4b45b25 PR |
1131 | { } |
1132 | }; | |
1133 | ||
f480ea90 PR |
1134 | /* |
1135 | * On some machines all fan related SMM functions implemented by Dell BIOS | |
1136 | * firmware freeze kernel for about 500ms. Until Dell fixes these problems fan | |
1137 | * support for affected blacklisted Dell machines stay disabled. | |
1138 | * See bug: https://bugzilla.kernel.org/show_bug.cgi?id=195751 | |
1139 | */ | |
1140 | static struct dmi_system_id i8k_blacklist_fan_support_dmi_table[] __initdata = { | |
1141 | { | |
1142 | .ident = "Dell Inspiron 7720", | |
1143 | .matches = { | |
1144 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
1145 | DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"), | |
1146 | }, | |
1147 | }, | |
6fbc4232 ON |
1148 | { |
1149 | .ident = "Dell Vostro 3360", | |
1150 | .matches = { | |
1151 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
1152 | DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"), | |
1153 | }, | |
1154 | }, | |
536e0019 HE |
1155 | { |
1156 | .ident = "Dell XPS13 9333", | |
1157 | .matches = { | |
1158 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
1159 | DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"), | |
1160 | }, | |
1161 | }, | |
4008bc7d TH |
1162 | { |
1163 | .ident = "Dell XPS 15 L502X", | |
1164 | .matches = { | |
1165 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
1166 | DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Dell System XPS L502X"), | |
1167 | }, | |
1168 | }, | |
f480ea90 PR |
1169 | { } |
1170 | }; | |
1171 | ||
afe45277 GM |
1172 | struct i8k_fan_control_data { |
1173 | unsigned int manual_fan; | |
1174 | unsigned int auto_fan; | |
1175 | }; | |
1176 | ||
1177 | enum i8k_fan_controls { | |
1178 | I8K_FAN_34A3_35A3, | |
1179 | }; | |
1180 | ||
1181 | static const struct i8k_fan_control_data i8k_fan_control_data[] = { | |
1182 | [I8K_FAN_34A3_35A3] = { | |
1183 | .manual_fan = 0x34a3, | |
1184 | .auto_fan = 0x35a3, | |
1185 | }, | |
1186 | }; | |
1187 | ||
1188 | static struct dmi_system_id i8k_whitelist_fan_control[] __initdata = { | |
1189 | { | |
1190 | .ident = "Dell Precision 5530", | |
1191 | .matches = { | |
1192 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
1193 | DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Precision 5530"), | |
1194 | }, | |
1195 | .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3], | |
1196 | }, | |
0ca8bb2c JL |
1197 | { |
1198 | .ident = "Dell Latitude 5480", | |
1199 | .matches = { | |
1200 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
1201 | DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Latitude 5480"), | |
1202 | }, | |
1203 | .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3], | |
1204 | }, | |
afe45277 GM |
1205 | { |
1206 | .ident = "Dell Latitude E6440", | |
1207 | .matches = { | |
1208 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
1209 | DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Latitude E6440"), | |
1210 | }, | |
1211 | .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3], | |
1212 | }, | |
807b8c29 SO |
1213 | { |
1214 | .ident = "Dell Latitude E7440", | |
1215 | .matches = { | |
1216 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
1217 | DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Latitude E7440"), | |
1218 | }, | |
1219 | .driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3], | |
1220 | }, | |
afe45277 GM |
1221 | { } |
1222 | }; | |
1223 | ||
1da177e4 LT |
1224 | /* |
1225 | * Probe for the presence of a supported laptop. | |
1226 | */ | |
1227 | static int __init i8k_probe(void) | |
1228 | { | |
afe45277 | 1229 | const struct dmi_system_id *id, *fan_control; |
8f21d8e9 | 1230 | int fan, ret; |
dec63ec3 | 1231 | |
1da177e4 | 1232 | /* |
dec63ec3 | 1233 | * Get DMI information |
1da177e4 | 1234 | */ |
2744d2fd | 1235 | if (!dmi_check_system(i8k_dmi_table)) { |
e70c9d5e DT |
1236 | if (!ignore_dmi && !force) |
1237 | return -ENODEV; | |
1238 | ||
60e71aaf GR |
1239 | pr_info("not running on a supported Dell system.\n"); |
1240 | pr_info("vendor=%s, model=%s, version=%s\n", | |
e70c9d5e DT |
1241 | i8k_get_dmi_data(DMI_SYS_VENDOR), |
1242 | i8k_get_dmi_data(DMI_PRODUCT_NAME), | |
1243 | i8k_get_dmi_data(DMI_BIOS_VERSION)); | |
1da177e4 | 1244 | } |
dec63ec3 | 1245 | |
f480ea90 PR |
1246 | if (dmi_check_system(i8k_blacklist_fan_support_dmi_table)) { |
1247 | pr_warn("broken Dell BIOS detected, disallow fan support\n"); | |
1248 | if (!force) | |
1249 | disallow_fan_support = true; | |
1250 | } | |
1251 | ||
836ad112 PR |
1252 | if (dmi_check_system(i8k_blacklist_fan_type_dmi_table)) { |
1253 | pr_warn("broken Dell BIOS detected, disallow fan type call\n"); | |
1254 | if (!force) | |
1255 | disallow_fan_type_call = true; | |
1256 | } | |
2744d2fd | 1257 | |
12186f51 GR |
1258 | strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION), |
1259 | sizeof(bios_version)); | |
7613663c PR |
1260 | strlcpy(bios_machineid, i8k_get_dmi_data(DMI_PRODUCT_SERIAL), |
1261 | sizeof(bios_machineid)); | |
e70c9d5e | 1262 | |
1da177e4 | 1263 | /* |
dec63ec3 | 1264 | * Get SMM Dell signature |
1da177e4 | 1265 | */ |
7e0fa31d DT |
1266 | if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) && |
1267 | i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) { | |
60e71aaf | 1268 | pr_err("unable to get SMM Dell signature\n"); |
e70c9d5e DT |
1269 | if (!force) |
1270 | return -ENODEV; | |
1da177e4 | 1271 | } |
1da177e4 | 1272 | |
8f21d8e9 PR |
1273 | /* |
1274 | * Set fan multiplier and maximal fan speed from dmi config | |
1275 | * Values specified in module parameters override values from dmi | |
1276 | */ | |
26d09382 | 1277 | id = dmi_first_match(i8k_dmi_table); |
81474fc2 GR |
1278 | if (id && id->driver_data) { |
1279 | const struct i8k_config_data *conf = id->driver_data; | |
8f21d8e9 PR |
1280 | if (!fan_mult && conf->fan_mult) |
1281 | fan_mult = conf->fan_mult; | |
1282 | if (!fan_max && conf->fan_max) | |
1283 | fan_max = conf->fan_max; | |
81474fc2 | 1284 | } |
8f21d8e9 PR |
1285 | |
1286 | i8k_fan_max = fan_max ? : I8K_FAN_HIGH; /* Must not be 0 */ | |
81474fc2 | 1287 | i8k_pwm_mult = DIV_ROUND_UP(255, i8k_fan_max); |
26d09382 | 1288 | |
afe45277 GM |
1289 | fan_control = dmi_first_match(i8k_whitelist_fan_control); |
1290 | if (fan_control && fan_control->driver_data) { | |
1291 | const struct i8k_fan_control_data *data = fan_control->driver_data; | |
1292 | ||
1293 | manual_fan = data->manual_fan; | |
1294 | auto_fan = data->auto_fan; | |
1295 | pr_info("enabling support for setting automatic/manual fan control\n"); | |
1296 | } | |
1297 | ||
8f21d8e9 PR |
1298 | if (!fan_mult) { |
1299 | /* | |
1300 | * Autodetect fan multiplier based on nominal rpm | |
1301 | * If fan reports rpm value too high then set multiplier to 1 | |
1302 | */ | |
1303 | for (fan = 0; fan < 2; ++fan) { | |
1304 | ret = i8k_get_fan_nominal_speed(fan, i8k_fan_max); | |
1305 | if (ret < 0) | |
1306 | continue; | |
1307 | if (ret > I8K_FAN_MAX_RPM) | |
1308 | i8k_fan_mult = 1; | |
1309 | break; | |
1310 | } | |
1311 | } else { | |
1312 | /* Fan multiplier was specified in module param or in dmi */ | |
1313 | i8k_fan_mult = fan_mult; | |
1314 | } | |
1315 | ||
dec63ec3 | 1316 | return 0; |
1da177e4 LT |
1317 | } |
1318 | ||
8378b924 | 1319 | static int __init i8k_init(void) |
1da177e4 | 1320 | { |
949a9d70 | 1321 | int err; |
dec63ec3 DT |
1322 | |
1323 | /* Are we running on an supported laptop? */ | |
e70c9d5e | 1324 | if (i8k_probe()) |
dec63ec3 | 1325 | return -ENODEV; |
dec63ec3 | 1326 | |
949a9d70 JD |
1327 | err = i8k_init_hwmon(); |
1328 | if (err) | |
039ae585 | 1329 | return err; |
949a9d70 | 1330 | |
039ae585 | 1331 | i8k_init_procfs(); |
dec63ec3 | 1332 | return 0; |
1da177e4 LT |
1333 | } |
1334 | ||
8378b924 | 1335 | static void __exit i8k_exit(void) |
1da177e4 | 1336 | { |
82ba1d3f | 1337 | hwmon_device_unregister(i8k_hwmon_dev); |
039ae585 | 1338 | i8k_exit_procfs(); |
1da177e4 | 1339 | } |
1da177e4 | 1340 | |
8378b924 DT |
1341 | module_init(i8k_init); |
1342 | module_exit(i8k_exit); |