gpio-button-hotplug: improve gpio button debouncing, verify state changes over multip...
[openwrt/openwrt.git] / package / kernel / gpio-button-hotplug / src / gpio-button-hotplug.c
1 /*
2 * GPIO Button Hotplug driver
3 *
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 * Copyright (C) 2008-2010 Gabor Juhos <juhosg@openwrt.org>
6 *
7 * Based on the diag.c - GPIO interface driver for Broadcom boards
8 * Copyright (C) 2006 Mike Baker <mbm@openwrt.org>,
9 * Copyright (C) 2006-2007 Felix Fietkau <nbd@openwrt.org>
10 * Copyright (C) 2008 Andy Boyett <agb@openwrt.org>
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License version 2 as published
14 * by the Free Software Foundation.
15 */
16
17 #include <linux/module.h>
18 #include <linux/version.h>
19 #include <linux/kmod.h>
20
21 #include <linux/workqueue.h>
22 #include <linux/skbuff.h>
23 #include <linux/netlink.h>
24 #include <linux/kobject.h>
25 #include <linux/input.h>
26 #include <linux/platform_device.h>
27 #include <linux/of_gpio.h>
28 #include <linux/gpio_keys.h>
29
30 #define DRV_NAME "gpio-keys-polled"
31
32 #define BH_SKB_SIZE 2048
33
34 #define PFX DRV_NAME ": "
35
36 #undef BH_DEBUG
37
38 #ifdef BH_DEBUG
39 #define BH_DBG(fmt, args...) printk(KERN_DEBUG "%s: " fmt, DRV_NAME, ##args )
40 #else
41 #define BH_DBG(fmt, args...) do {} while (0)
42 #endif
43
44 #define BH_ERR(fmt, args...) printk(KERN_ERR "%s: " fmt, DRV_NAME, ##args )
45
46 struct bh_priv {
47 unsigned long seen;
48 };
49
50 struct bh_event {
51 const char *name;
52 char *action;
53 unsigned long seen;
54
55 struct sk_buff *skb;
56 struct work_struct work;
57 };
58
59 struct bh_map {
60 unsigned int code;
61 const char *name;
62 };
63
64 struct gpio_keys_button_data {
65 struct delayed_work work;
66 struct bh_priv bh;
67 int last_state;
68 int count;
69 int threshold;
70 int can_sleep;
71 };
72
73 extern u64 uevent_next_seqnum(void);
74
75 #define BH_MAP(_code, _name) \
76 { \
77 .code = (_code), \
78 .name = (_name), \
79 }
80
81 static struct bh_map button_map[] = {
82 BH_MAP(BTN_0, "BTN_0"),
83 BH_MAP(BTN_1, "BTN_1"),
84 BH_MAP(BTN_2, "BTN_2"),
85 BH_MAP(BTN_3, "BTN_3"),
86 BH_MAP(BTN_4, "BTN_4"),
87 BH_MAP(BTN_5, "BTN_5"),
88 BH_MAP(BTN_6, "BTN_6"),
89 BH_MAP(BTN_7, "BTN_7"),
90 BH_MAP(BTN_8, "BTN_8"),
91 BH_MAP(BTN_9, "BTN_9"),
92 BH_MAP(KEY_RESTART, "reset"),
93 BH_MAP(KEY_RFKILL, "rfkill"),
94 #ifdef KEY_WPS_BUTTON
95 BH_MAP(KEY_WPS_BUTTON, "wps"),
96 #endif /* KEY_WPS_BUTTON */
97 };
98
99 /* -------------------------------------------------------------------------*/
100
101 static int bh_event_add_var(struct bh_event *event, int argv,
102 const char *format, ...)
103 {
104 static char buf[128];
105 char *s;
106 va_list args;
107 int len;
108
109 if (argv)
110 return 0;
111
112 va_start(args, format);
113 len = vsnprintf(buf, sizeof(buf), format, args);
114 va_end(args);
115
116 if (len >= sizeof(buf)) {
117 BH_ERR("buffer size too small\n");
118 WARN_ON(1);
119 return -ENOMEM;
120 }
121
122 s = skb_put(event->skb, len + 1);
123 strcpy(s, buf);
124
125 BH_DBG("added variable '%s'\n", s);
126
127 return 0;
128 }
129
130 static int button_hotplug_fill_event(struct bh_event *event)
131 {
132 int ret;
133
134 ret = bh_event_add_var(event, 0, "HOME=%s", "/");
135 if (ret)
136 return ret;
137
138 ret = bh_event_add_var(event, 0, "PATH=%s",
139 "/sbin:/bin:/usr/sbin:/usr/bin");
140 if (ret)
141 return ret;
142
143 ret = bh_event_add_var(event, 0, "SUBSYSTEM=%s", "button");
144 if (ret)
145 return ret;
146
147 ret = bh_event_add_var(event, 0, "ACTION=%s", event->action);
148 if (ret)
149 return ret;
150
151 ret = bh_event_add_var(event, 0, "BUTTON=%s", event->name);
152 if (ret)
153 return ret;
154
155 ret = bh_event_add_var(event, 0, "SEEN=%ld", event->seen);
156 if (ret)
157 return ret;
158
159 ret = bh_event_add_var(event, 0, "SEQNUM=%llu", uevent_next_seqnum());
160
161 return ret;
162 }
163
164 static void button_hotplug_work(struct work_struct *work)
165 {
166 struct bh_event *event = container_of(work, struct bh_event, work);
167 int ret = 0;
168
169 event->skb = alloc_skb(BH_SKB_SIZE, GFP_KERNEL);
170 if (!event->skb)
171 goto out_free_event;
172
173 ret = bh_event_add_var(event, 0, "%s@", event->action);
174 if (ret)
175 goto out_free_skb;
176
177 ret = button_hotplug_fill_event(event);
178 if (ret)
179 goto out_free_skb;
180
181 NETLINK_CB(event->skb).dst_group = 1;
182 broadcast_uevent(event->skb, 0, 1, GFP_KERNEL);
183
184 out_free_skb:
185 if (ret) {
186 BH_ERR("work error %d\n", ret);
187 kfree_skb(event->skb);
188 }
189 out_free_event:
190 kfree(event);
191 }
192
193 static int button_hotplug_create_event(const char *name, unsigned long seen,
194 int pressed)
195 {
196 struct bh_event *event;
197
198 BH_DBG("create event, name=%s, seen=%lu, pressed=%d\n",
199 name, seen, pressed);
200
201 event = kzalloc(sizeof(*event), GFP_KERNEL);
202 if (!event)
203 return -ENOMEM;
204
205 event->name = name;
206 event->seen = seen;
207 event->action = pressed ? "pressed" : "released";
208
209 INIT_WORK(&event->work, (void *)(void *)button_hotplug_work);
210 schedule_work(&event->work);
211
212 return 0;
213 }
214
215 /* -------------------------------------------------------------------------*/
216
217 #ifdef CONFIG_HOTPLUG
218 static int button_get_index(unsigned int code)
219 {
220 int i;
221
222 for (i = 0; i < ARRAY_SIZE(button_map); i++)
223 if (button_map[i].code == code)
224 return i;
225
226 return -1;
227 }
228 static void button_hotplug_event(struct gpio_keys_button_data *data,
229 unsigned int type, unsigned int code, int value)
230 {
231 struct bh_priv *priv = &data->bh;
232 unsigned long seen = jiffies;
233 int btn;
234
235 BH_DBG("event type=%u, code=%u, value=%d\n", type, code, value);
236
237 if (type != EV_KEY)
238 return;
239
240 btn = button_get_index(code);
241 if (btn < 0)
242 return;
243
244 button_hotplug_create_event(button_map[btn].name,
245 (seen - priv->seen) / HZ, value);
246 priv->seen = seen;
247 }
248 #else
249 static void button_hotplug_event(struct gpio_keys_button_data *data,
250 unsigned int type, unsigned int code, int value)
251 {
252 }
253 #endif /* CONFIG_HOTPLUG */
254
255 struct gpio_keys_polled_dev {
256 struct delayed_work work;
257
258 struct device *dev;
259 struct gpio_keys_platform_data *pdata;
260 struct gpio_keys_button_data data[0];
261 };
262
263 static void gpio_keys_polled_check_state(struct gpio_keys_button *button,
264 struct gpio_keys_button_data *bdata)
265 {
266 int state;
267
268 if (bdata->can_sleep)
269 state = !!gpio_get_value_cansleep(button->gpio);
270 else
271 state = !!gpio_get_value(button->gpio);
272
273 state = !!(state ^ button->active_low);
274 if (state != bdata->last_state) {
275 unsigned int type = button->type ?: EV_KEY;
276
277 if (bdata->count < bdata->threshold) {
278 bdata->count++;
279 return;
280 }
281
282 button_hotplug_event(bdata, type, button->code, state);
283 bdata->last_state = state;
284 }
285
286 bdata->count = 0;
287 }
288
289 static void gpio_keys_polled_queue_work(struct gpio_keys_polled_dev *bdev)
290 {
291 struct gpio_keys_platform_data *pdata = bdev->pdata;
292 unsigned long delay = msecs_to_jiffies(pdata->poll_interval);
293
294 if (delay >= HZ)
295 delay = round_jiffies_relative(delay);
296 schedule_delayed_work(&bdev->work, delay);
297 }
298
299 static void gpio_keys_polled_poll(struct work_struct *work)
300 {
301 struct gpio_keys_polled_dev *bdev =
302 container_of(work, struct gpio_keys_polled_dev, work.work);
303 struct gpio_keys_platform_data *pdata = bdev->pdata;
304 int i;
305
306 for (i = 0; i < bdev->pdata->nbuttons; i++) {
307 struct gpio_keys_button_data *bdata = &bdev->data[i];
308 gpio_keys_polled_check_state(&pdata->buttons[i], bdata);
309 }
310 gpio_keys_polled_queue_work(bdev);
311 }
312
313 static void gpio_keys_polled_open(struct gpio_keys_polled_dev *bdev)
314 {
315 struct gpio_keys_platform_data *pdata = bdev->pdata;
316 int i;
317
318 if (pdata->enable)
319 pdata->enable(bdev->dev);
320
321 /* report initial state of the buttons */
322 for (i = 0; i < pdata->nbuttons; i++)
323 gpio_keys_polled_check_state(&pdata->buttons[i], &bdev->data[i]);
324
325 gpio_keys_polled_queue_work(bdev);
326 }
327
328 #ifdef CONFIG_OF
329 static struct gpio_keys_platform_data *
330 gpio_keys_polled_get_devtree_pdata(struct device *dev)
331 {
332 struct device_node *node, *pp;
333 struct gpio_keys_platform_data *pdata;
334 struct gpio_keys_button *button;
335 int error;
336 int nbuttons;
337 int i;
338
339 node = dev->of_node;
340 if (!node)
341 return NULL;
342
343 nbuttons = of_get_child_count(node);
344 if (nbuttons == 0)
345 return NULL;
346
347 pdata = kzalloc(sizeof(*pdata) + nbuttons * (sizeof *button),
348 GFP_KERNEL);
349 if (!pdata) {
350 error = -ENOMEM;
351 goto err_out;
352 }
353
354 pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
355 pdata->nbuttons = nbuttons;
356
357 pdata->rep = !!of_get_property(node, "autorepeat", NULL);
358 of_property_read_u32(node, "poll-interval", &pdata->poll_interval);
359
360 i = 0;
361 for_each_child_of_node(node, pp) {
362 enum of_gpio_flags flags;
363
364 if (!of_find_property(pp, "gpios", NULL)) {
365 pdata->nbuttons--;
366 dev_warn(dev, "Found button without gpios\n");
367 continue;
368 }
369
370 button = &pdata->buttons[i++];
371
372 button->gpio = of_get_gpio_flags(pp, 0, &flags);
373 button->active_low = flags & OF_GPIO_ACTIVE_LOW;
374
375 if (of_property_read_u32(pp, "linux,code", &button->code)) {
376 dev_err(dev, "Button without keycode: 0x%x\n",
377 button->gpio);
378 error = -EINVAL;
379 goto err_free_pdata;
380 }
381
382 button->desc = of_get_property(pp, "label", NULL);
383
384 if (of_property_read_u32(pp, "linux,input-type", &button->type))
385 button->type = EV_KEY;
386
387 button->wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL);
388
389 if (of_property_read_u32(pp, "debounce-interval",
390 &button->debounce_interval))
391 button->debounce_interval = 5;
392 }
393
394 if (pdata->nbuttons == 0) {
395 error = -EINVAL;
396 goto err_free_pdata;
397 }
398
399 return pdata;
400
401 err_free_pdata:
402 kfree(pdata);
403 err_out:
404 return ERR_PTR(error);
405 }
406
407 static struct of_device_id gpio_keys_polled_of_match[] = {
408 { .compatible = "gpio-keys-polled", },
409 { },
410 };
411 MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match);
412
413 #else
414
415 static inline struct gpio_keys_platform_data *
416 gpio_keys_polled_get_devtree_pdata(struct device *dev)
417 {
418 return NULL;
419 }
420 #endif
421
422 static void gpio_keys_polled_close(struct gpio_keys_polled_dev *bdev)
423 {
424 struct gpio_keys_platform_data *pdata = bdev->pdata;
425
426 cancel_delayed_work_sync(&bdev->work);
427
428 if (pdata->disable)
429 pdata->disable(bdev->dev);
430 }
431
432 static int gpio_keys_polled_probe(struct platform_device *pdev)
433 {
434 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
435 struct device *dev = &pdev->dev;
436 struct gpio_keys_polled_dev *bdev;
437 int error;
438 int i;
439
440 if (!pdata) {
441 pdata = gpio_keys_polled_get_devtree_pdata(dev);
442 if (IS_ERR(pdata))
443 return PTR_ERR(pdata);
444 if (!pdata) {
445 dev_err(dev, "missing platform data\n");
446 return -EINVAL;
447 }
448 }
449
450 if (!pdata->poll_interval) {
451 dev_err(dev, "missing poll_interval value\n");
452 error = -EINVAL;
453 goto err_free_pdata;
454 }
455
456 bdev = kzalloc(sizeof(struct gpio_keys_polled_dev) +
457 pdata->nbuttons * sizeof(struct gpio_keys_button_data),
458 GFP_KERNEL);
459 if (!bdev) {
460 dev_err(dev, "no memory for private data\n");
461 return -ENOMEM;
462 }
463
464 for (i = 0; i < pdata->nbuttons; i++) {
465 struct gpio_keys_button *button = &pdata->buttons[i];
466 struct gpio_keys_button_data *bdata = &bdev->data[i];
467 unsigned int gpio = button->gpio;
468
469 if (button->wakeup) {
470 dev_err(dev, DRV_NAME " does not support wakeup\n");
471 error = -EINVAL;
472 goto err_free_gpio;
473 }
474
475 error = gpio_request(gpio,
476 button->desc ? button->desc : DRV_NAME);
477 if (error) {
478 dev_err(dev, "unable to claim gpio %u, err=%d\n",
479 gpio, error);
480 goto err_free_gpio;
481 }
482
483 error = gpio_direction_input(gpio);
484 if (error) {
485 dev_err(dev,
486 "unable to set direction on gpio %u, err=%d\n",
487 gpio, error);
488 goto err_free_gpio;
489 }
490
491 bdata->can_sleep = gpio_cansleep(gpio);
492 bdata->last_state = 0;
493 bdata->threshold = DIV_ROUND_UP(button->debounce_interval,
494 pdata->poll_interval);
495 }
496
497 bdev->dev = &pdev->dev;
498 bdev->pdata = pdata;
499 platform_set_drvdata(pdev, bdev);
500
501 INIT_DELAYED_WORK(&bdev->work, gpio_keys_polled_poll);
502
503 gpio_keys_polled_open(bdev);
504
505 return 0;
506
507 err_free_gpio:
508 while (--i >= 0)
509 gpio_free(pdata->buttons[i].gpio);
510
511 kfree(bdev);
512 platform_set_drvdata(pdev, NULL);
513
514 err_free_pdata:
515 /* If we have no platform_data, we allocated pdata dynamically. */
516 if (!dev_get_platdata(&pdev->dev))
517 kfree(pdata);
518
519 return error;
520 }
521
522 static int gpio_keys_polled_remove(struct platform_device *pdev)
523 {
524 struct gpio_keys_polled_dev *bdev = platform_get_drvdata(pdev);
525 struct gpio_keys_platform_data *pdata = bdev->pdata;
526 int i = pdata->nbuttons;
527
528 gpio_keys_polled_close(bdev);
529
530 while (--i >= 0)
531 gpio_free(pdata->buttons[i].gpio);
532
533 kfree(bdev);
534 platform_set_drvdata(pdev, NULL);
535
536 return 0;
537 }
538
539 static struct platform_driver gpio_keys_polled_driver = {
540 .probe = gpio_keys_polled_probe,
541 .remove = gpio_keys_polled_remove,
542 .driver = {
543 .name = DRV_NAME,
544 .owner = THIS_MODULE,
545 .of_match_table = of_match_ptr(gpio_keys_polled_of_match),
546 },
547 };
548
549 static int __init gpio_keys_polled_init(void)
550 {
551 return platform_driver_register(&gpio_keys_polled_driver);
552 }
553
554 static void __exit gpio_keys_polled_exit(void)
555 {
556 platform_driver_unregister(&gpio_keys_polled_driver);
557 }
558
559 module_init(gpio_keys_polled_init);
560 module_exit(gpio_keys_polled_exit);
561
562 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
563 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
564 MODULE_DESCRIPTION("Polled GPIO Buttons hotplug driver");
565 MODULE_LICENSE("GPL v2");
566 MODULE_ALIAS("platform:" DRV_NAME);