2 * Button Hotplug driver
4 * Copyright (C) 2008-2010 Gabor Juhos <juhosg@openwrt.org>
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>
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.
16 #include <linux/module.h>
17 #include <linux/version.h>
18 #include <linux/kmod.h>
19 #include <linux/input.h>
21 #include <linux/workqueue.h>
22 #include <linux/skbuff.h>
23 #include <linux/netlink.h>
24 #include <linux/kobject.h>
26 #define DRV_NAME "button-hotplug"
27 #define DRV_VERSION "0.4.1"
28 #define DRV_DESC "Button Hotplug driver"
30 #define BH_SKB_SIZE 2048
32 #define PFX DRV_NAME ": "
37 #define BH_DBG(fmt, args...) printk(KERN_DEBUG "%s: " fmt, DRV_NAME, ##args )
39 #define BH_DBG(fmt, args...) do {} while (0)
42 #define BH_ERR(fmt, args...) printk(KERN_ERR "%s: " fmt, DRV_NAME, ##args )
45 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
50 struct input_handle handle;
59 struct work_struct work;
67 extern u64 uevent_next_seqnum(void);
69 #define BH_MAP(_code, _name) \
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"),
88 BH_MAP(KEY_WPS_BUTTON, "wps"),
89 #endif /* KEY_WPS_BUTTON */
92 /* -------------------------------------------------------------------------*/
94 static int bh_event_add_var(struct bh_event *event, int argv,
95 const char *format, ...)
105 va_start(args, format);
106 len = vsnprintf(buf, sizeof(buf), format, args);
109 if (len >= sizeof(buf)) {
110 BH_ERR("buffer size too small\n");
115 s = skb_put(event->skb, len + 1);
118 BH_DBG("added variable '%s'\n", s);
123 static int button_hotplug_fill_event(struct bh_event *event)
127 ret = bh_event_add_var(event, 0, "HOME=%s", "/");
131 ret = bh_event_add_var(event, 0, "PATH=%s",
132 "/sbin:/bin:/usr/sbin:/usr/bin");
136 ret = bh_event_add_var(event, 0, "SUBSYSTEM=%s", "button");
140 ret = bh_event_add_var(event, 0, "ACTION=%s", event->action);
144 ret = bh_event_add_var(event, 0, "BUTTON=%s", event->name);
148 ret = bh_event_add_var(event, 0, "SEEN=%ld", event->seen);
152 ret = bh_event_add_var(event, 0, "SEQNUM=%llu", uevent_next_seqnum());
157 static void button_hotplug_work(struct work_struct *work)
159 struct bh_event *event = container_of(work, struct bh_event, work);
162 event->skb = alloc_skb(BH_SKB_SIZE, GFP_KERNEL);
166 ret = bh_event_add_var(event, 0, "%s@", event->action);
170 ret = button_hotplug_fill_event(event);
174 NETLINK_CB(event->skb).dst_group = 1;
175 broadcast_uevent(event->skb, 0, 1, GFP_KERNEL);
179 BH_ERR("work error %d\n", ret);
180 kfree_skb(event->skb);
186 static int button_hotplug_create_event(const char *name, unsigned long seen,
189 struct bh_event *event;
191 BH_DBG("create event, name=%s, seen=%lu, pressed=%d\n",
192 name, seen, pressed);
194 event = kzalloc(sizeof(*event), GFP_KERNEL);
200 event->action = pressed ? "pressed" : "released";
202 INIT_WORK(&event->work, (void *)(void *)button_hotplug_work);
203 schedule_work(&event->work);
208 /* -------------------------------------------------------------------------*/
210 #ifdef CONFIG_HOTPLUG
211 static int button_get_index(unsigned int code)
215 for (i = 0; i < ARRAY_SIZE(button_map); i++)
216 if (button_map[i].code == code)
221 static void button_hotplug_event(struct input_handle *handle,
222 unsigned int type, unsigned int code, int value)
224 struct bh_priv *priv = handle->private;
225 unsigned long seen = jiffies;
228 BH_DBG("event type=%u, code=%u, value=%d\n", type, code, value);
233 btn = button_get_index(code);
237 button_hotplug_create_event(button_map[btn].name,
238 (seen - priv->seen[btn]) / HZ, value);
239 priv->seen[btn] = seen;
242 static void button_hotplug_event(struct input_handle *handle,
243 unsigned int type, unsigned int code, int value)
246 #endif /* CONFIG_HOTPLUG */
248 static int button_hotplug_connect(struct input_handler *handler,
249 struct input_dev *dev, const struct input_device_id *id)
251 struct bh_priv *priv;
255 for (i = 0; i < ARRAY_SIZE(button_map); i++)
256 if (test_bit(button_map[i].code, dev->keybit))
259 if (i == ARRAY_SIZE(button_map))
262 priv = kzalloc(sizeof(*priv) +
263 (sizeof(unsigned long) * ARRAY_SIZE(button_map)),
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;
274 ret = input_register_handle(&priv->handle);
278 ret = input_open_device(&priv->handle);
280 goto err_unregister_handle;
282 BH_DBG("connected to %s\n", dev->name);
286 err_unregister_handle:
287 input_unregister_handle(&priv->handle);
294 static void button_hotplug_disconnect(struct input_handle *handle)
296 struct bh_priv *priv = handle->private;
298 input_close_device(handle);
299 input_unregister_handle(handle);
304 static const struct input_device_id button_hotplug_ids[] = {
306 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
307 .evbit = { BIT_MASK(EV_KEY) },
310 /* Terminating entry */
314 MODULE_DEVICE_TABLE(input, button_hotplug_ids);
316 static struct input_handler button_hotplug_handler = {
317 .event = button_hotplug_event,
318 .connect = button_hotplug_connect,
319 .disconnect = button_hotplug_disconnect,
321 .id_table = button_hotplug_ids,
324 /* -------------------------------------------------------------------------*/
326 static int __init button_hotplug_init(void)
330 printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
331 ret = input_register_handler(&button_hotplug_handler);
333 BH_ERR("unable to register input handler\n");
337 module_init(button_hotplug_init);
339 static void __exit button_hotplug_exit(void)
341 input_unregister_handler(&button_hotplug_handler);
343 module_exit(button_hotplug_exit);
345 MODULE_DESCRIPTION(DRV_DESC);
346 MODULE_VERSION(DRV_VERSION);
347 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
348 MODULE_LICENSE("GPL v2");