2 * User-space I/O driver support for HID subsystem
3 * Copyright (c) 2012 David Herrmann
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
13 #include <linux/atomic.h>
14 #include <linux/compat.h>
15 #include <linux/device.h>
17 #include <linux/hid.h>
18 #include <linux/input.h>
19 #include <linux/miscdevice.h>
20 #include <linux/module.h>
21 #include <linux/mutex.h>
22 #include <linux/poll.h>
23 #include <linux/sched.h>
24 #include <linux/spinlock.h>
25 #include <linux/uhid.h>
26 #include <linux/wait.h>
28 #define UHID_NAME "uhid"
29 #define UHID_BUFSIZE 32
38 struct hid_device *hid;
39 struct uhid_event input_buf;
41 wait_queue_head_t waitq;
45 struct uhid_event *outq[UHID_BUFSIZE];
47 struct mutex report_lock;
48 wait_queue_head_t report_wait;
51 struct uhid_event report_buf;
54 static struct miscdevice uhid_misc;
56 static void uhid_queue(struct uhid_device *uhid, struct uhid_event *ev)
60 newhead = (uhid->head + 1) % UHID_BUFSIZE;
62 if (newhead != uhid->tail) {
63 uhid->outq[uhid->head] = ev;
65 wake_up_interruptible(&uhid->waitq);
67 hid_warn(uhid->hid, "Output queue is full\n");
72 static int uhid_queue_event(struct uhid_device *uhid, __u32 event)
75 struct uhid_event *ev;
77 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
83 spin_lock_irqsave(&uhid->qlock, flags);
85 spin_unlock_irqrestore(&uhid->qlock, flags);
90 static int uhid_hid_start(struct hid_device *hid)
92 struct uhid_device *uhid = hid->driver_data;
94 return uhid_queue_event(uhid, UHID_START);
97 static void uhid_hid_stop(struct hid_device *hid)
99 struct uhid_device *uhid = hid->driver_data;
102 uhid_queue_event(uhid, UHID_STOP);
105 static int uhid_hid_open(struct hid_device *hid)
107 struct uhid_device *uhid = hid->driver_data;
109 return uhid_queue_event(uhid, UHID_OPEN);
112 static void uhid_hid_close(struct hid_device *hid)
114 struct uhid_device *uhid = hid->driver_data;
116 uhid_queue_event(uhid, UHID_CLOSE);
119 static int uhid_hid_parse(struct hid_device *hid)
121 struct uhid_device *uhid = hid->driver_data;
123 return hid_parse_report(hid, uhid->rd_data, uhid->rd_size);
126 static int uhid_hid_output_raw(struct hid_device *hid, __u8 *buf, size_t count,
127 unsigned char report_type)
129 struct uhid_device *uhid = hid->driver_data;
132 struct uhid_event *ev;
134 switch (report_type) {
135 case HID_FEATURE_REPORT:
136 rtype = UHID_FEATURE_REPORT;
138 case HID_OUTPUT_REPORT:
139 rtype = UHID_OUTPUT_REPORT;
145 if (count < 1 || count > UHID_DATA_MAX)
148 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
152 ev->type = UHID_OUTPUT;
153 ev->u.output.size = count;
154 ev->u.output.rtype = rtype;
155 memcpy(ev->u.output.data, buf, count);
157 spin_lock_irqsave(&uhid->qlock, flags);
158 uhid_queue(uhid, ev);
159 spin_unlock_irqrestore(&uhid->qlock, flags);
164 static int uhid_hid_output_report(struct hid_device *hid, __u8 *buf,
167 return uhid_hid_output_raw(hid, buf, count, HID_OUTPUT_REPORT);
170 static struct hid_ll_driver uhid_hid_driver = {
171 .start = uhid_hid_start,
172 .stop = uhid_hid_stop,
173 .open = uhid_hid_open,
174 .close = uhid_hid_close,
175 .parse = uhid_hid_parse,
176 .output_report = uhid_hid_output_report,
181 /* Apparently we haven't stepped on these rakes enough times yet. */
182 struct uhid_create_req_compat {
187 compat_uptr_t rd_data;
195 } __attribute__((__packed__));
197 static int uhid_event_from_user(const char __user *buffer, size_t len,
198 struct uhid_event *event)
200 if (is_compat_task()) {
203 if (get_user(type, buffer))
206 if (type == UHID_CREATE) {
208 * This is our messed up request with compat pointer.
209 * It is largish (more than 256 bytes) so we better
210 * allocate it from the heap.
212 struct uhid_create_req_compat *compat;
214 compat = kzalloc(sizeof(*compat), GFP_KERNEL);
218 buffer += sizeof(type);
220 if (copy_from_user(compat, buffer,
221 min(len, sizeof(*compat)))) {
226 /* Shuffle the data over to proper structure */
229 memcpy(event->u.create.name, compat->name,
230 sizeof(compat->name));
231 memcpy(event->u.create.phys, compat->phys,
232 sizeof(compat->phys));
233 memcpy(event->u.create.uniq, compat->uniq,
234 sizeof(compat->uniq));
236 event->u.create.rd_data = compat_ptr(compat->rd_data);
237 event->u.create.rd_size = compat->rd_size;
239 event->u.create.bus = compat->bus;
240 event->u.create.vendor = compat->vendor;
241 event->u.create.product = compat->product;
242 event->u.create.version = compat->version;
243 event->u.create.country = compat->country;
248 /* All others can be copied directly */
251 if (copy_from_user(event, buffer, min(len, sizeof(*event))))
257 static int uhid_event_from_user(const char __user *buffer, size_t len,
258 struct uhid_event *event)
260 if (copy_from_user(event, buffer, min(len, sizeof(*event))))
267 static int uhid_dev_create(struct uhid_device *uhid,
268 const struct uhid_event *ev)
270 struct hid_device *hid;
276 uhid->rd_size = ev->u.create.rd_size;
277 if (uhid->rd_size <= 0 || uhid->rd_size > HID_MAX_DESCRIPTOR_SIZE)
280 uhid->rd_data = kmalloc(uhid->rd_size, GFP_KERNEL);
284 if (copy_from_user(uhid->rd_data, ev->u.create.rd_data,
290 hid = hid_allocate_device();
296 strncpy(hid->name, ev->u.create.name, 127);
298 strncpy(hid->phys, ev->u.create.phys, 63);
300 strncpy(hid->uniq, ev->u.create.uniq, 63);
303 hid->ll_driver = &uhid_hid_driver;
304 hid->hid_output_raw_report = uhid_hid_output_raw;
305 hid->bus = ev->u.create.bus;
306 hid->vendor = ev->u.create.vendor;
307 hid->product = ev->u.create.product;
308 hid->version = ev->u.create.version;
309 hid->country = ev->u.create.country;
310 hid->driver_data = uhid;
311 hid->dev.parent = uhid_misc.this_device;
314 uhid->running = true;
316 ret = hid_add_device(hid);
318 hid_err(hid, "Cannot register HID device\n");
325 hid_destroy_device(hid);
327 uhid->running = false;
329 kfree(uhid->rd_data);
333 static int uhid_dev_destroy(struct uhid_device *uhid)
338 /* clear "running" before setting "report_done" */
339 uhid->running = false;
341 atomic_set(&uhid->report_done, 1);
342 wake_up_interruptible(&uhid->report_wait);
344 hid_destroy_device(uhid->hid);
345 kfree(uhid->rd_data);
350 static int uhid_dev_input(struct uhid_device *uhid, struct uhid_event *ev)
355 hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input.data,
356 min_t(size_t, ev->u.input.size, UHID_DATA_MAX), 0);
361 static int uhid_dev_feature_answer(struct uhid_device *uhid,
362 struct uhid_event *ev)
369 spin_lock_irqsave(&uhid->qlock, flags);
371 /* id for old report; drop it silently */
372 if (atomic_read(&uhid->report_id) != ev->u.feature_answer.id)
374 if (atomic_read(&uhid->report_done))
377 memcpy(&uhid->report_buf, ev, sizeof(*ev));
378 atomic_set(&uhid->report_done, 1);
379 wake_up_interruptible(&uhid->report_wait);
382 spin_unlock_irqrestore(&uhid->qlock, flags);
386 static int uhid_char_open(struct inode *inode, struct file *file)
388 struct uhid_device *uhid;
390 uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
394 mutex_init(&uhid->devlock);
395 mutex_init(&uhid->report_lock);
396 spin_lock_init(&uhid->qlock);
397 init_waitqueue_head(&uhid->waitq);
398 init_waitqueue_head(&uhid->report_wait);
399 uhid->running = false;
400 atomic_set(&uhid->report_done, 1);
402 file->private_data = uhid;
403 nonseekable_open(inode, file);
408 static int uhid_char_release(struct inode *inode, struct file *file)
410 struct uhid_device *uhid = file->private_data;
413 uhid_dev_destroy(uhid);
415 for (i = 0; i < UHID_BUFSIZE; ++i)
416 kfree(uhid->outq[i]);
423 static ssize_t uhid_char_read(struct file *file, char __user *buffer,
424 size_t count, loff_t *ppos)
426 struct uhid_device *uhid = file->private_data;
431 /* they need at least the "type" member of uhid_event */
432 if (count < sizeof(__u32))
436 if (file->f_flags & O_NONBLOCK) {
437 if (uhid->head == uhid->tail)
440 ret = wait_event_interruptible(uhid->waitq,
441 uhid->head != uhid->tail);
446 ret = mutex_lock_interruptible(&uhid->devlock);
450 if (uhid->head == uhid->tail) {
451 mutex_unlock(&uhid->devlock);
454 len = min(count, sizeof(**uhid->outq));
455 if (copy_to_user(buffer, uhid->outq[uhid->tail], len)) {
458 kfree(uhid->outq[uhid->tail]);
459 uhid->outq[uhid->tail] = NULL;
461 spin_lock_irqsave(&uhid->qlock, flags);
462 uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE;
463 spin_unlock_irqrestore(&uhid->qlock, flags);
467 mutex_unlock(&uhid->devlock);
468 return ret ? ret : len;
471 static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
472 size_t count, loff_t *ppos)
474 struct uhid_device *uhid = file->private_data;
478 /* we need at least the "type" member of uhid_event */
479 if (count < sizeof(__u32))
482 ret = mutex_lock_interruptible(&uhid->devlock);
486 memset(&uhid->input_buf, 0, sizeof(uhid->input_buf));
487 len = min(count, sizeof(uhid->input_buf));
489 ret = uhid_event_from_user(buffer, len, &uhid->input_buf);
493 switch (uhid->input_buf.type) {
495 ret = uhid_dev_create(uhid, &uhid->input_buf);
498 ret = uhid_dev_destroy(uhid);
501 ret = uhid_dev_input(uhid, &uhid->input_buf);
503 case UHID_FEATURE_ANSWER:
504 ret = uhid_dev_feature_answer(uhid, &uhid->input_buf);
511 mutex_unlock(&uhid->devlock);
513 /* return "count" not "len" to not confuse the caller */
514 return ret ? ret : count;
517 static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
519 struct uhid_device *uhid = file->private_data;
521 poll_wait(file, &uhid->waitq, wait);
523 if (uhid->head != uhid->tail)
524 return POLLIN | POLLRDNORM;
529 static const struct file_operations uhid_fops = {
530 .owner = THIS_MODULE,
531 .open = uhid_char_open,
532 .release = uhid_char_release,
533 .read = uhid_char_read,
534 .write = uhid_char_write,
535 .poll = uhid_char_poll,
539 static struct miscdevice uhid_misc = {
545 static int __init uhid_init(void)
547 return misc_register(&uhid_misc);
550 static void __exit uhid_exit(void)
552 misc_deregister(&uhid_misc);
555 module_init(uhid_init);
556 module_exit(uhid_exit);
557 MODULE_LICENSE("GPL");
558 MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
559 MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");
560 MODULE_ALIAS_MISCDEV(UHID_MINOR);
561 MODULE_ALIAS("devname:" UHID_NAME);