blob: 6f80c251f64136a78e2f5565555ce04f925bd81f [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/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700167 * ir_create_table() - initializes a scancode table
Sean Young1f17f682018-02-12 07:27:50 -0500168 * @dev: the rc_dev device
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300169 * @rc_map: the rc_map to initialize
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700170 * @name: name to assign to the table
Sean Young6d741bf2017-08-07 16:20:58 -0400171 * @rc_proto: ir type to assign to the new table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700172 * @size: initial size of the table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700173 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300174 * This routine will initialize the rc_map and will allocate
David Härdemand8b4b582010-10-29 16:08:23 -0300175 * memory to hold at least the specified number of elements.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500176 *
177 * return: zero on success or a negative error code
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700178 */
Sean Young1f17f682018-02-12 07:27:50 -0500179static int ir_create_table(struct rc_dev *dev, struct rc_map *rc_map,
Sean Young6d741bf2017-08-07 16:20:58 -0400180 const char *name, u64 rc_proto, size_t size)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700181{
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300182 rc_map->name = kstrdup(name, GFP_KERNEL);
183 if (!rc_map->name)
184 return -ENOMEM;
Sean Young6d741bf2017-08-07 16:20:58 -0400185 rc_map->rc_proto = rc_proto;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300186 rc_map->alloc = roundup_pow_of_two(size * sizeof(struct rc_map_table));
187 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300188 rc_map->scan = kmalloc(rc_map->alloc, GFP_KERNEL);
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300189 if (!rc_map->scan) {
190 kfree(rc_map->name);
191 rc_map->name = NULL;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700192 return -ENOMEM;
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300193 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700194
Sean Young1f17f682018-02-12 07:27:50 -0500195 dev_dbg(&dev->dev, "Allocated space for %u keycode entries (%u bytes)\n",
196 rc_map->size, rc_map->alloc);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700197 return 0;
198}
199
200/**
201 * ir_free_table() - frees memory allocated by a scancode table
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300202 * @rc_map: the table whose mappings need to be freed
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700203 *
204 * This routine will free memory alloctaed for key mappings used by given
205 * scancode table.
206 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300207static void ir_free_table(struct rc_map *rc_map)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700208{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300209 rc_map->size = 0;
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300210 kfree(rc_map->name);
Max Kellermannc183d352016-08-09 18:32:06 -0300211 rc_map->name = NULL;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300212 kfree(rc_map->scan);
213 rc_map->scan = NULL;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700214}
215
216/**
David Härdemanb3074c02010-04-02 15:58:28 -0300217 * ir_resize_table() - resizes a scancode table if necessary
Sean Young1f17f682018-02-12 07:27:50 -0500218 * @dev: the rc_dev device
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300219 * @rc_map: the rc_map to resize
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700220 * @gfp_flags: gfp flags to use when allocating memory
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300221 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300222 * This routine will shrink the rc_map if it has lots of
David Härdemanb3074c02010-04-02 15:58:28 -0300223 * unused entries and grow it if it is full.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500224 *
225 * return: zero on success or a negative error code
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300226 */
Sean Young1f17f682018-02-12 07:27:50 -0500227static int ir_resize_table(struct rc_dev *dev, struct rc_map *rc_map,
228 gfp_t gfp_flags)
David Härdemanb3074c02010-04-02 15:58:28 -0300229{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300230 unsigned int oldalloc = rc_map->alloc;
David Härdemanb3074c02010-04-02 15:58:28 -0300231 unsigned int newalloc = oldalloc;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300232 struct rc_map_table *oldscan = rc_map->scan;
233 struct rc_map_table *newscan;
David Härdemanb3074c02010-04-02 15:58:28 -0300234
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300235 if (rc_map->size == rc_map->len) {
David Härdemanb3074c02010-04-02 15:58:28 -0300236 /* All entries in use -> grow keytable */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300237 if (rc_map->alloc >= IR_TAB_MAX_SIZE)
David Härdemanb3074c02010-04-02 15:58:28 -0300238 return -ENOMEM;
239
240 newalloc *= 2;
Sean Young1f17f682018-02-12 07:27:50 -0500241 dev_dbg(&dev->dev, "Growing table to %u bytes\n", newalloc);
David Härdemanb3074c02010-04-02 15:58:28 -0300242 }
243
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300244 if ((rc_map->len * 3 < rc_map->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
David Härdemanb3074c02010-04-02 15:58:28 -0300245 /* Less than 1/3 of entries in use -> shrink keytable */
246 newalloc /= 2;
Sean Young1f17f682018-02-12 07:27:50 -0500247 dev_dbg(&dev->dev, "Shrinking table to %u bytes\n", newalloc);
David Härdemanb3074c02010-04-02 15:58:28 -0300248 }
249
250 if (newalloc == oldalloc)
251 return 0;
252
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700253 newscan = kmalloc(newalloc, gfp_flags);
Sean Young1f17f682018-02-12 07:27:50 -0500254 if (!newscan)
David Härdemanb3074c02010-04-02 15:58:28 -0300255 return -ENOMEM;
David Härdemanb3074c02010-04-02 15:58:28 -0300256
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300257 memcpy(newscan, rc_map->scan, rc_map->len * sizeof(struct rc_map_table));
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300258 rc_map->scan = newscan;
259 rc_map->alloc = newalloc;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300260 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
David Härdemanb3074c02010-04-02 15:58:28 -0300261 kfree(oldscan);
262 return 0;
263}
264
265/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700266 * ir_update_mapping() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300267 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300268 * @rc_map: scancode table to be adjusted
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700269 * @index: index of the mapping that needs to be updated
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500270 * @new_keycode: the desired keycode
David Härdemanb3074c02010-04-02 15:58:28 -0300271 *
David Härdemand8b4b582010-10-29 16:08:23 -0300272 * This routine is used to update scancode->keycode mapping at given
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700273 * position.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500274 *
275 * return: previous keycode assigned to the mapping
276 *
David Härdemanb3074c02010-04-02 15:58:28 -0300277 */
David Härdemand8b4b582010-10-29 16:08:23 -0300278static unsigned int ir_update_mapping(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300279 struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700280 unsigned int index,
281 unsigned int new_keycode)
282{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300283 int old_keycode = rc_map->scan[index].keycode;
Sean Young05f0eda2019-02-22 04:08:05 -0500284 int i;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700285
286 /* Did the user wish to remove the mapping? */
287 if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
Sean Young1f17f682018-02-12 07:27:50 -0500288 dev_dbg(&dev->dev, "#%d: Deleting scan 0x%04x\n",
289 index, rc_map->scan[index].scancode);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300290 rc_map->len--;
291 memmove(&rc_map->scan[index], &rc_map->scan[index+ 1],
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300292 (rc_map->len - index) * sizeof(struct rc_map_table));
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700293 } else {
Sean Young1f17f682018-02-12 07:27:50 -0500294 dev_dbg(&dev->dev, "#%d: %s scan 0x%04x with key 0x%04x\n",
295 index,
296 old_keycode == KEY_RESERVED ? "New" : "Replacing",
297 rc_map->scan[index].scancode, new_keycode);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300298 rc_map->scan[index].keycode = new_keycode;
Sean Young05f0eda2019-02-22 04:08:05 -0500299 __set_bit(new_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700300 }
301
302 if (old_keycode != KEY_RESERVED) {
Sean Young05f0eda2019-02-22 04:08:05 -0500303 /* A previous mapping was updated... */
304 __clear_bit(old_keycode, dev->input_dev->keybit);
305 /* ... but another scancode might use the same keycode */
306 for (i = 0; i < rc_map->len; i++) {
307 if (rc_map->scan[i].keycode == old_keycode) {
308 __set_bit(old_keycode, dev->input_dev->keybit);
309 break;
310 }
311 }
312
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700313 /* Possibly shrink the keytable, failure is not a problem */
Sean Young1f17f682018-02-12 07:27:50 -0500314 ir_resize_table(dev, rc_map, GFP_ATOMIC);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700315 }
316
317 return old_keycode;
318}
319
320/**
David Härdeman4c7b3552010-11-10 11:04:19 -0300321 * ir_establish_scancode() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300322 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300323 * @rc_map: scancode table to be searched
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700324 * @scancode: the desired scancode
325 * @resize: controls whether we allowed to resize the table to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300326 * accommodate not yet present scancodes
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700327 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300328 * This routine is used to locate given scancode in rc_map.
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700329 * If scancode is not yet present the routine will allocate a new slot
330 * for it.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500331 *
332 * return: index of the mapping containing scancode in question
333 * or -1U in case of failure.
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700334 */
David Härdemand8b4b582010-10-29 16:08:23 -0300335static unsigned int ir_establish_scancode(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300336 struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700337 unsigned int scancode,
338 bool resize)
David Härdemanb3074c02010-04-02 15:58:28 -0300339{
340 unsigned int i;
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300341
342 /*
343 * Unfortunately, some hardware-based IR decoders don't provide
344 * all bits for the complete IR code. In general, they provide only
345 * the command part of the IR code. Yet, as it is possible to replace
346 * the provided IR with another one, it is needed to allow loading
David Härdemand8b4b582010-10-29 16:08:23 -0300347 * IR tables from other remotes. So, we support specifying a mask to
348 * indicate the valid bits of the scancodes.
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300349 */
David Härdeman9d2f1d32014-04-03 20:32:26 -0300350 if (dev->scancode_mask)
351 scancode &= dev->scancode_mask;
David Härdemanb3074c02010-04-02 15:58:28 -0300352
353 /* First check if we already have a mapping for this ir command */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300354 for (i = 0; i < rc_map->len; i++) {
355 if (rc_map->scan[i].scancode == scancode)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700356 return i;
357
David Härdemanb3074c02010-04-02 15:58:28 -0300358 /* Keytable is sorted from lowest to highest scancode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300359 if (rc_map->scan[i].scancode >= scancode)
David Härdemanb3074c02010-04-02 15:58:28 -0300360 break;
David Härdemanb3074c02010-04-02 15:58:28 -0300361 }
362
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700363 /* No previous mapping found, we might need to grow the table */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300364 if (rc_map->size == rc_map->len) {
Sean Young1f17f682018-02-12 07:27:50 -0500365 if (!resize || ir_resize_table(dev, rc_map, GFP_ATOMIC))
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700366 return -1U;
367 }
David Härdemanb3074c02010-04-02 15:58:28 -0300368
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700369 /* i is the proper index to insert our new keycode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300370 if (i < rc_map->len)
371 memmove(&rc_map->scan[i + 1], &rc_map->scan[i],
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300372 (rc_map->len - i) * sizeof(struct rc_map_table));
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300373 rc_map->scan[i].scancode = scancode;
374 rc_map->scan[i].keycode = KEY_RESERVED;
375 rc_map->len++;
David Härdemanb3074c02010-04-02 15:58:28 -0300376
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700377 return i;
David Härdemanb3074c02010-04-02 15:58:28 -0300378}
379
380/**
381 * ir_setkeycode() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300382 * @idev: the struct input_dev device descriptor
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500383 * @ke: Input keymap entry
384 * @old_keycode: result
David Härdemanb3074c02010-04-02 15:58:28 -0300385 *
386 * This routine is used to handle evdev EVIOCSKEY ioctl.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500387 *
388 * return: -EINVAL if the keycode could not be inserted, otherwise zero.
David Härdemanb3074c02010-04-02 15:58:28 -0300389 */
David Härdemand8b4b582010-10-29 16:08:23 -0300390static int ir_setkeycode(struct input_dev *idev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700391 const struct input_keymap_entry *ke,
392 unsigned int *old_keycode)
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300393{
David Härdemand8b4b582010-10-29 16:08:23 -0300394 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300395 struct rc_map *rc_map = &rdev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700396 unsigned int index;
397 unsigned int scancode;
Mauro Carvalho Chehabdea8a392010-11-29 07:46:13 -0300398 int retval = 0;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700399 unsigned long flags;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300400
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300401 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700402
403 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
404 index = ke->index;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300405 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700406 retval = -EINVAL;
407 goto out;
408 }
409 } else {
410 retval = input_scancode_to_scalar(ke, &scancode);
411 if (retval)
412 goto out;
413
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300414 index = ir_establish_scancode(rdev, rc_map, scancode, true);
415 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700416 retval = -ENOMEM;
417 goto out;
418 }
419 }
420
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300421 *old_keycode = ir_update_mapping(rdev, rc_map, index, ke->keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700422
423out:
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300424 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700425 return retval;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300426}
427
428/**
David Härdemanb3074c02010-04-02 15:58:28 -0300429 * ir_setkeytable() - sets several entries in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300430 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300431 * @from: the struct rc_map to copy entries from
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300432 *
David Härdemanb3074c02010-04-02 15:58:28 -0300433 * This routine is used to handle table initialization.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500434 *
435 * return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300436 */
David Härdemand8b4b582010-10-29 16:08:23 -0300437static int ir_setkeytable(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300438 const struct rc_map *from)
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300439{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300440 struct rc_map *rc_map = &dev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700441 unsigned int i, index;
442 int rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300443
Sean Young1f17f682018-02-12 07:27:50 -0500444 rc = ir_create_table(dev, rc_map, from->name, from->rc_proto,
445 from->size);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700446 if (rc)
447 return rc;
448
David Härdemanb3074c02010-04-02 15:58:28 -0300449 for (i = 0; i < from->size; i++) {
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300450 index = ir_establish_scancode(dev, rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700451 from->scan[i].scancode, false);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300452 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700453 rc = -ENOMEM;
David Härdemanb3074c02010-04-02 15:58:28 -0300454 break;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700455 }
456
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300457 ir_update_mapping(dev, rc_map, index,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700458 from->scan[i].keycode);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300459 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700460
461 if (rc)
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300462 ir_free_table(rc_map);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700463
David Härdemanb3074c02010-04-02 15:58:28 -0300464 return rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300465}
466
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300467static int rc_map_cmp(const void *key, const void *elt)
468{
469 const unsigned int *scancode = key;
470 const struct rc_map_table *e = elt;
471
472 if (*scancode < e->scancode)
473 return -1;
474 else if (*scancode > e->scancode)
475 return 1;
476 return 0;
477}
478
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300479/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700480 * ir_lookup_by_scancode() - locate mapping by scancode
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300481 * @rc_map: the struct rc_map to search
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700482 * @scancode: scancode to look for in the table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700483 *
484 * This routine performs binary search in RC keykeymap table for
485 * given scancode.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500486 *
487 * return: index in the table, -1U if not found
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700488 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300489static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700490 unsigned int scancode)
491{
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300492 struct rc_map_table *res;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700493
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300494 res = bsearch(&scancode, rc_map->scan, rc_map->len,
495 sizeof(struct rc_map_table), rc_map_cmp);
496 if (!res)
497 return -1U;
498 else
499 return res - rc_map->scan;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700500}
501
502/**
David Härdemanb3074c02010-04-02 15:58:28 -0300503 * ir_getkeycode() - get a keycode from the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300504 * @idev: the struct input_dev device descriptor
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500505 * @ke: Input keymap entry
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300506 *
507 * This routine is used to handle evdev EVIOCGKEY ioctl.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500508 *
509 * return: always returns zero.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300510 */
David Härdemand8b4b582010-10-29 16:08:23 -0300511static int ir_getkeycode(struct input_dev *idev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700512 struct input_keymap_entry *ke)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300513{
David Härdemand8b4b582010-10-29 16:08:23 -0300514 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300515 struct rc_map *rc_map = &rdev->rc_map;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300516 struct rc_map_table *entry;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700517 unsigned long flags;
518 unsigned int index;
519 unsigned int scancode;
520 int retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300521
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300522 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700523
524 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
525 index = ke->index;
526 } else {
527 retval = input_scancode_to_scalar(ke, &scancode);
528 if (retval)
529 goto out;
530
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300531 index = ir_lookup_by_scancode(rc_map, scancode);
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300532 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700533
Dmitry Torokhov54e74b82011-01-28 23:33:29 -0800534 if (index < rc_map->len) {
535 entry = &rc_map->scan[index];
536
537 ke->index = index;
538 ke->keycode = entry->keycode;
539 ke->len = sizeof(entry->scancode);
540 memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
541
542 } else if (!(ke->flags & INPUT_KEYMAP_BY_INDEX)) {
543 /*
544 * We do not really know the valid range of scancodes
545 * so let's respond with KEY_RESERVED to anything we
546 * do not have mapping for [yet].
547 */
548 ke->index = index;
549 ke->keycode = KEY_RESERVED;
550 } else {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700551 retval = -EINVAL;
552 goto out;
553 }
554
Dmitry Torokhov47c5ba52010-10-31 15:18:42 -0700555 retval = 0;
556
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700557out:
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300558 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700559 return retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300560}
561
562/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300563 * rc_g_keycode_from_table() - gets the keycode that corresponds to a scancode
David Härdemand8b4b582010-10-29 16:08:23 -0300564 * @dev: the struct rc_dev descriptor of the device
565 * @scancode: the scancode to look for
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300566 *
David Härdemand8b4b582010-10-29 16:08:23 -0300567 * This routine is used by drivers which need to convert a scancode to a
568 * keycode. Normally it should not be used since drivers should have no
569 * interest in keycodes.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500570 *
571 * return: the corresponding keycode, or KEY_RESERVED
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300572 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300573u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300574{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300575 struct rc_map *rc_map = &dev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700576 unsigned int keycode;
577 unsigned int index;
578 unsigned long flags;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300579
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300580 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700581
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300582 index = ir_lookup_by_scancode(rc_map, scancode);
583 keycode = index < rc_map->len ?
584 rc_map->scan[index].keycode : KEY_RESERVED;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700585
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300586 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700587
Mauro Carvalho Chehab35438942010-04-03 16:53:16 -0300588 if (keycode != KEY_RESERVED)
Sean Young1f17f682018-02-12 07:27:50 -0500589 dev_dbg(&dev->dev, "%s: scancode 0x%04x keycode 0x%02x\n",
590 dev->device_name, scancode, keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700591
David Härdemanb3074c02010-04-02 15:58:28 -0300592 return keycode;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300593}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300594EXPORT_SYMBOL_GPL(rc_g_keycode_from_table);
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300595
596/**
David Härdeman62c65032010-10-29 16:08:07 -0300597 * ir_do_keyup() - internal function to signal the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300598 * @dev: the struct rc_dev descriptor of the device
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300599 * @sync: whether or not to call input_sync
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300600 *
David Härdeman62c65032010-10-29 16:08:07 -0300601 * This function is used internally to release a keypress, it must be
602 * called with keylock held.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300603 */
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300604static void ir_do_keyup(struct rc_dev *dev, bool sync)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300605{
David Härdemand8b4b582010-10-29 16:08:23 -0300606 if (!dev->keypressed)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300607 return;
608
Sean Young1f17f682018-02-12 07:27:50 -0500609 dev_dbg(&dev->dev, "keyup key 0x%04x\n", dev->last_keycode);
Sean Youngfb7ccc62017-12-03 08:55:24 -0500610 del_timer(&dev->timer_repeat);
David Härdemand8b4b582010-10-29 16:08:23 -0300611 input_report_key(dev->input_dev, dev->last_keycode, 0);
Sean Young153a60b2013-07-30 19:00:01 -0300612 led_trigger_event(led_feedback, LED_OFF);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300613 if (sync)
614 input_sync(dev->input_dev);
David Härdemand8b4b582010-10-29 16:08:23 -0300615 dev->keypressed = false;
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300616}
David Härdeman62c65032010-10-29 16:08:07 -0300617
618/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300619 * rc_keyup() - signals the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300620 * @dev: the struct rc_dev descriptor of the device
David Härdeman62c65032010-10-29 16:08:07 -0300621 *
622 * This routine is used to signal that a key has been released on the
623 * remote control.
624 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300625void rc_keyup(struct rc_dev *dev)
David Härdeman62c65032010-10-29 16:08:07 -0300626{
627 unsigned long flags;
David Härdeman62c65032010-10-29 16:08:07 -0300628
David Härdemand8b4b582010-10-29 16:08:23 -0300629 spin_lock_irqsave(&dev->keylock, flags);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300630 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300631 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300632}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300633EXPORT_SYMBOL_GPL(rc_keyup);
David Härdemana374fef2010-04-02 15:58:29 -0300634
635/**
636 * ir_timer_keyup() - generates a keyup event after a timeout
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500637 *
638 * @t: a pointer to the struct timer_list
David Härdemana374fef2010-04-02 15:58:29 -0300639 *
640 * This routine will generate a keyup event some time after a keydown event
641 * is generated when no further activity has been detected.
642 */
Kees Cookb17ec782017-10-24 11:23:14 -0400643static void ir_timer_keyup(struct timer_list *t)
David Härdemana374fef2010-04-02 15:58:29 -0300644{
Kees Cookb17ec782017-10-24 11:23:14 -0400645 struct rc_dev *dev = from_timer(dev, t, timer_keyup);
David Härdemana374fef2010-04-02 15:58:29 -0300646 unsigned long flags;
647
648 /*
649 * ir->keyup_jiffies is used to prevent a race condition if a
650 * hardware interrupt occurs at this point and the keyup timer
651 * event is moved further into the future as a result.
652 *
653 * The timer will then be reactivated and this function called
654 * again in the future. We need to exit gracefully in that case
655 * to allow the input subsystem to do its auto-repeat magic or
656 * a keyup event might follow immediately after the keydown.
657 */
David Härdemand8b4b582010-10-29 16:08:23 -0300658 spin_lock_irqsave(&dev->keylock, flags);
659 if (time_is_before_eq_jiffies(dev->keyup_jiffies))
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300660 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300661 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300662}
663
664/**
Sean Young57c642c2017-11-23 17:37:10 -0500665 * ir_timer_repeat() - generates a repeat event after a timeout
666 *
667 * @t: a pointer to the struct timer_list
668 *
669 * This routine will generate a soft repeat event every REP_PERIOD
670 * milliseconds.
671 */
672static void ir_timer_repeat(struct timer_list *t)
673{
674 struct rc_dev *dev = from_timer(dev, t, timer_repeat);
675 struct input_dev *input = dev->input_dev;
676 unsigned long flags;
677
678 spin_lock_irqsave(&dev->keylock, flags);
679 if (dev->keypressed) {
680 input_event(input, EV_KEY, dev->last_keycode, 2);
681 input_sync(input);
682 if (input->rep[REP_PERIOD])
683 mod_timer(&dev->timer_repeat, jiffies +
684 msecs_to_jiffies(input->rep[REP_PERIOD]));
685 }
686 spin_unlock_irqrestore(&dev->keylock, flags);
687}
688
Sean Youngf5dbee62018-07-28 05:11:15 -0400689static unsigned int repeat_period(int protocol)
690{
691 if (protocol >= ARRAY_SIZE(protocols))
692 return 100;
693
694 return protocols[protocol].repeat_period;
695}
696
Sean Young57c642c2017-11-23 17:37:10 -0500697/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300698 * rc_repeat() - signals that a key is still pressed
David Härdemand8b4b582010-10-29 16:08:23 -0300699 * @dev: the struct rc_dev descriptor of the device
David Härdemana374fef2010-04-02 15:58:29 -0300700 *
701 * This routine is used by IR decoders when a repeat message which does
702 * not include the necessary bits to reproduce the scancode has been
703 * received.
704 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300705void rc_repeat(struct rc_dev *dev)
David Härdemana374fef2010-04-02 15:58:29 -0300706{
707 unsigned long flags;
Sean Young28492252018-03-24 08:02:48 -0400708 unsigned int timeout = nsecs_to_jiffies(dev->timeout) +
Sean Youngf5dbee62018-07-28 05:11:15 -0400709 msecs_to_jiffies(repeat_period(dev->last_protocol));
Sean Youngb66218f2017-09-30 07:13:37 -0400710 struct lirc_scancode sc = {
711 .scancode = dev->last_scancode, .rc_proto = dev->last_protocol,
712 .keycode = dev->keypressed ? dev->last_keycode : KEY_RESERVED,
713 .flags = LIRC_SCANCODE_FLAG_REPEAT |
714 (dev->last_toggle ? LIRC_SCANCODE_FLAG_TOGGLE : 0)
715 };
716
Sean Younge5bb9d32018-10-22 05:01:50 -0400717 if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
718 ir_lirc_scancode_event(dev, &sc);
David Härdemana374fef2010-04-02 15:58:29 -0300719
David Härdemand8b4b582010-10-29 16:08:23 -0300720 spin_lock_irqsave(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300721
David Härdeman265a2982017-06-22 15:23:54 -0400722 input_event(dev->input_dev, EV_MSC, MSC_SCAN, dev->last_scancode);
723 input_sync(dev->input_dev);
724
Sean Youngb66218f2017-09-30 07:13:37 -0400725 if (dev->keypressed) {
Sean Young28492252018-03-24 08:02:48 -0400726 dev->keyup_jiffies = jiffies + timeout;
Sean Youngb66218f2017-09-30 07:13:37 -0400727 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
728 }
David Härdemana374fef2010-04-02 15:58:29 -0300729
David Härdemand8b4b582010-10-29 16:08:23 -0300730 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300731}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300732EXPORT_SYMBOL_GPL(rc_repeat);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300733
734/**
David Härdeman62c65032010-10-29 16:08:07 -0300735 * ir_do_keydown() - internal function to process a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300736 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300737 * @protocol: the protocol of the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300738 * @scancode: the scancode of the keypress
739 * @keycode: the keycode of the keypress
740 * @toggle: the toggle value of the keypress
741 *
742 * This function is used internally to register a keypress, it must be
743 * called with keylock held.
744 */
Sean Young6d741bf2017-08-07 16:20:58 -0400745static void ir_do_keydown(struct rc_dev *dev, enum rc_proto protocol,
David Härdeman120703f2014-04-03 20:31:30 -0300746 u32 scancode, u32 keycode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300747{
David Härdeman99b0f3c2014-04-04 19:06:06 -0300748 bool new_event = (!dev->keypressed ||
David Härdeman120703f2014-04-03 20:31:30 -0300749 dev->last_protocol != protocol ||
David Härdeman99b0f3c2014-04-04 19:06:06 -0300750 dev->last_scancode != scancode ||
David Härdeman120703f2014-04-03 20:31:30 -0300751 dev->last_toggle != toggle);
Sean Youngde142c32017-02-25 06:51:32 -0500752 struct lirc_scancode sc = {
753 .scancode = scancode, .rc_proto = protocol,
754 .flags = toggle ? LIRC_SCANCODE_FLAG_TOGGLE : 0,
755 .keycode = keycode
756 };
757
Sean Younge5bb9d32018-10-22 05:01:50 -0400758 if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
759 ir_lirc_scancode_event(dev, &sc);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300760
761 if (new_event && dev->keypressed)
762 ir_do_keyup(dev, false);
763
David Härdemand8b4b582010-10-29 16:08:23 -0300764 input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300765
Sean Youngb66218f2017-09-30 07:13:37 -0400766 dev->last_protocol = protocol;
767 dev->last_scancode = scancode;
768 dev->last_toggle = toggle;
769 dev->last_keycode = keycode;
770
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300771 if (new_event && keycode != KEY_RESERVED) {
772 /* Register a keypress */
773 dev->keypressed = true;
David Härdeman62c65032010-10-29 16:08:07 -0300774
Sean Young1f17f682018-02-12 07:27:50 -0500775 dev_dbg(&dev->dev, "%s: key down event, key 0x%04x, protocol 0x%04x, scancode 0x%08x\n",
776 dev->device_name, keycode, protocol, scancode);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300777 input_report_key(dev->input_dev, keycode, 1);
James Hogan70a2f912014-01-16 19:56:22 -0300778
779 led_trigger_event(led_feedback, LED_FULL);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300780 }
David Härdeman62c65032010-10-29 16:08:07 -0300781
Sean Young57c642c2017-11-23 17:37:10 -0500782 /*
783 * For CEC, start sending repeat messages as soon as the first
784 * repeated message is sent, as long as REP_DELAY = 0 and REP_PERIOD
785 * is non-zero. Otherwise, the input layer will generate repeat
786 * messages.
787 */
788 if (!new_event && keycode != KEY_RESERVED &&
789 dev->allowed_protocols == RC_PROTO_BIT_CEC &&
790 !timer_pending(&dev->timer_repeat) &&
791 dev->input_dev->rep[REP_PERIOD] &&
792 !dev->input_dev->rep[REP_DELAY]) {
793 input_event(dev->input_dev, EV_KEY, keycode, 2);
794 mod_timer(&dev->timer_repeat, jiffies +
795 msecs_to_jiffies(dev->input_dev->rep[REP_PERIOD]));
796 }
797
David Härdemand8b4b582010-10-29 16:08:23 -0300798 input_sync(dev->input_dev);
David Härdeman62c65032010-10-29 16:08:07 -0300799}
800
801/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300802 * rc_keydown() - generates input event for a key press
David Härdemand8b4b582010-10-29 16:08:23 -0300803 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300804 * @protocol: the protocol for the keypress
805 * @scancode: the scancode for the keypress
David Härdemana374fef2010-04-02 15:58:29 -0300806 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
807 * support toggle values, this should be set to zero)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300808 *
David Härdemand8b4b582010-10-29 16:08:23 -0300809 * This routine is used to signal that a key has been pressed on the
810 * remote control.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300811 */
Sean Young6d741bf2017-08-07 16:20:58 -0400812void rc_keydown(struct rc_dev *dev, enum rc_proto protocol, u32 scancode,
813 u8 toggle)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300814{
David Härdemana374fef2010-04-02 15:58:29 -0300815 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300816 u32 keycode = rc_g_keycode_from_table(dev, scancode);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300817
David Härdemand8b4b582010-10-29 16:08:23 -0300818 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300819 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300820
David Härdemand8b4b582010-10-29 16:08:23 -0300821 if (dev->keypressed) {
Sean Young28492252018-03-24 08:02:48 -0400822 dev->keyup_jiffies = jiffies + nsecs_to_jiffies(dev->timeout) +
Sean Youngf5dbee62018-07-28 05:11:15 -0400823 msecs_to_jiffies(repeat_period(protocol));
David Härdemand8b4b582010-10-29 16:08:23 -0300824 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
David Härdeman62c65032010-10-29 16:08:07 -0300825 }
David Härdemand8b4b582010-10-29 16:08:23 -0300826 spin_unlock_irqrestore(&dev->keylock, flags);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300827}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300828EXPORT_SYMBOL_GPL(rc_keydown);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300829
David Härdeman62c65032010-10-29 16:08:07 -0300830/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300831 * rc_keydown_notimeout() - generates input event for a key press without
David Härdeman62c65032010-10-29 16:08:07 -0300832 * an automatic keyup event at a later time
David Härdemand8b4b582010-10-29 16:08:23 -0300833 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300834 * @protocol: the protocol for the keypress
835 * @scancode: the scancode for the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300836 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
837 * support toggle values, this should be set to zero)
838 *
David Härdemand8b4b582010-10-29 16:08:23 -0300839 * This routine is used to signal that a key has been pressed on the
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300840 * remote control. The driver must manually call rc_keyup() at a later stage.
David Härdeman62c65032010-10-29 16:08:07 -0300841 */
Sean Young6d741bf2017-08-07 16:20:58 -0400842void rc_keydown_notimeout(struct rc_dev *dev, enum rc_proto protocol,
David Härdeman120703f2014-04-03 20:31:30 -0300843 u32 scancode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300844{
845 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300846 u32 keycode = rc_g_keycode_from_table(dev, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300847
David Härdemand8b4b582010-10-29 16:08:23 -0300848 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300849 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
David Härdemand8b4b582010-10-29 16:08:23 -0300850 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300851}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300852EXPORT_SYMBOL_GPL(rc_keydown_notimeout);
David Härdeman62c65032010-10-29 16:08:07 -0300853
Sean Youngb590c0b2016-12-05 19:24:59 -0200854/**
Sean Young6b514c42017-10-09 16:32:41 -0400855 * rc_validate_scancode() - checks that a scancode is valid for a protocol.
856 * For nec, it should do the opposite of ir_nec_bytes_to_scancode()
Sean Young49a4b362017-09-27 16:00:49 -0400857 * @proto: protocol
858 * @scancode: scancode
859 */
860bool rc_validate_scancode(enum rc_proto proto, u32 scancode)
861{
862 switch (proto) {
Sean Young6b514c42017-10-09 16:32:41 -0400863 /*
864 * NECX has a 16-bit address; if the lower 8 bits match the upper
865 * 8 bits inverted, then the address would match regular nec.
866 */
Sean Young49a4b362017-09-27 16:00:49 -0400867 case RC_PROTO_NECX:
868 if ((((scancode >> 16) ^ ~(scancode >> 8)) & 0xff) == 0)
869 return false;
870 break;
Sean Young6b514c42017-10-09 16:32:41 -0400871 /*
872 * NEC32 has a 16 bit address and 16 bit command. If the lower 8 bits
873 * of the command match the upper 8 bits inverted, then it would
874 * be either NEC or NECX.
875 */
Sean Young49a4b362017-09-27 16:00:49 -0400876 case RC_PROTO_NEC32:
Sean Young6b514c42017-10-09 16:32:41 -0400877 if ((((scancode >> 8) ^ ~scancode) & 0xff) == 0)
Sean Young49a4b362017-09-27 16:00:49 -0400878 return false;
879 break;
Sean Young6b514c42017-10-09 16:32:41 -0400880 /*
881 * If the customer code (top 32-bit) is 0x800f, it is MCE else it
882 * is regular mode-6a 32 bit
883 */
Sean Young49a4b362017-09-27 16:00:49 -0400884 case RC_PROTO_RC6_MCE:
885 if ((scancode & 0xffff0000) != 0x800f0000)
886 return false;
887 break;
888 case RC_PROTO_RC6_6A_32:
889 if ((scancode & 0xffff0000) == 0x800f0000)
890 return false;
891 break;
892 default:
893 break;
894 }
895
896 return true;
897}
898
899/**
Sean Youngb590c0b2016-12-05 19:24:59 -0200900 * rc_validate_filter() - checks that the scancode and mask are valid and
901 * provides sensible defaults
James Hoganf423ccc2015-03-31 14:48:10 -0300902 * @dev: the struct rc_dev descriptor of the device
Sean Youngb590c0b2016-12-05 19:24:59 -0200903 * @filter: the scancode and mask
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500904 *
905 * return: 0 or -EINVAL if the filter is not valid
Sean Youngb590c0b2016-12-05 19:24:59 -0200906 */
James Hoganf423ccc2015-03-31 14:48:10 -0300907static int rc_validate_filter(struct rc_dev *dev,
Sean Youngb590c0b2016-12-05 19:24:59 -0200908 struct rc_scancode_filter *filter)
909{
Sean Youngd57ea872017-08-09 13:19:16 -0400910 u32 mask, s = filter->data;
Sean Young6d741bf2017-08-07 16:20:58 -0400911 enum rc_proto protocol = dev->wakeup_protocol;
Sean Youngb590c0b2016-12-05 19:24:59 -0200912
Sean Youngd57ea872017-08-09 13:19:16 -0400913 if (protocol >= ARRAY_SIZE(protocols))
Sean Young2168b412017-08-07 09:21:29 -0400914 return -EINVAL;
915
Sean Youngd57ea872017-08-09 13:19:16 -0400916 mask = protocols[protocol].scancode_bits;
917
Sean Young49a4b362017-09-27 16:00:49 -0400918 if (!rc_validate_scancode(protocol, s))
919 return -EINVAL;
Sean Youngb590c0b2016-12-05 19:24:59 -0200920
Sean Youngd57ea872017-08-09 13:19:16 -0400921 filter->data &= mask;
922 filter->mask &= mask;
Sean Youngb590c0b2016-12-05 19:24:59 -0200923
James Hoganf423ccc2015-03-31 14:48:10 -0300924 /*
925 * If we have to raw encode the IR for wakeup, we cannot have a mask
926 */
Sean Youngd57ea872017-08-09 13:19:16 -0400927 if (dev->encode_wakeup && filter->mask != 0 && filter->mask != mask)
James Hoganf423ccc2015-03-31 14:48:10 -0300928 return -EINVAL;
929
Sean Youngb590c0b2016-12-05 19:24:59 -0200930 return 0;
931}
932
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300933int rc_open(struct rc_dev *rdev)
934{
935 int rval = 0;
936
937 if (!rdev)
938 return -EINVAL;
939
940 mutex_lock(&rdev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -0200941
Sean Youngcb843432017-09-23 17:44:03 -0400942 if (!rdev->registered) {
943 rval = -ENODEV;
944 } else {
945 if (!rdev->users++ && rdev->open)
946 rval = rdev->open(rdev);
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300947
Sean Youngcb843432017-09-23 17:44:03 -0400948 if (rval)
949 rdev->users--;
950 }
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300951
952 mutex_unlock(&rdev->lock);
953
954 return rval;
955}
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300956
David Härdemand8b4b582010-10-29 16:08:23 -0300957static int ir_open(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300958{
David Härdemand8b4b582010-10-29 16:08:23 -0300959 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300960
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300961 return rc_open(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300962}
963
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300964void rc_close(struct rc_dev *rdev)
965{
966 if (rdev) {
967 mutex_lock(&rdev->lock);
968
Sean Youngcb843432017-09-23 17:44:03 -0400969 if (!--rdev->users && rdev->close && rdev->registered)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300970 rdev->close(rdev);
971
972 mutex_unlock(&rdev->lock);
973 }
974}
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300975
David Härdemand8b4b582010-10-29 16:08:23 -0300976static void ir_close(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300977{
David Härdemand8b4b582010-10-29 16:08:23 -0300978 struct rc_dev *rdev = input_get_drvdata(idev);
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300979 rc_close(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300980}
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300981
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300982/* class for /sys/class/rc */
David Härdeman40fc5322013-03-06 16:52:10 -0300983static char *rc_devnode(struct device *dev, umode_t *mode)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300984{
985 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
986}
987
David Härdeman40fc5322013-03-06 16:52:10 -0300988static struct class rc_class = {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300989 .name = "rc",
David Härdeman40fc5322013-03-06 16:52:10 -0300990 .devnode = rc_devnode,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300991};
992
David Härdemanc003ab12012-10-11 19:11:54 -0300993/*
994 * These are the protocol textual descriptions that are
995 * used by the sysfs protocols file. Note that the order
996 * of the entries is relevant.
997 */
Heiner Kallweit53df8772015-11-16 17:52:17 -0200998static const struct {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300999 u64 type;
Heiner Kallweit53df8772015-11-16 17:52:17 -02001000 const char *name;
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001001 const char *module_name;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001002} proto_names[] = {
Sean Young6d741bf2017-08-07 16:20:58 -04001003 { RC_PROTO_BIT_NONE, "none", NULL },
1004 { RC_PROTO_BIT_OTHER, "other", NULL },
1005 { RC_PROTO_BIT_UNKNOWN, "unknown", NULL },
1006 { RC_PROTO_BIT_RC5 |
1007 RC_PROTO_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
1008 { RC_PROTO_BIT_NEC |
1009 RC_PROTO_BIT_NECX |
1010 RC_PROTO_BIT_NEC32, "nec", "ir-nec-decoder" },
1011 { RC_PROTO_BIT_RC6_0 |
1012 RC_PROTO_BIT_RC6_6A_20 |
1013 RC_PROTO_BIT_RC6_6A_24 |
1014 RC_PROTO_BIT_RC6_6A_32 |
1015 RC_PROTO_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
1016 { RC_PROTO_BIT_JVC, "jvc", "ir-jvc-decoder" },
1017 { RC_PROTO_BIT_SONY12 |
1018 RC_PROTO_BIT_SONY15 |
1019 RC_PROTO_BIT_SONY20, "sony", "ir-sony-decoder" },
1020 { RC_PROTO_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
1021 { RC_PROTO_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
1022 { RC_PROTO_BIT_SHARP, "sharp", "ir-sharp-decoder" },
1023 { RC_PROTO_BIT_MCIR2_KBD |
1024 RC_PROTO_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" },
1025 { RC_PROTO_BIT_XMP, "xmp", "ir-xmp-decoder" },
1026 { RC_PROTO_BIT_CEC, "cec", NULL },
Sean Young447dcc0c2017-12-03 11:06:54 -05001027 { RC_PROTO_BIT_IMON, "imon", "ir-imon-decoder" },
Patrick Lerda721074b2019-01-17 03:50:13 -05001028 { RC_PROTO_BIT_RCMM12 |
1029 RC_PROTO_BIT_RCMM24 |
1030 RC_PROTO_BIT_RCMM32, "rc-mm", "ir-rcmm-decoder" },
Matthias Reichl17287692019-03-24 05:43:51 -04001031 { RC_PROTO_BIT_XBOX_DVD, "xbox-dvd", NULL },
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001032};
1033
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001034/**
James Hoganab88c662014-02-28 20:17:05 -03001035 * struct rc_filter_attribute - Device attribute relating to a filter type.
1036 * @attr: Device attribute.
1037 * @type: Filter type.
1038 * @mask: false for filter value, true for filter mask.
1039 */
1040struct rc_filter_attribute {
1041 struct device_attribute attr;
1042 enum rc_filter_type type;
1043 bool mask;
1044};
1045#define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
1046
James Hoganab88c662014-02-28 20:17:05 -03001047#define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
1048 struct rc_filter_attribute dev_attr_##_name = { \
1049 .attr = __ATTR(_name, _mode, _show, _store), \
1050 .type = (_type), \
1051 .mask = (_mask), \
1052 }
1053
1054/**
Sean Young0751d332016-12-05 17:08:35 -02001055 * show_protocols() - shows the current IR protocol(s)
David Härdemand8b4b582010-10-29 16:08:23 -03001056 * @device: the device descriptor
David Härdemanda6e1622014-04-03 20:32:16 -03001057 * @mattr: the device attribute struct
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001058 * @buf: a pointer to the output buffer
1059 *
1060 * This routine is a callback routine for input read the IR protocol type(s).
Mauro Carvalho Chehab04ad3012019-02-18 14:29:01 -05001061 * it is triggered by reading /sys/class/rc/rc?/protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001062 * It returns the protocol names of supported protocols.
1063 * Enabled protocols are printed in brackets.
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001064 *
David Härdeman18726a32017-04-27 17:34:08 -03001065 * dev->lock is taken to guard against races between
1066 * store_protocols and show_protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001067 */
David Härdemand8b4b582010-10-29 16:08:23 -03001068static ssize_t show_protocols(struct device *device,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001069 struct device_attribute *mattr, char *buf)
1070{
David Härdemand8b4b582010-10-29 16:08:23 -03001071 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001072 u64 allowed, enabled;
1073 char *tmp = buf;
1074 int i;
1075
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001076 mutex_lock(&dev->lock);
1077
Sean Young0751d332016-12-05 17:08:35 -02001078 enabled = dev->enabled_protocols;
1079 allowed = dev->allowed_protocols;
1080 if (dev->raw && !allowed)
1081 allowed = ir_raw_get_allowed_protocols();
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001082
David Härdemanda6e1622014-04-03 20:32:16 -03001083 mutex_unlock(&dev->lock);
1084
Sean Young1f17f682018-02-12 07:27:50 -05001085 dev_dbg(&dev->dev, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
1086 __func__, (long long)allowed, (long long)enabled);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001087
1088 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1089 if (allowed & enabled & proto_names[i].type)
1090 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
1091 else if (allowed & proto_names[i].type)
1092 tmp += sprintf(tmp, "%s ", proto_names[i].name);
David Härdemanc003ab12012-10-11 19:11:54 -03001093
1094 if (allowed & proto_names[i].type)
1095 allowed &= ~proto_names[i].type;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001096 }
1097
Sean Younga60d64b2017-09-23 10:41:13 -04001098#ifdef CONFIG_LIRC
1099 if (dev->driver_type == RC_DRIVER_IR_RAW)
David Härdeman275ddb42015-05-19 19:03:22 -03001100 tmp += sprintf(tmp, "[lirc] ");
Sean Younga60d64b2017-09-23 10:41:13 -04001101#endif
David Härdeman275ddb42015-05-19 19:03:22 -03001102
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001103 if (tmp != buf)
1104 tmp--;
1105 *tmp = '\n';
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001106
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001107 return tmp + 1 - buf;
1108}
1109
1110/**
David Härdemanda6e1622014-04-03 20:32:16 -03001111 * parse_protocol_change() - parses a protocol change request
Sean Young1f17f682018-02-12 07:27:50 -05001112 * @dev: rc_dev device
David Härdemanda6e1622014-04-03 20:32:16 -03001113 * @protocols: pointer to the bitmask of current protocols
1114 * @buf: pointer to the buffer with a list of changes
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001115 *
David Härdemanda6e1622014-04-03 20:32:16 -03001116 * Writing "+proto" will add a protocol to the protocol mask.
1117 * Writing "-proto" will remove a protocol from protocol mask.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001118 * Writing "proto" will enable only "proto".
1119 * Writing "none" will disable all protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001120 * Returns the number of changes performed or a negative error code.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001121 */
Sean Young1f17f682018-02-12 07:27:50 -05001122static int parse_protocol_change(struct rc_dev *dev, u64 *protocols,
1123 const char *buf)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001124{
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001125 const char *tmp;
David Härdemanda6e1622014-04-03 20:32:16 -03001126 unsigned count = 0;
1127 bool enable, disable;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001128 u64 mask;
David Härdemanda6e1622014-04-03 20:32:16 -03001129 int i;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001130
David Härdemanda6e1622014-04-03 20:32:16 -03001131 while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001132 if (!*tmp)
1133 break;
1134
1135 if (*tmp == '+') {
1136 enable = true;
1137 disable = false;
1138 tmp++;
1139 } else if (*tmp == '-') {
1140 enable = false;
1141 disable = true;
1142 tmp++;
1143 } else {
1144 enable = false;
1145 disable = false;
1146 }
1147
David Härdemanc003ab12012-10-11 19:11:54 -03001148 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1149 if (!strcasecmp(tmp, proto_names[i].name)) {
1150 mask = proto_names[i].type;
1151 break;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001152 }
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001153 }
1154
David Härdemanc003ab12012-10-11 19:11:54 -03001155 if (i == ARRAY_SIZE(proto_names)) {
David Härdeman275ddb42015-05-19 19:03:22 -03001156 if (!strcasecmp(tmp, "lirc"))
1157 mask = 0;
1158 else {
Sean Young1f17f682018-02-12 07:27:50 -05001159 dev_dbg(&dev->dev, "Unknown protocol: '%s'\n",
1160 tmp);
David Härdeman275ddb42015-05-19 19:03:22 -03001161 return -EINVAL;
1162 }
David Härdemanc003ab12012-10-11 19:11:54 -03001163 }
1164
1165 count++;
1166
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001167 if (enable)
David Härdemanda6e1622014-04-03 20:32:16 -03001168 *protocols |= mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001169 else if (disable)
David Härdemanda6e1622014-04-03 20:32:16 -03001170 *protocols &= ~mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001171 else
David Härdemanda6e1622014-04-03 20:32:16 -03001172 *protocols = mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001173 }
1174
1175 if (!count) {
Sean Young1f17f682018-02-12 07:27:50 -05001176 dev_dbg(&dev->dev, "Protocol not specified\n");
David Härdemanda6e1622014-04-03 20:32:16 -03001177 return -EINVAL;
1178 }
1179
1180 return count;
1181}
1182
Sean Young0d39ab02017-02-25 06:51:31 -05001183void ir_raw_load_modules(u64 *protocols)
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001184{
1185 u64 available;
1186 int i, ret;
1187
1188 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
Sean Young6d741bf2017-08-07 16:20:58 -04001189 if (proto_names[i].type == RC_PROTO_BIT_NONE ||
1190 proto_names[i].type & (RC_PROTO_BIT_OTHER |
1191 RC_PROTO_BIT_UNKNOWN))
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001192 continue;
1193
1194 available = ir_raw_get_allowed_protocols();
1195 if (!(*protocols & proto_names[i].type & ~available))
1196 continue;
1197
1198 if (!proto_names[i].module_name) {
1199 pr_err("Can't enable IR protocol %s\n",
1200 proto_names[i].name);
1201 *protocols &= ~proto_names[i].type;
1202 continue;
1203 }
1204
1205 ret = request_module("%s", proto_names[i].module_name);
1206 if (ret < 0) {
1207 pr_err("Couldn't load IR protocol module %s\n",
1208 proto_names[i].module_name);
1209 *protocols &= ~proto_names[i].type;
1210 continue;
1211 }
1212 msleep(20);
1213 available = ir_raw_get_allowed_protocols();
1214 if (!(*protocols & proto_names[i].type & ~available))
1215 continue;
1216
Sean Young8caebcd2017-01-19 19:33:49 -02001217 pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001218 proto_names[i].module_name,
1219 proto_names[i].name);
1220 *protocols &= ~proto_names[i].type;
1221 }
1222}
1223
David Härdemanda6e1622014-04-03 20:32:16 -03001224/**
1225 * store_protocols() - changes the current/wakeup IR protocol(s)
1226 * @device: the device descriptor
1227 * @mattr: the device attribute struct
1228 * @buf: a pointer to the input buffer
1229 * @len: length of the input buffer
1230 *
1231 * This routine is for changing the IR protocol type.
Mauro Carvalho Chehab04ad3012019-02-18 14:29:01 -05001232 * It is triggered by writing to /sys/class/rc/rc?/[wakeup_]protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001233 * See parse_protocol_change() for the valid commands.
1234 * Returns @len on success or a negative error code.
1235 *
David Härdeman18726a32017-04-27 17:34:08 -03001236 * dev->lock is taken to guard against races between
1237 * store_protocols and show_protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001238 */
1239static ssize_t store_protocols(struct device *device,
1240 struct device_attribute *mattr,
1241 const char *buf, size_t len)
1242{
1243 struct rc_dev *dev = to_rc_dev(device);
David Härdemanda6e1622014-04-03 20:32:16 -03001244 u64 *current_protocols;
David Härdemanda6e1622014-04-03 20:32:16 -03001245 struct rc_scancode_filter *filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001246 u64 old_protocols, new_protocols;
1247 ssize_t rc;
1248
Sean Young1f17f682018-02-12 07:27:50 -05001249 dev_dbg(&dev->dev, "Normal protocol change requested\n");
Sean Young0751d332016-12-05 17:08:35 -02001250 current_protocols = &dev->enabled_protocols;
1251 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001252
Sean Young0751d332016-12-05 17:08:35 -02001253 if (!dev->change_protocol) {
Sean Young1f17f682018-02-12 07:27:50 -05001254 dev_dbg(&dev->dev, "Protocol switching not supported\n");
David Härdemanda6e1622014-04-03 20:32:16 -03001255 return -EINVAL;
1256 }
1257
1258 mutex_lock(&dev->lock);
1259
1260 old_protocols = *current_protocols;
1261 new_protocols = old_protocols;
Sean Young1f17f682018-02-12 07:27:50 -05001262 rc = parse_protocol_change(dev, &new_protocols, buf);
David Härdemanda6e1622014-04-03 20:32:16 -03001263 if (rc < 0)
1264 goto out;
1265
Sean Younga86d6df2018-03-23 16:47:37 -04001266 if (dev->driver_type == RC_DRIVER_IR_RAW)
1267 ir_raw_load_modules(&new_protocols);
1268
Sean Young0751d332016-12-05 17:08:35 -02001269 rc = dev->change_protocol(dev, &new_protocols);
David Härdemanda6e1622014-04-03 20:32:16 -03001270 if (rc < 0) {
Sean Young1f17f682018-02-12 07:27:50 -05001271 dev_dbg(&dev->dev, "Error setting protocols to 0x%llx\n",
1272 (long long)new_protocols);
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001273 goto out;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001274 }
1275
James Hogan983c5bd2014-12-08 13:17:07 -03001276 if (new_protocols != old_protocols) {
1277 *current_protocols = new_protocols;
Sean Young1f17f682018-02-12 07:27:50 -05001278 dev_dbg(&dev->dev, "Protocols changed to 0x%llx\n",
1279 (long long)new_protocols);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001280 }
1281
James Hogan6bea25a2014-02-28 20:17:06 -03001282 /*
James Hogan983c5bd2014-12-08 13:17:07 -03001283 * If a protocol change was attempted the filter may need updating, even
1284 * if the actual protocol mask hasn't changed (since the driver may have
1285 * cleared the filter).
James Hogan6bea25a2014-02-28 20:17:06 -03001286 * Try setting the same filter with the new protocol (if any).
1287 * Fall back to clearing the filter.
1288 */
Sean Young0751d332016-12-05 17:08:35 -02001289 if (dev->s_filter && filter->mask) {
David Härdemanda6e1622014-04-03 20:32:16 -03001290 if (new_protocols)
Sean Young0751d332016-12-05 17:08:35 -02001291 rc = dev->s_filter(dev, filter);
David Härdemanda6e1622014-04-03 20:32:16 -03001292 else
1293 rc = -1;
David Härdeman23c843b2014-04-04 19:06:01 -03001294
David Härdemanda6e1622014-04-03 20:32:16 -03001295 if (rc < 0) {
1296 filter->data = 0;
1297 filter->mask = 0;
Sean Young0751d332016-12-05 17:08:35 -02001298 dev->s_filter(dev, filter);
James Hogan6bea25a2014-02-28 20:17:06 -03001299 }
James Hogan6bea25a2014-02-28 20:17:06 -03001300 }
1301
David Härdemanda6e1622014-04-03 20:32:16 -03001302 rc = len;
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001303
1304out:
1305 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001306 return rc;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001307}
1308
James Hogan00942d12014-01-17 10:58:49 -03001309/**
James Hogan00942d12014-01-17 10:58:49 -03001310 * show_filter() - shows the current scancode filter value or mask
1311 * @device: the device descriptor
1312 * @attr: the device attribute struct
1313 * @buf: a pointer to the output buffer
1314 *
1315 * This routine is a callback routine to read a scancode filter value or mask.
Mauro Carvalho Chehab04ad3012019-02-18 14:29:01 -05001316 * It is triggered by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
James Hogan00942d12014-01-17 10:58:49 -03001317 * It prints the current scancode filter value or mask of the appropriate filter
1318 * type in hexadecimal into @buf and returns the size of the buffer.
1319 *
1320 * Bits of the filter value corresponding to set bits in the filter mask are
1321 * compared against input scancodes and non-matching scancodes are discarded.
1322 *
David Härdeman18726a32017-04-27 17:34:08 -03001323 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001324 * store_filter and show_filter.
1325 */
1326static ssize_t show_filter(struct device *device,
1327 struct device_attribute *attr,
1328 char *buf)
1329{
1330 struct rc_dev *dev = to_rc_dev(device);
1331 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001332 struct rc_scancode_filter *filter;
James Hogan00942d12014-01-17 10:58:49 -03001333 u32 val;
1334
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001335 mutex_lock(&dev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001336
David Härdemanda6e1622014-04-03 20:32:16 -03001337 if (fattr->type == RC_FILTER_NORMAL)
David Härdemanc5540fb2014-04-03 20:32:21 -03001338 filter = &dev->scancode_filter;
James Hogan00942d12014-01-17 10:58:49 -03001339 else
David Härdemanc5540fb2014-04-03 20:32:21 -03001340 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001341
David Härdemanda6e1622014-04-03 20:32:16 -03001342 if (fattr->mask)
1343 val = filter->mask;
1344 else
1345 val = filter->data;
James Hogan00942d12014-01-17 10:58:49 -03001346 mutex_unlock(&dev->lock);
1347
1348 return sprintf(buf, "%#x\n", val);
1349}
1350
1351/**
1352 * store_filter() - changes the scancode filter value
1353 * @device: the device descriptor
1354 * @attr: the device attribute struct
1355 * @buf: a pointer to the input buffer
1356 * @len: length of the input buffer
1357 *
1358 * This routine is for changing a scancode filter value or mask.
Mauro Carvalho Chehab04ad3012019-02-18 14:29:01 -05001359 * It is triggered by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
James Hogan00942d12014-01-17 10:58:49 -03001360 * Returns -EINVAL if an invalid filter value for the current protocol was
1361 * specified or if scancode filtering is not supported by the driver, otherwise
1362 * returns @len.
1363 *
1364 * Bits of the filter value corresponding to set bits in the filter mask are
1365 * compared against input scancodes and non-matching scancodes are discarded.
1366 *
David Härdeman18726a32017-04-27 17:34:08 -03001367 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001368 * store_filter and show_filter.
1369 */
1370static ssize_t store_filter(struct device *device,
1371 struct device_attribute *attr,
David Härdemanda6e1622014-04-03 20:32:16 -03001372 const char *buf, size_t len)
James Hogan00942d12014-01-17 10:58:49 -03001373{
1374 struct rc_dev *dev = to_rc_dev(device);
1375 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001376 struct rc_scancode_filter new_filter, *filter;
James Hogan00942d12014-01-17 10:58:49 -03001377 int ret;
1378 unsigned long val;
David Härdeman23c843b2014-04-04 19:06:01 -03001379 int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
James Hogan00942d12014-01-17 10:58:49 -03001380
James Hogan00942d12014-01-17 10:58:49 -03001381 ret = kstrtoul(buf, 0, &val);
1382 if (ret < 0)
1383 return ret;
1384
David Härdemanda6e1622014-04-03 20:32:16 -03001385 if (fattr->type == RC_FILTER_NORMAL) {
1386 set_filter = dev->s_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001387 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001388 } else {
1389 set_filter = dev->s_wakeup_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001390 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001391 }
1392
David Härdeman99b0f3c2014-04-04 19:06:06 -03001393 if (!set_filter)
1394 return -EINVAL;
James Hogan00942d12014-01-17 10:58:49 -03001395
1396 mutex_lock(&dev->lock);
1397
David Härdemanda6e1622014-04-03 20:32:16 -03001398 new_filter = *filter;
James Hogan00942d12014-01-17 10:58:49 -03001399 if (fattr->mask)
David Härdemanda6e1622014-04-03 20:32:16 -03001400 new_filter.mask = val;
James Hogan00942d12014-01-17 10:58:49 -03001401 else
David Härdemanda6e1622014-04-03 20:32:16 -03001402 new_filter.data = val;
David Härdeman23c843b2014-04-04 19:06:01 -03001403
Sean Young0751d332016-12-05 17:08:35 -02001404 if (fattr->type == RC_FILTER_WAKEUP) {
Sean Youngb590c0b2016-12-05 19:24:59 -02001405 /*
1406 * Refuse to set a filter unless a protocol is enabled
1407 * and the filter is valid for that protocol
1408 */
Sean Young6d741bf2017-08-07 16:20:58 -04001409 if (dev->wakeup_protocol != RC_PROTO_UNKNOWN)
James Hoganf423ccc2015-03-31 14:48:10 -03001410 ret = rc_validate_filter(dev, &new_filter);
Sean Youngb590c0b2016-12-05 19:24:59 -02001411 else
Sean Young0751d332016-12-05 17:08:35 -02001412 ret = -EINVAL;
Sean Youngb590c0b2016-12-05 19:24:59 -02001413
1414 if (ret != 0)
Sean Young0751d332016-12-05 17:08:35 -02001415 goto unlock;
Sean Young0751d332016-12-05 17:08:35 -02001416 }
1417
1418 if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
1419 val) {
James Hogan6bea25a2014-02-28 20:17:06 -03001420 /* refuse to set a filter unless a protocol is enabled */
1421 ret = -EINVAL;
1422 goto unlock;
1423 }
David Härdeman23c843b2014-04-04 19:06:01 -03001424
David Härdemanda6e1622014-04-03 20:32:16 -03001425 ret = set_filter(dev, &new_filter);
David Härdeman99b0f3c2014-04-04 19:06:06 -03001426 if (ret < 0)
1427 goto unlock;
James Hogan00942d12014-01-17 10:58:49 -03001428
David Härdemanda6e1622014-04-03 20:32:16 -03001429 *filter = new_filter;
James Hogan00942d12014-01-17 10:58:49 -03001430
1431unlock:
1432 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001433 return (ret < 0) ? ret : len;
James Hogan00942d12014-01-17 10:58:49 -03001434}
1435
Sean Young0751d332016-12-05 17:08:35 -02001436/**
1437 * show_wakeup_protocols() - shows the wakeup IR protocol
1438 * @device: the device descriptor
1439 * @mattr: the device attribute struct
1440 * @buf: a pointer to the output buffer
1441 *
1442 * This routine is a callback routine for input read the IR protocol type(s).
Mauro Carvalho Chehab04ad3012019-02-18 14:29:01 -05001443 * it is triggered by reading /sys/class/rc/rc?/wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001444 * It returns the protocol names of supported protocols.
1445 * The enabled protocols are printed in brackets.
1446 *
David Härdeman18726a32017-04-27 17:34:08 -03001447 * dev->lock is taken to guard against races between
1448 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001449 */
1450static ssize_t show_wakeup_protocols(struct device *device,
1451 struct device_attribute *mattr,
1452 char *buf)
1453{
1454 struct rc_dev *dev = to_rc_dev(device);
1455 u64 allowed;
Sean Young6d741bf2017-08-07 16:20:58 -04001456 enum rc_proto enabled;
Sean Young0751d332016-12-05 17:08:35 -02001457 char *tmp = buf;
1458 int i;
1459
Sean Young0751d332016-12-05 17:08:35 -02001460 mutex_lock(&dev->lock);
1461
1462 allowed = dev->allowed_wakeup_protocols;
1463 enabled = dev->wakeup_protocol;
1464
1465 mutex_unlock(&dev->lock);
1466
Sean Young1f17f682018-02-12 07:27:50 -05001467 dev_dbg(&dev->dev, "%s: allowed - 0x%llx, enabled - %d\n",
1468 __func__, (long long)allowed, enabled);
Sean Young0751d332016-12-05 17:08:35 -02001469
Sean Youngd57ea872017-08-09 13:19:16 -04001470 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001471 if (allowed & (1ULL << i)) {
1472 if (i == enabled)
Sean Youngd57ea872017-08-09 13:19:16 -04001473 tmp += sprintf(tmp, "[%s] ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001474 else
Sean Youngd57ea872017-08-09 13:19:16 -04001475 tmp += sprintf(tmp, "%s ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001476 }
1477 }
1478
1479 if (tmp != buf)
1480 tmp--;
1481 *tmp = '\n';
1482
1483 return tmp + 1 - buf;
1484}
1485
1486/**
1487 * store_wakeup_protocols() - changes the wakeup IR protocol(s)
1488 * @device: the device descriptor
1489 * @mattr: the device attribute struct
1490 * @buf: a pointer to the input buffer
1491 * @len: length of the input buffer
1492 *
1493 * This routine is for changing the IR protocol type.
Mauro Carvalho Chehab04ad3012019-02-18 14:29:01 -05001494 * It is triggered by writing to /sys/class/rc/rc?/wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001495 * Returns @len on success or a negative error code.
1496 *
David Härdeman18726a32017-04-27 17:34:08 -03001497 * dev->lock is taken to guard against races between
1498 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001499 */
1500static ssize_t store_wakeup_protocols(struct device *device,
1501 struct device_attribute *mattr,
1502 const char *buf, size_t len)
1503{
1504 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehab3d351532019-05-23 05:27:30 -04001505 enum rc_proto protocol = RC_PROTO_UNKNOWN;
Sean Young0751d332016-12-05 17:08:35 -02001506 ssize_t rc;
1507 u64 allowed;
1508 int i;
1509
Sean Young0751d332016-12-05 17:08:35 -02001510 mutex_lock(&dev->lock);
1511
1512 allowed = dev->allowed_wakeup_protocols;
1513
Mauro Carvalho Chehab3d351532019-05-23 05:27:30 -04001514 if (!sysfs_streq(buf, "none")) {
Sean Youngd57ea872017-08-09 13:19:16 -04001515 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001516 if ((allowed & (1ULL << i)) &&
Sean Youngd57ea872017-08-09 13:19:16 -04001517 sysfs_streq(buf, protocols[i].name)) {
Sean Young0751d332016-12-05 17:08:35 -02001518 protocol = i;
1519 break;
1520 }
1521 }
1522
Sean Youngd57ea872017-08-09 13:19:16 -04001523 if (i == ARRAY_SIZE(protocols)) {
Sean Young0751d332016-12-05 17:08:35 -02001524 rc = -EINVAL;
1525 goto out;
1526 }
James Hoganf423ccc2015-03-31 14:48:10 -03001527
1528 if (dev->encode_wakeup) {
1529 u64 mask = 1ULL << protocol;
1530
1531 ir_raw_load_modules(&mask);
1532 if (!mask) {
1533 rc = -EINVAL;
1534 goto out;
1535 }
1536 }
Sean Young0751d332016-12-05 17:08:35 -02001537 }
1538
1539 if (dev->wakeup_protocol != protocol) {
1540 dev->wakeup_protocol = protocol;
Sean Young1f17f682018-02-12 07:27:50 -05001541 dev_dbg(&dev->dev, "Wakeup protocol changed to %d\n", protocol);
Sean Young0751d332016-12-05 17:08:35 -02001542
Sean Young6d741bf2017-08-07 16:20:58 -04001543 if (protocol == RC_PROTO_RC6_MCE)
Sean Young0751d332016-12-05 17:08:35 -02001544 dev->scancode_wakeup_filter.data = 0x800f0000;
1545 else
1546 dev->scancode_wakeup_filter.data = 0;
1547 dev->scancode_wakeup_filter.mask = 0;
1548
1549 rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
1550 if (rc == 0)
1551 rc = len;
1552 } else {
1553 rc = len;
1554 }
1555
1556out:
1557 mutex_unlock(&dev->lock);
1558 return rc;
1559}
1560
David Härdemand8b4b582010-10-29 16:08:23 -03001561static void rc_dev_release(struct device *device)
1562{
Max Kellermann47cae1e2016-03-21 08:33:05 -03001563 struct rc_dev *dev = to_rc_dev(device);
1564
1565 kfree(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001566}
1567
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001568#define ADD_HOTPLUG_VAR(fmt, val...) \
1569 do { \
1570 int err = add_uevent_var(env, fmt, val); \
1571 if (err) \
1572 return err; \
1573 } while (0)
1574
1575static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1576{
David Härdemand8b4b582010-10-29 16:08:23 -03001577 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001578
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001579 if (dev->rc_map.name)
1580 ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
David Härdemand8b4b582010-10-29 16:08:23 -03001581 if (dev->driver_name)
1582 ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
Sean Youngb9f407e2017-09-01 11:34:23 -03001583 if (dev->device_name)
1584 ADD_HOTPLUG_VAR("DEV_NAME=%s", dev->device_name);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001585
1586 return 0;
1587}
1588
1589/*
1590 * Static device attribute struct with the sysfs attributes for IR's
1591 */
Sean Young6d75db32017-09-01 11:30:50 -03001592static struct device_attribute dev_attr_ro_protocols =
1593__ATTR(protocols, 0444, show_protocols, NULL);
1594static struct device_attribute dev_attr_rw_protocols =
1595__ATTR(protocols, 0644, show_protocols, store_protocols);
Sean Young0751d332016-12-05 17:08:35 -02001596static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
1597 store_wakeup_protocols);
James Hogan00942d12014-01-17 10:58:49 -03001598static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
1599 show_filter, store_filter, RC_FILTER_NORMAL, false);
1600static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
1601 show_filter, store_filter, RC_FILTER_NORMAL, true);
1602static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
1603 show_filter, store_filter, RC_FILTER_WAKEUP, false);
1604static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
1605 show_filter, store_filter, RC_FILTER_WAKEUP, true);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001606
Sean Young6d75db32017-09-01 11:30:50 -03001607static struct attribute *rc_dev_rw_protocol_attrs[] = {
1608 &dev_attr_rw_protocols.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001609 NULL,
1610};
1611
Sean Young6d75db32017-09-01 11:30:50 -03001612static const struct attribute_group rc_dev_rw_protocol_attr_grp = {
1613 .attrs = rc_dev_rw_protocol_attrs,
1614};
1615
1616static struct attribute *rc_dev_ro_protocol_attrs[] = {
1617 &dev_attr_ro_protocols.attr,
1618 NULL,
1619};
1620
1621static const struct attribute_group rc_dev_ro_protocol_attr_grp = {
1622 .attrs = rc_dev_ro_protocol_attrs,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001623};
1624
David Härdeman99b0f3c2014-04-04 19:06:06 -03001625static struct attribute *rc_dev_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001626 &dev_attr_filter.attr.attr,
1627 &dev_attr_filter_mask.attr.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001628 NULL,
1629};
1630
Arvind Yadavdb681022017-07-07 04:23:54 -04001631static const struct attribute_group rc_dev_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001632 .attrs = rc_dev_filter_attrs,
1633};
1634
1635static struct attribute *rc_dev_wakeup_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001636 &dev_attr_wakeup_filter.attr.attr,
1637 &dev_attr_wakeup_filter_mask.attr.attr,
Sean Young0751d332016-12-05 17:08:35 -02001638 &dev_attr_wakeup_protocols.attr,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001639 NULL,
1640};
1641
Arvind Yadavdb681022017-07-07 04:23:54 -04001642static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001643 .attrs = rc_dev_wakeup_filter_attrs,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001644};
1645
Bhumika Goyalf03f02f2017-08-19 05:22:15 -03001646static const struct device_type rc_dev_type = {
David Härdemand8b4b582010-10-29 16:08:23 -03001647 .release = rc_dev_release,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001648 .uevent = rc_dev_uevent,
1649};
1650
Andi Shyti0f7499f2016-12-16 06:50:58 -02001651struct rc_dev *rc_allocate_device(enum rc_driver_type type)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001652{
David Härdemand8b4b582010-10-29 16:08:23 -03001653 struct rc_dev *dev;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001654
David Härdemand8b4b582010-10-29 16:08:23 -03001655 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1656 if (!dev)
1657 return NULL;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001658
Andi Shytid34aee12016-12-16 04:12:15 -02001659 if (type != RC_DRIVER_IR_RAW_TX) {
1660 dev->input_dev = input_allocate_device();
1661 if (!dev->input_dev) {
1662 kfree(dev);
1663 return NULL;
1664 }
1665
1666 dev->input_dev->getkeycode = ir_getkeycode;
1667 dev->input_dev->setkeycode = ir_setkeycode;
1668 input_set_drvdata(dev->input_dev, dev);
1669
Sean Young28492252018-03-24 08:02:48 -04001670 dev->timeout = IR_DEFAULT_TIMEOUT;
Kees Cookb17ec782017-10-24 11:23:14 -04001671 timer_setup(&dev->timer_keyup, ir_timer_keyup, 0);
Sean Young57c642c2017-11-23 17:37:10 -05001672 timer_setup(&dev->timer_repeat, ir_timer_repeat, 0);
Andi Shytid34aee12016-12-16 04:12:15 -02001673
1674 spin_lock_init(&dev->rc_map.lock);
1675 spin_lock_init(&dev->keylock);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001676 }
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001677 mutex_init(&dev->lock);
David Härdemand8b4b582010-10-29 16:08:23 -03001678
1679 dev->dev.type = &rc_dev_type;
David Härdeman40fc5322013-03-06 16:52:10 -03001680 dev->dev.class = &rc_class;
David Härdemand8b4b582010-10-29 16:08:23 -03001681 device_initialize(&dev->dev);
1682
Andi Shyti0f7499f2016-12-16 06:50:58 -02001683 dev->driver_type = type;
1684
David Härdemand8b4b582010-10-29 16:08:23 -03001685 __module_get(THIS_MODULE);
1686 return dev;
1687}
1688EXPORT_SYMBOL_GPL(rc_allocate_device);
1689
1690void rc_free_device(struct rc_dev *dev)
1691{
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001692 if (!dev)
1693 return;
1694
Markus Elfring3dd94f02014-11-20 09:01:32 -03001695 input_free_device(dev->input_dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001696
1697 put_device(&dev->dev);
1698
Max Kellermann47cae1e2016-03-21 08:33:05 -03001699 /* kfree(dev) will be called by the callback function
1700 rc_dev_release() */
1701
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001702 module_put(THIS_MODULE);
David Härdemand8b4b582010-10-29 16:08:23 -03001703}
1704EXPORT_SYMBOL_GPL(rc_free_device);
1705
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001706static void devm_rc_alloc_release(struct device *dev, void *res)
1707{
1708 rc_free_device(*(struct rc_dev **)res);
1709}
1710
Andi Shyti0f7499f2016-12-16 06:50:58 -02001711struct rc_dev *devm_rc_allocate_device(struct device *dev,
1712 enum rc_driver_type type)
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001713{
1714 struct rc_dev **dr, *rc;
1715
1716 dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
1717 if (!dr)
1718 return NULL;
1719
Andi Shyti0f7499f2016-12-16 06:50:58 -02001720 rc = rc_allocate_device(type);
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001721 if (!rc) {
1722 devres_free(dr);
1723 return NULL;
1724 }
1725
1726 rc->dev.parent = dev;
1727 rc->managed_alloc = true;
1728 *dr = rc;
1729 devres_add(dev, dr);
1730
1731 return rc;
1732}
1733EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
1734
David Härdemanf56928a2017-05-03 07:04:00 -03001735static int rc_prepare_rx_device(struct rc_dev *dev)
David Härdemand8b4b582010-10-29 16:08:23 -03001736{
David Härdemanfcb13092015-05-19 19:03:17 -03001737 int rc;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001738 struct rc_map *rc_map;
Sean Young6d741bf2017-08-07 16:20:58 -04001739 u64 rc_proto;
David Härdemand8b4b582010-10-29 16:08:23 -03001740
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001741 if (!dev->map_name)
David Härdemand8b4b582010-10-29 16:08:23 -03001742 return -EINVAL;
1743
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001744 rc_map = rc_map_get(dev->map_name);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001745 if (!rc_map)
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001746 rc_map = rc_map_get(RC_MAP_EMPTY);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001747 if (!rc_map || !rc_map->scan || rc_map->size == 0)
David Härdemand8b4b582010-10-29 16:08:23 -03001748 return -EINVAL;
1749
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001750 rc = ir_setkeytable(dev, rc_map);
1751 if (rc)
1752 return rc;
1753
Sean Young6d741bf2017-08-07 16:20:58 -04001754 rc_proto = BIT_ULL(rc_map->rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001755
Sean Young831c4c82017-09-01 09:55:59 -03001756 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1757 dev->enabled_protocols = dev->allowed_protocols;
1758
Sean Younga86d6df2018-03-23 16:47:37 -04001759 if (dev->driver_type == RC_DRIVER_IR_RAW)
1760 ir_raw_load_modules(&rc_proto);
1761
Sean Young41380862017-02-22 18:48:01 -03001762 if (dev->change_protocol) {
Sean Young6d741bf2017-08-07 16:20:58 -04001763 rc = dev->change_protocol(dev, &rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001764 if (rc < 0)
1765 goto out_table;
Sean Young6d741bf2017-08-07 16:20:58 -04001766 dev->enabled_protocols = rc_proto;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001767 }
1768
Sean Young0ac5a602018-09-16 07:35:58 -04001769 /* Keyboard events */
David Härdemand8b4b582010-10-29 16:08:23 -03001770 set_bit(EV_KEY, dev->input_dev->evbit);
1771 set_bit(EV_REP, dev->input_dev->evbit);
1772 set_bit(EV_MSC, dev->input_dev->evbit);
1773 set_bit(MSC_SCAN, dev->input_dev->mscbit);
Sean Youngfec225a2018-09-18 04:50:20 -04001774
Sean Young0ac5a602018-09-16 07:35:58 -04001775 /* Pointer/mouse events */
Sean Youngce819642019-09-28 17:46:14 -03001776 set_bit(INPUT_PROP_POINTING_STICK, dev->input_dev->propbit);
Sean Young0ac5a602018-09-16 07:35:58 -04001777 set_bit(EV_REL, dev->input_dev->evbit);
1778 set_bit(REL_X, dev->input_dev->relbit);
1779 set_bit(REL_Y, dev->input_dev->relbit);
1780
David Härdemand8b4b582010-10-29 16:08:23 -03001781 if (dev->open)
1782 dev->input_dev->open = ir_open;
1783 if (dev->close)
1784 dev->input_dev->close = ir_close;
1785
David Härdemanb2aceb72017-04-27 17:33:58 -03001786 dev->input_dev->dev.parent = &dev->dev;
1787 memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
1788 dev->input_dev->phys = dev->input_phys;
Sean Young518f4b22017-07-01 12:13:19 -04001789 dev->input_dev->name = dev->device_name;
David Härdemanb2aceb72017-04-27 17:33:58 -03001790
David Härdemanf56928a2017-05-03 07:04:00 -03001791 return 0;
1792
1793out_table:
1794 ir_free_table(&dev->rc_map);
1795
1796 return rc;
1797}
1798
1799static int rc_setup_rx_device(struct rc_dev *dev)
1800{
1801 int rc;
1802
David Härdemanb2aceb72017-04-27 17:33:58 -03001803 /* rc_open will be called here */
1804 rc = input_register_device(dev->input_dev);
1805 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001806 return rc;
David Härdemanb2aceb72017-04-27 17:33:58 -03001807
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001808 /*
1809 * Default delay of 250ms is too short for some protocols, especially
1810 * since the timeout is currently set to 250ms. Increase it to 500ms,
1811 * to avoid wrong repetition of the keycodes. Note that this must be
1812 * set after the call to input_register_device().
1813 */
Sean Young57c642c2017-11-23 17:37:10 -05001814 if (dev->allowed_protocols == RC_PROTO_BIT_CEC)
1815 dev->input_dev->rep[REP_DELAY] = 0;
1816 else
1817 dev->input_dev->rep[REP_DELAY] = 500;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001818
1819 /*
1820 * As a repeat event on protocols like RC-5 and NEC take as long as
1821 * 110/114ms, using 33ms as a repeat period is not the right thing
1822 * to do.
1823 */
1824 dev->input_dev->rep[REP_PERIOD] = 125;
1825
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001826 return 0;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001827}
1828
1829static void rc_free_rx_device(struct rc_dev *dev)
1830{
David Härdemanf56928a2017-05-03 07:04:00 -03001831 if (!dev)
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001832 return;
1833
David Härdemanf56928a2017-05-03 07:04:00 -03001834 if (dev->input_dev) {
1835 input_unregister_device(dev->input_dev);
1836 dev->input_dev = NULL;
1837 }
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001838
David Härdemanf56928a2017-05-03 07:04:00 -03001839 ir_free_table(&dev->rc_map);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001840}
1841
1842int rc_register_device(struct rc_dev *dev)
1843{
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001844 const char *path;
1845 int attr = 0;
1846 int minor;
1847 int rc;
1848
1849 if (!dev)
1850 return -EINVAL;
1851
David Härdemanfcb13092015-05-19 19:03:17 -03001852 minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
1853 if (minor < 0)
1854 return minor;
1855
1856 dev->minor = minor;
1857 dev_set_name(&dev->dev, "rc%u", dev->minor);
1858 dev_set_drvdata(&dev->dev, dev);
Mauro Carvalho Chehab587d1b02014-01-14 16:27:55 -03001859
David Härdeman99b0f3c2014-04-04 19:06:06 -03001860 dev->dev.groups = dev->sysfs_groups;
Sean Young6d75db32017-09-01 11:30:50 -03001861 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1862 dev->sysfs_groups[attr++] = &rc_dev_ro_protocol_attr_grp;
1863 else if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
1864 dev->sysfs_groups[attr++] = &rc_dev_rw_protocol_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001865 if (dev->s_filter)
David Härdeman120703f2014-04-03 20:31:30 -03001866 dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001867 if (dev->s_wakeup_filter)
1868 dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001869 dev->sysfs_groups[attr++] = NULL;
1870
Sean Younga60d64b2017-09-23 10:41:13 -04001871 if (dev->driver_type == RC_DRIVER_IR_RAW) {
David Härdemanf56928a2017-05-03 07:04:00 -03001872 rc = ir_raw_event_prepare(dev);
1873 if (rc < 0)
1874 goto out_minor;
1875 }
1876
1877 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1878 rc = rc_prepare_rx_device(dev);
1879 if (rc)
1880 goto out_raw;
1881 }
1882
David Härdemand8b4b582010-10-29 16:08:23 -03001883 rc = device_add(&dev->dev);
1884 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001885 goto out_rx_free;
David Härdemand8b4b582010-10-29 16:08:23 -03001886
David Härdemand8b4b582010-10-29 16:08:23 -03001887 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Heiner Kallweit4dc0e902015-10-29 19:39:06 -02001888 dev_info(&dev->dev, "%s as %s\n",
Sean Young518f4b22017-07-01 12:13:19 -04001889 dev->device_name ?: "Unspecified device", path ?: "N/A");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001890 kfree(path);
1891
Sean Youngd7832cd2018-05-24 05:47:17 -04001892 dev->registered = true;
1893
Sean Young080d89f2019-11-21 11:10:47 +01001894 /*
1895 * once the the input device is registered in rc_setup_rx_device,
1896 * userspace can open the input device and rc_open() will be called
1897 * as a result. This results in driver code being allowed to submit
1898 * keycodes with rc_keydown, so lirc must be registered first.
1899 */
Sean Young62d6f1992017-09-24 12:43:24 -04001900 if (dev->allowed_protocols != RC_PROTO_BIT_CEC) {
Sean Younga60d64b2017-09-23 10:41:13 -04001901 rc = ir_lirc_register(dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001902 if (rc < 0)
Sean Young080d89f2019-11-21 11:10:47 +01001903 goto out_dev;
1904 }
1905
1906 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1907 rc = rc_setup_rx_device(dev);
1908 if (rc)
1909 goto out_lirc;
David Härdemand8b4b582010-10-29 16:08:23 -03001910 }
1911
Sean Younga60d64b2017-09-23 10:41:13 -04001912 if (dev->driver_type == RC_DRIVER_IR_RAW) {
1913 rc = ir_raw_event_register(dev);
1914 if (rc < 0)
Sean Young080d89f2019-11-21 11:10:47 +01001915 goto out_rx;
Sean Younga60d64b2017-09-23 10:41:13 -04001916 }
1917
Sean Young1f17f682018-02-12 07:27:50 -05001918 dev_dbg(&dev->dev, "Registered rc%u (driver: %s)\n", dev->minor,
1919 dev->driver_name ? dev->driver_name : "unknown");
David Härdemand8b4b582010-10-29 16:08:23 -03001920
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001921 return 0;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001922
Sean Young080d89f2019-11-21 11:10:47 +01001923out_rx:
1924 rc_free_rx_device(dev);
Sean Younga60d64b2017-09-23 10:41:13 -04001925out_lirc:
Sean Young62d6f1992017-09-24 12:43:24 -04001926 if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
Sean Younga60d64b2017-09-23 10:41:13 -04001927 ir_lirc_unregister(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001928out_dev:
1929 device_del(&dev->dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001930out_rx_free:
1931 ir_free_table(&dev->rc_map);
1932out_raw:
1933 ir_raw_event_free(dev);
1934out_minor:
David Härdemanfcb13092015-05-19 19:03:17 -03001935 ida_simple_remove(&rc_ida, minor);
David Härdemand8b4b582010-10-29 16:08:23 -03001936 return rc;
1937}
1938EXPORT_SYMBOL_GPL(rc_register_device);
1939
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001940static void devm_rc_release(struct device *dev, void *res)
1941{
1942 rc_unregister_device(*(struct rc_dev **)res);
1943}
1944
1945int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
1946{
1947 struct rc_dev **dr;
1948 int ret;
1949
1950 dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
1951 if (!dr)
1952 return -ENOMEM;
1953
1954 ret = rc_register_device(dev);
1955 if (ret) {
1956 devres_free(dr);
1957 return ret;
1958 }
1959
1960 *dr = dev;
1961 devres_add(parent, dr);
1962
1963 return 0;
1964}
1965EXPORT_SYMBOL_GPL(devm_rc_register_device);
1966
David Härdemand8b4b582010-10-29 16:08:23 -03001967void rc_unregister_device(struct rc_dev *dev)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001968{
David Härdemand8b4b582010-10-29 16:08:23 -03001969 if (!dev)
1970 return;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001971
David Härdemand8b4b582010-10-29 16:08:23 -03001972 if (dev->driver_type == RC_DRIVER_IR_RAW)
1973 ir_raw_event_unregister(dev);
1974
Sean Young8d406882018-03-06 08:57:57 -05001975 del_timer_sync(&dev->timer_keyup);
1976 del_timer_sync(&dev->timer_repeat);
1977
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001978 rc_free_rx_device(dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001979
Sean Young7790e812017-09-26 07:31:29 -04001980 mutex_lock(&dev->lock);
Sean Young8e782fc2018-11-04 05:12:09 -05001981 if (dev->users && dev->close)
1982 dev->close(dev);
Sean Young7790e812017-09-26 07:31:29 -04001983 dev->registered = false;
1984 mutex_unlock(&dev->lock);
1985
1986 /*
1987 * lirc device should be freed with dev->registered = false, so
1988 * that userspace polling will get notified.
1989 */
Sean Young62d6f1992017-09-24 12:43:24 -04001990 if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
Sean Younga60d64b2017-09-23 10:41:13 -04001991 ir_lirc_unregister(dev);
1992
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001993 device_del(&dev->dev);
1994
David Härdemanfcb13092015-05-19 19:03:17 -03001995 ida_simple_remove(&rc_ida, dev->minor);
1996
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001997 if (!dev->managed_alloc)
1998 rc_free_device(dev);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001999}
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03002000
David Härdemand8b4b582010-10-29 16:08:23 -03002001EXPORT_SYMBOL_GPL(rc_unregister_device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002002
2003/*
2004 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
2005 */
2006
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03002007static int __init rc_core_init(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002008{
David Härdeman40fc5322013-03-06 16:52:10 -03002009 int rc = class_register(&rc_class);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002010 if (rc) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -02002011 pr_err("rc_core: unable to register rc class\n");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002012 return rc;
2013 }
2014
Sean Younga60d64b2017-09-23 10:41:13 -04002015 rc = lirc_dev_init();
2016 if (rc) {
2017 pr_err("rc_core: unable to init lirc\n");
2018 class_unregister(&rc_class);
2019 return 0;
2020 }
2021
Sean Young153a60b2013-07-30 19:00:01 -03002022 led_trigger_register_simple("rc-feedback", &led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03002023 rc_map_register(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002024
2025 return 0;
2026}
2027
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03002028static void __exit rc_core_exit(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002029{
Sean Younga60d64b2017-09-23 10:41:13 -04002030 lirc_dev_exit();
David Härdeman40fc5322013-03-06 16:52:10 -03002031 class_unregister(&rc_class);
Sean Young153a60b2013-07-30 19:00:01 -03002032 led_trigger_unregister_simple(led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03002033 rc_map_unregister(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002034}
2035
David Härdemane76d4ce2013-03-06 16:52:15 -03002036subsys_initcall(rc_core_init);
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03002037module_exit(rc_core_exit);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002038
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02002039MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehab20835282017-12-01 08:47:08 -05002040MODULE_LICENSE("GPL v2");