blob: e84f9783b8c2b1eae9ef652e216eb6408f676b65 [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 Chehab4403b7b2010-06-28 12:37:13 -030036static struct {
37 u64 type;
38 char *name;
39} proto_names[] = {
40 { IR_TYPE_UNKNOWN, "unknown" },
Mauro Carvalho Chehaba9e55ea2010-06-28 12:43:25 -030041 { IR_TYPE_RC5, "rc-5" },
Mauro Carvalho Chehab4403b7b2010-06-28 12:37:13 -030042 { IR_TYPE_NEC, "nec" },
Mauro Carvalho Chehaba9e55ea2010-06-28 12:43:25 -030043 { IR_TYPE_RC6, "rc-6" },
Mauro Carvalho Chehab4403b7b2010-06-28 12:37:13 -030044 { IR_TYPE_JVC, "jvc" },
45 { IR_TYPE_SONY, "sony" },
46};
47
Mauro Carvalho Chehab5f124792010-06-28 12:58:48 -030048#define PROTO_NONE "none"
49
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -030050/**
David Härdeman667c9eb2010-06-13 17:29:31 -030051 * show_protocols() - shows the current IR protocol(s)
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -030052 * @d: the device descriptor
53 * @mattr: the device attribute struct (unused)
54 * @buf: a pointer to the output buffer
55 *
David Härdeman667c9eb2010-06-13 17:29:31 -030056 * This routine is a callback routine for input read the IR protocol type(s).
57 * it is trigged by reading /sys/class/rc/rc?/protocols.
58 * It returns the protocol names of supported protocols.
59 * Enabled protocols are printed in brackets.
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -030060 */
David Härdeman667c9eb2010-06-13 17:29:31 -030061static ssize_t show_protocols(struct device *d,
62 struct device_attribute *mattr, char *buf)
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030063{
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030064 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
David Härdeman667c9eb2010-06-13 17:29:31 -030065 u64 allowed, enabled;
66 char *tmp = buf;
Mauro Carvalho Chehab4403b7b2010-06-28 12:37:13 -030067 int i;
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030068
David Härdeman667c9eb2010-06-13 17:29:31 -030069 if (ir_dev->props->driver_type == RC_DRIVER_SCANCODE) {
70 enabled = ir_dev->rc_tab.ir_type;
71 allowed = ir_dev->props->allowed_protos;
72 } else {
73 enabled = ir_dev->raw->enabled_protocols;
74 allowed = ir_raw_get_allowed_protocols();
75 }
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030076
David Härdeman667c9eb2010-06-13 17:29:31 -030077 IR_dprintk(1, "allowed - 0x%llx, enabled - 0x%llx\n",
78 (long long)allowed,
79 (long long)enabled);
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030080
Mauro Carvalho Chehab4403b7b2010-06-28 12:37:13 -030081 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
82 if (allowed & enabled & proto_names[i].type)
83 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
84 else if (allowed & proto_names[i].type)
85 tmp += sprintf(tmp, "%s ", proto_names[i].name);
86 }
David Härdeman667c9eb2010-06-13 17:29:31 -030087
88 if (tmp != buf)
89 tmp--;
90 *tmp = '\n';
91 return tmp + 1 - buf;
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030092}
93
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -030094/**
David Härdeman667c9eb2010-06-13 17:29:31 -030095 * store_protocols() - changes the current IR protocol(s)
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -030096 * @d: the device descriptor
97 * @mattr: the device attribute struct (unused)
98 * @buf: a pointer to the input buffer
99 * @len: length of the input buffer
100 *
101 * This routine is a callback routine for changing the IR protocol type.
David Härdeman667c9eb2010-06-13 17:29:31 -0300102 * It is trigged by writing to /sys/class/rc/rc?/protocols.
103 * Writing "+proto" will add a protocol to the list of enabled protocols.
104 * Writing "-proto" will remove a protocol from the list of enabled protocols.
105 * Writing "proto" will enable only "proto".
Mauro Carvalho Chehab5f124792010-06-28 12:58:48 -0300106 * Writing "none" will disable all protocols.
David Härdeman667c9eb2010-06-13 17:29:31 -0300107 * Returns -EINVAL if an invalid protocol combination or unknown protocol name
108 * is used, otherwise @len.
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300109 */
David Härdeman667c9eb2010-06-13 17:29:31 -0300110static ssize_t store_protocols(struct device *d,
111 struct device_attribute *mattr,
112 const char *data,
113 size_t len)
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -0300114{
115 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
David Härdeman667c9eb2010-06-13 17:29:31 -0300116 bool enable, disable;
117 const char *tmp;
118 u64 type;
119 u64 mask;
Mauro Carvalho Chehab4403b7b2010-06-28 12:37:13 -0300120 int rc, i;
Mauro Carvalho Chehabeecee322009-12-14 02:56:15 -0300121 unsigned long flags;
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -0300122
David Härdeman667c9eb2010-06-13 17:29:31 -0300123 tmp = skip_spaces(data);
Mauro Carvalho Chehab4403b7b2010-06-28 12:37:13 -0300124 if (*tmp == '\0') {
125 IR_dprintk(1, "Protocol not specified\n");
126 return -EINVAL;
127 } else if (*tmp == '+') {
David Härdeman667c9eb2010-06-13 17:29:31 -0300128 enable = true;
129 disable = false;
130 tmp++;
131 } else if (*tmp == '-') {
132 enable = false;
133 disable = true;
134 tmp++;
135 } else {
136 enable = false;
137 disable = false;
Mauro Carvalho Chehabb320f802010-04-08 01:02:49 -0300138 }
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -0300139
Mauro Carvalho Chehab5f124792010-06-28 12:58:48 -0300140
141 if (!enable && !disable && !strncasecmp(tmp, PROTO_NONE, sizeof(PROTO_NONE))) {
142 mask = 0;
143 tmp += sizeof(PROTO_NONE);
144 } else {
145 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
146 if (!strncasecmp(tmp, proto_names[i].name, strlen(proto_names[i].name))) {
147 tmp += strlen(proto_names[i].name);
148 mask = proto_names[i].type;
149 break;
150 }
Mauro Carvalho Chehab4403b7b2010-06-28 12:37:13 -0300151 }
Mauro Carvalho Chehab5f124792010-06-28 12:58:48 -0300152 if (i == ARRAY_SIZE(proto_names)) {
153 IR_dprintk(1, "Unknown protocol\n");
154 return -EINVAL;
155 }
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -0300156 }
157
David Härdeman667c9eb2010-06-13 17:29:31 -0300158 tmp = skip_spaces(tmp);
159 if (*tmp != '\0') {
160 IR_dprintk(1, "Invalid trailing characters\n");
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -0300161 return -EINVAL;
162 }
163
David Härdeman667c9eb2010-06-13 17:29:31 -0300164 if (ir_dev->props->driver_type == RC_DRIVER_SCANCODE)
165 type = ir_dev->rc_tab.ir_type;
166 else
167 type = ir_dev->raw->enabled_protocols;
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -0300168
David Härdeman667c9eb2010-06-13 17:29:31 -0300169 if (enable)
170 type |= mask;
171 else if (disable)
172 type &= ~mask;
173 else
174 type = mask;
175
176 if (ir_dev->props && ir_dev->props->change_protocol) {
177 rc = ir_dev->props->change_protocol(ir_dev->props->priv,
178 type);
179 if (rc < 0) {
180 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
181 (long long)type);
182 return -EINVAL;
183 }
184 }
185
186 if (ir_dev->props->driver_type == RC_DRIVER_SCANCODE) {
187 spin_lock_irqsave(&ir_dev->rc_tab.lock, flags);
188 ir_dev->rc_tab.ir_type = type;
189 spin_unlock_irqrestore(&ir_dev->rc_tab.lock, flags);
190 } else {
191 ir_dev->raw->enabled_protocols = type;
192 }
193
194
195 IR_dprintk(1, "Current protocol(s): 0x%llx\n",
196 (long long)type);
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -0300197
198 return len;
199}
200
Mauro Carvalho Chehab9c89a182010-03-12 11:50:17 -0300201#define ADD_HOTPLUG_VAR(fmt, val...) \
202 do { \
203 int err = add_uevent_var(env, fmt, val); \
204 if (err) \
205 return err; \
206 } while (0)
207
David Härdeman667c9eb2010-06-13 17:29:31 -0300208static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
Mauro Carvalho Chehab9c89a182010-03-12 11:50:17 -0300209{
210 struct ir_input_dev *ir_dev = dev_get_drvdata(device);
211
212 if (ir_dev->rc_tab.name)
Mauro Carvalho Chehab3efaa062010-04-10 23:43:39 -0300213 ADD_HOTPLUG_VAR("NAME=%s", ir_dev->rc_tab.name);
Mauro Carvalho Chehab727e6252010-03-12 21:18:14 -0300214 if (ir_dev->driver_name)
Mauro Carvalho Chehab3efaa062010-04-10 23:43:39 -0300215 ADD_HOTPLUG_VAR("DRV_NAME=%s", ir_dev->driver_name);
Mauro Carvalho Chehab9c89a182010-03-12 11:50:17 -0300216
217 return 0;
218}
219
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300220/*
221 * Static device attribute struct with the sysfs attributes for IR's
222 */
David Härdeman667c9eb2010-06-13 17:29:31 -0300223static DEVICE_ATTR(protocols, S_IRUGO | S_IWUSR,
224 show_protocols, store_protocols);
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300225
David Härdeman667c9eb2010-06-13 17:29:31 -0300226static struct attribute *rc_dev_attrs[] = {
227 &dev_attr_protocols.attr,
Francesco Lavra9714d582009-12-29 15:48:04 -0300228 NULL,
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300229};
230
David Härdeman667c9eb2010-06-13 17:29:31 -0300231static struct attribute_group rc_dev_attr_grp = {
232 .attrs = rc_dev_attrs,
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300233};
234
David Härdeman667c9eb2010-06-13 17:29:31 -0300235static const struct attribute_group *rc_dev_attr_groups[] = {
236 &rc_dev_attr_grp,
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300237 NULL
238};
239
Mauro Carvalho Chehab626cf692010-04-06 23:21:46 -0300240static struct device_type rc_dev_type = {
David Härdeman667c9eb2010-06-13 17:29:31 -0300241 .groups = rc_dev_attr_groups,
242 .uevent = rc_dev_uevent,
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300243};
244
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300245/**
David Härdemande88f312010-03-28 18:38:49 -0300246 * ir_register_class() - creates the sysfs for /sys/class/rc/rc?
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300247 * @input_dev: the struct input_dev descriptor of the device
248 *
249 * This routine is used to register the syfs code for IR class
250 */
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300251int ir_register_class(struct input_dev *input_dev)
252{
253 int rc;
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300254 const char *path;
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300255 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
256 int devno = find_first_zero_bit(&ir_core_dev_number,
257 IRRCV_NUM_DEVICES);
258
259 if (unlikely(devno < 0))
260 return devno;
261
David Härdeman667c9eb2010-06-13 17:29:31 -0300262 ir_dev->dev.type = &rc_dev_type;
Mauro Carvalho Chehab626cf692010-04-06 23:21:46 -0300263
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300264 ir_dev->dev.class = &ir_input_class;
265 ir_dev->dev.parent = input_dev->dev.parent;
David Härdemande88f312010-03-28 18:38:49 -0300266 dev_set_name(&ir_dev->dev, "rc%d", devno);
Mauro Carvalho Chehab9c89a182010-03-12 11:50:17 -0300267 dev_set_drvdata(&ir_dev->dev, ir_dev);
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300268 rc = device_register(&ir_dev->dev);
269 if (rc)
270 return rc;
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300271
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300272
273 input_dev->dev.parent = &ir_dev->dev;
274 rc = input_register_device(input_dev);
275 if (rc < 0) {
276 device_del(&ir_dev->dev);
277 return rc;
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300278 }
279
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300280 __module_get(THIS_MODULE);
281
Mauro Carvalho Chehab9c89a182010-03-12 11:50:17 -0300282 path = kobject_get_path(&ir_dev->dev.kobj, GFP_KERNEL);
283 printk(KERN_INFO "%s: %s as %s\n",
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300284 dev_name(&ir_dev->dev),
285 input_dev->name ? input_dev->name : "Unspecified device",
286 path ? path : "N/A");
287 kfree(path);
288
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300289 ir_dev->devno = devno;
290 set_bit(devno, &ir_core_dev_number);
291
292 return 0;
293};
294
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300295/**
296 * ir_unregister_class() - removes the sysfs for sysfs for
David Härdemande88f312010-03-28 18:38:49 -0300297 * /sys/class/rc/rc?
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300298 * @input_dev: the struct input_dev descriptor of the device
299 *
300 * This routine is used to unregister the syfs code for IR class
301 */
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300302void ir_unregister_class(struct input_dev *input_dev)
303{
304 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300305
306 clear_bit(ir_dev->devno, &ir_core_dev_number);
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300307 input_unregister_device(input_dev);
308 device_del(&ir_dev->dev);
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300309
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300310 module_put(THIS_MODULE);
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300311}
312
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300313/*
Mauro Carvalho Chehabe202c152010-03-26 22:45:16 -0300314 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300315 */
316
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300317static int __init ir_core_init(void)
318{
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300319 int rc = class_register(&ir_input_class);
320 if (rc) {
Mauro Carvalho Chehabe202c152010-03-26 22:45:16 -0300321 printk(KERN_ERR "ir_core: unable to register rc class\n");
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300322 return rc;
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300323 }
324
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300325 /* Initialize/load the decoders/keymap code that will be used */
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300326 ir_raw_init();
327
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300328 return 0;
329}
330
331static void __exit ir_core_exit(void)
332{
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300333 class_unregister(&ir_input_class);
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300334}
335
336module_init(ir_core_init);
337module_exit(ir_core_exit);