ir-sysfs.c 8.3 KB

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