patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / char / i8k.c
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.
5  *
6  * Copyright (C) 2001  Massimo Dal Zotto <dz@debian.org>
7  *
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.
12  *
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.
17  */
18
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/init.h>
22 #include <linux/proc_fs.h>
23 #include <linux/apm_bios.h>
24 #include <asm/uaccess.h>
25 #include <asm/io.h>
26
27 #include <linux/i8k.h>
28
29 #define I8K_VERSION             "1.13 14/05/2002"
30
31 #define I8K_SMM_FN_STATUS       0x0025
32 #define I8K_SMM_POWER_STATUS    0x0069
33 #define I8K_SMM_SET_FAN         0x01a3
34 #define I8K_SMM_GET_FAN         0x00a3
35 #define I8K_SMM_GET_SPEED       0x02a3
36 #define I8K_SMM_GET_TEMP        0x10a3
37 #define I8K_SMM_GET_DELL_SIG    0xffa3
38 #define I8K_SMM_BIOS_VERSION    0x00a6
39
40 #define I8K_FAN_MULT            30
41 #define I8K_MAX_TEMP            127
42
43 #define I8K_FN_NONE             0x00
44 #define I8K_FN_UP               0x01
45 #define I8K_FN_DOWN             0x02
46 #define I8K_FN_MUTE             0x04
47 #define I8K_FN_MASK             0x07
48 #define I8K_FN_SHIFT            8
49
50 #define I8K_POWER_AC            0x05
51 #define I8K_POWER_BATTERY       0x01
52
53 #define I8K_TEMPERATURE_BUG     1
54
55 #define DELL_SIGNATURE          "Dell Computer"
56
57 static char *supported_models[] = {
58     "Inspiron",
59     "Latitude",
60     NULL
61 };
62
63 static char system_vendor[48] = "?";
64 static char product_name [48] = "?";
65 static char bios_version [4]  = "?";
66 static char serial_number[16] = "?";
67
68 static int force = 0;
69 static int restricted = 0;
70 static int power_status = 0;
71
72 MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)");
73 MODULE_DESCRIPTION("Driver for accessing SMM BIOS on Dell laptops");
74 MODULE_LICENSE("GPL");
75 MODULE_PARM(force, "i");
76 MODULE_PARM(restricted, "i");
77 MODULE_PARM(power_status, "i");
78 MODULE_PARM_DESC(force, "Force loading without checking for supported models");
79 MODULE_PARM_DESC(restricted, "Allow fan control if SYS_ADMIN capability set");
80 MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k");
81
82 static ssize_t i8k_read(struct file *, char __user *, size_t, loff_t *);
83 static int i8k_ioctl(struct inode *, struct file *, unsigned int,
84                      unsigned long);
85
86 static struct file_operations i8k_fops = {
87     .read       = i8k_read,
88     .ioctl      = i8k_ioctl,
89 };
90
91 typedef struct {
92     unsigned int eax;
93     unsigned int ebx __attribute__ ((packed));
94     unsigned int ecx __attribute__ ((packed));
95     unsigned int edx __attribute__ ((packed));
96     unsigned int esi __attribute__ ((packed));
97     unsigned int edi __attribute__ ((packed));
98 } SMMRegisters;
99
100 typedef struct {
101     u8  type;
102     u8  length;
103     u16 handle;
104 } DMIHeader;
105
106 /*
107  * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
108  */
109 static int i8k_smm(SMMRegisters *regs)
110 {
111     int rc;
112     int eax = regs->eax;
113
114     asm("pushl %%eax\n\t" \
115         "movl 0(%%eax),%%edx\n\t" \
116         "push %%edx\n\t" \
117         "movl 4(%%eax),%%ebx\n\t" \
118         "movl 8(%%eax),%%ecx\n\t" \
119         "movl 12(%%eax),%%edx\n\t" \
120         "movl 16(%%eax),%%esi\n\t" \
121         "movl 20(%%eax),%%edi\n\t" \
122         "popl %%eax\n\t" \
123         "out %%al,$0xb2\n\t" \
124         "out %%al,$0x84\n\t" \
125         "xchgl %%eax,(%%esp)\n\t"
126         "movl %%ebx,4(%%eax)\n\t" \
127         "movl %%ecx,8(%%eax)\n\t" \
128         "movl %%edx,12(%%eax)\n\t" \
129         "movl %%esi,16(%%eax)\n\t" \
130         "movl %%edi,20(%%eax)\n\t" \
131         "popl %%edx\n\t" \
132         "movl %%edx,0(%%eax)\n\t" \
133         "lahf\n\t" \
134         "shrl $8,%%eax\n\t" \
135         "andl $1,%%eax\n" \
136         : "=a" (rc)
137         : "a" (regs)
138         : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
139
140     if ((rc != 0) || ((regs->eax & 0xffff) == 0xffff) || (regs->eax == eax)) {
141         return -EINVAL;
142     }
143
144     return 0;
145 }
146
147 /*
148  * Read the bios version. Return the version as an integer corresponding
149  * to the ascii value, for example "A17" is returned as 0x00413137.
150  */
151 static int i8k_get_bios_version(void)
152 {
153     SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
154     int rc;
155
156     regs.eax = I8K_SMM_BIOS_VERSION;
157     if ((rc=i8k_smm(&regs)) < 0) {
158         return rc;
159     }
160
161     return regs.eax;
162 }
163
164 /*
165  * Read the machine id.
166  */
167 static int i8k_get_serial_number(unsigned char *buff)
168 {
169     strlcpy(buff, serial_number, sizeof(serial_number));
170     return 0;
171 }
172
173 /*
174  * Read the Fn key status.
175  */
176 static int i8k_get_fn_status(void)
177 {
178     SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
179     int rc;
180
181     regs.eax = I8K_SMM_FN_STATUS;
182     if ((rc=i8k_smm(&regs)) < 0) {
183         return rc;
184     }
185
186     switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
187     case I8K_FN_UP:
188         return I8K_VOL_UP;
189     case I8K_FN_DOWN:
190         return I8K_VOL_DOWN;
191     case I8K_FN_MUTE:
192         return I8K_VOL_MUTE;
193     default:
194         return 0;
195     }
196 }
197
198 /*
199  * Read the power status.
200  */
201 static int i8k_get_power_status(void)
202 {
203     SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
204     int rc;
205
206     regs.eax = I8K_SMM_POWER_STATUS;
207     if ((rc=i8k_smm(&regs)) < 0) {
208         return rc;
209     }
210
211     switch (regs.eax & 0xff) {
212     case I8K_POWER_AC:
213         return I8K_AC;
214     default:
215         return I8K_BATTERY;
216     }
217 }
218
219 /*
220  * Read the fan status.
221  */
222 static int i8k_get_fan_status(int fan)
223 {
224     SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
225     int rc;
226
227     regs.eax = I8K_SMM_GET_FAN;
228     regs.ebx = fan & 0xff;
229     if ((rc=i8k_smm(&regs)) < 0) {
230         return rc;
231     }
232
233     return (regs.eax & 0xff);
234 }
235
236 /*
237  * Read the fan speed in RPM.
238  */
239 static int i8k_get_fan_speed(int fan)
240 {
241     SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
242     int rc;
243
244     regs.eax = I8K_SMM_GET_SPEED;
245     regs.ebx = fan & 0xff;
246     if ((rc=i8k_smm(&regs)) < 0) {
247         return rc;
248     }
249
250     return (regs.eax & 0xffff) * I8K_FAN_MULT;
251 }
252
253 /*
254  * Set the fan speed (off, low, high). Returns the new fan status.
255  */
256 static int i8k_set_fan(int fan, int speed)
257 {
258     SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
259     int rc;
260
261     speed = (speed < 0) ? 0 : ((speed > I8K_FAN_MAX) ? I8K_FAN_MAX : speed);
262
263     regs.eax = I8K_SMM_SET_FAN;
264     regs.ebx = (fan & 0xff) | (speed << 8);
265     if ((rc=i8k_smm(&regs)) < 0) {
266         return rc;
267     }
268
269     return (i8k_get_fan_status(fan));
270 }
271
272 /*
273  * Read the cpu temperature.
274  */
275 static int i8k_get_cpu_temp(void)
276 {
277     SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
278     int rc;
279     int temp;
280
281 #ifdef I8K_TEMPERATURE_BUG
282     static int prev = 0;
283 #endif
284
285     regs.eax = I8K_SMM_GET_TEMP;
286     if ((rc=i8k_smm(&regs)) < 0) {
287         return rc;
288     }
289     temp = regs.eax & 0xff;
290
291 #ifdef I8K_TEMPERATURE_BUG
292     /*
293      * Sometimes the temperature sensor returns 0x99, which is out of range.
294      * In this case we return (once) the previous cached value. For example:
295      # 1003655137 00000058 00005a4b
296      # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
297      # 1003655139 00000054 00005c52
298      */
299     if (temp > I8K_MAX_TEMP) {
300         temp = prev;
301         prev = I8K_MAX_TEMP;
302     } else {
303         prev = temp;
304     }
305 #endif
306
307     return temp;
308 }
309
310 static int i8k_get_dell_signature(void)
311 {
312     SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
313     int rc;
314
315     regs.eax = I8K_SMM_GET_DELL_SIG;
316     if ((rc=i8k_smm(&regs)) < 0) {
317         return rc;
318     }
319
320     if ((regs.eax == 1145651527) && (regs.edx == 1145392204)) {
321         return 0;
322     } else {
323         return -1;
324     }
325 }
326
327 static int i8k_ioctl(struct inode *ip, struct file *fp, unsigned int cmd,
328                      unsigned long arg)
329 {
330     int val;
331     int speed;
332     unsigned char buff[16];
333     int __user *argp = (int __user *)arg;
334
335     if (!argp)
336         return -EINVAL;
337
338     switch (cmd) {
339     case I8K_BIOS_VERSION:
340         val = i8k_get_bios_version();
341         break;
342
343     case I8K_MACHINE_ID:
344         memset(buff, 0, 16);
345         val = i8k_get_serial_number(buff);
346         break;
347
348     case I8K_FN_STATUS:
349         val = i8k_get_fn_status();
350         break;
351
352     case I8K_POWER_STATUS:
353         val = i8k_get_power_status();
354         break;
355
356     case I8K_GET_TEMP:
357         val = i8k_get_cpu_temp();
358         break;
359
360     case I8K_GET_SPEED:
361         if (copy_from_user(&val, argp, sizeof(int))) {
362             return -EFAULT;
363         }
364         val = i8k_get_fan_speed(val);
365         break;
366
367     case I8K_GET_FAN:
368         if (copy_from_user(&val, argp, sizeof(int))) {
369             return -EFAULT;
370         }
371         val = i8k_get_fan_status(val);
372         break;
373
374     case I8K_SET_FAN:
375         if (restricted && !capable(CAP_SYS_ADMIN)) {
376             return -EPERM;
377         }
378         if (copy_from_user(&val, argp, sizeof(int))) {
379             return -EFAULT;
380         }
381         if (copy_from_user(&speed, argp+1, sizeof(int))) {
382             return -EFAULT;
383         }
384         val = i8k_set_fan(val, speed);
385         break;
386
387     default:
388         return -EINVAL;
389     }
390
391     if (val < 0) {
392         return val;
393     }
394
395     switch (cmd) {
396     case I8K_BIOS_VERSION:
397         if (copy_to_user(argp, &val, 4)) {
398             return -EFAULT;
399         }
400         break;
401     case I8K_MACHINE_ID:
402         if (copy_to_user(argp, buff, 16)) {
403             return -EFAULT;
404         }
405         break;
406     default:
407         if (copy_to_user(argp, &val, sizeof(int))) {
408             return -EFAULT;
409         }
410         break;
411     }
412
413     return 0;
414 }
415
416 /*
417  * Print the information for /proc/i8k.
418  */
419 static int i8k_get_info(char *buffer, char **start, off_t fpos, int length)
420 {
421     int n, fn_key, cpu_temp, ac_power;
422     int left_fan, right_fan, left_speed, right_speed;
423
424     cpu_temp     = i8k_get_cpu_temp();                  /* 11100 Âµs */
425     left_fan     = i8k_get_fan_status(I8K_FAN_LEFT);    /*   580 Âµs */
426     right_fan    = i8k_get_fan_status(I8K_FAN_RIGHT);   /*   580 Âµs */
427     left_speed   = i8k_get_fan_speed(I8K_FAN_LEFT);     /*   580 Âµs */
428     right_speed  = i8k_get_fan_speed(I8K_FAN_RIGHT);    /*   580 Âµs */
429     fn_key       = i8k_get_fn_status();                 /*   750 Âµs */
430     if (power_status) {
431         ac_power = i8k_get_power_status();              /* 14700 Âµs */
432     } else {
433         ac_power = -1;
434     }
435
436     /*
437      * Info:
438      *
439      * 1)  Format version (this will change if format changes)
440      * 2)  BIOS version
441      * 3)  BIOS machine ID
442      * 4)  Cpu temperature
443      * 5)  Left fan status
444      * 6)  Right fan status
445      * 7)  Left fan speed
446      * 8)  Right fan speed
447      * 9)  AC power
448      * 10) Fn Key status
449      */
450     n = sprintf(buffer, "%s %s %s %d %d %d %d %d %d %d\n",
451                 I8K_PROC_FMT,
452                 bios_version,
453                 serial_number,
454                 cpu_temp,
455                 left_fan,
456                 right_fan,
457                 left_speed,
458                 right_speed,
459                 ac_power,
460                 fn_key);
461
462     return n;
463 }
464
465 static ssize_t i8k_read(struct file *f, char __user *buffer, size_t len, loff_t *fpos)
466 {
467     int n;
468     char info[128];
469
470     n = i8k_get_info(info, NULL, 0, 128);
471     if (n <= 0) {
472         return n;
473     }
474
475     if (*fpos >= n) {
476         return 0;
477     }
478
479     if ((*fpos + len) >= n) {
480         len = n - *fpos;
481     }
482
483     if (copy_to_user(buffer, info, len) != 0) {
484         return -EFAULT;
485     }
486
487     *fpos += len;
488     return len;
489 }
490
491 static char* __init string_trim(char *s, int size)
492 {
493     int len;
494     char *p;
495
496     if ((len = strlen(s)) > size) {
497         len = size;
498     }
499
500     for (p=s+len-1; len && (*p==' '); len--,p--) {
501         *p = '\0';
502     }
503
504     return s;
505 }
506
507 /* DMI code, stolen from arch/i386/kernel/dmi_scan.c */
508
509 /*
510  * |<-- dmi->length -->|
511  * |                   |
512  * |dmi header    s=N  | string1,\0, ..., stringN,\0, ..., \0
513  *                |                       |
514  *                +-----------------------+
515  */
516 static char* __init dmi_string(DMIHeader *dmi, u8 s)
517 {
518     u8 *p;
519
520     if (!s) {
521         return "";
522     }
523     s--;
524
525     p = (u8 *)dmi + dmi->length;
526     while (s > 0) {
527         p += strlen(p);
528         p++;
529         s--;
530     }
531
532     return p;
533 }
534
535 static void __init dmi_decode(DMIHeader *dmi)
536 {
537     u8 *data = (u8 *) dmi;
538     char *p;
539
540 #ifdef I8K_DEBUG
541     int i;
542     printk("%08x ", (int)data);
543     for (i=0; i<data[1] && i<64; i++) {
544         printk("%02x ", data[i]);
545     }
546     printk("\n");
547 #endif
548
549     switch (dmi->type) {
550     case  0:    /* BIOS Information */
551         p = dmi_string(dmi,data[5]);
552         if (*p) {
553             strlcpy(bios_version, p, sizeof(bios_version));
554             string_trim(bios_version, sizeof(bios_version));
555         }
556         break;  
557     case 1:     /* System Information */
558         p = dmi_string(dmi,data[4]);
559         if (*p) {
560             strlcpy(system_vendor, p, sizeof(system_vendor));
561             string_trim(system_vendor, sizeof(system_vendor));
562         }
563         p = dmi_string(dmi,data[5]);
564         if (*p) {
565             strlcpy(product_name, p, sizeof(product_name));
566             string_trim(product_name, sizeof(product_name));
567         }
568         p = dmi_string(dmi,data[7]);
569         if (*p) {
570             strlcpy(serial_number, p, sizeof(serial_number));
571             string_trim(serial_number, sizeof(serial_number));
572         }
573         break;
574     }
575 }
576
577 static int __init dmi_table(u32 base, int len, int num, void (*fn)(DMIHeader*))
578 {
579     u8 *buf;
580     u8 *data;
581     DMIHeader *dmi;
582     int i = 1;
583
584     buf = ioremap(base, len);
585     if (buf == NULL) {
586         return -1;
587     }
588     data = buf;
589
590     /*
591      * Stop when we see al the items the table claimed to have
592      * or we run off the end of the table (also happens)
593      */
594     while ((i<num) && ((data-buf) < len)) {
595         dmi = (DMIHeader *)data;
596         /*
597          * Avoid misparsing crud if the length of the last
598          * record is crap
599          */
600         if ((data-buf+dmi->length) >= len) {
601             break;
602         }
603         fn(dmi);
604         data += dmi->length;
605         /*
606          * Don't go off the end of the data if there is
607          * stuff looking like string fill past the end
608          */
609         while (((data-buf) < len) && (*data || data[1])) {
610             data++;
611         }
612         data += 2;
613         i++;
614     }
615     iounmap(buf);
616
617     return 0;
618 }
619
620 static int __init dmi_iterate(void (*decode)(DMIHeader *))
621 {
622     unsigned char buf[20];
623     long fp = 0x000e0000L;
624     fp -= 16;
625
626     while (fp < 0x000fffffL) {
627         fp += 16;
628         isa_memcpy_fromio(buf, fp, 20);
629         if (memcmp(buf, "_DMI_", 5)==0) {
630             u16 num  = buf[13]<<8  | buf[12];
631             u16 len  = buf [7]<<8  | buf [6];
632             u32 base = buf[11]<<24 | buf[10]<<16 | buf[9]<<8 | buf[8];
633 #ifdef I8K_DEBUG
634             printk(KERN_INFO "DMI %d.%d present.\n",
635                    buf[14]>>4, buf[14]&0x0F);
636             printk(KERN_INFO "%d structures occupying %d bytes.\n",
637                    buf[13]<<8 | buf[12],
638                    buf [7]<<8 | buf[6]);
639             printk(KERN_INFO "DMI table at 0x%08X.\n",
640                    buf[11]<<24 | buf[10]<<16 | buf[9]<<8 | buf[8]);
641 #endif
642             if (dmi_table(base, len, num, decode)==0) {
643                 return 0;
644             }
645         }
646     }
647     return -1;
648 }
649 /* end of DMI code */
650
651 /*
652  * Get DMI information.
653  */
654 static int __init i8k_dmi_probe(void)
655 {
656     char **p;
657
658     if (dmi_iterate(dmi_decode) != 0) {
659         printk(KERN_INFO "i8k: unable to get DMI information\n");
660         return -ENODEV;
661     }
662
663     if (strncmp(system_vendor,DELL_SIGNATURE,strlen(DELL_SIGNATURE)) != 0) {
664         printk(KERN_INFO "i8k: not running on a Dell system\n");
665         return -ENODEV;
666     }
667
668     for (p=supported_models; ; p++) {
669         if (!*p) {
670             printk(KERN_INFO "i8k: unsupported model: %s\n", product_name);
671             return -ENODEV;
672         }
673         if (strncmp(product_name,*p,strlen(*p)) == 0) {
674             break;
675         }
676     }
677
678     return 0;
679 }
680
681 /*
682  * Probe for the presence of a supported laptop.
683  */
684 static int __init i8k_probe(void)
685 {
686     char buff[4];
687     int version;
688     int smm_found = 0;
689
690     /*
691      * Get DMI information
692      */
693     if (i8k_dmi_probe() != 0) {
694         printk(KERN_INFO "i8k: vendor=%s, model=%s, version=%s\n",
695                system_vendor, product_name, bios_version);
696     }
697
698     /*
699      * Get SMM Dell signature
700      */
701     if (i8k_get_dell_signature() != 0) {
702         printk(KERN_INFO "i8k: unable to get SMM Dell signature\n");
703     } else {
704         smm_found = 1;
705     }
706
707     /*
708      * Get SMM BIOS version.
709      */
710     version = i8k_get_bios_version();
711     if (version <= 0) {
712         printk(KERN_INFO "i8k: unable to get SMM BIOS version\n");
713     } else {
714         smm_found = 1;
715         buff[0] = (version >> 16) & 0xff;
716         buff[1] = (version >>  8) & 0xff;
717         buff[2] = (version)       & 0xff;
718         buff[3] = '\0';
719         /*
720          * If DMI BIOS version is unknown use SMM BIOS version.
721          */
722         if (bios_version[0] == '?') {
723             strcpy(bios_version, buff);
724         }
725         /*
726          * Check if the two versions match.
727          */
728         if (strncmp(buff,bios_version,sizeof(bios_version)) != 0) {
729             printk(KERN_INFO "i8k: BIOS version mismatch: %s != %s\n",
730                    buff, bios_version);
731         }
732     }
733
734     if (!smm_found && !force) {
735         return -ENODEV;
736     }
737
738     return 0;
739 }
740
741 #ifdef MODULE
742 static
743 #endif
744 int __init i8k_init(void)
745 {
746     struct proc_dir_entry *proc_i8k;
747
748     /* Are we running on an supported laptop? */
749     if (i8k_probe() != 0) {
750         return -ENODEV;
751     }
752
753     /* Register the proc entry */
754     proc_i8k = create_proc_info_entry("i8k", 0, NULL, i8k_get_info);
755     if (!proc_i8k) {
756         return -ENOENT;
757     }
758     proc_i8k->proc_fops = &i8k_fops;
759     proc_i8k->owner = THIS_MODULE;
760
761     printk(KERN_INFO
762            "Dell laptop SMM driver v%s Massimo Dal Zotto (dz@debian.org)\n",
763            I8K_VERSION);
764
765     return 0;
766 }
767
768 #ifdef MODULE
769 int init_module(void)
770 {
771     return i8k_init();
772 }
773
774 void cleanup_module(void)
775 {
776     /* Remove the proc entry */
777     remove_proc_entry("i8k", NULL);
778
779     printk(KERN_INFO "i8k: module unloaded\n");
780 }
781 #endif
782
783 /* end of file */