blob: e0901a2a1ec93c31b5c21554a0c9104b8592fd64 [file] [log] [blame]
David Härdeman829ba9f2010-11-19 20:43:27 -03001/* rc-main.c - Remote Controller core module
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -03002 *
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02003 * Copyright (C) 2009-2010 by Mauro Carvalho Chehab
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -030013 */
14
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -020015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -030017#include <media/rc-core.h>
Thomas Meyer8ca01d42017-09-08 13:33:36 -030018#include <linux/bsearch.h>
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030019#include <linux/spinlock.h>
20#include <linux/delay.h>
Mauro Carvalho Chehab882ead32009-12-29 10:37:38 -030021#include <linux/input.h>
Sean Young153a60b2013-07-30 19:00:01 -030022#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
David Härdemanfcb13092015-05-19 19:03:17 -030024#include <linux/idr.h>
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -030025#include <linux/device.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040026#include <linux/module.h>
Mauro Carvalho Chehabf62de672010-11-09 23:09:57 -030027#include "rc-core-priv.h"
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -030028
David Härdemanb3074c02010-04-02 15:58:28 -030029/* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */
30#define IR_TAB_MIN_SIZE 256
31#define IR_TAB_MAX_SIZE 8192
David Härdemanfcb13092015-05-19 19:03:17 -030032#define RC_DEV_MAX 256
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -030033
Sean Youngd57ea872017-08-09 13:19:16 -040034static const struct {
35 const char *name;
36 unsigned int repeat_period;
37 unsigned int scancode_bits;
38} protocols[] = {
Sean Young6d741bf2017-08-07 16:20:58 -040039 [RC_PROTO_UNKNOWN] = { .name = "unknown", .repeat_period = 250 },
40 [RC_PROTO_OTHER] = { .name = "other", .repeat_period = 250 },
41 [RC_PROTO_RC5] = { .name = "rc-5",
Sean Youngd57ea872017-08-09 13:19:16 -040042 .scancode_bits = 0x1f7f, .repeat_period = 164 },
Sean Young6d741bf2017-08-07 16:20:58 -040043 [RC_PROTO_RC5X_20] = { .name = "rc-5x-20",
Sean Youngd57ea872017-08-09 13:19:16 -040044 .scancode_bits = 0x1f7f3f, .repeat_period = 164 },
Sean Young6d741bf2017-08-07 16:20:58 -040045 [RC_PROTO_RC5_SZ] = { .name = "rc-5-sz",
Sean Youngd57ea872017-08-09 13:19:16 -040046 .scancode_bits = 0x2fff, .repeat_period = 164 },
Sean Young6d741bf2017-08-07 16:20:58 -040047 [RC_PROTO_JVC] = { .name = "jvc",
Sean Youngd57ea872017-08-09 13:19:16 -040048 .scancode_bits = 0xffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040049 [RC_PROTO_SONY12] = { .name = "sony-12",
Sean Youngd57ea872017-08-09 13:19:16 -040050 .scancode_bits = 0x1f007f, .repeat_period = 100 },
Sean Young6d741bf2017-08-07 16:20:58 -040051 [RC_PROTO_SONY15] = { .name = "sony-15",
Sean Youngd57ea872017-08-09 13:19:16 -040052 .scancode_bits = 0xff007f, .repeat_period = 100 },
Sean Young6d741bf2017-08-07 16:20:58 -040053 [RC_PROTO_SONY20] = { .name = "sony-20",
Sean Youngd57ea872017-08-09 13:19:16 -040054 .scancode_bits = 0x1fff7f, .repeat_period = 100 },
Sean Young6d741bf2017-08-07 16:20:58 -040055 [RC_PROTO_NEC] = { .name = "nec",
Sean Youngd57ea872017-08-09 13:19:16 -040056 .scancode_bits = 0xffff, .repeat_period = 160 },
Sean Young6d741bf2017-08-07 16:20:58 -040057 [RC_PROTO_NECX] = { .name = "nec-x",
Sean Youngd57ea872017-08-09 13:19:16 -040058 .scancode_bits = 0xffffff, .repeat_period = 160 },
Sean Young6d741bf2017-08-07 16:20:58 -040059 [RC_PROTO_NEC32] = { .name = "nec-32",
Sean Youngd57ea872017-08-09 13:19:16 -040060 .scancode_bits = 0xffffffff, .repeat_period = 160 },
Sean Young6d741bf2017-08-07 16:20:58 -040061 [RC_PROTO_SANYO] = { .name = "sanyo",
Sean Youngd57ea872017-08-09 13:19:16 -040062 .scancode_bits = 0x1fffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040063 [RC_PROTO_MCIR2_KBD] = { .name = "mcir2-kbd",
Sean Youngd57ea872017-08-09 13:19:16 -040064 .scancode_bits = 0xffff, .repeat_period = 150 },
Sean Young6d741bf2017-08-07 16:20:58 -040065 [RC_PROTO_MCIR2_MSE] = { .name = "mcir2-mse",
Sean Youngd57ea872017-08-09 13:19:16 -040066 .scancode_bits = 0x1fffff, .repeat_period = 150 },
Sean Young6d741bf2017-08-07 16:20:58 -040067 [RC_PROTO_RC6_0] = { .name = "rc-6-0",
Sean Youngd57ea872017-08-09 13:19:16 -040068 .scancode_bits = 0xffff, .repeat_period = 164 },
Sean Young6d741bf2017-08-07 16:20:58 -040069 [RC_PROTO_RC6_6A_20] = { .name = "rc-6-6a-20",
Sean Youngd57ea872017-08-09 13:19:16 -040070 .scancode_bits = 0xfffff, .repeat_period = 164 },
Sean Young6d741bf2017-08-07 16:20:58 -040071 [RC_PROTO_RC6_6A_24] = { .name = "rc-6-6a-24",
Sean Youngd57ea872017-08-09 13:19:16 -040072 .scancode_bits = 0xffffff, .repeat_period = 164 },
Sean Young6d741bf2017-08-07 16:20:58 -040073 [RC_PROTO_RC6_6A_32] = { .name = "rc-6-6a-32",
Sean Youngd57ea872017-08-09 13:19:16 -040074 .scancode_bits = 0xffffffff, .repeat_period = 164 },
Sean Young6d741bf2017-08-07 16:20:58 -040075 [RC_PROTO_RC6_MCE] = { .name = "rc-6-mce",
Sean Youngd57ea872017-08-09 13:19:16 -040076 .scancode_bits = 0xffff7fff, .repeat_period = 164 },
Sean Young6d741bf2017-08-07 16:20:58 -040077 [RC_PROTO_SHARP] = { .name = "sharp",
Sean Youngd57ea872017-08-09 13:19:16 -040078 .scancode_bits = 0x1fff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040079 [RC_PROTO_XMP] = { .name = "xmp", .repeat_period = 250 },
80 [RC_PROTO_CEC] = { .name = "cec", .repeat_period = 550 },
Sean Youngd57ea872017-08-09 13:19:16 -040081};
David Härdemana374fef2010-04-02 15:58:29 -030082
David Härdeman4c7b3552010-11-10 11:04:19 -030083/* Used to keep track of known keymaps */
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030084static LIST_HEAD(rc_map_list);
85static DEFINE_SPINLOCK(rc_map_lock);
Sean Young153a60b2013-07-30 19:00:01 -030086static struct led_trigger *led_feedback;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030087
David Härdemanfcb13092015-05-19 19:03:17 -030088/* Used to keep track of rc devices */
89static DEFINE_IDA(rc_ida);
90
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030091static struct rc_map_list *seek_rc_map(const char *name)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030092{
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030093 struct rc_map_list *map = NULL;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030094
95 spin_lock(&rc_map_lock);
96 list_for_each_entry(map, &rc_map_list, list) {
97 if (!strcmp(name, map->map.name)) {
98 spin_unlock(&rc_map_lock);
99 return map;
100 }
101 }
102 spin_unlock(&rc_map_lock);
103
104 return NULL;
105}
106
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300107struct rc_map *rc_map_get(const char *name)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300108{
109
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300110 struct rc_map_list *map;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300111
112 map = seek_rc_map(name);
Russell King2ff56fa2015-10-15 13:15:24 -0300113#ifdef CONFIG_MODULES
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300114 if (!map) {
Kees Cook8ea54882014-03-11 17:25:53 -0300115 int rc = request_module("%s", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300116 if (rc < 0) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -0200117 pr_err("Couldn't load IR keymap %s\n", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300118 return NULL;
119 }
120 msleep(20); /* Give some time for IR to register */
121
122 map = seek_rc_map(name);
123 }
124#endif
125 if (!map) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -0200126 pr_err("IR keymap %s not found\n", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300127 return NULL;
128 }
129
130 printk(KERN_INFO "Registered IR keymap %s\n", map->map.name);
131
132 return &map->map;
133}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300134EXPORT_SYMBOL_GPL(rc_map_get);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300135
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300136int rc_map_register(struct rc_map_list *map)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300137{
138 spin_lock(&rc_map_lock);
139 list_add_tail(&map->list, &rc_map_list);
140 spin_unlock(&rc_map_lock);
141 return 0;
142}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300143EXPORT_SYMBOL_GPL(rc_map_register);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300144
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300145void rc_map_unregister(struct rc_map_list *map)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300146{
147 spin_lock(&rc_map_lock);
148 list_del(&map->list);
149 spin_unlock(&rc_map_lock);
150}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300151EXPORT_SYMBOL_GPL(rc_map_unregister);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300152
153
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300154static struct rc_map_table empty[] = {
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300155 { 0x2a, KEY_COFFEE },
156};
157
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300158static struct rc_map_list empty_map = {
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300159 .map = {
Sean Young6d741bf2017-08-07 16:20:58 -0400160 .scan = empty,
161 .size = ARRAY_SIZE(empty),
162 .rc_proto = RC_PROTO_UNKNOWN, /* Legacy IR type */
163 .name = RC_MAP_EMPTY,
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300164 }
165};
166
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300167/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700168 * ir_create_table() - initializes a scancode table
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300169 * @rc_map: the rc_map to initialize
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700170 * @name: name to assign to the table
Sean Young6d741bf2017-08-07 16:20:58 -0400171 * @rc_proto: ir type to assign to the new table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700172 * @size: initial size of the table
173 * @return: zero on success or a negative error code
174 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300175 * This routine will initialize the rc_map and will allocate
David Härdemand8b4b582010-10-29 16:08:23 -0300176 * memory to hold at least the specified number of elements.
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700177 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300178static int ir_create_table(struct rc_map *rc_map,
Sean Young6d741bf2017-08-07 16:20:58 -0400179 const char *name, u64 rc_proto, size_t size)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700180{
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300181 rc_map->name = kstrdup(name, GFP_KERNEL);
182 if (!rc_map->name)
183 return -ENOMEM;
Sean Young6d741bf2017-08-07 16:20:58 -0400184 rc_map->rc_proto = rc_proto;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300185 rc_map->alloc = roundup_pow_of_two(size * sizeof(struct rc_map_table));
186 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300187 rc_map->scan = kmalloc(rc_map->alloc, GFP_KERNEL);
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300188 if (!rc_map->scan) {
189 kfree(rc_map->name);
190 rc_map->name = NULL;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700191 return -ENOMEM;
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300192 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700193
194 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300195 rc_map->size, rc_map->alloc);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700196 return 0;
197}
198
199/**
200 * ir_free_table() - frees memory allocated by a scancode table
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300201 * @rc_map: the table whose mappings need to be freed
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700202 *
203 * This routine will free memory alloctaed for key mappings used by given
204 * scancode table.
205 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300206static void ir_free_table(struct rc_map *rc_map)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700207{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300208 rc_map->size = 0;
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300209 kfree(rc_map->name);
Max Kellermannc183d352016-08-09 18:32:06 -0300210 rc_map->name = NULL;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300211 kfree(rc_map->scan);
212 rc_map->scan = NULL;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700213}
214
215/**
David Härdemanb3074c02010-04-02 15:58:28 -0300216 * ir_resize_table() - resizes a scancode table if necessary
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300217 * @rc_map: the rc_map to resize
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700218 * @gfp_flags: gfp flags to use when allocating memory
David Härdemanb3074c02010-04-02 15:58:28 -0300219 * @return: zero on success or a negative error code
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300220 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300221 * This routine will shrink the rc_map if it has lots of
David Härdemanb3074c02010-04-02 15:58:28 -0300222 * unused entries and grow it if it is full.
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300223 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300224static int ir_resize_table(struct rc_map *rc_map, gfp_t gfp_flags)
David Härdemanb3074c02010-04-02 15:58:28 -0300225{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300226 unsigned int oldalloc = rc_map->alloc;
David Härdemanb3074c02010-04-02 15:58:28 -0300227 unsigned int newalloc = oldalloc;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300228 struct rc_map_table *oldscan = rc_map->scan;
229 struct rc_map_table *newscan;
David Härdemanb3074c02010-04-02 15:58:28 -0300230
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300231 if (rc_map->size == rc_map->len) {
David Härdemanb3074c02010-04-02 15:58:28 -0300232 /* All entries in use -> grow keytable */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300233 if (rc_map->alloc >= IR_TAB_MAX_SIZE)
David Härdemanb3074c02010-04-02 15:58:28 -0300234 return -ENOMEM;
235
236 newalloc *= 2;
237 IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
238 }
239
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300240 if ((rc_map->len * 3 < rc_map->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
David Härdemanb3074c02010-04-02 15:58:28 -0300241 /* Less than 1/3 of entries in use -> shrink keytable */
242 newalloc /= 2;
243 IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
244 }
245
246 if (newalloc == oldalloc)
247 return 0;
248
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700249 newscan = kmalloc(newalloc, gfp_flags);
David Härdemanb3074c02010-04-02 15:58:28 -0300250 if (!newscan) {
251 IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
252 return -ENOMEM;
253 }
254
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300255 memcpy(newscan, rc_map->scan, rc_map->len * sizeof(struct rc_map_table));
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300256 rc_map->scan = newscan;
257 rc_map->alloc = newalloc;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300258 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
David Härdemanb3074c02010-04-02 15:58:28 -0300259 kfree(oldscan);
260 return 0;
261}
262
263/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700264 * ir_update_mapping() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300265 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300266 * @rc_map: scancode table to be adjusted
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700267 * @index: index of the mapping that needs to be updated
268 * @keycode: the desired keycode
269 * @return: previous keycode assigned to the mapping
David Härdemanb3074c02010-04-02 15:58:28 -0300270 *
David Härdemand8b4b582010-10-29 16:08:23 -0300271 * This routine is used to update scancode->keycode mapping at given
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700272 * position.
David Härdemanb3074c02010-04-02 15:58:28 -0300273 */
David Härdemand8b4b582010-10-29 16:08:23 -0300274static unsigned int ir_update_mapping(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300275 struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700276 unsigned int index,
277 unsigned int new_keycode)
278{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300279 int old_keycode = rc_map->scan[index].keycode;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700280 int i;
281
282 /* Did the user wish to remove the mapping? */
283 if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
284 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300285 index, rc_map->scan[index].scancode);
286 rc_map->len--;
287 memmove(&rc_map->scan[index], &rc_map->scan[index+ 1],
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300288 (rc_map->len - index) * sizeof(struct rc_map_table));
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700289 } else {
290 IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n",
291 index,
292 old_keycode == KEY_RESERVED ? "New" : "Replacing",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300293 rc_map->scan[index].scancode, new_keycode);
294 rc_map->scan[index].keycode = new_keycode;
David Härdemand8b4b582010-10-29 16:08:23 -0300295 __set_bit(new_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700296 }
297
298 if (old_keycode != KEY_RESERVED) {
299 /* A previous mapping was updated... */
David Härdemand8b4b582010-10-29 16:08:23 -0300300 __clear_bit(old_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700301 /* ... but another scancode might use the same keycode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300302 for (i = 0; i < rc_map->len; i++) {
303 if (rc_map->scan[i].keycode == old_keycode) {
David Härdemand8b4b582010-10-29 16:08:23 -0300304 __set_bit(old_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700305 break;
306 }
307 }
308
309 /* Possibly shrink the keytable, failure is not a problem */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300310 ir_resize_table(rc_map, GFP_ATOMIC);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700311 }
312
313 return old_keycode;
314}
315
316/**
David Härdeman4c7b3552010-11-10 11:04:19 -0300317 * ir_establish_scancode() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300318 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300319 * @rc_map: scancode table to be searched
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700320 * @scancode: the desired scancode
321 * @resize: controls whether we allowed to resize the table to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300322 * accommodate not yet present scancodes
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700323 * @return: index of the mapping containing scancode in question
324 * or -1U in case of failure.
325 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300326 * This routine is used to locate given scancode in rc_map.
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700327 * If scancode is not yet present the routine will allocate a new slot
328 * for it.
329 */
David Härdemand8b4b582010-10-29 16:08:23 -0300330static unsigned int ir_establish_scancode(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300331 struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700332 unsigned int scancode,
333 bool resize)
David Härdemanb3074c02010-04-02 15:58:28 -0300334{
335 unsigned int i;
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300336
337 /*
338 * Unfortunately, some hardware-based IR decoders don't provide
339 * all bits for the complete IR code. In general, they provide only
340 * the command part of the IR code. Yet, as it is possible to replace
341 * the provided IR with another one, it is needed to allow loading
David Härdemand8b4b582010-10-29 16:08:23 -0300342 * IR tables from other remotes. So, we support specifying a mask to
343 * indicate the valid bits of the scancodes.
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300344 */
David Härdeman9d2f1d32014-04-03 20:32:26 -0300345 if (dev->scancode_mask)
346 scancode &= dev->scancode_mask;
David Härdemanb3074c02010-04-02 15:58:28 -0300347
348 /* First check if we already have a mapping for this ir command */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300349 for (i = 0; i < rc_map->len; i++) {
350 if (rc_map->scan[i].scancode == scancode)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700351 return i;
352
David Härdemanb3074c02010-04-02 15:58:28 -0300353 /* Keytable is sorted from lowest to highest scancode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300354 if (rc_map->scan[i].scancode >= scancode)
David Härdemanb3074c02010-04-02 15:58:28 -0300355 break;
David Härdemanb3074c02010-04-02 15:58:28 -0300356 }
357
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700358 /* No previous mapping found, we might need to grow the table */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300359 if (rc_map->size == rc_map->len) {
360 if (!resize || ir_resize_table(rc_map, GFP_ATOMIC))
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700361 return -1U;
362 }
David Härdemanb3074c02010-04-02 15:58:28 -0300363
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700364 /* i is the proper index to insert our new keycode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300365 if (i < rc_map->len)
366 memmove(&rc_map->scan[i + 1], &rc_map->scan[i],
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300367 (rc_map->len - i) * sizeof(struct rc_map_table));
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300368 rc_map->scan[i].scancode = scancode;
369 rc_map->scan[i].keycode = KEY_RESERVED;
370 rc_map->len++;
David Härdemanb3074c02010-04-02 15:58:28 -0300371
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700372 return i;
David Härdemanb3074c02010-04-02 15:58:28 -0300373}
374
375/**
376 * ir_setkeycode() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300377 * @idev: the struct input_dev device descriptor
David Härdemanb3074c02010-04-02 15:58:28 -0300378 * @scancode: the desired scancode
379 * @keycode: result
380 * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
381 *
382 * This routine is used to handle evdev EVIOCSKEY ioctl.
383 */
David Härdemand8b4b582010-10-29 16:08:23 -0300384static int ir_setkeycode(struct input_dev *idev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700385 const struct input_keymap_entry *ke,
386 unsigned int *old_keycode)
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300387{
David Härdemand8b4b582010-10-29 16:08:23 -0300388 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300389 struct rc_map *rc_map = &rdev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700390 unsigned int index;
391 unsigned int scancode;
Mauro Carvalho Chehabdea8a392010-11-29 07:46:13 -0300392 int retval = 0;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700393 unsigned long flags;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300394
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300395 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700396
397 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
398 index = ke->index;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300399 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700400 retval = -EINVAL;
401 goto out;
402 }
403 } else {
404 retval = input_scancode_to_scalar(ke, &scancode);
405 if (retval)
406 goto out;
407
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300408 index = ir_establish_scancode(rdev, rc_map, scancode, true);
409 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700410 retval = -ENOMEM;
411 goto out;
412 }
413 }
414
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300415 *old_keycode = ir_update_mapping(rdev, rc_map, index, ke->keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700416
417out:
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300418 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700419 return retval;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300420}
421
422/**
David Härdemanb3074c02010-04-02 15:58:28 -0300423 * ir_setkeytable() - sets several entries in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300424 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300425 * @to: the struct rc_map to copy entries to
426 * @from: the struct rc_map to copy entries from
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700427 * @return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300428 *
David Härdemanb3074c02010-04-02 15:58:28 -0300429 * This routine is used to handle table initialization.
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300430 */
David Härdemand8b4b582010-10-29 16:08:23 -0300431static int ir_setkeytable(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300432 const struct rc_map *from)
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300433{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300434 struct rc_map *rc_map = &dev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700435 unsigned int i, index;
436 int rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300437
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300438 rc = ir_create_table(rc_map, from->name,
Sean Young6d741bf2017-08-07 16:20:58 -0400439 from->rc_proto, from->size);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700440 if (rc)
441 return rc;
442
443 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300444 rc_map->size, rc_map->alloc);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700445
David Härdemanb3074c02010-04-02 15:58:28 -0300446 for (i = 0; i < from->size; i++) {
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300447 index = ir_establish_scancode(dev, rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700448 from->scan[i].scancode, false);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300449 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700450 rc = -ENOMEM;
David Härdemanb3074c02010-04-02 15:58:28 -0300451 break;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700452 }
453
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300454 ir_update_mapping(dev, rc_map, index,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700455 from->scan[i].keycode);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300456 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700457
458 if (rc)
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300459 ir_free_table(rc_map);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700460
David Härdemanb3074c02010-04-02 15:58:28 -0300461 return rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300462}
463
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300464static int rc_map_cmp(const void *key, const void *elt)
465{
466 const unsigned int *scancode = key;
467 const struct rc_map_table *e = elt;
468
469 if (*scancode < e->scancode)
470 return -1;
471 else if (*scancode > e->scancode)
472 return 1;
473 return 0;
474}
475
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300476/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700477 * ir_lookup_by_scancode() - locate mapping by scancode
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300478 * @rc_map: the struct rc_map to search
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700479 * @scancode: scancode to look for in the table
480 * @return: index in the table, -1U if not found
481 *
482 * This routine performs binary search in RC keykeymap table for
483 * given scancode.
484 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300485static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700486 unsigned int scancode)
487{
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300488 struct rc_map_table *res;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700489
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300490 res = bsearch(&scancode, rc_map->scan, rc_map->len,
491 sizeof(struct rc_map_table), rc_map_cmp);
492 if (!res)
493 return -1U;
494 else
495 return res - rc_map->scan;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700496}
497
498/**
David Härdemanb3074c02010-04-02 15:58:28 -0300499 * ir_getkeycode() - get a keycode from the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300500 * @idev: the struct input_dev device descriptor
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300501 * @scancode: the desired scancode
David Härdemanb3074c02010-04-02 15:58:28 -0300502 * @keycode: used to return the keycode, if found, or KEY_RESERVED
503 * @return: always returns zero.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300504 *
505 * This routine is used to handle evdev EVIOCGKEY ioctl.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300506 */
David Härdemand8b4b582010-10-29 16:08:23 -0300507static int ir_getkeycode(struct input_dev *idev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700508 struct input_keymap_entry *ke)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300509{
David Härdemand8b4b582010-10-29 16:08:23 -0300510 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300511 struct rc_map *rc_map = &rdev->rc_map;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300512 struct rc_map_table *entry;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700513 unsigned long flags;
514 unsigned int index;
515 unsigned int scancode;
516 int retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300517
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300518 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700519
520 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
521 index = ke->index;
522 } else {
523 retval = input_scancode_to_scalar(ke, &scancode);
524 if (retval)
525 goto out;
526
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300527 index = ir_lookup_by_scancode(rc_map, scancode);
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300528 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700529
Dmitry Torokhov54e74b82011-01-28 23:33:29 -0800530 if (index < rc_map->len) {
531 entry = &rc_map->scan[index];
532
533 ke->index = index;
534 ke->keycode = entry->keycode;
535 ke->len = sizeof(entry->scancode);
536 memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
537
538 } else if (!(ke->flags & INPUT_KEYMAP_BY_INDEX)) {
539 /*
540 * We do not really know the valid range of scancodes
541 * so let's respond with KEY_RESERVED to anything we
542 * do not have mapping for [yet].
543 */
544 ke->index = index;
545 ke->keycode = KEY_RESERVED;
546 } else {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700547 retval = -EINVAL;
548 goto out;
549 }
550
Dmitry Torokhov47c5ba52010-10-31 15:18:42 -0700551 retval = 0;
552
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700553out:
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300554 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700555 return retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300556}
557
558/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300559 * rc_g_keycode_from_table() - gets the keycode that corresponds to a scancode
David Härdemand8b4b582010-10-29 16:08:23 -0300560 * @dev: the struct rc_dev descriptor of the device
561 * @scancode: the scancode to look for
562 * @return: the corresponding keycode, or KEY_RESERVED
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300563 *
David Härdemand8b4b582010-10-29 16:08:23 -0300564 * This routine is used by drivers which need to convert a scancode to a
565 * keycode. Normally it should not be used since drivers should have no
566 * interest in keycodes.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300567 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300568u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300569{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300570 struct rc_map *rc_map = &dev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700571 unsigned int keycode;
572 unsigned int index;
573 unsigned long flags;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300574
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300575 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700576
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300577 index = ir_lookup_by_scancode(rc_map, scancode);
578 keycode = index < rc_map->len ?
579 rc_map->scan[index].keycode : KEY_RESERVED;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700580
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300581 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700582
Mauro Carvalho Chehab35438942010-04-03 16:53:16 -0300583 if (keycode != KEY_RESERVED)
584 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
Sean Young518f4b22017-07-01 12:13:19 -0400585 dev->device_name, scancode, keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700586
David Härdemanb3074c02010-04-02 15:58:28 -0300587 return keycode;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300588}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300589EXPORT_SYMBOL_GPL(rc_g_keycode_from_table);
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300590
591/**
David Härdeman62c65032010-10-29 16:08:07 -0300592 * ir_do_keyup() - internal function to signal the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300593 * @dev: the struct rc_dev descriptor of the device
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300594 * @sync: whether or not to call input_sync
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300595 *
David Härdeman62c65032010-10-29 16:08:07 -0300596 * This function is used internally to release a keypress, it must be
597 * called with keylock held.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300598 */
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300599static void ir_do_keyup(struct rc_dev *dev, bool sync)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300600{
David Härdemand8b4b582010-10-29 16:08:23 -0300601 if (!dev->keypressed)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300602 return;
603
David Härdemand8b4b582010-10-29 16:08:23 -0300604 IR_dprintk(1, "keyup key 0x%04x\n", dev->last_keycode);
605 input_report_key(dev->input_dev, dev->last_keycode, 0);
Sean Young153a60b2013-07-30 19:00:01 -0300606 led_trigger_event(led_feedback, LED_OFF);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300607 if (sync)
608 input_sync(dev->input_dev);
David Härdemand8b4b582010-10-29 16:08:23 -0300609 dev->keypressed = false;
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300610}
David Härdeman62c65032010-10-29 16:08:07 -0300611
612/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300613 * rc_keyup() - signals the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300614 * @dev: the struct rc_dev descriptor of the device
David Härdeman62c65032010-10-29 16:08:07 -0300615 *
616 * This routine is used to signal that a key has been released on the
617 * remote control.
618 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300619void rc_keyup(struct rc_dev *dev)
David Härdeman62c65032010-10-29 16:08:07 -0300620{
621 unsigned long flags;
David Härdeman62c65032010-10-29 16:08:07 -0300622
David Härdemand8b4b582010-10-29 16:08:23 -0300623 spin_lock_irqsave(&dev->keylock, flags);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300624 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300625 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300626}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300627EXPORT_SYMBOL_GPL(rc_keyup);
David Härdemana374fef2010-04-02 15:58:29 -0300628
629/**
630 * ir_timer_keyup() - generates a keyup event after a timeout
David Härdemand8b4b582010-10-29 16:08:23 -0300631 * @cookie: a pointer to the struct rc_dev for the device
David Härdemana374fef2010-04-02 15:58:29 -0300632 *
633 * This routine will generate a keyup event some time after a keydown event
634 * is generated when no further activity has been detected.
635 */
636static void ir_timer_keyup(unsigned long cookie)
637{
David Härdemand8b4b582010-10-29 16:08:23 -0300638 struct rc_dev *dev = (struct rc_dev *)cookie;
David Härdemana374fef2010-04-02 15:58:29 -0300639 unsigned long flags;
640
641 /*
642 * ir->keyup_jiffies is used to prevent a race condition if a
643 * hardware interrupt occurs at this point and the keyup timer
644 * event is moved further into the future as a result.
645 *
646 * The timer will then be reactivated and this function called
647 * again in the future. We need to exit gracefully in that case
648 * to allow the input subsystem to do its auto-repeat magic or
649 * a keyup event might follow immediately after the keydown.
650 */
David Härdemand8b4b582010-10-29 16:08:23 -0300651 spin_lock_irqsave(&dev->keylock, flags);
652 if (time_is_before_eq_jiffies(dev->keyup_jiffies))
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300653 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300654 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300655}
656
657/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300658 * rc_repeat() - signals that a key is still pressed
David Härdemand8b4b582010-10-29 16:08:23 -0300659 * @dev: the struct rc_dev descriptor of the device
David Härdemana374fef2010-04-02 15:58:29 -0300660 *
661 * This routine is used by IR decoders when a repeat message which does
662 * not include the necessary bits to reproduce the scancode has been
663 * received.
664 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300665void rc_repeat(struct rc_dev *dev)
David Härdemana374fef2010-04-02 15:58:29 -0300666{
667 unsigned long flags;
Sean Youngd57ea872017-08-09 13:19:16 -0400668 unsigned int timeout = protocols[dev->last_protocol].repeat_period;
David Härdemana374fef2010-04-02 15:58:29 -0300669
David Härdemand8b4b582010-10-29 16:08:23 -0300670 spin_lock_irqsave(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300671
David Härdemand8b4b582010-10-29 16:08:23 -0300672 if (!dev->keypressed)
David Härdemana374fef2010-04-02 15:58:29 -0300673 goto out;
674
David Härdeman265a2982017-06-22 15:23:54 -0400675 input_event(dev->input_dev, EV_MSC, MSC_SCAN, dev->last_scancode);
676 input_sync(dev->input_dev);
677
Sean Youngd57ea872017-08-09 13:19:16 -0400678 dev->keyup_jiffies = jiffies + msecs_to_jiffies(timeout);
David Härdemand8b4b582010-10-29 16:08:23 -0300679 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
David Härdemana374fef2010-04-02 15:58:29 -0300680
681out:
David Härdemand8b4b582010-10-29 16:08:23 -0300682 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300683}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300684EXPORT_SYMBOL_GPL(rc_repeat);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300685
686/**
David Härdeman62c65032010-10-29 16:08:07 -0300687 * ir_do_keydown() - internal function to process a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300688 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300689 * @protocol: the protocol of the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300690 * @scancode: the scancode of the keypress
691 * @keycode: the keycode of the keypress
692 * @toggle: the toggle value of the keypress
693 *
694 * This function is used internally to register a keypress, it must be
695 * called with keylock held.
696 */
Sean Young6d741bf2017-08-07 16:20:58 -0400697static void ir_do_keydown(struct rc_dev *dev, enum rc_proto protocol,
David Härdeman120703f2014-04-03 20:31:30 -0300698 u32 scancode, u32 keycode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300699{
David Härdeman99b0f3c2014-04-04 19:06:06 -0300700 bool new_event = (!dev->keypressed ||
David Härdeman120703f2014-04-03 20:31:30 -0300701 dev->last_protocol != protocol ||
David Härdeman99b0f3c2014-04-04 19:06:06 -0300702 dev->last_scancode != scancode ||
David Härdeman120703f2014-04-03 20:31:30 -0300703 dev->last_toggle != toggle);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300704
705 if (new_event && dev->keypressed)
706 ir_do_keyup(dev, false);
707
David Härdemand8b4b582010-10-29 16:08:23 -0300708 input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300709
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300710 if (new_event && keycode != KEY_RESERVED) {
711 /* Register a keypress */
712 dev->keypressed = true;
David Härdeman120703f2014-04-03 20:31:30 -0300713 dev->last_protocol = protocol;
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300714 dev->last_scancode = scancode;
715 dev->last_toggle = toggle;
716 dev->last_keycode = keycode;
David Härdeman62c65032010-10-29 16:08:07 -0300717
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200718 IR_dprintk(1, "%s: key down event, key 0x%04x, protocol 0x%04x, scancode 0x%08x\n",
Sean Young518f4b22017-07-01 12:13:19 -0400719 dev->device_name, keycode, protocol, scancode);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300720 input_report_key(dev->input_dev, keycode, 1);
James Hogan70a2f912014-01-16 19:56:22 -0300721
722 led_trigger_event(led_feedback, LED_FULL);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300723 }
David Härdeman62c65032010-10-29 16:08:07 -0300724
David Härdemand8b4b582010-10-29 16:08:23 -0300725 input_sync(dev->input_dev);
David Härdeman62c65032010-10-29 16:08:07 -0300726}
727
728/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300729 * rc_keydown() - generates input event for a key press
David Härdemand8b4b582010-10-29 16:08:23 -0300730 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300731 * @protocol: the protocol for the keypress
732 * @scancode: the scancode for the keypress
David Härdemana374fef2010-04-02 15:58:29 -0300733 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
734 * support toggle values, this should be set to zero)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300735 *
David Härdemand8b4b582010-10-29 16:08:23 -0300736 * This routine is used to signal that a key has been pressed on the
737 * remote control.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300738 */
Sean Young6d741bf2017-08-07 16:20:58 -0400739void rc_keydown(struct rc_dev *dev, enum rc_proto protocol, u32 scancode,
740 u8 toggle)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300741{
David Härdemana374fef2010-04-02 15:58:29 -0300742 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300743 u32 keycode = rc_g_keycode_from_table(dev, scancode);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300744
David Härdemand8b4b582010-10-29 16:08:23 -0300745 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300746 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300747
David Härdemand8b4b582010-10-29 16:08:23 -0300748 if (dev->keypressed) {
Sean Youngd57ea872017-08-09 13:19:16 -0400749 dev->keyup_jiffies = jiffies +
750 msecs_to_jiffies(protocols[protocol].repeat_period);
David Härdemand8b4b582010-10-29 16:08:23 -0300751 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
David Härdeman62c65032010-10-29 16:08:07 -0300752 }
David Härdemand8b4b582010-10-29 16:08:23 -0300753 spin_unlock_irqrestore(&dev->keylock, flags);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300754}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300755EXPORT_SYMBOL_GPL(rc_keydown);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300756
David Härdeman62c65032010-10-29 16:08:07 -0300757/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300758 * rc_keydown_notimeout() - generates input event for a key press without
David Härdeman62c65032010-10-29 16:08:07 -0300759 * an automatic keyup event at a later time
David Härdemand8b4b582010-10-29 16:08:23 -0300760 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300761 * @protocol: the protocol for the keypress
762 * @scancode: the scancode for the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300763 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
764 * support toggle values, this should be set to zero)
765 *
David Härdemand8b4b582010-10-29 16:08:23 -0300766 * This routine is used to signal that a key has been pressed on the
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300767 * remote control. The driver must manually call rc_keyup() at a later stage.
David Härdeman62c65032010-10-29 16:08:07 -0300768 */
Sean Young6d741bf2017-08-07 16:20:58 -0400769void rc_keydown_notimeout(struct rc_dev *dev, enum rc_proto protocol,
David Härdeman120703f2014-04-03 20:31:30 -0300770 u32 scancode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300771{
772 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300773 u32 keycode = rc_g_keycode_from_table(dev, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300774
David Härdemand8b4b582010-10-29 16:08:23 -0300775 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300776 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
David Härdemand8b4b582010-10-29 16:08:23 -0300777 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300778}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300779EXPORT_SYMBOL_GPL(rc_keydown_notimeout);
David Härdeman62c65032010-10-29 16:08:07 -0300780
Sean Youngb590c0b2016-12-05 19:24:59 -0200781/**
782 * rc_validate_filter() - checks that the scancode and mask are valid and
783 * provides sensible defaults
James Hoganf423ccc2015-03-31 14:48:10 -0300784 * @dev: the struct rc_dev descriptor of the device
Sean Youngb590c0b2016-12-05 19:24:59 -0200785 * @filter: the scancode and mask
786 * @return: 0 or -EINVAL if the filter is not valid
787 */
James Hoganf423ccc2015-03-31 14:48:10 -0300788static int rc_validate_filter(struct rc_dev *dev,
Sean Youngb590c0b2016-12-05 19:24:59 -0200789 struct rc_scancode_filter *filter)
790{
Sean Youngd57ea872017-08-09 13:19:16 -0400791 u32 mask, s = filter->data;
Sean Young6d741bf2017-08-07 16:20:58 -0400792 enum rc_proto protocol = dev->wakeup_protocol;
Sean Youngb590c0b2016-12-05 19:24:59 -0200793
Sean Youngd57ea872017-08-09 13:19:16 -0400794 if (protocol >= ARRAY_SIZE(protocols))
Sean Young2168b412017-08-07 09:21:29 -0400795 return -EINVAL;
796
Sean Youngd57ea872017-08-09 13:19:16 -0400797 mask = protocols[protocol].scancode_bits;
798
Sean Youngb590c0b2016-12-05 19:24:59 -0200799 switch (protocol) {
Sean Young6d741bf2017-08-07 16:20:58 -0400800 case RC_PROTO_NECX:
Sean Youngb590c0b2016-12-05 19:24:59 -0200801 if ((((s >> 16) ^ ~(s >> 8)) & 0xff) == 0)
802 return -EINVAL;
803 break;
Sean Young6d741bf2017-08-07 16:20:58 -0400804 case RC_PROTO_NEC32:
Sean Youngb590c0b2016-12-05 19:24:59 -0200805 if ((((s >> 24) ^ ~(s >> 16)) & 0xff) == 0)
806 return -EINVAL;
807 break;
Sean Young6d741bf2017-08-07 16:20:58 -0400808 case RC_PROTO_RC6_MCE:
Sean Youngb590c0b2016-12-05 19:24:59 -0200809 if ((s & 0xffff0000) != 0x800f0000)
810 return -EINVAL;
811 break;
Sean Young6d741bf2017-08-07 16:20:58 -0400812 case RC_PROTO_RC6_6A_32:
Sean Youngb590c0b2016-12-05 19:24:59 -0200813 if ((s & 0xffff0000) == 0x800f0000)
814 return -EINVAL;
815 break;
816 default:
817 break;
818 }
819
Sean Youngd57ea872017-08-09 13:19:16 -0400820 filter->data &= mask;
821 filter->mask &= mask;
Sean Youngb590c0b2016-12-05 19:24:59 -0200822
James Hoganf423ccc2015-03-31 14:48:10 -0300823 /*
824 * If we have to raw encode the IR for wakeup, we cannot have a mask
825 */
Sean Youngd57ea872017-08-09 13:19:16 -0400826 if (dev->encode_wakeup && filter->mask != 0 && filter->mask != mask)
James Hoganf423ccc2015-03-31 14:48:10 -0300827 return -EINVAL;
828
Sean Youngb590c0b2016-12-05 19:24:59 -0200829 return 0;
830}
831
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300832int rc_open(struct rc_dev *rdev)
833{
834 int rval = 0;
835
836 if (!rdev)
837 return -EINVAL;
838
839 mutex_lock(&rdev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -0200840
Juergen Lockf02dcdd2013-08-16 15:00:24 -0300841 if (!rdev->users++ && rdev->open != NULL)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300842 rval = rdev->open(rdev);
843
844 if (rval)
845 rdev->users--;
846
847 mutex_unlock(&rdev->lock);
848
849 return rval;
850}
851EXPORT_SYMBOL_GPL(rc_open);
852
David Härdemand8b4b582010-10-29 16:08:23 -0300853static int ir_open(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300854{
David Härdemand8b4b582010-10-29 16:08:23 -0300855 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300856
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300857 return rc_open(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300858}
859
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300860void rc_close(struct rc_dev *rdev)
861{
862 if (rdev) {
863 mutex_lock(&rdev->lock);
864
Mauro Carvalho Chehab81b7d142015-04-28 09:43:17 -0300865 if (!--rdev->users && rdev->close != NULL)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300866 rdev->close(rdev);
867
868 mutex_unlock(&rdev->lock);
869 }
870}
871EXPORT_SYMBOL_GPL(rc_close);
872
David Härdemand8b4b582010-10-29 16:08:23 -0300873static void ir_close(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300874{
David Härdemand8b4b582010-10-29 16:08:23 -0300875 struct rc_dev *rdev = input_get_drvdata(idev);
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300876 rc_close(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300877}
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300878
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300879/* class for /sys/class/rc */
David Härdeman40fc5322013-03-06 16:52:10 -0300880static char *rc_devnode(struct device *dev, umode_t *mode)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300881{
882 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
883}
884
David Härdeman40fc5322013-03-06 16:52:10 -0300885static struct class rc_class = {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300886 .name = "rc",
David Härdeman40fc5322013-03-06 16:52:10 -0300887 .devnode = rc_devnode,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300888};
889
David Härdemanc003ab12012-10-11 19:11:54 -0300890/*
891 * These are the protocol textual descriptions that are
892 * used by the sysfs protocols file. Note that the order
893 * of the entries is relevant.
894 */
Heiner Kallweit53df8772015-11-16 17:52:17 -0200895static const struct {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300896 u64 type;
Heiner Kallweit53df8772015-11-16 17:52:17 -0200897 const char *name;
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200898 const char *module_name;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300899} proto_names[] = {
Sean Young6d741bf2017-08-07 16:20:58 -0400900 { RC_PROTO_BIT_NONE, "none", NULL },
901 { RC_PROTO_BIT_OTHER, "other", NULL },
902 { RC_PROTO_BIT_UNKNOWN, "unknown", NULL },
903 { RC_PROTO_BIT_RC5 |
904 RC_PROTO_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
905 { RC_PROTO_BIT_NEC |
906 RC_PROTO_BIT_NECX |
907 RC_PROTO_BIT_NEC32, "nec", "ir-nec-decoder" },
908 { RC_PROTO_BIT_RC6_0 |
909 RC_PROTO_BIT_RC6_6A_20 |
910 RC_PROTO_BIT_RC6_6A_24 |
911 RC_PROTO_BIT_RC6_6A_32 |
912 RC_PROTO_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
913 { RC_PROTO_BIT_JVC, "jvc", "ir-jvc-decoder" },
914 { RC_PROTO_BIT_SONY12 |
915 RC_PROTO_BIT_SONY15 |
916 RC_PROTO_BIT_SONY20, "sony", "ir-sony-decoder" },
917 { RC_PROTO_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
918 { RC_PROTO_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
919 { RC_PROTO_BIT_SHARP, "sharp", "ir-sharp-decoder" },
920 { RC_PROTO_BIT_MCIR2_KBD |
921 RC_PROTO_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" },
922 { RC_PROTO_BIT_XMP, "xmp", "ir-xmp-decoder" },
923 { RC_PROTO_BIT_CEC, "cec", NULL },
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300924};
925
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300926/**
James Hoganab88c662014-02-28 20:17:05 -0300927 * struct rc_filter_attribute - Device attribute relating to a filter type.
928 * @attr: Device attribute.
929 * @type: Filter type.
930 * @mask: false for filter value, true for filter mask.
931 */
932struct rc_filter_attribute {
933 struct device_attribute attr;
934 enum rc_filter_type type;
935 bool mask;
936};
937#define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
938
James Hoganab88c662014-02-28 20:17:05 -0300939#define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
940 struct rc_filter_attribute dev_attr_##_name = { \
941 .attr = __ATTR(_name, _mode, _show, _store), \
942 .type = (_type), \
943 .mask = (_mask), \
944 }
945
David Härdemandd6ff6a2015-07-22 17:55:24 -0300946static bool lirc_is_present(void)
947{
948#if defined(CONFIG_LIRC_MODULE)
949 struct module *lirc;
950
951 mutex_lock(&module_mutex);
952 lirc = find_module("lirc_dev");
953 mutex_unlock(&module_mutex);
954
955 return lirc ? true : false;
956#elif defined(CONFIG_LIRC)
957 return true;
958#else
959 return false;
960#endif
961}
962
James Hoganab88c662014-02-28 20:17:05 -0300963/**
Sean Young0751d332016-12-05 17:08:35 -0200964 * show_protocols() - shows the current IR protocol(s)
David Härdemand8b4b582010-10-29 16:08:23 -0300965 * @device: the device descriptor
David Härdemanda6e1622014-04-03 20:32:16 -0300966 * @mattr: the device attribute struct
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300967 * @buf: a pointer to the output buffer
968 *
969 * This routine is a callback routine for input read the IR protocol type(s).
Sean Young0751d332016-12-05 17:08:35 -0200970 * it is trigged by reading /sys/class/rc/rc?/protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300971 * It returns the protocol names of supported protocols.
972 * Enabled protocols are printed in brackets.
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300973 *
David Härdeman18726a32017-04-27 17:34:08 -0300974 * dev->lock is taken to guard against races between
975 * store_protocols and show_protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300976 */
David Härdemand8b4b582010-10-29 16:08:23 -0300977static ssize_t show_protocols(struct device *device,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300978 struct device_attribute *mattr, char *buf)
979{
David Härdemand8b4b582010-10-29 16:08:23 -0300980 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300981 u64 allowed, enabled;
982 char *tmp = buf;
983 int i;
984
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300985 mutex_lock(&dev->lock);
986
Sean Young0751d332016-12-05 17:08:35 -0200987 enabled = dev->enabled_protocols;
988 allowed = dev->allowed_protocols;
989 if (dev->raw && !allowed)
990 allowed = ir_raw_get_allowed_protocols();
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300991
David Härdemanda6e1622014-04-03 20:32:16 -0300992 mutex_unlock(&dev->lock);
993
994 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
995 __func__, (long long)allowed, (long long)enabled);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300996
997 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
998 if (allowed & enabled & proto_names[i].type)
999 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
1000 else if (allowed & proto_names[i].type)
1001 tmp += sprintf(tmp, "%s ", proto_names[i].name);
David Härdemanc003ab12012-10-11 19:11:54 -03001002
1003 if (allowed & proto_names[i].type)
1004 allowed &= ~proto_names[i].type;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001005 }
1006
David Härdemandd6ff6a2015-07-22 17:55:24 -03001007 if (dev->driver_type == RC_DRIVER_IR_RAW && lirc_is_present())
David Härdeman275ddb42015-05-19 19:03:22 -03001008 tmp += sprintf(tmp, "[lirc] ");
1009
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001010 if (tmp != buf)
1011 tmp--;
1012 *tmp = '\n';
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001013
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001014 return tmp + 1 - buf;
1015}
1016
1017/**
David Härdemanda6e1622014-04-03 20:32:16 -03001018 * parse_protocol_change() - parses a protocol change request
1019 * @protocols: pointer to the bitmask of current protocols
1020 * @buf: pointer to the buffer with a list of changes
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001021 *
David Härdemanda6e1622014-04-03 20:32:16 -03001022 * Writing "+proto" will add a protocol to the protocol mask.
1023 * Writing "-proto" will remove a protocol from protocol mask.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001024 * Writing "proto" will enable only "proto".
1025 * Writing "none" will disable all protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001026 * Returns the number of changes performed or a negative error code.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001027 */
David Härdemanda6e1622014-04-03 20:32:16 -03001028static int parse_protocol_change(u64 *protocols, const char *buf)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001029{
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001030 const char *tmp;
David Härdemanda6e1622014-04-03 20:32:16 -03001031 unsigned count = 0;
1032 bool enable, disable;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001033 u64 mask;
David Härdemanda6e1622014-04-03 20:32:16 -03001034 int i;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001035
David Härdemanda6e1622014-04-03 20:32:16 -03001036 while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001037 if (!*tmp)
1038 break;
1039
1040 if (*tmp == '+') {
1041 enable = true;
1042 disable = false;
1043 tmp++;
1044 } else if (*tmp == '-') {
1045 enable = false;
1046 disable = true;
1047 tmp++;
1048 } else {
1049 enable = false;
1050 disable = false;
1051 }
1052
David Härdemanc003ab12012-10-11 19:11:54 -03001053 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1054 if (!strcasecmp(tmp, proto_names[i].name)) {
1055 mask = proto_names[i].type;
1056 break;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001057 }
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001058 }
1059
David Härdemanc003ab12012-10-11 19:11:54 -03001060 if (i == ARRAY_SIZE(proto_names)) {
David Härdeman275ddb42015-05-19 19:03:22 -03001061 if (!strcasecmp(tmp, "lirc"))
1062 mask = 0;
1063 else {
1064 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
1065 return -EINVAL;
1066 }
David Härdemanc003ab12012-10-11 19:11:54 -03001067 }
1068
1069 count++;
1070
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001071 if (enable)
David Härdemanda6e1622014-04-03 20:32:16 -03001072 *protocols |= mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001073 else if (disable)
David Härdemanda6e1622014-04-03 20:32:16 -03001074 *protocols &= ~mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001075 else
David Härdemanda6e1622014-04-03 20:32:16 -03001076 *protocols = mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001077 }
1078
1079 if (!count) {
1080 IR_dprintk(1, "Protocol not specified\n");
David Härdemanda6e1622014-04-03 20:32:16 -03001081 return -EINVAL;
1082 }
1083
1084 return count;
1085}
1086
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001087static void ir_raw_load_modules(u64 *protocols)
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001088{
1089 u64 available;
1090 int i, ret;
1091
1092 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
Sean Young6d741bf2017-08-07 16:20:58 -04001093 if (proto_names[i].type == RC_PROTO_BIT_NONE ||
1094 proto_names[i].type & (RC_PROTO_BIT_OTHER |
1095 RC_PROTO_BIT_UNKNOWN))
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001096 continue;
1097
1098 available = ir_raw_get_allowed_protocols();
1099 if (!(*protocols & proto_names[i].type & ~available))
1100 continue;
1101
1102 if (!proto_names[i].module_name) {
1103 pr_err("Can't enable IR protocol %s\n",
1104 proto_names[i].name);
1105 *protocols &= ~proto_names[i].type;
1106 continue;
1107 }
1108
1109 ret = request_module("%s", proto_names[i].module_name);
1110 if (ret < 0) {
1111 pr_err("Couldn't load IR protocol module %s\n",
1112 proto_names[i].module_name);
1113 *protocols &= ~proto_names[i].type;
1114 continue;
1115 }
1116 msleep(20);
1117 available = ir_raw_get_allowed_protocols();
1118 if (!(*protocols & proto_names[i].type & ~available))
1119 continue;
1120
Sean Young8caebcd2017-01-19 19:33:49 -02001121 pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001122 proto_names[i].module_name,
1123 proto_names[i].name);
1124 *protocols &= ~proto_names[i].type;
1125 }
1126}
1127
David Härdemanda6e1622014-04-03 20:32:16 -03001128/**
1129 * store_protocols() - changes the current/wakeup IR protocol(s)
1130 * @device: the device descriptor
1131 * @mattr: the device attribute struct
1132 * @buf: a pointer to the input buffer
1133 * @len: length of the input buffer
1134 *
1135 * This routine is for changing the IR protocol type.
1136 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
1137 * See parse_protocol_change() for the valid commands.
1138 * Returns @len on success or a negative error code.
1139 *
David Härdeman18726a32017-04-27 17:34:08 -03001140 * dev->lock is taken to guard against races between
1141 * store_protocols and show_protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001142 */
1143static ssize_t store_protocols(struct device *device,
1144 struct device_attribute *mattr,
1145 const char *buf, size_t len)
1146{
1147 struct rc_dev *dev = to_rc_dev(device);
David Härdemanda6e1622014-04-03 20:32:16 -03001148 u64 *current_protocols;
David Härdemanda6e1622014-04-03 20:32:16 -03001149 struct rc_scancode_filter *filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001150 u64 old_protocols, new_protocols;
1151 ssize_t rc;
1152
Sean Young0751d332016-12-05 17:08:35 -02001153 IR_dprintk(1, "Normal protocol change requested\n");
1154 current_protocols = &dev->enabled_protocols;
1155 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001156
Sean Young0751d332016-12-05 17:08:35 -02001157 if (!dev->change_protocol) {
David Härdemanda6e1622014-04-03 20:32:16 -03001158 IR_dprintk(1, "Protocol switching not supported\n");
1159 return -EINVAL;
1160 }
1161
1162 mutex_lock(&dev->lock);
1163
1164 old_protocols = *current_protocols;
1165 new_protocols = old_protocols;
1166 rc = parse_protocol_change(&new_protocols, buf);
1167 if (rc < 0)
1168 goto out;
1169
Sean Young0751d332016-12-05 17:08:35 -02001170 rc = dev->change_protocol(dev, &new_protocols);
David Härdemanda6e1622014-04-03 20:32:16 -03001171 if (rc < 0) {
1172 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
1173 (long long)new_protocols);
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001174 goto out;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001175 }
1176
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001177 if (dev->driver_type == RC_DRIVER_IR_RAW)
1178 ir_raw_load_modules(&new_protocols);
1179
James Hogan983c5bd2014-12-08 13:17:07 -03001180 if (new_protocols != old_protocols) {
1181 *current_protocols = new_protocols;
1182 IR_dprintk(1, "Protocols changed to 0x%llx\n",
1183 (long long)new_protocols);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001184 }
1185
James Hogan6bea25a2014-02-28 20:17:06 -03001186 /*
James Hogan983c5bd2014-12-08 13:17:07 -03001187 * If a protocol change was attempted the filter may need updating, even
1188 * if the actual protocol mask hasn't changed (since the driver may have
1189 * cleared the filter).
James Hogan6bea25a2014-02-28 20:17:06 -03001190 * Try setting the same filter with the new protocol (if any).
1191 * Fall back to clearing the filter.
1192 */
Sean Young0751d332016-12-05 17:08:35 -02001193 if (dev->s_filter && filter->mask) {
David Härdemanda6e1622014-04-03 20:32:16 -03001194 if (new_protocols)
Sean Young0751d332016-12-05 17:08:35 -02001195 rc = dev->s_filter(dev, filter);
David Härdemanda6e1622014-04-03 20:32:16 -03001196 else
1197 rc = -1;
David Härdeman23c843b2014-04-04 19:06:01 -03001198
David Härdemanda6e1622014-04-03 20:32:16 -03001199 if (rc < 0) {
1200 filter->data = 0;
1201 filter->mask = 0;
Sean Young0751d332016-12-05 17:08:35 -02001202 dev->s_filter(dev, filter);
James Hogan6bea25a2014-02-28 20:17:06 -03001203 }
James Hogan6bea25a2014-02-28 20:17:06 -03001204 }
1205
David Härdemanda6e1622014-04-03 20:32:16 -03001206 rc = len;
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001207
1208out:
1209 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001210 return rc;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001211}
1212
James Hogan00942d12014-01-17 10:58:49 -03001213/**
James Hogan00942d12014-01-17 10:58:49 -03001214 * show_filter() - shows the current scancode filter value or mask
1215 * @device: the device descriptor
1216 * @attr: the device attribute struct
1217 * @buf: a pointer to the output buffer
1218 *
1219 * This routine is a callback routine to read a scancode filter value or mask.
1220 * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
1221 * It prints the current scancode filter value or mask of the appropriate filter
1222 * type in hexadecimal into @buf and returns the size of the buffer.
1223 *
1224 * Bits of the filter value corresponding to set bits in the filter mask are
1225 * compared against input scancodes and non-matching scancodes are discarded.
1226 *
David Härdeman18726a32017-04-27 17:34:08 -03001227 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001228 * store_filter and show_filter.
1229 */
1230static ssize_t show_filter(struct device *device,
1231 struct device_attribute *attr,
1232 char *buf)
1233{
1234 struct rc_dev *dev = to_rc_dev(device);
1235 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001236 struct rc_scancode_filter *filter;
James Hogan00942d12014-01-17 10:58:49 -03001237 u32 val;
1238
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001239 mutex_lock(&dev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001240
David Härdemanda6e1622014-04-03 20:32:16 -03001241 if (fattr->type == RC_FILTER_NORMAL)
David Härdemanc5540fb2014-04-03 20:32:21 -03001242 filter = &dev->scancode_filter;
James Hogan00942d12014-01-17 10:58:49 -03001243 else
David Härdemanc5540fb2014-04-03 20:32:21 -03001244 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001245
David Härdemanda6e1622014-04-03 20:32:16 -03001246 if (fattr->mask)
1247 val = filter->mask;
1248 else
1249 val = filter->data;
James Hogan00942d12014-01-17 10:58:49 -03001250 mutex_unlock(&dev->lock);
1251
1252 return sprintf(buf, "%#x\n", val);
1253}
1254
1255/**
1256 * store_filter() - changes the scancode filter value
1257 * @device: the device descriptor
1258 * @attr: the device attribute struct
1259 * @buf: a pointer to the input buffer
1260 * @len: length of the input buffer
1261 *
1262 * This routine is for changing a scancode filter value or mask.
1263 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
1264 * Returns -EINVAL if an invalid filter value for the current protocol was
1265 * specified or if scancode filtering is not supported by the driver, otherwise
1266 * returns @len.
1267 *
1268 * Bits of the filter value corresponding to set bits in the filter mask are
1269 * compared against input scancodes and non-matching scancodes are discarded.
1270 *
David Härdeman18726a32017-04-27 17:34:08 -03001271 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001272 * store_filter and show_filter.
1273 */
1274static ssize_t store_filter(struct device *device,
1275 struct device_attribute *attr,
David Härdemanda6e1622014-04-03 20:32:16 -03001276 const char *buf, size_t len)
James Hogan00942d12014-01-17 10:58:49 -03001277{
1278 struct rc_dev *dev = to_rc_dev(device);
1279 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001280 struct rc_scancode_filter new_filter, *filter;
James Hogan00942d12014-01-17 10:58:49 -03001281 int ret;
1282 unsigned long val;
David Härdeman23c843b2014-04-04 19:06:01 -03001283 int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
James Hogan00942d12014-01-17 10:58:49 -03001284
James Hogan00942d12014-01-17 10:58:49 -03001285 ret = kstrtoul(buf, 0, &val);
1286 if (ret < 0)
1287 return ret;
1288
David Härdemanda6e1622014-04-03 20:32:16 -03001289 if (fattr->type == RC_FILTER_NORMAL) {
1290 set_filter = dev->s_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001291 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001292 } else {
1293 set_filter = dev->s_wakeup_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001294 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001295 }
1296
David Härdeman99b0f3c2014-04-04 19:06:06 -03001297 if (!set_filter)
1298 return -EINVAL;
James Hogan00942d12014-01-17 10:58:49 -03001299
1300 mutex_lock(&dev->lock);
1301
David Härdemanda6e1622014-04-03 20:32:16 -03001302 new_filter = *filter;
James Hogan00942d12014-01-17 10:58:49 -03001303 if (fattr->mask)
David Härdemanda6e1622014-04-03 20:32:16 -03001304 new_filter.mask = val;
James Hogan00942d12014-01-17 10:58:49 -03001305 else
David Härdemanda6e1622014-04-03 20:32:16 -03001306 new_filter.data = val;
David Härdeman23c843b2014-04-04 19:06:01 -03001307
Sean Young0751d332016-12-05 17:08:35 -02001308 if (fattr->type == RC_FILTER_WAKEUP) {
Sean Youngb590c0b2016-12-05 19:24:59 -02001309 /*
1310 * Refuse to set a filter unless a protocol is enabled
1311 * and the filter is valid for that protocol
1312 */
Sean Young6d741bf2017-08-07 16:20:58 -04001313 if (dev->wakeup_protocol != RC_PROTO_UNKNOWN)
James Hoganf423ccc2015-03-31 14:48:10 -03001314 ret = rc_validate_filter(dev, &new_filter);
Sean Youngb590c0b2016-12-05 19:24:59 -02001315 else
Sean Young0751d332016-12-05 17:08:35 -02001316 ret = -EINVAL;
Sean Youngb590c0b2016-12-05 19:24:59 -02001317
1318 if (ret != 0)
Sean Young0751d332016-12-05 17:08:35 -02001319 goto unlock;
Sean Young0751d332016-12-05 17:08:35 -02001320 }
1321
1322 if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
1323 val) {
James Hogan6bea25a2014-02-28 20:17:06 -03001324 /* refuse to set a filter unless a protocol is enabled */
1325 ret = -EINVAL;
1326 goto unlock;
1327 }
David Härdeman23c843b2014-04-04 19:06:01 -03001328
David Härdemanda6e1622014-04-03 20:32:16 -03001329 ret = set_filter(dev, &new_filter);
David Härdeman99b0f3c2014-04-04 19:06:06 -03001330 if (ret < 0)
1331 goto unlock;
James Hogan00942d12014-01-17 10:58:49 -03001332
David Härdemanda6e1622014-04-03 20:32:16 -03001333 *filter = new_filter;
James Hogan00942d12014-01-17 10:58:49 -03001334
1335unlock:
1336 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001337 return (ret < 0) ? ret : len;
James Hogan00942d12014-01-17 10:58:49 -03001338}
1339
Sean Young0751d332016-12-05 17:08:35 -02001340/**
1341 * show_wakeup_protocols() - shows the wakeup IR protocol
1342 * @device: the device descriptor
1343 * @mattr: the device attribute struct
1344 * @buf: a pointer to the output buffer
1345 *
1346 * This routine is a callback routine for input read the IR protocol type(s).
1347 * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols.
1348 * It returns the protocol names of supported protocols.
1349 * The enabled protocols are printed in brackets.
1350 *
David Härdeman18726a32017-04-27 17:34:08 -03001351 * dev->lock is taken to guard against races between
1352 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001353 */
1354static ssize_t show_wakeup_protocols(struct device *device,
1355 struct device_attribute *mattr,
1356 char *buf)
1357{
1358 struct rc_dev *dev = to_rc_dev(device);
1359 u64 allowed;
Sean Young6d741bf2017-08-07 16:20:58 -04001360 enum rc_proto enabled;
Sean Young0751d332016-12-05 17:08:35 -02001361 char *tmp = buf;
1362 int i;
1363
Sean Young0751d332016-12-05 17:08:35 -02001364 mutex_lock(&dev->lock);
1365
1366 allowed = dev->allowed_wakeup_protocols;
1367 enabled = dev->wakeup_protocol;
1368
1369 mutex_unlock(&dev->lock);
1370
1371 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - %d\n",
1372 __func__, (long long)allowed, enabled);
1373
Sean Youngd57ea872017-08-09 13:19:16 -04001374 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001375 if (allowed & (1ULL << i)) {
1376 if (i == enabled)
Sean Youngd57ea872017-08-09 13:19:16 -04001377 tmp += sprintf(tmp, "[%s] ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001378 else
Sean Youngd57ea872017-08-09 13:19:16 -04001379 tmp += sprintf(tmp, "%s ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001380 }
1381 }
1382
1383 if (tmp != buf)
1384 tmp--;
1385 *tmp = '\n';
1386
1387 return tmp + 1 - buf;
1388}
1389
1390/**
1391 * store_wakeup_protocols() - changes the wakeup IR protocol(s)
1392 * @device: the device descriptor
1393 * @mattr: the device attribute struct
1394 * @buf: a pointer to the input buffer
1395 * @len: length of the input buffer
1396 *
1397 * This routine is for changing the IR protocol type.
1398 * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols.
1399 * Returns @len on success or a negative error code.
1400 *
David Härdeman18726a32017-04-27 17:34:08 -03001401 * dev->lock is taken to guard against races between
1402 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001403 */
1404static ssize_t store_wakeup_protocols(struct device *device,
1405 struct device_attribute *mattr,
1406 const char *buf, size_t len)
1407{
1408 struct rc_dev *dev = to_rc_dev(device);
Sean Young6d741bf2017-08-07 16:20:58 -04001409 enum rc_proto protocol;
Sean Young0751d332016-12-05 17:08:35 -02001410 ssize_t rc;
1411 u64 allowed;
1412 int i;
1413
Sean Young0751d332016-12-05 17:08:35 -02001414 mutex_lock(&dev->lock);
1415
1416 allowed = dev->allowed_wakeup_protocols;
1417
1418 if (sysfs_streq(buf, "none")) {
Sean Young6d741bf2017-08-07 16:20:58 -04001419 protocol = RC_PROTO_UNKNOWN;
Sean Young0751d332016-12-05 17:08:35 -02001420 } else {
Sean Youngd57ea872017-08-09 13:19:16 -04001421 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001422 if ((allowed & (1ULL << i)) &&
Sean Youngd57ea872017-08-09 13:19:16 -04001423 sysfs_streq(buf, protocols[i].name)) {
Sean Young0751d332016-12-05 17:08:35 -02001424 protocol = i;
1425 break;
1426 }
1427 }
1428
Sean Youngd57ea872017-08-09 13:19:16 -04001429 if (i == ARRAY_SIZE(protocols)) {
Sean Young0751d332016-12-05 17:08:35 -02001430 rc = -EINVAL;
1431 goto out;
1432 }
James Hoganf423ccc2015-03-31 14:48:10 -03001433
1434 if (dev->encode_wakeup) {
1435 u64 mask = 1ULL << protocol;
1436
1437 ir_raw_load_modules(&mask);
1438 if (!mask) {
1439 rc = -EINVAL;
1440 goto out;
1441 }
1442 }
Sean Young0751d332016-12-05 17:08:35 -02001443 }
1444
1445 if (dev->wakeup_protocol != protocol) {
1446 dev->wakeup_protocol = protocol;
1447 IR_dprintk(1, "Wakeup protocol changed to %d\n", protocol);
1448
Sean Young6d741bf2017-08-07 16:20:58 -04001449 if (protocol == RC_PROTO_RC6_MCE)
Sean Young0751d332016-12-05 17:08:35 -02001450 dev->scancode_wakeup_filter.data = 0x800f0000;
1451 else
1452 dev->scancode_wakeup_filter.data = 0;
1453 dev->scancode_wakeup_filter.mask = 0;
1454
1455 rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
1456 if (rc == 0)
1457 rc = len;
1458 } else {
1459 rc = len;
1460 }
1461
1462out:
1463 mutex_unlock(&dev->lock);
1464 return rc;
1465}
1466
David Härdemand8b4b582010-10-29 16:08:23 -03001467static void rc_dev_release(struct device *device)
1468{
Max Kellermann47cae1e2016-03-21 08:33:05 -03001469 struct rc_dev *dev = to_rc_dev(device);
1470
1471 kfree(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001472}
1473
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001474#define ADD_HOTPLUG_VAR(fmt, val...) \
1475 do { \
1476 int err = add_uevent_var(env, fmt, val); \
1477 if (err) \
1478 return err; \
1479 } while (0)
1480
1481static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1482{
David Härdemand8b4b582010-10-29 16:08:23 -03001483 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001484
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001485 if (dev->rc_map.name)
1486 ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
David Härdemand8b4b582010-10-29 16:08:23 -03001487 if (dev->driver_name)
1488 ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
Sean Youngb9f407e2017-09-01 11:34:23 -03001489 if (dev->device_name)
1490 ADD_HOTPLUG_VAR("DEV_NAME=%s", dev->device_name);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001491
1492 return 0;
1493}
1494
1495/*
1496 * Static device attribute struct with the sysfs attributes for IR's
1497 */
Sean Young6d75db32017-09-01 11:30:50 -03001498static struct device_attribute dev_attr_ro_protocols =
1499__ATTR(protocols, 0444, show_protocols, NULL);
1500static struct device_attribute dev_attr_rw_protocols =
1501__ATTR(protocols, 0644, show_protocols, store_protocols);
Sean Young0751d332016-12-05 17:08:35 -02001502static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
1503 store_wakeup_protocols);
James Hogan00942d12014-01-17 10:58:49 -03001504static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
1505 show_filter, store_filter, RC_FILTER_NORMAL, false);
1506static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
1507 show_filter, store_filter, RC_FILTER_NORMAL, true);
1508static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
1509 show_filter, store_filter, RC_FILTER_WAKEUP, false);
1510static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
1511 show_filter, store_filter, RC_FILTER_WAKEUP, true);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001512
Sean Young6d75db32017-09-01 11:30:50 -03001513static struct attribute *rc_dev_rw_protocol_attrs[] = {
1514 &dev_attr_rw_protocols.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001515 NULL,
1516};
1517
Sean Young6d75db32017-09-01 11:30:50 -03001518static const struct attribute_group rc_dev_rw_protocol_attr_grp = {
1519 .attrs = rc_dev_rw_protocol_attrs,
1520};
1521
1522static struct attribute *rc_dev_ro_protocol_attrs[] = {
1523 &dev_attr_ro_protocols.attr,
1524 NULL,
1525};
1526
1527static const struct attribute_group rc_dev_ro_protocol_attr_grp = {
1528 .attrs = rc_dev_ro_protocol_attrs,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001529};
1530
David Härdeman99b0f3c2014-04-04 19:06:06 -03001531static struct attribute *rc_dev_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001532 &dev_attr_filter.attr.attr,
1533 &dev_attr_filter_mask.attr.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001534 NULL,
1535};
1536
Arvind Yadavdb681022017-07-07 04:23:54 -04001537static const struct attribute_group rc_dev_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001538 .attrs = rc_dev_filter_attrs,
1539};
1540
1541static struct attribute *rc_dev_wakeup_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001542 &dev_attr_wakeup_filter.attr.attr,
1543 &dev_attr_wakeup_filter_mask.attr.attr,
Sean Young0751d332016-12-05 17:08:35 -02001544 &dev_attr_wakeup_protocols.attr,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001545 NULL,
1546};
1547
Arvind Yadavdb681022017-07-07 04:23:54 -04001548static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001549 .attrs = rc_dev_wakeup_filter_attrs,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001550};
1551
Bhumika Goyalf03f02f2017-08-19 05:22:15 -03001552static const struct device_type rc_dev_type = {
David Härdemand8b4b582010-10-29 16:08:23 -03001553 .release = rc_dev_release,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001554 .uevent = rc_dev_uevent,
1555};
1556
Andi Shyti0f7499f2016-12-16 06:50:58 -02001557struct rc_dev *rc_allocate_device(enum rc_driver_type type)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001558{
David Härdemand8b4b582010-10-29 16:08:23 -03001559 struct rc_dev *dev;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001560
David Härdemand8b4b582010-10-29 16:08:23 -03001561 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1562 if (!dev)
1563 return NULL;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001564
Andi Shytid34aee12016-12-16 04:12:15 -02001565 if (type != RC_DRIVER_IR_RAW_TX) {
1566 dev->input_dev = input_allocate_device();
1567 if (!dev->input_dev) {
1568 kfree(dev);
1569 return NULL;
1570 }
1571
1572 dev->input_dev->getkeycode = ir_getkeycode;
1573 dev->input_dev->setkeycode = ir_setkeycode;
1574 input_set_drvdata(dev->input_dev, dev);
1575
1576 setup_timer(&dev->timer_keyup, ir_timer_keyup,
1577 (unsigned long)dev);
1578
1579 spin_lock_init(&dev->rc_map.lock);
1580 spin_lock_init(&dev->keylock);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001581 }
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001582 mutex_init(&dev->lock);
David Härdemand8b4b582010-10-29 16:08:23 -03001583
1584 dev->dev.type = &rc_dev_type;
David Härdeman40fc5322013-03-06 16:52:10 -03001585 dev->dev.class = &rc_class;
David Härdemand8b4b582010-10-29 16:08:23 -03001586 device_initialize(&dev->dev);
1587
Andi Shyti0f7499f2016-12-16 06:50:58 -02001588 dev->driver_type = type;
1589
David Härdemand8b4b582010-10-29 16:08:23 -03001590 __module_get(THIS_MODULE);
1591 return dev;
1592}
1593EXPORT_SYMBOL_GPL(rc_allocate_device);
1594
1595void rc_free_device(struct rc_dev *dev)
1596{
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001597 if (!dev)
1598 return;
1599
Markus Elfring3dd94f02014-11-20 09:01:32 -03001600 input_free_device(dev->input_dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001601
1602 put_device(&dev->dev);
1603
Max Kellermann47cae1e2016-03-21 08:33:05 -03001604 /* kfree(dev) will be called by the callback function
1605 rc_dev_release() */
1606
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001607 module_put(THIS_MODULE);
David Härdemand8b4b582010-10-29 16:08:23 -03001608}
1609EXPORT_SYMBOL_GPL(rc_free_device);
1610
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001611static void devm_rc_alloc_release(struct device *dev, void *res)
1612{
1613 rc_free_device(*(struct rc_dev **)res);
1614}
1615
Andi Shyti0f7499f2016-12-16 06:50:58 -02001616struct rc_dev *devm_rc_allocate_device(struct device *dev,
1617 enum rc_driver_type type)
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001618{
1619 struct rc_dev **dr, *rc;
1620
1621 dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
1622 if (!dr)
1623 return NULL;
1624
Andi Shyti0f7499f2016-12-16 06:50:58 -02001625 rc = rc_allocate_device(type);
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001626 if (!rc) {
1627 devres_free(dr);
1628 return NULL;
1629 }
1630
1631 rc->dev.parent = dev;
1632 rc->managed_alloc = true;
1633 *dr = rc;
1634 devres_add(dev, dr);
1635
1636 return rc;
1637}
1638EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
1639
David Härdemanf56928a2017-05-03 07:04:00 -03001640static int rc_prepare_rx_device(struct rc_dev *dev)
David Härdemand8b4b582010-10-29 16:08:23 -03001641{
David Härdemanfcb13092015-05-19 19:03:17 -03001642 int rc;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001643 struct rc_map *rc_map;
Sean Young6d741bf2017-08-07 16:20:58 -04001644 u64 rc_proto;
David Härdemand8b4b582010-10-29 16:08:23 -03001645
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001646 if (!dev->map_name)
David Härdemand8b4b582010-10-29 16:08:23 -03001647 return -EINVAL;
1648
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001649 rc_map = rc_map_get(dev->map_name);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001650 if (!rc_map)
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001651 rc_map = rc_map_get(RC_MAP_EMPTY);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001652 if (!rc_map || !rc_map->scan || rc_map->size == 0)
David Härdemand8b4b582010-10-29 16:08:23 -03001653 return -EINVAL;
1654
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001655 rc = ir_setkeytable(dev, rc_map);
1656 if (rc)
1657 return rc;
1658
Sean Young6d741bf2017-08-07 16:20:58 -04001659 rc_proto = BIT_ULL(rc_map->rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001660
Sean Young831c4c82017-09-01 09:55:59 -03001661 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1662 dev->enabled_protocols = dev->allowed_protocols;
1663
Sean Young41380862017-02-22 18:48:01 -03001664 if (dev->change_protocol) {
Sean Young6d741bf2017-08-07 16:20:58 -04001665 rc = dev->change_protocol(dev, &rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001666 if (rc < 0)
1667 goto out_table;
Sean Young6d741bf2017-08-07 16:20:58 -04001668 dev->enabled_protocols = rc_proto;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001669 }
1670
Sean Young41380862017-02-22 18:48:01 -03001671 if (dev->driver_type == RC_DRIVER_IR_RAW)
Sean Young6d741bf2017-08-07 16:20:58 -04001672 ir_raw_load_modules(&rc_proto);
Sean Young41380862017-02-22 18:48:01 -03001673
David Härdemand8b4b582010-10-29 16:08:23 -03001674 set_bit(EV_KEY, dev->input_dev->evbit);
1675 set_bit(EV_REP, dev->input_dev->evbit);
1676 set_bit(EV_MSC, dev->input_dev->evbit);
1677 set_bit(MSC_SCAN, dev->input_dev->mscbit);
1678 if (dev->open)
1679 dev->input_dev->open = ir_open;
1680 if (dev->close)
1681 dev->input_dev->close = ir_close;
1682
David Härdemanb2aceb72017-04-27 17:33:58 -03001683 dev->input_dev->dev.parent = &dev->dev;
1684 memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
1685 dev->input_dev->phys = dev->input_phys;
Sean Young518f4b22017-07-01 12:13:19 -04001686 dev->input_dev->name = dev->device_name;
David Härdemanb2aceb72017-04-27 17:33:58 -03001687
David Härdemanf56928a2017-05-03 07:04:00 -03001688 return 0;
1689
1690out_table:
1691 ir_free_table(&dev->rc_map);
1692
1693 return rc;
1694}
1695
1696static int rc_setup_rx_device(struct rc_dev *dev)
1697{
1698 int rc;
1699
David Härdemanb2aceb72017-04-27 17:33:58 -03001700 /* rc_open will be called here */
1701 rc = input_register_device(dev->input_dev);
1702 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001703 return rc;
David Härdemanb2aceb72017-04-27 17:33:58 -03001704
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001705 /*
1706 * Default delay of 250ms is too short for some protocols, especially
1707 * since the timeout is currently set to 250ms. Increase it to 500ms,
1708 * to avoid wrong repetition of the keycodes. Note that this must be
1709 * set after the call to input_register_device().
1710 */
1711 dev->input_dev->rep[REP_DELAY] = 500;
1712
1713 /*
1714 * As a repeat event on protocols like RC-5 and NEC take as long as
1715 * 110/114ms, using 33ms as a repeat period is not the right thing
1716 * to do.
1717 */
1718 dev->input_dev->rep[REP_PERIOD] = 125;
1719
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001720 return 0;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001721}
1722
1723static void rc_free_rx_device(struct rc_dev *dev)
1724{
David Härdemanf56928a2017-05-03 07:04:00 -03001725 if (!dev)
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001726 return;
1727
David Härdemanf56928a2017-05-03 07:04:00 -03001728 if (dev->input_dev) {
1729 input_unregister_device(dev->input_dev);
1730 dev->input_dev = NULL;
1731 }
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001732
David Härdemanf56928a2017-05-03 07:04:00 -03001733 ir_free_table(&dev->rc_map);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001734}
1735
1736int rc_register_device(struct rc_dev *dev)
1737{
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001738 const char *path;
1739 int attr = 0;
1740 int minor;
1741 int rc;
1742
1743 if (!dev)
1744 return -EINVAL;
1745
David Härdemanfcb13092015-05-19 19:03:17 -03001746 minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
1747 if (minor < 0)
1748 return minor;
1749
1750 dev->minor = minor;
1751 dev_set_name(&dev->dev, "rc%u", dev->minor);
1752 dev_set_drvdata(&dev->dev, dev);
Mauro Carvalho Chehab587d1b02014-01-14 16:27:55 -03001753
David Härdeman99b0f3c2014-04-04 19:06:06 -03001754 dev->dev.groups = dev->sysfs_groups;
Sean Young6d75db32017-09-01 11:30:50 -03001755 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1756 dev->sysfs_groups[attr++] = &rc_dev_ro_protocol_attr_grp;
1757 else if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
1758 dev->sysfs_groups[attr++] = &rc_dev_rw_protocol_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001759 if (dev->s_filter)
David Härdeman120703f2014-04-03 20:31:30 -03001760 dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001761 if (dev->s_wakeup_filter)
1762 dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001763 dev->sysfs_groups[attr++] = NULL;
1764
David Härdemanf56928a2017-05-03 07:04:00 -03001765 if (dev->driver_type == RC_DRIVER_IR_RAW ||
1766 dev->driver_type == RC_DRIVER_IR_RAW_TX) {
1767 rc = ir_raw_event_prepare(dev);
1768 if (rc < 0)
1769 goto out_minor;
1770 }
1771
1772 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1773 rc = rc_prepare_rx_device(dev);
1774 if (rc)
1775 goto out_raw;
1776 }
1777
David Härdemand8b4b582010-10-29 16:08:23 -03001778 rc = device_add(&dev->dev);
1779 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001780 goto out_rx_free;
David Härdemand8b4b582010-10-29 16:08:23 -03001781
David Härdemand8b4b582010-10-29 16:08:23 -03001782 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Heiner Kallweit4dc0e902015-10-29 19:39:06 -02001783 dev_info(&dev->dev, "%s as %s\n",
Sean Young518f4b22017-07-01 12:13:19 -04001784 dev->device_name ?: "Unspecified device", path ?: "N/A");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001785 kfree(path);
1786
Sean Young5df62772017-02-23 06:11:21 -03001787 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1788 rc = rc_setup_rx_device(dev);
1789 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001790 goto out_dev;
1791 }
1792
1793 if (dev->driver_type == RC_DRIVER_IR_RAW ||
1794 dev->driver_type == RC_DRIVER_IR_RAW_TX) {
1795 rc = ir_raw_event_register(dev);
1796 if (rc < 0)
1797 goto out_rx;
David Härdemand8b4b582010-10-29 16:08:23 -03001798 }
1799
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001800 IR_dprintk(1, "Registered rc%u (driver: %s)\n",
David Härdemanfcb13092015-05-19 19:03:17 -03001801 dev->minor,
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001802 dev->driver_name ? dev->driver_name : "unknown");
David Härdemand8b4b582010-10-29 16:08:23 -03001803
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001804 return 0;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001805
David Härdemanf56928a2017-05-03 07:04:00 -03001806out_rx:
1807 rc_free_rx_device(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001808out_dev:
1809 device_del(&dev->dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001810out_rx_free:
1811 ir_free_table(&dev->rc_map);
1812out_raw:
1813 ir_raw_event_free(dev);
1814out_minor:
David Härdemanfcb13092015-05-19 19:03:17 -03001815 ida_simple_remove(&rc_ida, minor);
David Härdemand8b4b582010-10-29 16:08:23 -03001816 return rc;
1817}
1818EXPORT_SYMBOL_GPL(rc_register_device);
1819
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001820static void devm_rc_release(struct device *dev, void *res)
1821{
1822 rc_unregister_device(*(struct rc_dev **)res);
1823}
1824
1825int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
1826{
1827 struct rc_dev **dr;
1828 int ret;
1829
1830 dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
1831 if (!dr)
1832 return -ENOMEM;
1833
1834 ret = rc_register_device(dev);
1835 if (ret) {
1836 devres_free(dr);
1837 return ret;
1838 }
1839
1840 *dr = dev;
1841 devres_add(parent, dr);
1842
1843 return 0;
1844}
1845EXPORT_SYMBOL_GPL(devm_rc_register_device);
1846
David Härdemand8b4b582010-10-29 16:08:23 -03001847void rc_unregister_device(struct rc_dev *dev)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001848{
David Härdemand8b4b582010-10-29 16:08:23 -03001849 if (!dev)
1850 return;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001851
David Härdemand8b4b582010-10-29 16:08:23 -03001852 del_timer_sync(&dev->timer_keyup);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001853
David Härdemand8b4b582010-10-29 16:08:23 -03001854 if (dev->driver_type == RC_DRIVER_IR_RAW)
1855 ir_raw_event_unregister(dev);
1856
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001857 rc_free_rx_device(dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001858
1859 device_del(&dev->dev);
1860
David Härdemanfcb13092015-05-19 19:03:17 -03001861 ida_simple_remove(&rc_ida, dev->minor);
1862
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001863 if (!dev->managed_alloc)
1864 rc_free_device(dev);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001865}
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001866
David Härdemand8b4b582010-10-29 16:08:23 -03001867EXPORT_SYMBOL_GPL(rc_unregister_device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001868
1869/*
1870 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1871 */
1872
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001873static int __init rc_core_init(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001874{
David Härdeman40fc5322013-03-06 16:52:10 -03001875 int rc = class_register(&rc_class);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001876 if (rc) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -02001877 pr_err("rc_core: unable to register rc class\n");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001878 return rc;
1879 }
1880
Sean Young153a60b2013-07-30 19:00:01 -03001881 led_trigger_register_simple("rc-feedback", &led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001882 rc_map_register(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001883
1884 return 0;
1885}
1886
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001887static void __exit rc_core_exit(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001888{
David Härdeman40fc5322013-03-06 16:52:10 -03001889 class_unregister(&rc_class);
Sean Young153a60b2013-07-30 19:00:01 -03001890 led_trigger_unregister_simple(led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001891 rc_map_unregister(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001892}
1893
David Härdemane76d4ce2013-03-06 16:52:15 -03001894subsys_initcall(rc_core_init);
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001895module_exit(rc_core_exit);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001896
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001897int rc_core_debug; /* ir_debug level (0,1,2) */
1898EXPORT_SYMBOL_GPL(rc_core_debug);
1899module_param_named(debug, rc_core_debug, int, 0644);
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03001900
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02001901MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03001902MODULE_LICENSE("GPL");