blob: 7a136847c8b267dee66efd1015c2116516febe65 [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>
Eric Biggersab26f7f2018-11-14 13:55:09 -080015#include <linux/cred.h>
David Herrmann1ccd7a22012-06-10 15:16:13 +020016#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>
Eric Biggersab26f7f2018-11-14 13:55:09 -080028#include <linux/uaccess.h>
Marcel Holtmann4d29a642019-12-04 03:43:55 +010029#include <linux/eventpoll.h>
David Herrmann1ccd7a22012-06-10 15:16:13 +020030
31#define UHID_NAME "uhid"
David Herrmannace3d862012-06-10 15:16:14 +020032#define UHID_BUFSIZE 32
33
Dmitry Torokhov98350982017-05-30 14:46:26 -070034static DEFINE_MUTEX(uhid_open_mutex);
35
David Herrmannace3d862012-06-10 15:16:14 +020036struct uhid_device {
David Herrmannd937ae52012-06-10 15:16:16 +020037 struct mutex devlock;
David Herrmannd365c6c2012-06-10 15:16:18 +020038 bool running;
39
40 __u8 *rd_data;
41 uint rd_size;
42
David Herrmannace3d862012-06-10 15:16:14 +020043 struct hid_device *hid;
David Herrmann6664ef72012-06-10 15:16:17 +020044 struct uhid_event input_buf;
David Herrmannace3d862012-06-10 15:16:14 +020045
46 wait_queue_head_t waitq;
47 spinlock_t qlock;
48 __u8 head;
49 __u8 tail;
50 struct uhid_event *outq[UHID_BUFSIZE];
David Herrmannfcfcf0d2012-06-10 15:16:25 +020051
David Herrmann8cad5b02014-07-29 17:14:19 +020052 /* blocking GET_REPORT support; state changes protected by qlock */
David Herrmannfcfcf0d2012-06-10 15:16:25 +020053 struct mutex report_lock;
54 wait_queue_head_t report_wait;
David Herrmann5942b842014-07-29 17:14:20 +020055 bool report_running;
David Herrmann8cad5b02014-07-29 17:14:19 +020056 u32 report_id;
David Herrmann11c22152014-07-29 17:14:24 +020057 u32 report_type;
David Herrmannfcfcf0d2012-06-10 15:16:25 +020058 struct uhid_event report_buf;
Roderick Colenbrander67f8ecc2016-05-18 13:11:09 -070059 struct work_struct worker;
David Herrmannace3d862012-06-10 15:16:14 +020060};
David Herrmann1ccd7a22012-06-10 15:16:13 +020061
62static struct miscdevice uhid_misc;
63
Roderick Colenbrander67f8ecc2016-05-18 13:11:09 -070064static void uhid_device_add_worker(struct work_struct *work)
65{
66 struct uhid_device *uhid = container_of(work, struct uhid_device, worker);
67 int ret;
68
69 ret = hid_add_device(uhid->hid);
70 if (ret) {
71 hid_err(uhid->hid, "Cannot register HID device: error %d\n", ret);
72
73 hid_destroy_device(uhid->hid);
74 uhid->hid = NULL;
75 uhid->running = false;
76 }
77}
78
David Herrmannace3d862012-06-10 15:16:14 +020079static void uhid_queue(struct uhid_device *uhid, struct uhid_event *ev)
80{
81 __u8 newhead;
82
83 newhead = (uhid->head + 1) % UHID_BUFSIZE;
84
85 if (newhead != uhid->tail) {
86 uhid->outq[uhid->head] = ev;
87 uhid->head = newhead;
88 wake_up_interruptible(&uhid->waitq);
89 } else {
90 hid_warn(uhid->hid, "Output queue is full\n");
91 kfree(ev);
92 }
93}
94
95static int uhid_queue_event(struct uhid_device *uhid, __u32 event)
96{
97 unsigned long flags;
98 struct uhid_event *ev;
99
100 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
101 if (!ev)
102 return -ENOMEM;
103
104 ev->type = event;
105
106 spin_lock_irqsave(&uhid->qlock, flags);
107 uhid_queue(uhid, ev);
108 spin_unlock_irqrestore(&uhid->qlock, flags);
109
110 return 0;
111}
112
David Herrmannd365c6c2012-06-10 15:16:18 +0200113static int uhid_hid_start(struct hid_device *hid)
114{
David Herrmannec4b7de2012-06-10 15:16:21 +0200115 struct uhid_device *uhid = hid->driver_data;
David Herrmannc2b2f162014-07-29 17:14:25 +0200116 struct uhid_event *ev;
117 unsigned long flags;
David Herrmannec4b7de2012-06-10 15:16:21 +0200118
David Herrmannc2b2f162014-07-29 17:14:25 +0200119 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
120 if (!ev)
121 return -ENOMEM;
122
123 ev->type = UHID_START;
124
125 if (hid->report_enum[HID_FEATURE_REPORT].numbered)
126 ev->u.start.dev_flags |= UHID_DEV_NUMBERED_FEATURE_REPORTS;
127 if (hid->report_enum[HID_OUTPUT_REPORT].numbered)
128 ev->u.start.dev_flags |= UHID_DEV_NUMBERED_OUTPUT_REPORTS;
129 if (hid->report_enum[HID_INPUT_REPORT].numbered)
130 ev->u.start.dev_flags |= UHID_DEV_NUMBERED_INPUT_REPORTS;
131
132 spin_lock_irqsave(&uhid->qlock, flags);
133 uhid_queue(uhid, ev);
134 spin_unlock_irqrestore(&uhid->qlock, flags);
135
136 return 0;
David Herrmannd365c6c2012-06-10 15:16:18 +0200137}
138
139static void uhid_hid_stop(struct hid_device *hid)
140{
David Herrmannec4b7de2012-06-10 15:16:21 +0200141 struct uhid_device *uhid = hid->driver_data;
142
143 hid->claimed = 0;
144 uhid_queue_event(uhid, UHID_STOP);
David Herrmannd365c6c2012-06-10 15:16:18 +0200145}
146
147static int uhid_hid_open(struct hid_device *hid)
148{
David Herrmanne7191472012-06-10 15:16:22 +0200149 struct uhid_device *uhid = hid->driver_data;
150
Raghavendra Rao Ananta18a929c2018-11-15 22:08:24 -0800151 return uhid_queue_event(uhid, UHID_OPEN);
David Herrmannd365c6c2012-06-10 15:16:18 +0200152}
153
154static void uhid_hid_close(struct hid_device *hid)
155{
David Herrmanne7191472012-06-10 15:16:22 +0200156 struct uhid_device *uhid = hid->driver_data;
157
Raghavendra Rao Ananta18a929c2018-11-15 22:08:24 -0800158 uhid_queue_event(uhid, UHID_CLOSE);
David Herrmannd365c6c2012-06-10 15:16:18 +0200159}
160
David Herrmannd365c6c2012-06-10 15:16:18 +0200161static int uhid_hid_parse(struct hid_device *hid)
162{
David Herrmann037c0612012-06-10 15:16:20 +0200163 struct uhid_device *uhid = hid->driver_data;
164
165 return hid_parse_report(hid, uhid->rd_data, uhid->rd_size);
David Herrmannd365c6c2012-06-10 15:16:18 +0200166}
167
David Herrmann11c22152014-07-29 17:14:24 +0200168/* must be called with report_lock held */
169static int __uhid_report_queue_and_wait(struct uhid_device *uhid,
170 struct uhid_event *ev,
171 __u32 *report_id)
Jiri Kosina289a7162014-02-17 14:49:34 +0100172{
Jiri Kosina289a7162014-02-17 14:49:34 +0100173 unsigned long flags;
174 int ret;
Jiri Kosina289a7162014-02-17 14:49:34 +0100175
176 spin_lock_irqsave(&uhid->qlock, flags);
David Herrmann11c22152014-07-29 17:14:24 +0200177 *report_id = ++uhid->report_id;
Benjamin Tissoires8493ecc2014-10-01 11:59:47 -0400178 uhid->report_type = ev->type + 1;
David Herrmann5942b842014-07-29 17:14:20 +0200179 uhid->report_running = true;
Jiri Kosina289a7162014-02-17 14:49:34 +0100180 uhid_queue(uhid, ev);
181 spin_unlock_irqrestore(&uhid->qlock, flags);
182
183 ret = wait_event_interruptible_timeout(uhid->report_wait,
David Herrmann5942b842014-07-29 17:14:20 +0200184 !uhid->report_running || !uhid->running,
185 5 * HZ);
David Herrmann11c22152014-07-29 17:14:24 +0200186 if (!ret || !uhid->running || uhid->report_running)
Jiri Kosina289a7162014-02-17 14:49:34 +0100187 ret = -EIO;
David Herrmann11c22152014-07-29 17:14:24 +0200188 else if (ret < 0)
Jiri Kosina289a7162014-02-17 14:49:34 +0100189 ret = -ERESTARTSYS;
David Herrmann11c22152014-07-29 17:14:24 +0200190 else
191 ret = 0;
Jiri Kosina289a7162014-02-17 14:49:34 +0100192
David Herrmann5942b842014-07-29 17:14:20 +0200193 uhid->report_running = false;
Jiri Kosina289a7162014-02-17 14:49:34 +0100194
David Herrmann11c22152014-07-29 17:14:24 +0200195 return ret;
196}
197
198static void uhid_report_wake_up(struct uhid_device *uhid, u32 id,
199 const struct uhid_event *ev)
200{
201 unsigned long flags;
202
203 spin_lock_irqsave(&uhid->qlock, flags);
204
205 /* id for old report; drop it silently */
206 if (uhid->report_type != ev->type || uhid->report_id != id)
207 goto unlock;
208 if (!uhid->report_running)
209 goto unlock;
210
211 memcpy(&uhid->report_buf, ev, sizeof(*ev));
212 uhid->report_running = false;
213 wake_up_interruptible(&uhid->report_wait);
214
215unlock:
216 spin_unlock_irqrestore(&uhid->qlock, flags);
217}
218
219static int uhid_hid_get_report(struct hid_device *hid, unsigned char rnum,
220 u8 *buf, size_t count, u8 rtype)
221{
222 struct uhid_device *uhid = hid->driver_data;
223 struct uhid_get_report_reply_req *req;
224 struct uhid_event *ev;
225 int ret;
226
227 if (!uhid->running)
228 return -EIO;
229
230 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
231 if (!ev)
232 return -ENOMEM;
233
234 ev->type = UHID_GET_REPORT;
235 ev->u.get_report.rnum = rnum;
236 ev->u.get_report.rtype = rtype;
237
238 ret = mutex_lock_interruptible(&uhid->report_lock);
239 if (ret) {
240 kfree(ev);
241 return ret;
242 }
243
244 /* this _always_ takes ownership of @ev */
245 ret = __uhid_report_queue_and_wait(uhid, ev, &ev->u.get_report.id);
246 if (ret)
247 goto unlock;
248
249 req = &uhid->report_buf.u.get_report_reply;
250 if (req->err) {
251 ret = -EIO;
252 } else {
253 ret = min3(count, (size_t)req->size, (size_t)UHID_DATA_MAX);
254 memcpy(buf, req->data, ret);
255 }
256
Jiri Kosina289a7162014-02-17 14:49:34 +0100257unlock:
258 mutex_unlock(&uhid->report_lock);
David Herrmann11c22152014-07-29 17:14:24 +0200259 return ret;
260}
261
262static int uhid_hid_set_report(struct hid_device *hid, unsigned char rnum,
263 const u8 *buf, size_t count, u8 rtype)
264{
265 struct uhid_device *uhid = hid->driver_data;
266 struct uhid_event *ev;
267 int ret;
268
269 if (!uhid->running || count > UHID_DATA_MAX)
270 return -EIO;
271
272 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
273 if (!ev)
274 return -ENOMEM;
275
276 ev->type = UHID_SET_REPORT;
277 ev->u.set_report.rnum = rnum;
278 ev->u.set_report.rtype = rtype;
279 ev->u.set_report.size = count;
280 memcpy(ev->u.set_report.data, buf, count);
281
282 ret = mutex_lock_interruptible(&uhid->report_lock);
283 if (ret) {
284 kfree(ev);
285 return ret;
286 }
287
288 /* this _always_ takes ownership of @ev */
289 ret = __uhid_report_queue_and_wait(uhid, ev, &ev->u.set_report.id);
290 if (ret)
291 goto unlock;
292
293 if (uhid->report_buf.u.set_report_reply.err)
294 ret = -EIO;
295 else
296 ret = count;
297
298unlock:
299 mutex_unlock(&uhid->report_lock);
300 return ret;
Jiri Kosina289a7162014-02-17 14:49:34 +0100301}
302
David Herrmann7c4003b2014-07-29 17:14:23 +0200303static int uhid_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
304 __u8 *buf, size_t len, unsigned char rtype,
305 int reqtype)
306{
David Herrmann11c22152014-07-29 17:14:24 +0200307 u8 u_rtype;
308
309 switch (rtype) {
310 case HID_FEATURE_REPORT:
311 u_rtype = UHID_FEATURE_REPORT;
312 break;
313 case HID_OUTPUT_REPORT:
314 u_rtype = UHID_OUTPUT_REPORT;
315 break;
316 case HID_INPUT_REPORT:
317 u_rtype = UHID_INPUT_REPORT;
318 break;
319 default:
320 return -EINVAL;
321 }
322
David Herrmann7c4003b2014-07-29 17:14:23 +0200323 switch (reqtype) {
324 case HID_REQ_GET_REPORT:
David Herrmann11c22152014-07-29 17:14:24 +0200325 return uhid_hid_get_report(hid, reportnum, buf, len, u_rtype);
David Herrmann7c4003b2014-07-29 17:14:23 +0200326 case HID_REQ_SET_REPORT:
David Herrmann11c22152014-07-29 17:14:24 +0200327 return uhid_hid_set_report(hid, reportnum, buf, len, u_rtype);
David Herrmann7c4003b2014-07-29 17:14:23 +0200328 default:
329 return -EIO;
330 }
331}
332
David Herrmannd365c6c2012-06-10 15:16:18 +0200333static int uhid_hid_output_raw(struct hid_device *hid, __u8 *buf, size_t count,
334 unsigned char report_type)
335{
David Herrmann3b3baa82012-06-10 15:16:24 +0200336 struct uhid_device *uhid = hid->driver_data;
337 __u8 rtype;
338 unsigned long flags;
339 struct uhid_event *ev;
340
341 switch (report_type) {
342 case HID_FEATURE_REPORT:
343 rtype = UHID_FEATURE_REPORT;
344 break;
345 case HID_OUTPUT_REPORT:
346 rtype = UHID_OUTPUT_REPORT;
347 break;
348 default:
349 return -EINVAL;
350 }
351
352 if (count < 1 || count > UHID_DATA_MAX)
353 return -EINVAL;
354
355 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
356 if (!ev)
357 return -ENOMEM;
358
359 ev->type = UHID_OUTPUT;
360 ev->u.output.size = count;
361 ev->u.output.rtype = rtype;
362 memcpy(ev->u.output.data, buf, count);
363
364 spin_lock_irqsave(&uhid->qlock, flags);
365 uhid_queue(uhid, ev);
366 spin_unlock_irqrestore(&uhid->qlock, flags);
367
368 return count;
David Herrmannd365c6c2012-06-10 15:16:18 +0200369}
370
Frank Praznik596cfdd2014-01-22 13:49:43 -0500371static int uhid_hid_output_report(struct hid_device *hid, __u8 *buf,
372 size_t count)
373{
Benjamin Tissoires41abfb32014-02-10 12:58:46 -0500374 return uhid_hid_output_raw(hid, buf, count, HID_OUTPUT_REPORT);
Frank Praznik596cfdd2014-01-22 13:49:43 -0500375}
376
David Herrmannd365c6c2012-06-10 15:16:18 +0200377static struct hid_ll_driver uhid_hid_driver = {
378 .start = uhid_hid_start,
379 .stop = uhid_hid_stop,
380 .open = uhid_hid_open,
381 .close = uhid_hid_close,
David Herrmannd365c6c2012-06-10 15:16:18 +0200382 .parse = uhid_hid_parse,
David Herrmann7c4003b2014-07-29 17:14:23 +0200383 .raw_request = uhid_hid_raw_request,
Frank Praznik596cfdd2014-01-22 13:49:43 -0500384 .output_report = uhid_hid_output_report,
David Herrmannd365c6c2012-06-10 15:16:18 +0200385};
386
Dmitry Torokhovbefde022013-02-18 11:26:11 +0100387#ifdef CONFIG_COMPAT
388
389/* Apparently we haven't stepped on these rakes enough times yet. */
390struct uhid_create_req_compat {
391 __u8 name[128];
392 __u8 phys[64];
393 __u8 uniq[64];
394
395 compat_uptr_t rd_data;
396 __u16 rd_size;
397
398 __u16 bus;
399 __u32 vendor;
400 __u32 product;
401 __u32 version;
402 __u32 country;
403} __attribute__((__packed__));
404
405static int uhid_event_from_user(const char __user *buffer, size_t len,
406 struct uhid_event *event)
407{
Andy Lutomirski7365abb2016-03-22 14:25:24 -0700408 if (in_compat_syscall()) {
Dmitry Torokhovbefde022013-02-18 11:26:11 +0100409 u32 type;
410
411 if (get_user(type, buffer))
412 return -EFAULT;
413
414 if (type == UHID_CREATE) {
415 /*
416 * This is our messed up request with compat pointer.
417 * It is largish (more than 256 bytes) so we better
418 * allocate it from the heap.
419 */
420 struct uhid_create_req_compat *compat;
421
David Herrmann80897aa2013-11-26 13:58:18 +0100422 compat = kzalloc(sizeof(*compat), GFP_KERNEL);
Dmitry Torokhovbefde022013-02-18 11:26:11 +0100423 if (!compat)
424 return -ENOMEM;
425
426 buffer += sizeof(type);
427 len -= sizeof(type);
428 if (copy_from_user(compat, buffer,
429 min(len, sizeof(*compat)))) {
430 kfree(compat);
431 return -EFAULT;
432 }
433
434 /* Shuffle the data over to proper structure */
435 event->type = type;
436
437 memcpy(event->u.create.name, compat->name,
438 sizeof(compat->name));
439 memcpy(event->u.create.phys, compat->phys,
440 sizeof(compat->phys));
441 memcpy(event->u.create.uniq, compat->uniq,
442 sizeof(compat->uniq));
443
444 event->u.create.rd_data = compat_ptr(compat->rd_data);
445 event->u.create.rd_size = compat->rd_size;
446
447 event->u.create.bus = compat->bus;
448 event->u.create.vendor = compat->vendor;
449 event->u.create.product = compat->product;
450 event->u.create.version = compat->version;
451 event->u.create.country = compat->country;
452
453 kfree(compat);
454 return 0;
455 }
456 /* All others can be copied directly */
457 }
458
459 if (copy_from_user(event, buffer, min(len, sizeof(*event))))
460 return -EFAULT;
461
462 return 0;
463}
464#else
465static int uhid_event_from_user(const char __user *buffer, size_t len,
466 struct uhid_event *event)
467{
468 if (copy_from_user(event, buffer, min(len, sizeof(*event))))
469 return -EFAULT;
470
471 return 0;
472}
473#endif
474
Petri Gynther45226432014-03-24 13:50:01 -0700475static int uhid_dev_create2(struct uhid_device *uhid,
476 const struct uhid_event *ev)
477{
478 struct hid_device *hid;
David Herrmann25be7fe2014-07-29 17:14:18 +0200479 size_t rd_size, len;
David Herrmann41c4a462014-07-29 17:14:17 +0200480 void *rd_data;
Petri Gynther45226432014-03-24 13:50:01 -0700481 int ret;
482
483 if (uhid->running)
484 return -EALREADY;
485
David Herrmann41c4a462014-07-29 17:14:17 +0200486 rd_size = ev->u.create2.rd_size;
487 if (rd_size <= 0 || rd_size > HID_MAX_DESCRIPTOR_SIZE)
Petri Gynther45226432014-03-24 13:50:01 -0700488 return -EINVAL;
489
David Herrmann41c4a462014-07-29 17:14:17 +0200490 rd_data = kmemdup(ev->u.create2.rd_data, rd_size, GFP_KERNEL);
491 if (!rd_data)
Petri Gynther45226432014-03-24 13:50:01 -0700492 return -ENOMEM;
493
David Herrmann41c4a462014-07-29 17:14:17 +0200494 uhid->rd_size = rd_size;
495 uhid->rd_data = rd_data;
496
Petri Gynther45226432014-03-24 13:50:01 -0700497 hid = hid_allocate_device();
498 if (IS_ERR(hid)) {
499 ret = PTR_ERR(hid);
500 goto err_free;
501 }
502
David Herrmann25be7fe2014-07-29 17:14:18 +0200503 len = min(sizeof(hid->name), sizeof(ev->u.create2.name)) - 1;
504 strncpy(hid->name, ev->u.create2.name, len);
505 len = min(sizeof(hid->phys), sizeof(ev->u.create2.phys)) - 1;
506 strncpy(hid->phys, ev->u.create2.phys, len);
507 len = min(sizeof(hid->uniq), sizeof(ev->u.create2.uniq)) - 1;
508 strncpy(hid->uniq, ev->u.create2.uniq, len);
Petri Gynther45226432014-03-24 13:50:01 -0700509
510 hid->ll_driver = &uhid_hid_driver;
511 hid->bus = ev->u.create2.bus;
512 hid->vendor = ev->u.create2.vendor;
513 hid->product = ev->u.create2.product;
514 hid->version = ev->u.create2.version;
515 hid->country = ev->u.create2.country;
516 hid->driver_data = uhid;
517 hid->dev.parent = uhid_misc.this_device;
518
519 uhid->hid = hid;
520 uhid->running = true;
521
Roderick Colenbrander67f8ecc2016-05-18 13:11:09 -0700522 /* Adding of a HID device is done through a worker, to allow HID drivers
523 * which use feature requests during .probe to work, without they would
524 * be blocked on devlock, which is held by uhid_char_write.
525 */
526 schedule_work(&uhid->worker);
Petri Gynther45226432014-03-24 13:50:01 -0700527
528 return 0;
529
Petri Gynther45226432014-03-24 13:50:01 -0700530err_free:
531 kfree(uhid->rd_data);
David Herrmann41c4a462014-07-29 17:14:17 +0200532 uhid->rd_data = NULL;
533 uhid->rd_size = 0;
Petri Gynther45226432014-03-24 13:50:01 -0700534 return ret;
535}
536
David Herrmann56c47752014-07-29 17:14:16 +0200537static int uhid_dev_create(struct uhid_device *uhid,
538 struct uhid_event *ev)
539{
540 struct uhid_create_req orig;
541
542 orig = ev->u.create;
543
544 if (orig.rd_size <= 0 || orig.rd_size > HID_MAX_DESCRIPTOR_SIZE)
545 return -EINVAL;
546 if (copy_from_user(&ev->u.create2.rd_data, orig.rd_data, orig.rd_size))
547 return -EFAULT;
548
549 memcpy(ev->u.create2.name, orig.name, sizeof(orig.name));
550 memcpy(ev->u.create2.phys, orig.phys, sizeof(orig.phys));
551 memcpy(ev->u.create2.uniq, orig.uniq, sizeof(orig.uniq));
552 ev->u.create2.rd_size = orig.rd_size;
553 ev->u.create2.bus = orig.bus;
554 ev->u.create2.vendor = orig.vendor;
555 ev->u.create2.product = orig.product;
556 ev->u.create2.version = orig.version;
557 ev->u.create2.country = orig.country;
558
559 return uhid_dev_create2(uhid, ev);
560}
561
David Herrmannd365c6c2012-06-10 15:16:18 +0200562static int uhid_dev_destroy(struct uhid_device *uhid)
563{
564 if (!uhid->running)
565 return -EINVAL;
566
567 uhid->running = false;
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200568 wake_up_interruptible(&uhid->report_wait);
David Herrmannd365c6c2012-06-10 15:16:18 +0200569
Roderick Colenbrander67f8ecc2016-05-18 13:11:09 -0700570 cancel_work_sync(&uhid->worker);
571
David Herrmannd365c6c2012-06-10 15:16:18 +0200572 hid_destroy_device(uhid->hid);
573 kfree(uhid->rd_data);
574
575 return 0;
576}
577
David Herrmann5e87a362012-06-10 15:16:19 +0200578static int uhid_dev_input(struct uhid_device *uhid, struct uhid_event *ev)
579{
580 if (!uhid->running)
581 return -EINVAL;
582
583 hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input.data,
584 min_t(size_t, ev->u.input.size, UHID_DATA_MAX), 0);
585
586 return 0;
587}
588
Petri Gynther45226432014-03-24 13:50:01 -0700589static int uhid_dev_input2(struct uhid_device *uhid, struct uhid_event *ev)
590{
591 if (!uhid->running)
592 return -EINVAL;
593
594 hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input2.data,
595 min_t(size_t, ev->u.input2.size, UHID_DATA_MAX), 0);
596
597 return 0;
598}
599
David Herrmannfa71f322014-07-29 17:14:21 +0200600static int uhid_dev_get_report_reply(struct uhid_device *uhid,
601 struct uhid_event *ev)
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200602{
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200603 if (!uhid->running)
604 return -EINVAL;
605
David Herrmann11c22152014-07-29 17:14:24 +0200606 uhid_report_wake_up(uhid, ev->u.get_report_reply.id, ev);
607 return 0;
608}
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200609
David Herrmann11c22152014-07-29 17:14:24 +0200610static int uhid_dev_set_report_reply(struct uhid_device *uhid,
611 struct uhid_event *ev)
612{
613 if (!uhid->running)
614 return -EINVAL;
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200615
David Herrmann11c22152014-07-29 17:14:24 +0200616 uhid_report_wake_up(uhid, ev->u.set_report_reply.id, ev);
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200617 return 0;
618}
619
David Herrmann1ccd7a22012-06-10 15:16:13 +0200620static int uhid_char_open(struct inode *inode, struct file *file)
621{
David Herrmannace3d862012-06-10 15:16:14 +0200622 struct uhid_device *uhid;
623
624 uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
625 if (!uhid)
626 return -ENOMEM;
627
David Herrmannd937ae52012-06-10 15:16:16 +0200628 mutex_init(&uhid->devlock);
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200629 mutex_init(&uhid->report_lock);
David Herrmannace3d862012-06-10 15:16:14 +0200630 spin_lock_init(&uhid->qlock);
631 init_waitqueue_head(&uhid->waitq);
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200632 init_waitqueue_head(&uhid->report_wait);
David Herrmannd365c6c2012-06-10 15:16:18 +0200633 uhid->running = false;
Roderick Colenbrander67f8ecc2016-05-18 13:11:09 -0700634 INIT_WORK(&uhid->worker, uhid_device_add_worker);
David Herrmannace3d862012-06-10 15:16:14 +0200635
636 file->private_data = uhid;
637 nonseekable_open(inode, file);
638
David Herrmann1ccd7a22012-06-10 15:16:13 +0200639 return 0;
640}
641
642static int uhid_char_release(struct inode *inode, struct file *file)
643{
David Herrmannace3d862012-06-10 15:16:14 +0200644 struct uhid_device *uhid = file->private_data;
645 unsigned int i;
646
David Herrmannd365c6c2012-06-10 15:16:18 +0200647 uhid_dev_destroy(uhid);
648
David Herrmannace3d862012-06-10 15:16:14 +0200649 for (i = 0; i < UHID_BUFSIZE; ++i)
650 kfree(uhid->outq[i]);
651
652 kfree(uhid);
653
David Herrmann1ccd7a22012-06-10 15:16:13 +0200654 return 0;
655}
656
657static ssize_t uhid_char_read(struct file *file, char __user *buffer,
658 size_t count, loff_t *ppos)
659{
David Herrmannd937ae52012-06-10 15:16:16 +0200660 struct uhid_device *uhid = file->private_data;
661 int ret;
662 unsigned long flags;
663 size_t len;
664
665 /* they need at least the "type" member of uhid_event */
666 if (count < sizeof(__u32))
667 return -EINVAL;
668
669try_again:
670 if (file->f_flags & O_NONBLOCK) {
671 if (uhid->head == uhid->tail)
672 return -EAGAIN;
673 } else {
674 ret = wait_event_interruptible(uhid->waitq,
675 uhid->head != uhid->tail);
676 if (ret)
677 return ret;
678 }
679
680 ret = mutex_lock_interruptible(&uhid->devlock);
681 if (ret)
682 return ret;
683
684 if (uhid->head == uhid->tail) {
685 mutex_unlock(&uhid->devlock);
686 goto try_again;
687 } else {
688 len = min(count, sizeof(**uhid->outq));
Vinicius Costa Gomesadefb692012-07-14 18:59:25 -0300689 if (copy_to_user(buffer, uhid->outq[uhid->tail], len)) {
David Herrmannd937ae52012-06-10 15:16:16 +0200690 ret = -EFAULT;
691 } else {
692 kfree(uhid->outq[uhid->tail]);
693 uhid->outq[uhid->tail] = NULL;
694
695 spin_lock_irqsave(&uhid->qlock, flags);
696 uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE;
697 spin_unlock_irqrestore(&uhid->qlock, flags);
698 }
699 }
700
701 mutex_unlock(&uhid->devlock);
702 return ret ? ret : len;
David Herrmann1ccd7a22012-06-10 15:16:13 +0200703}
704
705static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
706 size_t count, loff_t *ppos)
707{
David Herrmann6664ef72012-06-10 15:16:17 +0200708 struct uhid_device *uhid = file->private_data;
709 int ret;
710 size_t len;
711
712 /* we need at least the "type" member of uhid_event */
713 if (count < sizeof(__u32))
714 return -EINVAL;
715
716 ret = mutex_lock_interruptible(&uhid->devlock);
717 if (ret)
718 return ret;
719
720 memset(&uhid->input_buf, 0, sizeof(uhid->input_buf));
721 len = min(count, sizeof(uhid->input_buf));
Dmitry Torokhovbefde022013-02-18 11:26:11 +0100722
723 ret = uhid_event_from_user(buffer, len, &uhid->input_buf);
724 if (ret)
David Herrmann6664ef72012-06-10 15:16:17 +0200725 goto unlock;
David Herrmann6664ef72012-06-10 15:16:17 +0200726
727 switch (uhid->input_buf.type) {
David Herrmannd365c6c2012-06-10 15:16:18 +0200728 case UHID_CREATE:
Eric Biggersab26f7f2018-11-14 13:55:09 -0800729 /*
730 * 'struct uhid_create_req' contains a __user pointer which is
731 * copied from, so it's unsafe to allow this with elevated
732 * privileges (e.g. from a setuid binary) or via kernel_write().
733 */
734 if (file->f_cred != current_cred() || uaccess_kernel()) {
735 pr_err_once("UHID_CREATE from different security context by process %d (%s), this is not allowed.\n",
736 task_tgid_vnr(current), current->comm);
737 ret = -EACCES;
738 goto unlock;
739 }
David Herrmannd365c6c2012-06-10 15:16:18 +0200740 ret = uhid_dev_create(uhid, &uhid->input_buf);
741 break;
Petri Gynther45226432014-03-24 13:50:01 -0700742 case UHID_CREATE2:
743 ret = uhid_dev_create2(uhid, &uhid->input_buf);
744 break;
David Herrmannd365c6c2012-06-10 15:16:18 +0200745 case UHID_DESTROY:
746 ret = uhid_dev_destroy(uhid);
747 break;
David Herrmann5e87a362012-06-10 15:16:19 +0200748 case UHID_INPUT:
749 ret = uhid_dev_input(uhid, &uhid->input_buf);
750 break;
Petri Gynther45226432014-03-24 13:50:01 -0700751 case UHID_INPUT2:
752 ret = uhid_dev_input2(uhid, &uhid->input_buf);
753 break;
David Herrmannfa71f322014-07-29 17:14:21 +0200754 case UHID_GET_REPORT_REPLY:
755 ret = uhid_dev_get_report_reply(uhid, &uhid->input_buf);
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200756 break;
David Herrmann11c22152014-07-29 17:14:24 +0200757 case UHID_SET_REPORT_REPLY:
758 ret = uhid_dev_set_report_reply(uhid, &uhid->input_buf);
759 break;
David Herrmann6664ef72012-06-10 15:16:17 +0200760 default:
761 ret = -EOPNOTSUPP;
762 }
763
764unlock:
765 mutex_unlock(&uhid->devlock);
766
767 /* return "count" not "len" to not confuse the caller */
768 return ret ? ret : count;
David Herrmann1ccd7a22012-06-10 15:16:13 +0200769}
770
771static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
772{
David Herrmann1f9dec12012-06-10 15:16:15 +0200773 struct uhid_device *uhid = file->private_data;
Jiri Kosinaf6d01872020-01-10 15:32:51 +0100774 unsigned int mask = POLLOUT | POLLWRNORM; /* uhid is always writable */
David Herrmann1f9dec12012-06-10 15:16:15 +0200775
776 poll_wait(file, &uhid->waitq, wait);
777
778 if (uhid->head != uhid->tail)
Jiri Kosinaf6d01872020-01-10 15:32:51 +0100779 mask |= POLLIN | POLLRDNORM;
David Herrmann1f9dec12012-06-10 15:16:15 +0200780
Jiri Kosinaf6d01872020-01-10 15:32:51 +0100781 return mask;
David Herrmann1ccd7a22012-06-10 15:16:13 +0200782}
783
784static const struct file_operations uhid_fops = {
785 .owner = THIS_MODULE,
786 .open = uhid_char_open,
787 .release = uhid_char_release,
788 .read = uhid_char_read,
789 .write = uhid_char_write,
790 .poll = uhid_char_poll,
791 .llseek = no_llseek,
792};
793
794static struct miscdevice uhid_misc = {
795 .fops = &uhid_fops,
David Herrmann19872d22013-09-09 18:33:54 +0200796 .minor = UHID_MINOR,
David Herrmann1ccd7a22012-06-10 15:16:13 +0200797 .name = UHID_NAME,
798};
PrasannaKumar Muralidharanca75d602016-08-25 22:30:49 +0530799module_misc_device(uhid_misc);
David Herrmann1ccd7a22012-06-10 15:16:13 +0200800
David Herrmann1ccd7a22012-06-10 15:16:13 +0200801MODULE_LICENSE("GPL");
802MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
803MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");
David Herrmann19872d22013-09-09 18:33:54 +0200804MODULE_ALIAS_MISCDEV(UHID_MINOR);
Marcel Holtmann60cbd532013-09-01 11:02:46 -0700805MODULE_ALIAS("devname:" UHID_NAME);