2 * Button Hotplug driver
4 * Copyright (C) 2008 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>
26 #define DRV_NAME "button-hotplug"
27 #define DRV_VERSION "0.3.1"
28 #define DRV_DESC "Button Hotplug driver"
30 #define BH_SKB_SIZE 2048
32 #define BH_BTN_MIN BTN_0
33 #define BH_BTN_MAX BTN_9
35 #define BH_BTN_COUNT (BH_BTN_MAX - BH_BTN_MIN + 1)
37 #define PFX DRV_NAME ": "
42 #define BH_DBG(fmt, args...) printk(KERN_DEBUG "%s: " fmt, DRV_NAME, ##args )
44 #define BH_DBG(fmt, args...) do {} while (0)
47 #define BH_ERR(fmt, args...) printk(KERN_ERR "%s: " fmt, DRV_NAME, ##args )
50 #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
54 unsigned long seen
[BH_BTN_COUNT
];
55 struct input_handle handle
;
64 struct work_struct work
;
67 extern struct sock
*uevent_sock
;
68 extern u64
uevent_next_seqnum(void);
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"
75 /* -------------------------------------------------------------------------*/
77 static int bh_event_add_var(struct bh_event
*event
, int argv
,
78 const char *format
, ...)
88 va_start(args
, format
);
89 len
= vsnprintf(buf
, sizeof(buf
), format
, args
);
92 if (len
>= sizeof(buf
)) {
93 BH_ERR("buffer size too small\n");
98 s
= skb_put(event
->skb
, len
+ 1);
101 BH_DBG("added variable '%s'\n", s
);
106 static int button_hotplug_fill_event(struct bh_event
*event
)
110 ret
= bh_event_add_var(event
, 0, "HOME=%s", "/");
114 ret
= bh_event_add_var(event
, 0, "PATH=%s",
115 "/sbin:/bin:/usr/sbin:/usr/bin");
119 ret
= bh_event_add_var(event
, 0, "SUBSYSTEM=%s", "button");
123 ret
= bh_event_add_var(event
, 0, "ACTION=%s", event
->action
);
127 ret
= bh_event_add_var(event
, 0, "BUTTON=%s", event
->name
);
131 ret
= bh_event_add_var(event
, 0, "SEEN=%ld", event
->seen
);
135 ret
= bh_event_add_var(event
, 0, "SEQNUM=%llu", uevent_next_seqnum());
140 static void button_hotplug_work(struct work_struct
*work
)
142 struct bh_event
*event
= container_of(work
, struct bh_event
, work
);
148 event
->skb
= alloc_skb(BH_SKB_SIZE
, GFP_KERNEL
);
152 ret
= bh_event_add_var(event
, 0, "%s@", event
->action
);
156 ret
= button_hotplug_fill_event(event
);
160 NETLINK_CB(event
->skb
).dst_group
= 1;
161 netlink_broadcast(uevent_sock
, event
->skb
, 0, 1, GFP_KERNEL
);
165 BH_ERR("work error %d\n", ret
);
166 kfree_skb(event
->skb
);
172 static int button_hotplug_create_event(char *name
, unsigned long seen
,
175 struct bh_event
*event
;
177 BH_DBG("create event, name=%s, seen=%lu, pressed=%d\n",
178 name
, seen
, pressed
);
180 event
= kzalloc(sizeof(*event
), GFP_KERNEL
);
186 event
->action
= pressed
? "pressed" : "released";
188 INIT_WORK(&event
->work
, (void *)(void *)button_hotplug_work
);
189 schedule_work(&event
->work
);
194 /* -------------------------------------------------------------------------*/
196 #ifdef CONFIG_HOTPLUG
197 static void button_hotplug_event(struct input_handle
*handle
,
198 unsigned int type
, unsigned int code
, int value
)
200 struct bh_priv
*priv
= handle
->private;
201 unsigned long seen
= jiffies
;
204 BH_DBG("event type=%u, code=%u, value=%d\n", type
, code
, value
);
209 if (code
< BH_BTN_MIN
|| code
> BH_BTN_MAX
)
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
;
218 static void button_hotplug_event(struct input_handle
*handle
,
219 unsigned int type
, unsigned int code
, int value
)
222 #endif /* CONFIG_HOTPLUG */
224 static int button_hotplug_connect(struct input_handler
*handler
,
225 struct input_dev
*dev
, const struct input_device_id
*id
)
227 struct bh_priv
*priv
;
231 for (i
= BH_BTN_MIN
; i
<= BH_BTN_MAX
; i
++)
232 if (test_bit(i
, dev
->keybit
))
238 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
242 priv
->handle
.private = priv
;
243 priv
->handle
.dev
= dev
;
244 priv
->handle
.handler
= handler
;
245 priv
->handle
.name
= DRV_NAME
;
247 ret
= input_register_handle(&priv
->handle
);
251 ret
= input_open_device(&priv
->handle
);
253 goto err_unregister_handle
;
255 BH_DBG("connected to %s\n", dev
->name
);
259 err_unregister_handle
:
260 input_unregister_handle(&priv
->handle
);
267 static void button_hotplug_disconnect(struct input_handle
*handle
)
269 struct bh_priv
*priv
= handle
->private;
271 input_close_device(handle
);
272 input_unregister_handle(handle
);
277 static const struct input_device_id button_hotplug_ids
[] = {
279 .flags
= INPUT_DEVICE_ID_MATCH_EVBIT
,
280 .evbit
= { BIT_MASK(EV_KEY
) },
283 /* Terminating entry */
287 MODULE_DEVICE_TABLE(input
, button_hotplug_ids
);
289 static struct input_handler button_hotplug_handler
= {
290 .event
= button_hotplug_event
,
291 .connect
= button_hotplug_connect
,
292 .disconnect
= button_hotplug_disconnect
,
294 .id_table
= button_hotplug_ids
,
297 /* -------------------------------------------------------------------------*/
299 static int __init
button_hotplug_init(void)
303 printk(KERN_INFO DRV_DESC
" version " DRV_VERSION
"\n");
304 ret
= input_register_handler(&button_hotplug_handler
);
306 BH_ERR("unable to register input handler\n");
310 module_init(button_hotplug_init
);
312 static void __exit
button_hotplug_exit(void)
314 input_unregister_handler(&button_hotplug_handler
);
316 module_exit(button_hotplug_exit
);
318 MODULE_DESCRIPTION(DRV_DESC
);
319 MODULE_VERSION(DRV_VERSION
);
320 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
321 MODULE_LICENSE("GPL v2");