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/uaccess.h>
30 #define UHID_NAME "uhid"
31 #define UHID_BUFSIZE 32
40 struct hid_device
*hid
;
41 struct uhid_event input_buf
;
43 wait_queue_head_t waitq
;
47 struct uhid_event
*outq
[UHID_BUFSIZE
];
49 /* blocking GET_REPORT support; state changes protected by qlock */
50 struct mutex report_lock
;
51 wait_queue_head_t report_wait
;
55 struct uhid_event report_buf
;
56 struct work_struct worker
;
59 static struct miscdevice uhid_misc
;
61 static void uhid_device_add_worker(struct work_struct
*work
)
63 struct uhid_device
*uhid
= container_of(work
, struct uhid_device
, worker
);
66 ret
= hid_add_device(uhid
->hid
);
68 hid_err(uhid
->hid
, "Cannot register HID device: error %d\n", ret
);
70 hid_destroy_device(uhid
->hid
);
72 uhid
->running
= false;
76 static void uhid_queue(struct uhid_device
*uhid
, struct uhid_event
*ev
)
80 newhead
= (uhid
->head
+ 1) % UHID_BUFSIZE
;
82 if (newhead
!= uhid
->tail
) {
83 uhid
->outq
[uhid
->head
] = ev
;
85 wake_up_interruptible(&uhid
->waitq
);
87 hid_warn(uhid
->hid
, "Output queue is full\n");
92 static int uhid_queue_event(struct uhid_device
*uhid
, __u32 event
)
95 struct uhid_event
*ev
;
97 ev
= kzalloc(sizeof(*ev
), GFP_KERNEL
);
103 spin_lock_irqsave(&uhid
->qlock
, flags
);
104 uhid_queue(uhid
, ev
);
105 spin_unlock_irqrestore(&uhid
->qlock
, flags
);
110 static int uhid_hid_start(struct hid_device
*hid
)
112 struct uhid_device
*uhid
= hid
->driver_data
;
113 struct uhid_event
*ev
;
116 ev
= kzalloc(sizeof(*ev
), GFP_KERNEL
);
120 ev
->type
= UHID_START
;
122 if (hid
->report_enum
[HID_FEATURE_REPORT
].numbered
)
123 ev
->u
.start
.dev_flags
|= UHID_DEV_NUMBERED_FEATURE_REPORTS
;
124 if (hid
->report_enum
[HID_OUTPUT_REPORT
].numbered
)
125 ev
->u
.start
.dev_flags
|= UHID_DEV_NUMBERED_OUTPUT_REPORTS
;
126 if (hid
->report_enum
[HID_INPUT_REPORT
].numbered
)
127 ev
->u
.start
.dev_flags
|= UHID_DEV_NUMBERED_INPUT_REPORTS
;
129 spin_lock_irqsave(&uhid
->qlock
, flags
);
130 uhid_queue(uhid
, ev
);
131 spin_unlock_irqrestore(&uhid
->qlock
, flags
);
136 static void uhid_hid_stop(struct hid_device
*hid
)
138 struct uhid_device
*uhid
= hid
->driver_data
;
141 uhid_queue_event(uhid
, UHID_STOP
);
144 static int uhid_hid_open(struct hid_device
*hid
)
146 struct uhid_device
*uhid
= hid
->driver_data
;
148 return uhid_queue_event(uhid
, UHID_OPEN
);
151 static void uhid_hid_close(struct hid_device
*hid
)
153 struct uhid_device
*uhid
= hid
->driver_data
;
155 uhid_queue_event(uhid
, UHID_CLOSE
);
158 static int uhid_hid_parse(struct hid_device
*hid
)
160 struct uhid_device
*uhid
= hid
->driver_data
;
162 return hid_parse_report(hid
, uhid
->rd_data
, uhid
->rd_size
);
165 /* must be called with report_lock held */
166 static int __uhid_report_queue_and_wait(struct uhid_device
*uhid
,
167 struct uhid_event
*ev
,
173 spin_lock_irqsave(&uhid
->qlock
, flags
);
174 *report_id
= ++uhid
->report_id
;
175 uhid
->report_type
= ev
->type
+ 1;
176 uhid
->report_running
= true;
177 uhid_queue(uhid
, ev
);
178 spin_unlock_irqrestore(&uhid
->qlock
, flags
);
180 ret
= wait_event_interruptible_timeout(uhid
->report_wait
,
181 !uhid
->report_running
|| !uhid
->running
,
183 if (!ret
|| !uhid
->running
|| uhid
->report_running
)
190 uhid
->report_running
= false;
195 static void uhid_report_wake_up(struct uhid_device
*uhid
, u32 id
,
196 const struct uhid_event
*ev
)
200 spin_lock_irqsave(&uhid
->qlock
, flags
);
202 /* id for old report; drop it silently */
203 if (uhid
->report_type
!= ev
->type
|| uhid
->report_id
!= id
)
205 if (!uhid
->report_running
)
208 memcpy(&uhid
->report_buf
, ev
, sizeof(*ev
));
209 uhid
->report_running
= false;
210 wake_up_interruptible(&uhid
->report_wait
);
213 spin_unlock_irqrestore(&uhid
->qlock
, flags
);
216 static int uhid_hid_get_report(struct hid_device
*hid
, unsigned char rnum
,
217 u8
*buf
, size_t count
, u8 rtype
)
219 struct uhid_device
*uhid
= hid
->driver_data
;
220 struct uhid_get_report_reply_req
*req
;
221 struct uhid_event
*ev
;
227 ev
= kzalloc(sizeof(*ev
), GFP_KERNEL
);
231 ev
->type
= UHID_GET_REPORT
;
232 ev
->u
.get_report
.rnum
= rnum
;
233 ev
->u
.get_report
.rtype
= rtype
;
235 ret
= mutex_lock_interruptible(&uhid
->report_lock
);
241 /* this _always_ takes ownership of @ev */
242 ret
= __uhid_report_queue_and_wait(uhid
, ev
, &ev
->u
.get_report
.id
);
246 req
= &uhid
->report_buf
.u
.get_report_reply
;
250 ret
= min3(count
, (size_t)req
->size
, (size_t)UHID_DATA_MAX
);
251 memcpy(buf
, req
->data
, ret
);
255 mutex_unlock(&uhid
->report_lock
);
259 static int uhid_hid_set_report(struct hid_device
*hid
, unsigned char rnum
,
260 const u8
*buf
, size_t count
, u8 rtype
)
262 struct uhid_device
*uhid
= hid
->driver_data
;
263 struct uhid_event
*ev
;
266 if (!uhid
->running
|| count
> UHID_DATA_MAX
)
269 ev
= kzalloc(sizeof(*ev
), GFP_KERNEL
);
273 ev
->type
= UHID_SET_REPORT
;
274 ev
->u
.set_report
.rnum
= rnum
;
275 ev
->u
.set_report
.rtype
= rtype
;
276 ev
->u
.set_report
.size
= count
;
277 memcpy(ev
->u
.set_report
.data
, buf
, count
);
279 ret
= mutex_lock_interruptible(&uhid
->report_lock
);
285 /* this _always_ takes ownership of @ev */
286 ret
= __uhid_report_queue_and_wait(uhid
, ev
, &ev
->u
.set_report
.id
);
290 if (uhid
->report_buf
.u
.set_report_reply
.err
)
296 mutex_unlock(&uhid
->report_lock
);
300 static int uhid_hid_raw_request(struct hid_device
*hid
, unsigned char reportnum
,
301 __u8
*buf
, size_t len
, unsigned char rtype
,
307 case HID_FEATURE_REPORT
:
308 u_rtype
= UHID_FEATURE_REPORT
;
310 case HID_OUTPUT_REPORT
:
311 u_rtype
= UHID_OUTPUT_REPORT
;
313 case HID_INPUT_REPORT
:
314 u_rtype
= UHID_INPUT_REPORT
;
321 case HID_REQ_GET_REPORT
:
322 return uhid_hid_get_report(hid
, reportnum
, buf
, len
, u_rtype
);
323 case HID_REQ_SET_REPORT
:
324 return uhid_hid_set_report(hid
, reportnum
, buf
, len
, u_rtype
);
330 static int uhid_hid_output_raw(struct hid_device
*hid
, __u8
*buf
, size_t count
,
331 unsigned char report_type
)
333 struct uhid_device
*uhid
= hid
->driver_data
;
336 struct uhid_event
*ev
;
338 switch (report_type
) {
339 case HID_FEATURE_REPORT
:
340 rtype
= UHID_FEATURE_REPORT
;
342 case HID_OUTPUT_REPORT
:
343 rtype
= UHID_OUTPUT_REPORT
;
349 if (count
< 1 || count
> UHID_DATA_MAX
)
352 ev
= kzalloc(sizeof(*ev
), GFP_KERNEL
);
356 ev
->type
= UHID_OUTPUT
;
357 ev
->u
.output
.size
= count
;
358 ev
->u
.output
.rtype
= rtype
;
359 memcpy(ev
->u
.output
.data
, buf
, count
);
361 spin_lock_irqsave(&uhid
->qlock
, flags
);
362 uhid_queue(uhid
, ev
);
363 spin_unlock_irqrestore(&uhid
->qlock
, flags
);
368 static int uhid_hid_output_report(struct hid_device
*hid
, __u8
*buf
,
371 return uhid_hid_output_raw(hid
, buf
, count
, HID_OUTPUT_REPORT
);
374 static struct hid_ll_driver uhid_hid_driver
= {
375 .start
= uhid_hid_start
,
376 .stop
= uhid_hid_stop
,
377 .open
= uhid_hid_open
,
378 .close
= uhid_hid_close
,
379 .parse
= uhid_hid_parse
,
380 .raw_request
= uhid_hid_raw_request
,
381 .output_report
= uhid_hid_output_report
,
386 /* Apparently we haven't stepped on these rakes enough times yet. */
387 struct uhid_create_req_compat
{
392 compat_uptr_t rd_data
;
400 } __attribute__((__packed__
));
402 static int uhid_event_from_user(const char __user
*buffer
, size_t len
,
403 struct uhid_event
*event
)
405 if (in_compat_syscall()) {
408 if (get_user(type
, buffer
))
411 if (type
== UHID_CREATE
) {
413 * This is our messed up request with compat pointer.
414 * It is largish (more than 256 bytes) so we better
415 * allocate it from the heap.
417 struct uhid_create_req_compat
*compat
;
419 compat
= kzalloc(sizeof(*compat
), GFP_KERNEL
);
423 buffer
+= sizeof(type
);
425 if (copy_from_user(compat
, buffer
,
426 min(len
, sizeof(*compat
)))) {
431 /* Shuffle the data over to proper structure */
434 memcpy(event
->u
.create
.name
, compat
->name
,
435 sizeof(compat
->name
));
436 memcpy(event
->u
.create
.phys
, compat
->phys
,
437 sizeof(compat
->phys
));
438 memcpy(event
->u
.create
.uniq
, compat
->uniq
,
439 sizeof(compat
->uniq
));
441 event
->u
.create
.rd_data
= compat_ptr(compat
->rd_data
);
442 event
->u
.create
.rd_size
= compat
->rd_size
;
444 event
->u
.create
.bus
= compat
->bus
;
445 event
->u
.create
.vendor
= compat
->vendor
;
446 event
->u
.create
.product
= compat
->product
;
447 event
->u
.create
.version
= compat
->version
;
448 event
->u
.create
.country
= compat
->country
;
453 /* All others can be copied directly */
456 if (copy_from_user(event
, buffer
, min(len
, sizeof(*event
))))
462 static int uhid_event_from_user(const char __user
*buffer
, size_t len
,
463 struct uhid_event
*event
)
465 if (copy_from_user(event
, buffer
, min(len
, sizeof(*event
))))
472 static int uhid_dev_create2(struct uhid_device
*uhid
,
473 const struct uhid_event
*ev
)
475 struct hid_device
*hid
;
483 rd_size
= ev
->u
.create2
.rd_size
;
484 if (rd_size
<= 0 || rd_size
> HID_MAX_DESCRIPTOR_SIZE
)
487 rd_data
= kmemdup(ev
->u
.create2
.rd_data
, rd_size
, GFP_KERNEL
);
491 uhid
->rd_size
= rd_size
;
492 uhid
->rd_data
= rd_data
;
494 hid
= hid_allocate_device();
500 len
= min(sizeof(hid
->name
), sizeof(ev
->u
.create2
.name
)) - 1;
501 strncpy(hid
->name
, ev
->u
.create2
.name
, len
);
502 len
= min(sizeof(hid
->phys
), sizeof(ev
->u
.create2
.phys
)) - 1;
503 strncpy(hid
->phys
, ev
->u
.create2
.phys
, len
);
504 len
= min(sizeof(hid
->uniq
), sizeof(ev
->u
.create2
.uniq
)) - 1;
505 strncpy(hid
->uniq
, ev
->u
.create2
.uniq
, len
);
507 hid
->ll_driver
= &uhid_hid_driver
;
508 hid
->bus
= ev
->u
.create2
.bus
;
509 hid
->vendor
= ev
->u
.create2
.vendor
;
510 hid
->product
= ev
->u
.create2
.product
;
511 hid
->version
= ev
->u
.create2
.version
;
512 hid
->country
= ev
->u
.create2
.country
;
513 hid
->driver_data
= uhid
;
514 hid
->dev
.parent
= uhid_misc
.this_device
;
517 uhid
->running
= true;
519 /* Adding of a HID device is done through a worker, to allow HID drivers
520 * which use feature requests during .probe to work, without they would
521 * be blocked on devlock, which is held by uhid_char_write.
523 schedule_work(&uhid
->worker
);
528 kfree(uhid
->rd_data
);
529 uhid
->rd_data
= NULL
;
534 static int uhid_dev_create(struct uhid_device
*uhid
,
535 struct uhid_event
*ev
)
537 struct uhid_create_req orig
;
541 if (orig
.rd_size
<= 0 || orig
.rd_size
> HID_MAX_DESCRIPTOR_SIZE
)
543 if (copy_from_user(&ev
->u
.create2
.rd_data
, orig
.rd_data
, orig
.rd_size
))
546 memcpy(ev
->u
.create2
.name
, orig
.name
, sizeof(orig
.name
));
547 memcpy(ev
->u
.create2
.phys
, orig
.phys
, sizeof(orig
.phys
));
548 memcpy(ev
->u
.create2
.uniq
, orig
.uniq
, sizeof(orig
.uniq
));
549 ev
->u
.create2
.rd_size
= orig
.rd_size
;
550 ev
->u
.create2
.bus
= orig
.bus
;
551 ev
->u
.create2
.vendor
= orig
.vendor
;
552 ev
->u
.create2
.product
= orig
.product
;
553 ev
->u
.create2
.version
= orig
.version
;
554 ev
->u
.create2
.country
= orig
.country
;
556 return uhid_dev_create2(uhid
, ev
);
559 static int uhid_dev_destroy(struct uhid_device
*uhid
)
564 uhid
->running
= false;
565 wake_up_interruptible(&uhid
->report_wait
);
567 cancel_work_sync(&uhid
->worker
);
569 hid_destroy_device(uhid
->hid
);
570 kfree(uhid
->rd_data
);
575 static int uhid_dev_input(struct uhid_device
*uhid
, struct uhid_event
*ev
)
580 hid_input_report(uhid
->hid
, HID_INPUT_REPORT
, ev
->u
.input
.data
,
581 min_t(size_t, ev
->u
.input
.size
, UHID_DATA_MAX
), 0);
586 static int uhid_dev_input2(struct uhid_device
*uhid
, struct uhid_event
*ev
)
591 hid_input_report(uhid
->hid
, HID_INPUT_REPORT
, ev
->u
.input2
.data
,
592 min_t(size_t, ev
->u
.input2
.size
, UHID_DATA_MAX
), 0);
597 static int uhid_dev_get_report_reply(struct uhid_device
*uhid
,
598 struct uhid_event
*ev
)
603 uhid_report_wake_up(uhid
, ev
->u
.get_report_reply
.id
, ev
);
607 static int uhid_dev_set_report_reply(struct uhid_device
*uhid
,
608 struct uhid_event
*ev
)
613 uhid_report_wake_up(uhid
, ev
->u
.set_report_reply
.id
, ev
);
617 static int uhid_char_open(struct inode
*inode
, struct file
*file
)
619 struct uhid_device
*uhid
;
621 uhid
= kzalloc(sizeof(*uhid
), GFP_KERNEL
);
625 mutex_init(&uhid
->devlock
);
626 mutex_init(&uhid
->report_lock
);
627 spin_lock_init(&uhid
->qlock
);
628 init_waitqueue_head(&uhid
->waitq
);
629 init_waitqueue_head(&uhid
->report_wait
);
630 uhid
->running
= false;
631 INIT_WORK(&uhid
->worker
, uhid_device_add_worker
);
633 file
->private_data
= uhid
;
634 nonseekable_open(inode
, file
);
639 static int uhid_char_release(struct inode
*inode
, struct file
*file
)
641 struct uhid_device
*uhid
= file
->private_data
;
644 uhid_dev_destroy(uhid
);
646 for (i
= 0; i
< UHID_BUFSIZE
; ++i
)
647 kfree(uhid
->outq
[i
]);
654 static ssize_t
uhid_char_read(struct file
*file
, char __user
*buffer
,
655 size_t count
, loff_t
*ppos
)
657 struct uhid_device
*uhid
= file
->private_data
;
662 /* they need at least the "type" member of uhid_event */
663 if (count
< sizeof(__u32
))
667 if (file
->f_flags
& O_NONBLOCK
) {
668 if (uhid
->head
== uhid
->tail
)
671 ret
= wait_event_interruptible(uhid
->waitq
,
672 uhid
->head
!= uhid
->tail
);
677 ret
= mutex_lock_interruptible(&uhid
->devlock
);
681 if (uhid
->head
== uhid
->tail
) {
682 mutex_unlock(&uhid
->devlock
);
685 len
= min(count
, sizeof(**uhid
->outq
));
686 if (copy_to_user(buffer
, uhid
->outq
[uhid
->tail
], len
)) {
689 kfree(uhid
->outq
[uhid
->tail
]);
690 uhid
->outq
[uhid
->tail
] = NULL
;
692 spin_lock_irqsave(&uhid
->qlock
, flags
);
693 uhid
->tail
= (uhid
->tail
+ 1) % UHID_BUFSIZE
;
694 spin_unlock_irqrestore(&uhid
->qlock
, flags
);
698 mutex_unlock(&uhid
->devlock
);
699 return ret
? ret
: len
;
702 static ssize_t
uhid_char_write(struct file
*file
, const char __user
*buffer
,
703 size_t count
, loff_t
*ppos
)
705 struct uhid_device
*uhid
= file
->private_data
;
709 /* we need at least the "type" member of uhid_event */
710 if (count
< sizeof(__u32
))
713 ret
= mutex_lock_interruptible(&uhid
->devlock
);
717 memset(&uhid
->input_buf
, 0, sizeof(uhid
->input_buf
));
718 len
= min(count
, sizeof(uhid
->input_buf
));
720 ret
= uhid_event_from_user(buffer
, len
, &uhid
->input_buf
);
724 switch (uhid
->input_buf
.type
) {
727 * 'struct uhid_create_req' contains a __user pointer which is
728 * copied from, so it's unsafe to allow this with elevated
729 * privileges (e.g. from a setuid binary) or via kernel_write().
731 if (file
->f_cred
!= current_cred() || uaccess_kernel()) {
732 pr_err_once("UHID_CREATE from different security context by process %d (%s), this is not allowed.\n",
733 task_tgid_vnr(current
), current
->comm
);
737 ret
= uhid_dev_create(uhid
, &uhid
->input_buf
);
740 ret
= uhid_dev_create2(uhid
, &uhid
->input_buf
);
743 ret
= uhid_dev_destroy(uhid
);
746 ret
= uhid_dev_input(uhid
, &uhid
->input_buf
);
749 ret
= uhid_dev_input2(uhid
, &uhid
->input_buf
);
751 case UHID_GET_REPORT_REPLY
:
752 ret
= uhid_dev_get_report_reply(uhid
, &uhid
->input_buf
);
754 case UHID_SET_REPORT_REPLY
:
755 ret
= uhid_dev_set_report_reply(uhid
, &uhid
->input_buf
);
762 mutex_unlock(&uhid
->devlock
);
764 /* return "count" not "len" to not confuse the caller */
765 return ret
? ret
: count
;
768 static unsigned int uhid_char_poll(struct file
*file
, poll_table
*wait
)
770 struct uhid_device
*uhid
= file
->private_data
;
772 poll_wait(file
, &uhid
->waitq
, wait
);
774 if (uhid
->head
!= uhid
->tail
)
775 return POLLIN
| POLLRDNORM
;
780 static const struct file_operations uhid_fops
= {
781 .owner
= THIS_MODULE
,
782 .open
= uhid_char_open
,
783 .release
= uhid_char_release
,
784 .read
= uhid_char_read
,
785 .write
= uhid_char_write
,
786 .poll
= uhid_char_poll
,
790 static struct miscdevice uhid_misc
= {
795 module_misc_device(uhid_misc
);
797 MODULE_LICENSE("GPL");
798 MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
799 MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");
800 MODULE_ALIAS_MISCDEV(UHID_MINOR
);
801 MODULE_ALIAS("devname:" UHID_NAME
);