ir-sysfs.c 5.6 KB

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