blob: 29a90adb0f7c6620253d7fa2fef8612371a56c7d [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}
849EXPORT_SYMBOL_GPL(rc_open);
850
David Härdemand8b4b582010-10-29 16:08:23 -0300851static int ir_open(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300852{
David Härdemand8b4b582010-10-29 16:08:23 -0300853 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300854
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300855 return rc_open(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300856}
857
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300858void rc_close(struct rc_dev *rdev)
859{
860 if (rdev) {
861 mutex_lock(&rdev->lock);
862
Mauro Carvalho Chehab81b7d142015-04-28 09:43:17 -0300863 if (!--rdev->users && rdev->close != NULL)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300864 rdev->close(rdev);
865
866 mutex_unlock(&rdev->lock);
867 }
868}
869EXPORT_SYMBOL_GPL(rc_close);
870
David Härdemand8b4b582010-10-29 16:08:23 -0300871static void ir_close(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300872{
David Härdemand8b4b582010-10-29 16:08:23 -0300873 struct rc_dev *rdev = input_get_drvdata(idev);
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300874 rc_close(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300875}
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300876
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300877/* class for /sys/class/rc */
David Härdeman40fc5322013-03-06 16:52:10 -0300878static char *rc_devnode(struct device *dev, umode_t *mode)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300879{
880 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
881}
882
David Härdeman40fc5322013-03-06 16:52:10 -0300883static struct class rc_class = {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300884 .name = "rc",
David Härdeman40fc5322013-03-06 16:52:10 -0300885 .devnode = rc_devnode,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300886};
887
David Härdemanc003ab12012-10-11 19:11:54 -0300888/*
889 * These are the protocol textual descriptions that are
890 * used by the sysfs protocols file. Note that the order
891 * of the entries is relevant.
892 */
Heiner Kallweit53df8772015-11-16 17:52:17 -0200893static const struct {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300894 u64 type;
Heiner Kallweit53df8772015-11-16 17:52:17 -0200895 const char *name;
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200896 const char *module_name;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300897} proto_names[] = {
Sean Young6d741bf2017-08-07 16:20:58 -0400898 { RC_PROTO_BIT_NONE, "none", NULL },
899 { RC_PROTO_BIT_OTHER, "other", NULL },
900 { RC_PROTO_BIT_UNKNOWN, "unknown", NULL },
901 { RC_PROTO_BIT_RC5 |
902 RC_PROTO_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
903 { RC_PROTO_BIT_NEC |
904 RC_PROTO_BIT_NECX |
905 RC_PROTO_BIT_NEC32, "nec", "ir-nec-decoder" },
906 { RC_PROTO_BIT_RC6_0 |
907 RC_PROTO_BIT_RC6_6A_20 |
908 RC_PROTO_BIT_RC6_6A_24 |
909 RC_PROTO_BIT_RC6_6A_32 |
910 RC_PROTO_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
911 { RC_PROTO_BIT_JVC, "jvc", "ir-jvc-decoder" },
912 { RC_PROTO_BIT_SONY12 |
913 RC_PROTO_BIT_SONY15 |
914 RC_PROTO_BIT_SONY20, "sony", "ir-sony-decoder" },
915 { RC_PROTO_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
916 { RC_PROTO_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
917 { RC_PROTO_BIT_SHARP, "sharp", "ir-sharp-decoder" },
918 { RC_PROTO_BIT_MCIR2_KBD |
919 RC_PROTO_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" },
920 { RC_PROTO_BIT_XMP, "xmp", "ir-xmp-decoder" },
921 { RC_PROTO_BIT_CEC, "cec", NULL },
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300922};
923
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300924/**
James Hoganab88c662014-02-28 20:17:05 -0300925 * struct rc_filter_attribute - Device attribute relating to a filter type.
926 * @attr: Device attribute.
927 * @type: Filter type.
928 * @mask: false for filter value, true for filter mask.
929 */
930struct rc_filter_attribute {
931 struct device_attribute attr;
932 enum rc_filter_type type;
933 bool mask;
934};
935#define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
936
James Hoganab88c662014-02-28 20:17:05 -0300937#define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
938 struct rc_filter_attribute dev_attr_##_name = { \
939 .attr = __ATTR(_name, _mode, _show, _store), \
940 .type = (_type), \
941 .mask = (_mask), \
942 }
943
David Härdemandd6ff6a2015-07-22 17:55:24 -0300944static bool lirc_is_present(void)
945{
946#if defined(CONFIG_LIRC_MODULE)
947 struct module *lirc;
948
949 mutex_lock(&module_mutex);
950 lirc = find_module("lirc_dev");
951 mutex_unlock(&module_mutex);
952
953 return lirc ? true : false;
954#elif defined(CONFIG_LIRC)
955 return true;
956#else
957 return false;
958#endif
959}
960
James Hoganab88c662014-02-28 20:17:05 -0300961/**
Sean Young0751d332016-12-05 17:08:35 -0200962 * show_protocols() - shows the current IR protocol(s)
David Härdemand8b4b582010-10-29 16:08:23 -0300963 * @device: the device descriptor
David Härdemanda6e1622014-04-03 20:32:16 -0300964 * @mattr: the device attribute struct
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300965 * @buf: a pointer to the output buffer
966 *
967 * This routine is a callback routine for input read the IR protocol type(s).
Sean Young0751d332016-12-05 17:08:35 -0200968 * it is trigged by reading /sys/class/rc/rc?/protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300969 * It returns the protocol names of supported protocols.
970 * Enabled protocols are printed in brackets.
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300971 *
David Härdeman18726a32017-04-27 17:34:08 -0300972 * dev->lock is taken to guard against races between
973 * store_protocols and show_protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300974 */
David Härdemand8b4b582010-10-29 16:08:23 -0300975static ssize_t show_protocols(struct device *device,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300976 struct device_attribute *mattr, char *buf)
977{
David Härdemand8b4b582010-10-29 16:08:23 -0300978 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300979 u64 allowed, enabled;
980 char *tmp = buf;
981 int i;
982
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300983 mutex_lock(&dev->lock);
984
Sean Young0751d332016-12-05 17:08:35 -0200985 enabled = dev->enabled_protocols;
986 allowed = dev->allowed_protocols;
987 if (dev->raw && !allowed)
988 allowed = ir_raw_get_allowed_protocols();
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300989
David Härdemanda6e1622014-04-03 20:32:16 -0300990 mutex_unlock(&dev->lock);
991
992 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
993 __func__, (long long)allowed, (long long)enabled);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300994
995 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
996 if (allowed & enabled & proto_names[i].type)
997 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
998 else if (allowed & proto_names[i].type)
999 tmp += sprintf(tmp, "%s ", proto_names[i].name);
David Härdemanc003ab12012-10-11 19:11:54 -03001000
1001 if (allowed & proto_names[i].type)
1002 allowed &= ~proto_names[i].type;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001003 }
1004
David Härdemandd6ff6a2015-07-22 17:55:24 -03001005 if (dev->driver_type == RC_DRIVER_IR_RAW && lirc_is_present())
David Härdeman275ddb42015-05-19 19:03:22 -03001006 tmp += sprintf(tmp, "[lirc] ");
1007
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001008 if (tmp != buf)
1009 tmp--;
1010 *tmp = '\n';
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001011
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001012 return tmp + 1 - buf;
1013}
1014
1015/**
David Härdemanda6e1622014-04-03 20:32:16 -03001016 * parse_protocol_change() - parses a protocol change request
1017 * @protocols: pointer to the bitmask of current protocols
1018 * @buf: pointer to the buffer with a list of changes
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001019 *
David Härdemanda6e1622014-04-03 20:32:16 -03001020 * Writing "+proto" will add a protocol to the protocol mask.
1021 * Writing "-proto" will remove a protocol from protocol mask.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001022 * Writing "proto" will enable only "proto".
1023 * Writing "none" will disable all protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001024 * Returns the number of changes performed or a negative error code.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001025 */
David Härdemanda6e1622014-04-03 20:32:16 -03001026static int parse_protocol_change(u64 *protocols, const char *buf)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001027{
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001028 const char *tmp;
David Härdemanda6e1622014-04-03 20:32:16 -03001029 unsigned count = 0;
1030 bool enable, disable;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001031 u64 mask;
David Härdemanda6e1622014-04-03 20:32:16 -03001032 int i;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001033
David Härdemanda6e1622014-04-03 20:32:16 -03001034 while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001035 if (!*tmp)
1036 break;
1037
1038 if (*tmp == '+') {
1039 enable = true;
1040 disable = false;
1041 tmp++;
1042 } else if (*tmp == '-') {
1043 enable = false;
1044 disable = true;
1045 tmp++;
1046 } else {
1047 enable = false;
1048 disable = false;
1049 }
1050
David Härdemanc003ab12012-10-11 19:11:54 -03001051 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1052 if (!strcasecmp(tmp, proto_names[i].name)) {
1053 mask = proto_names[i].type;
1054 break;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001055 }
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001056 }
1057
David Härdemanc003ab12012-10-11 19:11:54 -03001058 if (i == ARRAY_SIZE(proto_names)) {
David Härdeman275ddb42015-05-19 19:03:22 -03001059 if (!strcasecmp(tmp, "lirc"))
1060 mask = 0;
1061 else {
1062 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
1063 return -EINVAL;
1064 }
David Härdemanc003ab12012-10-11 19:11:54 -03001065 }
1066
1067 count++;
1068
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001069 if (enable)
David Härdemanda6e1622014-04-03 20:32:16 -03001070 *protocols |= mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001071 else if (disable)
David Härdemanda6e1622014-04-03 20:32:16 -03001072 *protocols &= ~mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001073 else
David Härdemanda6e1622014-04-03 20:32:16 -03001074 *protocols = mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001075 }
1076
1077 if (!count) {
1078 IR_dprintk(1, "Protocol not specified\n");
David Härdemanda6e1622014-04-03 20:32:16 -03001079 return -EINVAL;
1080 }
1081
1082 return count;
1083}
1084
Sean Young0d39ab02017-02-25 06:51:31 -05001085void ir_raw_load_modules(u64 *protocols)
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001086{
1087 u64 available;
1088 int i, ret;
1089
1090 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
Sean Young6d741bf2017-08-07 16:20:58 -04001091 if (proto_names[i].type == RC_PROTO_BIT_NONE ||
1092 proto_names[i].type & (RC_PROTO_BIT_OTHER |
1093 RC_PROTO_BIT_UNKNOWN))
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001094 continue;
1095
1096 available = ir_raw_get_allowed_protocols();
1097 if (!(*protocols & proto_names[i].type & ~available))
1098 continue;
1099
1100 if (!proto_names[i].module_name) {
1101 pr_err("Can't enable IR protocol %s\n",
1102 proto_names[i].name);
1103 *protocols &= ~proto_names[i].type;
1104 continue;
1105 }
1106
1107 ret = request_module("%s", proto_names[i].module_name);
1108 if (ret < 0) {
1109 pr_err("Couldn't load IR protocol module %s\n",
1110 proto_names[i].module_name);
1111 *protocols &= ~proto_names[i].type;
1112 continue;
1113 }
1114 msleep(20);
1115 available = ir_raw_get_allowed_protocols();
1116 if (!(*protocols & proto_names[i].type & ~available))
1117 continue;
1118
Sean Young8caebcd2017-01-19 19:33:49 -02001119 pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001120 proto_names[i].module_name,
1121 proto_names[i].name);
1122 *protocols &= ~proto_names[i].type;
1123 }
1124}
1125
David Härdemanda6e1622014-04-03 20:32:16 -03001126/**
1127 * store_protocols() - changes the current/wakeup IR protocol(s)
1128 * @device: the device descriptor
1129 * @mattr: the device attribute struct
1130 * @buf: a pointer to the input buffer
1131 * @len: length of the input buffer
1132 *
1133 * This routine is for changing the IR protocol type.
1134 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
1135 * See parse_protocol_change() for the valid commands.
1136 * Returns @len on success or a negative error code.
1137 *
David Härdeman18726a32017-04-27 17:34:08 -03001138 * dev->lock is taken to guard against races between
1139 * store_protocols and show_protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001140 */
1141static ssize_t store_protocols(struct device *device,
1142 struct device_attribute *mattr,
1143 const char *buf, size_t len)
1144{
1145 struct rc_dev *dev = to_rc_dev(device);
David Härdemanda6e1622014-04-03 20:32:16 -03001146 u64 *current_protocols;
David Härdemanda6e1622014-04-03 20:32:16 -03001147 struct rc_scancode_filter *filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001148 u64 old_protocols, new_protocols;
1149 ssize_t rc;
1150
Sean Young0751d332016-12-05 17:08:35 -02001151 IR_dprintk(1, "Normal protocol change requested\n");
1152 current_protocols = &dev->enabled_protocols;
1153 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001154
Sean Young0751d332016-12-05 17:08:35 -02001155 if (!dev->change_protocol) {
David Härdemanda6e1622014-04-03 20:32:16 -03001156 IR_dprintk(1, "Protocol switching not supported\n");
1157 return -EINVAL;
1158 }
1159
1160 mutex_lock(&dev->lock);
1161
1162 old_protocols = *current_protocols;
1163 new_protocols = old_protocols;
1164 rc = parse_protocol_change(&new_protocols, buf);
1165 if (rc < 0)
1166 goto out;
1167
Sean Young0751d332016-12-05 17:08:35 -02001168 rc = dev->change_protocol(dev, &new_protocols);
David Härdemanda6e1622014-04-03 20:32:16 -03001169 if (rc < 0) {
1170 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
1171 (long long)new_protocols);
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001172 goto out;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001173 }
1174
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001175 if (dev->driver_type == RC_DRIVER_IR_RAW)
1176 ir_raw_load_modules(&new_protocols);
1177
James Hogan983c5bd2014-12-08 13:17:07 -03001178 if (new_protocols != old_protocols) {
1179 *current_protocols = new_protocols;
1180 IR_dprintk(1, "Protocols changed to 0x%llx\n",
1181 (long long)new_protocols);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001182 }
1183
James Hogan6bea25a2014-02-28 20:17:06 -03001184 /*
James Hogan983c5bd2014-12-08 13:17:07 -03001185 * If a protocol change was attempted the filter may need updating, even
1186 * if the actual protocol mask hasn't changed (since the driver may have
1187 * cleared the filter).
James Hogan6bea25a2014-02-28 20:17:06 -03001188 * Try setting the same filter with the new protocol (if any).
1189 * Fall back to clearing the filter.
1190 */
Sean Young0751d332016-12-05 17:08:35 -02001191 if (dev->s_filter && filter->mask) {
David Härdemanda6e1622014-04-03 20:32:16 -03001192 if (new_protocols)
Sean Young0751d332016-12-05 17:08:35 -02001193 rc = dev->s_filter(dev, filter);
David Härdemanda6e1622014-04-03 20:32:16 -03001194 else
1195 rc = -1;
David Härdeman23c843b2014-04-04 19:06:01 -03001196
David Härdemanda6e1622014-04-03 20:32:16 -03001197 if (rc < 0) {
1198 filter->data = 0;
1199 filter->mask = 0;
Sean Young0751d332016-12-05 17:08:35 -02001200 dev->s_filter(dev, filter);
James Hogan6bea25a2014-02-28 20:17:06 -03001201 }
James Hogan6bea25a2014-02-28 20:17:06 -03001202 }
1203
David Härdemanda6e1622014-04-03 20:32:16 -03001204 rc = len;
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001205
1206out:
1207 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001208 return rc;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001209}
1210
James Hogan00942d12014-01-17 10:58:49 -03001211/**
James Hogan00942d12014-01-17 10:58:49 -03001212 * show_filter() - shows the current scancode filter value or mask
1213 * @device: the device descriptor
1214 * @attr: the device attribute struct
1215 * @buf: a pointer to the output buffer
1216 *
1217 * This routine is a callback routine to read a scancode filter value or mask.
1218 * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
1219 * It prints the current scancode filter value or mask of the appropriate filter
1220 * type in hexadecimal into @buf and returns the size of the buffer.
1221 *
1222 * Bits of the filter value corresponding to set bits in the filter mask are
1223 * compared against input scancodes and non-matching scancodes are discarded.
1224 *
David Härdeman18726a32017-04-27 17:34:08 -03001225 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001226 * store_filter and show_filter.
1227 */
1228static ssize_t show_filter(struct device *device,
1229 struct device_attribute *attr,
1230 char *buf)
1231{
1232 struct rc_dev *dev = to_rc_dev(device);
1233 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001234 struct rc_scancode_filter *filter;
James Hogan00942d12014-01-17 10:58:49 -03001235 u32 val;
1236
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001237 mutex_lock(&dev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001238
David Härdemanda6e1622014-04-03 20:32:16 -03001239 if (fattr->type == RC_FILTER_NORMAL)
David Härdemanc5540fb2014-04-03 20:32:21 -03001240 filter = &dev->scancode_filter;
James Hogan00942d12014-01-17 10:58:49 -03001241 else
David Härdemanc5540fb2014-04-03 20:32:21 -03001242 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001243
David Härdemanda6e1622014-04-03 20:32:16 -03001244 if (fattr->mask)
1245 val = filter->mask;
1246 else
1247 val = filter->data;
James Hogan00942d12014-01-17 10:58:49 -03001248 mutex_unlock(&dev->lock);
1249
1250 return sprintf(buf, "%#x\n", val);
1251}
1252
1253/**
1254 * store_filter() - changes the scancode filter value
1255 * @device: the device descriptor
1256 * @attr: the device attribute struct
1257 * @buf: a pointer to the input buffer
1258 * @len: length of the input buffer
1259 *
1260 * This routine is for changing a scancode filter value or mask.
1261 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
1262 * Returns -EINVAL if an invalid filter value for the current protocol was
1263 * specified or if scancode filtering is not supported by the driver, otherwise
1264 * returns @len.
1265 *
1266 * Bits of the filter value corresponding to set bits in the filter mask are
1267 * compared against input scancodes and non-matching scancodes are discarded.
1268 *
David Härdeman18726a32017-04-27 17:34:08 -03001269 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001270 * store_filter and show_filter.
1271 */
1272static ssize_t store_filter(struct device *device,
1273 struct device_attribute *attr,
David Härdemanda6e1622014-04-03 20:32:16 -03001274 const char *buf, size_t len)
James Hogan00942d12014-01-17 10:58:49 -03001275{
1276 struct rc_dev *dev = to_rc_dev(device);
1277 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001278 struct rc_scancode_filter new_filter, *filter;
James Hogan00942d12014-01-17 10:58:49 -03001279 int ret;
1280 unsigned long val;
David Härdeman23c843b2014-04-04 19:06:01 -03001281 int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
James Hogan00942d12014-01-17 10:58:49 -03001282
James Hogan00942d12014-01-17 10:58:49 -03001283 ret = kstrtoul(buf, 0, &val);
1284 if (ret < 0)
1285 return ret;
1286
David Härdemanda6e1622014-04-03 20:32:16 -03001287 if (fattr->type == RC_FILTER_NORMAL) {
1288 set_filter = dev->s_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001289 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001290 } else {
1291 set_filter = dev->s_wakeup_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001292 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001293 }
1294
David Härdeman99b0f3c2014-04-04 19:06:06 -03001295 if (!set_filter)
1296 return -EINVAL;
James Hogan00942d12014-01-17 10:58:49 -03001297
1298 mutex_lock(&dev->lock);
1299
David Härdemanda6e1622014-04-03 20:32:16 -03001300 new_filter = *filter;
James Hogan00942d12014-01-17 10:58:49 -03001301 if (fattr->mask)
David Härdemanda6e1622014-04-03 20:32:16 -03001302 new_filter.mask = val;
James Hogan00942d12014-01-17 10:58:49 -03001303 else
David Härdemanda6e1622014-04-03 20:32:16 -03001304 new_filter.data = val;
David Härdeman23c843b2014-04-04 19:06:01 -03001305
Sean Young0751d332016-12-05 17:08:35 -02001306 if (fattr->type == RC_FILTER_WAKEUP) {
Sean Youngb590c0b2016-12-05 19:24:59 -02001307 /*
1308 * Refuse to set a filter unless a protocol is enabled
1309 * and the filter is valid for that protocol
1310 */
Sean Young6d741bf2017-08-07 16:20:58 -04001311 if (dev->wakeup_protocol != RC_PROTO_UNKNOWN)
James Hoganf423ccc2015-03-31 14:48:10 -03001312 ret = rc_validate_filter(dev, &new_filter);
Sean Youngb590c0b2016-12-05 19:24:59 -02001313 else
Sean Young0751d332016-12-05 17:08:35 -02001314 ret = -EINVAL;
Sean Youngb590c0b2016-12-05 19:24:59 -02001315
1316 if (ret != 0)
Sean Young0751d332016-12-05 17:08:35 -02001317 goto unlock;
Sean Young0751d332016-12-05 17:08:35 -02001318 }
1319
1320 if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
1321 val) {
James Hogan6bea25a2014-02-28 20:17:06 -03001322 /* refuse to set a filter unless a protocol is enabled */
1323 ret = -EINVAL;
1324 goto unlock;
1325 }
David Härdeman23c843b2014-04-04 19:06:01 -03001326
David Härdemanda6e1622014-04-03 20:32:16 -03001327 ret = set_filter(dev, &new_filter);
David Härdeman99b0f3c2014-04-04 19:06:06 -03001328 if (ret < 0)
1329 goto unlock;
James Hogan00942d12014-01-17 10:58:49 -03001330
David Härdemanda6e1622014-04-03 20:32:16 -03001331 *filter = new_filter;
James Hogan00942d12014-01-17 10:58:49 -03001332
1333unlock:
1334 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001335 return (ret < 0) ? ret : len;
James Hogan00942d12014-01-17 10:58:49 -03001336}
1337
Sean Young0751d332016-12-05 17:08:35 -02001338/**
1339 * show_wakeup_protocols() - shows the wakeup IR protocol
1340 * @device: the device descriptor
1341 * @mattr: the device attribute struct
1342 * @buf: a pointer to the output buffer
1343 *
1344 * This routine is a callback routine for input read the IR protocol type(s).
1345 * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols.
1346 * It returns the protocol names of supported protocols.
1347 * The enabled protocols are printed in brackets.
1348 *
David Härdeman18726a32017-04-27 17:34:08 -03001349 * dev->lock is taken to guard against races between
1350 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001351 */
1352static ssize_t show_wakeup_protocols(struct device *device,
1353 struct device_attribute *mattr,
1354 char *buf)
1355{
1356 struct rc_dev *dev = to_rc_dev(device);
1357 u64 allowed;
Sean Young6d741bf2017-08-07 16:20:58 -04001358 enum rc_proto enabled;
Sean Young0751d332016-12-05 17:08:35 -02001359 char *tmp = buf;
1360 int i;
1361
Sean Young0751d332016-12-05 17:08:35 -02001362 mutex_lock(&dev->lock);
1363
1364 allowed = dev->allowed_wakeup_protocols;
1365 enabled = dev->wakeup_protocol;
1366
1367 mutex_unlock(&dev->lock);
1368
1369 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - %d\n",
1370 __func__, (long long)allowed, enabled);
1371
Sean Youngd57ea872017-08-09 13:19:16 -04001372 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001373 if (allowed & (1ULL << i)) {
1374 if (i == enabled)
Sean Youngd57ea872017-08-09 13:19:16 -04001375 tmp += sprintf(tmp, "[%s] ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001376 else
Sean Youngd57ea872017-08-09 13:19:16 -04001377 tmp += sprintf(tmp, "%s ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001378 }
1379 }
1380
1381 if (tmp != buf)
1382 tmp--;
1383 *tmp = '\n';
1384
1385 return tmp + 1 - buf;
1386}
1387
1388/**
1389 * store_wakeup_protocols() - changes the wakeup IR protocol(s)
1390 * @device: the device descriptor
1391 * @mattr: the device attribute struct
1392 * @buf: a pointer to the input buffer
1393 * @len: length of the input buffer
1394 *
1395 * This routine is for changing the IR protocol type.
1396 * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols.
1397 * Returns @len on success or a negative error code.
1398 *
David Härdeman18726a32017-04-27 17:34:08 -03001399 * dev->lock is taken to guard against races between
1400 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001401 */
1402static ssize_t store_wakeup_protocols(struct device *device,
1403 struct device_attribute *mattr,
1404 const char *buf, size_t len)
1405{
1406 struct rc_dev *dev = to_rc_dev(device);
Sean Young6d741bf2017-08-07 16:20:58 -04001407 enum rc_proto protocol;
Sean Young0751d332016-12-05 17:08:35 -02001408 ssize_t rc;
1409 u64 allowed;
1410 int i;
1411
Sean Young0751d332016-12-05 17:08:35 -02001412 mutex_lock(&dev->lock);
1413
1414 allowed = dev->allowed_wakeup_protocols;
1415
1416 if (sysfs_streq(buf, "none")) {
Sean Young6d741bf2017-08-07 16:20:58 -04001417 protocol = RC_PROTO_UNKNOWN;
Sean Young0751d332016-12-05 17:08:35 -02001418 } else {
Sean Youngd57ea872017-08-09 13:19:16 -04001419 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001420 if ((allowed & (1ULL << i)) &&
Sean Youngd57ea872017-08-09 13:19:16 -04001421 sysfs_streq(buf, protocols[i].name)) {
Sean Young0751d332016-12-05 17:08:35 -02001422 protocol = i;
1423 break;
1424 }
1425 }
1426
Sean Youngd57ea872017-08-09 13:19:16 -04001427 if (i == ARRAY_SIZE(protocols)) {
Sean Young0751d332016-12-05 17:08:35 -02001428 rc = -EINVAL;
1429 goto out;
1430 }
James Hoganf423ccc2015-03-31 14:48:10 -03001431
1432 if (dev->encode_wakeup) {
1433 u64 mask = 1ULL << protocol;
1434
1435 ir_raw_load_modules(&mask);
1436 if (!mask) {
1437 rc = -EINVAL;
1438 goto out;
1439 }
1440 }
Sean Young0751d332016-12-05 17:08:35 -02001441 }
1442
1443 if (dev->wakeup_protocol != protocol) {
1444 dev->wakeup_protocol = protocol;
1445 IR_dprintk(1, "Wakeup protocol changed to %d\n", protocol);
1446
Sean Young6d741bf2017-08-07 16:20:58 -04001447 if (protocol == RC_PROTO_RC6_MCE)
Sean Young0751d332016-12-05 17:08:35 -02001448 dev->scancode_wakeup_filter.data = 0x800f0000;
1449 else
1450 dev->scancode_wakeup_filter.data = 0;
1451 dev->scancode_wakeup_filter.mask = 0;
1452
1453 rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
1454 if (rc == 0)
1455 rc = len;
1456 } else {
1457 rc = len;
1458 }
1459
1460out:
1461 mutex_unlock(&dev->lock);
1462 return rc;
1463}
1464
David Härdemand8b4b582010-10-29 16:08:23 -03001465static void rc_dev_release(struct device *device)
1466{
Max Kellermann47cae1e2016-03-21 08:33:05 -03001467 struct rc_dev *dev = to_rc_dev(device);
1468
1469 kfree(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001470}
1471
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001472#define ADD_HOTPLUG_VAR(fmt, val...) \
1473 do { \
1474 int err = add_uevent_var(env, fmt, val); \
1475 if (err) \
1476 return err; \
1477 } while (0)
1478
1479static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1480{
David Härdemand8b4b582010-10-29 16:08:23 -03001481 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001482
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001483 if (dev->rc_map.name)
1484 ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
David Härdemand8b4b582010-10-29 16:08:23 -03001485 if (dev->driver_name)
1486 ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
Sean Youngb9f407e2017-09-01 11:34:23 -03001487 if (dev->device_name)
1488 ADD_HOTPLUG_VAR("DEV_NAME=%s", dev->device_name);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001489
1490 return 0;
1491}
1492
1493/*
1494 * Static device attribute struct with the sysfs attributes for IR's
1495 */
Sean Young6d75db32017-09-01 11:30:50 -03001496static struct device_attribute dev_attr_ro_protocols =
1497__ATTR(protocols, 0444, show_protocols, NULL);
1498static struct device_attribute dev_attr_rw_protocols =
1499__ATTR(protocols, 0644, show_protocols, store_protocols);
Sean Young0751d332016-12-05 17:08:35 -02001500static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
1501 store_wakeup_protocols);
James Hogan00942d12014-01-17 10:58:49 -03001502static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
1503 show_filter, store_filter, RC_FILTER_NORMAL, false);
1504static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
1505 show_filter, store_filter, RC_FILTER_NORMAL, true);
1506static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
1507 show_filter, store_filter, RC_FILTER_WAKEUP, false);
1508static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
1509 show_filter, store_filter, RC_FILTER_WAKEUP, true);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001510
Sean Young6d75db32017-09-01 11:30:50 -03001511static struct attribute *rc_dev_rw_protocol_attrs[] = {
1512 &dev_attr_rw_protocols.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001513 NULL,
1514};
1515
Sean Young6d75db32017-09-01 11:30:50 -03001516static const struct attribute_group rc_dev_rw_protocol_attr_grp = {
1517 .attrs = rc_dev_rw_protocol_attrs,
1518};
1519
1520static struct attribute *rc_dev_ro_protocol_attrs[] = {
1521 &dev_attr_ro_protocols.attr,
1522 NULL,
1523};
1524
1525static const struct attribute_group rc_dev_ro_protocol_attr_grp = {
1526 .attrs = rc_dev_ro_protocol_attrs,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001527};
1528
David Härdeman99b0f3c2014-04-04 19:06:06 -03001529static struct attribute *rc_dev_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001530 &dev_attr_filter.attr.attr,
1531 &dev_attr_filter_mask.attr.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001532 NULL,
1533};
1534
Arvind Yadavdb681022017-07-07 04:23:54 -04001535static const struct attribute_group rc_dev_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001536 .attrs = rc_dev_filter_attrs,
1537};
1538
1539static struct attribute *rc_dev_wakeup_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001540 &dev_attr_wakeup_filter.attr.attr,
1541 &dev_attr_wakeup_filter_mask.attr.attr,
Sean Young0751d332016-12-05 17:08:35 -02001542 &dev_attr_wakeup_protocols.attr,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001543 NULL,
1544};
1545
Arvind Yadavdb681022017-07-07 04:23:54 -04001546static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001547 .attrs = rc_dev_wakeup_filter_attrs,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001548};
1549
Bhumika Goyalf03f02f2017-08-19 05:22:15 -03001550static const struct device_type rc_dev_type = {
David Härdemand8b4b582010-10-29 16:08:23 -03001551 .release = rc_dev_release,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001552 .uevent = rc_dev_uevent,
1553};
1554
Andi Shyti0f7499f2016-12-16 06:50:58 -02001555struct rc_dev *rc_allocate_device(enum rc_driver_type type)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001556{
David Härdemand8b4b582010-10-29 16:08:23 -03001557 struct rc_dev *dev;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001558
David Härdemand8b4b582010-10-29 16:08:23 -03001559 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1560 if (!dev)
1561 return NULL;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001562
Andi Shytid34aee12016-12-16 04:12:15 -02001563 if (type != RC_DRIVER_IR_RAW_TX) {
1564 dev->input_dev = input_allocate_device();
1565 if (!dev->input_dev) {
1566 kfree(dev);
1567 return NULL;
1568 }
1569
1570 dev->input_dev->getkeycode = ir_getkeycode;
1571 dev->input_dev->setkeycode = ir_setkeycode;
1572 input_set_drvdata(dev->input_dev, dev);
1573
Kees Cookb17ec782017-10-24 11:23:14 -04001574 timer_setup(&dev->timer_keyup, ir_timer_keyup, 0);
Andi Shytid34aee12016-12-16 04:12:15 -02001575
1576 spin_lock_init(&dev->rc_map.lock);
1577 spin_lock_init(&dev->keylock);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001578 }
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001579 mutex_init(&dev->lock);
David Härdemand8b4b582010-10-29 16:08:23 -03001580
1581 dev->dev.type = &rc_dev_type;
David Härdeman40fc5322013-03-06 16:52:10 -03001582 dev->dev.class = &rc_class;
David Härdemand8b4b582010-10-29 16:08:23 -03001583 device_initialize(&dev->dev);
1584
Andi Shyti0f7499f2016-12-16 06:50:58 -02001585 dev->driver_type = type;
1586
David Härdemand8b4b582010-10-29 16:08:23 -03001587 __module_get(THIS_MODULE);
1588 return dev;
1589}
1590EXPORT_SYMBOL_GPL(rc_allocate_device);
1591
1592void rc_free_device(struct rc_dev *dev)
1593{
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001594 if (!dev)
1595 return;
1596
Markus Elfring3dd94f02014-11-20 09:01:32 -03001597 input_free_device(dev->input_dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001598
1599 put_device(&dev->dev);
1600
Max Kellermann47cae1e2016-03-21 08:33:05 -03001601 /* kfree(dev) will be called by the callback function
1602 rc_dev_release() */
1603
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001604 module_put(THIS_MODULE);
David Härdemand8b4b582010-10-29 16:08:23 -03001605}
1606EXPORT_SYMBOL_GPL(rc_free_device);
1607
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001608static void devm_rc_alloc_release(struct device *dev, void *res)
1609{
1610 rc_free_device(*(struct rc_dev **)res);
1611}
1612
Andi Shyti0f7499f2016-12-16 06:50:58 -02001613struct rc_dev *devm_rc_allocate_device(struct device *dev,
1614 enum rc_driver_type type)
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001615{
1616 struct rc_dev **dr, *rc;
1617
1618 dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
1619 if (!dr)
1620 return NULL;
1621
Andi Shyti0f7499f2016-12-16 06:50:58 -02001622 rc = rc_allocate_device(type);
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001623 if (!rc) {
1624 devres_free(dr);
1625 return NULL;
1626 }
1627
1628 rc->dev.parent = dev;
1629 rc->managed_alloc = true;
1630 *dr = rc;
1631 devres_add(dev, dr);
1632
1633 return rc;
1634}
1635EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
1636
David Härdemanf56928a2017-05-03 07:04:00 -03001637static int rc_prepare_rx_device(struct rc_dev *dev)
David Härdemand8b4b582010-10-29 16:08:23 -03001638{
David Härdemanfcb13092015-05-19 19:03:17 -03001639 int rc;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001640 struct rc_map *rc_map;
Sean Young6d741bf2017-08-07 16:20:58 -04001641 u64 rc_proto;
David Härdemand8b4b582010-10-29 16:08:23 -03001642
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001643 if (!dev->map_name)
David Härdemand8b4b582010-10-29 16:08:23 -03001644 return -EINVAL;
1645
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001646 rc_map = rc_map_get(dev->map_name);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001647 if (!rc_map)
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001648 rc_map = rc_map_get(RC_MAP_EMPTY);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001649 if (!rc_map || !rc_map->scan || rc_map->size == 0)
David Härdemand8b4b582010-10-29 16:08:23 -03001650 return -EINVAL;
1651
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001652 rc = ir_setkeytable(dev, rc_map);
1653 if (rc)
1654 return rc;
1655
Sean Young6d741bf2017-08-07 16:20:58 -04001656 rc_proto = BIT_ULL(rc_map->rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001657
Sean Young831c4c82017-09-01 09:55:59 -03001658 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1659 dev->enabled_protocols = dev->allowed_protocols;
1660
Sean Young41380862017-02-22 18:48:01 -03001661 if (dev->change_protocol) {
Sean Young6d741bf2017-08-07 16:20:58 -04001662 rc = dev->change_protocol(dev, &rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001663 if (rc < 0)
1664 goto out_table;
Sean Young6d741bf2017-08-07 16:20:58 -04001665 dev->enabled_protocols = rc_proto;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001666 }
1667
Sean Young41380862017-02-22 18:48:01 -03001668 if (dev->driver_type == RC_DRIVER_IR_RAW)
Sean Young6d741bf2017-08-07 16:20:58 -04001669 ir_raw_load_modules(&rc_proto);
Sean Young41380862017-02-22 18:48:01 -03001670
David Härdemand8b4b582010-10-29 16:08:23 -03001671 set_bit(EV_KEY, dev->input_dev->evbit);
1672 set_bit(EV_REP, dev->input_dev->evbit);
1673 set_bit(EV_MSC, dev->input_dev->evbit);
1674 set_bit(MSC_SCAN, dev->input_dev->mscbit);
1675 if (dev->open)
1676 dev->input_dev->open = ir_open;
1677 if (dev->close)
1678 dev->input_dev->close = ir_close;
1679
David Härdemanb2aceb72017-04-27 17:33:58 -03001680 dev->input_dev->dev.parent = &dev->dev;
1681 memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
1682 dev->input_dev->phys = dev->input_phys;
Sean Young518f4b22017-07-01 12:13:19 -04001683 dev->input_dev->name = dev->device_name;
David Härdemanb2aceb72017-04-27 17:33:58 -03001684
David Härdemanf56928a2017-05-03 07:04:00 -03001685 return 0;
1686
1687out_table:
1688 ir_free_table(&dev->rc_map);
1689
1690 return rc;
1691}
1692
1693static int rc_setup_rx_device(struct rc_dev *dev)
1694{
1695 int rc;
1696
David Härdemanb2aceb72017-04-27 17:33:58 -03001697 /* rc_open will be called here */
1698 rc = input_register_device(dev->input_dev);
1699 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001700 return rc;
David Härdemanb2aceb72017-04-27 17:33:58 -03001701
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001702 /*
1703 * Default delay of 250ms is too short for some protocols, especially
1704 * since the timeout is currently set to 250ms. Increase it to 500ms,
1705 * to avoid wrong repetition of the keycodes. Note that this must be
1706 * set after the call to input_register_device().
1707 */
1708 dev->input_dev->rep[REP_DELAY] = 500;
1709
1710 /*
1711 * As a repeat event on protocols like RC-5 and NEC take as long as
1712 * 110/114ms, using 33ms as a repeat period is not the right thing
1713 * to do.
1714 */
1715 dev->input_dev->rep[REP_PERIOD] = 125;
1716
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001717 return 0;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001718}
1719
1720static void rc_free_rx_device(struct rc_dev *dev)
1721{
David Härdemanf56928a2017-05-03 07:04:00 -03001722 if (!dev)
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001723 return;
1724
David Härdemanf56928a2017-05-03 07:04:00 -03001725 if (dev->input_dev) {
1726 input_unregister_device(dev->input_dev);
1727 dev->input_dev = NULL;
1728 }
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001729
David Härdemanf56928a2017-05-03 07:04:00 -03001730 ir_free_table(&dev->rc_map);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001731}
1732
1733int rc_register_device(struct rc_dev *dev)
1734{
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001735 const char *path;
1736 int attr = 0;
1737 int minor;
1738 int rc;
1739
1740 if (!dev)
1741 return -EINVAL;
1742
David Härdemanfcb13092015-05-19 19:03:17 -03001743 minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
1744 if (minor < 0)
1745 return minor;
1746
1747 dev->minor = minor;
1748 dev_set_name(&dev->dev, "rc%u", dev->minor);
1749 dev_set_drvdata(&dev->dev, dev);
Mauro Carvalho Chehab587d1b02014-01-14 16:27:55 -03001750
David Härdeman99b0f3c2014-04-04 19:06:06 -03001751 dev->dev.groups = dev->sysfs_groups;
Sean Young6d75db32017-09-01 11:30:50 -03001752 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1753 dev->sysfs_groups[attr++] = &rc_dev_ro_protocol_attr_grp;
1754 else if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
1755 dev->sysfs_groups[attr++] = &rc_dev_rw_protocol_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001756 if (dev->s_filter)
David Härdeman120703f2014-04-03 20:31:30 -03001757 dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001758 if (dev->s_wakeup_filter)
1759 dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001760 dev->sysfs_groups[attr++] = NULL;
1761
David Härdemanf56928a2017-05-03 07:04:00 -03001762 if (dev->driver_type == RC_DRIVER_IR_RAW ||
1763 dev->driver_type == RC_DRIVER_IR_RAW_TX) {
1764 rc = ir_raw_event_prepare(dev);
1765 if (rc < 0)
1766 goto out_minor;
1767 }
1768
1769 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1770 rc = rc_prepare_rx_device(dev);
1771 if (rc)
1772 goto out_raw;
1773 }
1774
David Härdemand8b4b582010-10-29 16:08:23 -03001775 rc = device_add(&dev->dev);
1776 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001777 goto out_rx_free;
David Härdemand8b4b582010-10-29 16:08:23 -03001778
David Härdemand8b4b582010-10-29 16:08:23 -03001779 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Heiner Kallweit4dc0e902015-10-29 19:39:06 -02001780 dev_info(&dev->dev, "%s as %s\n",
Sean Young518f4b22017-07-01 12:13:19 -04001781 dev->device_name ?: "Unspecified device", path ?: "N/A");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001782 kfree(path);
1783
Sean Young5df62772017-02-23 06:11:21 -03001784 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1785 rc = rc_setup_rx_device(dev);
1786 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001787 goto out_dev;
1788 }
1789
1790 if (dev->driver_type == RC_DRIVER_IR_RAW ||
1791 dev->driver_type == RC_DRIVER_IR_RAW_TX) {
1792 rc = ir_raw_event_register(dev);
1793 if (rc < 0)
1794 goto out_rx;
David Härdemand8b4b582010-10-29 16:08:23 -03001795 }
1796
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001797 IR_dprintk(1, "Registered rc%u (driver: %s)\n",
David Härdemanfcb13092015-05-19 19:03:17 -03001798 dev->minor,
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001799 dev->driver_name ? dev->driver_name : "unknown");
David Härdemand8b4b582010-10-29 16:08:23 -03001800
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001801 return 0;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001802
David Härdemanf56928a2017-05-03 07:04:00 -03001803out_rx:
1804 rc_free_rx_device(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001805out_dev:
1806 device_del(&dev->dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001807out_rx_free:
1808 ir_free_table(&dev->rc_map);
1809out_raw:
1810 ir_raw_event_free(dev);
1811out_minor:
David Härdemanfcb13092015-05-19 19:03:17 -03001812 ida_simple_remove(&rc_ida, minor);
David Härdemand8b4b582010-10-29 16:08:23 -03001813 return rc;
1814}
1815EXPORT_SYMBOL_GPL(rc_register_device);
1816
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001817static void devm_rc_release(struct device *dev, void *res)
1818{
1819 rc_unregister_device(*(struct rc_dev **)res);
1820}
1821
1822int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
1823{
1824 struct rc_dev **dr;
1825 int ret;
1826
1827 dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
1828 if (!dr)
1829 return -ENOMEM;
1830
1831 ret = rc_register_device(dev);
1832 if (ret) {
1833 devres_free(dr);
1834 return ret;
1835 }
1836
1837 *dr = dev;
1838 devres_add(parent, dr);
1839
1840 return 0;
1841}
1842EXPORT_SYMBOL_GPL(devm_rc_register_device);
1843
David Härdemand8b4b582010-10-29 16:08:23 -03001844void rc_unregister_device(struct rc_dev *dev)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001845{
David Härdemand8b4b582010-10-29 16:08:23 -03001846 if (!dev)
1847 return;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001848
David Härdemand8b4b582010-10-29 16:08:23 -03001849 del_timer_sync(&dev->timer_keyup);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001850
David Härdemand8b4b582010-10-29 16:08:23 -03001851 if (dev->driver_type == RC_DRIVER_IR_RAW)
1852 ir_raw_event_unregister(dev);
1853
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001854 rc_free_rx_device(dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001855
1856 device_del(&dev->dev);
1857
David Härdemanfcb13092015-05-19 19:03:17 -03001858 ida_simple_remove(&rc_ida, dev->minor);
1859
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001860 if (!dev->managed_alloc)
1861 rc_free_device(dev);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001862}
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001863
David Härdemand8b4b582010-10-29 16:08:23 -03001864EXPORT_SYMBOL_GPL(rc_unregister_device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001865
1866/*
1867 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1868 */
1869
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001870static int __init rc_core_init(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001871{
David Härdeman40fc5322013-03-06 16:52:10 -03001872 int rc = class_register(&rc_class);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001873 if (rc) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -02001874 pr_err("rc_core: unable to register rc class\n");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001875 return rc;
1876 }
1877
Sean Young153a60b2013-07-30 19:00:01 -03001878 led_trigger_register_simple("rc-feedback", &led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001879 rc_map_register(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001880
1881 return 0;
1882}
1883
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001884static void __exit rc_core_exit(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001885{
David Härdeman40fc5322013-03-06 16:52:10 -03001886 class_unregister(&rc_class);
Sean Young153a60b2013-07-30 19:00:01 -03001887 led_trigger_unregister_simple(led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001888 rc_map_unregister(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001889}
1890
David Härdemane76d4ce2013-03-06 16:52:15 -03001891subsys_initcall(rc_core_init);
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001892module_exit(rc_core_exit);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001893
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001894int rc_core_debug; /* ir_debug level (0,1,2) */
1895EXPORT_SYMBOL_GPL(rc_core_debug);
1896module_param_named(debug, rc_core_debug, int, 0644);
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03001897
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02001898MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehab20835282017-12-01 08:47:08 -05001899MODULE_LICENSE("GPL v2");