blob: 19511481a7d3a932e3306497d50616832ade72e6 [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>
David Herrmann1ccd7a22012-06-10 15:16:13 +020015#include <linux/device.h>
16#include <linux/fs.h>
17#include <linux/hid.h>
18#include <linux/input.h>
19#include <linux/miscdevice.h>
20#include <linux/module.h>
21#include <linux/mutex.h>
22#include <linux/poll.h>
23#include <linux/sched.h>
24#include <linux/spinlock.h>
25#include <linux/uhid.h>
26#include <linux/wait.h>
27
28#define UHID_NAME "uhid"
David Herrmannace3d862012-06-10 15:16:14 +020029#define UHID_BUFSIZE 32
30
31struct uhid_device {
David Herrmannd937ae52012-06-10 15:16:16 +020032 struct mutex devlock;
David Herrmannd365c6c2012-06-10 15:16:18 +020033 bool running;
34
35 __u8 *rd_data;
36 uint rd_size;
37
David Herrmannace3d862012-06-10 15:16:14 +020038 struct hid_device *hid;
David Herrmann6664ef72012-06-10 15:16:17 +020039 struct uhid_event input_buf;
David Herrmannace3d862012-06-10 15:16:14 +020040
41 wait_queue_head_t waitq;
42 spinlock_t qlock;
43 __u8 head;
44 __u8 tail;
45 struct uhid_event *outq[UHID_BUFSIZE];
David Herrmannfcfcf0d2012-06-10 15:16:25 +020046
David Herrmann8cad5b02014-07-29 17:14:19 +020047 /* blocking GET_REPORT support; state changes protected by qlock */
David Herrmannfcfcf0d2012-06-10 15:16:25 +020048 struct mutex report_lock;
49 wait_queue_head_t report_wait;
David Herrmann5942b842014-07-29 17:14:20 +020050 bool report_running;
David Herrmann8cad5b02014-07-29 17:14:19 +020051 u32 report_id;
David Herrmann11c22152014-07-29 17:14:24 +020052 u32 report_type;
David Herrmannfcfcf0d2012-06-10 15:16:25 +020053 struct uhid_event report_buf;
David Herrmannace3d862012-06-10 15:16:14 +020054};
David Herrmann1ccd7a22012-06-10 15:16:13 +020055
56static struct miscdevice uhid_misc;
57
David Herrmannace3d862012-06-10 15:16:14 +020058static void uhid_queue(struct uhid_device *uhid, struct uhid_event *ev)
59{
60 __u8 newhead;
61
62 newhead = (uhid->head + 1) % UHID_BUFSIZE;
63
64 if (newhead != uhid->tail) {
65 uhid->outq[uhid->head] = ev;
66 uhid->head = newhead;
67 wake_up_interruptible(&uhid->waitq);
68 } else {
69 hid_warn(uhid->hid, "Output queue is full\n");
70 kfree(ev);
71 }
72}
73
74static int uhid_queue_event(struct uhid_device *uhid, __u32 event)
75{
76 unsigned long flags;
77 struct uhid_event *ev;
78
79 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
80 if (!ev)
81 return -ENOMEM;
82
83 ev->type = event;
84
85 spin_lock_irqsave(&uhid->qlock, flags);
86 uhid_queue(uhid, ev);
87 spin_unlock_irqrestore(&uhid->qlock, flags);
88
89 return 0;
90}
91
David Herrmannd365c6c2012-06-10 15:16:18 +020092static int uhid_hid_start(struct hid_device *hid)
93{
David Herrmannec4b7de2012-06-10 15:16:21 +020094 struct uhid_device *uhid = hid->driver_data;
95
96 return uhid_queue_event(uhid, UHID_START);
David Herrmannd365c6c2012-06-10 15:16:18 +020097}
98
99static void uhid_hid_stop(struct hid_device *hid)
100{
David Herrmannec4b7de2012-06-10 15:16:21 +0200101 struct uhid_device *uhid = hid->driver_data;
102
103 hid->claimed = 0;
104 uhid_queue_event(uhid, UHID_STOP);
David Herrmannd365c6c2012-06-10 15:16:18 +0200105}
106
107static int uhid_hid_open(struct hid_device *hid)
108{
David Herrmanne7191472012-06-10 15:16:22 +0200109 struct uhid_device *uhid = hid->driver_data;
110
111 return uhid_queue_event(uhid, UHID_OPEN);
David Herrmannd365c6c2012-06-10 15:16:18 +0200112}
113
114static void uhid_hid_close(struct hid_device *hid)
115{
David Herrmanne7191472012-06-10 15:16:22 +0200116 struct uhid_device *uhid = hid->driver_data;
117
118 uhid_queue_event(uhid, UHID_CLOSE);
David Herrmannd365c6c2012-06-10 15:16:18 +0200119}
120
David Herrmannd365c6c2012-06-10 15:16:18 +0200121static int uhid_hid_parse(struct hid_device *hid)
122{
David Herrmann037c0612012-06-10 15:16:20 +0200123 struct uhid_device *uhid = hid->driver_data;
124
125 return hid_parse_report(hid, uhid->rd_data, uhid->rd_size);
David Herrmannd365c6c2012-06-10 15:16:18 +0200126}
127
David Herrmann11c22152014-07-29 17:14:24 +0200128/* must be called with report_lock held */
129static int __uhid_report_queue_and_wait(struct uhid_device *uhid,
130 struct uhid_event *ev,
131 __u32 *report_id)
Jiri Kosina289a7162014-02-17 14:49:34 +0100132{
Jiri Kosina289a7162014-02-17 14:49:34 +0100133 unsigned long flags;
134 int ret;
Jiri Kosina289a7162014-02-17 14:49:34 +0100135
136 spin_lock_irqsave(&uhid->qlock, flags);
David Herrmann11c22152014-07-29 17:14:24 +0200137 *report_id = ++uhid->report_id;
138 uhid->report_type = ev->type;
David Herrmann5942b842014-07-29 17:14:20 +0200139 uhid->report_running = true;
Jiri Kosina289a7162014-02-17 14:49:34 +0100140 uhid_queue(uhid, ev);
141 spin_unlock_irqrestore(&uhid->qlock, flags);
142
143 ret = wait_event_interruptible_timeout(uhid->report_wait,
David Herrmann5942b842014-07-29 17:14:20 +0200144 !uhid->report_running || !uhid->running,
145 5 * HZ);
David Herrmann11c22152014-07-29 17:14:24 +0200146 if (!ret || !uhid->running || uhid->report_running)
Jiri Kosina289a7162014-02-17 14:49:34 +0100147 ret = -EIO;
David Herrmann11c22152014-07-29 17:14:24 +0200148 else if (ret < 0)
Jiri Kosina289a7162014-02-17 14:49:34 +0100149 ret = -ERESTARTSYS;
David Herrmann11c22152014-07-29 17:14:24 +0200150 else
151 ret = 0;
Jiri Kosina289a7162014-02-17 14:49:34 +0100152
David Herrmann5942b842014-07-29 17:14:20 +0200153 uhid->report_running = false;
Jiri Kosina289a7162014-02-17 14:49:34 +0100154
David Herrmann11c22152014-07-29 17:14:24 +0200155 return ret;
156}
157
158static void uhid_report_wake_up(struct uhid_device *uhid, u32 id,
159 const struct uhid_event *ev)
160{
161 unsigned long flags;
162
163 spin_lock_irqsave(&uhid->qlock, flags);
164
165 /* id for old report; drop it silently */
166 if (uhid->report_type != ev->type || uhid->report_id != id)
167 goto unlock;
168 if (!uhid->report_running)
169 goto unlock;
170
171 memcpy(&uhid->report_buf, ev, sizeof(*ev));
172 uhid->report_running = false;
173 wake_up_interruptible(&uhid->report_wait);
174
175unlock:
176 spin_unlock_irqrestore(&uhid->qlock, flags);
177}
178
179static int uhid_hid_get_report(struct hid_device *hid, unsigned char rnum,
180 u8 *buf, size_t count, u8 rtype)
181{
182 struct uhid_device *uhid = hid->driver_data;
183 struct uhid_get_report_reply_req *req;
184 struct uhid_event *ev;
185 int ret;
186
187 if (!uhid->running)
188 return -EIO;
189
190 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
191 if (!ev)
192 return -ENOMEM;
193
194 ev->type = UHID_GET_REPORT;
195 ev->u.get_report.rnum = rnum;
196 ev->u.get_report.rtype = rtype;
197
198 ret = mutex_lock_interruptible(&uhid->report_lock);
199 if (ret) {
200 kfree(ev);
201 return ret;
202 }
203
204 /* this _always_ takes ownership of @ev */
205 ret = __uhid_report_queue_and_wait(uhid, ev, &ev->u.get_report.id);
206 if (ret)
207 goto unlock;
208
209 req = &uhid->report_buf.u.get_report_reply;
210 if (req->err) {
211 ret = -EIO;
212 } else {
213 ret = min3(count, (size_t)req->size, (size_t)UHID_DATA_MAX);
214 memcpy(buf, req->data, ret);
215 }
216
Jiri Kosina289a7162014-02-17 14:49:34 +0100217unlock:
218 mutex_unlock(&uhid->report_lock);
David Herrmann11c22152014-07-29 17:14:24 +0200219 return ret;
220}
221
222static int uhid_hid_set_report(struct hid_device *hid, unsigned char rnum,
223 const u8 *buf, size_t count, u8 rtype)
224{
225 struct uhid_device *uhid = hid->driver_data;
226 struct uhid_event *ev;
227 int ret;
228
229 if (!uhid->running || count > UHID_DATA_MAX)
230 return -EIO;
231
232 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
233 if (!ev)
234 return -ENOMEM;
235
236 ev->type = UHID_SET_REPORT;
237 ev->u.set_report.rnum = rnum;
238 ev->u.set_report.rtype = rtype;
239 ev->u.set_report.size = count;
240 memcpy(ev->u.set_report.data, buf, count);
241
242 ret = mutex_lock_interruptible(&uhid->report_lock);
243 if (ret) {
244 kfree(ev);
245 return ret;
246 }
247
248 /* this _always_ takes ownership of @ev */
249 ret = __uhid_report_queue_and_wait(uhid, ev, &ev->u.set_report.id);
250 if (ret)
251 goto unlock;
252
253 if (uhid->report_buf.u.set_report_reply.err)
254 ret = -EIO;
255 else
256 ret = count;
257
258unlock:
259 mutex_unlock(&uhid->report_lock);
260 return ret;
Jiri Kosina289a7162014-02-17 14:49:34 +0100261}
262
David Herrmann7c4003b2014-07-29 17:14:23 +0200263static int uhid_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
264 __u8 *buf, size_t len, unsigned char rtype,
265 int reqtype)
266{
David Herrmann11c22152014-07-29 17:14:24 +0200267 u8 u_rtype;
268
269 switch (rtype) {
270 case HID_FEATURE_REPORT:
271 u_rtype = UHID_FEATURE_REPORT;
272 break;
273 case HID_OUTPUT_REPORT:
274 u_rtype = UHID_OUTPUT_REPORT;
275 break;
276 case HID_INPUT_REPORT:
277 u_rtype = UHID_INPUT_REPORT;
278 break;
279 default:
280 return -EINVAL;
281 }
282
David Herrmann7c4003b2014-07-29 17:14:23 +0200283 switch (reqtype) {
284 case HID_REQ_GET_REPORT:
David Herrmann11c22152014-07-29 17:14:24 +0200285 return uhid_hid_get_report(hid, reportnum, buf, len, u_rtype);
David Herrmann7c4003b2014-07-29 17:14:23 +0200286 case HID_REQ_SET_REPORT:
David Herrmann11c22152014-07-29 17:14:24 +0200287 return uhid_hid_set_report(hid, reportnum, buf, len, u_rtype);
David Herrmann7c4003b2014-07-29 17:14:23 +0200288 default:
289 return -EIO;
290 }
291}
292
David Herrmannd365c6c2012-06-10 15:16:18 +0200293static int uhid_hid_output_raw(struct hid_device *hid, __u8 *buf, size_t count,
294 unsigned char report_type)
295{
David Herrmann3b3baa82012-06-10 15:16:24 +0200296 struct uhid_device *uhid = hid->driver_data;
297 __u8 rtype;
298 unsigned long flags;
299 struct uhid_event *ev;
300
301 switch (report_type) {
302 case HID_FEATURE_REPORT:
303 rtype = UHID_FEATURE_REPORT;
304 break;
305 case HID_OUTPUT_REPORT:
306 rtype = UHID_OUTPUT_REPORT;
307 break;
308 default:
309 return -EINVAL;
310 }
311
312 if (count < 1 || count > UHID_DATA_MAX)
313 return -EINVAL;
314
315 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
316 if (!ev)
317 return -ENOMEM;
318
319 ev->type = UHID_OUTPUT;
320 ev->u.output.size = count;
321 ev->u.output.rtype = rtype;
322 memcpy(ev->u.output.data, buf, count);
323
324 spin_lock_irqsave(&uhid->qlock, flags);
325 uhid_queue(uhid, ev);
326 spin_unlock_irqrestore(&uhid->qlock, flags);
327
328 return count;
David Herrmannd365c6c2012-06-10 15:16:18 +0200329}
330
Frank Praznik596cfdd2014-01-22 13:49:43 -0500331static int uhid_hid_output_report(struct hid_device *hid, __u8 *buf,
332 size_t count)
333{
Benjamin Tissoires41abfb32014-02-10 12:58:46 -0500334 return uhid_hid_output_raw(hid, buf, count, HID_OUTPUT_REPORT);
Frank Praznik596cfdd2014-01-22 13:49:43 -0500335}
336
David Herrmannd365c6c2012-06-10 15:16:18 +0200337static struct hid_ll_driver uhid_hid_driver = {
338 .start = uhid_hid_start,
339 .stop = uhid_hid_stop,
340 .open = uhid_hid_open,
341 .close = uhid_hid_close,
David Herrmannd365c6c2012-06-10 15:16:18 +0200342 .parse = uhid_hid_parse,
David Herrmann7c4003b2014-07-29 17:14:23 +0200343 .raw_request = uhid_hid_raw_request,
Frank Praznik596cfdd2014-01-22 13:49:43 -0500344 .output_report = uhid_hid_output_report,
David Herrmannd365c6c2012-06-10 15:16:18 +0200345};
346
Dmitry Torokhovbefde022013-02-18 11:26:11 +0100347#ifdef CONFIG_COMPAT
348
349/* Apparently we haven't stepped on these rakes enough times yet. */
350struct uhid_create_req_compat {
351 __u8 name[128];
352 __u8 phys[64];
353 __u8 uniq[64];
354
355 compat_uptr_t rd_data;
356 __u16 rd_size;
357
358 __u16 bus;
359 __u32 vendor;
360 __u32 product;
361 __u32 version;
362 __u32 country;
363} __attribute__((__packed__));
364
365static int uhid_event_from_user(const char __user *buffer, size_t len,
366 struct uhid_event *event)
367{
368 if (is_compat_task()) {
369 u32 type;
370
371 if (get_user(type, buffer))
372 return -EFAULT;
373
374 if (type == UHID_CREATE) {
375 /*
376 * This is our messed up request with compat pointer.
377 * It is largish (more than 256 bytes) so we better
378 * allocate it from the heap.
379 */
380 struct uhid_create_req_compat *compat;
381
David Herrmann80897aa2013-11-26 13:58:18 +0100382 compat = kzalloc(sizeof(*compat), GFP_KERNEL);
Dmitry Torokhovbefde022013-02-18 11:26:11 +0100383 if (!compat)
384 return -ENOMEM;
385
386 buffer += sizeof(type);
387 len -= sizeof(type);
388 if (copy_from_user(compat, buffer,
389 min(len, sizeof(*compat)))) {
390 kfree(compat);
391 return -EFAULT;
392 }
393
394 /* Shuffle the data over to proper structure */
395 event->type = type;
396
397 memcpy(event->u.create.name, compat->name,
398 sizeof(compat->name));
399 memcpy(event->u.create.phys, compat->phys,
400 sizeof(compat->phys));
401 memcpy(event->u.create.uniq, compat->uniq,
402 sizeof(compat->uniq));
403
404 event->u.create.rd_data = compat_ptr(compat->rd_data);
405 event->u.create.rd_size = compat->rd_size;
406
407 event->u.create.bus = compat->bus;
408 event->u.create.vendor = compat->vendor;
409 event->u.create.product = compat->product;
410 event->u.create.version = compat->version;
411 event->u.create.country = compat->country;
412
413 kfree(compat);
414 return 0;
415 }
416 /* All others can be copied directly */
417 }
418
419 if (copy_from_user(event, buffer, min(len, sizeof(*event))))
420 return -EFAULT;
421
422 return 0;
423}
424#else
425static int uhid_event_from_user(const char __user *buffer, size_t len,
426 struct uhid_event *event)
427{
428 if (copy_from_user(event, buffer, min(len, sizeof(*event))))
429 return -EFAULT;
430
431 return 0;
432}
433#endif
434
Petri Gynther45226432014-03-24 13:50:01 -0700435static int uhid_dev_create2(struct uhid_device *uhid,
436 const struct uhid_event *ev)
437{
438 struct hid_device *hid;
David Herrmann25be7fe2014-07-29 17:14:18 +0200439 size_t rd_size, len;
David Herrmann41c4a4642014-07-29 17:14:17 +0200440 void *rd_data;
Petri Gynther45226432014-03-24 13:50:01 -0700441 int ret;
442
443 if (uhid->running)
444 return -EALREADY;
445
David Herrmann41c4a4642014-07-29 17:14:17 +0200446 rd_size = ev->u.create2.rd_size;
447 if (rd_size <= 0 || rd_size > HID_MAX_DESCRIPTOR_SIZE)
Petri Gynther45226432014-03-24 13:50:01 -0700448 return -EINVAL;
449
David Herrmann41c4a4642014-07-29 17:14:17 +0200450 rd_data = kmemdup(ev->u.create2.rd_data, rd_size, GFP_KERNEL);
451 if (!rd_data)
Petri Gynther45226432014-03-24 13:50:01 -0700452 return -ENOMEM;
453
David Herrmann41c4a4642014-07-29 17:14:17 +0200454 uhid->rd_size = rd_size;
455 uhid->rd_data = rd_data;
456
Petri Gynther45226432014-03-24 13:50:01 -0700457 hid = hid_allocate_device();
458 if (IS_ERR(hid)) {
459 ret = PTR_ERR(hid);
460 goto err_free;
461 }
462
David Herrmann25be7fe2014-07-29 17:14:18 +0200463 len = min(sizeof(hid->name), sizeof(ev->u.create2.name)) - 1;
464 strncpy(hid->name, ev->u.create2.name, len);
465 len = min(sizeof(hid->phys), sizeof(ev->u.create2.phys)) - 1;
466 strncpy(hid->phys, ev->u.create2.phys, len);
467 len = min(sizeof(hid->uniq), sizeof(ev->u.create2.uniq)) - 1;
468 strncpy(hid->uniq, ev->u.create2.uniq, len);
Petri Gynther45226432014-03-24 13:50:01 -0700469
470 hid->ll_driver = &uhid_hid_driver;
471 hid->bus = ev->u.create2.bus;
472 hid->vendor = ev->u.create2.vendor;
473 hid->product = ev->u.create2.product;
474 hid->version = ev->u.create2.version;
475 hid->country = ev->u.create2.country;
476 hid->driver_data = uhid;
477 hid->dev.parent = uhid_misc.this_device;
478
479 uhid->hid = hid;
480 uhid->running = true;
481
482 ret = hid_add_device(hid);
483 if (ret) {
484 hid_err(hid, "Cannot register HID device\n");
485 goto err_hid;
486 }
487
488 return 0;
489
490err_hid:
491 hid_destroy_device(hid);
492 uhid->hid = NULL;
493 uhid->running = false;
494err_free:
495 kfree(uhid->rd_data);
David Herrmann41c4a4642014-07-29 17:14:17 +0200496 uhid->rd_data = NULL;
497 uhid->rd_size = 0;
Petri Gynther45226432014-03-24 13:50:01 -0700498 return ret;
499}
500
David Herrmann56c47752014-07-29 17:14:16 +0200501static int uhid_dev_create(struct uhid_device *uhid,
502 struct uhid_event *ev)
503{
504 struct uhid_create_req orig;
505
506 orig = ev->u.create;
507
508 if (orig.rd_size <= 0 || orig.rd_size > HID_MAX_DESCRIPTOR_SIZE)
509 return -EINVAL;
510 if (copy_from_user(&ev->u.create2.rd_data, orig.rd_data, orig.rd_size))
511 return -EFAULT;
512
513 memcpy(ev->u.create2.name, orig.name, sizeof(orig.name));
514 memcpy(ev->u.create2.phys, orig.phys, sizeof(orig.phys));
515 memcpy(ev->u.create2.uniq, orig.uniq, sizeof(orig.uniq));
516 ev->u.create2.rd_size = orig.rd_size;
517 ev->u.create2.bus = orig.bus;
518 ev->u.create2.vendor = orig.vendor;
519 ev->u.create2.product = orig.product;
520 ev->u.create2.version = orig.version;
521 ev->u.create2.country = orig.country;
522
523 return uhid_dev_create2(uhid, ev);
524}
525
David Herrmannd365c6c2012-06-10 15:16:18 +0200526static int uhid_dev_destroy(struct uhid_device *uhid)
527{
528 if (!uhid->running)
529 return -EINVAL;
530
531 uhid->running = false;
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200532 wake_up_interruptible(&uhid->report_wait);
David Herrmannd365c6c2012-06-10 15:16:18 +0200533
534 hid_destroy_device(uhid->hid);
535 kfree(uhid->rd_data);
536
537 return 0;
538}
539
David Herrmann5e87a362012-06-10 15:16:19 +0200540static int uhid_dev_input(struct uhid_device *uhid, struct uhid_event *ev)
541{
542 if (!uhid->running)
543 return -EINVAL;
544
545 hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input.data,
546 min_t(size_t, ev->u.input.size, UHID_DATA_MAX), 0);
547
548 return 0;
549}
550
Petri Gynther45226432014-03-24 13:50:01 -0700551static int uhid_dev_input2(struct uhid_device *uhid, struct uhid_event *ev)
552{
553 if (!uhid->running)
554 return -EINVAL;
555
556 hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input2.data,
557 min_t(size_t, ev->u.input2.size, UHID_DATA_MAX), 0);
558
559 return 0;
560}
561
David Herrmannfa71f322014-07-29 17:14:21 +0200562static int uhid_dev_get_report_reply(struct uhid_device *uhid,
563 struct uhid_event *ev)
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200564{
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200565 if (!uhid->running)
566 return -EINVAL;
567
David Herrmann11c22152014-07-29 17:14:24 +0200568 uhid_report_wake_up(uhid, ev->u.get_report_reply.id, ev);
569 return 0;
570}
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200571
David Herrmann11c22152014-07-29 17:14:24 +0200572static int uhid_dev_set_report_reply(struct uhid_device *uhid,
573 struct uhid_event *ev)
574{
575 if (!uhid->running)
576 return -EINVAL;
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200577
David Herrmann11c22152014-07-29 17:14:24 +0200578 uhid_report_wake_up(uhid, ev->u.set_report_reply.id, ev);
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200579 return 0;
580}
581
David Herrmann1ccd7a22012-06-10 15:16:13 +0200582static int uhid_char_open(struct inode *inode, struct file *file)
583{
David Herrmannace3d862012-06-10 15:16:14 +0200584 struct uhid_device *uhid;
585
586 uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
587 if (!uhid)
588 return -ENOMEM;
589
David Herrmannd937ae52012-06-10 15:16:16 +0200590 mutex_init(&uhid->devlock);
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200591 mutex_init(&uhid->report_lock);
David Herrmannace3d862012-06-10 15:16:14 +0200592 spin_lock_init(&uhid->qlock);
593 init_waitqueue_head(&uhid->waitq);
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200594 init_waitqueue_head(&uhid->report_wait);
David Herrmannd365c6c2012-06-10 15:16:18 +0200595 uhid->running = false;
David Herrmannace3d862012-06-10 15:16:14 +0200596
597 file->private_data = uhid;
598 nonseekable_open(inode, file);
599
David Herrmann1ccd7a22012-06-10 15:16:13 +0200600 return 0;
601}
602
603static int uhid_char_release(struct inode *inode, struct file *file)
604{
David Herrmannace3d862012-06-10 15:16:14 +0200605 struct uhid_device *uhid = file->private_data;
606 unsigned int i;
607
David Herrmannd365c6c2012-06-10 15:16:18 +0200608 uhid_dev_destroy(uhid);
609
David Herrmannace3d862012-06-10 15:16:14 +0200610 for (i = 0; i < UHID_BUFSIZE; ++i)
611 kfree(uhid->outq[i]);
612
613 kfree(uhid);
614
David Herrmann1ccd7a22012-06-10 15:16:13 +0200615 return 0;
616}
617
618static ssize_t uhid_char_read(struct file *file, char __user *buffer,
619 size_t count, loff_t *ppos)
620{
David Herrmannd937ae52012-06-10 15:16:16 +0200621 struct uhid_device *uhid = file->private_data;
622 int ret;
623 unsigned long flags;
624 size_t len;
625
626 /* they need at least the "type" member of uhid_event */
627 if (count < sizeof(__u32))
628 return -EINVAL;
629
630try_again:
631 if (file->f_flags & O_NONBLOCK) {
632 if (uhid->head == uhid->tail)
633 return -EAGAIN;
634 } else {
635 ret = wait_event_interruptible(uhid->waitq,
636 uhid->head != uhid->tail);
637 if (ret)
638 return ret;
639 }
640
641 ret = mutex_lock_interruptible(&uhid->devlock);
642 if (ret)
643 return ret;
644
645 if (uhid->head == uhid->tail) {
646 mutex_unlock(&uhid->devlock);
647 goto try_again;
648 } else {
649 len = min(count, sizeof(**uhid->outq));
Vinicius Costa Gomesadefb692012-07-14 18:59:25 -0300650 if (copy_to_user(buffer, uhid->outq[uhid->tail], len)) {
David Herrmannd937ae52012-06-10 15:16:16 +0200651 ret = -EFAULT;
652 } else {
653 kfree(uhid->outq[uhid->tail]);
654 uhid->outq[uhid->tail] = NULL;
655
656 spin_lock_irqsave(&uhid->qlock, flags);
657 uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE;
658 spin_unlock_irqrestore(&uhid->qlock, flags);
659 }
660 }
661
662 mutex_unlock(&uhid->devlock);
663 return ret ? ret : len;
David Herrmann1ccd7a22012-06-10 15:16:13 +0200664}
665
666static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
667 size_t count, loff_t *ppos)
668{
David Herrmann6664ef72012-06-10 15:16:17 +0200669 struct uhid_device *uhid = file->private_data;
670 int ret;
671 size_t len;
672
673 /* we need at least the "type" member of uhid_event */
674 if (count < sizeof(__u32))
675 return -EINVAL;
676
677 ret = mutex_lock_interruptible(&uhid->devlock);
678 if (ret)
679 return ret;
680
681 memset(&uhid->input_buf, 0, sizeof(uhid->input_buf));
682 len = min(count, sizeof(uhid->input_buf));
Dmitry Torokhovbefde022013-02-18 11:26:11 +0100683
684 ret = uhid_event_from_user(buffer, len, &uhid->input_buf);
685 if (ret)
David Herrmann6664ef72012-06-10 15:16:17 +0200686 goto unlock;
David Herrmann6664ef72012-06-10 15:16:17 +0200687
688 switch (uhid->input_buf.type) {
David Herrmannd365c6c2012-06-10 15:16:18 +0200689 case UHID_CREATE:
690 ret = uhid_dev_create(uhid, &uhid->input_buf);
691 break;
Petri Gynther45226432014-03-24 13:50:01 -0700692 case UHID_CREATE2:
693 ret = uhid_dev_create2(uhid, &uhid->input_buf);
694 break;
David Herrmannd365c6c2012-06-10 15:16:18 +0200695 case UHID_DESTROY:
696 ret = uhid_dev_destroy(uhid);
697 break;
David Herrmann5e87a362012-06-10 15:16:19 +0200698 case UHID_INPUT:
699 ret = uhid_dev_input(uhid, &uhid->input_buf);
700 break;
Petri Gynther45226432014-03-24 13:50:01 -0700701 case UHID_INPUT2:
702 ret = uhid_dev_input2(uhid, &uhid->input_buf);
703 break;
David Herrmannfa71f322014-07-29 17:14:21 +0200704 case UHID_GET_REPORT_REPLY:
705 ret = uhid_dev_get_report_reply(uhid, &uhid->input_buf);
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200706 break;
David Herrmann11c22152014-07-29 17:14:24 +0200707 case UHID_SET_REPORT_REPLY:
708 ret = uhid_dev_set_report_reply(uhid, &uhid->input_buf);
709 break;
David Herrmann6664ef72012-06-10 15:16:17 +0200710 default:
711 ret = -EOPNOTSUPP;
712 }
713
714unlock:
715 mutex_unlock(&uhid->devlock);
716
717 /* return "count" not "len" to not confuse the caller */
718 return ret ? ret : count;
David Herrmann1ccd7a22012-06-10 15:16:13 +0200719}
720
721static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
722{
David Herrmann1f9dec12012-06-10 15:16:15 +0200723 struct uhid_device *uhid = file->private_data;
724
725 poll_wait(file, &uhid->waitq, wait);
726
727 if (uhid->head != uhid->tail)
728 return POLLIN | POLLRDNORM;
729
David Herrmann1ccd7a22012-06-10 15:16:13 +0200730 return 0;
731}
732
733static const struct file_operations uhid_fops = {
734 .owner = THIS_MODULE,
735 .open = uhid_char_open,
736 .release = uhid_char_release,
737 .read = uhid_char_read,
738 .write = uhid_char_write,
739 .poll = uhid_char_poll,
740 .llseek = no_llseek,
741};
742
743static struct miscdevice uhid_misc = {
744 .fops = &uhid_fops,
David Herrmann19872d22013-09-09 18:33:54 +0200745 .minor = UHID_MINOR,
David Herrmann1ccd7a22012-06-10 15:16:13 +0200746 .name = UHID_NAME,
747};
748
749static int __init uhid_init(void)
750{
751 return misc_register(&uhid_misc);
752}
753
754static void __exit uhid_exit(void)
755{
756 misc_deregister(&uhid_misc);
757}
758
759module_init(uhid_init);
760module_exit(uhid_exit);
761MODULE_LICENSE("GPL");
762MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
763MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");
David Herrmann19872d22013-09-09 18:33:54 +0200764MODULE_ALIAS_MISCDEV(UHID_MINOR);
Marcel Holtmann60cbd532013-09-01 11:02:46 -0700765MODULE_ALIAS("devname:" UHID_NAME);