86 lines
2.2 KiB
C
86 lines
2.2 KiB
C
#include <socks/device.h>
|
|
#include <socks/input.h>
|
|
#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;
|
|
|
|
struct input_device *input_device_create(void)
|
|
{
|
|
struct device *dev = device_alloc();
|
|
if (!dev) {
|
|
return NULL;
|
|
}
|
|
|
|
dev->dev_type = DEV_TYPE_INPUT;
|
|
|
|
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;
|
|
}
|
|
|
|
return INPUT_DEVICE(dev);
|
|
}
|
|
|
|
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;
|
|
socks_flags_t flags = S_NORMAL;
|
|
if (noblock) {
|
|
flags = S_NOBLOCK;
|
|
}
|
|
|
|
size_t r = ringbuffer_write(event_queue, sizeof *ev, ev, flags);
|
|
|
|
return r == sizeof *ev ? KERN_OK : KERN_WOULD_BLOCK;
|
|
}
|
|
|
|
kern_status_t input_device_read(struct device *dev, void *buf, size_t size, size_t *bytes_read, socks_flags_t flags)
|
|
{
|
|
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;
|
|
|
|
size_t r = ringbuffer_read(event_queue, size, buf, flags);
|
|
|
|
if (bytes_read) {
|
|
*bytes_read = r;
|
|
}
|
|
|
|
return KERN_OK;
|
|
}
|
|
|
|
static kern_status_t generate_name(struct input_device *dev, char out[DEV_NAME_MAX])
|
|
{
|
|
snprintf(out, DEV_NAME_MAX, "input%u", dev->i_id);
|
|
return KERN_OK;
|
|
}
|
|
|
|
kern_status_t input_device_register(struct device *dev)
|
|
{
|
|
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);
|
|
|
|
struct input_device *inputdev = &dev->input;
|
|
inputdev->i_id = id;
|
|
|
|
char name[DEV_NAME_MAX];
|
|
generate_name(inputdev, name);
|
|
char path[OBJECT_PATH_MAX];
|
|
snprintf(path, sizeof path, "/dev/input/%s", name);
|
|
|
|
return object_namespace_create_link(global_namespace(), path, &dev->dev_base);
|
|
}
|
|
|
|
struct device_type_ops input_type_ops = {
|
|
.register_device = input_device_register,
|
|
};
|