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