]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/media/IR/ir-sysfs.c
V4L/DVB: ir-core: Add JVC support to ir-core
[net-next-2.6.git] / drivers / media / IR / ir-sysfs.c
CommitLineData
4714eda8
MCC
1/* ir-register.c - handle IR scancode->keycode tables
2 *
995187be 3 * Copyright (C) 2009-2010 by Mauro Carvalho Chehab <mchehab@redhat.com>
4714eda8
MCC
4 *
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
5a0e3ad6 15#include <linux/slab.h>
4714eda8
MCC
16#include <linux/input.h>
17#include <linux/device.h>
3f113e36 18#include "ir-core-priv.h"
4714eda8
MCC
19
20#define IRRCV_NUM_DEVICES 256
21
d4b778d3
MCC
22/* bit array to represent IR sysfs device number */
23static unsigned long ir_core_dev_number;
4714eda8 24
e202c15b 25/* class for /sys/class/rc */
945cdfa2
MCC
26static char *ir_devnode(struct device *dev, mode_t *mode)
27{
e202c15b 28 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
945cdfa2
MCC
29}
30
995187be 31static struct class ir_input_class = {
e202c15b 32 .name = "rc",
945cdfa2
MCC
33 .devnode = ir_devnode,
34};
4714eda8 35
d4b778d3
MCC
36/**
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.
de88f31c 43 * it is trigged by reading /sys/class/rc/rc?/current_protocol.
d4b778d3
MCC
44 * It returns the protocol name, as understood by the driver.
45 */
53f87022
MCC
46static 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);
971e8298 51 u64 ir_type = ir_dev->rc_tab.ir_type;
53f87022 52
971e8298 53 IR_dprintk(1, "Current protocol is %lld\n", (long long)ir_type);
53f87022
MCC
54
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)
b320f80a 59 s = "rc-5";
53f87022 60 else if (ir_type == IR_TYPE_PD)
b320f80a 61 s = "pulse-distance";
53f87022 62 else if (ir_type == IR_TYPE_NEC)
b320f80a 63 s = "nec";
784a4931
DH
64 else if (ir_type == IR_TYPE_RC6)
65 s = "rc6";
bf670f64
DH
66 else if (ir_type == IR_TYPE_JVC)
67 s = "jvc";
53f87022 68 else
b320f80a 69 s = "other";
53f87022
MCC
70
71 return sprintf(buf, "%s\n", s);
72}
73
d4b778d3
MCC
74/**
75 * store_protocol() - shows the current IR protocol
76 * @d: the device descriptor
77 * @mattr: the device attribute struct (unused)
78 * @buf: a pointer to the input buffer
79 * @len: length of the input buffer
80 *
81 * This routine is a callback routine for changing the IR protocol type.
de88f31c 82 * it is trigged by reading /sys/class/rc/rc?/current_protocol.
d4b778d3
MCC
83 * It changes the IR the protocol name, if the IR type is recognized
84 * by the driver.
85 * If an unknown protocol name is used, returns -EINVAL.
86 */
09b01b90
MCC
87static ssize_t store_protocol(struct device *d,
88 struct device_attribute *mattr,
89 const char *data,
90 size_t len)
91{
92 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
b320f80a 93 u64 ir_type = 0;
09b01b90 94 int rc = -EINVAL;
eecee32a 95 unsigned long flags;
09b01b90
MCC
96 char *buf;
97
d22e546e 98 while ((buf = strsep((char **) &data, " \n")) != NULL) {
b320f80a
MCC
99 if (!strcasecmp(buf, "rc-5") || !strcasecmp(buf, "rc5"))
100 ir_type |= IR_TYPE_RC5;
101 if (!strcasecmp(buf, "pd") || !strcasecmp(buf, "pulse-distance"))
102 ir_type |= IR_TYPE_PD;
103 if (!strcasecmp(buf, "nec"))
104 ir_type |= IR_TYPE_NEC;
bf670f64
DH
105 if (!strcasecmp(buf, "jvc"))
106 ir_type |= IR_TYPE_JVC;
b320f80a 107 }
09b01b90 108
b320f80a
MCC
109 if (!ir_type) {
110 IR_dprintk(1, "Unknown protocol\n");
09b01b90
MCC
111 return -EINVAL;
112 }
113
0f7ff395 114 if (ir_dev->props && ir_dev->props->change_protocol)
09b01b90
MCC
115 rc = ir_dev->props->change_protocol(ir_dev->props->priv,
116 ir_type);
117
118 if (rc < 0) {
971e8298
MCC
119 IR_dprintk(1, "Error setting protocol to %lld\n",
120 (long long)ir_type);
09b01b90
MCC
121 return -EINVAL;
122 }
123
eecee32a 124 spin_lock_irqsave(&ir_dev->rc_tab.lock, flags);
09b01b90 125 ir_dev->rc_tab.ir_type = ir_type;
eecee32a 126 spin_unlock_irqrestore(&ir_dev->rc_tab.lock, flags);
09b01b90 127
b320f80a 128 IR_dprintk(1, "Current protocol(s) is(are) %lld\n",
971e8298 129 (long long)ir_type);
09b01b90
MCC
130
131 return len;
132}
133
b320f80a
MCC
134static ssize_t show_supported_protocols(struct device *d,
135 struct device_attribute *mattr, char *buf)
136{
137 char *orgbuf = buf;
138 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
139
140 /* FIXME: doesn't support multiple protocols at the same time */
141 if (ir_dev->props->allowed_protos == IR_TYPE_UNKNOWN)
142 buf += sprintf(buf, "unknown ");
143 if (ir_dev->props->allowed_protos & IR_TYPE_RC5)
144 buf += sprintf(buf, "rc-5 ");
145 if (ir_dev->props->allowed_protos & IR_TYPE_PD)
146 buf += sprintf(buf, "pulse-distance ");
147 if (ir_dev->props->allowed_protos & IR_TYPE_NEC)
148 buf += sprintf(buf, "nec ");
149 if (buf == orgbuf)
150 buf += sprintf(buf, "other ");
151
152 buf += sprintf(buf - 1, "\n");
153
154 return buf - orgbuf;
155}
9c89a181
MCC
156
157#define ADD_HOTPLUG_VAR(fmt, val...) \
158 do { \
159 int err = add_uevent_var(env, fmt, val); \
160 if (err) \
161 return err; \
162 } while (0)
163
164static int ir_dev_uevent(struct device *device, struct kobj_uevent_env *env)
165{
166 struct ir_input_dev *ir_dev = dev_get_drvdata(device);
167
168 if (ir_dev->rc_tab.name)
3efaa062 169 ADD_HOTPLUG_VAR("NAME=%s", ir_dev->rc_tab.name);
727e625c 170 if (ir_dev->driver_name)
3efaa062 171 ADD_HOTPLUG_VAR("DRV_NAME=%s", ir_dev->driver_name);
9c89a181
MCC
172
173 return 0;
174}
175
d4b778d3
MCC
176/*
177 * Static device attribute struct with the sysfs attributes for IR's
178 */
b320f80a 179static DEVICE_ATTR(protocol, S_IRUGO | S_IWUSR,
09b01b90 180 show_protocol, store_protocol);
4714eda8 181
b320f80a
MCC
182static DEVICE_ATTR(supported_protocols, S_IRUGO | S_IWUSR,
183 show_supported_protocols, NULL);
184
626cf697 185static struct attribute *ir_hw_dev_attrs[] = {
b320f80a
MCC
186 &dev_attr_protocol.attr,
187 &dev_attr_supported_protocols.attr,
9714d587 188 NULL,
4714eda8
MCC
189};
190
626cf697
MCC
191static struct attribute_group ir_hw_dev_attr_grp = {
192 .attrs = ir_hw_dev_attrs,
945cdfa2
MCC
193};
194
626cf697
MCC
195static const struct attribute_group *ir_hw_dev_attr_groups[] = {
196 &ir_hw_dev_attr_grp,
945cdfa2
MCC
197 NULL
198};
199
626cf697
MCC
200static struct device_type rc_dev_type = {
201 .groups = ir_hw_dev_attr_groups,
202 .uevent = ir_dev_uevent,
203};
204
205static struct device_type ir_raw_dev_type = {
9c89a181 206 .uevent = ir_dev_uevent,
945cdfa2
MCC
207};
208
d4b778d3 209/**
de88f31c 210 * ir_register_class() - creates the sysfs for /sys/class/rc/rc?
d4b778d3
MCC
211 * @input_dev: the struct input_dev descriptor of the device
212 *
213 * This routine is used to register the syfs code for IR class
214 */
4714eda8
MCC
215int ir_register_class(struct input_dev *input_dev)
216{
217 int rc;
945cdfa2 218 const char *path;
4714eda8
MCC
219 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
220 int devno = find_first_zero_bit(&ir_core_dev_number,
221 IRRCV_NUM_DEVICES);
222
223 if (unlikely(devno < 0))
224 return devno;
225
626cf697
MCC
226 if (ir_dev->props->driver_type == RC_DRIVER_SCANCODE)
227 ir_dev->dev.type = &rc_dev_type;
228 else
229 ir_dev->dev.type = &ir_raw_dev_type;
230
945cdfa2
MCC
231 ir_dev->dev.class = &ir_input_class;
232 ir_dev->dev.parent = input_dev->dev.parent;
de88f31c 233 dev_set_name(&ir_dev->dev, "rc%d", devno);
9c89a181 234 dev_set_drvdata(&ir_dev->dev, ir_dev);
945cdfa2
MCC
235 rc = device_register(&ir_dev->dev);
236 if (rc)
237 return rc;
238
239
240 input_dev->dev.parent = &ir_dev->dev;
241 rc = input_register_device(input_dev);
242 if (rc < 0) {
243 device_del(&ir_dev->dev);
244 return rc;
4714eda8
MCC
245 }
246
945cdfa2
MCC
247 __module_get(THIS_MODULE);
248
9c89a181
MCC
249 path = kobject_get_path(&ir_dev->dev.kobj, GFP_KERNEL);
250 printk(KERN_INFO "%s: %s as %s\n",
945cdfa2
MCC
251 dev_name(&ir_dev->dev),
252 input_dev->name ? input_dev->name : "Unspecified device",
253 path ? path : "N/A");
254 kfree(path);
255
4714eda8
MCC
256 ir_dev->devno = devno;
257 set_bit(devno, &ir_core_dev_number);
258
259 return 0;
260};
261
d4b778d3
MCC
262/**
263 * ir_unregister_class() - removes the sysfs for sysfs for
de88f31c 264 * /sys/class/rc/rc?
d4b778d3
MCC
265 * @input_dev: the struct input_dev descriptor of the device
266 *
267 * This routine is used to unregister the syfs code for IR class
268 */
4714eda8
MCC
269void ir_unregister_class(struct input_dev *input_dev)
270{
271 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
4714eda8
MCC
272
273 clear_bit(ir_dev->devno, &ir_core_dev_number);
945cdfa2
MCC
274 input_unregister_device(input_dev);
275 device_del(&ir_dev->dev);
4714eda8 276
945cdfa2 277 module_put(THIS_MODULE);
4714eda8
MCC
278}
279
d4b778d3 280/*
e202c15b 281 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
d4b778d3
MCC
282 */
283
4714eda8
MCC
284static int __init ir_core_init(void)
285{
945cdfa2
MCC
286 int rc = class_register(&ir_input_class);
287 if (rc) {
e202c15b 288 printk(KERN_ERR "ir_core: unable to register rc class\n");
945cdfa2 289 return rc;
4714eda8
MCC
290 }
291
02858eed 292 /* Initialize/load the decoders/keymap code that will be used */
995187be 293 ir_raw_init();
02858eed
MCC
294 rc_map_init();
295
995187be 296
4714eda8
MCC
297 return 0;
298}
299
300static void __exit ir_core_exit(void)
301{
945cdfa2 302 class_unregister(&ir_input_class);
4714eda8
MCC
303}
304
305module_init(ir_core_init);
306module_exit(ir_core_exit);