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/mutex.h> 22 #include <linux/device.h> 23 #include <linux/idr.h> 24 #include <linux/poll.h> 25 #include <linux/sched.h> 26 #include <linux/wait.h> 27 28 #include "rc-core-priv.h" 29 #include <uapi/linux/lirc.h> 30 31 #define LIRCBUF_SIZE 256 32 33 static dev_t lirc_base_dev; 34 35 /* Used to keep track of allocated lirc devices */ 36 static DEFINE_IDA(lirc_ida); 37 38 /* Only used for sysfs but defined to void otherwise */ 39 static struct class *lirc_class; 40 41 /** 42 * ir_lirc_raw_event() - Send raw IR data to lirc to be relayed to userspace 43 * 44 * @dev: the struct rc_dev descriptor of the device 45 * @ev: the struct ir_raw_event descriptor of the pulse/space 46 */ 47 void ir_lirc_raw_event(struct rc_dev *dev, struct ir_raw_event ev) 48 { 49 unsigned long flags; 50 struct lirc_fh *fh; 51 int sample; 52 53 /* Packet start */ 54 if (ev.reset) { 55 /* 56 * Userspace expects a long space event before the start of 57 * the signal to use as a sync. This may be done with repeat 58 * packets and normal samples. But if a reset has been sent 59 * then we assume that a long time has passed, so we send a 60 * space with the maximum time value. 61 */ 62 sample = LIRC_SPACE(LIRC_VALUE_MASK); 63 IR_dprintk(2, "delivering reset sync space to lirc_dev\n"); 64 65 /* Carrier reports */ 66 } else if (ev.carrier_report) { 67 sample = LIRC_FREQUENCY(ev.carrier); 68 IR_dprintk(2, "carrier report (freq: %d)\n", sample); 69 70 /* Packet end */ 71 } else if (ev.timeout) { 72 if (dev->gap) 73 return; 74 75 dev->gap_start = ktime_get(); 76 dev->gap = true; 77 dev->gap_duration = ev.duration; 78 79 sample = LIRC_TIMEOUT(ev.duration / 1000); 80 IR_dprintk(2, "timeout report (duration: %d)\n", sample); 81 82 /* Normal sample */ 83 } else { 84 if (dev->gap) { 85 dev->gap_duration += ktime_to_ns(ktime_sub(ktime_get(), 86 dev->gap_start)); 87 88 /* Convert to ms and cap by LIRC_VALUE_MASK */ 89 do_div(dev->gap_duration, 1000); 90 dev->gap_duration = min_t(u64, dev->gap_duration, 91 LIRC_VALUE_MASK); 92 93 spin_lock_irqsave(&dev->lirc_fh_lock, flags); 94 list_for_each_entry(fh, &dev->lirc_fh, list) 95 kfifo_put(&fh->rawir, 96 LIRC_SPACE(dev->gap_duration)); 97 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags); 98 dev->gap = false; 99 } 100 101 sample = ev.pulse ? LIRC_PULSE(ev.duration / 1000) : 102 LIRC_SPACE(ev.duration / 1000); 103 IR_dprintk(2, "delivering %uus %s to lirc_dev\n", 104 TO_US(ev.duration), TO_STR(ev.pulse)); 105 } 106 107 spin_lock_irqsave(&dev->lirc_fh_lock, flags); 108 list_for_each_entry(fh, &dev->lirc_fh, list) { 109 if (LIRC_IS_TIMEOUT(sample) && !fh->send_timeout_reports) 110 continue; 111 if (kfifo_put(&fh->rawir, sample)) 112 wake_up_poll(&fh->wait_poll, POLLIN | POLLRDNORM); 113 } 114 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags); 115 } 116 117 /** 118 * ir_lirc_scancode_event() - Send scancode data to lirc to be relayed to 119 * userspace. This can be called in atomic context. 120 * @dev: the struct rc_dev descriptor of the device 121 * @lsc: the struct lirc_scancode describing the decoded scancode 122 */ 123 void ir_lirc_scancode_event(struct rc_dev *dev, struct lirc_scancode *lsc) 124 { 125 unsigned long flags; 126 struct lirc_fh *fh; 127 128 lsc->timestamp = ktime_get_ns(); 129 130 spin_lock_irqsave(&dev->lirc_fh_lock, flags); 131 list_for_each_entry(fh, &dev->lirc_fh, list) { 132 if (kfifo_put(&fh->scancodes, *lsc)) 133 wake_up_poll(&fh->wait_poll, POLLIN | POLLRDNORM); 134 } 135 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags); 136 } 137 EXPORT_SYMBOL_GPL(ir_lirc_scancode_event); 138 139 static int ir_lirc_open(struct inode *inode, struct file *file) 140 { 141 struct rc_dev *dev = container_of(inode->i_cdev, struct rc_dev, 142 lirc_cdev); 143 struct lirc_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL); 144 unsigned long flags; 145 int retval; 146 147 if (!fh) 148 return -ENOMEM; 149 150 get_device(&dev->dev); 151 152 if (!dev->registered) { 153 retval = -ENODEV; 154 goto out_fh; 155 } 156 157 if (dev->driver_type == RC_DRIVER_IR_RAW) { 158 if (kfifo_alloc(&fh->rawir, MAX_IR_EVENT_SIZE, GFP_KERNEL)) { 159 retval = -ENOMEM; 160 goto out_fh; 161 } 162 } 163 164 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) { 165 if (kfifo_alloc(&fh->scancodes, 32, GFP_KERNEL)) { 166 retval = -ENOMEM; 167 goto out_rawir; 168 } 169 } 170 171 fh->send_mode = LIRC_MODE_PULSE; 172 fh->rc = dev; 173 fh->send_timeout_reports = true; 174 175 if (dev->driver_type == RC_DRIVER_SCANCODE) 176 fh->rec_mode = LIRC_MODE_SCANCODE; 177 else 178 fh->rec_mode = LIRC_MODE_MODE2; 179 180 retval = rc_open(dev); 181 if (retval) 182 goto out_kfifo; 183 184 init_waitqueue_head(&fh->wait_poll); 185 186 file->private_data = fh; 187 spin_lock_irqsave(&dev->lirc_fh_lock, flags); 188 list_add(&fh->list, &dev->lirc_fh); 189 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags); 190 191 nonseekable_open(inode, file); 192 193 return 0; 194 out_kfifo: 195 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) 196 kfifo_free(&fh->scancodes); 197 out_rawir: 198 if (dev->driver_type == RC_DRIVER_IR_RAW) 199 kfifo_free(&fh->rawir); 200 out_fh: 201 kfree(fh); 202 put_device(&dev->dev); 203 204 return retval; 205 } 206 207 static int ir_lirc_close(struct inode *inode, struct file *file) 208 { 209 struct lirc_fh *fh = file->private_data; 210 struct rc_dev *dev = fh->rc; 211 unsigned long flags; 212 213 spin_lock_irqsave(&dev->lirc_fh_lock, flags); 214 list_del(&fh->list); 215 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags); 216 217 if (dev->driver_type == RC_DRIVER_IR_RAW) 218 kfifo_free(&fh->rawir); 219 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) 220 kfifo_free(&fh->scancodes); 221 kfree(fh); 222 223 rc_close(dev); 224 put_device(&dev->dev); 225 226 return 0; 227 } 228 229 static ssize_t ir_lirc_transmit_ir(struct file *file, const char __user *buf, 230 size_t n, loff_t *ppos) 231 { 232 struct lirc_fh *fh = file->private_data; 233 struct rc_dev *dev = fh->rc; 234 unsigned int *txbuf; 235 struct ir_raw_event *raw = NULL; 236 ssize_t ret; 237 size_t count; 238 ktime_t start; 239 s64 towait; 240 unsigned int duration = 0; /* signal duration in us */ 241 int i; 242 243 ret = mutex_lock_interruptible(&dev->lock); 244 if (ret) 245 return ret; 246 247 if (!dev->registered) { 248 ret = -ENODEV; 249 goto out_unlock; 250 } 251 252 start = ktime_get(); 253 254 if (!dev->tx_ir) { 255 ret = -EINVAL; 256 goto out_unlock; 257 } 258 259 if (fh->send_mode == LIRC_MODE_SCANCODE) { 260 struct lirc_scancode scan; 261 262 if (n != sizeof(scan)) { 263 ret = -EINVAL; 264 goto out_unlock; 265 } 266 267 if (copy_from_user(&scan, buf, sizeof(scan))) { 268 ret = -EFAULT; 269 goto out_unlock; 270 } 271 272 if (scan.flags || scan.keycode || scan.timestamp) { 273 ret = -EINVAL; 274 goto out_unlock; 275 } 276 277 /* 278 * The scancode field in lirc_scancode is 64-bit simply 279 * to future-proof it, since there are IR protocols encode 280 * use more than 32 bits. For now only 32-bit protocols 281 * are supported. 282 */ 283 if (scan.scancode > U32_MAX || 284 !rc_validate_scancode(scan.rc_proto, scan.scancode)) { 285 ret = -EINVAL; 286 goto out_unlock; 287 } 288 289 raw = kmalloc_array(LIRCBUF_SIZE, sizeof(*raw), GFP_KERNEL); 290 if (!raw) { 291 ret = -ENOMEM; 292 goto out_unlock; 293 } 294 295 ret = ir_raw_encode_scancode(scan.rc_proto, scan.scancode, 296 raw, LIRCBUF_SIZE); 297 if (ret < 0) 298 goto out_kfree; 299 300 count = ret; 301 302 txbuf = kmalloc_array(count, sizeof(unsigned int), GFP_KERNEL); 303 if (!txbuf) { 304 ret = -ENOMEM; 305 goto out_kfree; 306 } 307 308 for (i = 0; i < count; i++) 309 /* Convert from NS to US */ 310 txbuf[i] = DIV_ROUND_UP(raw[i].duration, 1000); 311 312 if (dev->s_tx_carrier) { 313 int carrier = ir_raw_encode_carrier(scan.rc_proto); 314 315 if (carrier > 0) 316 dev->s_tx_carrier(dev, carrier); 317 } 318 } else { 319 if (n < sizeof(unsigned int) || n % sizeof(unsigned int)) { 320 ret = -EINVAL; 321 goto out_unlock; 322 } 323 324 count = n / sizeof(unsigned int); 325 if (count > LIRCBUF_SIZE || count % 2 == 0) { 326 ret = -EINVAL; 327 goto out_unlock; 328 } 329 330 txbuf = memdup_user(buf, n); 331 if (IS_ERR(txbuf)) { 332 ret = PTR_ERR(txbuf); 333 goto out_unlock; 334 } 335 } 336 337 for (i = 0; i < count; i++) { 338 if (txbuf[i] > IR_MAX_DURATION / 1000 - duration || !txbuf[i]) { 339 ret = -EINVAL; 340 goto out_kfree; 341 } 342 343 duration += txbuf[i]; 344 } 345 346 ret = dev->tx_ir(dev, txbuf, count); 347 if (ret < 0) 348 goto out_kfree; 349 350 kfree(txbuf); 351 kfree(raw); 352 mutex_unlock(&dev->lock); 353 354 /* 355 * The lircd gap calculation expects the write function to 356 * wait for the actual IR signal to be transmitted before 357 * returning. 358 */ 359 towait = ktime_us_delta(ktime_add_us(start, duration), 360 ktime_get()); 361 if (towait > 0) { 362 set_current_state(TASK_INTERRUPTIBLE); 363 schedule_timeout(usecs_to_jiffies(towait)); 364 } 365 366 return n; 367 out_kfree: 368 kfree(txbuf); 369 kfree(raw); 370 out_unlock: 371 mutex_unlock(&dev->lock); 372 return ret; 373 } 374 375 static long ir_lirc_ioctl(struct file *file, unsigned int cmd, 376 unsigned long arg) 377 { 378 struct lirc_fh *fh = file->private_data; 379 struct rc_dev *dev = fh->rc; 380 u32 __user *argp = (u32 __user *)(arg); 381 u32 val = 0; 382 int ret; 383 384 if (_IOC_DIR(cmd) & _IOC_WRITE) { 385 ret = get_user(val, argp); 386 if (ret) 387 return ret; 388 } 389 390 ret = mutex_lock_interruptible(&dev->lock); 391 if (ret) 392 return ret; 393 394 if (!dev->registered) { 395 ret = -ENODEV; 396 goto out; 397 } 398 399 switch (cmd) { 400 case LIRC_GET_FEATURES: 401 if (dev->driver_type == RC_DRIVER_SCANCODE) 402 val |= LIRC_CAN_REC_SCANCODE; 403 404 if (dev->driver_type == RC_DRIVER_IR_RAW) { 405 val |= LIRC_CAN_REC_MODE2 | LIRC_CAN_REC_SCANCODE; 406 if (dev->rx_resolution) 407 val |= LIRC_CAN_GET_REC_RESOLUTION; 408 } 409 410 if (dev->tx_ir) { 411 val |= LIRC_CAN_SEND_PULSE | LIRC_CAN_SEND_SCANCODE; 412 if (dev->s_tx_mask) 413 val |= LIRC_CAN_SET_TRANSMITTER_MASK; 414 if (dev->s_tx_carrier) 415 val |= LIRC_CAN_SET_SEND_CARRIER; 416 if (dev->s_tx_duty_cycle) 417 val |= LIRC_CAN_SET_SEND_DUTY_CYCLE; 418 } 419 420 if (dev->s_rx_carrier_range) 421 val |= LIRC_CAN_SET_REC_CARRIER | 422 LIRC_CAN_SET_REC_CARRIER_RANGE; 423 424 if (dev->s_learning_mode) 425 val |= LIRC_CAN_USE_WIDEBAND_RECEIVER; 426 427 if (dev->s_carrier_report) 428 val |= LIRC_CAN_MEASURE_CARRIER; 429 430 if (dev->max_timeout) 431 val |= LIRC_CAN_SET_REC_TIMEOUT; 432 433 break; 434 435 /* mode support */ 436 case LIRC_GET_REC_MODE: 437 if (dev->driver_type == RC_DRIVER_IR_RAW_TX) 438 ret = -ENOTTY; 439 else 440 val = fh->rec_mode; 441 break; 442 443 case LIRC_SET_REC_MODE: 444 switch (dev->driver_type) { 445 case RC_DRIVER_IR_RAW_TX: 446 ret = -ENOTTY; 447 break; 448 case RC_DRIVER_SCANCODE: 449 if (val != LIRC_MODE_SCANCODE) 450 ret = -EINVAL; 451 break; 452 case RC_DRIVER_IR_RAW: 453 if (!(val == LIRC_MODE_MODE2 || 454 val == LIRC_MODE_SCANCODE)) 455 ret = -EINVAL; 456 break; 457 } 458 459 if (!ret) 460 fh->rec_mode = val; 461 break; 462 463 case LIRC_GET_SEND_MODE: 464 if (!dev->tx_ir) 465 ret = -ENOTTY; 466 else 467 val = fh->send_mode; 468 break; 469 470 case LIRC_SET_SEND_MODE: 471 if (!dev->tx_ir) 472 ret = -ENOTTY; 473 else if (!(val == LIRC_MODE_PULSE || val == LIRC_MODE_SCANCODE)) 474 ret = -EINVAL; 475 else 476 fh->send_mode = val; 477 break; 478 479 /* TX settings */ 480 case LIRC_SET_TRANSMITTER_MASK: 481 if (!dev->s_tx_mask) 482 ret = -ENOTTY; 483 else 484 ret = dev->s_tx_mask(dev, val); 485 break; 486 487 case LIRC_SET_SEND_CARRIER: 488 if (!dev->s_tx_carrier) 489 ret = -ENOTTY; 490 else 491 ret = dev->s_tx_carrier(dev, val); 492 break; 493 494 case LIRC_SET_SEND_DUTY_CYCLE: 495 if (!dev->s_tx_duty_cycle) 496 ret = -ENOTTY; 497 else if (val <= 0 || val >= 100) 498 ret = -EINVAL; 499 else 500 ret = dev->s_tx_duty_cycle(dev, val); 501 break; 502 503 /* RX settings */ 504 case LIRC_SET_REC_CARRIER: 505 if (!dev->s_rx_carrier_range) 506 ret = -ENOTTY; 507 else if (val <= 0) 508 ret = -EINVAL; 509 else 510 ret = dev->s_rx_carrier_range(dev, fh->carrier_low, 511 val); 512 break; 513 514 case LIRC_SET_REC_CARRIER_RANGE: 515 if (!dev->s_rx_carrier_range) 516 ret = -ENOTTY; 517 else if (val <= 0) 518 ret = -EINVAL; 519 else 520 fh->carrier_low = val; 521 break; 522 523 case LIRC_GET_REC_RESOLUTION: 524 if (!dev->rx_resolution) 525 ret = -ENOTTY; 526 else 527 val = dev->rx_resolution / 1000; 528 break; 529 530 case LIRC_SET_WIDEBAND_RECEIVER: 531 if (!dev->s_learning_mode) 532 ret = -ENOTTY; 533 else 534 ret = dev->s_learning_mode(dev, !!val); 535 break; 536 537 case LIRC_SET_MEASURE_CARRIER_MODE: 538 if (!dev->s_carrier_report) 539 ret = -ENOTTY; 540 else 541 ret = dev->s_carrier_report(dev, !!val); 542 break; 543 544 /* Generic timeout support */ 545 case LIRC_GET_MIN_TIMEOUT: 546 if (!dev->max_timeout) 547 ret = -ENOTTY; 548 else 549 val = DIV_ROUND_UP(dev->min_timeout, 1000); 550 break; 551 552 case LIRC_GET_MAX_TIMEOUT: 553 if (!dev->max_timeout) 554 ret = -ENOTTY; 555 else 556 val = dev->max_timeout / 1000; 557 break; 558 559 case LIRC_SET_REC_TIMEOUT: 560 if (!dev->max_timeout) { 561 ret = -ENOTTY; 562 } else if (val > U32_MAX / 1000) { 563 /* Check for multiply overflow */ 564 ret = -EINVAL; 565 } else { 566 u32 tmp = val * 1000; 567 568 if (tmp < dev->min_timeout || tmp > dev->max_timeout) 569 ret = -EINVAL; 570 else if (dev->s_timeout) 571 ret = dev->s_timeout(dev, tmp); 572 else if (!ret) 573 dev->timeout = tmp; 574 } 575 break; 576 577 case LIRC_SET_REC_TIMEOUT_REPORTS: 578 if (!dev->timeout) 579 ret = -ENOTTY; 580 else 581 fh->send_timeout_reports = !!val; 582 break; 583 584 default: 585 ret = -ENOTTY; 586 } 587 588 if (!ret && _IOC_DIR(cmd) & _IOC_READ) 589 ret = put_user(val, argp); 590 591 out: 592 mutex_unlock(&dev->lock); 593 return ret; 594 } 595 596 static unsigned int ir_lirc_poll(struct file *file, 597 struct poll_table_struct *wait) 598 { 599 struct lirc_fh *fh = file->private_data; 600 struct rc_dev *rcdev = fh->rc; 601 unsigned int events = 0; 602 603 poll_wait(file, &fh->wait_poll, wait); 604 605 if (!rcdev->registered) { 606 events = POLLHUP | POLLERR; 607 } else if (rcdev->driver_type != RC_DRIVER_IR_RAW_TX) { 608 if (fh->rec_mode == LIRC_MODE_SCANCODE && 609 !kfifo_is_empty(&fh->scancodes)) 610 events = POLLIN | POLLRDNORM; 611 612 if (fh->rec_mode == LIRC_MODE_MODE2 && 613 !kfifo_is_empty(&fh->rawir)) 614 events = POLLIN | POLLRDNORM; 615 } 616 617 return events; 618 } 619 620 static ssize_t ir_lirc_read_mode2(struct file *file, char __user *buffer, 621 size_t length) 622 { 623 struct lirc_fh *fh = file->private_data; 624 struct rc_dev *rcdev = fh->rc; 625 unsigned int copied; 626 int ret; 627 628 if (length < sizeof(unsigned int) || length % sizeof(unsigned int)) 629 return -EINVAL; 630 631 do { 632 if (kfifo_is_empty(&fh->rawir)) { 633 if (file->f_flags & O_NONBLOCK) 634 return -EAGAIN; 635 636 ret = wait_event_interruptible(fh->wait_poll, 637 !kfifo_is_empty(&fh->rawir) || 638 !rcdev->registered); 639 if (ret) 640 return ret; 641 } 642 643 if (!rcdev->registered) 644 return -ENODEV; 645 646 ret = mutex_lock_interruptible(&rcdev->lock); 647 if (ret) 648 return ret; 649 ret = kfifo_to_user(&fh->rawir, buffer, length, &copied); 650 mutex_unlock(&rcdev->lock); 651 if (ret) 652 return ret; 653 } while (copied == 0); 654 655 return copied; 656 } 657 658 static ssize_t ir_lirc_read_scancode(struct file *file, char __user *buffer, 659 size_t length) 660 { 661 struct lirc_fh *fh = file->private_data; 662 struct rc_dev *rcdev = fh->rc; 663 unsigned int copied; 664 int ret; 665 666 if (length < sizeof(struct lirc_scancode) || 667 length % sizeof(struct lirc_scancode)) 668 return -EINVAL; 669 670 do { 671 if (kfifo_is_empty(&fh->scancodes)) { 672 if (file->f_flags & O_NONBLOCK) 673 return -EAGAIN; 674 675 ret = wait_event_interruptible(fh->wait_poll, 676 !kfifo_is_empty(&fh->scancodes) || 677 !rcdev->registered); 678 if (ret) 679 return ret; 680 } 681 682 if (!rcdev->registered) 683 return -ENODEV; 684 685 ret = mutex_lock_interruptible(&rcdev->lock); 686 if (ret) 687 return ret; 688 ret = kfifo_to_user(&fh->scancodes, buffer, length, &copied); 689 mutex_unlock(&rcdev->lock); 690 if (ret) 691 return ret; 692 } while (copied == 0); 693 694 return copied; 695 } 696 697 static ssize_t ir_lirc_read(struct file *file, char __user *buffer, 698 size_t length, loff_t *ppos) 699 { 700 struct lirc_fh *fh = file->private_data; 701 struct rc_dev *rcdev = fh->rc; 702 703 if (rcdev->driver_type == RC_DRIVER_IR_RAW_TX) 704 return -EINVAL; 705 706 if (!rcdev->registered) 707 return -ENODEV; 708 709 if (fh->rec_mode == LIRC_MODE_MODE2) 710 return ir_lirc_read_mode2(file, buffer, length); 711 else /* LIRC_MODE_SCANCODE */ 712 return ir_lirc_read_scancode(file, buffer, length); 713 } 714 715 static const struct file_operations lirc_fops = { 716 .owner = THIS_MODULE, 717 .write = ir_lirc_transmit_ir, 718 .unlocked_ioctl = ir_lirc_ioctl, 719 #ifdef CONFIG_COMPAT 720 .compat_ioctl = ir_lirc_ioctl, 721 #endif 722 .read = ir_lirc_read, 723 .poll = ir_lirc_poll, 724 .open = ir_lirc_open, 725 .release = ir_lirc_close, 726 .llseek = no_llseek, 727 }; 728 729 static void lirc_release_device(struct device *ld) 730 { 731 struct rc_dev *rcdev = container_of(ld, struct rc_dev, lirc_dev); 732 733 put_device(&rcdev->dev); 734 } 735 736 int ir_lirc_register(struct rc_dev *dev) 737 { 738 int err, minor; 739 740 minor = ida_simple_get(&lirc_ida, 0, RC_DEV_MAX, GFP_KERNEL); 741 if (minor < 0) 742 return minor; 743 744 device_initialize(&dev->lirc_dev); 745 dev->lirc_dev.class = lirc_class; 746 dev->lirc_dev.parent = &dev->dev; 747 dev->lirc_dev.release = lirc_release_device; 748 dev->lirc_dev.devt = MKDEV(MAJOR(lirc_base_dev), minor); 749 dev_set_name(&dev->lirc_dev, "lirc%d", minor); 750 751 INIT_LIST_HEAD(&dev->lirc_fh); 752 spin_lock_init(&dev->lirc_fh_lock); 753 754 cdev_init(&dev->lirc_cdev, &lirc_fops); 755 756 err = cdev_device_add(&dev->lirc_cdev, &dev->lirc_dev); 757 if (err) 758 goto out_ida; 759 760 get_device(&dev->dev); 761 762 dev_info(&dev->dev, "lirc_dev: driver %s registered at minor = %d", 763 dev->driver_name, minor); 764 765 return 0; 766 767 out_ida: 768 ida_simple_remove(&lirc_ida, minor); 769 return err; 770 } 771 772 void ir_lirc_unregister(struct rc_dev *dev) 773 { 774 unsigned long flags; 775 struct lirc_fh *fh; 776 777 dev_dbg(&dev->dev, "lirc_dev: driver %s unregistered from minor = %d\n", 778 dev->driver_name, MINOR(dev->lirc_dev.devt)); 779 780 spin_lock_irqsave(&dev->lirc_fh_lock, flags); 781 list_for_each_entry(fh, &dev->lirc_fh, list) 782 wake_up_poll(&fh->wait_poll, POLLHUP | POLLERR); 783 spin_unlock_irqrestore(&dev->lirc_fh_lock, flags); 784 785 cdev_device_del(&dev->lirc_cdev, &dev->lirc_dev); 786 ida_simple_remove(&lirc_ida, MINOR(dev->lirc_dev.devt)); 787 } 788 789 int __init lirc_dev_init(void) 790 { 791 int retval; 792 793 lirc_class = class_create(THIS_MODULE, "lirc"); 794 if (IS_ERR(lirc_class)) { 795 pr_err("class_create failed\n"); 796 return PTR_ERR(lirc_class); 797 } 798 799 retval = alloc_chrdev_region(&lirc_base_dev, 0, RC_DEV_MAX, 800 "BaseRemoteCtl"); 801 if (retval) { 802 class_destroy(lirc_class); 803 pr_err("alloc_chrdev_region failed\n"); 804 return retval; 805 } 806 807 pr_info("IR Remote Control driver registered, major %d\n", 808 MAJOR(lirc_base_dev)); 809 810 return 0; 811 } 812 813 void __exit lirc_dev_exit(void) 814 { 815 class_destroy(lirc_class); 816 unregister_chrdev_region(lirc_base_dev, RC_DEV_MAX); 817 } 818