i8k: Add support for temperature sensor labels
[linux-2.6-block.git] / drivers / char / i8k.c
1 /*
2  * i8k.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  Guenter Roeck <linux@roeck-us.net>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU General Public License as published by the
12  * Free Software Foundation; either version 2, or (at your option) any
13  * later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  */
20
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/init.h>
26 #include <linux/proc_fs.h>
27 #include <linux/seq_file.h>
28 #include <linux/dmi.h>
29 #include <linux/capability.h>
30 #include <linux/mutex.h>
31 #include <linux/hwmon.h>
32 #include <linux/hwmon-sysfs.h>
33 #include <linux/uaccess.h>
34 #include <linux/io.h>
35 #include <linux/sched.h>
36
37 #include <linux/i8k.h>
38
39 #define I8K_SMM_FN_STATUS       0x0025
40 #define I8K_SMM_POWER_STATUS    0x0069
41 #define I8K_SMM_SET_FAN         0x01a3
42 #define I8K_SMM_GET_FAN         0x00a3
43 #define I8K_SMM_GET_SPEED       0x02a3
44 #define I8K_SMM_GET_TEMP        0x10a3
45 #define I8K_SMM_GET_TEMP_TYPE   0x11a3
46 #define I8K_SMM_GET_DELL_SIG1   0xfea3
47 #define I8K_SMM_GET_DELL_SIG2   0xffa3
48
49 #define I8K_FAN_MULT            30
50 #define I8K_MAX_TEMP            127
51
52 #define I8K_FN_NONE             0x00
53 #define I8K_FN_UP               0x01
54 #define I8K_FN_DOWN             0x02
55 #define I8K_FN_MUTE             0x04
56 #define I8K_FN_MASK             0x07
57 #define I8K_FN_SHIFT            8
58
59 #define I8K_POWER_AC            0x05
60 #define I8K_POWER_BATTERY       0x01
61
62 #define I8K_TEMPERATURE_BUG     1
63
64 static DEFINE_MUTEX(i8k_mutex);
65 static char bios_version[4];
66 static struct device *i8k_hwmon_dev;
67 static u32 i8k_hwmon_flags;
68 static int i8k_fan_mult;
69 static int i8k_pwm_mult;
70 static int i8k_fan_max = I8K_FAN_HIGH;
71
72 #define I8K_HWMON_HAVE_TEMP1    (1 << 0)
73 #define I8K_HWMON_HAVE_TEMP2    (1 << 1)
74 #define I8K_HWMON_HAVE_TEMP3    (1 << 2)
75 #define I8K_HWMON_HAVE_TEMP4    (1 << 3)
76 #define I8K_HWMON_HAVE_FAN1     (1 << 4)
77 #define I8K_HWMON_HAVE_FAN2     (1 << 5)
78
79 MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)");
80 MODULE_DESCRIPTION("Driver for accessing SMM BIOS on Dell laptops");
81 MODULE_LICENSE("GPL");
82
83 static bool force;
84 module_param(force, bool, 0);
85 MODULE_PARM_DESC(force, "Force loading without checking for supported models");
86
87 static bool ignore_dmi;
88 module_param(ignore_dmi, bool, 0);
89 MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match");
90
91 static bool restricted;
92 module_param(restricted, bool, 0);
93 MODULE_PARM_DESC(restricted, "Allow fan control if SYS_ADMIN capability set");
94
95 static bool power_status;
96 module_param(power_status, bool, 0600);
97 MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k");
98
99 static int fan_mult = I8K_FAN_MULT;
100 module_param(fan_mult, int, 0);
101 MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with");
102
103 static int fan_max = I8K_FAN_HIGH;
104 module_param(fan_max, int, 0);
105 MODULE_PARM_DESC(fan_max, "Maximum configurable fan speed");
106
107 static int i8k_open_fs(struct inode *inode, struct file *file);
108 static long i8k_ioctl(struct file *, unsigned int, unsigned long);
109
110 static const struct file_operations i8k_fops = {
111         .owner          = THIS_MODULE,
112         .open           = i8k_open_fs,
113         .read           = seq_read,
114         .llseek         = seq_lseek,
115         .release        = single_release,
116         .unlocked_ioctl = i8k_ioctl,
117 };
118
119 struct smm_regs {
120         unsigned int eax;
121         unsigned int ebx __packed;
122         unsigned int ecx __packed;
123         unsigned int edx __packed;
124         unsigned int esi __packed;
125         unsigned int edi __packed;
126 };
127
128 static inline const char *i8k_get_dmi_data(int field)
129 {
130         const char *dmi_data = dmi_get_system_info(field);
131
132         return dmi_data && *dmi_data ? dmi_data : "?";
133 }
134
135 /*
136  * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
137  */
138 static int i8k_smm(struct smm_regs *regs)
139 {
140         int rc;
141         int eax = regs->eax;
142         cpumask_var_t old_mask;
143
144         /* SMM requires CPU 0 */
145         if (!alloc_cpumask_var(&old_mask, GFP_KERNEL))
146                 return -ENOMEM;
147         cpumask_copy(old_mask, &current->cpus_allowed);
148         rc = set_cpus_allowed_ptr(current, cpumask_of(0));
149         if (rc)
150                 goto out;
151         if (smp_processor_id() != 0) {
152                 rc = -EBUSY;
153                 goto out;
154         }
155
156 #if defined(CONFIG_X86_64)
157         asm volatile("pushq %%rax\n\t"
158                 "movl 0(%%rax),%%edx\n\t"
159                 "pushq %%rdx\n\t"
160                 "movl 4(%%rax),%%ebx\n\t"
161                 "movl 8(%%rax),%%ecx\n\t"
162                 "movl 12(%%rax),%%edx\n\t"
163                 "movl 16(%%rax),%%esi\n\t"
164                 "movl 20(%%rax),%%edi\n\t"
165                 "popq %%rax\n\t"
166                 "out %%al,$0xb2\n\t"
167                 "out %%al,$0x84\n\t"
168                 "xchgq %%rax,(%%rsp)\n\t"
169                 "movl %%ebx,4(%%rax)\n\t"
170                 "movl %%ecx,8(%%rax)\n\t"
171                 "movl %%edx,12(%%rax)\n\t"
172                 "movl %%esi,16(%%rax)\n\t"
173                 "movl %%edi,20(%%rax)\n\t"
174                 "popq %%rdx\n\t"
175                 "movl %%edx,0(%%rax)\n\t"
176                 "pushfq\n\t"
177                 "popq %%rax\n\t"
178                 "andl $1,%%eax\n"
179                 : "=a"(rc)
180                 :    "a"(regs)
181                 :    "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
182 #else
183         asm volatile("pushl %%eax\n\t"
184             "movl 0(%%eax),%%edx\n\t"
185             "push %%edx\n\t"
186             "movl 4(%%eax),%%ebx\n\t"
187             "movl 8(%%eax),%%ecx\n\t"
188             "movl 12(%%eax),%%edx\n\t"
189             "movl 16(%%eax),%%esi\n\t"
190             "movl 20(%%eax),%%edi\n\t"
191             "popl %%eax\n\t"
192             "out %%al,$0xb2\n\t"
193             "out %%al,$0x84\n\t"
194             "xchgl %%eax,(%%esp)\n\t"
195             "movl %%ebx,4(%%eax)\n\t"
196             "movl %%ecx,8(%%eax)\n\t"
197             "movl %%edx,12(%%eax)\n\t"
198             "movl %%esi,16(%%eax)\n\t"
199             "movl %%edi,20(%%eax)\n\t"
200             "popl %%edx\n\t"
201             "movl %%edx,0(%%eax)\n\t"
202             "lahf\n\t"
203             "shrl $8,%%eax\n\t"
204             "andl $1,%%eax\n"
205             : "=a"(rc)
206             :    "a"(regs)
207             :    "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
208 #endif
209         if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax)
210                 rc = -EINVAL;
211
212 out:
213         set_cpus_allowed_ptr(current, old_mask);
214         free_cpumask_var(old_mask);
215         return rc;
216 }
217
218 /*
219  * Read the Fn key status.
220  */
221 static int i8k_get_fn_status(void)
222 {
223         struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, };
224         int rc;
225
226         rc = i8k_smm(&regs);
227         if (rc < 0)
228                 return rc;
229
230         switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
231         case I8K_FN_UP:
232                 return I8K_VOL_UP;
233         case I8K_FN_DOWN:
234                 return I8K_VOL_DOWN;
235         case I8K_FN_MUTE:
236                 return I8K_VOL_MUTE;
237         default:
238                 return 0;
239         }
240 }
241
242 /*
243  * Read the power status.
244  */
245 static int i8k_get_power_status(void)
246 {
247         struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, };
248         int rc;
249
250         rc = i8k_smm(&regs);
251         if (rc < 0)
252                 return rc;
253
254         return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY;
255 }
256
257 /*
258  * Read the fan status.
259  */
260 static int i8k_get_fan_status(int fan)
261 {
262         struct smm_regs regs = { .eax = I8K_SMM_GET_FAN, };
263
264         regs.ebx = fan & 0xff;
265         return i8k_smm(&regs) ? : regs.eax & 0xff;
266 }
267
268 /*
269  * Read the fan speed in RPM.
270  */
271 static int i8k_get_fan_speed(int fan)
272 {
273         struct smm_regs regs = { .eax = I8K_SMM_GET_SPEED, };
274
275         regs.ebx = fan & 0xff;
276         return i8k_smm(&regs) ? : (regs.eax & 0xffff) * i8k_fan_mult;
277 }
278
279 /*
280  * Set the fan speed (off, low, high). Returns the new fan status.
281  */
282 static int i8k_set_fan(int fan, int speed)
283 {
284         struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
285
286         speed = (speed < 0) ? 0 : ((speed > i8k_fan_max) ? i8k_fan_max : speed);
287         regs.ebx = (fan & 0xff) | (speed << 8);
288
289         return i8k_smm(&regs) ? : i8k_get_fan_status(fan);
290 }
291
292 static int i8k_get_temp_type(int sensor)
293 {
294         struct smm_regs regs = { .eax = I8K_SMM_GET_TEMP_TYPE, };
295
296         regs.ebx = sensor & 0xff;
297         return i8k_smm(&regs) ? : regs.eax & 0xff;
298 }
299
300 /*
301  * Read the cpu temperature.
302  */
303 static int i8k_get_temp(int sensor)
304 {
305         struct smm_regs regs = { .eax = I8K_SMM_GET_TEMP, };
306         int rc;
307         int temp;
308
309 #ifdef I8K_TEMPERATURE_BUG
310         static int prev[4] = { I8K_MAX_TEMP+1, I8K_MAX_TEMP+1, I8K_MAX_TEMP+1, I8K_MAX_TEMP+1 };
311 #endif
312         regs.ebx = sensor & 0xff;
313         rc = i8k_smm(&regs);
314         if (rc < 0)
315                 return rc;
316
317         temp = regs.eax & 0xff;
318
319 #ifdef I8K_TEMPERATURE_BUG
320         /*
321          * Sometimes the temperature sensor returns 0x99, which is out of range.
322          * In this case we return (once) the previous cached value. For example:
323          # 1003655137 00000058 00005a4b
324          # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
325          # 1003655139 00000054 00005c52
326          */
327         if (temp > I8K_MAX_TEMP) {
328                 temp = prev[sensor];
329                 prev[sensor] = I8K_MAX_TEMP+1;
330         } else {
331                 prev[sensor] = temp;
332         }
333         if (temp > I8K_MAX_TEMP)
334                 return -ERANGE;
335 #endif
336
337         return temp;
338 }
339
340 static int i8k_get_dell_signature(int req_fn)
341 {
342         struct smm_regs regs = { .eax = req_fn, };
343         int rc;
344
345         rc = i8k_smm(&regs);
346         if (rc < 0)
347                 return rc;
348
349         return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
350 }
351
352 static int
353 i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg)
354 {
355         int val = 0;
356         int speed;
357         unsigned char buff[16];
358         int __user *argp = (int __user *)arg;
359
360         if (!argp)
361                 return -EINVAL;
362
363         switch (cmd) {
364         case I8K_BIOS_VERSION:
365                 val = (bios_version[0] << 16) |
366                                 (bios_version[1] << 8) | bios_version[2];
367                 break;
368
369         case I8K_MACHINE_ID:
370                 memset(buff, 0, 16);
371                 strlcpy(buff, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
372                         sizeof(buff));
373                 break;
374
375         case I8K_FN_STATUS:
376                 val = i8k_get_fn_status();
377                 break;
378
379         case I8K_POWER_STATUS:
380                 val = i8k_get_power_status();
381                 break;
382
383         case I8K_GET_TEMP:
384                 val = i8k_get_temp(0);
385                 break;
386
387         case I8K_GET_SPEED:
388                 if (copy_from_user(&val, argp, sizeof(int)))
389                         return -EFAULT;
390
391                 val = i8k_get_fan_speed(val);
392                 break;
393
394         case I8K_GET_FAN:
395                 if (copy_from_user(&val, argp, sizeof(int)))
396                         return -EFAULT;
397
398                 val = i8k_get_fan_status(val);
399                 break;
400
401         case I8K_SET_FAN:
402                 if (restricted && !capable(CAP_SYS_ADMIN))
403                         return -EPERM;
404
405                 if (copy_from_user(&val, argp, sizeof(int)))
406                         return -EFAULT;
407
408                 if (copy_from_user(&speed, argp + 1, sizeof(int)))
409                         return -EFAULT;
410
411                 val = i8k_set_fan(val, speed);
412                 break;
413
414         default:
415                 return -EINVAL;
416         }
417
418         if (val < 0)
419                 return val;
420
421         switch (cmd) {
422         case I8K_BIOS_VERSION:
423                 if (copy_to_user(argp, &val, 4))
424                         return -EFAULT;
425
426                 break;
427         case I8K_MACHINE_ID:
428                 if (copy_to_user(argp, buff, 16))
429                         return -EFAULT;
430
431                 break;
432         default:
433                 if (copy_to_user(argp, &val, sizeof(int)))
434                         return -EFAULT;
435
436                 break;
437         }
438
439         return 0;
440 }
441
442 static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
443 {
444         long ret;
445
446         mutex_lock(&i8k_mutex);
447         ret = i8k_ioctl_unlocked(fp, cmd, arg);
448         mutex_unlock(&i8k_mutex);
449
450         return ret;
451 }
452
453 /*
454  * Print the information for /proc/i8k.
455  */
456 static int i8k_proc_show(struct seq_file *seq, void *offset)
457 {
458         int fn_key, cpu_temp, ac_power;
459         int left_fan, right_fan, left_speed, right_speed;
460
461         cpu_temp        = i8k_get_temp(0);                      /* 11100 Âµs */
462         left_fan        = i8k_get_fan_status(I8K_FAN_LEFT);     /*   580 Âµs */
463         right_fan       = i8k_get_fan_status(I8K_FAN_RIGHT);    /*   580 Âµs */
464         left_speed      = i8k_get_fan_speed(I8K_FAN_LEFT);      /*   580 Âµs */
465         right_speed     = i8k_get_fan_speed(I8K_FAN_RIGHT);     /*   580 Âµs */
466         fn_key          = i8k_get_fn_status();                  /*   750 Âµs */
467         if (power_status)
468                 ac_power = i8k_get_power_status();              /* 14700 Âµs */
469         else
470                 ac_power = -1;
471
472         /*
473          * Info:
474          *
475          * 1)  Format version (this will change if format changes)
476          * 2)  BIOS version
477          * 3)  BIOS machine ID
478          * 4)  Cpu temperature
479          * 5)  Left fan status
480          * 6)  Right fan status
481          * 7)  Left fan speed
482          * 8)  Right fan speed
483          * 9)  AC power
484          * 10) Fn Key status
485          */
486         return seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
487                           I8K_PROC_FMT,
488                           bios_version,
489                           i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
490                           cpu_temp,
491                           left_fan, right_fan, left_speed, right_speed,
492                           ac_power, fn_key);
493 }
494
495 static int i8k_open_fs(struct inode *inode, struct file *file)
496 {
497         return single_open(file, i8k_proc_show, NULL);
498 }
499
500
501 /*
502  * Hwmon interface
503  */
504
505 static ssize_t i8k_hwmon_show_temp_label(struct device *dev,
506                                          struct device_attribute *devattr,
507                                          char *buf)
508 {
509         static const char * const labels[] = {
510                 "CPU",
511                 "GPU",
512                 "SODIMM",
513                 "Other",
514                 "Ambient",
515                 "Other",
516         };
517         int index = to_sensor_dev_attr(devattr)->index;
518         int type;
519
520         type = i8k_get_temp_type(index);
521         if (type < 0)
522                 return type;
523         if (type >= ARRAY_SIZE(labels))
524                 type = ARRAY_SIZE(labels) - 1;
525         return sprintf(buf, "%s\n", labels[type]);
526 }
527
528 static ssize_t i8k_hwmon_show_temp(struct device *dev,
529                                    struct device_attribute *devattr,
530                                    char *buf)
531 {
532         int index = to_sensor_dev_attr(devattr)->index;
533         int temp;
534
535         temp = i8k_get_temp(index);
536         if (temp == -ERANGE)
537                 return -EINVAL;
538         if (temp < 0)
539                 return temp;
540         return sprintf(buf, "%d\n", temp * 1000);
541 }
542
543 static ssize_t i8k_hwmon_show_fan(struct device *dev,
544                                   struct device_attribute *devattr,
545                                   char *buf)
546 {
547         int index = to_sensor_dev_attr(devattr)->index;
548         int fan_speed;
549
550         fan_speed = i8k_get_fan_speed(index);
551         if (fan_speed < 0)
552                 return fan_speed;
553         return sprintf(buf, "%d\n", fan_speed);
554 }
555
556 static ssize_t i8k_hwmon_show_pwm(struct device *dev,
557                                   struct device_attribute *devattr,
558                                   char *buf)
559 {
560         int index = to_sensor_dev_attr(devattr)->index;
561         int status;
562
563         status = i8k_get_fan_status(index);
564         if (status < 0)
565                 return -EIO;
566         return sprintf(buf, "%d\n", clamp_val(status * i8k_pwm_mult, 0, 255));
567 }
568
569 static ssize_t i8k_hwmon_set_pwm(struct device *dev,
570                                  struct device_attribute *attr,
571                                  const char *buf, size_t count)
572 {
573         int index = to_sensor_dev_attr(attr)->index;
574         unsigned long val;
575         int err;
576
577         err = kstrtoul(buf, 10, &val);
578         if (err)
579                 return err;
580         val = clamp_val(DIV_ROUND_CLOSEST(val, i8k_pwm_mult), 0, i8k_fan_max);
581
582         mutex_lock(&i8k_mutex);
583         err = i8k_set_fan(index, val);
584         mutex_unlock(&i8k_mutex);
585
586         return err < 0 ? -EIO : count;
587 }
588
589 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 0);
590 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
591                           0);
592 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 1);
593 static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
594                           1);
595 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 2);
596 static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
597                           2);
598 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 3);
599 static SENSOR_DEVICE_ATTR(temp4_label, S_IRUGO, i8k_hwmon_show_temp_label, NULL,
600                           3);
601 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
602                           I8K_FAN_LEFT);
603 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
604                           i8k_hwmon_set_pwm, I8K_FAN_LEFT);
605 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
606                           I8K_FAN_RIGHT);
607 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
608                           i8k_hwmon_set_pwm, I8K_FAN_RIGHT);
609
610 static struct attribute *i8k_attrs[] = {
611         &sensor_dev_attr_temp1_input.dev_attr.attr,     /* 0 */
612         &sensor_dev_attr_temp1_label.dev_attr.attr,     /* 1 */
613         &sensor_dev_attr_temp2_input.dev_attr.attr,     /* 2 */
614         &sensor_dev_attr_temp2_label.dev_attr.attr,     /* 3 */
615         &sensor_dev_attr_temp3_input.dev_attr.attr,     /* 4 */
616         &sensor_dev_attr_temp3_label.dev_attr.attr,     /* 5 */
617         &sensor_dev_attr_temp4_input.dev_attr.attr,     /* 6 */
618         &sensor_dev_attr_temp4_label.dev_attr.attr,     /* 7 */
619         &sensor_dev_attr_fan1_input.dev_attr.attr,      /* 8 */
620         &sensor_dev_attr_pwm1.dev_attr.attr,            /* 9 */
621         &sensor_dev_attr_fan2_input.dev_attr.attr,      /* 10 */
622         &sensor_dev_attr_pwm2.dev_attr.attr,            /* 11 */
623         NULL
624 };
625
626 static umode_t i8k_is_visible(struct kobject *kobj, struct attribute *attr,
627                               int index)
628 {
629         if (index >= 0 && index <= 1 &&
630             !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP1))
631                 return 0;
632         if (index >= 2 && index <= 3 &&
633             !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP2))
634                 return 0;
635         if (index >= 4 && index <= 5 &&
636             !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP3))
637                 return 0;
638         if (index >= 6 && index <= 7 &&
639             !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP4))
640                 return 0;
641         if (index >= 8 && index <= 9 &&
642             !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN1))
643                 return 0;
644         if (index >= 10 && index <= 11 &&
645             !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN2))
646                 return 0;
647
648         return attr->mode;
649 }
650
651 static const struct attribute_group i8k_group = {
652         .attrs = i8k_attrs,
653         .is_visible = i8k_is_visible,
654 };
655 __ATTRIBUTE_GROUPS(i8k);
656
657 static int __init i8k_init_hwmon(void)
658 {
659         int err;
660
661         i8k_hwmon_flags = 0;
662
663         /* CPU temperature attributes, if temperature reading is OK */
664         err = i8k_get_temp(0);
665         if (err >= 0 || err == -ERANGE)
666                 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP1;
667         /* check for additional temperature sensors */
668         err = i8k_get_temp(1);
669         if (err >= 0 || err == -ERANGE)
670                 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP2;
671         err = i8k_get_temp(2);
672         if (err >= 0 || err == -ERANGE)
673                 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP3;
674         err = i8k_get_temp(3);
675         if (err >= 0 || err == -ERANGE)
676                 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP4;
677
678         /* Left fan attributes, if left fan is present */
679         err = i8k_get_fan_status(I8K_FAN_LEFT);
680         if (err >= 0)
681                 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN1;
682
683         /* Right fan attributes, if right fan is present */
684         err = i8k_get_fan_status(I8K_FAN_RIGHT);
685         if (err >= 0)
686                 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN2;
687
688         i8k_hwmon_dev = hwmon_device_register_with_groups(NULL, "i8k", NULL,
689                                                           i8k_groups);
690         if (IS_ERR(i8k_hwmon_dev)) {
691                 err = PTR_ERR(i8k_hwmon_dev);
692                 i8k_hwmon_dev = NULL;
693                 pr_err("hwmon registration failed (%d)\n", err);
694                 return err;
695         }
696         return 0;
697 }
698
699 struct i8k_config_data {
700         int fan_mult;
701         int fan_max;
702 };
703
704 enum i8k_configs {
705         DELL_LATITUDE_D520,
706         DELL_LATITUDE_E6540,
707         DELL_PRECISION_490,
708         DELL_STUDIO,
709         DELL_XPS_M140,
710 };
711
712 static const struct i8k_config_data i8k_config_data[] = {
713         [DELL_LATITUDE_D520] = {
714                 .fan_mult = 1,
715                 .fan_max = I8K_FAN_TURBO,
716         },
717         [DELL_LATITUDE_E6540] = {
718                 .fan_mult = 1,
719                 .fan_max = I8K_FAN_HIGH,
720         },
721         [DELL_PRECISION_490] = {
722                 .fan_mult = 1,
723                 .fan_max = I8K_FAN_TURBO,
724         },
725         [DELL_STUDIO] = {
726                 .fan_mult = 1,
727                 .fan_max = I8K_FAN_HIGH,
728         },
729         [DELL_XPS_M140] = {
730                 .fan_mult = 1,
731                 .fan_max = I8K_FAN_HIGH,
732         },
733 };
734
735 static struct dmi_system_id i8k_dmi_table[] __initdata = {
736         {
737                 .ident = "Dell Inspiron",
738                 .matches = {
739                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
740                         DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
741                 },
742         },
743         {
744                 .ident = "Dell Latitude",
745                 .matches = {
746                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
747                         DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
748                 },
749         },
750         {
751                 .ident = "Dell Inspiron 2",
752                 .matches = {
753                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
754                         DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
755                 },
756         },
757         {
758                 .ident = "Dell Latitude D520",
759                 .matches = {
760                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
761                         DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D520"),
762                 },
763                 .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_D520],
764         },
765         {
766                 .ident = "Dell Latitude E6440",
767                 .matches = {
768                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
769                         DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6440"),
770                 },
771                 .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_E6540],
772         },
773         {
774                 .ident = "Dell Latitude E6540",
775                 .matches = {
776                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
777                         DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6540"),
778                 },
779                 .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_E6540],
780         },
781         {
782                 .ident = "Dell Latitude 2",
783                 .matches = {
784                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
785                         DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
786                 },
787         },
788         {       /* UK Inspiron 6400  */
789                 .ident = "Dell Inspiron 3",
790                 .matches = {
791                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
792                         DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
793                 },
794         },
795         {
796                 .ident = "Dell Inspiron 3",
797                 .matches = {
798                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
799                         DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
800                 },
801         },
802         {
803                 .ident = "Dell Precision 490",
804                 .matches = {
805                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
806                         DMI_MATCH(DMI_PRODUCT_NAME,
807                                   "Precision WorkStation 490"),
808                 },
809                 .driver_data = (void *)&i8k_config_data[DELL_PRECISION_490],
810         },
811         {
812                 .ident = "Dell Precision",
813                 .matches = {
814                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
815                         DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
816                 },
817         },
818         {
819                 .ident = "Dell Vostro",
820                 .matches = {
821                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
822                         DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
823                 },
824         },
825         {
826                 .ident = "Dell XPS421",
827                 .matches = {
828                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
829                         DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"),
830                 },
831         },
832         {
833                 .ident = "Dell Studio",
834                 .matches = {
835                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
836                         DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
837                 },
838                 .driver_data = (void *)&i8k_config_data[DELL_STUDIO],
839         },
840         {
841                 .ident = "Dell XPS M140",
842                 .matches = {
843                         DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
844                         DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
845                 },
846                 .driver_data = (void *)&i8k_config_data[DELL_XPS_M140],
847         },
848         { }
849 };
850
851 MODULE_DEVICE_TABLE(dmi, i8k_dmi_table);
852
853 /*
854  * Probe for the presence of a supported laptop.
855  */
856 static int __init i8k_probe(void)
857 {
858         const struct dmi_system_id *id;
859
860         /*
861          * Get DMI information
862          */
863         if (!dmi_check_system(i8k_dmi_table)) {
864                 if (!ignore_dmi && !force)
865                         return -ENODEV;
866
867                 pr_info("not running on a supported Dell system.\n");
868                 pr_info("vendor=%s, model=%s, version=%s\n",
869                         i8k_get_dmi_data(DMI_SYS_VENDOR),
870                         i8k_get_dmi_data(DMI_PRODUCT_NAME),
871                         i8k_get_dmi_data(DMI_BIOS_VERSION));
872         }
873
874         strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
875                 sizeof(bios_version));
876
877         /*
878          * Get SMM Dell signature
879          */
880         if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) &&
881             i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) {
882                 pr_err("unable to get SMM Dell signature\n");
883                 if (!force)
884                         return -ENODEV;
885         }
886
887         i8k_fan_mult = fan_mult;
888         i8k_fan_max = fan_max ? : I8K_FAN_HIGH; /* Must not be 0 */
889         id = dmi_first_match(i8k_dmi_table);
890         if (id && id->driver_data) {
891                 const struct i8k_config_data *conf = id->driver_data;
892
893                 if (fan_mult == I8K_FAN_MULT && conf->fan_mult)
894                         i8k_fan_mult = conf->fan_mult;
895                 if (fan_max == I8K_FAN_HIGH && conf->fan_max)
896                         i8k_fan_max = conf->fan_max;
897         }
898         i8k_pwm_mult = DIV_ROUND_UP(255, i8k_fan_max);
899
900         return 0;
901 }
902
903 static int __init i8k_init(void)
904 {
905         struct proc_dir_entry *proc_i8k;
906         int err;
907
908         /* Are we running on an supported laptop? */
909         if (i8k_probe())
910                 return -ENODEV;
911
912         /* Register the proc entry */
913         proc_i8k = proc_create("i8k", 0, NULL, &i8k_fops);
914         if (!proc_i8k)
915                 return -ENOENT;
916
917         err = i8k_init_hwmon();
918         if (err)
919                 goto exit_remove_proc;
920
921         return 0;
922
923  exit_remove_proc:
924         remove_proc_entry("i8k", NULL);
925         return err;
926 }
927
928 static void __exit i8k_exit(void)
929 {
930         hwmon_device_unregister(i8k_hwmon_dev);
931         remove_proc_entry("i8k", NULL);
932 }
933
934 module_init(i8k_init);
935 module_exit(i8k_exit);