2023-06-10 21:41:07 +01:00
|
|
|
#include <socks/tty.h>
|
|
|
|
|
#include <socks/device.h>
|
2023-06-11 14:55:47 +01:00
|
|
|
#include <socks/console.h>
|
2023-06-11 16:47:33 +01:00
|
|
|
#include <socks/input.h>
|
2023-06-10 21:41:07 +01:00
|
|
|
#include <socks/libc/stdio.h>
|
|
|
|
|
|
2023-06-11 09:23:57 +01:00
|
|
|
static struct char_device_ops tty_ops = {
|
|
|
|
|
.read = tty_read,
|
|
|
|
|
.write = tty_write,
|
|
|
|
|
};
|
|
|
|
|
|
2023-12-30 09:09:18 +00:00
|
|
|
static spin_lock_t foreground_lock = SPIN_LOCK_INIT;
|
2023-06-11 16:47:33 +01:00
|
|
|
static struct device *foreground = NULL;
|
|
|
|
|
static struct device *foreground_input = NULL;
|
|
|
|
|
|
2023-12-30 09:09:18 +00:00
|
|
|
static spin_lock_t kernel_console_tty_lock = SPIN_LOCK_INIT;
|
|
|
|
|
static struct device *kernel_console_tty = NULL;
|
|
|
|
|
|
2023-06-11 16:47:33 +01:00
|
|
|
static void tty_input_hook_callback(struct device *dev, struct input_event *ev, enum input_event_hook_flags *flags, void *arg)
|
|
|
|
|
{
|
|
|
|
|
struct device *fg_tty = foreground;
|
|
|
|
|
|
|
|
|
|
if (!fg_tty) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tty_report_event(fg_tty, ev);
|
|
|
|
|
*flags = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct input_event_hook foreground_input_hook = {
|
|
|
|
|
.hook_callback = tty_input_hook_callback,
|
|
|
|
|
};
|
2023-06-11 14:55:47 +01:00
|
|
|
|
|
|
|
|
static void tty_console_write(struct console *con, const char *s, unsigned int len)
|
|
|
|
|
{
|
2023-12-30 09:09:18 +00:00
|
|
|
if (kernel_console_tty) {
|
2023-06-11 16:47:33 +01:00
|
|
|
size_t nr_written;
|
2023-12-30 09:09:18 +00:00
|
|
|
tty_write(kernel_console_tty, s, 0, len, &nr_written, 0);
|
2023-06-11 16:47:33 +01:00
|
|
|
}
|
2023-06-11 14:55:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct console tty_console = {
|
|
|
|
|
.c_name = "tty",
|
|
|
|
|
.c_write = tty_console_write,
|
|
|
|
|
};
|
|
|
|
|
|
2023-12-30 09:09:18 +00:00
|
|
|
void register_tty_console(void)
|
|
|
|
|
{
|
|
|
|
|
console_register(&tty_console);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void redirect_printk_to_tty(struct device *dest)
|
|
|
|
|
{
|
|
|
|
|
unsigned long flags;
|
|
|
|
|
spin_lock_irqsave(&kernel_console_tty_lock, &flags);
|
|
|
|
|
|
|
|
|
|
if (kernel_console_tty) {
|
|
|
|
|
device_deref(kernel_console_tty);
|
|
|
|
|
kernel_console_tty = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
kernel_console_tty = device_ref(dest);
|
|
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&kernel_console_tty_lock, flags);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-10 21:41:07 +01:00
|
|
|
struct device *tty_device_create(void)
|
|
|
|
|
{
|
|
|
|
|
struct char_device *cdev = char_device_create();
|
|
|
|
|
if (!cdev) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct tty_device *tty_dev = kmalloc(sizeof *tty_dev, VM_NORMAL);
|
|
|
|
|
if (!tty_dev) {
|
2023-06-11 16:47:33 +01:00
|
|
|
object_deref(char_device_object(cdev));
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-11 19:00:55 +01:00
|
|
|
kern_status_t status = ringbuffer_init(&tty_dev->tty_input, TTY_INPUT_QUEUE_SIZE * sizeof(char));
|
2023-06-11 16:47:33 +01:00
|
|
|
if (status != KERN_OK) {
|
|
|
|
|
kfree(tty_dev);
|
|
|
|
|
object_deref(char_device_object(cdev));
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tty_dev->tty_ldisc = tty_default_line_discipline();
|
|
|
|
|
|
2023-06-11 09:23:57 +01:00
|
|
|
cdev->c_ops = &tty_ops;
|
2023-06-10 21:41:07 +01:00
|
|
|
cdev->c_tty = tty_dev;
|
2023-06-11 09:23:57 +01:00
|
|
|
|
2023-06-10 21:41:07 +01:00
|
|
|
return char_device_base(cdev);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static kern_status_t generate_name(struct tty_driver *owner, struct device *dev, char out[DEV_NAME_MAX])
|
|
|
|
|
{
|
|
|
|
|
/* minor numbers start at 1. subtract 1 to start at 0 instead */
|
|
|
|
|
snprintf(out, DEV_NAME_MAX, "%s%u", owner->tty_name, dev->dev_minor - 1);
|
|
|
|
|
return KERN_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
kern_status_t tty_device_register(struct device *dev, struct tty_driver *owner, struct device *parent)
|
|
|
|
|
{
|
|
|
|
|
kern_status_t status = device_register(dev, tty_driver_base(owner), parent);
|
|
|
|
|
if (status != KERN_OK) {
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char link_name[DEV_NAME_MAX];
|
|
|
|
|
generate_name(owner, dev, link_name);
|
|
|
|
|
char link_path[OBJECT_PATH_MAX];
|
|
|
|
|
snprintf(link_path, sizeof link_path, "/dev/tty/%s", link_name);
|
|
|
|
|
|
|
|
|
|
return object_namespace_create_link(global_namespace(), link_path, &dev->dev_base);
|
|
|
|
|
}
|
2023-06-11 14:55:47 +01:00
|
|
|
|
2023-06-11 16:47:33 +01:00
|
|
|
void tty_set_foreground(struct device *tty)
|
2023-06-11 14:55:47 +01:00
|
|
|
{
|
2023-12-30 09:09:18 +00:00
|
|
|
unsigned long flags;
|
|
|
|
|
spin_lock_irqsave(&foreground_lock, &flags);
|
|
|
|
|
|
2023-06-11 16:47:33 +01:00
|
|
|
if (foreground) {
|
2023-06-14 21:45:05 +01:00
|
|
|
device_deref(foreground);
|
2023-12-30 09:09:18 +00:00
|
|
|
foreground = NULL;
|
2023-06-11 14:55:47 +01:00
|
|
|
}
|
|
|
|
|
|
2023-06-14 21:45:05 +01:00
|
|
|
foreground = device_ref(tty);
|
2023-06-11 14:55:47 +01:00
|
|
|
|
2023-12-30 09:09:18 +00:00
|
|
|
spin_unlock_irqrestore(&foreground_lock, flags);
|
2023-06-11 14:55:47 +01:00
|
|
|
}
|
2023-06-11 16:47:33 +01:00
|
|
|
|
|
|
|
|
kern_status_t tty_connect_foreground_input_device(struct device *input)
|
|
|
|
|
{
|
|
|
|
|
struct input_device *inputdev = INPUT_DEVICE(input);
|
|
|
|
|
if (!inputdev) {
|
|
|
|
|
return KERN_INVALID_ARGUMENT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned long flags;
|
|
|
|
|
spin_lock_irqsave(&foreground_lock, &flags);
|
|
|
|
|
|
|
|
|
|
if (foreground_input) {
|
|
|
|
|
struct device *prev = foreground_input;
|
|
|
|
|
foreground_input = NULL;
|
|
|
|
|
|
|
|
|
|
device_lock(prev);
|
|
|
|
|
input_device_remove_hook(prev, &foreground_input_hook);
|
|
|
|
|
device_unlock(prev);
|
2023-06-14 21:45:05 +01:00
|
|
|
device_deref(prev);
|
2023-06-11 16:47:33 +01:00
|
|
|
object_deref(&prev->dev_base);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-14 21:45:05 +01:00
|
|
|
foreground_input = device_ref(input);
|
2023-06-11 16:47:33 +01:00
|
|
|
device_lock(input);
|
|
|
|
|
input_device_add_hook(input, &foreground_input_hook);
|
|
|
|
|
device_unlock(input);
|
|
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&foreground_lock, flags);
|
|
|
|
|
|
|
|
|
|
return KERN_OK;
|
|
|
|
|
}
|