]>
Commit | Line | Data |
---|---|---|
dd3f616d | 1 | /* ir-sysfs.c - sysfs interface for RC devices (/sys/class/rc) |
4714eda8 | 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 */ |
23 | static unsigned long ir_core_dev_number; | |
4714eda8 | 24 | |
e202c15b | 25 | /* class for /sys/class/rc */ |
945cdfa2 MCC |
26 | static 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 | 31 | static struct class ir_input_class = { |
e202c15b | 32 | .name = "rc", |
945cdfa2 MCC |
33 | .devnode = ir_devnode, |
34 | }; | |
4714eda8 | 35 | |
4403b7b4 MCC |
36 | static struct { |
37 | u64 type; | |
38 | char *name; | |
39 | } proto_names[] = { | |
40 | { IR_TYPE_UNKNOWN, "unknown" }, | |
a9e55ea9 | 41 | { IR_TYPE_RC5, "rc-5" }, |
4403b7b4 | 42 | { IR_TYPE_NEC, "nec" }, |
a9e55ea9 | 43 | { IR_TYPE_RC6, "rc-6" }, |
4403b7b4 MCC |
44 | { IR_TYPE_JVC, "jvc" }, |
45 | { IR_TYPE_SONY, "sony" }, | |
46 | }; | |
47 | ||
d4b778d3 | 48 | /** |
667c9ebe | 49 | * show_protocols() - shows the current IR protocol(s) |
d4b778d3 MCC |
50 | * @d: the device descriptor |
51 | * @mattr: the device attribute struct (unused) | |
52 | * @buf: a pointer to the output buffer | |
53 | * | |
667c9ebe DH |
54 | * This routine is a callback routine for input read the IR protocol type(s). |
55 | * it is trigged by reading /sys/class/rc/rc?/protocols. | |
56 | * It returns the protocol names of supported protocols. | |
57 | * Enabled protocols are printed in brackets. | |
d4b778d3 | 58 | */ |
667c9ebe DH |
59 | static ssize_t show_protocols(struct device *d, |
60 | struct device_attribute *mattr, char *buf) | |
53f87022 | 61 | { |
53f87022 | 62 | struct ir_input_dev *ir_dev = dev_get_drvdata(d); |
667c9ebe DH |
63 | u64 allowed, enabled; |
64 | char *tmp = buf; | |
4403b7b4 | 65 | int i; |
667c9ebe DH |
66 | |
67 | if (ir_dev->props->driver_type == RC_DRIVER_SCANCODE) { | |
68 | enabled = ir_dev->rc_tab.ir_type; | |
69 | allowed = ir_dev->props->allowed_protos; | |
70 | } else { | |
71 | enabled = ir_dev->raw->enabled_protocols; | |
72 | allowed = ir_raw_get_allowed_protocols(); | |
73 | } | |
53f87022 | 74 | |
667c9ebe DH |
75 | IR_dprintk(1, "allowed - 0x%llx, enabled - 0x%llx\n", |
76 | (long long)allowed, | |
77 | (long long)enabled); | |
78 | ||
4403b7b4 MCC |
79 | for (i = 0; i < ARRAY_SIZE(proto_names); i++) { |
80 | if (allowed & enabled & proto_names[i].type) | |
81 | tmp += sprintf(tmp, "[%s] ", proto_names[i].name); | |
82 | else if (allowed & proto_names[i].type) | |
83 | tmp += sprintf(tmp, "%s ", proto_names[i].name); | |
84 | } | |
667c9ebe DH |
85 | |
86 | if (tmp != buf) | |
87 | tmp--; | |
88 | *tmp = '\n'; | |
89 | return tmp + 1 - buf; | |
53f87022 MCC |
90 | } |
91 | ||
d4b778d3 | 92 | /** |
667c9ebe | 93 | * store_protocols() - changes the current IR protocol(s) |
d4b778d3 MCC |
94 | * @d: the device descriptor |
95 | * @mattr: the device attribute struct (unused) | |
96 | * @buf: a pointer to the input buffer | |
97 | * @len: length of the input buffer | |
98 | * | |
99 | * This routine is a callback routine for changing the IR protocol type. | |
667c9ebe DH |
100 | * It is trigged by writing to /sys/class/rc/rc?/protocols. |
101 | * Writing "+proto" will add a protocol to the list of enabled protocols. | |
102 | * Writing "-proto" will remove a protocol from the list of enabled protocols. | |
103 | * Writing "proto" will enable only "proto". | |
104 | * Returns -EINVAL if an invalid protocol combination or unknown protocol name | |
105 | * is used, otherwise @len. | |
d4b778d3 | 106 | */ |
667c9ebe DH |
107 | static ssize_t store_protocols(struct device *d, |
108 | struct device_attribute *mattr, | |
109 | const char *data, | |
110 | size_t len) | |
09b01b90 MCC |
111 | { |
112 | struct ir_input_dev *ir_dev = dev_get_drvdata(d); | |
667c9ebe DH |
113 | bool enable, disable; |
114 | const char *tmp; | |
115 | u64 type; | |
116 | u64 mask; | |
4403b7b4 | 117 | int rc, i; |
eecee32a | 118 | unsigned long flags; |
667c9ebe DH |
119 | |
120 | tmp = skip_spaces(data); | |
4403b7b4 MCC |
121 | if (*tmp == '\0') { |
122 | IR_dprintk(1, "Protocol not specified\n"); | |
123 | return -EINVAL; | |
124 | } else if (*tmp == '+') { | |
667c9ebe DH |
125 | enable = true; |
126 | disable = false; | |
127 | tmp++; | |
128 | } else if (*tmp == '-') { | |
129 | enable = false; | |
130 | disable = true; | |
131 | tmp++; | |
132 | } else { | |
133 | enable = false; | |
134 | disable = false; | |
b320f80a | 135 | } |
09b01b90 | 136 | |
4403b7b4 MCC |
137 | for (i = 0; i < ARRAY_SIZE(proto_names); i++) { |
138 | if (!strncasecmp(tmp, proto_names[i].name, strlen(proto_names[i].name))) { | |
139 | tmp += strlen(proto_names[i].name); | |
140 | mask = proto_names[i].type; | |
141 | break; | |
142 | } | |
143 | } | |
144 | if (i == ARRAY_SIZE(proto_names)) { | |
b320f80a | 145 | IR_dprintk(1, "Unknown protocol\n"); |
09b01b90 MCC |
146 | return -EINVAL; |
147 | } | |
148 | ||
667c9ebe DH |
149 | tmp = skip_spaces(tmp); |
150 | if (*tmp != '\0') { | |
151 | IR_dprintk(1, "Invalid trailing characters\n"); | |
09b01b90 MCC |
152 | return -EINVAL; |
153 | } | |
154 | ||
667c9ebe DH |
155 | if (ir_dev->props->driver_type == RC_DRIVER_SCANCODE) |
156 | type = ir_dev->rc_tab.ir_type; | |
157 | else | |
158 | type = ir_dev->raw->enabled_protocols; | |
09b01b90 | 159 | |
667c9ebe DH |
160 | if (enable) |
161 | type |= mask; | |
162 | else if (disable) | |
163 | type &= ~mask; | |
164 | else | |
165 | type = mask; | |
09b01b90 | 166 | |
667c9ebe DH |
167 | if (ir_dev->props && ir_dev->props->change_protocol) { |
168 | rc = ir_dev->props->change_protocol(ir_dev->props->priv, | |
169 | type); | |
170 | if (rc < 0) { | |
171 | IR_dprintk(1, "Error setting protocols to 0x%llx\n", | |
172 | (long long)type); | |
173 | return -EINVAL; | |
174 | } | |
175 | } | |
09b01b90 | 176 | |
667c9ebe DH |
177 | if (ir_dev->props->driver_type == RC_DRIVER_SCANCODE) { |
178 | spin_lock_irqsave(&ir_dev->rc_tab.lock, flags); | |
179 | ir_dev->rc_tab.ir_type = type; | |
180 | spin_unlock_irqrestore(&ir_dev->rc_tab.lock, flags); | |
181 | } else { | |
182 | ir_dev->raw->enabled_protocols = type; | |
183 | } | |
b320f80a | 184 | |
b320f80a | 185 | |
667c9ebe DH |
186 | IR_dprintk(1, "Current protocol(s): 0x%llx\n", |
187 | (long long)type); | |
b320f80a | 188 | |
667c9ebe | 189 | return len; |
b320f80a | 190 | } |
9c89a181 MCC |
191 | |
192 | #define ADD_HOTPLUG_VAR(fmt, val...) \ | |
193 | do { \ | |
194 | int err = add_uevent_var(env, fmt, val); \ | |
195 | if (err) \ | |
196 | return err; \ | |
197 | } while (0) | |
198 | ||
667c9ebe | 199 | static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env) |
9c89a181 MCC |
200 | { |
201 | struct ir_input_dev *ir_dev = dev_get_drvdata(device); | |
202 | ||
203 | if (ir_dev->rc_tab.name) | |
3efaa062 | 204 | ADD_HOTPLUG_VAR("NAME=%s", ir_dev->rc_tab.name); |
727e625c | 205 | if (ir_dev->driver_name) |
3efaa062 | 206 | ADD_HOTPLUG_VAR("DRV_NAME=%s", ir_dev->driver_name); |
9c89a181 MCC |
207 | |
208 | return 0; | |
209 | } | |
210 | ||
d4b778d3 MCC |
211 | /* |
212 | * Static device attribute struct with the sysfs attributes for IR's | |
213 | */ | |
667c9ebe DH |
214 | static DEVICE_ATTR(protocols, S_IRUGO | S_IWUSR, |
215 | show_protocols, store_protocols); | |
b320f80a | 216 | |
667c9ebe DH |
217 | static struct attribute *rc_dev_attrs[] = { |
218 | &dev_attr_protocols.attr, | |
9714d587 | 219 | NULL, |
4714eda8 MCC |
220 | }; |
221 | ||
667c9ebe DH |
222 | static struct attribute_group rc_dev_attr_grp = { |
223 | .attrs = rc_dev_attrs, | |
945cdfa2 MCC |
224 | }; |
225 | ||
667c9ebe DH |
226 | static const struct attribute_group *rc_dev_attr_groups[] = { |
227 | &rc_dev_attr_grp, | |
945cdfa2 MCC |
228 | NULL |
229 | }; | |
230 | ||
626cf697 | 231 | static struct device_type rc_dev_type = { |
667c9ebe DH |
232 | .groups = rc_dev_attr_groups, |
233 | .uevent = rc_dev_uevent, | |
945cdfa2 MCC |
234 | }; |
235 | ||
d4b778d3 | 236 | /** |
de88f31c | 237 | * ir_register_class() - creates the sysfs for /sys/class/rc/rc? |
d4b778d3 MCC |
238 | * @input_dev: the struct input_dev descriptor of the device |
239 | * | |
240 | * This routine is used to register the syfs code for IR class | |
241 | */ | |
4714eda8 MCC |
242 | int ir_register_class(struct input_dev *input_dev) |
243 | { | |
244 | int rc; | |
945cdfa2 | 245 | const char *path; |
4714eda8 MCC |
246 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
247 | int devno = find_first_zero_bit(&ir_core_dev_number, | |
248 | IRRCV_NUM_DEVICES); | |
249 | ||
250 | if (unlikely(devno < 0)) | |
251 | return devno; | |
252 | ||
667c9ebe | 253 | ir_dev->dev.type = &rc_dev_type; |
626cf697 | 254 | |
945cdfa2 MCC |
255 | ir_dev->dev.class = &ir_input_class; |
256 | ir_dev->dev.parent = input_dev->dev.parent; | |
de88f31c | 257 | dev_set_name(&ir_dev->dev, "rc%d", devno); |
9c89a181 | 258 | dev_set_drvdata(&ir_dev->dev, ir_dev); |
945cdfa2 MCC |
259 | rc = device_register(&ir_dev->dev); |
260 | if (rc) | |
261 | return rc; | |
262 | ||
263 | ||
264 | input_dev->dev.parent = &ir_dev->dev; | |
265 | rc = input_register_device(input_dev); | |
266 | if (rc < 0) { | |
267 | device_del(&ir_dev->dev); | |
268 | return rc; | |
4714eda8 MCC |
269 | } |
270 | ||
945cdfa2 MCC |
271 | __module_get(THIS_MODULE); |
272 | ||
9c89a181 MCC |
273 | path = kobject_get_path(&ir_dev->dev.kobj, GFP_KERNEL); |
274 | printk(KERN_INFO "%s: %s as %s\n", | |
945cdfa2 MCC |
275 | dev_name(&ir_dev->dev), |
276 | input_dev->name ? input_dev->name : "Unspecified device", | |
277 | path ? path : "N/A"); | |
278 | kfree(path); | |
279 | ||
4714eda8 MCC |
280 | ir_dev->devno = devno; |
281 | set_bit(devno, &ir_core_dev_number); | |
282 | ||
283 | return 0; | |
284 | }; | |
285 | ||
d4b778d3 MCC |
286 | /** |
287 | * ir_unregister_class() - removes the sysfs for sysfs for | |
de88f31c | 288 | * /sys/class/rc/rc? |
d4b778d3 MCC |
289 | * @input_dev: the struct input_dev descriptor of the device |
290 | * | |
291 | * This routine is used to unregister the syfs code for IR class | |
292 | */ | |
4714eda8 MCC |
293 | void ir_unregister_class(struct input_dev *input_dev) |
294 | { | |
295 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); | |
4714eda8 MCC |
296 | |
297 | clear_bit(ir_dev->devno, &ir_core_dev_number); | |
945cdfa2 MCC |
298 | input_unregister_device(input_dev); |
299 | device_del(&ir_dev->dev); | |
4714eda8 | 300 | |
945cdfa2 | 301 | module_put(THIS_MODULE); |
4714eda8 MCC |
302 | } |
303 | ||
d4b778d3 | 304 | /* |
e202c15b | 305 | * Init/exit code for the module. Basically, creates/removes /sys/class/rc |
d4b778d3 MCC |
306 | */ |
307 | ||
4714eda8 MCC |
308 | static int __init ir_core_init(void) |
309 | { | |
945cdfa2 MCC |
310 | int rc = class_register(&ir_input_class); |
311 | if (rc) { | |
e202c15b | 312 | printk(KERN_ERR "ir_core: unable to register rc class\n"); |
945cdfa2 | 313 | return rc; |
4714eda8 MCC |
314 | } |
315 | ||
02858eed | 316 | /* Initialize/load the decoders/keymap code that will be used */ |
995187be MCC |
317 | ir_raw_init(); |
318 | ||
4714eda8 MCC |
319 | return 0; |
320 | } | |
321 | ||
322 | static void __exit ir_core_exit(void) | |
323 | { | |
945cdfa2 | 324 | class_unregister(&ir_input_class); |
4714eda8 MCC |
325 | } |
326 | ||
327 | module_init(ir_core_init); | |
328 | module_exit(ir_core_exit); |