Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / staging / speakup / fakekey.c
blob8f058b42f68d910aa5a681cb8a1270ebbec642b7
1 /* fakekey.c
2 * Functions for simulating keypresses.
4 * Copyright (C) 2010 the Speakup Team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/preempt.h>
19 #include <linux/percpu.h>
20 #include <linux/input.h>
22 #include "speakup.h"
24 #define PRESSED 1
25 #define RELEASED 0
27 static DEFINE_PER_CPU(int, reporting_keystroke);
29 static struct input_dev *virt_keyboard;
31 int speakup_add_virtual_keyboard(void)
33 int err;
35 virt_keyboard = input_allocate_device();
37 if (!virt_keyboard)
38 return -ENOMEM;
40 virt_keyboard->name = "Speakup";
41 virt_keyboard->id.bustype = BUS_VIRTUAL;
42 virt_keyboard->phys = "speakup/input0";
43 virt_keyboard->dev.parent = NULL;
45 __set_bit(EV_KEY, virt_keyboard->evbit);
46 __set_bit(KEY_DOWN, virt_keyboard->keybit);
48 err = input_register_device(virt_keyboard);
49 if (err) {
50 input_free_device(virt_keyboard);
51 virt_keyboard = NULL;
54 return err;
57 void speakup_remove_virtual_keyboard(void)
59 if (virt_keyboard != NULL) {
60 input_unregister_device(virt_keyboard);
61 virt_keyboard = NULL;
66 * Send a simulated down-arrow to the application.
68 void speakup_fake_down_arrow(void)
70 unsigned long flags;
72 /* disable keyboard interrupts */
73 local_irq_save(flags);
74 /* don't change CPU */
75 preempt_disable();
77 __this_cpu_write(reporting_keystroke, true);
78 input_report_key(virt_keyboard, KEY_DOWN, PRESSED);
79 input_report_key(virt_keyboard, KEY_DOWN, RELEASED);
80 input_sync(virt_keyboard);
81 __this_cpu_write(reporting_keystroke, false);
83 /* reenable preemption */
84 preempt_enable();
85 /* reenable keyboard interrupts */
86 local_irq_restore(flags);
90 * Are we handling a simulated keypress on the current CPU?
91 * Returns a boolean.
93 bool speakup_fake_key_pressed(void)
95 return this_cpu_read(reporting_keystroke);