]>
Commit | Line | Data |
---|---|---|
1 | /* rc-main.c - Remote Controller core module | |
2 | * | |
3 | * Copyright (C) 2009-2010 by Mauro Carvalho Chehab | |
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 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
16 | ||
17 | #include <media/rc-core.h> | |
18 | #include <linux/bsearch.h> | |
19 | #include <linux/spinlock.h> | |
20 | #include <linux/delay.h> | |
21 | #include <linux/input.h> | |
22 | #include <linux/leds.h> | |
23 | #include <linux/slab.h> | |
24 | #include <linux/idr.h> | |
25 | #include <linux/device.h> | |
26 | #include <linux/module.h> | |
27 | #include "rc-core-priv.h" | |
28 | ||
29 | /* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */ | |
30 | #define IR_TAB_MIN_SIZE 256 | |
31 | #define IR_TAB_MAX_SIZE 8192 | |
32 | #define RC_DEV_MAX 256 | |
33 | ||
34 | static const struct { | |
35 | const char *name; | |
36 | unsigned int repeat_period; | |
37 | unsigned int scancode_bits; | |
38 | } protocols[] = { | |
39 | [RC_PROTO_UNKNOWN] = { .name = "unknown", .repeat_period = 250 }, | |
40 | [RC_PROTO_OTHER] = { .name = "other", .repeat_period = 250 }, | |
41 | [RC_PROTO_RC5] = { .name = "rc-5", | |
42 | .scancode_bits = 0x1f7f, .repeat_period = 164 }, | |
43 | [RC_PROTO_RC5X_20] = { .name = "rc-5x-20", | |
44 | .scancode_bits = 0x1f7f3f, .repeat_period = 164 }, | |
45 | [RC_PROTO_RC5_SZ] = { .name = "rc-5-sz", | |
46 | .scancode_bits = 0x2fff, .repeat_period = 164 }, | |
47 | [RC_PROTO_JVC] = { .name = "jvc", | |
48 | .scancode_bits = 0xffff, .repeat_period = 250 }, | |
49 | [RC_PROTO_SONY12] = { .name = "sony-12", | |
50 | .scancode_bits = 0x1f007f, .repeat_period = 100 }, | |
51 | [RC_PROTO_SONY15] = { .name = "sony-15", | |
52 | .scancode_bits = 0xff007f, .repeat_period = 100 }, | |
53 | [RC_PROTO_SONY20] = { .name = "sony-20", | |
54 | .scancode_bits = 0x1fff7f, .repeat_period = 100 }, | |
55 | [RC_PROTO_NEC] = { .name = "nec", | |
56 | .scancode_bits = 0xffff, .repeat_period = 160 }, | |
57 | [RC_PROTO_NECX] = { .name = "nec-x", | |
58 | .scancode_bits = 0xffffff, .repeat_period = 160 }, | |
59 | [RC_PROTO_NEC32] = { .name = "nec-32", | |
60 | .scancode_bits = 0xffffffff, .repeat_period = 160 }, | |
61 | [RC_PROTO_SANYO] = { .name = "sanyo", | |
62 | .scancode_bits = 0x1fffff, .repeat_period = 250 }, | |
63 | [RC_PROTO_MCIR2_KBD] = { .name = "mcir2-kbd", | |
64 | .scancode_bits = 0xffff, .repeat_period = 150 }, | |
65 | [RC_PROTO_MCIR2_MSE] = { .name = "mcir2-mse", | |
66 | .scancode_bits = 0x1fffff, .repeat_period = 150 }, | |
67 | [RC_PROTO_RC6_0] = { .name = "rc-6-0", | |
68 | .scancode_bits = 0xffff, .repeat_period = 164 }, | |
69 | [RC_PROTO_RC6_6A_20] = { .name = "rc-6-6a-20", | |
70 | .scancode_bits = 0xfffff, .repeat_period = 164 }, | |
71 | [RC_PROTO_RC6_6A_24] = { .name = "rc-6-6a-24", | |
72 | .scancode_bits = 0xffffff, .repeat_period = 164 }, | |
73 | [RC_PROTO_RC6_6A_32] = { .name = "rc-6-6a-32", | |
74 | .scancode_bits = 0xffffffff, .repeat_period = 164 }, | |
75 | [RC_PROTO_RC6_MCE] = { .name = "rc-6-mce", | |
76 | .scancode_bits = 0xffff7fff, .repeat_period = 164 }, | |
77 | [RC_PROTO_SHARP] = { .name = "sharp", | |
78 | .scancode_bits = 0x1fff, .repeat_period = 250 }, | |
79 | [RC_PROTO_XMP] = { .name = "xmp", .repeat_period = 250 }, | |
80 | [RC_PROTO_CEC] = { .name = "cec", .repeat_period = 550 }, | |
81 | }; | |
82 | ||
83 | /* Used to keep track of known keymaps */ | |
84 | static LIST_HEAD(rc_map_list); | |
85 | static DEFINE_SPINLOCK(rc_map_lock); | |
86 | static struct led_trigger *led_feedback; | |
87 | ||
88 | /* Used to keep track of rc devices */ | |
89 | static DEFINE_IDA(rc_ida); | |
90 | ||
91 | static struct rc_map_list *seek_rc_map(const char *name) | |
92 | { | |
93 | struct rc_map_list *map = NULL; | |
94 | ||
95 | spin_lock(&rc_map_lock); | |
96 | list_for_each_entry(map, &rc_map_list, list) { | |
97 | if (!strcmp(name, map->map.name)) { | |
98 | spin_unlock(&rc_map_lock); | |
99 | return map; | |
100 | } | |
101 | } | |
102 | spin_unlock(&rc_map_lock); | |
103 | ||
104 | return NULL; | |
105 | } | |
106 | ||
107 | struct rc_map *rc_map_get(const char *name) | |
108 | { | |
109 | ||
110 | struct rc_map_list *map; | |
111 | ||
112 | map = seek_rc_map(name); | |
113 | #ifdef CONFIG_MODULES | |
114 | if (!map) { | |
115 | int rc = request_module("%s", name); | |
116 | if (rc < 0) { | |
117 | pr_err("Couldn't load IR keymap %s\n", name); | |
118 | return NULL; | |
119 | } | |
120 | msleep(20); /* Give some time for IR to register */ | |
121 | ||
122 | map = seek_rc_map(name); | |
123 | } | |
124 | #endif | |
125 | if (!map) { | |
126 | pr_err("IR keymap %s not found\n", name); | |
127 | return NULL; | |
128 | } | |
129 | ||
130 | printk(KERN_INFO "Registered IR keymap %s\n", map->map.name); | |
131 | ||
132 | return &map->map; | |
133 | } | |
134 | EXPORT_SYMBOL_GPL(rc_map_get); | |
135 | ||
136 | int rc_map_register(struct rc_map_list *map) | |
137 | { | |
138 | spin_lock(&rc_map_lock); | |
139 | list_add_tail(&map->list, &rc_map_list); | |
140 | spin_unlock(&rc_map_lock); | |
141 | return 0; | |
142 | } | |
143 | EXPORT_SYMBOL_GPL(rc_map_register); | |
144 | ||
145 | void rc_map_unregister(struct rc_map_list *map) | |
146 | { | |
147 | spin_lock(&rc_map_lock); | |
148 | list_del(&map->list); | |
149 | spin_unlock(&rc_map_lock); | |
150 | } | |
151 | EXPORT_SYMBOL_GPL(rc_map_unregister); | |
152 | ||
153 | ||
154 | static struct rc_map_table empty[] = { | |
155 | { 0x2a, KEY_COFFEE }, | |
156 | }; | |
157 | ||
158 | static struct rc_map_list empty_map = { | |
159 | .map = { | |
160 | .scan = empty, | |
161 | .size = ARRAY_SIZE(empty), | |
162 | .rc_proto = RC_PROTO_UNKNOWN, /* Legacy IR type */ | |
163 | .name = RC_MAP_EMPTY, | |
164 | } | |
165 | }; | |
166 | ||
167 | /** | |
168 | * ir_create_table() - initializes a scancode table | |
169 | * @rc_map: the rc_map to initialize | |
170 | * @name: name to assign to the table | |
171 | * @rc_proto: ir type to assign to the new table | |
172 | * @size: initial size of the table | |
173 | * @return: zero on success or a negative error code | |
174 | * | |
175 | * This routine will initialize the rc_map and will allocate | |
176 | * memory to hold at least the specified number of elements. | |
177 | */ | |
178 | static int ir_create_table(struct rc_map *rc_map, | |
179 | const char *name, u64 rc_proto, size_t size) | |
180 | { | |
181 | rc_map->name = kstrdup(name, GFP_KERNEL); | |
182 | if (!rc_map->name) | |
183 | return -ENOMEM; | |
184 | rc_map->rc_proto = rc_proto; | |
185 | rc_map->alloc = roundup_pow_of_two(size * sizeof(struct rc_map_table)); | |
186 | rc_map->size = rc_map->alloc / sizeof(struct rc_map_table); | |
187 | rc_map->scan = kmalloc(rc_map->alloc, GFP_KERNEL); | |
188 | if (!rc_map->scan) { | |
189 | kfree(rc_map->name); | |
190 | rc_map->name = NULL; | |
191 | return -ENOMEM; | |
192 | } | |
193 | ||
194 | IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n", | |
195 | rc_map->size, rc_map->alloc); | |
196 | return 0; | |
197 | } | |
198 | ||
199 | /** | |
200 | * ir_free_table() - frees memory allocated by a scancode table | |
201 | * @rc_map: the table whose mappings need to be freed | |
202 | * | |
203 | * This routine will free memory alloctaed for key mappings used by given | |
204 | * scancode table. | |
205 | */ | |
206 | static void ir_free_table(struct rc_map *rc_map) | |
207 | { | |
208 | rc_map->size = 0; | |
209 | kfree(rc_map->name); | |
210 | rc_map->name = NULL; | |
211 | kfree(rc_map->scan); | |
212 | rc_map->scan = NULL; | |
213 | } | |
214 | ||
215 | /** | |
216 | * ir_resize_table() - resizes a scancode table if necessary | |
217 | * @rc_map: the rc_map to resize | |
218 | * @gfp_flags: gfp flags to use when allocating memory | |
219 | * @return: zero on success or a negative error code | |
220 | * | |
221 | * This routine will shrink the rc_map if it has lots of | |
222 | * unused entries and grow it if it is full. | |
223 | */ | |
224 | static int ir_resize_table(struct rc_map *rc_map, gfp_t gfp_flags) | |
225 | { | |
226 | unsigned int oldalloc = rc_map->alloc; | |
227 | unsigned int newalloc = oldalloc; | |
228 | struct rc_map_table *oldscan = rc_map->scan; | |
229 | struct rc_map_table *newscan; | |
230 | ||
231 | if (rc_map->size == rc_map->len) { | |
232 | /* All entries in use -> grow keytable */ | |
233 | if (rc_map->alloc >= IR_TAB_MAX_SIZE) | |
234 | return -ENOMEM; | |
235 | ||
236 | newalloc *= 2; | |
237 | IR_dprintk(1, "Growing table to %u bytes\n", newalloc); | |
238 | } | |
239 | ||
240 | if ((rc_map->len * 3 < rc_map->size) && (oldalloc > IR_TAB_MIN_SIZE)) { | |
241 | /* Less than 1/3 of entries in use -> shrink keytable */ | |
242 | newalloc /= 2; | |
243 | IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc); | |
244 | } | |
245 | ||
246 | if (newalloc == oldalloc) | |
247 | return 0; | |
248 | ||
249 | newscan = kmalloc(newalloc, gfp_flags); | |
250 | if (!newscan) { | |
251 | IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc); | |
252 | return -ENOMEM; | |
253 | } | |
254 | ||
255 | memcpy(newscan, rc_map->scan, rc_map->len * sizeof(struct rc_map_table)); | |
256 | rc_map->scan = newscan; | |
257 | rc_map->alloc = newalloc; | |
258 | rc_map->size = rc_map->alloc / sizeof(struct rc_map_table); | |
259 | kfree(oldscan); | |
260 | return 0; | |
261 | } | |
262 | ||
263 | /** | |
264 | * ir_update_mapping() - set a keycode in the scancode->keycode table | |
265 | * @dev: the struct rc_dev device descriptor | |
266 | * @rc_map: scancode table to be adjusted | |
267 | * @index: index of the mapping that needs to be updated | |
268 | * @keycode: the desired keycode | |
269 | * @return: previous keycode assigned to the mapping | |
270 | * | |
271 | * This routine is used to update scancode->keycode mapping at given | |
272 | * position. | |
273 | */ | |
274 | static unsigned int ir_update_mapping(struct rc_dev *dev, | |
275 | struct rc_map *rc_map, | |
276 | unsigned int index, | |
277 | unsigned int new_keycode) | |
278 | { | |
279 | int old_keycode = rc_map->scan[index].keycode; | |
280 | int i; | |
281 | ||
282 | /* Did the user wish to remove the mapping? */ | |
283 | if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) { | |
284 | IR_dprintk(1, "#%d: Deleting scan 0x%04x\n", | |
285 | index, rc_map->scan[index].scancode); | |
286 | rc_map->len--; | |
287 | memmove(&rc_map->scan[index], &rc_map->scan[index+ 1], | |
288 | (rc_map->len - index) * sizeof(struct rc_map_table)); | |
289 | } else { | |
290 | IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n", | |
291 | index, | |
292 | old_keycode == KEY_RESERVED ? "New" : "Replacing", | |
293 | rc_map->scan[index].scancode, new_keycode); | |
294 | rc_map->scan[index].keycode = new_keycode; | |
295 | __set_bit(new_keycode, dev->input_dev->keybit); | |
296 | } | |
297 | ||
298 | if (old_keycode != KEY_RESERVED) { | |
299 | /* A previous mapping was updated... */ | |
300 | __clear_bit(old_keycode, dev->input_dev->keybit); | |
301 | /* ... but another scancode might use the same keycode */ | |
302 | for (i = 0; i < rc_map->len; i++) { | |
303 | if (rc_map->scan[i].keycode == old_keycode) { | |
304 | __set_bit(old_keycode, dev->input_dev->keybit); | |
305 | break; | |
306 | } | |
307 | } | |
308 | ||
309 | /* Possibly shrink the keytable, failure is not a problem */ | |
310 | ir_resize_table(rc_map, GFP_ATOMIC); | |
311 | } | |
312 | ||
313 | return old_keycode; | |
314 | } | |
315 | ||
316 | /** | |
317 | * ir_establish_scancode() - set a keycode in the scancode->keycode table | |
318 | * @dev: the struct rc_dev device descriptor | |
319 | * @rc_map: scancode table to be searched | |
320 | * @scancode: the desired scancode | |
321 | * @resize: controls whether we allowed to resize the table to | |
322 | * accommodate not yet present scancodes | |
323 | * @return: index of the mapping containing scancode in question | |
324 | * or -1U in case of failure. | |
325 | * | |
326 | * This routine is used to locate given scancode in rc_map. | |
327 | * If scancode is not yet present the routine will allocate a new slot | |
328 | * for it. | |
329 | */ | |
330 | static unsigned int ir_establish_scancode(struct rc_dev *dev, | |
331 | struct rc_map *rc_map, | |
332 | unsigned int scancode, | |
333 | bool resize) | |
334 | { | |
335 | unsigned int i; | |
336 | ||
337 | /* | |
338 | * Unfortunately, some hardware-based IR decoders don't provide | |
339 | * all bits for the complete IR code. In general, they provide only | |
340 | * the command part of the IR code. Yet, as it is possible to replace | |
341 | * the provided IR with another one, it is needed to allow loading | |
342 | * IR tables from other remotes. So, we support specifying a mask to | |
343 | * indicate the valid bits of the scancodes. | |
344 | */ | |
345 | if (dev->scancode_mask) | |
346 | scancode &= dev->scancode_mask; | |
347 | ||
348 | /* First check if we already have a mapping for this ir command */ | |
349 | for (i = 0; i < rc_map->len; i++) { | |
350 | if (rc_map->scan[i].scancode == scancode) | |
351 | return i; | |
352 | ||
353 | /* Keytable is sorted from lowest to highest scancode */ | |
354 | if (rc_map->scan[i].scancode >= scancode) | |
355 | break; | |
356 | } | |
357 | ||
358 | /* No previous mapping found, we might need to grow the table */ | |
359 | if (rc_map->size == rc_map->len) { | |
360 | if (!resize || ir_resize_table(rc_map, GFP_ATOMIC)) | |
361 | return -1U; | |
362 | } | |
363 | ||
364 | /* i is the proper index to insert our new keycode */ | |
365 | if (i < rc_map->len) | |
366 | memmove(&rc_map->scan[i + 1], &rc_map->scan[i], | |
367 | (rc_map->len - i) * sizeof(struct rc_map_table)); | |
368 | rc_map->scan[i].scancode = scancode; | |
369 | rc_map->scan[i].keycode = KEY_RESERVED; | |
370 | rc_map->len++; | |
371 | ||
372 | return i; | |
373 | } | |
374 | ||
375 | /** | |
376 | * ir_setkeycode() - set a keycode in the scancode->keycode table | |
377 | * @idev: the struct input_dev device descriptor | |
378 | * @scancode: the desired scancode | |
379 | * @keycode: result | |
380 | * @return: -EINVAL if the keycode could not be inserted, otherwise zero. | |
381 | * | |
382 | * This routine is used to handle evdev EVIOCSKEY ioctl. | |
383 | */ | |
384 | static int ir_setkeycode(struct input_dev *idev, | |
385 | const struct input_keymap_entry *ke, | |
386 | unsigned int *old_keycode) | |
387 | { | |
388 | struct rc_dev *rdev = input_get_drvdata(idev); | |
389 | struct rc_map *rc_map = &rdev->rc_map; | |
390 | unsigned int index; | |
391 | unsigned int scancode; | |
392 | int retval = 0; | |
393 | unsigned long flags; | |
394 | ||
395 | spin_lock_irqsave(&rc_map->lock, flags); | |
396 | ||
397 | if (ke->flags & INPUT_KEYMAP_BY_INDEX) { | |
398 | index = ke->index; | |
399 | if (index >= rc_map->len) { | |
400 | retval = -EINVAL; | |
401 | goto out; | |
402 | } | |
403 | } else { | |
404 | retval = input_scancode_to_scalar(ke, &scancode); | |
405 | if (retval) | |
406 | goto out; | |
407 | ||
408 | index = ir_establish_scancode(rdev, rc_map, scancode, true); | |
409 | if (index >= rc_map->len) { | |
410 | retval = -ENOMEM; | |
411 | goto out; | |
412 | } | |
413 | } | |
414 | ||
415 | *old_keycode = ir_update_mapping(rdev, rc_map, index, ke->keycode); | |
416 | ||
417 | out: | |
418 | spin_unlock_irqrestore(&rc_map->lock, flags); | |
419 | return retval; | |
420 | } | |
421 | ||
422 | /** | |
423 | * ir_setkeytable() - sets several entries in the scancode->keycode table | |
424 | * @dev: the struct rc_dev device descriptor | |
425 | * @to: the struct rc_map to copy entries to | |
426 | * @from: the struct rc_map to copy entries from | |
427 | * @return: -ENOMEM if all keycodes could not be inserted, otherwise zero. | |
428 | * | |
429 | * This routine is used to handle table initialization. | |
430 | */ | |
431 | static int ir_setkeytable(struct rc_dev *dev, | |
432 | const struct rc_map *from) | |
433 | { | |
434 | struct rc_map *rc_map = &dev->rc_map; | |
435 | unsigned int i, index; | |
436 | int rc; | |
437 | ||
438 | rc = ir_create_table(rc_map, from->name, | |
439 | from->rc_proto, from->size); | |
440 | if (rc) | |
441 | return rc; | |
442 | ||
443 | for (i = 0; i < from->size; i++) { | |
444 | index = ir_establish_scancode(dev, rc_map, | |
445 | from->scan[i].scancode, false); | |
446 | if (index >= rc_map->len) { | |
447 | rc = -ENOMEM; | |
448 | break; | |
449 | } | |
450 | ||
451 | ir_update_mapping(dev, rc_map, index, | |
452 | from->scan[i].keycode); | |
453 | } | |
454 | ||
455 | if (rc) | |
456 | ir_free_table(rc_map); | |
457 | ||
458 | return rc; | |
459 | } | |
460 | ||
461 | static int rc_map_cmp(const void *key, const void *elt) | |
462 | { | |
463 | const unsigned int *scancode = key; | |
464 | const struct rc_map_table *e = elt; | |
465 | ||
466 | if (*scancode < e->scancode) | |
467 | return -1; | |
468 | else if (*scancode > e->scancode) | |
469 | return 1; | |
470 | return 0; | |
471 | } | |
472 | ||
473 | /** | |
474 | * ir_lookup_by_scancode() - locate mapping by scancode | |
475 | * @rc_map: the struct rc_map to search | |
476 | * @scancode: scancode to look for in the table | |
477 | * @return: index in the table, -1U if not found | |
478 | * | |
479 | * This routine performs binary search in RC keykeymap table for | |
480 | * given scancode. | |
481 | */ | |
482 | static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map, | |
483 | unsigned int scancode) | |
484 | { | |
485 | struct rc_map_table *res; | |
486 | ||
487 | res = bsearch(&scancode, rc_map->scan, rc_map->len, | |
488 | sizeof(struct rc_map_table), rc_map_cmp); | |
489 | if (!res) | |
490 | return -1U; | |
491 | else | |
492 | return res - rc_map->scan; | |
493 | } | |
494 | ||
495 | /** | |
496 | * ir_getkeycode() - get a keycode from the scancode->keycode table | |
497 | * @idev: the struct input_dev device descriptor | |
498 | * @scancode: the desired scancode | |
499 | * @keycode: used to return the keycode, if found, or KEY_RESERVED | |
500 | * @return: always returns zero. | |
501 | * | |
502 | * This routine is used to handle evdev EVIOCGKEY ioctl. | |
503 | */ | |
504 | static int ir_getkeycode(struct input_dev *idev, | |
505 | struct input_keymap_entry *ke) | |
506 | { | |
507 | struct rc_dev *rdev = input_get_drvdata(idev); | |
508 | struct rc_map *rc_map = &rdev->rc_map; | |
509 | struct rc_map_table *entry; | |
510 | unsigned long flags; | |
511 | unsigned int index; | |
512 | unsigned int scancode; | |
513 | int retval; | |
514 | ||
515 | spin_lock_irqsave(&rc_map->lock, flags); | |
516 | ||
517 | if (ke->flags & INPUT_KEYMAP_BY_INDEX) { | |
518 | index = ke->index; | |
519 | } else { | |
520 | retval = input_scancode_to_scalar(ke, &scancode); | |
521 | if (retval) | |
522 | goto out; | |
523 | ||
524 | index = ir_lookup_by_scancode(rc_map, scancode); | |
525 | } | |
526 | ||
527 | if (index < rc_map->len) { | |
528 | entry = &rc_map->scan[index]; | |
529 | ||
530 | ke->index = index; | |
531 | ke->keycode = entry->keycode; | |
532 | ke->len = sizeof(entry->scancode); | |
533 | memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode)); | |
534 | ||
535 | } else if (!(ke->flags & INPUT_KEYMAP_BY_INDEX)) { | |
536 | /* | |
537 | * We do not really know the valid range of scancodes | |
538 | * so let's respond with KEY_RESERVED to anything we | |
539 | * do not have mapping for [yet]. | |
540 | */ | |
541 | ke->index = index; | |
542 | ke->keycode = KEY_RESERVED; | |
543 | } else { | |
544 | retval = -EINVAL; | |
545 | goto out; | |
546 | } | |
547 | ||
548 | retval = 0; | |
549 | ||
550 | out: | |
551 | spin_unlock_irqrestore(&rc_map->lock, flags); | |
552 | return retval; | |
553 | } | |
554 | ||
555 | /** | |
556 | * rc_g_keycode_from_table() - gets the keycode that corresponds to a scancode | |
557 | * @dev: the struct rc_dev descriptor of the device | |
558 | * @scancode: the scancode to look for | |
559 | * @return: the corresponding keycode, or KEY_RESERVED | |
560 | * | |
561 | * This routine is used by drivers which need to convert a scancode to a | |
562 | * keycode. Normally it should not be used since drivers should have no | |
563 | * interest in keycodes. | |
564 | */ | |
565 | u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode) | |
566 | { | |
567 | struct rc_map *rc_map = &dev->rc_map; | |
568 | unsigned int keycode; | |
569 | unsigned int index; | |
570 | unsigned long flags; | |
571 | ||
572 | spin_lock_irqsave(&rc_map->lock, flags); | |
573 | ||
574 | index = ir_lookup_by_scancode(rc_map, scancode); | |
575 | keycode = index < rc_map->len ? | |
576 | rc_map->scan[index].keycode : KEY_RESERVED; | |
577 | ||
578 | spin_unlock_irqrestore(&rc_map->lock, flags); | |
579 | ||
580 | if (keycode != KEY_RESERVED) | |
581 | IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n", | |
582 | dev->device_name, scancode, keycode); | |
583 | ||
584 | return keycode; | |
585 | } | |
586 | EXPORT_SYMBOL_GPL(rc_g_keycode_from_table); | |
587 | ||
588 | /** | |
589 | * ir_do_keyup() - internal function to signal the release of a keypress | |
590 | * @dev: the struct rc_dev descriptor of the device | |
591 | * @sync: whether or not to call input_sync | |
592 | * | |
593 | * This function is used internally to release a keypress, it must be | |
594 | * called with keylock held. | |
595 | */ | |
596 | static void ir_do_keyup(struct rc_dev *dev, bool sync) | |
597 | { | |
598 | if (!dev->keypressed) | |
599 | return; | |
600 | ||
601 | IR_dprintk(1, "keyup key 0x%04x\n", dev->last_keycode); | |
602 | input_report_key(dev->input_dev, dev->last_keycode, 0); | |
603 | led_trigger_event(led_feedback, LED_OFF); | |
604 | if (sync) | |
605 | input_sync(dev->input_dev); | |
606 | dev->keypressed = false; | |
607 | } | |
608 | ||
609 | /** | |
610 | * rc_keyup() - signals the release of a keypress | |
611 | * @dev: the struct rc_dev descriptor of the device | |
612 | * | |
613 | * This routine is used to signal that a key has been released on the | |
614 | * remote control. | |
615 | */ | |
616 | void rc_keyup(struct rc_dev *dev) | |
617 | { | |
618 | unsigned long flags; | |
619 | ||
620 | spin_lock_irqsave(&dev->keylock, flags); | |
621 | ir_do_keyup(dev, true); | |
622 | spin_unlock_irqrestore(&dev->keylock, flags); | |
623 | } | |
624 | EXPORT_SYMBOL_GPL(rc_keyup); | |
625 | ||
626 | /** | |
627 | * ir_timer_keyup() - generates a keyup event after a timeout | |
628 | * @cookie: a pointer to the struct rc_dev for the device | |
629 | * | |
630 | * This routine will generate a keyup event some time after a keydown event | |
631 | * is generated when no further activity has been detected. | |
632 | */ | |
633 | static void ir_timer_keyup(unsigned long cookie) | |
634 | { | |
635 | struct rc_dev *dev = (struct rc_dev *)cookie; | |
636 | unsigned long flags; | |
637 | ||
638 | /* | |
639 | * ir->keyup_jiffies is used to prevent a race condition if a | |
640 | * hardware interrupt occurs at this point and the keyup timer | |
641 | * event is moved further into the future as a result. | |
642 | * | |
643 | * The timer will then be reactivated and this function called | |
644 | * again in the future. We need to exit gracefully in that case | |
645 | * to allow the input subsystem to do its auto-repeat magic or | |
646 | * a keyup event might follow immediately after the keydown. | |
647 | */ | |
648 | spin_lock_irqsave(&dev->keylock, flags); | |
649 | if (time_is_before_eq_jiffies(dev->keyup_jiffies)) | |
650 | ir_do_keyup(dev, true); | |
651 | spin_unlock_irqrestore(&dev->keylock, flags); | |
652 | } | |
653 | ||
654 | /** | |
655 | * rc_repeat() - signals that a key is still pressed | |
656 | * @dev: the struct rc_dev descriptor of the device | |
657 | * | |
658 | * This routine is used by IR decoders when a repeat message which does | |
659 | * not include the necessary bits to reproduce the scancode has been | |
660 | * received. | |
661 | */ | |
662 | void rc_repeat(struct rc_dev *dev) | |
663 | { | |
664 | unsigned long flags; | |
665 | unsigned int timeout = protocols[dev->last_protocol].repeat_period; | |
666 | ||
667 | spin_lock_irqsave(&dev->keylock, flags); | |
668 | ||
669 | if (!dev->keypressed) | |
670 | goto out; | |
671 | ||
672 | input_event(dev->input_dev, EV_MSC, MSC_SCAN, dev->last_scancode); | |
673 | input_sync(dev->input_dev); | |
674 | ||
675 | dev->keyup_jiffies = jiffies + msecs_to_jiffies(timeout); | |
676 | mod_timer(&dev->timer_keyup, dev->keyup_jiffies); | |
677 | ||
678 | out: | |
679 | spin_unlock_irqrestore(&dev->keylock, flags); | |
680 | } | |
681 | EXPORT_SYMBOL_GPL(rc_repeat); | |
682 | ||
683 | /** | |
684 | * ir_do_keydown() - internal function to process a keypress | |
685 | * @dev: the struct rc_dev descriptor of the device | |
686 | * @protocol: the protocol of the keypress | |
687 | * @scancode: the scancode of the keypress | |
688 | * @keycode: the keycode of the keypress | |
689 | * @toggle: the toggle value of the keypress | |
690 | * | |
691 | * This function is used internally to register a keypress, it must be | |
692 | * called with keylock held. | |
693 | */ | |
694 | static void ir_do_keydown(struct rc_dev *dev, enum rc_proto protocol, | |
695 | u32 scancode, u32 keycode, u8 toggle) | |
696 | { | |
697 | bool new_event = (!dev->keypressed || | |
698 | dev->last_protocol != protocol || | |
699 | dev->last_scancode != scancode || | |
700 | dev->last_toggle != toggle); | |
701 | ||
702 | if (new_event && dev->keypressed) | |
703 | ir_do_keyup(dev, false); | |
704 | ||
705 | input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode); | |
706 | ||
707 | if (new_event && keycode != KEY_RESERVED) { | |
708 | /* Register a keypress */ | |
709 | dev->keypressed = true; | |
710 | dev->last_protocol = protocol; | |
711 | dev->last_scancode = scancode; | |
712 | dev->last_toggle = toggle; | |
713 | dev->last_keycode = keycode; | |
714 | ||
715 | IR_dprintk(1, "%s: key down event, key 0x%04x, protocol 0x%04x, scancode 0x%08x\n", | |
716 | dev->device_name, keycode, protocol, scancode); | |
717 | input_report_key(dev->input_dev, keycode, 1); | |
718 | ||
719 | led_trigger_event(led_feedback, LED_FULL); | |
720 | } | |
721 | ||
722 | input_sync(dev->input_dev); | |
723 | } | |
724 | ||
725 | /** | |
726 | * rc_keydown() - generates input event for a key press | |
727 | * @dev: the struct rc_dev descriptor of the device | |
728 | * @protocol: the protocol for the keypress | |
729 | * @scancode: the scancode for the keypress | |
730 | * @toggle: the toggle value (protocol dependent, if the protocol doesn't | |
731 | * support toggle values, this should be set to zero) | |
732 | * | |
733 | * This routine is used to signal that a key has been pressed on the | |
734 | * remote control. | |
735 | */ | |
736 | void rc_keydown(struct rc_dev *dev, enum rc_proto protocol, u32 scancode, | |
737 | u8 toggle) | |
738 | { | |
739 | unsigned long flags; | |
740 | u32 keycode = rc_g_keycode_from_table(dev, scancode); | |
741 | ||
742 | spin_lock_irqsave(&dev->keylock, flags); | |
743 | ir_do_keydown(dev, protocol, scancode, keycode, toggle); | |
744 | ||
745 | if (dev->keypressed) { | |
746 | dev->keyup_jiffies = jiffies + | |
747 | msecs_to_jiffies(protocols[protocol].repeat_period); | |
748 | mod_timer(&dev->timer_keyup, dev->keyup_jiffies); | |
749 | } | |
750 | spin_unlock_irqrestore(&dev->keylock, flags); | |
751 | } | |
752 | EXPORT_SYMBOL_GPL(rc_keydown); | |
753 | ||
754 | /** | |
755 | * rc_keydown_notimeout() - generates input event for a key press without | |
756 | * an automatic keyup event at a later time | |
757 | * @dev: the struct rc_dev descriptor of the device | |
758 | * @protocol: the protocol for the keypress | |
759 | * @scancode: the scancode for the keypress | |
760 | * @toggle: the toggle value (protocol dependent, if the protocol doesn't | |
761 | * support toggle values, this should be set to zero) | |
762 | * | |
763 | * This routine is used to signal that a key has been pressed on the | |
764 | * remote control. The driver must manually call rc_keyup() at a later stage. | |
765 | */ | |
766 | void rc_keydown_notimeout(struct rc_dev *dev, enum rc_proto protocol, | |
767 | u32 scancode, u8 toggle) | |
768 | { | |
769 | unsigned long flags; | |
770 | u32 keycode = rc_g_keycode_from_table(dev, scancode); | |
771 | ||
772 | spin_lock_irqsave(&dev->keylock, flags); | |
773 | ir_do_keydown(dev, protocol, scancode, keycode, toggle); | |
774 | spin_unlock_irqrestore(&dev->keylock, flags); | |
775 | } | |
776 | EXPORT_SYMBOL_GPL(rc_keydown_notimeout); | |
777 | ||
778 | /** | |
779 | * rc_validate_filter() - checks that the scancode and mask are valid and | |
780 | * provides sensible defaults | |
781 | * @dev: the struct rc_dev descriptor of the device | |
782 | * @filter: the scancode and mask | |
783 | * @return: 0 or -EINVAL if the filter is not valid | |
784 | */ | |
785 | static int rc_validate_filter(struct rc_dev *dev, | |
786 | struct rc_scancode_filter *filter) | |
787 | { | |
788 | u32 mask, s = filter->data; | |
789 | enum rc_proto protocol = dev->wakeup_protocol; | |
790 | ||
791 | if (protocol >= ARRAY_SIZE(protocols)) | |
792 | return -EINVAL; | |
793 | ||
794 | mask = protocols[protocol].scancode_bits; | |
795 | ||
796 | switch (protocol) { | |
797 | case RC_PROTO_NECX: | |
798 | if ((((s >> 16) ^ ~(s >> 8)) & 0xff) == 0) | |
799 | return -EINVAL; | |
800 | break; | |
801 | case RC_PROTO_NEC32: | |
802 | if ((((s >> 24) ^ ~(s >> 16)) & 0xff) == 0) | |
803 | return -EINVAL; | |
804 | break; | |
805 | case RC_PROTO_RC6_MCE: | |
806 | if ((s & 0xffff0000) != 0x800f0000) | |
807 | return -EINVAL; | |
808 | break; | |
809 | case RC_PROTO_RC6_6A_32: | |
810 | if ((s & 0xffff0000) == 0x800f0000) | |
811 | return -EINVAL; | |
812 | break; | |
813 | default: | |
814 | break; | |
815 | } | |
816 | ||
817 | filter->data &= mask; | |
818 | filter->mask &= mask; | |
819 | ||
820 | /* | |
821 | * If we have to raw encode the IR for wakeup, we cannot have a mask | |
822 | */ | |
823 | if (dev->encode_wakeup && filter->mask != 0 && filter->mask != mask) | |
824 | return -EINVAL; | |
825 | ||
826 | return 0; | |
827 | } | |
828 | ||
829 | int rc_open(struct rc_dev *rdev) | |
830 | { | |
831 | int rval = 0; | |
832 | ||
833 | if (!rdev) | |
834 | return -EINVAL; | |
835 | ||
836 | mutex_lock(&rdev->lock); | |
837 | ||
838 | if (!rdev->users++ && rdev->open != NULL) | |
839 | rval = rdev->open(rdev); | |
840 | ||
841 | if (rval) | |
842 | rdev->users--; | |
843 | ||
844 | mutex_unlock(&rdev->lock); | |
845 | ||
846 | return rval; | |
847 | } | |
848 | EXPORT_SYMBOL_GPL(rc_open); | |
849 | ||
850 | static int ir_open(struct input_dev *idev) | |
851 | { | |
852 | struct rc_dev *rdev = input_get_drvdata(idev); | |
853 | ||
854 | return rc_open(rdev); | |
855 | } | |
856 | ||
857 | void rc_close(struct rc_dev *rdev) | |
858 | { | |
859 | if (rdev) { | |
860 | mutex_lock(&rdev->lock); | |
861 | ||
862 | if (!--rdev->users && rdev->close != NULL) | |
863 | rdev->close(rdev); | |
864 | ||
865 | mutex_unlock(&rdev->lock); | |
866 | } | |
867 | } | |
868 | EXPORT_SYMBOL_GPL(rc_close); | |
869 | ||
870 | static void ir_close(struct input_dev *idev) | |
871 | { | |
872 | struct rc_dev *rdev = input_get_drvdata(idev); | |
873 | rc_close(rdev); | |
874 | } | |
875 | ||
876 | /* class for /sys/class/rc */ | |
877 | static char *rc_devnode(struct device *dev, umode_t *mode) | |
878 | { | |
879 | return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev)); | |
880 | } | |
881 | ||
882 | static struct class rc_class = { | |
883 | .name = "rc", | |
884 | .devnode = rc_devnode, | |
885 | }; | |
886 | ||
887 | /* | |
888 | * These are the protocol textual descriptions that are | |
889 | * used by the sysfs protocols file. Note that the order | |
890 | * of the entries is relevant. | |
891 | */ | |
892 | static const struct { | |
893 | u64 type; | |
894 | const char *name; | |
895 | const char *module_name; | |
896 | } proto_names[] = { | |
897 | { RC_PROTO_BIT_NONE, "none", NULL }, | |
898 | { RC_PROTO_BIT_OTHER, "other", NULL }, | |
899 | { RC_PROTO_BIT_UNKNOWN, "unknown", NULL }, | |
900 | { RC_PROTO_BIT_RC5 | | |
901 | RC_PROTO_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" }, | |
902 | { RC_PROTO_BIT_NEC | | |
903 | RC_PROTO_BIT_NECX | | |
904 | RC_PROTO_BIT_NEC32, "nec", "ir-nec-decoder" }, | |
905 | { RC_PROTO_BIT_RC6_0 | | |
906 | RC_PROTO_BIT_RC6_6A_20 | | |
907 | RC_PROTO_BIT_RC6_6A_24 | | |
908 | RC_PROTO_BIT_RC6_6A_32 | | |
909 | RC_PROTO_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" }, | |
910 | { RC_PROTO_BIT_JVC, "jvc", "ir-jvc-decoder" }, | |
911 | { RC_PROTO_BIT_SONY12 | | |
912 | RC_PROTO_BIT_SONY15 | | |
913 | RC_PROTO_BIT_SONY20, "sony", "ir-sony-decoder" }, | |
914 | { RC_PROTO_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" }, | |
915 | { RC_PROTO_BIT_SANYO, "sanyo", "ir-sanyo-decoder" }, | |
916 | { RC_PROTO_BIT_SHARP, "sharp", "ir-sharp-decoder" }, | |
917 | { RC_PROTO_BIT_MCIR2_KBD | | |
918 | RC_PROTO_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" }, | |
919 | { RC_PROTO_BIT_XMP, "xmp", "ir-xmp-decoder" }, | |
920 | { RC_PROTO_BIT_CEC, "cec", NULL }, | |
921 | }; | |
922 | ||
923 | /** | |
924 | * struct rc_filter_attribute - Device attribute relating to a filter type. | |
925 | * @attr: Device attribute. | |
926 | * @type: Filter type. | |
927 | * @mask: false for filter value, true for filter mask. | |
928 | */ | |
929 | struct rc_filter_attribute { | |
930 | struct device_attribute attr; | |
931 | enum rc_filter_type type; | |
932 | bool mask; | |
933 | }; | |
934 | #define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr) | |
935 | ||
936 | #define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \ | |
937 | struct rc_filter_attribute dev_attr_##_name = { \ | |
938 | .attr = __ATTR(_name, _mode, _show, _store), \ | |
939 | .type = (_type), \ | |
940 | .mask = (_mask), \ | |
941 | } | |
942 | ||
943 | static bool lirc_is_present(void) | |
944 | { | |
945 | #if defined(CONFIG_LIRC_MODULE) | |
946 | struct module *lirc; | |
947 | ||
948 | mutex_lock(&module_mutex); | |
949 | lirc = find_module("lirc_dev"); | |
950 | mutex_unlock(&module_mutex); | |
951 | ||
952 | return lirc ? true : false; | |
953 | #elif defined(CONFIG_LIRC) | |
954 | return true; | |
955 | #else | |
956 | return false; | |
957 | #endif | |
958 | } | |
959 | ||
960 | /** | |
961 | * show_protocols() - shows the current IR protocol(s) | |
962 | * @device: the device descriptor | |
963 | * @mattr: the device attribute struct | |
964 | * @buf: a pointer to the output buffer | |
965 | * | |
966 | * This routine is a callback routine for input read the IR protocol type(s). | |
967 | * it is trigged by reading /sys/class/rc/rc?/protocols. | |
968 | * It returns the protocol names of supported protocols. | |
969 | * Enabled protocols are printed in brackets. | |
970 | * | |
971 | * dev->lock is taken to guard against races between | |
972 | * store_protocols and show_protocols. | |
973 | */ | |
974 | static ssize_t show_protocols(struct device *device, | |
975 | struct device_attribute *mattr, char *buf) | |
976 | { | |
977 | struct rc_dev *dev = to_rc_dev(device); | |
978 | u64 allowed, enabled; | |
979 | char *tmp = buf; | |
980 | int i; | |
981 | ||
982 | mutex_lock(&dev->lock); | |
983 | ||
984 | enabled = dev->enabled_protocols; | |
985 | allowed = dev->allowed_protocols; | |
986 | if (dev->raw && !allowed) | |
987 | allowed = ir_raw_get_allowed_protocols(); | |
988 | ||
989 | mutex_unlock(&dev->lock); | |
990 | ||
991 | IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n", | |
992 | __func__, (long long)allowed, (long long)enabled); | |
993 | ||
994 | for (i = 0; i < ARRAY_SIZE(proto_names); i++) { | |
995 | if (allowed & enabled & proto_names[i].type) | |
996 | tmp += sprintf(tmp, "[%s] ", proto_names[i].name); | |
997 | else if (allowed & proto_names[i].type) | |
998 | tmp += sprintf(tmp, "%s ", proto_names[i].name); | |
999 | ||
1000 | if (allowed & proto_names[i].type) | |
1001 | allowed &= ~proto_names[i].type; | |
1002 | } | |
1003 | ||
1004 | if (dev->driver_type == RC_DRIVER_IR_RAW && lirc_is_present()) | |
1005 | tmp += sprintf(tmp, "[lirc] "); | |
1006 | ||
1007 | if (tmp != buf) | |
1008 | tmp--; | |
1009 | *tmp = '\n'; | |
1010 | ||
1011 | return tmp + 1 - buf; | |
1012 | } | |
1013 | ||
1014 | /** | |
1015 | * parse_protocol_change() - parses a protocol change request | |
1016 | * @protocols: pointer to the bitmask of current protocols | |
1017 | * @buf: pointer to the buffer with a list of changes | |
1018 | * | |
1019 | * Writing "+proto" will add a protocol to the protocol mask. | |
1020 | * Writing "-proto" will remove a protocol from protocol mask. | |
1021 | * Writing "proto" will enable only "proto". | |
1022 | * Writing "none" will disable all protocols. | |
1023 | * Returns the number of changes performed or a negative error code. | |
1024 | */ | |
1025 | static int parse_protocol_change(u64 *protocols, const char *buf) | |
1026 | { | |
1027 | const char *tmp; | |
1028 | unsigned count = 0; | |
1029 | bool enable, disable; | |
1030 | u64 mask; | |
1031 | int i; | |
1032 | ||
1033 | while ((tmp = strsep((char **)&buf, " \n")) != NULL) { | |
1034 | if (!*tmp) | |
1035 | break; | |
1036 | ||
1037 | if (*tmp == '+') { | |
1038 | enable = true; | |
1039 | disable = false; | |
1040 | tmp++; | |
1041 | } else if (*tmp == '-') { | |
1042 | enable = false; | |
1043 | disable = true; | |
1044 | tmp++; | |
1045 | } else { | |
1046 | enable = false; | |
1047 | disable = false; | |
1048 | } | |
1049 | ||
1050 | for (i = 0; i < ARRAY_SIZE(proto_names); i++) { | |
1051 | if (!strcasecmp(tmp, proto_names[i].name)) { | |
1052 | mask = proto_names[i].type; | |
1053 | break; | |
1054 | } | |
1055 | } | |
1056 | ||
1057 | if (i == ARRAY_SIZE(proto_names)) { | |
1058 | if (!strcasecmp(tmp, "lirc")) | |
1059 | mask = 0; | |
1060 | else { | |
1061 | IR_dprintk(1, "Unknown protocol: '%s'\n", tmp); | |
1062 | return -EINVAL; | |
1063 | } | |
1064 | } | |
1065 | ||
1066 | count++; | |
1067 | ||
1068 | if (enable) | |
1069 | *protocols |= mask; | |
1070 | else if (disable) | |
1071 | *protocols &= ~mask; | |
1072 | else | |
1073 | *protocols = mask; | |
1074 | } | |
1075 | ||
1076 | if (!count) { | |
1077 | IR_dprintk(1, "Protocol not specified\n"); | |
1078 | return -EINVAL; | |
1079 | } | |
1080 | ||
1081 | return count; | |
1082 | } | |
1083 | ||
1084 | static void ir_raw_load_modules(u64 *protocols) | |
1085 | { | |
1086 | u64 available; | |
1087 | int i, ret; | |
1088 | ||
1089 | for (i = 0; i < ARRAY_SIZE(proto_names); i++) { | |
1090 | if (proto_names[i].type == RC_PROTO_BIT_NONE || | |
1091 | proto_names[i].type & (RC_PROTO_BIT_OTHER | | |
1092 | RC_PROTO_BIT_UNKNOWN)) | |
1093 | continue; | |
1094 | ||
1095 | available = ir_raw_get_allowed_protocols(); | |
1096 | if (!(*protocols & proto_names[i].type & ~available)) | |
1097 | continue; | |
1098 | ||
1099 | if (!proto_names[i].module_name) { | |
1100 | pr_err("Can't enable IR protocol %s\n", | |
1101 | proto_names[i].name); | |
1102 | *protocols &= ~proto_names[i].type; | |
1103 | continue; | |
1104 | } | |
1105 | ||
1106 | ret = request_module("%s", proto_names[i].module_name); | |
1107 | if (ret < 0) { | |
1108 | pr_err("Couldn't load IR protocol module %s\n", | |
1109 | proto_names[i].module_name); | |
1110 | *protocols &= ~proto_names[i].type; | |
1111 | continue; | |
1112 | } | |
1113 | msleep(20); | |
1114 | available = ir_raw_get_allowed_protocols(); | |
1115 | if (!(*protocols & proto_names[i].type & ~available)) | |
1116 | continue; | |
1117 | ||
1118 | pr_err("Loaded IR protocol module %s, but protocol %s still not available\n", | |
1119 | proto_names[i].module_name, | |
1120 | proto_names[i].name); | |
1121 | *protocols &= ~proto_names[i].type; | |
1122 | } | |
1123 | } | |
1124 | ||
1125 | /** | |
1126 | * store_protocols() - changes the current/wakeup IR protocol(s) | |
1127 | * @device: the device descriptor | |
1128 | * @mattr: the device attribute struct | |
1129 | * @buf: a pointer to the input buffer | |
1130 | * @len: length of the input buffer | |
1131 | * | |
1132 | * This routine is for changing the IR protocol type. | |
1133 | * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols. | |
1134 | * See parse_protocol_change() for the valid commands. | |
1135 | * Returns @len on success or a negative error code. | |
1136 | * | |
1137 | * dev->lock is taken to guard against races between | |
1138 | * store_protocols and show_protocols. | |
1139 | */ | |
1140 | static ssize_t store_protocols(struct device *device, | |
1141 | struct device_attribute *mattr, | |
1142 | const char *buf, size_t len) | |
1143 | { | |
1144 | struct rc_dev *dev = to_rc_dev(device); | |
1145 | u64 *current_protocols; | |
1146 | struct rc_scancode_filter *filter; | |
1147 | u64 old_protocols, new_protocols; | |
1148 | ssize_t rc; | |
1149 | ||
1150 | IR_dprintk(1, "Normal protocol change requested\n"); | |
1151 | current_protocols = &dev->enabled_protocols; | |
1152 | filter = &dev->scancode_filter; | |
1153 | ||
1154 | if (!dev->change_protocol) { | |
1155 | IR_dprintk(1, "Protocol switching not supported\n"); | |
1156 | return -EINVAL; | |
1157 | } | |
1158 | ||
1159 | mutex_lock(&dev->lock); | |
1160 | ||
1161 | old_protocols = *current_protocols; | |
1162 | new_protocols = old_protocols; | |
1163 | rc = parse_protocol_change(&new_protocols, buf); | |
1164 | if (rc < 0) | |
1165 | goto out; | |
1166 | ||
1167 | rc = dev->change_protocol(dev, &new_protocols); | |
1168 | if (rc < 0) { | |
1169 | IR_dprintk(1, "Error setting protocols to 0x%llx\n", | |
1170 | (long long)new_protocols); | |
1171 | goto out; | |
1172 | } | |
1173 | ||
1174 | if (dev->driver_type == RC_DRIVER_IR_RAW) | |
1175 | ir_raw_load_modules(&new_protocols); | |
1176 | ||
1177 | if (new_protocols != old_protocols) { | |
1178 | *current_protocols = new_protocols; | |
1179 | IR_dprintk(1, "Protocols changed to 0x%llx\n", | |
1180 | (long long)new_protocols); | |
1181 | } | |
1182 | ||
1183 | /* | |
1184 | * If a protocol change was attempted the filter may need updating, even | |
1185 | * if the actual protocol mask hasn't changed (since the driver may have | |
1186 | * cleared the filter). | |
1187 | * Try setting the same filter with the new protocol (if any). | |
1188 | * Fall back to clearing the filter. | |
1189 | */ | |
1190 | if (dev->s_filter && filter->mask) { | |
1191 | if (new_protocols) | |
1192 | rc = dev->s_filter(dev, filter); | |
1193 | else | |
1194 | rc = -1; | |
1195 | ||
1196 | if (rc < 0) { | |
1197 | filter->data = 0; | |
1198 | filter->mask = 0; | |
1199 | dev->s_filter(dev, filter); | |
1200 | } | |
1201 | } | |
1202 | ||
1203 | rc = len; | |
1204 | ||
1205 | out: | |
1206 | mutex_unlock(&dev->lock); | |
1207 | return rc; | |
1208 | } | |
1209 | ||
1210 | /** | |
1211 | * show_filter() - shows the current scancode filter value or mask | |
1212 | * @device: the device descriptor | |
1213 | * @attr: the device attribute struct | |
1214 | * @buf: a pointer to the output buffer | |
1215 | * | |
1216 | * This routine is a callback routine to read a scancode filter value or mask. | |
1217 | * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask]. | |
1218 | * It prints the current scancode filter value or mask of the appropriate filter | |
1219 | * type in hexadecimal into @buf and returns the size of the buffer. | |
1220 | * | |
1221 | * Bits of the filter value corresponding to set bits in the filter mask are | |
1222 | * compared against input scancodes and non-matching scancodes are discarded. | |
1223 | * | |
1224 | * dev->lock is taken to guard against races between | |
1225 | * store_filter and show_filter. | |
1226 | */ | |
1227 | static ssize_t show_filter(struct device *device, | |
1228 | struct device_attribute *attr, | |
1229 | char *buf) | |
1230 | { | |
1231 | struct rc_dev *dev = to_rc_dev(device); | |
1232 | struct rc_filter_attribute *fattr = to_rc_filter_attr(attr); | |
1233 | struct rc_scancode_filter *filter; | |
1234 | u32 val; | |
1235 | ||
1236 | mutex_lock(&dev->lock); | |
1237 | ||
1238 | if (fattr->type == RC_FILTER_NORMAL) | |
1239 | filter = &dev->scancode_filter; | |
1240 | else | |
1241 | filter = &dev->scancode_wakeup_filter; | |
1242 | ||
1243 | if (fattr->mask) | |
1244 | val = filter->mask; | |
1245 | else | |
1246 | val = filter->data; | |
1247 | mutex_unlock(&dev->lock); | |
1248 | ||
1249 | return sprintf(buf, "%#x\n", val); | |
1250 | } | |
1251 | ||
1252 | /** | |
1253 | * store_filter() - changes the scancode filter value | |
1254 | * @device: the device descriptor | |
1255 | * @attr: the device attribute struct | |
1256 | * @buf: a pointer to the input buffer | |
1257 | * @len: length of the input buffer | |
1258 | * | |
1259 | * This routine is for changing a scancode filter value or mask. | |
1260 | * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask]. | |
1261 | * Returns -EINVAL if an invalid filter value for the current protocol was | |
1262 | * specified or if scancode filtering is not supported by the driver, otherwise | |
1263 | * returns @len. | |
1264 | * | |
1265 | * Bits of the filter value corresponding to set bits in the filter mask are | |
1266 | * compared against input scancodes and non-matching scancodes are discarded. | |
1267 | * | |
1268 | * dev->lock is taken to guard against races between | |
1269 | * store_filter and show_filter. | |
1270 | */ | |
1271 | static ssize_t store_filter(struct device *device, | |
1272 | struct device_attribute *attr, | |
1273 | const char *buf, size_t len) | |
1274 | { | |
1275 | struct rc_dev *dev = to_rc_dev(device); | |
1276 | struct rc_filter_attribute *fattr = to_rc_filter_attr(attr); | |
1277 | struct rc_scancode_filter new_filter, *filter; | |
1278 | int ret; | |
1279 | unsigned long val; | |
1280 | int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter); | |
1281 | ||
1282 | ret = kstrtoul(buf, 0, &val); | |
1283 | if (ret < 0) | |
1284 | return ret; | |
1285 | ||
1286 | if (fattr->type == RC_FILTER_NORMAL) { | |
1287 | set_filter = dev->s_filter; | |
1288 | filter = &dev->scancode_filter; | |
1289 | } else { | |
1290 | set_filter = dev->s_wakeup_filter; | |
1291 | filter = &dev->scancode_wakeup_filter; | |
1292 | } | |
1293 | ||
1294 | if (!set_filter) | |
1295 | return -EINVAL; | |
1296 | ||
1297 | mutex_lock(&dev->lock); | |
1298 | ||
1299 | new_filter = *filter; | |
1300 | if (fattr->mask) | |
1301 | new_filter.mask = val; | |
1302 | else | |
1303 | new_filter.data = val; | |
1304 | ||
1305 | if (fattr->type == RC_FILTER_WAKEUP) { | |
1306 | /* | |
1307 | * Refuse to set a filter unless a protocol is enabled | |
1308 | * and the filter is valid for that protocol | |
1309 | */ | |
1310 | if (dev->wakeup_protocol != RC_PROTO_UNKNOWN) | |
1311 | ret = rc_validate_filter(dev, &new_filter); | |
1312 | else | |
1313 | ret = -EINVAL; | |
1314 | ||
1315 | if (ret != 0) | |
1316 | goto unlock; | |
1317 | } | |
1318 | ||
1319 | if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols && | |
1320 | val) { | |
1321 | /* refuse to set a filter unless a protocol is enabled */ | |
1322 | ret = -EINVAL; | |
1323 | goto unlock; | |
1324 | } | |
1325 | ||
1326 | ret = set_filter(dev, &new_filter); | |
1327 | if (ret < 0) | |
1328 | goto unlock; | |
1329 | ||
1330 | *filter = new_filter; | |
1331 | ||
1332 | unlock: | |
1333 | mutex_unlock(&dev->lock); | |
1334 | return (ret < 0) ? ret : len; | |
1335 | } | |
1336 | ||
1337 | /** | |
1338 | * show_wakeup_protocols() - shows the wakeup IR protocol | |
1339 | * @device: the device descriptor | |
1340 | * @mattr: the device attribute struct | |
1341 | * @buf: a pointer to the output buffer | |
1342 | * | |
1343 | * This routine is a callback routine for input read the IR protocol type(s). | |
1344 | * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols. | |
1345 | * It returns the protocol names of supported protocols. | |
1346 | * The enabled protocols are printed in brackets. | |
1347 | * | |
1348 | * dev->lock is taken to guard against races between | |
1349 | * store_wakeup_protocols and show_wakeup_protocols. | |
1350 | */ | |
1351 | static ssize_t show_wakeup_protocols(struct device *device, | |
1352 | struct device_attribute *mattr, | |
1353 | char *buf) | |
1354 | { | |
1355 | struct rc_dev *dev = to_rc_dev(device); | |
1356 | u64 allowed; | |
1357 | enum rc_proto enabled; | |
1358 | char *tmp = buf; | |
1359 | int i; | |
1360 | ||
1361 | mutex_lock(&dev->lock); | |
1362 | ||
1363 | allowed = dev->allowed_wakeup_protocols; | |
1364 | enabled = dev->wakeup_protocol; | |
1365 | ||
1366 | mutex_unlock(&dev->lock); | |
1367 | ||
1368 | IR_dprintk(1, "%s: allowed - 0x%llx, enabled - %d\n", | |
1369 | __func__, (long long)allowed, enabled); | |
1370 | ||
1371 | for (i = 0; i < ARRAY_SIZE(protocols); i++) { | |
1372 | if (allowed & (1ULL << i)) { | |
1373 | if (i == enabled) | |
1374 | tmp += sprintf(tmp, "[%s] ", protocols[i].name); | |
1375 | else | |
1376 | tmp += sprintf(tmp, "%s ", protocols[i].name); | |
1377 | } | |
1378 | } | |
1379 | ||
1380 | if (tmp != buf) | |
1381 | tmp--; | |
1382 | *tmp = '\n'; | |
1383 | ||
1384 | return tmp + 1 - buf; | |
1385 | } | |
1386 | ||
1387 | /** | |
1388 | * store_wakeup_protocols() - changes the wakeup IR protocol(s) | |
1389 | * @device: the device descriptor | |
1390 | * @mattr: the device attribute struct | |
1391 | * @buf: a pointer to the input buffer | |
1392 | * @len: length of the input buffer | |
1393 | * | |
1394 | * This routine is for changing the IR protocol type. | |
1395 | * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols. | |
1396 | * Returns @len on success or a negative error code. | |
1397 | * | |
1398 | * dev->lock is taken to guard against races between | |
1399 | * store_wakeup_protocols and show_wakeup_protocols. | |
1400 | */ | |
1401 | static ssize_t store_wakeup_protocols(struct device *device, | |
1402 | struct device_attribute *mattr, | |
1403 | const char *buf, size_t len) | |
1404 | { | |
1405 | struct rc_dev *dev = to_rc_dev(device); | |
1406 | enum rc_proto protocol; | |
1407 | ssize_t rc; | |
1408 | u64 allowed; | |
1409 | int i; | |
1410 | ||
1411 | mutex_lock(&dev->lock); | |
1412 | ||
1413 | allowed = dev->allowed_wakeup_protocols; | |
1414 | ||
1415 | if (sysfs_streq(buf, "none")) { | |
1416 | protocol = RC_PROTO_UNKNOWN; | |
1417 | } else { | |
1418 | for (i = 0; i < ARRAY_SIZE(protocols); i++) { | |
1419 | if ((allowed & (1ULL << i)) && | |
1420 | sysfs_streq(buf, protocols[i].name)) { | |
1421 | protocol = i; | |
1422 | break; | |
1423 | } | |
1424 | } | |
1425 | ||
1426 | if (i == ARRAY_SIZE(protocols)) { | |
1427 | rc = -EINVAL; | |
1428 | goto out; | |
1429 | } | |
1430 | ||
1431 | if (dev->encode_wakeup) { | |
1432 | u64 mask = 1ULL << protocol; | |
1433 | ||
1434 | ir_raw_load_modules(&mask); | |
1435 | if (!mask) { | |
1436 | rc = -EINVAL; | |
1437 | goto out; | |
1438 | } | |
1439 | } | |
1440 | } | |
1441 | ||
1442 | if (dev->wakeup_protocol != protocol) { | |
1443 | dev->wakeup_protocol = protocol; | |
1444 | IR_dprintk(1, "Wakeup protocol changed to %d\n", protocol); | |
1445 | ||
1446 | if (protocol == RC_PROTO_RC6_MCE) | |
1447 | dev->scancode_wakeup_filter.data = 0x800f0000; | |
1448 | else | |
1449 | dev->scancode_wakeup_filter.data = 0; | |
1450 | dev->scancode_wakeup_filter.mask = 0; | |
1451 | ||
1452 | rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter); | |
1453 | if (rc == 0) | |
1454 | rc = len; | |
1455 | } else { | |
1456 | rc = len; | |
1457 | } | |
1458 | ||
1459 | out: | |
1460 | mutex_unlock(&dev->lock); | |
1461 | return rc; | |
1462 | } | |
1463 | ||
1464 | static void rc_dev_release(struct device *device) | |
1465 | { | |
1466 | struct rc_dev *dev = to_rc_dev(device); | |
1467 | ||
1468 | kfree(dev); | |
1469 | } | |
1470 | ||
1471 | #define ADD_HOTPLUG_VAR(fmt, val...) \ | |
1472 | do { \ | |
1473 | int err = add_uevent_var(env, fmt, val); \ | |
1474 | if (err) \ | |
1475 | return err; \ | |
1476 | } while (0) | |
1477 | ||
1478 | static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env) | |
1479 | { | |
1480 | struct rc_dev *dev = to_rc_dev(device); | |
1481 | ||
1482 | if (dev->rc_map.name) | |
1483 | ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name); | |
1484 | if (dev->driver_name) | |
1485 | ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name); | |
1486 | if (dev->device_name) | |
1487 | ADD_HOTPLUG_VAR("DEV_NAME=%s", dev->device_name); | |
1488 | ||
1489 | return 0; | |
1490 | } | |
1491 | ||
1492 | /* | |
1493 | * Static device attribute struct with the sysfs attributes for IR's | |
1494 | */ | |
1495 | static struct device_attribute dev_attr_ro_protocols = | |
1496 | __ATTR(protocols, 0444, show_protocols, NULL); | |
1497 | static struct device_attribute dev_attr_rw_protocols = | |
1498 | __ATTR(protocols, 0644, show_protocols, store_protocols); | |
1499 | static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols, | |
1500 | store_wakeup_protocols); | |
1501 | static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR, | |
1502 | show_filter, store_filter, RC_FILTER_NORMAL, false); | |
1503 | static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR, | |
1504 | show_filter, store_filter, RC_FILTER_NORMAL, true); | |
1505 | static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR, | |
1506 | show_filter, store_filter, RC_FILTER_WAKEUP, false); | |
1507 | static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR, | |
1508 | show_filter, store_filter, RC_FILTER_WAKEUP, true); | |
1509 | ||
1510 | static struct attribute *rc_dev_rw_protocol_attrs[] = { | |
1511 | &dev_attr_rw_protocols.attr, | |
1512 | NULL, | |
1513 | }; | |
1514 | ||
1515 | static const struct attribute_group rc_dev_rw_protocol_attr_grp = { | |
1516 | .attrs = rc_dev_rw_protocol_attrs, | |
1517 | }; | |
1518 | ||
1519 | static struct attribute *rc_dev_ro_protocol_attrs[] = { | |
1520 | &dev_attr_ro_protocols.attr, | |
1521 | NULL, | |
1522 | }; | |
1523 | ||
1524 | static const struct attribute_group rc_dev_ro_protocol_attr_grp = { | |
1525 | .attrs = rc_dev_ro_protocol_attrs, | |
1526 | }; | |
1527 | ||
1528 | static struct attribute *rc_dev_filter_attrs[] = { | |
1529 | &dev_attr_filter.attr.attr, | |
1530 | &dev_attr_filter_mask.attr.attr, | |
1531 | NULL, | |
1532 | }; | |
1533 | ||
1534 | static const struct attribute_group rc_dev_filter_attr_grp = { | |
1535 | .attrs = rc_dev_filter_attrs, | |
1536 | }; | |
1537 | ||
1538 | static struct attribute *rc_dev_wakeup_filter_attrs[] = { | |
1539 | &dev_attr_wakeup_filter.attr.attr, | |
1540 | &dev_attr_wakeup_filter_mask.attr.attr, | |
1541 | &dev_attr_wakeup_protocols.attr, | |
1542 | NULL, | |
1543 | }; | |
1544 | ||
1545 | static const struct attribute_group rc_dev_wakeup_filter_attr_grp = { | |
1546 | .attrs = rc_dev_wakeup_filter_attrs, | |
1547 | }; | |
1548 | ||
1549 | static const struct device_type rc_dev_type = { | |
1550 | .release = rc_dev_release, | |
1551 | .uevent = rc_dev_uevent, | |
1552 | }; | |
1553 | ||
1554 | struct rc_dev *rc_allocate_device(enum rc_driver_type type) | |
1555 | { | |
1556 | struct rc_dev *dev; | |
1557 | ||
1558 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | |
1559 | if (!dev) | |
1560 | return NULL; | |
1561 | ||
1562 | if (type != RC_DRIVER_IR_RAW_TX) { | |
1563 | dev->input_dev = input_allocate_device(); | |
1564 | if (!dev->input_dev) { | |
1565 | kfree(dev); | |
1566 | return NULL; | |
1567 | } | |
1568 | ||
1569 | dev->input_dev->getkeycode = ir_getkeycode; | |
1570 | dev->input_dev->setkeycode = ir_setkeycode; | |
1571 | input_set_drvdata(dev->input_dev, dev); | |
1572 | ||
1573 | setup_timer(&dev->timer_keyup, ir_timer_keyup, | |
1574 | (unsigned long)dev); | |
1575 | ||
1576 | spin_lock_init(&dev->rc_map.lock); | |
1577 | spin_lock_init(&dev->keylock); | |
1578 | } | |
1579 | mutex_init(&dev->lock); | |
1580 | ||
1581 | dev->dev.type = &rc_dev_type; | |
1582 | dev->dev.class = &rc_class; | |
1583 | device_initialize(&dev->dev); | |
1584 | ||
1585 | dev->driver_type = type; | |
1586 | ||
1587 | __module_get(THIS_MODULE); | |
1588 | return dev; | |
1589 | } | |
1590 | EXPORT_SYMBOL_GPL(rc_allocate_device); | |
1591 | ||
1592 | void rc_free_device(struct rc_dev *dev) | |
1593 | { | |
1594 | if (!dev) | |
1595 | return; | |
1596 | ||
1597 | input_free_device(dev->input_dev); | |
1598 | ||
1599 | put_device(&dev->dev); | |
1600 | ||
1601 | /* kfree(dev) will be called by the callback function | |
1602 | rc_dev_release() */ | |
1603 | ||
1604 | module_put(THIS_MODULE); | |
1605 | } | |
1606 | EXPORT_SYMBOL_GPL(rc_free_device); | |
1607 | ||
1608 | static void devm_rc_alloc_release(struct device *dev, void *res) | |
1609 | { | |
1610 | rc_free_device(*(struct rc_dev **)res); | |
1611 | } | |
1612 | ||
1613 | struct rc_dev *devm_rc_allocate_device(struct device *dev, | |
1614 | enum rc_driver_type type) | |
1615 | { | |
1616 | struct rc_dev **dr, *rc; | |
1617 | ||
1618 | dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL); | |
1619 | if (!dr) | |
1620 | return NULL; | |
1621 | ||
1622 | rc = rc_allocate_device(type); | |
1623 | if (!rc) { | |
1624 | devres_free(dr); | |
1625 | return NULL; | |
1626 | } | |
1627 | ||
1628 | rc->dev.parent = dev; | |
1629 | rc->managed_alloc = true; | |
1630 | *dr = rc; | |
1631 | devres_add(dev, dr); | |
1632 | ||
1633 | return rc; | |
1634 | } | |
1635 | EXPORT_SYMBOL_GPL(devm_rc_allocate_device); | |
1636 | ||
1637 | static int rc_prepare_rx_device(struct rc_dev *dev) | |
1638 | { | |
1639 | int rc; | |
1640 | struct rc_map *rc_map; | |
1641 | u64 rc_proto; | |
1642 | ||
1643 | if (!dev->map_name) | |
1644 | return -EINVAL; | |
1645 | ||
1646 | rc_map = rc_map_get(dev->map_name); | |
1647 | if (!rc_map) | |
1648 | rc_map = rc_map_get(RC_MAP_EMPTY); | |
1649 | if (!rc_map || !rc_map->scan || rc_map->size == 0) | |
1650 | return -EINVAL; | |
1651 | ||
1652 | rc = ir_setkeytable(dev, rc_map); | |
1653 | if (rc) | |
1654 | return rc; | |
1655 | ||
1656 | rc_proto = BIT_ULL(rc_map->rc_proto); | |
1657 | ||
1658 | if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol) | |
1659 | dev->enabled_protocols = dev->allowed_protocols; | |
1660 | ||
1661 | if (dev->change_protocol) { | |
1662 | rc = dev->change_protocol(dev, &rc_proto); | |
1663 | if (rc < 0) | |
1664 | goto out_table; | |
1665 | dev->enabled_protocols = rc_proto; | |
1666 | } | |
1667 | ||
1668 | if (dev->driver_type == RC_DRIVER_IR_RAW) | |
1669 | ir_raw_load_modules(&rc_proto); | |
1670 | ||
1671 | set_bit(EV_KEY, dev->input_dev->evbit); | |
1672 | set_bit(EV_REP, dev->input_dev->evbit); | |
1673 | set_bit(EV_MSC, dev->input_dev->evbit); | |
1674 | set_bit(MSC_SCAN, dev->input_dev->mscbit); | |
1675 | if (dev->open) | |
1676 | dev->input_dev->open = ir_open; | |
1677 | if (dev->close) | |
1678 | dev->input_dev->close = ir_close; | |
1679 | ||
1680 | dev->input_dev->dev.parent = &dev->dev; | |
1681 | memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id)); | |
1682 | dev->input_dev->phys = dev->input_phys; | |
1683 | dev->input_dev->name = dev->device_name; | |
1684 | ||
1685 | return 0; | |
1686 | ||
1687 | out_table: | |
1688 | ir_free_table(&dev->rc_map); | |
1689 | ||
1690 | return rc; | |
1691 | } | |
1692 | ||
1693 | static int rc_setup_rx_device(struct rc_dev *dev) | |
1694 | { | |
1695 | int rc; | |
1696 | ||
1697 | /* rc_open will be called here */ | |
1698 | rc = input_register_device(dev->input_dev); | |
1699 | if (rc) | |
1700 | return rc; | |
1701 | ||
1702 | /* | |
1703 | * Default delay of 250ms is too short for some protocols, especially | |
1704 | * since the timeout is currently set to 250ms. Increase it to 500ms, | |
1705 | * to avoid wrong repetition of the keycodes. Note that this must be | |
1706 | * set after the call to input_register_device(). | |
1707 | */ | |
1708 | dev->input_dev->rep[REP_DELAY] = 500; | |
1709 | ||
1710 | /* | |
1711 | * As a repeat event on protocols like RC-5 and NEC take as long as | |
1712 | * 110/114ms, using 33ms as a repeat period is not the right thing | |
1713 | * to do. | |
1714 | */ | |
1715 | dev->input_dev->rep[REP_PERIOD] = 125; | |
1716 | ||
1717 | return 0; | |
1718 | } | |
1719 | ||
1720 | static void rc_free_rx_device(struct rc_dev *dev) | |
1721 | { | |
1722 | if (!dev) | |
1723 | return; | |
1724 | ||
1725 | if (dev->input_dev) { | |
1726 | input_unregister_device(dev->input_dev); | |
1727 | dev->input_dev = NULL; | |
1728 | } | |
1729 | ||
1730 | ir_free_table(&dev->rc_map); | |
1731 | } | |
1732 | ||
1733 | int rc_register_device(struct rc_dev *dev) | |
1734 | { | |
1735 | const char *path; | |
1736 | int attr = 0; | |
1737 | int minor; | |
1738 | int rc; | |
1739 | ||
1740 | if (!dev) | |
1741 | return -EINVAL; | |
1742 | ||
1743 | minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL); | |
1744 | if (minor < 0) | |
1745 | return minor; | |
1746 | ||
1747 | dev->minor = minor; | |
1748 | dev_set_name(&dev->dev, "rc%u", dev->minor); | |
1749 | dev_set_drvdata(&dev->dev, dev); | |
1750 | ||
1751 | dev->dev.groups = dev->sysfs_groups; | |
1752 | if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol) | |
1753 | dev->sysfs_groups[attr++] = &rc_dev_ro_protocol_attr_grp; | |
1754 | else if (dev->driver_type != RC_DRIVER_IR_RAW_TX) | |
1755 | dev->sysfs_groups[attr++] = &rc_dev_rw_protocol_attr_grp; | |
1756 | if (dev->s_filter) | |
1757 | dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp; | |
1758 | if (dev->s_wakeup_filter) | |
1759 | dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp; | |
1760 | dev->sysfs_groups[attr++] = NULL; | |
1761 | ||
1762 | if (dev->driver_type == RC_DRIVER_IR_RAW || | |
1763 | dev->driver_type == RC_DRIVER_IR_RAW_TX) { | |
1764 | rc = ir_raw_event_prepare(dev); | |
1765 | if (rc < 0) | |
1766 | goto out_minor; | |
1767 | } | |
1768 | ||
1769 | if (dev->driver_type != RC_DRIVER_IR_RAW_TX) { | |
1770 | rc = rc_prepare_rx_device(dev); | |
1771 | if (rc) | |
1772 | goto out_raw; | |
1773 | } | |
1774 | ||
1775 | rc = device_add(&dev->dev); | |
1776 | if (rc) | |
1777 | goto out_rx_free; | |
1778 | ||
1779 | path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL); | |
1780 | dev_info(&dev->dev, "%s as %s\n", | |
1781 | dev->device_name ?: "Unspecified device", path ?: "N/A"); | |
1782 | kfree(path); | |
1783 | ||
1784 | if (dev->driver_type != RC_DRIVER_IR_RAW_TX) { | |
1785 | rc = rc_setup_rx_device(dev); | |
1786 | if (rc) | |
1787 | goto out_dev; | |
1788 | } | |
1789 | ||
1790 | if (dev->driver_type == RC_DRIVER_IR_RAW || | |
1791 | dev->driver_type == RC_DRIVER_IR_RAW_TX) { | |
1792 | rc = ir_raw_event_register(dev); | |
1793 | if (rc < 0) | |
1794 | goto out_rx; | |
1795 | } | |
1796 | ||
1797 | IR_dprintk(1, "Registered rc%u (driver: %s)\n", | |
1798 | dev->minor, | |
1799 | dev->driver_name ? dev->driver_name : "unknown"); | |
1800 | ||
1801 | return 0; | |
1802 | ||
1803 | out_rx: | |
1804 | rc_free_rx_device(dev); | |
1805 | out_dev: | |
1806 | device_del(&dev->dev); | |
1807 | out_rx_free: | |
1808 | ir_free_table(&dev->rc_map); | |
1809 | out_raw: | |
1810 | ir_raw_event_free(dev); | |
1811 | out_minor: | |
1812 | ida_simple_remove(&rc_ida, minor); | |
1813 | return rc; | |
1814 | } | |
1815 | EXPORT_SYMBOL_GPL(rc_register_device); | |
1816 | ||
1817 | static void devm_rc_release(struct device *dev, void *res) | |
1818 | { | |
1819 | rc_unregister_device(*(struct rc_dev **)res); | |
1820 | } | |
1821 | ||
1822 | int devm_rc_register_device(struct device *parent, struct rc_dev *dev) | |
1823 | { | |
1824 | struct rc_dev **dr; | |
1825 | int ret; | |
1826 | ||
1827 | dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL); | |
1828 | if (!dr) | |
1829 | return -ENOMEM; | |
1830 | ||
1831 | ret = rc_register_device(dev); | |
1832 | if (ret) { | |
1833 | devres_free(dr); | |
1834 | return ret; | |
1835 | } | |
1836 | ||
1837 | *dr = dev; | |
1838 | devres_add(parent, dr); | |
1839 | ||
1840 | return 0; | |
1841 | } | |
1842 | EXPORT_SYMBOL_GPL(devm_rc_register_device); | |
1843 | ||
1844 | void rc_unregister_device(struct rc_dev *dev) | |
1845 | { | |
1846 | if (!dev) | |
1847 | return; | |
1848 | ||
1849 | del_timer_sync(&dev->timer_keyup); | |
1850 | ||
1851 | if (dev->driver_type == RC_DRIVER_IR_RAW) | |
1852 | ir_raw_event_unregister(dev); | |
1853 | ||
1854 | rc_free_rx_device(dev); | |
1855 | ||
1856 | device_del(&dev->dev); | |
1857 | ||
1858 | ida_simple_remove(&rc_ida, dev->minor); | |
1859 | ||
1860 | if (!dev->managed_alloc) | |
1861 | rc_free_device(dev); | |
1862 | } | |
1863 | ||
1864 | EXPORT_SYMBOL_GPL(rc_unregister_device); | |
1865 | ||
1866 | /* | |
1867 | * Init/exit code for the module. Basically, creates/removes /sys/class/rc | |
1868 | */ | |
1869 | ||
1870 | static int __init rc_core_init(void) | |
1871 | { | |
1872 | int rc = class_register(&rc_class); | |
1873 | if (rc) { | |
1874 | pr_err("rc_core: unable to register rc class\n"); | |
1875 | return rc; | |
1876 | } | |
1877 | ||
1878 | led_trigger_register_simple("rc-feedback", &led_feedback); | |
1879 | rc_map_register(&empty_map); | |
1880 | ||
1881 | return 0; | |
1882 | } | |
1883 | ||
1884 | static void __exit rc_core_exit(void) | |
1885 | { | |
1886 | class_unregister(&rc_class); | |
1887 | led_trigger_unregister_simple(led_feedback); | |
1888 | rc_map_unregister(&empty_map); | |
1889 | } | |
1890 | ||
1891 | subsys_initcall(rc_core_init); | |
1892 | module_exit(rc_core_exit); | |
1893 | ||
1894 | int rc_core_debug; /* ir_debug level (0,1,2) */ | |
1895 | EXPORT_SYMBOL_GPL(rc_core_debug); | |
1896 | module_param_named(debug, rc_core_debug, int, 0644); | |
1897 | ||
1898 | MODULE_AUTHOR("Mauro Carvalho Chehab"); | |
1899 | MODULE_LICENSE("GPL"); |