blob: a29d5ceb00cf9e5903d4dfb7167b2abc9d8af22c [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;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 retval = fasync_helper(fd, file, on, &list->fasync);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return retval < 0 ? retval : 0;
85}
86
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040087static int evdev_flush(struct file *file, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 struct evdev_list *list = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040090
91 if (!list->evdev->exist)
92 return -ENODEV;
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return input_flush_device(&list->evdev->handle, file);
95}
96
97static void evdev_free(struct evdev *evdev)
98{
99 evdev_table[evdev->minor] = NULL;
100 kfree(evdev);
101}
102
103static int evdev_release(struct inode * inode, struct file * file)
104{
105 struct evdev_list *list = file->private_data;
106
107 if (list->evdev->grab == list) {
108 input_release_device(&list->evdev->handle);
109 list->evdev->grab = NULL;
110 }
111
112 evdev_fasync(-1, file, 0);
113 list_del(&list->node);
114
115 if (!--list->evdev->open) {
116 if (list->evdev->exist)
117 input_close_device(&list->evdev->handle);
118 else
119 evdev_free(list->evdev);
120 }
121
122 kfree(list);
123 return 0;
124}
125
126static int evdev_open(struct inode * inode, struct file * file)
127{
128 struct evdev_list *list;
129 int i = iminor(inode) - EVDEV_MINOR_BASE;
130 int accept_err;
131
132 if (i >= EVDEV_MINORS || !evdev_table[i] || !evdev_table[i]->exist)
133 return -ENODEV;
134
135 if ((accept_err = input_accept_process(&(evdev_table[i]->handle), file)))
136 return accept_err;
137
Eric Sesterhennb39787a2006-03-14 00:09:16 -0500138 if (!(list = kzalloc(sizeof(struct evdev_list), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 list->evdev = evdev_table[i];
142 list_add_tail(&list->node, &evdev_table[i]->list);
143 file->private_data = list;
144
145 if (!list->evdev->open++)
146 if (list->evdev->exist)
147 input_open_device(&list->evdev->handle);
148
149 return 0;
150}
151
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500152#ifdef CONFIG_COMPAT
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500153
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500154struct input_event_compat {
155 struct compat_timeval time;
156 __u16 type;
157 __u16 code;
158 __s32 value;
159};
160
Andi Kleenbf2fcc62006-01-11 22:44:06 +0100161/* Note to the author of this code: did it ever occur to
162 you why the ifdefs are needed? Think about it again. -AK */
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500163#ifdef CONFIG_X86_64
Andi Kleenbf2fcc62006-01-11 22:44:06 +0100164# define COMPAT_TEST is_compat_task()
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500165#elif defined(CONFIG_IA64)
Al Viro64505782006-01-12 01:06:06 -0800166# define COMPAT_TEST IS_IA32_PROCESS(task_pt_regs(current))
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800167#elif defined(CONFIG_S390)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500168# define COMPAT_TEST test_thread_flag(TIF_31BIT)
Ralf Baechle59df6bb2005-09-03 15:56:23 -0700169#elif defined(CONFIG_MIPS)
170# define COMPAT_TEST (current->thread.mflags & MF_32BIT_ADDR)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500171#else
172# define COMPAT_TEST test_thread_flag(TIF_32BIT)
173#endif
174
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500175static inline size_t evdev_event_size(void)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500176{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500177 return COMPAT_TEST ?
178 sizeof(struct input_event_compat) : sizeof(struct input_event);
179}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500180
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500181static int evdev_event_from_user(const char __user *buffer, struct input_event *event)
182{
183 if (COMPAT_TEST) {
184 struct input_event_compat compat_event;
185
186 if (copy_from_user(&compat_event, buffer, sizeof(struct input_event_compat)))
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500187 return -EFAULT;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500188
189 event->time.tv_sec = compat_event.time.tv_sec;
190 event->time.tv_usec = compat_event.time.tv_usec;
191 event->type = compat_event.type;
192 event->code = compat_event.code;
193 event->value = compat_event.value;
194
195 } else {
196 if (copy_from_user(event, buffer, sizeof(struct input_event)))
197 return -EFAULT;
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500198 }
199
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500200 return 0;
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500201}
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500202
203static int evdev_event_to_user(char __user *buffer, const struct input_event *event)
204{
205 if (COMPAT_TEST) {
206 struct input_event_compat compat_event;
207
208 compat_event.time.tv_sec = event->time.tv_sec;
209 compat_event.time.tv_usec = event->time.tv_usec;
210 compat_event.type = event->type;
211 compat_event.code = event->code;
212 compat_event.value = event->value;
213
214 if (copy_to_user(buffer, &compat_event, sizeof(struct input_event_compat)))
215 return -EFAULT;
216
217 } else {
218 if (copy_to_user(buffer, event, sizeof(struct input_event)))
219 return -EFAULT;
220 }
221
222 return 0;
223}
224
225#else
226
227static inline size_t evdev_event_size(void)
228{
229 return sizeof(struct input_event);
230}
231
232static int evdev_event_from_user(const char __user *buffer, struct input_event *event)
233{
234 if (copy_from_user(event, buffer, sizeof(struct input_event)))
235 return -EFAULT;
236
237 return 0;
238}
239
240static int evdev_event_to_user(char __user *buffer, const struct input_event *event)
241{
242 if (copy_to_user(buffer, event, sizeof(struct input_event)))
243 return -EFAULT;
244
245 return 0;
246}
247
248#endif /* CONFIG_COMPAT */
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250static ssize_t evdev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
251{
252 struct evdev_list *list = file->private_data;
253 struct input_event event;
254 int retval = 0;
255
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500256 if (!list->evdev->exist)
257 return -ENODEV;
258
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500259 while (retval < count) {
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500260
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500261 if (evdev_event_from_user(buffer + retval, &event))
262 return -EFAULT;
263 input_event(list->evdev->handle.dev, event.type, event.code, event.value);
264 retval += evdev_event_size();
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500265 }
266
267 return retval;
268}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270static ssize_t evdev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
271{
272 struct evdev_list *list = file->private_data;
273 int retval;
274
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500275 if (count < evdev_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return -EINVAL;
277
278 if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK))
279 return -EAGAIN;
280
281 retval = wait_event_interruptible(list->evdev->wait,
282 list->head != list->tail || (!list->evdev->exist));
283
284 if (retval)
285 return retval;
286
287 if (!list->evdev->exist)
288 return -ENODEV;
289
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500290 while (list->head != list->tail && retval + evdev_event_size() <= count) {
291
292 struct input_event *event = (struct input_event *) list->buffer + list->tail;
293
294 if (evdev_event_to_user(buffer + retval, event))
295 return -EFAULT;
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500298 retval += evdev_event_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
301 return retval;
302}
303
304/* No kernel lock - fine */
305static unsigned int evdev_poll(struct file *file, poll_table *wait)
306{
307 struct evdev_list *list = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 poll_wait(file, &list->evdev->wait, wait);
310 return ((list->head == list->tail) ? 0 : (POLLIN | POLLRDNORM)) |
311 (list->evdev->exist ? 0 : (POLLHUP | POLLERR));
312}
313
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500314#ifdef CONFIG_COMPAT
315
316#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
317#define NBITS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
318
319#ifdef __BIG_ENDIAN
320static int bits_to_user(unsigned long *bits, unsigned int maxbit,
321 unsigned int maxlen, void __user *p, int compat)
322{
323 int len, i;
324
325 if (compat) {
326 len = NBITS_COMPAT(maxbit) * sizeof(compat_long_t);
327 if (len < maxlen)
328 len = maxlen;
329
330 for (i = 0; i < len / sizeof(compat_long_t); i++)
331 if (copy_to_user((compat_long_t __user *) p + i,
332 (compat_long_t *) bits +
333 i + 1 - ((i % 2) << 1),
334 sizeof(compat_long_t)))
335 return -EFAULT;
336 } else {
337 len = NBITS(maxbit) * sizeof(long);
338 if (len > maxlen)
339 len = maxlen;
340
341 if (copy_to_user(p, bits, len))
342 return -EFAULT;
343 }
344
345 return len;
346}
347#else
348static int bits_to_user(unsigned long *bits, unsigned int maxbit,
349 unsigned int maxlen, void __user *p, int compat)
350{
351 int len = compat ?
352 NBITS_COMPAT(maxbit) * sizeof(compat_long_t) :
353 NBITS(maxbit) * sizeof(long);
354
355 if (len > maxlen)
356 len = maxlen;
357
358 return copy_to_user(p, bits, len) ? -EFAULT : len;
359}
360#endif /* __BIG_ENDIAN */
361
362#else
363
364static int bits_to_user(unsigned long *bits, unsigned int maxbit,
365 unsigned int maxlen, void __user *p, int compat)
366{
367 int len = NBITS(maxbit) * sizeof(long);
368
369 if (len > maxlen)
370 len = maxlen;
371
372 return copy_to_user(p, bits, len) ? -EFAULT : len;
373}
374
375#endif /* CONFIG_COMPAT */
376
377static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
378{
379 int len;
380
381 if (!str)
382 return -ENOENT;
383
384 len = strlen(str) + 1;
385 if (len > maxlen)
386 len = maxlen;
387
388 return copy_to_user(p, str, len) ? -EFAULT : len;
389}
390
391static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
392 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 struct evdev_list *list = file->private_data;
395 struct evdev *evdev = list->evdev;
396 struct input_dev *dev = evdev->handle.dev;
397 struct input_absinfo abs;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500398 int __user *ip = (int __user *)p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 int i, t, u, v;
400
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500401 if (!evdev->exist)
402 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 switch (cmd) {
405
406 case EVIOCGVERSION:
407 return put_user(EV_VERSION, ip);
408
409 case EVIOCGID:
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500410 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
411 return -EFAULT;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400412 return 0;
413
414 case EVIOCGREP:
415 if (!test_bit(EV_REP, dev->evbit))
416 return -ENOSYS;
417 if (put_user(dev->rep[REP_DELAY], ip))
418 return -EFAULT;
419 if (put_user(dev->rep[REP_PERIOD], ip + 1))
420 return -EFAULT;
421 return 0;
422
423 case EVIOCSREP:
424 if (!test_bit(EV_REP, dev->evbit))
425 return -ENOSYS;
426 if (get_user(u, ip))
427 return -EFAULT;
428 if (get_user(v, ip + 1))
429 return -EFAULT;
430
431 input_event(dev, EV_REP, REP_DELAY, u);
432 input_event(dev, EV_REP, REP_PERIOD, v);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500433
434 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 case EVIOCGKEYCODE:
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500437 if (get_user(t, ip))
438 return -EFAULT;
439 if (t < 0 || t >= dev->keycodemax || !dev->keycodesize)
440 return -EINVAL;
441 if (put_user(INPUT_KEYCODE(dev, t), ip + 1))
442 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return 0;
444
445 case EVIOCSKEYCODE:
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500446 if (get_user(t, ip))
447 return -EFAULT;
448 if (t < 0 || t >= dev->keycodemax || !dev->keycodesize)
449 return -EINVAL;
450 if (get_user(v, ip + 1))
451 return -EFAULT;
452 if (v < 0 || v > KEY_MAX)
453 return -EINVAL;
454 if (dev->keycodesize < sizeof(v) && (v >> (dev->keycodesize * 8)))
455 return -EINVAL;
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 u = SET_INPUT_KEYCODE(dev, t, v);
458 clear_bit(u, dev->keybit);
459 set_bit(v, dev->keybit);
460 for (i = 0; i < dev->keycodemax; i++)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500461 if (INPUT_KEYCODE(dev, i) == u)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 set_bit(u, dev->keybit);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return 0;
465
466 case EVIOCSFF:
467 if (dev->upload_effect) {
468 struct ff_effect effect;
469 int err;
470
471 if (copy_from_user(&effect, p, sizeof(effect)))
472 return -EFAULT;
473 err = dev->upload_effect(dev, &effect);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500474 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return -EFAULT;
476 return err;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500477 } else
478 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 case EVIOCRMFF:
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500481 if (!dev->erase_effect)
482 return -ENOSYS;
483
484 return dev->erase_effect(dev, (int)(unsigned long) p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 case EVIOCGEFFECTS:
487 if (put_user(dev->ff_effects_max, ip))
488 return -EFAULT;
489 return 0;
490
491 case EVIOCGRAB:
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500492 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (evdev->grab)
494 return -EBUSY;
495 if (input_grab_device(&evdev->handle))
496 return -EBUSY;
497 evdev->grab = list;
498 return 0;
499 } else {
500 if (evdev->grab != list)
501 return -EINVAL;
502 input_release_device(&evdev->handle);
503 evdev->grab = NULL;
504 return 0;
505 }
506
507 default:
508
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500509 if (_IOC_TYPE(cmd) != 'E')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return -EINVAL;
511
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500512 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500514 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500516 long *bits;
517 int len;
518
519 switch (_IOC_NR(cmd) & EV_MAX) {
520 case 0: bits = dev->evbit; len = EV_MAX; break;
521 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
522 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
523 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
524 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
525 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
526 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
527 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
Richard Purdie31581062005-09-06 15:19:06 -0700528 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500529 default: return -EINVAL;
530 }
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500531 return bits_to_user(bits, len, _IOC_SIZE(cmd), p, compat_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500533
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500534 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0)))
535 return bits_to_user(dev->key, KEY_MAX, _IOC_SIZE(cmd),
536 p, compat_mode);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500537
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500538 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0)))
539 return bits_to_user(dev->led, LED_MAX, _IOC_SIZE(cmd),
540 p, compat_mode);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500541
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500542 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0)))
543 return bits_to_user(dev->snd, SND_MAX, _IOC_SIZE(cmd),
544 p, compat_mode);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500545
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500546 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSW(0)))
547 return bits_to_user(dev->sw, SW_MAX, _IOC_SIZE(cmd),
548 p, compat_mode);
Richard Purdie31581062005-09-06 15:19:06 -0700549
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500550 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0)))
551 return str_to_user(dev->name, _IOC_SIZE(cmd), p);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500552
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500553 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0)))
554 return str_to_user(dev->phys, _IOC_SIZE(cmd), p);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500555
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500556 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0)))
557 return str_to_user(dev->uniq, _IOC_SIZE(cmd), p);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500558
559 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
560
561 int t = _IOC_NR(cmd) & ABS_MAX;
562
563 abs.value = dev->abs[t];
564 abs.minimum = dev->absmin[t];
565 abs.maximum = dev->absmax[t];
566 abs.fuzz = dev->absfuzz[t];
567 abs.flat = dev->absflat[t];
568
569 if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
570 return -EFAULT;
571
572 return 0;
573 }
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500577 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500579 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500581 int t = _IOC_NR(cmd) & ABS_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500583 if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
584 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500586 dev->abs[t] = abs.value;
587 dev->absmin[t] = abs.minimum;
588 dev->absmax[t] = abs.maximum;
589 dev->absfuzz[t] = abs.fuzz;
590 dev->absflat[t] = abs.flat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500592 return 0;
593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595 }
596 return -EINVAL;
597}
598
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500599static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
600{
601 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
602}
603
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500604#ifdef CONFIG_COMPAT
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500605static long evdev_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
606{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500607 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500608}
609#endif
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611static struct file_operations evdev_fops = {
612 .owner = THIS_MODULE,
613 .read = evdev_read,
614 .write = evdev_write,
615 .poll = evdev_poll,
616 .open = evdev_open,
617 .release = evdev_release,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500618 .unlocked_ioctl = evdev_ioctl,
619#ifdef CONFIG_COMPAT
620 .compat_ioctl = evdev_ioctl_compat,
621#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 .fasync = evdev_fasync,
623 .flush = evdev_flush
624};
625
626static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
627{
628 struct evdev *evdev;
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700629 struct class_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 int minor;
631
632 for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
633 if (minor == EVDEV_MINORS) {
634 printk(KERN_ERR "evdev: no more free evdev devices\n");
635 return NULL;
636 }
637
Eric Sesterhennb39787a2006-03-14 00:09:16 -0500638 if (!(evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 INIT_LIST_HEAD(&evdev->list);
642 init_waitqueue_head(&evdev->wait);
643
644 evdev->exist = 1;
645 evdev->minor = minor;
646 evdev->handle.dev = dev;
647 evdev->handle.name = evdev->name;
648 evdev->handle.handler = handler;
649 evdev->handle.private = evdev;
650 sprintf(evdev->name, "event%d", minor);
651
652 evdev_table[minor] = evdev;
653
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700654 cdev = class_device_create(&input_class, &dev->cdev,
gregkh@suse.de12356862005-03-15 14:26:30 -0800655 MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700656 dev->cdev.dev, evdev->name);
657
658 /* temporary symlink to keep userspace happy */
659 sysfs_create_link(&input_class.subsys.kset.kobj, &cdev->kobj,
660 evdev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 return &evdev->handle;
663}
664
665static void evdev_disconnect(struct input_handle *handle)
666{
667 struct evdev *evdev = handle->private;
668 struct evdev_list *list;
669
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700670 sysfs_remove_link(&input_class.subsys.kset.kobj, evdev->name);
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700671 class_device_destroy(&input_class,
gregkh@suse.de12356862005-03-15 14:26:30 -0800672 MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 evdev->exist = 0;
674
675 if (evdev->open) {
676 input_close_device(handle);
677 wake_up_interruptible(&evdev->wait);
678 list_for_each_entry(list, &evdev->list, node)
679 kill_fasync(&list->fasync, SIGIO, POLL_HUP);
680 } else
681 evdev_free(evdev);
682}
683
684static struct input_device_id evdev_ids[] = {
685 { .driver_info = 1 }, /* Matches all devices */
686 { }, /* Terminating zero entry */
687};
688
689MODULE_DEVICE_TABLE(input, evdev_ids);
690
691static struct input_handler evdev_handler = {
692 .event = evdev_event,
693 .connect = evdev_connect,
694 .disconnect = evdev_disconnect,
695 .fops = &evdev_fops,
696 .minor = EVDEV_MINOR_BASE,
697 .name = "evdev",
698 .id_table = evdev_ids,
699};
700
701static int __init evdev_init(void)
702{
703 input_register_handler(&evdev_handler);
704 return 0;
705}
706
707static void __exit evdev_exit(void)
708{
709 input_unregister_handler(&evdev_handler);
710}
711
712module_init(evdev_init);
713module_exit(evdev_exit);
714
715MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
716MODULE_DESCRIPTION("Input driver event char devices");
717MODULE_LICENSE("GPL");