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