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>
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
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.
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.
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>
35 * Number of attempts to read data from queue per poll;
36 * the queue can hold up to 31 entries
38 #define MAX_POLL_ITERATIONS 64
40 #define POLL_FREQUENCY 10 /* Number of polls per second */
42 #if POLL_FREQUENCY > HZ
43 #error "POLL_FREQUENCY too high"
46 /* BIOS subsystem IDs */
48 #define BLUETOOTH 0x34
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");
55 static int force; /* = 0; */
56 module_param(force, bool, 0);
57 MODULE_PARM_DESC(force, "Load even if computer is not in database");
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");
63 static struct platform_device *wistron_device;
65 /* BIOS interface implementation */
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;
71 static u8 cmos_address;
77 static void call_bios(struct regs *regs)
82 local_irq_save(flags);
83 asm volatile ("pushl %%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);
95 static ssize_t __init locate_wistron_bios(void __iomem *base)
97 static unsigned char __initdata signature[] =
98 { 0x42, 0x21, 0x55, 0x30 };
101 for (offset = 0; offset < 0x10000; offset += 0x10) {
102 if (check_signature(base + offset, signature,
103 sizeof(signature)) != 0)
109 static int __init map_bios(void)
115 base = ioremap(0xF0000, 0x10000); /* Can't fail */
116 offset = locate_wistron_bios(base);
118 printk(KERN_ERR "wistron_btns: BIOS entry point not found\n");
123 entry_point = readl(base + offset + 5);
125 "wistron_btns: BIOS signature found at %p, entry point %08X\n",
126 base + offset, entry_point);
128 if (entry_point >= 0xF0000) {
129 bios_code_map_base = base;
130 bios_entry_point = bios_code_map_base + (entry_point & 0xFFFF);
133 bios_code_map_base = ioremap(entry_point & ~0x3FFF, 0x4000);
134 if (bios_code_map_base == NULL) {
136 "wistron_btns: Can't map BIOS code at %08X\n",
137 entry_point & ~0x3FFF);
140 bios_entry_point = bios_code_map_base + (entry_point & 0x3FFF);
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");
151 iounmap(bios_code_map_base);
156 static inline void unmap_bios(void)
158 iounmap(bios_code_map_base);
159 iounmap(bios_data_map_base);
164 static u16 bios_pop_queue(void)
168 memset(®s, 0, sizeof (regs));
177 static void __devinit bios_attach(void)
181 memset(®s, 0, sizeof (regs));
187 static void bios_detach(void)
191 memset(®s, 0, sizeof (regs));
197 static u8 __devinit bios_get_cmos_address(void)
201 memset(®s, 0, sizeof (regs));
209 static u16 __devinit bios_get_default_setting(u8 subsys)
213 memset(®s, 0, sizeof (regs));
215 regs.ebx = 0x0200 | subsys;
221 static void bios_set_state(u8 subsys, int enable)
225 memset(®s, 0, sizeof (regs));
227 regs.ebx = (enable ? 0x0100 : 0x0000) | subsys;
231 /* Hardware database */
234 char type; /* See KE_* below */
236 unsigned keycode; /* For KE_KEY */
239 enum { KE_END, KE_KEY, KE_WIFI, KE_BLUETOOTH };
241 static const struct key_entry *keymap; /* = NULL; Current key map */
242 static int have_wifi;
243 static int have_bluetooth;
245 static int __init dmi_matched(struct dmi_system_id *dmi)
247 const struct key_entry *key;
249 keymap = dmi->driver_data;
250 for (key = keymap; key->type != KE_END; key++) {
251 if (key->type == KE_WIFI) {
254 } else if (key->type == KE_BLUETOOTH) {
262 static struct key_entry keymap_empty[] = {
266 static struct key_entry keymap_fs_amilo_pro_v2000[] = {
267 { KE_KEY, 0x01, KEY_HELP },
268 { KE_KEY, 0x11, KEY_PROG1 },
269 { KE_KEY, 0x12, KEY_PROG2 },
270 { KE_WIFI, 0x30, 0 },
271 { KE_KEY, 0x31, KEY_MAIL },
272 { KE_KEY, 0x36, KEY_WWW },
276 static struct key_entry keymap_fujitsu_n3510[] = {
277 { KE_KEY, 0x11, KEY_PROG1 },
278 { KE_KEY, 0x12, KEY_PROG2 },
279 { KE_KEY, 0x36, KEY_WWW },
280 { KE_KEY, 0x31, KEY_MAIL },
281 { KE_KEY, 0x71, KEY_STOPCD },
282 { KE_KEY, 0x72, KEY_PLAYPAUSE },
283 { KE_KEY, 0x74, KEY_REWIND },
284 { KE_KEY, 0x78, KEY_FORWARD },
288 static struct key_entry keymap_wistron_ms2111[] = {
289 { KE_KEY, 0x11, KEY_PROG1 },
290 { KE_KEY, 0x12, KEY_PROG2 },
291 { KE_KEY, 0x13, KEY_PROG3 },
292 { KE_KEY, 0x31, KEY_MAIL },
293 { KE_KEY, 0x36, KEY_WWW },
297 static struct key_entry keymap_wistron_ms2141[] = {
298 { KE_KEY, 0x11, KEY_PROG1 },
299 { KE_KEY, 0x12, KEY_PROG2 },
300 { KE_WIFI, 0x30, 0 },
301 { KE_KEY, 0x22, KEY_REWIND },
302 { KE_KEY, 0x23, KEY_FORWARD },
303 { KE_KEY, 0x24, KEY_PLAYPAUSE },
304 { KE_KEY, 0x25, KEY_STOPCD },
305 { KE_KEY, 0x31, KEY_MAIL },
306 { KE_KEY, 0x36, KEY_WWW },
310 static struct key_entry keymap_acer_aspire_1500[] = {
311 { KE_KEY, 0x11, KEY_PROG1 },
312 { KE_KEY, 0x12, KEY_PROG2 },
313 { KE_WIFI, 0x30, 0 },
314 { KE_KEY, 0x31, KEY_MAIL },
315 { KE_KEY, 0x36, KEY_WWW },
316 { KE_BLUETOOTH, 0x44, 0 },
320 static struct key_entry keymap_acer_travelmate_240[] = {
321 { KE_KEY, 0x31, KEY_MAIL },
322 { KE_KEY, 0x36, KEY_WWW },
323 { KE_KEY, 0x11, KEY_PROG1 },
324 { KE_KEY, 0x12, KEY_PROG2 },
325 { KE_BLUETOOTH, 0x44, 0 },
326 { KE_WIFI, 0x30, 0 },
330 static struct key_entry keymap_aopen_1559as[] = {
331 { KE_KEY, 0x01, KEY_HELP },
332 { KE_KEY, 0x06, KEY_PROG3 },
333 { KE_KEY, 0x11, KEY_PROG1 },
334 { KE_KEY, 0x12, KEY_PROG2 },
335 { KE_WIFI, 0x30, 0 },
336 { KE_KEY, 0x31, KEY_MAIL },
337 { KE_KEY, 0x36, KEY_WWW },
342 * If your machine is not here (which is currently rather likely), please send
343 * a list of buttons and their key codes (reported when loading this module
344 * with force=1) and the output of dmidecode to $MODULE_AUTHOR.
346 static struct dmi_system_id dmi_ids[] __initdata = {
348 .callback = dmi_matched,
349 .ident = "Fujitsu-Siemens Amilo Pro V2000",
351 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
352 DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Pro V2000"),
354 .driver_data = keymap_fs_amilo_pro_v2000
357 .callback = dmi_matched,
358 .ident = "Fujitsu-Siemens Amilo M7400",
360 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
361 DMI_MATCH(DMI_PRODUCT_NAME, "AMILO M "),
363 .driver_data = keymap_fs_amilo_pro_v2000
366 .callback = dmi_matched,
367 .ident = "Fujitsu N3510",
369 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
370 DMI_MATCH(DMI_PRODUCT_NAME, "N3510"),
372 .driver_data = keymap_fujitsu_n3510
375 .callback = dmi_matched,
376 .ident = "Acer Aspire 1500",
378 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
379 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 1500"),
381 .driver_data = keymap_acer_aspire_1500
384 .callback = dmi_matched,
385 .ident = "Acer TravelMate 240",
387 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
388 DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 240"),
390 .driver_data = keymap_acer_travelmate_240
393 .callback = dmi_matched,
394 .ident = "AOpen 1559AS",
396 DMI_MATCH(DMI_PRODUCT_NAME, "E2U"),
397 DMI_MATCH(DMI_BOARD_NAME, "E2U"),
399 .driver_data = keymap_aopen_1559as
402 .callback = dmi_matched,
403 .ident = "Medion MD 9783",
405 DMI_MATCH(DMI_SYS_VENDOR, "MEDIONNB"),
406 DMI_MATCH(DMI_PRODUCT_NAME, "MD 9783"),
408 .driver_data = keymap_wistron_ms2111
413 static int __init select_keymap(void)
415 if (keymap_name != NULL) {
416 if (strcmp (keymap_name, "1557/MS2141") == 0)
417 keymap = keymap_wistron_ms2141;
419 printk(KERN_ERR "wistron_btns: Keymap unknown\n");
423 dmi_check_system(dmi_ids);
424 if (keymap == NULL) {
426 printk(KERN_ERR "wistron_btns: System unknown\n");
429 keymap = keymap_empty;
434 /* Input layer interface */
436 static struct input_dev *input_dev;
438 static int __devinit setup_input_dev(void)
440 const struct key_entry *key;
443 input_dev = input_allocate_device();
447 input_dev->name = "Wistron laptop buttons";
448 input_dev->phys = "wistron/input0";
449 input_dev->id.bustype = BUS_HOST;
450 input_dev->cdev.dev = &wistron_device->dev;
452 for (key = keymap; key->type != KE_END; key++) {
453 if (key->type == KE_KEY) {
454 input_dev->evbit[LONG(EV_KEY)] = BIT(EV_KEY);
455 set_bit(key->keycode, input_dev->keybit);
459 error = input_register_device(input_dev);
461 input_free_device(input_dev);
468 static void report_key(unsigned keycode)
470 input_report_key(input_dev, keycode, 1);
471 input_sync(input_dev);
472 input_report_key(input_dev, keycode, 0);
473 input_sync(input_dev);
478 static int wifi_enabled;
479 static int bluetooth_enabled;
481 static void poll_bios(unsigned long);
483 static struct timer_list poll_timer = TIMER_INITIALIZER(poll_bios, 0, 0);
485 static void handle_key(u8 code)
487 const struct key_entry *key;
489 for (key = keymap; key->type != KE_END; key++) {
490 if (code == key->code) {
493 report_key(key->keycode);
498 wifi_enabled = !wifi_enabled;
499 bios_set_state(WIFI, wifi_enabled);
504 if (have_bluetooth) {
505 bluetooth_enabled = !bluetooth_enabled;
506 bios_set_state(BLUETOOTH, bluetooth_enabled);
517 printk(KERN_NOTICE "wistron_btns: Unknown key code %02X\n", code);
520 static void poll_bios(unsigned long discard)
526 qlen = CMOS_READ(cmos_address);
529 val = bios_pop_queue();
530 if (val != 0 && !discard)
534 mod_timer(&poll_timer, jiffies + HZ / POLL_FREQUENCY);
537 static int __devinit wistron_probe(struct platform_device *dev)
539 int err = setup_input_dev();
544 cmos_address = bios_get_cmos_address();
547 u16 wifi = bios_get_default_setting(WIFI);
549 wifi_enabled = (wifi & 2) ? 1 : 0;
554 bios_set_state(WIFI, wifi_enabled);
557 if (have_bluetooth) {
558 u16 bt = bios_get_default_setting(BLUETOOTH);
560 bluetooth_enabled = (bt & 2) ? 1 : 0;
565 bios_set_state(BLUETOOTH, bluetooth_enabled);
568 poll_bios(1); /* Flush stale event queue and arm timer */
573 static int __devexit wistron_remove(struct platform_device *dev)
575 del_timer_sync(&poll_timer);
576 input_unregister_device(input_dev);
583 static int wistron_suspend(struct platform_device *dev, pm_message_t state)
585 del_timer_sync(&poll_timer);
588 bios_set_state(WIFI, 0);
591 bios_set_state(BLUETOOTH, 0);
596 static int wistron_resume(struct platform_device *dev)
599 bios_set_state(WIFI, wifi_enabled);
602 bios_set_state(BLUETOOTH, bluetooth_enabled);
609 #define wistron_suspend NULL
610 #define wistron_resume NULL
613 static struct platform_driver wistron_driver = {
615 .name = "wistron-bios",
616 .owner = THIS_MODULE,
618 .probe = wistron_probe,
619 .remove = __devexit_p(wistron_remove),
620 .suspend = wistron_suspend,
621 .resume = wistron_resume,
624 static int __init wb_module_init(void)
628 err = select_keymap();
636 err = platform_driver_register(&wistron_driver);
640 wistron_device = platform_device_alloc("wistron-bios", -1);
641 if (!wistron_device) {
643 goto err_unregister_driver;
646 err = platform_device_add(wistron_device);
648 goto err_free_device;
653 platform_device_put(wistron_device);
654 err_unregister_driver:
655 platform_driver_unregister(&wistron_driver);
662 static void __exit wb_module_exit(void)
664 platform_device_unregister(wistron_device);
665 platform_driver_unregister(&wistron_driver);
669 module_init(wb_module_init);
670 module_exit(wb_module_exit);