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