blob: ba325f16d07796ede832c45cb81591eb3ee3b788 [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>
Juergen Kreileder52658bb2005-05-29 02:26:43 -050023#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
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
Eric Sesterhennb39787a2006-03-14 00:09:16 -0500133 if (!(list = kzalloc(sizeof(struct evdev_list), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 list->evdev = evdev_table[i];
137 list_add_tail(&list->node, &evdev_table[i]->list);
138 file->private_data = list;
139
140 if (!list->evdev->open++)
141 if (list->evdev->exist)
142 input_open_device(&list->evdev->handle);
143
144 return 0;
145}
146
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500147#ifdef CONFIG_COMPAT
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500148
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500149struct input_event_compat {
150 struct compat_timeval time;
151 __u16 type;
152 __u16 code;
153 __s32 value;
154};
155
Andi Kleenbf2fcc62006-01-11 22:44:06 +0100156/* Note to the author of this code: did it ever occur to
157 you why the ifdefs are needed? Think about it again. -AK */
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500158#ifdef CONFIG_X86_64
Andi Kleenbf2fcc62006-01-11 22:44:06 +0100159# define COMPAT_TEST is_compat_task()
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500160#elif defined(CONFIG_IA64)
Al Viro64505782006-01-12 01:06:06 -0800161# define COMPAT_TEST IS_IA32_PROCESS(task_pt_regs(current))
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800162#elif defined(CONFIG_S390)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500163# define COMPAT_TEST test_thread_flag(TIF_31BIT)
Ralf Baechle59df6bb2005-09-03 15:56:23 -0700164#elif defined(CONFIG_MIPS)
165# define COMPAT_TEST (current->thread.mflags & MF_32BIT_ADDR)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500166#else
167# define COMPAT_TEST test_thread_flag(TIF_32BIT)
168#endif
169
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500170static inline size_t evdev_event_size(void)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500171{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500172 return COMPAT_TEST ?
173 sizeof(struct input_event_compat) : sizeof(struct input_event);
174}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500175
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500176static int evdev_event_from_user(const char __user *buffer, struct input_event *event)
177{
178 if (COMPAT_TEST) {
179 struct input_event_compat compat_event;
180
181 if (copy_from_user(&compat_event, buffer, sizeof(struct input_event_compat)))
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500182 return -EFAULT;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500183
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;
189
190 } else {
191 if (copy_from_user(event, buffer, sizeof(struct input_event)))
192 return -EFAULT;
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500193 }
194
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500195 return 0;
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500196}
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500197
198static int evdev_event_to_user(char __user *buffer, const struct input_event *event)
199{
200 if (COMPAT_TEST) {
201 struct input_event_compat compat_event;
202
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;
208
209 if (copy_to_user(buffer, &compat_event, sizeof(struct input_event_compat)))
210 return -EFAULT;
211
212 } else {
213 if (copy_to_user(buffer, event, sizeof(struct input_event)))
214 return -EFAULT;
215 }
216
217 return 0;
218}
219
220#else
221
222static inline size_t evdev_event_size(void)
223{
224 return sizeof(struct input_event);
225}
226
227static int evdev_event_from_user(const char __user *buffer, struct input_event *event)
228{
229 if (copy_from_user(event, buffer, sizeof(struct input_event)))
230 return -EFAULT;
231
232 return 0;
233}
234
235static int evdev_event_to_user(char __user *buffer, const struct input_event *event)
236{
237 if (copy_to_user(buffer, event, sizeof(struct input_event)))
238 return -EFAULT;
239
240 return 0;
241}
242
243#endif /* CONFIG_COMPAT */
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245static ssize_t evdev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
246{
247 struct evdev_list *list = file->private_data;
248 struct input_event event;
249 int retval = 0;
250
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500251 if (!list->evdev->exist)
252 return -ENODEV;
253
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500254 while (retval < count) {
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500255
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500256 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();
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500260 }
261
262 return retval;
263}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265static ssize_t evdev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
266{
267 struct evdev_list *list = file->private_data;
268 int retval;
269
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500270 if (count < evdev_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return -EINVAL;
272
273 if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
274 return -EAGAIN;
275
276 retval = wait_event_interruptible(list->evdev->wait,
277 list->head != list->tail || (!list->evdev->exist));
278
279 if (retval)
280 return retval;
281
282 if (!list->evdev->exist)
283 return -ENODEV;
284
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500285 while (list->head != list->tail && retval + evdev_event_size() <= count) {
286
287 struct input_event *event = (struct input_event *) list->buffer + list->tail;
288
289 if (evdev_event_to_user(buffer + retval, event))
290 return -EFAULT;
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500293 retval += evdev_event_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295
296 return retval;
297}
298
299/* No kernel lock - fine */
300static unsigned int evdev_poll(struct file *file, poll_table *wait)
301{
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));
306}
307
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500308#ifdef CONFIG_COMPAT
309
310#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
311#define NBITS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
312
313#ifdef __BIG_ENDIAN
314static int bits_to_user(unsigned long *bits, unsigned int maxbit,
315 unsigned int maxlen, void __user *p, int compat)
316{
317 int len, i;
318
319 if (compat) {
320 len = NBITS_COMPAT(maxbit) * sizeof(compat_long_t);
321 if (len < maxlen)
322 len = maxlen;
323
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;
334
335 if (copy_to_user(p, bits, len))
336 return -EFAULT;
337 }
338
339 return len;
340}
341#else
342static int bits_to_user(unsigned long *bits, unsigned int maxbit,
343 unsigned int maxlen, void __user *p, int compat)
344{
345 int len = compat ?
346 NBITS_COMPAT(maxbit) * sizeof(compat_long_t) :
347 NBITS(maxbit) * sizeof(long);
348
349 if (len > maxlen)
350 len = maxlen;
351
352 return copy_to_user(p, bits, len) ? -EFAULT : len;
353}
354#endif /* __BIG_ENDIAN */
355
356#else
357
358static int bits_to_user(unsigned long *bits, unsigned int maxbit,
359 unsigned int maxlen, void __user *p, int compat)
360{
361 int len = NBITS(maxbit) * sizeof(long);
362
363 if (len > maxlen)
364 len = maxlen;
365
366 return copy_to_user(p, bits, len) ? -EFAULT : len;
367}
368
369#endif /* CONFIG_COMPAT */
370
371static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
372{
373 int len;
374
375 if (!str)
376 return -ENOENT;
377
378 len = strlen(str) + 1;
379 if (len > maxlen)
380 len = maxlen;
381
382 return copy_to_user(p, str, len) ? -EFAULT : len;
383}
384
385static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
386 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
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;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500392 int __user *ip = (int __user *)p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 int i, t, u, v;
394
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500395 if (!evdev->exist)
396 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 switch (cmd) {
399
400 case EVIOCGVERSION:
401 return put_user(EV_VERSION, ip);
402
403 case EVIOCGID:
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500404 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
405 return -EFAULT;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400406 return 0;
407
408 case EVIOCGREP:
409 if (!test_bit(EV_REP, dev->evbit))
410 return -ENOSYS;
411 if (put_user(dev->rep[REP_DELAY], ip))
412 return -EFAULT;
413 if (put_user(dev->rep[REP_PERIOD], ip + 1))
414 return -EFAULT;
415 return 0;
416
417 case EVIOCSREP:
418 if (!test_bit(EV_REP, dev->evbit))
419 return -ENOSYS;
420 if (get_user(u, ip))
421 return -EFAULT;
422 if (get_user(v, ip + 1))
423 return -EFAULT;
424
425 input_event(dev, EV_REP, REP_DELAY, u);
426 input_event(dev, EV_REP, REP_PERIOD, v);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500427
428 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 case EVIOCGKEYCODE:
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500431 if (get_user(t, ip))
432 return -EFAULT;
433 if (t < 0 || t >= dev->keycodemax || !dev->keycodesize)
434 return -EINVAL;
435 if (put_user(INPUT_KEYCODE(dev, t), ip + 1))
436 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return 0;
438
439 case EVIOCSKEYCODE:
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500440 if (get_user(t, ip))
441 return -EFAULT;
442 if (t < 0 || t >= dev->keycodemax || !dev->keycodesize)
443 return -EINVAL;
444 if (get_user(v, ip + 1))
445 return -EFAULT;
446 if (v < 0 || v > KEY_MAX)
447 return -EINVAL;
448 if (dev->keycodesize < sizeof(v) && (v >> (dev->keycodesize * 8)))
449 return -EINVAL;
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 u = SET_INPUT_KEYCODE(dev, t, v);
452 clear_bit(u, dev->keybit);
453 set_bit(v, dev->keybit);
454 for (i = 0; i < dev->keycodemax; i++)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500455 if (INPUT_KEYCODE(dev, i) == u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 set_bit(u, dev->keybit);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return 0;
459
460 case EVIOCSFF:
461 if (dev->upload_effect) {
462 struct ff_effect effect;
463 int err;
464
465 if (copy_from_user(&effect, p, sizeof(effect)))
466 return -EFAULT;
467 err = dev->upload_effect(dev, &effect);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500468 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return -EFAULT;
470 return err;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500471 } else
472 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 case EVIOCRMFF:
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500475 if (!dev->erase_effect)
476 return -ENOSYS;
477
478 return dev->erase_effect(dev, (int)(unsigned long) p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 case EVIOCGEFFECTS:
481 if (put_user(dev->ff_effects_max, ip))
482 return -EFAULT;
483 return 0;
484
485 case EVIOCGRAB:
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500486 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (evdev->grab)
488 return -EBUSY;
489 if (input_grab_device(&evdev->handle))
490 return -EBUSY;
491 evdev->grab = list;
492 return 0;
493 } else {
494 if (evdev->grab != list)
495 return -EINVAL;
496 input_release_device(&evdev->handle);
497 evdev->grab = NULL;
498 return 0;
499 }
500
501 default:
502
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500503 if (_IOC_TYPE(cmd) != 'E')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return -EINVAL;
505
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500506 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500508 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500510 long *bits;
511 int len;
512
513 switch (_IOC_NR(cmd) & EV_MAX) {
514 case 0: bits = dev->evbit; len = EV_MAX; break;
515 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
516 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
517 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
518 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
519 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
520 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
521 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
Richard Purdie31581062005-09-06 15:19:06 -0700522 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500523 default: return -EINVAL;
524 }
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500525 return bits_to_user(bits, len, _IOC_SIZE(cmd), p, compat_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500527
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500528 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0)))
529 return bits_to_user(dev->key, KEY_MAX, _IOC_SIZE(cmd),
530 p, compat_mode);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500531
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500532 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0)))
533 return bits_to_user(dev->led, LED_MAX, _IOC_SIZE(cmd),
534 p, compat_mode);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500535
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500536 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0)))
537 return bits_to_user(dev->snd, SND_MAX, _IOC_SIZE(cmd),
538 p, compat_mode);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500539
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500540 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSW(0)))
541 return bits_to_user(dev->sw, SW_MAX, _IOC_SIZE(cmd),
542 p, compat_mode);
Richard Purdie31581062005-09-06 15:19:06 -0700543
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500544 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0)))
545 return str_to_user(dev->name, _IOC_SIZE(cmd), p);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500546
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500547 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0)))
548 return str_to_user(dev->phys, _IOC_SIZE(cmd), p);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500549
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500550 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0)))
551 return str_to_user(dev->uniq, _IOC_SIZE(cmd), p);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500552
553 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
554
555 int t = _IOC_NR(cmd) & ABS_MAX;
556
557 abs.value = dev->abs[t];
558 abs.minimum = dev->absmin[t];
559 abs.maximum = dev->absmax[t];
560 abs.fuzz = dev->absfuzz[t];
561 abs.flat = dev->absflat[t];
562
563 if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
564 return -EFAULT;
565
566 return 0;
567 }
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500571 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500573 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500575 int t = _IOC_NR(cmd) & ABS_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500577 if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
578 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500580 dev->abs[t] = abs.value;
581 dev->absmin[t] = abs.minimum;
582 dev->absmax[t] = abs.maximum;
583 dev->absfuzz[t] = abs.fuzz;
584 dev->absflat[t] = abs.flat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500586 return 0;
587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589 }
590 return -EINVAL;
591}
592
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500593static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
594{
595 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
596}
597
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500598#ifdef CONFIG_COMPAT
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500599static long evdev_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
600{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500601 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500602}
603#endif
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605static struct file_operations evdev_fops = {
606 .owner = THIS_MODULE,
607 .read = evdev_read,
608 .write = evdev_write,
609 .poll = evdev_poll,
610 .open = evdev_open,
611 .release = evdev_release,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500612 .unlocked_ioctl = evdev_ioctl,
613#ifdef CONFIG_COMPAT
614 .compat_ioctl = evdev_ioctl_compat,
615#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 .fasync = evdev_fasync,
617 .flush = evdev_flush
618};
619
620static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
621{
622 struct evdev *evdev;
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700623 struct class_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 int minor;
625
626 for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
627 if (minor == EVDEV_MINORS) {
628 printk(KERN_ERR "evdev: no more free evdev devices\n");
629 return NULL;
630 }
631
Eric Sesterhennb39787a2006-03-14 00:09:16 -0500632 if (!(evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 INIT_LIST_HEAD(&evdev->list);
636 init_waitqueue_head(&evdev->wait);
637
638 evdev->exist = 1;
639 evdev->minor = minor;
640 evdev->handle.dev = dev;
641 evdev->handle.name = evdev->name;
642 evdev->handle.handler = handler;
643 evdev->handle.private = evdev;
644 sprintf(evdev->name, "event%d", minor);
645
646 evdev_table[minor] = evdev;
647
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700648 cdev = class_device_create(&input_class, &dev->cdev,
gregkh@suse.de12356862005-03-15 14:26:30 -0800649 MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700650 dev->cdev.dev, evdev->name);
651
652 /* temporary symlink to keep userspace happy */
653 sysfs_create_link(&input_class.subsys.kset.kobj, &cdev->kobj,
654 evdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 return &evdev->handle;
657}
658
659static void evdev_disconnect(struct input_handle *handle)
660{
661 struct evdev *evdev = handle->private;
662 struct evdev_list *list;
663
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700664 sysfs_remove_link(&input_class.subsys.kset.kobj, evdev->name);
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700665 class_device_destroy(&input_class,
gregkh@suse.de12356862005-03-15 14:26:30 -0800666 MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 evdev->exist = 0;
668
669 if (evdev->open) {
670 input_close_device(handle);
671 wake_up_interruptible(&evdev->wait);
672 list_for_each_entry(list, &evdev->list, node)
673 kill_fasync(&list->fasync, SIGIO, POLL_HUP);
674 } else
675 evdev_free(evdev);
676}
677
678static struct input_device_id evdev_ids[] = {
679 { .driver_info = 1 }, /* Matches all devices */
680 { }, /* Terminating zero entry */
681};
682
683MODULE_DEVICE_TABLE(input, evdev_ids);
684
685static struct input_handler evdev_handler = {
686 .event = evdev_event,
687 .connect = evdev_connect,
688 .disconnect = evdev_disconnect,
689 .fops = &evdev_fops,
690 .minor = EVDEV_MINOR_BASE,
691 .name = "evdev",
692 .id_table = evdev_ids,
693};
694
695static int __init evdev_init(void)
696{
697 input_register_handler(&evdev_handler);
698 return 0;
699}
700
701static void __exit evdev_exit(void)
702{
703 input_unregister_handler(&evdev_handler);
704}
705
706module_init(evdev_init);
707module_exit(evdev_exit);
708
709MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
710MODULE_DESCRIPTION("Input driver event char devices");
711MODULE_LICENSE("GPL");