2 * i8k.c -- Linux driver for accessing the SMM BIOS on Dell laptops.
3 * See http://www.debian.org/~dz/i8k/ for more information
4 * and for latest version of this driver.
6 * Copyright (C) 2001 Massimo Dal Zotto <dz@debian.org>
9 * Copyright (C) 2011 Jean Delvare <khali@linux-fr.org>
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
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.
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 <asm/uaccess.h>
35 #include <linux/i8k.h>
37 #define I8K_VERSION "1.14 21/02/2005"
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 char bios_machineid[16];
67 static struct device *i8k_hwmon_dev;
69 MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)");
70 MODULE_DESCRIPTION("Driver for accessing SMM BIOS on Dell laptops");
71 MODULE_LICENSE("GPL");
74 module_param(force, bool, 0);
75 MODULE_PARM_DESC(force, "Force loading without checking for supported models");
77 static int ignore_dmi;
78 module_param(ignore_dmi, bool, 0);
79 MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match");
81 static int restricted = true;
82 module_param(restricted, bool, 0);
83 MODULE_PARM_DESC(restricted, "Restrict fan control and serial number to CAP_SYS_ADMIN (default: 1)");
85 static int power_status;
86 module_param(power_status, bool, 0600);
87 MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k (default: 0)");
89 static int fan_mult = I8K_FAN_MULT;
90 module_param(fan_mult, int, 0);
91 MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with");
93 static int i8k_open_fs(struct inode *inode, struct file *file);
94 static long i8k_ioctl(struct file *, unsigned int, unsigned long);
96 static const struct file_operations i8k_fops = {
101 .release = single_release,
102 .unlocked_ioctl = i8k_ioctl,
107 unsigned int ebx __attribute__ ((packed));
108 unsigned int ecx __attribute__ ((packed));
109 unsigned int edx __attribute__ ((packed));
110 unsigned int esi __attribute__ ((packed));
111 unsigned int edi __attribute__ ((packed));
114 static inline const char *i8k_get_dmi_data(int field)
116 const char *dmi_data = dmi_get_system_info(field);
118 return dmi_data && *dmi_data ? dmi_data : "?";
122 * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
124 static int i8k_smm(struct smm_regs *regs)
129 #if defined(CONFIG_X86_64)
130 asm volatile("pushq %%rax\n\t"
131 "movl 0(%%rax),%%edx\n\t"
133 "movl 4(%%rax),%%ebx\n\t"
134 "movl 8(%%rax),%%ecx\n\t"
135 "movl 12(%%rax),%%edx\n\t"
136 "movl 16(%%rax),%%esi\n\t"
137 "movl 20(%%rax),%%edi\n\t"
141 "xchgq %%rax,(%%rsp)\n\t"
142 "movl %%ebx,4(%%rax)\n\t"
143 "movl %%ecx,8(%%rax)\n\t"
144 "movl %%edx,12(%%rax)\n\t"
145 "movl %%esi,16(%%rax)\n\t"
146 "movl %%edi,20(%%rax)\n\t"
148 "movl %%edx,0(%%rax)\n\t"
154 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
156 asm volatile("pushl %%eax\n\t"
157 "movl 0(%%eax),%%edx\n\t"
159 "movl 4(%%eax),%%ebx\n\t"
160 "movl 8(%%eax),%%ecx\n\t"
161 "movl 12(%%eax),%%edx\n\t"
162 "movl 16(%%eax),%%esi\n\t"
163 "movl 20(%%eax),%%edi\n\t"
167 "xchgl %%eax,(%%esp)\n\t"
168 "movl %%ebx,4(%%eax)\n\t"
169 "movl %%ecx,8(%%eax)\n\t"
170 "movl %%edx,12(%%eax)\n\t"
171 "movl %%esi,16(%%eax)\n\t"
172 "movl %%edi,20(%%eax)\n\t"
174 "movl %%edx,0(%%eax)\n\t"
180 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
182 if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax)
189 * Read the bios version. Return the version as an integer corresponding
190 * to the ascii value, for example "A17" is returned as 0x00413137.
192 static int i8k_get_bios_version(void)
194 struct smm_regs regs = { .eax = I8K_SMM_BIOS_VERSION, };
196 return i8k_smm(®s) ? : regs.eax;
200 * Read the Fn key status.
202 static int i8k_get_fn_status(void)
204 struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, };
207 if ((rc = i8k_smm(®s)) < 0)
210 switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
223 * Read the power status.
225 static int i8k_get_power_status(void)
227 struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, };
230 if ((rc = i8k_smm(®s)) < 0)
233 return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY;
237 * Read the fan status.
239 static int i8k_get_fan_status(int fan)
241 struct smm_regs regs = { .eax = I8K_SMM_GET_FAN, };
243 regs.ebx = fan & 0xff;
244 return i8k_smm(®s) ? : regs.eax & 0xff;
248 * Read the fan speed in RPM.
250 static int i8k_get_fan_speed(int fan)
252 struct smm_regs regs = { .eax = I8K_SMM_GET_SPEED, };
254 regs.ebx = fan & 0xff;
255 return i8k_smm(®s) ? : (regs.eax & 0xffff) * fan_mult;
259 * Set the fan speed (off, low, high). Returns the new fan status.
261 static int i8k_set_fan(int fan, int speed)
263 struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
265 speed = (speed < 0) ? 0 : ((speed > I8K_FAN_MAX) ? I8K_FAN_MAX : speed);
266 regs.ebx = (fan & 0xff) | (speed << 8);
268 return i8k_smm(®s) ? : i8k_get_fan_status(fan);
272 * Read the cpu temperature.
274 static int i8k_get_temp(int sensor)
276 struct smm_regs regs = { .eax = I8K_SMM_GET_TEMP, };
280 #ifdef I8K_TEMPERATURE_BUG
283 regs.ebx = sensor & 0xff;
284 if ((rc = i8k_smm(®s)) < 0)
287 temp = regs.eax & 0xff;
289 #ifdef I8K_TEMPERATURE_BUG
291 * Sometimes the temperature sensor returns 0x99, which is out of range.
292 * In this case we return (once) the previous cached value. For example:
293 # 1003655137 00000058 00005a4b
294 # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
295 # 1003655139 00000054 00005c52
297 if (temp > I8K_MAX_TEMP) {
308 static int i8k_get_dell_signature(int req_fn)
310 struct smm_regs regs = { .eax = req_fn, };
313 if ((rc = i8k_smm(®s)) < 0)
316 return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
320 i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg)
324 unsigned char buff[16];
325 int __user *argp = (int __user *)arg;
331 case I8K_BIOS_VERSION:
332 val = i8k_get_bios_version();
336 if (restricted && !capable(CAP_SYS_ADMIN))
339 memset(buff, 0, sizeof(buff));
340 strlcpy(buff, bios_machineid, sizeof(buff));
344 val = i8k_get_fn_status();
347 case I8K_POWER_STATUS:
348 val = i8k_get_power_status();
352 val = i8k_get_temp(0);
356 if (copy_from_user(&val, argp, sizeof(int)))
359 val = i8k_get_fan_speed(val);
363 if (copy_from_user(&val, argp, sizeof(int)))
366 val = i8k_get_fan_status(val);
370 if (restricted && !capable(CAP_SYS_ADMIN))
373 if (copy_from_user(&val, argp, sizeof(int)))
376 if (copy_from_user(&speed, argp + 1, sizeof(int)))
379 val = i8k_set_fan(val, speed);
390 case I8K_BIOS_VERSION:
391 if (copy_to_user(argp, &val, 4))
396 if (copy_to_user(argp, buff, 16))
401 if (copy_to_user(argp, &val, sizeof(int)))
410 static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
414 mutex_lock(&i8k_mutex);
415 ret = i8k_ioctl_unlocked(fp, cmd, arg);
416 mutex_unlock(&i8k_mutex);
422 * Print the information for /proc/i8k.
424 static int i8k_proc_show(struct seq_file *seq, void *offset)
426 int fn_key, cpu_temp, ac_power;
427 int left_fan, right_fan, left_speed, right_speed;
429 cpu_temp = i8k_get_temp(0); /* 11100 µs */
430 left_fan = i8k_get_fan_status(I8K_FAN_LEFT); /* 580 µs */
431 right_fan = i8k_get_fan_status(I8K_FAN_RIGHT); /* 580 µs */
432 left_speed = i8k_get_fan_speed(I8K_FAN_LEFT); /* 580 µs */
433 right_speed = i8k_get_fan_speed(I8K_FAN_RIGHT); /* 580 µs */
434 fn_key = i8k_get_fn_status(); /* 750 µs */
436 ac_power = i8k_get_power_status(); /* 14700 µs */
443 * 1) Format version (this will change if format changes)
448 * 6) Right fan status
454 return seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
457 (restricted && !capable(CAP_SYS_ADMIN)) ? "-1" : bios_machineid,
459 left_fan, right_fan, left_speed, right_speed,
463 static int i8k_open_fs(struct inode *inode, struct file *file)
465 return single_open(file, i8k_proc_show, NULL);
473 static ssize_t i8k_hwmon_show_temp(struct device *dev,
474 struct device_attribute *devattr,
479 cpu_temp = i8k_get_temp(0);
482 return sprintf(buf, "%d\n", cpu_temp * 1000);
485 static ssize_t i8k_hwmon_show_fan(struct device *dev,
486 struct device_attribute *devattr,
489 int index = to_sensor_dev_attr(devattr)->index;
492 fan_speed = i8k_get_fan_speed(index);
495 return sprintf(buf, "%d\n", fan_speed);
498 static ssize_t i8k_hwmon_show_label(struct device *dev,
499 struct device_attribute *devattr,
502 static const char *labels[4] = {
508 int index = to_sensor_dev_attr(devattr)->index;
510 return sprintf(buf, "%s\n", labels[index]);
513 static DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL);
514 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
516 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
518 static SENSOR_DEVICE_ATTR(name, S_IRUGO, i8k_hwmon_show_label, NULL, 0);
519 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, i8k_hwmon_show_label, NULL, 1);
520 static SENSOR_DEVICE_ATTR(fan1_label, S_IRUGO, i8k_hwmon_show_label, NULL, 2);
521 static SENSOR_DEVICE_ATTR(fan2_label, S_IRUGO, i8k_hwmon_show_label, NULL, 3);
523 static void i8k_hwmon_remove_files(struct device *dev)
525 device_remove_file(dev, &dev_attr_temp1_input);
526 device_remove_file(dev, &sensor_dev_attr_fan1_input.dev_attr);
527 device_remove_file(dev, &sensor_dev_attr_fan2_input.dev_attr);
528 device_remove_file(dev, &sensor_dev_attr_temp1_label.dev_attr);
529 device_remove_file(dev, &sensor_dev_attr_fan1_label.dev_attr);
530 device_remove_file(dev, &sensor_dev_attr_fan2_label.dev_attr);
531 device_remove_file(dev, &sensor_dev_attr_name.dev_attr);
534 static int __init i8k_init_hwmon(void)
538 i8k_hwmon_dev = hwmon_device_register(NULL);
539 if (IS_ERR(i8k_hwmon_dev)) {
540 err = PTR_ERR(i8k_hwmon_dev);
541 i8k_hwmon_dev = NULL;
542 printk(KERN_ERR "i8k: hwmon registration failed (%d)\n", err);
546 /* Required name attribute */
547 err = device_create_file(i8k_hwmon_dev,
548 &sensor_dev_attr_name.dev_attr);
550 goto exit_unregister;
552 /* CPU temperature attributes, if temperature reading is OK */
553 err = i8k_get_temp(0);
555 dev_dbg(i8k_hwmon_dev,
556 "Not creating temperature attributes (%d)\n", err);
558 err = device_create_file(i8k_hwmon_dev, &dev_attr_temp1_input);
560 goto exit_remove_files;
561 err = device_create_file(i8k_hwmon_dev,
562 &sensor_dev_attr_temp1_label.dev_attr);
564 goto exit_remove_files;
567 /* Left fan attributes, if left fan is present */
568 err = i8k_get_fan_status(I8K_FAN_LEFT);
570 dev_dbg(i8k_hwmon_dev,
571 "Not creating %s fan attributes (%d)\n", "left", err);
573 err = device_create_file(i8k_hwmon_dev,
574 &sensor_dev_attr_fan1_input.dev_attr);
576 goto exit_remove_files;
577 err = device_create_file(i8k_hwmon_dev,
578 &sensor_dev_attr_fan1_label.dev_attr);
580 goto exit_remove_files;
583 /* Right fan attributes, if right fan is present */
584 err = i8k_get_fan_status(I8K_FAN_RIGHT);
586 dev_dbg(i8k_hwmon_dev,
587 "Not creating %s fan attributes (%d)\n", "right", err);
589 err = device_create_file(i8k_hwmon_dev,
590 &sensor_dev_attr_fan2_input.dev_attr);
592 goto exit_remove_files;
593 err = device_create_file(i8k_hwmon_dev,
594 &sensor_dev_attr_fan2_label.dev_attr);
596 goto exit_remove_files;
602 i8k_hwmon_remove_files(i8k_hwmon_dev);
604 hwmon_device_unregister(i8k_hwmon_dev);
608 static void __exit i8k_exit_hwmon(void)
610 i8k_hwmon_remove_files(i8k_hwmon_dev);
611 hwmon_device_unregister(i8k_hwmon_dev);
614 static struct dmi_system_id __initdata i8k_dmi_table[] = {
616 .ident = "Dell Inspiron",
618 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
619 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
623 .ident = "Dell Latitude",
625 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
626 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
630 .ident = "Dell Inspiron 2",
632 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
633 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
637 .ident = "Dell Latitude 2",
639 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
640 DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
643 { /* UK Inspiron 6400 */
644 .ident = "Dell Inspiron 3",
646 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
647 DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
651 .ident = "Dell Inspiron 3",
653 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
654 DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
658 .ident = "Dell Precision",
660 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
661 DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
665 .ident = "Dell Vostro",
667 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
668 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
672 .ident = "Dell XPS421",
674 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
675 DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"),
682 * Probe for the presence of a supported laptop.
684 static int __init i8k_probe(void)
690 * Get DMI information
692 if (!dmi_check_system(i8k_dmi_table)) {
693 if (!ignore_dmi && !force)
696 printk(KERN_INFO "i8k: not running on a supported Dell system.\n");
697 printk(KERN_INFO "i8k: vendor=%s, model=%s, version=%s\n",
698 i8k_get_dmi_data(DMI_SYS_VENDOR),
699 i8k_get_dmi_data(DMI_PRODUCT_NAME),
700 i8k_get_dmi_data(DMI_BIOS_VERSION));
703 strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION), sizeof(bios_version));
704 strlcpy(bios_machineid, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
705 sizeof(bios_machineid));
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 printk(KERN_ERR "i8k: unable to get SMM Dell signature\n");
718 * Get SMM BIOS version.
720 version = i8k_get_bios_version();
722 printk(KERN_WARNING "i8k: 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 printk(KERN_WARNING "i8k: 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;
764 "Dell laptop SMM driver v%s Massimo Dal Zotto (dz@debian.org)\n",
770 remove_proc_entry("i8k", NULL);
774 static void __exit i8k_exit(void)
777 remove_proc_entry("i8k", NULL);
780 module_init(i8k_init);
781 module_exit(i8k_exit);