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>
8 * Copyright (C) 2013 Guenter Roeck <linux@roeck-us.net>
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
47 #define I8K_SMM_BIOS_VERSION 0x00a6
49 #define I8K_FAN_MULT 30
50 #define I8K_MAX_TEMP 127
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
59 #define I8K_POWER_AC 0x05
60 #define I8K_POWER_BATTERY 0x01
62 #define I8K_TEMPERATURE_BUG 1
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;
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)
76 MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)");
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 bios version. Return the version as an integer corresponding
211 * to the ascii value, for example "A17" is returned as 0x00413137.
213 static int i8k_get_bios_version(void)
215 struct smm_regs regs = { .eax = I8K_SMM_BIOS_VERSION, };
217 return i8k_smm(®s) ? : regs.eax;
221 * Read the Fn key status.
223 static int i8k_get_fn_status(void)
225 struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, };
232 switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
245 * Read the power status.
247 static int i8k_get_power_status(void)
249 struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, };
256 return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY;
260 * Read the fan status.
262 static int i8k_get_fan_status(int fan)
264 struct smm_regs regs = { .eax = I8K_SMM_GET_FAN, };
266 regs.ebx = fan & 0xff;
267 return i8k_smm(®s) ? : regs.eax & 0xff;
271 * Read the fan speed in RPM.
273 static int i8k_get_fan_speed(int fan)
275 struct smm_regs regs = { .eax = I8K_SMM_GET_SPEED, };
277 regs.ebx = fan & 0xff;
278 return i8k_smm(®s) ? : (regs.eax & 0xffff) * fan_mult;
282 * Set the fan speed (off, low, high). Returns the new fan status.
284 static int i8k_set_fan(int fan, int speed)
286 struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
288 speed = (speed < 0) ? 0 : ((speed > I8K_FAN_MAX) ? I8K_FAN_MAX : speed);
289 regs.ebx = (fan & 0xff) | (speed << 8);
291 return i8k_smm(®s) ? : i8k_get_fan_status(fan);
295 * Read the cpu temperature.
297 static int i8k_get_temp(int sensor)
299 struct smm_regs regs = { .eax = I8K_SMM_GET_TEMP, };
303 #ifdef I8K_TEMPERATURE_BUG
306 regs.ebx = sensor & 0xff;
311 temp = regs.eax & 0xff;
313 #ifdef I8K_TEMPERATURE_BUG
315 * Sometimes the temperature sensor returns 0x99, which is out of range.
316 * In this case we return (once) the previous cached value. For example:
317 # 1003655137 00000058 00005a4b
318 # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
319 # 1003655139 00000054 00005c52
321 if (temp > I8K_MAX_TEMP) {
323 prev[sensor] = I8K_MAX_TEMP;
332 static int i8k_get_dell_signature(int req_fn)
334 struct smm_regs regs = { .eax = req_fn, };
341 return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
345 i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg)
349 unsigned char buff[16];
350 int __user *argp = (int __user *)arg;
356 case I8K_BIOS_VERSION:
357 val = i8k_get_bios_version();
362 strlcpy(buff, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
367 val = i8k_get_fn_status();
370 case I8K_POWER_STATUS:
371 val = i8k_get_power_status();
375 val = i8k_get_temp(0);
379 if (copy_from_user(&val, argp, sizeof(int)))
382 val = i8k_get_fan_speed(val);
386 if (copy_from_user(&val, argp, sizeof(int)))
389 val = i8k_get_fan_status(val);
393 if (restricted && !capable(CAP_SYS_ADMIN))
396 if (copy_from_user(&val, argp, sizeof(int)))
399 if (copy_from_user(&speed, argp + 1, sizeof(int)))
402 val = i8k_set_fan(val, speed);
413 case I8K_BIOS_VERSION:
414 if (copy_to_user(argp, &val, 4))
419 if (copy_to_user(argp, buff, 16))
424 if (copy_to_user(argp, &val, sizeof(int)))
433 static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
437 mutex_lock(&i8k_mutex);
438 ret = i8k_ioctl_unlocked(fp, cmd, arg);
439 mutex_unlock(&i8k_mutex);
445 * Print the information for /proc/i8k.
447 static int i8k_proc_show(struct seq_file *seq, void *offset)
449 int fn_key, cpu_temp, ac_power;
450 int left_fan, right_fan, left_speed, right_speed;
452 cpu_temp = i8k_get_temp(0); /* 11100 µs */
453 left_fan = i8k_get_fan_status(I8K_FAN_LEFT); /* 580 µs */
454 right_fan = i8k_get_fan_status(I8K_FAN_RIGHT); /* 580 µs */
455 left_speed = i8k_get_fan_speed(I8K_FAN_LEFT); /* 580 µs */
456 right_speed = i8k_get_fan_speed(I8K_FAN_RIGHT); /* 580 µs */
457 fn_key = i8k_get_fn_status(); /* 750 µs */
459 ac_power = i8k_get_power_status(); /* 14700 µs */
466 * 1) Format version (this will change if format changes)
471 * 6) Right fan status
477 return seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
480 i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
482 left_fan, right_fan, left_speed, right_speed,
486 static int i8k_open_fs(struct inode *inode, struct file *file)
488 return single_open(file, i8k_proc_show, NULL);
496 static ssize_t i8k_hwmon_show_temp(struct device *dev,
497 struct device_attribute *devattr,
500 int index = to_sensor_dev_attr(devattr)->index;
503 temp = i8k_get_temp(index);
506 return sprintf(buf, "%d\n", temp * 1000);
509 static ssize_t i8k_hwmon_show_fan(struct device *dev,
510 struct device_attribute *devattr,
513 int index = to_sensor_dev_attr(devattr)->index;
516 fan_speed = i8k_get_fan_speed(index);
519 return sprintf(buf, "%d\n", fan_speed);
522 static ssize_t i8k_hwmon_show_label(struct device *dev,
523 struct device_attribute *devattr,
526 static const char *labels[3] = {
531 int index = to_sensor_dev_attr(devattr)->index;
533 return sprintf(buf, "%s\n", labels[index]);
536 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 0);
537 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 1);
538 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 2);
539 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 3);
540 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
542 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
544 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, i8k_hwmon_show_label, NULL, 0);
545 static SENSOR_DEVICE_ATTR(fan1_label, S_IRUGO, i8k_hwmon_show_label, NULL, 1);
546 static SENSOR_DEVICE_ATTR(fan2_label, S_IRUGO, i8k_hwmon_show_label, NULL, 2);
548 static struct attribute *i8k_attrs[] = {
549 &sensor_dev_attr_temp1_input.dev_attr.attr, /* 0 */
550 &sensor_dev_attr_temp1_label.dev_attr.attr, /* 1 */
551 &sensor_dev_attr_temp2_input.dev_attr.attr, /* 2 */
552 &sensor_dev_attr_temp3_input.dev_attr.attr, /* 3 */
553 &sensor_dev_attr_temp4_input.dev_attr.attr, /* 4 */
554 &sensor_dev_attr_fan1_input.dev_attr.attr, /* 5 */
555 &sensor_dev_attr_fan1_label.dev_attr.attr, /* 6 */
556 &sensor_dev_attr_fan2_input.dev_attr.attr, /* 7 */
557 &sensor_dev_attr_fan2_label.dev_attr.attr, /* 8 */
561 static umode_t i8k_is_visible(struct kobject *kobj, struct attribute *attr,
564 if ((index == 0 || index == 1) &&
565 !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP1))
567 if (index == 2 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP2))
569 if (index == 3 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP3))
571 if (index == 4 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP4))
573 if ((index == 5 || index == 6) &&
574 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN1))
576 if ((index == 7 || index == 8) &&
577 !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN2))
583 static const struct attribute_group i8k_group = {
585 .is_visible = i8k_is_visible,
587 __ATTRIBUTE_GROUPS(i8k);
589 static int __init i8k_init_hwmon(void)
595 /* CPU temperature attributes, if temperature reading is OK */
596 err = i8k_get_temp(0);
598 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP1;
599 /* check for additional temperature sensors */
600 err = i8k_get_temp(1);
602 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP2;
603 err = i8k_get_temp(2);
605 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP3;
606 err = i8k_get_temp(3);
608 i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP4;
610 /* Left fan attributes, if left fan is present */
611 err = i8k_get_fan_status(I8K_FAN_LEFT);
613 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN1;
615 /* Right fan attributes, if right fan is present */
616 err = i8k_get_fan_status(I8K_FAN_RIGHT);
618 i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN2;
620 i8k_hwmon_dev = hwmon_device_register_with_groups(NULL, "i8k", NULL,
622 if (IS_ERR(i8k_hwmon_dev)) {
623 err = PTR_ERR(i8k_hwmon_dev);
624 i8k_hwmon_dev = NULL;
625 pr_err("hwmon registration failed (%d)\n", err);
631 static struct dmi_system_id i8k_dmi_table[] __initdata = {
633 .ident = "Dell Inspiron",
635 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
636 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
640 .ident = "Dell Latitude",
642 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
643 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
647 .ident = "Dell Inspiron 2",
649 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
650 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
654 .ident = "Dell Latitude 2",
656 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
657 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
660 { /* UK Inspiron 6400 */
661 .ident = "Dell Inspiron 3",
663 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
664 DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
668 .ident = "Dell Inspiron 3",
670 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
671 DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
675 .ident = "Dell Precision",
677 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
678 DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
682 .ident = "Dell Vostro",
684 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
685 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
689 .ident = "Dell XPS421",
691 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
692 DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"),
696 .ident = "Dell Studio",
698 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
699 DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
706 * Probe for the presence of a supported laptop.
708 static int __init i8k_probe(void)
714 * Get DMI information
716 if (!dmi_check_system(i8k_dmi_table)) {
717 if (!ignore_dmi && !force)
720 pr_info("not running on a supported Dell system.\n");
721 pr_info("vendor=%s, model=%s, version=%s\n",
722 i8k_get_dmi_data(DMI_SYS_VENDOR),
723 i8k_get_dmi_data(DMI_PRODUCT_NAME),
724 i8k_get_dmi_data(DMI_BIOS_VERSION));
727 strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
728 sizeof(bios_version));
731 * Get SMM Dell signature
733 if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) &&
734 i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) {
735 pr_err("unable to get SMM Dell signature\n");
741 * Get SMM BIOS version.
743 version = i8k_get_bios_version();
745 pr_warn("unable to get SMM BIOS version\n");
747 buff[0] = (version >> 16) & 0xff;
748 buff[1] = (version >> 8) & 0xff;
749 buff[2] = (version) & 0xff;
752 * If DMI BIOS version is unknown use SMM BIOS version.
754 if (!dmi_get_system_info(DMI_BIOS_VERSION))
755 strlcpy(bios_version, buff, sizeof(bios_version));
758 * Check if the two versions match.
760 if (strncmp(buff, bios_version, sizeof(bios_version)) != 0)
761 pr_warn("BIOS version mismatch: %s != %s\n",
768 static int __init i8k_init(void)
770 struct proc_dir_entry *proc_i8k;
773 /* Are we running on an supported laptop? */
777 /* Register the proc entry */
778 proc_i8k = proc_create("i8k", 0, NULL, &i8k_fops);
782 err = i8k_init_hwmon();
784 goto exit_remove_proc;
789 remove_proc_entry("i8k", NULL);
793 static void __exit i8k_exit(void)
795 hwmon_device_unregister(i8k_hwmon_dev);
796 remove_proc_entry("i8k", NULL);
799 module_init(i8k_init);
800 module_exit(i8k_exit);