package/button-hotplug: handle KEY_RESTART and KEY_WPS_BUTTON codes as well
[openwrt/svn-archive/archive.git] / package / 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 <net/sock.h>
25
26 #define DRV_NAME "button-hotplug"
27 #define DRV_VERSION "0.4.0"
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 struct sock *uevent_sock;
68 extern u64 uevent_next_seqnum(void);
69
70 #define BH_MAP(_code, _name) \
71 { \
72 .code = (_code), \
73 .name = (_name), \
74 }
75
76 static struct bh_map button_map[] = {
77 BH_MAP(BTN_0, "BTN_0"),
78 BH_MAP(BTN_1, "BTN_1"),
79 BH_MAP(BTN_2, "BTN_2"),
80 BH_MAP(BTN_3, "BTN_3"),
81 BH_MAP(BTN_4, "BTN_4"),
82 BH_MAP(BTN_5, "BTN_5"),
83 BH_MAP(BTN_6, "BTN_6"),
84 BH_MAP(BTN_7, "BTN_7"),
85 BH_MAP(BTN_8, "BTN_8"),
86 BH_MAP(BTN_9, "BTN_9"),
87 BH_MAP(KEY_RESTART, "reset"),
88 #ifdef KEY_WPS_BUTTON
89 BH_MAP(KEY_WPS_BUTTON, "wps"),
90 #endif /* KEY_WPS_BUTTON */
91 };
92
93 /* -------------------------------------------------------------------------*/
94
95 static int bh_event_add_var(struct bh_event *event, int argv,
96 const char *format, ...)
97 {
98 static char buf[128];
99 char *s;
100 va_list args;
101 int len;
102
103 if (argv)
104 return 0;
105
106 va_start(args, format);
107 len = vsnprintf(buf, sizeof(buf), format, args);
108 va_end(args);
109
110 if (len >= sizeof(buf)) {
111 BH_ERR("buffer size too small\n");
112 WARN_ON(1);
113 return -ENOMEM;
114 }
115
116 s = skb_put(event->skb, len + 1);
117 strcpy(s, buf);
118
119 BH_DBG("added variable '%s'\n", s);
120
121 return 0;
122 }
123
124 static int button_hotplug_fill_event(struct bh_event *event)
125 {
126 int ret;
127
128 ret = bh_event_add_var(event, 0, "HOME=%s", "/");
129 if (ret)
130 return ret;
131
132 ret = bh_event_add_var(event, 0, "PATH=%s",
133 "/sbin:/bin:/usr/sbin:/usr/bin");
134 if (ret)
135 return ret;
136
137 ret = bh_event_add_var(event, 0, "SUBSYSTEM=%s", "button");
138 if (ret)
139 return ret;
140
141 ret = bh_event_add_var(event, 0, "ACTION=%s", event->action);
142 if (ret)
143 return ret;
144
145 ret = bh_event_add_var(event, 0, "BUTTON=%s", event->name);
146 if (ret)
147 return ret;
148
149 ret = bh_event_add_var(event, 0, "SEEN=%ld", event->seen);
150 if (ret)
151 return ret;
152
153 ret = bh_event_add_var(event, 0, "SEQNUM=%llu", uevent_next_seqnum());
154
155 return ret;
156 }
157
158 static void button_hotplug_work(struct work_struct *work)
159 {
160 struct bh_event *event = container_of(work, struct bh_event, work);
161 int ret = 0;
162
163 if (!uevent_sock)
164 goto out_free_event;
165
166 event->skb = alloc_skb(BH_SKB_SIZE, GFP_KERNEL);
167 if (!event->skb)
168 goto out_free_event;
169
170 ret = bh_event_add_var(event, 0, "%s@", event->action);
171 if (ret)
172 goto out_free_skb;
173
174 ret = button_hotplug_fill_event(event);
175 if (ret)
176 goto out_free_skb;
177
178 NETLINK_CB(event->skb).dst_group = 1;
179 netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
180
181 out_free_skb:
182 if (ret) {
183 BH_ERR("work error %d\n", ret);
184 kfree_skb(event->skb);
185 }
186 out_free_event:
187 kfree(event);
188 }
189
190 static int button_hotplug_create_event(const char *name, unsigned long seen,
191 int pressed)
192 {
193 struct bh_event *event;
194
195 BH_DBG("create event, name=%s, seen=%lu, pressed=%d\n",
196 name, seen, pressed);
197
198 event = kzalloc(sizeof(*event), GFP_KERNEL);
199 if (!event)
200 return -ENOMEM;
201
202 event->name = name;
203 event->seen = seen;
204 event->action = pressed ? "pressed" : "released";
205
206 INIT_WORK(&event->work, (void *)(void *)button_hotplug_work);
207 schedule_work(&event->work);
208
209 return 0;
210 }
211
212 /* -------------------------------------------------------------------------*/
213
214 #ifdef CONFIG_HOTPLUG
215 static int button_get_index(unsigned int code)
216 {
217 int i;
218
219 for (i = 0; i < ARRAY_SIZE(button_map); i++)
220 if (button_map[i].code == code)
221 return i;
222
223 return -1;
224 }
225 static void button_hotplug_event(struct input_handle *handle,
226 unsigned int type, unsigned int code, int value)
227 {
228 struct bh_priv *priv = handle->private;
229 unsigned long seen = jiffies;
230 int btn;
231
232 BH_DBG("event type=%u, code=%u, value=%d\n", type, code, value);
233
234 if (type != EV_KEY)
235 return;
236
237 btn = button_get_index(code);
238 if (btn < 0)
239 return;
240
241 button_hotplug_create_event(button_map[btn].name,
242 (seen - priv->seen[btn]) / HZ, value);
243 priv->seen[btn] = seen;
244 }
245 #else
246 static void button_hotplug_event(struct input_handle *handle,
247 unsigned int type, unsigned int code, int value)
248 {
249 }
250 #endif /* CONFIG_HOTPLUG */
251
252 static int button_hotplug_connect(struct input_handler *handler,
253 struct input_dev *dev, const struct input_device_id *id)
254 {
255 struct bh_priv *priv;
256 int ret;
257 int i;
258
259 for (i = 0; i < ARRAY_SIZE(button_map); i++)
260 if (test_bit(button_map[i].code, dev->keybit))
261 break;
262
263 if (i == ARRAY_SIZE(button_map))
264 return -ENODEV;
265
266 priv = kzalloc(sizeof(*priv) +
267 (sizeof(unsigned long) * ARRAY_SIZE(button_map)),
268 GFP_KERNEL);
269 if (!priv)
270 return -ENOMEM;
271
272 priv->seen = (unsigned long *) &priv[1];
273 priv->handle.private = priv;
274 priv->handle.dev = dev;
275 priv->handle.handler = handler;
276 priv->handle.name = DRV_NAME;
277
278 ret = input_register_handle(&priv->handle);
279 if (ret)
280 goto err_free_priv;
281
282 ret = input_open_device(&priv->handle);
283 if (ret)
284 goto err_unregister_handle;
285
286 BH_DBG("connected to %s\n", dev->name);
287
288 return 0;
289
290 err_unregister_handle:
291 input_unregister_handle(&priv->handle);
292
293 err_free_priv:
294 kfree(priv);
295 return ret;
296 }
297
298 static void button_hotplug_disconnect(struct input_handle *handle)
299 {
300 struct bh_priv *priv = handle->private;
301
302 input_close_device(handle);
303 input_unregister_handle(handle);
304
305 kfree(priv);
306 }
307
308 static const struct input_device_id button_hotplug_ids[] = {
309 {
310 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
311 .evbit = { BIT_MASK(EV_KEY) },
312 },
313 {
314 /* Terminating entry */
315 },
316 };
317
318 MODULE_DEVICE_TABLE(input, button_hotplug_ids);
319
320 static struct input_handler button_hotplug_handler = {
321 .event = button_hotplug_event,
322 .connect = button_hotplug_connect,
323 .disconnect = button_hotplug_disconnect,
324 .name = DRV_NAME,
325 .id_table = button_hotplug_ids,
326 };
327
328 /* -------------------------------------------------------------------------*/
329
330 static int __init button_hotplug_init(void)
331 {
332 int ret;
333
334 printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
335 ret = input_register_handler(&button_hotplug_handler);
336 if (ret)
337 BH_ERR("unable to register input handler\n");
338
339 return ret;
340 }
341 module_init(button_hotplug_init);
342
343 static void __exit button_hotplug_exit(void)
344 {
345 input_unregister_handler(&button_hotplug_handler);
346 }
347 module_exit(button_hotplug_exit);
348
349 MODULE_DESCRIPTION(DRV_DESC);
350 MODULE_VERSION(DRV_VERSION);
351 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
352 MODULE_LICENSE("GPL v2");
353