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