gpio-button-hotplug: add support for power buttons
[openwrt.git] / package / kernel / gpio-button-hotplug / src / gpio-button-hotplug.c
index 0b3140c0bff45e8e4c1f80c7f371ea9e7721f463..927f37dccf2f12cd251647d3879a2a811ee86432 100644 (file)
@@ -90,6 +90,7 @@ static struct bh_map button_map[] = {
        BH_MAP(BTN_7,           "BTN_7"),
        BH_MAP(BTN_8,           "BTN_8"),
        BH_MAP(BTN_9,           "BTN_9"),
+       BH_MAP(KEY_POWER,       "power"),
        BH_MAP(KEY_RESTART,     "reset"),
        BH_MAP(KEY_RFKILL,      "rfkill"),
        BH_MAP(KEY_WPS_BUTTON,  "wps"),
@@ -128,6 +129,7 @@ static int bh_event_add_var(struct bh_event *event, int argv,
 
 static int button_hotplug_fill_event(struct bh_event *event)
 {
+       char *s;
        int ret;
 
        ret = bh_event_add_var(event, 0, "HOME=%s", "/");
@@ -139,14 +141,11 @@ static int button_hotplug_fill_event(struct bh_event *event)
        if (ret)
                return ret;
 
-       char *s;
        switch (event->type) {
-               case EV_KEY:
-                       s = "button";
-                       break;
                case EV_SW:
                        s = "switch";
                        break;
+               case EV_KEY:
                default:
                        s = "button";
                        break;
@@ -274,17 +273,24 @@ struct gpio_keys_polled_dev {
        struct gpio_keys_button_data data[0];
 };
 
-static void gpio_keys_polled_check_state(struct gpio_keys_button *button,
-                                        struct gpio_keys_button_data *bdata)
+static int gpio_button_get_value(struct gpio_keys_button *button,
+                                struct gpio_keys_button_data *bdata)
 {
-       int state;
+       int val;
 
        if (bdata->can_sleep)
-               state = !!gpio_get_value_cansleep(button->gpio);
+               val = !!gpio_get_value_cansleep(button->gpio);
        else
-               state = !!gpio_get_value(button->gpio);
+               val = !!gpio_get_value(button->gpio);
+
+       return val ^ button->active_low;
+}
+
+static void gpio_keys_polled_check_state(struct gpio_keys_button *button,
+                                        struct gpio_keys_button_data *bdata)
+{
+       int state = gpio_button_get_value(button, bdata);
 
-       state = !!(state ^ button->active_low);
        if (state != bdata->last_state) {
                unsigned int type = button->type ?: EV_KEY;
 
@@ -293,7 +299,9 @@ static void gpio_keys_polled_check_state(struct gpio_keys_button *button,
                        return;
                }
 
-               button_hotplug_event(bdata, type, button->code, state);
+               if (bdata->last_state != -1)
+                       button_hotplug_event(bdata, type, button->code, state);
+
                bdata->last_state = state;
        }
 
@@ -503,7 +511,7 @@ static int gpio_keys_polled_probe(struct platform_device *pdev)
                }
 
                bdata->can_sleep = gpio_cansleep(gpio);
-               bdata->last_state = 0;
+               bdata->last_state = -1;
                bdata->threshold = DIV_ROUND_UP(button->debounce_interval,
                                                pdata->poll_interval);
        }