ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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])
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 * 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 * 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         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, (int *) arg);
220
221                 case EVIOCGID:
222                         return copy_to_user((void *) arg, &dev->id, sizeof(struct input_id)) ? -EFAULT : 0;
223                 
224                 case EVIOCGKEYCODE:
225                         if (get_user(t, ((int *) arg) + 0)) return -EFAULT;
226                         if (t < 0 || t > dev->keycodemax || !dev->keycodesize) return -EINVAL;
227                         if (put_user(INPUT_KEYCODE(dev, t), ((int *) arg) + 1)) return -EFAULT;
228                         return 0;
229
230                 case EVIOCSKEYCODE:
231                         if (get_user(t, ((int *) arg) + 0)) return -EFAULT;
232                         if (t < 0 || t > dev->keycodemax || !dev->keycodesize) return -EINVAL;
233                         if (get_user(v, ((int *) arg) + 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((void*)(&effect), (void*)arg, sizeof(effect)))
248                                         return -EFAULT;
249                                 err = dev->upload_effect(dev, &effect);
250                                 if (put_user(effect.id, &(((struct ff_effect*)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, (int*) arg))
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((char *) arg, 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((char *) arg, 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((char *) arg, 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((char *) arg, 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((char *) arg, 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((char *) arg, 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((char *) arg, 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((void *) arg, &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, (void *) arg, 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         evdev->exist = 0;
443
444         if (evdev->open) {
445                 input_close_device(handle);
446                 wake_up_interruptible(&evdev->wait);
447         } else
448                 evdev_free(evdev);
449 }
450
451 static struct input_device_id evdev_ids[] = {
452         { .driver_info = 1 },   /* Matches all devices */
453         { },                    /* Terminating zero entry */
454 };
455
456 MODULE_DEVICE_TABLE(input, evdev_ids);
457
458 static struct input_handler evdev_handler = {
459         .event =        evdev_event,
460         .connect =      evdev_connect,
461         .disconnect =   evdev_disconnect,
462         .fops =         &evdev_fops,
463         .minor =        EVDEV_MINOR_BASE,
464         .name =         "evdev",
465         .id_table =     evdev_ids,
466 };
467
468 static int __init evdev_init(void)
469 {
470         input_register_handler(&evdev_handler);
471         return 0;
472 }
473
474 static void __exit evdev_exit(void)
475 {
476         input_unregister_handler(&evdev_handler);
477 }
478
479 module_init(evdev_init);
480 module_exit(evdev_exit);
481
482 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
483 MODULE_DESCRIPTION("Input driver event char devices");
484 MODULE_LICENSE("GPL");