aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/evdev.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/input/evdev.c')
-rw-r--r--drivers/input/evdev.c190
1 files changed, 137 insertions, 53 deletions
diff --git a/drivers/input/evdev.c b/drivers/input/evdev.c
index 9ddafc30f432..4cf25347b015 100644
--- a/drivers/input/evdev.c
+++ b/drivers/input/evdev.c
@@ -8,6 +8,8 @@
8 * the Free Software Foundation. 8 * the Free Software Foundation.
9 */ 9 */
10 10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
11#define EVDEV_MINOR_BASE 64 13#define EVDEV_MINOR_BASE 64
12#define EVDEV_MINORS 32 14#define EVDEV_MINORS 32
13#define EVDEV_MIN_BUFFER_SIZE 64U 15#define EVDEV_MIN_BUFFER_SIZE 64U
@@ -28,7 +30,7 @@ struct evdev {
28 int minor; 30 int minor;
29 struct input_handle handle; 31 struct input_handle handle;
30 wait_queue_head_t wait; 32 wait_queue_head_t wait;
31 struct evdev_client *grab; 33 struct evdev_client __rcu *grab;
32 struct list_head client_list; 34 struct list_head client_list;
33 spinlock_t client_lock; /* protects client_list */ 35 spinlock_t client_lock; /* protects client_list */
34 struct mutex mutex; 36 struct mutex mutex;
@@ -37,13 +39,14 @@ struct evdev {
37}; 39};
38 40
39struct evdev_client { 41struct evdev_client {
40 int head; 42 unsigned int head;
41 int tail; 43 unsigned int tail;
44 unsigned int packet_head; /* [future] position of the first element of next packet */
42 spinlock_t buffer_lock; /* protects access to buffer, head and tail */ 45 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
43 struct fasync_struct *fasync; 46 struct fasync_struct *fasync;
44 struct evdev *evdev; 47 struct evdev *evdev;
45 struct list_head node; 48 struct list_head node;
46 int bufsize; 49 unsigned int bufsize;
47 struct input_event buffer[]; 50 struct input_event buffer[];
48}; 51};
49 52
@@ -53,20 +56,33 @@ static DEFINE_MUTEX(evdev_table_mutex);
53static void evdev_pass_event(struct evdev_client *client, 56static void evdev_pass_event(struct evdev_client *client,
54 struct input_event *event) 57 struct input_event *event)
55{ 58{
56 /* 59 /* Interrupts are disabled, just acquire the lock. */
57 * Interrupts are disabled, just acquire the lock.
58 * Make sure we don't leave with the client buffer
59 * "empty" by having client->head == client->tail.
60 */
61 spin_lock(&client->buffer_lock); 60 spin_lock(&client->buffer_lock);
62 do {
63 client->buffer[client->head++] = *event;
64 client->head &= client->bufsize - 1;
65 } while (client->head == client->tail);
66 spin_unlock(&client->buffer_lock);
67 61
68 if (event->type == EV_SYN) 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 }
79
80 if (event->type == EV_SYN && event->code == SYN_REPORT) {
81 client->packet_head = client->head;
69 kill_fasync(&client->fasync, SIGIO, POLL_IN); 82 kill_fasync(&client->fasync, SIGIO, POLL_IN);
83 }
84
85 spin_unlock(&client->buffer_lock);
70} 86}
71 87
72/* 88/*
@@ -95,7 +111,8 @@ static void evdev_event(struct input_handle *handle,
95 111
96 rcu_read_unlock(); 112 rcu_read_unlock();
97 113
98 wake_up_interruptible(&evdev->wait); 114 if (type == EV_SYN && code == SYN_REPORT)
115 wake_up_interruptible(&evdev->wait);
99} 116}
100 117
101static int evdev_fasync(int fd, struct file *file, int on) 118static int evdev_fasync(int fd, struct file *file, int on)
@@ -148,7 +165,6 @@ static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
148 return error; 165 return error;
149 166
150 rcu_assign_pointer(evdev->grab, client); 167 rcu_assign_pointer(evdev->grab, client);
151 synchronize_rcu();
152 168
153 return 0; 169 return 0;
154} 170}
@@ -171,7 +187,6 @@ static void evdev_attach_client(struct evdev *evdev,
171 spin_lock(&evdev->client_lock); 187 spin_lock(&evdev->client_lock);
172 list_add_tail_rcu(&client->node, &evdev->client_list); 188 list_add_tail_rcu(&client->node, &evdev->client_list);
173 spin_unlock(&evdev->client_lock); 189 spin_unlock(&evdev->client_lock);
174 synchronize_rcu();
175} 190}
176 191
177static void evdev_detach_client(struct evdev *evdev, 192static void evdev_detach_client(struct evdev *evdev,
@@ -319,6 +334,9 @@ static ssize_t evdev_write(struct file *file, const char __user *buffer,
319 struct input_event event; 334 struct input_event event;
320 int retval; 335 int retval;
321 336
337 if (count < input_event_size())
338 return -EINVAL;
339
322 retval = mutex_lock_interruptible(&evdev->mutex); 340 retval = mutex_lock_interruptible(&evdev->mutex);
323 if (retval) 341 if (retval)
324 return retval; 342 return retval;
@@ -328,17 +346,16 @@ static ssize_t evdev_write(struct file *file, const char __user *buffer,
328 goto out; 346 goto out;
329 } 347 }
330 348
331 while (retval < count) { 349 do {
332
333 if (input_event_from_user(buffer + retval, &event)) { 350 if (input_event_from_user(buffer + retval, &event)) {
334 retval = -EFAULT; 351 retval = -EFAULT;
335 goto out; 352 goto out;
336 } 353 }
354 retval += input_event_size();
337 355
338 input_inject_event(&evdev->handle, 356 input_inject_event(&evdev->handle,
339 event.type, event.code, event.value); 357 event.type, event.code, event.value);
340 retval += input_event_size(); 358 } while (retval + input_event_size() <= count);
341 }
342 359
343 out: 360 out:
344 mutex_unlock(&evdev->mutex); 361 mutex_unlock(&evdev->mutex);
@@ -374,12 +391,12 @@ static ssize_t evdev_read(struct file *file, char __user *buffer,
374 if (count < input_event_size()) 391 if (count < input_event_size())
375 return -EINVAL; 392 return -EINVAL;
376 393
377 if (client->head == client->tail && evdev->exist && 394 if (client->packet_head == client->tail && evdev->exist &&
378 (file->f_flags & O_NONBLOCK)) 395 (file->f_flags & O_NONBLOCK))
379 return -EAGAIN; 396 return -EAGAIN;
380 397
381 retval = wait_event_interruptible(evdev->wait, 398 retval = wait_event_interruptible(evdev->wait,
382 client->head != client->tail || !evdev->exist); 399 client->packet_head != client->tail || !evdev->exist);
383 if (retval) 400 if (retval)
384 return retval; 401 return retval;
385 402
@@ -408,7 +425,7 @@ static unsigned int evdev_poll(struct file *file, poll_table *wait)
408 poll_wait(file, &evdev->wait, wait); 425 poll_wait(file, &evdev->wait, wait);
409 426
410 mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR; 427 mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR;
411 if (client->head != client->tail) 428 if (client->packet_head != client->tail)
412 mask |= POLLIN | POLLRDNORM; 429 mask |= POLLIN | POLLRDNORM;
413 430
414 return mask; 431 return mask;
@@ -522,18 +539,88 @@ static int handle_eviocgbit(struct input_dev *dev,
522 if (type == EV_KEY && size == OLD_KEY_MAX) { 539 if (type == EV_KEY && size == OLD_KEY_MAX) {
523 len = OLD_KEY_MAX; 540 len = OLD_KEY_MAX;
524 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000)) 541 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
525 printk(KERN_WARNING 542 pr_warning("(EVIOCGBIT): Suspicious buffer size %u, "
526 "evdev.c(EVIOCGBIT): Suspicious buffer size %u, " 543 "limiting output to %zu bytes. See "
527 "limiting output to %zu bytes. See " 544 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
528 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n", 545 OLD_KEY_MAX,
529 OLD_KEY_MAX, 546 BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
530 BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
531 } 547 }
532 548
533 return bits_to_user(bits, len, size, p, compat_mode); 549 return bits_to_user(bits, len, size, p, compat_mode);
534} 550}
535#undef OLD_KEY_MAX 551#undef OLD_KEY_MAX
536 552
553static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
554{
555 struct input_keymap_entry ke = {
556 .len = sizeof(unsigned int),
557 .flags = 0,
558 };
559 int __user *ip = (int __user *)p;
560 int error;
561
562 /* legacy case */
563 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
564 return -EFAULT;
565
566 error = input_get_keycode(dev, &ke);
567 if (error)
568 return error;
569
570 if (put_user(ke.keycode, ip + 1))
571 return -EFAULT;
572
573 return 0;
574}
575
576static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
577{
578 struct input_keymap_entry ke;
579 int error;
580
581 if (copy_from_user(&ke, p, sizeof(ke)))
582 return -EFAULT;
583
584 error = input_get_keycode(dev, &ke);
585 if (error)
586 return error;
587
588 if (copy_to_user(p, &ke, sizeof(ke)))
589 return -EFAULT;
590
591 return 0;
592}
593
594static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
595{
596 struct input_keymap_entry ke = {
597 .len = sizeof(unsigned int),
598 .flags = 0,
599 };
600 int __user *ip = (int __user *)p;
601
602 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
603 return -EFAULT;
604
605 if (get_user(ke.keycode, ip + 1))
606 return -EFAULT;
607
608 return input_set_keycode(dev, &ke);
609}
610
611static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
612{
613 struct input_keymap_entry ke;
614
615 if (copy_from_user(&ke, p, sizeof(ke)))
616 return -EFAULT;
617
618 if (ke.len > sizeof(ke.scancode))
619 return -EINVAL;
620
621 return input_set_keycode(dev, &ke);
622}
623
537static long evdev_do_ioctl(struct file *file, unsigned int cmd, 624static long evdev_do_ioctl(struct file *file, unsigned int cmd,
538 void __user *p, int compat_mode) 625 void __user *p, int compat_mode)
539{ 626{
@@ -580,25 +667,6 @@ static long evdev_do_ioctl(struct file *file, unsigned int cmd,
580 667
581 return 0; 668 return 0;
582 669
583 case EVIOCGKEYCODE:
584 if (get_user(t, ip))
585 return -EFAULT;
586
587 error = input_get_keycode(dev, t, &v);
588 if (error)
589 return error;
590
591 if (put_user(v, ip + 1))
592 return -EFAULT;
593
594 return 0;
595
596 case EVIOCSKEYCODE:
597 if (get_user(t, ip) || get_user(v, ip + 1))
598 return -EFAULT;
599
600 return input_set_keycode(dev, t, v);
601
602 case EVIOCRMFF: 670 case EVIOCRMFF:
603 return input_ff_erase(dev, (int)(unsigned long) p, file); 671 return input_ff_erase(dev, (int)(unsigned long) p, file);
604 672
@@ -614,15 +682,30 @@ static long evdev_do_ioctl(struct file *file, unsigned int cmd,
614 return evdev_grab(evdev, client); 682 return evdev_grab(evdev, client);
615 else 683 else
616 return evdev_ungrab(evdev, client); 684 return evdev_ungrab(evdev, client);
685
686 case EVIOCGKEYCODE:
687 return evdev_handle_get_keycode(dev, p);
688
689 case EVIOCSKEYCODE:
690 return evdev_handle_set_keycode(dev, p);
691
692 case EVIOCGKEYCODE_V2:
693 return evdev_handle_get_keycode_v2(dev, p);
694
695 case EVIOCSKEYCODE_V2:
696 return evdev_handle_set_keycode_v2(dev, p);
617 } 697 }
618 698
619 size = _IOC_SIZE(cmd); 699 size = _IOC_SIZE(cmd);
620 700
621 /* Now check variable-length commands */ 701 /* Now check variable-length commands */
622#define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT)) 702#define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
623
624 switch (EVIOC_MASK_SIZE(cmd)) { 703 switch (EVIOC_MASK_SIZE(cmd)) {
625 704
705 case EVIOCGPROP(0):
706 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
707 size, p, compat_mode);
708
626 case EVIOCGKEY(0): 709 case EVIOCGKEY(0):
627 return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode); 710 return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode);
628 711
@@ -767,7 +850,8 @@ static const struct file_operations evdev_fops = {
767 .compat_ioctl = evdev_ioctl_compat, 850 .compat_ioctl = evdev_ioctl_compat,
768#endif 851#endif
769 .fasync = evdev_fasync, 852 .fasync = evdev_fasync,
770 .flush = evdev_flush 853 .flush = evdev_flush,
854 .llseek = no_llseek,
771}; 855};
772 856
773static int evdev_install_chrdev(struct evdev *evdev) 857static int evdev_install_chrdev(struct evdev *evdev)
@@ -833,7 +917,7 @@ static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
833 break; 917 break;
834 918
835 if (minor == EVDEV_MINORS) { 919 if (minor == EVDEV_MINORS) {
836 printk(KERN_ERR "evdev: no more free evdev devices\n"); 920 pr_err("no more free evdev devices\n");
837 return -ENFILE; 921 return -ENFILE;
838 } 922 }
839 923