Input: wistron - add support for TravelMate 610
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / input / misc / wistron_btns.c
1 /*
2 * Wistron laptop button driver
3 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
4 * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
5 * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
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 <linux/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>
32 #include <linux/platform_device.h>
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
46 /* BIOS subsystem IDs */
47 #define WIFI 0x35
48 #define BLUETOOTH 0x34
49
50 MODULE_AUTHOR("Miloslav Trmac <mitr@volny.cz>");
51 MODULE_DESCRIPTION("Wistron laptop button driver");
52 MODULE_LICENSE("GPL v2");
53 MODULE_VERSION("0.1");
54
55 static int force; /* = 0; */
56 module_param(force, bool, 0);
57 MODULE_PARM_DESC(force, "Load even if computer is not in database");
58
59 static char *keymap_name; /* = NULL; */
60 module_param_named(keymap, keymap_name, charp, 0);
61 MODULE_PARM_DESC(keymap, "Keymap name, if it can't be autodetected");
62
63 static struct platform_device *wistron_device;
64
65 /* BIOS interface implementation */
66
67 static void __iomem *bios_entry_point; /* BIOS routine entry point */
68 static void __iomem *bios_code_map_base;
69 static void __iomem *bios_data_map_base;
70
71 static u8 cmos_address;
72
73 struct regs {
74 u32 eax, ebx, ecx;
75 };
76
77 static 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
95 static ssize_t __init locate_wistron_bios(void __iomem *base)
96 {
97 static unsigned char __initdata signature[] =
98 { 0x42, 0x21, 0x55, 0x30 };
99 ssize_t offset;
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
109 static int __init map_bios(void)
110 {
111 void __iomem *base;
112 ssize_t offset;
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
150 err_code:
151 iounmap(bios_code_map_base);
152 err:
153 return -ENOMEM;
154 }
155
156 static inline void unmap_bios(void)
157 {
158 iounmap(bios_code_map_base);
159 iounmap(bios_data_map_base);
160 }
161
162 /* BIOS calls */
163
164 static 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
177 static void __devinit bios_attach(void)
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
187 static void bios_detach(void)
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
197 static u8 __devinit bios_get_cmos_address(void)
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
209 static u16 __devinit bios_get_default_setting(u8 subsys)
210 {
211 struct regs regs;
212
213 memset(&regs, 0, sizeof (regs));
214 regs.eax = 0x9610;
215 regs.ebx = 0x0200 | subsys;
216 call_bios(&regs);
217
218 return regs.eax;
219 }
220
221 static void bios_set_state(u8 subsys, int enable)
222 {
223 struct regs regs;
224
225 memset(&regs, 0, sizeof (regs));
226 regs.eax = 0x9610;
227 regs.ebx = (enable ? 0x0100 : 0x0000) | subsys;
228 call_bios(&regs);
229 }
230
231 /* Hardware database */
232
233 struct key_entry {
234 char type; /* See KE_* below */
235 u8 code;
236 unsigned keycode; /* For KE_KEY */
237 };
238
239 enum { KE_END, KE_KEY, KE_WIFI, KE_BLUETOOTH };
240
241 static const struct key_entry *keymap; /* = NULL; Current key map */
242 static int have_wifi;
243 static int have_bluetooth;
244
245 static 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++) {
251 if (key->type == KE_WIFI)
252 have_wifi = 1;
253 else if (key->type == KE_BLUETOOTH)
254 have_bluetooth = 1;
255 }
256 return 1;
257 }
258
259 static struct key_entry keymap_empty[] = {
260 { KE_END, 0 }
261 };
262
263 static struct key_entry keymap_fs_amilo_pro_v2000[] = {
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
273 static struct key_entry keymap_fujitsu_n3510[] = {
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
285 static struct key_entry keymap_wistron_ms2111[] = {
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
294 static struct key_entry keymap_wistron_ms2141[] = {
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
307 static struct key_entry keymap_acer_aspire_1500[] = {
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
317 static struct key_entry keymap_acer_travelmate_240[] = {
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
327 /* Wifi subsystem only activates the led. Therefore we need to pass
328 * wifi event as a normal key, then userspace can really change the wifi state.
329 * TODO we need to export led state to userspace (wifi and mail) */
330 static struct key_entry keymap_acer_travelmate_610[] = {
331 { KE_KEY, 0x01, KEY_HELP },
332 { KE_KEY, 0x02, KEY_CONFIG },
333 { KE_KEY, 0x11, KEY_PROG1 },
334 { KE_KEY, 0x12, KEY_PROG2 },
335 { KE_KEY, 0x13, KEY_PROG3 },
336 { KE_KEY, 0x14, KEY_MAIL },
337 { KE_KEY, 0x15, KEY_WWW },
338 { KE_KEY, 0x40, KEY_WLAN }, /* Wifi */
339 { KE_END, 0 }
340 };
341
342 static struct key_entry keymap_aopen_1559as[] = {
343 { KE_KEY, 0x01, KEY_HELP },
344 { KE_KEY, 0x06, KEY_PROG3 },
345 { KE_KEY, 0x11, KEY_PROG1 },
346 { KE_KEY, 0x12, KEY_PROG2 },
347 { KE_WIFI, 0x30, 0 },
348 { KE_KEY, 0x31, KEY_MAIL },
349 { KE_KEY, 0x36, KEY_WWW },
350 { KE_END, 0 },
351 };
352
353 static struct key_entry keymap_fs_amilo_d88x0[] = {
354 { KE_KEY, 0x01, KEY_HELP },
355 { KE_KEY, 0x08, KEY_MUTE },
356 { KE_KEY, 0x31, KEY_MAIL },
357 { KE_KEY, 0x36, KEY_WWW },
358 { KE_KEY, 0x11, KEY_PROG1 },
359 { KE_KEY, 0x12, KEY_PROG2 },
360 { KE_KEY, 0x13, KEY_PROG3 },
361 { KE_END, 0 }
362 };
363
364 /*
365 * If your machine is not here (which is currently rather likely), please send
366 * a list of buttons and their key codes (reported when loading this module
367 * with force=1) and the output of dmidecode to $MODULE_AUTHOR.
368 */
369 static struct dmi_system_id dmi_ids[] __initdata = {
370 {
371 .callback = dmi_matched,
372 .ident = "Fujitsu-Siemens Amilo Pro V2000",
373 .matches = {
374 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
375 DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Pro V2000"),
376 },
377 .driver_data = keymap_fs_amilo_pro_v2000
378 },
379 {
380 .callback = dmi_matched,
381 .ident = "Fujitsu-Siemens Amilo M7400",
382 .matches = {
383 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
384 DMI_MATCH(DMI_PRODUCT_NAME, "AMILO M "),
385 },
386 .driver_data = keymap_fs_amilo_pro_v2000
387 },
388 {
389 .callback = dmi_matched,
390 .ident = "Fujitsu N3510",
391 .matches = {
392 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
393 DMI_MATCH(DMI_PRODUCT_NAME, "N3510"),
394 },
395 .driver_data = keymap_fujitsu_n3510
396 },
397 {
398 .callback = dmi_matched,
399 .ident = "Acer Aspire 1500",
400 .matches = {
401 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
402 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 1500"),
403 },
404 .driver_data = keymap_acer_aspire_1500
405 },
406 {
407 .callback = dmi_matched,
408 .ident = "Acer TravelMate 240",
409 .matches = {
410 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
411 DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 240"),
412 },
413 .driver_data = keymap_acer_travelmate_240
414 },
415 {
416 .callback = dmi_matched,
417 .ident = "Acer TravelMate 2424NWXCi",
418 .matches = {
419 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
420 DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 2420"),
421 },
422 .driver_data = keymap_acer_travelmate_240
423 },
424 {
425 .callback = dmi_matched,
426 .ident = "Acer TravelMate 610",
427 .matches = {
428 DMI_MATCH(DMI_SYS_VENDOR, "ACER"),
429 DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 610"),
430 },
431 .driver_data = keymap_acer_travelmate_610
432 },
433 {
434 .callback = dmi_matched,
435 .ident = "AOpen 1559AS",
436 .matches = {
437 DMI_MATCH(DMI_PRODUCT_NAME, "E2U"),
438 DMI_MATCH(DMI_BOARD_NAME, "E2U"),
439 },
440 .driver_data = keymap_aopen_1559as
441 },
442 {
443 .callback = dmi_matched,
444 .ident = "Medion MD 9783",
445 .matches = {
446 DMI_MATCH(DMI_SYS_VENDOR, "MEDIONNB"),
447 DMI_MATCH(DMI_PRODUCT_NAME, "MD 9783"),
448 },
449 .driver_data = keymap_wistron_ms2111
450 },
451 {
452 .callback = dmi_matched,
453 .ident = "Fujitsu Siemens Amilo D88x0",
454 .matches = {
455 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
456 DMI_MATCH(DMI_PRODUCT_NAME, "AMILO D"),
457 },
458 .driver_data = keymap_fs_amilo_d88x0
459 },
460 { NULL, }
461 };
462
463 static int __init select_keymap(void)
464 {
465 if (keymap_name != NULL) {
466 if (strcmp (keymap_name, "1557/MS2141") == 0)
467 keymap = keymap_wistron_ms2141;
468 else {
469 printk(KERN_ERR "wistron_btns: Keymap unknown\n");
470 return -EINVAL;
471 }
472 }
473 dmi_check_system(dmi_ids);
474 if (keymap == NULL) {
475 if (!force) {
476 printk(KERN_ERR "wistron_btns: System unknown\n");
477 return -ENODEV;
478 }
479 keymap = keymap_empty;
480 }
481 return 0;
482 }
483
484 /* Input layer interface */
485
486 static struct input_dev *input_dev;
487
488 static int __devinit setup_input_dev(void)
489 {
490 const struct key_entry *key;
491 int error;
492
493 input_dev = input_allocate_device();
494 if (!input_dev)
495 return -ENOMEM;
496
497 input_dev->name = "Wistron laptop buttons";
498 input_dev->phys = "wistron/input0";
499 input_dev->id.bustype = BUS_HOST;
500 input_dev->cdev.dev = &wistron_device->dev;
501
502 for (key = keymap; key->type != KE_END; key++) {
503 if (key->type == KE_KEY) {
504 input_dev->evbit[LONG(EV_KEY)] = BIT(EV_KEY);
505 set_bit(key->keycode, input_dev->keybit);
506 }
507 }
508
509 error = input_register_device(input_dev);
510 if (error) {
511 input_free_device(input_dev);
512 return error;
513 }
514
515 return 0;
516 }
517
518 static void report_key(unsigned keycode)
519 {
520 input_report_key(input_dev, keycode, 1);
521 input_sync(input_dev);
522 input_report_key(input_dev, keycode, 0);
523 input_sync(input_dev);
524 }
525
526 /* Driver core */
527
528 static int wifi_enabled;
529 static int bluetooth_enabled;
530
531 static void poll_bios(unsigned long);
532
533 static struct timer_list poll_timer = TIMER_INITIALIZER(poll_bios, 0, 0);
534
535 static void handle_key(u8 code)
536 {
537 const struct key_entry *key;
538
539 for (key = keymap; key->type != KE_END; key++) {
540 if (code == key->code) {
541 switch (key->type) {
542 case KE_KEY:
543 report_key(key->keycode);
544 break;
545
546 case KE_WIFI:
547 if (have_wifi) {
548 wifi_enabled = !wifi_enabled;
549 bios_set_state(WIFI, wifi_enabled);
550 }
551 break;
552
553 case KE_BLUETOOTH:
554 if (have_bluetooth) {
555 bluetooth_enabled = !bluetooth_enabled;
556 bios_set_state(BLUETOOTH, bluetooth_enabled);
557 }
558 break;
559
560 case KE_END:
561 default:
562 BUG();
563 }
564 return;
565 }
566 }
567 printk(KERN_NOTICE "wistron_btns: Unknown key code %02X\n", code);
568 }
569
570 static void poll_bios(unsigned long discard)
571 {
572 u8 qlen;
573 u16 val;
574
575 for (;;) {
576 qlen = CMOS_READ(cmos_address);
577 if (qlen == 0)
578 break;
579 val = bios_pop_queue();
580 if (val != 0 && !discard)
581 handle_key((u8)val);
582 }
583
584 mod_timer(&poll_timer, jiffies + HZ / POLL_FREQUENCY);
585 }
586
587 static int __devinit wistron_probe(struct platform_device *dev)
588 {
589 int err = setup_input_dev();
590 if (err)
591 return err;
592
593 bios_attach();
594 cmos_address = bios_get_cmos_address();
595
596 if (have_wifi) {
597 u16 wifi = bios_get_default_setting(WIFI);
598 if (wifi & 1)
599 wifi_enabled = (wifi & 2) ? 1 : 0;
600 else
601 have_wifi = 0;
602
603 if (have_wifi)
604 bios_set_state(WIFI, wifi_enabled);
605 }
606
607 if (have_bluetooth) {
608 u16 bt = bios_get_default_setting(BLUETOOTH);
609 if (bt & 1)
610 bluetooth_enabled = (bt & 2) ? 1 : 0;
611 else
612 have_bluetooth = 0;
613
614 if (have_bluetooth)
615 bios_set_state(BLUETOOTH, bluetooth_enabled);
616 }
617
618 poll_bios(1); /* Flush stale event queue and arm timer */
619
620 return 0;
621 }
622
623 static int __devexit wistron_remove(struct platform_device *dev)
624 {
625 del_timer_sync(&poll_timer);
626 input_unregister_device(input_dev);
627 bios_detach();
628
629 return 0;
630 }
631
632 #ifdef CONFIG_PM
633 static int wistron_suspend(struct platform_device *dev, pm_message_t state)
634 {
635 del_timer_sync(&poll_timer);
636
637 if (have_wifi)
638 bios_set_state(WIFI, 0);
639
640 if (have_bluetooth)
641 bios_set_state(BLUETOOTH, 0);
642
643 return 0;
644 }
645
646 static int wistron_resume(struct platform_device *dev)
647 {
648 if (have_wifi)
649 bios_set_state(WIFI, wifi_enabled);
650
651 if (have_bluetooth)
652 bios_set_state(BLUETOOTH, bluetooth_enabled);
653
654 poll_bios(1);
655
656 return 0;
657 }
658 #else
659 #define wistron_suspend NULL
660 #define wistron_resume NULL
661 #endif
662
663 static struct platform_driver wistron_driver = {
664 .driver = {
665 .name = "wistron-bios",
666 .owner = THIS_MODULE,
667 },
668 .probe = wistron_probe,
669 .remove = __devexit_p(wistron_remove),
670 .suspend = wistron_suspend,
671 .resume = wistron_resume,
672 };
673
674 static int __init wb_module_init(void)
675 {
676 int err;
677
678 err = select_keymap();
679 if (err)
680 return err;
681
682 err = map_bios();
683 if (err)
684 return err;
685
686 err = platform_driver_register(&wistron_driver);
687 if (err)
688 goto err_unmap_bios;
689
690 wistron_device = platform_device_alloc("wistron-bios", -1);
691 if (!wistron_device) {
692 err = -ENOMEM;
693 goto err_unregister_driver;
694 }
695
696 err = platform_device_add(wistron_device);
697 if (err)
698 goto err_free_device;
699
700 return 0;
701
702 err_free_device:
703 platform_device_put(wistron_device);
704 err_unregister_driver:
705 platform_driver_unregister(&wistron_driver);
706 err_unmap_bios:
707 unmap_bios();
708
709 return err;
710 }
711
712 static void __exit wb_module_exit(void)
713 {
714 platform_device_unregister(wistron_device);
715 platform_driver_unregister(&wistron_driver);
716 unmap_bios();
717 }
718
719 module_init(wb_module_init);
720 module_exit(wb_module_exit);