V4L/DVB (13638): ir-core: documment missed functions
[cascardo/linux.git] / drivers / media / IR / ir-sysfs.c
1 /* ir-register.c - handle IR scancode->keycode tables
2  *
3  * Copyright (C) 2009 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/input.h>
16 #include <linux/device.h>
17 #include <media/ir-core.h>
18
19 #define IRRCV_NUM_DEVICES       256
20
21 /* bit array to represent IR sysfs device number */
22 static unsigned long ir_core_dev_number;
23
24 /* class for /sys/class/irrcv */
25 static struct class *ir_input_class;
26
27 /**
28  * show_protocol() - shows the current IR protocol
29  * @d:          the device descriptor
30  * @mattr:      the device attribute struct (unused)
31  * @buf:        a pointer to the output buffer
32  *
33  * This routine is a callback routine for input read the IR protocol type.
34  * it is trigged by reading /sys/class/irrcv/irrcv?/current_protocol.
35  * It returns the protocol name, as understood by the driver.
36  */
37 static ssize_t show_protocol(struct device *d,
38                              struct device_attribute *mattr, char *buf)
39 {
40         char *s;
41         struct ir_input_dev *ir_dev = dev_get_drvdata(d);
42         enum ir_type ir_type = ir_dev->rc_tab.ir_type;
43
44         IR_dprintk(1, "Current protocol is %ld\n", ir_type);
45
46         /* FIXME: doesn't support multiple protocols at the same time */
47         if (ir_type == IR_TYPE_UNKNOWN)
48                 s = "Unknown";
49         else if (ir_type == IR_TYPE_RC5)
50                 s = "RC-5";
51         else if (ir_type == IR_TYPE_PD)
52                 s = "Pulse/distance";
53         else if (ir_type == IR_TYPE_NEC)
54                 s = "NEC";
55         else
56                 s = "Other";
57
58         return sprintf(buf, "%s\n", s);
59 }
60
61 /**
62  * store_protocol() - shows the current IR protocol
63  * @d:          the device descriptor
64  * @mattr:      the device attribute struct (unused)
65  * @buf:        a pointer to the input buffer
66  * @len:        length of the input buffer
67  *
68  * This routine is a callback routine for changing the IR protocol type.
69  * it is trigged by reading /sys/class/irrcv/irrcv?/current_protocol.
70  * It changes the IR the protocol name, if the IR type is recognized
71  * by the driver.
72  * If an unknown protocol name is used, returns -EINVAL.
73  */
74 static ssize_t store_protocol(struct device *d,
75                               struct device_attribute *mattr,
76                               const char *data,
77                               size_t len)
78 {
79         struct ir_input_dev *ir_dev = dev_get_drvdata(d);
80         enum ir_type ir_type = IR_TYPE_UNKNOWN;
81         int rc = -EINVAL;
82         char *buf;
83
84         buf = strsep((char **) &data, "\n");
85
86         if (!strcasecmp(buf, "rc-5"))
87                 ir_type = IR_TYPE_RC5;
88         else if (!strcasecmp(buf, "pd"))
89                 ir_type = IR_TYPE_PD;
90         else if (!strcasecmp(buf, "nec"))
91                 ir_type = IR_TYPE_NEC;
92
93         if (ir_type == IR_TYPE_UNKNOWN) {
94                 IR_dprintk(1, "Error setting protocol to %ld\n", ir_type);
95                 return -EINVAL;
96         }
97
98         if (ir_dev->props->change_protocol)
99                 rc = ir_dev->props->change_protocol(ir_dev->props->priv,
100                                                     ir_type);
101
102         if (rc < 0) {
103                 IR_dprintk(1, "Error setting protocol to %ld\n", ir_type);
104                 return -EINVAL;
105         }
106
107         ir_dev->rc_tab.ir_type = ir_type;
108
109         IR_dprintk(1, "Current protocol is %ld\n", ir_type);
110
111         return len;
112 }
113
114 /*
115  * Static device attribute struct with the sysfs attributes for IR's
116  */
117 static DEVICE_ATTR(current_protocol, S_IRUGO | S_IWUSR,
118                    show_protocol, store_protocol);
119
120 static struct attribute *ir_dev_attrs[] = {
121         &dev_attr_current_protocol.attr,
122 };
123
124 /**
125  * ir_register_class() - creates the sysfs for /sys/class/irrcv/irrcv?
126  * @input_dev:  the struct input_dev descriptor of the device
127  *
128  * This routine is used to register the syfs code for IR class
129  */
130 int ir_register_class(struct input_dev *input_dev)
131 {
132         int rc;
133         struct kobject *kobj;
134
135         struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
136         int devno = find_first_zero_bit(&ir_core_dev_number,
137                                         IRRCV_NUM_DEVICES);
138
139         if (unlikely(devno < 0))
140                 return devno;
141
142         ir_dev->attr.attrs = ir_dev_attrs;
143         ir_dev->class_dev = device_create(ir_input_class, NULL,
144                                           input_dev->dev.devt, ir_dev,
145                                           "irrcv%d", devno);
146         kobj = &ir_dev->class_dev->kobj;
147
148         printk(KERN_WARNING "Creating IR device %s\n", kobject_name(kobj));
149         rc = sysfs_create_group(kobj, &ir_dev->attr);
150         if (unlikely(rc < 0)) {
151                 device_destroy(ir_input_class, input_dev->dev.devt);
152                 return -ENOMEM;
153         }
154
155         ir_dev->devno = devno;
156         set_bit(devno, &ir_core_dev_number);
157
158         return 0;
159 };
160
161 /**
162  * ir_unregister_class() - removes the sysfs for sysfs for
163  *                         /sys/class/irrcv/irrcv?
164  * @input_dev:  the struct input_dev descriptor of the device
165  *
166  * This routine is used to unregister the syfs code for IR class
167  */
168 void ir_unregister_class(struct input_dev *input_dev)
169 {
170         struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
171         struct kobject *kobj;
172
173         clear_bit(ir_dev->devno, &ir_core_dev_number);
174
175         kobj = &ir_dev->class_dev->kobj;
176
177         sysfs_remove_group(kobj, &ir_dev->attr);
178         device_destroy(ir_input_class, input_dev->dev.devt);
179
180         kfree(ir_dev->attr.name);
181 }
182
183 /*
184  * Init/exit code for the module. Basically, creates/removes /sys/class/irrcv
185  */
186
187 static int __init ir_core_init(void)
188 {
189         ir_input_class = class_create(THIS_MODULE, "irrcv");
190         if (IS_ERR(ir_input_class)) {
191                 printk(KERN_ERR "ir_core: unable to register irrcv class\n");
192                 return PTR_ERR(ir_input_class);
193         }
194
195         return 0;
196 }
197
198 static void __exit ir_core_exit(void)
199 {
200         class_destroy(ir_input_class);
201 }
202
203 module_init(ir_core_init);
204 module_exit(ir_core_exit);