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