Input: wistron - add support for Acer TravelMate 2424NWXCi
[linux-2.6-block.git] / drivers / input / misc / wistron_btns.c
CommitLineData
5fc14680
DT
1/*
2 * Wistron laptop button driver
3 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
84b256a6 4 * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
a5b0cc80 5 * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
5fc14680
DT
6 *
7 * You can redistribute and/or modify this program under the terms of the
8 * GNU General Public License version 2 as published by the Free Software
9 * Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14 * Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 59 Temple Place Suite 330, Boston, MA 02111-1307, USA.
19 */
20#include <asm/io.h>
21#include <linux/dmi.h>
22#include <linux/init.h>
23#include <linux/input.h>
24#include <linux/interrupt.h>
25#include <linux/kernel.h>
26#include <linux/mc146818rtc.h>
27#include <linux/module.h>
28#include <linux/preempt.h>
29#include <linux/string.h>
30#include <linux/timer.h>
31#include <linux/types.h>
a5b0cc80 32#include <linux/platform_device.h>
5fc14680
DT
33
34/*
35 * Number of attempts to read data from queue per poll;
36 * the queue can hold up to 31 entries
37 */
38#define MAX_POLL_ITERATIONS 64
39
40#define POLL_FREQUENCY 10 /* Number of polls per second */
41
42#if POLL_FREQUENCY > HZ
43#error "POLL_FREQUENCY too high"
44#endif
45
84b256a6
BR
46/* BIOS subsystem IDs */
47#define WIFI 0x35
48#define BLUETOOTH 0x34
49
5fc14680
DT
50MODULE_AUTHOR("Miloslav Trmac <mitr@volny.cz>");
51MODULE_DESCRIPTION("Wistron laptop button driver");
52MODULE_LICENSE("GPL v2");
53MODULE_VERSION("0.1");
54
55static int force; /* = 0; */
56module_param(force, bool, 0);
57MODULE_PARM_DESC(force, "Load even if computer is not in database");
58
59static char *keymap_name; /* = NULL; */
60module_param_named(keymap, keymap_name, charp, 0);
61MODULE_PARM_DESC(keymap, "Keymap name, if it can't be autodetected");
62
a5b0cc80
DT
63static struct platform_device *wistron_device;
64
5fc14680
DT
65 /* BIOS interface implementation */
66
67static void __iomem *bios_entry_point; /* BIOS routine entry point */
68static void __iomem *bios_code_map_base;
69static void __iomem *bios_data_map_base;
70
71static u8 cmos_address;
72
73struct regs {
74 u32 eax, ebx, ecx;
75};
76
77static void call_bios(struct regs *regs)
78{
79 unsigned long flags;
80
81 preempt_disable();
82 local_irq_save(flags);
83 asm volatile ("pushl %%ebp;"
84 "movl %7, %%ebp;"
85 "call *%6;"
86 "popl %%ebp"
87 : "=a" (regs->eax), "=b" (regs->ebx), "=c" (regs->ecx)
88 : "0" (regs->eax), "1" (regs->ebx), "2" (regs->ecx),
89 "m" (bios_entry_point), "m" (bios_data_map_base)
90 : "edx", "edi", "esi", "memory");
91 local_irq_restore(flags);
92 preempt_enable();
93}
94
c28c3583 95static ssize_t __init locate_wistron_bios(void __iomem *base)
5fc14680 96{
c7948989 97 static unsigned char __initdata signature[] =
5fc14680 98 { 0x42, 0x21, 0x55, 0x30 };
c28c3583 99 ssize_t offset;
5fc14680
DT
100
101 for (offset = 0; offset < 0x10000; offset += 0x10) {
102 if (check_signature(base + offset, signature,
103 sizeof(signature)) != 0)
104 return offset;
105 }
106 return -1;
107}
108
109static int __init map_bios(void)
110{
111 void __iomem *base;
c28c3583 112 ssize_t offset;
5fc14680
DT
113 u32 entry_point;
114
115 base = ioremap(0xF0000, 0x10000); /* Can't fail */
116 offset = locate_wistron_bios(base);
117 if (offset < 0) {
118 printk(KERN_ERR "wistron_btns: BIOS entry point not found\n");
119 iounmap(base);
120 return -ENODEV;
121 }
122
123 entry_point = readl(base + offset + 5);
124 printk(KERN_DEBUG
125 "wistron_btns: BIOS signature found at %p, entry point %08X\n",
126 base + offset, entry_point);
127
128 if (entry_point >= 0xF0000) {
129 bios_code_map_base = base;
130 bios_entry_point = bios_code_map_base + (entry_point & 0xFFFF);
131 } else {
132 iounmap(base);
133 bios_code_map_base = ioremap(entry_point & ~0x3FFF, 0x4000);
134 if (bios_code_map_base == NULL) {
135 printk(KERN_ERR
136 "wistron_btns: Can't map BIOS code at %08X\n",
137 entry_point & ~0x3FFF);
138 goto err;
139 }
140 bios_entry_point = bios_code_map_base + (entry_point & 0x3FFF);
141 }
142 /* The Windows driver maps 0x10000 bytes, we keep only one page... */
143 bios_data_map_base = ioremap(0x400, 0xc00);
144 if (bios_data_map_base == NULL) {
145 printk(KERN_ERR "wistron_btns: Can't map BIOS data\n");
146 goto err_code;
147 }
148 return 0;
149
150err_code:
151 iounmap(bios_code_map_base);
152err:
153 return -ENOMEM;
154}
155
22a397e2 156static inline void unmap_bios(void)
5fc14680
DT
157{
158 iounmap(bios_code_map_base);
159 iounmap(bios_data_map_base);
160}
161
162 /* BIOS calls */
163
164static u16 bios_pop_queue(void)
165{
166 struct regs regs;
167
168 memset(&regs, 0, sizeof (regs));
169 regs.eax = 0x9610;
170 regs.ebx = 0x061C;
171 regs.ecx = 0x0000;
172 call_bios(&regs);
173
174 return regs.eax;
175}
176
e7c3aad5 177static void __devinit bios_attach(void)
5fc14680
DT
178{
179 struct regs regs;
180
181 memset(&regs, 0, sizeof (regs));
182 regs.eax = 0x9610;
183 regs.ebx = 0x012E;
184 call_bios(&regs);
185}
186
22a397e2 187static void bios_detach(void)
5fc14680
DT
188{
189 struct regs regs;
190
191 memset(&regs, 0, sizeof (regs));
192 regs.eax = 0x9610;
193 regs.ebx = 0x002E;
194 call_bios(&regs);
195}
196
e7c3aad5 197static u8 __devinit bios_get_cmos_address(void)
5fc14680
DT
198{
199 struct regs regs;
200
201 memset(&regs, 0, sizeof (regs));
202 regs.eax = 0x9610;
203 regs.ebx = 0x051C;
204 call_bios(&regs);
205
206 return regs.ecx;
207}
208
e7c3aad5 209static u16 __devinit bios_get_default_setting(u8 subsys)
5fc14680
DT
210{
211 struct regs regs;
212
213 memset(&regs, 0, sizeof (regs));
214 regs.eax = 0x9610;
84b256a6 215 regs.ebx = 0x0200 | subsys;
5fc14680
DT
216 call_bios(&regs);
217
218 return regs.eax;
219}
220
84b256a6 221static void bios_set_state(u8 subsys, int enable)
5fc14680
DT
222{
223 struct regs regs;
224
225 memset(&regs, 0, sizeof (regs));
226 regs.eax = 0x9610;
84b256a6 227 regs.ebx = (enable ? 0x0100 : 0x0000) | subsys;
5fc14680
DT
228 call_bios(&regs);
229}
230
84b256a6 231/* Hardware database */
5fc14680
DT
232
233struct key_entry {
234 char type; /* See KE_* below */
235 u8 code;
236 unsigned keycode; /* For KE_KEY */
237};
238
84b256a6 239enum { KE_END, KE_KEY, KE_WIFI, KE_BLUETOOTH };
5fc14680
DT
240
241static const struct key_entry *keymap; /* = NULL; Current key map */
242static int have_wifi;
84b256a6 243static int have_bluetooth;
5fc14680
DT
244
245static int __init dmi_matched(struct dmi_system_id *dmi)
246{
247 const struct key_entry *key;
248
249 keymap = dmi->driver_data;
250 for (key = keymap; key->type != KE_END; key++) {
cde45f19 251 if (key->type == KE_WIFI)
5fc14680 252 have_wifi = 1;
cde45f19 253 else if (key->type == KE_BLUETOOTH)
84b256a6 254 have_bluetooth = 1;
5fc14680
DT
255 }
256 return 1;
257}
258
72a623be 259static struct key_entry keymap_empty[] = {
5fc14680
DT
260 { KE_END, 0 }
261};
262
72a623be 263static struct key_entry keymap_fs_amilo_pro_v2000[] = {
5fc14680
DT
264 { KE_KEY, 0x01, KEY_HELP },
265 { KE_KEY, 0x11, KEY_PROG1 },
266 { KE_KEY, 0x12, KEY_PROG2 },
267 { KE_WIFI, 0x30, 0 },
268 { KE_KEY, 0x31, KEY_MAIL },
269 { KE_KEY, 0x36, KEY_WWW },
270 { KE_END, 0 }
271};
272
72a623be 273static struct key_entry keymap_fujitsu_n3510[] = {
e2aa507a
JRR
274 { KE_KEY, 0x11, KEY_PROG1 },
275 { KE_KEY, 0x12, KEY_PROG2 },
276 { KE_KEY, 0x36, KEY_WWW },
277 { KE_KEY, 0x31, KEY_MAIL },
278 { KE_KEY, 0x71, KEY_STOPCD },
279 { KE_KEY, 0x72, KEY_PLAYPAUSE },
280 { KE_KEY, 0x74, KEY_REWIND },
281 { KE_KEY, 0x78, KEY_FORWARD },
282 { KE_END, 0 }
283};
284
72a623be 285static struct key_entry keymap_wistron_ms2111[] = {
9000195b
FL
286 { KE_KEY, 0x11, KEY_PROG1 },
287 { KE_KEY, 0x12, KEY_PROG2 },
288 { KE_KEY, 0x13, KEY_PROG3 },
289 { KE_KEY, 0x31, KEY_MAIL },
290 { KE_KEY, 0x36, KEY_WWW },
291 { KE_END, 0 }
292};
293
72a623be 294static struct key_entry keymap_wistron_ms2141[] = {
5fc14680
DT
295 { KE_KEY, 0x11, KEY_PROG1 },
296 { KE_KEY, 0x12, KEY_PROG2 },
297 { KE_WIFI, 0x30, 0 },
298 { KE_KEY, 0x22, KEY_REWIND },
299 { KE_KEY, 0x23, KEY_FORWARD },
300 { KE_KEY, 0x24, KEY_PLAYPAUSE },
301 { KE_KEY, 0x25, KEY_STOPCD },
302 { KE_KEY, 0x31, KEY_MAIL },
303 { KE_KEY, 0x36, KEY_WWW },
304 { KE_END, 0 }
305};
306
72a623be 307static struct key_entry keymap_acer_aspire_1500[] = {
84b256a6
BR
308 { KE_KEY, 0x11, KEY_PROG1 },
309 { KE_KEY, 0x12, KEY_PROG2 },
310 { KE_WIFI, 0x30, 0 },
311 { KE_KEY, 0x31, KEY_MAIL },
312 { KE_KEY, 0x36, KEY_WWW },
313 { KE_BLUETOOTH, 0x44, 0 },
314 { KE_END, 0 }
315};
316
72a623be 317static struct key_entry keymap_acer_travelmate_240[] = {
74a89c96
AN
318 { KE_KEY, 0x31, KEY_MAIL },
319 { KE_KEY, 0x36, KEY_WWW },
320 { KE_KEY, 0x11, KEY_PROG1 },
321 { KE_KEY, 0x12, KEY_PROG2 },
322 { KE_BLUETOOTH, 0x44, 0 },
323 { KE_WIFI, 0x30, 0 },
324 { KE_END, 0 }
325};
326
72a623be 327static struct key_entry keymap_aopen_1559as[] = {
e107b8ee 328 { KE_KEY, 0x01, KEY_HELP },
329 { KE_KEY, 0x06, KEY_PROG3 },
330 { KE_KEY, 0x11, KEY_PROG1 },
331 { KE_KEY, 0x12, KEY_PROG2 },
332 { KE_WIFI, 0x30, 0 },
333 { KE_KEY, 0x31, KEY_MAIL },
334 { KE_KEY, 0x36, KEY_WWW },
9000195b 335 { KE_END, 0 },
e107b8ee 336};
337
5fc14680
DT
338/*
339 * If your machine is not here (which is currently rather likely), please send
340 * a list of buttons and their key codes (reported when loading this module
341 * with force=1) and the output of dmidecode to $MODULE_AUTHOR.
342 */
c7948989 343static struct dmi_system_id dmi_ids[] __initdata = {
5fc14680
DT
344 {
345 .callback = dmi_matched,
346 .ident = "Fujitsu-Siemens Amilo Pro V2000",
347 .matches = {
348 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
349 DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Pro V2000"),
350 },
351 .driver_data = keymap_fs_amilo_pro_v2000
352 },
8a1b1708
SR
353 {
354 .callback = dmi_matched,
355 .ident = "Fujitsu-Siemens Amilo M7400",
356 .matches = {
357 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
358 DMI_MATCH(DMI_PRODUCT_NAME, "AMILO M "),
359 },
360 .driver_data = keymap_fs_amilo_pro_v2000
361 },
e2aa507a
JRR
362 {
363 .callback = dmi_matched,
364 .ident = "Fujitsu N3510",
365 .matches = {
366 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
367 DMI_MATCH(DMI_PRODUCT_NAME, "N3510"),
368 },
369 .driver_data = keymap_fujitsu_n3510
370 },
84b256a6
BR
371 {
372 .callback = dmi_matched,
373 .ident = "Acer Aspire 1500",
374 .matches = {
375 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
376 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 1500"),
377 },
378 .driver_data = keymap_acer_aspire_1500
379 },
74a89c96
AN
380 {
381 .callback = dmi_matched,
382 .ident = "Acer TravelMate 240",
383 .matches = {
384 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
385 DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 240"),
386 },
387 .driver_data = keymap_acer_travelmate_240
388 },
bb088590
AN
389 {
390 .callback = dmi_matched,
391 .ident = "Acer TravelMate 2424NWXCi",
392 .matches = {
393 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
394 DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 2420"),
395 },
396 .driver_data = keymap_acer_travelmate_240
397 },
398 {
e107b8ee 399 .callback = dmi_matched,
400 .ident = "AOpen 1559AS",
401 .matches = {
402 DMI_MATCH(DMI_PRODUCT_NAME, "E2U"),
403 DMI_MATCH(DMI_BOARD_NAME, "E2U"),
404 },
405 .driver_data = keymap_aopen_1559as
406 },
9000195b
FL
407 {
408 .callback = dmi_matched,
409 .ident = "Medion MD 9783",
410 .matches = {
411 DMI_MATCH(DMI_SYS_VENDOR, "MEDIONNB"),
412 DMI_MATCH(DMI_PRODUCT_NAME, "MD 9783"),
413 },
414 .driver_data = keymap_wistron_ms2111
415 },
81f0a91e 416 { NULL, }
5fc14680
DT
417};
418
419static int __init select_keymap(void)
420{
421 if (keymap_name != NULL) {
422 if (strcmp (keymap_name, "1557/MS2141") == 0)
423 keymap = keymap_wistron_ms2141;
424 else {
425 printk(KERN_ERR "wistron_btns: Keymap unknown\n");
426 return -EINVAL;
427 }
428 }
429 dmi_check_system(dmi_ids);
430 if (keymap == NULL) {
431 if (!force) {
432 printk(KERN_ERR "wistron_btns: System unknown\n");
433 return -ENODEV;
434 }
435 keymap = keymap_empty;
436 }
437 return 0;
438}
439
440 /* Input layer interface */
441
22a397e2 442static struct input_dev *input_dev;
5fc14680 443
e7c3aad5 444static int __devinit setup_input_dev(void)
5fc14680
DT
445{
446 const struct key_entry *key;
22a397e2
DT
447 int error;
448
449 input_dev = input_allocate_device();
450 if (!input_dev)
451 return -ENOMEM;
452
453 input_dev->name = "Wistron laptop buttons";
454 input_dev->phys = "wistron/input0";
455 input_dev->id.bustype = BUS_HOST;
a5b0cc80 456 input_dev->cdev.dev = &wistron_device->dev;
5fc14680
DT
457
458 for (key = keymap; key->type != KE_END; key++) {
459 if (key->type == KE_KEY) {
22a397e2
DT
460 input_dev->evbit[LONG(EV_KEY)] = BIT(EV_KEY);
461 set_bit(key->keycode, input_dev->keybit);
5fc14680
DT
462 }
463 }
464
22a397e2
DT
465 error = input_register_device(input_dev);
466 if (error) {
467 input_free_device(input_dev);
468 return error;
469 }
470
471 return 0;
5fc14680
DT
472}
473
474static void report_key(unsigned keycode)
475{
22a397e2
DT
476 input_report_key(input_dev, keycode, 1);
477 input_sync(input_dev);
478 input_report_key(input_dev, keycode, 0);
479 input_sync(input_dev);
5fc14680
DT
480}
481
482 /* Driver core */
483
484static int wifi_enabled;
84b256a6 485static int bluetooth_enabled;
5fc14680
DT
486
487static void poll_bios(unsigned long);
488
489static struct timer_list poll_timer = TIMER_INITIALIZER(poll_bios, 0, 0);
490
491static void handle_key(u8 code)
492{
493 const struct key_entry *key;
494
495 for (key = keymap; key->type != KE_END; key++) {
496 if (code == key->code) {
497 switch (key->type) {
498 case KE_KEY:
499 report_key(key->keycode);
500 break;
501
502 case KE_WIFI:
503 if (have_wifi) {
504 wifi_enabled = !wifi_enabled;
84b256a6
BR
505 bios_set_state(WIFI, wifi_enabled);
506 }
507 break;
508
509 case KE_BLUETOOTH:
510 if (have_bluetooth) {
511 bluetooth_enabled = !bluetooth_enabled;
512 bios_set_state(BLUETOOTH, bluetooth_enabled);
5fc14680
DT
513 }
514 break;
515
516 case KE_END:
517 default:
518 BUG();
519 }
520 return;
521 }
522 }
523 printk(KERN_NOTICE "wistron_btns: Unknown key code %02X\n", code);
524}
525
526static void poll_bios(unsigned long discard)
527{
528 u8 qlen;
529 u16 val;
530
531 for (;;) {
532 qlen = CMOS_READ(cmos_address);
533 if (qlen == 0)
534 break;
535 val = bios_pop_queue();
536 if (val != 0 && !discard)
537 handle_key((u8)val);
538 }
539
540 mod_timer(&poll_timer, jiffies + HZ / POLL_FREQUENCY);
541}
542
e7c3aad5
DT
543static int __devinit wistron_probe(struct platform_device *dev)
544{
545 int err = setup_input_dev();
546 if (err)
547 return err;
548
549 bios_attach();
550 cmos_address = bios_get_cmos_address();
551
552 if (have_wifi) {
553 u16 wifi = bios_get_default_setting(WIFI);
554 if (wifi & 1)
555 wifi_enabled = (wifi & 2) ? 1 : 0;
556 else
557 have_wifi = 0;
558
559 if (have_wifi)
560 bios_set_state(WIFI, wifi_enabled);
561 }
562
563 if (have_bluetooth) {
564 u16 bt = bios_get_default_setting(BLUETOOTH);
565 if (bt & 1)
566 bluetooth_enabled = (bt & 2) ? 1 : 0;
567 else
568 have_bluetooth = 0;
569
570 if (have_bluetooth)
571 bios_set_state(BLUETOOTH, bluetooth_enabled);
572 }
573
574 poll_bios(1); /* Flush stale event queue and arm timer */
575
576 return 0;
577}
578
579static int __devexit wistron_remove(struct platform_device *dev)
580{
581 del_timer_sync(&poll_timer);
582 input_unregister_device(input_dev);
583 bios_detach();
584
585 return 0;
586}
587
588#ifdef CONFIG_PM
a5b0cc80
DT
589static int wistron_suspend(struct platform_device *dev, pm_message_t state)
590{
591 del_timer_sync(&poll_timer);
592
e753b650
MT
593 if (have_wifi)
594 bios_set_state(WIFI, 0);
595
596 if (have_bluetooth)
597 bios_set_state(BLUETOOTH, 0);
598
a5b0cc80
DT
599 return 0;
600}
601
602static int wistron_resume(struct platform_device *dev)
603{
604 if (have_wifi)
605 bios_set_state(WIFI, wifi_enabled);
606
607 if (have_bluetooth)
608 bios_set_state(BLUETOOTH, bluetooth_enabled);
609
610 poll_bios(1);
611
612 return 0;
613}
e7c3aad5
DT
614#else
615#define wistron_suspend NULL
616#define wistron_resume NULL
617#endif
a5b0cc80
DT
618
619static struct platform_driver wistron_driver = {
a5b0cc80
DT
620 .driver = {
621 .name = "wistron-bios",
e7c3aad5 622 .owner = THIS_MODULE,
a5b0cc80 623 },
e7c3aad5
DT
624 .probe = wistron_probe,
625 .remove = __devexit_p(wistron_remove),
626 .suspend = wistron_suspend,
627 .resume = wistron_resume,
a5b0cc80
DT
628};
629
5fc14680
DT
630static int __init wb_module_init(void)
631{
632 int err;
633
634 err = select_keymap();
635 if (err)
636 return err;
22a397e2 637
5fc14680
DT
638 err = map_bios();
639 if (err)
640 return err;
22a397e2 641
a5b0cc80
DT
642 err = platform_driver_register(&wistron_driver);
643 if (err)
e7c3aad5 644 goto err_unmap_bios;
a5b0cc80 645
e7c3aad5
DT
646 wistron_device = platform_device_alloc("wistron-bios", -1);
647 if (!wistron_device) {
648 err = -ENOMEM;
a5b0cc80
DT
649 goto err_unregister_driver;
650 }
651
e7c3aad5 652 err = platform_device_add(wistron_device);
a5b0cc80 653 if (err)
e7c3aad5 654 goto err_free_device;
5fc14680
DT
655
656 return 0;
a5b0cc80 657
e7c3aad5
DT
658 err_free_device:
659 platform_device_put(wistron_device);
a5b0cc80
DT
660 err_unregister_driver:
661 platform_driver_unregister(&wistron_driver);
e7c3aad5 662 err_unmap_bios:
a5b0cc80
DT
663 unmap_bios();
664
665 return err;
5fc14680
DT
666}
667
668static void __exit wb_module_exit(void)
669{
a5b0cc80
DT
670 platform_device_unregister(wistron_device);
671 platform_driver_unregister(&wistron_driver);
5fc14680
DT
672 unmap_bios();
673}
674
675module_init(wb_module_init);
676module_exit(wb_module_exit);