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