blob: 7aaf28bcb01e19b1e479f1f54f06e9c7d16b5378 [file] [log] [blame]
David Härdeman829ba9f2010-11-19 20:43:27 -03001/* rc-main.c - Remote Controller core module
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -03002 *
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02003 * Copyright (C) 2009-2010 by Mauro Carvalho Chehab
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -030013 */
14
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -020015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -030017#include <media/rc-core.h>
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030018#include <linux/spinlock.h>
19#include <linux/delay.h>
Mauro Carvalho Chehab882ead32009-12-29 10:37:38 -030020#include <linux/input.h>
Sean Young153a60b2013-07-30 19:00:01 -030021#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
David Härdemanfcb13092015-05-19 19:03:17 -030023#include <linux/idr.h>
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -030024#include <linux/device.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040025#include <linux/module.h>
Mauro Carvalho Chehabf62de672010-11-09 23:09:57 -030026#include "rc-core-priv.h"
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -030027
David Härdemanb3074c02010-04-02 15:58:28 -030028/* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */
29#define IR_TAB_MIN_SIZE 256
30#define IR_TAB_MAX_SIZE 8192
David Härdemanfcb13092015-05-19 19:03:17 -030031#define RC_DEV_MAX 256
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -030032
David Härdemana374fef2010-04-02 15:58:29 -030033/* FIXME: IR_KEYPRESS_TIMEOUT should be protocol specific */
34#define IR_KEYPRESS_TIMEOUT 250
35
David Härdeman4c7b3552010-11-10 11:04:19 -030036/* Used to keep track of known keymaps */
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030037static LIST_HEAD(rc_map_list);
38static DEFINE_SPINLOCK(rc_map_lock);
Sean Young153a60b2013-07-30 19:00:01 -030039static struct led_trigger *led_feedback;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030040
David Härdemanfcb13092015-05-19 19:03:17 -030041/* Used to keep track of rc devices */
42static DEFINE_IDA(rc_ida);
43
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030044static struct rc_map_list *seek_rc_map(const char *name)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030045{
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030046 struct rc_map_list *map = NULL;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030047
48 spin_lock(&rc_map_lock);
49 list_for_each_entry(map, &rc_map_list, list) {
50 if (!strcmp(name, map->map.name)) {
51 spin_unlock(&rc_map_lock);
52 return map;
53 }
54 }
55 spin_unlock(&rc_map_lock);
56
57 return NULL;
58}
59
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030060struct rc_map *rc_map_get(const char *name)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030061{
62
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030063 struct rc_map_list *map;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030064
65 map = seek_rc_map(name);
Russell King2ff56fa2015-10-15 13:15:24 -030066#ifdef CONFIG_MODULES
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030067 if (!map) {
Kees Cook8ea54882014-03-11 17:25:53 -030068 int rc = request_module("%s", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030069 if (rc < 0) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -020070 pr_err("Couldn't load IR keymap %s\n", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030071 return NULL;
72 }
73 msleep(20); /* Give some time for IR to register */
74
75 map = seek_rc_map(name);
76 }
77#endif
78 if (!map) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -020079 pr_err("IR keymap %s not found\n", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030080 return NULL;
81 }
82
83 printk(KERN_INFO "Registered IR keymap %s\n", map->map.name);
84
85 return &map->map;
86}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030087EXPORT_SYMBOL_GPL(rc_map_get);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030088
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030089int rc_map_register(struct rc_map_list *map)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030090{
91 spin_lock(&rc_map_lock);
92 list_add_tail(&map->list, &rc_map_list);
93 spin_unlock(&rc_map_lock);
94 return 0;
95}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030096EXPORT_SYMBOL_GPL(rc_map_register);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030097
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030098void rc_map_unregister(struct rc_map_list *map)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030099{
100 spin_lock(&rc_map_lock);
101 list_del(&map->list);
102 spin_unlock(&rc_map_lock);
103}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300104EXPORT_SYMBOL_GPL(rc_map_unregister);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300105
106
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300107static struct rc_map_table empty[] = {
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300108 { 0x2a, KEY_COFFEE },
109};
110
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300111static struct rc_map_list empty_map = {
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300112 .map = {
113 .scan = empty,
114 .size = ARRAY_SIZE(empty),
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -0300115 .rc_type = RC_TYPE_UNKNOWN, /* Legacy IR type */
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300116 .name = RC_MAP_EMPTY,
117 }
118};
119
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300120/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700121 * ir_create_table() - initializes a scancode table
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300122 * @rc_map: the rc_map to initialize
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700123 * @name: name to assign to the table
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -0300124 * @rc_type: ir type to assign to the new table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700125 * @size: initial size of the table
126 * @return: zero on success or a negative error code
127 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300128 * This routine will initialize the rc_map and will allocate
David Härdemand8b4b582010-10-29 16:08:23 -0300129 * memory to hold at least the specified number of elements.
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700130 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300131static int ir_create_table(struct rc_map *rc_map,
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -0300132 const char *name, u64 rc_type, size_t size)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700133{
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300134 rc_map->name = kstrdup(name, GFP_KERNEL);
135 if (!rc_map->name)
136 return -ENOMEM;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300137 rc_map->rc_type = rc_type;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300138 rc_map->alloc = roundup_pow_of_two(size * sizeof(struct rc_map_table));
139 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300140 rc_map->scan = kmalloc(rc_map->alloc, GFP_KERNEL);
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300141 if (!rc_map->scan) {
142 kfree(rc_map->name);
143 rc_map->name = NULL;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700144 return -ENOMEM;
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300145 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700146
147 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300148 rc_map->size, rc_map->alloc);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700149 return 0;
150}
151
152/**
153 * ir_free_table() - frees memory allocated by a scancode table
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300154 * @rc_map: the table whose mappings need to be freed
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700155 *
156 * This routine will free memory alloctaed for key mappings used by given
157 * scancode table.
158 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300159static void ir_free_table(struct rc_map *rc_map)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700160{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300161 rc_map->size = 0;
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300162 kfree(rc_map->name);
Max Kellermannc183d352016-08-09 18:32:06 -0300163 rc_map->name = NULL;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300164 kfree(rc_map->scan);
165 rc_map->scan = NULL;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700166}
167
168/**
David Härdemanb3074c02010-04-02 15:58:28 -0300169 * ir_resize_table() - resizes a scancode table if necessary
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300170 * @rc_map: the rc_map to resize
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700171 * @gfp_flags: gfp flags to use when allocating memory
David Härdemanb3074c02010-04-02 15:58:28 -0300172 * @return: zero on success or a negative error code
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300173 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300174 * This routine will shrink the rc_map if it has lots of
David Härdemanb3074c02010-04-02 15:58:28 -0300175 * unused entries and grow it if it is full.
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300176 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300177static int ir_resize_table(struct rc_map *rc_map, gfp_t gfp_flags)
David Härdemanb3074c02010-04-02 15:58:28 -0300178{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300179 unsigned int oldalloc = rc_map->alloc;
David Härdemanb3074c02010-04-02 15:58:28 -0300180 unsigned int newalloc = oldalloc;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300181 struct rc_map_table *oldscan = rc_map->scan;
182 struct rc_map_table *newscan;
David Härdemanb3074c02010-04-02 15:58:28 -0300183
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300184 if (rc_map->size == rc_map->len) {
David Härdemanb3074c02010-04-02 15:58:28 -0300185 /* All entries in use -> grow keytable */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300186 if (rc_map->alloc >= IR_TAB_MAX_SIZE)
David Härdemanb3074c02010-04-02 15:58:28 -0300187 return -ENOMEM;
188
189 newalloc *= 2;
190 IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
191 }
192
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300193 if ((rc_map->len * 3 < rc_map->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
David Härdemanb3074c02010-04-02 15:58:28 -0300194 /* Less than 1/3 of entries in use -> shrink keytable */
195 newalloc /= 2;
196 IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
197 }
198
199 if (newalloc == oldalloc)
200 return 0;
201
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700202 newscan = kmalloc(newalloc, gfp_flags);
David Härdemanb3074c02010-04-02 15:58:28 -0300203 if (!newscan) {
204 IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
205 return -ENOMEM;
206 }
207
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300208 memcpy(newscan, rc_map->scan, rc_map->len * sizeof(struct rc_map_table));
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300209 rc_map->scan = newscan;
210 rc_map->alloc = newalloc;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300211 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
David Härdemanb3074c02010-04-02 15:58:28 -0300212 kfree(oldscan);
213 return 0;
214}
215
216/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700217 * ir_update_mapping() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300218 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300219 * @rc_map: scancode table to be adjusted
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700220 * @index: index of the mapping that needs to be updated
221 * @keycode: the desired keycode
222 * @return: previous keycode assigned to the mapping
David Härdemanb3074c02010-04-02 15:58:28 -0300223 *
David Härdemand8b4b582010-10-29 16:08:23 -0300224 * This routine is used to update scancode->keycode mapping at given
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700225 * position.
David Härdemanb3074c02010-04-02 15:58:28 -0300226 */
David Härdemand8b4b582010-10-29 16:08:23 -0300227static unsigned int ir_update_mapping(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300228 struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700229 unsigned int index,
230 unsigned int new_keycode)
231{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300232 int old_keycode = rc_map->scan[index].keycode;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700233 int i;
234
235 /* Did the user wish to remove the mapping? */
236 if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
237 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300238 index, rc_map->scan[index].scancode);
239 rc_map->len--;
240 memmove(&rc_map->scan[index], &rc_map->scan[index+ 1],
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300241 (rc_map->len - index) * sizeof(struct rc_map_table));
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700242 } else {
243 IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n",
244 index,
245 old_keycode == KEY_RESERVED ? "New" : "Replacing",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300246 rc_map->scan[index].scancode, new_keycode);
247 rc_map->scan[index].keycode = new_keycode;
David Härdemand8b4b582010-10-29 16:08:23 -0300248 __set_bit(new_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700249 }
250
251 if (old_keycode != KEY_RESERVED) {
252 /* A previous mapping was updated... */
David Härdemand8b4b582010-10-29 16:08:23 -0300253 __clear_bit(old_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700254 /* ... but another scancode might use the same keycode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300255 for (i = 0; i < rc_map->len; i++) {
256 if (rc_map->scan[i].keycode == old_keycode) {
David Härdemand8b4b582010-10-29 16:08:23 -0300257 __set_bit(old_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700258 break;
259 }
260 }
261
262 /* Possibly shrink the keytable, failure is not a problem */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300263 ir_resize_table(rc_map, GFP_ATOMIC);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700264 }
265
266 return old_keycode;
267}
268
269/**
David Härdeman4c7b3552010-11-10 11:04:19 -0300270 * ir_establish_scancode() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300271 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300272 * @rc_map: scancode table to be searched
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700273 * @scancode: the desired scancode
274 * @resize: controls whether we allowed to resize the table to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300275 * accommodate not yet present scancodes
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700276 * @return: index of the mapping containing scancode in question
277 * or -1U in case of failure.
278 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300279 * This routine is used to locate given scancode in rc_map.
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700280 * If scancode is not yet present the routine will allocate a new slot
281 * for it.
282 */
David Härdemand8b4b582010-10-29 16:08:23 -0300283static unsigned int ir_establish_scancode(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300284 struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700285 unsigned int scancode,
286 bool resize)
David Härdemanb3074c02010-04-02 15:58:28 -0300287{
288 unsigned int i;
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300289
290 /*
291 * Unfortunately, some hardware-based IR decoders don't provide
292 * all bits for the complete IR code. In general, they provide only
293 * the command part of the IR code. Yet, as it is possible to replace
294 * the provided IR with another one, it is needed to allow loading
David Härdemand8b4b582010-10-29 16:08:23 -0300295 * IR tables from other remotes. So, we support specifying a mask to
296 * indicate the valid bits of the scancodes.
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300297 */
David Härdeman9d2f1d32014-04-03 20:32:26 -0300298 if (dev->scancode_mask)
299 scancode &= dev->scancode_mask;
David Härdemanb3074c02010-04-02 15:58:28 -0300300
301 /* First check if we already have a mapping for this ir command */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300302 for (i = 0; i < rc_map->len; i++) {
303 if (rc_map->scan[i].scancode == scancode)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700304 return i;
305
David Härdemanb3074c02010-04-02 15:58:28 -0300306 /* Keytable is sorted from lowest to highest scancode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300307 if (rc_map->scan[i].scancode >= scancode)
David Härdemanb3074c02010-04-02 15:58:28 -0300308 break;
David Härdemanb3074c02010-04-02 15:58:28 -0300309 }
310
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700311 /* No previous mapping found, we might need to grow the table */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300312 if (rc_map->size == rc_map->len) {
313 if (!resize || ir_resize_table(rc_map, GFP_ATOMIC))
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700314 return -1U;
315 }
David Härdemanb3074c02010-04-02 15:58:28 -0300316
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700317 /* i is the proper index to insert our new keycode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300318 if (i < rc_map->len)
319 memmove(&rc_map->scan[i + 1], &rc_map->scan[i],
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300320 (rc_map->len - i) * sizeof(struct rc_map_table));
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300321 rc_map->scan[i].scancode = scancode;
322 rc_map->scan[i].keycode = KEY_RESERVED;
323 rc_map->len++;
David Härdemanb3074c02010-04-02 15:58:28 -0300324
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700325 return i;
David Härdemanb3074c02010-04-02 15:58:28 -0300326}
327
328/**
329 * ir_setkeycode() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300330 * @idev: the struct input_dev device descriptor
David Härdemanb3074c02010-04-02 15:58:28 -0300331 * @scancode: the desired scancode
332 * @keycode: result
333 * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
334 *
335 * This routine is used to handle evdev EVIOCSKEY ioctl.
336 */
David Härdemand8b4b582010-10-29 16:08:23 -0300337static int ir_setkeycode(struct input_dev *idev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700338 const struct input_keymap_entry *ke,
339 unsigned int *old_keycode)
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300340{
David Härdemand8b4b582010-10-29 16:08:23 -0300341 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300342 struct rc_map *rc_map = &rdev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700343 unsigned int index;
344 unsigned int scancode;
Mauro Carvalho Chehabdea8a392010-11-29 07:46:13 -0300345 int retval = 0;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700346 unsigned long flags;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300347
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300348 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700349
350 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
351 index = ke->index;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300352 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700353 retval = -EINVAL;
354 goto out;
355 }
356 } else {
357 retval = input_scancode_to_scalar(ke, &scancode);
358 if (retval)
359 goto out;
360
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300361 index = ir_establish_scancode(rdev, rc_map, scancode, true);
362 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700363 retval = -ENOMEM;
364 goto out;
365 }
366 }
367
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300368 *old_keycode = ir_update_mapping(rdev, rc_map, index, ke->keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700369
370out:
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300371 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700372 return retval;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300373}
374
375/**
David Härdemanb3074c02010-04-02 15:58:28 -0300376 * ir_setkeytable() - sets several entries in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300377 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300378 * @to: the struct rc_map to copy entries to
379 * @from: the struct rc_map to copy entries from
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700380 * @return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300381 *
David Härdemanb3074c02010-04-02 15:58:28 -0300382 * This routine is used to handle table initialization.
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300383 */
David Härdemand8b4b582010-10-29 16:08:23 -0300384static int ir_setkeytable(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300385 const struct rc_map *from)
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300386{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300387 struct rc_map *rc_map = &dev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700388 unsigned int i, index;
389 int rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300390
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300391 rc = ir_create_table(rc_map, from->name,
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -0300392 from->rc_type, from->size);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700393 if (rc)
394 return rc;
395
396 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300397 rc_map->size, rc_map->alloc);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700398
David Härdemanb3074c02010-04-02 15:58:28 -0300399 for (i = 0; i < from->size; i++) {
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300400 index = ir_establish_scancode(dev, rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700401 from->scan[i].scancode, false);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300402 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700403 rc = -ENOMEM;
David Härdemanb3074c02010-04-02 15:58:28 -0300404 break;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700405 }
406
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300407 ir_update_mapping(dev, rc_map, index,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700408 from->scan[i].keycode);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300409 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700410
411 if (rc)
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300412 ir_free_table(rc_map);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700413
David Härdemanb3074c02010-04-02 15:58:28 -0300414 return rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300415}
416
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300417/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700418 * ir_lookup_by_scancode() - locate mapping by scancode
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300419 * @rc_map: the struct rc_map to search
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700420 * @scancode: scancode to look for in the table
421 * @return: index in the table, -1U if not found
422 *
423 * This routine performs binary search in RC keykeymap table for
424 * given scancode.
425 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300426static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700427 unsigned int scancode)
428{
David Härdeman0d070252010-10-30 22:17:44 +0200429 int start = 0;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300430 int end = rc_map->len - 1;
David Härdeman0d070252010-10-30 22:17:44 +0200431 int mid;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700432
433 while (start <= end) {
434 mid = (start + end) / 2;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300435 if (rc_map->scan[mid].scancode < scancode)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700436 start = mid + 1;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300437 else if (rc_map->scan[mid].scancode > scancode)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700438 end = mid - 1;
439 else
440 return mid;
441 }
442
443 return -1U;
444}
445
446/**
David Härdemanb3074c02010-04-02 15:58:28 -0300447 * ir_getkeycode() - get a keycode from the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300448 * @idev: the struct input_dev device descriptor
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300449 * @scancode: the desired scancode
David Härdemanb3074c02010-04-02 15:58:28 -0300450 * @keycode: used to return the keycode, if found, or KEY_RESERVED
451 * @return: always returns zero.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300452 *
453 * This routine is used to handle evdev EVIOCGKEY ioctl.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300454 */
David Härdemand8b4b582010-10-29 16:08:23 -0300455static int ir_getkeycode(struct input_dev *idev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700456 struct input_keymap_entry *ke)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300457{
David Härdemand8b4b582010-10-29 16:08:23 -0300458 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300459 struct rc_map *rc_map = &rdev->rc_map;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300460 struct rc_map_table *entry;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700461 unsigned long flags;
462 unsigned int index;
463 unsigned int scancode;
464 int retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300465
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300466 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700467
468 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
469 index = ke->index;
470 } else {
471 retval = input_scancode_to_scalar(ke, &scancode);
472 if (retval)
473 goto out;
474
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300475 index = ir_lookup_by_scancode(rc_map, scancode);
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300476 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700477
Dmitry Torokhov54e74b82011-01-28 23:33:29 -0800478 if (index < rc_map->len) {
479 entry = &rc_map->scan[index];
480
481 ke->index = index;
482 ke->keycode = entry->keycode;
483 ke->len = sizeof(entry->scancode);
484 memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
485
486 } else if (!(ke->flags & INPUT_KEYMAP_BY_INDEX)) {
487 /*
488 * We do not really know the valid range of scancodes
489 * so let's respond with KEY_RESERVED to anything we
490 * do not have mapping for [yet].
491 */
492 ke->index = index;
493 ke->keycode = KEY_RESERVED;
494 } else {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700495 retval = -EINVAL;
496 goto out;
497 }
498
Dmitry Torokhov47c5ba52010-10-31 15:18:42 -0700499 retval = 0;
500
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700501out:
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300502 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700503 return retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300504}
505
506/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300507 * rc_g_keycode_from_table() - gets the keycode that corresponds to a scancode
David Härdemand8b4b582010-10-29 16:08:23 -0300508 * @dev: the struct rc_dev descriptor of the device
509 * @scancode: the scancode to look for
510 * @return: the corresponding keycode, or KEY_RESERVED
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300511 *
David Härdemand8b4b582010-10-29 16:08:23 -0300512 * This routine is used by drivers which need to convert a scancode to a
513 * keycode. Normally it should not be used since drivers should have no
514 * interest in keycodes.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300515 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300516u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300517{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300518 struct rc_map *rc_map = &dev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700519 unsigned int keycode;
520 unsigned int index;
521 unsigned long flags;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300522
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300523 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700524
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300525 index = ir_lookup_by_scancode(rc_map, scancode);
526 keycode = index < rc_map->len ?
527 rc_map->scan[index].keycode : KEY_RESERVED;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700528
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300529 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700530
Mauro Carvalho Chehab35438942010-04-03 16:53:16 -0300531 if (keycode != KEY_RESERVED)
532 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
Sean Young518f4b22017-07-01 12:13:19 -0400533 dev->device_name, scancode, keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700534
David Härdemanb3074c02010-04-02 15:58:28 -0300535 return keycode;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300536}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300537EXPORT_SYMBOL_GPL(rc_g_keycode_from_table);
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300538
539/**
David Härdeman62c65032010-10-29 16:08:07 -0300540 * ir_do_keyup() - internal function to signal the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300541 * @dev: the struct rc_dev descriptor of the device
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300542 * @sync: whether or not to call input_sync
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300543 *
David Härdeman62c65032010-10-29 16:08:07 -0300544 * This function is used internally to release a keypress, it must be
545 * called with keylock held.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300546 */
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300547static void ir_do_keyup(struct rc_dev *dev, bool sync)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300548{
David Härdemand8b4b582010-10-29 16:08:23 -0300549 if (!dev->keypressed)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300550 return;
551
David Härdemand8b4b582010-10-29 16:08:23 -0300552 IR_dprintk(1, "keyup key 0x%04x\n", dev->last_keycode);
553 input_report_key(dev->input_dev, dev->last_keycode, 0);
Sean Young153a60b2013-07-30 19:00:01 -0300554 led_trigger_event(led_feedback, LED_OFF);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300555 if (sync)
556 input_sync(dev->input_dev);
David Härdemand8b4b582010-10-29 16:08:23 -0300557 dev->keypressed = false;
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300558}
David Härdeman62c65032010-10-29 16:08:07 -0300559
560/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300561 * rc_keyup() - signals the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300562 * @dev: the struct rc_dev descriptor of the device
David Härdeman62c65032010-10-29 16:08:07 -0300563 *
564 * This routine is used to signal that a key has been released on the
565 * remote control.
566 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300567void rc_keyup(struct rc_dev *dev)
David Härdeman62c65032010-10-29 16:08:07 -0300568{
569 unsigned long flags;
David Härdeman62c65032010-10-29 16:08:07 -0300570
David Härdemand8b4b582010-10-29 16:08:23 -0300571 spin_lock_irqsave(&dev->keylock, flags);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300572 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300573 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300574}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300575EXPORT_SYMBOL_GPL(rc_keyup);
David Härdemana374fef2010-04-02 15:58:29 -0300576
577/**
578 * ir_timer_keyup() - generates a keyup event after a timeout
David Härdemand8b4b582010-10-29 16:08:23 -0300579 * @cookie: a pointer to the struct rc_dev for the device
David Härdemana374fef2010-04-02 15:58:29 -0300580 *
581 * This routine will generate a keyup event some time after a keydown event
582 * is generated when no further activity has been detected.
583 */
584static void ir_timer_keyup(unsigned long cookie)
585{
David Härdemand8b4b582010-10-29 16:08:23 -0300586 struct rc_dev *dev = (struct rc_dev *)cookie;
David Härdemana374fef2010-04-02 15:58:29 -0300587 unsigned long flags;
588
589 /*
590 * ir->keyup_jiffies is used to prevent a race condition if a
591 * hardware interrupt occurs at this point and the keyup timer
592 * event is moved further into the future as a result.
593 *
594 * The timer will then be reactivated and this function called
595 * again in the future. We need to exit gracefully in that case
596 * to allow the input subsystem to do its auto-repeat magic or
597 * a keyup event might follow immediately after the keydown.
598 */
David Härdemand8b4b582010-10-29 16:08:23 -0300599 spin_lock_irqsave(&dev->keylock, flags);
600 if (time_is_before_eq_jiffies(dev->keyup_jiffies))
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300601 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300602 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300603}
604
605/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300606 * rc_repeat() - signals that a key is still pressed
David Härdemand8b4b582010-10-29 16:08:23 -0300607 * @dev: the struct rc_dev descriptor of the device
David Härdemana374fef2010-04-02 15:58:29 -0300608 *
609 * This routine is used by IR decoders when a repeat message which does
610 * not include the necessary bits to reproduce the scancode has been
611 * received.
612 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300613void rc_repeat(struct rc_dev *dev)
David Härdemana374fef2010-04-02 15:58:29 -0300614{
615 unsigned long flags;
David Härdemana374fef2010-04-02 15:58:29 -0300616
David Härdemand8b4b582010-10-29 16:08:23 -0300617 spin_lock_irqsave(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300618
David Härdemand8b4b582010-10-29 16:08:23 -0300619 if (!dev->keypressed)
David Härdemana374fef2010-04-02 15:58:29 -0300620 goto out;
621
David Härdeman265a2982017-06-22 15:23:54 -0400622 input_event(dev->input_dev, EV_MSC, MSC_SCAN, dev->last_scancode);
623 input_sync(dev->input_dev);
624
David Härdemand8b4b582010-10-29 16:08:23 -0300625 dev->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
626 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
David Härdemana374fef2010-04-02 15:58:29 -0300627
628out:
David Härdemand8b4b582010-10-29 16:08:23 -0300629 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300630}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300631EXPORT_SYMBOL_GPL(rc_repeat);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300632
633/**
David Härdeman62c65032010-10-29 16:08:07 -0300634 * ir_do_keydown() - internal function to process a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300635 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300636 * @protocol: the protocol of the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300637 * @scancode: the scancode of the keypress
638 * @keycode: the keycode of the keypress
639 * @toggle: the toggle value of the keypress
640 *
641 * This function is used internally to register a keypress, it must be
642 * called with keylock held.
643 */
David Härdeman120703f2014-04-03 20:31:30 -0300644static void ir_do_keydown(struct rc_dev *dev, enum rc_type protocol,
645 u32 scancode, u32 keycode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300646{
David Härdeman99b0f3c2014-04-04 19:06:06 -0300647 bool new_event = (!dev->keypressed ||
David Härdeman120703f2014-04-03 20:31:30 -0300648 dev->last_protocol != protocol ||
David Härdeman99b0f3c2014-04-04 19:06:06 -0300649 dev->last_scancode != scancode ||
David Härdeman120703f2014-04-03 20:31:30 -0300650 dev->last_toggle != toggle);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300651
652 if (new_event && dev->keypressed)
653 ir_do_keyup(dev, false);
654
David Härdemand8b4b582010-10-29 16:08:23 -0300655 input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300656
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300657 if (new_event && keycode != KEY_RESERVED) {
658 /* Register a keypress */
659 dev->keypressed = true;
David Härdeman120703f2014-04-03 20:31:30 -0300660 dev->last_protocol = protocol;
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300661 dev->last_scancode = scancode;
662 dev->last_toggle = toggle;
663 dev->last_keycode = keycode;
David Härdeman62c65032010-10-29 16:08:07 -0300664
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200665 IR_dprintk(1, "%s: key down event, key 0x%04x, protocol 0x%04x, scancode 0x%08x\n",
Sean Young518f4b22017-07-01 12:13:19 -0400666 dev->device_name, keycode, protocol, scancode);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300667 input_report_key(dev->input_dev, keycode, 1);
James Hogan70a2f912014-01-16 19:56:22 -0300668
669 led_trigger_event(led_feedback, LED_FULL);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300670 }
David Härdeman62c65032010-10-29 16:08:07 -0300671
David Härdemand8b4b582010-10-29 16:08:23 -0300672 input_sync(dev->input_dev);
David Härdeman62c65032010-10-29 16:08:07 -0300673}
674
675/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300676 * rc_keydown() - generates input event for a key press
David Härdemand8b4b582010-10-29 16:08:23 -0300677 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300678 * @protocol: the protocol for the keypress
679 * @scancode: the scancode for the keypress
David Härdemana374fef2010-04-02 15:58:29 -0300680 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
681 * support toggle values, this should be set to zero)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300682 *
David Härdemand8b4b582010-10-29 16:08:23 -0300683 * This routine is used to signal that a key has been pressed on the
684 * remote control.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300685 */
David Härdeman120703f2014-04-03 20:31:30 -0300686void rc_keydown(struct rc_dev *dev, enum rc_type protocol, u32 scancode, u8 toggle)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300687{
David Härdemana374fef2010-04-02 15:58:29 -0300688 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300689 u32 keycode = rc_g_keycode_from_table(dev, scancode);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300690
David Härdemand8b4b582010-10-29 16:08:23 -0300691 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300692 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300693
David Härdemand8b4b582010-10-29 16:08:23 -0300694 if (dev->keypressed) {
695 dev->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
696 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
David Härdeman62c65032010-10-29 16:08:07 -0300697 }
David Härdemand8b4b582010-10-29 16:08:23 -0300698 spin_unlock_irqrestore(&dev->keylock, flags);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300699}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300700EXPORT_SYMBOL_GPL(rc_keydown);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300701
David Härdeman62c65032010-10-29 16:08:07 -0300702/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300703 * rc_keydown_notimeout() - generates input event for a key press without
David Härdeman62c65032010-10-29 16:08:07 -0300704 * an automatic keyup event at a later time
David Härdemand8b4b582010-10-29 16:08:23 -0300705 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300706 * @protocol: the protocol for the keypress
707 * @scancode: the scancode for the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300708 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
709 * support toggle values, this should be set to zero)
710 *
David Härdemand8b4b582010-10-29 16:08:23 -0300711 * This routine is used to signal that a key has been pressed on the
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300712 * remote control. The driver must manually call rc_keyup() at a later stage.
David Härdeman62c65032010-10-29 16:08:07 -0300713 */
David Härdeman120703f2014-04-03 20:31:30 -0300714void rc_keydown_notimeout(struct rc_dev *dev, enum rc_type protocol,
715 u32 scancode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300716{
717 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300718 u32 keycode = rc_g_keycode_from_table(dev, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300719
David Härdemand8b4b582010-10-29 16:08:23 -0300720 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300721 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
David Härdemand8b4b582010-10-29 16:08:23 -0300722 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300723}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300724EXPORT_SYMBOL_GPL(rc_keydown_notimeout);
David Härdeman62c65032010-10-29 16:08:07 -0300725
Sean Youngb590c0b2016-12-05 19:24:59 -0200726/**
727 * rc_validate_filter() - checks that the scancode and mask are valid and
728 * provides sensible defaults
James Hoganf423ccc2015-03-31 14:48:10 -0300729 * @dev: the struct rc_dev descriptor of the device
Sean Youngb590c0b2016-12-05 19:24:59 -0200730 * @filter: the scancode and mask
731 * @return: 0 or -EINVAL if the filter is not valid
732 */
James Hoganf423ccc2015-03-31 14:48:10 -0300733static int rc_validate_filter(struct rc_dev *dev,
Sean Youngb590c0b2016-12-05 19:24:59 -0200734 struct rc_scancode_filter *filter)
735{
Sean Young2168b412017-08-07 09:21:29 -0400736 static const u32 masks[] = {
Sean Youngb590c0b2016-12-05 19:24:59 -0200737 [RC_TYPE_RC5] = 0x1f7f,
738 [RC_TYPE_RC5X_20] = 0x1f7f3f,
739 [RC_TYPE_RC5_SZ] = 0x2fff,
740 [RC_TYPE_SONY12] = 0x1f007f,
741 [RC_TYPE_SONY15] = 0xff007f,
742 [RC_TYPE_SONY20] = 0x1fff7f,
743 [RC_TYPE_JVC] = 0xffff,
744 [RC_TYPE_NEC] = 0xffff,
745 [RC_TYPE_NECX] = 0xffffff,
746 [RC_TYPE_NEC32] = 0xffffffff,
747 [RC_TYPE_SANYO] = 0x1fffff,
Sean Youngb73bc162017-02-11 20:33:38 -0200748 [RC_TYPE_MCIR2_KBD] = 0xffff,
749 [RC_TYPE_MCIR2_MSE] = 0x1fffff,
Sean Youngb590c0b2016-12-05 19:24:59 -0200750 [RC_TYPE_RC6_0] = 0xffff,
751 [RC_TYPE_RC6_6A_20] = 0xfffff,
752 [RC_TYPE_RC6_6A_24] = 0xffffff,
753 [RC_TYPE_RC6_6A_32] = 0xffffffff,
754 [RC_TYPE_RC6_MCE] = 0xffff7fff,
755 [RC_TYPE_SHARP] = 0x1fff,
756 };
757 u32 s = filter->data;
James Hoganf423ccc2015-03-31 14:48:10 -0300758 enum rc_type protocol = dev->wakeup_protocol;
Sean Youngb590c0b2016-12-05 19:24:59 -0200759
Sean Young2168b412017-08-07 09:21:29 -0400760 if (protocol >= ARRAY_SIZE(masks))
761 return -EINVAL;
762
Sean Youngb590c0b2016-12-05 19:24:59 -0200763 switch (protocol) {
764 case RC_TYPE_NECX:
765 if ((((s >> 16) ^ ~(s >> 8)) & 0xff) == 0)
766 return -EINVAL;
767 break;
768 case RC_TYPE_NEC32:
769 if ((((s >> 24) ^ ~(s >> 16)) & 0xff) == 0)
770 return -EINVAL;
771 break;
772 case RC_TYPE_RC6_MCE:
773 if ((s & 0xffff0000) != 0x800f0000)
774 return -EINVAL;
775 break;
776 case RC_TYPE_RC6_6A_32:
777 if ((s & 0xffff0000) == 0x800f0000)
778 return -EINVAL;
779 break;
780 default:
781 break;
782 }
783
784 filter->data &= masks[protocol];
785 filter->mask &= masks[protocol];
786
James Hoganf423ccc2015-03-31 14:48:10 -0300787 /*
788 * If we have to raw encode the IR for wakeup, we cannot have a mask
789 */
790 if (dev->encode_wakeup &&
791 filter->mask != 0 && filter->mask != masks[protocol])
792 return -EINVAL;
793
Sean Youngb590c0b2016-12-05 19:24:59 -0200794 return 0;
795}
796
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300797int rc_open(struct rc_dev *rdev)
798{
799 int rval = 0;
800
801 if (!rdev)
802 return -EINVAL;
803
804 mutex_lock(&rdev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -0200805
Juergen Lockf02dcdd2013-08-16 15:00:24 -0300806 if (!rdev->users++ && rdev->open != NULL)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300807 rval = rdev->open(rdev);
808
809 if (rval)
810 rdev->users--;
811
812 mutex_unlock(&rdev->lock);
813
814 return rval;
815}
816EXPORT_SYMBOL_GPL(rc_open);
817
David Härdemand8b4b582010-10-29 16:08:23 -0300818static int ir_open(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300819{
David Härdemand8b4b582010-10-29 16:08:23 -0300820 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300821
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300822 return rc_open(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300823}
824
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300825void rc_close(struct rc_dev *rdev)
826{
827 if (rdev) {
828 mutex_lock(&rdev->lock);
829
Mauro Carvalho Chehab81b7d142015-04-28 09:43:17 -0300830 if (!--rdev->users && rdev->close != NULL)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300831 rdev->close(rdev);
832
833 mutex_unlock(&rdev->lock);
834 }
835}
836EXPORT_SYMBOL_GPL(rc_close);
837
David Härdemand8b4b582010-10-29 16:08:23 -0300838static void ir_close(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300839{
David Härdemand8b4b582010-10-29 16:08:23 -0300840 struct rc_dev *rdev = input_get_drvdata(idev);
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300841 rc_close(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300842}
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300843
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300844/* class for /sys/class/rc */
David Härdeman40fc5322013-03-06 16:52:10 -0300845static char *rc_devnode(struct device *dev, umode_t *mode)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300846{
847 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
848}
849
David Härdeman40fc5322013-03-06 16:52:10 -0300850static struct class rc_class = {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300851 .name = "rc",
David Härdeman40fc5322013-03-06 16:52:10 -0300852 .devnode = rc_devnode,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300853};
854
David Härdemanc003ab12012-10-11 19:11:54 -0300855/*
856 * These are the protocol textual descriptions that are
857 * used by the sysfs protocols file. Note that the order
858 * of the entries is relevant.
859 */
Heiner Kallweit53df8772015-11-16 17:52:17 -0200860static const struct {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300861 u64 type;
Heiner Kallweit53df8772015-11-16 17:52:17 -0200862 const char *name;
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200863 const char *module_name;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300864} proto_names[] = {
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200865 { RC_BIT_NONE, "none", NULL },
866 { RC_BIT_OTHER, "other", NULL },
867 { RC_BIT_UNKNOWN, "unknown", NULL },
David Härdemanc003ab12012-10-11 19:11:54 -0300868 { RC_BIT_RC5 |
Sean Young0fcd3f02016-12-02 15:16:14 -0200869 RC_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
Sean Young2ceeca02016-09-21 06:54:19 -0300870 { RC_BIT_NEC |
871 RC_BIT_NECX |
872 RC_BIT_NEC32, "nec", "ir-nec-decoder" },
David Härdemanc003ab12012-10-11 19:11:54 -0300873 { RC_BIT_RC6_0 |
874 RC_BIT_RC6_6A_20 |
875 RC_BIT_RC6_6A_24 |
876 RC_BIT_RC6_6A_32 |
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200877 RC_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
878 { RC_BIT_JVC, "jvc", "ir-jvc-decoder" },
David Härdemanc003ab12012-10-11 19:11:54 -0300879 { RC_BIT_SONY12 |
880 RC_BIT_SONY15 |
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200881 RC_BIT_SONY20, "sony", "ir-sony-decoder" },
882 { RC_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
883 { RC_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
884 { RC_BIT_SHARP, "sharp", "ir-sharp-decoder" },
Sean Youngb73bc162017-02-11 20:33:38 -0200885 { RC_BIT_MCIR2_KBD |
886 RC_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" },
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200887 { RC_BIT_XMP, "xmp", "ir-xmp-decoder" },
Kamil Debskiff42c8a2015-08-17 08:47:41 -0300888 { RC_BIT_CEC, "cec", NULL },
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300889};
890
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300891/**
James Hoganab88c662014-02-28 20:17:05 -0300892 * struct rc_filter_attribute - Device attribute relating to a filter type.
893 * @attr: Device attribute.
894 * @type: Filter type.
895 * @mask: false for filter value, true for filter mask.
896 */
897struct rc_filter_attribute {
898 struct device_attribute attr;
899 enum rc_filter_type type;
900 bool mask;
901};
902#define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
903
James Hoganab88c662014-02-28 20:17:05 -0300904#define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
905 struct rc_filter_attribute dev_attr_##_name = { \
906 .attr = __ATTR(_name, _mode, _show, _store), \
907 .type = (_type), \
908 .mask = (_mask), \
909 }
910
David Härdemandd6ff6a2015-07-22 17:55:24 -0300911static bool lirc_is_present(void)
912{
913#if defined(CONFIG_LIRC_MODULE)
914 struct module *lirc;
915
916 mutex_lock(&module_mutex);
917 lirc = find_module("lirc_dev");
918 mutex_unlock(&module_mutex);
919
920 return lirc ? true : false;
921#elif defined(CONFIG_LIRC)
922 return true;
923#else
924 return false;
925#endif
926}
927
James Hoganab88c662014-02-28 20:17:05 -0300928/**
Sean Young0751d332016-12-05 17:08:35 -0200929 * show_protocols() - shows the current IR protocol(s)
David Härdemand8b4b582010-10-29 16:08:23 -0300930 * @device: the device descriptor
David Härdemanda6e1622014-04-03 20:32:16 -0300931 * @mattr: the device attribute struct
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300932 * @buf: a pointer to the output buffer
933 *
934 * This routine is a callback routine for input read the IR protocol type(s).
Sean Young0751d332016-12-05 17:08:35 -0200935 * it is trigged by reading /sys/class/rc/rc?/protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300936 * It returns the protocol names of supported protocols.
937 * Enabled protocols are printed in brackets.
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300938 *
David Härdeman18726a32017-04-27 17:34:08 -0300939 * dev->lock is taken to guard against races between
940 * store_protocols and show_protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300941 */
David Härdemand8b4b582010-10-29 16:08:23 -0300942static ssize_t show_protocols(struct device *device,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300943 struct device_attribute *mattr, char *buf)
944{
David Härdemand8b4b582010-10-29 16:08:23 -0300945 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300946 u64 allowed, enabled;
947 char *tmp = buf;
948 int i;
949
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300950 mutex_lock(&dev->lock);
951
Sean Young0751d332016-12-05 17:08:35 -0200952 enabled = dev->enabled_protocols;
953 allowed = dev->allowed_protocols;
954 if (dev->raw && !allowed)
955 allowed = ir_raw_get_allowed_protocols();
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300956
David Härdemanda6e1622014-04-03 20:32:16 -0300957 mutex_unlock(&dev->lock);
958
959 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
960 __func__, (long long)allowed, (long long)enabled);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300961
962 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
963 if (allowed & enabled & proto_names[i].type)
964 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
965 else if (allowed & proto_names[i].type)
966 tmp += sprintf(tmp, "%s ", proto_names[i].name);
David Härdemanc003ab12012-10-11 19:11:54 -0300967
968 if (allowed & proto_names[i].type)
969 allowed &= ~proto_names[i].type;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300970 }
971
David Härdemandd6ff6a2015-07-22 17:55:24 -0300972 if (dev->driver_type == RC_DRIVER_IR_RAW && lirc_is_present())
David Härdeman275ddb42015-05-19 19:03:22 -0300973 tmp += sprintf(tmp, "[lirc] ");
974
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300975 if (tmp != buf)
976 tmp--;
977 *tmp = '\n';
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300978
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300979 return tmp + 1 - buf;
980}
981
982/**
David Härdemanda6e1622014-04-03 20:32:16 -0300983 * parse_protocol_change() - parses a protocol change request
984 * @protocols: pointer to the bitmask of current protocols
985 * @buf: pointer to the buffer with a list of changes
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300986 *
David Härdemanda6e1622014-04-03 20:32:16 -0300987 * Writing "+proto" will add a protocol to the protocol mask.
988 * Writing "-proto" will remove a protocol from protocol mask.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300989 * Writing "proto" will enable only "proto".
990 * Writing "none" will disable all protocols.
David Härdemanda6e1622014-04-03 20:32:16 -0300991 * Returns the number of changes performed or a negative error code.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300992 */
David Härdemanda6e1622014-04-03 20:32:16 -0300993static int parse_protocol_change(u64 *protocols, const char *buf)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300994{
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300995 const char *tmp;
David Härdemanda6e1622014-04-03 20:32:16 -0300996 unsigned count = 0;
997 bool enable, disable;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300998 u64 mask;
David Härdemanda6e1622014-04-03 20:32:16 -0300999 int i;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001000
David Härdemanda6e1622014-04-03 20:32:16 -03001001 while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001002 if (!*tmp)
1003 break;
1004
1005 if (*tmp == '+') {
1006 enable = true;
1007 disable = false;
1008 tmp++;
1009 } else if (*tmp == '-') {
1010 enable = false;
1011 disable = true;
1012 tmp++;
1013 } else {
1014 enable = false;
1015 disable = false;
1016 }
1017
David Härdemanc003ab12012-10-11 19:11:54 -03001018 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1019 if (!strcasecmp(tmp, proto_names[i].name)) {
1020 mask = proto_names[i].type;
1021 break;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001022 }
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001023 }
1024
David Härdemanc003ab12012-10-11 19:11:54 -03001025 if (i == ARRAY_SIZE(proto_names)) {
David Härdeman275ddb42015-05-19 19:03:22 -03001026 if (!strcasecmp(tmp, "lirc"))
1027 mask = 0;
1028 else {
1029 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
1030 return -EINVAL;
1031 }
David Härdemanc003ab12012-10-11 19:11:54 -03001032 }
1033
1034 count++;
1035
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001036 if (enable)
David Härdemanda6e1622014-04-03 20:32:16 -03001037 *protocols |= mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001038 else if (disable)
David Härdemanda6e1622014-04-03 20:32:16 -03001039 *protocols &= ~mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001040 else
David Härdemanda6e1622014-04-03 20:32:16 -03001041 *protocols = mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001042 }
1043
1044 if (!count) {
1045 IR_dprintk(1, "Protocol not specified\n");
David Härdemanda6e1622014-04-03 20:32:16 -03001046 return -EINVAL;
1047 }
1048
1049 return count;
1050}
1051
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001052static void ir_raw_load_modules(u64 *protocols)
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001053{
1054 u64 available;
1055 int i, ret;
1056
1057 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1058 if (proto_names[i].type == RC_BIT_NONE ||
1059 proto_names[i].type & (RC_BIT_OTHER | RC_BIT_UNKNOWN))
1060 continue;
1061
1062 available = ir_raw_get_allowed_protocols();
1063 if (!(*protocols & proto_names[i].type & ~available))
1064 continue;
1065
1066 if (!proto_names[i].module_name) {
1067 pr_err("Can't enable IR protocol %s\n",
1068 proto_names[i].name);
1069 *protocols &= ~proto_names[i].type;
1070 continue;
1071 }
1072
1073 ret = request_module("%s", proto_names[i].module_name);
1074 if (ret < 0) {
1075 pr_err("Couldn't load IR protocol module %s\n",
1076 proto_names[i].module_name);
1077 *protocols &= ~proto_names[i].type;
1078 continue;
1079 }
1080 msleep(20);
1081 available = ir_raw_get_allowed_protocols();
1082 if (!(*protocols & proto_names[i].type & ~available))
1083 continue;
1084
Sean Young8caebcd2017-01-19 19:33:49 -02001085 pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001086 proto_names[i].module_name,
1087 proto_names[i].name);
1088 *protocols &= ~proto_names[i].type;
1089 }
1090}
1091
David Härdemanda6e1622014-04-03 20:32:16 -03001092/**
1093 * store_protocols() - changes the current/wakeup IR protocol(s)
1094 * @device: the device descriptor
1095 * @mattr: the device attribute struct
1096 * @buf: a pointer to the input buffer
1097 * @len: length of the input buffer
1098 *
1099 * This routine is for changing the IR protocol type.
1100 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
1101 * See parse_protocol_change() for the valid commands.
1102 * Returns @len on success or a negative error code.
1103 *
David Härdeman18726a32017-04-27 17:34:08 -03001104 * dev->lock is taken to guard against races between
1105 * store_protocols and show_protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001106 */
1107static ssize_t store_protocols(struct device *device,
1108 struct device_attribute *mattr,
1109 const char *buf, size_t len)
1110{
1111 struct rc_dev *dev = to_rc_dev(device);
David Härdemanda6e1622014-04-03 20:32:16 -03001112 u64 *current_protocols;
David Härdemanda6e1622014-04-03 20:32:16 -03001113 struct rc_scancode_filter *filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001114 u64 old_protocols, new_protocols;
1115 ssize_t rc;
1116
Sean Young0751d332016-12-05 17:08:35 -02001117 IR_dprintk(1, "Normal protocol change requested\n");
1118 current_protocols = &dev->enabled_protocols;
1119 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001120
Sean Young0751d332016-12-05 17:08:35 -02001121 if (!dev->change_protocol) {
David Härdemanda6e1622014-04-03 20:32:16 -03001122 IR_dprintk(1, "Protocol switching not supported\n");
1123 return -EINVAL;
1124 }
1125
1126 mutex_lock(&dev->lock);
1127
1128 old_protocols = *current_protocols;
1129 new_protocols = old_protocols;
1130 rc = parse_protocol_change(&new_protocols, buf);
1131 if (rc < 0)
1132 goto out;
1133
Sean Young0751d332016-12-05 17:08:35 -02001134 rc = dev->change_protocol(dev, &new_protocols);
David Härdemanda6e1622014-04-03 20:32:16 -03001135 if (rc < 0) {
1136 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
1137 (long long)new_protocols);
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001138 goto out;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001139 }
1140
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001141 if (dev->driver_type == RC_DRIVER_IR_RAW)
1142 ir_raw_load_modules(&new_protocols);
1143
James Hogan983c5bd2014-12-08 13:17:07 -03001144 if (new_protocols != old_protocols) {
1145 *current_protocols = new_protocols;
1146 IR_dprintk(1, "Protocols changed to 0x%llx\n",
1147 (long long)new_protocols);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001148 }
1149
James Hogan6bea25a2014-02-28 20:17:06 -03001150 /*
James Hogan983c5bd2014-12-08 13:17:07 -03001151 * If a protocol change was attempted the filter may need updating, even
1152 * if the actual protocol mask hasn't changed (since the driver may have
1153 * cleared the filter).
James Hogan6bea25a2014-02-28 20:17:06 -03001154 * Try setting the same filter with the new protocol (if any).
1155 * Fall back to clearing the filter.
1156 */
Sean Young0751d332016-12-05 17:08:35 -02001157 if (dev->s_filter && filter->mask) {
David Härdemanda6e1622014-04-03 20:32:16 -03001158 if (new_protocols)
Sean Young0751d332016-12-05 17:08:35 -02001159 rc = dev->s_filter(dev, filter);
David Härdemanda6e1622014-04-03 20:32:16 -03001160 else
1161 rc = -1;
David Härdeman23c843b2014-04-04 19:06:01 -03001162
David Härdemanda6e1622014-04-03 20:32:16 -03001163 if (rc < 0) {
1164 filter->data = 0;
1165 filter->mask = 0;
Sean Young0751d332016-12-05 17:08:35 -02001166 dev->s_filter(dev, filter);
James Hogan6bea25a2014-02-28 20:17:06 -03001167 }
James Hogan6bea25a2014-02-28 20:17:06 -03001168 }
1169
David Härdemanda6e1622014-04-03 20:32:16 -03001170 rc = len;
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001171
1172out:
1173 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001174 return rc;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001175}
1176
James Hogan00942d12014-01-17 10:58:49 -03001177/**
James Hogan00942d12014-01-17 10:58:49 -03001178 * show_filter() - shows the current scancode filter value or mask
1179 * @device: the device descriptor
1180 * @attr: the device attribute struct
1181 * @buf: a pointer to the output buffer
1182 *
1183 * This routine is a callback routine to read a scancode filter value or mask.
1184 * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
1185 * It prints the current scancode filter value or mask of the appropriate filter
1186 * type in hexadecimal into @buf and returns the size of the buffer.
1187 *
1188 * Bits of the filter value corresponding to set bits in the filter mask are
1189 * compared against input scancodes and non-matching scancodes are discarded.
1190 *
David Härdeman18726a32017-04-27 17:34:08 -03001191 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001192 * store_filter and show_filter.
1193 */
1194static ssize_t show_filter(struct device *device,
1195 struct device_attribute *attr,
1196 char *buf)
1197{
1198 struct rc_dev *dev = to_rc_dev(device);
1199 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001200 struct rc_scancode_filter *filter;
James Hogan00942d12014-01-17 10:58:49 -03001201 u32 val;
1202
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001203 mutex_lock(&dev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001204
David Härdemanda6e1622014-04-03 20:32:16 -03001205 if (fattr->type == RC_FILTER_NORMAL)
David Härdemanc5540fb2014-04-03 20:32:21 -03001206 filter = &dev->scancode_filter;
James Hogan00942d12014-01-17 10:58:49 -03001207 else
David Härdemanc5540fb2014-04-03 20:32:21 -03001208 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001209
David Härdemanda6e1622014-04-03 20:32:16 -03001210 if (fattr->mask)
1211 val = filter->mask;
1212 else
1213 val = filter->data;
James Hogan00942d12014-01-17 10:58:49 -03001214 mutex_unlock(&dev->lock);
1215
1216 return sprintf(buf, "%#x\n", val);
1217}
1218
1219/**
1220 * store_filter() - changes the scancode filter value
1221 * @device: the device descriptor
1222 * @attr: the device attribute struct
1223 * @buf: a pointer to the input buffer
1224 * @len: length of the input buffer
1225 *
1226 * This routine is for changing a scancode filter value or mask.
1227 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
1228 * Returns -EINVAL if an invalid filter value for the current protocol was
1229 * specified or if scancode filtering is not supported by the driver, otherwise
1230 * returns @len.
1231 *
1232 * Bits of the filter value corresponding to set bits in the filter mask are
1233 * compared against input scancodes and non-matching scancodes are discarded.
1234 *
David Härdeman18726a32017-04-27 17:34:08 -03001235 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001236 * store_filter and show_filter.
1237 */
1238static ssize_t store_filter(struct device *device,
1239 struct device_attribute *attr,
David Härdemanda6e1622014-04-03 20:32:16 -03001240 const char *buf, size_t len)
James Hogan00942d12014-01-17 10:58:49 -03001241{
1242 struct rc_dev *dev = to_rc_dev(device);
1243 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001244 struct rc_scancode_filter new_filter, *filter;
James Hogan00942d12014-01-17 10:58:49 -03001245 int ret;
1246 unsigned long val;
David Härdeman23c843b2014-04-04 19:06:01 -03001247 int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
James Hogan00942d12014-01-17 10:58:49 -03001248
James Hogan00942d12014-01-17 10:58:49 -03001249 ret = kstrtoul(buf, 0, &val);
1250 if (ret < 0)
1251 return ret;
1252
David Härdemanda6e1622014-04-03 20:32:16 -03001253 if (fattr->type == RC_FILTER_NORMAL) {
1254 set_filter = dev->s_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001255 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001256 } else {
1257 set_filter = dev->s_wakeup_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001258 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001259 }
1260
David Härdeman99b0f3c2014-04-04 19:06:06 -03001261 if (!set_filter)
1262 return -EINVAL;
James Hogan00942d12014-01-17 10:58:49 -03001263
1264 mutex_lock(&dev->lock);
1265
David Härdemanda6e1622014-04-03 20:32:16 -03001266 new_filter = *filter;
James Hogan00942d12014-01-17 10:58:49 -03001267 if (fattr->mask)
David Härdemanda6e1622014-04-03 20:32:16 -03001268 new_filter.mask = val;
James Hogan00942d12014-01-17 10:58:49 -03001269 else
David Härdemanda6e1622014-04-03 20:32:16 -03001270 new_filter.data = val;
David Härdeman23c843b2014-04-04 19:06:01 -03001271
Sean Young0751d332016-12-05 17:08:35 -02001272 if (fattr->type == RC_FILTER_WAKEUP) {
Sean Youngb590c0b2016-12-05 19:24:59 -02001273 /*
1274 * Refuse to set a filter unless a protocol is enabled
1275 * and the filter is valid for that protocol
1276 */
1277 if (dev->wakeup_protocol != RC_TYPE_UNKNOWN)
James Hoganf423ccc2015-03-31 14:48:10 -03001278 ret = rc_validate_filter(dev, &new_filter);
Sean Youngb590c0b2016-12-05 19:24:59 -02001279 else
Sean Young0751d332016-12-05 17:08:35 -02001280 ret = -EINVAL;
Sean Youngb590c0b2016-12-05 19:24:59 -02001281
1282 if (ret != 0)
Sean Young0751d332016-12-05 17:08:35 -02001283 goto unlock;
Sean Young0751d332016-12-05 17:08:35 -02001284 }
1285
1286 if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
1287 val) {
James Hogan6bea25a2014-02-28 20:17:06 -03001288 /* refuse to set a filter unless a protocol is enabled */
1289 ret = -EINVAL;
1290 goto unlock;
1291 }
David Härdeman23c843b2014-04-04 19:06:01 -03001292
David Härdemanda6e1622014-04-03 20:32:16 -03001293 ret = set_filter(dev, &new_filter);
David Härdeman99b0f3c2014-04-04 19:06:06 -03001294 if (ret < 0)
1295 goto unlock;
James Hogan00942d12014-01-17 10:58:49 -03001296
David Härdemanda6e1622014-04-03 20:32:16 -03001297 *filter = new_filter;
James Hogan00942d12014-01-17 10:58:49 -03001298
1299unlock:
1300 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001301 return (ret < 0) ? ret : len;
James Hogan00942d12014-01-17 10:58:49 -03001302}
1303
Sean Young0751d332016-12-05 17:08:35 -02001304/*
1305 * This is the list of all variants of all protocols, which is used by
1306 * the wakeup_protocols sysfs entry. In the protocols sysfs entry some
1307 * some protocols are grouped together (e.g. nec = nec + necx + nec32).
1308 *
1309 * For wakeup we need to know the exact protocol variant so the hardware
1310 * can be programmed exactly what to expect.
1311 */
1312static const char * const proto_variant_names[] = {
1313 [RC_TYPE_UNKNOWN] = "unknown",
1314 [RC_TYPE_OTHER] = "other",
1315 [RC_TYPE_RC5] = "rc-5",
1316 [RC_TYPE_RC5X_20] = "rc-5x-20",
1317 [RC_TYPE_RC5_SZ] = "rc-5-sz",
1318 [RC_TYPE_JVC] = "jvc",
1319 [RC_TYPE_SONY12] = "sony-12",
1320 [RC_TYPE_SONY15] = "sony-15",
1321 [RC_TYPE_SONY20] = "sony-20",
1322 [RC_TYPE_NEC] = "nec",
1323 [RC_TYPE_NECX] = "nec-x",
1324 [RC_TYPE_NEC32] = "nec-32",
1325 [RC_TYPE_SANYO] = "sanyo",
Sean Youngb73bc162017-02-11 20:33:38 -02001326 [RC_TYPE_MCIR2_KBD] = "mcir2-kbd",
1327 [RC_TYPE_MCIR2_MSE] = "mcir2-mse",
Sean Young0751d332016-12-05 17:08:35 -02001328 [RC_TYPE_RC6_0] = "rc-6-0",
1329 [RC_TYPE_RC6_6A_20] = "rc-6-6a-20",
1330 [RC_TYPE_RC6_6A_24] = "rc-6-6a-24",
1331 [RC_TYPE_RC6_6A_32] = "rc-6-6a-32",
1332 [RC_TYPE_RC6_MCE] = "rc-6-mce",
1333 [RC_TYPE_SHARP] = "sharp",
1334 [RC_TYPE_XMP] = "xmp",
1335 [RC_TYPE_CEC] = "cec",
1336};
1337
1338/**
1339 * show_wakeup_protocols() - shows the wakeup IR protocol
1340 * @device: the device descriptor
1341 * @mattr: the device attribute struct
1342 * @buf: a pointer to the output buffer
1343 *
1344 * This routine is a callback routine for input read the IR protocol type(s).
1345 * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols.
1346 * It returns the protocol names of supported protocols.
1347 * The enabled protocols are printed in brackets.
1348 *
David Härdeman18726a32017-04-27 17:34:08 -03001349 * dev->lock is taken to guard against races between
1350 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001351 */
1352static ssize_t show_wakeup_protocols(struct device *device,
1353 struct device_attribute *mattr,
1354 char *buf)
1355{
1356 struct rc_dev *dev = to_rc_dev(device);
1357 u64 allowed;
1358 enum rc_type enabled;
1359 char *tmp = buf;
1360 int i;
1361
Sean Young0751d332016-12-05 17:08:35 -02001362 mutex_lock(&dev->lock);
1363
1364 allowed = dev->allowed_wakeup_protocols;
1365 enabled = dev->wakeup_protocol;
1366
1367 mutex_unlock(&dev->lock);
1368
1369 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - %d\n",
1370 __func__, (long long)allowed, enabled);
1371
1372 for (i = 0; i < ARRAY_SIZE(proto_variant_names); i++) {
1373 if (allowed & (1ULL << i)) {
1374 if (i == enabled)
1375 tmp += sprintf(tmp, "[%s] ",
1376 proto_variant_names[i]);
1377 else
1378 tmp += sprintf(tmp, "%s ",
1379 proto_variant_names[i]);
1380 }
1381 }
1382
1383 if (tmp != buf)
1384 tmp--;
1385 *tmp = '\n';
1386
1387 return tmp + 1 - buf;
1388}
1389
1390/**
1391 * store_wakeup_protocols() - changes the wakeup IR protocol(s)
1392 * @device: the device descriptor
1393 * @mattr: the device attribute struct
1394 * @buf: a pointer to the input buffer
1395 * @len: length of the input buffer
1396 *
1397 * This routine is for changing the IR protocol type.
1398 * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols.
1399 * Returns @len on success or a negative error code.
1400 *
David Härdeman18726a32017-04-27 17:34:08 -03001401 * dev->lock is taken to guard against races between
1402 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001403 */
1404static ssize_t store_wakeup_protocols(struct device *device,
1405 struct device_attribute *mattr,
1406 const char *buf, size_t len)
1407{
1408 struct rc_dev *dev = to_rc_dev(device);
1409 enum rc_type protocol;
1410 ssize_t rc;
1411 u64 allowed;
1412 int i;
1413
Sean Young0751d332016-12-05 17:08:35 -02001414 mutex_lock(&dev->lock);
1415
1416 allowed = dev->allowed_wakeup_protocols;
1417
1418 if (sysfs_streq(buf, "none")) {
1419 protocol = RC_TYPE_UNKNOWN;
1420 } else {
1421 for (i = 0; i < ARRAY_SIZE(proto_variant_names); i++) {
1422 if ((allowed & (1ULL << i)) &&
1423 sysfs_streq(buf, proto_variant_names[i])) {
1424 protocol = i;
1425 break;
1426 }
1427 }
1428
1429 if (i == ARRAY_SIZE(proto_variant_names)) {
1430 rc = -EINVAL;
1431 goto out;
1432 }
James Hoganf423ccc2015-03-31 14:48:10 -03001433
1434 if (dev->encode_wakeup) {
1435 u64 mask = 1ULL << protocol;
1436
1437 ir_raw_load_modules(&mask);
1438 if (!mask) {
1439 rc = -EINVAL;
1440 goto out;
1441 }
1442 }
Sean Young0751d332016-12-05 17:08:35 -02001443 }
1444
1445 if (dev->wakeup_protocol != protocol) {
1446 dev->wakeup_protocol = protocol;
1447 IR_dprintk(1, "Wakeup protocol changed to %d\n", protocol);
1448
1449 if (protocol == RC_TYPE_RC6_MCE)
1450 dev->scancode_wakeup_filter.data = 0x800f0000;
1451 else
1452 dev->scancode_wakeup_filter.data = 0;
1453 dev->scancode_wakeup_filter.mask = 0;
1454
1455 rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
1456 if (rc == 0)
1457 rc = len;
1458 } else {
1459 rc = len;
1460 }
1461
1462out:
1463 mutex_unlock(&dev->lock);
1464 return rc;
1465}
1466
David Härdemand8b4b582010-10-29 16:08:23 -03001467static void rc_dev_release(struct device *device)
1468{
Max Kellermann47cae1e2016-03-21 08:33:05 -03001469 struct rc_dev *dev = to_rc_dev(device);
1470
1471 kfree(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001472}
1473
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001474#define ADD_HOTPLUG_VAR(fmt, val...) \
1475 do { \
1476 int err = add_uevent_var(env, fmt, val); \
1477 if (err) \
1478 return err; \
1479 } while (0)
1480
1481static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1482{
David Härdemand8b4b582010-10-29 16:08:23 -03001483 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001484
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001485 if (dev->rc_map.name)
1486 ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
David Härdemand8b4b582010-10-29 16:08:23 -03001487 if (dev->driver_name)
1488 ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001489
1490 return 0;
1491}
1492
1493/*
1494 * Static device attribute struct with the sysfs attributes for IR's
1495 */
Sean Young0751d332016-12-05 17:08:35 -02001496static DEVICE_ATTR(protocols, 0644, show_protocols, store_protocols);
1497static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
1498 store_wakeup_protocols);
James Hogan00942d12014-01-17 10:58:49 -03001499static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
1500 show_filter, store_filter, RC_FILTER_NORMAL, false);
1501static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
1502 show_filter, store_filter, RC_FILTER_NORMAL, true);
1503static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
1504 show_filter, store_filter, RC_FILTER_WAKEUP, false);
1505static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
1506 show_filter, store_filter, RC_FILTER_WAKEUP, true);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001507
David Härdeman99b0f3c2014-04-04 19:06:06 -03001508static struct attribute *rc_dev_protocol_attrs[] = {
Sean Young0751d332016-12-05 17:08:35 -02001509 &dev_attr_protocols.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001510 NULL,
1511};
1512
Arvind Yadavdb681022017-07-07 04:23:54 -04001513static const struct attribute_group rc_dev_protocol_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001514 .attrs = rc_dev_protocol_attrs,
1515};
1516
David Härdeman99b0f3c2014-04-04 19:06:06 -03001517static struct attribute *rc_dev_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001518 &dev_attr_filter.attr.attr,
1519 &dev_attr_filter_mask.attr.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001520 NULL,
1521};
1522
Arvind Yadavdb681022017-07-07 04:23:54 -04001523static const struct attribute_group rc_dev_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001524 .attrs = rc_dev_filter_attrs,
1525};
1526
1527static struct attribute *rc_dev_wakeup_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001528 &dev_attr_wakeup_filter.attr.attr,
1529 &dev_attr_wakeup_filter_mask.attr.attr,
Sean Young0751d332016-12-05 17:08:35 -02001530 &dev_attr_wakeup_protocols.attr,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001531 NULL,
1532};
1533
Arvind Yadavdb681022017-07-07 04:23:54 -04001534static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001535 .attrs = rc_dev_wakeup_filter_attrs,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001536};
1537
1538static struct device_type rc_dev_type = {
David Härdemand8b4b582010-10-29 16:08:23 -03001539 .release = rc_dev_release,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001540 .uevent = rc_dev_uevent,
1541};
1542
Andi Shyti0f7499f2016-12-16 06:50:58 -02001543struct rc_dev *rc_allocate_device(enum rc_driver_type type)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001544{
David Härdemand8b4b582010-10-29 16:08:23 -03001545 struct rc_dev *dev;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001546
David Härdemand8b4b582010-10-29 16:08:23 -03001547 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1548 if (!dev)
1549 return NULL;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001550
Andi Shytid34aee12016-12-16 04:12:15 -02001551 if (type != RC_DRIVER_IR_RAW_TX) {
1552 dev->input_dev = input_allocate_device();
1553 if (!dev->input_dev) {
1554 kfree(dev);
1555 return NULL;
1556 }
1557
1558 dev->input_dev->getkeycode = ir_getkeycode;
1559 dev->input_dev->setkeycode = ir_setkeycode;
1560 input_set_drvdata(dev->input_dev, dev);
1561
1562 setup_timer(&dev->timer_keyup, ir_timer_keyup,
1563 (unsigned long)dev);
1564
1565 spin_lock_init(&dev->rc_map.lock);
1566 spin_lock_init(&dev->keylock);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001567 }
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001568 mutex_init(&dev->lock);
David Härdemand8b4b582010-10-29 16:08:23 -03001569
1570 dev->dev.type = &rc_dev_type;
David Härdeman40fc5322013-03-06 16:52:10 -03001571 dev->dev.class = &rc_class;
David Härdemand8b4b582010-10-29 16:08:23 -03001572 device_initialize(&dev->dev);
1573
Andi Shyti0f7499f2016-12-16 06:50:58 -02001574 dev->driver_type = type;
1575
David Härdemand8b4b582010-10-29 16:08:23 -03001576 __module_get(THIS_MODULE);
1577 return dev;
1578}
1579EXPORT_SYMBOL_GPL(rc_allocate_device);
1580
1581void rc_free_device(struct rc_dev *dev)
1582{
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001583 if (!dev)
1584 return;
1585
Markus Elfring3dd94f02014-11-20 09:01:32 -03001586 input_free_device(dev->input_dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001587
1588 put_device(&dev->dev);
1589
Max Kellermann47cae1e2016-03-21 08:33:05 -03001590 /* kfree(dev) will be called by the callback function
1591 rc_dev_release() */
1592
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001593 module_put(THIS_MODULE);
David Härdemand8b4b582010-10-29 16:08:23 -03001594}
1595EXPORT_SYMBOL_GPL(rc_free_device);
1596
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001597static void devm_rc_alloc_release(struct device *dev, void *res)
1598{
1599 rc_free_device(*(struct rc_dev **)res);
1600}
1601
Andi Shyti0f7499f2016-12-16 06:50:58 -02001602struct rc_dev *devm_rc_allocate_device(struct device *dev,
1603 enum rc_driver_type type)
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001604{
1605 struct rc_dev **dr, *rc;
1606
1607 dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
1608 if (!dr)
1609 return NULL;
1610
Andi Shyti0f7499f2016-12-16 06:50:58 -02001611 rc = rc_allocate_device(type);
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001612 if (!rc) {
1613 devres_free(dr);
1614 return NULL;
1615 }
1616
1617 rc->dev.parent = dev;
1618 rc->managed_alloc = true;
1619 *dr = rc;
1620 devres_add(dev, dr);
1621
1622 return rc;
1623}
1624EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
1625
David Härdemanf56928a2017-05-03 07:04:00 -03001626static int rc_prepare_rx_device(struct rc_dev *dev)
David Härdemand8b4b582010-10-29 16:08:23 -03001627{
David Härdemanfcb13092015-05-19 19:03:17 -03001628 int rc;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001629 struct rc_map *rc_map;
Sean Young41380862017-02-22 18:48:01 -03001630 u64 rc_type;
David Härdemand8b4b582010-10-29 16:08:23 -03001631
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001632 if (!dev->map_name)
David Härdemand8b4b582010-10-29 16:08:23 -03001633 return -EINVAL;
1634
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001635 rc_map = rc_map_get(dev->map_name);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001636 if (!rc_map)
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001637 rc_map = rc_map_get(RC_MAP_EMPTY);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001638 if (!rc_map || !rc_map->scan || rc_map->size == 0)
David Härdemand8b4b582010-10-29 16:08:23 -03001639 return -EINVAL;
1640
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001641 rc = ir_setkeytable(dev, rc_map);
1642 if (rc)
1643 return rc;
1644
Sean Young41380862017-02-22 18:48:01 -03001645 rc_type = BIT_ULL(rc_map->rc_type);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001646
Sean Young41380862017-02-22 18:48:01 -03001647 if (dev->change_protocol) {
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001648 rc = dev->change_protocol(dev, &rc_type);
1649 if (rc < 0)
1650 goto out_table;
1651 dev->enabled_protocols = rc_type;
1652 }
1653
Sean Young41380862017-02-22 18:48:01 -03001654 if (dev->driver_type == RC_DRIVER_IR_RAW)
1655 ir_raw_load_modules(&rc_type);
1656
David Härdemand8b4b582010-10-29 16:08:23 -03001657 set_bit(EV_KEY, dev->input_dev->evbit);
1658 set_bit(EV_REP, dev->input_dev->evbit);
1659 set_bit(EV_MSC, dev->input_dev->evbit);
1660 set_bit(MSC_SCAN, dev->input_dev->mscbit);
1661 if (dev->open)
1662 dev->input_dev->open = ir_open;
1663 if (dev->close)
1664 dev->input_dev->close = ir_close;
1665
David Härdemanb2aceb72017-04-27 17:33:58 -03001666 dev->input_dev->dev.parent = &dev->dev;
1667 memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
1668 dev->input_dev->phys = dev->input_phys;
Sean Young518f4b22017-07-01 12:13:19 -04001669 dev->input_dev->name = dev->device_name;
David Härdemanb2aceb72017-04-27 17:33:58 -03001670
David Härdemanf56928a2017-05-03 07:04:00 -03001671 return 0;
1672
1673out_table:
1674 ir_free_table(&dev->rc_map);
1675
1676 return rc;
1677}
1678
1679static int rc_setup_rx_device(struct rc_dev *dev)
1680{
1681 int rc;
1682
David Härdemanb2aceb72017-04-27 17:33:58 -03001683 /* rc_open will be called here */
1684 rc = input_register_device(dev->input_dev);
1685 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001686 return rc;
David Härdemanb2aceb72017-04-27 17:33:58 -03001687
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001688 /*
1689 * Default delay of 250ms is too short for some protocols, especially
1690 * since the timeout is currently set to 250ms. Increase it to 500ms,
1691 * to avoid wrong repetition of the keycodes. Note that this must be
1692 * set after the call to input_register_device().
1693 */
1694 dev->input_dev->rep[REP_DELAY] = 500;
1695
1696 /*
1697 * As a repeat event on protocols like RC-5 and NEC take as long as
1698 * 110/114ms, using 33ms as a repeat period is not the right thing
1699 * to do.
1700 */
1701 dev->input_dev->rep[REP_PERIOD] = 125;
1702
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001703 return 0;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001704}
1705
1706static void rc_free_rx_device(struct rc_dev *dev)
1707{
David Härdemanf56928a2017-05-03 07:04:00 -03001708 if (!dev)
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001709 return;
1710
David Härdemanf56928a2017-05-03 07:04:00 -03001711 if (dev->input_dev) {
1712 input_unregister_device(dev->input_dev);
1713 dev->input_dev = NULL;
1714 }
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001715
David Härdemanf56928a2017-05-03 07:04:00 -03001716 ir_free_table(&dev->rc_map);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001717}
1718
1719int rc_register_device(struct rc_dev *dev)
1720{
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001721 const char *path;
1722 int attr = 0;
1723 int minor;
1724 int rc;
1725
1726 if (!dev)
1727 return -EINVAL;
1728
David Härdemanfcb13092015-05-19 19:03:17 -03001729 minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
1730 if (minor < 0)
1731 return minor;
1732
1733 dev->minor = minor;
1734 dev_set_name(&dev->dev, "rc%u", dev->minor);
1735 dev_set_drvdata(&dev->dev, dev);
Mauro Carvalho Chehab587d1b02014-01-14 16:27:55 -03001736
David Härdeman99b0f3c2014-04-04 19:06:06 -03001737 dev->dev.groups = dev->sysfs_groups;
Andi Shytid34aee12016-12-16 04:12:15 -02001738 if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
1739 dev->sysfs_groups[attr++] = &rc_dev_protocol_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001740 if (dev->s_filter)
David Härdeman120703f2014-04-03 20:31:30 -03001741 dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001742 if (dev->s_wakeup_filter)
1743 dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001744 dev->sysfs_groups[attr++] = NULL;
1745
David Härdemanf56928a2017-05-03 07:04:00 -03001746 if (dev->driver_type == RC_DRIVER_IR_RAW ||
1747 dev->driver_type == RC_DRIVER_IR_RAW_TX) {
1748 rc = ir_raw_event_prepare(dev);
1749 if (rc < 0)
1750 goto out_minor;
1751 }
1752
1753 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1754 rc = rc_prepare_rx_device(dev);
1755 if (rc)
1756 goto out_raw;
1757 }
1758
David Härdemand8b4b582010-10-29 16:08:23 -03001759 rc = device_add(&dev->dev);
1760 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001761 goto out_rx_free;
David Härdemand8b4b582010-10-29 16:08:23 -03001762
David Härdemand8b4b582010-10-29 16:08:23 -03001763 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Heiner Kallweit4dc0e902015-10-29 19:39:06 -02001764 dev_info(&dev->dev, "%s as %s\n",
Sean Young518f4b22017-07-01 12:13:19 -04001765 dev->device_name ?: "Unspecified device", path ?: "N/A");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001766 kfree(path);
1767
Sean Young5df62772017-02-23 06:11:21 -03001768 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1769 rc = rc_setup_rx_device(dev);
1770 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001771 goto out_dev;
1772 }
1773
1774 if (dev->driver_type == RC_DRIVER_IR_RAW ||
1775 dev->driver_type == RC_DRIVER_IR_RAW_TX) {
1776 rc = ir_raw_event_register(dev);
1777 if (rc < 0)
1778 goto out_rx;
David Härdemand8b4b582010-10-29 16:08:23 -03001779 }
1780
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001781 IR_dprintk(1, "Registered rc%u (driver: %s)\n",
David Härdemanfcb13092015-05-19 19:03:17 -03001782 dev->minor,
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001783 dev->driver_name ? dev->driver_name : "unknown");
David Härdemand8b4b582010-10-29 16:08:23 -03001784
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001785 return 0;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001786
David Härdemanf56928a2017-05-03 07:04:00 -03001787out_rx:
1788 rc_free_rx_device(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001789out_dev:
1790 device_del(&dev->dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001791out_rx_free:
1792 ir_free_table(&dev->rc_map);
1793out_raw:
1794 ir_raw_event_free(dev);
1795out_minor:
David Härdemanfcb13092015-05-19 19:03:17 -03001796 ida_simple_remove(&rc_ida, minor);
David Härdemand8b4b582010-10-29 16:08:23 -03001797 return rc;
1798}
1799EXPORT_SYMBOL_GPL(rc_register_device);
1800
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001801static void devm_rc_release(struct device *dev, void *res)
1802{
1803 rc_unregister_device(*(struct rc_dev **)res);
1804}
1805
1806int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
1807{
1808 struct rc_dev **dr;
1809 int ret;
1810
1811 dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
1812 if (!dr)
1813 return -ENOMEM;
1814
1815 ret = rc_register_device(dev);
1816 if (ret) {
1817 devres_free(dr);
1818 return ret;
1819 }
1820
1821 *dr = dev;
1822 devres_add(parent, dr);
1823
1824 return 0;
1825}
1826EXPORT_SYMBOL_GPL(devm_rc_register_device);
1827
David Härdemand8b4b582010-10-29 16:08:23 -03001828void rc_unregister_device(struct rc_dev *dev)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001829{
David Härdemand8b4b582010-10-29 16:08:23 -03001830 if (!dev)
1831 return;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001832
David Härdemand8b4b582010-10-29 16:08:23 -03001833 del_timer_sync(&dev->timer_keyup);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001834
David Härdemand8b4b582010-10-29 16:08:23 -03001835 if (dev->driver_type == RC_DRIVER_IR_RAW)
1836 ir_raw_event_unregister(dev);
1837
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001838 rc_free_rx_device(dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001839
1840 device_del(&dev->dev);
1841
David Härdemanfcb13092015-05-19 19:03:17 -03001842 ida_simple_remove(&rc_ida, dev->minor);
1843
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001844 if (!dev->managed_alloc)
1845 rc_free_device(dev);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001846}
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001847
David Härdemand8b4b582010-10-29 16:08:23 -03001848EXPORT_SYMBOL_GPL(rc_unregister_device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001849
1850/*
1851 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1852 */
1853
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001854static int __init rc_core_init(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001855{
David Härdeman40fc5322013-03-06 16:52:10 -03001856 int rc = class_register(&rc_class);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001857 if (rc) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -02001858 pr_err("rc_core: unable to register rc class\n");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001859 return rc;
1860 }
1861
Sean Young153a60b2013-07-30 19:00:01 -03001862 led_trigger_register_simple("rc-feedback", &led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001863 rc_map_register(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001864
1865 return 0;
1866}
1867
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001868static void __exit rc_core_exit(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001869{
David Härdeman40fc5322013-03-06 16:52:10 -03001870 class_unregister(&rc_class);
Sean Young153a60b2013-07-30 19:00:01 -03001871 led_trigger_unregister_simple(led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001872 rc_map_unregister(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001873}
1874
David Härdemane76d4ce2013-03-06 16:52:15 -03001875subsys_initcall(rc_core_init);
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001876module_exit(rc_core_exit);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001877
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001878int rc_core_debug; /* ir_debug level (0,1,2) */
1879EXPORT_SYMBOL_GPL(rc_core_debug);
1880module_param_named(debug, rc_core_debug, int, 0644);
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03001881
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02001882MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03001883MODULE_LICENSE("GPL");