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>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
19 #include <linux/module.h>
20 #include <linux/version.h>
21 #include <linux/types.h>
22 #include <linux/init.h>
23 #include <linux/proc_fs.h>
24 #include <linux/apm_bios.h>
25 #include <linux/kbd_kern.h>
26 #include <linux/kbd_ll.h>
27 #include <linux/timer.h>
28 #include <asm/uaccess.h>
31 #include <linux/i8k.h>
33 #define I8K_VERSION "1.13 14/05/2002"
35 #define I8K_SMM_FN_STATUS 0x0025
36 #define I8K_SMM_POWER_STATUS 0x0069
37 #define I8K_SMM_SET_FAN 0x01a3
38 #define I8K_SMM_GET_FAN 0x00a3
39 #define I8K_SMM_GET_SPEED 0x02a3
40 #define I8K_SMM_GET_TEMP 0x10a3
41 #define I8K_SMM_GET_DELL_SIG 0xffa3
42 #define I8K_SMM_BIOS_VERSION 0x00a6
44 #define I8K_FAN_MULT 30
45 #define I8K_MAX_TEMP 127
47 #define I8K_FN_NONE 0x00
48 #define I8K_FN_UP 0x01
49 #define I8K_FN_DOWN 0x02
50 #define I8K_FN_MUTE 0x04
51 #define I8K_FN_MASK 0x07
52 #define I8K_FN_SHIFT 8
54 #define I8K_POWER_AC 0x05
55 #define I8K_POWER_BATTERY 0x01
57 #define I8K_TEMPERATURE_BUG 1
59 #define DELL_SIGNATURE "Dell Computer"
61 /* Interval between polling of keys, in jiffies. */
62 #define I8K_POLL_INTERVAL (HZ/20)
63 #define I8K_REPEAT_DELAY 250 /* 250 ms */
64 #define I8K_REPEAT_RATE 10
67 * (To be escaped) Scancodes for the keys. These were chosen to match other
68 * "Internet" keyboards.
70 #define I8K_KEYS_UP_SCANCODE 0x30
71 #define I8K_KEYS_DOWN_SCANCODE 0x2e
72 #define I8K_KEYS_MUTE_SCANCODE 0x20
74 static char *supported_models[] = {
80 static char system_vendor[48] = "?";
81 static char product_name [48] = "?";
82 static char bios_version [4] = "?";
83 static char serial_number[16] = "?";
87 int handle_buttons = 0;
88 int repeat_delay = I8K_REPEAT_DELAY;
89 int repeat_rate = I8K_REPEAT_RATE;
92 static struct timer_list i8k_keys_timer;
94 MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)");
95 MODULE_DESCRIPTION("Driver for accessing SMM BIOS on Dell laptops");
96 MODULE_LICENSE("GPL");
97 MODULE_PARM(force, "i");
98 MODULE_PARM(restricted, "i");
99 MODULE_PARM(handle_buttons, "i");
100 MODULE_PARM(repeat_delay, "i");
101 MODULE_PARM(repeat_rate, "i");
102 MODULE_PARM(power_status, "i");
103 MODULE_PARM_DESC(force, "Force loading without checking for supported models");
104 MODULE_PARM_DESC(restricted, "Allow fan control if SYS_ADMIN capability set");
105 MODULE_PARM_DESC(handle_buttons, "Generate keyboard events for i8k buttons");
106 MODULE_PARM_DESC(repeat_delay, "I8k buttons repeat delay (ms)");
107 MODULE_PARM_DESC(repeat_rate, "I8k buttons repeat rate");
108 MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k");
110 static ssize_t i8k_read(struct file *, char *, size_t, loff_t *);
111 static int i8k_ioctl(struct inode *, struct file *, unsigned int,
113 static void i8k_keys_set_timer(void);
115 static struct file_operations i8k_fops = {
122 unsigned int ebx __attribute__ ((packed));
123 unsigned int ecx __attribute__ ((packed));
124 unsigned int edx __attribute__ ((packed));
125 unsigned int esi __attribute__ ((packed));
126 unsigned int edi __attribute__ ((packed));
136 * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
138 static int i8k_smm(SMMRegisters *regs)
143 asm("pushl %%eax\n\t" \
144 "movl 0(%%eax),%%edx\n\t" \
146 "movl 4(%%eax),%%ebx\n\t" \
147 "movl 8(%%eax),%%ecx\n\t" \
148 "movl 12(%%eax),%%edx\n\t" \
149 "movl 16(%%eax),%%esi\n\t" \
150 "movl 20(%%eax),%%edi\n\t" \
152 "out %%al,$0xb2\n\t" \
153 "out %%al,$0x84\n\t" \
154 "xchgl %%eax,(%%esp)\n\t"
155 "movl %%ebx,4(%%eax)\n\t" \
156 "movl %%ecx,8(%%eax)\n\t" \
157 "movl %%edx,12(%%eax)\n\t" \
158 "movl %%esi,16(%%eax)\n\t" \
159 "movl %%edi,20(%%eax)\n\t" \
161 "movl %%edx,0(%%eax)\n\t" \
163 "shrl $8,%%eax\n\t" \
167 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
169 if ((rc != 0) || ((regs->eax & 0xffff) == 0xffff) || (regs->eax == eax)) {
177 * Read the bios version. Return the version as an integer corresponding
178 * to the ascii value, for example "A17" is returned as 0x00413137.
180 static int i8k_get_bios_version(void)
182 SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
185 regs.eax = I8K_SMM_BIOS_VERSION;
186 if ((rc=i8k_smm(®s)) < 0) {
194 * Read the machine id.
196 static int i8k_get_serial_number(unsigned char *buff)
198 strncpy(buff, serial_number, 16);
203 * Read the Fn key status.
205 static int i8k_get_fn_status(void)
207 SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
210 regs.eax = I8K_SMM_FN_STATUS;
211 if ((rc=i8k_smm(®s)) < 0) {
215 switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
228 * Read the power status.
230 static int i8k_get_power_status(void)
232 SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
235 regs.eax = I8K_SMM_POWER_STATUS;
236 if ((rc=i8k_smm(®s)) < 0) {
240 switch (regs.eax & 0xff) {
249 * Read the fan status.
251 static int i8k_get_fan_status(int fan)
253 SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
256 regs.eax = I8K_SMM_GET_FAN;
257 regs.ebx = fan & 0xff;
258 if ((rc=i8k_smm(®s)) < 0) {
262 return (regs.eax & 0xff);
266 * Read the fan speed in RPM.
268 static int i8k_get_fan_speed(int fan)
270 SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
273 regs.eax = I8K_SMM_GET_SPEED;
274 regs.ebx = fan & 0xff;
275 if ((rc=i8k_smm(®s)) < 0) {
279 return (regs.eax & 0xffff) * I8K_FAN_MULT;
283 * Set the fan speed (off, low, high). Returns the new fan status.
285 static int i8k_set_fan(int fan, int speed)
287 SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
290 speed = (speed < 0) ? 0 : ((speed > I8K_FAN_MAX) ? I8K_FAN_MAX : speed);
292 regs.eax = I8K_SMM_SET_FAN;
293 regs.ebx = (fan & 0xff) | (speed << 8);
294 if ((rc=i8k_smm(®s)) < 0) {
298 return (i8k_get_fan_status(fan));
302 * Read the cpu temperature.
304 static int i8k_get_cpu_temp(void)
306 SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
310 #ifdef I8K_TEMPERATURE_BUG
314 regs.eax = I8K_SMM_GET_TEMP;
315 if ((rc=i8k_smm(®s)) < 0) {
318 temp = regs.eax & 0xff;
320 #ifdef I8K_TEMPERATURE_BUG
322 * Sometimes the temperature sensor returns 0x99, which is out of range.
323 * In this case we return (once) the previous cached value. For example:
324 # 1003655137 00000058 00005a4b
325 # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
326 # 1003655139 00000054 00005c52
328 if (temp > I8K_MAX_TEMP) {
339 static int i8k_get_dell_signature(void)
341 SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
344 regs.eax = I8K_SMM_GET_DELL_SIG;
345 if ((rc=i8k_smm(®s)) < 0) {
349 if ((regs.eax == 1145651527) && (regs.edx == 1145392204)) {
356 static int i8k_ioctl(struct inode *ip, struct file *fp, unsigned int cmd,
361 unsigned char buff[16];
368 case I8K_BIOS_VERSION:
369 val = i8k_get_bios_version();
374 val = i8k_get_serial_number(buff);
378 val = i8k_get_fn_status();
381 case I8K_POWER_STATUS:
382 val = i8k_get_power_status();
386 val = i8k_get_cpu_temp();
390 if (copy_from_user(&val, (int *)arg, sizeof(int))) {
393 val = i8k_get_fan_speed(val);
397 if (copy_from_user(&val, (int *)arg, sizeof(int))) {
400 val = i8k_get_fan_status(val);
404 if (restricted && !capable(CAP_SYS_ADMIN)) {
407 if (copy_from_user(&val, (int *)arg, sizeof(int))) {
410 if (copy_from_user(&speed, (int *)arg+1, sizeof(int))) {
413 val = i8k_set_fan(val, speed);
425 case I8K_BIOS_VERSION:
426 if (copy_to_user((int *)arg, &val, 4)) {
431 if (copy_to_user((int *)arg, buff, 16)) {
436 if (copy_to_user((int *)arg, &val, sizeof(int))) {
446 * Print the information for /proc/i8k.
448 static int i8k_get_info(char *buffer, char **start, off_t fpos, int length)
450 int n, fn_key, cpu_temp, ac_power;
451 int left_fan, right_fan, left_speed, right_speed;
453 cpu_temp = i8k_get_cpu_temp(); /* 11100 µs */
454 left_fan = i8k_get_fan_status(I8K_FAN_LEFT); /* 580 µs */
455 right_fan = i8k_get_fan_status(I8K_FAN_RIGHT); /* 580 µs */
456 left_speed = i8k_get_fan_speed(I8K_FAN_LEFT); /* 580 µs */
457 right_speed = i8k_get_fan_speed(I8K_FAN_RIGHT); /* 580 µs */
458 fn_key = i8k_get_fn_status(); /* 750 µs */
460 ac_power = i8k_get_power_status(); /* 14700 µs */
468 * 1) Format version (this will change if format changes)
473 * 6) Right fan status
479 n = sprintf(buffer, "%s %s %s %d %d %d %d %d %d %d\n",
494 static ssize_t i8k_read(struct file *f, char *buffer, size_t len, loff_t *fpos)
499 n = i8k_get_info(info, NULL, 0, 128);
508 if ((*fpos + len) >= n) {
512 if (copy_to_user(buffer, info, len) != 0) {
521 * i8k_keys stuff. Thanks to David Bustos <bustos@caltech.edu>
524 static unsigned char i8k_keys_make_scancode(int x) {
526 case I8K_FN_UP: return I8K_KEYS_UP_SCANCODE;
527 case I8K_FN_DOWN: return I8K_KEYS_DOWN_SCANCODE;
528 case I8K_FN_MUTE: return I8K_KEYS_MUTE_SCANCODE;
534 static void i8k_keys_poll(unsigned long data) {
536 static int repeat = 0;
540 curr = i8k_get_fn_status();
543 repeat = jiffies + (HZ * repeat_delay / 1000);
546 handle_scancode(0xe0, 0);
547 handle_scancode(i8k_keys_make_scancode(last), 0);
551 handle_scancode(0xe0, 1);
552 handle_scancode(i8k_keys_make_scancode(curr), 1);
555 /* Generate keyboard repeat events with current scancode -- dz */
556 if ((curr) && (repeat_rate > 0) && (jiffies >= repeat)) {
557 repeat = jiffies + (HZ / repeat_rate);
558 handle_scancode(0xe0, 1);
559 handle_scancode(i8k_keys_make_scancode(curr), 1);
566 /* Reset the timer. */
567 i8k_keys_set_timer();
570 static void i8k_keys_set_timer() {
571 i8k_keys_timer.expires = jiffies + I8K_POLL_INTERVAL;
572 add_timer(&i8k_keys_timer);
575 static char* __init string_trim(char *s, int size)
580 if ((len = strlen(s)) > size) {
584 for (p=s+len-1; len && (*p==' '); len--,p--) {
591 /* DMI code, stolen from arch/i386/kernel/dmi_scan.c */
594 * |<-- dmi->length -->|
596 * |dmi header s=N | string1,\0, ..., stringN,\0, ..., \0
598 * +-----------------------+
600 static char* __init dmi_string(DMIHeader *dmi, u8 s)
609 p = (u8 *)dmi + dmi->length;
619 static void __init dmi_decode(DMIHeader *dmi)
621 u8 *data = (u8 *) dmi;
626 printk("%08x ", (int)data);
627 for (i=0; i<data[1] && i<64; i++) {
628 printk("%02x ", data[i]);
634 case 0: /* BIOS Information */
635 p = dmi_string(dmi,data[5]);
637 strncpy(bios_version, p, sizeof(bios_version));
638 string_trim(bios_version, sizeof(bios_version));
641 case 1: /* System Information */
642 p = dmi_string(dmi,data[4]);
644 strncpy(system_vendor, p, sizeof(system_vendor));
645 string_trim(system_vendor, sizeof(system_vendor));
647 p = dmi_string(dmi,data[5]);
649 strncpy(product_name, p, sizeof(product_name));
650 string_trim(product_name, sizeof(product_name));
652 p = dmi_string(dmi,data[7]);
654 strncpy(serial_number, p, sizeof(serial_number));
655 string_trim(serial_number, sizeof(serial_number));
661 static int __init dmi_table(u32 base, int len, int num, void (*fn)(DMIHeader*))
668 buf = ioremap(base, len);
675 * Stop when we see al the items the table claimed to have
676 * or we run off the end of the table (also happens)
678 while ((i<num) && ((data-buf) < len)) {
679 dmi = (DMIHeader *)data;
681 * Avoid misparsing crud if the length of the last
684 if ((data-buf+dmi->length) >= len) {
690 * Don't go off the end of the data if there is
691 * stuff looking like string fill past the end
693 while (((data-buf) < len) && (*data || data[1])) {
704 static int __init dmi_iterate(void (*decode)(DMIHeader *))
706 unsigned char buf[20];
707 long fp = 0x000e0000L;
710 while (fp < 0x000fffffL) {
712 isa_memcpy_fromio(buf, fp, 20);
713 if (memcmp(buf, "_DMI_", 5)==0) {
714 u16 num = buf[13]<<8 | buf[12];
715 u16 len = buf [7]<<8 | buf [6];
716 u32 base = buf[11]<<24 | buf[10]<<16 | buf[9]<<8 | buf[8];
718 printk(KERN_INFO "DMI %d.%d present.\n",
719 buf[14]>>4, buf[14]&0x0F);
720 printk(KERN_INFO "%d structures occupying %d bytes.\n",
721 buf[13]<<8 | buf[12],
722 buf [7]<<8 | buf[6]);
723 printk(KERN_INFO "DMI table at 0x%08X.\n",
724 buf[11]<<24 | buf[10]<<16 | buf[9]<<8 | buf[8]);
726 if (dmi_table(base, len, num, decode)==0) {
733 /* end of DMI code */
736 * Get DMI information.
738 static int __init i8k_dmi_probe(void)
742 if (dmi_iterate(dmi_decode) != 0) {
743 printk(KERN_INFO "i8k: unable to get DMI information\n");
747 if (strncmp(system_vendor,DELL_SIGNATURE,strlen(DELL_SIGNATURE)) != 0) {
748 printk(KERN_INFO "i8k: not running on a Dell system\n");
752 for (p=supported_models; ; p++) {
754 printk(KERN_INFO "i8k: unsupported model: %s\n", product_name);
757 if (strncmp(product_name,*p,strlen(*p)) == 0) {
766 * Probe for the presence of a supported laptop.
768 static int __init i8k_probe(void)
775 * Get DMI information
777 if (i8k_dmi_probe() != 0) {
778 printk(KERN_INFO "i8k: vendor=%s, model=%s, version=%s\n",
779 system_vendor, product_name, bios_version);
783 * Get SMM Dell signature
785 if (i8k_get_dell_signature() != 0) {
786 printk(KERN_INFO "i8k: unable to get SMM Dell signature\n");
792 * Get SMM BIOS version.
794 version = i8k_get_bios_version();
796 printk(KERN_INFO "i8k: unable to get SMM BIOS version\n");
799 buff[0] = (version >> 16) & 0xff;
800 buff[1] = (version >> 8) & 0xff;
801 buff[2] = (version) & 0xff;
804 * If DMI BIOS version is unknown use SMM BIOS version.
806 if (bios_version[0] == '?') {
807 strcpy(bios_version, buff);
810 * Check if the two versions match.
812 if (strncmp(buff,bios_version,sizeof(bios_version)) != 0) {
813 printk(KERN_INFO "i8k: BIOS version mismatch: %s != %s\n",
818 if (!smm_found && !force) {
828 int __init i8k_init(void)
830 struct proc_dir_entry *proc_i8k;
832 /* Are we running on an supported laptop? */
833 if (i8k_probe() != 0) {
837 /* Register the proc entry */
838 proc_i8k = create_proc_info_entry("i8k", 0, NULL, i8k_get_info);
842 proc_i8k->proc_fops = &i8k_fops;
843 SET_MODULE_OWNER(proc_i8k);
846 "Dell laptop SMM driver v%s Massimo Dal Zotto (dz@debian.org)\n",
849 /* Register the i8k_keys timer. */
850 if (handle_buttons) {
852 "i8k: enabling buttons events, delay=%d, rate=%d\n",
853 repeat_delay, repeat_rate);
854 init_timer(&i8k_keys_timer);
855 i8k_keys_timer.function = i8k_keys_poll;
856 i8k_keys_set_timer();
863 int init_module(void)
868 void cleanup_module(void)
870 /* Remove the proc entry */
871 remove_proc_entry("i8k", NULL);
873 /* Unregister the i8k_keys timer. */
874 while (handle_buttons && !del_timer(&i8k_keys_timer)) {
875 schedule_timeout(I8K_POLL_INTERVAL);
878 printk(KERN_INFO "i8k: module unloaded\n");