blob: 8e88dc8ea6c5ee99351dc44457ce3748dbc2d0c7 [file] [log] [blame]
Mauro Carvalho Chehab20835282017-12-01 08:47:08 -05001// SPDX-License-Identifier: GPL-2.0
2// rc-main.c - Remote Controller core module
3//
4// Copyright (C) 2009-2010 by Mauro Carvalho Chehab
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -03005
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -02006#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03008#include <media/rc-core.h>
Thomas Meyer8ca01d42017-09-08 13:33:36 -03009#include <linux/bsearch.h>
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030010#include <linux/spinlock.h>
11#include <linux/delay.h>
Mauro Carvalho Chehab882ead32009-12-29 10:37:38 -030012#include <linux/input.h>
Sean Young153a60b2013-07-30 19:00:01 -030013#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
David Härdemanfcb13092015-05-19 19:03:17 -030015#include <linux/idr.h>
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -030016#include <linux/device.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040017#include <linux/module.h>
Mauro Carvalho Chehabf62de672010-11-09 23:09:57 -030018#include "rc-core-priv.h"
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -030019
David Härdemanb3074c02010-04-02 15:58:28 -030020/* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */
21#define IR_TAB_MIN_SIZE 256
22#define IR_TAB_MAX_SIZE 8192
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -030023
Sean Youngd57ea872017-08-09 13:19:16 -040024static const struct {
25 const char *name;
26 unsigned int repeat_period;
27 unsigned int scancode_bits;
28} protocols[] = {
Sean Young28492252018-03-24 08:02:48 -040029 [RC_PROTO_UNKNOWN] = { .name = "unknown", .repeat_period = 125 },
30 [RC_PROTO_OTHER] = { .name = "other", .repeat_period = 125 },
Sean Young6d741bf2017-08-07 16:20:58 -040031 [RC_PROTO_RC5] = { .name = "rc-5",
Sean Young28492252018-03-24 08:02:48 -040032 .scancode_bits = 0x1f7f, .repeat_period = 114 },
Sean Young6d741bf2017-08-07 16:20:58 -040033 [RC_PROTO_RC5X_20] = { .name = "rc-5x-20",
Sean Young28492252018-03-24 08:02:48 -040034 .scancode_bits = 0x1f7f3f, .repeat_period = 114 },
Sean Young6d741bf2017-08-07 16:20:58 -040035 [RC_PROTO_RC5_SZ] = { .name = "rc-5-sz",
Sean Young28492252018-03-24 08:02:48 -040036 .scancode_bits = 0x2fff, .repeat_period = 114 },
Sean Young6d741bf2017-08-07 16:20:58 -040037 [RC_PROTO_JVC] = { .name = "jvc",
Sean Young28492252018-03-24 08:02:48 -040038 .scancode_bits = 0xffff, .repeat_period = 125 },
Sean Young6d741bf2017-08-07 16:20:58 -040039 [RC_PROTO_SONY12] = { .name = "sony-12",
Sean Young28492252018-03-24 08:02:48 -040040 .scancode_bits = 0x1f007f, .repeat_period = 100 },
Sean Young6d741bf2017-08-07 16:20:58 -040041 [RC_PROTO_SONY15] = { .name = "sony-15",
Sean Young28492252018-03-24 08:02:48 -040042 .scancode_bits = 0xff007f, .repeat_period = 100 },
Sean Young6d741bf2017-08-07 16:20:58 -040043 [RC_PROTO_SONY20] = { .name = "sony-20",
Sean Young28492252018-03-24 08:02:48 -040044 .scancode_bits = 0x1fff7f, .repeat_period = 100 },
Sean Young6d741bf2017-08-07 16:20:58 -040045 [RC_PROTO_NEC] = { .name = "nec",
Sean Young28492252018-03-24 08:02:48 -040046 .scancode_bits = 0xffff, .repeat_period = 110 },
Sean Young6d741bf2017-08-07 16:20:58 -040047 [RC_PROTO_NECX] = { .name = "nec-x",
Sean Young28492252018-03-24 08:02:48 -040048 .scancode_bits = 0xffffff, .repeat_period = 110 },
Sean Young6d741bf2017-08-07 16:20:58 -040049 [RC_PROTO_NEC32] = { .name = "nec-32",
Sean Young28492252018-03-24 08:02:48 -040050 .scancode_bits = 0xffffffff, .repeat_period = 110 },
Sean Young6d741bf2017-08-07 16:20:58 -040051 [RC_PROTO_SANYO] = { .name = "sanyo",
Sean Young28492252018-03-24 08:02:48 -040052 .scancode_bits = 0x1fffff, .repeat_period = 125 },
Sean Young6d741bf2017-08-07 16:20:58 -040053 [RC_PROTO_MCIR2_KBD] = { .name = "mcir2-kbd",
Sean Young53932762018-04-07 17:41:17 -040054 .scancode_bits = 0xffffff, .repeat_period = 100 },
Sean Young6d741bf2017-08-07 16:20:58 -040055 [RC_PROTO_MCIR2_MSE] = { .name = "mcir2-mse",
Sean Young28492252018-03-24 08:02:48 -040056 .scancode_bits = 0x1fffff, .repeat_period = 100 },
Sean Young6d741bf2017-08-07 16:20:58 -040057 [RC_PROTO_RC6_0] = { .name = "rc-6-0",
Sean Young28492252018-03-24 08:02:48 -040058 .scancode_bits = 0xffff, .repeat_period = 114 },
Sean Young6d741bf2017-08-07 16:20:58 -040059 [RC_PROTO_RC6_6A_20] = { .name = "rc-6-6a-20",
Sean Young28492252018-03-24 08:02:48 -040060 .scancode_bits = 0xfffff, .repeat_period = 114 },
Sean Young6d741bf2017-08-07 16:20:58 -040061 [RC_PROTO_RC6_6A_24] = { .name = "rc-6-6a-24",
Sean Young28492252018-03-24 08:02:48 -040062 .scancode_bits = 0xffffff, .repeat_period = 114 },
Sean Young6d741bf2017-08-07 16:20:58 -040063 [RC_PROTO_RC6_6A_32] = { .name = "rc-6-6a-32",
Sean Young28492252018-03-24 08:02:48 -040064 .scancode_bits = 0xffffffff, .repeat_period = 114 },
Sean Young6d741bf2017-08-07 16:20:58 -040065 [RC_PROTO_RC6_MCE] = { .name = "rc-6-mce",
Sean Young28492252018-03-24 08:02:48 -040066 .scancode_bits = 0xffff7fff, .repeat_period = 114 },
Sean Young6d741bf2017-08-07 16:20:58 -040067 [RC_PROTO_SHARP] = { .name = "sharp",
Sean Young28492252018-03-24 08:02:48 -040068 .scancode_bits = 0x1fff, .repeat_period = 125 },
69 [RC_PROTO_XMP] = { .name = "xmp", .repeat_period = 125 },
70 [RC_PROTO_CEC] = { .name = "cec", .repeat_period = 0 },
Sean Young447dcc0c2017-12-03 11:06:54 -050071 [RC_PROTO_IMON] = { .name = "imon",
Sean Young28492252018-03-24 08:02:48 -040072 .scancode_bits = 0x7fffffff, .repeat_period = 114 },
Patrick Lerda721074b2019-01-17 03:50:13 -050073 [RC_PROTO_RCMM12] = { .name = "rc-mm-12",
74 .scancode_bits = 0x00000fff, .repeat_period = 114 },
75 [RC_PROTO_RCMM24] = { .name = "rc-mm-24",
76 .scancode_bits = 0x00ffffff, .repeat_period = 114 },
77 [RC_PROTO_RCMM32] = { .name = "rc-mm-32",
78 .scancode_bits = 0xffffffff, .repeat_period = 114 },
Matthias Reichl17287692019-03-24 05:43:51 -040079 [RC_PROTO_XBOX_DVD] = { .name = "xbox-dvd", .repeat_period = 64 },
Sean Youngd57ea872017-08-09 13:19:16 -040080};
David Härdemana374fef2010-04-02 15:58:29 -030081
David Härdeman4c7b3552010-11-10 11:04:19 -030082/* Used to keep track of known keymaps */
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030083static LIST_HEAD(rc_map_list);
84static DEFINE_SPINLOCK(rc_map_lock);
Sean Young153a60b2013-07-30 19:00:01 -030085static struct led_trigger *led_feedback;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030086
David Härdemanfcb13092015-05-19 19:03:17 -030087/* Used to keep track of rc devices */
88static DEFINE_IDA(rc_ida);
89
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030090static struct rc_map_list *seek_rc_map(const char *name)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030091{
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030092 struct rc_map_list *map = NULL;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030093
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 Chehabd100e652010-11-17 15:56:53 -0300106struct rc_map *rc_map_get(const char *name)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300107{
108
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300109 struct rc_map_list *map;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300110
111 map = seek_rc_map(name);
Russell King2ff56fa2015-10-15 13:15:24 -0300112#ifdef CONFIG_MODULES
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300113 if (!map) {
Kees Cook8ea54882014-03-11 17:25:53 -0300114 int rc = request_module("%s", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300115 if (rc < 0) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -0200116 pr_err("Couldn't load IR keymap %s\n", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300117 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 Chehabd3d96822016-10-20 15:04:39 -0200125 pr_err("IR keymap %s not found\n", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300126 return NULL;
127 }
128
129 printk(KERN_INFO "Registered IR keymap %s\n", map->map.name);
130
131 return &map->map;
132}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300133EXPORT_SYMBOL_GPL(rc_map_get);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300134
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300135int rc_map_register(struct rc_map_list *map)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300136{
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 Chehabd100e652010-11-17 15:56:53 -0300142EXPORT_SYMBOL_GPL(rc_map_register);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300143
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300144void rc_map_unregister(struct rc_map_list *map)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300145{
146 spin_lock(&rc_map_lock);
147 list_del(&map->list);
148 spin_unlock(&rc_map_lock);
149}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300150EXPORT_SYMBOL_GPL(rc_map_unregister);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300151
152
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300153static struct rc_map_table empty[] = {
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300154 { 0x2a, KEY_COFFEE },
155};
156
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300157static struct rc_map_list empty_map = {
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300158 .map = {
Sean Young6d741bf2017-08-07 16:20:58 -0400159 .scan = empty,
160 .size = ARRAY_SIZE(empty),
161 .rc_proto = RC_PROTO_UNKNOWN, /* Legacy IR type */
162 .name = RC_MAP_EMPTY,
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300163 }
164};
165
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300166/**
Sean Younge6c6d7d2020-01-17 17:46:36 +0100167 * scancode_to_u64() - converts scancode in &struct input_keymap_entry
168 * @ke: keymap entry containing scancode to be converted.
169 * @scancode: pointer to the location where converted scancode should
170 * be stored.
171 *
172 * This function is a version of input_scancode_to_scalar specialized for
173 * rc-core.
174 */
175static int scancode_to_u64(const struct input_keymap_entry *ke, u64 *scancode)
176{
177 switch (ke->len) {
178 case 1:
179 *scancode = *((u8 *)ke->scancode);
180 break;
181
182 case 2:
183 *scancode = *((u16 *)ke->scancode);
184 break;
185
186 case 4:
187 *scancode = *((u32 *)ke->scancode);
188 break;
189
190 case 8:
191 *scancode = *((u64 *)ke->scancode);
192 break;
193
194 default:
195 return -EINVAL;
196 }
197
198 return 0;
199}
200
201/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700202 * ir_create_table() - initializes a scancode table
Sean Young1f17f682018-02-12 07:27:50 -0500203 * @dev: the rc_dev device
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300204 * @rc_map: the rc_map to initialize
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700205 * @name: name to assign to the table
Sean Young6d741bf2017-08-07 16:20:58 -0400206 * @rc_proto: ir type to assign to the new table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700207 * @size: initial size of the table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700208 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300209 * This routine will initialize the rc_map and will allocate
David Härdemand8b4b582010-10-29 16:08:23 -0300210 * memory to hold at least the specified number of elements.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500211 *
212 * return: zero on success or a negative error code
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700213 */
Sean Young1f17f682018-02-12 07:27:50 -0500214static int ir_create_table(struct rc_dev *dev, struct rc_map *rc_map,
Sean Young6d741bf2017-08-07 16:20:58 -0400215 const char *name, u64 rc_proto, size_t size)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700216{
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300217 rc_map->name = kstrdup(name, GFP_KERNEL);
218 if (!rc_map->name)
219 return -ENOMEM;
Sean Young6d741bf2017-08-07 16:20:58 -0400220 rc_map->rc_proto = rc_proto;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300221 rc_map->alloc = roundup_pow_of_two(size * sizeof(struct rc_map_table));
222 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300223 rc_map->scan = kmalloc(rc_map->alloc, GFP_KERNEL);
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300224 if (!rc_map->scan) {
225 kfree(rc_map->name);
226 rc_map->name = NULL;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700227 return -ENOMEM;
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300228 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700229
Sean Young1f17f682018-02-12 07:27:50 -0500230 dev_dbg(&dev->dev, "Allocated space for %u keycode entries (%u bytes)\n",
231 rc_map->size, rc_map->alloc);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700232 return 0;
233}
234
235/**
236 * ir_free_table() - frees memory allocated by a scancode table
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300237 * @rc_map: the table whose mappings need to be freed
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700238 *
239 * This routine will free memory alloctaed for key mappings used by given
240 * scancode table.
241 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300242static void ir_free_table(struct rc_map *rc_map)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700243{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300244 rc_map->size = 0;
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300245 kfree(rc_map->name);
Max Kellermannc183d352016-08-09 18:32:06 -0300246 rc_map->name = NULL;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300247 kfree(rc_map->scan);
248 rc_map->scan = NULL;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700249}
250
251/**
David Härdemanb3074c02010-04-02 15:58:28 -0300252 * ir_resize_table() - resizes a scancode table if necessary
Sean Young1f17f682018-02-12 07:27:50 -0500253 * @dev: the rc_dev device
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300254 * @rc_map: the rc_map to resize
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700255 * @gfp_flags: gfp flags to use when allocating memory
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300256 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300257 * This routine will shrink the rc_map if it has lots of
David Härdemanb3074c02010-04-02 15:58:28 -0300258 * unused entries and grow it if it is full.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500259 *
260 * return: zero on success or a negative error code
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300261 */
Sean Young1f17f682018-02-12 07:27:50 -0500262static int ir_resize_table(struct rc_dev *dev, struct rc_map *rc_map,
263 gfp_t gfp_flags)
David Härdemanb3074c02010-04-02 15:58:28 -0300264{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300265 unsigned int oldalloc = rc_map->alloc;
David Härdemanb3074c02010-04-02 15:58:28 -0300266 unsigned int newalloc = oldalloc;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300267 struct rc_map_table *oldscan = rc_map->scan;
268 struct rc_map_table *newscan;
David Härdemanb3074c02010-04-02 15:58:28 -0300269
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300270 if (rc_map->size == rc_map->len) {
David Härdemanb3074c02010-04-02 15:58:28 -0300271 /* All entries in use -> grow keytable */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300272 if (rc_map->alloc >= IR_TAB_MAX_SIZE)
David Härdemanb3074c02010-04-02 15:58:28 -0300273 return -ENOMEM;
274
275 newalloc *= 2;
Sean Young1f17f682018-02-12 07:27:50 -0500276 dev_dbg(&dev->dev, "Growing table to %u bytes\n", newalloc);
David Härdemanb3074c02010-04-02 15:58:28 -0300277 }
278
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300279 if ((rc_map->len * 3 < rc_map->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
David Härdemanb3074c02010-04-02 15:58:28 -0300280 /* Less than 1/3 of entries in use -> shrink keytable */
281 newalloc /= 2;
Sean Young1f17f682018-02-12 07:27:50 -0500282 dev_dbg(&dev->dev, "Shrinking table to %u bytes\n", newalloc);
David Härdemanb3074c02010-04-02 15:58:28 -0300283 }
284
285 if (newalloc == oldalloc)
286 return 0;
287
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700288 newscan = kmalloc(newalloc, gfp_flags);
Sean Young1f17f682018-02-12 07:27:50 -0500289 if (!newscan)
David Härdemanb3074c02010-04-02 15:58:28 -0300290 return -ENOMEM;
David Härdemanb3074c02010-04-02 15:58:28 -0300291
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300292 memcpy(newscan, rc_map->scan, rc_map->len * sizeof(struct rc_map_table));
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300293 rc_map->scan = newscan;
294 rc_map->alloc = newalloc;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300295 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
David Härdemanb3074c02010-04-02 15:58:28 -0300296 kfree(oldscan);
297 return 0;
298}
299
300/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700301 * ir_update_mapping() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300302 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300303 * @rc_map: scancode table to be adjusted
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700304 * @index: index of the mapping that needs to be updated
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500305 * @new_keycode: the desired keycode
David Härdemanb3074c02010-04-02 15:58:28 -0300306 *
David Härdemand8b4b582010-10-29 16:08:23 -0300307 * This routine is used to update scancode->keycode mapping at given
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700308 * position.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500309 *
310 * return: previous keycode assigned to the mapping
311 *
David Härdemanb3074c02010-04-02 15:58:28 -0300312 */
David Härdemand8b4b582010-10-29 16:08:23 -0300313static unsigned int ir_update_mapping(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300314 struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700315 unsigned int index,
316 unsigned int new_keycode)
317{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300318 int old_keycode = rc_map->scan[index].keycode;
Sean Young05f0eda2019-02-22 04:08:05 -0500319 int i;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700320
321 /* Did the user wish to remove the mapping? */
322 if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
Sean Younge6c6d7d2020-01-17 17:46:36 +0100323 dev_dbg(&dev->dev, "#%d: Deleting scan 0x%04llx\n",
Sean Young1f17f682018-02-12 07:27:50 -0500324 index, rc_map->scan[index].scancode);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300325 rc_map->len--;
326 memmove(&rc_map->scan[index], &rc_map->scan[index+ 1],
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300327 (rc_map->len - index) * sizeof(struct rc_map_table));
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700328 } else {
Sean Younge6c6d7d2020-01-17 17:46:36 +0100329 dev_dbg(&dev->dev, "#%d: %s scan 0x%04llx with key 0x%04x\n",
Sean Young1f17f682018-02-12 07:27:50 -0500330 index,
331 old_keycode == KEY_RESERVED ? "New" : "Replacing",
332 rc_map->scan[index].scancode, new_keycode);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300333 rc_map->scan[index].keycode = new_keycode;
Sean Young05f0eda2019-02-22 04:08:05 -0500334 __set_bit(new_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700335 }
336
337 if (old_keycode != KEY_RESERVED) {
Sean Young05f0eda2019-02-22 04:08:05 -0500338 /* A previous mapping was updated... */
339 __clear_bit(old_keycode, dev->input_dev->keybit);
340 /* ... but another scancode might use the same keycode */
341 for (i = 0; i < rc_map->len; i++) {
342 if (rc_map->scan[i].keycode == old_keycode) {
343 __set_bit(old_keycode, dev->input_dev->keybit);
344 break;
345 }
346 }
347
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700348 /* Possibly shrink the keytable, failure is not a problem */
Sean Young1f17f682018-02-12 07:27:50 -0500349 ir_resize_table(dev, rc_map, GFP_ATOMIC);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700350 }
351
352 return old_keycode;
353}
354
355/**
David Härdeman4c7b3552010-11-10 11:04:19 -0300356 * ir_establish_scancode() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300357 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300358 * @rc_map: scancode table to be searched
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700359 * @scancode: the desired scancode
360 * @resize: controls whether we allowed to resize the table to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300361 * accommodate not yet present scancodes
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700362 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300363 * This routine is used to locate given scancode in rc_map.
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700364 * If scancode is not yet present the routine will allocate a new slot
365 * for it.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500366 *
367 * return: index of the mapping containing scancode in question
368 * or -1U in case of failure.
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700369 */
David Härdemand8b4b582010-10-29 16:08:23 -0300370static unsigned int ir_establish_scancode(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300371 struct rc_map *rc_map,
Sean Younge6c6d7d2020-01-17 17:46:36 +0100372 u64 scancode, bool resize)
David Härdemanb3074c02010-04-02 15:58:28 -0300373{
374 unsigned int i;
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300375
376 /*
377 * Unfortunately, some hardware-based IR decoders don't provide
378 * all bits for the complete IR code. In general, they provide only
379 * the command part of the IR code. Yet, as it is possible to replace
380 * the provided IR with another one, it is needed to allow loading
David Härdemand8b4b582010-10-29 16:08:23 -0300381 * IR tables from other remotes. So, we support specifying a mask to
382 * indicate the valid bits of the scancodes.
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300383 */
David Härdeman9d2f1d32014-04-03 20:32:26 -0300384 if (dev->scancode_mask)
385 scancode &= dev->scancode_mask;
David Härdemanb3074c02010-04-02 15:58:28 -0300386
387 /* First check if we already have a mapping for this ir command */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300388 for (i = 0; i < rc_map->len; i++) {
389 if (rc_map->scan[i].scancode == scancode)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700390 return i;
391
David Härdemanb3074c02010-04-02 15:58:28 -0300392 /* Keytable is sorted from lowest to highest scancode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300393 if (rc_map->scan[i].scancode >= scancode)
David Härdemanb3074c02010-04-02 15:58:28 -0300394 break;
David Härdemanb3074c02010-04-02 15:58:28 -0300395 }
396
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700397 /* No previous mapping found, we might need to grow the table */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300398 if (rc_map->size == rc_map->len) {
Sean Young1f17f682018-02-12 07:27:50 -0500399 if (!resize || ir_resize_table(dev, rc_map, GFP_ATOMIC))
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700400 return -1U;
401 }
David Härdemanb3074c02010-04-02 15:58:28 -0300402
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700403 /* i is the proper index to insert our new keycode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300404 if (i < rc_map->len)
405 memmove(&rc_map->scan[i + 1], &rc_map->scan[i],
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300406 (rc_map->len - i) * sizeof(struct rc_map_table));
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300407 rc_map->scan[i].scancode = scancode;
408 rc_map->scan[i].keycode = KEY_RESERVED;
409 rc_map->len++;
David Härdemanb3074c02010-04-02 15:58:28 -0300410
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700411 return i;
David Härdemanb3074c02010-04-02 15:58:28 -0300412}
413
414/**
415 * ir_setkeycode() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300416 * @idev: the struct input_dev device descriptor
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500417 * @ke: Input keymap entry
418 * @old_keycode: result
David Härdemanb3074c02010-04-02 15:58:28 -0300419 *
420 * This routine is used to handle evdev EVIOCSKEY ioctl.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500421 *
422 * return: -EINVAL if the keycode could not be inserted, otherwise zero.
David Härdemanb3074c02010-04-02 15:58:28 -0300423 */
David Härdemand8b4b582010-10-29 16:08:23 -0300424static int ir_setkeycode(struct input_dev *idev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700425 const struct input_keymap_entry *ke,
426 unsigned int *old_keycode)
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300427{
David Härdemand8b4b582010-10-29 16:08:23 -0300428 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300429 struct rc_map *rc_map = &rdev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700430 unsigned int index;
Sean Younge6c6d7d2020-01-17 17:46:36 +0100431 u64 scancode;
Mauro Carvalho Chehabdea8a392010-11-29 07:46:13 -0300432 int retval = 0;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700433 unsigned long flags;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300434
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300435 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700436
437 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
438 index = ke->index;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300439 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700440 retval = -EINVAL;
441 goto out;
442 }
443 } else {
Sean Younge6c6d7d2020-01-17 17:46:36 +0100444 retval = scancode_to_u64(ke, &scancode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700445 if (retval)
446 goto out;
447
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300448 index = ir_establish_scancode(rdev, rc_map, scancode, true);
449 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700450 retval = -ENOMEM;
451 goto out;
452 }
453 }
454
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300455 *old_keycode = ir_update_mapping(rdev, rc_map, index, ke->keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700456
457out:
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300458 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700459 return retval;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300460}
461
462/**
David Härdemanb3074c02010-04-02 15:58:28 -0300463 * ir_setkeytable() - sets several entries in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300464 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300465 * @from: the struct rc_map to copy entries from
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300466 *
David Härdemanb3074c02010-04-02 15:58:28 -0300467 * This routine is used to handle table initialization.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500468 *
469 * return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300470 */
Sean Younge6c6d7d2020-01-17 17:46:36 +0100471static int ir_setkeytable(struct rc_dev *dev, const struct rc_map *from)
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300472{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300473 struct rc_map *rc_map = &dev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700474 unsigned int i, index;
475 int rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300476
Sean Young1f17f682018-02-12 07:27:50 -0500477 rc = ir_create_table(dev, rc_map, from->name, from->rc_proto,
478 from->size);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700479 if (rc)
480 return rc;
481
David Härdemanb3074c02010-04-02 15:58:28 -0300482 for (i = 0; i < from->size; i++) {
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300483 index = ir_establish_scancode(dev, rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700484 from->scan[i].scancode, false);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300485 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700486 rc = -ENOMEM;
David Härdemanb3074c02010-04-02 15:58:28 -0300487 break;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700488 }
489
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300490 ir_update_mapping(dev, rc_map, index,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700491 from->scan[i].keycode);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300492 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700493
494 if (rc)
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300495 ir_free_table(rc_map);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700496
David Härdemanb3074c02010-04-02 15:58:28 -0300497 return rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300498}
499
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300500static int rc_map_cmp(const void *key, const void *elt)
501{
Sean Younge6c6d7d2020-01-17 17:46:36 +0100502 const u64 *scancode = key;
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300503 const struct rc_map_table *e = elt;
504
505 if (*scancode < e->scancode)
506 return -1;
507 else if (*scancode > e->scancode)
508 return 1;
509 return 0;
510}
511
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300512/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700513 * ir_lookup_by_scancode() - locate mapping by scancode
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300514 * @rc_map: the struct rc_map to search
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700515 * @scancode: scancode to look for in the table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700516 *
517 * This routine performs binary search in RC keykeymap table for
518 * given scancode.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500519 *
520 * return: index in the table, -1U if not found
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700521 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300522static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map,
Sean Younge6c6d7d2020-01-17 17:46:36 +0100523 u64 scancode)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700524{
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300525 struct rc_map_table *res;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700526
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300527 res = bsearch(&scancode, rc_map->scan, rc_map->len,
528 sizeof(struct rc_map_table), rc_map_cmp);
529 if (!res)
530 return -1U;
531 else
532 return res - rc_map->scan;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700533}
534
535/**
David Härdemanb3074c02010-04-02 15:58:28 -0300536 * ir_getkeycode() - get a keycode from the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300537 * @idev: the struct input_dev device descriptor
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500538 * @ke: Input keymap entry
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300539 *
540 * This routine is used to handle evdev EVIOCGKEY ioctl.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500541 *
542 * return: always returns zero.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300543 */
David Härdemand8b4b582010-10-29 16:08:23 -0300544static int ir_getkeycode(struct input_dev *idev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700545 struct input_keymap_entry *ke)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300546{
David Härdemand8b4b582010-10-29 16:08:23 -0300547 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300548 struct rc_map *rc_map = &rdev->rc_map;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300549 struct rc_map_table *entry;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700550 unsigned long flags;
551 unsigned int index;
Sean Younge6c6d7d2020-01-17 17:46:36 +0100552 u64 scancode;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700553 int retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300554
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300555 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700556
557 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
558 index = ke->index;
559 } else {
Sean Younge6c6d7d2020-01-17 17:46:36 +0100560 retval = scancode_to_u64(ke, &scancode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700561 if (retval)
562 goto out;
563
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300564 index = ir_lookup_by_scancode(rc_map, scancode);
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300565 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700566
Dmitry Torokhov54e74b82011-01-28 23:33:29 -0800567 if (index < rc_map->len) {
568 entry = &rc_map->scan[index];
569
570 ke->index = index;
571 ke->keycode = entry->keycode;
572 ke->len = sizeof(entry->scancode);
573 memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
Dmitry Torokhov54e74b82011-01-28 23:33:29 -0800574 } else if (!(ke->flags & INPUT_KEYMAP_BY_INDEX)) {
575 /*
576 * We do not really know the valid range of scancodes
577 * so let's respond with KEY_RESERVED to anything we
578 * do not have mapping for [yet].
579 */
580 ke->index = index;
581 ke->keycode = KEY_RESERVED;
582 } else {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700583 retval = -EINVAL;
584 goto out;
585 }
586
Dmitry Torokhov47c5ba52010-10-31 15:18:42 -0700587 retval = 0;
588
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700589out:
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300590 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700591 return retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300592}
593
594/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300595 * rc_g_keycode_from_table() - gets the keycode that corresponds to a scancode
David Härdemand8b4b582010-10-29 16:08:23 -0300596 * @dev: the struct rc_dev descriptor of the device
597 * @scancode: the scancode to look for
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300598 *
David Härdemand8b4b582010-10-29 16:08:23 -0300599 * This routine is used by drivers which need to convert a scancode to a
600 * keycode. Normally it should not be used since drivers should have no
601 * interest in keycodes.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500602 *
603 * return: the corresponding keycode, or KEY_RESERVED
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300604 */
Sean Younge6c6d7d2020-01-17 17:46:36 +0100605u32 rc_g_keycode_from_table(struct rc_dev *dev, u64 scancode)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300606{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300607 struct rc_map *rc_map = &dev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700608 unsigned int keycode;
609 unsigned int index;
610 unsigned long flags;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300611
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300612 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700613
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300614 index = ir_lookup_by_scancode(rc_map, scancode);
615 keycode = index < rc_map->len ?
616 rc_map->scan[index].keycode : KEY_RESERVED;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700617
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300618 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700619
Mauro Carvalho Chehab35438942010-04-03 16:53:16 -0300620 if (keycode != KEY_RESERVED)
Sean Younge6c6d7d2020-01-17 17:46:36 +0100621 dev_dbg(&dev->dev, "%s: scancode 0x%04llx keycode 0x%02x\n",
Sean Young1f17f682018-02-12 07:27:50 -0500622 dev->device_name, scancode, keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700623
David Härdemanb3074c02010-04-02 15:58:28 -0300624 return keycode;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300625}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300626EXPORT_SYMBOL_GPL(rc_g_keycode_from_table);
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300627
628/**
David Härdeman62c65032010-10-29 16:08:07 -0300629 * ir_do_keyup() - internal function to signal the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300630 * @dev: the struct rc_dev descriptor of the device
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300631 * @sync: whether or not to call input_sync
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300632 *
David Härdeman62c65032010-10-29 16:08:07 -0300633 * This function is used internally to release a keypress, it must be
634 * called with keylock held.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300635 */
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300636static void ir_do_keyup(struct rc_dev *dev, bool sync)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300637{
David Härdemand8b4b582010-10-29 16:08:23 -0300638 if (!dev->keypressed)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300639 return;
640
Sean Young1f17f682018-02-12 07:27:50 -0500641 dev_dbg(&dev->dev, "keyup key 0x%04x\n", dev->last_keycode);
Sean Youngfb7ccc62017-12-03 08:55:24 -0500642 del_timer(&dev->timer_repeat);
David Härdemand8b4b582010-10-29 16:08:23 -0300643 input_report_key(dev->input_dev, dev->last_keycode, 0);
Sean Young153a60b2013-07-30 19:00:01 -0300644 led_trigger_event(led_feedback, LED_OFF);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300645 if (sync)
646 input_sync(dev->input_dev);
David Härdemand8b4b582010-10-29 16:08:23 -0300647 dev->keypressed = false;
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300648}
David Härdeman62c65032010-10-29 16:08:07 -0300649
650/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300651 * rc_keyup() - signals the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300652 * @dev: the struct rc_dev descriptor of the device
David Härdeman62c65032010-10-29 16:08:07 -0300653 *
654 * This routine is used to signal that a key has been released on the
655 * remote control.
656 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300657void rc_keyup(struct rc_dev *dev)
David Härdeman62c65032010-10-29 16:08:07 -0300658{
659 unsigned long flags;
David Härdeman62c65032010-10-29 16:08:07 -0300660
David Härdemand8b4b582010-10-29 16:08:23 -0300661 spin_lock_irqsave(&dev->keylock, flags);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300662 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300663 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300664}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300665EXPORT_SYMBOL_GPL(rc_keyup);
David Härdemana374fef2010-04-02 15:58:29 -0300666
667/**
668 * ir_timer_keyup() - generates a keyup event after a timeout
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500669 *
670 * @t: a pointer to the struct timer_list
David Härdemana374fef2010-04-02 15:58:29 -0300671 *
672 * This routine will generate a keyup event some time after a keydown event
673 * is generated when no further activity has been detected.
674 */
Kees Cookb17ec782017-10-24 11:23:14 -0400675static void ir_timer_keyup(struct timer_list *t)
David Härdemana374fef2010-04-02 15:58:29 -0300676{
Kees Cookb17ec782017-10-24 11:23:14 -0400677 struct rc_dev *dev = from_timer(dev, t, timer_keyup);
David Härdemana374fef2010-04-02 15:58:29 -0300678 unsigned long flags;
679
680 /*
681 * ir->keyup_jiffies is used to prevent a race condition if a
682 * hardware interrupt occurs at this point and the keyup timer
683 * event is moved further into the future as a result.
684 *
685 * The timer will then be reactivated and this function called
686 * again in the future. We need to exit gracefully in that case
687 * to allow the input subsystem to do its auto-repeat magic or
688 * a keyup event might follow immediately after the keydown.
689 */
David Härdemand8b4b582010-10-29 16:08:23 -0300690 spin_lock_irqsave(&dev->keylock, flags);
691 if (time_is_before_eq_jiffies(dev->keyup_jiffies))
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300692 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300693 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300694}
695
696/**
Sean Young57c642c2017-11-23 17:37:10 -0500697 * ir_timer_repeat() - generates a repeat event after a timeout
698 *
699 * @t: a pointer to the struct timer_list
700 *
701 * This routine will generate a soft repeat event every REP_PERIOD
702 * milliseconds.
703 */
704static void ir_timer_repeat(struct timer_list *t)
705{
706 struct rc_dev *dev = from_timer(dev, t, timer_repeat);
707 struct input_dev *input = dev->input_dev;
708 unsigned long flags;
709
710 spin_lock_irqsave(&dev->keylock, flags);
711 if (dev->keypressed) {
712 input_event(input, EV_KEY, dev->last_keycode, 2);
713 input_sync(input);
714 if (input->rep[REP_PERIOD])
715 mod_timer(&dev->timer_repeat, jiffies +
716 msecs_to_jiffies(input->rep[REP_PERIOD]));
717 }
718 spin_unlock_irqrestore(&dev->keylock, flags);
719}
720
Sean Youngf5dbee62018-07-28 05:11:15 -0400721static unsigned int repeat_period(int protocol)
722{
723 if (protocol >= ARRAY_SIZE(protocols))
724 return 100;
725
726 return protocols[protocol].repeat_period;
727}
728
Sean Young57c642c2017-11-23 17:37:10 -0500729/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300730 * rc_repeat() - signals that a key is still pressed
David Härdemand8b4b582010-10-29 16:08:23 -0300731 * @dev: the struct rc_dev descriptor of the device
David Härdemana374fef2010-04-02 15:58:29 -0300732 *
733 * This routine is used by IR decoders when a repeat message which does
734 * not include the necessary bits to reproduce the scancode has been
735 * received.
736 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300737void rc_repeat(struct rc_dev *dev)
David Härdemana374fef2010-04-02 15:58:29 -0300738{
739 unsigned long flags;
Matthias Reichl06b83152021-01-05 10:30:23 +0100740 unsigned int timeout = usecs_to_jiffies(dev->timeout) +
Sean Youngf5dbee62018-07-28 05:11:15 -0400741 msecs_to_jiffies(repeat_period(dev->last_protocol));
Sean Youngb66218f2017-09-30 07:13:37 -0400742 struct lirc_scancode sc = {
743 .scancode = dev->last_scancode, .rc_proto = dev->last_protocol,
744 .keycode = dev->keypressed ? dev->last_keycode : KEY_RESERVED,
745 .flags = LIRC_SCANCODE_FLAG_REPEAT |
746 (dev->last_toggle ? LIRC_SCANCODE_FLAG_TOGGLE : 0)
747 };
748
Sean Younge5bb9d32018-10-22 05:01:50 -0400749 if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
Sean Young75992a42020-08-24 21:10:45 +0200750 lirc_scancode_event(dev, &sc);
David Härdemana374fef2010-04-02 15:58:29 -0300751
David Härdemand8b4b582010-10-29 16:08:23 -0300752 spin_lock_irqsave(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300753
Sean Younge6c6d7d2020-01-17 17:46:36 +0100754 if (dev->last_scancode <= U32_MAX) {
755 input_event(dev->input_dev, EV_MSC, MSC_SCAN,
756 dev->last_scancode);
757 input_sync(dev->input_dev);
758 }
David Härdeman265a2982017-06-22 15:23:54 -0400759
Sean Youngb66218f2017-09-30 07:13:37 -0400760 if (dev->keypressed) {
Sean Young28492252018-03-24 08:02:48 -0400761 dev->keyup_jiffies = jiffies + timeout;
Sean Youngb66218f2017-09-30 07:13:37 -0400762 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
763 }
David Härdemana374fef2010-04-02 15:58:29 -0300764
David Härdemand8b4b582010-10-29 16:08:23 -0300765 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300766}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300767EXPORT_SYMBOL_GPL(rc_repeat);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300768
769/**
David Härdeman62c65032010-10-29 16:08:07 -0300770 * ir_do_keydown() - internal function to process a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300771 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300772 * @protocol: the protocol of the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300773 * @scancode: the scancode of the keypress
774 * @keycode: the keycode of the keypress
775 * @toggle: the toggle value of the keypress
776 *
777 * This function is used internally to register a keypress, it must be
778 * called with keylock held.
779 */
Sean Young6d741bf2017-08-07 16:20:58 -0400780static void ir_do_keydown(struct rc_dev *dev, enum rc_proto protocol,
Sean Younge6c6d7d2020-01-17 17:46:36 +0100781 u64 scancode, u32 keycode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300782{
David Härdeman99b0f3c2014-04-04 19:06:06 -0300783 bool new_event = (!dev->keypressed ||
David Härdeman120703f2014-04-03 20:31:30 -0300784 dev->last_protocol != protocol ||
David Härdeman99b0f3c2014-04-04 19:06:06 -0300785 dev->last_scancode != scancode ||
David Härdeman120703f2014-04-03 20:31:30 -0300786 dev->last_toggle != toggle);
Sean Youngde142c32017-02-25 06:51:32 -0500787 struct lirc_scancode sc = {
788 .scancode = scancode, .rc_proto = protocol,
789 .flags = toggle ? LIRC_SCANCODE_FLAG_TOGGLE : 0,
790 .keycode = keycode
791 };
792
Sean Younge5bb9d32018-10-22 05:01:50 -0400793 if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
Sean Young75992a42020-08-24 21:10:45 +0200794 lirc_scancode_event(dev, &sc);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300795
796 if (new_event && dev->keypressed)
797 ir_do_keyup(dev, false);
798
Sean Younge6c6d7d2020-01-17 17:46:36 +0100799 if (scancode <= U32_MAX)
800 input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300801
Sean Youngb66218f2017-09-30 07:13:37 -0400802 dev->last_protocol = protocol;
803 dev->last_scancode = scancode;
804 dev->last_toggle = toggle;
805 dev->last_keycode = keycode;
806
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300807 if (new_event && keycode != KEY_RESERVED) {
808 /* Register a keypress */
809 dev->keypressed = true;
David Härdeman62c65032010-10-29 16:08:07 -0300810
Sean Younge6c6d7d2020-01-17 17:46:36 +0100811 dev_dbg(&dev->dev, "%s: key down event, key 0x%04x, protocol 0x%04x, scancode 0x%08llx\n",
Sean Young1f17f682018-02-12 07:27:50 -0500812 dev->device_name, keycode, protocol, scancode);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300813 input_report_key(dev->input_dev, keycode, 1);
James Hogan70a2f912014-01-16 19:56:22 -0300814
815 led_trigger_event(led_feedback, LED_FULL);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300816 }
David Härdeman62c65032010-10-29 16:08:07 -0300817
Sean Young57c642c2017-11-23 17:37:10 -0500818 /*
819 * For CEC, start sending repeat messages as soon as the first
820 * repeated message is sent, as long as REP_DELAY = 0 and REP_PERIOD
821 * is non-zero. Otherwise, the input layer will generate repeat
822 * messages.
823 */
824 if (!new_event && keycode != KEY_RESERVED &&
825 dev->allowed_protocols == RC_PROTO_BIT_CEC &&
826 !timer_pending(&dev->timer_repeat) &&
827 dev->input_dev->rep[REP_PERIOD] &&
828 !dev->input_dev->rep[REP_DELAY]) {
829 input_event(dev->input_dev, EV_KEY, keycode, 2);
830 mod_timer(&dev->timer_repeat, jiffies +
831 msecs_to_jiffies(dev->input_dev->rep[REP_PERIOD]));
832 }
833
David Härdemand8b4b582010-10-29 16:08:23 -0300834 input_sync(dev->input_dev);
David Härdeman62c65032010-10-29 16:08:07 -0300835}
836
837/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300838 * rc_keydown() - generates input event for a key press
David Härdemand8b4b582010-10-29 16:08:23 -0300839 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300840 * @protocol: the protocol for the keypress
841 * @scancode: the scancode for the keypress
David Härdemana374fef2010-04-02 15:58:29 -0300842 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
843 * support toggle values, this should be set to zero)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300844 *
David Härdemand8b4b582010-10-29 16:08:23 -0300845 * This routine is used to signal that a key has been pressed on the
846 * remote control.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300847 */
Sean Younge6c6d7d2020-01-17 17:46:36 +0100848void rc_keydown(struct rc_dev *dev, enum rc_proto protocol, u64 scancode,
Sean Young6d741bf2017-08-07 16:20:58 -0400849 u8 toggle)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300850{
David Härdemana374fef2010-04-02 15:58:29 -0300851 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300852 u32 keycode = rc_g_keycode_from_table(dev, scancode);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300853
David Härdemand8b4b582010-10-29 16:08:23 -0300854 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300855 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300856
David Härdemand8b4b582010-10-29 16:08:23 -0300857 if (dev->keypressed) {
Matthias Reichl06b83152021-01-05 10:30:23 +0100858 dev->keyup_jiffies = jiffies + usecs_to_jiffies(dev->timeout) +
Sean Youngf5dbee62018-07-28 05:11:15 -0400859 msecs_to_jiffies(repeat_period(protocol));
David Härdemand8b4b582010-10-29 16:08:23 -0300860 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
David Härdeman62c65032010-10-29 16:08:07 -0300861 }
David Härdemand8b4b582010-10-29 16:08:23 -0300862 spin_unlock_irqrestore(&dev->keylock, flags);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300863}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300864EXPORT_SYMBOL_GPL(rc_keydown);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300865
David Härdeman62c65032010-10-29 16:08:07 -0300866/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300867 * rc_keydown_notimeout() - generates input event for a key press without
David Härdeman62c65032010-10-29 16:08:07 -0300868 * an automatic keyup event at a later time
David Härdemand8b4b582010-10-29 16:08:23 -0300869 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300870 * @protocol: the protocol for the keypress
871 * @scancode: the scancode for the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300872 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
873 * support toggle values, this should be set to zero)
874 *
David Härdemand8b4b582010-10-29 16:08:23 -0300875 * This routine is used to signal that a key has been pressed on the
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300876 * remote control. The driver must manually call rc_keyup() at a later stage.
David Härdeman62c65032010-10-29 16:08:07 -0300877 */
Sean Young6d741bf2017-08-07 16:20:58 -0400878void rc_keydown_notimeout(struct rc_dev *dev, enum rc_proto protocol,
Sean Younge6c6d7d2020-01-17 17:46:36 +0100879 u64 scancode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300880{
881 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300882 u32 keycode = rc_g_keycode_from_table(dev, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300883
David Härdemand8b4b582010-10-29 16:08:23 -0300884 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300885 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
David Härdemand8b4b582010-10-29 16:08:23 -0300886 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300887}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300888EXPORT_SYMBOL_GPL(rc_keydown_notimeout);
David Härdeman62c65032010-10-29 16:08:07 -0300889
Sean Youngb590c0b2016-12-05 19:24:59 -0200890/**
Sean Young6b514c42017-10-09 16:32:41 -0400891 * rc_validate_scancode() - checks that a scancode is valid for a protocol.
892 * For nec, it should do the opposite of ir_nec_bytes_to_scancode()
Sean Young49a4b362017-09-27 16:00:49 -0400893 * @proto: protocol
894 * @scancode: scancode
895 */
896bool rc_validate_scancode(enum rc_proto proto, u32 scancode)
897{
898 switch (proto) {
Sean Young6b514c42017-10-09 16:32:41 -0400899 /*
900 * NECX has a 16-bit address; if the lower 8 bits match the upper
901 * 8 bits inverted, then the address would match regular nec.
902 */
Sean Young49a4b362017-09-27 16:00:49 -0400903 case RC_PROTO_NECX:
904 if ((((scancode >> 16) ^ ~(scancode >> 8)) & 0xff) == 0)
905 return false;
906 break;
Sean Young6b514c42017-10-09 16:32:41 -0400907 /*
908 * NEC32 has a 16 bit address and 16 bit command. If the lower 8 bits
909 * of the command match the upper 8 bits inverted, then it would
910 * be either NEC or NECX.
911 */
Sean Young49a4b362017-09-27 16:00:49 -0400912 case RC_PROTO_NEC32:
Sean Young6b514c42017-10-09 16:32:41 -0400913 if ((((scancode >> 8) ^ ~scancode) & 0xff) == 0)
Sean Young49a4b362017-09-27 16:00:49 -0400914 return false;
915 break;
Sean Young6b514c42017-10-09 16:32:41 -0400916 /*
917 * If the customer code (top 32-bit) is 0x800f, it is MCE else it
918 * is regular mode-6a 32 bit
919 */
Sean Young49a4b362017-09-27 16:00:49 -0400920 case RC_PROTO_RC6_MCE:
921 if ((scancode & 0xffff0000) != 0x800f0000)
922 return false;
923 break;
924 case RC_PROTO_RC6_6A_32:
925 if ((scancode & 0xffff0000) == 0x800f0000)
926 return false;
927 break;
928 default:
929 break;
930 }
931
932 return true;
933}
934
935/**
Sean Youngb590c0b2016-12-05 19:24:59 -0200936 * rc_validate_filter() - checks that the scancode and mask are valid and
937 * provides sensible defaults
James Hoganf423ccc2015-03-31 14:48:10 -0300938 * @dev: the struct rc_dev descriptor of the device
Sean Youngb590c0b2016-12-05 19:24:59 -0200939 * @filter: the scancode and mask
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500940 *
941 * return: 0 or -EINVAL if the filter is not valid
Sean Youngb590c0b2016-12-05 19:24:59 -0200942 */
James Hoganf423ccc2015-03-31 14:48:10 -0300943static int rc_validate_filter(struct rc_dev *dev,
Sean Youngb590c0b2016-12-05 19:24:59 -0200944 struct rc_scancode_filter *filter)
945{
Sean Youngd57ea872017-08-09 13:19:16 -0400946 u32 mask, s = filter->data;
Sean Young6d741bf2017-08-07 16:20:58 -0400947 enum rc_proto protocol = dev->wakeup_protocol;
Sean Youngb590c0b2016-12-05 19:24:59 -0200948
Sean Youngd57ea872017-08-09 13:19:16 -0400949 if (protocol >= ARRAY_SIZE(protocols))
Sean Young2168b412017-08-07 09:21:29 -0400950 return -EINVAL;
951
Sean Youngd57ea872017-08-09 13:19:16 -0400952 mask = protocols[protocol].scancode_bits;
953
Sean Young49a4b362017-09-27 16:00:49 -0400954 if (!rc_validate_scancode(protocol, s))
955 return -EINVAL;
Sean Youngb590c0b2016-12-05 19:24:59 -0200956
Sean Youngd57ea872017-08-09 13:19:16 -0400957 filter->data &= mask;
958 filter->mask &= mask;
Sean Youngb590c0b2016-12-05 19:24:59 -0200959
James Hoganf423ccc2015-03-31 14:48:10 -0300960 /*
961 * If we have to raw encode the IR for wakeup, we cannot have a mask
962 */
Sean Youngd57ea872017-08-09 13:19:16 -0400963 if (dev->encode_wakeup && filter->mask != 0 && filter->mask != mask)
James Hoganf423ccc2015-03-31 14:48:10 -0300964 return -EINVAL;
965
Sean Youngb590c0b2016-12-05 19:24:59 -0200966 return 0;
967}
968
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300969int rc_open(struct rc_dev *rdev)
970{
971 int rval = 0;
972
973 if (!rdev)
974 return -EINVAL;
975
976 mutex_lock(&rdev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -0200977
Sean Youngcb843432017-09-23 17:44:03 -0400978 if (!rdev->registered) {
979 rval = -ENODEV;
980 } else {
981 if (!rdev->users++ && rdev->open)
982 rval = rdev->open(rdev);
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300983
Sean Youngcb843432017-09-23 17:44:03 -0400984 if (rval)
985 rdev->users--;
986 }
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300987
988 mutex_unlock(&rdev->lock);
989
990 return rval;
991}
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300992
David Härdemand8b4b582010-10-29 16:08:23 -0300993static int ir_open(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300994{
David Härdemand8b4b582010-10-29 16:08:23 -0300995 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300996
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300997 return rc_open(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300998}
999
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -03001000void rc_close(struct rc_dev *rdev)
1001{
1002 if (rdev) {
1003 mutex_lock(&rdev->lock);
1004
Sean Youngcb843432017-09-23 17:44:03 -04001005 if (!--rdev->users && rdev->close && rdev->registered)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -03001006 rdev->close(rdev);
1007
1008 mutex_unlock(&rdev->lock);
1009 }
1010}
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -03001011
David Härdemand8b4b582010-10-29 16:08:23 -03001012static void ir_close(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -03001013{
David Härdemand8b4b582010-10-29 16:08:23 -03001014 struct rc_dev *rdev = input_get_drvdata(idev);
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -03001015 rc_close(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -03001016}
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -03001017
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001018/* class for /sys/class/rc */
David Härdeman40fc5322013-03-06 16:52:10 -03001019static char *rc_devnode(struct device *dev, umode_t *mode)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001020{
1021 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
1022}
1023
David Härdeman40fc5322013-03-06 16:52:10 -03001024static struct class rc_class = {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001025 .name = "rc",
David Härdeman40fc5322013-03-06 16:52:10 -03001026 .devnode = rc_devnode,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001027};
1028
David Härdemanc003ab12012-10-11 19:11:54 -03001029/*
1030 * These are the protocol textual descriptions that are
1031 * used by the sysfs protocols file. Note that the order
1032 * of the entries is relevant.
1033 */
Heiner Kallweit53df8772015-11-16 17:52:17 -02001034static const struct {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001035 u64 type;
Heiner Kallweit53df8772015-11-16 17:52:17 -02001036 const char *name;
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001037 const char *module_name;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001038} proto_names[] = {
Sean Young6d741bf2017-08-07 16:20:58 -04001039 { RC_PROTO_BIT_NONE, "none", NULL },
1040 { RC_PROTO_BIT_OTHER, "other", NULL },
1041 { RC_PROTO_BIT_UNKNOWN, "unknown", NULL },
1042 { RC_PROTO_BIT_RC5 |
1043 RC_PROTO_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
1044 { RC_PROTO_BIT_NEC |
1045 RC_PROTO_BIT_NECX |
1046 RC_PROTO_BIT_NEC32, "nec", "ir-nec-decoder" },
1047 { RC_PROTO_BIT_RC6_0 |
1048 RC_PROTO_BIT_RC6_6A_20 |
1049 RC_PROTO_BIT_RC6_6A_24 |
1050 RC_PROTO_BIT_RC6_6A_32 |
1051 RC_PROTO_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
1052 { RC_PROTO_BIT_JVC, "jvc", "ir-jvc-decoder" },
1053 { RC_PROTO_BIT_SONY12 |
1054 RC_PROTO_BIT_SONY15 |
1055 RC_PROTO_BIT_SONY20, "sony", "ir-sony-decoder" },
1056 { RC_PROTO_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
1057 { RC_PROTO_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
1058 { RC_PROTO_BIT_SHARP, "sharp", "ir-sharp-decoder" },
1059 { RC_PROTO_BIT_MCIR2_KBD |
1060 RC_PROTO_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" },
1061 { RC_PROTO_BIT_XMP, "xmp", "ir-xmp-decoder" },
1062 { RC_PROTO_BIT_CEC, "cec", NULL },
Sean Young447dcc0c2017-12-03 11:06:54 -05001063 { RC_PROTO_BIT_IMON, "imon", "ir-imon-decoder" },
Patrick Lerda721074b2019-01-17 03:50:13 -05001064 { RC_PROTO_BIT_RCMM12 |
1065 RC_PROTO_BIT_RCMM24 |
1066 RC_PROTO_BIT_RCMM32, "rc-mm", "ir-rcmm-decoder" },
Matthias Reichl17287692019-03-24 05:43:51 -04001067 { RC_PROTO_BIT_XBOX_DVD, "xbox-dvd", NULL },
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001068};
1069
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001070/**
James Hoganab88c662014-02-28 20:17:05 -03001071 * struct rc_filter_attribute - Device attribute relating to a filter type.
1072 * @attr: Device attribute.
1073 * @type: Filter type.
1074 * @mask: false for filter value, true for filter mask.
1075 */
1076struct rc_filter_attribute {
1077 struct device_attribute attr;
1078 enum rc_filter_type type;
1079 bool mask;
1080};
1081#define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
1082
James Hoganab88c662014-02-28 20:17:05 -03001083#define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
1084 struct rc_filter_attribute dev_attr_##_name = { \
1085 .attr = __ATTR(_name, _mode, _show, _store), \
1086 .type = (_type), \
1087 .mask = (_mask), \
1088 }
1089
1090/**
Sean Young0751d332016-12-05 17:08:35 -02001091 * show_protocols() - shows the current IR protocol(s)
David Härdemand8b4b582010-10-29 16:08:23 -03001092 * @device: the device descriptor
David Härdemanda6e1622014-04-03 20:32:16 -03001093 * @mattr: the device attribute struct
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001094 * @buf: a pointer to the output buffer
1095 *
1096 * This routine is a callback routine for input read the IR protocol type(s).
Mauro Carvalho Chehab04ad3012019-02-18 14:29:01 -05001097 * it is triggered by reading /sys/class/rc/rc?/protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001098 * It returns the protocol names of supported protocols.
1099 * Enabled protocols are printed in brackets.
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001100 *
David Härdeman18726a32017-04-27 17:34:08 -03001101 * dev->lock is taken to guard against races between
1102 * store_protocols and show_protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001103 */
David Härdemand8b4b582010-10-29 16:08:23 -03001104static ssize_t show_protocols(struct device *device,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001105 struct device_attribute *mattr, char *buf)
1106{
David Härdemand8b4b582010-10-29 16:08:23 -03001107 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001108 u64 allowed, enabled;
1109 char *tmp = buf;
1110 int i;
1111
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001112 mutex_lock(&dev->lock);
1113
Sean Young0751d332016-12-05 17:08:35 -02001114 enabled = dev->enabled_protocols;
1115 allowed = dev->allowed_protocols;
1116 if (dev->raw && !allowed)
1117 allowed = ir_raw_get_allowed_protocols();
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001118
David Härdemanda6e1622014-04-03 20:32:16 -03001119 mutex_unlock(&dev->lock);
1120
Sean Young1f17f682018-02-12 07:27:50 -05001121 dev_dbg(&dev->dev, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
1122 __func__, (long long)allowed, (long long)enabled);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001123
1124 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1125 if (allowed & enabled & proto_names[i].type)
1126 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
1127 else if (allowed & proto_names[i].type)
1128 tmp += sprintf(tmp, "%s ", proto_names[i].name);
David Härdemanc003ab12012-10-11 19:11:54 -03001129
1130 if (allowed & proto_names[i].type)
1131 allowed &= ~proto_names[i].type;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001132 }
1133
Sean Younga60d64b2017-09-23 10:41:13 -04001134#ifdef CONFIG_LIRC
1135 if (dev->driver_type == RC_DRIVER_IR_RAW)
David Härdeman275ddb42015-05-19 19:03:22 -03001136 tmp += sprintf(tmp, "[lirc] ");
Sean Younga60d64b2017-09-23 10:41:13 -04001137#endif
David Härdeman275ddb42015-05-19 19:03:22 -03001138
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001139 if (tmp != buf)
1140 tmp--;
1141 *tmp = '\n';
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001142
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001143 return tmp + 1 - buf;
1144}
1145
1146/**
David Härdemanda6e1622014-04-03 20:32:16 -03001147 * parse_protocol_change() - parses a protocol change request
Sean Young1f17f682018-02-12 07:27:50 -05001148 * @dev: rc_dev device
David Härdemanda6e1622014-04-03 20:32:16 -03001149 * @protocols: pointer to the bitmask of current protocols
1150 * @buf: pointer to the buffer with a list of changes
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001151 *
David Härdemanda6e1622014-04-03 20:32:16 -03001152 * Writing "+proto" will add a protocol to the protocol mask.
1153 * Writing "-proto" will remove a protocol from protocol mask.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001154 * Writing "proto" will enable only "proto".
1155 * Writing "none" will disable all protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001156 * Returns the number of changes performed or a negative error code.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001157 */
Sean Young1f17f682018-02-12 07:27:50 -05001158static int parse_protocol_change(struct rc_dev *dev, u64 *protocols,
1159 const char *buf)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001160{
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001161 const char *tmp;
David Härdemanda6e1622014-04-03 20:32:16 -03001162 unsigned count = 0;
1163 bool enable, disable;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001164 u64 mask;
David Härdemanda6e1622014-04-03 20:32:16 -03001165 int i;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001166
David Härdemanda6e1622014-04-03 20:32:16 -03001167 while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001168 if (!*tmp)
1169 break;
1170
1171 if (*tmp == '+') {
1172 enable = true;
1173 disable = false;
1174 tmp++;
1175 } else if (*tmp == '-') {
1176 enable = false;
1177 disable = true;
1178 tmp++;
1179 } else {
1180 enable = false;
1181 disable = false;
1182 }
1183
David Härdemanc003ab12012-10-11 19:11:54 -03001184 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1185 if (!strcasecmp(tmp, proto_names[i].name)) {
1186 mask = proto_names[i].type;
1187 break;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001188 }
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001189 }
1190
David Härdemanc003ab12012-10-11 19:11:54 -03001191 if (i == ARRAY_SIZE(proto_names)) {
David Härdeman275ddb42015-05-19 19:03:22 -03001192 if (!strcasecmp(tmp, "lirc"))
1193 mask = 0;
1194 else {
Sean Young1f17f682018-02-12 07:27:50 -05001195 dev_dbg(&dev->dev, "Unknown protocol: '%s'\n",
1196 tmp);
David Härdeman275ddb42015-05-19 19:03:22 -03001197 return -EINVAL;
1198 }
David Härdemanc003ab12012-10-11 19:11:54 -03001199 }
1200
1201 count++;
1202
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001203 if (enable)
David Härdemanda6e1622014-04-03 20:32:16 -03001204 *protocols |= mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001205 else if (disable)
David Härdemanda6e1622014-04-03 20:32:16 -03001206 *protocols &= ~mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001207 else
David Härdemanda6e1622014-04-03 20:32:16 -03001208 *protocols = mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001209 }
1210
1211 if (!count) {
Sean Young1f17f682018-02-12 07:27:50 -05001212 dev_dbg(&dev->dev, "Protocol not specified\n");
David Härdemanda6e1622014-04-03 20:32:16 -03001213 return -EINVAL;
1214 }
1215
1216 return count;
1217}
1218
Sean Young0d39ab02017-02-25 06:51:31 -05001219void ir_raw_load_modules(u64 *protocols)
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001220{
1221 u64 available;
1222 int i, ret;
1223
1224 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
Sean Young6d741bf2017-08-07 16:20:58 -04001225 if (proto_names[i].type == RC_PROTO_BIT_NONE ||
1226 proto_names[i].type & (RC_PROTO_BIT_OTHER |
1227 RC_PROTO_BIT_UNKNOWN))
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001228 continue;
1229
1230 available = ir_raw_get_allowed_protocols();
1231 if (!(*protocols & proto_names[i].type & ~available))
1232 continue;
1233
1234 if (!proto_names[i].module_name) {
1235 pr_err("Can't enable IR protocol %s\n",
1236 proto_names[i].name);
1237 *protocols &= ~proto_names[i].type;
1238 continue;
1239 }
1240
1241 ret = request_module("%s", proto_names[i].module_name);
1242 if (ret < 0) {
1243 pr_err("Couldn't load IR protocol module %s\n",
1244 proto_names[i].module_name);
1245 *protocols &= ~proto_names[i].type;
1246 continue;
1247 }
1248 msleep(20);
1249 available = ir_raw_get_allowed_protocols();
1250 if (!(*protocols & proto_names[i].type & ~available))
1251 continue;
1252
Sean Young8caebcd2017-01-19 19:33:49 -02001253 pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001254 proto_names[i].module_name,
1255 proto_names[i].name);
1256 *protocols &= ~proto_names[i].type;
1257 }
1258}
1259
David Härdemanda6e1622014-04-03 20:32:16 -03001260/**
1261 * store_protocols() - changes the current/wakeup IR protocol(s)
1262 * @device: the device descriptor
1263 * @mattr: the device attribute struct
1264 * @buf: a pointer to the input buffer
1265 * @len: length of the input buffer
1266 *
1267 * This routine is for changing the IR protocol type.
Mauro Carvalho Chehab04ad3012019-02-18 14:29:01 -05001268 * It is triggered by writing to /sys/class/rc/rc?/[wakeup_]protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001269 * See parse_protocol_change() for the valid commands.
1270 * Returns @len on success or a negative error code.
1271 *
David Härdeman18726a32017-04-27 17:34:08 -03001272 * dev->lock is taken to guard against races between
1273 * store_protocols and show_protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001274 */
1275static ssize_t store_protocols(struct device *device,
1276 struct device_attribute *mattr,
1277 const char *buf, size_t len)
1278{
1279 struct rc_dev *dev = to_rc_dev(device);
David Härdemanda6e1622014-04-03 20:32:16 -03001280 u64 *current_protocols;
David Härdemanda6e1622014-04-03 20:32:16 -03001281 struct rc_scancode_filter *filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001282 u64 old_protocols, new_protocols;
1283 ssize_t rc;
1284
Sean Young1f17f682018-02-12 07:27:50 -05001285 dev_dbg(&dev->dev, "Normal protocol change requested\n");
Sean Young0751d332016-12-05 17:08:35 -02001286 current_protocols = &dev->enabled_protocols;
1287 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001288
Sean Young0751d332016-12-05 17:08:35 -02001289 if (!dev->change_protocol) {
Sean Young1f17f682018-02-12 07:27:50 -05001290 dev_dbg(&dev->dev, "Protocol switching not supported\n");
David Härdemanda6e1622014-04-03 20:32:16 -03001291 return -EINVAL;
1292 }
1293
1294 mutex_lock(&dev->lock);
Sean Younga2e2d732020-08-08 13:38:02 +02001295 if (!dev->registered) {
1296 mutex_unlock(&dev->lock);
1297 return -ENODEV;
1298 }
David Härdemanda6e1622014-04-03 20:32:16 -03001299
1300 old_protocols = *current_protocols;
1301 new_protocols = old_protocols;
Sean Young1f17f682018-02-12 07:27:50 -05001302 rc = parse_protocol_change(dev, &new_protocols, buf);
David Härdemanda6e1622014-04-03 20:32:16 -03001303 if (rc < 0)
1304 goto out;
1305
Sean Younga86d6df2018-03-23 16:47:37 -04001306 if (dev->driver_type == RC_DRIVER_IR_RAW)
1307 ir_raw_load_modules(&new_protocols);
1308
Sean Young0751d332016-12-05 17:08:35 -02001309 rc = dev->change_protocol(dev, &new_protocols);
David Härdemanda6e1622014-04-03 20:32:16 -03001310 if (rc < 0) {
Sean Young1f17f682018-02-12 07:27:50 -05001311 dev_dbg(&dev->dev, "Error setting protocols to 0x%llx\n",
1312 (long long)new_protocols);
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001313 goto out;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001314 }
1315
James Hogan983c5bd2014-12-08 13:17:07 -03001316 if (new_protocols != old_protocols) {
1317 *current_protocols = new_protocols;
Sean Young1f17f682018-02-12 07:27:50 -05001318 dev_dbg(&dev->dev, "Protocols changed to 0x%llx\n",
1319 (long long)new_protocols);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001320 }
1321
James Hogan6bea25a2014-02-28 20:17:06 -03001322 /*
James Hogan983c5bd2014-12-08 13:17:07 -03001323 * If a protocol change was attempted the filter may need updating, even
1324 * if the actual protocol mask hasn't changed (since the driver may have
1325 * cleared the filter).
James Hogan6bea25a2014-02-28 20:17:06 -03001326 * Try setting the same filter with the new protocol (if any).
1327 * Fall back to clearing the filter.
1328 */
Sean Young0751d332016-12-05 17:08:35 -02001329 if (dev->s_filter && filter->mask) {
David Härdemanda6e1622014-04-03 20:32:16 -03001330 if (new_protocols)
Sean Young0751d332016-12-05 17:08:35 -02001331 rc = dev->s_filter(dev, filter);
David Härdemanda6e1622014-04-03 20:32:16 -03001332 else
1333 rc = -1;
David Härdeman23c843b2014-04-04 19:06:01 -03001334
David Härdemanda6e1622014-04-03 20:32:16 -03001335 if (rc < 0) {
1336 filter->data = 0;
1337 filter->mask = 0;
Sean Young0751d332016-12-05 17:08:35 -02001338 dev->s_filter(dev, filter);
James Hogan6bea25a2014-02-28 20:17:06 -03001339 }
James Hogan6bea25a2014-02-28 20:17:06 -03001340 }
1341
David Härdemanda6e1622014-04-03 20:32:16 -03001342 rc = len;
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001343
1344out:
1345 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001346 return rc;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001347}
1348
James Hogan00942d12014-01-17 10:58:49 -03001349/**
James Hogan00942d12014-01-17 10:58:49 -03001350 * show_filter() - shows the current scancode filter value or mask
1351 * @device: the device descriptor
1352 * @attr: the device attribute struct
1353 * @buf: a pointer to the output buffer
1354 *
1355 * This routine is a callback routine to read a scancode filter value or mask.
Mauro Carvalho Chehab04ad3012019-02-18 14:29:01 -05001356 * It is triggered by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
James Hogan00942d12014-01-17 10:58:49 -03001357 * It prints the current scancode filter value or mask of the appropriate filter
1358 * type in hexadecimal into @buf and returns the size of the buffer.
1359 *
1360 * Bits of the filter value corresponding to set bits in the filter mask are
1361 * compared against input scancodes and non-matching scancodes are discarded.
1362 *
David Härdeman18726a32017-04-27 17:34:08 -03001363 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001364 * store_filter and show_filter.
1365 */
1366static ssize_t show_filter(struct device *device,
1367 struct device_attribute *attr,
1368 char *buf)
1369{
1370 struct rc_dev *dev = to_rc_dev(device);
1371 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001372 struct rc_scancode_filter *filter;
James Hogan00942d12014-01-17 10:58:49 -03001373 u32 val;
1374
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001375 mutex_lock(&dev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001376
David Härdemanda6e1622014-04-03 20:32:16 -03001377 if (fattr->type == RC_FILTER_NORMAL)
David Härdemanc5540fb2014-04-03 20:32:21 -03001378 filter = &dev->scancode_filter;
James Hogan00942d12014-01-17 10:58:49 -03001379 else
David Härdemanc5540fb2014-04-03 20:32:21 -03001380 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001381
David Härdemanda6e1622014-04-03 20:32:16 -03001382 if (fattr->mask)
1383 val = filter->mask;
1384 else
1385 val = filter->data;
James Hogan00942d12014-01-17 10:58:49 -03001386 mutex_unlock(&dev->lock);
1387
1388 return sprintf(buf, "%#x\n", val);
1389}
1390
1391/**
1392 * store_filter() - changes the scancode filter value
1393 * @device: the device descriptor
1394 * @attr: the device attribute struct
1395 * @buf: a pointer to the input buffer
1396 * @len: length of the input buffer
1397 *
1398 * This routine is for changing a scancode filter value or mask.
Mauro Carvalho Chehab04ad3012019-02-18 14:29:01 -05001399 * It is triggered by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
James Hogan00942d12014-01-17 10:58:49 -03001400 * Returns -EINVAL if an invalid filter value for the current protocol was
1401 * specified or if scancode filtering is not supported by the driver, otherwise
1402 * returns @len.
1403 *
1404 * Bits of the filter value corresponding to set bits in the filter mask are
1405 * compared against input scancodes and non-matching scancodes are discarded.
1406 *
David Härdeman18726a32017-04-27 17:34:08 -03001407 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001408 * store_filter and show_filter.
1409 */
1410static ssize_t store_filter(struct device *device,
1411 struct device_attribute *attr,
David Härdemanda6e1622014-04-03 20:32:16 -03001412 const char *buf, size_t len)
James Hogan00942d12014-01-17 10:58:49 -03001413{
1414 struct rc_dev *dev = to_rc_dev(device);
1415 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001416 struct rc_scancode_filter new_filter, *filter;
James Hogan00942d12014-01-17 10:58:49 -03001417 int ret;
1418 unsigned long val;
David Härdeman23c843b2014-04-04 19:06:01 -03001419 int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
James Hogan00942d12014-01-17 10:58:49 -03001420
James Hogan00942d12014-01-17 10:58:49 -03001421 ret = kstrtoul(buf, 0, &val);
1422 if (ret < 0)
1423 return ret;
1424
David Härdemanda6e1622014-04-03 20:32:16 -03001425 if (fattr->type == RC_FILTER_NORMAL) {
1426 set_filter = dev->s_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001427 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001428 } else {
1429 set_filter = dev->s_wakeup_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001430 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001431 }
1432
David Härdeman99b0f3c2014-04-04 19:06:06 -03001433 if (!set_filter)
1434 return -EINVAL;
James Hogan00942d12014-01-17 10:58:49 -03001435
1436 mutex_lock(&dev->lock);
Sean Younga2e2d732020-08-08 13:38:02 +02001437 if (!dev->registered) {
1438 mutex_unlock(&dev->lock);
1439 return -ENODEV;
1440 }
James Hogan00942d12014-01-17 10:58:49 -03001441
David Härdemanda6e1622014-04-03 20:32:16 -03001442 new_filter = *filter;
James Hogan00942d12014-01-17 10:58:49 -03001443 if (fattr->mask)
David Härdemanda6e1622014-04-03 20:32:16 -03001444 new_filter.mask = val;
James Hogan00942d12014-01-17 10:58:49 -03001445 else
David Härdemanda6e1622014-04-03 20:32:16 -03001446 new_filter.data = val;
David Härdeman23c843b2014-04-04 19:06:01 -03001447
Sean Young0751d332016-12-05 17:08:35 -02001448 if (fattr->type == RC_FILTER_WAKEUP) {
Sean Youngb590c0b2016-12-05 19:24:59 -02001449 /*
1450 * Refuse to set a filter unless a protocol is enabled
1451 * and the filter is valid for that protocol
1452 */
Sean Young6d741bf2017-08-07 16:20:58 -04001453 if (dev->wakeup_protocol != RC_PROTO_UNKNOWN)
James Hoganf423ccc2015-03-31 14:48:10 -03001454 ret = rc_validate_filter(dev, &new_filter);
Sean Youngb590c0b2016-12-05 19:24:59 -02001455 else
Sean Young0751d332016-12-05 17:08:35 -02001456 ret = -EINVAL;
Sean Youngb590c0b2016-12-05 19:24:59 -02001457
1458 if (ret != 0)
Sean Young0751d332016-12-05 17:08:35 -02001459 goto unlock;
Sean Young0751d332016-12-05 17:08:35 -02001460 }
1461
1462 if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
1463 val) {
James Hogan6bea25a2014-02-28 20:17:06 -03001464 /* refuse to set a filter unless a protocol is enabled */
1465 ret = -EINVAL;
1466 goto unlock;
1467 }
David Härdeman23c843b2014-04-04 19:06:01 -03001468
David Härdemanda6e1622014-04-03 20:32:16 -03001469 ret = set_filter(dev, &new_filter);
David Härdeman99b0f3c2014-04-04 19:06:06 -03001470 if (ret < 0)
1471 goto unlock;
James Hogan00942d12014-01-17 10:58:49 -03001472
David Härdemanda6e1622014-04-03 20:32:16 -03001473 *filter = new_filter;
James Hogan00942d12014-01-17 10:58:49 -03001474
1475unlock:
1476 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001477 return (ret < 0) ? ret : len;
James Hogan00942d12014-01-17 10:58:49 -03001478}
1479
Sean Young0751d332016-12-05 17:08:35 -02001480/**
1481 * show_wakeup_protocols() - shows the wakeup IR protocol
1482 * @device: the device descriptor
1483 * @mattr: the device attribute struct
1484 * @buf: a pointer to the output buffer
1485 *
1486 * This routine is a callback routine for input read the IR protocol type(s).
Mauro Carvalho Chehab04ad3012019-02-18 14:29:01 -05001487 * it is triggered by reading /sys/class/rc/rc?/wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001488 * It returns the protocol names of supported protocols.
1489 * The enabled protocols are printed in brackets.
1490 *
David Härdeman18726a32017-04-27 17:34:08 -03001491 * dev->lock is taken to guard against races between
1492 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001493 */
1494static ssize_t show_wakeup_protocols(struct device *device,
1495 struct device_attribute *mattr,
1496 char *buf)
1497{
1498 struct rc_dev *dev = to_rc_dev(device);
1499 u64 allowed;
Sean Young6d741bf2017-08-07 16:20:58 -04001500 enum rc_proto enabled;
Sean Young0751d332016-12-05 17:08:35 -02001501 char *tmp = buf;
1502 int i;
1503
Sean Young0751d332016-12-05 17:08:35 -02001504 mutex_lock(&dev->lock);
1505
1506 allowed = dev->allowed_wakeup_protocols;
1507 enabled = dev->wakeup_protocol;
1508
1509 mutex_unlock(&dev->lock);
1510
Sean Young1f17f682018-02-12 07:27:50 -05001511 dev_dbg(&dev->dev, "%s: allowed - 0x%llx, enabled - %d\n",
1512 __func__, (long long)allowed, enabled);
Sean Young0751d332016-12-05 17:08:35 -02001513
Sean Youngd57ea872017-08-09 13:19:16 -04001514 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001515 if (allowed & (1ULL << i)) {
1516 if (i == enabled)
Sean Youngd57ea872017-08-09 13:19:16 -04001517 tmp += sprintf(tmp, "[%s] ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001518 else
Sean Youngd57ea872017-08-09 13:19:16 -04001519 tmp += sprintf(tmp, "%s ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001520 }
1521 }
1522
1523 if (tmp != buf)
1524 tmp--;
1525 *tmp = '\n';
1526
1527 return tmp + 1 - buf;
1528}
1529
1530/**
1531 * store_wakeup_protocols() - changes the wakeup IR protocol(s)
1532 * @device: the device descriptor
1533 * @mattr: the device attribute struct
1534 * @buf: a pointer to the input buffer
1535 * @len: length of the input buffer
1536 *
1537 * This routine is for changing the IR protocol type.
Mauro Carvalho Chehab04ad3012019-02-18 14:29:01 -05001538 * It is triggered by writing to /sys/class/rc/rc?/wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001539 * Returns @len on success or a negative error code.
1540 *
David Härdeman18726a32017-04-27 17:34:08 -03001541 * dev->lock is taken to guard against races between
1542 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001543 */
1544static ssize_t store_wakeup_protocols(struct device *device,
1545 struct device_attribute *mattr,
1546 const char *buf, size_t len)
1547{
1548 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehab3d351532019-05-23 05:27:30 -04001549 enum rc_proto protocol = RC_PROTO_UNKNOWN;
Sean Young0751d332016-12-05 17:08:35 -02001550 ssize_t rc;
1551 u64 allowed;
1552 int i;
1553
Sean Young0751d332016-12-05 17:08:35 -02001554 mutex_lock(&dev->lock);
Sean Younga2e2d732020-08-08 13:38:02 +02001555 if (!dev->registered) {
1556 mutex_unlock(&dev->lock);
1557 return -ENODEV;
1558 }
Sean Young0751d332016-12-05 17:08:35 -02001559
1560 allowed = dev->allowed_wakeup_protocols;
1561
Mauro Carvalho Chehab3d351532019-05-23 05:27:30 -04001562 if (!sysfs_streq(buf, "none")) {
Sean Youngd57ea872017-08-09 13:19:16 -04001563 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001564 if ((allowed & (1ULL << i)) &&
Sean Youngd57ea872017-08-09 13:19:16 -04001565 sysfs_streq(buf, protocols[i].name)) {
Sean Young0751d332016-12-05 17:08:35 -02001566 protocol = i;
1567 break;
1568 }
1569 }
1570
Sean Youngd57ea872017-08-09 13:19:16 -04001571 if (i == ARRAY_SIZE(protocols)) {
Sean Young0751d332016-12-05 17:08:35 -02001572 rc = -EINVAL;
1573 goto out;
1574 }
James Hoganf423ccc2015-03-31 14:48:10 -03001575
1576 if (dev->encode_wakeup) {
1577 u64 mask = 1ULL << protocol;
1578
1579 ir_raw_load_modules(&mask);
1580 if (!mask) {
1581 rc = -EINVAL;
1582 goto out;
1583 }
1584 }
Sean Young0751d332016-12-05 17:08:35 -02001585 }
1586
1587 if (dev->wakeup_protocol != protocol) {
1588 dev->wakeup_protocol = protocol;
Sean Young1f17f682018-02-12 07:27:50 -05001589 dev_dbg(&dev->dev, "Wakeup protocol changed to %d\n", protocol);
Sean Young0751d332016-12-05 17:08:35 -02001590
Sean Young6d741bf2017-08-07 16:20:58 -04001591 if (protocol == RC_PROTO_RC6_MCE)
Sean Young0751d332016-12-05 17:08:35 -02001592 dev->scancode_wakeup_filter.data = 0x800f0000;
1593 else
1594 dev->scancode_wakeup_filter.data = 0;
1595 dev->scancode_wakeup_filter.mask = 0;
1596
1597 rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
1598 if (rc == 0)
1599 rc = len;
1600 } else {
1601 rc = len;
1602 }
1603
1604out:
1605 mutex_unlock(&dev->lock);
1606 return rc;
1607}
1608
David Härdemand8b4b582010-10-29 16:08:23 -03001609static void rc_dev_release(struct device *device)
1610{
Max Kellermann47cae1e2016-03-21 08:33:05 -03001611 struct rc_dev *dev = to_rc_dev(device);
1612
1613 kfree(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001614}
1615
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001616static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1617{
David Härdemand8b4b582010-10-29 16:08:23 -03001618 struct rc_dev *dev = to_rc_dev(device);
Sean Young4f0835d2020-08-08 13:19:12 +02001619 int ret = 0;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001620
Sean Young4f0835d2020-08-08 13:19:12 +02001621 mutex_lock(&dev->lock);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001622
Sean Young4f0835d2020-08-08 13:19:12 +02001623 if (!dev->registered)
1624 ret = -ENODEV;
1625 if (ret == 0 && dev->rc_map.name)
1626 ret = add_uevent_var(env, "NAME=%s", dev->rc_map.name);
1627 if (ret == 0 && dev->driver_name)
1628 ret = add_uevent_var(env, "DRV_NAME=%s", dev->driver_name);
1629 if (ret == 0 && dev->device_name)
1630 ret = add_uevent_var(env, "DEV_NAME=%s", dev->device_name);
1631
1632 mutex_unlock(&dev->lock);
1633
1634 return ret;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001635}
1636
1637/*
1638 * Static device attribute struct with the sysfs attributes for IR's
1639 */
Sean Young6d75db32017-09-01 11:30:50 -03001640static struct device_attribute dev_attr_ro_protocols =
1641__ATTR(protocols, 0444, show_protocols, NULL);
1642static struct device_attribute dev_attr_rw_protocols =
1643__ATTR(protocols, 0644, show_protocols, store_protocols);
Sean Young0751d332016-12-05 17:08:35 -02001644static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
1645 store_wakeup_protocols);
James Hogan00942d12014-01-17 10:58:49 -03001646static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
1647 show_filter, store_filter, RC_FILTER_NORMAL, false);
1648static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
1649 show_filter, store_filter, RC_FILTER_NORMAL, true);
1650static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
1651 show_filter, store_filter, RC_FILTER_WAKEUP, false);
1652static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
1653 show_filter, store_filter, RC_FILTER_WAKEUP, true);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001654
Sean Young6d75db32017-09-01 11:30:50 -03001655static struct attribute *rc_dev_rw_protocol_attrs[] = {
1656 &dev_attr_rw_protocols.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001657 NULL,
1658};
1659
Sean Young6d75db32017-09-01 11:30:50 -03001660static const struct attribute_group rc_dev_rw_protocol_attr_grp = {
1661 .attrs = rc_dev_rw_protocol_attrs,
1662};
1663
1664static struct attribute *rc_dev_ro_protocol_attrs[] = {
1665 &dev_attr_ro_protocols.attr,
1666 NULL,
1667};
1668
1669static const struct attribute_group rc_dev_ro_protocol_attr_grp = {
1670 .attrs = rc_dev_ro_protocol_attrs,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001671};
1672
David Härdeman99b0f3c2014-04-04 19:06:06 -03001673static struct attribute *rc_dev_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001674 &dev_attr_filter.attr.attr,
1675 &dev_attr_filter_mask.attr.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001676 NULL,
1677};
1678
Arvind Yadavdb681022017-07-07 04:23:54 -04001679static const struct attribute_group rc_dev_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001680 .attrs = rc_dev_filter_attrs,
1681};
1682
1683static struct attribute *rc_dev_wakeup_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001684 &dev_attr_wakeup_filter.attr.attr,
1685 &dev_attr_wakeup_filter_mask.attr.attr,
Sean Young0751d332016-12-05 17:08:35 -02001686 &dev_attr_wakeup_protocols.attr,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001687 NULL,
1688};
1689
Arvind Yadavdb681022017-07-07 04:23:54 -04001690static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001691 .attrs = rc_dev_wakeup_filter_attrs,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001692};
1693
Bhumika Goyalf03f02f2017-08-19 05:22:15 -03001694static const struct device_type rc_dev_type = {
David Härdemand8b4b582010-10-29 16:08:23 -03001695 .release = rc_dev_release,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001696 .uevent = rc_dev_uevent,
1697};
1698
Andi Shyti0f7499f2016-12-16 06:50:58 -02001699struct rc_dev *rc_allocate_device(enum rc_driver_type type)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001700{
David Härdemand8b4b582010-10-29 16:08:23 -03001701 struct rc_dev *dev;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001702
David Härdemand8b4b582010-10-29 16:08:23 -03001703 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1704 if (!dev)
1705 return NULL;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001706
Andi Shytid34aee12016-12-16 04:12:15 -02001707 if (type != RC_DRIVER_IR_RAW_TX) {
1708 dev->input_dev = input_allocate_device();
1709 if (!dev->input_dev) {
1710 kfree(dev);
1711 return NULL;
1712 }
1713
1714 dev->input_dev->getkeycode = ir_getkeycode;
1715 dev->input_dev->setkeycode = ir_setkeycode;
1716 input_set_drvdata(dev->input_dev, dev);
1717
Sean Young28492252018-03-24 08:02:48 -04001718 dev->timeout = IR_DEFAULT_TIMEOUT;
Kees Cookb17ec782017-10-24 11:23:14 -04001719 timer_setup(&dev->timer_keyup, ir_timer_keyup, 0);
Sean Young57c642c2017-11-23 17:37:10 -05001720 timer_setup(&dev->timer_repeat, ir_timer_repeat, 0);
Andi Shytid34aee12016-12-16 04:12:15 -02001721
1722 spin_lock_init(&dev->rc_map.lock);
1723 spin_lock_init(&dev->keylock);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001724 }
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001725 mutex_init(&dev->lock);
David Härdemand8b4b582010-10-29 16:08:23 -03001726
1727 dev->dev.type = &rc_dev_type;
David Härdeman40fc5322013-03-06 16:52:10 -03001728 dev->dev.class = &rc_class;
David Härdemand8b4b582010-10-29 16:08:23 -03001729 device_initialize(&dev->dev);
1730
Andi Shyti0f7499f2016-12-16 06:50:58 -02001731 dev->driver_type = type;
1732
David Härdemand8b4b582010-10-29 16:08:23 -03001733 __module_get(THIS_MODULE);
1734 return dev;
1735}
1736EXPORT_SYMBOL_GPL(rc_allocate_device);
1737
1738void rc_free_device(struct rc_dev *dev)
1739{
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001740 if (!dev)
1741 return;
1742
Markus Elfring3dd94f02014-11-20 09:01:32 -03001743 input_free_device(dev->input_dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001744
1745 put_device(&dev->dev);
1746
Max Kellermann47cae1e2016-03-21 08:33:05 -03001747 /* kfree(dev) will be called by the callback function
1748 rc_dev_release() */
1749
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001750 module_put(THIS_MODULE);
David Härdemand8b4b582010-10-29 16:08:23 -03001751}
1752EXPORT_SYMBOL_GPL(rc_free_device);
1753
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001754static void devm_rc_alloc_release(struct device *dev, void *res)
1755{
1756 rc_free_device(*(struct rc_dev **)res);
1757}
1758
Andi Shyti0f7499f2016-12-16 06:50:58 -02001759struct rc_dev *devm_rc_allocate_device(struct device *dev,
1760 enum rc_driver_type type)
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001761{
1762 struct rc_dev **dr, *rc;
1763
1764 dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
1765 if (!dr)
1766 return NULL;
1767
Andi Shyti0f7499f2016-12-16 06:50:58 -02001768 rc = rc_allocate_device(type);
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001769 if (!rc) {
1770 devres_free(dr);
1771 return NULL;
1772 }
1773
1774 rc->dev.parent = dev;
1775 rc->managed_alloc = true;
1776 *dr = rc;
1777 devres_add(dev, dr);
1778
1779 return rc;
1780}
1781EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
1782
David Härdemanf56928a2017-05-03 07:04:00 -03001783static int rc_prepare_rx_device(struct rc_dev *dev)
David Härdemand8b4b582010-10-29 16:08:23 -03001784{
David Härdemanfcb13092015-05-19 19:03:17 -03001785 int rc;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001786 struct rc_map *rc_map;
Sean Young6d741bf2017-08-07 16:20:58 -04001787 u64 rc_proto;
David Härdemand8b4b582010-10-29 16:08:23 -03001788
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001789 if (!dev->map_name)
David Härdemand8b4b582010-10-29 16:08:23 -03001790 return -EINVAL;
1791
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001792 rc_map = rc_map_get(dev->map_name);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001793 if (!rc_map)
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001794 rc_map = rc_map_get(RC_MAP_EMPTY);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001795 if (!rc_map || !rc_map->scan || rc_map->size == 0)
David Härdemand8b4b582010-10-29 16:08:23 -03001796 return -EINVAL;
1797
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001798 rc = ir_setkeytable(dev, rc_map);
1799 if (rc)
1800 return rc;
1801
Sean Young6d741bf2017-08-07 16:20:58 -04001802 rc_proto = BIT_ULL(rc_map->rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001803
Sean Young831c4c82017-09-01 09:55:59 -03001804 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1805 dev->enabled_protocols = dev->allowed_protocols;
1806
Sean Younga86d6df2018-03-23 16:47:37 -04001807 if (dev->driver_type == RC_DRIVER_IR_RAW)
1808 ir_raw_load_modules(&rc_proto);
1809
Sean Young41380862017-02-22 18:48:01 -03001810 if (dev->change_protocol) {
Sean Young6d741bf2017-08-07 16:20:58 -04001811 rc = dev->change_protocol(dev, &rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001812 if (rc < 0)
1813 goto out_table;
Sean Young6d741bf2017-08-07 16:20:58 -04001814 dev->enabled_protocols = rc_proto;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001815 }
1816
Sean Young0ac5a602018-09-16 07:35:58 -04001817 /* Keyboard events */
David Härdemand8b4b582010-10-29 16:08:23 -03001818 set_bit(EV_KEY, dev->input_dev->evbit);
1819 set_bit(EV_REP, dev->input_dev->evbit);
1820 set_bit(EV_MSC, dev->input_dev->evbit);
1821 set_bit(MSC_SCAN, dev->input_dev->mscbit);
Sean Youngfec225a2018-09-18 04:50:20 -04001822
Sean Young0ac5a602018-09-16 07:35:58 -04001823 /* Pointer/mouse events */
Sean Youngce819642019-09-28 17:46:14 -03001824 set_bit(INPUT_PROP_POINTING_STICK, dev->input_dev->propbit);
Sean Young0ac5a602018-09-16 07:35:58 -04001825 set_bit(EV_REL, dev->input_dev->evbit);
1826 set_bit(REL_X, dev->input_dev->relbit);
1827 set_bit(REL_Y, dev->input_dev->relbit);
1828
David Härdemand8b4b582010-10-29 16:08:23 -03001829 if (dev->open)
1830 dev->input_dev->open = ir_open;
1831 if (dev->close)
1832 dev->input_dev->close = ir_close;
1833
David Härdemanb2aceb72017-04-27 17:33:58 -03001834 dev->input_dev->dev.parent = &dev->dev;
1835 memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
1836 dev->input_dev->phys = dev->input_phys;
Sean Young518f4b22017-07-01 12:13:19 -04001837 dev->input_dev->name = dev->device_name;
David Härdemanb2aceb72017-04-27 17:33:58 -03001838
David Härdemanf56928a2017-05-03 07:04:00 -03001839 return 0;
1840
1841out_table:
1842 ir_free_table(&dev->rc_map);
1843
1844 return rc;
1845}
1846
1847static int rc_setup_rx_device(struct rc_dev *dev)
1848{
1849 int rc;
1850
David Härdemanb2aceb72017-04-27 17:33:58 -03001851 /* rc_open will be called here */
1852 rc = input_register_device(dev->input_dev);
1853 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001854 return rc;
David Härdemanb2aceb72017-04-27 17:33:58 -03001855
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001856 /*
1857 * Default delay of 250ms is too short for some protocols, especially
1858 * since the timeout is currently set to 250ms. Increase it to 500ms,
1859 * to avoid wrong repetition of the keycodes. Note that this must be
1860 * set after the call to input_register_device().
1861 */
Sean Young57c642c2017-11-23 17:37:10 -05001862 if (dev->allowed_protocols == RC_PROTO_BIT_CEC)
1863 dev->input_dev->rep[REP_DELAY] = 0;
1864 else
1865 dev->input_dev->rep[REP_DELAY] = 500;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001866
1867 /*
1868 * As a repeat event on protocols like RC-5 and NEC take as long as
1869 * 110/114ms, using 33ms as a repeat period is not the right thing
1870 * to do.
1871 */
1872 dev->input_dev->rep[REP_PERIOD] = 125;
1873
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001874 return 0;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001875}
1876
1877static void rc_free_rx_device(struct rc_dev *dev)
1878{
David Härdemanf56928a2017-05-03 07:04:00 -03001879 if (!dev)
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001880 return;
1881
David Härdemanf56928a2017-05-03 07:04:00 -03001882 if (dev->input_dev) {
1883 input_unregister_device(dev->input_dev);
1884 dev->input_dev = NULL;
1885 }
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001886
David Härdemanf56928a2017-05-03 07:04:00 -03001887 ir_free_table(&dev->rc_map);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001888}
1889
1890int rc_register_device(struct rc_dev *dev)
1891{
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001892 const char *path;
1893 int attr = 0;
1894 int minor;
1895 int rc;
1896
1897 if (!dev)
1898 return -EINVAL;
1899
David Härdemanfcb13092015-05-19 19:03:17 -03001900 minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
1901 if (minor < 0)
1902 return minor;
1903
1904 dev->minor = minor;
1905 dev_set_name(&dev->dev, "rc%u", dev->minor);
1906 dev_set_drvdata(&dev->dev, dev);
Mauro Carvalho Chehab587d1b02014-01-14 16:27:55 -03001907
David Härdeman99b0f3c2014-04-04 19:06:06 -03001908 dev->dev.groups = dev->sysfs_groups;
Sean Young6d75db32017-09-01 11:30:50 -03001909 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1910 dev->sysfs_groups[attr++] = &rc_dev_ro_protocol_attr_grp;
1911 else if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
1912 dev->sysfs_groups[attr++] = &rc_dev_rw_protocol_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001913 if (dev->s_filter)
David Härdeman120703f2014-04-03 20:31:30 -03001914 dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001915 if (dev->s_wakeup_filter)
1916 dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001917 dev->sysfs_groups[attr++] = NULL;
1918
Sean Younga60d64b2017-09-23 10:41:13 -04001919 if (dev->driver_type == RC_DRIVER_IR_RAW) {
David Härdemanf56928a2017-05-03 07:04:00 -03001920 rc = ir_raw_event_prepare(dev);
1921 if (rc < 0)
1922 goto out_minor;
1923 }
1924
1925 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1926 rc = rc_prepare_rx_device(dev);
1927 if (rc)
1928 goto out_raw;
1929 }
1930
Sean Young896111d2020-12-20 13:29:54 +01001931 dev->registered = true;
1932
David Härdemand8b4b582010-10-29 16:08:23 -03001933 rc = device_add(&dev->dev);
1934 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001935 goto out_rx_free;
David Härdemand8b4b582010-10-29 16:08:23 -03001936
David Härdemand8b4b582010-10-29 16:08:23 -03001937 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Heiner Kallweit4dc0e902015-10-29 19:39:06 -02001938 dev_info(&dev->dev, "%s as %s\n",
Sean Young518f4b22017-07-01 12:13:19 -04001939 dev->device_name ?: "Unspecified device", path ?: "N/A");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001940 kfree(path);
1941
Sean Young080d89f2019-11-21 11:10:47 +01001942 /*
1943 * once the the input device is registered in rc_setup_rx_device,
1944 * userspace can open the input device and rc_open() will be called
1945 * as a result. This results in driver code being allowed to submit
1946 * keycodes with rc_keydown, so lirc must be registered first.
1947 */
Sean Young62d6f1992017-09-24 12:43:24 -04001948 if (dev->allowed_protocols != RC_PROTO_BIT_CEC) {
Sean Young75992a42020-08-24 21:10:45 +02001949 rc = lirc_register(dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001950 if (rc < 0)
Sean Young080d89f2019-11-21 11:10:47 +01001951 goto out_dev;
1952 }
1953
1954 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1955 rc = rc_setup_rx_device(dev);
1956 if (rc)
1957 goto out_lirc;
David Härdemand8b4b582010-10-29 16:08:23 -03001958 }
1959
Sean Younga60d64b2017-09-23 10:41:13 -04001960 if (dev->driver_type == RC_DRIVER_IR_RAW) {
1961 rc = ir_raw_event_register(dev);
1962 if (rc < 0)
Sean Young080d89f2019-11-21 11:10:47 +01001963 goto out_rx;
Sean Younga60d64b2017-09-23 10:41:13 -04001964 }
1965
Sean Young1f17f682018-02-12 07:27:50 -05001966 dev_dbg(&dev->dev, "Registered rc%u (driver: %s)\n", dev->minor,
1967 dev->driver_name ? dev->driver_name : "unknown");
David Härdemand8b4b582010-10-29 16:08:23 -03001968
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001969 return 0;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001970
Sean Young080d89f2019-11-21 11:10:47 +01001971out_rx:
1972 rc_free_rx_device(dev);
Sean Younga60d64b2017-09-23 10:41:13 -04001973out_lirc:
Sean Young62d6f1992017-09-24 12:43:24 -04001974 if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
Sean Young75992a42020-08-24 21:10:45 +02001975 lirc_unregister(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001976out_dev:
1977 device_del(&dev->dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001978out_rx_free:
1979 ir_free_table(&dev->rc_map);
1980out_raw:
1981 ir_raw_event_free(dev);
1982out_minor:
David Härdemanfcb13092015-05-19 19:03:17 -03001983 ida_simple_remove(&rc_ida, minor);
David Härdemand8b4b582010-10-29 16:08:23 -03001984 return rc;
1985}
1986EXPORT_SYMBOL_GPL(rc_register_device);
1987
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001988static void devm_rc_release(struct device *dev, void *res)
1989{
1990 rc_unregister_device(*(struct rc_dev **)res);
1991}
1992
1993int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
1994{
1995 struct rc_dev **dr;
1996 int ret;
1997
1998 dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
1999 if (!dr)
2000 return -ENOMEM;
2001
2002 ret = rc_register_device(dev);
2003 if (ret) {
2004 devres_free(dr);
2005 return ret;
2006 }
2007
2008 *dr = dev;
2009 devres_add(parent, dr);
2010
2011 return 0;
2012}
2013EXPORT_SYMBOL_GPL(devm_rc_register_device);
2014
David Härdemand8b4b582010-10-29 16:08:23 -03002015void rc_unregister_device(struct rc_dev *dev)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002016{
David Härdemand8b4b582010-10-29 16:08:23 -03002017 if (!dev)
2018 return;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002019
David Härdemand8b4b582010-10-29 16:08:23 -03002020 if (dev->driver_type == RC_DRIVER_IR_RAW)
2021 ir_raw_event_unregister(dev);
2022
Sean Young8d406882018-03-06 08:57:57 -05002023 del_timer_sync(&dev->timer_keyup);
2024 del_timer_sync(&dev->timer_repeat);
2025
Sean Young7790e812017-09-26 07:31:29 -04002026 mutex_lock(&dev->lock);
Sean Young8e782fc2018-11-04 05:12:09 -05002027 if (dev->users && dev->close)
2028 dev->close(dev);
Sean Young7790e812017-09-26 07:31:29 -04002029 dev->registered = false;
2030 mutex_unlock(&dev->lock);
2031
Sean Young4f0835d2020-08-08 13:19:12 +02002032 rc_free_rx_device(dev);
2033
Sean Young7790e812017-09-26 07:31:29 -04002034 /*
2035 * lirc device should be freed with dev->registered = false, so
2036 * that userspace polling will get notified.
2037 */
Sean Young62d6f1992017-09-24 12:43:24 -04002038 if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
Sean Young75992a42020-08-24 21:10:45 +02002039 lirc_unregister(dev);
Sean Younga60d64b2017-09-23 10:41:13 -04002040
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03002041 device_del(&dev->dev);
2042
David Härdemanfcb13092015-05-19 19:03:17 -03002043 ida_simple_remove(&rc_ida, dev->minor);
2044
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03002045 if (!dev->managed_alloc)
2046 rc_free_device(dev);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002047}
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03002048
David Härdemand8b4b582010-10-29 16:08:23 -03002049EXPORT_SYMBOL_GPL(rc_unregister_device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002050
2051/*
2052 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
2053 */
2054
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03002055static int __init rc_core_init(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002056{
David Härdeman40fc5322013-03-06 16:52:10 -03002057 int rc = class_register(&rc_class);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002058 if (rc) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -02002059 pr_err("rc_core: unable to register rc class\n");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002060 return rc;
2061 }
2062
Sean Younga60d64b2017-09-23 10:41:13 -04002063 rc = lirc_dev_init();
2064 if (rc) {
2065 pr_err("rc_core: unable to init lirc\n");
2066 class_unregister(&rc_class);
Evgeny Novikov3b4cfc62020-06-18 15:15:38 +02002067 return rc;
Sean Younga60d64b2017-09-23 10:41:13 -04002068 }
2069
Sean Young153a60b2013-07-30 19:00:01 -03002070 led_trigger_register_simple("rc-feedback", &led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03002071 rc_map_register(&empty_map);
Hans Verkuilf09f9f92021-02-26 11:37:47 +01002072#ifdef CONFIG_MEDIA_CEC_RC
2073 rc_map_register(&cec_map);
2074#endif
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002075
2076 return 0;
2077}
2078
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03002079static void __exit rc_core_exit(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002080{
Sean Younga60d64b2017-09-23 10:41:13 -04002081 lirc_dev_exit();
David Härdeman40fc5322013-03-06 16:52:10 -03002082 class_unregister(&rc_class);
Sean Young153a60b2013-07-30 19:00:01 -03002083 led_trigger_unregister_simple(led_feedback);
Hans Verkuilf09f9f92021-02-26 11:37:47 +01002084#ifdef CONFIG_MEDIA_CEC_RC
2085 rc_map_unregister(&cec_map);
2086#endif
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03002087 rc_map_unregister(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002088}
2089
David Härdemane76d4ce2013-03-06 16:52:15 -03002090subsys_initcall(rc_core_init);
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03002091module_exit(rc_core_exit);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002092
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02002093MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehab20835282017-12-01 08:47:08 -05002094MODULE_LICENSE("GPL v2");