vserver 1.9.5.x5
[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/devfs_fs_kernel.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 static ssize_t evdev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
149 {
150         struct evdev_list *list = file->private_data;
151         struct input_event event;
152         int retval = 0;
153
154         if (!list->evdev->exist) return -ENODEV;
155
156         while (retval < count) {
157
158                 if (copy_from_user(&event, buffer + retval, sizeof(struct input_event)))
159                         return -EFAULT;
160                 input_event(list->evdev->handle.dev, event.type, event.code, event.value);
161                 retval += sizeof(struct input_event);
162         }
163
164         return retval;
165 }
166
167 static ssize_t evdev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
168 {
169         struct evdev_list *list = file->private_data;
170         int retval;
171
172         if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
173                 return -EAGAIN;
174
175         retval = wait_event_interruptible(list->evdev->wait,
176                 list->head != list->tail || (!list->evdev->exist));
177
178         if (retval)
179                 return retval;
180
181         if (!list->evdev->exist)
182                 return -ENODEV;
183
184         while (list->head != list->tail && retval + sizeof(struct input_event) <= count) {
185                 if (copy_to_user(buffer + retval, list->buffer + list->tail,
186                          sizeof(struct input_event))) return -EFAULT;
187                 list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
188                 retval += sizeof(struct input_event);
189         }
190
191         return retval;
192 }
193
194 /* No kernel lock - fine */
195 static unsigned int evdev_poll(struct file *file, poll_table *wait)
196 {
197         struct evdev_list *list = file->private_data;
198         poll_wait(file, &list->evdev->wait, wait);
199         if (list->head != list->tail)
200                 return POLLIN | POLLRDNORM;
201         return 0;
202 }
203
204 static int evdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
205 {
206         struct evdev_list *list = file->private_data;
207         struct evdev *evdev = list->evdev;
208         struct input_dev *dev = evdev->handle.dev;
209         struct input_absinfo abs;
210         void __user *p = (void __user *)arg;
211         int __user *ip = (int __user *)arg;
212         int i, t, u, v;
213
214         if (!evdev->exist) return -ENODEV;
215
216         switch (cmd) {
217
218                 case EVIOCGVERSION:
219                         return put_user(EV_VERSION, ip);
220
221                 case EVIOCGID:
222                         return copy_to_user(p, &dev->id, sizeof(struct input_id)) ? -EFAULT : 0;
223
224                 case EVIOCGKEYCODE:
225                         if (get_user(t, ip)) return -EFAULT;
226                         if (t < 0 || t > dev->keycodemax || !dev->keycodesize) return -EINVAL;
227                         if (put_user(INPUT_KEYCODE(dev, t), ip + 1)) return -EFAULT;
228                         return 0;
229
230                 case EVIOCSKEYCODE:
231                         if (get_user(t, ip)) return -EFAULT;
232                         if (t < 0 || t > dev->keycodemax || !dev->keycodesize) return -EINVAL;
233                         if (get_user(v, ip + 1)) return -EFAULT;
234                         u = SET_INPUT_KEYCODE(dev, t, v);
235                         clear_bit(u, dev->keybit);
236                         set_bit(v, dev->keybit);
237                         for (i = 0; i < dev->keycodemax; i++)
238                                 if (INPUT_KEYCODE(dev,i) == u)
239                                         set_bit(u, dev->keybit);
240                         return 0;
241
242                 case EVIOCSFF:
243                         if (dev->upload_effect) {
244                                 struct ff_effect effect;
245                                 int err;
246
247                                 if (copy_from_user(&effect, p, sizeof(effect)))
248                                         return -EFAULT;
249                                 err = dev->upload_effect(dev, &effect);
250                                 if (put_user(effect.id, &(((struct ff_effect __user *)arg)->id)))
251                                         return -EFAULT;
252                                 return err;
253                         }
254                         else return -ENOSYS;
255
256                 case EVIOCRMFF:
257                         if (dev->erase_effect) {
258                                 return dev->erase_effect(dev, (int)arg);
259                         }
260                         else return -ENOSYS;
261
262                 case EVIOCGEFFECTS:
263                         if (put_user(dev->ff_effects_max, ip))
264                                 return -EFAULT;
265                         return 0;
266
267                 case EVIOCGRAB:
268                         if (arg) {
269                                 if (evdev->grab)
270                                         return -EBUSY;
271                                 if (input_grab_device(&evdev->handle))
272                                         return -EBUSY;
273                                 evdev->grab = list;
274                                 return 0;
275                         } else {
276                                 if (evdev->grab != list)
277                                         return -EINVAL;
278                                 input_release_device(&evdev->handle);
279                                 evdev->grab = NULL;
280                                 return 0;
281                         }
282
283                 default:
284
285                         if (_IOC_TYPE(cmd) != 'E' || _IOC_DIR(cmd) != _IOC_READ)
286                                 return -EINVAL;
287
288                         if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
289
290                                 long *bits;
291                                 int len;
292
293                                 switch (_IOC_NR(cmd) & EV_MAX) {
294                                         case      0: bits = dev->evbit;  len = EV_MAX;  break;
295                                         case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
296                                         case EV_REL: bits = dev->relbit; len = REL_MAX; break;
297                                         case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
298                                         case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
299                                         case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
300                                         case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
301                                         case EV_FF:  bits = dev->ffbit;  len = FF_MAX;  break;
302                                         default: return -EINVAL;
303                                 }
304                                 len = NBITS(len) * sizeof(long);
305                                 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
306                                 return copy_to_user(p, bits, len) ? -EFAULT : len;
307                         }
308
309                         if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0))) {
310                                 int len;
311                                 len = NBITS(KEY_MAX) * sizeof(long);
312                                 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
313                                 return copy_to_user(p, dev->key, len) ? -EFAULT : len;
314                         }
315
316                         if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0))) {
317                                 int len;
318                                 len = NBITS(LED_MAX) * sizeof(long);
319                                 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
320                                 return copy_to_user(p, dev->led, len) ? -EFAULT : len;
321                         }
322
323                         if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0))) {
324                                 int len;
325                                 len = NBITS(SND_MAX) * sizeof(long);
326                                 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
327                                 return copy_to_user(p, dev->snd, len) ? -EFAULT : len;
328                         }
329
330                         if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) {
331                                 int len;
332                                 if (!dev->name) return -ENOENT;
333                                 len = strlen(dev->name) + 1;
334                                 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
335                                 return copy_to_user(p, dev->name, len) ? -EFAULT : len;
336                         }
337
338                         if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) {
339                                 int len;
340                                 if (!dev->phys) return -ENOENT;
341                                 len = strlen(dev->phys) + 1;
342                                 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
343                                 return copy_to_user(p, dev->phys, len) ? -EFAULT : len;
344                         }
345
346                         if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) {
347                                 int len;
348                                 if (!dev->uniq) return -ENOENT;
349                                 len = strlen(dev->uniq) + 1;
350                                 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
351                                 return copy_to_user(p, dev->uniq, len) ? -EFAULT : len;
352                         }
353
354                         if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
355
356                                 int t = _IOC_NR(cmd) & ABS_MAX;
357
358                                 abs.value = dev->abs[t];
359                                 abs.minimum = dev->absmin[t];
360                                 abs.maximum = dev->absmax[t];
361                                 abs.fuzz = dev->absfuzz[t];
362                                 abs.flat = dev->absflat[t];
363
364                                 if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
365                                         return -EFAULT;
366
367                                 return 0;
368                         }
369
370                         if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
371
372                                 int t = _IOC_NR(cmd) & ABS_MAX;
373
374                                 if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
375                                         return -EFAULT;
376
377                                 dev->abs[t] = abs.value;
378                                 dev->absmin[t] = abs.minimum;
379                                 dev->absmax[t] = abs.maximum;
380                                 dev->absfuzz[t] = abs.fuzz;
381                                 dev->absflat[t] = abs.flat;
382
383                                 return 0;
384                         }
385         }
386         return -EINVAL;
387 }
388
389 static struct file_operations evdev_fops = {
390         .owner =        THIS_MODULE,
391         .read =         evdev_read,
392         .write =        evdev_write,
393         .poll =         evdev_poll,
394         .open =         evdev_open,
395         .release =      evdev_release,
396         .ioctl =        evdev_ioctl,
397         .fasync =       evdev_fasync,
398         .flush =        evdev_flush
399 };
400
401 static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
402 {
403         struct evdev *evdev;
404         int minor;
405
406         for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
407         if (minor == EVDEV_MINORS) {
408                 printk(KERN_ERR "evdev: no more free evdev devices\n");
409                 return NULL;
410         }
411
412         if (!(evdev = kmalloc(sizeof(struct evdev), GFP_KERNEL)))
413                 return NULL;
414         memset(evdev, 0, sizeof(struct evdev));
415
416         INIT_LIST_HEAD(&evdev->list);
417         init_waitqueue_head(&evdev->wait);
418
419         evdev->exist = 1;
420         evdev->minor = minor;
421         evdev->handle.dev = dev;
422         evdev->handle.name = evdev->name;
423         evdev->handle.handler = handler;
424         evdev->handle.private = evdev;
425         sprintf(evdev->name, "event%d", minor);
426
427         evdev_table[minor] = evdev;
428
429         devfs_mk_cdev(MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
430                         S_IFCHR|S_IRUGO|S_IWUSR, "input/event%d", minor);
431         class_simple_device_add(input_class,
432                                 MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
433                                 dev->dev, "event%d", minor);
434
435         return &evdev->handle;
436 }
437
438 static void evdev_disconnect(struct input_handle *handle)
439 {
440         struct evdev *evdev = handle->private;
441
442         class_simple_device_remove(MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor));
443         devfs_remove("input/event%d", evdev->minor);
444         evdev->exist = 0;
445
446         if (evdev->open) {
447                 input_close_device(handle);
448                 wake_up_interruptible(&evdev->wait);
449         } else
450                 evdev_free(evdev);
451 }
452
453 static struct input_device_id evdev_ids[] = {
454         { .driver_info = 1 },   /* Matches all devices */
455         { },                    /* Terminating zero entry */
456 };
457
458 MODULE_DEVICE_TABLE(input, evdev_ids);
459
460 static struct input_handler evdev_handler = {
461         .event =        evdev_event,
462         .connect =      evdev_connect,
463         .disconnect =   evdev_disconnect,
464         .fops =         &evdev_fops,
465         .minor =        EVDEV_MINOR_BASE,
466         .name =         "evdev",
467         .id_table =     evdev_ids,
468 };
469
470 static int __init evdev_init(void)
471 {
472         input_register_handler(&evdev_handler);
473         return 0;
474 }
475
476 static void __exit evdev_exit(void)
477 {
478         input_unregister_handler(&evdev_handler);
479 }
480
481 module_init(evdev_init);
482 module_exit(evdev_exit);
483
484 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
485 MODULE_DESCRIPTION("Input driver event char devices");
486 MODULE_LICENSE("GPL");