- patches.apparmor/remove_suid_new_case_in_2.6.22.diff: Merge fix.
[linux-flexiantxendom0-3.2.10.git] / drivers / input / keyboard / gpio_keys.c
1 /*
2  * Driver for keys on GPIO lines capable of generating interrupts.
3  *
4  * Copyright 2005 Phil Blundell
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/module.h>
12 #include <linux/version.h>
13
14 #include <linux/init.h>
15 #include <linux/fs.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
18 #include <linux/sched.h>
19 #include <linux/pm.h>
20 #include <linux/sysctl.h>
21 #include <linux/proc_fs.h>
22 #include <linux/delay.h>
23 #include <linux/platform_device.h>
24 #include <linux/input.h>
25 #include <linux/irq.h>
26 #include <linux/gpio_keys.h>
27
28 #include <asm/gpio.h>
29
30 static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
31 {
32         int i;
33         struct platform_device *pdev = dev_id;
34         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
35         struct input_dev *input = platform_get_drvdata(pdev);
36
37         for (i = 0; i < pdata->nbuttons; i++) {
38                 struct gpio_keys_button *button = &pdata->buttons[i];
39                 int gpio = button->gpio;
40
41                 if (irq == gpio_to_irq(gpio)) {
42                         unsigned int type = button->type ?: EV_KEY;
43                         int state = (gpio_get_value(gpio) ? 1 : 0) ^ button->active_low;
44
45                         input_event(input, type, button->code, !!state);
46                         input_sync(input);
47                 }
48         }
49
50         return IRQ_HANDLED;
51 }
52
53 static int __devinit gpio_keys_probe(struct platform_device *pdev)
54 {
55         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
56         struct input_dev *input;
57         int i, error;
58
59         input = input_allocate_device();
60         if (!input)
61                 return -ENOMEM;
62
63         platform_set_drvdata(pdev, input);
64
65         input->evbit[0] = BIT(EV_KEY);
66
67         input->name = pdev->name;
68         input->phys = "gpio-keys/input0";
69         input->dev.parent = &pdev->dev;
70
71         input->id.bustype = BUS_HOST;
72         input->id.vendor = 0x0001;
73         input->id.product = 0x0001;
74         input->id.version = 0x0100;
75
76         for (i = 0; i < pdata->nbuttons; i++) {
77                 struct gpio_keys_button *button = &pdata->buttons[i];
78                 int irq = gpio_to_irq(button->gpio);
79                 unsigned int type = button->type ?: EV_KEY;
80
81                 set_irq_type(irq, IRQ_TYPE_EDGE_BOTH);
82                 error = request_irq(irq, gpio_keys_isr, IRQF_SAMPLE_RANDOM,
83                                      button->desc ? button->desc : "gpio_keys",
84                                      pdev);
85                 if (error) {
86                         printk(KERN_ERR "gpio-keys: unable to claim irq %d; error %d\n",
87                                 irq, error);
88                         goto fail;
89                 }
90
91                 input_set_capability(input, type, button->code);
92         }
93
94         error = input_register_device(input);
95         if (error) {
96                 printk(KERN_ERR "Unable to register gpio-keys input device\n");
97                 goto fail;
98         }
99
100         return 0;
101
102  fail:
103         for (i = i - 1; i >= 0; i--)
104                 free_irq(gpio_to_irq(pdata->buttons[i].gpio), pdev);
105
106         input_free_device(input);
107
108         return error;
109 }
110
111 static int __devexit gpio_keys_remove(struct platform_device *pdev)
112 {
113         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
114         struct input_dev *input = platform_get_drvdata(pdev);
115         int i;
116
117         for (i = 0; i < pdata->nbuttons; i++) {
118                 int irq = gpio_to_irq(pdata->buttons[i].gpio);
119                 free_irq(irq, pdev);
120         }
121
122         input_unregister_device(input);
123
124         return 0;
125 }
126
127 struct platform_driver gpio_keys_device_driver = {
128         .probe          = gpio_keys_probe,
129         .remove         = __devexit_p(gpio_keys_remove),
130         .driver         = {
131                 .name   = "gpio-keys",
132         }
133 };
134
135 static int __init gpio_keys_init(void)
136 {
137         return platform_driver_register(&gpio_keys_device_driver);
138 }
139
140 static void __exit gpio_keys_exit(void)
141 {
142         platform_driver_unregister(&gpio_keys_device_driver);
143 }
144
145 module_init(gpio_keys_init);
146 module_exit(gpio_keys_exit);
147
148 MODULE_LICENSE("GPL");
149 MODULE_AUTHOR("Phil Blundell <pb@handhelds.org>");
150 MODULE_DESCRIPTION("Keyboard driver for CPU GPIOs");