#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; } 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; 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; enum ringbuffer_flags flags = RB_NORMAL; if (noblock) { flags = RB_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) { 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, RB_NORMAL); if (bytes_read) { *bytes_read = r; } return KERN_OK; } 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; }