hwmon: (dell-smm) Disallow fan_type() calls on broken machines
[deliverable/linux.git] / drivers / hwmon / dell-smm-hwmon.c
CommitLineData
1da177e4 1/*
a5afba16 2 * dell-smm-hwmon.c -- Linux driver for accessing the SMM BIOS on Dell laptops.
1da177e4
LT
3 *
4 * Copyright (C) 2001 Massimo Dal Zotto <dz@debian.org>
5 *
949a9d70 6 * Hwmon integration:
7c81c60f 7 * Copyright (C) 2011 Jean Delvare <jdelvare@suse.de>
564132d9 8 * Copyright (C) 2013, 2014 Guenter Roeck <linux@roeck-us.net>
a5afba16 9 * Copyright (C) 2014, 2015 Pali Rohár <pali.rohar@gmail.com>
949a9d70 10 *
1da177e4
LT
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2, or (at your option) any
14 * later version.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 */
21
60e71aaf
GR
22#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
564132d9 24#include <linux/delay.h>
1da177e4
LT
25#include <linux/module.h>
26#include <linux/types.h>
27#include <linux/init.h>
28#include <linux/proc_fs.h>
352f8f8b 29#include <linux/seq_file.h>
e70c9d5e 30#include <linux/dmi.h>
7e80d0d0 31#include <linux/capability.h>
613655fa 32#include <linux/mutex.h>
949a9d70
JD
33#include <linux/hwmon.h>
34#include <linux/hwmon-sysfs.h>
12186f51
GR
35#include <linux/uaccess.h>
36#include <linux/io.h>
f36fdb9f 37#include <linux/sched.h>
053ea640 38#include <linux/ctype.h>
1da177e4
LT
39
40#include <linux/i8k.h>
41
1da177e4
LT
42#define I8K_SMM_FN_STATUS 0x0025
43#define I8K_SMM_POWER_STATUS 0x0069
44#define I8K_SMM_SET_FAN 0x01a3
45#define I8K_SMM_GET_FAN 0x00a3
46#define I8K_SMM_GET_SPEED 0x02a3
f989e554 47#define I8K_SMM_GET_FAN_TYPE 0x03a3
8f21d8e9 48#define I8K_SMM_GET_NOM_SPEED 0x04a3
1da177e4 49#define I8K_SMM_GET_TEMP 0x10a3
5114b474 50#define I8K_SMM_GET_TEMP_TYPE 0x11a3
7e0fa31d
DT
51#define I8K_SMM_GET_DELL_SIG1 0xfea3
52#define I8K_SMM_GET_DELL_SIG2 0xffa3
1da177e4
LT
53
54#define I8K_FAN_MULT 30
8f21d8e9 55#define I8K_FAN_MAX_RPM 30000
1da177e4
LT
56#define I8K_MAX_TEMP 127
57
58#define I8K_FN_NONE 0x00
59#define I8K_FN_UP 0x01
60#define I8K_FN_DOWN 0x02
61#define I8K_FN_MUTE 0x04
62#define I8K_FN_MASK 0x07
63#define I8K_FN_SHIFT 8
64
65#define I8K_POWER_AC 0x05
66#define I8K_POWER_BATTERY 0x01
67
613655fa 68static DEFINE_MUTEX(i8k_mutex);
e70c9d5e 69static char bios_version[4];
7613663c 70static char bios_machineid[16];
949a9d70 71static struct device *i8k_hwmon_dev;
82ba1d3f 72static u32 i8k_hwmon_flags;
8f21d8e9 73static uint i8k_fan_mult = I8K_FAN_MULT;
7f69fb03
PR
74static uint i8k_pwm_mult;
75static uint i8k_fan_max = I8K_FAN_HIGH;
2744d2fd 76static bool disallow_fan_type_call;
82ba1d3f
GR
77
78#define I8K_HWMON_HAVE_TEMP1 (1 << 0)
d83c39de
GR
79#define I8K_HWMON_HAVE_TEMP2 (1 << 1)
80#define I8K_HWMON_HAVE_TEMP3 (1 << 2)
81#define I8K_HWMON_HAVE_TEMP4 (1 << 3)
82#define I8K_HWMON_HAVE_FAN1 (1 << 4)
83#define I8K_HWMON_HAVE_FAN2 (1 << 5)
1da177e4
LT
84
85MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)");
a5afba16 86MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
039ae585 87MODULE_DESCRIPTION("Dell laptop SMM BIOS hwmon driver");
1da177e4 88MODULE_LICENSE("GPL");
a5afba16 89MODULE_ALIAS("i8k");
1da177e4 90
90ab5ee9 91static bool force;
1da177e4
LT
92module_param(force, bool, 0);
93MODULE_PARM_DESC(force, "Force loading without checking for supported models");
94
90ab5ee9 95static bool ignore_dmi;
e70c9d5e
DT
96module_param(ignore_dmi, bool, 0);
97MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match");
98
039ae585 99#if IS_ENABLED(CONFIG_I8K)
7613663c 100static bool restricted = true;
1da177e4 101module_param(restricted, bool, 0);
7613663c 102MODULE_PARM_DESC(restricted, "Restrict fan control and serial number to CAP_SYS_ADMIN (default: 1)");
1da177e4 103
90ab5ee9 104static bool power_status;
1da177e4 105module_param(power_status, bool, 0600);
7613663c 106MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k (default: 0)");
039ae585 107#endif
1da177e4 108
8f21d8e9 109static uint fan_mult;
7f69fb03 110module_param(fan_mult, uint, 0);
8f21d8e9 111MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with (default: autodetect)");
4ed99a27 112
8f21d8e9 113static uint fan_max;
7f69fb03 114module_param(fan_max, uint, 0);
8f21d8e9 115MODULE_PARM_DESC(fan_max, "Maximum configurable fan speed (default: autodetect)");
81474fc2 116
8378b924 117struct smm_regs {
dec63ec3 118 unsigned int eax;
12186f51
GR
119 unsigned int ebx __packed;
120 unsigned int ecx __packed;
121 unsigned int edx __packed;
122 unsigned int esi __packed;
123 unsigned int edi __packed;
8378b924 124};
1da177e4 125
1855256c 126static inline const char *i8k_get_dmi_data(int field)
e70c9d5e 127{
1855256c 128 const char *dmi_data = dmi_get_system_info(field);
4f005551
DT
129
130 return dmi_data && *dmi_data ? dmi_data : "?";
e70c9d5e 131}
1da177e4
LT
132
133/*
134 * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
135 */
8378b924 136static int i8k_smm(struct smm_regs *regs)
1da177e4 137{
dec63ec3
DT
138 int rc;
139 int eax = regs->eax;
f36fdb9f
GR
140 cpumask_var_t old_mask;
141
142 /* SMM requires CPU 0 */
143 if (!alloc_cpumask_var(&old_mask, GFP_KERNEL))
144 return -ENOMEM;
145 cpumask_copy(old_mask, &current->cpus_allowed);
6d827fbc
GR
146 rc = set_cpus_allowed_ptr(current, cpumask_of(0));
147 if (rc)
148 goto out;
f36fdb9f
GR
149 if (smp_processor_id() != 0) {
150 rc = -EBUSY;
151 goto out;
152 }
dec63ec3 153
fe04f22f 154#if defined(CONFIG_X86_64)
22d3243d 155 asm volatile("pushq %%rax\n\t"
fe04f22f
BS
156 "movl 0(%%rax),%%edx\n\t"
157 "pushq %%rdx\n\t"
158 "movl 4(%%rax),%%ebx\n\t"
159 "movl 8(%%rax),%%ecx\n\t"
160 "movl 12(%%rax),%%edx\n\t"
161 "movl 16(%%rax),%%esi\n\t"
162 "movl 20(%%rax),%%edi\n\t"
163 "popq %%rax\n\t"
164 "out %%al,$0xb2\n\t"
165 "out %%al,$0x84\n\t"
166 "xchgq %%rax,(%%rsp)\n\t"
167 "movl %%ebx,4(%%rax)\n\t"
168 "movl %%ecx,8(%%rax)\n\t"
169 "movl %%edx,12(%%rax)\n\t"
170 "movl %%esi,16(%%rax)\n\t"
171 "movl %%edi,20(%%rax)\n\t"
172 "popq %%rdx\n\t"
173 "movl %%edx,0(%%rax)\n\t"
bc1f419c
LT
174 "pushfq\n\t"
175 "popq %%rax\n\t"
fe04f22f 176 "andl $1,%%eax\n"
12186f51 177 : "=a"(rc)
fe04f22f
BS
178 : "a"(regs)
179 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
180#else
22d3243d 181 asm volatile("pushl %%eax\n\t"
dec63ec3
DT
182 "movl 0(%%eax),%%edx\n\t"
183 "push %%edx\n\t"
184 "movl 4(%%eax),%%ebx\n\t"
185 "movl 8(%%eax),%%ecx\n\t"
186 "movl 12(%%eax),%%edx\n\t"
187 "movl 16(%%eax),%%esi\n\t"
188 "movl 20(%%eax),%%edi\n\t"
189 "popl %%eax\n\t"
190 "out %%al,$0xb2\n\t"
191 "out %%al,$0x84\n\t"
192 "xchgl %%eax,(%%esp)\n\t"
193 "movl %%ebx,4(%%eax)\n\t"
194 "movl %%ecx,8(%%eax)\n\t"
195 "movl %%edx,12(%%eax)\n\t"
196 "movl %%esi,16(%%eax)\n\t"
197 "movl %%edi,20(%%eax)\n\t"
198 "popl %%edx\n\t"
199 "movl %%edx,0(%%eax)\n\t"
200 "lahf\n\t"
201 "shrl $8,%%eax\n\t"
6b4e81db 202 "andl $1,%%eax\n"
12186f51 203 : "=a"(rc)
dec63ec3
DT
204 : "a"(regs)
205 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
fe04f22f 206#endif
8378b924 207 if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax)
f36fdb9f 208 rc = -EINVAL;
dec63ec3 209
f36fdb9f
GR
210out:
211 set_cpus_allowed_ptr(current, old_mask);
212 free_cpumask_var(old_mask);
213 return rc;
1da177e4
LT
214}
215
1da177e4
LT
216/*
217 * Read the fan status.
218 */
219static int i8k_get_fan_status(int fan)
220{
8378b924 221 struct smm_regs regs = { .eax = I8K_SMM_GET_FAN, };
1da177e4 222
dec63ec3 223 regs.ebx = fan & 0xff;
8378b924 224 return i8k_smm(&regs) ? : regs.eax & 0xff;
1da177e4
LT
225}
226
227/*
228 * Read the fan speed in RPM.
229 */
230static int i8k_get_fan_speed(int fan)
231{
8378b924 232 struct smm_regs regs = { .eax = I8K_SMM_GET_SPEED, };
1da177e4 233
dec63ec3 234 regs.ebx = fan & 0xff;
26d09382 235 return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
1da177e4
LT
236}
237
f989e554
PR
238/*
239 * Read the fan type.
240 */
241static int i8k_get_fan_type(int fan)
242{
243 struct smm_regs regs = { .eax = I8K_SMM_GET_FAN_TYPE, };
244
2744d2fd
PR
245 if (disallow_fan_type_call)
246 return -EINVAL;
247
f989e554
PR
248 regs.ebx = fan & 0xff;
249 return i8k_smm(&regs) ? : regs.eax & 0xff;
250}
251
8f21d8e9
PR
252/*
253 * Read the fan nominal rpm for specific fan speed.
254 */
255static int i8k_get_fan_nominal_speed(int fan, int speed)
256{
257 struct smm_regs regs = { .eax = I8K_SMM_GET_NOM_SPEED, };
258
259 regs.ebx = (fan & 0xff) | (speed << 8);
260 return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
261}
262
1da177e4
LT
263/*
264 * Set the fan speed (off, low, high). Returns the new fan status.
265 */
266static int i8k_set_fan(int fan, int speed)
267{
8378b924 268 struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
1da177e4 269
81474fc2 270 speed = (speed < 0) ? 0 : ((speed > i8k_fan_max) ? i8k_fan_max : speed);
dec63ec3 271 regs.ebx = (fan & 0xff) | (speed << 8);
1da177e4 272
8378b924 273 return i8k_smm(&regs) ? : i8k_get_fan_status(fan);
1da177e4
LT
274}
275
5114b474
PR
276static int i8k_get_temp_type(int sensor)
277{
278 struct smm_regs regs = { .eax = I8K_SMM_GET_TEMP_TYPE, };
279
280 regs.ebx = sensor & 0xff;
281 return i8k_smm(&regs) ? : regs.eax & 0xff;
282}
283
1da177e4
LT
284/*
285 * Read the cpu temperature.
286 */
564132d9 287static int _i8k_get_temp(int sensor)
1da177e4 288{
564132d9
GR
289 struct smm_regs regs = {
290 .eax = I8K_SMM_GET_TEMP,
291 .ebx = sensor & 0xff,
292 };
1da177e4 293
564132d9
GR
294 return i8k_smm(&regs) ? : regs.eax & 0xff;
295}
8378b924 296
564132d9
GR
297static int i8k_get_temp(int sensor)
298{
299 int temp = _i8k_get_temp(sensor);
1da177e4 300
dec63ec3
DT
301 /*
302 * Sometimes the temperature sensor returns 0x99, which is out of range.
564132d9 303 * In this case we retry (once) before returning an error.
dec63ec3
DT
304 # 1003655137 00000058 00005a4b
305 # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
306 # 1003655139 00000054 00005c52
307 */
564132d9
GR
308 if (temp == 0x99) {
309 msleep(100);
310 temp = _i8k_get_temp(sensor);
dec63ec3 311 }
564132d9
GR
312 /*
313 * Return -ENODATA for all invalid temperatures.
314 *
315 * Known instances are the 0x99 value as seen above as well as
316 * 0xc1 (193), which may be returned when trying to read the GPU
317 * temperature if the system supports a GPU and it is currently
318 * turned off.
319 */
723493ca 320 if (temp > I8K_MAX_TEMP)
83d514d7 321 return -ENODATA;
1da177e4 322
dec63ec3 323 return temp;
1da177e4
LT
324}
325
7e0fa31d 326static int i8k_get_dell_signature(int req_fn)
1da177e4 327{
7e0fa31d 328 struct smm_regs regs = { .eax = req_fn, };
dec63ec3 329 int rc;
1da177e4 330
12186f51
GR
331 rc = i8k_smm(&regs);
332 if (rc < 0)
dec63ec3 333 return rc;
1da177e4 334
8378b924 335 return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
1da177e4
LT
336}
337
039ae585
PR
338#if IS_ENABLED(CONFIG_I8K)
339
340/*
341 * Read the Fn key status.
342 */
343static int i8k_get_fn_status(void)
344{
345 struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, };
346 int rc;
347
348 rc = i8k_smm(&regs);
349 if (rc < 0)
350 return rc;
351
352 switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
353 case I8K_FN_UP:
354 return I8K_VOL_UP;
355 case I8K_FN_DOWN:
356 return I8K_VOL_DOWN;
357 case I8K_FN_MUTE:
358 return I8K_VOL_MUTE;
359 default:
360 return 0;
361 }
362}
363
364/*
365 * Read the power status.
366 */
367static int i8k_get_power_status(void)
368{
369 struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, };
370 int rc;
371
372 rc = i8k_smm(&regs);
373 if (rc < 0)
374 return rc;
375
376 return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY;
377}
378
379/*
380 * Procfs interface
381 */
382
d79b6f4d
FW
383static int
384i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg)
1da177e4 385{
e70c9d5e 386 int val = 0;
dec63ec3
DT
387 int speed;
388 unsigned char buff[16];
389 int __user *argp = (int __user *)arg;
390
391 if (!argp)
392 return -EINVAL;
393
394 switch (cmd) {
395 case I8K_BIOS_VERSION:
053ea640
PR
396 if (!isdigit(bios_version[0]) || !isdigit(bios_version[1]) ||
397 !isdigit(bios_version[2]))
398 return -EINVAL;
399
e551b152
GR
400 val = (bios_version[0] << 16) |
401 (bios_version[1] << 8) | bios_version[2];
dec63ec3
DT
402 break;
403
404 case I8K_MACHINE_ID:
7613663c
PR
405 if (restricted && !capable(CAP_SYS_ADMIN))
406 return -EPERM;
407
408 memset(buff, 0, sizeof(buff));
409 strlcpy(buff, bios_machineid, sizeof(buff));
dec63ec3
DT
410 break;
411
412 case I8K_FN_STATUS:
413 val = i8k_get_fn_status();
414 break;
415
416 case I8K_POWER_STATUS:
417 val = i8k_get_power_status();
418 break;
419
420 case I8K_GET_TEMP:
7e0fa31d 421 val = i8k_get_temp(0);
dec63ec3
DT
422 break;
423
424 case I8K_GET_SPEED:
8378b924 425 if (copy_from_user(&val, argp, sizeof(int)))
dec63ec3 426 return -EFAULT;
8378b924 427
dec63ec3
DT
428 val = i8k_get_fan_speed(val);
429 break;
1da177e4 430
dec63ec3 431 case I8K_GET_FAN:
8378b924 432 if (copy_from_user(&val, argp, sizeof(int)))
dec63ec3 433 return -EFAULT;
8378b924 434
dec63ec3
DT
435 val = i8k_get_fan_status(val);
436 break;
1da177e4 437
dec63ec3 438 case I8K_SET_FAN:
8378b924 439 if (restricted && !capable(CAP_SYS_ADMIN))
dec63ec3 440 return -EPERM;
8378b924
DT
441
442 if (copy_from_user(&val, argp, sizeof(int)))
dec63ec3 443 return -EFAULT;
8378b924
DT
444
445 if (copy_from_user(&speed, argp + 1, sizeof(int)))
dec63ec3 446 return -EFAULT;
8378b924 447
dec63ec3
DT
448 val = i8k_set_fan(val, speed);
449 break;
1da177e4 450
dec63ec3
DT
451 default:
452 return -EINVAL;
1da177e4 453 }
1da177e4 454
8378b924 455 if (val < 0)
dec63ec3 456 return val;
1da177e4 457
dec63ec3
DT
458 switch (cmd) {
459 case I8K_BIOS_VERSION:
8378b924 460 if (copy_to_user(argp, &val, 4))
dec63ec3 461 return -EFAULT;
8378b924 462
dec63ec3
DT
463 break;
464 case I8K_MACHINE_ID:
8378b924 465 if (copy_to_user(argp, buff, 16))
dec63ec3 466 return -EFAULT;
8378b924 467
dec63ec3
DT
468 break;
469 default:
8378b924 470 if (copy_to_user(argp, &val, sizeof(int)))
dec63ec3 471 return -EFAULT;
8378b924 472
dec63ec3 473 break;
1da177e4 474 }
1da177e4 475
dec63ec3 476 return 0;
1da177e4
LT
477}
478
d79b6f4d
FW
479static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
480{
481 long ret;
482
613655fa 483 mutex_lock(&i8k_mutex);
d79b6f4d 484 ret = i8k_ioctl_unlocked(fp, cmd, arg);
613655fa 485 mutex_unlock(&i8k_mutex);
d79b6f4d
FW
486
487 return ret;
488}
489
1da177e4
LT
490/*
491 * Print the information for /proc/i8k.
492 */
352f8f8b 493static int i8k_proc_show(struct seq_file *seq, void *offset)
1da177e4 494{
352f8f8b 495 int fn_key, cpu_temp, ac_power;
dec63ec3
DT
496 int left_fan, right_fan, left_speed, right_speed;
497
96de0e25
JE
498 cpu_temp = i8k_get_temp(0); /* 11100 µs */
499 left_fan = i8k_get_fan_status(I8K_FAN_LEFT); /* 580 µs */
500 right_fan = i8k_get_fan_status(I8K_FAN_RIGHT); /* 580 µs */
501 left_speed = i8k_get_fan_speed(I8K_FAN_LEFT); /* 580 µs */
502 right_speed = i8k_get_fan_speed(I8K_FAN_RIGHT); /* 580 µs */
503 fn_key = i8k_get_fn_status(); /* 750 µs */
8378b924 504 if (power_status)
96de0e25 505 ac_power = i8k_get_power_status(); /* 14700 µs */
8378b924 506 else
dec63ec3 507 ac_power = -1;
dec63ec3
DT
508
509 /*
510 * Info:
511 *
512 * 1) Format version (this will change if format changes)
513 * 2) BIOS version
514 * 3) BIOS machine ID
515 * 4) Cpu temperature
516 * 5) Left fan status
517 * 6) Right fan status
518 * 7) Left fan speed
519 * 8) Right fan speed
520 * 9) AC power
521 * 10) Fn Key status
522 */
3a267d3b
JP
523 seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
524 I8K_PROC_FMT,
525 bios_version,
7613663c 526 (restricted && !capable(CAP_SYS_ADMIN)) ? "-1" : bios_machineid,
3a267d3b
JP
527 cpu_temp,
528 left_fan, right_fan, left_speed, right_speed,
529 ac_power, fn_key);
530
531 return 0;
1da177e4
LT
532}
533
352f8f8b 534static int i8k_open_fs(struct inode *inode, struct file *file)
1da177e4 535{
352f8f8b 536 return single_open(file, i8k_proc_show, NULL);
1da177e4
LT
537}
538
039ae585
PR
539static const struct file_operations i8k_fops = {
540 .owner = THIS_MODULE,
541 .open = i8k_open_fs,
542 .read = seq_read,
543 .llseek = seq_lseek,
544 .release = single_release,
545 .unlocked_ioctl = i8k_ioctl,
546};
547
548static void __init i8k_init_procfs(void)
549{
550 /* Register the proc entry */
551 proc_create("i8k", 0, NULL, &i8k_fops);
552}
553
554static void __exit i8k_exit_procfs(void)
555{
556 remove_proc_entry("i8k", NULL);
557}
558
559#else
560
561static inline void __init i8k_init_procfs(void)
562{
563}
564
565static inline void __exit i8k_exit_procfs(void)
566{
567}
568
569#endif
949a9d70
JD
570
571/*
572 * Hwmon interface
573 */
574
5114b474
PR
575static ssize_t i8k_hwmon_show_temp_label(struct device *dev,
576 struct device_attribute *devattr,
577 char *buf)
578{
579 static const char * const labels[] = {
580 "CPU",
581 "GPU",
582 "SODIMM",
583 "Other",
584 "Ambient",
585 "Other",
586 };
587 int index = to_sensor_dev_attr(devattr)->index;
588 int type;
589
590 type = i8k_get_temp_type(index);
591 if (type < 0)
592 return type;
593 if (type >= ARRAY_SIZE(labels))
594 type = ARRAY_SIZE(labels) - 1;
595 return sprintf(buf, "%s\n", labels[type]);
596}
597
949a9d70
JD
598static ssize_t i8k_hwmon_show_temp(struct device *dev,
599 struct device_attribute *devattr,
600 char *buf)
601{
d83c39de
GR
602 int index = to_sensor_dev_attr(devattr)->index;
603 int temp;
949a9d70 604
d83c39de
GR
605 temp = i8k_get_temp(index);
606 if (temp < 0)
607 return temp;
608 return sprintf(buf, "%d\n", temp * 1000);
949a9d70
JD
609}
610
f989e554
PR
611static ssize_t i8k_hwmon_show_fan_label(struct device *dev,
612 struct device_attribute *devattr,
613 char *buf)
614{
615 static const char * const labels[] = {
616 "Processor Fan",
617 "Motherboard Fan",
618 "Video Fan",
619 "Power Supply Fan",
620 "Chipset Fan",
621 "Other Fan",
622 };
623 int index = to_sensor_dev_attr(devattr)->index;
624 bool dock = false;
625 int type;
626
627 type = i8k_get_fan_type(index);
628 if (type < 0)
629 return type;
630
631 if (type & 0x10) {
632 dock = true;
633 type &= 0x0F;
634 }
635
636 if (type >= ARRAY_SIZE(labels))
637 type = (ARRAY_SIZE(labels) - 1);
638
639 return sprintf(buf, "%s%s\n", (dock ? "Docking " : ""), labels[type]);
640}
641
949a9d70
JD
642static ssize_t i8k_hwmon_show_fan(struct device *dev,
643 struct device_attribute *devattr,
644 char *buf)
645{
646 int index = to_sensor_dev_attr(devattr)->index;
647 int fan_speed;
648
649 fan_speed = i8k_get_fan_speed(index);
650 if (fan_speed < 0)
651 return fan_speed;
652 return sprintf(buf, "%d\n", fan_speed);
653}
654
e7f5f275
GR
655static ssize_t i8k_hwmon_show_pwm(struct device *dev,
656 struct device_attribute *devattr,
657 char *buf)
658{
659 int index = to_sensor_dev_attr(devattr)->index;
660 int status;
661
662 status = i8k_get_fan_status(index);
663 if (status < 0)
664 return -EIO;
81474fc2 665 return sprintf(buf, "%d\n", clamp_val(status * i8k_pwm_mult, 0, 255));
e7f5f275
GR
666}
667
668static ssize_t i8k_hwmon_set_pwm(struct device *dev,
669 struct device_attribute *attr,
670 const char *buf, size_t count)
671{
672 int index = to_sensor_dev_attr(attr)->index;
673 unsigned long val;
674 int err;
675
676 err = kstrtoul(buf, 10, &val);
677 if (err)
678 return err;
81474fc2 679 val = clamp_val(DIV_ROUND_CLOSEST(val, i8k_pwm_mult), 0, i8k_fan_max);
e7f5f275
GR
680
681 mutex_lock(&i8k_mutex);
682 err = i8k_set_fan(index, val);
683 mutex_unlock(&i8k_mutex);
684
685 return err < 0 ? -EIO : count;
686}
687
d83c39de 688static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 0);
5114b474
PR
689static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
690 0);
d83c39de 691static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 1);
5114b474
PR
692static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
693 1);
d83c39de 694static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 2);
5114b474
PR
695static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
696 2);
d83c39de 697static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 3);
5114b474
PR
698static SENSOR_DEVICE_ATTR(temp4_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
699 3);
f989e554
PR
700static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, i8k_hwmon_show_fan, NULL, 0);
701static SENSOR_DEVICE_ATTR(fan1_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
702 0);
e7f5f275 703static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
f989e554 704 i8k_hwmon_set_pwm, 0);
949a9d70 705static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
f989e554
PR
706 1);
707static SENSOR_DEVICE_ATTR(fan2_label, S_IRUGO, i8k_hwmon_show_fan_label, NULL,
708 1);
e7f5f275 709static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
f989e554 710 i8k_hwmon_set_pwm, 1);
82ba1d3f
GR
711
712static struct attribute *i8k_attrs[] = {
d83c39de 713 &sensor_dev_attr_temp1_input.dev_attr.attr, /* 0 */
5114b474
PR
714 &sensor_dev_attr_temp1_label.dev_attr.attr, /* 1 */
715 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 2 */
716 &sensor_dev_attr_temp2_label.dev_attr.attr, /* 3 */
717 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 4 */
718 &sensor_dev_attr_temp3_label.dev_attr.attr, /* 5 */
719 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 6 */
720 &sensor_dev_attr_temp4_label.dev_attr.attr, /* 7 */
721 &sensor_dev_attr_fan1_input.dev_attr.attr, /* 8 */
f989e554
PR
722 &sensor_dev_attr_fan1_label.dev_attr.attr, /* 9 */
723 &sensor_dev_attr_pwm1.dev_attr.attr, /* 10 */
724 &sensor_dev_attr_fan2_input.dev_attr.attr, /* 11 */
725 &sensor_dev_attr_fan2_label.dev_attr.attr, /* 12 */
726 &sensor_dev_attr_pwm2.dev_attr.attr, /* 13 */
82ba1d3f
GR
727 NULL
728};
949a9d70 729
82ba1d3f
GR
730static umode_t i8k_is_visible(struct kobject *kobj, struct attribute *attr,
731 int index)
949a9d70 732{
2744d2fd
PR
733 if (disallow_fan_type_call &&
734 (index == 9 || index == 12))
735 return 0;
5114b474
PR
736 if (index >= 0 && index <= 1 &&
737 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP1))
82ba1d3f 738 return 0;
5114b474
PR
739 if (index >= 2 && index <= 3 &&
740 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP2))
d83c39de 741 return 0;
5114b474
PR
742 if (index >= 4 && index <= 5 &&
743 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP3))
d83c39de 744 return 0;
5114b474
PR
745 if (index >= 6 && index <= 7 &&
746 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP4))
d83c39de 747 return 0;
f989e554 748 if (index >= 8 && index <= 10 &&
82ba1d3f
GR
749 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN1))
750 return 0;
f989e554 751 if (index >= 11 && index <= 13 &&
82ba1d3f
GR
752 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN2))
753 return 0;
754
755 return attr->mode;
949a9d70
JD
756}
757
82ba1d3f
GR
758static const struct attribute_group i8k_group = {
759 .attrs = i8k_attrs,
760 .is_visible = i8k_is_visible,
761};
762__ATTRIBUTE_GROUPS(i8k);
763
949a9d70
JD
764static int __init i8k_init_hwmon(void)
765{
766 int err;
767
82ba1d3f 768 i8k_hwmon_flags = 0;
949a9d70 769
672af223
PR
770 /* CPU temperature attributes, if temperature type is OK */
771 err = i8k_get_temp_type(0);
772 if (err >= 0)
82ba1d3f 773 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP1;
d83c39de 774 /* check for additional temperature sensors */
672af223
PR
775 err = i8k_get_temp_type(1);
776 if (err >= 0)
d83c39de 777 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP2;
672af223
PR
778 err = i8k_get_temp_type(2);
779 if (err >= 0)
d83c39de 780 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP3;
672af223
PR
781 err = i8k_get_temp_type(3);
782 if (err >= 0)
d83c39de 783 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP4;
949a9d70 784
f989e554
PR
785 /* First fan attributes, if fan type is OK */
786 err = i8k_get_fan_type(0);
82ba1d3f
GR
787 if (err >= 0)
788 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN1;
949a9d70 789
f989e554
PR
790 /* Second fan attributes, if fan type is OK */
791 err = i8k_get_fan_type(1);
82ba1d3f
GR
792 if (err >= 0)
793 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN2;
949a9d70 794
9026cae1 795 i8k_hwmon_dev = hwmon_device_register_with_groups(NULL, "dell_smm",
039ae585 796 NULL, i8k_groups);
82ba1d3f
GR
797 if (IS_ERR(i8k_hwmon_dev)) {
798 err = PTR_ERR(i8k_hwmon_dev);
799 i8k_hwmon_dev = NULL;
800 pr_err("hwmon registration failed (%d)\n", err);
801 return err;
802 }
949a9d70 803 return 0;
949a9d70
JD
804}
805
81474fc2 806struct i8k_config_data {
7f69fb03
PR
807 uint fan_mult;
808 uint fan_max;
81474fc2
GR
809};
810
811enum i8k_configs {
7b883446
GR
812 DELL_LATITUDE_D520,
813 DELL_PRECISION_490,
81474fc2 814 DELL_STUDIO,
34ae40f6 815 DELL_XPS,
81474fc2
GR
816};
817
818static const struct i8k_config_data i8k_config_data[] = {
7b883446
GR
819 [DELL_LATITUDE_D520] = {
820 .fan_mult = 1,
821 .fan_max = I8K_FAN_TURBO,
822 },
823 [DELL_PRECISION_490] = {
824 .fan_mult = 1,
825 .fan_max = I8K_FAN_TURBO,
826 },
81474fc2
GR
827 [DELL_STUDIO] = {
828 .fan_mult = 1,
829 .fan_max = I8K_FAN_HIGH,
830 },
34ae40f6 831 [DELL_XPS] = {
81474fc2
GR
832 .fan_mult = 1,
833 .fan_max = I8K_FAN_HIGH,
834 },
835};
836
12186f51 837static struct dmi_system_id i8k_dmi_table[] __initdata = {
e70c9d5e
DT
838 {
839 .ident = "Dell Inspiron",
840 .matches = {
841 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
842 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
843 },
844 },
845 {
846 .ident = "Dell Latitude",
847 .matches = {
848 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
849 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
850 },
851 },
7e0fa31d
DT
852 {
853 .ident = "Dell Inspiron 2",
854 .matches = {
855 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
856 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
857 },
858 },
7b883446
GR
859 {
860 .ident = "Dell Latitude D520",
861 .matches = {
862 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
863 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D520"),
864 },
865 .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_D520],
866 },
7e0fa31d
DT
867 {
868 .ident = "Dell Latitude 2",
869 .matches = {
870 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
871 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
872 },
873 },
a9000d03
NW
874 { /* UK Inspiron 6400 */
875 .ident = "Dell Inspiron 3",
876 .matches = {
877 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
878 DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
879 },
880 },
48103c52
FS
881 {
882 .ident = "Dell Inspiron 3",
883 .matches = {
884 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
885 DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
886 },
887 },
7b883446
GR
888 {
889 .ident = "Dell Precision 490",
890 .matches = {
891 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
892 DMI_MATCH(DMI_PRODUCT_NAME,
893 "Precision WorkStation 490"),
894 },
895 .driver_data = (void *)&i8k_config_data[DELL_PRECISION_490],
896 },
7ab21a86
AS
897 {
898 .ident = "Dell Precision",
899 .matches = {
900 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
901 DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
902 },
903 },
bef2a508
FH
904 {
905 .ident = "Dell Vostro",
906 .matches = {
907 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
908 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
909 },
910 },
9aa5b018
AC
911 {
912 .ident = "Dell XPS421",
913 .matches = {
914 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
915 DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"),
916 },
917 },
ff457bde
GR
918 {
919 .ident = "Dell Studio",
920 .matches = {
921 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
922 DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
923 },
81474fc2 924 .driver_data = (void *)&i8k_config_data[DELL_STUDIO],
ff457bde 925 },
34ae40f6
GR
926 {
927 .ident = "Dell XPS 13",
928 .matches = {
929 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
930 DMI_MATCH(DMI_PRODUCT_NAME, "XPS13"),
931 },
932 .driver_data = (void *)&i8k_config_data[DELL_XPS],
933 },
919a0304
GR
934 {
935 .ident = "Dell XPS M140",
936 .matches = {
937 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
938 DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
939 },
34ae40f6 940 .driver_data = (void *)&i8k_config_data[DELL_XPS],
919a0304 941 },
12186f51 942 { }
e70c9d5e 943};
1da177e4 944
148b1fda
PR
945MODULE_DEVICE_TABLE(dmi, i8k_dmi_table);
946
2744d2fd
PR
947/*
948 * On some machines once I8K_SMM_GET_FAN_TYPE is issued then CPU fan speed
949 * randomly going up and down due to bug in Dell SMM or BIOS. Here is blacklist
950 * of affected Dell machines for which we disallow I8K_SMM_GET_FAN_TYPE call.
951 * See bug: https://bugzilla.kernel.org/show_bug.cgi?id=100121
952 */
953static struct dmi_system_id i8k_blacklist_fan_type_dmi_table[] __initdata = {
6220f4eb 954 {
6220f4eb
TL
955 .ident = "Dell Studio XPS 8000",
956 .matches = {
957 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
958 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8000"),
959 },
960 },
a4b45b25 961 {
a4b45b25
PR
962 .ident = "Dell Studio XPS 8100",
963 .matches = {
964 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
965 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8100"),
966 },
967 },
2744d2fd
PR
968 {
969 .ident = "Dell Inspiron 580",
970 .matches = {
971 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
972 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 580 "),
973 },
974 },
a4b45b25
PR
975 { }
976};
977
1da177e4
LT
978/*
979 * Probe for the presence of a supported laptop.
980 */
981static int __init i8k_probe(void)
982{
26d09382 983 const struct dmi_system_id *id;
8f21d8e9 984 int fan, ret;
dec63ec3 985
1da177e4 986 /*
dec63ec3 987 * Get DMI information
1da177e4 988 */
2744d2fd 989 if (!dmi_check_system(i8k_dmi_table)) {
e70c9d5e
DT
990 if (!ignore_dmi && !force)
991 return -ENODEV;
992
60e71aaf
GR
993 pr_info("not running on a supported Dell system.\n");
994 pr_info("vendor=%s, model=%s, version=%s\n",
e70c9d5e
DT
995 i8k_get_dmi_data(DMI_SYS_VENDOR),
996 i8k_get_dmi_data(DMI_PRODUCT_NAME),
997 i8k_get_dmi_data(DMI_BIOS_VERSION));
1da177e4 998 }
dec63ec3 999
2744d2fd
PR
1000 if (dmi_check_system(i8k_blacklist_fan_type_dmi_table))
1001 disallow_fan_type_call = true;
1002
12186f51
GR
1003 strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
1004 sizeof(bios_version));
7613663c
PR
1005 strlcpy(bios_machineid, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
1006 sizeof(bios_machineid));
e70c9d5e 1007
1da177e4 1008 /*
dec63ec3 1009 * Get SMM Dell signature
1da177e4 1010 */
7e0fa31d
DT
1011 if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) &&
1012 i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) {
60e71aaf 1013 pr_err("unable to get SMM Dell signature\n");
e70c9d5e
DT
1014 if (!force)
1015 return -ENODEV;
1da177e4 1016 }
1da177e4 1017
8f21d8e9
PR
1018 /*
1019 * Set fan multiplier and maximal fan speed from dmi config
1020 * Values specified in module parameters override values from dmi
1021 */
26d09382 1022 id = dmi_first_match(i8k_dmi_table);
81474fc2
GR
1023 if (id && id->driver_data) {
1024 const struct i8k_config_data *conf = id->driver_data;
8f21d8e9
PR
1025 if (!fan_mult && conf->fan_mult)
1026 fan_mult = conf->fan_mult;
1027 if (!fan_max && conf->fan_max)
1028 fan_max = conf->fan_max;
81474fc2 1029 }
8f21d8e9
PR
1030
1031 i8k_fan_max = fan_max ? : I8K_FAN_HIGH; /* Must not be 0 */
81474fc2 1032 i8k_pwm_mult = DIV_ROUND_UP(255, i8k_fan_max);
26d09382 1033
8f21d8e9
PR
1034 if (!fan_mult) {
1035 /*
1036 * Autodetect fan multiplier based on nominal rpm
1037 * If fan reports rpm value too high then set multiplier to 1
1038 */
1039 for (fan = 0; fan < 2; ++fan) {
1040 ret = i8k_get_fan_nominal_speed(fan, i8k_fan_max);
1041 if (ret < 0)
1042 continue;
1043 if (ret > I8K_FAN_MAX_RPM)
1044 i8k_fan_mult = 1;
1045 break;
1046 }
1047 } else {
1048 /* Fan multiplier was specified in module param or in dmi */
1049 i8k_fan_mult = fan_mult;
1050 }
1051
dec63ec3 1052 return 0;
1da177e4
LT
1053}
1054
8378b924 1055static int __init i8k_init(void)
1da177e4 1056{
949a9d70 1057 int err;
dec63ec3
DT
1058
1059 /* Are we running on an supported laptop? */
e70c9d5e 1060 if (i8k_probe())
dec63ec3 1061 return -ENODEV;
dec63ec3 1062
949a9d70
JD
1063 err = i8k_init_hwmon();
1064 if (err)
039ae585 1065 return err;
949a9d70 1066
039ae585 1067 i8k_init_procfs();
dec63ec3 1068 return 0;
1da177e4
LT
1069}
1070
8378b924 1071static void __exit i8k_exit(void)
1da177e4 1072{
82ba1d3f 1073 hwmon_device_unregister(i8k_hwmon_dev);
039ae585 1074 i8k_exit_procfs();
1da177e4 1075}
1da177e4 1076
8378b924
DT
1077module_init(i8k_init);
1078module_exit(i8k_exit);
This page took 1.111723 seconds and 5 git commands to generate.