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