ir-sysfs.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* ir-sysfs.c - sysfs interface for RC devices (/sys/class/rc)
  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 "ir-core-priv.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/rc */
  22. static char *ir_devnode(struct device *dev, mode_t *mode)
  23. {
  24. return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
  25. }
  26. static struct class ir_input_class = {
  27. .name = "rc",
  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/rc/rc?/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_NEC)
  53. s = "nec";
  54. else if (ir_type == IR_TYPE_RC6)
  55. s = "rc6";
  56. else if (ir_type == IR_TYPE_JVC)
  57. s = "jvc";
  58. else if (ir_type == IR_TYPE_SONY)
  59. s = "sony";
  60. else
  61. s = "other";
  62. return sprintf(buf, "%s\n", s);
  63. }
  64. /**
  65. * store_protocol() - shows the current IR protocol
  66. * @d: the device descriptor
  67. * @mattr: the device attribute struct (unused)
  68. * @buf: a pointer to the input buffer
  69. * @len: length of the input buffer
  70. *
  71. * This routine is a callback routine for changing the IR protocol type.
  72. * it is trigged by reading /sys/class/rc/rc?/current_protocol.
  73. * It changes the IR the protocol name, if the IR type is recognized
  74. * by the driver.
  75. * If an unknown protocol name is used, returns -EINVAL.
  76. */
  77. static ssize_t store_protocol(struct device *d,
  78. struct device_attribute *mattr,
  79. const char *data,
  80. size_t len)
  81. {
  82. struct ir_input_dev *ir_dev = dev_get_drvdata(d);
  83. u64 ir_type = 0;
  84. int rc = -EINVAL;
  85. unsigned long flags;
  86. char *buf;
  87. while ((buf = strsep((char **) &data, " \n")) != NULL) {
  88. if (!strcasecmp(buf, "rc-5") || !strcasecmp(buf, "rc5"))
  89. ir_type |= IR_TYPE_RC5;
  90. if (!strcasecmp(buf, "nec"))
  91. ir_type |= IR_TYPE_NEC;
  92. if (!strcasecmp(buf, "jvc"))
  93. ir_type |= IR_TYPE_JVC;
  94. if (!strcasecmp(buf, "sony"))
  95. ir_type |= IR_TYPE_SONY;
  96. }
  97. if (!ir_type) {
  98. IR_dprintk(1, "Unknown protocol\n");
  99. return -EINVAL;
  100. }
  101. if (ir_dev->props && ir_dev->props->change_protocol)
  102. rc = ir_dev->props->change_protocol(ir_dev->props->priv,
  103. ir_type);
  104. if (rc < 0) {
  105. IR_dprintk(1, "Error setting protocol to %lld\n",
  106. (long long)ir_type);
  107. return -EINVAL;
  108. }
  109. spin_lock_irqsave(&ir_dev->rc_tab.lock, flags);
  110. ir_dev->rc_tab.ir_type = ir_type;
  111. spin_unlock_irqrestore(&ir_dev->rc_tab.lock, flags);
  112. IR_dprintk(1, "Current protocol(s) is(are) %lld\n",
  113. (long long)ir_type);
  114. return len;
  115. }
  116. static ssize_t show_supported_protocols(struct device *d,
  117. struct device_attribute *mattr, char *buf)
  118. {
  119. char *orgbuf = buf;
  120. struct ir_input_dev *ir_dev = dev_get_drvdata(d);
  121. /* FIXME: doesn't support multiple protocols at the same time */
  122. if (ir_dev->props->allowed_protos == IR_TYPE_UNKNOWN)
  123. buf += sprintf(buf, "unknown ");
  124. if (ir_dev->props->allowed_protos & IR_TYPE_RC5)
  125. buf += sprintf(buf, "rc-5 ");
  126. if (ir_dev->props->allowed_protos & IR_TYPE_NEC)
  127. buf += sprintf(buf, "nec ");
  128. if (buf == orgbuf)
  129. buf += sprintf(buf, "other ");
  130. buf += sprintf(buf - 1, "\n");
  131. return buf - orgbuf;
  132. }
  133. #define ADD_HOTPLUG_VAR(fmt, val...) \
  134. do { \
  135. int err = add_uevent_var(env, fmt, val); \
  136. if (err) \
  137. return err; \
  138. } while (0)
  139. static int ir_dev_uevent(struct device *device, struct kobj_uevent_env *env)
  140. {
  141. struct ir_input_dev *ir_dev = dev_get_drvdata(device);
  142. if (ir_dev->rc_tab.name)
  143. ADD_HOTPLUG_VAR("NAME=%s", ir_dev->rc_tab.name);
  144. if (ir_dev->driver_name)
  145. ADD_HOTPLUG_VAR("DRV_NAME=%s", ir_dev->driver_name);
  146. return 0;
  147. }
  148. /*
  149. * Static device attribute struct with the sysfs attributes for IR's
  150. */
  151. static DEVICE_ATTR(protocol, S_IRUGO | S_IWUSR,
  152. show_protocol, store_protocol);
  153. static DEVICE_ATTR(supported_protocols, S_IRUGO | S_IWUSR,
  154. show_supported_protocols, NULL);
  155. static struct attribute *ir_hw_dev_attrs[] = {
  156. &dev_attr_protocol.attr,
  157. &dev_attr_supported_protocols.attr,
  158. NULL,
  159. };
  160. static struct attribute_group ir_hw_dev_attr_grp = {
  161. .attrs = ir_hw_dev_attrs,
  162. };
  163. static const struct attribute_group *ir_hw_dev_attr_groups[] = {
  164. &ir_hw_dev_attr_grp,
  165. NULL
  166. };
  167. static struct device_type rc_dev_type = {
  168. .groups = ir_hw_dev_attr_groups,
  169. .uevent = ir_dev_uevent,
  170. };
  171. static struct device_type ir_raw_dev_type = {
  172. .uevent = ir_dev_uevent,
  173. };
  174. /**
  175. * ir_register_class() - creates the sysfs for /sys/class/rc/rc?
  176. * @input_dev: the struct input_dev descriptor of the device
  177. *
  178. * This routine is used to register the syfs code for IR class
  179. */
  180. int ir_register_class(struct input_dev *input_dev)
  181. {
  182. int rc;
  183. const char *path;
  184. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  185. int devno = find_first_zero_bit(&ir_core_dev_number,
  186. IRRCV_NUM_DEVICES);
  187. if (unlikely(devno < 0))
  188. return devno;
  189. if (ir_dev->props->driver_type == RC_DRIVER_SCANCODE)
  190. ir_dev->dev.type = &rc_dev_type;
  191. else
  192. ir_dev->dev.type = &ir_raw_dev_type;
  193. ir_dev->dev.class = &ir_input_class;
  194. ir_dev->dev.parent = input_dev->dev.parent;
  195. dev_set_name(&ir_dev->dev, "rc%d", devno);
  196. dev_set_drvdata(&ir_dev->dev, ir_dev);
  197. rc = device_register(&ir_dev->dev);
  198. if (rc)
  199. return rc;
  200. input_dev->dev.parent = &ir_dev->dev;
  201. rc = input_register_device(input_dev);
  202. if (rc < 0) {
  203. device_del(&ir_dev->dev);
  204. return rc;
  205. }
  206. __module_get(THIS_MODULE);
  207. path = kobject_get_path(&ir_dev->dev.kobj, GFP_KERNEL);
  208. printk(KERN_INFO "%s: %s as %s\n",
  209. dev_name(&ir_dev->dev),
  210. input_dev->name ? input_dev->name : "Unspecified device",
  211. path ? path : "N/A");
  212. kfree(path);
  213. ir_dev->devno = devno;
  214. set_bit(devno, &ir_core_dev_number);
  215. return 0;
  216. };
  217. /**
  218. * ir_unregister_class() - removes the sysfs for sysfs for
  219. * /sys/class/rc/rc?
  220. * @input_dev: the struct input_dev descriptor of the device
  221. *
  222. * This routine is used to unregister the syfs code for IR class
  223. */
  224. void ir_unregister_class(struct input_dev *input_dev)
  225. {
  226. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  227. clear_bit(ir_dev->devno, &ir_core_dev_number);
  228. input_unregister_device(input_dev);
  229. device_del(&ir_dev->dev);
  230. module_put(THIS_MODULE);
  231. }
  232. /*
  233. * Init/exit code for the module. Basically, creates/removes /sys/class/rc
  234. */
  235. static int __init ir_core_init(void)
  236. {
  237. int rc = class_register(&ir_input_class);
  238. if (rc) {
  239. printk(KERN_ERR "ir_core: unable to register rc class\n");
  240. return rc;
  241. }
  242. /* Initialize/load the decoders/keymap code that will be used */
  243. ir_raw_init();
  244. return 0;
  245. }
  246. static void __exit ir_core_exit(void)
  247. {
  248. class_unregister(&ir_input_class);
  249. }
  250. module_init(ir_core_init);
  251. module_exit(ir_core_exit);