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/device.h>
16 #include <linux/hid.h>
17 #include <linux/input.h>
18 #include <linux/miscdevice.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/poll.h>
22 #include <linux/sched.h>
23 #include <linux/spinlock.h>
24 #include <linux/uhid.h>
25 #include <linux/wait.h>
27 #define UHID_NAME "uhid"
28 #define UHID_BUFSIZE 32
37 struct hid_device
*hid
;
38 struct uhid_event input_buf
;
40 wait_queue_head_t waitq
;
44 struct uhid_event
*outq
[UHID_BUFSIZE
];
46 struct mutex report_lock
;
47 wait_queue_head_t report_wait
;
50 struct uhid_event report_buf
;
53 static struct miscdevice uhid_misc
;
55 static void uhid_queue(struct uhid_device
*uhid
, struct uhid_event
*ev
)
59 newhead
= (uhid
->head
+ 1) % UHID_BUFSIZE
;
61 if (newhead
!= uhid
->tail
) {
62 uhid
->outq
[uhid
->head
] = ev
;
64 wake_up_interruptible(&uhid
->waitq
);
66 hid_warn(uhid
->hid
, "Output queue is full\n");
71 static int uhid_queue_event(struct uhid_device
*uhid
, __u32 event
)
74 struct uhid_event
*ev
;
76 ev
= kzalloc(sizeof(*ev
), GFP_KERNEL
);
82 spin_lock_irqsave(&uhid
->qlock
, flags
);
84 spin_unlock_irqrestore(&uhid
->qlock
, flags
);
89 static int uhid_hid_start(struct hid_device
*hid
)
91 struct uhid_device
*uhid
= hid
->driver_data
;
93 return uhid_queue_event(uhid
, UHID_START
);
96 static void uhid_hid_stop(struct hid_device
*hid
)
98 struct uhid_device
*uhid
= hid
->driver_data
;
101 uhid_queue_event(uhid
, UHID_STOP
);
104 static int uhid_hid_open(struct hid_device
*hid
)
106 struct uhid_device
*uhid
= hid
->driver_data
;
108 return uhid_queue_event(uhid
, UHID_OPEN
);
111 static void uhid_hid_close(struct hid_device
*hid
)
113 struct uhid_device
*uhid
= hid
->driver_data
;
115 uhid_queue_event(uhid
, UHID_CLOSE
);
118 static int uhid_hid_input(struct input_dev
*input
, unsigned int type
,
119 unsigned int code
, int value
)
121 struct hid_device
*hid
= input_get_drvdata(input
);
122 struct uhid_device
*uhid
= hid
->driver_data
;
124 struct uhid_event
*ev
;
126 ev
= kzalloc(sizeof(*ev
), GFP_ATOMIC
);
130 ev
->type
= UHID_OUTPUT_EV
;
131 ev
->u
.output_ev
.type
= type
;
132 ev
->u
.output_ev
.code
= code
;
133 ev
->u
.output_ev
.value
= value
;
135 spin_lock_irqsave(&uhid
->qlock
, flags
);
136 uhid_queue(uhid
, ev
);
137 spin_unlock_irqrestore(&uhid
->qlock
, flags
);
142 static int uhid_hid_parse(struct hid_device
*hid
)
144 struct uhid_device
*uhid
= hid
->driver_data
;
146 return hid_parse_report(hid
, uhid
->rd_data
, uhid
->rd_size
);
149 static int uhid_hid_get_raw(struct hid_device
*hid
, unsigned char rnum
,
150 __u8
*buf
, size_t count
, unsigned char rtype
)
152 struct uhid_device
*uhid
= hid
->driver_data
;
154 struct uhid_event
*ev
;
157 size_t uninitialized_var(len
);
158 struct uhid_feature_answer_req
*req
;
164 case HID_FEATURE_REPORT
:
165 report_type
= UHID_FEATURE_REPORT
;
167 case HID_OUTPUT_REPORT
:
168 report_type
= UHID_OUTPUT_REPORT
;
170 case HID_INPUT_REPORT
:
171 report_type
= UHID_INPUT_REPORT
;
177 ret
= mutex_lock_interruptible(&uhid
->report_lock
);
181 ev
= kzalloc(sizeof(*ev
), GFP_KERNEL
);
187 spin_lock_irqsave(&uhid
->qlock
, flags
);
188 ev
->type
= UHID_FEATURE
;
189 ev
->u
.feature
.id
= atomic_inc_return(&uhid
->report_id
);
190 ev
->u
.feature
.rnum
= rnum
;
191 ev
->u
.feature
.rtype
= report_type
;
193 atomic_set(&uhid
->report_done
, 0);
194 uhid_queue(uhid
, ev
);
195 spin_unlock_irqrestore(&uhid
->qlock
, flags
);
197 ret
= wait_event_interruptible_timeout(uhid
->report_wait
,
198 atomic_read(&uhid
->report_done
), 5 * HZ
);
201 * Make sure "uhid->running" is cleared on shutdown before
202 * "uhid->report_done" is set.
205 if (!ret
|| !uhid
->running
) {
207 } else if (ret
< 0) {
210 spin_lock_irqsave(&uhid
->qlock
, flags
);
211 req
= &uhid
->report_buf
.u
.feature_answer
;
218 min_t(size_t, req
->size
, UHID_DATA_MAX
));
219 memcpy(buf
, req
->data
, len
);
222 spin_unlock_irqrestore(&uhid
->qlock
, flags
);
225 atomic_set(&uhid
->report_done
, 1);
228 mutex_unlock(&uhid
->report_lock
);
229 return ret
? ret
: len
;
232 static int uhid_hid_output_raw(struct hid_device
*hid
, __u8
*buf
, size_t count
,
233 unsigned char report_type
)
235 struct uhid_device
*uhid
= hid
->driver_data
;
238 struct uhid_event
*ev
;
240 switch (report_type
) {
241 case HID_FEATURE_REPORT
:
242 rtype
= UHID_FEATURE_REPORT
;
244 case HID_OUTPUT_REPORT
:
245 rtype
= UHID_OUTPUT_REPORT
;
251 if (count
< 1 || count
> UHID_DATA_MAX
)
254 ev
= kzalloc(sizeof(*ev
), GFP_KERNEL
);
258 ev
->type
= UHID_OUTPUT
;
259 ev
->u
.output
.size
= count
;
260 ev
->u
.output
.rtype
= rtype
;
261 memcpy(ev
->u
.output
.data
, buf
, count
);
263 spin_lock_irqsave(&uhid
->qlock
, flags
);
264 uhid_queue(uhid
, ev
);
265 spin_unlock_irqrestore(&uhid
->qlock
, flags
);
270 static struct hid_ll_driver uhid_hid_driver
= {
271 .start
= uhid_hid_start
,
272 .stop
= uhid_hid_stop
,
273 .open
= uhid_hid_open
,
274 .close
= uhid_hid_close
,
275 .hidinput_input_event
= uhid_hid_input
,
276 .parse
= uhid_hid_parse
,
279 static int uhid_dev_create(struct uhid_device
*uhid
,
280 const struct uhid_event
*ev
)
282 struct hid_device
*hid
;
288 uhid
->rd_size
= ev
->u
.create
.rd_size
;
289 if (uhid
->rd_size
<= 0 || uhid
->rd_size
> HID_MAX_DESCRIPTOR_SIZE
)
292 uhid
->rd_data
= kmalloc(uhid
->rd_size
, GFP_KERNEL
);
296 if (copy_from_user(uhid
->rd_data
, ev
->u
.create
.rd_data
,
302 hid
= hid_allocate_device();
308 strncpy(hid
->name
, ev
->u
.create
.name
, 127);
310 strncpy(hid
->phys
, ev
->u
.create
.phys
, 63);
312 strncpy(hid
->uniq
, ev
->u
.create
.uniq
, 63);
315 hid
->ll_driver
= &uhid_hid_driver
;
316 hid
->hid_get_raw_report
= uhid_hid_get_raw
;
317 hid
->hid_output_raw_report
= uhid_hid_output_raw
;
318 hid
->bus
= ev
->u
.create
.bus
;
319 hid
->vendor
= ev
->u
.create
.vendor
;
320 hid
->product
= ev
->u
.create
.product
;
321 hid
->version
= ev
->u
.create
.version
;
322 hid
->country
= ev
->u
.create
.country
;
323 hid
->driver_data
= uhid
;
324 hid
->dev
.parent
= uhid_misc
.this_device
;
327 uhid
->running
= true;
329 ret
= hid_add_device(hid
);
331 hid_err(hid
, "Cannot register HID device\n");
338 hid_destroy_device(hid
);
340 uhid
->running
= false;
342 kfree(uhid
->rd_data
);
346 static int uhid_dev_destroy(struct uhid_device
*uhid
)
351 /* clear "running" before setting "report_done" */
352 uhid
->running
= false;
354 atomic_set(&uhid
->report_done
, 1);
355 wake_up_interruptible(&uhid
->report_wait
);
357 hid_destroy_device(uhid
->hid
);
358 kfree(uhid
->rd_data
);
363 static int uhid_dev_input(struct uhid_device
*uhid
, struct uhid_event
*ev
)
368 hid_input_report(uhid
->hid
, HID_INPUT_REPORT
, ev
->u
.input
.data
,
369 min_t(size_t, ev
->u
.input
.size
, UHID_DATA_MAX
), 0);
374 static int uhid_dev_feature_answer(struct uhid_device
*uhid
,
375 struct uhid_event
*ev
)
382 spin_lock_irqsave(&uhid
->qlock
, flags
);
384 /* id for old report; drop it silently */
385 if (atomic_read(&uhid
->report_id
) != ev
->u
.feature_answer
.id
)
387 if (atomic_read(&uhid
->report_done
))
390 memcpy(&uhid
->report_buf
, ev
, sizeof(*ev
));
391 atomic_set(&uhid
->report_done
, 1);
392 wake_up_interruptible(&uhid
->report_wait
);
395 spin_unlock_irqrestore(&uhid
->qlock
, flags
);
399 static int uhid_char_open(struct inode
*inode
, struct file
*file
)
401 struct uhid_device
*uhid
;
403 uhid
= kzalloc(sizeof(*uhid
), GFP_KERNEL
);
407 mutex_init(&uhid
->devlock
);
408 mutex_init(&uhid
->report_lock
);
409 spin_lock_init(&uhid
->qlock
);
410 init_waitqueue_head(&uhid
->waitq
);
411 init_waitqueue_head(&uhid
->report_wait
);
412 uhid
->running
= false;
413 atomic_set(&uhid
->report_done
, 1);
415 file
->private_data
= uhid
;
416 nonseekable_open(inode
, file
);
421 static int uhid_char_release(struct inode
*inode
, struct file
*file
)
423 struct uhid_device
*uhid
= file
->private_data
;
426 uhid_dev_destroy(uhid
);
428 for (i
= 0; i
< UHID_BUFSIZE
; ++i
)
429 kfree(uhid
->outq
[i
]);
436 static ssize_t
uhid_char_read(struct file
*file
, char __user
*buffer
,
437 size_t count
, loff_t
*ppos
)
439 struct uhid_device
*uhid
= file
->private_data
;
444 /* they need at least the "type" member of uhid_event */
445 if (count
< sizeof(__u32
))
449 if (file
->f_flags
& O_NONBLOCK
) {
450 if (uhid
->head
== uhid
->tail
)
453 ret
= wait_event_interruptible(uhid
->waitq
,
454 uhid
->head
!= uhid
->tail
);
459 ret
= mutex_lock_interruptible(&uhid
->devlock
);
463 if (uhid
->head
== uhid
->tail
) {
464 mutex_unlock(&uhid
->devlock
);
467 len
= min(count
, sizeof(**uhid
->outq
));
468 if (copy_to_user(buffer
, uhid
->outq
[uhid
->tail
], len
)) {
471 kfree(uhid
->outq
[uhid
->tail
]);
472 uhid
->outq
[uhid
->tail
] = NULL
;
474 spin_lock_irqsave(&uhid
->qlock
, flags
);
475 uhid
->tail
= (uhid
->tail
+ 1) % UHID_BUFSIZE
;
476 spin_unlock_irqrestore(&uhid
->qlock
, flags
);
480 mutex_unlock(&uhid
->devlock
);
481 return ret
? ret
: len
;
484 static ssize_t
uhid_char_write(struct file
*file
, const char __user
*buffer
,
485 size_t count
, loff_t
*ppos
)
487 struct uhid_device
*uhid
= file
->private_data
;
491 /* we need at least the "type" member of uhid_event */
492 if (count
< sizeof(__u32
))
495 ret
= mutex_lock_interruptible(&uhid
->devlock
);
499 memset(&uhid
->input_buf
, 0, sizeof(uhid
->input_buf
));
500 len
= min(count
, sizeof(uhid
->input_buf
));
501 if (copy_from_user(&uhid
->input_buf
, buffer
, len
)) {
506 switch (uhid
->input_buf
.type
) {
508 ret
= uhid_dev_create(uhid
, &uhid
->input_buf
);
511 ret
= uhid_dev_destroy(uhid
);
514 ret
= uhid_dev_input(uhid
, &uhid
->input_buf
);
516 case UHID_FEATURE_ANSWER
:
517 ret
= uhid_dev_feature_answer(uhid
, &uhid
->input_buf
);
524 mutex_unlock(&uhid
->devlock
);
526 /* return "count" not "len" to not confuse the caller */
527 return ret
? ret
: count
;
530 static unsigned int uhid_char_poll(struct file
*file
, poll_table
*wait
)
532 struct uhid_device
*uhid
= file
->private_data
;
534 poll_wait(file
, &uhid
->waitq
, wait
);
536 if (uhid
->head
!= uhid
->tail
)
537 return POLLIN
| POLLRDNORM
;
542 static const struct file_operations uhid_fops
= {
543 .owner
= THIS_MODULE
,
544 .open
= uhid_char_open
,
545 .release
= uhid_char_release
,
546 .read
= uhid_char_read
,
547 .write
= uhid_char_write
,
548 .poll
= uhid_char_poll
,
552 static struct miscdevice uhid_misc
= {
554 .minor
= MISC_DYNAMIC_MINOR
,
558 static int __init
uhid_init(void)
560 return misc_register(&uhid_misc
);
563 static void __exit
uhid_exit(void)
565 misc_deregister(&uhid_misc
);
568 module_init(uhid_init
);
569 module_exit(uhid_exit
);
570 MODULE_LICENSE("GPL");
571 MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
572 MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");