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