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