blob: ca68e1d2b2f989cf366bdecc999db5e388d50e11 [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 },
Sean Youngd57ea872017-08-09 13:19:16 -040073};
David Härdemana374fef2010-04-02 15:58:29 -030074
David Härdeman4c7b3552010-11-10 11:04:19 -030075/* Used to keep track of known keymaps */
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030076static LIST_HEAD(rc_map_list);
77static DEFINE_SPINLOCK(rc_map_lock);
Sean Young153a60b2013-07-30 19:00:01 -030078static struct led_trigger *led_feedback;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030079
David Härdemanfcb13092015-05-19 19:03:17 -030080/* Used to keep track of rc devices */
81static DEFINE_IDA(rc_ida);
82
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030083static struct rc_map_list *seek_rc_map(const char *name)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030084{
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030085 struct rc_map_list *map = NULL;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030086
87 spin_lock(&rc_map_lock);
88 list_for_each_entry(map, &rc_map_list, list) {
89 if (!strcmp(name, map->map.name)) {
90 spin_unlock(&rc_map_lock);
91 return map;
92 }
93 }
94 spin_unlock(&rc_map_lock);
95
96 return NULL;
97}
98
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030099struct rc_map *rc_map_get(const char *name)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300100{
101
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300102 struct rc_map_list *map;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300103
104 map = seek_rc_map(name);
Russell King2ff56fa2015-10-15 13:15:24 -0300105#ifdef CONFIG_MODULES
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300106 if (!map) {
Kees Cook8ea54882014-03-11 17:25:53 -0300107 int rc = request_module("%s", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300108 if (rc < 0) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -0200109 pr_err("Couldn't load IR keymap %s\n", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300110 return NULL;
111 }
112 msleep(20); /* Give some time for IR to register */
113
114 map = seek_rc_map(name);
115 }
116#endif
117 if (!map) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -0200118 pr_err("IR keymap %s not found\n", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300119 return NULL;
120 }
121
122 printk(KERN_INFO "Registered IR keymap %s\n", map->map.name);
123
124 return &map->map;
125}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300126EXPORT_SYMBOL_GPL(rc_map_get);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300127
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300128int rc_map_register(struct rc_map_list *map)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300129{
130 spin_lock(&rc_map_lock);
131 list_add_tail(&map->list, &rc_map_list);
132 spin_unlock(&rc_map_lock);
133 return 0;
134}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300135EXPORT_SYMBOL_GPL(rc_map_register);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300136
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300137void rc_map_unregister(struct rc_map_list *map)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300138{
139 spin_lock(&rc_map_lock);
140 list_del(&map->list);
141 spin_unlock(&rc_map_lock);
142}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300143EXPORT_SYMBOL_GPL(rc_map_unregister);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300144
145
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300146static struct rc_map_table empty[] = {
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300147 { 0x2a, KEY_COFFEE },
148};
149
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300150static struct rc_map_list empty_map = {
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300151 .map = {
Sean Young6d741bf2017-08-07 16:20:58 -0400152 .scan = empty,
153 .size = ARRAY_SIZE(empty),
154 .rc_proto = RC_PROTO_UNKNOWN, /* Legacy IR type */
155 .name = RC_MAP_EMPTY,
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300156 }
157};
158
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300159/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700160 * ir_create_table() - initializes a scancode table
Sean Young1f17f682018-02-12 07:27:50 -0500161 * @dev: the rc_dev device
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300162 * @rc_map: the rc_map to initialize
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700163 * @name: name to assign to the table
Sean Young6d741bf2017-08-07 16:20:58 -0400164 * @rc_proto: ir type to assign to the new table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700165 * @size: initial size of the table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700166 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300167 * This routine will initialize the rc_map and will allocate
David Härdemand8b4b582010-10-29 16:08:23 -0300168 * memory to hold at least the specified number of elements.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500169 *
170 * return: zero on success or a negative error code
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700171 */
Sean Young1f17f682018-02-12 07:27:50 -0500172static int ir_create_table(struct rc_dev *dev, struct rc_map *rc_map,
Sean Young6d741bf2017-08-07 16:20:58 -0400173 const char *name, u64 rc_proto, size_t size)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700174{
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300175 rc_map->name = kstrdup(name, GFP_KERNEL);
176 if (!rc_map->name)
177 return -ENOMEM;
Sean Young6d741bf2017-08-07 16:20:58 -0400178 rc_map->rc_proto = rc_proto;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300179 rc_map->alloc = roundup_pow_of_two(size * sizeof(struct rc_map_table));
180 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300181 rc_map->scan = kmalloc(rc_map->alloc, GFP_KERNEL);
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300182 if (!rc_map->scan) {
183 kfree(rc_map->name);
184 rc_map->name = NULL;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700185 return -ENOMEM;
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300186 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700187
Sean Young1f17f682018-02-12 07:27:50 -0500188 dev_dbg(&dev->dev, "Allocated space for %u keycode entries (%u bytes)\n",
189 rc_map->size, rc_map->alloc);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700190 return 0;
191}
192
193/**
194 * ir_free_table() - frees memory allocated by a scancode table
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300195 * @rc_map: the table whose mappings need to be freed
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700196 *
197 * This routine will free memory alloctaed for key mappings used by given
198 * scancode table.
199 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300200static void ir_free_table(struct rc_map *rc_map)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700201{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300202 rc_map->size = 0;
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300203 kfree(rc_map->name);
Max Kellermannc183d352016-08-09 18:32:06 -0300204 rc_map->name = NULL;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300205 kfree(rc_map->scan);
206 rc_map->scan = NULL;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700207}
208
209/**
David Härdemanb3074c02010-04-02 15:58:28 -0300210 * ir_resize_table() - resizes a scancode table if necessary
Sean Young1f17f682018-02-12 07:27:50 -0500211 * @dev: the rc_dev device
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300212 * @rc_map: the rc_map to resize
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700213 * @gfp_flags: gfp flags to use when allocating memory
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300214 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300215 * This routine will shrink the rc_map if it has lots of
David Härdemanb3074c02010-04-02 15:58:28 -0300216 * unused entries and grow it if it is full.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500217 *
218 * return: zero on success or a negative error code
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300219 */
Sean Young1f17f682018-02-12 07:27:50 -0500220static int ir_resize_table(struct rc_dev *dev, struct rc_map *rc_map,
221 gfp_t gfp_flags)
David Härdemanb3074c02010-04-02 15:58:28 -0300222{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300223 unsigned int oldalloc = rc_map->alloc;
David Härdemanb3074c02010-04-02 15:58:28 -0300224 unsigned int newalloc = oldalloc;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300225 struct rc_map_table *oldscan = rc_map->scan;
226 struct rc_map_table *newscan;
David Härdemanb3074c02010-04-02 15:58:28 -0300227
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300228 if (rc_map->size == rc_map->len) {
David Härdemanb3074c02010-04-02 15:58:28 -0300229 /* All entries in use -> grow keytable */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300230 if (rc_map->alloc >= IR_TAB_MAX_SIZE)
David Härdemanb3074c02010-04-02 15:58:28 -0300231 return -ENOMEM;
232
233 newalloc *= 2;
Sean Young1f17f682018-02-12 07:27:50 -0500234 dev_dbg(&dev->dev, "Growing table to %u bytes\n", newalloc);
David Härdemanb3074c02010-04-02 15:58:28 -0300235 }
236
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300237 if ((rc_map->len * 3 < rc_map->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
David Härdemanb3074c02010-04-02 15:58:28 -0300238 /* Less than 1/3 of entries in use -> shrink keytable */
239 newalloc /= 2;
Sean Young1f17f682018-02-12 07:27:50 -0500240 dev_dbg(&dev->dev, "Shrinking table to %u bytes\n", newalloc);
David Härdemanb3074c02010-04-02 15:58:28 -0300241 }
242
243 if (newalloc == oldalloc)
244 return 0;
245
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700246 newscan = kmalloc(newalloc, gfp_flags);
Sean Young1f17f682018-02-12 07:27:50 -0500247 if (!newscan)
David Härdemanb3074c02010-04-02 15:58:28 -0300248 return -ENOMEM;
David Härdemanb3074c02010-04-02 15:58:28 -0300249
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300250 memcpy(newscan, rc_map->scan, rc_map->len * sizeof(struct rc_map_table));
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300251 rc_map->scan = newscan;
252 rc_map->alloc = newalloc;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300253 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
David Härdemanb3074c02010-04-02 15:58:28 -0300254 kfree(oldscan);
255 return 0;
256}
257
258/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700259 * ir_update_mapping() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300260 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300261 * @rc_map: scancode table to be adjusted
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700262 * @index: index of the mapping that needs to be updated
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500263 * @new_keycode: the desired keycode
David Härdemanb3074c02010-04-02 15:58:28 -0300264 *
David Härdemand8b4b582010-10-29 16:08:23 -0300265 * This routine is used to update scancode->keycode mapping at given
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700266 * position.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500267 *
268 * return: previous keycode assigned to the mapping
269 *
David Härdemanb3074c02010-04-02 15:58:28 -0300270 */
David Härdemand8b4b582010-10-29 16:08:23 -0300271static unsigned int ir_update_mapping(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300272 struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700273 unsigned int index,
274 unsigned int new_keycode)
275{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300276 int old_keycode = rc_map->scan[index].keycode;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700277 int i;
278
279 /* Did the user wish to remove the mapping? */
280 if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
Sean Young1f17f682018-02-12 07:27:50 -0500281 dev_dbg(&dev->dev, "#%d: Deleting scan 0x%04x\n",
282 index, rc_map->scan[index].scancode);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300283 rc_map->len--;
284 memmove(&rc_map->scan[index], &rc_map->scan[index+ 1],
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300285 (rc_map->len - index) * sizeof(struct rc_map_table));
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700286 } else {
Sean Young1f17f682018-02-12 07:27:50 -0500287 dev_dbg(&dev->dev, "#%d: %s scan 0x%04x with key 0x%04x\n",
288 index,
289 old_keycode == KEY_RESERVED ? "New" : "Replacing",
290 rc_map->scan[index].scancode, new_keycode);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300291 rc_map->scan[index].keycode = new_keycode;
David Härdemand8b4b582010-10-29 16:08:23 -0300292 __set_bit(new_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700293 }
294
295 if (old_keycode != KEY_RESERVED) {
296 /* A previous mapping was updated... */
David Härdemand8b4b582010-10-29 16:08:23 -0300297 __clear_bit(old_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700298 /* ... but another scancode might use the same keycode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300299 for (i = 0; i < rc_map->len; i++) {
300 if (rc_map->scan[i].keycode == old_keycode) {
David Härdemand8b4b582010-10-29 16:08:23 -0300301 __set_bit(old_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700302 break;
303 }
304 }
305
306 /* Possibly shrink the keytable, failure is not a problem */
Sean Young1f17f682018-02-12 07:27:50 -0500307 ir_resize_table(dev, rc_map, GFP_ATOMIC);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700308 }
309
310 return old_keycode;
311}
312
313/**
David Härdeman4c7b3552010-11-10 11:04:19 -0300314 * ir_establish_scancode() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300315 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300316 * @rc_map: scancode table to be searched
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700317 * @scancode: the desired scancode
318 * @resize: controls whether we allowed to resize the table to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300319 * accommodate not yet present scancodes
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700320 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300321 * This routine is used to locate given scancode in rc_map.
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700322 * If scancode is not yet present the routine will allocate a new slot
323 * for it.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500324 *
325 * return: index of the mapping containing scancode in question
326 * or -1U in case of failure.
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700327 */
David Härdemand8b4b582010-10-29 16:08:23 -0300328static unsigned int ir_establish_scancode(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300329 struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700330 unsigned int scancode,
331 bool resize)
David Härdemanb3074c02010-04-02 15:58:28 -0300332{
333 unsigned int i;
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300334
335 /*
336 * Unfortunately, some hardware-based IR decoders don't provide
337 * all bits for the complete IR code. In general, they provide only
338 * the command part of the IR code. Yet, as it is possible to replace
339 * the provided IR with another one, it is needed to allow loading
David Härdemand8b4b582010-10-29 16:08:23 -0300340 * IR tables from other remotes. So, we support specifying a mask to
341 * indicate the valid bits of the scancodes.
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300342 */
David Härdeman9d2f1d32014-04-03 20:32:26 -0300343 if (dev->scancode_mask)
344 scancode &= dev->scancode_mask;
David Härdemanb3074c02010-04-02 15:58:28 -0300345
346 /* First check if we already have a mapping for this ir command */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300347 for (i = 0; i < rc_map->len; i++) {
348 if (rc_map->scan[i].scancode == scancode)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700349 return i;
350
David Härdemanb3074c02010-04-02 15:58:28 -0300351 /* Keytable is sorted from lowest to highest scancode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300352 if (rc_map->scan[i].scancode >= scancode)
David Härdemanb3074c02010-04-02 15:58:28 -0300353 break;
David Härdemanb3074c02010-04-02 15:58:28 -0300354 }
355
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700356 /* No previous mapping found, we might need to grow the table */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300357 if (rc_map->size == rc_map->len) {
Sean Young1f17f682018-02-12 07:27:50 -0500358 if (!resize || ir_resize_table(dev, rc_map, GFP_ATOMIC))
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700359 return -1U;
360 }
David Härdemanb3074c02010-04-02 15:58:28 -0300361
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700362 /* i is the proper index to insert our new keycode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300363 if (i < rc_map->len)
364 memmove(&rc_map->scan[i + 1], &rc_map->scan[i],
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300365 (rc_map->len - i) * sizeof(struct rc_map_table));
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300366 rc_map->scan[i].scancode = scancode;
367 rc_map->scan[i].keycode = KEY_RESERVED;
368 rc_map->len++;
David Härdemanb3074c02010-04-02 15:58:28 -0300369
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700370 return i;
David Härdemanb3074c02010-04-02 15:58:28 -0300371}
372
373/**
374 * ir_setkeycode() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300375 * @idev: the struct input_dev device descriptor
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500376 * @ke: Input keymap entry
377 * @old_keycode: result
David Härdemanb3074c02010-04-02 15:58:28 -0300378 *
379 * This routine is used to handle evdev EVIOCSKEY ioctl.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500380 *
381 * return: -EINVAL if the keycode could not be inserted, otherwise zero.
David Härdemanb3074c02010-04-02 15:58:28 -0300382 */
David Härdemand8b4b582010-10-29 16:08:23 -0300383static int ir_setkeycode(struct input_dev *idev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700384 const struct input_keymap_entry *ke,
385 unsigned int *old_keycode)
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300386{
David Härdemand8b4b582010-10-29 16:08:23 -0300387 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300388 struct rc_map *rc_map = &rdev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700389 unsigned int index;
390 unsigned int scancode;
Mauro Carvalho Chehabdea8a392010-11-29 07:46:13 -0300391 int retval = 0;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700392 unsigned long flags;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300393
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300394 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700395
396 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
397 index = ke->index;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300398 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700399 retval = -EINVAL;
400 goto out;
401 }
402 } else {
403 retval = input_scancode_to_scalar(ke, &scancode);
404 if (retval)
405 goto out;
406
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300407 index = ir_establish_scancode(rdev, rc_map, scancode, true);
408 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700409 retval = -ENOMEM;
410 goto out;
411 }
412 }
413
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300414 *old_keycode = ir_update_mapping(rdev, rc_map, index, ke->keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700415
416out:
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300417 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700418 return retval;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300419}
420
421/**
David Härdemanb3074c02010-04-02 15:58:28 -0300422 * ir_setkeytable() - sets several entries in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300423 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300424 * @from: the struct rc_map to copy entries from
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300425 *
David Härdemanb3074c02010-04-02 15:58:28 -0300426 * This routine is used to handle table initialization.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500427 *
428 * return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300429 */
David Härdemand8b4b582010-10-29 16:08:23 -0300430static int ir_setkeytable(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300431 const struct rc_map *from)
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300432{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300433 struct rc_map *rc_map = &dev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700434 unsigned int i, index;
435 int rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300436
Sean Young1f17f682018-02-12 07:27:50 -0500437 rc = ir_create_table(dev, rc_map, from->name, from->rc_proto,
438 from->size);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700439 if (rc)
440 return rc;
441
David Härdemanb3074c02010-04-02 15:58:28 -0300442 for (i = 0; i < from->size; i++) {
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300443 index = ir_establish_scancode(dev, rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700444 from->scan[i].scancode, false);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300445 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700446 rc = -ENOMEM;
David Härdemanb3074c02010-04-02 15:58:28 -0300447 break;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700448 }
449
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300450 ir_update_mapping(dev, rc_map, index,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700451 from->scan[i].keycode);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300452 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700453
454 if (rc)
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300455 ir_free_table(rc_map);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700456
David Härdemanb3074c02010-04-02 15:58:28 -0300457 return rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300458}
459
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300460static int rc_map_cmp(const void *key, const void *elt)
461{
462 const unsigned int *scancode = key;
463 const struct rc_map_table *e = elt;
464
465 if (*scancode < e->scancode)
466 return -1;
467 else if (*scancode > e->scancode)
468 return 1;
469 return 0;
470}
471
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300472/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700473 * ir_lookup_by_scancode() - locate mapping by scancode
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300474 * @rc_map: the struct rc_map to search
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700475 * @scancode: scancode to look for in the table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700476 *
477 * This routine performs binary search in RC keykeymap table for
478 * given scancode.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500479 *
480 * return: index in the table, -1U if not found
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700481 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300482static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700483 unsigned int scancode)
484{
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300485 struct rc_map_table *res;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700486
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300487 res = bsearch(&scancode, rc_map->scan, rc_map->len,
488 sizeof(struct rc_map_table), rc_map_cmp);
489 if (!res)
490 return -1U;
491 else
492 return res - rc_map->scan;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700493}
494
495/**
David Härdemanb3074c02010-04-02 15:58:28 -0300496 * ir_getkeycode() - get a keycode from the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300497 * @idev: the struct input_dev device descriptor
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500498 * @ke: Input keymap entry
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300499 *
500 * This routine is used to handle evdev EVIOCGKEY ioctl.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500501 *
502 * return: always returns zero.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300503 */
David Härdemand8b4b582010-10-29 16:08:23 -0300504static int ir_getkeycode(struct input_dev *idev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700505 struct input_keymap_entry *ke)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300506{
David Härdemand8b4b582010-10-29 16:08:23 -0300507 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300508 struct rc_map *rc_map = &rdev->rc_map;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300509 struct rc_map_table *entry;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700510 unsigned long flags;
511 unsigned int index;
512 unsigned int scancode;
513 int retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300514
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300515 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700516
517 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
518 index = ke->index;
519 } else {
520 retval = input_scancode_to_scalar(ke, &scancode);
521 if (retval)
522 goto out;
523
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300524 index = ir_lookup_by_scancode(rc_map, scancode);
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300525 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700526
Dmitry Torokhov54e74b82011-01-28 23:33:29 -0800527 if (index < rc_map->len) {
528 entry = &rc_map->scan[index];
529
530 ke->index = index;
531 ke->keycode = entry->keycode;
532 ke->len = sizeof(entry->scancode);
533 memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
534
535 } else if (!(ke->flags & INPUT_KEYMAP_BY_INDEX)) {
536 /*
537 * We do not really know the valid range of scancodes
538 * so let's respond with KEY_RESERVED to anything we
539 * do not have mapping for [yet].
540 */
541 ke->index = index;
542 ke->keycode = KEY_RESERVED;
543 } else {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700544 retval = -EINVAL;
545 goto out;
546 }
547
Dmitry Torokhov47c5ba52010-10-31 15:18:42 -0700548 retval = 0;
549
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700550out:
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300551 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700552 return retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300553}
554
555/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300556 * rc_g_keycode_from_table() - gets the keycode that corresponds to a scancode
David Härdemand8b4b582010-10-29 16:08:23 -0300557 * @dev: the struct rc_dev descriptor of the device
558 * @scancode: the scancode to look for
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300559 *
David Härdemand8b4b582010-10-29 16:08:23 -0300560 * This routine is used by drivers which need to convert a scancode to a
561 * keycode. Normally it should not be used since drivers should have no
562 * interest in keycodes.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500563 *
564 * return: the corresponding keycode, or KEY_RESERVED
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300565 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300566u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300567{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300568 struct rc_map *rc_map = &dev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700569 unsigned int keycode;
570 unsigned int index;
571 unsigned long flags;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300572
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300573 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700574
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300575 index = ir_lookup_by_scancode(rc_map, scancode);
576 keycode = index < rc_map->len ?
577 rc_map->scan[index].keycode : KEY_RESERVED;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700578
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300579 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700580
Mauro Carvalho Chehab35438942010-04-03 16:53:16 -0300581 if (keycode != KEY_RESERVED)
Sean Young1f17f682018-02-12 07:27:50 -0500582 dev_dbg(&dev->dev, "%s: scancode 0x%04x keycode 0x%02x\n",
583 dev->device_name, scancode, keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700584
David Härdemanb3074c02010-04-02 15:58:28 -0300585 return keycode;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300586}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300587EXPORT_SYMBOL_GPL(rc_g_keycode_from_table);
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300588
589/**
David Härdeman62c65032010-10-29 16:08:07 -0300590 * ir_do_keyup() - internal function to signal the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300591 * @dev: the struct rc_dev descriptor of the device
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300592 * @sync: whether or not to call input_sync
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300593 *
David Härdeman62c65032010-10-29 16:08:07 -0300594 * This function is used internally to release a keypress, it must be
595 * called with keylock held.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300596 */
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300597static void ir_do_keyup(struct rc_dev *dev, bool sync)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300598{
David Härdemand8b4b582010-10-29 16:08:23 -0300599 if (!dev->keypressed)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300600 return;
601
Sean Young1f17f682018-02-12 07:27:50 -0500602 dev_dbg(&dev->dev, "keyup key 0x%04x\n", dev->last_keycode);
Sean Youngfb7ccc62017-12-03 08:55:24 -0500603 del_timer(&dev->timer_repeat);
David Härdemand8b4b582010-10-29 16:08:23 -0300604 input_report_key(dev->input_dev, dev->last_keycode, 0);
Sean Young153a60b2013-07-30 19:00:01 -0300605 led_trigger_event(led_feedback, LED_OFF);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300606 if (sync)
607 input_sync(dev->input_dev);
David Härdemand8b4b582010-10-29 16:08:23 -0300608 dev->keypressed = false;
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300609}
David Härdeman62c65032010-10-29 16:08:07 -0300610
611/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300612 * rc_keyup() - signals the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300613 * @dev: the struct rc_dev descriptor of the device
David Härdeman62c65032010-10-29 16:08:07 -0300614 *
615 * This routine is used to signal that a key has been released on the
616 * remote control.
617 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300618void rc_keyup(struct rc_dev *dev)
David Härdeman62c65032010-10-29 16:08:07 -0300619{
620 unsigned long flags;
David Härdeman62c65032010-10-29 16:08:07 -0300621
David Härdemand8b4b582010-10-29 16:08:23 -0300622 spin_lock_irqsave(&dev->keylock, flags);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300623 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300624 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300625}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300626EXPORT_SYMBOL_GPL(rc_keyup);
David Härdemana374fef2010-04-02 15:58:29 -0300627
628/**
629 * ir_timer_keyup() - generates a keyup event after a timeout
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500630 *
631 * @t: a pointer to the struct timer_list
David Härdemana374fef2010-04-02 15:58:29 -0300632 *
633 * This routine will generate a keyup event some time after a keydown event
634 * is generated when no further activity has been detected.
635 */
Kees Cookb17ec782017-10-24 11:23:14 -0400636static void ir_timer_keyup(struct timer_list *t)
David Härdemana374fef2010-04-02 15:58:29 -0300637{
Kees Cookb17ec782017-10-24 11:23:14 -0400638 struct rc_dev *dev = from_timer(dev, t, timer_keyup);
David Härdemana374fef2010-04-02 15:58:29 -0300639 unsigned long flags;
640
641 /*
642 * ir->keyup_jiffies is used to prevent a race condition if a
643 * hardware interrupt occurs at this point and the keyup timer
644 * event is moved further into the future as a result.
645 *
646 * The timer will then be reactivated and this function called
647 * again in the future. We need to exit gracefully in that case
648 * to allow the input subsystem to do its auto-repeat magic or
649 * a keyup event might follow immediately after the keydown.
650 */
David Härdemand8b4b582010-10-29 16:08:23 -0300651 spin_lock_irqsave(&dev->keylock, flags);
652 if (time_is_before_eq_jiffies(dev->keyup_jiffies))
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300653 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300654 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300655}
656
657/**
Sean Young57c642c2017-11-23 17:37:10 -0500658 * ir_timer_repeat() - generates a repeat event after a timeout
659 *
660 * @t: a pointer to the struct timer_list
661 *
662 * This routine will generate a soft repeat event every REP_PERIOD
663 * milliseconds.
664 */
665static void ir_timer_repeat(struct timer_list *t)
666{
667 struct rc_dev *dev = from_timer(dev, t, timer_repeat);
668 struct input_dev *input = dev->input_dev;
669 unsigned long flags;
670
671 spin_lock_irqsave(&dev->keylock, flags);
672 if (dev->keypressed) {
673 input_event(input, EV_KEY, dev->last_keycode, 2);
674 input_sync(input);
675 if (input->rep[REP_PERIOD])
676 mod_timer(&dev->timer_repeat, jiffies +
677 msecs_to_jiffies(input->rep[REP_PERIOD]));
678 }
679 spin_unlock_irqrestore(&dev->keylock, flags);
680}
681
Sean Youngf5dbee62018-07-28 05:11:15 -0400682static unsigned int repeat_period(int protocol)
683{
684 if (protocol >= ARRAY_SIZE(protocols))
685 return 100;
686
687 return protocols[protocol].repeat_period;
688}
689
Sean Young57c642c2017-11-23 17:37:10 -0500690/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300691 * rc_repeat() - signals that a key is still pressed
David Härdemand8b4b582010-10-29 16:08:23 -0300692 * @dev: the struct rc_dev descriptor of the device
David Härdemana374fef2010-04-02 15:58:29 -0300693 *
694 * This routine is used by IR decoders when a repeat message which does
695 * not include the necessary bits to reproduce the scancode has been
696 * received.
697 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300698void rc_repeat(struct rc_dev *dev)
David Härdemana374fef2010-04-02 15:58:29 -0300699{
700 unsigned long flags;
Sean Young28492252018-03-24 08:02:48 -0400701 unsigned int timeout = nsecs_to_jiffies(dev->timeout) +
Sean Youngf5dbee62018-07-28 05:11:15 -0400702 msecs_to_jiffies(repeat_period(dev->last_protocol));
Sean Youngb66218f2017-09-30 07:13:37 -0400703 struct lirc_scancode sc = {
704 .scancode = dev->last_scancode, .rc_proto = dev->last_protocol,
705 .keycode = dev->keypressed ? dev->last_keycode : KEY_RESERVED,
706 .flags = LIRC_SCANCODE_FLAG_REPEAT |
707 (dev->last_toggle ? LIRC_SCANCODE_FLAG_TOGGLE : 0)
708 };
709
710 ir_lirc_scancode_event(dev, &sc);
David Härdemana374fef2010-04-02 15:58:29 -0300711
David Härdemand8b4b582010-10-29 16:08:23 -0300712 spin_lock_irqsave(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300713
David Härdeman265a2982017-06-22 15:23:54 -0400714 input_event(dev->input_dev, EV_MSC, MSC_SCAN, dev->last_scancode);
715 input_sync(dev->input_dev);
716
Sean Youngb66218f2017-09-30 07:13:37 -0400717 if (dev->keypressed) {
Sean Young28492252018-03-24 08:02:48 -0400718 dev->keyup_jiffies = jiffies + timeout;
Sean Youngb66218f2017-09-30 07:13:37 -0400719 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
720 }
David Härdemana374fef2010-04-02 15:58:29 -0300721
David Härdemand8b4b582010-10-29 16:08:23 -0300722 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300723}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300724EXPORT_SYMBOL_GPL(rc_repeat);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300725
726/**
David Härdeman62c65032010-10-29 16:08:07 -0300727 * ir_do_keydown() - internal function to process a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300728 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300729 * @protocol: the protocol of the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300730 * @scancode: the scancode of the keypress
731 * @keycode: the keycode of the keypress
732 * @toggle: the toggle value of the keypress
733 *
734 * This function is used internally to register a keypress, it must be
735 * called with keylock held.
736 */
Sean Young6d741bf2017-08-07 16:20:58 -0400737static void ir_do_keydown(struct rc_dev *dev, enum rc_proto protocol,
David Härdeman120703f2014-04-03 20:31:30 -0300738 u32 scancode, u32 keycode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300739{
David Härdeman99b0f3c2014-04-04 19:06:06 -0300740 bool new_event = (!dev->keypressed ||
David Härdeman120703f2014-04-03 20:31:30 -0300741 dev->last_protocol != protocol ||
David Härdeman99b0f3c2014-04-04 19:06:06 -0300742 dev->last_scancode != scancode ||
David Härdeman120703f2014-04-03 20:31:30 -0300743 dev->last_toggle != toggle);
Sean Youngde142c32017-02-25 06:51:32 -0500744 struct lirc_scancode sc = {
745 .scancode = scancode, .rc_proto = protocol,
746 .flags = toggle ? LIRC_SCANCODE_FLAG_TOGGLE : 0,
747 .keycode = keycode
748 };
749
750 ir_lirc_scancode_event(dev, &sc);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300751
752 if (new_event && dev->keypressed)
753 ir_do_keyup(dev, false);
754
David Härdemand8b4b582010-10-29 16:08:23 -0300755 input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300756
Sean Youngb66218f2017-09-30 07:13:37 -0400757 dev->last_protocol = protocol;
758 dev->last_scancode = scancode;
759 dev->last_toggle = toggle;
760 dev->last_keycode = keycode;
761
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300762 if (new_event && keycode != KEY_RESERVED) {
763 /* Register a keypress */
764 dev->keypressed = true;
David Härdeman62c65032010-10-29 16:08:07 -0300765
Sean Young1f17f682018-02-12 07:27:50 -0500766 dev_dbg(&dev->dev, "%s: key down event, key 0x%04x, protocol 0x%04x, scancode 0x%08x\n",
767 dev->device_name, keycode, protocol, scancode);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300768 input_report_key(dev->input_dev, keycode, 1);
James Hogan70a2f912014-01-16 19:56:22 -0300769
770 led_trigger_event(led_feedback, LED_FULL);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300771 }
David Härdeman62c65032010-10-29 16:08:07 -0300772
Sean Young57c642c2017-11-23 17:37:10 -0500773 /*
774 * For CEC, start sending repeat messages as soon as the first
775 * repeated message is sent, as long as REP_DELAY = 0 and REP_PERIOD
776 * is non-zero. Otherwise, the input layer will generate repeat
777 * messages.
778 */
779 if (!new_event && keycode != KEY_RESERVED &&
780 dev->allowed_protocols == RC_PROTO_BIT_CEC &&
781 !timer_pending(&dev->timer_repeat) &&
782 dev->input_dev->rep[REP_PERIOD] &&
783 !dev->input_dev->rep[REP_DELAY]) {
784 input_event(dev->input_dev, EV_KEY, keycode, 2);
785 mod_timer(&dev->timer_repeat, jiffies +
786 msecs_to_jiffies(dev->input_dev->rep[REP_PERIOD]));
787 }
788
David Härdemand8b4b582010-10-29 16:08:23 -0300789 input_sync(dev->input_dev);
David Härdeman62c65032010-10-29 16:08:07 -0300790}
791
792/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300793 * rc_keydown() - generates input event for a key press
David Härdemand8b4b582010-10-29 16:08:23 -0300794 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300795 * @protocol: the protocol for the keypress
796 * @scancode: the scancode for the keypress
David Härdemana374fef2010-04-02 15:58:29 -0300797 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
798 * support toggle values, this should be set to zero)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300799 *
David Härdemand8b4b582010-10-29 16:08:23 -0300800 * This routine is used to signal that a key has been pressed on the
801 * remote control.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300802 */
Sean Young6d741bf2017-08-07 16:20:58 -0400803void rc_keydown(struct rc_dev *dev, enum rc_proto protocol, u32 scancode,
804 u8 toggle)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300805{
David Härdemana374fef2010-04-02 15:58:29 -0300806 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300807 u32 keycode = rc_g_keycode_from_table(dev, scancode);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300808
David Härdemand8b4b582010-10-29 16:08:23 -0300809 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300810 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300811
David Härdemand8b4b582010-10-29 16:08:23 -0300812 if (dev->keypressed) {
Sean Young28492252018-03-24 08:02:48 -0400813 dev->keyup_jiffies = jiffies + nsecs_to_jiffies(dev->timeout) +
Sean Youngf5dbee62018-07-28 05:11:15 -0400814 msecs_to_jiffies(repeat_period(protocol));
David Härdemand8b4b582010-10-29 16:08:23 -0300815 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
David Härdeman62c65032010-10-29 16:08:07 -0300816 }
David Härdemand8b4b582010-10-29 16:08:23 -0300817 spin_unlock_irqrestore(&dev->keylock, flags);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300818}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300819EXPORT_SYMBOL_GPL(rc_keydown);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300820
David Härdeman62c65032010-10-29 16:08:07 -0300821/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300822 * rc_keydown_notimeout() - generates input event for a key press without
David Härdeman62c65032010-10-29 16:08:07 -0300823 * an automatic keyup event at a later time
David Härdemand8b4b582010-10-29 16:08:23 -0300824 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300825 * @protocol: the protocol for the keypress
826 * @scancode: the scancode for the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300827 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
828 * support toggle values, this should be set to zero)
829 *
David Härdemand8b4b582010-10-29 16:08:23 -0300830 * This routine is used to signal that a key has been pressed on the
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300831 * remote control. The driver must manually call rc_keyup() at a later stage.
David Härdeman62c65032010-10-29 16:08:07 -0300832 */
Sean Young6d741bf2017-08-07 16:20:58 -0400833void rc_keydown_notimeout(struct rc_dev *dev, enum rc_proto protocol,
David Härdeman120703f2014-04-03 20:31:30 -0300834 u32 scancode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300835{
836 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300837 u32 keycode = rc_g_keycode_from_table(dev, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300838
David Härdemand8b4b582010-10-29 16:08:23 -0300839 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300840 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
David Härdemand8b4b582010-10-29 16:08:23 -0300841 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300842}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300843EXPORT_SYMBOL_GPL(rc_keydown_notimeout);
David Härdeman62c65032010-10-29 16:08:07 -0300844
Sean Youngb590c0b2016-12-05 19:24:59 -0200845/**
Sean Young6b514c42017-10-09 16:32:41 -0400846 * rc_validate_scancode() - checks that a scancode is valid for a protocol.
847 * For nec, it should do the opposite of ir_nec_bytes_to_scancode()
Sean Young49a4b362017-09-27 16:00:49 -0400848 * @proto: protocol
849 * @scancode: scancode
850 */
851bool rc_validate_scancode(enum rc_proto proto, u32 scancode)
852{
853 switch (proto) {
Sean Young6b514c42017-10-09 16:32:41 -0400854 /*
855 * NECX has a 16-bit address; if the lower 8 bits match the upper
856 * 8 bits inverted, then the address would match regular nec.
857 */
Sean Young49a4b362017-09-27 16:00:49 -0400858 case RC_PROTO_NECX:
859 if ((((scancode >> 16) ^ ~(scancode >> 8)) & 0xff) == 0)
860 return false;
861 break;
Sean Young6b514c42017-10-09 16:32:41 -0400862 /*
863 * NEC32 has a 16 bit address and 16 bit command. If the lower 8 bits
864 * of the command match the upper 8 bits inverted, then it would
865 * be either NEC or NECX.
866 */
Sean Young49a4b362017-09-27 16:00:49 -0400867 case RC_PROTO_NEC32:
Sean Young6b514c42017-10-09 16:32:41 -0400868 if ((((scancode >> 8) ^ ~scancode) & 0xff) == 0)
Sean Young49a4b362017-09-27 16:00:49 -0400869 return false;
870 break;
Sean Young6b514c42017-10-09 16:32:41 -0400871 /*
872 * If the customer code (top 32-bit) is 0x800f, it is MCE else it
873 * is regular mode-6a 32 bit
874 */
Sean Young49a4b362017-09-27 16:00:49 -0400875 case RC_PROTO_RC6_MCE:
876 if ((scancode & 0xffff0000) != 0x800f0000)
877 return false;
878 break;
879 case RC_PROTO_RC6_6A_32:
880 if ((scancode & 0xffff0000) == 0x800f0000)
881 return false;
882 break;
883 default:
884 break;
885 }
886
887 return true;
888}
889
890/**
Sean Youngb590c0b2016-12-05 19:24:59 -0200891 * rc_validate_filter() - checks that the scancode and mask are valid and
892 * provides sensible defaults
James Hoganf423ccc2015-03-31 14:48:10 -0300893 * @dev: the struct rc_dev descriptor of the device
Sean Youngb590c0b2016-12-05 19:24:59 -0200894 * @filter: the scancode and mask
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500895 *
896 * return: 0 or -EINVAL if the filter is not valid
Sean Youngb590c0b2016-12-05 19:24:59 -0200897 */
James Hoganf423ccc2015-03-31 14:48:10 -0300898static int rc_validate_filter(struct rc_dev *dev,
Sean Youngb590c0b2016-12-05 19:24:59 -0200899 struct rc_scancode_filter *filter)
900{
Sean Youngd57ea872017-08-09 13:19:16 -0400901 u32 mask, s = filter->data;
Sean Young6d741bf2017-08-07 16:20:58 -0400902 enum rc_proto protocol = dev->wakeup_protocol;
Sean Youngb590c0b2016-12-05 19:24:59 -0200903
Sean Youngd57ea872017-08-09 13:19:16 -0400904 if (protocol >= ARRAY_SIZE(protocols))
Sean Young2168b412017-08-07 09:21:29 -0400905 return -EINVAL;
906
Sean Youngd57ea872017-08-09 13:19:16 -0400907 mask = protocols[protocol].scancode_bits;
908
Sean Young49a4b362017-09-27 16:00:49 -0400909 if (!rc_validate_scancode(protocol, s))
910 return -EINVAL;
Sean Youngb590c0b2016-12-05 19:24:59 -0200911
Sean Youngd57ea872017-08-09 13:19:16 -0400912 filter->data &= mask;
913 filter->mask &= mask;
Sean Youngb590c0b2016-12-05 19:24:59 -0200914
James Hoganf423ccc2015-03-31 14:48:10 -0300915 /*
916 * If we have to raw encode the IR for wakeup, we cannot have a mask
917 */
Sean Youngd57ea872017-08-09 13:19:16 -0400918 if (dev->encode_wakeup && filter->mask != 0 && filter->mask != mask)
James Hoganf423ccc2015-03-31 14:48:10 -0300919 return -EINVAL;
920
Sean Youngb590c0b2016-12-05 19:24:59 -0200921 return 0;
922}
923
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300924int rc_open(struct rc_dev *rdev)
925{
926 int rval = 0;
927
928 if (!rdev)
929 return -EINVAL;
930
931 mutex_lock(&rdev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -0200932
Sean Youngcb843432017-09-23 17:44:03 -0400933 if (!rdev->registered) {
934 rval = -ENODEV;
935 } else {
936 if (!rdev->users++ && rdev->open)
937 rval = rdev->open(rdev);
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300938
Sean Youngcb843432017-09-23 17:44:03 -0400939 if (rval)
940 rdev->users--;
941 }
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300942
943 mutex_unlock(&rdev->lock);
944
945 return rval;
946}
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300947
David Härdemand8b4b582010-10-29 16:08:23 -0300948static int ir_open(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300949{
David Härdemand8b4b582010-10-29 16:08:23 -0300950 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300951
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300952 return rc_open(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300953}
954
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300955void rc_close(struct rc_dev *rdev)
956{
957 if (rdev) {
958 mutex_lock(&rdev->lock);
959
Sean Youngcb843432017-09-23 17:44:03 -0400960 if (!--rdev->users && rdev->close && rdev->registered)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300961 rdev->close(rdev);
962
963 mutex_unlock(&rdev->lock);
964 }
965}
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300966
David Härdemand8b4b582010-10-29 16:08:23 -0300967static void ir_close(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300968{
David Härdemand8b4b582010-10-29 16:08:23 -0300969 struct rc_dev *rdev = input_get_drvdata(idev);
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300970 rc_close(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300971}
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300972
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300973/* class for /sys/class/rc */
David Härdeman40fc5322013-03-06 16:52:10 -0300974static char *rc_devnode(struct device *dev, umode_t *mode)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300975{
976 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
977}
978
David Härdeman40fc5322013-03-06 16:52:10 -0300979static struct class rc_class = {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300980 .name = "rc",
David Härdeman40fc5322013-03-06 16:52:10 -0300981 .devnode = rc_devnode,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300982};
983
David Härdemanc003ab12012-10-11 19:11:54 -0300984/*
985 * These are the protocol textual descriptions that are
986 * used by the sysfs protocols file. Note that the order
987 * of the entries is relevant.
988 */
Heiner Kallweit53df8772015-11-16 17:52:17 -0200989static const struct {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300990 u64 type;
Heiner Kallweit53df8772015-11-16 17:52:17 -0200991 const char *name;
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200992 const char *module_name;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300993} proto_names[] = {
Sean Young6d741bf2017-08-07 16:20:58 -0400994 { RC_PROTO_BIT_NONE, "none", NULL },
995 { RC_PROTO_BIT_OTHER, "other", NULL },
996 { RC_PROTO_BIT_UNKNOWN, "unknown", NULL },
997 { RC_PROTO_BIT_RC5 |
998 RC_PROTO_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
999 { RC_PROTO_BIT_NEC |
1000 RC_PROTO_BIT_NECX |
1001 RC_PROTO_BIT_NEC32, "nec", "ir-nec-decoder" },
1002 { RC_PROTO_BIT_RC6_0 |
1003 RC_PROTO_BIT_RC6_6A_20 |
1004 RC_PROTO_BIT_RC6_6A_24 |
1005 RC_PROTO_BIT_RC6_6A_32 |
1006 RC_PROTO_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
1007 { RC_PROTO_BIT_JVC, "jvc", "ir-jvc-decoder" },
1008 { RC_PROTO_BIT_SONY12 |
1009 RC_PROTO_BIT_SONY15 |
1010 RC_PROTO_BIT_SONY20, "sony", "ir-sony-decoder" },
1011 { RC_PROTO_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
1012 { RC_PROTO_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
1013 { RC_PROTO_BIT_SHARP, "sharp", "ir-sharp-decoder" },
1014 { RC_PROTO_BIT_MCIR2_KBD |
1015 RC_PROTO_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" },
1016 { RC_PROTO_BIT_XMP, "xmp", "ir-xmp-decoder" },
1017 { RC_PROTO_BIT_CEC, "cec", NULL },
Sean Young447dcc0c2017-12-03 11:06:54 -05001018 { RC_PROTO_BIT_IMON, "imon", "ir-imon-decoder" },
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001019};
1020
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001021/**
James Hoganab88c662014-02-28 20:17:05 -03001022 * struct rc_filter_attribute - Device attribute relating to a filter type.
1023 * @attr: Device attribute.
1024 * @type: Filter type.
1025 * @mask: false for filter value, true for filter mask.
1026 */
1027struct rc_filter_attribute {
1028 struct device_attribute attr;
1029 enum rc_filter_type type;
1030 bool mask;
1031};
1032#define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
1033
James Hoganab88c662014-02-28 20:17:05 -03001034#define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
1035 struct rc_filter_attribute dev_attr_##_name = { \
1036 .attr = __ATTR(_name, _mode, _show, _store), \
1037 .type = (_type), \
1038 .mask = (_mask), \
1039 }
1040
1041/**
Sean Young0751d332016-12-05 17:08:35 -02001042 * show_protocols() - shows the current IR protocol(s)
David Härdemand8b4b582010-10-29 16:08:23 -03001043 * @device: the device descriptor
David Härdemanda6e1622014-04-03 20:32:16 -03001044 * @mattr: the device attribute struct
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001045 * @buf: a pointer to the output buffer
1046 *
1047 * This routine is a callback routine for input read the IR protocol type(s).
Sean Young0751d332016-12-05 17:08:35 -02001048 * it is trigged by reading /sys/class/rc/rc?/protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001049 * It returns the protocol names of supported protocols.
1050 * Enabled protocols are printed in brackets.
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001051 *
David Härdeman18726a32017-04-27 17:34:08 -03001052 * dev->lock is taken to guard against races between
1053 * store_protocols and show_protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001054 */
David Härdemand8b4b582010-10-29 16:08:23 -03001055static ssize_t show_protocols(struct device *device,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001056 struct device_attribute *mattr, char *buf)
1057{
David Härdemand8b4b582010-10-29 16:08:23 -03001058 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001059 u64 allowed, enabled;
1060 char *tmp = buf;
1061 int i;
1062
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001063 mutex_lock(&dev->lock);
1064
Sean Young0751d332016-12-05 17:08:35 -02001065 enabled = dev->enabled_protocols;
1066 allowed = dev->allowed_protocols;
1067 if (dev->raw && !allowed)
1068 allowed = ir_raw_get_allowed_protocols();
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001069
David Härdemanda6e1622014-04-03 20:32:16 -03001070 mutex_unlock(&dev->lock);
1071
Sean Young1f17f682018-02-12 07:27:50 -05001072 dev_dbg(&dev->dev, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
1073 __func__, (long long)allowed, (long long)enabled);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001074
1075 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1076 if (allowed & enabled & proto_names[i].type)
1077 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
1078 else if (allowed & proto_names[i].type)
1079 tmp += sprintf(tmp, "%s ", proto_names[i].name);
David Härdemanc003ab12012-10-11 19:11:54 -03001080
1081 if (allowed & proto_names[i].type)
1082 allowed &= ~proto_names[i].type;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001083 }
1084
Sean Younga60d64b2017-09-23 10:41:13 -04001085#ifdef CONFIG_LIRC
1086 if (dev->driver_type == RC_DRIVER_IR_RAW)
David Härdeman275ddb42015-05-19 19:03:22 -03001087 tmp += sprintf(tmp, "[lirc] ");
Sean Younga60d64b2017-09-23 10:41:13 -04001088#endif
David Härdeman275ddb42015-05-19 19:03:22 -03001089
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001090 if (tmp != buf)
1091 tmp--;
1092 *tmp = '\n';
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001093
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001094 return tmp + 1 - buf;
1095}
1096
1097/**
David Härdemanda6e1622014-04-03 20:32:16 -03001098 * parse_protocol_change() - parses a protocol change request
Sean Young1f17f682018-02-12 07:27:50 -05001099 * @dev: rc_dev device
David Härdemanda6e1622014-04-03 20:32:16 -03001100 * @protocols: pointer to the bitmask of current protocols
1101 * @buf: pointer to the buffer with a list of changes
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001102 *
David Härdemanda6e1622014-04-03 20:32:16 -03001103 * Writing "+proto" will add a protocol to the protocol mask.
1104 * Writing "-proto" will remove a protocol from protocol mask.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001105 * Writing "proto" will enable only "proto".
1106 * Writing "none" will disable all protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001107 * Returns the number of changes performed or a negative error code.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001108 */
Sean Young1f17f682018-02-12 07:27:50 -05001109static int parse_protocol_change(struct rc_dev *dev, u64 *protocols,
1110 const char *buf)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001111{
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001112 const char *tmp;
David Härdemanda6e1622014-04-03 20:32:16 -03001113 unsigned count = 0;
1114 bool enable, disable;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001115 u64 mask;
David Härdemanda6e1622014-04-03 20:32:16 -03001116 int i;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001117
David Härdemanda6e1622014-04-03 20:32:16 -03001118 while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001119 if (!*tmp)
1120 break;
1121
1122 if (*tmp == '+') {
1123 enable = true;
1124 disable = false;
1125 tmp++;
1126 } else if (*tmp == '-') {
1127 enable = false;
1128 disable = true;
1129 tmp++;
1130 } else {
1131 enable = false;
1132 disable = false;
1133 }
1134
David Härdemanc003ab12012-10-11 19:11:54 -03001135 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1136 if (!strcasecmp(tmp, proto_names[i].name)) {
1137 mask = proto_names[i].type;
1138 break;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001139 }
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001140 }
1141
David Härdemanc003ab12012-10-11 19:11:54 -03001142 if (i == ARRAY_SIZE(proto_names)) {
David Härdeman275ddb42015-05-19 19:03:22 -03001143 if (!strcasecmp(tmp, "lirc"))
1144 mask = 0;
1145 else {
Sean Young1f17f682018-02-12 07:27:50 -05001146 dev_dbg(&dev->dev, "Unknown protocol: '%s'\n",
1147 tmp);
David Härdeman275ddb42015-05-19 19:03:22 -03001148 return -EINVAL;
1149 }
David Härdemanc003ab12012-10-11 19:11:54 -03001150 }
1151
1152 count++;
1153
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001154 if (enable)
David Härdemanda6e1622014-04-03 20:32:16 -03001155 *protocols |= mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001156 else if (disable)
David Härdemanda6e1622014-04-03 20:32:16 -03001157 *protocols &= ~mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001158 else
David Härdemanda6e1622014-04-03 20:32:16 -03001159 *protocols = mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001160 }
1161
1162 if (!count) {
Sean Young1f17f682018-02-12 07:27:50 -05001163 dev_dbg(&dev->dev, "Protocol not specified\n");
David Härdemanda6e1622014-04-03 20:32:16 -03001164 return -EINVAL;
1165 }
1166
1167 return count;
1168}
1169
Sean Young0d39ab02017-02-25 06:51:31 -05001170void ir_raw_load_modules(u64 *protocols)
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001171{
1172 u64 available;
1173 int i, ret;
1174
1175 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
Sean Young6d741bf2017-08-07 16:20:58 -04001176 if (proto_names[i].type == RC_PROTO_BIT_NONE ||
1177 proto_names[i].type & (RC_PROTO_BIT_OTHER |
1178 RC_PROTO_BIT_UNKNOWN))
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001179 continue;
1180
1181 available = ir_raw_get_allowed_protocols();
1182 if (!(*protocols & proto_names[i].type & ~available))
1183 continue;
1184
1185 if (!proto_names[i].module_name) {
1186 pr_err("Can't enable IR protocol %s\n",
1187 proto_names[i].name);
1188 *protocols &= ~proto_names[i].type;
1189 continue;
1190 }
1191
1192 ret = request_module("%s", proto_names[i].module_name);
1193 if (ret < 0) {
1194 pr_err("Couldn't load IR protocol module %s\n",
1195 proto_names[i].module_name);
1196 *protocols &= ~proto_names[i].type;
1197 continue;
1198 }
1199 msleep(20);
1200 available = ir_raw_get_allowed_protocols();
1201 if (!(*protocols & proto_names[i].type & ~available))
1202 continue;
1203
Sean Young8caebcd2017-01-19 19:33:49 -02001204 pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001205 proto_names[i].module_name,
1206 proto_names[i].name);
1207 *protocols &= ~proto_names[i].type;
1208 }
1209}
1210
David Härdemanda6e1622014-04-03 20:32:16 -03001211/**
1212 * store_protocols() - changes the current/wakeup IR protocol(s)
1213 * @device: the device descriptor
1214 * @mattr: the device attribute struct
1215 * @buf: a pointer to the input buffer
1216 * @len: length of the input buffer
1217 *
1218 * This routine is for changing the IR protocol type.
1219 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
1220 * See parse_protocol_change() for the valid commands.
1221 * Returns @len on success or a negative error code.
1222 *
David Härdeman18726a32017-04-27 17:34:08 -03001223 * dev->lock is taken to guard against races between
1224 * store_protocols and show_protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001225 */
1226static ssize_t store_protocols(struct device *device,
1227 struct device_attribute *mattr,
1228 const char *buf, size_t len)
1229{
1230 struct rc_dev *dev = to_rc_dev(device);
David Härdemanda6e1622014-04-03 20:32:16 -03001231 u64 *current_protocols;
David Härdemanda6e1622014-04-03 20:32:16 -03001232 struct rc_scancode_filter *filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001233 u64 old_protocols, new_protocols;
1234 ssize_t rc;
1235
Sean Young1f17f682018-02-12 07:27:50 -05001236 dev_dbg(&dev->dev, "Normal protocol change requested\n");
Sean Young0751d332016-12-05 17:08:35 -02001237 current_protocols = &dev->enabled_protocols;
1238 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001239
Sean Young0751d332016-12-05 17:08:35 -02001240 if (!dev->change_protocol) {
Sean Young1f17f682018-02-12 07:27:50 -05001241 dev_dbg(&dev->dev, "Protocol switching not supported\n");
David Härdemanda6e1622014-04-03 20:32:16 -03001242 return -EINVAL;
1243 }
1244
1245 mutex_lock(&dev->lock);
1246
1247 old_protocols = *current_protocols;
1248 new_protocols = old_protocols;
Sean Young1f17f682018-02-12 07:27:50 -05001249 rc = parse_protocol_change(dev, &new_protocols, buf);
David Härdemanda6e1622014-04-03 20:32:16 -03001250 if (rc < 0)
1251 goto out;
1252
Sean Younga86d6df2018-03-23 16:47:37 -04001253 if (dev->driver_type == RC_DRIVER_IR_RAW)
1254 ir_raw_load_modules(&new_protocols);
1255
Sean Young0751d332016-12-05 17:08:35 -02001256 rc = dev->change_protocol(dev, &new_protocols);
David Härdemanda6e1622014-04-03 20:32:16 -03001257 if (rc < 0) {
Sean Young1f17f682018-02-12 07:27:50 -05001258 dev_dbg(&dev->dev, "Error setting protocols to 0x%llx\n",
1259 (long long)new_protocols);
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001260 goto out;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001261 }
1262
James Hogan983c5bd2014-12-08 13:17:07 -03001263 if (new_protocols != old_protocols) {
1264 *current_protocols = new_protocols;
Sean Young1f17f682018-02-12 07:27:50 -05001265 dev_dbg(&dev->dev, "Protocols changed to 0x%llx\n",
1266 (long long)new_protocols);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001267 }
1268
James Hogan6bea25a2014-02-28 20:17:06 -03001269 /*
James Hogan983c5bd2014-12-08 13:17:07 -03001270 * If a protocol change was attempted the filter may need updating, even
1271 * if the actual protocol mask hasn't changed (since the driver may have
1272 * cleared the filter).
James Hogan6bea25a2014-02-28 20:17:06 -03001273 * Try setting the same filter with the new protocol (if any).
1274 * Fall back to clearing the filter.
1275 */
Sean Young0751d332016-12-05 17:08:35 -02001276 if (dev->s_filter && filter->mask) {
David Härdemanda6e1622014-04-03 20:32:16 -03001277 if (new_protocols)
Sean Young0751d332016-12-05 17:08:35 -02001278 rc = dev->s_filter(dev, filter);
David Härdemanda6e1622014-04-03 20:32:16 -03001279 else
1280 rc = -1;
David Härdeman23c843b2014-04-04 19:06:01 -03001281
David Härdemanda6e1622014-04-03 20:32:16 -03001282 if (rc < 0) {
1283 filter->data = 0;
1284 filter->mask = 0;
Sean Young0751d332016-12-05 17:08:35 -02001285 dev->s_filter(dev, filter);
James Hogan6bea25a2014-02-28 20:17:06 -03001286 }
James Hogan6bea25a2014-02-28 20:17:06 -03001287 }
1288
David Härdemanda6e1622014-04-03 20:32:16 -03001289 rc = len;
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001290
1291out:
1292 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001293 return rc;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001294}
1295
James Hogan00942d12014-01-17 10:58:49 -03001296/**
James Hogan00942d12014-01-17 10:58:49 -03001297 * show_filter() - shows the current scancode filter value or mask
1298 * @device: the device descriptor
1299 * @attr: the device attribute struct
1300 * @buf: a pointer to the output buffer
1301 *
1302 * This routine is a callback routine to read a scancode filter value or mask.
1303 * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
1304 * It prints the current scancode filter value or mask of the appropriate filter
1305 * type in hexadecimal into @buf and returns the size of the buffer.
1306 *
1307 * Bits of the filter value corresponding to set bits in the filter mask are
1308 * compared against input scancodes and non-matching scancodes are discarded.
1309 *
David Härdeman18726a32017-04-27 17:34:08 -03001310 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001311 * store_filter and show_filter.
1312 */
1313static ssize_t show_filter(struct device *device,
1314 struct device_attribute *attr,
1315 char *buf)
1316{
1317 struct rc_dev *dev = to_rc_dev(device);
1318 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001319 struct rc_scancode_filter *filter;
James Hogan00942d12014-01-17 10:58:49 -03001320 u32 val;
1321
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001322 mutex_lock(&dev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001323
David Härdemanda6e1622014-04-03 20:32:16 -03001324 if (fattr->type == RC_FILTER_NORMAL)
David Härdemanc5540fb2014-04-03 20:32:21 -03001325 filter = &dev->scancode_filter;
James Hogan00942d12014-01-17 10:58:49 -03001326 else
David Härdemanc5540fb2014-04-03 20:32:21 -03001327 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001328
David Härdemanda6e1622014-04-03 20:32:16 -03001329 if (fattr->mask)
1330 val = filter->mask;
1331 else
1332 val = filter->data;
James Hogan00942d12014-01-17 10:58:49 -03001333 mutex_unlock(&dev->lock);
1334
1335 return sprintf(buf, "%#x\n", val);
1336}
1337
1338/**
1339 * store_filter() - changes the scancode filter value
1340 * @device: the device descriptor
1341 * @attr: the device attribute struct
1342 * @buf: a pointer to the input buffer
1343 * @len: length of the input buffer
1344 *
1345 * This routine is for changing a scancode filter value or mask.
1346 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
1347 * Returns -EINVAL if an invalid filter value for the current protocol was
1348 * specified or if scancode filtering is not supported by the driver, otherwise
1349 * returns @len.
1350 *
1351 * Bits of the filter value corresponding to set bits in the filter mask are
1352 * compared against input scancodes and non-matching scancodes are discarded.
1353 *
David Härdeman18726a32017-04-27 17:34:08 -03001354 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001355 * store_filter and show_filter.
1356 */
1357static ssize_t store_filter(struct device *device,
1358 struct device_attribute *attr,
David Härdemanda6e1622014-04-03 20:32:16 -03001359 const char *buf, size_t len)
James Hogan00942d12014-01-17 10:58:49 -03001360{
1361 struct rc_dev *dev = to_rc_dev(device);
1362 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001363 struct rc_scancode_filter new_filter, *filter;
James Hogan00942d12014-01-17 10:58:49 -03001364 int ret;
1365 unsigned long val;
David Härdeman23c843b2014-04-04 19:06:01 -03001366 int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
James Hogan00942d12014-01-17 10:58:49 -03001367
James Hogan00942d12014-01-17 10:58:49 -03001368 ret = kstrtoul(buf, 0, &val);
1369 if (ret < 0)
1370 return ret;
1371
David Härdemanda6e1622014-04-03 20:32:16 -03001372 if (fattr->type == RC_FILTER_NORMAL) {
1373 set_filter = dev->s_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001374 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001375 } else {
1376 set_filter = dev->s_wakeup_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001377 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001378 }
1379
David Härdeman99b0f3c2014-04-04 19:06:06 -03001380 if (!set_filter)
1381 return -EINVAL;
James Hogan00942d12014-01-17 10:58:49 -03001382
1383 mutex_lock(&dev->lock);
1384
David Härdemanda6e1622014-04-03 20:32:16 -03001385 new_filter = *filter;
James Hogan00942d12014-01-17 10:58:49 -03001386 if (fattr->mask)
David Härdemanda6e1622014-04-03 20:32:16 -03001387 new_filter.mask = val;
James Hogan00942d12014-01-17 10:58:49 -03001388 else
David Härdemanda6e1622014-04-03 20:32:16 -03001389 new_filter.data = val;
David Härdeman23c843b2014-04-04 19:06:01 -03001390
Sean Young0751d332016-12-05 17:08:35 -02001391 if (fattr->type == RC_FILTER_WAKEUP) {
Sean Youngb590c0b2016-12-05 19:24:59 -02001392 /*
1393 * Refuse to set a filter unless a protocol is enabled
1394 * and the filter is valid for that protocol
1395 */
Sean Young6d741bf2017-08-07 16:20:58 -04001396 if (dev->wakeup_protocol != RC_PROTO_UNKNOWN)
James Hoganf423ccc2015-03-31 14:48:10 -03001397 ret = rc_validate_filter(dev, &new_filter);
Sean Youngb590c0b2016-12-05 19:24:59 -02001398 else
Sean Young0751d332016-12-05 17:08:35 -02001399 ret = -EINVAL;
Sean Youngb590c0b2016-12-05 19:24:59 -02001400
1401 if (ret != 0)
Sean Young0751d332016-12-05 17:08:35 -02001402 goto unlock;
Sean Young0751d332016-12-05 17:08:35 -02001403 }
1404
1405 if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
1406 val) {
James Hogan6bea25a2014-02-28 20:17:06 -03001407 /* refuse to set a filter unless a protocol is enabled */
1408 ret = -EINVAL;
1409 goto unlock;
1410 }
David Härdeman23c843b2014-04-04 19:06:01 -03001411
David Härdemanda6e1622014-04-03 20:32:16 -03001412 ret = set_filter(dev, &new_filter);
David Härdeman99b0f3c2014-04-04 19:06:06 -03001413 if (ret < 0)
1414 goto unlock;
James Hogan00942d12014-01-17 10:58:49 -03001415
David Härdemanda6e1622014-04-03 20:32:16 -03001416 *filter = new_filter;
James Hogan00942d12014-01-17 10:58:49 -03001417
1418unlock:
1419 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001420 return (ret < 0) ? ret : len;
James Hogan00942d12014-01-17 10:58:49 -03001421}
1422
Sean Young0751d332016-12-05 17:08:35 -02001423/**
1424 * show_wakeup_protocols() - shows the wakeup IR protocol
1425 * @device: the device descriptor
1426 * @mattr: the device attribute struct
1427 * @buf: a pointer to the output buffer
1428 *
1429 * This routine is a callback routine for input read the IR protocol type(s).
1430 * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols.
1431 * It returns the protocol names of supported protocols.
1432 * The enabled protocols are printed in brackets.
1433 *
David Härdeman18726a32017-04-27 17:34:08 -03001434 * dev->lock is taken to guard against races between
1435 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001436 */
1437static ssize_t show_wakeup_protocols(struct device *device,
1438 struct device_attribute *mattr,
1439 char *buf)
1440{
1441 struct rc_dev *dev = to_rc_dev(device);
1442 u64 allowed;
Sean Young6d741bf2017-08-07 16:20:58 -04001443 enum rc_proto enabled;
Sean Young0751d332016-12-05 17:08:35 -02001444 char *tmp = buf;
1445 int i;
1446
Sean Young0751d332016-12-05 17:08:35 -02001447 mutex_lock(&dev->lock);
1448
1449 allowed = dev->allowed_wakeup_protocols;
1450 enabled = dev->wakeup_protocol;
1451
1452 mutex_unlock(&dev->lock);
1453
Sean Young1f17f682018-02-12 07:27:50 -05001454 dev_dbg(&dev->dev, "%s: allowed - 0x%llx, enabled - %d\n",
1455 __func__, (long long)allowed, enabled);
Sean Young0751d332016-12-05 17:08:35 -02001456
Sean Youngd57ea872017-08-09 13:19:16 -04001457 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001458 if (allowed & (1ULL << i)) {
1459 if (i == enabled)
Sean Youngd57ea872017-08-09 13:19:16 -04001460 tmp += sprintf(tmp, "[%s] ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001461 else
Sean Youngd57ea872017-08-09 13:19:16 -04001462 tmp += sprintf(tmp, "%s ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001463 }
1464 }
1465
1466 if (tmp != buf)
1467 tmp--;
1468 *tmp = '\n';
1469
1470 return tmp + 1 - buf;
1471}
1472
1473/**
1474 * store_wakeup_protocols() - changes the wakeup IR protocol(s)
1475 * @device: the device descriptor
1476 * @mattr: the device attribute struct
1477 * @buf: a pointer to the input buffer
1478 * @len: length of the input buffer
1479 *
1480 * This routine is for changing the IR protocol type.
1481 * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols.
1482 * Returns @len on success or a negative error code.
1483 *
David Härdeman18726a32017-04-27 17:34:08 -03001484 * dev->lock is taken to guard against races between
1485 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001486 */
1487static ssize_t store_wakeup_protocols(struct device *device,
1488 struct device_attribute *mattr,
1489 const char *buf, size_t len)
1490{
1491 struct rc_dev *dev = to_rc_dev(device);
Sean Young6d741bf2017-08-07 16:20:58 -04001492 enum rc_proto protocol;
Sean Young0751d332016-12-05 17:08:35 -02001493 ssize_t rc;
1494 u64 allowed;
1495 int i;
1496
Sean Young0751d332016-12-05 17:08:35 -02001497 mutex_lock(&dev->lock);
1498
1499 allowed = dev->allowed_wakeup_protocols;
1500
1501 if (sysfs_streq(buf, "none")) {
Sean Young6d741bf2017-08-07 16:20:58 -04001502 protocol = RC_PROTO_UNKNOWN;
Sean Young0751d332016-12-05 17:08:35 -02001503 } else {
Sean Youngd57ea872017-08-09 13:19:16 -04001504 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001505 if ((allowed & (1ULL << i)) &&
Sean Youngd57ea872017-08-09 13:19:16 -04001506 sysfs_streq(buf, protocols[i].name)) {
Sean Young0751d332016-12-05 17:08:35 -02001507 protocol = i;
1508 break;
1509 }
1510 }
1511
Sean Youngd57ea872017-08-09 13:19:16 -04001512 if (i == ARRAY_SIZE(protocols)) {
Sean Young0751d332016-12-05 17:08:35 -02001513 rc = -EINVAL;
1514 goto out;
1515 }
James Hoganf423ccc2015-03-31 14:48:10 -03001516
1517 if (dev->encode_wakeup) {
1518 u64 mask = 1ULL << protocol;
1519
1520 ir_raw_load_modules(&mask);
1521 if (!mask) {
1522 rc = -EINVAL;
1523 goto out;
1524 }
1525 }
Sean Young0751d332016-12-05 17:08:35 -02001526 }
1527
1528 if (dev->wakeup_protocol != protocol) {
1529 dev->wakeup_protocol = protocol;
Sean Young1f17f682018-02-12 07:27:50 -05001530 dev_dbg(&dev->dev, "Wakeup protocol changed to %d\n", protocol);
Sean Young0751d332016-12-05 17:08:35 -02001531
Sean Young6d741bf2017-08-07 16:20:58 -04001532 if (protocol == RC_PROTO_RC6_MCE)
Sean Young0751d332016-12-05 17:08:35 -02001533 dev->scancode_wakeup_filter.data = 0x800f0000;
1534 else
1535 dev->scancode_wakeup_filter.data = 0;
1536 dev->scancode_wakeup_filter.mask = 0;
1537
1538 rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
1539 if (rc == 0)
1540 rc = len;
1541 } else {
1542 rc = len;
1543 }
1544
1545out:
1546 mutex_unlock(&dev->lock);
1547 return rc;
1548}
1549
David Härdemand8b4b582010-10-29 16:08:23 -03001550static void rc_dev_release(struct device *device)
1551{
Max Kellermann47cae1e2016-03-21 08:33:05 -03001552 struct rc_dev *dev = to_rc_dev(device);
1553
1554 kfree(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001555}
1556
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001557#define ADD_HOTPLUG_VAR(fmt, val...) \
1558 do { \
1559 int err = add_uevent_var(env, fmt, val); \
1560 if (err) \
1561 return err; \
1562 } while (0)
1563
1564static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1565{
David Härdemand8b4b582010-10-29 16:08:23 -03001566 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001567
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001568 if (dev->rc_map.name)
1569 ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
David Härdemand8b4b582010-10-29 16:08:23 -03001570 if (dev->driver_name)
1571 ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
Sean Youngb9f407e2017-09-01 11:34:23 -03001572 if (dev->device_name)
1573 ADD_HOTPLUG_VAR("DEV_NAME=%s", dev->device_name);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001574
1575 return 0;
1576}
1577
1578/*
1579 * Static device attribute struct with the sysfs attributes for IR's
1580 */
Sean Young6d75db32017-09-01 11:30:50 -03001581static struct device_attribute dev_attr_ro_protocols =
1582__ATTR(protocols, 0444, show_protocols, NULL);
1583static struct device_attribute dev_attr_rw_protocols =
1584__ATTR(protocols, 0644, show_protocols, store_protocols);
Sean Young0751d332016-12-05 17:08:35 -02001585static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
1586 store_wakeup_protocols);
James Hogan00942d12014-01-17 10:58:49 -03001587static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
1588 show_filter, store_filter, RC_FILTER_NORMAL, false);
1589static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
1590 show_filter, store_filter, RC_FILTER_NORMAL, true);
1591static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
1592 show_filter, store_filter, RC_FILTER_WAKEUP, false);
1593static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
1594 show_filter, store_filter, RC_FILTER_WAKEUP, true);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001595
Sean Young6d75db32017-09-01 11:30:50 -03001596static struct attribute *rc_dev_rw_protocol_attrs[] = {
1597 &dev_attr_rw_protocols.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001598 NULL,
1599};
1600
Sean Young6d75db32017-09-01 11:30:50 -03001601static const struct attribute_group rc_dev_rw_protocol_attr_grp = {
1602 .attrs = rc_dev_rw_protocol_attrs,
1603};
1604
1605static struct attribute *rc_dev_ro_protocol_attrs[] = {
1606 &dev_attr_ro_protocols.attr,
1607 NULL,
1608};
1609
1610static const struct attribute_group rc_dev_ro_protocol_attr_grp = {
1611 .attrs = rc_dev_ro_protocol_attrs,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001612};
1613
David Härdeman99b0f3c2014-04-04 19:06:06 -03001614static struct attribute *rc_dev_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001615 &dev_attr_filter.attr.attr,
1616 &dev_attr_filter_mask.attr.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001617 NULL,
1618};
1619
Arvind Yadavdb681022017-07-07 04:23:54 -04001620static const struct attribute_group rc_dev_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001621 .attrs = rc_dev_filter_attrs,
1622};
1623
1624static struct attribute *rc_dev_wakeup_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001625 &dev_attr_wakeup_filter.attr.attr,
1626 &dev_attr_wakeup_filter_mask.attr.attr,
Sean Young0751d332016-12-05 17:08:35 -02001627 &dev_attr_wakeup_protocols.attr,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001628 NULL,
1629};
1630
Arvind Yadavdb681022017-07-07 04:23:54 -04001631static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001632 .attrs = rc_dev_wakeup_filter_attrs,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001633};
1634
Bhumika Goyalf03f02f2017-08-19 05:22:15 -03001635static const struct device_type rc_dev_type = {
David Härdemand8b4b582010-10-29 16:08:23 -03001636 .release = rc_dev_release,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001637 .uevent = rc_dev_uevent,
1638};
1639
Andi Shyti0f7499f2016-12-16 06:50:58 -02001640struct rc_dev *rc_allocate_device(enum rc_driver_type type)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001641{
David Härdemand8b4b582010-10-29 16:08:23 -03001642 struct rc_dev *dev;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001643
David Härdemand8b4b582010-10-29 16:08:23 -03001644 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1645 if (!dev)
1646 return NULL;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001647
Andi Shytid34aee12016-12-16 04:12:15 -02001648 if (type != RC_DRIVER_IR_RAW_TX) {
1649 dev->input_dev = input_allocate_device();
1650 if (!dev->input_dev) {
1651 kfree(dev);
1652 return NULL;
1653 }
1654
1655 dev->input_dev->getkeycode = ir_getkeycode;
1656 dev->input_dev->setkeycode = ir_setkeycode;
1657 input_set_drvdata(dev->input_dev, dev);
1658
Sean Young28492252018-03-24 08:02:48 -04001659 dev->timeout = IR_DEFAULT_TIMEOUT;
Kees Cookb17ec782017-10-24 11:23:14 -04001660 timer_setup(&dev->timer_keyup, ir_timer_keyup, 0);
Sean Young57c642c2017-11-23 17:37:10 -05001661 timer_setup(&dev->timer_repeat, ir_timer_repeat, 0);
Andi Shytid34aee12016-12-16 04:12:15 -02001662
1663 spin_lock_init(&dev->rc_map.lock);
1664 spin_lock_init(&dev->keylock);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001665 }
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001666 mutex_init(&dev->lock);
David Härdemand8b4b582010-10-29 16:08:23 -03001667
1668 dev->dev.type = &rc_dev_type;
David Härdeman40fc5322013-03-06 16:52:10 -03001669 dev->dev.class = &rc_class;
David Härdemand8b4b582010-10-29 16:08:23 -03001670 device_initialize(&dev->dev);
1671
Andi Shyti0f7499f2016-12-16 06:50:58 -02001672 dev->driver_type = type;
1673
David Härdemand8b4b582010-10-29 16:08:23 -03001674 __module_get(THIS_MODULE);
1675 return dev;
1676}
1677EXPORT_SYMBOL_GPL(rc_allocate_device);
1678
1679void rc_free_device(struct rc_dev *dev)
1680{
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001681 if (!dev)
1682 return;
1683
Markus Elfring3dd94f02014-11-20 09:01:32 -03001684 input_free_device(dev->input_dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001685
1686 put_device(&dev->dev);
1687
Max Kellermann47cae1e2016-03-21 08:33:05 -03001688 /* kfree(dev) will be called by the callback function
1689 rc_dev_release() */
1690
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001691 module_put(THIS_MODULE);
David Härdemand8b4b582010-10-29 16:08:23 -03001692}
1693EXPORT_SYMBOL_GPL(rc_free_device);
1694
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001695static void devm_rc_alloc_release(struct device *dev, void *res)
1696{
1697 rc_free_device(*(struct rc_dev **)res);
1698}
1699
Andi Shyti0f7499f2016-12-16 06:50:58 -02001700struct rc_dev *devm_rc_allocate_device(struct device *dev,
1701 enum rc_driver_type type)
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001702{
1703 struct rc_dev **dr, *rc;
1704
1705 dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
1706 if (!dr)
1707 return NULL;
1708
Andi Shyti0f7499f2016-12-16 06:50:58 -02001709 rc = rc_allocate_device(type);
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001710 if (!rc) {
1711 devres_free(dr);
1712 return NULL;
1713 }
1714
1715 rc->dev.parent = dev;
1716 rc->managed_alloc = true;
1717 *dr = rc;
1718 devres_add(dev, dr);
1719
1720 return rc;
1721}
1722EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
1723
David Härdemanf56928a2017-05-03 07:04:00 -03001724static int rc_prepare_rx_device(struct rc_dev *dev)
David Härdemand8b4b582010-10-29 16:08:23 -03001725{
David Härdemanfcb13092015-05-19 19:03:17 -03001726 int rc;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001727 struct rc_map *rc_map;
Sean Young6d741bf2017-08-07 16:20:58 -04001728 u64 rc_proto;
David Härdemand8b4b582010-10-29 16:08:23 -03001729
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001730 if (!dev->map_name)
David Härdemand8b4b582010-10-29 16:08:23 -03001731 return -EINVAL;
1732
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001733 rc_map = rc_map_get(dev->map_name);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001734 if (!rc_map)
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001735 rc_map = rc_map_get(RC_MAP_EMPTY);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001736 if (!rc_map || !rc_map->scan || rc_map->size == 0)
David Härdemand8b4b582010-10-29 16:08:23 -03001737 return -EINVAL;
1738
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001739 rc = ir_setkeytable(dev, rc_map);
1740 if (rc)
1741 return rc;
1742
Sean Young6d741bf2017-08-07 16:20:58 -04001743 rc_proto = BIT_ULL(rc_map->rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001744
Sean Young831c4c82017-09-01 09:55:59 -03001745 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1746 dev->enabled_protocols = dev->allowed_protocols;
1747
Sean Younga86d6df2018-03-23 16:47:37 -04001748 if (dev->driver_type == RC_DRIVER_IR_RAW)
1749 ir_raw_load_modules(&rc_proto);
1750
Sean Young41380862017-02-22 18:48:01 -03001751 if (dev->change_protocol) {
Sean Young6d741bf2017-08-07 16:20:58 -04001752 rc = dev->change_protocol(dev, &rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001753 if (rc < 0)
1754 goto out_table;
Sean Young6d741bf2017-08-07 16:20:58 -04001755 dev->enabled_protocols = rc_proto;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001756 }
1757
David Härdemand8b4b582010-10-29 16:08:23 -03001758 set_bit(EV_KEY, dev->input_dev->evbit);
1759 set_bit(EV_REP, dev->input_dev->evbit);
1760 set_bit(EV_MSC, dev->input_dev->evbit);
1761 set_bit(MSC_SCAN, dev->input_dev->mscbit);
1762 if (dev->open)
1763 dev->input_dev->open = ir_open;
1764 if (dev->close)
1765 dev->input_dev->close = ir_close;
1766
David Härdemanb2aceb72017-04-27 17:33:58 -03001767 dev->input_dev->dev.parent = &dev->dev;
1768 memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
1769 dev->input_dev->phys = dev->input_phys;
Sean Young518f4b22017-07-01 12:13:19 -04001770 dev->input_dev->name = dev->device_name;
David Härdemanb2aceb72017-04-27 17:33:58 -03001771
David Härdemanf56928a2017-05-03 07:04:00 -03001772 return 0;
1773
1774out_table:
1775 ir_free_table(&dev->rc_map);
1776
1777 return rc;
1778}
1779
1780static int rc_setup_rx_device(struct rc_dev *dev)
1781{
1782 int rc;
1783
David Härdemanb2aceb72017-04-27 17:33:58 -03001784 /* rc_open will be called here */
1785 rc = input_register_device(dev->input_dev);
1786 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001787 return rc;
David Härdemanb2aceb72017-04-27 17:33:58 -03001788
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001789 /*
1790 * Default delay of 250ms is too short for some protocols, especially
1791 * since the timeout is currently set to 250ms. Increase it to 500ms,
1792 * to avoid wrong repetition of the keycodes. Note that this must be
1793 * set after the call to input_register_device().
1794 */
Sean Young57c642c2017-11-23 17:37:10 -05001795 if (dev->allowed_protocols == RC_PROTO_BIT_CEC)
1796 dev->input_dev->rep[REP_DELAY] = 0;
1797 else
1798 dev->input_dev->rep[REP_DELAY] = 500;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001799
1800 /*
1801 * As a repeat event on protocols like RC-5 and NEC take as long as
1802 * 110/114ms, using 33ms as a repeat period is not the right thing
1803 * to do.
1804 */
1805 dev->input_dev->rep[REP_PERIOD] = 125;
1806
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001807 return 0;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001808}
1809
1810static void rc_free_rx_device(struct rc_dev *dev)
1811{
David Härdemanf56928a2017-05-03 07:04:00 -03001812 if (!dev)
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001813 return;
1814
David Härdemanf56928a2017-05-03 07:04:00 -03001815 if (dev->input_dev) {
1816 input_unregister_device(dev->input_dev);
1817 dev->input_dev = NULL;
1818 }
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001819
David Härdemanf56928a2017-05-03 07:04:00 -03001820 ir_free_table(&dev->rc_map);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001821}
1822
1823int rc_register_device(struct rc_dev *dev)
1824{
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001825 const char *path;
1826 int attr = 0;
1827 int minor;
1828 int rc;
1829
1830 if (!dev)
1831 return -EINVAL;
1832
David Härdemanfcb13092015-05-19 19:03:17 -03001833 minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
1834 if (minor < 0)
1835 return minor;
1836
1837 dev->minor = minor;
1838 dev_set_name(&dev->dev, "rc%u", dev->minor);
1839 dev_set_drvdata(&dev->dev, dev);
Mauro Carvalho Chehab587d1b02014-01-14 16:27:55 -03001840
David Härdeman99b0f3c2014-04-04 19:06:06 -03001841 dev->dev.groups = dev->sysfs_groups;
Sean Young6d75db32017-09-01 11:30:50 -03001842 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1843 dev->sysfs_groups[attr++] = &rc_dev_ro_protocol_attr_grp;
1844 else if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
1845 dev->sysfs_groups[attr++] = &rc_dev_rw_protocol_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001846 if (dev->s_filter)
David Härdeman120703f2014-04-03 20:31:30 -03001847 dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001848 if (dev->s_wakeup_filter)
1849 dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001850 dev->sysfs_groups[attr++] = NULL;
1851
Sean Younga60d64b2017-09-23 10:41:13 -04001852 if (dev->driver_type == RC_DRIVER_IR_RAW) {
David Härdemanf56928a2017-05-03 07:04:00 -03001853 rc = ir_raw_event_prepare(dev);
1854 if (rc < 0)
1855 goto out_minor;
1856 }
1857
1858 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1859 rc = rc_prepare_rx_device(dev);
1860 if (rc)
1861 goto out_raw;
1862 }
1863
David Härdemand8b4b582010-10-29 16:08:23 -03001864 rc = device_add(&dev->dev);
1865 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001866 goto out_rx_free;
David Härdemand8b4b582010-10-29 16:08:23 -03001867
David Härdemand8b4b582010-10-29 16:08:23 -03001868 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Heiner Kallweit4dc0e902015-10-29 19:39:06 -02001869 dev_info(&dev->dev, "%s as %s\n",
Sean Young518f4b22017-07-01 12:13:19 -04001870 dev->device_name ?: "Unspecified device", path ?: "N/A");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001871 kfree(path);
1872
Sean Youngd7832cd2018-05-24 05:47:17 -04001873 dev->registered = true;
1874
Sean Young5df62772017-02-23 06:11:21 -03001875 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1876 rc = rc_setup_rx_device(dev);
1877 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001878 goto out_dev;
1879 }
1880
Sean Younga60d64b2017-09-23 10:41:13 -04001881 /* Ensure that the lirc kfifo is setup before we start the thread */
Sean Young62d6f1992017-09-24 12:43:24 -04001882 if (dev->allowed_protocols != RC_PROTO_BIT_CEC) {
Sean Younga60d64b2017-09-23 10:41:13 -04001883 rc = ir_lirc_register(dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001884 if (rc < 0)
1885 goto out_rx;
David Härdemand8b4b582010-10-29 16:08:23 -03001886 }
1887
Sean Younga60d64b2017-09-23 10:41:13 -04001888 if (dev->driver_type == RC_DRIVER_IR_RAW) {
1889 rc = ir_raw_event_register(dev);
1890 if (rc < 0)
1891 goto out_lirc;
1892 }
1893
Sean Young1f17f682018-02-12 07:27:50 -05001894 dev_dbg(&dev->dev, "Registered rc%u (driver: %s)\n", dev->minor,
1895 dev->driver_name ? dev->driver_name : "unknown");
David Härdemand8b4b582010-10-29 16:08:23 -03001896
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001897 return 0;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001898
Sean Younga60d64b2017-09-23 10:41:13 -04001899out_lirc:
Sean Young62d6f1992017-09-24 12:43:24 -04001900 if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
Sean Younga60d64b2017-09-23 10:41:13 -04001901 ir_lirc_unregister(dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001902out_rx:
1903 rc_free_rx_device(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001904out_dev:
1905 device_del(&dev->dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001906out_rx_free:
1907 ir_free_table(&dev->rc_map);
1908out_raw:
1909 ir_raw_event_free(dev);
1910out_minor:
David Härdemanfcb13092015-05-19 19:03:17 -03001911 ida_simple_remove(&rc_ida, minor);
David Härdemand8b4b582010-10-29 16:08:23 -03001912 return rc;
1913}
1914EXPORT_SYMBOL_GPL(rc_register_device);
1915
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001916static void devm_rc_release(struct device *dev, void *res)
1917{
1918 rc_unregister_device(*(struct rc_dev **)res);
1919}
1920
1921int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
1922{
1923 struct rc_dev **dr;
1924 int ret;
1925
1926 dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
1927 if (!dr)
1928 return -ENOMEM;
1929
1930 ret = rc_register_device(dev);
1931 if (ret) {
1932 devres_free(dr);
1933 return ret;
1934 }
1935
1936 *dr = dev;
1937 devres_add(parent, dr);
1938
1939 return 0;
1940}
1941EXPORT_SYMBOL_GPL(devm_rc_register_device);
1942
David Härdemand8b4b582010-10-29 16:08:23 -03001943void rc_unregister_device(struct rc_dev *dev)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001944{
David Härdemand8b4b582010-10-29 16:08:23 -03001945 if (!dev)
1946 return;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001947
David Härdemand8b4b582010-10-29 16:08:23 -03001948 if (dev->driver_type == RC_DRIVER_IR_RAW)
1949 ir_raw_event_unregister(dev);
1950
Sean Young8d406882018-03-06 08:57:57 -05001951 del_timer_sync(&dev->timer_keyup);
1952 del_timer_sync(&dev->timer_repeat);
1953
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001954 rc_free_rx_device(dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001955
Sean Young7790e812017-09-26 07:31:29 -04001956 mutex_lock(&dev->lock);
1957 dev->registered = false;
1958 mutex_unlock(&dev->lock);
1959
1960 /*
1961 * lirc device should be freed with dev->registered = false, so
1962 * that userspace polling will get notified.
1963 */
Sean Young62d6f1992017-09-24 12:43:24 -04001964 if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
Sean Younga60d64b2017-09-23 10:41:13 -04001965 ir_lirc_unregister(dev);
1966
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001967 device_del(&dev->dev);
1968
David Härdemanfcb13092015-05-19 19:03:17 -03001969 ida_simple_remove(&rc_ida, dev->minor);
1970
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001971 if (!dev->managed_alloc)
1972 rc_free_device(dev);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001973}
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001974
David Härdemand8b4b582010-10-29 16:08:23 -03001975EXPORT_SYMBOL_GPL(rc_unregister_device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001976
1977/*
1978 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1979 */
1980
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001981static int __init rc_core_init(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001982{
David Härdeman40fc5322013-03-06 16:52:10 -03001983 int rc = class_register(&rc_class);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001984 if (rc) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -02001985 pr_err("rc_core: unable to register rc class\n");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001986 return rc;
1987 }
1988
Sean Younga60d64b2017-09-23 10:41:13 -04001989 rc = lirc_dev_init();
1990 if (rc) {
1991 pr_err("rc_core: unable to init lirc\n");
1992 class_unregister(&rc_class);
1993 return 0;
1994 }
1995
Sean Young153a60b2013-07-30 19:00:01 -03001996 led_trigger_register_simple("rc-feedback", &led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001997 rc_map_register(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001998
1999 return 0;
2000}
2001
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03002002static void __exit rc_core_exit(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002003{
Sean Younga60d64b2017-09-23 10:41:13 -04002004 lirc_dev_exit();
David Härdeman40fc5322013-03-06 16:52:10 -03002005 class_unregister(&rc_class);
Sean Young153a60b2013-07-30 19:00:01 -03002006 led_trigger_unregister_simple(led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03002007 rc_map_unregister(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002008}
2009
David Härdemane76d4ce2013-03-06 16:52:15 -03002010subsys_initcall(rc_core_init);
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03002011module_exit(rc_core_exit);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002012
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02002013MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehab20835282017-12-01 08:47:08 -05002014MODULE_LICENSE("GPL v2");