blob: 56b322b3d3258f54521b654b23252acf5e74266c [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/**
779 * rc_validate_filter() - checks that the scancode and mask are valid and
780 * provides sensible defaults
James Hoganf423ccc2015-03-31 14:48:10 -0300781 * @dev: the struct rc_dev descriptor of the device
Sean Youngb590c0b2016-12-05 19:24:59 -0200782 * @filter: the scancode and mask
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500783 *
784 * return: 0 or -EINVAL if the filter is not valid
Sean Youngb590c0b2016-12-05 19:24:59 -0200785 */
James Hoganf423ccc2015-03-31 14:48:10 -0300786static int rc_validate_filter(struct rc_dev *dev,
Sean Youngb590c0b2016-12-05 19:24:59 -0200787 struct rc_scancode_filter *filter)
788{
Sean Youngd57ea872017-08-09 13:19:16 -0400789 u32 mask, s = filter->data;
Sean Young6d741bf2017-08-07 16:20:58 -0400790 enum rc_proto protocol = dev->wakeup_protocol;
Sean Youngb590c0b2016-12-05 19:24:59 -0200791
Sean Youngd57ea872017-08-09 13:19:16 -0400792 if (protocol >= ARRAY_SIZE(protocols))
Sean Young2168b412017-08-07 09:21:29 -0400793 return -EINVAL;
794
Sean Youngd57ea872017-08-09 13:19:16 -0400795 mask = protocols[protocol].scancode_bits;
796
Sean Youngb590c0b2016-12-05 19:24:59 -0200797 switch (protocol) {
Sean Young6d741bf2017-08-07 16:20:58 -0400798 case RC_PROTO_NECX:
Sean Youngb590c0b2016-12-05 19:24:59 -0200799 if ((((s >> 16) ^ ~(s >> 8)) & 0xff) == 0)
800 return -EINVAL;
801 break;
Sean Young6d741bf2017-08-07 16:20:58 -0400802 case RC_PROTO_NEC32:
Sean Youngb590c0b2016-12-05 19:24:59 -0200803 if ((((s >> 24) ^ ~(s >> 16)) & 0xff) == 0)
804 return -EINVAL;
805 break;
Sean Young6d741bf2017-08-07 16:20:58 -0400806 case RC_PROTO_RC6_MCE:
Sean Youngb590c0b2016-12-05 19:24:59 -0200807 if ((s & 0xffff0000) != 0x800f0000)
808 return -EINVAL;
809 break;
Sean Young6d741bf2017-08-07 16:20:58 -0400810 case RC_PROTO_RC6_6A_32:
Sean Youngb590c0b2016-12-05 19:24:59 -0200811 if ((s & 0xffff0000) == 0x800f0000)
812 return -EINVAL;
813 break;
814 default:
815 break;
816 }
817
Sean Youngd57ea872017-08-09 13:19:16 -0400818 filter->data &= mask;
819 filter->mask &= mask;
Sean Youngb590c0b2016-12-05 19:24:59 -0200820
James Hoganf423ccc2015-03-31 14:48:10 -0300821 /*
822 * If we have to raw encode the IR for wakeup, we cannot have a mask
823 */
Sean Youngd57ea872017-08-09 13:19:16 -0400824 if (dev->encode_wakeup && filter->mask != 0 && filter->mask != mask)
James Hoganf423ccc2015-03-31 14:48:10 -0300825 return -EINVAL;
826
Sean Youngb590c0b2016-12-05 19:24:59 -0200827 return 0;
828}
829
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300830int rc_open(struct rc_dev *rdev)
831{
832 int rval = 0;
833
834 if (!rdev)
835 return -EINVAL;
836
837 mutex_lock(&rdev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -0200838
Juergen Lockf02dcdd2013-08-16 15:00:24 -0300839 if (!rdev->users++ && rdev->open != NULL)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300840 rval = rdev->open(rdev);
841
842 if (rval)
843 rdev->users--;
844
845 mutex_unlock(&rdev->lock);
846
847 return rval;
848}
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300849
David Härdemand8b4b582010-10-29 16:08:23 -0300850static int ir_open(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300851{
David Härdemand8b4b582010-10-29 16:08:23 -0300852 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300853
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300854 return rc_open(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300855}
856
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300857void rc_close(struct rc_dev *rdev)
858{
859 if (rdev) {
860 mutex_lock(&rdev->lock);
861
Mauro Carvalho Chehab81b7d142015-04-28 09:43:17 -0300862 if (!--rdev->users && rdev->close != NULL)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300863 rdev->close(rdev);
864
865 mutex_unlock(&rdev->lock);
866 }
867}
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300868
David Härdemand8b4b582010-10-29 16:08:23 -0300869static void ir_close(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300870{
David Härdemand8b4b582010-10-29 16:08:23 -0300871 struct rc_dev *rdev = input_get_drvdata(idev);
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300872 rc_close(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300873}
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300874
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300875/* class for /sys/class/rc */
David Härdeman40fc5322013-03-06 16:52:10 -0300876static char *rc_devnode(struct device *dev, umode_t *mode)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300877{
878 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
879}
880
David Härdeman40fc5322013-03-06 16:52:10 -0300881static struct class rc_class = {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300882 .name = "rc",
David Härdeman40fc5322013-03-06 16:52:10 -0300883 .devnode = rc_devnode,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300884};
885
David Härdemanc003ab12012-10-11 19:11:54 -0300886/*
887 * These are the protocol textual descriptions that are
888 * used by the sysfs protocols file. Note that the order
889 * of the entries is relevant.
890 */
Heiner Kallweit53df8772015-11-16 17:52:17 -0200891static const struct {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300892 u64 type;
Heiner Kallweit53df8772015-11-16 17:52:17 -0200893 const char *name;
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200894 const char *module_name;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300895} proto_names[] = {
Sean Young6d741bf2017-08-07 16:20:58 -0400896 { RC_PROTO_BIT_NONE, "none", NULL },
897 { RC_PROTO_BIT_OTHER, "other", NULL },
898 { RC_PROTO_BIT_UNKNOWN, "unknown", NULL },
899 { RC_PROTO_BIT_RC5 |
900 RC_PROTO_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
901 { RC_PROTO_BIT_NEC |
902 RC_PROTO_BIT_NECX |
903 RC_PROTO_BIT_NEC32, "nec", "ir-nec-decoder" },
904 { RC_PROTO_BIT_RC6_0 |
905 RC_PROTO_BIT_RC6_6A_20 |
906 RC_PROTO_BIT_RC6_6A_24 |
907 RC_PROTO_BIT_RC6_6A_32 |
908 RC_PROTO_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
909 { RC_PROTO_BIT_JVC, "jvc", "ir-jvc-decoder" },
910 { RC_PROTO_BIT_SONY12 |
911 RC_PROTO_BIT_SONY15 |
912 RC_PROTO_BIT_SONY20, "sony", "ir-sony-decoder" },
913 { RC_PROTO_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
914 { RC_PROTO_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
915 { RC_PROTO_BIT_SHARP, "sharp", "ir-sharp-decoder" },
916 { RC_PROTO_BIT_MCIR2_KBD |
917 RC_PROTO_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" },
918 { RC_PROTO_BIT_XMP, "xmp", "ir-xmp-decoder" },
919 { RC_PROTO_BIT_CEC, "cec", NULL },
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300920};
921
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300922/**
James Hoganab88c662014-02-28 20:17:05 -0300923 * struct rc_filter_attribute - Device attribute relating to a filter type.
924 * @attr: Device attribute.
925 * @type: Filter type.
926 * @mask: false for filter value, true for filter mask.
927 */
928struct rc_filter_attribute {
929 struct device_attribute attr;
930 enum rc_filter_type type;
931 bool mask;
932};
933#define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
934
James Hoganab88c662014-02-28 20:17:05 -0300935#define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
936 struct rc_filter_attribute dev_attr_##_name = { \
937 .attr = __ATTR(_name, _mode, _show, _store), \
938 .type = (_type), \
939 .mask = (_mask), \
940 }
941
942/**
Sean Young0751d332016-12-05 17:08:35 -0200943 * show_protocols() - shows the current IR protocol(s)
David Härdemand8b4b582010-10-29 16:08:23 -0300944 * @device: the device descriptor
David Härdemanda6e1622014-04-03 20:32:16 -0300945 * @mattr: the device attribute struct
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300946 * @buf: a pointer to the output buffer
947 *
948 * This routine is a callback routine for input read the IR protocol type(s).
Sean Young0751d332016-12-05 17:08:35 -0200949 * it is trigged by reading /sys/class/rc/rc?/protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300950 * It returns the protocol names of supported protocols.
951 * Enabled protocols are printed in brackets.
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300952 *
David Härdeman18726a32017-04-27 17:34:08 -0300953 * dev->lock is taken to guard against races between
954 * store_protocols and show_protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300955 */
David Härdemand8b4b582010-10-29 16:08:23 -0300956static ssize_t show_protocols(struct device *device,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300957 struct device_attribute *mattr, char *buf)
958{
David Härdemand8b4b582010-10-29 16:08:23 -0300959 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300960 u64 allowed, enabled;
961 char *tmp = buf;
962 int i;
963
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300964 mutex_lock(&dev->lock);
965
Sean Young0751d332016-12-05 17:08:35 -0200966 enabled = dev->enabled_protocols;
967 allowed = dev->allowed_protocols;
968 if (dev->raw && !allowed)
969 allowed = ir_raw_get_allowed_protocols();
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300970
David Härdemanda6e1622014-04-03 20:32:16 -0300971 mutex_unlock(&dev->lock);
972
973 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
974 __func__, (long long)allowed, (long long)enabled);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300975
976 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
977 if (allowed & enabled & proto_names[i].type)
978 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
979 else if (allowed & proto_names[i].type)
980 tmp += sprintf(tmp, "%s ", proto_names[i].name);
David Härdemanc003ab12012-10-11 19:11:54 -0300981
982 if (allowed & proto_names[i].type)
983 allowed &= ~proto_names[i].type;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300984 }
985
Sean Younga60d64b2017-09-23 10:41:13 -0400986#ifdef CONFIG_LIRC
987 if (dev->driver_type == RC_DRIVER_IR_RAW)
David Härdeman275ddb42015-05-19 19:03:22 -0300988 tmp += sprintf(tmp, "[lirc] ");
Sean Younga60d64b2017-09-23 10:41:13 -0400989#endif
David Härdeman275ddb42015-05-19 19:03:22 -0300990
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300991 if (tmp != buf)
992 tmp--;
993 *tmp = '\n';
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300994
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300995 return tmp + 1 - buf;
996}
997
998/**
David Härdemanda6e1622014-04-03 20:32:16 -0300999 * parse_protocol_change() - parses a protocol change request
1000 * @protocols: pointer to the bitmask of current protocols
1001 * @buf: pointer to the buffer with a list of changes
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001002 *
David Härdemanda6e1622014-04-03 20:32:16 -03001003 * Writing "+proto" will add a protocol to the protocol mask.
1004 * Writing "-proto" will remove a protocol from protocol mask.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001005 * Writing "proto" will enable only "proto".
1006 * Writing "none" will disable all protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001007 * Returns the number of changes performed or a negative error code.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001008 */
David Härdemanda6e1622014-04-03 20:32:16 -03001009static int parse_protocol_change(u64 *protocols, const char *buf)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001010{
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001011 const char *tmp;
David Härdemanda6e1622014-04-03 20:32:16 -03001012 unsigned count = 0;
1013 bool enable, disable;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001014 u64 mask;
David Härdemanda6e1622014-04-03 20:32:16 -03001015 int i;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001016
David Härdemanda6e1622014-04-03 20:32:16 -03001017 while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001018 if (!*tmp)
1019 break;
1020
1021 if (*tmp == '+') {
1022 enable = true;
1023 disable = false;
1024 tmp++;
1025 } else if (*tmp == '-') {
1026 enable = false;
1027 disable = true;
1028 tmp++;
1029 } else {
1030 enable = false;
1031 disable = false;
1032 }
1033
David Härdemanc003ab12012-10-11 19:11:54 -03001034 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1035 if (!strcasecmp(tmp, proto_names[i].name)) {
1036 mask = proto_names[i].type;
1037 break;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001038 }
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001039 }
1040
David Härdemanc003ab12012-10-11 19:11:54 -03001041 if (i == ARRAY_SIZE(proto_names)) {
David Härdeman275ddb42015-05-19 19:03:22 -03001042 if (!strcasecmp(tmp, "lirc"))
1043 mask = 0;
1044 else {
1045 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
1046 return -EINVAL;
1047 }
David Härdemanc003ab12012-10-11 19:11:54 -03001048 }
1049
1050 count++;
1051
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001052 if (enable)
David Härdemanda6e1622014-04-03 20:32:16 -03001053 *protocols |= mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001054 else if (disable)
David Härdemanda6e1622014-04-03 20:32:16 -03001055 *protocols &= ~mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001056 else
David Härdemanda6e1622014-04-03 20:32:16 -03001057 *protocols = mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001058 }
1059
1060 if (!count) {
1061 IR_dprintk(1, "Protocol not specified\n");
David Härdemanda6e1622014-04-03 20:32:16 -03001062 return -EINVAL;
1063 }
1064
1065 return count;
1066}
1067
Sean Young0d39ab02017-02-25 06:51:31 -05001068void ir_raw_load_modules(u64 *protocols)
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001069{
1070 u64 available;
1071 int i, ret;
1072
1073 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
Sean Young6d741bf2017-08-07 16:20:58 -04001074 if (proto_names[i].type == RC_PROTO_BIT_NONE ||
1075 proto_names[i].type & (RC_PROTO_BIT_OTHER |
1076 RC_PROTO_BIT_UNKNOWN))
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001077 continue;
1078
1079 available = ir_raw_get_allowed_protocols();
1080 if (!(*protocols & proto_names[i].type & ~available))
1081 continue;
1082
1083 if (!proto_names[i].module_name) {
1084 pr_err("Can't enable IR protocol %s\n",
1085 proto_names[i].name);
1086 *protocols &= ~proto_names[i].type;
1087 continue;
1088 }
1089
1090 ret = request_module("%s", proto_names[i].module_name);
1091 if (ret < 0) {
1092 pr_err("Couldn't load IR protocol module %s\n",
1093 proto_names[i].module_name);
1094 *protocols &= ~proto_names[i].type;
1095 continue;
1096 }
1097 msleep(20);
1098 available = ir_raw_get_allowed_protocols();
1099 if (!(*protocols & proto_names[i].type & ~available))
1100 continue;
1101
Sean Young8caebcd2017-01-19 19:33:49 -02001102 pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001103 proto_names[i].module_name,
1104 proto_names[i].name);
1105 *protocols &= ~proto_names[i].type;
1106 }
1107}
1108
David Härdemanda6e1622014-04-03 20:32:16 -03001109/**
1110 * store_protocols() - changes the current/wakeup IR protocol(s)
1111 * @device: the device descriptor
1112 * @mattr: the device attribute struct
1113 * @buf: a pointer to the input buffer
1114 * @len: length of the input buffer
1115 *
1116 * This routine is for changing the IR protocol type.
1117 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
1118 * See parse_protocol_change() for the valid commands.
1119 * Returns @len on success or a negative error code.
1120 *
David Härdeman18726a32017-04-27 17:34:08 -03001121 * dev->lock is taken to guard against races between
1122 * store_protocols and show_protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001123 */
1124static ssize_t store_protocols(struct device *device,
1125 struct device_attribute *mattr,
1126 const char *buf, size_t len)
1127{
1128 struct rc_dev *dev = to_rc_dev(device);
David Härdemanda6e1622014-04-03 20:32:16 -03001129 u64 *current_protocols;
David Härdemanda6e1622014-04-03 20:32:16 -03001130 struct rc_scancode_filter *filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001131 u64 old_protocols, new_protocols;
1132 ssize_t rc;
1133
Sean Young0751d332016-12-05 17:08:35 -02001134 IR_dprintk(1, "Normal protocol change requested\n");
1135 current_protocols = &dev->enabled_protocols;
1136 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001137
Sean Young0751d332016-12-05 17:08:35 -02001138 if (!dev->change_protocol) {
David Härdemanda6e1622014-04-03 20:32:16 -03001139 IR_dprintk(1, "Protocol switching not supported\n");
1140 return -EINVAL;
1141 }
1142
1143 mutex_lock(&dev->lock);
1144
1145 old_protocols = *current_protocols;
1146 new_protocols = old_protocols;
1147 rc = parse_protocol_change(&new_protocols, buf);
1148 if (rc < 0)
1149 goto out;
1150
Sean Young0751d332016-12-05 17:08:35 -02001151 rc = dev->change_protocol(dev, &new_protocols);
David Härdemanda6e1622014-04-03 20:32:16 -03001152 if (rc < 0) {
1153 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
1154 (long long)new_protocols);
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001155 goto out;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001156 }
1157
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001158 if (dev->driver_type == RC_DRIVER_IR_RAW)
1159 ir_raw_load_modules(&new_protocols);
1160
James Hogan983c5bd2014-12-08 13:17:07 -03001161 if (new_protocols != old_protocols) {
1162 *current_protocols = new_protocols;
1163 IR_dprintk(1, "Protocols changed to 0x%llx\n",
1164 (long long)new_protocols);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001165 }
1166
James Hogan6bea25a2014-02-28 20:17:06 -03001167 /*
James Hogan983c5bd2014-12-08 13:17:07 -03001168 * If a protocol change was attempted the filter may need updating, even
1169 * if the actual protocol mask hasn't changed (since the driver may have
1170 * cleared the filter).
James Hogan6bea25a2014-02-28 20:17:06 -03001171 * Try setting the same filter with the new protocol (if any).
1172 * Fall back to clearing the filter.
1173 */
Sean Young0751d332016-12-05 17:08:35 -02001174 if (dev->s_filter && filter->mask) {
David Härdemanda6e1622014-04-03 20:32:16 -03001175 if (new_protocols)
Sean Young0751d332016-12-05 17:08:35 -02001176 rc = dev->s_filter(dev, filter);
David Härdemanda6e1622014-04-03 20:32:16 -03001177 else
1178 rc = -1;
David Härdeman23c843b2014-04-04 19:06:01 -03001179
David Härdemanda6e1622014-04-03 20:32:16 -03001180 if (rc < 0) {
1181 filter->data = 0;
1182 filter->mask = 0;
Sean Young0751d332016-12-05 17:08:35 -02001183 dev->s_filter(dev, filter);
James Hogan6bea25a2014-02-28 20:17:06 -03001184 }
James Hogan6bea25a2014-02-28 20:17:06 -03001185 }
1186
David Härdemanda6e1622014-04-03 20:32:16 -03001187 rc = len;
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001188
1189out:
1190 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001191 return rc;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001192}
1193
James Hogan00942d12014-01-17 10:58:49 -03001194/**
James Hogan00942d12014-01-17 10:58:49 -03001195 * show_filter() - shows the current scancode filter value or mask
1196 * @device: the device descriptor
1197 * @attr: the device attribute struct
1198 * @buf: a pointer to the output buffer
1199 *
1200 * This routine is a callback routine to read a scancode filter value or mask.
1201 * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
1202 * It prints the current scancode filter value or mask of the appropriate filter
1203 * type in hexadecimal into @buf and returns the size of the buffer.
1204 *
1205 * Bits of the filter value corresponding to set bits in the filter mask are
1206 * compared against input scancodes and non-matching scancodes are discarded.
1207 *
David Härdeman18726a32017-04-27 17:34:08 -03001208 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001209 * store_filter and show_filter.
1210 */
1211static ssize_t show_filter(struct device *device,
1212 struct device_attribute *attr,
1213 char *buf)
1214{
1215 struct rc_dev *dev = to_rc_dev(device);
1216 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001217 struct rc_scancode_filter *filter;
James Hogan00942d12014-01-17 10:58:49 -03001218 u32 val;
1219
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001220 mutex_lock(&dev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001221
David Härdemanda6e1622014-04-03 20:32:16 -03001222 if (fattr->type == RC_FILTER_NORMAL)
David Härdemanc5540fb2014-04-03 20:32:21 -03001223 filter = &dev->scancode_filter;
James Hogan00942d12014-01-17 10:58:49 -03001224 else
David Härdemanc5540fb2014-04-03 20:32:21 -03001225 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001226
David Härdemanda6e1622014-04-03 20:32:16 -03001227 if (fattr->mask)
1228 val = filter->mask;
1229 else
1230 val = filter->data;
James Hogan00942d12014-01-17 10:58:49 -03001231 mutex_unlock(&dev->lock);
1232
1233 return sprintf(buf, "%#x\n", val);
1234}
1235
1236/**
1237 * store_filter() - changes the scancode filter value
1238 * @device: the device descriptor
1239 * @attr: the device attribute struct
1240 * @buf: a pointer to the input buffer
1241 * @len: length of the input buffer
1242 *
1243 * This routine is for changing a scancode filter value or mask.
1244 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
1245 * Returns -EINVAL if an invalid filter value for the current protocol was
1246 * specified or if scancode filtering is not supported by the driver, otherwise
1247 * returns @len.
1248 *
1249 * Bits of the filter value corresponding to set bits in the filter mask are
1250 * compared against input scancodes and non-matching scancodes are discarded.
1251 *
David Härdeman18726a32017-04-27 17:34:08 -03001252 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001253 * store_filter and show_filter.
1254 */
1255static ssize_t store_filter(struct device *device,
1256 struct device_attribute *attr,
David Härdemanda6e1622014-04-03 20:32:16 -03001257 const char *buf, size_t len)
James Hogan00942d12014-01-17 10:58:49 -03001258{
1259 struct rc_dev *dev = to_rc_dev(device);
1260 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001261 struct rc_scancode_filter new_filter, *filter;
James Hogan00942d12014-01-17 10:58:49 -03001262 int ret;
1263 unsigned long val;
David Härdeman23c843b2014-04-04 19:06:01 -03001264 int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
James Hogan00942d12014-01-17 10:58:49 -03001265
James Hogan00942d12014-01-17 10:58:49 -03001266 ret = kstrtoul(buf, 0, &val);
1267 if (ret < 0)
1268 return ret;
1269
David Härdemanda6e1622014-04-03 20:32:16 -03001270 if (fattr->type == RC_FILTER_NORMAL) {
1271 set_filter = dev->s_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001272 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001273 } else {
1274 set_filter = dev->s_wakeup_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001275 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001276 }
1277
David Härdeman99b0f3c2014-04-04 19:06:06 -03001278 if (!set_filter)
1279 return -EINVAL;
James Hogan00942d12014-01-17 10:58:49 -03001280
1281 mutex_lock(&dev->lock);
1282
David Härdemanda6e1622014-04-03 20:32:16 -03001283 new_filter = *filter;
James Hogan00942d12014-01-17 10:58:49 -03001284 if (fattr->mask)
David Härdemanda6e1622014-04-03 20:32:16 -03001285 new_filter.mask = val;
James Hogan00942d12014-01-17 10:58:49 -03001286 else
David Härdemanda6e1622014-04-03 20:32:16 -03001287 new_filter.data = val;
David Härdeman23c843b2014-04-04 19:06:01 -03001288
Sean Young0751d332016-12-05 17:08:35 -02001289 if (fattr->type == RC_FILTER_WAKEUP) {
Sean Youngb590c0b2016-12-05 19:24:59 -02001290 /*
1291 * Refuse to set a filter unless a protocol is enabled
1292 * and the filter is valid for that protocol
1293 */
Sean Young6d741bf2017-08-07 16:20:58 -04001294 if (dev->wakeup_protocol != RC_PROTO_UNKNOWN)
James Hoganf423ccc2015-03-31 14:48:10 -03001295 ret = rc_validate_filter(dev, &new_filter);
Sean Youngb590c0b2016-12-05 19:24:59 -02001296 else
Sean Young0751d332016-12-05 17:08:35 -02001297 ret = -EINVAL;
Sean Youngb590c0b2016-12-05 19:24:59 -02001298
1299 if (ret != 0)
Sean Young0751d332016-12-05 17:08:35 -02001300 goto unlock;
Sean Young0751d332016-12-05 17:08:35 -02001301 }
1302
1303 if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
1304 val) {
James Hogan6bea25a2014-02-28 20:17:06 -03001305 /* refuse to set a filter unless a protocol is enabled */
1306 ret = -EINVAL;
1307 goto unlock;
1308 }
David Härdeman23c843b2014-04-04 19:06:01 -03001309
David Härdemanda6e1622014-04-03 20:32:16 -03001310 ret = set_filter(dev, &new_filter);
David Härdeman99b0f3c2014-04-04 19:06:06 -03001311 if (ret < 0)
1312 goto unlock;
James Hogan00942d12014-01-17 10:58:49 -03001313
David Härdemanda6e1622014-04-03 20:32:16 -03001314 *filter = new_filter;
James Hogan00942d12014-01-17 10:58:49 -03001315
1316unlock:
1317 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001318 return (ret < 0) ? ret : len;
James Hogan00942d12014-01-17 10:58:49 -03001319}
1320
Sean Young0751d332016-12-05 17:08:35 -02001321/**
1322 * show_wakeup_protocols() - shows the wakeup IR protocol
1323 * @device: the device descriptor
1324 * @mattr: the device attribute struct
1325 * @buf: a pointer to the output buffer
1326 *
1327 * This routine is a callback routine for input read the IR protocol type(s).
1328 * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols.
1329 * It returns the protocol names of supported protocols.
1330 * The enabled protocols are printed in brackets.
1331 *
David Härdeman18726a32017-04-27 17:34:08 -03001332 * dev->lock is taken to guard against races between
1333 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001334 */
1335static ssize_t show_wakeup_protocols(struct device *device,
1336 struct device_attribute *mattr,
1337 char *buf)
1338{
1339 struct rc_dev *dev = to_rc_dev(device);
1340 u64 allowed;
Sean Young6d741bf2017-08-07 16:20:58 -04001341 enum rc_proto enabled;
Sean Young0751d332016-12-05 17:08:35 -02001342 char *tmp = buf;
1343 int i;
1344
Sean Young0751d332016-12-05 17:08:35 -02001345 mutex_lock(&dev->lock);
1346
1347 allowed = dev->allowed_wakeup_protocols;
1348 enabled = dev->wakeup_protocol;
1349
1350 mutex_unlock(&dev->lock);
1351
1352 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - %d\n",
1353 __func__, (long long)allowed, enabled);
1354
Sean Youngd57ea872017-08-09 13:19:16 -04001355 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001356 if (allowed & (1ULL << i)) {
1357 if (i == enabled)
Sean Youngd57ea872017-08-09 13:19:16 -04001358 tmp += sprintf(tmp, "[%s] ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001359 else
Sean Youngd57ea872017-08-09 13:19:16 -04001360 tmp += sprintf(tmp, "%s ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001361 }
1362 }
1363
1364 if (tmp != buf)
1365 tmp--;
1366 *tmp = '\n';
1367
1368 return tmp + 1 - buf;
1369}
1370
1371/**
1372 * store_wakeup_protocols() - changes the wakeup IR protocol(s)
1373 * @device: the device descriptor
1374 * @mattr: the device attribute struct
1375 * @buf: a pointer to the input buffer
1376 * @len: length of the input buffer
1377 *
1378 * This routine is for changing the IR protocol type.
1379 * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols.
1380 * Returns @len on success or a negative error code.
1381 *
David Härdeman18726a32017-04-27 17:34:08 -03001382 * dev->lock is taken to guard against races between
1383 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001384 */
1385static ssize_t store_wakeup_protocols(struct device *device,
1386 struct device_attribute *mattr,
1387 const char *buf, size_t len)
1388{
1389 struct rc_dev *dev = to_rc_dev(device);
Sean Young6d741bf2017-08-07 16:20:58 -04001390 enum rc_proto protocol;
Sean Young0751d332016-12-05 17:08:35 -02001391 ssize_t rc;
1392 u64 allowed;
1393 int i;
1394
Sean Young0751d332016-12-05 17:08:35 -02001395 mutex_lock(&dev->lock);
1396
1397 allowed = dev->allowed_wakeup_protocols;
1398
1399 if (sysfs_streq(buf, "none")) {
Sean Young6d741bf2017-08-07 16:20:58 -04001400 protocol = RC_PROTO_UNKNOWN;
Sean Young0751d332016-12-05 17:08:35 -02001401 } else {
Sean Youngd57ea872017-08-09 13:19:16 -04001402 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001403 if ((allowed & (1ULL << i)) &&
Sean Youngd57ea872017-08-09 13:19:16 -04001404 sysfs_streq(buf, protocols[i].name)) {
Sean Young0751d332016-12-05 17:08:35 -02001405 protocol = i;
1406 break;
1407 }
1408 }
1409
Sean Youngd57ea872017-08-09 13:19:16 -04001410 if (i == ARRAY_SIZE(protocols)) {
Sean Young0751d332016-12-05 17:08:35 -02001411 rc = -EINVAL;
1412 goto out;
1413 }
James Hoganf423ccc2015-03-31 14:48:10 -03001414
1415 if (dev->encode_wakeup) {
1416 u64 mask = 1ULL << protocol;
1417
1418 ir_raw_load_modules(&mask);
1419 if (!mask) {
1420 rc = -EINVAL;
1421 goto out;
1422 }
1423 }
Sean Young0751d332016-12-05 17:08:35 -02001424 }
1425
1426 if (dev->wakeup_protocol != protocol) {
1427 dev->wakeup_protocol = protocol;
1428 IR_dprintk(1, "Wakeup protocol changed to %d\n", protocol);
1429
Sean Young6d741bf2017-08-07 16:20:58 -04001430 if (protocol == RC_PROTO_RC6_MCE)
Sean Young0751d332016-12-05 17:08:35 -02001431 dev->scancode_wakeup_filter.data = 0x800f0000;
1432 else
1433 dev->scancode_wakeup_filter.data = 0;
1434 dev->scancode_wakeup_filter.mask = 0;
1435
1436 rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
1437 if (rc == 0)
1438 rc = len;
1439 } else {
1440 rc = len;
1441 }
1442
1443out:
1444 mutex_unlock(&dev->lock);
1445 return rc;
1446}
1447
David Härdemand8b4b582010-10-29 16:08:23 -03001448static void rc_dev_release(struct device *device)
1449{
Max Kellermann47cae1e2016-03-21 08:33:05 -03001450 struct rc_dev *dev = to_rc_dev(device);
1451
1452 kfree(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001453}
1454
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001455#define ADD_HOTPLUG_VAR(fmt, val...) \
1456 do { \
1457 int err = add_uevent_var(env, fmt, val); \
1458 if (err) \
1459 return err; \
1460 } while (0)
1461
1462static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1463{
David Härdemand8b4b582010-10-29 16:08:23 -03001464 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001465
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001466 if (dev->rc_map.name)
1467 ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
David Härdemand8b4b582010-10-29 16:08:23 -03001468 if (dev->driver_name)
1469 ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
Sean Youngb9f407e2017-09-01 11:34:23 -03001470 if (dev->device_name)
1471 ADD_HOTPLUG_VAR("DEV_NAME=%s", dev->device_name);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001472
1473 return 0;
1474}
1475
1476/*
1477 * Static device attribute struct with the sysfs attributes for IR's
1478 */
Sean Young6d75db32017-09-01 11:30:50 -03001479static struct device_attribute dev_attr_ro_protocols =
1480__ATTR(protocols, 0444, show_protocols, NULL);
1481static struct device_attribute dev_attr_rw_protocols =
1482__ATTR(protocols, 0644, show_protocols, store_protocols);
Sean Young0751d332016-12-05 17:08:35 -02001483static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
1484 store_wakeup_protocols);
James Hogan00942d12014-01-17 10:58:49 -03001485static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
1486 show_filter, store_filter, RC_FILTER_NORMAL, false);
1487static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
1488 show_filter, store_filter, RC_FILTER_NORMAL, true);
1489static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
1490 show_filter, store_filter, RC_FILTER_WAKEUP, false);
1491static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
1492 show_filter, store_filter, RC_FILTER_WAKEUP, true);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001493
Sean Young6d75db32017-09-01 11:30:50 -03001494static struct attribute *rc_dev_rw_protocol_attrs[] = {
1495 &dev_attr_rw_protocols.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001496 NULL,
1497};
1498
Sean Young6d75db32017-09-01 11:30:50 -03001499static const struct attribute_group rc_dev_rw_protocol_attr_grp = {
1500 .attrs = rc_dev_rw_protocol_attrs,
1501};
1502
1503static struct attribute *rc_dev_ro_protocol_attrs[] = {
1504 &dev_attr_ro_protocols.attr,
1505 NULL,
1506};
1507
1508static const struct attribute_group rc_dev_ro_protocol_attr_grp = {
1509 .attrs = rc_dev_ro_protocol_attrs,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001510};
1511
David Härdeman99b0f3c2014-04-04 19:06:06 -03001512static struct attribute *rc_dev_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001513 &dev_attr_filter.attr.attr,
1514 &dev_attr_filter_mask.attr.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001515 NULL,
1516};
1517
Arvind Yadavdb681022017-07-07 04:23:54 -04001518static const struct attribute_group rc_dev_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001519 .attrs = rc_dev_filter_attrs,
1520};
1521
1522static struct attribute *rc_dev_wakeup_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001523 &dev_attr_wakeup_filter.attr.attr,
1524 &dev_attr_wakeup_filter_mask.attr.attr,
Sean Young0751d332016-12-05 17:08:35 -02001525 &dev_attr_wakeup_protocols.attr,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001526 NULL,
1527};
1528
Arvind Yadavdb681022017-07-07 04:23:54 -04001529static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001530 .attrs = rc_dev_wakeup_filter_attrs,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001531};
1532
Bhumika Goyalf03f02f2017-08-19 05:22:15 -03001533static const struct device_type rc_dev_type = {
David Härdemand8b4b582010-10-29 16:08:23 -03001534 .release = rc_dev_release,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001535 .uevent = rc_dev_uevent,
1536};
1537
Andi Shyti0f7499f2016-12-16 06:50:58 -02001538struct rc_dev *rc_allocate_device(enum rc_driver_type type)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001539{
David Härdemand8b4b582010-10-29 16:08:23 -03001540 struct rc_dev *dev;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001541
David Härdemand8b4b582010-10-29 16:08:23 -03001542 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1543 if (!dev)
1544 return NULL;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001545
Andi Shytid34aee12016-12-16 04:12:15 -02001546 if (type != RC_DRIVER_IR_RAW_TX) {
1547 dev->input_dev = input_allocate_device();
1548 if (!dev->input_dev) {
1549 kfree(dev);
1550 return NULL;
1551 }
1552
1553 dev->input_dev->getkeycode = ir_getkeycode;
1554 dev->input_dev->setkeycode = ir_setkeycode;
1555 input_set_drvdata(dev->input_dev, dev);
1556
Kees Cookb17ec782017-10-24 11:23:14 -04001557 timer_setup(&dev->timer_keyup, ir_timer_keyup, 0);
Andi Shytid34aee12016-12-16 04:12:15 -02001558
1559 spin_lock_init(&dev->rc_map.lock);
1560 spin_lock_init(&dev->keylock);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001561 }
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001562 mutex_init(&dev->lock);
David Härdemand8b4b582010-10-29 16:08:23 -03001563
1564 dev->dev.type = &rc_dev_type;
David Härdeman40fc5322013-03-06 16:52:10 -03001565 dev->dev.class = &rc_class;
David Härdemand8b4b582010-10-29 16:08:23 -03001566 device_initialize(&dev->dev);
1567
Andi Shyti0f7499f2016-12-16 06:50:58 -02001568 dev->driver_type = type;
1569
David Härdemand8b4b582010-10-29 16:08:23 -03001570 __module_get(THIS_MODULE);
1571 return dev;
1572}
1573EXPORT_SYMBOL_GPL(rc_allocate_device);
1574
1575void rc_free_device(struct rc_dev *dev)
1576{
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001577 if (!dev)
1578 return;
1579
Markus Elfring3dd94f02014-11-20 09:01:32 -03001580 input_free_device(dev->input_dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001581
1582 put_device(&dev->dev);
1583
Max Kellermann47cae1e2016-03-21 08:33:05 -03001584 /* kfree(dev) will be called by the callback function
1585 rc_dev_release() */
1586
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001587 module_put(THIS_MODULE);
David Härdemand8b4b582010-10-29 16:08:23 -03001588}
1589EXPORT_SYMBOL_GPL(rc_free_device);
1590
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001591static void devm_rc_alloc_release(struct device *dev, void *res)
1592{
1593 rc_free_device(*(struct rc_dev **)res);
1594}
1595
Andi Shyti0f7499f2016-12-16 06:50:58 -02001596struct rc_dev *devm_rc_allocate_device(struct device *dev,
1597 enum rc_driver_type type)
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001598{
1599 struct rc_dev **dr, *rc;
1600
1601 dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
1602 if (!dr)
1603 return NULL;
1604
Andi Shyti0f7499f2016-12-16 06:50:58 -02001605 rc = rc_allocate_device(type);
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001606 if (!rc) {
1607 devres_free(dr);
1608 return NULL;
1609 }
1610
1611 rc->dev.parent = dev;
1612 rc->managed_alloc = true;
1613 *dr = rc;
1614 devres_add(dev, dr);
1615
1616 return rc;
1617}
1618EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
1619
David Härdemanf56928a2017-05-03 07:04:00 -03001620static int rc_prepare_rx_device(struct rc_dev *dev)
David Härdemand8b4b582010-10-29 16:08:23 -03001621{
David Härdemanfcb13092015-05-19 19:03:17 -03001622 int rc;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001623 struct rc_map *rc_map;
Sean Young6d741bf2017-08-07 16:20:58 -04001624 u64 rc_proto;
David Härdemand8b4b582010-10-29 16:08:23 -03001625
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001626 if (!dev->map_name)
David Härdemand8b4b582010-10-29 16:08:23 -03001627 return -EINVAL;
1628
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001629 rc_map = rc_map_get(dev->map_name);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001630 if (!rc_map)
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001631 rc_map = rc_map_get(RC_MAP_EMPTY);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001632 if (!rc_map || !rc_map->scan || rc_map->size == 0)
David Härdemand8b4b582010-10-29 16:08:23 -03001633 return -EINVAL;
1634
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001635 rc = ir_setkeytable(dev, rc_map);
1636 if (rc)
1637 return rc;
1638
Sean Young6d741bf2017-08-07 16:20:58 -04001639 rc_proto = BIT_ULL(rc_map->rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001640
Sean Young831c4c82017-09-01 09:55:59 -03001641 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1642 dev->enabled_protocols = dev->allowed_protocols;
1643
Sean Young41380862017-02-22 18:48:01 -03001644 if (dev->change_protocol) {
Sean Young6d741bf2017-08-07 16:20:58 -04001645 rc = dev->change_protocol(dev, &rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001646 if (rc < 0)
1647 goto out_table;
Sean Young6d741bf2017-08-07 16:20:58 -04001648 dev->enabled_protocols = rc_proto;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001649 }
1650
Sean Young41380862017-02-22 18:48:01 -03001651 if (dev->driver_type == RC_DRIVER_IR_RAW)
Sean Young6d741bf2017-08-07 16:20:58 -04001652 ir_raw_load_modules(&rc_proto);
Sean Young41380862017-02-22 18:48:01 -03001653
David Härdemand8b4b582010-10-29 16:08:23 -03001654 set_bit(EV_KEY, dev->input_dev->evbit);
1655 set_bit(EV_REP, dev->input_dev->evbit);
1656 set_bit(EV_MSC, dev->input_dev->evbit);
1657 set_bit(MSC_SCAN, dev->input_dev->mscbit);
1658 if (dev->open)
1659 dev->input_dev->open = ir_open;
1660 if (dev->close)
1661 dev->input_dev->close = ir_close;
1662
David Härdemanb2aceb72017-04-27 17:33:58 -03001663 dev->input_dev->dev.parent = &dev->dev;
1664 memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
1665 dev->input_dev->phys = dev->input_phys;
Sean Young518f4b22017-07-01 12:13:19 -04001666 dev->input_dev->name = dev->device_name;
David Härdemanb2aceb72017-04-27 17:33:58 -03001667
David Härdemanf56928a2017-05-03 07:04:00 -03001668 return 0;
1669
1670out_table:
1671 ir_free_table(&dev->rc_map);
1672
1673 return rc;
1674}
1675
1676static int rc_setup_rx_device(struct rc_dev *dev)
1677{
1678 int rc;
1679
David Härdemanb2aceb72017-04-27 17:33:58 -03001680 /* rc_open will be called here */
1681 rc = input_register_device(dev->input_dev);
1682 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001683 return rc;
David Härdemanb2aceb72017-04-27 17:33:58 -03001684
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001685 /*
1686 * Default delay of 250ms is too short for some protocols, especially
1687 * since the timeout is currently set to 250ms. Increase it to 500ms,
1688 * to avoid wrong repetition of the keycodes. Note that this must be
1689 * set after the call to input_register_device().
1690 */
1691 dev->input_dev->rep[REP_DELAY] = 500;
1692
1693 /*
1694 * As a repeat event on protocols like RC-5 and NEC take as long as
1695 * 110/114ms, using 33ms as a repeat period is not the right thing
1696 * to do.
1697 */
1698 dev->input_dev->rep[REP_PERIOD] = 125;
1699
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001700 return 0;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001701}
1702
1703static void rc_free_rx_device(struct rc_dev *dev)
1704{
David Härdemanf56928a2017-05-03 07:04:00 -03001705 if (!dev)
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001706 return;
1707
David Härdemanf56928a2017-05-03 07:04:00 -03001708 if (dev->input_dev) {
1709 input_unregister_device(dev->input_dev);
1710 dev->input_dev = NULL;
1711 }
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001712
David Härdemanf56928a2017-05-03 07:04:00 -03001713 ir_free_table(&dev->rc_map);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001714}
1715
1716int rc_register_device(struct rc_dev *dev)
1717{
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001718 const char *path;
1719 int attr = 0;
1720 int minor;
1721 int rc;
1722
1723 if (!dev)
1724 return -EINVAL;
1725
David Härdemanfcb13092015-05-19 19:03:17 -03001726 minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
1727 if (minor < 0)
1728 return minor;
1729
1730 dev->minor = minor;
1731 dev_set_name(&dev->dev, "rc%u", dev->minor);
1732 dev_set_drvdata(&dev->dev, dev);
Mauro Carvalho Chehab587d1b02014-01-14 16:27:55 -03001733
David Härdeman99b0f3c2014-04-04 19:06:06 -03001734 dev->dev.groups = dev->sysfs_groups;
Sean Young6d75db32017-09-01 11:30:50 -03001735 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1736 dev->sysfs_groups[attr++] = &rc_dev_ro_protocol_attr_grp;
1737 else if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
1738 dev->sysfs_groups[attr++] = &rc_dev_rw_protocol_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001739 if (dev->s_filter)
David Härdeman120703f2014-04-03 20:31:30 -03001740 dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001741 if (dev->s_wakeup_filter)
1742 dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001743 dev->sysfs_groups[attr++] = NULL;
1744
Sean Younga60d64b2017-09-23 10:41:13 -04001745 if (dev->driver_type == RC_DRIVER_IR_RAW) {
David Härdemanf56928a2017-05-03 07:04:00 -03001746 rc = ir_raw_event_prepare(dev);
1747 if (rc < 0)
1748 goto out_minor;
1749 }
1750
1751 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1752 rc = rc_prepare_rx_device(dev);
1753 if (rc)
1754 goto out_raw;
1755 }
1756
David Härdemand8b4b582010-10-29 16:08:23 -03001757 rc = device_add(&dev->dev);
1758 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001759 goto out_rx_free;
David Härdemand8b4b582010-10-29 16:08:23 -03001760
David Härdemand8b4b582010-10-29 16:08:23 -03001761 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Heiner Kallweit4dc0e902015-10-29 19:39:06 -02001762 dev_info(&dev->dev, "%s as %s\n",
Sean Young518f4b22017-07-01 12:13:19 -04001763 dev->device_name ?: "Unspecified device", path ?: "N/A");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001764 kfree(path);
1765
Sean Young5df62772017-02-23 06:11:21 -03001766 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1767 rc = rc_setup_rx_device(dev);
1768 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001769 goto out_dev;
1770 }
1771
Sean Younga60d64b2017-09-23 10:41:13 -04001772 /* Ensure that the lirc kfifo is setup before we start the thread */
1773 if (dev->driver_type != RC_DRIVER_SCANCODE) {
1774 rc = ir_lirc_register(dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001775 if (rc < 0)
1776 goto out_rx;
David Härdemand8b4b582010-10-29 16:08:23 -03001777 }
1778
Sean Younga60d64b2017-09-23 10:41:13 -04001779 if (dev->driver_type == RC_DRIVER_IR_RAW) {
1780 rc = ir_raw_event_register(dev);
1781 if (rc < 0)
1782 goto out_lirc;
1783 }
1784
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001785 IR_dprintk(1, "Registered rc%u (driver: %s)\n",
David Härdemanfcb13092015-05-19 19:03:17 -03001786 dev->minor,
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001787 dev->driver_name ? dev->driver_name : "unknown");
David Härdemand8b4b582010-10-29 16:08:23 -03001788
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001789 return 0;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001790
Sean Younga60d64b2017-09-23 10:41:13 -04001791out_lirc:
1792 if (dev->driver_type != RC_DRIVER_SCANCODE)
1793 ir_lirc_unregister(dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001794out_rx:
1795 rc_free_rx_device(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001796out_dev:
1797 device_del(&dev->dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001798out_rx_free:
1799 ir_free_table(&dev->rc_map);
1800out_raw:
1801 ir_raw_event_free(dev);
1802out_minor:
David Härdemanfcb13092015-05-19 19:03:17 -03001803 ida_simple_remove(&rc_ida, minor);
David Härdemand8b4b582010-10-29 16:08:23 -03001804 return rc;
1805}
1806EXPORT_SYMBOL_GPL(rc_register_device);
1807
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001808static void devm_rc_release(struct device *dev, void *res)
1809{
1810 rc_unregister_device(*(struct rc_dev **)res);
1811}
1812
1813int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
1814{
1815 struct rc_dev **dr;
1816 int ret;
1817
1818 dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
1819 if (!dr)
1820 return -ENOMEM;
1821
1822 ret = rc_register_device(dev);
1823 if (ret) {
1824 devres_free(dr);
1825 return ret;
1826 }
1827
1828 *dr = dev;
1829 devres_add(parent, dr);
1830
1831 return 0;
1832}
1833EXPORT_SYMBOL_GPL(devm_rc_register_device);
1834
David Härdemand8b4b582010-10-29 16:08:23 -03001835void rc_unregister_device(struct rc_dev *dev)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001836{
David Härdemand8b4b582010-10-29 16:08:23 -03001837 if (!dev)
1838 return;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001839
David Härdemand8b4b582010-10-29 16:08:23 -03001840 del_timer_sync(&dev->timer_keyup);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001841
David Härdemand8b4b582010-10-29 16:08:23 -03001842 if (dev->driver_type == RC_DRIVER_IR_RAW)
1843 ir_raw_event_unregister(dev);
1844
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001845 rc_free_rx_device(dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001846
Sean Younga60d64b2017-09-23 10:41:13 -04001847 if (dev->driver_type != RC_DRIVER_SCANCODE)
1848 ir_lirc_unregister(dev);
1849
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001850 device_del(&dev->dev);
1851
David Härdemanfcb13092015-05-19 19:03:17 -03001852 ida_simple_remove(&rc_ida, dev->minor);
1853
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001854 if (!dev->managed_alloc)
1855 rc_free_device(dev);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001856}
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001857
David Härdemand8b4b582010-10-29 16:08:23 -03001858EXPORT_SYMBOL_GPL(rc_unregister_device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001859
1860/*
1861 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1862 */
1863
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001864static int __init rc_core_init(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001865{
David Härdeman40fc5322013-03-06 16:52:10 -03001866 int rc = class_register(&rc_class);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001867 if (rc) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -02001868 pr_err("rc_core: unable to register rc class\n");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001869 return rc;
1870 }
1871
Sean Younga60d64b2017-09-23 10:41:13 -04001872 rc = lirc_dev_init();
1873 if (rc) {
1874 pr_err("rc_core: unable to init lirc\n");
1875 class_unregister(&rc_class);
1876 return 0;
1877 }
1878
Sean Young153a60b2013-07-30 19:00:01 -03001879 led_trigger_register_simple("rc-feedback", &led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001880 rc_map_register(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001881
1882 return 0;
1883}
1884
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001885static void __exit rc_core_exit(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001886{
Sean Younga60d64b2017-09-23 10:41:13 -04001887 lirc_dev_exit();
David Härdeman40fc5322013-03-06 16:52:10 -03001888 class_unregister(&rc_class);
Sean Young153a60b2013-07-30 19:00:01 -03001889 led_trigger_unregister_simple(led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001890 rc_map_unregister(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001891}
1892
David Härdemane76d4ce2013-03-06 16:52:15 -03001893subsys_initcall(rc_core_init);
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001894module_exit(rc_core_exit);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001895
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001896int rc_core_debug; /* ir_debug level (0,1,2) */
1897EXPORT_SYMBOL_GPL(rc_core_debug);
1898module_param_named(debug, rc_core_debug, int, 0644);
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03001899
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02001900MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehab20835282017-12-01 08:47:08 -05001901MODULE_LICENSE("GPL v2");