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