blob: 49fbe97ebb16c49fca1d41cf6e574d981268fd16 [file] [log] [blame]
David Herrmann1ccd7a22012-06-10 15:16:13 +02001/*
2 * User-space I/O driver support for HID subsystem
3 * Copyright (c) 2012 David Herrmann
4 */
5
6/*
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)
10 * any later version.
11 */
12
13#include <linux/atomic.h>
Dmitry Torokhovbefde022013-02-18 11:26:11 +010014#include <linux/compat.h>
David Herrmann1ccd7a22012-06-10 15:16:13 +020015#include <linux/device.h>
16#include <linux/fs.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>
27
28#define UHID_NAME "uhid"
David Herrmannace3d862012-06-10 15:16:14 +020029#define UHID_BUFSIZE 32
30
31struct uhid_device {
David Herrmannd937ae52012-06-10 15:16:16 +020032 struct mutex devlock;
David Herrmannd365c6c2012-06-10 15:16:18 +020033 bool running;
34
35 __u8 *rd_data;
36 uint rd_size;
37
David Herrmannace3d862012-06-10 15:16:14 +020038 struct hid_device *hid;
David Herrmann6664ef72012-06-10 15:16:17 +020039 struct uhid_event input_buf;
David Herrmannace3d862012-06-10 15:16:14 +020040
41 wait_queue_head_t waitq;
42 spinlock_t qlock;
43 __u8 head;
44 __u8 tail;
45 struct uhid_event *outq[UHID_BUFSIZE];
David Herrmannfcfcf0d2012-06-10 15:16:25 +020046
47 struct mutex report_lock;
48 wait_queue_head_t report_wait;
49 atomic_t report_done;
50 atomic_t report_id;
51 struct uhid_event report_buf;
David Herrmannace3d862012-06-10 15:16:14 +020052};
David Herrmann1ccd7a22012-06-10 15:16:13 +020053
54static struct miscdevice uhid_misc;
55
David Herrmannace3d862012-06-10 15:16:14 +020056static void uhid_queue(struct uhid_device *uhid, struct uhid_event *ev)
57{
58 __u8 newhead;
59
60 newhead = (uhid->head + 1) % UHID_BUFSIZE;
61
62 if (newhead != uhid->tail) {
63 uhid->outq[uhid->head] = ev;
64 uhid->head = newhead;
65 wake_up_interruptible(&uhid->waitq);
66 } else {
67 hid_warn(uhid->hid, "Output queue is full\n");
68 kfree(ev);
69 }
70}
71
72static int uhid_queue_event(struct uhid_device *uhid, __u32 event)
73{
74 unsigned long flags;
75 struct uhid_event *ev;
76
77 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
78 if (!ev)
79 return -ENOMEM;
80
81 ev->type = event;
82
83 spin_lock_irqsave(&uhid->qlock, flags);
84 uhid_queue(uhid, ev);
85 spin_unlock_irqrestore(&uhid->qlock, flags);
86
87 return 0;
88}
89
David Herrmannd365c6c2012-06-10 15:16:18 +020090static int uhid_hid_start(struct hid_device *hid)
91{
David Herrmannec4b7de2012-06-10 15:16:21 +020092 struct uhid_device *uhid = hid->driver_data;
93
94 return uhid_queue_event(uhid, UHID_START);
David Herrmannd365c6c2012-06-10 15:16:18 +020095}
96
97static void uhid_hid_stop(struct hid_device *hid)
98{
David Herrmannec4b7de2012-06-10 15:16:21 +020099 struct uhid_device *uhid = hid->driver_data;
100
101 hid->claimed = 0;
102 uhid_queue_event(uhid, UHID_STOP);
David Herrmannd365c6c2012-06-10 15:16:18 +0200103}
104
105static int uhid_hid_open(struct hid_device *hid)
106{
David Herrmanne7191472012-06-10 15:16:22 +0200107 struct uhid_device *uhid = hid->driver_data;
108
109 return uhid_queue_event(uhid, UHID_OPEN);
David Herrmannd365c6c2012-06-10 15:16:18 +0200110}
111
112static void uhid_hid_close(struct hid_device *hid)
113{
David Herrmanne7191472012-06-10 15:16:22 +0200114 struct uhid_device *uhid = hid->driver_data;
115
116 uhid_queue_event(uhid, UHID_CLOSE);
David Herrmannd365c6c2012-06-10 15:16:18 +0200117}
118
David Herrmannd365c6c2012-06-10 15:16:18 +0200119static int uhid_hid_parse(struct hid_device *hid)
120{
David Herrmann037c0612012-06-10 15:16:20 +0200121 struct uhid_device *uhid = hid->driver_data;
122
123 return hid_parse_report(hid, uhid->rd_data, uhid->rd_size);
David Herrmannd365c6c2012-06-10 15:16:18 +0200124}
125
David Herrmannd365c6c2012-06-10 15:16:18 +0200126static int uhid_hid_output_raw(struct hid_device *hid, __u8 *buf, size_t count,
127 unsigned char report_type)
128{
David Herrmann3b3baa82012-06-10 15:16:24 +0200129 struct uhid_device *uhid = hid->driver_data;
130 __u8 rtype;
131 unsigned long flags;
132 struct uhid_event *ev;
133
134 switch (report_type) {
135 case HID_FEATURE_REPORT:
136 rtype = UHID_FEATURE_REPORT;
137 break;
138 case HID_OUTPUT_REPORT:
139 rtype = UHID_OUTPUT_REPORT;
140 break;
141 default:
142 return -EINVAL;
143 }
144
145 if (count < 1 || count > UHID_DATA_MAX)
146 return -EINVAL;
147
148 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
149 if (!ev)
150 return -ENOMEM;
151
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);
156
157 spin_lock_irqsave(&uhid->qlock, flags);
158 uhid_queue(uhid, ev);
159 spin_unlock_irqrestore(&uhid->qlock, flags);
160
161 return count;
David Herrmannd365c6c2012-06-10 15:16:18 +0200162}
163
Frank Praznik596cfdd2014-01-22 13:49:43 -0500164static int uhid_hid_output_report(struct hid_device *hid, __u8 *buf,
165 size_t count)
166{
Benjamin Tissoires41abfb32014-02-10 12:58:46 -0500167 return uhid_hid_output_raw(hid, buf, count, HID_OUTPUT_REPORT);
Frank Praznik596cfdd2014-01-22 13:49:43 -0500168}
169
David Herrmannd365c6c2012-06-10 15:16:18 +0200170static 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,
David Herrmannd365c6c2012-06-10 15:16:18 +0200175 .parse = uhid_hid_parse,
Frank Praznik596cfdd2014-01-22 13:49:43 -0500176 .output_report = uhid_hid_output_report,
David Herrmannd365c6c2012-06-10 15:16:18 +0200177};
178
Dmitry Torokhovbefde022013-02-18 11:26:11 +0100179#ifdef CONFIG_COMPAT
180
181/* Apparently we haven't stepped on these rakes enough times yet. */
182struct uhid_create_req_compat {
183 __u8 name[128];
184 __u8 phys[64];
185 __u8 uniq[64];
186
187 compat_uptr_t rd_data;
188 __u16 rd_size;
189
190 __u16 bus;
191 __u32 vendor;
192 __u32 product;
193 __u32 version;
194 __u32 country;
195} __attribute__((__packed__));
196
197static int uhid_event_from_user(const char __user *buffer, size_t len,
198 struct uhid_event *event)
199{
200 if (is_compat_task()) {
201 u32 type;
202
203 if (get_user(type, buffer))
204 return -EFAULT;
205
206 if (type == UHID_CREATE) {
207 /*
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.
211 */
212 struct uhid_create_req_compat *compat;
213
David Herrmann80897aa2013-11-26 13:58:18 +0100214 compat = kzalloc(sizeof(*compat), GFP_KERNEL);
Dmitry Torokhovbefde022013-02-18 11:26:11 +0100215 if (!compat)
216 return -ENOMEM;
217
218 buffer += sizeof(type);
219 len -= sizeof(type);
220 if (copy_from_user(compat, buffer,
221 min(len, sizeof(*compat)))) {
222 kfree(compat);
223 return -EFAULT;
224 }
225
226 /* Shuffle the data over to proper structure */
227 event->type = type;
228
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));
235
236 event->u.create.rd_data = compat_ptr(compat->rd_data);
237 event->u.create.rd_size = compat->rd_size;
238
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;
244
245 kfree(compat);
246 return 0;
247 }
248 /* All others can be copied directly */
249 }
250
251 if (copy_from_user(event, buffer, min(len, sizeof(*event))))
252 return -EFAULT;
253
254 return 0;
255}
256#else
257static int uhid_event_from_user(const char __user *buffer, size_t len,
258 struct uhid_event *event)
259{
260 if (copy_from_user(event, buffer, min(len, sizeof(*event))))
261 return -EFAULT;
262
263 return 0;
264}
265#endif
266
David Herrmannd365c6c2012-06-10 15:16:18 +0200267static int uhid_dev_create(struct uhid_device *uhid,
268 const struct uhid_event *ev)
269{
270 struct hid_device *hid;
271 int ret;
272
273 if (uhid->running)
274 return -EALREADY;
275
276 uhid->rd_size = ev->u.create.rd_size;
277 if (uhid->rd_size <= 0 || uhid->rd_size > HID_MAX_DESCRIPTOR_SIZE)
278 return -EINVAL;
279
280 uhid->rd_data = kmalloc(uhid->rd_size, GFP_KERNEL);
281 if (!uhid->rd_data)
282 return -ENOMEM;
283
284 if (copy_from_user(uhid->rd_data, ev->u.create.rd_data,
285 uhid->rd_size)) {
286 ret = -EFAULT;
287 goto err_free;
288 }
289
290 hid = hid_allocate_device();
291 if (IS_ERR(hid)) {
292 ret = PTR_ERR(hid);
293 goto err_free;
294 }
295
296 strncpy(hid->name, ev->u.create.name, 127);
297 hid->name[127] = 0;
298 strncpy(hid->phys, ev->u.create.phys, 63);
299 hid->phys[63] = 0;
300 strncpy(hid->uniq, ev->u.create.uniq, 63);
301 hid->uniq[63] = 0;
302
303 hid->ll_driver = &uhid_hid_driver;
David Herrmannd365c6c2012-06-10 15:16:18 +0200304 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;
312
313 uhid->hid = hid;
314 uhid->running = true;
315
316 ret = hid_add_device(hid);
317 if (ret) {
318 hid_err(hid, "Cannot register HID device\n");
319 goto err_hid;
320 }
321
322 return 0;
323
324err_hid:
325 hid_destroy_device(hid);
326 uhid->hid = NULL;
327 uhid->running = false;
328err_free:
329 kfree(uhid->rd_data);
330 return ret;
331}
332
333static int uhid_dev_destroy(struct uhid_device *uhid)
334{
335 if (!uhid->running)
336 return -EINVAL;
337
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200338 /* clear "running" before setting "report_done" */
David Herrmannd365c6c2012-06-10 15:16:18 +0200339 uhid->running = false;
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200340 smp_wmb();
341 atomic_set(&uhid->report_done, 1);
342 wake_up_interruptible(&uhid->report_wait);
David Herrmannd365c6c2012-06-10 15:16:18 +0200343
344 hid_destroy_device(uhid->hid);
345 kfree(uhid->rd_data);
346
347 return 0;
348}
349
David Herrmann5e87a362012-06-10 15:16:19 +0200350static int uhid_dev_input(struct uhid_device *uhid, struct uhid_event *ev)
351{
352 if (!uhid->running)
353 return -EINVAL;
354
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);
357
358 return 0;
359}
360
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200361static int uhid_dev_feature_answer(struct uhid_device *uhid,
362 struct uhid_event *ev)
363{
364 unsigned long flags;
365
366 if (!uhid->running)
367 return -EINVAL;
368
369 spin_lock_irqsave(&uhid->qlock, flags);
370
371 /* id for old report; drop it silently */
372 if (atomic_read(&uhid->report_id) != ev->u.feature_answer.id)
373 goto unlock;
374 if (atomic_read(&uhid->report_done))
375 goto unlock;
376
377 memcpy(&uhid->report_buf, ev, sizeof(*ev));
378 atomic_set(&uhid->report_done, 1);
379 wake_up_interruptible(&uhid->report_wait);
380
381unlock:
382 spin_unlock_irqrestore(&uhid->qlock, flags);
383 return 0;
384}
385
David Herrmann1ccd7a22012-06-10 15:16:13 +0200386static int uhid_char_open(struct inode *inode, struct file *file)
387{
David Herrmannace3d862012-06-10 15:16:14 +0200388 struct uhid_device *uhid;
389
390 uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
391 if (!uhid)
392 return -ENOMEM;
393
David Herrmannd937ae52012-06-10 15:16:16 +0200394 mutex_init(&uhid->devlock);
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200395 mutex_init(&uhid->report_lock);
David Herrmannace3d862012-06-10 15:16:14 +0200396 spin_lock_init(&uhid->qlock);
397 init_waitqueue_head(&uhid->waitq);
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200398 init_waitqueue_head(&uhid->report_wait);
David Herrmannd365c6c2012-06-10 15:16:18 +0200399 uhid->running = false;
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200400 atomic_set(&uhid->report_done, 1);
David Herrmannace3d862012-06-10 15:16:14 +0200401
402 file->private_data = uhid;
403 nonseekable_open(inode, file);
404
David Herrmann1ccd7a22012-06-10 15:16:13 +0200405 return 0;
406}
407
408static int uhid_char_release(struct inode *inode, struct file *file)
409{
David Herrmannace3d862012-06-10 15:16:14 +0200410 struct uhid_device *uhid = file->private_data;
411 unsigned int i;
412
David Herrmannd365c6c2012-06-10 15:16:18 +0200413 uhid_dev_destroy(uhid);
414
David Herrmannace3d862012-06-10 15:16:14 +0200415 for (i = 0; i < UHID_BUFSIZE; ++i)
416 kfree(uhid->outq[i]);
417
418 kfree(uhid);
419
David Herrmann1ccd7a22012-06-10 15:16:13 +0200420 return 0;
421}
422
423static ssize_t uhid_char_read(struct file *file, char __user *buffer,
424 size_t count, loff_t *ppos)
425{
David Herrmannd937ae52012-06-10 15:16:16 +0200426 struct uhid_device *uhid = file->private_data;
427 int ret;
428 unsigned long flags;
429 size_t len;
430
431 /* they need at least the "type" member of uhid_event */
432 if (count < sizeof(__u32))
433 return -EINVAL;
434
435try_again:
436 if (file->f_flags & O_NONBLOCK) {
437 if (uhid->head == uhid->tail)
438 return -EAGAIN;
439 } else {
440 ret = wait_event_interruptible(uhid->waitq,
441 uhid->head != uhid->tail);
442 if (ret)
443 return ret;
444 }
445
446 ret = mutex_lock_interruptible(&uhid->devlock);
447 if (ret)
448 return ret;
449
450 if (uhid->head == uhid->tail) {
451 mutex_unlock(&uhid->devlock);
452 goto try_again;
453 } else {
454 len = min(count, sizeof(**uhid->outq));
Vinicius Costa Gomesadefb692012-07-14 18:59:25 -0300455 if (copy_to_user(buffer, uhid->outq[uhid->tail], len)) {
David Herrmannd937ae52012-06-10 15:16:16 +0200456 ret = -EFAULT;
457 } else {
458 kfree(uhid->outq[uhid->tail]);
459 uhid->outq[uhid->tail] = NULL;
460
461 spin_lock_irqsave(&uhid->qlock, flags);
462 uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE;
463 spin_unlock_irqrestore(&uhid->qlock, flags);
464 }
465 }
466
467 mutex_unlock(&uhid->devlock);
468 return ret ? ret : len;
David Herrmann1ccd7a22012-06-10 15:16:13 +0200469}
470
471static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
472 size_t count, loff_t *ppos)
473{
David Herrmann6664ef72012-06-10 15:16:17 +0200474 struct uhid_device *uhid = file->private_data;
475 int ret;
476 size_t len;
477
478 /* we need at least the "type" member of uhid_event */
479 if (count < sizeof(__u32))
480 return -EINVAL;
481
482 ret = mutex_lock_interruptible(&uhid->devlock);
483 if (ret)
484 return ret;
485
486 memset(&uhid->input_buf, 0, sizeof(uhid->input_buf));
487 len = min(count, sizeof(uhid->input_buf));
Dmitry Torokhovbefde022013-02-18 11:26:11 +0100488
489 ret = uhid_event_from_user(buffer, len, &uhid->input_buf);
490 if (ret)
David Herrmann6664ef72012-06-10 15:16:17 +0200491 goto unlock;
David Herrmann6664ef72012-06-10 15:16:17 +0200492
493 switch (uhid->input_buf.type) {
David Herrmannd365c6c2012-06-10 15:16:18 +0200494 case UHID_CREATE:
495 ret = uhid_dev_create(uhid, &uhid->input_buf);
496 break;
497 case UHID_DESTROY:
498 ret = uhid_dev_destroy(uhid);
499 break;
David Herrmann5e87a362012-06-10 15:16:19 +0200500 case UHID_INPUT:
501 ret = uhid_dev_input(uhid, &uhid->input_buf);
502 break;
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200503 case UHID_FEATURE_ANSWER:
504 ret = uhid_dev_feature_answer(uhid, &uhid->input_buf);
505 break;
David Herrmann6664ef72012-06-10 15:16:17 +0200506 default:
507 ret = -EOPNOTSUPP;
508 }
509
510unlock:
511 mutex_unlock(&uhid->devlock);
512
513 /* return "count" not "len" to not confuse the caller */
514 return ret ? ret : count;
David Herrmann1ccd7a22012-06-10 15:16:13 +0200515}
516
517static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
518{
David Herrmann1f9dec12012-06-10 15:16:15 +0200519 struct uhid_device *uhid = file->private_data;
520
521 poll_wait(file, &uhid->waitq, wait);
522
523 if (uhid->head != uhid->tail)
524 return POLLIN | POLLRDNORM;
525
David Herrmann1ccd7a22012-06-10 15:16:13 +0200526 return 0;
527}
528
529static 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,
536 .llseek = no_llseek,
537};
538
539static struct miscdevice uhid_misc = {
540 .fops = &uhid_fops,
David Herrmann19872d22013-09-09 18:33:54 +0200541 .minor = UHID_MINOR,
David Herrmann1ccd7a22012-06-10 15:16:13 +0200542 .name = UHID_NAME,
543};
544
545static int __init uhid_init(void)
546{
547 return misc_register(&uhid_misc);
548}
549
550static void __exit uhid_exit(void)
551{
552 misc_deregister(&uhid_misc);
553}
554
555module_init(uhid_init);
556module_exit(uhid_exit);
557MODULE_LICENSE("GPL");
558MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
559MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");
David Herrmann19872d22013-09-09 18:33:54 +0200560MODULE_ALIAS_MISCDEV(UHID_MINOR);
Marcel Holtmann60cbd532013-09-01 11:02:46 -0700561MODULE_ALIAS("devname:" UHID_NAME);