ir-sysfs.c 8.3 KB

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