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