1 /* ir-register.c - handle IR scancode->keycode tables
3 * Copyright (C) 2009 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 <media/ir-core.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/irrcv */
26 static char *ir_devnode(struct device *dev, mode_t *mode)
28 return kasprintf(GFP_KERNEL, "irrcv/%s", dev_name(dev));
31 struct class ir_input_class = {
33 .devnode = ir_devnode,
37 * show_protocol() - shows the current IR protocol
38 * @d: the device descriptor
39 * @mattr: the device attribute struct (unused)
40 * @buf: a pointer to the output buffer
42 * This routine is a callback routine for input read the IR protocol type.
43 * it is trigged by reading /sys/class/irrcv/irrcv?/current_protocol.
44 * It returns the protocol name, as understood by the driver.
46 static ssize_t show_protocol(struct device *d,
47 struct device_attribute *mattr, char *buf)
50 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
51 u64 ir_type = ir_dev->rc_tab.ir_type;
53 IR_dprintk(1, "Current protocol is %lld\n", (long long)ir_type);
55 /* FIXME: doesn't support multiple protocols at the same time */
56 if (ir_type == IR_TYPE_UNKNOWN)
58 else if (ir_type == IR_TYPE_RC5)
60 else if (ir_type == IR_TYPE_PD)
62 else if (ir_type == IR_TYPE_NEC)
67 return sprintf(buf, "%s\n", s);
71 * store_protocol() - shows the current IR protocol
72 * @d: the device descriptor
73 * @mattr: the device attribute struct (unused)
74 * @buf: a pointer to the input buffer
75 * @len: length of the input buffer
77 * This routine is a callback routine for changing the IR protocol type.
78 * it is trigged by reading /sys/class/irrcv/irrcv?/current_protocol.
79 * It changes the IR the protocol name, if the IR type is recognized
81 * If an unknown protocol name is used, returns -EINVAL.
83 static ssize_t store_protocol(struct device *d,
84 struct device_attribute *mattr,
88 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
89 u64 ir_type = IR_TYPE_UNKNOWN;
94 buf = strsep((char **) &data, "\n");
96 if (!strcasecmp(buf, "rc-5") || !strcasecmp(buf, "rc5"))
97 ir_type = IR_TYPE_RC5;
98 else if (!strcasecmp(buf, "pd"))
100 else if (!strcasecmp(buf, "nec"))
101 ir_type = IR_TYPE_NEC;
103 if (ir_type == IR_TYPE_UNKNOWN) {
104 IR_dprintk(1, "Error setting protocol to %lld\n",
109 if (ir_dev->props && ir_dev->props->change_protocol)
110 rc = ir_dev->props->change_protocol(ir_dev->props->priv,
114 IR_dprintk(1, "Error setting protocol to %lld\n",
119 spin_lock_irqsave(&ir_dev->rc_tab.lock, flags);
120 ir_dev->rc_tab.ir_type = ir_type;
121 spin_unlock_irqrestore(&ir_dev->rc_tab.lock, flags);
123 IR_dprintk(1, "Current protocol is %lld\n",
130 #define ADD_HOTPLUG_VAR(fmt, val...) \
132 int err = add_uevent_var(env, fmt, val); \
137 static int ir_dev_uevent(struct device *device, struct kobj_uevent_env *env)
139 struct ir_input_dev *ir_dev = dev_get_drvdata(device);
141 if (ir_dev->rc_tab.name)
142 ADD_HOTPLUG_VAR("NAME=\"%s\"", ir_dev->rc_tab.name);
143 if (ir_dev->driver_name)
144 ADD_HOTPLUG_VAR("DRV_NAME=\"%s\"", ir_dev->driver_name);
150 * Static device attribute struct with the sysfs attributes for IR's
152 static DEVICE_ATTR(current_protocol, S_IRUGO | S_IWUSR,
153 show_protocol, store_protocol);
155 static struct attribute *ir_dev_attrs[] = {
156 &dev_attr_current_protocol.attr,
160 static struct attribute_group ir_dev_attr_grp = {
161 .attrs = ir_dev_attrs,
164 static const struct attribute_group *ir_dev_attr_groups[] = {
169 static struct device_type ir_dev_type = {
170 .groups = ir_dev_attr_groups,
171 .uevent = ir_dev_uevent,
175 * ir_register_class() - creates the sysfs for /sys/class/irrcv/irrcv?
176 * @input_dev: the struct input_dev descriptor of the device
178 * This routine is used to register the syfs code for IR class
180 int ir_register_class(struct input_dev *input_dev)
185 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
186 int devno = find_first_zero_bit(&ir_core_dev_number,
189 if (unlikely(devno < 0))
192 ir_dev->dev.type = &ir_dev_type;
193 ir_dev->dev.class = &ir_input_class;
194 ir_dev->dev.parent = input_dev->dev.parent;
195 dev_set_name(&ir_dev->dev, "irrcv%d", devno);
196 dev_set_drvdata(&ir_dev->dev, ir_dev);
197 rc = device_register(&ir_dev->dev);
202 input_dev->dev.parent = &ir_dev->dev;
203 rc = input_register_device(input_dev);
205 device_del(&ir_dev->dev);
209 __module_get(THIS_MODULE);
211 path = kobject_get_path(&ir_dev->dev.kobj, GFP_KERNEL);
212 printk(KERN_INFO "%s: %s as %s\n",
213 dev_name(&ir_dev->dev),
214 input_dev->name ? input_dev->name : "Unspecified device",
215 path ? path : "N/A");
218 ir_dev->devno = devno;
219 set_bit(devno, &ir_core_dev_number);
225 * ir_unregister_class() - removes the sysfs for sysfs for
226 * /sys/class/irrcv/irrcv?
227 * @input_dev: the struct input_dev descriptor of the device
229 * This routine is used to unregister the syfs code for IR class
231 void ir_unregister_class(struct input_dev *input_dev)
233 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
235 clear_bit(ir_dev->devno, &ir_core_dev_number);
236 input_unregister_device(input_dev);
237 device_del(&ir_dev->dev);
239 module_put(THIS_MODULE);
243 * Init/exit code for the module. Basically, creates/removes /sys/class/irrcv
246 static int __init ir_core_init(void)
248 int rc = class_register(&ir_input_class);
250 printk(KERN_ERR "ir_core: unable to register irrcv class\n");
257 static void __exit ir_core_exit(void)
259 class_unregister(&ir_input_class);
262 module_init(ir_core_init);
263 module_exit(ir_core_exit);