blob: 5e6d32251d0908e6e2b832b90a9f62f8664f0f85 [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
Jason Gereckee6dfea82017-07-24 09:46:18 -0700377struct hid_ll_driver uhid_hid_driver = {
David Herrmannd365c6c2012-06-10 15:16:18 +0200378 .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};
Jason Gereckee6dfea82017-07-24 09:46:18 -0700386EXPORT_SYMBOL_GPL(uhid_hid_driver);
David Herrmannd365c6c2012-06-10 15:16:18 +0200387
Dmitry Torokhovbefde022013-02-18 11:26:11 +0100388#ifdef CONFIG_COMPAT
389
390/* Apparently we haven't stepped on these rakes enough times yet. */
391struct uhid_create_req_compat {
392 __u8 name[128];
393 __u8 phys[64];
394 __u8 uniq[64];
395
396 compat_uptr_t rd_data;
397 __u16 rd_size;
398
399 __u16 bus;
400 __u32 vendor;
401 __u32 product;
402 __u32 version;
403 __u32 country;
404} __attribute__((__packed__));
405
406static int uhid_event_from_user(const char __user *buffer, size_t len,
407 struct uhid_event *event)
408{
Andy Lutomirski7365abb2016-03-22 14:25:24 -0700409 if (in_compat_syscall()) {
Dmitry Torokhovbefde022013-02-18 11:26:11 +0100410 u32 type;
411
412 if (get_user(type, buffer))
413 return -EFAULT;
414
415 if (type == UHID_CREATE) {
416 /*
417 * This is our messed up request with compat pointer.
418 * It is largish (more than 256 bytes) so we better
419 * allocate it from the heap.
420 */
421 struct uhid_create_req_compat *compat;
422
David Herrmann80897aa2013-11-26 13:58:18 +0100423 compat = kzalloc(sizeof(*compat), GFP_KERNEL);
Dmitry Torokhovbefde022013-02-18 11:26:11 +0100424 if (!compat)
425 return -ENOMEM;
426
427 buffer += sizeof(type);
428 len -= sizeof(type);
429 if (copy_from_user(compat, buffer,
430 min(len, sizeof(*compat)))) {
431 kfree(compat);
432 return -EFAULT;
433 }
434
435 /* Shuffle the data over to proper structure */
436 event->type = type;
437
438 memcpy(event->u.create.name, compat->name,
439 sizeof(compat->name));
440 memcpy(event->u.create.phys, compat->phys,
441 sizeof(compat->phys));
442 memcpy(event->u.create.uniq, compat->uniq,
443 sizeof(compat->uniq));
444
445 event->u.create.rd_data = compat_ptr(compat->rd_data);
446 event->u.create.rd_size = compat->rd_size;
447
448 event->u.create.bus = compat->bus;
449 event->u.create.vendor = compat->vendor;
450 event->u.create.product = compat->product;
451 event->u.create.version = compat->version;
452 event->u.create.country = compat->country;
453
454 kfree(compat);
455 return 0;
456 }
457 /* All others can be copied directly */
458 }
459
460 if (copy_from_user(event, buffer, min(len, sizeof(*event))))
461 return -EFAULT;
462
463 return 0;
464}
465#else
466static int uhid_event_from_user(const char __user *buffer, size_t len,
467 struct uhid_event *event)
468{
469 if (copy_from_user(event, buffer, min(len, sizeof(*event))))
470 return -EFAULT;
471
472 return 0;
473}
474#endif
475
Petri Gynther45226432014-03-24 13:50:01 -0700476static int uhid_dev_create2(struct uhid_device *uhid,
477 const struct uhid_event *ev)
478{
479 struct hid_device *hid;
David Herrmann25be7fe2014-07-29 17:14:18 +0200480 size_t rd_size, len;
David Herrmann41c4a462014-07-29 17:14:17 +0200481 void *rd_data;
Petri Gynther45226432014-03-24 13:50:01 -0700482 int ret;
483
484 if (uhid->running)
485 return -EALREADY;
486
David Herrmann41c4a462014-07-29 17:14:17 +0200487 rd_size = ev->u.create2.rd_size;
488 if (rd_size <= 0 || rd_size > HID_MAX_DESCRIPTOR_SIZE)
Petri Gynther45226432014-03-24 13:50:01 -0700489 return -EINVAL;
490
David Herrmann41c4a462014-07-29 17:14:17 +0200491 rd_data = kmemdup(ev->u.create2.rd_data, rd_size, GFP_KERNEL);
492 if (!rd_data)
Petri Gynther45226432014-03-24 13:50:01 -0700493 return -ENOMEM;
494
David Herrmann41c4a462014-07-29 17:14:17 +0200495 uhid->rd_size = rd_size;
496 uhid->rd_data = rd_data;
497
Petri Gynther45226432014-03-24 13:50:01 -0700498 hid = hid_allocate_device();
499 if (IS_ERR(hid)) {
500 ret = PTR_ERR(hid);
501 goto err_free;
502 }
503
David Herrmann25be7fe2014-07-29 17:14:18 +0200504 len = min(sizeof(hid->name), sizeof(ev->u.create2.name)) - 1;
505 strncpy(hid->name, ev->u.create2.name, len);
506 len = min(sizeof(hid->phys), sizeof(ev->u.create2.phys)) - 1;
507 strncpy(hid->phys, ev->u.create2.phys, len);
508 len = min(sizeof(hid->uniq), sizeof(ev->u.create2.uniq)) - 1;
509 strncpy(hid->uniq, ev->u.create2.uniq, len);
Petri Gynther45226432014-03-24 13:50:01 -0700510
511 hid->ll_driver = &uhid_hid_driver;
512 hid->bus = ev->u.create2.bus;
513 hid->vendor = ev->u.create2.vendor;
514 hid->product = ev->u.create2.product;
515 hid->version = ev->u.create2.version;
516 hid->country = ev->u.create2.country;
517 hid->driver_data = uhid;
518 hid->dev.parent = uhid_misc.this_device;
519
520 uhid->hid = hid;
521 uhid->running = true;
522
Roderick Colenbrander67f8ecc2016-05-18 13:11:09 -0700523 /* Adding of a HID device is done through a worker, to allow HID drivers
524 * which use feature requests during .probe to work, without they would
525 * be blocked on devlock, which is held by uhid_char_write.
526 */
527 schedule_work(&uhid->worker);
Petri Gynther45226432014-03-24 13:50:01 -0700528
529 return 0;
530
Petri Gynther45226432014-03-24 13:50:01 -0700531err_free:
532 kfree(uhid->rd_data);
David Herrmann41c4a462014-07-29 17:14:17 +0200533 uhid->rd_data = NULL;
534 uhid->rd_size = 0;
Petri Gynther45226432014-03-24 13:50:01 -0700535 return ret;
536}
537
David Herrmann56c47752014-07-29 17:14:16 +0200538static int uhid_dev_create(struct uhid_device *uhid,
539 struct uhid_event *ev)
540{
541 struct uhid_create_req orig;
542
543 orig = ev->u.create;
544
545 if (orig.rd_size <= 0 || orig.rd_size > HID_MAX_DESCRIPTOR_SIZE)
546 return -EINVAL;
547 if (copy_from_user(&ev->u.create2.rd_data, orig.rd_data, orig.rd_size))
548 return -EFAULT;
549
550 memcpy(ev->u.create2.name, orig.name, sizeof(orig.name));
551 memcpy(ev->u.create2.phys, orig.phys, sizeof(orig.phys));
552 memcpy(ev->u.create2.uniq, orig.uniq, sizeof(orig.uniq));
553 ev->u.create2.rd_size = orig.rd_size;
554 ev->u.create2.bus = orig.bus;
555 ev->u.create2.vendor = orig.vendor;
556 ev->u.create2.product = orig.product;
557 ev->u.create2.version = orig.version;
558 ev->u.create2.country = orig.country;
559
560 return uhid_dev_create2(uhid, ev);
561}
562
David Herrmannd365c6c2012-06-10 15:16:18 +0200563static int uhid_dev_destroy(struct uhid_device *uhid)
564{
565 if (!uhid->running)
566 return -EINVAL;
567
568 uhid->running = false;
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200569 wake_up_interruptible(&uhid->report_wait);
David Herrmannd365c6c2012-06-10 15:16:18 +0200570
Roderick Colenbrander67f8ecc2016-05-18 13:11:09 -0700571 cancel_work_sync(&uhid->worker);
572
David Herrmannd365c6c2012-06-10 15:16:18 +0200573 hid_destroy_device(uhid->hid);
574 kfree(uhid->rd_data);
575
576 return 0;
577}
578
David Herrmann5e87a362012-06-10 15:16:19 +0200579static int uhid_dev_input(struct uhid_device *uhid, struct uhid_event *ev)
580{
581 if (!uhid->running)
582 return -EINVAL;
583
584 hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input.data,
585 min_t(size_t, ev->u.input.size, UHID_DATA_MAX), 0);
586
587 return 0;
588}
589
Petri Gynther45226432014-03-24 13:50:01 -0700590static int uhid_dev_input2(struct uhid_device *uhid, struct uhid_event *ev)
591{
592 if (!uhid->running)
593 return -EINVAL;
594
595 hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input2.data,
596 min_t(size_t, ev->u.input2.size, UHID_DATA_MAX), 0);
597
598 return 0;
599}
600
David Herrmannfa71f322014-07-29 17:14:21 +0200601static int uhid_dev_get_report_reply(struct uhid_device *uhid,
602 struct uhid_event *ev)
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200603{
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200604 if (!uhid->running)
605 return -EINVAL;
606
David Herrmann11c22152014-07-29 17:14:24 +0200607 uhid_report_wake_up(uhid, ev->u.get_report_reply.id, ev);
608 return 0;
609}
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200610
David Herrmann11c22152014-07-29 17:14:24 +0200611static int uhid_dev_set_report_reply(struct uhid_device *uhid,
612 struct uhid_event *ev)
613{
614 if (!uhid->running)
615 return -EINVAL;
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200616
David Herrmann11c22152014-07-29 17:14:24 +0200617 uhid_report_wake_up(uhid, ev->u.set_report_reply.id, ev);
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200618 return 0;
619}
620
David Herrmann1ccd7a22012-06-10 15:16:13 +0200621static int uhid_char_open(struct inode *inode, struct file *file)
622{
David Herrmannace3d862012-06-10 15:16:14 +0200623 struct uhid_device *uhid;
624
625 uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
626 if (!uhid)
627 return -ENOMEM;
628
David Herrmannd937ae52012-06-10 15:16:16 +0200629 mutex_init(&uhid->devlock);
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200630 mutex_init(&uhid->report_lock);
David Herrmannace3d862012-06-10 15:16:14 +0200631 spin_lock_init(&uhid->qlock);
632 init_waitqueue_head(&uhid->waitq);
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200633 init_waitqueue_head(&uhid->report_wait);
David Herrmannd365c6c2012-06-10 15:16:18 +0200634 uhid->running = false;
Roderick Colenbrander67f8ecc2016-05-18 13:11:09 -0700635 INIT_WORK(&uhid->worker, uhid_device_add_worker);
David Herrmannace3d862012-06-10 15:16:14 +0200636
637 file->private_data = uhid;
638 nonseekable_open(inode, file);
639
David Herrmann1ccd7a22012-06-10 15:16:13 +0200640 return 0;
641}
642
643static int uhid_char_release(struct inode *inode, struct file *file)
644{
David Herrmannace3d862012-06-10 15:16:14 +0200645 struct uhid_device *uhid = file->private_data;
646 unsigned int i;
647
David Herrmannd365c6c2012-06-10 15:16:18 +0200648 uhid_dev_destroy(uhid);
649
David Herrmannace3d862012-06-10 15:16:14 +0200650 for (i = 0; i < UHID_BUFSIZE; ++i)
651 kfree(uhid->outq[i]);
652
653 kfree(uhid);
654
David Herrmann1ccd7a22012-06-10 15:16:13 +0200655 return 0;
656}
657
658static ssize_t uhid_char_read(struct file *file, char __user *buffer,
659 size_t count, loff_t *ppos)
660{
David Herrmannd937ae52012-06-10 15:16:16 +0200661 struct uhid_device *uhid = file->private_data;
662 int ret;
663 unsigned long flags;
664 size_t len;
665
666 /* they need at least the "type" member of uhid_event */
667 if (count < sizeof(__u32))
668 return -EINVAL;
669
670try_again:
671 if (file->f_flags & O_NONBLOCK) {
672 if (uhid->head == uhid->tail)
673 return -EAGAIN;
674 } else {
675 ret = wait_event_interruptible(uhid->waitq,
676 uhid->head != uhid->tail);
677 if (ret)
678 return ret;
679 }
680
681 ret = mutex_lock_interruptible(&uhid->devlock);
682 if (ret)
683 return ret;
684
685 if (uhid->head == uhid->tail) {
686 mutex_unlock(&uhid->devlock);
687 goto try_again;
688 } else {
689 len = min(count, sizeof(**uhid->outq));
Vinicius Costa Gomesadefb692012-07-14 18:59:25 -0300690 if (copy_to_user(buffer, uhid->outq[uhid->tail], len)) {
David Herrmannd937ae52012-06-10 15:16:16 +0200691 ret = -EFAULT;
692 } else {
693 kfree(uhid->outq[uhid->tail]);
694 uhid->outq[uhid->tail] = NULL;
695
696 spin_lock_irqsave(&uhid->qlock, flags);
697 uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE;
698 spin_unlock_irqrestore(&uhid->qlock, flags);
699 }
700 }
701
702 mutex_unlock(&uhid->devlock);
703 return ret ? ret : len;
David Herrmann1ccd7a22012-06-10 15:16:13 +0200704}
705
706static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
707 size_t count, loff_t *ppos)
708{
David Herrmann6664ef72012-06-10 15:16:17 +0200709 struct uhid_device *uhid = file->private_data;
710 int ret;
711 size_t len;
712
713 /* we need at least the "type" member of uhid_event */
714 if (count < sizeof(__u32))
715 return -EINVAL;
716
717 ret = mutex_lock_interruptible(&uhid->devlock);
718 if (ret)
719 return ret;
720
721 memset(&uhid->input_buf, 0, sizeof(uhid->input_buf));
722 len = min(count, sizeof(uhid->input_buf));
Dmitry Torokhovbefde022013-02-18 11:26:11 +0100723
724 ret = uhid_event_from_user(buffer, len, &uhid->input_buf);
725 if (ret)
David Herrmann6664ef72012-06-10 15:16:17 +0200726 goto unlock;
David Herrmann6664ef72012-06-10 15:16:17 +0200727
728 switch (uhid->input_buf.type) {
David Herrmannd365c6c2012-06-10 15:16:18 +0200729 case UHID_CREATE:
Eric Biggersab26f7f2018-11-14 13:55:09 -0800730 /*
731 * 'struct uhid_create_req' contains a __user pointer which is
732 * copied from, so it's unsafe to allow this with elevated
733 * privileges (e.g. from a setuid binary) or via kernel_write().
734 */
735 if (file->f_cred != current_cred() || uaccess_kernel()) {
736 pr_err_once("UHID_CREATE from different security context by process %d (%s), this is not allowed.\n",
737 task_tgid_vnr(current), current->comm);
738 ret = -EACCES;
739 goto unlock;
740 }
David Herrmannd365c6c2012-06-10 15:16:18 +0200741 ret = uhid_dev_create(uhid, &uhid->input_buf);
742 break;
Petri Gynther45226432014-03-24 13:50:01 -0700743 case UHID_CREATE2:
744 ret = uhid_dev_create2(uhid, &uhid->input_buf);
745 break;
David Herrmannd365c6c2012-06-10 15:16:18 +0200746 case UHID_DESTROY:
747 ret = uhid_dev_destroy(uhid);
748 break;
David Herrmann5e87a362012-06-10 15:16:19 +0200749 case UHID_INPUT:
750 ret = uhid_dev_input(uhid, &uhid->input_buf);
751 break;
Petri Gynther45226432014-03-24 13:50:01 -0700752 case UHID_INPUT2:
753 ret = uhid_dev_input2(uhid, &uhid->input_buf);
754 break;
David Herrmannfa71f322014-07-29 17:14:21 +0200755 case UHID_GET_REPORT_REPLY:
756 ret = uhid_dev_get_report_reply(uhid, &uhid->input_buf);
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200757 break;
David Herrmann11c22152014-07-29 17:14:24 +0200758 case UHID_SET_REPORT_REPLY:
759 ret = uhid_dev_set_report_reply(uhid, &uhid->input_buf);
760 break;
David Herrmann6664ef72012-06-10 15:16:17 +0200761 default:
762 ret = -EOPNOTSUPP;
763 }
764
765unlock:
766 mutex_unlock(&uhid->devlock);
767
768 /* return "count" not "len" to not confuse the caller */
769 return ret ? ret : count;
David Herrmann1ccd7a22012-06-10 15:16:13 +0200770}
771
772static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
773{
David Herrmann1f9dec12012-06-10 15:16:15 +0200774 struct uhid_device *uhid = file->private_data;
Jiri Kosinaf6d01872020-01-10 15:32:51 +0100775 unsigned int mask = POLLOUT | POLLWRNORM; /* uhid is always writable */
David Herrmann1f9dec12012-06-10 15:16:15 +0200776
777 poll_wait(file, &uhid->waitq, wait);
778
779 if (uhid->head != uhid->tail)
Jiri Kosinaf6d01872020-01-10 15:32:51 +0100780 mask |= POLLIN | POLLRDNORM;
David Herrmann1f9dec12012-06-10 15:16:15 +0200781
Jiri Kosinaf6d01872020-01-10 15:32:51 +0100782 return mask;
David Herrmann1ccd7a22012-06-10 15:16:13 +0200783}
784
785static const struct file_operations uhid_fops = {
786 .owner = THIS_MODULE,
787 .open = uhid_char_open,
788 .release = uhid_char_release,
789 .read = uhid_char_read,
790 .write = uhid_char_write,
791 .poll = uhid_char_poll,
792 .llseek = no_llseek,
793};
794
795static struct miscdevice uhid_misc = {
796 .fops = &uhid_fops,
David Herrmann19872d22013-09-09 18:33:54 +0200797 .minor = UHID_MINOR,
David Herrmann1ccd7a22012-06-10 15:16:13 +0200798 .name = UHID_NAME,
799};
PrasannaKumar Muralidharanca75d602016-08-25 22:30:49 +0530800module_misc_device(uhid_misc);
David Herrmann1ccd7a22012-06-10 15:16:13 +0200801
David Herrmann1ccd7a22012-06-10 15:16:13 +0200802MODULE_LICENSE("GPL");
803MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
804MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");
David Herrmann19872d22013-09-09 18:33:54 +0200805MODULE_ALIAS_MISCDEV(UHID_MINOR);
Marcel Holtmann60cbd532013-09-01 11:02:46 -0700806MODULE_ALIAS("devname:" UHID_NAME);