1 /* ir-sysfs.c - sysfs interface for RC devices (/sys/class/rc)
3 * Copyright (C) 2009-2010 by Mauro Carvalho Chehab <mchehab@redhat.com>
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.
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.
15 #include <linux/slab.h>
16 #include <linux/input.h>
17 #include <linux/device.h>
18 #include "ir-core-priv.h"
20 #define IRRCV_NUM_DEVICES 256
22 /* bit array to represent IR sysfs device number */
23 static unsigned long ir_core_dev_number;
25 /* class for /sys/class/rc */
26 static char *ir_devnode(struct device *dev, mode_t *mode)
28 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
31 static struct class ir_input_class = {
33 .devnode = ir_devnode,
40 { IR_TYPE_UNKNOWN, "unknown" },
41 { IR_TYPE_RC5, "rc-5" },
42 { IR_TYPE_NEC, "nec" },
43 { IR_TYPE_RC6, "rc-6" },
44 { IR_TYPE_JVC, "jvc" },
45 { IR_TYPE_SONY, "sony" },
46 { IR_TYPE_LIRC, "lirc" },
49 #define PROTO_NONE "none"
52 * show_protocols() - shows the current IR protocol(s)
53 * @d: the device descriptor
54 * @mattr: the device attribute struct (unused)
55 * @buf: a pointer to the output buffer
57 * This routine is a callback routine for input read the IR protocol type(s).
58 * it is trigged by reading /sys/class/rc/rc?/protocols.
59 * It returns the protocol names of supported protocols.
60 * Enabled protocols are printed in brackets.
62 static ssize_t show_protocols(struct device *d,
63 struct device_attribute *mattr, char *buf)
65 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
70 if (ir_dev->props->driver_type == RC_DRIVER_SCANCODE) {
71 enabled = ir_dev->rc_tab.ir_type;
72 allowed = ir_dev->props->allowed_protos;
74 enabled = ir_dev->raw->enabled_protocols;
75 allowed = ir_raw_get_allowed_protocols();
78 IR_dprintk(1, "allowed - 0x%llx, enabled - 0x%llx\n",
82 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
83 if (allowed & enabled & proto_names[i].type)
84 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
85 else if (allowed & proto_names[i].type)
86 tmp += sprintf(tmp, "%s ", proto_names[i].name);
96 * store_protocols() - changes the current IR protocol(s)
97 * @d: the device descriptor
98 * @mattr: the device attribute struct (unused)
99 * @buf: a pointer to the input buffer
100 * @len: length of the input buffer
102 * This routine is a callback routine for changing the IR protocol type.
103 * It is trigged by writing to /sys/class/rc/rc?/protocols.
104 * Writing "+proto" will add a protocol to the list of enabled protocols.
105 * Writing "-proto" will remove a protocol from the list of enabled protocols.
106 * Writing "proto" will enable only "proto".
107 * Writing "none" will disable all protocols.
108 * Returns -EINVAL if an invalid protocol combination or unknown protocol name
109 * is used, otherwise @len.
111 static ssize_t store_protocols(struct device *d,
112 struct device_attribute *mattr,
116 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
117 bool enable, disable;
121 int rc, i, count = 0;
124 if (ir_dev->props->driver_type == RC_DRIVER_SCANCODE)
125 type = ir_dev->rc_tab.ir_type;
127 type = ir_dev->raw->enabled_protocols;
129 while ((tmp = strsep((char **) &data, " \n")) != NULL) {
137 } else if (*tmp == '-') {
146 if (!enable && !disable && !strncasecmp(tmp, PROTO_NONE, sizeof(PROTO_NONE))) {
147 tmp += sizeof(PROTO_NONE);
151 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
152 if (!strncasecmp(tmp, proto_names[i].name, strlen(proto_names[i].name))) {
153 tmp += strlen(proto_names[i].name);
154 mask = proto_names[i].type;
158 if (i == ARRAY_SIZE(proto_names)) {
159 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
174 IR_dprintk(1, "Protocol not specified\n");
178 if (ir_dev->props && ir_dev->props->change_protocol) {
179 rc = ir_dev->props->change_protocol(ir_dev->props->priv,
182 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
188 if (ir_dev->props->driver_type == RC_DRIVER_SCANCODE) {
189 spin_lock_irqsave(&ir_dev->rc_tab.lock, flags);
190 ir_dev->rc_tab.ir_type = type;
191 spin_unlock_irqrestore(&ir_dev->rc_tab.lock, flags);
193 ir_dev->raw->enabled_protocols = type;
196 IR_dprintk(1, "Current protocol(s): 0x%llx\n",
202 #define ADD_HOTPLUG_VAR(fmt, val...) \
204 int err = add_uevent_var(env, fmt, val); \
209 static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
211 struct ir_input_dev *ir_dev = dev_get_drvdata(device);
213 if (ir_dev->rc_tab.name)
214 ADD_HOTPLUG_VAR("NAME=%s", ir_dev->rc_tab.name);
215 if (ir_dev->driver_name)
216 ADD_HOTPLUG_VAR("DRV_NAME=%s", ir_dev->driver_name);
222 * Static device attribute struct with the sysfs attributes for IR's
224 static DEVICE_ATTR(protocols, S_IRUGO | S_IWUSR,
225 show_protocols, store_protocols);
227 static struct attribute *rc_dev_attrs[] = {
228 &dev_attr_protocols.attr,
232 static struct attribute_group rc_dev_attr_grp = {
233 .attrs = rc_dev_attrs,
236 static const struct attribute_group *rc_dev_attr_groups[] = {
241 static struct device_type rc_dev_type = {
242 .groups = rc_dev_attr_groups,
243 .uevent = rc_dev_uevent,
247 * ir_register_class() - creates the sysfs for /sys/class/rc/rc?
248 * @input_dev: the struct input_dev descriptor of the device
250 * This routine is used to register the syfs code for IR class
252 int ir_register_class(struct input_dev *input_dev)
256 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
257 int devno = find_first_zero_bit(&ir_core_dev_number,
260 if (unlikely(devno < 0))
263 ir_dev->dev.type = &rc_dev_type;
265 ir_dev->dev.class = &ir_input_class;
266 ir_dev->dev.parent = input_dev->dev.parent;
267 dev_set_name(&ir_dev->dev, "rc%d", devno);
268 dev_set_drvdata(&ir_dev->dev, ir_dev);
269 rc = device_register(&ir_dev->dev);
274 input_dev->dev.parent = &ir_dev->dev;
275 rc = input_register_device(input_dev);
277 device_del(&ir_dev->dev);
281 __module_get(THIS_MODULE);
283 path = kobject_get_path(&ir_dev->dev.kobj, GFP_KERNEL);
284 printk(KERN_INFO "%s: %s as %s\n",
285 dev_name(&ir_dev->dev),
286 input_dev->name ? input_dev->name : "Unspecified device",
287 path ? path : "N/A");
290 ir_dev->devno = devno;
291 set_bit(devno, &ir_core_dev_number);
297 * ir_unregister_class() - removes the sysfs for sysfs for
299 * @input_dev: the struct input_dev descriptor of the device
301 * This routine is used to unregister the syfs code for IR class
303 void ir_unregister_class(struct input_dev *input_dev)
305 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
307 clear_bit(ir_dev->devno, &ir_core_dev_number);
308 input_unregister_device(input_dev);
309 device_del(&ir_dev->dev);
311 module_put(THIS_MODULE);
315 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
318 static int __init ir_core_init(void)
320 int rc = class_register(&ir_input_class);
322 printk(KERN_ERR "ir_core: unable to register rc class\n");
326 /* Initialize/load the decoders/keymap code that will be used */
332 static void __exit ir_core_exit(void)
334 class_unregister(&ir_input_class);
337 module_init(ir_core_init);
338 module_exit(ir_core_exit);