Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Wistron laptop button driver |
| 3 | * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz> |
Bernhard Rosenkraenzer | 84b256a | 2005-11-20 00:50:37 -0500 | [diff] [blame] | 4 | * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org> |
Dmitry Torokhov | a5b0cc80 | 2005-11-20 00:50:58 -0500 | [diff] [blame] | 5 | * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru> |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 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 <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 Torokhov | a5b0cc80 | 2005-11-20 00:50:58 -0500 | [diff] [blame] | 32 | #include <linux/platform_device.h> |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 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 | |
Bernhard Rosenkraenzer | 84b256a | 2005-11-20 00:50:37 -0500 | [diff] [blame] | 46 | /* BIOS subsystem IDs */ |
| 47 | #define WIFI 0x35 |
| 48 | #define BLUETOOTH 0x34 |
| 49 | |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 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 | |
Dmitry Torokhov | a5b0cc80 | 2005-11-20 00:50:58 -0500 | [diff] [blame] | 63 | static struct platform_device *wistron_device; |
| 64 | |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 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 | |
Miloslav Trmac | c28c358 | 2006-01-10 01:59:07 -0500 | [diff] [blame] | 95 | static ssize_t __init locate_wistron_bios(void __iomem *base) |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 96 | { |
| 97 | static const unsigned char __initdata signature[] = |
| 98 | { 0x42, 0x21, 0x55, 0x30 }; |
Miloslav Trmac | c28c358 | 2006-01-10 01:59:07 -0500 | [diff] [blame] | 99 | ssize_t offset; |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 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; |
Miloslav Trmac | c28c358 | 2006-01-10 01:59:07 -0500 | [diff] [blame] | 112 | ssize_t offset; |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 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 | |
Dmitry Torokhov | 22a397e | 2005-11-20 00:50:46 -0500 | [diff] [blame] | 156 | static inline void unmap_bios(void) |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 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(®s, 0, sizeof (regs)); |
| 169 | regs.eax = 0x9610; |
| 170 | regs.ebx = 0x061C; |
| 171 | regs.ecx = 0x0000; |
| 172 | call_bios(®s); |
| 173 | |
| 174 | return regs.eax; |
| 175 | } |
| 176 | |
Dmitry Torokhov | e7c3aad | 2005-12-28 01:26:24 -0500 | [diff] [blame] | 177 | static void __devinit bios_attach(void) |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 178 | { |
| 179 | struct regs regs; |
| 180 | |
| 181 | memset(®s, 0, sizeof (regs)); |
| 182 | regs.eax = 0x9610; |
| 183 | regs.ebx = 0x012E; |
| 184 | call_bios(®s); |
| 185 | } |
| 186 | |
Dmitry Torokhov | 22a397e | 2005-11-20 00:50:46 -0500 | [diff] [blame] | 187 | static void bios_detach(void) |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 188 | { |
| 189 | struct regs regs; |
| 190 | |
| 191 | memset(®s, 0, sizeof (regs)); |
| 192 | regs.eax = 0x9610; |
| 193 | regs.ebx = 0x002E; |
| 194 | call_bios(®s); |
| 195 | } |
| 196 | |
Dmitry Torokhov | e7c3aad | 2005-12-28 01:26:24 -0500 | [diff] [blame] | 197 | static u8 __devinit bios_get_cmos_address(void) |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 198 | { |
| 199 | struct regs regs; |
| 200 | |
| 201 | memset(®s, 0, sizeof (regs)); |
| 202 | regs.eax = 0x9610; |
| 203 | regs.ebx = 0x051C; |
| 204 | call_bios(®s); |
| 205 | |
| 206 | return regs.ecx; |
| 207 | } |
| 208 | |
Dmitry Torokhov | e7c3aad | 2005-12-28 01:26:24 -0500 | [diff] [blame] | 209 | static u16 __devinit bios_get_default_setting(u8 subsys) |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 210 | { |
| 211 | struct regs regs; |
| 212 | |
| 213 | memset(®s, 0, sizeof (regs)); |
| 214 | regs.eax = 0x9610; |
Bernhard Rosenkraenzer | 84b256a | 2005-11-20 00:50:37 -0500 | [diff] [blame] | 215 | regs.ebx = 0x0200 | subsys; |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 216 | call_bios(®s); |
| 217 | |
| 218 | return regs.eax; |
| 219 | } |
| 220 | |
Bernhard Rosenkraenzer | 84b256a | 2005-11-20 00:50:37 -0500 | [diff] [blame] | 221 | static void bios_set_state(u8 subsys, int enable) |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 222 | { |
| 223 | struct regs regs; |
| 224 | |
| 225 | memset(®s, 0, sizeof (regs)); |
| 226 | regs.eax = 0x9610; |
Bernhard Rosenkraenzer | 84b256a | 2005-11-20 00:50:37 -0500 | [diff] [blame] | 227 | regs.ebx = (enable ? 0x0100 : 0x0000) | subsys; |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 228 | call_bios(®s); |
| 229 | } |
| 230 | |
Bernhard Rosenkraenzer | 84b256a | 2005-11-20 00:50:37 -0500 | [diff] [blame] | 231 | /* Hardware database */ |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 232 | |
| 233 | struct key_entry { |
| 234 | char type; /* See KE_* below */ |
| 235 | u8 code; |
| 236 | unsigned keycode; /* For KE_KEY */ |
| 237 | }; |
| 238 | |
Bernhard Rosenkraenzer | 84b256a | 2005-11-20 00:50:37 -0500 | [diff] [blame] | 239 | enum { KE_END, KE_KEY, KE_WIFI, KE_BLUETOOTH }; |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 240 | |
| 241 | static const struct key_entry *keymap; /* = NULL; Current key map */ |
| 242 | static int have_wifi; |
Bernhard Rosenkraenzer | 84b256a | 2005-11-20 00:50:37 -0500 | [diff] [blame] | 243 | static int have_bluetooth; |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 244 | |
| 245 | static 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 Rosenkraenzer | 84b256a | 2005-11-20 00:50:37 -0500 | [diff] [blame] | 254 | } else if (key->type == KE_BLUETOOTH) { |
| 255 | have_bluetooth = 1; |
| 256 | break; |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | return 1; |
| 260 | } |
| 261 | |
| 262 | static struct key_entry keymap_empty[] = { |
| 263 | { KE_END, 0 } |
| 264 | }; |
| 265 | |
| 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 }, |
| 273 | { KE_END, 0 } |
| 274 | }; |
| 275 | |
John Reed Riley | e2aa507 | 2006-04-05 00:40:01 -0400 | [diff] [blame] | 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 }, |
| 285 | { KE_END, 0 } |
| 286 | }; |
| 287 | |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 288 | static struct key_entry keymap_wistron_ms2141[] = { |
| 289 | { KE_KEY, 0x11, KEY_PROG1 }, |
| 290 | { KE_KEY, 0x12, KEY_PROG2 }, |
| 291 | { KE_WIFI, 0x30, 0 }, |
| 292 | { KE_KEY, 0x22, KEY_REWIND }, |
| 293 | { KE_KEY, 0x23, KEY_FORWARD }, |
| 294 | { KE_KEY, 0x24, KEY_PLAYPAUSE }, |
| 295 | { KE_KEY, 0x25, KEY_STOPCD }, |
| 296 | { KE_KEY, 0x31, KEY_MAIL }, |
| 297 | { KE_KEY, 0x36, KEY_WWW }, |
| 298 | { KE_END, 0 } |
| 299 | }; |
| 300 | |
Bernhard Rosenkraenzer | 84b256a | 2005-11-20 00:50:37 -0500 | [diff] [blame] | 301 | static struct key_entry keymap_acer_aspire_1500[] = { |
| 302 | { KE_KEY, 0x11, KEY_PROG1 }, |
| 303 | { KE_KEY, 0x12, KEY_PROG2 }, |
| 304 | { KE_WIFI, 0x30, 0 }, |
| 305 | { KE_KEY, 0x31, KEY_MAIL }, |
| 306 | { KE_KEY, 0x36, KEY_WWW }, |
| 307 | { KE_BLUETOOTH, 0x44, 0 }, |
| 308 | { KE_END, 0 } |
| 309 | }; |
| 310 | |
Ashutosh Naik | 74a89c9 | 2005-12-11 12:41:32 -0500 | [diff] [blame] | 311 | static struct key_entry keymap_acer_travelmate_240[] = { |
| 312 | { KE_KEY, 0x31, KEY_MAIL }, |
| 313 | { KE_KEY, 0x36, KEY_WWW }, |
| 314 | { KE_KEY, 0x11, KEY_PROG1 }, |
| 315 | { KE_KEY, 0x12, KEY_PROG2 }, |
| 316 | { KE_BLUETOOTH, 0x44, 0 }, |
| 317 | { KE_WIFI, 0x30, 0 }, |
| 318 | { KE_END, 0 } |
| 319 | }; |
| 320 | |
masc@theaterzentrum.at | e107b8e | 2006-05-29 23:29:36 -0400 | [diff] [blame] | 321 | static struct key_entry keymap_aopen_1559as[] = { |
| 322 | { KE_KEY, 0x01, KEY_HELP }, |
| 323 | { KE_KEY, 0x06, KEY_PROG3 }, |
| 324 | { KE_KEY, 0x11, KEY_PROG1 }, |
| 325 | { KE_KEY, 0x12, KEY_PROG2 }, |
| 326 | { KE_WIFI, 0x30, 0 }, |
| 327 | { KE_KEY, 0x31, KEY_MAIL }, |
| 328 | { KE_KEY, 0x36, KEY_WWW }, |
| 329 | }; |
| 330 | |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 331 | /* |
| 332 | * If your machine is not here (which is currently rather likely), please send |
| 333 | * a list of buttons and their key codes (reported when loading this module |
| 334 | * with force=1) and the output of dmidecode to $MODULE_AUTHOR. |
| 335 | */ |
| 336 | static struct dmi_system_id dmi_ids[] = { |
| 337 | { |
| 338 | .callback = dmi_matched, |
| 339 | .ident = "Fujitsu-Siemens Amilo Pro V2000", |
| 340 | .matches = { |
| 341 | DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"), |
| 342 | DMI_MATCH(DMI_PRODUCT_NAME, "AMILO Pro V2000"), |
| 343 | }, |
| 344 | .driver_data = keymap_fs_amilo_pro_v2000 |
| 345 | }, |
Bernhard Rosenkraenzer | 84b256a | 2005-11-20 00:50:37 -0500 | [diff] [blame] | 346 | { |
| 347 | .callback = dmi_matched, |
Stefan Rompf | 8a1b170 | 2006-04-05 00:39:20 -0400 | [diff] [blame] | 348 | .ident = "Fujitsu-Siemens Amilo M7400", |
| 349 | .matches = { |
| 350 | DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"), |
| 351 | DMI_MATCH(DMI_PRODUCT_NAME, "AMILO M "), |
| 352 | }, |
| 353 | .driver_data = keymap_fs_amilo_pro_v2000 |
| 354 | }, |
| 355 | { |
| 356 | .callback = dmi_matched, |
John Reed Riley | e2aa507 | 2006-04-05 00:40:01 -0400 | [diff] [blame] | 357 | .ident = "Fujitsu N3510", |
| 358 | .matches = { |
| 359 | DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU"), |
| 360 | DMI_MATCH(DMI_PRODUCT_NAME, "N3510"), |
| 361 | }, |
| 362 | .driver_data = keymap_fujitsu_n3510 |
| 363 | }, |
| 364 | { |
| 365 | .callback = dmi_matched, |
Bernhard Rosenkraenzer | 84b256a | 2005-11-20 00:50:37 -0500 | [diff] [blame] | 366 | .ident = "Acer Aspire 1500", |
| 367 | .matches = { |
| 368 | DMI_MATCH(DMI_SYS_VENDOR, "Acer"), |
| 369 | DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 1500"), |
| 370 | }, |
| 371 | .driver_data = keymap_acer_aspire_1500 |
| 372 | }, |
Ashutosh Naik | 74a89c9 | 2005-12-11 12:41:32 -0500 | [diff] [blame] | 373 | { |
| 374 | .callback = dmi_matched, |
| 375 | .ident = "Acer TravelMate 240", |
| 376 | .matches = { |
| 377 | DMI_MATCH(DMI_SYS_VENDOR, "Acer"), |
| 378 | DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 240"), |
| 379 | }, |
| 380 | .driver_data = keymap_acer_travelmate_240 |
| 381 | }, |
masc@theaterzentrum.at | e107b8e | 2006-05-29 23:29:36 -0400 | [diff] [blame] | 382 | { |
| 383 | .callback = dmi_matched, |
| 384 | .ident = "AOpen 1559AS", |
| 385 | .matches = { |
| 386 | DMI_MATCH(DMI_PRODUCT_NAME, "E2U"), |
| 387 | DMI_MATCH(DMI_BOARD_NAME, "E2U"), |
| 388 | }, |
| 389 | .driver_data = keymap_aopen_1559as |
| 390 | }, |
Al Viro | 81f0a91 | 2005-12-15 09:19:05 +0000 | [diff] [blame] | 391 | { NULL, } |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 392 | }; |
| 393 | |
| 394 | static int __init select_keymap(void) |
| 395 | { |
| 396 | if (keymap_name != NULL) { |
| 397 | if (strcmp (keymap_name, "1557/MS2141") == 0) |
| 398 | keymap = keymap_wistron_ms2141; |
| 399 | else { |
| 400 | printk(KERN_ERR "wistron_btns: Keymap unknown\n"); |
| 401 | return -EINVAL; |
| 402 | } |
| 403 | } |
| 404 | dmi_check_system(dmi_ids); |
| 405 | if (keymap == NULL) { |
| 406 | if (!force) { |
| 407 | printk(KERN_ERR "wistron_btns: System unknown\n"); |
| 408 | return -ENODEV; |
| 409 | } |
| 410 | keymap = keymap_empty; |
| 411 | } |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | /* Input layer interface */ |
| 416 | |
Dmitry Torokhov | 22a397e | 2005-11-20 00:50:46 -0500 | [diff] [blame] | 417 | static struct input_dev *input_dev; |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 418 | |
Dmitry Torokhov | e7c3aad | 2005-12-28 01:26:24 -0500 | [diff] [blame] | 419 | static int __devinit setup_input_dev(void) |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 420 | { |
| 421 | const struct key_entry *key; |
Dmitry Torokhov | 22a397e | 2005-11-20 00:50:46 -0500 | [diff] [blame] | 422 | int error; |
| 423 | |
| 424 | input_dev = input_allocate_device(); |
| 425 | if (!input_dev) |
| 426 | return -ENOMEM; |
| 427 | |
| 428 | input_dev->name = "Wistron laptop buttons"; |
| 429 | input_dev->phys = "wistron/input0"; |
| 430 | input_dev->id.bustype = BUS_HOST; |
Dmitry Torokhov | a5b0cc80 | 2005-11-20 00:50:58 -0500 | [diff] [blame] | 431 | input_dev->cdev.dev = &wistron_device->dev; |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 432 | |
| 433 | for (key = keymap; key->type != KE_END; key++) { |
| 434 | if (key->type == KE_KEY) { |
Dmitry Torokhov | 22a397e | 2005-11-20 00:50:46 -0500 | [diff] [blame] | 435 | input_dev->evbit[LONG(EV_KEY)] = BIT(EV_KEY); |
| 436 | set_bit(key->keycode, input_dev->keybit); |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
Dmitry Torokhov | 22a397e | 2005-11-20 00:50:46 -0500 | [diff] [blame] | 440 | error = input_register_device(input_dev); |
| 441 | if (error) { |
| 442 | input_free_device(input_dev); |
| 443 | return error; |
| 444 | } |
| 445 | |
| 446 | return 0; |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | static void report_key(unsigned keycode) |
| 450 | { |
Dmitry Torokhov | 22a397e | 2005-11-20 00:50:46 -0500 | [diff] [blame] | 451 | input_report_key(input_dev, keycode, 1); |
| 452 | input_sync(input_dev); |
| 453 | input_report_key(input_dev, keycode, 0); |
| 454 | input_sync(input_dev); |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /* Driver core */ |
| 458 | |
| 459 | static int wifi_enabled; |
Bernhard Rosenkraenzer | 84b256a | 2005-11-20 00:50:37 -0500 | [diff] [blame] | 460 | static int bluetooth_enabled; |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 461 | |
| 462 | static void poll_bios(unsigned long); |
| 463 | |
| 464 | static struct timer_list poll_timer = TIMER_INITIALIZER(poll_bios, 0, 0); |
| 465 | |
| 466 | static void handle_key(u8 code) |
| 467 | { |
| 468 | const struct key_entry *key; |
| 469 | |
| 470 | for (key = keymap; key->type != KE_END; key++) { |
| 471 | if (code == key->code) { |
| 472 | switch (key->type) { |
| 473 | case KE_KEY: |
| 474 | report_key(key->keycode); |
| 475 | break; |
| 476 | |
| 477 | case KE_WIFI: |
| 478 | if (have_wifi) { |
| 479 | wifi_enabled = !wifi_enabled; |
Bernhard Rosenkraenzer | 84b256a | 2005-11-20 00:50:37 -0500 | [diff] [blame] | 480 | bios_set_state(WIFI, wifi_enabled); |
| 481 | } |
| 482 | break; |
| 483 | |
| 484 | case KE_BLUETOOTH: |
| 485 | if (have_bluetooth) { |
| 486 | bluetooth_enabled = !bluetooth_enabled; |
| 487 | bios_set_state(BLUETOOTH, bluetooth_enabled); |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 488 | } |
| 489 | break; |
| 490 | |
| 491 | case KE_END: |
| 492 | default: |
| 493 | BUG(); |
| 494 | } |
| 495 | return; |
| 496 | } |
| 497 | } |
| 498 | printk(KERN_NOTICE "wistron_btns: Unknown key code %02X\n", code); |
| 499 | } |
| 500 | |
| 501 | static void poll_bios(unsigned long discard) |
| 502 | { |
| 503 | u8 qlen; |
| 504 | u16 val; |
| 505 | |
| 506 | for (;;) { |
| 507 | qlen = CMOS_READ(cmos_address); |
| 508 | if (qlen == 0) |
| 509 | break; |
| 510 | val = bios_pop_queue(); |
| 511 | if (val != 0 && !discard) |
| 512 | handle_key((u8)val); |
| 513 | } |
| 514 | |
| 515 | mod_timer(&poll_timer, jiffies + HZ / POLL_FREQUENCY); |
| 516 | } |
| 517 | |
Dmitry Torokhov | e7c3aad | 2005-12-28 01:26:24 -0500 | [diff] [blame] | 518 | static int __devinit wistron_probe(struct platform_device *dev) |
| 519 | { |
| 520 | int err = setup_input_dev(); |
| 521 | if (err) |
| 522 | return err; |
| 523 | |
| 524 | bios_attach(); |
| 525 | cmos_address = bios_get_cmos_address(); |
| 526 | |
| 527 | if (have_wifi) { |
| 528 | u16 wifi = bios_get_default_setting(WIFI); |
| 529 | if (wifi & 1) |
| 530 | wifi_enabled = (wifi & 2) ? 1 : 0; |
| 531 | else |
| 532 | have_wifi = 0; |
| 533 | |
| 534 | if (have_wifi) |
| 535 | bios_set_state(WIFI, wifi_enabled); |
| 536 | } |
| 537 | |
| 538 | if (have_bluetooth) { |
| 539 | u16 bt = bios_get_default_setting(BLUETOOTH); |
| 540 | if (bt & 1) |
| 541 | bluetooth_enabled = (bt & 2) ? 1 : 0; |
| 542 | else |
| 543 | have_bluetooth = 0; |
| 544 | |
| 545 | if (have_bluetooth) |
| 546 | bios_set_state(BLUETOOTH, bluetooth_enabled); |
| 547 | } |
| 548 | |
| 549 | poll_bios(1); /* Flush stale event queue and arm timer */ |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | static int __devexit wistron_remove(struct platform_device *dev) |
| 555 | { |
| 556 | del_timer_sync(&poll_timer); |
| 557 | input_unregister_device(input_dev); |
| 558 | bios_detach(); |
| 559 | |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | #ifdef CONFIG_PM |
Dmitry Torokhov | a5b0cc80 | 2005-11-20 00:50:58 -0500 | [diff] [blame] | 564 | static int wistron_suspend(struct platform_device *dev, pm_message_t state) |
| 565 | { |
| 566 | del_timer_sync(&poll_timer); |
| 567 | |
Miloslav Trmac | e753b65 | 2005-11-20 00:51:05 -0500 | [diff] [blame] | 568 | if (have_wifi) |
| 569 | bios_set_state(WIFI, 0); |
| 570 | |
| 571 | if (have_bluetooth) |
| 572 | bios_set_state(BLUETOOTH, 0); |
| 573 | |
Dmitry Torokhov | a5b0cc80 | 2005-11-20 00:50:58 -0500 | [diff] [blame] | 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | static int wistron_resume(struct platform_device *dev) |
| 578 | { |
| 579 | if (have_wifi) |
| 580 | bios_set_state(WIFI, wifi_enabled); |
| 581 | |
| 582 | if (have_bluetooth) |
| 583 | bios_set_state(BLUETOOTH, bluetooth_enabled); |
| 584 | |
| 585 | poll_bios(1); |
| 586 | |
| 587 | return 0; |
| 588 | } |
Dmitry Torokhov | e7c3aad | 2005-12-28 01:26:24 -0500 | [diff] [blame] | 589 | #else |
| 590 | #define wistron_suspend NULL |
| 591 | #define wistron_resume NULL |
| 592 | #endif |
Dmitry Torokhov | a5b0cc80 | 2005-11-20 00:50:58 -0500 | [diff] [blame] | 593 | |
| 594 | static struct platform_driver wistron_driver = { |
Dmitry Torokhov | a5b0cc80 | 2005-11-20 00:50:58 -0500 | [diff] [blame] | 595 | .driver = { |
| 596 | .name = "wistron-bios", |
Dmitry Torokhov | e7c3aad | 2005-12-28 01:26:24 -0500 | [diff] [blame] | 597 | .owner = THIS_MODULE, |
Dmitry Torokhov | a5b0cc80 | 2005-11-20 00:50:58 -0500 | [diff] [blame] | 598 | }, |
Dmitry Torokhov | e7c3aad | 2005-12-28 01:26:24 -0500 | [diff] [blame] | 599 | .probe = wistron_probe, |
| 600 | .remove = __devexit_p(wistron_remove), |
| 601 | .suspend = wistron_suspend, |
| 602 | .resume = wistron_resume, |
Dmitry Torokhov | a5b0cc80 | 2005-11-20 00:50:58 -0500 | [diff] [blame] | 603 | }; |
| 604 | |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 605 | static int __init wb_module_init(void) |
| 606 | { |
| 607 | int err; |
| 608 | |
| 609 | err = select_keymap(); |
| 610 | if (err) |
| 611 | return err; |
Dmitry Torokhov | 22a397e | 2005-11-20 00:50:46 -0500 | [diff] [blame] | 612 | |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 613 | err = map_bios(); |
| 614 | if (err) |
| 615 | return err; |
Dmitry Torokhov | 22a397e | 2005-11-20 00:50:46 -0500 | [diff] [blame] | 616 | |
Dmitry Torokhov | a5b0cc80 | 2005-11-20 00:50:58 -0500 | [diff] [blame] | 617 | err = platform_driver_register(&wistron_driver); |
| 618 | if (err) |
Dmitry Torokhov | e7c3aad | 2005-12-28 01:26:24 -0500 | [diff] [blame] | 619 | goto err_unmap_bios; |
Dmitry Torokhov | a5b0cc80 | 2005-11-20 00:50:58 -0500 | [diff] [blame] | 620 | |
Dmitry Torokhov | e7c3aad | 2005-12-28 01:26:24 -0500 | [diff] [blame] | 621 | wistron_device = platform_device_alloc("wistron-bios", -1); |
| 622 | if (!wistron_device) { |
| 623 | err = -ENOMEM; |
Dmitry Torokhov | a5b0cc80 | 2005-11-20 00:50:58 -0500 | [diff] [blame] | 624 | goto err_unregister_driver; |
| 625 | } |
| 626 | |
Dmitry Torokhov | e7c3aad | 2005-12-28 01:26:24 -0500 | [diff] [blame] | 627 | err = platform_device_add(wistron_device); |
Dmitry Torokhov | a5b0cc80 | 2005-11-20 00:50:58 -0500 | [diff] [blame] | 628 | if (err) |
Dmitry Torokhov | e7c3aad | 2005-12-28 01:26:24 -0500 | [diff] [blame] | 629 | goto err_free_device; |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 630 | |
| 631 | return 0; |
Dmitry Torokhov | a5b0cc80 | 2005-11-20 00:50:58 -0500 | [diff] [blame] | 632 | |
Dmitry Torokhov | e7c3aad | 2005-12-28 01:26:24 -0500 | [diff] [blame] | 633 | err_free_device: |
| 634 | platform_device_put(wistron_device); |
Dmitry Torokhov | a5b0cc80 | 2005-11-20 00:50:58 -0500 | [diff] [blame] | 635 | err_unregister_driver: |
| 636 | platform_driver_unregister(&wistron_driver); |
Dmitry Torokhov | e7c3aad | 2005-12-28 01:26:24 -0500 | [diff] [blame] | 637 | err_unmap_bios: |
Dmitry Torokhov | a5b0cc80 | 2005-11-20 00:50:58 -0500 | [diff] [blame] | 638 | unmap_bios(); |
| 639 | |
| 640 | return err; |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | static void __exit wb_module_exit(void) |
| 644 | { |
Dmitry Torokhov | a5b0cc80 | 2005-11-20 00:50:58 -0500 | [diff] [blame] | 645 | platform_device_unregister(wistron_device); |
| 646 | platform_driver_unregister(&wistron_driver); |
Dmitry Torokhov | 5fc14680 | 2005-11-20 00:50:06 -0500 | [diff] [blame] | 647 | unmap_bios(); |
| 648 | } |
| 649 | |
| 650 | module_init(wb_module_init); |
| 651 | module_exit(wb_module_exit); |