blob: 8a4cce5c7806acd269d783890837ea26061f9daa [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;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040032 struct evdev_client *grab;
33 struct list_head client_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034};
35
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040036struct evdev_client {
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 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;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040050 struct evdev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 if (evdev->grab) {
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040053 client = evdev->grab;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040055 do_gettimeofday(&client->buffer[client->head].time);
56 client->buffer[client->head].type = type;
57 client->buffer[client->head].code = code;
58 client->buffer[client->head].value = value;
59 client->head = (client->head + 1) & (EVDEV_BUFFER_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040061 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 } else
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040063 list_for_each_entry(client, &evdev->client_list, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040065 do_gettimeofday(&client->buffer[client->head].time);
66 client->buffer[client->head].type = type;
67 client->buffer[client->head].code = code;
68 client->buffer[client->head].value = value;
69 client->head = (client->head + 1) & (EVDEV_BUFFER_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040071 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 }
73
74 wake_up_interruptible(&evdev->wait);
75}
76
77static int evdev_fasync(int fd, struct file *file, int on)
78{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040079 struct evdev_client *client = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 int retval;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040081
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040082 retval = fasync_helper(fd, file, on, &client->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{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040089 struct evdev_client *client = file->private_data;
90 struct evdev *evdev = client->evdev;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040091
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040092 if (!evdev->exist)
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040093 return -ENODEV;
94
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040095 return input_flush_device(&evdev->handle, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
98static void evdev_free(struct evdev *evdev)
99{
100 evdev_table[evdev->minor] = NULL;
101 kfree(evdev);
102}
103
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400104static int evdev_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400106 struct evdev_client *client = file->private_data;
107 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400109 if (evdev->grab == client) {
110 input_release_device(&evdev->handle);
111 evdev->grab = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113
114 evdev_fasync(-1, file, 0);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400115 list_del(&client->node);
116 kfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400118 if (!--evdev->open) {
119 if (evdev->exist)
120 input_close_device(&evdev->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 else
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400122 evdev_free(evdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return 0;
126}
127
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400128static int evdev_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400130 struct evdev_client *client;
131 struct evdev *evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 int i = iminor(inode) - EVDEV_MINOR_BASE;
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 Torokhovd0ffb9b2007-04-12 01:30:00 -0400149 if (!evdev->open++ && evdev->exist)
150 input_open_device(&evdev->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400152 file->private_data = client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return 0;
154}
155
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500156#ifdef CONFIG_COMPAT
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500157
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500158struct input_event_compat {
159 struct compat_timeval time;
160 __u16 type;
161 __u16 code;
162 __s32 value;
163};
164
Andi Kleenbf2fcc62006-01-11 22:44:06 +0100165/* Note to the author of this code: did it ever occur to
166 you why the ifdefs are needed? Think about it again. -AK */
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500167#ifdef CONFIG_X86_64
Andi Kleenbf2fcc62006-01-11 22:44:06 +0100168# define COMPAT_TEST is_compat_task()
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500169#elif defined(CONFIG_IA64)
Al Viro64505782006-01-12 01:06:06 -0800170# define COMPAT_TEST IS_IA32_PROCESS(task_pt_regs(current))
Martin Schwidefsky347a8dc2006-01-06 00:19:28 -0800171#elif defined(CONFIG_S390)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500172# define COMPAT_TEST test_thread_flag(TIF_31BIT)
Ralf Baechle59df6bb2005-09-03 15:56:23 -0700173#elif defined(CONFIG_MIPS)
174# define COMPAT_TEST (current->thread.mflags & MF_32BIT_ADDR)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500175#else
176# define COMPAT_TEST test_thread_flag(TIF_32BIT)
177#endif
178
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500179static inline size_t evdev_event_size(void)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500180{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500181 return COMPAT_TEST ?
182 sizeof(struct input_event_compat) : sizeof(struct input_event);
183}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500184
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500185static int evdev_event_from_user(const char __user *buffer, struct input_event *event)
186{
187 if (COMPAT_TEST) {
188 struct input_event_compat compat_event;
189
190 if (copy_from_user(&compat_event, buffer, sizeof(struct input_event_compat)))
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500191 return -EFAULT;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500192
193 event->time.tv_sec = compat_event.time.tv_sec;
194 event->time.tv_usec = compat_event.time.tv_usec;
195 event->type = compat_event.type;
196 event->code = compat_event.code;
197 event->value = compat_event.value;
198
199 } else {
200 if (copy_from_user(event, buffer, sizeof(struct input_event)))
201 return -EFAULT;
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500202 }
203
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500204 return 0;
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500205}
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500206
207static int evdev_event_to_user(char __user *buffer, const struct input_event *event)
208{
209 if (COMPAT_TEST) {
210 struct input_event_compat compat_event;
211
212 compat_event.time.tv_sec = event->time.tv_sec;
213 compat_event.time.tv_usec = event->time.tv_usec;
214 compat_event.type = event->type;
215 compat_event.code = event->code;
216 compat_event.value = event->value;
217
218 if (copy_to_user(buffer, &compat_event, sizeof(struct input_event_compat)))
219 return -EFAULT;
220
221 } else {
222 if (copy_to_user(buffer, event, sizeof(struct input_event)))
223 return -EFAULT;
224 }
225
226 return 0;
227}
228
229#else
230
231static inline size_t evdev_event_size(void)
232{
233 return sizeof(struct input_event);
234}
235
236static int evdev_event_from_user(const char __user *buffer, struct input_event *event)
237{
238 if (copy_from_user(event, buffer, sizeof(struct input_event)))
239 return -EFAULT;
240
241 return 0;
242}
243
244static int evdev_event_to_user(char __user *buffer, const struct input_event *event)
245{
246 if (copy_to_user(buffer, event, sizeof(struct input_event)))
247 return -EFAULT;
248
249 return 0;
250}
251
252#endif /* CONFIG_COMPAT */
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500253
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400254static ssize_t evdev_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400256 struct evdev_client *client = file->private_data;
257 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 struct input_event event;
259 int retval = 0;
260
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400261 if (!evdev->exist)
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500262 return -ENODEV;
263
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500264 while (retval < count) {
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500265
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500266 if (evdev_event_from_user(buffer + retval, &event))
267 return -EFAULT;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400268 input_inject_event(&evdev->handle, event.type, event.code, event.value);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500269 retval += evdev_event_size();
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500270 }
271
272 return retval;
273}
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500274
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400275static ssize_t evdev_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400277 struct evdev_client *client = file->private_data;
278 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 int retval;
280
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500281 if (count < evdev_event_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return -EINVAL;
283
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400284 if (client->head == client->tail && evdev->exist && (file->f_flags & O_NONBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return -EAGAIN;
286
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400287 retval = wait_event_interruptible(evdev->wait,
288 client->head != client->tail || !evdev->exist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (retval)
290 return retval;
291
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400292 if (!evdev->exist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return -ENODEV;
294
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400295 while (client->head != client->tail && retval + evdev_event_size() <= count) {
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500296
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400297 struct input_event *event = (struct input_event *) client->buffer + client->tail;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500298
299 if (evdev_event_to_user(buffer + retval, event))
300 return -EFAULT;
301
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400302 client->tail = (client->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500303 retval += evdev_event_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305
306 return retval;
307}
308
309/* No kernel lock - fine */
310static unsigned int evdev_poll(struct file *file, poll_table *wait)
311{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400312 struct evdev_client *client = file->private_data;
313 struct evdev *evdev = client->evdev;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400314
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400315 poll_wait(file, &evdev->wait, wait);
316 return ((client->head == client->tail) ? 0 : (POLLIN | POLLRDNORM)) |
317 (evdev->exist ? 0 : (POLLHUP | POLLERR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500320#ifdef CONFIG_COMPAT
321
322#define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
323#define NBITS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
324
325#ifdef __BIG_ENDIAN
326static int bits_to_user(unsigned long *bits, unsigned int maxbit,
327 unsigned int maxlen, void __user *p, int compat)
328{
329 int len, i;
330
331 if (compat) {
332 len = NBITS_COMPAT(maxbit) * sizeof(compat_long_t);
333 if (len < maxlen)
334 len = maxlen;
335
336 for (i = 0; i < len / sizeof(compat_long_t); i++)
337 if (copy_to_user((compat_long_t __user *) p + i,
338 (compat_long_t *) bits +
339 i + 1 - ((i % 2) << 1),
340 sizeof(compat_long_t)))
341 return -EFAULT;
342 } else {
343 len = NBITS(maxbit) * sizeof(long);
344 if (len > maxlen)
345 len = maxlen;
346
347 if (copy_to_user(p, bits, len))
348 return -EFAULT;
349 }
350
351 return len;
352}
353#else
354static int bits_to_user(unsigned long *bits, unsigned int maxbit,
355 unsigned int maxlen, void __user *p, int compat)
356{
357 int len = compat ?
358 NBITS_COMPAT(maxbit) * sizeof(compat_long_t) :
359 NBITS(maxbit) * sizeof(long);
360
361 if (len > maxlen)
362 len = maxlen;
363
364 return copy_to_user(p, bits, len) ? -EFAULT : len;
365}
366#endif /* __BIG_ENDIAN */
367
368#else
369
370static int bits_to_user(unsigned long *bits, unsigned int maxbit,
371 unsigned int maxlen, void __user *p, int compat)
372{
373 int len = NBITS(maxbit) * sizeof(long);
374
375 if (len > maxlen)
376 len = maxlen;
377
378 return copy_to_user(p, bits, len) ? -EFAULT : len;
379}
380
381#endif /* CONFIG_COMPAT */
382
383static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
384{
385 int len;
386
387 if (!str)
388 return -ENOENT;
389
390 len = strlen(str) + 1;
391 if (len > maxlen)
392 len = maxlen;
393
394 return copy_to_user(p, str, len) ? -EFAULT : len;
395}
396
397static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
398 void __user *p, int compat_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400400 struct evdev_client *client = file->private_data;
401 struct evdev *evdev = client->evdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 struct input_dev *dev = evdev->handle.dev;
403 struct input_absinfo abs;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400404 struct ff_effect effect;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500405 int __user *ip = (int __user *)p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 int i, t, u, v;
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400407 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500409 if (!evdev->exist)
410 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 switch (cmd) {
413
414 case EVIOCGVERSION:
415 return put_user(EV_VERSION, ip);
416
417 case EVIOCGID:
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500418 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
419 return -EFAULT;
Dmitry Torokhov08791e52006-04-29 01:13:21 -0400420 return 0;
421
422 case EVIOCGREP:
423 if (!test_bit(EV_REP, dev->evbit))
424 return -ENOSYS;
425 if (put_user(dev->rep[REP_DELAY], ip))
426 return -EFAULT;
427 if (put_user(dev->rep[REP_PERIOD], ip + 1))
428 return -EFAULT;
429 return 0;
430
431 case EVIOCSREP:
432 if (!test_bit(EV_REP, dev->evbit))
433 return -ENOSYS;
434 if (get_user(u, ip))
435 return -EFAULT;
436 if (get_user(v, ip + 1))
437 return -EFAULT;
438
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400439 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
440 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500441
442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 case EVIOCGKEYCODE:
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500445 if (get_user(t, ip))
446 return -EFAULT;
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400447
448 error = dev->getkeycode(dev, t, &v);
449 if (error)
450 return error;
451
452 if (put_user(v, ip + 1))
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500453 return -EFAULT;
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return 0;
456
457 case EVIOCSKEYCODE:
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400458 if (get_user(t, ip) || get_user(v, ip + 1))
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500459 return -EFAULT;
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500460
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400461 return dev->setkeycode(dev, t, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 case EVIOCSFF:
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400464 if (copy_from_user(&effect, p, sizeof(effect)))
465 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400467 error = input_ff_upload(dev, &effect, file);
468
469 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
470 return -EFAULT;
471
472 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 case EVIOCRMFF:
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400475 return input_ff_erase(dev, (int)(unsigned long) p, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 case EVIOCGEFFECTS:
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400478 i = test_bit(EV_FF, dev->evbit) ? dev->ff->max_effects : 0;
479 if (put_user(i, ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return -EFAULT;
481 return 0;
482
483 case EVIOCGRAB:
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500484 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (evdev->grab)
486 return -EBUSY;
487 if (input_grab_device(&evdev->handle))
488 return -EBUSY;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400489 evdev->grab = client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return 0;
491 } else {
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400492 if (evdev->grab != client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return -EINVAL;
494 input_release_device(&evdev->handle);
495 evdev->grab = NULL;
496 return 0;
497 }
498
499 default:
500
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500501 if (_IOC_TYPE(cmd) != 'E')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return -EINVAL;
503
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500504 if (_IOC_DIR(cmd) == _IOC_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500506 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500508 long *bits;
509 int len;
510
511 switch (_IOC_NR(cmd) & EV_MAX) {
512 case 0: bits = dev->evbit; len = EV_MAX; break;
513 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
514 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
515 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
516 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
517 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
518 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
519 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
Richard Purdie31581062005-09-06 15:19:06 -0700520 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500521 default: return -EINVAL;
522 }
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500523 return bits_to_user(bits, len, _IOC_SIZE(cmd), p, compat_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500525
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500526 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0)))
527 return bits_to_user(dev->key, KEY_MAX, _IOC_SIZE(cmd),
528 p, compat_mode);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500529
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500530 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0)))
531 return bits_to_user(dev->led, LED_MAX, _IOC_SIZE(cmd),
532 p, compat_mode);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500533
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500534 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0)))
535 return bits_to_user(dev->snd, SND_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(EVIOCGSW(0)))
539 return bits_to_user(dev->sw, SW_MAX, _IOC_SIZE(cmd),
540 p, compat_mode);
Richard Purdie31581062005-09-06 15:19:06 -0700541
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500542 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0)))
543 return str_to_user(dev->name, _IOC_SIZE(cmd), p);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500544
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500545 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0)))
546 return str_to_user(dev->phys, _IOC_SIZE(cmd), p);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500547
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500548 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0)))
549 return str_to_user(dev->uniq, _IOC_SIZE(cmd), p);
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500550
551 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
552
553 int t = _IOC_NR(cmd) & ABS_MAX;
554
555 abs.value = dev->abs[t];
556 abs.minimum = dev->absmin[t];
557 abs.maximum = dev->absmax[t];
558 abs.fuzz = dev->absfuzz[t];
559 abs.flat = dev->absflat[t];
560
561 if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
562 return -EFAULT;
563
564 return 0;
565 }
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500569 if (_IOC_DIR(cmd) == _IOC_WRITE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500571 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500573 int t = _IOC_NR(cmd) & ABS_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500575 if (copy_from_user(&abs, p, sizeof(struct input_absinfo)))
576 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500578 dev->abs[t] = abs.value;
579 dev->absmin[t] = abs.minimum;
580 dev->absmax[t] = abs.maximum;
581 dev->absfuzz[t] = abs.fuzz;
582 dev->absflat[t] = abs.flat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Vojtech Pavlik41e979f2005-05-29 02:30:15 -0500584 return 0;
585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587 }
588 return -EINVAL;
589}
590
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500591static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
592{
593 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
594}
595
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500596#ifdef CONFIG_COMPAT
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500597static long evdev_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg)
598{
Dmitry Torokhov3a51f7c2005-12-11 12:40:37 -0500599 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500600}
601#endif
602
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400603static const struct file_operations evdev_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 .owner = THIS_MODULE,
605 .read = evdev_read,
606 .write = evdev_write,
607 .poll = evdev_poll,
608 .open = evdev_open,
609 .release = evdev_release,
Juergen Kreileder52658bb2005-05-29 02:26:43 -0500610 .unlocked_ioctl = evdev_ioctl,
611#ifdef CONFIG_COMPAT
612 .compat_ioctl = evdev_ioctl_compat,
613#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 .fasync = evdev_fasync,
615 .flush = evdev_flush
616};
617
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400618static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
619 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 struct evdev *evdev;
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700622 struct class_device *cdev;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400623 dev_t devt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 int minor;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400625 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
628 if (minor == EVDEV_MINORS) {
629 printk(KERN_ERR "evdev: no more free evdev devices\n");
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400630 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400633 evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
634 if (!evdev)
635 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400637 INIT_LIST_HEAD(&evdev->client_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 init_waitqueue_head(&evdev->wait);
639
640 evdev->exist = 1;
641 evdev->minor = minor;
642 evdev->handle.dev = dev;
643 evdev->handle.name = evdev->name;
644 evdev->handle.handler = handler;
645 evdev->handle.private = evdev;
646 sprintf(evdev->name, "event%d", minor);
647
648 evdev_table[minor] = evdev;
649
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400650 devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),
651
652 cdev = class_device_create(&input_class, &dev->cdev, devt,
653 dev->cdev.dev, evdev->name);
654 if (IS_ERR(cdev)) {
655 error = PTR_ERR(cdev);
656 goto err_free_evdev;
657 }
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700658
659 /* temporary symlink to keep userspace happy */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400660 error = sysfs_create_link(&input_class.subsys.kset.kobj,
661 &cdev->kobj, evdev->name);
662 if (error)
663 goto err_cdev_destroy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400665 error = input_register_handle(&evdev->handle);
666 if (error)
667 goto err_remove_link;
668
669 return 0;
670
671 err_remove_link:
672 sysfs_remove_link(&input_class.subsys.kset.kobj, evdev->name);
673 err_cdev_destroy:
674 class_device_destroy(&input_class, devt);
675 err_free_evdev:
676 kfree(evdev);
677 evdev_table[minor] = NULL;
678 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
681static void evdev_disconnect(struct input_handle *handle)
682{
683 struct evdev *evdev = handle->private;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400684 struct evdev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400686 input_unregister_handle(handle);
687
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700688 sysfs_remove_link(&input_class.subsys.kset.kobj, evdev->name);
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700689 class_device_destroy(&input_class,
gregkh@suse.de12356862005-03-15 14:26:30 -0800690 MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 evdev->exist = 0;
692
693 if (evdev->open) {
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400694 input_flush_device(handle, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 input_close_device(handle);
696 wake_up_interruptible(&evdev->wait);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400697 list_for_each_entry(client, &evdev->client_list, node)
698 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 } else
700 evdev_free(evdev);
701}
702
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400703static const struct input_device_id evdev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 { .driver_info = 1 }, /* Matches all devices */
705 { }, /* Terminating zero entry */
706};
707
708MODULE_DEVICE_TABLE(input, evdev_ids);
709
710static struct input_handler evdev_handler = {
711 .event = evdev_event,
712 .connect = evdev_connect,
713 .disconnect = evdev_disconnect,
714 .fops = &evdev_fops,
715 .minor = EVDEV_MINOR_BASE,
716 .name = "evdev",
717 .id_table = evdev_ids,
718};
719
720static int __init evdev_init(void)
721{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -0400722 return input_register_handler(&evdev_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
725static void __exit evdev_exit(void)
726{
727 input_unregister_handler(&evdev_handler);
728}
729
730module_init(evdev_init);
731module_exit(evdev_exit);
732
733MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
734MODULE_DESCRIPTION("Input driver event char devices");
735MODULE_LICENSE("GPL");