[MIPS] Sparse: fix sparse for 64-bit kernels.
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / input / evdev.c
bloba34e3d91d9ed987d387bf77f1eab888e63029552
1 /*
2 * Event char devices, giving access to raw input device events.
4 * Copyright (c) 1999-2002 Vojtech Pavlik
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 */
11 #define EVDEV_MINOR_BASE 64
12 #define EVDEV_MINORS 32
13 #define EVDEV_BUFFER_SIZE 64
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>
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;
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;
45 static struct evdev *evdev_table[EVDEV_MINORS];
47 static void evdev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
49 struct evdev *evdev = handle->private;
50 struct evdev_list *list;
52 if (evdev->grab) {
53 list = evdev->grab;
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);
61 kill_fasync(&list->fasync, SIGIO, POLL_IN);
62 } else
63 list_for_each_entry(list, &evdev->list, node) {
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);
71 kill_fasync(&list->fasync, SIGIO, POLL_IN);
74 wake_up_interruptible(&evdev->wait);
77 static int evdev_fasync(int fd, struct file *file, int on)
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;
85 static int evdev_flush(struct file * file)
87 struct evdev_list *list = file->private_data;
88 if (!list->evdev->exist) return -ENODEV;
89 return input_flush_device(&list->evdev->handle, file);
92 static void evdev_free(struct evdev *evdev)
94 evdev_table[evdev->minor] = NULL;
95 kfree(evdev);
98 static int evdev_release(struct inode * inode, struct file * file)
100 struct evdev_list *list = file->private_data;
102 if (list->evdev->grab == list) {
103 input_release_device(&list->evdev->handle);
104 list->evdev->grab = NULL;
107 evdev_fasync(-1, file, 0);
108 list_del(&list->node);
110 if (!--list->evdev->open) {
111 if (list->evdev->exist)
112 input_close_device(&list->evdev->handle);
113 else
114 evdev_free(list->evdev);
117 kfree(list);
118 return 0;
121 static int evdev_open(struct inode * inode, struct file * file)
123 struct evdev_list *list;
124 int i = iminor(inode) - EVDEV_MINOR_BASE;
125 int accept_err;
127 if (i >= EVDEV_MINORS || !evdev_table[i] || !evdev_table[i]->exist)
128 return -ENODEV;
130 if ((accept_err = input_accept_process(&(evdev_table[i]->handle), file)))
131 return accept_err;
133 if (!(list = kzalloc(sizeof(struct evdev_list), GFP_KERNEL)))
134 return -ENOMEM;
136 list->evdev = evdev_table[i];
137 list_add_tail(&list->node, &evdev_table[i]->list);
138 file->private_data = list;
140 if (!list->evdev->open++)
141 if (list->evdev->exist)
142 input_open_device(&list->evdev->handle);
144 return 0;
147 #ifdef CONFIG_COMPAT
149 struct input_event_compat {
150 struct compat_timeval time;
151 __u16 type;
152 __u16 code;
153 __s32 value;
156 /* Note to the author of this code: did it ever occur to
157 you why the ifdefs are needed? Think about it again. -AK */
158 #ifdef CONFIG_X86_64
159 # define COMPAT_TEST is_compat_task()
160 #elif defined(CONFIG_IA64)
161 # define COMPAT_TEST IS_IA32_PROCESS(task_pt_regs(current))
162 #elif defined(CONFIG_S390)
163 # define COMPAT_TEST test_thread_flag(TIF_31BIT)
164 #elif defined(CONFIG_MIPS)
165 # define COMPAT_TEST (current->thread.mflags & MF_32BIT_ADDR)
166 #else
167 # define COMPAT_TEST test_thread_flag(TIF_32BIT)
168 #endif
170 static inline size_t evdev_event_size(void)
172 return COMPAT_TEST ?
173 sizeof(struct input_event_compat) : sizeof(struct input_event);
176 static int evdev_event_from_user(const char __user *buffer, struct input_event *event)
178 if (COMPAT_TEST) {
179 struct input_event_compat compat_event;
181 if (copy_from_user(&compat_event, buffer, sizeof(struct input_event_compat)))
182 return -EFAULT;
184 event->time.tv_sec = compat_event.time.tv_sec;
185 event->time.tv_usec = compat_event.time.tv_usec;
186 event->type = compat_event.type;
187 event->code = compat_event.code;
188 event->value = compat_event.value;
190 } else {
191 if (copy_from_user(event, buffer, sizeof(struct input_event)))
192 return -EFAULT;
195 return 0;
198 static int evdev_event_to_user(char __user *buffer, const struct input_event *event)
200 if (COMPAT_TEST) {
201 struct input_event_compat compat_event;
203 compat_event.time.tv_sec = event->time.tv_sec;
204 compat_event.time.tv_usec = event->time.tv_usec;
205 compat_event.type = event->type;
206 compat_event.code = event->code;
207 compat_event.value = event->value;
209 if (copy_to_user(buffer, &compat_event, sizeof(struct input_event_compat)))
210 return -EFAULT;
212 } else {
213 if (copy_to_user(buffer, event, sizeof(struct input_event)))
214 return -EFAULT;
217 return 0;
220 #else
222 static inline size_t evdev_event_size(void)
224 return sizeof(struct input_event);
227 static int evdev_event_from_user(const char __user *buffer, struct input_event *event)
229 if (copy_from_user(event, buffer, sizeof(struct input_event)))
230 return -EFAULT;
232 return 0;
235 static int evdev_event_to_user(char __user *buffer, const struct input_event *event)
237 if (copy_to_user(buffer, event, sizeof(struct input_event)))
238 return -EFAULT;
240 return 0;
243 #endif /* CONFIG_COMPAT */
245 static ssize_t evdev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
247 struct evdev_list *list = file->private_data;
248 struct input_event event;
249 int retval = 0;
251 if (!list->evdev->exist)
252 return -ENODEV;
254 while (retval < count) {
256 if (evdev_event_from_user(buffer + retval, &event))
257 return -EFAULT;
258 input_event(list->evdev->handle.dev, event.type, event.code, event.value);
259 retval += evdev_event_size();
262 return retval;
265 static ssize_t evdev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
267 struct evdev_list *list = file->private_data;
268 int retval;
270 if (count < evdev_event_size())
271 return -EINVAL;
273 if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
274 return -EAGAIN;
276 retval = wait_event_interruptible(list->evdev->wait,
277 list->head != list->tail || (!list->evdev->exist));
279 if (retval)
280 return retval;
282 if (!list->evdev->exist)
283 return -ENODEV;
285 while (list->head != list->tail && retval + evdev_event_size() <= count) {
287 struct input_event *event = (struct input_event *) list->buffer + list->tail;
289 if (evdev_event_to_user(buffer + retval, event))
290 return -EFAULT;
292 list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
293 retval += evdev_event_size();
296 return retval;
299 /* No kernel lock - fine */
300 static unsigned int evdev_poll(struct file *file, poll_table *wait)
302 struct evdev_list *list = file->private_data;
303 poll_wait(file, &list->evdev->wait, wait);
304 return ((list->head == list->tail) ? 0 : (POLLIN | POLLRDNORM)) |
305 (list->evdev->exist ? 0 : (POLLHUP | POLLERR));
308 #ifdef CONFIG_COMPAT
310 #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
311 #define NBITS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
313 #ifdef __BIG_ENDIAN
314 static int bits_to_user(unsigned long *bits, unsigned int maxbit,
315 unsigned int maxlen, void __user *p, int compat)
317 int len, i;
319 if (compat) {
320 len = NBITS_COMPAT(maxbit) * sizeof(compat_long_t);
321 if (len < maxlen)
322 len = maxlen;
324 for (i = 0; i < len / sizeof(compat_long_t); i++)
325 if (copy_to_user((compat_long_t __user *) p + i,
326 (compat_long_t *) bits +
327 i + 1 - ((i % 2) << 1),
328 sizeof(compat_long_t)))
329 return -EFAULT;
330 } else {
331 len = NBITS(maxbit) * sizeof(long);
332 if (len > maxlen)
333 len = maxlen;
335 if (copy_to_user(p, bits, len))
336 return -EFAULT;
339 return len;
341 #else
342 static int bits_to_user(unsigned long *bits, unsigned int maxbit,
343 unsigned int maxlen, void __user *p, int compat)
345 int len = compat ?
346 NBITS_COMPAT(maxbit) * sizeof(compat_long_t) :
347 NBITS(maxbit) * sizeof(long);
349 if (len > maxlen)
350 len = maxlen;
352 return copy_to_user(p, bits, len) ? -EFAULT : len;
354 #endif /* __BIG_ENDIAN */
356 #else
358 static int bits_to_user(unsigned long *bits, unsigned int maxbit,
359 unsigned int maxlen, void __user *p, int compat)
361 int len = NBITS(maxbit) * sizeof(long);
363 if (len > maxlen)
364 len = maxlen;
366 return copy_to_user(p, bits, len) ? -EFAULT : len;
369 #endif /* CONFIG_COMPAT */
371 static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
373 int len;
375 if (!str)
376 return -ENOENT;
378 len = strlen(str) + 1;
379 if (len > maxlen)
380 len = maxlen;
382 return copy_to_user(p, str, len) ? -EFAULT : len;
385 static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
386 void __user *p, int compat_mode)
388 struct evdev_list *list = file->private_data;
389 struct evdev *evdev = list->evdev;
390 struct input_dev *dev = evdev->handle.dev;
391 struct input_absinfo abs;
392 int __user *ip = (int __user *)p;
393 int i, t, u, v;
395 if (!evdev->exist)
396 return -ENODEV;
398 switch (cmd) {
400 case EVIOCGVERSION:
401 return put_user(EV_VERSION, ip);
403 case EVIOCGID:
404 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
405 return -EFAULT;
407 return 0;
409 case EVIOCGKEYCODE:
410 if (get_user(t, ip))
411 return -EFAULT;
412 if (t < 0 || t >= dev->keycodemax || !dev->keycodesize)
413 return -EINVAL;
414 if (put_user(INPUT_KEYCODE(dev, t), ip + 1))
415 return -EFAULT;
416 return 0;
418 case EVIOCSKEYCODE:
419 if (get_user(t, ip))
420 return -EFAULT;
421 if (t < 0 || t >= dev->keycodemax || !dev->keycodesize)
422 return -EINVAL;
423 if (get_user(v, ip + 1))
424 return -EFAULT;
425 if (v < 0 || v > KEY_MAX)
426 return -EINVAL;
427 if (dev->keycodesize < sizeof(v) && (v >> (dev->keycodesize * 8)))
428 return -EINVAL;
430 u = SET_INPUT_KEYCODE(dev, t, v);
431 clear_bit(u, dev->keybit);
432 set_bit(v, dev->keybit);
433 for (i = 0; i < dev->keycodemax; i++)
434 if (INPUT_KEYCODE(dev, i) == u)
435 set_bit(u, dev->keybit);
437 return 0;
439 case EVIOCSFF:
440 if (dev->upload_effect) {
441 struct ff_effect effect;
442 int err;
444 if (copy_from_user(&effect, p, sizeof(effect)))
445 return -EFAULT;
446 err = dev->upload_effect(dev, &effect);
447 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
448 return -EFAULT;
449 return err;
450 } else
451 return -ENOSYS;
453 case EVIOCRMFF:
454 if (!dev->erase_effect)
455 return -ENOSYS;
457 return dev->erase_effect(dev, (int)(unsigned long) p);
459 case EVIOCGEFFECTS:
460 if (put_user(dev->ff_effects_max, ip))
461 return -EFAULT;
462 return 0;
464 case EVIOCGRAB:
465 if (p) {
466 if (evdev->grab)
467 return -EBUSY;
468 if (input_grab_device(&evdev->handle))
469 return -EBUSY;
470 evdev->grab = list;
471 return 0;
472 } else {
473 if (evdev->grab != list)
474 return -EINVAL;
475 input_release_device(&evdev->handle);
476 evdev->grab = NULL;
477 return 0;
480 default:
482 if (_IOC_TYPE(cmd) != 'E')
483 return -EINVAL;
485 if (_IOC_DIR(cmd) == _IOC_READ) {
487 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
489 long *bits;
490 int len;
492 switch (_IOC_NR(cmd) & EV_MAX) {
493 case 0: bits = dev->evbit; len = EV_MAX; break;
494 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
495 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
496 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
497 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
498 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
499 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
500 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
501 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
502 default: return -EINVAL;
504 return bits_to_user(bits, len, _IOC_SIZE(cmd), p, compat_mode);
507 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0)))
508 return bits_to_user(dev->key, KEY_MAX, _IOC_SIZE(cmd),
509 p, compat_mode);
511 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0)))
512 return bits_to_user(dev->led, LED_MAX, _IOC_SIZE(cmd),
513 p, compat_mode);
515 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0)))
516 return bits_to_user(dev->snd, SND_MAX, _IOC_SIZE(cmd),
517 p, compat_mode);
519 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSW(0)))
520 return bits_to_user(dev->sw, SW_MAX, _IOC_SIZE(cmd),
521 p, compat_mode);
523 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0)))
524 return str_to_user(dev->name, _IOC_SIZE(cmd), p);
526 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0)))
527 return str_to_user(dev->phys, _IOC_SIZE(cmd), p);
529 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0)))
530 return str_to_user(dev->uniq, _IOC_SIZE(cmd), p);
532 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
534 int t = _IOC_NR(cmd) & ABS_MAX;
536 abs.value = dev->abs[t];
537 abs.minimum = dev->absmin[t];
538 abs.maximum = dev->absmax[t];
539 abs.fuzz = dev->absfuzz[t];
540 abs.flat = dev->absflat[t];
542 if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
543 return -EFAULT;
545 return 0;
550 if (_IOC_DIR(cmd) == _IOC_WRITE) {
552 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
554 int t = _IOC_NR(cmd) & ABS_MAX;
556 if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
557 return -EFAULT;
559 dev->abs[t] = abs.value;
560 dev->absmin[t] = abs.minimum;
561 dev->absmax[t] = abs.maximum;
562 dev->absfuzz[t] = abs.fuzz;
563 dev->absflat[t] = abs.flat;
565 return 0;
569 return -EINVAL;
572 static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
574 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
577 #ifdef CONFIG_COMPAT
578 static long evdev_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
580 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
582 #endif
584 static struct file_operations evdev_fops = {
585 .owner = THIS_MODULE,
586 .read = evdev_read,
587 .write = evdev_write,
588 .poll = evdev_poll,
589 .open = evdev_open,
590 .release = evdev_release,
591 .unlocked_ioctl = evdev_ioctl,
592 #ifdef CONFIG_COMPAT
593 .compat_ioctl = evdev_ioctl_compat,
594 #endif
595 .fasync = evdev_fasync,
596 .flush = evdev_flush
599 static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
601 struct evdev *evdev;
602 struct class_device *cdev;
603 int minor;
605 for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
606 if (minor == EVDEV_MINORS) {
607 printk(KERN_ERR "evdev: no more free evdev devices\n");
608 return NULL;
611 if (!(evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL)))
612 return NULL;
614 INIT_LIST_HEAD(&evdev->list);
615 init_waitqueue_head(&evdev->wait);
617 evdev->exist = 1;
618 evdev->minor = minor;
619 evdev->handle.dev = dev;
620 evdev->handle.name = evdev->name;
621 evdev->handle.handler = handler;
622 evdev->handle.private = evdev;
623 sprintf(evdev->name, "event%d", minor);
625 evdev_table[minor] = evdev;
627 cdev = class_device_create(&input_class, &dev->cdev,
628 MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
629 dev->cdev.dev, evdev->name);
631 /* temporary symlink to keep userspace happy */
632 sysfs_create_link(&input_class.subsys.kset.kobj, &cdev->kobj,
633 evdev->name);
635 return &evdev->handle;
638 static void evdev_disconnect(struct input_handle *handle)
640 struct evdev *evdev = handle->private;
641 struct evdev_list *list;
643 sysfs_remove_link(&input_class.subsys.kset.kobj, evdev->name);
644 class_device_destroy(&input_class,
645 MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor));
646 evdev->exist = 0;
648 if (evdev->open) {
649 input_close_device(handle);
650 wake_up_interruptible(&evdev->wait);
651 list_for_each_entry(list, &evdev->list, node)
652 kill_fasync(&list->fasync, SIGIO, POLL_HUP);
653 } else
654 evdev_free(evdev);
657 static struct input_device_id evdev_ids[] = {
658 { .driver_info = 1 }, /* Matches all devices */
659 { }, /* Terminating zero entry */
662 MODULE_DEVICE_TABLE(input, evdev_ids);
664 static struct input_handler evdev_handler = {
665 .event = evdev_event,
666 .connect = evdev_connect,
667 .disconnect = evdev_disconnect,
668 .fops = &evdev_fops,
669 .minor = EVDEV_MINOR_BASE,
670 .name = "evdev",
671 .id_table = evdev_ids,
674 static int __init evdev_init(void)
676 input_register_handler(&evdev_handler);
677 return 0;
680 static void __exit evdev_exit(void)
682 input_unregister_handler(&evdev_handler);
685 module_init(evdev_init);
686 module_exit(evdev_exit);
688 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
689 MODULE_DESCRIPTION("Input driver event char devices");
690 MODULE_LICENSE("GPL");