blob: 2d2025a027fed168a139253e4c298c52f6ff85ad [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 Herrmannfcfcf0d2012-06-10 15:16:25 +020052 struct uhid_event report_buf;
David Herrmannace3d862012-06-10 15:16:14 +020053};
David Herrmann1ccd7a22012-06-10 15:16:13 +020054
55static struct miscdevice uhid_misc;
56
David Herrmannace3d862012-06-10 15:16:14 +020057static void uhid_queue(struct uhid_device *uhid, struct uhid_event *ev)
58{
59 __u8 newhead;
60
61 newhead = (uhid->head + 1) % UHID_BUFSIZE;
62
63 if (newhead != uhid->tail) {
64 uhid->outq[uhid->head] = ev;
65 uhid->head = newhead;
66 wake_up_interruptible(&uhid->waitq);
67 } else {
68 hid_warn(uhid->hid, "Output queue is full\n");
69 kfree(ev);
70 }
71}
72
73static int uhid_queue_event(struct uhid_device *uhid, __u32 event)
74{
75 unsigned long flags;
76 struct uhid_event *ev;
77
78 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
79 if (!ev)
80 return -ENOMEM;
81
82 ev->type = event;
83
84 spin_lock_irqsave(&uhid->qlock, flags);
85 uhid_queue(uhid, ev);
86 spin_unlock_irqrestore(&uhid->qlock, flags);
87
88 return 0;
89}
90
David Herrmannd365c6c2012-06-10 15:16:18 +020091static int uhid_hid_start(struct hid_device *hid)
92{
David Herrmannec4b7de2012-06-10 15:16:21 +020093 struct uhid_device *uhid = hid->driver_data;
94
95 return uhid_queue_event(uhid, UHID_START);
David Herrmannd365c6c2012-06-10 15:16:18 +020096}
97
98static void uhid_hid_stop(struct hid_device *hid)
99{
David Herrmannec4b7de2012-06-10 15:16:21 +0200100 struct uhid_device *uhid = hid->driver_data;
101
102 hid->claimed = 0;
103 uhid_queue_event(uhid, UHID_STOP);
David Herrmannd365c6c2012-06-10 15:16:18 +0200104}
105
106static int uhid_hid_open(struct hid_device *hid)
107{
David Herrmanne7191472012-06-10 15:16:22 +0200108 struct uhid_device *uhid = hid->driver_data;
109
110 return uhid_queue_event(uhid, UHID_OPEN);
David Herrmannd365c6c2012-06-10 15:16:18 +0200111}
112
113static void uhid_hid_close(struct hid_device *hid)
114{
David Herrmanne7191472012-06-10 15:16:22 +0200115 struct uhid_device *uhid = hid->driver_data;
116
117 uhid_queue_event(uhid, UHID_CLOSE);
David Herrmannd365c6c2012-06-10 15:16:18 +0200118}
119
David Herrmannd365c6c2012-06-10 15:16:18 +0200120static int uhid_hid_parse(struct hid_device *hid)
121{
David Herrmann037c0612012-06-10 15:16:20 +0200122 struct uhid_device *uhid = hid->driver_data;
123
124 return hid_parse_report(hid, uhid->rd_data, uhid->rd_size);
David Herrmannd365c6c2012-06-10 15:16:18 +0200125}
126
Jiri Kosina289a7162014-02-17 14:49:34 +0100127static int uhid_hid_get_raw(struct hid_device *hid, unsigned char rnum,
128 __u8 *buf, size_t count, unsigned char rtype)
129{
130 struct uhid_device *uhid = hid->driver_data;
131 __u8 report_type;
132 struct uhid_event *ev;
133 unsigned long flags;
134 int ret;
135 size_t uninitialized_var(len);
136 struct uhid_feature_answer_req *req;
137
138 if (!uhid->running)
139 return -EIO;
140
141 switch (rtype) {
142 case HID_FEATURE_REPORT:
143 report_type = UHID_FEATURE_REPORT;
144 break;
145 case HID_OUTPUT_REPORT:
146 report_type = UHID_OUTPUT_REPORT;
147 break;
148 case HID_INPUT_REPORT:
149 report_type = UHID_INPUT_REPORT;
150 break;
151 default:
152 return -EINVAL;
153 }
154
155 ret = mutex_lock_interruptible(&uhid->report_lock);
156 if (ret)
157 return ret;
158
159 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
160 if (!ev) {
161 ret = -ENOMEM;
162 goto unlock;
163 }
164
165 spin_lock_irqsave(&uhid->qlock, flags);
166 ev->type = UHID_FEATURE;
David Herrmann8cad5b02014-07-29 17:14:19 +0200167 ev->u.feature.id = ++uhid->report_id;
Jiri Kosina289a7162014-02-17 14:49:34 +0100168 ev->u.feature.rnum = rnum;
169 ev->u.feature.rtype = report_type;
170
David Herrmann5942b842014-07-29 17:14:20 +0200171 uhid->report_running = true;
Jiri Kosina289a7162014-02-17 14:49:34 +0100172 uhid_queue(uhid, ev);
173 spin_unlock_irqrestore(&uhid->qlock, flags);
174
175 ret = wait_event_interruptible_timeout(uhid->report_wait,
David Herrmann5942b842014-07-29 17:14:20 +0200176 !uhid->report_running || !uhid->running,
177 5 * HZ);
Jiri Kosina289a7162014-02-17 14:49:34 +0100178
Jiri Kosina289a7162014-02-17 14:49:34 +0100179 if (!ret || !uhid->running) {
180 ret = -EIO;
181 } else if (ret < 0) {
182 ret = -ERESTARTSYS;
183 } else {
184 spin_lock_irqsave(&uhid->qlock, flags);
185 req = &uhid->report_buf.u.feature_answer;
186
187 if (req->err) {
188 ret = -EIO;
189 } else {
190 ret = 0;
191 len = min(count,
192 min_t(size_t, req->size, UHID_DATA_MAX));
193 memcpy(buf, req->data, len);
194 }
195
196 spin_unlock_irqrestore(&uhid->qlock, flags);
197 }
198
David Herrmann5942b842014-07-29 17:14:20 +0200199 uhid->report_running = false;
Jiri Kosina289a7162014-02-17 14:49:34 +0100200
201unlock:
202 mutex_unlock(&uhid->report_lock);
203 return ret ? ret : len;
204}
205
David Herrmannd365c6c2012-06-10 15:16:18 +0200206static int uhid_hid_output_raw(struct hid_device *hid, __u8 *buf, size_t count,
207 unsigned char report_type)
208{
David Herrmann3b3baa82012-06-10 15:16:24 +0200209 struct uhid_device *uhid = hid->driver_data;
210 __u8 rtype;
211 unsigned long flags;
212 struct uhid_event *ev;
213
214 switch (report_type) {
215 case HID_FEATURE_REPORT:
216 rtype = UHID_FEATURE_REPORT;
217 break;
218 case HID_OUTPUT_REPORT:
219 rtype = UHID_OUTPUT_REPORT;
220 break;
221 default:
222 return -EINVAL;
223 }
224
225 if (count < 1 || count > UHID_DATA_MAX)
226 return -EINVAL;
227
228 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
229 if (!ev)
230 return -ENOMEM;
231
232 ev->type = UHID_OUTPUT;
233 ev->u.output.size = count;
234 ev->u.output.rtype = rtype;
235 memcpy(ev->u.output.data, buf, count);
236
237 spin_lock_irqsave(&uhid->qlock, flags);
238 uhid_queue(uhid, ev);
239 spin_unlock_irqrestore(&uhid->qlock, flags);
240
241 return count;
David Herrmannd365c6c2012-06-10 15:16:18 +0200242}
243
Frank Praznik596cfdd2014-01-22 13:49:43 -0500244static int uhid_hid_output_report(struct hid_device *hid, __u8 *buf,
245 size_t count)
246{
Benjamin Tissoires41abfb32014-02-10 12:58:46 -0500247 return uhid_hid_output_raw(hid, buf, count, HID_OUTPUT_REPORT);
Frank Praznik596cfdd2014-01-22 13:49:43 -0500248}
249
Benjamin Tissoires706daef2014-02-10 12:58:47 -0500250static int uhid_raw_request(struct hid_device *hid, unsigned char reportnum,
251 __u8 *buf, size_t len, unsigned char rtype,
252 int reqtype)
253{
254 switch (reqtype) {
255 case HID_REQ_GET_REPORT:
256 return uhid_hid_get_raw(hid, reportnum, buf, len, rtype);
257 case HID_REQ_SET_REPORT:
258 /* TODO: implement proper SET_REPORT functionality */
259 return -ENOSYS;
260 default:
261 return -EIO;
262 }
263}
264
David Herrmannd365c6c2012-06-10 15:16:18 +0200265static struct hid_ll_driver uhid_hid_driver = {
266 .start = uhid_hid_start,
267 .stop = uhid_hid_stop,
268 .open = uhid_hid_open,
269 .close = uhid_hid_close,
David Herrmannd365c6c2012-06-10 15:16:18 +0200270 .parse = uhid_hid_parse,
Frank Praznik596cfdd2014-01-22 13:49:43 -0500271 .output_report = uhid_hid_output_report,
Benjamin Tissoires706daef2014-02-10 12:58:47 -0500272 .raw_request = uhid_raw_request,
David Herrmannd365c6c2012-06-10 15:16:18 +0200273};
274
Dmitry Torokhovbefde022013-02-18 11:26:11 +0100275#ifdef CONFIG_COMPAT
276
277/* Apparently we haven't stepped on these rakes enough times yet. */
278struct uhid_create_req_compat {
279 __u8 name[128];
280 __u8 phys[64];
281 __u8 uniq[64];
282
283 compat_uptr_t rd_data;
284 __u16 rd_size;
285
286 __u16 bus;
287 __u32 vendor;
288 __u32 product;
289 __u32 version;
290 __u32 country;
291} __attribute__((__packed__));
292
293static int uhid_event_from_user(const char __user *buffer, size_t len,
294 struct uhid_event *event)
295{
296 if (is_compat_task()) {
297 u32 type;
298
299 if (get_user(type, buffer))
300 return -EFAULT;
301
302 if (type == UHID_CREATE) {
303 /*
304 * This is our messed up request with compat pointer.
305 * It is largish (more than 256 bytes) so we better
306 * allocate it from the heap.
307 */
308 struct uhid_create_req_compat *compat;
309
David Herrmann80897aa2013-11-26 13:58:18 +0100310 compat = kzalloc(sizeof(*compat), GFP_KERNEL);
Dmitry Torokhovbefde022013-02-18 11:26:11 +0100311 if (!compat)
312 return -ENOMEM;
313
314 buffer += sizeof(type);
315 len -= sizeof(type);
316 if (copy_from_user(compat, buffer,
317 min(len, sizeof(*compat)))) {
318 kfree(compat);
319 return -EFAULT;
320 }
321
322 /* Shuffle the data over to proper structure */
323 event->type = type;
324
325 memcpy(event->u.create.name, compat->name,
326 sizeof(compat->name));
327 memcpy(event->u.create.phys, compat->phys,
328 sizeof(compat->phys));
329 memcpy(event->u.create.uniq, compat->uniq,
330 sizeof(compat->uniq));
331
332 event->u.create.rd_data = compat_ptr(compat->rd_data);
333 event->u.create.rd_size = compat->rd_size;
334
335 event->u.create.bus = compat->bus;
336 event->u.create.vendor = compat->vendor;
337 event->u.create.product = compat->product;
338 event->u.create.version = compat->version;
339 event->u.create.country = compat->country;
340
341 kfree(compat);
342 return 0;
343 }
344 /* All others can be copied directly */
345 }
346
347 if (copy_from_user(event, buffer, min(len, sizeof(*event))))
348 return -EFAULT;
349
350 return 0;
351}
352#else
353static int uhid_event_from_user(const char __user *buffer, size_t len,
354 struct uhid_event *event)
355{
356 if (copy_from_user(event, buffer, min(len, sizeof(*event))))
357 return -EFAULT;
358
359 return 0;
360}
361#endif
362
Petri Gynther45226432014-03-24 13:50:01 -0700363static int uhid_dev_create2(struct uhid_device *uhid,
364 const struct uhid_event *ev)
365{
366 struct hid_device *hid;
David Herrmann25be7fe2014-07-29 17:14:18 +0200367 size_t rd_size, len;
David Herrmann41c4a462014-07-29 17:14:17 +0200368 void *rd_data;
Petri Gynther45226432014-03-24 13:50:01 -0700369 int ret;
370
371 if (uhid->running)
372 return -EALREADY;
373
David Herrmann41c4a462014-07-29 17:14:17 +0200374 rd_size = ev->u.create2.rd_size;
375 if (rd_size <= 0 || rd_size > HID_MAX_DESCRIPTOR_SIZE)
Petri Gynther45226432014-03-24 13:50:01 -0700376 return -EINVAL;
377
David Herrmann41c4a462014-07-29 17:14:17 +0200378 rd_data = kmemdup(ev->u.create2.rd_data, rd_size, GFP_KERNEL);
379 if (!rd_data)
Petri Gynther45226432014-03-24 13:50:01 -0700380 return -ENOMEM;
381
David Herrmann41c4a462014-07-29 17:14:17 +0200382 uhid->rd_size = rd_size;
383 uhid->rd_data = rd_data;
384
Petri Gynther45226432014-03-24 13:50:01 -0700385 hid = hid_allocate_device();
386 if (IS_ERR(hid)) {
387 ret = PTR_ERR(hid);
388 goto err_free;
389 }
390
David Herrmann25be7fe2014-07-29 17:14:18 +0200391 len = min(sizeof(hid->name), sizeof(ev->u.create2.name)) - 1;
392 strncpy(hid->name, ev->u.create2.name, len);
393 len = min(sizeof(hid->phys), sizeof(ev->u.create2.phys)) - 1;
394 strncpy(hid->phys, ev->u.create2.phys, len);
395 len = min(sizeof(hid->uniq), sizeof(ev->u.create2.uniq)) - 1;
396 strncpy(hid->uniq, ev->u.create2.uniq, len);
Petri Gynther45226432014-03-24 13:50:01 -0700397
398 hid->ll_driver = &uhid_hid_driver;
399 hid->bus = ev->u.create2.bus;
400 hid->vendor = ev->u.create2.vendor;
401 hid->product = ev->u.create2.product;
402 hid->version = ev->u.create2.version;
403 hid->country = ev->u.create2.country;
404 hid->driver_data = uhid;
405 hid->dev.parent = uhid_misc.this_device;
406
407 uhid->hid = hid;
408 uhid->running = true;
409
410 ret = hid_add_device(hid);
411 if (ret) {
412 hid_err(hid, "Cannot register HID device\n");
413 goto err_hid;
414 }
415
416 return 0;
417
418err_hid:
419 hid_destroy_device(hid);
420 uhid->hid = NULL;
421 uhid->running = false;
422err_free:
423 kfree(uhid->rd_data);
David Herrmann41c4a462014-07-29 17:14:17 +0200424 uhid->rd_data = NULL;
425 uhid->rd_size = 0;
Petri Gynther45226432014-03-24 13:50:01 -0700426 return ret;
427}
428
David Herrmann56c47752014-07-29 17:14:16 +0200429static int uhid_dev_create(struct uhid_device *uhid,
430 struct uhid_event *ev)
431{
432 struct uhid_create_req orig;
433
434 orig = ev->u.create;
435
436 if (orig.rd_size <= 0 || orig.rd_size > HID_MAX_DESCRIPTOR_SIZE)
437 return -EINVAL;
438 if (copy_from_user(&ev->u.create2.rd_data, orig.rd_data, orig.rd_size))
439 return -EFAULT;
440
441 memcpy(ev->u.create2.name, orig.name, sizeof(orig.name));
442 memcpy(ev->u.create2.phys, orig.phys, sizeof(orig.phys));
443 memcpy(ev->u.create2.uniq, orig.uniq, sizeof(orig.uniq));
444 ev->u.create2.rd_size = orig.rd_size;
445 ev->u.create2.bus = orig.bus;
446 ev->u.create2.vendor = orig.vendor;
447 ev->u.create2.product = orig.product;
448 ev->u.create2.version = orig.version;
449 ev->u.create2.country = orig.country;
450
451 return uhid_dev_create2(uhid, ev);
452}
453
David Herrmannd365c6c2012-06-10 15:16:18 +0200454static int uhid_dev_destroy(struct uhid_device *uhid)
455{
456 if (!uhid->running)
457 return -EINVAL;
458
459 uhid->running = false;
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200460 wake_up_interruptible(&uhid->report_wait);
David Herrmannd365c6c2012-06-10 15:16:18 +0200461
462 hid_destroy_device(uhid->hid);
463 kfree(uhid->rd_data);
464
465 return 0;
466}
467
David Herrmann5e87a362012-06-10 15:16:19 +0200468static int uhid_dev_input(struct uhid_device *uhid, struct uhid_event *ev)
469{
470 if (!uhid->running)
471 return -EINVAL;
472
473 hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input.data,
474 min_t(size_t, ev->u.input.size, UHID_DATA_MAX), 0);
475
476 return 0;
477}
478
Petri Gynther45226432014-03-24 13:50:01 -0700479static int uhid_dev_input2(struct uhid_device *uhid, struct uhid_event *ev)
480{
481 if (!uhid->running)
482 return -EINVAL;
483
484 hid_input_report(uhid->hid, HID_INPUT_REPORT, ev->u.input2.data,
485 min_t(size_t, ev->u.input2.size, UHID_DATA_MAX), 0);
486
487 return 0;
488}
489
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200490static int uhid_dev_feature_answer(struct uhid_device *uhid,
491 struct uhid_event *ev)
492{
493 unsigned long flags;
494
495 if (!uhid->running)
496 return -EINVAL;
497
498 spin_lock_irqsave(&uhid->qlock, flags);
499
500 /* id for old report; drop it silently */
David Herrmann8cad5b02014-07-29 17:14:19 +0200501 if (uhid->report_id != ev->u.feature_answer.id)
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200502 goto unlock;
David Herrmann5942b842014-07-29 17:14:20 +0200503 if (!uhid->report_running)
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200504 goto unlock;
505
506 memcpy(&uhid->report_buf, ev, sizeof(*ev));
David Herrmann5942b842014-07-29 17:14:20 +0200507 uhid->report_running = false;
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200508 wake_up_interruptible(&uhid->report_wait);
509
510unlock:
511 spin_unlock_irqrestore(&uhid->qlock, flags);
512 return 0;
513}
514
David Herrmann1ccd7a22012-06-10 15:16:13 +0200515static int uhid_char_open(struct inode *inode, struct file *file)
516{
David Herrmannace3d862012-06-10 15:16:14 +0200517 struct uhid_device *uhid;
518
519 uhid = kzalloc(sizeof(*uhid), GFP_KERNEL);
520 if (!uhid)
521 return -ENOMEM;
522
David Herrmannd937ae52012-06-10 15:16:16 +0200523 mutex_init(&uhid->devlock);
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200524 mutex_init(&uhid->report_lock);
David Herrmannace3d862012-06-10 15:16:14 +0200525 spin_lock_init(&uhid->qlock);
526 init_waitqueue_head(&uhid->waitq);
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200527 init_waitqueue_head(&uhid->report_wait);
David Herrmannd365c6c2012-06-10 15:16:18 +0200528 uhid->running = false;
David Herrmannace3d862012-06-10 15:16:14 +0200529
530 file->private_data = uhid;
531 nonseekable_open(inode, file);
532
David Herrmann1ccd7a22012-06-10 15:16:13 +0200533 return 0;
534}
535
536static int uhid_char_release(struct inode *inode, struct file *file)
537{
David Herrmannace3d862012-06-10 15:16:14 +0200538 struct uhid_device *uhid = file->private_data;
539 unsigned int i;
540
David Herrmannd365c6c2012-06-10 15:16:18 +0200541 uhid_dev_destroy(uhid);
542
David Herrmannace3d862012-06-10 15:16:14 +0200543 for (i = 0; i < UHID_BUFSIZE; ++i)
544 kfree(uhid->outq[i]);
545
546 kfree(uhid);
547
David Herrmann1ccd7a22012-06-10 15:16:13 +0200548 return 0;
549}
550
551static ssize_t uhid_char_read(struct file *file, char __user *buffer,
552 size_t count, loff_t *ppos)
553{
David Herrmannd937ae52012-06-10 15:16:16 +0200554 struct uhid_device *uhid = file->private_data;
555 int ret;
556 unsigned long flags;
557 size_t len;
558
559 /* they need at least the "type" member of uhid_event */
560 if (count < sizeof(__u32))
561 return -EINVAL;
562
563try_again:
564 if (file->f_flags & O_NONBLOCK) {
565 if (uhid->head == uhid->tail)
566 return -EAGAIN;
567 } else {
568 ret = wait_event_interruptible(uhid->waitq,
569 uhid->head != uhid->tail);
570 if (ret)
571 return ret;
572 }
573
574 ret = mutex_lock_interruptible(&uhid->devlock);
575 if (ret)
576 return ret;
577
578 if (uhid->head == uhid->tail) {
579 mutex_unlock(&uhid->devlock);
580 goto try_again;
581 } else {
582 len = min(count, sizeof(**uhid->outq));
Vinicius Costa Gomesadefb692012-07-14 18:59:25 -0300583 if (copy_to_user(buffer, uhid->outq[uhid->tail], len)) {
David Herrmannd937ae52012-06-10 15:16:16 +0200584 ret = -EFAULT;
585 } else {
586 kfree(uhid->outq[uhid->tail]);
587 uhid->outq[uhid->tail] = NULL;
588
589 spin_lock_irqsave(&uhid->qlock, flags);
590 uhid->tail = (uhid->tail + 1) % UHID_BUFSIZE;
591 spin_unlock_irqrestore(&uhid->qlock, flags);
592 }
593 }
594
595 mutex_unlock(&uhid->devlock);
596 return ret ? ret : len;
David Herrmann1ccd7a22012-06-10 15:16:13 +0200597}
598
599static ssize_t uhid_char_write(struct file *file, const char __user *buffer,
600 size_t count, loff_t *ppos)
601{
David Herrmann6664ef72012-06-10 15:16:17 +0200602 struct uhid_device *uhid = file->private_data;
603 int ret;
604 size_t len;
605
606 /* we need at least the "type" member of uhid_event */
607 if (count < sizeof(__u32))
608 return -EINVAL;
609
610 ret = mutex_lock_interruptible(&uhid->devlock);
611 if (ret)
612 return ret;
613
614 memset(&uhid->input_buf, 0, sizeof(uhid->input_buf));
615 len = min(count, sizeof(uhid->input_buf));
Dmitry Torokhovbefde022013-02-18 11:26:11 +0100616
617 ret = uhid_event_from_user(buffer, len, &uhid->input_buf);
618 if (ret)
David Herrmann6664ef72012-06-10 15:16:17 +0200619 goto unlock;
David Herrmann6664ef72012-06-10 15:16:17 +0200620
621 switch (uhid->input_buf.type) {
David Herrmannd365c6c2012-06-10 15:16:18 +0200622 case UHID_CREATE:
623 ret = uhid_dev_create(uhid, &uhid->input_buf);
624 break;
Petri Gynther45226432014-03-24 13:50:01 -0700625 case UHID_CREATE2:
626 ret = uhid_dev_create2(uhid, &uhid->input_buf);
627 break;
David Herrmannd365c6c2012-06-10 15:16:18 +0200628 case UHID_DESTROY:
629 ret = uhid_dev_destroy(uhid);
630 break;
David Herrmann5e87a362012-06-10 15:16:19 +0200631 case UHID_INPUT:
632 ret = uhid_dev_input(uhid, &uhid->input_buf);
633 break;
Petri Gynther45226432014-03-24 13:50:01 -0700634 case UHID_INPUT2:
635 ret = uhid_dev_input2(uhid, &uhid->input_buf);
636 break;
David Herrmannfcfcf0d2012-06-10 15:16:25 +0200637 case UHID_FEATURE_ANSWER:
638 ret = uhid_dev_feature_answer(uhid, &uhid->input_buf);
639 break;
David Herrmann6664ef72012-06-10 15:16:17 +0200640 default:
641 ret = -EOPNOTSUPP;
642 }
643
644unlock:
645 mutex_unlock(&uhid->devlock);
646
647 /* return "count" not "len" to not confuse the caller */
648 return ret ? ret : count;
David Herrmann1ccd7a22012-06-10 15:16:13 +0200649}
650
651static unsigned int uhid_char_poll(struct file *file, poll_table *wait)
652{
David Herrmann1f9dec12012-06-10 15:16:15 +0200653 struct uhid_device *uhid = file->private_data;
654
655 poll_wait(file, &uhid->waitq, wait);
656
657 if (uhid->head != uhid->tail)
658 return POLLIN | POLLRDNORM;
659
David Herrmann1ccd7a22012-06-10 15:16:13 +0200660 return 0;
661}
662
663static const struct file_operations uhid_fops = {
664 .owner = THIS_MODULE,
665 .open = uhid_char_open,
666 .release = uhid_char_release,
667 .read = uhid_char_read,
668 .write = uhid_char_write,
669 .poll = uhid_char_poll,
670 .llseek = no_llseek,
671};
672
673static struct miscdevice uhid_misc = {
674 .fops = &uhid_fops,
David Herrmann19872d22013-09-09 18:33:54 +0200675 .minor = UHID_MINOR,
David Herrmann1ccd7a22012-06-10 15:16:13 +0200676 .name = UHID_NAME,
677};
678
679static int __init uhid_init(void)
680{
681 return misc_register(&uhid_misc);
682}
683
684static void __exit uhid_exit(void)
685{
686 misc_deregister(&uhid_misc);
687}
688
689module_init(uhid_init);
690module_exit(uhid_exit);
691MODULE_LICENSE("GPL");
692MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
693MODULE_DESCRIPTION("User-space I/O driver support for HID subsystem");
David Herrmann19872d22013-09-09 18:33:54 +0200694MODULE_ALIAS_MISCDEV(UHID_MINOR);
Marcel Holtmann60cbd532013-09-01 11:02:46 -0700695MODULE_ALIAS("devname:" UHID_NAME);