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