ir-sysfs.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. static struct {
  31. u64 type;
  32. char *name;
  33. } proto_names[] = {
  34. { IR_TYPE_UNKNOWN, "unknown" },
  35. { IR_TYPE_RC5, "rc-5" },
  36. { IR_TYPE_NEC, "nec" },
  37. { IR_TYPE_RC6, "rc-6" },
  38. { IR_TYPE_JVC, "jvc" },
  39. { IR_TYPE_SONY, "sony" },
  40. { IR_TYPE_LIRC, "lirc" },
  41. };
  42. #define PROTO_NONE "none"
  43. /**
  44. * show_protocols() - shows the current IR protocol(s)
  45. * @d: the device descriptor
  46. * @mattr: the device attribute struct (unused)
  47. * @buf: a pointer to the output buffer
  48. *
  49. * This routine is a callback routine for input read the IR protocol type(s).
  50. * it is trigged by reading /sys/class/rc/rc?/protocols.
  51. * It returns the protocol names of supported protocols.
  52. * Enabled protocols are printed in brackets.
  53. */
  54. static ssize_t show_protocols(struct device *d,
  55. struct device_attribute *mattr, char *buf)
  56. {
  57. struct ir_input_dev *ir_dev = dev_get_drvdata(d);
  58. u64 allowed, enabled;
  59. char *tmp = buf;
  60. int i;
  61. if (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_SCANCODE) {
  62. enabled = ir_dev->rc_tab.ir_type;
  63. allowed = ir_dev->props->allowed_protos;
  64. } else if (ir_dev->raw) {
  65. enabled = ir_dev->raw->enabled_protocols;
  66. allowed = ir_raw_get_allowed_protocols();
  67. } else
  68. return sprintf(tmp, "[builtin]\n");
  69. IR_dprintk(1, "allowed - 0x%llx, enabled - 0x%llx\n",
  70. (long long)allowed,
  71. (long long)enabled);
  72. for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
  73. if (allowed & enabled & proto_names[i].type)
  74. tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
  75. else if (allowed & proto_names[i].type)
  76. tmp += sprintf(tmp, "%s ", proto_names[i].name);
  77. }
  78. if (tmp != buf)
  79. tmp--;
  80. *tmp = '\n';
  81. return tmp + 1 - buf;
  82. }
  83. /**
  84. * store_protocols() - changes the current IR protocol(s)
  85. * @d: the device descriptor
  86. * @mattr: the device attribute struct (unused)
  87. * @buf: a pointer to the input buffer
  88. * @len: length of the input buffer
  89. *
  90. * This routine is a callback routine for changing the IR protocol type.
  91. * It is trigged by writing to /sys/class/rc/rc?/protocols.
  92. * Writing "+proto" will add a protocol to the list of enabled protocols.
  93. * Writing "-proto" will remove a protocol from the list of enabled protocols.
  94. * Writing "proto" will enable only "proto".
  95. * Writing "none" will disable all protocols.
  96. * Returns -EINVAL if an invalid protocol combination or unknown protocol name
  97. * is used, otherwise @len.
  98. */
  99. static ssize_t store_protocols(struct device *d,
  100. struct device_attribute *mattr,
  101. const char *data,
  102. size_t len)
  103. {
  104. struct ir_input_dev *ir_dev = dev_get_drvdata(d);
  105. bool enable, disable;
  106. const char *tmp;
  107. u64 type;
  108. u64 mask;
  109. int rc, i, count = 0;
  110. unsigned long flags;
  111. if (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_SCANCODE)
  112. type = ir_dev->rc_tab.ir_type;
  113. else if (ir_dev->raw)
  114. type = ir_dev->raw->enabled_protocols;
  115. else {
  116. IR_dprintk(1, "Protocol switching not supported\n");
  117. return -EINVAL;
  118. }
  119. while ((tmp = strsep((char **) &data, " \n")) != NULL) {
  120. if (!*tmp)
  121. break;
  122. if (*tmp == '+') {
  123. enable = true;
  124. disable = false;
  125. tmp++;
  126. } else if (*tmp == '-') {
  127. enable = false;
  128. disable = true;
  129. tmp++;
  130. } else {
  131. enable = false;
  132. disable = false;
  133. }
  134. if (!enable && !disable && !strncasecmp(tmp, PROTO_NONE, sizeof(PROTO_NONE))) {
  135. tmp += sizeof(PROTO_NONE);
  136. mask = 0;
  137. count++;
  138. } else {
  139. for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
  140. if (!strncasecmp(tmp, proto_names[i].name, strlen(proto_names[i].name))) {
  141. tmp += strlen(proto_names[i].name);
  142. mask = proto_names[i].type;
  143. break;
  144. }
  145. }
  146. if (i == ARRAY_SIZE(proto_names)) {
  147. IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
  148. return -EINVAL;
  149. }
  150. count++;
  151. }
  152. if (enable)
  153. type |= mask;
  154. else if (disable)
  155. type &= ~mask;
  156. else
  157. type = mask;
  158. }
  159. if (!count) {
  160. IR_dprintk(1, "Protocol not specified\n");
  161. return -EINVAL;
  162. }
  163. if (ir_dev->props && ir_dev->props->change_protocol) {
  164. rc = ir_dev->props->change_protocol(ir_dev->props->priv,
  165. type);
  166. if (rc < 0) {
  167. IR_dprintk(1, "Error setting protocols to 0x%llx\n",
  168. (long long)type);
  169. return -EINVAL;
  170. }
  171. }
  172. if (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_SCANCODE) {
  173. spin_lock_irqsave(&ir_dev->rc_tab.lock, flags);
  174. ir_dev->rc_tab.ir_type = type;
  175. spin_unlock_irqrestore(&ir_dev->rc_tab.lock, flags);
  176. } else {
  177. ir_dev->raw->enabled_protocols = type;
  178. }
  179. IR_dprintk(1, "Current protocol(s): 0x%llx\n",
  180. (long long)type);
  181. return len;
  182. }
  183. #define ADD_HOTPLUG_VAR(fmt, val...) \
  184. do { \
  185. int err = add_uevent_var(env, fmt, val); \
  186. if (err) \
  187. return err; \
  188. } while (0)
  189. static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
  190. {
  191. struct ir_input_dev *ir_dev = dev_get_drvdata(device);
  192. if (ir_dev->rc_tab.name)
  193. ADD_HOTPLUG_VAR("NAME=%s", ir_dev->rc_tab.name);
  194. if (ir_dev->driver_name)
  195. ADD_HOTPLUG_VAR("DRV_NAME=%s", ir_dev->driver_name);
  196. return 0;
  197. }
  198. /*
  199. * Static device attribute struct with the sysfs attributes for IR's
  200. */
  201. static DEVICE_ATTR(protocols, S_IRUGO | S_IWUSR,
  202. show_protocols, store_protocols);
  203. static struct attribute *rc_dev_attrs[] = {
  204. &dev_attr_protocols.attr,
  205. NULL,
  206. };
  207. static struct attribute_group rc_dev_attr_grp = {
  208. .attrs = rc_dev_attrs,
  209. };
  210. static const struct attribute_group *rc_dev_attr_groups[] = {
  211. &rc_dev_attr_grp,
  212. NULL
  213. };
  214. static struct device_type rc_dev_type = {
  215. .groups = rc_dev_attr_groups,
  216. .uevent = rc_dev_uevent,
  217. };
  218. /**
  219. * ir_register_class() - creates the sysfs for /sys/class/rc/rc?
  220. * @input_dev: the struct input_dev descriptor of the device
  221. *
  222. * This routine is used to register the syfs code for IR class
  223. */
  224. int ir_register_class(struct input_dev *input_dev)
  225. {
  226. int rc;
  227. const char *path;
  228. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  229. int devno = find_first_zero_bit(&ir_core_dev_number,
  230. IRRCV_NUM_DEVICES);
  231. if (unlikely(devno < 0))
  232. return devno;
  233. ir_dev->dev.type = &rc_dev_type;
  234. ir_dev->dev.class = &ir_input_class;
  235. ir_dev->dev.parent = input_dev->dev.parent;
  236. dev_set_name(&ir_dev->dev, "rc%d", devno);
  237. dev_set_drvdata(&ir_dev->dev, ir_dev);
  238. rc = device_register(&ir_dev->dev);
  239. if (rc)
  240. return rc;
  241. input_dev->dev.parent = &ir_dev->dev;
  242. rc = input_register_device(input_dev);
  243. if (rc < 0) {
  244. device_del(&ir_dev->dev);
  245. return rc;
  246. }
  247. __module_get(THIS_MODULE);
  248. path = kobject_get_path(&ir_dev->dev.kobj, GFP_KERNEL);
  249. printk(KERN_INFO "%s: %s as %s\n",
  250. dev_name(&ir_dev->dev),
  251. input_dev->name ? input_dev->name : "Unspecified device",
  252. path ? path : "N/A");
  253. kfree(path);
  254. ir_dev->devno = devno;
  255. set_bit(devno, &ir_core_dev_number);
  256. return 0;
  257. };
  258. /**
  259. * ir_unregister_class() - removes the sysfs for sysfs for
  260. * /sys/class/rc/rc?
  261. * @input_dev: the struct input_dev descriptor of the device
  262. *
  263. * This routine is used to unregister the syfs code for IR class
  264. */
  265. void ir_unregister_class(struct input_dev *input_dev)
  266. {
  267. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  268. clear_bit(ir_dev->devno, &ir_core_dev_number);
  269. input_unregister_device(input_dev);
  270. device_del(&ir_dev->dev);
  271. module_put(THIS_MODULE);
  272. }
  273. /*
  274. * Init/exit code for the module. Basically, creates/removes /sys/class/rc
  275. */
  276. static int __init ir_core_init(void)
  277. {
  278. int rc = class_register(&ir_input_class);
  279. if (rc) {
  280. printk(KERN_ERR "ir_core: unable to register rc class\n");
  281. return rc;
  282. }
  283. /* Initialize/load the decoders/keymap code that will be used */
  284. ir_raw_init();
  285. ir_rcmap_init();
  286. return 0;
  287. }
  288. static void __exit ir_core_exit(void)
  289. {
  290. class_unregister(&ir_input_class);
  291. ir_rcmap_cleanup();
  292. }
  293. module_init(ir_core_init);
  294. module_exit(ir_core_exit);