#include #include #include #include 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); } struct input_device *input_device_from_generic(struct device *dev) { dev->dev_type = DEV_TYPE_INPUT; 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; } struct input_event new_ev = *ev; enum input_event_hook_flags hook_flags = 0; queue_foreach (struct input_event_hook, hook, &dev->i_hooks, hook_head) { if (hook->hook_callback) { hook->hook_callback(input_device_base(dev), &new_ev, &hook_flags, hook->hook_arg); } if (hook_flags & INPUT_HOOK_SQUASH_EVENT) { break; } } if (hook_flags & INPUT_HOOK_SQUASH_EVENT) { return KERN_OK; } size_t r = ringbuffer_write(event_queue, sizeof new_ev, &new_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; } kern_status_t input_device_add_hook(struct device *dev, struct input_event_hook *hook) { struct input_device *inputdev = INPUT_DEVICE(dev); if (!inputdev) { return KERN_INVALID_ARGUMENT; } queue_push_back(&inputdev->i_hooks, &hook->hook_head); return KERN_OK; } kern_status_t input_device_remove_hook(struct device *dev, struct input_event_hook *hook) { struct input_device *inputdev = INPUT_DEVICE(dev); if (!inputdev) { return KERN_INVALID_ARGUMENT; } queue_delete(&inputdev->i_hooks, &hook->hook_head); 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, .read = input_device_read, };