patch-2_6_7-vs1_9_1_12
[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         devfs_remove("input/event%d", evdev->minor);
95         class_simple_device_remove(MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor));
96         evdev_table[evdev->minor] = NULL;
97         kfree(evdev);
98 }
99
100 static int evdev_release(struct inode * inode, struct file * file)
101 {
102         struct evdev_list *list = file->private_data;
103
104         if (list->evdev->grab == list) {
105                 input_release_device(&list->evdev->handle);
106                 list->evdev->grab = NULL;
107         }
108
109         evdev_fasync(-1, file, 0);
110         list_del(&list->node);
111
112         if (!--list->evdev->open) {
113                 if (list->evdev->exist)
114                         input_close_device(&list->evdev->handle);
115                 else
116                         evdev_free(list->evdev);
117         }
118
119         kfree(list);
120         return 0;
121 }
122
123 static int evdev_open(struct inode * inode, struct file * file)
124 {
125         struct evdev_list *list;
126         int i = iminor(inode) - EVDEV_MINOR_BASE;
127         int accept_err;
128
129         if (i >= EVDEV_MINORS || !evdev_table[i] || !evdev_table[i]->exist)
130                 return -ENODEV;
131
132         if ((accept_err = input_accept_process(&(evdev_table[i]->handle), file)))
133                 return accept_err;
134
135         if (!(list = kmalloc(sizeof(struct evdev_list), GFP_KERNEL)))
136                 return -ENOMEM;
137         memset(list, 0, sizeof(struct evdev_list));
138
139         list->evdev = evdev_table[i];
140         list_add_tail(&list->node, &evdev_table[i]->list);
141         file->private_data = list;
142
143         if (!list->evdev->open++)
144                 if (list->evdev->exist)
145                         input_open_device(&list->evdev->handle);
146
147         return 0;
148 }
149
150 static ssize_t evdev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
151 {
152         struct evdev_list *list = file->private_data;
153         struct input_event event;
154         int retval = 0;
155
156         if (!list->evdev->exist) return -ENODEV;
157
158         while (retval < count) {
159
160                 if (copy_from_user(&event, buffer + retval, sizeof(struct input_event)))
161                         return -EFAULT;
162                 input_event(list->evdev->handle.dev, event.type, event.code, event.value);
163                 retval += sizeof(struct input_event);
164         }
165
166         return retval;
167 }
168
169 static ssize_t evdev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
170 {
171         struct evdev_list *list = file->private_data;
172         int retval;
173
174         if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
175                 return -EAGAIN;
176
177         retval = wait_event_interruptible(list->evdev->wait,
178                 list->head != list->tail || (!list->evdev->exist));
179
180         if (retval)
181                 return retval;
182
183         if (!list->evdev->exist)
184                 return -ENODEV;
185
186         while (list->head != list->tail && retval + sizeof(struct input_event) <= count) {
187                 if (copy_to_user(buffer + retval, list->buffer + list->tail,
188                          sizeof(struct input_event))) return -EFAULT;
189                 list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
190                 retval += sizeof(struct input_event);
191         }
192
193         return retval;
194 }
195
196 /* No kernel lock - fine */
197 static unsigned int evdev_poll(struct file *file, poll_table *wait)
198 {
199         struct evdev_list *list = file->private_data;
200         poll_wait(file, &list->evdev->wait, wait);
201         if (list->head != list->tail)
202                 return POLLIN | POLLRDNORM;
203         return 0;
204 }
205
206 static int evdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
207 {
208         struct evdev_list *list = file->private_data;
209         struct evdev *evdev = list->evdev;
210         struct input_dev *dev = evdev->handle.dev;
211         struct input_absinfo abs;
212         void __user *p = (void __user *)arg;
213         int __user *ip = (int __user *)arg;
214         int i, t, u, v;
215
216         if (!evdev->exist) return -ENODEV;
217
218         switch (cmd) {
219
220                 case EVIOCGVERSION:
221                         return put_user(EV_VERSION, ip);
222
223                 case EVIOCGID:
224                         return copy_to_user(p, &dev->id, sizeof(struct input_id)) ? -EFAULT : 0;
225
226                 case EVIOCGKEYCODE:
227                         if (get_user(t, ip)) return -EFAULT;
228                         if (t < 0 || t > dev->keycodemax || !dev->keycodesize) return -EINVAL;
229                         if (put_user(INPUT_KEYCODE(dev, t), ip + 1)) return -EFAULT;
230                         return 0;
231
232                 case EVIOCSKEYCODE:
233                         if (get_user(t, ip)) return -EFAULT;
234                         if (t < 0 || t > dev->keycodemax || !dev->keycodesize) return -EINVAL;
235                         if (get_user(v, ip + 1)) return -EFAULT;
236                         u = SET_INPUT_KEYCODE(dev, t, v);
237                         clear_bit(u, dev->keybit);
238                         set_bit(v, dev->keybit);
239                         for (i = 0; i < dev->keycodemax; i++)
240                                 if (INPUT_KEYCODE(dev,i) == u)
241                                         set_bit(u, dev->keybit);
242                         return 0;
243
244                 case EVIOCSFF:
245                         if (dev->upload_effect) {
246                                 struct ff_effect effect;
247                                 int err;
248
249                                 if (copy_from_user(&effect, p, sizeof(effect)))
250                                         return -EFAULT;
251                                 err = dev->upload_effect(dev, &effect);
252                                 if (put_user(effect.id, &(((struct ff_effect __user *)arg)->id)))
253                                         return -EFAULT;
254                                 return err;
255                         }
256                         else return -ENOSYS;
257
258                 case EVIOCRMFF:
259                         if (dev->erase_effect) {
260                                 return dev->erase_effect(dev, (int)arg);
261                         }
262                         else return -ENOSYS;
263
264                 case EVIOCGEFFECTS:
265                         if (put_user(dev->ff_effects_max, ip))
266                                 return -EFAULT;
267                         return 0;
268
269                 case EVIOCGRAB:
270                         if (arg) {
271                                 if (evdev->grab)
272                                         return -EBUSY;
273                                 if (input_grab_device(&evdev->handle))
274                                         return -EBUSY;
275                                 evdev->grab = list;
276                                 return 0;
277                         } else {
278                                 if (evdev->grab != list)
279                                         return -EINVAL;
280                                 input_release_device(&evdev->handle);
281                                 evdev->grab = NULL;
282                                 return 0;
283                         }
284
285                 default:
286
287                         if (_IOC_TYPE(cmd) != 'E' || _IOC_DIR(cmd) != _IOC_READ)
288                                 return -EINVAL;
289
290                         if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
291
292                                 long *bits;
293                                 int len;
294
295                                 switch (_IOC_NR(cmd) & EV_MAX) {
296                                         case      0: bits = dev->evbit;  len = EV_MAX;  break;
297                                         case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
298                                         case EV_REL: bits = dev->relbit; len = REL_MAX; break;
299                                         case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
300                                         case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
301                                         case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
302                                         case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
303                                         case EV_FF:  bits = dev->ffbit;  len = FF_MAX;  break;
304                                         default: return -EINVAL;
305                                 }
306                                 len = NBITS(len) * sizeof(long);
307                                 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
308                                 return copy_to_user(p, bits, len) ? -EFAULT : len;
309                         }
310
311                         if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0))) {
312                                 int len;
313                                 len = NBITS(KEY_MAX) * sizeof(long);
314                                 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
315                                 return copy_to_user(p, dev->key, len) ? -EFAULT : len;
316                         }
317
318                         if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0))) {
319                                 int len;
320                                 len = NBITS(LED_MAX) * sizeof(long);
321                                 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
322                                 return copy_to_user(p, dev->led, len) ? -EFAULT : len;
323                         }
324
325                         if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0))) {
326                                 int len;
327                                 len = NBITS(SND_MAX) * sizeof(long);
328                                 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
329                                 return copy_to_user(p, dev->snd, len) ? -EFAULT : len;
330                         }
331
332                         if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) {
333                                 int len;
334                                 if (!dev->name) return -ENOENT;
335                                 len = strlen(dev->name) + 1;
336                                 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
337                                 return copy_to_user(p, dev->name, len) ? -EFAULT : len;
338                         }
339
340                         if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) {
341                                 int len;
342                                 if (!dev->phys) return -ENOENT;
343                                 len = strlen(dev->phys) + 1;
344                                 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
345                                 return copy_to_user(p, dev->phys, len) ? -EFAULT : len;
346                         }
347
348                         if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) {
349                                 int len;
350                                 if (!dev->uniq) return -ENOENT;
351                                 len = strlen(dev->uniq) + 1;
352                                 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
353                                 return copy_to_user(p, dev->uniq, len) ? -EFAULT : len;
354                         }
355
356                         if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
357
358                                 int t = _IOC_NR(cmd) & ABS_MAX;
359
360                                 abs.value = dev->abs[t];
361                                 abs.minimum = dev->absmin[t];
362                                 abs.maximum = dev->absmax[t];
363                                 abs.fuzz = dev->absfuzz[t];
364                                 abs.flat = dev->absflat[t];
365
366                                 if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
367                                         return -EFAULT;
368
369                                 return 0;
370                         }
371
372                         if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
373
374                                 int t = _IOC_NR(cmd) & ABS_MAX;
375
376                                 if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
377                                         return -EFAULT;
378
379                                 dev->abs[t] = abs.value;
380                                 dev->absmin[t] = abs.minimum;
381                                 dev->absmax[t] = abs.maximum;
382                                 dev->absfuzz[t] = abs.fuzz;
383                                 dev->absflat[t] = abs.flat;
384
385                                 return 0;
386                         }
387         }
388         return -EINVAL;
389 }
390
391 static struct file_operations evdev_fops = {
392         .owner =        THIS_MODULE,
393         .read =         evdev_read,
394         .write =        evdev_write,
395         .poll =         evdev_poll,
396         .open =         evdev_open,
397         .release =      evdev_release,
398         .ioctl =        evdev_ioctl,
399         .fasync =       evdev_fasync,
400         .flush =        evdev_flush
401 };
402
403 static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
404 {
405         struct evdev *evdev;
406         int minor;
407
408         for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
409         if (minor == EVDEV_MINORS) {
410                 printk(KERN_ERR "evdev: no more free evdev devices\n");
411                 return NULL;
412         }
413
414         if (!(evdev = kmalloc(sizeof(struct evdev), GFP_KERNEL)))
415                 return NULL;
416         memset(evdev, 0, sizeof(struct evdev));
417
418         INIT_LIST_HEAD(&evdev->list);
419         init_waitqueue_head(&evdev->wait);
420
421         evdev->exist = 1;
422         evdev->minor = minor;
423         evdev->handle.dev = dev;
424         evdev->handle.name = evdev->name;
425         evdev->handle.handler = handler;
426         evdev->handle.private = evdev;
427         sprintf(evdev->name, "event%d", minor);
428
429         evdev_table[minor] = evdev;
430
431         devfs_mk_cdev(MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
432                         S_IFCHR|S_IRUGO|S_IWUSR, "input/event%d", minor);
433         class_simple_device_add(input_class,
434                                 MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
435                                 dev->dev, "event%d", minor);
436
437         return &evdev->handle;
438 }
439
440 static void evdev_disconnect(struct input_handle *handle)
441 {
442         struct evdev *evdev = handle->private;
443
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");