^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* fakekey.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Functions for simulating keypresses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2010 the Speakup Team
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/preempt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/percpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "speakup.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define PRESSED 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define RELEASED 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static DEFINE_PER_CPU(int, reporting_keystroke);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static struct input_dev *virt_keyboard;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) int speakup_add_virtual_keyboard(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) virt_keyboard = input_allocate_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (!virt_keyboard)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) virt_keyboard->name = "Speakup";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) virt_keyboard->id.bustype = BUS_VIRTUAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) virt_keyboard->phys = "speakup/input0";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) virt_keyboard->dev.parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) __set_bit(EV_KEY, virt_keyboard->evbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) __set_bit(KEY_DOWN, virt_keyboard->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) err = input_register_device(virt_keyboard);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) input_free_device(virt_keyboard);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) virt_keyboard = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) void speakup_remove_virtual_keyboard(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (virt_keyboard) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) input_unregister_device(virt_keyboard);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) virt_keyboard = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * Send a simulated down-arrow to the application.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) void speakup_fake_down_arrow(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /* disable keyboard interrupts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /* don't change CPU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) __this_cpu_write(reporting_keystroke, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) input_report_key(virt_keyboard, KEY_DOWN, PRESSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) input_report_key(virt_keyboard, KEY_DOWN, RELEASED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) input_sync(virt_keyboard);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) __this_cpu_write(reporting_keystroke, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /* reenable preemption */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /* reenable keyboard interrupts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * Are we handling a simulated keypress on the current CPU?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * Returns a boolean.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) bool speakup_fake_key_pressed(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return this_cpu_read(reporting_keystroke);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }