ARM: cpu topology: Add debugfs interface for cpu_power
[cmplus.git] / drivers / input / evdev.c
blobd497c972e714352f121d766fdd7e673a5576cc2e
1 /*
2 * Event char devices, giving access to raw input device events.
4 * Copyright (c) 1999-2002 Vojtech Pavlik
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 */
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #define EVDEV_MINOR_BASE 64
14 #define EVDEV_MINORS 32
15 #define EVDEV_MIN_BUFFER_SIZE 64U
16 #define EVDEV_BUF_PACKETS 8
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.h>
24 #include <linux/major.h>
25 #include <linux/device.h>
26 #include <linux/wakelock.h>
27 #include "input-compat.h"
29 struct 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;
42 struct 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 unsigned int bufsize;
54 struct input_event buffer[];
57 static struct evdev *evdev_table[EVDEV_MINORS];
58 static DEFINE_MUTEX(evdev_table_mutex);
60 static void evdev_pass_event(struct evdev_client *client,
61 struct input_event *event)
63 /* Interrupts are disabled, just acquire the lock. */
64 spin_lock(&client->buffer_lock);
66 client->buffer[client->head++] = *event;
67 client->head &= client->bufsize - 1;
69 if (unlikely(client->head == client->tail)) {
71 * This effectively "drops" all unconsumed events, leaving
72 * EV_SYN/SYN_DROPPED plus the newest event in the queue.
74 client->tail = (client->head - 2) & (client->bufsize - 1);
76 client->buffer[client->tail].time = event->time;
77 client->buffer[client->tail].type = EV_SYN;
78 client->buffer[client->tail].code = SYN_DROPPED;
79 client->buffer[client->tail].value = 0;
81 client->packet_head = client->tail;
82 if (client->use_wake_lock)
83 wake_unlock(&client->wake_lock);
86 if (event->type == EV_SYN && event->code == SYN_REPORT) {
87 client->packet_head = client->head;
88 if (client->use_wake_lock)
89 wake_lock(&client->wake_lock);
90 kill_fasync(&client->fasync, SIGIO, POLL_IN);
93 spin_unlock(&client->buffer_lock);
97 * Pass incoming event to all connected clients.
99 static void evdev_event(struct input_handle *handle,
100 unsigned int type, unsigned int code, int value)
102 struct evdev *evdev = handle->private;
103 struct evdev_client *client;
104 struct input_event event;
105 struct timespec ts;
107 ktime_get_ts(&ts);
108 event.time.tv_sec = ts.tv_sec;
109 event.time.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
110 event.type = type;
111 event.code = code;
112 event.value = value;
114 rcu_read_lock();
116 client = rcu_dereference(evdev->grab);
117 if (client)
118 evdev_pass_event(client, &event);
119 else
120 list_for_each_entry_rcu(client, &evdev->client_list, node)
121 evdev_pass_event(client, &event);
123 rcu_read_unlock();
125 if (type == EV_SYN && code == SYN_REPORT)
126 wake_up_interruptible(&evdev->wait);
129 static int evdev_fasync(int fd, struct file *file, int on)
131 struct evdev_client *client = file->private_data;
133 return fasync_helper(fd, file, on, &client->fasync);
136 static int evdev_flush(struct file *file, fl_owner_t id)
138 struct evdev_client *client = file->private_data;
139 struct evdev *evdev = client->evdev;
140 int retval;
142 retval = mutex_lock_interruptible(&evdev->mutex);
143 if (retval)
144 return retval;
146 if (!evdev->exist)
147 retval = -ENODEV;
148 else
149 retval = input_flush_device(&evdev->handle, file);
151 mutex_unlock(&evdev->mutex);
152 return retval;
155 static void evdev_free(struct device *dev)
157 struct evdev *evdev = container_of(dev, struct evdev, dev);
159 input_put_device(evdev->handle.dev);
160 kfree(evdev);
164 * Grabs an event device (along with underlying input device).
165 * This function is called with evdev->mutex taken.
167 static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
169 int error;
171 if (evdev->grab)
172 return -EBUSY;
174 error = input_grab_device(&evdev->handle);
175 if (error)
176 return error;
178 rcu_assign_pointer(evdev->grab, client);
180 return 0;
183 static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
185 if (evdev->grab != client)
186 return -EINVAL;
188 rcu_assign_pointer(evdev->grab, NULL);
189 synchronize_rcu();
190 input_release_device(&evdev->handle);
192 return 0;
195 static void evdev_attach_client(struct evdev *evdev,
196 struct evdev_client *client)
198 spin_lock(&evdev->client_lock);
199 list_add_tail_rcu(&client->node, &evdev->client_list);
200 spin_unlock(&evdev->client_lock);
203 static void evdev_detach_client(struct evdev *evdev,
204 struct evdev_client *client)
206 spin_lock(&evdev->client_lock);
207 list_del_rcu(&client->node);
208 spin_unlock(&evdev->client_lock);
209 synchronize_rcu();
212 static int evdev_open_device(struct evdev *evdev)
214 int retval;
216 retval = mutex_lock_interruptible(&evdev->mutex);
217 if (retval)
218 return retval;
220 if (!evdev->exist)
221 retval = -ENODEV;
222 else if (!evdev->open++) {
223 retval = input_open_device(&evdev->handle);
224 if (retval)
225 evdev->open--;
228 mutex_unlock(&evdev->mutex);
229 return retval;
232 static void evdev_close_device(struct evdev *evdev)
234 mutex_lock(&evdev->mutex);
236 if (evdev->exist && !--evdev->open)
237 input_close_device(&evdev->handle);
239 mutex_unlock(&evdev->mutex);
243 * Wake up users waiting for IO so they can disconnect from
244 * dead device.
246 static void evdev_hangup(struct evdev *evdev)
248 struct evdev_client *client;
250 spin_lock(&evdev->client_lock);
251 list_for_each_entry(client, &evdev->client_list, node)
252 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
253 spin_unlock(&evdev->client_lock);
255 wake_up_interruptible(&evdev->wait);
258 static int evdev_release(struct inode *inode, struct file *file)
260 struct evdev_client *client = file->private_data;
261 struct evdev *evdev = client->evdev;
263 mutex_lock(&evdev->mutex);
264 if (evdev->grab == client)
265 evdev_ungrab(evdev, client);
266 mutex_unlock(&evdev->mutex);
268 evdev_detach_client(evdev, client);
269 if (client->use_wake_lock)
270 wake_lock_destroy(&client->wake_lock);
271 kfree(client);
273 evdev_close_device(evdev);
274 put_device(&evdev->dev);
276 return 0;
279 static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
281 unsigned int n_events =
282 max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
283 EVDEV_MIN_BUFFER_SIZE);
285 return roundup_pow_of_two(n_events);
288 static int evdev_open(struct inode *inode, struct file *file)
290 struct evdev *evdev;
291 struct evdev_client *client;
292 int i = iminor(inode) - EVDEV_MINOR_BASE;
293 unsigned int bufsize;
294 int error;
296 if (i >= EVDEV_MINORS)
297 return -ENODEV;
299 error = mutex_lock_interruptible(&evdev_table_mutex);
300 if (error)
301 return error;
302 evdev = evdev_table[i];
303 if (evdev)
304 get_device(&evdev->dev);
305 mutex_unlock(&evdev_table_mutex);
307 if (!evdev)
308 return -ENODEV;
310 bufsize = evdev_compute_buffer_size(evdev->handle.dev);
312 client = kzalloc(sizeof(struct evdev_client) +
313 bufsize * sizeof(struct input_event),
314 GFP_KERNEL);
315 if (!client) {
316 error = -ENOMEM;
317 goto err_put_evdev;
320 client->bufsize = bufsize;
321 spin_lock_init(&client->buffer_lock);
322 snprintf(client->name, sizeof(client->name), "%s-%d",
323 dev_name(&evdev->dev), task_tgid_vnr(current));
324 client->evdev = evdev;
325 evdev_attach_client(evdev, client);
327 error = evdev_open_device(evdev);
328 if (error)
329 goto err_free_client;
331 file->private_data = client;
332 nonseekable_open(inode, file);
334 return 0;
336 err_free_client:
337 evdev_detach_client(evdev, client);
338 kfree(client);
339 err_put_evdev:
340 put_device(&evdev->dev);
341 return error;
344 static ssize_t evdev_write(struct file *file, const char __user *buffer,
345 size_t count, loff_t *ppos)
347 struct evdev_client *client = file->private_data;
348 struct evdev *evdev = client->evdev;
349 struct input_event event;
350 int retval;
352 if (count < input_event_size())
353 return -EINVAL;
355 retval = mutex_lock_interruptible(&evdev->mutex);
356 if (retval)
357 return retval;
359 if (!evdev->exist) {
360 retval = -ENODEV;
361 goto out;
364 do {
365 if (input_event_from_user(buffer + retval, &event)) {
366 retval = -EFAULT;
367 goto out;
369 retval += input_event_size();
371 input_inject_event(&evdev->handle,
372 event.type, event.code, event.value);
373 } while (retval + input_event_size() <= count);
375 out:
376 mutex_unlock(&evdev->mutex);
377 return retval;
380 static int evdev_fetch_next_event(struct evdev_client *client,
381 struct input_event *event)
383 int have_event;
385 spin_lock_irq(&client->buffer_lock);
387 have_event = client->packet_head != client->tail;
388 if (have_event) {
389 *event = client->buffer[client->tail++];
390 client->tail &= client->bufsize - 1;
391 if (client->use_wake_lock &&
392 client->packet_head == client->tail)
393 wake_unlock(&client->wake_lock);
396 spin_unlock_irq(&client->buffer_lock);
398 return have_event;
401 static ssize_t evdev_read(struct file *file, char __user *buffer,
402 size_t count, loff_t *ppos)
404 struct evdev_client *client = file->private_data;
405 struct evdev *evdev = client->evdev;
406 struct input_event event;
407 int retval;
409 if (count < input_event_size())
410 return -EINVAL;
412 if (!(file->f_flags & O_NONBLOCK)) {
413 retval = wait_event_interruptible(evdev->wait,
414 client->packet_head != client->tail || !evdev->exist);
415 if (retval)
416 return retval;
419 if (!evdev->exist)
420 return -ENODEV;
422 while (retval + input_event_size() <= count &&
423 evdev_fetch_next_event(client, &event)) {
425 if (input_event_to_user(buffer + retval, &event))
426 return -EFAULT;
428 retval += input_event_size();
431 if (retval == 0 && file->f_flags & O_NONBLOCK)
432 retval = -EAGAIN;
433 return retval;
436 /* No kernel lock - fine */
437 static unsigned int evdev_poll(struct file *file, poll_table *wait)
439 struct evdev_client *client = file->private_data;
440 struct evdev *evdev = client->evdev;
441 unsigned int mask;
443 poll_wait(file, &evdev->wait, wait);
445 mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR;
446 if (client->packet_head != client->tail)
447 mask |= POLLIN | POLLRDNORM;
449 return mask;
452 #ifdef CONFIG_COMPAT
454 #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
455 #define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
457 #ifdef __BIG_ENDIAN
458 static int bits_to_user(unsigned long *bits, unsigned int maxbit,
459 unsigned int maxlen, void __user *p, int compat)
461 int len, i;
463 if (compat) {
464 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
465 if (len > maxlen)
466 len = maxlen;
468 for (i = 0; i < len / sizeof(compat_long_t); i++)
469 if (copy_to_user((compat_long_t __user *) p + i,
470 (compat_long_t *) bits +
471 i + 1 - ((i % 2) << 1),
472 sizeof(compat_long_t)))
473 return -EFAULT;
474 } else {
475 len = BITS_TO_LONGS(maxbit) * sizeof(long);
476 if (len > maxlen)
477 len = maxlen;
479 if (copy_to_user(p, bits, len))
480 return -EFAULT;
483 return len;
485 #else
486 static int bits_to_user(unsigned long *bits, unsigned int maxbit,
487 unsigned int maxlen, void __user *p, int compat)
489 int len = compat ?
490 BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
491 BITS_TO_LONGS(maxbit) * sizeof(long);
493 if (len > maxlen)
494 len = maxlen;
496 return copy_to_user(p, bits, len) ? -EFAULT : len;
498 #endif /* __BIG_ENDIAN */
500 #else
502 static int bits_to_user(unsigned long *bits, unsigned int maxbit,
503 unsigned int maxlen, void __user *p, int compat)
505 int len = BITS_TO_LONGS(maxbit) * sizeof(long);
507 if (len > maxlen)
508 len = maxlen;
510 return copy_to_user(p, bits, len) ? -EFAULT : len;
513 #endif /* CONFIG_COMPAT */
515 static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
517 int len;
519 if (!str)
520 return -ENOENT;
522 len = strlen(str) + 1;
523 if (len > maxlen)
524 len = maxlen;
526 return copy_to_user(p, str, len) ? -EFAULT : len;
529 #define OLD_KEY_MAX 0x1ff
530 static int handle_eviocgbit(struct input_dev *dev,
531 unsigned int type, unsigned int size,
532 void __user *p, int compat_mode)
534 static unsigned long keymax_warn_time;
535 unsigned long *bits;
536 int len;
538 switch (type) {
540 case 0: bits = dev->evbit; len = EV_MAX; break;
541 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
542 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
543 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
544 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
545 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
546 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
547 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
548 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
549 default: return -EINVAL;
553 * Work around bugs in userspace programs that like to do
554 * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
555 * should be in bytes, not in bits.
557 if (type == EV_KEY && size == OLD_KEY_MAX) {
558 len = OLD_KEY_MAX;
559 if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
560 pr_warning("(EVIOCGBIT): Suspicious buffer size %u, "
561 "limiting output to %zu bytes. See "
562 "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
563 OLD_KEY_MAX,
564 BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
567 return bits_to_user(bits, len, size, p, compat_mode);
569 #undef OLD_KEY_MAX
571 static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
573 struct input_keymap_entry ke = {
574 .len = sizeof(unsigned int),
575 .flags = 0,
577 int __user *ip = (int __user *)p;
578 int error;
580 /* legacy case */
581 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
582 return -EFAULT;
584 error = input_get_keycode(dev, &ke);
585 if (error)
586 return error;
588 if (put_user(ke.keycode, ip + 1))
589 return -EFAULT;
591 return 0;
594 static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
596 struct input_keymap_entry ke;
597 int error;
599 if (copy_from_user(&ke, p, sizeof(ke)))
600 return -EFAULT;
602 error = input_get_keycode(dev, &ke);
603 if (error)
604 return error;
606 if (copy_to_user(p, &ke, sizeof(ke)))
607 return -EFAULT;
609 return 0;
612 static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
614 struct input_keymap_entry ke = {
615 .len = sizeof(unsigned int),
616 .flags = 0,
618 int __user *ip = (int __user *)p;
620 if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
621 return -EFAULT;
623 if (get_user(ke.keycode, ip + 1))
624 return -EFAULT;
626 return input_set_keycode(dev, &ke);
629 static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
631 struct input_keymap_entry ke;
633 if (copy_from_user(&ke, p, sizeof(ke)))
634 return -EFAULT;
636 if (ke.len > sizeof(ke.scancode))
637 return -EINVAL;
639 return input_set_keycode(dev, &ke);
642 static int evdev_enable_suspend_block(struct evdev *evdev,
643 struct evdev_client *client)
645 if (client->use_wake_lock)
646 return 0;
648 spin_lock_irq(&client->buffer_lock);
649 wake_lock_init(&client->wake_lock, WAKE_LOCK_SUSPEND, client->name);
650 client->use_wake_lock = true;
651 if (client->packet_head != client->tail)
652 wake_lock(&client->wake_lock);
653 spin_unlock_irq(&client->buffer_lock);
654 return 0;
657 static int evdev_disable_suspend_block(struct evdev *evdev,
658 struct evdev_client *client)
660 if (!client->use_wake_lock)
661 return 0;
663 spin_lock_irq(&client->buffer_lock);
664 client->use_wake_lock = false;
665 wake_lock_destroy(&client->wake_lock);
666 spin_unlock_irq(&client->buffer_lock);
668 return 0;
671 static long evdev_do_ioctl(struct file *file, unsigned int cmd,
672 void __user *p, int compat_mode)
674 struct evdev_client *client = file->private_data;
675 struct evdev *evdev = client->evdev;
676 struct input_dev *dev = evdev->handle.dev;
677 struct input_absinfo abs;
678 struct ff_effect effect;
679 int __user *ip = (int __user *)p;
680 unsigned int i, t, u, v;
681 unsigned int size;
682 int error;
684 /* First we check for fixed-length commands */
685 switch (cmd) {
687 case EVIOCGVERSION:
688 return put_user(EV_VERSION, ip);
690 case EVIOCGID:
691 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
692 return -EFAULT;
693 return 0;
695 case EVIOCGREP:
696 if (!test_bit(EV_REP, dev->evbit))
697 return -ENOSYS;
698 if (put_user(dev->rep[REP_DELAY], ip))
699 return -EFAULT;
700 if (put_user(dev->rep[REP_PERIOD], ip + 1))
701 return -EFAULT;
702 return 0;
704 case EVIOCSREP:
705 if (!test_bit(EV_REP, dev->evbit))
706 return -ENOSYS;
707 if (get_user(u, ip))
708 return -EFAULT;
709 if (get_user(v, ip + 1))
710 return -EFAULT;
712 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
713 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
715 return 0;
717 case EVIOCRMFF:
718 return input_ff_erase(dev, (int)(unsigned long) p, file);
720 case EVIOCGEFFECTS:
721 i = test_bit(EV_FF, dev->evbit) ?
722 dev->ff->max_effects : 0;
723 if (put_user(i, ip))
724 return -EFAULT;
725 return 0;
727 case EVIOCGRAB:
728 if (p)
729 return evdev_grab(evdev, client);
730 else
731 return evdev_ungrab(evdev, client);
733 case EVIOCGKEYCODE:
734 return evdev_handle_get_keycode(dev, p);
736 case EVIOCSKEYCODE:
737 return evdev_handle_set_keycode(dev, p);
739 case EVIOCGKEYCODE_V2:
740 return evdev_handle_get_keycode_v2(dev, p);
742 case EVIOCSKEYCODE_V2:
743 return evdev_handle_set_keycode_v2(dev, p);
745 case EVIOCGSUSPENDBLOCK:
746 return put_user(client->use_wake_lock, ip);
748 case EVIOCSSUSPENDBLOCK:
749 if (p)
750 return evdev_enable_suspend_block(evdev, client);
751 else
752 return evdev_disable_suspend_block(evdev, client);
755 size = _IOC_SIZE(cmd);
757 /* Now check variable-length commands */
758 #define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
759 switch (EVIOC_MASK_SIZE(cmd)) {
761 case EVIOCGPROP(0):
762 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
763 size, p, compat_mode);
765 case EVIOCGKEY(0):
766 return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode);
768 case EVIOCGLED(0):
769 return bits_to_user(dev->led, LED_MAX, size, p, compat_mode);
771 case EVIOCGSND(0):
772 return bits_to_user(dev->snd, SND_MAX, size, p, compat_mode);
774 case EVIOCGSW(0):
775 return bits_to_user(dev->sw, SW_MAX, size, p, compat_mode);
777 case EVIOCGNAME(0):
778 return str_to_user(dev->name, size, p);
780 case EVIOCGPHYS(0):
781 return str_to_user(dev->phys, size, p);
783 case EVIOCGUNIQ(0):
784 return str_to_user(dev->uniq, size, p);
786 case EVIOC_MASK_SIZE(EVIOCSFF):
787 if (input_ff_effect_from_user(p, size, &effect))
788 return -EFAULT;
790 error = input_ff_upload(dev, &effect, file);
792 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
793 return -EFAULT;
795 return error;
798 /* Multi-number variable-length handlers */
799 if (_IOC_TYPE(cmd) != 'E')
800 return -EINVAL;
802 if (_IOC_DIR(cmd) == _IOC_READ) {
804 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
805 return handle_eviocgbit(dev,
806 _IOC_NR(cmd) & EV_MAX, size,
807 p, compat_mode);
809 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
811 if (!dev->absinfo)
812 return -EINVAL;
814 t = _IOC_NR(cmd) & ABS_MAX;
815 abs = dev->absinfo[t];
817 if (copy_to_user(p, &abs, min_t(size_t,
818 size, sizeof(struct input_absinfo))))
819 return -EFAULT;
821 return 0;
825 if (_IOC_DIR(cmd) == _IOC_WRITE) {
827 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
829 if (!dev->absinfo)
830 return -EINVAL;
832 t = _IOC_NR(cmd) & ABS_MAX;
834 if (copy_from_user(&abs, p, min_t(size_t,
835 size, sizeof(struct input_absinfo))))
836 return -EFAULT;
838 if (size < sizeof(struct input_absinfo))
839 abs.resolution = 0;
841 /* We can't change number of reserved MT slots */
842 if (t == ABS_MT_SLOT)
843 return -EINVAL;
846 * Take event lock to ensure that we are not
847 * changing device parameters in the middle
848 * of event.
850 spin_lock_irq(&dev->event_lock);
851 dev->absinfo[t] = abs;
852 spin_unlock_irq(&dev->event_lock);
854 return 0;
858 return -EINVAL;
861 static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
862 void __user *p, int compat_mode)
864 struct evdev_client *client = file->private_data;
865 struct evdev *evdev = client->evdev;
866 int retval;
868 retval = mutex_lock_interruptible(&evdev->mutex);
869 if (retval)
870 return retval;
872 if (!evdev->exist) {
873 retval = -ENODEV;
874 goto out;
877 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
879 out:
880 mutex_unlock(&evdev->mutex);
881 return retval;
884 static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
886 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
889 #ifdef CONFIG_COMPAT
890 static long evdev_ioctl_compat(struct file *file,
891 unsigned int cmd, unsigned long arg)
893 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
895 #endif
897 static const struct file_operations evdev_fops = {
898 .owner = THIS_MODULE,
899 .read = evdev_read,
900 .write = evdev_write,
901 .poll = evdev_poll,
902 .open = evdev_open,
903 .release = evdev_release,
904 .unlocked_ioctl = evdev_ioctl,
905 #ifdef CONFIG_COMPAT
906 .compat_ioctl = evdev_ioctl_compat,
907 #endif
908 .fasync = evdev_fasync,
909 .flush = evdev_flush,
910 .llseek = no_llseek,
913 static int evdev_install_chrdev(struct evdev *evdev)
916 * No need to do any locking here as calls to connect and
917 * disconnect are serialized by the input core
919 evdev_table[evdev->minor] = evdev;
920 return 0;
923 static void evdev_remove_chrdev(struct evdev *evdev)
926 * Lock evdev table to prevent race with evdev_open()
928 mutex_lock(&evdev_table_mutex);
929 evdev_table[evdev->minor] = NULL;
930 mutex_unlock(&evdev_table_mutex);
934 * Mark device non-existent. This disables writes, ioctls and
935 * prevents new users from opening the device. Already posted
936 * blocking reads will stay, however new ones will fail.
938 static void evdev_mark_dead(struct evdev *evdev)
940 mutex_lock(&evdev->mutex);
941 evdev->exist = false;
942 mutex_unlock(&evdev->mutex);
945 static void evdev_cleanup(struct evdev *evdev)
947 struct input_handle *handle = &evdev->handle;
949 evdev_mark_dead(evdev);
950 evdev_hangup(evdev);
951 evdev_remove_chrdev(evdev);
953 /* evdev is marked dead so no one else accesses evdev->open */
954 if (evdev->open) {
955 input_flush_device(handle, NULL);
956 input_close_device(handle);
961 * Create new evdev device. Note that input core serializes calls
962 * to connect and disconnect so we don't need to lock evdev_table here.
964 static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
965 const struct input_device_id *id)
967 struct evdev *evdev;
968 int minor;
969 int error;
971 for (minor = 0; minor < EVDEV_MINORS; minor++)
972 if (!evdev_table[minor])
973 break;
975 if (minor == EVDEV_MINORS) {
976 pr_err("no more free evdev devices\n");
977 return -ENFILE;
980 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
981 if (!evdev)
982 return -ENOMEM;
984 INIT_LIST_HEAD(&evdev->client_list);
985 spin_lock_init(&evdev->client_lock);
986 mutex_init(&evdev->mutex);
987 init_waitqueue_head(&evdev->wait);
989 dev_set_name(&evdev->dev, "event%d", minor);
990 evdev->exist = true;
991 evdev->minor = minor;
993 evdev->handle.dev = input_get_device(dev);
994 evdev->handle.name = dev_name(&evdev->dev);
995 evdev->handle.handler = handler;
996 evdev->handle.private = evdev;
998 evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
999 evdev->dev.class = &input_class;
1000 evdev->dev.parent = &dev->dev;
1001 evdev->dev.release = evdev_free;
1002 device_initialize(&evdev->dev);
1004 error = input_register_handle(&evdev->handle);
1005 if (error)
1006 goto err_free_evdev;
1008 error = evdev_install_chrdev(evdev);
1009 if (error)
1010 goto err_unregister_handle;
1012 error = device_add(&evdev->dev);
1013 if (error)
1014 goto err_cleanup_evdev;
1016 return 0;
1018 err_cleanup_evdev:
1019 evdev_cleanup(evdev);
1020 err_unregister_handle:
1021 input_unregister_handle(&evdev->handle);
1022 err_free_evdev:
1023 put_device(&evdev->dev);
1024 return error;
1027 static void evdev_disconnect(struct input_handle *handle)
1029 struct evdev *evdev = handle->private;
1031 device_del(&evdev->dev);
1032 evdev_cleanup(evdev);
1033 input_unregister_handle(handle);
1034 put_device(&evdev->dev);
1037 static const struct input_device_id evdev_ids[] = {
1038 { .driver_info = 1 }, /* Matches all devices */
1039 { }, /* Terminating zero entry */
1042 MODULE_DEVICE_TABLE(input, evdev_ids);
1044 static struct input_handler evdev_handler = {
1045 .event = evdev_event,
1046 .connect = evdev_connect,
1047 .disconnect = evdev_disconnect,
1048 .fops = &evdev_fops,
1049 .minor = EVDEV_MINOR_BASE,
1050 .name = "evdev",
1051 .id_table = evdev_ids,
1054 static int __init evdev_init(void)
1056 return input_register_handler(&evdev_handler);
1059 static void __exit evdev_exit(void)
1061 input_unregister_handler(&evdev_handler);
1064 module_init(evdev_init);
1065 module_exit(evdev_exit);
1067 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1068 MODULE_DESCRIPTION("Input driver event char devices");
1069 MODULE_LICENSE("GPL");