blob: 6d7e865f9007dd2045d07485b99143cd2f6db586 [file] [log] [blame]
Dmitry Torokhov5fc14682005-11-20 00:50:06 -05001/*
2 * Wistron laptop button driver
3 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -05004 * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
Dmitry Torokhov5fc14682005-11-20 00:50:06 -05005 *
6 * You can redistribute and/or modify this program under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 * Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place Suite 330, Boston, MA 02111-1307, USA.
18 */
19#include <asm/io.h>
20#include <linux/dmi.h>
21#include <linux/init.h>
22#include <linux/input.h>
23#include <linux/interrupt.h>
24#include <linux/kernel.h>
25#include <linux/mc146818rtc.h>
26#include <linux/module.h>
27#include <linux/preempt.h>
28#include <linux/string.h>
29#include <linux/timer.h>
30#include <linux/types.h>
31
32/*
33 * Number of attempts to read data from queue per poll;
34 * the queue can hold up to 31 entries
35 */
36#define MAX_POLL_ITERATIONS 64
37
38#define POLL_FREQUENCY 10 /* Number of polls per second */
39
40#if POLL_FREQUENCY > HZ
41#error "POLL_FREQUENCY too high"
42#endif
43
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -050044/* BIOS subsystem IDs */
45#define WIFI 0x35
46#define BLUETOOTH 0x34
47
Dmitry Torokhov5fc14682005-11-20 00:50:06 -050048MODULE_AUTHOR("Miloslav Trmac <mitr@volny.cz>");
49MODULE_DESCRIPTION("Wistron laptop button driver");
50MODULE_LICENSE("GPL v2");
51MODULE_VERSION("0.1");
52
53static int force; /* = 0; */
54module_param(force, bool, 0);
55MODULE_PARM_DESC(force, "Load even if computer is not in database");
56
57static char *keymap_name; /* = NULL; */
58module_param_named(keymap, keymap_name, charp, 0);
59MODULE_PARM_DESC(keymap, "Keymap name, if it can't be autodetected");
60
61 /* BIOS interface implementation */
62
63static void __iomem *bios_entry_point; /* BIOS routine entry point */
64static void __iomem *bios_code_map_base;
65static void __iomem *bios_data_map_base;
66
67static u8 cmos_address;
68
69struct regs {
70 u32 eax, ebx, ecx;
71};
72
73static void call_bios(struct regs *regs)
74{
75 unsigned long flags;
76
77 preempt_disable();
78 local_irq_save(flags);
79 asm volatile ("pushl %%ebp;"
80 "movl %7, %%ebp;"
81 "call *%6;"
82 "popl %%ebp"
83 : "=a" (regs->eax), "=b" (regs->ebx), "=c" (regs->ecx)
84 : "0" (regs->eax), "1" (regs->ebx), "2" (regs->ecx),
85 "m" (bios_entry_point), "m" (bios_data_map_base)
86 : "edx", "edi", "esi", "memory");
87 local_irq_restore(flags);
88 preempt_enable();
89}
90
91static size_t __init locate_wistron_bios(void __iomem *base)
92{
93 static const unsigned char __initdata signature[] =
94 { 0x42, 0x21, 0x55, 0x30 };
95 size_t offset;
96
97 for (offset = 0; offset < 0x10000; offset += 0x10) {
98 if (check_signature(base + offset, signature,
99 sizeof(signature)) != 0)
100 return offset;
101 }
102 return -1;
103}
104
105static int __init map_bios(void)
106{
107 void __iomem *base;
108 size_t offset;
109 u32 entry_point;
110
111 base = ioremap(0xF0000, 0x10000); /* Can't fail */
112 offset = locate_wistron_bios(base);
113 if (offset < 0) {
114 printk(KERN_ERR "wistron_btns: BIOS entry point not found\n");
115 iounmap(base);
116 return -ENODEV;
117 }
118
119 entry_point = readl(base + offset + 5);
120 printk(KERN_DEBUG
121 "wistron_btns: BIOS signature found at %p, entry point %08X\n",
122 base + offset, entry_point);
123
124 if (entry_point >= 0xF0000) {
125 bios_code_map_base = base;
126 bios_entry_point = bios_code_map_base + (entry_point & 0xFFFF);
127 } else {
128 iounmap(base);
129 bios_code_map_base = ioremap(entry_point & ~0x3FFF, 0x4000);
130 if (bios_code_map_base == NULL) {
131 printk(KERN_ERR
132 "wistron_btns: Can't map BIOS code at %08X\n",
133 entry_point & ~0x3FFF);
134 goto err;
135 }
136 bios_entry_point = bios_code_map_base + (entry_point & 0x3FFF);
137 }
138 /* The Windows driver maps 0x10000 bytes, we keep only one page... */
139 bios_data_map_base = ioremap(0x400, 0xc00);
140 if (bios_data_map_base == NULL) {
141 printk(KERN_ERR "wistron_btns: Can't map BIOS data\n");
142 goto err_code;
143 }
144 return 0;
145
146err_code:
147 iounmap(bios_code_map_base);
148err:
149 return -ENOMEM;
150}
151
152static void __exit unmap_bios(void)
153{
154 iounmap(bios_code_map_base);
155 iounmap(bios_data_map_base);
156}
157
158 /* BIOS calls */
159
160static u16 bios_pop_queue(void)
161{
162 struct regs regs;
163
164 memset(&regs, 0, sizeof (regs));
165 regs.eax = 0x9610;
166 regs.ebx = 0x061C;
167 regs.ecx = 0x0000;
168 call_bios(&regs);
169
170 return regs.eax;
171}
172
173static void __init bios_attach(void)
174{
175 struct regs regs;
176
177 memset(&regs, 0, sizeof (regs));
178 regs.eax = 0x9610;
179 regs.ebx = 0x012E;
180 call_bios(&regs);
181}
182
183static void __exit bios_detach(void)
184{
185 struct regs regs;
186
187 memset(&regs, 0, sizeof (regs));
188 regs.eax = 0x9610;
189 regs.ebx = 0x002E;
190 call_bios(&regs);
191}
192
193static u8 __init bios_get_cmos_address(void)
194{
195 struct regs regs;
196
197 memset(&regs, 0, sizeof (regs));
198 regs.eax = 0x9610;
199 regs.ebx = 0x051C;
200 call_bios(&regs);
201
202 return regs.ecx;
203}
204
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500205static u16 __init bios_get_default_setting(u8 subsys)
Dmitry Torokhov5fc14682005-11-20 00:50:06 -0500206{
207 struct regs regs;
208
209 memset(&regs, 0, sizeof (regs));
210 regs.eax = 0x9610;
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500211 regs.ebx = 0x0200 | subsys;
Dmitry Torokhov5fc14682005-11-20 00:50:06 -0500212 call_bios(&regs);
213
214 return regs.eax;
215}
216
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500217static void bios_set_state(u8 subsys, int enable)
Dmitry Torokhov5fc14682005-11-20 00:50:06 -0500218{
219 struct regs regs;
220
221 memset(&regs, 0, sizeof (regs));
222 regs.eax = 0x9610;
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500223 regs.ebx = (enable ? 0x0100 : 0x0000) | subsys;
Dmitry Torokhov5fc14682005-11-20 00:50:06 -0500224 call_bios(&regs);
225}
226
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500227/* Hardware database */
Dmitry Torokhov5fc14682005-11-20 00:50:06 -0500228
229struct key_entry {
230 char type; /* See KE_* below */
231 u8 code;
232 unsigned keycode; /* For KE_KEY */
233};
234
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500235enum { KE_END, KE_KEY, KE_WIFI, KE_BLUETOOTH };
Dmitry Torokhov5fc14682005-11-20 00:50:06 -0500236
237static const struct key_entry *keymap; /* = NULL; Current key map */
238static int have_wifi;
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500239static int have_bluetooth;
Dmitry Torokhov5fc14682005-11-20 00:50:06 -0500240
241static int __init dmi_matched(struct dmi_system_id *dmi)
242{
243 const struct key_entry *key;
244
245 keymap = dmi->driver_data;
246 for (key = keymap; key->type != KE_END; key++) {
247 if (key->type == KE_WIFI) {
248 have_wifi = 1;
249 break;
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500250 } else if (key->type == KE_BLUETOOTH) {
251 have_bluetooth = 1;
252 break;
Dmitry Torokhov5fc14682005-11-20 00:50:06 -0500253 }
254 }
255 return 1;
256}
257
258static struct key_entry keymap_empty[] = {
259 { KE_END, 0 }
260};
261
262static struct key_entry keymap_fs_amilo_pro_v2000[] = {
263 { KE_KEY, 0x01, KEY_HELP },
264 { KE_KEY, 0x11, KEY_PROG1 },
265 { KE_KEY, 0x12, KEY_PROG2 },
266 { KE_WIFI, 0x30, 0 },
267 { KE_KEY, 0x31, KEY_MAIL },
268 { KE_KEY, 0x36, KEY_WWW },
269 { KE_END, 0 }
270};
271
272static struct key_entry keymap_wistron_ms2141[] = {
273 { KE_KEY, 0x11, KEY_PROG1 },
274 { KE_KEY, 0x12, KEY_PROG2 },
275 { KE_WIFI, 0x30, 0 },
276 { KE_KEY, 0x22, KEY_REWIND },
277 { KE_KEY, 0x23, KEY_FORWARD },
278 { KE_KEY, 0x24, KEY_PLAYPAUSE },
279 { KE_KEY, 0x25, KEY_STOPCD },
280 { KE_KEY, 0x31, KEY_MAIL },
281 { KE_KEY, 0x36, KEY_WWW },
282 { KE_END, 0 }
283};
284
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500285static struct key_entry keymap_acer_aspire_1500[] = {
286 { KE_KEY, 0x11, KEY_PROG1 },
287 { KE_KEY, 0x12, KEY_PROG2 },
288 { KE_WIFI, 0x30, 0 },
289 { KE_KEY, 0x31, KEY_MAIL },
290 { KE_KEY, 0x36, KEY_WWW },
291 { KE_BLUETOOTH, 0x44, 0 },
292 { KE_END, 0 }
293};
294
Dmitry Torokhov5fc14682005-11-20 00:50:06 -0500295/*
296 * If your machine is not here (which is currently rather likely), please send
297 * a list of buttons and their key codes (reported when loading this module
298 * with force=1) and the output of dmidecode to $MODULE_AUTHOR.
299 */
300static struct dmi_system_id dmi_ids[] = {
301 {
302 .callback = dmi_matched,
303 .ident = "Fujitsu-Siemens Amilo Pro V2000",
304 .matches = {
305 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
306 DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Pro V2000"),
307 },
308 .driver_data = keymap_fs_amilo_pro_v2000
309 },
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500310 {
311 .callback = dmi_matched,
312 .ident = "Acer Aspire 1500",
313 .matches = {
314 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
315 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 1500"),
316 },
317 .driver_data = keymap_acer_aspire_1500
318 },
Dmitry Torokhov5fc14682005-11-20 00:50:06 -0500319 { 0, }
320};
321
322static int __init select_keymap(void)
323{
324 if (keymap_name != NULL) {
325 if (strcmp (keymap_name, "1557/MS2141") == 0)
326 keymap = keymap_wistron_ms2141;
327 else {
328 printk(KERN_ERR "wistron_btns: Keymap unknown\n");
329 return -EINVAL;
330 }
331 }
332 dmi_check_system(dmi_ids);
333 if (keymap == NULL) {
334 if (!force) {
335 printk(KERN_ERR "wistron_btns: System unknown\n");
336 return -ENODEV;
337 }
338 keymap = keymap_empty;
339 }
340 return 0;
341}
342
343 /* Input layer interface */
344
345static struct input_dev input_dev = {
346 .name = "Wistron laptop buttons",
347};
348
349static void __init setup_input_dev(void)
350{
351 const struct key_entry *key;
352
353 for (key = keymap; key->type != KE_END; key++) {
354 if (key->type == KE_KEY) {
355 input_dev.evbit[LONG(EV_KEY)] = BIT(EV_KEY);
356 input_dev.keybit[LONG(key->keycode)]
357 |= BIT(key->keycode);
358 }
359 }
360
361 input_register_device(&input_dev);
362}
363
364static void report_key(unsigned keycode)
365{
366 input_report_key(&input_dev, keycode, 1);
367 input_sync(&input_dev);
368 input_report_key(&input_dev, keycode, 0);
369 input_sync(&input_dev);
370}
371
372 /* Driver core */
373
374static int wifi_enabled;
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500375static int bluetooth_enabled;
Dmitry Torokhov5fc14682005-11-20 00:50:06 -0500376
377static void poll_bios(unsigned long);
378
379static struct timer_list poll_timer = TIMER_INITIALIZER(poll_bios, 0, 0);
380
381static void handle_key(u8 code)
382{
383 const struct key_entry *key;
384
385 for (key = keymap; key->type != KE_END; key++) {
386 if (code == key->code) {
387 switch (key->type) {
388 case KE_KEY:
389 report_key(key->keycode);
390 break;
391
392 case KE_WIFI:
393 if (have_wifi) {
394 wifi_enabled = !wifi_enabled;
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500395 bios_set_state(WIFI, wifi_enabled);
396 }
397 break;
398
399 case KE_BLUETOOTH:
400 if (have_bluetooth) {
401 bluetooth_enabled = !bluetooth_enabled;
402 bios_set_state(BLUETOOTH, bluetooth_enabled);
Dmitry Torokhov5fc14682005-11-20 00:50:06 -0500403 }
404 break;
405
406 case KE_END:
407 default:
408 BUG();
409 }
410 return;
411 }
412 }
413 printk(KERN_NOTICE "wistron_btns: Unknown key code %02X\n", code);
414}
415
416static void poll_bios(unsigned long discard)
417{
418 u8 qlen;
419 u16 val;
420
421 for (;;) {
422 qlen = CMOS_READ(cmos_address);
423 if (qlen == 0)
424 break;
425 val = bios_pop_queue();
426 if (val != 0 && !discard)
427 handle_key((u8)val);
428 }
429
430 mod_timer(&poll_timer, jiffies + HZ / POLL_FREQUENCY);
431}
432
433static int __init wb_module_init(void)
434{
435 int err;
436
437 err = select_keymap();
438 if (err)
439 return err;
440 err = map_bios();
441 if (err)
442 return err;
443 bios_attach();
444 cmos_address = bios_get_cmos_address();
445 if (have_wifi) {
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500446 u16 wifi = bios_get_default_setting(WIFI);
447 if (wifi & 1)
448 wifi_enabled = (wifi & 2) ? 1 : 0;
449 else
Dmitry Torokhov5fc14682005-11-20 00:50:06 -0500450 have_wifi = 0;
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500451
Dmitry Torokhov5fc14682005-11-20 00:50:06 -0500452 if (have_wifi)
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500453 bios_set_state(WIFI, wifi_enabled);
454 }
455 if (have_bluetooth) {
456 u16 bt = bios_get_default_setting(BLUETOOTH);
457 if (bt & 1)
458 bluetooth_enabled = (bt & 2) ? 1 : 0;
459 else
460 have_bluetooth = 0;
461
462 if (have_bluetooth)
463 bios_set_state(BLUETOOTH, bluetooth_enabled);
Dmitry Torokhov5fc14682005-11-20 00:50:06 -0500464 }
465
466 setup_input_dev();
467
468 poll_bios(1); /* Flush stale event queue and arm timer */
469
470 return 0;
471}
472
473static void __exit wb_module_exit(void)
474{
475 del_timer_sync(&poll_timer);
476 input_unregister_device(&input_dev);
477 bios_detach();
478 unmap_bios();
479}
480
481module_init(wb_module_init);
482module_exit(wb_module_exit);