blob: 12ff6d87b113f194cf0193ebcef15b42724b61f2 [file] [log] [blame]
Mauro Carvalho Chehab20835282017-12-01 08:47:08 -05001// SPDX-License-Identifier: GPL-2.0
2// rc-main.c - Remote Controller core module
3//
4// Copyright (C) 2009-2010 by Mauro Carvalho Chehab
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -03005
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -02006#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03008#include <media/rc-core.h>
Thomas Meyer8ca01d42017-09-08 13:33:36 -03009#include <linux/bsearch.h>
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030010#include <linux/spinlock.h>
11#include <linux/delay.h>
Mauro Carvalho Chehab882ead32009-12-29 10:37:38 -030012#include <linux/input.h>
Sean Young153a60b2013-07-30 19:00:01 -030013#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
David Härdemanfcb13092015-05-19 19:03:17 -030015#include <linux/idr.h>
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -030016#include <linux/device.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040017#include <linux/module.h>
Mauro Carvalho Chehabf62de672010-11-09 23:09:57 -030018#include "rc-core-priv.h"
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -030019
David Härdemanb3074c02010-04-02 15:58:28 -030020/* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */
21#define IR_TAB_MIN_SIZE 256
22#define IR_TAB_MAX_SIZE 8192
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -030023
Sean Youngd57ea872017-08-09 13:19:16 -040024static const struct {
25 const char *name;
26 unsigned int repeat_period;
27 unsigned int scancode_bits;
28} protocols[] = {
Sean Young6d741bf2017-08-07 16:20:58 -040029 [RC_PROTO_UNKNOWN] = { .name = "unknown", .repeat_period = 250 },
30 [RC_PROTO_OTHER] = { .name = "other", .repeat_period = 250 },
31 [RC_PROTO_RC5] = { .name = "rc-5",
Sean Young67f0f152017-11-19 16:57:27 -050032 .scancode_bits = 0x1f7f, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040033 [RC_PROTO_RC5X_20] = { .name = "rc-5x-20",
Sean Young67f0f152017-11-19 16:57:27 -050034 .scancode_bits = 0x1f7f3f, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040035 [RC_PROTO_RC5_SZ] = { .name = "rc-5-sz",
Sean Young67f0f152017-11-19 16:57:27 -050036 .scancode_bits = 0x2fff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040037 [RC_PROTO_JVC] = { .name = "jvc",
Sean Youngd57ea872017-08-09 13:19:16 -040038 .scancode_bits = 0xffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040039 [RC_PROTO_SONY12] = { .name = "sony-12",
Sean Young67f0f152017-11-19 16:57:27 -050040 .scancode_bits = 0x1f007f, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040041 [RC_PROTO_SONY15] = { .name = "sony-15",
Sean Young67f0f152017-11-19 16:57:27 -050042 .scancode_bits = 0xff007f, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040043 [RC_PROTO_SONY20] = { .name = "sony-20",
Sean Young67f0f152017-11-19 16:57:27 -050044 .scancode_bits = 0x1fff7f, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040045 [RC_PROTO_NEC] = { .name = "nec",
Sean Young67f0f152017-11-19 16:57:27 -050046 .scancode_bits = 0xffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040047 [RC_PROTO_NECX] = { .name = "nec-x",
Sean Young67f0f152017-11-19 16:57:27 -050048 .scancode_bits = 0xffffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040049 [RC_PROTO_NEC32] = { .name = "nec-32",
Sean Young67f0f152017-11-19 16:57:27 -050050 .scancode_bits = 0xffffffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040051 [RC_PROTO_SANYO] = { .name = "sanyo",
Sean Youngd57ea872017-08-09 13:19:16 -040052 .scancode_bits = 0x1fffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040053 [RC_PROTO_MCIR2_KBD] = { .name = "mcir2-kbd",
Sean Young67f0f152017-11-19 16:57:27 -050054 .scancode_bits = 0xffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040055 [RC_PROTO_MCIR2_MSE] = { .name = "mcir2-mse",
Sean Young67f0f152017-11-19 16:57:27 -050056 .scancode_bits = 0x1fffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040057 [RC_PROTO_RC6_0] = { .name = "rc-6-0",
Sean Young67f0f152017-11-19 16:57:27 -050058 .scancode_bits = 0xffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040059 [RC_PROTO_RC6_6A_20] = { .name = "rc-6-6a-20",
Sean Young67f0f152017-11-19 16:57:27 -050060 .scancode_bits = 0xfffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040061 [RC_PROTO_RC6_6A_24] = { .name = "rc-6-6a-24",
Sean Young67f0f152017-11-19 16:57:27 -050062 .scancode_bits = 0xffffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040063 [RC_PROTO_RC6_6A_32] = { .name = "rc-6-6a-32",
Sean Young67f0f152017-11-19 16:57:27 -050064 .scancode_bits = 0xffffffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040065 [RC_PROTO_RC6_MCE] = { .name = "rc-6-mce",
Sean Young67f0f152017-11-19 16:57:27 -050066 .scancode_bits = 0xffff7fff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040067 [RC_PROTO_SHARP] = { .name = "sharp",
Sean Youngd57ea872017-08-09 13:19:16 -040068 .scancode_bits = 0x1fff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040069 [RC_PROTO_XMP] = { .name = "xmp", .repeat_period = 250 },
70 [RC_PROTO_CEC] = { .name = "cec", .repeat_period = 550 },
Sean Youngd57ea872017-08-09 13:19:16 -040071};
David Härdemana374fef2010-04-02 15:58:29 -030072
David Härdeman4c7b3552010-11-10 11:04:19 -030073/* Used to keep track of known keymaps */
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030074static LIST_HEAD(rc_map_list);
75static DEFINE_SPINLOCK(rc_map_lock);
Sean Young153a60b2013-07-30 19:00:01 -030076static struct led_trigger *led_feedback;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030077
David Härdemanfcb13092015-05-19 19:03:17 -030078/* Used to keep track of rc devices */
79static DEFINE_IDA(rc_ida);
80
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030081static struct rc_map_list *seek_rc_map(const char *name)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030082{
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030083 struct rc_map_list *map = NULL;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030084
85 spin_lock(&rc_map_lock);
86 list_for_each_entry(map, &rc_map_list, list) {
87 if (!strcmp(name, map->map.name)) {
88 spin_unlock(&rc_map_lock);
89 return map;
90 }
91 }
92 spin_unlock(&rc_map_lock);
93
94 return NULL;
95}
96
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030097struct rc_map *rc_map_get(const char *name)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030098{
99
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300100 struct rc_map_list *map;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300101
102 map = seek_rc_map(name);
Russell King2ff56fa2015-10-15 13:15:24 -0300103#ifdef CONFIG_MODULES
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300104 if (!map) {
Kees Cook8ea54882014-03-11 17:25:53 -0300105 int rc = request_module("%s", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300106 if (rc < 0) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -0200107 pr_err("Couldn't load IR keymap %s\n", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300108 return NULL;
109 }
110 msleep(20); /* Give some time for IR to register */
111
112 map = seek_rc_map(name);
113 }
114#endif
115 if (!map) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -0200116 pr_err("IR keymap %s not found\n", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300117 return NULL;
118 }
119
120 printk(KERN_INFO "Registered IR keymap %s\n", map->map.name);
121
122 return &map->map;
123}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300124EXPORT_SYMBOL_GPL(rc_map_get);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300125
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300126int rc_map_register(struct rc_map_list *map)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300127{
128 spin_lock(&rc_map_lock);
129 list_add_tail(&map->list, &rc_map_list);
130 spin_unlock(&rc_map_lock);
131 return 0;
132}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300133EXPORT_SYMBOL_GPL(rc_map_register);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300134
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300135void rc_map_unregister(struct rc_map_list *map)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300136{
137 spin_lock(&rc_map_lock);
138 list_del(&map->list);
139 spin_unlock(&rc_map_lock);
140}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300141EXPORT_SYMBOL_GPL(rc_map_unregister);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300142
143
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300144static struct rc_map_table empty[] = {
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300145 { 0x2a, KEY_COFFEE },
146};
147
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300148static struct rc_map_list empty_map = {
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300149 .map = {
Sean Young6d741bf2017-08-07 16:20:58 -0400150 .scan = empty,
151 .size = ARRAY_SIZE(empty),
152 .rc_proto = RC_PROTO_UNKNOWN, /* Legacy IR type */
153 .name = RC_MAP_EMPTY,
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300154 }
155};
156
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300157/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700158 * ir_create_table() - initializes a scancode table
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300159 * @rc_map: the rc_map to initialize
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700160 * @name: name to assign to the table
Sean Young6d741bf2017-08-07 16:20:58 -0400161 * @rc_proto: ir type to assign to the new table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700162 * @size: initial size of the table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700163 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300164 * This routine will initialize the rc_map and will allocate
David Härdemand8b4b582010-10-29 16:08:23 -0300165 * memory to hold at least the specified number of elements.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500166 *
167 * return: zero on success or a negative error code
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700168 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300169static int ir_create_table(struct rc_map *rc_map,
Sean Young6d741bf2017-08-07 16:20:58 -0400170 const char *name, u64 rc_proto, size_t size)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700171{
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300172 rc_map->name = kstrdup(name, GFP_KERNEL);
173 if (!rc_map->name)
174 return -ENOMEM;
Sean Young6d741bf2017-08-07 16:20:58 -0400175 rc_map->rc_proto = rc_proto;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300176 rc_map->alloc = roundup_pow_of_two(size * sizeof(struct rc_map_table));
177 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300178 rc_map->scan = kmalloc(rc_map->alloc, GFP_KERNEL);
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300179 if (!rc_map->scan) {
180 kfree(rc_map->name);
181 rc_map->name = NULL;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700182 return -ENOMEM;
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300183 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700184
185 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300186 rc_map->size, rc_map->alloc);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700187 return 0;
188}
189
190/**
191 * ir_free_table() - frees memory allocated by a scancode table
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300192 * @rc_map: the table whose mappings need to be freed
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700193 *
194 * This routine will free memory alloctaed for key mappings used by given
195 * scancode table.
196 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300197static void ir_free_table(struct rc_map *rc_map)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700198{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300199 rc_map->size = 0;
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300200 kfree(rc_map->name);
Max Kellermannc183d352016-08-09 18:32:06 -0300201 rc_map->name = NULL;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300202 kfree(rc_map->scan);
203 rc_map->scan = NULL;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700204}
205
206/**
David Härdemanb3074c02010-04-02 15:58:28 -0300207 * ir_resize_table() - resizes a scancode table if necessary
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300208 * @rc_map: the rc_map to resize
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700209 * @gfp_flags: gfp flags to use when allocating memory
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300210 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300211 * This routine will shrink the rc_map if it has lots of
David Härdemanb3074c02010-04-02 15:58:28 -0300212 * unused entries and grow it if it is full.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500213 *
214 * return: zero on success or a negative error code
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300215 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300216static int ir_resize_table(struct rc_map *rc_map, gfp_t gfp_flags)
David Härdemanb3074c02010-04-02 15:58:28 -0300217{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300218 unsigned int oldalloc = rc_map->alloc;
David Härdemanb3074c02010-04-02 15:58:28 -0300219 unsigned int newalloc = oldalloc;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300220 struct rc_map_table *oldscan = rc_map->scan;
221 struct rc_map_table *newscan;
David Härdemanb3074c02010-04-02 15:58:28 -0300222
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300223 if (rc_map->size == rc_map->len) {
David Härdemanb3074c02010-04-02 15:58:28 -0300224 /* All entries in use -> grow keytable */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300225 if (rc_map->alloc >= IR_TAB_MAX_SIZE)
David Härdemanb3074c02010-04-02 15:58:28 -0300226 return -ENOMEM;
227
228 newalloc *= 2;
229 IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
230 }
231
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300232 if ((rc_map->len * 3 < rc_map->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
David Härdemanb3074c02010-04-02 15:58:28 -0300233 /* Less than 1/3 of entries in use -> shrink keytable */
234 newalloc /= 2;
235 IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
236 }
237
238 if (newalloc == oldalloc)
239 return 0;
240
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700241 newscan = kmalloc(newalloc, gfp_flags);
David Härdemanb3074c02010-04-02 15:58:28 -0300242 if (!newscan) {
243 IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
244 return -ENOMEM;
245 }
246
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300247 memcpy(newscan, rc_map->scan, rc_map->len * sizeof(struct rc_map_table));
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300248 rc_map->scan = newscan;
249 rc_map->alloc = newalloc;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300250 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
David Härdemanb3074c02010-04-02 15:58:28 -0300251 kfree(oldscan);
252 return 0;
253}
254
255/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700256 * ir_update_mapping() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300257 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300258 * @rc_map: scancode table to be adjusted
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700259 * @index: index of the mapping that needs to be updated
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500260 * @new_keycode: the desired keycode
David Härdemanb3074c02010-04-02 15:58:28 -0300261 *
David Härdemand8b4b582010-10-29 16:08:23 -0300262 * This routine is used to update scancode->keycode mapping at given
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700263 * position.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500264 *
265 * return: previous keycode assigned to the mapping
266 *
David Härdemanb3074c02010-04-02 15:58:28 -0300267 */
David Härdemand8b4b582010-10-29 16:08:23 -0300268static unsigned int ir_update_mapping(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300269 struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700270 unsigned int index,
271 unsigned int new_keycode)
272{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300273 int old_keycode = rc_map->scan[index].keycode;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700274 int i;
275
276 /* Did the user wish to remove the mapping? */
277 if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
278 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300279 index, rc_map->scan[index].scancode);
280 rc_map->len--;
281 memmove(&rc_map->scan[index], &rc_map->scan[index+ 1],
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300282 (rc_map->len - index) * sizeof(struct rc_map_table));
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700283 } else {
284 IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n",
285 index,
286 old_keycode == KEY_RESERVED ? "New" : "Replacing",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300287 rc_map->scan[index].scancode, new_keycode);
288 rc_map->scan[index].keycode = new_keycode;
David Härdemand8b4b582010-10-29 16:08:23 -0300289 __set_bit(new_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700290 }
291
292 if (old_keycode != KEY_RESERVED) {
293 /* A previous mapping was updated... */
David Härdemand8b4b582010-10-29 16:08:23 -0300294 __clear_bit(old_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700295 /* ... but another scancode might use the same keycode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300296 for (i = 0; i < rc_map->len; i++) {
297 if (rc_map->scan[i].keycode == old_keycode) {
David Härdemand8b4b582010-10-29 16:08:23 -0300298 __set_bit(old_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700299 break;
300 }
301 }
302
303 /* Possibly shrink the keytable, failure is not a problem */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300304 ir_resize_table(rc_map, GFP_ATOMIC);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700305 }
306
307 return old_keycode;
308}
309
310/**
David Härdeman4c7b3552010-11-10 11:04:19 -0300311 * ir_establish_scancode() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300312 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300313 * @rc_map: scancode table to be searched
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700314 * @scancode: the desired scancode
315 * @resize: controls whether we allowed to resize the table to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300316 * accommodate not yet present scancodes
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700317 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300318 * This routine is used to locate given scancode in rc_map.
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700319 * If scancode is not yet present the routine will allocate a new slot
320 * for it.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500321 *
322 * return: index of the mapping containing scancode in question
323 * or -1U in case of failure.
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700324 */
David Härdemand8b4b582010-10-29 16:08:23 -0300325static unsigned int ir_establish_scancode(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300326 struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700327 unsigned int scancode,
328 bool resize)
David Härdemanb3074c02010-04-02 15:58:28 -0300329{
330 unsigned int i;
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300331
332 /*
333 * Unfortunately, some hardware-based IR decoders don't provide
334 * all bits for the complete IR code. In general, they provide only
335 * the command part of the IR code. Yet, as it is possible to replace
336 * the provided IR with another one, it is needed to allow loading
David Härdemand8b4b582010-10-29 16:08:23 -0300337 * IR tables from other remotes. So, we support specifying a mask to
338 * indicate the valid bits of the scancodes.
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300339 */
David Härdeman9d2f1d32014-04-03 20:32:26 -0300340 if (dev->scancode_mask)
341 scancode &= dev->scancode_mask;
David Härdemanb3074c02010-04-02 15:58:28 -0300342
343 /* First check if we already have a mapping for this ir command */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300344 for (i = 0; i < rc_map->len; i++) {
345 if (rc_map->scan[i].scancode == scancode)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700346 return i;
347
David Härdemanb3074c02010-04-02 15:58:28 -0300348 /* Keytable is sorted from lowest to highest scancode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300349 if (rc_map->scan[i].scancode >= scancode)
David Härdemanb3074c02010-04-02 15:58:28 -0300350 break;
David Härdemanb3074c02010-04-02 15:58:28 -0300351 }
352
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700353 /* No previous mapping found, we might need to grow the table */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300354 if (rc_map->size == rc_map->len) {
355 if (!resize || ir_resize_table(rc_map, GFP_ATOMIC))
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700356 return -1U;
357 }
David Härdemanb3074c02010-04-02 15:58:28 -0300358
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700359 /* i is the proper index to insert our new keycode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300360 if (i < rc_map->len)
361 memmove(&rc_map->scan[i + 1], &rc_map->scan[i],
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300362 (rc_map->len - i) * sizeof(struct rc_map_table));
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300363 rc_map->scan[i].scancode = scancode;
364 rc_map->scan[i].keycode = KEY_RESERVED;
365 rc_map->len++;
David Härdemanb3074c02010-04-02 15:58:28 -0300366
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700367 return i;
David Härdemanb3074c02010-04-02 15:58:28 -0300368}
369
370/**
371 * ir_setkeycode() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300372 * @idev: the struct input_dev device descriptor
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500373 * @ke: Input keymap entry
374 * @old_keycode: result
David Härdemanb3074c02010-04-02 15:58:28 -0300375 *
376 * This routine is used to handle evdev EVIOCSKEY ioctl.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500377 *
378 * return: -EINVAL if the keycode could not be inserted, otherwise zero.
David Härdemanb3074c02010-04-02 15:58:28 -0300379 */
David Härdemand8b4b582010-10-29 16:08:23 -0300380static int ir_setkeycode(struct input_dev *idev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700381 const struct input_keymap_entry *ke,
382 unsigned int *old_keycode)
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300383{
David Härdemand8b4b582010-10-29 16:08:23 -0300384 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300385 struct rc_map *rc_map = &rdev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700386 unsigned int index;
387 unsigned int scancode;
Mauro Carvalho Chehabdea8a392010-11-29 07:46:13 -0300388 int retval = 0;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700389 unsigned long flags;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300390
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300391 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700392
393 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
394 index = ke->index;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300395 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700396 retval = -EINVAL;
397 goto out;
398 }
399 } else {
400 retval = input_scancode_to_scalar(ke, &scancode);
401 if (retval)
402 goto out;
403
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300404 index = ir_establish_scancode(rdev, rc_map, scancode, true);
405 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700406 retval = -ENOMEM;
407 goto out;
408 }
409 }
410
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300411 *old_keycode = ir_update_mapping(rdev, rc_map, index, ke->keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700412
413out:
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300414 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700415 return retval;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300416}
417
418/**
David Härdemanb3074c02010-04-02 15:58:28 -0300419 * ir_setkeytable() - sets several entries in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300420 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300421 * @from: the struct rc_map to copy entries from
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300422 *
David Härdemanb3074c02010-04-02 15:58:28 -0300423 * This routine is used to handle table initialization.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500424 *
425 * return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300426 */
David Härdemand8b4b582010-10-29 16:08:23 -0300427static int ir_setkeytable(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300428 const struct rc_map *from)
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300429{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300430 struct rc_map *rc_map = &dev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700431 unsigned int i, index;
432 int rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300433
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300434 rc = ir_create_table(rc_map, from->name,
Sean Young6d741bf2017-08-07 16:20:58 -0400435 from->rc_proto, from->size);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700436 if (rc)
437 return rc;
438
David Härdemanb3074c02010-04-02 15:58:28 -0300439 for (i = 0; i < from->size; i++) {
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300440 index = ir_establish_scancode(dev, rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700441 from->scan[i].scancode, false);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300442 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700443 rc = -ENOMEM;
David Härdemanb3074c02010-04-02 15:58:28 -0300444 break;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700445 }
446
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300447 ir_update_mapping(dev, rc_map, index,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700448 from->scan[i].keycode);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300449 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700450
451 if (rc)
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300452 ir_free_table(rc_map);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700453
David Härdemanb3074c02010-04-02 15:58:28 -0300454 return rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300455}
456
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300457static int rc_map_cmp(const void *key, const void *elt)
458{
459 const unsigned int *scancode = key;
460 const struct rc_map_table *e = elt;
461
462 if (*scancode < e->scancode)
463 return -1;
464 else if (*scancode > e->scancode)
465 return 1;
466 return 0;
467}
468
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300469/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700470 * ir_lookup_by_scancode() - locate mapping by scancode
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300471 * @rc_map: the struct rc_map to search
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700472 * @scancode: scancode to look for in the table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700473 *
474 * This routine performs binary search in RC keykeymap table for
475 * given scancode.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500476 *
477 * return: index in the table, -1U if not found
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700478 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300479static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700480 unsigned int scancode)
481{
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300482 struct rc_map_table *res;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700483
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300484 res = bsearch(&scancode, rc_map->scan, rc_map->len,
485 sizeof(struct rc_map_table), rc_map_cmp);
486 if (!res)
487 return -1U;
488 else
489 return res - rc_map->scan;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700490}
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 Chehabf67f3662017-11-27 08:35:13 -0500495 * @ke: Input keymap entry
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300496 *
497 * This routine is used to handle evdev EVIOCGKEY ioctl.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500498 *
499 * return: always returns zero.
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
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300556 *
David Härdemand8b4b582010-10-29 16:08:23 -0300557 * This routine is used by drivers which need to convert a scancode to a
558 * keycode. Normally it should not be used since drivers should have no
559 * interest in keycodes.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500560 *
561 * return: the corresponding keycode, or KEY_RESERVED
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300562 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300563u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300564{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300565 struct rc_map *rc_map = &dev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700566 unsigned int keycode;
567 unsigned int index;
568 unsigned long flags;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300569
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300570 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700571
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300572 index = ir_lookup_by_scancode(rc_map, scancode);
573 keycode = index < rc_map->len ?
574 rc_map->scan[index].keycode : KEY_RESERVED;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700575
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300576 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700577
Mauro Carvalho Chehab35438942010-04-03 16:53:16 -0300578 if (keycode != KEY_RESERVED)
579 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
Sean Young518f4b22017-07-01 12:13:19 -0400580 dev->device_name, scancode, keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700581
David Härdemanb3074c02010-04-02 15:58:28 -0300582 return keycode;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300583}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300584EXPORT_SYMBOL_GPL(rc_g_keycode_from_table);
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300585
586/**
David Härdeman62c65032010-10-29 16:08:07 -0300587 * ir_do_keyup() - internal function to signal the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300588 * @dev: the struct rc_dev descriptor of the device
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300589 * @sync: whether or not to call input_sync
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300590 *
David Härdeman62c65032010-10-29 16:08:07 -0300591 * This function is used internally to release a keypress, it must be
592 * called with keylock held.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300593 */
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300594static void ir_do_keyup(struct rc_dev *dev, bool sync)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300595{
David Härdemand8b4b582010-10-29 16:08:23 -0300596 if (!dev->keypressed)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300597 return;
598
David Härdemand8b4b582010-10-29 16:08:23 -0300599 IR_dprintk(1, "keyup key 0x%04x\n", dev->last_keycode);
600 input_report_key(dev->input_dev, dev->last_keycode, 0);
Sean Young153a60b2013-07-30 19:00:01 -0300601 led_trigger_event(led_feedback, LED_OFF);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300602 if (sync)
603 input_sync(dev->input_dev);
David Härdemand8b4b582010-10-29 16:08:23 -0300604 dev->keypressed = false;
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300605}
David Härdeman62c65032010-10-29 16:08:07 -0300606
607/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300608 * rc_keyup() - signals the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300609 * @dev: the struct rc_dev descriptor of the device
David Härdeman62c65032010-10-29 16:08:07 -0300610 *
611 * This routine is used to signal that a key has been released on the
612 * remote control.
613 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300614void rc_keyup(struct rc_dev *dev)
David Härdeman62c65032010-10-29 16:08:07 -0300615{
616 unsigned long flags;
David Härdeman62c65032010-10-29 16:08:07 -0300617
David Härdemand8b4b582010-10-29 16:08:23 -0300618 spin_lock_irqsave(&dev->keylock, flags);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300619 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300620 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300621}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300622EXPORT_SYMBOL_GPL(rc_keyup);
David Härdemana374fef2010-04-02 15:58:29 -0300623
624/**
625 * ir_timer_keyup() - generates a keyup event after a timeout
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500626 *
627 * @t: a pointer to the struct timer_list
David Härdemana374fef2010-04-02 15:58:29 -0300628 *
629 * This routine will generate a keyup event some time after a keydown event
630 * is generated when no further activity has been detected.
631 */
Kees Cookb17ec782017-10-24 11:23:14 -0400632static void ir_timer_keyup(struct timer_list *t)
David Härdemana374fef2010-04-02 15:58:29 -0300633{
Kees Cookb17ec782017-10-24 11:23:14 -0400634 struct rc_dev *dev = from_timer(dev, t, timer_keyup);
David Härdemana374fef2010-04-02 15:58:29 -0300635 unsigned long flags;
636
637 /*
638 * ir->keyup_jiffies is used to prevent a race condition if a
639 * hardware interrupt occurs at this point and the keyup timer
640 * event is moved further into the future as a result.
641 *
642 * The timer will then be reactivated and this function called
643 * again in the future. We need to exit gracefully in that case
644 * to allow the input subsystem to do its auto-repeat magic or
645 * a keyup event might follow immediately after the keydown.
646 */
David Härdemand8b4b582010-10-29 16:08:23 -0300647 spin_lock_irqsave(&dev->keylock, flags);
648 if (time_is_before_eq_jiffies(dev->keyup_jiffies))
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300649 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300650 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300651}
652
653/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300654 * rc_repeat() - signals that a key is still pressed
David Härdemand8b4b582010-10-29 16:08:23 -0300655 * @dev: the struct rc_dev descriptor of the device
David Härdemana374fef2010-04-02 15:58:29 -0300656 *
657 * This routine is used by IR decoders when a repeat message which does
658 * not include the necessary bits to reproduce the scancode has been
659 * received.
660 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300661void rc_repeat(struct rc_dev *dev)
David Härdemana374fef2010-04-02 15:58:29 -0300662{
663 unsigned long flags;
Sean Youngd57ea872017-08-09 13:19:16 -0400664 unsigned int timeout = protocols[dev->last_protocol].repeat_period;
David Härdemana374fef2010-04-02 15:58:29 -0300665
David Härdemand8b4b582010-10-29 16:08:23 -0300666 spin_lock_irqsave(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300667
David Härdemand8b4b582010-10-29 16:08:23 -0300668 if (!dev->keypressed)
David Härdemana374fef2010-04-02 15:58:29 -0300669 goto out;
670
David Härdeman265a2982017-06-22 15:23:54 -0400671 input_event(dev->input_dev, EV_MSC, MSC_SCAN, dev->last_scancode);
672 input_sync(dev->input_dev);
673
Sean Youngd57ea872017-08-09 13:19:16 -0400674 dev->keyup_jiffies = jiffies + msecs_to_jiffies(timeout);
David Härdemand8b4b582010-10-29 16:08:23 -0300675 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
David Härdemana374fef2010-04-02 15:58:29 -0300676
677out:
David Härdemand8b4b582010-10-29 16:08:23 -0300678 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300679}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300680EXPORT_SYMBOL_GPL(rc_repeat);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300681
682/**
David Härdeman62c65032010-10-29 16:08:07 -0300683 * ir_do_keydown() - internal function to process a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300684 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300685 * @protocol: the protocol of the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300686 * @scancode: the scancode of the keypress
687 * @keycode: the keycode of the keypress
688 * @toggle: the toggle value of the keypress
689 *
690 * This function is used internally to register a keypress, it must be
691 * called with keylock held.
692 */
Sean Young6d741bf2017-08-07 16:20:58 -0400693static void ir_do_keydown(struct rc_dev *dev, enum rc_proto protocol,
David Härdeman120703f2014-04-03 20:31:30 -0300694 u32 scancode, u32 keycode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300695{
David Härdeman99b0f3c2014-04-04 19:06:06 -0300696 bool new_event = (!dev->keypressed ||
David Härdeman120703f2014-04-03 20:31:30 -0300697 dev->last_protocol != protocol ||
David Härdeman99b0f3c2014-04-04 19:06:06 -0300698 dev->last_scancode != scancode ||
David Härdeman120703f2014-04-03 20:31:30 -0300699 dev->last_toggle != toggle);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300700
701 if (new_event && dev->keypressed)
702 ir_do_keyup(dev, false);
703
David Härdemand8b4b582010-10-29 16:08:23 -0300704 input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300705
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300706 if (new_event && keycode != KEY_RESERVED) {
707 /* Register a keypress */
708 dev->keypressed = true;
David Härdeman120703f2014-04-03 20:31:30 -0300709 dev->last_protocol = protocol;
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300710 dev->last_scancode = scancode;
711 dev->last_toggle = toggle;
712 dev->last_keycode = keycode;
David Härdeman62c65032010-10-29 16:08:07 -0300713
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200714 IR_dprintk(1, "%s: key down event, key 0x%04x, protocol 0x%04x, scancode 0x%08x\n",
Sean Young518f4b22017-07-01 12:13:19 -0400715 dev->device_name, keycode, protocol, scancode);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300716 input_report_key(dev->input_dev, keycode, 1);
James Hogan70a2f912014-01-16 19:56:22 -0300717
718 led_trigger_event(led_feedback, LED_FULL);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300719 }
David Härdeman62c65032010-10-29 16:08:07 -0300720
David Härdemand8b4b582010-10-29 16:08:23 -0300721 input_sync(dev->input_dev);
David Härdeman62c65032010-10-29 16:08:07 -0300722}
723
724/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300725 * rc_keydown() - generates input event for a key press
David Härdemand8b4b582010-10-29 16:08:23 -0300726 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300727 * @protocol: the protocol for the keypress
728 * @scancode: the scancode for the keypress
David Härdemana374fef2010-04-02 15:58:29 -0300729 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
730 * support toggle values, this should be set to zero)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300731 *
David Härdemand8b4b582010-10-29 16:08:23 -0300732 * This routine is used to signal that a key has been pressed on the
733 * remote control.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300734 */
Sean Young6d741bf2017-08-07 16:20:58 -0400735void rc_keydown(struct rc_dev *dev, enum rc_proto protocol, u32 scancode,
736 u8 toggle)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300737{
David Härdemana374fef2010-04-02 15:58:29 -0300738 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300739 u32 keycode = rc_g_keycode_from_table(dev, scancode);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300740
David Härdemand8b4b582010-10-29 16:08:23 -0300741 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300742 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300743
David Härdemand8b4b582010-10-29 16:08:23 -0300744 if (dev->keypressed) {
Sean Youngd57ea872017-08-09 13:19:16 -0400745 dev->keyup_jiffies = jiffies +
746 msecs_to_jiffies(protocols[protocol].repeat_period);
David Härdemand8b4b582010-10-29 16:08:23 -0300747 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
David Härdeman62c65032010-10-29 16:08:07 -0300748 }
David Härdemand8b4b582010-10-29 16:08:23 -0300749 spin_unlock_irqrestore(&dev->keylock, flags);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300750}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300751EXPORT_SYMBOL_GPL(rc_keydown);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300752
David Härdeman62c65032010-10-29 16:08:07 -0300753/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300754 * rc_keydown_notimeout() - generates input event for a key press without
David Härdeman62c65032010-10-29 16:08:07 -0300755 * an automatic keyup event at a later time
David Härdemand8b4b582010-10-29 16:08:23 -0300756 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300757 * @protocol: the protocol for the keypress
758 * @scancode: the scancode for the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300759 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
760 * support toggle values, this should be set to zero)
761 *
David Härdemand8b4b582010-10-29 16:08:23 -0300762 * This routine is used to signal that a key has been pressed on the
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300763 * remote control. The driver must manually call rc_keyup() at a later stage.
David Härdeman62c65032010-10-29 16:08:07 -0300764 */
Sean Young6d741bf2017-08-07 16:20:58 -0400765void rc_keydown_notimeout(struct rc_dev *dev, enum rc_proto protocol,
David Härdeman120703f2014-04-03 20:31:30 -0300766 u32 scancode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300767{
768 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300769 u32 keycode = rc_g_keycode_from_table(dev, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300770
David Härdemand8b4b582010-10-29 16:08:23 -0300771 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300772 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
David Härdemand8b4b582010-10-29 16:08:23 -0300773 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300774}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300775EXPORT_SYMBOL_GPL(rc_keydown_notimeout);
David Härdeman62c65032010-10-29 16:08:07 -0300776
Sean Youngb590c0b2016-12-05 19:24:59 -0200777/**
Sean Young6b514c42017-10-09 16:32:41 -0400778 * rc_validate_scancode() - checks that a scancode is valid for a protocol.
779 * For nec, it should do the opposite of ir_nec_bytes_to_scancode()
Sean Young49a4b362017-09-27 16:00:49 -0400780 * @proto: protocol
781 * @scancode: scancode
782 */
783bool rc_validate_scancode(enum rc_proto proto, u32 scancode)
784{
785 switch (proto) {
Sean Young6b514c42017-10-09 16:32:41 -0400786 /*
787 * NECX has a 16-bit address; if the lower 8 bits match the upper
788 * 8 bits inverted, then the address would match regular nec.
789 */
Sean Young49a4b362017-09-27 16:00:49 -0400790 case RC_PROTO_NECX:
791 if ((((scancode >> 16) ^ ~(scancode >> 8)) & 0xff) == 0)
792 return false;
793 break;
Sean Young6b514c42017-10-09 16:32:41 -0400794 /*
795 * NEC32 has a 16 bit address and 16 bit command. If the lower 8 bits
796 * of the command match the upper 8 bits inverted, then it would
797 * be either NEC or NECX.
798 */
Sean Young49a4b362017-09-27 16:00:49 -0400799 case RC_PROTO_NEC32:
Sean Young6b514c42017-10-09 16:32:41 -0400800 if ((((scancode >> 8) ^ ~scancode) & 0xff) == 0)
Sean Young49a4b362017-09-27 16:00:49 -0400801 return false;
802 break;
Sean Young6b514c42017-10-09 16:32:41 -0400803 /*
804 * If the customer code (top 32-bit) is 0x800f, it is MCE else it
805 * is regular mode-6a 32 bit
806 */
Sean Young49a4b362017-09-27 16:00:49 -0400807 case RC_PROTO_RC6_MCE:
808 if ((scancode & 0xffff0000) != 0x800f0000)
809 return false;
810 break;
811 case RC_PROTO_RC6_6A_32:
812 if ((scancode & 0xffff0000) == 0x800f0000)
813 return false;
814 break;
815 default:
816 break;
817 }
818
819 return true;
820}
821
822/**
Sean Youngb590c0b2016-12-05 19:24:59 -0200823 * rc_validate_filter() - checks that the scancode and mask are valid and
824 * provides sensible defaults
James Hoganf423ccc2015-03-31 14:48:10 -0300825 * @dev: the struct rc_dev descriptor of the device
Sean Youngb590c0b2016-12-05 19:24:59 -0200826 * @filter: the scancode and mask
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500827 *
828 * return: 0 or -EINVAL if the filter is not valid
Sean Youngb590c0b2016-12-05 19:24:59 -0200829 */
James Hoganf423ccc2015-03-31 14:48:10 -0300830static int rc_validate_filter(struct rc_dev *dev,
Sean Youngb590c0b2016-12-05 19:24:59 -0200831 struct rc_scancode_filter *filter)
832{
Sean Youngd57ea872017-08-09 13:19:16 -0400833 u32 mask, s = filter->data;
Sean Young6d741bf2017-08-07 16:20:58 -0400834 enum rc_proto protocol = dev->wakeup_protocol;
Sean Youngb590c0b2016-12-05 19:24:59 -0200835
Sean Youngd57ea872017-08-09 13:19:16 -0400836 if (protocol >= ARRAY_SIZE(protocols))
Sean Young2168b412017-08-07 09:21:29 -0400837 return -EINVAL;
838
Sean Youngd57ea872017-08-09 13:19:16 -0400839 mask = protocols[protocol].scancode_bits;
840
Sean Young49a4b362017-09-27 16:00:49 -0400841 if (!rc_validate_scancode(protocol, s))
842 return -EINVAL;
Sean Youngb590c0b2016-12-05 19:24:59 -0200843
Sean Youngd57ea872017-08-09 13:19:16 -0400844 filter->data &= mask;
845 filter->mask &= mask;
Sean Youngb590c0b2016-12-05 19:24:59 -0200846
James Hoganf423ccc2015-03-31 14:48:10 -0300847 /*
848 * If we have to raw encode the IR for wakeup, we cannot have a mask
849 */
Sean Youngd57ea872017-08-09 13:19:16 -0400850 if (dev->encode_wakeup && filter->mask != 0 && filter->mask != mask)
James Hoganf423ccc2015-03-31 14:48:10 -0300851 return -EINVAL;
852
Sean Youngb590c0b2016-12-05 19:24:59 -0200853 return 0;
854}
855
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300856int rc_open(struct rc_dev *rdev)
857{
858 int rval = 0;
859
860 if (!rdev)
861 return -EINVAL;
862
863 mutex_lock(&rdev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -0200864
Sean Youngcb843432017-09-23 17:44:03 -0400865 if (!rdev->registered) {
866 rval = -ENODEV;
867 } else {
868 if (!rdev->users++ && rdev->open)
869 rval = rdev->open(rdev);
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300870
Sean Youngcb843432017-09-23 17:44:03 -0400871 if (rval)
872 rdev->users--;
873 }
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300874
875 mutex_unlock(&rdev->lock);
876
877 return rval;
878}
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300879
David Härdemand8b4b582010-10-29 16:08:23 -0300880static int ir_open(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300881{
David Härdemand8b4b582010-10-29 16:08:23 -0300882 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300883
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300884 return rc_open(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300885}
886
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300887void rc_close(struct rc_dev *rdev)
888{
889 if (rdev) {
890 mutex_lock(&rdev->lock);
891
Sean Youngcb843432017-09-23 17:44:03 -0400892 if (!--rdev->users && rdev->close && rdev->registered)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300893 rdev->close(rdev);
894
895 mutex_unlock(&rdev->lock);
896 }
897}
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300898
David Härdemand8b4b582010-10-29 16:08:23 -0300899static void ir_close(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300900{
David Härdemand8b4b582010-10-29 16:08:23 -0300901 struct rc_dev *rdev = input_get_drvdata(idev);
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300902 rc_close(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300903}
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300904
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300905/* class for /sys/class/rc */
David Härdeman40fc5322013-03-06 16:52:10 -0300906static char *rc_devnode(struct device *dev, umode_t *mode)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300907{
908 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
909}
910
David Härdeman40fc5322013-03-06 16:52:10 -0300911static struct class rc_class = {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300912 .name = "rc",
David Härdeman40fc5322013-03-06 16:52:10 -0300913 .devnode = rc_devnode,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300914};
915
David Härdemanc003ab12012-10-11 19:11:54 -0300916/*
917 * These are the protocol textual descriptions that are
918 * used by the sysfs protocols file. Note that the order
919 * of the entries is relevant.
920 */
Heiner Kallweit53df8772015-11-16 17:52:17 -0200921static const struct {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300922 u64 type;
Heiner Kallweit53df8772015-11-16 17:52:17 -0200923 const char *name;
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200924 const char *module_name;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300925} proto_names[] = {
Sean Young6d741bf2017-08-07 16:20:58 -0400926 { RC_PROTO_BIT_NONE, "none", NULL },
927 { RC_PROTO_BIT_OTHER, "other", NULL },
928 { RC_PROTO_BIT_UNKNOWN, "unknown", NULL },
929 { RC_PROTO_BIT_RC5 |
930 RC_PROTO_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
931 { RC_PROTO_BIT_NEC |
932 RC_PROTO_BIT_NECX |
933 RC_PROTO_BIT_NEC32, "nec", "ir-nec-decoder" },
934 { RC_PROTO_BIT_RC6_0 |
935 RC_PROTO_BIT_RC6_6A_20 |
936 RC_PROTO_BIT_RC6_6A_24 |
937 RC_PROTO_BIT_RC6_6A_32 |
938 RC_PROTO_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
939 { RC_PROTO_BIT_JVC, "jvc", "ir-jvc-decoder" },
940 { RC_PROTO_BIT_SONY12 |
941 RC_PROTO_BIT_SONY15 |
942 RC_PROTO_BIT_SONY20, "sony", "ir-sony-decoder" },
943 { RC_PROTO_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
944 { RC_PROTO_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
945 { RC_PROTO_BIT_SHARP, "sharp", "ir-sharp-decoder" },
946 { RC_PROTO_BIT_MCIR2_KBD |
947 RC_PROTO_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" },
948 { RC_PROTO_BIT_XMP, "xmp", "ir-xmp-decoder" },
949 { RC_PROTO_BIT_CEC, "cec", NULL },
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300950};
951
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300952/**
James Hoganab88c662014-02-28 20:17:05 -0300953 * struct rc_filter_attribute - Device attribute relating to a filter type.
954 * @attr: Device attribute.
955 * @type: Filter type.
956 * @mask: false for filter value, true for filter mask.
957 */
958struct rc_filter_attribute {
959 struct device_attribute attr;
960 enum rc_filter_type type;
961 bool mask;
962};
963#define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
964
James Hoganab88c662014-02-28 20:17:05 -0300965#define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
966 struct rc_filter_attribute dev_attr_##_name = { \
967 .attr = __ATTR(_name, _mode, _show, _store), \
968 .type = (_type), \
969 .mask = (_mask), \
970 }
971
972/**
Sean Young0751d332016-12-05 17:08:35 -0200973 * show_protocols() - shows the current IR protocol(s)
David Härdemand8b4b582010-10-29 16:08:23 -0300974 * @device: the device descriptor
David Härdemanda6e1622014-04-03 20:32:16 -0300975 * @mattr: the device attribute struct
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300976 * @buf: a pointer to the output buffer
977 *
978 * This routine is a callback routine for input read the IR protocol type(s).
Sean Young0751d332016-12-05 17:08:35 -0200979 * it is trigged by reading /sys/class/rc/rc?/protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300980 * It returns the protocol names of supported protocols.
981 * Enabled protocols are printed in brackets.
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300982 *
David Härdeman18726a32017-04-27 17:34:08 -0300983 * dev->lock is taken to guard against races between
984 * store_protocols and show_protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300985 */
David Härdemand8b4b582010-10-29 16:08:23 -0300986static ssize_t show_protocols(struct device *device,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300987 struct device_attribute *mattr, char *buf)
988{
David Härdemand8b4b582010-10-29 16:08:23 -0300989 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300990 u64 allowed, enabled;
991 char *tmp = buf;
992 int i;
993
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300994 mutex_lock(&dev->lock);
995
Sean Young0751d332016-12-05 17:08:35 -0200996 enabled = dev->enabled_protocols;
997 allowed = dev->allowed_protocols;
998 if (dev->raw && !allowed)
999 allowed = ir_raw_get_allowed_protocols();
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001000
David Härdemanda6e1622014-04-03 20:32:16 -03001001 mutex_unlock(&dev->lock);
1002
1003 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
1004 __func__, (long long)allowed, (long long)enabled);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001005
1006 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1007 if (allowed & enabled & proto_names[i].type)
1008 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
1009 else if (allowed & proto_names[i].type)
1010 tmp += sprintf(tmp, "%s ", proto_names[i].name);
David Härdemanc003ab12012-10-11 19:11:54 -03001011
1012 if (allowed & proto_names[i].type)
1013 allowed &= ~proto_names[i].type;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001014 }
1015
Sean Younga60d64b2017-09-23 10:41:13 -04001016#ifdef CONFIG_LIRC
1017 if (dev->driver_type == RC_DRIVER_IR_RAW)
David Härdeman275ddb42015-05-19 19:03:22 -03001018 tmp += sprintf(tmp, "[lirc] ");
Sean Younga60d64b2017-09-23 10:41:13 -04001019#endif
David Härdeman275ddb42015-05-19 19:03:22 -03001020
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001021 if (tmp != buf)
1022 tmp--;
1023 *tmp = '\n';
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001024
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001025 return tmp + 1 - buf;
1026}
1027
1028/**
David Härdemanda6e1622014-04-03 20:32:16 -03001029 * parse_protocol_change() - parses a protocol change request
1030 * @protocols: pointer to the bitmask of current protocols
1031 * @buf: pointer to the buffer with a list of changes
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001032 *
David Härdemanda6e1622014-04-03 20:32:16 -03001033 * Writing "+proto" will add a protocol to the protocol mask.
1034 * Writing "-proto" will remove a protocol from protocol mask.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001035 * Writing "proto" will enable only "proto".
1036 * Writing "none" will disable all protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001037 * Returns the number of changes performed or a negative error code.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001038 */
David Härdemanda6e1622014-04-03 20:32:16 -03001039static int parse_protocol_change(u64 *protocols, const char *buf)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001040{
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001041 const char *tmp;
David Härdemanda6e1622014-04-03 20:32:16 -03001042 unsigned count = 0;
1043 bool enable, disable;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001044 u64 mask;
David Härdemanda6e1622014-04-03 20:32:16 -03001045 int i;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001046
David Härdemanda6e1622014-04-03 20:32:16 -03001047 while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001048 if (!*tmp)
1049 break;
1050
1051 if (*tmp == '+') {
1052 enable = true;
1053 disable = false;
1054 tmp++;
1055 } else if (*tmp == '-') {
1056 enable = false;
1057 disable = true;
1058 tmp++;
1059 } else {
1060 enable = false;
1061 disable = false;
1062 }
1063
David Härdemanc003ab12012-10-11 19:11:54 -03001064 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1065 if (!strcasecmp(tmp, proto_names[i].name)) {
1066 mask = proto_names[i].type;
1067 break;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001068 }
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001069 }
1070
David Härdemanc003ab12012-10-11 19:11:54 -03001071 if (i == ARRAY_SIZE(proto_names)) {
David Härdeman275ddb42015-05-19 19:03:22 -03001072 if (!strcasecmp(tmp, "lirc"))
1073 mask = 0;
1074 else {
1075 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
1076 return -EINVAL;
1077 }
David Härdemanc003ab12012-10-11 19:11:54 -03001078 }
1079
1080 count++;
1081
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001082 if (enable)
David Härdemanda6e1622014-04-03 20:32:16 -03001083 *protocols |= mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001084 else if (disable)
David Härdemanda6e1622014-04-03 20:32:16 -03001085 *protocols &= ~mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001086 else
David Härdemanda6e1622014-04-03 20:32:16 -03001087 *protocols = mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001088 }
1089
1090 if (!count) {
1091 IR_dprintk(1, "Protocol not specified\n");
David Härdemanda6e1622014-04-03 20:32:16 -03001092 return -EINVAL;
1093 }
1094
1095 return count;
1096}
1097
Sean Young0d39ab02017-02-25 06:51:31 -05001098void ir_raw_load_modules(u64 *protocols)
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001099{
1100 u64 available;
1101 int i, ret;
1102
1103 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
Sean Young6d741bf2017-08-07 16:20:58 -04001104 if (proto_names[i].type == RC_PROTO_BIT_NONE ||
1105 proto_names[i].type & (RC_PROTO_BIT_OTHER |
1106 RC_PROTO_BIT_UNKNOWN))
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001107 continue;
1108
1109 available = ir_raw_get_allowed_protocols();
1110 if (!(*protocols & proto_names[i].type & ~available))
1111 continue;
1112
1113 if (!proto_names[i].module_name) {
1114 pr_err("Can't enable IR protocol %s\n",
1115 proto_names[i].name);
1116 *protocols &= ~proto_names[i].type;
1117 continue;
1118 }
1119
1120 ret = request_module("%s", proto_names[i].module_name);
1121 if (ret < 0) {
1122 pr_err("Couldn't load IR protocol module %s\n",
1123 proto_names[i].module_name);
1124 *protocols &= ~proto_names[i].type;
1125 continue;
1126 }
1127 msleep(20);
1128 available = ir_raw_get_allowed_protocols();
1129 if (!(*protocols & proto_names[i].type & ~available))
1130 continue;
1131
Sean Young8caebcd2017-01-19 19:33:49 -02001132 pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001133 proto_names[i].module_name,
1134 proto_names[i].name);
1135 *protocols &= ~proto_names[i].type;
1136 }
1137}
1138
David Härdemanda6e1622014-04-03 20:32:16 -03001139/**
1140 * store_protocols() - changes the current/wakeup IR protocol(s)
1141 * @device: the device descriptor
1142 * @mattr: the device attribute struct
1143 * @buf: a pointer to the input buffer
1144 * @len: length of the input buffer
1145 *
1146 * This routine is for changing the IR protocol type.
1147 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
1148 * See parse_protocol_change() for the valid commands.
1149 * Returns @len on success or a negative error code.
1150 *
David Härdeman18726a32017-04-27 17:34:08 -03001151 * dev->lock is taken to guard against races between
1152 * store_protocols and show_protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001153 */
1154static ssize_t store_protocols(struct device *device,
1155 struct device_attribute *mattr,
1156 const char *buf, size_t len)
1157{
1158 struct rc_dev *dev = to_rc_dev(device);
David Härdemanda6e1622014-04-03 20:32:16 -03001159 u64 *current_protocols;
David Härdemanda6e1622014-04-03 20:32:16 -03001160 struct rc_scancode_filter *filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001161 u64 old_protocols, new_protocols;
1162 ssize_t rc;
1163
Sean Young0751d332016-12-05 17:08:35 -02001164 IR_dprintk(1, "Normal protocol change requested\n");
1165 current_protocols = &dev->enabled_protocols;
1166 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001167
Sean Young0751d332016-12-05 17:08:35 -02001168 if (!dev->change_protocol) {
David Härdemanda6e1622014-04-03 20:32:16 -03001169 IR_dprintk(1, "Protocol switching not supported\n");
1170 return -EINVAL;
1171 }
1172
1173 mutex_lock(&dev->lock);
1174
1175 old_protocols = *current_protocols;
1176 new_protocols = old_protocols;
1177 rc = parse_protocol_change(&new_protocols, buf);
1178 if (rc < 0)
1179 goto out;
1180
Sean Young0751d332016-12-05 17:08:35 -02001181 rc = dev->change_protocol(dev, &new_protocols);
David Härdemanda6e1622014-04-03 20:32:16 -03001182 if (rc < 0) {
1183 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
1184 (long long)new_protocols);
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001185 goto out;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001186 }
1187
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001188 if (dev->driver_type == RC_DRIVER_IR_RAW)
1189 ir_raw_load_modules(&new_protocols);
1190
James Hogan983c5bd2014-12-08 13:17:07 -03001191 if (new_protocols != old_protocols) {
1192 *current_protocols = new_protocols;
1193 IR_dprintk(1, "Protocols changed to 0x%llx\n",
1194 (long long)new_protocols);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001195 }
1196
James Hogan6bea25a2014-02-28 20:17:06 -03001197 /*
James Hogan983c5bd2014-12-08 13:17:07 -03001198 * If a protocol change was attempted the filter may need updating, even
1199 * if the actual protocol mask hasn't changed (since the driver may have
1200 * cleared the filter).
James Hogan6bea25a2014-02-28 20:17:06 -03001201 * Try setting the same filter with the new protocol (if any).
1202 * Fall back to clearing the filter.
1203 */
Sean Young0751d332016-12-05 17:08:35 -02001204 if (dev->s_filter && filter->mask) {
David Härdemanda6e1622014-04-03 20:32:16 -03001205 if (new_protocols)
Sean Young0751d332016-12-05 17:08:35 -02001206 rc = dev->s_filter(dev, filter);
David Härdemanda6e1622014-04-03 20:32:16 -03001207 else
1208 rc = -1;
David Härdeman23c843b2014-04-04 19:06:01 -03001209
David Härdemanda6e1622014-04-03 20:32:16 -03001210 if (rc < 0) {
1211 filter->data = 0;
1212 filter->mask = 0;
Sean Young0751d332016-12-05 17:08:35 -02001213 dev->s_filter(dev, filter);
James Hogan6bea25a2014-02-28 20:17:06 -03001214 }
James Hogan6bea25a2014-02-28 20:17:06 -03001215 }
1216
David Härdemanda6e1622014-04-03 20:32:16 -03001217 rc = len;
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001218
1219out:
1220 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001221 return rc;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001222}
1223
James Hogan00942d12014-01-17 10:58:49 -03001224/**
James Hogan00942d12014-01-17 10:58:49 -03001225 * show_filter() - shows the current scancode filter value or mask
1226 * @device: the device descriptor
1227 * @attr: the device attribute struct
1228 * @buf: a pointer to the output buffer
1229 *
1230 * This routine is a callback routine to read a scancode filter value or mask.
1231 * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
1232 * It prints the current scancode filter value or mask of the appropriate filter
1233 * type in hexadecimal into @buf and returns the size of the buffer.
1234 *
1235 * Bits of the filter value corresponding to set bits in the filter mask are
1236 * compared against input scancodes and non-matching scancodes are discarded.
1237 *
David Härdeman18726a32017-04-27 17:34:08 -03001238 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001239 * store_filter and show_filter.
1240 */
1241static ssize_t show_filter(struct device *device,
1242 struct device_attribute *attr,
1243 char *buf)
1244{
1245 struct rc_dev *dev = to_rc_dev(device);
1246 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001247 struct rc_scancode_filter *filter;
James Hogan00942d12014-01-17 10:58:49 -03001248 u32 val;
1249
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001250 mutex_lock(&dev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001251
David Härdemanda6e1622014-04-03 20:32:16 -03001252 if (fattr->type == RC_FILTER_NORMAL)
David Härdemanc5540fb2014-04-03 20:32:21 -03001253 filter = &dev->scancode_filter;
James Hogan00942d12014-01-17 10:58:49 -03001254 else
David Härdemanc5540fb2014-04-03 20:32:21 -03001255 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001256
David Härdemanda6e1622014-04-03 20:32:16 -03001257 if (fattr->mask)
1258 val = filter->mask;
1259 else
1260 val = filter->data;
James Hogan00942d12014-01-17 10:58:49 -03001261 mutex_unlock(&dev->lock);
1262
1263 return sprintf(buf, "%#x\n", val);
1264}
1265
1266/**
1267 * store_filter() - changes the scancode filter value
1268 * @device: the device descriptor
1269 * @attr: the device attribute struct
1270 * @buf: a pointer to the input buffer
1271 * @len: length of the input buffer
1272 *
1273 * This routine is for changing a scancode filter value or mask.
1274 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
1275 * Returns -EINVAL if an invalid filter value for the current protocol was
1276 * specified or if scancode filtering is not supported by the driver, otherwise
1277 * returns @len.
1278 *
1279 * Bits of the filter value corresponding to set bits in the filter mask are
1280 * compared against input scancodes and non-matching scancodes are discarded.
1281 *
David Härdeman18726a32017-04-27 17:34:08 -03001282 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001283 * store_filter and show_filter.
1284 */
1285static ssize_t store_filter(struct device *device,
1286 struct device_attribute *attr,
David Härdemanda6e1622014-04-03 20:32:16 -03001287 const char *buf, size_t len)
James Hogan00942d12014-01-17 10:58:49 -03001288{
1289 struct rc_dev *dev = to_rc_dev(device);
1290 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001291 struct rc_scancode_filter new_filter, *filter;
James Hogan00942d12014-01-17 10:58:49 -03001292 int ret;
1293 unsigned long val;
David Härdeman23c843b2014-04-04 19:06:01 -03001294 int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
James Hogan00942d12014-01-17 10:58:49 -03001295
James Hogan00942d12014-01-17 10:58:49 -03001296 ret = kstrtoul(buf, 0, &val);
1297 if (ret < 0)
1298 return ret;
1299
David Härdemanda6e1622014-04-03 20:32:16 -03001300 if (fattr->type == RC_FILTER_NORMAL) {
1301 set_filter = dev->s_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001302 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001303 } else {
1304 set_filter = dev->s_wakeup_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001305 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001306 }
1307
David Härdeman99b0f3c2014-04-04 19:06:06 -03001308 if (!set_filter)
1309 return -EINVAL;
James Hogan00942d12014-01-17 10:58:49 -03001310
1311 mutex_lock(&dev->lock);
1312
David Härdemanda6e1622014-04-03 20:32:16 -03001313 new_filter = *filter;
James Hogan00942d12014-01-17 10:58:49 -03001314 if (fattr->mask)
David Härdemanda6e1622014-04-03 20:32:16 -03001315 new_filter.mask = val;
James Hogan00942d12014-01-17 10:58:49 -03001316 else
David Härdemanda6e1622014-04-03 20:32:16 -03001317 new_filter.data = val;
David Härdeman23c843b2014-04-04 19:06:01 -03001318
Sean Young0751d332016-12-05 17:08:35 -02001319 if (fattr->type == RC_FILTER_WAKEUP) {
Sean Youngb590c0b2016-12-05 19:24:59 -02001320 /*
1321 * Refuse to set a filter unless a protocol is enabled
1322 * and the filter is valid for that protocol
1323 */
Sean Young6d741bf2017-08-07 16:20:58 -04001324 if (dev->wakeup_protocol != RC_PROTO_UNKNOWN)
James Hoganf423ccc2015-03-31 14:48:10 -03001325 ret = rc_validate_filter(dev, &new_filter);
Sean Youngb590c0b2016-12-05 19:24:59 -02001326 else
Sean Young0751d332016-12-05 17:08:35 -02001327 ret = -EINVAL;
Sean Youngb590c0b2016-12-05 19:24:59 -02001328
1329 if (ret != 0)
Sean Young0751d332016-12-05 17:08:35 -02001330 goto unlock;
Sean Young0751d332016-12-05 17:08:35 -02001331 }
1332
1333 if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
1334 val) {
James Hogan6bea25a2014-02-28 20:17:06 -03001335 /* refuse to set a filter unless a protocol is enabled */
1336 ret = -EINVAL;
1337 goto unlock;
1338 }
David Härdeman23c843b2014-04-04 19:06:01 -03001339
David Härdemanda6e1622014-04-03 20:32:16 -03001340 ret = set_filter(dev, &new_filter);
David Härdeman99b0f3c2014-04-04 19:06:06 -03001341 if (ret < 0)
1342 goto unlock;
James Hogan00942d12014-01-17 10:58:49 -03001343
David Härdemanda6e1622014-04-03 20:32:16 -03001344 *filter = new_filter;
James Hogan00942d12014-01-17 10:58:49 -03001345
1346unlock:
1347 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001348 return (ret < 0) ? ret : len;
James Hogan00942d12014-01-17 10:58:49 -03001349}
1350
Sean Young0751d332016-12-05 17:08:35 -02001351/**
1352 * show_wakeup_protocols() - shows the wakeup IR protocol
1353 * @device: the device descriptor
1354 * @mattr: the device attribute struct
1355 * @buf: a pointer to the output buffer
1356 *
1357 * This routine is a callback routine for input read the IR protocol type(s).
1358 * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols.
1359 * It returns the protocol names of supported protocols.
1360 * The enabled protocols are printed in brackets.
1361 *
David Härdeman18726a32017-04-27 17:34:08 -03001362 * dev->lock is taken to guard against races between
1363 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001364 */
1365static ssize_t show_wakeup_protocols(struct device *device,
1366 struct device_attribute *mattr,
1367 char *buf)
1368{
1369 struct rc_dev *dev = to_rc_dev(device);
1370 u64 allowed;
Sean Young6d741bf2017-08-07 16:20:58 -04001371 enum rc_proto enabled;
Sean Young0751d332016-12-05 17:08:35 -02001372 char *tmp = buf;
1373 int i;
1374
Sean Young0751d332016-12-05 17:08:35 -02001375 mutex_lock(&dev->lock);
1376
1377 allowed = dev->allowed_wakeup_protocols;
1378 enabled = dev->wakeup_protocol;
1379
1380 mutex_unlock(&dev->lock);
1381
1382 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - %d\n",
1383 __func__, (long long)allowed, enabled);
1384
Sean Youngd57ea872017-08-09 13:19:16 -04001385 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001386 if (allowed & (1ULL << i)) {
1387 if (i == enabled)
Sean Youngd57ea872017-08-09 13:19:16 -04001388 tmp += sprintf(tmp, "[%s] ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001389 else
Sean Youngd57ea872017-08-09 13:19:16 -04001390 tmp += sprintf(tmp, "%s ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001391 }
1392 }
1393
1394 if (tmp != buf)
1395 tmp--;
1396 *tmp = '\n';
1397
1398 return tmp + 1 - buf;
1399}
1400
1401/**
1402 * store_wakeup_protocols() - changes the wakeup IR protocol(s)
1403 * @device: the device descriptor
1404 * @mattr: the device attribute struct
1405 * @buf: a pointer to the input buffer
1406 * @len: length of the input buffer
1407 *
1408 * This routine is for changing the IR protocol type.
1409 * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols.
1410 * Returns @len on success or a negative error code.
1411 *
David Härdeman18726a32017-04-27 17:34:08 -03001412 * dev->lock is taken to guard against races between
1413 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001414 */
1415static ssize_t store_wakeup_protocols(struct device *device,
1416 struct device_attribute *mattr,
1417 const char *buf, size_t len)
1418{
1419 struct rc_dev *dev = to_rc_dev(device);
Sean Young6d741bf2017-08-07 16:20:58 -04001420 enum rc_proto protocol;
Sean Young0751d332016-12-05 17:08:35 -02001421 ssize_t rc;
1422 u64 allowed;
1423 int i;
1424
Sean Young0751d332016-12-05 17:08:35 -02001425 mutex_lock(&dev->lock);
1426
1427 allowed = dev->allowed_wakeup_protocols;
1428
1429 if (sysfs_streq(buf, "none")) {
Sean Young6d741bf2017-08-07 16:20:58 -04001430 protocol = RC_PROTO_UNKNOWN;
Sean Young0751d332016-12-05 17:08:35 -02001431 } else {
Sean Youngd57ea872017-08-09 13:19:16 -04001432 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001433 if ((allowed & (1ULL << i)) &&
Sean Youngd57ea872017-08-09 13:19:16 -04001434 sysfs_streq(buf, protocols[i].name)) {
Sean Young0751d332016-12-05 17:08:35 -02001435 protocol = i;
1436 break;
1437 }
1438 }
1439
Sean Youngd57ea872017-08-09 13:19:16 -04001440 if (i == ARRAY_SIZE(protocols)) {
Sean Young0751d332016-12-05 17:08:35 -02001441 rc = -EINVAL;
1442 goto out;
1443 }
James Hoganf423ccc2015-03-31 14:48:10 -03001444
1445 if (dev->encode_wakeup) {
1446 u64 mask = 1ULL << protocol;
1447
1448 ir_raw_load_modules(&mask);
1449 if (!mask) {
1450 rc = -EINVAL;
1451 goto out;
1452 }
1453 }
Sean Young0751d332016-12-05 17:08:35 -02001454 }
1455
1456 if (dev->wakeup_protocol != protocol) {
1457 dev->wakeup_protocol = protocol;
1458 IR_dprintk(1, "Wakeup protocol changed to %d\n", protocol);
1459
Sean Young6d741bf2017-08-07 16:20:58 -04001460 if (protocol == RC_PROTO_RC6_MCE)
Sean Young0751d332016-12-05 17:08:35 -02001461 dev->scancode_wakeup_filter.data = 0x800f0000;
1462 else
1463 dev->scancode_wakeup_filter.data = 0;
1464 dev->scancode_wakeup_filter.mask = 0;
1465
1466 rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
1467 if (rc == 0)
1468 rc = len;
1469 } else {
1470 rc = len;
1471 }
1472
1473out:
1474 mutex_unlock(&dev->lock);
1475 return rc;
1476}
1477
David Härdemand8b4b582010-10-29 16:08:23 -03001478static void rc_dev_release(struct device *device)
1479{
Max Kellermann47cae1e2016-03-21 08:33:05 -03001480 struct rc_dev *dev = to_rc_dev(device);
1481
1482 kfree(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001483}
1484
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001485#define ADD_HOTPLUG_VAR(fmt, val...) \
1486 do { \
1487 int err = add_uevent_var(env, fmt, val); \
1488 if (err) \
1489 return err; \
1490 } while (0)
1491
1492static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1493{
David Härdemand8b4b582010-10-29 16:08:23 -03001494 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001495
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001496 if (dev->rc_map.name)
1497 ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
David Härdemand8b4b582010-10-29 16:08:23 -03001498 if (dev->driver_name)
1499 ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
Sean Youngb9f407e2017-09-01 11:34:23 -03001500 if (dev->device_name)
1501 ADD_HOTPLUG_VAR("DEV_NAME=%s", dev->device_name);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001502
1503 return 0;
1504}
1505
1506/*
1507 * Static device attribute struct with the sysfs attributes for IR's
1508 */
Sean Young6d75db32017-09-01 11:30:50 -03001509static struct device_attribute dev_attr_ro_protocols =
1510__ATTR(protocols, 0444, show_protocols, NULL);
1511static struct device_attribute dev_attr_rw_protocols =
1512__ATTR(protocols, 0644, show_protocols, store_protocols);
Sean Young0751d332016-12-05 17:08:35 -02001513static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
1514 store_wakeup_protocols);
James Hogan00942d12014-01-17 10:58:49 -03001515static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
1516 show_filter, store_filter, RC_FILTER_NORMAL, false);
1517static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
1518 show_filter, store_filter, RC_FILTER_NORMAL, true);
1519static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
1520 show_filter, store_filter, RC_FILTER_WAKEUP, false);
1521static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
1522 show_filter, store_filter, RC_FILTER_WAKEUP, true);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001523
Sean Young6d75db32017-09-01 11:30:50 -03001524static struct attribute *rc_dev_rw_protocol_attrs[] = {
1525 &dev_attr_rw_protocols.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001526 NULL,
1527};
1528
Sean Young6d75db32017-09-01 11:30:50 -03001529static const struct attribute_group rc_dev_rw_protocol_attr_grp = {
1530 .attrs = rc_dev_rw_protocol_attrs,
1531};
1532
1533static struct attribute *rc_dev_ro_protocol_attrs[] = {
1534 &dev_attr_ro_protocols.attr,
1535 NULL,
1536};
1537
1538static const struct attribute_group rc_dev_ro_protocol_attr_grp = {
1539 .attrs = rc_dev_ro_protocol_attrs,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001540};
1541
David Härdeman99b0f3c2014-04-04 19:06:06 -03001542static struct attribute *rc_dev_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001543 &dev_attr_filter.attr.attr,
1544 &dev_attr_filter_mask.attr.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001545 NULL,
1546};
1547
Arvind Yadavdb681022017-07-07 04:23:54 -04001548static const struct attribute_group rc_dev_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001549 .attrs = rc_dev_filter_attrs,
1550};
1551
1552static struct attribute *rc_dev_wakeup_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001553 &dev_attr_wakeup_filter.attr.attr,
1554 &dev_attr_wakeup_filter_mask.attr.attr,
Sean Young0751d332016-12-05 17:08:35 -02001555 &dev_attr_wakeup_protocols.attr,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001556 NULL,
1557};
1558
Arvind Yadavdb681022017-07-07 04:23:54 -04001559static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001560 .attrs = rc_dev_wakeup_filter_attrs,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001561};
1562
Bhumika Goyalf03f02f2017-08-19 05:22:15 -03001563static const struct device_type rc_dev_type = {
David Härdemand8b4b582010-10-29 16:08:23 -03001564 .release = rc_dev_release,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001565 .uevent = rc_dev_uevent,
1566};
1567
Andi Shyti0f7499f2016-12-16 06:50:58 -02001568struct rc_dev *rc_allocate_device(enum rc_driver_type type)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001569{
David Härdemand8b4b582010-10-29 16:08:23 -03001570 struct rc_dev *dev;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001571
David Härdemand8b4b582010-10-29 16:08:23 -03001572 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1573 if (!dev)
1574 return NULL;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001575
Andi Shytid34aee12016-12-16 04:12:15 -02001576 if (type != RC_DRIVER_IR_RAW_TX) {
1577 dev->input_dev = input_allocate_device();
1578 if (!dev->input_dev) {
1579 kfree(dev);
1580 return NULL;
1581 }
1582
1583 dev->input_dev->getkeycode = ir_getkeycode;
1584 dev->input_dev->setkeycode = ir_setkeycode;
1585 input_set_drvdata(dev->input_dev, dev);
1586
Kees Cookb17ec782017-10-24 11:23:14 -04001587 timer_setup(&dev->timer_keyup, ir_timer_keyup, 0);
Andi Shytid34aee12016-12-16 04:12:15 -02001588
1589 spin_lock_init(&dev->rc_map.lock);
1590 spin_lock_init(&dev->keylock);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001591 }
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001592 mutex_init(&dev->lock);
David Härdemand8b4b582010-10-29 16:08:23 -03001593
1594 dev->dev.type = &rc_dev_type;
David Härdeman40fc5322013-03-06 16:52:10 -03001595 dev->dev.class = &rc_class;
David Härdemand8b4b582010-10-29 16:08:23 -03001596 device_initialize(&dev->dev);
1597
Andi Shyti0f7499f2016-12-16 06:50:58 -02001598 dev->driver_type = type;
1599
David Härdemand8b4b582010-10-29 16:08:23 -03001600 __module_get(THIS_MODULE);
1601 return dev;
1602}
1603EXPORT_SYMBOL_GPL(rc_allocate_device);
1604
1605void rc_free_device(struct rc_dev *dev)
1606{
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001607 if (!dev)
1608 return;
1609
Markus Elfring3dd94f02014-11-20 09:01:32 -03001610 input_free_device(dev->input_dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001611
1612 put_device(&dev->dev);
1613
Max Kellermann47cae1e2016-03-21 08:33:05 -03001614 /* kfree(dev) will be called by the callback function
1615 rc_dev_release() */
1616
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001617 module_put(THIS_MODULE);
David Härdemand8b4b582010-10-29 16:08:23 -03001618}
1619EXPORT_SYMBOL_GPL(rc_free_device);
1620
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001621static void devm_rc_alloc_release(struct device *dev, void *res)
1622{
1623 rc_free_device(*(struct rc_dev **)res);
1624}
1625
Andi Shyti0f7499f2016-12-16 06:50:58 -02001626struct rc_dev *devm_rc_allocate_device(struct device *dev,
1627 enum rc_driver_type type)
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001628{
1629 struct rc_dev **dr, *rc;
1630
1631 dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
1632 if (!dr)
1633 return NULL;
1634
Andi Shyti0f7499f2016-12-16 06:50:58 -02001635 rc = rc_allocate_device(type);
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001636 if (!rc) {
1637 devres_free(dr);
1638 return NULL;
1639 }
1640
1641 rc->dev.parent = dev;
1642 rc->managed_alloc = true;
1643 *dr = rc;
1644 devres_add(dev, dr);
1645
1646 return rc;
1647}
1648EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
1649
David Härdemanf56928a2017-05-03 07:04:00 -03001650static int rc_prepare_rx_device(struct rc_dev *dev)
David Härdemand8b4b582010-10-29 16:08:23 -03001651{
David Härdemanfcb13092015-05-19 19:03:17 -03001652 int rc;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001653 struct rc_map *rc_map;
Sean Young6d741bf2017-08-07 16:20:58 -04001654 u64 rc_proto;
David Härdemand8b4b582010-10-29 16:08:23 -03001655
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001656 if (!dev->map_name)
David Härdemand8b4b582010-10-29 16:08:23 -03001657 return -EINVAL;
1658
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001659 rc_map = rc_map_get(dev->map_name);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001660 if (!rc_map)
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001661 rc_map = rc_map_get(RC_MAP_EMPTY);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001662 if (!rc_map || !rc_map->scan || rc_map->size == 0)
David Härdemand8b4b582010-10-29 16:08:23 -03001663 return -EINVAL;
1664
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001665 rc = ir_setkeytable(dev, rc_map);
1666 if (rc)
1667 return rc;
1668
Sean Young6d741bf2017-08-07 16:20:58 -04001669 rc_proto = BIT_ULL(rc_map->rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001670
Sean Young831c4c82017-09-01 09:55:59 -03001671 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1672 dev->enabled_protocols = dev->allowed_protocols;
1673
Sean Young41380862017-02-22 18:48:01 -03001674 if (dev->change_protocol) {
Sean Young6d741bf2017-08-07 16:20:58 -04001675 rc = dev->change_protocol(dev, &rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001676 if (rc < 0)
1677 goto out_table;
Sean Young6d741bf2017-08-07 16:20:58 -04001678 dev->enabled_protocols = rc_proto;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001679 }
1680
Sean Young41380862017-02-22 18:48:01 -03001681 if (dev->driver_type == RC_DRIVER_IR_RAW)
Sean Young6d741bf2017-08-07 16:20:58 -04001682 ir_raw_load_modules(&rc_proto);
Sean Young41380862017-02-22 18:48:01 -03001683
David Härdemand8b4b582010-10-29 16:08:23 -03001684 set_bit(EV_KEY, dev->input_dev->evbit);
1685 set_bit(EV_REP, dev->input_dev->evbit);
1686 set_bit(EV_MSC, dev->input_dev->evbit);
1687 set_bit(MSC_SCAN, dev->input_dev->mscbit);
1688 if (dev->open)
1689 dev->input_dev->open = ir_open;
1690 if (dev->close)
1691 dev->input_dev->close = ir_close;
1692
David Härdemanb2aceb72017-04-27 17:33:58 -03001693 dev->input_dev->dev.parent = &dev->dev;
1694 memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
1695 dev->input_dev->phys = dev->input_phys;
Sean Young518f4b22017-07-01 12:13:19 -04001696 dev->input_dev->name = dev->device_name;
David Härdemanb2aceb72017-04-27 17:33:58 -03001697
David Härdemanf56928a2017-05-03 07:04:00 -03001698 return 0;
1699
1700out_table:
1701 ir_free_table(&dev->rc_map);
1702
1703 return rc;
1704}
1705
1706static int rc_setup_rx_device(struct rc_dev *dev)
1707{
1708 int rc;
1709
David Härdemanb2aceb72017-04-27 17:33:58 -03001710 /* rc_open will be called here */
1711 rc = input_register_device(dev->input_dev);
1712 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001713 return rc;
David Härdemanb2aceb72017-04-27 17:33:58 -03001714
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001715 /*
1716 * Default delay of 250ms is too short for some protocols, especially
1717 * since the timeout is currently set to 250ms. Increase it to 500ms,
1718 * to avoid wrong repetition of the keycodes. Note that this must be
1719 * set after the call to input_register_device().
1720 */
1721 dev->input_dev->rep[REP_DELAY] = 500;
1722
1723 /*
1724 * As a repeat event on protocols like RC-5 and NEC take as long as
1725 * 110/114ms, using 33ms as a repeat period is not the right thing
1726 * to do.
1727 */
1728 dev->input_dev->rep[REP_PERIOD] = 125;
1729
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001730 return 0;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001731}
1732
1733static void rc_free_rx_device(struct rc_dev *dev)
1734{
David Härdemanf56928a2017-05-03 07:04:00 -03001735 if (!dev)
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001736 return;
1737
David Härdemanf56928a2017-05-03 07:04:00 -03001738 if (dev->input_dev) {
1739 input_unregister_device(dev->input_dev);
1740 dev->input_dev = NULL;
1741 }
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001742
David Härdemanf56928a2017-05-03 07:04:00 -03001743 ir_free_table(&dev->rc_map);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001744}
1745
1746int rc_register_device(struct rc_dev *dev)
1747{
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001748 const char *path;
1749 int attr = 0;
1750 int minor;
1751 int rc;
1752
1753 if (!dev)
1754 return -EINVAL;
1755
David Härdemanfcb13092015-05-19 19:03:17 -03001756 minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
1757 if (minor < 0)
1758 return minor;
1759
1760 dev->minor = minor;
1761 dev_set_name(&dev->dev, "rc%u", dev->minor);
1762 dev_set_drvdata(&dev->dev, dev);
Mauro Carvalho Chehab587d1b02014-01-14 16:27:55 -03001763
David Härdeman99b0f3c2014-04-04 19:06:06 -03001764 dev->dev.groups = dev->sysfs_groups;
Sean Young6d75db32017-09-01 11:30:50 -03001765 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1766 dev->sysfs_groups[attr++] = &rc_dev_ro_protocol_attr_grp;
1767 else if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
1768 dev->sysfs_groups[attr++] = &rc_dev_rw_protocol_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001769 if (dev->s_filter)
David Härdeman120703f2014-04-03 20:31:30 -03001770 dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001771 if (dev->s_wakeup_filter)
1772 dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001773 dev->sysfs_groups[attr++] = NULL;
1774
Sean Younga60d64b2017-09-23 10:41:13 -04001775 if (dev->driver_type == RC_DRIVER_IR_RAW) {
David Härdemanf56928a2017-05-03 07:04:00 -03001776 rc = ir_raw_event_prepare(dev);
1777 if (rc < 0)
1778 goto out_minor;
1779 }
1780
1781 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1782 rc = rc_prepare_rx_device(dev);
1783 if (rc)
1784 goto out_raw;
1785 }
1786
David Härdemand8b4b582010-10-29 16:08:23 -03001787 rc = device_add(&dev->dev);
1788 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001789 goto out_rx_free;
David Härdemand8b4b582010-10-29 16:08:23 -03001790
David Härdemand8b4b582010-10-29 16:08:23 -03001791 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Heiner Kallweit4dc0e902015-10-29 19:39:06 -02001792 dev_info(&dev->dev, "%s as %s\n",
Sean Young518f4b22017-07-01 12:13:19 -04001793 dev->device_name ?: "Unspecified device", path ?: "N/A");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001794 kfree(path);
1795
Sean Young5df62772017-02-23 06:11:21 -03001796 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1797 rc = rc_setup_rx_device(dev);
1798 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001799 goto out_dev;
1800 }
1801
Sean Younga60d64b2017-09-23 10:41:13 -04001802 /* Ensure that the lirc kfifo is setup before we start the thread */
1803 if (dev->driver_type != RC_DRIVER_SCANCODE) {
1804 rc = ir_lirc_register(dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001805 if (rc < 0)
1806 goto out_rx;
David Härdemand8b4b582010-10-29 16:08:23 -03001807 }
1808
Sean Younga60d64b2017-09-23 10:41:13 -04001809 if (dev->driver_type == RC_DRIVER_IR_RAW) {
1810 rc = ir_raw_event_register(dev);
1811 if (rc < 0)
1812 goto out_lirc;
1813 }
1814
Sean Young7790e812017-09-26 07:31:29 -04001815 dev->registered = true;
1816
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001817 IR_dprintk(1, "Registered rc%u (driver: %s)\n",
David Härdemanfcb13092015-05-19 19:03:17 -03001818 dev->minor,
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001819 dev->driver_name ? dev->driver_name : "unknown");
David Härdemand8b4b582010-10-29 16:08:23 -03001820
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001821 return 0;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001822
Sean Younga60d64b2017-09-23 10:41:13 -04001823out_lirc:
1824 if (dev->driver_type != RC_DRIVER_SCANCODE)
1825 ir_lirc_unregister(dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001826out_rx:
1827 rc_free_rx_device(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001828out_dev:
1829 device_del(&dev->dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001830out_rx_free:
1831 ir_free_table(&dev->rc_map);
1832out_raw:
1833 ir_raw_event_free(dev);
1834out_minor:
David Härdemanfcb13092015-05-19 19:03:17 -03001835 ida_simple_remove(&rc_ida, minor);
David Härdemand8b4b582010-10-29 16:08:23 -03001836 return rc;
1837}
1838EXPORT_SYMBOL_GPL(rc_register_device);
1839
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001840static void devm_rc_release(struct device *dev, void *res)
1841{
1842 rc_unregister_device(*(struct rc_dev **)res);
1843}
1844
1845int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
1846{
1847 struct rc_dev **dr;
1848 int ret;
1849
1850 dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
1851 if (!dr)
1852 return -ENOMEM;
1853
1854 ret = rc_register_device(dev);
1855 if (ret) {
1856 devres_free(dr);
1857 return ret;
1858 }
1859
1860 *dr = dev;
1861 devres_add(parent, dr);
1862
1863 return 0;
1864}
1865EXPORT_SYMBOL_GPL(devm_rc_register_device);
1866
David Härdemand8b4b582010-10-29 16:08:23 -03001867void rc_unregister_device(struct rc_dev *dev)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001868{
David Härdemand8b4b582010-10-29 16:08:23 -03001869 if (!dev)
1870 return;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001871
David Härdemand8b4b582010-10-29 16:08:23 -03001872 del_timer_sync(&dev->timer_keyup);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001873
David Härdemand8b4b582010-10-29 16:08:23 -03001874 if (dev->driver_type == RC_DRIVER_IR_RAW)
1875 ir_raw_event_unregister(dev);
1876
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001877 rc_free_rx_device(dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001878
Sean Young7790e812017-09-26 07:31:29 -04001879 mutex_lock(&dev->lock);
1880 dev->registered = false;
1881 mutex_unlock(&dev->lock);
1882
1883 /*
1884 * lirc device should be freed with dev->registered = false, so
1885 * that userspace polling will get notified.
1886 */
Sean Younga60d64b2017-09-23 10:41:13 -04001887 if (dev->driver_type != RC_DRIVER_SCANCODE)
1888 ir_lirc_unregister(dev);
1889
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001890 device_del(&dev->dev);
1891
David Härdemanfcb13092015-05-19 19:03:17 -03001892 ida_simple_remove(&rc_ida, dev->minor);
1893
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001894 if (!dev->managed_alloc)
1895 rc_free_device(dev);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001896}
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001897
David Härdemand8b4b582010-10-29 16:08:23 -03001898EXPORT_SYMBOL_GPL(rc_unregister_device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001899
1900/*
1901 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1902 */
1903
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001904static int __init rc_core_init(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001905{
David Härdeman40fc5322013-03-06 16:52:10 -03001906 int rc = class_register(&rc_class);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001907 if (rc) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -02001908 pr_err("rc_core: unable to register rc class\n");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001909 return rc;
1910 }
1911
Sean Younga60d64b2017-09-23 10:41:13 -04001912 rc = lirc_dev_init();
1913 if (rc) {
1914 pr_err("rc_core: unable to init lirc\n");
1915 class_unregister(&rc_class);
1916 return 0;
1917 }
1918
Sean Young153a60b2013-07-30 19:00:01 -03001919 led_trigger_register_simple("rc-feedback", &led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001920 rc_map_register(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001921
1922 return 0;
1923}
1924
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001925static void __exit rc_core_exit(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001926{
Sean Younga60d64b2017-09-23 10:41:13 -04001927 lirc_dev_exit();
David Härdeman40fc5322013-03-06 16:52:10 -03001928 class_unregister(&rc_class);
Sean Young153a60b2013-07-30 19:00:01 -03001929 led_trigger_unregister_simple(led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001930 rc_map_unregister(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001931}
1932
David Härdemane76d4ce2013-03-06 16:52:15 -03001933subsys_initcall(rc_core_init);
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001934module_exit(rc_core_exit);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001935
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001936int rc_core_debug; /* ir_debug level (0,1,2) */
1937EXPORT_SYMBOL_GPL(rc_core_debug);
1938module_param_named(debug, rc_core_debug, int, 0644);
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03001939
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02001940MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehab20835282017-12-01 08:47:08 -05001941MODULE_LICENSE("GPL v2");