aboutsummaryrefslogtreecommitdiffstats
path: root/package/kernel/button-hotplug/src/button-hotplug.c
blob: 5294a9700df117416ee171258330e29b4c80cf6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 *  Button Hotplug driver
 *
 *  Copyright (C) 2008-2010 Gabor Juhos <juhosg@openwrt.org>
 *
 *  Based on the diag.c - GPIO interface driver for Broadcom boards
 *    Copyright (C) 2006 Mike Baker <mbm@openwrt.org>,
 *    Copyright (C) 2006-2007 Felix Fietkau <nbd@nbd.name>
 *    Copyright (C) 2008 Andy Boyett <agb@openwrt.org>
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 2 as published
 *  by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/version.h>
#include <linux/kmod.h>
#include <linux/input.h>

#include <linux/workqueue.h>
#include <linux/skbuff.h>
#include <linux/netlink.h>
#include <linux/kobject.h>

#define DRV_NAME	"button-hotplug"
#define DRV_VERSION	"0.4.1"
#define DRV_DESC	"Button Hotplug driver"

#define BH_SKB_SIZE	2048

#define PFX	DRV_NAME ": "

#undef BH_DEBUG

#ifdef BH_DEBUG
#define BH_DBG(fmt, args...) printk(KERN_DEBUG "%s: " fmt, DRV_NAME, ##args )
#else
#define BH_DBG(fmt, args...) do {} while (0)
#endif

#define BH_ERR(fmt, args...) printk(KERN_ERR "%s: " fmt, DRV_NAME, ##args )

#ifndef BIT_MASK
#define BIT_MASK(nr)            (1UL << ((nr) % BITS_PER_LONG))
#endif

struct bh_priv {
	unsigned long		*seen;
	struct input_handle	handle;
};

struct bh_event {
	const char		*name;
	char			*action;
	unsigned long		seen;

	struct sk_buff		*skb;
	struct work_struct	work;
};

struct bh_map {
	unsigned int	code;
	const char	*name;
};

extern u64 uevent_next_seqnum(void);

#define BH_MAP(_code, _name)		\
	{				\
		.code = (_code),	\
		.name = (_name),	\
	}

static struct bh_map button_map[] = {
	BH_MAP(BTN_0,		"BTN_0"),
	BH_MAP(BTN_1,		"BTN_1"),
	BH_MAP(BTN_2,		"BTN_2"),
	BH_MAP(BTN_3,		"BTN_3"),
	BH_MAP(BTN_4,		"BTN_4"),
	BH_MAP(BTN_5,		"BTN_5"),
	BH_MAP(BTN_6,		"BTN_6"),
	BH_MAP(BTN_7,		"BTN_7"),
	BH_MAP(BTN_8,		"BTN_8"),
	BH_MAP(BTN_9,		"BTN_9"),
	BH_MAP(KEY_RESTART,	"reset"),
	BH_MAP(KEY_POWER,	"power"),
	BH_MAP(KEY_RFKILL,	"rfkill"),
	BH_MAP(KEY_WPS_BUTTON,	"wps"),
	BH_MAP(KEY_WIMAX,	"wwan"),
};

/* -------------------------------------------------------------------------*/

static int bh_event_add_var(struct bh_event *event, int argv,
		const char *format, ...)
{
	static char buf[128];
	char *s;
	va_list args;
	int len;

	if (argv)
		return 0;

	va_start(args, format);
	len = vsnprintf(buf, sizeof(buf), format, args);
	va_end(args);

	if (len >= sizeof(buf)) {
		BH_ERR("buffer size too small\n");
		WARN_ON(1);
		return -ENOMEM;
	}

	s = skb_put(event->skb, len + 1);
	strcpy(s, buf);

	BH_DBG("added variable '%s'\n", s);

	return 0;
}

static int button_hotplug_fill_event(struct bh_event *event)
{
	int ret;

	ret = bh_event_add_var(event, 0, "HOME=%s", "/");
	if (ret)
		return ret;

	ret = bh_event_add_var(event, 0, "PATH=%s",
					"/sbin:/bin:/usr/sbin:/usr/bin");
	if (ret)
		return ret;

	ret = bh_event_add_var(event, 0, "SUBSYSTEM=%s", "button");
	if (ret)
		return ret;

	ret = bh_event_add_var(event, 0, "ACTION=%s", event->action);
	if (ret)
		return ret;

	ret = bh_event_add_var(event, 0, "BUTTON=%s", event->name);
	if (ret)
		return ret;

	ret = bh_event_add_var(event, 0, "SEEN=%ld", event->seen);
	if (ret)
		return ret;

	ret = bh_event_add_var(event, 0, "SEQNUM=%llu", uevent_next_seqnum());

	return ret;
}

