[tools] fix segfault when using the maxlen argument (#4359)
[openwrt.git] / package / button-hotplug / src / button-hotplug.c
1 /*
2 * Button Hotplug driver
3 *
4 * Copyright (C) 2008 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.3.1"
28 #define DRV_DESC "Button Hotplug driver"
29
30 #define BH_SKB_SIZE 2048
31
32 #define BH_BTN_MIN BTN_0
33 #define BH_BTN_MAX BTN_9
34
35 #define BH_BTN_COUNT (BH_BTN_MAX - BH_BTN_MIN + 1)
36
37 #define PFX DRV_NAME ": "
38
39 #undef BH_DEBUG
40
41 #ifdef BH_DEBUG
42 #define BH_DBG(fmt, args...) printk(KERN_DEBUG "%s: " fmt, DRV_NAME, ##args )
43 #else
44 #define BH_DBG(fmt, args...) do {} while (0)
45 #endif
46
47 #define BH_ERR(fmt, args...) printk(KERN_ERR "%s: " fmt, DRV_NAME, ##args )
48
49 #ifndef BIT_MASK
50 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
51 #endif
52
53 struct bh_priv {
54 unsigned long seen[BH_BTN_COUNT];
55 struct input_handle handle;
56 };
57
58 struct bh_event {
59 char *name;
60 char *action;
61 unsigned long seen;
62
63 struct sk_buff *skb;
64 struct work_struct work;
65 };
66
67 extern struct sock *uevent_sock;
68 extern u64 uevent_next_seqnum(void);
69
70 static char *button_names[BH_BTN_COUNT] = {
71 "BTN_0", "BTN_1", "BTN_2", "BTN_3", "BTN_4",
72 "BTN_5", "BTN_6", "BTN_7", "BTN_8", "BTN_9"
73 };
74
75 /* -------------------------------------------------------------------------*/
76
77 static int bh_event_add_var(struct bh_event *event, int argv,
78 const char *format, ...)
79 {
80 static char buf[128];
81 char *s;
82 va_list args;
83 int len;
84
85 if (argv)
86 return 0;
87
88 va_start(args, format);
89 len = vsnprintf(buf, sizeof(buf), format, args);
90 va_end(args);
91
92 if (len >= sizeof(buf)) {
93 BH_ERR("buffer size too small\n");
94 WARN_ON(1);
95 return -ENOMEM;
96 }
97
98 s = skb_put(event->skb, len + 1);
99 strcpy(s, buf);
100
101 BH_DBG("added variable '%s'\n", s);
102
103 return 0;
104 }
105
106 static int button_hotplug_fill_event(struct bh_event *event)
107 {
108 int ret;
109
110 ret = bh_event_add_var(event, 0, "HOME=%s", "/");
111 if (ret)
112 return ret;
113
114 ret = bh_event_add_var(event, 0, "PATH=%s",
115 "/sbin:/bin:/usr/sbin:/usr/bin");
116 if (ret)
117 return ret;
118
119 ret = bh_event_add_var(event, 0, "SUBSYSTEM=%s", "button");
120 if (ret)
121 return ret;
122
123 ret = bh_event_add_var(event, 0, "ACTION=%s", event->action);
124 if (ret)
125 return ret;
126
127 ret = bh_event_add_var(event, 0, "BUTTON=%s", event->name);
128 if (ret)
129 return ret;
130
131 ret = bh_event_add_var(event, 0, "SEEN=%ld", event->seen);
132 if (ret)
133 return ret;
134
135 ret = bh_event_add_var(event, 0, "SEQNUM=%llu", uevent_next_seqnum());
136
137 return ret;
138 }
139
140 static void button_hotplug_work(struct work_struct *work)
141 {
142 struct bh_event *event = container_of(work, struct bh_event, work);
143 int ret = 0;
144
145 if (!uevent_sock)
146 goto out_free_event;
147
148 event->skb = alloc_skb(BH_SKB_SIZE, GFP_KERNEL);
149 if (!event->skb)
150 goto out_free_event;
151
152 ret = bh_event_add_var(event, 0, "%s@", event->action);
153 if (ret)
154 goto out_free_skb;
155
156 ret = button_hotplug_fill_event(event);
157 if (ret)
158 goto out_free_skb;
159
160 NETLINK_CB(event->skb).dst_group = 1;
161 netlink_broadcast(uevent_sock, event->skb, 0, 1, GFP_KERNEL);
162
163 out_free_skb:
164 if (ret) {
165 BH_ERR("work error %d\n", ret);
166 kfree_skb(event->skb);
167 }
168 out_free_event:
169 kfree(event);
170 }
171
172 static int button_hotplug_create_event(char *name, unsigned long seen,
173 int pressed)
174 {
175 struct bh_event *event;
176
177 BH_DBG("create event, name=%s, seen=%lu, pressed=%d\n",
178 name, seen, pressed);
179
180 event = kzalloc(sizeof(*event), GFP_KERNEL);
181 if (!event)
182 return -ENOMEM;
183
184 event->name = name;
185 event->seen = seen;
186 event->action = pressed ? "pressed" : "released";
187
188 INIT_WORK(&event->work, (void *)(void *)button_hotplug_work);
189 schedule_work(&event->work);
190
191 return 0;
192 }
193
194 /* -------------------------------------------------------------------------*/
195
196 #ifdef CONFIG_HOTPLUG
197 static void button_hotplug_event(struct input_handle *handle,
198 unsigned int type, unsigned int code, int value)
199 {
200 struct bh_priv *priv = handle->private;
201 unsigned long seen = jiffies;
202 unsigned int btn;
203
204 BH_DBG("event type=%u, code=%u, value=%d\n", type, code, value);
205
206 if (type != EV_KEY)
207 return;
208
209 if (code < BH_BTN_MIN || code > BH_BTN_MAX)
210 return;
211
212 btn = code - BH_BTN_MIN;
213 button_hotplug_create_event(button_names[btn],
214 (seen - priv->seen[btn]) / HZ, value);
215 priv->seen[btn] = seen;
216 }
217 #else
218 static void button_hotplug_event(struct input_handle *handle,
219 unsigned int type, unsigned int code, int value)
220 {
221 }
222 #endif /* CONFIG_HOTPLUG */
223
224 static int button_hotplug_connect(struct input_handler *handler,
225 struct input_dev *dev, const struct input_device_id *id)
226 {
227 struct bh_priv *priv;
228 int ret;
229 int i;
230
231 for (i = BH_BTN_MIN; i <= BH_BTN_MAX; i++)
232 if (test_bit(i, dev->keybit))
233 break;
234
235 if (i > BH_BTN_MAX)
236 return -ENODEV;
237
238 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
239 if (!priv)
240 return -ENOMEM;
241
242 priv->handle.private = priv;
243 priv->handle.dev = dev;
244 priv->handle.handler = handler;
245 priv->handle.name = DRV_NAME;
246
247 ret = input_register_handle(&priv->handle);
248 if (ret)
249 goto err_free_priv;
250
251 ret = input_open_device(&priv->handle);
252 if (ret)
253 goto err_unregister_handle;
254
255 BH_DBG("connected to %s\n", dev->name);
256
257 return 0;
258
259 err_unregister_handle:
260 input_unregister_handle(&priv->handle);
261
262 err_free_priv:
263 kfree(priv);
264 return ret;
265 }
266
267 static void button_hotplug_disconnect(struct input_handle *handle)
268 {
269 struct bh_priv *priv = handle->private;
270
271 input_close_device(handle);
272 input_unregister_handle(handle);
273
274 kfree(priv);
275 }
276
277 static const struct input_device_id button_hotplug_ids[] = {
278 {
279 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
280 .evbit = { BIT_MASK(EV_KEY) },
281 },
282 {
283 /* Terminating entry */
284 },
285 };
286
287 MODULE_DEVICE_TABLE(input, button_hotplug_ids);
288
289 static struct input_handler button_hotplug_handler = {
290 .event = button_hotplug_event,
291 .connect = button_hotplug_connect,
292 .disconnect = button_hotplug_disconnect,
293 .name = DRV_NAME,
294 .id_table = button_hotplug_ids,
295 };
296
297 /* -------------------------------------------------------------------------*/
298
299 static int __init button_hotplug_init(void)
300 {
301 int ret;
302
303 printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
304 ret = input_register_handler(&button_hotplug_handler);
305 if (ret)
306 BH_ERR("unable to register input handler\n");
307
308 return ret;
309 }
310 module_init(button_hotplug_init);
311
312 static void __exit button_hotplug_exit(void)
313 {
314 input_unregister_handler(&button_hotplug_handler);
315 }
316 module_exit(button_hotplug_exit);
317
318 MODULE_DESCRIPTION(DRV_DESC);
319 MODULE_VERSION(DRV_VERSION);
320 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
321 MODULE_LICENSE("GPL v2");
322
This page took 0.054419 seconds and 5 git commands to generate.