Merge branch 'nfs-server-stable' of git://linux-nfs.org/~bfields/linux
[pandora-kernel.git] / drivers / input / misc / wistron_btns.c
index b9ef1df..b438d99 100644 (file)
 #include <linux/io.h>
 #include <linux/dmi.h>
 #include <linux/init.h>
-#include <linux/input.h>
+#include <linux/input-polldev.h>
 #include <linux/interrupt.h>
+#include <linux/jiffies.h>
 #include <linux/kernel.h>
 #include <linux/mc146818rtc.h>
 #include <linux/module.h>
 #include <linux/preempt.h>
 #include <linux/string.h>
-#include <linux/timer.h>
 #include <linux/types.h>
 #include <linux/platform_device.h>
+#include <linux/leds.h>
 
-/*
- * Number of attempts to read data from queue per poll;
- * the queue can hold up to 31 entries
- */
-#define MAX_POLL_ITERATIONS 64
-
-#define POLL_FREQUENCY 10 /* Number of polls per second */
-
-#if POLL_FREQUENCY > HZ
-#error "POLL_FREQUENCY too high"
-#endif
+/* How often we poll keys - msecs */
+#define POLL_INTERVAL_DEFAULT  500 /* when idle */
+#define POLL_INTERVAL_BURST    100 /* when a key was recently pressed */
 
 /* BIOS subsystem IDs */
 #define WIFI           0x35
 #define BLUETOOTH      0x34
+#define MAIL_LED       0x31
 
 MODULE_AUTHOR("Miloslav Trmac <mitr@volny.cz>");
 MODULE_DESCRIPTION("Wistron laptop button driver");
 MODULE_LICENSE("GPL v2");
-MODULE_VERSION("0.1");
+MODULE_VERSION("0.3");
 
 static int force; /* = 0; */
 module_param(force, bool, 0);
@@ -248,11 +242,12 @@ enum { KE_END, KE_KEY, KE_SW, KE_WIFI, KE_BLUETOOTH };
 #define FE_WIFI_LED 0x02
 #define FE_UNTESTED 0x80
 
-static const struct key_entry *keymap; /* = NULL; Current key map */
+static struct key_entry *keymap; /* = NULL; Current key map */
 static int have_wifi;
 static int have_bluetooth;
+static int have_leds;
 
-static int __init dmi_matched(struct dmi_system_id *dmi)
+static int __init dmi_matched(const struct dmi_system_id *dmi)
 {
        const struct key_entry *key;
 
@@ -263,14 +258,16 @@ static int __init dmi_matched(struct dmi_system_id *dmi)
                else if (key->type == KE_BLUETOOTH)
                        have_bluetooth = 1;
        }
+       have_leds = key->code & (FE_MAIL_LED | FE_WIFI_LED);
+
        return 1;
 }
 
