lantiq: Tune the XWAY subtarget cflags
[openwrt/staging/mkresin.git] / package / system / button-hotplug / src / button-hotplug.c
1 /*
2 * Button Hotplug driver
3 *
4 * Copyright (C) 2008-2010 Gabor Juhos <juhosg@openwrt.org>
5 *
6 * Based on the diag.c - GPIO interface driver for Broadcom boards
7 * Copyright (C) 2006 Mike Baker <mbm@openwrt.org>,
8 * Copyright (C) 2006-2007 Felix Fietkau <nbd@openwrt.org>
9 * Copyright (C) 2008 Andy Boyett <agb@openwrt.org>
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 as published
13 * by the Free Software Foundation.
14 */
15
16 #include <linux/module.h>
17 #include <linux/version.h>
18 #include <linux/kmod.h>
19 #include <linux/input.h>
20
21 #include <linux/workqueue.h>
22 #include <linux/skbuff.h>
23 #include <linux/netlink.h>
24 #include <linux/kobject.h>
25
26 #define DRV_NAME "button-hotplug"
27 #define DRV_VERSION "0.4.1"
28 #define DRV_DESC "Button Hotplug driver"
29
30 #define BH_SKB_SIZE 2048
31
32 #define PFX DRV_NAME ": "
33
34 #undef BH_DEBUG
35
36 #ifdef BH_DEBUG
37 #define BH_DBG(fmt, args...) printk(KERN_DEBUG "%s: " fmt, DRV_NAME, ##args )
38 #else
39 #define BH_DBG(fmt, args...) do {} while (0)
40 #endif
41
42 #define BH_ERR(fmt, args...) printk(KERN_ERR "%s: " fmt, DRV_NAME, ##args )
43
44 #ifndef BIT_MASK
45 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
46 #endif
47
48 struct bh_priv {
49 unsigned long *seen;
50 struct input_handle handle;
51 };
52
53 struct bh_event {
54 const char *name;
55 char *action;
56 unsigned long seen;
57
58 struct sk_buff *skb;
59 struct work_struct work;
60 };
61
62 struct bh_map {
63 unsigned int code;
64 const char *name;
65 };
66
67 extern u64 uevent_next_seqnum(void);
68
69 #define BH_MAP(_code, _name) \
70 { \
71 .code = (_code), \
72 .name = (_name), \
73 }
74
75 static struct bh_map button_map[] = {
76 BH_MAP(BTN_0, "BTN_0"),
77 BH_MAP(BTN_1, "BTN_1"),
78 BH_MAP(BTN_2, "BTN_2"),
79 BH_MAP(BTN_3, "BTN_3"),
80 BH_MAP(BTN_4, "BTN_4"),
81 BH_MAP(BTN_5, "BTN_5"),
82 BH_MAP(BTN_6, "BTN_6"),
83 BH_MAP(BTN_7, "BTN_7"),
84 BH_MAP(BTN_8, "BTN_8"),
85 BH_MAP(BTN_9, "BTN_9"),
86 BH_MAP(KEY_RESTART, "reset"),
87 #ifdef KEY_WPS_BUTTON
88 BH_MAP(KEY_WPS_BUTTON, "wps"),
89 #endif /* KEY_WPS_BUTTON */
90 };
91
92 /* -------------------------------------------------------------------------*/
93
94 static int bh_event_add_var(struct bh_event *event, int argv,
95 const char *format, ...)
96 {
97 static char buf[128];
98 char *s;
99 va_list args;
100 int len;
101
102 if (argv)
103 return 0;
104
105 va_start(args, format);
106 len = vsnprintf(buf, sizeof(buf), format, args);
107 va_end(args);
108
109 if (len >= sizeof(buf)) {
110 BH_ERR("buffer size too small\n");
111 WARN_ON(1);
112 return -ENOMEM;
113 }
114
115 s = skb_put(event->skb, len + 1);
116 strcpy(s, buf);
117
118 BH_DBG("added variable '%s'\n", s);
119
120 return 0;
121 }
122
123 static int button_hotplug_fill_event(struct bh_event *event)
124 {
125 int ret;
126
127 ret = bh_event_add_var(event, 0, "HOME=%s", "/");
128 if (ret)
129 return ret;
130
131 ret = bh_event_add_var(event, 0, "PATH=%s",
132 "/sbin:/bin:/usr/sbin:/usr/bin");
133 if (ret)
134 return ret;
135
136 ret = bh_event_add_var(event, 0, "SUBSYSTEM=%s", "button");
137 if (ret)
138 return ret;
139
140 ret = bh_event_add_var(event, 0, "ACTION=%s", event->action);
141 if (ret)
142 return ret;
143
144 ret = bh_event_add_var(event, 0, "BUTTON=%s", event->name);
145 if (ret)
146 return ret;
147
148 ret = bh_event_add_var(event, 0, "SEEN=%ld", event->seen);
149 if (ret)
150 return ret;
151
152 ret = bh_event_add_var(event, 0, "SEQNUM=%llu", uevent_next_seqnum());
153
154 return ret;
155 }
156
157 static void button_hotplug_work(struct work_struct *work)
158 {
159 struct bh_event *event = container_of(work, struct bh_event, work);
160 int ret = 0;
161
162 event->skb = alloc_skb(BH_SKB_SIZE, GFP_KERNEL);
163 if (!event->skb)
164 goto out_free_event;
165
166 ret = bh_event_add_var(event, 0, "%s@", event->action);
167 if (ret)
168 goto out_free_skb;
169
170 ret = button_hotplug_fill_event(event);
171 if (ret)
172 goto out_free_skb;
173
174 NETLINK_CB(event->skb).dst_group = 1;
175 broadcast_uevent(event->skb, 0, 1, GFP_KERNEL);
176
177 out_free_skb:
178 if (ret) {
179 BH_ERR("work error %d\n", ret);
180 kfree_skb(event->skb);
181 }
182 out_free_event:
183 kfree(event);
184 }
185
186 static int button_hotplug_create_event(const char *name, unsigned long seen,
187 int pressed)
188 {
189 struct bh_event *event;
190
191 BH_DBG("create event, name=%s, seen=%lu, pressed=%d\n",
192 name, seen, pressed);
193
194 event = kzalloc(sizeof(*event), GFP_KERNEL);
195 if (!event)
196 return -ENOMEM;
197
198 event->name = name;
199 event->seen = seen;
200 event->action = pressed ? "pressed" : "released";
201
202 INIT_WORK(&event->work, (void *)(void *)button_hotplug_work);
203 schedule_work(&event->work);
204
205 return 0;
206 }
207
208 /* -------------------------------------------------------------------------*/
209
210 #ifdef CONFIG_HOTPLUG
211 static int button_get_index(unsigned int code)
212 {
213 int i;
214
215 for (i = 0; i < ARRAY_SIZE(button_map); i++)
216 if (button_map[i].code == code)
217 return i;
218
219 return -1;
220 }
221 static void button_hotplug_event(struct input_handle *handle,
222 unsigned int type, unsigned int code, int value)
223 {
224 struct bh_priv *priv = handle->private;
225 unsigned long seen = jiffies;
226 int btn;
227
228 BH_DBG("event type=%u, code=%u, value=%d\n", type, code, value);
229
230 if (type != EV_KEY)
231 return;
232
233 btn = button_get_index(code);
234 if (btn < 0)
235 return;
236
237 button_hotplug_create_event(button_map[btn].name,
238 (seen - priv->seen[btn]) / HZ, value);
239 priv->seen[btn] = seen;
240 }
241 #else
242 static void button_hotplug_event(struct input_handle *handle,
243 unsigned int type, unsigned int code, int value)
244 {
245 }
246 #endif /* CONFIG_HOTPLUG */
247
248 static int button_hotplug_connect(struct input_handler *handler,
249 struct input_dev *dev, const struct input_device_id *id)
250 {
251 struct bh_priv *priv;
252 int ret;
253 int i;
254
255 for (i = 0; i < ARRAY_SIZE(button_map); i++)
256 if (test_bit(button_map[i].code, dev->keybit))
257 break;
258
259 if (i == ARRAY_SIZE(button_map))
260 return -ENODEV;
261
262 priv = kzalloc(sizeof(*priv) +
263 (sizeof(unsigned long) * ARRAY_SIZE(button_map)),
264 GFP_KERNEL);
265 if (!priv)
266 return -ENOMEM;
267
268 priv->seen = (unsigned long *) &priv[1];
269 priv->handle.private = priv;
270 priv->handle.dev = dev;
271 priv->handle.handler = handler;
272 priv->handle.name = DRV_NAME;
273
274 ret = input_register_handle(&priv->handle);
275 if (ret)
276 goto err_free_priv;
277
278 ret = input_open_device(&priv->handle);
279 if (ret)
280 goto err_unregister_handle;
281
282 BH_DBG("connected to %s\n", dev->name);
283
284 return 0;
285
286 err_unregister_handle:
287 input_unregister_handle(&priv->handle);
288
289 err_free_priv:
290 kfree(priv);
291 return ret;
292 }
293
294 static void button_hotplug_disconnect(struct input_handle *handle)
295 {
296 struct bh_priv *priv = handle->private;
297
298 input_close_device(handle);
299 input_unregister_handle(handle);
300
301 kfree(priv);
302 }
303
304 static const struct input_device_id button_hotplug_ids[] = {
305 {
306 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
307 .evbit = { BIT_MASK(EV_KEY) },
308 },
309 {
310 /* Terminating entry */
311 },
312 };
313
314 MODULE_DEVICE_TABLE(input, button_hotplug_ids);
315
316 static struct input_handler button_hotplug_handler = {
317 .event = button_hotplug_event,
318 .connect = button_hotplug_connect,
319 .disconnect = button_hotplug_disconnect,
320 .name = DRV_NAME,
321 .id_table = button_hotplug_ids,
322 };
323
324 /* -------------------------------------------------------------------------*/
325
326 static int __init button_hotplug_init(void)
327 {
328 int ret;
329
330 printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
331 ret = input_register_handler(&button_hotplug_handler);
332 if (ret)
333 BH_ERR("unable to register input handler\n");
334
335 return ret;
336 }
337 module_init(button_hotplug_init);
338
339 static void __exit button_hotplug_exit(void)
340 {
341 input_unregister_handler(&button_hotplug_handler);
342 }
343 module_exit(button_hotplug_exit);
344
345 MODULE_DESCRIPTION(DRV_DESC);
346 MODULE_VERSION(DRV_VERSION);
347 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
348 MODULE_LICENSE("GPL v2");
349