]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/hid/uhid.c
HID: sony: Fix memory corruption issue on cleanup.
[mirror_ubuntu-bionic-kernel.git] / drivers / hid / uhid.c
CommitLineData
1ccd7a2a
DH
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>
befde022 14#include <linux/compat.h>
5e12f7f7 15#include <linux/cred.h>
1ccd7a2a
DH
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"
ace3d861
DH
30#define UHID_BUFSIZE 32
31
32struct uhid_device {
d937ae5f 33 struct mutex devlock;
d365c6cf
DH
34 bool running;
35
36 __u8 *rd_data;
37 uint rd_size;
38
ace3d861 39 struct hid_device *hid;
6664ef72 40 struct uhid_event input_buf;
ace3d861
DH
41
42 wait_queue_head_t waitq;
43 spinlock_t qlock;
44 __u8 head;
45 __u8 tail;
46 struct uhid_event *outq[UHID_BUFSIZE];
fcfcf0de 47
8cad5b01 48 /* blocking GET_REPORT support; state changes protected by qlock */
fcfcf0de
DH
49 struct mutex report_lock;
50 wait_queue_head_t report_wait;
5942b849 51 bool report_running;
8cad5b01 52 u32 report_id;
11c22155 53 u32 report_type;
fcfcf0de 54 struct uhid_event report_buf;
67f8ecc5 55 struct work_struct worker;
ace3d861 56};
1ccd7a2a
DH
57
58static struct miscdevice uhid_misc;
59
67f8ecc5
RC
60static void uhid_device_add_worker(struct work_struct *work)
61{
62 struct uhid_device *uhid = container_of(work, struct uhid_device, worker);
63 int ret;
64
65 ret = hid_add_device(uhid->hid);
66 if (ret) {
67 hid_err(uhid->hid, "Cannot register HID device: error %d\n", ret);
68
69 hid_destroy_device(uhid->hid);
70 uhid->hid = NULL;
71 uhid->running = false;
72 }
73}
74
ace3d861
DH
75static void uhid_queue(struct uhid_device *uhid, struct uhid_event *ev)
76{
77 __u8 newhead;
78
79 newhead = (uhid->head + 1) % UHID_BUFSIZE;
80
81 if (newhead != uhid->tail) {
82 uhid->outq[uhid->head] = ev;
83 uhid->head = newhead;
84 wake_up_interruptible(&uhid->waitq);
85 } else {
86 hid_warn(uhid->hid, "Output queue is full\n");
87 kfree(ev);
88 }
89}
90
91static int uhid_queue_event(struct uhid_device *uhid, __u32 event)
92{
93 unsigned long flags;
94 struct uhid_event *ev;
95
96 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
97 if (!ev)
98 return -ENOMEM;
99
100 ev->type = event;
101
102 spin_lock_irqsave(&uhid->qlock, flags);
103 uhid_queue(uhid, ev);
104 spin_unlock_irqrestore(&uhid->qlock, flags);
105
106 return 0;
107}
108
d365c6cf
DH
109static int uhid_hid_start(struct hid_device *hid)
110{
ec4b7dea 111 struct uhid_device *uhid = hid->driver_data;
c2b2f16c
DH
112 struct uhid_event *ev;
113 unsigned long flags;
114
115 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
116 if (!ev)
117 return -ENOMEM;
118
119 ev->type = UHID_START;
ec4b7dea 120
c2b2f16c
DH
121 if (hid->report_enum[HID_FEATURE_REPORT].numbered)
122 ev->u.start.dev_flags |= UHID_DEV_NUMBERED_FEATURE_REPORTS;
123 if (hid->report_enum[HID_OUTPUT_REPORT].numbered)
124 ev->u.start.dev_flags |= UHID_DEV_NUMBERED_OUTPUT_REPORTS;
125 if (hid->report_enum[HID_INPUT_REPORT].numbered)
126 ev->u.start.dev_flags |= UHID_DEV_NUMBERED_INPUT_REPORTS;
127
128 spin_lock_irqsave(&uhid->qlock, flags);
129 uhid_queue(uhid, ev);
130 spin_unlock_irqrestore(&uhid->qlock, flags);
131
132 return 0;
d365c6cf
DH
133}
134
135static void uhid_hid_stop(struct hid_device *hid)
136{
ec4b7dea
DH
137 struct uhid_device *uhid = hid->driver_data;
138
139 hid->claimed = 0;
140 uhid_queue_event(uhid, UHID_STOP);
d365c6cf
DH
141}
142
143static int uhid_hid_open(struct hid_device *hid)
144{
e7191474
DH
145 struct uhid_device *uhid = hid->driver_data;
146
147 return uhid_queue_event(uhid, UHID_OPEN);
d365c6cf
DH
148}
149
150static void uhid_hid_close(struct hid_device *hid)
151{
e7191474
DH
152 struct uhid_device *uhid = hid->driver_data;
153
154 uhid_queue_event(uhid, UHID_CLOSE);
d365c6cf
DH
155}
156
d365c6cf
DH
157static int uhid_hid_parse(struct hid_device *hid)
158{
037c061b
DH
159 struct uhid_device *uhid = hid->driver_data;
160
161 return hid_parse_report(hid, uhid->rd_data, uhid->rd_size);
d365c6cf
DH
162}
163
11c22155
DH
164/* must be called with report_lock held */
165static int __uhid_report_queue_and_wait(struct uhid_device *uhid,
166 struct uhid_event *ev,
167 __u32 *report_id)
168{
169 unsigned long flags;
170 int ret;
171
172 spin_lock_irqsave(&uhid->qlock, flags);
173 *report_id = ++uhid->report_id;
8493ecca 174 uhid->report_type = ev->type + 1;
11c22155
DH
175 uhid->report_running = true;
176 uhid_queue(uhid, ev);
177 spin_unlock_irqrestore(&uhid->qlock, flags);
178
179 ret = wait_event_interruptible_timeout(uhid->report_wait,
180 !uhid->report_running || !uhid->running,
181 5 * HZ);
182 if (!ret || !uhid->running || uhid->report_running)
183 ret = -EIO;
184 else if (ret < 0)
185 ret = -ERESTARTSYS;
186 else
187 ret = 0;
188
189 uhid->report_running = false;
190
191 return ret;
192}
193
194static void uhid_report_wake_up(struct uhid_device *uhid, u32 id,
195 const struct uhid_event *ev)
196{
197 unsigned long flags;
198
199 spin_lock_irqsave(&uhid->qlock, flags);
200
201 /* id for old report; drop it silently */
202 if (uhid->report_type != ev->type || uhid->report_id != id)
203 goto unlock;
204 if (!uhid->report_running)
205 goto unlock;
206
207 memcpy(&uhid->report_buf, ev, sizeof(*ev));
208 uhid->report_running = false;
209 wake_up_interruptible(&uhid->report_wait);
210
211unlock:
212 spin_unlock_irqrestore(&uhid->qlock, flags);
213}
214
fa71f32b 215static int uhid_hid_get_report(struct hid_device *hid, unsigned char rnum,
11c22155 216 u8 *buf, size_t count, u8 rtype)
289a7162
JK
217{
218 struct uhid_device *uhid = hid->driver_data;
11c22155 219 struct uhid_get_report_reply_req *req;
289a7162 220 struct uhid_event *ev;
289a7162 221 int ret;
289a7162
JK
222
223 if (!uhid->running)
224 return -EIO;
225
11c22155
DH
226 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
227 if (!ev)
228 return -ENOMEM;
229
230 ev->type = UHID_GET_REPORT;
231 ev->u.get_report.rnum = rnum;
232 ev->u.get_report.rtype = rtype;
289a7162
JK
233
234 ret = mutex_lock_interruptible(&uhid->report_lock);
11c22155
DH
235 if (ret) {
236 kfree(ev);
289a7162 237 return ret;
11c22155 238 }
289a7162 239
11c22155
DH
240 /* this _always_ takes ownership of @ev */
241 ret = __uhid_report_queue_and_wait(uhid, ev, &ev->u.get_report.id);
242 if (ret)
289a7162 243 goto unlock;
11c22155
DH
244
245 req = &uhid->report_buf.u.get_report_reply;
246 if (req->err) {
247 ret = -EIO;
248 } else {
249 ret = min3(count, (size_t)req->size, (size_t)UHID_DATA_MAX);
250 memcpy(buf, req->data, ret);
289a7162
JK
251 }
252
11c22155
DH
253unlock:
254 mutex_unlock(&uhid->report_lock);
255 return ret;
256}
289a7162 257
11c22155
DH
258static int uhid_hid_set_report(struct hid_device *hid, unsigned char rnum,
259 const u8 *buf, size_t count, u8 rtype)
260{
261 struct uhid_device *uhid = hid->driver_data;
262 struct uhid_event *ev;
263 int ret;
289a7162 264
11c22155
DH
265 if (!uhid->running || count > UHID_DATA_MAX)
266 return -EIO;
289a7162 267
11c22155
DH
268 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
269 if (!ev)
270 return -ENOMEM;
289a7162 271
11c22155
DH
272 ev->type = UHID_SET_REPORT;
273 ev->u.set_report.rnum = rnum;
274 ev->u.set_report.rtype = rtype;
275 ev->u.set_report.size = count;
276 memcpy(ev->u.set_report.data, buf, count);
289a7162 277
11c22155
DH
278 ret = mutex_lock_interruptible(&uhid->report_lock);
279 if (ret) {
280 kfree(ev);
281 return ret;
289a7162
JK
282 }
283
11c22155
DH
284 /* this _always_ takes ownership of @ev */
285 ret = __uhid_report_queue_and_wait(uhid, ev, &ev->u.set_report.id);
286 if (ret)
287 goto unlock;
288
289 if (uhid->report_buf.u.set_report_reply.err)
290 ret = -EIO;
291 else
292 ret = count;
289a7162
JK
293
294unlock:
295 mutex_unlock(&uhid->report_lock);
11c22155 296 return ret;
289a7162
JK
297}
298
7c4003bc
DH
299static int uhid_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
300 __u8 *buf, size_t len, unsigned char rtype,
301 int reqtype)
302{
11c22155
DH
303 u8 u_rtype;
304
305 switch (rtype) {
306 case HID_FEATURE_REPORT:
307 u_rtype = UHID_FEATURE_REPORT;
308 break;
309 case HID_OUTPUT_REPORT:
310 u_rtype = UHID_OUTPUT_REPORT;
311 break;
312 case HID_INPUT_REPORT:
313 u_rtype = UHID_INPUT_REPORT;
314 break;
315 default:
316 return -EINVAL;
317 }
318
7c4003bc
DH
319 switch (reqtype) {
320 case HID_REQ_GET_REPORT:
11c22155 321 return uhid_hid_get_report(hid, reportnum, buf, len, u_rtype);
7c4003bc 322 case HID_REQ_SET_REPORT:
11c22155 323 return uhid_hid_set_report(hid, reportnum, buf, len, u_rtype);
7c4003bc
DH
324 default:
325 return -EIO;
326 }
327}
328
d365c6cf
DH
329static int uhid_hid_output_raw(struct hid_device *hid, __u8 *buf, size_t count,
330 unsigned char report_type)
331{
3b3baa82
DH
332 struct uhid_device *uhid = hid->driver_data;
333 __u8 rtype;
334 unsigned long flags;
335 struct uhid_event *ev;
336
337 switch (report_type) {
338 case HID_FEATURE_REPORT:
339 rtype = UHID_FEATURE_REPORT;
340 break;
341 case HID_OUTPUT_REPORT:
342 rtype = UHID_OUTPUT_REPORT;
343 break;
344 default:
345 return -EINVAL;
346 }
347
348 if (count < 1 || count > UHID_DATA_MAX)
349 return -EINVAL;
350
351 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
352 if (!ev)
353 return -ENOMEM;
354
355 ev->type = UHID_OUTPUT;
356 ev->u.output.size = count;
357 ev->u.output.rtype = rtype;
358 memcpy(ev->u.output.data, buf, count);
359
360 spin_lock_irqsave(&uhid->qlock, flags);
361 uhid_queue(uhid, ev);
362 spin_unlock_irqrestore(&uhid->qlock, flags);
363
364 return count;
d365c6cf
DH
365}
366
596cfdd8
FP
367static int uhid_hid_output_report(struct hid_device *hid, __u8 *buf,
368 size_t count)
369{
41abfb36 370 return uhid_hid_output_raw(hid, buf, count, HID_OUTPUT_REPORT);
596cfdd8
FP
371}
372
fc2237a7 373struct hid_ll_driver uhid_hid_driver = {
d365c6cf
DH
374 .start = uhid_hid_start,
375 .stop = uhid_hid_stop,
376 .open = uhid_hid_open,
377 .close = uhid_hid_close,
d365c6cf 378 .parse = uhid_hid_parse,
7c4003bc 379 .raw_request = uhid_hid_raw_request,
596cfdd8 380 .output_report = uhid_hid_output_report,
d365c6cf 381};
fc2237a7 382EXPORT_SYMBOL_GPL(uhid_hid_driver);
d365c6cf 383
befde022
DT
384#ifdef CONFIG_COMPAT
385
386/* Apparently we haven't stepped on these rakes enough times yet. */
387struct uhid_create_req_compat {
388 __u8 name[128];
389 __u8 phys[64];
390 __u8 uniq[64];
391
392 compat_uptr_t rd_data;
393 __u16 rd_size;
394
395 __u16 bus;
396 __u32 vendor;
397 __u32 product;
398 __u32 version;
399 __u32 country;
400} __attribute__((__packed__));
401
402static int uhid_event_from_user(const char __user *buffer, size_t len,
403 struct uhid_event *event)
404{
7365abba 405 if (in_compat_syscall()) {
befde022
DT
406 u32 type;
407
408 if (get_user(type, buffer))
409 return -EFAULT;
410
411 if (type == UHID_CREATE) {
412 /*
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.
416 */
417 struct uhid_create_req_compat *compat;
418
80897aa7 419 compat = kzalloc(sizeof(*compat), GFP_KERNEL);
befde022
DT
420 if (!compat)
421 return -ENOMEM;
422
423 buffer += sizeof(type);
424 len -= sizeof(type);
425 if (copy_from_user(compat, buffer,
426 min(len, sizeof(*compat)))) {
427 kfree(compat);
428 return -EFAULT;
429 }
430
431 /* Shuffle the data over to proper structure */
432 event->type = type;
433
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));
440
441 event->u.create.rd_data = compat_ptr(compat->rd_data);
442 event->u.create.rd_size = compat->rd_size;
443
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;
449
450 kfree(compat);
451 return 0;
452 }
453 /* All others can be copied directly */
454 }
455
456 if (copy_from_user(event, buffer, min(len, sizeof(*event))))
457 return -EFAULT;
458
459 return 0;
460}
461#else
462static int uhid_event_from_user(const char __user *buffer, size_t len,
463 struct uhid_event *event)
464{
465 if (copy_from_user(event, buffer, min(len, sizeof(*event))))
466 return -EFAULT;
467
468 return 0;
469}
470#endif
471
4522643a
PG
472static int uhid_dev_create2(struct uhid_device *uhid,
473 const struct uhid_event *ev)
474{
475 struct hid_device *hid;
25be7fe2 476 size_t rd_size, len;
41c4a464 477 void *rd_data;
4522643a
PG
478 int ret;
479
480 if (uhid->running)
481 return -EALREADY;
482
41c4a464
DH
483 rd_size = ev->u.create2.rd_size;
484 if (rd_size <= 0 || rd_size > HID_MAX_DESCRIPTOR_SIZE)
4522643a
PG
485 return -EINVAL;
486
41c4a464
DH
487 rd_data = kmemdup(ev->u.create2.rd_data, rd_size, GFP_KERNEL);
488 if (!rd_data)
4522643a
PG
489 return -ENOMEM;
490
41c4a464
DH
491 uhid->rd_size = rd_size;
492 uhid->rd_data = rd_data;
493
4522643a
PG
494 hid = hid_allocate_device();
495 if (IS_ERR(hid)) {
496 ret = PTR_ERR(hid);
497 goto err_free;
498 }
499
25be7fe2
DH
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);
4522643a
PG
506
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;
515
516 uhid->hid = hid;
517 uhid->running = true;
518
67f8ecc5
RC
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.
522 */
523 schedule_work(&uhid->worker);
4522643a
PG
524
525 return 0;
526
4522643a
PG
527err_free:
528 kfree(uhid->rd_data);
41c4a464
DH
529 uhid->rd_data = NULL;
530 uhid->rd_size = 0;
4522643a
PG
531 return ret;
532}
533
56c47754
DH
534static int uhid_dev_create(struct uhid_device *uhid,
535 struct uhid_event *ev)
536{
537 struct uhid_create_req orig;
538
539 orig = ev->u.create;
540
541 if (orig.rd_size <= 0 || orig.rd_size > HID_MAX_DESCRIPTOR_SIZE)
542 return -EINVAL;
543 if (copy_from_user(&ev->u.create2.rd_data, orig.rd_data, orig.rd_size))
544 return -EFAULT;
545
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;
555
556 return uhid_dev_create2(uhid, ev);
557}
558
d365c6cf
DH
559static int uhid_dev_destroy(struct uhid_device *uhid)
560{
561 if (!uhid->running)
562 return -EINVAL;
563
564 uhid->running = false;
fcfcf0de 565 wake_up_interruptible(&uhid->report_wait);
d365c6cf 566
67f8ecc5
RC
567 cancel_work_sync(&uhid->worker);
568
d365c6cf
DH
569 hid_destroy_device(uhid->hid);
570 kfree(uhid->rd_data);
571
572 return 0;
573}
574
5e87a36a
DH
575static int uhid_dev_input(struct uhid_device *uhid, struct uhid_event *ev)
576{
577 if (!uhid->running)
578 return -EINVAL;
579
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);
582
583 return 0;
584}
585
4522643a
PG
586static int uhid_dev_input2(struct uhid_device *uhid, struct uhid_event *ev)
587{
588 if (!uhid->running)
589 return -EINVAL;
590
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);
593
594 return 0;
595}
596
fa71f32b
DH
597static int uhid_dev_get_report_reply(struct uhid_device *uhid,
598 struct uhid_event *ev)
fcfcf0de 599{
fcfcf0de
DH
600 if (!uhid->running)
601 return -EINVAL;
602
11c22155
DH
603 uhid_report_wake_up(uhid, ev->u.get_report_reply.id, ev);
604 return 0;
605}
fcfcf0de 606
11c22155
DH
607static int uhid_dev_set_report_reply(struct uhid_device *uhid,
608 struct uhid_event *ev)
609{
610 if (!uhid->running)
611 return -EINVAL;
fcfcf0de 612
11c22155 613 uhid_report_wake_up(uhid, ev->u.set_report_reply.id, ev);
fcfcf0de
DH
614 return 0;
615}
616
1ccd7a2a
DH
617static int uhid_char_open(struct inode *inode, struct file *file)
618{
ace3d861
DH
619 struct uhid_device *uhid;
620
621 uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
622 if (!uhid)
623 return -ENOMEM;
624
d937ae5f 625 mutex_init(&uhid->devlock);
fcfcf0de 626 mutex_init(&uhid->report_lock);
ace3d861
DH
627 spin_lock_init(&uhid->qlock);
628 init_waitqueue_head(&uhid->waitq);
fcfcf0de 629 init_waitqueue_head(&uhid->report_wait);
d365c6cf 630 uhid->running = false;
67f8ecc5 631 INIT_WORK(&uhid->worker, uhid_device_add_worker);
ace3d861
DH
632
633 file->private_data = uhid;
634 nonseekable_open(inode, file);
635
1ccd7a2a
DH
636 return 0;
637}
638
639static int uhid_char_release(struct inode *inode, struct file *file)
640{
ace3d861
DH
641 struct uhid_device *uhid = file->private_data;
642 unsigned int i;
643
d365c6cf
DH
644 uhid_dev_destroy(uhid);
645
ace3d861
DH
646 for (i = 0; i < UHID_BUFSIZE; ++i)
647 kfree(uhid->outq[i]);
648
649 kfree(uhid);
650
1ccd7a2a
DH
651 return 0;
652}
653
654static ssize_t uhid_char_read(struct file *file, char __user *buffer,
655 size_t count, loff_t *ppos)
656{
d937ae5f
DH
657 struct uhid_device *uhid = file->private_data;
658 int ret;
659 unsigned long flags;
660 size_t len;
661
662 /* they need at least the "type" member of uhid_event */
663 if (count < sizeof(__u32))
664 return -EINVAL;
665
666try_again:
667 if (file->f_flags & O_NONBLOCK) {
668 if (uhid->head == uhid->tail)
669 return -EAGAIN;
670 } else {
671 ret = wait_event_interruptible(uhid->waitq,
672 uhid->head != uhid->tail);
673 if (ret)
674 return ret;
675 }
676
677 ret = mutex_lock_interruptible(&uhid->devlock);
678 if (ret)
679 return ret;
680
681 if (uhid->head == uhid->tail) {
682 mutex_unlock(&uhid->devlock);
683 goto try_again;
684 } else {
685 len = min(count, sizeof(**uhid->outq));
adefb69b 686 if (copy_to_user(buffer, uhid->outq[uhid->tail], len)) {
d937ae5f
DH
687 ret = -EFAULT;
688 } else {
689 kfree(uhid->outq[uhid->tail]);
690 uhid->outq[uhid->tail] = NULL;
691
692 spin_lock_irqsave(&uhid->qlock, flags);
693 uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE;
694 spin_unlock_irqrestore(&uhid->qlock, flags);
695 }
696 }
697
698 mutex_unlock(&uhid->devlock);
699 return ret ? ret : len;
1ccd7a2a
DH
700}
701
702static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
703 size_t count, loff_t *ppos)
704{
6664ef72
DH
705 struct uhid_device *uhid = file->private_data;
706 int ret;
707 size_t len;
708
709 /* we need at least the "type" member of uhid_event */
710 if (count < sizeof(__u32))
711 return -EINVAL;
712
713 ret = mutex_lock_interruptible(&uhid->devlock);
714 if (ret)
715 return ret;
716
717 memset(&uhid->input_buf, 0, sizeof(uhid->input_buf));
718 len = min(count, sizeof(uhid->input_buf));
befde022
DT
719
720 ret = uhid_event_from_user(buffer, len, &uhid->input_buf);
721 if (ret)
6664ef72 722 goto unlock;
6664ef72
DH
723
724 switch (uhid->input_buf.type) {
d365c6cf 725 case UHID_CREATE:
5e12f7f7
EB
726 /*
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().
730 */
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);
734 ret = -EACCES;
735 goto unlock;
736 }
d365c6cf
DH
737 ret = uhid_dev_create(uhid, &uhid->input_buf);
738 break;
4522643a
PG
739 case UHID_CREATE2:
740 ret = uhid_dev_create2(uhid, &uhid->input_buf);
741 break;
d365c6cf
DH
742 case UHID_DESTROY:
743 ret = uhid_dev_destroy(uhid);
744 break;
5e87a36a
DH
745 case UHID_INPUT:
746 ret = uhid_dev_input(uhid, &uhid->input_buf);
747 break;
4522643a
PG
748 case UHID_INPUT2:
749 ret = uhid_dev_input2(uhid, &uhid->input_buf);
750 break;
fa71f32b
DH
751 case UHID_GET_REPORT_REPLY:
752 ret = uhid_dev_get_report_reply(uhid, &uhid->input_buf);
fcfcf0de 753 break;
11c22155
DH
754 case UHID_SET_REPORT_REPLY:
755 ret = uhid_dev_set_report_reply(uhid, &uhid->input_buf);
756 break;
6664ef72
DH
757 default:
758 ret = -EOPNOTSUPP;
759 }
760
761unlock:
762 mutex_unlock(&uhid->devlock);
763
764 /* return "count" not "len" to not confuse the caller */
765 return ret ? ret : count;
1ccd7a2a
DH
766}
767
768static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
769{
1f9dec1e
DH
770 struct uhid_device *uhid = file->private_data;
771
772 poll_wait(file, &uhid->waitq, wait);
773
774 if (uhid->head != uhid->tail)
775 return POLLIN | POLLRDNORM;
776
1ccd7a2a
DH
777 return 0;
778}
779
780static 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,
787 .llseek = no_llseek,
788};
789
790static struct miscdevice uhid_misc = {
791 .fops = &uhid_fops,
19872d20 792 .minor = UHID_MINOR,
1ccd7a2a
DH
793 .name = UHID_NAME,
794};
ca75d601 795module_misc_device(uhid_misc);
1ccd7a2a 796
1ccd7a2a
DH
797MODULE_LICENSE("GPL");
798MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
799MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");
19872d20 800MODULE_ALIAS_MISCDEV(UHID_MINOR);
60cbd53e 801MODULE_ALIAS("devname:" UHID_NAME);