1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* toshiba.c -- Linux driver for accessing the SMM on Toshiba laptops
4 * Copyright (c) 1996-2001 Jonathan A. Buzzard (jonathan@buzzard.org.uk)
6 * Valuable assistance and patches from:
7 * Tom May <tom@you-bastards.com>
8 * Rob Napier <rnapier@employees.org>
10 * Fn status port numbers for machine ID's courtesy of
11 * 0xfc02: Scott Eisert <scott.e@sky-eye.com>
12 * 0xfc04: Steve VanDevender <stevev@efn.org>
13 * 0xfc08: Garth Berry <garth@itsbruce.net>
14 * 0xfc0a: Egbert Eich <eich@xfree86.org>
15 * 0xfc10: Andrew Lofthouse <Andrew.Lofthouse@robins.af.mil>
16 * 0xfc11: Spencer Olson <solson@novell.com>
17 * 0xfc13: Claudius Frankewitz <kryp@gmx.de>
18 * 0xfc15: Tom May <tom@you-bastards.com>
19 * 0xfc17: Dave Konrad <konrad@xenia.it>
20 * 0xfc1a: George Betzos <betzos@engr.colostate.edu>
21 * 0xfc1b: Munemasa Wada <munemasa@jnovel.co.jp>
22 * 0xfc1d: Arthur Liu <armie@slap.mine.nu>
23 * 0xfc5a: Jacques L'helgoualc'h <lhh@free.fr>
24 * 0xfcd1: Mr. Dave Konrad <konrad@xenia.it>
26 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
28 * This code is covered by the GNU GPL and you are free to make any
29 * changes you wish to it under the terms of the license. However the
30 * code has the potential to render your computer and/or someone else's
31 * unusable. Please proceed with care when modifying the code.
33 * Note: Unfortunately the laptop hardware can close the System Configuration
34 * Interface on it's own accord. It is therefore necessary for *all*
35 * programs using this driver to be aware that *any* SCI call can fail at
36 * *any* time. It is up to any program to be aware of this eventuality
37 * and take appropriate steps.
39 * The information used to write this driver has been obtained by reverse
40 * engineering the software supplied by Toshiba for their portable computers in
41 * strict accordance with the European Council Directive 92/250/EEC on the legal
42 * protection of computer programs, and it's implementation into English Law by
43 * the Copyright (Computer Programs) Regulations 1992 (S.I. 1992 No.3233).
46 #define TOSH_VERSION "1.11 26/9/2001"
49 #include <linux/module.h>
50 #include <linux/kernel.h>
51 #include <linux/types.h>
52 #include <linux/fcntl.h>
53 #include <linux/miscdevice.h>
54 #include <linux/ioport.h>
56 #include <linux/uaccess.h>
57 #include <linux/init.h>
58 #include <linux/stat.h>
59 #include <linux/proc_fs.h>
60 #include <linux/seq_file.h>
61 #include <linux/mutex.h>
62 #include <linux/toshiba.h>
64 MODULE_LICENSE("GPL");
65 MODULE_AUTHOR("Jonathan Buzzard <jonathan@buzzard.org.uk>");
66 MODULE_DESCRIPTION("Toshiba laptop SMM driver");
68 static DEFINE_MUTEX(tosh_mutex
);
70 module_param_named(fn
, tosh_fn
, int, 0);
71 MODULE_PARM_DESC(fn
, "User specified Fn key detection port");
79 static long tosh_ioctl(struct file
*, unsigned int,
83 static const struct file_operations tosh_fops
= {
85 .unlocked_ioctl
= tosh_ioctl
,
86 .llseek
= noop_llseek
,
89 static struct miscdevice tosh_device
= {
96 * Read the Fn key status
99 static int tosh_fn_status(void)
107 local_irq_save(flags
);
110 local_irq_restore(flags
);
119 * For the Portage 610CT and the Tecra 700CS/700CDT emulate the HCI fan function
121 static int tosh_emulate_fan(SMMRegisters
*regs
)
123 unsigned long eax
,ecx
,flags
;
126 eax
= regs
->eax
& 0xff00;
127 ecx
= regs
->ecx
& 0xffff;
131 if (tosh_id
==0xfccb) {
134 local_irq_save(flags
);
137 local_irq_restore(flags
);
139 regs
->ecx
= (unsigned int) (al
& 0x01);
141 if ((eax
==0xff00) && (ecx
==0x0000)) {
143 local_irq_save(flags
);
147 outb (al
| 0x01, 0xe5);
148 local_irq_restore(flags
);
152 if ((eax
==0xff00) && (ecx
==0x0001)) {
154 local_irq_save(flags
);
158 outb(al
& 0xfe, 0xe5);
159 local_irq_restore(flags
);
165 /* Tecra 700CS/CDT */
167 if (tosh_id
==0xfccc) {
170 local_irq_save(flags
);
173 local_irq_restore(flags
);
175 regs
->ecx
= al
& 0x01;
177 if ((eax
==0xff00) && (ecx
==0x0000)) {
179 local_irq_save(flags
);
182 outw(0xe0 | ((al
& 0xfe) << 8), 0xe4);
183 local_irq_restore(flags
);
187 if ((eax
==0xff00) && (ecx
==0x0001)) {
189 local_irq_save(flags
);
192 outw(0xe0 | ((al
| 0x01) << 8), 0xe4);
193 local_irq_restore(flags
);
204 * Put the laptop into System Management Mode
206 int tosh_smm(SMMRegisters
*regs
)
210 asm ("# load the values into the registers\n\t" \
212 "movl 0(%%eax),%%edx\n\t" \
214 "movl 4(%%eax),%%ebx\n\t" \
215 "movl 8(%%eax),%%ecx\n\t" \
216 "movl 12(%%eax),%%edx\n\t" \
217 "movl 16(%%eax),%%esi\n\t" \
218 "movl 20(%%eax),%%edi\n\t" \
220 "# call the System Management mode\n\t" \
222 "# fill out the memory with the values in the registers\n\t" \
223 "xchgl %%eax,(%%esp)\n\t"
224 "movl %%ebx,4(%%eax)\n\t" \
225 "movl %%ecx,8(%%eax)\n\t" \
226 "movl %%edx,12(%%eax)\n\t" \
227 "movl %%esi,16(%%eax)\n\t" \
228 "movl %%edi,20(%%eax)\n\t" \
230 "movl %%edx,0(%%eax)\n\t" \
231 "# setup the return value to the carry flag\n\t" \
233 "shrl $8,%%eax\n\t" \
237 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
241 EXPORT_SYMBOL(tosh_smm
);
244 static long tosh_ioctl(struct file
*fp
, unsigned int cmd
, unsigned long arg
)
247 SMMRegisters __user
*argp
= (SMMRegisters __user
*)arg
;
248 unsigned short ax
,bx
;
254 if (copy_from_user(®s
, argp
, sizeof(SMMRegisters
)))
259 ax
= regs
.eax
& 0xff00;
260 bx
= regs
.ebx
& 0xffff;
261 /* block HCI calls to read/write memory & PCI devices */
262 if (((ax
==0xff00) || (ax
==0xfe00)) && (bx
>0x0069))
265 /* do we need to emulate the fan ? */
266 mutex_lock(&tosh_mutex
);
268 if (((ax
==0xf300) || (ax
==0xf400)) && (bx
==0x0004)) {
269 err
= tosh_emulate_fan(®s
);
270 mutex_unlock(&tosh_mutex
);
274 err
= tosh_smm(®s
);
275 mutex_unlock(&tosh_mutex
);
281 if (copy_to_user(argp
, ®s
, sizeof(SMMRegisters
)))
284 return (err
==0) ? 0:-EINVAL
;
289 * Print the information for /proc/toshiba
291 #ifdef CONFIG_PROC_FS
292 static int proc_toshiba_show(struct seq_file
*m
, void *v
)
296 key
= tosh_fn_status();
299 0) Linux driver version (this will change if format changes)
302 3) BIOS version (major, minor)
303 4) BIOS date (in SCI date format)
306 seq_printf(m
, "1.1 0x%04x %d.%d %d.%d 0x%04x 0x%02x\n",
308 (tosh_sci
& 0xff00)>>8,
310 (tosh_bios
& 0xff00)>>8,
320 * Determine which port to use for the Fn key status
322 static void tosh_set_fn_port(void)
325 case 0xfc02: case 0xfc04: case 0xfc09: case 0xfc0a: case 0xfc10:
326 case 0xfc11: case 0xfc13: case 0xfc15: case 0xfc1a: case 0xfc1b:
330 case 0xfc08: case 0xfc17: case 0xfc1d: case 0xfcd1: case 0xfce0:
344 * Get the machine identification number of the current model
346 static int tosh_get_machine_id(void __iomem
*bios
)
350 unsigned short bx
,cx
;
351 unsigned long address
;
353 id
= (0x100*(int) readb(bios
+0xfffe))+((int) readb(bios
+0xfffa));
355 /* do we have a SCTTable machine identication number on our hands */
359 /* start by getting a pointer into the BIOS */
365 bx
= (unsigned short) (regs
.ebx
& 0xffff);
367 /* At this point in the Toshiba routines under MS Windows
368 the bx register holds 0xe6f5. However my code is producing
369 a different value! For the time being I will just fudge the
370 value. This has been verified on a Satellite Pro 430CDT,
371 Tecra 750CDT, Tecra 780DVD and Satellite 310CDT. */
373 pr_debug("toshiba: debugging ID ebx=0x%04x\n", regs
.ebx
);
377 /* now twiddle with our pointer a bit */
380 cx
= readw(bios
+ address
);
382 cx
= readw(bios
+ address
);
384 cx
= readw(bios
+ address
);
386 /* now construct our machine identification number */
388 id
= ((cx
& 0xff)<<8)+((cx
& 0xff00)>>8);
396 * Probe for the presence of a Toshiba laptop
398 * returns and non-zero if unable to detect the presence of a Toshiba
399 * laptop, otherwise zero and determines the Machine ID, BIOS version and
400 * date, and SCI version.
402 static int tosh_probe(void)
404 int i
,major
,minor
,day
,year
,month
,flag
;
405 unsigned char signature
[7] = { 0x54,0x4f,0x53,0x48,0x49,0x42,0x41 };
407 void __iomem
*bios
= ioremap(0xf0000, 0x10000);
412 /* extra sanity check for the string "TOSHIBA" in the BIOS because
413 some machines that are not Toshiba's pass the next test */
416 if (readb(bios
+0xe010+i
)!=signature
[i
]) {
417 pr_err("toshiba: not a supported Toshiba laptop\n");
423 /* call the Toshiba SCI support check routine */
428 flag
= tosh_smm(®s
);
430 /* if this is not a Toshiba laptop carry flag is set and ah=0x86 */
432 if ((flag
==1) || ((regs
.eax
& 0xff00)==0x8600)) {
433 pr_err("toshiba: not a supported Toshiba laptop\n");
438 /* if we get this far then we are running on a Toshiba (probably)! */
440 tosh_sci
= regs
.edx
& 0xffff;
442 /* next get the machine ID of the current laptop */
444 tosh_id
= tosh_get_machine_id(bios
);
446 /* get the BIOS version */
448 major
= readb(bios
+0xe009)-'0';
449 minor
= ((readb(bios
+0xe00b)-'0')*10)+(readb(bios
+0xe00c)-'0');
450 tosh_bios
= (major
*0x100)+minor
;
452 /* get the BIOS date */
454 day
= ((readb(bios
+0xfff5)-'0')*10)+(readb(bios
+0xfff6)-'0');
455 month
= ((readb(bios
+0xfff8)-'0')*10)+(readb(bios
+0xfff9)-'0');
456 year
= ((readb(bios
+0xfffb)-'0')*10)+(readb(bios
+0xfffc)-'0');
457 tosh_date
= (((year
-90) & 0x1f)<<10) | ((month
& 0xf)<<6)
461 /* in theory we should check the ports we are going to use for the
462 fn key detection (and the fan on the Portage 610/Tecra700), and
463 then request them to stop other drivers using them. However as
464 the keyboard driver grabs 0x60-0x6f and the pic driver grabs
465 0xa0-0xbf we can't. We just have to live dangerously and use the
466 ports anyway, oh boy! */
468 /* do we need to emulate the fan? */
470 if ((tosh_id
==0xfccb) || (tosh_id
==0xfccc))
478 static int __init
toshiba_init(void)
481 /* are we running on a Toshiba laptop */
486 pr_info("Toshiba System Management Mode driver v" TOSH_VERSION
"\n");
488 /* set the port to use for Fn status if not specified as a parameter */
492 /* register the device file */
493 retval
= misc_register(&tosh_device
);
497 #ifdef CONFIG_PROC_FS
499 struct proc_dir_entry
*pde
;
501 pde
= proc_create_single("toshiba", 0, NULL
, proc_toshiba_show
);
503 misc_deregister(&tosh_device
);
512 static void __exit
toshiba_exit(void)
514 remove_proc_entry("toshiba", NULL
);
515 misc_deregister(&tosh_device
);
518 module_init(toshiba_init
);
519 module_exit(toshiba_exit
);