ir-sysfs.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* ir-register.c - handle IR scancode->keycode tables
  2. *
  3. * Copyright (C) 2009-2010 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 char *ir_devnode(struct device *dev, mode_t *mode)
  23. {
  24. return kasprintf(GFP_KERNEL, "irrcv/%s", dev_name(dev));
  25. }
  26. static struct class ir_input_class = {
  27. .name = "irrcv",
  28. .devnode = ir_devnode,
  29. };
  30. /**
  31. * show_protocol() - shows the current IR protocol
  32. * @d: the device descriptor
  33. * @mattr: the device attribute struct (unused)
  34. * @buf: a pointer to the output buffer
  35. *
  36. * This routine is a callback routine for input read the IR protocol type.
  37. * it is trigged by reading /sys/class/irrcv/irrcv?/current_protocol.
  38. * It returns the protocol name, as understood by the driver.
  39. */
  40. static ssize_t show_protocol(struct device *d,
  41. struct device_attribute *mattr, char *buf)
  42. {
  43. char *s;
  44. struct ir_input_dev *ir_dev = dev_get_drvdata(d);
  45. u64 ir_type = ir_dev->rc_tab.ir_type;
  46. IR_dprintk(1, "Current protocol is %lld\n", (long long)ir_type);
  47. /* FIXME: doesn't support multiple protocols at the same time */
  48. if (ir_type == IR_TYPE_UNKNOWN)
  49. s = "Unknown";
  50. else if (ir_type == IR_TYPE_RC5)
  51. s = "RC-5";
  52. else if (ir_type == IR_TYPE_PD)
  53. s = "Pulse/distance";
  54. else if (ir_type == IR_TYPE_NEC)
  55. s = "NEC";
  56. else
  57. s = "Other";
  58. return sprintf(buf, "%s\n", s);
  59. }
  60. /**
  61. * store_protocol() - shows the current IR protocol
  62. * @d: the device descriptor
  63. * @mattr: the device attribute struct (unused)
  64. * @buf: a pointer to the input buffer
  65. * @len: length of the input buffer
  66. *
  67. * This routine is a callback routine for changing the IR protocol type.
  68. * it is trigged by reading /sys/class/irrcv/irrcv?/current_protocol.
  69. * It changes the IR the protocol name, if the IR type is recognized
  70. * by the driver.
  71. * If an unknown protocol name is used, returns -EINVAL.
  72. */
  73. static ssize_t store_protocol(struct device *d,
  74. struct device_attribute *mattr,
  75. const char *data,
  76. size_t len)
  77. {
  78. struct ir_input_dev *ir_dev = dev_get_drvdata(d);
  79. u64 ir_type = IR_TYPE_UNKNOWN;
  80. int rc = -EINVAL;
  81. unsigned long flags;
  82. char *buf;
  83. buf = strsep((char **) &data, "\n");
  84. if (!strcasecmp(buf, "rc-5") || !strcasecmp(buf, "rc5"))
  85. ir_type = IR_TYPE_RC5;
  86. else if (!strcasecmp(buf, "pd"))
  87. ir_type = IR_TYPE_PD;
  88. else if (!strcasecmp(buf, "nec"))
  89. ir_type = IR_TYPE_NEC;
  90. if (ir_type == IR_TYPE_UNKNOWN) {
  91. IR_dprintk(1, "Error setting protocol to %lld\n",
  92. (long long)ir_type);
  93. return -EINVAL;
  94. }
  95. if (ir_dev->props && ir_dev->props->change_protocol)
  96. rc = ir_dev->props->change_protocol(ir_dev->props->priv,
  97. ir_type);
  98. if (rc < 0) {
  99. IR_dprintk(1, "Error setting protocol to %lld\n",
  100. (long long)ir_type);
  101. return -EINVAL;
  102. }
  103. spin_lock_irqsave(&ir_dev->rc_tab.lock, flags);
  104. ir_dev->rc_tab.ir_type = ir_type;
  105. spin_unlock_irqrestore(&ir_dev->rc_tab.lock, flags);
  106. IR_dprintk(1, "Current protocol is %lld\n",
  107. (long long)ir_type);
  108. return len;
  109. }
  110. #define ADD_HOTPLUG_VAR(fmt, val...) \
  111. do { \
  112. int err = add_uevent_var(env, fmt, val); \
  113. if (err) \
  114. return err; \
  115. } while (0)
  116. static int ir_dev_uevent(struct device *device, struct kobj_uevent_env *env)
  117. {
  118. struct ir_input_dev *ir_dev = dev_get_drvdata(device);
  119. if (ir_dev->rc_tab.name)
  120. ADD_HOTPLUG_VAR("NAME=\"%s\"", ir_dev->rc_tab.name);
  121. if (ir_dev->driver_name)
  122. ADD_HOTPLUG_VAR("DRV_NAME=\"%s\"", ir_dev->driver_name);
  123. return 0;
  124. }
  125. /*
  126. * Static device attribute struct with the sysfs attributes for IR's
  127. */
  128. static DEVICE_ATTR(current_protocol, S_IRUGO | S_IWUSR,
  129. show_protocol, store_protocol);
  130. static struct attribute *ir_dev_attrs[] = {
  131. &dev_attr_current_protocol.attr,
  132. NULL,
  133. };
  134. static struct attribute_group ir_dev_attr_grp = {
  135. .attrs = ir_dev_attrs,
  136. };
  137. static const struct attribute_group *ir_dev_attr_groups[] = {
  138. &ir_dev_attr_grp,
  139. NULL
  140. };
  141. static struct device_type ir_dev_type = {
  142. .groups = ir_dev_attr_groups,
  143. .uevent = ir_dev_uevent,
  144. };
  145. /**
  146. * ir_register_class() - creates the sysfs for /sys/class/irrcv/irrcv?
  147. * @input_dev: the struct input_dev descriptor of the device
  148. *
  149. * This routine is used to register the syfs code for IR class
  150. */
  151. int ir_register_class(struct input_dev *input_dev)
  152. {
  153. int rc;
  154. const char *path;
  155. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  156. int devno = find_first_zero_bit(&ir_core_dev_number,
  157. IRRCV_NUM_DEVICES);
  158. if (unlikely(devno < 0))
  159. return devno;
  160. ir_dev->dev.type = &ir_dev_type;
  161. ir_dev->dev.class = &ir_input_class;
  162. ir_dev->dev.parent = input_dev->dev.parent;
  163. dev_set_name(&ir_dev->dev, "irrcv%d", devno);
  164. dev_set_drvdata(&ir_dev->dev, ir_dev);
  165. rc = device_register(&ir_dev->dev);
  166. if (rc)
  167. return rc;
  168. input_dev->dev.parent = &ir_dev->dev;
  169. rc = input_register_device(input_dev);
  170. if (rc < 0) {
  171. device_del(&ir_dev->dev);
  172. return rc;
  173. }
  174. __module_get(THIS_MODULE);
  175. path = kobject_get_path(&ir_dev->dev.kobj, GFP_KERNEL);
  176. printk(KERN_INFO "%s: %s as %s\n",
  177. dev_name(&ir_dev->dev),
  178. input_dev->name ? input_dev->name : "Unspecified device",
  179. path ? path : "N/A");
  180. kfree(path);
  181. ir_dev->devno = devno;
  182. set_bit(devno, &ir_core_dev_number);
  183. return 0;
  184. };
  185. /**
  186. * ir_unregister_class() - removes the sysfs for sysfs for
  187. * /sys/class/irrcv/irrcv?
  188. * @input_dev: the struct input_dev descriptor of the device
  189. *
  190. * This routine is used to unregister the syfs code for IR class
  191. */
  192. void ir_unregister_class(struct input_dev *input_dev)
  193. {
  194. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  195. clear_bit(ir_dev->devno, &ir_core_dev_number);
  196. input_unregister_device(input_dev);
  197. device_del(&ir_dev->dev);
  198. module_put(THIS_MODULE);
  199. }
  200. /*
  201. * Init/exit code for the module. Basically, creates/removes /sys/class/irrcv
  202. */
  203. static int __init ir_core_init(void)
  204. {
  205. int rc = class_register(&ir_input_class);
  206. if (rc) {
  207. printk(KERN_ERR "ir_core: unable to register irrcv class\n");
  208. return rc;
  209. }
  210. /* Initialize/load the decoders that will be used */
  211. ir_raw_init();
  212. return 0;
  213. }
  214. static void __exit ir_core_exit(void)
  215. {
  216. class_unregister(&ir_input_class);
  217. }
  218. module_init(ir_core_init);
  219. module_exit(ir_core_exit);