Merge with Linux 2.5.59.
[linux-2.6/linux-mips.git] / drivers / char / i8k.c
blob3ecb6223d9325db206b1d2d996c61d97545e0284
1 /*
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
11 * later version.
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 <asm/uaccess.h>
26 #include <asm/io.h>
28 #include <linux/i8k.h>
30 #define I8K_VERSION "1.13 14/05/2002"
32 #define I8K_SMM_FN_STATUS 0x0025
33 #define I8K_SMM_POWER_STATUS 0x0069
34 #define I8K_SMM_SET_FAN 0x01a3
35 #define I8K_SMM_GET_FAN 0x00a3
36 #define I8K_SMM_GET_SPEED 0x02a3
37 #define I8K_SMM_GET_TEMP 0x10a3
38 #define I8K_SMM_GET_DELL_SIG 0xffa3
39 #define I8K_SMM_BIOS_VERSION 0x00a6
41 #define I8K_FAN_MULT 30
42 #define I8K_MAX_TEMP 127
44 #define I8K_FN_NONE 0x00
45 #define I8K_FN_UP 0x01
46 #define I8K_FN_DOWN 0x02
47 #define I8K_FN_MUTE 0x04
48 #define I8K_FN_MASK 0x07
49 #define I8K_FN_SHIFT 8
51 #define I8K_POWER_AC 0x05
52 #define I8K_POWER_BATTERY 0x01
54 #define I8K_TEMPERATURE_BUG 1
56 #define DELL_SIGNATURE "Dell Computer"
58 static char *supported_models[] = {
59 "Inspiron",
60 "Latitude",
61 NULL
64 static char system_vendor[48] = "?";
65 static char product_name [48] = "?";
66 static char bios_version [4] = "?";
67 static char serial_number[16] = "?";
69 static int force = 0;
70 static int restricted = 0;
71 static int power_status = 0;
73 MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)");
74 MODULE_DESCRIPTION("Driver for accessing SMM BIOS on Dell laptops");
75 MODULE_LICENSE("GPL");
76 MODULE_PARM(force, "i");
77 MODULE_PARM(restricted, "i");
78 MODULE_PARM(power_status, "i");
79 MODULE_PARM_DESC(force, "Force loading without checking for supported models");
80 MODULE_PARM_DESC(restricted, "Allow fan control if SYS_ADMIN capability set");
81 MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k");
83 static ssize_t i8k_read(struct file *, char *, size_t, loff_t *);
84 static int i8k_ioctl(struct inode *, struct file *, unsigned int,
85 unsigned long);
87 static struct file_operations i8k_fops = {
88 .read = i8k_read,
89 .ioctl = i8k_ioctl,
92 typedef struct {
93 unsigned int eax;
94 unsigned int ebx __attribute__ ((packed));
95 unsigned int ecx __attribute__ ((packed));
96 unsigned int edx __attribute__ ((packed));
97 unsigned int esi __attribute__ ((packed));
98 unsigned int edi __attribute__ ((packed));
99 } SMMRegisters;
101 typedef struct {
102 u8 type;
103 u8 length;
104 u16 handle;
105 } DMIHeader;
108 * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
110 static int i8k_smm(SMMRegisters *regs)
112 int rc;
113 int eax = regs->eax;
115 asm("pushl %%eax\n\t" \
116 "movl 0(%%eax),%%edx\n\t" \
117 "push %%edx\n\t" \
118 "movl 4(%%eax),%%ebx\n\t" \
119 "movl 8(%%eax),%%ecx\n\t" \
120 "movl 12(%%eax),%%edx\n\t" \
121 "movl 16(%%eax),%%esi\n\t" \
122 "movl 20(%%eax),%%edi\n\t" \
123 "popl %%eax\n\t" \
124 "out %%al,$0xb2\n\t" \
125 "out %%al,$0x84\n\t" \
126 "xchgl %%eax,(%%esp)\n\t"
127 "movl %%ebx,4(%%eax)\n\t" \
128 "movl %%ecx,8(%%eax)\n\t" \
129 "movl %%edx,12(%%eax)\n\t" \
130 "movl %%esi,16(%%eax)\n\t" \
131 "movl %%edi,20(%%eax)\n\t" \
132 "popl %%edx\n\t" \
133 "movl %%edx,0(%%eax)\n\t" \
134 "lahf\n\t" \
135 "shrl $8,%%eax\n\t" \
136 "andl $1,%%eax\n" \
137 : "=a" (rc)
138 : "a" (regs)
139 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
141 if ((rc != 0) || ((regs->eax & 0xffff) == 0xffff) || (regs->eax == eax)) {
142 return -EINVAL;
145 return 0;
149 * Read the bios version. Return the version as an integer corresponding
150 * to the ascii value, for example "A17" is returned as 0x00413137.
152 static int i8k_get_bios_version(void)
154 SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
155 int rc;
157 regs.eax = I8K_SMM_BIOS_VERSION;
158 if ((rc=i8k_smm(&regs)) < 0) {
159 return rc;
162 return regs.eax;
166 * Read the machine id.
168 static int i8k_get_serial_number(unsigned char *buff)
170 strncpy(buff, serial_number, 16);
171 return 0;
175 * Read the Fn key status.
177 static int i8k_get_fn_status(void)
179 SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
180 int rc;
182 regs.eax = I8K_SMM_FN_STATUS;
183 if ((rc=i8k_smm(&regs)) < 0) {
184 return rc;
187 switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
188 case I8K_FN_UP:
189 return I8K_VOL_UP;
190 case I8K_FN_DOWN:
191 return I8K_VOL_DOWN;
192 case I8K_FN_MUTE:
193 return I8K_VOL_MUTE;
194 default:
195 return 0;
200 * Read the power status.
202 static int i8k_get_power_status(void)
204 SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
205 int rc;
207 regs.eax = I8K_SMM_POWER_STATUS;
208 if ((rc=i8k_smm(&regs)) < 0) {
209 return rc;
212 switch (regs.eax & 0xff) {
213 case I8K_POWER_AC:
214 return I8K_AC;
215 default:
216 return I8K_BATTERY;
221 * Read the fan status.
223 static int i8k_get_fan_status(int fan)
225 SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
226 int rc;
228 regs.eax = I8K_SMM_GET_FAN;
229 regs.ebx = fan & 0xff;
230 if ((rc=i8k_smm(&regs)) < 0) {
231 return rc;
234 return (regs.eax & 0xff);
238 * Read the fan speed in RPM.
240 static int i8k_get_fan_speed(int fan)
242 SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
243 int rc;
245 regs.eax = I8K_SMM_GET_SPEED;
246 regs.ebx = fan & 0xff;
247 if ((rc=i8k_smm(&regs)) < 0) {
248 return rc;
251 return (regs.eax & 0xffff) * I8K_FAN_MULT;
255 * Set the fan speed (off, low, high). Returns the new fan status.
257 static int i8k_set_fan(int fan, int speed)
259 SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
260 int rc;
262 speed = (speed < 0) ? 0 : ((speed > I8K_FAN_MAX) ? I8K_FAN_MAX : speed);
264 regs.eax = I8K_SMM_SET_FAN;
265 regs.ebx = (fan & 0xff) | (speed << 8);
266 if ((rc=i8k_smm(&regs)) < 0) {
267 return rc;
270 return (i8k_get_fan_status(fan));
274 * Read the cpu temperature.
276 static int i8k_get_cpu_temp(void)
278 SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
279 int rc;
280 int temp;
282 #ifdef I8K_TEMPERATURE_BUG
283 static int prev = 0;
284 #endif
286 regs.eax = I8K_SMM_GET_TEMP;
287 if ((rc=i8k_smm(&regs)) < 0) {
288 return rc;
290 temp = regs.eax & 0xff;
292 #ifdef I8K_TEMPERATURE_BUG
294 * Sometimes the temperature sensor returns 0x99, which is out of range.
295 * In this case we return (once) the previous cached value. For example:
296 # 1003655137 00000058 00005a4b
297 # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
298 # 1003655139 00000054 00005c52
300 if (temp > I8K_MAX_TEMP) {
301 temp = prev;
302 prev = I8K_MAX_TEMP;
303 } else {
304 prev = temp;
306 #endif
308 return temp;
311 static int i8k_get_dell_signature(void)
313 SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
314 int rc;
316 regs.eax = I8K_SMM_GET_DELL_SIG;
317 if ((rc=i8k_smm(&regs)) < 0) {
318 return rc;
321 if ((regs.eax == 1145651527) && (regs.edx == 1145392204)) {
322 return 0;
323 } else {
324 return -1;
328 static int i8k_ioctl(struct inode *ip, struct file *fp, unsigned int cmd,
329 unsigned long arg)
331 int val;
332 int speed;
333 unsigned char buff[16];
335 if (!arg) {
336 return -EINVAL;
339 switch (cmd) {
340 case I8K_BIOS_VERSION:
341 val = i8k_get_bios_version();
342 break;
344 case I8K_MACHINE_ID:
345 memset(buff, 0, 16);
346 val = i8k_get_serial_number(buff);
347 break;
349 case I8K_FN_STATUS:
350 val = i8k_get_fn_status();
351 break;
353 case I8K_POWER_STATUS:
354 val = i8k_get_power_status();
355 break;
357 case I8K_GET_TEMP:
358 val = i8k_get_cpu_temp();
359 break;
361 case I8K_GET_SPEED:
362 if (copy_from_user(&val, (int *)arg, sizeof(int))) {
363 return -EFAULT;
365 val = i8k_get_fan_speed(val);
366 break;
368 case I8K_GET_FAN:
369 if (copy_from_user(&val, (int *)arg, sizeof(int))) {
370 return -EFAULT;
372 val = i8k_get_fan_status(val);
373 break;
375 case I8K_SET_FAN:
376 if (restricted && !capable(CAP_SYS_ADMIN)) {
377 return -EPERM;
379 if (copy_from_user(&val, (int *)arg, sizeof(int))) {
380 return -EFAULT;
382 if (copy_from_user(&speed, (int *)arg+1, sizeof(int))) {
383 return -EFAULT;
385 val = i8k_set_fan(val, speed);
386 break;
388 default:
389 return -EINVAL;
392 if (val < 0) {
393 return val;
396 switch (cmd) {
397 case I8K_BIOS_VERSION:
398 if (copy_to_user((int *)arg, &val, 4)) {
399 return -EFAULT;
401 break;
402 case I8K_MACHINE_ID:
403 if (copy_to_user((int *)arg, buff, 16)) {
404 return -EFAULT;
406 break;
407 default:
408 if (copy_to_user((int *)arg, &val, sizeof(int))) {
409 return -EFAULT;
411 break;
414 return 0;
418 * Print the information for /proc/i8k.
420 static int i8k_get_info(char *buffer, char **start, off_t fpos, int length)
422 int n, fn_key, cpu_temp, ac_power;
423 int left_fan, right_fan, left_speed, right_speed;
425 cpu_temp = i8k_get_cpu_temp(); /* 11100 µs */
426 left_fan = i8k_get_fan_status(I8K_FAN_LEFT); /* 580 µs */
427 right_fan = i8k_get_fan_status(I8K_FAN_RIGHT); /* 580 µs */
428 left_speed = i8k_get_fan_speed(I8K_FAN_LEFT); /* 580 µs */
429 right_speed = i8k_get_fan_speed(I8K_FAN_RIGHT); /* 580 µs */
430 fn_key = i8k_get_fn_status(); /* 750 µs */
431 if (power_status) {
432 ac_power = i8k_get_power_status(); /* 14700 µs */
433 } else {
434 ac_power = -1;
438 * Info:
440 * 1) Format version (this will change if format changes)
441 * 2) BIOS version
442 * 3) BIOS machine ID
443 * 4) Cpu temperature
444 * 5) Left fan status
445 * 6) Right fan status
446 * 7) Left fan speed
447 * 8) Right fan speed
448 * 9) AC power
449 * 10) Fn Key status
451 n = sprintf(buffer, "%s %s %s %d %d %d %d %d %d %d\n",
452 I8K_PROC_FMT,
453 bios_version,
454 serial_number,
455 cpu_temp,
456 left_fan,
457 right_fan,
458 left_speed,
459 right_speed,
460 ac_power,
461 fn_key);
463 return n;
466 static ssize_t i8k_read(struct file *f, char *buffer, size_t len, loff_t *fpos)
468 int n;
469 char info[128];
471 n = i8k_get_info(info, NULL, 0, 128);
472 if (n <= 0) {
473 return n;
476 if (*fpos >= n) {
477 return 0;
480 if ((*fpos + len) >= n) {
481 len = n - *fpos;
484 if (copy_to_user(buffer, info, len) != 0) {
485 return -EFAULT;
488 *fpos += len;
489 return len;
492 static char* __init string_trim(char *s, int size)
494 int len;
495 char *p;
497 if ((len = strlen(s)) > size) {
498 len = size;
501 for (p=s+len-1; len && (*p==' '); len--,p--) {
502 *p = '\0';
505 return s;
508 /* DMI code, stolen from arch/i386/kernel/dmi_scan.c */
511 * |<-- dmi->length -->|
512 * | |
513 * |dmi header s=N | string1,\0, ..., stringN,\0, ..., \0
514 * | |
515 * +-----------------------+
517 static char* __init dmi_string(DMIHeader *dmi, u8 s)
519 u8 *p;
521 if (!s) {
522 return "";
524 s--;
526 p = (u8 *)dmi + dmi->length;
527 while (s > 0) {
528 p += strlen(p);
529 p++;
530 s--;
533 return p;
536 static void __init dmi_decode(DMIHeader *dmi)
538 u8 *data = (u8 *) dmi;
539 char *p;
541 #ifdef I8K_DEBUG
542 int i;
543 printk("%08x ", (int)data);
544 for (i=0; i<data[1] && i<64; i++) {
545 printk("%02x ", data[i]);
547 printk("\n");
548 #endif
550 switch (dmi->type) {
551 case 0: /* BIOS Information */
552 p = dmi_string(dmi,data[5]);
553 if (*p) {
554 strncpy(bios_version, p, sizeof(bios_version));
555 string_trim(bios_version, sizeof(bios_version));
557 break;
558 case 1: /* System Information */
559 p = dmi_string(dmi,data[4]);
560 if (*p) {
561 strncpy(system_vendor, p, sizeof(system_vendor));
562 string_trim(system_vendor, sizeof(system_vendor));
564 p = dmi_string(dmi,data[5]);
565 if (*p) {
566 strncpy(product_name, p, sizeof(product_name));
567 string_trim(product_name, sizeof(product_name));
569 p = dmi_string(dmi,data[7]);
570 if (*p) {
571 strncpy(serial_number, p, sizeof(serial_number));
572 string_trim(serial_number, sizeof(serial_number));
574 break;
578 static int __init dmi_table(u32 base, int len, int num, void (*fn)(DMIHeader*))
580 u8 *buf;
581 u8 *data;
582 DMIHeader *dmi;
583 int i = 1;
585 buf = ioremap(base, len);
586 if (buf == NULL) {
587 return -1;
589 data = buf;
592 * Stop when we see al the items the table claimed to have
593 * or we run off the end of the table (also happens)
595 while ((i<num) && ((data-buf) < len)) {
596 dmi = (DMIHeader *)data;
598 * Avoid misparsing crud if the length of the last
599 * record is crap
601 if ((data-buf+dmi->length) >= len) {
602 break;
604 fn(dmi);
605 data += dmi->length;
607 * Don't go off the end of the data if there is
608 * stuff looking like string fill past the end
610 while (((data-buf) < len) && (*data || data[1])) {
611 data++;
613 data += 2;
614 i++;
616 iounmap(buf);
618 return 0;
621 static int __init dmi_iterate(void (*decode)(DMIHeader *))
623 unsigned char buf[20];
624 long fp = 0x000e0000L;
625 fp -= 16;
627 while (fp < 0x000fffffL) {
628 fp += 16;
629 isa_memcpy_fromio(buf, fp, 20);
630 if (memcmp(buf, "_DMI_", 5)==0) {
631 u16 num = buf[13]<<8 | buf[12];
632 u16 len = buf [7]<<8 | buf [6];
633 u32 base = buf[11]<<24 | buf[10]<<16 | buf[9]<<8 | buf[8];
634 #ifdef I8K_DEBUG
635 printk(KERN_INFO "DMI %d.%d present.\n",
636 buf[14]>>4, buf[14]&0x0F);
637 printk(KERN_INFO "%d structures occupying %d bytes.\n",
638 buf[13]<<8 | buf[12],
639 buf [7]<<8 | buf[6]);
640 printk(KERN_INFO "DMI table at 0x%08X.\n",
641 buf[11]<<24 | buf[10]<<16 | buf[9]<<8 | buf[8]);
642 #endif
643 if (dmi_table(base, len, num, decode)==0) {
644 return 0;
648 return -1;
650 /* end of DMI code */
653 * Get DMI information.
655 static int __init i8k_dmi_probe(void)
657 char **p;
659 if (dmi_iterate(dmi_decode) != 0) {
660 printk(KERN_INFO "i8k: unable to get DMI information\n");
661 return -ENODEV;
664 if (strncmp(system_vendor,DELL_SIGNATURE,strlen(DELL_SIGNATURE)) != 0) {
665 printk(KERN_INFO "i8k: not running on a Dell system\n");
666 return -ENODEV;
669 for (p=supported_models; ; p++) {
670 if (!*p) {
671 printk(KERN_INFO "i8k: unsupported model: %s\n", product_name);
672 return -ENODEV;
674 if (strncmp(product_name,*p,strlen(*p)) == 0) {
675 break;
679 return 0;
683 * Probe for the presence of a supported laptop.
685 static int __init i8k_probe(void)
687 char buff[4];
688 int version;
689 int smm_found = 0;
692 * Get DMI information
694 if (i8k_dmi_probe() != 0) {
695 printk(KERN_INFO "i8k: vendor=%s, model=%s, version=%s\n",
696 system_vendor, product_name, bios_version);
700 * Get SMM Dell signature
702 if (i8k_get_dell_signature() != 0) {
703 printk(KERN_INFO "i8k: unable to get SMM Dell signature\n");
704 } else {
705 smm_found = 1;
709 * Get SMM BIOS version.
711 version = i8k_get_bios_version();
712 if (version <= 0) {
713 printk(KERN_INFO "i8k: unable to get SMM BIOS version\n");
714 } else {
715 smm_found = 1;
716 buff[0] = (version >> 16) & 0xff;
717 buff[1] = (version >> 8) & 0xff;
718 buff[2] = (version) & 0xff;
719 buff[3] = '\0';
721 * If DMI BIOS version is unknown use SMM BIOS version.
723 if (bios_version[0] == '?') {
724 strcpy(bios_version, buff);
727 * Check if the two versions match.
729 if (strncmp(buff,bios_version,sizeof(bios_version)) != 0) {
730 printk(KERN_INFO "i8k: BIOS version mismatch: %s != %s\n",
731 buff, bios_version);
735 if (!smm_found && !force) {
736 return -ENODEV;
739 return 0;
742 #ifdef MODULE
743 static
744 #endif
745 int __init i8k_init(void)
747 struct proc_dir_entry *proc_i8k;
749 /* Are we running on an supported laptop? */
750 if (i8k_probe() != 0) {
751 return -ENODEV;
754 /* Register the proc entry */
755 proc_i8k = create_proc_info_entry("i8k", 0, NULL, i8k_get_info);
756 if (!proc_i8k) {
757 return -ENOENT;
759 proc_i8k->proc_fops = &i8k_fops;
760 SET_MODULE_OWNER(proc_i8k);
762 printk(KERN_INFO
763 "Dell laptop SMM driver v%s Massimo Dal Zotto (dz@debian.org)\n",
764 I8K_VERSION);
766 return 0;
769 #ifdef MODULE
770 int init_module(void)
772 return i8k_init();
775 void cleanup_module(void)
777 /* Remove the proc entry */
778 remove_proc_entry("i8k", NULL);
780 printk(KERN_INFO "i8k: module unloaded\n");
782 #endif
784 /* end of file */