2023-04-03 16:59:14 +01:00
|
|
|
#include <socks/device.h>
|
2023-05-10 20:33:40 +01:00
|
|
|
#include <socks/input.h>
|
2023-05-11 21:19:00 +01:00
|
|
|
#include <socks/bitmap.h>
|
|
|
|
|
#include <socks/libc/stdio.h>
|
|
|
|
|
|
|
|
|
|
static DECLARE_BITMAP(input_device_ids, INPUT_DEVICE_MAX);
|
|
|
|
|
static spin_lock_t input_device_ids_lock = SPIN_LOCK_INIT;
|
2023-04-03 16:59:14 +01:00
|
|
|
|
|
|
|
|
struct input_device *input_device_create(void)
|
|
|
|
|
{
|
|
|
|
|
struct device *dev = device_alloc();
|
|
|
|
|
if (!dev) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dev->dev_type = DEV_TYPE_INPUT;
|
2023-05-10 20:33:40 +01:00
|
|
|
|
|
|
|
|
struct input_device *input_dev = INPUT_DEVICE(dev);
|
|
|
|
|
if (ringbuffer_init(&input_dev->i_events, INPUT_DEVICE_EVENT_QUEUE_SIZE * sizeof(struct input_event)) != KERN_OK) {
|
|
|
|
|
/* TODO destroy device */
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-11 21:19:00 +01:00
|
|
|
unsigned long flags;
|
|
|
|
|
spin_lock_irqsave(&input_device_ids_lock, &flags);
|
|
|
|
|
unsigned int id = bitmap_lowest_clear(input_device_ids, INPUT_DEVICE_MAX);
|
|
|
|
|
bitmap_set(input_device_ids, id);
|
|
|
|
|
spin_unlock_irqrestore(&input_device_ids_lock, flags);
|
|
|
|
|
|
|
|
|
|
input_dev->i_id = id;
|
|
|
|
|
|
2023-04-03 16:59:14 +01:00
|
|
|
return INPUT_DEVICE(dev);
|
|
|
|
|
}
|
2023-05-10 20:33:40 +01:00
|
|
|
|
|
|
|
|
kern_status_t input_device_report_event(struct input_device *dev, const struct input_event *ev, bool noblock)
|
|
|
|
|
{
|
|
|
|
|
struct ringbuffer *event_queue = &dev->i_events;
|
2023-05-14 21:11:32 +01:00
|
|
|
socks_flags_t flags = S_NORMAL;
|
2023-05-10 20:33:40 +01:00
|
|
|
if (noblock) {
|
2023-05-14 21:11:32 +01:00
|
|
|
flags = S_NOBLOCK;
|
2023-05-10 20:33:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t r = ringbuffer_write(event_queue, sizeof *ev, ev, flags);
|
|
|
|
|
|
|
|
|
|
return r == sizeof *ev ? KERN_OK : KERN_WOULD_BLOCK;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-14 21:11:32 +01:00
|
|
|
kern_status_t input_device_read(struct device *dev, void *buf, size_t size, size_t *bytes_read, socks_flags_t flags)
|
2023-05-10 20:33:40 +01:00
|
|
|
{
|
|
|
|
|
if (dev->dev_type != DEV_TYPE_INPUT || (size % sizeof (struct input_event)) != 0) {
|
|
|
|
|
return KERN_INVALID_ARGUMENT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct input_device *input_dev = INPUT_DEVICE(dev);
|
|
|
|
|
struct ringbuffer *event_queue = &input_dev->i_events;
|
|
|
|
|
|
2023-05-14 21:11:32 +01:00
|
|
|
size_t r = ringbuffer_read(event_queue, size, buf, flags);
|
2023-05-10 20:33:40 +01:00
|
|
|
|
|
|
|
|
if (bytes_read) {
|
|
|
|
|
*bytes_read = r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return KERN_OK;
|
|
|
|
|
}
|
2023-05-11 21:19:00 +01:00
|
|
|
|
|
|
|
|
kern_status_t input_device_generate_name(struct input_device *dev)
|
|
|
|
|
{
|
|
|
|
|
struct device *d = input_device_base(dev);
|
|
|
|
|
snprintf(d->dev_name, sizeof d->dev_name, "input%u", dev->i_id);
|
|
|
|
|
return KERN_OK;
|
|
|
|
|
}
|