[tools] mm-macros: update to 0.9.5
[openwrt.git] / package / 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/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 char *action;
53 unsigned long seen;
54
55 struct sk_buff *skb;
56 struct work_struct work;
57 };
58
59 struct bh_map {
60 unsigned int code;
61 const char *name;
62 };
63
64 struct gpio_keys_button_data {
65 struct delayed_work work;
66 struct bh_priv bh;
67 int last_state;
68 int count;
69 int threshold;
70 int can_sleep;
71 };
72
73 extern u64 uevent_next_seqnum(void);
74
75 #define BH_MAP(_code, _name) \
76 { \
77 .code = (_code), \
78 .name = (_name), \
79 }
80
81 static struct bh_map button_map[] = {
82 BH_MAP(BTN_0, "BTN_0"),
83 BH_MAP(BTN_1, "BTN_1"),
84 BH_MAP(BTN_2, "BTN_2"),
85 BH_MAP(BTN_3, "BTN_3"),
86 BH_MAP(BTN_4, "BTN_4"),
87 BH_MAP(BTN_5, "BTN_5"),
88 BH_MAP(BTN_6, "BTN_6"),
89 BH_MAP(BTN_7, "BTN_7"),
90 BH_MAP(BTN_8, "BTN_8"),
91 BH_MAP(BTN_9, "BTN_9"),
92 BH_MAP(KEY_RESTART, "reset"),
93 #ifdef KEY_WPS_BUTTON
94 BH_MAP(KEY_WPS_BUTTON, "wps"),
95 #endif /* KEY_WPS_BUTTON */
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 ret = bh_event_add_var(event, 0, "SUBSYSTEM=%s", "button");
143 if (ret)
144 return ret;
145
146 ret = bh_event_add_var(event, 0, "ACTION=%s", event->action);
147 if (ret)
148 return ret;
149
150 ret = bh_event_add_var(event, 0, "BUTTON=%s", event->name);
151 if (ret)
152 return ret;
153
154 ret = bh_event_add_var(event, 0, "SEEN=%ld", event->seen);
155 if (ret)
156 return ret;
157
158 ret = bh_event_add_var(event, 0, "SEQNUM=%llu", uevent_next_seqnum());
159
160 return ret;
161 }
162
163 static void button_hotplug_work(struct work_struct *work)
164 {
165 struct bh_event *event = container_of(work, struct bh_event, work);
166 int ret = 0;
167
168 event->skb = alloc_skb(BH_SKB_SIZE, GFP_KERNEL);
169 if (!event->skb)
170 goto out_free_event;
171
172 ret = bh_event_add_var(event, 0, "%s@", event->action);
173 if (ret)
174 goto out_free_skb;
175
176 ret = button_hotplug_fill_event(event);
177 if (ret)
178 goto out_free_skb;
179
180 NETLINK_CB(event->skb).dst_group = 1;
181 broadcast_uevent(event->skb, 0, 1, GFP_KERNEL);
182
183 out_free_skb:
184 if (ret) {
185 BH_ERR("work error %d\n", ret);
186 kfree_skb(event->skb);
187 }
188 out_free_event:
189 kfree(event);
190 }
191
192 static int button_hotplug_create_event(const char *name, unsigned long seen,
193 int pressed)
194 {
195 struct bh_event *event;
196
197 BH_DBG("create event, name=%s, seen=%lu, pressed=%d\n",
198 name, seen, pressed);
199
200 event = kzalloc(sizeof(*event), GFP_KERNEL);
201 if (!event)
202 return -ENOMEM;
203
204 event->name = name;
205 event->seen = seen;
206 event->action = pressed ? "pressed" : "released";
207
208 INIT_WORK(&event->work, (void *)(void *)button_hotplug_work);
209 schedule_work(&event->work);
210
211 return 0;
212 }
213
214 /* -------------------------------------------------------------------------*/
215
216 #ifdef CONFIG_HOTPLUG
217 static int button_get_index(unsigned int code)
218 {
219 int i;
220
221 for (i = 0; i < ARRAY_SIZE(button_map); i++)
222 if (button_map[i].code == code)
223 return i;
224
225 return -1;
226 }
227 static void button_hotplug_event(struct gpio_keys_button_data *data,
228 unsigned int type, unsigned int code, int value)
229 {
230 struct bh_priv *priv = &data->bh;
231 unsigned long seen = jiffies;
232 int btn;
233
234 BH_DBG("event type=%u, code=%u, value=%d\n", type, code, value);
235
236 if (type != EV_KEY)
237 return;
238
239 btn = button_get_index(code);
240 if (btn < 0)
241 return;
242
243 button_hotplug_create_event(button_map[btn].name,
244 (seen - priv->seen) / HZ, value);
245 priv->seen = seen;
246 }
247 #else
248 static void button_hotplug_event(struct gpio_keys_button_data *data,
249 unsigned int type, unsigned int code, int value)
250 {
251 }
252 #endif /* CONFIG_HOTPLUG */
253
254 struct gpio_keys_polled_dev {
255 struct delayed_work work;
256
257 struct device *dev;
258 struct gpio_keys_platform_data *pdata;
259 struct gpio_keys_button_data data[0];
260 };
261
262 static void gpio_keys_polled_check_state(struct gpio_keys_button *button,
263 struct gpio_keys_button_data *bdata)
264 {
265 int state;
266
267 if (bdata->can_sleep)
268 state = !!gpio_get_value_cansleep(button->gpio);
269 else
270 state = !!gpio_get_value(button->gpio);
271
272 state = !!(state ^ button->active_low);
273 if (state != bdata->last_state) {
274 unsigned int type = button->type ?: EV_KEY;
275
276 button_hotplug_event(bdata, type, button->code, state);
277 bdata->count = 0;
278 bdata->last_state = state;
279 }
280 }
281
282 static void gpio_keys_polled_queue_work(struct gpio_keys_polled_dev *bdev)
283 {
284 struct gpio_keys_platform_data *pdata = bdev->pdata;
285 unsigned long delay = msecs_to_jiffies(pdata->poll_interval);
286
287 if (delay >= HZ)
288 delay = round_jiffies_relative(delay);
289 schedule_delayed_work(&bdev->work, delay);
290 }
291
292 static void gpio_keys_polled_poll(struct work_struct *work)
293 {
294 struct gpio_keys_polled_dev *bdev =
295 container_of(work, struct gpio_keys_polled_dev, work.work);
296 struct gpio_keys_platform_data *pdata = bdev->pdata;
297 int i;
298
299 for (i = 0; i < bdev->pdata->nbuttons; i++) {
300 struct gpio_keys_button_data *bdata = &bdev->data[i];
301
302 if (bdata->count < bdata->threshold)
303 bdata->count++;
304 else
305 gpio_keys_polled_check_state(&pdata->buttons[i], bdata);
306 }
307 gpio_keys_polled_queue_work(bdev);
308 }
309
310 static void __devinit gpio_keys_polled_open(struct gpio_keys_polled_dev *bdev)
311 {
312 struct gpio_keys_platform_data *pdata = bdev->pdata;
313 int i;
314
315 if (pdata->enable)
316 pdata->enable(bdev->dev);
317
318 /* report initial state of the buttons */
319 for (i = 0; i < pdata->nbuttons; i++)
320 gpio_keys_polled_check_state(&pdata->buttons[i], &bdev->data[i]);
321
322 gpio_keys_polled_queue_work(bdev);
323 }
324
325 static void __devexit gpio_keys_polled_close(struct gpio_keys_polled_dev *bdev)
326 {
327 struct gpio_keys_platform_data *pdata = bdev->pdata;
328
329 cancel_delayed_work_sync(&bdev->work);
330
331 if (pdata->disable)
332 pdata->disable(bdev->dev);
333 }
334
335 static int __devinit gpio_keys_polled_probe(struct platform_device *pdev)
336 {
337 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
338 struct device *dev = &pdev->dev;
339 struct gpio_keys_polled_dev *bdev;
340 int error;
341 int i;
342
343 if (!pdata || !pdata->poll_interval)
344 return -EINVAL;
345
346 bdev = kzalloc(sizeof(struct gpio_keys_polled_dev) +
347 pdata->nbuttons * sizeof(struct gpio_keys_button_data),
348 GFP_KERNEL);
349 if (!bdev) {
350 dev_err(dev, "no memory for private data\n");
351 return -ENOMEM;
352 }
353
354 for (i = 0; i < pdata->nbuttons; i++) {
355 struct gpio_keys_button *button = &pdata->buttons[i];
356 struct gpio_keys_button_data *bdata = &bdev->data[i];
357 unsigned int gpio = button->gpio;
358
359 if (button->wakeup) {
360 dev_err(dev, DRV_NAME " does not support wakeup\n");
361 error = -EINVAL;
362 goto err_free_gpio;
363 }
364
365 error = gpio_request(gpio,
366 button->desc ? button->desc : DRV_NAME);
367 if (error) {
368 dev_err(dev, "unable to claim gpio %u, err=%d\n",
369 gpio, error);
370 goto err_free_gpio;
371 }
372
373 error = gpio_direction_input(gpio);
374 if (error) {
375 dev_err(dev,
376 "unable to set direction on gpio %u, err=%d\n",
377 gpio, error);
378 goto err_free_gpio;
379 }
380
381 bdata->can_sleep = gpio_cansleep(gpio);
382 bdata->last_state = 0;
383 bdata->threshold = DIV_ROUND_UP(button->debounce_interval,
384 pdata->poll_interval);
385 }
386
387 bdev->dev = &pdev->dev;
388 bdev->pdata = pdata;
389 platform_set_drvdata(pdev, bdev);
390
391 INIT_DELAYED_WORK(&bdev->work, gpio_keys_polled_poll);
392
393 gpio_keys_polled_open(bdev);
394
395 return 0;
396
397 err_free_gpio:
398 while (--i >= 0)
399 gpio_free(pdata->buttons[i].gpio);
400
401 kfree(bdev);
402 platform_set_drvdata(pdev, NULL);
403
404 return error;
405 }
406
407 static int __devexit gpio_keys_polled_remove(struct platform_device *pdev)
408 {
409 struct gpio_keys_polled_dev *bdev = platform_get_drvdata(pdev);
410 struct gpio_keys_platform_data *pdata = bdev->pdata;
411 int i = pdata->nbuttons;
412
413 gpio_keys_polled_close(bdev);
414
415 while (--i >= 0)
416 gpio_free(pdata->buttons[i].gpio);
417
418 kfree(bdev);
419 platform_set_drvdata(pdev, NULL);
420
421 return 0;
422 }
423
424 static struct platform_driver gpio_keys_polled_driver = {
425 .probe = gpio_keys_polled_probe,
426 .remove = __devexit_p(gpio_keys_polled_remove),
427 .driver = {
428 .name = DRV_NAME,
429 .owner = THIS_MODULE,
430 },
431 };
432
433 static int __init gpio_keys_polled_init(void)
434 {
435 return platform_driver_register(&gpio_keys_polled_driver);
436 }
437
438 static void __exit gpio_keys_polled_exit(void)
439 {
440 platform_driver_unregister(&gpio_keys_polled_driver);
441 }
442
443 module_init(gpio_keys_polled_init);
444 module_exit(gpio_keys_polled_exit);
445
446 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
447 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
448 MODULE_DESCRIPTION("Polled GPIO Buttons hotplug driver");
449 MODULE_LICENSE("GPL v2");
450 MODULE_ALIAS("platform:" DRV_NAME);
This page took 0.073405 seconds and 5 git commands to generate.