llseek: automatically add .llseek fop
[deliverable/linux.git] / drivers / char / toshiba.c
1 /* 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/types.h>
62 #include <linux/fcntl.h>
63 #include <linux/miscdevice.h>
64 #include <linux/ioport.h>
65 #include <asm/io.h>
66 #include <asm/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/smp_lock.h>
72 #include <linux/toshiba.h>
73
74 #define TOSH_MINOR_DEV 181
75
76 MODULE_LICENSE("GPL");
77 MODULE_AUTHOR("Jonathan Buzzard <jonathan@buzzard.org.uk>");
78 MODULE_DESCRIPTION("Toshiba laptop SMM driver");
79 MODULE_SUPPORTED_DEVICE("toshiba");
80
81 static int tosh_fn;
82 module_param_named(fn, tosh_fn, int, 0);
83 MODULE_PARM_DESC(fn, "User specified Fn key detection port");
84
85 static int tosh_id;
86 static int tosh_bios;
87 static int tosh_date;
88 static int tosh_sci;
89 static int tosh_fan;
90
91 static long tosh_ioctl(struct file *, unsigned int,
92 unsigned long);
93
94
95 static const struct file_operations tosh_fops = {
96 .owner = THIS_MODULE,
97 .unlocked_ioctl = tosh_ioctl,
98 .llseek = noop_llseek,
99 };
100
101 static struct miscdevice tosh_device = {
102 TOSH_MINOR_DEV,
103 "toshiba",
104 &tosh_fops
105 };
106
107 /*
108 * Read the Fn key status
109 */
110 #ifdef CONFIG_PROC_FS
111 static int tosh_fn_status(void)
112 {
113 unsigned char scan;
114 unsigned long flags;
115
116 if (tosh_fn!=0) {
117 scan = inb(tosh_fn);
118 } else {
119 local_irq_save(flags);
120 outb(0x8e, 0xe4);
121 scan = inb(0xe5);
122 local_irq_restore(flags);
123 }
124
125 return (int) scan;
126 }
127 #endif
128
129
130 /*
131 * For the Portage 610CT and the Tecra 700CS/700CDT emulate the HCI fan function
132 */
133 static int tosh_emulate_fan(SMMRegisters *regs)
134 {
135 unsigned long eax,ecx,flags;
136 unsigned char al;
137
138 eax = regs->eax & 0xff00;
139 ecx = regs->ecx & 0xffff;
140
141 /* Portage 610CT */
142
143 if (tosh_id==0xfccb) {
144 if (eax==0xfe00) {
145 /* fan status */
146 local_irq_save(flags);
147 outb(0xbe, 0xe4);
148 al = inb(0xe5);
149 local_irq_restore(flags);
150 regs->eax = 0x00;
151 regs->ecx = (unsigned int) (al & 0x01);
152 }
153 if ((eax==0xff00) && (ecx==0x0000)) {
154 /* fan off */
155 local_irq_save(flags);
156 outb(0xbe, 0xe4);
157 al = inb(0xe5);
158 outb(0xbe, 0xe4);
159 outb (al | 0x01, 0xe5);
160 local_irq_restore(flags);
161 regs->eax = 0x00;
162 regs->ecx = 0x00;
163 }
164 if ((eax==0xff00) && (ecx==0x0001)) {
165 /* fan on */
166 local_irq_save(flags);
167 outb(0xbe, 0xe4);
168 al = inb(0xe5);
169 outb(0xbe, 0xe4);
170 outb(al & 0xfe, 0xe5);
171 local_irq_restore(flags);
172 regs->eax = 0x00;
173 regs->ecx = 0x01;
174 }
175 }
176
177 /* Tecra 700CS/CDT */
178
179 if (tosh_id==0xfccc) {
180 if (eax==0xfe00) {
181 /* fan status */
182 local_irq_save(flags);
183 outb(0xe0, 0xe4);
184 al = inb(0xe5);
185 local_irq_restore(flags);
186 regs->eax = 0x00;
187 regs->ecx = al & 0x01;
188 }
189 if ((eax==0xff00) && (ecx==0x0000)) {
190 /* fan off */
191 local_irq_save(flags);
192 outb(0xe0, 0xe4);
193 al = inb(0xe5);
194 outw(0xe0 | ((al & 0xfe) << 8), 0xe4);
195 local_irq_restore(flags);
196 regs->eax = 0x00;
197 regs->ecx = 0x00;
198 }
199 if ((eax==0xff00) && (ecx==0x0001)) {
200 /* fan on */
201 local_irq_save(flags);
202 outb(0xe0, 0xe4);
203 al = inb(0xe5);
204 outw(0xe0 | ((al | 0x01) << 8), 0xe4);
205 local_irq_restore(flags);
206 regs->eax = 0x00;
207 regs->ecx = 0x01;
208 }
209 }
210
211 return 0;
212 }
213
214
215 /*
216 * Put the laptop into System Management Mode
217 */
218 int tosh_smm(SMMRegisters *regs)
219 {
220 int eax;
221
222 asm ("# load the values into the registers\n\t" \
223 "pushl %%eax\n\t" \
224 "movl 0(%%eax),%%edx\n\t" \
225 "push %%edx\n\t" \
226 "movl 4(%%eax),%%ebx\n\t" \
227 "movl 8(%%eax),%%ecx\n\t" \
228 "movl 12(%%eax),%%edx\n\t" \
229 "movl 16(%%eax),%%esi\n\t" \
230 "movl 20(%%eax),%%edi\n\t" \
231 "popl %%eax\n\t" \
232 "# call the System Management mode\n\t" \
233 "inb $0xb2,%%al\n\t"
234 "# fill out the memory with the values in the registers\n\t" \
235 "xchgl %%eax,(%%esp)\n\t"
236 "movl %%ebx,4(%%eax)\n\t" \
237 "movl %%ecx,8(%%eax)\n\t" \
238 "movl %%edx,12(%%eax)\n\t" \
239 "movl %%esi,16(%%eax)\n\t" \
240 "movl %%edi,20(%%eax)\n\t" \
241 "popl %%edx\n\t" \
242 "movl %%edx,0(%%eax)\n\t" \
243 "# setup the return value to the carry flag\n\t" \
244 "lahf\n\t" \
245 "shrl $8,%%eax\n\t" \
246 "andl $1,%%eax\n" \
247 : "=a" (eax)
248 : "a" (regs)
249 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
250
251 return eax;
252 }
253 EXPORT_SYMBOL(tosh_smm);
254
255
256 static long tosh_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
257 {
258 SMMRegisters regs;
259 SMMRegisters __user *argp = (SMMRegisters __user *)arg;
260 unsigned short ax,bx;
261 int err;
262
263 if (!argp)
264 return -EINVAL;
265
266 if (copy_from_user(&regs, argp, sizeof(SMMRegisters)))
267 return -EFAULT;
268
269 switch (cmd) {
270 case TOSH_SMM:
271 ax = regs.eax & 0xff00;
272 bx = regs.ebx & 0xffff;
273 /* block HCI calls to read/write memory & PCI devices */
274 if (((ax==0xff00) || (ax==0xfe00)) && (bx>0x0069))
275 return -EINVAL;
276
277 /* do we need to emulate the fan ? */
278 lock_kernel();
279 if (tosh_fan==1) {
280 if (((ax==0xf300) || (ax==0xf400)) && (bx==0x0004)) {
281 err = tosh_emulate_fan(&regs);
282 unlock_kernel();
283 break;
284 }
285 }
286 err = tosh_smm(&regs);
287 unlock_kernel();
288 break;
289 default:
290 return -EINVAL;
291 }
292
293 if (copy_to_user(argp, &regs, sizeof(SMMRegisters)))
294 return -EFAULT;
295
296 return (err==0) ? 0:-EINVAL;
297 }
298
299
300 /*
301 * Print the information for /proc/toshiba
302 */
303 #ifdef CONFIG_PROC_FS
304 static int proc_toshiba_show(struct seq_file *m, void *v)
305 {
306 int key;
307
308 key = tosh_fn_status();
309
310 /* Arguments
311 0) Linux driver version (this will change if format changes)
312 1) Machine ID
313 2) SCI version
314 3) BIOS version (major, minor)
315 4) BIOS date (in SCI date format)
316 5) Fn Key status
317 */
318 seq_printf(m, "1.1 0x%04x %d.%d %d.%d 0x%04x 0x%02x\n",
319 tosh_id,
320 (tosh_sci & 0xff00)>>8,
321 tosh_sci & 0xff,
322 (tosh_bios & 0xff00)>>8,
323 tosh_bios & 0xff,
324 tosh_date,
325 key);
326 return 0;
327 }
328
329 static int proc_toshiba_open(struct inode *inode, struct file *file)
330 {
331 return single_open(file, proc_toshiba_show, NULL);
332 }
333
334 static const struct file_operations proc_toshiba_fops = {
335 .owner = THIS_MODULE,
336 .open = proc_toshiba_open,
337 .read = seq_read,
338 .llseek = seq_lseek,
339 .release = single_release,
340 };
341 #endif
342
343
344 /*
345 * Determine which port to use for the Fn key status
346 */
347 static void tosh_set_fn_port(void)
348 {
349 switch (tosh_id) {
350 case 0xfc02: case 0xfc04: case 0xfc09: case 0xfc0a: case 0xfc10:
351 case 0xfc11: case 0xfc13: case 0xfc15: case 0xfc1a: case 0xfc1b:
352 case 0xfc5a:
353 tosh_fn = 0x62;
354 break;
355 case 0xfc08: case 0xfc17: case 0xfc1d: case 0xfcd1: case 0xfce0:
356 case 0xfce2:
357 tosh_fn = 0x68;
358 break;
359 default:
360 tosh_fn = 0x00;
361 break;
362 }
363
364 return;
365 }
366
367
368 /*
369 * Get the machine identification number of the current model
370 */
371 static int tosh_get_machine_id(void __iomem *bios)
372 {
373 int id;
374 SMMRegisters regs;
375 unsigned short bx,cx;
376 unsigned long address;
377
378 id = (0x100*(int) readb(bios+0xfffe))+((int) readb(bios+0xfffa));
379
380 /* do we have a SCTTable machine identication number on our hands */
381
382 if (id==0xfc2f) {
383
384 /* start by getting a pointer into the BIOS */
385
386 regs.eax = 0xc000;
387 regs.ebx = 0x0000;
388 regs.ecx = 0x0000;
389 tosh_smm(&regs);
390 bx = (unsigned short) (regs.ebx & 0xffff);
391
392 /* At this point in the Toshiba routines under MS Windows
393 the bx register holds 0xe6f5. However my code is producing
394 a different value! For the time being I will just fudge the
395 value. This has been verified on a Satellite Pro 430CDT,
396 Tecra 750CDT, Tecra 780DVD and Satellite 310CDT. */
397 #if TOSH_DEBUG
398 printk("toshiba: debugging ID ebx=0x%04x\n", regs.ebx);
399 #endif
400 bx = 0xe6f5;
401
402 /* now twiddle with our pointer a bit */
403
404 address = bx;
405 cx = readw(bios + address);
406 address = 9+bx+cx;
407 cx = readw(bios + address);
408 address = 0xa+cx;
409 cx = readw(bios + address);
410
411 /* now construct our machine identification number */
412
413 id = ((cx & 0xff)<<8)+((cx & 0xff00)>>8);
414 }
415
416 return id;
417 }
418
419
420 /*
421 * Probe for the presence of a Toshiba laptop
422 *
423 * returns and non-zero if unable to detect the presence of a Toshiba
424 * laptop, otherwise zero and determines the Machine ID, BIOS version and
425 * date, and SCI version.
426 */
427 static int tosh_probe(void)
428 {
429 int i,major,minor,day,year,month,flag;
430 unsigned char signature[7] = { 0x54,0x4f,0x53,0x48,0x49,0x42,0x41 };
431 SMMRegisters regs;
432 void __iomem *bios = ioremap_cache(0xf0000, 0x10000);
433
434 if (!bios)
435 return -ENOMEM;
436
437 /* extra sanity check for the string "TOSHIBA" in the BIOS because
438 some machines that are not Toshiba's pass the next test */
439
440 for (i=0;i<7;i++) {
441 if (readb(bios+0xe010+i)!=signature[i]) {
442 printk("toshiba: not a supported Toshiba laptop\n");
443 iounmap(bios);
444 return -ENODEV;
445 }
446 }
447
448 /* call the Toshiba SCI support check routine */
449
450 regs.eax = 0xf0f0;
451 regs.ebx = 0x0000;
452 regs.ecx = 0x0000;
453 flag = tosh_smm(&regs);
454
455 /* if this is not a Toshiba laptop carry flag is set and ah=0x86 */
456
457 if ((flag==1) || ((regs.eax & 0xff00)==0x8600)) {
458 printk("toshiba: not a supported Toshiba laptop\n");
459 iounmap(bios);
460 return -ENODEV;
461 }
462
463 /* if we get this far then we are running on a Toshiba (probably)! */
464
465 tosh_sci = regs.edx & 0xffff;
466
467 /* next get the machine ID of the current laptop */
468
469 tosh_id = tosh_get_machine_id(bios);
470
471 /* get the BIOS version */
472
473 major = readb(bios+0xe009)-'0';
474 minor = ((readb(bios+0xe00b)-'0')*10)+(readb(bios+0xe00c)-'0');
475 tosh_bios = (major*0x100)+minor;
476
477 /* get the BIOS date */
478
479 day = ((readb(bios+0xfff5)-'0')*10)+(readb(bios+0xfff6)-'0');
480 month = ((readb(bios+0xfff8)-'0')*10)+(readb(bios+0xfff9)-'0');
481 year = ((readb(bios+0xfffb)-'0')*10)+(readb(bios+0xfffc)-'0');
482 tosh_date = (((year-90) & 0x1f)<<10) | ((month & 0xf)<<6)
483 | ((day & 0x1f)<<1);
484
485
486 /* in theory we should check the ports we are going to use for the
487 fn key detection (and the fan on the Portage 610/Tecra700), and
488 then request them to stop other drivers using them. However as
489 the keyboard driver grabs 0x60-0x6f and the pic driver grabs
490 0xa0-0xbf we can't. We just have to live dangerously and use the
491 ports anyway, oh boy! */
492
493 /* do we need to emulate the fan? */
494
495 if ((tosh_id==0xfccb) || (tosh_id==0xfccc))
496 tosh_fan = 1;
497
498 iounmap(bios);
499
500 return 0;
501 }
502
503 static int __init toshiba_init(void)
504 {
505 int retval;
506 /* are we running on a Toshiba laptop */
507
508 if (tosh_probe())
509 return -ENODEV;
510
511 printk(KERN_INFO "Toshiba System Management Mode driver v" TOSH_VERSION "\n");
512
513 /* set the port to use for Fn status if not specified as a parameter */
514 if (tosh_fn==0x00)
515 tosh_set_fn_port();
516
517 /* register the device file */
518 retval = misc_register(&tosh_device);
519 if (retval < 0)
520 return retval;
521
522 #ifdef CONFIG_PROC_FS
523 {
524 struct proc_dir_entry *pde;
525
526 pde = proc_create("toshiba", 0, NULL, &proc_toshiba_fops);
527 if (!pde) {
528 misc_deregister(&tosh_device);
529 return -ENOMEM;
530 }
531 }
532 #endif
533
534 return 0;
535 }
536
537 static void __exit toshiba_exit(void)
538 {
539 remove_proc_entry("toshiba", NULL);
540 misc_deregister(&tosh_device);
541 }
542
543 module_init(toshiba_init);
544 module_exit(toshiba_exit);
545
This page took 0.05848 seconds and 5 git commands to generate.