static void button_hotplug_work(struct work_struct *work)
{
	struct bh_event *event = container_of(work, struct bh_event, work);
	int ret = 0;

	event->skb = alloc_skb(BH_SKB_SIZE, GFP_KERNEL);
	if (!event->skb)
		goto out_free_event;

	ret = bh_event_add_var(event, 0, "%s@", event->action);
	if (ret)
		goto out_free_skb;

	ret = button_hotplug_fill_event(event);
	if (ret)
		goto out_free_skb;

	NETLINK_CB(event->skb).dst_group = 1;
	broadcast_uevent(event->skb, 0, 1, GFP_KERNEL);

 out_free_skb:
	if (ret) {
		BH_ERR("work error %d\n", ret);
		kfree_skb(event->skb);
	}
 out_free_event:
	kfree(event);
}

static int button_hotplug_create_event(const char *name, unsigned long seen,
		int pressed)
{
	struct bh_event *event;

	BH_DBG("create event, name=%s, seen=%lu, pressed=%d\n",
		name, seen, pressed);

	event = kzalloc(sizeof(*event), GFP_KERNEL);
	if (!event)
		return -ENOMEM;

	event->name = name;
	event->seen = seen;
	event->action = pressed ? "pressed" : "released";

	INIT_WORK(&event->work, (void *)(void *)button_hotplug_work);
	schedule_work(&event->work);

	return 0;
}

/* -------------------------------------------------------------------------*/

static int button_get_index(unsigned int code)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(button_map); i++)
		if (button_map[i].code == code)
			return i;

	return -1;
}
static void button_hotplug_event(struct input_handle *handle,
			   unsigned int type, unsigned int code, int value)
{
	struct bh_priv *priv = handle->private;
	unsigned long seen = jiffies;
	int btn;

	BH_DBG("event type=%u, code=%u, value=%d\n", type, code, value);

	if (type != EV_KEY)
		return;

	btn = button_get_index(code);
	if (btn < 0)
		return;

	button_hotplug_create_event(button_map[btn].name,
			(seen - priv->seen[btn]) / HZ, value);
	priv->seen[btn] = seen;
}

static int button_hotplug_connect(struct input_handler *handler,
		struct input_dev *dev, const struct input_device_id *id)
{
	struct bh_priv *priv;
	int ret;
	int i;

	for (i = 0; i < ARRAY_SIZE(button_map); i++)
		if (test_bit(button_map[i].code, dev->keybit))
			break;

	if (i == ARRAY_SIZE(button_map))
		return -ENODEV;

