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/spinlock.h>
19 #include <linux/delay.h>
20 #include <linux/input.h>
21 #include <linux/leds.h>
22 #include <linux/slab.h>
23 #include <linux/idr.h>
24 #include <linux/device.h>
25 #include <linux/module.h>
26 #include "rc-core-priv.h"
28 /* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */
29 #define IR_TAB_MIN_SIZE 256
30 #define IR_TAB_MAX_SIZE 8192
31 #define RC_DEV_MAX 256
35 unsigned int repeat_period
;
36 unsigned int scancode_bits
;
38 [RC_PROTO_UNKNOWN
] = { .name
= "unknown", .repeat_period
= 250 },
39 [RC_PROTO_OTHER
] = { .name
= "other", .repeat_period
= 250 },
40 [RC_PROTO_RC5
] = { .name
= "rc-5",
41 .scancode_bits
= 0x1f7f, .repeat_period
= 250 },
42 [RC_PROTO_RC5X_20
] = { .name
= "rc-5x-20",
43 .scancode_bits
= 0x1f7f3f, .repeat_period
= 250 },
44 [RC_PROTO_RC5_SZ
] = { .name
= "rc-5-sz",
45 .scancode_bits
= 0x2fff, .repeat_period
= 250 },
46 [RC_PROTO_JVC
] = { .name
= "jvc",
47 .scancode_bits
= 0xffff, .repeat_period
= 250 },
48 [RC_PROTO_SONY12
] = { .name
= "sony-12",
49 .scancode_bits
= 0x1f007f, .repeat_period
= 250 },
50 [RC_PROTO_SONY15
] = { .name
= "sony-15",
51 .scancode_bits
= 0xff007f, .repeat_period
= 250 },
52 [RC_PROTO_SONY20
] = { .name
= "sony-20",
53 .scancode_bits
= 0x1fff7f, .repeat_period
= 250 },
54 [RC_PROTO_NEC
] = { .name
= "nec",
55 .scancode_bits
= 0xffff, .repeat_period
= 250 },
56 [RC_PROTO_NECX
] = { .name
= "nec-x",
57 .scancode_bits
= 0xffffff, .repeat_period
= 250 },
58 [RC_PROTO_NEC32
] = { .name
= "nec-32",
59 .scancode_bits
= 0xffffffff, .repeat_period
= 250 },
60 [RC_PROTO_SANYO
] = { .name
= "sanyo",
61 .scancode_bits
= 0x1fffff, .repeat_period
= 250 },
62 [RC_PROTO_MCIR2_KBD
] = { .name
= "mcir2-kbd",
63 .scancode_bits
= 0xffff, .repeat_period
= 250 },
64 [RC_PROTO_MCIR2_MSE
] = { .name
= "mcir2-mse",
65 .scancode_bits
= 0x1fffff, .repeat_period
= 250 },
66 [RC_PROTO_RC6_0
] = { .name
= "rc-6-0",
67 .scancode_bits
= 0xffff, .repeat_period
= 250 },
68 [RC_PROTO_RC6_6A_20
] = { .name
= "rc-6-6a-20",
69 .scancode_bits
= 0xfffff, .repeat_period
= 250 },
70 [RC_PROTO_RC6_6A_24
] = { .name
= "rc-6-6a-24",
71 .scancode_bits
= 0xffffff, .repeat_period
= 250 },
72 [RC_PROTO_RC6_6A_32
] = { .name
= "rc-6-6a-32",
73 .scancode_bits
= 0xffffffff, .repeat_period
= 250 },
74 [RC_PROTO_RC6_MCE
] = { .name
= "rc-6-mce",
75 .scancode_bits
= 0xffff7fff, .repeat_period
= 250 },
76 [RC_PROTO_SHARP
] = { .name
= "sharp",
77 .scancode_bits
= 0x1fff, .repeat_period
= 250 },
78 [RC_PROTO_XMP
] = { .name
= "xmp", .repeat_period
= 250 },
79 [RC_PROTO_CEC
] = { .name
= "cec", .repeat_period
= 550 },
82 /* Used to keep track of known keymaps */
83 static LIST_HEAD(rc_map_list
);
84 static DEFINE_SPINLOCK(rc_map_lock
);
85 static struct led_trigger
*led_feedback
;
87 /* Used to keep track of rc devices */
88 static DEFINE_IDA(rc_ida
);
90 static struct rc_map_list
*seek_rc_map(const char *name
)
92 struct rc_map_list
*map
= NULL
;
94 spin_lock(&rc_map_lock
);
95 list_for_each_entry(map
, &rc_map_list
, list
) {
96 if (!strcmp(name
, map
->map
.name
)) {
97 spin_unlock(&rc_map_lock
);
101 spin_unlock(&rc_map_lock
);
106 struct rc_map
*rc_map_get(const char *name
)
109 struct rc_map_list
*map
;
111 map
= seek_rc_map(name
);
112 #ifdef CONFIG_MODULES
114 int rc
= request_module("%s", name
);
116 pr_err("Couldn't load IR keymap %s\n", name
);
119 msleep(20); /* Give some time for IR to register */
121 map
= seek_rc_map(name
);
125 pr_err("IR keymap %s not found\n", name
);
129 printk(KERN_INFO
"Registered IR keymap %s\n", map
->map
.name
);
133 EXPORT_SYMBOL_GPL(rc_map_get
);
135 int rc_map_register(struct rc_map_list
*map
)
137 spin_lock(&rc_map_lock
);
138 list_add_tail(&map
->list
, &rc_map_list
);
139 spin_unlock(&rc_map_lock
);
142 EXPORT_SYMBOL_GPL(rc_map_register
);
144 void rc_map_unregister(struct rc_map_list
*map
)
146 spin_lock(&rc_map_lock
);
147 list_del(&map
->list
);
148 spin_unlock(&rc_map_lock
);
150 EXPORT_SYMBOL_GPL(rc_map_unregister
);
153 static struct rc_map_table empty
[] = {
154 { 0x2a, KEY_COFFEE
},
157 static struct rc_map_list empty_map
= {
160 .size
= ARRAY_SIZE(empty
),
161 .rc_proto
= RC_PROTO_UNKNOWN
, /* Legacy IR type */
162 .name
= RC_MAP_EMPTY
,
167 * ir_create_table() - initializes a scancode table
168 * @rc_map: the rc_map to initialize
169 * @name: name to assign to the table
170 * @rc_proto: ir type to assign to the new table
171 * @size: initial size of the table
172 * @return: zero on success or a negative error code
174 * This routine will initialize the rc_map and will allocate
175 * memory to hold at least the specified number of elements.
177 static int ir_create_table(struct rc_map
*rc_map
,
178 const char *name
, u64 rc_proto
, size_t size
)
180 rc_map
->name
= kstrdup(name
, GFP_KERNEL
);
183 rc_map
->rc_proto
= rc_proto
;
184 rc_map
->alloc
= roundup_pow_of_two(size
* sizeof(struct rc_map_table
));
185 rc_map
->size
= rc_map
->alloc
/ sizeof(struct rc_map_table
);
186 rc_map
->scan
= kmalloc(rc_map
->alloc
, GFP_KERNEL
);
193 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
194 rc_map
->size
, rc_map
->alloc
);
199 * ir_free_table() - frees memory allocated by a scancode table
200 * @rc_map: the table whose mappings need to be freed
202 * This routine will free memory alloctaed for key mappings used by given
205 static void ir_free_table(struct rc_map
*rc_map
)
215 * ir_resize_table() - resizes a scancode table if necessary
216 * @rc_map: the rc_map to resize
217 * @gfp_flags: gfp flags to use when allocating memory
218 * @return: zero on success or a negative error code
220 * This routine will shrink the rc_map if it has lots of
221 * unused entries and grow it if it is full.
223 static int ir_resize_table(struct rc_map
*rc_map
, gfp_t gfp_flags
)
225 unsigned int oldalloc
= rc_map
->alloc
;
226 unsigned int newalloc
= oldalloc
;
227 struct rc_map_table
*oldscan
= rc_map
->scan
;
228 struct rc_map_table
*newscan
;
230 if (rc_map
->size
== rc_map
->len
) {
231 /* All entries in use -> grow keytable */
232 if (rc_map
->alloc
>= IR_TAB_MAX_SIZE
)
236 IR_dprintk(1, "Growing table to %u bytes\n", newalloc
);
239 if ((rc_map
->len
* 3 < rc_map
->size
) && (oldalloc
> IR_TAB_MIN_SIZE
)) {
240 /* Less than 1/3 of entries in use -> shrink keytable */
242 IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc
);
245 if (newalloc
== oldalloc
)
248 newscan
= kmalloc(newalloc
, gfp_flags
);
250 IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc
);
254 memcpy(newscan
, rc_map
->scan
, rc_map
->len
* sizeof(struct rc_map_table
));
255 rc_map
->scan
= newscan
;
256 rc_map
->alloc
= newalloc
;
257 rc_map
->size
= rc_map
->alloc
/ sizeof(struct rc_map_table
);
263 * ir_update_mapping() - set a keycode in the scancode->keycode table
264 * @dev: the struct rc_dev device descriptor
265 * @rc_map: scancode table to be adjusted
266 * @index: index of the mapping that needs to be updated
267 * @keycode: the desired keycode
268 * @return: previous keycode assigned to the mapping
270 * This routine is used to update scancode->keycode mapping at given
273 static unsigned int ir_update_mapping(struct rc_dev
*dev
,
274 struct rc_map
*rc_map
,
276 unsigned int new_keycode
)
278 int old_keycode
= rc_map
->scan
[index
].keycode
;
281 /* Did the user wish to remove the mapping? */
282 if (new_keycode
== KEY_RESERVED
|| new_keycode
== KEY_UNKNOWN
) {
283 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
284 index
, rc_map
->scan
[index
].scancode
);
286 memmove(&rc_map
->scan
[index
], &rc_map
->scan
[index
+ 1],
287 (rc_map
->len
- index
) * sizeof(struct rc_map_table
));
289 IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n",
291 old_keycode
== KEY_RESERVED
? "New" : "Replacing",
292 rc_map
->scan
[index
].scancode
, new_keycode
);
293 rc_map
->scan
[index
].keycode
= new_keycode
;
294 __set_bit(new_keycode
, dev
->input_dev
->keybit
);
297 if (old_keycode
!= KEY_RESERVED
) {
298 /* A previous mapping was updated... */
299 __clear_bit(old_keycode
, dev
->input_dev
->keybit
);
300 /* ... but another scancode might use the same keycode */
301 for (i
= 0; i
< rc_map
->len
; i
++) {
302 if (rc_map
->scan
[i
].keycode
== old_keycode
) {
303 __set_bit(old_keycode
, dev
->input_dev
->keybit
);
308 /* Possibly shrink the keytable, failure is not a problem */
309 ir_resize_table(rc_map
, GFP_ATOMIC
);
316 * ir_establish_scancode() - set a keycode in the scancode->keycode table
317 * @dev: the struct rc_dev device descriptor
318 * @rc_map: scancode table to be searched
319 * @scancode: the desired scancode
320 * @resize: controls whether we allowed to resize the table to
321 * accommodate not yet present scancodes
322 * @return: index of the mapping containing scancode in question
323 * or -1U in case of failure.
325 * This routine is used to locate given scancode in rc_map.
326 * If scancode is not yet present the routine will allocate a new slot
329 static unsigned int ir_establish_scancode(struct rc_dev
*dev
,
330 struct rc_map
*rc_map
,
331 unsigned int scancode
,
337 * Unfortunately, some hardware-based IR decoders don't provide
338 * all bits for the complete IR code. In general, they provide only
339 * the command part of the IR code. Yet, as it is possible to replace
340 * the provided IR with another one, it is needed to allow loading
341 * IR tables from other remotes. So, we support specifying a mask to
342 * indicate the valid bits of the scancodes.
344 if (dev
->scancode_mask
)
345 scancode
&= dev
->scancode_mask
;
347 /* First check if we already have a mapping for this ir command */
348 for (i
= 0; i
< rc_map
->len
; i
++) {
349 if (rc_map
->scan
[i
].scancode
== scancode
)
352 /* Keytable is sorted from lowest to highest scancode */
353 if (rc_map
->scan
[i
].scancode
>= scancode
)
357 /* No previous mapping found, we might need to grow the table */
358 if (rc_map
->size
== rc_map
->len
) {
359 if (!resize
|| ir_resize_table(rc_map
, GFP_ATOMIC
))
363 /* i is the proper index to insert our new keycode */
365 memmove(&rc_map
->scan
[i
+ 1], &rc_map
->scan
[i
],
366 (rc_map
->len
- i
) * sizeof(struct rc_map_table
));
367 rc_map
->scan
[i
].scancode
= scancode
;
368 rc_map
->scan
[i
].keycode
= KEY_RESERVED
;
375 * ir_setkeycode() - set a keycode in the scancode->keycode table
376 * @idev: the struct input_dev device descriptor
377 * @scancode: the desired scancode
379 * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
381 * This routine is used to handle evdev EVIOCSKEY ioctl.
383 static int ir_setkeycode(struct input_dev
*idev
,
384 const struct input_keymap_entry
*ke
,
385 unsigned int *old_keycode
)
387 struct rc_dev
*rdev
= input_get_drvdata(idev
);
388 struct rc_map
*rc_map
= &rdev
->rc_map
;
390 unsigned int scancode
;
394 spin_lock_irqsave(&rc_map
->lock
, flags
);
396 if (ke
->flags
& INPUT_KEYMAP_BY_INDEX
) {
398 if (index
>= rc_map
->len
) {
403 retval
= input_scancode_to_scalar(ke
, &scancode
);
407 index
= ir_establish_scancode(rdev
, rc_map
, scancode
, true);
408 if (index
>= rc_map
->len
) {
414 *old_keycode
= ir_update_mapping(rdev
, rc_map
, index
, ke
->keycode
);
417 spin_unlock_irqrestore(&rc_map
->lock
, flags
);
422 * ir_setkeytable() - sets several entries in the scancode->keycode table
423 * @dev: the struct rc_dev device descriptor
424 * @to: the struct rc_map to copy entries to
425 * @from: the struct rc_map to copy entries from
426 * @return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
428 * This routine is used to handle table initialization.
430 static int ir_setkeytable(struct rc_dev
*dev
,
431 const struct rc_map
*from
)
433 struct rc_map
*rc_map
= &dev
->rc_map
;
434 unsigned int i
, index
;
437 rc
= ir_create_table(rc_map
, from
->name
,
438 from
->rc_proto
, from
->size
);
442 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
443 rc_map
->size
, rc_map
->alloc
);
445 for (i
= 0; i
< from
->size
; i
++) {
446 index
= ir_establish_scancode(dev
, rc_map
,
447 from
->scan
[i
].scancode
, false);
448 if (index
>= rc_map
->len
) {
453 ir_update_mapping(dev
, rc_map
, index
,
454 from
->scan
[i
].keycode
);
458 ir_free_table(rc_map
);
464 * ir_lookup_by_scancode() - locate mapping by scancode
465 * @rc_map: the struct rc_map to search
466 * @scancode: scancode to look for in the table
467 * @return: index in the table, -1U if not found
469 * This routine performs binary search in RC keykeymap table for
472 static unsigned int ir_lookup_by_scancode(const struct rc_map
*rc_map
,
473 unsigned int scancode
)
476 int end
= rc_map
->len
- 1;
479 while (start
<= end
) {
480 mid
= (start
+ end
) / 2;
481 if (rc_map
->scan
[mid
].scancode
< scancode
)
483 else if (rc_map
->scan
[mid
].scancode
> scancode
)
493 * ir_getkeycode() - get a keycode from the scancode->keycode table
494 * @idev: the struct input_dev device descriptor
495 * @scancode: the desired scancode
496 * @keycode: used to return the keycode, if found, or KEY_RESERVED
497 * @return: always returns zero.
499 * This routine is used to handle evdev EVIOCGKEY ioctl.
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
556 * @return: the corresponding keycode, or KEY_RESERVED
558 * This routine is used by drivers which need to convert a scancode to a
559 * keycode. Normally it should not be used since drivers should have no
560 * interest in keycodes.
562 u32
rc_g_keycode_from_table(struct rc_dev
*dev
, u32 scancode
)
564 struct rc_map
*rc_map
= &dev
->rc_map
;
565 unsigned int keycode
;
569 spin_lock_irqsave(&rc_map
->lock
, flags
);
571 index
= ir_lookup_by_scancode(rc_map
, scancode
);
572 keycode
= index
< rc_map
->len
?
573 rc_map
->scan
[index
].keycode
: KEY_RESERVED
;
575 spin_unlock_irqrestore(&rc_map
->lock
, flags
);
577 if (keycode
!= KEY_RESERVED
)
578 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
579 dev
->device_name
, scancode
, keycode
);
583 EXPORT_SYMBOL_GPL(rc_g_keycode_from_table
);
586 * ir_do_keyup() - internal function to signal the release of a keypress
587 * @dev: the struct rc_dev descriptor of the device
588 * @sync: whether or not to call input_sync
590 * This function is used internally to release a keypress, it must be
591 * called with keylock held.
593 static void ir_do_keyup(struct rc_dev
*dev
, bool sync
)
595 if (!dev
->keypressed
)
598 IR_dprintk(1, "keyup key 0x%04x\n", dev
->last_keycode
);
599 input_report_key(dev
->input_dev
, dev
->last_keycode
, 0);
600 led_trigger_event(led_feedback
, LED_OFF
);
602 input_sync(dev
->input_dev
);
603 dev
->keypressed
= false;
607 * rc_keyup() - signals the release of a keypress
608 * @dev: the struct rc_dev descriptor of the device
610 * This routine is used to signal that a key has been released on the
613 void rc_keyup(struct rc_dev
*dev
)
617 spin_lock_irqsave(&dev
->keylock
, flags
);
618 ir_do_keyup(dev
, true);
619 spin_unlock_irqrestore(&dev
->keylock
, flags
);
621 EXPORT_SYMBOL_GPL(rc_keyup
);
624 * ir_timer_keyup() - generates a keyup event after a timeout
625 * @cookie: a pointer to the struct rc_dev for the device
627 * This routine will generate a keyup event some time after a keydown event
628 * is generated when no further activity has been detected.
630 static void ir_timer_keyup(unsigned long cookie
)
632 struct rc_dev
*dev
= (struct rc_dev
*)cookie
;
636 * ir->keyup_jiffies is used to prevent a race condition if a
637 * hardware interrupt occurs at this point and the keyup timer
638 * event is moved further into the future as a result.
640 * The timer will then be reactivated and this function called
641 * again in the future. We need to exit gracefully in that case
642 * to allow the input subsystem to do its auto-repeat magic or
643 * a keyup event might follow immediately after the keydown.
645 spin_lock_irqsave(&dev
->keylock
, flags
);
646 if (time_is_before_eq_jiffies(dev
->keyup_jiffies
))
647 ir_do_keyup(dev
, true);
648 spin_unlock_irqrestore(&dev
->keylock
, flags
);
652 * rc_repeat() - signals that a key is still pressed
653 * @dev: the struct rc_dev descriptor of the device
655 * This routine is used by IR decoders when a repeat message which does
656 * not include the necessary bits to reproduce the scancode has been
659 void rc_repeat(struct rc_dev
*dev
)
662 unsigned int timeout
= protocols
[dev
->last_protocol
].repeat_period
;
664 spin_lock_irqsave(&dev
->keylock
, flags
);
666 if (!dev
->keypressed
)
669 input_event(dev
->input_dev
, EV_MSC
, MSC_SCAN
, dev
->last_scancode
);
670 input_sync(dev
->input_dev
);
672 dev
->keyup_jiffies
= jiffies
+ msecs_to_jiffies(timeout
);
673 mod_timer(&dev
->timer_keyup
, dev
->keyup_jiffies
);
676 spin_unlock_irqrestore(&dev
->keylock
, flags
);
678 EXPORT_SYMBOL_GPL(rc_repeat
);
681 * ir_do_keydown() - internal function to process a keypress
682 * @dev: the struct rc_dev descriptor of the device
683 * @protocol: the protocol of the keypress
684 * @scancode: the scancode of the keypress
685 * @keycode: the keycode of the keypress
686 * @toggle: the toggle value of the keypress
688 * This function is used internally to register a keypress, it must be
689 * called with keylock held.
691 static void ir_do_keydown(struct rc_dev
*dev
, enum rc_proto protocol
,
692 u32 scancode
, u32 keycode
, u8 toggle
)
694 bool new_event
= (!dev
->keypressed
||
695 dev
->last_protocol
!= protocol
||
696 dev
->last_scancode
!= scancode
||
697 dev
->last_toggle
!= toggle
);
699 if (new_event
&& dev
->keypressed
)
700 ir_do_keyup(dev
, false);
702 input_event(dev
->input_dev
, EV_MSC
, MSC_SCAN
, scancode
);
704 if (new_event
&& keycode
!= KEY_RESERVED
) {
705 /* Register a keypress */
706 dev
->keypressed
= true;
707 dev
->last_protocol
= protocol
;
708 dev
->last_scancode
= scancode
;
709 dev
->last_toggle
= toggle
;
710 dev
->last_keycode
= keycode
;
712 IR_dprintk(1, "%s: key down event, key 0x%04x, protocol 0x%04x, scancode 0x%08x\n",
713 dev
->device_name
, keycode
, protocol
, scancode
);
714 input_report_key(dev
->input_dev
, keycode
, 1);
716 led_trigger_event(led_feedback
, LED_FULL
);
719 input_sync(dev
->input_dev
);
723 * rc_keydown() - generates input event for a key press
724 * @dev: the struct rc_dev descriptor of the device
725 * @protocol: the protocol for the keypress
726 * @scancode: the scancode for the keypress
727 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
728 * support toggle values, this should be set to zero)
730 * This routine is used to signal that a key has been pressed on the
733 void rc_keydown(struct rc_dev
*dev
, enum rc_proto protocol
, u32 scancode
,
737 u32 keycode
= rc_g_keycode_from_table(dev
, scancode
);
739 spin_lock_irqsave(&dev
->keylock
, flags
);
740 ir_do_keydown(dev
, protocol
, scancode
, keycode
, toggle
);
742 if (dev
->keypressed
) {
743 dev
->keyup_jiffies
= jiffies
+
744 msecs_to_jiffies(protocols
[protocol
].repeat_period
);
745 mod_timer(&dev
->timer_keyup
, dev
->keyup_jiffies
);
747 spin_unlock_irqrestore(&dev
->keylock
, flags
);
749 EXPORT_SYMBOL_GPL(rc_keydown
);
752 * rc_keydown_notimeout() - generates input event for a key press without
753 * an automatic keyup event at a later time
754 * @dev: the struct rc_dev descriptor of the device
755 * @protocol: the protocol for the keypress
756 * @scancode: the scancode for the keypress
757 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
758 * support toggle values, this should be set to zero)
760 * This routine is used to signal that a key has been pressed on the
761 * remote control. The driver must manually call rc_keyup() at a later stage.
763 void rc_keydown_notimeout(struct rc_dev
*dev
, enum rc_proto protocol
,
764 u32 scancode
, u8 toggle
)
767 u32 keycode
= rc_g_keycode_from_table(dev
, scancode
);
769 spin_lock_irqsave(&dev
->keylock
, flags
);
770 ir_do_keydown(dev
, protocol
, scancode
, keycode
, toggle
);
771 spin_unlock_irqrestore(&dev
->keylock
, flags
);
773 EXPORT_SYMBOL_GPL(rc_keydown_notimeout
);
776 * rc_validate_filter() - checks that the scancode and mask are valid and
777 * provides sensible defaults
778 * @dev: the struct rc_dev descriptor of the device
779 * @filter: the scancode and mask
780 * @return: 0 or -EINVAL if the filter is not valid
782 static int rc_validate_filter(struct rc_dev
*dev
,
783 struct rc_scancode_filter
*filter
)
785 u32 mask
, s
= filter
->data
;
786 enum rc_proto protocol
= dev
->wakeup_protocol
;
788 if (protocol
>= ARRAY_SIZE(protocols
))
791 mask
= protocols
[protocol
].scancode_bits
;
795 if ((((s
>> 16) ^ ~(s
>> 8)) & 0xff) == 0)
799 if ((((s
>> 24) ^ ~(s
>> 16)) & 0xff) == 0)
802 case RC_PROTO_RC6_MCE
:
803 if ((s
& 0xffff0000) != 0x800f0000)
806 case RC_PROTO_RC6_6A_32
:
807 if ((s
& 0xffff0000) == 0x800f0000)
814 filter
->data
&= mask
;
815 filter
->mask
&= mask
;
818 * If we have to raw encode the IR for wakeup, we cannot have a mask
820 if (dev
->encode_wakeup
&& filter
->mask
!= 0 && filter
->mask
!= mask
)
826 int rc_open(struct rc_dev
*rdev
)
833 mutex_lock(&rdev
->lock
);
835 if (!rdev
->users
++ && rdev
->open
!= NULL
)
836 rval
= rdev
->open(rdev
);
841 mutex_unlock(&rdev
->lock
);
845 EXPORT_SYMBOL_GPL(rc_open
);
847 static int ir_open(struct input_dev
*idev
)
849 struct rc_dev
*rdev
= input_get_drvdata(idev
);
851 return rc_open(rdev
);
854 void rc_close(struct rc_dev
*rdev
)
857 mutex_lock(&rdev
->lock
);
859 if (!--rdev
->users
&& rdev
->close
!= NULL
)
862 mutex_unlock(&rdev
->lock
);
865 EXPORT_SYMBOL_GPL(rc_close
);
867 static void ir_close(struct input_dev
*idev
)
869 struct rc_dev
*rdev
= input_get_drvdata(idev
);
873 /* class for /sys/class/rc */
874 static char *rc_devnode(struct device
*dev
, umode_t
*mode
)
876 return kasprintf(GFP_KERNEL
, "rc/%s", dev_name(dev
));
879 static struct class rc_class
= {
881 .devnode
= rc_devnode
,
885 * These are the protocol textual descriptions that are
886 * used by the sysfs protocols file. Note that the order
887 * of the entries is relevant.
889 static const struct {
892 const char *module_name
;
894 { RC_PROTO_BIT_NONE
, "none", NULL
},
895 { RC_PROTO_BIT_OTHER
, "other", NULL
},
896 { RC_PROTO_BIT_UNKNOWN
, "unknown", NULL
},
898 RC_PROTO_BIT_RC5X_20
, "rc-5", "ir-rc5-decoder" },
901 RC_PROTO_BIT_NEC32
, "nec", "ir-nec-decoder" },
902 { RC_PROTO_BIT_RC6_0
|
903 RC_PROTO_BIT_RC6_6A_20
|
904 RC_PROTO_BIT_RC6_6A_24
|
905 RC_PROTO_BIT_RC6_6A_32
|
906 RC_PROTO_BIT_RC6_MCE
, "rc-6", "ir-rc6-decoder" },
907 { RC_PROTO_BIT_JVC
, "jvc", "ir-jvc-decoder" },
908 { RC_PROTO_BIT_SONY12
|
909 RC_PROTO_BIT_SONY15
|
910 RC_PROTO_BIT_SONY20
, "sony", "ir-sony-decoder" },
911 { RC_PROTO_BIT_RC5_SZ
, "rc-5-sz", "ir-rc5-decoder" },
912 { RC_PROTO_BIT_SANYO
, "sanyo", "ir-sanyo-decoder" },
913 { RC_PROTO_BIT_SHARP
, "sharp", "ir-sharp-decoder" },
914 { RC_PROTO_BIT_MCIR2_KBD
|
915 RC_PROTO_BIT_MCIR2_MSE
, "mce_kbd", "ir-mce_kbd-decoder" },
916 { RC_PROTO_BIT_XMP
, "xmp", "ir-xmp-decoder" },
917 { RC_PROTO_BIT_CEC
, "cec", NULL
},
921 * struct rc_filter_attribute - Device attribute relating to a filter type.
922 * @attr: Device attribute.
923 * @type: Filter type.
924 * @mask: false for filter value, true for filter mask.
926 struct rc_filter_attribute
{
927 struct device_attribute attr
;
928 enum rc_filter_type type
;
931 #define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
933 #define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
934 struct rc_filter_attribute dev_attr_##_name = { \
935 .attr = __ATTR(_name, _mode, _show, _store), \
940 static bool lirc_is_present(void)
942 #if defined(CONFIG_LIRC_MODULE)
945 mutex_lock(&module_mutex
);
946 lirc
= find_module("lirc_dev");
947 mutex_unlock(&module_mutex
);
949 return lirc
? true : false;
950 #elif defined(CONFIG_LIRC)
958 * show_protocols() - shows the current IR protocol(s)
959 * @device: the device descriptor
960 * @mattr: the device attribute struct
961 * @buf: a pointer to the output buffer
963 * This routine is a callback routine for input read the IR protocol type(s).
964 * it is trigged by reading /sys/class/rc/rc?/protocols.
965 * It returns the protocol names of supported protocols.
966 * Enabled protocols are printed in brackets.
968 * dev->lock is taken to guard against races between
969 * store_protocols and show_protocols.
971 static ssize_t
show_protocols(struct device
*device
,
972 struct device_attribute
*mattr
, char *buf
)
974 struct rc_dev
*dev
= to_rc_dev(device
);
975 u64 allowed
, enabled
;
979 mutex_lock(&dev
->lock
);
981 enabled
= dev
->enabled_protocols
;
982 allowed
= dev
->allowed_protocols
;
983 if (dev
->raw
&& !allowed
)
984 allowed
= ir_raw_get_allowed_protocols();
986 mutex_unlock(&dev
->lock
);
988 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
989 __func__
, (long long)allowed
, (long long)enabled
);
991 for (i
= 0; i
< ARRAY_SIZE(proto_names
); i
++) {
992 if (allowed
& enabled
& proto_names
[i
].type
)
993 tmp
+= sprintf(tmp
, "[%s] ", proto_names
[i
].name
);
994 else if (allowed
& proto_names
[i
].type
)
995 tmp
+= sprintf(tmp
, "%s ", proto_names
[i
].name
);
997 if (allowed
& proto_names
[i
].type
)
998 allowed
&= ~proto_names
[i
].type
;
1001 if (dev
->driver_type
== RC_DRIVER_IR_RAW
&& lirc_is_present())
1002 tmp
+= sprintf(tmp
, "[lirc] ");
1008 return tmp
+ 1 - buf
;
1012 * parse_protocol_change() - parses a protocol change request
1013 * @protocols: pointer to the bitmask of current protocols
1014 * @buf: pointer to the buffer with a list of changes
1016 * Writing "+proto" will add a protocol to the protocol mask.
1017 * Writing "-proto" will remove a protocol from protocol mask.
1018 * Writing "proto" will enable only "proto".
1019 * Writing "none" will disable all protocols.
1020 * Returns the number of changes performed or a negative error code.
1022 static int parse_protocol_change(u64
*protocols
, const char *buf
)
1026 bool enable
, disable
;
1030 while ((tmp
= strsep((char **)&buf
, " \n")) != NULL
) {
1038 } else if (*tmp
== '-') {
1047 for (i
= 0; i
< ARRAY_SIZE(proto_names
); i
++) {
1048 if (!strcasecmp(tmp
, proto_names
[i
].name
)) {
1049 mask
= proto_names
[i
].type
;
1054 if (i
== ARRAY_SIZE(proto_names
)) {
1055 if (!strcasecmp(tmp
, "lirc"))
1058 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp
);
1068 *protocols
&= ~mask
;
1074 IR_dprintk(1, "Protocol not specified\n");
1081 static void ir_raw_load_modules(u64
*protocols
)
1086 for (i
= 0; i
< ARRAY_SIZE(proto_names
); i
++) {
1087 if (proto_names
[i
].type
== RC_PROTO_BIT_NONE
||
1088 proto_names
[i
].type
& (RC_PROTO_BIT_OTHER
|
1089 RC_PROTO_BIT_UNKNOWN
))
1092 available
= ir_raw_get_allowed_protocols();
1093 if (!(*protocols
& proto_names
[i
].type
& ~available
))
1096 if (!proto_names
[i
].module_name
) {
1097 pr_err("Can't enable IR protocol %s\n",
1098 proto_names
[i
].name
);
1099 *protocols
&= ~proto_names
[i
].type
;
1103 ret
= request_module("%s", proto_names
[i
].module_name
);
1105 pr_err("Couldn't load IR protocol module %s\n",
1106 proto_names
[i
].module_name
);
1107 *protocols
&= ~proto_names
[i
].type
;
1111 available
= ir_raw_get_allowed_protocols();
1112 if (!(*protocols
& proto_names
[i
].type
& ~available
))
1115 pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
1116 proto_names
[i
].module_name
,
1117 proto_names
[i
].name
);
1118 *protocols
&= ~proto_names
[i
].type
;
1123 * store_protocols() - changes the current/wakeup IR protocol(s)
1124 * @device: the device descriptor
1125 * @mattr: the device attribute struct
1126 * @buf: a pointer to the input buffer
1127 * @len: length of the input buffer
1129 * This routine is for changing the IR protocol type.
1130 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
1131 * See parse_protocol_change() for the valid commands.
1132 * Returns @len on success or a negative error code.
1134 * dev->lock is taken to guard against races between
1135 * store_protocols and show_protocols.
1137 static ssize_t
store_protocols(struct device
*device
,
1138 struct device_attribute
*mattr
,
1139 const char *buf
, size_t len
)
1141 struct rc_dev
*dev
= to_rc_dev(device
);
1142 u64
*current_protocols
;
1143 struct rc_scancode_filter
*filter
;
1144 u64 old_protocols
, new_protocols
;
1147 IR_dprintk(1, "Normal protocol change requested\n");
1148 current_protocols
= &dev
->enabled_protocols
;
1149 filter
= &dev
->scancode_filter
;
1151 if (!dev
->change_protocol
) {
1152 IR_dprintk(1, "Protocol switching not supported\n");
1156 mutex_lock(&dev
->lock
);
1158 old_protocols
= *current_protocols
;
1159 new_protocols
= old_protocols
;
1160 rc
= parse_protocol_change(&new_protocols
, buf
);
1164 rc
= dev
->change_protocol(dev
, &new_protocols
);
1166 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
1167 (long long)new_protocols
);
1171 if (dev
->driver_type
== RC_DRIVER_IR_RAW
)
1172 ir_raw_load_modules(&new_protocols
);
1174 if (new_protocols
!= old_protocols
) {
1175 *current_protocols
= new_protocols
;
1176 IR_dprintk(1, "Protocols changed to 0x%llx\n",
1177 (long long)new_protocols
);
1181 * If a protocol change was attempted the filter may need updating, even
1182 * if the actual protocol mask hasn't changed (since the driver may have
1183 * cleared the filter).
1184 * Try setting the same filter with the new protocol (if any).
1185 * Fall back to clearing the filter.
1187 if (dev
->s_filter
&& filter
->mask
) {
1189 rc
= dev
->s_filter(dev
, filter
);
1196 dev
->s_filter(dev
, filter
);
1203 mutex_unlock(&dev
->lock
);
1208 * show_filter() - shows the current scancode filter value or mask
1209 * @device: the device descriptor
1210 * @attr: the device attribute struct
1211 * @buf: a pointer to the output buffer
1213 * This routine is a callback routine to read a scancode filter value or mask.
1214 * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
1215 * It prints the current scancode filter value or mask of the appropriate filter
1216 * type in hexadecimal into @buf and returns the size of the buffer.
1218 * Bits of the filter value corresponding to set bits in the filter mask are
1219 * compared against input scancodes and non-matching scancodes are discarded.
1221 * dev->lock is taken to guard against races between
1222 * store_filter and show_filter.
1224 static ssize_t
show_filter(struct device
*device
,
1225 struct device_attribute
*attr
,
1228 struct rc_dev
*dev
= to_rc_dev(device
);
1229 struct rc_filter_attribute
*fattr
= to_rc_filter_attr(attr
);
1230 struct rc_scancode_filter
*filter
;
1233 mutex_lock(&dev
->lock
);
1235 if (fattr
->type
== RC_FILTER_NORMAL
)
1236 filter
= &dev
->scancode_filter
;
1238 filter
= &dev
->scancode_wakeup_filter
;
1244 mutex_unlock(&dev
->lock
);
1246 return sprintf(buf
, "%#x\n", val
);
1250 * store_filter() - changes the scancode filter value
1251 * @device: the device descriptor
1252 * @attr: the device attribute struct
1253 * @buf: a pointer to the input buffer
1254 * @len: length of the input buffer
1256 * This routine is for changing a scancode filter value or mask.
1257 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
1258 * Returns -EINVAL if an invalid filter value for the current protocol was
1259 * specified or if scancode filtering is not supported by the driver, otherwise
1262 * Bits of the filter value corresponding to set bits in the filter mask are
1263 * compared against input scancodes and non-matching scancodes are discarded.
1265 * dev->lock is taken to guard against races between
1266 * store_filter and show_filter.
1268 static ssize_t
store_filter(struct device
*device
,
1269 struct device_attribute
*attr
,
1270 const char *buf
, size_t len
)
1272 struct rc_dev
*dev
= to_rc_dev(device
);
1273 struct rc_filter_attribute
*fattr
= to_rc_filter_attr(attr
);
1274 struct rc_scancode_filter new_filter
, *filter
;
1277 int (*set_filter
)(struct rc_dev
*dev
, struct rc_scancode_filter
*filter
);
1279 ret
= kstrtoul(buf
, 0, &val
);
1283 if (fattr
->type
== RC_FILTER_NORMAL
) {
1284 set_filter
= dev
->s_filter
;
1285 filter
= &dev
->scancode_filter
;
1287 set_filter
= dev
->s_wakeup_filter
;
1288 filter
= &dev
->scancode_wakeup_filter
;
1294 mutex_lock(&dev
->lock
);
1296 new_filter
= *filter
;
1298 new_filter
.mask
= val
;
1300 new_filter
.data
= val
;
1302 if (fattr
->type
== RC_FILTER_WAKEUP
) {
1304 * Refuse to set a filter unless a protocol is enabled
1305 * and the filter is valid for that protocol
1307 if (dev
->wakeup_protocol
!= RC_PROTO_UNKNOWN
)
1308 ret
= rc_validate_filter(dev
, &new_filter
);
1316 if (fattr
->type
== RC_FILTER_NORMAL
&& !dev
->enabled_protocols
&&
1318 /* refuse to set a filter unless a protocol is enabled */
1323 ret
= set_filter(dev
, &new_filter
);
1327 *filter
= new_filter
;
1330 mutex_unlock(&dev
->lock
);
1331 return (ret
< 0) ? ret
: len
;
1335 * show_wakeup_protocols() - shows the wakeup IR protocol
1336 * @device: the device descriptor
1337 * @mattr: the device attribute struct
1338 * @buf: a pointer to the output buffer
1340 * This routine is a callback routine for input read the IR protocol type(s).
1341 * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols.
1342 * It returns the protocol names of supported protocols.
1343 * The enabled protocols are printed in brackets.
1345 * dev->lock is taken to guard against races between
1346 * store_wakeup_protocols and show_wakeup_protocols.
1348 static ssize_t
show_wakeup_protocols(struct device
*device
,
1349 struct device_attribute
*mattr
,
1352 struct rc_dev
*dev
= to_rc_dev(device
);
1354 enum rc_proto enabled
;
1358 mutex_lock(&dev
->lock
);
1360 allowed
= dev
->allowed_wakeup_protocols
;
1361 enabled
= dev
->wakeup_protocol
;
1363 mutex_unlock(&dev
->lock
);
1365 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - %d\n",
1366 __func__
, (long long)allowed
, enabled
);
1368 for (i
= 0; i
< ARRAY_SIZE(protocols
); i
++) {
1369 if (allowed
& (1ULL << i
)) {
1371 tmp
+= sprintf(tmp
, "[%s] ", protocols
[i
].name
);
1373 tmp
+= sprintf(tmp
, "%s ", protocols
[i
].name
);
1381 return tmp
+ 1 - buf
;
1385 * store_wakeup_protocols() - changes the wakeup IR protocol(s)
1386 * @device: the device descriptor
1387 * @mattr: the device attribute struct
1388 * @buf: a pointer to the input buffer
1389 * @len: length of the input buffer
1391 * This routine is for changing the IR protocol type.
1392 * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols.
1393 * Returns @len on success or a negative error code.
1395 * dev->lock is taken to guard against races between
1396 * store_wakeup_protocols and show_wakeup_protocols.
1398 static ssize_t
store_wakeup_protocols(struct device
*device
,
1399 struct device_attribute
*mattr
,
1400 const char *buf
, size_t len
)
1402 struct rc_dev
*dev
= to_rc_dev(device
);
1403 enum rc_proto protocol
;
1408 mutex_lock(&dev
->lock
);
1410 allowed
= dev
->allowed_wakeup_protocols
;
1412 if (sysfs_streq(buf
, "none")) {
1413 protocol
= RC_PROTO_UNKNOWN
;
1415 for (i
= 0; i
< ARRAY_SIZE(protocols
); i
++) {
1416 if ((allowed
& (1ULL << i
)) &&
1417 sysfs_streq(buf
, protocols
[i
].name
)) {
1423 if (i
== ARRAY_SIZE(protocols
)) {
1428 if (dev
->encode_wakeup
) {
1429 u64 mask
= 1ULL << protocol
;
1431 ir_raw_load_modules(&mask
);
1439 if (dev
->wakeup_protocol
!= protocol
) {
1440 dev
->wakeup_protocol
= protocol
;
1441 IR_dprintk(1, "Wakeup protocol changed to %d\n", protocol
);
1443 if (protocol
== RC_PROTO_RC6_MCE
)
1444 dev
->scancode_wakeup_filter
.data
= 0x800f0000;
1446 dev
->scancode_wakeup_filter
.data
= 0;
1447 dev
->scancode_wakeup_filter
.mask
= 0;
1449 rc
= dev
->s_wakeup_filter(dev
, &dev
->scancode_wakeup_filter
);
1457 mutex_unlock(&dev
->lock
);
1461 static void rc_dev_release(struct device
*device
)
1463 struct rc_dev
*dev
= to_rc_dev(device
);
1468 #define ADD_HOTPLUG_VAR(fmt, val...) \
1470 int err = add_uevent_var(env, fmt, val); \
1475 static int rc_dev_uevent(struct device
*device
, struct kobj_uevent_env
*env
)
1477 struct rc_dev
*dev
= to_rc_dev(device
);
1479 if (dev
->rc_map
.name
)
1480 ADD_HOTPLUG_VAR("NAME=%s", dev
->rc_map
.name
);
1481 if (dev
->driver_name
)
1482 ADD_HOTPLUG_VAR("DRV_NAME=%s", dev
->driver_name
);
1488 * Static device attribute struct with the sysfs attributes for IR's
1490 static DEVICE_ATTR(protocols
, 0644, show_protocols
, store_protocols
);
1491 static DEVICE_ATTR(wakeup_protocols
, 0644, show_wakeup_protocols
,
1492 store_wakeup_protocols
);
1493 static RC_FILTER_ATTR(filter
, S_IRUGO
|S_IWUSR
,
1494 show_filter
, store_filter
, RC_FILTER_NORMAL
, false);
1495 static RC_FILTER_ATTR(filter_mask
, S_IRUGO
|S_IWUSR
,
1496 show_filter
, store_filter
, RC_FILTER_NORMAL
, true);
1497 static RC_FILTER_ATTR(wakeup_filter
, S_IRUGO
|S_IWUSR
,
1498 show_filter
, store_filter
, RC_FILTER_WAKEUP
, false);
1499 static RC_FILTER_ATTR(wakeup_filter_mask
, S_IRUGO
|S_IWUSR
,
1500 show_filter
, store_filter
, RC_FILTER_WAKEUP
, true);
1502 static struct attribute
*rc_dev_protocol_attrs
[] = {
1503 &dev_attr_protocols
.attr
,
1507 static const struct attribute_group rc_dev_protocol_attr_grp
= {
1508 .attrs
= rc_dev_protocol_attrs
,
1511 static struct attribute
*rc_dev_filter_attrs
[] = {
1512 &dev_attr_filter
.attr
.attr
,
1513 &dev_attr_filter_mask
.attr
.attr
,
1517 static const struct attribute_group rc_dev_filter_attr_grp
= {
1518 .attrs
= rc_dev_filter_attrs
,
1521 static struct attribute
*rc_dev_wakeup_filter_attrs
[] = {
1522 &dev_attr_wakeup_filter
.attr
.attr
,
1523 &dev_attr_wakeup_filter_mask
.attr
.attr
,
1524 &dev_attr_wakeup_protocols
.attr
,
1528 static const struct attribute_group rc_dev_wakeup_filter_attr_grp
= {
1529 .attrs
= rc_dev_wakeup_filter_attrs
,
1532 static struct device_type rc_dev_type
= {
1533 .release
= rc_dev_release
,
1534 .uevent
= rc_dev_uevent
,
1537 struct rc_dev
*rc_allocate_device(enum rc_driver_type type
)
1541 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1545 if (type
!= RC_DRIVER_IR_RAW_TX
) {
1546 dev
->input_dev
= input_allocate_device();
1547 if (!dev
->input_dev
) {
1552 dev
->input_dev
->getkeycode
= ir_getkeycode
;
1553 dev
->input_dev
->setkeycode
= ir_setkeycode
;
1554 input_set_drvdata(dev
->input_dev
, dev
);
1556 setup_timer(&dev
->timer_keyup
, ir_timer_keyup
,
1557 (unsigned long)dev
);
1559 spin_lock_init(&dev
->rc_map
.lock
);
1560 spin_lock_init(&dev
->keylock
);
1562 mutex_init(&dev
->lock
);
1564 dev
->dev
.type
= &rc_dev_type
;
1565 dev
->dev
.class = &rc_class
;
1566 device_initialize(&dev
->dev
);
1568 dev
->driver_type
= type
;
1570 __module_get(THIS_MODULE
);
1573 EXPORT_SYMBOL_GPL(rc_allocate_device
);
1575 void rc_free_device(struct rc_dev
*dev
)
1580 input_free_device(dev
->input_dev
);
1582 put_device(&dev
->dev
);
1584 /* kfree(dev) will be called by the callback function
1587 module_put(THIS_MODULE
);
1589 EXPORT_SYMBOL_GPL(rc_free_device
);
1591 static void devm_rc_alloc_release(struct device
*dev
, void *res
)
1593 rc_free_device(*(struct rc_dev
**)res
);
1596 struct rc_dev
*devm_rc_allocate_device(struct device
*dev
,
1597 enum rc_driver_type type
)
1599 struct rc_dev
**dr
, *rc
;
1601 dr
= devres_alloc(devm_rc_alloc_release
, sizeof(*dr
), GFP_KERNEL
);
1605 rc
= rc_allocate_device(type
);
1611 rc
->dev
.parent
= dev
;
1612 rc
->managed_alloc
= true;
1614 devres_add(dev
, dr
);
1618 EXPORT_SYMBOL_GPL(devm_rc_allocate_device
);
1620 static int rc_prepare_rx_device(struct rc_dev
*dev
)
1623 struct rc_map
*rc_map
;
1629 rc_map
= rc_map_get(dev
->map_name
);
1631 rc_map
= rc_map_get(RC_MAP_EMPTY
);
1632 if (!rc_map
|| !rc_map
->scan
|| rc_map
->size
== 0)
1635 rc
= ir_setkeytable(dev
, rc_map
);
1639 rc_proto
= BIT_ULL(rc_map
->rc_proto
);
1641 if (dev
->change_protocol
) {
1642 rc
= dev
->change_protocol(dev
, &rc_proto
);
1645 dev
->enabled_protocols
= rc_proto
;
1648 if (dev
->driver_type
== RC_DRIVER_IR_RAW
)
1649 ir_raw_load_modules(&rc_proto
);
1651 set_bit(EV_KEY
, dev
->input_dev
->evbit
);
1652 set_bit(EV_REP
, dev
->input_dev
->evbit
);
1653 set_bit(EV_MSC
, dev
->input_dev
->evbit
);
1654 set_bit(MSC_SCAN
, dev
->input_dev
->mscbit
);
1656 dev
->input_dev
->open
= ir_open
;
1658 dev
->input_dev
->close
= ir_close
;
1660 dev
->input_dev
->dev
.parent
= &dev
->dev
;
1661 memcpy(&dev
->input_dev
->id
, &dev
->input_id
, sizeof(dev
->input_id
));
1662 dev
->input_dev
->phys
= dev
->input_phys
;
1663 dev
->input_dev
->name
= dev
->device_name
;
1668 ir_free_table(&dev
->rc_map
);
1673 static int rc_setup_rx_device(struct rc_dev
*dev
)
1677 /* rc_open will be called here */
1678 rc
= input_register_device(dev
->input_dev
);
1683 * Default delay of 250ms is too short for some protocols, especially
1684 * since the timeout is currently set to 250ms. Increase it to 500ms,
1685 * to avoid wrong repetition of the keycodes. Note that this must be
1686 * set after the call to input_register_device().
1688 dev
->input_dev
->rep
[REP_DELAY
] = 500;
1691 * As a repeat event on protocols like RC-5 and NEC take as long as
1692 * 110/114ms, using 33ms as a repeat period is not the right thing
1695 dev
->input_dev
->rep
[REP_PERIOD
] = 125;
1700 static void rc_free_rx_device(struct rc_dev
*dev
)
1705 if (dev
->input_dev
) {
1706 input_unregister_device(dev
->input_dev
);
1707 dev
->input_dev
= NULL
;
1710 ir_free_table(&dev
->rc_map
);
1713 int rc_register_device(struct rc_dev
*dev
)
1723 minor
= ida_simple_get(&rc_ida
, 0, RC_DEV_MAX
, GFP_KERNEL
);
1728 dev_set_name(&dev
->dev
, "rc%u", dev
->minor
);
1729 dev_set_drvdata(&dev
->dev
, dev
);
1731 dev
->dev
.groups
= dev
->sysfs_groups
;
1732 if (dev
->driver_type
!= RC_DRIVER_IR_RAW_TX
)
1733 dev
->sysfs_groups
[attr
++] = &rc_dev_protocol_attr_grp
;
1735 dev
->sysfs_groups
[attr
++] = &rc_dev_filter_attr_grp
;
1736 if (dev
->s_wakeup_filter
)
1737 dev
->sysfs_groups
[attr
++] = &rc_dev_wakeup_filter_attr_grp
;
1738 dev
->sysfs_groups
[attr
++] = NULL
;
1740 if (dev
->driver_type
== RC_DRIVER_IR_RAW
||
1741 dev
->driver_type
== RC_DRIVER_IR_RAW_TX
) {
1742 rc
= ir_raw_event_prepare(dev
);
1747 if (dev
->driver_type
!= RC_DRIVER_IR_RAW_TX
) {
1748 rc
= rc_prepare_rx_device(dev
);
1753 rc
= device_add(&dev
->dev
);
1757 path
= kobject_get_path(&dev
->dev
.kobj
, GFP_KERNEL
);
1758 dev_info(&dev
->dev
, "%s as %s\n",
1759 dev
->device_name
?: "Unspecified device", path
?: "N/A");
1762 if (dev
->driver_type
!= RC_DRIVER_IR_RAW_TX
) {
1763 rc
= rc_setup_rx_device(dev
);
1768 if (dev
->driver_type
== RC_DRIVER_IR_RAW
||
1769 dev
->driver_type
== RC_DRIVER_IR_RAW_TX
) {
1770 rc
= ir_raw_event_register(dev
);
1775 IR_dprintk(1, "Registered rc%u (driver: %s)\n",
1777 dev
->driver_name
? dev
->driver_name
: "unknown");
1782 rc_free_rx_device(dev
);
1784 device_del(&dev
->dev
);
1786 ir_free_table(&dev
->rc_map
);
1788 ir_raw_event_free(dev
);
1790 ida_simple_remove(&rc_ida
, minor
);
1793 EXPORT_SYMBOL_GPL(rc_register_device
);
1795 static void devm_rc_release(struct device
*dev
, void *res
)
1797 rc_unregister_device(*(struct rc_dev
**)res
);
1800 int devm_rc_register_device(struct device
*parent
, struct rc_dev
*dev
)
1805 dr
= devres_alloc(devm_rc_release
, sizeof(*dr
), GFP_KERNEL
);
1809 ret
= rc_register_device(dev
);
1816 devres_add(parent
, dr
);
1820 EXPORT_SYMBOL_GPL(devm_rc_register_device
);
1822 void rc_unregister_device(struct rc_dev
*dev
)
1827 del_timer_sync(&dev
->timer_keyup
);
1829 if (dev
->driver_type
== RC_DRIVER_IR_RAW
)
1830 ir_raw_event_unregister(dev
);
1832 rc_free_rx_device(dev
);
1834 device_del(&dev
->dev
);
1836 ida_simple_remove(&rc_ida
, dev
->minor
);
1838 if (!dev
->managed_alloc
)
1839 rc_free_device(dev
);
1842 EXPORT_SYMBOL_GPL(rc_unregister_device
);
1845 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1848 static int __init
rc_core_init(void)
1850 int rc
= class_register(&rc_class
);
1852 pr_err("rc_core: unable to register rc class\n");
1856 led_trigger_register_simple("rc-feedback", &led_feedback
);
1857 rc_map_register(&empty_map
);
1862 static void __exit
rc_core_exit(void)
1864 class_unregister(&rc_class
);
1865 led_trigger_unregister_simple(led_feedback
);
1866 rc_map_unregister(&empty_map
);
1869 subsys_initcall(rc_core_init
);
1870 module_exit(rc_core_exit
);
1872 int rc_core_debug
; /* ir_debug level (0,1,2) */
1873 EXPORT_SYMBOL_GPL(rc_core_debug
);
1874 module_param_named(debug
, rc_core_debug
, int, 0644);
1876 MODULE_AUTHOR("Mauro Carvalho Chehab");
1877 MODULE_LICENSE("GPL");