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/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>
29 #include <asm/io.h>
30 
31 #include <linux/i8k.h>
32 
33 #define I8K_VERSION		"1.13 14/05/2002"
34 
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
43 
44 #define I8K_FAN_MULT		30
45 #define I8K_MAX_TEMP		127
46 
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
53 
54 #define I8K_POWER_AC		0x05
55 #define I8K_POWER_BATTERY	0x01
56 
57 #define I8K_TEMPERATURE_BUG	1
58 
59 #define DELL_SIGNATURE		"Dell Computer"
60 
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
65 
66 /*
67  * (To be escaped) Scancodes for the keys.  These were chosen to match other
68  * "Internet" keyboards.
69  */
70 #define I8K_KEYS_UP_SCANCODE	0x30
71 #define I8K_KEYS_DOWN_SCANCODE	0x2e
72 #define I8K_KEYS_MUTE_SCANCODE	0x20
73 
74 static char *supported_models[] = {
75     "Inspiron",
76     "Latitude",
77     NULL
78 };
79 
80 static char system_vendor[48] = "?";
81 static char product_name [48] = "?";
82 static char bios_version [4]  = "?";
83 static char serial_number[16] = "?";
84 
85 int force = 0;
86 int restricted = 0;
87 int handle_buttons = 0;
88 int repeat_delay = I8K_REPEAT_DELAY;
89 int repeat_rate = I8K_REPEAT_RATE;
90 int power_status = 0;
91 
92 static struct timer_list  i8k_keys_timer;
93 
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");
109 
110 static ssize_t i8k_read(struct file *, char *, size_t, loff_t *);
111 static int i8k_ioctl(struct inode *, struct file *, unsigned int,
112 		     unsigned long);
113 static void i8k_keys_set_timer(void);
114 
115 static struct file_operations i8k_fops = {
116     read:	i8k_read,
117     ioctl:	i8k_ioctl,
118 };
119 
120 typedef struct {
121     unsigned int eax;
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));
127 } SMMRegisters;
128 
129 typedef struct {
130     u8	type;
131     u8	length;
132     u16	handle;
133 } DMIHeader;
134 
135 /*
136  * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
137  */
i8k_smm(SMMRegisters * regs)138 static int i8k_smm(SMMRegisters *regs)
139 {
140     int rc;
141     int eax = regs->eax;
142 
143     asm("pushl %%eax\n\t" \
144 	"movl 0(%%eax),%%edx\n\t" \
145 	"push %%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" \
151 	"popl %%eax\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" \
160 	"popl %%edx\n\t" \
161 	"movl %%edx,0(%%eax)\n\t" \
162 	"lahf\n\t" \
163 	"shrl $8,%%eax\n\t" \
164 	"andl $1,%%eax\n" \
165 	: "=a" (rc)
166 	: "a" (regs)
167 	: "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
168 
169     if ((rc != 0) || ((regs->eax & 0xffff) == 0xffff) || (regs->eax == eax)) {
170 	return -EINVAL;
171     }
172 
173     return 0;
174 }
175 
176 /*
177  * Read the bios version. Return the version as an integer corresponding
178  * to the ascii value, for example "A17" is returned as 0x00413137.
179  */
i8k_get_bios_version(void)180 static int i8k_get_bios_version(void)
181 {
182     SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
183     int rc;
184 
185     regs.eax = I8K_SMM_BIOS_VERSION;
186     if ((rc=i8k_smm(&regs)) < 0) {
187 	return rc;
188     }
189 
190     return regs.eax;
191 }
192 
193 /*
194  * Read the machine id.
195  */
i8k_get_serial_number(unsigned char * buff)196 static int i8k_get_serial_number(unsigned char *buff)
197 {
198     strncpy(buff, serial_number, 16);
199     return 0;
200 }
201 
202 /*
203  * Read the Fn key status.
204  */
i8k_get_fn_status(void)205 static int i8k_get_fn_status(void)
206 {
207     SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
208     int rc;
209 
210     regs.eax = I8K_SMM_FN_STATUS;
211     if ((rc=i8k_smm(&regs)) < 0) {
212 	return rc;
213     }
214 
215     switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
216     case I8K_FN_UP:
217 	return I8K_VOL_UP;
218     case I8K_FN_DOWN:
219 	return I8K_VOL_DOWN;
220     case I8K_FN_MUTE:
221 	return I8K_VOL_MUTE;
222     default:
223 	return 0;
224     }
225 }
226 
227 /*
228  * Read the power status.
229  */
i8k_get_power_status(void)230 static int i8k_get_power_status(void)
231 {
232     SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
233     int rc;
234 
235     regs.eax = I8K_SMM_POWER_STATUS;
236     if ((rc=i8k_smm(&regs)) < 0) {
237 	return rc;
238     }
239 
240     switch (regs.eax & 0xff) {
241     case I8K_POWER_AC:
242 	return I8K_AC;
243     default:
244 	return I8K_BATTERY;
245     }
246 }
247 
248 /*
249  * Read the fan status.
250  */
i8k_get_fan_status(int fan)251 static int i8k_get_fan_status(int fan)
252 {
253     SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
254     int rc;
255 
256     regs.eax = I8K_SMM_GET_FAN;
257     regs.ebx = fan & 0xff;
258     if ((rc=i8k_smm(&regs)) < 0) {
259 	return rc;
260     }
261 
262     return (regs.eax & 0xff);
263 }
264 
265 /*
266  * Read the fan speed in RPM.
267  */
i8k_get_fan_speed(int fan)268 static int i8k_get_fan_speed(int fan)
269 {
270     SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
271     int rc;
272 
273     regs.eax = I8K_SMM_GET_SPEED;
274     regs.ebx = fan & 0xff;
275     if ((rc=i8k_smm(&regs)) < 0) {
276 	return rc;
277     }
278 
279     return (regs.eax & 0xffff) * I8K_FAN_MULT;
280 }
281 
282 /*
283  * Set the fan speed (off, low, high). Returns the new fan status.
284  */
i8k_set_fan(int fan,int speed)285 static int i8k_set_fan(int fan, int speed)
286 {
287     SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
288     int rc;
289 
290     speed = (speed < 0) ? 0 : ((speed > I8K_FAN_MAX) ? I8K_FAN_MAX : speed);
291 
292     regs.eax = I8K_SMM_SET_FAN;
293     regs.ebx = (fan & 0xff) | (speed << 8);
294     if ((rc=i8k_smm(&regs)) < 0) {
295 	return rc;
296     }
297 
298     return (i8k_get_fan_status(fan));
299 }
300 
301 /*
302  * Read the cpu temperature.
303  */
i8k_get_cpu_temp(void)304 static int i8k_get_cpu_temp(void)
305 {
306     SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
307     int rc;
308     int temp;
309 
310 #ifdef I8K_TEMPERATURE_BUG
311     static int prev = 0;
312 #endif
313 
314     regs.eax = I8K_SMM_GET_TEMP;
315     if ((rc=i8k_smm(&regs)) < 0) {
316 	return rc;
317     }
318     temp = regs.eax & 0xff;
319 
320 #ifdef I8K_TEMPERATURE_BUG
321     /*
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
327      */
328     if (temp > I8K_MAX_TEMP) {
329 	temp = prev;
330 	prev = I8K_MAX_TEMP;
331     } else {
332 	prev = temp;
333     }
334 #endif
335 
336     return temp;
337 }
338 
i8k_get_dell_signature(void)339 static int i8k_get_dell_signature(void)
340 {
341     SMMRegisters regs = { 0, 0, 0, 0, 0, 0 };
342     int rc;
343 
344     regs.eax = I8K_SMM_GET_DELL_SIG;
345     if ((rc=i8k_smm(&regs)) < 0) {
346 	return rc;
347     }
348 
349     if ((regs.eax == 1145651527) && (regs.edx == 1145392204)) {
350 	return 0;
351     } else {
352 	return -1;
353     }
354 }
355 
i8k_ioctl(struct inode * ip,struct file * fp,unsigned int cmd,unsigned long arg)356 static int i8k_ioctl(struct inode *ip, struct file *fp, unsigned int cmd,
357 		     unsigned long arg)
358 {
359     int val;
360     int speed;
361     unsigned char buff[16];
362 
363     if (!arg) {
364 	return -EINVAL;
365     }
366 
367     switch (cmd) {
368     case I8K_BIOS_VERSION:
369 	val = i8k_get_bios_version();
370 	break;
371 
372     case I8K_MACHINE_ID:
373 	memset(buff, 0, 16);
374 	val = i8k_get_serial_number(buff);
375 	break;
376 
377     case I8K_FN_STATUS:
378 	val = i8k_get_fn_status();
379 	break;
380 
381     case I8K_POWER_STATUS:
382 	val = i8k_get_power_status();
383 	break;
384 
385     case I8K_GET_TEMP:
386 	val = i8k_get_cpu_temp();
387 	break;
388 
389     case I8K_GET_SPEED:
390 	if (copy_from_user(&val, (int *)arg, sizeof(int))) {
391 	    return -EFAULT;
392 	}
393 	val = i8k_get_fan_speed(val);
394 	break;
395 
396     case I8K_GET_FAN:
397 	if (copy_from_user(&val, (int *)arg, sizeof(int))) {
398 	    return -EFAULT;
399 	}
400 	val = i8k_get_fan_status(val);
401 	break;
402 
403     case I8K_SET_FAN:
404 	if (restricted && !capable(CAP_SYS_ADMIN)) {
405 	    return -EPERM;
406 	}
407 	if (copy_from_user(&val, (int *)arg, sizeof(int))) {
408 	    return -EFAULT;
409 	}
410 	if (copy_from_user(&speed, (int *)arg+1, sizeof(int))) {
411 	    return -EFAULT;
412 	}
413 	val = i8k_set_fan(val, speed);
414 	break;
415 
416     default:
417 	return -EINVAL;
418     }
419 
420     if (val < 0) {
421 	return val;
422     }
423 
424     switch (cmd) {
425     case I8K_BIOS_VERSION:
426 	if (copy_to_user((int *)arg, &val, 4)) {
427 	    return -EFAULT;
428 	}
429 	break;
430     case I8K_MACHINE_ID:
431 	if (copy_to_user((int *)arg, buff, 16)) {
432 	    return -EFAULT;
433 	}
434 	break;
435     default:
436 	if (copy_to_user((int *)arg, &val, sizeof(int))) {
437 	    return -EFAULT;
438 	}
439 	break;
440     }
441 
442     return 0;
443 }
444 
445 /*
446  * Print the information for /proc/i8k.
447  */
i8k_get_info(char * buffer,char ** start,off_t fpos,int length)448 static int i8k_get_info(char *buffer, char **start, off_t fpos, int length)
449 {
450     int n, fn_key, cpu_temp, ac_power;
451     int left_fan, right_fan, left_speed, right_speed;
452 
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 */
459     if (power_status) {
460 	ac_power = i8k_get_power_status();		/* 14700 �s */
461     } else {
462 	ac_power = -1;
463     }
464 
465     /*
466      * Info:
467      *
468      * 1)  Format version (this will change if format changes)
469      * 2)  BIOS version
470      * 3)  BIOS machine ID
471      * 4)  Cpu temperature
472      * 5)  Left fan status
473      * 6)  Right fan status
474      * 7)  Left fan speed
475      * 8)  Right fan speed
476      * 9)  AC power
477      * 10) Fn Key status
478      */
479     n = sprintf(buffer, "%s %s %s %d %d %d %d %d %d %d\n",
480 		I8K_PROC_FMT,
481 		bios_version,
482 		serial_number,
483 		cpu_temp,
484 		left_fan,
485 		right_fan,
486 		left_speed,
487 		right_speed,
488 		ac_power,
489 		fn_key);
490 
491     return n;
492 }
493 
i8k_read(struct file * f,char * buffer,size_t len,loff_t * fpos)494 static ssize_t i8k_read(struct file *f, char *buffer, size_t len, loff_t *fpos)
495 {
496     loff_t pos = *fpos;
497     int n;
498     char info[128];
499 
500     n = i8k_get_info(info, NULL, 0, 128);
501     if (n <= 0) {
502 	return n;
503     }
504 
505     if (pos != (unsigned)pos || pos >= n) {
506 	return 0;
507     }
508 
509     if (len >= n - pos) {
510 	len = n - pos;
511     }
512 
513     if (copy_to_user(buffer, info, len) != 0) {
514 	return -EFAULT;
515     }
516 
517     *fpos = pos + len;
518     return len;
519 }
520 
521 /*
522  * i8k_keys stuff. Thanks to David Bustos <bustos@caltech.edu>
523  */
524 
i8k_keys_make_scancode(int x)525 static unsigned char i8k_keys_make_scancode(int x) {
526     switch (x) {
527     case I8K_FN_UP:	return I8K_KEYS_UP_SCANCODE;
528     case I8K_FN_DOWN:	return I8K_KEYS_DOWN_SCANCODE;
529     case I8K_FN_MUTE:	return I8K_KEYS_MUTE_SCANCODE;
530     }
531 
532     return 0;
533 }
534 
i8k_keys_poll(unsigned long data)535 static void i8k_keys_poll(unsigned long data) {
536     static int last = 0;
537     static int repeat = 0;
538 
539     int  curr;
540 
541     curr = i8k_get_fn_status();
542     if (curr >= 0) {
543 	if (curr != last) {
544 	    repeat = jiffies + (HZ * repeat_delay / 1000);
545 
546 	    if (last != 0) {
547 		handle_scancode(0xe0, 0);
548 		handle_scancode(i8k_keys_make_scancode(last), 0);
549 	    }
550 
551 	    if (curr != 0) {
552 		handle_scancode(0xe0, 1);
553 		handle_scancode(i8k_keys_make_scancode(curr), 1);
554 	    }
555 	} else {
556 	    /* Generate keyboard repeat events with current scancode -- dz */
557 	    if ((curr) && (repeat_rate > 0) && (jiffies >= repeat)) {
558 		repeat = jiffies + (HZ / repeat_rate);
559 		handle_scancode(0xe0, 1);
560 		handle_scancode(i8k_keys_make_scancode(curr), 1);
561 	    }
562 	}
563 
564 	last = curr;
565     }
566 
567     /* Reset the timer. */
568     i8k_keys_set_timer();
569 }
570 
i8k_keys_set_timer()571 static void i8k_keys_set_timer() {
572     i8k_keys_timer.expires = jiffies + I8K_POLL_INTERVAL;
573     add_timer(&i8k_keys_timer);
574 }
575 
string_trim(char * s,int size)576 static char* __init string_trim(char *s, int size)
577 {
578     int len;
579     char *p;
580 
581     if ((len = strlen(s)) > size) {
582 	len = size;
583     }
584 
585     for (p=s+len-1; len && (*p==' '); len--,p--) {
586 	*p = '\0';
587     }
588 
589     return s;
590 }
591 
592 /* DMI code, stolen from arch/i386/kernel/dmi_scan.c */
593 
594 /*
595  * |<-- dmi->length -->|
596  * |                   |
597  * |dmi header    s=N  | string1,\0, ..., stringN,\0, ..., \0
598  *                |                       |
599  *                +-----------------------+
600  */
dmi_string(DMIHeader * dmi,u8 s)601 static char* __init dmi_string(DMIHeader *dmi, u8 s)
602 {
603     u8 *p;
604 
605     if (!s) {
606 	return "";
607     }
608     s--;
609 
610     p = (u8 *)dmi + dmi->length;
611     while (s > 0) {
612 	p += strlen(p);
613 	p++;
614 	s--;
615     }
616 
617     return p;
618 }
619 
dmi_decode(DMIHeader * dmi)620 static void __init dmi_decode(DMIHeader *dmi)
621 {
622     u8 *data = (u8 *) dmi;
623     char *p;
624 
625 #ifdef I8K_DEBUG
626     int i;
627     printk("%08x ", (int)data);
628     for (i=0; i<data[1] && i<64; i++) {
629 	printk("%02x ", data[i]);
630     }
631     printk("\n");
632 #endif
633 
634     switch (dmi->type) {
635     case  0:	/* BIOS Information */
636 	p = dmi_string(dmi,data[5]);
637 	if (*p) {
638 	    strncpy(bios_version, p, sizeof(bios_version));
639 	    string_trim(bios_version, sizeof(bios_version));
640 	}
641 	break;
642     case 1:	/* System Information */
643 	p = dmi_string(dmi,data[4]);
644 	if (*p) {
645 	    strncpy(system_vendor, p, sizeof(system_vendor));
646 	    string_trim(system_vendor, sizeof(system_vendor));
647 	}
648 	p = dmi_string(dmi,data[5]);
649 	if (*p) {
650 	    strncpy(product_name, p, sizeof(product_name));
651 	    string_trim(product_name, sizeof(product_name));
652 	}
653 	p = dmi_string(dmi,data[7]);
654 	if (*p) {
655 	    strncpy(serial_number, p, sizeof(serial_number));
656 	    string_trim(serial_number, sizeof(serial_number));
657 	}
658 	break;
659     }
660 }
661 
dmi_table(u32 base,int len,int num,void (* fn)(DMIHeader *))662 static int __init dmi_table(u32 base, int len, int num, void (*fn)(DMIHeader*))
663 {
664     u8 *buf;
665     u8 *data;
666     DMIHeader *dmi;
667     int i = 1;
668 
669     buf = ioremap(base, len);
670     if (buf == NULL) {
671 	return -1;
672     }
673     data = buf;
674 
675     /*
676      * Stop when we see al the items the table claimed to have
677      * or we run off the end of the table (also happens)
678      */
679     while ((i<num) && ((data-buf) < len)) {
680 	dmi = (DMIHeader *)data;
681 	/*
682 	 * Avoid misparsing crud if the length of the last
683 	 * record is crap
684 	 */
685 	if ((data-buf+dmi->length) >= len) {
686 	    break;
687 	}
688 	fn(dmi);
689 	data += dmi->length;
690 	/*
691 	 * Don't go off the end of the data if there is
692 	 * stuff looking like string fill past the end
693 	 */
694 	while (((data-buf) < len) && (*data || data[1])) {
695 	    data++;
696 	}
697 	data += 2;
698 	i++;
699     }
700     iounmap(buf);
701 
702     return 0;
703 }
704 
dmi_iterate(void (* decode)(DMIHeader *))705 static int __init dmi_iterate(void (*decode)(DMIHeader *))
706 {
707     unsigned char buf[20];
708     long fp = 0x000e0000L;
709     fp -= 16;
710 
711     while (fp < 0x000fffffL) {
712 	fp += 16;
713 	isa_memcpy_fromio(buf, fp, 20);
714 	if (memcmp(buf, "_DMI_", 5)==0) {
715 	    u16 num  = buf[13]<<8  | buf[12];
716 	    u16 len  = buf [7]<<8  | buf [6];
717 	    u32 base = buf[11]<<24 | buf[10]<<16 | buf[9]<<8 | buf[8];
718 #ifdef I8K_DEBUG
719 	    printk(KERN_INFO "DMI %d.%d present.\n",
720 		   buf[14]>>4, buf[14]&0x0F);
721 	    printk(KERN_INFO "%d structures occupying %d bytes.\n",
722 		   buf[13]<<8 | buf[12],
723 		   buf [7]<<8 | buf[6]);
724 	    printk(KERN_INFO "DMI table at 0x%08X.\n",
725 		   buf[11]<<24 | buf[10]<<16 | buf[9]<<8 | buf[8]);
726 #endif
727 	    if (dmi_table(base, len, num, decode)==0) {
728 		return 0;
729 	    }
730 	}
731     }
732     return -1;
733 }
734 /* end of DMI code */
735 
736 /*
737  * Get DMI information.
738  */
i8k_dmi_probe(void)739 static int __init i8k_dmi_probe(void)
740 {
741     char **p;
742 
743     if (dmi_iterate(dmi_decode) != 0) {
744 	printk(KERN_INFO "i8k: unable to get DMI information\n");
745 	return -ENODEV;
746     }
747 
748     if (strncmp(system_vendor,DELL_SIGNATURE,strlen(DELL_SIGNATURE)) != 0) {
749 	printk(KERN_INFO "i8k: not running on a Dell system\n");
750 	return -ENODEV;
751     }
752 
753     for (p=supported_models; ; p++) {
754 	if (!*p) {
755 	    printk(KERN_INFO "i8k: unsupported model: %s\n", product_name);
756 	    return -ENODEV;
757 	}
758 	if (strncmp(product_name,*p,strlen(*p)) == 0) {
759 	    break;
760 	}
761     }
762 
763     return 0;
764 }
765 
766 /*
767  * Probe for the presence of a supported laptop.
768  */
i8k_probe(void)769 static int __init i8k_probe(void)
770 {
771     char buff[4];
772     int version;
773     int smm_found = 0;
774 
775     /*
776      * Get DMI information
777      */
778     if (i8k_dmi_probe() != 0) {
779 	printk(KERN_INFO "i8k: vendor=%s, model=%s, version=%s\n",
780 	       system_vendor, product_name, bios_version);
781     }
782 
783     /*
784      * Get SMM Dell signature
785      */
786     if (i8k_get_dell_signature() != 0) {
787 	printk(KERN_INFO "i8k: unable to get SMM Dell signature\n");
788     } else {
789 	smm_found = 1;
790     }
791 
792     /*
793      * Get SMM BIOS version.
794      */
795     version = i8k_get_bios_version();
796     if (version <= 0) {
797 	printk(KERN_INFO "i8k: unable to get SMM BIOS version\n");
798     } else {
799 	smm_found = 1;
800 	buff[0] = (version >> 16) & 0xff;
801 	buff[1] = (version >>  8) & 0xff;
802 	buff[2] = (version)       & 0xff;
803 	buff[3] = '\0';
804 	/*
805 	 * If DMI BIOS version is unknown use SMM BIOS version.
806 	 */
807 	if (bios_version[0] == '?') {
808 	    strcpy(bios_version, buff);
809 	}
810 	/*
811 	 * Check if the two versions match.
812 	 */
813 	if (strncmp(buff,bios_version,sizeof(bios_version)) != 0) {
814 	    printk(KERN_INFO "i8k: BIOS version mismatch: %s != %s\n",
815 		   buff, bios_version);
816 	}
817     }
818 
819     if (!smm_found && !force) {
820 	return -ENODEV;
821     }
822 
823     return 0;
824 }
825 
826 #ifdef MODULE
827 static
828 #endif
i8k_init(void)829 int __init i8k_init(void)
830 {
831     struct proc_dir_entry *proc_i8k;
832 
833     /* Are we running on an supported laptop? */
834     if (i8k_probe() != 0) {
835 	return -ENODEV;
836     }
837 
838     /* Register the proc entry */
839     proc_i8k = create_proc_info_entry("i8k", 0, NULL, i8k_get_info);
840     if (!proc_i8k) {
841 	return -ENOENT;
842     }
843     proc_i8k->proc_fops = &i8k_fops;
844     SET_MODULE_OWNER(proc_i8k);
845 
846     printk(KERN_INFO
847 	   "Dell laptop SMM driver v%s Massimo Dal Zotto (dz@debian.org)\n",
848 	   I8K_VERSION);
849 
850     /* Register the i8k_keys timer. */
851     if (handle_buttons) {
852 	printk(KERN_INFO
853 	       "i8k: enabling buttons events, delay=%d, rate=%d\n",
854 	       repeat_delay, repeat_rate);
855 	init_timer(&i8k_keys_timer);
856 	i8k_keys_timer.function = i8k_keys_poll;
857 	i8k_keys_set_timer();
858     }
859 
860     return 0;
861 }
862 
863 #ifdef MODULE
init_module(void)864 int init_module(void)
865 {
866     return i8k_init();
867 }
868 
cleanup_module(void)869 void cleanup_module(void)
870 {
871     /* Remove the proc entry */
872     remove_proc_entry("i8k", NULL);
873 
874     /* Unregister the i8k_keys timer. */
875     while (handle_buttons && !del_timer(&i8k_keys_timer)) {
876 	schedule_timeout(I8K_POLL_INTERVAL);
877     }
878 
879     printk(KERN_INFO "i8k: module unloaded\n");
880 }
881 #endif
882 
883 /* end of file */
884