blob: ef7ee924ecc6795f023d4ac7747f9cf938c3760b [file] [log] [blame]
Dmitry Torokhov5fc146802005-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 Torokhova5b0cc802005-11-20 00:50:58 -05005 * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
Dmitry Torokhov5fc146802005-11-20 00:50:06 -05006 *
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 <asm/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>
Dmitry Torokhova5b0cc802005-11-20 00:50:58 -050032#include <linux/platform_device.h>
Dmitry Torokhov5fc146802005-11-20 00:50:06 -050033
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
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -050046/* BIOS subsystem IDs */
47#define WIFI 0x35
48#define BLUETOOTH 0x34
49
Dmitry Torokhov5fc146802005-11-20 00:50:06 -050050MODULE_AUTHOR("Miloslav Trmac <mitr@volny.cz>");
51MODULE_DESCRIPTION("Wistron laptop button driver");
52MODULE_LICENSE("GPL v2");
53MODULE_VERSION("0.1");
54
55static int force; /* = 0; */
56module_param(force, bool, 0);
57MODULE_PARM_DESC(force, "Load even if computer is not in database");
58
59static char *keymap_name; /* = NULL; */
60module_param_named(keymap, keymap_name, charp, 0);
61MODULE_PARM_DESC(keymap, "Keymap name, if it can't be autodetected");
62
Dmitry Torokhova5b0cc802005-11-20 00:50:58 -050063static struct platform_device *wistron_device;
64
Dmitry Torokhov5fc146802005-11-20 00:50:06 -050065 /* BIOS interface implementation */
66
67static void __iomem *bios_entry_point; /* BIOS routine entry point */
68static void __iomem *bios_code_map_base;
69static void __iomem *bios_data_map_base;
70
71static u8 cmos_address;
72
73struct regs {
74 u32 eax, ebx, ecx;
75};
76
77static 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
95static size_t __init locate_wistron_bios(void __iomem *base)
96{
97 static const unsigned char __initdata signature[] =
98 { 0x42, 0x21, 0x55, 0x30 };
99 size_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
109static int __init map_bios(void)
110{
111 void __iomem *base;
112 size_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
150err_code:
151 iounmap(bios_code_map_base);
152err:
153 return -ENOMEM;
154}
155
Dmitry Torokhov22a397e2005-11-20 00:50:46 -0500156static inline void unmap_bios(void)
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500157{
158 iounmap(bios_code_map_base);
159 iounmap(bios_data_map_base);
160}
161
162 /* BIOS calls */
163
164static 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
177static void __init 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
Dmitry Torokhov22a397e2005-11-20 00:50:46 -0500187static void bios_detach(void)
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500188{
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
197static u8 __init 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
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500209static u16 __init bios_get_default_setting(u8 subsys)
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500210{
211 struct regs regs;
212
213 memset(&regs, 0, sizeof (regs));
214 regs.eax = 0x9610;
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500215 regs.ebx = 0x0200 | subsys;
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500216 call_bios(&regs);
217
218 return regs.eax;
219}
220
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500221static void bios_set_state(u8 subsys, int enable)
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500222{
223 struct regs regs;
224
225 memset(&regs, 0, sizeof (regs));
226 regs.eax = 0x9610;
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500227 regs.ebx = (enable ? 0x0100 : 0x0000) | subsys;
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500228 call_bios(&regs);
229}
230
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500231/* Hardware database */
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500232
233struct key_entry {
234 char type; /* See KE_* below */
235 u8 code;
236 unsigned keycode; /* For KE_KEY */
237};
238
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500239enum { KE_END, KE_KEY, KE_WIFI, KE_BLUETOOTH };
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500240
241static const struct key_entry *keymap; /* = NULL; Current key map */
242static int have_wifi;
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500243static int have_bluetooth;
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500244
245static 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 break;
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500254 } else if (key->type == KE_BLUETOOTH) {
255 have_bluetooth = 1;
256 break;
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500257 }
258 }
259 return 1;
260}
261
262static struct key_entry keymap_empty[] = {
263 { KE_END, 0 }
264};
265
266static 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 },
273 { KE_END, 0 }
274};
275
276static struct key_entry keymap_wistron_ms2141[] = {
277 { KE_KEY, 0x11, KEY_PROG1 },
278 { KE_KEY, 0x12, KEY_PROG2 },
279 { KE_WIFI, 0x30, 0 },
280 { KE_KEY, 0x22, KEY_REWIND },
281 { KE_KEY, 0x23, KEY_FORWARD },
282 { KE_KEY, 0x24, KEY_PLAYPAUSE },
283 { KE_KEY, 0x25, KEY_STOPCD },
284 { KE_KEY, 0x31, KEY_MAIL },
285 { KE_KEY, 0x36, KEY_WWW },
286 { KE_END, 0 }
287};
288
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500289static struct key_entry keymap_acer_aspire_1500[] = {
290 { KE_KEY, 0x11, KEY_PROG1 },
291 { KE_KEY, 0x12, KEY_PROG2 },
292 { KE_WIFI, 0x30, 0 },
293 { KE_KEY, 0x31, KEY_MAIL },
294 { KE_KEY, 0x36, KEY_WWW },
295 { KE_BLUETOOTH, 0x44, 0 },
296 { KE_END, 0 }
297};
298
Ashutosh Naik74a89c92005-12-11 12:41:32 -0500299static struct key_entry keymap_acer_travelmate_240[] = {
300 { KE_KEY, 0x31, KEY_MAIL },
301 { KE_KEY, 0x36, KEY_WWW },
302 { KE_KEY, 0x11, KEY_PROG1 },
303 { KE_KEY, 0x12, KEY_PROG2 },
304 { KE_BLUETOOTH, 0x44, 0 },
305 { KE_WIFI, 0x30, 0 },
306 { KE_END, 0 }
307};
308
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500309/*
310 * If your machine is not here (which is currently rather likely), please send
311 * a list of buttons and their key codes (reported when loading this module
312 * with force=1) and the output of dmidecode to $MODULE_AUTHOR.
313 */
314static struct dmi_system_id dmi_ids[] = {
315 {
316 .callback = dmi_matched,
317 .ident = "Fujitsu-Siemens Amilo Pro V2000",
318 .matches = {
319 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
320 DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Pro V2000"),
321 },
322 .driver_data = keymap_fs_amilo_pro_v2000
323 },
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500324 {
325 .callback = dmi_matched,
326 .ident = "Acer Aspire 1500",
327 .matches = {
328 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
329 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 1500"),
330 },
331 .driver_data = keymap_acer_aspire_1500
332 },
Ashutosh Naik74a89c92005-12-11 12:41:32 -0500333 {
334 .callback = dmi_matched,
335 .ident = "Acer TravelMate 240",
336 .matches = {
337 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
338 DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 240"),
339 },
340 .driver_data = keymap_acer_travelmate_240
341 },
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500342 { 0, }
343};
344
345static int __init select_keymap(void)
346{
347 if (keymap_name != NULL) {
348 if (strcmp (keymap_name, "1557/MS2141") == 0)
349 keymap = keymap_wistron_ms2141;
350 else {
351 printk(KERN_ERR "wistron_btns: Keymap unknown\n");
352 return -EINVAL;
353 }
354 }
355 dmi_check_system(dmi_ids);
356 if (keymap == NULL) {
357 if (!force) {
358 printk(KERN_ERR "wistron_btns: System unknown\n");
359 return -ENODEV;
360 }
361 keymap = keymap_empty;
362 }
363 return 0;
364}
365
366 /* Input layer interface */
367
Dmitry Torokhov22a397e2005-11-20 00:50:46 -0500368static struct input_dev *input_dev;
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500369
Dmitry Torokhov22a397e2005-11-20 00:50:46 -0500370static int __init setup_input_dev(void)
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500371{
372 const struct key_entry *key;
Dmitry Torokhov22a397e2005-11-20 00:50:46 -0500373 int error;
374
375 input_dev = input_allocate_device();
376 if (!input_dev)
377 return -ENOMEM;
378
379 input_dev->name = "Wistron laptop buttons";
380 input_dev->phys = "wistron/input0";
381 input_dev->id.bustype = BUS_HOST;
Dmitry Torokhova5b0cc802005-11-20 00:50:58 -0500382 input_dev->cdev.dev = &wistron_device->dev;
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500383
384 for (key = keymap; key->type != KE_END; key++) {
385 if (key->type == KE_KEY) {
Dmitry Torokhov22a397e2005-11-20 00:50:46 -0500386 input_dev->evbit[LONG(EV_KEY)] = BIT(EV_KEY);
387 set_bit(key->keycode, input_dev->keybit);
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500388 }
389 }
390
Dmitry Torokhov22a397e2005-11-20 00:50:46 -0500391 error = input_register_device(input_dev);
392 if (error) {
393 input_free_device(input_dev);
394 return error;
395 }
396
397 return 0;
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500398}
399
400static void report_key(unsigned keycode)
401{
Dmitry Torokhov22a397e2005-11-20 00:50:46 -0500402 input_report_key(input_dev, keycode, 1);
403 input_sync(input_dev);
404 input_report_key(input_dev, keycode, 0);
405 input_sync(input_dev);
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500406}
407
408 /* Driver core */
409
410static int wifi_enabled;
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500411static int bluetooth_enabled;
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500412
413static void poll_bios(unsigned long);
414
415static struct timer_list poll_timer = TIMER_INITIALIZER(poll_bios, 0, 0);
416
417static void handle_key(u8 code)
418{
419 const struct key_entry *key;
420
421 for (key = keymap; key->type != KE_END; key++) {
422 if (code == key->code) {
423 switch (key->type) {
424 case KE_KEY:
425 report_key(key->keycode);
426 break;
427
428 case KE_WIFI:
429 if (have_wifi) {
430 wifi_enabled = !wifi_enabled;
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500431 bios_set_state(WIFI, wifi_enabled);
432 }
433 break;
434
435 case KE_BLUETOOTH:
436 if (have_bluetooth) {
437 bluetooth_enabled = !bluetooth_enabled;
438 bios_set_state(BLUETOOTH, bluetooth_enabled);
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500439 }
440 break;
441
442 case KE_END:
443 default:
444 BUG();
445 }
446 return;
447 }
448 }
449 printk(KERN_NOTICE "wistron_btns: Unknown key code %02X\n", code);
450}
451
452static void poll_bios(unsigned long discard)
453{
454 u8 qlen;
455 u16 val;
456
457 for (;;) {
458 qlen = CMOS_READ(cmos_address);
459 if (qlen == 0)
460 break;
461 val = bios_pop_queue();
462 if (val != 0 && !discard)
463 handle_key((u8)val);
464 }
465
466 mod_timer(&poll_timer, jiffies + HZ / POLL_FREQUENCY);
467}
468
Dmitry Torokhova5b0cc802005-11-20 00:50:58 -0500469static int wistron_suspend(struct platform_device *dev, pm_message_t state)
470{
471 del_timer_sync(&poll_timer);
472
Miloslav Trmace753b652005-11-20 00:51:05 -0500473 if (have_wifi)
474 bios_set_state(WIFI, 0);
475
476 if (have_bluetooth)
477 bios_set_state(BLUETOOTH, 0);
478
Dmitry Torokhova5b0cc802005-11-20 00:50:58 -0500479 return 0;
480}
481
482static int wistron_resume(struct platform_device *dev)
483{
484 if (have_wifi)
485 bios_set_state(WIFI, wifi_enabled);
486
487 if (have_bluetooth)
488 bios_set_state(BLUETOOTH, bluetooth_enabled);
489
490 poll_bios(1);
491
492 return 0;
493}
494
495static struct platform_driver wistron_driver = {
496 .suspend = wistron_suspend,
497 .resume = wistron_resume,
498 .driver = {
499 .name = "wistron-bios",
500 },
501};
502
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500503static int __init wb_module_init(void)
504{
505 int err;
506
507 err = select_keymap();
508 if (err)
509 return err;
Dmitry Torokhov22a397e2005-11-20 00:50:46 -0500510
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500511 err = map_bios();
512 if (err)
513 return err;
Dmitry Torokhov22a397e2005-11-20 00:50:46 -0500514
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500515 bios_attach();
516 cmos_address = bios_get_cmos_address();
Dmitry Torokhov22a397e2005-11-20 00:50:46 -0500517
Dmitry Torokhova5b0cc802005-11-20 00:50:58 -0500518 err = platform_driver_register(&wistron_driver);
519 if (err)
520 goto err_detach_bios;
521
522 wistron_device = platform_device_register_simple("wistron-bios", -1, NULL, 0);
523 if (IS_ERR(wistron_device)) {
524 err = PTR_ERR(wistron_device);
525 goto err_unregister_driver;
526 }
527
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500528 if (have_wifi) {
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500529 u16 wifi = bios_get_default_setting(WIFI);
530 if (wifi & 1)
531 wifi_enabled = (wifi & 2) ? 1 : 0;
532 else
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500533 have_wifi = 0;
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500534
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500535 if (have_wifi)
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500536 bios_set_state(WIFI, wifi_enabled);
537 }
Dmitry Torokhov22a397e2005-11-20 00:50:46 -0500538
Bernhard Rosenkraenzer84b256a2005-11-20 00:50:37 -0500539 if (have_bluetooth) {
540 u16 bt = bios_get_default_setting(BLUETOOTH);
541 if (bt & 1)
542 bluetooth_enabled = (bt & 2) ? 1 : 0;
543 else
544 have_bluetooth = 0;
545
546 if (have_bluetooth)
547 bios_set_state(BLUETOOTH, bluetooth_enabled);
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500548 }
549
Dmitry Torokhov22a397e2005-11-20 00:50:46 -0500550 err = setup_input_dev();
Dmitry Torokhova5b0cc802005-11-20 00:50:58 -0500551 if (err)
552 goto err_unregister_device;
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500553
554 poll_bios(1); /* Flush stale event queue and arm timer */
555
556 return 0;
Dmitry Torokhova5b0cc802005-11-20 00:50:58 -0500557
558 err_unregister_device:
559 platform_device_unregister(wistron_device);
560 err_unregister_driver:
561 platform_driver_unregister(&wistron_driver);
562 err_detach_bios:
563 bios_detach();
564 unmap_bios();
565
566 return err;
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500567}
568
569static void __exit wb_module_exit(void)
570{
571 del_timer_sync(&poll_timer);
Dmitry Torokhov22a397e2005-11-20 00:50:46 -0500572 input_unregister_device(input_dev);
Dmitry Torokhova5b0cc802005-11-20 00:50:58 -0500573 platform_device_unregister(wistron_device);
574 platform_driver_unregister(&wistron_driver);
Dmitry Torokhov5fc146802005-11-20 00:50:06 -0500575 bios_detach();
576 unmap_bios();
577}
578
579module_init(wb_module_init);
580module_exit(wb_module_exit);