blob: 745979f33dc2e2cf3e3a9bbc1909b50897cb6c04 [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
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
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500148#ifdef CONFIG_COMPAT
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500149
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500150struct input_event_compat {
151 struct compat_timeval time;
152 __u16 type;
153 __u16 code;
154 __s32 value;
155};
156
Andi Kleenbf2fcc62006-01-11 22:44:06 +0100157/* Note to the author of this code: did it ever occur to
158 you why the ifdefs are needed? Think about it again. -AK */
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500159#ifdef CONFIG_X86_64
Andi Kleenbf2fcc62006-01-11 22:44:06 +0100160# define COMPAT_TEST is_compat_task()
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500161#elif defined(CONFIG_IA64)
Al Viro64505782006-01-12 01:06:06 -0800162# define COMPAT_TEST IS_IA32_PROCESS(task_pt_regs(current))
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800163#elif defined(CONFIG_S390)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500164# define COMPAT_TEST test_thread_flag(TIF_31BIT)
Ralf Baechle59df6bb2005-09-03 15:56:23 -0700165#elif defined(CONFIG_MIPS)
166# define COMPAT_TEST (current->thread.mflags & MF_32BIT_ADDR)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500167#else
168# define COMPAT_TEST test_thread_flag(TIF_32BIT)
169#endif
170
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500171static inline size_t evdev_event_size(void)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500172{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500173 return COMPAT_TEST ?
174 sizeof(struct input_event_compat) : sizeof(struct input_event);
175}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500176
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500177static int evdev_event_from_user(const char __user *buffer, struct input_event *event)
178{
179 if (COMPAT_TEST) {
180 struct input_event_compat compat_event;
181
182 if (copy_from_user(&compat_event, buffer, sizeof(struct input_event_compat)))
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500183 return -EFAULT;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500184
185 event->time.tv_sec = compat_event.time.tv_sec;
186 event->time.tv_usec = compat_event.time.tv_usec;
187 event->type = compat_event.type;
188 event->code = compat_event.code;
189 event->value = compat_event.value;
190
191 } else {
192 if (copy_from_user(event, buffer, sizeof(struct input_event)))
193 return -EFAULT;
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500194 }
195
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500196 return 0;
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500197}
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500198
199static int evdev_event_to_user(char __user *buffer, const struct input_event *event)
200{
201 if (COMPAT_TEST) {
202 struct input_event_compat compat_event;
203
204 compat_event.time.tv_sec = event->time.tv_sec;
205 compat_event.time.tv_usec = event->time.tv_usec;
206 compat_event.type = event->type;
207 compat_event.code = event->code;
208 compat_event.value = event->value;
209
210 if (copy_to_user(buffer, &compat_event, sizeof(struct input_event_compat)))
211 return -EFAULT;
212
213 } else {
214 if (copy_to_user(buffer, event, sizeof(struct input_event)))
215 return -EFAULT;
216 }
217
218 return 0;
219}
220
221#else
222
223static inline size_t evdev_event_size(void)
224{
225 return sizeof(struct input_event);
226}
227
228static int evdev_event_from_user(const char __user *buffer, struct input_event *event)
229{
230 if (copy_from_user(event, buffer, sizeof(struct input_event)))
231 return -EFAULT;
232
233 return 0;
234}
235
236static int evdev_event_to_user(char __user *buffer, const struct input_event *event)
237{
238 if (copy_to_user(buffer, event, sizeof(struct input_event)))
239 return -EFAULT;
240
241 return 0;
242}
243
244#endif /* CONFIG_COMPAT */
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246static ssize_t evdev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
247{
248 struct evdev_list *list = file->private_data;
249 struct input_event event;
250 int retval = 0;
251
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500252 if (!list->evdev->exist)
253 return -ENODEV;
254
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500255 while (retval < count) {
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500256
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500257 if (evdev_event_from_user(buffer + retval, &event))
258 return -EFAULT;
259 input_event(list->evdev->handle.dev, event.type, event.code, event.value);
260 retval += evdev_event_size();
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500261 }
262
263 return retval;
264}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266static ssize_t evdev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
267{
268 struct evdev_list *list = file->private_data;
269 int retval;
270
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500271 if (count < evdev_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return -EINVAL;
273
274 if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
275 return -EAGAIN;
276
277 retval = wait_event_interruptible(list->evdev->wait,
278 list->head != list->tail || (!list->evdev->exist));
279
280 if (retval)
281 return retval;
282
283 if (!list->evdev->exist)
284 return -ENODEV;
285
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500286 while (list->head != list->tail && retval + evdev_event_size() <= count) {
287
288 struct input_event *event = (struct input_event *) list->buffer + list->tail;
289
290 if (evdev_event_to_user(buffer + retval, event))
291 return -EFAULT;
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500294 retval += evdev_event_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296
297 return retval;
298}
299
300/* No kernel lock - fine */
301static unsigned int evdev_poll(struct file *file, poll_table *wait)
302{
303 struct evdev_list *list = file->private_data;
304 poll_wait(file, &list->evdev->wait, wait);
305 return ((list->head == list->tail) ? 0 : (POLLIN | POLLRDNORM)) |
306 (list->evdev->exist ? 0 : (POLLHUP | POLLERR));
307}
308
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500309#ifdef CONFIG_COMPAT
310
311#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
312#define NBITS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
313
314#ifdef __BIG_ENDIAN
315static int bits_to_user(unsigned long *bits, unsigned int maxbit,
316 unsigned int maxlen, void __user *p, int compat)
317{
318 int len, i;
319
320 if (compat) {
321 len = NBITS_COMPAT(maxbit) * sizeof(compat_long_t);
322 if (len < maxlen)
323 len = maxlen;
324
325 for (i = 0; i < len / sizeof(compat_long_t); i++)
326 if (copy_to_user((compat_long_t __user *) p + i,
327 (compat_long_t *) bits +
328 i + 1 - ((i % 2) << 1),
329 sizeof(compat_long_t)))
330 return -EFAULT;
331 } else {
332 len = NBITS(maxbit) * sizeof(long);
333 if (len > maxlen)
334 len = maxlen;
335
336 if (copy_to_user(p, bits, len))
337 return -EFAULT;
338 }
339
340 return len;
341}
342#else
343static int bits_to_user(unsigned long *bits, unsigned int maxbit,
344 unsigned int maxlen, void __user *p, int compat)
345{
346 int len = compat ?
347 NBITS_COMPAT(maxbit) * sizeof(compat_long_t) :
348 NBITS(maxbit) * sizeof(long);
349
350 if (len > maxlen)
351 len = maxlen;
352
353 return copy_to_user(p, bits, len) ? -EFAULT : len;
354}
355#endif /* __BIG_ENDIAN */
356
357#else
358
359static int bits_to_user(unsigned long *bits, unsigned int maxbit,
360 unsigned int maxlen, void __user *p, int compat)
361{
362 int len = NBITS(maxbit) * sizeof(long);
363
364 if (len > maxlen)
365 len = maxlen;
366
367 return copy_to_user(p, bits, len) ? -EFAULT : len;
368}
369
370#endif /* CONFIG_COMPAT */
371
372static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
373{
374 int len;
375
376 if (!str)
377 return -ENOENT;
378
379 len = strlen(str) + 1;
380 if (len > maxlen)
381 len = maxlen;
382
383 return copy_to_user(p, str, len) ? -EFAULT : len;
384}
385
386static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
387 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 struct evdev_list *list = file->private_data;
390 struct evdev *evdev = list->evdev;
391 struct input_dev *dev = evdev->handle.dev;
392 struct input_absinfo abs;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500393 int __user *ip = (int __user *)p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 int i, t, u, v;
395
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500396 if (!evdev->exist)
397 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 switch (cmd) {
400
401 case EVIOCGVERSION:
402 return put_user(EV_VERSION, ip);
403
404 case EVIOCGID:
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500405 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
406 return -EFAULT;
407
408 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 case EVIOCGKEYCODE:
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500411 if (get_user(t, ip))
412 return -EFAULT;
413 if (t < 0 || t >= dev->keycodemax || !dev->keycodesize)
414 return -EINVAL;
415 if (put_user(INPUT_KEYCODE(dev, t), ip + 1))
416 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return 0;
418
419 case EVIOCSKEYCODE:
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500420 if (get_user(t, ip))
421 return -EFAULT;
422 if (t < 0 || t >= dev->keycodemax || !dev->keycodesize)
423 return -EINVAL;
424 if (get_user(v, ip + 1))
425 return -EFAULT;
426 if (v < 0 || v > KEY_MAX)
427 return -EINVAL;
428 if (dev->keycodesize < sizeof(v) && (v >> (dev->keycodesize * 8)))
429 return -EINVAL;
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 u = SET_INPUT_KEYCODE(dev, t, v);
432 clear_bit(u, dev->keybit);
433 set_bit(v, dev->keybit);
434 for (i = 0; i < dev->keycodemax; i++)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500435 if (INPUT_KEYCODE(dev, i) == u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 set_bit(u, dev->keybit);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return 0;
439
440 case EVIOCSFF:
441 if (dev->upload_effect) {
442 struct ff_effect effect;
443 int err;
444
445 if (copy_from_user(&effect, p, sizeof(effect)))
446 return -EFAULT;
447 err = dev->upload_effect(dev, &effect);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500448 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return -EFAULT;
450 return err;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500451 } else
452 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 case EVIOCRMFF:
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500455 if (!dev->erase_effect)
456 return -ENOSYS;
457
458 return dev->erase_effect(dev, (int)(unsigned long) p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 case EVIOCGEFFECTS:
461 if (put_user(dev->ff_effects_max, ip))
462 return -EFAULT;
463 return 0;
464
465 case EVIOCGRAB:
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500466 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (evdev->grab)
468 return -EBUSY;
469 if (input_grab_device(&evdev->handle))
470 return -EBUSY;
471 evdev->grab = list;
472 return 0;
473 } else {
474 if (evdev->grab != list)
475 return -EINVAL;
476 input_release_device(&evdev->handle);
477 evdev->grab = NULL;
478 return 0;
479 }
480
481 default:
482
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500483 if (_IOC_TYPE(cmd) != 'E')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return -EINVAL;
485
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500486 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500488 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500490 long *bits;
491 int len;
492
493 switch (_IOC_NR(cmd) & EV_MAX) {
494 case 0: bits = dev->evbit; len = EV_MAX; break;
495 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
496 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
497 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
498 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
499 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
500 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
501 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
Richard Purdie31581062005-09-06 15:19:06 -0700502 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500503 default: return -EINVAL;
504 }
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500505 return bits_to_user(bits, len, _IOC_SIZE(cmd), p, compat_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500507
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500508 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0)))
509 return bits_to_user(dev->key, KEY_MAX, _IOC_SIZE(cmd),
510 p, compat_mode);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500511
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500512 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0)))
513 return bits_to_user(dev->led, LED_MAX, _IOC_SIZE(cmd),
514 p, compat_mode);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500515
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500516 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0)))
517 return bits_to_user(dev->snd, SND_MAX, _IOC_SIZE(cmd),
518 p, compat_mode);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500519
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500520 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSW(0)))
521 return bits_to_user(dev->sw, SW_MAX, _IOC_SIZE(cmd),
522 p, compat_mode);
Richard Purdie31581062005-09-06 15:19:06 -0700523
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500524 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0)))
525 return str_to_user(dev->name, _IOC_SIZE(cmd), p);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500526
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500527 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0)))
528 return str_to_user(dev->phys, _IOC_SIZE(cmd), p);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500529
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500530 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0)))
531 return str_to_user(dev->uniq, _IOC_SIZE(cmd), p);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500532
533 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
534
535 int t = _IOC_NR(cmd) & ABS_MAX;
536
537 abs.value = dev->abs[t];
538 abs.minimum = dev->absmin[t];
539 abs.maximum = dev->absmax[t];
540 abs.fuzz = dev->absfuzz[t];
541 abs.flat = dev->absflat[t];
542
543 if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
544 return -EFAULT;
545
546 return 0;
547 }
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500551 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500553 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500555 int t = _IOC_NR(cmd) & ABS_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500557 if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
558 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500560 dev->abs[t] = abs.value;
561 dev->absmin[t] = abs.minimum;
562 dev->absmax[t] = abs.maximum;
563 dev->absfuzz[t] = abs.fuzz;
564 dev->absflat[t] = abs.flat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500566 return 0;
567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569 }
570 return -EINVAL;
571}
572
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500573static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
574{
575 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
576}
577
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500578#ifdef CONFIG_COMPAT
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500579static long evdev_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
580{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500581 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500582}
583#endif
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585static struct file_operations evdev_fops = {
586 .owner = THIS_MODULE,
587 .read = evdev_read,
588 .write = evdev_write,
589 .poll = evdev_poll,
590 .open = evdev_open,
591 .release = evdev_release,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500592 .unlocked_ioctl = evdev_ioctl,
593#ifdef CONFIG_COMPAT
594 .compat_ioctl = evdev_ioctl_compat,
595#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 .fasync = evdev_fasync,
597 .flush = evdev_flush
598};
599
600static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
601{
602 struct evdev *evdev;
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700603 struct class_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 int minor;
605
606 for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
607 if (minor == EVDEV_MINORS) {
608 printk(KERN_ERR "evdev: no more free evdev devices\n");
609 return NULL;
610 }
611
612 if (!(evdev = kmalloc(sizeof(struct evdev), GFP_KERNEL)))
613 return NULL;
614 memset(evdev, 0, sizeof(struct evdev));
615
616 INIT_LIST_HEAD(&evdev->list);
617 init_waitqueue_head(&evdev->wait);
618
619 evdev->exist = 1;
620 evdev->minor = minor;
621 evdev->handle.dev = dev;
622 evdev->handle.name = evdev->name;
623 evdev->handle.handler = handler;
624 evdev->handle.private = evdev;
625 sprintf(evdev->name, "event%d", minor);
626
627 evdev_table[minor] = evdev;
628
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700629 cdev = class_device_create(&input_class, &dev->cdev,
gregkh@suse.de12356862005-03-15 14:26:30 -0800630 MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700631 dev->cdev.dev, evdev->name);
632
633 /* temporary symlink to keep userspace happy */
634 sysfs_create_link(&input_class.subsys.kset.kobj, &cdev->kobj,
635 evdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 return &evdev->handle;
638}
639
640static void evdev_disconnect(struct input_handle *handle)
641{
642 struct evdev *evdev = handle->private;
643 struct evdev_list *list;
644
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700645 sysfs_remove_link(&input_class.subsys.kset.kobj, evdev->name);
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700646 class_device_destroy(&input_class,
gregkh@suse.de12356862005-03-15 14:26:30 -0800647 MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 evdev->exist = 0;
649
650 if (evdev->open) {
651 input_close_device(handle);
652 wake_up_interruptible(&evdev->wait);
653 list_for_each_entry(list, &evdev->list, node)
654 kill_fasync(&list->fasync, SIGIO, POLL_HUP);
655 } else
656 evdev_free(evdev);
657}
658
659static struct input_device_id evdev_ids[] = {
660 { .driver_info = 1 }, /* Matches all devices */
661 { }, /* Terminating zero entry */
662};
663
664MODULE_DEVICE_TABLE(input, evdev_ids);
665
666static struct input_handler evdev_handler = {
667 .event = evdev_event,
668 .connect = evdev_connect,
669 .disconnect = evdev_disconnect,
670 .fops = &evdev_fops,
671 .minor = EVDEV_MINOR_BASE,
672 .name = "evdev",
673 .id_table = evdev_ids,
674};
675
676static int __init evdev_init(void)
677{
678 input_register_handler(&evdev_handler);
679 return 0;
680}
681
682static void __exit evdev_exit(void)
683{
684 input_unregister_handler(&evdev_handler);
685}
686
687module_init(evdev_init);
688module_exit(evdev_exit);
689
690MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
691MODULE_DESCRIPTION("Input driver event char devices");
692MODULE_LICENSE("GPL");