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