1 /* 2 * LIRC base driver 3 * 4 * by Artur Lipowski <alipowski@interia.pl> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 */ 17 18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 19 20 #include <linux/module.h> 21 #include <linux/kernel.h> 22 #include <linux/sched/signal.h> 23 #include <linux/errno.h> 24 #include <linux/ioctl.h> 25 #include <linux/fs.h> 26 #include <linux/poll.h> 27 #include <linux/completion.h> 28 #include <linux/mutex.h> 29 #include <linux/wait.h> 30 #include <linux/unistd.h> 31 #include <linux/kthread.h> 32 #include <linux/bitops.h> 33 #include <linux/device.h> 34 #include <linux/cdev.h> 35 36 #include <media/rc-core.h> 37 #include <media/lirc.h> 38 #include <media/lirc_dev.h> 39 40 static bool debug; 41 42 #define IRCTL_DEV_NAME "BaseRemoteCtl" 43 #define NOPLUG -1 44 #define LOGHEAD "lirc_dev (%s[%d]): " 45 46 static dev_t lirc_base_dev; 47 48 struct irctl { 49 struct lirc_driver d; 50 int attached; 51 int open; 52 53 struct mutex irctl_lock; 54 struct lirc_buffer *buf; 55 unsigned int chunk_size; 56 57 struct device dev; 58 struct cdev cdev; 59 60 struct task_struct *task; 61 long jiffies_to_wait; 62 }; 63 64 static DEFINE_MUTEX(lirc_dev_lock); 65 66 static struct irctl *irctls[MAX_IRCTL_DEVICES]; 67 68 /* Only used for sysfs but defined to void otherwise */ 69 static struct class *lirc_class; 70 71 /* helper function 72 * initializes the irctl structure 73 */ 74 static void lirc_irctl_init(struct irctl *ir) 75 { 76 mutex_init(&ir->irctl_lock); 77 ir->d.minor = NOPLUG; 78 } 79 80 static void lirc_release(struct device *ld) 81 { 82 struct irctl *ir = container_of(ld, struct irctl, dev); 83 84 put_device(ir->dev.parent); 85 86 if (ir->buf != ir->d.rbuf) { 87 lirc_buffer_free(ir->buf); 88 kfree(ir->buf); 89 } 90 91 mutex_lock(&lirc_dev_lock); 92 irctls[ir->d.minor] = NULL; 93 mutex_unlock(&lirc_dev_lock); 94 kfree(ir); 95 } 96 97 /* helper function 98 * reads key codes from driver and puts them into buffer 99 * returns 0 on success 100 */ 101 static int lirc_add_to_buf(struct irctl *ir) 102 { 103 int res; 104 int got_data = -1; 105 106 if (!ir->d.add_to_buf) 107 return 0; 108 109 /* 110 * service the device as long as it is returning 111 * data and we have space 112 */ 113 do { 114 got_data++; 115 res = ir->d.add_to_buf(ir->d.data, ir->buf); 116 } while (!res); 117 118 if (res == -ENODEV) 119 kthread_stop(ir->task); 120 121 return got_data ? 0 : res; 122 } 123 124 /* main function of the polling thread 125 */ 126 static int lirc_thread(void *irctl) 127 { 128 struct irctl *ir = irctl; 129 130 do { 131 if (ir->open) { 132 if (ir->jiffies_to_wait) { 133 set_current_state(TASK_INTERRUPTIBLE); 134 schedule_timeout(ir->jiffies_to_wait); 135 } 136 if (kthread_should_stop()) 137 break; 138 if (!lirc_add_to_buf(ir)) 139 wake_up_interruptible(&ir->buf->wait_poll); 140 } else { 141 set_current_state(TASK_INTERRUPTIBLE); 142 schedule(); 143 } 144 } while (!kthread_should_stop()); 145 146 return 0; 147 } 148 149 150 static const struct file_operations lirc_dev_fops = { 151 .owner = THIS_MODULE, 152 .read = lirc_dev_fop_read, 153 .write = lirc_dev_fop_write, 154 .poll = lirc_dev_fop_poll, 155 .unlocked_ioctl = lirc_dev_fop_ioctl, 156 .open = lirc_dev_fop_open, 157 .release = lirc_dev_fop_close, 158 .llseek = noop_llseek, 159 }; 160 161 static int lirc_cdev_add(struct irctl *ir) 162 { 163 struct lirc_driver *d = &ir->d; 164 struct cdev *cdev; 165 int retval; 166 167 cdev = &ir->cdev; 168 169 if (d->fops) { 170 cdev_init(cdev, d->fops); 171 cdev->owner = d->owner; 172 } else { 173 cdev_init(cdev, &lirc_dev_fops); 174 cdev->owner = THIS_MODULE; 175 } 176 retval = kobject_set_name(&cdev->kobj, "lirc%d", d->minor); 177 if (retval) 178 return retval; 179 180 cdev->kobj.parent = &ir->dev.kobj; 181 return cdev_add(cdev, ir->dev.devt, 1); 182 } 183 184 static int lirc_allocate_buffer(struct irctl *ir) 185 { 186 int err = 0; 187 int bytes_in_key; 188 unsigned int chunk_size; 189 unsigned int buffer_size; 190 struct lirc_driver *d = &ir->d; 191 192 mutex_lock(&lirc_dev_lock); 193 194 bytes_in_key = BITS_TO_LONGS(d->code_length) + 195 (d->code_length % 8 ? 1 : 0); 196 buffer_size = d->buffer_size ? d->buffer_size : BUFLEN / bytes_in_key; 197 chunk_size = d->chunk_size ? d->chunk_size : bytes_in_key; 198 199 if (d->rbuf) { 200 ir->buf = d->rbuf; 201 } else { 202 ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL); 203 if (!ir->buf) { 204 err = -ENOMEM; 205 goto out; 206 } 207 208 err = lirc_buffer_init(ir->buf, chunk_size, buffer_size); 209 if (err) { 210 kfree(ir->buf); 211 goto out; 212 } 213 } 214 ir->chunk_size = ir->buf->chunk_size; 215 216 out: 217 mutex_unlock(&lirc_dev_lock); 218 219 return err; 220 } 221 222 static int lirc_allocate_driver(struct lirc_driver *d) 223 { 224 struct irctl *ir; 225 int minor; 226 int err; 227 228 if (!d) { 229 pr_err("driver pointer must be not NULL!\n"); 230 return -EBADRQC; 231 } 232 233 if (!d->dev) { 234 pr_err("dev pointer not filled in!\n"); 235 return -EINVAL; 236 } 237 238 if (d->minor >= MAX_IRCTL_DEVICES) { 239 dev_err(d->dev, "minor must be between 0 and %d!\n", 240 MAX_IRCTL_DEVICES - 1); 241 return -EBADRQC; 242 } 243 244 if (d->code_length < 1 || d->code_length > (BUFLEN * 8)) { 245 dev_err(d->dev, "code length must be less than %d bits\n", 246 BUFLEN * 8); 247 return -EBADRQC; 248 } 249 250 if (d->sample_rate) { 251 if (2 > d->sample_rate || HZ < d->sample_rate) { 252 dev_err(d->dev, "invalid %d sample rate\n", 253 d->sample_rate); 254 return -EBADRQC; 255 } 256 if (!d->add_to_buf) { 257 dev_err(d->dev, "add_to_buf not set\n"); 258 return -EBADRQC; 259 } 260 } else if (!d->rbuf && !(d->fops && d->fops->read && 261 d->fops->poll && d->fops->unlocked_ioctl)) { 262 dev_err(d->dev, "undefined read, poll, ioctl\n"); 263 return -EBADRQC; 264 } 265 266 mutex_lock(&lirc_dev_lock); 267 268 minor = d->minor; 269 270 if (minor < 0) { 271 /* find first free slot for driver */ 272 for (minor = 0; minor < MAX_IRCTL_DEVICES; minor++) 273 if (!irctls[minor]) 274 break; 275 if (minor == MAX_IRCTL_DEVICES) { 276 dev_err(d->dev, "no free slots for drivers!\n"); 277 err = -ENOMEM; 278 goto out_lock; 279 } 280 } else if (irctls[minor]) { 281 dev_err(d->dev, "minor (%d) just registered!\n", minor); 282 err = -EBUSY; 283 goto out_lock; 284 } 285 286 ir = kzalloc(sizeof(struct irctl), GFP_KERNEL); 287 if (!ir) { 288 err = -ENOMEM; 289 goto out_lock; 290 } 291 lirc_irctl_init(ir); 292 irctls[minor] = ir; 293 d->minor = minor; 294 295 /* some safety check 8-) */ 296 d->name[sizeof(d->name)-1] = '\0'; 297 298 if (d->features == 0) 299 d->features = LIRC_CAN_REC_LIRCCODE; 300 301 ir->d = *d; 302 303 ir->dev.devt = MKDEV(MAJOR(lirc_base_dev), ir->d.minor); 304 ir->dev.class = lirc_class; 305 ir->dev.parent = d->dev; 306 ir->dev.release = lirc_release; 307 dev_set_name(&ir->dev, "lirc%d", ir->d.minor); 308 device_initialize(&ir->dev); 309 310 if (d->sample_rate) { 311 ir->jiffies_to_wait = HZ / d->sample_rate; 312 313 /* try to fire up polling thread */ 314 ir->task = kthread_run(lirc_thread, (void *)ir, "lirc_dev"); 315 if (IS_ERR(ir->task)) { 316 dev_err(d->dev, "cannot run thread for minor = %d\n", 317 d->minor); 318 err = -ECHILD; 319 goto out_sysfs; 320 } 321 } else { 322 /* it means - wait for external event in task queue */ 323 ir->jiffies_to_wait = 0; 324 } 325 326 err = lirc_cdev_add(ir); 327 if (err) 328 goto out_sysfs; 329 330 ir->attached = 1; 331 332 err = device_add(&ir->dev); 333 if (err) 334 goto out_cdev; 335 336 mutex_unlock(&lirc_dev_lock); 337 338 get_device(ir->dev.parent); 339 340 dev_info(ir->d.dev, "lirc_dev: driver %s registered at minor = %d\n", 341 ir->d.name, ir->d.minor); 342 return minor; 343 out_cdev: 344 cdev_del(&ir->cdev); 345 out_sysfs: 346 put_device(&ir->dev); 347 out_lock: 348 mutex_unlock(&lirc_dev_lock); 349 350 return err; 351 } 352 353 int lirc_register_driver(struct lirc_driver *d) 354 { 355 int minor, err = 0; 356 357 minor = lirc_allocate_driver(d); 358 if (minor < 0) 359 return minor; 360 361 if (LIRC_CAN_REC(d->features)) { 362 err = lirc_allocate_buffer(irctls[minor]); 363 if (err) 364 lirc_unregister_driver(minor); 365 } 366 367 return err ? err : minor; 368 } 369 EXPORT_SYMBOL(lirc_register_driver); 370 371 int lirc_unregister_driver(int minor) 372 { 373 struct irctl *ir; 374 375 if (minor < 0 || minor >= MAX_IRCTL_DEVICES) { 376 pr_err("minor (%d) must be between 0 and %d!\n", 377 minor, MAX_IRCTL_DEVICES - 1); 378 return -EBADRQC; 379 } 380 381 ir = irctls[minor]; 382 if (!ir) { 383 pr_err("failed to get irctl\n"); 384 return -ENOENT; 385 } 386 387 mutex_lock(&lirc_dev_lock); 388 389 if (ir->d.minor != minor) { 390 dev_err(ir->d.dev, "lirc_dev: minor %d device not registered\n", 391 minor); 392 mutex_unlock(&lirc_dev_lock); 393 return -ENOENT; 394 } 395 396 /* end up polling thread */ 397 if (ir->task) 398 kthread_stop(ir->task); 399 400 dev_dbg(ir->d.dev, "lirc_dev: driver %s unregistered from minor = %d\n", 401 ir->d.name, ir->d.minor); 402 403 ir->attached = 0; 404 if (ir->open) { 405 dev_dbg(ir->d.dev, LOGHEAD "releasing opened driver\n", 406 ir->d.name, ir->d.minor); 407 wake_up_interruptible(&ir->buf->wait_poll); 408 } 409 410 mutex_lock(&ir->irctl_lock); 411 412 if (ir->d.set_use_dec) 413 ir->d.set_use_dec(ir->d.data); 414 415 mutex_unlock(&ir->irctl_lock); 416 mutex_unlock(&lirc_dev_lock); 417 418 device_del(&ir->dev); 419 cdev_del(&ir->cdev); 420 put_device(&ir->dev); 421 422 return 0; 423 } 424 EXPORT_SYMBOL(lirc_unregister_driver); 425 426 int lirc_dev_fop_open(struct inode *inode, struct file *file) 427 { 428 struct irctl *ir; 429 int retval = 0; 430 431 if (iminor(inode) >= MAX_IRCTL_DEVICES) { 432 pr_err("open result for %d is -ENODEV\n", iminor(inode)); 433 return -ENODEV; 434 } 435 436 if (mutex_lock_interruptible(&lirc_dev_lock)) 437 return -ERESTARTSYS; 438 439 ir = irctls[iminor(inode)]; 440 mutex_unlock(&lirc_dev_lock); 441 442 if (!ir) { 443 retval = -ENODEV; 444 goto error; 445 } 446 447 dev_dbg(ir->d.dev, LOGHEAD "open called\n", ir->d.name, ir->d.minor); 448 449 if (ir->d.minor == NOPLUG) { 450 retval = -ENODEV; 451 goto error; 452 } 453 454 if (ir->open) { 455 retval = -EBUSY; 456 goto error; 457 } 458 459 if (ir->d.rdev) { 460 retval = rc_open(ir->d.rdev); 461 if (retval) 462 goto error; 463 } 464 465 ir->open++; 466 if (ir->d.set_use_inc) 467 retval = ir->d.set_use_inc(ir->d.data); 468 if (retval) { 469 ir->open--; 470 } else { 471 if (ir->buf) 472 lirc_buffer_clear(ir->buf); 473 if (ir->task) 474 wake_up_process(ir->task); 475 } 476 477 error: 478 nonseekable_open(inode, file); 479 480 return retval; 481 } 482 EXPORT_SYMBOL(lirc_dev_fop_open); 483 484 int lirc_dev_fop_close(struct inode *inode, struct file *file) 485 { 486 struct irctl *ir = irctls[iminor(inode)]; 487 int ret; 488 489 if (!ir) { 490 pr_err("called with invalid irctl\n"); 491 return -EINVAL; 492 } 493 494 ret = mutex_lock_killable(&lirc_dev_lock); 495 WARN_ON(ret); 496 497 rc_close(ir->d.rdev); 498 499 ir->open--; 500 if (ir->d.set_use_dec) 501 ir->d.set_use_dec(ir->d.data); 502 if (!ret) 503 mutex_unlock(&lirc_dev_lock); 504 505 return 0; 506 } 507 EXPORT_SYMBOL(lirc_dev_fop_close); 508 509 unsigned int lirc_dev_fop_poll(struct file *file, poll_table *wait) 510 { 511 struct irctl *ir = irctls[iminor(file_inode(file))]; 512 unsigned int ret; 513 514 if (!ir) { 515 pr_err("called with invalid irctl\n"); 516 return POLLERR; 517 } 518 519 if (!ir->attached) 520 return POLLERR; 521 522 if (ir->buf) { 523 poll_wait(file, &ir->buf->wait_poll, wait); 524 525 if (lirc_buffer_empty(ir->buf)) 526 ret = 0; 527 else 528 ret = POLLIN | POLLRDNORM; 529 } else 530 ret = POLLERR; 531 532 dev_dbg(ir->d.dev, LOGHEAD "poll result = %d\n", 533 ir->d.name, ir->d.minor, ret); 534 535 return ret; 536 } 537 EXPORT_SYMBOL(lirc_dev_fop_poll); 538 539 long lirc_dev_fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 540 { 541 __u32 mode; 542 int result = 0; 543 struct irctl *ir = irctls[iminor(file_inode(file))]; 544 545 if (!ir) { 546 pr_err("no irctl found!\n"); 547 return -ENODEV; 548 } 549 550 dev_dbg(ir->d.dev, LOGHEAD "ioctl called (0x%x)\n", 551 ir->d.name, ir->d.minor, cmd); 552 553 if (ir->d.minor == NOPLUG || !ir->attached) { 554 dev_err(ir->d.dev, LOGHEAD "ioctl result = -ENODEV\n", 555 ir->d.name, ir->d.minor); 556 return -ENODEV; 557 } 558 559 mutex_lock(&ir->irctl_lock); 560 561 switch (cmd) { 562 case LIRC_GET_FEATURES: 563 result = put_user(ir->d.features, (__u32 __user *)arg); 564 break; 565 case LIRC_GET_REC_MODE: 566 if (!LIRC_CAN_REC(ir->d.features)) { 567 result = -ENOTTY; 568 break; 569 } 570 571 result = put_user(LIRC_REC2MODE 572 (ir->d.features & LIRC_CAN_REC_MASK), 573 (__u32 __user *)arg); 574 break; 575 case LIRC_SET_REC_MODE: 576 if (!LIRC_CAN_REC(ir->d.features)) { 577 result = -ENOTTY; 578 break; 579 } 580 581 result = get_user(mode, (__u32 __user *)arg); 582 if (!result && !(LIRC_MODE2REC(mode) & ir->d.features)) 583 result = -EINVAL; 584 /* 585 * FIXME: We should actually set the mode somehow but 586 * for now, lirc_serial doesn't support mode changing either 587 */ 588 break; 589 case LIRC_GET_LENGTH: 590 result = put_user(ir->d.code_length, (__u32 __user *)arg); 591 break; 592 case LIRC_GET_MIN_TIMEOUT: 593 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) || 594 ir->d.min_timeout == 0) { 595 result = -ENOTTY; 596 break; 597 } 598 599 result = put_user(ir->d.min_timeout, (__u32 __user *)arg); 600 break; 601 case LIRC_GET_MAX_TIMEOUT: 602 if (!(ir->d.features & LIRC_CAN_SET_REC_TIMEOUT) || 603 ir->d.max_timeout == 0) { 604 result = -ENOTTY; 605 break; 606 } 607 608 result = put_user(ir->d.max_timeout, (__u32 __user *)arg); 609 break; 610 default: 611 result = -ENOTTY; 612 } 613 614 mutex_unlock(&ir->irctl_lock); 615 616 return result; 617 } 618 EXPORT_SYMBOL(lirc_dev_fop_ioctl); 619 620 ssize_t lirc_dev_fop_read(struct file *file, 621 char __user *buffer, 622 size_t length, 623 loff_t *ppos) 624 { 625 struct irctl *ir = irctls[iminor(file_inode(file))]; 626 unsigned char *buf; 627 int ret = 0, written = 0; 628 DECLARE_WAITQUEUE(wait, current); 629 630 if (!ir) { 631 pr_err("called with invalid irctl\n"); 632 return -ENODEV; 633 } 634 635 if (!LIRC_CAN_REC(ir->d.features)) 636 return -EINVAL; 637 638 dev_dbg(ir->d.dev, LOGHEAD "read called\n", ir->d.name, ir->d.minor); 639 640 buf = kzalloc(ir->chunk_size, GFP_KERNEL); 641 if (!buf) 642 return -ENOMEM; 643 644 if (mutex_lock_interruptible(&ir->irctl_lock)) { 645 ret = -ERESTARTSYS; 646 goto out_unlocked; 647 } 648 if (!ir->attached) { 649 ret = -ENODEV; 650 goto out_locked; 651 } 652 653 if (length % ir->chunk_size) { 654 ret = -EINVAL; 655 goto out_locked; 656 } 657 658 /* 659 * we add ourselves to the task queue before buffer check 660 * to avoid losing scan code (in case when queue is awaken somewhere 661 * between while condition checking and scheduling) 662 */ 663 add_wait_queue(&ir->buf->wait_poll, &wait); 664 665 /* 666 * while we didn't provide 'length' bytes, device is opened in blocking 667 * mode and 'copy_to_user' is happy, wait for data. 668 */ 669 while (written < length && ret == 0) { 670 if (lirc_buffer_empty(ir->buf)) { 671 /* According to the read(2) man page, 'written' can be 672 * returned as less than 'length', instead of blocking 673 * again, returning -EWOULDBLOCK, or returning 674 * -ERESTARTSYS 675 */ 676 if (written) 677 break; 678 if (file->f_flags & O_NONBLOCK) { 679 ret = -EWOULDBLOCK; 680 break; 681 } 682 if (signal_pending(current)) { 683 ret = -ERESTARTSYS; 684 break; 685 } 686 687 mutex_unlock(&ir->irctl_lock); 688 set_current_state(TASK_INTERRUPTIBLE); 689 schedule(); 690 set_current_state(TASK_RUNNING); 691 692 if (mutex_lock_interruptible(&ir->irctl_lock)) { 693 ret = -ERESTARTSYS; 694 remove_wait_queue(&ir->buf->wait_poll, &wait); 695 goto out_unlocked; 696 } 697 698 if (!ir->attached) { 699 ret = -ENODEV; 700 goto out_locked; 701 } 702 } else { 703 lirc_buffer_read(ir->buf, buf); 704 ret = copy_to_user((void __user *)buffer+written, buf, 705 ir->buf->chunk_size); 706 if (!ret) 707 written += ir->buf->chunk_size; 708 else 709 ret = -EFAULT; 710 } 711 } 712 713 remove_wait_queue(&ir->buf->wait_poll, &wait); 714 715 out_locked: 716 mutex_unlock(&ir->irctl_lock); 717 718 out_unlocked: 719 kfree(buf); 720 721 return ret ? ret : written; 722 } 723 EXPORT_SYMBOL(lirc_dev_fop_read); 724 725 void *lirc_get_pdata(struct file *file) 726 { 727 return irctls[iminor(file_inode(file))]->d.data; 728 } 729 EXPORT_SYMBOL(lirc_get_pdata); 730 731 732 ssize_t lirc_dev_fop_write(struct file *file, const char __user *buffer, 733 size_t length, loff_t *ppos) 734 { 735 struct irctl *ir = irctls[iminor(file_inode(file))]; 736 737 if (!ir) { 738 pr_err("called with invalid irctl\n"); 739 return -ENODEV; 740 } 741 742 if (!ir->attached) 743 return -ENODEV; 744 745 return -EINVAL; 746 } 747 EXPORT_SYMBOL(lirc_dev_fop_write); 748 749 750 static int __init lirc_dev_init(void) 751 { 752 int retval; 753 754 lirc_class = class_create(THIS_MODULE, "lirc"); 755 if (IS_ERR(lirc_class)) { 756 pr_err("class_create failed\n"); 757 return PTR_ERR(lirc_class); 758 } 759 760 retval = alloc_chrdev_region(&lirc_base_dev, 0, MAX_IRCTL_DEVICES, 761 IRCTL_DEV_NAME); 762 if (retval) { 763 class_destroy(lirc_class); 764 pr_err("alloc_chrdev_region failed\n"); 765 return retval; 766 } 767 768 pr_info("IR Remote Control driver registered, major %d\n", 769 MAJOR(lirc_base_dev)); 770 771 return 0; 772 } 773 774 static void __exit lirc_dev_exit(void) 775 { 776 class_destroy(lirc_class); 777 unregister_chrdev_region(lirc_base_dev, MAX_IRCTL_DEVICES); 778 pr_info("module unloaded\n"); 779 } 780 781 module_init(lirc_dev_init); 782 module_exit(lirc_dev_exit); 783 784 MODULE_DESCRIPTION("LIRC base driver module"); 785 MODULE_AUTHOR("Artur Lipowski"); 786 MODULE_LICENSE("GPL"); 787 788 module_param(debug, bool, S_IRUGO | S_IWUSR); 789 MODULE_PARM_DESC(debug, "Enable debugging messages"); 790