GNU Linux-libre 4.9.330-gnu1
[releases.git] / drivers / hid / uhid.c
1 /*
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>
14 #include <linux/compat.h>
15 #include <linux/cred.h>
16 #include <linux/device.h>
17 #include <linux/fs.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>
29 #include <linux/eventpoll.h>
30
31 #define UHID_NAME       "uhid"
32 #define UHID_BUFSIZE    32
33
34 struct uhid_device {
35         struct mutex devlock;
36
37         /* This flag tracks whether the HID device is usable for commands from
38          * userspace. The flag is already set before hid_add_device(), which
39          * runs in workqueue context, to allow hid_add_device() to communicate
40          * with userspace.
41          * However, if hid_add_device() fails, the flag is cleared without
42          * holding devlock.
43          * We guarantee that if @running changes from true to false while you're
44          * holding @devlock, it's still fine to access @hid.
45          */
46         bool running;
47
48         __u8 *rd_data;
49         uint rd_size;
50
51         /* When this is NULL, userspace may use UHID_CREATE/UHID_CREATE2. */
52         struct hid_device *hid;
53         struct uhid_event input_buf;
54
55         wait_queue_head_t waitq;
56         spinlock_t qlock;
57         __u8 head;
58         __u8 tail;
59         struct uhid_event *outq[UHID_BUFSIZE];
60
61         /* blocking GET_REPORT support; state changes protected by qlock */
62         struct mutex report_lock;
63         wait_queue_head_t report_wait;
64         bool report_running;
65         u32 report_id;
66         u32 report_type;
67         struct uhid_event report_buf;
68         struct work_struct worker;
69 };
70
71 static struct miscdevice uhid_misc;
72
73 static void uhid_device_add_worker(struct work_struct *work)
74 {
75         struct uhid_device *uhid = container_of(work, struct uhid_device, worker);
76         int ret;
77
78         ret = hid_add_device(uhid->hid);
79         if (ret) {
80                 hid_err(uhid->hid, "Cannot register HID device: error %d\n", ret);
81
82                 /* We used to call hid_destroy_device() here, but that's really
83                  * messy to get right because we have to coordinate with
84                  * concurrent writes from userspace that might be in the middle
85                  * of using uhid->hid.
86                  * Just leave uhid->hid as-is for now, and clean it up when
87                  * userspace tries to close or reinitialize the uhid instance.
88                  *
89                  * However, we do have to clear the ->running flag and do a
90                  * wakeup to make sure userspace knows that the device is gone.
91                  */
92                 uhid->running = false;
93                 wake_up_interruptible(&uhid->report_wait);
94         }
95 }
96
97 static void uhid_queue(struct uhid_device *uhid, struct uhid_event *ev)
98 {
99         __u8 newhead;
100
101         newhead = (uhid->head + 1) % UHID_BUFSIZE;
102
103         if (newhead != uhid->tail) {
104                 uhid->outq[uhid->head] = ev;
105                 uhid->head = newhead;
106                 wake_up_interruptible(&uhid->waitq);
107         } else {
108                 hid_warn(uhid->hid, "Output queue is full\n");
109                 kfree(ev);
110         }
111 }
112
113 static int uhid_queue_event(struct uhid_device *uhid, __u32 event)
114 {
115         unsigned long flags;
116         struct uhid_event *ev;
117
118         ev = kzalloc(sizeof(*ev), GFP_KERNEL);
119         if (!ev)
120                 return -ENOMEM;
121
122         ev->type = event;
123
124         spin_lock_irqsave(&uhid->qlock, flags);
125         uhid_queue(uhid, ev);
126         spin_unlock_irqrestore(&uhid->qlock, flags);
127
128         return 0;
129 }
130
131 static int uhid_hid_start(struct hid_device *hid)
132 {
133         struct uhid_device *uhid = hid->driver_data;
134         struct uhid_event *ev;
135         unsigned long flags;
136
137         ev = kzalloc(sizeof(*ev), GFP_KERNEL);
138         if (!ev)
139                 return -ENOMEM;
140
141         ev->type = UHID_START;
142
143         if (hid->report_enum[HID_FEATURE_REPORT].numbered)
144                 ev->u.start.dev_flags |= UHID_DEV_NUMBERED_FEATURE_REPORTS;
145         if (hid->report_enum[HID_OUTPUT_REPORT].numbered)
146                 ev->u.start.dev_flags |= UHID_DEV_NUMBERED_OUTPUT_REPORTS;
147         if (hid->report_enum[HID_INPUT_REPORT].numbered)
148                 ev->u.start.dev_flags |= UHID_DEV_NUMBERED_INPUT_REPORTS;
149
150         spin_lock_irqsave(&uhid->qlock, flags);
151         uhid_queue(uhid, ev);
152         spin_unlock_irqrestore(&uhid->qlock, flags);
153
154         return 0;
155 }
156
157 static void uhid_hid_stop(struct hid_device *hid)
158 {
159         struct uhid_device *uhid = hid->driver_data;
160
161         hid->claimed = 0;
162         uhid_queue_event(uhid, UHID_STOP);
163 }
164
165 static int uhid_hid_open(struct hid_device *hid)
166 {
167         struct uhid_device *uhid = hid->driver_data;
168
169         return uhid_queue_event(uhid, UHID_OPEN);
170 }
171
172 static void uhid_hid_close(struct hid_device *hid)
173 {
174         struct uhid_device *uhid = hid->driver_data;
175
176         uhid_queue_event(uhid, UHID_CLOSE);
177 }
178
179 static int uhid_hid_parse(struct hid_device *hid)
180 {
181         struct uhid_device *uhid = hid->driver_data;
182
183         return hid_parse_report(hid, uhid->rd_data, uhid->rd_size);
184 }
185
186 /* must be called with report_lock held */
187 static int __uhid_report_queue_and_wait(struct uhid_device *uhid,
188                                         struct uhid_event *ev,
189                                         __u32 *report_id)
190 {
191         unsigned long flags;
192         int ret;
193
194         spin_lock_irqsave(&uhid->qlock, flags);
195         *report_id = ++uhid->report_id;
196         uhid->report_type = ev->type + 1;
197         uhid->report_running = true;
198         uhid_queue(uhid, ev);
199         spin_unlock_irqrestore(&uhid->qlock, flags);
200
201         ret = wait_event_interruptible_timeout(uhid->report_wait,
202                                 !uhid->report_running || !uhid->running,
203                                 5 * HZ);
204         if (!ret || !uhid->running || uhid->report_running)
205                 ret = -EIO;
206         else if (ret < 0)
207                 ret = -ERESTARTSYS;
208         else
209                 ret = 0;
210
211         uhid->report_running = false;
212
213         return ret;
214 }
215
216 static void uhid_report_wake_up(struct uhid_device *uhid, u32 id,
217                                 const struct uhid_event *ev)
218 {
219         unsigned long flags;
220
221         spin_lock_irqsave(&uhid->qlock, flags);
222
223         /* id for old report; drop it silently */
224         if (uhid->report_type != ev->type || uhid->report_id != id)
225                 goto unlock;
226         if (!uhid->report_running)
227                 goto unlock;
228
229         memcpy(&uhid->report_buf, ev, sizeof(*ev));
230         uhid->report_running = false;
231         wake_up_interruptible(&uhid->report_wait);
232
233 unlock:
234         spin_unlock_irqrestore(&uhid->qlock, flags);
235 }
236
237 static int uhid_hid_get_report(struct hid_device *hid, unsigned char rnum,
238                                u8 *buf, size_t count, u8 rtype)
239 {
240         struct uhid_device *uhid = hid->driver_data;
241         struct uhid_get_report_reply_req *req;
242         struct uhid_event *ev;
243         int ret;
244
245         if (!uhid->running)
246                 return -EIO;
247
248         ev = kzalloc(sizeof(*ev), GFP_KERNEL);
249         if (!ev)
250                 return -ENOMEM;
251
252         ev->type = UHID_GET_REPORT;
253         ev->u.get_report.rnum = rnum;
254         ev->u.get_report.rtype = rtype;
255
256         ret = mutex_lock_interruptible(&uhid->report_lock);
257         if (ret) {
258                 kfree(ev);
259                 return ret;
260         }
261
262         /* this _always_ takes ownership of @ev */
263         ret = __uhid_report_queue_and_wait(uhid, ev, &ev->u.get_report.id);
264         if (ret)
265                 goto unlock;
266
267         req = &uhid->report_buf.u.get_report_reply;
268         if (req->err) {
269                 ret = -EIO;
270         } else {
271                 ret = min3(count, (size_t)req->size, (size_t)UHID_DATA_MAX);
272                 memcpy(buf, req->data, ret);
273         }
274
275 unlock:
276         mutex_unlock(&uhid->report_lock);
277         return ret;
278 }
279
280 static int uhid_hid_set_report(struct hid_device *hid, unsigned char rnum,
281                                const u8 *buf, size_t count, u8 rtype)
282 {
283         struct uhid_device *uhid = hid->driver_data;
284         struct uhid_event *ev;
285         int ret;
286
287         if (!uhid->running || count > UHID_DATA_MAX)
288                 return -EIO;
289
290         ev = kzalloc(sizeof(*ev), GFP_KERNEL);
291         if (!ev)
292                 return -ENOMEM;
293
294         ev->type = UHID_SET_REPORT;
295         ev->u.set_report.rnum = rnum;
296         ev->u.set_report.rtype = rtype;
297         ev->u.set_report.size = count;
298         memcpy(ev->u.set_report.data, buf, count);
299
300         ret = mutex_lock_interruptible(&uhid->report_lock);
301         if (ret) {
302                 kfree(ev);
303                 return ret;
304         }
305
306         /* this _always_ takes ownership of @ev */
307         ret = __uhid_report_queue_and_wait(uhid, ev, &ev->u.set_report.id);
308         if (ret)
309                 goto unlock;
310
311         if (uhid->report_buf.u.set_report_reply.err)
312                 ret = -EIO;
313         else
314                 ret = count;
315
316 unlock:
317         mutex_unlock(&uhid->report_lock);
318         return ret;
319 }
320
321 static int uhid_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
322                                 __u8 *buf, size_t len, unsigned char rtype,
323                                 int reqtype)
324 {
325         u8 u_rtype;
326
327         switch (rtype) {
328         case HID_FEATURE_REPORT:
329                 u_rtype = UHID_FEATURE_REPORT;
330                 break;
331         case HID_OUTPUT_REPORT:
332                 u_rtype = UHID_OUTPUT_REPORT;
333                 break;
334         case HID_INPUT_REPORT:
335                 u_rtype = UHID_INPUT_REPORT;
336                 break;
337         default:
338                 return -EINVAL;
339         }
340
341         switch (reqtype) {
342         case HID_REQ_GET_REPORT:
343                 return uhid_hid_get_report(hid, reportnum, buf, len, u_rtype);
344         case HID_REQ_SET_REPORT:
345                 return uhid_hid_set_report(hid, reportnum, buf, len, u_rtype);
346         default:
347                 return -EIO;
348         }
349 }
350
351 static int uhid_hid_output_raw(struct hid_device *hid, __u8 *buf, size_t count,
352                                unsigned char report_type)
353 {
354         struct uhid_device *uhid = hid->driver_data;
355         __u8 rtype;
356         unsigned long flags;
357         struct uhid_event *ev;
358
359         switch (report_type) {
360         case HID_FEATURE_REPORT:
361                 rtype = UHID_FEATURE_REPORT;
362                 break;
363         case HID_OUTPUT_REPORT:
364                 rtype = UHID_OUTPUT_REPORT;
365                 break;
366         default:
367                 return -EINVAL;
368         }
369
370         if (count < 1 || count > UHID_DATA_MAX)
371                 return -EINVAL;
372
373         ev = kzalloc(sizeof(*ev), GFP_KERNEL);
374         if (!ev)
375                 return -ENOMEM;
376
377         ev->type = UHID_OUTPUT;
378         ev->u.output.size = count;
379         ev->u.output.rtype = rtype;
380         memcpy(ev->u.output.data, buf, count);
381
382         spin_lock_irqsave(&uhid->qlock, flags);
383         uhid_queue(uhid, ev);
384         spin_unlock_irqrestore(&uhid->qlock, flags);
385
386         return count;
387 }
388
389 static int uhid_hid_output_report(struct hid_device *hid, __u8 *buf,
390                                   size_t count)
391 {
392         return uhid_hid_output_raw(hid, buf, count, HID_OUTPUT_REPORT);
393 }
394
395 struct hid_ll_driver uhid_hid_driver = {
396         .start = uhid_hid_start,
397         .stop = uhid_hid_stop,
398         .open = uhid_hid_open,
399         .close = uhid_hid_close,
400         .parse = uhid_hid_parse,
401         .raw_request = uhid_hid_raw_request,
402         .output_report = uhid_hid_output_report,
403 };
404 EXPORT_SYMBOL_GPL(uhid_hid_driver);
405
406 #ifdef CONFIG_COMPAT
407
408 /* Apparently we haven't stepped on these rakes enough times yet. */
409 struct uhid_create_req_compat {
410         __u8 name[128];
411         __u8 phys[64];
412         __u8 uniq[64];
413
414         compat_uptr_t rd_data;
415         __u16 rd_size;
416
417         __u16 bus;
418         __u32 vendor;
419         __u32 product;
420         __u32 version;
421         __u32 country;
422 } __attribute__((__packed__));
423
424 static int uhid_event_from_user(const char __user *buffer, size_t len,
425                                 struct uhid_event *event)
426 {
427         if (in_compat_syscall()) {
428                 u32 type;
429
430                 if (get_user(type, buffer))
431                         return -EFAULT;
432
433                 if (type == UHID_CREATE) {
434                         /*
435                          * This is our messed up request with compat pointer.
436                          * It is largish (more than 256 bytes) so we better
437                          * allocate it from the heap.
438                          */
439                         struct uhid_create_req_compat *compat;
440
441                         compat = kzalloc(sizeof(*compat), GFP_KERNEL);
442                         if (!compat)
443                                 return -ENOMEM;
444
445                         buffer += sizeof(type);
446                         len -= sizeof(type);
447                         if (copy_from_user(compat, buffer,
448                                            min(len, sizeof(*compat)))) {
449                                 kfree(compat);
450                                 return -EFAULT;
451                         }
452
453                         /* Shuffle the data over to proper structure */
454                         event->type = type;
455
456                         memcpy(event->u.create.name, compat->name,
457                                 sizeof(compat->name));
458                         memcpy(event->u.create.phys, compat->phys,
459                                 sizeof(compat->phys));
460                         memcpy(event->u.create.uniq, compat->uniq,
461                                 sizeof(compat->uniq));
462
463                         event->u.create.rd_data = compat_ptr(compat->rd_data);
464                         event->u.create.rd_size = compat->rd_size;
465
466                         event->u.create.bus = compat->bus;
467                         event->u.create.vendor = compat->vendor;
468                         event->u.create.product = compat->product;
469                         event->u.create.version = compat->version;
470                         event->u.create.country = compat->country;
471
472                         kfree(compat);
473                         return 0;
474                 }
475                 /* All others can be copied directly */
476         }
477
478         if (copy_from_user(event, buffer, min(len, sizeof(*event))))
479                 return -EFAULT;
480
481         return 0;
482 }
483 #else
484 static int uhid_event_from_user(const char __user *buffer, size_t len,
485                                 struct uhid_event *event)
486 {
487         if (copy_from_user(event, buffer, min(len, sizeof(*event))))
488                 return -EFAULT;
489
490         return 0;
491 }
492 #endif
493
494 static int uhid_dev_create2(struct uhid_device *uhid,
495                             const struct uhid_event *ev)
496 {
497         struct hid_device *hid;
498         size_t rd_size, len;
499         void *rd_data;
500         int ret;
501
502         if (uhid->hid)
503                 return -EALREADY;
504
505         rd_size = ev->u.create2.rd_size;
506         if (rd_size <= 0 || rd_size > HID_MAX_DESCRIPTOR_SIZE)
507                 return -EINVAL;
508
509         rd_data = kmemdup(ev->u.create2.rd_data, rd_size, GFP_KERNEL);
510         if (!rd_data)
511                 return -ENOMEM;
512
513         uhid->rd_size = rd_size;
514         uhid->rd_data = rd_data;
515
516         hid = hid_allocate_device();
517         if (IS_ERR(hid)) {
518                 ret = PTR_ERR(hid);
519                 goto err_free;
520         }
521
522         len = min(sizeof(hid->name), sizeof(ev->u.create2.name)) - 1;
523         strncpy(hid->name, ev->u.create2.name, len);
524         len = min(sizeof(hid->phys), sizeof(ev->u.create2.phys)) - 1;
525         strncpy(hid->phys, ev->u.create2.phys, len);
526         len = min(sizeof(hid->uniq), sizeof(ev->u.create2.uniq)) - 1;
527         strncpy(hid->uniq, ev->u.create2.uniq, len);
528
529         hid->ll_driver = &uhid_hid_driver;
530         hid->bus = ev->u.create2.bus;
531         hid->vendor = ev->u.create2.vendor;
532         hid->product = ev->u.create2.product;
533         hid->version = ev->u.create2.version;
534         hid->country = ev->u.create2.country;
535         hid->driver_data = uhid;
536         hid->dev.parent = uhid_misc.this_device;
537
538         uhid->hid = hid;
539         uhid->running = true;
540
541         /* Adding of a HID device is done through a worker, to allow HID drivers
542          * which use feature requests during .probe to work, without they would
543          * be blocked on devlock, which is held by uhid_char_write.
544          */
545         schedule_work(&uhid->worker);
546
547         return 0;
548
549 err_free:
550         kfree(uhid->rd_data);
551         uhid->rd_data = NULL;
552         uhid->rd_size = 0;
553         return ret;
554 }
555
556 static int uhid_dev_create(struct uhid_device *uhid,
557                            struct uhid_event *ev)
558 {
559         struct uhid_create_req orig;
560
561         orig = ev->u.create;
562
563         if (orig.rd_size <= 0 || orig.rd_size > HID_MAX_DESCRIPTOR_SIZE)
564                 return -EINVAL;
565         if (copy_from_user(&ev->u.create2.rd_data, orig.rd_data, orig.rd_size))
566                 return -EFAULT;
567
568         memcpy(ev->u.create2.name, orig.name, sizeof(orig.name));
569         memcpy(ev->u.create2.phys, orig.phys, sizeof(orig.phys));
570         memcpy(ev->u.create2.uniq, orig.uniq, sizeof(orig.uniq));
571         ev->u.create2.rd_size = orig.rd_size;
572         ev->u.create2.bus = orig.bus;
573         ev->u.create2.vendor = orig.vendor;
574         ev->u.create2.product = orig.product;
575         ev->u.create2.version = orig.version;
576         ev->u.create2.country = orig.country;
577
578         return uhid_dev_create2(uhid, ev);
579 }
580
581 static int uhid_dev_destroy(struct uhid_device *uhid)
582 {
583         if (!uhid->hid)
584                 return -EINVAL;
585
586         uhid->running = false;
587         wake_up_interruptible(&uhid->report_wait);
588
589         cancel_work_sync(&uhid->worker);
590
591         hid_destroy_device(uhid->hid);
592         uhid->hid = NULL;
593         kfree(uhid->rd_data);
594
595         return 0;
596 }
597
598 static int uhid_dev_input(struct uhid_device *uhid, struct uhid_event *ev)
599 {
600         if (!uhid->running)
601                 return -EINVAL;
602
603         hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input.data,
604                          min_t(size_t, ev->u.input.size, UHID_DATA_MAX), 0);
605
606         return 0;
607 }
608
609 static int uhid_dev_input2(struct uhid_device *uhid, struct uhid_event *ev)
610 {
611         if (!uhid->running)
612                 return -EINVAL;
613
614         hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input2.data,
615                          min_t(size_t, ev->u.input2.size, UHID_DATA_MAX), 0);
616
617         return 0;
618 }
619
620 static int uhid_dev_get_report_reply(struct uhid_device *uhid,
621                                      struct uhid_event *ev)
622 {
623         if (!uhid->running)
624                 return -EINVAL;
625
626         uhid_report_wake_up(uhid, ev->u.get_report_reply.id, ev);
627         return 0;
628 }
629
630 static int uhid_dev_set_report_reply(struct uhid_device *uhid,
631                                      struct uhid_event *ev)
632 {
633         if (!uhid->running)
634                 return -EINVAL;
635
636         uhid_report_wake_up(uhid, ev->u.set_report_reply.id, ev);
637         return 0;
638 }
639
640 static int uhid_char_open(struct inode *inode, struct file *file)
641 {
642         struct uhid_device *uhid;
643
644         uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
645         if (!uhid)
646                 return -ENOMEM;
647
648         mutex_init(&uhid->devlock);
649         mutex_init(&uhid->report_lock);
650         spin_lock_init(&uhid->qlock);
651         init_waitqueue_head(&uhid->waitq);
652         init_waitqueue_head(&uhid->report_wait);
653         uhid->running = false;
654         INIT_WORK(&uhid->worker, uhid_device_add_worker);
655
656         file->private_data = uhid;
657         nonseekable_open(inode, file);
658
659         return 0;
660 }
661
662 static int uhid_char_release(struct inode *inode, struct file *file)
663 {
664         struct uhid_device *uhid = file->private_data;
665         unsigned int i;
666
667         uhid_dev_destroy(uhid);
668
669         for (i = 0; i < UHID_BUFSIZE; ++i)
670                 kfree(uhid->outq[i]);
671
672         kfree(uhid);
673
674         return 0;
675 }
676
677 static ssize_t uhid_char_read(struct file *file, char __user *buffer,
678                                 size_t count, loff_t *ppos)
679 {
680         struct uhid_device *uhid = file->private_data;
681         int ret;
682         unsigned long flags;
683         size_t len;
684
685         /* they need at least the "type" member of uhid_event */
686         if (count < sizeof(__u32))
687                 return -EINVAL;
688
689 try_again:
690         if (file->f_flags & O_NONBLOCK) {
691                 if (uhid->head == uhid->tail)
692                         return -EAGAIN;
693         } else {
694                 ret = wait_event_interruptible(uhid->waitq,
695                                                 uhid->head != uhid->tail);
696                 if (ret)
697                         return ret;
698         }
699
700         ret = mutex_lock_interruptible(&uhid->devlock);
701         if (ret)
702                 return ret;
703
704         if (uhid->head == uhid->tail) {
705                 mutex_unlock(&uhid->devlock);
706                 goto try_again;
707         } else {
708                 len = min(count, sizeof(**uhid->outq));
709                 if (copy_to_user(buffer, uhid->outq[uhid->tail], len)) {
710                         ret = -EFAULT;
711                 } else {
712                         kfree(uhid->outq[uhid->tail]);
713                         uhid->outq[uhid->tail] = NULL;
714
715                         spin_lock_irqsave(&uhid->qlock, flags);
716                         uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE;
717                         spin_unlock_irqrestore(&uhid->qlock, flags);
718                 }
719         }
720
721         mutex_unlock(&uhid->devlock);
722         return ret ? ret : len;
723 }
724
725 static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
726                                 size_t count, loff_t *ppos)
727 {
728         struct uhid_device *uhid = file->private_data;
729         int ret;
730         size_t len;
731
732         /* we need at least the "type" member of uhid_event */
733         if (count < sizeof(__u32))
734                 return -EINVAL;
735
736         ret = mutex_lock_interruptible(&uhid->devlock);
737         if (ret)
738                 return ret;
739
740         memset(&uhid->input_buf, 0, sizeof(uhid->input_buf));
741         len = min(count, sizeof(uhid->input_buf));
742
743         ret = uhid_event_from_user(buffer, len, &uhid->input_buf);
744         if (ret)
745                 goto unlock;
746
747         switch (uhid->input_buf.type) {
748         case UHID_CREATE:
749                 /*
750                  * 'struct uhid_create_req' contains a __user pointer which is
751                  * copied from, so it's unsafe to allow this with elevated
752                  * privileges (e.g. from a setuid binary) or via kernel_write().
753                  */
754                 if (file->f_cred != current_cred() || uaccess_kernel()) {
755                         pr_err_once("UHID_CREATE from different security context by process %d (%s), this is not allowed.\n",
756                                     task_tgid_vnr(current), current->comm);
757                         ret = -EACCES;
758                         goto unlock;
759                 }
760                 ret = uhid_dev_create(uhid, &uhid->input_buf);
761                 break;
762         case UHID_CREATE2:
763                 ret = uhid_dev_create2(uhid, &uhid->input_buf);
764                 break;
765         case UHID_DESTROY:
766                 ret = uhid_dev_destroy(uhid);
767                 break;
768         case UHID_INPUT:
769                 ret = uhid_dev_input(uhid, &uhid->input_buf);
770                 break;
771         case UHID_INPUT2:
772                 ret = uhid_dev_input2(uhid, &uhid->input_buf);
773                 break;
774         case UHID_GET_REPORT_REPLY:
775                 ret = uhid_dev_get_report_reply(uhid, &uhid->input_buf);
776                 break;
777         case UHID_SET_REPORT_REPLY:
778                 ret = uhid_dev_set_report_reply(uhid, &uhid->input_buf);
779                 break;
780         default:
781                 ret = -EOPNOTSUPP;
782         }
783
784 unlock:
785         mutex_unlock(&uhid->devlock);
786
787         /* return "count" not "len" to not confuse the caller */
788         return ret ? ret : count;
789 }
790
791 static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
792 {
793         struct uhid_device *uhid = file->private_data;
794         unsigned int mask = POLLOUT | POLLWRNORM; /* uhid is always writable */
795
796         poll_wait(file, &uhid->waitq, wait);
797
798         if (uhid->head != uhid->tail)
799                 mask |= POLLIN | POLLRDNORM;
800
801         return mask;
802 }
803
804 static const struct file_operations uhid_fops = {
805         .owner          = THIS_MODULE,
806         .open           = uhid_char_open,
807         .release        = uhid_char_release,
808         .read           = uhid_char_read,
809         .write          = uhid_char_write,
810         .poll           = uhid_char_poll,
811         .llseek         = no_llseek,
812 };
813
814 static struct miscdevice uhid_misc = {
815         .fops           = &uhid_fops,
816         .minor          = UHID_MINOR,
817         .name           = UHID_NAME,
818 };
819 module_misc_device(uhid_misc);
820
821 MODULE_LICENSE("GPL");
822 MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
823 MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");
824 MODULE_ALIAS_MISCDEV(UHID_MINOR);
825 MODULE_ALIAS("devname:" UHID_NAME);