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