Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6
[linux-2.6] / 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_BUFFER_SIZE       64
14
15 #include <linux/poll.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/input.h>
20 #include <linux/major.h>
21 #include <linux/smp_lock.h>
22 #include <linux/device.h>
23 #include <linux/compat.h>
24
25 struct evdev {
26         int exist;
27         int open;
28         int minor;
29         char name[16];
30         struct input_handle handle;
31         wait_queue_head_t wait;
32         struct evdev_list *grab;
33         struct list_head list;
34 };
35
36 struct evdev_list {
37         struct input_event buffer[EVDEV_BUFFER_SIZE];
38         int head;
39         int tail;
40         struct fasync_struct *fasync;
41         struct evdev *evdev;
42         struct list_head node;
43 };
44
45 static struct evdev *evdev_table[EVDEV_MINORS];
46
47 static void evdev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
48 {
49         struct evdev *evdev = handle->private;
50         struct evdev_list *list;
51
52         if (evdev->grab) {
53                 list = evdev->grab;
54
55                 do_gettimeofday(&list->buffer[list->head].time);
56                 list->buffer[list->head].type = type;
57                 list->buffer[list->head].code = code;
58                 list->buffer[list->head].value = value;
59                 list->head = (list->head + 1) & (EVDEV_BUFFER_SIZE - 1);
60
61                 kill_fasync(&list->fasync, SIGIO, POLL_IN);
62         } else
63                 list_for_each_entry(list, &evdev->list, node) {
64
65                         do_gettimeofday(&list->buffer[list->head].time);
66                         list->buffer[list->head].type = type;
67                         list->buffer[list->head].code = code;
68                         list->buffer[list->head].value = value;
69                         list->head = (list->head + 1) & (EVDEV_BUFFER_SIZE - 1);
70
71                         kill_fasync(&list->fasync, SIGIO, POLL_IN);
72                 }
73
74         wake_up_interruptible(&evdev->wait);
75 }
76
77 static int evdev_fasync(int fd, struct file *file, int on)
78 {
79         int retval;
80         struct evdev_list *list = file->private_data;
81         retval = fasync_helper(fd, file, on, &list->fasync);
82         return retval < 0 ? retval : 0;
83 }
84
85 static int evdev_flush(struct file * file)
86 {
87         struct evdev_list *list = file->private_data;
88         if (!list->evdev->exist) return -ENODEV;
89         return input_flush_device(&list->evdev->handle, file);
90 }
91
92 static void evdev_free(struct evdev *evdev)
93 {
94         evdev_table[evdev->minor] = NULL;
95         kfree(evdev);
96 }
97
98 static int evdev_release(struct inode * inode, struct file * file)
99 {
100         struct evdev_list *list = file->private_data;
101
102         if (list->evdev->grab == list) {
103                 input_release_device(&list->evdev->handle);
104                 list->evdev->grab = NULL;
105         }
106
107         evdev_fasync(-1, file, 0);
108         list_del(&list->node);
109
110         if (!--list->evdev->open) {
111                 if (list->evdev->exist)
112                         input_close_device(&list->evdev->handle);
113                 else
114                         evdev_free(list->evdev);
115         }
116
117         kfree(list);
118         return 0;
119 }
120
121 static int evdev_open(struct inode * inode, struct file * file)
122 {
123         struct evdev_list *list;
124         int i = iminor(inode) - EVDEV_MINOR_BASE;
125         int accept_err;
126
127         if (i >= EVDEV_MINORS || !evdev_table[i] || !evdev_table[i]->exist)
128                 return -ENODEV;
129
130         if ((accept_err = input_accept_process(&(evdev_table[i]->handle), file)))
131                 return accept_err;
132
133         if (!(list = kmalloc(sizeof(struct evdev_list), GFP_KERNEL)))
134                 return -ENOMEM;
135         memset(list, 0, sizeof(struct evdev_list));
136
137         list->evdev = evdev_table[i];
138         list_add_tail(&list->node, &evdev_table[i]->list);
139         file->private_data = list;
140
141         if (!list->evdev->open++)
142                 if (list->evdev->exist)
143                         input_open_device(&list->evdev->handle);
144
145         return 0;
146 }
147
148 #ifdef CONFIG_COMPAT
149 struct input_event_compat {
150         struct compat_timeval time;
151         __u16 type;
152         __u16 code;
153         __s32 value;
154 };
155
156 #ifdef CONFIG_X86_64
157 #  define COMPAT_TEST test_thread_flag(TIF_IA32)
158 #elif defined(CONFIG_IA64)
159 #  define COMPAT_TEST IS_IA32_PROCESS(ia64_task_regs(current))
160 #elif defined(CONFIG_ARCH_S390)
161 #  define COMPAT_TEST test_thread_flag(TIF_31BIT)
162 #elif defined(CONFIG_MIPS)
163 #  define COMPAT_TEST (current->thread.mflags & MF_32BIT_ADDR)
164 #else
165 #  define COMPAT_TEST test_thread_flag(TIF_32BIT)
166 #endif
167
168 static ssize_t evdev_write_compat(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
169 {
170         struct evdev_list *list = file->private_data;
171         struct input_event_compat event;
172         int retval = 0;
173
174         while (retval < count) {
175                 if (copy_from_user(&event, buffer + retval, sizeof(struct input_event_compat)))
176                         return -EFAULT;
177                 input_event(list->evdev->handle.dev, event.type, event.code, event.value);
178                 retval += sizeof(struct input_event_compat);
179         }
180
181         return retval;
182 }
183 #endif
184
185 static ssize_t evdev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
186 {
187         struct evdev_list *list = file->private_data;
188         struct input_event event;
189         int retval = 0;
190
191         if (!list->evdev->exist) return -ENODEV;
192
193 #ifdef CONFIG_COMPAT
194         if (COMPAT_TEST)
195                 return evdev_write_compat(file, buffer, count, ppos);
196 #endif
197
198         while (retval < count) {
199
200                 if (copy_from_user(&event, buffer + retval, sizeof(struct input_event)))
201                         return -EFAULT;
202                 input_event(list->evdev->handle.dev, event.type, event.code, event.value);
203                 retval += sizeof(struct input_event);
204         }
205
206         return retval;
207 }
208
209 #ifdef CONFIG_COMPAT
210 static ssize_t evdev_read_compat(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
211 {
212         struct evdev_list *list = file->private_data;
213         int retval;
214
215         if (count < sizeof(struct input_event_compat))
216                 return -EINVAL;
217
218         if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
219                 return -EAGAIN;
220
221         retval = wait_event_interruptible(list->evdev->wait,
222                 list->head != list->tail || (!list->evdev->exist));
223
224         if (retval)
225                 return retval;
226
227         if (!list->evdev->exist)
228                 return -ENODEV;
229
230         while (list->head != list->tail && retval + sizeof(struct input_event_compat) <= count) {
231                 struct input_event *event = (struct input_event *) list->buffer + list->tail;
232                 struct input_event_compat event_compat;
233                 event_compat.time.tv_sec = event->time.tv_sec;
234                 event_compat.time.tv_usec = event->time.tv_usec;
235                 event_compat.type = event->type;
236                 event_compat.code = event->code;
237                 event_compat.value = event->value;
238
239                 if (copy_to_user(buffer + retval, &event_compat,
240                         sizeof(struct input_event_compat))) return -EFAULT;
241                 list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
242                 retval += sizeof(struct input_event_compat);
243         }
244
245         return retval;
246 }
247 #endif
248
249 static ssize_t evdev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
250 {
251         struct evdev_list *list = file->private_data;
252         int retval;
253
254 #ifdef CONFIG_COMPAT
255         if (COMPAT_TEST)
256                 return evdev_read_compat(file, buffer, count, ppos);
257 #endif
258
259         if (count < sizeof(struct input_event))
260                 return -EINVAL;
261
262         if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
263                 return -EAGAIN;
264
265         retval = wait_event_interruptible(list->evdev->wait,
266                 list->head != list->tail || (!list->evdev->exist));
267
268         if (retval)
269                 return retval;
270
271         if (!list->evdev->exist)
272                 return -ENODEV;
273
274         while (list->head != list->tail && retval + sizeof(struct input_event) <= count) {
275                 if (copy_to_user(buffer + retval, list->buffer + list->tail,
276                         sizeof(struct input_event))) return -EFAULT;
277                 list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
278                 retval += sizeof(struct input_event);
279         }
280
281         return retval;
282 }
283
284 /* No kernel lock - fine */
285 static unsigned int evdev_poll(struct file *file, poll_table *wait)
286 {
287         struct evdev_list *list = file->private_data;
288         poll_wait(file, &list->evdev->wait, wait);
289         return ((list->head == list->tail) ? 0 : (POLLIN | POLLRDNORM)) |
290                 (list->evdev->exist ? 0 : (POLLHUP | POLLERR));
291 }
292
293 static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
294 {
295         struct evdev_list *list = file->private_data;
296         struct evdev *evdev = list->evdev;
297         struct input_dev *dev = evdev->handle.dev;
298         struct input_absinfo abs;
299         void __user *p = (void __user *)arg;
300         int __user *ip = (int __user *)arg;
301         int i, t, u, v;
302
303         if (!evdev->exist) return -ENODEV;
304
305         switch (cmd) {
306
307                 case EVIOCGVERSION:
308                         return put_user(EV_VERSION, ip);
309
310                 case EVIOCGID:
311                         return copy_to_user(p, &dev->id, sizeof(struct input_id)) ? -EFAULT : 0;
312
313                 case EVIOCGKEYCODE:
314                         if (get_user(t, ip)) return -EFAULT;
315                         if (t < 0 || t >= dev->keycodemax || !dev->keycodesize) return -EINVAL;
316                         if (put_user(INPUT_KEYCODE(dev, t), ip + 1)) return -EFAULT;
317                         return 0;
318
319                 case EVIOCSKEYCODE:
320                         if (get_user(t, ip)) return -EFAULT;
321                         if (t < 0 || t >= dev->keycodemax || !dev->keycodesize) return -EINVAL;
322                         if (get_user(v, ip + 1)) return -EFAULT;
323                         if (v < 0 || v > KEY_MAX) return -EINVAL;
324                         if (dev->keycodesize < sizeof(v) && (v >> (dev->keycodesize * 8))) return -EINVAL;
325                         u = SET_INPUT_KEYCODE(dev, t, v);
326                         clear_bit(u, dev->keybit);
327                         set_bit(v, dev->keybit);
328                         for (i = 0; i < dev->keycodemax; i++)
329                                 if (INPUT_KEYCODE(dev,i) == u)
330                                         set_bit(u, dev->keybit);
331                         return 0;
332
333                 case EVIOCSFF:
334                         if (dev->upload_effect) {
335                                 struct ff_effect effect;
336                                 int err;
337
338                                 if (copy_from_user(&effect, p, sizeof(effect)))
339                                         return -EFAULT;
340                                 err = dev->upload_effect(dev, &effect);
341                                 if (put_user(effect.id, &(((struct ff_effect __user *)arg)->id)))
342                                         return -EFAULT;
343                                 return err;
344                         }
345                         else return -ENOSYS;
346
347                 case EVIOCRMFF:
348                         if (dev->erase_effect) {
349                                 return dev->erase_effect(dev, (int)arg);
350                         }
351                         else return -ENOSYS;
352
353                 case EVIOCGEFFECTS:
354                         if (put_user(dev->ff_effects_max, ip))
355                                 return -EFAULT;
356                         return 0;
357
358                 case EVIOCGRAB:
359                         if (arg) {
360                                 if (evdev->grab)
361                                         return -EBUSY;
362                                 if (input_grab_device(&evdev->handle))
363                                         return -EBUSY;
364                                 evdev->grab = list;
365                                 return 0;
366                         } else {
367                                 if (evdev->grab != list)
368                                         return -EINVAL;
369                                 input_release_device(&evdev->handle);
370                                 evdev->grab = NULL;
371                                 return 0;
372                         }
373
374                 default:
375
376                         if (_IOC_TYPE(cmd) != 'E')
377                                 return -EINVAL;
378
379                         if (_IOC_DIR(cmd) == _IOC_READ) {
380
381                                 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
382
383                                         long *bits;
384                                         int len;
385
386                                         switch (_IOC_NR(cmd) & EV_MAX) {
387                                                 case      0: bits = dev->evbit;  len = EV_MAX;  break;
388                                                 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
389                                                 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
390                                                 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
391                                                 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
392                                                 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
393                                                 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
394                                                 case EV_FF:  bits = dev->ffbit;  len = FF_MAX;  break;
395                                                 case EV_SW:  bits = dev->swbit;  len = SW_MAX;  break;
396                                                 default: return -EINVAL;
397                                         }
398                                         len = NBITS(len) * sizeof(long);
399                                         if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
400                                         return copy_to_user(p, bits, len) ? -EFAULT : len;
401                                 }
402
403                                 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0))) {
404                                         int len;
405                                         len = NBITS(KEY_MAX) * sizeof(long);
406                                         if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
407                                         return copy_to_user(p, dev->key, len) ? -EFAULT : len;
408                                 }
409
410                                 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0))) {
411                                         int len;
412                                         len = NBITS(LED_MAX) * sizeof(long);
413                                         if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
414                                         return copy_to_user(p, dev->led, len) ? -EFAULT : len;
415                                 }
416
417                                 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0))) {
418                                         int len;
419                                         len = NBITS(SND_MAX) * sizeof(long);
420                                         if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
421                                         return copy_to_user(p, dev->snd, len) ? -EFAULT : len;
422                                 }
423
424                                 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSW(0))) {
425                                         int len;
426                                         len = NBITS(SW_MAX) * sizeof(long);
427                                         if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
428                                         return copy_to_user(p, dev->sw, len) ? -EFAULT : len;
429                                 }
430
431                                 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) {
432                                         int len;
433                                         if (!dev->name) return -ENOENT;
434                                         len = strlen(dev->name) + 1;
435                                         if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
436                                         return copy_to_user(p, dev->name, len) ? -EFAULT : len;
437                                 }
438
439                                 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) {
440                                         int len;
441                                         if (!dev->phys) return -ENOENT;
442                                         len = strlen(dev->phys) + 1;
443                                         if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
444                                         return copy_to_user(p, dev->phys, len) ? -EFAULT : len;
445                                 }
446
447                                 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) {
448                                         int len;
449                                         if (!dev->uniq) return -ENOENT;
450                                         len = strlen(dev->uniq) + 1;
451                                         if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
452                                         return copy_to_user(p, dev->uniq, len) ? -EFAULT : len;
453                                 }
454
455                                 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
456
457                                         int t = _IOC_NR(cmd) & ABS_MAX;
458
459                                         abs.value = dev->abs[t];
460                                         abs.minimum = dev->absmin[t];
461                                         abs.maximum = dev->absmax[t];
462                                         abs.fuzz = dev->absfuzz[t];
463                                         abs.flat = dev->absflat[t];
464
465                                         if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
466                                                 return -EFAULT;
467
468                                         return 0;
469                                 }
470
471                         }
472
473                         if (_IOC_DIR(cmd) == _IOC_WRITE) {
474
475                                 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
476
477                                         int t = _IOC_NR(cmd) & ABS_MAX;
478
479                                         if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
480                                                 return -EFAULT;
481
482                                         dev->abs[t] = abs.value;
483                                         dev->absmin[t] = abs.minimum;
484                                         dev->absmax[t] = abs.maximum;
485                                         dev->absfuzz[t] = abs.fuzz;
486                                         dev->absflat[t] = abs.flat;
487
488                                         return 0;
489                                 }
490                         }
491         }
492         return -EINVAL;
493 }
494
495 #ifdef CONFIG_COMPAT
496
497 #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
498 #define NBITS_COMPAT(x) ((((x)-1)/BITS_PER_LONG_COMPAT)+1)
499 #define OFF_COMPAT(x)  ((x)%BITS_PER_LONG_COMPAT)
500 #define BIT_COMPAT(x)  (1UL<<OFF_COMPAT(x))
501 #define LONG_COMPAT(x) ((x)/BITS_PER_LONG_COMPAT)
502 #define test_bit_compat(bit, array) ((array[LONG_COMPAT(bit)] >> OFF_COMPAT(bit)) & 1)
503
504 #ifdef __BIG_ENDIAN
505 #define bit_to_user(bit, max) \
506 do { \
507         int i; \
508         int len = NBITS_COMPAT((max)) * sizeof(compat_long_t); \
509         if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd); \
510         for (i = 0; i < len / sizeof(compat_long_t); i++) \
511                 if (copy_to_user((compat_long_t __user *) p + i, \
512                                  (compat_long_t*) (bit) + i + 1 - ((i % 2) << 1), \
513                                  sizeof(compat_long_t))) \
514                         return -EFAULT; \
515         return len; \
516 } while (0)
517 #else
518 #define bit_to_user(bit, max) \
519 do { \
520         int len = NBITS_COMPAT((max)) * sizeof(compat_long_t); \
521         if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd); \
522         return copy_to_user(p, (bit), len) ? -EFAULT : len; \
523 } while (0)
524 #endif
525
526 static long evdev_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
527 {
528         struct evdev_list *list = file->private_data;
529         struct evdev *evdev = list->evdev;
530         struct input_dev *dev = evdev->handle.dev;
531         struct input_absinfo abs;
532         void __user *p = compat_ptr(arg);
533
534         if (!evdev->exist) return -ENODEV;
535
536         switch (cmd) {
537
538                 case EVIOCGVERSION:
539                 case EVIOCGID:
540                 case EVIOCGKEYCODE:
541                 case EVIOCSKEYCODE:
542                 case EVIOCSFF:
543                 case EVIOCRMFF:
544                 case EVIOCGEFFECTS:
545                 case EVIOCGRAB:
546                         return evdev_ioctl(file, cmd, (unsigned long) p);
547
548                 default:
549
550                         if (_IOC_TYPE(cmd) != 'E')
551                                 return -EINVAL;
552
553                         if (_IOC_DIR(cmd) == _IOC_READ) {
554
555                                 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
556                                         long *bits;
557                                         int max;
558
559                                         switch (_IOC_NR(cmd) & EV_MAX) {
560                                                 case      0: bits = dev->evbit;  max = EV_MAX;  break;
561                                                 case EV_KEY: bits = dev->keybit; max = KEY_MAX; break;
562                                                 case EV_REL: bits = dev->relbit; max = REL_MAX; break;
563                                                 case EV_ABS: bits = dev->absbit; max = ABS_MAX; break;
564                                                 case EV_MSC: bits = dev->mscbit; max = MSC_MAX; break;
565                                                 case EV_LED: bits = dev->ledbit; max = LED_MAX; break;
566                                                 case EV_SND: bits = dev->sndbit; max = SND_MAX; break;
567                                                 case EV_FF:  bits = dev->ffbit;  max = FF_MAX;  break;
568                                                 case EV_SW:  bits = dev->swbit;  max = SW_MAX;  break;
569                                                 default: return -EINVAL;
570                                         }
571                                         bit_to_user(bits, max);
572                                 }
573
574                                 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0)))
575                                         bit_to_user(dev->key, KEY_MAX);
576
577                                 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0)))
578                                         bit_to_user(dev->led, LED_MAX);
579
580                                 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0)))
581                                         bit_to_user(dev->snd, SND_MAX);
582
583                                 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSW(0)))
584                                         bit_to_user(dev->sw, SW_MAX);
585
586                                 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) {
587                                         int len;
588                                         if (!dev->name) return -ENOENT;
589                                         len = strlen(dev->name) + 1;
590                                         if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
591                                         return copy_to_user(p, dev->name, len) ? -EFAULT : len;
592                                 }
593
594                                 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) {
595                                         int len;
596                                         if (!dev->phys) return -ENOENT;
597                                         len = strlen(dev->phys) + 1;
598                                         if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
599                                         return copy_to_user(p, dev->phys, len) ? -EFAULT : len;
600                                 }
601
602                                 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) {
603                                         int len;
604                                         if (!dev->uniq) return -ENOENT;
605                                         len = strlen(dev->uniq) + 1;
606                                         if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
607                                         return copy_to_user(p, dev->uniq, len) ? -EFAULT : len;
608                                 }
609
610                                 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
611
612                                         int t = _IOC_NR(cmd) & ABS_MAX;
613
614                                         abs.value = dev->abs[t];
615                                         abs.minimum = dev->absmin[t];
616                                         abs.maximum = dev->absmax[t];
617                                         abs.fuzz = dev->absfuzz[t];
618                                         abs.flat = dev->absflat[t];
619
620                                         if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
621                                                 return -EFAULT;
622
623                                         return 0;
624                                 }
625                         }
626
627                         if (_IOC_DIR(cmd) == _IOC_WRITE) {
628
629                                 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
630
631                                         int t = _IOC_NR(cmd) & ABS_MAX;
632
633                                         if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
634                                                 return -EFAULT;
635
636                                         dev->abs[t] = abs.value;
637                                         dev->absmin[t] = abs.minimum;
638                                         dev->absmax[t] = abs.maximum;
639                                         dev->absfuzz[t] = abs.fuzz;
640                                         dev->absflat[t] = abs.flat;
641
642                                         return 0;
643                                 }
644                         }
645         }
646         return -EINVAL;
647 }
648 #endif
649
650 static struct file_operations evdev_fops = {
651         .owner =        THIS_MODULE,
652         .read =         evdev_read,
653         .write =        evdev_write,
654         .poll =         evdev_poll,
655         .open =         evdev_open,
656         .release =      evdev_release,
657         .unlocked_ioctl = evdev_ioctl,
658 #ifdef CONFIG_COMPAT
659         .compat_ioctl = evdev_ioctl_compat,
660 #endif
661         .fasync =       evdev_fasync,
662         .flush =        evdev_flush
663 };
664
665 static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
666 {
667         struct evdev *evdev;
668         struct class_device *cdev;
669         int minor;
670
671         for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
672         if (minor == EVDEV_MINORS) {
673                 printk(KERN_ERR "evdev: no more free evdev devices\n");
674                 return NULL;
675         }
676
677         if (!(evdev = kmalloc(sizeof(struct evdev), GFP_KERNEL)))
678                 return NULL;
679         memset(evdev, 0, sizeof(struct evdev));
680
681         INIT_LIST_HEAD(&evdev->list);
682         init_waitqueue_head(&evdev->wait);
683
684         evdev->exist = 1;
685         evdev->minor = minor;
686         evdev->handle.dev = dev;
687         evdev->handle.name = evdev->name;
688         evdev->handle.handler = handler;
689         evdev->handle.private = evdev;
690         sprintf(evdev->name, "event%d", minor);
691
692         evdev_table[minor] = evdev;
693
694         cdev = class_device_create(&input_class, &dev->cdev,
695                         MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
696                         dev->cdev.dev, evdev->name);
697
698         /* temporary symlink to keep userspace happy */
699         sysfs_create_link(&input_class.subsys.kset.kobj, &cdev->kobj,
700                           evdev->name);
701
702         return &evdev->handle;
703 }
704
705 static void evdev_disconnect(struct input_handle *handle)
706 {
707         struct evdev *evdev = handle->private;
708         struct evdev_list *list;
709
710         sysfs_remove_link(&input_class.subsys.kset.kobj, evdev->name);
711         class_device_destroy(&input_class,
712                         MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor));
713         evdev->exist = 0;
714
715         if (evdev->open) {
716                 input_close_device(handle);
717                 wake_up_interruptible(&evdev->wait);
718                 list_for_each_entry(list, &evdev->list, node)
719                         kill_fasync(&list->fasync, SIGIO, POLL_HUP);
720         } else
721                 evdev_free(evdev);
722 }
723
724 static struct input_device_id evdev_ids[] = {
725         { .driver_info = 1 },   /* Matches all devices */
726         { },                    /* Terminating zero entry */
727 };
728
729 MODULE_DEVICE_TABLE(input, evdev_ids);
730
731 static struct input_handler evdev_handler = {
732         .event =        evdev_event,
733         .connect =      evdev_connect,
734         .disconnect =   evdev_disconnect,
735         .fops =         &evdev_fops,
736         .minor =        EVDEV_MINOR_BASE,
737         .name =         "evdev",
738         .id_table =     evdev_ids,
739 };
740
741 static int __init evdev_init(void)
742 {
743         input_register_handler(&evdev_handler);
744         return 0;
745 }
746
747 static void __exit evdev_exit(void)
748 {
749         input_unregister_handler(&evdev_handler);
750 }
751
752 module_init(evdev_init);
753 module_exit(evdev_exit);
754
755 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
756 MODULE_DESCRIPTION("Input driver event char devices");
757 MODULE_LICENSE("GPL");