blob: 877978dbd409de878292b278e7baca80a06c1587 [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
278 /* Did the user wish to remove the mapping? */
279 if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
Sean Young1f17f682018-02-12 07:27:50 -0500280 dev_dbg(&dev->dev, "#%d: Deleting scan 0x%04x\n",
281 index, rc_map->scan[index].scancode);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300282 rc_map->len--;
283 memmove(&rc_map->scan[index], &rc_map->scan[index+ 1],
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300284 (rc_map->len - index) * sizeof(struct rc_map_table));
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700285 } else {
Sean Young1f17f682018-02-12 07:27:50 -0500286 dev_dbg(&dev->dev, "#%d: %s scan 0x%04x with key 0x%04x\n",
287 index,
288 old_keycode == KEY_RESERVED ? "New" : "Replacing",
289 rc_map->scan[index].scancode, new_keycode);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300290 rc_map->scan[index].keycode = new_keycode;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700291 }
292
293 if (old_keycode != KEY_RESERVED) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700294 /* Possibly shrink the keytable, failure is not a problem */
Sean Young1f17f682018-02-12 07:27:50 -0500295 ir_resize_table(dev, rc_map, GFP_ATOMIC);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700296 }
297
298 return old_keycode;
299}
300
301/**
David Härdeman4c7b3552010-11-10 11:04:19 -0300302 * ir_establish_scancode() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300303 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300304 * @rc_map: scancode table to be searched
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700305 * @scancode: the desired scancode
306 * @resize: controls whether we allowed to resize the table to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300307 * accommodate not yet present scancodes
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700308 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300309 * This routine is used to locate given scancode in rc_map.
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700310 * If scancode is not yet present the routine will allocate a new slot
311 * for it.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500312 *
313 * return: index of the mapping containing scancode in question
314 * or -1U in case of failure.
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700315 */
David Härdemand8b4b582010-10-29 16:08:23 -0300316static unsigned int ir_establish_scancode(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300317 struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700318 unsigned int scancode,
319 bool resize)
David Härdemanb3074c02010-04-02 15:58:28 -0300320{
321 unsigned int i;
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300322
323 /*
324 * Unfortunately, some hardware-based IR decoders don't provide
325 * all bits for the complete IR code. In general, they provide only
326 * the command part of the IR code. Yet, as it is possible to replace
327 * the provided IR with another one, it is needed to allow loading
David Härdemand8b4b582010-10-29 16:08:23 -0300328 * IR tables from other remotes. So, we support specifying a mask to
329 * indicate the valid bits of the scancodes.
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300330 */
David Härdeman9d2f1d32014-04-03 20:32:26 -0300331 if (dev->scancode_mask)
332 scancode &= dev->scancode_mask;
David Härdemanb3074c02010-04-02 15:58:28 -0300333
334 /* First check if we already have a mapping for this ir command */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300335 for (i = 0; i < rc_map->len; i++) {
336 if (rc_map->scan[i].scancode == scancode)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700337 return i;
338
David Härdemanb3074c02010-04-02 15:58:28 -0300339 /* Keytable is sorted from lowest to highest scancode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300340 if (rc_map->scan[i].scancode >= scancode)
David Härdemanb3074c02010-04-02 15:58:28 -0300341 break;
David Härdemanb3074c02010-04-02 15:58:28 -0300342 }
343
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700344 /* No previous mapping found, we might need to grow the table */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300345 if (rc_map->size == rc_map->len) {
Sean Young1f17f682018-02-12 07:27:50 -0500346 if (!resize || ir_resize_table(dev, rc_map, GFP_ATOMIC))
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700347 return -1U;
348 }
David Härdemanb3074c02010-04-02 15:58:28 -0300349
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700350 /* i is the proper index to insert our new keycode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300351 if (i < rc_map->len)
352 memmove(&rc_map->scan[i + 1], &rc_map->scan[i],
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300353 (rc_map->len - i) * sizeof(struct rc_map_table));
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300354 rc_map->scan[i].scancode = scancode;
355 rc_map->scan[i].keycode = KEY_RESERVED;
356 rc_map->len++;
David Härdemanb3074c02010-04-02 15:58:28 -0300357
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700358 return i;
David Härdemanb3074c02010-04-02 15:58:28 -0300359}
360
361/**
362 * ir_setkeycode() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300363 * @idev: the struct input_dev device descriptor
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500364 * @ke: Input keymap entry
365 * @old_keycode: result
David Härdemanb3074c02010-04-02 15:58:28 -0300366 *
367 * This routine is used to handle evdev EVIOCSKEY ioctl.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500368 *
369 * return: -EINVAL if the keycode could not be inserted, otherwise zero.
David Härdemanb3074c02010-04-02 15:58:28 -0300370 */
David Härdemand8b4b582010-10-29 16:08:23 -0300371static int ir_setkeycode(struct input_dev *idev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700372 const struct input_keymap_entry *ke,
373 unsigned int *old_keycode)
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300374{
David Härdemand8b4b582010-10-29 16:08:23 -0300375 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300376 struct rc_map *rc_map = &rdev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700377 unsigned int index;
378 unsigned int scancode;
Mauro Carvalho Chehabdea8a392010-11-29 07:46:13 -0300379 int retval = 0;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700380 unsigned long flags;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300381
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300382 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700383
384 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
385 index = ke->index;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300386 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700387 retval = -EINVAL;
388 goto out;
389 }
390 } else {
391 retval = input_scancode_to_scalar(ke, &scancode);
392 if (retval)
393 goto out;
394
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300395 index = ir_establish_scancode(rdev, rc_map, scancode, true);
396 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700397 retval = -ENOMEM;
398 goto out;
399 }
400 }
401
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300402 *old_keycode = ir_update_mapping(rdev, rc_map, index, ke->keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700403
404out:
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300405 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700406 return retval;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300407}
408
409/**
David Härdemanb3074c02010-04-02 15:58:28 -0300410 * ir_setkeytable() - sets several entries in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300411 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300412 * @from: the struct rc_map to copy entries from
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300413 *
David Härdemanb3074c02010-04-02 15:58:28 -0300414 * This routine is used to handle table initialization.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500415 *
416 * return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300417 */
David Härdemand8b4b582010-10-29 16:08:23 -0300418static int ir_setkeytable(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300419 const struct rc_map *from)
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300420{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300421 struct rc_map *rc_map = &dev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700422 unsigned int i, index;
423 int rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300424
Sean Young1f17f682018-02-12 07:27:50 -0500425 rc = ir_create_table(dev, rc_map, from->name, from->rc_proto,
426 from->size);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700427 if (rc)
428 return rc;
429
David Härdemanb3074c02010-04-02 15:58:28 -0300430 for (i = 0; i < from->size; i++) {
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300431 index = ir_establish_scancode(dev, rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700432 from->scan[i].scancode, false);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300433 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700434 rc = -ENOMEM;
David Härdemanb3074c02010-04-02 15:58:28 -0300435 break;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700436 }
437
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300438 ir_update_mapping(dev, rc_map, index,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700439 from->scan[i].keycode);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300440 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700441
442 if (rc)
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300443 ir_free_table(rc_map);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700444
David Härdemanb3074c02010-04-02 15:58:28 -0300445 return rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300446}
447
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300448static int rc_map_cmp(const void *key, const void *elt)
449{
450 const unsigned int *scancode = key;
451 const struct rc_map_table *e = elt;
452
453 if (*scancode < e->scancode)
454 return -1;
455 else if (*scancode > e->scancode)
456 return 1;
457 return 0;
458}
459
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300460/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700461 * ir_lookup_by_scancode() - locate mapping by scancode
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300462 * @rc_map: the struct rc_map to search
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700463 * @scancode: scancode to look for in the table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700464 *
465 * This routine performs binary search in RC keykeymap table for
466 * given scancode.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500467 *
468 * return: index in the table, -1U if not found
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700469 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300470static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700471 unsigned int scancode)
472{
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300473 struct rc_map_table *res;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700474
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300475 res = bsearch(&scancode, rc_map->scan, rc_map->len,
476 sizeof(struct rc_map_table), rc_map_cmp);
477 if (!res)
478 return -1U;
479 else
480 return res - rc_map->scan;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700481}
482
483/**
David Härdemanb3074c02010-04-02 15:58:28 -0300484 * ir_getkeycode() - get a keycode from the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300485 * @idev: the struct input_dev device descriptor
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500486 * @ke: Input keymap entry
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300487 *
488 * This routine is used to handle evdev EVIOCGKEY ioctl.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500489 *
490 * return: always returns zero.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300491 */
David Härdemand8b4b582010-10-29 16:08:23 -0300492static int ir_getkeycode(struct input_dev *idev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700493 struct input_keymap_entry *ke)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300494{
David Härdemand8b4b582010-10-29 16:08:23 -0300495 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300496 struct rc_map *rc_map = &rdev->rc_map;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300497 struct rc_map_table *entry;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700498 unsigned long flags;
499 unsigned int index;
500 unsigned int scancode;
501 int retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300502
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300503 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700504
505 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
506 index = ke->index;
507 } else {
508 retval = input_scancode_to_scalar(ke, &scancode);
509 if (retval)
510 goto out;
511
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300512 index = ir_lookup_by_scancode(rc_map, scancode);
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300513 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700514
Dmitry Torokhov54e74b82011-01-28 23:33:29 -0800515 if (index < rc_map->len) {
516 entry = &rc_map->scan[index];
517
518 ke->index = index;
519 ke->keycode = entry->keycode;
520 ke->len = sizeof(entry->scancode);
521 memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
522
523 } else if (!(ke->flags & INPUT_KEYMAP_BY_INDEX)) {
524 /*
525 * We do not really know the valid range of scancodes
526 * so let's respond with KEY_RESERVED to anything we
527 * do not have mapping for [yet].
528 */
529 ke->index = index;
530 ke->keycode = KEY_RESERVED;
531 } else {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700532 retval = -EINVAL;
533 goto out;
534 }
535
Dmitry Torokhov47c5ba52010-10-31 15:18:42 -0700536 retval = 0;
537
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700538out:
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300539 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700540 return retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300541}
542
543/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300544 * rc_g_keycode_from_table() - gets the keycode that corresponds to a scancode
David Härdemand8b4b582010-10-29 16:08:23 -0300545 * @dev: the struct rc_dev descriptor of the device
546 * @scancode: the scancode to look for
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300547 *
David Härdemand8b4b582010-10-29 16:08:23 -0300548 * This routine is used by drivers which need to convert a scancode to a
549 * keycode. Normally it should not be used since drivers should have no
550 * interest in keycodes.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500551 *
552 * return: the corresponding keycode, or KEY_RESERVED
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300553 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300554u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300555{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300556 struct rc_map *rc_map = &dev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700557 unsigned int keycode;
558 unsigned int index;
559 unsigned long flags;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300560
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300561 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700562
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300563 index = ir_lookup_by_scancode(rc_map, scancode);
564 keycode = index < rc_map->len ?
565 rc_map->scan[index].keycode : KEY_RESERVED;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700566
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300567 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700568
Mauro Carvalho Chehab35438942010-04-03 16:53:16 -0300569 if (keycode != KEY_RESERVED)
Sean Young1f17f682018-02-12 07:27:50 -0500570 dev_dbg(&dev->dev, "%s: scancode 0x%04x keycode 0x%02x\n",
571 dev->device_name, scancode, keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700572
David Härdemanb3074c02010-04-02 15:58:28 -0300573 return keycode;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300574}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300575EXPORT_SYMBOL_GPL(rc_g_keycode_from_table);
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300576
577/**
David Härdeman62c65032010-10-29 16:08:07 -0300578 * ir_do_keyup() - internal function to signal the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300579 * @dev: the struct rc_dev descriptor of the device
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300580 * @sync: whether or not to call input_sync
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300581 *
David Härdeman62c65032010-10-29 16:08:07 -0300582 * This function is used internally to release a keypress, it must be
583 * called with keylock held.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300584 */
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300585static void ir_do_keyup(struct rc_dev *dev, bool sync)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300586{
David Härdemand8b4b582010-10-29 16:08:23 -0300587 if (!dev->keypressed)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300588 return;
589
Sean Young1f17f682018-02-12 07:27:50 -0500590 dev_dbg(&dev->dev, "keyup key 0x%04x\n", dev->last_keycode);
Sean Youngfb7ccc62017-12-03 08:55:24 -0500591 del_timer(&dev->timer_repeat);
David Härdemand8b4b582010-10-29 16:08:23 -0300592 input_report_key(dev->input_dev, dev->last_keycode, 0);
Sean Young153a60b2013-07-30 19:00:01 -0300593 led_trigger_event(led_feedback, LED_OFF);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300594 if (sync)
595 input_sync(dev->input_dev);
David Härdemand8b4b582010-10-29 16:08:23 -0300596 dev->keypressed = false;
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300597}
David Härdeman62c65032010-10-29 16:08:07 -0300598
599/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300600 * rc_keyup() - signals the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300601 * @dev: the struct rc_dev descriptor of the device
David Härdeman62c65032010-10-29 16:08:07 -0300602 *
603 * This routine is used to signal that a key has been released on the
604 * remote control.
605 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300606void rc_keyup(struct rc_dev *dev)
David Härdeman62c65032010-10-29 16:08:07 -0300607{
608 unsigned long flags;
David Härdeman62c65032010-10-29 16:08:07 -0300609
David Härdemand8b4b582010-10-29 16:08:23 -0300610 spin_lock_irqsave(&dev->keylock, flags);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300611 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300612 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300613}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300614EXPORT_SYMBOL_GPL(rc_keyup);
David Härdemana374fef2010-04-02 15:58:29 -0300615
616/**
617 * ir_timer_keyup() - generates a keyup event after a timeout
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500618 *
619 * @t: a pointer to the struct timer_list
David Härdemana374fef2010-04-02 15:58:29 -0300620 *
621 * This routine will generate a keyup event some time after a keydown event
622 * is generated when no further activity has been detected.
623 */
Kees Cookb17ec782017-10-24 11:23:14 -0400624static void ir_timer_keyup(struct timer_list *t)
David Härdemana374fef2010-04-02 15:58:29 -0300625{
Kees Cookb17ec782017-10-24 11:23:14 -0400626 struct rc_dev *dev = from_timer(dev, t, timer_keyup);
David Härdemana374fef2010-04-02 15:58:29 -0300627 unsigned long flags;
628
629 /*
630 * ir->keyup_jiffies is used to prevent a race condition if a
631 * hardware interrupt occurs at this point and the keyup timer
632 * event is moved further into the future as a result.
633 *
634 * The timer will then be reactivated and this function called
635 * again in the future. We need to exit gracefully in that case
636 * to allow the input subsystem to do its auto-repeat magic or
637 * a keyup event might follow immediately after the keydown.
638 */
David Härdemand8b4b582010-10-29 16:08:23 -0300639 spin_lock_irqsave(&dev->keylock, flags);
640 if (time_is_before_eq_jiffies(dev->keyup_jiffies))
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300641 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300642 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300643}
644
645/**
Sean Young57c642c2017-11-23 17:37:10 -0500646 * ir_timer_repeat() - generates a repeat event after a timeout
647 *
648 * @t: a pointer to the struct timer_list
649 *
650 * This routine will generate a soft repeat event every REP_PERIOD
651 * milliseconds.
652 */
653static void ir_timer_repeat(struct timer_list *t)
654{
655 struct rc_dev *dev = from_timer(dev, t, timer_repeat);
656 struct input_dev *input = dev->input_dev;
657 unsigned long flags;
658
659 spin_lock_irqsave(&dev->keylock, flags);
660 if (dev->keypressed) {
661 input_event(input, EV_KEY, dev->last_keycode, 2);
662 input_sync(input);
663 if (input->rep[REP_PERIOD])
664 mod_timer(&dev->timer_repeat, jiffies +
665 msecs_to_jiffies(input->rep[REP_PERIOD]));
666 }
667 spin_unlock_irqrestore(&dev->keylock, flags);
668}
669
Sean Youngf5dbee62018-07-28 05:11:15 -0400670static unsigned int repeat_period(int protocol)
671{
672 if (protocol >= ARRAY_SIZE(protocols))
673 return 100;
674
675 return protocols[protocol].repeat_period;
676}
677
Sean Young57c642c2017-11-23 17:37:10 -0500678/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300679 * rc_repeat() - signals that a key is still pressed
David Härdemand8b4b582010-10-29 16:08:23 -0300680 * @dev: the struct rc_dev descriptor of the device
David Härdemana374fef2010-04-02 15:58:29 -0300681 *
682 * This routine is used by IR decoders when a repeat message which does
683 * not include the necessary bits to reproduce the scancode has been
684 * received.
685 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300686void rc_repeat(struct rc_dev *dev)
David Härdemana374fef2010-04-02 15:58:29 -0300687{
688 unsigned long flags;
Sean Young28492252018-03-24 08:02:48 -0400689 unsigned int timeout = nsecs_to_jiffies(dev->timeout) +
Sean Youngf5dbee62018-07-28 05:11:15 -0400690 msecs_to_jiffies(repeat_period(dev->last_protocol));
Sean Youngb66218f2017-09-30 07:13:37 -0400691 struct lirc_scancode sc = {
692 .scancode = dev->last_scancode, .rc_proto = dev->last_protocol,
693 .keycode = dev->keypressed ? dev->last_keycode : KEY_RESERVED,
694 .flags = LIRC_SCANCODE_FLAG_REPEAT |
695 (dev->last_toggle ? LIRC_SCANCODE_FLAG_TOGGLE : 0)
696 };
697
Sean Younge5bb9d32018-10-22 05:01:50 -0400698 if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
699 ir_lirc_scancode_event(dev, &sc);
David Härdemana374fef2010-04-02 15:58:29 -0300700
David Härdemand8b4b582010-10-29 16:08:23 -0300701 spin_lock_irqsave(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300702
David Härdeman265a2982017-06-22 15:23:54 -0400703 input_event(dev->input_dev, EV_MSC, MSC_SCAN, dev->last_scancode);
704 input_sync(dev->input_dev);
705
Sean Youngb66218f2017-09-30 07:13:37 -0400706 if (dev->keypressed) {
Sean Young28492252018-03-24 08:02:48 -0400707 dev->keyup_jiffies = jiffies + timeout;
Sean Youngb66218f2017-09-30 07:13:37 -0400708 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
709 }
David Härdemana374fef2010-04-02 15:58:29 -0300710
David Härdemand8b4b582010-10-29 16:08:23 -0300711 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300712}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300713EXPORT_SYMBOL_GPL(rc_repeat);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300714
715/**
David Härdeman62c65032010-10-29 16:08:07 -0300716 * ir_do_keydown() - internal function to process a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300717 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300718 * @protocol: the protocol of the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300719 * @scancode: the scancode of the keypress
720 * @keycode: the keycode of the keypress
721 * @toggle: the toggle value of the keypress
722 *
723 * This function is used internally to register a keypress, it must be
724 * called with keylock held.
725 */
Sean Young6d741bf2017-08-07 16:20:58 -0400726static void ir_do_keydown(struct rc_dev *dev, enum rc_proto protocol,
David Härdeman120703f2014-04-03 20:31:30 -0300727 u32 scancode, u32 keycode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300728{
David Härdeman99b0f3c2014-04-04 19:06:06 -0300729 bool new_event = (!dev->keypressed ||
David Härdeman120703f2014-04-03 20:31:30 -0300730 dev->last_protocol != protocol ||
David Härdeman99b0f3c2014-04-04 19:06:06 -0300731 dev->last_scancode != scancode ||
David Härdeman120703f2014-04-03 20:31:30 -0300732 dev->last_toggle != toggle);
Sean Youngde142c32017-02-25 06:51:32 -0500733 struct lirc_scancode sc = {
734 .scancode = scancode, .rc_proto = protocol,
735 .flags = toggle ? LIRC_SCANCODE_FLAG_TOGGLE : 0,
736 .keycode = keycode
737 };
738
Sean Younge5bb9d32018-10-22 05:01:50 -0400739 if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
740 ir_lirc_scancode_event(dev, &sc);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300741
742 if (new_event && dev->keypressed)
743 ir_do_keyup(dev, false);
744
David Härdemand8b4b582010-10-29 16:08:23 -0300745 input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300746
Sean Youngb66218f2017-09-30 07:13:37 -0400747 dev->last_protocol = protocol;
748 dev->last_scancode = scancode;
749 dev->last_toggle = toggle;
750 dev->last_keycode = keycode;
751
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300752 if (new_event && keycode != KEY_RESERVED) {
753 /* Register a keypress */
754 dev->keypressed = true;
David Härdeman62c65032010-10-29 16:08:07 -0300755
Sean Young1f17f682018-02-12 07:27:50 -0500756 dev_dbg(&dev->dev, "%s: key down event, key 0x%04x, protocol 0x%04x, scancode 0x%08x\n",
757 dev->device_name, keycode, protocol, scancode);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300758 input_report_key(dev->input_dev, keycode, 1);
James Hogan70a2f912014-01-16 19:56:22 -0300759
760 led_trigger_event(led_feedback, LED_FULL);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300761 }
David Härdeman62c65032010-10-29 16:08:07 -0300762
Sean Young57c642c2017-11-23 17:37:10 -0500763 /*
764 * For CEC, start sending repeat messages as soon as the first
765 * repeated message is sent, as long as REP_DELAY = 0 and REP_PERIOD
766 * is non-zero. Otherwise, the input layer will generate repeat
767 * messages.
768 */
769 if (!new_event && keycode != KEY_RESERVED &&
770 dev->allowed_protocols == RC_PROTO_BIT_CEC &&
771 !timer_pending(&dev->timer_repeat) &&
772 dev->input_dev->rep[REP_PERIOD] &&
773 !dev->input_dev->rep[REP_DELAY]) {
774 input_event(dev->input_dev, EV_KEY, keycode, 2);
775 mod_timer(&dev->timer_repeat, jiffies +
776 msecs_to_jiffies(dev->input_dev->rep[REP_PERIOD]));
777 }
778
David Härdemand8b4b582010-10-29 16:08:23 -0300779 input_sync(dev->input_dev);
David Härdeman62c65032010-10-29 16:08:07 -0300780}
781
782/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300783 * rc_keydown() - generates input event for a key press
David Härdemand8b4b582010-10-29 16:08:23 -0300784 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300785 * @protocol: the protocol for the keypress
786 * @scancode: the scancode for the keypress
David Härdemana374fef2010-04-02 15:58:29 -0300787 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
788 * support toggle values, this should be set to zero)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300789 *
David Härdemand8b4b582010-10-29 16:08:23 -0300790 * This routine is used to signal that a key has been pressed on the
791 * remote control.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300792 */
Sean Young6d741bf2017-08-07 16:20:58 -0400793void rc_keydown(struct rc_dev *dev, enum rc_proto protocol, u32 scancode,
794 u8 toggle)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300795{
David Härdemana374fef2010-04-02 15:58:29 -0300796 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300797 u32 keycode = rc_g_keycode_from_table(dev, scancode);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300798
David Härdemand8b4b582010-10-29 16:08:23 -0300799 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300800 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300801
David Härdemand8b4b582010-10-29 16:08:23 -0300802 if (dev->keypressed) {
Sean Young28492252018-03-24 08:02:48 -0400803 dev->keyup_jiffies = jiffies + nsecs_to_jiffies(dev->timeout) +
Sean Youngf5dbee62018-07-28 05:11:15 -0400804 msecs_to_jiffies(repeat_period(protocol));
David Härdemand8b4b582010-10-29 16:08:23 -0300805 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
David Härdeman62c65032010-10-29 16:08:07 -0300806 }
David Härdemand8b4b582010-10-29 16:08:23 -0300807 spin_unlock_irqrestore(&dev->keylock, flags);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300808}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300809EXPORT_SYMBOL_GPL(rc_keydown);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300810
David Härdeman62c65032010-10-29 16:08:07 -0300811/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300812 * rc_keydown_notimeout() - generates input event for a key press without
David Härdeman62c65032010-10-29 16:08:07 -0300813 * an automatic keyup event at a later time
David Härdemand8b4b582010-10-29 16:08:23 -0300814 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300815 * @protocol: the protocol for the keypress
816 * @scancode: the scancode for the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300817 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
818 * support toggle values, this should be set to zero)
819 *
David Härdemand8b4b582010-10-29 16:08:23 -0300820 * This routine is used to signal that a key has been pressed on the
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300821 * remote control. The driver must manually call rc_keyup() at a later stage.
David Härdeman62c65032010-10-29 16:08:07 -0300822 */
Sean Young6d741bf2017-08-07 16:20:58 -0400823void rc_keydown_notimeout(struct rc_dev *dev, enum rc_proto protocol,
David Härdeman120703f2014-04-03 20:31:30 -0300824 u32 scancode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300825{
826 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300827 u32 keycode = rc_g_keycode_from_table(dev, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300828
David Härdemand8b4b582010-10-29 16:08:23 -0300829 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300830 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
David Härdemand8b4b582010-10-29 16:08:23 -0300831 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300832}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300833EXPORT_SYMBOL_GPL(rc_keydown_notimeout);
David Härdeman62c65032010-10-29 16:08:07 -0300834
Sean Youngb590c0b2016-12-05 19:24:59 -0200835/**
Sean Young6b514c42017-10-09 16:32:41 -0400836 * rc_validate_scancode() - checks that a scancode is valid for a protocol.
837 * For nec, it should do the opposite of ir_nec_bytes_to_scancode()
Sean Young49a4b362017-09-27 16:00:49 -0400838 * @proto: protocol
839 * @scancode: scancode
840 */
841bool rc_validate_scancode(enum rc_proto proto, u32 scancode)
842{
843 switch (proto) {
Sean Young6b514c42017-10-09 16:32:41 -0400844 /*
845 * NECX has a 16-bit address; if the lower 8 bits match the upper
846 * 8 bits inverted, then the address would match regular nec.
847 */
Sean Young49a4b362017-09-27 16:00:49 -0400848 case RC_PROTO_NECX:
849 if ((((scancode >> 16) ^ ~(scancode >> 8)) & 0xff) == 0)
850 return false;
851 break;
Sean Young6b514c42017-10-09 16:32:41 -0400852 /*
853 * NEC32 has a 16 bit address and 16 bit command. If the lower 8 bits
854 * of the command match the upper 8 bits inverted, then it would
855 * be either NEC or NECX.
856 */
Sean Young49a4b362017-09-27 16:00:49 -0400857 case RC_PROTO_NEC32:
Sean Young6b514c42017-10-09 16:32:41 -0400858 if ((((scancode >> 8) ^ ~scancode) & 0xff) == 0)
Sean Young49a4b362017-09-27 16:00:49 -0400859 return false;
860 break;
Sean Young6b514c42017-10-09 16:32:41 -0400861 /*
862 * If the customer code (top 32-bit) is 0x800f, it is MCE else it
863 * is regular mode-6a 32 bit
864 */
Sean Young49a4b362017-09-27 16:00:49 -0400865 case RC_PROTO_RC6_MCE:
866 if ((scancode & 0xffff0000) != 0x800f0000)
867 return false;
868 break;
869 case RC_PROTO_RC6_6A_32:
870 if ((scancode & 0xffff0000) == 0x800f0000)
871 return false;
872 break;
873 default:
874 break;
875 }
876
877 return true;
878}
879
880/**
Sean Youngb590c0b2016-12-05 19:24:59 -0200881 * rc_validate_filter() - checks that the scancode and mask are valid and
882 * provides sensible defaults
James Hoganf423ccc2015-03-31 14:48:10 -0300883 * @dev: the struct rc_dev descriptor of the device
Sean Youngb590c0b2016-12-05 19:24:59 -0200884 * @filter: the scancode and mask
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500885 *
886 * return: 0 or -EINVAL if the filter is not valid
Sean Youngb590c0b2016-12-05 19:24:59 -0200887 */
James Hoganf423ccc2015-03-31 14:48:10 -0300888static int rc_validate_filter(struct rc_dev *dev,
Sean Youngb590c0b2016-12-05 19:24:59 -0200889 struct rc_scancode_filter *filter)
890{
Sean Youngd57ea872017-08-09 13:19:16 -0400891 u32 mask, s = filter->data;
Sean Young6d741bf2017-08-07 16:20:58 -0400892 enum rc_proto protocol = dev->wakeup_protocol;
Sean Youngb590c0b2016-12-05 19:24:59 -0200893
Sean Youngd57ea872017-08-09 13:19:16 -0400894 if (protocol >= ARRAY_SIZE(protocols))
Sean Young2168b412017-08-07 09:21:29 -0400895 return -EINVAL;
896
Sean Youngd57ea872017-08-09 13:19:16 -0400897 mask = protocols[protocol].scancode_bits;
898
Sean Young49a4b362017-09-27 16:00:49 -0400899 if (!rc_validate_scancode(protocol, s))
900 return -EINVAL;
Sean Youngb590c0b2016-12-05 19:24:59 -0200901
Sean Youngd57ea872017-08-09 13:19:16 -0400902 filter->data &= mask;
903 filter->mask &= mask;
Sean Youngb590c0b2016-12-05 19:24:59 -0200904
James Hoganf423ccc2015-03-31 14:48:10 -0300905 /*
906 * If we have to raw encode the IR for wakeup, we cannot have a mask
907 */
Sean Youngd57ea872017-08-09 13:19:16 -0400908 if (dev->encode_wakeup && filter->mask != 0 && filter->mask != mask)
James Hoganf423ccc2015-03-31 14:48:10 -0300909 return -EINVAL;
910
Sean Youngb590c0b2016-12-05 19:24:59 -0200911 return 0;
912}
913
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300914int rc_open(struct rc_dev *rdev)
915{
916 int rval = 0;
917
918 if (!rdev)
919 return -EINVAL;
920
921 mutex_lock(&rdev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -0200922
Sean Youngcb843432017-09-23 17:44:03 -0400923 if (!rdev->registered) {
924 rval = -ENODEV;
925 } else {
926 if (!rdev->users++ && rdev->open)
927 rval = rdev->open(rdev);
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300928
Sean Youngcb843432017-09-23 17:44:03 -0400929 if (rval)
930 rdev->users--;
931 }
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300932
933 mutex_unlock(&rdev->lock);
934
935 return rval;
936}
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300937
David Härdemand8b4b582010-10-29 16:08:23 -0300938static int ir_open(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300939{
David Härdemand8b4b582010-10-29 16:08:23 -0300940 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300941
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300942 return rc_open(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300943}
944
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300945void rc_close(struct rc_dev *rdev)
946{
947 if (rdev) {
948 mutex_lock(&rdev->lock);
949
Sean Youngcb843432017-09-23 17:44:03 -0400950 if (!--rdev->users && rdev->close && rdev->registered)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300951 rdev->close(rdev);
952
953 mutex_unlock(&rdev->lock);
954 }
955}
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300956
David Härdemand8b4b582010-10-29 16:08:23 -0300957static void ir_close(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300958{
David Härdemand8b4b582010-10-29 16:08:23 -0300959 struct rc_dev *rdev = input_get_drvdata(idev);
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300960 rc_close(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300961}
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300962
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300963/* class for /sys/class/rc */
David Härdeman40fc5322013-03-06 16:52:10 -0300964static char *rc_devnode(struct device *dev, umode_t *mode)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300965{
966 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
967}
968
David Härdeman40fc5322013-03-06 16:52:10 -0300969static struct class rc_class = {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300970 .name = "rc",
David Härdeman40fc5322013-03-06 16:52:10 -0300971 .devnode = rc_devnode,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300972};
973
David Härdemanc003ab12012-10-11 19:11:54 -0300974/*
975 * These are the protocol textual descriptions that are
976 * used by the sysfs protocols file. Note that the order
977 * of the entries is relevant.
978 */
Heiner Kallweit53df8772015-11-16 17:52:17 -0200979static const struct {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300980 u64 type;
Heiner Kallweit53df8772015-11-16 17:52:17 -0200981 const char *name;
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200982 const char *module_name;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300983} proto_names[] = {
Sean Young6d741bf2017-08-07 16:20:58 -0400984 { RC_PROTO_BIT_NONE, "none", NULL },
985 { RC_PROTO_BIT_OTHER, "other", NULL },
986 { RC_PROTO_BIT_UNKNOWN, "unknown", NULL },
987 { RC_PROTO_BIT_RC5 |
988 RC_PROTO_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
989 { RC_PROTO_BIT_NEC |
990 RC_PROTO_BIT_NECX |
991 RC_PROTO_BIT_NEC32, "nec", "ir-nec-decoder" },
992 { RC_PROTO_BIT_RC6_0 |
993 RC_PROTO_BIT_RC6_6A_20 |
994 RC_PROTO_BIT_RC6_6A_24 |
995 RC_PROTO_BIT_RC6_6A_32 |
996 RC_PROTO_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
997 { RC_PROTO_BIT_JVC, "jvc", "ir-jvc-decoder" },
998 { RC_PROTO_BIT_SONY12 |
999 RC_PROTO_BIT_SONY15 |
1000 RC_PROTO_BIT_SONY20, "sony", "ir-sony-decoder" },
1001 { RC_PROTO_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
1002 { RC_PROTO_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
1003 { RC_PROTO_BIT_SHARP, "sharp", "ir-sharp-decoder" },
1004 { RC_PROTO_BIT_MCIR2_KBD |
1005 RC_PROTO_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" },
1006 { RC_PROTO_BIT_XMP, "xmp", "ir-xmp-decoder" },
1007 { RC_PROTO_BIT_CEC, "cec", NULL },
Sean Young447dcc0c2017-12-03 11:06:54 -05001008 { RC_PROTO_BIT_IMON, "imon", "ir-imon-decoder" },
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001009};
1010
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001011/**
James Hoganab88c662014-02-28 20:17:05 -03001012 * struct rc_filter_attribute - Device attribute relating to a filter type.
1013 * @attr: Device attribute.
1014 * @type: Filter type.
1015 * @mask: false for filter value, true for filter mask.
1016 */
1017struct rc_filter_attribute {
1018 struct device_attribute attr;
1019 enum rc_filter_type type;
1020 bool mask;
1021};
1022#define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
1023
James Hoganab88c662014-02-28 20:17:05 -03001024#define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
1025 struct rc_filter_attribute dev_attr_##_name = { \
1026 .attr = __ATTR(_name, _mode, _show, _store), \
1027 .type = (_type), \
1028 .mask = (_mask), \
1029 }
1030
1031/**
Sean Young0751d332016-12-05 17:08:35 -02001032 * show_protocols() - shows the current IR protocol(s)
David Härdemand8b4b582010-10-29 16:08:23 -03001033 * @device: the device descriptor
David Härdemanda6e1622014-04-03 20:32:16 -03001034 * @mattr: the device attribute struct
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001035 * @buf: a pointer to the output buffer
1036 *
1037 * This routine is a callback routine for input read the IR protocol type(s).
Sean Young0751d332016-12-05 17:08:35 -02001038 * it is trigged by reading /sys/class/rc/rc?/protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001039 * It returns the protocol names of supported protocols.
1040 * Enabled protocols are printed in brackets.
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001041 *
David Härdeman18726a32017-04-27 17:34:08 -03001042 * dev->lock is taken to guard against races between
1043 * store_protocols and show_protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001044 */
David Härdemand8b4b582010-10-29 16:08:23 -03001045static ssize_t show_protocols(struct device *device,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001046 struct device_attribute *mattr, char *buf)
1047{
David Härdemand8b4b582010-10-29 16:08:23 -03001048 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001049 u64 allowed, enabled;
1050 char *tmp = buf;
1051 int i;
1052
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001053 mutex_lock(&dev->lock);
1054
Sean Young0751d332016-12-05 17:08:35 -02001055 enabled = dev->enabled_protocols;
1056 allowed = dev->allowed_protocols;
1057 if (dev->raw && !allowed)
1058 allowed = ir_raw_get_allowed_protocols();
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001059
David Härdemanda6e1622014-04-03 20:32:16 -03001060 mutex_unlock(&dev->lock);
1061
Sean Young1f17f682018-02-12 07:27:50 -05001062 dev_dbg(&dev->dev, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
1063 __func__, (long long)allowed, (long long)enabled);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001064
1065 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1066 if (allowed & enabled & proto_names[i].type)
1067 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
1068 else if (allowed & proto_names[i].type)
1069 tmp += sprintf(tmp, "%s ", proto_names[i].name);
David Härdemanc003ab12012-10-11 19:11:54 -03001070
1071 if (allowed & proto_names[i].type)
1072 allowed &= ~proto_names[i].type;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001073 }
1074
Sean Younga60d64b2017-09-23 10:41:13 -04001075#ifdef CONFIG_LIRC
1076 if (dev->driver_type == RC_DRIVER_IR_RAW)
David Härdeman275ddb42015-05-19 19:03:22 -03001077 tmp += sprintf(tmp, "[lirc] ");
Sean Younga60d64b2017-09-23 10:41:13 -04001078#endif
David Härdeman275ddb42015-05-19 19:03:22 -03001079
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001080 if (tmp != buf)
1081 tmp--;
1082 *tmp = '\n';
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001083
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001084 return tmp + 1 - buf;
1085}
1086
1087/**
David Härdemanda6e1622014-04-03 20:32:16 -03001088 * parse_protocol_change() - parses a protocol change request
Sean Young1f17f682018-02-12 07:27:50 -05001089 * @dev: rc_dev device
David Härdemanda6e1622014-04-03 20:32:16 -03001090 * @protocols: pointer to the bitmask of current protocols
1091 * @buf: pointer to the buffer with a list of changes
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001092 *
David Härdemanda6e1622014-04-03 20:32:16 -03001093 * Writing "+proto" will add a protocol to the protocol mask.
1094 * Writing "-proto" will remove a protocol from protocol mask.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001095 * Writing "proto" will enable only "proto".
1096 * Writing "none" will disable all protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001097 * Returns the number of changes performed or a negative error code.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001098 */
Sean Young1f17f682018-02-12 07:27:50 -05001099static int parse_protocol_change(struct rc_dev *dev, u64 *protocols,
1100 const char *buf)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001101{
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001102 const char *tmp;
David Härdemanda6e1622014-04-03 20:32:16 -03001103 unsigned count = 0;
1104 bool enable, disable;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001105 u64 mask;
David Härdemanda6e1622014-04-03 20:32:16 -03001106 int i;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001107
David Härdemanda6e1622014-04-03 20:32:16 -03001108 while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001109 if (!*tmp)
1110 break;
1111
1112 if (*tmp == '+') {
1113 enable = true;
1114 disable = false;
1115 tmp++;
1116 } else if (*tmp == '-') {
1117 enable = false;
1118 disable = true;
1119 tmp++;
1120 } else {
1121 enable = false;
1122 disable = false;
1123 }
1124
David Härdemanc003ab12012-10-11 19:11:54 -03001125 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1126 if (!strcasecmp(tmp, proto_names[i].name)) {
1127 mask = proto_names[i].type;
1128 break;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001129 }
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001130 }
1131
David Härdemanc003ab12012-10-11 19:11:54 -03001132 if (i == ARRAY_SIZE(proto_names)) {
David Härdeman275ddb42015-05-19 19:03:22 -03001133 if (!strcasecmp(tmp, "lirc"))
1134 mask = 0;
1135 else {
Sean Young1f17f682018-02-12 07:27:50 -05001136 dev_dbg(&dev->dev, "Unknown protocol: '%s'\n",
1137 tmp);
David Härdeman275ddb42015-05-19 19:03:22 -03001138 return -EINVAL;
1139 }
David Härdemanc003ab12012-10-11 19:11:54 -03001140 }
1141
1142 count++;
1143
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001144 if (enable)
David Härdemanda6e1622014-04-03 20:32:16 -03001145 *protocols |= mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001146 else if (disable)
David Härdemanda6e1622014-04-03 20:32:16 -03001147 *protocols &= ~mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001148 else
David Härdemanda6e1622014-04-03 20:32:16 -03001149 *protocols = mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001150 }
1151
1152 if (!count) {
Sean Young1f17f682018-02-12 07:27:50 -05001153 dev_dbg(&dev->dev, "Protocol not specified\n");
David Härdemanda6e1622014-04-03 20:32:16 -03001154 return -EINVAL;
1155 }
1156
1157 return count;
1158}
1159
Sean Young0d39ab02017-02-25 06:51:31 -05001160void ir_raw_load_modules(u64 *protocols)
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001161{
1162 u64 available;
1163 int i, ret;
1164
1165 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
Sean Young6d741bf2017-08-07 16:20:58 -04001166 if (proto_names[i].type == RC_PROTO_BIT_NONE ||
1167 proto_names[i].type & (RC_PROTO_BIT_OTHER |
1168 RC_PROTO_BIT_UNKNOWN))
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001169 continue;
1170
1171 available = ir_raw_get_allowed_protocols();
1172 if (!(*protocols & proto_names[i].type & ~available))
1173 continue;
1174
1175 if (!proto_names[i].module_name) {
1176 pr_err("Can't enable IR protocol %s\n",
1177 proto_names[i].name);
1178 *protocols &= ~proto_names[i].type;
1179 continue;
1180 }
1181
1182 ret = request_module("%s", proto_names[i].module_name);
1183 if (ret < 0) {
1184 pr_err("Couldn't load IR protocol module %s\n",
1185 proto_names[i].module_name);
1186 *protocols &= ~proto_names[i].type;
1187 continue;
1188 }
1189 msleep(20);
1190 available = ir_raw_get_allowed_protocols();
1191 if (!(*protocols & proto_names[i].type & ~available))
1192 continue;
1193
Sean Young8caebcd2017-01-19 19:33:49 -02001194 pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001195 proto_names[i].module_name,
1196 proto_names[i].name);
1197 *protocols &= ~proto_names[i].type;
1198 }
1199}
1200
David Härdemanda6e1622014-04-03 20:32:16 -03001201/**
1202 * store_protocols() - changes the current/wakeup IR protocol(s)
1203 * @device: the device descriptor
1204 * @mattr: the device attribute struct
1205 * @buf: a pointer to the input buffer
1206 * @len: length of the input buffer
1207 *
1208 * This routine is for changing the IR protocol type.
1209 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
1210 * See parse_protocol_change() for the valid commands.
1211 * Returns @len on success or a negative error code.
1212 *
David Härdeman18726a32017-04-27 17:34:08 -03001213 * dev->lock is taken to guard against races between
1214 * store_protocols and show_protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001215 */
1216static ssize_t store_protocols(struct device *device,
1217 struct device_attribute *mattr,
1218 const char *buf, size_t len)
1219{
1220 struct rc_dev *dev = to_rc_dev(device);
David Härdemanda6e1622014-04-03 20:32:16 -03001221 u64 *current_protocols;
David Härdemanda6e1622014-04-03 20:32:16 -03001222 struct rc_scancode_filter *filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001223 u64 old_protocols, new_protocols;
1224 ssize_t rc;
1225
Sean Young1f17f682018-02-12 07:27:50 -05001226 dev_dbg(&dev->dev, "Normal protocol change requested\n");
Sean Young0751d332016-12-05 17:08:35 -02001227 current_protocols = &dev->enabled_protocols;
1228 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001229
Sean Young0751d332016-12-05 17:08:35 -02001230 if (!dev->change_protocol) {
Sean Young1f17f682018-02-12 07:27:50 -05001231 dev_dbg(&dev->dev, "Protocol switching not supported\n");
David Härdemanda6e1622014-04-03 20:32:16 -03001232 return -EINVAL;
1233 }
1234
1235 mutex_lock(&dev->lock);
1236
1237 old_protocols = *current_protocols;
1238 new_protocols = old_protocols;
Sean Young1f17f682018-02-12 07:27:50 -05001239 rc = parse_protocol_change(dev, &new_protocols, buf);
David Härdemanda6e1622014-04-03 20:32:16 -03001240 if (rc < 0)
1241 goto out;
1242
Sean Younga86d6df2018-03-23 16:47:37 -04001243 if (dev->driver_type == RC_DRIVER_IR_RAW)
1244 ir_raw_load_modules(&new_protocols);
1245
Sean Young0751d332016-12-05 17:08:35 -02001246 rc = dev->change_protocol(dev, &new_protocols);
David Härdemanda6e1622014-04-03 20:32:16 -03001247 if (rc < 0) {
Sean Young1f17f682018-02-12 07:27:50 -05001248 dev_dbg(&dev->dev, "Error setting protocols to 0x%llx\n",
1249 (long long)new_protocols);
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001250 goto out;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001251 }
1252
James Hogan983c5bd2014-12-08 13:17:07 -03001253 if (new_protocols != old_protocols) {
1254 *current_protocols = new_protocols;
Sean Young1f17f682018-02-12 07:27:50 -05001255 dev_dbg(&dev->dev, "Protocols changed to 0x%llx\n",
1256 (long long)new_protocols);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001257 }
1258
James Hogan6bea25a2014-02-28 20:17:06 -03001259 /*
James Hogan983c5bd2014-12-08 13:17:07 -03001260 * If a protocol change was attempted the filter may need updating, even
1261 * if the actual protocol mask hasn't changed (since the driver may have
1262 * cleared the filter).
James Hogan6bea25a2014-02-28 20:17:06 -03001263 * Try setting the same filter with the new protocol (if any).
1264 * Fall back to clearing the filter.
1265 */
Sean Young0751d332016-12-05 17:08:35 -02001266 if (dev->s_filter && filter->mask) {
David Härdemanda6e1622014-04-03 20:32:16 -03001267 if (new_protocols)
Sean Young0751d332016-12-05 17:08:35 -02001268 rc = dev->s_filter(dev, filter);
David Härdemanda6e1622014-04-03 20:32:16 -03001269 else
1270 rc = -1;
David Härdeman23c843b2014-04-04 19:06:01 -03001271
David Härdemanda6e1622014-04-03 20:32:16 -03001272 if (rc < 0) {
1273 filter->data = 0;
1274 filter->mask = 0;
Sean Young0751d332016-12-05 17:08:35 -02001275 dev->s_filter(dev, filter);
James Hogan6bea25a2014-02-28 20:17:06 -03001276 }
James Hogan6bea25a2014-02-28 20:17:06 -03001277 }
1278
David Härdemanda6e1622014-04-03 20:32:16 -03001279 rc = len;
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001280
1281out:
1282 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001283 return rc;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001284}
1285
James Hogan00942d12014-01-17 10:58:49 -03001286/**
James Hogan00942d12014-01-17 10:58:49 -03001287 * show_filter() - shows the current scancode filter value or mask
1288 * @device: the device descriptor
1289 * @attr: the device attribute struct
1290 * @buf: a pointer to the output buffer
1291 *
1292 * This routine is a callback routine to read a scancode filter value or mask.
1293 * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
1294 * It prints the current scancode filter value or mask of the appropriate filter
1295 * type in hexadecimal into @buf and returns the size of the buffer.
1296 *
1297 * Bits of the filter value corresponding to set bits in the filter mask are
1298 * compared against input scancodes and non-matching scancodes are discarded.
1299 *
David Härdeman18726a32017-04-27 17:34:08 -03001300 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001301 * store_filter and show_filter.
1302 */
1303static ssize_t show_filter(struct device *device,
1304 struct device_attribute *attr,
1305 char *buf)
1306{
1307 struct rc_dev *dev = to_rc_dev(device);
1308 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001309 struct rc_scancode_filter *filter;
James Hogan00942d12014-01-17 10:58:49 -03001310 u32 val;
1311
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001312 mutex_lock(&dev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001313
David Härdemanda6e1622014-04-03 20:32:16 -03001314 if (fattr->type == RC_FILTER_NORMAL)
David Härdemanc5540fb2014-04-03 20:32:21 -03001315 filter = &dev->scancode_filter;
James Hogan00942d12014-01-17 10:58:49 -03001316 else
David Härdemanc5540fb2014-04-03 20:32:21 -03001317 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001318
David Härdemanda6e1622014-04-03 20:32:16 -03001319 if (fattr->mask)
1320 val = filter->mask;
1321 else
1322 val = filter->data;
James Hogan00942d12014-01-17 10:58:49 -03001323 mutex_unlock(&dev->lock);
1324
1325 return sprintf(buf, "%#x\n", val);
1326}
1327
1328/**
1329 * store_filter() - changes the scancode filter value
1330 * @device: the device descriptor
1331 * @attr: the device attribute struct
1332 * @buf: a pointer to the input buffer
1333 * @len: length of the input buffer
1334 *
1335 * This routine is for changing a scancode filter value or mask.
1336 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
1337 * Returns -EINVAL if an invalid filter value for the current protocol was
1338 * specified or if scancode filtering is not supported by the driver, otherwise
1339 * returns @len.
1340 *
1341 * Bits of the filter value corresponding to set bits in the filter mask are
1342 * compared against input scancodes and non-matching scancodes are discarded.
1343 *
David Härdeman18726a32017-04-27 17:34:08 -03001344 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001345 * store_filter and show_filter.
1346 */
1347static ssize_t store_filter(struct device *device,
1348 struct device_attribute *attr,
David Härdemanda6e1622014-04-03 20:32:16 -03001349 const char *buf, size_t len)
James Hogan00942d12014-01-17 10:58:49 -03001350{
1351 struct rc_dev *dev = to_rc_dev(device);
1352 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001353 struct rc_scancode_filter new_filter, *filter;
James Hogan00942d12014-01-17 10:58:49 -03001354 int ret;
1355 unsigned long val;
David Härdeman23c843b2014-04-04 19:06:01 -03001356 int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
James Hogan00942d12014-01-17 10:58:49 -03001357
James Hogan00942d12014-01-17 10:58:49 -03001358 ret = kstrtoul(buf, 0, &val);
1359 if (ret < 0)
1360 return ret;
1361
David Härdemanda6e1622014-04-03 20:32:16 -03001362 if (fattr->type == RC_FILTER_NORMAL) {
1363 set_filter = dev->s_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001364 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001365 } else {
1366 set_filter = dev->s_wakeup_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001367 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001368 }
1369
David Härdeman99b0f3c2014-04-04 19:06:06 -03001370 if (!set_filter)
1371 return -EINVAL;
James Hogan00942d12014-01-17 10:58:49 -03001372
1373 mutex_lock(&dev->lock);
1374
David Härdemanda6e1622014-04-03 20:32:16 -03001375 new_filter = *filter;
James Hogan00942d12014-01-17 10:58:49 -03001376 if (fattr->mask)
David Härdemanda6e1622014-04-03 20:32:16 -03001377 new_filter.mask = val;
James Hogan00942d12014-01-17 10:58:49 -03001378 else
David Härdemanda6e1622014-04-03 20:32:16 -03001379 new_filter.data = val;
David Härdeman23c843b2014-04-04 19:06:01 -03001380
Sean Young0751d332016-12-05 17:08:35 -02001381 if (fattr->type == RC_FILTER_WAKEUP) {
Sean Youngb590c0b2016-12-05 19:24:59 -02001382 /*
1383 * Refuse to set a filter unless a protocol is enabled
1384 * and the filter is valid for that protocol
1385 */
Sean Young6d741bf2017-08-07 16:20:58 -04001386 if (dev->wakeup_protocol != RC_PROTO_UNKNOWN)
James Hoganf423ccc2015-03-31 14:48:10 -03001387 ret = rc_validate_filter(dev, &new_filter);
Sean Youngb590c0b2016-12-05 19:24:59 -02001388 else
Sean Young0751d332016-12-05 17:08:35 -02001389 ret = -EINVAL;
Sean Youngb590c0b2016-12-05 19:24:59 -02001390
1391 if (ret != 0)
Sean Young0751d332016-12-05 17:08:35 -02001392 goto unlock;
Sean Young0751d332016-12-05 17:08:35 -02001393 }
1394
1395 if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
1396 val) {
James Hogan6bea25a2014-02-28 20:17:06 -03001397 /* refuse to set a filter unless a protocol is enabled */
1398 ret = -EINVAL;
1399 goto unlock;
1400 }
David Härdeman23c843b2014-04-04 19:06:01 -03001401
David Härdemanda6e1622014-04-03 20:32:16 -03001402 ret = set_filter(dev, &new_filter);
David Härdeman99b0f3c2014-04-04 19:06:06 -03001403 if (ret < 0)
1404 goto unlock;
James Hogan00942d12014-01-17 10:58:49 -03001405
David Härdemanda6e1622014-04-03 20:32:16 -03001406 *filter = new_filter;
James Hogan00942d12014-01-17 10:58:49 -03001407
1408unlock:
1409 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001410 return (ret < 0) ? ret : len;
James Hogan00942d12014-01-17 10:58:49 -03001411}
1412
Sean Young0751d332016-12-05 17:08:35 -02001413/**
1414 * show_wakeup_protocols() - shows the wakeup IR protocol
1415 * @device: the device descriptor
1416 * @mattr: the device attribute struct
1417 * @buf: a pointer to the output buffer
1418 *
1419 * This routine is a callback routine for input read the IR protocol type(s).
1420 * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols.
1421 * It returns the protocol names of supported protocols.
1422 * The enabled protocols are printed in brackets.
1423 *
David Härdeman18726a32017-04-27 17:34:08 -03001424 * dev->lock is taken to guard against races between
1425 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001426 */
1427static ssize_t show_wakeup_protocols(struct device *device,
1428 struct device_attribute *mattr,
1429 char *buf)
1430{
1431 struct rc_dev *dev = to_rc_dev(device);
1432 u64 allowed;
Sean Young6d741bf2017-08-07 16:20:58 -04001433 enum rc_proto enabled;
Sean Young0751d332016-12-05 17:08:35 -02001434 char *tmp = buf;
1435 int i;
1436
Sean Young0751d332016-12-05 17:08:35 -02001437 mutex_lock(&dev->lock);
1438
1439 allowed = dev->allowed_wakeup_protocols;
1440 enabled = dev->wakeup_protocol;
1441
1442 mutex_unlock(&dev->lock);
1443
Sean Young1f17f682018-02-12 07:27:50 -05001444 dev_dbg(&dev->dev, "%s: allowed - 0x%llx, enabled - %d\n",
1445 __func__, (long long)allowed, enabled);
Sean Young0751d332016-12-05 17:08:35 -02001446
Sean Youngd57ea872017-08-09 13:19:16 -04001447 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001448 if (allowed & (1ULL << i)) {
1449 if (i == enabled)
Sean Youngd57ea872017-08-09 13:19:16 -04001450 tmp += sprintf(tmp, "[%s] ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001451 else
Sean Youngd57ea872017-08-09 13:19:16 -04001452 tmp += sprintf(tmp, "%s ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001453 }
1454 }
1455
1456 if (tmp != buf)
1457 tmp--;
1458 *tmp = '\n';
1459
1460 return tmp + 1 - buf;
1461}
1462
1463/**
1464 * store_wakeup_protocols() - changes the wakeup IR protocol(s)
1465 * @device: the device descriptor
1466 * @mattr: the device attribute struct
1467 * @buf: a pointer to the input buffer
1468 * @len: length of the input buffer
1469 *
1470 * This routine is for changing the IR protocol type.
1471 * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols.
1472 * Returns @len on success or a negative error code.
1473 *
David Härdeman18726a32017-04-27 17:34:08 -03001474 * dev->lock is taken to guard against races between
1475 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001476 */
1477static ssize_t store_wakeup_protocols(struct device *device,
1478 struct device_attribute *mattr,
1479 const char *buf, size_t len)
1480{
1481 struct rc_dev *dev = to_rc_dev(device);
Sean Young6d741bf2017-08-07 16:20:58 -04001482 enum rc_proto protocol;
Sean Young0751d332016-12-05 17:08:35 -02001483 ssize_t rc;
1484 u64 allowed;
1485 int i;
1486
Sean Young0751d332016-12-05 17:08:35 -02001487 mutex_lock(&dev->lock);
1488
1489 allowed = dev->allowed_wakeup_protocols;
1490
1491 if (sysfs_streq(buf, "none")) {
Sean Young6d741bf2017-08-07 16:20:58 -04001492 protocol = RC_PROTO_UNKNOWN;
Sean Young0751d332016-12-05 17:08:35 -02001493 } else {
Sean Youngd57ea872017-08-09 13:19:16 -04001494 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001495 if ((allowed & (1ULL << i)) &&
Sean Youngd57ea872017-08-09 13:19:16 -04001496 sysfs_streq(buf, protocols[i].name)) {
Sean Young0751d332016-12-05 17:08:35 -02001497 protocol = i;
1498 break;
1499 }
1500 }
1501
Sean Youngd57ea872017-08-09 13:19:16 -04001502 if (i == ARRAY_SIZE(protocols)) {
Sean Young0751d332016-12-05 17:08:35 -02001503 rc = -EINVAL;
1504 goto out;
1505 }
James Hoganf423ccc2015-03-31 14:48:10 -03001506
1507 if (dev->encode_wakeup) {
1508 u64 mask = 1ULL << protocol;
1509
1510 ir_raw_load_modules(&mask);
1511 if (!mask) {
1512 rc = -EINVAL;
1513 goto out;
1514 }
1515 }
Sean Young0751d332016-12-05 17:08:35 -02001516 }
1517
1518 if (dev->wakeup_protocol != protocol) {
1519 dev->wakeup_protocol = protocol;
Sean Young1f17f682018-02-12 07:27:50 -05001520 dev_dbg(&dev->dev, "Wakeup protocol changed to %d\n", protocol);
Sean Young0751d332016-12-05 17:08:35 -02001521
Sean Young6d741bf2017-08-07 16:20:58 -04001522 if (protocol == RC_PROTO_RC6_MCE)
Sean Young0751d332016-12-05 17:08:35 -02001523 dev->scancode_wakeup_filter.data = 0x800f0000;
1524 else
1525 dev->scancode_wakeup_filter.data = 0;
1526 dev->scancode_wakeup_filter.mask = 0;
1527
1528 rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
1529 if (rc == 0)
1530 rc = len;
1531 } else {
1532 rc = len;
1533 }
1534
1535out:
1536 mutex_unlock(&dev->lock);
1537 return rc;
1538}
1539
David Härdemand8b4b582010-10-29 16:08:23 -03001540static void rc_dev_release(struct device *device)
1541{
Max Kellermann47cae1e2016-03-21 08:33:05 -03001542 struct rc_dev *dev = to_rc_dev(device);
1543
1544 kfree(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001545}
1546
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001547#define ADD_HOTPLUG_VAR(fmt, val...) \
1548 do { \
1549 int err = add_uevent_var(env, fmt, val); \
1550 if (err) \
1551 return err; \
1552 } while (0)
1553
1554static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1555{
David Härdemand8b4b582010-10-29 16:08:23 -03001556 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001557
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001558 if (dev->rc_map.name)
1559 ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
David Härdemand8b4b582010-10-29 16:08:23 -03001560 if (dev->driver_name)
1561 ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
Sean Youngb9f407e2017-09-01 11:34:23 -03001562 if (dev->device_name)
1563 ADD_HOTPLUG_VAR("DEV_NAME=%s", dev->device_name);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001564
1565 return 0;
1566}
1567
1568/*
1569 * Static device attribute struct with the sysfs attributes for IR's
1570 */
Sean Young6d75db32017-09-01 11:30:50 -03001571static struct device_attribute dev_attr_ro_protocols =
1572__ATTR(protocols, 0444, show_protocols, NULL);
1573static struct device_attribute dev_attr_rw_protocols =
1574__ATTR(protocols, 0644, show_protocols, store_protocols);
Sean Young0751d332016-12-05 17:08:35 -02001575static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
1576 store_wakeup_protocols);
James Hogan00942d12014-01-17 10:58:49 -03001577static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
1578 show_filter, store_filter, RC_FILTER_NORMAL, false);
1579static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
1580 show_filter, store_filter, RC_FILTER_NORMAL, true);
1581static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
1582 show_filter, store_filter, RC_FILTER_WAKEUP, false);
1583static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
1584 show_filter, store_filter, RC_FILTER_WAKEUP, true);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001585
Sean Young6d75db32017-09-01 11:30:50 -03001586static struct attribute *rc_dev_rw_protocol_attrs[] = {
1587 &dev_attr_rw_protocols.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001588 NULL,
1589};
1590
Sean Young6d75db32017-09-01 11:30:50 -03001591static const struct attribute_group rc_dev_rw_protocol_attr_grp = {
1592 .attrs = rc_dev_rw_protocol_attrs,
1593};
1594
1595static struct attribute *rc_dev_ro_protocol_attrs[] = {
1596 &dev_attr_ro_protocols.attr,
1597 NULL,
1598};
1599
1600static const struct attribute_group rc_dev_ro_protocol_attr_grp = {
1601 .attrs = rc_dev_ro_protocol_attrs,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001602};
1603
David Härdeman99b0f3c2014-04-04 19:06:06 -03001604static struct attribute *rc_dev_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001605 &dev_attr_filter.attr.attr,
1606 &dev_attr_filter_mask.attr.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001607 NULL,
1608};
1609
Arvind Yadavdb681022017-07-07 04:23:54 -04001610static const struct attribute_group rc_dev_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001611 .attrs = rc_dev_filter_attrs,
1612};
1613
1614static struct attribute *rc_dev_wakeup_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001615 &dev_attr_wakeup_filter.attr.attr,
1616 &dev_attr_wakeup_filter_mask.attr.attr,
Sean Young0751d332016-12-05 17:08:35 -02001617 &dev_attr_wakeup_protocols.attr,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001618 NULL,
1619};
1620
Arvind Yadavdb681022017-07-07 04:23:54 -04001621static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001622 .attrs = rc_dev_wakeup_filter_attrs,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001623};
1624
Bhumika Goyalf03f02f2017-08-19 05:22:15 -03001625static const struct device_type rc_dev_type = {
David Härdemand8b4b582010-10-29 16:08:23 -03001626 .release = rc_dev_release,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001627 .uevent = rc_dev_uevent,
1628};
1629
Andi Shyti0f7499f2016-12-16 06:50:58 -02001630struct rc_dev *rc_allocate_device(enum rc_driver_type type)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001631{
David Härdemand8b4b582010-10-29 16:08:23 -03001632 struct rc_dev *dev;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001633
David Härdemand8b4b582010-10-29 16:08:23 -03001634 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1635 if (!dev)
1636 return NULL;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001637
Andi Shytid34aee12016-12-16 04:12:15 -02001638 if (type != RC_DRIVER_IR_RAW_TX) {
1639 dev->input_dev = input_allocate_device();
1640 if (!dev->input_dev) {
1641 kfree(dev);
1642 return NULL;
1643 }
1644
1645 dev->input_dev->getkeycode = ir_getkeycode;
1646 dev->input_dev->setkeycode = ir_setkeycode;
1647 input_set_drvdata(dev->input_dev, dev);
1648
Sean Young28492252018-03-24 08:02:48 -04001649 dev->timeout = IR_DEFAULT_TIMEOUT;
Kees Cookb17ec782017-10-24 11:23:14 -04001650 timer_setup(&dev->timer_keyup, ir_timer_keyup, 0);
Sean Young57c642c2017-11-23 17:37:10 -05001651 timer_setup(&dev->timer_repeat, ir_timer_repeat, 0);
Andi Shytid34aee12016-12-16 04:12:15 -02001652
1653 spin_lock_init(&dev->rc_map.lock);
1654 spin_lock_init(&dev->keylock);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001655 }
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001656 mutex_init(&dev->lock);
David Härdemand8b4b582010-10-29 16:08:23 -03001657
1658 dev->dev.type = &rc_dev_type;
David Härdeman40fc5322013-03-06 16:52:10 -03001659 dev->dev.class = &rc_class;
David Härdemand8b4b582010-10-29 16:08:23 -03001660 device_initialize(&dev->dev);
1661
Andi Shyti0f7499f2016-12-16 06:50:58 -02001662 dev->driver_type = type;
1663
David Härdemand8b4b582010-10-29 16:08:23 -03001664 __module_get(THIS_MODULE);
1665 return dev;
1666}
1667EXPORT_SYMBOL_GPL(rc_allocate_device);
1668
1669void rc_free_device(struct rc_dev *dev)
1670{
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001671 if (!dev)
1672 return;
1673
Markus Elfring3dd94f02014-11-20 09:01:32 -03001674 input_free_device(dev->input_dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001675
1676 put_device(&dev->dev);
1677
Max Kellermann47cae1e2016-03-21 08:33:05 -03001678 /* kfree(dev) will be called by the callback function
1679 rc_dev_release() */
1680
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001681 module_put(THIS_MODULE);
David Härdemand8b4b582010-10-29 16:08:23 -03001682}
1683EXPORT_SYMBOL_GPL(rc_free_device);
1684
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001685static void devm_rc_alloc_release(struct device *dev, void *res)
1686{
1687 rc_free_device(*(struct rc_dev **)res);
1688}
1689
Andi Shyti0f7499f2016-12-16 06:50:58 -02001690struct rc_dev *devm_rc_allocate_device(struct device *dev,
1691 enum rc_driver_type type)
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001692{
1693 struct rc_dev **dr, *rc;
1694
1695 dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
1696 if (!dr)
1697 return NULL;
1698
Andi Shyti0f7499f2016-12-16 06:50:58 -02001699 rc = rc_allocate_device(type);
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001700 if (!rc) {
1701 devres_free(dr);
1702 return NULL;
1703 }
1704
1705 rc->dev.parent = dev;
1706 rc->managed_alloc = true;
1707 *dr = rc;
1708 devres_add(dev, dr);
1709
1710 return rc;
1711}
1712EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
1713
David Härdemanf56928a2017-05-03 07:04:00 -03001714static int rc_prepare_rx_device(struct rc_dev *dev)
David Härdemand8b4b582010-10-29 16:08:23 -03001715{
David Härdemanfcb13092015-05-19 19:03:17 -03001716 int rc;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001717 struct rc_map *rc_map;
Sean Young6d741bf2017-08-07 16:20:58 -04001718 u64 rc_proto;
David Härdemand8b4b582010-10-29 16:08:23 -03001719
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001720 if (!dev->map_name)
David Härdemand8b4b582010-10-29 16:08:23 -03001721 return -EINVAL;
1722
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001723 rc_map = rc_map_get(dev->map_name);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001724 if (!rc_map)
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001725 rc_map = rc_map_get(RC_MAP_EMPTY);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001726 if (!rc_map || !rc_map->scan || rc_map->size == 0)
David Härdemand8b4b582010-10-29 16:08:23 -03001727 return -EINVAL;
1728
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001729 rc = ir_setkeytable(dev, rc_map);
1730 if (rc)
1731 return rc;
1732
Sean Young6d741bf2017-08-07 16:20:58 -04001733 rc_proto = BIT_ULL(rc_map->rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001734
Sean Young831c4c82017-09-01 09:55:59 -03001735 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1736 dev->enabled_protocols = dev->allowed_protocols;
1737
Sean Younga86d6df2018-03-23 16:47:37 -04001738 if (dev->driver_type == RC_DRIVER_IR_RAW)
1739 ir_raw_load_modules(&rc_proto);
1740
Sean Young41380862017-02-22 18:48:01 -03001741 if (dev->change_protocol) {
Sean Young6d741bf2017-08-07 16:20:58 -04001742 rc = dev->change_protocol(dev, &rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001743 if (rc < 0)
1744 goto out_table;
Sean Young6d741bf2017-08-07 16:20:58 -04001745 dev->enabled_protocols = rc_proto;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001746 }
1747
Sean Young0ac5a602018-09-16 07:35:58 -04001748 /* Keyboard events */
David Härdemand8b4b582010-10-29 16:08:23 -03001749 set_bit(EV_KEY, dev->input_dev->evbit);
1750 set_bit(EV_REP, dev->input_dev->evbit);
1751 set_bit(EV_MSC, dev->input_dev->evbit);
1752 set_bit(MSC_SCAN, dev->input_dev->mscbit);
Sean Youngfec225a2018-09-18 04:50:20 -04001753 bitmap_fill(dev->input_dev->keybit, KEY_CNT);
1754
Sean Young0ac5a602018-09-16 07:35:58 -04001755 /* Pointer/mouse events */
1756 set_bit(EV_REL, dev->input_dev->evbit);
1757 set_bit(REL_X, dev->input_dev->relbit);
1758 set_bit(REL_Y, dev->input_dev->relbit);
1759
David Härdemand8b4b582010-10-29 16:08:23 -03001760 if (dev->open)
1761 dev->input_dev->open = ir_open;
1762 if (dev->close)
1763 dev->input_dev->close = ir_close;
1764
David Härdemanb2aceb72017-04-27 17:33:58 -03001765 dev->input_dev->dev.parent = &dev->dev;
1766 memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
1767 dev->input_dev->phys = dev->input_phys;
Sean Young518f4b22017-07-01 12:13:19 -04001768 dev->input_dev->name = dev->device_name;
David Härdemanb2aceb72017-04-27 17:33:58 -03001769
David Härdemanf56928a2017-05-03 07:04:00 -03001770 return 0;
1771
1772out_table:
1773 ir_free_table(&dev->rc_map);
1774
1775 return rc;
1776}
1777
1778static int rc_setup_rx_device(struct rc_dev *dev)
1779{
1780 int rc;
1781
David Härdemanb2aceb72017-04-27 17:33:58 -03001782 /* rc_open will be called here */
1783 rc = input_register_device(dev->input_dev);
1784 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001785 return rc;
David Härdemanb2aceb72017-04-27 17:33:58 -03001786
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001787 /*
1788 * Default delay of 250ms is too short for some protocols, especially
1789 * since the timeout is currently set to 250ms. Increase it to 500ms,
1790 * to avoid wrong repetition of the keycodes. Note that this must be
1791 * set after the call to input_register_device().
1792 */
Sean Young57c642c2017-11-23 17:37:10 -05001793 if (dev->allowed_protocols == RC_PROTO_BIT_CEC)
1794 dev->input_dev->rep[REP_DELAY] = 0;
1795 else
1796 dev->input_dev->rep[REP_DELAY] = 500;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001797
1798 /*
1799 * As a repeat event on protocols like RC-5 and NEC take as long as
1800 * 110/114ms, using 33ms as a repeat period is not the right thing
1801 * to do.
1802 */
1803 dev->input_dev->rep[REP_PERIOD] = 125;
1804
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001805 return 0;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001806}
1807
1808static void rc_free_rx_device(struct rc_dev *dev)
1809{
David Härdemanf56928a2017-05-03 07:04:00 -03001810 if (!dev)
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001811 return;
1812
David Härdemanf56928a2017-05-03 07:04:00 -03001813 if (dev->input_dev) {
1814 input_unregister_device(dev->input_dev);
1815 dev->input_dev = NULL;
1816 }
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001817
David Härdemanf56928a2017-05-03 07:04:00 -03001818 ir_free_table(&dev->rc_map);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001819}
1820
1821int rc_register_device(struct rc_dev *dev)
1822{
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001823 const char *path;
1824 int attr = 0;
1825 int minor;
1826 int rc;
1827
1828 if (!dev)
1829 return -EINVAL;
1830
David Härdemanfcb13092015-05-19 19:03:17 -03001831 minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
1832 if (minor < 0)
1833 return minor;
1834
1835 dev->minor = minor;
1836 dev_set_name(&dev->dev, "rc%u", dev->minor);
1837 dev_set_drvdata(&dev->dev, dev);
Mauro Carvalho Chehab587d1b02014-01-14 16:27:55 -03001838
David Härdeman99b0f3c2014-04-04 19:06:06 -03001839 dev->dev.groups = dev->sysfs_groups;
Sean Young6d75db32017-09-01 11:30:50 -03001840 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1841 dev->sysfs_groups[attr++] = &rc_dev_ro_protocol_attr_grp;
1842 else if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
1843 dev->sysfs_groups[attr++] = &rc_dev_rw_protocol_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001844 if (dev->s_filter)
David Härdeman120703f2014-04-03 20:31:30 -03001845 dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001846 if (dev->s_wakeup_filter)
1847 dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001848 dev->sysfs_groups[attr++] = NULL;
1849
Sean Younga60d64b2017-09-23 10:41:13 -04001850 if (dev->driver_type == RC_DRIVER_IR_RAW) {
David Härdemanf56928a2017-05-03 07:04:00 -03001851 rc = ir_raw_event_prepare(dev);
1852 if (rc < 0)
1853 goto out_minor;
1854 }
1855
1856 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1857 rc = rc_prepare_rx_device(dev);
1858 if (rc)
1859 goto out_raw;
1860 }
1861
David Härdemand8b4b582010-10-29 16:08:23 -03001862 rc = device_add(&dev->dev);
1863 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001864 goto out_rx_free;
David Härdemand8b4b582010-10-29 16:08:23 -03001865
David Härdemand8b4b582010-10-29 16:08:23 -03001866 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Heiner Kallweit4dc0e902015-10-29 19:39:06 -02001867 dev_info(&dev->dev, "%s as %s\n",
Sean Young518f4b22017-07-01 12:13:19 -04001868 dev->device_name ?: "Unspecified device", path ?: "N/A");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001869 kfree(path);
1870
Sean Youngd7832cd2018-05-24 05:47:17 -04001871 dev->registered = true;
1872
Sean Young5df62772017-02-23 06:11:21 -03001873 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1874 rc = rc_setup_rx_device(dev);
1875 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001876 goto out_dev;
1877 }
1878
Sean Younga60d64b2017-09-23 10:41:13 -04001879 /* Ensure that the lirc kfifo is setup before we start the thread */
Sean Young62d6f1992017-09-24 12:43:24 -04001880 if (dev->allowed_protocols != RC_PROTO_BIT_CEC) {
Sean Younga60d64b2017-09-23 10:41:13 -04001881 rc = ir_lirc_register(dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001882 if (rc < 0)
1883 goto out_rx;
David Härdemand8b4b582010-10-29 16:08:23 -03001884 }
1885
Sean Younga60d64b2017-09-23 10:41:13 -04001886 if (dev->driver_type == RC_DRIVER_IR_RAW) {
1887 rc = ir_raw_event_register(dev);
1888 if (rc < 0)
1889 goto out_lirc;
1890 }
1891
Sean Young1f17f682018-02-12 07:27:50 -05001892 dev_dbg(&dev->dev, "Registered rc%u (driver: %s)\n", dev->minor,
1893 dev->driver_name ? dev->driver_name : "unknown");
David Härdemand8b4b582010-10-29 16:08:23 -03001894
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001895 return 0;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001896
Sean Younga60d64b2017-09-23 10:41:13 -04001897out_lirc:
Sean Young62d6f1992017-09-24 12:43:24 -04001898 if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
Sean Younga60d64b2017-09-23 10:41:13 -04001899 ir_lirc_unregister(dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001900out_rx:
1901 rc_free_rx_device(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001902out_dev:
1903 device_del(&dev->dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001904out_rx_free:
1905 ir_free_table(&dev->rc_map);
1906out_raw:
1907 ir_raw_event_free(dev);
1908out_minor:
David Härdemanfcb13092015-05-19 19:03:17 -03001909 ida_simple_remove(&rc_ida, minor);
David Härdemand8b4b582010-10-29 16:08:23 -03001910 return rc;
1911}
1912EXPORT_SYMBOL_GPL(rc_register_device);
1913
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001914static void devm_rc_release(struct device *dev, void *res)
1915{
1916 rc_unregister_device(*(struct rc_dev **)res);
1917}
1918
1919int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
1920{
1921 struct rc_dev **dr;
1922 int ret;
1923
1924 dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
1925 if (!dr)
1926 return -ENOMEM;
1927
1928 ret = rc_register_device(dev);
1929 if (ret) {
1930 devres_free(dr);
1931 return ret;
1932 }
1933
1934 *dr = dev;
1935 devres_add(parent, dr);
1936
1937 return 0;
1938}
1939EXPORT_SYMBOL_GPL(devm_rc_register_device);
1940
David Härdemand8b4b582010-10-29 16:08:23 -03001941void rc_unregister_device(struct rc_dev *dev)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001942{
David Härdemand8b4b582010-10-29 16:08:23 -03001943 if (!dev)
1944 return;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001945
David Härdemand8b4b582010-10-29 16:08:23 -03001946 if (dev->driver_type == RC_DRIVER_IR_RAW)
1947 ir_raw_event_unregister(dev);
1948
Sean Young8d406882018-03-06 08:57:57 -05001949 del_timer_sync(&dev->timer_keyup);
1950 del_timer_sync(&dev->timer_repeat);
1951
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001952 rc_free_rx_device(dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001953
Sean Young7790e812017-09-26 07:31:29 -04001954 mutex_lock(&dev->lock);
1955 dev->registered = false;
1956 mutex_unlock(&dev->lock);
1957
1958 /*
1959 * lirc device should be freed with dev->registered = false, so
1960 * that userspace polling will get notified.
1961 */
Sean Young62d6f1992017-09-24 12:43:24 -04001962 if (dev->allowed_protocols != RC_PROTO_BIT_CEC)
Sean Younga60d64b2017-09-23 10:41:13 -04001963 ir_lirc_unregister(dev);
1964
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001965 device_del(&dev->dev);
1966
David Härdemanfcb13092015-05-19 19:03:17 -03001967 ida_simple_remove(&rc_ida, dev->minor);
1968
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001969 if (!dev->managed_alloc)
1970 rc_free_device(dev);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001971}
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001972
David Härdemand8b4b582010-10-29 16:08:23 -03001973EXPORT_SYMBOL_GPL(rc_unregister_device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001974
1975/*
1976 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1977 */
1978
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001979static int __init rc_core_init(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001980{
David Härdeman40fc5322013-03-06 16:52:10 -03001981 int rc = class_register(&rc_class);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001982 if (rc) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -02001983 pr_err("rc_core: unable to register rc class\n");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001984 return rc;
1985 }
1986
Sean Younga60d64b2017-09-23 10:41:13 -04001987 rc = lirc_dev_init();
1988 if (rc) {
1989 pr_err("rc_core: unable to init lirc\n");
1990 class_unregister(&rc_class);
1991 return 0;
1992 }
1993
Sean Young153a60b2013-07-30 19:00:01 -03001994 led_trigger_register_simple("rc-feedback", &led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001995 rc_map_register(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001996
1997 return 0;
1998}
1999
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03002000static void __exit rc_core_exit(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002001{
Sean Younga60d64b2017-09-23 10:41:13 -04002002 lirc_dev_exit();
David Härdeman40fc5322013-03-06 16:52:10 -03002003 class_unregister(&rc_class);
Sean Young153a60b2013-07-30 19:00:01 -03002004 led_trigger_unregister_simple(led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03002005 rc_map_unregister(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002006}
2007
David Härdemane76d4ce2013-03-06 16:52:15 -03002008subsys_initcall(rc_core_init);
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03002009module_exit(rc_core_exit);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03002010
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02002011MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehab20835282017-12-01 08:47:08 -05002012MODULE_LICENSE("GPL v2");