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