gpio-button-hotplug: fix wrong initial seen value
[oweals/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@nbd.name>
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@nbd.name>
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/interrupt.h>
27 #include <linux/platform_device.h>
28 #include <linux/of_gpio.h>
29 #include <linux/of_irq.h>
30 #include <linux/gpio_keys.h>
31
32 #define BH_SKB_SIZE     2048
33
34 #define DRV_NAME        "gpio-keys"
35 #define PFX     DRV_NAME ": "
36
37 struct bh_priv {
38         unsigned long           seen;
39 };
40
41 struct bh_event {
42         const char              *name;
43         unsigned int            type;
44         char                    *action;
45         unsigned long           seen;
46
47         struct sk_buff          *skb;
48         struct work_struct      work;
49 };
50
51 struct bh_map {
52         unsigned int    code;
53         const char      *name;
54 };
55
56 struct gpio_keys_button_data {
57         struct delayed_work work;
58         struct bh_priv bh;
59         int last_state;
60         int count;
61         int threshold;
62         int can_sleep;
63         int irq;
64         unsigned int software_debounce;
65         struct gpio_desc *gpiod;
66         const struct gpio_keys_button *b;
67 };
68
69 extern u64 uevent_next_seqnum(void);
70
71 #define BH_MAP(_code, _name)            \
72         {                               \
73                 .code = (_code),        \
74                 .name = (_name),        \
75         }
76
77 static struct bh_map button_map[] = {
78         BH_MAP(BTN_0,                   "BTN_0"),
79         BH_MAP(BTN_1,                   "BTN_1"),
80         BH_MAP(BTN_2,                   "BTN_2"),
81         BH_MAP(BTN_3,                   "BTN_3"),
82         BH_MAP(BTN_4,                   "BTN_4"),
83         BH_MAP(BTN_5,                   "BTN_5"),
84         BH_MAP(BTN_6,                   "BTN_6"),
85         BH_MAP(BTN_7,                   "BTN_7"),
86         BH_MAP(BTN_8,                   "BTN_8"),
87         BH_MAP(BTN_9,                   "BTN_9"),
88         BH_MAP(KEY_BRIGHTNESS_ZERO,     "brightness_zero"),
89         BH_MAP(KEY_CONFIG,              "config"),
90         BH_MAP(KEY_COPY,                "copy"),
91         BH_MAP(KEY_EJECTCD,             "eject"),
92         BH_MAP(KEY_HELP,                "help"),
93         BH_MAP(KEY_LIGHTS_TOGGLE,       "lights_toggle"),
94         BH_MAP(KEY_PHONE,               "phone"),
95         BH_MAP(KEY_POWER,               "power"),
96         BH_MAP(KEY_POWER2,              "reboot"),
97         BH_MAP(KEY_RESTART,             "reset"),
98         BH_MAP(KEY_RFKILL,              "rfkill"),
99         BH_MAP(KEY_VIDEO,               "video"),
100         BH_MAP(KEY_WIMAX,               "wwan"),
101         BH_MAP(KEY_WLAN,                "wlan"),
102         BH_MAP(KEY_WPS_BUTTON,          "wps"),
103 };
104
105 /* -------------------------------------------------------------------------*/
106
107 static __printf(3, 4)
108 int bh_event_add_var(struct bh_event *event, int argv, const char *format, ...)
109 {
110         static char buf[128];
111         char *s;
112         va_list args;
113         int len;
114
115         if (argv)
116                 return 0;
117
118         va_start(args, format);
119         len = vsnprintf(buf, sizeof(buf), format, args);
120         va_end(args);
121
122         if (len >= sizeof(buf)) {
123                 WARN(1, "buffer size too small");
124                 return -ENOMEM;
125         }
126
127         s = skb_put(event->skb, len + 1);
128         strcpy(s, buf);
129
130         pr_debug(PFX "added variable '%s'\n", s);
131
132         return 0;
133 }
134
135 static int button_hotplug_fill_event(struct bh_event *event)
136 {
137         int ret;
138
139         ret = bh_event_add_var(event, 0, "HOME=%s", "/");
140         if (ret)
141                 return ret;
142
143         ret = bh_event_add_var(event, 0, "PATH=%s",
144                                         "/sbin:/bin:/usr/sbin:/usr/bin");
145         if (ret)
146                 return ret;
147
148         ret = bh_event_add_var(event, 0, "SUBSYSTEM=%s", "button");
149         if (ret)
150                 return ret;
151
152         ret = bh_event_add_var(event, 0, "ACTION=%s", event->action);
153         if (ret)
154                 return ret;
155
156         ret = bh_event_add_var(event, 0, "BUTTON=%s", event->name);
157         if (ret)
158                 return ret;
159
160         if (event->type == EV_SW) {
161                 ret = bh_event_add_var(event, 0, "TYPE=%s", "switch");
162                 if (ret)
163                         return ret;
164         }
165
166         ret = bh_event_add_var(event, 0, "SEEN=%ld", event->seen);
167         if (ret)
168                 return ret;
169
170         ret = bh_event_add_var(event, 0, "SEQNUM=%llu", uevent_next_seqnum());
171
172         return ret;
173 }
174
175 static void button_hotplug_work(struct work_struct *work)
176 {
177         struct bh_event *event = container_of(work, struct bh_event, work);
178         int ret = 0;
179
180         event->skb = alloc_skb(BH_SKB_SIZE, GFP_KERNEL);
181         if (!event->skb)
182                 goto out_free_event;
183
184         ret = bh_event_add_var(event, 0, "%s@", event->action);
185         if (ret)
186                 goto out_free_skb;
187
188         ret = button_hotplug_fill_event(event);
189         if (ret)
190                 goto out_free_skb;
191
192         NETLINK_CB(event->skb).dst_group = 1;
193         broadcast_uevent(event->skb, 0, 1, GFP_KERNEL);
194
195  out_free_skb:
196         if (ret) {
197                 pr_err(PFX "work error %d\n", ret);
198                 kfree_skb(event->skb);
199         }
200  out_free_event:
201         kfree(event);
202 }
203
204 static int button_hotplug_create_event(const char *name, unsigned int type,
205                 unsigned long seen, int pressed)
206 {
207         struct bh_event *event;
208
209         pr_debug(PFX "create event, name=%s, seen=%lu, pressed=%d\n",
210                  name, seen, pressed);
211
212         event = kzalloc(sizeof(*event), GFP_KERNEL);
213         if (!event)
214                 return -ENOMEM;
215
216         event->name = name;
217         event->type = type;
218         event->seen = seen;
219         event->action = pressed ? "pressed" : "released";
220
221         INIT_WORK(&event->work, (void *)(void *)button_hotplug_work);
222         schedule_work(&event->work);
223
224         return 0;
225 }
226
227 /* -------------------------------------------------------------------------*/
228
229 static int button_get_index(unsigned int code)
230 {
231         int i;
232
233         for (i = 0; i < ARRAY_SIZE(button_map); i++)
234                 if (button_map[i].code == code)
235                         return i;
236
237         return -1;
238 }
239
240 static void button_hotplug_event(struct gpio_keys_button_data *data,
241                            unsigned int type, int value)
242 {
243         struct bh_priv *priv = &data->bh;
244         unsigned long seen = jiffies;
245         int btn;
246
247         pr_debug(PFX "event type=%u, code=%u, value=%d\n", type, data->b->code, value);
248
249         if ((type != EV_KEY) && (type != EV_SW))
250                 return;
251
252         btn = button_get_index(data->b->code);
253         if (btn < 0)
254                 return;
255
256         if (priv->seen == 0)
257                 priv->seen = seen;
258
259         button_hotplug_create_event(button_map[btn].name, type,
260                         (seen - priv->seen) / HZ, value);
261         priv->seen = seen;
262 }
263
264 struct gpio_keys_button_dev {
265         int polled;
266         struct delayed_work work;
267
268         struct device *dev;
269         struct gpio_keys_platform_data *pdata;
270         struct gpio_keys_button_data data[0];
271 };
272
273 static int gpio_button_get_value(struct gpio_keys_button_data *bdata)
274 {
275         int val;
276
277         if (bdata->can_sleep)
278                 val = !!gpio_get_value_cansleep(bdata->b->gpio);
279         else
280                 val = !!gpio_get_value(bdata->b->gpio);
281
282         return val ^ bdata->b->active_low;
283 }
284
285 static void gpio_keys_polled_check_state(struct gpio_keys_button_data *bdata)
286 {
287         int state = gpio_button_get_value(bdata);
288
289         if (state != bdata->last_state) {
290                 unsigned int type = bdata->b->type ?: EV_KEY;
291
292                 if (bdata->count < bdata->threshold) {
293                         bdata->count++;
294                         return;
295                 }
296
297                 if (bdata->last_state != -1 || type == EV_SW)
298                         button_hotplug_event(bdata, type, state);
299
300                 bdata->last_state = state;
301         }
302
303         bdata->count = 0;
304 }
305
306 static void gpio_keys_polled_queue_work(struct gpio_keys_button_dev *bdev)
307 {
308         struct gpio_keys_platform_data *pdata = bdev->pdata;
309         unsigned long delay = msecs_to_jiffies(pdata->poll_interval);
310
311         if (delay >= HZ)
312                 delay = round_jiffies_relative(delay);
313         schedule_delayed_work(&bdev->work, delay);
314 }
315
316 static void gpio_keys_polled_poll(struct work_struct *work)
317 {
318         struct gpio_keys_button_dev *bdev =
319                 container_of(work, struct gpio_keys_button_dev, work.work);
320         int i;
321
322         for (i = 0; i < bdev->pdata->nbuttons; i++) {
323                 struct gpio_keys_button_data *bdata = &bdev->data[i];
324                 gpio_keys_polled_check_state(bdata);
325         }
326         gpio_keys_polled_queue_work(bdev);
327 }
328
329 static void gpio_keys_polled_close(struct gpio_keys_button_dev *bdev)
330 {
331         struct gpio_keys_platform_data *pdata = bdev->pdata;
332
333         cancel_delayed_work_sync(&bdev->work);
334
335         if (pdata->disable)
336                 pdata->disable(bdev->dev);
337 }
338
339 static void gpio_keys_irq_work_func(struct work_struct *work)
340 {
341         struct gpio_keys_button_data *bdata = container_of(work,
342                 struct gpio_keys_button_data, work.work);
343         int state = gpio_button_get_value(bdata);
344
345         if (state != bdata->last_state) {
346                 unsigned int type = bdata->b->type ?: EV_KEY;
347
348                 if (bdata->last_state != -1 || type == EV_SW)
349                         button_hotplug_event(bdata, type, state);
350
351                 bdata->last_state = state;
352         }
353 }
354
355 static irqreturn_t button_handle_irq(int irq, void *_bdata)
356 {
357         struct gpio_keys_button_data *bdata =
358                 (struct gpio_keys_button_data *) _bdata;
359
360         schedule_delayed_work(&bdata->work,
361                               msecs_to_jiffies(bdata->software_debounce));
362
363         return IRQ_HANDLED;
364 }
365
366 #ifdef CONFIG_OF
367 static struct gpio_keys_platform_data *
368 gpio_keys_get_devtree_pdata(struct device *dev)
369 {
370         struct device_node *node, *pp;
371         struct gpio_keys_platform_data *pdata;
372         struct gpio_keys_button *button;
373         int error;
374         int nbuttons;
375         int i = 0;
376
377         node = dev->of_node;
378         if (!node)
379                 return NULL;
380
381         nbuttons = of_get_child_count(node);
382         if (nbuttons == 0)
383                 return NULL;
384
385         pdata = devm_kzalloc(dev, sizeof(*pdata) + nbuttons * (sizeof *button),
386                 GFP_KERNEL);
387         if (!pdata) {
388                 error = -ENOMEM;
389                 goto err_out;
390         }
391
392         pdata->buttons = (struct gpio_keys_button *)(pdata + 1);
393         pdata->nbuttons = nbuttons;
394
395         pdata->rep = !!of_get_property(node, "autorepeat", NULL);
396         of_property_read_u32(node, "poll-interval", &pdata->poll_interval);
397
398         for_each_child_of_node(node, pp) {
399                 enum of_gpio_flags flags;
400
401                 if (!of_find_property(pp, "gpios", NULL)) {
402                         pdata->nbuttons--;
403                         dev_warn(dev, "Found button without gpios\n");
404                         continue;
405                 }
406
407                 button = (struct gpio_keys_button *)(&pdata->buttons[i++]);
408
409                 button->irq = irq_of_parse_and_map(pp, 0);
410
411                 button->gpio = of_get_gpio_flags(pp, 0, &flags);
412                 if (button->gpio < 0) {
413                         error = button->gpio;
414                         if (error != -ENOENT) {
415                                 if (error != -EPROBE_DEFER)
416                                         dev_err(dev,
417                                                 "Failed to get gpio flags, error: %d\n",
418                                                 error);
419                                 return ERR_PTR(error);
420                         }
421                 } else {
422                         button->active_low = flags & OF_GPIO_ACTIVE_LOW;
423                 }
424
425                 if (of_property_read_u32(pp, "linux,code", &button->code)) {
426                         dev_err(dev, "Button without keycode: 0x%x\n",
427                                 button->gpio);
428                         error = -EINVAL;
429                         goto err_out;
430                 }
431
432                 button->desc = of_get_property(pp, "label", NULL);
433
434                 if (of_property_read_u32(pp, "linux,input-type", &button->type))
435                         button->type = EV_KEY;
436
437                 button->wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL);
438
439                 if (of_property_read_u32(pp, "debounce-interval",
440                                         &button->debounce_interval))
441                         button->debounce_interval = 5;
442         }
443
444         if (pdata->nbuttons == 0) {
445                 error = -EINVAL;
446                 goto err_out;
447         }
448
449         return pdata;
450
451 err_out:
452         return ERR_PTR(error);
453 }
454
455 static struct of_device_id gpio_keys_of_match[] = {
456         { .compatible = "gpio-keys", },
457         { },
458 };
459 MODULE_DEVICE_TABLE(of, gpio_keys_of_match);
460
461 static struct of_device_id gpio_keys_polled_of_match[] = {
462         { .compatible = "gpio-keys-polled", },
463         { },
464 };
465 MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match);
466
467 #else
468
469 static inline struct gpio_keys_platform_data *
470 gpio_keys_get_devtree_pdata(struct device *dev)
471 {
472         return NULL;
473 }
474 #endif
475
476 static int gpio_keys_button_probe(struct platform_device *pdev,
477                 struct gpio_keys_button_dev **_bdev, int polled)
478 {
479         struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
480         struct device *dev = &pdev->dev;
481         struct gpio_keys_button_dev *bdev;
482         struct gpio_keys_button *buttons;
483         int error;
484         int i;
485
486         if (!pdata) {
487                 pdata = gpio_keys_get_devtree_pdata(dev);
488                 if (IS_ERR(pdata))
489                         return PTR_ERR(pdata);
490                 if (!pdata) {
491                         dev_err(dev, "missing platform data\n");
492                         return -EINVAL;
493                 }
494         }
495
496         if (polled && !pdata->poll_interval) {
497                 dev_err(dev, "missing poll_interval value\n");
498                 return -EINVAL;
499         }
500
501         buttons = devm_kzalloc(dev, pdata->nbuttons * sizeof(struct gpio_keys_button),
502                        GFP_KERNEL);
503         if (!buttons) {
504                 dev_err(dev, "no memory for button data\n");
505                 return -ENOMEM;
506         }
507         memcpy(buttons, pdata->buttons, pdata->nbuttons * sizeof(struct gpio_keys_button));
508
509         bdev = devm_kzalloc(dev, sizeof(struct gpio_keys_button_dev) +
510                        pdata->nbuttons * sizeof(struct gpio_keys_button_data),
511                        GFP_KERNEL);
512         if (!bdev) {
513                 dev_err(dev, "no memory for private data\n");
514                 return -ENOMEM;
515         }
516
517         bdev->polled = polled;
518
519         for (i = 0; i < pdata->nbuttons; i++) {
520                 struct gpio_keys_button *button = &buttons[i];
521                 struct gpio_keys_button_data *bdata = &bdev->data[i];
522                 unsigned int gpio = button->gpio;
523
524                 if (button->wakeup) {
525                         dev_err(dev, DRV_NAME "does not support wakeup\n");
526                         return -EINVAL;
527                 }
528
529                 error = devm_gpio_request(dev, gpio,
530                                      button->desc ? button->desc : DRV_NAME);
531                 if (error) {
532                         dev_err(dev, "unable to claim gpio %u, err=%d\n",
533                                 gpio, error);
534                         return error;
535                 }
536                 bdata->gpiod = gpio_to_desc(gpio);
537                 if (!bdata->gpiod)
538                         return -EINVAL;
539
540                 error = gpio_direction_input(gpio);
541                 if (error) {
542                         dev_err(dev,
543                                 "unable to set direction on gpio %u, err=%d\n",
544                                 gpio, error);
545                         return error;
546                 }
547
548                 bdata->can_sleep = gpio_cansleep(gpio);
549                 bdata->last_state = -1;
550
551                 if (bdev->polled) {
552                         bdata->threshold = DIV_ROUND_UP(button->debounce_interval,
553                                                         pdata->poll_interval);
554                 } else {
555                         bdata->threshold = 1;
556
557                         if (button->debounce_interval) {
558                                 error = gpiod_set_debounce(bdata->gpiod,
559                                         button->debounce_interval * 1000);
560                                 /*
561                                  * use timer if gpiolib doesn't provide
562                                  * debounce.
563                                  */
564                                 if (error < 0) {
565                                         bdata->software_debounce =
566                                                 button->debounce_interval;
567                                 }
568                         }
569                 }
570
571                 bdata->b = &pdata->buttons[i];
572         }
573
574         bdev->dev = &pdev->dev;
575         bdev->pdata = pdata;
576         platform_set_drvdata(pdev, bdev);
577
578         *_bdev = bdev;
579
580         return 0;
581 }
582
583 static int gpio_keys_probe(struct platform_device *pdev)
584 {
585         struct gpio_keys_platform_data *pdata;
586         struct gpio_keys_button_dev *bdev;
587         int ret, i;
588
589
590         ret = gpio_keys_button_probe(pdev, &bdev, 0);
591
592         if (ret)
593                 return ret;
594
595         pdata = bdev->pdata;
596         for (i = 0; i < pdata->nbuttons; i++) {
597                 const struct gpio_keys_button *button = &pdata->buttons[i];
598                 struct gpio_keys_button_data *bdata = &bdev->data[i];
599                 unsigned long irqflags = IRQF_ONESHOT;
600
601                 if (!button->irq) {
602                         bdata->irq = gpio_to_irq(button->gpio);
603
604                         if (bdata->irq < 0) {
605                                 dev_err(&pdev->dev, "failed to get irq for gpio:%d\n",
606                                         button->gpio);
607                                 continue;
608                         }
609
610                         irqflags |= IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
611                 } else {
612                         bdata->irq = button->irq;
613                 }
614
615                 INIT_DELAYED_WORK(&bdata->work, gpio_keys_irq_work_func);
616
617                 ret = devm_request_threaded_irq(&pdev->dev,
618                         bdata->irq, NULL, button_handle_irq,
619                         irqflags, dev_name(&pdev->dev), bdata);
620
621                 if (ret < 0) {
622                         bdata->irq = 0;
623                         dev_err(&pdev->dev, "failed to request irq:%d for gpio:%d\n",
624                                 bdata->irq, button->gpio);
625                         continue;
626                 } else {
627                         dev_dbg(&pdev->dev, "gpio:%d has irq:%d\n",
628                                 button->gpio, bdata->irq);
629                 }
630
631                 if (bdata->b->type == EV_SW)
632                         button_hotplug_event(bdata, EV_SW, gpio_button_get_value(bdata));
633         }
634
635         return 0;
636 }
637
638 static int gpio_keys_polled_probe(struct platform_device *pdev)
639 {
640         struct gpio_keys_platform_data *pdata;
641         struct gpio_keys_button_dev *bdev;
642         int ret;
643         int i;
644
645         ret = gpio_keys_button_probe(pdev, &bdev, 1);
646
647         if (ret)
648                 return ret;
649
650         INIT_DELAYED_WORK(&bdev->work, gpio_keys_polled_poll);
651
652         pdata = bdev->pdata;
653
654         if (pdata->enable)
655                 pdata->enable(bdev->dev);
656
657         for (i = 0; i < pdata->nbuttons; i++)
658                 gpio_keys_polled_check_state(&bdev->data[i]);
659
660         gpio_keys_polled_queue_work(bdev);
661
662         return ret;
663 }
664
665 static void gpio_keys_irq_close(struct gpio_keys_button_dev *bdev)
666 {
667         struct gpio_keys_platform_data *pdata = bdev->pdata;
668         size_t i;
669
670         for (i = 0; i < pdata->nbuttons; i++) {
671                 struct gpio_keys_button_data *bdata = &bdev->data[i];
672
673                 disable_irq(bdata->irq);
674                 cancel_delayed_work_sync(&bdata->work);
675         }
676 }
677
678 static int gpio_keys_remove(struct platform_device *pdev)
679 {
680         struct gpio_keys_button_dev *bdev = platform_get_drvdata(pdev);
681
682         platform_set_drvdata(pdev, NULL);
683
684         if (bdev->polled)
685                 gpio_keys_polled_close(bdev);
686         else
687                 gpio_keys_irq_close(bdev);
688
689         return 0;
690 }
691
692 static struct platform_driver gpio_keys_driver = {
693         .probe  = gpio_keys_probe,
694         .remove = gpio_keys_remove,
695         .driver = {
696                 .name   = "gpio-keys",
697                 .owner  = THIS_MODULE,
698                 .of_match_table = of_match_ptr(gpio_keys_of_match),
699         },
700 };
701
702 static struct platform_driver gpio_keys_polled_driver = {
703         .probe  = gpio_keys_polled_probe,
704         .remove = gpio_keys_remove,
705         .driver = {
706                 .name   = "gpio-keys-polled",
707                 .owner  = THIS_MODULE,
708                 .of_match_table = of_match_ptr(gpio_keys_polled_of_match),
709         },
710 };
711
712 static int __init gpio_button_init(void)
713 {
714         int ret;
715
716         ret = platform_driver_register(&gpio_keys_driver);
717         if (ret)
718                 return ret;
719
720         ret = platform_driver_register(&gpio_keys_polled_driver);
721         if (ret)
722                 platform_driver_unregister(&gpio_keys_driver);
723
724         return ret;
725 }
726
727 static void __exit gpio_button_exit(void)
728 {
729         platform_driver_unregister(&gpio_keys_driver);
730         platform_driver_unregister(&gpio_keys_polled_driver);
731 }
732
733 module_init(gpio_button_init);
734 module_exit(gpio_button_exit);
735
736 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
737 MODULE_AUTHOR("Felix Fietkau <nbd@nbd.name>");
738 MODULE_DESCRIPTION("Polled GPIO Buttons hotplug driver");
739 MODULE_LICENSE("GPL v2");
740 MODULE_ALIAS("platform:" DRV_NAME);