-static struct key_entry keymap_empty[] = {
+static struct key_entry keymap_empty[] __initdata = {
        { KE_END, 0 }
 };
 
-static struct key_entry keymap_fs_amilo_pro_v2000[] = {
+static struct key_entry keymap_fs_amilo_pro_v2000[] __initdata = {
        { KE_KEY,  0x01, {KEY_HELP} },
        { KE_KEY,  0x11, {KEY_PROG1} },
        { KE_KEY,  0x12, {KEY_PROG2} },
@@ -280,7 +277,7 @@ static struct key_entry keymap_fs_amilo_pro_v2000[] = {
        { KE_END,  0 }
 };
 
-static struct key_entry keymap_fujitsu_n3510[] = {
+static struct key_entry keymap_fujitsu_n3510[] __initdata = {
        { KE_KEY, 0x11, {KEY_PROG1} },
        { KE_KEY, 0x12, {KEY_PROG2} },
        { KE_KEY, 0x36, {KEY_WWW} },
@@ -292,7 +289,7 @@ static struct key_entry keymap_fujitsu_n3510[] = {
        { KE_END, 0 }
 };
 
-static struct key_entry keymap_wistron_ms2111[] = {
+static struct key_entry keymap_wistron_ms2111[] __initdata = {
        { KE_KEY,  0x11, {KEY_PROG1} },
        { KE_KEY,  0x12, {KEY_PROG2} },
        { KE_KEY,  0x13, {KEY_PROG3} },
@@ -301,7 +298,7 @@ static struct key_entry keymap_wistron_ms2111[] = {
        { KE_END, FE_MAIL_LED }
 };
 
-static struct key_entry keymap_wistron_md40100[] = {
+static struct key_entry keymap_wistron_md40100[] __initdata = {
        { KE_KEY, 0x01, {KEY_HELP} },
        { KE_KEY, 0x02, {KEY_CONFIG} },
        { KE_KEY, 0x31, {KEY_MAIL} },
@@ -310,7 +307,7 @@ static struct key_entry keymap_wistron_md40100[] = {
        { KE_END, FE_MAIL_LED | FE_WIFI_LED | FE_UNTESTED }
 };
 
-static struct key_entry keymap_wistron_ms2141[] = {
+static struct key_entry keymap_wistron_ms2141[] __initdata = {
        { KE_KEY,  0x11, {KEY_PROG1} },
        { KE_KEY,  0x12, {KEY_PROG2} },
        { KE_WIFI, 0x30 },
@@ -323,7 +320,7 @@ static struct key_entry keymap_wistron_ms2141[] = {
        { KE_END,  0 }
 };
 
-static struct key_entry keymap_acer_aspire_1500[] = {
+static struct key_entry keymap_acer_aspire_1500[] __initdata = {
        { KE_KEY, 0x01, {KEY_HELP} },
        { KE_KEY, 0x03, {KEY_POWER} },
        { KE_KEY, 0x11, {KEY_PROG1} },
@@ -336,7 +333,7 @@ static struct key_entry keymap_acer_aspire_1500[] = {
        { KE_END, FE_UNTESTED }
 };
 
-static struct key_entry keymap_acer_aspire_1600[] = {
+static struct key_entry keymap_acer_aspire_1600[] __initdata = {
        { KE_KEY, 0x01, {KEY_HELP} },
        { KE_KEY, 0x03, {KEY_POWER} },
        { KE_KEY, 0x08, {KEY_MUTE} },
@@ -352,7 +349,7 @@ static struct key_entry keymap_acer_aspire_1600[] = {
 };
 
 /* 3020 has been tested */
-static struct key_entry keymap_acer_aspire_5020[] = {
+static struct key_entry keymap_acer_aspire_5020[] __initdata = {
        { KE_KEY, 0x01, {KEY_HELP} },
        { KE_KEY, 0x03, {KEY_POWER} },
        { KE_KEY, 0x05, {KEY_SWITCHVIDEOMODE} }, /* Display selection */
@@ -366,7 +363,7 @@ static struct key_entry keymap_acer_aspire_5020[] = {
        { KE_END, FE_MAIL_LED | FE_UNTESTED }
 };
 
-static struct key_entry keymap_acer_travelmate_2410[] = {
+static struct key_entry keymap_acer_travelmate_2410[] __initdata = {
        { KE_KEY, 0x01, {KEY_HELP} },
        { KE_KEY, 0x6d, {KEY_POWER} },
        { KE_KEY, 0x11, {KEY_PROG1} },
@@ -379,7 +376,7 @@ static struct key_entry keymap_acer_travelmate_2410[] = {
        { KE_END, FE_MAIL_LED | FE_UNTESTED }
 };
 
-static struct key_entry keymap_acer_travelmate_110[] = {
+static struct key_entry keymap_acer_travelmate_110[] __initdata = {
        { KE_KEY, 0x01, {KEY_HELP} },
        { KE_KEY, 0x02, {KEY_CONFIG} },
        { KE_KEY, 0x03, {KEY_POWER} },
@@ -396,7 +393,7 @@ static struct key_entry keymap_acer_travelmate_110[] = {
        { KE_END, FE_MAIL_LED | FE_UNTESTED }
 };
 
-static struct key_entry keymap_acer_travelmate_300[] = {
+static struct key_entry keymap_acer_travelmate_300[] __initdata = {
        { KE_KEY, 0x01, {KEY_HELP} },
        { KE_KEY, 0x02, {KEY_CONFIG} },
        { KE_KEY, 0x03, {KEY_POWER} },
@@ -412,7 +409,7 @@ static struct key_entry keymap_acer_travelmate_300[] = {
        { KE_END, FE_MAIL_LED | FE_UNTESTED }
 };
 
-static struct key_entry keymap_acer_travelmate_380[] = {
+static struct key_entry keymap_acer_travelmate_380[] __initdata = {
        { KE_KEY, 0x01, {KEY_HELP} },
        { KE_KEY, 0x02, {KEY_CONFIG} },
        { KE_KEY, 0x03, {KEY_POWER} }, /* not 370 */
@@ -426,7 +423,7 @@ static struct key_entry keymap_acer_travelmate_380[] = {
 };
 
 /* unusual map */
-static struct key_entry keymap_acer_travelmate_220[] = {
+static struct key_entry keymap_acer_travelmate_220[] __initdata = {
        { KE_KEY, 0x01, {KEY_HELP} },
        { KE_KEY, 0x02, {KEY_CONFIG} },
        { KE_KEY, 0x11, {KEY_MAIL} },
@@ -436,7 +433,7 @@ static struct key_entry keymap_acer_travelmate_220[] = {
        { KE_END, FE_WIFI_LED | FE_UNTESTED }
 };
 
-static struct key_entry keymap_acer_travelmate_230[] = {
+static struct key_entry keymap_acer_travelmate_230[] __initdata = {
        { KE_KEY, 0x01, {KEY_HELP} },
        { KE_KEY, 0x02, {KEY_CONFIG} },
        { KE_KEY, 0x11, {KEY_PROG1} },
@@ -446,7 +443,7 @@ static struct key_entry keymap_acer_travelmate_230[] = {
        { KE_END, FE_WIFI_LED | FE_UNTESTED }
 };
 
-static struct key_entry keymap_acer_travelmate_240[] = {
+static struct key_entry keymap_acer_travelmate_240[] __initdata = {
        { KE_KEY, 0x01, {KEY_HELP} },
        { KE_KEY, 0x02, {KEY_CONFIG} },
        { KE_KEY, 0x03, {KEY_POWER} },
@@ -460,7 +457,7 @@ static struct key_entry keymap_acer_travelmate_240[] = {
        { KE_END, FE_UNTESTED }
 };
 
-static struct key_entry keymap_acer_travelmate_350[] = {
+static struct key_entry keymap_acer_travelmate_350[] __initdata = {
        { KE_KEY, 0x01, {KEY_HELP} },
        { KE_KEY, 0x02, {KEY_CONFIG} },
        { KE_KEY, 0x11, {KEY_PROG1} },
@@ -471,7 +468,7 @@ static struct key_entry keymap_acer_travelmate_350[] = {
        { KE_END, FE_MAIL_LED | FE_WIFI_LED | FE_UNTESTED }
 };
 
-static struct key_entry keymap_acer_travelmate_360[] = {
+static struct key_entry keymap_acer_travelmate_360[] __initdata = {
        { KE_KEY, 0x01, {KEY_HELP} },
        { KE_KEY, 0x02, {KEY_CONFIG} },
        { KE_KEY, 0x11, {KEY_PROG1} },
@@ -486,11 +483,11 @@ static struct key_entry keymap_acer_travelmate_360[] = {
 /* Wifi subsystem only activates the led. Therefore we need to pass
  * wifi event as a normal key, then userspace can really change the wifi state.
  * TODO we need to export led state to userspace (wifi and mail) */
-static struct key_entry keymap_acer_travelmate_610[] = {
+static struct key_entry keymap_acer_travelmate_610[] __initdata = {
        { KE_KEY, 0x01, {KEY_HELP} },
        { KE_KEY, 0x02, {KEY_CONFIG} },
        { KE_KEY, 0x11, {KEY_PROG1} },
-       { KE_KEY, 0x12, {KEY_PROG3} },
+       { KE_KEY, 0x12, {KEY_PROG2} },
        { KE_KEY, 0x13, {KEY_PROG3} },
        { KE_KEY, 0x14, {KEY_MAIL} },
        { KE_KEY, 0x15, {KEY_WWW} },
@@ -498,7 +495,7 @@ static struct key_entry keymap_acer_travelmate_610[] = {
        { KE_END, FE_MAIL_LED | FE_WIFI_LED }
 };
 
-static struct key_entry keymap_acer_travelmate_630[] = {
+static struct key_entry keymap_acer_travelmate_630[] __initdata = {
        { KE_KEY, 0x01, {KEY_HELP} },
        { KE_KEY, 0x02, {KEY_CONFIG} },
        { KE_KEY, 0x03, {KEY_POWER} },
@@ -514,7 +511,7 @@ static struct key_entry keymap_acer_travelmate_630[] = {
        { KE_END, FE_MAIL_LED | FE_UNTESTED }
 };
 
-static struct key_entry keymap_aopen_1559as[] = {
+static struct key_entry keymap_aopen_1559as[] __initdata = {
        { KE_KEY,  0x01, {KEY_HELP} },
        { KE_KEY,  0x06, {KEY_PROG3} },
        { KE_KEY,  0x11, {KEY_PROG1} },
@@ -525,7 +522,7 @@ static struct key_entry keymap_aopen_1559as[] = {
        { KE_END,  0 },
 };
 
-static struct key_entry keymap_fs_amilo_d88x0[] = {
+static struct key_entry keymap_fs_amilo_d88x0[] __initdata = {
        { KE_KEY, 0x01, {KEY_HELP} },
        { KE_KEY, 0x08, {KEY_MUTE} },
        { KE_KEY, 0x31, {KEY_MAIL} },
@@ -536,7 +533,7 @@ static struct key_entry keymap_fs_amilo_d88x0[] = {
        { KE_END, FE_MAIL_LED | FE_WIFI_LED | FE_UNTESTED }
 };
 
-static struct key_entry keymap_wistron_md2900[] = {
+static struct key_entry keymap_wistron_md2900[] __initdata = {
        { KE_KEY, 0x01, {KEY_HELP} },
        { KE_KEY, 0x02, {KEY_CONFIG} },
        { KE_KEY, 0x11, {KEY_PROG1} },
@@ -547,7 +544,7 @@ static struct key_entry keymap_wistron_md2900[] = {
        { KE_END, FE_MAIL_LED | FE_UNTESTED }
 };
 
-static struct key_entry keymap_wistron_md96500[] = {
+static struct key_entry keymap_wistron_md96500[] __initdata = {
        { KE_KEY, 0x01, {KEY_HELP} },
        { KE_KEY, 0x02, {KEY_CONFIG} },
        { KE_KEY, 0x05, {KEY_SWITCHVIDEOMODE} }, /* Display selection */
@@ -568,7 +565,7 @@ static struct key_entry keymap_wistron_md96500[] = {
        { KE_END, FE_UNTESTED }
 };
 
-static struct key_entry keymap_wistron_generic[] = {
+static struct key_entry keymap_wistron_generic[] __initdata = {
        { KE_KEY, 0x01, {KEY_HELP} },
        { KE_KEY, 0x02, {KEY_CONFIG} },
        { KE_KEY, 0x03, {KEY_POWER} },
@@ -920,6 +917,26 @@ static struct dmi_system_id dmi_ids[] __initdata = {
        { NULL, }
 };
 
+/* Copy the good keymap, as the original ones are free'd */
+static int __init copy_keymap(void)
+{
+       const struct key_entry *key;
+       struct key_entry *new_keymap;
+       unsigned int length = 1;
+
+       for (key = keymap; key->type != KE_END; key++)
+               length++;
+
+       new_keymap = kmalloc(length * sizeof(struct key_entry), GFP_KERNEL);
+       if (!new_keymap)
+               return -ENOMEM;
+
+       memcpy(new_keymap, keymap, length * sizeof(struct key_entry));
+       keymap = new_keymap;
+
+       return 0;
+}
+
 static int __init select_keymap(void)
 {
        dmi_check_system(dmi_ids);
@@ -940,123 +957,169 @@ static int __init select_keymap(void)
                }
                keymap = keymap_empty;
        }
-       return 0;
+
+       return copy_keymap();
 }
 
  /* Input layer interface */
 
-static struct input_dev *input_dev;
+static struct input_polled_dev *wistron_idev;
+static unsigned long jiffies_last_press;
+static int wifi_enabled;
+static int bluetooth_enabled;
 
-static int __devinit setup_input_dev(void)
+static void report_key(struct input_dev *dev, unsigned int keycode)
 {
-       const struct key_entry *key;
-       int error;
+       input_report_key(dev, keycode, 1);
+       input_sync(dev);
+       input_report_key(dev, keycode, 0);
+       input_sync(dev);
+}
 
-       input_dev = input_allocate_device();
-       if (!input_dev)
-               return -ENOMEM;
+static void report_switch(struct input_dev *dev, unsigned int code, int value)
+{
+       input_report_switch(dev, code, value);
+       input_sync(dev);
+}
 
-       input_dev->name = "Wistron laptop buttons";
-       input_dev->phys = "wistron/input0";
-       input_dev->id.bustype = BUS_HOST;
-       input_dev->cdev.dev = &wistron_device->dev;
 
-       for (key = keymap; key->type != KE_END; key++) {
-               switch (key->type) {
-                       case KE_KEY:
-                               set_bit(EV_KEY, input_dev->evbit);
-                               set_bit(key->keycode, input_dev->keybit);
-                               break;
+ /* led management */
+static void wistron_mail_led_set(struct led_classdev *led_cdev,
+                               enum led_brightness value)
+{
+       bios_set_state(MAIL_LED, (value != LED_OFF) ? 1 : 0);
+}
 
-                       case KE_SW:
-                               set_bit(EV_SW, input_dev->evbit);
-                               set_bit(key->sw.code, input_dev->swbit);
-                               break;
+/* same as setting up wifi card, but for laptops on which the led is managed */
+static void wistron_wifi_led_set(struct led_classdev *led_cdev,
+                               enum led_brightness value)
+{
+       bios_set_state(WIFI, (value != LED_OFF) ? 1 : 0);
+}
 
-                       default:
-                               ;
-               }
-       }
+static struct led_classdev wistron_mail_led = {
+       .name                   = "mail:green",
+       .brightness_set         = wistron_mail_led_set,
+};
 
-       /* reads information flags on KE_END */
-       if (key->code & FE_UNTESTED)
-               printk(KERN_WARNING "Untested laptop multimedia keys, "
-                       "please report success or failure to eric.piel"
-                       "@tremplin-utc.net\n");
+static struct led_classdev wistron_wifi_led = {
+       .name                   = "wifi:red",
+       .brightness_set         = wistron_wifi_led_set,
+};
 
-       error = input_register_device(input_dev);
-       if (error) {
-               input_free_device(input_dev);
-               return error;
+static void __devinit wistron_led_init(struct device *parent)
+{
+       if (have_leds & FE_WIFI_LED) {
+               u16 wifi = bios_get_default_setting(WIFI);
+               if (wifi & 1) {
+                       wistron_wifi_led.brightness = (wifi & 2) ? LED_FULL : LED_OFF;
+                       if (led_classdev_register(parent, &wistron_wifi_led))
+                               have_leds &= ~FE_WIFI_LED;
+                       else
+                               bios_set_state(WIFI, wistron_wifi_led.brightness);
+
+               } else
+                       have_leds &= ~FE_WIFI_LED;
        }
 
-       return 0;
+       if (have_leds & FE_MAIL_LED) {
+               /* bios_get_default_setting(MAIL) always retuns 0, so just turn the led off */
+               wistron_mail_led.brightness = LED_OFF;
+               if (led_classdev_register(parent, &wistron_mail_led))
+                       have_leds &= ~FE_MAIL_LED;
+               else
+                       bios_set_state(MAIL_LED, wistron_mail_led.brightness);
+       }
 }
 
-static void report_key(unsigned keycode)
+static void __devexit wistron_led_remove(void)
 {
-       input_report_key(input_dev, keycode, 1);
-       input_sync(input_dev);
-       input_report_key(input_dev, keycode, 0);
-       input_sync(input_dev);
+       if (have_leds & FE_MAIL_LED)
+               led_classdev_unregister(&wistron_mail_led);
+
+       if (have_leds & FE_WIFI_LED)
+               led_classdev_unregister(&wistron_wifi_led);
 }
 
-static void report_switch(unsigned code, int value)
+static inline void wistron_led_suspend(void)
 {
-       input_report_switch(input_dev, code, value);
-       input_sync(input_dev);
+       if (have_leds & FE_MAIL_LED)
+               led_classdev_suspend(&wistron_mail_led);
+
+       if (have_leds & FE_WIFI_LED)
+               led_classdev_suspend(&wistron_wifi_led);
 }
 
- /* Driver core */
+static inline void wistron_led_resume(void)
+{
+       if (have_leds & FE_MAIL_LED)
+               led_classdev_resume(&wistron_mail_led);
 
-static int wifi_enabled;
-static int bluetooth_enabled;
+       if (have_leds & FE_WIFI_LED)
+               led_classdev_resume(&wistron_wifi_led);
+}
 
-static void poll_bios(unsigned long);
+static struct key_entry *wistron_get_entry_by_scancode(int code)
+{
+       struct key_entry *key;
 
-static struct timer_list poll_timer = TIMER_INITIALIZER(poll_bios, 0, 0);
+       for (key = keymap; key->type != KE_END; key++)
+               if (code == key->code)
+                       return key;
 
-static void handle_key(u8 code)
+       return NULL;
+}
+
+static struct key_entry *wistron_get_entry_by_keycode(int keycode)
 {
-       const struct key_entry *key;
+       struct key_entry *key;
 
-       for (key = keymap; key->type != KE_END; key++) {
-               if (code == key->code) {
-                       switch (key->type) {
-                       case KE_KEY:
-                               report_key(key->keycode);
-                               break;
+       for (key = keymap; key->type != KE_END; key++)
+               if (key->type == KE_KEY && keycode == key->keycode)
+                       return key;
 
-                       case KE_SW:
-                               report_switch(key->sw.code, key->sw.value);
-                               break;
+       return NULL;
+}
 
-                       case KE_WIFI:
-                               if (have_wifi) {
-                                       wifi_enabled = !wifi_enabled;
-                                       bios_set_state(WIFI, wifi_enabled);
-                               }
-                               break;
+static void handle_key(u8 code)
+{
+       const struct key_entry *key = wistron_get_entry_by_scancode(code);
 
-                       case KE_BLUETOOTH:
-                               if (have_bluetooth) {
-                                       bluetooth_enabled = !bluetooth_enabled;
-                                       bios_set_state(BLUETOOTH, bluetooth_enabled);
-                               }
-                               break;
+       if (key) {
+               switch (key->type) {
+               case KE_KEY:
+                       report_key(wistron_idev->input, key->keycode);
+                       break;
 
-                       case KE_END:
-                               break;
-                       default:
-                               BUG();
+               case KE_SW:
+                       report_switch(wistron_idev->input,
+                                     key->sw.code, key->sw.value);
+                       break;
+
+               case KE_WIFI:
+                       if (have_wifi) {
+                               wifi_enabled = !wifi_enabled;
+                               bios_set_state(WIFI, wifi_enabled);
+                       }
+                       break;
+
+               case KE_BLUETOOTH:
+                       if (have_bluetooth) {
+                               bluetooth_enabled = !bluetooth_enabled;
+                               bios_set_state(BLUETOOTH, bluetooth_enabled);
                        }
-                       return;
+                       break;
+
+               default:
+                       BUG();
                }
-       }
-       printk(KERN_NOTICE "wistron_btns: Unknown key code %02X\n", code);
+               jiffies_last_press = jiffies;
+       } else
+               printk(KERN_NOTICE
+                       "wistron_btns: Unknown key code %02X\n", code);
 }
 
-static void poll_bios(unsigned long discard)
+static void poll_bios(bool discard)
 {
        u8 qlen;
        u16 val;
@@ -1069,15 +1132,118 @@ static void poll_bios(unsigned long discard)
                if (val != 0 && !discard)
                        handle_key((u8)val);
        }
+}
+
+static void wistron_flush(struct input_polled_dev *dev)
+{
+       /* Flush stale event queue */
+       poll_bios(true);
+}
 
-       mod_timer(&poll_timer, jiffies + HZ / POLL_FREQUENCY);
+static void wistron_poll(struct input_polled_dev *dev)
+{
+       poll_bios(false);
+
+       /* Increase poll frequency if user is currently pressing keys (< 2s ago) */
+       if (time_before(jiffies, jiffies_last_press + 2 * HZ))
+               dev->poll_interval = POLL_INTERVAL_BURST;
+       else
+               dev->poll_interval = POLL_INTERVAL_DEFAULT;
 }
 
+static int wistron_getkeycode(struct input_dev *dev, int scancode, int *keycode)
+{
+       const struct key_entry *key = wistron_get_entry_by_scancode(scancode);
+
+       if (key && key->type == KE_KEY) {
+               *keycode = key->keycode;
+               return 0;
+       }
+
+       return -EINVAL;
+}
+
+static int wistron_setkeycode(struct input_dev *dev, int scancode, int keycode)
+{
+       struct key_entry *key;
+       int old_keycode;
+
+       if (keycode < 0 || keycode > KEY_MAX)
+               return -EINVAL;
+
+       key = wistron_get_entry_by_scancode(scancode);
+       if (key && key->type == KE_KEY) {
+               old_keycode = key->keycode;
+               key->keycode = keycode;
+               set_bit(keycode, dev->keybit);
+               if (!wistron_get_entry_by_keycode(old_keycode))
+                       clear_bit(old_keycode, dev->keybit);
+               return 0;
+       }
+
+       return -EINVAL;
+}
+
+static int __devinit setup_input_dev(void)
+{
+       const struct key_entry *key;
+       struct input_dev *input_dev;
+       int error;
+
+       wistron_idev = input_allocate_polled_device();
+       if (!wistron_idev)
+               return -ENOMEM;
+
+       wistron_idev->flush = wistron_flush;
+       wistron_idev->poll = wistron_poll;
+       wistron_idev->poll_interval = POLL_INTERVAL_DEFAULT;
+
+       input_dev = wistron_idev->input;
+       input_dev->name = "Wistron laptop buttons";
+       input_dev->phys = "wistron/input0";
+       input_dev->id.bustype = BUS_HOST;
+       input_dev->dev.parent = &wistron_device->dev;
+
+       input_dev->getkeycode = wistron_getkeycode;
+       input_dev->setkeycode = wistron_setkeycode;
+
+       for (key = keymap; key->type != KE_END; key++) {
+               switch (key->type) {
+                       case KE_KEY:
+                               set_bit(EV_KEY, input_dev->evbit);
+                               set_bit(key->keycode, input_dev->keybit);
+                               break;
+
+                       case KE_SW:
+                               set_bit(EV_SW, input_dev->evbit);
+                               set_bit(key->sw.code, input_dev->swbit);
+                               break;
+
+                       default:
+                               break;
+               }
+       }
+
+       /* reads information flags on KE_END */
+       if (key->code & FE_UNTESTED)
+               printk(KERN_WARNING "Untested laptop multimedia keys, "
+                       "please report success or failure to eric.piel"
+                       "@tremplin-utc.net\n");
+
+       error = input_register_polled_device(wistron_idev);
+       if (error) {
+               input_free_polled_device(wistron_idev);
+               return error;
+       }
+
+       return 0;
+}
+
+/* Driver core */
+
 static int __devinit wistron_probe(struct platform_device *dev)
 {
-       int err = setup_input_dev();
-       if (err)
-               return err;
+       int err;
 
        bios_attach();
        cmos_address = bios_get_cmos_address();
@@ -1104,15 +1270,21 @@ static int __devinit wistron_probe(struct platform_device *dev)
                        bios_set_state(BLUETOOTH, bluetooth_enabled);
        }
 
-       poll_bios(1); /* Flush stale event queue and arm timer */
+       wistron_led_init(&dev->dev);
+       err = setup_input_dev();
+       if (err) {
+               bios_detach();
+               return err;
+       }
 
        return 0;
 }
 
 static int __devexit wistron_remove(struct platform_device *dev)
 {
-       del_timer_sync(&poll_timer);
-       input_unregister_device(input_dev);
+       wistron_led_remove();
+       input_unregister_polled_device(wistron_idev);
+       input_free_polled_device(wistron_idev);
        bios_detach();
 
        return 0;
@@ -1121,14 +1293,13 @@ static int __devexit wistron_remove(struct platform_device *dev)
 #ifdef CONFIG_PM
 static int wistron_suspend(struct platform_device *dev, pm_message_t state)
 {
-       del_timer_sync(&poll_timer);
-
        if (have_wifi)
                bios_set_state(WIFI, 0);
 
        if (have_bluetooth)
                bios_set_state(BLUETOOTH, 0);
 
+       wistron_led_suspend();
        return 0;
 }
 
@@ -1140,7 +1311,8 @@ static int wistron_resume(struct platform_device *dev)
        if (have_bluetooth)
                bios_set_state(BLUETOOTH, bluetooth_enabled);
 
-       poll_bios(1);
+       wistron_led_resume();
+       poll_bios(true);
 
        return 0;
 }
@@ -1203,6 +1375,7 @@ static void __exit wb_module_exit(void)
        platform_device_unregister(wistron_device);
        platform_driver_unregister(&wistron_driver);
        unmap_bios();
+       kfree(keymap);
 }
 
 module_init(wb_module_init);