blob: e944d28b96d2b329a04338f86053e6c93461490f [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
David Härdemanfcb13092015-05-19 19:03:17 -030023#define RC_DEV_MAX 256
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -030024
Sean Youngd57ea872017-08-09 13:19:16 -040025static const struct {
26 const char *name;
27 unsigned int repeat_period;
28 unsigned int scancode_bits;
29} protocols[] = {
Sean Young6d741bf2017-08-07 16:20:58 -040030 [RC_PROTO_UNKNOWN] = { .name = "unknown", .repeat_period = 250 },
31 [RC_PROTO_OTHER] = { .name = "other", .repeat_period = 250 },
32 [RC_PROTO_RC5] = { .name = "rc-5",
Sean Young67f0f152017-11-19 16:57:27 -050033 .scancode_bits = 0x1f7f, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040034 [RC_PROTO_RC5X_20] = { .name = "rc-5x-20",
Sean Young67f0f152017-11-19 16:57:27 -050035 .scancode_bits = 0x1f7f3f, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040036 [RC_PROTO_RC5_SZ] = { .name = "rc-5-sz",
Sean Young67f0f152017-11-19 16:57:27 -050037 .scancode_bits = 0x2fff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040038 [RC_PROTO_JVC] = { .name = "jvc",
Sean Youngd57ea872017-08-09 13:19:16 -040039 .scancode_bits = 0xffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040040 [RC_PROTO_SONY12] = { .name = "sony-12",
Sean Young67f0f152017-11-19 16:57:27 -050041 .scancode_bits = 0x1f007f, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040042 [RC_PROTO_SONY15] = { .name = "sony-15",
Sean Young67f0f152017-11-19 16:57:27 -050043 .scancode_bits = 0xff007f, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040044 [RC_PROTO_SONY20] = { .name = "sony-20",
Sean Young67f0f152017-11-19 16:57:27 -050045 .scancode_bits = 0x1fff7f, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040046 [RC_PROTO_NEC] = { .name = "nec",
Sean Young67f0f152017-11-19 16:57:27 -050047 .scancode_bits = 0xffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040048 [RC_PROTO_NECX] = { .name = "nec-x",
Sean Young67f0f152017-11-19 16:57:27 -050049 .scancode_bits = 0xffffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040050 [RC_PROTO_NEC32] = { .name = "nec-32",
Sean Young67f0f152017-11-19 16:57:27 -050051 .scancode_bits = 0xffffffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040052 [RC_PROTO_SANYO] = { .name = "sanyo",
Sean Youngd57ea872017-08-09 13:19:16 -040053 .scancode_bits = 0x1fffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040054 [RC_PROTO_MCIR2_KBD] = { .name = "mcir2-kbd",
Sean Young67f0f152017-11-19 16:57:27 -050055 .scancode_bits = 0xffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040056 [RC_PROTO_MCIR2_MSE] = { .name = "mcir2-mse",
Sean Young67f0f152017-11-19 16:57:27 -050057 .scancode_bits = 0x1fffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040058 [RC_PROTO_RC6_0] = { .name = "rc-6-0",
Sean Young67f0f152017-11-19 16:57:27 -050059 .scancode_bits = 0xffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040060 [RC_PROTO_RC6_6A_20] = { .name = "rc-6-6a-20",
Sean Young67f0f152017-11-19 16:57:27 -050061 .scancode_bits = 0xfffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040062 [RC_PROTO_RC6_6A_24] = { .name = "rc-6-6a-24",
Sean Young67f0f152017-11-19 16:57:27 -050063 .scancode_bits = 0xffffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040064 [RC_PROTO_RC6_6A_32] = { .name = "rc-6-6a-32",
Sean Young67f0f152017-11-19 16:57:27 -050065 .scancode_bits = 0xffffffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040066 [RC_PROTO_RC6_MCE] = { .name = "rc-6-mce",
Sean Young67f0f152017-11-19 16:57:27 -050067 .scancode_bits = 0xffff7fff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040068 [RC_PROTO_SHARP] = { .name = "sharp",
Sean Youngd57ea872017-08-09 13:19:16 -040069 .scancode_bits = 0x1fff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040070 [RC_PROTO_XMP] = { .name = "xmp", .repeat_period = 250 },
71 [RC_PROTO_CEC] = { .name = "cec", .repeat_period = 550 },
Sean Youngd57ea872017-08-09 13:19:16 -040072};
David Härdemana374fef2010-04-02 15:58:29 -030073
David Härdeman4c7b3552010-11-10 11:04:19 -030074/* Used to keep track of known keymaps */
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030075static LIST_HEAD(rc_map_list);
76static DEFINE_SPINLOCK(rc_map_lock);
Sean Young153a60b2013-07-30 19:00:01 -030077static struct led_trigger *led_feedback;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030078
David Härdemanfcb13092015-05-19 19:03:17 -030079/* Used to keep track of rc devices */
80static DEFINE_IDA(rc_ida);
81
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030082static struct rc_map_list *seek_rc_map(const char *name)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030083{
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030084 struct rc_map_list *map = NULL;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030085
86 spin_lock(&rc_map_lock);
87 list_for_each_entry(map, &rc_map_list, list) {
88 if (!strcmp(name, map->map.name)) {
89 spin_unlock(&rc_map_lock);
90 return map;
91 }
92 }
93 spin_unlock(&rc_map_lock);
94
95 return NULL;
96}
97
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030098struct rc_map *rc_map_get(const char *name)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030099{
100
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300101 struct rc_map_list *map;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300102
103 map = seek_rc_map(name);
Russell King2ff56fa2015-10-15 13:15:24 -0300104#ifdef CONFIG_MODULES
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300105 if (!map) {
Kees Cook8ea54882014-03-11 17:25:53 -0300106 int rc = request_module("%s", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300107 if (rc < 0) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -0200108 pr_err("Couldn't load IR keymap %s\n", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300109 return NULL;
110 }
111 msleep(20); /* Give some time for IR to register */
112
113 map = seek_rc_map(name);
114 }
115#endif
116 if (!map) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -0200117 pr_err("IR keymap %s not found\n", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300118 return NULL;
119 }
120
121 printk(KERN_INFO "Registered IR keymap %s\n", map->map.name);
122
123 return &map->map;
124}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300125EXPORT_SYMBOL_GPL(rc_map_get);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300126
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300127int rc_map_register(struct rc_map_list *map)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300128{
129 spin_lock(&rc_map_lock);
130 list_add_tail(&map->list, &rc_map_list);
131 spin_unlock(&rc_map_lock);
132 return 0;
133}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300134EXPORT_SYMBOL_GPL(rc_map_register);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300135
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300136void rc_map_unregister(struct rc_map_list *map)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300137{
138 spin_lock(&rc_map_lock);
139 list_del(&map->list);
140 spin_unlock(&rc_map_lock);
141}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300142EXPORT_SYMBOL_GPL(rc_map_unregister);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300143
144
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300145static struct rc_map_table empty[] = {
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300146 { 0x2a, KEY_COFFEE },
147};
148
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300149static struct rc_map_list empty_map = {
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300150 .map = {
Sean Young6d741bf2017-08-07 16:20:58 -0400151 .scan = empty,
152 .size = ARRAY_SIZE(empty),
153 .rc_proto = RC_PROTO_UNKNOWN, /* Legacy IR type */
154 .name = RC_MAP_EMPTY,
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300155 }
156};
157
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300158/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700159 * ir_create_table() - initializes a scancode table
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300160 * @rc_map: the rc_map to initialize
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700161 * @name: name to assign to the table
Sean Young6d741bf2017-08-07 16:20:58 -0400162 * @rc_proto: ir type to assign to the new table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700163 * @size: initial size of the table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700164 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300165 * This routine will initialize the rc_map and will allocate
David Härdemand8b4b582010-10-29 16:08:23 -0300166 * memory to hold at least the specified number of elements.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500167 *
168 * return: zero on success or a negative error code
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700169 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300170static int ir_create_table(struct rc_map *rc_map,
Sean Young6d741bf2017-08-07 16:20:58 -0400171 const char *name, u64 rc_proto, size_t size)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700172{
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300173 rc_map->name = kstrdup(name, GFP_KERNEL);
174 if (!rc_map->name)
175 return -ENOMEM;
Sean Young6d741bf2017-08-07 16:20:58 -0400176 rc_map->rc_proto = rc_proto;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300177 rc_map->alloc = roundup_pow_of_two(size * sizeof(struct rc_map_table));
178 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300179 rc_map->scan = kmalloc(rc_map->alloc, GFP_KERNEL);
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300180 if (!rc_map->scan) {
181 kfree(rc_map->name);
182 rc_map->name = NULL;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700183 return -ENOMEM;
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300184 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700185
186 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300187 rc_map->size, rc_map->alloc);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700188 return 0;
189}
190
191/**
192 * ir_free_table() - frees memory allocated by a scancode table
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300193 * @rc_map: the table whose mappings need to be freed
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700194 *
195 * This routine will free memory alloctaed for key mappings used by given
196 * scancode table.
197 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300198static void ir_free_table(struct rc_map *rc_map)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700199{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300200 rc_map->size = 0;
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300201 kfree(rc_map->name);
Max Kellermannc183d352016-08-09 18:32:06 -0300202 rc_map->name = NULL;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300203 kfree(rc_map->scan);
204 rc_map->scan = NULL;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700205}
206
207/**
David Härdemanb3074c02010-04-02 15:58:28 -0300208 * ir_resize_table() - resizes a scancode table if necessary
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300209 * @rc_map: the rc_map to resize
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700210 * @gfp_flags: gfp flags to use when allocating memory
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300211 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300212 * This routine will shrink the rc_map if it has lots of
David Härdemanb3074c02010-04-02 15:58:28 -0300213 * unused entries and grow it if it is full.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500214 *
215 * return: zero on success or a negative error code
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300216 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300217static int ir_resize_table(struct rc_map *rc_map, gfp_t gfp_flags)
David Härdemanb3074c02010-04-02 15:58:28 -0300218{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300219 unsigned int oldalloc = rc_map->alloc;
David Härdemanb3074c02010-04-02 15:58:28 -0300220 unsigned int newalloc = oldalloc;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300221 struct rc_map_table *oldscan = rc_map->scan;
222 struct rc_map_table *newscan;
David Härdemanb3074c02010-04-02 15:58:28 -0300223
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300224 if (rc_map->size == rc_map->len) {
David Härdemanb3074c02010-04-02 15:58:28 -0300225 /* All entries in use -> grow keytable */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300226 if (rc_map->alloc >= IR_TAB_MAX_SIZE)
David Härdemanb3074c02010-04-02 15:58:28 -0300227 return -ENOMEM;
228
229 newalloc *= 2;
230 IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
231 }
232
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300233 if ((rc_map->len * 3 < rc_map->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
David Härdemanb3074c02010-04-02 15:58:28 -0300234 /* Less than 1/3 of entries in use -> shrink keytable */
235 newalloc /= 2;
236 IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
237 }
238
239 if (newalloc == oldalloc)
240 return 0;
241
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700242 newscan = kmalloc(newalloc, gfp_flags);
David Härdemanb3074c02010-04-02 15:58:28 -0300243 if (!newscan) {
244 IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
245 return -ENOMEM;
246 }
247
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300248 memcpy(newscan, rc_map->scan, rc_map->len * sizeof(struct rc_map_table));
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300249 rc_map->scan = newscan;
250 rc_map->alloc = newalloc;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300251 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
David Härdemanb3074c02010-04-02 15:58:28 -0300252 kfree(oldscan);
253 return 0;
254}
255
256/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700257 * ir_update_mapping() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300258 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300259 * @rc_map: scancode table to be adjusted
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700260 * @index: index of the mapping that needs to be updated
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500261 * @new_keycode: the desired keycode
David Härdemanb3074c02010-04-02 15:58:28 -0300262 *
David Härdemand8b4b582010-10-29 16:08:23 -0300263 * This routine is used to update scancode->keycode mapping at given
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700264 * position.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500265 *
266 * return: previous keycode assigned to the mapping
267 *
David Härdemanb3074c02010-04-02 15:58:28 -0300268 */
David Härdemand8b4b582010-10-29 16:08:23 -0300269static unsigned int ir_update_mapping(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300270 struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700271 unsigned int index,
272 unsigned int new_keycode)
273{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300274 int old_keycode = rc_map->scan[index].keycode;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700275 int i;
276
277 /* Did the user wish to remove the mapping? */
278 if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
279 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300280 index, rc_map->scan[index].scancode);
281 rc_map->len--;
282 memmove(&rc_map->scan[index], &rc_map->scan[index+ 1],
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300283 (rc_map->len - index) * sizeof(struct rc_map_table));
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700284 } else {
285 IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n",
286 index,
287 old_keycode == KEY_RESERVED ? "New" : "Replacing",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300288 rc_map->scan[index].scancode, new_keycode);
289 rc_map->scan[index].keycode = new_keycode;
David Härdemand8b4b582010-10-29 16:08:23 -0300290 __set_bit(new_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700291 }
292
293 if (old_keycode != KEY_RESERVED) {
294 /* A previous mapping was updated... */
David Härdemand8b4b582010-10-29 16:08:23 -0300295 __clear_bit(old_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700296 /* ... but another scancode might use the same keycode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300297 for (i = 0; i < rc_map->len; i++) {
298 if (rc_map->scan[i].keycode == old_keycode) {
David Härdemand8b4b582010-10-29 16:08:23 -0300299 __set_bit(old_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700300 break;
301 }
302 }
303
304 /* Possibly shrink the keytable, failure is not a problem */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300305 ir_resize_table(rc_map, GFP_ATOMIC);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700306 }
307
308 return old_keycode;
309}
310
311/**
David Härdeman4c7b3552010-11-10 11:04:19 -0300312 * ir_establish_scancode() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300313 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300314 * @rc_map: scancode table to be searched
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700315 * @scancode: the desired scancode
316 * @resize: controls whether we allowed to resize the table to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300317 * accommodate not yet present scancodes
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700318 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300319 * This routine is used to locate given scancode in rc_map.
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700320 * If scancode is not yet present the routine will allocate a new slot
321 * for it.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500322 *
323 * return: index of the mapping containing scancode in question
324 * or -1U in case of failure.
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700325 */
David Härdemand8b4b582010-10-29 16:08:23 -0300326static unsigned int ir_establish_scancode(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300327 struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700328 unsigned int scancode,
329 bool resize)
David Härdemanb3074c02010-04-02 15:58:28 -0300330{
331 unsigned int i;
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300332
333 /*
334 * Unfortunately, some hardware-based IR decoders don't provide
335 * all bits for the complete IR code. In general, they provide only
336 * the command part of the IR code. Yet, as it is possible to replace
337 * the provided IR with another one, it is needed to allow loading
David Härdemand8b4b582010-10-29 16:08:23 -0300338 * IR tables from other remotes. So, we support specifying a mask to
339 * indicate the valid bits of the scancodes.
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300340 */
David Härdeman9d2f1d32014-04-03 20:32:26 -0300341 if (dev->scancode_mask)
342 scancode &= dev->scancode_mask;
David Härdemanb3074c02010-04-02 15:58:28 -0300343
344 /* First check if we already have a mapping for this ir command */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300345 for (i = 0; i < rc_map->len; i++) {
346 if (rc_map->scan[i].scancode == scancode)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700347 return i;
348
David Härdemanb3074c02010-04-02 15:58:28 -0300349 /* Keytable is sorted from lowest to highest scancode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300350 if (rc_map->scan[i].scancode >= scancode)
David Härdemanb3074c02010-04-02 15:58:28 -0300351 break;
David Härdemanb3074c02010-04-02 15:58:28 -0300352 }
353
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700354 /* No previous mapping found, we might need to grow the table */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300355 if (rc_map->size == rc_map->len) {
356 if (!resize || ir_resize_table(rc_map, GFP_ATOMIC))
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700357 return -1U;
358 }
David Härdemanb3074c02010-04-02 15:58:28 -0300359
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700360 /* i is the proper index to insert our new keycode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300361 if (i < rc_map->len)
362 memmove(&rc_map->scan[i + 1], &rc_map->scan[i],
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300363 (rc_map->len - i) * sizeof(struct rc_map_table));
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300364 rc_map->scan[i].scancode = scancode;
365 rc_map->scan[i].keycode = KEY_RESERVED;
366 rc_map->len++;
David Härdemanb3074c02010-04-02 15:58:28 -0300367
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700368 return i;
David Härdemanb3074c02010-04-02 15:58:28 -0300369}
370
371/**
372 * ir_setkeycode() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300373 * @idev: the struct input_dev device descriptor
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500374 * @ke: Input keymap entry
375 * @old_keycode: result
David Härdemanb3074c02010-04-02 15:58:28 -0300376 *
377 * This routine is used to handle evdev EVIOCSKEY ioctl.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500378 *
379 * return: -EINVAL if the keycode could not be inserted, otherwise zero.
David Härdemanb3074c02010-04-02 15:58:28 -0300380 */
David Härdemand8b4b582010-10-29 16:08:23 -0300381static int ir_setkeycode(struct input_dev *idev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700382 const struct input_keymap_entry *ke,
383 unsigned int *old_keycode)
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300384{
David Härdemand8b4b582010-10-29 16:08:23 -0300385 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300386 struct rc_map *rc_map = &rdev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700387 unsigned int index;
388 unsigned int scancode;
Mauro Carvalho Chehabdea8a392010-11-29 07:46:13 -0300389 int retval = 0;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700390 unsigned long flags;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300391
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300392 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700393
394 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
395 index = ke->index;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300396 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700397 retval = -EINVAL;
398 goto out;
399 }
400 } else {
401 retval = input_scancode_to_scalar(ke, &scancode);
402 if (retval)
403 goto out;
404
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300405 index = ir_establish_scancode(rdev, rc_map, scancode, true);
406 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700407 retval = -ENOMEM;
408 goto out;
409 }
410 }
411
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300412 *old_keycode = ir_update_mapping(rdev, rc_map, index, ke->keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700413
414out:
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300415 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700416 return retval;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300417}
418
419/**
David Härdemanb3074c02010-04-02 15:58:28 -0300420 * ir_setkeytable() - sets several entries in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300421 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300422 * @from: the struct rc_map to copy entries from
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300423 *
David Härdemanb3074c02010-04-02 15:58:28 -0300424 * This routine is used to handle table initialization.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500425 *
426 * return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300427 */
David Härdemand8b4b582010-10-29 16:08:23 -0300428static int ir_setkeytable(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300429 const struct rc_map *from)
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300430{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300431 struct rc_map *rc_map = &dev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700432 unsigned int i, index;
433 int rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300434
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300435 rc = ir_create_table(rc_map, from->name,
Sean Young6d741bf2017-08-07 16:20:58 -0400436 from->rc_proto, from->size);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700437 if (rc)
438 return rc;
439
David Härdemanb3074c02010-04-02 15:58:28 -0300440 for (i = 0; i < from->size; i++) {
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300441 index = ir_establish_scancode(dev, rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700442 from->scan[i].scancode, false);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300443 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700444 rc = -ENOMEM;
David Härdemanb3074c02010-04-02 15:58:28 -0300445 break;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700446 }
447
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300448 ir_update_mapping(dev, rc_map, index,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700449 from->scan[i].keycode);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300450 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700451
452 if (rc)
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300453 ir_free_table(rc_map);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700454
David Härdemanb3074c02010-04-02 15:58:28 -0300455 return rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300456}
457
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300458static int rc_map_cmp(const void *key, const void *elt)
459{
460 const unsigned int *scancode = key;
461 const struct rc_map_table *e = elt;
462
463 if (*scancode < e->scancode)
464 return -1;
465 else if (*scancode > e->scancode)
466 return 1;
467 return 0;
468}
469
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300470/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700471 * ir_lookup_by_scancode() - locate mapping by scancode
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300472 * @rc_map: the struct rc_map to search
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700473 * @scancode: scancode to look for in the table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700474 *
475 * This routine performs binary search in RC keykeymap table for
476 * given scancode.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500477 *
478 * return: index in the table, -1U if not found
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700479 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300480static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700481 unsigned int scancode)
482{
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300483 struct rc_map_table *res;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700484
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300485 res = bsearch(&scancode, rc_map->scan, rc_map->len,
486 sizeof(struct rc_map_table), rc_map_cmp);
487 if (!res)
488 return -1U;
489 else
490 return res - rc_map->scan;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700491}
492
493/**
David Härdemanb3074c02010-04-02 15:58:28 -0300494 * ir_getkeycode() - get a keycode from the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300495 * @idev: the struct input_dev device descriptor
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500496 * @ke: Input keymap entry
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300497 *
498 * This routine is used to handle evdev EVIOCGKEY ioctl.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500499 *
500 * return: always returns zero.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300501 */
David Härdemand8b4b582010-10-29 16:08:23 -0300502static int ir_getkeycode(struct input_dev *idev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700503 struct input_keymap_entry *ke)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300504{
David Härdemand8b4b582010-10-29 16:08:23 -0300505 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300506 struct rc_map *rc_map = &rdev->rc_map;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300507 struct rc_map_table *entry;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700508 unsigned long flags;
509 unsigned int index;
510 unsigned int scancode;
511 int retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300512
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300513 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700514
515 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
516 index = ke->index;
517 } else {
518 retval = input_scancode_to_scalar(ke, &scancode);
519 if (retval)
520 goto out;
521
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300522 index = ir_lookup_by_scancode(rc_map, scancode);
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300523 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700524
Dmitry Torokhov54e74b82011-01-28 23:33:29 -0800525 if (index < rc_map->len) {
526 entry = &rc_map->scan[index];
527
528 ke->index = index;
529 ke->keycode = entry->keycode;
530 ke->len = sizeof(entry->scancode);
531 memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
532
533 } else if (!(ke->flags & INPUT_KEYMAP_BY_INDEX)) {
534 /*
535 * We do not really know the valid range of scancodes
536 * so let's respond with KEY_RESERVED to anything we
537 * do not have mapping for [yet].
538 */
539 ke->index = index;
540 ke->keycode = KEY_RESERVED;
541 } else {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700542 retval = -EINVAL;
543 goto out;
544 }
545
Dmitry Torokhov47c5ba52010-10-31 15:18:42 -0700546 retval = 0;
547
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700548out:
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300549 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700550 return retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300551}
552
553/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300554 * rc_g_keycode_from_table() - gets the keycode that corresponds to a scancode
David Härdemand8b4b582010-10-29 16:08:23 -0300555 * @dev: the struct rc_dev descriptor of the device
556 * @scancode: the scancode to look for
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300557 *
David Härdemand8b4b582010-10-29 16:08:23 -0300558 * This routine is used by drivers which need to convert a scancode to a
559 * keycode. Normally it should not be used since drivers should have no
560 * interest in keycodes.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500561 *
562 * return: the corresponding keycode, or KEY_RESERVED
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300563 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300564u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300565{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300566 struct rc_map *rc_map = &dev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700567 unsigned int keycode;
568 unsigned int index;
569 unsigned long flags;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300570
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300571 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700572
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300573 index = ir_lookup_by_scancode(rc_map, scancode);
574 keycode = index < rc_map->len ?
575 rc_map->scan[index].keycode : KEY_RESERVED;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700576
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300577 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700578
Mauro Carvalho Chehab35438942010-04-03 16:53:16 -0300579 if (keycode != KEY_RESERVED)
580 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
Sean Young518f4b22017-07-01 12:13:19 -0400581 dev->device_name, scancode, keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700582
David Härdemanb3074c02010-04-02 15:58:28 -0300583 return keycode;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300584}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300585EXPORT_SYMBOL_GPL(rc_g_keycode_from_table);
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300586
587/**
David Härdeman62c65032010-10-29 16:08:07 -0300588 * ir_do_keyup() - internal function to signal the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300589 * @dev: the struct rc_dev descriptor of the device
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300590 * @sync: whether or not to call input_sync
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300591 *
David Härdeman62c65032010-10-29 16:08:07 -0300592 * This function is used internally to release a keypress, it must be
593 * called with keylock held.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300594 */
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300595static void ir_do_keyup(struct rc_dev *dev, bool sync)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300596{
David Härdemand8b4b582010-10-29 16:08:23 -0300597 if (!dev->keypressed)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300598 return;
599
David Härdemand8b4b582010-10-29 16:08:23 -0300600 IR_dprintk(1, "keyup key 0x%04x\n", dev->last_keycode);
601 input_report_key(dev->input_dev, dev->last_keycode, 0);
Sean Young153a60b2013-07-30 19:00:01 -0300602 led_trigger_event(led_feedback, LED_OFF);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300603 if (sync)
604 input_sync(dev->input_dev);
David Härdemand8b4b582010-10-29 16:08:23 -0300605 dev->keypressed = false;
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300606}
David Härdeman62c65032010-10-29 16:08:07 -0300607
608/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300609 * rc_keyup() - signals the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300610 * @dev: the struct rc_dev descriptor of the device
David Härdeman62c65032010-10-29 16:08:07 -0300611 *
612 * This routine is used to signal that a key has been released on the
613 * remote control.
614 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300615void rc_keyup(struct rc_dev *dev)
David Härdeman62c65032010-10-29 16:08:07 -0300616{
617 unsigned long flags;
David Härdeman62c65032010-10-29 16:08:07 -0300618
David Härdemand8b4b582010-10-29 16:08:23 -0300619 spin_lock_irqsave(&dev->keylock, flags);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300620 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300621 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300622}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300623EXPORT_SYMBOL_GPL(rc_keyup);
David Härdemana374fef2010-04-02 15:58:29 -0300624
625/**
626 * ir_timer_keyup() - generates a keyup event after a timeout
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500627 *
628 * @t: a pointer to the struct timer_list
David Härdemana374fef2010-04-02 15:58:29 -0300629 *
630 * This routine will generate a keyup event some time after a keydown event
631 * is generated when no further activity has been detected.
632 */
Kees Cookb17ec782017-10-24 11:23:14 -0400633static void ir_timer_keyup(struct timer_list *t)
David Härdemana374fef2010-04-02 15:58:29 -0300634{
Kees Cookb17ec782017-10-24 11:23:14 -0400635 struct rc_dev *dev = from_timer(dev, t, timer_keyup);
David Härdemana374fef2010-04-02 15:58:29 -0300636 unsigned long flags;
637
638 /*
639 * ir->keyup_jiffies is used to prevent a race condition if a
640 * hardware interrupt occurs at this point and the keyup timer
641 * event is moved further into the future as a result.
642 *
643 * The timer will then be reactivated and this function called
644 * again in the future. We need to exit gracefully in that case
645 * to allow the input subsystem to do its auto-repeat magic or
646 * a keyup event might follow immediately after the keydown.
647 */
David Härdemand8b4b582010-10-29 16:08:23 -0300648 spin_lock_irqsave(&dev->keylock, flags);
649 if (time_is_before_eq_jiffies(dev->keyup_jiffies))
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300650 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300651 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300652}
653
654/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300655 * rc_repeat() - signals that a key is still pressed
David Härdemand8b4b582010-10-29 16:08:23 -0300656 * @dev: the struct rc_dev descriptor of the device
David Härdemana374fef2010-04-02 15:58:29 -0300657 *
658 * This routine is used by IR decoders when a repeat message which does
659 * not include the necessary bits to reproduce the scancode has been
660 * received.
661 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300662void rc_repeat(struct rc_dev *dev)
David Härdemana374fef2010-04-02 15:58:29 -0300663{
664 unsigned long flags;
Sean Youngd57ea872017-08-09 13:19:16 -0400665 unsigned int timeout = protocols[dev->last_protocol].repeat_period;
David Härdemana374fef2010-04-02 15:58:29 -0300666
David Härdemand8b4b582010-10-29 16:08:23 -0300667 spin_lock_irqsave(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300668
David Härdemand8b4b582010-10-29 16:08:23 -0300669 if (!dev->keypressed)
David Härdemana374fef2010-04-02 15:58:29 -0300670 goto out;
671
David Härdeman265a2982017-06-22 15:23:54 -0400672 input_event(dev->input_dev, EV_MSC, MSC_SCAN, dev->last_scancode);
673 input_sync(dev->input_dev);
674
Sean Youngd57ea872017-08-09 13:19:16 -0400675 dev->keyup_jiffies = jiffies + msecs_to_jiffies(timeout);
David Härdemand8b4b582010-10-29 16:08:23 -0300676 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
David Härdemana374fef2010-04-02 15:58:29 -0300677
678out:
David Härdemand8b4b582010-10-29 16:08:23 -0300679 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300680}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300681EXPORT_SYMBOL_GPL(rc_repeat);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300682
683/**
David Härdeman62c65032010-10-29 16:08:07 -0300684 * ir_do_keydown() - internal function to process a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300685 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300686 * @protocol: the protocol of the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300687 * @scancode: the scancode of the keypress
688 * @keycode: the keycode of the keypress
689 * @toggle: the toggle value of the keypress
690 *
691 * This function is used internally to register a keypress, it must be
692 * called with keylock held.
693 */
Sean Young6d741bf2017-08-07 16:20:58 -0400694static void ir_do_keydown(struct rc_dev *dev, enum rc_proto protocol,
David Härdeman120703f2014-04-03 20:31:30 -0300695 u32 scancode, u32 keycode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300696{
David Härdeman99b0f3c2014-04-04 19:06:06 -0300697 bool new_event = (!dev->keypressed ||
David Härdeman120703f2014-04-03 20:31:30 -0300698 dev->last_protocol != protocol ||
David Härdeman99b0f3c2014-04-04 19:06:06 -0300699 dev->last_scancode != scancode ||
David Härdeman120703f2014-04-03 20:31:30 -0300700 dev->last_toggle != toggle);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300701
702 if (new_event && dev->keypressed)
703 ir_do_keyup(dev, false);
704
David Härdemand8b4b582010-10-29 16:08:23 -0300705 input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300706
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300707 if (new_event && keycode != KEY_RESERVED) {
708 /* Register a keypress */
709 dev->keypressed = true;
David Härdeman120703f2014-04-03 20:31:30 -0300710 dev->last_protocol = protocol;
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300711 dev->last_scancode = scancode;
712 dev->last_toggle = toggle;
713 dev->last_keycode = keycode;
David Härdeman62c65032010-10-29 16:08:07 -0300714
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200715 IR_dprintk(1, "%s: key down event, key 0x%04x, protocol 0x%04x, scancode 0x%08x\n",
Sean Young518f4b22017-07-01 12:13:19 -0400716 dev->device_name, keycode, protocol, scancode);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300717 input_report_key(dev->input_dev, keycode, 1);
James Hogan70a2f912014-01-16 19:56:22 -0300718
719 led_trigger_event(led_feedback, LED_FULL);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300720 }
David Härdeman62c65032010-10-29 16:08:07 -0300721
David Härdemand8b4b582010-10-29 16:08:23 -0300722 input_sync(dev->input_dev);
David Härdeman62c65032010-10-29 16:08:07 -0300723}
724
725/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300726 * rc_keydown() - generates input event for a key press
David Härdemand8b4b582010-10-29 16:08:23 -0300727 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300728 * @protocol: the protocol for the keypress
729 * @scancode: the scancode for the keypress
David Härdemana374fef2010-04-02 15:58:29 -0300730 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
731 * support toggle values, this should be set to zero)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300732 *
David Härdemand8b4b582010-10-29 16:08:23 -0300733 * This routine is used to signal that a key has been pressed on the
734 * remote control.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300735 */
Sean Young6d741bf2017-08-07 16:20:58 -0400736void rc_keydown(struct rc_dev *dev, enum rc_proto protocol, u32 scancode,
737 u8 toggle)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300738{
David Härdemana374fef2010-04-02 15:58:29 -0300739 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300740 u32 keycode = rc_g_keycode_from_table(dev, scancode);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300741
David Härdemand8b4b582010-10-29 16:08:23 -0300742 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300743 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300744
David Härdemand8b4b582010-10-29 16:08:23 -0300745 if (dev->keypressed) {
Sean Youngd57ea872017-08-09 13:19:16 -0400746 dev->keyup_jiffies = jiffies +
747 msecs_to_jiffies(protocols[protocol].repeat_period);
David Härdemand8b4b582010-10-29 16:08:23 -0300748 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
David Härdeman62c65032010-10-29 16:08:07 -0300749 }
David Härdemand8b4b582010-10-29 16:08:23 -0300750 spin_unlock_irqrestore(&dev->keylock, flags);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300751}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300752EXPORT_SYMBOL_GPL(rc_keydown);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300753
David Härdeman62c65032010-10-29 16:08:07 -0300754/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300755 * rc_keydown_notimeout() - generates input event for a key press without
David Härdeman62c65032010-10-29 16:08:07 -0300756 * an automatic keyup event at a later time
David Härdemand8b4b582010-10-29 16:08:23 -0300757 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300758 * @protocol: the protocol for the keypress
759 * @scancode: the scancode for the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300760 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
761 * support toggle values, this should be set to zero)
762 *
David Härdemand8b4b582010-10-29 16:08:23 -0300763 * This routine is used to signal that a key has been pressed on the
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300764 * remote control. The driver must manually call rc_keyup() at a later stage.
David Härdeman62c65032010-10-29 16:08:07 -0300765 */
Sean Young6d741bf2017-08-07 16:20:58 -0400766void rc_keydown_notimeout(struct rc_dev *dev, enum rc_proto protocol,
David Härdeman120703f2014-04-03 20:31:30 -0300767 u32 scancode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300768{
769 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300770 u32 keycode = rc_g_keycode_from_table(dev, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300771
David Härdemand8b4b582010-10-29 16:08:23 -0300772 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300773 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
David Härdemand8b4b582010-10-29 16:08:23 -0300774 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300775}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300776EXPORT_SYMBOL_GPL(rc_keydown_notimeout);
David Härdeman62c65032010-10-29 16:08:07 -0300777
Sean Youngb590c0b2016-12-05 19:24:59 -0200778/**
Sean Young6b514c42017-10-09 16:32:41 -0400779 * rc_validate_scancode() - checks that a scancode is valid for a protocol.
780 * For nec, it should do the opposite of ir_nec_bytes_to_scancode()
Sean Young49a4b362017-09-27 16:00:49 -0400781 * @proto: protocol
782 * @scancode: scancode
783 */
784bool rc_validate_scancode(enum rc_proto proto, u32 scancode)
785{
786 switch (proto) {
Sean Young6b514c42017-10-09 16:32:41 -0400787 /*
788 * NECX has a 16-bit address; if the lower 8 bits match the upper
789 * 8 bits inverted, then the address would match regular nec.
790 */
Sean Young49a4b362017-09-27 16:00:49 -0400791 case RC_PROTO_NECX:
792 if ((((scancode >> 16) ^ ~(scancode >> 8)) & 0xff) == 0)
793 return false;
794 break;
Sean Young6b514c42017-10-09 16:32:41 -0400795 /*
796 * NEC32 has a 16 bit address and 16 bit command. If the lower 8 bits
797 * of the command match the upper 8 bits inverted, then it would
798 * be either NEC or NECX.
799 */
Sean Young49a4b362017-09-27 16:00:49 -0400800 case RC_PROTO_NEC32:
Sean Young6b514c42017-10-09 16:32:41 -0400801 if ((((scancode >> 8) ^ ~scancode) & 0xff) == 0)
Sean Young49a4b362017-09-27 16:00:49 -0400802 return false;
803 break;
Sean Young6b514c42017-10-09 16:32:41 -0400804 /*
805 * If the customer code (top 32-bit) is 0x800f, it is MCE else it
806 * is regular mode-6a 32 bit
807 */
Sean Young49a4b362017-09-27 16:00:49 -0400808 case RC_PROTO_RC6_MCE:
809 if ((scancode & 0xffff0000) != 0x800f0000)
810 return false;
811 break;
812 case RC_PROTO_RC6_6A_32:
813 if ((scancode & 0xffff0000) == 0x800f0000)
814 return false;
815 break;
816 default:
817 break;
818 }
819
820 return true;
821}
822
823/**
Sean Youngb590c0b2016-12-05 19:24:59 -0200824 * rc_validate_filter() - checks that the scancode and mask are valid and
825 * provides sensible defaults
James Hoganf423ccc2015-03-31 14:48:10 -0300826 * @dev: the struct rc_dev descriptor of the device
Sean Youngb590c0b2016-12-05 19:24:59 -0200827 * @filter: the scancode and mask
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500828 *
829 * return: 0 or -EINVAL if the filter is not valid
Sean Youngb590c0b2016-12-05 19:24:59 -0200830 */
James Hoganf423ccc2015-03-31 14:48:10 -0300831static int rc_validate_filter(struct rc_dev *dev,
Sean Youngb590c0b2016-12-05 19:24:59 -0200832 struct rc_scancode_filter *filter)
833{
Sean Youngd57ea872017-08-09 13:19:16 -0400834 u32 mask, s = filter->data;
Sean Young6d741bf2017-08-07 16:20:58 -0400835 enum rc_proto protocol = dev->wakeup_protocol;
Sean Youngb590c0b2016-12-05 19:24:59 -0200836
Sean Youngd57ea872017-08-09 13:19:16 -0400837 if (protocol >= ARRAY_SIZE(protocols))
Sean Young2168b412017-08-07 09:21:29 -0400838 return -EINVAL;
839
Sean Youngd57ea872017-08-09 13:19:16 -0400840 mask = protocols[protocol].scancode_bits;
841
Sean Young49a4b362017-09-27 16:00:49 -0400842 if (!rc_validate_scancode(protocol, s))
843 return -EINVAL;
Sean Youngb590c0b2016-12-05 19:24:59 -0200844
Sean Youngd57ea872017-08-09 13:19:16 -0400845 filter->data &= mask;
846 filter->mask &= mask;
Sean Youngb590c0b2016-12-05 19:24:59 -0200847
James Hoganf423ccc2015-03-31 14:48:10 -0300848 /*
849 * If we have to raw encode the IR for wakeup, we cannot have a mask
850 */
Sean Youngd57ea872017-08-09 13:19:16 -0400851 if (dev->encode_wakeup && filter->mask != 0 && filter->mask != mask)
James Hoganf423ccc2015-03-31 14:48:10 -0300852 return -EINVAL;
853
Sean Youngb590c0b2016-12-05 19:24:59 -0200854 return 0;
855}
856
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300857int rc_open(struct rc_dev *rdev)
858{
859 int rval = 0;
860
861 if (!rdev)
862 return -EINVAL;
863
864 mutex_lock(&rdev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -0200865
Juergen Lockf02dcdd2013-08-16 15:00:24 -0300866 if (!rdev->users++ && rdev->open != NULL)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300867 rval = rdev->open(rdev);
868
869 if (rval)
870 rdev->users--;
871
872 mutex_unlock(&rdev->lock);
873
874 return rval;
875}
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300876
David Härdemand8b4b582010-10-29 16:08:23 -0300877static int ir_open(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300878{
David Härdemand8b4b582010-10-29 16:08:23 -0300879 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300880
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300881 return rc_open(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300882}
883
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300884void rc_close(struct rc_dev *rdev)
885{
886 if (rdev) {
887 mutex_lock(&rdev->lock);
888
Mauro Carvalho Chehab81b7d142015-04-28 09:43:17 -0300889 if (!--rdev->users && rdev->close != NULL)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300890 rdev->close(rdev);
891
892 mutex_unlock(&rdev->lock);
893 }
894}
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300895
David Härdemand8b4b582010-10-29 16:08:23 -0300896static void ir_close(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300897{
David Härdemand8b4b582010-10-29 16:08:23 -0300898 struct rc_dev *rdev = input_get_drvdata(idev);
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300899 rc_close(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300900}
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300901
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300902/* class for /sys/class/rc */
David Härdeman40fc5322013-03-06 16:52:10 -0300903static char *rc_devnode(struct device *dev, umode_t *mode)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300904{
905 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
906}
907
David Härdeman40fc5322013-03-06 16:52:10 -0300908static struct class rc_class = {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300909 .name = "rc",
David Härdeman40fc5322013-03-06 16:52:10 -0300910 .devnode = rc_devnode,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300911};
912
David Härdemanc003ab12012-10-11 19:11:54 -0300913/*
914 * These are the protocol textual descriptions that are
915 * used by the sysfs protocols file. Note that the order
916 * of the entries is relevant.
917 */
Heiner Kallweit53df8772015-11-16 17:52:17 -0200918static const struct {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300919 u64 type;
Heiner Kallweit53df8772015-11-16 17:52:17 -0200920 const char *name;
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200921 const char *module_name;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300922} proto_names[] = {
Sean Young6d741bf2017-08-07 16:20:58 -0400923 { RC_PROTO_BIT_NONE, "none", NULL },
924 { RC_PROTO_BIT_OTHER, "other", NULL },
925 { RC_PROTO_BIT_UNKNOWN, "unknown", NULL },
926 { RC_PROTO_BIT_RC5 |
927 RC_PROTO_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
928 { RC_PROTO_BIT_NEC |
929 RC_PROTO_BIT_NECX |
930 RC_PROTO_BIT_NEC32, "nec", "ir-nec-decoder" },
931 { RC_PROTO_BIT_RC6_0 |
932 RC_PROTO_BIT_RC6_6A_20 |
933 RC_PROTO_BIT_RC6_6A_24 |
934 RC_PROTO_BIT_RC6_6A_32 |
935 RC_PROTO_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
936 { RC_PROTO_BIT_JVC, "jvc", "ir-jvc-decoder" },
937 { RC_PROTO_BIT_SONY12 |
938 RC_PROTO_BIT_SONY15 |
939 RC_PROTO_BIT_SONY20, "sony", "ir-sony-decoder" },
940 { RC_PROTO_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
941 { RC_PROTO_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
942 { RC_PROTO_BIT_SHARP, "sharp", "ir-sharp-decoder" },
943 { RC_PROTO_BIT_MCIR2_KBD |
944 RC_PROTO_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" },
945 { RC_PROTO_BIT_XMP, "xmp", "ir-xmp-decoder" },
946 { RC_PROTO_BIT_CEC, "cec", NULL },
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300947};
948
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300949/**
James Hoganab88c662014-02-28 20:17:05 -0300950 * struct rc_filter_attribute - Device attribute relating to a filter type.
951 * @attr: Device attribute.
952 * @type: Filter type.
953 * @mask: false for filter value, true for filter mask.
954 */
955struct rc_filter_attribute {
956 struct device_attribute attr;
957 enum rc_filter_type type;
958 bool mask;
959};
960#define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
961
James Hoganab88c662014-02-28 20:17:05 -0300962#define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
963 struct rc_filter_attribute dev_attr_##_name = { \
964 .attr = __ATTR(_name, _mode, _show, _store), \
965 .type = (_type), \
966 .mask = (_mask), \
967 }
968
969/**
Sean Young0751d332016-12-05 17:08:35 -0200970 * show_protocols() - shows the current IR protocol(s)
David Härdemand8b4b582010-10-29 16:08:23 -0300971 * @device: the device descriptor
David Härdemanda6e1622014-04-03 20:32:16 -0300972 * @mattr: the device attribute struct
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300973 * @buf: a pointer to the output buffer
974 *
975 * This routine is a callback routine for input read the IR protocol type(s).
Sean Young0751d332016-12-05 17:08:35 -0200976 * it is trigged by reading /sys/class/rc/rc?/protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300977 * It returns the protocol names of supported protocols.
978 * Enabled protocols are printed in brackets.
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300979 *
David Härdeman18726a32017-04-27 17:34:08 -0300980 * dev->lock is taken to guard against races between
981 * store_protocols and show_protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300982 */
David Härdemand8b4b582010-10-29 16:08:23 -0300983static ssize_t show_protocols(struct device *device,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300984 struct device_attribute *mattr, char *buf)
985{
David Härdemand8b4b582010-10-29 16:08:23 -0300986 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300987 u64 allowed, enabled;
988 char *tmp = buf;
989 int i;
990
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300991 mutex_lock(&dev->lock);
992
Sean Young0751d332016-12-05 17:08:35 -0200993 enabled = dev->enabled_protocols;
994 allowed = dev->allowed_protocols;
995 if (dev->raw && !allowed)
996 allowed = ir_raw_get_allowed_protocols();
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300997
David Härdemanda6e1622014-04-03 20:32:16 -0300998 mutex_unlock(&dev->lock);
999
1000 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
1001 __func__, (long long)allowed, (long long)enabled);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001002
1003 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1004 if (allowed & enabled & proto_names[i].type)
1005 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
1006 else if (allowed & proto_names[i].type)
1007 tmp += sprintf(tmp, "%s ", proto_names[i].name);
David Härdemanc003ab12012-10-11 19:11:54 -03001008
1009 if (allowed & proto_names[i].type)
1010 allowed &= ~proto_names[i].type;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001011 }
1012
Sean Younga60d64b2017-09-23 10:41:13 -04001013#ifdef CONFIG_LIRC
1014 if (dev->driver_type == RC_DRIVER_IR_RAW)
David Härdeman275ddb42015-05-19 19:03:22 -03001015 tmp += sprintf(tmp, "[lirc] ");
Sean Younga60d64b2017-09-23 10:41:13 -04001016#endif
David Härdeman275ddb42015-05-19 19:03:22 -03001017
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001018 if (tmp != buf)
1019 tmp--;
1020 *tmp = '\n';
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001021
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001022 return tmp + 1 - buf;
1023}
1024
1025/**
David Härdemanda6e1622014-04-03 20:32:16 -03001026 * parse_protocol_change() - parses a protocol change request
1027 * @protocols: pointer to the bitmask of current protocols
1028 * @buf: pointer to the buffer with a list of changes
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001029 *
David Härdemanda6e1622014-04-03 20:32:16 -03001030 * Writing "+proto" will add a protocol to the protocol mask.
1031 * Writing "-proto" will remove a protocol from protocol mask.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001032 * Writing "proto" will enable only "proto".
1033 * Writing "none" will disable all protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001034 * Returns the number of changes performed or a negative error code.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001035 */
David Härdemanda6e1622014-04-03 20:32:16 -03001036static int parse_protocol_change(u64 *protocols, const char *buf)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001037{
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001038 const char *tmp;
David Härdemanda6e1622014-04-03 20:32:16 -03001039 unsigned count = 0;
1040 bool enable, disable;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001041 u64 mask;
David Härdemanda6e1622014-04-03 20:32:16 -03001042 int i;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001043
David Härdemanda6e1622014-04-03 20:32:16 -03001044 while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001045 if (!*tmp)
1046 break;
1047
1048 if (*tmp == '+') {
1049 enable = true;
1050 disable = false;
1051 tmp++;
1052 } else if (*tmp == '-') {
1053 enable = false;
1054 disable = true;
1055 tmp++;
1056 } else {
1057 enable = false;
1058 disable = false;
1059 }
1060
David Härdemanc003ab12012-10-11 19:11:54 -03001061 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1062 if (!strcasecmp(tmp, proto_names[i].name)) {
1063 mask = proto_names[i].type;
1064 break;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001065 }
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001066 }
1067
David Härdemanc003ab12012-10-11 19:11:54 -03001068 if (i == ARRAY_SIZE(proto_names)) {
David Härdeman275ddb42015-05-19 19:03:22 -03001069 if (!strcasecmp(tmp, "lirc"))
1070 mask = 0;
1071 else {
1072 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
1073 return -EINVAL;
1074 }
David Härdemanc003ab12012-10-11 19:11:54 -03001075 }
1076
1077 count++;
1078
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001079 if (enable)
David Härdemanda6e1622014-04-03 20:32:16 -03001080 *protocols |= mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001081 else if (disable)
David Härdemanda6e1622014-04-03 20:32:16 -03001082 *protocols &= ~mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001083 else
David Härdemanda6e1622014-04-03 20:32:16 -03001084 *protocols = mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001085 }
1086
1087 if (!count) {
1088 IR_dprintk(1, "Protocol not specified\n");
David Härdemanda6e1622014-04-03 20:32:16 -03001089 return -EINVAL;
1090 }
1091
1092 return count;
1093}
1094
Sean Young0d39ab02017-02-25 06:51:31 -05001095void ir_raw_load_modules(u64 *protocols)
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001096{
1097 u64 available;
1098 int i, ret;
1099
1100 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
Sean Young6d741bf2017-08-07 16:20:58 -04001101 if (proto_names[i].type == RC_PROTO_BIT_NONE ||
1102 proto_names[i].type & (RC_PROTO_BIT_OTHER |
1103 RC_PROTO_BIT_UNKNOWN))
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001104 continue;
1105
1106 available = ir_raw_get_allowed_protocols();
1107 if (!(*protocols & proto_names[i].type & ~available))
1108 continue;
1109
1110 if (!proto_names[i].module_name) {
1111 pr_err("Can't enable IR protocol %s\n",
1112 proto_names[i].name);
1113 *protocols &= ~proto_names[i].type;
1114 continue;
1115 }
1116
1117 ret = request_module("%s", proto_names[i].module_name);
1118 if (ret < 0) {
1119 pr_err("Couldn't load IR protocol module %s\n",
1120 proto_names[i].module_name);
1121 *protocols &= ~proto_names[i].type;
1122 continue;
1123 }
1124 msleep(20);
1125 available = ir_raw_get_allowed_protocols();
1126 if (!(*protocols & proto_names[i].type & ~available))
1127 continue;
1128
Sean Young8caebcd2017-01-19 19:33:49 -02001129 pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001130 proto_names[i].module_name,
1131 proto_names[i].name);
1132 *protocols &= ~proto_names[i].type;
1133 }
1134}
1135
David Härdemanda6e1622014-04-03 20:32:16 -03001136/**
1137 * store_protocols() - changes the current/wakeup IR protocol(s)
1138 * @device: the device descriptor
1139 * @mattr: the device attribute struct
1140 * @buf: a pointer to the input buffer
1141 * @len: length of the input buffer
1142 *
1143 * This routine is for changing the IR protocol type.
1144 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
1145 * See parse_protocol_change() for the valid commands.
1146 * Returns @len on success or a negative error code.
1147 *
David Härdeman18726a32017-04-27 17:34:08 -03001148 * dev->lock is taken to guard against races between
1149 * store_protocols and show_protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001150 */
1151static ssize_t store_protocols(struct device *device,
1152 struct device_attribute *mattr,
1153 const char *buf, size_t len)
1154{
1155 struct rc_dev *dev = to_rc_dev(device);
David Härdemanda6e1622014-04-03 20:32:16 -03001156 u64 *current_protocols;
David Härdemanda6e1622014-04-03 20:32:16 -03001157 struct rc_scancode_filter *filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001158 u64 old_protocols, new_protocols;
1159 ssize_t rc;
1160
Sean Young0751d332016-12-05 17:08:35 -02001161 IR_dprintk(1, "Normal protocol change requested\n");
1162 current_protocols = &dev->enabled_protocols;
1163 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001164
Sean Young0751d332016-12-05 17:08:35 -02001165 if (!dev->change_protocol) {
David Härdemanda6e1622014-04-03 20:32:16 -03001166 IR_dprintk(1, "Protocol switching not supported\n");
1167 return -EINVAL;
1168 }
1169
1170 mutex_lock(&dev->lock);
1171
1172 old_protocols = *current_protocols;
1173 new_protocols = old_protocols;
1174 rc = parse_protocol_change(&new_protocols, buf);
1175 if (rc < 0)
1176 goto out;
1177
Sean Young0751d332016-12-05 17:08:35 -02001178 rc = dev->change_protocol(dev, &new_protocols);
David Härdemanda6e1622014-04-03 20:32:16 -03001179 if (rc < 0) {
1180 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
1181 (long long)new_protocols);
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001182 goto out;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001183 }
1184
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001185 if (dev->driver_type == RC_DRIVER_IR_RAW)
1186 ir_raw_load_modules(&new_protocols);
1187
James Hogan983c5bd2014-12-08 13:17:07 -03001188 if (new_protocols != old_protocols) {
1189 *current_protocols = new_protocols;
1190 IR_dprintk(1, "Protocols changed to 0x%llx\n",
1191 (long long)new_protocols);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001192 }
1193
James Hogan6bea25a2014-02-28 20:17:06 -03001194 /*
James Hogan983c5bd2014-12-08 13:17:07 -03001195 * If a protocol change was attempted the filter may need updating, even
1196 * if the actual protocol mask hasn't changed (since the driver may have
1197 * cleared the filter).
James Hogan6bea25a2014-02-28 20:17:06 -03001198 * Try setting the same filter with the new protocol (if any).
1199 * Fall back to clearing the filter.
1200 */
Sean Young0751d332016-12-05 17:08:35 -02001201 if (dev->s_filter && filter->mask) {
David Härdemanda6e1622014-04-03 20:32:16 -03001202 if (new_protocols)
Sean Young0751d332016-12-05 17:08:35 -02001203 rc = dev->s_filter(dev, filter);
David Härdemanda6e1622014-04-03 20:32:16 -03001204 else
1205 rc = -1;
David Härdeman23c843b2014-04-04 19:06:01 -03001206
David Härdemanda6e1622014-04-03 20:32:16 -03001207 if (rc < 0) {
1208 filter->data = 0;
1209 filter->mask = 0;
Sean Young0751d332016-12-05 17:08:35 -02001210 dev->s_filter(dev, filter);
James Hogan6bea25a2014-02-28 20:17:06 -03001211 }
James Hogan6bea25a2014-02-28 20:17:06 -03001212 }
1213
David Härdemanda6e1622014-04-03 20:32:16 -03001214 rc = len;
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001215
1216out:
1217 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001218 return rc;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001219}
1220
James Hogan00942d12014-01-17 10:58:49 -03001221/**
James Hogan00942d12014-01-17 10:58:49 -03001222 * show_filter() - shows the current scancode filter value or mask
1223 * @device: the device descriptor
1224 * @attr: the device attribute struct
1225 * @buf: a pointer to the output buffer
1226 *
1227 * This routine is a callback routine to read a scancode filter value or mask.
1228 * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
1229 * It prints the current scancode filter value or mask of the appropriate filter
1230 * type in hexadecimal into @buf and returns the size of the buffer.
1231 *
1232 * Bits of the filter value corresponding to set bits in the filter mask are
1233 * compared against input scancodes and non-matching scancodes are discarded.
1234 *
David Härdeman18726a32017-04-27 17:34:08 -03001235 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001236 * store_filter and show_filter.
1237 */
1238static ssize_t show_filter(struct device *device,
1239 struct device_attribute *attr,
1240 char *buf)
1241{
1242 struct rc_dev *dev = to_rc_dev(device);
1243 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001244 struct rc_scancode_filter *filter;
James Hogan00942d12014-01-17 10:58:49 -03001245 u32 val;
1246
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001247 mutex_lock(&dev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001248
David Härdemanda6e1622014-04-03 20:32:16 -03001249 if (fattr->type == RC_FILTER_NORMAL)
David Härdemanc5540fb2014-04-03 20:32:21 -03001250 filter = &dev->scancode_filter;
James Hogan00942d12014-01-17 10:58:49 -03001251 else
David Härdemanc5540fb2014-04-03 20:32:21 -03001252 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001253
David Härdemanda6e1622014-04-03 20:32:16 -03001254 if (fattr->mask)
1255 val = filter->mask;
1256 else
1257 val = filter->data;
James Hogan00942d12014-01-17 10:58:49 -03001258 mutex_unlock(&dev->lock);
1259
1260 return sprintf(buf, "%#x\n", val);
1261}
1262
1263/**
1264 * store_filter() - changes the scancode filter value
1265 * @device: the device descriptor
1266 * @attr: the device attribute struct
1267 * @buf: a pointer to the input buffer
1268 * @len: length of the input buffer
1269 *
1270 * This routine is for changing a scancode filter value or mask.
1271 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
1272 * Returns -EINVAL if an invalid filter value for the current protocol was
1273 * specified or if scancode filtering is not supported by the driver, otherwise
1274 * returns @len.
1275 *
1276 * Bits of the filter value corresponding to set bits in the filter mask are
1277 * compared against input scancodes and non-matching scancodes are discarded.
1278 *
David Härdeman18726a32017-04-27 17:34:08 -03001279 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001280 * store_filter and show_filter.
1281 */
1282static ssize_t store_filter(struct device *device,
1283 struct device_attribute *attr,
David Härdemanda6e1622014-04-03 20:32:16 -03001284 const char *buf, size_t len)
James Hogan00942d12014-01-17 10:58:49 -03001285{
1286 struct rc_dev *dev = to_rc_dev(device);
1287 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001288 struct rc_scancode_filter new_filter, *filter;
James Hogan00942d12014-01-17 10:58:49 -03001289 int ret;
1290 unsigned long val;
David Härdeman23c843b2014-04-04 19:06:01 -03001291 int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
James Hogan00942d12014-01-17 10:58:49 -03001292
James Hogan00942d12014-01-17 10:58:49 -03001293 ret = kstrtoul(buf, 0, &val);
1294 if (ret < 0)
1295 return ret;
1296
David Härdemanda6e1622014-04-03 20:32:16 -03001297 if (fattr->type == RC_FILTER_NORMAL) {
1298 set_filter = dev->s_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001299 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001300 } else {
1301 set_filter = dev->s_wakeup_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001302 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001303 }
1304
David Härdeman99b0f3c2014-04-04 19:06:06 -03001305 if (!set_filter)
1306 return -EINVAL;
James Hogan00942d12014-01-17 10:58:49 -03001307
1308 mutex_lock(&dev->lock);
1309
David Härdemanda6e1622014-04-03 20:32:16 -03001310 new_filter = *filter;
James Hogan00942d12014-01-17 10:58:49 -03001311 if (fattr->mask)
David Härdemanda6e1622014-04-03 20:32:16 -03001312 new_filter.mask = val;
James Hogan00942d12014-01-17 10:58:49 -03001313 else
David Härdemanda6e1622014-04-03 20:32:16 -03001314 new_filter.data = val;
David Härdeman23c843b2014-04-04 19:06:01 -03001315
Sean Young0751d332016-12-05 17:08:35 -02001316 if (fattr->type == RC_FILTER_WAKEUP) {
Sean Youngb590c0b2016-12-05 19:24:59 -02001317 /*
1318 * Refuse to set a filter unless a protocol is enabled
1319 * and the filter is valid for that protocol
1320 */
Sean Young6d741bf2017-08-07 16:20:58 -04001321 if (dev->wakeup_protocol != RC_PROTO_UNKNOWN)
James Hoganf423ccc2015-03-31 14:48:10 -03001322 ret = rc_validate_filter(dev, &new_filter);
Sean Youngb590c0b2016-12-05 19:24:59 -02001323 else
Sean Young0751d332016-12-05 17:08:35 -02001324 ret = -EINVAL;
Sean Youngb590c0b2016-12-05 19:24:59 -02001325
1326 if (ret != 0)
Sean Young0751d332016-12-05 17:08:35 -02001327 goto unlock;
Sean Young0751d332016-12-05 17:08:35 -02001328 }
1329
1330 if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
1331 val) {
James Hogan6bea25a2014-02-28 20:17:06 -03001332 /* refuse to set a filter unless a protocol is enabled */
1333 ret = -EINVAL;
1334 goto unlock;
1335 }
David Härdeman23c843b2014-04-04 19:06:01 -03001336
David Härdemanda6e1622014-04-03 20:32:16 -03001337 ret = set_filter(dev, &new_filter);
David Härdeman99b0f3c2014-04-04 19:06:06 -03001338 if (ret < 0)
1339 goto unlock;
James Hogan00942d12014-01-17 10:58:49 -03001340
David Härdemanda6e1622014-04-03 20:32:16 -03001341 *filter = new_filter;
James Hogan00942d12014-01-17 10:58:49 -03001342
1343unlock:
1344 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001345 return (ret < 0) ? ret : len;
James Hogan00942d12014-01-17 10:58:49 -03001346}
1347
Sean Young0751d332016-12-05 17:08:35 -02001348/**
1349 * show_wakeup_protocols() - shows the wakeup IR protocol
1350 * @device: the device descriptor
1351 * @mattr: the device attribute struct
1352 * @buf: a pointer to the output buffer
1353 *
1354 * This routine is a callback routine for input read the IR protocol type(s).
1355 * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols.
1356 * It returns the protocol names of supported protocols.
1357 * The enabled protocols are printed in brackets.
1358 *
David Härdeman18726a32017-04-27 17:34:08 -03001359 * dev->lock is taken to guard against races between
1360 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001361 */
1362static ssize_t show_wakeup_protocols(struct device *device,
1363 struct device_attribute *mattr,
1364 char *buf)
1365{
1366 struct rc_dev *dev = to_rc_dev(device);
1367 u64 allowed;
Sean Young6d741bf2017-08-07 16:20:58 -04001368 enum rc_proto enabled;
Sean Young0751d332016-12-05 17:08:35 -02001369 char *tmp = buf;
1370 int i;
1371
Sean Young0751d332016-12-05 17:08:35 -02001372 mutex_lock(&dev->lock);
1373
1374 allowed = dev->allowed_wakeup_protocols;
1375 enabled = dev->wakeup_protocol;
1376
1377 mutex_unlock(&dev->lock);
1378
1379 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - %d\n",
1380 __func__, (long long)allowed, enabled);
1381
Sean Youngd57ea872017-08-09 13:19:16 -04001382 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001383 if (allowed & (1ULL << i)) {
1384 if (i == enabled)
Sean Youngd57ea872017-08-09 13:19:16 -04001385 tmp += sprintf(tmp, "[%s] ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001386 else
Sean Youngd57ea872017-08-09 13:19:16 -04001387 tmp += sprintf(tmp, "%s ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001388 }
1389 }
1390
1391 if (tmp != buf)
1392 tmp--;
1393 *tmp = '\n';
1394
1395 return tmp + 1 - buf;
1396}
1397
1398/**
1399 * store_wakeup_protocols() - changes the wakeup IR protocol(s)
1400 * @device: the device descriptor
1401 * @mattr: the device attribute struct
1402 * @buf: a pointer to the input buffer
1403 * @len: length of the input buffer
1404 *
1405 * This routine is for changing the IR protocol type.
1406 * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols.
1407 * Returns @len on success or a negative error code.
1408 *
David Härdeman18726a32017-04-27 17:34:08 -03001409 * dev->lock is taken to guard against races between
1410 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001411 */
1412static ssize_t store_wakeup_protocols(struct device *device,
1413 struct device_attribute *mattr,
1414 const char *buf, size_t len)
1415{
1416 struct rc_dev *dev = to_rc_dev(device);
Sean Young6d741bf2017-08-07 16:20:58 -04001417 enum rc_proto protocol;
Sean Young0751d332016-12-05 17:08:35 -02001418 ssize_t rc;
1419 u64 allowed;
1420 int i;
1421
Sean Young0751d332016-12-05 17:08:35 -02001422 mutex_lock(&dev->lock);
1423
1424 allowed = dev->allowed_wakeup_protocols;
1425
1426 if (sysfs_streq(buf, "none")) {
Sean Young6d741bf2017-08-07 16:20:58 -04001427 protocol = RC_PROTO_UNKNOWN;
Sean Young0751d332016-12-05 17:08:35 -02001428 } else {
Sean Youngd57ea872017-08-09 13:19:16 -04001429 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001430 if ((allowed & (1ULL << i)) &&
Sean Youngd57ea872017-08-09 13:19:16 -04001431 sysfs_streq(buf, protocols[i].name)) {
Sean Young0751d332016-12-05 17:08:35 -02001432 protocol = i;
1433 break;
1434 }
1435 }
1436
Sean Youngd57ea872017-08-09 13:19:16 -04001437 if (i == ARRAY_SIZE(protocols)) {
Sean Young0751d332016-12-05 17:08:35 -02001438 rc = -EINVAL;
1439 goto out;
1440 }
James Hoganf423ccc2015-03-31 14:48:10 -03001441
1442 if (dev->encode_wakeup) {
1443 u64 mask = 1ULL << protocol;
1444
1445 ir_raw_load_modules(&mask);
1446 if (!mask) {
1447 rc = -EINVAL;
1448 goto out;
1449 }
1450 }
Sean Young0751d332016-12-05 17:08:35 -02001451 }
1452
1453 if (dev->wakeup_protocol != protocol) {
1454 dev->wakeup_protocol = protocol;
1455 IR_dprintk(1, "Wakeup protocol changed to %d\n", protocol);
1456
Sean Young6d741bf2017-08-07 16:20:58 -04001457 if (protocol == RC_PROTO_RC6_MCE)
Sean Young0751d332016-12-05 17:08:35 -02001458 dev->scancode_wakeup_filter.data = 0x800f0000;
1459 else
1460 dev->scancode_wakeup_filter.data = 0;
1461 dev->scancode_wakeup_filter.mask = 0;
1462
1463 rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
1464 if (rc == 0)
1465 rc = len;
1466 } else {
1467 rc = len;
1468 }
1469
1470out:
1471 mutex_unlock(&dev->lock);
1472 return rc;
1473}
1474
David Härdemand8b4b582010-10-29 16:08:23 -03001475static void rc_dev_release(struct device *device)
1476{
Max Kellermann47cae1e2016-03-21 08:33:05 -03001477 struct rc_dev *dev = to_rc_dev(device);
1478
1479 kfree(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001480}
1481
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001482#define ADD_HOTPLUG_VAR(fmt, val...) \
1483 do { \
1484 int err = add_uevent_var(env, fmt, val); \
1485 if (err) \
1486 return err; \
1487 } while (0)
1488
1489static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1490{
David Härdemand8b4b582010-10-29 16:08:23 -03001491 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001492
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001493 if (dev->rc_map.name)
1494 ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
David Härdemand8b4b582010-10-29 16:08:23 -03001495 if (dev->driver_name)
1496 ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
Sean Youngb9f407e2017-09-01 11:34:23 -03001497 if (dev->device_name)
1498 ADD_HOTPLUG_VAR("DEV_NAME=%s", dev->device_name);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001499
1500 return 0;
1501}
1502
1503/*
1504 * Static device attribute struct with the sysfs attributes for IR's
1505 */
Sean Young6d75db32017-09-01 11:30:50 -03001506static struct device_attribute dev_attr_ro_protocols =
1507__ATTR(protocols, 0444, show_protocols, NULL);
1508static struct device_attribute dev_attr_rw_protocols =
1509__ATTR(protocols, 0644, show_protocols, store_protocols);
Sean Young0751d332016-12-05 17:08:35 -02001510static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
1511 store_wakeup_protocols);
James Hogan00942d12014-01-17 10:58:49 -03001512static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
1513 show_filter, store_filter, RC_FILTER_NORMAL, false);
1514static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
1515 show_filter, store_filter, RC_FILTER_NORMAL, true);
1516static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
1517 show_filter, store_filter, RC_FILTER_WAKEUP, false);
1518static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
1519 show_filter, store_filter, RC_FILTER_WAKEUP, true);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001520
Sean Young6d75db32017-09-01 11:30:50 -03001521static struct attribute *rc_dev_rw_protocol_attrs[] = {
1522 &dev_attr_rw_protocols.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001523 NULL,
1524};
1525
Sean Young6d75db32017-09-01 11:30:50 -03001526static const struct attribute_group rc_dev_rw_protocol_attr_grp = {
1527 .attrs = rc_dev_rw_protocol_attrs,
1528};
1529
1530static struct attribute *rc_dev_ro_protocol_attrs[] = {
1531 &dev_attr_ro_protocols.attr,
1532 NULL,
1533};
1534
1535static const struct attribute_group rc_dev_ro_protocol_attr_grp = {
1536 .attrs = rc_dev_ro_protocol_attrs,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001537};
1538
David Härdeman99b0f3c2014-04-04 19:06:06 -03001539static struct attribute *rc_dev_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001540 &dev_attr_filter.attr.attr,
1541 &dev_attr_filter_mask.attr.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001542 NULL,
1543};
1544
Arvind Yadavdb681022017-07-07 04:23:54 -04001545static const struct attribute_group rc_dev_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001546 .attrs = rc_dev_filter_attrs,
1547};
1548
1549static struct attribute *rc_dev_wakeup_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001550 &dev_attr_wakeup_filter.attr.attr,
1551 &dev_attr_wakeup_filter_mask.attr.attr,
Sean Young0751d332016-12-05 17:08:35 -02001552 &dev_attr_wakeup_protocols.attr,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001553 NULL,
1554};
1555
Arvind Yadavdb681022017-07-07 04:23:54 -04001556static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001557 .attrs = rc_dev_wakeup_filter_attrs,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001558};
1559
Bhumika Goyalf03f02f2017-08-19 05:22:15 -03001560static const struct device_type rc_dev_type = {
David Härdemand8b4b582010-10-29 16:08:23 -03001561 .release = rc_dev_release,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001562 .uevent = rc_dev_uevent,
1563};
1564
Andi Shyti0f7499f2016-12-16 06:50:58 -02001565struct rc_dev *rc_allocate_device(enum rc_driver_type type)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001566{
David Härdemand8b4b582010-10-29 16:08:23 -03001567 struct rc_dev *dev;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001568
David Härdemand8b4b582010-10-29 16:08:23 -03001569 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1570 if (!dev)
1571 return NULL;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001572
Andi Shytid34aee12016-12-16 04:12:15 -02001573 if (type != RC_DRIVER_IR_RAW_TX) {
1574 dev->input_dev = input_allocate_device();
1575 if (!dev->input_dev) {
1576 kfree(dev);
1577 return NULL;
1578 }
1579
1580 dev->input_dev->getkeycode = ir_getkeycode;
1581 dev->input_dev->setkeycode = ir_setkeycode;
1582 input_set_drvdata(dev->input_dev, dev);
1583
Kees Cookb17ec782017-10-24 11:23:14 -04001584 timer_setup(&dev->timer_keyup, ir_timer_keyup, 0);
Andi Shytid34aee12016-12-16 04:12:15 -02001585
1586 spin_lock_init(&dev->rc_map.lock);
1587 spin_lock_init(&dev->keylock);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001588 }
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001589 mutex_init(&dev->lock);
David Härdemand8b4b582010-10-29 16:08:23 -03001590
1591 dev->dev.type = &rc_dev_type;
David Härdeman40fc5322013-03-06 16:52:10 -03001592 dev->dev.class = &rc_class;
David Härdemand8b4b582010-10-29 16:08:23 -03001593 device_initialize(&dev->dev);
1594
Andi Shyti0f7499f2016-12-16 06:50:58 -02001595 dev->driver_type = type;
1596
David Härdemand8b4b582010-10-29 16:08:23 -03001597 __module_get(THIS_MODULE);
1598 return dev;
1599}
1600EXPORT_SYMBOL_GPL(rc_allocate_device);
1601
1602void rc_free_device(struct rc_dev *dev)
1603{
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001604 if (!dev)
1605 return;
1606
Markus Elfring3dd94f02014-11-20 09:01:32 -03001607 input_free_device(dev->input_dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001608
1609 put_device(&dev->dev);
1610
Max Kellermann47cae1e2016-03-21 08:33:05 -03001611 /* kfree(dev) will be called by the callback function
1612 rc_dev_release() */
1613
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001614 module_put(THIS_MODULE);
David Härdemand8b4b582010-10-29 16:08:23 -03001615}
1616EXPORT_SYMBOL_GPL(rc_free_device);
1617
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001618static void devm_rc_alloc_release(struct device *dev, void *res)
1619{
1620 rc_free_device(*(struct rc_dev **)res);
1621}
1622
Andi Shyti0f7499f2016-12-16 06:50:58 -02001623struct rc_dev *devm_rc_allocate_device(struct device *dev,
1624 enum rc_driver_type type)
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001625{
1626 struct rc_dev **dr, *rc;
1627
1628 dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
1629 if (!dr)
1630 return NULL;
1631
Andi Shyti0f7499f2016-12-16 06:50:58 -02001632 rc = rc_allocate_device(type);
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001633 if (!rc) {
1634 devres_free(dr);
1635 return NULL;
1636 }
1637
1638 rc->dev.parent = dev;
1639 rc->managed_alloc = true;
1640 *dr = rc;
1641 devres_add(dev, dr);
1642
1643 return rc;
1644}
1645EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
1646
David Härdemanf56928a2017-05-03 07:04:00 -03001647static int rc_prepare_rx_device(struct rc_dev *dev)
David Härdemand8b4b582010-10-29 16:08:23 -03001648{
David Härdemanfcb13092015-05-19 19:03:17 -03001649 int rc;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001650 struct rc_map *rc_map;
Sean Young6d741bf2017-08-07 16:20:58 -04001651 u64 rc_proto;
David Härdemand8b4b582010-10-29 16:08:23 -03001652
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001653 if (!dev->map_name)
David Härdemand8b4b582010-10-29 16:08:23 -03001654 return -EINVAL;
1655
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001656 rc_map = rc_map_get(dev->map_name);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001657 if (!rc_map)
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001658 rc_map = rc_map_get(RC_MAP_EMPTY);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001659 if (!rc_map || !rc_map->scan || rc_map->size == 0)
David Härdemand8b4b582010-10-29 16:08:23 -03001660 return -EINVAL;
1661
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001662 rc = ir_setkeytable(dev, rc_map);
1663 if (rc)
1664 return rc;
1665
Sean Young6d741bf2017-08-07 16:20:58 -04001666 rc_proto = BIT_ULL(rc_map->rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001667
Sean Young831c4c82017-09-01 09:55:59 -03001668 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1669 dev->enabled_protocols = dev->allowed_protocols;
1670
Sean Young41380862017-02-22 18:48:01 -03001671 if (dev->change_protocol) {
Sean Young6d741bf2017-08-07 16:20:58 -04001672 rc = dev->change_protocol(dev, &rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001673 if (rc < 0)
1674 goto out_table;
Sean Young6d741bf2017-08-07 16:20:58 -04001675 dev->enabled_protocols = rc_proto;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001676 }
1677
Sean Young41380862017-02-22 18:48:01 -03001678 if (dev->driver_type == RC_DRIVER_IR_RAW)
Sean Young6d741bf2017-08-07 16:20:58 -04001679 ir_raw_load_modules(&rc_proto);
Sean Young41380862017-02-22 18:48:01 -03001680
David Härdemand8b4b582010-10-29 16:08:23 -03001681 set_bit(EV_KEY, dev->input_dev->evbit);
1682 set_bit(EV_REP, dev->input_dev->evbit);
1683 set_bit(EV_MSC, dev->input_dev->evbit);
1684 set_bit(MSC_SCAN, dev->input_dev->mscbit);
1685 if (dev->open)
1686 dev->input_dev->open = ir_open;
1687 if (dev->close)
1688 dev->input_dev->close = ir_close;
1689
David Härdemanb2aceb72017-04-27 17:33:58 -03001690 dev->input_dev->dev.parent = &dev->dev;
1691 memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
1692 dev->input_dev->phys = dev->input_phys;
Sean Young518f4b22017-07-01 12:13:19 -04001693 dev->input_dev->name = dev->device_name;
David Härdemanb2aceb72017-04-27 17:33:58 -03001694
David Härdemanf56928a2017-05-03 07:04:00 -03001695 return 0;
1696
1697out_table:
1698 ir_free_table(&dev->rc_map);
1699
1700 return rc;
1701}
1702
1703static int rc_setup_rx_device(struct rc_dev *dev)
1704{
1705 int rc;
1706
David Härdemanb2aceb72017-04-27 17:33:58 -03001707 /* rc_open will be called here */
1708 rc = input_register_device(dev->input_dev);
1709 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001710 return rc;
David Härdemanb2aceb72017-04-27 17:33:58 -03001711
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001712 /*
1713 * Default delay of 250ms is too short for some protocols, especially
1714 * since the timeout is currently set to 250ms. Increase it to 500ms,
1715 * to avoid wrong repetition of the keycodes. Note that this must be
1716 * set after the call to input_register_device().
1717 */
1718 dev->input_dev->rep[REP_DELAY] = 500;
1719
1720 /*
1721 * As a repeat event on protocols like RC-5 and NEC take as long as
1722 * 110/114ms, using 33ms as a repeat period is not the right thing
1723 * to do.
1724 */
1725 dev->input_dev->rep[REP_PERIOD] = 125;
1726
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001727 return 0;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001728}
1729
1730static void rc_free_rx_device(struct rc_dev *dev)
1731{
David Härdemanf56928a2017-05-03 07:04:00 -03001732 if (!dev)
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001733 return;
1734
David Härdemanf56928a2017-05-03 07:04:00 -03001735 if (dev->input_dev) {
1736 input_unregister_device(dev->input_dev);
1737 dev->input_dev = NULL;
1738 }
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001739
David Härdemanf56928a2017-05-03 07:04:00 -03001740 ir_free_table(&dev->rc_map);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001741}
1742
1743int rc_register_device(struct rc_dev *dev)
1744{
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001745 const char *path;
1746 int attr = 0;
1747 int minor;
1748 int rc;
1749
1750 if (!dev)
1751 return -EINVAL;
1752
David Härdemanfcb13092015-05-19 19:03:17 -03001753 minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
1754 if (minor < 0)
1755 return minor;
1756
1757 dev->minor = minor;
1758 dev_set_name(&dev->dev, "rc%u", dev->minor);
1759 dev_set_drvdata(&dev->dev, dev);
Mauro Carvalho Chehab587d1b02014-01-14 16:27:55 -03001760
David Härdeman99b0f3c2014-04-04 19:06:06 -03001761 dev->dev.groups = dev->sysfs_groups;
Sean Young6d75db32017-09-01 11:30:50 -03001762 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1763 dev->sysfs_groups[attr++] = &rc_dev_ro_protocol_attr_grp;
1764 else if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
1765 dev->sysfs_groups[attr++] = &rc_dev_rw_protocol_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001766 if (dev->s_filter)
David Härdeman120703f2014-04-03 20:31:30 -03001767 dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001768 if (dev->s_wakeup_filter)
1769 dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001770 dev->sysfs_groups[attr++] = NULL;
1771
Sean Younga60d64b2017-09-23 10:41:13 -04001772 if (dev->driver_type == RC_DRIVER_IR_RAW) {
David Härdemanf56928a2017-05-03 07:04:00 -03001773 rc = ir_raw_event_prepare(dev);
1774 if (rc < 0)
1775 goto out_minor;
1776 }
1777
1778 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1779 rc = rc_prepare_rx_device(dev);
1780 if (rc)
1781 goto out_raw;
1782 }
1783
David Härdemand8b4b582010-10-29 16:08:23 -03001784 rc = device_add(&dev->dev);
1785 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001786 goto out_rx_free;
David Härdemand8b4b582010-10-29 16:08:23 -03001787
David Härdemand8b4b582010-10-29 16:08:23 -03001788 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Heiner Kallweit4dc0e902015-10-29 19:39:06 -02001789 dev_info(&dev->dev, "%s as %s\n",
Sean Young518f4b22017-07-01 12:13:19 -04001790 dev->device_name ?: "Unspecified device", path ?: "N/A");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001791 kfree(path);
1792
Sean Young5df62772017-02-23 06:11:21 -03001793 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1794 rc = rc_setup_rx_device(dev);
1795 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001796 goto out_dev;
1797 }
1798
Sean Younga60d64b2017-09-23 10:41:13 -04001799 /* Ensure that the lirc kfifo is setup before we start the thread */
1800 if (dev->driver_type != RC_DRIVER_SCANCODE) {
1801 rc = ir_lirc_register(dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001802 if (rc < 0)
1803 goto out_rx;
David Härdemand8b4b582010-10-29 16:08:23 -03001804 }
1805
Sean Younga60d64b2017-09-23 10:41:13 -04001806 if (dev->driver_type == RC_DRIVER_IR_RAW) {
1807 rc = ir_raw_event_register(dev);
1808 if (rc < 0)
1809 goto out_lirc;
1810 }
1811
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001812 IR_dprintk(1, "Registered rc%u (driver: %s)\n",
David Härdemanfcb13092015-05-19 19:03:17 -03001813 dev->minor,
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001814 dev->driver_name ? dev->driver_name : "unknown");
David Härdemand8b4b582010-10-29 16:08:23 -03001815
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001816 return 0;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001817
Sean Younga60d64b2017-09-23 10:41:13 -04001818out_lirc:
1819 if (dev->driver_type != RC_DRIVER_SCANCODE)
1820 ir_lirc_unregister(dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001821out_rx:
1822 rc_free_rx_device(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001823out_dev:
1824 device_del(&dev->dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001825out_rx_free:
1826 ir_free_table(&dev->rc_map);
1827out_raw:
1828 ir_raw_event_free(dev);
1829out_minor:
David Härdemanfcb13092015-05-19 19:03:17 -03001830 ida_simple_remove(&rc_ida, minor);
David Härdemand8b4b582010-10-29 16:08:23 -03001831 return rc;
1832}
1833EXPORT_SYMBOL_GPL(rc_register_device);
1834
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001835static void devm_rc_release(struct device *dev, void *res)
1836{
1837 rc_unregister_device(*(struct rc_dev **)res);
1838}
1839
1840int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
1841{
1842 struct rc_dev **dr;
1843 int ret;
1844
1845 dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
1846 if (!dr)
1847 return -ENOMEM;
1848
1849 ret = rc_register_device(dev);
1850 if (ret) {
1851 devres_free(dr);
1852 return ret;
1853 }
1854
1855 *dr = dev;
1856 devres_add(parent, dr);
1857
1858 return 0;
1859}
1860EXPORT_SYMBOL_GPL(devm_rc_register_device);
1861
David Härdemand8b4b582010-10-29 16:08:23 -03001862void rc_unregister_device(struct rc_dev *dev)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001863{
David Härdemand8b4b582010-10-29 16:08:23 -03001864 if (!dev)
1865 return;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001866
David Härdemand8b4b582010-10-29 16:08:23 -03001867 del_timer_sync(&dev->timer_keyup);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001868
David Härdemand8b4b582010-10-29 16:08:23 -03001869 if (dev->driver_type == RC_DRIVER_IR_RAW)
1870 ir_raw_event_unregister(dev);
1871
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001872 rc_free_rx_device(dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001873
Sean Younga60d64b2017-09-23 10:41:13 -04001874 if (dev->driver_type != RC_DRIVER_SCANCODE)
1875 ir_lirc_unregister(dev);
1876
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001877 device_del(&dev->dev);
1878
David Härdemanfcb13092015-05-19 19:03:17 -03001879 ida_simple_remove(&rc_ida, dev->minor);
1880
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001881 if (!dev->managed_alloc)
1882 rc_free_device(dev);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001883}
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001884
David Härdemand8b4b582010-10-29 16:08:23 -03001885EXPORT_SYMBOL_GPL(rc_unregister_device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001886
1887/*
1888 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1889 */
1890
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001891static int __init rc_core_init(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001892{
David Härdeman40fc5322013-03-06 16:52:10 -03001893 int rc = class_register(&rc_class);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001894 if (rc) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -02001895 pr_err("rc_core: unable to register rc class\n");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001896 return rc;
1897 }
1898
Sean Younga60d64b2017-09-23 10:41:13 -04001899 rc = lirc_dev_init();
1900 if (rc) {
1901 pr_err("rc_core: unable to init lirc\n");
1902 class_unregister(&rc_class);
1903 return 0;
1904 }
1905
Sean Young153a60b2013-07-30 19:00:01 -03001906 led_trigger_register_simple("rc-feedback", &led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001907 rc_map_register(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001908
1909 return 0;
1910}
1911
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001912static void __exit rc_core_exit(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001913{
Sean Younga60d64b2017-09-23 10:41:13 -04001914 lirc_dev_exit();
David Härdeman40fc5322013-03-06 16:52:10 -03001915 class_unregister(&rc_class);
Sean Young153a60b2013-07-30 19:00:01 -03001916 led_trigger_unregister_simple(led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001917 rc_map_unregister(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001918}
1919
David Härdemane76d4ce2013-03-06 16:52:15 -03001920subsys_initcall(rc_core_init);
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001921module_exit(rc_core_exit);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001922
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001923int rc_core_debug; /* ir_debug level (0,1,2) */
1924EXPORT_SYMBOL_GPL(rc_core_debug);
1925module_param_named(debug, rc_core_debug, int, 0644);
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03001926
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02001927MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehab20835282017-12-01 08:47:08 -05001928MODULE_LICENSE("GPL v2");