blob: 002b1412331951a052e524c6ea53f2c2f87df415 [file] [log] [blame]
David Härdemandd3f6162010-04-15 18:46:35 -03001/* ir-sysfs.c - sysfs interface for RC devices (/sys/class/rc)
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -03002 *
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -03003 * Copyright (C) 2009-2010 by Mauro Carvalho Chehab <mchehab@redhat.com>
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -03004 *
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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -030016#include <linux/input.h>
17#include <linux/device.h>
Mauro Carvalho Chehab3f113e32010-04-08 15:10:27 -030018#include "ir-core-priv.h"
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -030019
20#define IRRCV_NUM_DEVICES 256
21
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -030022/* bit array to represent IR sysfs device number */
23static unsigned long ir_core_dev_number;
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -030024
Mauro Carvalho Chehabe202c152010-03-26 22:45:16 -030025/* class for /sys/class/rc */
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -030026static char *ir_devnode(struct device *dev, mode_t *mode)
27{
Mauro Carvalho Chehabe202c152010-03-26 22:45:16 -030028 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -030029}
30
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -030031static struct class ir_input_class = {
Mauro Carvalho Chehabe202c152010-03-26 22:45:16 -030032 .name = "rc",
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -030033 .devnode = ir_devnode,
34};
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -030035
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -030036/**
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
41 *
42 * This routine is a callback routine for input read the IR protocol type.
David Härdemande88f312010-03-28 18:38:49 -030043 * it is trigged by reading /sys/class/rc/rc?/current_protocol.
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -030044 * It returns the protocol name, as understood by the driver.
45 */
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030046static ssize_t show_protocol(struct device *d,
47 struct device_attribute *mattr, char *buf)
48{
49 char *s;
50 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
Mauro Carvalho Chehab971e8292009-12-14 13:53:37 -030051 u64 ir_type = ir_dev->rc_tab.ir_type;
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030052
Mauro Carvalho Chehab971e8292009-12-14 13:53:37 -030053 IR_dprintk(1, "Current protocol is %lld\n", (long long)ir_type);
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030054
55 /* FIXME: doesn't support multiple protocols at the same time */
56 if (ir_type == IR_TYPE_UNKNOWN)
57 s = "Unknown";
58 else if (ir_type == IR_TYPE_RC5)
Mauro Carvalho Chehabb320f802010-04-08 01:02:49 -030059 s = "rc-5";
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030060 else if (ir_type == IR_TYPE_PD)
Mauro Carvalho Chehabb320f802010-04-08 01:02:49 -030061 s = "pulse-distance";
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030062 else if (ir_type == IR_TYPE_NEC)
Mauro Carvalho Chehabb320f802010-04-08 01:02:49 -030063 s = "nec";
David Härdeman784a4932010-04-08 20:04:40 -030064 else if (ir_type == IR_TYPE_RC6)
65 s = "rc6";
David Härdemanbf670f62010-04-15 18:46:05 -030066 else if (ir_type == IR_TYPE_JVC)
67 s = "jvc";
David Härdeman3fe29c82010-04-15 18:46:10 -030068 else if (ir_type == IR_TYPE_SONY)
69 s = "sony";
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030070 else
Mauro Carvalho Chehabb320f802010-04-08 01:02:49 -030071 s = "other";
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030072
73 return sprintf(buf, "%s\n", s);
74}
75
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -030076/**
77 * store_protocol() - shows the current IR protocol
78 * @d: the device descriptor
79 * @mattr: the device attribute struct (unused)
80 * @buf: a pointer to the input buffer
81 * @len: length of the input buffer
82 *
83 * This routine is a callback routine for changing the IR protocol type.
David Härdemande88f312010-03-28 18:38:49 -030084 * it is trigged by reading /sys/class/rc/rc?/current_protocol.
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -030085 * It changes the IR the protocol name, if the IR type is recognized
86 * by the driver.
87 * If an unknown protocol name is used, returns -EINVAL.
88 */
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -030089static ssize_t store_protocol(struct device *d,
90 struct device_attribute *mattr,
91 const char *data,
92 size_t len)
93{
94 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
Mauro Carvalho Chehabb320f802010-04-08 01:02:49 -030095 u64 ir_type = 0;
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -030096 int rc = -EINVAL;
Mauro Carvalho Chehabeecee322009-12-14 02:56:15 -030097 unsigned long flags;
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -030098 char *buf;
99
Mauro Carvalho Chehabd22e5462010-04-08 09:33:45 -0300100 while ((buf = strsep((char **) &data, " \n")) != NULL) {
Mauro Carvalho Chehabb320f802010-04-08 01:02:49 -0300101 if (!strcasecmp(buf, "rc-5") || !strcasecmp(buf, "rc5"))
102 ir_type |= IR_TYPE_RC5;
103 if (!strcasecmp(buf, "pd") || !strcasecmp(buf, "pulse-distance"))
104 ir_type |= IR_TYPE_PD;
105 if (!strcasecmp(buf, "nec"))
106 ir_type |= IR_TYPE_NEC;
David Härdemanbf670f62010-04-15 18:46:05 -0300107 if (!strcasecmp(buf, "jvc"))
108 ir_type |= IR_TYPE_JVC;
David Härdeman3fe29c82010-04-15 18:46:10 -0300109 if (!strcasecmp(buf, "sony"))
110 ir_type |= IR_TYPE_SONY;
Mauro Carvalho Chehabb320f802010-04-08 01:02:49 -0300111 }
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -0300112
Mauro Carvalho Chehabb320f802010-04-08 01:02:49 -0300113 if (!ir_type) {
114 IR_dprintk(1, "Unknown protocol\n");
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -0300115 return -EINVAL;
116 }
117
Mauro Carvalho Chehab0f7ff392009-12-16 23:46:48 -0300118 if (ir_dev->props && ir_dev->props->change_protocol)
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -0300119 rc = ir_dev->props->change_protocol(ir_dev->props->priv,
120 ir_type);
121
122 if (rc < 0) {
Mauro Carvalho Chehab971e8292009-12-14 13:53:37 -0300123 IR_dprintk(1, "Error setting protocol to %lld\n",
124 (long long)ir_type);
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -0300125 return -EINVAL;
126 }
127
Mauro Carvalho Chehabeecee322009-12-14 02:56:15 -0300128 spin_lock_irqsave(&ir_dev->rc_tab.lock, flags);
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -0300129 ir_dev->rc_tab.ir_type = ir_type;
Mauro Carvalho Chehabeecee322009-12-14 02:56:15 -0300130 spin_unlock_irqrestore(&ir_dev->rc_tab.lock, flags);
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -0300131
Mauro Carvalho Chehabb320f802010-04-08 01:02:49 -0300132 IR_dprintk(1, "Current protocol(s) is(are) %lld\n",
Mauro Carvalho Chehab971e8292009-12-14 13:53:37 -0300133 (long long)ir_type);
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -0300134
135 return len;
136}
137
Mauro Carvalho Chehabb320f802010-04-08 01:02:49 -0300138static ssize_t show_supported_protocols(struct device *d,
139 struct device_attribute *mattr, char *buf)
140{
141 char *orgbuf = buf;
142 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
143
144 /* FIXME: doesn't support multiple protocols at the same time */
145 if (ir_dev->props->allowed_protos == IR_TYPE_UNKNOWN)
146 buf += sprintf(buf, "unknown ");
147 if (ir_dev->props->allowed_protos & IR_TYPE_RC5)
148 buf += sprintf(buf, "rc-5 ");
149 if (ir_dev->props->allowed_protos & IR_TYPE_PD)
150 buf += sprintf(buf, "pulse-distance ");
151 if (ir_dev->props->allowed_protos & IR_TYPE_NEC)
152 buf += sprintf(buf, "nec ");
153 if (buf == orgbuf)
154 buf += sprintf(buf, "other ");
155
156 buf += sprintf(buf - 1, "\n");
157
158 return buf - orgbuf;
159}
Mauro Carvalho Chehab9c89a182010-03-12 11:50:17 -0300160
161#define ADD_HOTPLUG_VAR(fmt, val...) \
162 do { \
163 int err = add_uevent_var(env, fmt, val); \
164 if (err) \
165 return err; \
166 } while (0)
167
168static int ir_dev_uevent(struct device *device, struct kobj_uevent_env *env)
169{
170 struct ir_input_dev *ir_dev = dev_get_drvdata(device);
171
172 if (ir_dev->rc_tab.name)
Mauro Carvalho Chehab3efaa062010-04-10 23:43:39 -0300173 ADD_HOTPLUG_VAR("NAME=%s", ir_dev->rc_tab.name);
Mauro Carvalho Chehab727e6252010-03-12 21:18:14 -0300174 if (ir_dev->driver_name)
Mauro Carvalho Chehab3efaa062010-04-10 23:43:39 -0300175 ADD_HOTPLUG_VAR("DRV_NAME=%s", ir_dev->driver_name);
Mauro Carvalho Chehab9c89a182010-03-12 11:50:17 -0300176
177 return 0;
178}
179
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300180/*
181 * Static device attribute struct with the sysfs attributes for IR's
182 */
Mauro Carvalho Chehabb320f802010-04-08 01:02:49 -0300183static DEVICE_ATTR(protocol, S_IRUGO | S_IWUSR,
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -0300184 show_protocol, store_protocol);
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300185
Mauro Carvalho Chehabb320f802010-04-08 01:02:49 -0300186static DEVICE_ATTR(supported_protocols, S_IRUGO | S_IWUSR,
187 show_supported_protocols, NULL);
188
Mauro Carvalho Chehab626cf692010-04-06 23:21:46 -0300189static struct attribute *ir_hw_dev_attrs[] = {
Mauro Carvalho Chehabb320f802010-04-08 01:02:49 -0300190 &dev_attr_protocol.attr,
191 &dev_attr_supported_protocols.attr,
Francesco Lavra9714d582009-12-29 15:48:04 -0300192 NULL,
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300193};
194
Mauro Carvalho Chehab626cf692010-04-06 23:21:46 -0300195static struct attribute_group ir_hw_dev_attr_grp = {
196 .attrs = ir_hw_dev_attrs,
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300197};
198
Mauro Carvalho Chehab626cf692010-04-06 23:21:46 -0300199static const struct attribute_group *ir_hw_dev_attr_groups[] = {
200 &ir_hw_dev_attr_grp,
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300201 NULL
202};
203
Mauro Carvalho Chehab626cf692010-04-06 23:21:46 -0300204static struct device_type rc_dev_type = {
205 .groups = ir_hw_dev_attr_groups,
206 .uevent = ir_dev_uevent,
207};
208
209static struct device_type ir_raw_dev_type = {
Mauro Carvalho Chehab9c89a182010-03-12 11:50:17 -0300210 .uevent = ir_dev_uevent,
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300211};
212
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300213/**
David Härdemande88f312010-03-28 18:38:49 -0300214 * ir_register_class() - creates the sysfs for /sys/class/rc/rc?
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300215 * @input_dev: the struct input_dev descriptor of the device
216 *
217 * This routine is used to register the syfs code for IR class
218 */
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300219int ir_register_class(struct input_dev *input_dev)
220{
221 int rc;
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300222 const char *path;
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300223 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
224 int devno = find_first_zero_bit(&ir_core_dev_number,
225 IRRCV_NUM_DEVICES);
226
227 if (unlikely(devno < 0))
228 return devno;
229
Mauro Carvalho Chehab626cf692010-04-06 23:21:46 -0300230 if (ir_dev->props->driver_type == RC_DRIVER_SCANCODE)
231 ir_dev->dev.type = &rc_dev_type;
232 else
233 ir_dev->dev.type = &ir_raw_dev_type;
234
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300235 ir_dev->dev.class = &ir_input_class;
236 ir_dev->dev.parent = input_dev->dev.parent;
David Härdemande88f312010-03-28 18:38:49 -0300237 dev_set_name(&ir_dev->dev, "rc%d", devno);
Mauro Carvalho Chehab9c89a182010-03-12 11:50:17 -0300238 dev_set_drvdata(&ir_dev->dev, ir_dev);
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300239 rc = device_register(&ir_dev->dev);
240 if (rc)
241 return rc;
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300242
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300243
244 input_dev->dev.parent = &ir_dev->dev;
245 rc = input_register_device(input_dev);
246 if (rc < 0) {
247 device_del(&ir_dev->dev);
248 return rc;
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300249 }
250
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300251 __module_get(THIS_MODULE);
252
Mauro Carvalho Chehab9c89a182010-03-12 11:50:17 -0300253 path = kobject_get_path(&ir_dev->dev.kobj, GFP_KERNEL);
254 printk(KERN_INFO "%s: %s as %s\n",
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300255 dev_name(&ir_dev->dev),
256 input_dev->name ? input_dev->name : "Unspecified device",
257 path ? path : "N/A");
258 kfree(path);
259
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300260 ir_dev->devno = devno;
261 set_bit(devno, &ir_core_dev_number);
262
263 return 0;
264};
265
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300266/**
267 * ir_unregister_class() - removes the sysfs for sysfs for
David Härdemande88f312010-03-28 18:38:49 -0300268 * /sys/class/rc/rc?
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300269 * @input_dev: the struct input_dev descriptor of the device
270 *
271 * This routine is used to unregister the syfs code for IR class
272 */
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300273void ir_unregister_class(struct input_dev *input_dev)
274{
275 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300276
277 clear_bit(ir_dev->devno, &ir_core_dev_number);
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300278 input_unregister_device(input_dev);
279 device_del(&ir_dev->dev);
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300280
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300281 module_put(THIS_MODULE);
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300282}
283
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300284/*
Mauro Carvalho Chehabe202c152010-03-26 22:45:16 -0300285 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300286 */
287
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300288static int __init ir_core_init(void)
289{
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300290 int rc = class_register(&ir_input_class);
291 if (rc) {
Mauro Carvalho Chehabe202c152010-03-26 22:45:16 -0300292 printk(KERN_ERR "ir_core: unable to register rc class\n");
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300293 return rc;
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300294 }
295
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300296 /* Initialize/load the decoders/keymap code that will be used */
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300297 ir_raw_init();
298
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300299 return 0;
300}
301
302static void __exit ir_core_exit(void)
303{
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300304 class_unregister(&ir_input_class);
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300305}
306
307module_init(ir_core_init);
308module_exit(ir_core_exit);