blob: f306e67b8b660957951e2f549b2f0c31035a99e7 [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{
736 static u32 masks[] = {
737 [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
760 switch (protocol) {
761 case RC_TYPE_NECX:
762 if ((((s >> 16) ^ ~(s >> 8)) & 0xff) == 0)
763 return -EINVAL;
764 break;
765 case RC_TYPE_NEC32:
766 if ((((s >> 24) ^ ~(s >> 16)) & 0xff) == 0)
767 return -EINVAL;
768 break;
769 case RC_TYPE_RC6_MCE:
770 if ((s & 0xffff0000) != 0x800f0000)
771 return -EINVAL;
772 break;
773 case RC_TYPE_RC6_6A_32:
774 if ((s & 0xffff0000) == 0x800f0000)
775 return -EINVAL;
776 break;
777 default:
778 break;
779 }
780
781 filter->data &= masks[protocol];
782 filter->mask &= masks[protocol];
783
James Hoganf423ccc2015-03-31 14:48:10 -0300784 /*
785 * If we have to raw encode the IR for wakeup, we cannot have a mask
786 */
787 if (dev->encode_wakeup &&
788 filter->mask != 0 && filter->mask != masks[protocol])
789 return -EINVAL;
790
Sean Youngb590c0b2016-12-05 19:24:59 -0200791 return 0;
792}
793
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300794int rc_open(struct rc_dev *rdev)
795{
796 int rval = 0;
797
798 if (!rdev)
799 return -EINVAL;
800
801 mutex_lock(&rdev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -0200802
Juergen Lockf02dcdd2013-08-16 15:00:24 -0300803 if (!rdev->users++ && rdev->open != NULL)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300804 rval = rdev->open(rdev);
805
806 if (rval)
807 rdev->users--;
808
809 mutex_unlock(&rdev->lock);
810
811 return rval;
812}
813EXPORT_SYMBOL_GPL(rc_open);
814
David Härdemand8b4b582010-10-29 16:08:23 -0300815static int ir_open(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300816{
David Härdemand8b4b582010-10-29 16:08:23 -0300817 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300818
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300819 return rc_open(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300820}
821
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300822void rc_close(struct rc_dev *rdev)
823{
824 if (rdev) {
825 mutex_lock(&rdev->lock);
826
Mauro Carvalho Chehab81b7d142015-04-28 09:43:17 -0300827 if (!--rdev->users && rdev->close != NULL)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300828 rdev->close(rdev);
829
830 mutex_unlock(&rdev->lock);
831 }
832}
833EXPORT_SYMBOL_GPL(rc_close);
834
David Härdemand8b4b582010-10-29 16:08:23 -0300835static void ir_close(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300836{
David Härdemand8b4b582010-10-29 16:08:23 -0300837 struct rc_dev *rdev = input_get_drvdata(idev);
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300838 rc_close(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300839}
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300840
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300841/* class for /sys/class/rc */
David Härdeman40fc5322013-03-06 16:52:10 -0300842static char *rc_devnode(struct device *dev, umode_t *mode)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300843{
844 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
845}
846
David Härdeman40fc5322013-03-06 16:52:10 -0300847static struct class rc_class = {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300848 .name = "rc",
David Härdeman40fc5322013-03-06 16:52:10 -0300849 .devnode = rc_devnode,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300850};
851
David Härdemanc003ab12012-10-11 19:11:54 -0300852/*
853 * These are the protocol textual descriptions that are
854 * used by the sysfs protocols file. Note that the order
855 * of the entries is relevant.
856 */
Heiner Kallweit53df8772015-11-16 17:52:17 -0200857static const struct {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300858 u64 type;
Heiner Kallweit53df8772015-11-16 17:52:17 -0200859 const char *name;
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200860 const char *module_name;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300861} proto_names[] = {
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200862 { RC_BIT_NONE, "none", NULL },
863 { RC_BIT_OTHER, "other", NULL },
864 { RC_BIT_UNKNOWN, "unknown", NULL },
David Härdemanc003ab12012-10-11 19:11:54 -0300865 { RC_BIT_RC5 |
Sean Young0fcd3f02016-12-02 15:16:14 -0200866 RC_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
Sean Young2ceeca02016-09-21 06:54:19 -0300867 { RC_BIT_NEC |
868 RC_BIT_NECX |
869 RC_BIT_NEC32, "nec", "ir-nec-decoder" },
David Härdemanc003ab12012-10-11 19:11:54 -0300870 { RC_BIT_RC6_0 |
871 RC_BIT_RC6_6A_20 |
872 RC_BIT_RC6_6A_24 |
873 RC_BIT_RC6_6A_32 |
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200874 RC_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
875 { RC_BIT_JVC, "jvc", "ir-jvc-decoder" },
David Härdemanc003ab12012-10-11 19:11:54 -0300876 { RC_BIT_SONY12 |
877 RC_BIT_SONY15 |
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200878 RC_BIT_SONY20, "sony", "ir-sony-decoder" },
879 { RC_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
880 { RC_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
881 { RC_BIT_SHARP, "sharp", "ir-sharp-decoder" },
Sean Youngb73bc162017-02-11 20:33:38 -0200882 { RC_BIT_MCIR2_KBD |
883 RC_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" },
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200884 { RC_BIT_XMP, "xmp", "ir-xmp-decoder" },
Kamil Debskiff42c8a2015-08-17 08:47:41 -0300885 { RC_BIT_CEC, "cec", NULL },
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300886};
887
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300888/**
James Hoganab88c662014-02-28 20:17:05 -0300889 * struct rc_filter_attribute - Device attribute relating to a filter type.
890 * @attr: Device attribute.
891 * @type: Filter type.
892 * @mask: false for filter value, true for filter mask.
893 */
894struct rc_filter_attribute {
895 struct device_attribute attr;
896 enum rc_filter_type type;
897 bool mask;
898};
899#define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
900
James Hoganab88c662014-02-28 20:17:05 -0300901#define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
902 struct rc_filter_attribute dev_attr_##_name = { \
903 .attr = __ATTR(_name, _mode, _show, _store), \
904 .type = (_type), \
905 .mask = (_mask), \
906 }
907
David Härdemandd6ff6a2015-07-22 17:55:24 -0300908static bool lirc_is_present(void)
909{
910#if defined(CONFIG_LIRC_MODULE)
911 struct module *lirc;
912
913 mutex_lock(&module_mutex);
914 lirc = find_module("lirc_dev");
915 mutex_unlock(&module_mutex);
916
917 return lirc ? true : false;
918#elif defined(CONFIG_LIRC)
919 return true;
920#else
921 return false;
922#endif
923}
924
James Hoganab88c662014-02-28 20:17:05 -0300925/**
Sean Young0751d332016-12-05 17:08:35 -0200926 * show_protocols() - shows the current IR protocol(s)
David Härdemand8b4b582010-10-29 16:08:23 -0300927 * @device: the device descriptor
David Härdemanda6e1622014-04-03 20:32:16 -0300928 * @mattr: the device attribute struct
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300929 * @buf: a pointer to the output buffer
930 *
931 * This routine is a callback routine for input read the IR protocol type(s).
Sean Young0751d332016-12-05 17:08:35 -0200932 * it is trigged by reading /sys/class/rc/rc?/protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300933 * It returns the protocol names of supported protocols.
934 * Enabled protocols are printed in brackets.
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300935 *
David Härdeman18726a32017-04-27 17:34:08 -0300936 * dev->lock is taken to guard against races between
937 * store_protocols and show_protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300938 */
David Härdemand8b4b582010-10-29 16:08:23 -0300939static ssize_t show_protocols(struct device *device,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300940 struct device_attribute *mattr, char *buf)
941{
David Härdemand8b4b582010-10-29 16:08:23 -0300942 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300943 u64 allowed, enabled;
944 char *tmp = buf;
945 int i;
946
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300947 mutex_lock(&dev->lock);
948
Sean Young0751d332016-12-05 17:08:35 -0200949 enabled = dev->enabled_protocols;
950 allowed = dev->allowed_protocols;
951 if (dev->raw && !allowed)
952 allowed = ir_raw_get_allowed_protocols();
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300953
David Härdemanda6e1622014-04-03 20:32:16 -0300954 mutex_unlock(&dev->lock);
955
956 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
957 __func__, (long long)allowed, (long long)enabled);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300958
959 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
960 if (allowed & enabled & proto_names[i].type)
961 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
962 else if (allowed & proto_names[i].type)
963 tmp += sprintf(tmp, "%s ", proto_names[i].name);
David Härdemanc003ab12012-10-11 19:11:54 -0300964
965 if (allowed & proto_names[i].type)
966 allowed &= ~proto_names[i].type;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300967 }
968
David Härdemandd6ff6a2015-07-22 17:55:24 -0300969 if (dev->driver_type == RC_DRIVER_IR_RAW && lirc_is_present())
David Härdeman275ddb42015-05-19 19:03:22 -0300970 tmp += sprintf(tmp, "[lirc] ");
971
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300972 if (tmp != buf)
973 tmp--;
974 *tmp = '\n';
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300975
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300976 return tmp + 1 - buf;
977}
978
979/**
David Härdemanda6e1622014-04-03 20:32:16 -0300980 * parse_protocol_change() - parses a protocol change request
981 * @protocols: pointer to the bitmask of current protocols
982 * @buf: pointer to the buffer with a list of changes
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300983 *
David Härdemanda6e1622014-04-03 20:32:16 -0300984 * Writing "+proto" will add a protocol to the protocol mask.
985 * Writing "-proto" will remove a protocol from protocol mask.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300986 * Writing "proto" will enable only "proto".
987 * Writing "none" will disable all protocols.
David Härdemanda6e1622014-04-03 20:32:16 -0300988 * Returns the number of changes performed or a negative error code.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300989 */
David Härdemanda6e1622014-04-03 20:32:16 -0300990static int parse_protocol_change(u64 *protocols, const char *buf)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300991{
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300992 const char *tmp;
David Härdemanda6e1622014-04-03 20:32:16 -0300993 unsigned count = 0;
994 bool enable, disable;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300995 u64 mask;
David Härdemanda6e1622014-04-03 20:32:16 -0300996 int i;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300997
David Härdemanda6e1622014-04-03 20:32:16 -0300998 while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300999 if (!*tmp)
1000 break;
1001
1002 if (*tmp == '+') {
1003 enable = true;
1004 disable = false;
1005 tmp++;
1006 } else if (*tmp == '-') {
1007 enable = false;
1008 disable = true;
1009 tmp++;
1010 } else {
1011 enable = false;
1012 disable = false;
1013 }
1014
David Härdemanc003ab12012-10-11 19:11:54 -03001015 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1016 if (!strcasecmp(tmp, proto_names[i].name)) {
1017 mask = proto_names[i].type;
1018 break;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001019 }
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001020 }
1021
David Härdemanc003ab12012-10-11 19:11:54 -03001022 if (i == ARRAY_SIZE(proto_names)) {
David Härdeman275ddb42015-05-19 19:03:22 -03001023 if (!strcasecmp(tmp, "lirc"))
1024 mask = 0;
1025 else {
1026 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
1027 return -EINVAL;
1028 }
David Härdemanc003ab12012-10-11 19:11:54 -03001029 }
1030
1031 count++;
1032
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001033 if (enable)
David Härdemanda6e1622014-04-03 20:32:16 -03001034 *protocols |= mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001035 else if (disable)
David Härdemanda6e1622014-04-03 20:32:16 -03001036 *protocols &= ~mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001037 else
David Härdemanda6e1622014-04-03 20:32:16 -03001038 *protocols = mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001039 }
1040
1041 if (!count) {
1042 IR_dprintk(1, "Protocol not specified\n");
David Härdemanda6e1622014-04-03 20:32:16 -03001043 return -EINVAL;
1044 }
1045
1046 return count;
1047}
1048
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001049static void ir_raw_load_modules(u64 *protocols)
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001050{
1051 u64 available;
1052 int i, ret;
1053
1054 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1055 if (proto_names[i].type == RC_BIT_NONE ||
1056 proto_names[i].type & (RC_BIT_OTHER | RC_BIT_UNKNOWN))
1057 continue;
1058
1059 available = ir_raw_get_allowed_protocols();
1060 if (!(*protocols & proto_names[i].type & ~available))
1061 continue;
1062
1063 if (!proto_names[i].module_name) {
1064 pr_err("Can't enable IR protocol %s\n",
1065 proto_names[i].name);
1066 *protocols &= ~proto_names[i].type;
1067 continue;
1068 }
1069
1070 ret = request_module("%s", proto_names[i].module_name);
1071 if (ret < 0) {
1072 pr_err("Couldn't load IR protocol module %s\n",
1073 proto_names[i].module_name);
1074 *protocols &= ~proto_names[i].type;
1075 continue;
1076 }
1077 msleep(20);
1078 available = ir_raw_get_allowed_protocols();
1079 if (!(*protocols & proto_names[i].type & ~available))
1080 continue;
1081
Sean Young8caebcd2017-01-19 19:33:49 -02001082 pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001083 proto_names[i].module_name,
1084 proto_names[i].name);
1085 *protocols &= ~proto_names[i].type;
1086 }
1087}
1088
David Härdemanda6e1622014-04-03 20:32:16 -03001089/**
1090 * store_protocols() - changes the current/wakeup IR protocol(s)
1091 * @device: the device descriptor
1092 * @mattr: the device attribute struct
1093 * @buf: a pointer to the input buffer
1094 * @len: length of the input buffer
1095 *
1096 * This routine is for changing the IR protocol type.
1097 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
1098 * See parse_protocol_change() for the valid commands.
1099 * Returns @len on success or a negative error code.
1100 *
David Härdeman18726a32017-04-27 17:34:08 -03001101 * dev->lock is taken to guard against races between
1102 * store_protocols and show_protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001103 */
1104static ssize_t store_protocols(struct device *device,
1105 struct device_attribute *mattr,
1106 const char *buf, size_t len)
1107{
1108 struct rc_dev *dev = to_rc_dev(device);
David Härdemanda6e1622014-04-03 20:32:16 -03001109 u64 *current_protocols;
David Härdemanda6e1622014-04-03 20:32:16 -03001110 struct rc_scancode_filter *filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001111 u64 old_protocols, new_protocols;
1112 ssize_t rc;
1113
Sean Young0751d332016-12-05 17:08:35 -02001114 IR_dprintk(1, "Normal protocol change requested\n");
1115 current_protocols = &dev->enabled_protocols;
1116 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001117
Sean Young0751d332016-12-05 17:08:35 -02001118 if (!dev->change_protocol) {
David Härdemanda6e1622014-04-03 20:32:16 -03001119 IR_dprintk(1, "Protocol switching not supported\n");
1120 return -EINVAL;
1121 }
1122
1123 mutex_lock(&dev->lock);
1124
1125 old_protocols = *current_protocols;
1126 new_protocols = old_protocols;
1127 rc = parse_protocol_change(&new_protocols, buf);
1128 if (rc < 0)
1129 goto out;
1130
Sean Young0751d332016-12-05 17:08:35 -02001131 rc = dev->change_protocol(dev, &new_protocols);
David Härdemanda6e1622014-04-03 20:32:16 -03001132 if (rc < 0) {
1133 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
1134 (long long)new_protocols);
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001135 goto out;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001136 }
1137
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001138 if (dev->driver_type == RC_DRIVER_IR_RAW)
1139 ir_raw_load_modules(&new_protocols);
1140
James Hogan983c5bd2014-12-08 13:17:07 -03001141 if (new_protocols != old_protocols) {
1142 *current_protocols = new_protocols;
1143 IR_dprintk(1, "Protocols changed to 0x%llx\n",
1144 (long long)new_protocols);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001145 }
1146
James Hogan6bea25a2014-02-28 20:17:06 -03001147 /*
James Hogan983c5bd2014-12-08 13:17:07 -03001148 * If a protocol change was attempted the filter may need updating, even
1149 * if the actual protocol mask hasn't changed (since the driver may have
1150 * cleared the filter).
James Hogan6bea25a2014-02-28 20:17:06 -03001151 * Try setting the same filter with the new protocol (if any).
1152 * Fall back to clearing the filter.
1153 */
Sean Young0751d332016-12-05 17:08:35 -02001154 if (dev->s_filter && filter->mask) {
David Härdemanda6e1622014-04-03 20:32:16 -03001155 if (new_protocols)
Sean Young0751d332016-12-05 17:08:35 -02001156 rc = dev->s_filter(dev, filter);
David Härdemanda6e1622014-04-03 20:32:16 -03001157 else
1158 rc = -1;
David Härdeman23c843b2014-04-04 19:06:01 -03001159
David Härdemanda6e1622014-04-03 20:32:16 -03001160 if (rc < 0) {
1161 filter->data = 0;
1162 filter->mask = 0;
Sean Young0751d332016-12-05 17:08:35 -02001163 dev->s_filter(dev, filter);
James Hogan6bea25a2014-02-28 20:17:06 -03001164 }
James Hogan6bea25a2014-02-28 20:17:06 -03001165 }
1166
David Härdemanda6e1622014-04-03 20:32:16 -03001167 rc = len;
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001168
1169out:
1170 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001171 return rc;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001172}
1173
James Hogan00942d12014-01-17 10:58:49 -03001174/**
James Hogan00942d12014-01-17 10:58:49 -03001175 * show_filter() - shows the current scancode filter value or mask
1176 * @device: the device descriptor
1177 * @attr: the device attribute struct
1178 * @buf: a pointer to the output buffer
1179 *
1180 * This routine is a callback routine to read a scancode filter value or mask.
1181 * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
1182 * It prints the current scancode filter value or mask of the appropriate filter
1183 * type in hexadecimal into @buf and returns the size of the buffer.
1184 *
1185 * Bits of the filter value corresponding to set bits in the filter mask are
1186 * compared against input scancodes and non-matching scancodes are discarded.
1187 *
David Härdeman18726a32017-04-27 17:34:08 -03001188 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001189 * store_filter and show_filter.
1190 */
1191static ssize_t show_filter(struct device *device,
1192 struct device_attribute *attr,
1193 char *buf)
1194{
1195 struct rc_dev *dev = to_rc_dev(device);
1196 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001197 struct rc_scancode_filter *filter;
James Hogan00942d12014-01-17 10:58:49 -03001198 u32 val;
1199
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001200 mutex_lock(&dev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001201
David Härdemanda6e1622014-04-03 20:32:16 -03001202 if (fattr->type == RC_FILTER_NORMAL)
David Härdemanc5540fb2014-04-03 20:32:21 -03001203 filter = &dev->scancode_filter;
James Hogan00942d12014-01-17 10:58:49 -03001204 else
David Härdemanc5540fb2014-04-03 20:32:21 -03001205 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001206
David Härdemanda6e1622014-04-03 20:32:16 -03001207 if (fattr->mask)
1208 val = filter->mask;
1209 else
1210 val = filter->data;
James Hogan00942d12014-01-17 10:58:49 -03001211 mutex_unlock(&dev->lock);
1212
1213 return sprintf(buf, "%#x\n", val);
1214}
1215
1216/**
1217 * store_filter() - changes the scancode filter value
1218 * @device: the device descriptor
1219 * @attr: the device attribute struct
1220 * @buf: a pointer to the input buffer
1221 * @len: length of the input buffer
1222 *
1223 * This routine is for changing a scancode filter value or mask.
1224 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
1225 * Returns -EINVAL if an invalid filter value for the current protocol was
1226 * specified or if scancode filtering is not supported by the driver, otherwise
1227 * returns @len.
1228 *
1229 * Bits of the filter value corresponding to set bits in the filter mask are
1230 * compared against input scancodes and non-matching scancodes are discarded.
1231 *
David Härdeman18726a32017-04-27 17:34:08 -03001232 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001233 * store_filter and show_filter.
1234 */
1235static ssize_t store_filter(struct device *device,
1236 struct device_attribute *attr,
David Härdemanda6e1622014-04-03 20:32:16 -03001237 const char *buf, size_t len)
James Hogan00942d12014-01-17 10:58:49 -03001238{
1239 struct rc_dev *dev = to_rc_dev(device);
1240 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001241 struct rc_scancode_filter new_filter, *filter;
James Hogan00942d12014-01-17 10:58:49 -03001242 int ret;
1243 unsigned long val;
David Härdeman23c843b2014-04-04 19:06:01 -03001244 int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
James Hogan00942d12014-01-17 10:58:49 -03001245
James Hogan00942d12014-01-17 10:58:49 -03001246 ret = kstrtoul(buf, 0, &val);
1247 if (ret < 0)
1248 return ret;
1249
David Härdemanda6e1622014-04-03 20:32:16 -03001250 if (fattr->type == RC_FILTER_NORMAL) {
1251 set_filter = dev->s_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001252 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001253 } else {
1254 set_filter = dev->s_wakeup_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001255 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001256 }
1257
David Härdeman99b0f3c2014-04-04 19:06:06 -03001258 if (!set_filter)
1259 return -EINVAL;
James Hogan00942d12014-01-17 10:58:49 -03001260
1261 mutex_lock(&dev->lock);
1262
David Härdemanda6e1622014-04-03 20:32:16 -03001263 new_filter = *filter;
James Hogan00942d12014-01-17 10:58:49 -03001264 if (fattr->mask)
David Härdemanda6e1622014-04-03 20:32:16 -03001265 new_filter.mask = val;
James Hogan00942d12014-01-17 10:58:49 -03001266 else
David Härdemanda6e1622014-04-03 20:32:16 -03001267 new_filter.data = val;
David Härdeman23c843b2014-04-04 19:06:01 -03001268
Sean Young0751d332016-12-05 17:08:35 -02001269 if (fattr->type == RC_FILTER_WAKEUP) {
Sean Youngb590c0b2016-12-05 19:24:59 -02001270 /*
1271 * Refuse to set a filter unless a protocol is enabled
1272 * and the filter is valid for that protocol
1273 */
1274 if (dev->wakeup_protocol != RC_TYPE_UNKNOWN)
James Hoganf423ccc2015-03-31 14:48:10 -03001275 ret = rc_validate_filter(dev, &new_filter);
Sean Youngb590c0b2016-12-05 19:24:59 -02001276 else
Sean Young0751d332016-12-05 17:08:35 -02001277 ret = -EINVAL;
Sean Youngb590c0b2016-12-05 19:24:59 -02001278
1279 if (ret != 0)
Sean Young0751d332016-12-05 17:08:35 -02001280 goto unlock;
Sean Young0751d332016-12-05 17:08:35 -02001281 }
1282
1283 if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
1284 val) {
James Hogan6bea25a2014-02-28 20:17:06 -03001285 /* refuse to set a filter unless a protocol is enabled */
1286 ret = -EINVAL;
1287 goto unlock;
1288 }
David Härdeman23c843b2014-04-04 19:06:01 -03001289
David Härdemanda6e1622014-04-03 20:32:16 -03001290 ret = set_filter(dev, &new_filter);
David Härdeman99b0f3c2014-04-04 19:06:06 -03001291 if (ret < 0)
1292 goto unlock;
James Hogan00942d12014-01-17 10:58:49 -03001293
David Härdemanda6e1622014-04-03 20:32:16 -03001294 *filter = new_filter;
James Hogan00942d12014-01-17 10:58:49 -03001295
1296unlock:
1297 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001298 return (ret < 0) ? ret : len;
James Hogan00942d12014-01-17 10:58:49 -03001299}
1300
Sean Young0751d332016-12-05 17:08:35 -02001301/*
1302 * This is the list of all variants of all protocols, which is used by
1303 * the wakeup_protocols sysfs entry. In the protocols sysfs entry some
1304 * some protocols are grouped together (e.g. nec = nec + necx + nec32).
1305 *
1306 * For wakeup we need to know the exact protocol variant so the hardware
1307 * can be programmed exactly what to expect.
1308 */
1309static const char * const proto_variant_names[] = {
1310 [RC_TYPE_UNKNOWN] = "unknown",
1311 [RC_TYPE_OTHER] = "other",
1312 [RC_TYPE_RC5] = "rc-5",
1313 [RC_TYPE_RC5X_20] = "rc-5x-20",
1314 [RC_TYPE_RC5_SZ] = "rc-5-sz",
1315 [RC_TYPE_JVC] = "jvc",
1316 [RC_TYPE_SONY12] = "sony-12",
1317 [RC_TYPE_SONY15] = "sony-15",
1318 [RC_TYPE_SONY20] = "sony-20",
1319 [RC_TYPE_NEC] = "nec",
1320 [RC_TYPE_NECX] = "nec-x",
1321 [RC_TYPE_NEC32] = "nec-32",
1322 [RC_TYPE_SANYO] = "sanyo",
Sean Youngb73bc162017-02-11 20:33:38 -02001323 [RC_TYPE_MCIR2_KBD] = "mcir2-kbd",
1324 [RC_TYPE_MCIR2_MSE] = "mcir2-mse",
Sean Young0751d332016-12-05 17:08:35 -02001325 [RC_TYPE_RC6_0] = "rc-6-0",
1326 [RC_TYPE_RC6_6A_20] = "rc-6-6a-20",
1327 [RC_TYPE_RC6_6A_24] = "rc-6-6a-24",
1328 [RC_TYPE_RC6_6A_32] = "rc-6-6a-32",
1329 [RC_TYPE_RC6_MCE] = "rc-6-mce",
1330 [RC_TYPE_SHARP] = "sharp",
1331 [RC_TYPE_XMP] = "xmp",
1332 [RC_TYPE_CEC] = "cec",
1333};
1334
1335/**
1336 * show_wakeup_protocols() - shows the wakeup IR protocol
1337 * @device: the device descriptor
1338 * @mattr: the device attribute struct
1339 * @buf: a pointer to the output buffer
1340 *
1341 * This routine is a callback routine for input read the IR protocol type(s).
1342 * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols.
1343 * It returns the protocol names of supported protocols.
1344 * The enabled protocols are printed in brackets.
1345 *
David Härdeman18726a32017-04-27 17:34:08 -03001346 * dev->lock is taken to guard against races between
1347 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001348 */
1349static ssize_t show_wakeup_protocols(struct device *device,
1350 struct device_attribute *mattr,
1351 char *buf)
1352{
1353 struct rc_dev *dev = to_rc_dev(device);
1354 u64 allowed;
1355 enum rc_type enabled;
1356 char *tmp = buf;
1357 int i;
1358
Sean Young0751d332016-12-05 17:08:35 -02001359 mutex_lock(&dev->lock);
1360
1361 allowed = dev->allowed_wakeup_protocols;
1362 enabled = dev->wakeup_protocol;
1363
1364 mutex_unlock(&dev->lock);
1365
1366 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - %d\n",
1367 __func__, (long long)allowed, enabled);
1368
1369 for (i = 0; i < ARRAY_SIZE(proto_variant_names); i++) {
1370 if (allowed & (1ULL << i)) {
1371 if (i == enabled)
1372 tmp += sprintf(tmp, "[%s] ",
1373 proto_variant_names[i]);
1374 else
1375 tmp += sprintf(tmp, "%s ",
1376 proto_variant_names[i]);
1377 }
1378 }
1379
1380 if (tmp != buf)
1381 tmp--;
1382 *tmp = '\n';
1383
1384 return tmp + 1 - buf;
1385}
1386
1387/**
1388 * store_wakeup_protocols() - changes the wakeup IR protocol(s)
1389 * @device: the device descriptor
1390 * @mattr: the device attribute struct
1391 * @buf: a pointer to the input buffer
1392 * @len: length of the input buffer
1393 *
1394 * This routine is for changing the IR protocol type.
1395 * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols.
1396 * Returns @len on success or a negative error code.
1397 *
David Härdeman18726a32017-04-27 17:34:08 -03001398 * dev->lock is taken to guard against races between
1399 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001400 */
1401static ssize_t store_wakeup_protocols(struct device *device,
1402 struct device_attribute *mattr,
1403 const char *buf, size_t len)
1404{
1405 struct rc_dev *dev = to_rc_dev(device);
1406 enum rc_type protocol;
1407 ssize_t rc;
1408 u64 allowed;
1409 int i;
1410
Sean Young0751d332016-12-05 17:08:35 -02001411 mutex_lock(&dev->lock);
1412
1413 allowed = dev->allowed_wakeup_protocols;
1414
1415 if (sysfs_streq(buf, "none")) {
1416 protocol = RC_TYPE_UNKNOWN;
1417 } else {
1418 for (i = 0; i < ARRAY_SIZE(proto_variant_names); i++) {
1419 if ((allowed & (1ULL << i)) &&
1420 sysfs_streq(buf, proto_variant_names[i])) {
1421 protocol = i;
1422 break;
1423 }
1424 }
1425
1426 if (i == ARRAY_SIZE(proto_variant_names)) {
1427 rc = -EINVAL;
1428 goto out;
1429 }
James Hoganf423ccc2015-03-31 14:48:10 -03001430
1431 if (dev->encode_wakeup) {
1432 u64 mask = 1ULL << protocol;
1433
1434 ir_raw_load_modules(&mask);
1435 if (!mask) {
1436 rc = -EINVAL;
1437 goto out;
1438 }
1439 }
Sean Young0751d332016-12-05 17:08:35 -02001440 }
1441
1442 if (dev->wakeup_protocol != protocol) {
1443 dev->wakeup_protocol = protocol;
1444 IR_dprintk(1, "Wakeup protocol changed to %d\n", protocol);
1445
1446 if (protocol == RC_TYPE_RC6_MCE)
1447 dev->scancode_wakeup_filter.data = 0x800f0000;
1448 else
1449 dev->scancode_wakeup_filter.data = 0;
1450 dev->scancode_wakeup_filter.mask = 0;
1451
1452 rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
1453 if (rc == 0)
1454 rc = len;
1455 } else {
1456 rc = len;
1457 }
1458
1459out:
1460 mutex_unlock(&dev->lock);
1461 return rc;
1462}
1463
David Härdemand8b4b582010-10-29 16:08:23 -03001464static void rc_dev_release(struct device *device)
1465{
Max Kellermann47cae1e2016-03-21 08:33:05 -03001466 struct rc_dev *dev = to_rc_dev(device);
1467
1468 kfree(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001469}
1470
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001471#define ADD_HOTPLUG_VAR(fmt, val...) \
1472 do { \
1473 int err = add_uevent_var(env, fmt, val); \
1474 if (err) \
1475 return err; \
1476 } while (0)
1477
1478static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1479{
David Härdemand8b4b582010-10-29 16:08:23 -03001480 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001481
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001482 if (dev->rc_map.name)
1483 ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
David Härdemand8b4b582010-10-29 16:08:23 -03001484 if (dev->driver_name)
1485 ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001486
1487 return 0;
1488}
1489
1490/*
1491 * Static device attribute struct with the sysfs attributes for IR's
1492 */
Sean Young0751d332016-12-05 17:08:35 -02001493static DEVICE_ATTR(protocols, 0644, show_protocols, store_protocols);
1494static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
1495 store_wakeup_protocols);
James Hogan00942d12014-01-17 10:58:49 -03001496static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
1497 show_filter, store_filter, RC_FILTER_NORMAL, false);
1498static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
1499 show_filter, store_filter, RC_FILTER_NORMAL, true);
1500static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
1501 show_filter, store_filter, RC_FILTER_WAKEUP, false);
1502static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
1503 show_filter, store_filter, RC_FILTER_WAKEUP, true);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001504
David Härdeman99b0f3c2014-04-04 19:06:06 -03001505static struct attribute *rc_dev_protocol_attrs[] = {
Sean Young0751d332016-12-05 17:08:35 -02001506 &dev_attr_protocols.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001507 NULL,
1508};
1509
Arvind Yadavdb681022017-07-07 04:23:54 -04001510static const struct attribute_group rc_dev_protocol_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001511 .attrs = rc_dev_protocol_attrs,
1512};
1513
David Härdeman99b0f3c2014-04-04 19:06:06 -03001514static struct attribute *rc_dev_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001515 &dev_attr_filter.attr.attr,
1516 &dev_attr_filter_mask.attr.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001517 NULL,
1518};
1519
Arvind Yadavdb681022017-07-07 04:23:54 -04001520static const struct attribute_group rc_dev_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001521 .attrs = rc_dev_filter_attrs,
1522};
1523
1524static struct attribute *rc_dev_wakeup_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001525 &dev_attr_wakeup_filter.attr.attr,
1526 &dev_attr_wakeup_filter_mask.attr.attr,
Sean Young0751d332016-12-05 17:08:35 -02001527 &dev_attr_wakeup_protocols.attr,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001528 NULL,
1529};
1530
Arvind Yadavdb681022017-07-07 04:23:54 -04001531static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001532 .attrs = rc_dev_wakeup_filter_attrs,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001533};
1534
1535static struct device_type rc_dev_type = {
David Härdemand8b4b582010-10-29 16:08:23 -03001536 .release = rc_dev_release,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001537 .uevent = rc_dev_uevent,
1538};
1539
Andi Shyti0f7499f2016-12-16 06:50:58 -02001540struct rc_dev *rc_allocate_device(enum rc_driver_type type)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001541{
David Härdemand8b4b582010-10-29 16:08:23 -03001542 struct rc_dev *dev;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001543
David Härdemand8b4b582010-10-29 16:08:23 -03001544 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1545 if (!dev)
1546 return NULL;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001547
Andi Shytid34aee12016-12-16 04:12:15 -02001548 if (type != RC_DRIVER_IR_RAW_TX) {
1549 dev->input_dev = input_allocate_device();
1550 if (!dev->input_dev) {
1551 kfree(dev);
1552 return NULL;
1553 }
1554
1555 dev->input_dev->getkeycode = ir_getkeycode;
1556 dev->input_dev->setkeycode = ir_setkeycode;
1557 input_set_drvdata(dev->input_dev, dev);
1558
1559 setup_timer(&dev->timer_keyup, ir_timer_keyup,
1560 (unsigned long)dev);
1561
1562 spin_lock_init(&dev->rc_map.lock);
1563 spin_lock_init(&dev->keylock);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001564 }
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001565 mutex_init(&dev->lock);
David Härdemand8b4b582010-10-29 16:08:23 -03001566
1567 dev->dev.type = &rc_dev_type;
David Härdeman40fc5322013-03-06 16:52:10 -03001568 dev->dev.class = &rc_class;
David Härdemand8b4b582010-10-29 16:08:23 -03001569 device_initialize(&dev->dev);
1570
Andi Shyti0f7499f2016-12-16 06:50:58 -02001571 dev->driver_type = type;
1572
David Härdemand8b4b582010-10-29 16:08:23 -03001573 __module_get(THIS_MODULE);
1574 return dev;
1575}
1576EXPORT_SYMBOL_GPL(rc_allocate_device);
1577
1578void rc_free_device(struct rc_dev *dev)
1579{
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001580 if (!dev)
1581 return;
1582
Markus Elfring3dd94f02014-11-20 09:01:32 -03001583 input_free_device(dev->input_dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001584
1585 put_device(&dev->dev);
1586
Max Kellermann47cae1e2016-03-21 08:33:05 -03001587 /* kfree(dev) will be called by the callback function
1588 rc_dev_release() */
1589
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001590 module_put(THIS_MODULE);
David Härdemand8b4b582010-10-29 16:08:23 -03001591}
1592EXPORT_SYMBOL_GPL(rc_free_device);
1593
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001594static void devm_rc_alloc_release(struct device *dev, void *res)
1595{
1596 rc_free_device(*(struct rc_dev **)res);
1597}
1598
Andi Shyti0f7499f2016-12-16 06:50:58 -02001599struct rc_dev *devm_rc_allocate_device(struct device *dev,
1600 enum rc_driver_type type)
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001601{
1602 struct rc_dev **dr, *rc;
1603
1604 dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
1605 if (!dr)
1606 return NULL;
1607
Andi Shyti0f7499f2016-12-16 06:50:58 -02001608 rc = rc_allocate_device(type);
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001609 if (!rc) {
1610 devres_free(dr);
1611 return NULL;
1612 }
1613
1614 rc->dev.parent = dev;
1615 rc->managed_alloc = true;
1616 *dr = rc;
1617 devres_add(dev, dr);
1618
1619 return rc;
1620}
1621EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
1622
David Härdemanf56928a2017-05-03 07:04:00 -03001623static int rc_prepare_rx_device(struct rc_dev *dev)
David Härdemand8b4b582010-10-29 16:08:23 -03001624{
David Härdemanfcb13092015-05-19 19:03:17 -03001625 int rc;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001626 struct rc_map *rc_map;
Sean Young41380862017-02-22 18:48:01 -03001627 u64 rc_type;
David Härdemand8b4b582010-10-29 16:08:23 -03001628
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001629 if (!dev->map_name)
David Härdemand8b4b582010-10-29 16:08:23 -03001630 return -EINVAL;
1631
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001632 rc_map = rc_map_get(dev->map_name);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001633 if (!rc_map)
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001634 rc_map = rc_map_get(RC_MAP_EMPTY);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001635 if (!rc_map || !rc_map->scan || rc_map->size == 0)
David Härdemand8b4b582010-10-29 16:08:23 -03001636 return -EINVAL;
1637
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001638 rc = ir_setkeytable(dev, rc_map);
1639 if (rc)
1640 return rc;
1641
Sean Young41380862017-02-22 18:48:01 -03001642 rc_type = BIT_ULL(rc_map->rc_type);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001643
Sean Young41380862017-02-22 18:48:01 -03001644 if (dev->change_protocol) {
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001645 rc = dev->change_protocol(dev, &rc_type);
1646 if (rc < 0)
1647 goto out_table;
1648 dev->enabled_protocols = rc_type;
1649 }
1650
Sean Young41380862017-02-22 18:48:01 -03001651 if (dev->driver_type == RC_DRIVER_IR_RAW)
1652 ir_raw_load_modules(&rc_type);
1653
David Härdemand8b4b582010-10-29 16:08:23 -03001654 set_bit(EV_KEY, dev->input_dev->evbit);
1655 set_bit(EV_REP, dev->input_dev->evbit);
1656 set_bit(EV_MSC, dev->input_dev->evbit);
1657 set_bit(MSC_SCAN, dev->input_dev->mscbit);
1658 if (dev->open)
1659 dev->input_dev->open = ir_open;
1660 if (dev->close)
1661 dev->input_dev->close = ir_close;
1662
David Härdemanb2aceb72017-04-27 17:33:58 -03001663 dev->input_dev->dev.parent = &dev->dev;
1664 memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
1665 dev->input_dev->phys = dev->input_phys;
Sean Young518f4b22017-07-01 12:13:19 -04001666 dev->input_dev->name = dev->device_name;
David Härdemanb2aceb72017-04-27 17:33:58 -03001667
David Härdemanf56928a2017-05-03 07:04:00 -03001668 return 0;
1669
1670out_table:
1671 ir_free_table(&dev->rc_map);
1672
1673 return rc;
1674}
1675
1676static int rc_setup_rx_device(struct rc_dev *dev)
1677{
1678 int rc;
1679
David Härdemanb2aceb72017-04-27 17:33:58 -03001680 /* rc_open will be called here */
1681 rc = input_register_device(dev->input_dev);
1682 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001683 return rc;
David Härdemanb2aceb72017-04-27 17:33:58 -03001684
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001685 /*
1686 * Default delay of 250ms is too short for some protocols, especially
1687 * since the timeout is currently set to 250ms. Increase it to 500ms,
1688 * to avoid wrong repetition of the keycodes. Note that this must be
1689 * set after the call to input_register_device().
1690 */
1691 dev->input_dev->rep[REP_DELAY] = 500;
1692
1693 /*
1694 * As a repeat event on protocols like RC-5 and NEC take as long as
1695 * 110/114ms, using 33ms as a repeat period is not the right thing
1696 * to do.
1697 */
1698 dev->input_dev->rep[REP_PERIOD] = 125;
1699
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001700 return 0;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001701}
1702
1703static void rc_free_rx_device(struct rc_dev *dev)
1704{
David Härdemanf56928a2017-05-03 07:04:00 -03001705 if (!dev)
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001706 return;
1707
David Härdemanf56928a2017-05-03 07:04:00 -03001708 if (dev->input_dev) {
1709 input_unregister_device(dev->input_dev);
1710 dev->input_dev = NULL;
1711 }
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001712
David Härdemanf56928a2017-05-03 07:04:00 -03001713 ir_free_table(&dev->rc_map);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001714}
1715
1716int rc_register_device(struct rc_dev *dev)
1717{
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001718 const char *path;
1719 int attr = 0;
1720 int minor;
1721 int rc;
1722
1723 if (!dev)
1724 return -EINVAL;
1725
David Härdemanfcb13092015-05-19 19:03:17 -03001726 minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
1727 if (minor < 0)
1728 return minor;
1729
1730 dev->minor = minor;
1731 dev_set_name(&dev->dev, "rc%u", dev->minor);
1732 dev_set_drvdata(&dev->dev, dev);
Mauro Carvalho Chehab587d1b02014-01-14 16:27:55 -03001733
David Härdeman99b0f3c2014-04-04 19:06:06 -03001734 dev->dev.groups = dev->sysfs_groups;
Andi Shytid34aee12016-12-16 04:12:15 -02001735 if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
1736 dev->sysfs_groups[attr++] = &rc_dev_protocol_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001737 if (dev->s_filter)
David Härdeman120703f2014-04-03 20:31:30 -03001738 dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001739 if (dev->s_wakeup_filter)
1740 dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001741 dev->sysfs_groups[attr++] = NULL;
1742
David Härdemanf56928a2017-05-03 07:04:00 -03001743 if (dev->driver_type == RC_DRIVER_IR_RAW ||
1744 dev->driver_type == RC_DRIVER_IR_RAW_TX) {
1745 rc = ir_raw_event_prepare(dev);
1746 if (rc < 0)
1747 goto out_minor;
1748 }
1749
1750 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1751 rc = rc_prepare_rx_device(dev);
1752 if (rc)
1753 goto out_raw;
1754 }
1755
David Härdemand8b4b582010-10-29 16:08:23 -03001756 rc = device_add(&dev->dev);
1757 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001758 goto out_rx_free;
David Härdemand8b4b582010-10-29 16:08:23 -03001759
David Härdemand8b4b582010-10-29 16:08:23 -03001760 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Heiner Kallweit4dc0e902015-10-29 19:39:06 -02001761 dev_info(&dev->dev, "%s as %s\n",
Sean Young518f4b22017-07-01 12:13:19 -04001762 dev->device_name ?: "Unspecified device", path ?: "N/A");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001763 kfree(path);
1764
Sean Young5df62772017-02-23 06:11:21 -03001765 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1766 rc = rc_setup_rx_device(dev);
1767 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001768 goto out_dev;
1769 }
1770
1771 if (dev->driver_type == RC_DRIVER_IR_RAW ||
1772 dev->driver_type == RC_DRIVER_IR_RAW_TX) {
1773 rc = ir_raw_event_register(dev);
1774 if (rc < 0)
1775 goto out_rx;
David Härdemand8b4b582010-10-29 16:08:23 -03001776 }
1777
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001778 IR_dprintk(1, "Registered rc%u (driver: %s)\n",
David Härdemanfcb13092015-05-19 19:03:17 -03001779 dev->minor,
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001780 dev->driver_name ? dev->driver_name : "unknown");
David Härdemand8b4b582010-10-29 16:08:23 -03001781
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001782 return 0;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001783
David Härdemanf56928a2017-05-03 07:04:00 -03001784out_rx:
1785 rc_free_rx_device(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001786out_dev:
1787 device_del(&dev->dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001788out_rx_free:
1789 ir_free_table(&dev->rc_map);
1790out_raw:
1791 ir_raw_event_free(dev);
1792out_minor:
David Härdemanfcb13092015-05-19 19:03:17 -03001793 ida_simple_remove(&rc_ida, minor);
David Härdemand8b4b582010-10-29 16:08:23 -03001794 return rc;
1795}
1796EXPORT_SYMBOL_GPL(rc_register_device);
1797
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001798static void devm_rc_release(struct device *dev, void *res)
1799{
1800 rc_unregister_device(*(struct rc_dev **)res);
1801}
1802
1803int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
1804{
1805 struct rc_dev **dr;
1806 int ret;
1807
1808 dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
1809 if (!dr)
1810 return -ENOMEM;
1811
1812 ret = rc_register_device(dev);
1813 if (ret) {
1814 devres_free(dr);
1815 return ret;
1816 }
1817
1818 *dr = dev;
1819 devres_add(parent, dr);
1820
1821 return 0;
1822}
1823EXPORT_SYMBOL_GPL(devm_rc_register_device);
1824
David Härdemand8b4b582010-10-29 16:08:23 -03001825void rc_unregister_device(struct rc_dev *dev)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001826{
David Härdemand8b4b582010-10-29 16:08:23 -03001827 if (!dev)
1828 return;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001829
David Härdemand8b4b582010-10-29 16:08:23 -03001830 del_timer_sync(&dev->timer_keyup);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001831
David Härdemand8b4b582010-10-29 16:08:23 -03001832 if (dev->driver_type == RC_DRIVER_IR_RAW)
1833 ir_raw_event_unregister(dev);
1834
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001835 rc_free_rx_device(dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001836
1837 device_del(&dev->dev);
1838
David Härdemanfcb13092015-05-19 19:03:17 -03001839 ida_simple_remove(&rc_ida, dev->minor);
1840
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001841 if (!dev->managed_alloc)
1842 rc_free_device(dev);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001843}
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001844
David Härdemand8b4b582010-10-29 16:08:23 -03001845EXPORT_SYMBOL_GPL(rc_unregister_device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001846
1847/*
1848 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1849 */
1850
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001851static int __init rc_core_init(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001852{
David Härdeman40fc5322013-03-06 16:52:10 -03001853 int rc = class_register(&rc_class);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001854 if (rc) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -02001855 pr_err("rc_core: unable to register rc class\n");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001856 return rc;
1857 }
1858
Sean Young153a60b2013-07-30 19:00:01 -03001859 led_trigger_register_simple("rc-feedback", &led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001860 rc_map_register(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001861
1862 return 0;
1863}
1864
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001865static void __exit rc_core_exit(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001866{
David Härdeman40fc5322013-03-06 16:52:10 -03001867 class_unregister(&rc_class);
Sean Young153a60b2013-07-30 19:00:01 -03001868 led_trigger_unregister_simple(led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001869 rc_map_unregister(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001870}
1871
David Härdemane76d4ce2013-03-06 16:52:15 -03001872subsys_initcall(rc_core_init);
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001873module_exit(rc_core_exit);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001874
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001875int rc_core_debug; /* ir_debug level (0,1,2) */
1876EXPORT_SYMBOL_GPL(rc_core_debug);
1877module_param_named(debug, rc_core_debug, int, 0644);
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03001878
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02001879MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03001880MODULE_LICENSE("GPL");