blob: 17950e29d4e3ec72a18e4ad2bee2286c99ab99e3 [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
David Härdemanb3074c02010-04-02 15:58:28 -0300443 for (i = 0; i < from->size; i++) {
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300444 index = ir_establish_scancode(dev, rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700445 from->scan[i].scancode, false);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300446 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700447 rc = -ENOMEM;
David Härdemanb3074c02010-04-02 15:58:28 -0300448 break;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700449 }
450
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300451 ir_update_mapping(dev, rc_map, index,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700452 from->scan[i].keycode);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300453 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700454
455 if (rc)
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300456 ir_free_table(rc_map);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700457
David Härdemanb3074c02010-04-02 15:58:28 -0300458 return rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300459}
460
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300461static int rc_map_cmp(const void *key, const void *elt)
462{
463 const unsigned int *scancode = key;
464 const struct rc_map_table *e = elt;
465
466 if (*scancode < e->scancode)
467 return -1;
468 else if (*scancode > e->scancode)
469 return 1;
470 return 0;
471}
472
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300473/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700474 * ir_lookup_by_scancode() - locate mapping by scancode
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300475 * @rc_map: the struct rc_map to search
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700476 * @scancode: scancode to look for in the table
477 * @return: index in the table, -1U if not found
478 *
479 * This routine performs binary search in RC keykeymap table for
480 * given scancode.
481 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300482static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700483 unsigned int scancode)
484{
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300485 struct rc_map_table *res;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700486
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300487 res = bsearch(&scancode, rc_map->scan, rc_map->len,
488 sizeof(struct rc_map_table), rc_map_cmp);
489 if (!res)
490 return -1U;
491 else
492 return res - rc_map->scan;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700493}
494
495/**
David Härdemanb3074c02010-04-02 15:58:28 -0300496 * ir_getkeycode() - get a keycode from the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300497 * @idev: the struct input_dev device descriptor
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300498 * @scancode: the desired scancode
David Härdemanb3074c02010-04-02 15:58:28 -0300499 * @keycode: used to return the keycode, if found, or KEY_RESERVED
500 * @return: always returns zero.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300501 *
502 * This routine is used to handle evdev EVIOCGKEY ioctl.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300503 */
David Härdemand8b4b582010-10-29 16:08:23 -0300504static int ir_getkeycode(struct input_dev *idev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700505 struct input_keymap_entry *ke)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300506{
David Härdemand8b4b582010-10-29 16:08:23 -0300507 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300508 struct rc_map *rc_map = &rdev->rc_map;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300509 struct rc_map_table *entry;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700510 unsigned long flags;
511 unsigned int index;
512 unsigned int scancode;
513 int retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300514
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300515 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700516
517 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
518 index = ke->index;
519 } else {
520 retval = input_scancode_to_scalar(ke, &scancode);
521 if (retval)
522 goto out;
523
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300524 index = ir_lookup_by_scancode(rc_map, scancode);
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300525 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700526
Dmitry Torokhov54e74b82011-01-28 23:33:29 -0800527 if (index < rc_map->len) {
528 entry = &rc_map->scan[index];
529
530 ke->index = index;
531 ke->keycode = entry->keycode;
532 ke->len = sizeof(entry->scancode);
533 memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
534
535 } else if (!(ke->flags & INPUT_KEYMAP_BY_INDEX)) {
536 /*
537 * We do not really know the valid range of scancodes
538 * so let's respond with KEY_RESERVED to anything we
539 * do not have mapping for [yet].
540 */
541 ke->index = index;
542 ke->keycode = KEY_RESERVED;
543 } else {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700544 retval = -EINVAL;
545 goto out;
546 }
547
Dmitry Torokhov47c5ba52010-10-31 15:18:42 -0700548 retval = 0;
549
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700550out:
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300551 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700552 return retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300553}
554
555/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300556 * rc_g_keycode_from_table() - gets the keycode that corresponds to a scancode
David Härdemand8b4b582010-10-29 16:08:23 -0300557 * @dev: the struct rc_dev descriptor of the device
558 * @scancode: the scancode to look for
559 * @return: the corresponding keycode, or KEY_RESERVED
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300560 *
David Härdemand8b4b582010-10-29 16:08:23 -0300561 * This routine is used by drivers which need to convert a scancode to a
562 * keycode. Normally it should not be used since drivers should have no
563 * interest in keycodes.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300564 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300565u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300566{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300567 struct rc_map *rc_map = &dev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700568 unsigned int keycode;
569 unsigned int index;
570 unsigned long flags;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300571
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300572 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700573
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300574 index = ir_lookup_by_scancode(rc_map, scancode);
575 keycode = index < rc_map->len ?
576 rc_map->scan[index].keycode : KEY_RESERVED;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700577
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300578 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700579
Mauro Carvalho Chehab35438942010-04-03 16:53:16 -0300580 if (keycode != KEY_RESERVED)
581 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
Sean Young518f4b22017-07-01 12:13:19 -0400582 dev->device_name, scancode, keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700583
David Härdemanb3074c02010-04-02 15:58:28 -0300584 return keycode;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300585}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300586EXPORT_SYMBOL_GPL(rc_g_keycode_from_table);
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300587
588/**
David Härdeman62c65032010-10-29 16:08:07 -0300589 * ir_do_keyup() - internal function to signal the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300590 * @dev: the struct rc_dev descriptor of the device
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300591 * @sync: whether or not to call input_sync
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300592 *
David Härdeman62c65032010-10-29 16:08:07 -0300593 * This function is used internally to release a keypress, it must be
594 * called with keylock held.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300595 */
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300596static void ir_do_keyup(struct rc_dev *dev, bool sync)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300597{
David Härdemand8b4b582010-10-29 16:08:23 -0300598 if (!dev->keypressed)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300599 return;
600
David Härdemand8b4b582010-10-29 16:08:23 -0300601 IR_dprintk(1, "keyup key 0x%04x\n", dev->last_keycode);
602 input_report_key(dev->input_dev, dev->last_keycode, 0);
Sean Young153a60b2013-07-30 19:00:01 -0300603 led_trigger_event(led_feedback, LED_OFF);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300604 if (sync)
605 input_sync(dev->input_dev);
David Härdemand8b4b582010-10-29 16:08:23 -0300606 dev->keypressed = false;
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300607}
David Härdeman62c65032010-10-29 16:08:07 -0300608
609/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300610 * rc_keyup() - signals the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300611 * @dev: the struct rc_dev descriptor of the device
David Härdeman62c65032010-10-29 16:08:07 -0300612 *
613 * This routine is used to signal that a key has been released on the
614 * remote control.
615 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300616void rc_keyup(struct rc_dev *dev)
David Härdeman62c65032010-10-29 16:08:07 -0300617{
618 unsigned long flags;
David Härdeman62c65032010-10-29 16:08:07 -0300619
David Härdemand8b4b582010-10-29 16:08:23 -0300620 spin_lock_irqsave(&dev->keylock, flags);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300621 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300622 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300623}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300624EXPORT_SYMBOL_GPL(rc_keyup);
David Härdemana374fef2010-04-02 15:58:29 -0300625
626/**
627 * ir_timer_keyup() - generates a keyup event after a timeout
David Härdemand8b4b582010-10-29 16:08:23 -0300628 * @cookie: a pointer to the struct rc_dev for the device
David Härdemana374fef2010-04-02 15:58:29 -0300629 *
630 * This routine will generate a keyup event some time after a keydown event
631 * is generated when no further activity has been detected.
632 */
Kees Cookb17ec782017-10-24 11:23:14 -0400633static void ir_timer_keyup(struct timer_list *t)
David Härdemana374fef2010-04-02 15:58:29 -0300634{
Kees Cookb17ec782017-10-24 11:23:14 -0400635 struct rc_dev *dev = from_timer(dev, t, timer_keyup);
David Härdemana374fef2010-04-02 15:58:29 -0300636 unsigned long flags;
637
638 /*
639 * ir->keyup_jiffies is used to prevent a race condition if a
640 * hardware interrupt occurs at this point and the keyup timer
641 * event is moved further into the future as a result.
642 *
643 * The timer will then be reactivated and this function called
644 * again in the future. We need to exit gracefully in that case
645 * to allow the input subsystem to do its auto-repeat magic or
646 * a keyup event might follow immediately after the keydown.
647 */
David Härdemand8b4b582010-10-29 16:08:23 -0300648 spin_lock_irqsave(&dev->keylock, flags);
649 if (time_is_before_eq_jiffies(dev->keyup_jiffies))
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300650 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300651 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300652}
653
654/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300655 * rc_repeat() - signals that a key is still pressed
David Härdemand8b4b582010-10-29 16:08:23 -0300656 * @dev: the struct rc_dev descriptor of the device
David Härdemana374fef2010-04-02 15:58:29 -0300657 *
658 * This routine is used by IR decoders when a repeat message which does
659 * not include the necessary bits to reproduce the scancode has been
660 * received.
661 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300662void rc_repeat(struct rc_dev *dev)
David Härdemana374fef2010-04-02 15:58:29 -0300663{
664 unsigned long flags;
Sean Youngd57ea872017-08-09 13:19:16 -0400665 unsigned int timeout = protocols[dev->last_protocol].repeat_period;
David Härdemana374fef2010-04-02 15:58:29 -0300666
David Härdemand8b4b582010-10-29 16:08:23 -0300667 spin_lock_irqsave(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300668
David Härdemand8b4b582010-10-29 16:08:23 -0300669 if (!dev->keypressed)
David Härdemana374fef2010-04-02 15:58:29 -0300670 goto out;
671
David Härdeman265a2982017-06-22 15:23:54 -0400672 input_event(dev->input_dev, EV_MSC, MSC_SCAN, dev->last_scancode);
673 input_sync(dev->input_dev);
674
Sean Youngd57ea872017-08-09 13:19:16 -0400675 dev->keyup_jiffies = jiffies + msecs_to_jiffies(timeout);
David Härdemand8b4b582010-10-29 16:08:23 -0300676 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
David Härdemana374fef2010-04-02 15:58:29 -0300677
678out:
David Härdemand8b4b582010-10-29 16:08:23 -0300679 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300680}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300681EXPORT_SYMBOL_GPL(rc_repeat);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300682
683/**
David Härdeman62c65032010-10-29 16:08:07 -0300684 * ir_do_keydown() - internal function to process a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300685 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300686 * @protocol: the protocol of the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300687 * @scancode: the scancode of the keypress
688 * @keycode: the keycode of the keypress
689 * @toggle: the toggle value of the keypress
690 *
691 * This function is used internally to register a keypress, it must be
692 * called with keylock held.
693 */
Sean Young6d741bf2017-08-07 16:20:58 -0400694static void ir_do_keydown(struct rc_dev *dev, enum rc_proto protocol,
David Härdeman120703f2014-04-03 20:31:30 -0300695 u32 scancode, u32 keycode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300696{
David Härdeman99b0f3c2014-04-04 19:06:06 -0300697 bool new_event = (!dev->keypressed ||
David Härdeman120703f2014-04-03 20:31:30 -0300698 dev->last_protocol != protocol ||
David Härdeman99b0f3c2014-04-04 19:06:06 -0300699 dev->last_scancode != scancode ||
David Härdeman120703f2014-04-03 20:31:30 -0300700 dev->last_toggle != toggle);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300701
702 if (new_event && dev->keypressed)
703 ir_do_keyup(dev, false);
704
David Härdemand8b4b582010-10-29 16:08:23 -0300705 input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300706
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300707 if (new_event && keycode != KEY_RESERVED) {
708 /* Register a keypress */
709 dev->keypressed = true;
David Härdeman120703f2014-04-03 20:31:30 -0300710 dev->last_protocol = protocol;
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300711 dev->last_scancode = scancode;
712 dev->last_toggle = toggle;
713 dev->last_keycode = keycode;
David Härdeman62c65032010-10-29 16:08:07 -0300714
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200715 IR_dprintk(1, "%s: key down event, key 0x%04x, protocol 0x%04x, scancode 0x%08x\n",
Sean Young518f4b22017-07-01 12:13:19 -0400716 dev->device_name, keycode, protocol, scancode);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300717 input_report_key(dev->input_dev, keycode, 1);
James Hogan70a2f912014-01-16 19:56:22 -0300718
719 led_trigger_event(led_feedback, LED_FULL);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300720 }
David Härdeman62c65032010-10-29 16:08:07 -0300721
David Härdemand8b4b582010-10-29 16:08:23 -0300722 input_sync(dev->input_dev);
David Härdeman62c65032010-10-29 16:08:07 -0300723}
724
725/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300726 * rc_keydown() - generates input event for a key press
David Härdemand8b4b582010-10-29 16:08:23 -0300727 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300728 * @protocol: the protocol for the keypress
729 * @scancode: the scancode for the keypress
David Härdemana374fef2010-04-02 15:58:29 -0300730 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
731 * support toggle values, this should be set to zero)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300732 *
David Härdemand8b4b582010-10-29 16:08:23 -0300733 * This routine is used to signal that a key has been pressed on the
734 * remote control.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300735 */
Sean Young6d741bf2017-08-07 16:20:58 -0400736void rc_keydown(struct rc_dev *dev, enum rc_proto protocol, u32 scancode,
737 u8 toggle)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300738{
David Härdemana374fef2010-04-02 15:58:29 -0300739 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300740 u32 keycode = rc_g_keycode_from_table(dev, scancode);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300741
David Härdemand8b4b582010-10-29 16:08:23 -0300742 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300743 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300744
David Härdemand8b4b582010-10-29 16:08:23 -0300745 if (dev->keypressed) {
Sean Youngd57ea872017-08-09 13:19:16 -0400746 dev->keyup_jiffies = jiffies +
747 msecs_to_jiffies(protocols[protocol].repeat_period);
David Härdemand8b4b582010-10-29 16:08:23 -0300748 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
David Härdeman62c65032010-10-29 16:08:07 -0300749 }
David Härdemand8b4b582010-10-29 16:08:23 -0300750 spin_unlock_irqrestore(&dev->keylock, flags);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300751}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300752EXPORT_SYMBOL_GPL(rc_keydown);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300753
David Härdeman62c65032010-10-29 16:08:07 -0300754/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300755 * rc_keydown_notimeout() - generates input event for a key press without
David Härdeman62c65032010-10-29 16:08:07 -0300756 * an automatic keyup event at a later time
David Härdemand8b4b582010-10-29 16:08:23 -0300757 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300758 * @protocol: the protocol for the keypress
759 * @scancode: the scancode for the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300760 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
761 * support toggle values, this should be set to zero)
762 *
David Härdemand8b4b582010-10-29 16:08:23 -0300763 * This routine is used to signal that a key has been pressed on the
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300764 * remote control. The driver must manually call rc_keyup() at a later stage.
David Härdeman62c65032010-10-29 16:08:07 -0300765 */
Sean Young6d741bf2017-08-07 16:20:58 -0400766void rc_keydown_notimeout(struct rc_dev *dev, enum rc_proto protocol,
David Härdeman120703f2014-04-03 20:31:30 -0300767 u32 scancode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300768{
769 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300770 u32 keycode = rc_g_keycode_from_table(dev, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300771
David Härdemand8b4b582010-10-29 16:08:23 -0300772 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300773 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
David Härdemand8b4b582010-10-29 16:08:23 -0300774 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300775}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300776EXPORT_SYMBOL_GPL(rc_keydown_notimeout);
David Härdeman62c65032010-10-29 16:08:07 -0300777
Sean Youngb590c0b2016-12-05 19:24:59 -0200778/**
779 * rc_validate_filter() - checks that the scancode and mask are valid and
780 * provides sensible defaults
James Hoganf423ccc2015-03-31 14:48:10 -0300781 * @dev: the struct rc_dev descriptor of the device
Sean Youngb590c0b2016-12-05 19:24:59 -0200782 * @filter: the scancode and mask
783 * @return: 0 or -EINVAL if the filter is not valid
784 */
James Hoganf423ccc2015-03-31 14:48:10 -0300785static int rc_validate_filter(struct rc_dev *dev,
Sean Youngb590c0b2016-12-05 19:24:59 -0200786 struct rc_scancode_filter *filter)
787{
Sean Youngd57ea872017-08-09 13:19:16 -0400788 u32 mask, s = filter->data;
Sean Young6d741bf2017-08-07 16:20:58 -0400789 enum rc_proto protocol = dev->wakeup_protocol;
Sean Youngb590c0b2016-12-05 19:24:59 -0200790
Sean Youngd57ea872017-08-09 13:19:16 -0400791 if (protocol >= ARRAY_SIZE(protocols))
Sean Young2168b412017-08-07 09:21:29 -0400792 return -EINVAL;
793
Sean Youngd57ea872017-08-09 13:19:16 -0400794 mask = protocols[protocol].scancode_bits;
795
Sean Youngb590c0b2016-12-05 19:24:59 -0200796 switch (protocol) {
Sean Young6d741bf2017-08-07 16:20:58 -0400797 case RC_PROTO_NECX:
Sean Youngb590c0b2016-12-05 19:24:59 -0200798 if ((((s >> 16) ^ ~(s >> 8)) & 0xff) == 0)
799 return -EINVAL;
800 break;
Sean Young6d741bf2017-08-07 16:20:58 -0400801 case RC_PROTO_NEC32:
Sean Youngb590c0b2016-12-05 19:24:59 -0200802 if ((((s >> 24) ^ ~(s >> 16)) & 0xff) == 0)
803 return -EINVAL;
804 break;
Sean Young6d741bf2017-08-07 16:20:58 -0400805 case RC_PROTO_RC6_MCE:
Sean Youngb590c0b2016-12-05 19:24:59 -0200806 if ((s & 0xffff0000) != 0x800f0000)
807 return -EINVAL;
808 break;
Sean Young6d741bf2017-08-07 16:20:58 -0400809 case RC_PROTO_RC6_6A_32:
Sean Youngb590c0b2016-12-05 19:24:59 -0200810 if ((s & 0xffff0000) == 0x800f0000)
811 return -EINVAL;
812 break;
813 default:
814 break;
815 }
816
Sean Youngd57ea872017-08-09 13:19:16 -0400817 filter->data &= mask;
818 filter->mask &= mask;
Sean Youngb590c0b2016-12-05 19:24:59 -0200819
James Hoganf423ccc2015-03-31 14:48:10 -0300820 /*
821 * If we have to raw encode the IR for wakeup, we cannot have a mask
822 */
Sean Youngd57ea872017-08-09 13:19:16 -0400823 if (dev->encode_wakeup && filter->mask != 0 && filter->mask != mask)
James Hoganf423ccc2015-03-31 14:48:10 -0300824 return -EINVAL;
825
Sean Youngb590c0b2016-12-05 19:24:59 -0200826 return 0;
827}
828
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300829int rc_open(struct rc_dev *rdev)
830{
831 int rval = 0;
832
833 if (!rdev)
834 return -EINVAL;
835
836 mutex_lock(&rdev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -0200837
Juergen Lockf02dcdd2013-08-16 15:00:24 -0300838 if (!rdev->users++ && rdev->open != NULL)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300839 rval = rdev->open(rdev);
840
841 if (rval)
842 rdev->users--;
843
844 mutex_unlock(&rdev->lock);
845
846 return rval;
847}
848EXPORT_SYMBOL_GPL(rc_open);
849
David Härdemand8b4b582010-10-29 16:08:23 -0300850static int ir_open(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300851{
David Härdemand8b4b582010-10-29 16:08:23 -0300852 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300853
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300854 return rc_open(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300855}
856
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300857void rc_close(struct rc_dev *rdev)
858{
859 if (rdev) {
860 mutex_lock(&rdev->lock);
861
Mauro Carvalho Chehab81b7d142015-04-28 09:43:17 -0300862 if (!--rdev->users && rdev->close != NULL)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300863 rdev->close(rdev);
864
865 mutex_unlock(&rdev->lock);
866 }
867}
868EXPORT_SYMBOL_GPL(rc_close);
869
David Härdemand8b4b582010-10-29 16:08:23 -0300870static void ir_close(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300871{
David Härdemand8b4b582010-10-29 16:08:23 -0300872 struct rc_dev *rdev = input_get_drvdata(idev);
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300873 rc_close(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300874}
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300875
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300876/* class for /sys/class/rc */
David Härdeman40fc5322013-03-06 16:52:10 -0300877static char *rc_devnode(struct device *dev, umode_t *mode)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300878{
879 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
880}
881
David Härdeman40fc5322013-03-06 16:52:10 -0300882static struct class rc_class = {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300883 .name = "rc",
David Härdeman40fc5322013-03-06 16:52:10 -0300884 .devnode = rc_devnode,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300885};
886
David Härdemanc003ab12012-10-11 19:11:54 -0300887/*
888 * These are the protocol textual descriptions that are
889 * used by the sysfs protocols file. Note that the order
890 * of the entries is relevant.
891 */
Heiner Kallweit53df8772015-11-16 17:52:17 -0200892static const struct {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300893 u64 type;
Heiner Kallweit53df8772015-11-16 17:52:17 -0200894 const char *name;
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200895 const char *module_name;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300896} proto_names[] = {
Sean Young6d741bf2017-08-07 16:20:58 -0400897 { RC_PROTO_BIT_NONE, "none", NULL },
898 { RC_PROTO_BIT_OTHER, "other", NULL },
899 { RC_PROTO_BIT_UNKNOWN, "unknown", NULL },
900 { RC_PROTO_BIT_RC5 |
901 RC_PROTO_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
902 { RC_PROTO_BIT_NEC |
903 RC_PROTO_BIT_NECX |
904 RC_PROTO_BIT_NEC32, "nec", "ir-nec-decoder" },
905 { RC_PROTO_BIT_RC6_0 |
906 RC_PROTO_BIT_RC6_6A_20 |
907 RC_PROTO_BIT_RC6_6A_24 |
908 RC_PROTO_BIT_RC6_6A_32 |
909 RC_PROTO_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
910 { RC_PROTO_BIT_JVC, "jvc", "ir-jvc-decoder" },
911 { RC_PROTO_BIT_SONY12 |
912 RC_PROTO_BIT_SONY15 |
913 RC_PROTO_BIT_SONY20, "sony", "ir-sony-decoder" },
914 { RC_PROTO_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
915 { RC_PROTO_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
916 { RC_PROTO_BIT_SHARP, "sharp", "ir-sharp-decoder" },
917 { RC_PROTO_BIT_MCIR2_KBD |
918 RC_PROTO_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" },
919 { RC_PROTO_BIT_XMP, "xmp", "ir-xmp-decoder" },
920 { RC_PROTO_BIT_CEC, "cec", NULL },
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300921};
922
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300923/**
James Hoganab88c662014-02-28 20:17:05 -0300924 * struct rc_filter_attribute - Device attribute relating to a filter type.
925 * @attr: Device attribute.
926 * @type: Filter type.
927 * @mask: false for filter value, true for filter mask.
928 */
929struct rc_filter_attribute {
930 struct device_attribute attr;
931 enum rc_filter_type type;
932 bool mask;
933};
934#define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
935
James Hoganab88c662014-02-28 20:17:05 -0300936#define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
937 struct rc_filter_attribute dev_attr_##_name = { \
938 .attr = __ATTR(_name, _mode, _show, _store), \
939 .type = (_type), \
940 .mask = (_mask), \
941 }
942
David Härdemandd6ff6a2015-07-22 17:55:24 -0300943static bool lirc_is_present(void)
944{
945#if defined(CONFIG_LIRC_MODULE)
946 struct module *lirc;
947
948 mutex_lock(&module_mutex);
949 lirc = find_module("lirc_dev");
950 mutex_unlock(&module_mutex);
951
952 return lirc ? true : false;
953#elif defined(CONFIG_LIRC)
954 return true;
955#else
956 return false;
957#endif
958}
959
James Hoganab88c662014-02-28 20:17:05 -0300960/**
Sean Young0751d332016-12-05 17:08:35 -0200961 * show_protocols() - shows the current IR protocol(s)
David Härdemand8b4b582010-10-29 16:08:23 -0300962 * @device: the device descriptor
David Härdemanda6e1622014-04-03 20:32:16 -0300963 * @mattr: the device attribute struct
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300964 * @buf: a pointer to the output buffer
965 *
966 * This routine is a callback routine for input read the IR protocol type(s).
Sean Young0751d332016-12-05 17:08:35 -0200967 * it is trigged by reading /sys/class/rc/rc?/protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300968 * It returns the protocol names of supported protocols.
969 * Enabled protocols are printed in brackets.
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300970 *
David Härdeman18726a32017-04-27 17:34:08 -0300971 * dev->lock is taken to guard against races between
972 * store_protocols and show_protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300973 */
David Härdemand8b4b582010-10-29 16:08:23 -0300974static ssize_t show_protocols(struct device *device,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300975 struct device_attribute *mattr, char *buf)
976{
David Härdemand8b4b582010-10-29 16:08:23 -0300977 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300978 u64 allowed, enabled;
979 char *tmp = buf;
980 int i;
981
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300982 mutex_lock(&dev->lock);
983
Sean Young0751d332016-12-05 17:08:35 -0200984 enabled = dev->enabled_protocols;
985 allowed = dev->allowed_protocols;
986 if (dev->raw && !allowed)
987 allowed = ir_raw_get_allowed_protocols();
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300988
David Härdemanda6e1622014-04-03 20:32:16 -0300989 mutex_unlock(&dev->lock);
990
991 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
992 __func__, (long long)allowed, (long long)enabled);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300993
994 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
995 if (allowed & enabled & proto_names[i].type)
996 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
997 else if (allowed & proto_names[i].type)
998 tmp += sprintf(tmp, "%s ", proto_names[i].name);
David Härdemanc003ab12012-10-11 19:11:54 -0300999
1000 if (allowed & proto_names[i].type)
1001 allowed &= ~proto_names[i].type;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001002 }
1003
David Härdemandd6ff6a2015-07-22 17:55:24 -03001004 if (dev->driver_type == RC_DRIVER_IR_RAW && lirc_is_present())
David Härdeman275ddb42015-05-19 19:03:22 -03001005 tmp += sprintf(tmp, "[lirc] ");
1006
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001007 if (tmp != buf)
1008 tmp--;
1009 *tmp = '\n';
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001010
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001011 return tmp + 1 - buf;
1012}
1013
1014/**
David Härdemanda6e1622014-04-03 20:32:16 -03001015 * parse_protocol_change() - parses a protocol change request
1016 * @protocols: pointer to the bitmask of current protocols
1017 * @buf: pointer to the buffer with a list of changes
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001018 *
David Härdemanda6e1622014-04-03 20:32:16 -03001019 * Writing "+proto" will add a protocol to the protocol mask.
1020 * Writing "-proto" will remove a protocol from protocol mask.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001021 * Writing "proto" will enable only "proto".
1022 * Writing "none" will disable all protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001023 * Returns the number of changes performed or a negative error code.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001024 */
David Härdemanda6e1622014-04-03 20:32:16 -03001025static int parse_protocol_change(u64 *protocols, const char *buf)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001026{
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001027 const char *tmp;
David Härdemanda6e1622014-04-03 20:32:16 -03001028 unsigned count = 0;
1029 bool enable, disable;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001030 u64 mask;
David Härdemanda6e1622014-04-03 20:32:16 -03001031 int i;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001032
David Härdemanda6e1622014-04-03 20:32:16 -03001033 while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001034 if (!*tmp)
1035 break;
1036
1037 if (*tmp == '+') {
1038 enable = true;
1039 disable = false;
1040 tmp++;
1041 } else if (*tmp == '-') {
1042 enable = false;
1043 disable = true;
1044 tmp++;
1045 } else {
1046 enable = false;
1047 disable = false;
1048 }
1049
David Härdemanc003ab12012-10-11 19:11:54 -03001050 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1051 if (!strcasecmp(tmp, proto_names[i].name)) {
1052 mask = proto_names[i].type;
1053 break;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001054 }
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001055 }
1056
David Härdemanc003ab12012-10-11 19:11:54 -03001057 if (i == ARRAY_SIZE(proto_names)) {
David Härdeman275ddb42015-05-19 19:03:22 -03001058 if (!strcasecmp(tmp, "lirc"))
1059 mask = 0;
1060 else {
1061 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
1062 return -EINVAL;
1063 }
David Härdemanc003ab12012-10-11 19:11:54 -03001064 }
1065
1066 count++;
1067
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001068 if (enable)
David Härdemanda6e1622014-04-03 20:32:16 -03001069 *protocols |= mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001070 else if (disable)
David Härdemanda6e1622014-04-03 20:32:16 -03001071 *protocols &= ~mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001072 else
David Härdemanda6e1622014-04-03 20:32:16 -03001073 *protocols = mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001074 }
1075
1076 if (!count) {
1077 IR_dprintk(1, "Protocol not specified\n");
David Härdemanda6e1622014-04-03 20:32:16 -03001078 return -EINVAL;
1079 }
1080
1081 return count;
1082}
1083
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001084static void ir_raw_load_modules(u64 *protocols)
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001085{
1086 u64 available;
1087 int i, ret;
1088
1089 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
Sean Young6d741bf2017-08-07 16:20:58 -04001090 if (proto_names[i].type == RC_PROTO_BIT_NONE ||
1091 proto_names[i].type & (RC_PROTO_BIT_OTHER |
1092 RC_PROTO_BIT_UNKNOWN))
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001093 continue;
1094
1095 available = ir_raw_get_allowed_protocols();
1096 if (!(*protocols & proto_names[i].type & ~available))
1097 continue;
1098
1099 if (!proto_names[i].module_name) {
1100 pr_err("Can't enable IR protocol %s\n",
1101 proto_names[i].name);
1102 *protocols &= ~proto_names[i].type;
1103 continue;
1104 }
1105
1106 ret = request_module("%s", proto_names[i].module_name);
1107 if (ret < 0) {
1108 pr_err("Couldn't load IR protocol module %s\n",
1109 proto_names[i].module_name);
1110 *protocols &= ~proto_names[i].type;
1111 continue;
1112 }
1113 msleep(20);
1114 available = ir_raw_get_allowed_protocols();
1115 if (!(*protocols & proto_names[i].type & ~available))
1116 continue;
1117
Sean Young8caebcd2017-01-19 19:33:49 -02001118 pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001119 proto_names[i].module_name,
1120 proto_names[i].name);
1121 *protocols &= ~proto_names[i].type;
1122 }
1123}
1124
David Härdemanda6e1622014-04-03 20:32:16 -03001125/**
1126 * store_protocols() - changes the current/wakeup IR protocol(s)
1127 * @device: the device descriptor
1128 * @mattr: the device attribute struct
1129 * @buf: a pointer to the input buffer
1130 * @len: length of the input buffer
1131 *
1132 * This routine is for changing the IR protocol type.
1133 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
1134 * See parse_protocol_change() for the valid commands.
1135 * Returns @len on success or a negative error code.
1136 *
David Härdeman18726a32017-04-27 17:34:08 -03001137 * dev->lock is taken to guard against races between
1138 * store_protocols and show_protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001139 */
1140static ssize_t store_protocols(struct device *device,
1141 struct device_attribute *mattr,
1142 const char *buf, size_t len)
1143{
1144 struct rc_dev *dev = to_rc_dev(device);
David Härdemanda6e1622014-04-03 20:32:16 -03001145 u64 *current_protocols;
David Härdemanda6e1622014-04-03 20:32:16 -03001146 struct rc_scancode_filter *filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001147 u64 old_protocols, new_protocols;
1148 ssize_t rc;
1149
Sean Young0751d332016-12-05 17:08:35 -02001150 IR_dprintk(1, "Normal protocol change requested\n");
1151 current_protocols = &dev->enabled_protocols;
1152 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001153
Sean Young0751d332016-12-05 17:08:35 -02001154 if (!dev->change_protocol) {
David Härdemanda6e1622014-04-03 20:32:16 -03001155 IR_dprintk(1, "Protocol switching not supported\n");
1156 return -EINVAL;
1157 }
1158
1159 mutex_lock(&dev->lock);
1160
1161 old_protocols = *current_protocols;
1162 new_protocols = old_protocols;
1163 rc = parse_protocol_change(&new_protocols, buf);
1164 if (rc < 0)
1165 goto out;
1166
Sean Young0751d332016-12-05 17:08:35 -02001167 rc = dev->change_protocol(dev, &new_protocols);
David Härdemanda6e1622014-04-03 20:32:16 -03001168 if (rc < 0) {
1169 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
1170 (long long)new_protocols);
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001171 goto out;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001172 }
1173
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001174 if (dev->driver_type == RC_DRIVER_IR_RAW)
1175 ir_raw_load_modules(&new_protocols);
1176
James Hogan983c5bd2014-12-08 13:17:07 -03001177 if (new_protocols != old_protocols) {
1178 *current_protocols = new_protocols;
1179 IR_dprintk(1, "Protocols changed to 0x%llx\n",
1180 (long long)new_protocols);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001181 }
1182
James Hogan6bea25a2014-02-28 20:17:06 -03001183 /*
James Hogan983c5bd2014-12-08 13:17:07 -03001184 * If a protocol change was attempted the filter may need updating, even
1185 * if the actual protocol mask hasn't changed (since the driver may have
1186 * cleared the filter).
James Hogan6bea25a2014-02-28 20:17:06 -03001187 * Try setting the same filter with the new protocol (if any).
1188 * Fall back to clearing the filter.
1189 */
Sean Young0751d332016-12-05 17:08:35 -02001190 if (dev->s_filter && filter->mask) {
David Härdemanda6e1622014-04-03 20:32:16 -03001191 if (new_protocols)
Sean Young0751d332016-12-05 17:08:35 -02001192 rc = dev->s_filter(dev, filter);
David Härdemanda6e1622014-04-03 20:32:16 -03001193 else
1194 rc = -1;
David Härdeman23c843b2014-04-04 19:06:01 -03001195
David Härdemanda6e1622014-04-03 20:32:16 -03001196 if (rc < 0) {
1197 filter->data = 0;
1198 filter->mask = 0;
Sean Young0751d332016-12-05 17:08:35 -02001199 dev->s_filter(dev, filter);
James Hogan6bea25a2014-02-28 20:17:06 -03001200 }
James Hogan6bea25a2014-02-28 20:17:06 -03001201 }
1202
David Härdemanda6e1622014-04-03 20:32:16 -03001203 rc = len;
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001204
1205out:
1206 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001207 return rc;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001208}
1209
James Hogan00942d12014-01-17 10:58:49 -03001210/**
James Hogan00942d12014-01-17 10:58:49 -03001211 * show_filter() - shows the current scancode filter value or mask
1212 * @device: the device descriptor
1213 * @attr: the device attribute struct
1214 * @buf: a pointer to the output buffer
1215 *
1216 * This routine is a callback routine to read a scancode filter value or mask.
1217 * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
1218 * It prints the current scancode filter value or mask of the appropriate filter
1219 * type in hexadecimal into @buf and returns the size of the buffer.
1220 *
1221 * Bits of the filter value corresponding to set bits in the filter mask are
1222 * compared against input scancodes and non-matching scancodes are discarded.
1223 *
David Härdeman18726a32017-04-27 17:34:08 -03001224 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001225 * store_filter and show_filter.
1226 */
1227static ssize_t show_filter(struct device *device,
1228 struct device_attribute *attr,
1229 char *buf)
1230{
1231 struct rc_dev *dev = to_rc_dev(device);
1232 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001233 struct rc_scancode_filter *filter;
James Hogan00942d12014-01-17 10:58:49 -03001234 u32 val;
1235
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001236 mutex_lock(&dev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001237
David Härdemanda6e1622014-04-03 20:32:16 -03001238 if (fattr->type == RC_FILTER_NORMAL)
David Härdemanc5540fb2014-04-03 20:32:21 -03001239 filter = &dev->scancode_filter;
James Hogan00942d12014-01-17 10:58:49 -03001240 else
David Härdemanc5540fb2014-04-03 20:32:21 -03001241 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001242
David Härdemanda6e1622014-04-03 20:32:16 -03001243 if (fattr->mask)
1244 val = filter->mask;
1245 else
1246 val = filter->data;
James Hogan00942d12014-01-17 10:58:49 -03001247 mutex_unlock(&dev->lock);
1248
1249 return sprintf(buf, "%#x\n", val);
1250}
1251
1252/**
1253 * store_filter() - changes the scancode filter value
1254 * @device: the device descriptor
1255 * @attr: the device attribute struct
1256 * @buf: a pointer to the input buffer
1257 * @len: length of the input buffer
1258 *
1259 * This routine is for changing a scancode filter value or mask.
1260 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
1261 * Returns -EINVAL if an invalid filter value for the current protocol was
1262 * specified or if scancode filtering is not supported by the driver, otherwise
1263 * returns @len.
1264 *
1265 * Bits of the filter value corresponding to set bits in the filter mask are
1266 * compared against input scancodes and non-matching scancodes are discarded.
1267 *
David Härdeman18726a32017-04-27 17:34:08 -03001268 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001269 * store_filter and show_filter.
1270 */
1271static ssize_t store_filter(struct device *device,
1272 struct device_attribute *attr,
David Härdemanda6e1622014-04-03 20:32:16 -03001273 const char *buf, size_t len)
James Hogan00942d12014-01-17 10:58:49 -03001274{
1275 struct rc_dev *dev = to_rc_dev(device);
1276 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001277 struct rc_scancode_filter new_filter, *filter;
James Hogan00942d12014-01-17 10:58:49 -03001278 int ret;
1279 unsigned long val;
David Härdeman23c843b2014-04-04 19:06:01 -03001280 int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
James Hogan00942d12014-01-17 10:58:49 -03001281
James Hogan00942d12014-01-17 10:58:49 -03001282 ret = kstrtoul(buf, 0, &val);
1283 if (ret < 0)
1284 return ret;
1285
David Härdemanda6e1622014-04-03 20:32:16 -03001286 if (fattr->type == RC_FILTER_NORMAL) {
1287 set_filter = dev->s_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001288 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001289 } else {
1290 set_filter = dev->s_wakeup_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001291 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001292 }
1293
David Härdeman99b0f3c2014-04-04 19:06:06 -03001294 if (!set_filter)
1295 return -EINVAL;
James Hogan00942d12014-01-17 10:58:49 -03001296
1297 mutex_lock(&dev->lock);
1298
David Härdemanda6e1622014-04-03 20:32:16 -03001299 new_filter = *filter;
James Hogan00942d12014-01-17 10:58:49 -03001300 if (fattr->mask)
David Härdemanda6e1622014-04-03 20:32:16 -03001301 new_filter.mask = val;
James Hogan00942d12014-01-17 10:58:49 -03001302 else
David Härdemanda6e1622014-04-03 20:32:16 -03001303 new_filter.data = val;
David Härdeman23c843b2014-04-04 19:06:01 -03001304
Sean Young0751d332016-12-05 17:08:35 -02001305 if (fattr->type == RC_FILTER_WAKEUP) {
Sean Youngb590c0b2016-12-05 19:24:59 -02001306 /*
1307 * Refuse to set a filter unless a protocol is enabled
1308 * and the filter is valid for that protocol
1309 */
Sean Young6d741bf2017-08-07 16:20:58 -04001310 if (dev->wakeup_protocol != RC_PROTO_UNKNOWN)
James Hoganf423ccc2015-03-31 14:48:10 -03001311 ret = rc_validate_filter(dev, &new_filter);
Sean Youngb590c0b2016-12-05 19:24:59 -02001312 else
Sean Young0751d332016-12-05 17:08:35 -02001313 ret = -EINVAL;
Sean Youngb590c0b2016-12-05 19:24:59 -02001314
1315 if (ret != 0)
Sean Young0751d332016-12-05 17:08:35 -02001316 goto unlock;
Sean Young0751d332016-12-05 17:08:35 -02001317 }
1318
1319 if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
1320 val) {
James Hogan6bea25a2014-02-28 20:17:06 -03001321 /* refuse to set a filter unless a protocol is enabled */
1322 ret = -EINVAL;
1323 goto unlock;
1324 }
David Härdeman23c843b2014-04-04 19:06:01 -03001325
David Härdemanda6e1622014-04-03 20:32:16 -03001326 ret = set_filter(dev, &new_filter);
David Härdeman99b0f3c2014-04-04 19:06:06 -03001327 if (ret < 0)
1328 goto unlock;
James Hogan00942d12014-01-17 10:58:49 -03001329
David Härdemanda6e1622014-04-03 20:32:16 -03001330 *filter = new_filter;
James Hogan00942d12014-01-17 10:58:49 -03001331
1332unlock:
1333 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001334 return (ret < 0) ? ret : len;
James Hogan00942d12014-01-17 10:58:49 -03001335}
1336
Sean Young0751d332016-12-05 17:08:35 -02001337/**
1338 * show_wakeup_protocols() - shows the wakeup IR protocol
1339 * @device: the device descriptor
1340 * @mattr: the device attribute struct
1341 * @buf: a pointer to the output buffer
1342 *
1343 * This routine is a callback routine for input read the IR protocol type(s).
1344 * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols.
1345 * It returns the protocol names of supported protocols.
1346 * The enabled protocols are printed in brackets.
1347 *
David Härdeman18726a32017-04-27 17:34:08 -03001348 * dev->lock is taken to guard against races between
1349 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001350 */
1351static ssize_t show_wakeup_protocols(struct device *device,
1352 struct device_attribute *mattr,
1353 char *buf)
1354{
1355 struct rc_dev *dev = to_rc_dev(device);
1356 u64 allowed;
Sean Young6d741bf2017-08-07 16:20:58 -04001357 enum rc_proto enabled;
Sean Young0751d332016-12-05 17:08:35 -02001358 char *tmp = buf;
1359 int i;
1360
Sean Young0751d332016-12-05 17:08:35 -02001361 mutex_lock(&dev->lock);
1362
1363 allowed = dev->allowed_wakeup_protocols;
1364 enabled = dev->wakeup_protocol;
1365
1366 mutex_unlock(&dev->lock);
1367
1368 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - %d\n",
1369 __func__, (long long)allowed, enabled);
1370
Sean Youngd57ea872017-08-09 13:19:16 -04001371 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001372 if (allowed & (1ULL << i)) {
1373 if (i == enabled)
Sean Youngd57ea872017-08-09 13:19:16 -04001374 tmp += sprintf(tmp, "[%s] ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001375 else
Sean Youngd57ea872017-08-09 13:19:16 -04001376 tmp += sprintf(tmp, "%s ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001377 }
1378 }
1379
1380 if (tmp != buf)
1381 tmp--;
1382 *tmp = '\n';
1383
1384 return tmp + 1 - buf;
1385}
1386
1387/**
1388 * store_wakeup_protocols() - changes the wakeup IR protocol(s)
1389 * @device: the device descriptor
1390 * @mattr: the device attribute struct
1391 * @buf: a pointer to the input buffer
1392 * @len: length of the input buffer
1393 *
1394 * This routine is for changing the IR protocol type.
1395 * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols.
1396 * Returns @len on success or a negative error code.
1397 *
David Härdeman18726a32017-04-27 17:34:08 -03001398 * dev->lock is taken to guard against races between
1399 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001400 */
1401static ssize_t store_wakeup_protocols(struct device *device,
1402 struct device_attribute *mattr,
1403 const char *buf, size_t len)
1404{
1405 struct rc_dev *dev = to_rc_dev(device);
Sean Young6d741bf2017-08-07 16:20:58 -04001406 enum rc_proto protocol;
Sean Young0751d332016-12-05 17:08:35 -02001407 ssize_t rc;
1408 u64 allowed;
1409 int i;
1410
Sean Young0751d332016-12-05 17:08:35 -02001411 mutex_lock(&dev->lock);
1412
1413 allowed = dev->allowed_wakeup_protocols;
1414
1415 if (sysfs_streq(buf, "none")) {
Sean Young6d741bf2017-08-07 16:20:58 -04001416 protocol = RC_PROTO_UNKNOWN;
Sean Young0751d332016-12-05 17:08:35 -02001417 } else {
Sean Youngd57ea872017-08-09 13:19:16 -04001418 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001419 if ((allowed & (1ULL << i)) &&
Sean Youngd57ea872017-08-09 13:19:16 -04001420 sysfs_streq(buf, protocols[i].name)) {
Sean Young0751d332016-12-05 17:08:35 -02001421 protocol = i;
1422 break;
1423 }
1424 }
1425
Sean Youngd57ea872017-08-09 13:19:16 -04001426 if (i == ARRAY_SIZE(protocols)) {
Sean Young0751d332016-12-05 17:08:35 -02001427 rc = -EINVAL;
1428 goto out;
1429 }
James Hoganf423ccc2015-03-31 14:48:10 -03001430
1431 if (dev->encode_wakeup) {
1432 u64 mask = 1ULL << protocol;
1433
1434 ir_raw_load_modules(&mask);
1435 if (!mask) {
1436 rc = -EINVAL;
1437 goto out;
1438 }
1439 }
Sean Young0751d332016-12-05 17:08:35 -02001440 }
1441
1442 if (dev->wakeup_protocol != protocol) {
1443 dev->wakeup_protocol = protocol;
1444 IR_dprintk(1, "Wakeup protocol changed to %d\n", protocol);
1445
Sean Young6d741bf2017-08-07 16:20:58 -04001446 if (protocol == RC_PROTO_RC6_MCE)
Sean Young0751d332016-12-05 17:08:35 -02001447 dev->scancode_wakeup_filter.data = 0x800f0000;
1448 else
1449 dev->scancode_wakeup_filter.data = 0;
1450 dev->scancode_wakeup_filter.mask = 0;
1451
1452 rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
1453 if (rc == 0)
1454 rc = len;
1455 } else {
1456 rc = len;
1457 }
1458
1459out:
1460 mutex_unlock(&dev->lock);
1461 return rc;
1462}
1463
David Härdemand8b4b582010-10-29 16:08:23 -03001464static void rc_dev_release(struct device *device)
1465{
Max Kellermann47cae1e2016-03-21 08:33:05 -03001466 struct rc_dev *dev = to_rc_dev(device);
1467
1468 kfree(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001469}
1470
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001471#define ADD_HOTPLUG_VAR(fmt, val...) \
1472 do { \
1473 int err = add_uevent_var(env, fmt, val); \
1474 if (err) \
1475 return err; \
1476 } while (0)
1477
1478static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1479{
David Härdemand8b4b582010-10-29 16:08:23 -03001480 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001481
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001482 if (dev->rc_map.name)
1483 ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
David Härdemand8b4b582010-10-29 16:08:23 -03001484 if (dev->driver_name)
1485 ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
Sean Youngb9f407e2017-09-01 11:34:23 -03001486 if (dev->device_name)
1487 ADD_HOTPLUG_VAR("DEV_NAME=%s", dev->device_name);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001488
1489 return 0;
1490}
1491
1492/*
1493 * Static device attribute struct with the sysfs attributes for IR's
1494 */
Sean Young6d75db32017-09-01 11:30:50 -03001495static struct device_attribute dev_attr_ro_protocols =
1496__ATTR(protocols, 0444, show_protocols, NULL);
1497static struct device_attribute dev_attr_rw_protocols =
1498__ATTR(protocols, 0644, show_protocols, store_protocols);
Sean Young0751d332016-12-05 17:08:35 -02001499static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
1500 store_wakeup_protocols);
James Hogan00942d12014-01-17 10:58:49 -03001501static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
1502 show_filter, store_filter, RC_FILTER_NORMAL, false);
1503static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
1504 show_filter, store_filter, RC_FILTER_NORMAL, true);
1505static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
1506 show_filter, store_filter, RC_FILTER_WAKEUP, false);
1507static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
1508 show_filter, store_filter, RC_FILTER_WAKEUP, true);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001509
Sean Young6d75db32017-09-01 11:30:50 -03001510static struct attribute *rc_dev_rw_protocol_attrs[] = {
1511 &dev_attr_rw_protocols.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001512 NULL,
1513};
1514
Sean Young6d75db32017-09-01 11:30:50 -03001515static const struct attribute_group rc_dev_rw_protocol_attr_grp = {
1516 .attrs = rc_dev_rw_protocol_attrs,
1517};
1518
1519static struct attribute *rc_dev_ro_protocol_attrs[] = {
1520 &dev_attr_ro_protocols.attr,
1521 NULL,
1522};
1523
1524static const struct attribute_group rc_dev_ro_protocol_attr_grp = {
1525 .attrs = rc_dev_ro_protocol_attrs,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001526};
1527
David Härdeman99b0f3c2014-04-04 19:06:06 -03001528static struct attribute *rc_dev_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001529 &dev_attr_filter.attr.attr,
1530 &dev_attr_filter_mask.attr.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001531 NULL,
1532};
1533
Arvind Yadavdb681022017-07-07 04:23:54 -04001534static const struct attribute_group rc_dev_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001535 .attrs = rc_dev_filter_attrs,
1536};
1537
1538static struct attribute *rc_dev_wakeup_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001539 &dev_attr_wakeup_filter.attr.attr,
1540 &dev_attr_wakeup_filter_mask.attr.attr,
Sean Young0751d332016-12-05 17:08:35 -02001541 &dev_attr_wakeup_protocols.attr,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001542 NULL,
1543};
1544
Arvind Yadavdb681022017-07-07 04:23:54 -04001545static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001546 .attrs = rc_dev_wakeup_filter_attrs,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001547};
1548
Bhumika Goyalf03f02f2017-08-19 05:22:15 -03001549static const struct device_type rc_dev_type = {
David Härdemand8b4b582010-10-29 16:08:23 -03001550 .release = rc_dev_release,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001551 .uevent = rc_dev_uevent,
1552};
1553
Andi Shyti0f7499f2016-12-16 06:50:58 -02001554struct rc_dev *rc_allocate_device(enum rc_driver_type type)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001555{
David Härdemand8b4b582010-10-29 16:08:23 -03001556 struct rc_dev *dev;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001557
David Härdemand8b4b582010-10-29 16:08:23 -03001558 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1559 if (!dev)
1560 return NULL;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001561
Andi Shytid34aee12016-12-16 04:12:15 -02001562 if (type != RC_DRIVER_IR_RAW_TX) {
1563 dev->input_dev = input_allocate_device();
1564 if (!dev->input_dev) {
1565 kfree(dev);
1566 return NULL;
1567 }
1568
1569 dev->input_dev->getkeycode = ir_getkeycode;
1570 dev->input_dev->setkeycode = ir_setkeycode;
1571 input_set_drvdata(dev->input_dev, dev);
1572
Kees Cookb17ec782017-10-24 11:23:14 -04001573 timer_setup(&dev->timer_keyup, ir_timer_keyup, 0);
Andi Shytid34aee12016-12-16 04:12:15 -02001574
1575 spin_lock_init(&dev->rc_map.lock);
1576 spin_lock_init(&dev->keylock);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001577 }
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001578 mutex_init(&dev->lock);
David Härdemand8b4b582010-10-29 16:08:23 -03001579
1580 dev->dev.type = &rc_dev_type;
David Härdeman40fc5322013-03-06 16:52:10 -03001581 dev->dev.class = &rc_class;
David Härdemand8b4b582010-10-29 16:08:23 -03001582 device_initialize(&dev->dev);
1583
Andi Shyti0f7499f2016-12-16 06:50:58 -02001584 dev->driver_type = type;
1585
David Härdemand8b4b582010-10-29 16:08:23 -03001586 __module_get(THIS_MODULE);
1587 return dev;
1588}
1589EXPORT_SYMBOL_GPL(rc_allocate_device);
1590
1591void rc_free_device(struct rc_dev *dev)
1592{
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001593 if (!dev)
1594 return;
1595
Markus Elfring3dd94f02014-11-20 09:01:32 -03001596 input_free_device(dev->input_dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001597
1598 put_device(&dev->dev);
1599
Max Kellermann47cae1e2016-03-21 08:33:05 -03001600 /* kfree(dev) will be called by the callback function
1601 rc_dev_release() */
1602
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001603 module_put(THIS_MODULE);
David Härdemand8b4b582010-10-29 16:08:23 -03001604}
1605EXPORT_SYMBOL_GPL(rc_free_device);
1606
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001607static void devm_rc_alloc_release(struct device *dev, void *res)
1608{
1609 rc_free_device(*(struct rc_dev **)res);
1610}
1611
Andi Shyti0f7499f2016-12-16 06:50:58 -02001612struct rc_dev *devm_rc_allocate_device(struct device *dev,
1613 enum rc_driver_type type)
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001614{
1615 struct rc_dev **dr, *rc;
1616
1617 dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
1618 if (!dr)
1619 return NULL;
1620
Andi Shyti0f7499f2016-12-16 06:50:58 -02001621 rc = rc_allocate_device(type);
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001622 if (!rc) {
1623 devres_free(dr);
1624 return NULL;
1625 }
1626
1627 rc->dev.parent = dev;
1628 rc->managed_alloc = true;
1629 *dr = rc;
1630 devres_add(dev, dr);
1631
1632 return rc;
1633}
1634EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
1635
David Härdemanf56928a2017-05-03 07:04:00 -03001636static int rc_prepare_rx_device(struct rc_dev *dev)
David Härdemand8b4b582010-10-29 16:08:23 -03001637{
David Härdemanfcb13092015-05-19 19:03:17 -03001638 int rc;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001639 struct rc_map *rc_map;
Sean Young6d741bf2017-08-07 16:20:58 -04001640 u64 rc_proto;
David Härdemand8b4b582010-10-29 16:08:23 -03001641
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001642 if (!dev->map_name)
David Härdemand8b4b582010-10-29 16:08:23 -03001643 return -EINVAL;
1644
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001645 rc_map = rc_map_get(dev->map_name);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001646 if (!rc_map)
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001647 rc_map = rc_map_get(RC_MAP_EMPTY);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001648 if (!rc_map || !rc_map->scan || rc_map->size == 0)
David Härdemand8b4b582010-10-29 16:08:23 -03001649 return -EINVAL;
1650
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001651 rc = ir_setkeytable(dev, rc_map);
1652 if (rc)
1653 return rc;
1654
Sean Young6d741bf2017-08-07 16:20:58 -04001655 rc_proto = BIT_ULL(rc_map->rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001656
Sean Young831c4c82017-09-01 09:55:59 -03001657 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1658 dev->enabled_protocols = dev->allowed_protocols;
1659
Sean Young41380862017-02-22 18:48:01 -03001660 if (dev->change_protocol) {
Sean Young6d741bf2017-08-07 16:20:58 -04001661 rc = dev->change_protocol(dev, &rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001662 if (rc < 0)
1663 goto out_table;
Sean Young6d741bf2017-08-07 16:20:58 -04001664 dev->enabled_protocols = rc_proto;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001665 }
1666
Sean Young41380862017-02-22 18:48:01 -03001667 if (dev->driver_type == RC_DRIVER_IR_RAW)
Sean Young6d741bf2017-08-07 16:20:58 -04001668 ir_raw_load_modules(&rc_proto);
Sean Young41380862017-02-22 18:48:01 -03001669
David Härdemand8b4b582010-10-29 16:08:23 -03001670 set_bit(EV_KEY, dev->input_dev->evbit);
1671 set_bit(EV_REP, dev->input_dev->evbit);
1672 set_bit(EV_MSC, dev->input_dev->evbit);
1673 set_bit(MSC_SCAN, dev->input_dev->mscbit);
1674 if (dev->open)
1675 dev->input_dev->open = ir_open;
1676 if (dev->close)
1677 dev->input_dev->close = ir_close;
1678
David Härdemanb2aceb72017-04-27 17:33:58 -03001679 dev->input_dev->dev.parent = &dev->dev;
1680 memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
1681 dev->input_dev->phys = dev->input_phys;
Sean Young518f4b22017-07-01 12:13:19 -04001682 dev->input_dev->name = dev->device_name;
David Härdemanb2aceb72017-04-27 17:33:58 -03001683
David Härdemanf56928a2017-05-03 07:04:00 -03001684 return 0;
1685
1686out_table:
1687 ir_free_table(&dev->rc_map);
1688
1689 return rc;
1690}
1691
1692static int rc_setup_rx_device(struct rc_dev *dev)
1693{
1694 int rc;
1695
David Härdemanb2aceb72017-04-27 17:33:58 -03001696 /* rc_open will be called here */
1697 rc = input_register_device(dev->input_dev);
1698 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001699 return rc;
David Härdemanb2aceb72017-04-27 17:33:58 -03001700
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001701 /*
1702 * Default delay of 250ms is too short for some protocols, especially
1703 * since the timeout is currently set to 250ms. Increase it to 500ms,
1704 * to avoid wrong repetition of the keycodes. Note that this must be
1705 * set after the call to input_register_device().
1706 */
1707 dev->input_dev->rep[REP_DELAY] = 500;
1708
1709 /*
1710 * As a repeat event on protocols like RC-5 and NEC take as long as
1711 * 110/114ms, using 33ms as a repeat period is not the right thing
1712 * to do.
1713 */
1714 dev->input_dev->rep[REP_PERIOD] = 125;
1715
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001716 return 0;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001717}
1718
1719static void rc_free_rx_device(struct rc_dev *dev)
1720{
David Härdemanf56928a2017-05-03 07:04:00 -03001721 if (!dev)
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001722 return;
1723
David Härdemanf56928a2017-05-03 07:04:00 -03001724 if (dev->input_dev) {
1725 input_unregister_device(dev->input_dev);
1726 dev->input_dev = NULL;
1727 }
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001728
David Härdemanf56928a2017-05-03 07:04:00 -03001729 ir_free_table(&dev->rc_map);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001730}
1731
1732int rc_register_device(struct rc_dev *dev)
1733{
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001734 const char *path;
1735 int attr = 0;
1736 int minor;
1737 int rc;
1738
1739 if (!dev)
1740 return -EINVAL;
1741
David Härdemanfcb13092015-05-19 19:03:17 -03001742 minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
1743 if (minor < 0)
1744 return minor;
1745
1746 dev->minor = minor;
1747 dev_set_name(&dev->dev, "rc%u", dev->minor);
1748 dev_set_drvdata(&dev->dev, dev);
Mauro Carvalho Chehab587d1b02014-01-14 16:27:55 -03001749
David Härdeman99b0f3c2014-04-04 19:06:06 -03001750 dev->dev.groups = dev->sysfs_groups;
Sean Young6d75db32017-09-01 11:30:50 -03001751 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1752 dev->sysfs_groups[attr++] = &rc_dev_ro_protocol_attr_grp;
1753 else if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
1754 dev->sysfs_groups[attr++] = &rc_dev_rw_protocol_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001755 if (dev->s_filter)
David Härdeman120703f2014-04-03 20:31:30 -03001756 dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001757 if (dev->s_wakeup_filter)
1758 dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001759 dev->sysfs_groups[attr++] = NULL;
1760
David Härdemanf56928a2017-05-03 07:04:00 -03001761 if (dev->driver_type == RC_DRIVER_IR_RAW ||
1762 dev->driver_type == RC_DRIVER_IR_RAW_TX) {
1763 rc = ir_raw_event_prepare(dev);
1764 if (rc < 0)
1765 goto out_minor;
1766 }
1767
1768 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1769 rc = rc_prepare_rx_device(dev);
1770 if (rc)
1771 goto out_raw;
1772 }
1773
David Härdemand8b4b582010-10-29 16:08:23 -03001774 rc = device_add(&dev->dev);
1775 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001776 goto out_rx_free;
David Härdemand8b4b582010-10-29 16:08:23 -03001777
David Härdemand8b4b582010-10-29 16:08:23 -03001778 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Heiner Kallweit4dc0e902015-10-29 19:39:06 -02001779 dev_info(&dev->dev, "%s as %s\n",
Sean Young518f4b22017-07-01 12:13:19 -04001780 dev->device_name ?: "Unspecified device", path ?: "N/A");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001781 kfree(path);
1782
Sean Young5df62772017-02-23 06:11:21 -03001783 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1784 rc = rc_setup_rx_device(dev);
1785 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001786 goto out_dev;
1787 }
1788
1789 if (dev->driver_type == RC_DRIVER_IR_RAW ||
1790 dev->driver_type == RC_DRIVER_IR_RAW_TX) {
1791 rc = ir_raw_event_register(dev);
1792 if (rc < 0)
1793 goto out_rx;
David Härdemand8b4b582010-10-29 16:08:23 -03001794 }
1795
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001796 IR_dprintk(1, "Registered rc%u (driver: %s)\n",
David Härdemanfcb13092015-05-19 19:03:17 -03001797 dev->minor,
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001798 dev->driver_name ? dev->driver_name : "unknown");
David Härdemand8b4b582010-10-29 16:08:23 -03001799
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001800 return 0;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001801
David Härdemanf56928a2017-05-03 07:04:00 -03001802out_rx:
1803 rc_free_rx_device(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001804out_dev:
1805 device_del(&dev->dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001806out_rx_free:
1807 ir_free_table(&dev->rc_map);
1808out_raw:
1809 ir_raw_event_free(dev);
1810out_minor:
David Härdemanfcb13092015-05-19 19:03:17 -03001811 ida_simple_remove(&rc_ida, minor);
David Härdemand8b4b582010-10-29 16:08:23 -03001812 return rc;
1813}
1814EXPORT_SYMBOL_GPL(rc_register_device);
1815
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001816static void devm_rc_release(struct device *dev, void *res)
1817{
1818 rc_unregister_device(*(struct rc_dev **)res);
1819}
1820
1821int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
1822{
1823 struct rc_dev **dr;
1824 int ret;
1825
1826 dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
1827 if (!dr)
1828 return -ENOMEM;
1829
1830 ret = rc_register_device(dev);
1831 if (ret) {
1832 devres_free(dr);
1833 return ret;
1834 }
1835
1836 *dr = dev;
1837 devres_add(parent, dr);
1838
1839 return 0;
1840}
1841EXPORT_SYMBOL_GPL(devm_rc_register_device);
1842
David Härdemand8b4b582010-10-29 16:08:23 -03001843void rc_unregister_device(struct rc_dev *dev)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001844{
David Härdemand8b4b582010-10-29 16:08:23 -03001845 if (!dev)
1846 return;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001847
David Härdemand8b4b582010-10-29 16:08:23 -03001848 del_timer_sync(&dev->timer_keyup);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001849
David Härdemand8b4b582010-10-29 16:08:23 -03001850 if (dev->driver_type == RC_DRIVER_IR_RAW)
1851 ir_raw_event_unregister(dev);
1852
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001853 rc_free_rx_device(dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001854
1855 device_del(&dev->dev);
1856
David Härdemanfcb13092015-05-19 19:03:17 -03001857 ida_simple_remove(&rc_ida, dev->minor);
1858
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001859 if (!dev->managed_alloc)
1860 rc_free_device(dev);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001861}
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001862
David Härdemand8b4b582010-10-29 16:08:23 -03001863EXPORT_SYMBOL_GPL(rc_unregister_device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001864
1865/*
1866 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1867 */
1868
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001869static int __init rc_core_init(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001870{
David Härdeman40fc5322013-03-06 16:52:10 -03001871 int rc = class_register(&rc_class);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001872 if (rc) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -02001873 pr_err("rc_core: unable to register rc class\n");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001874 return rc;
1875 }
1876
Sean Young153a60b2013-07-30 19:00:01 -03001877 led_trigger_register_simple("rc-feedback", &led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001878 rc_map_register(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001879
1880 return 0;
1881}
1882
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001883static void __exit rc_core_exit(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001884{
David Härdeman40fc5322013-03-06 16:52:10 -03001885 class_unregister(&rc_class);
Sean Young153a60b2013-07-30 19:00:01 -03001886 led_trigger_unregister_simple(led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001887 rc_map_unregister(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001888}
1889
David Härdemane76d4ce2013-03-06 16:52:15 -03001890subsys_initcall(rc_core_init);
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001891module_exit(rc_core_exit);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001892
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001893int rc_core_debug; /* ir_debug level (0,1,2) */
1894EXPORT_SYMBOL_GPL(rc_core_debug);
1895module_param_named(debug, rc_core_debug, int, 0644);
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03001896
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02001897MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03001898MODULE_LICENSE("GPL");