blob: 96dafc425c8e61495cd662bf7f4c11182d674e79 [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" },
Jarod Wilsonca414692010-07-03 01:07:53 -030046 { IR_TYPE_LIRC, "lirc" },
Mauro Carvalho Chehab4403b7b2010-06-28 12:37:13 -030047};
48
Mauro Carvalho Chehab5f124792010-06-28 12:58:48 -030049#define PROTO_NONE "none"
50
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -030051/**
David Härdeman667c9eb2010-06-13 17:29:31 -030052 * show_protocols() - shows the current IR protocol(s)
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -030053 * @d: the device descriptor
54 * @mattr: the device attribute struct (unused)
55 * @buf: a pointer to the output buffer
56 *
David Härdeman667c9eb2010-06-13 17:29:31 -030057 * 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.
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -030061 */
David Härdeman667c9eb2010-06-13 17:29:31 -030062static ssize_t show_protocols(struct device *d,
63 struct device_attribute *mattr, char *buf)
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030064{
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030065 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
David Härdeman667c9eb2010-06-13 17:29:31 -030066 u64 allowed, enabled;
67 char *tmp = buf;
Mauro Carvalho Chehab4403b7b2010-06-28 12:37:13 -030068 int i;
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030069
David Härdeman667c9eb2010-06-13 17:29:31 -030070 if (ir_dev->props->driver_type == RC_DRIVER_SCANCODE) {
71 enabled = ir_dev->rc_tab.ir_type;
72 allowed = ir_dev->props->allowed_protos;
73 } else {
74 enabled = ir_dev->raw->enabled_protocols;
75 allowed = ir_raw_get_allowed_protocols();
76 }
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030077
David Härdeman667c9eb2010-06-13 17:29:31 -030078 IR_dprintk(1, "allowed - 0x%llx, enabled - 0x%llx\n",
79 (long long)allowed,
80 (long long)enabled);
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030081
Mauro Carvalho Chehab4403b7b2010-06-28 12:37:13 -030082 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);
87 }
David Härdeman667c9eb2010-06-13 17:29:31 -030088
89 if (tmp != buf)
90 tmp--;
91 *tmp = '\n';
92 return tmp + 1 - buf;
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030093}
94
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -030095/**
David Härdeman667c9eb2010-06-13 17:29:31 -030096 * store_protocols() - changes the current IR protocol(s)
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -030097 * @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
101 *
102 * This routine is a callback routine for changing the IR protocol type.
David Härdeman667c9eb2010-06-13 17:29:31 -0300103 * 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".
Mauro Carvalho Chehab5f124792010-06-28 12:58:48 -0300107 * Writing "none" will disable all protocols.
David Härdeman667c9eb2010-06-13 17:29:31 -0300108 * Returns -EINVAL if an invalid protocol combination or unknown protocol name
109 * is used, otherwise @len.
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300110 */
David Härdeman667c9eb2010-06-13 17:29:31 -0300111static ssize_t store_protocols(struct device *d,
112 struct device_attribute *mattr,
113 const char *data,
114 size_t len)
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -0300115{
116 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
David Härdeman667c9eb2010-06-13 17:29:31 -0300117 bool enable, disable;
118 const char *tmp;
119 u64 type;
120 u64 mask;
Mauro Carvalho Chehabde8592b2010-06-28 13:52:07 -0300121 int rc, i, count = 0;
Mauro Carvalho Chehabeecee322009-12-14 02:56:15 -0300122 unsigned long flags;
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -0300123
David Härdeman667c9eb2010-06-13 17:29:31 -0300124 if (ir_dev->props->driver_type == RC_DRIVER_SCANCODE)
125 type = ir_dev->rc_tab.ir_type;
126 else
127 type = ir_dev->raw->enabled_protocols;
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -0300128
Mauro Carvalho Chehabde8592b2010-06-28 13:52:07 -0300129 while ((tmp = strsep((char **) &data, " \n")) != NULL) {
130 if (!*tmp)
131 break;
132
133 if (*tmp == '+') {
134 enable = true;
135 disable = false;
136 tmp++;
137 } else if (*tmp == '-') {
138 enable = false;
139 disable = true;
140 tmp++;
141 } else {
142 enable = false;
143 disable = false;
144 }
145
146 if (!enable && !disable && !strncasecmp(tmp, PROTO_NONE, sizeof(PROTO_NONE))) {
147 tmp += sizeof(PROTO_NONE);
148 mask = 0;
149 count++;
150 } else {
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;
155 break;
156 }
157 }
158 if (i == ARRAY_SIZE(proto_names)) {
159 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
160 return -EINVAL;
161 }
162 count++;
163 }
164
165 if (enable)
166 type |= mask;
167 else if (disable)
168 type &= ~mask;
169 else
170 type = mask;
171 }
172
173 if (!count) {
174 IR_dprintk(1, "Protocol not specified\n");
175 return -EINVAL;
176 }
David Härdeman667c9eb2010-06-13 17:29:31 -0300177
178 if (ir_dev->props && ir_dev->props->change_protocol) {
179 rc = ir_dev->props->change_protocol(ir_dev->props->priv,
180 type);
181 if (rc < 0) {
182 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
183 (long long)type);
184 return -EINVAL;
185 }
186 }
187
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);
192 } else {
193 ir_dev->raw->enabled_protocols = type;
194 }
195
David Härdeman667c9eb2010-06-13 17:29:31 -0300196 IR_dprintk(1, "Current protocol(s): 0x%llx\n",
197 (long long)type);
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -0300198
199 return len;
200}
201
Mauro Carvalho Chehab9c89a182010-03-12 11:50:17 -0300202#define ADD_HOTPLUG_VAR(fmt, val...) \
203 do { \
204 int err = add_uevent_var(env, fmt, val); \
205 if (err) \
206 return err; \
207 } while (0)
208
David Härdeman667c9eb2010-06-13 17:29:31 -0300209static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
Mauro Carvalho Chehab9c89a182010-03-12 11:50:17 -0300210{
211 struct ir_input_dev *ir_dev = dev_get_drvdata(device);
212
213 if (ir_dev->rc_tab.name)
Mauro Carvalho Chehab3efaa062010-04-10 23:43:39 -0300214 ADD_HOTPLUG_VAR("NAME=%s", ir_dev->rc_tab.name);
Mauro Carvalho Chehab727e6252010-03-12 21:18:14 -0300215 if (ir_dev->driver_name)
Mauro Carvalho Chehab3efaa062010-04-10 23:43:39 -0300216 ADD_HOTPLUG_VAR("DRV_NAME=%s", ir_dev->driver_name);
Mauro Carvalho Chehab9c89a182010-03-12 11:50:17 -0300217
218 return 0;
219}
220
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300221/*
222 * Static device attribute struct with the sysfs attributes for IR's
223 */
David Härdeman667c9eb2010-06-13 17:29:31 -0300224static DEVICE_ATTR(protocols, S_IRUGO | S_IWUSR,
225 show_protocols, store_protocols);
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300226
David Härdeman667c9eb2010-06-13 17:29:31 -0300227static struct attribute *rc_dev_attrs[] = {
228 &dev_attr_protocols.attr,
Francesco Lavra9714d582009-12-29 15:48:04 -0300229 NULL,
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300230};
231
David Härdeman667c9eb2010-06-13 17:29:31 -0300232static struct attribute_group rc_dev_attr_grp = {
233 .attrs = rc_dev_attrs,
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300234};
235
David Härdeman667c9eb2010-06-13 17:29:31 -0300236static const struct attribute_group *rc_dev_attr_groups[] = {
237 &rc_dev_attr_grp,
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300238 NULL
239};
240
Mauro Carvalho Chehab626cf692010-04-06 23:21:46 -0300241static struct device_type rc_dev_type = {
David Härdeman667c9eb2010-06-13 17:29:31 -0300242 .groups = rc_dev_attr_groups,
243 .uevent = rc_dev_uevent,
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300244};
245
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300246/**
David Härdemande88f312010-03-28 18:38:49 -0300247 * ir_register_class() - creates the sysfs for /sys/class/rc/rc?
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300248 * @input_dev: the struct input_dev descriptor of the device
249 *
250 * This routine is used to register the syfs code for IR class
251 */
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300252int ir_register_class(struct input_dev *input_dev)
253{
254 int rc;
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300255 const char *path;
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300256 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
257 int devno = find_first_zero_bit(&ir_core_dev_number,
258 IRRCV_NUM_DEVICES);
259
260 if (unlikely(devno < 0))
261 return devno;
262
David Härdeman667c9eb2010-06-13 17:29:31 -0300263 ir_dev->dev.type = &rc_dev_type;
Mauro Carvalho Chehab626cf692010-04-06 23:21:46 -0300264
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300265 ir_dev->dev.class = &ir_input_class;
266 ir_dev->dev.parent = input_dev->dev.parent;
David Härdemande88f312010-03-28 18:38:49 -0300267 dev_set_name(&ir_dev->dev, "rc%d", devno);
Mauro Carvalho Chehab9c89a182010-03-12 11:50:17 -0300268 dev_set_drvdata(&ir_dev->dev, ir_dev);
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300269 rc = device_register(&ir_dev->dev);
270 if (rc)
271 return rc;
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300272
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300273
274 input_dev->dev.parent = &ir_dev->dev;
275 rc = input_register_device(input_dev);
276 if (rc < 0) {
277 device_del(&ir_dev->dev);
278 return rc;
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300279 }
280
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300281 __module_get(THIS_MODULE);
282
Mauro Carvalho Chehab9c89a182010-03-12 11:50:17 -0300283 path = kobject_get_path(&ir_dev->dev.kobj, GFP_KERNEL);
284 printk(KERN_INFO "%s: %s as %s\n",
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300285 dev_name(&ir_dev->dev),
286 input_dev->name ? input_dev->name : "Unspecified device",
287 path ? path : "N/A");
288 kfree(path);
289
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300290 ir_dev->devno = devno;
291 set_bit(devno, &ir_core_dev_number);
292
293 return 0;
294};
295
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300296/**
297 * ir_unregister_class() - removes the sysfs for sysfs for
David Härdemande88f312010-03-28 18:38:49 -0300298 * /sys/class/rc/rc?
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300299 * @input_dev: the struct input_dev descriptor of the device
300 *
301 * This routine is used to unregister the syfs code for IR class
302 */
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300303void ir_unregister_class(struct input_dev *input_dev)
304{
305 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300306
307 clear_bit(ir_dev->devno, &ir_core_dev_number);
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300308 input_unregister_device(input_dev);
309 device_del(&ir_dev->dev);
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300310
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300311 module_put(THIS_MODULE);
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300312}
313
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300314/*
Mauro Carvalho Chehabe202c152010-03-26 22:45:16 -0300315 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300316 */
317
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300318static int __init ir_core_init(void)
319{
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300320 int rc = class_register(&ir_input_class);
321 if (rc) {
Mauro Carvalho Chehabe202c152010-03-26 22:45:16 -0300322 printk(KERN_ERR "ir_core: unable to register rc class\n");
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300323 return rc;
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300324 }
325
Mauro Carvalho Chehab02858ee2010-04-02 20:01:00 -0300326 /* Initialize/load the decoders/keymap code that will be used */
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300327 ir_raw_init();
Maxim Levitskyb378f432010-07-31 11:59:21 -0300328 ir_rcmap_init();
Mauro Carvalho Chehab995187b2010-03-24 20:47:53 -0300329
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300330 return 0;
331}
332
333static void __exit ir_core_exit(void)
334{
Mauro Carvalho Chehab945cdfa2010-03-11 12:41:56 -0300335 class_unregister(&ir_input_class);
Maxim Levitskyb378f432010-07-31 11:59:21 -0300336 ir_rcmap_cleanup();
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300337}
338
339module_init(ir_core_init);
340module_exit(ir_core_exit);