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