aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/evdev.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/input/evdev.c')
-rw-r--r--drivers/input/evdev.c492
1 files changed, 492 insertions, 0 deletions
diff --git a/drivers/input/evdev.c b/drivers/input/evdev.c
new file mode 100644
index 000000000000..17552a29978b
--- /dev/null
+++ b/drivers/input/evdev.c
@@ -0,0 +1,492 @@
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
25struct 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
36struct 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
45static struct evdev *evdev_table[EVDEV_MINORS];
46
47static 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
77static 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
85static 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
92static void evdev_free(struct evdev *evdev)
93{
94 evdev_table[evdev->minor] = NULL;
95 kfree(evdev);
96}
97
98static int evdev_release(struct inode * inode, struct file * file)
99{
100 struct evdev_list *list = file->private_data;
101
102 if (list->evdev->grab == list) {
103 input_release_device(&list->evdev->handle);
104 list->evdev->grab = NULL;
105 }
106
107 evdev_fasync(-1, file, 0);
108 list_del(&list->node);
109
110 if (!--list->evdev->open) {
111 if (list->evdev->exist)
112 input_close_device(&list->evdev->handle);
113 else
114 evdev_free(list->evdev);
115 }
116
117 kfree(list);
118 return 0;
119}
120
121static int evdev_open(struct inode * inode, struct file * file)
122{
123 struct evdev_list *list;
124 int i = iminor(inode) - EVDEV_MINOR_BASE;
125 int accept_err;
126
127 if (i >= EVDEV_MINORS || !evdev_table[i] || !evdev_table[i]->exist)
128 return -ENODEV;
129
130 if ((accept_err = input_accept_process(&(evdev_table[i]->handle), file)))
131 return accept_err;
132
133 if (!(list = kmalloc(sizeof(struct evdev_list), GFP_KERNEL)))
134 return -ENOMEM;
135 memset(list, 0, sizeof(struct evdev_list));
136
137 list->evdev = evdev_table[i];
138 list_add_tail(&list->node, &evdev_table[i]->list);
139 file->private_data = list;
140
141 if (!list->evdev->open++)
142 if (list->evdev->exist)
143 input_open_device(&list->evdev->handle);
144
145 return 0;
146}
147
148static ssize_t evdev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
149{
150 struct evdev_list *list = file->private_data;
151 struct input_event event;
152 int retval = 0;
153
154 if (!list->evdev->exist) return -ENODEV;
155
156 while (retval < count) {
157
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);
162 }
163
164 return retval;
165}
166
167static ssize_t evdev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
168{
169 struct evdev_list *list = file->private_data;
170 int retval;
171
172 if (count < sizeof(struct input_event))
173 return -EINVAL;
174
175 if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
176 return -EAGAIN;
177
178 retval = wait_event_interruptible(list->evdev->wait,
179 list->head != list->tail || (!list->evdev->exist));
180
181 if (retval)
182 return retval;
183
184 if (!list->evdev->exist)
185 return -ENODEV;
186
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);
192 }
193
194 return retval;
195}
196
197/* No kernel lock - fine */
198static unsigned int evdev_poll(struct file *file, poll_table *wait)
199{
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));
204}
205
206static 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 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;
244
245 case EVIOCSFF:
246 if (dev->upload_effect) {
247 struct ff_effect effect;
248 int err;
249
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;
256 }
257 else return -ENOSYS;
258
259 case EVIOCRMFF:
260 if (dev->erase_effect) {
261 return dev->erase_effect(dev, (int)arg);
262 }
263 else return -ENOSYS;
264
265 case EVIOCGEFFECTS:
266 if (put_user(dev->ff_effects_max, ip))
267 return -EFAULT;
268 return 0;
269
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;
284 }
285
286 default:
287
288 if (_IOC_TYPE(cmd) != 'E' || _IOC_DIR(cmd) != _IOC_READ)
289 return -EINVAL;
290
291 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
292
293 long *bits;
294 int len;
295
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;
306 }
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;
310 }
311
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;
317 }
318
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;
324 }
325
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;
331 }
332
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;
339 }
340
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;
347 }
348
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;
355 }
356
357 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
358
359 int t = _IOC_NR(cmd) & ABS_MAX;
360
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];
366
367 if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
368 return -EFAULT;
369
370 return 0;
371 }
372
373 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
374
375 int t = _IOC_NR(cmd) & ABS_MAX;
376
377 if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
378 return -EFAULT;
379
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;
385
386 return 0;
387 }
388 }
389 return -EINVAL;
390}
391
392static 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
402};
403
404static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
405{
406 struct evdev *evdev;
407 int minor;
408
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;
413 }
414
415 if (!(evdev = kmalloc(sizeof(struct evdev), GFP_KERNEL)))
416 return NULL;
417 memset(evdev, 0, sizeof(struct evdev));
418
419 INIT_LIST_HEAD(&evdev->list);
420 init_waitqueue_head(&evdev->wait);
421
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);
429
430 evdev_table[minor] = evdev;
431
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);
437
438 return &evdev->handle;
439}
440
441static void evdev_disconnect(struct input_handle *handle)
442{
443 struct evdev *evdev = handle->private;
444 struct evdev_list *list;
445
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;
449
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);
457}
458
459static struct input_device_id evdev_ids[] = {
460 { .driver_info = 1 }, /* Matches all devices */
461 { }, /* Terminating zero entry */
462};
463
464MODULE_DEVICE_TABLE(input, evdev_ids);
465
466static 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,
474};
475
476static int __init evdev_init(void)
477{
478 input_register_handler(&evdev_handler);
479 return 0;
480}
481
482static void __exit evdev_exit(void)
483{
484 input_unregister_handler(&evdev_handler);
485}
486
487module_init(evdev_init);
488module_exit(evdev_exit);
489
490MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
491MODULE_DESCRIPTION("Input driver event char devices");
492MODULE_LICENSE("GPL");