ir-sysfs.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* ir-register.c - handle IR scancode->keycode tables
  2. *
  3. * Copyright (C) 2009 by Mauro Carvalho Chehab <mchehab@redhat.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation version 2 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/input.h>
  15. #include <linux/device.h>
  16. #include <media/ir-core.h>
  17. #define IRRCV_NUM_DEVICES 256
  18. unsigned long ir_core_dev_number;
  19. static struct class *ir_input_class;
  20. static ssize_t show_protocol(struct device *d,
  21. struct device_attribute *mattr, char *buf)
  22. {
  23. char *s;
  24. struct ir_input_dev *ir_dev = dev_get_drvdata(d);
  25. enum ir_type ir_type = ir_dev->rc_tab.ir_type;
  26. IR_dprintk(1, "Current protocol is %ld\n", ir_type);
  27. /* FIXME: doesn't support multiple protocols at the same time */
  28. if (ir_type == IR_TYPE_UNKNOWN)
  29. s = "Unknown";
  30. else if (ir_type == IR_TYPE_RC5)
  31. s = "RC-5";
  32. else if (ir_type == IR_TYPE_PD)
  33. s = "Pulse/distance";
  34. else if (ir_type == IR_TYPE_NEC)
  35. s = "NEC";
  36. else
  37. s = "Other";
  38. return sprintf(buf, "%s\n", s);
  39. }
  40. static ssize_t store_protocol(struct device *d,
  41. struct device_attribute *mattr,
  42. const char *data,
  43. size_t len)
  44. {
  45. struct ir_input_dev *ir_dev = dev_get_drvdata(d);
  46. enum ir_type ir_type = IR_TYPE_UNKNOWN;
  47. int rc = -EINVAL;
  48. char *buf;
  49. buf = strsep((char **) &data, "\n");
  50. if (!strcasecmp(buf, "rc-5"))
  51. ir_type = IR_TYPE_RC5;
  52. else if (!strcasecmp(buf, "pd"))
  53. ir_type = IR_TYPE_PD;
  54. else if (!strcasecmp(buf, "nec"))
  55. ir_type = IR_TYPE_NEC;
  56. if (ir_type == IR_TYPE_UNKNOWN) {
  57. IR_dprintk(1, "Error setting protocol to %ld\n", ir_type);
  58. return -EINVAL;
  59. }
  60. if (ir_dev->props->change_protocol)
  61. rc = ir_dev->props->change_protocol(ir_dev->props->priv,
  62. ir_type);
  63. if (rc < 0) {
  64. IR_dprintk(1, "Error setting protocol to %ld\n", ir_type);
  65. return -EINVAL;
  66. }
  67. ir_dev->rc_tab.ir_type = ir_type;
  68. IR_dprintk(1, "Current protocol is %ld\n", ir_type);
  69. return len;
  70. }
  71. static DEVICE_ATTR(current_protocol, S_IRUGO | S_IWUSR,
  72. show_protocol, store_protocol);
  73. static struct attribute *ir_dev_attrs[] = {
  74. &dev_attr_current_protocol.attr,
  75. };
  76. int ir_register_class(struct input_dev *input_dev)
  77. {
  78. int rc;
  79. struct kobject *kobj;
  80. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  81. int devno = find_first_zero_bit(&ir_core_dev_number,
  82. IRRCV_NUM_DEVICES);
  83. if (unlikely(devno < 0))
  84. return devno;
  85. ir_dev->attr.attrs = ir_dev_attrs;
  86. ir_dev->class_dev = device_create(ir_input_class, NULL,
  87. input_dev->dev.devt, ir_dev,
  88. "irrcv%d", devno);
  89. kobj = &ir_dev->class_dev->kobj;
  90. printk(KERN_WARNING "Creating IR device %s\n", kobject_name(kobj));
  91. rc = sysfs_create_group(kobj, &ir_dev->attr);
  92. if (unlikely(rc < 0)) {
  93. device_destroy(ir_input_class, input_dev->dev.devt);
  94. return -ENOMEM;
  95. }
  96. ir_dev->devno = devno;
  97. set_bit(devno, &ir_core_dev_number);
  98. return 0;
  99. };
  100. void ir_unregister_class(struct input_dev *input_dev)
  101. {
  102. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  103. struct kobject *kobj;
  104. clear_bit(ir_dev->devno, &ir_core_dev_number);
  105. kobj = &ir_dev->class_dev->kobj;
  106. sysfs_remove_group(kobj, &ir_dev->attr);
  107. device_destroy(ir_input_class, input_dev->dev.devt);
  108. kfree(ir_dev->attr.name);
  109. }
  110. static int __init ir_core_init(void)
  111. {
  112. ir_input_class = class_create(THIS_MODULE, "irrcv");
  113. if (IS_ERR(ir_input_class)) {
  114. printk(KERN_ERR "ir_core: unable to register irrcv class\n");
  115. return PTR_ERR(ir_input_class);
  116. }
  117. return 0;
  118. }
  119. static void __exit ir_core_exit(void)
  120. {
  121. class_destroy(ir_input_class);
  122. }
  123. module_init(ir_core_init);
  124. module_exit(ir_core_exit);