Coarsly sort out 32-bit-only, 64-bit-only and ``portable'' MIPS lib/
[linux-2.6/linux-mips.git] / drivers / input / evdev.c
blob5b3a7540118e66814735a0de84da7b509b3ad829
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 devfs_remove("input/event%d", evdev->minor);
95 evdev_table[evdev->minor] = NULL;
96 kfree(evdev);
99 static int evdev_release(struct inode * inode, struct file * file)
101 struct evdev_list *list = file->private_data;
103 if (list->evdev->grab == list) {
104 input_release_device(&list->evdev->handle);
105 list->evdev->grab = NULL;
108 evdev_fasync(-1, file, 0);
109 list_del(&list->node);
111 if (!--list->evdev->open) {
112 if (list->evdev->exist)
113 input_close_device(&list->evdev->handle);
114 else
115 evdev_free(list->evdev);
118 kfree(list);
119 return 0;
122 static int evdev_open(struct inode * inode, struct file * file)
124 struct evdev_list *list;
125 int i = minor(inode->i_rdev) - EVDEV_MINOR_BASE;
126 int accept_err;
128 if (i >= EVDEV_MINORS || !evdev_table[i])
129 return -ENODEV;
131 if ((accept_err = input_accept_process(&(evdev_table[i]->handle), file)))
132 return accept_err;
134 if (!(list = kmalloc(sizeof(struct evdev_list), GFP_KERNEL)))
135 return -ENOMEM;
136 memset(list, 0, sizeof(struct evdev_list));
138 list->evdev = evdev_table[i];
139 list_add_tail(&list->node, &evdev_table[i]->list);
140 file->private_data = list;
142 if (!list->evdev->open++)
143 if (list->evdev->exist)
144 input_open_device(&list->evdev->handle);
146 return 0;
149 static ssize_t evdev_write(struct file * file, const char * buffer, size_t count, loff_t *ppos)
151 struct evdev_list *list = file->private_data;
152 struct input_event event;
153 int retval = 0;
155 if (!list->evdev->exist) return -ENODEV;
157 while (retval < count) {
159 if (copy_from_user(&event, buffer + retval, sizeof(struct input_event)))
160 return -EFAULT;
161 input_event(list->evdev->handle.dev, event.type, event.code, event.value);
162 retval += sizeof(struct input_event);
165 return retval;
168 static ssize_t evdev_read(struct file * file, char * buffer, size_t count, loff_t *ppos)
170 struct evdev_list *list = file->private_data;
171 int retval;
173 if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
174 return -EAGAIN;
176 retval = wait_event_interruptible(list->evdev->wait,
177 list->head != list->tail && list->evdev->exist);
179 if (retval)
180 return retval;
182 if (!list->evdev->exist)
183 return -ENODEV;
185 while (list->head != list->tail && retval + sizeof(struct input_event) <= count) {
186 if (copy_to_user(buffer + retval, list->buffer + list->tail,
187 sizeof(struct input_event))) return -EFAULT;
188 list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
189 retval += sizeof(struct input_event);
192 return retval;
195 /* No kernel lock - fine */
196 static unsigned int evdev_poll(struct file *file, poll_table *wait)
198 struct evdev_list *list = file->private_data;
199 poll_wait(file, &list->evdev->wait, wait);
200 if (list->head != list->tail)
201 return POLLIN | POLLRDNORM;
202 return 0;
205 static int evdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
207 struct evdev_list *list = file->private_data;
208 struct evdev *evdev = list->evdev;
209 struct input_dev *dev = evdev->handle.dev;
210 struct input_absinfo abs;
211 int i, t, u;
213 if (!evdev->exist) return -ENODEV;
215 switch (cmd) {
217 case EVIOCGVERSION:
218 return put_user(EV_VERSION, (int *) arg);
220 case EVIOCGID:
221 return copy_to_user((void *) arg, &dev->id, sizeof(struct input_id));
223 case EVIOCGREP:
224 if (put_user(dev->rep[0], ((int *) arg) + 0)) return -EFAULT;
225 if (put_user(dev->rep[1], ((int *) arg) + 1)) return -EFAULT;
226 return 0;
228 case EVIOCSREP:
229 if (get_user(dev->rep[0], ((int *) arg) + 0)) return -EFAULT;
230 if (get_user(dev->rep[1], ((int *) arg) + 1)) return -EFAULT;
231 return 0;
233 case EVIOCGKEYCODE:
234 if (get_user(t, ((int *) arg) + 0)) return -EFAULT;
235 if (t < 0 || t > dev->keycodemax || !dev->keycodesize) return -EINVAL;
236 if (put_user(INPUT_KEYCODE(dev, t), ((int *) arg) + 1)) return -EFAULT;
237 return 0;
239 case EVIOCSKEYCODE:
240 if (get_user(t, ((int *) arg) + 0)) return -EFAULT;
241 if (t < 0 || t > dev->keycodemax || !dev->keycodesize) return -EINVAL;
242 u = INPUT_KEYCODE(dev, t);
243 if (get_user(INPUT_KEYCODE(dev, t), ((int *) arg) + 1)) return -EFAULT;
245 for (i = 0; i < dev->keycodemax; i++)
246 if(INPUT_KEYCODE(dev, t) == u) break;
247 if (i == dev->keycodemax) clear_bit(u, dev->keybit);
248 set_bit(INPUT_KEYCODE(dev, t), dev->keybit);
250 return 0;
252 case EVIOCSFF:
253 if (dev->upload_effect) {
254 struct ff_effect effect;
255 int err;
257 if (copy_from_user((void*)(&effect), (void*)arg, sizeof(effect)))
258 return -EFAULT;
259 err = dev->upload_effect(dev, &effect);
260 if (put_user(effect.id, &(((struct ff_effect*)arg)->id)))
261 return -EFAULT;
262 return err;
264 else return -ENOSYS;
266 case EVIOCRMFF:
267 if (dev->erase_effect) {
268 return dev->erase_effect(dev, (int)arg);
270 else return -ENOSYS;
272 case EVIOCGEFFECTS:
273 if (put_user(dev->ff_effects_max, (int*) arg))
274 return -EFAULT;
275 return 0;
277 case EVIOCGRAB:
278 if (arg) {
279 if (evdev->grab)
280 return -EBUSY;
281 if (input_grab_device(&evdev->handle))
282 return -EBUSY;
283 evdev->grab = list;
284 return 0;
285 } else {
286 if (evdev->grab != list)
287 return -EINVAL;
288 input_release_device(&evdev->handle);
289 evdev->grab = NULL;
290 return 0;
293 default:
295 if (_IOC_TYPE(cmd) != 'E' || _IOC_DIR(cmd) != _IOC_READ)
296 return -EINVAL;
298 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
300 long *bits;
301 int len;
303 switch (_IOC_NR(cmd) & EV_MAX) {
304 case 0: bits = dev->evbit; len = EV_MAX; break;
305 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
306 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
307 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
308 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
309 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
310 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
311 default: return -EINVAL;
313 len = NBITS(len) * sizeof(long);
314 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
315 return copy_to_user((char *) arg, bits, len) ? -EFAULT : len;
318 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0))) {
319 int len;
320 len = NBITS(KEY_MAX) * sizeof(long);
321 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
322 return copy_to_user((char *) arg, dev->key, len) ? -EFAULT : len;
325 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0))) {
326 int len;
327 len = NBITS(LED_MAX) * sizeof(long);
328 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
329 return copy_to_user((char *) arg, dev->led, len) ? -EFAULT : len;
332 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0))) {
333 int len;
334 len = NBITS(SND_MAX) * sizeof(long);
335 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
336 return copy_to_user((char *) arg, dev->snd, len) ? -EFAULT : len;
339 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) {
340 int len;
341 if (!dev->name) return -ENOENT;
342 len = strlen(dev->name) + 1;
343 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
344 return copy_to_user((char *) arg, dev->name, len) ? -EFAULT : len;
347 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) {
348 int len;
349 if (!dev->phys) return -ENOENT;
350 len = strlen(dev->phys) + 1;
351 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
352 return copy_to_user((char *) arg, dev->phys, len) ? -EFAULT : len;
355 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) {
356 int len;
357 if (!dev->uniq) return -ENOENT;
358 len = strlen(dev->uniq) + 1;
359 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
360 return copy_to_user((char *) arg, dev->uniq, len) ? -EFAULT : len;
363 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
365 int t = _IOC_NR(cmd) & ABS_MAX;
367 abs.value = dev->abs[t];
368 abs.minimum = dev->absmin[t];
369 abs.maximum = dev->absmax[t];
370 abs.fuzz = dev->absfuzz[t];
371 abs.flat = dev->absflat[t];
373 if (copy_to_user((void *) arg, &abs, sizeof(struct input_absinfo)))
374 return -EFAULT;
376 return 0;
379 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
381 int t = _IOC_NR(cmd) & ABS_MAX;
383 if (copy_from_user(&abs, (void *) arg, sizeof(struct input_absinfo)))
384 return -EFAULT;
386 dev->abs[t] = abs.value;
387 dev->absmin[t] = abs.minimum;
388 dev->absmax[t] = abs.maximum;
389 dev->absfuzz[t] = abs.fuzz;
390 dev->absflat[t] = abs.flat;
392 return 0;
395 return -EINVAL;
398 static struct file_operations evdev_fops = {
399 .owner = THIS_MODULE,
400 .read = evdev_read,
401 .write = evdev_write,
402 .poll = evdev_poll,
403 .open = evdev_open,
404 .release = evdev_release,
405 .ioctl = evdev_ioctl,
406 .fasync = evdev_fasync,
407 .flush = evdev_flush
410 static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
412 struct evdev *evdev;
413 int minor;
415 for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
416 if (minor == EVDEV_MINORS) {
417 printk(KERN_ERR "evdev: no more free evdev devices\n");
418 return NULL;
421 if (!(evdev = kmalloc(sizeof(struct evdev), GFP_KERNEL)))
422 return NULL;
423 memset(evdev, 0, sizeof(struct evdev));
425 INIT_LIST_HEAD(&evdev->list);
426 init_waitqueue_head(&evdev->wait);
428 evdev->exist = 1;
429 evdev->minor = minor;
430 evdev->handle.dev = dev;
431 evdev->handle.name = evdev->name;
432 evdev->handle.handler = handler;
433 evdev->handle.private = evdev;
434 sprintf(evdev->name, "event%d", minor);
436 evdev_table[minor] = evdev;
438 devfs_mk_cdev(MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
439 S_IFCHR|S_IRUGO|S_IWUSR, "input/event%d", minor);
441 return &evdev->handle;
444 static void evdev_disconnect(struct input_handle *handle)
446 struct evdev *evdev = handle->private;
448 evdev->exist = 0;
450 if (evdev->open) {
451 input_close_device(handle);
452 wake_up_interruptible(&evdev->wait);
453 } else
454 evdev_free(evdev);
457 static struct input_device_id evdev_ids[] = {
458 { .driver_info = 1 }, /* Matches all devices */
459 { }, /* Terminating zero entry */
462 MODULE_DEVICE_TABLE(input, evdev_ids);
464 static struct input_handler evdev_handler = {
465 .event = evdev_event,
466 .connect = evdev_connect,
467 .disconnect = evdev_disconnect,
468 .fops = &evdev_fops,
469 .minor = EVDEV_MINOR_BASE,
470 .name = "evdev",
471 .id_table = evdev_ids,
474 static int __init evdev_init(void)
476 input_register_handler(&evdev_handler);
477 return 0;
480 static void __exit evdev_exit(void)
482 input_unregister_handler(&evdev_handler);
485 module_init(evdev_init);
486 module_exit(evdev_exit);
488 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
489 MODULE_DESCRIPTION("Input driver event char devices");
490 MODULE_LICENSE("GPL");