[kernel] gpio_buttons: include slab.h
[openwrt.git] / target / linux / generic-2.6 / files / drivers / input / misc / gpio_buttons.c
index 0d219ff..eb0e301 100644 (file)
@@ -1,7 +1,8 @@
 /*
  *  Driver for buttons on GPIO lines not capable of generating interrupts
  *
- *  Copyright (C) 2007,2008 Gabor Juhos <juhosg at openwrt.org>
+ *  Copyright (C) 2007-2010 Gabor Juhos <juhosg@openwrt.org>
+ *  Copyright (C) 2010 Nuno Goncalves <nunojpg@gmail.com>
  *
  *  This file was based on: /drivers/input/misc/cobalt_btns.c
  *     Copyright (C) 2007 Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp>
  *
  */
 
+#include <linux/kernel.h>
+#include <linux/module.h>
 #include <linux/init.h>
+#include <linux/slab.h>
+
+#include <linux/input.h>
 #include <linux/input-polldev.h>
 #include <linux/ioport.h>
-#include <linux/module.h>
 #include <linux/platform_device.h>
 
 #include <linux/gpio_buttons.h>
 #include <asm/gpio.h>
 
 #define DRV_NAME       "gpio-buttons"
-#define DRV_VERSION    "0.1.0"
+#define DRV_VERSION    "0.1.2"
 #define PFX            DRV_NAME ": "
 
+struct gpio_button_data {
+       int last_state;
+       int count;
+};
+
 struct gpio_buttons_dev {
        struct input_polled_dev *poll_dev;
        struct gpio_buttons_platform_data *pdata;
+       struct gpio_button_data *data;
 };
 
 static void gpio_buttons_poll(struct input_polled_dev *dev)
@@ -46,22 +57,18 @@ static void gpio_buttons_poll(struct input_polled_dev *dev)
                unsigned int type = button->type ?: EV_KEY;
                int state;
 
-               state = gpio_get_value(button->gpio) ? 1 : 0;
-               state ^= button->active_low;
-
-               if (state) {
-                       button->count++;
-               } else {
-                       if (button->count >= button->threshold) {
-                               input_event(input, type, button->code, 1);
-                               input_sync(input);
-                       }
-                       button->count = 0;
+               if (bdev->data[i].count < button->threshold) {
+                       bdev->data[i].count++;
+                       continue;
                }
 
-               if (button->count == button->threshold) {
-                       input_event(input, type, button->code, 0);
+               state = gpio_get_value(button->gpio) ? 1 : 0;
+               if (state != bdev->data[i].last_state) {
+                       input_event(input, type, button->code,
+                                   !!(state ^ button->active_low));
                        input_sync(input);
+                       bdev->data[i].count = 0;
+                       bdev->data[i].last_state = state;
                }
        }
 }
@@ -74,19 +81,22 @@ static int __devinit gpio_buttons_probe(struct platform_device *pdev)
        struct input_dev *input;
        int error, i;
 
-
        if (!pdata)
                return -ENXIO;
 
-       bdev = kzalloc(sizeof(*bdev), GFP_KERNEL);
+       bdev = kzalloc(sizeof(struct gpio_buttons_dev) +
+                      sizeof(struct gpio_button_data) * pdata->nbuttons,
+                      GFP_KERNEL);
        if (!bdev) {
-               pr_err(DRV_NAME "no memory for device\n");
+               printk(KERN_ERR DRV_NAME "no memory for device\n");
                return -ENOMEM;
        }
 
+       bdev->data = (struct gpio_button_data *) &bdev[1];
+
        poll_dev = input_allocate_polled_device();
        if (!poll_dev) {
-               pr_err(DRV_NAME "no memory for polled device\n");
+               printk(KERN_ERR DRV_NAME "no memory for polled device\n");
                error = -ENOMEM;
                goto err_free_bdev;
        }
@@ -97,7 +107,7 @@ static int __devinit gpio_buttons_probe(struct platform_device *pdev)
 
        input = poll_dev->input;
 
-       input->evbit[0] = BIT_MASK(EV_KEY);
+       input->evbit[0] = BIT(EV_KEY);
        input->name = pdev->name;
        input->phys = "gpio-buttons/input0";
        input->dev.parent = &pdev->dev;
@@ -115,20 +125,20 @@ static int __devinit gpio_buttons_probe(struct platform_device *pdev)
                error = gpio_request(gpio, button->desc ?
                                button->desc : DRV_NAME);
                if (error) {
-                       pr_err(PFX "unable to claim gpio %u, error %d\n",
-                               gpio, error);
+                       printk(KERN_ERR PFX "unable to claim gpio %u, "
+                               "error %d\n", gpio, error);
                        goto err_free_gpio;
                }
 
                error = gpio_direction_input(gpio);
                if (error) {
-                       pr_err(PFX "unable to set direction on gpio %u, "
-                               "error %d\n", gpio, error);
+                       printk(KERN_ERR PFX "unable to set direction on "
+                               "gpio %u, error %d\n", gpio, error);
                        goto err_free_gpio;
                }
 
                input_set_capability(input, type, button->code);
-               button->count = 0;
+               bdev->data[i].last_state = gpio_get_value(button->gpio) ? 1 : 0;
        }
 
        bdev->poll_dev = poll_dev;
@@ -137,8 +147,8 @@ static int __devinit gpio_buttons_probe(struct platform_device *pdev)
 
        error = input_register_polled_device(poll_dev);
        if (error) {
-               pr_err(PFX "unable to register polled device, error %d\n",
-                           error);
+               printk(KERN_ERR PFX "unable to register polled device, "
+                       "error %d\n", error);
                goto err_free_gpio;
        }
 
@@ -152,7 +162,7 @@ err_free_gpio:
 
 err_free_bdev:
        kfree(bdev);
-err:
+
        platform_set_drvdata(pdev, NULL);
        return error;
 }
@@ -187,7 +197,7 @@ static struct platform_driver gpio_buttons_driver = {
 
 static int __init gpio_buttons_init(void)
 {
-       pr_info(DRV_NAME " driver version " DRV_VERSION "\n");
+       printk(KERN_INFO DRV_NAME " driver version " DRV_VERSION "\n");
        return platform_driver_register(&gpio_buttons_driver);
 }