| 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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 12 | |
| 13 | #define EVDEV_MINOR_BASE 64 |
| 14 | #define EVDEV_MINORS 32 |
| 15 | #define EVDEV_MIN_BUFFER_SIZE 64U |
| 16 | #define EVDEV_BUF_PACKETS 8 |
| 17 | |
| 18 | #include <linux/poll.h> |
| 19 | #include <linux/sched.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/vmalloc.h> |
| 22 | #include <linux/mm.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/input/mt.h> |
| 26 | #include <linux/major.h> |
| 27 | #include <linux/device.h> |
| 28 | #include <linux/cdev.h> |
| 29 | #include <linux/wakelock.h> |
| 30 | #include "input-compat.h" |
| 31 | |
| 32 | struct evdev { |
| 33 | int open; |
| 34 | struct input_handle handle; |
| 35 | wait_queue_head_t wait; |
| 36 | struct evdev_client __rcu *grab; |
| 37 | struct list_head client_list; |
| 38 | spinlock_t client_lock; /* protects client_list */ |
| 39 | struct mutex mutex; |
| 40 | struct device dev; |
| 41 | struct cdev cdev; |
| 42 | bool exist; |
| 43 | }; |
| 44 | |
| 45 | struct evdev_client { |
| 46 | unsigned int head; |
| 47 | unsigned int tail; |
| 48 | unsigned int packet_head; /* [future] position of the first element of next packet */ |
| 49 | spinlock_t buffer_lock; /* protects access to buffer, head and tail */ |
| 50 | struct wake_lock wake_lock; |
| 51 | bool use_wake_lock; |
| 52 | char name[28]; |
| 53 | struct fasync_struct *fasync; |
| 54 | struct evdev *evdev; |
| 55 | struct list_head node; |
| 56 | int clkid; |
| 57 | unsigned int bufsize; |
| 58 | struct input_event buffer[]; |
| 59 | }; |
| 60 | |
| 61 | static void __pass_event(struct evdev_client *client, |
| 62 | const struct input_event *event) |
| 63 | { |
| 64 | client->buffer[client->head++] = *event; |
| 65 | client->head &= client->bufsize - 1; |
| 66 | |
| 67 | if (unlikely(client->head == client->tail)) { |
| 68 | /* |
| 69 | * This effectively "drops" all unconsumed events, leaving |
| 70 | * EV_SYN/SYN_DROPPED plus the newest event in the queue. |
| 71 | */ |
| 72 | client->tail = (client->head - 2) & (client->bufsize - 1); |
| 73 | |
| 74 | client->buffer[client->tail].time = event->time; |
| 75 | client->buffer[client->tail].type = EV_SYN; |
| 76 | client->buffer[client->tail].code = SYN_DROPPED; |
| 77 | client->buffer[client->tail].value = 0; |
| 78 | |
| 79 | client->packet_head = client->tail; |
| 80 | if (client->use_wake_lock) |
| 81 | wake_unlock(&client->wake_lock); |
| 82 | } |
| 83 | |
| 84 | if (event->type == EV_SYN && event->code == SYN_REPORT) { |
| 85 | client->packet_head = client->head; |
| 86 | if (client->use_wake_lock) |
| 87 | wake_lock(&client->wake_lock); |
| 88 | kill_fasync(&client->fasync, SIGIO, POLL_IN); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** M: MET driver milestone. @{ */ |
| 93 | #define MET_TOUCH |
| 94 | #ifdef MET_TOUCH |
| 95 | #define CREATE_TRACE_POINTS |
| 96 | #include <linux/met_ftrace_touch.h> |
| 97 | |
| 98 | noinline void MET_touch(struct input_event* event) |
| 99 | { |
| 100 | switch (event->type) { |
| 101 | case EV_ABS: |
| 102 | if (ABS_MT_POSITION_X == event->code) { |
| 103 | trace_MET_touch("EV_ABS", event->time.tv_sec, event->time.tv_usec,"X", event->value); |
| 104 | } else if (ABS_MT_POSITION_Y == event->code) { |
| 105 | trace_MET_touch("EV_ABS", event->time.tv_sec, event->time.tv_usec,"Y", event->value); |
| 106 | } |
| 107 | break; |
| 108 | case EV_KEY: |
| 109 | if (BTN_TOUCH == event->code) { |
| 110 | trace_MET_touch("EV_KEY", event->time.tv_sec, event->time.tv_usec,"BTN_TOUCH", event->value); |
| 111 | } |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | #endif |
| 116 | /** @} */ |
| 117 | |
| 118 | static void evdev_pass_values(struct evdev_client *client, |
| 119 | const struct input_value *vals, unsigned int count, |
| 120 | ktime_t mono, ktime_t real) |
| 121 | { |
| 122 | struct evdev *evdev = client->evdev; |
| 123 | const struct input_value *v; |
| 124 | struct input_event event; |
| 125 | bool wakeup = false; |
| 126 | |
| 127 | event.time = ktime_to_timeval(client->clkid == CLOCK_MONOTONIC ? |
| 128 | mono : real); |
| 129 | |
| 130 | /* Interrupts are disabled, just acquire the lock. */ |
| 131 | spin_lock(&client->buffer_lock); |
| 132 | |
| 133 | for (v = vals; v != vals + count; v++) { |
| 134 | event.type = v->type; |
| 135 | event.code = v->code; |
| 136 | event.value = v->value; |
| 137 | __pass_event(client, &event); |
| 138 | /** M: MET driver milestone. @{ */ |
| 139 | #ifdef MET_TOUCH |
| 140 | MET_touch(&event); |
| 141 | #endif |
| 142 | /** @} */ |
| 143 | if (v->type == EV_SYN && v->code == SYN_REPORT) |
| 144 | wakeup = true; |
| 145 | } |
| 146 | |
| 147 | spin_unlock(&client->buffer_lock); |
| 148 | |
| 149 | if (wakeup) |
| 150 | wake_up_interruptible(&evdev->wait); |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Pass incoming events to all connected clients. |
| 155 | */ |
| 156 | static void evdev_events(struct input_handle *handle, |
| 157 | const struct input_value *vals, unsigned int count) |
| 158 | { |
| 159 | struct evdev *evdev = handle->private; |
| 160 | struct evdev_client *client; |
| 161 | ktime_t time_mono, time_real; |
| 162 | |
| 163 | time_mono = ktime_get(); |
| 164 | time_real = ktime_sub(time_mono, ktime_get_monotonic_offset()); |
| 165 | |
| 166 | rcu_read_lock(); |
| 167 | |
| 168 | client = rcu_dereference(evdev->grab); |
| 169 | |
| 170 | if (client) |
| 171 | evdev_pass_values(client, vals, count, time_mono, time_real); |
| 172 | else |
| 173 | list_for_each_entry_rcu(client, &evdev->client_list, node) |
| 174 | evdev_pass_values(client, vals, count, |
| 175 | time_mono, time_real); |
| 176 | |
| 177 | rcu_read_unlock(); |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Pass incoming event to all connected clients. |
| 182 | */ |
| 183 | static void evdev_event(struct input_handle *handle, |
| 184 | unsigned int type, unsigned int code, int value) |
| 185 | { |
| 186 | struct input_value vals[] = { { type, code, value } }; |
| 187 | |
| 188 | evdev_events(handle, vals, 1); |
| 189 | } |
| 190 | |
| 191 | static int evdev_fasync(int fd, struct file *file, int on) |
| 192 | { |
| 193 | struct evdev_client *client = file->private_data; |
| 194 | |
| 195 | return fasync_helper(fd, file, on, &client->fasync); |
| 196 | } |
| 197 | |
| 198 | static int evdev_flush(struct file *file, fl_owner_t id) |
| 199 | { |
| 200 | struct evdev_client *client = file->private_data; |
| 201 | struct evdev *evdev = client->evdev; |
| 202 | int retval; |
| 203 | |
| 204 | retval = mutex_lock_interruptible(&evdev->mutex); |
| 205 | if (retval) |
| 206 | return retval; |
| 207 | |
| 208 | if (!evdev->exist) |
| 209 | retval = -ENODEV; |
| 210 | else |
| 211 | retval = input_flush_device(&evdev->handle, file); |
| 212 | |
| 213 | mutex_unlock(&evdev->mutex); |
| 214 | return retval; |
| 215 | } |
| 216 | |
| 217 | static void evdev_free(struct device *dev) |
| 218 | { |
| 219 | struct evdev *evdev = container_of(dev, struct evdev, dev); |
| 220 | |
| 221 | input_put_device(evdev->handle.dev); |
| 222 | kfree(evdev); |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * Grabs an event device (along with underlying input device). |
| 227 | * This function is called with evdev->mutex taken. |
| 228 | */ |
| 229 | static int evdev_grab(struct evdev *evdev, struct evdev_client *client) |
| 230 | { |
| 231 | int error; |
| 232 | |
| 233 | if (evdev->grab) |
| 234 | return -EBUSY; |
| 235 | |
| 236 | error = input_grab_device(&evdev->handle); |
| 237 | if (error) |
| 238 | return error; |
| 239 | |
| 240 | rcu_assign_pointer(evdev->grab, client); |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client) |
| 246 | { |
| 247 | struct evdev_client *grab = rcu_dereference_protected(evdev->grab, |
| 248 | lockdep_is_held(&evdev->mutex)); |
| 249 | |
| 250 | if (grab != client) |
| 251 | return -EINVAL; |
| 252 | |
| 253 | rcu_assign_pointer(evdev->grab, NULL); |
| 254 | synchronize_rcu(); |
| 255 | input_release_device(&evdev->handle); |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static void evdev_attach_client(struct evdev *evdev, |
| 261 | struct evdev_client *client) |
| 262 | { |
| 263 | spin_lock(&evdev->client_lock); |
| 264 | list_add_tail_rcu(&client->node, &evdev->client_list); |
| 265 | spin_unlock(&evdev->client_lock); |
| 266 | } |
| 267 | |
| 268 | static void evdev_detach_client(struct evdev *evdev, |
| 269 | struct evdev_client *client) |
| 270 | { |
| 271 | spin_lock(&evdev->client_lock); |
| 272 | list_del_rcu(&client->node); |
| 273 | spin_unlock(&evdev->client_lock); |
| 274 | synchronize_rcu(); |
| 275 | } |
| 276 | |
| 277 | static int evdev_open_device(struct evdev *evdev) |
| 278 | { |
| 279 | int retval; |
| 280 | |
| 281 | retval = mutex_lock_interruptible(&evdev->mutex); |
| 282 | if (retval) |
| 283 | return retval; |
| 284 | |
| 285 | if (!evdev->exist) |
| 286 | retval = -ENODEV; |
| 287 | else if (!evdev->open++) { |
| 288 | retval = input_open_device(&evdev->handle); |
| 289 | if (retval) |
| 290 | evdev->open--; |
| 291 | } |
| 292 | |
| 293 | mutex_unlock(&evdev->mutex); |
| 294 | return retval; |
| 295 | } |
| 296 | |
| 297 | static void evdev_close_device(struct evdev *evdev) |
| 298 | { |
| 299 | mutex_lock(&evdev->mutex); |
| 300 | |
| 301 | if (evdev->exist && !--evdev->open) |
| 302 | input_close_device(&evdev->handle); |
| 303 | |
| 304 | mutex_unlock(&evdev->mutex); |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * Wake up users waiting for IO so they can disconnect from |
| 309 | * dead device. |
| 310 | */ |
| 311 | static void evdev_hangup(struct evdev *evdev) |
| 312 | { |
| 313 | struct evdev_client *client; |
| 314 | |
| 315 | spin_lock(&evdev->client_lock); |
| 316 | list_for_each_entry(client, &evdev->client_list, node) |
| 317 | kill_fasync(&client->fasync, SIGIO, POLL_HUP); |
| 318 | spin_unlock(&evdev->client_lock); |
| 319 | |
| 320 | wake_up_interruptible(&evdev->wait); |
| 321 | } |
| 322 | |
| 323 | static int evdev_release(struct inode *inode, struct file *file) |
| 324 | { |
| 325 | struct evdev_client *client = file->private_data; |
| 326 | struct evdev *evdev = client->evdev; |
| 327 | |
| 328 | mutex_lock(&evdev->mutex); |
| 329 | evdev_ungrab(evdev, client); |
| 330 | mutex_unlock(&evdev->mutex); |
| 331 | |
| 332 | evdev_detach_client(evdev, client); |
| 333 | if (client->use_wake_lock) |
| 334 | wake_lock_destroy(&client->wake_lock); |
| 335 | |
| 336 | if (is_vmalloc_addr(client)) |
| 337 | vfree(client); |
| 338 | else |
| 339 | kfree(client); |
| 340 | |
| 341 | evdev_close_device(evdev); |
| 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | static unsigned int evdev_compute_buffer_size(struct input_dev *dev) |
| 347 | { |
| 348 | unsigned int n_events = |
| 349 | max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS, |
| 350 | EVDEV_MIN_BUFFER_SIZE); |
| 351 | |
| 352 | return roundup_pow_of_two(n_events); |
| 353 | } |
| 354 | |
| 355 | static int evdev_open(struct inode *inode, struct file *file) |
| 356 | { |
| 357 | struct evdev *evdev = container_of(inode->i_cdev, struct evdev, cdev); |
| 358 | unsigned int bufsize = evdev_compute_buffer_size(evdev->handle.dev); |
| 359 | unsigned int size = sizeof(struct evdev_client) + |
| 360 | bufsize * sizeof(struct input_event); |
| 361 | struct evdev_client *client; |
| 362 | int error; |
| 363 | |
| 364 | client = kzalloc(size, GFP_KERNEL | __GFP_NOWARN); |
| 365 | if (!client) |
| 366 | client = vzalloc(size); |
| 367 | if (!client) |
| 368 | return -ENOMEM; |
| 369 | |
| 370 | client->bufsize = bufsize; |
| 371 | spin_lock_init(&client->buffer_lock); |
| 372 | snprintf(client->name, sizeof(client->name), "%s-%d", |
| 373 | dev_name(&evdev->dev), task_tgid_vnr(current)); |
| 374 | client->evdev = evdev; |
| 375 | evdev_attach_client(evdev, client); |
| 376 | |
| 377 | error = evdev_open_device(evdev); |
| 378 | if (error) |
| 379 | goto err_free_client; |
| 380 | |
| 381 | file->private_data = client; |
| 382 | nonseekable_open(inode, file); |
| 383 | |
| 384 | return 0; |
| 385 | |
| 386 | err_free_client: |
| 387 | evdev_detach_client(evdev, client); |
| 388 | kfree(client); |
| 389 | return error; |
| 390 | } |
| 391 | |
| 392 | static ssize_t evdev_write(struct file *file, const char __user *buffer, |
| 393 | size_t count, loff_t *ppos) |
| 394 | { |
| 395 | struct evdev_client *client = file->private_data; |
| 396 | struct evdev *evdev = client->evdev; |
| 397 | struct input_event event; |
| 398 | int retval = 0; |
| 399 | |
| 400 | if (count != 0 && count < input_event_size()) |
| 401 | return -EINVAL; |
| 402 | |
| 403 | retval = mutex_lock_interruptible(&evdev->mutex); |
| 404 | if (retval) |
| 405 | return retval; |
| 406 | |
| 407 | if (!evdev->exist) { |
| 408 | retval = -ENODEV; |
| 409 | goto out; |
| 410 | } |
| 411 | |
| 412 | while (retval + input_event_size() <= count) { |
| 413 | |
| 414 | if (input_event_from_user(buffer + retval, &event)) { |
| 415 | retval = -EFAULT; |
| 416 | goto out; |
| 417 | } |
| 418 | retval += input_event_size(); |
| 419 | |
| 420 | input_inject_event(&evdev->handle, |
| 421 | event.type, event.code, event.value); |
| 422 | } |
| 423 | |
| 424 | out: |
| 425 | mutex_unlock(&evdev->mutex); |
| 426 | return retval; |
| 427 | } |
| 428 | |
| 429 | static int evdev_fetch_next_event(struct evdev_client *client, |
| 430 | struct input_event *event) |
| 431 | { |
| 432 | int have_event; |
| 433 | |
| 434 | spin_lock_irq(&client->buffer_lock); |
| 435 | |
| 436 | have_event = client->packet_head != client->tail; |
| 437 | if (have_event) { |
| 438 | *event = client->buffer[client->tail++]; |
| 439 | client->tail &= client->bufsize - 1; |
| 440 | if (client->use_wake_lock && |
| 441 | client->packet_head == client->tail) |
| 442 | wake_unlock(&client->wake_lock); |
| 443 | } |
| 444 | |
| 445 | spin_unlock_irq(&client->buffer_lock); |
| 446 | |
| 447 | return have_event; |
| 448 | } |
| 449 | |
| 450 | static ssize_t evdev_read(struct file *file, char __user *buffer, |
| 451 | size_t count, loff_t *ppos) |
| 452 | { |
| 453 | struct evdev_client *client = file->private_data; |
| 454 | struct evdev *evdev = client->evdev; |
| 455 | struct input_event event; |
| 456 | size_t read = 0; |
| 457 | int error; |
| 458 | |
| 459 | if (count != 0 && count < input_event_size()) |
| 460 | return -EINVAL; |
| 461 | |
| 462 | for (;;) { |
| 463 | if (!evdev->exist) |
| 464 | return -ENODEV; |
| 465 | |
| 466 | if (client->packet_head == client->tail && |
| 467 | (file->f_flags & O_NONBLOCK)) |
| 468 | return -EAGAIN; |
| 469 | |
| 470 | /* |
| 471 | * count == 0 is special - no IO is done but we check |
| 472 | * for error conditions (see above). |
| 473 | */ |
| 474 | if (count == 0) |
| 475 | break; |
| 476 | |
| 477 | while (read + input_event_size() <= count && |
| 478 | evdev_fetch_next_event(client, &event)) { |
| 479 | |
| 480 | if (input_event_to_user(buffer + read, &event)) |
| 481 | return -EFAULT; |
| 482 | |
| 483 | read += input_event_size(); |
| 484 | } |
| 485 | |
| 486 | if (read) |
| 487 | break; |
| 488 | |
| 489 | if (!(file->f_flags & O_NONBLOCK)) { |
| 490 | error = wait_event_interruptible(evdev->wait, |
| 491 | client->packet_head != client->tail || |
| 492 | !evdev->exist); |
| 493 | if (error) |
| 494 | return error; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | return read; |
| 499 | } |
| 500 | |
| 501 | /* No kernel lock - fine */ |
| 502 | static unsigned int evdev_poll(struct file *file, poll_table *wait) |
| 503 | { |
| 504 | struct evdev_client *client = file->private_data; |
| 505 | struct evdev *evdev = client->evdev; |
| 506 | unsigned int mask; |
| 507 | |
| 508 | poll_wait(file, &evdev->wait, wait); |
| 509 | |
| 510 | mask = evdev->exist ? POLLOUT | POLLWRNORM : POLLHUP | POLLERR; |
| 511 | if (client->packet_head != client->tail) |
| 512 | mask |= POLLIN | POLLRDNORM; |
| 513 | |
| 514 | return mask; |
| 515 | } |
| 516 | |
| 517 | #ifdef CONFIG_COMPAT |
| 518 | |
| 519 | #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8) |
| 520 | #define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1) |
| 521 | |
| 522 | #ifdef __BIG_ENDIAN |
| 523 | static int bits_to_user(unsigned long *bits, unsigned int maxbit, |
| 524 | unsigned int maxlen, void __user *p, int compat) |
| 525 | { |
| 526 | int len, i; |
| 527 | |
| 528 | if (compat) { |
| 529 | len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t); |
| 530 | if (len > maxlen) |
| 531 | len = maxlen; |
| 532 | |
| 533 | for (i = 0; i < len / sizeof(compat_long_t); i++) |
| 534 | if (copy_to_user((compat_long_t __user *) p + i, |
| 535 | (compat_long_t *) bits + |
| 536 | i + 1 - ((i % 2) << 1), |
| 537 | sizeof(compat_long_t))) |
| 538 | return -EFAULT; |
| 539 | } else { |
| 540 | len = BITS_TO_LONGS(maxbit) * sizeof(long); |
| 541 | if (len > maxlen) |
| 542 | len = maxlen; |
| 543 | |
| 544 | if (copy_to_user(p, bits, len)) |
| 545 | return -EFAULT; |
| 546 | } |
| 547 | |
| 548 | return len; |
| 549 | } |
| 550 | #else |
| 551 | static int bits_to_user(unsigned long *bits, unsigned int maxbit, |
| 552 | unsigned int maxlen, void __user *p, int compat) |
| 553 | { |
| 554 | int len = compat ? |
| 555 | BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) : |
| 556 | BITS_TO_LONGS(maxbit) * sizeof(long); |
| 557 | |
| 558 | if (len > maxlen) |
| 559 | len = maxlen; |
| 560 | |
| 561 | return copy_to_user(p, bits, len) ? -EFAULT : len; |
| 562 | } |
| 563 | #endif /* __BIG_ENDIAN */ |
| 564 | |
| 565 | #else |
| 566 | |
| 567 | static int bits_to_user(unsigned long *bits, unsigned int maxbit, |
| 568 | unsigned int maxlen, void __user *p, int compat) |
| 569 | { |
| 570 | int len = BITS_TO_LONGS(maxbit) * sizeof(long); |
| 571 | |
| 572 | if (len > maxlen) |
| 573 | len = maxlen; |
| 574 | |
| 575 | return copy_to_user(p, bits, len) ? -EFAULT : len; |
| 576 | } |
| 577 | |
| 578 | #endif /* CONFIG_COMPAT */ |
| 579 | |
| 580 | static int str_to_user(const char *str, unsigned int maxlen, void __user *p) |
| 581 | { |
| 582 | int len; |
| 583 | |
| 584 | if (!str) |
| 585 | return -ENOENT; |
| 586 | |
| 587 | len = strlen(str) + 1; |
| 588 | if (len > maxlen) |
| 589 | len = maxlen; |
| 590 | |
| 591 | return copy_to_user(p, str, len) ? -EFAULT : len; |
| 592 | } |
| 593 | |
| 594 | #define OLD_KEY_MAX 0x1ff |
| 595 | static int handle_eviocgbit(struct input_dev *dev, |
| 596 | unsigned int type, unsigned int size, |
| 597 | void __user *p, int compat_mode) |
| 598 | { |
| 599 | static unsigned long keymax_warn_time; |
| 600 | unsigned long *bits; |
| 601 | int len; |
| 602 | |
| 603 | switch (type) { |
| 604 | |
| 605 | case 0: bits = dev->evbit; len = EV_MAX; break; |
| 606 | case EV_KEY: bits = dev->keybit; len = KEY_MAX; break; |
| 607 | case EV_REL: bits = dev->relbit; len = REL_MAX; break; |
| 608 | case EV_ABS: bits = dev->absbit; len = ABS_MAX; break; |
| 609 | case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break; |
| 610 | case EV_LED: bits = dev->ledbit; len = LED_MAX; break; |
| 611 | case EV_SND: bits = dev->sndbit; len = SND_MAX; break; |
| 612 | case EV_FF: bits = dev->ffbit; len = FF_MAX; break; |
| 613 | case EV_SW: bits = dev->swbit; len = SW_MAX; break; |
| 614 | default: return -EINVAL; |
| 615 | } |
| 616 | |
| 617 | /* |
| 618 | * Work around bugs in userspace programs that like to do |
| 619 | * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len' |
| 620 | * should be in bytes, not in bits. |
| 621 | */ |
| 622 | if (type == EV_KEY && size == OLD_KEY_MAX) { |
| 623 | len = OLD_KEY_MAX; |
| 624 | if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000)) |
| 625 | pr_warning("(EVIOCGBIT): Suspicious buffer size %u, " |
| 626 | "limiting output to %zu bytes. See " |
| 627 | "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n", |
| 628 | OLD_KEY_MAX, |
| 629 | BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long)); |
| 630 | } |
| 631 | |
| 632 | return bits_to_user(bits, len, size, p, compat_mode); |
| 633 | } |
| 634 | #undef OLD_KEY_MAX |
| 635 | |
| 636 | static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p) |
| 637 | { |
| 638 | struct input_keymap_entry ke = { |
| 639 | .len = sizeof(unsigned int), |
| 640 | .flags = 0, |
| 641 | }; |
| 642 | int __user *ip = (int __user *)p; |
| 643 | int error; |
| 644 | |
| 645 | /* legacy case */ |
| 646 | if (copy_from_user(ke.scancode, p, sizeof(unsigned int))) |
| 647 | return -EFAULT; |
| 648 | |
| 649 | error = input_get_keycode(dev, &ke); |
| 650 | if (error) |
| 651 | return error; |
| 652 | |
| 653 | if (put_user(ke.keycode, ip + 1)) |
| 654 | return -EFAULT; |
| 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p) |
| 660 | { |
| 661 | struct input_keymap_entry ke; |
| 662 | int error; |
| 663 | |
| 664 | if (copy_from_user(&ke, p, sizeof(ke))) |
| 665 | return -EFAULT; |
| 666 | |
| 667 | error = input_get_keycode(dev, &ke); |
| 668 | if (error) |
| 669 | return error; |
| 670 | |
| 671 | if (copy_to_user(p, &ke, sizeof(ke))) |
| 672 | return -EFAULT; |
| 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p) |
| 678 | { |
| 679 | struct input_keymap_entry ke = { |
| 680 | .len = sizeof(unsigned int), |
| 681 | .flags = 0, |
| 682 | }; |
| 683 | int __user *ip = (int __user *)p; |
| 684 | |
| 685 | if (copy_from_user(ke.scancode, p, sizeof(unsigned int))) |
| 686 | return -EFAULT; |
| 687 | |
| 688 | if (get_user(ke.keycode, ip + 1)) |
| 689 | return -EFAULT; |
| 690 | |
| 691 | return input_set_keycode(dev, &ke); |
| 692 | } |
| 693 | |
| 694 | static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p) |
| 695 | { |
| 696 | struct input_keymap_entry ke; |
| 697 | |
| 698 | if (copy_from_user(&ke, p, sizeof(ke))) |
| 699 | return -EFAULT; |
| 700 | |
| 701 | if (ke.len > sizeof(ke.scancode)) |
| 702 | return -EINVAL; |
| 703 | |
| 704 | return input_set_keycode(dev, &ke); |
| 705 | } |
| 706 | |
| 707 | static int evdev_handle_mt_request(struct input_dev *dev, |
| 708 | unsigned int size, |
| 709 | int __user *ip) |
| 710 | { |
| 711 | const struct input_mt *mt = dev->mt; |
| 712 | unsigned int code; |
| 713 | int max_slots; |
| 714 | int i; |
| 715 | |
| 716 | if (get_user(code, &ip[0])) |
| 717 | return -EFAULT; |
| 718 | if (!mt || !input_is_mt_value(code)) |
| 719 | return -EINVAL; |
| 720 | |
| 721 | max_slots = (size - sizeof(__u32)) / sizeof(__s32); |
| 722 | for (i = 0; i < mt->num_slots && i < max_slots; i++) { |
| 723 | int value = input_mt_get_value(&mt->slots[i], code); |
| 724 | if (put_user(value, &ip[1 + i])) |
| 725 | return -EFAULT; |
| 726 | } |
| 727 | |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | static int evdev_enable_suspend_block(struct evdev *evdev, |
| 732 | struct evdev_client *client) |
| 733 | { |
| 734 | if (client->use_wake_lock) |
| 735 | return 0; |
| 736 | |
| 737 | spin_lock_irq(&client->buffer_lock); |
| 738 | wake_lock_init(&client->wake_lock, WAKE_LOCK_SUSPEND, client->name); |
| 739 | client->use_wake_lock = true; |
| 740 | if (client->packet_head != client->tail) |
| 741 | wake_lock(&client->wake_lock); |
| 742 | spin_unlock_irq(&client->buffer_lock); |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | static int evdev_disable_suspend_block(struct evdev *evdev, |
| 747 | struct evdev_client *client) |
| 748 | { |
| 749 | if (!client->use_wake_lock) |
| 750 | return 0; |
| 751 | |
| 752 | spin_lock_irq(&client->buffer_lock); |
| 753 | client->use_wake_lock = false; |
| 754 | wake_lock_destroy(&client->wake_lock); |
| 755 | spin_unlock_irq(&client->buffer_lock); |
| 756 | |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | static long evdev_do_ioctl(struct file *file, unsigned int cmd, |
| 761 | void __user *p, int compat_mode) |
| 762 | { |
| 763 | struct evdev_client *client = file->private_data; |
| 764 | struct evdev *evdev = client->evdev; |
| 765 | struct input_dev *dev = evdev->handle.dev; |
| 766 | struct input_absinfo abs; |
| 767 | struct ff_effect effect; |
| 768 | int __user *ip = (int __user *)p; |
| 769 | unsigned int i, t, u, v; |
| 770 | unsigned int size; |
| 771 | int error; |
| 772 | |
| 773 | /* First we check for fixed-length commands */ |
| 774 | switch (cmd) { |
| 775 | |
| 776 | case EVIOCGVERSION: |
| 777 | return put_user(EV_VERSION, ip); |
| 778 | |
| 779 | case EVIOCGID: |
| 780 | if (copy_to_user(p, &dev->id, sizeof(struct input_id))) |
| 781 | return -EFAULT; |
| 782 | return 0; |
| 783 | |
| 784 | case EVIOCGREP: |
| 785 | if (!test_bit(EV_REP, dev->evbit)) |
| 786 | return -ENOSYS; |
| 787 | if (put_user(dev->rep[REP_DELAY], ip)) |
| 788 | return -EFAULT; |
| 789 | if (put_user(dev->rep[REP_PERIOD], ip + 1)) |
| 790 | return -EFAULT; |
| 791 | return 0; |
| 792 | |
| 793 | case EVIOCSREP: |
| 794 | if (!test_bit(EV_REP, dev->evbit)) |
| 795 | return -ENOSYS; |
| 796 | if (get_user(u, ip)) |
| 797 | return -EFAULT; |
| 798 | if (get_user(v, ip + 1)) |
| 799 | return -EFAULT; |
| 800 | |
| 801 | input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u); |
| 802 | input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v); |
| 803 | |
| 804 | return 0; |
| 805 | |
| 806 | case EVIOCRMFF: |
| 807 | return input_ff_erase(dev, (int)(unsigned long) p, file); |
| 808 | |
| 809 | case EVIOCGEFFECTS: |
| 810 | i = test_bit(EV_FF, dev->evbit) ? |
| 811 | dev->ff->max_effects : 0; |
| 812 | if (put_user(i, ip)) |
| 813 | return -EFAULT; |
| 814 | return 0; |
| 815 | |
| 816 | case EVIOCGRAB: |
| 817 | if (p) |
| 818 | return evdev_grab(evdev, client); |
| 819 | else |
| 820 | return evdev_ungrab(evdev, client); |
| 821 | |
| 822 | case EVIOCSCLOCKID: |
| 823 | if (copy_from_user(&i, p, sizeof(unsigned int))) |
| 824 | return -EFAULT; |
| 825 | if (i != CLOCK_MONOTONIC && i != CLOCK_REALTIME) |
| 826 | return -EINVAL; |
| 827 | client->clkid = i; |
| 828 | return 0; |
| 829 | |
| 830 | case EVIOCGKEYCODE: |
| 831 | return evdev_handle_get_keycode(dev, p); |
| 832 | |
| 833 | case EVIOCSKEYCODE: |
| 834 | return evdev_handle_set_keycode(dev, p); |
| 835 | |
| 836 | case EVIOCGKEYCODE_V2: |
| 837 | return evdev_handle_get_keycode_v2(dev, p); |
| 838 | |
| 839 | case EVIOCSKEYCODE_V2: |
| 840 | return evdev_handle_set_keycode_v2(dev, p); |
| 841 | |
| 842 | case EVIOCGSUSPENDBLOCK: |
| 843 | return put_user(client->use_wake_lock, ip); |
| 844 | |
| 845 | case EVIOCSSUSPENDBLOCK: |
| 846 | if (p) |
| 847 | return evdev_enable_suspend_block(evdev, client); |
| 848 | else |
| 849 | return evdev_disable_suspend_block(evdev, client); |
| 850 | } |
| 851 | |
| 852 | size = _IOC_SIZE(cmd); |
| 853 | |
| 854 | /* Now check variable-length commands */ |
| 855 | #define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT)) |
| 856 | switch (EVIOC_MASK_SIZE(cmd)) { |
| 857 | |
| 858 | case EVIOCGPROP(0): |
| 859 | return bits_to_user(dev->propbit, INPUT_PROP_MAX, |
| 860 | size, p, compat_mode); |
| 861 | |
| 862 | case EVIOCGMTSLOTS(0): |
| 863 | return evdev_handle_mt_request(dev, size, ip); |
| 864 | |
| 865 | case EVIOCGKEY(0): |
| 866 | return bits_to_user(dev->key, KEY_MAX, size, p, compat_mode); |
| 867 | |
| 868 | case EVIOCGLED(0): |
| 869 | return bits_to_user(dev->led, LED_MAX, size, p, compat_mode); |
| 870 | |
| 871 | case EVIOCGSND(0): |
| 872 | return bits_to_user(dev->snd, SND_MAX, size, p, compat_mode); |
| 873 | |
| 874 | case EVIOCGSW(0): |
| 875 | return bits_to_user(dev->sw, SW_MAX, size, p, compat_mode); |
| 876 | |
| 877 | case EVIOCGNAME(0): |
| 878 | return str_to_user(dev->name, size, p); |
| 879 | |
| 880 | case EVIOCGPHYS(0): |
| 881 | return str_to_user(dev->phys, size, p); |
| 882 | |
| 883 | case EVIOCGUNIQ(0): |
| 884 | return str_to_user(dev->uniq, size, p); |
| 885 | |
| 886 | case EVIOC_MASK_SIZE(EVIOCSFF): |
| 887 | if (input_ff_effect_from_user(p, size, &effect)) |
| 888 | return -EFAULT; |
| 889 | |
| 890 | error = input_ff_upload(dev, &effect, file); |
| 891 | |
| 892 | if (put_user(effect.id, &(((struct ff_effect __user *)p)->id))) |
| 893 | return -EFAULT; |
| 894 | |
| 895 | return error; |
| 896 | } |
| 897 | |
| 898 | /* Multi-number variable-length handlers */ |
| 899 | if (_IOC_TYPE(cmd) != 'E') |
| 900 | return -EINVAL; |
| 901 | |
| 902 | if (_IOC_DIR(cmd) == _IOC_READ) { |
| 903 | |
| 904 | if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0))) |
| 905 | return handle_eviocgbit(dev, |
| 906 | _IOC_NR(cmd) & EV_MAX, size, |
| 907 | p, compat_mode); |
| 908 | |
| 909 | if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) { |
| 910 | |
| 911 | if (!dev->absinfo) |
| 912 | return -EINVAL; |
| 913 | |
| 914 | t = _IOC_NR(cmd) & ABS_MAX; |
| 915 | abs = dev->absinfo[t]; |
| 916 | |
| 917 | if (copy_to_user(p, &abs, min_t(size_t, |
| 918 | size, sizeof(struct input_absinfo)))) |
| 919 | return -EFAULT; |
| 920 | |
| 921 | return 0; |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | if (_IOC_DIR(cmd) == _IOC_WRITE) { |
| 926 | |
| 927 | if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) { |
| 928 | |
| 929 | if (!dev->absinfo) |
| 930 | return -EINVAL; |
| 931 | |
| 932 | t = _IOC_NR(cmd) & ABS_MAX; |
| 933 | |
| 934 | if (copy_from_user(&abs, p, min_t(size_t, |
| 935 | size, sizeof(struct input_absinfo)))) |
| 936 | return -EFAULT; |
| 937 | |
| 938 | if (size < sizeof(struct input_absinfo)) |
| 939 | abs.resolution = 0; |
| 940 | |
| 941 | /* We can't change number of reserved MT slots */ |
| 942 | if (t == ABS_MT_SLOT) |
| 943 | return -EINVAL; |
| 944 | |
| 945 | /* |
| 946 | * Take event lock to ensure that we are not |
| 947 | * changing device parameters in the middle |
| 948 | * of event. |
| 949 | */ |
| 950 | spin_lock_irq(&dev->event_lock); |
| 951 | dev->absinfo[t] = abs; |
| 952 | spin_unlock_irq(&dev->event_lock); |
| 953 | |
| 954 | return 0; |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | return -EINVAL; |
| 959 | } |
| 960 | |
| 961 | static long evdev_ioctl_handler(struct file *file, unsigned int cmd, |
| 962 | void __user *p, int compat_mode) |
| 963 | { |
| 964 | struct evdev_client *client = file->private_data; |
| 965 | struct evdev *evdev = client->evdev; |
| 966 | int retval; |
| 967 | |
| 968 | retval = mutex_lock_interruptible(&evdev->mutex); |
| 969 | if (retval) |
| 970 | return retval; |
| 971 | |
| 972 | if (!evdev->exist) { |
| 973 | retval = -ENODEV; |
| 974 | goto out; |
| 975 | } |
| 976 | |
| 977 | retval = evdev_do_ioctl(file, cmd, p, compat_mode); |
| 978 | |
| 979 | out: |
| 980 | mutex_unlock(&evdev->mutex); |
| 981 | return retval; |
| 982 | } |
| 983 | |
| 984 | static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 985 | { |
| 986 | return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0); |
| 987 | } |
| 988 | |
| 989 | #ifdef CONFIG_COMPAT |
| 990 | static long evdev_ioctl_compat(struct file *file, |
| 991 | unsigned int cmd, unsigned long arg) |
| 992 | { |
| 993 | return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1); |
| 994 | } |
| 995 | #endif |
| 996 | |
| 997 | static const struct file_operations evdev_fops = { |
| 998 | .owner = THIS_MODULE, |
| 999 | .read = evdev_read, |
| 1000 | .write = evdev_write, |
| 1001 | .poll = evdev_poll, |
| 1002 | .open = evdev_open, |
| 1003 | .release = evdev_release, |
| 1004 | .unlocked_ioctl = evdev_ioctl, |
| 1005 | #ifdef CONFIG_COMPAT |
| 1006 | .compat_ioctl = evdev_ioctl_compat, |
| 1007 | #endif |
| 1008 | .fasync = evdev_fasync, |
| 1009 | .flush = evdev_flush, |
| 1010 | .llseek = no_llseek, |
| 1011 | }; |
| 1012 | |
| 1013 | /* |
| 1014 | * Mark device non-existent. This disables writes, ioctls and |
| 1015 | * prevents new users from opening the device. Already posted |
| 1016 | * blocking reads will stay, however new ones will fail. |
| 1017 | */ |
| 1018 | static void evdev_mark_dead(struct evdev *evdev) |
| 1019 | { |
| 1020 | mutex_lock(&evdev->mutex); |
| 1021 | evdev->exist = false; |
| 1022 | mutex_unlock(&evdev->mutex); |
| 1023 | } |
| 1024 | |
| 1025 | static void evdev_cleanup(struct evdev *evdev) |
| 1026 | { |
| 1027 | struct input_handle *handle = &evdev->handle; |
| 1028 | |
| 1029 | evdev_mark_dead(evdev); |
| 1030 | evdev_hangup(evdev); |
| 1031 | |
| 1032 | cdev_del(&evdev->cdev); |
| 1033 | |
| 1034 | /* evdev is marked dead so no one else accesses evdev->open */ |
| 1035 | if (evdev->open) { |
| 1036 | input_flush_device(handle, NULL); |
| 1037 | input_close_device(handle); |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | /* |
| 1042 | * Create new evdev device. Note that input core serializes calls |
| 1043 | * to connect and disconnect. |
| 1044 | */ |
| 1045 | static int evdev_connect(struct input_handler *handler, struct input_dev *dev, |
| 1046 | const struct input_device_id *id) |
| 1047 | { |
| 1048 | struct evdev *evdev; |
| 1049 | int minor; |
| 1050 | int dev_no; |
| 1051 | int error; |
| 1052 | |
| 1053 | minor = input_get_new_minor(EVDEV_MINOR_BASE, EVDEV_MINORS, true); |
| 1054 | if (minor < 0) { |
| 1055 | error = minor; |
| 1056 | pr_err("failed to reserve new minor: %d\n", error); |
| 1057 | return error; |
| 1058 | } |
| 1059 | |
| 1060 | evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL); |
| 1061 | if (!evdev) { |
| 1062 | error = -ENOMEM; |
| 1063 | goto err_free_minor; |
| 1064 | } |
| 1065 | |
| 1066 | INIT_LIST_HEAD(&evdev->client_list); |
| 1067 | spin_lock_init(&evdev->client_lock); |
| 1068 | mutex_init(&evdev->mutex); |
| 1069 | init_waitqueue_head(&evdev->wait); |
| 1070 | evdev->exist = true; |
| 1071 | |
| 1072 | dev_no = minor; |
| 1073 | /* Normalize device number if it falls into legacy range */ |
| 1074 | if (dev_no < EVDEV_MINOR_BASE + EVDEV_MINORS) |
| 1075 | dev_no -= EVDEV_MINOR_BASE; |
| 1076 | dev_set_name(&evdev->dev, "event%d", dev_no); |
| 1077 | |
| 1078 | evdev->handle.dev = input_get_device(dev); |
| 1079 | evdev->handle.name = dev_name(&evdev->dev); |
| 1080 | evdev->handle.handler = handler; |
| 1081 | evdev->handle.private = evdev; |
| 1082 | |
| 1083 | evdev->dev.devt = MKDEV(INPUT_MAJOR, minor); |
| 1084 | evdev->dev.class = &input_class; |
| 1085 | evdev->dev.parent = &dev->dev; |
| 1086 | evdev->dev.release = evdev_free; |
| 1087 | device_initialize(&evdev->dev); |
| 1088 | |
| 1089 | error = input_register_handle(&evdev->handle); |
| 1090 | if (error) |
| 1091 | goto err_free_evdev; |
| 1092 | |
| 1093 | cdev_init(&evdev->cdev, &evdev_fops); |
| 1094 | evdev->cdev.kobj.parent = &evdev->dev.kobj; |
| 1095 | error = cdev_add(&evdev->cdev, evdev->dev.devt, 1); |
| 1096 | if (error) |
| 1097 | goto err_unregister_handle; |
| 1098 | |
| 1099 | error = device_add(&evdev->dev); |
| 1100 | if (error) |
| 1101 | goto err_cleanup_evdev; |
| 1102 | |
| 1103 | return 0; |
| 1104 | |
| 1105 | err_cleanup_evdev: |
| 1106 | evdev_cleanup(evdev); |
| 1107 | err_unregister_handle: |
| 1108 | input_unregister_handle(&evdev->handle); |
| 1109 | err_free_evdev: |
| 1110 | put_device(&evdev->dev); |
| 1111 | err_free_minor: |
| 1112 | input_free_minor(minor); |
| 1113 | return error; |
| 1114 | } |
| 1115 | |
| 1116 | static void evdev_disconnect(struct input_handle *handle) |
| 1117 | { |
| 1118 | struct evdev *evdev = handle->private; |
| 1119 | |
| 1120 | device_del(&evdev->dev); |
| 1121 | evdev_cleanup(evdev); |
| 1122 | input_free_minor(MINOR(evdev->dev.devt)); |
| 1123 | input_unregister_handle(handle); |
| 1124 | put_device(&evdev->dev); |
| 1125 | } |
| 1126 | |
| 1127 | static const struct input_device_id evdev_ids[] = { |
| 1128 | { .driver_info = 1 }, /* Matches all devices */ |
| 1129 | { }, /* Terminating zero entry */ |
| 1130 | }; |
| 1131 | |
| 1132 | MODULE_DEVICE_TABLE(input, evdev_ids); |
| 1133 | |
| 1134 | static struct input_handler evdev_handler = { |
| 1135 | .event = evdev_event, |
| 1136 | .events = evdev_events, |
| 1137 | .connect = evdev_connect, |
| 1138 | .disconnect = evdev_disconnect, |
| 1139 | .legacy_minors = true, |
| 1140 | .minor = EVDEV_MINOR_BASE, |
| 1141 | .name = "evdev", |
| 1142 | .id_table = evdev_ids, |
| 1143 | }; |
| 1144 | |
| 1145 | static int __init evdev_init(void) |
| 1146 | { |
| 1147 | return input_register_handler(&evdev_handler); |
| 1148 | } |
| 1149 | |
| 1150 | static void __exit evdev_exit(void) |
| 1151 | { |
| 1152 | input_unregister_handler(&evdev_handler); |
| 1153 | } |
| 1154 | |
| 1155 | module_init(evdev_init); |
| 1156 | module_exit(evdev_exit); |
| 1157 | |
| 1158 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 1159 | MODULE_DESCRIPTION("Input driver event char devices"); |
| 1160 | MODULE_LICENSE("GPL"); |