ir-sysfs.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. /* bit array to represent IR sysfs device number */
  19. static unsigned long ir_core_dev_number;
  20. /* class for /sys/class/irrcv */
  21. static struct class *ir_input_class;
  22. /**
  23. * show_protocol() - shows the current IR protocol
  24. * @d: the device descriptor
  25. * @mattr: the device attribute struct (unused)
  26. * @buf: a pointer to the output buffer
  27. *
  28. * This routine is a callback routine for input read the IR protocol type.
  29. * it is trigged by reading /sys/class/irrcv/irrcv?/current_protocol.
  30. * It returns the protocol name, as understood by the driver.
  31. */
  32. static ssize_t show_protocol(struct device *d,
  33. struct device_attribute *mattr, char *buf)
  34. {
  35. char *s;
  36. struct ir_input_dev *ir_dev = dev_get_drvdata(d);
  37. u64 ir_type = ir_dev->rc_tab.ir_type;
  38. IR_dprintk(1, "Current protocol is %lld\n", (long long)ir_type);
  39. /* FIXME: doesn't support multiple protocols at the same time */
  40. if (ir_type == IR_TYPE_UNKNOWN)
  41. s = "Unknown";
  42. else if (ir_type == IR_TYPE_RC5)
  43. s = "RC-5";
  44. else if (ir_type == IR_TYPE_PD)
  45. s = "Pulse/distance";
  46. else if (ir_type == IR_TYPE_NEC)
  47. s = "NEC";
  48. else
  49. s = "Other";
  50. return sprintf(buf, "%s\n", s);
  51. }
  52. /**
  53. * store_protocol() - shows the current IR protocol
  54. * @d: the device descriptor
  55. * @mattr: the device attribute struct (unused)
  56. * @buf: a pointer to the input buffer
  57. * @len: length of the input buffer
  58. *
  59. * This routine is a callback routine for changing the IR protocol type.
  60. * it is trigged by reading /sys/class/irrcv/irrcv?/current_protocol.
  61. * It changes the IR the protocol name, if the IR type is recognized
  62. * by the driver.
  63. * If an unknown protocol name is used, returns -EINVAL.
  64. */
  65. static ssize_t store_protocol(struct device *d,
  66. struct device_attribute *mattr,
  67. const char *data,
  68. size_t len)
  69. {
  70. struct ir_input_dev *ir_dev = dev_get_drvdata(d);
  71. u64 ir_type = IR_TYPE_UNKNOWN;
  72. int rc = -EINVAL;
  73. unsigned long flags;
  74. char *buf;
  75. buf = strsep((char **) &data, "\n");
  76. if (!strcasecmp(buf, "rc-5"))
  77. ir_type = IR_TYPE_RC5;
  78. else if (!strcasecmp(buf, "pd"))
  79. ir_type = IR_TYPE_PD;
  80. else if (!strcasecmp(buf, "nec"))
  81. ir_type = IR_TYPE_NEC;
  82. if (ir_type == IR_TYPE_UNKNOWN) {
  83. IR_dprintk(1, "Error setting protocol to %lld\n",
  84. (long long)ir_type);
  85. return -EINVAL;
  86. }
  87. if (ir_dev->props && ir_dev->props->change_protocol)
  88. rc = ir_dev->props->change_protocol(ir_dev->props->priv,
  89. ir_type);
  90. if (rc < 0) {
  91. IR_dprintk(1, "Error setting protocol to %lld\n",
  92. (long long)ir_type);
  93. return -EINVAL;
  94. }
  95. spin_lock_irqsave(&ir_dev->rc_tab.lock, flags);
  96. ir_dev->rc_tab.ir_type = ir_type;
  97. spin_unlock_irqrestore(&ir_dev->rc_tab.lock, flags);
  98. IR_dprintk(1, "Current protocol is %lld\n",
  99. (long long)ir_type);
  100. return len;
  101. }
  102. /*
  103. * Static device attribute struct with the sysfs attributes for IR's
  104. */
  105. static DEVICE_ATTR(current_protocol, S_IRUGO | S_IWUSR,
  106. show_protocol, store_protocol);
  107. static struct attribute *ir_dev_attrs[] = {
  108. &dev_attr_current_protocol.attr,
  109. NULL,
  110. };
  111. /**
  112. * ir_register_class() - creates the sysfs for /sys/class/irrcv/irrcv?
  113. * @input_dev: the struct input_dev descriptor of the device
  114. *
  115. * This routine is used to register the syfs code for IR class
  116. */
  117. int ir_register_class(struct input_dev *input_dev)
  118. {
  119. int rc;
  120. struct kobject *kobj;
  121. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  122. int devno = find_first_zero_bit(&ir_core_dev_number,
  123. IRRCV_NUM_DEVICES);
  124. if (unlikely(devno < 0))
  125. return devno;
  126. ir_dev->attr.attrs = ir_dev_attrs;
  127. ir_dev->class_dev = device_create(ir_input_class, NULL,
  128. input_dev->dev.devt, ir_dev,
  129. "irrcv%d", devno);
  130. kobj = &ir_dev->class_dev->kobj;
  131. printk(KERN_WARNING "Creating IR device %s\n", kobject_name(kobj));
  132. rc = sysfs_create_group(kobj, &ir_dev->attr);
  133. if (unlikely(rc < 0)) {
  134. device_destroy(ir_input_class, input_dev->dev.devt);
  135. return -ENOMEM;
  136. }
  137. ir_dev->devno = devno;
  138. set_bit(devno, &ir_core_dev_number);
  139. return 0;
  140. };
  141. /**
  142. * ir_unregister_class() - removes the sysfs for sysfs for
  143. * /sys/class/irrcv/irrcv?
  144. * @input_dev: the struct input_dev descriptor of the device
  145. *
  146. * This routine is used to unregister the syfs code for IR class
  147. */
  148. void ir_unregister_class(struct input_dev *input_dev)
  149. {
  150. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  151. struct kobject *kobj;
  152. clear_bit(ir_dev->devno, &ir_core_dev_number);
  153. kobj = &ir_dev->class_dev->kobj;
  154. sysfs_remove_group(kobj, &ir_dev->attr);
  155. device_destroy(ir_input_class, input_dev->dev.devt);
  156. kfree(ir_dev->attr.name);
  157. }
  158. /*
  159. * Init/exit code for the module. Basically, creates/removes /sys/class/irrcv
  160. */
  161. static int __init ir_core_init(void)
  162. {
  163. ir_input_class = class_create(THIS_MODULE, "irrcv");
  164. if (IS_ERR(ir_input_class)) {
  165. printk(KERN_ERR "ir_core: unable to register irrcv class\n");
  166. return PTR_ERR(ir_input_class);
  167. }
  168. return 0;
  169. }
  170. static void __exit ir_core_exit(void)
  171. {
  172. class_destroy(ir_input_class);
  173. }
  174. module_init(ir_core_init);
  175. module_exit(ir_core_exit);