tegra: Make tegra_vi01_device accessible
[linux-2.6.git] / drivers / input / evdev.c
1 /*
2  * Event char devices, giving access to raw input device events.
3  *
4  * Copyright (c) 1999-2002 Vojtech Pavlik
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published by
8  * the Free Software Foundation.
9  */
10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13 #define EVDEV_MINOR_BASE        64
14 #define EVDEV_MINORS            32
15 #define EVDEV_MIN_BUFFER_SIZE   64U
16 #define EVDEV_BUF_PACKETS       8
17
18 #include <linux/poll.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/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"
28
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;
40 };
41
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[];
55 };
56
57 static struct evdev *evdev_table[EVDEV_MINORS];
58 static DEFINE_MUTEX(evdev_table_mutex);
59
60 static void evdev_pass_event(struct evdev_client *client,
61                              struct input_event *event)
62 {
63         /* Interrupts are disabled, just acquire the lock. */
64         spin_lock(&client->buffer_lock);
65
66         client->buffer[client->head++] = *event;
67         client->head &= client->bufsize - 1;
68
69         if (unlikely(client->head == client->tail)) {
70                 /*
71                  * This effectively "drops" all unconsumed events, leaving
72                  * EV_SYN/SYN_DROPPED plus the newest event in the queue.
73                  */
74                 client->tail = (client->head - 2) & (client->bufsize - 1);
75
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;
80
81                 client->packet_head = client->tail;
82                 if (client->use_wake_lock)
83                         wake_unlock(&client->wake_lock);
84         }
85
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);
91         }
92
93         spin_unlock(&client->buffer_lock);
94 }
95
96 /*
97  * Pass incoming event to all connected clients.
98  */
99 static void evdev_event(struct input_handle *handle,
100                         unsigned int type, unsigned int code, int value)
101 {
102         struct evdev *evdev = handle->private;
103         struct evdev_client *client;
104         struct input_event event;
105         struct timespec ts;
106
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;
113
114         rcu_read_lock();
115
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);
122
123         rcu_read_unlock();
124
125         if (type == EV_SYN && code == SYN_REPORT)
126                 wake_up_interruptible(&evdev->wait);
127 }
128
129 static int evdev_fasync(int fd, struct file *file, int on)
130 {
131         struct evdev_client *client = file->private_data;
132
133         return fasync_helper(fd, file, on, &client->fasync);
134 }
135
136 static int evdev_flush(struct file *file, fl_owner_t id)
137 {
138         struct evdev_client *client = file->private_data;
139         struct evdev *evdev = client->evdev;
140         int retval;
141
142         retval = mutex_lock_interruptible(&evdev->mutex);
143         if (retval)
144                 return retval;
145
146         if (!evdev->exist)
147                 retval = -ENODEV;
148         else
149                 retval = input_flush_device(&evdev->handle, file);
150
151         mutex_unlock(&evdev->mutex);
152         return retval;
153 }
154
155 static void evdev_free(struct device *dev)
156 {
157         struct evdev *evdev = container_of(dev, struct evdev, dev);
158
159         input_put_device(evdev->handle.dev);
160         kfree(evdev);
161 }
162
163 /*
164  * Grabs an event device (along with underlying input device).
165  * This function is called with evdev->mutex taken.
166  */
167 static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
168 {
169         int error;
170
171         if (evdev->grab)
172                 return -EBUSY;
173
174         error = input_grab_device(&evdev->handle);
175         if (error)
176                 return error;
177
178         rcu_assign_pointer(evdev->grab, client);
179
180         return 0;
181 }
182
183 static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
184 {
185         if (evdev->grab != client)
186                 return  -EINVAL;
187
188         rcu_assign_pointer(evdev->grab, NULL);
189         synchronize_rcu();
190         input_release_device(&evdev->handle);
191
192         return 0;
193 }
194
195 static void evdev_attach_client(struct evdev *evdev,
196                                 struct evdev_client *client)
197 {
198         spin_lock(&evdev->client_lock);
199         list_add_tail_rcu(&client->node, &evdev->client_list);
200         spin_unlock(&evdev->client_lock);
201 }
202
203 static void evdev_detach_client(struct evdev *evdev,
204                                 struct evdev_client *client)
205 {
206         spin_lock(&evdev->client_lock);
207         list_del_rcu(&client->node);
208         spin_unlock(&evdev->client_lock);
209         synchronize_rcu();
210 }
211
212 static int evdev_open_device(struct evdev *evdev)
213 {
214         int retval;
215
216         retval = mutex_lock_interruptible(&evdev->mutex);
217         if (retval)
218                 return retval;
219
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--;
226         }
227
228         mutex_unlock(&evdev->mutex);
229         return retval;
230 }
231
232 static void evdev_close_device(struct evdev *evdev)
233 {
234         mutex_lock(&evdev->mutex);
235
236         if (evdev->exist && !--evdev->open)
237                 input_close_device(&evdev->handle);
238
239         mutex_unlock(&evdev->mutex);
240 }
241
242 /*
243  * Wake up users waiting for IO so they can disconnect from
244  * dead device.
245  */
246 static void evdev_hangup(struct evdev *evdev)
247 {
248         struct evdev_client *client;
249
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);
254
255         wake_up_interruptible(&evdev->wait);
256 }
257
258 static int evdev_release(struct inode *inode, struct file *file)
259 {
260         struct evdev_client *client = file->private_data;
261         struct evdev *evdev = client->evdev;
262
263         mutex_lock(&evdev->mutex);
264         if (evdev->grab == client)
265                 evdev_ungrab(evdev, client);
266         mutex_unlock(&evdev->mutex);
267
268         evdev_detach_client(evdev, client);
269         if (client->use_wake_lock)
270                 wake_lock_destroy(&client->wake_lock);
271         kfree(client);
272
273         evdev_close_device(evdev);
274         put_device(&evdev->dev);
275
276         return 0;
277 }
278
279 static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
280 {
281         unsigned int n_events =
282                 max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
283                     EVDEV_MIN_BUFFER_SIZE);
284
285         return roundup_pow_of_two(n_events);
286 }
287
288 static int evdev_open(struct inode *inode, struct file *file)
289 {
290         struct evdev *evdev;
291         struct evdev_client *client;
292         int i = iminor(inode) - EVDEV_MINOR_BASE;
293         unsigned int bufsize;
294         int error;
295
296         if (i >= EVDEV_MINORS)
297                 return -ENODEV;
298
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);
306
307         if (!evdev)
308                 return -ENODEV;
309
310         bufsize = evdev_compute_buffer_size(evdev->handle.dev);
311
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;
318         }
319
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);
326
327         error = evdev_open_device(evdev);
328         if (error)
329                 goto err_free_client;
330
331         file->private_data = client;
332         nonseekable_open(inode, file);
333
334         return 0;
335
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;
342 }
343
344 static ssize_t evdev_write(struct file *file, const char __user *buffer,
345                            size_t count, loff_t *ppos)
346 {
347         struct evdev_client *client = file->private_data;
348         struct evdev *evdev = client->evdev;
349         struct input_event event;
350         int retval;
351
352         if (count < input_event_size())
353                 return -EINVAL;
354
355         retval = mutex_lock_interruptible(&evdev->mutex);
356         if (retval)
357                 return retval;
358
359         if (!evdev->exist) {
360                 retval = -ENODEV;
361                 goto out;
362         }
363
364         do {
365                 if (input_event_from_user(buffer + retval, &event)) {
366                         retval = -EFAULT;
367                         goto out;
368                 }
369                 retval += input_event_size();
370
371                 input_inject_event(&evdev->handle,
372                                    event.type, event.code, event.value);
373         } while (retval + input_event_size() <= count);
374
375  out:
376         mutex_unlock(&evdev->mutex);
377         return retval;
378 }
379
380 static int evdev_fetch_next_event(struct evdev_client *client,
381                                   struct input_event *event)
382 {
383         int have_event;
384
385         spin_lock_irq(&client->buffer_lock);
386
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);
394         }
395
396         spin_unlock_irq(&client->buffer_lock);
397
398         return have_event;
399 }
400
401 static ssize_t evdev_read(struct file *file, char __user *buffer,
402                           size_t count, loff_t *ppos)
403 {
404         struct evdev_client *client = file->private_data;
405         struct evdev *evdev = client->evdev;
406         struct input_event event;
407         int retval = 0;
408
409         if (count < input_event_size())
410                 return -EINVAL;
411
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;
417         }
418
419         if (!evdev->exist)
420                 return -ENODEV;
421
422         while (retval + input_event_size() <= count &&
423                evdev_fetch_next_event(client, &event)) {
424
425                 if (input_event_to_user(buffer + retval, &event))
426                         return -EFAULT;
427
428                 retval += input_event_size();
429         }
430
431         if (retval == 0 && file->f_flags & O_NONBLOCK)
432                 retval = -EAGAIN;
433         return retval;
434 }
435
436 /* No kernel lock - fine */
437 static unsigned int evdev_poll(struct file *file, poll_table *wait)
438 {
439         struct evdev_client *client = file->private_data;
440         struct evdev *evdev = client->evdev;
441         unsigned int mask;
442
443         poll_wait(file, &evdev->wait, wait);
444
445         mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR;
446         if (client->packet_head != client->tail)
447                 mask |= POLLIN | POLLRDNORM;
448
449         return mask;
450 }
451
452 #ifdef CONFIG_COMPAT
453
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)
456
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)
460 {
461         int len, i;
462
463         if (compat) {
464                 len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
465                 if (len > maxlen)
466                         len = maxlen;
467
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;
478
479                 if (copy_to_user(p, bits, len))
480                         return -EFAULT;
481         }
482
483         return len;
484 }
485 #else
486 static int bits_to_user(unsigned long *bits, unsigned int maxbit,
487                         unsigned int maxlen, void __user *p, int compat)
488 {
489         int len = compat ?
490                         BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
491                         BITS_TO_LONGS(maxbit) * sizeof(long);
492
493         if (len > maxlen)
494                 len = maxlen;
495
496         return copy_to_user(p, bits, len) ? -EFAULT : len;
497 }
498 #endif /* __BIG_ENDIAN */
499
500 #else
501
502 static int bits_to_user(unsigned long *bits, unsigned int maxbit,
503                         unsigned int maxlen, void __user *p, int compat)
504 {
505         int len = BITS_TO_LONGS(maxbit) * sizeof(long);
506
507         if (len > maxlen)
508                 len = maxlen;
509
510         return copy_to_user(p, bits, len) ? -EFAULT : len;
511 }
512
513 #endif /* CONFIG_COMPAT */
514
515 static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
516 {
517         int len;
518
519         if (!str)
520                 return -ENOENT;
521
522         len = strlen(str) + 1;
523         if (len > maxlen)
524                 len = maxlen;
525
526         return copy_to_user(p, str, len) ? -EFAULT : len;
527 }
528
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)
533 {
534         static unsigned long keymax_warn_time;
535         unsigned long *bits;
536         int len;
537
538         switch (type) {
539
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;
550         }
551
552         /*
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.
556          */
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));
565         }
566
567         return bits_to_user(bits, len, size, p, compat_mode);
568 }
569 #undef OLD_KEY_MAX
570
571 static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
572 {
573         struct input_keymap_entry ke = {
574                 .len    = sizeof(unsigned int),
575                 .flags  = 0,
576         };
577         int __user *ip = (int __user *)p;
578         int error;
579
580         /* legacy case */
581         if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
582                 return -EFAULT;
583
584         error = input_get_keycode(dev, &ke);
585         if (error)
586                 return error;
587
588         if (put_user(ke.keycode, ip + 1))
589                 return -EFAULT;
590
591         return 0;
592 }
593
594 static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
595 {
596         struct input_keymap_entry ke;
597         int error;
598
599         if (copy_from_user(&ke, p, sizeof(ke)))
600                 return -EFAULT;
601
602         error = input_get_keycode(dev, &ke);
603         if (error)
604                 return error;
605
606         if (copy_to_user(p, &ke, sizeof(ke)))
607                 return -EFAULT;
608
609         return 0;
610 }
611
612 static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
613 {
614         struct input_keymap_entry ke = {
615                 .len    = sizeof(unsigned int),
616                 .flags  = 0,
617         };
618         int __user *ip = (int __user *)p;
619
620         if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
621                 return -EFAULT;
622
623         if (get_user(ke.keycode, ip + 1))
624                 return -EFAULT;
625
626         return input_set_keycode(dev, &ke);
627 }
628
629 static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
630 {
631         struct input_keymap_entry ke;
632
633         if (copy_from_user(&ke, p, sizeof(ke)))
634                 return -EFAULT;
635
636         if (ke.len > sizeof(ke.scancode))
637                 return -EINVAL;
638
639         return input_set_keycode(dev, &ke);
640 }
641
642 static int evdev_enable_suspend_block(struct evdev *evdev,
643                                       struct evdev_client *client)
644 {
645         if (client->use_wake_lock)
646                 return 0;
647
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;
655 }
656
657 static int evdev_disable_suspend_block(struct evdev *evdev,
658                                        struct evdev_client *client)
659 {
660         if (!client->use_wake_lock)
661                 return 0;
662
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);
667
668         return 0;
669 }
670
671 static long evdev_do_ioctl(struct file *file, unsigned int cmd,
672                            void __user *p, int compat_mode)
673 {
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;
683
684         /* First we check for fixed-length commands */
685         switch (cmd) {
686
687         case EVIOCGVERSION:
688                 return put_user(EV_VERSION, ip);
689
690         case EVIOCGID:
691                 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
692                         return -EFAULT;
693                 return 0;
694
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;
703
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;
711
712                 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
713                 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
714
715                 return 0;
716
717         case EVIOCRMFF:
718                 return input_ff_erase(dev, (int)(unsigned long) p, file);
719
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;
726
727         case EVIOCGRAB:
728                 if (p)
729                         return evdev_grab(evdev, client);
730                 else
731                         return evdev_ungrab(evdev, client);
732
733         case EVIOCGKEYCODE:
734                 return evdev_handle_get_keycode(dev, p);
735
736         case EVIOCSKEYCODE:
737                 return evdev_handle_set_keycode(dev, p);
738
739         case EVIOCGKEYCODE_V2:
740                 return evdev_handle_get_keycode_v2(dev, p);
741
742         case EVIOCSKEYCODE_V2:
743                 return evdev_handle_set_keycode_v2(dev, p);
744
745         case EVIOCGSUSPENDBLOCK:
746                 return put_user(client->use_wake_lock, ip);
747
748         case EVIOCSSUSPENDBLOCK:
749                 if (p)
750                         return evdev_enable_suspend_block(evdev, client);
751                 else
752                         return evdev_disable_suspend_block(evdev, client);
753         }
754
755         size = _IOC_SIZE(cmd);
756
757         /* Now check variable-length commands */
758 #define EVIOC_MASK_SIZE(nr)     ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
759         switch (EVIOC_MASK_SIZE(cmd)) {
760
761         case EVIOCGPROP(0):
762                 return bits_to_user(dev->propbit, INPUT_PROP_MAX,
763                                     size, p, compat_mode);
764
765         case EVIOCGKEY(0):
766                 return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode);
767
768         case EVIOCGLED(0):
769                 return bits_to_user(dev->led, LED_MAX, size, p, compat_mode);
770
771         case EVIOCGSND(0):
772                 return bits_to_user(dev->snd, SND_MAX, size, p, compat_mode);
773
774         case EVIOCGSW(0):
775                 return bits_to_user(dev->sw, SW_MAX, size, p, compat_mode);
776
777         case EVIOCGNAME(0):
778                 return str_to_user(dev->name, size, p);
779
780         case EVIOCGPHYS(0):
781                 return str_to_user(dev->phys, size, p);
782
783         case EVIOCGUNIQ(0):
784                 return str_to_user(dev->uniq, size, p);
785
786         case EVIOC_MASK_SIZE(EVIOCSFF):
787                 if (input_ff_effect_from_user(p, size, &effect))
788                         return -EFAULT;
789
790                 error = input_ff_upload(dev, &effect, file);
791
792                 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
793                         return -EFAULT;
794
795                 return error;
796         }
797
798         /* Multi-number variable-length handlers */
799         if (_IOC_TYPE(cmd) != 'E')
800                 return -EINVAL;
801
802         if (_IOC_DIR(cmd) == _IOC_READ) {
803
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);
808
809                 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
810
811                         if (!dev->absinfo)
812                                 return -EINVAL;
813
814                         t = _IOC_NR(cmd) & ABS_MAX;
815                         abs = dev->absinfo[t];
816
817                         if (copy_to_user(p, &abs, min_t(size_t,
818                                         size, sizeof(struct input_absinfo))))
819                                 return -EFAULT;
820
821                         return 0;
822                 }
823         }
824
825         if (_IOC_DIR(cmd) == _IOC_WRITE) {
826
827                 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
828
829                         if (!dev->absinfo)
830                                 return -EINVAL;
831
832                         t = _IOC_NR(cmd) & ABS_MAX;
833
834                         if (copy_from_user(&abs, p, min_t(size_t,
835                                         size, sizeof(struct input_absinfo))))
836                                 return -EFAULT;
837
838                         if (size < sizeof(struct input_absinfo))
839                                 abs.resolution = 0;
840
841                         /* We can't change number of reserved MT slots */
842                         if (t == ABS_MT_SLOT)
843                                 return -EINVAL;
844
845                         /*
846                          * Take event lock to ensure that we are not
847                          * changing device parameters in the middle
848                          * of event.
849                          */
850                         spin_lock_irq(&dev->event_lock);
851                         dev->absinfo[t] = abs;
852                         spin_unlock_irq(&dev->event_lock);
853
854                         return 0;
855                 }
856         }
857
858         return -EINVAL;
859 }
860
861 static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
862                                 void __user *p, int compat_mode)
863 {
864         struct evdev_client *client = file->private_data;
865         struct evdev *evdev = client->evdev;
866         int retval;
867
868         retval = mutex_lock_interruptible(&evdev->mutex);
869         if (retval)
870                 return retval;
871
872         if (!evdev->exist) {
873                 retval = -ENODEV;
874                 goto out;
875         }
876
877         retval = evdev_do_ioctl(file, cmd, p, compat_mode);
878
879  out:
880         mutex_unlock(&evdev->mutex);
881         return retval;
882 }
883
884 static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
885 {
886         return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
887 }
888
889 #ifdef CONFIG_COMPAT
890 static long evdev_ioctl_compat(struct file *file,
891                                 unsigned int cmd, unsigned long arg)
892 {
893         return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
894 }
895 #endif
896
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,
911 };
912
913 static int evdev_install_chrdev(struct evdev *evdev)
914 {
915         /*
916          * No need to do any locking here as calls to connect and
917          * disconnect are serialized by the input core
918          */
919         evdev_table[evdev->minor] = evdev;
920         return 0;
921 }
922
923 static void evdev_remove_chrdev(struct evdev *evdev)
924 {
925         /*
926          * Lock evdev table to prevent race with evdev_open()
927          */
928         mutex_lock(&evdev_table_mutex);
929         evdev_table[evdev->minor] = NULL;
930         mutex_unlock(&evdev_table_mutex);
931 }
932
933 /*
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.
937  */
938 static void evdev_mark_dead(struct evdev *evdev)
939 {
940         mutex_lock(&evdev->mutex);
941         evdev->exist = false;
942         mutex_unlock(&evdev->mutex);
943 }
944
945 static void evdev_cleanup(struct evdev *evdev)
946 {
947         struct input_handle *handle = &evdev->handle;
948
949         evdev_mark_dead(evdev);
950         evdev_hangup(evdev);
951         evdev_remove_chrdev(evdev);
952
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);
957         }
958 }
959
960 /*
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.
963  */
964 static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
965                          const struct input_device_id *id)
966 {
967         struct evdev *evdev;
968         int minor;
969         int error;
970
971         for (minor = 0; minor < EVDEV_MINORS; minor++)
972                 if (!evdev_table[minor])
973                         break;
974
975         if (minor == EVDEV_MINORS) {
976                 pr_err("no more free evdev devices\n");
977                 return -ENFILE;
978         }
979
980         evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
981         if (!evdev)
982                 return -ENOMEM;
983
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);
988
989         dev_set_name(&evdev->dev, "event%d", minor);
990         evdev->exist = true;
991         evdev->minor = minor;
992
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;
997
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);
1003
1004         error = input_register_handle(&evdev->handle);
1005         if (error)
1006                 goto err_free_evdev;
1007
1008         error = evdev_install_chrdev(evdev);
1009         if (error)
1010                 goto err_unregister_handle;
1011
1012         error = device_add(&evdev->dev);
1013         if (error)
1014                 goto err_cleanup_evdev;
1015
1016         return 0;
1017
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;
1025 }
1026
1027 static void evdev_disconnect(struct input_handle *handle)
1028 {
1029         struct evdev *evdev = handle->private;
1030
1031         device_del(&evdev->dev);
1032         evdev_cleanup(evdev);
1033         input_unregister_handle(handle);
1034         put_device(&evdev->dev);
1035 }
1036
1037 static const struct input_device_id evdev_ids[] = {
1038         { .driver_info = 1 },   /* Matches all devices */
1039         { },                    /* Terminating zero entry */
1040 };
1041
1042 MODULE_DEVICE_TABLE(input, evdev_ids);
1043
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,
1052 };
1053
1054 static int __init evdev_init(void)
1055 {
1056         return input_register_handler(&evdev_handler);
1057 }
1058
1059 static void __exit evdev_exit(void)
1060 {
1061         input_unregister_handler(&evdev_handler);
1062 }
1063
1064 module_init(evdev_init);
1065 module_exit(evdev_exit);
1066
1067 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1068 MODULE_DESCRIPTION("Input driver event char devices");
1069 MODULE_LICENSE("GPL");