1 /* rc-main.c - Remote Controller core module
3 * Copyright (C) 2009-2010 by Mauro Carvalho Chehab
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.
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.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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"
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
36 unsigned int repeat_period;
37 unsigned int scancode_bits;
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 },
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;
88 /* Used to keep track of rc devices */
89 static DEFINE_IDA(rc_ida);
91 static struct rc_map_list *seek_rc_map(const char *name)
93 struct rc_map_list *map = NULL;
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);
102 spin_unlock(&rc_map_lock);
107 struct rc_map *rc_map_get(const char *name)
110 struct rc_map_list *map;
112 map = seek_rc_map(name);
113 #ifdef CONFIG_MODULES
115 int rc = request_module("%s", name);
117 pr_err("Couldn't load IR keymap %s\n", name);
120 msleep(20); /* Give some time for IR to register */
122 map = seek_rc_map(name);
126 pr_err("IR keymap %s not found\n", name);
130 printk(KERN_INFO "Registered IR keymap %s\n", map->map.name);
134 EXPORT_SYMBOL_GPL(rc_map_get);
136 int rc_map_register(struct rc_map_list *map)
138 spin_lock(&rc_map_lock);
139 list_add_tail(&map->list, &rc_map_list);
140 spin_unlock(&rc_map_lock);
143 EXPORT_SYMBOL_GPL(rc_map_register);
145 void rc_map_unregister(struct rc_map_list *map)
147 spin_lock(&rc_map_lock);
148 list_del(&map->list);
149 spin_unlock(&rc_map_lock);
151 EXPORT_SYMBOL_GPL(rc_map_unregister);
154 static struct rc_map_table empty[] = {
155 { 0x2a, KEY_COFFEE },
158 static struct rc_map_list empty_map = {
161 .size = ARRAY_SIZE(empty),
162 .rc_proto = RC_PROTO_UNKNOWN, /* Legacy IR type */
163 .name = RC_MAP_EMPTY,
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
174 * This routine will initialize the rc_map and will allocate
175 * memory to hold at least the specified number of elements.
177 * return: zero on success or a negative error code
179 static int ir_create_table(struct rc_map *rc_map,
180 const char *name, u64 rc_proto, size_t size)
182 rc_map->name = kstrdup(name, GFP_KERNEL);
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);
195 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
196 rc_map->size, rc_map->alloc);
201 * ir_free_table() - frees memory allocated by a scancode table
202 * @rc_map: the table whose mappings need to be freed
204 * This routine will free memory alloctaed for key mappings used by given
207 static void ir_free_table(struct rc_map *rc_map)
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
221 * This routine will shrink the rc_map if it has lots of
222 * unused entries and grow it if it is full.
224 * return: zero on success or a negative error code
226 static int ir_resize_table(struct rc_map *rc_map, gfp_t gfp_flags)
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;
233 if (rc_map->size == rc_map->len) {
234 /* All entries in use -> grow keytable */
235 if (rc_map->alloc >= IR_TAB_MAX_SIZE)
239 IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
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 */
245 IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
248 if (newalloc == oldalloc)
251 newscan = kmalloc(newalloc, gfp_flags);
253 IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
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);
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
272 * This routine is used to update scancode->keycode mapping at given
275 * return: previous keycode assigned to the mapping
278 static unsigned int ir_update_mapping(struct rc_dev *dev,
279 struct rc_map *rc_map,
281 unsigned int new_keycode)
283 int old_keycode = rc_map->scan[index].keycode;
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);
291 memmove(&rc_map->scan[index], &rc_map->scan[index+ 1],
292 (rc_map->len - index) * sizeof(struct rc_map_table));
294 IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n",
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);
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);
313 /* Possibly shrink the keytable, failure is not a problem */
314 ir_resize_table(rc_map, GFP_ATOMIC);
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
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
332 * return: index of the mapping containing scancode in question
333 * or -1U in case of failure.
335 static unsigned int ir_establish_scancode(struct rc_dev *dev,
336 struct rc_map *rc_map,
337 unsigned int scancode,
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.
350 if (dev->scancode_mask)
351 scancode &= dev->scancode_mask;
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)
358 /* Keytable is sorted from lowest to highest scancode */
359 if (rc_map->scan[i].scancode >= scancode)
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))
369 /* i is the proper index to insert our new keycode */
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;
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
386 * This routine is used to handle evdev EVIOCSKEY ioctl.
388 * return: -EINVAL if the keycode could not be inserted, otherwise zero.
390 static int ir_setkeycode(struct input_dev *idev,
391 const struct input_keymap_entry *ke,
392 unsigned int *old_keycode)
394 struct rc_dev *rdev = input_get_drvdata(idev);
395 struct rc_map *rc_map = &rdev->rc_map;
397 unsigned int scancode;
401 spin_lock_irqsave(&rc_map->lock, flags);
403 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
405 if (index >= rc_map->len) {
410 retval = input_scancode_to_scalar(ke, &scancode);
414 index = ir_establish_scancode(rdev, rc_map, scancode, true);
415 if (index >= rc_map->len) {
421 *old_keycode = ir_update_mapping(rdev, rc_map, index, ke->keycode);
424 spin_unlock_irqrestore(&rc_map->lock, flags);
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
433 * This routine is used to handle table initialization.
435 * return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
437 static int ir_setkeytable(struct rc_dev *dev,
438 const struct rc_map *from)
440 struct rc_map *rc_map = &dev->rc_map;
441 unsigned int i, index;
444 rc = ir_create_table(rc_map, from->name,
445 from->rc_proto, from->size);
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) {
457 ir_update_mapping(dev, rc_map, index,
458 from->scan[i].keycode);
462 ir_free_table(rc_map);
467 static int rc_map_cmp(const void *key, const void *elt)
469 const unsigned int *scancode = key;
470 const struct rc_map_table *e = elt;
472 if (*scancode < e->scancode)
474 else if (*scancode > e->scancode)
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
484 * This routine performs binary search in RC keykeymap table for
487 * return: index in the table, -1U if not found
489 static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map,
490 unsigned int scancode)
492 struct rc_map_table *res;
494 res = bsearch(&scancode, rc_map->scan, rc_map->len,
495 sizeof(struct rc_map_table), rc_map_cmp);
499 return res - rc_map->scan;
503 * ir_getkeycode() - get a keycode from the scancode->keycode table
504 * @idev: the struct input_dev device descriptor
505 * @ke: Input keymap entry
507 * This routine is used to handle evdev EVIOCGKEY ioctl.
509 * return: always returns zero.
511 static int ir_getkeycode(struct input_dev *idev,
512 struct input_keymap_entry *ke)
514 struct rc_dev *rdev = input_get_drvdata(idev);
515 struct rc_map *rc_map = &rdev->rc_map;
516 struct rc_map_table *entry;
519 unsigned int scancode;
522 spin_lock_irqsave(&rc_map->lock, flags);
524 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
527 retval = input_scancode_to_scalar(ke, &scancode);
531 index = ir_lookup_by_scancode(rc_map, scancode);
534 if (index < rc_map->len) {
535 entry = &rc_map->scan[index];
538 ke->keycode = entry->keycode;
539 ke->len = sizeof(entry->scancode);
540 memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
542 } else if (!(ke->flags & INPUT_KEYMAP_BY_INDEX)) {
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].
549 ke->keycode = KEY_RESERVED;
558 spin_unlock_irqrestore(&rc_map->lock, flags);
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
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.
571 * return: the corresponding keycode, or KEY_RESERVED
573 u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode)
575 struct rc_map *rc_map = &dev->rc_map;
576 unsigned int keycode;
580 spin_lock_irqsave(&rc_map->lock, flags);
582 index = ir_lookup_by_scancode(rc_map, scancode);
583 keycode = index < rc_map->len ?
584 rc_map->scan[index].keycode : KEY_RESERVED;
586 spin_unlock_irqrestore(&rc_map->lock, flags);
588 if (keycode != KEY_RESERVED)
589 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
590 dev->device_name, scancode, keycode);
594 EXPORT_SYMBOL_GPL(rc_g_keycode_from_table);
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
601 * This function is used internally to release a keypress, it must be
602 * called with keylock held.
604 static void ir_do_keyup(struct rc_dev *dev, bool sync)
606 if (!dev->keypressed)
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);
613 input_sync(dev->input_dev);
614 dev->keypressed = false;
618 * rc_keyup() - signals the release of a keypress
619 * @dev: the struct rc_dev descriptor of the device
621 * This routine is used to signal that a key has been released on the
624 void rc_keyup(struct rc_dev *dev)
628 spin_lock_irqsave(&dev->keylock, flags);
629 ir_do_keyup(dev, true);
630 spin_unlock_irqrestore(&dev->keylock, flags);
632 EXPORT_SYMBOL_GPL(rc_keyup);
635 * ir_timer_keyup() - generates a keyup event after a timeout
637 * @t: a pointer to the struct timer_list
639 * This routine will generate a keyup event some time after a keydown event
640 * is generated when no further activity has been detected.
642 static void ir_timer_keyup(struct timer_list *t)
644 struct rc_dev *dev = from_timer(dev, t, timer_keyup);
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.
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.
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);
664 * rc_repeat() - signals that a key is still pressed
665 * @dev: the struct rc_dev descriptor of the device
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
671 void rc_repeat(struct rc_dev *dev)
674 unsigned int timeout = protocols[dev->last_protocol].repeat_period;
676 spin_lock_irqsave(&dev->keylock, flags);
678 if (!dev->keypressed)
681 input_event(dev->input_dev, EV_MSC, MSC_SCAN, dev->last_scancode);
682 input_sync(dev->input_dev);
684 dev->keyup_jiffies = jiffies + msecs_to_jiffies(timeout);
685 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
688 spin_unlock_irqrestore(&dev->keylock, flags);
690 EXPORT_SYMBOL_GPL(rc_repeat);
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
700 * This function is used internally to register a keypress, it must be
701 * called with keylock held.
703 static void ir_do_keydown(struct rc_dev *dev, enum rc_proto protocol,
704 u32 scancode, u32 keycode, u8 toggle)
706 bool new_event = (!dev->keypressed ||
707 dev->last_protocol != protocol ||
708 dev->last_scancode != scancode ||
709 dev->last_toggle != toggle);
711 if (new_event && dev->keypressed)
712 ir_do_keyup(dev, false);
714 input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode);
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;
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);
728 led_trigger_event(led_feedback, LED_FULL);
731 input_sync(dev->input_dev);
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)
742 * This routine is used to signal that a key has been pressed on the
745 void rc_keydown(struct rc_dev *dev, enum rc_proto protocol, u32 scancode,
749 u32 keycode = rc_g_keycode_from_table(dev, scancode);
751 spin_lock_irqsave(&dev->keylock, flags);
752 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
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);
759 spin_unlock_irqrestore(&dev->keylock, flags);
761 EXPORT_SYMBOL_GPL(rc_keydown);
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)
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.
775 void rc_keydown_notimeout(struct rc_dev *dev, enum rc_proto protocol,
776 u32 scancode, u8 toggle)
779 u32 keycode = rc_g_keycode_from_table(dev, scancode);
781 spin_lock_irqsave(&dev->keylock, flags);
782 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
783 spin_unlock_irqrestore(&dev->keylock, flags);
785 EXPORT_SYMBOL_GPL(rc_keydown_notimeout);
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
793 * return: 0 or -EINVAL if the filter is not valid
795 static int rc_validate_filter(struct rc_dev *dev,
796 struct rc_scancode_filter *filter)
798 u32 mask, s = filter->data;
799 enum rc_proto protocol = dev->wakeup_protocol;
801 if (protocol >= ARRAY_SIZE(protocols))
804 mask = protocols[protocol].scancode_bits;
808 if ((((s >> 16) ^ ~(s >> 8)) & 0xff) == 0)
812 if ((((s >> 24) ^ ~(s >> 16)) & 0xff) == 0)
815 case RC_PROTO_RC6_MCE:
816 if ((s & 0xffff0000) != 0x800f0000)
819 case RC_PROTO_RC6_6A_32:
820 if ((s & 0xffff0000) == 0x800f0000)
827 filter->data &= mask;
828 filter->mask &= mask;
831 * If we have to raw encode the IR for wakeup, we cannot have a mask
833 if (dev->encode_wakeup && filter->mask != 0 && filter->mask != mask)
839 int rc_open(struct rc_dev *rdev)
846 mutex_lock(&rdev->lock);
848 if (!rdev->users++ && rdev->open != NULL)
849 rval = rdev->open(rdev);
854 mutex_unlock(&rdev->lock);
858 EXPORT_SYMBOL_GPL(rc_open);
860 static int ir_open(struct input_dev *idev)
862 struct rc_dev *rdev = input_get_drvdata(idev);
864 return rc_open(rdev);
867 void rc_close(struct rc_dev *rdev)
870 mutex_lock(&rdev->lock);
872 if (!--rdev->users && rdev->close != NULL)
875 mutex_unlock(&rdev->lock);
878 EXPORT_SYMBOL_GPL(rc_close);
880 static void ir_close(struct input_dev *idev)
882 struct rc_dev *rdev = input_get_drvdata(idev);
886 /* class for /sys/class/rc */
887 static char *rc_devnode(struct device *dev, umode_t *mode)
889 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
892 static struct class rc_class = {
894 .devnode = rc_devnode,
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.
902 static const struct {
905 const char *module_name;
907 { RC_PROTO_BIT_NONE, "none", NULL },
908 { RC_PROTO_BIT_OTHER, "other", NULL },
909 { RC_PROTO_BIT_UNKNOWN, "unknown", NULL },
911 RC_PROTO_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
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 },
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.
939 struct rc_filter_attribute {
940 struct device_attribute attr;
941 enum rc_filter_type type;
944 #define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
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), \
953 static bool lirc_is_present(void)
955 #if defined(CONFIG_LIRC_MODULE)
958 mutex_lock(&module_mutex);
959 lirc = find_module("lirc_dev");
960 mutex_unlock(&module_mutex);
962 return lirc ? true : false;
963 #elif defined(CONFIG_LIRC)
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
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.
981 * dev->lock is taken to guard against races between
982 * store_protocols and show_protocols.
984 static ssize_t show_protocols(struct device *device,
985 struct device_attribute *mattr, char *buf)
987 struct rc_dev *dev = to_rc_dev(device);
988 u64 allowed, enabled;
992 mutex_lock(&dev->lock);
994 enabled = dev->enabled_protocols;
995 allowed = dev->allowed_protocols;
996 if (dev->raw && !allowed)
997 allowed = ir_raw_get_allowed_protocols();
999 mutex_unlock(&dev->lock);
1001 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
1002 __func__, (long long)allowed, (long long)enabled);
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);
1010 if (allowed & proto_names[i].type)
1011 allowed &= ~proto_names[i].type;
1014 if (dev->driver_type == RC_DRIVER_IR_RAW && lirc_is_present())
1015 tmp += sprintf(tmp, "[lirc] ");
1021 return tmp + 1 - buf;
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
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.
1035 static int parse_protocol_change(u64 *protocols, const char *buf)
1039 bool enable, disable;
1043 while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
1051 } else if (*tmp == '-') {
1060 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1061 if (!strcasecmp(tmp, proto_names[i].name)) {
1062 mask = proto_names[i].type;
1067 if (i == ARRAY_SIZE(proto_names)) {
1068 if (!strcasecmp(tmp, "lirc"))
1071 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
1081 *protocols &= ~mask;
1087 IR_dprintk(1, "Protocol not specified\n");
1094 static void ir_raw_load_modules(u64 *protocols)
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))
1105 available = ir_raw_get_allowed_protocols();
1106 if (!(*protocols & proto_names[i].type & ~available))
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;
1116 ret = request_module("%s", proto_names[i].module_name);
1118 pr_err("Couldn't load IR protocol module %s\n",
1119 proto_names[i].module_name);
1120 *protocols &= ~proto_names[i].type;
1124 available = ir_raw_get_allowed_protocols();
1125 if (!(*protocols & proto_names[i].type & ~available))
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;
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
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.
1147 * dev->lock is taken to guard against races between
1148 * store_protocols and show_protocols.
1150 static ssize_t store_protocols(struct device *device,
1151 struct device_attribute *mattr,
1152 const char *buf, size_t len)
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;
1160 IR_dprintk(1, "Normal protocol change requested\n");
1161 current_protocols = &dev->enabled_protocols;
1162 filter = &dev->scancode_filter;
1164 if (!dev->change_protocol) {
1165 IR_dprintk(1, "Protocol switching not supported\n");
1169 mutex_lock(&dev->lock);
1171 old_protocols = *current_protocols;
1172 new_protocols = old_protocols;
1173 rc = parse_protocol_change(&new_protocols, buf);
1177 rc = dev->change_protocol(dev, &new_protocols);
1179 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
1180 (long long)new_protocols);
1184 if (dev->driver_type == RC_DRIVER_IR_RAW)
1185 ir_raw_load_modules(&new_protocols);
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);
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.
1200 if (dev->s_filter && filter->mask) {
1202 rc = dev->s_filter(dev, filter);
1209 dev->s_filter(dev, filter);
1216 mutex_unlock(&dev->lock);
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
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.
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.
1234 * dev->lock is taken to guard against races between
1235 * store_filter and show_filter.
1237 static ssize_t show_filter(struct device *device,
1238 struct device_attribute *attr,
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;
1246 mutex_lock(&dev->lock);
1248 if (fattr->type == RC_FILTER_NORMAL)
1249 filter = &dev->scancode_filter;
1251 filter = &dev->scancode_wakeup_filter;
1257 mutex_unlock(&dev->lock);
1259 return sprintf(buf, "%#x\n", val);
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
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
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.
1278 * dev->lock is taken to guard against races between
1279 * store_filter and show_filter.
1281 static ssize_t store_filter(struct device *device,
1282 struct device_attribute *attr,
1283 const char *buf, size_t len)
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;
1290 int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
1292 ret = kstrtoul(buf, 0, &val);
1296 if (fattr->type == RC_FILTER_NORMAL) {
1297 set_filter = dev->s_filter;
1298 filter = &dev->scancode_filter;
1300 set_filter = dev->s_wakeup_filter;
1301 filter = &dev->scancode_wakeup_filter;
1307 mutex_lock(&dev->lock);
1309 new_filter = *filter;
1311 new_filter.mask = val;
1313 new_filter.data = val;
1315 if (fattr->type == RC_FILTER_WAKEUP) {
1317 * Refuse to set a filter unless a protocol is enabled
1318 * and the filter is valid for that protocol
1320 if (dev->wakeup_protocol != RC_PROTO_UNKNOWN)
1321 ret = rc_validate_filter(dev, &new_filter);
1329 if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
1331 /* refuse to set a filter unless a protocol is enabled */
1336 ret = set_filter(dev, &new_filter);
1340 *filter = new_filter;
1343 mutex_unlock(&dev->lock);
1344 return (ret < 0) ? ret : len;
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
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.
1358 * dev->lock is taken to guard against races between
1359 * store_wakeup_protocols and show_wakeup_protocols.
1361 static ssize_t show_wakeup_protocols(struct device *device,
1362 struct device_attribute *mattr,
1365 struct rc_dev *dev = to_rc_dev(device);
1367 enum rc_proto enabled;
1371 mutex_lock(&dev->lock);
1373 allowed = dev->allowed_wakeup_protocols;
1374 enabled = dev->wakeup_protocol;
1376 mutex_unlock(&dev->lock);
1378 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - %d\n",
1379 __func__, (long long)allowed, enabled);
1381 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
1382 if (allowed & (1ULL << i)) {
1384 tmp += sprintf(tmp, "[%s] ", protocols[i].name);
1386 tmp += sprintf(tmp, "%s ", protocols[i].name);
1394 return tmp + 1 - buf;
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
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.
1408 * dev->lock is taken to guard against races between
1409 * store_wakeup_protocols and show_wakeup_protocols.
1411 static ssize_t store_wakeup_protocols(struct device *device,
1412 struct device_attribute *mattr,
1413 const char *buf, size_t len)
1415 struct rc_dev *dev = to_rc_dev(device);
1416 enum rc_proto protocol;
1421 mutex_lock(&dev->lock);
1423 allowed = dev->allowed_wakeup_protocols;
1425 if (sysfs_streq(buf, "none")) {
1426 protocol = RC_PROTO_UNKNOWN;
1428 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
1429 if ((allowed & (1ULL << i)) &&
1430 sysfs_streq(buf, protocols[i].name)) {
1436 if (i == ARRAY_SIZE(protocols)) {
1441 if (dev->encode_wakeup) {
1442 u64 mask = 1ULL << protocol;
1444 ir_raw_load_modules(&mask);
1452 if (dev->wakeup_protocol != protocol) {
1453 dev->wakeup_protocol = protocol;
1454 IR_dprintk(1, "Wakeup protocol changed to %d\n", protocol);
1456 if (protocol == RC_PROTO_RC6_MCE)
1457 dev->scancode_wakeup_filter.data = 0x800f0000;
1459 dev->scancode_wakeup_filter.data = 0;
1460 dev->scancode_wakeup_filter.mask = 0;
1462 rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
1470 mutex_unlock(&dev->lock);
1474 static void rc_dev_release(struct device *device)
1476 struct rc_dev *dev = to_rc_dev(device);
1481 #define ADD_HOTPLUG_VAR(fmt, val...) \
1483 int err = add_uevent_var(env, fmt, val); \
1488 static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1490 struct rc_dev *dev = to_rc_dev(device);
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);
1503 * Static device attribute struct with the sysfs attributes for IR's
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);
1520 static struct attribute *rc_dev_rw_protocol_attrs[] = {
1521 &dev_attr_rw_protocols.attr,
1525 static const struct attribute_group rc_dev_rw_protocol_attr_grp = {
1526 .attrs = rc_dev_rw_protocol_attrs,
1529 static struct attribute *rc_dev_ro_protocol_attrs[] = {
1530 &dev_attr_ro_protocols.attr,
1534 static const struct attribute_group rc_dev_ro_protocol_attr_grp = {
1535 .attrs = rc_dev_ro_protocol_attrs,
1538 static struct attribute *rc_dev_filter_attrs[] = {
1539 &dev_attr_filter.attr.attr,
1540 &dev_attr_filter_mask.attr.attr,
1544 static const struct attribute_group rc_dev_filter_attr_grp = {
1545 .attrs = rc_dev_filter_attrs,
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,
1555 static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
1556 .attrs = rc_dev_wakeup_filter_attrs,
1559 static const struct device_type rc_dev_type = {
1560 .release = rc_dev_release,
1561 .uevent = rc_dev_uevent,
1564 struct rc_dev *rc_allocate_device(enum rc_driver_type type)
1568 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1572 if (type != RC_DRIVER_IR_RAW_TX) {
1573 dev->input_dev = input_allocate_device();
1574 if (!dev->input_dev) {
1579 dev->input_dev->getkeycode = ir_getkeycode;
1580 dev->input_dev->setkeycode = ir_setkeycode;
1581 input_set_drvdata(dev->input_dev, dev);
1583 timer_setup(&dev->timer_keyup, ir_timer_keyup, 0);
1585 spin_lock_init(&dev->rc_map.lock);
1586 spin_lock_init(&dev->keylock);
1588 mutex_init(&dev->lock);
1590 dev->dev.type = &rc_dev_type;
1591 dev->dev.class = &rc_class;
1592 device_initialize(&dev->dev);
1594 dev->driver_type = type;
1596 __module_get(THIS_MODULE);
1599 EXPORT_SYMBOL_GPL(rc_allocate_device);
1601 void rc_free_device(struct rc_dev *dev)
1606 input_free_device(dev->input_dev);
1608 put_device(&dev->dev);
1610 /* kfree(dev) will be called by the callback function
1613 module_put(THIS_MODULE);
1615 EXPORT_SYMBOL_GPL(rc_free_device);
1617 static void devm_rc_alloc_release(struct device *dev, void *res)
1619 rc_free_device(*(struct rc_dev **)res);
1622 struct rc_dev *devm_rc_allocate_device(struct device *dev,
1623 enum rc_driver_type type)
1625 struct rc_dev **dr, *rc;
1627 dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
1631 rc = rc_allocate_device(type);
1637 rc->dev.parent = dev;
1638 rc->managed_alloc = true;
1640 devres_add(dev, dr);
1644 EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
1646 static int rc_prepare_rx_device(struct rc_dev *dev)
1649 struct rc_map *rc_map;
1655 rc_map = rc_map_get(dev->map_name);
1657 rc_map = rc_map_get(RC_MAP_EMPTY);
1658 if (!rc_map || !rc_map->scan || rc_map->size == 0)
1661 rc = ir_setkeytable(dev, rc_map);
1665 rc_proto = BIT_ULL(rc_map->rc_proto);
1667 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1668 dev->enabled_protocols = dev->allowed_protocols;
1670 if (dev->change_protocol) {
1671 rc = dev->change_protocol(dev, &rc_proto);
1674 dev->enabled_protocols = rc_proto;
1677 if (dev->driver_type == RC_DRIVER_IR_RAW)
1678 ir_raw_load_modules(&rc_proto);
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);
1685 dev->input_dev->open = ir_open;
1687 dev->input_dev->close = ir_close;
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;
1697 ir_free_table(&dev->rc_map);
1702 static int rc_setup_rx_device(struct rc_dev *dev)
1706 /* rc_open will be called here */
1707 rc = input_register_device(dev->input_dev);
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().
1717 dev->input_dev->rep[REP_DELAY] = 500;
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
1724 dev->input_dev->rep[REP_PERIOD] = 125;
1729 static void rc_free_rx_device(struct rc_dev *dev)
1734 if (dev->input_dev) {
1735 input_unregister_device(dev->input_dev);
1736 dev->input_dev = NULL;
1739 ir_free_table(&dev->rc_map);
1742 int rc_register_device(struct rc_dev *dev)
1752 minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
1757 dev_set_name(&dev->dev, "rc%u", dev->minor);
1758 dev_set_drvdata(&dev->dev, dev);
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;
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;
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);
1778 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1779 rc = rc_prepare_rx_device(dev);
1784 rc = device_add(&dev->dev);
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");
1793 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1794 rc = rc_setup_rx_device(dev);
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);
1806 IR_dprintk(1, "Registered rc%u (driver: %s)\n",
1808 dev->driver_name ? dev->driver_name : "unknown");
1813 rc_free_rx_device(dev);
1815 device_del(&dev->dev);
1817 ir_free_table(&dev->rc_map);
1819 ir_raw_event_free(dev);
1821 ida_simple_remove(&rc_ida, minor);
1824 EXPORT_SYMBOL_GPL(rc_register_device);
1826 static void devm_rc_release(struct device *dev, void *res)
1828 rc_unregister_device(*(struct rc_dev **)res);
1831 int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
1836 dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
1840 ret = rc_register_device(dev);
1847 devres_add(parent, dr);
1851 EXPORT_SYMBOL_GPL(devm_rc_register_device);
1853 void rc_unregister_device(struct rc_dev *dev)
1858 del_timer_sync(&dev->timer_keyup);
1860 if (dev->driver_type == RC_DRIVER_IR_RAW)
1861 ir_raw_event_unregister(dev);
1863 rc_free_rx_device(dev);
1865 device_del(&dev->dev);
1867 ida_simple_remove(&rc_ida, dev->minor);
1869 if (!dev->managed_alloc)
1870 rc_free_device(dev);
1873 EXPORT_SYMBOL_GPL(rc_unregister_device);
1876 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1879 static int __init rc_core_init(void)
1881 int rc = class_register(&rc_class);
1883 pr_err("rc_core: unable to register rc class\n");
1887 led_trigger_register_simple("rc-feedback", &led_feedback);
1888 rc_map_register(&empty_map);
1893 static void __exit rc_core_exit(void)
1895 class_unregister(&rc_class);
1896 led_trigger_unregister_simple(led_feedback);
1897 rc_map_unregister(&empty_map);
1900 subsys_initcall(rc_core_init);
1901 module_exit(rc_core_exit);
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);
1907 MODULE_AUTHOR("Mauro Carvalho Chehab");
1908 MODULE_LICENSE("GPL");