2 * i8k.c -- Linux driver for accessing the SMM BIOS on Dell laptops.
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
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.
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
35 #include <linux/sched.h>
37 #include <linux/i8k.h>
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_DELL_SIG1 0xfea3
46 #define I8K_SMM_GET_DELL_SIG2 0xffa3
48 #define I8K_FAN_MULT 30
49 #define I8K_MAX_TEMP 127
51 #define I8K_FN_NONE 0x00
52 #define I8K_FN_UP 0x01
53 #define I8K_FN_DOWN 0x02
54 #define I8K_FN_MUTE 0x04
55 #define I8K_FN_MASK 0x07
56 #define I8K_FN_SHIFT 8
58 #define I8K_POWER_AC 0x05
59 #define I8K_POWER_BATTERY 0x01
61 #define I8K_TEMPERATURE_BUG 1
63 static DEFINE_MUTEX(i8k_mutex);
64 static char bios_version[4];
65 static struct device *i8k_hwmon_dev;
66 static u32 i8k_hwmon_flags;
67 static int i8k_fan_mult;
69 #define I8K_HWMON_HAVE_TEMP1 (1 << 0)
70 #define I8K_HWMON_HAVE_TEMP2 (1 << 1)
71 #define I8K_HWMON_HAVE_TEMP3 (1 << 2)
72 #define I8K_HWMON_HAVE_TEMP4 (1 << 3)
73 #define I8K_HWMON_HAVE_FAN1 (1 << 4)
74 #define I8K_HWMON_HAVE_FAN2 (1 << 5)
77 MODULE_DESCRIPTION("Driver for accessing SMM BIOS on Dell laptops");
78 MODULE_LICENSE("GPL");
81 module_param(force, bool, 0);
82 MODULE_PARM_DESC(force, "Force loading without checking for supported models");
84 static bool ignore_dmi;
85 module_param(ignore_dmi, bool, 0);
86 MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match");
88 static bool restricted;
89 module_param(restricted, bool, 0);
90 MODULE_PARM_DESC(restricted, "Allow fan control if SYS_ADMIN capability set");
92 static bool power_status;
93 module_param(power_status, bool, 0600);
94 MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k");
96 static int fan_mult = I8K_FAN_MULT;
97 module_param(fan_mult, int, 0);
98 MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with");
100 static int i8k_open_fs(struct inode *inode, struct file *file);
101 static long i8k_ioctl(struct file *, unsigned int, unsigned long);
103 static const struct file_operations i8k_fops = {
104 .owner = THIS_MODULE,
108 .release = single_release,
109 .unlocked_ioctl = i8k_ioctl,
114 unsigned int ebx __packed;
115 unsigned int ecx __packed;
116 unsigned int edx __packed;
117 unsigned int esi __packed;
118 unsigned int edi __packed;
121 static inline const char *i8k_get_dmi_data(int field)
123 const char *dmi_data = dmi_get_system_info(field);
125 return dmi_data && *dmi_data ? dmi_data : "?";
129 * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
131 static int i8k_smm(struct smm_regs *regs)
135 cpumask_var_t old_mask;
137 /* SMM requires CPU 0 */
138 if (!alloc_cpumask_var(&old_mask, GFP_KERNEL))
140 cpumask_copy(old_mask, ¤t->cpus_allowed);
141 set_cpus_allowed_ptr(current, cpumask_of(0));
142 if (smp_processor_id() != 0) {
147 #if defined(CONFIG_X86_64)
148 asm volatile("pushq %%rax\n\t"
149 "movl 0(%%rax),%%edx\n\t"
151 "movl 4(%%rax),%%ebx\n\t"
152 "movl 8(%%rax),%%ecx\n\t"
153 "movl 12(%%rax),%%edx\n\t"
154 "movl 16(%%rax),%%esi\n\t"
155 "movl 20(%%rax),%%edi\n\t"
159 "xchgq %%rax,(%%rsp)\n\t"
160 "movl %%ebx,4(%%rax)\n\t"
161 "movl %%ecx,8(%%rax)\n\t"
162 "movl %%edx,12(%%rax)\n\t"
163 "movl %%esi,16(%%rax)\n\t"
164 "movl %%edi,20(%%rax)\n\t"
166 "movl %%edx,0(%%rax)\n\t"
172 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
174 asm volatile("pushl %%eax\n\t"
175 "movl 0(%%eax),%%edx\n\t"
177 "movl 4(%%eax),%%ebx\n\t"
178 "movl 8(%%eax),%%ecx\n\t"
179 "movl 12(%%eax),%%edx\n\t"
180 "movl 16(%%eax),%%esi\n\t"
181 "movl 20(%%eax),%%edi\n\t"
185 "xchgl %%eax,(%%esp)\n\t"
186 "movl %%ebx,4(%%eax)\n\t"
187 "movl %%ecx,8(%%eax)\n\t"
188 "movl %%edx,12(%%eax)\n\t"
189 "movl %%esi,16(%%eax)\n\t"
190 "movl %%edi,20(%%eax)\n\t"
192 "movl %%edx,0(%%eax)\n\t"
198 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
200 if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax)
204 set_cpus_allowed_ptr(current, old_mask);
205 free_cpumask_var(old_mask);
210 * Read the Fn key status.
212 static int i8k_get_fn_status(void)
214 struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, };
221 switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
234 * Read the power status.
236 static int i8k_get_power_status(void)
238 struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, };
245 return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY;
249 * Read the fan status.
251 static int i8k_get_fan_status(int fan)
253 struct smm_regs regs = { .eax = I8K_SMM_GET_FAN, };
255 regs.ebx = fan & 0xff;
256 return i8k_smm(®s) ? : regs.eax & 0xff;
260 * Read the fan speed in RPM.
262 static int i8k_get_fan_speed(int fan)
264 struct smm_regs regs = { .eax = I8K_SMM_GET_SPEED, };
266 regs.ebx = fan & 0xff;
267 return i8k_smm(®s) ? : (regs.eax & 0xffff) * i8k_fan_mult;
271 * Set the fan speed (off, low, high). Returns the new fan status.
273 static int i8k_set_fan(int fan, int speed)
275 struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
277 speed = (speed < 0) ? 0 : ((speed > I8K_FAN_MAX) ? I8K_FAN_MAX : speed);
278 regs.ebx = (fan & 0xff) | (speed << 8);
280 return i8k_smm(®s) ? : i8k_get_fan_status(fan);
284 * Read the cpu temperature.
286 static int i8k_get_temp(int sensor)
288 struct smm_regs regs = { .eax = I8K_SMM_GET_TEMP, };
292 #ifdef I8K_TEMPERATURE_BUG
295 regs.ebx = sensor & 0xff;
300 temp = regs.eax & 0xff;
302 #ifdef I8K_TEMPERATURE_BUG
304 * Sometimes the temperature sensor returns 0x99, which is out of range.
305 * In this case we return (once) the previous cached value. For example:
306 # 1003655137 00000058 00005a4b
307 # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
308 # 1003655139 00000054 00005c52
310 if (temp > I8K_MAX_TEMP) {
312 prev[sensor] = I8K_MAX_TEMP;
321 static int i8k_get_dell_signature(int req_fn)
323 struct smm_regs regs = { .eax = req_fn, };
330 return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
334 i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg)
338 unsigned char buff[16];
339 int __user *argp = (int __user *)arg;
345 case I8K_BIOS_VERSION:
346 val = (bios_version[0] << 16) |
347 (bios_version[1] << 8) | bios_version[2];
352 strlcpy(buff, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
357 val = i8k_get_fn_status();
360 case I8K_POWER_STATUS:
361 val = i8k_get_power_status();
365 val = i8k_get_temp(0);
369 if (copy_from_user(&val, argp, sizeof(int)))
372 val = i8k_get_fan_speed(val);
376 if (copy_from_user(&val, argp, sizeof(int)))
379 val = i8k_get_fan_status(val);
383 if (restricted && !capable(CAP_SYS_ADMIN))
386 if (copy_from_user(&val, argp, sizeof(int)))
389 if (copy_from_user(&speed, argp + 1, sizeof(int)))
392 val = i8k_set_fan(val, speed);
403 case I8K_BIOS_VERSION:
404 if (copy_to_user(argp, &val, 4))
409 if (copy_to_user(argp, buff, 16))
414 if (copy_to_user(argp, &val, sizeof(int)))
423 static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
427 mutex_lock(&i8k_mutex);
428 ret = i8k_ioctl_unlocked(fp, cmd, arg);
429 mutex_unlock(&i8k_mutex);
435 * Print the information for /proc/i8k.
437 static int i8k_proc_show(struct seq_file *seq, void *offset)
439 int fn_key, cpu_temp, ac_power;
440 int left_fan, right_fan, left_speed, right_speed;
442 cpu_temp = i8k_get_temp(0); /* 11100 µs */
443 left_fan = i8k_get_fan_status(I8K_FAN_LEFT); /* 580 µs */
444 right_fan = i8k_get_fan_status(I8K_FAN_RIGHT); /* 580 µs */
445 left_speed = i8k_get_fan_speed(I8K_FAN_LEFT); /* 580 µs */
446 right_speed = i8k_get_fan_speed(I8K_FAN_RIGHT); /* 580 µs */
447 fn_key = i8k_get_fn_status(); /* 750 µs */
449 ac_power = i8k_get_power_status(); /* 14700 µs */
456 * 1) Format version (this will change if format changes)
461 * 6) Right fan status
467 return seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
470 i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
472 left_fan, right_fan, left_speed, right_speed,
476 static int i8k_open_fs(struct inode *inode, struct file *file)
478 return single_open(file, i8k_proc_show, NULL);
486 static ssize_t i8k_hwmon_show_temp(struct device *dev,
487 struct device_attribute *devattr,
490 int index = to_sensor_dev_attr(devattr)->index;
493 temp = i8k_get_temp(index);
496 return sprintf(buf, "%d\n", temp * 1000);
499 static ssize_t i8k_hwmon_show_fan(struct device *dev,
500 struct device_attribute *devattr,
503 int index = to_sensor_dev_attr(devattr)->index;
506 fan_speed = i8k_get_fan_speed(index);
509 return sprintf(buf, "%d\n", fan_speed);
512 static ssize_t i8k_hwmon_show_pwm(struct device *dev,
513 struct device_attribute *devattr,
516 int index = to_sensor_dev_attr(devattr)->index;
519 status = i8k_get_fan_status(index);
522 return sprintf(buf, "%d\n", clamp_val(status * 128, 0, 255));
525 static ssize_t i8k_hwmon_set_pwm(struct device *dev,
526 struct device_attribute *attr,
527 const char *buf, size_t count)
529 int index = to_sensor_dev_attr(attr)->index;
533 err = kstrtoul(buf, 10, &val);
536 val = clamp_val(DIV_ROUND_CLOSEST(val, 128), 0, 2);
538 mutex_lock(&i8k_mutex);
539 err = i8k_set_fan(index, val);
540 mutex_unlock(&i8k_mutex);
542 return err < 0 ? -EIO : count;
545 static ssize_t i8k_hwmon_show_label(struct device *dev,
546 struct device_attribute *devattr,
549 static const char *labels[3] = {
554 int index = to_sensor_dev_attr(devattr)->index;
556 return sprintf(buf, "%s\n", labels[index]);
559 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 0);
560 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 1);
561 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 2);
562 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 3);
563 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
565 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
566 i8k_hwmon_set_pwm, I8K_FAN_LEFT);
567 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
569 static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm,
570 i8k_hwmon_set_pwm, I8K_FAN_RIGHT);
571 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, i8k_hwmon_show_label, NULL, 0);
572 static SENSOR_DEVICE_ATTR(fan1_label, S_IRUGO, i8k_hwmon_show_label, NULL, 1);
573 static SENSOR_DEVICE_ATTR(fan2_label, S_IRUGO, i8k_hwmon_show_label, NULL, 2);
575 static struct attribute *i8k_attrs[] = {
576 &sensor_dev_attr_temp1_input.dev_attr.attr, /* 0 */
577 &sensor_dev_attr_temp1_label.dev_attr.attr, /* 1 */
578 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 2 */
579 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 3 */
580 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 4 */
581 &sensor_dev_attr_fan1_input.dev_attr.attr, /* 5 */
582 &sensor_dev_attr_pwm1.dev_attr.attr, /* 6 */
583 &sensor_dev_attr_fan1_label.dev_attr.attr, /* 7 */
584 &sensor_dev_attr_fan2_input.dev_attr.attr, /* 8 */
585 &sensor_dev_attr_pwm2.dev_attr.attr, /* 9 */
586 &sensor_dev_attr_fan2_label.dev_attr.attr, /* 10 */
590 static umode_t i8k_is_visible(struct kobject *kobj, struct attribute *attr,
593 if ((index == 0 || index == 1) &&
594 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP1))
596 if (index == 2 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP2))
598 if (index == 3 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP3))
600 if (index == 4 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP4))
602 if (index >= 5 && index <= 7 &&
603 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN1))
605 if (index >= 8 && index <= 10 &&
606 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN2))
612 static const struct attribute_group i8k_group = {
614 .is_visible = i8k_is_visible,
616 __ATTRIBUTE_GROUPS(i8k);
618 static int __init i8k_init_hwmon(void)
624 /* CPU temperature attributes, if temperature reading is OK */
625 err = i8k_get_temp(0);
627 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP1;
628 /* check for additional temperature sensors */
629 err = i8k_get_temp(1);
631 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP2;
632 err = i8k_get_temp(2);
634 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP3;
635 err = i8k_get_temp(3);
637 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP4;
639 /* Left fan attributes, if left fan is present */
640 err = i8k_get_fan_status(I8K_FAN_LEFT);
642 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN1;
644 /* Right fan attributes, if right fan is present */
645 err = i8k_get_fan_status(I8K_FAN_RIGHT);
647 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN2;
649 i8k_hwmon_dev = hwmon_device_register_with_groups(NULL, "i8k", NULL,
651 if (IS_ERR(i8k_hwmon_dev)) {
652 err = PTR_ERR(i8k_hwmon_dev);
653 i8k_hwmon_dev = NULL;
654 pr_err("hwmon registration failed (%d)\n", err);
660 static struct dmi_system_id i8k_dmi_table[] __initdata = {
662 .ident = "Dell Inspiron",
664 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
665 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
669 .ident = "Dell Latitude",
671 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
672 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
676 .ident = "Dell Inspiron 2",
678 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
679 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
683 .ident = "Dell Latitude 2",
685 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
686 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
689 { /* UK Inspiron 6400 */
690 .ident = "Dell Inspiron 3",
692 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
693 DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
697 .ident = "Dell Inspiron 3",
699 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
700 DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
704 .ident = "Dell Precision",
706 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
707 DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
711 .ident = "Dell Vostro",
713 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
714 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
718 .ident = "Dell XPS421",
720 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
721 DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"),
725 .ident = "Dell Studio",
727 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
728 DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
730 .driver_data = (void *)1, /* fan multiplier override */
733 .ident = "Dell XPS M140",
735 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
736 DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
738 .driver_data = (void *)1, /* fan multiplier override */
744 * Probe for the presence of a supported laptop.
746 static int __init i8k_probe(void)
748 const struct dmi_system_id *id;
751 * Get DMI information
753 if (!dmi_check_system(i8k_dmi_table)) {
754 if (!ignore_dmi && !force)
757 pr_info("not running on a supported Dell system.\n");
758 pr_info("vendor=%s, model=%s, version=%s\n",
759 i8k_get_dmi_data(DMI_SYS_VENDOR),
760 i8k_get_dmi_data(DMI_PRODUCT_NAME),
761 i8k_get_dmi_data(DMI_BIOS_VERSION));
764 strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
765 sizeof(bios_version));
768 * Get SMM Dell signature
770 if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) &&
771 i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) {
772 pr_err("unable to get SMM Dell signature\n");
777 i8k_fan_mult = fan_mult;
778 id = dmi_first_match(i8k_dmi_table);
779 if (id && fan_mult == I8K_FAN_MULT && id->driver_data)
780 i8k_fan_mult = (unsigned long)id->driver_data;
785 static int __init i8k_init(void)
787 struct proc_dir_entry *proc_i8k;
790 /* Are we running on an supported laptop? */
794 /* Register the proc entry */
795 proc_i8k = proc_create("i8k", 0, NULL, &i8k_fops);
799 err = i8k_init_hwmon();
801 goto exit_remove_proc;
806 remove_proc_entry("i8k", NULL);
810 static void __exit i8k_exit(void)
812 hwmon_device_unregister(i8k_hwmon_dev);
813 remove_proc_entry("i8k", NULL);
816 module_init(i8k_init);
817 module_exit(i8k_exit);