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