blob: 19c14c4beb44e2537b2207435e5a625efb9a5f2f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Juergen Kreileder52658bb2005-05-29 02:26:43 -050024#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26struct evdev {
27 int exist;
28 int open;
29 int minor;
30 char name[16];
31 struct input_handle handle;
32 wait_queue_head_t wait;
33 struct evdev_list *grab;
34 struct list_head list;
35};
36
37struct evdev_list {
38 struct input_event buffer[EVDEV_BUFFER_SIZE];
39 int head;
40 int tail;
41 struct fasync_struct *fasync;
42 struct evdev *evdev;
43 struct list_head node;
44};
45
46static struct evdev *evdev_table[EVDEV_MINORS];
47
48static void evdev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
49{
50 struct evdev *evdev = handle->private;
51 struct evdev_list *list;
52
53 if (evdev->grab) {
54 list = evdev->grab;
55
56 do_gettimeofday(&list->buffer[list->head].time);
57 list->buffer[list->head].type = type;
58 list->buffer[list->head].code = code;
59 list->buffer[list->head].value = value;
60 list->head = (list->head + 1) & (EVDEV_BUFFER_SIZE - 1);
61
62 kill_fasync(&list->fasync, SIGIO, POLL_IN);
63 } else
64 list_for_each_entry(list, &evdev->list, node) {
65
66 do_gettimeofday(&list->buffer[list->head].time);
67 list->buffer[list->head].type = type;
68 list->buffer[list->head].code = code;
69 list->buffer[list->head].value = value;
70 list->head = (list->head + 1) & (EVDEV_BUFFER_SIZE - 1);
71
72 kill_fasync(&list->fasync, SIGIO, POLL_IN);
73 }
74
75 wake_up_interruptible(&evdev->wait);
76}
77
78static int evdev_fasync(int fd, struct file *file, int on)
79{
80 int retval;
81 struct evdev_list *list = file->private_data;
82 retval = fasync_helper(fd, file, on, &list->fasync);
83 return retval < 0 ? retval : 0;
84}
85
86static int evdev_flush(struct file * file)
87{
88 struct evdev_list *list = file->private_data;
89 if (!list->evdev->exist) return -ENODEV;
90 return input_flush_device(&list->evdev->handle, file);
91}
92
93static void evdev_free(struct evdev *evdev)
94{
95 evdev_table[evdev->minor] = NULL;
96 kfree(evdev);
97}
98
99static int evdev_release(struct inode * inode, struct file * file)
100{
101 struct evdev_list *list = file->private_data;
102
103 if (list->evdev->grab == list) {
104 input_release_device(&list->evdev->handle);
105 list->evdev->grab = NULL;
106 }
107
108 evdev_fasync(-1, file, 0);
109 list_del(&list->node);
110
111 if (!--list->evdev->open) {
112 if (list->evdev->exist)
113 input_close_device(&list->evdev->handle);
114 else
115 evdev_free(list->evdev);
116 }
117
118 kfree(list);
119 return 0;
120}
121
122static int evdev_open(struct inode * inode, struct file * file)
123{
124 struct evdev_list *list;
125 int i = iminor(inode) - EVDEV_MINOR_BASE;
126 int accept_err;
127
128 if (i >= EVDEV_MINORS || !evdev_table[i] || !evdev_table[i]->exist)
129 return -ENODEV;
130
131 if ((accept_err = input_accept_process(&(evdev_table[i]->handle), file)))
132 return accept_err;
133
134 if (!(list = kmalloc(sizeof(struct evdev_list), GFP_KERNEL)))
135 return -ENOMEM;
136 memset(list, 0, sizeof(struct evdev_list));
137
138 list->evdev = evdev_table[i];
139 list_add_tail(&list->node, &evdev_table[i]->list);
140 file->private_data = list;
141
142 if (!list->evdev->open++)
143 if (list->evdev->exist)
144 input_open_device(&list->evdev->handle);
145
146 return 0;
147}
148
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500149#ifdef CONFIG_COMPAT
150struct input_event_compat {
151 struct compat_timeval time;
152 __u16 type;
153 __u16 code;
154 __s32 value;
155};
156
157#ifdef CONFIG_X86_64
158# define COMPAT_TEST test_thread_flag(TIF_IA32)
159#elif defined(CONFIG_IA64)
160# define COMPAT_TEST IS_IA32_PROCESS(ia64_task_regs(current))
161#elif defined(CONFIG_ARCH_S390)
162# define COMPAT_TEST test_thread_flag(TIF_31BIT)
Ralf Baechle59df6bb2005-09-03 15:56:23 -0700163#elif defined(CONFIG_MIPS)
164# define COMPAT_TEST (current->thread.mflags & MF_32BIT_ADDR)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500165#else
166# define COMPAT_TEST test_thread_flag(TIF_32BIT)
167#endif
168
169static ssize_t evdev_write_compat(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
170{
171 struct evdev_list *list = file->private_data;
172 struct input_event_compat event;
173 int retval = 0;
174
175 while (retval < count) {
176 if (copy_from_user(&event, buffer + retval, sizeof(struct input_event_compat)))
177 return -EFAULT;
178 input_event(list->evdev->handle.dev, event.type, event.code, event.value);
179 retval += sizeof(struct input_event_compat);
180 }
181
182 return retval;
183}
184#endif
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static ssize_t evdev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
187{
188 struct evdev_list *list = file->private_data;
189 struct input_event event;
190 int retval = 0;
191
192 if (!list->evdev->exist) return -ENODEV;
193
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500194#ifdef CONFIG_COMPAT
195 if (COMPAT_TEST)
196 return evdev_write_compat(file, buffer, count, ppos);
197#endif
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 while (retval < count) {
200
201 if (copy_from_user(&event, buffer + retval, sizeof(struct input_event)))
202 return -EFAULT;
203 input_event(list->evdev->handle.dev, event.type, event.code, event.value);
204 retval += sizeof(struct input_event);
205 }
206
207 return retval;
208}
209
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500210#ifdef CONFIG_COMPAT
211static ssize_t evdev_read_compat(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
212{
213 struct evdev_list *list = file->private_data;
214 int retval;
215
216 if (count < sizeof(struct input_event_compat))
217 return -EINVAL;
218
219 if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
220 return -EAGAIN;
221
222 retval = wait_event_interruptible(list->evdev->wait,
223 list->head != list->tail || (!list->evdev->exist));
224
225 if (retval)
226 return retval;
227
228 if (!list->evdev->exist)
229 return -ENODEV;
230
231 while (list->head != list->tail && retval + sizeof(struct input_event_compat) <= count) {
232 struct input_event *event = (struct input_event *) list->buffer + list->tail;
233 struct input_event_compat event_compat;
234 event_compat.time.tv_sec = event->time.tv_sec;
235 event_compat.time.tv_usec = event->time.tv_usec;
236 event_compat.type = event->type;
237 event_compat.code = event->code;
238 event_compat.value = event->value;
239
240 if (copy_to_user(buffer + retval, &event_compat,
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500241 sizeof(struct input_event_compat))) return -EFAULT;
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500242 list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
243 retval += sizeof(struct input_event_compat);
244 }
245
246 return retval;
247}
248#endif
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250static ssize_t evdev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
251{
252 struct evdev_list *list = file->private_data;
253 int retval;
254
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500255#ifdef CONFIG_COMPAT
256 if (COMPAT_TEST)
257 return evdev_read_compat(file, buffer, count, ppos);
258#endif
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (count < sizeof(struct input_event))
261 return -EINVAL;
262
263 if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
264 return -EAGAIN;
265
266 retval = wait_event_interruptible(list->evdev->wait,
267 list->head != list->tail || (!list->evdev->exist));
268
269 if (retval)
270 return retval;
271
272 if (!list->evdev->exist)
273 return -ENODEV;
274
275 while (list->head != list->tail && retval + sizeof(struct input_event) <= count) {
276 if (copy_to_user(buffer + retval, list->buffer + list->tail,
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500277 sizeof(struct input_event))) return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
279 retval += sizeof(struct input_event);
280 }
281
282 return retval;
283}
284
285/* No kernel lock - fine */
286static unsigned int evdev_poll(struct file *file, poll_table *wait)
287{
288 struct evdev_list *list = file->private_data;
289 poll_wait(file, &list->evdev->wait, wait);
290 return ((list->head == list->tail) ? 0 : (POLLIN | POLLRDNORM)) |
291 (list->evdev->exist ? 0 : (POLLHUP | POLLERR));
292}
293
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500294static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 struct evdev_list *list = file->private_data;
297 struct evdev *evdev = list->evdev;
298 struct input_dev *dev = evdev->handle.dev;
299 struct input_absinfo abs;
300 void __user *p = (void __user *)arg;
301 int __user *ip = (int __user *)arg;
302 int i, t, u, v;
303
304 if (!evdev->exist) return -ENODEV;
305
306 switch (cmd) {
307
308 case EVIOCGVERSION:
309 return put_user(EV_VERSION, ip);
310
311 case EVIOCGID:
312 return copy_to_user(p, &dev->id, sizeof(struct input_id)) ? -EFAULT : 0;
313
314 case EVIOCGKEYCODE:
315 if (get_user(t, ip)) return -EFAULT;
316 if (t < 0 || t >= dev->keycodemax || !dev->keycodesize) return -EINVAL;
317 if (put_user(INPUT_KEYCODE(dev, t), ip + 1)) return -EFAULT;
318 return 0;
319
320 case EVIOCSKEYCODE:
321 if (get_user(t, ip)) return -EFAULT;
322 if (t < 0 || t >= dev->keycodemax || !dev->keycodesize) return -EINVAL;
323 if (get_user(v, ip + 1)) return -EFAULT;
324 if (v < 0 || v > KEY_MAX) return -EINVAL;
Vojtech Pavlik5ac7ba32005-07-24 00:50:03 -0500325 if (v >> (dev->keycodesize * 8)) return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 u = SET_INPUT_KEYCODE(dev, t, v);
327 clear_bit(u, dev->keybit);
328 set_bit(v, dev->keybit);
329 for (i = 0; i < dev->keycodemax; i++)
330 if (INPUT_KEYCODE(dev,i) == u)
331 set_bit(u, dev->keybit);
332 return 0;
333
334 case EVIOCSFF:
335 if (dev->upload_effect) {
336 struct ff_effect effect;
337 int err;
338
339 if (copy_from_user(&effect, p, sizeof(effect)))
340 return -EFAULT;
341 err = dev->upload_effect(dev, &effect);
342 if (put_user(effect.id, &(((struct ff_effect __user *)arg)->id)))
343 return -EFAULT;
344 return err;
345 }
346 else return -ENOSYS;
347
348 case EVIOCRMFF:
349 if (dev->erase_effect) {
350 return dev->erase_effect(dev, (int)arg);
351 }
352 else return -ENOSYS;
353
354 case EVIOCGEFFECTS:
355 if (put_user(dev->ff_effects_max, ip))
356 return -EFAULT;
357 return 0;
358
359 case EVIOCGRAB:
360 if (arg) {
361 if (evdev->grab)
362 return -EBUSY;
363 if (input_grab_device(&evdev->handle))
364 return -EBUSY;
365 evdev->grab = list;
366 return 0;
367 } else {
368 if (evdev->grab != list)
369 return -EINVAL;
370 input_release_device(&evdev->handle);
371 evdev->grab = NULL;
372 return 0;
373 }
374
375 default:
376
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500377 if (_IOC_TYPE(cmd) != 'E')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return -EINVAL;
379
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500380 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500382 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500384 long *bits;
385 int len;
386
387 switch (_IOC_NR(cmd) & EV_MAX) {
388 case 0: bits = dev->evbit; len = EV_MAX; break;
389 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
390 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
391 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
392 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
393 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
394 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
395 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
Richard Purdie31581062005-09-06 15:19:06 -0700396 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500397 default: return -EINVAL;
398 }
399 len = NBITS(len) * sizeof(long);
400 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
401 return copy_to_user(p, bits, len) ? -EFAULT : len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500403
404 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0))) {
405 int len;
406 len = NBITS(KEY_MAX) * sizeof(long);
407 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
408 return copy_to_user(p, dev->key, len) ? -EFAULT : len;
409 }
410
411 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0))) {
412 int len;
413 len = NBITS(LED_MAX) * sizeof(long);
414 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
415 return copy_to_user(p, dev->led, len) ? -EFAULT : len;
416 }
417
418 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0))) {
419 int len;
420 len = NBITS(SND_MAX) * sizeof(long);
421 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
422 return copy_to_user(p, dev->snd, len) ? -EFAULT : len;
423 }
424
Richard Purdie31581062005-09-06 15:19:06 -0700425 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSW(0))) {
426 int len;
427 len = NBITS(SW_MAX) * sizeof(long);
428 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
429 return copy_to_user(p, dev->sw, len) ? -EFAULT : len;
430 }
431
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500432 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) {
433 int len;
434 if (!dev->name) return -ENOENT;
435 len = strlen(dev->name) + 1;
436 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
437 return copy_to_user(p, dev->name, len) ? -EFAULT : len;
438 }
439
440 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) {
441 int len;
442 if (!dev->phys) return -ENOENT;
443 len = strlen(dev->phys) + 1;
444 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
445 return copy_to_user(p, dev->phys, len) ? -EFAULT : len;
446 }
447
448 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) {
449 int len;
450 if (!dev->uniq) return -ENOENT;
451 len = strlen(dev->uniq) + 1;
452 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
453 return copy_to_user(p, dev->uniq, len) ? -EFAULT : len;
454 }
455
456 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
457
458 int t = _IOC_NR(cmd) & ABS_MAX;
459
460 abs.value = dev->abs[t];
461 abs.minimum = dev->absmin[t];
462 abs.maximum = dev->absmax[t];
463 abs.fuzz = dev->absfuzz[t];
464 abs.flat = dev->absflat[t];
465
466 if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
467 return -EFAULT;
468
469 return 0;
470 }
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500474 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500476 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500478 int t = _IOC_NR(cmd) & ABS_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500480 if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
481 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500483 dev->abs[t] = abs.value;
484 dev->absmin[t] = abs.minimum;
485 dev->absmax[t] = abs.maximum;
486 dev->absfuzz[t] = abs.fuzz;
487 dev->absflat[t] = abs.flat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500489 return 0;
490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492 }
493 return -EINVAL;
494}
495
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500496#ifdef CONFIG_COMPAT
497
498#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
499#define NBITS_COMPAT(x) ((((x)-1)/BITS_PER_LONG_COMPAT)+1)
500#define OFF_COMPAT(x) ((x)%BITS_PER_LONG_COMPAT)
501#define BIT_COMPAT(x) (1UL<<OFF_COMPAT(x))
502#define LONG_COMPAT(x) ((x)/BITS_PER_LONG_COMPAT)
503#define test_bit_compat(bit, array) ((array[LONG_COMPAT(bit)] >> OFF_COMPAT(bit)) & 1)
504
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500505#ifdef __BIG_ENDIAN
506#define bit_to_user(bit, max) \
507do { \
508 int i; \
509 int len = NBITS_COMPAT((max)) * sizeof(compat_long_t); \
510 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd); \
511 for (i = 0; i < len / sizeof(compat_long_t); i++) \
512 if (copy_to_user((compat_long_t*) p + i, \
513 (compat_long_t*) (bit) + i + 1 - ((i % 2) << 1), \
514 sizeof(compat_long_t))) \
515 return -EFAULT; \
516 return len; \
517} while (0)
518#else
519#define bit_to_user(bit, max) \
520do { \
521 int len = NBITS_COMPAT((max)) * sizeof(compat_long_t); \
522 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd); \
523 return copy_to_user(p, (bit), len) ? -EFAULT : len; \
524} while (0)
525#endif
526
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500527static long evdev_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
528{
529 struct evdev_list *list = file->private_data;
530 struct evdev *evdev = list->evdev;
531 struct input_dev *dev = evdev->handle.dev;
532 struct input_absinfo abs;
533 void __user *p = compat_ptr(arg);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500534
535 if (!evdev->exist) return -ENODEV;
536
537 switch (cmd) {
538
539 case EVIOCGVERSION:
540 case EVIOCGID:
541 case EVIOCGKEYCODE:
542 case EVIOCSKEYCODE:
543 case EVIOCSFF:
544 case EVIOCRMFF:
545 case EVIOCGEFFECTS:
546 case EVIOCGRAB:
547 return evdev_ioctl(file, cmd, (unsigned long) p);
548
549 default:
550
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500551 if (_IOC_TYPE(cmd) != 'E')
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500552 return -EINVAL;
553
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500554 if (_IOC_DIR(cmd) == _IOC_READ) {
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500555
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500556 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
557 long *bits;
558 int max;
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500559
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500560 switch (_IOC_NR(cmd) & EV_MAX) {
561 case 0: bits = dev->evbit; max = EV_MAX; break;
562 case EV_KEY: bits = dev->keybit; max = KEY_MAX; break;
563 case EV_REL: bits = dev->relbit; max = REL_MAX; break;
564 case EV_ABS: bits = dev->absbit; max = ABS_MAX; break;
565 case EV_MSC: bits = dev->mscbit; max = MSC_MAX; break;
566 case EV_LED: bits = dev->ledbit; max = LED_MAX; break;
567 case EV_SND: bits = dev->sndbit; max = SND_MAX; break;
568 case EV_FF: bits = dev->ffbit; max = FF_MAX; break;
569 default: return -EINVAL;
570 }
571 bit_to_user(bits, max);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500572 }
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500573
574 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0)))
575 bit_to_user(dev->key, KEY_MAX);
576
577 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0)))
578 bit_to_user(dev->led, LED_MAX);
579
580 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0)))
581 bit_to_user(dev->snd, SND_MAX);
582
583 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) {
584 int len;
585 if (!dev->name) return -ENOENT;
586 len = strlen(dev->name) + 1;
587 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
588 return copy_to_user(p, dev->name, len) ? -EFAULT : len;
589 }
590
591 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) {
592 int len;
593 if (!dev->phys) return -ENOENT;
594 len = strlen(dev->phys) + 1;
595 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
596 return copy_to_user(p, dev->phys, len) ? -EFAULT : len;
597 }
598
599 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) {
600 int len;
601 if (!dev->uniq) return -ENOENT;
602 len = strlen(dev->uniq) + 1;
603 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
604 return copy_to_user(p, dev->uniq, len) ? -EFAULT : len;
605 }
606
607 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
608
609 int t = _IOC_NR(cmd) & ABS_MAX;
610
611 abs.value = dev->abs[t];
612 abs.minimum = dev->absmin[t];
613 abs.maximum = dev->absmax[t];
614 abs.fuzz = dev->absfuzz[t];
615 abs.flat = dev->absflat[t];
616
617 if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500618 return -EFAULT;
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500619
620 return 0;
621 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500622 }
623
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500624 if (_IOC_DIR(cmd) == _IOC_WRITE) {
625
626 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
627
628 int t = _IOC_NR(cmd) & ABS_MAX;
629
630 if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500631 return -EFAULT;
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500632
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500633 dev->abs[t] = abs.value;
634 dev->absmin[t] = abs.minimum;
635 dev->absmax[t] = abs.maximum;
636 dev->absfuzz[t] = abs.fuzz;
637 dev->absflat[t] = abs.flat;
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500638
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500639 return 0;
640 }
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500641 }
642 }
643 return -EINVAL;
644}
645#endif
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647static struct file_operations evdev_fops = {
648 .owner = THIS_MODULE,
649 .read = evdev_read,
650 .write = evdev_write,
651 .poll = evdev_poll,
652 .open = evdev_open,
653 .release = evdev_release,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500654 .unlocked_ioctl = evdev_ioctl,
655#ifdef CONFIG_COMPAT
656 .compat_ioctl = evdev_ioctl_compat,
657#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 .fasync = evdev_fasync,
659 .flush = evdev_flush
660};
661
662static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
663{
664 struct evdev *evdev;
665 int minor;
666
667 for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
668 if (minor == EVDEV_MINORS) {
669 printk(KERN_ERR "evdev: no more free evdev devices\n");
670 return NULL;
671 }
672
673 if (!(evdev = kmalloc(sizeof(struct evdev), GFP_KERNEL)))
674 return NULL;
675 memset(evdev, 0, sizeof(struct evdev));
676
677 INIT_LIST_HEAD(&evdev->list);
678 init_waitqueue_head(&evdev->wait);
679
680 evdev->exist = 1;
681 evdev->minor = minor;
682 evdev->handle.dev = dev;
683 evdev->handle.name = evdev->name;
684 evdev->handle.handler = handler;
685 evdev->handle.private = evdev;
686 sprintf(evdev->name, "event%d", minor);
687
688 evdev_table[minor] = evdev;
689
690 devfs_mk_cdev(MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
691 S_IFCHR|S_IRUGO|S_IWUSR, "input/event%d", minor);
gregkh@suse.de12356862005-03-15 14:26:30 -0800692 class_device_create(input_class,
693 MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
694 dev->dev, "event%d", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 return &evdev->handle;
697}
698
699static void evdev_disconnect(struct input_handle *handle)
700{
701 struct evdev *evdev = handle->private;
702 struct evdev_list *list;
703
gregkh@suse.de12356862005-03-15 14:26:30 -0800704 class_device_destroy(input_class,
705 MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 devfs_remove("input/event%d", evdev->minor);
707 evdev->exist = 0;
708
709 if (evdev->open) {
710 input_close_device(handle);
711 wake_up_interruptible(&evdev->wait);
712 list_for_each_entry(list, &evdev->list, node)
713 kill_fasync(&list->fasync, SIGIO, POLL_HUP);
714 } else
715 evdev_free(evdev);
716}
717
718static struct input_device_id evdev_ids[] = {
719 { .driver_info = 1 }, /* Matches all devices */
720 { }, /* Terminating zero entry */
721};
722
723MODULE_DEVICE_TABLE(input, evdev_ids);
724
725static struct input_handler evdev_handler = {
726 .event = evdev_event,
727 .connect = evdev_connect,
728 .disconnect = evdev_disconnect,
729 .fops = &evdev_fops,
730 .minor = EVDEV_MINOR_BASE,
731 .name = "evdev",
732 .id_table = evdev_ids,
733};
734
735static int __init evdev_init(void)
736{
737 input_register_handler(&evdev_handler);
738 return 0;
739}
740
741static void __exit evdev_exit(void)
742{
743 input_unregister_handler(&evdev_handler);
744}
745
746module_init(evdev_init);
747module_exit(evdev_exit);
748
749MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
750MODULE_DESCRIPTION("Input driver event char devices");
751MODULE_LICENSE("GPL");