[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / drivers / input / evdev.c
blob17552a29978b862853e3bacf413547e2237e5931
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/devfs_fs_kernel.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 = kmalloc(sizeof(struct evdev_list), GFP_KERNEL)))
134 return -ENOMEM;
135 memset(list, 0, sizeof(struct evdev_list));
137 list->evdev = evdev_table[i];
138 list_add_tail(&list->node, &evdev_table[i]->list);
139 file->private_data = list;
141 if (!list->evdev->open++)
142 if (list->evdev->exist)
143 input_open_device(&list->evdev->handle);
145 return 0;
148 static ssize_t evdev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
150 struct evdev_list *list = file->private_data;
151 struct input_event event;
152 int retval = 0;
154 if (!list->evdev->exist) return -ENODEV;
156 while (retval < count) {
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);
164 return retval;
167 static ssize_t evdev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
169 struct evdev_list *list = file->private_data;
170 int retval;
172 if (count < sizeof(struct input_event))
173 return -EINVAL;
175 if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
176 return -EAGAIN;
178 retval = wait_event_interruptible(list->evdev->wait,
179 list->head != list->tail || (!list->evdev->exist));
181 if (retval)
182 return retval;
184 if (!list->evdev->exist)
185 return -ENODEV;
187 while (list->head != list->tail && retval + sizeof(struct input_event) <= count) {
188 if (copy_to_user(buffer + retval, list->buffer + list->tail,
189 sizeof(struct input_event))) return -EFAULT;
190 list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
191 retval += sizeof(struct input_event);
194 return retval;
197 /* No kernel lock - fine */
198 static unsigned int evdev_poll(struct file *file, poll_table *wait)
200 struct evdev_list *list = file->private_data;
201 poll_wait(file, &list->evdev->wait, wait);
202 return ((list->head == list->tail) ? 0 : (POLLIN | POLLRDNORM)) |
203 (list->evdev->exist ? 0 : (POLLHUP | POLLERR));
206 static int evdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
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;
216 if (!evdev->exist) return -ENODEV;
218 switch (cmd) {
220 case EVIOCGVERSION:
221 return put_user(EV_VERSION, ip);
223 case EVIOCGID:
224 return copy_to_user(p, &dev->id, sizeof(struct input_id)) ? -EFAULT : 0;
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;
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 if (v < 0 || v > KEY_MAX) return -EINVAL;
237 u = SET_INPUT_KEYCODE(dev, t, v);
238 clear_bit(u, dev->keybit);
239 set_bit(v, dev->keybit);
240 for (i = 0; i < dev->keycodemax; i++)
241 if (INPUT_KEYCODE(dev,i) == u)
242 set_bit(u, dev->keybit);
243 return 0;
245 case EVIOCSFF:
246 if (dev->upload_effect) {
247 struct ff_effect effect;
248 int err;
250 if (copy_from_user(&effect, p, sizeof(effect)))
251 return -EFAULT;
252 err = dev->upload_effect(dev, &effect);
253 if (put_user(effect.id, &(((struct ff_effect __user *)arg)->id)))
254 return -EFAULT;
255 return err;
257 else return -ENOSYS;
259 case EVIOCRMFF:
260 if (dev->erase_effect) {
261 return dev->erase_effect(dev, (int)arg);
263 else return -ENOSYS;
265 case EVIOCGEFFECTS:
266 if (put_user(dev->ff_effects_max, ip))
267 return -EFAULT;
268 return 0;
270 case EVIOCGRAB:
271 if (arg) {
272 if (evdev->grab)
273 return -EBUSY;
274 if (input_grab_device(&evdev->handle))
275 return -EBUSY;
276 evdev->grab = list;
277 return 0;
278 } else {
279 if (evdev->grab != list)
280 return -EINVAL;
281 input_release_device(&evdev->handle);
282 evdev->grab = NULL;
283 return 0;
286 default:
288 if (_IOC_TYPE(cmd) != 'E' || _IOC_DIR(cmd) != _IOC_READ)
289 return -EINVAL;
291 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
293 long *bits;
294 int len;
296 switch (_IOC_NR(cmd) & EV_MAX) {
297 case 0: bits = dev->evbit; len = EV_MAX; break;
298 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
299 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
300 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
301 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
302 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
303 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
304 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
305 default: return -EINVAL;
307 len = NBITS(len) * sizeof(long);
308 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
309 return copy_to_user(p, bits, len) ? -EFAULT : len;
312 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0))) {
313 int len;
314 len = NBITS(KEY_MAX) * sizeof(long);
315 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
316 return copy_to_user(p, dev->key, len) ? -EFAULT : len;
319 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0))) {
320 int len;
321 len = NBITS(LED_MAX) * sizeof(long);
322 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
323 return copy_to_user(p, dev->led, len) ? -EFAULT : len;
326 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0))) {
327 int len;
328 len = NBITS(SND_MAX) * sizeof(long);
329 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
330 return copy_to_user(p, dev->snd, len) ? -EFAULT : len;
333 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) {
334 int len;
335 if (!dev->name) return -ENOENT;
336 len = strlen(dev->name) + 1;
337 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
338 return copy_to_user(p, dev->name, len) ? -EFAULT : len;
341 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) {
342 int len;
343 if (!dev->phys) return -ENOENT;
344 len = strlen(dev->phys) + 1;
345 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
346 return copy_to_user(p, dev->phys, len) ? -EFAULT : len;
349 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) {
350 int len;
351 if (!dev->uniq) return -ENOENT;
352 len = strlen(dev->uniq) + 1;
353 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
354 return copy_to_user(p, dev->uniq, len) ? -EFAULT : len;
357 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
359 int t = _IOC_NR(cmd) & ABS_MAX;
361 abs.value = dev->abs[t];
362 abs.minimum = dev->absmin[t];
363 abs.maximum = dev->absmax[t];
364 abs.fuzz = dev->absfuzz[t];
365 abs.flat = dev->absflat[t];
367 if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
368 return -EFAULT;
370 return 0;
373 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
375 int t = _IOC_NR(cmd) & ABS_MAX;
377 if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
378 return -EFAULT;
380 dev->abs[t] = abs.value;
381 dev->absmin[t] = abs.minimum;
382 dev->absmax[t] = abs.maximum;
383 dev->absfuzz[t] = abs.fuzz;
384 dev->absflat[t] = abs.flat;
386 return 0;
389 return -EINVAL;
392 static struct file_operations evdev_fops = {
393 .owner = THIS_MODULE,
394 .read = evdev_read,
395 .write = evdev_write,
396 .poll = evdev_poll,
397 .open = evdev_open,
398 .release = evdev_release,
399 .ioctl = evdev_ioctl,
400 .fasync = evdev_fasync,
401 .flush = evdev_flush
404 static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
406 struct evdev *evdev;
407 int minor;
409 for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
410 if (minor == EVDEV_MINORS) {
411 printk(KERN_ERR "evdev: no more free evdev devices\n");
412 return NULL;
415 if (!(evdev = kmalloc(sizeof(struct evdev), GFP_KERNEL)))
416 return NULL;
417 memset(evdev, 0, sizeof(struct evdev));
419 INIT_LIST_HEAD(&evdev->list);
420 init_waitqueue_head(&evdev->wait);
422 evdev->exist = 1;
423 evdev->minor = minor;
424 evdev->handle.dev = dev;
425 evdev->handle.name = evdev->name;
426 evdev->handle.handler = handler;
427 evdev->handle.private = evdev;
428 sprintf(evdev->name, "event%d", minor);
430 evdev_table[minor] = evdev;
432 devfs_mk_cdev(MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
433 S_IFCHR|S_IRUGO|S_IWUSR, "input/event%d", minor);
434 class_simple_device_add(input_class,
435 MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
436 dev->dev, "event%d", minor);
438 return &evdev->handle;
441 static void evdev_disconnect(struct input_handle *handle)
443 struct evdev *evdev = handle->private;
444 struct evdev_list *list;
446 class_simple_device_remove(MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor));
447 devfs_remove("input/event%d", evdev->minor);
448 evdev->exist = 0;
450 if (evdev->open) {
451 input_close_device(handle);
452 wake_up_interruptible(&evdev->wait);
453 list_for_each_entry(list, &evdev->list, node)
454 kill_fasync(&list->fasync, SIGIO, POLL_HUP);
455 } else
456 evdev_free(evdev);
459 static struct input_device_id evdev_ids[] = {
460 { .driver_info = 1 }, /* Matches all devices */
461 { }, /* Terminating zero entry */
464 MODULE_DEVICE_TABLE(input, evdev_ids);
466 static struct input_handler evdev_handler = {
467 .event = evdev_event,
468 .connect = evdev_connect,
469 .disconnect = evdev_disconnect,
470 .fops = &evdev_fops,
471 .minor = EVDEV_MINOR_BASE,
472 .name = "evdev",
473 .id_table = evdev_ids,
476 static int __init evdev_init(void)
478 input_register_handler(&evdev_handler);
479 return 0;
482 static void __exit evdev_exit(void)
484 input_unregister_handler(&evdev_handler);
487 module_init(evdev_init);
488 module_exit(evdev_exit);
490 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
491 MODULE_DESCRIPTION("Input driver event char devices");
492 MODULE_LICENSE("GPL");