blob: 8c828fee4f5a71ce9680155eb7d3c466e1d9606f [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>
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030018#include <linux/spinlock.h>
19#include <linux/delay.h>
Mauro Carvalho Chehab882ead32009-12-29 10:37:38 -030020#include <linux/input.h>
Sean Young153a60b2013-07-30 19:00:01 -030021#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
David Härdemanfcb13092015-05-19 19:03:17 -030023#include <linux/idr.h>
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -030024#include <linux/device.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040025#include <linux/module.h>
Mauro Carvalho Chehabf62de672010-11-09 23:09:57 -030026#include "rc-core-priv.h"
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -030027
David Härdemanb3074c02010-04-02 15:58:28 -030028/* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */
29#define IR_TAB_MIN_SIZE 256
30#define IR_TAB_MAX_SIZE 8192
David Härdemanfcb13092015-05-19 19:03:17 -030031#define RC_DEV_MAX 256
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -030032
Sean Youngd57ea872017-08-09 13:19:16 -040033static const struct {
34 const char *name;
35 unsigned int repeat_period;
36 unsigned int scancode_bits;
37} protocols[] = {
Sean Young6d741bf2017-08-07 16:20:58 -040038 [RC_PROTO_UNKNOWN] = { .name = "unknown", .repeat_period = 250 },
39 [RC_PROTO_OTHER] = { .name = "other", .repeat_period = 250 },
40 [RC_PROTO_RC5] = { .name = "rc-5",
Sean Youngd57ea872017-08-09 13:19:16 -040041 .scancode_bits = 0x1f7f, .repeat_period = 164 },
Sean Young6d741bf2017-08-07 16:20:58 -040042 [RC_PROTO_RC5X_20] = { .name = "rc-5x-20",
Sean Youngd57ea872017-08-09 13:19:16 -040043 .scancode_bits = 0x1f7f3f, .repeat_period = 164 },
Sean Young6d741bf2017-08-07 16:20:58 -040044 [RC_PROTO_RC5_SZ] = { .name = "rc-5-sz",
Sean Youngd57ea872017-08-09 13:19:16 -040045 .scancode_bits = 0x2fff, .repeat_period = 164 },
Sean Young6d741bf2017-08-07 16:20:58 -040046 [RC_PROTO_JVC] = { .name = "jvc",
Sean Youngd57ea872017-08-09 13:19:16 -040047 .scancode_bits = 0xffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040048 [RC_PROTO_SONY12] = { .name = "sony-12",
Sean Youngd57ea872017-08-09 13:19:16 -040049 .scancode_bits = 0x1f007f, .repeat_period = 100 },
Sean Young6d741bf2017-08-07 16:20:58 -040050 [RC_PROTO_SONY15] = { .name = "sony-15",
Sean Youngd57ea872017-08-09 13:19:16 -040051 .scancode_bits = 0xff007f, .repeat_period = 100 },
Sean Young6d741bf2017-08-07 16:20:58 -040052 [RC_PROTO_SONY20] = { .name = "sony-20",
Sean Youngd57ea872017-08-09 13:19:16 -040053 .scancode_bits = 0x1fff7f, .repeat_period = 100 },
Sean Young6d741bf2017-08-07 16:20:58 -040054 [RC_PROTO_NEC] = { .name = "nec",
Sean Youngd57ea872017-08-09 13:19:16 -040055 .scancode_bits = 0xffff, .repeat_period = 160 },
Sean Young6d741bf2017-08-07 16:20:58 -040056 [RC_PROTO_NECX] = { .name = "nec-x",
Sean Youngd57ea872017-08-09 13:19:16 -040057 .scancode_bits = 0xffffff, .repeat_period = 160 },
Sean Young6d741bf2017-08-07 16:20:58 -040058 [RC_PROTO_NEC32] = { .name = "nec-32",
Sean Youngd57ea872017-08-09 13:19:16 -040059 .scancode_bits = 0xffffffff, .repeat_period = 160 },
Sean Young6d741bf2017-08-07 16:20:58 -040060 [RC_PROTO_SANYO] = { .name = "sanyo",
Sean Youngd57ea872017-08-09 13:19:16 -040061 .scancode_bits = 0x1fffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040062 [RC_PROTO_MCIR2_KBD] = { .name = "mcir2-kbd",
Sean Youngd57ea872017-08-09 13:19:16 -040063 .scancode_bits = 0xffff, .repeat_period = 150 },
Sean Young6d741bf2017-08-07 16:20:58 -040064 [RC_PROTO_MCIR2_MSE] = { .name = "mcir2-mse",
Sean Youngd57ea872017-08-09 13:19:16 -040065 .scancode_bits = 0x1fffff, .repeat_period = 150 },
Sean Young6d741bf2017-08-07 16:20:58 -040066 [RC_PROTO_RC6_0] = { .name = "rc-6-0",
Sean Youngd57ea872017-08-09 13:19:16 -040067 .scancode_bits = 0xffff, .repeat_period = 164 },
Sean Young6d741bf2017-08-07 16:20:58 -040068 [RC_PROTO_RC6_6A_20] = { .name = "rc-6-6a-20",
Sean Youngd57ea872017-08-09 13:19:16 -040069 .scancode_bits = 0xfffff, .repeat_period = 164 },
Sean Young6d741bf2017-08-07 16:20:58 -040070 [RC_PROTO_RC6_6A_24] = { .name = "rc-6-6a-24",
Sean Youngd57ea872017-08-09 13:19:16 -040071 .scancode_bits = 0xffffff, .repeat_period = 164 },
Sean Young6d741bf2017-08-07 16:20:58 -040072 [RC_PROTO_RC6_6A_32] = { .name = "rc-6-6a-32",
Sean Youngd57ea872017-08-09 13:19:16 -040073 .scancode_bits = 0xffffffff, .repeat_period = 164 },
Sean Young6d741bf2017-08-07 16:20:58 -040074 [RC_PROTO_RC6_MCE] = { .name = "rc-6-mce",
Sean Youngd57ea872017-08-09 13:19:16 -040075 .scancode_bits = 0xffff7fff, .repeat_period = 164 },
Sean Young6d741bf2017-08-07 16:20:58 -040076 [RC_PROTO_SHARP] = { .name = "sharp",
Sean Youngd57ea872017-08-09 13:19:16 -040077 .scancode_bits = 0x1fff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040078 [RC_PROTO_XMP] = { .name = "xmp", .repeat_period = 250 },
79 [RC_PROTO_CEC] = { .name = "cec", .repeat_period = 550 },
Sean Youngd57ea872017-08-09 13:19:16 -040080};
David Härdemana374fef2010-04-02 15:58:29 -030081
David Härdeman4c7b3552010-11-10 11:04:19 -030082/* Used to keep track of known keymaps */
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030083static LIST_HEAD(rc_map_list);
84static DEFINE_SPINLOCK(rc_map_lock);
Sean Young153a60b2013-07-30 19:00:01 -030085static struct led_trigger *led_feedback;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030086
David Härdemanfcb13092015-05-19 19:03:17 -030087/* Used to keep track of rc devices */
88static DEFINE_IDA(rc_ida);
89
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030090static struct rc_map_list *seek_rc_map(const char *name)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030091{
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030092 struct rc_map_list *map = NULL;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030093
94 spin_lock(&rc_map_lock);
95 list_for_each_entry(map, &rc_map_list, list) {
96 if (!strcmp(name, map->map.name)) {
97 spin_unlock(&rc_map_lock);
98 return map;
99 }
100 }
101 spin_unlock(&rc_map_lock);
102
103 return NULL;
104}
105
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300106struct rc_map *rc_map_get(const char *name)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300107{
108
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300109 struct rc_map_list *map;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300110
111 map = seek_rc_map(name);
Russell King2ff56fa2015-10-15 13:15:24 -0300112#ifdef CONFIG_MODULES
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300113 if (!map) {
Kees Cook8ea54882014-03-11 17:25:53 -0300114 int rc = request_module("%s", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300115 if (rc < 0) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -0200116 pr_err("Couldn't load IR keymap %s\n", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300117 return NULL;
118 }
119 msleep(20); /* Give some time for IR to register */
120
121 map = seek_rc_map(name);
122 }
123#endif
124 if (!map) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -0200125 pr_err("IR keymap %s not found\n", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300126 return NULL;
127 }
128
129 printk(KERN_INFO "Registered IR keymap %s\n", map->map.name);
130
131 return &map->map;
132}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300133EXPORT_SYMBOL_GPL(rc_map_get);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300134
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300135int rc_map_register(struct rc_map_list *map)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300136{
137 spin_lock(&rc_map_lock);
138 list_add_tail(&map->list, &rc_map_list);
139 spin_unlock(&rc_map_lock);
140 return 0;
141}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300142EXPORT_SYMBOL_GPL(rc_map_register);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300143
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300144void rc_map_unregister(struct rc_map_list *map)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300145{
146 spin_lock(&rc_map_lock);
147 list_del(&map->list);
148 spin_unlock(&rc_map_lock);
149}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300150EXPORT_SYMBOL_GPL(rc_map_unregister);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300151
152
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300153static struct rc_map_table empty[] = {
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300154 { 0x2a, KEY_COFFEE },
155};
156
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300157static struct rc_map_list empty_map = {
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300158 .map = {
Sean Young6d741bf2017-08-07 16:20:58 -0400159 .scan = empty,
160 .size = ARRAY_SIZE(empty),
161 .rc_proto = RC_PROTO_UNKNOWN, /* Legacy IR type */
162 .name = RC_MAP_EMPTY,
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300163 }
164};
165
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300166/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700167 * ir_create_table() - initializes a scancode table
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300168 * @rc_map: the rc_map to initialize
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700169 * @name: name to assign to the table
Sean Young6d741bf2017-08-07 16:20:58 -0400170 * @rc_proto: ir type to assign to the new table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700171 * @size: initial size of the table
172 * @return: zero on success or a negative error code
173 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300174 * This routine will initialize the rc_map and will allocate
David Härdemand8b4b582010-10-29 16:08:23 -0300175 * memory to hold at least the specified number of elements.
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700176 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300177static int ir_create_table(struct rc_map *rc_map,
Sean Young6d741bf2017-08-07 16:20:58 -0400178 const char *name, u64 rc_proto, size_t size)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700179{
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300180 rc_map->name = kstrdup(name, GFP_KERNEL);
181 if (!rc_map->name)
182 return -ENOMEM;
Sean Young6d741bf2017-08-07 16:20:58 -0400183 rc_map->rc_proto = rc_proto;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300184 rc_map->alloc = roundup_pow_of_two(size * sizeof(struct rc_map_table));
185 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300186 rc_map->scan = kmalloc(rc_map->alloc, GFP_KERNEL);
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300187 if (!rc_map->scan) {
188 kfree(rc_map->name);
189 rc_map->name = NULL;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700190 return -ENOMEM;
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300191 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700192
193 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300194 rc_map->size, rc_map->alloc);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700195 return 0;
196}
197
198/**
199 * ir_free_table() - frees memory allocated by a scancode table
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300200 * @rc_map: the table whose mappings need to be freed
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700201 *
202 * This routine will free memory alloctaed for key mappings used by given
203 * scancode table.
204 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300205static void ir_free_table(struct rc_map *rc_map)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700206{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300207 rc_map->size = 0;
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300208 kfree(rc_map->name);
Max Kellermannc183d352016-08-09 18:32:06 -0300209 rc_map->name = NULL;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300210 kfree(rc_map->scan);
211 rc_map->scan = NULL;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700212}
213
214/**
David Härdemanb3074c02010-04-02 15:58:28 -0300215 * ir_resize_table() - resizes a scancode table if necessary
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300216 * @rc_map: the rc_map to resize
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700217 * @gfp_flags: gfp flags to use when allocating memory
David Härdemanb3074c02010-04-02 15:58:28 -0300218 * @return: zero on success or a negative error code
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300219 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300220 * This routine will shrink the rc_map if it has lots of
David Härdemanb3074c02010-04-02 15:58:28 -0300221 * unused entries and grow it if it is full.
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300222 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300223static int ir_resize_table(struct rc_map *rc_map, gfp_t gfp_flags)
David Härdemanb3074c02010-04-02 15:58:28 -0300224{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300225 unsigned int oldalloc = rc_map->alloc;
David Härdemanb3074c02010-04-02 15:58:28 -0300226 unsigned int newalloc = oldalloc;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300227 struct rc_map_table *oldscan = rc_map->scan;
228 struct rc_map_table *newscan;
David Härdemanb3074c02010-04-02 15:58:28 -0300229
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300230 if (rc_map->size == rc_map->len) {
David Härdemanb3074c02010-04-02 15:58:28 -0300231 /* All entries in use -> grow keytable */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300232 if (rc_map->alloc >= IR_TAB_MAX_SIZE)
David Härdemanb3074c02010-04-02 15:58:28 -0300233 return -ENOMEM;
234
235 newalloc *= 2;
236 IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
237 }
238
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300239 if ((rc_map->len * 3 < rc_map->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
David Härdemanb3074c02010-04-02 15:58:28 -0300240 /* Less than 1/3 of entries in use -> shrink keytable */
241 newalloc /= 2;
242 IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
243 }
244
245 if (newalloc == oldalloc)
246 return 0;
247
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700248 newscan = kmalloc(newalloc, gfp_flags);
David Härdemanb3074c02010-04-02 15:58:28 -0300249 if (!newscan) {
250 IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
251 return -ENOMEM;
252 }
253
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300254 memcpy(newscan, rc_map->scan, rc_map->len * sizeof(struct rc_map_table));
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300255 rc_map->scan = newscan;
256 rc_map->alloc = newalloc;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300257 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
David Härdemanb3074c02010-04-02 15:58:28 -0300258 kfree(oldscan);
259 return 0;
260}
261
262/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700263 * ir_update_mapping() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300264 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300265 * @rc_map: scancode table to be adjusted
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700266 * @index: index of the mapping that needs to be updated
267 * @keycode: the desired keycode
268 * @return: previous keycode assigned to the mapping
David Härdemanb3074c02010-04-02 15:58:28 -0300269 *
David Härdemand8b4b582010-10-29 16:08:23 -0300270 * This routine is used to update scancode->keycode mapping at given
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700271 * position.
David Härdemanb3074c02010-04-02 15:58:28 -0300272 */
David Härdemand8b4b582010-10-29 16:08:23 -0300273static unsigned int ir_update_mapping(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300274 struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700275 unsigned int index,
276 unsigned int new_keycode)
277{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300278 int old_keycode = rc_map->scan[index].keycode;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700279 int i;
280
281 /* Did the user wish to remove the mapping? */
282 if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
283 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300284 index, rc_map->scan[index].scancode);
285 rc_map->len--;
286 memmove(&rc_map->scan[index], &rc_map->scan[index+ 1],
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300287 (rc_map->len - index) * sizeof(struct rc_map_table));
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700288 } else {
289 IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n",
290 index,
291 old_keycode == KEY_RESERVED ? "New" : "Replacing",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300292 rc_map->scan[index].scancode, new_keycode);
293 rc_map->scan[index].keycode = new_keycode;
David Härdemand8b4b582010-10-29 16:08:23 -0300294 __set_bit(new_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700295 }
296
297 if (old_keycode != KEY_RESERVED) {
298 /* A previous mapping was updated... */
David Härdemand8b4b582010-10-29 16:08:23 -0300299 __clear_bit(old_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700300 /* ... but another scancode might use the same keycode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300301 for (i = 0; i < rc_map->len; i++) {
302 if (rc_map->scan[i].keycode == old_keycode) {
David Härdemand8b4b582010-10-29 16:08:23 -0300303 __set_bit(old_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700304 break;
305 }
306 }
307
308 /* Possibly shrink the keytable, failure is not a problem */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300309 ir_resize_table(rc_map, GFP_ATOMIC);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700310 }
311
312 return old_keycode;
313}
314
315/**
David Härdeman4c7b3552010-11-10 11:04:19 -0300316 * ir_establish_scancode() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300317 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300318 * @rc_map: scancode table to be searched
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700319 * @scancode: the desired scancode
320 * @resize: controls whether we allowed to resize the table to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300321 * accommodate not yet present scancodes
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700322 * @return: index of the mapping containing scancode in question
323 * or -1U in case of failure.
324 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300325 * This routine is used to locate given scancode in rc_map.
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700326 * If scancode is not yet present the routine will allocate a new slot
327 * for it.
328 */
David Härdemand8b4b582010-10-29 16:08:23 -0300329static unsigned int ir_establish_scancode(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300330 struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700331 unsigned int scancode,
332 bool resize)
David Härdemanb3074c02010-04-02 15:58:28 -0300333{
334 unsigned int i;
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300335
336 /*
337 * Unfortunately, some hardware-based IR decoders don't provide
338 * all bits for the complete IR code. In general, they provide only
339 * the command part of the IR code. Yet, as it is possible to replace
340 * the provided IR with another one, it is needed to allow loading
David Härdemand8b4b582010-10-29 16:08:23 -0300341 * IR tables from other remotes. So, we support specifying a mask to
342 * indicate the valid bits of the scancodes.
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300343 */
David Härdeman9d2f1d32014-04-03 20:32:26 -0300344 if (dev->scancode_mask)
345 scancode &= dev->scancode_mask;
David Härdemanb3074c02010-04-02 15:58:28 -0300346
347 /* First check if we already have a mapping for this ir command */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300348 for (i = 0; i < rc_map->len; i++) {
349 if (rc_map->scan[i].scancode == scancode)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700350 return i;
351
David Härdemanb3074c02010-04-02 15:58:28 -0300352 /* Keytable is sorted from lowest to highest scancode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300353 if (rc_map->scan[i].scancode >= scancode)
David Härdemanb3074c02010-04-02 15:58:28 -0300354 break;
David Härdemanb3074c02010-04-02 15:58:28 -0300355 }
356
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700357 /* No previous mapping found, we might need to grow the table */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300358 if (rc_map->size == rc_map->len) {
359 if (!resize || ir_resize_table(rc_map, GFP_ATOMIC))
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700360 return -1U;
361 }
David Härdemanb3074c02010-04-02 15:58:28 -0300362
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700363 /* i is the proper index to insert our new keycode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300364 if (i < rc_map->len)
365 memmove(&rc_map->scan[i + 1], &rc_map->scan[i],
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300366 (rc_map->len - i) * sizeof(struct rc_map_table));
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300367 rc_map->scan[i].scancode = scancode;
368 rc_map->scan[i].keycode = KEY_RESERVED;
369 rc_map->len++;
David Härdemanb3074c02010-04-02 15:58:28 -0300370
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700371 return i;
David Härdemanb3074c02010-04-02 15:58:28 -0300372}
373
374/**
375 * ir_setkeycode() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300376 * @idev: the struct input_dev device descriptor
David Härdemanb3074c02010-04-02 15:58:28 -0300377 * @scancode: the desired scancode
378 * @keycode: result
379 * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
380 *
381 * This routine is used to handle evdev EVIOCSKEY ioctl.
382 */
David Härdemand8b4b582010-10-29 16:08:23 -0300383static int ir_setkeycode(struct input_dev *idev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700384 const struct input_keymap_entry *ke,
385 unsigned int *old_keycode)
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300386{
David Härdemand8b4b582010-10-29 16:08:23 -0300387 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300388 struct rc_map *rc_map = &rdev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700389 unsigned int index;
390 unsigned int scancode;
Mauro Carvalho Chehabdea8a392010-11-29 07:46:13 -0300391 int retval = 0;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700392 unsigned long flags;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300393
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300394 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700395
396 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
397 index = ke->index;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300398 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700399 retval = -EINVAL;
400 goto out;
401 }
402 } else {
403 retval = input_scancode_to_scalar(ke, &scancode);
404 if (retval)
405 goto out;
406
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300407 index = ir_establish_scancode(rdev, rc_map, scancode, true);
408 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700409 retval = -ENOMEM;
410 goto out;
411 }
412 }
413
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300414 *old_keycode = ir_update_mapping(rdev, rc_map, index, ke->keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700415
416out:
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300417 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700418 return retval;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300419}
420
421/**
David Härdemanb3074c02010-04-02 15:58:28 -0300422 * ir_setkeytable() - sets several entries in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300423 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300424 * @to: the struct rc_map to copy entries to
425 * @from: the struct rc_map to copy entries from
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700426 * @return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300427 *
David Härdemanb3074c02010-04-02 15:58:28 -0300428 * This routine is used to handle table initialization.
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300429 */
David Härdemand8b4b582010-10-29 16:08:23 -0300430static int ir_setkeytable(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300431 const struct rc_map *from)
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300432{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300433 struct rc_map *rc_map = &dev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700434 unsigned int i, index;
435 int rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300436
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300437 rc = ir_create_table(rc_map, from->name,
Sean Young6d741bf2017-08-07 16:20:58 -0400438 from->rc_proto, from->size);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700439 if (rc)
440 return rc;
441
442 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300443 rc_map->size, rc_map->alloc);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700444
David Härdemanb3074c02010-04-02 15:58:28 -0300445 for (i = 0; i < from->size; i++) {
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300446 index = ir_establish_scancode(dev, rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700447 from->scan[i].scancode, false);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300448 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700449 rc = -ENOMEM;
David Härdemanb3074c02010-04-02 15:58:28 -0300450 break;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700451 }
452
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300453 ir_update_mapping(dev, rc_map, index,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700454 from->scan[i].keycode);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300455 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700456
457 if (rc)
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300458 ir_free_table(rc_map);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700459
David Härdemanb3074c02010-04-02 15:58:28 -0300460 return rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300461}
462
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300463/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700464 * ir_lookup_by_scancode() - locate mapping by scancode
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300465 * @rc_map: the struct rc_map to search
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700466 * @scancode: scancode to look for in the table
467 * @return: index in the table, -1U if not found
468 *
469 * This routine performs binary search in RC keykeymap table for
470 * given scancode.
471 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300472static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700473 unsigned int scancode)
474{
David Härdeman0d070252010-10-30 22:17:44 +0200475 int start = 0;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300476 int end = rc_map->len - 1;
David Härdeman0d070252010-10-30 22:17:44 +0200477 int mid;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700478
479 while (start <= end) {
480 mid = (start + end) / 2;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300481 if (rc_map->scan[mid].scancode < scancode)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700482 start = mid + 1;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300483 else if (rc_map->scan[mid].scancode > scancode)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700484 end = mid - 1;
485 else
486 return mid;
487 }
488
489 return -1U;
490}
491
492/**
David Härdemanb3074c02010-04-02 15:58:28 -0300493 * ir_getkeycode() - get a keycode from the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300494 * @idev: the struct input_dev device descriptor
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300495 * @scancode: the desired scancode
David Härdemanb3074c02010-04-02 15:58:28 -0300496 * @keycode: used to return the keycode, if found, or KEY_RESERVED
497 * @return: always returns zero.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300498 *
499 * This routine is used to handle evdev EVIOCGKEY ioctl.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300500 */
David Härdemand8b4b582010-10-29 16:08:23 -0300501static int ir_getkeycode(struct input_dev *idev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700502 struct input_keymap_entry *ke)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300503{
David Härdemand8b4b582010-10-29 16:08:23 -0300504 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300505 struct rc_map *rc_map = &rdev->rc_map;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300506 struct rc_map_table *entry;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700507 unsigned long flags;
508 unsigned int index;
509 unsigned int scancode;
510 int retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300511
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300512 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700513
514 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
515 index = ke->index;
516 } else {
517 retval = input_scancode_to_scalar(ke, &scancode);
518 if (retval)
519 goto out;
520
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300521 index = ir_lookup_by_scancode(rc_map, scancode);
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300522 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700523
Dmitry Torokhov54e74b82011-01-28 23:33:29 -0800524 if (index < rc_map->len) {
525 entry = &rc_map->scan[index];
526
527 ke->index = index;
528 ke->keycode = entry->keycode;
529 ke->len = sizeof(entry->scancode);
530 memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
531
532 } else if (!(ke->flags & INPUT_KEYMAP_BY_INDEX)) {
533 /*
534 * We do not really know the valid range of scancodes
535 * so let's respond with KEY_RESERVED to anything we
536 * do not have mapping for [yet].
537 */
538 ke->index = index;
539 ke->keycode = KEY_RESERVED;
540 } else {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700541 retval = -EINVAL;
542 goto out;
543 }
544
Dmitry Torokhov47c5ba52010-10-31 15:18:42 -0700545 retval = 0;
546
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700547out:
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300548 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700549 return retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300550}
551
552/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300553 * rc_g_keycode_from_table() - gets the keycode that corresponds to a scancode
David Härdemand8b4b582010-10-29 16:08:23 -0300554 * @dev: the struct rc_dev descriptor of the device
555 * @scancode: the scancode to look for
556 * @return: the corresponding keycode, or KEY_RESERVED
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300557 *
David Härdemand8b4b582010-10-29 16:08:23 -0300558 * This routine is used by drivers which need to convert a scancode to a
559 * keycode. Normally it should not be used since drivers should have no
560 * interest in keycodes.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300561 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300562u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300563{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300564 struct rc_map *rc_map = &dev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700565 unsigned int keycode;
566 unsigned int index;
567 unsigned long flags;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300568
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300569 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700570
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300571 index = ir_lookup_by_scancode(rc_map, scancode);
572 keycode = index < rc_map->len ?
573 rc_map->scan[index].keycode : KEY_RESERVED;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700574
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300575 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700576
Mauro Carvalho Chehab35438942010-04-03 16:53:16 -0300577 if (keycode != KEY_RESERVED)
578 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
Sean Young518f4b22017-07-01 12:13:19 -0400579 dev->device_name, scancode, keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700580
David Härdemanb3074c02010-04-02 15:58:28 -0300581 return keycode;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300582}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300583EXPORT_SYMBOL_GPL(rc_g_keycode_from_table);
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300584
585/**
David Härdeman62c65032010-10-29 16:08:07 -0300586 * ir_do_keyup() - internal function to signal the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300587 * @dev: the struct rc_dev descriptor of the device
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300588 * @sync: whether or not to call input_sync
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300589 *
David Härdeman62c65032010-10-29 16:08:07 -0300590 * This function is used internally to release a keypress, it must be
591 * called with keylock held.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300592 */
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300593static void ir_do_keyup(struct rc_dev *dev, bool sync)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300594{
David Härdemand8b4b582010-10-29 16:08:23 -0300595 if (!dev->keypressed)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300596 return;
597
David Härdemand8b4b582010-10-29 16:08:23 -0300598 IR_dprintk(1, "keyup key 0x%04x\n", dev->last_keycode);
599 input_report_key(dev->input_dev, dev->last_keycode, 0);
Sean Young153a60b2013-07-30 19:00:01 -0300600 led_trigger_event(led_feedback, LED_OFF);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300601 if (sync)
602 input_sync(dev->input_dev);
David Härdemand8b4b582010-10-29 16:08:23 -0300603 dev->keypressed = false;
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300604}
David Härdeman62c65032010-10-29 16:08:07 -0300605
606/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300607 * rc_keyup() - signals the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300608 * @dev: the struct rc_dev descriptor of the device
David Härdeman62c65032010-10-29 16:08:07 -0300609 *
610 * This routine is used to signal that a key has been released on the
611 * remote control.
612 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300613void rc_keyup(struct rc_dev *dev)
David Härdeman62c65032010-10-29 16:08:07 -0300614{
615 unsigned long flags;
David Härdeman62c65032010-10-29 16:08:07 -0300616
David Härdemand8b4b582010-10-29 16:08:23 -0300617 spin_lock_irqsave(&dev->keylock, flags);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300618 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300619 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300620}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300621EXPORT_SYMBOL_GPL(rc_keyup);
David Härdemana374fef2010-04-02 15:58:29 -0300622
623/**
624 * ir_timer_keyup() - generates a keyup event after a timeout
David Härdemand8b4b582010-10-29 16:08:23 -0300625 * @cookie: a pointer to the struct rc_dev for the device
David Härdemana374fef2010-04-02 15:58:29 -0300626 *
627 * This routine will generate a keyup event some time after a keydown event
628 * is generated when no further activity has been detected.
629 */
630static void ir_timer_keyup(unsigned long cookie)
631{
David Härdemand8b4b582010-10-29 16:08:23 -0300632 struct rc_dev *dev = (struct rc_dev *)cookie;
David Härdemana374fef2010-04-02 15:58:29 -0300633 unsigned long flags;
634
635 /*
636 * ir->keyup_jiffies is used to prevent a race condition if a
637 * hardware interrupt occurs at this point and the keyup timer
638 * event is moved further into the future as a result.
639 *
640 * The timer will then be reactivated and this function called
641 * again in the future. We need to exit gracefully in that case
642 * to allow the input subsystem to do its auto-repeat magic or
643 * a keyup event might follow immediately after the keydown.
644 */
David Härdemand8b4b582010-10-29 16:08:23 -0300645 spin_lock_irqsave(&dev->keylock, flags);
646 if (time_is_before_eq_jiffies(dev->keyup_jiffies))
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300647 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300648 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300649}
650
651/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300652 * rc_repeat() - signals that a key is still pressed
David Härdemand8b4b582010-10-29 16:08:23 -0300653 * @dev: the struct rc_dev descriptor of the device
David Härdemana374fef2010-04-02 15:58:29 -0300654 *
655 * This routine is used by IR decoders when a repeat message which does
656 * not include the necessary bits to reproduce the scancode has been
657 * received.
658 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300659void rc_repeat(struct rc_dev *dev)
David Härdemana374fef2010-04-02 15:58:29 -0300660{
661 unsigned long flags;
Sean Youngd57ea872017-08-09 13:19:16 -0400662 unsigned int timeout = protocols[dev->last_protocol].repeat_period;
David Härdemana374fef2010-04-02 15:58:29 -0300663
David Härdemand8b4b582010-10-29 16:08:23 -0300664 spin_lock_irqsave(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300665
David Härdemand8b4b582010-10-29 16:08:23 -0300666 if (!dev->keypressed)
David Härdemana374fef2010-04-02 15:58:29 -0300667 goto out;
668
David Härdeman265a2982017-06-22 15:23:54 -0400669 input_event(dev->input_dev, EV_MSC, MSC_SCAN, dev->last_scancode);
670 input_sync(dev->input_dev);
671
Sean Youngd57ea872017-08-09 13:19:16 -0400672 dev->keyup_jiffies = jiffies + msecs_to_jiffies(timeout);
David Härdemand8b4b582010-10-29 16:08:23 -0300673 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
David Härdemana374fef2010-04-02 15:58:29 -0300674
675out:
David Härdemand8b4b582010-10-29 16:08:23 -0300676 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300677}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300678EXPORT_SYMBOL_GPL(rc_repeat);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300679
680/**
David Härdeman62c65032010-10-29 16:08:07 -0300681 * ir_do_keydown() - internal function to process a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300682 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300683 * @protocol: the protocol of the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300684 * @scancode: the scancode of the keypress
685 * @keycode: the keycode of the keypress
686 * @toggle: the toggle value of the keypress
687 *
688 * This function is used internally to register a keypress, it must be
689 * called with keylock held.
690 */
Sean Young6d741bf2017-08-07 16:20:58 -0400691static void ir_do_keydown(struct rc_dev *dev, enum rc_proto protocol,
David Härdeman120703f2014-04-03 20:31:30 -0300692 u32 scancode, u32 keycode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300693{
David Härdeman99b0f3c2014-04-04 19:06:06 -0300694 bool new_event = (!dev->keypressed ||
David Härdeman120703f2014-04-03 20:31:30 -0300695 dev->last_protocol != protocol ||
David Härdeman99b0f3c2014-04-04 19:06:06 -0300696 dev->last_scancode != scancode ||
David Härdeman120703f2014-04-03 20:31:30 -0300697 dev->last_toggle != toggle);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300698
699 if (new_event && dev->keypressed)
700 ir_do_keyup(dev, false);
701
David Härdemand8b4b582010-10-29 16:08:23 -0300702 input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300703
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300704 if (new_event && keycode != KEY_RESERVED) {
705 /* Register a keypress */
706 dev->keypressed = true;
David Härdeman120703f2014-04-03 20:31:30 -0300707 dev->last_protocol = protocol;
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300708 dev->last_scancode = scancode;
709 dev->last_toggle = toggle;
710 dev->last_keycode = keycode;
David Härdeman62c65032010-10-29 16:08:07 -0300711
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200712 IR_dprintk(1, "%s: key down event, key 0x%04x, protocol 0x%04x, scancode 0x%08x\n",
Sean Young518f4b22017-07-01 12:13:19 -0400713 dev->device_name, keycode, protocol, scancode);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300714 input_report_key(dev->input_dev, keycode, 1);
James Hogan70a2f912014-01-16 19:56:22 -0300715
716 led_trigger_event(led_feedback, LED_FULL);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300717 }
David Härdeman62c65032010-10-29 16:08:07 -0300718
David Härdemand8b4b582010-10-29 16:08:23 -0300719 input_sync(dev->input_dev);
David Härdeman62c65032010-10-29 16:08:07 -0300720}
721
722/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300723 * rc_keydown() - generates input event for a key press
David Härdemand8b4b582010-10-29 16:08:23 -0300724 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300725 * @protocol: the protocol for the keypress
726 * @scancode: the scancode for the keypress
David Härdemana374fef2010-04-02 15:58:29 -0300727 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
728 * support toggle values, this should be set to zero)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300729 *
David Härdemand8b4b582010-10-29 16:08:23 -0300730 * This routine is used to signal that a key has been pressed on the
731 * remote control.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300732 */
Sean Young6d741bf2017-08-07 16:20:58 -0400733void rc_keydown(struct rc_dev *dev, enum rc_proto protocol, u32 scancode,
734 u8 toggle)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300735{
David Härdemana374fef2010-04-02 15:58:29 -0300736 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300737 u32 keycode = rc_g_keycode_from_table(dev, scancode);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300738
David Härdemand8b4b582010-10-29 16:08:23 -0300739 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300740 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300741
David Härdemand8b4b582010-10-29 16:08:23 -0300742 if (dev->keypressed) {
Sean Youngd57ea872017-08-09 13:19:16 -0400743 dev->keyup_jiffies = jiffies +
744 msecs_to_jiffies(protocols[protocol].repeat_period);
David Härdemand8b4b582010-10-29 16:08:23 -0300745 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
David Härdeman62c65032010-10-29 16:08:07 -0300746 }
David Härdemand8b4b582010-10-29 16:08:23 -0300747 spin_unlock_irqrestore(&dev->keylock, flags);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300748}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300749EXPORT_SYMBOL_GPL(rc_keydown);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300750
David Härdeman62c65032010-10-29 16:08:07 -0300751/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300752 * rc_keydown_notimeout() - generates input event for a key press without
David Härdeman62c65032010-10-29 16:08:07 -0300753 * an automatic keyup event at a later time
David Härdemand8b4b582010-10-29 16:08:23 -0300754 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300755 * @protocol: the protocol for the keypress
756 * @scancode: the scancode for the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300757 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
758 * support toggle values, this should be set to zero)
759 *
David Härdemand8b4b582010-10-29 16:08:23 -0300760 * This routine is used to signal that a key has been pressed on the
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300761 * remote control. The driver must manually call rc_keyup() at a later stage.
David Härdeman62c65032010-10-29 16:08:07 -0300762 */
Sean Young6d741bf2017-08-07 16:20:58 -0400763void rc_keydown_notimeout(struct rc_dev *dev, enum rc_proto protocol,
David Härdeman120703f2014-04-03 20:31:30 -0300764 u32 scancode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300765{
766 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300767 u32 keycode = rc_g_keycode_from_table(dev, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300768
David Härdemand8b4b582010-10-29 16:08:23 -0300769 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300770 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
David Härdemand8b4b582010-10-29 16:08:23 -0300771 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300772}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300773EXPORT_SYMBOL_GPL(rc_keydown_notimeout);
David Härdeman62c65032010-10-29 16:08:07 -0300774
Sean Youngb590c0b2016-12-05 19:24:59 -0200775/**
776 * rc_validate_filter() - checks that the scancode and mask are valid and
777 * provides sensible defaults
James Hoganf423ccc2015-03-31 14:48:10 -0300778 * @dev: the struct rc_dev descriptor of the device
Sean Youngb590c0b2016-12-05 19:24:59 -0200779 * @filter: the scancode and mask
780 * @return: 0 or -EINVAL if the filter is not valid
781 */
James Hoganf423ccc2015-03-31 14:48:10 -0300782static int rc_validate_filter(struct rc_dev *dev,
Sean Youngb590c0b2016-12-05 19:24:59 -0200783 struct rc_scancode_filter *filter)
784{
Sean Youngd57ea872017-08-09 13:19:16 -0400785 u32 mask, s = filter->data;
Sean Young6d741bf2017-08-07 16:20:58 -0400786 enum rc_proto protocol = dev->wakeup_protocol;
Sean Youngb590c0b2016-12-05 19:24:59 -0200787
Sean Youngd57ea872017-08-09 13:19:16 -0400788 if (protocol >= ARRAY_SIZE(protocols))
Sean Young2168b412017-08-07 09:21:29 -0400789 return -EINVAL;
790
Sean Youngd57ea872017-08-09 13:19:16 -0400791 mask = protocols[protocol].scancode_bits;
792
Sean Youngb590c0b2016-12-05 19:24:59 -0200793 switch (protocol) {
Sean Young6d741bf2017-08-07 16:20:58 -0400794 case RC_PROTO_NECX:
Sean Youngb590c0b2016-12-05 19:24:59 -0200795 if ((((s >> 16) ^ ~(s >> 8)) & 0xff) == 0)
796 return -EINVAL;
797 break;
Sean Young6d741bf2017-08-07 16:20:58 -0400798 case RC_PROTO_NEC32:
Sean Youngb590c0b2016-12-05 19:24:59 -0200799 if ((((s >> 24) ^ ~(s >> 16)) & 0xff) == 0)
800 return -EINVAL;
801 break;
Sean Young6d741bf2017-08-07 16:20:58 -0400802 case RC_PROTO_RC6_MCE:
Sean Youngb590c0b2016-12-05 19:24:59 -0200803 if ((s & 0xffff0000) != 0x800f0000)
804 return -EINVAL;
805 break;
Sean Young6d741bf2017-08-07 16:20:58 -0400806 case RC_PROTO_RC6_6A_32:
Sean Youngb590c0b2016-12-05 19:24:59 -0200807 if ((s & 0xffff0000) == 0x800f0000)
808 return -EINVAL;
809 break;
810 default:
811 break;
812 }
813
Sean Youngd57ea872017-08-09 13:19:16 -0400814 filter->data &= mask;
815 filter->mask &= mask;
Sean Youngb590c0b2016-12-05 19:24:59 -0200816
James Hoganf423ccc2015-03-31 14:48:10 -0300817 /*
818 * If we have to raw encode the IR for wakeup, we cannot have a mask
819 */
Sean Youngd57ea872017-08-09 13:19:16 -0400820 if (dev->encode_wakeup && filter->mask != 0 && filter->mask != mask)
James Hoganf423ccc2015-03-31 14:48:10 -0300821 return -EINVAL;
822
Sean Youngb590c0b2016-12-05 19:24:59 -0200823 return 0;
824}
825
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300826int rc_open(struct rc_dev *rdev)
827{
828 int rval = 0;
829
830 if (!rdev)
831 return -EINVAL;
832
833 mutex_lock(&rdev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -0200834
Juergen Lockf02dcdd2013-08-16 15:00:24 -0300835 if (!rdev->users++ && rdev->open != NULL)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300836 rval = rdev->open(rdev);
837
838 if (rval)
839 rdev->users--;
840
841 mutex_unlock(&rdev->lock);
842
843 return rval;
844}
845EXPORT_SYMBOL_GPL(rc_open);
846
David Härdemand8b4b582010-10-29 16:08:23 -0300847static int ir_open(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300848{
David Härdemand8b4b582010-10-29 16:08:23 -0300849 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300850
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300851 return rc_open(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300852}
853
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300854void rc_close(struct rc_dev *rdev)
855{
856 if (rdev) {
857 mutex_lock(&rdev->lock);
858
Mauro Carvalho Chehab81b7d142015-04-28 09:43:17 -0300859 if (!--rdev->users && rdev->close != NULL)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300860 rdev->close(rdev);
861
862 mutex_unlock(&rdev->lock);
863 }
864}
865EXPORT_SYMBOL_GPL(rc_close);
866
David Härdemand8b4b582010-10-29 16:08:23 -0300867static void ir_close(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300868{
David Härdemand8b4b582010-10-29 16:08:23 -0300869 struct rc_dev *rdev = input_get_drvdata(idev);
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300870 rc_close(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300871}
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300872
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300873/* class for /sys/class/rc */
David Härdeman40fc5322013-03-06 16:52:10 -0300874static char *rc_devnode(struct device *dev, umode_t *mode)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300875{
876 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
877}
878
David Härdeman40fc5322013-03-06 16:52:10 -0300879static struct class rc_class = {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300880 .name = "rc",
David Härdeman40fc5322013-03-06 16:52:10 -0300881 .devnode = rc_devnode,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300882};
883
David Härdemanc003ab12012-10-11 19:11:54 -0300884/*
885 * These are the protocol textual descriptions that are
886 * used by the sysfs protocols file. Note that the order
887 * of the entries is relevant.
888 */
Heiner Kallweit53df8772015-11-16 17:52:17 -0200889static const struct {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300890 u64 type;
Heiner Kallweit53df8772015-11-16 17:52:17 -0200891 const char *name;
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200892 const char *module_name;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300893} proto_names[] = {
Sean Young6d741bf2017-08-07 16:20:58 -0400894 { RC_PROTO_BIT_NONE, "none", NULL },
895 { RC_PROTO_BIT_OTHER, "other", NULL },
896 { RC_PROTO_BIT_UNKNOWN, "unknown", NULL },
897 { RC_PROTO_BIT_RC5 |
898 RC_PROTO_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
899 { RC_PROTO_BIT_NEC |
900 RC_PROTO_BIT_NECX |
901 RC_PROTO_BIT_NEC32, "nec", "ir-nec-decoder" },
902 { RC_PROTO_BIT_RC6_0 |
903 RC_PROTO_BIT_RC6_6A_20 |
904 RC_PROTO_BIT_RC6_6A_24 |
905 RC_PROTO_BIT_RC6_6A_32 |
906 RC_PROTO_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
907 { RC_PROTO_BIT_JVC, "jvc", "ir-jvc-decoder" },
908 { RC_PROTO_BIT_SONY12 |
909 RC_PROTO_BIT_SONY15 |
910 RC_PROTO_BIT_SONY20, "sony", "ir-sony-decoder" },
911 { RC_PROTO_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
912 { RC_PROTO_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
913 { RC_PROTO_BIT_SHARP, "sharp", "ir-sharp-decoder" },
914 { RC_PROTO_BIT_MCIR2_KBD |
915 RC_PROTO_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" },
916 { RC_PROTO_BIT_XMP, "xmp", "ir-xmp-decoder" },
917 { RC_PROTO_BIT_CEC, "cec", NULL },
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300918};
919
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300920/**
James Hoganab88c662014-02-28 20:17:05 -0300921 * struct rc_filter_attribute - Device attribute relating to a filter type.
922 * @attr: Device attribute.
923 * @type: Filter type.
924 * @mask: false for filter value, true for filter mask.
925 */
926struct rc_filter_attribute {
927 struct device_attribute attr;
928 enum rc_filter_type type;
929 bool mask;
930};
931#define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
932
James Hoganab88c662014-02-28 20:17:05 -0300933#define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
934 struct rc_filter_attribute dev_attr_##_name = { \
935 .attr = __ATTR(_name, _mode, _show, _store), \
936 .type = (_type), \
937 .mask = (_mask), \
938 }
939
David Härdemandd6ff6a2015-07-22 17:55:24 -0300940static bool lirc_is_present(void)
941{
942#if defined(CONFIG_LIRC_MODULE)
943 struct module *lirc;
944
945 mutex_lock(&module_mutex);
946 lirc = find_module("lirc_dev");
947 mutex_unlock(&module_mutex);
948
949 return lirc ? true : false;
950#elif defined(CONFIG_LIRC)
951 return true;
952#else
953 return false;
954#endif
955}
956
James Hoganab88c662014-02-28 20:17:05 -0300957/**
Sean Young0751d332016-12-05 17:08:35 -0200958 * show_protocols() - shows the current IR protocol(s)
David Härdemand8b4b582010-10-29 16:08:23 -0300959 * @device: the device descriptor
David Härdemanda6e1622014-04-03 20:32:16 -0300960 * @mattr: the device attribute struct
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300961 * @buf: a pointer to the output buffer
962 *
963 * This routine is a callback routine for input read the IR protocol type(s).
Sean Young0751d332016-12-05 17:08:35 -0200964 * it is trigged by reading /sys/class/rc/rc?/protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300965 * It returns the protocol names of supported protocols.
966 * Enabled protocols are printed in brackets.
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300967 *
David Härdeman18726a32017-04-27 17:34:08 -0300968 * dev->lock is taken to guard against races between
969 * store_protocols and show_protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300970 */
David Härdemand8b4b582010-10-29 16:08:23 -0300971static ssize_t show_protocols(struct device *device,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300972 struct device_attribute *mattr, char *buf)
973{
David Härdemand8b4b582010-10-29 16:08:23 -0300974 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300975 u64 allowed, enabled;
976 char *tmp = buf;
977 int i;
978
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300979 mutex_lock(&dev->lock);
980
Sean Young0751d332016-12-05 17:08:35 -0200981 enabled = dev->enabled_protocols;
982 allowed = dev->allowed_protocols;
983 if (dev->raw && !allowed)
984 allowed = ir_raw_get_allowed_protocols();
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300985
David Härdemanda6e1622014-04-03 20:32:16 -0300986 mutex_unlock(&dev->lock);
987
988 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
989 __func__, (long long)allowed, (long long)enabled);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300990
991 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
992 if (allowed & enabled & proto_names[i].type)
993 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
994 else if (allowed & proto_names[i].type)
995 tmp += sprintf(tmp, "%s ", proto_names[i].name);
David Härdemanc003ab12012-10-11 19:11:54 -0300996
997 if (allowed & proto_names[i].type)
998 allowed &= ~proto_names[i].type;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300999 }
1000
David Härdemandd6ff6a2015-07-22 17:55:24 -03001001 if (dev->driver_type == RC_DRIVER_IR_RAW && lirc_is_present())
David Härdeman275ddb42015-05-19 19:03:22 -03001002 tmp += sprintf(tmp, "[lirc] ");
1003
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001004 if (tmp != buf)
1005 tmp--;
1006 *tmp = '\n';
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001007
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001008 return tmp + 1 - buf;
1009}
1010
1011/**
David Härdemanda6e1622014-04-03 20:32:16 -03001012 * parse_protocol_change() - parses a protocol change request
1013 * @protocols: pointer to the bitmask of current protocols
1014 * @buf: pointer to the buffer with a list of changes
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001015 *
David Härdemanda6e1622014-04-03 20:32:16 -03001016 * Writing "+proto" will add a protocol to the protocol mask.
1017 * Writing "-proto" will remove a protocol from protocol mask.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001018 * Writing "proto" will enable only "proto".
1019 * Writing "none" will disable all protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001020 * Returns the number of changes performed or a negative error code.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001021 */
David Härdemanda6e1622014-04-03 20:32:16 -03001022static int parse_protocol_change(u64 *protocols, const char *buf)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001023{
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001024 const char *tmp;
David Härdemanda6e1622014-04-03 20:32:16 -03001025 unsigned count = 0;
1026 bool enable, disable;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001027 u64 mask;
David Härdemanda6e1622014-04-03 20:32:16 -03001028 int i;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001029
David Härdemanda6e1622014-04-03 20:32:16 -03001030 while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001031 if (!*tmp)
1032 break;
1033
1034 if (*tmp == '+') {
1035 enable = true;
1036 disable = false;
1037 tmp++;
1038 } else if (*tmp == '-') {
1039 enable = false;
1040 disable = true;
1041 tmp++;
1042 } else {
1043 enable = false;
1044 disable = false;
1045 }
1046
David Härdemanc003ab12012-10-11 19:11:54 -03001047 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1048 if (!strcasecmp(tmp, proto_names[i].name)) {
1049 mask = proto_names[i].type;
1050 break;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001051 }
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001052 }
1053
David Härdemanc003ab12012-10-11 19:11:54 -03001054 if (i == ARRAY_SIZE(proto_names)) {
David Härdeman275ddb42015-05-19 19:03:22 -03001055 if (!strcasecmp(tmp, "lirc"))
1056 mask = 0;
1057 else {
1058 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
1059 return -EINVAL;
1060 }
David Härdemanc003ab12012-10-11 19:11:54 -03001061 }
1062
1063 count++;
1064
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001065 if (enable)
David Härdemanda6e1622014-04-03 20:32:16 -03001066 *protocols |= mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001067 else if (disable)
David Härdemanda6e1622014-04-03 20:32:16 -03001068 *protocols &= ~mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001069 else
David Härdemanda6e1622014-04-03 20:32:16 -03001070 *protocols = mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001071 }
1072
1073 if (!count) {
1074 IR_dprintk(1, "Protocol not specified\n");
David Härdemanda6e1622014-04-03 20:32:16 -03001075 return -EINVAL;
1076 }
1077
1078 return count;
1079}
1080
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001081static void ir_raw_load_modules(u64 *protocols)
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001082{
1083 u64 available;
1084 int i, ret;
1085
1086 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
Sean Young6d741bf2017-08-07 16:20:58 -04001087 if (proto_names[i].type == RC_PROTO_BIT_NONE ||
1088 proto_names[i].type & (RC_PROTO_BIT_OTHER |
1089 RC_PROTO_BIT_UNKNOWN))
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001090 continue;
1091
1092 available = ir_raw_get_allowed_protocols();
1093 if (!(*protocols & proto_names[i].type & ~available))
1094 continue;
1095
1096 if (!proto_names[i].module_name) {
1097 pr_err("Can't enable IR protocol %s\n",
1098 proto_names[i].name);
1099 *protocols &= ~proto_names[i].type;
1100 continue;
1101 }
1102
1103 ret = request_module("%s", proto_names[i].module_name);
1104 if (ret < 0) {
1105 pr_err("Couldn't load IR protocol module %s\n",
1106 proto_names[i].module_name);
1107 *protocols &= ~proto_names[i].type;
1108 continue;
1109 }
1110 msleep(20);
1111 available = ir_raw_get_allowed_protocols();
1112 if (!(*protocols & proto_names[i].type & ~available))
1113 continue;
1114
Sean Young8caebcd2017-01-19 19:33:49 -02001115 pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001116 proto_names[i].module_name,
1117 proto_names[i].name);
1118 *protocols &= ~proto_names[i].type;
1119 }
1120}
1121
David Härdemanda6e1622014-04-03 20:32:16 -03001122/**
1123 * store_protocols() - changes the current/wakeup IR protocol(s)
1124 * @device: the device descriptor
1125 * @mattr: the device attribute struct
1126 * @buf: a pointer to the input buffer
1127 * @len: length of the input buffer
1128 *
1129 * This routine is for changing the IR protocol type.
1130 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
1131 * See parse_protocol_change() for the valid commands.
1132 * Returns @len on success or a negative error code.
1133 *
David Härdeman18726a32017-04-27 17:34:08 -03001134 * dev->lock is taken to guard against races between
1135 * store_protocols and show_protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001136 */
1137static ssize_t store_protocols(struct device *device,
1138 struct device_attribute *mattr,
1139 const char *buf, size_t len)
1140{
1141 struct rc_dev *dev = to_rc_dev(device);
David Härdemanda6e1622014-04-03 20:32:16 -03001142 u64 *current_protocols;
David Härdemanda6e1622014-04-03 20:32:16 -03001143 struct rc_scancode_filter *filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001144 u64 old_protocols, new_protocols;
1145 ssize_t rc;
1146
Sean Young0751d332016-12-05 17:08:35 -02001147 IR_dprintk(1, "Normal protocol change requested\n");
1148 current_protocols = &dev->enabled_protocols;
1149 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001150
Sean Young0751d332016-12-05 17:08:35 -02001151 if (!dev->change_protocol) {
David Härdemanda6e1622014-04-03 20:32:16 -03001152 IR_dprintk(1, "Protocol switching not supported\n");
1153 return -EINVAL;
1154 }
1155
1156 mutex_lock(&dev->lock);
1157
1158 old_protocols = *current_protocols;
1159 new_protocols = old_protocols;
1160 rc = parse_protocol_change(&new_protocols, buf);
1161 if (rc < 0)
1162 goto out;
1163
Sean Young0751d332016-12-05 17:08:35 -02001164 rc = dev->change_protocol(dev, &new_protocols);
David Härdemanda6e1622014-04-03 20:32:16 -03001165 if (rc < 0) {
1166 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
1167 (long long)new_protocols);
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001168 goto out;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001169 }
1170
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001171 if (dev->driver_type == RC_DRIVER_IR_RAW)
1172 ir_raw_load_modules(&new_protocols);
1173
James Hogan983c5bd2014-12-08 13:17:07 -03001174 if (new_protocols != old_protocols) {
1175 *current_protocols = new_protocols;
1176 IR_dprintk(1, "Protocols changed to 0x%llx\n",
1177 (long long)new_protocols);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001178 }
1179
James Hogan6bea25a2014-02-28 20:17:06 -03001180 /*
James Hogan983c5bd2014-12-08 13:17:07 -03001181 * If a protocol change was attempted the filter may need updating, even
1182 * if the actual protocol mask hasn't changed (since the driver may have
1183 * cleared the filter).
James Hogan6bea25a2014-02-28 20:17:06 -03001184 * Try setting the same filter with the new protocol (if any).
1185 * Fall back to clearing the filter.
1186 */
Sean Young0751d332016-12-05 17:08:35 -02001187 if (dev->s_filter && filter->mask) {
David Härdemanda6e1622014-04-03 20:32:16 -03001188 if (new_protocols)
Sean Young0751d332016-12-05 17:08:35 -02001189 rc = dev->s_filter(dev, filter);
David Härdemanda6e1622014-04-03 20:32:16 -03001190 else
1191 rc = -1;
David Härdeman23c843b2014-04-04 19:06:01 -03001192
David Härdemanda6e1622014-04-03 20:32:16 -03001193 if (rc < 0) {
1194 filter->data = 0;
1195 filter->mask = 0;
Sean Young0751d332016-12-05 17:08:35 -02001196 dev->s_filter(dev, filter);
James Hogan6bea25a2014-02-28 20:17:06 -03001197 }
James Hogan6bea25a2014-02-28 20:17:06 -03001198 }
1199
David Härdemanda6e1622014-04-03 20:32:16 -03001200 rc = len;
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001201
1202out:
1203 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001204 return rc;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001205}
1206
James Hogan00942d12014-01-17 10:58:49 -03001207/**
James Hogan00942d12014-01-17 10:58:49 -03001208 * show_filter() - shows the current scancode filter value or mask
1209 * @device: the device descriptor
1210 * @attr: the device attribute struct
1211 * @buf: a pointer to the output buffer
1212 *
1213 * This routine is a callback routine to read a scancode filter value or mask.
1214 * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
1215 * It prints the current scancode filter value or mask of the appropriate filter
1216 * type in hexadecimal into @buf and returns the size of the buffer.
1217 *
1218 * Bits of the filter value corresponding to set bits in the filter mask are
1219 * compared against input scancodes and non-matching scancodes are discarded.
1220 *
David Härdeman18726a32017-04-27 17:34:08 -03001221 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001222 * store_filter and show_filter.
1223 */
1224static ssize_t show_filter(struct device *device,
1225 struct device_attribute *attr,
1226 char *buf)
1227{
1228 struct rc_dev *dev = to_rc_dev(device);
1229 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001230 struct rc_scancode_filter *filter;
James Hogan00942d12014-01-17 10:58:49 -03001231 u32 val;
1232
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001233 mutex_lock(&dev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001234
David Härdemanda6e1622014-04-03 20:32:16 -03001235 if (fattr->type == RC_FILTER_NORMAL)
David Härdemanc5540fb2014-04-03 20:32:21 -03001236 filter = &dev->scancode_filter;
James Hogan00942d12014-01-17 10:58:49 -03001237 else
David Härdemanc5540fb2014-04-03 20:32:21 -03001238 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001239
David Härdemanda6e1622014-04-03 20:32:16 -03001240 if (fattr->mask)
1241 val = filter->mask;
1242 else
1243 val = filter->data;
James Hogan00942d12014-01-17 10:58:49 -03001244 mutex_unlock(&dev->lock);
1245
1246 return sprintf(buf, "%#x\n", val);
1247}
1248
1249/**
1250 * store_filter() - changes the scancode filter value
1251 * @device: the device descriptor
1252 * @attr: the device attribute struct
1253 * @buf: a pointer to the input buffer
1254 * @len: length of the input buffer
1255 *
1256 * This routine is for changing a scancode filter value or mask.
1257 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
1258 * Returns -EINVAL if an invalid filter value for the current protocol was
1259 * specified or if scancode filtering is not supported by the driver, otherwise
1260 * returns @len.
1261 *
1262 * Bits of the filter value corresponding to set bits in the filter mask are
1263 * compared against input scancodes and non-matching scancodes are discarded.
1264 *
David Härdeman18726a32017-04-27 17:34:08 -03001265 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001266 * store_filter and show_filter.
1267 */
1268static ssize_t store_filter(struct device *device,
1269 struct device_attribute *attr,
David Härdemanda6e1622014-04-03 20:32:16 -03001270 const char *buf, size_t len)
James Hogan00942d12014-01-17 10:58:49 -03001271{
1272 struct rc_dev *dev = to_rc_dev(device);
1273 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001274 struct rc_scancode_filter new_filter, *filter;
James Hogan00942d12014-01-17 10:58:49 -03001275 int ret;
1276 unsigned long val;
David Härdeman23c843b2014-04-04 19:06:01 -03001277 int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
James Hogan00942d12014-01-17 10:58:49 -03001278
James Hogan00942d12014-01-17 10:58:49 -03001279 ret = kstrtoul(buf, 0, &val);
1280 if (ret < 0)
1281 return ret;
1282
David Härdemanda6e1622014-04-03 20:32:16 -03001283 if (fattr->type == RC_FILTER_NORMAL) {
1284 set_filter = dev->s_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001285 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001286 } else {
1287 set_filter = dev->s_wakeup_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001288 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001289 }
1290
David Härdeman99b0f3c2014-04-04 19:06:06 -03001291 if (!set_filter)
1292 return -EINVAL;
James Hogan00942d12014-01-17 10:58:49 -03001293
1294 mutex_lock(&dev->lock);
1295
David Härdemanda6e1622014-04-03 20:32:16 -03001296 new_filter = *filter;
James Hogan00942d12014-01-17 10:58:49 -03001297 if (fattr->mask)
David Härdemanda6e1622014-04-03 20:32:16 -03001298 new_filter.mask = val;
James Hogan00942d12014-01-17 10:58:49 -03001299 else
David Härdemanda6e1622014-04-03 20:32:16 -03001300 new_filter.data = val;
David Härdeman23c843b2014-04-04 19:06:01 -03001301
Sean Young0751d332016-12-05 17:08:35 -02001302 if (fattr->type == RC_FILTER_WAKEUP) {
Sean Youngb590c0b2016-12-05 19:24:59 -02001303 /*
1304 * Refuse to set a filter unless a protocol is enabled
1305 * and the filter is valid for that protocol
1306 */
Sean Young6d741bf2017-08-07 16:20:58 -04001307 if (dev->wakeup_protocol != RC_PROTO_UNKNOWN)
James Hoganf423ccc2015-03-31 14:48:10 -03001308 ret = rc_validate_filter(dev, &new_filter);
Sean Youngb590c0b2016-12-05 19:24:59 -02001309 else
Sean Young0751d332016-12-05 17:08:35 -02001310 ret = -EINVAL;
Sean Youngb590c0b2016-12-05 19:24:59 -02001311
1312 if (ret != 0)
Sean Young0751d332016-12-05 17:08:35 -02001313 goto unlock;
Sean Young0751d332016-12-05 17:08:35 -02001314 }
1315
1316 if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
1317 val) {
James Hogan6bea25a2014-02-28 20:17:06 -03001318 /* refuse to set a filter unless a protocol is enabled */
1319 ret = -EINVAL;
1320 goto unlock;
1321 }
David Härdeman23c843b2014-04-04 19:06:01 -03001322
David Härdemanda6e1622014-04-03 20:32:16 -03001323 ret = set_filter(dev, &new_filter);
David Härdeman99b0f3c2014-04-04 19:06:06 -03001324 if (ret < 0)
1325 goto unlock;
James Hogan00942d12014-01-17 10:58:49 -03001326
David Härdemanda6e1622014-04-03 20:32:16 -03001327 *filter = new_filter;
James Hogan00942d12014-01-17 10:58:49 -03001328
1329unlock:
1330 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001331 return (ret < 0) ? ret : len;
James Hogan00942d12014-01-17 10:58:49 -03001332}
1333
Sean Young0751d332016-12-05 17:08:35 -02001334/**
1335 * show_wakeup_protocols() - shows the wakeup IR protocol
1336 * @device: the device descriptor
1337 * @mattr: the device attribute struct
1338 * @buf: a pointer to the output buffer
1339 *
1340 * This routine is a callback routine for input read the IR protocol type(s).
1341 * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols.
1342 * It returns the protocol names of supported protocols.
1343 * The enabled protocols are printed in brackets.
1344 *
David Härdeman18726a32017-04-27 17:34:08 -03001345 * dev->lock is taken to guard against races between
1346 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001347 */
1348static ssize_t show_wakeup_protocols(struct device *device,
1349 struct device_attribute *mattr,
1350 char *buf)
1351{
1352 struct rc_dev *dev = to_rc_dev(device);
1353 u64 allowed;
Sean Young6d741bf2017-08-07 16:20:58 -04001354 enum rc_proto enabled;
Sean Young0751d332016-12-05 17:08:35 -02001355 char *tmp = buf;
1356 int i;
1357
Sean Young0751d332016-12-05 17:08:35 -02001358 mutex_lock(&dev->lock);
1359
1360 allowed = dev->allowed_wakeup_protocols;
1361 enabled = dev->wakeup_protocol;
1362
1363 mutex_unlock(&dev->lock);
1364
1365 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - %d\n",
1366 __func__, (long long)allowed, enabled);
1367
Sean Youngd57ea872017-08-09 13:19:16 -04001368 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001369 if (allowed & (1ULL << i)) {
1370 if (i == enabled)
Sean Youngd57ea872017-08-09 13:19:16 -04001371 tmp += sprintf(tmp, "[%s] ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001372 else
Sean Youngd57ea872017-08-09 13:19:16 -04001373 tmp += sprintf(tmp, "%s ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001374 }
1375 }
1376
1377 if (tmp != buf)
1378 tmp--;
1379 *tmp = '\n';
1380
1381 return tmp + 1 - buf;
1382}
1383
1384/**
1385 * store_wakeup_protocols() - changes the wakeup IR protocol(s)
1386 * @device: the device descriptor
1387 * @mattr: the device attribute struct
1388 * @buf: a pointer to the input buffer
1389 * @len: length of the input buffer
1390 *
1391 * This routine is for changing the IR protocol type.
1392 * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols.
1393 * Returns @len on success or a negative error code.
1394 *
David Härdeman18726a32017-04-27 17:34:08 -03001395 * dev->lock is taken to guard against races between
1396 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001397 */
1398static ssize_t store_wakeup_protocols(struct device *device,
1399 struct device_attribute *mattr,
1400 const char *buf, size_t len)
1401{
1402 struct rc_dev *dev = to_rc_dev(device);
Sean Young6d741bf2017-08-07 16:20:58 -04001403 enum rc_proto protocol;
Sean Young0751d332016-12-05 17:08:35 -02001404 ssize_t rc;
1405 u64 allowed;
1406 int i;
1407
Sean Young0751d332016-12-05 17:08:35 -02001408 mutex_lock(&dev->lock);
1409
1410 allowed = dev->allowed_wakeup_protocols;
1411
1412 if (sysfs_streq(buf, "none")) {
Sean Young6d741bf2017-08-07 16:20:58 -04001413 protocol = RC_PROTO_UNKNOWN;
Sean Young0751d332016-12-05 17:08:35 -02001414 } else {
Sean Youngd57ea872017-08-09 13:19:16 -04001415 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001416 if ((allowed & (1ULL << i)) &&
Sean Youngd57ea872017-08-09 13:19:16 -04001417 sysfs_streq(buf, protocols[i].name)) {
Sean Young0751d332016-12-05 17:08:35 -02001418 protocol = i;
1419 break;
1420 }
1421 }
1422
Sean Youngd57ea872017-08-09 13:19:16 -04001423 if (i == ARRAY_SIZE(protocols)) {
Sean Young0751d332016-12-05 17:08:35 -02001424 rc = -EINVAL;
1425 goto out;
1426 }
James Hoganf423ccc2015-03-31 14:48:10 -03001427
1428 if (dev->encode_wakeup) {
1429 u64 mask = 1ULL << protocol;
1430
1431 ir_raw_load_modules(&mask);
1432 if (!mask) {
1433 rc = -EINVAL;
1434 goto out;
1435 }
1436 }
Sean Young0751d332016-12-05 17:08:35 -02001437 }
1438
1439 if (dev->wakeup_protocol != protocol) {
1440 dev->wakeup_protocol = protocol;
1441 IR_dprintk(1, "Wakeup protocol changed to %d\n", protocol);
1442
Sean Young6d741bf2017-08-07 16:20:58 -04001443 if (protocol == RC_PROTO_RC6_MCE)
Sean Young0751d332016-12-05 17:08:35 -02001444 dev->scancode_wakeup_filter.data = 0x800f0000;
1445 else
1446 dev->scancode_wakeup_filter.data = 0;
1447 dev->scancode_wakeup_filter.mask = 0;
1448
1449 rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
1450 if (rc == 0)
1451 rc = len;
1452 } else {
1453 rc = len;
1454 }
1455
1456out:
1457 mutex_unlock(&dev->lock);
1458 return rc;
1459}
1460
David Härdemand8b4b582010-10-29 16:08:23 -03001461static void rc_dev_release(struct device *device)
1462{
Max Kellermann47cae1e2016-03-21 08:33:05 -03001463 struct rc_dev *dev = to_rc_dev(device);
1464
1465 kfree(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001466}
1467
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001468#define ADD_HOTPLUG_VAR(fmt, val...) \
1469 do { \
1470 int err = add_uevent_var(env, fmt, val); \
1471 if (err) \
1472 return err; \
1473 } while (0)
1474
1475static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1476{
David Härdemand8b4b582010-10-29 16:08:23 -03001477 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001478
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001479 if (dev->rc_map.name)
1480 ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
David Härdemand8b4b582010-10-29 16:08:23 -03001481 if (dev->driver_name)
1482 ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
Sean Youngb9f407e2017-09-01 11:34:23 -03001483 if (dev->device_name)
1484 ADD_HOTPLUG_VAR("DEV_NAME=%s", dev->device_name);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001485
1486 return 0;
1487}
1488
1489/*
1490 * Static device attribute struct with the sysfs attributes for IR's
1491 */
Sean Young6d75db32017-09-01 11:30:50 -03001492static struct device_attribute dev_attr_ro_protocols =
1493__ATTR(protocols, 0444, show_protocols, NULL);
1494static struct device_attribute dev_attr_rw_protocols =
1495__ATTR(protocols, 0644, show_protocols, store_protocols);
Sean Young0751d332016-12-05 17:08:35 -02001496static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
1497 store_wakeup_protocols);
James Hogan00942d12014-01-17 10:58:49 -03001498static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
1499 show_filter, store_filter, RC_FILTER_NORMAL, false);
1500static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
1501 show_filter, store_filter, RC_FILTER_NORMAL, true);
1502static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
1503 show_filter, store_filter, RC_FILTER_WAKEUP, false);
1504static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
1505 show_filter, store_filter, RC_FILTER_WAKEUP, true);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001506
Sean Young6d75db32017-09-01 11:30:50 -03001507static struct attribute *rc_dev_rw_protocol_attrs[] = {
1508 &dev_attr_rw_protocols.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001509 NULL,
1510};
1511
Sean Young6d75db32017-09-01 11:30:50 -03001512static const struct attribute_group rc_dev_rw_protocol_attr_grp = {
1513 .attrs = rc_dev_rw_protocol_attrs,
1514};
1515
1516static struct attribute *rc_dev_ro_protocol_attrs[] = {
1517 &dev_attr_ro_protocols.attr,
1518 NULL,
1519};
1520
1521static const struct attribute_group rc_dev_ro_protocol_attr_grp = {
1522 .attrs = rc_dev_ro_protocol_attrs,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001523};
1524
David Härdeman99b0f3c2014-04-04 19:06:06 -03001525static struct attribute *rc_dev_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001526 &dev_attr_filter.attr.attr,
1527 &dev_attr_filter_mask.attr.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001528 NULL,
1529};
1530
Arvind Yadavdb681022017-07-07 04:23:54 -04001531static const struct attribute_group rc_dev_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001532 .attrs = rc_dev_filter_attrs,
1533};
1534
1535static struct attribute *rc_dev_wakeup_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001536 &dev_attr_wakeup_filter.attr.attr,
1537 &dev_attr_wakeup_filter_mask.attr.attr,
Sean Young0751d332016-12-05 17:08:35 -02001538 &dev_attr_wakeup_protocols.attr,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001539 NULL,
1540};
1541
Arvind Yadavdb681022017-07-07 04:23:54 -04001542static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001543 .attrs = rc_dev_wakeup_filter_attrs,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001544};
1545
Bhumika Goyalf03f02f2017-08-19 05:22:15 -03001546static const struct device_type rc_dev_type = {
David Härdemand8b4b582010-10-29 16:08:23 -03001547 .release = rc_dev_release,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001548 .uevent = rc_dev_uevent,
1549};
1550
Andi Shyti0f7499f2016-12-16 06:50:58 -02001551struct rc_dev *rc_allocate_device(enum rc_driver_type type)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001552{
David Härdemand8b4b582010-10-29 16:08:23 -03001553 struct rc_dev *dev;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001554
David Härdemand8b4b582010-10-29 16:08:23 -03001555 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1556 if (!dev)
1557 return NULL;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001558
Andi Shytid34aee12016-12-16 04:12:15 -02001559 if (type != RC_DRIVER_IR_RAW_TX) {
1560 dev->input_dev = input_allocate_device();
1561 if (!dev->input_dev) {
1562 kfree(dev);
1563 return NULL;
1564 }
1565
1566 dev->input_dev->getkeycode = ir_getkeycode;
1567 dev->input_dev->setkeycode = ir_setkeycode;
1568 input_set_drvdata(dev->input_dev, dev);
1569
1570 setup_timer(&dev->timer_keyup, ir_timer_keyup,
1571 (unsigned long)dev);
1572
1573 spin_lock_init(&dev->rc_map.lock);
1574 spin_lock_init(&dev->keylock);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001575 }
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001576 mutex_init(&dev->lock);
David Härdemand8b4b582010-10-29 16:08:23 -03001577
1578 dev->dev.type = &rc_dev_type;
David Härdeman40fc5322013-03-06 16:52:10 -03001579 dev->dev.class = &rc_class;
David Härdemand8b4b582010-10-29 16:08:23 -03001580 device_initialize(&dev->dev);
1581
Andi Shyti0f7499f2016-12-16 06:50:58 -02001582 dev->driver_type = type;
1583
David Härdemand8b4b582010-10-29 16:08:23 -03001584 __module_get(THIS_MODULE);
1585 return dev;
1586}
1587EXPORT_SYMBOL_GPL(rc_allocate_device);
1588
1589void rc_free_device(struct rc_dev *dev)
1590{
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001591 if (!dev)
1592 return;
1593
Markus Elfring3dd94f02014-11-20 09:01:32 -03001594 input_free_device(dev->input_dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001595
1596 put_device(&dev->dev);
1597
Max Kellermann47cae1e2016-03-21 08:33:05 -03001598 /* kfree(dev) will be called by the callback function
1599 rc_dev_release() */
1600
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001601 module_put(THIS_MODULE);
David Härdemand8b4b582010-10-29 16:08:23 -03001602}
1603EXPORT_SYMBOL_GPL(rc_free_device);
1604
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001605static void devm_rc_alloc_release(struct device *dev, void *res)
1606{
1607 rc_free_device(*(struct rc_dev **)res);
1608}
1609
Andi Shyti0f7499f2016-12-16 06:50:58 -02001610struct rc_dev *devm_rc_allocate_device(struct device *dev,
1611 enum rc_driver_type type)
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001612{
1613 struct rc_dev **dr, *rc;
1614
1615 dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
1616 if (!dr)
1617 return NULL;
1618
Andi Shyti0f7499f2016-12-16 06:50:58 -02001619 rc = rc_allocate_device(type);
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001620 if (!rc) {
1621 devres_free(dr);
1622 return NULL;
1623 }
1624
1625 rc->dev.parent = dev;
1626 rc->managed_alloc = true;
1627 *dr = rc;
1628 devres_add(dev, dr);
1629
1630 return rc;
1631}
1632EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
1633
David Härdemanf56928a2017-05-03 07:04:00 -03001634static int rc_prepare_rx_device(struct rc_dev *dev)
David Härdemand8b4b582010-10-29 16:08:23 -03001635{
David Härdemanfcb13092015-05-19 19:03:17 -03001636 int rc;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001637 struct rc_map *rc_map;
Sean Young6d741bf2017-08-07 16:20:58 -04001638 u64 rc_proto;
David Härdemand8b4b582010-10-29 16:08:23 -03001639
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001640 if (!dev->map_name)
David Härdemand8b4b582010-10-29 16:08:23 -03001641 return -EINVAL;
1642
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001643 rc_map = rc_map_get(dev->map_name);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001644 if (!rc_map)
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001645 rc_map = rc_map_get(RC_MAP_EMPTY);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001646 if (!rc_map || !rc_map->scan || rc_map->size == 0)
David Härdemand8b4b582010-10-29 16:08:23 -03001647 return -EINVAL;
1648
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001649 rc = ir_setkeytable(dev, rc_map);
1650 if (rc)
1651 return rc;
1652
Sean Young6d741bf2017-08-07 16:20:58 -04001653 rc_proto = BIT_ULL(rc_map->rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001654
Sean Young831c4c82017-09-01 09:55:59 -03001655 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1656 dev->enabled_protocols = dev->allowed_protocols;
1657
Sean Young41380862017-02-22 18:48:01 -03001658 if (dev->change_protocol) {
Sean Young6d741bf2017-08-07 16:20:58 -04001659 rc = dev->change_protocol(dev, &rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001660 if (rc < 0)
1661 goto out_table;
Sean Young6d741bf2017-08-07 16:20:58 -04001662 dev->enabled_protocols = rc_proto;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001663 }
1664
Sean Young41380862017-02-22 18:48:01 -03001665 if (dev->driver_type == RC_DRIVER_IR_RAW)
Sean Young6d741bf2017-08-07 16:20:58 -04001666 ir_raw_load_modules(&rc_proto);
Sean Young41380862017-02-22 18:48:01 -03001667
David Härdemand8b4b582010-10-29 16:08:23 -03001668 set_bit(EV_KEY, dev->input_dev->evbit);
1669 set_bit(EV_REP, dev->input_dev->evbit);
1670 set_bit(EV_MSC, dev->input_dev->evbit);
1671 set_bit(MSC_SCAN, dev->input_dev->mscbit);
1672 if (dev->open)
1673 dev->input_dev->open = ir_open;
1674 if (dev->close)
1675 dev->input_dev->close = ir_close;
1676
David Härdemanb2aceb72017-04-27 17:33:58 -03001677 dev->input_dev->dev.parent = &dev->dev;
1678 memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
1679 dev->input_dev->phys = dev->input_phys;
Sean Young518f4b22017-07-01 12:13:19 -04001680 dev->input_dev->name = dev->device_name;
David Härdemanb2aceb72017-04-27 17:33:58 -03001681
David Härdemanf56928a2017-05-03 07:04:00 -03001682 return 0;
1683
1684out_table:
1685 ir_free_table(&dev->rc_map);
1686
1687 return rc;
1688}
1689
1690static int rc_setup_rx_device(struct rc_dev *dev)
1691{
1692 int rc;
1693
David Härdemanb2aceb72017-04-27 17:33:58 -03001694 /* rc_open will be called here */
1695 rc = input_register_device(dev->input_dev);
1696 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001697 return rc;
David Härdemanb2aceb72017-04-27 17:33:58 -03001698
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001699 /*
1700 * Default delay of 250ms is too short for some protocols, especially
1701 * since the timeout is currently set to 250ms. Increase it to 500ms,
1702 * to avoid wrong repetition of the keycodes. Note that this must be
1703 * set after the call to input_register_device().
1704 */
1705 dev->input_dev->rep[REP_DELAY] = 500;
1706
1707 /*
1708 * As a repeat event on protocols like RC-5 and NEC take as long as
1709 * 110/114ms, using 33ms as a repeat period is not the right thing
1710 * to do.
1711 */
1712 dev->input_dev->rep[REP_PERIOD] = 125;
1713
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001714 return 0;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001715}
1716
1717static void rc_free_rx_device(struct rc_dev *dev)
1718{
David Härdemanf56928a2017-05-03 07:04:00 -03001719 if (!dev)
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001720 return;
1721
David Härdemanf56928a2017-05-03 07:04:00 -03001722 if (dev->input_dev) {
1723 input_unregister_device(dev->input_dev);
1724 dev->input_dev = NULL;
1725 }
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001726
David Härdemanf56928a2017-05-03 07:04:00 -03001727 ir_free_table(&dev->rc_map);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001728}
1729
1730int rc_register_device(struct rc_dev *dev)
1731{
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001732 const char *path;
1733 int attr = 0;
1734 int minor;
1735 int rc;
1736
1737 if (!dev)
1738 return -EINVAL;
1739
David Härdemanfcb13092015-05-19 19:03:17 -03001740 minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
1741 if (minor < 0)
1742 return minor;
1743
1744 dev->minor = minor;
1745 dev_set_name(&dev->dev, "rc%u", dev->minor);
1746 dev_set_drvdata(&dev->dev, dev);
Mauro Carvalho Chehab587d1b02014-01-14 16:27:55 -03001747
David Härdeman99b0f3c2014-04-04 19:06:06 -03001748 dev->dev.groups = dev->sysfs_groups;
Sean Young6d75db32017-09-01 11:30:50 -03001749 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1750 dev->sysfs_groups[attr++] = &rc_dev_ro_protocol_attr_grp;
1751 else if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
1752 dev->sysfs_groups[attr++] = &rc_dev_rw_protocol_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001753 if (dev->s_filter)
David Härdeman120703f2014-04-03 20:31:30 -03001754 dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001755 if (dev->s_wakeup_filter)
1756 dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001757 dev->sysfs_groups[attr++] = NULL;
1758
David Härdemanf56928a2017-05-03 07:04:00 -03001759 if (dev->driver_type == RC_DRIVER_IR_RAW ||
1760 dev->driver_type == RC_DRIVER_IR_RAW_TX) {
1761 rc = ir_raw_event_prepare(dev);
1762 if (rc < 0)
1763 goto out_minor;
1764 }
1765
1766 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1767 rc = rc_prepare_rx_device(dev);
1768 if (rc)
1769 goto out_raw;
1770 }
1771
David Härdemand8b4b582010-10-29 16:08:23 -03001772 rc = device_add(&dev->dev);
1773 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001774 goto out_rx_free;
David Härdemand8b4b582010-10-29 16:08:23 -03001775
David Härdemand8b4b582010-10-29 16:08:23 -03001776 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Heiner Kallweit4dc0e902015-10-29 19:39:06 -02001777 dev_info(&dev->dev, "%s as %s\n",
Sean Young518f4b22017-07-01 12:13:19 -04001778 dev->device_name ?: "Unspecified device", path ?: "N/A");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001779 kfree(path);
1780
Sean Young5df62772017-02-23 06:11:21 -03001781 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1782 rc = rc_setup_rx_device(dev);
1783 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001784 goto out_dev;
1785 }
1786
1787 if (dev->driver_type == RC_DRIVER_IR_RAW ||
1788 dev->driver_type == RC_DRIVER_IR_RAW_TX) {
1789 rc = ir_raw_event_register(dev);
1790 if (rc < 0)
1791 goto out_rx;
David Härdemand8b4b582010-10-29 16:08:23 -03001792 }
1793
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001794 IR_dprintk(1, "Registered rc%u (driver: %s)\n",
David Härdemanfcb13092015-05-19 19:03:17 -03001795 dev->minor,
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001796 dev->driver_name ? dev->driver_name : "unknown");
David Härdemand8b4b582010-10-29 16:08:23 -03001797
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001798 return 0;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001799
David Härdemanf56928a2017-05-03 07:04:00 -03001800out_rx:
1801 rc_free_rx_device(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001802out_dev:
1803 device_del(&dev->dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001804out_rx_free:
1805 ir_free_table(&dev->rc_map);
1806out_raw:
1807 ir_raw_event_free(dev);
1808out_minor:
David Härdemanfcb13092015-05-19 19:03:17 -03001809 ida_simple_remove(&rc_ida, minor);
David Härdemand8b4b582010-10-29 16:08:23 -03001810 return rc;
1811}
1812EXPORT_SYMBOL_GPL(rc_register_device);
1813
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001814static void devm_rc_release(struct device *dev, void *res)
1815{
1816 rc_unregister_device(*(struct rc_dev **)res);
1817}
1818
1819int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
1820{
1821 struct rc_dev **dr;
1822 int ret;
1823
1824 dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
1825 if (!dr)
1826 return -ENOMEM;
1827
1828 ret = rc_register_device(dev);
1829 if (ret) {
1830 devres_free(dr);
1831 return ret;
1832 }
1833
1834 *dr = dev;
1835 devres_add(parent, dr);
1836
1837 return 0;
1838}
1839EXPORT_SYMBOL_GPL(devm_rc_register_device);
1840
David Härdemand8b4b582010-10-29 16:08:23 -03001841void rc_unregister_device(struct rc_dev *dev)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001842{
David Härdemand8b4b582010-10-29 16:08:23 -03001843 if (!dev)
1844 return;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001845
David Härdemand8b4b582010-10-29 16:08:23 -03001846 del_timer_sync(&dev->timer_keyup);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001847
David Härdemand8b4b582010-10-29 16:08:23 -03001848 if (dev->driver_type == RC_DRIVER_IR_RAW)
1849 ir_raw_event_unregister(dev);
1850
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001851 rc_free_rx_device(dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001852
1853 device_del(&dev->dev);
1854
David Härdemanfcb13092015-05-19 19:03:17 -03001855 ida_simple_remove(&rc_ida, dev->minor);
1856
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001857 if (!dev->managed_alloc)
1858 rc_free_device(dev);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001859}
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001860
David Härdemand8b4b582010-10-29 16:08:23 -03001861EXPORT_SYMBOL_GPL(rc_unregister_device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001862
1863/*
1864 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1865 */
1866
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001867static int __init rc_core_init(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001868{
David Härdeman40fc5322013-03-06 16:52:10 -03001869 int rc = class_register(&rc_class);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001870 if (rc) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -02001871 pr_err("rc_core: unable to register rc class\n");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001872 return rc;
1873 }
1874
Sean Young153a60b2013-07-30 19:00:01 -03001875 led_trigger_register_simple("rc-feedback", &led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001876 rc_map_register(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001877
1878 return 0;
1879}
1880
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001881static void __exit rc_core_exit(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001882{
David Härdeman40fc5322013-03-06 16:52:10 -03001883 class_unregister(&rc_class);
Sean Young153a60b2013-07-30 19:00:01 -03001884 led_trigger_unregister_simple(led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001885 rc_map_unregister(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001886}
1887
David Härdemane76d4ce2013-03-06 16:52:15 -03001888subsys_initcall(rc_core_init);
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001889module_exit(rc_core_exit);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001890
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001891int rc_core_debug; /* ir_debug level (0,1,2) */
1892EXPORT_SYMBOL_GPL(rc_core_debug);
1893module_param_named(debug, rc_core_debug, int, 0644);
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03001894
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02001895MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03001896MODULE_LICENSE("GPL");