Input: wistron - add acerhk laptop database
[pandora-kernel.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         union {
237                 u16 keycode;            /* For KE_KEY */
238                 struct {                /* For KE_SW */
239                         u8 code;
240                         u8 value;
241                 } sw;
242         };
243 };
244
245 enum { KE_END, KE_KEY, KE_SW, KE_WIFI, KE_BLUETOOTH };
246
247 #define FE_MAIL_LED 0x01
248 #define FE_WIFI_LED 0x02
249 #define FE_UNTESTED 0x80
250
251 static const struct key_entry *keymap; /* = NULL; Current key map */
252 static int have_wifi;
253 static int have_bluetooth;
254
255 static int __init dmi_matched(struct dmi_system_id *dmi)
256 {
257         const struct key_entry *key;
258
259         keymap = dmi->driver_data;
260         for (key = keymap; key->type != KE_END; key++) {
261                 if (key->type == KE_WIFI)
262                         have_wifi = 1;
263                 else if (key->type == KE_BLUETOOTH)
264                         have_bluetooth = 1;
265         }
266         return 1;
267 }
268
269 static struct key_entry keymap_empty[] = {
270         { KE_END, 0 }
271 };
272
273 static struct key_entry keymap_fs_amilo_pro_v2000[] = {
274         { KE_KEY,  0x01, {KEY_HELP} },
275         { KE_KEY,  0x11, {KEY_PROG1} },
276         { KE_KEY,  0x12, {KEY_PROG2} },
277         { KE_WIFI, 0x30 },
278         { KE_KEY,  0x31, {KEY_MAIL} },
279         { KE_KEY,  0x36, {KEY_WWW} },
280         { KE_END,  0 }
281 };
282
283 static struct key_entry keymap_fujitsu_n3510[] = {
284         { KE_KEY, 0x11, {KEY_PROG1} },
285         { KE_KEY, 0x12, {KEY_PROG2} },
286         { KE_KEY, 0x36, {KEY_WWW} },
287         { KE_KEY, 0x31, {KEY_MAIL} },
288         { KE_KEY, 0x71, {KEY_STOPCD} },
289         { KE_KEY, 0x72, {KEY_PLAYPAUSE} },
290         { KE_KEY, 0x74, {KEY_REWIND} },
291         { KE_KEY, 0x78, {KEY_FORWARD} },
292         { KE_END, 0 }
293 };
294
295 static struct key_entry keymap_wistron_ms2111[] = {
296         { KE_KEY,  0x11, {KEY_PROG1} },
297         { KE_KEY,  0x12, {KEY_PROG2} },
298         { KE_KEY,  0x13, {KEY_PROG3} },
299         { KE_KEY,  0x31, {KEY_MAIL} },
300         { KE_KEY,  0x36, {KEY_WWW} },
301         { KE_END, FE_MAIL_LED }
302 };
303
304 static struct key_entry keymap_wistron_md40100[] = {
305         { KE_KEY, 0x01, {KEY_HELP} },
306         { KE_KEY, 0x02, {KEY_CONFIG} },
307         { KE_KEY, 0x31, {KEY_MAIL} },
308         { KE_KEY, 0x36, {KEY_WWW} },
309         { KE_KEY, 0x37, {KEY_DISPLAYTOGGLE} }, /* Display on/off */
310         { KE_END, FE_MAIL_LED | FE_WIFI_LED | FE_UNTESTED }
311 };
312
313 static struct key_entry keymap_wistron_ms2141[] = {
314         { KE_KEY,  0x11, {KEY_PROG1} },
315         { KE_KEY,  0x12, {KEY_PROG2} },
316         { KE_WIFI, 0x30 },
317         { KE_KEY,  0x22, {KEY_REWIND} },
318         { KE_KEY,  0x23, {KEY_FORWARD} },
319         { KE_KEY,  0x24, {KEY_PLAYPAUSE} },
320         { KE_KEY,  0x25, {KEY_STOPCD} },
321         { KE_KEY,  0x31, {KEY_MAIL} },
322         { KE_KEY,  0x36, {KEY_WWW} },
323         { KE_END,  0 }
324 };
325
326 static struct key_entry keymap_acer_aspire_1500[] = {
327         { KE_KEY, 0x01, {KEY_HELP} },
328         { KE_KEY, 0x03, {KEY_POWER} },
329         { KE_KEY, 0x11, {KEY_PROG1} },
330         { KE_KEY, 0x12, {KEY_PROG2} },
331         { KE_WIFI, 0x30 },
332         { KE_KEY, 0x31, {KEY_MAIL} },
333         { KE_KEY, 0x36, {KEY_WWW} },
334         { KE_KEY, 0x49, {KEY_CONFIG} },
335         { KE_BLUETOOTH, 0x44 },
336         { KE_END, FE_UNTESTED }
337 };
338
339 static struct key_entry keymap_acer_aspire_1600[] = {
340         { KE_KEY, 0x01, {KEY_HELP} },
341         { KE_KEY, 0x03, {KEY_POWER} },
342         { KE_KEY, 0x08, {KEY_MUTE} },
343         { KE_KEY, 0x11, {KEY_PROG1} },
344         { KE_KEY, 0x12, {KEY_PROG2} },
345         { KE_KEY, 0x13, {KEY_PROG3} },
346         { KE_KEY, 0x31, {KEY_MAIL} },
347         { KE_KEY, 0x36, {KEY_WWW} },
348         { KE_KEY, 0x49, {KEY_CONFIG} },
349         { KE_WIFI, 0x30 },
350         { KE_BLUETOOTH, 0x44 },
351         { KE_END, FE_MAIL_LED | FE_UNTESTED }
352 };
353
354 /* 3020 has been tested */
355 static struct key_entry keymap_acer_aspire_5020[] = {
356         { KE_KEY, 0x01, {KEY_HELP} },
357         { KE_KEY, 0x03, {KEY_POWER} },
358         { KE_KEY, 0x05, {KEY_SWITCHVIDEOMODE} }, /* Display selection */
359         { KE_KEY, 0x11, {KEY_PROG1} },
360         { KE_KEY, 0x12, {KEY_PROG2} },
361         { KE_KEY, 0x31, {KEY_MAIL} },
362         { KE_KEY, 0x36, {KEY_WWW} },
363         { KE_KEY, 0x6a, {KEY_CONFIG} },
364         { KE_WIFI, 0x30 },
365         { KE_BLUETOOTH, 0x44 },
366         { KE_END, FE_MAIL_LED | FE_UNTESTED }
367 };
368
369 static struct key_entry keymap_acer_travelmate_2410[] = {
370         { KE_KEY, 0x01, {KEY_HELP} },
371         { KE_KEY, 0x6d, {KEY_POWER} },
372         { KE_KEY, 0x11, {KEY_PROG1} },
373         { KE_KEY, 0x12, {KEY_PROG2} },
374         { KE_KEY, 0x31, {KEY_MAIL} },
375         { KE_KEY, 0x36, {KEY_WWW} },
376         { KE_KEY, 0x6a, {KEY_CONFIG} },
377         { KE_WIFI, 0x30 },
378         { KE_BLUETOOTH, 0x44 },
379         { KE_END, FE_MAIL_LED | FE_UNTESTED }
380 };
381
382 static struct key_entry keymap_acer_travelmate_110[] = {
383         { KE_KEY, 0x01, {KEY_HELP} },
384         { KE_KEY, 0x02, {KEY_CONFIG} },
385         { KE_KEY, 0x03, {KEY_POWER} },
386         { KE_KEY, 0x08, {KEY_MUTE} },
387         { KE_KEY, 0x11, {KEY_PROG1} },
388         { KE_KEY, 0x12, {KEY_PROG2} },
389         { KE_KEY, 0x20, {KEY_VOLUMEUP} },
390         { KE_KEY, 0x21, {KEY_VOLUMEDOWN} },
391         { KE_KEY, 0x31, {KEY_MAIL} },
392         { KE_KEY, 0x36, {KEY_WWW} },
393         { KE_SW, 0x4a, {.sw = {SW_LID, 1}} }, /* lid close */
394         { KE_SW, 0x4b, {.sw = {SW_LID, 0}} }, /* lid open */
395         { KE_WIFI, 0x30 },
396         { KE_END, FE_MAIL_LED | FE_UNTESTED }
397 };
398
399 static struct key_entry keymap_acer_travelmate_300[] = {
400         { KE_KEY, 0x01, {KEY_HELP} },
401         { KE_KEY, 0x02, {KEY_CONFIG} },
402         { KE_KEY, 0x03, {KEY_POWER} },
403         { KE_KEY, 0x08, {KEY_MUTE} },
404         { KE_KEY, 0x11, {KEY_PROG1} },
405         { KE_KEY, 0x12, {KEY_PROG2} },
406         { KE_KEY, 0x20, {KEY_VOLUMEUP} },
407         { KE_KEY, 0x21, {KEY_VOLUMEDOWN} },
408         { KE_KEY, 0x31, {KEY_MAIL} },
409         { KE_KEY, 0x36, {KEY_WWW} },
410         { KE_WIFI, 0x30 },
411         { KE_BLUETOOTH, 0x44 },
412         { KE_END, FE_MAIL_LED | FE_UNTESTED }
413 };
414
415 static struct key_entry keymap_acer_travelmate_380[] = {
416         { KE_KEY, 0x01, {KEY_HELP} },
417         { KE_KEY, 0x02, {KEY_CONFIG} },
418         { KE_KEY, 0x03, {KEY_POWER} }, /* not 370 */
419         { KE_KEY, 0x11, {KEY_PROG1} },
420         { KE_KEY, 0x12, {KEY_PROG2} },
421         { KE_KEY, 0x13, {KEY_PROG3} },
422         { KE_KEY, 0x31, {KEY_MAIL} },
423         { KE_KEY, 0x36, {KEY_WWW} },
424         { KE_WIFI, 0x30 },
425         { KE_END, FE_MAIL_LED | FE_UNTESTED }
426 };
427
428 /* unusual map */
429 static struct key_entry keymap_acer_travelmate_220[] = {
430         { KE_KEY, 0x01, {KEY_HELP} },
431         { KE_KEY, 0x02, {KEY_CONFIG} },
432         { KE_KEY, 0x11, {KEY_MAIL} },
433         { KE_KEY, 0x12, {KEY_WWW} },
434         { KE_KEY, 0x13, {KEY_PROG2} },
435         { KE_KEY, 0x31, {KEY_PROG1} },
436         { KE_END, FE_WIFI_LED | FE_UNTESTED }
437 };
438
439 static struct key_entry keymap_acer_travelmate_230[] = {
440         { KE_KEY, 0x01, {KEY_HELP} },
441         { KE_KEY, 0x02, {KEY_CONFIG} },
442         { KE_KEY, 0x11, {KEY_PROG1} },
443         { KE_KEY, 0x12, {KEY_PROG2} },
444         { KE_KEY, 0x31, {KEY_MAIL} },
445         { KE_KEY, 0x36, {KEY_WWW} },
446         { KE_END, FE_WIFI_LED | FE_UNTESTED }
447 };
448
449 static struct key_entry keymap_acer_travelmate_240[] = {
450         { KE_KEY, 0x01, {KEY_HELP} },
451         { KE_KEY, 0x02, {KEY_CONFIG} },
452         { KE_KEY, 0x03, {KEY_POWER} },
453         { KE_KEY, 0x08, {KEY_MUTE} },
454         { KE_KEY, 0x31, {KEY_MAIL} },
455         { KE_KEY, 0x36, {KEY_WWW} },
456         { KE_KEY, 0x11, {KEY_PROG1} },
457         { KE_KEY, 0x12, {KEY_PROG2} },
458         { KE_BLUETOOTH, 0x44 },
459         { KE_WIFI, 0x30 },
460         { KE_END, FE_UNTESTED }
461 };
462
463 static struct key_entry keymap_acer_travelmate_350[] = {
464         { KE_KEY, 0x01, {KEY_HELP} },
465         { KE_KEY, 0x02, {KEY_CONFIG} },
466         { KE_KEY, 0x11, {KEY_PROG1} },
467         { KE_KEY, 0x12, {KEY_PROG2} },
468         { KE_KEY, 0x13, {KEY_MAIL} },
469         { KE_KEY, 0x14, {KEY_PROG3} },
470         { KE_KEY, 0x15, {KEY_WWW} },
471         { KE_END, FE_MAIL_LED | FE_WIFI_LED | FE_UNTESTED }
472 };
473
474 static struct key_entry keymap_acer_travelmate_360[] = {
475         { KE_KEY, 0x01, {KEY_HELP} },
476         { KE_KEY, 0x02, {KEY_CONFIG} },
477         { KE_KEY, 0x11, {KEY_PROG1} },
478         { KE_KEY, 0x12, {KEY_PROG2} },
479         { KE_KEY, 0x13, {KEY_MAIL} },
480         { KE_KEY, 0x14, {KEY_PROG3} },
481         { KE_KEY, 0x15, {KEY_WWW} },
482         { KE_KEY, 0x40, {KEY_WLAN} },
483         { KE_END, FE_WIFI_LED | FE_UNTESTED } /* no mail led */
484 };
485
486 /* Wifi subsystem only activates the led. Therefore we need to pass
487  * wifi event as a normal key, then userspace can really change the wifi state.
488  * TODO we need to export led state to userspace (wifi and mail) */
489 static struct key_entry keymap_acer_travelmate_610[] = {
490         { KE_KEY, 0x01, {KEY_HELP} },
491         { KE_KEY, 0x02, {KEY_CONFIG} },
492         { KE_KEY, 0x11, {KEY_PROG1} },
493         { KE_KEY, 0x12, {KEY_PROG3} },
494         { KE_KEY, 0x13, {KEY_PROG3} },
495         { KE_KEY, 0x14, {KEY_MAIL} },
496         { KE_KEY, 0x15, {KEY_WWW} },
497         { KE_KEY, 0x40, {KEY_WLAN} },
498         { KE_END, FE_MAIL_LED | FE_WIFI_LED }
499 };
500
501 static struct key_entry keymap_acer_travelmate_630[] = {
502         { KE_KEY, 0x01, {KEY_HELP} },
503         { KE_KEY, 0x02, {KEY_CONFIG} },
504         { KE_KEY, 0x03, {KEY_POWER} },
505         { KE_KEY, 0x08, {KEY_MUTE} }, /* not 620 */
506         { KE_KEY, 0x11, {KEY_PROG1} },
507         { KE_KEY, 0x12, {KEY_PROG2} },
508         { KE_KEY, 0x13, {KEY_PROG3} },
509         { KE_KEY, 0x20, {KEY_VOLUMEUP} },
510         { KE_KEY, 0x21, {KEY_VOLUMEDOWN} },
511         { KE_KEY, 0x31, {KEY_MAIL} },
512         { KE_KEY, 0x36, {KEY_WWW} },
513         { KE_WIFI, 0x30 },
514         { KE_END, FE_MAIL_LED | FE_UNTESTED }
515 };
516
517 static struct key_entry keymap_aopen_1559as[] = {
518         { KE_KEY,  0x01, {KEY_HELP} },
519         { KE_KEY,  0x06, {KEY_PROG3} },
520         { KE_KEY,  0x11, {KEY_PROG1} },
521         { KE_KEY,  0x12, {KEY_PROG2} },
522         { KE_WIFI, 0x30 },
523         { KE_KEY,  0x31, {KEY_MAIL} },
524         { KE_KEY,  0x36, {KEY_WWW} },
525         { KE_END,  0 },
526 };
527
528 static struct key_entry keymap_fs_amilo_d88x0[] = {
529         { KE_KEY, 0x01, {KEY_HELP} },
530         { KE_KEY, 0x08, {KEY_MUTE} },
531         { KE_KEY, 0x31, {KEY_MAIL} },
532         { KE_KEY, 0x36, {KEY_WWW} },
533         { KE_KEY, 0x11, {KEY_PROG1} },
534         { KE_KEY, 0x12, {KEY_PROG2} },
535         { KE_KEY, 0x13, {KEY_PROG3} },
536         { KE_END, FE_MAIL_LED | FE_WIFI_LED | FE_UNTESTED }
537 };
538
539 static struct key_entry keymap_wistron_md2900[] = {
540         { KE_KEY, 0x01, {KEY_HELP} },
541         { KE_KEY, 0x02, {KEY_CONFIG} },
542         { KE_KEY, 0x11, {KEY_PROG1} },
543         { KE_KEY, 0x12, {KEY_PROG2} },
544         { KE_KEY, 0x31, {KEY_MAIL} },
545         { KE_KEY, 0x36, {KEY_WWW} },
546         { KE_WIFI, 0x30 },
547         { KE_END, FE_MAIL_LED | FE_UNTESTED }
548 };
549
550 static struct key_entry keymap_wistron_md96500[] = {
551         { KE_KEY, 0x01, {KEY_HELP} },
552         { KE_KEY, 0x02, {KEY_CONFIG} },
553         { KE_KEY, 0x05, {KEY_SWITCHVIDEOMODE} }, /* Display selection */
554         { KE_KEY, 0x06, {KEY_DISPLAYTOGGLE} }, /* Display on/off */
555         { KE_KEY, 0x08, {KEY_MUTE} },
556         { KE_KEY, 0x11, {KEY_PROG1} },
557         { KE_KEY, 0x12, {KEY_PROG2} },
558         { KE_KEY, 0x20, {KEY_VOLUMEUP} },
559         { KE_KEY, 0x21, {KEY_VOLUMEDOWN} },
560         { KE_KEY, 0x22, {KEY_REWIND} },
561         { KE_KEY, 0x23, {KEY_FORWARD} },
562         { KE_KEY, 0x24, {KEY_PLAYPAUSE} },
563         { KE_KEY, 0x25, {KEY_STOPCD} },
564         { KE_KEY, 0x31, {KEY_MAIL} },
565         { KE_KEY, 0x36, {KEY_WWW} },
566         { KE_WIFI, 0x30 },
567         { KE_BLUETOOTH, 0x44 },
568         { KE_END, FE_UNTESTED }
569 };
570
571 /*
572  * If your machine is not here (which is currently rather likely), please send
573  * a list of buttons and their key codes (reported when loading this module
574  * with force=1) and the output of dmidecode to $MODULE_AUTHOR.
575  */
576 static struct dmi_system_id dmi_ids[] __initdata = {
577         {
578                 .callback = dmi_matched,
579                 .ident = "Fujitsu-Siemens Amilo Pro V2000",
580                 .matches = {
581                         DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
582                         DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Pro V2000"),
583                 },
584                 .driver_data = keymap_fs_amilo_pro_v2000
585         },
586         {
587                 .callback = dmi_matched,
588                 .ident = "Fujitsu-Siemens Amilo M7400",
589                 .matches = {
590                         DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
591                         DMI_MATCH(DMI_PRODUCT_NAME, "AMILO M        "),
592                 },
593                 .driver_data = keymap_fs_amilo_pro_v2000
594         },
595         {
596                 .callback = dmi_matched,
597                 .ident = "Fujitsu N3510",
598                 .matches = {
599                         DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"),
600                         DMI_MATCH(DMI_PRODUCT_NAME, "N3510"),
601                 },
602                 .driver_data = keymap_fujitsu_n3510
603         },
604         {
605                 .callback = dmi_matched,
606                 .ident = "Acer Aspire 1500",
607                 .matches = {
608                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
609                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 1500"),
610                 },
611                 .driver_data = keymap_acer_aspire_1500
612         },
613         {
614                 .callback = dmi_matched,
615                 .ident = "Acer Aspire 1600",
616                 .matches = {
617                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
618                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 1600"),
619                 },
620                 .driver_data = keymap_acer_aspire_1600
621         },
622         {
623                 .callback = dmi_matched,
624                 .ident = "Acer Aspire 3020",
625                 .matches = {
626                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
627                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 3020"),
628                 },
629                 .driver_data = keymap_acer_aspire_5020
630         },
631         {
632                 .callback = dmi_matched,
633                 .ident = "Acer Aspire 5020",
634                 .matches = {
635                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
636                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5020"),
637                 },
638                 .driver_data = keymap_acer_aspire_5020
639         },
640         {
641                 .callback = dmi_matched,
642                 .ident = "Acer TravelMate 2100",
643                 .matches = {
644                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
645                         DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 2100"),
646                 },
647                 .driver_data = keymap_acer_aspire_5020
648         },
649         {
650                 .callback = dmi_matched,
651                 .ident = "Acer TravelMate 2410",
652                 .matches = {
653                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
654                         DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 2410"),
655                 },
656                 .driver_data = keymap_acer_travelmate_2410
657         },
658         {
659                 .callback = dmi_matched,
660                 .ident = "Acer TravelMate C300",
661                 .matches = {
662                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
663                         DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate C300"),
664                 },
665                 .driver_data = keymap_acer_travelmate_300
666         },
667         {
668                 .callback = dmi_matched,
669                 .ident = "Acer TravelMate C100",
670                 .matches = {
671                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
672                         DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate C100"),
673                 },
674                 .driver_data = keymap_acer_travelmate_300
675         },
676         {
677                 .callback = dmi_matched,
678                 .ident = "Acer TravelMate C110",
679                 .matches = {
680                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
681                         DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate C110"),
682                 },
683                 .driver_data = keymap_acer_travelmate_110
684         },
685         {
686                 .callback = dmi_matched,
687                 .ident = "Acer TravelMate 380",
688                 .matches = {
689                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
690                         DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 380"),
691                 },
692                 .driver_data = keymap_acer_travelmate_380
693         },
694         {
695                 .callback = dmi_matched,
696                 .ident = "Acer TravelMate 370",
697                 .matches = {
698                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
699                         DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 370"),
700                 },
701                 .driver_data = keymap_acer_travelmate_380 /* keyboard minus 1 key */
702         },
703         {
704                 .callback = dmi_matched,
705                 .ident = "Acer TravelMate 220",
706                 .matches = {
707                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
708                         DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 220"),
709                 },
710                 .driver_data = keymap_acer_travelmate_220
711         },
712         {
713                 .callback = dmi_matched,
714                 .ident = "Acer TravelMate 260",
715                 .matches = {
716                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
717                         DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 260"),
718                 },
719                 .driver_data = keymap_acer_travelmate_220
720         },
721         {
722                 .callback = dmi_matched,
723                 .ident = "Acer TravelMate 230",
724                 .matches = {
725                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
726                         DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 230"),
727                         /* acerhk looks for "TravelMate F4..." ?! */
728                 },
729                 .driver_data = keymap_acer_travelmate_230
730         },
731         {
732                 .callback = dmi_matched,
733                 .ident = "Acer TravelMate 280",
734                 .matches = {
735                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
736                         DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 280"),
737                 },
738                 .driver_data = keymap_acer_travelmate_230
739         },
740         {
741                 .callback = dmi_matched,
742                 .ident = "Acer TravelMate 240",
743                 .matches = {
744                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
745                         DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 240"),
746                 },
747                 .driver_data = keymap_acer_travelmate_240
748         },
749         {
750                 .callback = dmi_matched,
751                 .ident = "Acer TravelMate 250",
752                 .matches = {
753                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
754                         DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 250"),
755                 },
756                 .driver_data = keymap_acer_travelmate_240
757         },
758         {
759                 .callback = dmi_matched,
760                 .ident = "Acer TravelMate 2424NWXCi",
761                 .matches = {
762                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
763                         DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 2420"),
764                 },
765                 .driver_data = keymap_acer_travelmate_240
766         },
767         {
768                 .callback = dmi_matched,
769                 .ident = "Acer TravelMate 350",
770                 .matches = {
771                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
772                         DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 350"),
773                 },
774                 .driver_data = keymap_acer_travelmate_350
775         },
776         {
777                 .callback = dmi_matched,
778                 .ident = "Acer TravelMate 360",
779                 .matches = {
780                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
781                         DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 360"),
782                 },
783                 .driver_data = keymap_acer_travelmate_360
784         },
785         {
786                 .callback = dmi_matched,
787                 .ident = "Acer TravelMate 610",
788                 .matches = {
789                         DMI_MATCH(DMI_SYS_VENDOR, "ACER"),
790                         DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 610"),
791                 },
792                 .driver_data = keymap_acer_travelmate_610
793         },
794         {
795                 .callback = dmi_matched,
796                 .ident = "Acer TravelMate 620",
797                 .matches = {
798                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
799                         DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 620"),
800                 },
801                 .driver_data = keymap_acer_travelmate_630
802         },
803         {
804                 .callback = dmi_matched,
805                 .ident = "Acer TravelMate 630",
806                 .matches = {
807                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
808                         DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 630"),
809                 },
810                 .driver_data = keymap_acer_travelmate_630
811         },
812         {
813                 .callback = dmi_matched,
814                 .ident = "AOpen 1559AS",
815                 .matches = {
816                         DMI_MATCH(DMI_PRODUCT_NAME, "E2U"),
817                         DMI_MATCH(DMI_BOARD_NAME, "E2U"),
818                 },
819                 .driver_data = keymap_aopen_1559as
820         },
821         {
822                 .callback = dmi_matched,
823                 .ident = "Medion MD 9783",
824                 .matches = {
825                         DMI_MATCH(DMI_SYS_VENDOR, "MEDIONNB"),
826                         DMI_MATCH(DMI_PRODUCT_NAME, "MD 9783"),
827                 },
828                 .driver_data = keymap_wistron_ms2111
829         },
830         {
831                 .callback = dmi_matched,
832                 .ident = "Medion MD 40100",
833                 .matches = {
834                         DMI_MATCH(DMI_SYS_VENDOR, "MEDIONNB"),
835                         DMI_MATCH(DMI_PRODUCT_NAME, "WID2000"),
836                 },
837                 .driver_data = keymap_wistron_md40100
838         },
839         {
840                 .callback = dmi_matched,
841                 .ident = "Medion MD 2900",
842                 .matches = {
843                         DMI_MATCH(DMI_SYS_VENDOR, "MEDIONNB"),
844                         DMI_MATCH(DMI_PRODUCT_NAME, "WIM 2000"),
845                 },
846                 .driver_data = keymap_wistron_md2900
847         },
848         {
849                 .callback = dmi_matched,
850                 .ident = "Medion MD 96500",
851                 .matches = {
852                         DMI_MATCH(DMI_SYS_VENDOR, "MEDIONPC"),
853                         DMI_MATCH(DMI_PRODUCT_NAME, "WIM 2040"),
854                 },
855                 .driver_data = keymap_wistron_md96500
856         },
857         {
858                 .callback = dmi_matched,
859                 .ident = "Medion MD 95400",
860                 .matches = {
861                         DMI_MATCH(DMI_SYS_VENDOR, "MEDIONPC"),
862                         DMI_MATCH(DMI_PRODUCT_NAME, "WIM 2050"),
863                 },
864                 .driver_data = keymap_wistron_md96500
865         },
866         {
867                 .callback = dmi_matched,
868                 .ident = "Fujitsu Siemens Amilo D7820",
869                 .matches = {
870                         DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"), /* not sure */
871                         DMI_MATCH(DMI_PRODUCT_NAME, "Amilo D"),
872                 },
873                 .driver_data = keymap_fs_amilo_d88x0
874         },
875         {
876                 .callback = dmi_matched,
877                 .ident = "Fujitsu Siemens Amilo D88x0",
878                 .matches = {
879                         DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
880                         DMI_MATCH(DMI_PRODUCT_NAME, "AMILO D"),
881                 },
882                 .driver_data = keymap_fs_amilo_d88x0
883         },
884         { NULL, }
885 };
886
887 static int __init select_keymap(void)
888 {
889         if (keymap_name != NULL) {
890                 if (strcmp (keymap_name, "1557/MS2141") == 0)
891                         keymap = keymap_wistron_ms2141;
892                 else {
893                         printk(KERN_ERR "wistron_btns: Keymap unknown\n");
894                         return -EINVAL;
895                 }
896         }
897         dmi_check_system(dmi_ids);
898         if (keymap == NULL) {
899                 if (!force) {
900                         printk(KERN_ERR "wistron_btns: System unknown\n");
901                         return -ENODEV;
902                 }
903                 keymap = keymap_empty;
904         }
905         return 0;
906 }
907
908  /* Input layer interface */
909
910 static struct input_dev *input_dev;
911
912 static int __devinit setup_input_dev(void)
913 {
914         const struct key_entry *key;
915         int error;
916
917         input_dev = input_allocate_device();
918         if (!input_dev)
919                 return -ENOMEM;
920
921         input_dev->name = "Wistron laptop buttons";
922         input_dev->phys = "wistron/input0";
923         input_dev->id.bustype = BUS_HOST;
924         input_dev->cdev.dev = &wistron_device->dev;
925
926         for (key = keymap; key->type != KE_END; key++) {
927                 switch (key->type) {
928                         case KE_KEY:
929                                 set_bit(EV_KEY, input_dev->evbit);
930                                 set_bit(key->keycode, input_dev->keybit);
931                                 break;
932
933                         case KE_SW:
934                                 set_bit(EV_SW, input_dev->evbit);
935                                 set_bit(key->sw.code, input_dev->swbit);
936                                 break;
937
938                         default:
939                                 ;
940                 }
941         }
942
943         /* reads information flags on KE_END */
944         if (key->code & FE_UNTESTED)
945                 printk(KERN_WARNING "Untested laptop multimedia keys, "
946                         "please report success or failure to eric.piel"
947                         "@tremplin-utc.net\n");
948
949         error = input_register_device(input_dev);
950         if (error) {
951                 input_free_device(input_dev);
952                 return error;
953         }
954
955         return 0;
956 }
957
958 static void report_key(unsigned keycode)
959 {
960         input_report_key(input_dev, keycode, 1);
961         input_sync(input_dev);
962         input_report_key(input_dev, keycode, 0);
963         input_sync(input_dev);
964 }
965
966 static void report_switch(unsigned code, int value)
967 {
968         input_report_switch(input_dev, code, value);
969         input_sync(input_dev);
970 }
971
972  /* Driver core */
973
974 static int wifi_enabled;
975 static int bluetooth_enabled;
976
977 static void poll_bios(unsigned long);
978
979 static struct timer_list poll_timer = TIMER_INITIALIZER(poll_bios, 0, 0);
980
981 static void handle_key(u8 code)
982 {
983         const struct key_entry *key;
984
985         for (key = keymap; key->type != KE_END; key++) {
986                 if (code == key->code) {
987                         switch (key->type) {
988                         case KE_KEY:
989                                 report_key(key->keycode);
990                                 break;
991
992                         case KE_SW:
993                                 report_switch(key->sw.code, key->sw.value);
994                                 break;
995
996                         case KE_WIFI:
997                                 if (have_wifi) {
998                                         wifi_enabled = !wifi_enabled;
999                                         bios_set_state(WIFI, wifi_enabled);
1000                                 }
1001                                 break;
1002
1003                         case KE_BLUETOOTH:
1004                                 if (have_bluetooth) {
1005                                         bluetooth_enabled = !bluetooth_enabled;
1006                                         bios_set_state(BLUETOOTH, bluetooth_enabled);
1007                                 }
1008                                 break;
1009
1010                         case KE_END:
1011                                 break;
1012                         default:
1013                                 BUG();
1014                         }
1015                         return;
1016                 }
1017         }
1018         printk(KERN_NOTICE "wistron_btns: Unknown key code %02X\n", code);
1019 }
1020
1021 static void poll_bios(unsigned long discard)
1022 {
1023         u8 qlen;
1024         u16 val;
1025
1026         for (;;) {
1027                 qlen = CMOS_READ(cmos_address);
1028                 if (qlen == 0)
1029                         break;
1030                 val = bios_pop_queue();
1031                 if (val != 0 && !discard)
1032                         handle_key((u8)val);
1033         }
1034
1035         mod_timer(&poll_timer, jiffies + HZ / POLL_FREQUENCY);
1036 }
1037
1038 static int __devinit wistron_probe(struct platform_device *dev)
1039 {
1040         int err = setup_input_dev();
1041         if (err)
1042                 return err;
1043
1044         bios_attach();
1045         cmos_address = bios_get_cmos_address();
1046
1047         if (have_wifi) {
1048                 u16 wifi = bios_get_default_setting(WIFI);
1049                 if (wifi & 1)
1050                         wifi_enabled = (wifi & 2) ? 1 : 0;
1051                 else
1052                         have_wifi = 0;
1053
1054                 if (have_wifi)
1055                         bios_set_state(WIFI, wifi_enabled);
1056         }
1057
1058         if (have_bluetooth) {
1059                 u16 bt = bios_get_default_setting(BLUETOOTH);
1060                 if (bt & 1)
1061                         bluetooth_enabled = (bt & 2) ? 1 : 0;
1062                 else
1063                         have_bluetooth = 0;
1064
1065                 if (have_bluetooth)
1066                         bios_set_state(BLUETOOTH, bluetooth_enabled);
1067         }
1068
1069         poll_bios(1); /* Flush stale event queue and arm timer */
1070
1071         return 0;
1072 }
1073
1074 static int __devexit wistron_remove(struct platform_device *dev)
1075 {
1076         del_timer_sync(&poll_timer);
1077         input_unregister_device(input_dev);
1078         bios_detach();
1079
1080         return 0;
1081 }
1082
1083 #ifdef CONFIG_PM
1084 static int wistron_suspend(struct platform_device *dev, pm_message_t state)
1085 {
1086         del_timer_sync(&poll_timer);
1087
1088         if (have_wifi)
1089                 bios_set_state(WIFI, 0);
1090
1091         if (have_bluetooth)
1092                 bios_set_state(BLUETOOTH, 0);
1093
1094         return 0;
1095 }
1096
1097 static int wistron_resume(struct platform_device *dev)
1098 {
1099         if (have_wifi)
1100                 bios_set_state(WIFI, wifi_enabled);
1101
1102         if (have_bluetooth)
1103                 bios_set_state(BLUETOOTH, bluetooth_enabled);
1104
1105         poll_bios(1);
1106
1107         return 0;
1108 }
1109 #else
1110 #define wistron_suspend         NULL
1111 #define wistron_resume          NULL
1112 #endif
1113
1114 static struct platform_driver wistron_driver = {
1115         .driver         = {
1116                 .name   = "wistron-bios",
1117                 .owner  = THIS_MODULE,
1118         },
1119         .probe          = wistron_probe,
1120         .remove         = __devexit_p(wistron_remove),
1121         .suspend        = wistron_suspend,
1122         .resume         = wistron_resume,
1123 };
1124
1125 static int __init wb_module_init(void)
1126 {
1127         int err;
1128
1129         err = select_keymap();
1130         if (err)
1131                 return err;
1132
1133         err = map_bios();
1134         if (err)
1135                 return err;
1136
1137         err = platform_driver_register(&wistron_driver);
1138         if (err)
1139                 goto err_unmap_bios;
1140
1141         wistron_device = platform_device_alloc("wistron-bios", -1);
1142         if (!wistron_device) {
1143                 err = -ENOMEM;
1144                 goto err_unregister_driver;
1145         }
1146
1147         err = platform_device_add(wistron_device);
1148         if (err)
1149                 goto err_free_device;
1150
1151         return 0;
1152
1153  err_free_device:
1154         platform_device_put(wistron_device);
1155  err_unregister_driver:
1156         platform_driver_unregister(&wistron_driver);
1157  err_unmap_bios:
1158         unmap_bios();
1159
1160         return err;
1161 }
1162
1163 static void __exit wb_module_exit(void)
1164 {
1165         platform_device_unregister(wistron_device);
1166         platform_driver_unregister(&wistron_driver);
1167         unmap_bios();
1168 }
1169
1170 module_init(wb_module_init);
1171 module_exit(wb_module_exit);