blob: f2d3cc450d086e5bf20f88b7c606845d378e5cf0 [file] [log] [blame]
David Härdeman829ba9f2010-11-19 20:43:27 -03001/* rc-main.c - Remote Controller core module
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -03002 *
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02003 * Copyright (C) 2009-2010 by Mauro Carvalho Chehab
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -030013 */
14
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -020015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -030017#include <media/rc-core.h>
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030018#include <linux/spinlock.h>
19#include <linux/delay.h>
Mauro Carvalho Chehab882ead32009-12-29 10:37:38 -030020#include <linux/input.h>
Sean Young153a60b2013-07-30 19:00:01 -030021#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
David Härdemanfcb13092015-05-19 19:03:17 -030023#include <linux/idr.h>
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -030024#include <linux/device.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040025#include <linux/module.h>
Mauro Carvalho Chehabf62de672010-11-09 23:09:57 -030026#include "rc-core-priv.h"
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -030027
David Härdemanb3074c02010-04-02 15:58:28 -030028/* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */
29#define IR_TAB_MIN_SIZE 256
30#define IR_TAB_MAX_SIZE 8192
David Härdemanfcb13092015-05-19 19:03:17 -030031#define RC_DEV_MAX 256
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -030032
Sean Youngd57ea872017-08-09 13:19:16 -040033static const struct {
34 const char *name;
35 unsigned int repeat_period;
36 unsigned int scancode_bits;
37} protocols[] = {
38 [RC_TYPE_UNKNOWN] = { .name = "unknown", .repeat_period = 250 },
39 [RC_TYPE_OTHER] = { .name = "other", .repeat_period = 250 },
40 [RC_TYPE_RC5] = { .name = "rc-5",
41 .scancode_bits = 0x1f7f, .repeat_period = 164 },
42 [RC_TYPE_RC5X_20] = { .name = "rc-5x-20",
43 .scancode_bits = 0x1f7f3f, .repeat_period = 164 },
44 [RC_TYPE_RC5_SZ] = { .name = "rc-5-sz",
45 .scancode_bits = 0x2fff, .repeat_period = 164 },
46 [RC_TYPE_JVC] = { .name = "jvc",
47 .scancode_bits = 0xffff, .repeat_period = 250 },
48 [RC_TYPE_SONY12] = { .name = "sony-12",
49 .scancode_bits = 0x1f007f, .repeat_period = 100 },
50 [RC_TYPE_SONY15] = { .name = "sony-15",
51 .scancode_bits = 0xff007f, .repeat_period = 100 },
52 [RC_TYPE_SONY20] = { .name = "sony-20",
53 .scancode_bits = 0x1fff7f, .repeat_period = 100 },
54 [RC_TYPE_NEC] = { .name = "nec",
55 .scancode_bits = 0xffff, .repeat_period = 160 },
56 [RC_TYPE_NECX] = { .name = "nec-x",
57 .scancode_bits = 0xffffff, .repeat_period = 160 },
58 [RC_TYPE_NEC32] = { .name = "nec-32",
59 .scancode_bits = 0xffffffff, .repeat_period = 160 },
60 [RC_TYPE_SANYO] = { .name = "sanyo",
61 .scancode_bits = 0x1fffff, .repeat_period = 250 },
62 [RC_TYPE_MCIR2_KBD] = { .name = "mcir2-kbd",
63 .scancode_bits = 0xffff, .repeat_period = 150 },
64 [RC_TYPE_MCIR2_MSE] = { .name = "mcir2-mse",
65 .scancode_bits = 0x1fffff, .repeat_period = 150 },
66 [RC_TYPE_RC6_0] = { .name = "rc-6-0",
67 .scancode_bits = 0xffff, .repeat_period = 164 },
68 [RC_TYPE_RC6_6A_20] = { .name = "rc-6-6a-20",
69 .scancode_bits = 0xfffff, .repeat_period = 164 },
70 [RC_TYPE_RC6_6A_24] = { .name = "rc-6-6a-24",
71 .scancode_bits = 0xffffff, .repeat_period = 164 },
72 [RC_TYPE_RC6_6A_32] = { .name = "rc-6-6a-32",
73 .scancode_bits = 0xffffffff, .repeat_period = 164 },
74 [RC_TYPE_RC6_MCE] = { .name = "rc-6-mce",
75 .scancode_bits = 0xffff7fff, .repeat_period = 164 },
76 [RC_TYPE_SHARP] = { .name = "sharp",
77 .scancode_bits = 0x1fff, .repeat_period = 250 },
78 [RC_TYPE_XMP] = { .name = "xmp", .repeat_period = 250 },
79 [RC_TYPE_CEC] = { .name = "cec", .repeat_period = 550 },
80};
David Härdemana374fef2010-04-02 15:58:29 -030081
David Härdeman4c7b3552010-11-10 11:04:19 -030082/* Used to keep track of known keymaps */
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030083static LIST_HEAD(rc_map_list);
84static DEFINE_SPINLOCK(rc_map_lock);
Sean Young153a60b2013-07-30 19:00:01 -030085static struct led_trigger *led_feedback;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030086
David Härdemanfcb13092015-05-19 19:03:17 -030087/* Used to keep track of rc devices */
88static DEFINE_IDA(rc_ida);
89
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030090static struct rc_map_list *seek_rc_map(const char *name)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030091{
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -030092 struct rc_map_list *map = NULL;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -030093
94 spin_lock(&rc_map_lock);
95 list_for_each_entry(map, &rc_map_list, list) {
96 if (!strcmp(name, map->map.name)) {
97 spin_unlock(&rc_map_lock);
98 return map;
99 }
100 }
101 spin_unlock(&rc_map_lock);
102
103 return NULL;
104}
105
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300106struct rc_map *rc_map_get(const char *name)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300107{
108
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300109 struct rc_map_list *map;
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300110
111 map = seek_rc_map(name);
Russell King2ff56fa2015-10-15 13:15:24 -0300112#ifdef CONFIG_MODULES
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300113 if (!map) {
Kees Cook8ea54882014-03-11 17:25:53 -0300114 int rc = request_module("%s", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300115 if (rc < 0) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -0200116 pr_err("Couldn't load IR keymap %s\n", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300117 return NULL;
118 }
119 msleep(20); /* Give some time for IR to register */
120
121 map = seek_rc_map(name);
122 }
123#endif
124 if (!map) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -0200125 pr_err("IR keymap %s not found\n", name);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300126 return NULL;
127 }
128
129 printk(KERN_INFO "Registered IR keymap %s\n", map->map.name);
130
131 return &map->map;
132}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300133EXPORT_SYMBOL_GPL(rc_map_get);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300134
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300135int rc_map_register(struct rc_map_list *map)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300136{
137 spin_lock(&rc_map_lock);
138 list_add_tail(&map->list, &rc_map_list);
139 spin_unlock(&rc_map_lock);
140 return 0;
141}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300142EXPORT_SYMBOL_GPL(rc_map_register);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300143
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300144void rc_map_unregister(struct rc_map_list *map)
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300145{
146 spin_lock(&rc_map_lock);
147 list_del(&map->list);
148 spin_unlock(&rc_map_lock);
149}
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300150EXPORT_SYMBOL_GPL(rc_map_unregister);
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300151
152
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300153static struct rc_map_table empty[] = {
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300154 { 0x2a, KEY_COFFEE },
155};
156
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -0300157static struct rc_map_list empty_map = {
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300158 .map = {
159 .scan = empty,
160 .size = ARRAY_SIZE(empty),
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -0300161 .rc_type = RC_TYPE_UNKNOWN, /* Legacy IR type */
Mauro Carvalho Chehab631493e2010-11-09 23:44:27 -0300162 .name = RC_MAP_EMPTY,
163 }
164};
165
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300166/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700167 * ir_create_table() - initializes a scancode table
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300168 * @rc_map: the rc_map to initialize
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700169 * @name: name to assign to the table
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -0300170 * @rc_type: ir type to assign to the new table
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700171 * @size: initial size of the table
172 * @return: zero on success or a negative error code
173 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300174 * This routine will initialize the rc_map and will allocate
David Härdemand8b4b582010-10-29 16:08:23 -0300175 * memory to hold at least the specified number of elements.
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700176 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300177static int ir_create_table(struct rc_map *rc_map,
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -0300178 const char *name, u64 rc_type, size_t size)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700179{
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300180 rc_map->name = kstrdup(name, GFP_KERNEL);
181 if (!rc_map->name)
182 return -ENOMEM;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300183 rc_map->rc_type = rc_type;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300184 rc_map->alloc = roundup_pow_of_two(size * sizeof(struct rc_map_table));
185 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300186 rc_map->scan = kmalloc(rc_map->alloc, GFP_KERNEL);
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300187 if (!rc_map->scan) {
188 kfree(rc_map->name);
189 rc_map->name = NULL;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700190 return -ENOMEM;
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300191 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700192
193 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300194 rc_map->size, rc_map->alloc);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700195 return 0;
196}
197
198/**
199 * ir_free_table() - frees memory allocated by a scancode table
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300200 * @rc_map: the table whose mappings need to be freed
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700201 *
202 * This routine will free memory alloctaed for key mappings used by given
203 * scancode table.
204 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300205static void ir_free_table(struct rc_map *rc_map)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700206{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300207 rc_map->size = 0;
Hans Verkuild54fc3b2016-06-26 07:44:56 -0300208 kfree(rc_map->name);
Max Kellermannc183d352016-08-09 18:32:06 -0300209 rc_map->name = NULL;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300210 kfree(rc_map->scan);
211 rc_map->scan = NULL;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700212}
213
214/**
David Härdemanb3074c02010-04-02 15:58:28 -0300215 * ir_resize_table() - resizes a scancode table if necessary
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300216 * @rc_map: the rc_map to resize
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700217 * @gfp_flags: gfp flags to use when allocating memory
David Härdemanb3074c02010-04-02 15:58:28 -0300218 * @return: zero on success or a negative error code
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300219 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300220 * This routine will shrink the rc_map if it has lots of
David Härdemanb3074c02010-04-02 15:58:28 -0300221 * unused entries and grow it if it is full.
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300222 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300223static int ir_resize_table(struct rc_map *rc_map, gfp_t gfp_flags)
David Härdemanb3074c02010-04-02 15:58:28 -0300224{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300225 unsigned int oldalloc = rc_map->alloc;
David Härdemanb3074c02010-04-02 15:58:28 -0300226 unsigned int newalloc = oldalloc;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300227 struct rc_map_table *oldscan = rc_map->scan;
228 struct rc_map_table *newscan;
David Härdemanb3074c02010-04-02 15:58:28 -0300229
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300230 if (rc_map->size == rc_map->len) {
David Härdemanb3074c02010-04-02 15:58:28 -0300231 /* All entries in use -> grow keytable */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300232 if (rc_map->alloc >= IR_TAB_MAX_SIZE)
David Härdemanb3074c02010-04-02 15:58:28 -0300233 return -ENOMEM;
234
235 newalloc *= 2;
236 IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
237 }
238
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300239 if ((rc_map->len * 3 < rc_map->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
David Härdemanb3074c02010-04-02 15:58:28 -0300240 /* Less than 1/3 of entries in use -> shrink keytable */
241 newalloc /= 2;
242 IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
243 }
244
245 if (newalloc == oldalloc)
246 return 0;
247
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700248 newscan = kmalloc(newalloc, gfp_flags);
David Härdemanb3074c02010-04-02 15:58:28 -0300249 if (!newscan) {
250 IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
251 return -ENOMEM;
252 }
253
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300254 memcpy(newscan, rc_map->scan, rc_map->len * sizeof(struct rc_map_table));
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300255 rc_map->scan = newscan;
256 rc_map->alloc = newalloc;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300257 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
David Härdemanb3074c02010-04-02 15:58:28 -0300258 kfree(oldscan);
259 return 0;
260}
261
262/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700263 * ir_update_mapping() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300264 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300265 * @rc_map: scancode table to be adjusted
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700266 * @index: index of the mapping that needs to be updated
267 * @keycode: the desired keycode
268 * @return: previous keycode assigned to the mapping
David Härdemanb3074c02010-04-02 15:58:28 -0300269 *
David Härdemand8b4b582010-10-29 16:08:23 -0300270 * This routine is used to update scancode->keycode mapping at given
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700271 * position.
David Härdemanb3074c02010-04-02 15:58:28 -0300272 */
David Härdemand8b4b582010-10-29 16:08:23 -0300273static unsigned int ir_update_mapping(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300274 struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700275 unsigned int index,
276 unsigned int new_keycode)
277{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300278 int old_keycode = rc_map->scan[index].keycode;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700279 int i;
280
281 /* Did the user wish to remove the mapping? */
282 if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
283 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300284 index, rc_map->scan[index].scancode);
285 rc_map->len--;
286 memmove(&rc_map->scan[index], &rc_map->scan[index+ 1],
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300287 (rc_map->len - index) * sizeof(struct rc_map_table));
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700288 } else {
289 IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n",
290 index,
291 old_keycode == KEY_RESERVED ? "New" : "Replacing",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300292 rc_map->scan[index].scancode, new_keycode);
293 rc_map->scan[index].keycode = new_keycode;
David Härdemand8b4b582010-10-29 16:08:23 -0300294 __set_bit(new_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700295 }
296
297 if (old_keycode != KEY_RESERVED) {
298 /* A previous mapping was updated... */
David Härdemand8b4b582010-10-29 16:08:23 -0300299 __clear_bit(old_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700300 /* ... but another scancode might use the same keycode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300301 for (i = 0; i < rc_map->len; i++) {
302 if (rc_map->scan[i].keycode == old_keycode) {
David Härdemand8b4b582010-10-29 16:08:23 -0300303 __set_bit(old_keycode, dev->input_dev->keybit);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700304 break;
305 }
306 }
307
308 /* Possibly shrink the keytable, failure is not a problem */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300309 ir_resize_table(rc_map, GFP_ATOMIC);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700310 }
311
312 return old_keycode;
313}
314
315/**
David Härdeman4c7b3552010-11-10 11:04:19 -0300316 * ir_establish_scancode() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300317 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300318 * @rc_map: scancode table to be searched
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700319 * @scancode: the desired scancode
320 * @resize: controls whether we allowed to resize the table to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300321 * accommodate not yet present scancodes
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700322 * @return: index of the mapping containing scancode in question
323 * or -1U in case of failure.
324 *
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300325 * This routine is used to locate given scancode in rc_map.
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700326 * If scancode is not yet present the routine will allocate a new slot
327 * for it.
328 */
David Härdemand8b4b582010-10-29 16:08:23 -0300329static unsigned int ir_establish_scancode(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300330 struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700331 unsigned int scancode,
332 bool resize)
David Härdemanb3074c02010-04-02 15:58:28 -0300333{
334 unsigned int i;
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300335
336 /*
337 * Unfortunately, some hardware-based IR decoders don't provide
338 * all bits for the complete IR code. In general, they provide only
339 * the command part of the IR code. Yet, as it is possible to replace
340 * the provided IR with another one, it is needed to allow loading
David Härdemand8b4b582010-10-29 16:08:23 -0300341 * IR tables from other remotes. So, we support specifying a mask to
342 * indicate the valid bits of the scancodes.
Mauro Carvalho Chehab9dfe4e82010-04-04 14:06:55 -0300343 */
David Härdeman9d2f1d32014-04-03 20:32:26 -0300344 if (dev->scancode_mask)
345 scancode &= dev->scancode_mask;
David Härdemanb3074c02010-04-02 15:58:28 -0300346
347 /* First check if we already have a mapping for this ir command */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300348 for (i = 0; i < rc_map->len; i++) {
349 if (rc_map->scan[i].scancode == scancode)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700350 return i;
351
David Härdemanb3074c02010-04-02 15:58:28 -0300352 /* Keytable is sorted from lowest to highest scancode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300353 if (rc_map->scan[i].scancode >= scancode)
David Härdemanb3074c02010-04-02 15:58:28 -0300354 break;
David Härdemanb3074c02010-04-02 15:58:28 -0300355 }
356
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700357 /* No previous mapping found, we might need to grow the table */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300358 if (rc_map->size == rc_map->len) {
359 if (!resize || ir_resize_table(rc_map, GFP_ATOMIC))
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700360 return -1U;
361 }
David Härdemanb3074c02010-04-02 15:58:28 -0300362
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700363 /* i is the proper index to insert our new keycode */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300364 if (i < rc_map->len)
365 memmove(&rc_map->scan[i + 1], &rc_map->scan[i],
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300366 (rc_map->len - i) * sizeof(struct rc_map_table));
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300367 rc_map->scan[i].scancode = scancode;
368 rc_map->scan[i].keycode = KEY_RESERVED;
369 rc_map->len++;
David Härdemanb3074c02010-04-02 15:58:28 -0300370
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700371 return i;
David Härdemanb3074c02010-04-02 15:58:28 -0300372}
373
374/**
375 * ir_setkeycode() - set a keycode in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300376 * @idev: the struct input_dev device descriptor
David Härdemanb3074c02010-04-02 15:58:28 -0300377 * @scancode: the desired scancode
378 * @keycode: result
379 * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
380 *
381 * This routine is used to handle evdev EVIOCSKEY ioctl.
382 */
David Härdemand8b4b582010-10-29 16:08:23 -0300383static int ir_setkeycode(struct input_dev *idev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700384 const struct input_keymap_entry *ke,
385 unsigned int *old_keycode)
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300386{
David Härdemand8b4b582010-10-29 16:08:23 -0300387 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300388 struct rc_map *rc_map = &rdev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700389 unsigned int index;
390 unsigned int scancode;
Mauro Carvalho Chehabdea8a392010-11-29 07:46:13 -0300391 int retval = 0;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700392 unsigned long flags;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300393
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300394 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700395
396 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
397 index = ke->index;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300398 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700399 retval = -EINVAL;
400 goto out;
401 }
402 } else {
403 retval = input_scancode_to_scalar(ke, &scancode);
404 if (retval)
405 goto out;
406
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300407 index = ir_establish_scancode(rdev, rc_map, scancode, true);
408 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700409 retval = -ENOMEM;
410 goto out;
411 }
412 }
413
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300414 *old_keycode = ir_update_mapping(rdev, rc_map, index, ke->keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700415
416out:
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300417 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700418 return retval;
Mauro Carvalho Chehab7fee03e2009-12-02 15:56:47 -0300419}
420
421/**
David Härdemanb3074c02010-04-02 15:58:28 -0300422 * ir_setkeytable() - sets several entries in the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300423 * @dev: the struct rc_dev device descriptor
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300424 * @to: the struct rc_map to copy entries to
425 * @from: the struct rc_map to copy entries from
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700426 * @return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300427 *
David Härdemanb3074c02010-04-02 15:58:28 -0300428 * This routine is used to handle table initialization.
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300429 */
David Härdemand8b4b582010-10-29 16:08:23 -0300430static int ir_setkeytable(struct rc_dev *dev,
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300431 const struct rc_map *from)
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300432{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300433 struct rc_map *rc_map = &dev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700434 unsigned int i, index;
435 int rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300436
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300437 rc = ir_create_table(rc_map, from->name,
Mauro Carvalho Chehab52b66142010-11-17 14:20:52 -0300438 from->rc_type, from->size);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700439 if (rc)
440 return rc;
441
442 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300443 rc_map->size, rc_map->alloc);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700444
David Härdemanb3074c02010-04-02 15:58:28 -0300445 for (i = 0; i < from->size; i++) {
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300446 index = ir_establish_scancode(dev, rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700447 from->scan[i].scancode, false);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300448 if (index >= rc_map->len) {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700449 rc = -ENOMEM;
David Härdemanb3074c02010-04-02 15:58:28 -0300450 break;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700451 }
452
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300453 ir_update_mapping(dev, rc_map, index,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700454 from->scan[i].keycode);
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300455 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700456
457 if (rc)
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300458 ir_free_table(rc_map);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700459
David Härdemanb3074c02010-04-02 15:58:28 -0300460 return rc;
Mauro Carvalho Chehabf6fc5042009-11-29 11:08:02 -0300461}
462
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300463/**
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700464 * ir_lookup_by_scancode() - locate mapping by scancode
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300465 * @rc_map: the struct rc_map to search
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700466 * @scancode: scancode to look for in the table
467 * @return: index in the table, -1U if not found
468 *
469 * This routine performs binary search in RC keykeymap table for
470 * given scancode.
471 */
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300472static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700473 unsigned int scancode)
474{
David Härdeman0d070252010-10-30 22:17:44 +0200475 int start = 0;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300476 int end = rc_map->len - 1;
David Härdeman0d070252010-10-30 22:17:44 +0200477 int mid;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700478
479 while (start <= end) {
480 mid = (start + end) / 2;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300481 if (rc_map->scan[mid].scancode < scancode)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700482 start = mid + 1;
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300483 else if (rc_map->scan[mid].scancode > scancode)
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700484 end = mid - 1;
485 else
486 return mid;
487 }
488
489 return -1U;
490}
491
492/**
David Härdemanb3074c02010-04-02 15:58:28 -0300493 * ir_getkeycode() - get a keycode from the scancode->keycode table
David Härdemand8b4b582010-10-29 16:08:23 -0300494 * @idev: the struct input_dev device descriptor
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300495 * @scancode: the desired scancode
David Härdemanb3074c02010-04-02 15:58:28 -0300496 * @keycode: used to return the keycode, if found, or KEY_RESERVED
497 * @return: always returns zero.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300498 *
499 * This routine is used to handle evdev EVIOCGKEY ioctl.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300500 */
David Härdemand8b4b582010-10-29 16:08:23 -0300501static int ir_getkeycode(struct input_dev *idev,
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700502 struct input_keymap_entry *ke)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300503{
David Härdemand8b4b582010-10-29 16:08:23 -0300504 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300505 struct rc_map *rc_map = &rdev->rc_map;
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300506 struct rc_map_table *entry;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700507 unsigned long flags;
508 unsigned int index;
509 unsigned int scancode;
510 int retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300511
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300512 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700513
514 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
515 index = ke->index;
516 } else {
517 retval = input_scancode_to_scalar(ke, &scancode);
518 if (retval)
519 goto out;
520
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300521 index = ir_lookup_by_scancode(rc_map, scancode);
Mauro Carvalho Chehabe97f4672009-12-04 17:17:47 -0300522 }
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700523
Dmitry Torokhov54e74b82011-01-28 23:33:29 -0800524 if (index < rc_map->len) {
525 entry = &rc_map->scan[index];
526
527 ke->index = index;
528 ke->keycode = entry->keycode;
529 ke->len = sizeof(entry->scancode);
530 memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
531
532 } else if (!(ke->flags & INPUT_KEYMAP_BY_INDEX)) {
533 /*
534 * We do not really know the valid range of scancodes
535 * so let's respond with KEY_RESERVED to anything we
536 * do not have mapping for [yet].
537 */
538 ke->index = index;
539 ke->keycode = KEY_RESERVED;
540 } else {
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700541 retval = -EINVAL;
542 goto out;
543 }
544
Dmitry Torokhov47c5ba52010-10-31 15:18:42 -0700545 retval = 0;
546
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700547out:
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300548 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700549 return retval;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300550}
551
552/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300553 * rc_g_keycode_from_table() - gets the keycode that corresponds to a scancode
David Härdemand8b4b582010-10-29 16:08:23 -0300554 * @dev: the struct rc_dev descriptor of the device
555 * @scancode: the scancode to look for
556 * @return: the corresponding keycode, or KEY_RESERVED
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300557 *
David Härdemand8b4b582010-10-29 16:08:23 -0300558 * This routine is used by drivers which need to convert a scancode to a
559 * keycode. Normally it should not be used since drivers should have no
560 * interest in keycodes.
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300561 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300562u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode)
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300563{
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300564 struct rc_map *rc_map = &dev->rc_map;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700565 unsigned int keycode;
566 unsigned int index;
567 unsigned long flags;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300568
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300569 spin_lock_irqsave(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700570
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300571 index = ir_lookup_by_scancode(rc_map, scancode);
572 keycode = index < rc_map->len ?
573 rc_map->scan[index].keycode : KEY_RESERVED;
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700574
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -0300575 spin_unlock_irqrestore(&rc_map->lock, flags);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700576
Mauro Carvalho Chehab35438942010-04-03 16:53:16 -0300577 if (keycode != KEY_RESERVED)
578 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
Sean Young518f4b22017-07-01 12:13:19 -0400579 dev->device_name, scancode, keycode);
Dmitry Torokhov9f470092010-09-09 21:59:11 -0700580
David Härdemanb3074c02010-04-02 15:58:28 -0300581 return keycode;
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300582}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300583EXPORT_SYMBOL_GPL(rc_g_keycode_from_table);
Mauro Carvalho Chehabef53a112009-11-27 22:01:23 -0300584
585/**
David Härdeman62c65032010-10-29 16:08:07 -0300586 * ir_do_keyup() - internal function to signal the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300587 * @dev: the struct rc_dev descriptor of the device
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300588 * @sync: whether or not to call input_sync
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300589 *
David Härdeman62c65032010-10-29 16:08:07 -0300590 * This function is used internally to release a keypress, it must be
591 * called with keylock held.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300592 */
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300593static void ir_do_keyup(struct rc_dev *dev, bool sync)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300594{
David Härdemand8b4b582010-10-29 16:08:23 -0300595 if (!dev->keypressed)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300596 return;
597
David Härdemand8b4b582010-10-29 16:08:23 -0300598 IR_dprintk(1, "keyup key 0x%04x\n", dev->last_keycode);
599 input_report_key(dev->input_dev, dev->last_keycode, 0);
Sean Young153a60b2013-07-30 19:00:01 -0300600 led_trigger_event(led_feedback, LED_OFF);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300601 if (sync)
602 input_sync(dev->input_dev);
David Härdemand8b4b582010-10-29 16:08:23 -0300603 dev->keypressed = false;
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300604}
David Härdeman62c65032010-10-29 16:08:07 -0300605
606/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300607 * rc_keyup() - signals the release of a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300608 * @dev: the struct rc_dev descriptor of the device
David Härdeman62c65032010-10-29 16:08:07 -0300609 *
610 * This routine is used to signal that a key has been released on the
611 * remote control.
612 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300613void rc_keyup(struct rc_dev *dev)
David Härdeman62c65032010-10-29 16:08:07 -0300614{
615 unsigned long flags;
David Härdeman62c65032010-10-29 16:08:07 -0300616
David Härdemand8b4b582010-10-29 16:08:23 -0300617 spin_lock_irqsave(&dev->keylock, flags);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300618 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300619 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300620}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300621EXPORT_SYMBOL_GPL(rc_keyup);
David Härdemana374fef2010-04-02 15:58:29 -0300622
623/**
624 * ir_timer_keyup() - generates a keyup event after a timeout
David Härdemand8b4b582010-10-29 16:08:23 -0300625 * @cookie: a pointer to the struct rc_dev for the device
David Härdemana374fef2010-04-02 15:58:29 -0300626 *
627 * This routine will generate a keyup event some time after a keydown event
628 * is generated when no further activity has been detected.
629 */
630static void ir_timer_keyup(unsigned long cookie)
631{
David Härdemand8b4b582010-10-29 16:08:23 -0300632 struct rc_dev *dev = (struct rc_dev *)cookie;
David Härdemana374fef2010-04-02 15:58:29 -0300633 unsigned long flags;
634
635 /*
636 * ir->keyup_jiffies is used to prevent a race condition if a
637 * hardware interrupt occurs at this point and the keyup timer
638 * event is moved further into the future as a result.
639 *
640 * The timer will then be reactivated and this function called
641 * again in the future. We need to exit gracefully in that case
642 * to allow the input subsystem to do its auto-repeat magic or
643 * a keyup event might follow immediately after the keydown.
644 */
David Härdemand8b4b582010-10-29 16:08:23 -0300645 spin_lock_irqsave(&dev->keylock, flags);
646 if (time_is_before_eq_jiffies(dev->keyup_jiffies))
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300647 ir_do_keyup(dev, true);
David Härdemand8b4b582010-10-29 16:08:23 -0300648 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300649}
650
651/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300652 * rc_repeat() - signals that a key is still pressed
David Härdemand8b4b582010-10-29 16:08:23 -0300653 * @dev: the struct rc_dev descriptor of the device
David Härdemana374fef2010-04-02 15:58:29 -0300654 *
655 * This routine is used by IR decoders when a repeat message which does
656 * not include the necessary bits to reproduce the scancode has been
657 * received.
658 */
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300659void rc_repeat(struct rc_dev *dev)
David Härdemana374fef2010-04-02 15:58:29 -0300660{
661 unsigned long flags;
Sean Youngd57ea872017-08-09 13:19:16 -0400662 unsigned int timeout = protocols[dev->last_protocol].repeat_period;
David Härdemana374fef2010-04-02 15:58:29 -0300663
David Härdemand8b4b582010-10-29 16:08:23 -0300664 spin_lock_irqsave(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300665
David Härdemand8b4b582010-10-29 16:08:23 -0300666 if (!dev->keypressed)
David Härdemana374fef2010-04-02 15:58:29 -0300667 goto out;
668
David Härdeman265a2982017-06-22 15:23:54 -0400669 input_event(dev->input_dev, EV_MSC, MSC_SCAN, dev->last_scancode);
670 input_sync(dev->input_dev);
671
Sean Youngd57ea872017-08-09 13:19:16 -0400672 dev->keyup_jiffies = jiffies + msecs_to_jiffies(timeout);
David Härdemand8b4b582010-10-29 16:08:23 -0300673 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
David Härdemana374fef2010-04-02 15:58:29 -0300674
675out:
David Härdemand8b4b582010-10-29 16:08:23 -0300676 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdemana374fef2010-04-02 15:58:29 -0300677}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300678EXPORT_SYMBOL_GPL(rc_repeat);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300679
680/**
David Härdeman62c65032010-10-29 16:08:07 -0300681 * ir_do_keydown() - internal function to process a keypress
David Härdemand8b4b582010-10-29 16:08:23 -0300682 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300683 * @protocol: the protocol of the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300684 * @scancode: the scancode of the keypress
685 * @keycode: the keycode of the keypress
686 * @toggle: the toggle value of the keypress
687 *
688 * This function is used internally to register a keypress, it must be
689 * called with keylock held.
690 */
David Härdeman120703f2014-04-03 20:31:30 -0300691static void ir_do_keydown(struct rc_dev *dev, enum rc_type protocol,
692 u32 scancode, u32 keycode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300693{
David Härdeman99b0f3c2014-04-04 19:06:06 -0300694 bool new_event = (!dev->keypressed ||
David Härdeman120703f2014-04-03 20:31:30 -0300695 dev->last_protocol != protocol ||
David Härdeman99b0f3c2014-04-04 19:06:06 -0300696 dev->last_scancode != scancode ||
David Härdeman120703f2014-04-03 20:31:30 -0300697 dev->last_toggle != toggle);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300698
699 if (new_event && dev->keypressed)
700 ir_do_keyup(dev, false);
701
David Härdemand8b4b582010-10-29 16:08:23 -0300702 input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300703
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300704 if (new_event && keycode != KEY_RESERVED) {
705 /* Register a keypress */
706 dev->keypressed = true;
David Härdeman120703f2014-04-03 20:31:30 -0300707 dev->last_protocol = protocol;
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300708 dev->last_scancode = scancode;
709 dev->last_toggle = toggle;
710 dev->last_keycode = keycode;
David Härdeman62c65032010-10-29 16:08:07 -0300711
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200712 IR_dprintk(1, "%s: key down event, key 0x%04x, protocol 0x%04x, scancode 0x%08x\n",
Sean Young518f4b22017-07-01 12:13:19 -0400713 dev->device_name, keycode, protocol, scancode);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300714 input_report_key(dev->input_dev, keycode, 1);
James Hogan70a2f912014-01-16 19:56:22 -0300715
716 led_trigger_event(led_feedback, LED_FULL);
Jarod Wilson98c32bc2011-06-23 10:40:55 -0300717 }
David Härdeman62c65032010-10-29 16:08:07 -0300718
David Härdemand8b4b582010-10-29 16:08:23 -0300719 input_sync(dev->input_dev);
David Härdeman62c65032010-10-29 16:08:07 -0300720}
721
722/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300723 * rc_keydown() - generates input event for a key press
David Härdemand8b4b582010-10-29 16:08:23 -0300724 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300725 * @protocol: the protocol for the keypress
726 * @scancode: the scancode for the keypress
David Härdemana374fef2010-04-02 15:58:29 -0300727 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
728 * support toggle values, this should be set to zero)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300729 *
David Härdemand8b4b582010-10-29 16:08:23 -0300730 * This routine is used to signal that a key has been pressed on the
731 * remote control.
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300732 */
David Härdeman120703f2014-04-03 20:31:30 -0300733void rc_keydown(struct rc_dev *dev, enum rc_type protocol, u32 scancode, u8 toggle)
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300734{
David Härdemana374fef2010-04-02 15:58:29 -0300735 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300736 u32 keycode = rc_g_keycode_from_table(dev, scancode);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300737
David Härdemand8b4b582010-10-29 16:08:23 -0300738 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300739 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300740
David Härdemand8b4b582010-10-29 16:08:23 -0300741 if (dev->keypressed) {
Sean Youngd57ea872017-08-09 13:19:16 -0400742 dev->keyup_jiffies = jiffies +
743 msecs_to_jiffies(protocols[protocol].repeat_period);
David Härdemand8b4b582010-10-29 16:08:23 -0300744 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
David Härdeman62c65032010-10-29 16:08:07 -0300745 }
David Härdemand8b4b582010-10-29 16:08:23 -0300746 spin_unlock_irqrestore(&dev->keylock, flags);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300747}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300748EXPORT_SYMBOL_GPL(rc_keydown);
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300749
David Härdeman62c65032010-10-29 16:08:07 -0300750/**
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300751 * rc_keydown_notimeout() - generates input event for a key press without
David Härdeman62c65032010-10-29 16:08:07 -0300752 * an automatic keyup event at a later time
David Härdemand8b4b582010-10-29 16:08:23 -0300753 * @dev: the struct rc_dev descriptor of the device
David Härdeman120703f2014-04-03 20:31:30 -0300754 * @protocol: the protocol for the keypress
755 * @scancode: the scancode for the keypress
David Härdeman62c65032010-10-29 16:08:07 -0300756 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
757 * support toggle values, this should be set to zero)
758 *
David Härdemand8b4b582010-10-29 16:08:23 -0300759 * This routine is used to signal that a key has been pressed on the
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300760 * remote control. The driver must manually call rc_keyup() at a later stage.
David Härdeman62c65032010-10-29 16:08:07 -0300761 */
David Härdeman120703f2014-04-03 20:31:30 -0300762void rc_keydown_notimeout(struct rc_dev *dev, enum rc_type protocol,
763 u32 scancode, u8 toggle)
David Härdeman62c65032010-10-29 16:08:07 -0300764{
765 unsigned long flags;
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300766 u32 keycode = rc_g_keycode_from_table(dev, scancode);
David Härdeman62c65032010-10-29 16:08:07 -0300767
David Härdemand8b4b582010-10-29 16:08:23 -0300768 spin_lock_irqsave(&dev->keylock, flags);
David Härdeman120703f2014-04-03 20:31:30 -0300769 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
David Härdemand8b4b582010-10-29 16:08:23 -0300770 spin_unlock_irqrestore(&dev->keylock, flags);
David Härdeman62c65032010-10-29 16:08:07 -0300771}
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300772EXPORT_SYMBOL_GPL(rc_keydown_notimeout);
David Härdeman62c65032010-10-29 16:08:07 -0300773
Sean Youngb590c0b2016-12-05 19:24:59 -0200774/**
775 * rc_validate_filter() - checks that the scancode and mask are valid and
776 * provides sensible defaults
James Hoganf423ccc2015-03-31 14:48:10 -0300777 * @dev: the struct rc_dev descriptor of the device
Sean Youngb590c0b2016-12-05 19:24:59 -0200778 * @filter: the scancode and mask
779 * @return: 0 or -EINVAL if the filter is not valid
780 */
James Hoganf423ccc2015-03-31 14:48:10 -0300781static int rc_validate_filter(struct rc_dev *dev,
Sean Youngb590c0b2016-12-05 19:24:59 -0200782 struct rc_scancode_filter *filter)
783{
Sean Youngd57ea872017-08-09 13:19:16 -0400784 u32 mask, s = filter->data;
James Hoganf423ccc2015-03-31 14:48:10 -0300785 enum rc_type protocol = dev->wakeup_protocol;
Sean Youngb590c0b2016-12-05 19:24:59 -0200786
Sean Youngd57ea872017-08-09 13:19:16 -0400787 if (protocol >= ARRAY_SIZE(protocols))
Sean Young2168b412017-08-07 09:21:29 -0400788 return -EINVAL;
789
Sean Youngd57ea872017-08-09 13:19:16 -0400790 mask = protocols[protocol].scancode_bits;
791
Sean Youngb590c0b2016-12-05 19:24:59 -0200792 switch (protocol) {
793 case RC_TYPE_NECX:
794 if ((((s >> 16) ^ ~(s >> 8)) & 0xff) == 0)
795 return -EINVAL;
796 break;
797 case RC_TYPE_NEC32:
798 if ((((s >> 24) ^ ~(s >> 16)) & 0xff) == 0)
799 return -EINVAL;
800 break;
801 case RC_TYPE_RC6_MCE:
802 if ((s & 0xffff0000) != 0x800f0000)
803 return -EINVAL;
804 break;
805 case RC_TYPE_RC6_6A_32:
806 if ((s & 0xffff0000) == 0x800f0000)
807 return -EINVAL;
808 break;
809 default:
810 break;
811 }
812
Sean Youngd57ea872017-08-09 13:19:16 -0400813 filter->data &= mask;
814 filter->mask &= mask;
Sean Youngb590c0b2016-12-05 19:24:59 -0200815
James Hoganf423ccc2015-03-31 14:48:10 -0300816 /*
817 * If we have to raw encode the IR for wakeup, we cannot have a mask
818 */
Sean Youngd57ea872017-08-09 13:19:16 -0400819 if (dev->encode_wakeup && filter->mask != 0 && filter->mask != mask)
James Hoganf423ccc2015-03-31 14:48:10 -0300820 return -EINVAL;
821
Sean Youngb590c0b2016-12-05 19:24:59 -0200822 return 0;
823}
824
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300825int rc_open(struct rc_dev *rdev)
826{
827 int rval = 0;
828
829 if (!rdev)
830 return -EINVAL;
831
832 mutex_lock(&rdev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -0200833
Juergen Lockf02dcdd2013-08-16 15:00:24 -0300834 if (!rdev->users++ && rdev->open != NULL)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300835 rval = rdev->open(rdev);
836
837 if (rval)
838 rdev->users--;
839
840 mutex_unlock(&rdev->lock);
841
842 return rval;
843}
844EXPORT_SYMBOL_GPL(rc_open);
845
David Härdemand8b4b582010-10-29 16:08:23 -0300846static int ir_open(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300847{
David Härdemand8b4b582010-10-29 16:08:23 -0300848 struct rc_dev *rdev = input_get_drvdata(idev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300849
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300850 return rc_open(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300851}
852
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300853void rc_close(struct rc_dev *rdev)
854{
855 if (rdev) {
856 mutex_lock(&rdev->lock);
857
Mauro Carvalho Chehab81b7d142015-04-28 09:43:17 -0300858 if (!--rdev->users && rdev->close != NULL)
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300859 rdev->close(rdev);
860
861 mutex_unlock(&rdev->lock);
862 }
863}
864EXPORT_SYMBOL_GPL(rc_close);
865
David Härdemand8b4b582010-10-29 16:08:23 -0300866static void ir_close(struct input_dev *idev)
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300867{
David Härdemand8b4b582010-10-29 16:08:23 -0300868 struct rc_dev *rdev = input_get_drvdata(idev);
Srinivas Kandagatla8b2ff322013-07-22 04:22:57 -0300869 rc_close(rdev);
Mauro Carvalho Chehab716aab42010-03-31 14:40:35 -0300870}
Mauro Carvalho Chehab6660de52010-03-21 12:15:16 -0300871
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300872/* class for /sys/class/rc */
David Härdeman40fc5322013-03-06 16:52:10 -0300873static char *rc_devnode(struct device *dev, umode_t *mode)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300874{
875 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
876}
877
David Härdeman40fc5322013-03-06 16:52:10 -0300878static struct class rc_class = {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300879 .name = "rc",
David Härdeman40fc5322013-03-06 16:52:10 -0300880 .devnode = rc_devnode,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300881};
882
David Härdemanc003ab12012-10-11 19:11:54 -0300883/*
884 * These are the protocol textual descriptions that are
885 * used by the sysfs protocols file. Note that the order
886 * of the entries is relevant.
887 */
Heiner Kallweit53df8772015-11-16 17:52:17 -0200888static const struct {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300889 u64 type;
Heiner Kallweit53df8772015-11-16 17:52:17 -0200890 const char *name;
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200891 const char *module_name;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300892} proto_names[] = {
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200893 { RC_BIT_NONE, "none", NULL },
894 { RC_BIT_OTHER, "other", NULL },
895 { RC_BIT_UNKNOWN, "unknown", NULL },
David Härdemanc003ab12012-10-11 19:11:54 -0300896 { RC_BIT_RC5 |
Sean Young0fcd3f02016-12-02 15:16:14 -0200897 RC_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
Sean Young2ceeca02016-09-21 06:54:19 -0300898 { RC_BIT_NEC |
899 RC_BIT_NECX |
900 RC_BIT_NEC32, "nec", "ir-nec-decoder" },
David Härdemanc003ab12012-10-11 19:11:54 -0300901 { RC_BIT_RC6_0 |
902 RC_BIT_RC6_6A_20 |
903 RC_BIT_RC6_6A_24 |
904 RC_BIT_RC6_6A_32 |
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200905 RC_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
906 { RC_BIT_JVC, "jvc", "ir-jvc-decoder" },
David Härdemanc003ab12012-10-11 19:11:54 -0300907 { RC_BIT_SONY12 |
908 RC_BIT_SONY15 |
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200909 RC_BIT_SONY20, "sony", "ir-sony-decoder" },
910 { RC_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
911 { RC_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
912 { RC_BIT_SHARP, "sharp", "ir-sharp-decoder" },
Sean Youngb73bc162017-02-11 20:33:38 -0200913 { RC_BIT_MCIR2_KBD |
914 RC_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" },
Heiner Kallweit9f0bf362015-11-16 17:52:08 -0200915 { RC_BIT_XMP, "xmp", "ir-xmp-decoder" },
Kamil Debskiff42c8a2015-08-17 08:47:41 -0300916 { RC_BIT_CEC, "cec", NULL },
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300917};
918
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300919/**
James Hoganab88c662014-02-28 20:17:05 -0300920 * struct rc_filter_attribute - Device attribute relating to a filter type.
921 * @attr: Device attribute.
922 * @type: Filter type.
923 * @mask: false for filter value, true for filter mask.
924 */
925struct rc_filter_attribute {
926 struct device_attribute attr;
927 enum rc_filter_type type;
928 bool mask;
929};
930#define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
931
James Hoganab88c662014-02-28 20:17:05 -0300932#define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
933 struct rc_filter_attribute dev_attr_##_name = { \
934 .attr = __ATTR(_name, _mode, _show, _store), \
935 .type = (_type), \
936 .mask = (_mask), \
937 }
938
David Härdemandd6ff6a2015-07-22 17:55:24 -0300939static bool lirc_is_present(void)
940{
941#if defined(CONFIG_LIRC_MODULE)
942 struct module *lirc;
943
944 mutex_lock(&module_mutex);
945 lirc = find_module("lirc_dev");
946 mutex_unlock(&module_mutex);
947
948 return lirc ? true : false;
949#elif defined(CONFIG_LIRC)
950 return true;
951#else
952 return false;
953#endif
954}
955
James Hoganab88c662014-02-28 20:17:05 -0300956/**
Sean Young0751d332016-12-05 17:08:35 -0200957 * show_protocols() - shows the current IR protocol(s)
David Härdemand8b4b582010-10-29 16:08:23 -0300958 * @device: the device descriptor
David Härdemanda6e1622014-04-03 20:32:16 -0300959 * @mattr: the device attribute struct
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300960 * @buf: a pointer to the output buffer
961 *
962 * This routine is a callback routine for input read the IR protocol type(s).
Sean Young0751d332016-12-05 17:08:35 -0200963 * it is trigged by reading /sys/class/rc/rc?/protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300964 * It returns the protocol names of supported protocols.
965 * Enabled protocols are printed in brackets.
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300966 *
David Härdeman18726a32017-04-27 17:34:08 -0300967 * dev->lock is taken to guard against races between
968 * store_protocols and show_protocols.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300969 */
David Härdemand8b4b582010-10-29 16:08:23 -0300970static ssize_t show_protocols(struct device *device,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300971 struct device_attribute *mattr, char *buf)
972{
David Härdemand8b4b582010-10-29 16:08:23 -0300973 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300974 u64 allowed, enabled;
975 char *tmp = buf;
976 int i;
977
Jarod Wilson08aeb7c2011-05-11 15:14:31 -0300978 mutex_lock(&dev->lock);
979
Sean Young0751d332016-12-05 17:08:35 -0200980 enabled = dev->enabled_protocols;
981 allowed = dev->allowed_protocols;
982 if (dev->raw && !allowed)
983 allowed = ir_raw_get_allowed_protocols();
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300984
David Härdemanda6e1622014-04-03 20:32:16 -0300985 mutex_unlock(&dev->lock);
986
987 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
988 __func__, (long long)allowed, (long long)enabled);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300989
990 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
991 if (allowed & enabled & proto_names[i].type)
992 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
993 else if (allowed & proto_names[i].type)
994 tmp += sprintf(tmp, "%s ", proto_names[i].name);
David Härdemanc003ab12012-10-11 19:11:54 -0300995
996 if (allowed & proto_names[i].type)
997 allowed &= ~proto_names[i].type;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -0300998 }
999
David Härdemandd6ff6a2015-07-22 17:55:24 -03001000 if (dev->driver_type == RC_DRIVER_IR_RAW && lirc_is_present())
David Härdeman275ddb42015-05-19 19:03:22 -03001001 tmp += sprintf(tmp, "[lirc] ");
1002
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001003 if (tmp != buf)
1004 tmp--;
1005 *tmp = '\n';
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001006
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001007 return tmp + 1 - buf;
1008}
1009
1010/**
David Härdemanda6e1622014-04-03 20:32:16 -03001011 * parse_protocol_change() - parses a protocol change request
1012 * @protocols: pointer to the bitmask of current protocols
1013 * @buf: pointer to the buffer with a list of changes
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001014 *
David Härdemanda6e1622014-04-03 20:32:16 -03001015 * Writing "+proto" will add a protocol to the protocol mask.
1016 * Writing "-proto" will remove a protocol from protocol mask.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001017 * Writing "proto" will enable only "proto".
1018 * Writing "none" will disable all protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001019 * Returns the number of changes performed or a negative error code.
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001020 */
David Härdemanda6e1622014-04-03 20:32:16 -03001021static int parse_protocol_change(u64 *protocols, const char *buf)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001022{
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001023 const char *tmp;
David Härdemanda6e1622014-04-03 20:32:16 -03001024 unsigned count = 0;
1025 bool enable, disable;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001026 u64 mask;
David Härdemanda6e1622014-04-03 20:32:16 -03001027 int i;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001028
David Härdemanda6e1622014-04-03 20:32:16 -03001029 while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001030 if (!*tmp)
1031 break;
1032
1033 if (*tmp == '+') {
1034 enable = true;
1035 disable = false;
1036 tmp++;
1037 } else if (*tmp == '-') {
1038 enable = false;
1039 disable = true;
1040 tmp++;
1041 } else {
1042 enable = false;
1043 disable = false;
1044 }
1045
David Härdemanc003ab12012-10-11 19:11:54 -03001046 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1047 if (!strcasecmp(tmp, proto_names[i].name)) {
1048 mask = proto_names[i].type;
1049 break;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001050 }
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001051 }
1052
David Härdemanc003ab12012-10-11 19:11:54 -03001053 if (i == ARRAY_SIZE(proto_names)) {
David Härdeman275ddb42015-05-19 19:03:22 -03001054 if (!strcasecmp(tmp, "lirc"))
1055 mask = 0;
1056 else {
1057 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
1058 return -EINVAL;
1059 }
David Härdemanc003ab12012-10-11 19:11:54 -03001060 }
1061
1062 count++;
1063
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001064 if (enable)
David Härdemanda6e1622014-04-03 20:32:16 -03001065 *protocols |= mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001066 else if (disable)
David Härdemanda6e1622014-04-03 20:32:16 -03001067 *protocols &= ~mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001068 else
David Härdemanda6e1622014-04-03 20:32:16 -03001069 *protocols = mask;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001070 }
1071
1072 if (!count) {
1073 IR_dprintk(1, "Protocol not specified\n");
David Härdemanda6e1622014-04-03 20:32:16 -03001074 return -EINVAL;
1075 }
1076
1077 return count;
1078}
1079
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001080static void ir_raw_load_modules(u64 *protocols)
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001081{
1082 u64 available;
1083 int i, ret;
1084
1085 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1086 if (proto_names[i].type == RC_BIT_NONE ||
1087 proto_names[i].type & (RC_BIT_OTHER | RC_BIT_UNKNOWN))
1088 continue;
1089
1090 available = ir_raw_get_allowed_protocols();
1091 if (!(*protocols & proto_names[i].type & ~available))
1092 continue;
1093
1094 if (!proto_names[i].module_name) {
1095 pr_err("Can't enable IR protocol %s\n",
1096 proto_names[i].name);
1097 *protocols &= ~proto_names[i].type;
1098 continue;
1099 }
1100
1101 ret = request_module("%s", proto_names[i].module_name);
1102 if (ret < 0) {
1103 pr_err("Couldn't load IR protocol module %s\n",
1104 proto_names[i].module_name);
1105 *protocols &= ~proto_names[i].type;
1106 continue;
1107 }
1108 msleep(20);
1109 available = ir_raw_get_allowed_protocols();
1110 if (!(*protocols & proto_names[i].type & ~available))
1111 continue;
1112
Sean Young8caebcd2017-01-19 19:33:49 -02001113 pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001114 proto_names[i].module_name,
1115 proto_names[i].name);
1116 *protocols &= ~proto_names[i].type;
1117 }
1118}
1119
David Härdemanda6e1622014-04-03 20:32:16 -03001120/**
1121 * store_protocols() - changes the current/wakeup IR protocol(s)
1122 * @device: the device descriptor
1123 * @mattr: the device attribute struct
1124 * @buf: a pointer to the input buffer
1125 * @len: length of the input buffer
1126 *
1127 * This routine is for changing the IR protocol type.
1128 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
1129 * See parse_protocol_change() for the valid commands.
1130 * Returns @len on success or a negative error code.
1131 *
David Härdeman18726a32017-04-27 17:34:08 -03001132 * dev->lock is taken to guard against races between
1133 * store_protocols and show_protocols.
David Härdemanda6e1622014-04-03 20:32:16 -03001134 */
1135static ssize_t store_protocols(struct device *device,
1136 struct device_attribute *mattr,
1137 const char *buf, size_t len)
1138{
1139 struct rc_dev *dev = to_rc_dev(device);
David Härdemanda6e1622014-04-03 20:32:16 -03001140 u64 *current_protocols;
David Härdemanda6e1622014-04-03 20:32:16 -03001141 struct rc_scancode_filter *filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001142 u64 old_protocols, new_protocols;
1143 ssize_t rc;
1144
Sean Young0751d332016-12-05 17:08:35 -02001145 IR_dprintk(1, "Normal protocol change requested\n");
1146 current_protocols = &dev->enabled_protocols;
1147 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001148
Sean Young0751d332016-12-05 17:08:35 -02001149 if (!dev->change_protocol) {
David Härdemanda6e1622014-04-03 20:32:16 -03001150 IR_dprintk(1, "Protocol switching not supported\n");
1151 return -EINVAL;
1152 }
1153
1154 mutex_lock(&dev->lock);
1155
1156 old_protocols = *current_protocols;
1157 new_protocols = old_protocols;
1158 rc = parse_protocol_change(&new_protocols, buf);
1159 if (rc < 0)
1160 goto out;
1161
Sean Young0751d332016-12-05 17:08:35 -02001162 rc = dev->change_protocol(dev, &new_protocols);
David Härdemanda6e1622014-04-03 20:32:16 -03001163 if (rc < 0) {
1164 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
1165 (long long)new_protocols);
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001166 goto out;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001167 }
1168
Heiner Kallweit9f0bf362015-11-16 17:52:08 -02001169 if (dev->driver_type == RC_DRIVER_IR_RAW)
1170 ir_raw_load_modules(&new_protocols);
1171
James Hogan983c5bd2014-12-08 13:17:07 -03001172 if (new_protocols != old_protocols) {
1173 *current_protocols = new_protocols;
1174 IR_dprintk(1, "Protocols changed to 0x%llx\n",
1175 (long long)new_protocols);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001176 }
1177
James Hogan6bea25a2014-02-28 20:17:06 -03001178 /*
James Hogan983c5bd2014-12-08 13:17:07 -03001179 * If a protocol change was attempted the filter may need updating, even
1180 * if the actual protocol mask hasn't changed (since the driver may have
1181 * cleared the filter).
James Hogan6bea25a2014-02-28 20:17:06 -03001182 * Try setting the same filter with the new protocol (if any).
1183 * Fall back to clearing the filter.
1184 */
Sean Young0751d332016-12-05 17:08:35 -02001185 if (dev->s_filter && filter->mask) {
David Härdemanda6e1622014-04-03 20:32:16 -03001186 if (new_protocols)
Sean Young0751d332016-12-05 17:08:35 -02001187 rc = dev->s_filter(dev, filter);
David Härdemanda6e1622014-04-03 20:32:16 -03001188 else
1189 rc = -1;
David Härdeman23c843b2014-04-04 19:06:01 -03001190
David Härdemanda6e1622014-04-03 20:32:16 -03001191 if (rc < 0) {
1192 filter->data = 0;
1193 filter->mask = 0;
Sean Young0751d332016-12-05 17:08:35 -02001194 dev->s_filter(dev, filter);
James Hogan6bea25a2014-02-28 20:17:06 -03001195 }
James Hogan6bea25a2014-02-28 20:17:06 -03001196 }
1197
David Härdemanda6e1622014-04-03 20:32:16 -03001198 rc = len;
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001199
1200out:
1201 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001202 return rc;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001203}
1204
James Hogan00942d12014-01-17 10:58:49 -03001205/**
James Hogan00942d12014-01-17 10:58:49 -03001206 * show_filter() - shows the current scancode filter value or mask
1207 * @device: the device descriptor
1208 * @attr: the device attribute struct
1209 * @buf: a pointer to the output buffer
1210 *
1211 * This routine is a callback routine to read a scancode filter value or mask.
1212 * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
1213 * It prints the current scancode filter value or mask of the appropriate filter
1214 * type in hexadecimal into @buf and returns the size of the buffer.
1215 *
1216 * Bits of the filter value corresponding to set bits in the filter mask are
1217 * compared against input scancodes and non-matching scancodes are discarded.
1218 *
David Härdeman18726a32017-04-27 17:34:08 -03001219 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001220 * store_filter and show_filter.
1221 */
1222static ssize_t show_filter(struct device *device,
1223 struct device_attribute *attr,
1224 char *buf)
1225{
1226 struct rc_dev *dev = to_rc_dev(device);
1227 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001228 struct rc_scancode_filter *filter;
James Hogan00942d12014-01-17 10:58:49 -03001229 u32 val;
1230
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001231 mutex_lock(&dev->lock);
Mauro Carvalho Chehabc73bbaa2016-02-11 10:33:31 -02001232
David Härdemanda6e1622014-04-03 20:32:16 -03001233 if (fattr->type == RC_FILTER_NORMAL)
David Härdemanc5540fb2014-04-03 20:32:21 -03001234 filter = &dev->scancode_filter;
James Hogan00942d12014-01-17 10:58:49 -03001235 else
David Härdemanc5540fb2014-04-03 20:32:21 -03001236 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001237
David Härdemanda6e1622014-04-03 20:32:16 -03001238 if (fattr->mask)
1239 val = filter->mask;
1240 else
1241 val = filter->data;
James Hogan00942d12014-01-17 10:58:49 -03001242 mutex_unlock(&dev->lock);
1243
1244 return sprintf(buf, "%#x\n", val);
1245}
1246
1247/**
1248 * store_filter() - changes the scancode filter value
1249 * @device: the device descriptor
1250 * @attr: the device attribute struct
1251 * @buf: a pointer to the input buffer
1252 * @len: length of the input buffer
1253 *
1254 * This routine is for changing a scancode filter value or mask.
1255 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
1256 * Returns -EINVAL if an invalid filter value for the current protocol was
1257 * specified or if scancode filtering is not supported by the driver, otherwise
1258 * returns @len.
1259 *
1260 * Bits of the filter value corresponding to set bits in the filter mask are
1261 * compared against input scancodes and non-matching scancodes are discarded.
1262 *
David Härdeman18726a32017-04-27 17:34:08 -03001263 * dev->lock is taken to guard against races between
James Hogan00942d12014-01-17 10:58:49 -03001264 * store_filter and show_filter.
1265 */
1266static ssize_t store_filter(struct device *device,
1267 struct device_attribute *attr,
David Härdemanda6e1622014-04-03 20:32:16 -03001268 const char *buf, size_t len)
James Hogan00942d12014-01-17 10:58:49 -03001269{
1270 struct rc_dev *dev = to_rc_dev(device);
1271 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
David Härdemanda6e1622014-04-03 20:32:16 -03001272 struct rc_scancode_filter new_filter, *filter;
James Hogan00942d12014-01-17 10:58:49 -03001273 int ret;
1274 unsigned long val;
David Härdeman23c843b2014-04-04 19:06:01 -03001275 int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
James Hogan00942d12014-01-17 10:58:49 -03001276
James Hogan00942d12014-01-17 10:58:49 -03001277 ret = kstrtoul(buf, 0, &val);
1278 if (ret < 0)
1279 return ret;
1280
David Härdemanda6e1622014-04-03 20:32:16 -03001281 if (fattr->type == RC_FILTER_NORMAL) {
1282 set_filter = dev->s_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001283 filter = &dev->scancode_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001284 } else {
1285 set_filter = dev->s_wakeup_filter;
David Härdemanc5540fb2014-04-03 20:32:21 -03001286 filter = &dev->scancode_wakeup_filter;
David Härdemanda6e1622014-04-03 20:32:16 -03001287 }
1288
David Härdeman99b0f3c2014-04-04 19:06:06 -03001289 if (!set_filter)
1290 return -EINVAL;
James Hogan00942d12014-01-17 10:58:49 -03001291
1292 mutex_lock(&dev->lock);
1293
David Härdemanda6e1622014-04-03 20:32:16 -03001294 new_filter = *filter;
James Hogan00942d12014-01-17 10:58:49 -03001295 if (fattr->mask)
David Härdemanda6e1622014-04-03 20:32:16 -03001296 new_filter.mask = val;
James Hogan00942d12014-01-17 10:58:49 -03001297 else
David Härdemanda6e1622014-04-03 20:32:16 -03001298 new_filter.data = val;
David Härdeman23c843b2014-04-04 19:06:01 -03001299
Sean Young0751d332016-12-05 17:08:35 -02001300 if (fattr->type == RC_FILTER_WAKEUP) {
Sean Youngb590c0b2016-12-05 19:24:59 -02001301 /*
1302 * Refuse to set a filter unless a protocol is enabled
1303 * and the filter is valid for that protocol
1304 */
1305 if (dev->wakeup_protocol != RC_TYPE_UNKNOWN)
James Hoganf423ccc2015-03-31 14:48:10 -03001306 ret = rc_validate_filter(dev, &new_filter);
Sean Youngb590c0b2016-12-05 19:24:59 -02001307 else
Sean Young0751d332016-12-05 17:08:35 -02001308 ret = -EINVAL;
Sean Youngb590c0b2016-12-05 19:24:59 -02001309
1310 if (ret != 0)
Sean Young0751d332016-12-05 17:08:35 -02001311 goto unlock;
Sean Young0751d332016-12-05 17:08:35 -02001312 }
1313
1314 if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
1315 val) {
James Hogan6bea25a2014-02-28 20:17:06 -03001316 /* refuse to set a filter unless a protocol is enabled */
1317 ret = -EINVAL;
1318 goto unlock;
1319 }
David Härdeman23c843b2014-04-04 19:06:01 -03001320
David Härdemanda6e1622014-04-03 20:32:16 -03001321 ret = set_filter(dev, &new_filter);
David Härdeman99b0f3c2014-04-04 19:06:06 -03001322 if (ret < 0)
1323 goto unlock;
James Hogan00942d12014-01-17 10:58:49 -03001324
David Härdemanda6e1622014-04-03 20:32:16 -03001325 *filter = new_filter;
James Hogan00942d12014-01-17 10:58:49 -03001326
1327unlock:
1328 mutex_unlock(&dev->lock);
David Härdemanda6e1622014-04-03 20:32:16 -03001329 return (ret < 0) ? ret : len;
James Hogan00942d12014-01-17 10:58:49 -03001330}
1331
Sean Young0751d332016-12-05 17:08:35 -02001332/**
1333 * show_wakeup_protocols() - shows the wakeup IR protocol
1334 * @device: the device descriptor
1335 * @mattr: the device attribute struct
1336 * @buf: a pointer to the output buffer
1337 *
1338 * This routine is a callback routine for input read the IR protocol type(s).
1339 * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols.
1340 * It returns the protocol names of supported protocols.
1341 * The enabled protocols are printed in brackets.
1342 *
David Härdeman18726a32017-04-27 17:34:08 -03001343 * dev->lock is taken to guard against races between
1344 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001345 */
1346static ssize_t show_wakeup_protocols(struct device *device,
1347 struct device_attribute *mattr,
1348 char *buf)
1349{
1350 struct rc_dev *dev = to_rc_dev(device);
1351 u64 allowed;
1352 enum rc_type enabled;
1353 char *tmp = buf;
1354 int i;
1355
Sean Young0751d332016-12-05 17:08:35 -02001356 mutex_lock(&dev->lock);
1357
1358 allowed = dev->allowed_wakeup_protocols;
1359 enabled = dev->wakeup_protocol;
1360
1361 mutex_unlock(&dev->lock);
1362
1363 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - %d\n",
1364 __func__, (long long)allowed, enabled);
1365
Sean Youngd57ea872017-08-09 13:19:16 -04001366 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001367 if (allowed & (1ULL << i)) {
1368 if (i == enabled)
Sean Youngd57ea872017-08-09 13:19:16 -04001369 tmp += sprintf(tmp, "[%s] ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001370 else
Sean Youngd57ea872017-08-09 13:19:16 -04001371 tmp += sprintf(tmp, "%s ", protocols[i].name);
Sean Young0751d332016-12-05 17:08:35 -02001372 }
1373 }
1374
1375 if (tmp != buf)
1376 tmp--;
1377 *tmp = '\n';
1378
1379 return tmp + 1 - buf;
1380}
1381
1382/**
1383 * store_wakeup_protocols() - changes the wakeup IR protocol(s)
1384 * @device: the device descriptor
1385 * @mattr: the device attribute struct
1386 * @buf: a pointer to the input buffer
1387 * @len: length of the input buffer
1388 *
1389 * This routine is for changing the IR protocol type.
1390 * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols.
1391 * Returns @len on success or a negative error code.
1392 *
David Härdeman18726a32017-04-27 17:34:08 -03001393 * dev->lock is taken to guard against races between
1394 * store_wakeup_protocols and show_wakeup_protocols.
Sean Young0751d332016-12-05 17:08:35 -02001395 */
1396static ssize_t store_wakeup_protocols(struct device *device,
1397 struct device_attribute *mattr,
1398 const char *buf, size_t len)
1399{
1400 struct rc_dev *dev = to_rc_dev(device);
1401 enum rc_type protocol;
1402 ssize_t rc;
1403 u64 allowed;
1404 int i;
1405
Sean Young0751d332016-12-05 17:08:35 -02001406 mutex_lock(&dev->lock);
1407
1408 allowed = dev->allowed_wakeup_protocols;
1409
1410 if (sysfs_streq(buf, "none")) {
1411 protocol = RC_TYPE_UNKNOWN;
1412 } else {
Sean Youngd57ea872017-08-09 13:19:16 -04001413 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
Sean Young0751d332016-12-05 17:08:35 -02001414 if ((allowed & (1ULL << i)) &&
Sean Youngd57ea872017-08-09 13:19:16 -04001415 sysfs_streq(buf, protocols[i].name)) {
Sean Young0751d332016-12-05 17:08:35 -02001416 protocol = i;
1417 break;
1418 }
1419 }
1420
Sean Youngd57ea872017-08-09 13:19:16 -04001421 if (i == ARRAY_SIZE(protocols)) {
Sean Young0751d332016-12-05 17:08:35 -02001422 rc = -EINVAL;
1423 goto out;
1424 }
James Hoganf423ccc2015-03-31 14:48:10 -03001425
1426 if (dev->encode_wakeup) {
1427 u64 mask = 1ULL << protocol;
1428
1429 ir_raw_load_modules(&mask);
1430 if (!mask) {
1431 rc = -EINVAL;
1432 goto out;
1433 }
1434 }
Sean Young0751d332016-12-05 17:08:35 -02001435 }
1436
1437 if (dev->wakeup_protocol != protocol) {
1438 dev->wakeup_protocol = protocol;
1439 IR_dprintk(1, "Wakeup protocol changed to %d\n", protocol);
1440
1441 if (protocol == RC_TYPE_RC6_MCE)
1442 dev->scancode_wakeup_filter.data = 0x800f0000;
1443 else
1444 dev->scancode_wakeup_filter.data = 0;
1445 dev->scancode_wakeup_filter.mask = 0;
1446
1447 rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
1448 if (rc == 0)
1449 rc = len;
1450 } else {
1451 rc = len;
1452 }
1453
1454out:
1455 mutex_unlock(&dev->lock);
1456 return rc;
1457}
1458
David Härdemand8b4b582010-10-29 16:08:23 -03001459static void rc_dev_release(struct device *device)
1460{
Max Kellermann47cae1e2016-03-21 08:33:05 -03001461 struct rc_dev *dev = to_rc_dev(device);
1462
1463 kfree(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001464}
1465
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001466#define ADD_HOTPLUG_VAR(fmt, val...) \
1467 do { \
1468 int err = add_uevent_var(env, fmt, val); \
1469 if (err) \
1470 return err; \
1471 } while (0)
1472
1473static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1474{
David Härdemand8b4b582010-10-29 16:08:23 -03001475 struct rc_dev *dev = to_rc_dev(device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001476
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001477 if (dev->rc_map.name)
1478 ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
David Härdemand8b4b582010-10-29 16:08:23 -03001479 if (dev->driver_name)
1480 ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001481
1482 return 0;
1483}
1484
1485/*
1486 * Static device attribute struct with the sysfs attributes for IR's
1487 */
Sean Young0751d332016-12-05 17:08:35 -02001488static DEVICE_ATTR(protocols, 0644, show_protocols, store_protocols);
1489static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
1490 store_wakeup_protocols);
James Hogan00942d12014-01-17 10:58:49 -03001491static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
1492 show_filter, store_filter, RC_FILTER_NORMAL, false);
1493static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
1494 show_filter, store_filter, RC_FILTER_NORMAL, true);
1495static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
1496 show_filter, store_filter, RC_FILTER_WAKEUP, false);
1497static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
1498 show_filter, store_filter, RC_FILTER_WAKEUP, true);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001499
David Härdeman99b0f3c2014-04-04 19:06:06 -03001500static struct attribute *rc_dev_protocol_attrs[] = {
Sean Young0751d332016-12-05 17:08:35 -02001501 &dev_attr_protocols.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001502 NULL,
1503};
1504
Arvind Yadavdb681022017-07-07 04:23:54 -04001505static const struct attribute_group rc_dev_protocol_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001506 .attrs = rc_dev_protocol_attrs,
1507};
1508
David Härdeman99b0f3c2014-04-04 19:06:06 -03001509static struct attribute *rc_dev_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001510 &dev_attr_filter.attr.attr,
1511 &dev_attr_filter_mask.attr.attr,
David Härdeman99b0f3c2014-04-04 19:06:06 -03001512 NULL,
1513};
1514
Arvind Yadavdb681022017-07-07 04:23:54 -04001515static const struct attribute_group rc_dev_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001516 .attrs = rc_dev_filter_attrs,
1517};
1518
1519static struct attribute *rc_dev_wakeup_filter_attrs[] = {
James Hogan00942d12014-01-17 10:58:49 -03001520 &dev_attr_wakeup_filter.attr.attr,
1521 &dev_attr_wakeup_filter_mask.attr.attr,
Sean Young0751d332016-12-05 17:08:35 -02001522 &dev_attr_wakeup_protocols.attr,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001523 NULL,
1524};
1525
Arvind Yadavdb681022017-07-07 04:23:54 -04001526static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
David Härdeman99b0f3c2014-04-04 19:06:06 -03001527 .attrs = rc_dev_wakeup_filter_attrs,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001528};
1529
1530static struct device_type rc_dev_type = {
David Härdemand8b4b582010-10-29 16:08:23 -03001531 .release = rc_dev_release,
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001532 .uevent = rc_dev_uevent,
1533};
1534
Andi Shyti0f7499f2016-12-16 06:50:58 -02001535struct rc_dev *rc_allocate_device(enum rc_driver_type type)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001536{
David Härdemand8b4b582010-10-29 16:08:23 -03001537 struct rc_dev *dev;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001538
David Härdemand8b4b582010-10-29 16:08:23 -03001539 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1540 if (!dev)
1541 return NULL;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001542
Andi Shytid34aee12016-12-16 04:12:15 -02001543 if (type != RC_DRIVER_IR_RAW_TX) {
1544 dev->input_dev = input_allocate_device();
1545 if (!dev->input_dev) {
1546 kfree(dev);
1547 return NULL;
1548 }
1549
1550 dev->input_dev->getkeycode = ir_getkeycode;
1551 dev->input_dev->setkeycode = ir_setkeycode;
1552 input_set_drvdata(dev->input_dev, dev);
1553
1554 setup_timer(&dev->timer_keyup, ir_timer_keyup,
1555 (unsigned long)dev);
1556
1557 spin_lock_init(&dev->rc_map.lock);
1558 spin_lock_init(&dev->keylock);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001559 }
Jarod Wilson08aeb7c2011-05-11 15:14:31 -03001560 mutex_init(&dev->lock);
David Härdemand8b4b582010-10-29 16:08:23 -03001561
1562 dev->dev.type = &rc_dev_type;
David Härdeman40fc5322013-03-06 16:52:10 -03001563 dev->dev.class = &rc_class;
David Härdemand8b4b582010-10-29 16:08:23 -03001564 device_initialize(&dev->dev);
1565
Andi Shyti0f7499f2016-12-16 06:50:58 -02001566 dev->driver_type = type;
1567
David Härdemand8b4b582010-10-29 16:08:23 -03001568 __module_get(THIS_MODULE);
1569 return dev;
1570}
1571EXPORT_SYMBOL_GPL(rc_allocate_device);
1572
1573void rc_free_device(struct rc_dev *dev)
1574{
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001575 if (!dev)
1576 return;
1577
Markus Elfring3dd94f02014-11-20 09:01:32 -03001578 input_free_device(dev->input_dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001579
1580 put_device(&dev->dev);
1581
Max Kellermann47cae1e2016-03-21 08:33:05 -03001582 /* kfree(dev) will be called by the callback function
1583 rc_dev_release() */
1584
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001585 module_put(THIS_MODULE);
David Härdemand8b4b582010-10-29 16:08:23 -03001586}
1587EXPORT_SYMBOL_GPL(rc_free_device);
1588
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001589static void devm_rc_alloc_release(struct device *dev, void *res)
1590{
1591 rc_free_device(*(struct rc_dev **)res);
1592}
1593
Andi Shyti0f7499f2016-12-16 06:50:58 -02001594struct rc_dev *devm_rc_allocate_device(struct device *dev,
1595 enum rc_driver_type type)
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001596{
1597 struct rc_dev **dr, *rc;
1598
1599 dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
1600 if (!dr)
1601 return NULL;
1602
Andi Shyti0f7499f2016-12-16 06:50:58 -02001603 rc = rc_allocate_device(type);
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001604 if (!rc) {
1605 devres_free(dr);
1606 return NULL;
1607 }
1608
1609 rc->dev.parent = dev;
1610 rc->managed_alloc = true;
1611 *dr = rc;
1612 devres_add(dev, dr);
1613
1614 return rc;
1615}
1616EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
1617
David Härdemanf56928a2017-05-03 07:04:00 -03001618static int rc_prepare_rx_device(struct rc_dev *dev)
David Härdemand8b4b582010-10-29 16:08:23 -03001619{
David Härdemanfcb13092015-05-19 19:03:17 -03001620 int rc;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001621 struct rc_map *rc_map;
Sean Young41380862017-02-22 18:48:01 -03001622 u64 rc_type;
David Härdemand8b4b582010-10-29 16:08:23 -03001623
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001624 if (!dev->map_name)
David Härdemand8b4b582010-10-29 16:08:23 -03001625 return -EINVAL;
1626
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001627 rc_map = rc_map_get(dev->map_name);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001628 if (!rc_map)
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001629 rc_map = rc_map_get(RC_MAP_EMPTY);
Mauro Carvalho Chehabb088ba62010-11-17 14:28:27 -03001630 if (!rc_map || !rc_map->scan || rc_map->size == 0)
David Härdemand8b4b582010-10-29 16:08:23 -03001631 return -EINVAL;
1632
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001633 rc = ir_setkeytable(dev, rc_map);
1634 if (rc)
1635 return rc;
1636
Sean Young41380862017-02-22 18:48:01 -03001637 rc_type = BIT_ULL(rc_map->rc_type);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001638
Sean Young41380862017-02-22 18:48:01 -03001639 if (dev->change_protocol) {
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001640 rc = dev->change_protocol(dev, &rc_type);
1641 if (rc < 0)
1642 goto out_table;
1643 dev->enabled_protocols = rc_type;
1644 }
1645
Sean Young41380862017-02-22 18:48:01 -03001646 if (dev->driver_type == RC_DRIVER_IR_RAW)
1647 ir_raw_load_modules(&rc_type);
1648
David Härdemand8b4b582010-10-29 16:08:23 -03001649 set_bit(EV_KEY, dev->input_dev->evbit);
1650 set_bit(EV_REP, dev->input_dev->evbit);
1651 set_bit(EV_MSC, dev->input_dev->evbit);
1652 set_bit(MSC_SCAN, dev->input_dev->mscbit);
1653 if (dev->open)
1654 dev->input_dev->open = ir_open;
1655 if (dev->close)
1656 dev->input_dev->close = ir_close;
1657
David Härdemanb2aceb72017-04-27 17:33:58 -03001658 dev->input_dev->dev.parent = &dev->dev;
1659 memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
1660 dev->input_dev->phys = dev->input_phys;
Sean Young518f4b22017-07-01 12:13:19 -04001661 dev->input_dev->name = dev->device_name;
David Härdemanb2aceb72017-04-27 17:33:58 -03001662
David Härdemanf56928a2017-05-03 07:04:00 -03001663 return 0;
1664
1665out_table:
1666 ir_free_table(&dev->rc_map);
1667
1668 return rc;
1669}
1670
1671static int rc_setup_rx_device(struct rc_dev *dev)
1672{
1673 int rc;
1674
David Härdemanb2aceb72017-04-27 17:33:58 -03001675 /* rc_open will be called here */
1676 rc = input_register_device(dev->input_dev);
1677 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001678 return rc;
David Härdemanb2aceb72017-04-27 17:33:58 -03001679
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001680 /*
1681 * Default delay of 250ms is too short for some protocols, especially
1682 * since the timeout is currently set to 250ms. Increase it to 500ms,
1683 * to avoid wrong repetition of the keycodes. Note that this must be
1684 * set after the call to input_register_device().
1685 */
1686 dev->input_dev->rep[REP_DELAY] = 500;
1687
1688 /*
1689 * As a repeat event on protocols like RC-5 and NEC take as long as
1690 * 110/114ms, using 33ms as a repeat period is not the right thing
1691 * to do.
1692 */
1693 dev->input_dev->rep[REP_PERIOD] = 125;
1694
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001695 return 0;
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001696}
1697
1698static void rc_free_rx_device(struct rc_dev *dev)
1699{
David Härdemanf56928a2017-05-03 07:04:00 -03001700 if (!dev)
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001701 return;
1702
David Härdemanf56928a2017-05-03 07:04:00 -03001703 if (dev->input_dev) {
1704 input_unregister_device(dev->input_dev);
1705 dev->input_dev = NULL;
1706 }
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001707
David Härdemanf56928a2017-05-03 07:04:00 -03001708 ir_free_table(&dev->rc_map);
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001709}
1710
1711int rc_register_device(struct rc_dev *dev)
1712{
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001713 const char *path;
1714 int attr = 0;
1715 int minor;
1716 int rc;
1717
1718 if (!dev)
1719 return -EINVAL;
1720
David Härdemanfcb13092015-05-19 19:03:17 -03001721 minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
1722 if (minor < 0)
1723 return minor;
1724
1725 dev->minor = minor;
1726 dev_set_name(&dev->dev, "rc%u", dev->minor);
1727 dev_set_drvdata(&dev->dev, dev);
Mauro Carvalho Chehab587d1b02014-01-14 16:27:55 -03001728
David Härdeman99b0f3c2014-04-04 19:06:06 -03001729 dev->dev.groups = dev->sysfs_groups;
Andi Shytid34aee12016-12-16 04:12:15 -02001730 if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
1731 dev->sysfs_groups[attr++] = &rc_dev_protocol_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001732 if (dev->s_filter)
David Härdeman120703f2014-04-03 20:31:30 -03001733 dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001734 if (dev->s_wakeup_filter)
1735 dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
David Härdeman99b0f3c2014-04-04 19:06:06 -03001736 dev->sysfs_groups[attr++] = NULL;
1737
David Härdemanf56928a2017-05-03 07:04:00 -03001738 if (dev->driver_type == RC_DRIVER_IR_RAW ||
1739 dev->driver_type == RC_DRIVER_IR_RAW_TX) {
1740 rc = ir_raw_event_prepare(dev);
1741 if (rc < 0)
1742 goto out_minor;
1743 }
1744
1745 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1746 rc = rc_prepare_rx_device(dev);
1747 if (rc)
1748 goto out_raw;
1749 }
1750
David Härdemand8b4b582010-10-29 16:08:23 -03001751 rc = device_add(&dev->dev);
1752 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001753 goto out_rx_free;
David Härdemand8b4b582010-10-29 16:08:23 -03001754
David Härdemand8b4b582010-10-29 16:08:23 -03001755 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Heiner Kallweit4dc0e902015-10-29 19:39:06 -02001756 dev_info(&dev->dev, "%s as %s\n",
Sean Young518f4b22017-07-01 12:13:19 -04001757 dev->device_name ?: "Unspecified device", path ?: "N/A");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001758 kfree(path);
1759
Sean Young5df62772017-02-23 06:11:21 -03001760 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1761 rc = rc_setup_rx_device(dev);
1762 if (rc)
David Härdemanf56928a2017-05-03 07:04:00 -03001763 goto out_dev;
1764 }
1765
1766 if (dev->driver_type == RC_DRIVER_IR_RAW ||
1767 dev->driver_type == RC_DRIVER_IR_RAW_TX) {
1768 rc = ir_raw_event_register(dev);
1769 if (rc < 0)
1770 goto out_rx;
David Härdemand8b4b582010-10-29 16:08:23 -03001771 }
1772
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001773 IR_dprintk(1, "Registered rc%u (driver: %s)\n",
David Härdemanfcb13092015-05-19 19:03:17 -03001774 dev->minor,
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001775 dev->driver_name ? dev->driver_name : "unknown");
David Härdemand8b4b582010-10-29 16:08:23 -03001776
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001777 return 0;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001778
David Härdemanf56928a2017-05-03 07:04:00 -03001779out_rx:
1780 rc_free_rx_device(dev);
David Härdemand8b4b582010-10-29 16:08:23 -03001781out_dev:
1782 device_del(&dev->dev);
David Härdemanf56928a2017-05-03 07:04:00 -03001783out_rx_free:
1784 ir_free_table(&dev->rc_map);
1785out_raw:
1786 ir_raw_event_free(dev);
1787out_minor:
David Härdemanfcb13092015-05-19 19:03:17 -03001788 ida_simple_remove(&rc_ida, minor);
David Härdemand8b4b582010-10-29 16:08:23 -03001789 return rc;
1790}
1791EXPORT_SYMBOL_GPL(rc_register_device);
1792
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001793static void devm_rc_release(struct device *dev, void *res)
1794{
1795 rc_unregister_device(*(struct rc_dev **)res);
1796}
1797
1798int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
1799{
1800 struct rc_dev **dr;
1801 int ret;
1802
1803 dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
1804 if (!dr)
1805 return -ENOMEM;
1806
1807 ret = rc_register_device(dev);
1808 if (ret) {
1809 devres_free(dr);
1810 return ret;
1811 }
1812
1813 *dr = dev;
1814 devres_add(parent, dr);
1815
1816 return 0;
1817}
1818EXPORT_SYMBOL_GPL(devm_rc_register_device);
1819
David Härdemand8b4b582010-10-29 16:08:23 -03001820void rc_unregister_device(struct rc_dev *dev)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001821{
David Härdemand8b4b582010-10-29 16:08:23 -03001822 if (!dev)
1823 return;
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001824
David Härdemand8b4b582010-10-29 16:08:23 -03001825 del_timer_sync(&dev->timer_keyup);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001826
David Härdemand8b4b582010-10-29 16:08:23 -03001827 if (dev->driver_type == RC_DRIVER_IR_RAW)
1828 ir_raw_event_unregister(dev);
1829
Andi Shyti7ff2c2b2016-12-16 04:12:14 -02001830 rc_free_rx_device(dev);
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001831
1832 device_del(&dev->dev);
1833
David Härdemanfcb13092015-05-19 19:03:17 -03001834 ida_simple_remove(&rc_ida, dev->minor);
1835
Heiner Kallweitddbf7d52016-09-30 17:42:07 -03001836 if (!dev->managed_alloc)
1837 rc_free_device(dev);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001838}
Mauro Carvalho Chehabb05681b2011-07-29 02:23:20 -03001839
David Härdemand8b4b582010-10-29 16:08:23 -03001840EXPORT_SYMBOL_GPL(rc_unregister_device);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001841
1842/*
1843 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1844 */
1845
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001846static int __init rc_core_init(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001847{
David Härdeman40fc5322013-03-06 16:52:10 -03001848 int rc = class_register(&rc_class);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001849 if (rc) {
Mauro Carvalho Chehabd3d96822016-10-20 15:04:39 -02001850 pr_err("rc_core: unable to register rc class\n");
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001851 return rc;
1852 }
1853
Sean Young153a60b2013-07-30 19:00:01 -03001854 led_trigger_register_simple("rc-feedback", &led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001855 rc_map_register(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001856
1857 return 0;
1858}
1859
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001860static void __exit rc_core_exit(void)
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001861{
David Härdeman40fc5322013-03-06 16:52:10 -03001862 class_unregister(&rc_class);
Sean Young153a60b2013-07-30 19:00:01 -03001863 led_trigger_unregister_simple(led_feedback);
Mauro Carvalho Chehabd100e652010-11-17 15:56:53 -03001864 rc_map_unregister(&empty_map);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001865}
1866
David Härdemane76d4ce2013-03-06 16:52:15 -03001867subsys_initcall(rc_core_init);
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001868module_exit(rc_core_exit);
Mauro Carvalho Chehabbc2a6c52010-11-09 23:18:24 -03001869
Mauro Carvalho Chehab6bda9642010-11-17 13:28:38 -03001870int rc_core_debug; /* ir_debug level (0,1,2) */
1871EXPORT_SYMBOL_GPL(rc_core_debug);
1872module_param_named(debug, rc_core_debug, int, 0644);
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03001873
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02001874MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehab446e4a62009-12-11 08:34:07 -03001875MODULE_LICENSE("GPL");