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