lirc_dev.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/device.h>
  21. #include <linux/idr.h>
  22. #include <linux/poll.h>
  23. #include "rc-core-priv.h"
  24. #include <media/lirc.h>
  25. #define LOGHEAD "lirc_dev (%s[%d]): "
  26. static dev_t lirc_base_dev;
  27. /* Used to keep track of allocated lirc devices */
  28. static DEFINE_IDA(lirc_ida);
  29. /* Only used for sysfs but defined to void otherwise */
  30. static struct class *lirc_class;
  31. static void lirc_release_device(struct device *ld)
  32. {
  33. struct rc_dev *rcdev = container_of(ld, struct rc_dev, lirc_dev);
  34. if (rcdev->driver_type == RC_DRIVER_IR_RAW)
  35. kfifo_free(&rcdev->rawir);
  36. put_device(&rcdev->dev);
  37. }
  38. int ir_lirc_register(struct rc_dev *dev)
  39. {
  40. int err, minor;
  41. device_initialize(&dev->lirc_dev);
  42. dev->lirc_dev.class = lirc_class;
  43. dev->lirc_dev.release = lirc_release_device;
  44. dev->send_mode = LIRC_MODE_PULSE;
  45. if (dev->driver_type == RC_DRIVER_IR_RAW) {
  46. if (kfifo_alloc(&dev->rawir, MAX_IR_EVENT_SIZE, GFP_KERNEL))
  47. return -ENOMEM;
  48. }
  49. init_waitqueue_head(&dev->wait_poll);
  50. minor = ida_simple_get(&lirc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
  51. if (minor < 0) {
  52. err = minor;
  53. goto out_kfifo;
  54. }
  55. dev->lirc_dev.parent = &dev->dev;
  56. dev->lirc_dev.devt = MKDEV(MAJOR(lirc_base_dev), minor);
  57. dev_set_name(&dev->lirc_dev, "lirc%d", minor);
  58. cdev_init(&dev->lirc_cdev, &lirc_fops);
  59. err = cdev_device_add(&dev->lirc_cdev, &dev->lirc_dev);
  60. if (err)
  61. goto out_ida;
  62. get_device(&dev->dev);
  63. dev_info(&dev->dev, "lirc_dev: driver %s registered at minor = %d",
  64. dev->driver_name, minor);
  65. return 0;
  66. out_ida:
  67. ida_simple_remove(&lirc_ida, minor);
  68. out_kfifo:
  69. if (dev->driver_type == RC_DRIVER_IR_RAW)
  70. kfifo_free(&dev->rawir);
  71. return err;
  72. }
  73. void ir_lirc_unregister(struct rc_dev *dev)
  74. {
  75. dev_dbg(&dev->dev, "lirc_dev: driver %s unregistered from minor = %d\n",
  76. dev->driver_name, MINOR(dev->lirc_dev.devt));
  77. mutex_lock(&dev->lock);
  78. if (dev->lirc_open) {
  79. dev_dbg(&dev->dev, LOGHEAD "releasing opened driver\n",
  80. dev->driver_name, MINOR(dev->lirc_dev.devt));
  81. wake_up_poll(&dev->wait_poll, POLLHUP);
  82. }
  83. mutex_unlock(&dev->lock);
  84. cdev_device_del(&dev->lirc_cdev, &dev->lirc_dev);
  85. ida_simple_remove(&lirc_ida, MINOR(dev->lirc_dev.devt));
  86. put_device(&dev->lirc_dev);
  87. }
  88. int __init lirc_dev_init(void)
  89. {
  90. int retval;
  91. lirc_class = class_create(THIS_MODULE, "lirc");
  92. if (IS_ERR(lirc_class)) {
  93. pr_err("class_create failed\n");
  94. return PTR_ERR(lirc_class);
  95. }
  96. retval = alloc_chrdev_region(&lirc_base_dev, 0, RC_DEV_MAX,
  97. "BaseRemoteCtl");
  98. if (retval) {
  99. class_destroy(lirc_class);
  100. pr_err("alloc_chrdev_region failed\n");
  101. return retval;
  102. }
  103. pr_info("IR Remote Control driver registered, major %d\n",
  104. MAJOR(lirc_base_dev));
  105. return 0;
  106. }
  107. void __exit lirc_dev_exit(void)
  108. {
  109. class_destroy(lirc_class);
  110. unregister_chrdev_region(lirc_base_dev, RC_DEV_MAX);
  111. }