blob: e2fb234dee40c36708b3cd046561469610ab0ad9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* toshiba.c -- Linux driver for accessing the SMM on Toshiba laptops
2 *
3 * Copyright (c) 1996-2001 Jonathan A. Buzzard (jonathan@buzzard.org.uk)
4 *
5 * Valuable assistance and patches from:
6 * Tom May <tom@you-bastards.com>
7 * Rob Napier <rnapier@employees.org>
8 *
9 * Fn status port numbers for machine ID's courtesy of
10 * 0xfc02: Scott Eisert <scott.e@sky-eye.com>
11 * 0xfc04: Steve VanDevender <stevev@efn.org>
12 * 0xfc08: Garth Berry <garth@itsbruce.net>
13 * 0xfc0a: Egbert Eich <eich@xfree86.org>
14 * 0xfc10: Andrew Lofthouse <Andrew.Lofthouse@robins.af.mil>
15 * 0xfc11: Spencer Olson <solson@novell.com>
16 * 0xfc13: Claudius Frankewitz <kryp@gmx.de>
17 * 0xfc15: Tom May <tom@you-bastards.com>
18 * 0xfc17: Dave Konrad <konrad@xenia.it>
19 * 0xfc1a: George Betzos <betzos@engr.colostate.edu>
20 * 0xfc1b: Munemasa Wada <munemasa@jnovel.co.jp>
21 * 0xfc1d: Arthur Liu <armie@slap.mine.nu>
22 * 0xfc5a: Jacques L'helgoualc'h <lhh@free.fr>
23 * 0xfcd1: Mr. Dave Konrad <konrad@xenia.it>
24 *
25 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
26 *
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.
31 *
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.
37 *
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
41 * later version.
42 *
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.
47 *
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).
53 *
54 */
55
56#define TOSH_VERSION "1.11 26/9/2001"
57#define TOSH_DEBUG 0
58
59#include <linux/module.h>
60#include <linux/kernel.h>
61#include <linux/sched.h>
62#include <linux/types.h>
63#include <linux/fcntl.h>
64#include <linux/miscdevice.h>
65#include <linux/ioport.h>
66#include <asm/io.h>
67#include <asm/uaccess.h>
68#include <linux/init.h>
69#include <linux/stat.h>
70#include <linux/proc_fs.h>
71
72#include <linux/toshiba.h>
73
74#define TOSH_MINOR_DEV 181
75
Dmitry Torokhov3f5f7e22005-06-25 14:54:22 -070076MODULE_LICENSE("GPL");
77MODULE_AUTHOR("Jonathan Buzzard <jonathan@buzzard.org.uk>");
78MODULE_DESCRIPTION("Toshiba laptop SMM driver");
79MODULE_SUPPORTED_DEVICE("toshiba");
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Dmitry Torokhov3f5f7e22005-06-25 14:54:22 -070081static int tosh_fn;
82module_param_named(fn, tosh_fn, int, 0);
83MODULE_PARM_DESC(fn, "User specified Fn key detection port");
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Dmitry Torokhov3f5f7e22005-06-25 14:54:22 -070085static int tosh_id;
86static int tosh_bios;
87static int tosh_date;
88static int tosh_sci;
89static int tosh_fan;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91static int tosh_ioctl(struct inode *, struct file *, unsigned int,
92 unsigned long);
93
94
95static struct file_operations tosh_fops = {
96 .owner = THIS_MODULE,
97 .ioctl = tosh_ioctl,
98};
99
100static struct miscdevice tosh_device = {
101 TOSH_MINOR_DEV,
102 "toshiba",
103 &tosh_fops
104};
105
106/*
107 * Read the Fn key status
108 */
109#ifdef CONFIG_PROC_FS
110static int tosh_fn_status(void)
111{
112 unsigned char scan;
113 unsigned long flags;
114
115 if (tosh_fn!=0) {
116 scan = inb(tosh_fn);
117 } else {
118 local_irq_save(flags);
119 outb(0x8e, 0xe4);
120 scan = inb(0xe5);
121 local_irq_restore(flags);
122 }
123
124 return (int) scan;
125}
126#endif
127
128
129/*
130 * For the Portage 610CT and the Tecra 700CS/700CDT emulate the HCI fan function
131 */
132static int tosh_emulate_fan(SMMRegisters *regs)
133{
134 unsigned long eax,ecx,flags;
135 unsigned char al;
136
137 eax = regs->eax & 0xff00;
138 ecx = regs->ecx & 0xffff;
139
140 /* Portage 610CT */
141
142 if (tosh_id==0xfccb) {
143 if (eax==0xfe00) {
144 /* fan status */
145 local_irq_save(flags);
146 outb(0xbe, 0xe4);
147 al = inb(0xe5);
148 local_irq_restore(flags);
149 regs->eax = 0x00;
150 regs->ecx = (unsigned int) (al & 0x01);
151 }
152 if ((eax==0xff00) && (ecx==0x0000)) {
153 /* fan off */
154 local_irq_save(flags);
155 outb(0xbe, 0xe4);
156 al = inb(0xe5);
157 outb(0xbe, 0xe4);
158 outb (al | 0x01, 0xe5);
159 local_irq_restore(flags);
160 regs->eax = 0x00;
161 regs->ecx = 0x00;
162 }
163 if ((eax==0xff00) && (ecx==0x0001)) {
164 /* fan on */
165 local_irq_save(flags);
166 outb(0xbe, 0xe4);
167 al = inb(0xe5);
168 outb(0xbe, 0xe4);
169 outb(al & 0xfe, 0xe5);
170 local_irq_restore(flags);
171 regs->eax = 0x00;
172 regs->ecx = 0x01;
173 }
174 }
175
176 /* Tecra 700CS/CDT */
177
178 if (tosh_id==0xfccc) {
179 if (eax==0xfe00) {
180 /* fan status */
181 local_irq_save(flags);
182 outb(0xe0, 0xe4);
183 al = inb(0xe5);
184 local_irq_restore(flags);
185 regs->eax = 0x00;
186 regs->ecx = al & 0x01;
187 }
188 if ((eax==0xff00) && (ecx==0x0000)) {
189 /* fan off */
190 local_irq_save(flags);
191 outb(0xe0, 0xe4);
192 al = inb(0xe5);
193 outw(0xe0 | ((al & 0xfe) << 8), 0xe4);
194 local_irq_restore(flags);
195 regs->eax = 0x00;
196 regs->ecx = 0x00;
197 }
198 if ((eax==0xff00) && (ecx==0x0001)) {
199 /* fan on */
200 local_irq_save(flags);
201 outb(0xe0, 0xe4);
202 al = inb(0xe5);
203 outw(0xe0 | ((al | 0x01) << 8), 0xe4);
204 local_irq_restore(flags);
205 regs->eax = 0x00;
206 regs->ecx = 0x01;
207 }
208 }
209
210 return 0;
211}
212
213
214/*
215 * Put the laptop into System Management Mode
216 */
217int tosh_smm(SMMRegisters *regs)
218{
219 int eax;
220
221 asm ("# load the values into the registers\n\t" \
222 "pushl %%eax\n\t" \
223 "movl 0(%%eax),%%edx\n\t" \
224 "push %%edx\n\t" \
225 "movl 4(%%eax),%%ebx\n\t" \
226 "movl 8(%%eax),%%ecx\n\t" \
227 "movl 12(%%eax),%%edx\n\t" \
228 "movl 16(%%eax),%%esi\n\t" \
229 "movl 20(%%eax),%%edi\n\t" \
230 "popl %%eax\n\t" \
231 "# call the System Management mode\n\t" \
232 "inb $0xb2,%%al\n\t"
233 "# fill out the memory with the values in the registers\n\t" \
234 "xchgl %%eax,(%%esp)\n\t"
235 "movl %%ebx,4(%%eax)\n\t" \
236 "movl %%ecx,8(%%eax)\n\t" \
237 "movl %%edx,12(%%eax)\n\t" \
238 "movl %%esi,16(%%eax)\n\t" \
239 "movl %%edi,20(%%eax)\n\t" \
240 "popl %%edx\n\t" \
241 "movl %%edx,0(%%eax)\n\t" \
242 "# setup the return value to the carry flag\n\t" \
243 "lahf\n\t" \
244 "shrl $8,%%eax\n\t" \
245 "andl $1,%%eax\n" \
246 : "=a" (eax)
247 : "a" (regs)
248 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
249
250 return eax;
251}
252
253
254static int tosh_ioctl(struct inode *ip, struct file *fp, unsigned int cmd,
255 unsigned long arg)
256{
257 SMMRegisters regs;
258 SMMRegisters __user *argp = (SMMRegisters __user *)arg;
259 unsigned short ax,bx;
260 int err;
261
262 if (!argp)
263 return -EINVAL;
264
265 if (copy_from_user(&regs, argp, sizeof(SMMRegisters)))
266 return -EFAULT;
267
268 switch (cmd) {
269 case TOSH_SMM:
270 ax = regs.eax & 0xff00;
271 bx = regs.ebx & 0xffff;
272 /* block HCI calls to read/write memory & PCI devices */
273 if (((ax==0xff00) || (ax==0xfe00)) && (bx>0x0069))
274 return -EINVAL;
275
276 /* do we need to emulate the fan ? */
277 if (tosh_fan==1) {
278 if (((ax==0xf300) || (ax==0xf400)) && (bx==0x0004)) {
279 err = tosh_emulate_fan(&regs);
280 break;
281 }
282 }
283 err = tosh_smm(&regs);
284 break;
285 default:
286 return -EINVAL;
287 }
288
289 if (copy_to_user(argp, &regs, sizeof(SMMRegisters)))
290 return -EFAULT;
291
292 return (err==0) ? 0:-EINVAL;
293}
294
295
296/*
297 * Print the information for /proc/toshiba
298 */
299#ifdef CONFIG_PROC_FS
300static int tosh_get_info(char *buffer, char **start, off_t fpos, int length)
301{
302 char *temp;
303 int key;
304
305 temp = buffer;
306 key = tosh_fn_status();
307
308 /* Arguments
309 0) Linux driver version (this will change if format changes)
310 1) Machine ID
311 2) SCI version
312 3) BIOS version (major, minor)
313 4) BIOS date (in SCI date format)
314 5) Fn Key status
315 */
316
317 temp += sprintf(temp, "1.1 0x%04x %d.%d %d.%d 0x%04x 0x%02x\n",
318 tosh_id,
319 (tosh_sci & 0xff00)>>8,
320 tosh_sci & 0xff,
321 (tosh_bios & 0xff00)>>8,
322 tosh_bios & 0xff,
323 tosh_date,
324 key);
325
326 return temp-buffer;
327}
328#endif
329
330
331/*
332 * Determine which port to use for the Fn key status
333 */
334static void tosh_set_fn_port(void)
335{
336 switch (tosh_id) {
337 case 0xfc02: case 0xfc04: case 0xfc09: case 0xfc0a: case 0xfc10:
338 case 0xfc11: case 0xfc13: case 0xfc15: case 0xfc1a: case 0xfc1b:
339 case 0xfc5a:
340 tosh_fn = 0x62;
341 break;
342 case 0xfc08: case 0xfc17: case 0xfc1d: case 0xfcd1: case 0xfce0:
343 case 0xfce2:
344 tosh_fn = 0x68;
345 break;
346 default:
347 tosh_fn = 0x00;
348 break;
349 }
350
351 return;
352}
353
354
355/*
356 * Get the machine identification number of the current model
357 */
Al Viroef5a4c82006-03-24 03:15:36 -0800358static int tosh_get_machine_id(void __iomem *bios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 int id;
361 SMMRegisters regs;
362 unsigned short bx,cx;
363 unsigned long address;
364
Al Viroef5a4c82006-03-24 03:15:36 -0800365 id = (0x100*(int) readb(bios+0xfffe))+((int) readb(bios+0xfffa));
Dmitry Torokhov3f5f7e22005-06-25 14:54:22 -0700366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 /* do we have a SCTTable machine identication number on our hands */
368
369 if (id==0xfc2f) {
370
371 /* start by getting a pointer into the BIOS */
372
373 regs.eax = 0xc000;
374 regs.ebx = 0x0000;
375 regs.ecx = 0x0000;
376 tosh_smm(&regs);
377 bx = (unsigned short) (regs.ebx & 0xffff);
378
379 /* At this point in the Toshiba routines under MS Windows
380 the bx register holds 0xe6f5. However my code is producing
381 a different value! For the time being I will just fudge the
382 value. This has been verified on a Satellite Pro 430CDT,
383 Tecra 750CDT, Tecra 780DVD and Satellite 310CDT. */
384#if TOSH_DEBUG
385 printk("toshiba: debugging ID ebx=0x%04x\n", regs.ebx);
386#endif
387 bx = 0xe6f5;
388
389 /* now twiddle with our pointer a bit */
390
Al Viroef5a4c82006-03-24 03:15:36 -0800391 address = bx;
392 cx = readw(bios + address);
393 address = 9+bx+cx;
394 cx = readw(bios + address);
395 address = 0xa+cx;
396 cx = readw(bios + address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 /* now construct our machine identification number */
399
400 id = ((cx & 0xff)<<8)+((cx & 0xff00)>>8);
401 }
402
403 return id;
404}
405
406
407/*
408 * Probe for the presence of a Toshiba laptop
409 *
410 * returns and non-zero if unable to detect the presence of a Toshiba
411 * laptop, otherwise zero and determines the Machine ID, BIOS version and
412 * date, and SCI version.
413 */
414static int tosh_probe(void)
415{
416 int i,major,minor,day,year,month,flag;
417 unsigned char signature[7] = { 0x54,0x4f,0x53,0x48,0x49,0x42,0x41 };
418 SMMRegisters regs;
Al Viroef5a4c82006-03-24 03:15:36 -0800419 void __iomem *bios = ioremap(0xf0000, 0x10000);
420
421 if (!bios)
422 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 /* extra sanity check for the string "TOSHIBA" in the BIOS because
425 some machines that are not Toshiba's pass the next test */
426
427 for (i=0;i<7;i++) {
Al Viroef5a4c82006-03-24 03:15:36 -0800428 if (readb(bios+0xe010+i)!=signature[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 printk("toshiba: not a supported Toshiba laptop\n");
Al Viroef5a4c82006-03-24 03:15:36 -0800430 iounmap(bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return -ENODEV;
432 }
433 }
434
435 /* call the Toshiba SCI support check routine */
Dmitry Torokhov3f5f7e22005-06-25 14:54:22 -0700436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 regs.eax = 0xf0f0;
438 regs.ebx = 0x0000;
439 regs.ecx = 0x0000;
440 flag = tosh_smm(&regs);
441
442 /* if this is not a Toshiba laptop carry flag is set and ah=0x86 */
443
444 if ((flag==1) || ((regs.eax & 0xff00)==0x8600)) {
445 printk("toshiba: not a supported Toshiba laptop\n");
Al Viroef5a4c82006-03-24 03:15:36 -0800446 iounmap(bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return -ENODEV;
448 }
449
450 /* if we get this far then we are running on a Toshiba (probably)! */
451
452 tosh_sci = regs.edx & 0xffff;
Dmitry Torokhov3f5f7e22005-06-25 14:54:22 -0700453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /* next get the machine ID of the current laptop */
455
Al Viroef5a4c82006-03-24 03:15:36 -0800456 tosh_id = tosh_get_machine_id(bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /* get the BIOS version */
459
Al Viroef5a4c82006-03-24 03:15:36 -0800460 major = readb(bios+0xe009)-'0';
461 minor = ((readb(bios+0xe00b)-'0')*10)+(readb(bios+0xe00c)-'0');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 tosh_bios = (major*0x100)+minor;
463
464 /* get the BIOS date */
465
Al Viroef5a4c82006-03-24 03:15:36 -0800466 day = ((readb(bios+0xfff5)-'0')*10)+(readb(bios+0xfff6)-'0');
467 month = ((readb(bios+0xfff8)-'0')*10)+(readb(bios+0xfff9)-'0');
468 year = ((readb(bios+0xfffb)-'0')*10)+(readb(bios+0xfffc)-'0');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 tosh_date = (((year-90) & 0x1f)<<10) | ((month & 0xf)<<6)
470 | ((day & 0x1f)<<1);
471
472
473 /* in theory we should check the ports we are going to use for the
474 fn key detection (and the fan on the Portage 610/Tecra700), and
475 then request them to stop other drivers using them. However as
476 the keyboard driver grabs 0x60-0x6f and the pic driver grabs
477 0xa0-0xbf we can't. We just have to live dangerously and use the
478 ports anyway, oh boy! */
479
480 /* do we need to emulate the fan? */
481
482 if ((tosh_id==0xfccb) || (tosh_id==0xfccc))
483 tosh_fan = 1;
484
Al Viroef5a4c82006-03-24 03:15:36 -0800485 iounmap(bios);
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return 0;
488}
489
Dmitry Torokhov3f5f7e22005-06-25 14:54:22 -0700490static int __init toshiba_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 int retval;
493 /* are we running on a Toshiba laptop */
494
Dmitry Torokhov3f5f7e22005-06-25 14:54:22 -0700495 if (tosh_probe())
496 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Dmitry Torokhov3f5f7e22005-06-25 14:54:22 -0700498 printk(KERN_INFO "Toshiba System Managment Mode driver v" TOSH_VERSION "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 /* set the port to use for Fn status if not specified as a parameter */
501 if (tosh_fn==0x00)
502 tosh_set_fn_port();
503
504 /* register the device file */
505 retval = misc_register(&tosh_device);
Dmitry Torokhov3f5f7e22005-06-25 14:54:22 -0700506 if (retval < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return retval;
508
509#ifdef CONFIG_PROC_FS
510 /* register the proc entry */
Dmitry Torokhov3f5f7e22005-06-25 14:54:22 -0700511 if (create_proc_info_entry("toshiba", 0, NULL, tosh_get_info) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 misc_deregister(&tosh_device);
513 return -ENOMEM;
514 }
515#endif
516
517 return 0;
518}
519
Dmitry Torokhov3f5f7e22005-06-25 14:54:22 -0700520static void __exit toshiba_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 remove_proc_entry("toshiba", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 misc_deregister(&tosh_device);
524}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Dmitry Torokhov3f5f7e22005-06-25 14:54:22 -0700526module_init(toshiba_init);
527module_exit(toshiba_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528