]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/media/IR/ir-sysfs.c
V4L/DVB: rename sysfs remote controller devices from rcrcv to rc
[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>
18#include <media/ir-core.h>
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)
59 s = "RC-5";
60 else if (ir_type == IR_TYPE_PD)
61 s = "Pulse/distance";
62 else if (ir_type == IR_TYPE_NEC)
63 s = "NEC";
64 else
65 s = "Other";
66
67 return sprintf(buf, "%s\n", s);
68}
69
d4b778d3
MCC
70/**
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
76 *
77 * This routine is a callback routine for changing the IR protocol type.
de88f31c 78 * it is trigged by reading /sys/class/rc/rc?/current_protocol.
d4b778d3
MCC
79 * It changes the IR the protocol name, if the IR type is recognized
80 * by the driver.
81 * If an unknown protocol name is used, returns -EINVAL.
82 */
09b01b90
MCC
83static ssize_t store_protocol(struct device *d,
84 struct device_attribute *mattr,
85 const char *data,
86 size_t len)
87{
88 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
971e8298 89 u64 ir_type = IR_TYPE_UNKNOWN;
09b01b90 90 int rc = -EINVAL;
eecee32a 91 unsigned long flags;
09b01b90
MCC
92 char *buf;
93
94 buf = strsep((char **) &data, "\n");
95
ecf6e72d 96 if (!strcasecmp(buf, "rc-5") || !strcasecmp(buf, "rc5"))
09b01b90
MCC
97 ir_type = IR_TYPE_RC5;
98 else if (!strcasecmp(buf, "pd"))
99 ir_type = IR_TYPE_PD;
100 else if (!strcasecmp(buf, "nec"))
101 ir_type = IR_TYPE_NEC;
102
103 if (ir_type == IR_TYPE_UNKNOWN) {
971e8298
MCC
104 IR_dprintk(1, "Error setting protocol to %lld\n",
105 (long long)ir_type);
09b01b90
MCC
106 return -EINVAL;
107 }
108
0f7ff395 109 if (ir_dev->props && ir_dev->props->change_protocol)
09b01b90
MCC
110 rc = ir_dev->props->change_protocol(ir_dev->props->priv,
111 ir_type);
112
113 if (rc < 0) {
971e8298
MCC
114 IR_dprintk(1, "Error setting protocol to %lld\n",
115 (long long)ir_type);
09b01b90
MCC
116 return -EINVAL;
117 }
118
eecee32a 119 spin_lock_irqsave(&ir_dev->rc_tab.lock, flags);
09b01b90 120 ir_dev->rc_tab.ir_type = ir_type;
eecee32a 121 spin_unlock_irqrestore(&ir_dev->rc_tab.lock, flags);
09b01b90 122
971e8298
MCC
123 IR_dprintk(1, "Current protocol is %lld\n",
124 (long long)ir_type);
09b01b90
MCC
125
126 return len;
127}
128
9c89a181
MCC
129
130#define ADD_HOTPLUG_VAR(fmt, val...) \
131 do { \
132 int err = add_uevent_var(env, fmt, val); \
133 if (err) \
134 return err; \
135 } while (0)
136
137static int ir_dev_uevent(struct device *device, struct kobj_uevent_env *env)
138{
139 struct ir_input_dev *ir_dev = dev_get_drvdata(device);
140
141 if (ir_dev->rc_tab.name)
142 ADD_HOTPLUG_VAR("NAME=\"%s\"", ir_dev->rc_tab.name);
727e625c
MCC
143 if (ir_dev->driver_name)
144 ADD_HOTPLUG_VAR("DRV_NAME=\"%s\"", ir_dev->driver_name);
9c89a181
MCC
145
146 return 0;
147}
148
d4b778d3
MCC
149/*
150 * Static device attribute struct with the sysfs attributes for IR's
151 */
53f87022 152static DEVICE_ATTR(current_protocol, S_IRUGO | S_IWUSR,
09b01b90 153 show_protocol, store_protocol);
4714eda8
MCC
154
155static struct attribute *ir_dev_attrs[] = {
53f87022 156 &dev_attr_current_protocol.attr,
9714d587 157 NULL,
4714eda8
MCC
158};
159
945cdfa2
MCC
160static struct attribute_group ir_dev_attr_grp = {
161 .attrs = ir_dev_attrs,
162};
163
164static const struct attribute_group *ir_dev_attr_groups[] = {
165 &ir_dev_attr_grp,
166 NULL
167};
168
169static struct device_type ir_dev_type = {
170 .groups = ir_dev_attr_groups,
9c89a181 171 .uevent = ir_dev_uevent,
945cdfa2
MCC
172};
173
d4b778d3 174/**
de88f31c 175 * ir_register_class() - creates the sysfs for /sys/class/rc/rc?
d4b778d3
MCC
176 * @input_dev: the struct input_dev descriptor of the device
177 *
178 * This routine is used to register the syfs code for IR class
179 */
4714eda8
MCC
180int ir_register_class(struct input_dev *input_dev)
181{
182 int rc;
945cdfa2 183 const char *path;
4714eda8
MCC
184
185 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
186 int devno = find_first_zero_bit(&ir_core_dev_number,
187 IRRCV_NUM_DEVICES);
188
189 if (unlikely(devno < 0))
190 return devno;
191
945cdfa2
MCC
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;
de88f31c 195 dev_set_name(&ir_dev->dev, "rc%d", devno);
9c89a181 196 dev_set_drvdata(&ir_dev->dev, ir_dev);
945cdfa2
MCC
197 rc = device_register(&ir_dev->dev);
198 if (rc)
199 return rc;
200
201
202 input_dev->dev.parent = &ir_dev->dev;
203 rc = input_register_device(input_dev);
204 if (rc < 0) {
205 device_del(&ir_dev->dev);
206 return rc;
4714eda8
MCC
207 }
208
945cdfa2
MCC
209 __module_get(THIS_MODULE);
210
9c89a181
MCC
211 path = kobject_get_path(&ir_dev->dev.kobj, GFP_KERNEL);
212 printk(KERN_INFO "%s: %s as %s\n",
945cdfa2
MCC
213 dev_name(&ir_dev->dev),
214 input_dev->name ? input_dev->name : "Unspecified device",
215 path ? path : "N/A");
216 kfree(path);
217
4714eda8
MCC
218 ir_dev->devno = devno;
219 set_bit(devno, &ir_core_dev_number);
220
221 return 0;
222};
223
d4b778d3
MCC
224/**
225 * ir_unregister_class() - removes the sysfs for sysfs for
de88f31c 226 * /sys/class/rc/rc?
d4b778d3
MCC
227 * @input_dev: the struct input_dev descriptor of the device
228 *
229 * This routine is used to unregister the syfs code for IR class
230 */
4714eda8
MCC
231void ir_unregister_class(struct input_dev *input_dev)
232{
233 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
4714eda8
MCC
234
235 clear_bit(ir_dev->devno, &ir_core_dev_number);
945cdfa2
MCC
236 input_unregister_device(input_dev);
237 device_del(&ir_dev->dev);
4714eda8 238
945cdfa2 239 module_put(THIS_MODULE);
4714eda8
MCC
240}
241
d4b778d3 242/*
e202c15b 243 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
d4b778d3
MCC
244 */
245
4714eda8
MCC
246static int __init ir_core_init(void)
247{
945cdfa2
MCC
248 int rc = class_register(&ir_input_class);
249 if (rc) {
e202c15b 250 printk(KERN_ERR "ir_core: unable to register rc class\n");
945cdfa2 251 return rc;
4714eda8
MCC
252 }
253
02858eed 254 /* Initialize/load the decoders/keymap code that will be used */
995187be 255 ir_raw_init();
02858eed
MCC
256 rc_map_init();
257
995187be 258
4714eda8
MCC
259 return 0;
260}
261
262static void __exit ir_core_exit(void)
263{
945cdfa2 264 class_unregister(&ir_input_class);
4714eda8
MCC
265}
266
267module_init(ir_core_init);
268module_exit(ir_core_exit);