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