1 // SPDX-License-Identifier: GPL-2.0
2 // rc-main.c - Remote Controller core module
4 // Copyright (C) 2009-2010 by Mauro Carvalho Chehab
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <media/rc-core.h>
9 #include <linux/bsearch.h>
10 #include <linux/spinlock.h>
11 #include <linux/delay.h>
12 #include <linux/input.h>
13 #include <linux/leds.h>
14 #include <linux/slab.h>
15 #include <linux/idr.h>
16 #include <linux/device.h>
17 #include <linux/module.h>
18 #include "rc-core-priv.h"
20 /* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */
21 #define IR_TAB_MIN_SIZE 256
22 #define IR_TAB_MAX_SIZE 8192
26 unsigned int repeat_period
;
27 unsigned int scancode_bits
;
29 [RC_PROTO_UNKNOWN
] = { .name
= "unknown", .repeat_period
= 250 },
30 [RC_PROTO_OTHER
] = { .name
= "other", .repeat_period
= 250 },
31 [RC_PROTO_RC5
] = { .name
= "rc-5",
32 .scancode_bits
= 0x1f7f, .repeat_period
= 250 },
33 [RC_PROTO_RC5X_20
] = { .name
= "rc-5x-20",
34 .scancode_bits
= 0x1f7f3f, .repeat_period
= 250 },
35 [RC_PROTO_RC5_SZ
] = { .name
= "rc-5-sz",
36 .scancode_bits
= 0x2fff, .repeat_period
= 250 },
37 [RC_PROTO_JVC
] = { .name
= "jvc",
38 .scancode_bits
= 0xffff, .repeat_period
= 250 },
39 [RC_PROTO_SONY12
] = { .name
= "sony-12",
40 .scancode_bits
= 0x1f007f, .repeat_period
= 250 },
41 [RC_PROTO_SONY15
] = { .name
= "sony-15",
42 .scancode_bits
= 0xff007f, .repeat_period
= 250 },
43 [RC_PROTO_SONY20
] = { .name
= "sony-20",
44 .scancode_bits
= 0x1fff7f, .repeat_period
= 250 },
45 [RC_PROTO_NEC
] = { .name
= "nec",
46 .scancode_bits
= 0xffff, .repeat_period
= 250 },
47 [RC_PROTO_NECX
] = { .name
= "nec-x",
48 .scancode_bits
= 0xffffff, .repeat_period
= 250 },
49 [RC_PROTO_NEC32
] = { .name
= "nec-32",
50 .scancode_bits
= 0xffffffff, .repeat_period
= 250 },
51 [RC_PROTO_SANYO
] = { .name
= "sanyo",
52 .scancode_bits
= 0x1fffff, .repeat_period
= 250 },
53 [RC_PROTO_MCIR2_KBD
] = { .name
= "mcir2-kbd",
54 .scancode_bits
= 0xffff, .repeat_period
= 250 },
55 [RC_PROTO_MCIR2_MSE
] = { .name
= "mcir2-mse",
56 .scancode_bits
= 0x1fffff, .repeat_period
= 250 },
57 [RC_PROTO_RC6_0
] = { .name
= "rc-6-0",
58 .scancode_bits
= 0xffff, .repeat_period
= 250 },
59 [RC_PROTO_RC6_6A_20
] = { .name
= "rc-6-6a-20",
60 .scancode_bits
= 0xfffff, .repeat_period
= 250 },
61 [RC_PROTO_RC6_6A_24
] = { .name
= "rc-6-6a-24",
62 .scancode_bits
= 0xffffff, .repeat_period
= 250 },
63 [RC_PROTO_RC6_6A_32
] = { .name
= "rc-6-6a-32",
64 .scancode_bits
= 0xffffffff, .repeat_period
= 250 },
65 [RC_PROTO_RC6_MCE
] = { .name
= "rc-6-mce",
66 .scancode_bits
= 0xffff7fff, .repeat_period
= 250 },
67 [RC_PROTO_SHARP
] = { .name
= "sharp",
68 .scancode_bits
= 0x1fff, .repeat_period
= 250 },
69 [RC_PROTO_XMP
] = { .name
= "xmp", .repeat_period
= 250 },
70 [RC_PROTO_CEC
] = { .name
= "cec", .repeat_period
= 550 },
73 /* Used to keep track of known keymaps */
74 static LIST_HEAD(rc_map_list
);
75 static DEFINE_SPINLOCK(rc_map_lock
);
76 static struct led_trigger
*led_feedback
;
78 /* Used to keep track of rc devices */
79 static DEFINE_IDA(rc_ida
);
81 static struct rc_map_list
*seek_rc_map(const char *name
)
83 struct rc_map_list
*map
= NULL
;
85 spin_lock(&rc_map_lock
);
86 list_for_each_entry(map
, &rc_map_list
, list
) {
87 if (!strcmp(name
, map
->map
.name
)) {
88 spin_unlock(&rc_map_lock
);
92 spin_unlock(&rc_map_lock
);
97 struct rc_map
*rc_map_get(const char *name
)
100 struct rc_map_list
*map
;
102 map
= seek_rc_map(name
);
103 #ifdef CONFIG_MODULES
105 int rc
= request_module("%s", name
);
107 pr_err("Couldn't load IR keymap %s\n", name
);
110 msleep(20); /* Give some time for IR to register */
112 map
= seek_rc_map(name
);
116 pr_err("IR keymap %s not found\n", name
);
120 printk(KERN_INFO
"Registered IR keymap %s\n", map
->map
.name
);
124 EXPORT_SYMBOL_GPL(rc_map_get
);
126 int rc_map_register(struct rc_map_list
*map
)
128 spin_lock(&rc_map_lock
);
129 list_add_tail(&map
->list
, &rc_map_list
);
130 spin_unlock(&rc_map_lock
);
133 EXPORT_SYMBOL_GPL(rc_map_register
);
135 void rc_map_unregister(struct rc_map_list
*map
)
137 spin_lock(&rc_map_lock
);
138 list_del(&map
->list
);
139 spin_unlock(&rc_map_lock
);
141 EXPORT_SYMBOL_GPL(rc_map_unregister
);
144 static struct rc_map_table empty
[] = {
145 { 0x2a, KEY_COFFEE
},
148 static struct rc_map_list empty_map
= {
151 .size
= ARRAY_SIZE(empty
),
152 .rc_proto
= RC_PROTO_UNKNOWN
, /* Legacy IR type */
153 .name
= RC_MAP_EMPTY
,
158 * ir_create_table() - initializes a scancode table
159 * @rc_map: the rc_map to initialize
160 * @name: name to assign to the table
161 * @rc_proto: ir type to assign to the new table
162 * @size: initial size of the table
164 * This routine will initialize the rc_map and will allocate
165 * memory to hold at least the specified number of elements.
167 * return: zero on success or a negative error code
169 static int ir_create_table(struct rc_map
*rc_map
,
170 const char *name
, u64 rc_proto
, size_t size
)
172 rc_map
->name
= kstrdup(name
, GFP_KERNEL
);
175 rc_map
->rc_proto
= rc_proto
;
176 rc_map
->alloc
= roundup_pow_of_two(size
* sizeof(struct rc_map_table
));
177 rc_map
->size
= rc_map
->alloc
/ sizeof(struct rc_map_table
);
178 rc_map
->scan
= kmalloc(rc_map
->alloc
, GFP_KERNEL
);
185 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
186 rc_map
->size
, rc_map
->alloc
);
191 * ir_free_table() - frees memory allocated by a scancode table
192 * @rc_map: the table whose mappings need to be freed
194 * This routine will free memory alloctaed for key mappings used by given
197 static void ir_free_table(struct rc_map
*rc_map
)
207 * ir_resize_table() - resizes a scancode table if necessary
208 * @rc_map: the rc_map to resize
209 * @gfp_flags: gfp flags to use when allocating memory
211 * This routine will shrink the rc_map if it has lots of
212 * unused entries and grow it if it is full.
214 * return: zero on success or a negative error code
216 static int ir_resize_table(struct rc_map
*rc_map
, gfp_t gfp_flags
)
218 unsigned int oldalloc
= rc_map
->alloc
;
219 unsigned int newalloc
= oldalloc
;
220 struct rc_map_table
*oldscan
= rc_map
->scan
;
221 struct rc_map_table
*newscan
;
223 if (rc_map
->size
== rc_map
->len
) {
224 /* All entries in use -> grow keytable */
225 if (rc_map
->alloc
>= IR_TAB_MAX_SIZE
)
229 IR_dprintk(1, "Growing table to %u bytes\n", newalloc
);
232 if ((rc_map
->len
* 3 < rc_map
->size
) && (oldalloc
> IR_TAB_MIN_SIZE
)) {
233 /* Less than 1/3 of entries in use -> shrink keytable */
235 IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc
);
238 if (newalloc
== oldalloc
)
241 newscan
= kmalloc(newalloc
, gfp_flags
);
243 IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc
);
247 memcpy(newscan
, rc_map
->scan
, rc_map
->len
* sizeof(struct rc_map_table
));
248 rc_map
->scan
= newscan
;
249 rc_map
->alloc
= newalloc
;
250 rc_map
->size
= rc_map
->alloc
/ sizeof(struct rc_map_table
);
256 * ir_update_mapping() - set a keycode in the scancode->keycode table
257 * @dev: the struct rc_dev device descriptor
258 * @rc_map: scancode table to be adjusted
259 * @index: index of the mapping that needs to be updated
260 * @new_keycode: the desired keycode
262 * This routine is used to update scancode->keycode mapping at given
265 * return: previous keycode assigned to the mapping
268 static unsigned int ir_update_mapping(struct rc_dev
*dev
,
269 struct rc_map
*rc_map
,
271 unsigned int new_keycode
)
273 int old_keycode
= rc_map
->scan
[index
].keycode
;
276 /* Did the user wish to remove the mapping? */
277 if (new_keycode
== KEY_RESERVED
|| new_keycode
== KEY_UNKNOWN
) {
278 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
279 index
, rc_map
->scan
[index
].scancode
);
281 memmove(&rc_map
->scan
[index
], &rc_map
->scan
[index
+ 1],
282 (rc_map
->len
- index
) * sizeof(struct rc_map_table
));
284 IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n",
286 old_keycode
== KEY_RESERVED
? "New" : "Replacing",
287 rc_map
->scan
[index
].scancode
, new_keycode
);
288 rc_map
->scan
[index
].keycode
= new_keycode
;
289 __set_bit(new_keycode
, dev
->input_dev
->keybit
);
292 if (old_keycode
!= KEY_RESERVED
) {
293 /* A previous mapping was updated... */
294 __clear_bit(old_keycode
, dev
->input_dev
->keybit
);
295 /* ... but another scancode might use the same keycode */
296 for (i
= 0; i
< rc_map
->len
; i
++) {
297 if (rc_map
->scan
[i
].keycode
== old_keycode
) {
298 __set_bit(old_keycode
, dev
->input_dev
->keybit
);
303 /* Possibly shrink the keytable, failure is not a problem */
304 ir_resize_table(rc_map
, GFP_ATOMIC
);
311 * ir_establish_scancode() - set a keycode in the scancode->keycode table
312 * @dev: the struct rc_dev device descriptor
313 * @rc_map: scancode table to be searched
314 * @scancode: the desired scancode
315 * @resize: controls whether we allowed to resize the table to
316 * accommodate not yet present scancodes
318 * This routine is used to locate given scancode in rc_map.
319 * If scancode is not yet present the routine will allocate a new slot
322 * return: index of the mapping containing scancode in question
323 * or -1U in case of failure.
325 static unsigned int ir_establish_scancode(struct rc_dev
*dev
,
326 struct rc_map
*rc_map
,
327 unsigned int scancode
,
333 * Unfortunately, some hardware-based IR decoders don't provide
334 * all bits for the complete IR code. In general, they provide only
335 * the command part of the IR code. Yet, as it is possible to replace
336 * the provided IR with another one, it is needed to allow loading
337 * IR tables from other remotes. So, we support specifying a mask to
338 * indicate the valid bits of the scancodes.
340 if (dev
->scancode_mask
)
341 scancode
&= dev
->scancode_mask
;
343 /* First check if we already have a mapping for this ir command */
344 for (i
= 0; i
< rc_map
->len
; i
++) {
345 if (rc_map
->scan
[i
].scancode
== scancode
)
348 /* Keytable is sorted from lowest to highest scancode */
349 if (rc_map
->scan
[i
].scancode
>= scancode
)
353 /* No previous mapping found, we might need to grow the table */
354 if (rc_map
->size
== rc_map
->len
) {
355 if (!resize
|| ir_resize_table(rc_map
, GFP_ATOMIC
))
359 /* i is the proper index to insert our new keycode */
361 memmove(&rc_map
->scan
[i
+ 1], &rc_map
->scan
[i
],
362 (rc_map
->len
- i
) * sizeof(struct rc_map_table
));
363 rc_map
->scan
[i
].scancode
= scancode
;
364 rc_map
->scan
[i
].keycode
= KEY_RESERVED
;
371 * ir_setkeycode() - set a keycode in the scancode->keycode table
372 * @idev: the struct input_dev device descriptor
373 * @ke: Input keymap entry
374 * @old_keycode: result
376 * This routine is used to handle evdev EVIOCSKEY ioctl.
378 * return: -EINVAL if the keycode could not be inserted, otherwise zero.
380 static int ir_setkeycode(struct input_dev
*idev
,
381 const struct input_keymap_entry
*ke
,
382 unsigned int *old_keycode
)
384 struct rc_dev
*rdev
= input_get_drvdata(idev
);
385 struct rc_map
*rc_map
= &rdev
->rc_map
;
387 unsigned int scancode
;
391 spin_lock_irqsave(&rc_map
->lock
, flags
);
393 if (ke
->flags
& INPUT_KEYMAP_BY_INDEX
) {
395 if (index
>= rc_map
->len
) {
400 retval
= input_scancode_to_scalar(ke
, &scancode
);
404 index
= ir_establish_scancode(rdev
, rc_map
, scancode
, true);
405 if (index
>= rc_map
->len
) {
411 *old_keycode
= ir_update_mapping(rdev
, rc_map
, index
, ke
->keycode
);
414 spin_unlock_irqrestore(&rc_map
->lock
, flags
);
419 * ir_setkeytable() - sets several entries in the scancode->keycode table
420 * @dev: the struct rc_dev device descriptor
421 * @from: the struct rc_map to copy entries from
423 * This routine is used to handle table initialization.
425 * return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
427 static int ir_setkeytable(struct rc_dev
*dev
,
428 const struct rc_map
*from
)
430 struct rc_map
*rc_map
= &dev
->rc_map
;
431 unsigned int i
, index
;
434 rc
= ir_create_table(rc_map
, from
->name
,
435 from
->rc_proto
, from
->size
);
439 for (i
= 0; i
< from
->size
; i
++) {
440 index
= ir_establish_scancode(dev
, rc_map
,
441 from
->scan
[i
].scancode
, false);
442 if (index
>= rc_map
->len
) {
447 ir_update_mapping(dev
, rc_map
, index
,
448 from
->scan
[i
].keycode
);
452 ir_free_table(rc_map
);
457 static int rc_map_cmp(const void *key
, const void *elt
)
459 const unsigned int *scancode
= key
;
460 const struct rc_map_table
*e
= elt
;
462 if (*scancode
< e
->scancode
)
464 else if (*scancode
> e
->scancode
)
470 * ir_lookup_by_scancode() - locate mapping by scancode
471 * @rc_map: the struct rc_map to search
472 * @scancode: scancode to look for in the table
474 * This routine performs binary search in RC keykeymap table for
477 * return: index in the table, -1U if not found
479 static unsigned int ir_lookup_by_scancode(const struct rc_map
*rc_map
,
480 unsigned int scancode
)
482 struct rc_map_table
*res
;
484 res
= bsearch(&scancode
, rc_map
->scan
, rc_map
->len
,
485 sizeof(struct rc_map_table
), rc_map_cmp
);
489 return res
- rc_map
->scan
;
493 * ir_getkeycode() - get a keycode from the scancode->keycode table
494 * @idev: the struct input_dev device descriptor
495 * @ke: Input keymap entry
497 * This routine is used to handle evdev EVIOCGKEY ioctl.
499 * return: always returns zero.
501 static int ir_getkeycode(struct input_dev
*idev
,
502 struct input_keymap_entry
*ke
)
504 struct rc_dev
*rdev
= input_get_drvdata(idev
);
505 struct rc_map
*rc_map
= &rdev
->rc_map
;
506 struct rc_map_table
*entry
;
509 unsigned int scancode
;
512 spin_lock_irqsave(&rc_map
->lock
, flags
);
514 if (ke
->flags
& INPUT_KEYMAP_BY_INDEX
) {
517 retval
= input_scancode_to_scalar(ke
, &scancode
);
521 index
= ir_lookup_by_scancode(rc_map
, scancode
);
524 if (index
< rc_map
->len
) {
525 entry
= &rc_map
->scan
[index
];
528 ke
->keycode
= entry
->keycode
;
529 ke
->len
= sizeof(entry
->scancode
);
530 memcpy(ke
->scancode
, &entry
->scancode
, sizeof(entry
->scancode
));
532 } else if (!(ke
->flags
& INPUT_KEYMAP_BY_INDEX
)) {
534 * We do not really know the valid range of scancodes
535 * so let's respond with KEY_RESERVED to anything we
536 * do not have mapping for [yet].
539 ke
->keycode
= KEY_RESERVED
;
548 spin_unlock_irqrestore(&rc_map
->lock
, flags
);
553 * rc_g_keycode_from_table() - gets the keycode that corresponds to a scancode
554 * @dev: the struct rc_dev descriptor of the device
555 * @scancode: the scancode to look for
557 * This routine is used by drivers which need to convert a scancode to a
558 * keycode. Normally it should not be used since drivers should have no
559 * interest in keycodes.
561 * return: the corresponding keycode, or KEY_RESERVED
563 u32
rc_g_keycode_from_table(struct rc_dev
*dev
, u32 scancode
)
565 struct rc_map
*rc_map
= &dev
->rc_map
;
566 unsigned int keycode
;
570 spin_lock_irqsave(&rc_map
->lock
, flags
);
572 index
= ir_lookup_by_scancode(rc_map
, scancode
);
573 keycode
= index
< rc_map
->len
?
574 rc_map
->scan
[index
].keycode
: KEY_RESERVED
;
576 spin_unlock_irqrestore(&rc_map
->lock
, flags
);
578 if (keycode
!= KEY_RESERVED
)
579 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
580 dev
->device_name
, scancode
, keycode
);
584 EXPORT_SYMBOL_GPL(rc_g_keycode_from_table
);
587 * ir_do_keyup() - internal function to signal the release of a keypress
588 * @dev: the struct rc_dev descriptor of the device
589 * @sync: whether or not to call input_sync
591 * This function is used internally to release a keypress, it must be
592 * called with keylock held.
594 static void ir_do_keyup(struct rc_dev
*dev
, bool sync
)
596 if (!dev
->keypressed
)
599 IR_dprintk(1, "keyup key 0x%04x\n", dev
->last_keycode
);
600 del_timer(&dev
->timer_repeat
);
601 input_report_key(dev
->input_dev
, dev
->last_keycode
, 0);
602 led_trigger_event(led_feedback
, LED_OFF
);
604 input_sync(dev
->input_dev
);
605 dev
->keypressed
= false;
609 * rc_keyup() - signals the release of a keypress
610 * @dev: the struct rc_dev descriptor of the device
612 * This routine is used to signal that a key has been released on the
615 void rc_keyup(struct rc_dev
*dev
)
619 spin_lock_irqsave(&dev
->keylock
, flags
);
620 ir_do_keyup(dev
, true);
621 spin_unlock_irqrestore(&dev
->keylock
, flags
);
623 EXPORT_SYMBOL_GPL(rc_keyup
);
626 * ir_timer_keyup() - generates a keyup event after a timeout
628 * @t: a pointer to the struct timer_list
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 * ir_timer_repeat() - generates a repeat event after a timeout
657 * @t: a pointer to the struct timer_list
659 * This routine will generate a soft repeat event every REP_PERIOD
662 static void ir_timer_repeat(struct timer_list
*t
)
664 struct rc_dev
*dev
= from_timer(dev
, t
, timer_repeat
);
665 struct input_dev
*input
= dev
->input_dev
;
668 spin_lock_irqsave(&dev
->keylock
, flags
);
669 if (dev
->keypressed
) {
670 input_event(input
, EV_KEY
, dev
->last_keycode
, 2);
672 if (input
->rep
[REP_PERIOD
])
673 mod_timer(&dev
->timer_repeat
, jiffies
+
674 msecs_to_jiffies(input
->rep
[REP_PERIOD
]));
676 spin_unlock_irqrestore(&dev
->keylock
, flags
);
680 * rc_repeat() - signals that a key is still pressed
681 * @dev: the struct rc_dev descriptor of the device
683 * This routine is used by IR decoders when a repeat message which does
684 * not include the necessary bits to reproduce the scancode has been
687 void rc_repeat(struct rc_dev
*dev
)
690 unsigned int timeout
= protocols
[dev
->last_protocol
].repeat_period
;
691 struct lirc_scancode sc
= {
692 .scancode
= dev
->last_scancode
, .rc_proto
= dev
->last_protocol
,
693 .keycode
= dev
->keypressed
? dev
->last_keycode
: KEY_RESERVED
,
694 .flags
= LIRC_SCANCODE_FLAG_REPEAT
|
695 (dev
->last_toggle
? LIRC_SCANCODE_FLAG_TOGGLE
: 0)
698 ir_lirc_scancode_event(dev
, &sc
);
700 spin_lock_irqsave(&dev
->keylock
, flags
);
702 input_event(dev
->input_dev
, EV_MSC
, MSC_SCAN
, dev
->last_scancode
);
703 input_sync(dev
->input_dev
);
705 if (dev
->keypressed
) {
706 dev
->keyup_jiffies
= jiffies
+ msecs_to_jiffies(timeout
);
707 mod_timer(&dev
->timer_keyup
, dev
->keyup_jiffies
);
710 spin_unlock_irqrestore(&dev
->keylock
, flags
);
712 EXPORT_SYMBOL_GPL(rc_repeat
);
715 * ir_do_keydown() - internal function to process a keypress
716 * @dev: the struct rc_dev descriptor of the device
717 * @protocol: the protocol of the keypress
718 * @scancode: the scancode of the keypress
719 * @keycode: the keycode of the keypress
720 * @toggle: the toggle value of the keypress
722 * This function is used internally to register a keypress, it must be
723 * called with keylock held.
725 static void ir_do_keydown(struct rc_dev
*dev
, enum rc_proto protocol
,
726 u32 scancode
, u32 keycode
, u8 toggle
)
728 bool new_event
= (!dev
->keypressed
||
729 dev
->last_protocol
!= protocol
||
730 dev
->last_scancode
!= scancode
||
731 dev
->last_toggle
!= toggle
);
732 struct lirc_scancode sc
= {
733 .scancode
= scancode
, .rc_proto
= protocol
,
734 .flags
= toggle
? LIRC_SCANCODE_FLAG_TOGGLE
: 0,
738 ir_lirc_scancode_event(dev
, &sc
);
740 if (new_event
&& dev
->keypressed
)
741 ir_do_keyup(dev
, false);
743 input_event(dev
->input_dev
, EV_MSC
, MSC_SCAN
, scancode
);
745 dev
->last_protocol
= protocol
;
746 dev
->last_scancode
= scancode
;
747 dev
->last_toggle
= toggle
;
748 dev
->last_keycode
= keycode
;
750 if (new_event
&& keycode
!= KEY_RESERVED
) {
751 /* Register a keypress */
752 dev
->keypressed
= true;
754 IR_dprintk(1, "%s: key down event, key 0x%04x, protocol 0x%04x, scancode 0x%08x\n",
755 dev
->device_name
, keycode
, protocol
, scancode
);
756 input_report_key(dev
->input_dev
, keycode
, 1);
758 led_trigger_event(led_feedback
, LED_FULL
);
762 * For CEC, start sending repeat messages as soon as the first
763 * repeated message is sent, as long as REP_DELAY = 0 and REP_PERIOD
764 * is non-zero. Otherwise, the input layer will generate repeat
767 if (!new_event
&& keycode
!= KEY_RESERVED
&&
768 dev
->allowed_protocols
== RC_PROTO_BIT_CEC
&&
769 !timer_pending(&dev
->timer_repeat
) &&
770 dev
->input_dev
->rep
[REP_PERIOD
] &&
771 !dev
->input_dev
->rep
[REP_DELAY
]) {
772 input_event(dev
->input_dev
, EV_KEY
, keycode
, 2);
773 mod_timer(&dev
->timer_repeat
, jiffies
+
774 msecs_to_jiffies(dev
->input_dev
->rep
[REP_PERIOD
]));
777 input_sync(dev
->input_dev
);
781 * rc_keydown() - generates input event for a key press
782 * @dev: the struct rc_dev descriptor of the device
783 * @protocol: the protocol for the keypress
784 * @scancode: the scancode for the keypress
785 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
786 * support toggle values, this should be set to zero)
788 * This routine is used to signal that a key has been pressed on the
791 void rc_keydown(struct rc_dev
*dev
, enum rc_proto protocol
, u32 scancode
,
795 u32 keycode
= rc_g_keycode_from_table(dev
, scancode
);
797 spin_lock_irqsave(&dev
->keylock
, flags
);
798 ir_do_keydown(dev
, protocol
, scancode
, keycode
, toggle
);
800 if (dev
->keypressed
) {
801 dev
->keyup_jiffies
= jiffies
+
802 msecs_to_jiffies(protocols
[protocol
].repeat_period
);
803 mod_timer(&dev
->timer_keyup
, dev
->keyup_jiffies
);
805 spin_unlock_irqrestore(&dev
->keylock
, flags
);
807 EXPORT_SYMBOL_GPL(rc_keydown
);
810 * rc_keydown_notimeout() - generates input event for a key press without
811 * an automatic keyup event at a later time
812 * @dev: the struct rc_dev descriptor of the device
813 * @protocol: the protocol for the keypress
814 * @scancode: the scancode for the keypress
815 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
816 * support toggle values, this should be set to zero)
818 * This routine is used to signal that a key has been pressed on the
819 * remote control. The driver must manually call rc_keyup() at a later stage.
821 void rc_keydown_notimeout(struct rc_dev
*dev
, enum rc_proto protocol
,
822 u32 scancode
, u8 toggle
)
825 u32 keycode
= rc_g_keycode_from_table(dev
, scancode
);
827 spin_lock_irqsave(&dev
->keylock
, flags
);
828 ir_do_keydown(dev
, protocol
, scancode
, keycode
, toggle
);
829 spin_unlock_irqrestore(&dev
->keylock
, flags
);
831 EXPORT_SYMBOL_GPL(rc_keydown_notimeout
);
834 * rc_validate_scancode() - checks that a scancode is valid for a protocol.
835 * For nec, it should do the opposite of ir_nec_bytes_to_scancode()
837 * @scancode: scancode
839 bool rc_validate_scancode(enum rc_proto proto
, u32 scancode
)
843 * NECX has a 16-bit address; if the lower 8 bits match the upper
844 * 8 bits inverted, then the address would match regular nec.
847 if ((((scancode
>> 16) ^ ~(scancode
>> 8)) & 0xff) == 0)
851 * NEC32 has a 16 bit address and 16 bit command. If the lower 8 bits
852 * of the command match the upper 8 bits inverted, then it would
853 * be either NEC or NECX.
856 if ((((scancode
>> 8) ^ ~scancode
) & 0xff) == 0)
860 * If the customer code (top 32-bit) is 0x800f, it is MCE else it
861 * is regular mode-6a 32 bit
863 case RC_PROTO_RC6_MCE
:
864 if ((scancode
& 0xffff0000) != 0x800f0000)
867 case RC_PROTO_RC6_6A_32
:
868 if ((scancode
& 0xffff0000) == 0x800f0000)
879 * rc_validate_filter() - checks that the scancode and mask are valid and
880 * provides sensible defaults
881 * @dev: the struct rc_dev descriptor of the device
882 * @filter: the scancode and mask
884 * return: 0 or -EINVAL if the filter is not valid
886 static int rc_validate_filter(struct rc_dev
*dev
,
887 struct rc_scancode_filter
*filter
)
889 u32 mask
, s
= filter
->data
;
890 enum rc_proto protocol
= dev
->wakeup_protocol
;
892 if (protocol
>= ARRAY_SIZE(protocols
))
895 mask
= protocols
[protocol
].scancode_bits
;
897 if (!rc_validate_scancode(protocol
, s
))
900 filter
->data
&= mask
;
901 filter
->mask
&= mask
;
904 * If we have to raw encode the IR for wakeup, we cannot have a mask
906 if (dev
->encode_wakeup
&& filter
->mask
!= 0 && filter
->mask
!= mask
)
912 int rc_open(struct rc_dev
*rdev
)
919 mutex_lock(&rdev
->lock
);
921 if (!rdev
->registered
) {
924 if (!rdev
->users
++ && rdev
->open
)
925 rval
= rdev
->open(rdev
);
931 mutex_unlock(&rdev
->lock
);
936 static int ir_open(struct input_dev
*idev
)
938 struct rc_dev
*rdev
= input_get_drvdata(idev
);
940 return rc_open(rdev
);
943 void rc_close(struct rc_dev
*rdev
)
946 mutex_lock(&rdev
->lock
);
948 if (!--rdev
->users
&& rdev
->close
&& rdev
->registered
)
951 mutex_unlock(&rdev
->lock
);
955 static void ir_close(struct input_dev
*idev
)
957 struct rc_dev
*rdev
= input_get_drvdata(idev
);
961 /* class for /sys/class/rc */
962 static char *rc_devnode(struct device
*dev
, umode_t
*mode
)
964 return kasprintf(GFP_KERNEL
, "rc/%s", dev_name(dev
));
967 static struct class rc_class
= {
969 .devnode
= rc_devnode
,
973 * These are the protocol textual descriptions that are
974 * used by the sysfs protocols file. Note that the order
975 * of the entries is relevant.
977 static const struct {
980 const char *module_name
;
982 { RC_PROTO_BIT_NONE
, "none", NULL
},
983 { RC_PROTO_BIT_OTHER
, "other", NULL
},
984 { RC_PROTO_BIT_UNKNOWN
, "unknown", NULL
},
986 RC_PROTO_BIT_RC5X_20
, "rc-5", "ir-rc5-decoder" },
989 RC_PROTO_BIT_NEC32
, "nec", "ir-nec-decoder" },
990 { RC_PROTO_BIT_RC6_0
|
991 RC_PROTO_BIT_RC6_6A_20
|
992 RC_PROTO_BIT_RC6_6A_24
|
993 RC_PROTO_BIT_RC6_6A_32
|
994 RC_PROTO_BIT_RC6_MCE
, "rc-6", "ir-rc6-decoder" },
995 { RC_PROTO_BIT_JVC
, "jvc", "ir-jvc-decoder" },
996 { RC_PROTO_BIT_SONY12
|
997 RC_PROTO_BIT_SONY15
|
998 RC_PROTO_BIT_SONY20
, "sony", "ir-sony-decoder" },
999 { RC_PROTO_BIT_RC5_SZ
, "rc-5-sz", "ir-rc5-decoder" },
1000 { RC_PROTO_BIT_SANYO
, "sanyo", "ir-sanyo-decoder" },
1001 { RC_PROTO_BIT_SHARP
, "sharp", "ir-sharp-decoder" },
1002 { RC_PROTO_BIT_MCIR2_KBD
|
1003 RC_PROTO_BIT_MCIR2_MSE
, "mce_kbd", "ir-mce_kbd-decoder" },
1004 { RC_PROTO_BIT_XMP
, "xmp", "ir-xmp-decoder" },
1005 { RC_PROTO_BIT_CEC
, "cec", NULL
},
1009 * struct rc_filter_attribute - Device attribute relating to a filter type.
1010 * @attr: Device attribute.
1011 * @type: Filter type.
1012 * @mask: false for filter value, true for filter mask.
1014 struct rc_filter_attribute
{
1015 struct device_attribute attr
;
1016 enum rc_filter_type type
;
1019 #define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
1021 #define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
1022 struct rc_filter_attribute dev_attr_##_name = { \
1023 .attr = __ATTR(_name, _mode, _show, _store), \
1029 * show_protocols() - shows the current IR protocol(s)
1030 * @device: the device descriptor
1031 * @mattr: the device attribute struct
1032 * @buf: a pointer to the output buffer
1034 * This routine is a callback routine for input read the IR protocol type(s).
1035 * it is trigged by reading /sys/class/rc/rc?/protocols.
1036 * It returns the protocol names of supported protocols.
1037 * Enabled protocols are printed in brackets.
1039 * dev->lock is taken to guard against races between
1040 * store_protocols and show_protocols.
1042 static ssize_t
show_protocols(struct device
*device
,
1043 struct device_attribute
*mattr
, char *buf
)
1045 struct rc_dev
*dev
= to_rc_dev(device
);
1046 u64 allowed
, enabled
;
1050 mutex_lock(&dev
->lock
);
1052 enabled
= dev
->enabled_protocols
;
1053 allowed
= dev
->allowed_protocols
;
1054 if (dev
->raw
&& !allowed
)
1055 allowed
= ir_raw_get_allowed_protocols();
1057 mutex_unlock(&dev
->lock
);
1059 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
1060 __func__
, (long long)allowed
, (long long)enabled
);
1062 for (i
= 0; i
< ARRAY_SIZE(proto_names
); i
++) {
1063 if (allowed
& enabled
& proto_names
[i
].type
)
1064 tmp
+= sprintf(tmp
, "[%s] ", proto_names
[i
].name
);
1065 else if (allowed
& proto_names
[i
].type
)
1066 tmp
+= sprintf(tmp
, "%s ", proto_names
[i
].name
);
1068 if (allowed
& proto_names
[i
].type
)
1069 allowed
&= ~proto_names
[i
].type
;
1073 if (dev
->driver_type
== RC_DRIVER_IR_RAW
)
1074 tmp
+= sprintf(tmp
, "[lirc] ");
1081 return tmp
+ 1 - buf
;
1085 * parse_protocol_change() - parses a protocol change request
1086 * @protocols: pointer to the bitmask of current protocols
1087 * @buf: pointer to the buffer with a list of changes
1089 * Writing "+proto" will add a protocol to the protocol mask.
1090 * Writing "-proto" will remove a protocol from protocol mask.
1091 * Writing "proto" will enable only "proto".
1092 * Writing "none" will disable all protocols.
1093 * Returns the number of changes performed or a negative error code.
1095 static int parse_protocol_change(u64
*protocols
, const char *buf
)
1099 bool enable
, disable
;
1103 while ((tmp
= strsep((char **)&buf
, " \n")) != NULL
) {
1111 } else if (*tmp
== '-') {
1120 for (i
= 0; i
< ARRAY_SIZE(proto_names
); i
++) {
1121 if (!strcasecmp(tmp
, proto_names
[i
].name
)) {
1122 mask
= proto_names
[i
].type
;
1127 if (i
== ARRAY_SIZE(proto_names
)) {
1128 if (!strcasecmp(tmp
, "lirc"))
1131 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp
);
1141 *protocols
&= ~mask
;
1147 IR_dprintk(1, "Protocol not specified\n");
1154 void ir_raw_load_modules(u64
*protocols
)
1159 for (i
= 0; i
< ARRAY_SIZE(proto_names
); i
++) {
1160 if (proto_names
[i
].type
== RC_PROTO_BIT_NONE
||
1161 proto_names
[i
].type
& (RC_PROTO_BIT_OTHER
|
1162 RC_PROTO_BIT_UNKNOWN
))
1165 available
= ir_raw_get_allowed_protocols();
1166 if (!(*protocols
& proto_names
[i
].type
& ~available
))
1169 if (!proto_names
[i
].module_name
) {
1170 pr_err("Can't enable IR protocol %s\n",
1171 proto_names
[i
].name
);
1172 *protocols
&= ~proto_names
[i
].type
;
1176 ret
= request_module("%s", proto_names
[i
].module_name
);
1178 pr_err("Couldn't load IR protocol module %s\n",
1179 proto_names
[i
].module_name
);
1180 *protocols
&= ~proto_names
[i
].type
;
1184 available
= ir_raw_get_allowed_protocols();
1185 if (!(*protocols
& proto_names
[i
].type
& ~available
))
1188 pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
1189 proto_names
[i
].module_name
,
1190 proto_names
[i
].name
);
1191 *protocols
&= ~proto_names
[i
].type
;
1196 * store_protocols() - changes the current/wakeup IR protocol(s)
1197 * @device: the device descriptor
1198 * @mattr: the device attribute struct
1199 * @buf: a pointer to the input buffer
1200 * @len: length of the input buffer
1202 * This routine is for changing the IR protocol type.
1203 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
1204 * See parse_protocol_change() for the valid commands.
1205 * Returns @len on success or a negative error code.
1207 * dev->lock is taken to guard against races between
1208 * store_protocols and show_protocols.
1210 static ssize_t
store_protocols(struct device
*device
,
1211 struct device_attribute
*mattr
,
1212 const char *buf
, size_t len
)
1214 struct rc_dev
*dev
= to_rc_dev(device
);
1215 u64
*current_protocols
;
1216 struct rc_scancode_filter
*filter
;
1217 u64 old_protocols
, new_protocols
;
1220 IR_dprintk(1, "Normal protocol change requested\n");
1221 current_protocols
= &dev
->enabled_protocols
;
1222 filter
= &dev
->scancode_filter
;
1224 if (!dev
->change_protocol
) {
1225 IR_dprintk(1, "Protocol switching not supported\n");
1229 mutex_lock(&dev
->lock
);
1231 old_protocols
= *current_protocols
;
1232 new_protocols
= old_protocols
;
1233 rc
= parse_protocol_change(&new_protocols
, buf
);
1237 rc
= dev
->change_protocol(dev
, &new_protocols
);
1239 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
1240 (long long)new_protocols
);
1244 if (dev
->driver_type
== RC_DRIVER_IR_RAW
)
1245 ir_raw_load_modules(&new_protocols
);
1247 if (new_protocols
!= old_protocols
) {
1248 *current_protocols
= new_protocols
;
1249 IR_dprintk(1, "Protocols changed to 0x%llx\n",
1250 (long long)new_protocols
);
1254 * If a protocol change was attempted the filter may need updating, even
1255 * if the actual protocol mask hasn't changed (since the driver may have
1256 * cleared the filter).
1257 * Try setting the same filter with the new protocol (if any).
1258 * Fall back to clearing the filter.
1260 if (dev
->s_filter
&& filter
->mask
) {
1262 rc
= dev
->s_filter(dev
, filter
);
1269 dev
->s_filter(dev
, filter
);
1276 mutex_unlock(&dev
->lock
);
1281 * show_filter() - shows the current scancode filter value or mask
1282 * @device: the device descriptor
1283 * @attr: the device attribute struct
1284 * @buf: a pointer to the output buffer
1286 * This routine is a callback routine to read a scancode filter value or mask.
1287 * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
1288 * It prints the current scancode filter value or mask of the appropriate filter
1289 * type in hexadecimal into @buf and returns the size of the buffer.
1291 * Bits of the filter value corresponding to set bits in the filter mask are
1292 * compared against input scancodes and non-matching scancodes are discarded.
1294 * dev->lock is taken to guard against races between
1295 * store_filter and show_filter.
1297 static ssize_t
show_filter(struct device
*device
,
1298 struct device_attribute
*attr
,
1301 struct rc_dev
*dev
= to_rc_dev(device
);
1302 struct rc_filter_attribute
*fattr
= to_rc_filter_attr(attr
);
1303 struct rc_scancode_filter
*filter
;
1306 mutex_lock(&dev
->lock
);
1308 if (fattr
->type
== RC_FILTER_NORMAL
)
1309 filter
= &dev
->scancode_filter
;
1311 filter
= &dev
->scancode_wakeup_filter
;
1317 mutex_unlock(&dev
->lock
);
1319 return sprintf(buf
, "%#x\n", val
);
1323 * store_filter() - changes the scancode filter value
1324 * @device: the device descriptor
1325 * @attr: the device attribute struct
1326 * @buf: a pointer to the input buffer
1327 * @len: length of the input buffer
1329 * This routine is for changing a scancode filter value or mask.
1330 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
1331 * Returns -EINVAL if an invalid filter value for the current protocol was
1332 * specified or if scancode filtering is not supported by the driver, otherwise
1335 * Bits of the filter value corresponding to set bits in the filter mask are
1336 * compared against input scancodes and non-matching scancodes are discarded.
1338 * dev->lock is taken to guard against races between
1339 * store_filter and show_filter.
1341 static ssize_t
store_filter(struct device
*device
,
1342 struct device_attribute
*attr
,
1343 const char *buf
, size_t len
)
1345 struct rc_dev
*dev
= to_rc_dev(device
);
1346 struct rc_filter_attribute
*fattr
= to_rc_filter_attr(attr
);
1347 struct rc_scancode_filter new_filter
, *filter
;
1350 int (*set_filter
)(struct rc_dev
*dev
, struct rc_scancode_filter
*filter
);
1352 ret
= kstrtoul(buf
, 0, &val
);
1356 if (fattr
->type
== RC_FILTER_NORMAL
) {
1357 set_filter
= dev
->s_filter
;
1358 filter
= &dev
->scancode_filter
;
1360 set_filter
= dev
->s_wakeup_filter
;
1361 filter
= &dev
->scancode_wakeup_filter
;
1367 mutex_lock(&dev
->lock
);
1369 new_filter
= *filter
;
1371 new_filter
.mask
= val
;
1373 new_filter
.data
= val
;
1375 if (fattr
->type
== RC_FILTER_WAKEUP
) {
1377 * Refuse to set a filter unless a protocol is enabled
1378 * and the filter is valid for that protocol
1380 if (dev
->wakeup_protocol
!= RC_PROTO_UNKNOWN
)
1381 ret
= rc_validate_filter(dev
, &new_filter
);
1389 if (fattr
->type
== RC_FILTER_NORMAL
&& !dev
->enabled_protocols
&&
1391 /* refuse to set a filter unless a protocol is enabled */
1396 ret
= set_filter(dev
, &new_filter
);
1400 *filter
= new_filter
;
1403 mutex_unlock(&dev
->lock
);
1404 return (ret
< 0) ? ret
: len
;
1408 * show_wakeup_protocols() - shows the wakeup IR protocol
1409 * @device: the device descriptor
1410 * @mattr: the device attribute struct
1411 * @buf: a pointer to the output buffer
1413 * This routine is a callback routine for input read the IR protocol type(s).
1414 * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols.
1415 * It returns the protocol names of supported protocols.
1416 * The enabled protocols are printed in brackets.
1418 * dev->lock is taken to guard against races between
1419 * store_wakeup_protocols and show_wakeup_protocols.
1421 static ssize_t
show_wakeup_protocols(struct device
*device
,
1422 struct device_attribute
*mattr
,
1425 struct rc_dev
*dev
= to_rc_dev(device
);
1427 enum rc_proto enabled
;
1431 mutex_lock(&dev
->lock
);
1433 allowed
= dev
->allowed_wakeup_protocols
;
1434 enabled
= dev
->wakeup_protocol
;
1436 mutex_unlock(&dev
->lock
);
1438 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - %d\n",
1439 __func__
, (long long)allowed
, enabled
);
1441 for (i
= 0; i
< ARRAY_SIZE(protocols
); i
++) {
1442 if (allowed
& (1ULL << i
)) {
1444 tmp
+= sprintf(tmp
, "[%s] ", protocols
[i
].name
);
1446 tmp
+= sprintf(tmp
, "%s ", protocols
[i
].name
);
1454 return tmp
+ 1 - buf
;
1458 * store_wakeup_protocols() - changes the wakeup IR protocol(s)
1459 * @device: the device descriptor
1460 * @mattr: the device attribute struct
1461 * @buf: a pointer to the input buffer
1462 * @len: length of the input buffer
1464 * This routine is for changing the IR protocol type.
1465 * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols.
1466 * Returns @len on success or a negative error code.
1468 * dev->lock is taken to guard against races between
1469 * store_wakeup_protocols and show_wakeup_protocols.
1471 static ssize_t
store_wakeup_protocols(struct device
*device
,
1472 struct device_attribute
*mattr
,
1473 const char *buf
, size_t len
)
1475 struct rc_dev
*dev
= to_rc_dev(device
);
1476 enum rc_proto protocol
;
1481 mutex_lock(&dev
->lock
);
1483 allowed
= dev
->allowed_wakeup_protocols
;
1485 if (sysfs_streq(buf
, "none")) {
1486 protocol
= RC_PROTO_UNKNOWN
;
1488 for (i
= 0; i
< ARRAY_SIZE(protocols
); i
++) {
1489 if ((allowed
& (1ULL << i
)) &&
1490 sysfs_streq(buf
, protocols
[i
].name
)) {
1496 if (i
== ARRAY_SIZE(protocols
)) {
1501 if (dev
->encode_wakeup
) {
1502 u64 mask
= 1ULL << protocol
;
1504 ir_raw_load_modules(&mask
);
1512 if (dev
->wakeup_protocol
!= protocol
) {
1513 dev
->wakeup_protocol
= protocol
;
1514 IR_dprintk(1, "Wakeup protocol changed to %d\n", protocol
);
1516 if (protocol
== RC_PROTO_RC6_MCE
)
1517 dev
->scancode_wakeup_filter
.data
= 0x800f0000;
1519 dev
->scancode_wakeup_filter
.data
= 0;
1520 dev
->scancode_wakeup_filter
.mask
= 0;
1522 rc
= dev
->s_wakeup_filter(dev
, &dev
->scancode_wakeup_filter
);
1530 mutex_unlock(&dev
->lock
);
1534 static void rc_dev_release(struct device
*device
)
1536 struct rc_dev
*dev
= to_rc_dev(device
);
1541 #define ADD_HOTPLUG_VAR(fmt, val...) \
1543 int err = add_uevent_var(env, fmt, val); \
1548 static int rc_dev_uevent(struct device
*device
, struct kobj_uevent_env
*env
)
1550 struct rc_dev
*dev
= to_rc_dev(device
);
1552 if (dev
->rc_map
.name
)
1553 ADD_HOTPLUG_VAR("NAME=%s", dev
->rc_map
.name
);
1554 if (dev
->driver_name
)
1555 ADD_HOTPLUG_VAR("DRV_NAME=%s", dev
->driver_name
);
1556 if (dev
->device_name
)
1557 ADD_HOTPLUG_VAR("DEV_NAME=%s", dev
->device_name
);
1563 * Static device attribute struct with the sysfs attributes for IR's
1565 static struct device_attribute dev_attr_ro_protocols
=
1566 __ATTR(protocols
, 0444, show_protocols
, NULL
);
1567 static struct device_attribute dev_attr_rw_protocols
=
1568 __ATTR(protocols
, 0644, show_protocols
, store_protocols
);
1569 static DEVICE_ATTR(wakeup_protocols
, 0644, show_wakeup_protocols
,
1570 store_wakeup_protocols
);
1571 static RC_FILTER_ATTR(filter
, S_IRUGO
|S_IWUSR
,
1572 show_filter
, store_filter
, RC_FILTER_NORMAL
, false);
1573 static RC_FILTER_ATTR(filter_mask
, S_IRUGO
|S_IWUSR
,
1574 show_filter
, store_filter
, RC_FILTER_NORMAL
, true);
1575 static RC_FILTER_ATTR(wakeup_filter
, S_IRUGO
|S_IWUSR
,
1576 show_filter
, store_filter
, RC_FILTER_WAKEUP
, false);
1577 static RC_FILTER_ATTR(wakeup_filter_mask
, S_IRUGO
|S_IWUSR
,
1578 show_filter
, store_filter
, RC_FILTER_WAKEUP
, true);
1580 static struct attribute
*rc_dev_rw_protocol_attrs
[] = {
1581 &dev_attr_rw_protocols
.attr
,
1585 static const struct attribute_group rc_dev_rw_protocol_attr_grp
= {
1586 .attrs
= rc_dev_rw_protocol_attrs
,
1589 static struct attribute
*rc_dev_ro_protocol_attrs
[] = {
1590 &dev_attr_ro_protocols
.attr
,
1594 static const struct attribute_group rc_dev_ro_protocol_attr_grp
= {
1595 .attrs
= rc_dev_ro_protocol_attrs
,
1598 static struct attribute
*rc_dev_filter_attrs
[] = {
1599 &dev_attr_filter
.attr
.attr
,
1600 &dev_attr_filter_mask
.attr
.attr
,
1604 static const struct attribute_group rc_dev_filter_attr_grp
= {
1605 .attrs
= rc_dev_filter_attrs
,
1608 static struct attribute
*rc_dev_wakeup_filter_attrs
[] = {
1609 &dev_attr_wakeup_filter
.attr
.attr
,
1610 &dev_attr_wakeup_filter_mask
.attr
.attr
,
1611 &dev_attr_wakeup_protocols
.attr
,
1615 static const struct attribute_group rc_dev_wakeup_filter_attr_grp
= {
1616 .attrs
= rc_dev_wakeup_filter_attrs
,
1619 static const struct device_type rc_dev_type
= {
1620 .release
= rc_dev_release
,
1621 .uevent
= rc_dev_uevent
,
1624 struct rc_dev
*rc_allocate_device(enum rc_driver_type type
)
1628 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1632 if (type
!= RC_DRIVER_IR_RAW_TX
) {
1633 dev
->input_dev
= input_allocate_device();
1634 if (!dev
->input_dev
) {
1639 dev
->input_dev
->getkeycode
= ir_getkeycode
;
1640 dev
->input_dev
->setkeycode
= ir_setkeycode
;
1641 input_set_drvdata(dev
->input_dev
, dev
);
1643 timer_setup(&dev
->timer_keyup
, ir_timer_keyup
, 0);
1644 timer_setup(&dev
->timer_repeat
, ir_timer_repeat
, 0);
1646 spin_lock_init(&dev
->rc_map
.lock
);
1647 spin_lock_init(&dev
->keylock
);
1649 mutex_init(&dev
->lock
);
1651 dev
->dev
.type
= &rc_dev_type
;
1652 dev
->dev
.class = &rc_class
;
1653 device_initialize(&dev
->dev
);
1655 dev
->driver_type
= type
;
1657 __module_get(THIS_MODULE
);
1660 EXPORT_SYMBOL_GPL(rc_allocate_device
);
1662 void rc_free_device(struct rc_dev
*dev
)
1667 input_free_device(dev
->input_dev
);
1669 put_device(&dev
->dev
);
1671 /* kfree(dev) will be called by the callback function
1674 module_put(THIS_MODULE
);
1676 EXPORT_SYMBOL_GPL(rc_free_device
);
1678 static void devm_rc_alloc_release(struct device
*dev
, void *res
)
1680 rc_free_device(*(struct rc_dev
**)res
);
1683 struct rc_dev
*devm_rc_allocate_device(struct device
*dev
,
1684 enum rc_driver_type type
)
1686 struct rc_dev
**dr
, *rc
;
1688 dr
= devres_alloc(devm_rc_alloc_release
, sizeof(*dr
), GFP_KERNEL
);
1692 rc
= rc_allocate_device(type
);
1698 rc
->dev
.parent
= dev
;
1699 rc
->managed_alloc
= true;
1701 devres_add(dev
, dr
);
1705 EXPORT_SYMBOL_GPL(devm_rc_allocate_device
);
1707 static int rc_prepare_rx_device(struct rc_dev
*dev
)
1710 struct rc_map
*rc_map
;
1716 rc_map
= rc_map_get(dev
->map_name
);
1718 rc_map
= rc_map_get(RC_MAP_EMPTY
);
1719 if (!rc_map
|| !rc_map
->scan
|| rc_map
->size
== 0)
1722 rc
= ir_setkeytable(dev
, rc_map
);
1726 rc_proto
= BIT_ULL(rc_map
->rc_proto
);
1728 if (dev
->driver_type
== RC_DRIVER_SCANCODE
&& !dev
->change_protocol
)
1729 dev
->enabled_protocols
= dev
->allowed_protocols
;
1731 if (dev
->change_protocol
) {
1732 rc
= dev
->change_protocol(dev
, &rc_proto
);
1735 dev
->enabled_protocols
= rc_proto
;
1738 if (dev
->driver_type
== RC_DRIVER_IR_RAW
)
1739 ir_raw_load_modules(&rc_proto
);
1741 set_bit(EV_KEY
, dev
->input_dev
->evbit
);
1742 set_bit(EV_REP
, dev
->input_dev
->evbit
);
1743 set_bit(EV_MSC
, dev
->input_dev
->evbit
);
1744 set_bit(MSC_SCAN
, dev
->input_dev
->mscbit
);
1746 dev
->input_dev
->open
= ir_open
;
1748 dev
->input_dev
->close
= ir_close
;
1750 dev
->input_dev
->dev
.parent
= &dev
->dev
;
1751 memcpy(&dev
->input_dev
->id
, &dev
->input_id
, sizeof(dev
->input_id
));
1752 dev
->input_dev
->phys
= dev
->input_phys
;
1753 dev
->input_dev
->name
= dev
->device_name
;
1758 ir_free_table(&dev
->rc_map
);
1763 static int rc_setup_rx_device(struct rc_dev
*dev
)
1767 /* rc_open will be called here */
1768 rc
= input_register_device(dev
->input_dev
);
1773 * Default delay of 250ms is too short for some protocols, especially
1774 * since the timeout is currently set to 250ms. Increase it to 500ms,
1775 * to avoid wrong repetition of the keycodes. Note that this must be
1776 * set after the call to input_register_device().
1778 if (dev
->allowed_protocols
== RC_PROTO_BIT_CEC
)
1779 dev
->input_dev
->rep
[REP_DELAY
] = 0;
1781 dev
->input_dev
->rep
[REP_DELAY
] = 500;
1784 * As a repeat event on protocols like RC-5 and NEC take as long as
1785 * 110/114ms, using 33ms as a repeat period is not the right thing
1788 dev
->input_dev
->rep
[REP_PERIOD
] = 125;
1793 static void rc_free_rx_device(struct rc_dev
*dev
)
1798 if (dev
->input_dev
) {
1799 input_unregister_device(dev
->input_dev
);
1800 dev
->input_dev
= NULL
;
1803 ir_free_table(&dev
->rc_map
);
1806 int rc_register_device(struct rc_dev
*dev
)
1816 minor
= ida_simple_get(&rc_ida
, 0, RC_DEV_MAX
, GFP_KERNEL
);
1821 dev_set_name(&dev
->dev
, "rc%u", dev
->minor
);
1822 dev_set_drvdata(&dev
->dev
, dev
);
1824 dev
->dev
.groups
= dev
->sysfs_groups
;
1825 if (dev
->driver_type
== RC_DRIVER_SCANCODE
&& !dev
->change_protocol
)
1826 dev
->sysfs_groups
[attr
++] = &rc_dev_ro_protocol_attr_grp
;
1827 else if (dev
->driver_type
!= RC_DRIVER_IR_RAW_TX
)
1828 dev
->sysfs_groups
[attr
++] = &rc_dev_rw_protocol_attr_grp
;
1830 dev
->sysfs_groups
[attr
++] = &rc_dev_filter_attr_grp
;
1831 if (dev
->s_wakeup_filter
)
1832 dev
->sysfs_groups
[attr
++] = &rc_dev_wakeup_filter_attr_grp
;
1833 dev
->sysfs_groups
[attr
++] = NULL
;
1835 if (dev
->driver_type
== RC_DRIVER_IR_RAW
) {
1836 rc
= ir_raw_event_prepare(dev
);
1841 if (dev
->driver_type
!= RC_DRIVER_IR_RAW_TX
) {
1842 rc
= rc_prepare_rx_device(dev
);
1847 rc
= device_add(&dev
->dev
);
1851 path
= kobject_get_path(&dev
->dev
.kobj
, GFP_KERNEL
);
1852 dev_info(&dev
->dev
, "%s as %s\n",
1853 dev
->device_name
?: "Unspecified device", path
?: "N/A");
1856 if (dev
->driver_type
!= RC_DRIVER_IR_RAW_TX
) {
1857 rc
= rc_setup_rx_device(dev
);
1862 /* Ensure that the lirc kfifo is setup before we start the thread */
1863 if (dev
->allowed_protocols
!= RC_PROTO_BIT_CEC
) {
1864 rc
= ir_lirc_register(dev
);
1869 if (dev
->driver_type
== RC_DRIVER_IR_RAW
) {
1870 rc
= ir_raw_event_register(dev
);
1875 dev
->registered
= true;
1877 IR_dprintk(1, "Registered rc%u (driver: %s)\n",
1879 dev
->driver_name
? dev
->driver_name
: "unknown");
1884 if (dev
->allowed_protocols
!= RC_PROTO_BIT_CEC
)
1885 ir_lirc_unregister(dev
);
1887 rc_free_rx_device(dev
);
1889 device_del(&dev
->dev
);
1891 ir_free_table(&dev
->rc_map
);
1893 ir_raw_event_free(dev
);
1895 ida_simple_remove(&rc_ida
, minor
);
1898 EXPORT_SYMBOL_GPL(rc_register_device
);
1900 static void devm_rc_release(struct device
*dev
, void *res
)
1902 rc_unregister_device(*(struct rc_dev
**)res
);
1905 int devm_rc_register_device(struct device
*parent
, struct rc_dev
*dev
)
1910 dr
= devres_alloc(devm_rc_release
, sizeof(*dr
), GFP_KERNEL
);
1914 ret
= rc_register_device(dev
);
1921 devres_add(parent
, dr
);
1925 EXPORT_SYMBOL_GPL(devm_rc_register_device
);
1927 void rc_unregister_device(struct rc_dev
*dev
)
1932 del_timer_sync(&dev
->timer_keyup
);
1933 del_timer_sync(&dev
->timer_repeat
);
1935 if (dev
->driver_type
== RC_DRIVER_IR_RAW
)
1936 ir_raw_event_unregister(dev
);
1938 rc_free_rx_device(dev
);
1940 mutex_lock(&dev
->lock
);
1941 dev
->registered
= false;
1942 mutex_unlock(&dev
->lock
);
1945 * lirc device should be freed with dev->registered = false, so
1946 * that userspace polling will get notified.
1948 if (dev
->allowed_protocols
!= RC_PROTO_BIT_CEC
)
1949 ir_lirc_unregister(dev
);
1951 device_del(&dev
->dev
);
1953 ida_simple_remove(&rc_ida
, dev
->minor
);
1955 if (!dev
->managed_alloc
)
1956 rc_free_device(dev
);
1959 EXPORT_SYMBOL_GPL(rc_unregister_device
);
1962 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1965 static int __init
rc_core_init(void)
1967 int rc
= class_register(&rc_class
);
1969 pr_err("rc_core: unable to register rc class\n");
1973 rc
= lirc_dev_init();
1975 pr_err("rc_core: unable to init lirc\n");
1976 class_unregister(&rc_class
);
1980 led_trigger_register_simple("rc-feedback", &led_feedback
);
1981 rc_map_register(&empty_map
);
1986 static void __exit
rc_core_exit(void)
1989 class_unregister(&rc_class
);
1990 led_trigger_unregister_simple(led_feedback
);
1991 rc_map_unregister(&empty_map
);
1994 subsys_initcall(rc_core_init
);
1995 module_exit(rc_core_exit
);
1997 int rc_core_debug
; /* ir_debug level (0,1,2) */
1998 EXPORT_SYMBOL_GPL(rc_core_debug
);
1999 module_param_named(debug
, rc_core_debug
, int, 0644);
2001 MODULE_AUTHOR("Mauro Carvalho Chehab");
2002 MODULE_LICENSE("GPL v2");