blob: c144b77eac987b5e5431b7e49cb27941002d6b04 [file] [log] [blame]
David Härdeman829ba9f2010-11-19 20:43:27 -03001/* rc-main.c - Remote Controller core module
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -03002 *
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02003 * Copyright (C) 2009-2010 by Mauro Carvalho Chehab
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -030013 */
14
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -020015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -030017#include <media/rc-core.h>
Thomas Meyer8ca01d42017-09-08 13:33:36 -030018#include <linux/bsearch.h>
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030019#include <linux/spinlock.h>
20#include <linux/delay.h>
Mauro Carvalho Chehab882ead32009-12-29 10:37:38 -030021#include <linux/input.h>
Sean Young153a60b2013-07-30 19:00:01 -030022#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
David Härdemanfcb13092015-05-19 19:03:17 -030024#include <linux/idr.h>
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -030025#include <linux/device.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040026#include <linux/module.h>
Mauro Carvalho Chehabf62de672010-11-09 23:09:57 -030027#include "rc-core-priv.h"
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -030028
David Härdemanb3074c02010-04-02 15:58:28 -030029/* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */
30#define IR_TAB_MIN_SIZE 256
31#define IR_TAB_MAX_SIZE 8192
David Härdemanfcb13092015-05-19 19:03:17 -030032#define RC_DEV_MAX 256
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -030033
Sean Youngd57ea872017-08-09 13:19:16 -040034static const struct {
35 const char *name;
36 unsigned int repeat_period;
37 unsigned int scancode_bits;
38} protocols[] = {
Sean Young6d741bf2017-08-07 16:20:58 -040039 [RC_PROTO_UNKNOWN] = { .name = "unknown", .repeat_period = 250 },
40 [RC_PROTO_OTHER] = { .name = "other", .repeat_period = 250 },
41 [RC_PROTO_RC5] = { .name = "rc-5",
Sean Young67f0f152017-11-19 16:57:27 -050042 .scancode_bits = 0x1f7f, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040043 [RC_PROTO_RC5X_20] = { .name = "rc-5x-20",
Sean Young67f0f152017-11-19 16:57:27 -050044 .scancode_bits = 0x1f7f3f, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040045 [RC_PROTO_RC5_SZ] = { .name = "rc-5-sz",
Sean Young67f0f152017-11-19 16:57:27 -050046 .scancode_bits = 0x2fff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040047 [RC_PROTO_JVC] = { .name = "jvc",
Sean Youngd57ea872017-08-09 13:19:16 -040048 .scancode_bits = 0xffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040049 [RC_PROTO_SONY12] = { .name = "sony-12",
Sean Young67f0f152017-11-19 16:57:27 -050050 .scancode_bits = 0x1f007f, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040051 [RC_PROTO_SONY15] = { .name = "sony-15",
Sean Young67f0f152017-11-19 16:57:27 -050052 .scancode_bits = 0xff007f, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040053 [RC_PROTO_SONY20] = { .name = "sony-20",
Sean Young67f0f152017-11-19 16:57:27 -050054 .scancode_bits = 0x1fff7f, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040055 [RC_PROTO_NEC] = { .name = "nec",
Sean Young67f0f152017-11-19 16:57:27 -050056 .scancode_bits = 0xffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040057 [RC_PROTO_NECX] = { .name = "nec-x",
Sean Young67f0f152017-11-19 16:57:27 -050058 .scancode_bits = 0xffffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040059 [RC_PROTO_NEC32] = { .name = "nec-32",
Sean Young67f0f152017-11-19 16:57:27 -050060 .scancode_bits = 0xffffffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040061 [RC_PROTO_SANYO] = { .name = "sanyo",
Sean Youngd57ea872017-08-09 13:19:16 -040062 .scancode_bits = 0x1fffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040063 [RC_PROTO_MCIR2_KBD] = { .name = "mcir2-kbd",
Sean Young67f0f152017-11-19 16:57:27 -050064 .scancode_bits = 0xffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040065 [RC_PROTO_MCIR2_MSE] = { .name = "mcir2-mse",
Sean Young67f0f152017-11-19 16:57:27 -050066 .scancode_bits = 0x1fffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040067 [RC_PROTO_RC6_0] = { .name = "rc-6-0",
Sean Young67f0f152017-11-19 16:57:27 -050068 .scancode_bits = 0xffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040069 [RC_PROTO_RC6_6A_20] = { .name = "rc-6-6a-20",
Sean Young67f0f152017-11-19 16:57:27 -050070 .scancode_bits = 0xfffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040071 [RC_PROTO_RC6_6A_24] = { .name = "rc-6-6a-24",
Sean Young67f0f152017-11-19 16:57:27 -050072 .scancode_bits = 0xffffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040073 [RC_PROTO_RC6_6A_32] = { .name = "rc-6-6a-32",
Sean Young67f0f152017-11-19 16:57:27 -050074 .scancode_bits = 0xffffffff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040075 [RC_PROTO_RC6_MCE] = { .name = "rc-6-mce",
Sean Young67f0f152017-11-19 16:57:27 -050076 .scancode_bits = 0xffff7fff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040077 [RC_PROTO_SHARP] = { .name = "sharp",
Sean Youngd57ea872017-08-09 13:19:16 -040078 .scancode_bits = 0x1fff, .repeat_period = 250 },
Sean Young6d741bf2017-08-07 16:20:58 -040079 [RC_PROTO_XMP] = { .name = "xmp", .repeat_period = 250 },
80 [RC_PROTO_CEC] = { .name = "cec", .repeat_period = 550 },
Sean Youngd57ea872017-08-09 13:19:16 -040081};
David Härdemana374fef2010-04-02 15:58:29 -030082
David Härdeman4c7b3552010-11-10 11:04:19 -030083/* Used to keep track of known keymaps */
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030084static LIST_HEAD(rc_map_list);
85static DEFINE_SPINLOCK(rc_map_lock);
Sean Young153a60b2013-07-30 19:00:01 -030086static struct led_trigger *led_feedback;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030087
David Härdemanfcb13092015-05-19 19:03:17 -030088/* Used to keep track of rc devices */
89static DEFINE_IDA(rc_ida);
90
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030091static struct rc_map_list *seek_rc_map(const char *name)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030092{
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030093 struct rc_map_list *map = NULL;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030094
95 spin_lock(&rc_map_lock);
96 list_for_each_entry(map, &rc_map_list, list) {
97 if (!strcmp(name, map->map.name)) {
98 spin_unlock(&rc_map_lock);
99 return map;
100 }
101 }
102 spin_unlock(&rc_map_lock);
103
104 return NULL;
105}
106
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300107struct rc_map *rc_map_get(const char *name)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300108{
109
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300110 struct rc_map_list *map;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300111
112 map = seek_rc_map(name);
Russell King2ff56fa2015-10-15 13:15:24 -0300113#ifdef CONFIG_MODULES
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300114 if (!map) {
Kees Cook8ea54882014-03-11 17:25:53 -0300115 int rc = request_module("%s", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300116 if (rc < 0) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -0200117 pr_err("Couldn't load IR keymap %s\n", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300118 return NULL;
119 }
120 msleep(20); /* Give some time for IR to register */
121
122 map = seek_rc_map(name);
123 }
124#endif
125 if (!map) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -0200126 pr_err("IR keymap %s not found\n", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300127 return NULL;
128 }
129
130 printk(KERN_INFO "Registered IR keymap %s\n", map->map.name);
131
132 return &map->map;
133}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300134EXPORT_SYMBOL_GPL(rc_map_get);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300135
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300136int rc_map_register(struct rc_map_list *map)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300137{
138 spin_lock(&rc_map_lock);
139 list_add_tail(&map->list, &rc_map_list);
140 spin_unlock(&rc_map_lock);
141 return 0;
142}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300143EXPORT_SYMBOL_GPL(rc_map_register);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300144
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300145void rc_map_unregister(struct rc_map_list *map)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300146{
147 spin_lock(&rc_map_lock);
148 list_del(&map->list);
149 spin_unlock(&rc_map_lock);
150}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300151EXPORT_SYMBOL_GPL(rc_map_unregister);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300152
153
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300154static struct rc_map_table empty[] = {
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300155 { 0x2a, KEY_COFFEE },
156};
157
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300158static struct rc_map_list empty_map = {
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300159 .map = {
Sean Young6d741bf2017-08-07 16:20:58 -0400160 .scan = empty,
161 .size = ARRAY_SIZE(empty),
162 .rc_proto = RC_PROTO_UNKNOWN, /* Legacy IR type */
163 .name = RC_MAP_EMPTY,
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300164 }
165};
166
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300167/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700168 * ir_create_table() - initializes a scancode table
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300169 * @rc_map: the rc_map to initialize
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700170 * @name: name to assign to the table
Sean Young6d741bf2017-08-07 16:20:58 -0400171 * @rc_proto: ir type to assign to the new table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700172 * @size: initial size of the table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700173 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300174 * This routine will initialize the rc_map and will allocate
David Härdemand8b4b582010-10-29 16:08:23 -0300175 * memory to hold at least the specified number of elements.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500176 *
177 * return: zero on success or a negative error code
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700178 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300179static int ir_create_table(struct rc_map *rc_map,
Sean Young6d741bf2017-08-07 16:20:58 -0400180 const char *name, u64 rc_proto, size_t size)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700181{
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300182 rc_map->name = kstrdup(name, GFP_KERNEL);
183 if (!rc_map->name)
184 return -ENOMEM;
Sean Young6d741bf2017-08-07 16:20:58 -0400185 rc_map->rc_proto = rc_proto;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300186 rc_map->alloc = roundup_pow_of_two(size * sizeof(struct rc_map_table));
187 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300188 rc_map->scan = kmalloc(rc_map->alloc, GFP_KERNEL);
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300189 if (!rc_map->scan) {
190 kfree(rc_map->name);
191 rc_map->name = NULL;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700192 return -ENOMEM;
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300193 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700194
195 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300196 rc_map->size, rc_map->alloc);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700197 return 0;
198}
199
200/**
201 * ir_free_table() - frees memory allocated by a scancode table
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300202 * @rc_map: the table whose mappings need to be freed
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700203 *
204 * This routine will free memory alloctaed for key mappings used by given
205 * scancode table.
206 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300207static void ir_free_table(struct rc_map *rc_map)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700208{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300209 rc_map->size = 0;
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300210 kfree(rc_map->name);
Max Kellermannc183d352016-08-09 18:32:06 -0300211 rc_map->name = NULL;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300212 kfree(rc_map->scan);
213 rc_map->scan = NULL;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700214}
215
216/**
David Härdemanb3074c02010-04-02 15:58:28 -0300217 * ir_resize_table() - resizes a scancode table if necessary
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300218 * @rc_map: the rc_map to resize
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700219 * @gfp_flags: gfp flags to use when allocating memory
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300220 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300221 * This routine will shrink the rc_map if it has lots of
David Härdemanb3074c02010-04-02 15:58:28 -0300222 * unused entries and grow it if it is full.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500223 *
224 * return: zero on success or a negative error code
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300225 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300226static int ir_resize_table(struct rc_map *rc_map, gfp_t gfp_flags)
David Härdemanb3074c02010-04-02 15:58:28 -0300227{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300228 unsigned int oldalloc = rc_map->alloc;
David Härdemanb3074c02010-04-02 15:58:28 -0300229 unsigned int newalloc = oldalloc;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300230 struct rc_map_table *oldscan = rc_map->scan;
231 struct rc_map_table *newscan;
David Härdemanb3074c02010-04-02 15:58:28 -0300232
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300233 if (rc_map->size == rc_map->len) {
David Härdemanb3074c02010-04-02 15:58:28 -0300234 /* All entries in use -> grow keytable */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300235 if (rc_map->alloc >= IR_TAB_MAX_SIZE)
David Härdemanb3074c02010-04-02 15:58:28 -0300236 return -ENOMEM;
237
238 newalloc *= 2;
239 IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
240 }
241
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300242 if ((rc_map->len * 3 < rc_map->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
David Härdemanb3074c02010-04-02 15:58:28 -0300243 /* Less than 1/3 of entries in use -> shrink keytable */
244 newalloc /= 2;
245 IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
246 }
247
248 if (newalloc == oldalloc)
249 return 0;
250
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700251 newscan = kmalloc(newalloc, gfp_flags);
David Härdemanb3074c02010-04-02 15:58:28 -0300252 if (!newscan) {
253 IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
254 return -ENOMEM;
255 }
256
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300257 memcpy(newscan, rc_map->scan, rc_map->len * sizeof(struct rc_map_table));
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300258 rc_map->scan = newscan;
259 rc_map->alloc = newalloc;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300260 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
David Härdemanb3074c02010-04-02 15:58:28 -0300261 kfree(oldscan);
262 return 0;
263}
264
265/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700266 * ir_update_mapping() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300267 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300268 * @rc_map: scancode table to be adjusted
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700269 * @index: index of the mapping that needs to be updated
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500270 * @new_keycode: the desired keycode
David Härdemanb3074c02010-04-02 15:58:28 -0300271 *
David Härdemand8b4b582010-10-29 16:08:23 -0300272 * This routine is used to update scancode->keycode mapping at given
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700273 * position.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500274 *
275 * return: previous keycode assigned to the mapping
276 *
David Härdemanb3074c02010-04-02 15:58:28 -0300277 */
David Härdemand8b4b582010-10-29 16:08:23 -0300278static unsigned int ir_update_mapping(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300279 struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700280 unsigned int index,
281 unsigned int new_keycode)
282{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300283 int old_keycode = rc_map->scan[index].keycode;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700284 int i;
285
286 /* Did the user wish to remove the mapping? */
287 if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
288 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300289 index, rc_map->scan[index].scancode);
290 rc_map->len--;
291 memmove(&rc_map->scan[index], &rc_map->scan[index+ 1],
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300292 (rc_map->len - index) * sizeof(struct rc_map_table));
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700293 } else {
294 IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n",
295 index,
296 old_keycode == KEY_RESERVED ? "New" : "Replacing",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300297 rc_map->scan[index].scancode, new_keycode);
298 rc_map->scan[index].keycode = new_keycode;
David Härdemand8b4b582010-10-29 16:08:23 -0300299 __set_bit(new_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700300 }
301
302 if (old_keycode != KEY_RESERVED) {
303 /* A previous mapping was updated... */
David Härdemand8b4b582010-10-29 16:08:23 -0300304 __clear_bit(old_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700305 /* ... but another scancode might use the same keycode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300306 for (i = 0; i < rc_map->len; i++) {
307 if (rc_map->scan[i].keycode == old_keycode) {
David Härdemand8b4b582010-10-29 16:08:23 -0300308 __set_bit(old_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700309 break;
310 }
311 }
312
313 /* Possibly shrink the keytable, failure is not a problem */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300314 ir_resize_table(rc_map, GFP_ATOMIC);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700315 }
316
317 return old_keycode;
318}
319
320/**
David Härdeman4c7b3552010-11-10 11:04:19 -0300321 * ir_establish_scancode() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300322 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300323 * @rc_map: scancode table to be searched
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700324 * @scancode: the desired scancode
325 * @resize: controls whether we allowed to resize the table to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300326 * accommodate not yet present scancodes
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700327 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300328 * This routine is used to locate given scancode in rc_map.
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700329 * If scancode is not yet present the routine will allocate a new slot
330 * for it.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500331 *
332 * return: index of the mapping containing scancode in question
333 * or -1U in case of failure.
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700334 */
David Härdemand8b4b582010-10-29 16:08:23 -0300335static unsigned int ir_establish_scancode(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300336 struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700337 unsigned int scancode,
338 bool resize)
David Härdemanb3074c02010-04-02 15:58:28 -0300339{
340 unsigned int i;
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300341
342 /*
343 * Unfortunately, some hardware-based IR decoders don't provide
344 * all bits for the complete IR code. In general, they provide only
345 * the command part of the IR code. Yet, as it is possible to replace
346 * the provided IR with another one, it is needed to allow loading
David Härdemand8b4b582010-10-29 16:08:23 -0300347 * IR tables from other remotes. So, we support specifying a mask to
348 * indicate the valid bits of the scancodes.
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300349 */
David Härdeman9d2f1d32014-04-03 20:32:26 -0300350 if (dev->scancode_mask)
351 scancode &= dev->scancode_mask;
David Härdemanb3074c02010-04-02 15:58:28 -0300352
353 /* First check if we already have a mapping for this ir command */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300354 for (i = 0; i < rc_map->len; i++) {
355 if (rc_map->scan[i].scancode == scancode)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700356 return i;
357
David Härdemanb3074c02010-04-02 15:58:28 -0300358 /* Keytable is sorted from lowest to highest scancode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300359 if (rc_map->scan[i].scancode >= scancode)
David Härdemanb3074c02010-04-02 15:58:28 -0300360 break;
David Härdemanb3074c02010-04-02 15:58:28 -0300361 }
362
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700363 /* No previous mapping found, we might need to grow the table */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300364 if (rc_map->size == rc_map->len) {
365 if (!resize || ir_resize_table(rc_map, GFP_ATOMIC))
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700366 return -1U;
367 }
David Härdemanb3074c02010-04-02 15:58:28 -0300368
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700369 /* i is the proper index to insert our new keycode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300370 if (i < rc_map->len)
371 memmove(&rc_map->scan[i + 1], &rc_map->scan[i],
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300372 (rc_map->len - i) * sizeof(struct rc_map_table));
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300373 rc_map->scan[i].scancode = scancode;
374 rc_map->scan[i].keycode = KEY_RESERVED;
375 rc_map->len++;
David Härdemanb3074c02010-04-02 15:58:28 -0300376
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700377 return i;
David Härdemanb3074c02010-04-02 15:58:28 -0300378}
379
380/**
381 * ir_setkeycode() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300382 * @idev: the struct input_dev device descriptor
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500383 * @ke: Input keymap entry
384 * @old_keycode: result
David Härdemanb3074c02010-04-02 15:58:28 -0300385 *
386 * This routine is used to handle evdev EVIOCSKEY ioctl.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500387 *
388 * return: -EINVAL if the keycode could not be inserted, otherwise zero.
David Härdemanb3074c02010-04-02 15:58:28 -0300389 */
David Härdemand8b4b582010-10-29 16:08:23 -0300390static int ir_setkeycode(struct input_dev *idev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700391 const struct input_keymap_entry *ke,
392 unsigned int *old_keycode)
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300393{
David Härdemand8b4b582010-10-29 16:08:23 -0300394 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300395 struct rc_map *rc_map = &rdev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700396 unsigned int index;
397 unsigned int scancode;
Mauro Carvalho Chehabdea8a392010-11-29 07:46:13 -0300398 int retval = 0;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700399 unsigned long flags;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300400
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300401 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700402
403 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
404 index = ke->index;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300405 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700406 retval = -EINVAL;
407 goto out;
408 }
409 } else {
410 retval = input_scancode_to_scalar(ke, &scancode);
411 if (retval)
412 goto out;
413
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300414 index = ir_establish_scancode(rdev, rc_map, scancode, true);
415 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700416 retval = -ENOMEM;
417 goto out;
418 }
419 }
420
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300421 *old_keycode = ir_update_mapping(rdev, rc_map, index, ke->keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700422
423out:
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300424 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700425 return retval;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300426}
427
428/**
David Härdemanb3074c02010-04-02 15:58:28 -0300429 * ir_setkeytable() - sets several entries in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300430 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300431 * @from: the struct rc_map to copy entries from
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300432 *
David Härdemanb3074c02010-04-02 15:58:28 -0300433 * This routine is used to handle table initialization.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500434 *
435 * return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300436 */
David Härdemand8b4b582010-10-29 16:08:23 -0300437static int ir_setkeytable(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300438 const struct rc_map *from)
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300439{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300440 struct rc_map *rc_map = &dev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700441 unsigned int i, index;
442 int rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300443
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300444 rc = ir_create_table(rc_map, from->name,
Sean Young6d741bf2017-08-07 16:20:58 -0400445 from->rc_proto, from->size);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700446 if (rc)
447 return rc;
448
David Härdemanb3074c02010-04-02 15:58:28 -0300449 for (i = 0; i < from->size; i++) {
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300450 index = ir_establish_scancode(dev, rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700451 from->scan[i].scancode, false);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300452 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700453 rc = -ENOMEM;
David Härdemanb3074c02010-04-02 15:58:28 -0300454 break;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700455 }
456
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300457 ir_update_mapping(dev, rc_map, index,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700458 from->scan[i].keycode);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300459 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700460
461 if (rc)
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300462 ir_free_table(rc_map);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700463
David Härdemanb3074c02010-04-02 15:58:28 -0300464 return rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300465}
466
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300467static int rc_map_cmp(const void *key, const void *elt)
468{
469 const unsigned int *scancode = key;
470 const struct rc_map_table *e = elt;
471
472 if (*scancode < e->scancode)
473 return -1;
474 else if (*scancode > e->scancode)
475 return 1;
476 return 0;
477}
478
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300479/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700480 * ir_lookup_by_scancode() - locate mapping by scancode
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300481 * @rc_map: the struct rc_map to search
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700482 * @scancode: scancode to look for in the table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700483 *
484 * This routine performs binary search in RC keykeymap table for
485 * given scancode.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500486 *
487 * return: index in the table, -1U if not found
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700488 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300489static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700490 unsigned int scancode)
491{
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300492 struct rc_map_table *res;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700493
Thomas Meyer8ca01d42017-09-08 13:33:36 -0300494 res = bsearch(&scancode, rc_map->scan, rc_map->len,
495 sizeof(struct rc_map_table), rc_map_cmp);
496 if (!res)
497 return -1U;
498 else
499 return res - rc_map->scan;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700500}
501
502/**
David Härdemanb3074c02010-04-02 15:58:28 -0300503 * ir_getkeycode() - get a keycode from the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300504 * @idev: the struct input_dev device descriptor
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500505 * @ke: Input keymap entry
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300506 *
507 * This routine is used to handle evdev EVIOCGKEY ioctl.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500508 *
509 * return: always returns zero.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300510 */
David Härdemand8b4b582010-10-29 16:08:23 -0300511static int ir_getkeycode(struct input_dev *idev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700512 struct input_keymap_entry *ke)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300513{
David Härdemand8b4b582010-10-29 16:08:23 -0300514 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300515 struct rc_map *rc_map = &rdev->rc_map;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300516 struct rc_map_table *entry;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700517 unsigned long flags;
518 unsigned int index;
519 unsigned int scancode;
520 int retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300521
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300522 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700523
524 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
525 index = ke->index;
526 } else {
527 retval = input_scancode_to_scalar(ke, &scancode);
528 if (retval)
529 goto out;
530
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300531 index = ir_lookup_by_scancode(rc_map, scancode);
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300532 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700533
Dmitry Torokhov54e74b82011-01-28 23:33:29 -0800534 if (index < rc_map->len) {
535 entry = &rc_map->scan[index];
536
537 ke->index = index;
538 ke->keycode = entry->keycode;
539 ke->len = sizeof(entry->scancode);
540 memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
541
542 } else if (!(ke->flags & INPUT_KEYMAP_BY_INDEX)) {
543 /*
544 * We do not really know the valid range of scancodes
545 * so let's respond with KEY_RESERVED to anything we
546 * do not have mapping for [yet].
547 */
548 ke->index = index;
549 ke->keycode = KEY_RESERVED;
550 } else {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700551 retval = -EINVAL;
552 goto out;
553 }
554
Dmitry Torokhov47c5ba52010-10-31 15:18:42 -0700555 retval = 0;
556
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700557out:
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300558 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700559 return retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300560}
561
562/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300563 * rc_g_keycode_from_table() - gets the keycode that corresponds to a scancode
David Härdemand8b4b582010-10-29 16:08:23 -0300564 * @dev: the struct rc_dev descriptor of the device
565 * @scancode: the scancode to look for
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300566 *
David Härdemand8b4b582010-10-29 16:08:23 -0300567 * This routine is used by drivers which need to convert a scancode to a
568 * keycode. Normally it should not be used since drivers should have no
569 * interest in keycodes.
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500570 *
571 * return: the corresponding keycode, or KEY_RESERVED
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300572 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300573u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300574{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300575 struct rc_map *rc_map = &dev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700576 unsigned int keycode;
577 unsigned int index;
578 unsigned long flags;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300579
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300580 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700581
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300582 index = ir_lookup_by_scancode(rc_map, scancode);
583 keycode = index < rc_map->len ?
584 rc_map->scan[index].keycode : KEY_RESERVED;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700585
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300586 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700587
Mauro Carvalho Chehab35438942010-04-03 16:53:16 -0300588 if (keycode != KEY_RESERVED)
589 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
Sean Young518f4b22017-07-01 12:13:19 -0400590 dev->device_name, scancode, keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700591
David Härdemanb3074c02010-04-02 15:58:28 -0300592 return keycode;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300593}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300594EXPORT_SYMBOL_GPL(rc_g_keycode_from_table);
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300595
596/**
David Härdeman62c65032010-10-29 16:08:07 -0300597 * ir_do_keyup() - internal function to signal the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300598 * @dev: the struct rc_dev descriptor of the device
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300599 * @sync: whether or not to call input_sync
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300600 *
David Härdeman62c65032010-10-29 16:08:07 -0300601 * This function is used internally to release a keypress, it must be
602 * called with keylock held.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300603 */
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300604static void ir_do_keyup(struct rc_dev *dev, bool sync)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300605{
David Härdemand8b4b582010-10-29 16:08:23 -0300606 if (!dev->keypressed)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300607 return;
608
David Härdemand8b4b582010-10-29 16:08:23 -0300609 IR_dprintk(1, "keyup key 0x%04x\n", dev->last_keycode);
610 input_report_key(dev->input_dev, dev->last_keycode, 0);
Sean Young153a60b2013-07-30 19:00:01 -0300611 led_trigger_event(led_feedback, LED_OFF);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300612 if (sync)
613 input_sync(dev->input_dev);
David Härdemand8b4b582010-10-29 16:08:23 -0300614 dev->keypressed = false;
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300615}
David Härdeman62c65032010-10-29 16:08:07 -0300616
617/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300618 * rc_keyup() - signals the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300619 * @dev: the struct rc_dev descriptor of the device
David Härdeman62c65032010-10-29 16:08:07 -0300620 *
621 * This routine is used to signal that a key has been released on the
622 * remote control.
623 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300624void rc_keyup(struct rc_dev *dev)
David Härdeman62c65032010-10-29 16:08:07 -0300625{
626 unsigned long flags;
David Härdeman62c65032010-10-29 16:08:07 -0300627
David Härdemand8b4b582010-10-29 16:08:23 -0300628 spin_lock_irqsave(&dev->keylock, flags);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300629 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300630 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300631}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300632EXPORT_SYMBOL_GPL(rc_keyup);
David Härdemana374fef2010-04-02 15:58:29 -0300633
634/**
635 * ir_timer_keyup() - generates a keyup event after a timeout
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500636 *
637 * @t: a pointer to the struct timer_list
David Härdemana374fef2010-04-02 15:58:29 -0300638 *
639 * This routine will generate a keyup event some time after a keydown event
640 * is generated when no further activity has been detected.
641 */
Kees Cookb17ec782017-10-24 11:23:14 -0400642static void ir_timer_keyup(struct timer_list *t)
David Härdemana374fef2010-04-02 15:58:29 -0300643{
Kees Cookb17ec782017-10-24 11:23:14 -0400644 struct rc_dev *dev = from_timer(dev, t, timer_keyup);
David Härdemana374fef2010-04-02 15:58:29 -0300645 unsigned long flags;
646
647 /*
648 * ir->keyup_jiffies is used to prevent a race condition if a
649 * hardware interrupt occurs at this point and the keyup timer
650 * event is moved further into the future as a result.
651 *
652 * The timer will then be reactivated and this function called
653 * again in the future. We need to exit gracefully in that case
654 * to allow the input subsystem to do its auto-repeat magic or
655 * a keyup event might follow immediately after the keydown.
656 */
David Härdemand8b4b582010-10-29 16:08:23 -0300657 spin_lock_irqsave(&dev->keylock, flags);
658 if (time_is_before_eq_jiffies(dev->keyup_jiffies))
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300659 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300660 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300661}
662
663/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300664 * rc_repeat() - signals that a key is still pressed
David Härdemand8b4b582010-10-29 16:08:23 -0300665 * @dev: the struct rc_dev descriptor of the device
David Härdemana374fef2010-04-02 15:58:29 -0300666 *
667 * This routine is used by IR decoders when a repeat message which does
668 * not include the necessary bits to reproduce the scancode has been
669 * received.
670 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300671void rc_repeat(struct rc_dev *dev)
David Härdemana374fef2010-04-02 15:58:29 -0300672{
673 unsigned long flags;
Sean Youngd57ea872017-08-09 13:19:16 -0400674 unsigned int timeout = protocols[dev->last_protocol].repeat_period;
David Härdemana374fef2010-04-02 15:58:29 -0300675
David Härdemand8b4b582010-10-29 16:08:23 -0300676 spin_lock_irqsave(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300677
David Härdemand8b4b582010-10-29 16:08:23 -0300678 if (!dev->keypressed)
David Härdemana374fef2010-04-02 15:58:29 -0300679 goto out;
680
David Härdeman265a2982017-06-22 15:23:54 -0400681 input_event(dev->input_dev, EV_MSC, MSC_SCAN, dev->last_scancode);
682 input_sync(dev->input_dev);
683
Sean Youngd57ea872017-08-09 13:19:16 -0400684 dev->keyup_jiffies = jiffies + msecs_to_jiffies(timeout);
David Härdemand8b4b582010-10-29 16:08:23 -0300685 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
David Härdemana374fef2010-04-02 15:58:29 -0300686
687out:
David Härdemand8b4b582010-10-29 16:08:23 -0300688 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300689}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300690EXPORT_SYMBOL_GPL(rc_repeat);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300691
692/**
David Härdeman62c65032010-10-29 16:08:07 -0300693 * ir_do_keydown() - internal function to process a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300694 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300695 * @protocol: the protocol of the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300696 * @scancode: the scancode of the keypress
697 * @keycode: the keycode of the keypress
698 * @toggle: the toggle value of the keypress
699 *
700 * This function is used internally to register a keypress, it must be
701 * called with keylock held.
702 */
Sean Young6d741bf2017-08-07 16:20:58 -0400703static void ir_do_keydown(struct rc_dev *dev, enum rc_proto protocol,
David Härdeman120703f2014-04-03 20:31:30 -0300704 u32 scancode, u32 keycode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300705{
David Härdeman99b0f3c2014-04-04 19:06:06 -0300706 bool new_event = (!dev->keypressed ||
David Härdeman120703f2014-04-03 20:31:30 -0300707 dev->last_protocol != protocol ||
David Härdeman99b0f3c2014-04-04 19:06:06 -0300708 dev->last_scancode != scancode ||
David Härdeman120703f2014-04-03 20:31:30 -0300709 dev->last_toggle != toggle);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300710
711 if (new_event && dev->keypressed)
712 ir_do_keyup(dev, false);
713
David Härdemand8b4b582010-10-29 16:08:23 -0300714 input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300715
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300716 if (new_event && keycode != KEY_RESERVED) {
717 /* Register a keypress */
718 dev->keypressed = true;
David Härdeman120703f2014-04-03 20:31:30 -0300719 dev->last_protocol = protocol;
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300720 dev->last_scancode = scancode;
721 dev->last_toggle = toggle;
722 dev->last_keycode = keycode;
David Härdeman62c65032010-10-29 16:08:07 -0300723
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200724 IR_dprintk(1, "%s: key down event, key 0x%04x, protocol 0x%04x, scancode 0x%08x\n",
Sean Young518f4b22017-07-01 12:13:19 -0400725 dev->device_name, keycode, protocol, scancode);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300726 input_report_key(dev->input_dev, keycode, 1);
James Hogan70a2f912014-01-16 19:56:22 -0300727
728 led_trigger_event(led_feedback, LED_FULL);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300729 }
David Härdeman62c65032010-10-29 16:08:07 -0300730
David Härdemand8b4b582010-10-29 16:08:23 -0300731 input_sync(dev->input_dev);
David Härdeman62c65032010-10-29 16:08:07 -0300732}
733
734/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300735 * rc_keydown() - generates input event for a key press
David Härdemand8b4b582010-10-29 16:08:23 -0300736 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300737 * @protocol: the protocol for the keypress
738 * @scancode: the scancode for the keypress
David Härdemana374fef2010-04-02 15:58:29 -0300739 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
740 * support toggle values, this should be set to zero)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300741 *
David Härdemand8b4b582010-10-29 16:08:23 -0300742 * This routine is used to signal that a key has been pressed on the
743 * remote control.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300744 */
Sean Young6d741bf2017-08-07 16:20:58 -0400745void rc_keydown(struct rc_dev *dev, enum rc_proto protocol, u32 scancode,
746 u8 toggle)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300747{
David Härdemana374fef2010-04-02 15:58:29 -0300748 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300749 u32 keycode = rc_g_keycode_from_table(dev, scancode);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300750
David Härdemand8b4b582010-10-29 16:08:23 -0300751 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300752 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300753
David Härdemand8b4b582010-10-29 16:08:23 -0300754 if (dev->keypressed) {
Sean Youngd57ea872017-08-09 13:19:16 -0400755 dev->keyup_jiffies = jiffies +
756 msecs_to_jiffies(protocols[protocol].repeat_period);
David Härdemand8b4b582010-10-29 16:08:23 -0300757 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
David Härdeman62c65032010-10-29 16:08:07 -0300758 }
David Härdemand8b4b582010-10-29 16:08:23 -0300759 spin_unlock_irqrestore(&dev->keylock, flags);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300760}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300761EXPORT_SYMBOL_GPL(rc_keydown);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300762
David Härdeman62c65032010-10-29 16:08:07 -0300763/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300764 * rc_keydown_notimeout() - generates input event for a key press without
David Härdeman62c65032010-10-29 16:08:07 -0300765 * an automatic keyup event at a later time
David Härdemand8b4b582010-10-29 16:08:23 -0300766 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300767 * @protocol: the protocol for the keypress
768 * @scancode: the scancode for the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300769 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
770 * support toggle values, this should be set to zero)
771 *
David Härdemand8b4b582010-10-29 16:08:23 -0300772 * This routine is used to signal that a key has been pressed on the
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300773 * remote control. The driver must manually call rc_keyup() at a later stage.
David Härdeman62c65032010-10-29 16:08:07 -0300774 */
Sean Young6d741bf2017-08-07 16:20:58 -0400775void rc_keydown_notimeout(struct rc_dev *dev, enum rc_proto protocol,
David Härdeman120703f2014-04-03 20:31:30 -0300776 u32 scancode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300777{
778 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300779 u32 keycode = rc_g_keycode_from_table(dev, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300780
David Härdemand8b4b582010-10-29 16:08:23 -0300781 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300782 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
David Härdemand8b4b582010-10-29 16:08:23 -0300783 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300784}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300785EXPORT_SYMBOL_GPL(rc_keydown_notimeout);
David Härdeman62c65032010-10-29 16:08:07 -0300786
Sean Youngb590c0b2016-12-05 19:24:59 -0200787/**
788 * rc_validate_filter() - checks that the scancode and mask are valid and
789 * provides sensible defaults
James Hoganf423ccc2015-03-31 14:48:10 -0300790 * @dev: the struct rc_dev descriptor of the device
Sean Youngb590c0b2016-12-05 19:24:59 -0200791 * @filter: the scancode and mask
Mauro Carvalho Chehabf67f3662017-11-27 08:35:13 -0500792 *
793 * return: 0 or -EINVAL if the filter is not valid
Sean Youngb590c0b2016-12-05 19:24:59 -0200794 */
James Hoganf423ccc2015-03-31 14:48:10 -0300795static int rc_validate_filter(struct rc_dev *dev,
Sean Youngb590c0b2016-12-05 19:24:59 -0200796 struct rc_scancode_filter *filter)
797{
Sean Youngd57ea872017-08-09 13:19:16 -0400798 u32 mask, s = filter->data;
Sean Young6d741bf2017-08-07 16:20:58 -0400799 enum rc_proto protocol = dev->wakeup_protocol;
Sean Youngb590c0b2016-12-05 19:24:59 -0200800
Sean Youngd57ea872017-08-09 13:19:16 -0400801 if (protocol >= ARRAY_SIZE(protocols))
Sean Young2168b412017-08-07 09:21:29 -0400802 return -EINVAL;
803
Sean Youngd57ea872017-08-09 13:19:16 -0400804 mask = protocols[protocol].scancode_bits;
805
Sean Youngb590c0b2016-12-05 19:24:59 -0200806 switch (protocol) {
Sean Young6d741bf2017-08-07 16:20:58 -0400807 case RC_PROTO_NECX:
Sean Youngb590c0b2016-12-05 19:24:59 -0200808 if ((((s >> 16) ^ ~(s >> 8)) & 0xff) == 0)
809 return -EINVAL;
810 break;
Sean Young6d741bf2017-08-07 16:20:58 -0400811 case RC_PROTO_NEC32:
Sean Youngb590c0b2016-12-05 19:24:59 -0200812 if ((((s >> 24) ^ ~(s >> 16)) & 0xff) == 0)
813 return -EINVAL;
814 break;
Sean Young6d741bf2017-08-07 16:20:58 -0400815 case RC_PROTO_RC6_MCE:
Sean Youngb590c0b2016-12-05 19:24:59 -0200816 if ((s & 0xffff0000) != 0x800f0000)
817 return -EINVAL;
818 break;
Sean Young6d741bf2017-08-07 16:20:58 -0400819 case RC_PROTO_RC6_6A_32:
Sean Youngb590c0b2016-12-05 19:24:59 -0200820 if ((s & 0xffff0000) == 0x800f0000)
821 return -EINVAL;
822 break;
823 default:
824 break;
825 }
826
Sean Youngd57ea872017-08-09 13:19:16 -0400827 filter->data &= mask;
828 filter->mask &= mask;
Sean Youngb590c0b2016-12-05 19:24:59 -0200829
James Hoganf423ccc2015-03-31 14:48:10 -0300830 /*
831 * If we have to raw encode the IR for wakeup, we cannot have a mask
832 */
Sean Youngd57ea872017-08-09 13:19:16 -0400833 if (dev->encode_wakeup && filter->mask != 0 && filter->mask != mask)
James Hoganf423ccc2015-03-31 14:48:10 -0300834 return -EINVAL;
835
Sean Youngb590c0b2016-12-05 19:24:59 -0200836 return 0;
837}
838
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300839int rc_open(struct rc_dev *rdev)
840{
841 int rval = 0;
842
843 if (!rdev)
844 return -EINVAL;
845
846 mutex_lock(&rdev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -0200847
Juergen Lockf02dcdd2013-08-16 15:00:24 -0300848 if (!rdev->users++ && rdev->open != NULL)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300849 rval = rdev->open(rdev);
850
851 if (rval)
852 rdev->users--;
853
854 mutex_unlock(&rdev->lock);
855
856 return rval;
857}
858EXPORT_SYMBOL_GPL(rc_open);
859
David Härdemand8b4b582010-10-29 16:08:23 -0300860static int ir_open(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300861{
David Härdemand8b4b582010-10-29 16:08:23 -0300862 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300863
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300864 return rc_open(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300865}
866
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300867void rc_close(struct rc_dev *rdev)
868{
869 if (rdev) {
870 mutex_lock(&rdev->lock);
871
Mauro Carvalho Chehab81b7d142015-04-28 09:43:17 -0300872 if (!--rdev->users && rdev->close != NULL)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300873 rdev->close(rdev);
874
875 mutex_unlock(&rdev->lock);
876 }
877}
878EXPORT_SYMBOL_GPL(rc_close);
879
David Härdemand8b4b582010-10-29 16:08:23 -0300880static void ir_close(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);
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300883 rc_close(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300884}
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300885
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300886/* class for /sys/class/rc */
David Härdeman40fc5322013-03-06 16:52:10 -0300887static char *rc_devnode(struct device *dev, umode_t *mode)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300888{
889 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
890}
891
David Härdeman40fc5322013-03-06 16:52:10 -0300892static struct class rc_class = {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300893 .name = "rc",
David Härdeman40fc5322013-03-06 16:52:10 -0300894 .devnode = rc_devnode,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300895};
896
David Härdemanc003ab12012-10-11 19:11:54 -0300897/*
898 * These are the protocol textual descriptions that are
899 * used by the sysfs protocols file. Note that the order
900 * of the entries is relevant.
901 */
Heiner Kallweit53df8772015-11-16 17:52:17 -0200902static const struct {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300903 u64 type;
Heiner Kallweit53df8772015-11-16 17:52:17 -0200904 const char *name;
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200905 const char *module_name;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300906} proto_names[] = {
Sean Young6d741bf2017-08-07 16:20:58 -0400907 { RC_PROTO_BIT_NONE, "none", NULL },
908 { RC_PROTO_BIT_OTHER, "other", NULL },
909 { RC_PROTO_BIT_UNKNOWN, "unknown", NULL },
910 { RC_PROTO_BIT_RC5 |
911 RC_PROTO_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
912 { RC_PROTO_BIT_NEC |
913 RC_PROTO_BIT_NECX |
914 RC_PROTO_BIT_NEC32, "nec", "ir-nec-decoder" },
915 { RC_PROTO_BIT_RC6_0 |
916 RC_PROTO_BIT_RC6_6A_20 |
917 RC_PROTO_BIT_RC6_6A_24 |
918 RC_PROTO_BIT_RC6_6A_32 |
919 RC_PROTO_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
920 { RC_PROTO_BIT_JVC, "jvc", "ir-jvc-decoder" },
921 { RC_PROTO_BIT_SONY12 |
922 RC_PROTO_BIT_SONY15 |
923 RC_PROTO_BIT_SONY20, "sony", "ir-sony-decoder" },
924 { RC_PROTO_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
925 { RC_PROTO_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
926 { RC_PROTO_BIT_SHARP, "sharp", "ir-sharp-decoder" },
927 { RC_PROTO_BIT_MCIR2_KBD |
928 RC_PROTO_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" },
929 { RC_PROTO_BIT_XMP, "xmp", "ir-xmp-decoder" },
930 { RC_PROTO_BIT_CEC, "cec", NULL },
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300931};
932
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300933/**
James Hoganab88c662014-02-28 20:17:05 -0300934 * struct rc_filter_attribute - Device attribute relating to a filter type.
935 * @attr: Device attribute.
936 * @type: Filter type.
937 * @mask: false for filter value, true for filter mask.
938 */
939struct rc_filter_attribute {
940 struct device_attribute attr;
941 enum rc_filter_type type;
942 bool mask;
943};
944#define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
945
James Hoganab88c662014-02-28 20:17:05 -0300946#define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
947 struct rc_filter_attribute dev_attr_##_name = { \
948 .attr = __ATTR(_name, _mode, _show, _store), \
949 .type = (_type), \
950 .mask = (_mask), \
951 }
952
David Härdemandd6ff6a2015-07-22 17:55:24 -0300953static bool lirc_is_present(void)
954{
955#if defined(CONFIG_LIRC_MODULE)
956 struct module *lirc;
957
958 mutex_lock(&module_mutex);
959 lirc = find_module("lirc_dev");
960 mutex_unlock(&module_mutex);
961
962 return lirc ? true : false;
963#elif defined(CONFIG_LIRC)
964 return true;
965#else
966 return false;
967#endif
968}
969
James Hoganab88c662014-02-28 20:17:05 -0300970/**
Sean Young0751d332016-12-05 17:08:35 -0200971 * show_protocols() - shows the current IR protocol(s)
David Härdemand8b4b582010-10-29 16:08:23 -0300972 * @device: the device descriptor
David Härdemanda6e1622014-04-03 20:32:16 -0300973 * @mattr: the device attribute struct
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300974 * @buf: a pointer to the output buffer
975 *
976 * This routine is a callback routine for input read the IR protocol type(s).
Sean Young0751d332016-12-05 17:08:35 -0200977 * it is trigged by reading /sys/class/rc/rc?/protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300978 * It returns the protocol names of supported protocols.
979 * Enabled protocols are printed in brackets.
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300980 *
David Härdeman18726a32017-04-27 17:34:08 -0300981 * dev->lock is taken to guard against races between
982 * store_protocols and show_protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300983 */
David Härdemand8b4b582010-10-29 16:08:23 -0300984static ssize_t show_protocols(struct device *device,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300985 struct device_attribute *mattr, char *buf)
986{
David Härdemand8b4b582010-10-29 16:08:23 -0300987 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300988 u64 allowed, enabled;
989 char *tmp = buf;
990 int i;
991
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300992 mutex_lock(&dev->lock);
993
Sean Young0751d332016-12-05 17:08:35 -0200994 enabled = dev->enabled_protocols;
995 allowed = dev->allowed_protocols;
996 if (dev->raw && !allowed)
997 allowed = ir_raw_get_allowed_protocols();
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300998
David Härdemanda6e1622014-04-03 20:32:16 -0300999 mutex_unlock(&dev->lock);
1000
1001 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
1002 __func__, (long long)allowed, (long long)enabled);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001003
1004 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1005 if (allowed & enabled & proto_names[i].type)
1006 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
1007 else if (allowed & proto_names[i].type)
1008 tmp += sprintf(tmp, "%s ", proto_names[i].name);
David Härdemanc003ab12012-10-11 19:11:54 -03001009
1010 if (allowed & proto_names[i].type)
1011 allowed &= ~proto_names[i].type;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001012 }
1013
David Härdemandd6ff6a2015-07-22 17:55:24 -03001014 if (dev->driver_type == RC_DRIVER_IR_RAW && lirc_is_present())
David Härdeman275ddb42015-05-19 19:03:22 -03001015 tmp += sprintf(tmp, "[lirc] ");
1016
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001017 if (tmp != buf)
1018 tmp--;
1019 *tmp = '\n';
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001020
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001021 return tmp + 1 - buf;
1022}
1023
1024/**
David Härdemanda6e1622014-04-03 20:32:16 -03001025 * parse_protocol_change() - parses a protocol change request
1026 * @protocols: pointer to the bitmask of current protocols
1027 * @buf: pointer to the buffer with a list of changes
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001028 *
David Härdemanda6e1622014-04-03 20:32:16 -03001029 * Writing "+proto" will add a protocol to the protocol mask.
1030 * Writing "-proto" will remove a protocol from protocol mask.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001031 * Writing "proto" will enable only "proto".
1032 * Writing "none" will disable all protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001033 * Returns the number of changes performed or a negative error code.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001034 */
David Härdemanda6e1622014-04-03 20:32:16 -03001035static int parse_protocol_change(u64 *protocols, const char *buf)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001036{
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001037 const char *tmp;
David Härdemanda6e1622014-04-03 20:32:16 -03001038 unsigned count = 0;
1039 bool enable, disable;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001040 u64 mask;
David Härdemanda6e1622014-04-03 20:32:16 -03001041 int i;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001042
David Härdemanda6e1622014-04-03 20:32:16 -03001043 while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001044 if (!*tmp)
1045 break;
1046
1047 if (*tmp == '+') {
1048 enable = true;
1049 disable = false;
1050 tmp++;
1051 } else if (*tmp == '-') {
1052 enable = false;
1053 disable = true;
1054 tmp++;
1055 } else {
1056 enable = false;
1057 disable = false;
1058 }
1059
David Härdemanc003ab12012-10-11 19:11:54 -03001060 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1061 if (!strcasecmp(tmp, proto_names[i].name)) {
1062 mask = proto_names[i].type;
1063 break;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001064 }
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001065 }
1066
David Härdemanc003ab12012-10-11 19:11:54 -03001067 if (i == ARRAY_SIZE(proto_names)) {
David Härdeman275ddb42015-05-19 19:03:22 -03001068 if (!strcasecmp(tmp, "lirc"))
1069 mask = 0;
1070 else {
1071 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
1072 return -EINVAL;
1073 }
David Härdemanc003ab12012-10-11 19:11:54 -03001074 }
1075
1076 count++;
1077
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001078 if (enable)
David Härdemanda6e1622014-04-03 20:32:16 -03001079 *protocols |= mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001080 else if (disable)
David Härdemanda6e1622014-04-03 20:32:16 -03001081 *protocols &= ~mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001082 else
David Härdemanda6e1622014-04-03 20:32:16 -03001083 *protocols = mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001084 }
1085
1086 if (!count) {
1087 IR_dprintk(1, "Protocol not specified\n");
David Härdemanda6e1622014-04-03 20:32:16 -03001088 return -EINVAL;
1089 }
1090
1091 return count;
1092}
1093
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001094static void ir_raw_load_modules(u64 *protocols)
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001095{
1096 u64 available;
1097 int i, ret;
1098
1099 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
Sean Young6d741bf2017-08-07 16:20:58 -04001100 if (proto_names[i].type == RC_PROTO_BIT_NONE ||
1101 proto_names[i].type & (RC_PROTO_BIT_OTHER |
1102 RC_PROTO_BIT_UNKNOWN))
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001103 continue;
1104
1105 available = ir_raw_get_allowed_protocols();
1106 if (!(*protocols & proto_names[i].type & ~available))
1107 continue;
1108
1109 if (!proto_names[i].module_name) {
1110 pr_err("Can't enable IR protocol %s\n",
1111 proto_names[i].name);
1112 *protocols &= ~proto_names[i].type;
1113 continue;
1114 }
1115
1116 ret = request_module("%s", proto_names[i].module_name);
1117 if (ret < 0) {
1118 pr_err("Couldn't load IR protocol module %s\n",
1119 proto_names[i].module_name);
1120 *protocols &= ~proto_names[i].type;
1121 continue;
1122 }
1123 msleep(20);
1124 available = ir_raw_get_allowed_protocols();
1125 if (!(*protocols & proto_names[i].type & ~available))
1126 continue;
1127
Sean Young8caebcd2017-01-19 19:33:49 -02001128 pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001129 proto_names[i].module_name,
1130 proto_names[i].name);
1131 *protocols &= ~proto_names[i].type;
1132 }
1133}
1134
David Härdemanda6e1622014-04-03 20:32:16 -03001135/**
1136 * store_protocols() - changes the current/wakeup IR protocol(s)
1137 * @device: the device descriptor
1138 * @mattr: the device attribute struct
1139 * @buf: a pointer to the input buffer
1140 * @len: length of the input buffer
1141 *
1142 * This routine is for changing the IR protocol type.
1143 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
1144 * See parse_protocol_change() for the valid commands.
1145 * Returns @len on success or a negative error code.
1146 *
David Härdeman18726a32017-04-27 17:34:08 -03001147 * dev->lock is taken to guard against races between
1148 * store_protocols and show_protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001149 */
1150static ssize_t store_protocols(struct device *device,
1151 struct device_attribute *mattr,
1152 const char *buf, size_t len)
1153{
1154 struct rc_dev *dev = to_rc_dev(device);
David Härdemanda6e1622014-04-03 20:32:16 -03001155 u64 *current_protocols;
David Härdemanda6e1622014-04-03 20:32:16 -03001156 struct rc_scancode_filter *filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001157 u64 old_protocols, new_protocols;
1158 ssize_t rc;
1159
Sean Young0751d332016-12-05 17:08:35 -02001160 IR_dprintk(1, "Normal protocol change requested\n");
1161 current_protocols = &dev->enabled_protocols;
1162 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001163
Sean Young0751d332016-12-05 17:08:35 -02001164 if (!dev->change_protocol) {
David Härdemanda6e1622014-04-03 20:32:16 -03001165 IR_dprintk(1, "Protocol switching not supported\n");
1166 return -EINVAL;
1167 }
1168
1169 mutex_lock(&dev->lock);
1170
1171 old_protocols = *current_protocols;
1172 new_protocols = old_protocols;
1173 rc = parse_protocol_change(&new_protocols, buf);
1174 if (rc < 0)
1175 goto out;
1176
Sean Young0751d332016-12-05 17:08:35 -02001177 rc = dev->change_protocol(dev, &new_protocols);
David Härdemanda6e1622014-04-03 20:32:16 -03001178 if (rc < 0) {
1179 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
1180 (long long)new_protocols);
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001181 goto out;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001182 }
1183
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001184 if (dev->driver_type == RC_DRIVER_IR_RAW)
1185 ir_raw_load_modules(&new_protocols);
1186
James Hogan983c5bd2014-12-08 13:17:07 -03001187 if (new_protocols != old_protocols) {
1188 *current_protocols = new_protocols;
1189 IR_dprintk(1, "Protocols changed to 0x%llx\n",
1190 (long long)new_protocols);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001191 }
1192
James Hogan6bea25a2014-02-28 20:17:06 -03001193 /*
James Hogan983c5bd2014-12-08 13:17:07 -03001194 * If a protocol change was attempted the filter may need updating, even
1195 * if the actual protocol mask hasn't changed (since the driver may have
1196 * cleared the filter).
James Hogan6bea25a2014-02-28 20:17:06 -03001197 * Try setting the same filter with the new protocol (if any).
1198 * Fall back to clearing the filter.
1199 */
Sean Young0751d332016-12-05 17:08:35 -02001200 if (dev->s_filter && filter->mask) {
David Härdemanda6e1622014-04-03 20:32:16 -03001201 if (new_protocols)
Sean Young0751d332016-12-05 17:08:35 -02001202 rc = dev->s_filter(dev, filter);
David Härdemanda6e1622014-04-03 20:32:16 -03001203 else
1204 rc = -1;
David Härdeman23c843b2014-04-04 19:06:01 -03001205
David Härdemanda6e1622014-04-03 20:32:16 -03001206 if (rc < 0) {
1207 filter->data = 0;
1208 filter->mask = 0;
Sean Young0751d332016-12-05 17:08:35 -02001209 dev->s_filter(dev, filter);
James Hogan6bea25a2014-02-28 20:17:06 -03001210 }
James Hogan6bea25a2014-02-28 20:17:06 -03001211 }
1212
David Härdemanda6e1622014-04-03 20:32:16 -03001213 rc = len;
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001214
1215out:
1216 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001217 return rc;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001218}
1219
James Hogan00942d12014-01-17 10:58:49 -03001220/**
James Hogan00942d12014-01-17 10:58:49 -03001221 * show_filter() - shows the current scancode filter value or mask
1222 * @device: the device descriptor
1223 * @attr: the device attribute struct
1224 * @buf: a pointer to the output buffer
1225 *
1226 * This routine is a callback routine to read a scancode filter value or mask.
1227 * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
1228 * It prints the current scancode filter value or mask of the appropriate filter
1229 * type in hexadecimal into @buf and returns the size of the buffer.
1230 *
1231 * Bits of the filter value corresponding to set bits in the filter mask are
1232 * compared against input scancodes and non-matching scancodes are discarded.
1233 *
David Härdeman18726a32017-04-27 17:34:08 -03001234 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001235 * store_filter and show_filter.
1236 */
1237static ssize_t show_filter(struct device *device,
1238 struct device_attribute *attr,
1239 char *buf)
1240{
1241 struct rc_dev *dev = to_rc_dev(device);
1242 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001243 struct rc_scancode_filter *filter;
James Hogan00942d12014-01-17 10:58:49 -03001244 u32 val;
1245
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001246 mutex_lock(&dev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001247
David Härdemanda6e1622014-04-03 20:32:16 -03001248 if (fattr->type == RC_FILTER_NORMAL)
David Härdemanc5540fb2014-04-03 20:32:21 -03001249 filter = &dev->scancode_filter;
James Hogan00942d12014-01-17 10:58:49 -03001250 else
David Härdemanc5540fb2014-04-03 20:32:21 -03001251 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001252
David Härdemanda6e1622014-04-03 20:32:16 -03001253 if (fattr->mask)
1254 val = filter->mask;
1255 else
1256 val = filter->data;
James Hogan00942d12014-01-17 10:58:49 -03001257 mutex_unlock(&dev->lock);
1258
1259 return sprintf(buf, "%#x\n", val);
1260}
1261
1262/**
1263 * store_filter() - changes the scancode filter value
1264 * @device: the device descriptor
1265 * @attr: the device attribute struct
1266 * @buf: a pointer to the input buffer
1267 * @len: length of the input buffer
1268 *
1269 * This routine is for changing a scancode filter value or mask.
1270 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
1271 * Returns -EINVAL if an invalid filter value for the current protocol was
1272 * specified or if scancode filtering is not supported by the driver, otherwise
1273 * returns @len.
1274 *
1275 * Bits of the filter value corresponding to set bits in the filter mask are
1276 * compared against input scancodes and non-matching scancodes are discarded.
1277 *
David Härdeman18726a32017-04-27 17:34:08 -03001278 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001279 * store_filter and show_filter.
1280 */
1281static ssize_t store_filter(struct device *device,
1282 struct device_attribute *attr,
David Härdemanda6e1622014-04-03 20:32:16 -03001283 const char *buf, size_t len)
James Hogan00942d12014-01-17 10:58:49 -03001284{
1285 struct rc_dev *dev = to_rc_dev(device);
1286 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001287 struct rc_scancode_filter new_filter, *filter;
James Hogan00942d12014-01-17 10:58:49 -03001288 int ret;
1289 unsigned long val;
David Härdeman23c843b2014-04-04 19:06:01 -03001290 int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
James Hogan00942d12014-01-17 10:58:49 -03001291
James Hogan00942d12014-01-17 10:58:49 -03001292 ret = kstrtoul(buf, 0, &val);
1293 if (ret < 0)
1294 return ret;
1295
David Härdemanda6e1622014-04-03 20:32:16 -03001296 if (fattr->type == RC_FILTER_NORMAL) {
1297 set_filter = dev->s_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001298 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001299 } else {
1300 set_filter = dev->s_wakeup_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001301 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001302 }
1303
David Härdeman99b0f3c2014-04-04 19:06:06 -03001304 if (!set_filter)
1305 return -EINVAL;
James Hogan00942d12014-01-17 10:58:49 -03001306
1307 mutex_lock(&dev->lock);
1308
David Härdemanda6e1622014-04-03 20:32:16 -03001309 new_filter = *filter;
James Hogan00942d12014-01-17 10:58:49 -03001310 if (fattr->mask)
David Härdemanda6e1622014-04-03 20:32:16 -03001311 new_filter.mask = val;
James Hogan00942d12014-01-17 10:58:49 -03001312 else
David Härdemanda6e1622014-04-03 20:32:16 -03001313 new_filter.data = val;
David Härdeman23c843b2014-04-04 19:06:01 -03001314
Sean Young0751d332016-12-05 17:08:35 -02001315 if (fattr->type == RC_FILTER_WAKEUP) {
Sean Youngb590c0b2016-12-05 19:24:59 -02001316 /*
1317 * Refuse to set a filter unless a protocol is enabled
1318 * and the filter is valid for that protocol
1319 */
Sean Young6d741bf2017-08-07 16:20:58 -04001320 if (dev->wakeup_protocol != RC_PROTO_UNKNOWN)
James Hoganf423ccc2015-03-31 14:48:10 -03001321 ret = rc_validate_filter(dev, &new_filter);
Sean Youngb590c0b2016-12-05 19:24:59 -02001322 else
Sean Young0751d332016-12-05 17:08:35 -02001323 ret = -EINVAL;
Sean Youngb590c0b2016-12-05 19:24:59 -02001324
1325 if (ret != 0)
Sean Young0751d332016-12-05 17:08:35 -02001326 goto unlock;
Sean Young0751d332016-12-05 17:08:35 -02001327 }
1328
1329 if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
1330 val) {
James Hogan6bea25a2014-02-28 20:17:06 -03001331 /* refuse to set a filter unless a protocol is enabled */
1332 ret = -EINVAL;
1333 goto unlock;
1334 }
David Härdeman23c843b2014-04-04 19:06:01 -03001335
David Härdemanda6e1622014-04-03 20:32:16 -03001336 ret = set_filter(dev, &new_filter);
David Härdeman99b0f3c2014-04-04 19:06:06 -03001337 if (ret < 0)
1338 goto unlock;
James Hogan00942d12014-01-17 10:58:49 -03001339
David Härdemanda6e1622014-04-03 20:32:16 -03001340 *filter = new_filter;
James Hogan00942d12014-01-17 10:58:49 -03001341
1342unlock:
1343 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001344 return (ret < 0) ? ret : len;
James Hogan00942d12014-01-17 10:58:49 -03001345}
1346
Sean Young0751d332016-12-05 17:08:35 -02001347/**
1348 * show_wakeup_protocols() - shows the wakeup IR protocol
1349 * @device: the device descriptor
1350 * @mattr: the device attribute struct
1351 * @buf: a pointer to the output buffer
1352 *
1353 * This routine is a callback routine for input read the IR protocol type(s).
1354 * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols.
1355 * It returns the protocol names of supported protocols.
1356 * The enabled protocols are printed in brackets.
1357 *
David Härdeman18726a32017-04-27 17:34:08 -03001358 * dev->lock is taken to guard against races between
1359 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001360 */
1361static ssize_t show_wakeup_protocols(struct device *device,
1362 struct device_attribute *mattr,
1363 char *buf)
1364{
1365 struct rc_dev *dev = to_rc_dev(device);
1366 u64 allowed;
Sean Young6d741bf2017-08-07 16:20:58 -04001367 enum rc_proto enabled;
Sean Young0751d332016-12-05 17:08:35 -02001368 char *tmp = buf;
1369 int i;
1370
Sean Young0751d332016-12-05 17:08:35 -02001371 mutex_lock(&dev->lock);
1372
1373 allowed = dev->allowed_wakeup_protocols;
1374 enabled = dev->wakeup_protocol;
1375
1376 mutex_unlock(&dev->lock);
1377
1378 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - %d\n",
1379 __func__, (long long)allowed, enabled);
1380
Sean Youngd57ea872017-08-09 13:19:16 -04001381 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001382 if (allowed & (1ULL << i)) {
1383 if (i == enabled)
Sean Youngd57ea872017-08-09 13:19:16 -04001384 tmp += sprintf(tmp, "[%s] ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001385 else
Sean Youngd57ea872017-08-09 13:19:16 -04001386 tmp += sprintf(tmp, "%s ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001387 }
1388 }
1389
1390 if (tmp != buf)
1391 tmp--;
1392 *tmp = '\n';
1393
1394 return tmp + 1 - buf;
1395}
1396
1397/**
1398 * store_wakeup_protocols() - changes the wakeup IR protocol(s)
1399 * @device: the device descriptor
1400 * @mattr: the device attribute struct
1401 * @buf: a pointer to the input buffer
1402 * @len: length of the input buffer
1403 *
1404 * This routine is for changing the IR protocol type.
1405 * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols.
1406 * Returns @len on success or a negative error code.
1407 *
David Härdeman18726a32017-04-27 17:34:08 -03001408 * dev->lock is taken to guard against races between
1409 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001410 */
1411static ssize_t store_wakeup_protocols(struct device *device,
1412 struct device_attribute *mattr,
1413 const char *buf, size_t len)
1414{
1415 struct rc_dev *dev = to_rc_dev(device);
Sean Young6d741bf2017-08-07 16:20:58 -04001416 enum rc_proto protocol;
Sean Young0751d332016-12-05 17:08:35 -02001417 ssize_t rc;
1418 u64 allowed;
1419 int i;
1420
Sean Young0751d332016-12-05 17:08:35 -02001421 mutex_lock(&dev->lock);
1422
1423 allowed = dev->allowed_wakeup_protocols;
1424
1425 if (sysfs_streq(buf, "none")) {
Sean Young6d741bf2017-08-07 16:20:58 -04001426 protocol = RC_PROTO_UNKNOWN;
Sean Young0751d332016-12-05 17:08:35 -02001427 } else {
Sean Youngd57ea872017-08-09 13:19:16 -04001428 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001429 if ((allowed & (1ULL << i)) &&
Sean Youngd57ea872017-08-09 13:19:16 -04001430 sysfs_streq(buf, protocols[i].name)) {
Sean Young0751d332016-12-05 17:08:35 -02001431 protocol = i;
1432 break;
1433 }
1434 }
1435
Sean Youngd57ea872017-08-09 13:19:16 -04001436 if (i == ARRAY_SIZE(protocols)) {
Sean Young0751d332016-12-05 17:08:35 -02001437 rc = -EINVAL;
1438 goto out;
1439 }
James Hoganf423ccc2015-03-31 14:48:10 -03001440
1441 if (dev->encode_wakeup) {
1442 u64 mask = 1ULL << protocol;
1443
1444 ir_raw_load_modules(&mask);
1445 if (!mask) {
1446 rc = -EINVAL;
1447 goto out;
1448 }
1449 }
Sean Young0751d332016-12-05 17:08:35 -02001450 }
1451
1452 if (dev->wakeup_protocol != protocol) {
1453 dev->wakeup_protocol = protocol;
1454 IR_dprintk(1, "Wakeup protocol changed to %d\n", protocol);
1455
Sean Young6d741bf2017-08-07 16:20:58 -04001456 if (protocol == RC_PROTO_RC6_MCE)
Sean Young0751d332016-12-05 17:08:35 -02001457 dev->scancode_wakeup_filter.data = 0x800f0000;
1458 else
1459 dev->scancode_wakeup_filter.data = 0;
1460 dev->scancode_wakeup_filter.mask = 0;
1461
1462 rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
1463 if (rc == 0)
1464 rc = len;
1465 } else {
1466 rc = len;
1467 }
1468
1469out:
1470 mutex_unlock(&dev->lock);
1471 return rc;
1472}
1473
David Härdemand8b4b582010-10-29 16:08:23 -03001474static void rc_dev_release(struct device *device)
1475{
Max Kellermann47cae1e2016-03-21 08:33:05 -03001476 struct rc_dev *dev = to_rc_dev(device);
1477
1478 kfree(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001479}
1480
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001481#define ADD_HOTPLUG_VAR(fmt, val...) \
1482 do { \
1483 int err = add_uevent_var(env, fmt, val); \
1484 if (err) \
1485 return err; \
1486 } while (0)
1487
1488static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1489{
David Härdemand8b4b582010-10-29 16:08:23 -03001490 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001491
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001492 if (dev->rc_map.name)
1493 ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
David Härdemand8b4b582010-10-29 16:08:23 -03001494 if (dev->driver_name)
1495 ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
Sean Youngb9f407e2017-09-01 11:34:23 -03001496 if (dev->device_name)
1497 ADD_HOTPLUG_VAR("DEV_NAME=%s", dev->device_name);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001498
1499 return 0;
1500}
1501
1502/*
1503 * Static device attribute struct with the sysfs attributes for IR's
1504 */
Sean Young6d75db32017-09-01 11:30:50 -03001505static struct device_attribute dev_attr_ro_protocols =
1506__ATTR(protocols, 0444, show_protocols, NULL);
1507static struct device_attribute dev_attr_rw_protocols =
1508__ATTR(protocols, 0644, show_protocols, store_protocols);
Sean Young0751d332016-12-05 17:08:35 -02001509static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
1510 store_wakeup_protocols);
James Hogan00942d12014-01-17 10:58:49 -03001511static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
1512 show_filter, store_filter, RC_FILTER_NORMAL, false);
1513static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
1514 show_filter, store_filter, RC_FILTER_NORMAL, true);
1515static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
1516 show_filter, store_filter, RC_FILTER_WAKEUP, false);
1517static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
1518 show_filter, store_filter, RC_FILTER_WAKEUP, true);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001519
Sean Young6d75db32017-09-01 11:30:50 -03001520static struct attribute *rc_dev_rw_protocol_attrs[] = {
1521 &dev_attr_rw_protocols.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001522 NULL,
1523};
1524
Sean Young6d75db32017-09-01 11:30:50 -03001525static const struct attribute_group rc_dev_rw_protocol_attr_grp = {
1526 .attrs = rc_dev_rw_protocol_attrs,
1527};
1528
1529static struct attribute *rc_dev_ro_protocol_attrs[] = {
1530 &dev_attr_ro_protocols.attr,
1531 NULL,
1532};
1533
1534static const struct attribute_group rc_dev_ro_protocol_attr_grp = {
1535 .attrs = rc_dev_ro_protocol_attrs,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001536};
1537
David Härdeman99b0f3c2014-04-04 19:06:06 -03001538static struct attribute *rc_dev_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001539 &dev_attr_filter.attr.attr,
1540 &dev_attr_filter_mask.attr.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001541 NULL,
1542};
1543
Arvind Yadavdb681022017-07-07 04:23:54 -04001544static const struct attribute_group rc_dev_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001545 .attrs = rc_dev_filter_attrs,
1546};
1547
1548static struct attribute *rc_dev_wakeup_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001549 &dev_attr_wakeup_filter.attr.attr,
1550 &dev_attr_wakeup_filter_mask.attr.attr,
Sean Young0751d332016-12-05 17:08:35 -02001551 &dev_attr_wakeup_protocols.attr,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001552 NULL,
1553};
1554
Arvind Yadavdb681022017-07-07 04:23:54 -04001555static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001556 .attrs = rc_dev_wakeup_filter_attrs,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001557};
1558
Bhumika Goyalf03f02f2017-08-19 05:22:15 -03001559static const struct device_type rc_dev_type = {
David Härdemand8b4b582010-10-29 16:08:23 -03001560 .release = rc_dev_release,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001561 .uevent = rc_dev_uevent,
1562};
1563
Andi Shyti0f7499f2016-12-16 06:50:58 -02001564struct rc_dev *rc_allocate_device(enum rc_driver_type type)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001565{
David Härdemand8b4b582010-10-29 16:08:23 -03001566 struct rc_dev *dev;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001567
David Härdemand8b4b582010-10-29 16:08:23 -03001568 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1569 if (!dev)
1570 return NULL;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001571
Andi Shytid34aee12016-12-16 04:12:15 -02001572 if (type != RC_DRIVER_IR_RAW_TX) {
1573 dev->input_dev = input_allocate_device();
1574 if (!dev->input_dev) {
1575 kfree(dev);
1576 return NULL;
1577 }
1578
1579 dev->input_dev->getkeycode = ir_getkeycode;
1580 dev->input_dev->setkeycode = ir_setkeycode;
1581 input_set_drvdata(dev->input_dev, dev);
1582
Kees Cookb17ec782017-10-24 11:23:14 -04001583 timer_setup(&dev->timer_keyup, ir_timer_keyup, 0);
Andi Shytid34aee12016-12-16 04:12:15 -02001584
1585 spin_lock_init(&dev->rc_map.lock);
1586 spin_lock_init(&dev->keylock);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001587 }
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001588 mutex_init(&dev->lock);
David Härdemand8b4b582010-10-29 16:08:23 -03001589
1590 dev->dev.type = &rc_dev_type;
David Härdeman40fc5322013-03-06 16:52:10 -03001591 dev->dev.class = &rc_class;
David Härdemand8b4b582010-10-29 16:08:23 -03001592 device_initialize(&dev->dev);
1593
Andi Shyti0f7499f2016-12-16 06:50:58 -02001594 dev->driver_type = type;
1595
David Härdemand8b4b582010-10-29 16:08:23 -03001596 __module_get(THIS_MODULE);
1597 return dev;
1598}
1599EXPORT_SYMBOL_GPL(rc_allocate_device);
1600
1601void rc_free_device(struct rc_dev *dev)
1602{
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001603 if (!dev)
1604 return;
1605
Markus Elfring3dd94f02014-11-20 09:01:32 -03001606 input_free_device(dev->input_dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001607
1608 put_device(&dev->dev);
1609
Max Kellermann47cae1e2016-03-21 08:33:05 -03001610 /* kfree(dev) will be called by the callback function
1611 rc_dev_release() */
1612
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001613 module_put(THIS_MODULE);
David Härdemand8b4b582010-10-29 16:08:23 -03001614}
1615EXPORT_SYMBOL_GPL(rc_free_device);
1616
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001617static void devm_rc_alloc_release(struct device *dev, void *res)
1618{
1619 rc_free_device(*(struct rc_dev **)res);
1620}
1621
Andi Shyti0f7499f2016-12-16 06:50:58 -02001622struct rc_dev *devm_rc_allocate_device(struct device *dev,
1623 enum rc_driver_type type)
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001624{
1625 struct rc_dev **dr, *rc;
1626
1627 dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
1628 if (!dr)
1629 return NULL;
1630
Andi Shyti0f7499f2016-12-16 06:50:58 -02001631 rc = rc_allocate_device(type);
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001632 if (!rc) {
1633 devres_free(dr);
1634 return NULL;
1635 }
1636
1637 rc->dev.parent = dev;
1638 rc->managed_alloc = true;
1639 *dr = rc;
1640 devres_add(dev, dr);
1641
1642 return rc;
1643}
1644EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
1645
David Härdemanf56928a2017-05-03 07:04:00 -03001646static int rc_prepare_rx_device(struct rc_dev *dev)
David Härdemand8b4b582010-10-29 16:08:23 -03001647{
David Härdemanfcb13092015-05-19 19:03:17 -03001648 int rc;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001649 struct rc_map *rc_map;
Sean Young6d741bf2017-08-07 16:20:58 -04001650 u64 rc_proto;
David Härdemand8b4b582010-10-29 16:08:23 -03001651
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001652 if (!dev->map_name)
David Härdemand8b4b582010-10-29 16:08:23 -03001653 return -EINVAL;
1654
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001655 rc_map = rc_map_get(dev->map_name);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001656 if (!rc_map)
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001657 rc_map = rc_map_get(RC_MAP_EMPTY);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001658 if (!rc_map || !rc_map->scan || rc_map->size == 0)
David Härdemand8b4b582010-10-29 16:08:23 -03001659 return -EINVAL;
1660
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001661 rc = ir_setkeytable(dev, rc_map);
1662 if (rc)
1663 return rc;
1664
Sean Young6d741bf2017-08-07 16:20:58 -04001665 rc_proto = BIT_ULL(rc_map->rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001666
Sean Young831c4c82017-09-01 09:55:59 -03001667 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1668 dev->enabled_protocols = dev->allowed_protocols;
1669
Sean Young41380862017-02-22 18:48:01 -03001670 if (dev->change_protocol) {
Sean Young6d741bf2017-08-07 16:20:58 -04001671 rc = dev->change_protocol(dev, &rc_proto);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001672 if (rc < 0)
1673 goto out_table;
Sean Young6d741bf2017-08-07 16:20:58 -04001674 dev->enabled_protocols = rc_proto;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001675 }
1676
Sean Young41380862017-02-22 18:48:01 -03001677 if (dev->driver_type == RC_DRIVER_IR_RAW)
Sean Young6d741bf2017-08-07 16:20:58 -04001678 ir_raw_load_modules(&rc_proto);
Sean Young41380862017-02-22 18:48:01 -03001679
David Härdemand8b4b582010-10-29 16:08:23 -03001680 set_bit(EV_KEY, dev->input_dev->evbit);
1681 set_bit(EV_REP, dev->input_dev->evbit);
1682 set_bit(EV_MSC, dev->input_dev->evbit);
1683 set_bit(MSC_SCAN, dev->input_dev->mscbit);
1684 if (dev->open)
1685 dev->input_dev->open = ir_open;
1686 if (dev->close)
1687 dev->input_dev->close = ir_close;
1688
David Härdemanb2aceb72017-04-27 17:33:58 -03001689 dev->input_dev->dev.parent = &dev->dev;
1690 memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
1691 dev->input_dev->phys = dev->input_phys;
Sean Young518f4b22017-07-01 12:13:19 -04001692 dev->input_dev->name = dev->device_name;
David Härdemanb2aceb72017-04-27 17:33:58 -03001693
David Härdemanf56928a2017-05-03 07:04:00 -03001694 return 0;
1695
1696out_table:
1697 ir_free_table(&dev->rc_map);
1698
1699 return rc;
1700}
1701
1702static int rc_setup_rx_device(struct rc_dev *dev)
1703{
1704 int rc;
1705
David Härdemanb2aceb72017-04-27 17:33:58 -03001706 /* rc_open will be called here */
1707 rc = input_register_device(dev->input_dev);
1708 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001709 return rc;
David Härdemanb2aceb72017-04-27 17:33:58 -03001710
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001711 /*
1712 * Default delay of 250ms is too short for some protocols, especially
1713 * since the timeout is currently set to 250ms. Increase it to 500ms,
1714 * to avoid wrong repetition of the keycodes. Note that this must be
1715 * set after the call to input_register_device().
1716 */
1717 dev->input_dev->rep[REP_DELAY] = 500;
1718
1719 /*
1720 * As a repeat event on protocols like RC-5 and NEC take as long as
1721 * 110/114ms, using 33ms as a repeat period is not the right thing
1722 * to do.
1723 */
1724 dev->input_dev->rep[REP_PERIOD] = 125;
1725
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001726 return 0;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001727}
1728
1729static void rc_free_rx_device(struct rc_dev *dev)
1730{
David Härdemanf56928a2017-05-03 07:04:00 -03001731 if (!dev)
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001732 return;
1733
David Härdemanf56928a2017-05-03 07:04:00 -03001734 if (dev->input_dev) {
1735 input_unregister_device(dev->input_dev);
1736 dev->input_dev = NULL;
1737 }
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001738
David Härdemanf56928a2017-05-03 07:04:00 -03001739 ir_free_table(&dev->rc_map);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001740}
1741
1742int rc_register_device(struct rc_dev *dev)
1743{
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001744 const char *path;
1745 int attr = 0;
1746 int minor;
1747 int rc;
1748
1749 if (!dev)
1750 return -EINVAL;
1751
David Härdemanfcb13092015-05-19 19:03:17 -03001752 minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
1753 if (minor < 0)
1754 return minor;
1755
1756 dev->minor = minor;
1757 dev_set_name(&dev->dev, "rc%u", dev->minor);
1758 dev_set_drvdata(&dev->dev, dev);
Mauro Carvalho Chehab587d1b02014-01-14 16:27:55 -03001759
David Härdeman99b0f3c2014-04-04 19:06:06 -03001760 dev->dev.groups = dev->sysfs_groups;
Sean Young6d75db32017-09-01 11:30:50 -03001761 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1762 dev->sysfs_groups[attr++] = &rc_dev_ro_protocol_attr_grp;
1763 else if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
1764 dev->sysfs_groups[attr++] = &rc_dev_rw_protocol_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001765 if (dev->s_filter)
David Härdeman120703f2014-04-03 20:31:30 -03001766 dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001767 if (dev->s_wakeup_filter)
1768 dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001769 dev->sysfs_groups[attr++] = NULL;
1770
David Härdemanf56928a2017-05-03 07:04:00 -03001771 if (dev->driver_type == RC_DRIVER_IR_RAW ||
1772 dev->driver_type == RC_DRIVER_IR_RAW_TX) {
1773 rc = ir_raw_event_prepare(dev);
1774 if (rc < 0)
1775 goto out_minor;
1776 }
1777
1778 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1779 rc = rc_prepare_rx_device(dev);
1780 if (rc)
1781 goto out_raw;
1782 }
1783
David Härdemand8b4b582010-10-29 16:08:23 -03001784 rc = device_add(&dev->dev);
1785 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001786 goto out_rx_free;
David Härdemand8b4b582010-10-29 16:08:23 -03001787
David Härdemand8b4b582010-10-29 16:08:23 -03001788 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Heiner Kallweit4dc0e902015-10-29 19:39:06 -02001789 dev_info(&dev->dev, "%s as %s\n",
Sean Young518f4b22017-07-01 12:13:19 -04001790 dev->device_name ?: "Unspecified device", path ?: "N/A");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001791 kfree(path);
1792
Sean Young5df62772017-02-23 06:11:21 -03001793 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1794 rc = rc_setup_rx_device(dev);
1795 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001796 goto out_dev;
1797 }
1798
1799 if (dev->driver_type == RC_DRIVER_IR_RAW ||
1800 dev->driver_type == RC_DRIVER_IR_RAW_TX) {
1801 rc = ir_raw_event_register(dev);
1802 if (rc < 0)
1803 goto out_rx;
David Härdemand8b4b582010-10-29 16:08:23 -03001804 }
1805
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001806 IR_dprintk(1, "Registered rc%u (driver: %s)\n",
David Härdemanfcb13092015-05-19 19:03:17 -03001807 dev->minor,
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001808 dev->driver_name ? dev->driver_name : "unknown");
David Härdemand8b4b582010-10-29 16:08:23 -03001809
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001810 return 0;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001811
David Härdemanf56928a2017-05-03 07:04:00 -03001812out_rx:
1813 rc_free_rx_device(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001814out_dev:
1815 device_del(&dev->dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001816out_rx_free:
1817 ir_free_table(&dev->rc_map);
1818out_raw:
1819 ir_raw_event_free(dev);
1820out_minor:
David Härdemanfcb13092015-05-19 19:03:17 -03001821 ida_simple_remove(&rc_ida, minor);
David Härdemand8b4b582010-10-29 16:08:23 -03001822 return rc;
1823}
1824EXPORT_SYMBOL_GPL(rc_register_device);
1825
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001826static void devm_rc_release(struct device *dev, void *res)
1827{
1828 rc_unregister_device(*(struct rc_dev **)res);
1829}
1830
1831int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
1832{
1833 struct rc_dev **dr;
1834 int ret;
1835
1836 dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
1837 if (!dr)
1838 return -ENOMEM;
1839
1840 ret = rc_register_device(dev);
1841 if (ret) {
1842 devres_free(dr);
1843 return ret;
1844 }
1845
1846 *dr = dev;
1847 devres_add(parent, dr);
1848
1849 return 0;
1850}
1851EXPORT_SYMBOL_GPL(devm_rc_register_device);
1852
David Härdemand8b4b582010-10-29 16:08:23 -03001853void rc_unregister_device(struct rc_dev *dev)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001854{
David Härdemand8b4b582010-10-29 16:08:23 -03001855 if (!dev)
1856 return;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001857
David Härdemand8b4b582010-10-29 16:08:23 -03001858 del_timer_sync(&dev->timer_keyup);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001859
David Härdemand8b4b582010-10-29 16:08:23 -03001860 if (dev->driver_type == RC_DRIVER_IR_RAW)
1861 ir_raw_event_unregister(dev);
1862
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001863 rc_free_rx_device(dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001864
1865 device_del(&dev->dev);
1866
David Härdemanfcb13092015-05-19 19:03:17 -03001867 ida_simple_remove(&rc_ida, dev->minor);
1868
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001869 if (!dev->managed_alloc)
1870 rc_free_device(dev);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001871}
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001872
David Härdemand8b4b582010-10-29 16:08:23 -03001873EXPORT_SYMBOL_GPL(rc_unregister_device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001874
1875/*
1876 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1877 */
1878
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001879static int __init rc_core_init(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001880{
David Härdeman40fc5322013-03-06 16:52:10 -03001881 int rc = class_register(&rc_class);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001882 if (rc) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -02001883 pr_err("rc_core: unable to register rc class\n");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001884 return rc;
1885 }
1886
Sean Young153a60b2013-07-30 19:00:01 -03001887 led_trigger_register_simple("rc-feedback", &led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001888 rc_map_register(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001889
1890 return 0;
1891}
1892
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001893static void __exit rc_core_exit(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001894{
David Härdeman40fc5322013-03-06 16:52:10 -03001895 class_unregister(&rc_class);
Sean Young153a60b2013-07-30 19:00:01 -03001896 led_trigger_unregister_simple(led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001897 rc_map_unregister(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001898}
1899
David Härdemane76d4ce2013-03-06 16:52:15 -03001900subsys_initcall(rc_core_init);
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001901module_exit(rc_core_exit);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001902
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001903int rc_core_debug; /* ir_debug level (0,1,2) */
1904EXPORT_SYMBOL_GPL(rc_core_debug);
1905module_param_named(debug, rc_core_debug, int, 0644);
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03001906
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02001907MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03001908MODULE_LICENSE("GPL");