2 * i8k.c -- Linux driver for accessing the SMM BIOS on Dell laptops.
4 * Copyright (C) 2001 Massimo Dal Zotto <dz@debian.org>
7 * Copyright (C) 2011 Jean Delvare <khali@linux-fr.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/init.h>
25 #include <linux/proc_fs.h>
26 #include <linux/seq_file.h>
27 #include <linux/dmi.h>
28 #include <linux/capability.h>
29 #include <linux/mutex.h>
30 #include <linux/hwmon.h>
31 #include <linux/hwmon-sysfs.h>
32 #include <linux/uaccess.h>
35 #include <linux/i8k.h>
37 #define I8K_SMM_FN_STATUS 0x0025
38 #define I8K_SMM_POWER_STATUS 0x0069
39 #define I8K_SMM_SET_FAN 0x01a3
40 #define I8K_SMM_GET_FAN 0x00a3
41 #define I8K_SMM_GET_SPEED 0x02a3
42 #define I8K_SMM_GET_TEMP 0x10a3
43 #define I8K_SMM_GET_DELL_SIG1 0xfea3
44 #define I8K_SMM_GET_DELL_SIG2 0xffa3
45 #define I8K_SMM_BIOS_VERSION 0x00a6
47 #define I8K_FAN_MULT 30
48 #define I8K_MAX_TEMP 127
50 #define I8K_FN_NONE 0x00
51 #define I8K_FN_UP 0x01
52 #define I8K_FN_DOWN 0x02
53 #define I8K_FN_MUTE 0x04
54 #define I8K_FN_MASK 0x07
55 #define I8K_FN_SHIFT 8
57 #define I8K_POWER_AC 0x05
58 #define I8K_POWER_BATTERY 0x01
60 #define I8K_TEMPERATURE_BUG 1
62 static DEFINE_MUTEX(i8k_mutex);
63 static char bios_version[4];
64 static struct device *i8k_hwmon_dev;
65 static u32 i8k_hwmon_flags;
67 #define I8K_HWMON_HAVE_TEMP1 (1 << 0)
68 #define I8K_HWMON_HAVE_TEMP2 (1 << 1)
69 #define I8K_HWMON_HAVE_TEMP3 (1 << 2)
70 #define I8K_HWMON_HAVE_TEMP4 (1 << 3)
71 #define I8K_HWMON_HAVE_FAN1 (1 << 4)
72 #define I8K_HWMON_HAVE_FAN2 (1 << 5)
74 MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)");
75 MODULE_DESCRIPTION("Driver for accessing SMM BIOS on Dell laptops");
76 MODULE_LICENSE("GPL");
79 module_param(force, bool, 0);
80 MODULE_PARM_DESC(force, "Force loading without checking for supported models");
82 static bool ignore_dmi;
83 module_param(ignore_dmi, bool, 0);
84 MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match");
86 static bool restricted;
87 module_param(restricted, bool, 0);
88 MODULE_PARM_DESC(restricted, "Allow fan control if SYS_ADMIN capability set");
90 static bool power_status;
91 module_param(power_status, bool, 0600);
92 MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k");
94 static int fan_mult = I8K_FAN_MULT;
95 module_param(fan_mult, int, 0);
96 MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with");
98 static int i8k_open_fs(struct inode *inode, struct file *file);
99 static long i8k_ioctl(struct file *, unsigned int, unsigned long);
101 static const struct file_operations i8k_fops = {
102 .owner = THIS_MODULE,
106 .release = single_release,
107 .unlocked_ioctl = i8k_ioctl,
112 unsigned int ebx __packed;
113 unsigned int ecx __packed;
114 unsigned int edx __packed;
115 unsigned int esi __packed;
116 unsigned int edi __packed;
119 static inline const char *i8k_get_dmi_data(int field)
121 const char *dmi_data = dmi_get_system_info(field);
123 return dmi_data && *dmi_data ? dmi_data : "?";
127 * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
129 static int i8k_smm(struct smm_regs *regs)
134 #if defined(CONFIG_X86_64)
135 asm volatile("pushq %%rax\n\t"
136 "movl 0(%%rax),%%edx\n\t"
138 "movl 4(%%rax),%%ebx\n\t"
139 "movl 8(%%rax),%%ecx\n\t"
140 "movl 12(%%rax),%%edx\n\t"
141 "movl 16(%%rax),%%esi\n\t"
142 "movl 20(%%rax),%%edi\n\t"
146 "xchgq %%rax,(%%rsp)\n\t"
147 "movl %%ebx,4(%%rax)\n\t"
148 "movl %%ecx,8(%%rax)\n\t"
149 "movl %%edx,12(%%rax)\n\t"
150 "movl %%esi,16(%%rax)\n\t"
151 "movl %%edi,20(%%rax)\n\t"
153 "movl %%edx,0(%%rax)\n\t"
159 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
161 asm volatile("pushl %%eax\n\t"
162 "movl 0(%%eax),%%edx\n\t"
164 "movl 4(%%eax),%%ebx\n\t"
165 "movl 8(%%eax),%%ecx\n\t"
166 "movl 12(%%eax),%%edx\n\t"
167 "movl 16(%%eax),%%esi\n\t"
168 "movl 20(%%eax),%%edi\n\t"
172 "xchgl %%eax,(%%esp)\n\t"
173 "movl %%ebx,4(%%eax)\n\t"
174 "movl %%ecx,8(%%eax)\n\t"
175 "movl %%edx,12(%%eax)\n\t"
176 "movl %%esi,16(%%eax)\n\t"
177 "movl %%edi,20(%%eax)\n\t"
179 "movl %%edx,0(%%eax)\n\t"
185 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
187 if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax)
194 * Read the bios version. Return the version as an integer corresponding
195 * to the ascii value, for example "A17" is returned as 0x00413137.
197 static int i8k_get_bios_version(void)
199 struct smm_regs regs = { .eax = I8K_SMM_BIOS_VERSION, };
201 return i8k_smm(®s) ? : regs.eax;
205 * Read the Fn key status.
207 static int i8k_get_fn_status(void)
209 struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, };
216 switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
229 * Read the power status.
231 static int i8k_get_power_status(void)
233 struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, };
240 return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY;
244 * Read the fan status.
246 static int i8k_get_fan_status(int fan)
248 struct smm_regs regs = { .eax = I8K_SMM_GET_FAN, };
250 regs.ebx = fan & 0xff;
251 return i8k_smm(®s) ? : regs.eax & 0xff;
255 * Read the fan speed in RPM.
257 static int i8k_get_fan_speed(int fan)
259 struct smm_regs regs = { .eax = I8K_SMM_GET_SPEED, };
261 regs.ebx = fan & 0xff;
262 return i8k_smm(®s) ? : (regs.eax & 0xffff) * fan_mult;
266 * Set the fan speed (off, low, high). Returns the new fan status.
268 static int i8k_set_fan(int fan, int speed)
270 struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
272 speed = (speed < 0) ? 0 : ((speed > I8K_FAN_MAX) ? I8K_FAN_MAX : speed);
273 regs.ebx = (fan & 0xff) | (speed << 8);
275 return i8k_smm(®s) ? : i8k_get_fan_status(fan);
279 * Read the cpu temperature.
281 static int i8k_get_temp(int sensor)
283 struct smm_regs regs = { .eax = I8K_SMM_GET_TEMP, };
287 #ifdef I8K_TEMPERATURE_BUG
290 regs.ebx = sensor & 0xff;
295 temp = regs.eax & 0xff;
297 #ifdef I8K_TEMPERATURE_BUG
299 * Sometimes the temperature sensor returns 0x99, which is out of range.
300 * In this case we return (once) the previous cached value. For example:
301 # 1003655137 00000058 00005a4b
302 # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
303 # 1003655139 00000054 00005c52
305 if (temp > I8K_MAX_TEMP) {
307 prev[sensor] = I8K_MAX_TEMP;
316 static int i8k_get_dell_signature(int req_fn)
318 struct smm_regs regs = { .eax = req_fn, };
325 return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
329 i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg)
333 unsigned char buff[16];
334 int __user *argp = (int __user *)arg;
340 case I8K_BIOS_VERSION:
341 val = i8k_get_bios_version();
346 strlcpy(buff, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
351 val = i8k_get_fn_status();
354 case I8K_POWER_STATUS:
355 val = i8k_get_power_status();
359 val = i8k_get_temp(0);
363 if (copy_from_user(&val, argp, sizeof(int)))
366 val = i8k_get_fan_speed(val);
370 if (copy_from_user(&val, argp, sizeof(int)))
373 val = i8k_get_fan_status(val);
377 if (restricted && !capable(CAP_SYS_ADMIN))
380 if (copy_from_user(&val, argp, sizeof(int)))
383 if (copy_from_user(&speed, argp + 1, sizeof(int)))
386 val = i8k_set_fan(val, speed);
397 case I8K_BIOS_VERSION:
398 if (copy_to_user(argp, &val, 4))
403 if (copy_to_user(argp, buff, 16))
408 if (copy_to_user(argp, &val, sizeof(int)))
417 static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
421 mutex_lock(&i8k_mutex);
422 ret = i8k_ioctl_unlocked(fp, cmd, arg);
423 mutex_unlock(&i8k_mutex);
429 * Print the information for /proc/i8k.
431 static int i8k_proc_show(struct seq_file *seq, void *offset)
433 int fn_key, cpu_temp, ac_power;
434 int left_fan, right_fan, left_speed, right_speed;
436 cpu_temp = i8k_get_temp(0); /* 11100 µs */
437 left_fan = i8k_get_fan_status(I8K_FAN_LEFT); /* 580 µs */
438 right_fan = i8k_get_fan_status(I8K_FAN_RIGHT); /* 580 µs */
439 left_speed = i8k_get_fan_speed(I8K_FAN_LEFT); /* 580 µs */
440 right_speed = i8k_get_fan_speed(I8K_FAN_RIGHT); /* 580 µs */
441 fn_key = i8k_get_fn_status(); /* 750 µs */
443 ac_power = i8k_get_power_status(); /* 14700 µs */
450 * 1) Format version (this will change if format changes)
455 * 6) Right fan status
461 return seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
464 i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
466 left_fan, right_fan, left_speed, right_speed,
470 static int i8k_open_fs(struct inode *inode, struct file *file)
472 return single_open(file, i8k_proc_show, NULL);
480 static ssize_t i8k_hwmon_show_temp(struct device *dev,
481 struct device_attribute *devattr,
484 int index = to_sensor_dev_attr(devattr)->index;
487 temp = i8k_get_temp(index);
490 return sprintf(buf, "%d\n", temp * 1000);
493 static ssize_t i8k_hwmon_show_fan(struct device *dev,
494 struct device_attribute *devattr,
497 int index = to_sensor_dev_attr(devattr)->index;
500 fan_speed = i8k_get_fan_speed(index);
503 return sprintf(buf, "%d\n", fan_speed);
506 static ssize_t i8k_hwmon_show_label(struct device *dev,
507 struct device_attribute *devattr,
510 static const char *labels[3] = {
515 int index = to_sensor_dev_attr(devattr)->index;
517 return sprintf(buf, "%s\n", labels[index]);
520 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 0);
521 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 1);
522 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 2);
523 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 3);
524 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
526 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
528 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, i8k_hwmon_show_label, NULL, 0);
529 static SENSOR_DEVICE_ATTR(fan1_label, S_IRUGO, i8k_hwmon_show_label, NULL, 1);
530 static SENSOR_DEVICE_ATTR(fan2_label, S_IRUGO, i8k_hwmon_show_label, NULL, 2);
532 static struct attribute *i8k_attrs[] = {
533 &sensor_dev_attr_temp1_input.dev_attr.attr, /* 0 */
534 &sensor_dev_attr_temp1_label.dev_attr.attr, /* 1 */
535 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 2 */
536 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 3 */
537 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 4 */
538 &sensor_dev_attr_fan1_input.dev_attr.attr, /* 5 */
539 &sensor_dev_attr_fan1_label.dev_attr.attr, /* 6 */
540 &sensor_dev_attr_fan2_input.dev_attr.attr, /* 7 */
541 &sensor_dev_attr_fan2_label.dev_attr.attr, /* 8 */
545 static umode_t i8k_is_visible(struct kobject *kobj, struct attribute *attr,
548 if ((index == 0 || index == 1) &&
549 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP1))
551 if (index == 2 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP2))
553 if (index == 3 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP3))
555 if (index == 4 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP4))
557 if ((index == 5 || index == 6) &&
558 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN1))
560 if ((index == 7 || index == 8) &&
561 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN2))
567 static const struct attribute_group i8k_group = {
569 .is_visible = i8k_is_visible,
571 __ATTRIBUTE_GROUPS(i8k);
573 static int __init i8k_init_hwmon(void)
579 /* CPU temperature attributes, if temperature reading is OK */
580 err = i8k_get_temp(0);
582 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP1;
583 /* check for additional temperature sensors */
584 err = i8k_get_temp(1);
586 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP2;
587 err = i8k_get_temp(2);
589 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP3;
590 err = i8k_get_temp(3);
592 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP4;
594 /* Left fan attributes, if left fan is present */
595 err = i8k_get_fan_status(I8K_FAN_LEFT);
597 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN1;
599 /* Right fan attributes, if right fan is present */
600 err = i8k_get_fan_status(I8K_FAN_RIGHT);
602 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN2;
604 i8k_hwmon_dev = hwmon_device_register_with_groups(NULL, "i8k", NULL,
606 if (IS_ERR(i8k_hwmon_dev)) {
607 err = PTR_ERR(i8k_hwmon_dev);
608 i8k_hwmon_dev = NULL;
609 pr_err("hwmon registration failed (%d)\n", err);
615 static struct dmi_system_id i8k_dmi_table[] __initdata = {
617 .ident = "Dell Inspiron",
619 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
620 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
624 .ident = "Dell Latitude",
626 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
627 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
631 .ident = "Dell Inspiron 2",
633 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
634 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
638 .ident = "Dell Latitude 2",
640 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
641 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
644 { /* UK Inspiron 6400 */
645 .ident = "Dell Inspiron 3",
647 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
648 DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
652 .ident = "Dell Inspiron 3",
654 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
655 DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
659 .ident = "Dell Precision",
661 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
662 DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
666 .ident = "Dell Vostro",
668 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
669 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
673 .ident = "Dell XPS421",
675 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
676 DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"),
683 * Probe for the presence of a supported laptop.
685 static int __init i8k_probe(void)
691 * Get DMI information
693 if (!dmi_check_system(i8k_dmi_table)) {
694 if (!ignore_dmi && !force)
697 pr_info("not running on a supported Dell system.\n");
698 pr_info("vendor=%s, model=%s, version=%s\n",
699 i8k_get_dmi_data(DMI_SYS_VENDOR),
700 i8k_get_dmi_data(DMI_PRODUCT_NAME),
701 i8k_get_dmi_data(DMI_BIOS_VERSION));
704 strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
705 sizeof(bios_version));
708 * Get SMM Dell signature
710 if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) &&
711 i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) {
712 pr_err("unable to get SMM Dell signature\n");
718 * Get SMM BIOS version.
720 version = i8k_get_bios_version();
722 pr_warn("unable to get SMM BIOS version\n");
724 buff[0] = (version >> 16) & 0xff;
725 buff[1] = (version >> 8) & 0xff;
726 buff[2] = (version) & 0xff;
729 * If DMI BIOS version is unknown use SMM BIOS version.
731 if (!dmi_get_system_info(DMI_BIOS_VERSION))
732 strlcpy(bios_version, buff, sizeof(bios_version));
735 * Check if the two versions match.
737 if (strncmp(buff, bios_version, sizeof(bios_version)) != 0)
738 pr_warn("BIOS version mismatch: %s != %s\n",
745 static int __init i8k_init(void)
747 struct proc_dir_entry *proc_i8k;
750 /* Are we running on an supported laptop? */
754 /* Register the proc entry */
755 proc_i8k = proc_create("i8k", 0, NULL, &i8k_fops);
759 err = i8k_init_hwmon();
761 goto exit_remove_proc;
766 remove_proc_entry("i8k", NULL);
770 static void __exit i8k_exit(void)
772 hwmon_device_unregister(i8k_hwmon_dev);
773 remove_proc_entry("i8k", NULL);
776 module_init(i8k_init);
777 module_exit(i8k_exit);