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