ir-sysfs.c 9.0 KB

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