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