blob: be6b93c20f606b9e477746e81688d7abe25bcdda [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/device.h>
Juergen Kreileder52658bb2005-05-29 02:26:43 -050022#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24struct evdev {
25 int exist;
26 int open;
27 int minor;
28 char name[16];
29 struct input_handle handle;
30 wait_queue_head_t wait;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040031 struct evdev_client *grab;
32 struct list_head client_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033};
34
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040035struct evdev_client {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 struct input_event buffer[EVDEV_BUFFER_SIZE];
37 int head;
38 int tail;
39 struct fasync_struct *fasync;
40 struct evdev *evdev;
41 struct list_head node;
42};
43
44static struct evdev *evdev_table[EVDEV_MINORS];
45
46static void evdev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
47{
48 struct evdev *evdev = handle->private;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040049 struct evdev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51 if (evdev->grab) {
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040052 client = evdev->grab;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040054 do_gettimeofday(&client->buffer[client->head].time);
55 client->buffer[client->head].type = type;
56 client->buffer[client->head].code = code;
57 client->buffer[client->head].value = value;
58 client->head = (client->head + 1) & (EVDEV_BUFFER_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040060 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 } else
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040062 list_for_each_entry(client, &evdev->client_list, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040064 do_gettimeofday(&client->buffer[client->head].time);
65 client->buffer[client->head].type = type;
66 client->buffer[client->head].code = code;
67 client->buffer[client->head].value = value;
68 client->head = (client->head + 1) & (EVDEV_BUFFER_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040070 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 }
72
73 wake_up_interruptible(&evdev->wait);
74}
75
76static int evdev_fasync(int fd, struct file *file, int on)
77{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040078 struct evdev_client *client = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 int retval;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040080
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040081 retval = fasync_helper(fd, file, on, &client->fasync);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return retval < 0 ? retval : 0;
84}
85
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040086static int evdev_flush(struct file *file, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040088 struct evdev_client *client = file->private_data;
89 struct evdev *evdev = client->evdev;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040090
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040091 if (!evdev->exist)
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040092 return -ENODEV;
93
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040094 return input_flush_device(&evdev->handle, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97static void evdev_free(struct evdev *evdev)
98{
99 evdev_table[evdev->minor] = NULL;
100 kfree(evdev);
101}
102
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400103static int evdev_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400105 struct evdev_client *client = file->private_data;
106 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400108 if (evdev->grab == client) {
109 input_release_device(&evdev->handle);
110 evdev->grab = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
112
113 evdev_fasync(-1, file, 0);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400114 list_del(&client->node);
115 kfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400117 if (!--evdev->open) {
118 if (evdev->exist)
119 input_close_device(&evdev->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 else
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400121 evdev_free(evdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return 0;
125}
126
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400127static int evdev_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400129 struct evdev_client *client;
130 struct evdev *evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 int i = iminor(inode) - EVDEV_MINOR_BASE;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400132 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400134 if (i >= EVDEV_MINORS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return -ENODEV;
136
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400137 evdev = evdev_table[i];
138
139 if (!evdev || !evdev->exist)
140 return -ENODEV;
141
142 client = kzalloc(sizeof(struct evdev_client), GFP_KERNEL);
143 if (!client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400146 client->evdev = evdev;
147 list_add_tail(&client->node, &evdev->client_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400149 if (!evdev->open++ && evdev->exist) {
150 error = input_open_device(&evdev->handle);
151 if (error) {
152 list_del(&client->node);
153 kfree(client);
154 return error;
155 }
156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400158 file->private_data = client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return 0;
160}
161
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500162#ifdef CONFIG_COMPAT
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500163
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500164struct input_event_compat {
165 struct compat_timeval time;
166 __u16 type;
167 __u16 code;
168 __s32 value;
169};
170
Andi Kleenbf2fcc62006-01-11 22:44:06 +0100171/* Note to the author of this code: did it ever occur to
172 you why the ifdefs are needed? Think about it again. -AK */
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500173#ifdef CONFIG_X86_64
Andi Kleenbf2fcc62006-01-11 22:44:06 +0100174# define COMPAT_TEST is_compat_task()
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500175#elif defined(CONFIG_IA64)
Al Viro64505782006-01-12 01:06:06 -0800176# define COMPAT_TEST IS_IA32_PROCESS(task_pt_regs(current))
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800177#elif defined(CONFIG_S390)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500178# define COMPAT_TEST test_thread_flag(TIF_31BIT)
Ralf Baechle59df6bb2005-09-03 15:56:23 -0700179#elif defined(CONFIG_MIPS)
180# define COMPAT_TEST (current->thread.mflags & MF_32BIT_ADDR)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500181#else
182# define COMPAT_TEST test_thread_flag(TIF_32BIT)
183#endif
184
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500185static inline size_t evdev_event_size(void)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500186{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500187 return COMPAT_TEST ?
188 sizeof(struct input_event_compat) : sizeof(struct input_event);
189}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500190
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500191static int evdev_event_from_user(const char __user *buffer, struct input_event *event)
192{
193 if (COMPAT_TEST) {
194 struct input_event_compat compat_event;
195
196 if (copy_from_user(&compat_event, buffer, sizeof(struct input_event_compat)))
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500197 return -EFAULT;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500198
199 event->time.tv_sec = compat_event.time.tv_sec;
200 event->time.tv_usec = compat_event.time.tv_usec;
201 event->type = compat_event.type;
202 event->code = compat_event.code;
203 event->value = compat_event.value;
204
205 } else {
206 if (copy_from_user(event, buffer, sizeof(struct input_event)))
207 return -EFAULT;
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500208 }
209
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500210 return 0;
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500211}
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500212
213static int evdev_event_to_user(char __user *buffer, const struct input_event *event)
214{
215 if (COMPAT_TEST) {
216 struct input_event_compat compat_event;
217
218 compat_event.time.tv_sec = event->time.tv_sec;
219 compat_event.time.tv_usec = event->time.tv_usec;
220 compat_event.type = event->type;
221 compat_event.code = event->code;
222 compat_event.value = event->value;
223
224 if (copy_to_user(buffer, &compat_event, sizeof(struct input_event_compat)))
225 return -EFAULT;
226
227 } else {
228 if (copy_to_user(buffer, event, sizeof(struct input_event)))
229 return -EFAULT;
230 }
231
232 return 0;
233}
234
235#else
236
237static inline size_t evdev_event_size(void)
238{
239 return sizeof(struct input_event);
240}
241
242static int evdev_event_from_user(const char __user *buffer, struct input_event *event)
243{
244 if (copy_from_user(event, buffer, sizeof(struct input_event)))
245 return -EFAULT;
246
247 return 0;
248}
249
250static int evdev_event_to_user(char __user *buffer, const struct input_event *event)
251{
252 if (copy_to_user(buffer, event, sizeof(struct input_event)))
253 return -EFAULT;
254
255 return 0;
256}
257
258#endif /* CONFIG_COMPAT */
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500259
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400260static ssize_t evdev_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400262 struct evdev_client *client = file->private_data;
263 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 struct input_event event;
265 int retval = 0;
266
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400267 if (!evdev->exist)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500268 return -ENODEV;
269
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500270 while (retval < count) {
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500271
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500272 if (evdev_event_from_user(buffer + retval, &event))
273 return -EFAULT;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400274 input_inject_event(&evdev->handle, event.type, event.code, event.value);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500275 retval += evdev_event_size();
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500276 }
277
278 return retval;
279}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500280
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400281static ssize_t evdev_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400283 struct evdev_client *client = file->private_data;
284 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 int retval;
286
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500287 if (count < evdev_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return -EINVAL;
289
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400290 if (client->head == client->tail && evdev->exist && (file->f_flags & O_NONBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return -EAGAIN;
292
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400293 retval = wait_event_interruptible(evdev->wait,
294 client->head != client->tail || !evdev->exist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (retval)
296 return retval;
297
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400298 if (!evdev->exist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return -ENODEV;
300
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400301 while (client->head != client->tail && retval + evdev_event_size() <= count) {
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500302
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400303 struct input_event *event = (struct input_event *) client->buffer + client->tail;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500304
305 if (evdev_event_to_user(buffer + retval, event))
306 return -EFAULT;
307
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400308 client->tail = (client->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500309 retval += evdev_event_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311
312 return retval;
313}
314
315/* No kernel lock - fine */
316static unsigned int evdev_poll(struct file *file, poll_table *wait)
317{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400318 struct evdev_client *client = file->private_data;
319 struct evdev *evdev = client->evdev;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400320
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400321 poll_wait(file, &evdev->wait, wait);
322 return ((client->head == client->tail) ? 0 : (POLLIN | POLLRDNORM)) |
323 (evdev->exist ? 0 : (POLLHUP | POLLERR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500326#ifdef CONFIG_COMPAT
327
328#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
329#define NBITS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
330
331#ifdef __BIG_ENDIAN
332static int bits_to_user(unsigned long *bits, unsigned int maxbit,
333 unsigned int maxlen, void __user *p, int compat)
334{
335 int len, i;
336
337 if (compat) {
338 len = NBITS_COMPAT(maxbit) * sizeof(compat_long_t);
Kenichi Nagaibf61f8d2007-05-11 01:12:15 -0400339 if (len > maxlen)
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500340 len = maxlen;
341
342 for (i = 0; i < len / sizeof(compat_long_t); i++)
343 if (copy_to_user((compat_long_t __user *) p + i,
344 (compat_long_t *) bits +
345 i + 1 - ((i % 2) << 1),
346 sizeof(compat_long_t)))
347 return -EFAULT;
348 } else {
349 len = NBITS(maxbit) * sizeof(long);
350 if (len > maxlen)
351 len = maxlen;
352
353 if (copy_to_user(p, bits, len))
354 return -EFAULT;
355 }
356
357 return len;
358}
359#else
360static int bits_to_user(unsigned long *bits, unsigned int maxbit,
361 unsigned int maxlen, void __user *p, int compat)
362{
363 int len = compat ?
364 NBITS_COMPAT(maxbit) * sizeof(compat_long_t) :
365 NBITS(maxbit) * sizeof(long);
366
367 if (len > maxlen)
368 len = maxlen;
369
370 return copy_to_user(p, bits, len) ? -EFAULT : len;
371}
372#endif /* __BIG_ENDIAN */
373
374#else
375
376static int bits_to_user(unsigned long *bits, unsigned int maxbit,
377 unsigned int maxlen, void __user *p, int compat)
378{
379 int len = NBITS(maxbit) * sizeof(long);
380
381 if (len > maxlen)
382 len = maxlen;
383
384 return copy_to_user(p, bits, len) ? -EFAULT : len;
385}
386
387#endif /* CONFIG_COMPAT */
388
389static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
390{
391 int len;
392
393 if (!str)
394 return -ENOENT;
395
396 len = strlen(str) + 1;
397 if (len > maxlen)
398 len = maxlen;
399
400 return copy_to_user(p, str, len) ? -EFAULT : len;
401}
402
403static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
404 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400406 struct evdev_client *client = file->private_data;
407 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 struct input_dev *dev = evdev->handle.dev;
409 struct input_absinfo abs;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400410 struct ff_effect effect;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500411 int __user *ip = (int __user *)p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 int i, t, u, v;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400413 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500415 if (!evdev->exist)
416 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 switch (cmd) {
419
420 case EVIOCGVERSION:
421 return put_user(EV_VERSION, ip);
422
423 case EVIOCGID:
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500424 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
425 return -EFAULT;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400426 return 0;
427
428 case EVIOCGREP:
429 if (!test_bit(EV_REP, dev->evbit))
430 return -ENOSYS;
431 if (put_user(dev->rep[REP_DELAY], ip))
432 return -EFAULT;
433 if (put_user(dev->rep[REP_PERIOD], ip + 1))
434 return -EFAULT;
435 return 0;
436
437 case EVIOCSREP:
438 if (!test_bit(EV_REP, dev->evbit))
439 return -ENOSYS;
440 if (get_user(u, ip))
441 return -EFAULT;
442 if (get_user(v, ip + 1))
443 return -EFAULT;
444
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400445 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
446 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500447
448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 case EVIOCGKEYCODE:
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500451 if (get_user(t, ip))
452 return -EFAULT;
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400453
454 error = dev->getkeycode(dev, t, &v);
455 if (error)
456 return error;
457
458 if (put_user(v, ip + 1))
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500459 return -EFAULT;
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return 0;
462
463 case EVIOCSKEYCODE:
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400464 if (get_user(t, ip) || get_user(v, ip + 1))
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500465 return -EFAULT;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500466
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400467 return dev->setkeycode(dev, t, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 case EVIOCSFF:
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400470 if (copy_from_user(&effect, p, sizeof(effect)))
471 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400473 error = input_ff_upload(dev, &effect, file);
474
475 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
476 return -EFAULT;
477
478 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 case EVIOCRMFF:
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400481 return input_ff_erase(dev, (int)(unsigned long) p, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 case EVIOCGEFFECTS:
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400484 i = test_bit(EV_FF, dev->evbit) ? dev->ff->max_effects : 0;
485 if (put_user(i, ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return -EFAULT;
487 return 0;
488
489 case EVIOCGRAB:
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500490 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (evdev->grab)
492 return -EBUSY;
493 if (input_grab_device(&evdev->handle))
494 return -EBUSY;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400495 evdev->grab = client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return 0;
497 } else {
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400498 if (evdev->grab != client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return -EINVAL;
500 input_release_device(&evdev->handle);
501 evdev->grab = NULL;
502 return 0;
503 }
504
505 default:
506
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500507 if (_IOC_TYPE(cmd) != 'E')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return -EINVAL;
509
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500510 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500512 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Dmitry Torokhovce305b62007-05-03 00:53:18 -0400514 unsigned long *bits;
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500515 int len;
516
517 switch (_IOC_NR(cmd) & EV_MAX) {
518 case 0: bits = dev->evbit; len = EV_MAX; break;
519 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
520 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
521 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
522 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
523 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
524 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
525 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
Richard Purdie31581062005-09-06 15:19:06 -0700526 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500527 default: return -EINVAL;
528 }
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500529 return bits_to_user(bits, len, _IOC_SIZE(cmd), p, compat_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500531
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500532 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0)))
533 return bits_to_user(dev->key, KEY_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(EVIOCGLED(0)))
537 return bits_to_user(dev->led, LED_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(EVIOCGSND(0)))
541 return bits_to_user(dev->snd, SND_MAX, _IOC_SIZE(cmd),
542 p, compat_mode);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500543
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500544 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSW(0)))
545 return bits_to_user(dev->sw, SW_MAX, _IOC_SIZE(cmd),
546 p, compat_mode);
Richard Purdie31581062005-09-06 15:19:06 -0700547
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500548 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0)))
549 return str_to_user(dev->name, _IOC_SIZE(cmd), p);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500550
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500551 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0)))
552 return str_to_user(dev->phys, _IOC_SIZE(cmd), p);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500553
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500554 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0)))
555 return str_to_user(dev->uniq, _IOC_SIZE(cmd), p);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500556
557 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
558
Dmitry Torokhovce305b62007-05-03 00:53:18 -0400559 t = _IOC_NR(cmd) & ABS_MAX;
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500560
561 abs.value = dev->abs[t];
562 abs.minimum = dev->absmin[t];
563 abs.maximum = dev->absmax[t];
564 abs.fuzz = dev->absfuzz[t];
565 abs.flat = dev->absflat[t];
566
567 if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
568 return -EFAULT;
569
570 return 0;
571 }
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500575 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500577 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Dmitry Torokhovce305b62007-05-03 00:53:18 -0400579 t = _IOC_NR(cmd) & ABS_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500581 if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
582 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500584 dev->abs[t] = abs.value;
585 dev->absmin[t] = abs.minimum;
586 dev->absmax[t] = abs.maximum;
587 dev->absfuzz[t] = abs.fuzz;
588 dev->absflat[t] = abs.flat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500590 return 0;
591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593 }
594 return -EINVAL;
595}
596
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500597static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
598{
599 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
600}
601
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500602#ifdef CONFIG_COMPAT
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500603static long evdev_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
604{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500605 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500606}
607#endif
608
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400609static const struct file_operations evdev_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 .owner = THIS_MODULE,
611 .read = evdev_read,
612 .write = evdev_write,
613 .poll = evdev_poll,
614 .open = evdev_open,
615 .release = evdev_release,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500616 .unlocked_ioctl = evdev_ioctl,
617#ifdef CONFIG_COMPAT
618 .compat_ioctl = evdev_ioctl_compat,
619#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 .fasync = evdev_fasync,
621 .flush = evdev_flush
622};
623
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400624static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
625 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 struct evdev *evdev;
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700628 struct class_device *cdev;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400629 dev_t devt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 int minor;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400631 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
634 if (minor == EVDEV_MINORS) {
635 printk(KERN_ERR "evdev: no more free evdev devices\n");
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400636 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
638
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400639 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
640 if (!evdev)
641 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400643 INIT_LIST_HEAD(&evdev->client_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 init_waitqueue_head(&evdev->wait);
645
646 evdev->exist = 1;
647 evdev->minor = minor;
648 evdev->handle.dev = dev;
649 evdev->handle.name = evdev->name;
650 evdev->handle.handler = handler;
651 evdev->handle.private = evdev;
652 sprintf(evdev->name, "event%d", minor);
653
654 evdev_table[minor] = evdev;
655
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400656 devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
657
658 cdev = class_device_create(&input_class, &dev->cdev, devt,
659 dev->cdev.dev, evdev->name);
660 if (IS_ERR(cdev)) {
661 error = PTR_ERR(cdev);
662 goto err_free_evdev;
663 }
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700664
665 /* temporary symlink to keep userspace happy */
Linus Torvaldsa3d52132007-05-04 18:13:17 -0700666 error = sysfs_create_link(&input_class.subsys.kobj,
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400667 &cdev->kobj, evdev->name);
668 if (error)
669 goto err_cdev_destroy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400671 error = input_register_handle(&evdev->handle);
672 if (error)
673 goto err_remove_link;
674
675 return 0;
676
677 err_remove_link:
Linus Torvaldsa3d52132007-05-04 18:13:17 -0700678 sysfs_remove_link(&input_class.subsys.kobj, evdev->name);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400679 err_cdev_destroy:
680 class_device_destroy(&input_class, devt);
681 err_free_evdev:
682 kfree(evdev);
683 evdev_table[minor] = NULL;
684 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
687static void evdev_disconnect(struct input_handle *handle)
688{
689 struct evdev *evdev = handle->private;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400690 struct evdev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400692 input_unregister_handle(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700694 sysfs_remove_link(&input_class.subsys.kobj, evdev->name);
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700695 class_device_destroy(&input_class,
gregkh@suse.de12356862005-03-15 14:26:30 -0800696 MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 evdev->exist = 0;
698
699 if (evdev->open) {
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400700 input_flush_device(handle, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 input_close_device(handle);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400702 list_for_each_entry(client, &evdev->client_list, node)
703 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
Dmitry Torokhov1dfa2812007-06-03 23:29:36 -0400704 wake_up_interruptible(&evdev->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 } else
706 evdev_free(evdev);
707}
708
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400709static const struct input_device_id evdev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 { .driver_info = 1 }, /* Matches all devices */
711 { }, /* Terminating zero entry */
712};
713
714MODULE_DEVICE_TABLE(input, evdev_ids);
715
716static struct input_handler evdev_handler = {
717 .event = evdev_event,
718 .connect = evdev_connect,
719 .disconnect = evdev_disconnect,
720 .fops = &evdev_fops,
721 .minor = EVDEV_MINOR_BASE,
722 .name = "evdev",
723 .id_table = evdev_ids,
724};
725
726static int __init evdev_init(void)
727{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -0400728 return input_register_handler(&evdev_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
731static void __exit evdev_exit(void)
732{
733 input_unregister_handler(&evdev_handler);
734}
735
736module_init(evdev_init);
737module_exit(evdev_exit);
738
739MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
740MODULE_DESCRIPTION("Input driver event char devices");
741MODULE_LICENSE("GPL");