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;
  108. unsigned long flags;
  109. tmp = skip_spaces(data);
  110. if (*tmp == '\0') {
  111. IR_dprintk(1, "Protocol not specified\n");
  112. return -EINVAL;
  113. } else if (*tmp == '+') {
  114. enable = true;
  115. disable = false;
  116. tmp++;
  117. } else if (*tmp == '-') {
  118. enable = false;
  119. disable = true;
  120. tmp++;
  121. } else {
  122. enable = false;
  123. disable = false;
  124. }
  125. if (!enable && !disable && !strncasecmp(tmp, PROTO_NONE, sizeof(PROTO_NONE))) {
  126. mask = 0;
  127. tmp += sizeof(PROTO_NONE);
  128. } else {
  129. for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
  130. if (!strncasecmp(tmp, proto_names[i].name, strlen(proto_names[i].name))) {
  131. tmp += strlen(proto_names[i].name);
  132. mask = proto_names[i].type;
  133. break;
  134. }
  135. }
  136. if (i == ARRAY_SIZE(proto_names)) {
  137. IR_dprintk(1, "Unknown protocol\n");
  138. return -EINVAL;
  139. }
  140. }
  141. tmp = skip_spaces(tmp);
  142. if (*tmp != '\0') {
  143. IR_dprintk(1, "Invalid trailing characters\n");
  144. return -EINVAL;
  145. }
  146. if (ir_dev->props->driver_type == RC_DRIVER_SCANCODE)
  147. type = ir_dev->rc_tab.ir_type;
  148. else
  149. type = ir_dev->raw->enabled_protocols;
  150. if (enable)
  151. type |= mask;
  152. else if (disable)
  153. type &= ~mask;
  154. else
  155. type = mask;
  156. if (ir_dev->props && ir_dev->props->change_protocol) {
  157. rc = ir_dev->props->change_protocol(ir_dev->props->priv,
  158. type);
  159. if (rc < 0) {
  160. IR_dprintk(1, "Error setting protocols to 0x%llx\n",
  161. (long long)type);
  162. return -EINVAL;
  163. }
  164. }
  165. if (ir_dev->props->driver_type == RC_DRIVER_SCANCODE) {
  166. spin_lock_irqsave(&ir_dev->rc_tab.lock, flags);
  167. ir_dev->rc_tab.ir_type = type;
  168. spin_unlock_irqrestore(&ir_dev->rc_tab.lock, flags);
  169. } else {
  170. ir_dev->raw->enabled_protocols = type;
  171. }
  172. IR_dprintk(1, "Current protocol(s): 0x%llx\n",
  173. (long long)type);
  174. return len;
  175. }
  176. #define ADD_HOTPLUG_VAR(fmt, val...) \
  177. do { \
  178. int err = add_uevent_var(env, fmt, val); \
  179. if (err) \
  180. return err; \
  181. } while (0)
  182. static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
  183. {
  184. struct ir_input_dev *ir_dev = dev_get_drvdata(device);
  185. if (ir_dev->rc_tab.name)
  186. ADD_HOTPLUG_VAR("NAME=%s", ir_dev->rc_tab.name);
  187. if (ir_dev->driver_name)
  188. ADD_HOTPLUG_VAR("DRV_NAME=%s", ir_dev->driver_name);
  189. return 0;
  190. }
  191. /*
  192. * Static device attribute struct with the sysfs attributes for IR's
  193. */
  194. static DEVICE_ATTR(protocols, S_IRUGO | S_IWUSR,
  195. show_protocols, store_protocols);
  196. static struct attribute *rc_dev_attrs[] = {
  197. &dev_attr_protocols.attr,
  198. NULL,
  199. };
  200. static struct attribute_group rc_dev_attr_grp = {
  201. .attrs = rc_dev_attrs,
  202. };
  203. static const struct attribute_group *rc_dev_attr_groups[] = {
  204. &rc_dev_attr_grp,
  205. NULL
  206. };
  207. static struct device_type rc_dev_type = {
  208. .groups = rc_dev_attr_groups,
  209. .uevent = rc_dev_uevent,
  210. };
  211. /**
  212. * ir_register_class() - creates the sysfs for /sys/class/rc/rc?
  213. * @input_dev: the struct input_dev descriptor of the device
  214. *
  215. * This routine is used to register the syfs code for IR class
  216. */
  217. int ir_register_class(struct input_dev *input_dev)
  218. {
  219. int rc;
  220. const char *path;
  221. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  222. int devno = find_first_zero_bit(&ir_core_dev_number,
  223. IRRCV_NUM_DEVICES);
  224. if (unlikely(devno < 0))
  225. return devno;
  226. ir_dev->dev.type = &rc_dev_type;
  227. ir_dev->dev.class = &ir_input_class;
  228. ir_dev->dev.parent = input_dev->dev.parent;
  229. dev_set_name(&ir_dev->dev, "rc%d", devno);
  230. dev_set_drvdata(&ir_dev->dev, ir_dev);
  231. rc = device_register(&ir_dev->dev);
  232. if (rc)
  233. return rc;
  234. input_dev->dev.parent = &ir_dev->dev;
  235. rc = input_register_device(input_dev);
  236. if (rc < 0) {
  237. device_del(&ir_dev->dev);
  238. return rc;
  239. }
  240. __module_get(THIS_MODULE);
  241. path = kobject_get_path(&ir_dev->dev.kobj, GFP_KERNEL);
  242. printk(KERN_INFO "%s: %s as %s\n",
  243. dev_name(&ir_dev->dev),
  244. input_dev->name ? input_dev->name : "Unspecified device",
  245. path ? path : "N/A");
  246. kfree(path);
  247. ir_dev->devno = devno;
  248. set_bit(devno, &ir_core_dev_number);
  249. return 0;
  250. };
  251. /**
  252. * ir_unregister_class() - removes the sysfs for sysfs for
  253. * /sys/class/rc/rc?
  254. * @input_dev: the struct input_dev descriptor of the device
  255. *
  256. * This routine is used to unregister the syfs code for IR class
  257. */
  258. void ir_unregister_class(struct input_dev *input_dev)
  259. {
  260. struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
  261. clear_bit(ir_dev->devno, &ir_core_dev_number);
  262. input_unregister_device(input_dev);
  263. device_del(&ir_dev->dev);
  264. module_put(THIS_MODULE);
  265. }
  266. /*
  267. * Init/exit code for the module. Basically, creates/removes /sys/class/rc
  268. */
  269. static int __init ir_core_init(void)
  270. {
  271. int rc = class_register(&ir_input_class);
  272. if (rc) {
  273. printk(KERN_ERR "ir_core: unable to register rc class\n");
  274. return rc;
  275. }
  276. /* Initialize/load the decoders/keymap code that will be used */
  277. ir_raw_init();
  278. return 0;
  279. }
  280. static void __exit ir_core_exit(void)
  281. {
  282. class_unregister(&ir_input_class);
  283. }
  284. module_init(ir_core_init);
  285. module_exit(ir_core_exit);