2 * Wistron laptop button driver
3 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
5 * You can redistribute and/or modify this program under the terms of the
6 * GNU General Public License version 2 as published by the Free Software
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
12 * Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place Suite 330, Boston, MA 02111-1307, USA.
19 #include <linux/dmi.h>
20 #include <linux/init.h>
21 #include <linux/input.h>
22 #include <linux/interrupt.h>
23 #include <linux/kernel.h>
24 #include <linux/mc146818rtc.h>
25 #include <linux/module.h>
26 #include <linux/preempt.h>
27 #include <linux/string.h>
28 #include <linux/timer.h>
29 #include <linux/types.h>
32 * Number of attempts to read data from queue per poll;
33 * the queue can hold up to 31 entries
35 #define MAX_POLL_ITERATIONS 64
37 #define POLL_FREQUENCY 10 /* Number of polls per second */
39 #if POLL_FREQUENCY > HZ
40 #error "POLL_FREQUENCY too high"
43 MODULE_AUTHOR("Miloslav Trmac <mitr@volny.cz>");
44 MODULE_DESCRIPTION("Wistron laptop button driver");
45 MODULE_LICENSE("GPL v2");
46 MODULE_VERSION("0.1");
48 static int force; /* = 0; */
49 module_param(force, bool, 0);
50 MODULE_PARM_DESC(force, "Load even if computer is not in database");
52 static char *keymap_name; /* = NULL; */
53 module_param_named(keymap, keymap_name, charp, 0);
54 MODULE_PARM_DESC(keymap, "Keymap name, if it can't be autodetected");
56 /* BIOS interface implementation */
58 static void __iomem *bios_entry_point; /* BIOS routine entry point */
59 static void __iomem *bios_code_map_base;
60 static void __iomem *bios_data_map_base;
62 static u8 cmos_address;
68 static void call_bios(struct regs *regs)
73 local_irq_save(flags);
74 asm volatile ("pushl %%ebp;"
78 : "=a" (regs->eax), "=b" (regs->ebx), "=c" (regs->ecx)
79 : "0" (regs->eax), "1" (regs->ebx), "2" (regs->ecx),
80 "m" (bios_entry_point), "m" (bios_data_map_base)
81 : "edx", "edi", "esi", "memory");
82 local_irq_restore(flags);
86 static size_t __init locate_wistron_bios(void __iomem *base)
88 static const unsigned char __initdata signature[] =
89 { 0x42, 0x21, 0x55, 0x30 };
92 for (offset = 0; offset < 0x10000; offset += 0x10) {
93 if (check_signature(base + offset, signature,
94 sizeof(signature)) != 0)
100 static int __init map_bios(void)
106 base = ioremap(0xF0000, 0x10000); /* Can't fail */
107 offset = locate_wistron_bios(base);
109 printk(KERN_ERR "wistron_btns: BIOS entry point not found\n");
114 entry_point = readl(base + offset + 5);
116 "wistron_btns: BIOS signature found at %p, entry point %08X\n",
117 base + offset, entry_point);
119 if (entry_point >= 0xF0000) {
120 bios_code_map_base = base;
121 bios_entry_point = bios_code_map_base + (entry_point & 0xFFFF);
124 bios_code_map_base = ioremap(entry_point & ~0x3FFF, 0x4000);
125 if (bios_code_map_base == NULL) {
127 "wistron_btns: Can't map BIOS code at %08X\n",
128 entry_point & ~0x3FFF);
131 bios_entry_point = bios_code_map_base + (entry_point & 0x3FFF);
133 /* The Windows driver maps 0x10000 bytes, we keep only one page... */
134 bios_data_map_base = ioremap(0x400, 0xc00);
135 if (bios_data_map_base == NULL) {
136 printk(KERN_ERR "wistron_btns: Can't map BIOS data\n");
142 iounmap(bios_code_map_base);
147 static void __exit unmap_bios(void)
149 iounmap(bios_code_map_base);
150 iounmap(bios_data_map_base);
155 static u16 bios_pop_queue(void)
159 memset(®s, 0, sizeof (regs));
168 static void __init bios_attach(void)
172 memset(®s, 0, sizeof (regs));
178 static void __exit bios_detach(void)
182 memset(®s, 0, sizeof (regs));
188 static u8 __init bios_get_cmos_address(void)
192 memset(®s, 0, sizeof (regs));
200 static u16 __init bios_wifi_get_default_setting(void)
204 memset(®s, 0, sizeof (regs));
212 static void bios_wifi_set_state(int enable)
216 memset(®s, 0, sizeof (regs));
218 regs.ebx = enable ? 0x0135 : 0x0035;
222 /* Hardware database */
225 char type; /* See KE_* below */
227 unsigned keycode; /* For KE_KEY */
230 enum { KE_END, KE_KEY, KE_WIFI };
232 static const struct key_entry *keymap; /* = NULL; Current key map */
233 static int have_wifi;
235 static int __init dmi_matched(struct dmi_system_id *dmi)
237 const struct key_entry *key;
239 keymap = dmi->driver_data;
240 for (key = keymap; key->type != KE_END; key++) {
241 if (key->type == KE_WIFI) {
249 static struct key_entry keymap_empty[] = {
253 static struct key_entry keymap_fs_amilo_pro_v2000[] = {
254 { KE_KEY, 0x01, KEY_HELP },
255 { KE_KEY, 0x11, KEY_PROG1 },
256 { KE_KEY, 0x12, KEY_PROG2 },
257 { KE_WIFI, 0x30, 0 },
258 { KE_KEY, 0x31, KEY_MAIL },
259 { KE_KEY, 0x36, KEY_WWW },
263 static struct key_entry keymap_wistron_ms2141[] = {
264 { KE_KEY, 0x11, KEY_PROG1 },
265 { KE_KEY, 0x12, KEY_PROG2 },
266 { KE_WIFI, 0x30, 0 },
267 { KE_KEY, 0x22, KEY_REWIND },
268 { KE_KEY, 0x23, KEY_FORWARD },
269 { KE_KEY, 0x24, KEY_PLAYPAUSE },
270 { KE_KEY, 0x25, KEY_STOPCD },
271 { KE_KEY, 0x31, KEY_MAIL },
272 { KE_KEY, 0x36, KEY_WWW },
277 * If your machine is not here (which is currently rather likely), please send
278 * a list of buttons and their key codes (reported when loading this module
279 * with force=1) and the output of dmidecode to $MODULE_AUTHOR.
281 static struct dmi_system_id dmi_ids[] = {
283 .callback = dmi_matched,
284 .ident = "Fujitsu-Siemens Amilo Pro V2000",
286 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
287 DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Pro V2000"),
289 .driver_data = keymap_fs_amilo_pro_v2000
294 static int __init select_keymap(void)
296 if (keymap_name != NULL) {
297 if (strcmp (keymap_name, "1557/MS2141") == 0)
298 keymap = keymap_wistron_ms2141;
300 printk(KERN_ERR "wistron_btns: Keymap unknown\n");
304 dmi_check_system(dmi_ids);
305 if (keymap == NULL) {
307 printk(KERN_ERR "wistron_btns: System unknown\n");
310 keymap = keymap_empty;
315 /* Input layer interface */
317 static struct input_dev input_dev = {
318 .name = "Wistron laptop buttons",
321 static void __init setup_input_dev(void)
323 const struct key_entry *key;
325 for (key = keymap; key->type != KE_END; key++) {
326 if (key->type == KE_KEY) {
327 input_dev.evbit[LONG(EV_KEY)] = BIT(EV_KEY);
328 input_dev.keybit[LONG(key->keycode)]
329 |= BIT(key->keycode);
333 input_register_device(&input_dev);
336 static void report_key(unsigned keycode)
338 input_report_key(&input_dev, keycode, 1);
339 input_sync(&input_dev);
340 input_report_key(&input_dev, keycode, 0);
341 input_sync(&input_dev);
346 static int wifi_enabled;
348 static void poll_bios(unsigned long);
350 static struct timer_list poll_timer = TIMER_INITIALIZER(poll_bios, 0, 0);
352 static void handle_key(u8 code)
354 const struct key_entry *key;
356 for (key = keymap; key->type != KE_END; key++) {
357 if (code == key->code) {
360 report_key(key->keycode);
365 wifi_enabled = !wifi_enabled;
366 bios_wifi_set_state(wifi_enabled);
377 printk(KERN_NOTICE "wistron_btns: Unknown key code %02X\n", code);
380 static void poll_bios(unsigned long discard)
386 qlen = CMOS_READ(cmos_address);
389 val = bios_pop_queue();
390 if (val != 0 && !discard)
394 mod_timer(&poll_timer, jiffies + HZ / POLL_FREQUENCY);
397 static int __init wb_module_init(void)
401 err = select_keymap();
408 cmos_address = bios_get_cmos_address();
410 switch (bios_wifi_get_default_setting()) {
424 bios_wifi_set_state(wifi_enabled);
429 poll_bios(1); /* Flush stale event queue and arm timer */
434 static void __exit wb_module_exit(void)
436 del_timer_sync(&poll_timer);
437 input_unregister_device(&input_dev);
442 module_init(wb_module_init);
443 module_exit(wb_module_exit);