OpenWrt – Blame information for rev 4

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