	priv = kzalloc(sizeof(*priv) +
		       (sizeof(unsigned long) * ARRAY_SIZE(button_map)),
		       GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	priv->seen = (unsigned long *) &priv[1];
	priv->handle.private = priv;
	priv->handle.dev = dev;
	priv->handle.handler = handler;
	priv->handle.name = DRV_NAME;

	ret = input_register_handle(&priv->handle);
	if (ret)
		goto err_free_priv;

	ret = input_open_device(&priv->handle);
	if (ret)
		goto err_unregister_handle;

	BH_DBG("connected to %s\n", dev->name);

	return 0;

 err_unregister_handle:
	input_unregister_handle(&priv->handle);

 err_free_priv:
	kfree(priv);
	return ret;
}

static void button_hotplug_disconnect(struct input_handle *handle)
{
	struct bh_priv *priv = handle->private;

	input_close_device(handle);
	input_unregister_handle(handle);

	kfree(priv);
}

static const struct input_device_id button_hotplug_ids[] = {
	{
                .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
                .evbit = { BIT_MASK(EV_KEY) },
        },
	{
		/* Terminating entry */
	},
};

MODULE_DEVICE_TABLE(input, button_hotplug_ids);

static struct input_handler button_hotplug_handler = {
	.event =	button_hotplug_event,
	.connect =	button_hotplug_connect,
	.disconnect =	button_hotplug_disconnect,
	.name =		DRV_NAME,
	.id_table =	button_hotplug_ids,
};

/* -------------------------------------------------------------------------*/

static int __init button_hotplug_init(void)
{
	int ret;

	printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
	ret = input_register_handler(&button_hotplug_handler);
	if (ret)
		BH_ERR("unable to register input handler\n");

	return ret;
}
module_init(button_hotplug_init);

static void __exit button_hotplug_exit(void)
{
	input_unregister_handler(&button_hotplug_handler);
}
module_exit(button_hotplug_exit);

MODULE_DESCRIPTION(DRV_DESC);
MODULE_VERSION(DRV_VERSION);
MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
MODULE_LICENSE("GPL v2");
span class="n">irq_mapping_update_lock); } int bind_evtchn_to_irq(int evtchn) { int irq; mtx_lock(&irq_mapping_update_lock); if ( (irq = evtchn_to_irq[evtchn]) == -1 ) { irq = find_unbound_irq(); evtchn_to_irq[evtchn] = irq; irq_to_evtchn[irq] = evtchn; } irq_bindcount[irq]++; mtx_unlock(&irq_mapping_update_lock); return irq; } void unbind_evtchn_from_irq(int evtchn) { int irq = evtchn_to_irq[evtchn]; mtx_lock(&irq_mapping_update_lock); if ( --irq_bindcount[irq] == 0 ) { evtchn_to_irq[evtchn] = -1; irq_to_evtchn[irq] = -1; } mtx_unlock(&irq_mapping_update_lock); } /* * Interface to generic handling in intr_machdep.c */ /*------------ interrupt handling --------------------------------------*/ #define TODO printf("%s: not implemented!\n", __func__) struct mtx xenpic_lock; struct xenpic_intsrc { struct intsrc xp_intsrc; uint8_t xp_vector; boolean_t xp_masked; }; struct xenpic { struct pic xp_pic; /* this MUST be first */ uint16_t xp_numintr; struct xenpic_intsrc xp_pins[0]; }; static void xenpic_enable_dynirq_source(struct intsrc *isrc); static void xenpic_disable_dynirq_source(struct intsrc *isrc, int); static void xenpic_eoi_source(struct intsrc *isrc); static void xenpic_enable_dynirq_intr(struct intsrc *isrc); static int xenpic_vector(struct intsrc *isrc); static int xenpic_source_pending(struct intsrc *isrc); static void xenpic_suspend(struct intsrc *isrc); static void xenpic_resume(struct intsrc *isrc); struct pic xenpic_template = { xenpic_enable_dynirq_source, xenpic_disable_dynirq_source, xenpic_eoi_source, xenpic_enable_dynirq_intr, xenpic_vector, xenpic_source_pending, xenpic_suspend, xenpic_resume }; void xenpic_enable_dynirq_source(struct intsrc *isrc) { unsigned int irq; struct xenpic_intsrc *xp; xp = (struct xenpic_intsrc *)isrc; if (xp->xp_masked) { irq = xenpic_vector(isrc); unmask_evtchn(irq_to_evtchn[irq]); xp->xp_masked = FALSE; } } static void xenpic_disable_dynirq_source(struct intsrc *isrc, int foo) { unsigned int irq; struct xenpic_intsrc *xp; xp = (struct xenpic_intsrc *)isrc; if (!xp->xp_masked) { irq = xenpic_vector(isrc); mask_evtchn(irq_to_evtchn[irq]); xp->xp_masked = TRUE; } } static void xenpic_enable_dynirq_intr(struct intsrc *isrc) { unsigned int irq; irq = xenpic_vector(isrc); unmask_evtchn(irq_to_evtchn[irq]); } static void xenpic_eoi_source(struct intsrc *isrc) { unsigned int irq = xenpic_vector(isrc); clear_evtchn(irq_to_evtchn[irq]); } static int xenpic_vector(struct intsrc *isrc) { struct xenpic_intsrc *pin = (struct xenpic_intsrc *)isrc; return (pin->xp_vector); } static int xenpic_source_pending(struct intsrc *isrc) { TODO; return 0; } static void xenpic_suspend(struct intsrc *isrc) { TODO; } static void xenpic_resume(struct intsrc *isrc) { TODO; } #ifdef CONFIG_PHYSDEV /* required for support of physical devices */ static inline void pirq_unmask_notify(int pirq) { physdev_op_t op; if ( unlikely(test_bit(pirq, &pirq_needs_unmask_notify[0])) ) { op.cmd = PHYSDEVOP_IRQ_UNMASK_NOTIFY; (void)HYPERVISOR_physdev_op(&op); } } static inline void pirq_query_unmask(int pirq) { physdev_op_t op; op.cmd = PHYSDEVOP_IRQ_STATUS_QUERY; op.u.irq_status_query.irq = pirq; (void)HYPERVISOR_physdev_op(&op); clear_bit(pirq, &pirq_needs_unmask_notify[0]); if ( op.u.irq_status_query.flags & PHYSDEVOP_IRQ_NEEDS_UNMASK_NOTIFY ) set_bit(pirq, &pirq_needs_unmask_notify[0]); } /* * On startup, if there is no action associated with the IRQ then we are * probing. In this case we should not share with others as it will confuse us. */ #define probing_irq(_irq) (irq_desc[(_irq)].action == NULL) static unsigned int startup_pirq(unsigned int irq) { evtchn_op_t op; int evtchn; op.cmd = EVTCHNOP_bind_pirq; op.u.bind_pirq.pirq = irq; /* NB. We are happy to share unless we are probing. */ op.u.bind_pirq.flags = probing_irq(irq) ? 0 : BIND_PIRQ__WILL_SHARE; if ( HYPERVISOR_event_channel_op(&op) != 0 ) { if ( !probing_irq(irq) ) /* Some failures are expected when probing. */ printk(KERN_INFO "Failed to obtain physical IRQ %d\n", irq); return 0; } evtchn = op.u.bind_pirq.port; pirq_query_unmask(irq_to_pirq(irq)); evtchn_to_irq[evtchn] = irq; irq_to_evtchn[irq] = evtchn; unmask_evtchn(evtchn); pirq_unmask_notify(irq_to_pirq(irq)); return 0; } static void shutdown_pirq(unsigned int irq) { evtchn_op_t op; int evtchn = irq_to_evtchn[irq]; if ( !VALID_EVTCHN(evtchn) ) return; mask_evtchn(evtchn); op.cmd = EVTCHNOP_close; op.u.close.dom = DOMID_SELF; op.u.close.port = evtchn; if ( HYPERVISOR_event_channel_op(&op) != 0 ) panic("Failed to unbind physical IRQ %d\n", irq); evtchn_to_irq[evtchn] = -1; irq_to_evtchn[irq] = -1; } static void enable_pirq(unsigned int irq) { int evtchn = irq_to_evtchn[irq]; if ( !VALID_EVTCHN(evtchn) ) return; unmask_evtchn(evtchn); pirq_unmask_notify(irq_to_pirq(irq)); } static void disable_pirq(unsigned int irq) { int evtchn = irq_to_evtchn[irq]; if ( !VALID_EVTCHN(evtchn) ) return; mask_evtchn(evtchn); } static void ack_pirq(unsigned int irq) { int evtchn = irq_to_evtchn[irq]; if ( !VALID_EVTCHN(evtchn) ) return; mask_evtchn(evtchn); clear_evtchn(evtchn); } static void end_pirq(unsigned int irq) { int evtchn = irq_to_evtchn[irq]; if ( !VALID_EVTCHN(evtchn) ) return; if ( !(irq_desc[irq].status & IRQ_DISABLED) ) { unmask_evtchn(evtchn); pirq_unmask_notify(irq_to_pirq(irq)); } } static struct hw_interrupt_type pirq_type = { "Phys-irq", startup_pirq, shutdown_pirq, enable_pirq, disable_pirq, ack_pirq, end_pirq, NULL }; #endif #if 0 static void misdirect_interrupt(void *sc) { } #endif void irq_suspend(void) { int virq, irq, evtchn; /* Unbind VIRQs from event channels. */ for ( virq = 0; virq < NR_VIRQS; virq++ ) { if ( (irq = PCPU_GET(virq_to_irq)[virq]) == -1 ) continue; evtchn = irq_to_evtchn[irq]; /* Mark the event channel as unused in our table. */ evtchn_to_irq[evtchn] = -1; irq_to_evtchn[irq] = -1; } /* * We should now be unbound from all event channels. Stale bindings to * PIRQs and/or inter-domain event channels will cause us to barf here. */ for ( evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++ ) if ( evtchn_to_irq[evtchn] != -1 ) panic("Suspend attempted while bound to evtchn %d.\n", evtchn); } void irq_resume(void) { evtchn_op_t op; int virq, irq, evtchn; for ( evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++ ) mask_evtchn(evtchn); /* New event-channel space is not 'live' yet. */ for ( virq = 0; virq < NR_VIRQS; virq++ ) { if ( (irq = PCPU_GET(virq_to_irq)[virq]) == -1 ) continue; /* Get a new binding from Xen. */ op.cmd = EVTCHNOP_bind_virq; op.u.bind_virq.virq = virq; if ( HYPERVISOR_event_channel_op(&op) != 0 ) panic("Failed to bind virtual IRQ %d\n", virq); evtchn = op.u.bind_virq.port; /* Record the new mapping. */ evtchn_to_irq[evtchn] = irq; irq_to_evtchn[irq] = evtchn; /* Ready for use. */ unmask_evtchn(evtchn); } } void ap_evtchn_init(int cpu) { int i; /* XXX -- expedience hack */ PCPU_SET(virq_to_irq, (int *)&virq_to_irq[cpu]); PCPU_SET(ipi_to_evtchn, (int *)&ipi_to_evtchn[cpu]); /* No VIRQ -> IRQ mappings. */ for ( i = 0; i < NR_VIRQS; i++ ) PCPU_GET(virq_to_irq)[i] = -1; } static void evtchn_init(void *dummy __unused) { int i; struct xenpic *xp; struct xenpic_intsrc *pin; /* XXX -- expedience hack */ PCPU_SET(virq_to_irq, (int *)&virq_to_irq[0]); PCPU_SET(ipi_to_evtchn, (int *)&ipi_to_evtchn[0]); /* No VIRQ -> IRQ mappings. */ for ( i = 0; i < NR_VIRQS; i++ ) PCPU_GET(virq_to_irq)[i] = -1; /* No event-channel -> IRQ mappings. */ for ( i = 0; i < NR_EVENT_CHANNELS; i++ ) { evtchn_to_irq[i] = -1; mask_evtchn(i); /* No event channels are 'live' right now. */ } /* No IRQ -> event-channel mappings. */ for ( i = 0; i < NR_IRQS; i++ ) irq_to_evtchn[i] = -1; xp = malloc(sizeof(struct xenpic) + NR_DYNIRQS*sizeof(struct xenpic_intsrc), M_DEVBUF, M_WAITOK); xp->xp_pic = xenpic_template; xp->xp_numintr = NR_DYNIRQS; bzero(xp->xp_pins, sizeof(struct xenpic_intsrc) * NR_DYNIRQS); for ( i = 0, pin = xp->xp_pins; i < NR_DYNIRQS; i++, pin++ ) { /* Dynamic IRQ space is currently unbound. Zero the refcnts. */ irq_bindcount[dynirq_to_irq(i)] = 0; pin->xp_intsrc.is_pic = (struct pic *)xp; pin->xp_vector = i; intr_register_source(&pin->xp_intsrc); } /* We don't currently have any support for physical devices in XenoFreeBSD * so leaving this out for the moment for the sake of expediency. */ #ifdef notyet for ( i = 0; i < NR_PIRQS; i++ ) { /* Phys IRQ space is statically bound (1:1 mapping). Nail refcnts. */ irq_bindcount[pirq_to_irq(i)] = 1; irq_desc[pirq_to_irq(i)].status = IRQ_DISABLED; irq_desc[pirq_to_irq(i)].action = 0; irq_desc[pirq_to_irq(i)].depth = 1; irq_desc[pirq_to_irq(i)].handler = &pirq_type; } #endif #if 0 (void) intr_add_handler("xb_mis", bind_virq_to_irq(VIRQ_MISDIRECT), (driver_intr_t *)misdirect_interrupt, NULL, INTR_TYPE_MISC, NULL); #endif } SYSINIT(evtchn_init, SI_SUB_INTR, SI_ORDER_ANY, evtchn_init, NULL); /* * xenpic_lock: in order to allow an interrupt to occur in a critical * section, to set pcpu->ipending (etc...) properly, we * must be able to get the icu lock, so it can't be * under witness. */ MTX_SYSINIT(irq_mapping_update_lock, &irq_mapping_update_lock, "xp", MTX_DEF|MTX_NOWITNESS);