1 /* toshiba.c -- Linux driver for accessing the SMM on Toshiba laptops
5 * Valuable assistance and patches from:
9 * Fn status port numbers for machine ID's courtesy of
25 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
27 * This code is covered by the GNU GPL and you are free to make any
28 * changes you wish to it under the terms of the license. However the
29 * code has the potential to render your computer and/or someone else's
30 * unusable. Please proceed with care when modifying the code.
32 * Note: Unfortunately the laptop hardware can close the System Configuration
33 * Interface on it's own accord. It is therefore necessary for *all*
34 * programs using this driver to be aware that *any* SCI call can fail at
35 * *any* time. It is up to any program to be aware of this eventuality
36 * and take appropriate steps.
38 * This program is free software; you can redistribute it and/or modify it
39 * under the terms of the GNU General Public License as published by the
40 * Free Software Foundation; either version 2, or (at your option) any
43 * This program is distributed in the hope that it will be useful, but
44 * WITHOUT ANY WARRANTY; without even the implied warranty of
45 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
46 * General Public License for more details.
48 * The information used to write this driver has been obtained by reverse
49 * engineering the software supplied by Toshiba for their portable computers in
50 * strict accordance with the European Council Directive 92/250/EEC on the legal
51 * protection of computer programs, and it's implementation into English Law by
52 * the Copyright (Computer Programs) Regulations 1992 (S.I. 1992 No.3233).
56 #define TOSH_VERSION "1.11 26/9/2001"
59 #include <linux/module.h>
60 #include <linux/kernel.h>
61 #include <linux/types.h>
62 #include <linux/fcntl.h>
63 #include <linux/miscdevice.h>
64 #include <linux/ioport.h>
66 #include <linux/uaccess.h>
67 #include <linux/init.h>
68 #include <linux/stat.h>
69 #include <linux/proc_fs.h>
70 #include <linux/seq_file.h>
71 #include <linux/mutex.h>
72 #include <linux/toshiba.h>
74 #define TOSH_MINOR_DEV 181
76 MODULE_LICENSE("GPL");
78 MODULE_DESCRIPTION("Toshiba laptop SMM driver");
79 MODULE_SUPPORTED_DEVICE("toshiba");
81 static DEFINE_MUTEX(tosh_mutex);
83 module_param_named(fn, tosh_fn, int, 0);
84 MODULE_PARM_DESC(fn, "User specified Fn key detection port");
92 static long tosh_ioctl(struct file *, unsigned int,
96 static const struct file_operations tosh_fops = {
98 .unlocked_ioctl = tosh_ioctl,
99 .llseek = noop_llseek,
102 static struct miscdevice tosh_device = {
109 * Read the Fn key status
111 #ifdef CONFIG_PROC_FS
112 static int tosh_fn_status(void)
120 local_irq_save(flags);
123 local_irq_restore(flags);
132 * For the Portage 610CT and the Tecra 700CS/700CDT emulate the HCI fan function
134 static int tosh_emulate_fan(SMMRegisters *regs)
136 unsigned long eax,ecx,flags;
139 eax = regs->eax & 0xff00;
140 ecx = regs->ecx & 0xffff;
144 if (tosh_id==0xfccb) {
147 local_irq_save(flags);
150 local_irq_restore(flags);
152 regs->ecx = (unsigned int) (al & 0x01);
154 if ((eax==0xff00) && (ecx==0x0000)) {
156 local_irq_save(flags);
160 outb (al | 0x01, 0xe5);
161 local_irq_restore(flags);
165 if ((eax==0xff00) && (ecx==0x0001)) {
167 local_irq_save(flags);
171 outb(al & 0xfe, 0xe5);
172 local_irq_restore(flags);
178 /* Tecra 700CS/CDT */
180 if (tosh_id==0xfccc) {
183 local_irq_save(flags);
186 local_irq_restore(flags);
188 regs->ecx = al & 0x01;
190 if ((eax==0xff00) && (ecx==0x0000)) {
192 local_irq_save(flags);
195 outw(0xe0 | ((al & 0xfe) << 8), 0xe4);
196 local_irq_restore(flags);
200 if ((eax==0xff00) && (ecx==0x0001)) {
202 local_irq_save(flags);
205 outw(0xe0 | ((al | 0x01) << 8), 0xe4);
206 local_irq_restore(flags);
217 * Put the laptop into System Management Mode
219 int tosh_smm(SMMRegisters *regs)
223 asm ("# load the values into the registers\n\t" \
225 "movl 0(%%eax),%%edx\n\t" \
227 "movl 4(%%eax),%%ebx\n\t" \
228 "movl 8(%%eax),%%ecx\n\t" \
229 "movl 12(%%eax),%%edx\n\t" \
230 "movl 16(%%eax),%%esi\n\t" \
231 "movl 20(%%eax),%%edi\n\t" \
233 "# call the System Management mode\n\t" \
235 "# fill out the memory with the values in the registers\n\t" \
236 "xchgl %%eax,(%%esp)\n\t"
237 "movl %%ebx,4(%%eax)\n\t" \
238 "movl %%ecx,8(%%eax)\n\t" \
239 "movl %%edx,12(%%eax)\n\t" \
240 "movl %%esi,16(%%eax)\n\t" \
241 "movl %%edi,20(%%eax)\n\t" \
243 "movl %%edx,0(%%eax)\n\t" \
244 "# setup the return value to the carry flag\n\t" \
246 "shrl $8,%%eax\n\t" \
250 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
254 EXPORT_SYMBOL(tosh_smm);
257 static long tosh_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
260 SMMRegisters __user *argp = (SMMRegisters __user *)arg;
261 unsigned short ax,bx;
267 if (copy_from_user(®s, argp, sizeof(SMMRegisters)))
272 ax = regs.eax & 0xff00;
273 bx = regs.ebx & 0xffff;
274 /* block HCI calls to read/write memory & PCI devices */
275 if (((ax==0xff00) || (ax==0xfe00)) && (bx>0x0069))
278 /* do we need to emulate the fan ? */
279 mutex_lock(&tosh_mutex);
281 if (((ax==0xf300) || (ax==0xf400)) && (bx==0x0004)) {
282 err = tosh_emulate_fan(®s);
283 mutex_unlock(&tosh_mutex);
287 err = tosh_smm(®s);
288 mutex_unlock(&tosh_mutex);
294 if (copy_to_user(argp, ®s, sizeof(SMMRegisters)))
297 return (err==0) ? 0:-EINVAL;
302 * Print the information for /proc/toshiba
304 #ifdef CONFIG_PROC_FS
305 static int proc_toshiba_show(struct seq_file *m, void *v)
309 key = tosh_fn_status();
312 0) Linux driver version (this will change if format changes)
315 3) BIOS version (major, minor)
316 4) BIOS date (in SCI date format)
319 seq_printf(m, "1.1 0x%04x %d.%d %d.%d 0x%04x 0x%02x\n",
321 (tosh_sci & 0xff00)>>8,
323 (tosh_bios & 0xff00)>>8,
333 * Determine which port to use for the Fn key status
335 static void tosh_set_fn_port(void)
338 case 0xfc02: case 0xfc04: case 0xfc09: case 0xfc0a: case 0xfc10:
339 case 0xfc11: case 0xfc13: case 0xfc15: case 0xfc1a: case 0xfc1b:
343 case 0xfc08: case 0xfc17: case 0xfc1d: case 0xfcd1: case 0xfce0:
357 * Get the machine identification number of the current model
359 static int tosh_get_machine_id(void __iomem *bios)
363 unsigned short bx,cx;
364 unsigned long address;
366 id = (0x100*(int) readb(bios+0xfffe))+((int) readb(bios+0xfffa));
368 /* do we have a SCTTable machine identication number on our hands */
372 /* start by getting a pointer into the BIOS */
378 bx = (unsigned short) (regs.ebx & 0xffff);
380 /* At this point in the Toshiba routines under MS Windows
381 the bx register holds 0xe6f5. However my code is producing
382 a different value! For the time being I will just fudge the
383 value. This has been verified on a Satellite Pro 430CDT,
384 Tecra 750CDT, Tecra 780DVD and Satellite 310CDT. */
386 printk("toshiba: debugging ID ebx=0x%04x\n", regs.ebx);
390 /* now twiddle with our pointer a bit */
393 cx = readw(bios + address);
395 cx = readw(bios + address);
397 cx = readw(bios + address);
399 /* now construct our machine identification number */
401 id = ((cx & 0xff)<<8)+((cx & 0xff00)>>8);
409 * Probe for the presence of a Toshiba laptop
411 * returns and non-zero if unable to detect the presence of a Toshiba
412 * laptop, otherwise zero and determines the Machine ID, BIOS version and
413 * date, and SCI version.
415 static int tosh_probe(void)
417 int i,major,minor,day,year,month,flag;
418 unsigned char signature[7] = { 0x54,0x4f,0x53,0x48,0x49,0x42,0x41 };
420 void __iomem *bios = ioremap(0xf0000, 0x10000);
425 /* extra sanity check for the string "TOSHIBA" in the BIOS because
426 some machines that are not Toshiba's pass the next test */
429 if (readb(bios+0xe010+i)!=signature[i]) {
430 printk("toshiba: not a supported Toshiba laptop\n");
436 /* call the Toshiba SCI support check routine */
441 flag = tosh_smm(®s);
443 /* if this is not a Toshiba laptop carry flag is set and ah=0x86 */
445 if ((flag==1) || ((regs.eax & 0xff00)==0x8600)) {
446 printk("toshiba: not a supported Toshiba laptop\n");
451 /* if we get this far then we are running on a Toshiba (probably)! */
453 tosh_sci = regs.edx & 0xffff;
455 /* next get the machine ID of the current laptop */
457 tosh_id = tosh_get_machine_id(bios);
459 /* get the BIOS version */
461 major = readb(bios+0xe009)-'0';
462 minor = ((readb(bios+0xe00b)-'0')*10)+(readb(bios+0xe00c)-'0');
463 tosh_bios = (major*0x100)+minor;
465 /* get the BIOS date */
467 day = ((readb(bios+0xfff5)-'0')*10)+(readb(bios+0xfff6)-'0');
468 month = ((readb(bios+0xfff8)-'0')*10)+(readb(bios+0xfff9)-'0');
469 year = ((readb(bios+0xfffb)-'0')*10)+(readb(bios+0xfffc)-'0');
470 tosh_date = (((year-90) & 0x1f)<<10) | ((month & 0xf)<<6)
474 /* in theory we should check the ports we are going to use for the
475 fn key detection (and the fan on the Portage 610/Tecra700), and
476 then request them to stop other drivers using them. However as
477 the keyboard driver grabs 0x60-0x6f and the pic driver grabs
478 0xa0-0xbf we can't. We just have to live dangerously and use the
479 ports anyway, oh boy! */
481 /* do we need to emulate the fan? */
483 if ((tosh_id==0xfccb) || (tosh_id==0xfccc))
491 static int __init toshiba_init(void)
494 /* are we running on a Toshiba laptop */
499 printk(KERN_INFO "Toshiba System Management Mode driver v" TOSH_VERSION "\n");
501 /* set the port to use for Fn status if not specified as a parameter */
505 /* register the device file */
506 retval = misc_register(&tosh_device);
510 #ifdef CONFIG_PROC_FS
512 struct proc_dir_entry *pde;
514 pde = proc_create_single("toshiba", 0, NULL, proc_toshiba_show);
516 misc_deregister(&tosh_device);
525 static void __exit toshiba_exit(void)
527 remove_proc_entry("toshiba", NULL);
528 misc_deregister(&tosh_device);
531 module_init(toshiba_init);
532 module_exit(toshiba_exit);