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