[PATCH] drivers/input/mouse: convert to dynamic input_dev allocation
[linux-2.6.git] / drivers / input / evdev.c
1 /*
2  * Event char devices, giving access to raw input device events.
3  *
4  * Copyright (c) 1999-2002 Vojtech Pavlik
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published by
8  * the Free Software Foundation.
9  */
10
11 #define 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                                                 default: return -EINVAL;
569                                         }
570                                         bit_to_user(bits, max);
571                                 }
572
573                                 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0)))
574                                         bit_to_user(dev->key, KEY_MAX);
575
576                                 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0)))
577                                         bit_to_user(dev->led, LED_MAX);
578
579                                 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0)))
580                                         bit_to_user(dev->snd, SND_MAX);
581
582                                 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) {
583                                         int len;
584                                         if (!dev->name) return -ENOENT;
585                                         len = strlen(dev->name) + 1;
586                                         if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
587                                         return copy_to_user(p, dev->name, len) ? -EFAULT : len;
588                                 }
589
590                                 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) {
591                                         int len;
592                                         if (!dev->phys) return -ENOENT;
593                                         len = strlen(dev->phys) + 1;
594                                         if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
595                                         return copy_to_user(p, dev->phys, len) ? -EFAULT : len;
596                                 }
597
598                                 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) {
599                                         int len;
600                                         if (!dev->uniq) return -ENOENT;
601                                         len = strlen(dev->uniq) + 1;
602                                         if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
603                                         return copy_to_user(p, dev->uniq, len) ? -EFAULT : len;
604                                 }
605
606                                 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
607
608                                         int t = _IOC_NR(cmd) & ABS_MAX;
609
610                                         abs.value = dev->abs[t];
611                                         abs.minimum = dev->absmin[t];
612                                         abs.maximum = dev->absmax[t];
613                                         abs.fuzz = dev->absfuzz[t];
614                                         abs.flat = dev->absflat[t];
615
616                                         if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
617                                                 return -EFAULT;
618
619                                         return 0;
620                                 }
621                         }
622
623                         if (_IOC_DIR(cmd) == _IOC_WRITE) {
624
625                                 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
626
627                                         int t = _IOC_NR(cmd) & ABS_MAX;
628
629                                         if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
630                                                 return -EFAULT;
631
632                                         dev->abs[t] = abs.value;
633                                         dev->absmin[t] = abs.minimum;
634                                         dev->absmax[t] = abs.maximum;
635                                         dev->absfuzz[t] = abs.fuzz;
636                                         dev->absflat[t] = abs.flat;
637
638                                         return 0;
639                                 }
640                         }
641         }
642         return -EINVAL;
643 }
644 #endif
645
646 static struct file_operations evdev_fops = {
647         .owner =        THIS_MODULE,
648         .read =         evdev_read,
649         .write =        evdev_write,
650         .poll =         evdev_poll,
651         .open =         evdev_open,
652         .release =      evdev_release,
653         .unlocked_ioctl = evdev_ioctl,
654 #ifdef CONFIG_COMPAT
655         .compat_ioctl = evdev_ioctl_compat,
656 #endif
657         .fasync =       evdev_fasync,
658         .flush =        evdev_flush
659 };
660
661 static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
662 {
663         struct evdev *evdev;
664         int minor;
665
666         for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
667         if (minor == EVDEV_MINORS) {
668                 printk(KERN_ERR "evdev: no more free evdev devices\n");
669                 return NULL;
670         }
671
672         if (!(evdev = kmalloc(sizeof(struct evdev), GFP_KERNEL)))
673                 return NULL;
674         memset(evdev, 0, sizeof(struct evdev));
675
676         INIT_LIST_HEAD(&evdev->list);
677         init_waitqueue_head(&evdev->wait);
678
679         evdev->exist = 1;
680         evdev->minor = minor;
681         evdev->handle.dev = dev;
682         evdev->handle.name = evdev->name;
683         evdev->handle.handler = handler;
684         evdev->handle.private = evdev;
685         sprintf(evdev->name, "event%d", minor);
686
687         evdev_table[minor] = evdev;
688
689         class_device_create(input_class, NULL,
690                         MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
691                         dev->dev, "event%d", minor);
692
693         return &evdev->handle;
694 }
695
696 static void evdev_disconnect(struct input_handle *handle)
697 {
698         struct evdev *evdev = handle->private;
699         struct evdev_list *list;
700
701         class_device_destroy(input_class,
702                         MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor));
703         evdev->exist = 0;
704
705         if (evdev->open) {
706                 input_close_device(handle);
707                 wake_up_interruptible(&evdev->wait);
708                 list_for_each_entry(list, &evdev->list, node)
709                         kill_fasync(&list->fasync, SIGIO, POLL_HUP);
710         } else
711                 evdev_free(evdev);
712 }
713
714 static struct input_device_id evdev_ids[] = {
715         { .driver_info = 1 },   /* Matches all devices */
716         { },                    /* Terminating zero entry */
717 };
718
719 MODULE_DEVICE_TABLE(input, evdev_ids);
720
721 static struct input_handler evdev_handler = {
722         .event =        evdev_event,
723         .connect =      evdev_connect,
724         .disconnect =   evdev_disconnect,
725         .fops =         &evdev_fops,
726         .minor =        EVDEV_MINOR_BASE,
727         .name =         "evdev",
728         .id_table =     evdev_ids,
729 };
730
731 static int __init evdev_init(void)
732 {
733         input_register_handler(&evdev_handler);
734         return 0;
735 }
736
737 static void __exit evdev_exit(void)
738 {
739         input_unregister_handler(&evdev_handler);
740 }
741
742 module_init(evdev_init);
743 module_exit(evdev_exit);
744
745 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
746 MODULE_DESCRIPTION("Input driver event char devices");
747 MODULE_LICENSE("GPL");