1 /*
2 * Event char devices, giving access to raw input device events.
3 *
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 */
10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13 #define EVDEV_MINOR_BASE 64
14 #define EVDEV_MINORS 32
15 #define EVDEV_MIN_BUFFER_SIZE 64U
16 #define EVDEV_BUF_PACKETS 8
17
18 #include <linux/poll.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/input/mt.h>
24 #include <linux/major.h>
25 #include <linux/device.h>
26 #include <linux/cdev.h>
27 #include <linux/wakelock.h>
28 #include "input-compat.h"
29
30 struct evdev {
31 int open;
32 struct input_handle handle;
33 wait_queue_head_t wait;
34 struct evdev_client __rcu *grab;
35 struct list_head client_list;
36 spinlock_t client_lock; /* protects client_list */
37 struct mutex mutex;
38 struct device dev;
39 struct cdev cdev;
40 bool exist;
41 };
42
43 struct evdev_client {
44 unsigned int head;
45 unsigned int tail;
46 unsigned int packet_head; /* [future] position of the first element of next packet */
47 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
48 struct wake_lock wake_lock;
49 bool use_wake_lock;
50 char name[28];
51 struct fasync_struct *fasync;
52 struct evdev *evdev;
53 struct list_head node;
54 int clkid;
55 unsigned int bufsize;
56 struct input_event buffer[];
57 };
58
__pass_event(struct evdev_client * client,const struct input_event * event)59 static void __pass_event(struct evdev_client *client,
60 const struct input_event *event)
61 {
62 client->buffer[client->head++] = *event;
63 client->head &= client->bufsize - 1;
64
65 if (unlikely(client->head == client->tail)) {
66 /*
67 * This effectively "drops" all unconsumed events, leaving
68 * EV_SYN/SYN_DROPPED plus the newest event in the queue.
69 */
70 client->tail = (client->head - 2) & (client->bufsize - 1);
71
72 client->buffer[client->tail].time = event->time;
73 client->buffer[client->tail].type = EV_SYN;
74 client->buffer[client->tail].code = SYN_DROPPED;
75 client->buffer[client->tail].value = 0;
76
77 client->packet_head = client->tail;
78 if (client->use_wake_lock)
79 wake_unlock(&client->wake_lock);
80 }
81
82 if (event->type == EV_SYN && event->code == SYN_REPORT) {
83 client->packet_head = client->head;
84 if (client->use_wake_lock)
85 wake_lock(&client->wake_lock);
86 kill_fasync(&client->fasync, SIGIO, POLL_IN);
87 }
88 }
89
evdev_pass_values(struct evdev_client * client,const struct input_value * vals,unsigned int count,ktime_t mono,ktime_t real)90 static void evdev_pass_values(struct evdev_client *client,
91 const struct input_value *vals, unsigned int count,
92 ktime_t mono, ktime_t real)
93 {
94 struct evdev *evdev = client->evdev;
95 const struct input_value *v;
96 struct input_event event;
97 bool wakeup = false;
98
99 event.time = ktime_to_timeval(client->clkid == CLOCK_MONOTONIC ?
100 mono : real);
101
102 /* Interrupts are disabled, just acquire the lock. */
103 spin_lock(&client->buffer_lock);
104
105 for (v = vals; v != vals + count; v++) {
106 event.type = v->type;
107 event.code = v->code;
108 event.value = v->value;
109 __pass_event(client, &event);
110 if (v->type == EV_SYN && v->code == SYN_REPORT)
111 wakeup = true;
112 }
113
114 spin_unlock(&client->buffer_lock);
115
116 if (wakeup)
117 wake_up_interruptible(&evdev->wait);
118 }
119
120 /*
121 * Pass incoming events to all connected clients.
122 */
evdev_events(struct input_handle * handle,const struct input_value * vals,unsigned int count)123 static void evdev_events(struct input_handle *handle,
124 const struct input_value *vals, unsigned int count)
125 {
126 struct evdev *evdev = handle->private;
127 struct evdev_client *client;
128 ktime_t time_mono, time_real;
129
130 time_mono = ktime_get();
131 time_real = ktime_sub(time_mono, ktime_get_monotonic_offset());
132
133 rcu_read_lock();
134
135 client = rcu_dereference(evdev->grab);
136
137 if (client)
138 evdev_pass_values(client, vals, count, time_mono, time_real);
139 else
140 list_for_each_entry_rcu(client, &evdev->client_list, node)
141 evdev_pass_values(client, vals, count,
142 time_mono, time_real);
143
144 rcu_read_unlock();
145 }
146
147 /*
148 * Pass incoming event to all connected clients.
149 */
evdev_event(struct input_handle * handle,unsigned int type,unsigned int code,int value)150 static void evdev_event(struct input_handle *handle,
151 unsigned int type, unsigned int code, int value)
152 {
153 struct input_value vals[] = { { type, code, value } };
154
155 evdev_events(handle, vals, 1);
156 }
157
evdev_fasync(int fd,struct file * file,int on)158 static int evdev_fasync(int fd, struct file *file, int on)
159 {
160 struct evdev_client *client = file->private_data;
161
162 return fasync_helper(fd, file, on, &client->fasync);
163 }
164
evdev_flush(struct file * file,fl_owner_t id)165 static int evdev_flush(struct file *file, fl_owner_t id)
166 {
167 struct evdev_client *client = file->private_data;
168 struct evdev *evdev = client->evdev;
169 int retval;
170
171 retval = mutex_lock_interruptible(&evdev->mutex);
172 if (retval)
173 return retval;
174
175 if (!evdev->exist)
176 retval = -ENODEV;
177 else
178 retval = input_flush_device(&evdev->handle, file);
179
180 mutex_unlock(&evdev->mutex);
181 return retval;
182 }
183
evdev_free(struct device * dev)184 static void evdev_free(struct device *dev)
185 {
186 struct evdev *evdev = container_of(dev, struct evdev, dev);
187
188 input_put_device(evdev->handle.dev);
189 kfree(evdev);
190 }
191
192 /*
193 * Grabs an event device (along with underlying input device).
194 * This function is called with evdev->mutex taken.
195 */
evdev_grab(struct evdev * evdev,struct evdev_client * client)196 static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
197 {
198 int error;
199
200 if (evdev->grab)
201 return -EBUSY;
202
203 error = input_grab_device(&evdev->handle);
204 if (error)
205 return error;
206
207 rcu_assign_pointer(evdev->grab, client);
208
209 return 0;
210 }
211
evdev_ungrab(struct evdev * evdev,struct evdev_client * client)212 static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
213 {
214 struct evdev_client *grab = rcu_dereference_protected(evdev->grab,
215 lockdep_is_held(&evdev->mutex));
216
217 if (grab != client)
218 return -EINVAL;
219
220 rcu_assign_pointer(evdev->grab, NULL);
221 synchronize_rcu();
222 input_release_device(&evdev->handle);
223
224 return 0;
225 }
226
evdev_attach_client(struct evdev * evdev,struct evdev_client * client)227 static void evdev_attach_client(struct evdev *evdev,
228 struct evdev_client *client)
229 {
230 spin_lock(&evdev->client_lock);
231 list_add_tail_rcu(&client->node, &evdev->client_list);
232 spin_unlock(&evdev->client_lock);
233 }
234
evdev_detach_client(struct evdev * evdev,struct evdev_client * client)235 static void evdev_detach_client(struct evdev *evdev,
236 struct evdev_client *client)
237 {
238 spin_lock(&evdev->client_lock);
239 list_del_rcu(&client->node);
240 spin_unlock(&evdev->client_lock);
241 synchronize_rcu();
242 }
243
evdev_open_device(struct evdev * evdev)244 static int evdev_open_device(struct evdev *evdev)
245 {
246 int retval;
247
248 retval = mutex_lock_interruptible(&evdev->mutex);
249 if (retval)
250 return retval;
251
252 if (!evdev->exist)
253 retval = -ENODEV;
254 else if (!evdev->open++) {
255 retval = input_open_device(&evdev->handle);
256 if (retval)
257 evdev->open--;
258 }
259
260 mutex_unlock(&evdev->mutex);
261 return retval;
262 }
263
evdev_close_device(struct evdev * evdev)264 static void evdev_close_device(struct evdev *evdev)
265 {
266 mutex_lock(&evdev->mutex);
267
268 if (evdev->exist && !--evdev->open)
269 input_close_device(&evdev->handle);
270
271 mutex_unlock(&evdev->mutex);
272 }
273
274 /*
275 * Wake up users waiting for IO so they can disconnect from
276 * dead device.
277 */
evdev_hangup(struct evdev * evdev)278 static void evdev_hangup(struct evdev *evdev)
279 {
280 struct evdev_client *client;
281
282 spin_lock(&evdev->client_lock);
283 list_for_each_entry(client, &evdev->client_list, node)
284 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
285 spin_unlock(&evdev->client_lock);
286
287 wake_up_interruptible(&evdev->wait);
288 }
289
evdev_release(struct inode * inode,struct file * file)290 static int evdev_release(struct inode *inode, struct file *file)
291 {
292 struct evdev_client *client = file->private_data;
293 struct evdev *evdev = client->evdev;
294
295 mutex_lock(&evdev->mutex);
296 evdev_ungrab(evdev, client);
297 mutex_unlock(&evdev->mutex);
298
299 evdev_detach_client(evdev, client);
300 if (client->use_wake_lock)
301 wake_lock_destroy(&client->wake_lock);
302 kfree(client);
303
304 evdev_close_device(evdev);
305
306 return 0;
307 }
308
evdev_compute_buffer_size(struct input_dev * dev)309 static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
310 {
311 unsigned int n_events =
312 max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
313 EVDEV_MIN_BUFFER_SIZE);
314
315 return roundup_pow_of_two(n_events);
316 }
317
evdev_open(struct inode * inode,struct file * file)318 static int evdev_open(struct inode *inode, struct file *file)
319 {
320 struct evdev *evdev = container_of(inode->i_cdev, struct evdev, cdev);
321 unsigned int bufsize = evdev_compute_buffer_size(evdev->handle.dev);
322 struct evdev_client *client;
323 int error;
324
325 client = kzalloc(sizeof(struct evdev_client) +
326 bufsize * sizeof(struct input_event),
327 GFP_KERNEL);
328 if (!client)
329 return -ENOMEM;
330
331 client->bufsize = bufsize;
332 spin_lock_init(&client->buffer_lock);
333 snprintf(client->name, sizeof(client->name), "%s-%d",
334 dev_name(&evdev->dev), task_tgid_vnr(current));
335 client->evdev = evdev;
336 evdev_attach_client(evdev, client);
337
338 error = evdev_open_device(evdev);
339 if (error)
340 goto err_free_client;
341
342 file->private_data = client;
343 nonseekable_open(inode, file);
344
345 return 0;
346
347 err_free_client:
348 evdev_detach_client(evdev, client);
349 kfree(client);
350 return error;
351 }
352
evdev_write(struct file * file,const char __user * buffer,size_t count,loff_t * ppos)353 static ssize_t evdev_write(struct file *file, const char __user *buffer,
354 size_t count, loff_t *ppos)
355 {
356 struct evdev_client *client = file->private_data;
357 struct evdev *evdev = client->evdev;
358 struct input_event event;
359 int retval = 0;
360
361 if (count != 0 && count < input_event_size())
362 return -EINVAL;
363
364 retval = mutex_lock_interruptible(&evdev->mutex);
365 if (retval)
366 return retval;
367
368 if (!evdev->exist) {
369 retval = -ENODEV;
370 goto out;
371 }
372
373 while (retval + input_event_size() <= count) {
374
375 if (input_event_from_user(buffer + retval, &event)) {
376 retval = -EFAULT;
377 goto out;
378 }
379 retval += input_event_size();
380
381 input_inject_event(&evdev->handle,
382 event.type, event.code, event.value);
383 }
384
385 out:
386 mutex_unlock(&evdev->mutex);
387 return retval;
388 }
389
evdev_fetch_next_event(struct evdev_client * client,struct input_event * event)390 static int evdev_fetch_next_event(struct evdev_client *client,
391 struct input_event *event)
392 {
393 int have_event;
394
395 spin_lock_irq(&client->buffer_lock);
396
397 have_event = client->packet_head != client->tail;
398 if (have_event) {
399 *event = client->buffer[client->tail++];
400 client->tail &= client->bufsize - 1;
401 if (client->use_wake_lock &&
402 client->packet_head == client->tail)
403 wake_unlock(&client->wake_lock);
404 }
405
406 spin_unlock_irq(&client->buffer_lock);
407
408 return have_event;
409 }
410
evdev_read(struct file * file,char __user * buffer,size_t count,loff_t * ppos)411 static ssize_t evdev_read(struct file *file, char __user *buffer,
412 size_t count, loff_t *ppos)
413 {
414 struct evdev_client *client = file->private_data;
415 struct evdev *evdev = client->evdev;
416 struct input_event event;
417 size_t read = 0;
418 int error;
419
420 if (count != 0 && count < input_event_size())
421 return -EINVAL;
422
423 for (;;) {
424 if (!evdev->exist)
425 return -ENODEV;
426
427 if (client->packet_head == client->tail &&
428 (file->f_flags & O_NONBLOCK))
429 return -EAGAIN;
430
431 /*
432 * count == 0 is special - no IO is done but we check
433 * for error conditions (see above).
434 */
435 if (count == 0)
436 break;
437
438 while (read + input_event_size() <= count &&
439 evdev_fetch_next_event(client, &event)) {
440
441 if (input_event_to_user(buffer + read, &event))
442 return -EFAULT;
443
444 read += input_event_size();
445 }
446
447 if (read)
448 break;
449
450 if (!(file->f_flags & O_NONBLOCK)) {
451 error = wait_event_interruptible(evdev->wait,
452 client->packet_head != client->tail ||
453 !evdev->exist);
454 if (error)
455 return error;
456 }
457 }
458
459 return read;
460 }
461
462 /* No kernel lock - fine */
evdev_poll(struct file * file,poll_table * wait)463 static unsigned int evdev_poll(struct file *file, poll_table *wait)
464 {
465 struct evdev_client *client = file->private_data;
466 struct evdev *evdev = client->evdev;
467 unsigned int mask;
468
469 poll_wait(file, &evdev->wait, wait);
470
471 mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR;
472 if (client->packet_head != client->tail)
473 mask |= POLLIN | POLLRDNORM;
474
475 return mask;
476 }
477
478 #ifdef CONFIG_COMPAT
479
480 #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
481 #define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
482
483 #ifdef __BIG_ENDIAN
bits_to_user(unsigned long * bits,unsigned int maxbit,unsigned int maxlen,void __user * p,int compat)484 static int bits_to_user(unsigned long *bits, unsigned int maxbit,
485 unsigned int maxlen, void __user *p, int compat)
486 {
487 int len, i;
488
489 if (compat) {
490 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
491 if (len > maxlen)
492 len = maxlen;
493
494 for (i = 0; i < len / sizeof(compat_long_t); i++)
495 if (copy_to_user((compat_long_t __user *) p + i,
496 (compat_long_t *) bits +
497 i + 1 - ((i % 2) << 1),
498 sizeof(compat_long_t)))
499 return -EFAULT;
500 } else {
501 len = BITS_TO_LONGS(maxbit) * sizeof(long);
502 if (len > maxlen)
503 len = maxlen;
504
505 if (copy_to_user(p, bits, len))
506 return -EFAULT;
507 }
508
509 return len;
510 }
511 #else
bits_to_user(unsigned long * bits,unsigned int maxbit,unsigned int maxlen,void __user * p,int compat)512 static int bits_to_user(unsigned long *bits, unsigned int maxbit,
513 unsigned int maxlen, void __user *p, int compat)
514 {
515 int len = compat ?
516 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
517 BITS_TO_LONGS(maxbit) * sizeof(long);
518
519 if (len > maxlen)
520 len = maxlen;
521
522 return copy_to_user(p, bits, len) ? -EFAULT : len;
523 }
524 #endif /* __BIG_ENDIAN */
525
526 #else
527
bits_to_user(unsigned long * bits,unsigned int maxbit,unsigned int maxlen,void __user * p,int compat)528 static int bits_to_user(unsigned long *bits, unsigned int maxbit,
529 unsigned int maxlen, void __user *p, int compat)
530 {
531 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
532
533 if (len > maxlen)
534 len = maxlen;
535
536 return copy_to_user(p, bits, len) ? -EFAULT : len;
537 }
538
539 #endif /* CONFIG_COMPAT */
540
str_to_user(const char * str,unsigned int maxlen,void __user * p)541 static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
542 {
543 int len;
544
545 if (!str)
546 return -ENOENT;
547
548 len = strlen(str) + 1;
549 if (len > maxlen)
550 len = maxlen;
551
552 return copy_to_user(p, str, len) ? -EFAULT : len;
553 }
554
555 #define OLD_KEY_MAX 0x1ff
handle_eviocgbit(struct input_dev * dev,unsigned int type,unsigned int size,void __user * p,int compat_mode)556 static int handle_eviocgbit(struct input_dev *dev,
557 unsigned int type, unsigned int size,
558 void __user *p, int compat_mode)
559 {
560 static unsigned long keymax_warn_time;
561 unsigned long *bits;
562 int len;
563
564 switch (type) {
565
566 case 0: bits = dev->evbit; len = EV_MAX; break;
567 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
568 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
569 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
570 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
571 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
572 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
573 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
574 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
575 default: return -EINVAL;
576 }
577
578 /*
579 * Work around bugs in userspace programs that like to do
580 * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
581 * should be in bytes, not in bits.
582 */
583 if (type == EV_KEY && size == OLD_KEY_MAX) {
584 len = OLD_KEY_MAX;
585 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
586 pr_warning("(EVIOCGBIT): Suspicious buffer size %u, "
587 "limiting output to %zu bytes. See "
588 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
589 OLD_KEY_MAX,
590 BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
591 }
592
593 return bits_to_user(bits, len, size, p, compat_mode);
594 }
595 #undef OLD_KEY_MAX
596
evdev_handle_get_keycode(struct input_dev * dev,void __user * p)597 static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
598 {
599 struct input_keymap_entry ke = {
600 .len = sizeof(unsigned int),
601 .flags = 0,
602 };
603 int __user *ip = (int __user *)p;
604 int error;
605
606 /* legacy case */
607 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
608 return -EFAULT;
609
610 error = input_get_keycode(dev, &ke);
611 if (error)
612 return error;
613
614 if (put_user(ke.keycode, ip + 1))
615 return -EFAULT;
616
617 return 0;
618 }
619
evdev_handle_get_keycode_v2(struct input_dev * dev,void __user * p)620 static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
621 {
622 struct input_keymap_entry ke;
623 int error;
624
625 if (copy_from_user(&ke, p, sizeof(ke)))
626 return -EFAULT;
627
628 error = input_get_keycode(dev, &ke);
629 if (error)
630 return error;
631
632 if (copy_to_user(p, &ke, sizeof(ke)))
633 return -EFAULT;
634
635 return 0;
636 }
637
evdev_handle_set_keycode(struct input_dev * dev,void __user * p)638 static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
639 {
640 struct input_keymap_entry ke = {
641 .len = sizeof(unsigned int),
642 .flags = 0,
643 };
644 int __user *ip = (int __user *)p;
645
646 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
647 return -EFAULT;
648
649 if (get_user(ke.keycode, ip + 1))
650 return -EFAULT;
651
652 return input_set_keycode(dev, &ke);
653 }
654
evdev_handle_set_keycode_v2(struct input_dev * dev,void __user * p)655 static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
656 {
657 struct input_keymap_entry ke;
658
659 if (copy_from_user(&ke, p, sizeof(ke)))
660 return -EFAULT;
661
662 if (ke.len > sizeof(ke.scancode))
663 return -EINVAL;
664
665 return input_set_keycode(dev, &ke);
666 }
667
evdev_handle_mt_request(struct input_dev * dev,unsigned int size,int __user * ip)668 static int evdev_handle_mt_request(struct input_dev *dev,
669 unsigned int size,
670 int __user *ip)
671 {
672 const struct input_mt *mt = dev->mt;
673 unsigned int code;
674 int max_slots;
675 int i;
676
677 if (get_user(code, &ip[0]))
678 return -EFAULT;
679 if (!mt || !input_is_mt_value(code))
680 return -EINVAL;
681
682 max_slots = (size - sizeof(__u32)) / sizeof(__s32);
683 for (i = 0; i < mt->num_slots && i < max_slots; i++) {
684 int value = input_mt_get_value(&mt->slots[i], code);
685 if (put_user(value, &ip[1 + i]))
686 return -EFAULT;
687 }
688
689 return 0;
690 }
691
evdev_enable_suspend_block(struct evdev * evdev,struct evdev_client * client)692 static int evdev_enable_suspend_block(struct evdev *evdev,
693 struct evdev_client *client)
694 {
695 if (client->use_wake_lock)
696 return 0;
697
698 spin_lock_irq(&client->buffer_lock);
699 wake_lock_init(&client->wake_lock, WAKE_LOCK_SUSPEND, client->name);
700 client->use_wake_lock = true;
701 if (client->packet_head != client->tail)
702 wake_lock(&client->wake_lock);
703 spin_unlock_irq(&client->buffer_lock);
704 return 0;
705 }
706
evdev_disable_suspend_block(struct evdev * evdev,struct evdev_client * client)707 static int evdev_disable_suspend_block(struct evdev *evdev,
708 struct evdev_client *client)
709 {
710 if (!client->use_wake_lock)
711 return 0;
712
713 spin_lock_irq(&client->buffer_lock);
714 client->use_wake_lock = false;
715 spin_unlock_irq(&client->buffer_lock);
716 wake_lock_destroy(&client->wake_lock);
717
718 return 0;
719 }
720
evdev_do_ioctl(struct file * file,unsigned int cmd,void __user * p,int compat_mode)721 static long evdev_do_ioctl(struct file *file, unsigned int cmd,
722 void __user *p, int compat_mode)
723 {
724 struct evdev_client *client = file->private_data;
725 struct evdev *evdev = client->evdev;
726 struct input_dev *dev = evdev->handle.dev;
727 struct input_absinfo abs;
728 struct ff_effect effect;
729 int __user *ip = (int __user *)p;
730 unsigned int i, t, u, v;
731 unsigned int size;
732 int error;
733
734 /* First we check for fixed-length commands */
735 switch (cmd) {
736
737 case EVIOCGVERSION:
738 return put_user(EV_VERSION, ip);
739
740 case EVIOCGID:
741 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
742 return -EFAULT;
743 return 0;
744
745 case EVIOCGREP:
746 if (!test_bit(EV_REP, dev->evbit))
747 return -ENOSYS;
748 if (put_user(dev->rep[REP_DELAY], ip))
749 return -EFAULT;
750 if (put_user(dev->rep[REP_PERIOD], ip + 1))
751 return -EFAULT;
752 return 0;
753
754 case EVIOCSREP:
755 if (!test_bit(EV_REP, dev->evbit))
756 return -ENOSYS;
757 if (get_user(u, ip))
758 return -EFAULT;
759 if (get_user(v, ip + 1))
760 return -EFAULT;
761
762 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
763 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
764
765 return 0;
766
767 case EVIOCRMFF:
768 return input_ff_erase(dev, (int)(unsigned long) p, file);
769
770 case EVIOCGEFFECTS:
771 i = test_bit(EV_FF, dev->evbit) ?
772 dev->ff->max_effects : 0;
773 if (put_user(i, ip))
774 return -EFAULT;
775 return 0;
776
777 case EVIOCGRAB:
778 if (p)
779 return evdev_grab(evdev, client);
780 else
781 return evdev_ungrab(evdev, client);
782
783 case EVIOCSCLOCKID:
784 if (copy_from_user(&i, p, sizeof(unsigned int)))
785 return -EFAULT;
786 if (i != CLOCK_MONOTONIC && i != CLOCK_REALTIME)
787 return -EINVAL;
788 client->clkid = i;
789 return 0;
790
791 case EVIOCGKEYCODE:
792 return evdev_handle_get_keycode(dev, p);
793
794 case EVIOCSKEYCODE:
795 return evdev_handle_set_keycode(dev, p);
796
797 case EVIOCGKEYCODE_V2:
798 return evdev_handle_get_keycode_v2(dev, p);
799
800 case EVIOCSKEYCODE_V2:
801 return evdev_handle_set_keycode_v2(dev, p);
802
803 case EVIOCGSUSPENDBLOCK:
804 return put_user(client->use_wake_lock, ip);
805
806 case EVIOCSSUSPENDBLOCK:
807 if (p)
808 return evdev_enable_suspend_block(evdev, client);
809 else
810 return evdev_disable_suspend_block(evdev, client);
811 }
812
813 size = _IOC_SIZE(cmd);
814
815 /* Now check variable-length commands */
816 #define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
817 switch (EVIOC_MASK_SIZE(cmd)) {
818
819 case EVIOCGPROP(0):
820 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
821 size, p, compat_mode);
822
823 case EVIOCGMTSLOTS(0):
824 return evdev_handle_mt_request(dev, size, ip);
825
826 case EVIOCGKEY(0):
827 return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode);
828
829 case EVIOCGLED(0):
830 return bits_to_user(dev->led, LED_MAX, size, p, compat_mode);
831
832 case EVIOCGSND(0):
833 return bits_to_user(dev->snd, SND_MAX, size, p, compat_mode);
834
835 case EVIOCGSW(0):
836 return bits_to_user(dev->sw, SW_MAX, size, p, compat_mode);
837
838 case EVIOCGNAME(0):
839 return str_to_user(dev->name, size, p);
840
841 case EVIOCGPHYS(0):
842 return str_to_user(dev->phys, size, p);
843
844 case EVIOCGUNIQ(0):
845 return str_to_user(dev->uniq, size, p);
846
847 case EVIOC_MASK_SIZE(EVIOCSFF):
848 if (input_ff_effect_from_user(p, size, &effect))
849 return -EFAULT;
850
851 error = input_ff_upload(dev, &effect, file);
852
853 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
854 return -EFAULT;
855
856 return error;
857 }
858
859 /* Multi-number variable-length handlers */
860 if (_IOC_TYPE(cmd) != 'E')
861 return -EINVAL;
862
863 if (_IOC_DIR(cmd) == _IOC_READ) {
864
865 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
866 return handle_eviocgbit(dev,
867 _IOC_NR(cmd) & EV_MAX, size,
868 p, compat_mode);
869
870 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
871
872 if (!dev->absinfo)
873 return -EINVAL;
874
875 t = _IOC_NR(cmd) & ABS_MAX;
876 abs = dev->absinfo[t];
877
878 if (copy_to_user(p, &abs, min_t(size_t,
879 size, sizeof(struct input_absinfo))))
880 return -EFAULT;
881
882 return 0;
883 }
884 }
885
886 if (_IOC_DIR(cmd) == _IOC_WRITE) {
887
888 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
889
890 if (!dev->absinfo)
891 return -EINVAL;
892
893 t = _IOC_NR(cmd) & ABS_MAX;
894
895 if (copy_from_user(&abs, p, min_t(size_t,
896 size, sizeof(struct input_absinfo))))
897 return -EFAULT;
898
899 if (size < sizeof(struct input_absinfo))
900 abs.resolution = 0;
901
902 /* We can't change number of reserved MT slots */
903 if (t == ABS_MT_SLOT)
904 return -EINVAL;
905
906 /*
907 * Take event lock to ensure that we are not
908 * changing device parameters in the middle
909 * of event.
910 */
911 spin_lock_irq(&dev->event_lock);
912 dev->absinfo[t] = abs;
913 spin_unlock_irq(&dev->event_lock);
914
915 return 0;
916 }
917 }
918
919 return -EINVAL;
920 }
921
evdev_ioctl_handler(struct file * file,unsigned int cmd,void __user * p,int compat_mode)922 static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
923 void __user *p, int compat_mode)
924 {
925 struct evdev_client *client = file->private_data;
926 struct evdev *evdev = client->evdev;
927 int retval;
928
929 retval = mutex_lock_interruptible(&evdev->mutex);
930 if (retval)
931 return retval;
932
933 if (!evdev->exist) {
934 retval = -ENODEV;
935 goto out;
936 }
937
938 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
939
940 out:
941 mutex_unlock(&evdev->mutex);
942 return retval;
943 }
944
evdev_ioctl(struct file * file,unsigned int cmd,unsigned long arg)945 static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
946 {
947 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
948 }
949
950 #ifdef CONFIG_COMPAT
evdev_ioctl_compat(struct file * file,unsigned int cmd,unsigned long arg)951 static long evdev_ioctl_compat(struct file *file,
952 unsigned int cmd, unsigned long arg)
953 {
954 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
955 }
956 #endif
957
958 static const struct file_operations evdev_fops = {
959 .owner = THIS_MODULE,
960 .read = evdev_read,
961 .write = evdev_write,
962 .poll = evdev_poll,
963 .open = evdev_open,
964 .release = evdev_release,
965 .unlocked_ioctl = evdev_ioctl,
966 #ifdef CONFIG_COMPAT
967 .compat_ioctl = evdev_ioctl_compat,
968 #endif
969 .fasync = evdev_fasync,
970 .flush = evdev_flush,
971 .llseek = no_llseek,
972 };
973
974 /*
975 * Mark device non-existent. This disables writes, ioctls and
976 * prevents new users from opening the device. Already posted
977 * blocking reads will stay, however new ones will fail.
978 */
evdev_mark_dead(struct evdev * evdev)979 static void evdev_mark_dead(struct evdev *evdev)
980 {
981 mutex_lock(&evdev->mutex);
982 evdev->exist = false;
983 mutex_unlock(&evdev->mutex);
984 }
985
evdev_cleanup(struct evdev * evdev)986 static void evdev_cleanup(struct evdev *evdev)
987 {
988 struct input_handle *handle = &evdev->handle;
989
990 evdev_mark_dead(evdev);
991 evdev_hangup(evdev);
992
993 cdev_del(&evdev->cdev);
994
995 /* evdev is marked dead so no one else accesses evdev->open */
996 if (evdev->open) {
997 input_flush_device(handle, NULL);
998 input_close_device(handle);
999 }
1000 }
1001
1002 /*
1003 * Create new evdev device. Note that input core serializes calls
1004 * to connect and disconnect.
1005 */
evdev_connect(struct input_handler * handler,struct input_dev * dev,const struct input_device_id * id)1006 static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
1007 const struct input_device_id *id)
1008 {
1009 struct evdev *evdev;
1010 int minor;
1011 int dev_no;
1012 int error;
1013
1014 minor = input_get_new_minor(EVDEV_MINOR_BASE, EVDEV_MINORS, true);
1015 if (minor < 0) {
1016 error = minor;
1017 pr_err("failed to reserve new minor: %d\n", error);
1018 return error;
1019 }
1020
1021 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
1022 if (!evdev) {
1023 error = -ENOMEM;
1024 goto err_free_minor;
1025 }
1026
1027 INIT_LIST_HEAD(&evdev->client_list);
1028 spin_lock_init(&evdev->client_lock);
1029 mutex_init(&evdev->mutex);
1030 init_waitqueue_head(&evdev->wait);
1031 evdev->exist = true;
1032
1033 dev_no = minor;
1034 /* Normalize device number if it falls into legacy range */
1035 if (dev_no < EVDEV_MINOR_BASE + EVDEV_MINORS)
1036 dev_no -= EVDEV_MINOR_BASE;
1037 dev_set_name(&evdev->dev, "event%d", dev_no);
1038
1039 evdev->handle.dev = input_get_device(dev);
1040 evdev->handle.name = dev_name(&evdev->dev);
1041 evdev->handle.handler = handler;
1042 evdev->handle.private = evdev;
1043
1044 evdev->dev.devt = MKDEV(INPUT_MAJOR, minor);
1045 evdev->dev.class = &input_class;
1046 evdev->dev.parent = &dev->dev;
1047 evdev->dev.release = evdev_free;
1048 device_initialize(&evdev->dev);
1049
1050 error = input_register_handle(&evdev->handle);
1051 if (error)
1052 goto err_free_evdev;
1053
1054 cdev_init(&evdev->cdev, &evdev_fops);
1055 evdev->cdev.kobj.parent = &evdev->dev.kobj;
1056 error = cdev_add(&evdev->cdev, evdev->dev.devt, 1);
1057 if (error)
1058 goto err_unregister_handle;
1059
1060 error = device_add(&evdev->dev);
1061 if (error)
1062 goto err_cleanup_evdev;
1063
1064 return 0;
1065
1066 err_cleanup_evdev:
1067 evdev_cleanup(evdev);
1068 err_unregister_handle:
1069 input_unregister_handle(&evdev->handle);
1070 err_free_evdev:
1071 put_device(&evdev->dev);
1072 err_free_minor:
1073 input_free_minor(minor);
1074 return error;
1075 }
1076
evdev_disconnect(struct input_handle * handle)1077 static void evdev_disconnect(struct input_handle *handle)
1078 {
1079 struct evdev *evdev = handle->private;
1080
1081 device_del(&evdev->dev);
1082 evdev_cleanup(evdev);
1083 input_free_minor(MINOR(evdev->dev.devt));
1084 input_unregister_handle(handle);
1085 put_device(&evdev->dev);
1086 }
1087
1088 static const struct input_device_id evdev_ids[] = {
1089 { .driver_info = 1 }, /* Matches all devices */
1090 { }, /* Terminating zero entry */
1091 };
1092
1093 MODULE_DEVICE_TABLE(input, evdev_ids);
1094
1095 static struct input_handler evdev_handler = {
1096 .event = evdev_event,
1097 .events = evdev_events,
1098 .connect = evdev_connect,
1099 .disconnect = evdev_disconnect,
1100 .legacy_minors = true,
1101 .minor = EVDEV_MINOR_BASE,
1102 .name = "evdev",
1103 .id_table = evdev_ids,
1104 };
1105
evdev_init(void)1106 static int __init evdev_init(void)
1107 {
1108 return input_register_handler(&evdev_handler);
1109 }
1110
evdev_exit(void)1111 static void __exit evdev_exit(void)
1112 {
1113 input_unregister_handler(&evdev_handler);
1114 }
1115
1116 module_init(evdev_init);
1117 module_exit(evdev_exit);
1118
1119 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1120 MODULE_DESCRIPTION("Input driver event char devices");
1121 MODULE_LICENSE("GPL");
1122