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; |
| 81 | retval = fasync_helper(fd, file, on, &list->fasync); |
| 82 | return retval < 0 ? retval : 0; |
| 83 | } |
| 84 | |
| 85 | static int evdev_flush(struct file * file) |
| 86 | { |
| 87 | struct evdev_list *list = file->private_data; |
| 88 | if (!list->evdev->exist) return -ENODEV; |
| 89 | return input_flush_device(&list->evdev->handle, file); |
| 90 | } |
| 91 | |
| 92 | static void evdev_free(struct evdev *evdev) |
| 93 | { |
| 94 | evdev_table[evdev->minor] = NULL; |
| 95 | kfree(evdev); |
| 96 | } |
| 97 | |
| 98 | static int evdev_release(struct inode * inode, struct file * file) |
| 99 | { |
| 100 | struct evdev_list *list = file->private_data; |
| 101 | |
| 102 | if (list->evdev->grab == list) { |
| 103 | input_release_device(&list->evdev->handle); |
| 104 | list->evdev->grab = NULL; |
| 105 | } |
| 106 | |
| 107 | evdev_fasync(-1, file, 0); |
| 108 | list_del(&list->node); |
| 109 | |
| 110 | if (!--list->evdev->open) { |
| 111 | if (list->evdev->exist) |
| 112 | input_close_device(&list->evdev->handle); |
| 113 | else |
| 114 | evdev_free(list->evdev); |
| 115 | } |
| 116 | |
| 117 | kfree(list); |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static int evdev_open(struct inode * inode, struct file * file) |
| 122 | { |
| 123 | struct evdev_list *list; |
| 124 | int i = iminor(inode) - EVDEV_MINOR_BASE; |
| 125 | int accept_err; |
| 126 | |
| 127 | if (i >= EVDEV_MINORS || !evdev_table[i] || !evdev_table[i]->exist) |
| 128 | return -ENODEV; |
| 129 | |
| 130 | if ((accept_err = input_accept_process(&(evdev_table[i]->handle), file))) |
| 131 | return accept_err; |
| 132 | |
| 133 | if (!(list = kmalloc(sizeof(struct evdev_list), GFP_KERNEL))) |
| 134 | return -ENOMEM; |
| 135 | memset(list, 0, sizeof(struct evdev_list)); |
| 136 | |
| 137 | list->evdev = evdev_table[i]; |
| 138 | list_add_tail(&list->node, &evdev_table[i]->list); |
| 139 | file->private_data = list; |
| 140 | |
| 141 | if (!list->evdev->open++) |
| 142 | if (list->evdev->exist) |
| 143 | input_open_device(&list->evdev->handle); |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
Juergen 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 | |
| 157 | #ifdef CONFIG_X86_64 |
| 158 | # define COMPAT_TEST test_thread_flag(TIF_IA32) |
| 159 | #elif defined(CONFIG_IA64) |
| 160 | # define COMPAT_TEST IS_IA32_PROCESS(ia64_task_regs(current)) |
Martin Schwidefsky | 347a8dc | 2006-01-06 00:19:28 -0800 | [diff] [blame] | 161 | #elif defined(CONFIG_S390) |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 162 | # define COMPAT_TEST test_thread_flag(TIF_31BIT) |
Ralf Baechle | 59df6bb | 2005-09-03 15:56:23 -0700 | [diff] [blame] | 163 | #elif defined(CONFIG_MIPS) |
| 164 | # define COMPAT_TEST (current->thread.mflags & MF_32BIT_ADDR) |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 165 | #else |
| 166 | # define COMPAT_TEST test_thread_flag(TIF_32BIT) |
| 167 | #endif |
| 168 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 169 | static inline size_t evdev_event_size(void) |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 170 | { |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 171 | return COMPAT_TEST ? |
| 172 | sizeof(struct input_event_compat) : sizeof(struct input_event); |
| 173 | } |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 174 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 175 | static int evdev_event_from_user(const char __user *buffer, struct input_event *event) |
| 176 | { |
| 177 | if (COMPAT_TEST) { |
| 178 | struct input_event_compat compat_event; |
| 179 | |
| 180 | if (copy_from_user(&compat_event, buffer, sizeof(struct input_event_compat))) |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 181 | return -EFAULT; |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 182 | |
| 183 | event->time.tv_sec = compat_event.time.tv_sec; |
| 184 | event->time.tv_usec = compat_event.time.tv_usec; |
| 185 | event->type = compat_event.type; |
| 186 | event->code = compat_event.code; |
| 187 | event->value = compat_event.value; |
| 188 | |
| 189 | } else { |
| 190 | if (copy_from_user(event, buffer, sizeof(struct input_event))) |
| 191 | return -EFAULT; |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 192 | } |
| 193 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 194 | return 0; |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 195 | } |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 196 | |
| 197 | static int evdev_event_to_user(char __user *buffer, const struct input_event *event) |
| 198 | { |
| 199 | if (COMPAT_TEST) { |
| 200 | struct input_event_compat compat_event; |
| 201 | |
| 202 | compat_event.time.tv_sec = event->time.tv_sec; |
| 203 | compat_event.time.tv_usec = event->time.tv_usec; |
| 204 | compat_event.type = event->type; |
| 205 | compat_event.code = event->code; |
| 206 | compat_event.value = event->value; |
| 207 | |
| 208 | if (copy_to_user(buffer, &compat_event, sizeof(struct input_event_compat))) |
| 209 | return -EFAULT; |
| 210 | |
| 211 | } else { |
| 212 | if (copy_to_user(buffer, event, sizeof(struct input_event))) |
| 213 | return -EFAULT; |
| 214 | } |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | #else |
| 220 | |
| 221 | static inline size_t evdev_event_size(void) |
| 222 | { |
| 223 | return sizeof(struct input_event); |
| 224 | } |
| 225 | |
| 226 | static int evdev_event_from_user(const char __user *buffer, struct input_event *event) |
| 227 | { |
| 228 | if (copy_from_user(event, buffer, sizeof(struct input_event))) |
| 229 | return -EFAULT; |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | static int evdev_event_to_user(char __user *buffer, const struct input_event *event) |
| 235 | { |
| 236 | if (copy_to_user(buffer, event, sizeof(struct input_event))) |
| 237 | return -EFAULT; |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | #endif /* CONFIG_COMPAT */ |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | static ssize_t evdev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos) |
| 245 | { |
| 246 | struct evdev_list *list = file->private_data; |
| 247 | struct input_event event; |
| 248 | int retval = 0; |
| 249 | |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 250 | if (!list->evdev->exist) |
| 251 | return -ENODEV; |
| 252 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 253 | while (retval < count) { |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 254 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 255 | if (evdev_event_from_user(buffer + retval, &event)) |
| 256 | return -EFAULT; |
| 257 | input_event(list->evdev->handle.dev, event.type, event.code, event.value); |
| 258 | retval += evdev_event_size(); |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | return retval; |
| 262 | } |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | static ssize_t evdev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos) |
| 265 | { |
| 266 | struct evdev_list *list = file->private_data; |
| 267 | int retval; |
| 268 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 269 | if (count < evdev_event_size()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | return -EINVAL; |
| 271 | |
| 272 | if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK)) |
| 273 | return -EAGAIN; |
| 274 | |
| 275 | retval = wait_event_interruptible(list->evdev->wait, |
| 276 | list->head != list->tail || (!list->evdev->exist)); |
| 277 | |
| 278 | if (retval) |
| 279 | return retval; |
| 280 | |
| 281 | if (!list->evdev->exist) |
| 282 | return -ENODEV; |
| 283 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 284 | while (list->head != list->tail && retval + evdev_event_size() <= count) { |
| 285 | |
| 286 | struct input_event *event = (struct input_event *) list->buffer + list->tail; |
| 287 | |
| 288 | if (evdev_event_to_user(buffer + retval, event)) |
| 289 | return -EFAULT; |
| 290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1); |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 292 | retval += evdev_event_size(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | return retval; |
| 296 | } |
| 297 | |
| 298 | /* No kernel lock - fine */ |
| 299 | static unsigned int evdev_poll(struct file *file, poll_table *wait) |
| 300 | { |
| 301 | struct evdev_list *list = file->private_data; |
| 302 | poll_wait(file, &list->evdev->wait, wait); |
| 303 | return ((list->head == list->tail) ? 0 : (POLLIN | POLLRDNORM)) | |
| 304 | (list->evdev->exist ? 0 : (POLLHUP | POLLERR)); |
| 305 | } |
| 306 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 307 | #ifdef CONFIG_COMPAT |
| 308 | |
| 309 | #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8) |
| 310 | #define NBITS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1) |
| 311 | |
| 312 | #ifdef __BIG_ENDIAN |
| 313 | static int bits_to_user(unsigned long *bits, unsigned int maxbit, |
| 314 | unsigned int maxlen, void __user *p, int compat) |
| 315 | { |
| 316 | int len, i; |
| 317 | |
| 318 | if (compat) { |
| 319 | len = NBITS_COMPAT(maxbit) * sizeof(compat_long_t); |
| 320 | if (len < maxlen) |
| 321 | len = maxlen; |
| 322 | |
| 323 | for (i = 0; i < len / sizeof(compat_long_t); i++) |
| 324 | if (copy_to_user((compat_long_t __user *) p + i, |
| 325 | (compat_long_t *) bits + |
| 326 | i + 1 - ((i % 2) << 1), |
| 327 | sizeof(compat_long_t))) |
| 328 | return -EFAULT; |
| 329 | } else { |
| 330 | len = NBITS(maxbit) * sizeof(long); |
| 331 | if (len > maxlen) |
| 332 | len = maxlen; |
| 333 | |
| 334 | if (copy_to_user(p, bits, len)) |
| 335 | return -EFAULT; |
| 336 | } |
| 337 | |
| 338 | return len; |
| 339 | } |
| 340 | #else |
| 341 | static int bits_to_user(unsigned long *bits, unsigned int maxbit, |
| 342 | unsigned int maxlen, void __user *p, int compat) |
| 343 | { |
| 344 | int len = compat ? |
| 345 | NBITS_COMPAT(maxbit) * sizeof(compat_long_t) : |
| 346 | NBITS(maxbit) * sizeof(long); |
| 347 | |
| 348 | if (len > maxlen) |
| 349 | len = maxlen; |
| 350 | |
| 351 | return copy_to_user(p, bits, len) ? -EFAULT : len; |
| 352 | } |
| 353 | #endif /* __BIG_ENDIAN */ |
| 354 | |
| 355 | #else |
| 356 | |
| 357 | static int bits_to_user(unsigned long *bits, unsigned int maxbit, |
| 358 | unsigned int maxlen, void __user *p, int compat) |
| 359 | { |
| 360 | int len = NBITS(maxbit) * sizeof(long); |
| 361 | |
| 362 | if (len > maxlen) |
| 363 | len = maxlen; |
| 364 | |
| 365 | return copy_to_user(p, bits, len) ? -EFAULT : len; |
| 366 | } |
| 367 | |
| 368 | #endif /* CONFIG_COMPAT */ |
| 369 | |
| 370 | static int str_to_user(const char *str, unsigned int maxlen, void __user *p) |
| 371 | { |
| 372 | int len; |
| 373 | |
| 374 | if (!str) |
| 375 | return -ENOENT; |
| 376 | |
| 377 | len = strlen(str) + 1; |
| 378 | if (len > maxlen) |
| 379 | len = maxlen; |
| 380 | |
| 381 | return copy_to_user(p, str, len) ? -EFAULT : len; |
| 382 | } |
| 383 | |
| 384 | static long evdev_ioctl_handler(struct file *file, unsigned int cmd, |
| 385 | void __user *p, int compat_mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | { |
| 387 | struct evdev_list *list = file->private_data; |
| 388 | struct evdev *evdev = list->evdev; |
| 389 | struct input_dev *dev = evdev->handle.dev; |
| 390 | struct input_absinfo abs; |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 391 | int __user *ip = (int __user *)p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | int i, t, u, v; |
| 393 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 394 | if (!evdev->exist) |
| 395 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
| 397 | switch (cmd) { |
| 398 | |
| 399 | case EVIOCGVERSION: |
| 400 | return put_user(EV_VERSION, ip); |
| 401 | |
| 402 | case EVIOCGID: |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 403 | if (copy_to_user(p, &dev->id, sizeof(struct input_id))) |
| 404 | return -EFAULT; |
| 405 | |
| 406 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
| 408 | case EVIOCGKEYCODE: |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 409 | if (get_user(t, ip)) |
| 410 | return -EFAULT; |
| 411 | if (t < 0 || t >= dev->keycodemax || !dev->keycodesize) |
| 412 | return -EINVAL; |
| 413 | if (put_user(INPUT_KEYCODE(dev, t), ip + 1)) |
| 414 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | return 0; |
| 416 | |
| 417 | case EVIOCSKEYCODE: |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 418 | if (get_user(t, ip)) |
| 419 | return -EFAULT; |
| 420 | if (t < 0 || t >= dev->keycodemax || !dev->keycodesize) |
| 421 | return -EINVAL; |
| 422 | if (get_user(v, ip + 1)) |
| 423 | return -EFAULT; |
| 424 | if (v < 0 || v > KEY_MAX) |
| 425 | return -EINVAL; |
| 426 | if (dev->keycodesize < sizeof(v) && (v >> (dev->keycodesize * 8))) |
| 427 | return -EINVAL; |
| 428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | u = SET_INPUT_KEYCODE(dev, t, v); |
| 430 | clear_bit(u, dev->keybit); |
| 431 | set_bit(v, dev->keybit); |
| 432 | for (i = 0; i < dev->keycodemax; i++) |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 433 | if (INPUT_KEYCODE(dev, i) == u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | set_bit(u, dev->keybit); |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | return 0; |
| 437 | |
| 438 | case EVIOCSFF: |
| 439 | if (dev->upload_effect) { |
| 440 | struct ff_effect effect; |
| 441 | int err; |
| 442 | |
| 443 | if (copy_from_user(&effect, p, sizeof(effect))) |
| 444 | return -EFAULT; |
| 445 | err = dev->upload_effect(dev, &effect); |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 446 | if (put_user(effect.id, &(((struct ff_effect __user *)p)->id))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | return -EFAULT; |
| 448 | return err; |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 449 | } else |
| 450 | return -ENOSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
| 452 | case EVIOCRMFF: |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 453 | if (!dev->erase_effect) |
| 454 | return -ENOSYS; |
| 455 | |
| 456 | return dev->erase_effect(dev, (int)(unsigned long) p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
| 458 | case EVIOCGEFFECTS: |
| 459 | if (put_user(dev->ff_effects_max, ip)) |
| 460 | return -EFAULT; |
| 461 | return 0; |
| 462 | |
| 463 | case EVIOCGRAB: |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 464 | if (p) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | if (evdev->grab) |
| 466 | return -EBUSY; |
| 467 | if (input_grab_device(&evdev->handle)) |
| 468 | return -EBUSY; |
| 469 | evdev->grab = list; |
| 470 | return 0; |
| 471 | } else { |
| 472 | if (evdev->grab != list) |
| 473 | return -EINVAL; |
| 474 | input_release_device(&evdev->handle); |
| 475 | evdev->grab = NULL; |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | default: |
| 480 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 481 | if (_IOC_TYPE(cmd) != 'E') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | return -EINVAL; |
| 483 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 484 | if (_IOC_DIR(cmd) == _IOC_READ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 486 | if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 488 | long *bits; |
| 489 | int len; |
| 490 | |
| 491 | switch (_IOC_NR(cmd) & EV_MAX) { |
| 492 | case 0: bits = dev->evbit; len = EV_MAX; break; |
| 493 | case EV_KEY: bits = dev->keybit; len = KEY_MAX; break; |
| 494 | case EV_REL: bits = dev->relbit; len = REL_MAX; break; |
| 495 | case EV_ABS: bits = dev->absbit; len = ABS_MAX; break; |
| 496 | case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break; |
| 497 | case EV_LED: bits = dev->ledbit; len = LED_MAX; break; |
| 498 | case EV_SND: bits = dev->sndbit; len = SND_MAX; break; |
| 499 | case EV_FF: bits = dev->ffbit; len = FF_MAX; break; |
Richard Purdie | 3158106 | 2005-09-06 15:19:06 -0700 | [diff] [blame] | 500 | case EV_SW: bits = dev->swbit; len = SW_MAX; break; |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 501 | default: return -EINVAL; |
| 502 | } |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 503 | return bits_to_user(bits, len, _IOC_SIZE(cmd), p, compat_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 505 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 506 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0))) |
| 507 | return bits_to_user(dev->key, KEY_MAX, _IOC_SIZE(cmd), |
| 508 | p, compat_mode); |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 509 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 510 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0))) |
| 511 | return bits_to_user(dev->led, LED_MAX, _IOC_SIZE(cmd), |
| 512 | p, compat_mode); |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 513 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 514 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0))) |
| 515 | return bits_to_user(dev->snd, SND_MAX, _IOC_SIZE(cmd), |
| 516 | p, compat_mode); |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 517 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 518 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSW(0))) |
| 519 | return bits_to_user(dev->sw, SW_MAX, _IOC_SIZE(cmd), |
| 520 | p, compat_mode); |
Richard Purdie | 3158106 | 2005-09-06 15:19:06 -0700 | [diff] [blame] | 521 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 522 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) |
| 523 | return str_to_user(dev->name, _IOC_SIZE(cmd), p); |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 524 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 525 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) |
| 526 | return str_to_user(dev->phys, _IOC_SIZE(cmd), p); |
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(EVIOCGUNIQ(0))) |
| 529 | return str_to_user(dev->uniq, _IOC_SIZE(cmd), p); |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 530 | |
| 531 | if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) { |
| 532 | |
| 533 | int t = _IOC_NR(cmd) & ABS_MAX; |
| 534 | |
| 535 | abs.value = dev->abs[t]; |
| 536 | abs.minimum = dev->absmin[t]; |
| 537 | abs.maximum = dev->absmax[t]; |
| 538 | abs.fuzz = dev->absfuzz[t]; |
| 539 | abs.flat = dev->absflat[t]; |
| 540 | |
| 541 | if (copy_to_user(p, &abs, sizeof(struct input_absinfo))) |
| 542 | return -EFAULT; |
| 543 | |
| 544 | return 0; |
| 545 | } |
| 546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 549 | if (_IOC_DIR(cmd) == _IOC_WRITE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 551 | if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 553 | int t = _IOC_NR(cmd) & ABS_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 555 | if (copy_from_user(&abs, p, sizeof(struct input_absinfo))) |
| 556 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 558 | dev->abs[t] = abs.value; |
| 559 | dev->absmin[t] = abs.minimum; |
| 560 | dev->absmax[t] = abs.maximum; |
| 561 | dev->absfuzz[t] = abs.fuzz; |
| 562 | dev->absflat[t] = abs.flat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
Vojtech Pavlik | 41e979f | 2005-05-29 02:30:15 -0500 | [diff] [blame] | 564 | return 0; |
| 565 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | return -EINVAL; |
| 569 | } |
| 570 | |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 571 | static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 572 | { |
| 573 | return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0); |
| 574 | } |
| 575 | |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 576 | #ifdef CONFIG_COMPAT |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 577 | static long evdev_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg) |
| 578 | { |
Dmitry Torokhov | 3a51f7c | 2005-12-11 12:40:37 -0500 | [diff] [blame] | 579 | return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1); |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 580 | } |
| 581 | #endif |
| 582 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | static struct file_operations evdev_fops = { |
| 584 | .owner = THIS_MODULE, |
| 585 | .read = evdev_read, |
| 586 | .write = evdev_write, |
| 587 | .poll = evdev_poll, |
| 588 | .open = evdev_open, |
| 589 | .release = evdev_release, |
Juergen Kreileder | 52658bb | 2005-05-29 02:26:43 -0500 | [diff] [blame] | 590 | .unlocked_ioctl = evdev_ioctl, |
| 591 | #ifdef CONFIG_COMPAT |
| 592 | .compat_ioctl = evdev_ioctl_compat, |
| 593 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | .fasync = evdev_fasync, |
| 595 | .flush = evdev_flush |
| 596 | }; |
| 597 | |
| 598 | static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id) |
| 599 | { |
| 600 | struct evdev *evdev; |
Greg Kroah-Hartman | c9bcd58 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 601 | struct class_device *cdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | int minor; |
| 603 | |
| 604 | for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++); |
| 605 | if (minor == EVDEV_MINORS) { |
| 606 | printk(KERN_ERR "evdev: no more free evdev devices\n"); |
| 607 | return NULL; |
| 608 | } |
| 609 | |
| 610 | if (!(evdev = kmalloc(sizeof(struct evdev), GFP_KERNEL))) |
| 611 | return NULL; |
| 612 | memset(evdev, 0, sizeof(struct evdev)); |
| 613 | |
| 614 | INIT_LIST_HEAD(&evdev->list); |
| 615 | init_waitqueue_head(&evdev->wait); |
| 616 | |
| 617 | evdev->exist = 1; |
| 618 | evdev->minor = minor; |
| 619 | evdev->handle.dev = dev; |
| 620 | evdev->handle.name = evdev->name; |
| 621 | evdev->handle.handler = handler; |
| 622 | evdev->handle.private = evdev; |
| 623 | sprintf(evdev->name, "event%d", minor); |
| 624 | |
| 625 | evdev_table[minor] = evdev; |
| 626 | |
Greg Kroah-Hartman | c9bcd58 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 627 | cdev = class_device_create(&input_class, &dev->cdev, |
gregkh@suse.de | 1235686 | 2005-03-15 14:26:30 -0800 | [diff] [blame] | 628 | MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor), |
Greg Kroah-Hartman | c9bcd58 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 629 | dev->cdev.dev, evdev->name); |
| 630 | |
| 631 | /* temporary symlink to keep userspace happy */ |
| 632 | sysfs_create_link(&input_class.subsys.kset.kobj, &cdev->kobj, |
| 633 | evdev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
| 635 | return &evdev->handle; |
| 636 | } |
| 637 | |
| 638 | static void evdev_disconnect(struct input_handle *handle) |
| 639 | { |
| 640 | struct evdev *evdev = handle->private; |
| 641 | struct evdev_list *list; |
| 642 | |
Greg Kroah-Hartman | c9bcd58 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 643 | sysfs_remove_link(&input_class.subsys.kset.kobj, evdev->name); |
Greg Kroah-Hartman | ea9f240 | 2005-10-27 22:25:43 -0700 | [diff] [blame] | 644 | class_device_destroy(&input_class, |
gregkh@suse.de | 1235686 | 2005-03-15 14:26:30 -0800 | [diff] [blame] | 645 | MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | evdev->exist = 0; |
| 647 | |
| 648 | if (evdev->open) { |
| 649 | input_close_device(handle); |
| 650 | wake_up_interruptible(&evdev->wait); |
| 651 | list_for_each_entry(list, &evdev->list, node) |
| 652 | kill_fasync(&list->fasync, SIGIO, POLL_HUP); |
| 653 | } else |
| 654 | evdev_free(evdev); |
| 655 | } |
| 656 | |
| 657 | static struct input_device_id evdev_ids[] = { |
| 658 | { .driver_info = 1 }, /* Matches all devices */ |
| 659 | { }, /* Terminating zero entry */ |
| 660 | }; |
| 661 | |
| 662 | MODULE_DEVICE_TABLE(input, evdev_ids); |
| 663 | |
| 664 | static struct input_handler evdev_handler = { |
| 665 | .event = evdev_event, |
| 666 | .connect = evdev_connect, |
| 667 | .disconnect = evdev_disconnect, |
| 668 | .fops = &evdev_fops, |
| 669 | .minor = EVDEV_MINOR_BASE, |
| 670 | .name = "evdev", |
| 671 | .id_table = evdev_ids, |
| 672 | }; |
| 673 | |
| 674 | static int __init evdev_init(void) |
| 675 | { |
| 676 | input_register_handler(&evdev_handler); |
| 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | static void __exit evdev_exit(void) |
| 681 | { |
| 682 | input_unregister_handler(&evdev_handler); |
| 683 | } |
| 684 | |
| 685 | module_init(evdev_init); |
| 686 | module_exit(evdev_exit); |
| 687 | |
| 688 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 689 | MODULE_DESCRIPTION("Input driver event char devices"); |
| 690 | MODULE_LICENSE("GPL"); |