blob: 731a7b3e01870d163f5939ebc8256877914a5cd3 [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
34struct uhid_device {
David Herrmannd937ae52012-06-10 15:16:16 +020035 struct mutex devlock;
David Herrmannd365c6c2012-06-10 15:16:18 +020036 bool running;
37
38 __u8 *rd_data;
39 uint rd_size;
40
David Herrmannace3d862012-06-10 15:16:14 +020041 struct hid_device *hid;
David Herrmann6664ef72012-06-10 15:16:17 +020042 struct uhid_event input_buf;
David Herrmannace3d862012-06-10 15:16:14 +020043
44 wait_queue_head_t waitq;
45 spinlock_t qlock;
46 __u8 head;
47 __u8 tail;
48 struct uhid_event *outq[UHID_BUFSIZE];
David Herrmannfcfcf0d2012-06-10 15:16:25 +020049
David Herrmann8cad5b02014-07-29 17:14:19 +020050 /* blocking GET_REPORT support; state changes protected by qlock */
David Herrmannfcfcf0d2012-06-10 15:16:25 +020051 struct mutex report_lock;
52 wait_queue_head_t report_wait;
David Herrmann5942b842014-07-29 17:14:20 +020053 bool report_running;
David Herrmann8cad5b02014-07-29 17:14:19 +020054 u32 report_id;
David Herrmann11c22152014-07-29 17:14:24 +020055 u32 report_type;
David Herrmannfcfcf0d2012-06-10 15:16:25 +020056 struct uhid_event report_buf;
Roderick Colenbrander67f8ecc2016-05-18 13:11:09 -070057 struct work_struct worker;
David Herrmannace3d862012-06-10 15:16:14 +020058};
David Herrmann1ccd7a22012-06-10 15:16:13 +020059
60static struct miscdevice uhid_misc;
61
Roderick Colenbrander67f8ecc2016-05-18 13:11:09 -070062static void uhid_device_add_worker(struct work_struct *work)
63{
64 struct uhid_device *uhid = container_of(work, struct uhid_device, worker);
65 int ret;
66
67 ret = hid_add_device(uhid->hid);
68 if (ret) {
69 hid_err(uhid->hid, "Cannot register HID device: error %d\n", ret);
70
71 hid_destroy_device(uhid->hid);
72 uhid->hid = NULL;
73 uhid->running = false;
74 }
75}
76
David Herrmannace3d862012-06-10 15:16:14 +020077static void uhid_queue(struct uhid_device *uhid, struct uhid_event *ev)
78{
79 __u8 newhead;
80
81 newhead = (uhid->head + 1) % UHID_BUFSIZE;
82
83 if (newhead != uhid->tail) {
84 uhid->outq[uhid->head] = ev;
85 uhid->head = newhead;
86 wake_up_interruptible(&uhid->waitq);
87 } else {
88 hid_warn(uhid->hid, "Output queue is full\n");
89 kfree(ev);
90 }
91}
92
93static int uhid_queue_event(struct uhid_device *uhid, __u32 event)
94{
95 unsigned long flags;
96 struct uhid_event *ev;
97
98 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
99 if (!ev)
100 return -ENOMEM;
101
102 ev->type = event;
103
104 spin_lock_irqsave(&uhid->qlock, flags);
105 uhid_queue(uhid, ev);
106 spin_unlock_irqrestore(&uhid->qlock, flags);
107
108 return 0;
109}
110
David Herrmannd365c6c2012-06-10 15:16:18 +0200111static int uhid_hid_start(struct hid_device *hid)
112{
David Herrmannec4b7de2012-06-10 15:16:21 +0200113 struct uhid_device *uhid = hid->driver_data;
David Herrmannc2b2f162014-07-29 17:14:25 +0200114 struct uhid_event *ev;
115 unsigned long flags;
David Herrmannec4b7de2012-06-10 15:16:21 +0200116
David Herrmannc2b2f162014-07-29 17:14:25 +0200117 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
118 if (!ev)
119 return -ENOMEM;
120
121 ev->type = UHID_START;
122
123 if (hid->report_enum[HID_FEATURE_REPORT].numbered)
124 ev->u.start.dev_flags |= UHID_DEV_NUMBERED_FEATURE_REPORTS;
125 if (hid->report_enum[HID_OUTPUT_REPORT].numbered)
126 ev->u.start.dev_flags |= UHID_DEV_NUMBERED_OUTPUT_REPORTS;
127 if (hid->report_enum[HID_INPUT_REPORT].numbered)
128 ev->u.start.dev_flags |= UHID_DEV_NUMBERED_INPUT_REPORTS;
129
130 spin_lock_irqsave(&uhid->qlock, flags);
131 uhid_queue(uhid, ev);
132 spin_unlock_irqrestore(&uhid->qlock, flags);
133
134 return 0;
David Herrmannd365c6c2012-06-10 15:16:18 +0200135}
136
137static void uhid_hid_stop(struct hid_device *hid)
138{
David Herrmannec4b7de2012-06-10 15:16:21 +0200139 struct uhid_device *uhid = hid->driver_data;
140
141 hid->claimed = 0;
142 uhid_queue_event(uhid, UHID_STOP);
David Herrmannd365c6c2012-06-10 15:16:18 +0200143}
144
145static int uhid_hid_open(struct hid_device *hid)
146{
David Herrmanne7191472012-06-10 15:16:22 +0200147 struct uhid_device *uhid = hid->driver_data;
148
149 return uhid_queue_event(uhid, UHID_OPEN);
David Herrmannd365c6c2012-06-10 15:16:18 +0200150}
151
152static void uhid_hid_close(struct hid_device *hid)
153{
David Herrmanne7191472012-06-10 15:16:22 +0200154 struct uhid_device *uhid = hid->driver_data;
155
156 uhid_queue_event(uhid, UHID_CLOSE);
David Herrmannd365c6c2012-06-10 15:16:18 +0200157}
158
David Herrmannd365c6c2012-06-10 15:16:18 +0200159static int uhid_hid_parse(struct hid_device *hid)
160{
David Herrmann037c0612012-06-10 15:16:20 +0200161 struct uhid_device *uhid = hid->driver_data;
162
163 return hid_parse_report(hid, uhid->rd_data, uhid->rd_size);
David Herrmannd365c6c2012-06-10 15:16:18 +0200164}
165
David Herrmann11c22152014-07-29 17:14:24 +0200166/* must be called with report_lock held */
167static int __uhid_report_queue_and_wait(struct uhid_device *uhid,
168 struct uhid_event *ev,
169 __u32 *report_id)
Jiri Kosina289a7162014-02-17 14:49:34 +0100170{
Jiri Kosina289a7162014-02-17 14:49:34 +0100171 unsigned long flags;
172 int ret;
Jiri Kosina289a7162014-02-17 14:49:34 +0100173
174 spin_lock_irqsave(&uhid->qlock, flags);
David Herrmann11c22152014-07-29 17:14:24 +0200175 *report_id = ++uhid->report_id;
Benjamin Tissoires8493ecc2014-10-01 11:59:47 -0400176 uhid->report_type = ev->type + 1;
David Herrmann5942b842014-07-29 17:14:20 +0200177 uhid->report_running = true;
Jiri Kosina289a7162014-02-17 14:49:34 +0100178 uhid_queue(uhid, ev);
179 spin_unlock_irqrestore(&uhid->qlock, flags);
180
181 ret = wait_event_interruptible_timeout(uhid->report_wait,
David Herrmann5942b842014-07-29 17:14:20 +0200182 !uhid->report_running || !uhid->running,
183 5 * HZ);
David Herrmann11c22152014-07-29 17:14:24 +0200184 if (!ret || !uhid->running || uhid->report_running)
Jiri Kosina289a7162014-02-17 14:49:34 +0100185 ret = -EIO;
David Herrmann11c22152014-07-29 17:14:24 +0200186 else if (ret < 0)
Jiri Kosina289a7162014-02-17 14:49:34 +0100187 ret = -ERESTARTSYS;
David Herrmann11c22152014-07-29 17:14:24 +0200188 else
189 ret = 0;
Jiri Kosina289a7162014-02-17 14:49:34 +0100190
David Herrmann5942b842014-07-29 17:14:20 +0200191 uhid->report_running = false;
Jiri Kosina289a7162014-02-17 14:49:34 +0100192
David Herrmann11c22152014-07-29 17:14:24 +0200193 return ret;
194}
195
196static void uhid_report_wake_up(struct uhid_device *uhid, u32 id,
197 const struct uhid_event *ev)
198{
199 unsigned long flags;
200
201 spin_lock_irqsave(&uhid->qlock, flags);
202
203 /* id for old report; drop it silently */
204 if (uhid->report_type != ev->type || uhid->report_id != id)
205 goto unlock;
206 if (!uhid->report_running)
207 goto unlock;
208
209 memcpy(&uhid->report_buf, ev, sizeof(*ev));
210 uhid->report_running = false;
211 wake_up_interruptible(&uhid->report_wait);
212
213unlock:
214 spin_unlock_irqrestore(&uhid->qlock, flags);
215}
216
217static int uhid_hid_get_report(struct hid_device *hid, unsigned char rnum,
218 u8 *buf, size_t count, u8 rtype)
219{
220 struct uhid_device *uhid = hid->driver_data;
221 struct uhid_get_report_reply_req *req;
222 struct uhid_event *ev;
223 int ret;
224
225 if (!uhid->running)
226 return -EIO;
227
228 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
229 if (!ev)
230 return -ENOMEM;
231
232 ev->type = UHID_GET_REPORT;
233 ev->u.get_report.rnum = rnum;
234 ev->u.get_report.rtype = rtype;
235
236 ret = mutex_lock_interruptible(&uhid->report_lock);
237 if (ret) {
238 kfree(ev);
239 return ret;
240 }
241
242 /* this _always_ takes ownership of @ev */
243 ret = __uhid_report_queue_and_wait(uhid, ev, &ev->u.get_report.id);
244 if (ret)
245 goto unlock;
246
247 req = &uhid->report_buf.u.get_report_reply;
248 if (req->err) {
249 ret = -EIO;
250 } else {
251 ret = min3(count, (size_t)req->size, (size_t)UHID_DATA_MAX);
252 memcpy(buf, req->data, ret);
253 }
254
Jiri Kosina289a7162014-02-17 14:49:34 +0100255unlock:
256 mutex_unlock(&uhid->report_lock);
David Herrmann11c22152014-07-29 17:14:24 +0200257 return ret;
258}
259
260static int uhid_hid_set_report(struct hid_device *hid, unsigned char rnum,
261 const u8 *buf, size_t count, u8 rtype)
262{
263 struct uhid_device *uhid = hid->driver_data;
264 struct uhid_event *ev;
265 int ret;
266
267 if (!uhid->running || count > UHID_DATA_MAX)
268 return -EIO;
269
270 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
271 if (!ev)
272 return -ENOMEM;
273
274 ev->type = UHID_SET_REPORT;
275 ev->u.set_report.rnum = rnum;
276 ev->u.set_report.rtype = rtype;
277 ev->u.set_report.size = count;
278 memcpy(ev->u.set_report.data, buf, count);
279
280 ret = mutex_lock_interruptible(&uhid->report_lock);
281 if (ret) {
282 kfree(ev);
283 return ret;
284 }
285
286 /* this _always_ takes ownership of @ev */
287 ret = __uhid_report_queue_and_wait(uhid, ev, &ev->u.set_report.id);
288 if (ret)
289 goto unlock;
290
291 if (uhid->report_buf.u.set_report_reply.err)
292 ret = -EIO;
293 else
294 ret = count;
295
296unlock:
297 mutex_unlock(&uhid->report_lock);
298 return ret;
Jiri Kosina289a7162014-02-17 14:49:34 +0100299}
300
David Herrmann7c4003b2014-07-29 17:14:23 +0200301static int uhid_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
302 __u8 *buf, size_t len, unsigned char rtype,
303 int reqtype)
304{
David Herrmann11c22152014-07-29 17:14:24 +0200305 u8 u_rtype;
306
307 switch (rtype) {
308 case HID_FEATURE_REPORT:
309 u_rtype = UHID_FEATURE_REPORT;
310 break;
311 case HID_OUTPUT_REPORT:
312 u_rtype = UHID_OUTPUT_REPORT;
313 break;
314 case HID_INPUT_REPORT:
315 u_rtype = UHID_INPUT_REPORT;
316 break;
317 default:
318 return -EINVAL;
319 }
320
David Herrmann7c4003b2014-07-29 17:14:23 +0200321 switch (reqtype) {
322 case HID_REQ_GET_REPORT:
David Herrmann11c22152014-07-29 17:14:24 +0200323 return uhid_hid_get_report(hid, reportnum, buf, len, u_rtype);
David Herrmann7c4003b2014-07-29 17:14:23 +0200324 case HID_REQ_SET_REPORT:
David Herrmann11c22152014-07-29 17:14:24 +0200325 return uhid_hid_set_report(hid, reportnum, buf, len, u_rtype);
David Herrmann7c4003b2014-07-29 17:14:23 +0200326 default:
327 return -EIO;
328 }
329}
330
David Herrmannd365c6c2012-06-10 15:16:18 +0200331static int uhid_hid_output_raw(struct hid_device *hid, __u8 *buf, size_t count,
332 unsigned char report_type)
333{
David Herrmann3b3baa82012-06-10 15:16:24 +0200334 struct uhid_device *uhid = hid->driver_data;
335 __u8 rtype;
336 unsigned long flags;
337 struct uhid_event *ev;
338
339 switch (report_type) {
340 case HID_FEATURE_REPORT:
341 rtype = UHID_FEATURE_REPORT;
342 break;
343 case HID_OUTPUT_REPORT:
344 rtype = UHID_OUTPUT_REPORT;
345 break;
346 default:
347 return -EINVAL;
348 }
349
350 if (count < 1 || count > UHID_DATA_MAX)
351 return -EINVAL;
352
353 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
354 if (!ev)
355 return -ENOMEM;
356
357 ev->type = UHID_OUTPUT;
358 ev->u.output.size = count;
359 ev->u.output.rtype = rtype;
360 memcpy(ev->u.output.data, buf, count);
361
362 spin_lock_irqsave(&uhid->qlock, flags);
363 uhid_queue(uhid, ev);
364 spin_unlock_irqrestore(&uhid->qlock, flags);
365
366 return count;
David Herrmannd365c6c2012-06-10 15:16:18 +0200367}
368
Frank Praznik596cfdd2014-01-22 13:49:43 -0500369static int uhid_hid_output_report(struct hid_device *hid, __u8 *buf,
370 size_t count)
371{
Benjamin Tissoires41abfb32014-02-10 12:58:46 -0500372 return uhid_hid_output_raw(hid, buf, count, HID_OUTPUT_REPORT);
Frank Praznik596cfdd2014-01-22 13:49:43 -0500373}
374
David Herrmannd365c6c2012-06-10 15:16:18 +0200375static struct hid_ll_driver uhid_hid_driver = {
376 .start = uhid_hid_start,
377 .stop = uhid_hid_stop,
378 .open = uhid_hid_open,
379 .close = uhid_hid_close,
David Herrmannd365c6c2012-06-10 15:16:18 +0200380 .parse = uhid_hid_parse,
David Herrmann7c4003b2014-07-29 17:14:23 +0200381 .raw_request = uhid_hid_raw_request,
Frank Praznik596cfdd2014-01-22 13:49:43 -0500382 .output_report = uhid_hid_output_report,
David Herrmannd365c6c2012-06-10 15:16:18 +0200383};
384
Dmitry Torokhovbefde022013-02-18 11:26:11 +0100385#ifdef CONFIG_COMPAT
386
387/* Apparently we haven't stepped on these rakes enough times yet. */
388struct uhid_create_req_compat {
389 __u8 name[128];
390 __u8 phys[64];
391 __u8 uniq[64];
392
393 compat_uptr_t rd_data;
394 __u16 rd_size;
395
396 __u16 bus;
397 __u32 vendor;
398 __u32 product;
399 __u32 version;
400 __u32 country;
401} __attribute__((__packed__));
402
403static int uhid_event_from_user(const char __user *buffer, size_t len,
404 struct uhid_event *event)
405{
Andy Lutomirski7365abb2016-03-22 14:25:24 -0700406 if (in_compat_syscall()) {
Dmitry Torokhovbefde022013-02-18 11:26:11 +0100407 u32 type;
408
409 if (get_user(type, buffer))
410 return -EFAULT;
411
412 if (type == UHID_CREATE) {
413 /*
414 * This is our messed up request with compat pointer.
415 * It is largish (more than 256 bytes) so we better
416 * allocate it from the heap.
417 */
418 struct uhid_create_req_compat *compat;
419
David Herrmann80897aa2013-11-26 13:58:18 +0100420 compat = kzalloc(sizeof(*compat), GFP_KERNEL);
Dmitry Torokhovbefde022013-02-18 11:26:11 +0100421 if (!compat)
422 return -ENOMEM;
423
424 buffer += sizeof(type);
425 len -= sizeof(type);
426 if (copy_from_user(compat, buffer,
427 min(len, sizeof(*compat)))) {
428 kfree(compat);
429 return -EFAULT;
430 }
431
432 /* Shuffle the data over to proper structure */
433 event->type = type;
434
435 memcpy(event->u.create.name, compat->name,
436 sizeof(compat->name));
437 memcpy(event->u.create.phys, compat->phys,
438 sizeof(compat->phys));
439 memcpy(event->u.create.uniq, compat->uniq,
440 sizeof(compat->uniq));
441
442 event->u.create.rd_data = compat_ptr(compat->rd_data);
443 event->u.create.rd_size = compat->rd_size;
444
445 event->u.create.bus = compat->bus;
446 event->u.create.vendor = compat->vendor;
447 event->u.create.product = compat->product;
448 event->u.create.version = compat->version;
449 event->u.create.country = compat->country;
450
451 kfree(compat);
452 return 0;
453 }
454 /* All others can be copied directly */
455 }
456
457 if (copy_from_user(event, buffer, min(len, sizeof(*event))))
458 return -EFAULT;
459
460 return 0;
461}
462#else
463static int uhid_event_from_user(const char __user *buffer, size_t len,
464 struct uhid_event *event)
465{
466 if (copy_from_user(event, buffer, min(len, sizeof(*event))))
467 return -EFAULT;
468
469 return 0;
470}
471#endif
472
Petri Gynther45226432014-03-24 13:50:01 -0700473static int uhid_dev_create2(struct uhid_device *uhid,
474 const struct uhid_event *ev)
475{
476 struct hid_device *hid;
David Herrmann25be7fe2014-07-29 17:14:18 +0200477 size_t rd_size, len;
David Herrmann41c4a462014-07-29 17:14:17 +0200478 void *rd_data;
Petri Gynther45226432014-03-24 13:50:01 -0700479 int ret;
480
481 if (uhid->running)
482 return -EALREADY;
483
David Herrmann41c4a462014-07-29 17:14:17 +0200484 rd_size = ev->u.create2.rd_size;
485 if (rd_size <= 0 || rd_size > HID_MAX_DESCRIPTOR_SIZE)
Petri Gynther45226432014-03-24 13:50:01 -0700486 return -EINVAL;
487
David Herrmann41c4a462014-07-29 17:14:17 +0200488 rd_data = kmemdup(ev->u.create2.rd_data, rd_size, GFP_KERNEL);
489 if (!rd_data)
Petri Gynther45226432014-03-24 13:50:01 -0700490 return -ENOMEM;
491
David Herrmann41c4a462014-07-29 17:14:17 +0200492 uhid->rd_size = rd_size;
493 uhid->rd_data = rd_data;
494
Petri Gynther45226432014-03-24 13:50:01 -0700495 hid = hid_allocate_device();
496 if (IS_ERR(hid)) {
497 ret = PTR_ERR(hid);
498 goto err_free;
499 }
500
David Herrmann25be7fe2014-07-29 17:14:18 +0200501 len = min(sizeof(hid->name), sizeof(ev->u.create2.name)) - 1;
502 strncpy(hid->name, ev->u.create2.name, len);
503 len = min(sizeof(hid->phys), sizeof(ev->u.create2.phys)) - 1;
504 strncpy(hid->phys, ev->u.create2.phys, len);
505 len = min(sizeof(hid->uniq), sizeof(ev->u.create2.uniq)) - 1;
506 strncpy(hid->uniq, ev->u.create2.uniq, len);
Petri Gynther45226432014-03-24 13:50:01 -0700507
508 hid->ll_driver = &uhid_hid_driver;
509 hid->bus = ev->u.create2.bus;
510 hid->vendor = ev->u.create2.vendor;
511 hid->product = ev->u.create2.product;
512 hid->version = ev->u.create2.version;
513 hid->country = ev->u.create2.country;
514 hid->driver_data = uhid;
515 hid->dev.parent = uhid_misc.this_device;
516
517 uhid->hid = hid;
518 uhid->running = true;
519
Roderick Colenbrander67f8ecc2016-05-18 13:11:09 -0700520 /* Adding of a HID device is done through a worker, to allow HID drivers
521 * which use feature requests during .probe to work, without they would
522 * be blocked on devlock, which is held by uhid_char_write.
523 */
524 schedule_work(&uhid->worker);
Petri Gynther45226432014-03-24 13:50:01 -0700525
526 return 0;
527
Petri Gynther45226432014-03-24 13:50:01 -0700528err_free:
529 kfree(uhid->rd_data);
David Herrmann41c4a462014-07-29 17:14:17 +0200530 uhid->rd_data = NULL;
531 uhid->rd_size = 0;
Petri Gynther45226432014-03-24 13:50:01 -0700532 return ret;
533}
534
David Herrmann56c47752014-07-29 17:14:16 +0200535static int uhid_dev_create(struct uhid_device *uhid,
536 struct uhid_event *ev)
537{
538 struct uhid_create_req orig;
539
540 orig = ev->u.create;
541
542 if (orig.rd_size <= 0 || orig.rd_size > HID_MAX_DESCRIPTOR_SIZE)
543 return -EINVAL;
544 if (copy_from_user(&ev->u.create2.rd_data, orig.rd_data, orig.rd_size))
545 return -EFAULT;
546
547 memcpy(ev->u.create2.name, orig.name, sizeof(orig.name));
548 memcpy(ev->u.create2.phys, orig.phys, sizeof(orig.phys));
549 memcpy(ev->u.create2.uniq, orig.uniq, sizeof(orig.uniq));
550 ev->u.create2.rd_size = orig.rd_size;
551 ev->u.create2.bus = orig.bus;
552 ev->u.create2.vendor = orig.vendor;
553 ev->u.create2.product = orig.product;
554 ev->u.create2.version = orig.version;
555 ev->u.create2.country = orig.country;
556
557 return uhid_dev_create2(uhid, ev);
558}
559
David Herrmannd365c6c2012-06-10 15:16:18 +0200560static int uhid_dev_destroy(struct uhid_device *uhid)
561{
562 if (!uhid->running)
563 return -EINVAL;
564
565 uhid->running = false;
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200566 wake_up_interruptible(&uhid->report_wait);
David Herrmannd365c6c2012-06-10 15:16:18 +0200567
Roderick Colenbrander67f8ecc2016-05-18 13:11:09 -0700568 cancel_work_sync(&uhid->worker);
569
David Herrmannd365c6c2012-06-10 15:16:18 +0200570 hid_destroy_device(uhid->hid);
571 kfree(uhid->rd_data);
572
573 return 0;
574}
575
David Herrmann5e87a362012-06-10 15:16:19 +0200576static int uhid_dev_input(struct uhid_device *uhid, struct uhid_event *ev)
577{
578 if (!uhid->running)
579 return -EINVAL;
580
581 hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input.data,
582 min_t(size_t, ev->u.input.size, UHID_DATA_MAX), 0);
583
584 return 0;
585}
586
Petri Gynther45226432014-03-24 13:50:01 -0700587static int uhid_dev_input2(struct uhid_device *uhid, struct uhid_event *ev)
588{
589 if (!uhid->running)
590 return -EINVAL;
591
592 hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input2.data,
593 min_t(size_t, ev->u.input2.size, UHID_DATA_MAX), 0);
594
595 return 0;
596}
597
David Herrmannfa71f322014-07-29 17:14:21 +0200598static int uhid_dev_get_report_reply(struct uhid_device *uhid,
599 struct uhid_event *ev)
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200600{
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200601 if (!uhid->running)
602 return -EINVAL;
603
David Herrmann11c22152014-07-29 17:14:24 +0200604 uhid_report_wake_up(uhid, ev->u.get_report_reply.id, ev);
605 return 0;
606}
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200607
David Herrmann11c22152014-07-29 17:14:24 +0200608static int uhid_dev_set_report_reply(struct uhid_device *uhid,
609 struct uhid_event *ev)
610{
611 if (!uhid->running)
612 return -EINVAL;
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200613
David Herrmann11c22152014-07-29 17:14:24 +0200614 uhid_report_wake_up(uhid, ev->u.set_report_reply.id, ev);
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200615 return 0;
616}
617
David Herrmann1ccd7a22012-06-10 15:16:13 +0200618static int uhid_char_open(struct inode *inode, struct file *file)
619{
David Herrmannace3d862012-06-10 15:16:14 +0200620 struct uhid_device *uhid;
621
622 uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
623 if (!uhid)
624 return -ENOMEM;
625
David Herrmannd937ae52012-06-10 15:16:16 +0200626 mutex_init(&uhid->devlock);
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200627 mutex_init(&uhid->report_lock);
David Herrmannace3d862012-06-10 15:16:14 +0200628 spin_lock_init(&uhid->qlock);
629 init_waitqueue_head(&uhid->waitq);
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200630 init_waitqueue_head(&uhid->report_wait);
David Herrmannd365c6c2012-06-10 15:16:18 +0200631 uhid->running = false;
Roderick Colenbrander67f8ecc2016-05-18 13:11:09 -0700632 INIT_WORK(&uhid->worker, uhid_device_add_worker);
David Herrmannace3d862012-06-10 15:16:14 +0200633
634 file->private_data = uhid;
635 nonseekable_open(inode, file);
636
David Herrmann1ccd7a22012-06-10 15:16:13 +0200637 return 0;
638}
639
640static int uhid_char_release(struct inode *inode, struct file *file)
641{
David Herrmannace3d862012-06-10 15:16:14 +0200642 struct uhid_device *uhid = file->private_data;
643 unsigned int i;
644
David Herrmannd365c6c2012-06-10 15:16:18 +0200645 uhid_dev_destroy(uhid);
646
David Herrmannace3d862012-06-10 15:16:14 +0200647 for (i = 0; i < UHID_BUFSIZE; ++i)
648 kfree(uhid->outq[i]);
649
650 kfree(uhid);
651
David Herrmann1ccd7a22012-06-10 15:16:13 +0200652 return 0;
653}
654
655static ssize_t uhid_char_read(struct file *file, char __user *buffer,
656 size_t count, loff_t *ppos)
657{
David Herrmannd937ae52012-06-10 15:16:16 +0200658 struct uhid_device *uhid = file->private_data;
659 int ret;
660 unsigned long flags;
661 size_t len;
662
663 /* they need at least the "type" member of uhid_event */
664 if (count < sizeof(__u32))
665 return -EINVAL;
666
667try_again:
668 if (file->f_flags & O_NONBLOCK) {
669 if (uhid->head == uhid->tail)
670 return -EAGAIN;
671 } else {
672 ret = wait_event_interruptible(uhid->waitq,
673 uhid->head != uhid->tail);
674 if (ret)
675 return ret;
676 }
677
678 ret = mutex_lock_interruptible(&uhid->devlock);
679 if (ret)
680 return ret;
681
682 if (uhid->head == uhid->tail) {
683 mutex_unlock(&uhid->devlock);
684 goto try_again;
685 } else {
686 len = min(count, sizeof(**uhid->outq));
Vinicius Costa Gomesadefb692012-07-14 18:59:25 -0300687 if (copy_to_user(buffer, uhid->outq[uhid->tail], len)) {
David Herrmannd937ae52012-06-10 15:16:16 +0200688 ret = -EFAULT;
689 } else {
690 kfree(uhid->outq[uhid->tail]);
691 uhid->outq[uhid->tail] = NULL;
692
693 spin_lock_irqsave(&uhid->qlock, flags);
694 uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE;
695 spin_unlock_irqrestore(&uhid->qlock, flags);
696 }
697 }
698
699 mutex_unlock(&uhid->devlock);
700 return ret ? ret : len;
David Herrmann1ccd7a22012-06-10 15:16:13 +0200701}
702
703static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
704 size_t count, loff_t *ppos)
705{
David Herrmann6664ef72012-06-10 15:16:17 +0200706 struct uhid_device *uhid = file->private_data;
707 int ret;
708 size_t len;
709
710 /* we need at least the "type" member of uhid_event */
711 if (count < sizeof(__u32))
712 return -EINVAL;
713
714 ret = mutex_lock_interruptible(&uhid->devlock);
715 if (ret)
716 return ret;
717
718 memset(&uhid->input_buf, 0, sizeof(uhid->input_buf));
719 len = min(count, sizeof(uhid->input_buf));
Dmitry Torokhovbefde022013-02-18 11:26:11 +0100720
721 ret = uhid_event_from_user(buffer, len, &uhid->input_buf);
722 if (ret)
David Herrmann6664ef72012-06-10 15:16:17 +0200723 goto unlock;
David Herrmann6664ef72012-06-10 15:16:17 +0200724
725 switch (uhid->input_buf.type) {
David Herrmannd365c6c2012-06-10 15:16:18 +0200726 case UHID_CREATE:
Eric Biggersab26f7f2018-11-14 13:55:09 -0800727 /*
728 * 'struct uhid_create_req' contains a __user pointer which is
729 * copied from, so it's unsafe to allow this with elevated
730 * privileges (e.g. from a setuid binary) or via kernel_write().
731 */
732 if (file->f_cred != current_cred() || uaccess_kernel()) {
733 pr_err_once("UHID_CREATE from different security context by process %d (%s), this is not allowed.\n",
734 task_tgid_vnr(current), current->comm);
735 ret = -EACCES;
736 goto unlock;
737 }
David Herrmannd365c6c2012-06-10 15:16:18 +0200738 ret = uhid_dev_create(uhid, &uhid->input_buf);
739 break;
Petri Gynther45226432014-03-24 13:50:01 -0700740 case UHID_CREATE2:
741 ret = uhid_dev_create2(uhid, &uhid->input_buf);
742 break;
David Herrmannd365c6c2012-06-10 15:16:18 +0200743 case UHID_DESTROY:
744 ret = uhid_dev_destroy(uhid);
745 break;
David Herrmann5e87a362012-06-10 15:16:19 +0200746 case UHID_INPUT:
747 ret = uhid_dev_input(uhid, &uhid->input_buf);
748 break;
Petri Gynther45226432014-03-24 13:50:01 -0700749 case UHID_INPUT2:
750 ret = uhid_dev_input2(uhid, &uhid->input_buf);
751 break;
David Herrmannfa71f322014-07-29 17:14:21 +0200752 case UHID_GET_REPORT_REPLY:
753 ret = uhid_dev_get_report_reply(uhid, &uhid->input_buf);
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200754 break;
David Herrmann11c22152014-07-29 17:14:24 +0200755 case UHID_SET_REPORT_REPLY:
756 ret = uhid_dev_set_report_reply(uhid, &uhid->input_buf);
757 break;
David Herrmann6664ef72012-06-10 15:16:17 +0200758 default:
759 ret = -EOPNOTSUPP;
760 }
761
762unlock:
763 mutex_unlock(&uhid->devlock);
764
765 /* return "count" not "len" to not confuse the caller */
766 return ret ? ret : count;
David Herrmann1ccd7a22012-06-10 15:16:13 +0200767}
768
769static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
770{
David Herrmann1f9dec12012-06-10 15:16:15 +0200771 struct uhid_device *uhid = file->private_data;
Jiri Kosinaf6d01872020-01-10 15:32:51 +0100772 unsigned int mask = POLLOUT | POLLWRNORM; /* uhid is always writable */
David Herrmann1f9dec12012-06-10 15:16:15 +0200773
774 poll_wait(file, &uhid->waitq, wait);
775
776 if (uhid->head != uhid->tail)
Jiri Kosinaf6d01872020-01-10 15:32:51 +0100777 mask |= POLLIN | POLLRDNORM;
David Herrmann1f9dec12012-06-10 15:16:15 +0200778
Jiri Kosinaf6d01872020-01-10 15:32:51 +0100779 return mask;
David Herrmann1ccd7a22012-06-10 15:16:13 +0200780}
781
782static const struct file_operations uhid_fops = {
783 .owner = THIS_MODULE,
784 .open = uhid_char_open,
785 .release = uhid_char_release,
786 .read = uhid_char_read,
787 .write = uhid_char_write,
788 .poll = uhid_char_poll,
789 .llseek = no_llseek,
790};
791
792static struct miscdevice uhid_misc = {
793 .fops = &uhid_fops,
David Herrmann19872d22013-09-09 18:33:54 +0200794 .minor = UHID_MINOR,
David Herrmann1ccd7a22012-06-10 15:16:13 +0200795 .name = UHID_NAME,
796};
PrasannaKumar Muralidharanca75d602016-08-25 22:30:49 +0530797module_misc_device(uhid_misc);
David Herrmann1ccd7a22012-06-10 15:16:13 +0200798
David Herrmann1ccd7a22012-06-10 15:16:13 +0200799MODULE_LICENSE("GPL");
800MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
801MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");
David Herrmann19872d22013-09-09 18:33:54 +0200802MODULE_ALIAS_MISCDEV(UHID_MINOR);
Marcel Holtmann60cbd532013-09-01 11:02:46 -0700803MODULE_ALIAS("devname:" UHID_NAME);