Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 23 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | struct 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 | |
| 36 | struct 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 | |
| 45 | static struct evdev *evdev_table[EVDEV_MINORS]; |
| 46 | |
| 47 | static 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 | |
| 77 | static int evdev_fasync(int fd, struct file *file, int on) |
| 78 | { |
| 79 | int retval; |
| 80 | struct evdev_list *list = file->private_data; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | retval = fasync_helper(fd, file, on, &list->fasync); |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | return retval < 0 ? retval : 0; |
| 85 | } |
| 86 | |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 87 | static int evdev_flush(struct file *file, fl_owner_t id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
| 89 | struct evdev_list *list = file->private_data; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 90 | |
| 91 | if (!list->evdev->exist) |
| 92 | return -ENODEV; |
| 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | return input_flush_device(&list->evdev->handle, file); |
| 95 | } |
| 96 | |
| 97 | static void evdev_free(struct evdev *evdev) |
| 98 | { |
| 99 | evdev_table[evdev->minor] = NULL; |
| 100 | kfree(evdev); |
| 101 | } |
| 102 | |
| 103 | static 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 | |
| 126 | static int evdev_open(struct inode * inode, struct file * file) |
| 127 | { |
| 128 | struct evdev_list *list; |
| 129 | int i = iminor(inode) - EVDEV_MINOR_BASE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
| 131 | if (i >= EVDEV_MINORS || !evdev_table[i] || !evdev_table[i]->exist) |
| 132 | return -ENODEV; |
| 133 | |
Eric Sesterhenn | b39787a | 2006-03-14 00:09:16 -0500 | [diff] [blame] | 134 | if (!(list = kzalloc(sizeof(struct evdev_list), GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 148 | #ifdef CONFIG_COMPAT |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 149 | |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 150 | struct input_event_compat { |
| 151 | struct compat_timeval time; |
| 152 | __u16 type; |
| 153 | __u16 code; |
| 154 | __s32 value; |
| 155 | }; |
| 156 | |
Andi Kleen | bf2fcc6 | 2006-01-11 22:44:06 +0100 | [diff] [blame] | 157 | /* 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 Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 159 | #ifdef CONFIG_X86_64 |
Andi Kleen | bf2fcc6 | 2006-01-11 22:44:06 +0100 | [diff] [blame] | 160 | # define COMPAT_TEST is_compat_task() |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 161 | #elif defined(CONFIG_IA64) |
Al Viro | 6450578 | 2006-01-12 01:06:06 -0800 | [diff] [blame] | 162 | # define COMPAT_TEST IS_IA32_PROCESS(task_pt_regs(current)) |
Martin Schwidefsky | 347a8dc | 2006-01-06 00:19:28 -0800 | [diff] [blame] | 163 | #elif defined(CONFIG_S390) |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 164 | # define COMPAT_TEST test_thread_flag(TIF_31BIT) |
Ralf Baechle | 59df6bb | 2005-09-03 15:56:23 -0700 | [diff] [blame] | 165 | #elif defined(CONFIG_MIPS) |
| 166 | # define COMPAT_TEST (current->thread.mflags & MF_32BIT_ADDR) |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 167 | #else |
| 168 | # define COMPAT_TEST test_thread_flag(TIF_32BIT) |
| 169 | #endif |
| 170 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 171 | static inline size_t evdev_event_size(void) |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 172 | { |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 173 | return COMPAT_TEST ? |
| 174 | sizeof(struct input_event_compat) : sizeof(struct input_event); |
| 175 | } |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 176 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 177 | static 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 Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 183 | return -EFAULT; |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 184 | |
| 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 Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 194 | } |
| 195 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 196 | return 0; |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 197 | } |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 198 | |
| 199 | static 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 | |
| 223 | static inline size_t evdev_event_size(void) |
| 224 | { |
| 225 | return sizeof(struct input_event); |
| 226 | } |
| 227 | |
| 228 | static 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 | |
| 236 | static 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 Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | static 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 Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 252 | if (!list->evdev->exist) |
| 253 | return -ENODEV; |
| 254 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 255 | while (retval < count) { |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 256 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 257 | if (evdev_event_from_user(buffer + retval, &event)) |
| 258 | return -EFAULT; |
Dmitry Torokhov | 0e739d2 | 2006-07-06 00:22:43 -0400 | [diff] [blame] | 259 | input_inject_event(&list->evdev->handle, event.type, event.code, event.value); |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 260 | retval += evdev_event_size(); |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | return retval; |
| 264 | } |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | static 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 Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 271 | if (count < evdev_event_size()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | 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 Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 286 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1); |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 294 | retval += evdev_event_size(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | return retval; |
| 298 | } |
| 299 | |
| 300 | /* No kernel lock - fine */ |
| 301 | static unsigned int evdev_poll(struct file *file, poll_table *wait) |
| 302 | { |
| 303 | struct evdev_list *list = file->private_data; |
Dmitry Torokhov | 1e0afb2 | 2006-06-26 01:48:47 -0400 | [diff] [blame] | 304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | poll_wait(file, &list->evdev->wait, wait); |
| 306 | return ((list->head == list->tail) ? 0 : (POLLIN | POLLRDNORM)) | |
| 307 | (list->evdev->exist ? 0 : (POLLHUP | POLLERR)); |
| 308 | } |
| 309 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 310 | #ifdef CONFIG_COMPAT |
| 311 | |
| 312 | #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8) |
| 313 | #define NBITS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1) |
| 314 | |
| 315 | #ifdef __BIG_ENDIAN |
| 316 | static int bits_to_user(unsigned long *bits, unsigned int maxbit, |
| 317 | unsigned int maxlen, void __user *p, int compat) |
| 318 | { |
| 319 | int len, i; |
| 320 | |
| 321 | if (compat) { |
| 322 | len = NBITS_COMPAT(maxbit) * sizeof(compat_long_t); |
| 323 | if (len < maxlen) |
| 324 | len = maxlen; |
| 325 | |
| 326 | for (i = 0; i < len / sizeof(compat_long_t); i++) |
| 327 | if (copy_to_user((compat_long_t __user *) p + i, |
| 328 | (compat_long_t *) bits + |
| 329 | i + 1 - ((i % 2) << 1), |
| 330 | sizeof(compat_long_t))) |
| 331 | return -EFAULT; |
| 332 | } else { |
| 333 | len = NBITS(maxbit) * sizeof(long); |
| 334 | if (len > maxlen) |
| 335 | len = maxlen; |
| 336 | |
| 337 | if (copy_to_user(p, bits, len)) |
| 338 | return -EFAULT; |
| 339 | } |
| 340 | |
| 341 | return len; |
| 342 | } |
| 343 | #else |
| 344 | static int bits_to_user(unsigned long *bits, unsigned int maxbit, |
| 345 | unsigned int maxlen, void __user *p, int compat) |
| 346 | { |
| 347 | int len = compat ? |
| 348 | NBITS_COMPAT(maxbit) * sizeof(compat_long_t) : |
| 349 | NBITS(maxbit) * sizeof(long); |
| 350 | |
| 351 | if (len > maxlen) |
| 352 | len = maxlen; |
| 353 | |
| 354 | return copy_to_user(p, bits, len) ? -EFAULT : len; |
| 355 | } |
| 356 | #endif /* __BIG_ENDIAN */ |
| 357 | |
| 358 | #else |
| 359 | |
| 360 | static int bits_to_user(unsigned long *bits, unsigned int maxbit, |
| 361 | unsigned int maxlen, void __user *p, int compat) |
| 362 | { |
| 363 | int len = NBITS(maxbit) * sizeof(long); |
| 364 | |
| 365 | if (len > maxlen) |
| 366 | len = maxlen; |
| 367 | |
| 368 | return copy_to_user(p, bits, len) ? -EFAULT : len; |
| 369 | } |
| 370 | |
| 371 | #endif /* CONFIG_COMPAT */ |
| 372 | |
| 373 | static int str_to_user(const char *str, unsigned int maxlen, void __user *p) |
| 374 | { |
| 375 | int len; |
| 376 | |
| 377 | if (!str) |
| 378 | return -ENOENT; |
| 379 | |
| 380 | len = strlen(str) + 1; |
| 381 | if (len > maxlen) |
| 382 | len = maxlen; |
| 383 | |
| 384 | return copy_to_user(p, str, len) ? -EFAULT : len; |
| 385 | } |
| 386 | |
| 387 | static long evdev_ioctl_handler(struct file *file, unsigned int cmd, |
| 388 | void __user *p, int compat_mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | { |
| 390 | struct evdev_list *list = file->private_data; |
| 391 | struct evdev *evdev = list->evdev; |
| 392 | struct input_dev *dev = evdev->handle.dev; |
| 393 | struct input_absinfo abs; |
Anssi Hannula | 509ca1a | 2006-07-19 01:40:22 -0400 | [diff] [blame] | 394 | struct ff_effect effect; |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 395 | int __user *ip = (int __user *)p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | int i, t, u, v; |
Anssi Hannula | 509ca1a | 2006-07-19 01:40:22 -0400 | [diff] [blame] | 397 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 399 | if (!evdev->exist) |
| 400 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | |
| 402 | switch (cmd) { |
| 403 | |
| 404 | case EVIOCGVERSION: |
| 405 | return put_user(EV_VERSION, ip); |
| 406 | |
| 407 | case EVIOCGID: |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 408 | if (copy_to_user(p, &dev->id, sizeof(struct input_id))) |
| 409 | return -EFAULT; |
Dmitry Torokhov | 08791e5 | 2006-04-29 01:13:21 -0400 | [diff] [blame] | 410 | return 0; |
| 411 | |
| 412 | case EVIOCGREP: |
| 413 | if (!test_bit(EV_REP, dev->evbit)) |
| 414 | return -ENOSYS; |
| 415 | if (put_user(dev->rep[REP_DELAY], ip)) |
| 416 | return -EFAULT; |
| 417 | if (put_user(dev->rep[REP_PERIOD], ip + 1)) |
| 418 | return -EFAULT; |
| 419 | return 0; |
| 420 | |
| 421 | case EVIOCSREP: |
| 422 | if (!test_bit(EV_REP, dev->evbit)) |
| 423 | return -ENOSYS; |
| 424 | if (get_user(u, ip)) |
| 425 | return -EFAULT; |
| 426 | if (get_user(v, ip + 1)) |
| 427 | return -EFAULT; |
| 428 | |
Dmitry Torokhov | 0e739d2 | 2006-07-06 00:22:43 -0400 | [diff] [blame] | 429 | input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u); |
| 430 | input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v); |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 431 | |
| 432 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
| 434 | case EVIOCGKEYCODE: |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 435 | if (get_user(t, ip)) |
| 436 | return -EFAULT; |
Marvin Raaijmakers | c8e4c77 | 2007-03-14 22:50:42 -0400 | [diff] [blame] | 437 | |
| 438 | error = dev->getkeycode(dev, t, &v); |
| 439 | if (error) |
| 440 | return error; |
| 441 | |
| 442 | if (put_user(v, ip + 1)) |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 443 | return -EFAULT; |
Marvin Raaijmakers | c8e4c77 | 2007-03-14 22:50:42 -0400 | [diff] [blame] | 444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | return 0; |
| 446 | |
| 447 | case EVIOCSKEYCODE: |
Marvin Raaijmakers | c8e4c77 | 2007-03-14 22:50:42 -0400 | [diff] [blame] | 448 | if (get_user(t, ip) || get_user(v, ip + 1)) |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 449 | return -EFAULT; |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 450 | |
Marvin Raaijmakers | c8e4c77 | 2007-03-14 22:50:42 -0400 | [diff] [blame] | 451 | return dev->setkeycode(dev, t, v); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
| 453 | case EVIOCSFF: |
Anssi Hannula | 509ca1a | 2006-07-19 01:40:22 -0400 | [diff] [blame] | 454 | if (copy_from_user(&effect, p, sizeof(effect))) |
| 455 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | |
Anssi Hannula | 509ca1a | 2006-07-19 01:40:22 -0400 | [diff] [blame] | 457 | error = input_ff_upload(dev, &effect, file); |
| 458 | |
| 459 | if (put_user(effect.id, &(((struct ff_effect __user *)p)->id))) |
| 460 | return -EFAULT; |
| 461 | |
| 462 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
| 464 | case EVIOCRMFF: |
Anssi Hannula | 509ca1a | 2006-07-19 01:40:22 -0400 | [diff] [blame] | 465 | return input_ff_erase(dev, (int)(unsigned long) p, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
| 467 | case EVIOCGEFFECTS: |
Anssi Hannula | 509ca1a | 2006-07-19 01:40:22 -0400 | [diff] [blame] | 468 | i = test_bit(EV_FF, dev->evbit) ? dev->ff->max_effects : 0; |
| 469 | if (put_user(i, ip)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | return -EFAULT; |
| 471 | return 0; |
| 472 | |
| 473 | case EVIOCGRAB: |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 474 | if (p) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | if (evdev->grab) |
| 476 | return -EBUSY; |
| 477 | if (input_grab_device(&evdev->handle)) |
| 478 | return -EBUSY; |
| 479 | evdev->grab = list; |
| 480 | return 0; |
| 481 | } else { |
| 482 | if (evdev->grab != list) |
| 483 | return -EINVAL; |
| 484 | input_release_device(&evdev->handle); |
| 485 | evdev->grab = NULL; |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | default: |
| 490 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 491 | if (_IOC_TYPE(cmd) != 'E') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | return -EINVAL; |
| 493 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 494 | if (_IOC_DIR(cmd) == _IOC_READ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 496 | if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 498 | long *bits; |
| 499 | int len; |
| 500 | |
| 501 | switch (_IOC_NR(cmd) & EV_MAX) { |
| 502 | case 0: bits = dev->evbit; len = EV_MAX; break; |
| 503 | case EV_KEY: bits = dev->keybit; len = KEY_MAX; break; |
| 504 | case EV_REL: bits = dev->relbit; len = REL_MAX; break; |
| 505 | case EV_ABS: bits = dev->absbit; len = ABS_MAX; break; |
| 506 | case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break; |
| 507 | case EV_LED: bits = dev->ledbit; len = LED_MAX; break; |
| 508 | case EV_SND: bits = dev->sndbit; len = SND_MAX; break; |
| 509 | case EV_FF: bits = dev->ffbit; len = FF_MAX; break; |
Richard Purdie | 3158106 | 2005-09-06 15:19:06 -0700 | [diff] [blame] | 510 | case EV_SW: bits = dev->swbit; len = SW_MAX; break; |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 511 | default: return -EINVAL; |
| 512 | } |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 513 | return bits_to_user(bits, len, _IOC_SIZE(cmd), p, compat_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 515 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 516 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0))) |
| 517 | return bits_to_user(dev->key, KEY_MAX, _IOC_SIZE(cmd), |
| 518 | p, compat_mode); |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 519 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 520 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0))) |
| 521 | return bits_to_user(dev->led, LED_MAX, _IOC_SIZE(cmd), |
| 522 | p, compat_mode); |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 523 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 524 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0))) |
| 525 | return bits_to_user(dev->snd, SND_MAX, _IOC_SIZE(cmd), |
| 526 | p, compat_mode); |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 527 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 528 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSW(0))) |
| 529 | return bits_to_user(dev->sw, SW_MAX, _IOC_SIZE(cmd), |
| 530 | p, compat_mode); |
Richard Purdie | 3158106 | 2005-09-06 15:19:06 -0700 | [diff] [blame] | 531 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 532 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) |
| 533 | return str_to_user(dev->name, _IOC_SIZE(cmd), p); |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 534 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 535 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) |
| 536 | return str_to_user(dev->phys, _IOC_SIZE(cmd), p); |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 537 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 538 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) |
| 539 | return str_to_user(dev->uniq, _IOC_SIZE(cmd), p); |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 540 | |
| 541 | if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) { |
| 542 | |
| 543 | int t = _IOC_NR(cmd) & ABS_MAX; |
| 544 | |
| 545 | abs.value = dev->abs[t]; |
| 546 | abs.minimum = dev->absmin[t]; |
| 547 | abs.maximum = dev->absmax[t]; |
| 548 | abs.fuzz = dev->absfuzz[t]; |
| 549 | abs.flat = dev->absflat[t]; |
| 550 | |
| 551 | if (copy_to_user(p, &abs, sizeof(struct input_absinfo))) |
| 552 | return -EFAULT; |
| 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 559 | if (_IOC_DIR(cmd) == _IOC_WRITE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 561 | if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 563 | int t = _IOC_NR(cmd) & ABS_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 565 | if (copy_from_user(&abs, p, sizeof(struct input_absinfo))) |
| 566 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 568 | dev->abs[t] = abs.value; |
| 569 | dev->absmin[t] = abs.minimum; |
| 570 | dev->absmax[t] = abs.maximum; |
| 571 | dev->absfuzz[t] = abs.fuzz; |
| 572 | dev->absflat[t] = abs.flat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 574 | return 0; |
| 575 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | return -EINVAL; |
| 579 | } |
| 580 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 581 | static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 582 | { |
| 583 | return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0); |
| 584 | } |
| 585 | |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 586 | #ifdef CONFIG_COMPAT |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 587 | static long evdev_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg) |
| 588 | { |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 589 | return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1); |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 590 | } |
| 591 | #endif |
| 592 | |
Dmitry Torokhov | 66e6611 | 2006-09-14 01:31:59 -0400 | [diff] [blame] | 593 | static const struct file_operations evdev_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | .owner = THIS_MODULE, |
| 595 | .read = evdev_read, |
| 596 | .write = evdev_write, |
| 597 | .poll = evdev_poll, |
| 598 | .open = evdev_open, |
| 599 | .release = evdev_release, |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 600 | .unlocked_ioctl = evdev_ioctl, |
| 601 | #ifdef CONFIG_COMPAT |
| 602 | .compat_ioctl = evdev_ioctl_compat, |
| 603 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | .fasync = evdev_fasync, |
| 605 | .flush = evdev_flush |
| 606 | }; |
| 607 | |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame^] | 608 | static int evdev_connect(struct input_handler *handler, struct input_dev *dev, |
| 609 | const struct input_device_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | { |
| 611 | struct evdev *evdev; |
Greg Kroah-Hartman | c9bcd58 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 612 | struct class_device *cdev; |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame^] | 613 | dev_t devt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | int minor; |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame^] | 615 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | |
| 617 | for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++); |
| 618 | if (minor == EVDEV_MINORS) { |
| 619 | printk(KERN_ERR "evdev: no more free evdev devices\n"); |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame^] | 620 | return -ENFILE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame^] | 623 | evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL); |
| 624 | if (!evdev) |
| 625 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | |
| 627 | INIT_LIST_HEAD(&evdev->list); |
| 628 | init_waitqueue_head(&evdev->wait); |
| 629 | |
| 630 | evdev->exist = 1; |
| 631 | evdev->minor = minor; |
| 632 | evdev->handle.dev = dev; |
| 633 | evdev->handle.name = evdev->name; |
| 634 | evdev->handle.handler = handler; |
| 635 | evdev->handle.private = evdev; |
| 636 | sprintf(evdev->name, "event%d", minor); |
| 637 | |
| 638 | evdev_table[minor] = evdev; |
| 639 | |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame^] | 640 | devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor), |
| 641 | |
| 642 | cdev = class_device_create(&input_class, &dev->cdev, devt, |
| 643 | dev->cdev.dev, evdev->name); |
| 644 | if (IS_ERR(cdev)) { |
| 645 | error = PTR_ERR(cdev); |
| 646 | goto err_free_evdev; |
| 647 | } |
Greg Kroah-Hartman | c9bcd58 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 648 | |
| 649 | /* temporary symlink to keep userspace happy */ |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame^] | 650 | error = sysfs_create_link(&input_class.subsys.kset.kobj, |
| 651 | &cdev->kobj, evdev->name); |
| 652 | if (error) |
| 653 | goto err_cdev_destroy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame^] | 655 | error = input_register_handle(&evdev->handle); |
| 656 | if (error) |
| 657 | goto err_remove_link; |
| 658 | |
| 659 | return 0; |
| 660 | |
| 661 | err_remove_link: |
| 662 | sysfs_remove_link(&input_class.subsys.kset.kobj, evdev->name); |
| 663 | err_cdev_destroy: |
| 664 | class_device_destroy(&input_class, devt); |
| 665 | err_free_evdev: |
| 666 | kfree(evdev); |
| 667 | evdev_table[minor] = NULL; |
| 668 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | static void evdev_disconnect(struct input_handle *handle) |
| 672 | { |
| 673 | struct evdev *evdev = handle->private; |
| 674 | struct evdev_list *list; |
| 675 | |
Dmitry Torokhov | 5b2a0826 | 2007-04-12 01:29:46 -0400 | [diff] [blame^] | 676 | input_unregister_handle(handle); |
| 677 | |
Greg Kroah-Hartman | c9bcd58 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 678 | sysfs_remove_link(&input_class.subsys.kset.kobj, evdev->name); |
Greg Kroah-Hartman | ea9f240 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 679 | class_device_destroy(&input_class, |
gregkh@suse.de | 1235686 | 2005-03-15 14:26:30 -0800 | [diff] [blame] | 680 | MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | evdev->exist = 0; |
| 682 | |
| 683 | if (evdev->open) { |
Anssi Hannula | 509ca1a | 2006-07-19 01:40:22 -0400 | [diff] [blame] | 684 | input_flush_device(handle, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | input_close_device(handle); |
| 686 | wake_up_interruptible(&evdev->wait); |
| 687 | list_for_each_entry(list, &evdev->list, node) |
| 688 | kill_fasync(&list->fasync, SIGIO, POLL_HUP); |
| 689 | } else |
| 690 | evdev_free(evdev); |
| 691 | } |
| 692 | |
Dmitry Torokhov | 66e6611 | 2006-09-14 01:31:59 -0400 | [diff] [blame] | 693 | static const struct input_device_id evdev_ids[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | { .driver_info = 1 }, /* Matches all devices */ |
| 695 | { }, /* Terminating zero entry */ |
| 696 | }; |
| 697 | |
| 698 | MODULE_DEVICE_TABLE(input, evdev_ids); |
| 699 | |
| 700 | static struct input_handler evdev_handler = { |
| 701 | .event = evdev_event, |
| 702 | .connect = evdev_connect, |
| 703 | .disconnect = evdev_disconnect, |
| 704 | .fops = &evdev_fops, |
| 705 | .minor = EVDEV_MINOR_BASE, |
| 706 | .name = "evdev", |
| 707 | .id_table = evdev_ids, |
| 708 | }; |
| 709 | |
| 710 | static int __init evdev_init(void) |
| 711 | { |
Dmitry Torokhov | 4263cf0 | 2006-09-14 01:32:39 -0400 | [diff] [blame] | 712 | return input_register_handler(&evdev_handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | static void __exit evdev_exit(void) |
| 716 | { |
| 717 | input_unregister_handler(&evdev_handler); |
| 718 | } |
| 719 | |
| 720 | module_init(evdev_init); |
| 721 | module_exit(evdev_exit); |
| 722 | |
| 723 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 724 | MODULE_DESCRIPTION("Input driver event char devices"); |
| 725 | MODULE_LICENSE("GPL"); |