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/cred.h>
16 #include <linux/device.h>
18 #include <linux/hid.h>
19 #include <linux/input.h>
20 #include <linux/miscdevice.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/poll.h>
24 #include <linux/sched.h>
25 #include <linux/spinlock.h>
26 #include <linux/uhid.h>
27 #include <linux/wait.h>
28 #include <linux/eventpoll.h>
30 #define UHID_NAME "uhid"
31 #define UHID_BUFSIZE 32
36 /* This flag tracks whether the HID device is usable for commands from
37 * userspace. The flag is already set before hid_add_device(), which
38 * runs in workqueue context, to allow hid_add_device() to communicate
40 * However, if hid_add_device() fails, the flag is cleared without
42 * We guarantee that if @running changes from true to false while you're
43 * holding @devlock, it's still fine to access @hid.
50 /* When this is NULL, userspace may use UHID_CREATE/UHID_CREATE2. */
51 struct hid_device *hid;
52 struct uhid_event input_buf;
54 wait_queue_head_t waitq;
58 struct uhid_event *outq[UHID_BUFSIZE];
60 /* blocking GET_REPORT support; state changes protected by qlock */
61 struct mutex report_lock;
62 wait_queue_head_t report_wait;
66 struct uhid_event report_buf;
67 struct work_struct worker;
70 static struct miscdevice uhid_misc;
72 static void uhid_device_add_worker(struct work_struct *work)
74 struct uhid_device *uhid = container_of(work, struct uhid_device, worker);
77 ret = hid_add_device(uhid->hid);
79 hid_err(uhid->hid, "Cannot register HID device: error %d\n", ret);
81 /* We used to call hid_destroy_device() here, but that's really
82 * messy to get right because we have to coordinate with
83 * concurrent writes from userspace that might be in the middle
85 * Just leave uhid->hid as-is for now, and clean it up when
86 * userspace tries to close or reinitialize the uhid instance.
88 * However, we do have to clear the ->running flag and do a
89 * wakeup to make sure userspace knows that the device is gone.
91 uhid->running = false;
92 wake_up_interruptible(&uhid->report_wait);
96 static void uhid_queue(struct uhid_device *uhid, struct uhid_event *ev)
100 newhead = (uhid->head + 1) % UHID_BUFSIZE;
102 if (newhead != uhid->tail) {
103 uhid->outq[uhid->head] = ev;
104 uhid->head = newhead;
105 wake_up_interruptible(&uhid->waitq);
107 hid_warn(uhid->hid, "Output queue is full\n");
112 static int uhid_queue_event(struct uhid_device *uhid, __u32 event)
115 struct uhid_event *ev;
117 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
123 spin_lock_irqsave(&uhid->qlock, flags);
124 uhid_queue(uhid, ev);
125 spin_unlock_irqrestore(&uhid->qlock, flags);
130 static int uhid_hid_start(struct hid_device *hid)
132 struct uhid_device *uhid = hid->driver_data;
133 struct uhid_event *ev;
136 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
140 ev->type = UHID_START;
142 if (hid->report_enum[HID_FEATURE_REPORT].numbered)
143 ev->u.start.dev_flags |= UHID_DEV_NUMBERED_FEATURE_REPORTS;
144 if (hid->report_enum[HID_OUTPUT_REPORT].numbered)
145 ev->u.start.dev_flags |= UHID_DEV_NUMBERED_OUTPUT_REPORTS;
146 if (hid->report_enum[HID_INPUT_REPORT].numbered)
147 ev->u.start.dev_flags |= UHID_DEV_NUMBERED_INPUT_REPORTS;
149 spin_lock_irqsave(&uhid->qlock, flags);
150 uhid_queue(uhid, ev);
151 spin_unlock_irqrestore(&uhid->qlock, flags);
156 static void uhid_hid_stop(struct hid_device *hid)
158 struct uhid_device *uhid = hid->driver_data;
161 uhid_queue_event(uhid, UHID_STOP);
164 static int uhid_hid_open(struct hid_device *hid)
166 struct uhid_device *uhid = hid->driver_data;
168 return uhid_queue_event(uhid, UHID_OPEN);
171 static void uhid_hid_close(struct hid_device *hid)
173 struct uhid_device *uhid = hid->driver_data;
175 uhid_queue_event(uhid, UHID_CLOSE);
178 static int uhid_hid_parse(struct hid_device *hid)
180 struct uhid_device *uhid = hid->driver_data;
182 return hid_parse_report(hid, uhid->rd_data, uhid->rd_size);
185 /* must be called with report_lock held */
186 static int __uhid_report_queue_and_wait(struct uhid_device *uhid,
187 struct uhid_event *ev,
193 spin_lock_irqsave(&uhid->qlock, flags);
194 *report_id = ++uhid->report_id;
195 uhid->report_type = ev->type + 1;
196 uhid->report_running = true;
197 uhid_queue(uhid, ev);
198 spin_unlock_irqrestore(&uhid->qlock, flags);
200 ret = wait_event_interruptible_timeout(uhid->report_wait,
201 !uhid->report_running || !uhid->running,
203 if (!ret || !uhid->running || uhid->report_running)
210 uhid->report_running = false;
215 static void uhid_report_wake_up(struct uhid_device *uhid, u32 id,
216 const struct uhid_event *ev)
220 spin_lock_irqsave(&uhid->qlock, flags);
222 /* id for old report; drop it silently */
223 if (uhid->report_type != ev->type || uhid->report_id != id)
225 if (!uhid->report_running)
228 memcpy(&uhid->report_buf, ev, sizeof(*ev));
229 uhid->report_running = false;
230 wake_up_interruptible(&uhid->report_wait);
233 spin_unlock_irqrestore(&uhid->qlock, flags);
236 static int uhid_hid_get_report(struct hid_device *hid, unsigned char rnum,
237 u8 *buf, size_t count, u8 rtype)
239 struct uhid_device *uhid = hid->driver_data;
240 struct uhid_get_report_reply_req *req;
241 struct uhid_event *ev;
247 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
251 ev->type = UHID_GET_REPORT;
252 ev->u.get_report.rnum = rnum;
253 ev->u.get_report.rtype = rtype;
255 ret = mutex_lock_interruptible(&uhid->report_lock);
261 /* this _always_ takes ownership of @ev */
262 ret = __uhid_report_queue_and_wait(uhid, ev, &ev->u.get_report.id);
266 req = &uhid->report_buf.u.get_report_reply;
270 ret = min3(count, (size_t)req->size, (size_t)UHID_DATA_MAX);
271 memcpy(buf, req->data, ret);
275 mutex_unlock(&uhid->report_lock);
279 static int uhid_hid_set_report(struct hid_device *hid, unsigned char rnum,
280 const u8 *buf, size_t count, u8 rtype)
282 struct uhid_device *uhid = hid->driver_data;
283 struct uhid_event *ev;
286 if (!uhid->running || count > UHID_DATA_MAX)
289 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
293 ev->type = UHID_SET_REPORT;
294 ev->u.set_report.rnum = rnum;
295 ev->u.set_report.rtype = rtype;
296 ev->u.set_report.size = count;
297 memcpy(ev->u.set_report.data, buf, count);
299 ret = mutex_lock_interruptible(&uhid->report_lock);
305 /* this _always_ takes ownership of @ev */
306 ret = __uhid_report_queue_and_wait(uhid, ev, &ev->u.set_report.id);
310 if (uhid->report_buf.u.set_report_reply.err)
316 mutex_unlock(&uhid->report_lock);
320 static int uhid_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
321 __u8 *buf, size_t len, unsigned char rtype,
327 case HID_FEATURE_REPORT:
328 u_rtype = UHID_FEATURE_REPORT;
330 case HID_OUTPUT_REPORT:
331 u_rtype = UHID_OUTPUT_REPORT;
333 case HID_INPUT_REPORT:
334 u_rtype = UHID_INPUT_REPORT;
341 case HID_REQ_GET_REPORT:
342 return uhid_hid_get_report(hid, reportnum, buf, len, u_rtype);
343 case HID_REQ_SET_REPORT:
344 return uhid_hid_set_report(hid, reportnum, buf, len, u_rtype);
350 static int uhid_hid_output_raw(struct hid_device *hid, __u8 *buf, size_t count,
351 unsigned char report_type)
353 struct uhid_device *uhid = hid->driver_data;
356 struct uhid_event *ev;
358 switch (report_type) {
359 case HID_FEATURE_REPORT:
360 rtype = UHID_FEATURE_REPORT;
362 case HID_OUTPUT_REPORT:
363 rtype = UHID_OUTPUT_REPORT;
369 if (count < 1 || count > UHID_DATA_MAX)
372 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
376 ev->type = UHID_OUTPUT;
377 ev->u.output.size = count;
378 ev->u.output.rtype = rtype;
379 memcpy(ev->u.output.data, buf, count);
381 spin_lock_irqsave(&uhid->qlock, flags);
382 uhid_queue(uhid, ev);
383 spin_unlock_irqrestore(&uhid->qlock, flags);
388 static int uhid_hid_output_report(struct hid_device *hid, __u8 *buf,
391 return uhid_hid_output_raw(hid, buf, count, HID_OUTPUT_REPORT);
394 struct hid_ll_driver uhid_hid_driver = {
395 .start = uhid_hid_start,
396 .stop = uhid_hid_stop,
397 .open = uhid_hid_open,
398 .close = uhid_hid_close,
399 .parse = uhid_hid_parse,
400 .raw_request = uhid_hid_raw_request,
401 .output_report = uhid_hid_output_report,
403 EXPORT_SYMBOL_GPL(uhid_hid_driver);
407 /* Apparently we haven't stepped on these rakes enough times yet. */
408 struct uhid_create_req_compat {
413 compat_uptr_t rd_data;
421 } __attribute__((__packed__));
423 static int uhid_event_from_user(const char __user *buffer, size_t len,
424 struct uhid_event *event)
426 if (in_compat_syscall()) {
429 if (get_user(type, buffer))
432 if (type == UHID_CREATE) {
434 * This is our messed up request with compat pointer.
435 * It is largish (more than 256 bytes) so we better
436 * allocate it from the heap.
438 struct uhid_create_req_compat *compat;
440 compat = kzalloc(sizeof(*compat), GFP_KERNEL);
444 buffer += sizeof(type);
446 if (copy_from_user(compat, buffer,
447 min(len, sizeof(*compat)))) {
452 /* Shuffle the data over to proper structure */
455 memcpy(event->u.create.name, compat->name,
456 sizeof(compat->name));
457 memcpy(event->u.create.phys, compat->phys,
458 sizeof(compat->phys));
459 memcpy(event->u.create.uniq, compat->uniq,
460 sizeof(compat->uniq));
462 event->u.create.rd_data = compat_ptr(compat->rd_data);
463 event->u.create.rd_size = compat->rd_size;
465 event->u.create.bus = compat->bus;
466 event->u.create.vendor = compat->vendor;
467 event->u.create.product = compat->product;
468 event->u.create.version = compat->version;
469 event->u.create.country = compat->country;
474 /* All others can be copied directly */
477 if (copy_from_user(event, buffer, min(len, sizeof(*event))))
483 static int uhid_event_from_user(const char __user *buffer, size_t len,
484 struct uhid_event *event)
486 if (copy_from_user(event, buffer, min(len, sizeof(*event))))
493 static int uhid_dev_create2(struct uhid_device *uhid,
494 const struct uhid_event *ev)
496 struct hid_device *hid;
504 rd_size = ev->u.create2.rd_size;
505 if (rd_size <= 0 || rd_size > HID_MAX_DESCRIPTOR_SIZE)
508 rd_data = kmemdup(ev->u.create2.rd_data, rd_size, GFP_KERNEL);
512 uhid->rd_size = rd_size;
513 uhid->rd_data = rd_data;
515 hid = hid_allocate_device();
521 len = min(sizeof(hid->name), sizeof(ev->u.create2.name)) - 1;
522 strncpy(hid->name, ev->u.create2.name, len);
523 len = min(sizeof(hid->phys), sizeof(ev->u.create2.phys)) - 1;
524 strncpy(hid->phys, ev->u.create2.phys, len);
525 len = min(sizeof(hid->uniq), sizeof(ev->u.create2.uniq)) - 1;
526 strncpy(hid->uniq, ev->u.create2.uniq, len);
528 hid->ll_driver = &uhid_hid_driver;
529 hid->bus = ev->u.create2.bus;
530 hid->vendor = ev->u.create2.vendor;
531 hid->product = ev->u.create2.product;
532 hid->version = ev->u.create2.version;
533 hid->country = ev->u.create2.country;
534 hid->driver_data = uhid;
535 hid->dev.parent = uhid_misc.this_device;
538 uhid->running = true;
540 /* Adding of a HID device is done through a worker, to allow HID drivers
541 * which use feature requests during .probe to work, without they would
542 * be blocked on devlock, which is held by uhid_char_write.
544 schedule_work(&uhid->worker);
549 kfree(uhid->rd_data);
550 uhid->rd_data = NULL;
555 static int uhid_dev_create(struct uhid_device *uhid,
556 struct uhid_event *ev)
558 struct uhid_create_req orig;
562 if (orig.rd_size <= 0 || orig.rd_size > HID_MAX_DESCRIPTOR_SIZE)
564 if (copy_from_user(&ev->u.create2.rd_data, orig.rd_data, orig.rd_size))
567 memcpy(ev->u.create2.name, orig.name, sizeof(orig.name));
568 memcpy(ev->u.create2.phys, orig.phys, sizeof(orig.phys));
569 memcpy(ev->u.create2.uniq, orig.uniq, sizeof(orig.uniq));
570 ev->u.create2.rd_size = orig.rd_size;
571 ev->u.create2.bus = orig.bus;
572 ev->u.create2.vendor = orig.vendor;
573 ev->u.create2.product = orig.product;
574 ev->u.create2.version = orig.version;
575 ev->u.create2.country = orig.country;
577 return uhid_dev_create2(uhid, ev);
580 static int uhid_dev_destroy(struct uhid_device *uhid)
585 uhid->running = false;
586 wake_up_interruptible(&uhid->report_wait);
588 cancel_work_sync(&uhid->worker);
590 hid_destroy_device(uhid->hid);
592 kfree(uhid->rd_data);
597 static int uhid_dev_input(struct uhid_device *uhid, struct uhid_event *ev)
602 hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input.data,
603 min_t(size_t, ev->u.input.size, UHID_DATA_MAX), 0);
608 static int uhid_dev_input2(struct uhid_device *uhid, struct uhid_event *ev)
613 hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input2.data,
614 min_t(size_t, ev->u.input2.size, UHID_DATA_MAX), 0);
619 static int uhid_dev_get_report_reply(struct uhid_device *uhid,
620 struct uhid_event *ev)
625 uhid_report_wake_up(uhid, ev->u.get_report_reply.id, ev);
629 static int uhid_dev_set_report_reply(struct uhid_device *uhid,
630 struct uhid_event *ev)
635 uhid_report_wake_up(uhid, ev->u.set_report_reply.id, ev);
639 static int uhid_char_open(struct inode *inode, struct file *file)
641 struct uhid_device *uhid;
643 uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
647 mutex_init(&uhid->devlock);
648 mutex_init(&uhid->report_lock);
649 spin_lock_init(&uhid->qlock);
650 init_waitqueue_head(&uhid->waitq);
651 init_waitqueue_head(&uhid->report_wait);
652 uhid->running = false;
653 INIT_WORK(&uhid->worker, uhid_device_add_worker);
655 file->private_data = uhid;
656 nonseekable_open(inode, file);
661 static int uhid_char_release(struct inode *inode, struct file *file)
663 struct uhid_device *uhid = file->private_data;
666 uhid_dev_destroy(uhid);
668 for (i = 0; i < UHID_BUFSIZE; ++i)
669 kfree(uhid->outq[i]);
676 static ssize_t uhid_char_read(struct file *file, char __user *buffer,
677 size_t count, loff_t *ppos)
679 struct uhid_device *uhid = file->private_data;
684 /* they need at least the "type" member of uhid_event */
685 if (count < sizeof(__u32))
689 if (file->f_flags & O_NONBLOCK) {
690 if (uhid->head == uhid->tail)
693 ret = wait_event_interruptible(uhid->waitq,
694 uhid->head != uhid->tail);
699 ret = mutex_lock_interruptible(&uhid->devlock);
703 if (uhid->head == uhid->tail) {
704 mutex_unlock(&uhid->devlock);
707 len = min(count, sizeof(**uhid->outq));
708 if (copy_to_user(buffer, uhid->outq[uhid->tail], len)) {
711 kfree(uhid->outq[uhid->tail]);
712 uhid->outq[uhid->tail] = NULL;
714 spin_lock_irqsave(&uhid->qlock, flags);
715 uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE;
716 spin_unlock_irqrestore(&uhid->qlock, flags);
720 mutex_unlock(&uhid->devlock);
721 return ret ? ret : len;
724 static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
725 size_t count, loff_t *ppos)
727 struct uhid_device *uhid = file->private_data;
731 /* we need at least the "type" member of uhid_event */
732 if (count < sizeof(__u32))
735 ret = mutex_lock_interruptible(&uhid->devlock);
739 memset(&uhid->input_buf, 0, sizeof(uhid->input_buf));
740 len = min(count, sizeof(uhid->input_buf));
742 ret = uhid_event_from_user(buffer, len, &uhid->input_buf);
746 switch (uhid->input_buf.type) {
749 * 'struct uhid_create_req' contains a __user pointer which is
750 * copied from, so it's unsafe to allow this with elevated
751 * privileges (e.g. from a setuid binary) or via kernel_write().
753 if (file->f_cred != current_cred() || uaccess_kernel()) {
754 pr_err_once("UHID_CREATE from different security context by process %d (%s), this is not allowed.\n",
755 task_tgid_vnr(current), current->comm);
759 ret = uhid_dev_create(uhid, &uhid->input_buf);
762 ret = uhid_dev_create2(uhid, &uhid->input_buf);
765 ret = uhid_dev_destroy(uhid);
768 ret = uhid_dev_input(uhid, &uhid->input_buf);
771 ret = uhid_dev_input2(uhid, &uhid->input_buf);
773 case UHID_GET_REPORT_REPLY:
774 ret = uhid_dev_get_report_reply(uhid, &uhid->input_buf);
776 case UHID_SET_REPORT_REPLY:
777 ret = uhid_dev_set_report_reply(uhid, &uhid->input_buf);
784 mutex_unlock(&uhid->devlock);
786 /* return "count" not "len" to not confuse the caller */
787 return ret ? ret : count;
790 static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
792 struct uhid_device *uhid = file->private_data;
793 unsigned int mask = POLLOUT | POLLWRNORM; /* uhid is always writable */
795 poll_wait(file, &uhid->waitq, wait);
797 if (uhid->head != uhid->tail)
798 mask |= POLLIN | POLLRDNORM;
803 static const struct file_operations uhid_fops = {
804 .owner = THIS_MODULE,
805 .open = uhid_char_open,
806 .release = uhid_char_release,
807 .read = uhid_char_read,
808 .write = uhid_char_write,
809 .poll = uhid_char_poll,
813 static struct miscdevice uhid_misc = {
818 module_misc_device(uhid_misc);
820 MODULE_LICENSE("GPL");
821 MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
822 MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");
823 MODULE_ALIAS_MISCDEV(UHID_MINOR);
824 MODULE_ALIAS("devname:" UHID_NAME);