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 = 164 },
43 [RC_PROTO_RC5X_20] = { .name = "rc-5x-20",
44 .scancode_bits = 0x1f7f3f, .repeat_period = 164 },
45 [RC_PROTO_RC5_SZ] = { .name = "rc-5-sz",
46 .scancode_bits = 0x2fff, .repeat_period = 164 },
47 [RC_PROTO_JVC] = { .name = "jvc",
48 .scancode_bits = 0xffff, .repeat_period = 250 },
49 [RC_PROTO_SONY12] = { .name = "sony-12",
50 .scancode_bits = 0x1f007f, .repeat_period = 100 },
51 [RC_PROTO_SONY15] = { .name = "sony-15",
52 .scancode_bits = 0xff007f, .repeat_period = 100 },
53 [RC_PROTO_SONY20] = { .name = "sony-20",
54 .scancode_bits = 0x1fff7f, .repeat_period = 100 },
55 [RC_PROTO_NEC] = { .name = "nec",
56 .scancode_bits = 0xffff, .repeat_period = 160 },
57 [RC_PROTO_NECX] = { .name = "nec-x",
58 .scancode_bits = 0xffffff, .repeat_period = 160 },
59 [RC_PROTO_NEC32] = { .name = "nec-32",
60 .scancode_bits = 0xffffffff, .repeat_period = 160 },
61 [RC_PROTO_SANYO] = { .name = "sanyo",
62 .scancode_bits = 0x1fffff, .repeat_period = 250 },
63 [RC_PROTO_MCIR2_KBD] = { .name = "mcir2-kbd",
64 .scancode_bits = 0xffff, .repeat_period = 150 },
65 [RC_PROTO_MCIR2_MSE] = { .name = "mcir2-mse",
66 .scancode_bits = 0x1fffff, .repeat_period = 150 },
67 [RC_PROTO_RC6_0] = { .name = "rc-6-0",
68 .scancode_bits = 0xffff, .repeat_period = 164 },
69 [RC_PROTO_RC6_6A_20] = { .name = "rc-6-6a-20",
70 .scancode_bits = 0xfffff, .repeat_period = 164 },
71 [RC_PROTO_RC6_6A_24] = { .name = "rc-6-6a-24",
72 .scancode_bits = 0xffffff, .repeat_period = 164 },
73 [RC_PROTO_RC6_6A_32] = { .name = "rc-6-6a-32",
74 .scancode_bits = 0xffffffff, .repeat_period = 164 },
75 [RC_PROTO_RC6_MCE] = { .name = "rc-6-mce",
76 .scancode_bits = 0xffff7fff, .repeat_period = 164 },
77 [RC_PROTO_SHARP] = { .name = "sharp",
78 .scancode_bits = 0x1fff, .repeat_period = 250 },
79 [RC_PROTO_XMP] = { .name = "xmp", .repeat_period = 250 },
80 [RC_PROTO_CEC] = { .name = "cec", .repeat_period = 550 },
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
173 * @return: zero on success or a negative error code
175 * This routine will initialize the rc_map and will allocate
176 * memory to hold at least the specified number of elements.
178 static int ir_create_table(struct rc_map *rc_map,
179 const char *name, u64 rc_proto, size_t size)
181 rc_map->name = kstrdup(name, GFP_KERNEL);
184 rc_map->rc_proto = rc_proto;
185 rc_map->alloc = roundup_pow_of_two(size * sizeof(struct rc_map_table));
186 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
187 rc_map->scan = kmalloc(rc_map->alloc, GFP_KERNEL);
194 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
195 rc_map->size, rc_map->alloc);
200 * ir_free_table() - frees memory allocated by a scancode table
201 * @rc_map: the table whose mappings need to be freed
203 * This routine will free memory alloctaed for key mappings used by given
206 static void ir_free_table(struct rc_map *rc_map)
216 * ir_resize_table() - resizes a scancode table if necessary
217 * @rc_map: the rc_map to resize
218 * @gfp_flags: gfp flags to use when allocating memory
219 * @return: zero on success or a negative error code
221 * This routine will shrink the rc_map if it has lots of
222 * unused entries and grow it if it is full.
224 static int ir_resize_table(struct rc_map *rc_map, gfp_t gfp_flags)
226 unsigned int oldalloc = rc_map->alloc;
227 unsigned int newalloc = oldalloc;
228 struct rc_map_table *oldscan = rc_map->scan;
229 struct rc_map_table *newscan;
231 if (rc_map->size == rc_map->len) {
232 /* All entries in use -> grow keytable */
233 if (rc_map->alloc >= IR_TAB_MAX_SIZE)
237 IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
240 if ((rc_map->len * 3 < rc_map->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
241 /* Less than 1/3 of entries in use -> shrink keytable */
243 IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
246 if (newalloc == oldalloc)
249 newscan = kmalloc(newalloc, gfp_flags);
251 IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
255 memcpy(newscan, rc_map->scan, rc_map->len * sizeof(struct rc_map_table));
256 rc_map->scan = newscan;
257 rc_map->alloc = newalloc;
258 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
264 * ir_update_mapping() - set a keycode in the scancode->keycode table
265 * @dev: the struct rc_dev device descriptor
266 * @rc_map: scancode table to be adjusted
267 * @index: index of the mapping that needs to be updated
268 * @keycode: the desired keycode
269 * @return: previous keycode assigned to the mapping
271 * This routine is used to update scancode->keycode mapping at given
274 static unsigned int ir_update_mapping(struct rc_dev *dev,
275 struct rc_map *rc_map,
277 unsigned int new_keycode)
279 int old_keycode = rc_map->scan[index].keycode;
282 /* Did the user wish to remove the mapping? */
283 if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
284 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
285 index, rc_map->scan[index].scancode);
287 memmove(&rc_map->scan[index], &rc_map->scan[index+ 1],
288 (rc_map->len - index) * sizeof(struct rc_map_table));
290 IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n",
292 old_keycode == KEY_RESERVED ? "New" : "Replacing",
293 rc_map->scan[index].scancode, new_keycode);
294 rc_map->scan[index].keycode = new_keycode;
295 __set_bit(new_keycode, dev->input_dev->keybit);
298 if (old_keycode != KEY_RESERVED) {
299 /* A previous mapping was updated... */
300 __clear_bit(old_keycode, dev->input_dev->keybit);
301 /* ... but another scancode might use the same keycode */
302 for (i = 0; i < rc_map->len; i++) {
303 if (rc_map->scan[i].keycode == old_keycode) {
304 __set_bit(old_keycode, dev->input_dev->keybit);
309 /* Possibly shrink the keytable, failure is not a problem */
310 ir_resize_table(rc_map, GFP_ATOMIC);
317 * ir_establish_scancode() - set a keycode in the scancode->keycode table
318 * @dev: the struct rc_dev device descriptor
319 * @rc_map: scancode table to be searched
320 * @scancode: the desired scancode
321 * @resize: controls whether we allowed to resize the table to
322 * accommodate not yet present scancodes
323 * @return: index of the mapping containing scancode in question
324 * or -1U in case of failure.
326 * This routine is used to locate given scancode in rc_map.
327 * If scancode is not yet present the routine will allocate a new slot
330 static unsigned int ir_establish_scancode(struct rc_dev *dev,
331 struct rc_map *rc_map,
332 unsigned int scancode,
338 * Unfortunately, some hardware-based IR decoders don't provide
339 * all bits for the complete IR code. In general, they provide only
340 * the command part of the IR code. Yet, as it is possible to replace
341 * the provided IR with another one, it is needed to allow loading
342 * IR tables from other remotes. So, we support specifying a mask to
343 * indicate the valid bits of the scancodes.
345 if (dev->scancode_mask)
346 scancode &= dev->scancode_mask;
348 /* First check if we already have a mapping for this ir command */
349 for (i = 0; i < rc_map->len; i++) {
350 if (rc_map->scan[i].scancode == scancode)
353 /* Keytable is sorted from lowest to highest scancode */
354 if (rc_map->scan[i].scancode >= scancode)
358 /* No previous mapping found, we might need to grow the table */
359 if (rc_map->size == rc_map->len) {
360 if (!resize || ir_resize_table(rc_map, GFP_ATOMIC))
364 /* i is the proper index to insert our new keycode */
366 memmove(&rc_map->scan[i + 1], &rc_map->scan[i],
367 (rc_map->len - i) * sizeof(struct rc_map_table));
368 rc_map->scan[i].scancode = scancode;
369 rc_map->scan[i].keycode = KEY_RESERVED;
376 * ir_setkeycode() - set a keycode in the scancode->keycode table
377 * @idev: the struct input_dev device descriptor
378 * @scancode: the desired scancode
380 * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
382 * This routine is used to handle evdev EVIOCSKEY ioctl.
384 static int ir_setkeycode(struct input_dev *idev,
385 const struct input_keymap_entry *ke,
386 unsigned int *old_keycode)
388 struct rc_dev *rdev = input_get_drvdata(idev);
389 struct rc_map *rc_map = &rdev->rc_map;
391 unsigned int scancode;
395 spin_lock_irqsave(&rc_map->lock, flags);
397 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
399 if (index >= rc_map->len) {
404 retval = input_scancode_to_scalar(ke, &scancode);
408 index = ir_establish_scancode(rdev, rc_map, scancode, true);
409 if (index >= rc_map->len) {
415 *old_keycode = ir_update_mapping(rdev, rc_map, index, ke->keycode);
418 spin_unlock_irqrestore(&rc_map->lock, flags);
423 * ir_setkeytable() - sets several entries in the scancode->keycode table
424 * @dev: the struct rc_dev device descriptor
425 * @to: the struct rc_map to copy entries to
426 * @from: the struct rc_map to copy entries from
427 * @return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
429 * This routine is used to handle table initialization.
431 static int ir_setkeytable(struct rc_dev *dev,
432 const struct rc_map *from)
434 struct rc_map *rc_map = &dev->rc_map;
435 unsigned int i, index;
438 rc = ir_create_table(rc_map, from->name,
439 from->rc_proto, from->size);
443 for (i = 0; i < from->size; i++) {
444 index = ir_establish_scancode(dev, rc_map,
445 from->scan[i].scancode, false);
446 if (index >= rc_map->len) {
451 ir_update_mapping(dev, rc_map, index,
452 from->scan[i].keycode);
456 ir_free_table(rc_map);
461 static int rc_map_cmp(const void *key, const void *elt)
463 const unsigned int *scancode = key;
464 const struct rc_map_table *e = elt;
466 if (*scancode < e->scancode)
468 else if (*scancode > e->scancode)
474 * ir_lookup_by_scancode() - locate mapping by scancode
475 * @rc_map: the struct rc_map to search
476 * @scancode: scancode to look for in the table
477 * @return: index in the table, -1U if not found
479 * This routine performs binary search in RC keykeymap table for
482 static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map,
483 unsigned int scancode)
485 struct rc_map_table *res;
487 res = bsearch(&scancode, rc_map->scan, rc_map->len,
488 sizeof(struct rc_map_table), rc_map_cmp);
492 return res - rc_map->scan;
496 * ir_getkeycode() - get a keycode from the scancode->keycode table
497 * @idev: the struct input_dev device descriptor
498 * @scancode: the desired scancode
499 * @keycode: used to return the keycode, if found, or KEY_RESERVED
500 * @return: always returns zero.
502 * This routine is used to handle evdev EVIOCGKEY ioctl.
504 static int ir_getkeycode(struct input_dev *idev,
505 struct input_keymap_entry *ke)
507 struct rc_dev *rdev = input_get_drvdata(idev);
508 struct rc_map *rc_map = &rdev->rc_map;
509 struct rc_map_table *entry;
512 unsigned int scancode;
515 spin_lock_irqsave(&rc_map->lock, flags);
517 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
520 retval = input_scancode_to_scalar(ke, &scancode);
524 index = ir_lookup_by_scancode(rc_map, scancode);
527 if (index < rc_map->len) {
528 entry = &rc_map->scan[index];
531 ke->keycode = entry->keycode;
532 ke->len = sizeof(entry->scancode);
533 memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
535 } else if (!(ke->flags & INPUT_KEYMAP_BY_INDEX)) {
537 * We do not really know the valid range of scancodes
538 * so let's respond with KEY_RESERVED to anything we
539 * do not have mapping for [yet].
542 ke->keycode = KEY_RESERVED;
551 spin_unlock_irqrestore(&rc_map->lock, flags);
556 * rc_g_keycode_from_table() - gets the keycode that corresponds to a scancode
557 * @dev: the struct rc_dev descriptor of the device
558 * @scancode: the scancode to look for
559 * @return: the corresponding keycode, or KEY_RESERVED
561 * This routine is used by drivers which need to convert a scancode to a
562 * keycode. Normally it should not be used since drivers should have no
563 * interest in keycodes.
565 u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode)
567 struct rc_map *rc_map = &dev->rc_map;
568 unsigned int keycode;
572 spin_lock_irqsave(&rc_map->lock, flags);
574 index = ir_lookup_by_scancode(rc_map, scancode);
575 keycode = index < rc_map->len ?
576 rc_map->scan[index].keycode : KEY_RESERVED;
578 spin_unlock_irqrestore(&rc_map->lock, flags);
580 if (keycode != KEY_RESERVED)
581 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
582 dev->device_name, scancode, keycode);
586 EXPORT_SYMBOL_GPL(rc_g_keycode_from_table);
589 * ir_do_keyup() - internal function to signal the release of a keypress
590 * @dev: the struct rc_dev descriptor of the device
591 * @sync: whether or not to call input_sync
593 * This function is used internally to release a keypress, it must be
594 * called with keylock held.
596 static void ir_do_keyup(struct rc_dev *dev, bool sync)
598 if (!dev->keypressed)
601 IR_dprintk(1, "keyup key 0x%04x\n", dev->last_keycode);
602 input_report_key(dev->input_dev, dev->last_keycode, 0);
603 led_trigger_event(led_feedback, LED_OFF);
605 input_sync(dev->input_dev);
606 dev->keypressed = false;
610 * rc_keyup() - signals the release of a keypress
611 * @dev: the struct rc_dev descriptor of the device
613 * This routine is used to signal that a key has been released on the
616 void rc_keyup(struct rc_dev *dev)
620 spin_lock_irqsave(&dev->keylock, flags);
621 ir_do_keyup(dev, true);
622 spin_unlock_irqrestore(&dev->keylock, flags);
624 EXPORT_SYMBOL_GPL(rc_keyup);
627 * ir_timer_keyup() - generates a keyup event after a timeout
628 * @cookie: a pointer to the struct rc_dev for the device
630 * This routine will generate a keyup event some time after a keydown event
631 * is generated when no further activity has been detected.
633 static void ir_timer_keyup(struct timer_list *t)
635 struct rc_dev *dev = from_timer(dev, t, timer_keyup);
639 * ir->keyup_jiffies is used to prevent a race condition if a
640 * hardware interrupt occurs at this point and the keyup timer
641 * event is moved further into the future as a result.
643 * The timer will then be reactivated and this function called
644 * again in the future. We need to exit gracefully in that case
645 * to allow the input subsystem to do its auto-repeat magic or
646 * a keyup event might follow immediately after the keydown.
648 spin_lock_irqsave(&dev->keylock, flags);
649 if (time_is_before_eq_jiffies(dev->keyup_jiffies))
650 ir_do_keyup(dev, true);
651 spin_unlock_irqrestore(&dev->keylock, flags);
655 * rc_repeat() - signals that a key is still pressed
656 * @dev: the struct rc_dev descriptor of the device
658 * This routine is used by IR decoders when a repeat message which does
659 * not include the necessary bits to reproduce the scancode has been
662 void rc_repeat(struct rc_dev *dev)
665 unsigned int timeout = protocols[dev->last_protocol].repeat_period;
667 spin_lock_irqsave(&dev->keylock, flags);
669 if (!dev->keypressed)
672 input_event(dev->input_dev, EV_MSC, MSC_SCAN, dev->last_scancode);
673 input_sync(dev->input_dev);
675 dev->keyup_jiffies = jiffies + msecs_to_jiffies(timeout);
676 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
679 spin_unlock_irqrestore(&dev->keylock, flags);
681 EXPORT_SYMBOL_GPL(rc_repeat);
684 * ir_do_keydown() - internal function to process a keypress
685 * @dev: the struct rc_dev descriptor of the device
686 * @protocol: the protocol of the keypress
687 * @scancode: the scancode of the keypress
688 * @keycode: the keycode of the keypress
689 * @toggle: the toggle value of the keypress
691 * This function is used internally to register a keypress, it must be
692 * called with keylock held.
694 static void ir_do_keydown(struct rc_dev *dev, enum rc_proto protocol,
695 u32 scancode, u32 keycode, u8 toggle)
697 bool new_event = (!dev->keypressed ||
698 dev->last_protocol != protocol ||
699 dev->last_scancode != scancode ||
700 dev->last_toggle != toggle);
702 if (new_event && dev->keypressed)
703 ir_do_keyup(dev, false);
705 input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode);
707 if (new_event && keycode != KEY_RESERVED) {
708 /* Register a keypress */
709 dev->keypressed = true;
710 dev->last_protocol = protocol;
711 dev->last_scancode = scancode;
712 dev->last_toggle = toggle;
713 dev->last_keycode = keycode;
715 IR_dprintk(1, "%s: key down event, key 0x%04x, protocol 0x%04x, scancode 0x%08x\n",
716 dev->device_name, keycode, protocol, scancode);
717 input_report_key(dev->input_dev, keycode, 1);
719 led_trigger_event(led_feedback, LED_FULL);
722 input_sync(dev->input_dev);
726 * rc_keydown() - generates input event for a key press
727 * @dev: the struct rc_dev descriptor of the device
728 * @protocol: the protocol for the keypress
729 * @scancode: the scancode for the keypress
730 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
731 * support toggle values, this should be set to zero)
733 * This routine is used to signal that a key has been pressed on the
736 void rc_keydown(struct rc_dev *dev, enum rc_proto protocol, u32 scancode,
740 u32 keycode = rc_g_keycode_from_table(dev, scancode);
742 spin_lock_irqsave(&dev->keylock, flags);
743 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
745 if (dev->keypressed) {
746 dev->keyup_jiffies = jiffies +
747 msecs_to_jiffies(protocols[protocol].repeat_period);
748 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
750 spin_unlock_irqrestore(&dev->keylock, flags);
752 EXPORT_SYMBOL_GPL(rc_keydown);
755 * rc_keydown_notimeout() - generates input event for a key press without
756 * an automatic keyup event at a later time
757 * @dev: the struct rc_dev descriptor of the device
758 * @protocol: the protocol for the keypress
759 * @scancode: the scancode for the keypress
760 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
761 * support toggle values, this should be set to zero)
763 * This routine is used to signal that a key has been pressed on the
764 * remote control. The driver must manually call rc_keyup() at a later stage.
766 void rc_keydown_notimeout(struct rc_dev *dev, enum rc_proto protocol,
767 u32 scancode, u8 toggle)
770 u32 keycode = rc_g_keycode_from_table(dev, scancode);
772 spin_lock_irqsave(&dev->keylock, flags);
773 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
774 spin_unlock_irqrestore(&dev->keylock, flags);
776 EXPORT_SYMBOL_GPL(rc_keydown_notimeout);
779 * rc_validate_filter() - checks that the scancode and mask are valid and
780 * provides sensible defaults
781 * @dev: the struct rc_dev descriptor of the device
782 * @filter: the scancode and mask
783 * @return: 0 or -EINVAL if the filter is not valid
785 static int rc_validate_filter(struct rc_dev *dev,
786 struct rc_scancode_filter *filter)
788 u32 mask, s = filter->data;
789 enum rc_proto protocol = dev->wakeup_protocol;
791 if (protocol >= ARRAY_SIZE(protocols))
794 mask = protocols[protocol].scancode_bits;
798 if ((((s >> 16) ^ ~(s >> 8)) & 0xff) == 0)
802 if ((((s >> 24) ^ ~(s >> 16)) & 0xff) == 0)
805 case RC_PROTO_RC6_MCE:
806 if ((s & 0xffff0000) != 0x800f0000)
809 case RC_PROTO_RC6_6A_32:
810 if ((s & 0xffff0000) == 0x800f0000)
817 filter->data &= mask;
818 filter->mask &= mask;
821 * If we have to raw encode the IR for wakeup, we cannot have a mask
823 if (dev->encode_wakeup && filter->mask != 0 && filter->mask != mask)
829 int rc_open(struct rc_dev *rdev)
836 mutex_lock(&rdev->lock);
838 if (!rdev->users++ && rdev->open != NULL)
839 rval = rdev->open(rdev);
844 mutex_unlock(&rdev->lock);
848 EXPORT_SYMBOL_GPL(rc_open);
850 static int ir_open(struct input_dev *idev)
852 struct rc_dev *rdev = input_get_drvdata(idev);
854 return rc_open(rdev);
857 void rc_close(struct rc_dev *rdev)
860 mutex_lock(&rdev->lock);
862 if (!--rdev->users && rdev->close != NULL)
865 mutex_unlock(&rdev->lock);
868 EXPORT_SYMBOL_GPL(rc_close);
870 static void ir_close(struct input_dev *idev)
872 struct rc_dev *rdev = input_get_drvdata(idev);
876 /* class for /sys/class/rc */
877 static char *rc_devnode(struct device *dev, umode_t *mode)
879 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
882 static struct class rc_class = {
884 .devnode = rc_devnode,
888 * These are the protocol textual descriptions that are
889 * used by the sysfs protocols file. Note that the order
890 * of the entries is relevant.
892 static const struct {
895 const char *module_name;
897 { RC_PROTO_BIT_NONE, "none", NULL },
898 { RC_PROTO_BIT_OTHER, "other", NULL },
899 { RC_PROTO_BIT_UNKNOWN, "unknown", NULL },
901 RC_PROTO_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
904 RC_PROTO_BIT_NEC32, "nec", "ir-nec-decoder" },
905 { RC_PROTO_BIT_RC6_0 |
906 RC_PROTO_BIT_RC6_6A_20 |
907 RC_PROTO_BIT_RC6_6A_24 |
908 RC_PROTO_BIT_RC6_6A_32 |
909 RC_PROTO_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
910 { RC_PROTO_BIT_JVC, "jvc", "ir-jvc-decoder" },
911 { RC_PROTO_BIT_SONY12 |
912 RC_PROTO_BIT_SONY15 |
913 RC_PROTO_BIT_SONY20, "sony", "ir-sony-decoder" },
914 { RC_PROTO_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
915 { RC_PROTO_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
916 { RC_PROTO_BIT_SHARP, "sharp", "ir-sharp-decoder" },
917 { RC_PROTO_BIT_MCIR2_KBD |
918 RC_PROTO_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" },
919 { RC_PROTO_BIT_XMP, "xmp", "ir-xmp-decoder" },
920 { RC_PROTO_BIT_CEC, "cec", NULL },
924 * struct rc_filter_attribute - Device attribute relating to a filter type.
925 * @attr: Device attribute.
926 * @type: Filter type.
927 * @mask: false for filter value, true for filter mask.
929 struct rc_filter_attribute {
930 struct device_attribute attr;
931 enum rc_filter_type type;
934 #define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
936 #define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
937 struct rc_filter_attribute dev_attr_##_name = { \
938 .attr = __ATTR(_name, _mode, _show, _store), \
943 static bool lirc_is_present(void)
945 #if defined(CONFIG_LIRC_MODULE)
948 mutex_lock(&module_mutex);
949 lirc = find_module("lirc_dev");
950 mutex_unlock(&module_mutex);
952 return lirc ? true : false;
953 #elif defined(CONFIG_LIRC)
961 * show_protocols() - shows the current IR protocol(s)
962 * @device: the device descriptor
963 * @mattr: the device attribute struct
964 * @buf: a pointer to the output buffer
966 * This routine is a callback routine for input read the IR protocol type(s).
967 * it is trigged by reading /sys/class/rc/rc?/protocols.
968 * It returns the protocol names of supported protocols.
969 * Enabled protocols are printed in brackets.
971 * dev->lock is taken to guard against races between
972 * store_protocols and show_protocols.
974 static ssize_t show_protocols(struct device *device,
975 struct device_attribute *mattr, char *buf)
977 struct rc_dev *dev = to_rc_dev(device);
978 u64 allowed, enabled;
982 mutex_lock(&dev->lock);
984 enabled = dev->enabled_protocols;
985 allowed = dev->allowed_protocols;
986 if (dev->raw && !allowed)
987 allowed = ir_raw_get_allowed_protocols();
989 mutex_unlock(&dev->lock);
991 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
992 __func__, (long long)allowed, (long long)enabled);
994 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
995 if (allowed & enabled & proto_names[i].type)
996 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
997 else if (allowed & proto_names[i].type)
998 tmp += sprintf(tmp, "%s ", proto_names[i].name);
1000 if (allowed & proto_names[i].type)
1001 allowed &= ~proto_names[i].type;
1004 if (dev->driver_type == RC_DRIVER_IR_RAW && lirc_is_present())
1005 tmp += sprintf(tmp, "[lirc] ");
1011 return tmp + 1 - buf;
1015 * parse_protocol_change() - parses a protocol change request
1016 * @protocols: pointer to the bitmask of current protocols
1017 * @buf: pointer to the buffer with a list of changes
1019 * Writing "+proto" will add a protocol to the protocol mask.
1020 * Writing "-proto" will remove a protocol from protocol mask.
1021 * Writing "proto" will enable only "proto".
1022 * Writing "none" will disable all protocols.
1023 * Returns the number of changes performed or a negative error code.
1025 static int parse_protocol_change(u64 *protocols, const char *buf)
1029 bool enable, disable;
1033 while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
1041 } else if (*tmp == '-') {
1050 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1051 if (!strcasecmp(tmp, proto_names[i].name)) {
1052 mask = proto_names[i].type;
1057 if (i == ARRAY_SIZE(proto_names)) {
1058 if (!strcasecmp(tmp, "lirc"))
1061 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
1071 *protocols &= ~mask;
1077 IR_dprintk(1, "Protocol not specified\n");
1084 static void ir_raw_load_modules(u64 *protocols)
1089 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1090 if (proto_names[i].type == RC_PROTO_BIT_NONE ||
1091 proto_names[i].type & (RC_PROTO_BIT_OTHER |
1092 RC_PROTO_BIT_UNKNOWN))
1095 available = ir_raw_get_allowed_protocols();
1096 if (!(*protocols & proto_names[i].type & ~available))
1099 if (!proto_names[i].module_name) {
1100 pr_err("Can't enable IR protocol %s\n",
1101 proto_names[i].name);
1102 *protocols &= ~proto_names[i].type;
1106 ret = request_module("%s", proto_names[i].module_name);
1108 pr_err("Couldn't load IR protocol module %s\n",
1109 proto_names[i].module_name);
1110 *protocols &= ~proto_names[i].type;
1114 available = ir_raw_get_allowed_protocols();
1115 if (!(*protocols & proto_names[i].type & ~available))
1118 pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
1119 proto_names[i].module_name,
1120 proto_names[i].name);
1121 *protocols &= ~proto_names[i].type;
1126 * store_protocols() - changes the current/wakeup IR protocol(s)
1127 * @device: the device descriptor
1128 * @mattr: the device attribute struct
1129 * @buf: a pointer to the input buffer
1130 * @len: length of the input buffer
1132 * This routine is for changing the IR protocol type.
1133 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
1134 * See parse_protocol_change() for the valid commands.
1135 * Returns @len on success or a negative error code.
1137 * dev->lock is taken to guard against races between
1138 * store_protocols and show_protocols.
1140 static ssize_t store_protocols(struct device *device,
1141 struct device_attribute *mattr,
1142 const char *buf, size_t len)
1144 struct rc_dev *dev = to_rc_dev(device);
1145 u64 *current_protocols;
1146 struct rc_scancode_filter *filter;
1147 u64 old_protocols, new_protocols;
1150 IR_dprintk(1, "Normal protocol change requested\n");
1151 current_protocols = &dev->enabled_protocols;
1152 filter = &dev->scancode_filter;
1154 if (!dev->change_protocol) {
1155 IR_dprintk(1, "Protocol switching not supported\n");
1159 mutex_lock(&dev->lock);
1161 old_protocols = *current_protocols;
1162 new_protocols = old_protocols;
1163 rc = parse_protocol_change(&new_protocols, buf);
1167 rc = dev->change_protocol(dev, &new_protocols);
1169 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
1170 (long long)new_protocols);
1174 if (dev->driver_type == RC_DRIVER_IR_RAW)
1175 ir_raw_load_modules(&new_protocols);
1177 if (new_protocols != old_protocols) {
1178 *current_protocols = new_protocols;
1179 IR_dprintk(1, "Protocols changed to 0x%llx\n",
1180 (long long)new_protocols);
1184 * If a protocol change was attempted the filter may need updating, even
1185 * if the actual protocol mask hasn't changed (since the driver may have
1186 * cleared the filter).
1187 * Try setting the same filter with the new protocol (if any).
1188 * Fall back to clearing the filter.
1190 if (dev->s_filter && filter->mask) {
1192 rc = dev->s_filter(dev, filter);
1199 dev->s_filter(dev, filter);
1206 mutex_unlock(&dev->lock);
1211 * show_filter() - shows the current scancode filter value or mask
1212 * @device: the device descriptor
1213 * @attr: the device attribute struct
1214 * @buf: a pointer to the output buffer
1216 * This routine is a callback routine to read a scancode filter value or mask.
1217 * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
1218 * It prints the current scancode filter value or mask of the appropriate filter
1219 * type in hexadecimal into @buf and returns the size of the buffer.
1221 * Bits of the filter value corresponding to set bits in the filter mask are
1222 * compared against input scancodes and non-matching scancodes are discarded.
1224 * dev->lock is taken to guard against races between
1225 * store_filter and show_filter.
1227 static ssize_t show_filter(struct device *device,
1228 struct device_attribute *attr,
1231 struct rc_dev *dev = to_rc_dev(device);
1232 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
1233 struct rc_scancode_filter *filter;
1236 mutex_lock(&dev->lock);
1238 if (fattr->type == RC_FILTER_NORMAL)
1239 filter = &dev->scancode_filter;
1241 filter = &dev->scancode_wakeup_filter;
1247 mutex_unlock(&dev->lock);
1249 return sprintf(buf, "%#x\n", val);
1253 * store_filter() - changes the scancode filter value
1254 * @device: the device descriptor
1255 * @attr: the device attribute struct
1256 * @buf: a pointer to the input buffer
1257 * @len: length of the input buffer
1259 * This routine is for changing a scancode filter value or mask.
1260 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
1261 * Returns -EINVAL if an invalid filter value for the current protocol was
1262 * specified or if scancode filtering is not supported by the driver, otherwise
1265 * Bits of the filter value corresponding to set bits in the filter mask are
1266 * compared against input scancodes and non-matching scancodes are discarded.
1268 * dev->lock is taken to guard against races between
1269 * store_filter and show_filter.
1271 static ssize_t store_filter(struct device *device,
1272 struct device_attribute *attr,
1273 const char *buf, size_t len)
1275 struct rc_dev *dev = to_rc_dev(device);
1276 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
1277 struct rc_scancode_filter new_filter, *filter;
1280 int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
1282 ret = kstrtoul(buf, 0, &val);
1286 if (fattr->type == RC_FILTER_NORMAL) {
1287 set_filter = dev->s_filter;
1288 filter = &dev->scancode_filter;
1290 set_filter = dev->s_wakeup_filter;
1291 filter = &dev->scancode_wakeup_filter;
1297 mutex_lock(&dev->lock);
1299 new_filter = *filter;
1301 new_filter.mask = val;
1303 new_filter.data = val;
1305 if (fattr->type == RC_FILTER_WAKEUP) {
1307 * Refuse to set a filter unless a protocol is enabled
1308 * and the filter is valid for that protocol
1310 if (dev->wakeup_protocol != RC_PROTO_UNKNOWN)
1311 ret = rc_validate_filter(dev, &new_filter);
1319 if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
1321 /* refuse to set a filter unless a protocol is enabled */
1326 ret = set_filter(dev, &new_filter);
1330 *filter = new_filter;
1333 mutex_unlock(&dev->lock);
1334 return (ret < 0) ? ret : len;
1338 * show_wakeup_protocols() - shows the wakeup IR protocol
1339 * @device: the device descriptor
1340 * @mattr: the device attribute struct
1341 * @buf: a pointer to the output buffer
1343 * This routine is a callback routine for input read the IR protocol type(s).
1344 * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols.
1345 * It returns the protocol names of supported protocols.
1346 * The enabled protocols are printed in brackets.
1348 * dev->lock is taken to guard against races between
1349 * store_wakeup_protocols and show_wakeup_protocols.
1351 static ssize_t show_wakeup_protocols(struct device *device,
1352 struct device_attribute *mattr,
1355 struct rc_dev *dev = to_rc_dev(device);
1357 enum rc_proto enabled;
1361 mutex_lock(&dev->lock);
1363 allowed = dev->allowed_wakeup_protocols;
1364 enabled = dev->wakeup_protocol;
1366 mutex_unlock(&dev->lock);
1368 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - %d\n",
1369 __func__, (long long)allowed, enabled);
1371 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
1372 if (allowed & (1ULL << i)) {
1374 tmp += sprintf(tmp, "[%s] ", protocols[i].name);
1376 tmp += sprintf(tmp, "%s ", protocols[i].name);
1384 return tmp + 1 - buf;
1388 * store_wakeup_protocols() - changes the wakeup IR protocol(s)
1389 * @device: the device descriptor
1390 * @mattr: the device attribute struct
1391 * @buf: a pointer to the input buffer
1392 * @len: length of the input buffer
1394 * This routine is for changing the IR protocol type.
1395 * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols.
1396 * Returns @len on success or a negative error code.
1398 * dev->lock is taken to guard against races between
1399 * store_wakeup_protocols and show_wakeup_protocols.
1401 static ssize_t store_wakeup_protocols(struct device *device,
1402 struct device_attribute *mattr,
1403 const char *buf, size_t len)
1405 struct rc_dev *dev = to_rc_dev(device);
1406 enum rc_proto protocol;
1411 mutex_lock(&dev->lock);
1413 allowed = dev->allowed_wakeup_protocols;
1415 if (sysfs_streq(buf, "none")) {
1416 protocol = RC_PROTO_UNKNOWN;
1418 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
1419 if ((allowed & (1ULL << i)) &&
1420 sysfs_streq(buf, protocols[i].name)) {
1426 if (i == ARRAY_SIZE(protocols)) {
1431 if (dev->encode_wakeup) {
1432 u64 mask = 1ULL << protocol;
1434 ir_raw_load_modules(&mask);
1442 if (dev->wakeup_protocol != protocol) {
1443 dev->wakeup_protocol = protocol;
1444 IR_dprintk(1, "Wakeup protocol changed to %d\n", protocol);
1446 if (protocol == RC_PROTO_RC6_MCE)
1447 dev->scancode_wakeup_filter.data = 0x800f0000;
1449 dev->scancode_wakeup_filter.data = 0;
1450 dev->scancode_wakeup_filter.mask = 0;
1452 rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
1460 mutex_unlock(&dev->lock);
1464 static void rc_dev_release(struct device *device)
1466 struct rc_dev *dev = to_rc_dev(device);
1471 #define ADD_HOTPLUG_VAR(fmt, val...) \
1473 int err = add_uevent_var(env, fmt, val); \
1478 static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1480 struct rc_dev *dev = to_rc_dev(device);
1482 if (dev->rc_map.name)
1483 ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
1484 if (dev->driver_name)
1485 ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
1486 if (dev->device_name)
1487 ADD_HOTPLUG_VAR("DEV_NAME=%s", dev->device_name);
1493 * Static device attribute struct with the sysfs attributes for IR's
1495 static struct device_attribute dev_attr_ro_protocols =
1496 __ATTR(protocols, 0444, show_protocols, NULL);
1497 static struct device_attribute dev_attr_rw_protocols =
1498 __ATTR(protocols, 0644, show_protocols, store_protocols);
1499 static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
1500 store_wakeup_protocols);
1501 static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
1502 show_filter, store_filter, RC_FILTER_NORMAL, false);
1503 static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
1504 show_filter, store_filter, RC_FILTER_NORMAL, true);
1505 static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
1506 show_filter, store_filter, RC_FILTER_WAKEUP, false);
1507 static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
1508 show_filter, store_filter, RC_FILTER_WAKEUP, true);
1510 static struct attribute *rc_dev_rw_protocol_attrs[] = {
1511 &dev_attr_rw_protocols.attr,
1515 static const struct attribute_group rc_dev_rw_protocol_attr_grp = {
1516 .attrs = rc_dev_rw_protocol_attrs,
1519 static struct attribute *rc_dev_ro_protocol_attrs[] = {
1520 &dev_attr_ro_protocols.attr,
1524 static const struct attribute_group rc_dev_ro_protocol_attr_grp = {
1525 .attrs = rc_dev_ro_protocol_attrs,
1528 static struct attribute *rc_dev_filter_attrs[] = {
1529 &dev_attr_filter.attr.attr,
1530 &dev_attr_filter_mask.attr.attr,
1534 static const struct attribute_group rc_dev_filter_attr_grp = {
1535 .attrs = rc_dev_filter_attrs,
1538 static struct attribute *rc_dev_wakeup_filter_attrs[] = {
1539 &dev_attr_wakeup_filter.attr.attr,
1540 &dev_attr_wakeup_filter_mask.attr.attr,
1541 &dev_attr_wakeup_protocols.attr,
1545 static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
1546 .attrs = rc_dev_wakeup_filter_attrs,
1549 static const struct device_type rc_dev_type = {
1550 .release = rc_dev_release,
1551 .uevent = rc_dev_uevent,
1554 struct rc_dev *rc_allocate_device(enum rc_driver_type type)
1558 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1562 if (type != RC_DRIVER_IR_RAW_TX) {
1563 dev->input_dev = input_allocate_device();
1564 if (!dev->input_dev) {
1569 dev->input_dev->getkeycode = ir_getkeycode;
1570 dev->input_dev->setkeycode = ir_setkeycode;
1571 input_set_drvdata(dev->input_dev, dev);
1573 timer_setup(&dev->timer_keyup, ir_timer_keyup, 0);
1575 spin_lock_init(&dev->rc_map.lock);
1576 spin_lock_init(&dev->keylock);
1578 mutex_init(&dev->lock);
1580 dev->dev.type = &rc_dev_type;
1581 dev->dev.class = &rc_class;
1582 device_initialize(&dev->dev);
1584 dev->driver_type = type;
1586 __module_get(THIS_MODULE);
1589 EXPORT_SYMBOL_GPL(rc_allocate_device);
1591 void rc_free_device(struct rc_dev *dev)
1596 input_free_device(dev->input_dev);
1598 put_device(&dev->dev);
1600 /* kfree(dev) will be called by the callback function
1603 module_put(THIS_MODULE);
1605 EXPORT_SYMBOL_GPL(rc_free_device);
1607 static void devm_rc_alloc_release(struct device *dev, void *res)
1609 rc_free_device(*(struct rc_dev **)res);
1612 struct rc_dev *devm_rc_allocate_device(struct device *dev,
1613 enum rc_driver_type type)
1615 struct rc_dev **dr, *rc;
1617 dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
1621 rc = rc_allocate_device(type);
1627 rc->dev.parent = dev;
1628 rc->managed_alloc = true;
1630 devres_add(dev, dr);
1634 EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
1636 static int rc_prepare_rx_device(struct rc_dev *dev)
1639 struct rc_map *rc_map;
1645 rc_map = rc_map_get(dev->map_name);
1647 rc_map = rc_map_get(RC_MAP_EMPTY);
1648 if (!rc_map || !rc_map->scan || rc_map->size == 0)
1651 rc = ir_setkeytable(dev, rc_map);
1655 rc_proto = BIT_ULL(rc_map->rc_proto);
1657 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1658 dev->enabled_protocols = dev->allowed_protocols;
1660 if (dev->change_protocol) {
1661 rc = dev->change_protocol(dev, &rc_proto);
1664 dev->enabled_protocols = rc_proto;
1667 if (dev->driver_type == RC_DRIVER_IR_RAW)
1668 ir_raw_load_modules(&rc_proto);
1670 set_bit(EV_KEY, dev->input_dev->evbit);
1671 set_bit(EV_REP, dev->input_dev->evbit);
1672 set_bit(EV_MSC, dev->input_dev->evbit);
1673 set_bit(MSC_SCAN, dev->input_dev->mscbit);
1675 dev->input_dev->open = ir_open;
1677 dev->input_dev->close = ir_close;
1679 dev->input_dev->dev.parent = &dev->dev;
1680 memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
1681 dev->input_dev->phys = dev->input_phys;
1682 dev->input_dev->name = dev->device_name;
1687 ir_free_table(&dev->rc_map);
1692 static int rc_setup_rx_device(struct rc_dev *dev)
1696 /* rc_open will be called here */
1697 rc = input_register_device(dev->input_dev);
1702 * Default delay of 250ms is too short for some protocols, especially
1703 * since the timeout is currently set to 250ms. Increase it to 500ms,
1704 * to avoid wrong repetition of the keycodes. Note that this must be
1705 * set after the call to input_register_device().
1707 dev->input_dev->rep[REP_DELAY] = 500;
1710 * As a repeat event on protocols like RC-5 and NEC take as long as
1711 * 110/114ms, using 33ms as a repeat period is not the right thing
1714 dev->input_dev->rep[REP_PERIOD] = 125;
1719 static void rc_free_rx_device(struct rc_dev *dev)
1724 if (dev->input_dev) {
1725 input_unregister_device(dev->input_dev);
1726 dev->input_dev = NULL;
1729 ir_free_table(&dev->rc_map);
1732 int rc_register_device(struct rc_dev *dev)
1742 minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
1747 dev_set_name(&dev->dev, "rc%u", dev->minor);
1748 dev_set_drvdata(&dev->dev, dev);
1750 dev->dev.groups = dev->sysfs_groups;
1751 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1752 dev->sysfs_groups[attr++] = &rc_dev_ro_protocol_attr_grp;
1753 else if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
1754 dev->sysfs_groups[attr++] = &rc_dev_rw_protocol_attr_grp;
1756 dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
1757 if (dev->s_wakeup_filter)
1758 dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
1759 dev->sysfs_groups[attr++] = NULL;
1761 if (dev->driver_type == RC_DRIVER_IR_RAW ||
1762 dev->driver_type == RC_DRIVER_IR_RAW_TX) {
1763 rc = ir_raw_event_prepare(dev);
1768 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1769 rc = rc_prepare_rx_device(dev);
1774 rc = device_add(&dev->dev);
1778 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
1779 dev_info(&dev->dev, "%s as %s\n",
1780 dev->device_name ?: "Unspecified device", path ?: "N/A");
1783 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1784 rc = rc_setup_rx_device(dev);
1789 if (dev->driver_type == RC_DRIVER_IR_RAW ||
1790 dev->driver_type == RC_DRIVER_IR_RAW_TX) {
1791 rc = ir_raw_event_register(dev);
1796 IR_dprintk(1, "Registered rc%u (driver: %s)\n",
1798 dev->driver_name ? dev->driver_name : "unknown");
1803 rc_free_rx_device(dev);
1805 device_del(&dev->dev);
1807 ir_free_table(&dev->rc_map);
1809 ir_raw_event_free(dev);
1811 ida_simple_remove(&rc_ida, minor);
1814 EXPORT_SYMBOL_GPL(rc_register_device);
1816 static void devm_rc_release(struct device *dev, void *res)
1818 rc_unregister_device(*(struct rc_dev **)res);
1821 int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
1826 dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
1830 ret = rc_register_device(dev);
1837 devres_add(parent, dr);
1841 EXPORT_SYMBOL_GPL(devm_rc_register_device);
1843 void rc_unregister_device(struct rc_dev *dev)
1848 del_timer_sync(&dev->timer_keyup);
1850 if (dev->driver_type == RC_DRIVER_IR_RAW)
1851 ir_raw_event_unregister(dev);
1853 rc_free_rx_device(dev);
1855 device_del(&dev->dev);
1857 ida_simple_remove(&rc_ida, dev->minor);
1859 if (!dev->managed_alloc)
1860 rc_free_device(dev);
1863 EXPORT_SYMBOL_GPL(rc_unregister_device);
1866 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1869 static int __init rc_core_init(void)
1871 int rc = class_register(&rc_class);
1873 pr_err("rc_core: unable to register rc class\n");
1877 led_trigger_register_simple("rc-feedback", &led_feedback);
1878 rc_map_register(&empty_map);
1883 static void __exit rc_core_exit(void)
1885 class_unregister(&rc_class);
1886 led_trigger_unregister_simple(led_feedback);
1887 rc_map_unregister(&empty_map);
1890 subsys_initcall(rc_core_init);
1891 module_exit(rc_core_exit);
1893 int rc_core_debug; /* ir_debug level (0,1,2) */
1894 EXPORT_SYMBOL_GPL(rc_core_debug);
1895 module_param_named(debug, rc_core_debug, int, 0644);
1897 MODULE_AUTHOR("Mauro Carvalho Chehab");
1898 MODULE_LICENSE("GPL");