David Härdeman | 829ba9f | 2010-11-19 20:43:27 -0300 | [diff] [blame] | 1 | /* rc-main.c - Remote Controller core module |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 2 | * |
Mauro Carvalho Chehab | 37e59f8 | 2014-02-07 08:03:07 -0200 | [diff] [blame] | 3 | * Copyright (C) 2009-2010 by Mauro Carvalho Chehab |
Mauro Carvalho Chehab | 446e4a6 | 2009-12-11 08:34:07 -0300 | [diff] [blame] | 4 | * |
| 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 Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 13 | */ |
| 14 | |
Mauro Carvalho Chehab | d3d9682 | 2016-10-20 15:04:39 -0200 | [diff] [blame] | 15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 16 | |
Mauro Carvalho Chehab | 6bda964 | 2010-11-17 13:28:38 -0300 | [diff] [blame] | 17 | #include <media/rc-core.h> |
Mauro Carvalho Chehab | 631493e | 2010-11-09 23:44:27 -0300 | [diff] [blame] | 18 | #include <linux/spinlock.h> |
| 19 | #include <linux/delay.h> |
Mauro Carvalho Chehab | 882ead3 | 2009-12-29 10:37:38 -0300 | [diff] [blame] | 20 | #include <linux/input.h> |
Sean Young | 153a60b | 2013-07-30 19:00:01 -0300 | [diff] [blame] | 21 | #include <linux/leds.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
David Härdeman | fcb1309 | 2015-05-19 19:03:17 -0300 | [diff] [blame] | 23 | #include <linux/idr.h> |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 24 | #include <linux/device.h> |
Paul Gortmaker | 7a707b8 | 2011-07-03 14:03:12 -0400 | [diff] [blame] | 25 | #include <linux/module.h> |
Mauro Carvalho Chehab | f62de67 | 2010-11-09 23:09:57 -0300 | [diff] [blame] | 26 | #include "rc-core-priv.h" |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 27 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 28 | /* 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ärdeman | fcb1309 | 2015-05-19 19:03:17 -0300 | [diff] [blame] | 31 | #define RC_DEV_MAX 256 |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 32 | |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 33 | static const struct { |
| 34 | const char *name; |
| 35 | unsigned int repeat_period; |
| 36 | unsigned int scancode_bits; |
| 37 | } protocols[] = { |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 38 | [RC_PROTO_UNKNOWN] = { .name = "unknown", .repeat_period = 250 }, |
| 39 | [RC_PROTO_OTHER] = { .name = "other", .repeat_period = 250 }, |
| 40 | [RC_PROTO_RC5] = { .name = "rc-5", |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 41 | .scancode_bits = 0x1f7f, .repeat_period = 164 }, |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 42 | [RC_PROTO_RC5X_20] = { .name = "rc-5x-20", |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 43 | .scancode_bits = 0x1f7f3f, .repeat_period = 164 }, |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 44 | [RC_PROTO_RC5_SZ] = { .name = "rc-5-sz", |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 45 | .scancode_bits = 0x2fff, .repeat_period = 164 }, |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 46 | [RC_PROTO_JVC] = { .name = "jvc", |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 47 | .scancode_bits = 0xffff, .repeat_period = 250 }, |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 48 | [RC_PROTO_SONY12] = { .name = "sony-12", |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 49 | .scancode_bits = 0x1f007f, .repeat_period = 100 }, |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 50 | [RC_PROTO_SONY15] = { .name = "sony-15", |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 51 | .scancode_bits = 0xff007f, .repeat_period = 100 }, |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 52 | [RC_PROTO_SONY20] = { .name = "sony-20", |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 53 | .scancode_bits = 0x1fff7f, .repeat_period = 100 }, |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 54 | [RC_PROTO_NEC] = { .name = "nec", |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 55 | .scancode_bits = 0xffff, .repeat_period = 160 }, |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 56 | [RC_PROTO_NECX] = { .name = "nec-x", |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 57 | .scancode_bits = 0xffffff, .repeat_period = 160 }, |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 58 | [RC_PROTO_NEC32] = { .name = "nec-32", |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 59 | .scancode_bits = 0xffffffff, .repeat_period = 160 }, |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 60 | [RC_PROTO_SANYO] = { .name = "sanyo", |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 61 | .scancode_bits = 0x1fffff, .repeat_period = 250 }, |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 62 | [RC_PROTO_MCIR2_KBD] = { .name = "mcir2-kbd", |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 63 | .scancode_bits = 0xffff, .repeat_period = 150 }, |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 64 | [RC_PROTO_MCIR2_MSE] = { .name = "mcir2-mse", |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 65 | .scancode_bits = 0x1fffff, .repeat_period = 150 }, |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 66 | [RC_PROTO_RC6_0] = { .name = "rc-6-0", |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 67 | .scancode_bits = 0xffff, .repeat_period = 164 }, |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 68 | [RC_PROTO_RC6_6A_20] = { .name = "rc-6-6a-20", |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 69 | .scancode_bits = 0xfffff, .repeat_period = 164 }, |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 70 | [RC_PROTO_RC6_6A_24] = { .name = "rc-6-6a-24", |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 71 | .scancode_bits = 0xffffff, .repeat_period = 164 }, |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 72 | [RC_PROTO_RC6_6A_32] = { .name = "rc-6-6a-32", |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 73 | .scancode_bits = 0xffffffff, .repeat_period = 164 }, |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 74 | [RC_PROTO_RC6_MCE] = { .name = "rc-6-mce", |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 75 | .scancode_bits = 0xffff7fff, .repeat_period = 164 }, |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 76 | [RC_PROTO_SHARP] = { .name = "sharp", |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 77 | .scancode_bits = 0x1fff, .repeat_period = 250 }, |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 78 | [RC_PROTO_XMP] = { .name = "xmp", .repeat_period = 250 }, |
| 79 | [RC_PROTO_CEC] = { .name = "cec", .repeat_period = 550 }, |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 80 | }; |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 81 | |
David Härdeman | 4c7b355 | 2010-11-10 11:04:19 -0300 | [diff] [blame] | 82 | /* Used to keep track of known keymaps */ |
Mauro Carvalho Chehab | 631493e | 2010-11-09 23:44:27 -0300 | [diff] [blame] | 83 | static LIST_HEAD(rc_map_list); |
| 84 | static DEFINE_SPINLOCK(rc_map_lock); |
Sean Young | 153a60b | 2013-07-30 19:00:01 -0300 | [diff] [blame] | 85 | static struct led_trigger *led_feedback; |
Mauro Carvalho Chehab | 631493e | 2010-11-09 23:44:27 -0300 | [diff] [blame] | 86 | |
David Härdeman | fcb1309 | 2015-05-19 19:03:17 -0300 | [diff] [blame] | 87 | /* Used to keep track of rc devices */ |
| 88 | static DEFINE_IDA(rc_ida); |
| 89 | |
Mauro Carvalho Chehab | d100e65 | 2010-11-17 15:56:53 -0300 | [diff] [blame] | 90 | static struct rc_map_list *seek_rc_map(const char *name) |
Mauro Carvalho Chehab | 631493e | 2010-11-09 23:44:27 -0300 | [diff] [blame] | 91 | { |
Mauro Carvalho Chehab | d100e65 | 2010-11-17 15:56:53 -0300 | [diff] [blame] | 92 | struct rc_map_list *map = NULL; |
Mauro Carvalho Chehab | 631493e | 2010-11-09 23:44:27 -0300 | [diff] [blame] | 93 | |
| 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 Chehab | d100e65 | 2010-11-17 15:56:53 -0300 | [diff] [blame] | 106 | struct rc_map *rc_map_get(const char *name) |
Mauro Carvalho Chehab | 631493e | 2010-11-09 23:44:27 -0300 | [diff] [blame] | 107 | { |
| 108 | |
Mauro Carvalho Chehab | d100e65 | 2010-11-17 15:56:53 -0300 | [diff] [blame] | 109 | struct rc_map_list *map; |
Mauro Carvalho Chehab | 631493e | 2010-11-09 23:44:27 -0300 | [diff] [blame] | 110 | |
| 111 | map = seek_rc_map(name); |
Russell King | 2ff56fa | 2015-10-15 13:15:24 -0300 | [diff] [blame] | 112 | #ifdef CONFIG_MODULES |
Mauro Carvalho Chehab | 631493e | 2010-11-09 23:44:27 -0300 | [diff] [blame] | 113 | if (!map) { |
Kees Cook | 8ea5488 | 2014-03-11 17:25:53 -0300 | [diff] [blame] | 114 | int rc = request_module("%s", name); |
Mauro Carvalho Chehab | 631493e | 2010-11-09 23:44:27 -0300 | [diff] [blame] | 115 | if (rc < 0) { |
Mauro Carvalho Chehab | d3d9682 | 2016-10-20 15:04:39 -0200 | [diff] [blame] | 116 | pr_err("Couldn't load IR keymap %s\n", name); |
Mauro Carvalho Chehab | 631493e | 2010-11-09 23:44:27 -0300 | [diff] [blame] | 117 | 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 Chehab | d3d9682 | 2016-10-20 15:04:39 -0200 | [diff] [blame] | 125 | pr_err("IR keymap %s not found\n", name); |
Mauro Carvalho Chehab | 631493e | 2010-11-09 23:44:27 -0300 | [diff] [blame] | 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | printk(KERN_INFO "Registered IR keymap %s\n", map->map.name); |
| 130 | |
| 131 | return &map->map; |
| 132 | } |
Mauro Carvalho Chehab | d100e65 | 2010-11-17 15:56:53 -0300 | [diff] [blame] | 133 | EXPORT_SYMBOL_GPL(rc_map_get); |
Mauro Carvalho Chehab | 631493e | 2010-11-09 23:44:27 -0300 | [diff] [blame] | 134 | |
Mauro Carvalho Chehab | d100e65 | 2010-11-17 15:56:53 -0300 | [diff] [blame] | 135 | int rc_map_register(struct rc_map_list *map) |
Mauro Carvalho Chehab | 631493e | 2010-11-09 23:44:27 -0300 | [diff] [blame] | 136 | { |
| 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 Chehab | d100e65 | 2010-11-17 15:56:53 -0300 | [diff] [blame] | 142 | EXPORT_SYMBOL_GPL(rc_map_register); |
Mauro Carvalho Chehab | 631493e | 2010-11-09 23:44:27 -0300 | [diff] [blame] | 143 | |
Mauro Carvalho Chehab | d100e65 | 2010-11-17 15:56:53 -0300 | [diff] [blame] | 144 | void rc_map_unregister(struct rc_map_list *map) |
Mauro Carvalho Chehab | 631493e | 2010-11-09 23:44:27 -0300 | [diff] [blame] | 145 | { |
| 146 | spin_lock(&rc_map_lock); |
| 147 | list_del(&map->list); |
| 148 | spin_unlock(&rc_map_lock); |
| 149 | } |
Mauro Carvalho Chehab | d100e65 | 2010-11-17 15:56:53 -0300 | [diff] [blame] | 150 | EXPORT_SYMBOL_GPL(rc_map_unregister); |
Mauro Carvalho Chehab | 631493e | 2010-11-09 23:44:27 -0300 | [diff] [blame] | 151 | |
| 152 | |
Mauro Carvalho Chehab | 2f4f58d | 2010-11-17 15:46:09 -0300 | [diff] [blame] | 153 | static struct rc_map_table empty[] = { |
Mauro Carvalho Chehab | 631493e | 2010-11-09 23:44:27 -0300 | [diff] [blame] | 154 | { 0x2a, KEY_COFFEE }, |
| 155 | }; |
| 156 | |
Mauro Carvalho Chehab | d100e65 | 2010-11-17 15:56:53 -0300 | [diff] [blame] | 157 | static struct rc_map_list empty_map = { |
Mauro Carvalho Chehab | 631493e | 2010-11-09 23:44:27 -0300 | [diff] [blame] | 158 | .map = { |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 159 | .scan = empty, |
| 160 | .size = ARRAY_SIZE(empty), |
| 161 | .rc_proto = RC_PROTO_UNKNOWN, /* Legacy IR type */ |
| 162 | .name = RC_MAP_EMPTY, |
Mauro Carvalho Chehab | 631493e | 2010-11-09 23:44:27 -0300 | [diff] [blame] | 163 | } |
| 164 | }; |
| 165 | |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 166 | /** |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 167 | * ir_create_table() - initializes a scancode table |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 168 | * @rc_map: the rc_map to initialize |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 169 | * @name: name to assign to the table |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 170 | * @rc_proto: ir type to assign to the new table |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 171 | * @size: initial size of the table |
| 172 | * @return: zero on success or a negative error code |
| 173 | * |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 174 | * This routine will initialize the rc_map and will allocate |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 175 | * memory to hold at least the specified number of elements. |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 176 | */ |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 177 | static int ir_create_table(struct rc_map *rc_map, |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 178 | const char *name, u64 rc_proto, size_t size) |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 179 | { |
Hans Verkuil | d54fc3b | 2016-06-26 07:44:56 -0300 | [diff] [blame] | 180 | rc_map->name = kstrdup(name, GFP_KERNEL); |
| 181 | if (!rc_map->name) |
| 182 | return -ENOMEM; |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 183 | rc_map->rc_proto = rc_proto; |
Mauro Carvalho Chehab | 2f4f58d | 2010-11-17 15:46:09 -0300 | [diff] [blame] | 184 | 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 Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 186 | rc_map->scan = kmalloc(rc_map->alloc, GFP_KERNEL); |
Hans Verkuil | d54fc3b | 2016-06-26 07:44:56 -0300 | [diff] [blame] | 187 | if (!rc_map->scan) { |
| 188 | kfree(rc_map->name); |
| 189 | rc_map->name = NULL; |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 190 | return -ENOMEM; |
Hans Verkuil | d54fc3b | 2016-06-26 07:44:56 -0300 | [diff] [blame] | 191 | } |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 192 | |
| 193 | IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n", |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 194 | rc_map->size, rc_map->alloc); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * ir_free_table() - frees memory allocated by a scancode table |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 200 | * @rc_map: the table whose mappings need to be freed |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 201 | * |
| 202 | * This routine will free memory alloctaed for key mappings used by given |
| 203 | * scancode table. |
| 204 | */ |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 205 | static void ir_free_table(struct rc_map *rc_map) |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 206 | { |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 207 | rc_map->size = 0; |
Hans Verkuil | d54fc3b | 2016-06-26 07:44:56 -0300 | [diff] [blame] | 208 | kfree(rc_map->name); |
Max Kellermann | c183d35 | 2016-08-09 18:32:06 -0300 | [diff] [blame] | 209 | rc_map->name = NULL; |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 210 | kfree(rc_map->scan); |
| 211 | rc_map->scan = NULL; |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /** |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 215 | * ir_resize_table() - resizes a scancode table if necessary |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 216 | * @rc_map: the rc_map to resize |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 217 | * @gfp_flags: gfp flags to use when allocating memory |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 218 | * @return: zero on success or a negative error code |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 219 | * |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 220 | * This routine will shrink the rc_map if it has lots of |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 221 | * unused entries and grow it if it is full. |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 222 | */ |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 223 | static int ir_resize_table(struct rc_map *rc_map, gfp_t gfp_flags) |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 224 | { |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 225 | unsigned int oldalloc = rc_map->alloc; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 226 | unsigned int newalloc = oldalloc; |
Mauro Carvalho Chehab | 2f4f58d | 2010-11-17 15:46:09 -0300 | [diff] [blame] | 227 | struct rc_map_table *oldscan = rc_map->scan; |
| 228 | struct rc_map_table *newscan; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 229 | |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 230 | if (rc_map->size == rc_map->len) { |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 231 | /* All entries in use -> grow keytable */ |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 232 | if (rc_map->alloc >= IR_TAB_MAX_SIZE) |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 233 | return -ENOMEM; |
| 234 | |
| 235 | newalloc *= 2; |
| 236 | IR_dprintk(1, "Growing table to %u bytes\n", newalloc); |
| 237 | } |
| 238 | |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 239 | if ((rc_map->len * 3 < rc_map->size) && (oldalloc > IR_TAB_MIN_SIZE)) { |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 240 | /* 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 Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 248 | newscan = kmalloc(newalloc, gfp_flags); |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 249 | if (!newscan) { |
| 250 | IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc); |
| 251 | return -ENOMEM; |
| 252 | } |
| 253 | |
Mauro Carvalho Chehab | 2f4f58d | 2010-11-17 15:46:09 -0300 | [diff] [blame] | 254 | memcpy(newscan, rc_map->scan, rc_map->len * sizeof(struct rc_map_table)); |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 255 | rc_map->scan = newscan; |
| 256 | rc_map->alloc = newalloc; |
Mauro Carvalho Chehab | 2f4f58d | 2010-11-17 15:46:09 -0300 | [diff] [blame] | 257 | rc_map->size = rc_map->alloc / sizeof(struct rc_map_table); |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 258 | kfree(oldscan); |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | /** |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 263 | * ir_update_mapping() - set a keycode in the scancode->keycode table |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 264 | * @dev: the struct rc_dev device descriptor |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 265 | * @rc_map: scancode table to be adjusted |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 266 | * @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ärdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 269 | * |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 270 | * This routine is used to update scancode->keycode mapping at given |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 271 | * position. |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 272 | */ |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 273 | static unsigned int ir_update_mapping(struct rc_dev *dev, |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 274 | struct rc_map *rc_map, |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 275 | unsigned int index, |
| 276 | unsigned int new_keycode) |
| 277 | { |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 278 | int old_keycode = rc_map->scan[index].keycode; |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 279 | 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 Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 284 | index, rc_map->scan[index].scancode); |
| 285 | rc_map->len--; |
| 286 | memmove(&rc_map->scan[index], &rc_map->scan[index+ 1], |
Mauro Carvalho Chehab | 2f4f58d | 2010-11-17 15:46:09 -0300 | [diff] [blame] | 287 | (rc_map->len - index) * sizeof(struct rc_map_table)); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 288 | } 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 Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 292 | rc_map->scan[index].scancode, new_keycode); |
| 293 | rc_map->scan[index].keycode = new_keycode; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 294 | __set_bit(new_keycode, dev->input_dev->keybit); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | if (old_keycode != KEY_RESERVED) { |
| 298 | /* A previous mapping was updated... */ |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 299 | __clear_bit(old_keycode, dev->input_dev->keybit); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 300 | /* ... but another scancode might use the same keycode */ |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 301 | for (i = 0; i < rc_map->len; i++) { |
| 302 | if (rc_map->scan[i].keycode == old_keycode) { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 303 | __set_bit(old_keycode, dev->input_dev->keybit); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 304 | break; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /* Possibly shrink the keytable, failure is not a problem */ |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 309 | ir_resize_table(rc_map, GFP_ATOMIC); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | return old_keycode; |
| 313 | } |
| 314 | |
| 315 | /** |
David Härdeman | 4c7b355 | 2010-11-10 11:04:19 -0300 | [diff] [blame] | 316 | * ir_establish_scancode() - set a keycode in the scancode->keycode table |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 317 | * @dev: the struct rc_dev device descriptor |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 318 | * @rc_map: scancode table to be searched |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 319 | * @scancode: the desired scancode |
| 320 | * @resize: controls whether we allowed to resize the table to |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 321 | * accommodate not yet present scancodes |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 322 | * @return: index of the mapping containing scancode in question |
| 323 | * or -1U in case of failure. |
| 324 | * |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 325 | * This routine is used to locate given scancode in rc_map. |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 326 | * If scancode is not yet present the routine will allocate a new slot |
| 327 | * for it. |
| 328 | */ |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 329 | static unsigned int ir_establish_scancode(struct rc_dev *dev, |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 330 | struct rc_map *rc_map, |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 331 | unsigned int scancode, |
| 332 | bool resize) |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 333 | { |
| 334 | unsigned int i; |
Mauro Carvalho Chehab | 9dfe4e8 | 2010-04-04 14:06:55 -0300 | [diff] [blame] | 335 | |
| 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ärdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 341 | * IR tables from other remotes. So, we support specifying a mask to |
| 342 | * indicate the valid bits of the scancodes. |
Mauro Carvalho Chehab | 9dfe4e8 | 2010-04-04 14:06:55 -0300 | [diff] [blame] | 343 | */ |
David Härdeman | 9d2f1d3 | 2014-04-03 20:32:26 -0300 | [diff] [blame] | 344 | if (dev->scancode_mask) |
| 345 | scancode &= dev->scancode_mask; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 346 | |
| 347 | /* First check if we already have a mapping for this ir command */ |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 348 | for (i = 0; i < rc_map->len; i++) { |
| 349 | if (rc_map->scan[i].scancode == scancode) |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 350 | return i; |
| 351 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 352 | /* Keytable is sorted from lowest to highest scancode */ |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 353 | if (rc_map->scan[i].scancode >= scancode) |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 354 | break; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 355 | } |
| 356 | |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 357 | /* No previous mapping found, we might need to grow the table */ |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 358 | if (rc_map->size == rc_map->len) { |
| 359 | if (!resize || ir_resize_table(rc_map, GFP_ATOMIC)) |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 360 | return -1U; |
| 361 | } |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 362 | |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 363 | /* i is the proper index to insert our new keycode */ |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 364 | if (i < rc_map->len) |
| 365 | memmove(&rc_map->scan[i + 1], &rc_map->scan[i], |
Mauro Carvalho Chehab | 2f4f58d | 2010-11-17 15:46:09 -0300 | [diff] [blame] | 366 | (rc_map->len - i) * sizeof(struct rc_map_table)); |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 367 | rc_map->scan[i].scancode = scancode; |
| 368 | rc_map->scan[i].keycode = KEY_RESERVED; |
| 369 | rc_map->len++; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 370 | |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 371 | return i; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /** |
| 375 | * ir_setkeycode() - set a keycode in the scancode->keycode table |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 376 | * @idev: the struct input_dev device descriptor |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 377 | * @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ärdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 383 | static int ir_setkeycode(struct input_dev *idev, |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 384 | const struct input_keymap_entry *ke, |
| 385 | unsigned int *old_keycode) |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 386 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 387 | struct rc_dev *rdev = input_get_drvdata(idev); |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 388 | struct rc_map *rc_map = &rdev->rc_map; |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 389 | unsigned int index; |
| 390 | unsigned int scancode; |
Mauro Carvalho Chehab | dea8a39 | 2010-11-29 07:46:13 -0300 | [diff] [blame] | 391 | int retval = 0; |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 392 | unsigned long flags; |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 393 | |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 394 | spin_lock_irqsave(&rc_map->lock, flags); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 395 | |
| 396 | if (ke->flags & INPUT_KEYMAP_BY_INDEX) { |
| 397 | index = ke->index; |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 398 | if (index >= rc_map->len) { |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 399 | 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 Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 407 | index = ir_establish_scancode(rdev, rc_map, scancode, true); |
| 408 | if (index >= rc_map->len) { |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 409 | retval = -ENOMEM; |
| 410 | goto out; |
| 411 | } |
| 412 | } |
| 413 | |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 414 | *old_keycode = ir_update_mapping(rdev, rc_map, index, ke->keycode); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 415 | |
| 416 | out: |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 417 | spin_unlock_irqrestore(&rc_map->lock, flags); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 418 | return retval; |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /** |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 422 | * ir_setkeytable() - sets several entries in the scancode->keycode table |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 423 | * @dev: the struct rc_dev device descriptor |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 424 | * @to: the struct rc_map to copy entries to |
| 425 | * @from: the struct rc_map to copy entries from |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 426 | * @return: -ENOMEM if all keycodes could not be inserted, otherwise zero. |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 427 | * |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 428 | * This routine is used to handle table initialization. |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 429 | */ |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 430 | static int ir_setkeytable(struct rc_dev *dev, |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 431 | const struct rc_map *from) |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 432 | { |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 433 | struct rc_map *rc_map = &dev->rc_map; |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 434 | unsigned int i, index; |
| 435 | int rc; |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 436 | |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 437 | rc = ir_create_table(rc_map, from->name, |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 438 | from->rc_proto, from->size); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 439 | if (rc) |
| 440 | return rc; |
| 441 | |
| 442 | IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n", |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 443 | rc_map->size, rc_map->alloc); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 444 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 445 | for (i = 0; i < from->size; i++) { |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 446 | index = ir_establish_scancode(dev, rc_map, |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 447 | from->scan[i].scancode, false); |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 448 | if (index >= rc_map->len) { |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 449 | rc = -ENOMEM; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 450 | break; |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 453 | ir_update_mapping(dev, rc_map, index, |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 454 | from->scan[i].keycode); |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 455 | } |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 456 | |
| 457 | if (rc) |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 458 | ir_free_table(rc_map); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 459 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 460 | return rc; |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 461 | } |
| 462 | |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 463 | /** |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 464 | * ir_lookup_by_scancode() - locate mapping by scancode |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 465 | * @rc_map: the struct rc_map to search |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 466 | * @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 Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 472 | static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map, |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 473 | unsigned int scancode) |
| 474 | { |
David Härdeman | 0d07025 | 2010-10-30 22:17:44 +0200 | [diff] [blame] | 475 | int start = 0; |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 476 | int end = rc_map->len - 1; |
David Härdeman | 0d07025 | 2010-10-30 22:17:44 +0200 | [diff] [blame] | 477 | int mid; |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 478 | |
| 479 | while (start <= end) { |
| 480 | mid = (start + end) / 2; |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 481 | if (rc_map->scan[mid].scancode < scancode) |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 482 | start = mid + 1; |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 483 | else if (rc_map->scan[mid].scancode > scancode) |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 484 | end = mid - 1; |
| 485 | else |
| 486 | return mid; |
| 487 | } |
| 488 | |
| 489 | return -1U; |
| 490 | } |
| 491 | |
| 492 | /** |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 493 | * ir_getkeycode() - get a keycode from the scancode->keycode table |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 494 | * @idev: the struct input_dev device descriptor |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 495 | * @scancode: the desired scancode |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 496 | * @keycode: used to return the keycode, if found, or KEY_RESERVED |
| 497 | * @return: always returns zero. |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 498 | * |
| 499 | * This routine is used to handle evdev EVIOCGKEY ioctl. |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 500 | */ |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 501 | static int ir_getkeycode(struct input_dev *idev, |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 502 | struct input_keymap_entry *ke) |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 503 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 504 | struct rc_dev *rdev = input_get_drvdata(idev); |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 505 | struct rc_map *rc_map = &rdev->rc_map; |
Mauro Carvalho Chehab | 2f4f58d | 2010-11-17 15:46:09 -0300 | [diff] [blame] | 506 | struct rc_map_table *entry; |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 507 | unsigned long flags; |
| 508 | unsigned int index; |
| 509 | unsigned int scancode; |
| 510 | int retval; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 511 | |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 512 | spin_lock_irqsave(&rc_map->lock, flags); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 513 | |
| 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 Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 521 | index = ir_lookup_by_scancode(rc_map, scancode); |
Mauro Carvalho Chehab | e97f467 | 2009-12-04 17:17:47 -0300 | [diff] [blame] | 522 | } |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 523 | |
Dmitry Torokhov | 54e74b8 | 2011-01-28 23:33:29 -0800 | [diff] [blame] | 524 | 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 Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 541 | retval = -EINVAL; |
| 542 | goto out; |
| 543 | } |
| 544 | |
Dmitry Torokhov | 47c5ba5 | 2010-10-31 15:18:42 -0700 | [diff] [blame] | 545 | retval = 0; |
| 546 | |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 547 | out: |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 548 | spin_unlock_irqrestore(&rc_map->lock, flags); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 549 | return retval; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /** |
Mauro Carvalho Chehab | ca86674 | 2010-11-17 13:53:11 -0300 | [diff] [blame] | 553 | * rc_g_keycode_from_table() - gets the keycode that corresponds to a scancode |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 554 | * @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 Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 557 | * |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 558 | * 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 Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 561 | */ |
Mauro Carvalho Chehab | ca86674 | 2010-11-17 13:53:11 -0300 | [diff] [blame] | 562 | u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode) |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 563 | { |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 564 | struct rc_map *rc_map = &dev->rc_map; |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 565 | unsigned int keycode; |
| 566 | unsigned int index; |
| 567 | unsigned long flags; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 568 | |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 569 | spin_lock_irqsave(&rc_map->lock, flags); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 570 | |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 571 | index = ir_lookup_by_scancode(rc_map, scancode); |
| 572 | keycode = index < rc_map->len ? |
| 573 | rc_map->scan[index].keycode : KEY_RESERVED; |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 574 | |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 575 | spin_unlock_irqrestore(&rc_map->lock, flags); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 576 | |
Mauro Carvalho Chehab | 3543894 | 2010-04-03 16:53:16 -0300 | [diff] [blame] | 577 | if (keycode != KEY_RESERVED) |
| 578 | IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n", |
Sean Young | 518f4b2 | 2017-07-01 12:13:19 -0400 | [diff] [blame] | 579 | dev->device_name, scancode, keycode); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 580 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 581 | return keycode; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 582 | } |
Mauro Carvalho Chehab | ca86674 | 2010-11-17 13:53:11 -0300 | [diff] [blame] | 583 | EXPORT_SYMBOL_GPL(rc_g_keycode_from_table); |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 584 | |
| 585 | /** |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 586 | * ir_do_keyup() - internal function to signal the release of a keypress |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 587 | * @dev: the struct rc_dev descriptor of the device |
Jarod Wilson | 98c32bc | 2011-06-23 10:40:55 -0300 | [diff] [blame] | 588 | * @sync: whether or not to call input_sync |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 589 | * |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 590 | * This function is used internally to release a keypress, it must be |
| 591 | * called with keylock held. |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 592 | */ |
Jarod Wilson | 98c32bc | 2011-06-23 10:40:55 -0300 | [diff] [blame] | 593 | static void ir_do_keyup(struct rc_dev *dev, bool sync) |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 594 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 595 | if (!dev->keypressed) |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 596 | return; |
| 597 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 598 | IR_dprintk(1, "keyup key 0x%04x\n", dev->last_keycode); |
| 599 | input_report_key(dev->input_dev, dev->last_keycode, 0); |
Sean Young | 153a60b | 2013-07-30 19:00:01 -0300 | [diff] [blame] | 600 | led_trigger_event(led_feedback, LED_OFF); |
Jarod Wilson | 98c32bc | 2011-06-23 10:40:55 -0300 | [diff] [blame] | 601 | if (sync) |
| 602 | input_sync(dev->input_dev); |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 603 | dev->keypressed = false; |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 604 | } |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 605 | |
| 606 | /** |
Mauro Carvalho Chehab | ca86674 | 2010-11-17 13:53:11 -0300 | [diff] [blame] | 607 | * rc_keyup() - signals the release of a keypress |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 608 | * @dev: the struct rc_dev descriptor of the device |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 609 | * |
| 610 | * This routine is used to signal that a key has been released on the |
| 611 | * remote control. |
| 612 | */ |
Mauro Carvalho Chehab | ca86674 | 2010-11-17 13:53:11 -0300 | [diff] [blame] | 613 | void rc_keyup(struct rc_dev *dev) |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 614 | { |
| 615 | unsigned long flags; |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 616 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 617 | spin_lock_irqsave(&dev->keylock, flags); |
Jarod Wilson | 98c32bc | 2011-06-23 10:40:55 -0300 | [diff] [blame] | 618 | ir_do_keyup(dev, true); |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 619 | spin_unlock_irqrestore(&dev->keylock, flags); |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 620 | } |
Mauro Carvalho Chehab | ca86674 | 2010-11-17 13:53:11 -0300 | [diff] [blame] | 621 | EXPORT_SYMBOL_GPL(rc_keyup); |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 622 | |
| 623 | /** |
| 624 | * ir_timer_keyup() - generates a keyup event after a timeout |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 625 | * @cookie: a pointer to the struct rc_dev for the device |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 626 | * |
| 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 | */ |
| 630 | static void ir_timer_keyup(unsigned long cookie) |
| 631 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 632 | struct rc_dev *dev = (struct rc_dev *)cookie; |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 633 | 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ärdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 645 | spin_lock_irqsave(&dev->keylock, flags); |
| 646 | if (time_is_before_eq_jiffies(dev->keyup_jiffies)) |
Jarod Wilson | 98c32bc | 2011-06-23 10:40:55 -0300 | [diff] [blame] | 647 | ir_do_keyup(dev, true); |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 648 | spin_unlock_irqrestore(&dev->keylock, flags); |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | /** |
Mauro Carvalho Chehab | ca86674 | 2010-11-17 13:53:11 -0300 | [diff] [blame] | 652 | * rc_repeat() - signals that a key is still pressed |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 653 | * @dev: the struct rc_dev descriptor of the device |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 654 | * |
| 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 Chehab | ca86674 | 2010-11-17 13:53:11 -0300 | [diff] [blame] | 659 | void rc_repeat(struct rc_dev *dev) |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 660 | { |
| 661 | unsigned long flags; |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 662 | unsigned int timeout = protocols[dev->last_protocol].repeat_period; |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 663 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 664 | spin_lock_irqsave(&dev->keylock, flags); |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 665 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 666 | if (!dev->keypressed) |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 667 | goto out; |
| 668 | |
David Härdeman | 265a298 | 2017-06-22 15:23:54 -0400 | [diff] [blame] | 669 | input_event(dev->input_dev, EV_MSC, MSC_SCAN, dev->last_scancode); |
| 670 | input_sync(dev->input_dev); |
| 671 | |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 672 | dev->keyup_jiffies = jiffies + msecs_to_jiffies(timeout); |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 673 | mod_timer(&dev->timer_keyup, dev->keyup_jiffies); |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 674 | |
| 675 | out: |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 676 | spin_unlock_irqrestore(&dev->keylock, flags); |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 677 | } |
Mauro Carvalho Chehab | ca86674 | 2010-11-17 13:53:11 -0300 | [diff] [blame] | 678 | EXPORT_SYMBOL_GPL(rc_repeat); |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 679 | |
| 680 | /** |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 681 | * ir_do_keydown() - internal function to process a keypress |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 682 | * @dev: the struct rc_dev descriptor of the device |
David Härdeman | 120703f | 2014-04-03 20:31:30 -0300 | [diff] [blame] | 683 | * @protocol: the protocol of the keypress |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 684 | * @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 | */ |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 691 | static void ir_do_keydown(struct rc_dev *dev, enum rc_proto protocol, |
David Härdeman | 120703f | 2014-04-03 20:31:30 -0300 | [diff] [blame] | 692 | u32 scancode, u32 keycode, u8 toggle) |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 693 | { |
David Härdeman | 99b0f3c | 2014-04-04 19:06:06 -0300 | [diff] [blame] | 694 | bool new_event = (!dev->keypressed || |
David Härdeman | 120703f | 2014-04-03 20:31:30 -0300 | [diff] [blame] | 695 | dev->last_protocol != protocol || |
David Härdeman | 99b0f3c | 2014-04-04 19:06:06 -0300 | [diff] [blame] | 696 | dev->last_scancode != scancode || |
David Härdeman | 120703f | 2014-04-03 20:31:30 -0300 | [diff] [blame] | 697 | dev->last_toggle != toggle); |
Jarod Wilson | 98c32bc | 2011-06-23 10:40:55 -0300 | [diff] [blame] | 698 | |
| 699 | if (new_event && dev->keypressed) |
| 700 | ir_do_keyup(dev, false); |
| 701 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 702 | input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode); |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 703 | |
Jarod Wilson | 98c32bc | 2011-06-23 10:40:55 -0300 | [diff] [blame] | 704 | if (new_event && keycode != KEY_RESERVED) { |
| 705 | /* Register a keypress */ |
| 706 | dev->keypressed = true; |
David Härdeman | 120703f | 2014-04-03 20:31:30 -0300 | [diff] [blame] | 707 | dev->last_protocol = protocol; |
Jarod Wilson | 98c32bc | 2011-06-23 10:40:55 -0300 | [diff] [blame] | 708 | dev->last_scancode = scancode; |
| 709 | dev->last_toggle = toggle; |
| 710 | dev->last_keycode = keycode; |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 711 | |
Mauro Carvalho Chehab | 25ec587 | 2016-10-18 17:44:25 -0200 | [diff] [blame] | 712 | IR_dprintk(1, "%s: key down event, key 0x%04x, protocol 0x%04x, scancode 0x%08x\n", |
Sean Young | 518f4b2 | 2017-07-01 12:13:19 -0400 | [diff] [blame] | 713 | dev->device_name, keycode, protocol, scancode); |
Jarod Wilson | 98c32bc | 2011-06-23 10:40:55 -0300 | [diff] [blame] | 714 | input_report_key(dev->input_dev, keycode, 1); |
James Hogan | 70a2f91 | 2014-01-16 19:56:22 -0300 | [diff] [blame] | 715 | |
| 716 | led_trigger_event(led_feedback, LED_FULL); |
Jarod Wilson | 98c32bc | 2011-06-23 10:40:55 -0300 | [diff] [blame] | 717 | } |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 718 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 719 | input_sync(dev->input_dev); |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | /** |
Mauro Carvalho Chehab | ca86674 | 2010-11-17 13:53:11 -0300 | [diff] [blame] | 723 | * rc_keydown() - generates input event for a key press |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 724 | * @dev: the struct rc_dev descriptor of the device |
David Härdeman | 120703f | 2014-04-03 20:31:30 -0300 | [diff] [blame] | 725 | * @protocol: the protocol for the keypress |
| 726 | * @scancode: the scancode for the keypress |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 727 | * @toggle: the toggle value (protocol dependent, if the protocol doesn't |
| 728 | * support toggle values, this should be set to zero) |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 729 | * |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 730 | * This routine is used to signal that a key has been pressed on the |
| 731 | * remote control. |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 732 | */ |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 733 | void rc_keydown(struct rc_dev *dev, enum rc_proto protocol, u32 scancode, |
| 734 | u8 toggle) |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 735 | { |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 736 | unsigned long flags; |
Mauro Carvalho Chehab | ca86674 | 2010-11-17 13:53:11 -0300 | [diff] [blame] | 737 | u32 keycode = rc_g_keycode_from_table(dev, scancode); |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 738 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 739 | spin_lock_irqsave(&dev->keylock, flags); |
David Härdeman | 120703f | 2014-04-03 20:31:30 -0300 | [diff] [blame] | 740 | ir_do_keydown(dev, protocol, scancode, keycode, toggle); |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 741 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 742 | if (dev->keypressed) { |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 743 | dev->keyup_jiffies = jiffies + |
| 744 | msecs_to_jiffies(protocols[protocol].repeat_period); |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 745 | mod_timer(&dev->timer_keyup, dev->keyup_jiffies); |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 746 | } |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 747 | spin_unlock_irqrestore(&dev->keylock, flags); |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 748 | } |
Mauro Carvalho Chehab | ca86674 | 2010-11-17 13:53:11 -0300 | [diff] [blame] | 749 | EXPORT_SYMBOL_GPL(rc_keydown); |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 750 | |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 751 | /** |
Mauro Carvalho Chehab | ca86674 | 2010-11-17 13:53:11 -0300 | [diff] [blame] | 752 | * rc_keydown_notimeout() - generates input event for a key press without |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 753 | * an automatic keyup event at a later time |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 754 | * @dev: the struct rc_dev descriptor of the device |
David Härdeman | 120703f | 2014-04-03 20:31:30 -0300 | [diff] [blame] | 755 | * @protocol: the protocol for the keypress |
| 756 | * @scancode: the scancode for the keypress |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 757 | * @toggle: the toggle value (protocol dependent, if the protocol doesn't |
| 758 | * support toggle values, this should be set to zero) |
| 759 | * |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 760 | * This routine is used to signal that a key has been pressed on the |
Mauro Carvalho Chehab | ca86674 | 2010-11-17 13:53:11 -0300 | [diff] [blame] | 761 | * remote control. The driver must manually call rc_keyup() at a later stage. |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 762 | */ |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 763 | void rc_keydown_notimeout(struct rc_dev *dev, enum rc_proto protocol, |
David Härdeman | 120703f | 2014-04-03 20:31:30 -0300 | [diff] [blame] | 764 | u32 scancode, u8 toggle) |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 765 | { |
| 766 | unsigned long flags; |
Mauro Carvalho Chehab | ca86674 | 2010-11-17 13:53:11 -0300 | [diff] [blame] | 767 | u32 keycode = rc_g_keycode_from_table(dev, scancode); |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 768 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 769 | spin_lock_irqsave(&dev->keylock, flags); |
David Härdeman | 120703f | 2014-04-03 20:31:30 -0300 | [diff] [blame] | 770 | ir_do_keydown(dev, protocol, scancode, keycode, toggle); |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 771 | spin_unlock_irqrestore(&dev->keylock, flags); |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 772 | } |
Mauro Carvalho Chehab | ca86674 | 2010-11-17 13:53:11 -0300 | [diff] [blame] | 773 | EXPORT_SYMBOL_GPL(rc_keydown_notimeout); |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 774 | |
Sean Young | b590c0b | 2016-12-05 19:24:59 -0200 | [diff] [blame] | 775 | /** |
| 776 | * rc_validate_filter() - checks that the scancode and mask are valid and |
| 777 | * provides sensible defaults |
James Hogan | f423ccc | 2015-03-31 14:48:10 -0300 | [diff] [blame] | 778 | * @dev: the struct rc_dev descriptor of the device |
Sean Young | b590c0b | 2016-12-05 19:24:59 -0200 | [diff] [blame] | 779 | * @filter: the scancode and mask |
| 780 | * @return: 0 or -EINVAL if the filter is not valid |
| 781 | */ |
James Hogan | f423ccc | 2015-03-31 14:48:10 -0300 | [diff] [blame] | 782 | static int rc_validate_filter(struct rc_dev *dev, |
Sean Young | b590c0b | 2016-12-05 19:24:59 -0200 | [diff] [blame] | 783 | struct rc_scancode_filter *filter) |
| 784 | { |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 785 | u32 mask, s = filter->data; |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 786 | enum rc_proto protocol = dev->wakeup_protocol; |
Sean Young | b590c0b | 2016-12-05 19:24:59 -0200 | [diff] [blame] | 787 | |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 788 | if (protocol >= ARRAY_SIZE(protocols)) |
Sean Young | 2168b41 | 2017-08-07 09:21:29 -0400 | [diff] [blame] | 789 | return -EINVAL; |
| 790 | |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 791 | mask = protocols[protocol].scancode_bits; |
| 792 | |
Sean Young | b590c0b | 2016-12-05 19:24:59 -0200 | [diff] [blame] | 793 | switch (protocol) { |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 794 | case RC_PROTO_NECX: |
Sean Young | b590c0b | 2016-12-05 19:24:59 -0200 | [diff] [blame] | 795 | if ((((s >> 16) ^ ~(s >> 8)) & 0xff) == 0) |
| 796 | return -EINVAL; |
| 797 | break; |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 798 | case RC_PROTO_NEC32: |
Sean Young | b590c0b | 2016-12-05 19:24:59 -0200 | [diff] [blame] | 799 | if ((((s >> 24) ^ ~(s >> 16)) & 0xff) == 0) |
| 800 | return -EINVAL; |
| 801 | break; |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 802 | case RC_PROTO_RC6_MCE: |
Sean Young | b590c0b | 2016-12-05 19:24:59 -0200 | [diff] [blame] | 803 | if ((s & 0xffff0000) != 0x800f0000) |
| 804 | return -EINVAL; |
| 805 | break; |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 806 | case RC_PROTO_RC6_6A_32: |
Sean Young | b590c0b | 2016-12-05 19:24:59 -0200 | [diff] [blame] | 807 | if ((s & 0xffff0000) == 0x800f0000) |
| 808 | return -EINVAL; |
| 809 | break; |
| 810 | default: |
| 811 | break; |
| 812 | } |
| 813 | |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 814 | filter->data &= mask; |
| 815 | filter->mask &= mask; |
Sean Young | b590c0b | 2016-12-05 19:24:59 -0200 | [diff] [blame] | 816 | |
James Hogan | f423ccc | 2015-03-31 14:48:10 -0300 | [diff] [blame] | 817 | /* |
| 818 | * If we have to raw encode the IR for wakeup, we cannot have a mask |
| 819 | */ |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 820 | if (dev->encode_wakeup && filter->mask != 0 && filter->mask != mask) |
James Hogan | f423ccc | 2015-03-31 14:48:10 -0300 | [diff] [blame] | 821 | return -EINVAL; |
| 822 | |
Sean Young | b590c0b | 2016-12-05 19:24:59 -0200 | [diff] [blame] | 823 | return 0; |
| 824 | } |
| 825 | |
Srinivas Kandagatla | 8b2ff32 | 2013-07-22 04:22:57 -0300 | [diff] [blame] | 826 | int rc_open(struct rc_dev *rdev) |
| 827 | { |
| 828 | int rval = 0; |
| 829 | |
| 830 | if (!rdev) |
| 831 | return -EINVAL; |
| 832 | |
| 833 | mutex_lock(&rdev->lock); |
Mauro Carvalho Chehab | c73bbaa | 2016-02-11 10:33:31 -0200 | [diff] [blame] | 834 | |
Juergen Lock | f02dcdd | 2013-08-16 15:00:24 -0300 | [diff] [blame] | 835 | if (!rdev->users++ && rdev->open != NULL) |
Srinivas Kandagatla | 8b2ff32 | 2013-07-22 04:22:57 -0300 | [diff] [blame] | 836 | rval = rdev->open(rdev); |
| 837 | |
| 838 | if (rval) |
| 839 | rdev->users--; |
| 840 | |
| 841 | mutex_unlock(&rdev->lock); |
| 842 | |
| 843 | return rval; |
| 844 | } |
| 845 | EXPORT_SYMBOL_GPL(rc_open); |
| 846 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 847 | static int ir_open(struct input_dev *idev) |
Mauro Carvalho Chehab | 716aab4 | 2010-03-31 14:40:35 -0300 | [diff] [blame] | 848 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 849 | struct rc_dev *rdev = input_get_drvdata(idev); |
Mauro Carvalho Chehab | 716aab4 | 2010-03-31 14:40:35 -0300 | [diff] [blame] | 850 | |
Srinivas Kandagatla | 8b2ff32 | 2013-07-22 04:22:57 -0300 | [diff] [blame] | 851 | return rc_open(rdev); |
Mauro Carvalho Chehab | 716aab4 | 2010-03-31 14:40:35 -0300 | [diff] [blame] | 852 | } |
| 853 | |
Srinivas Kandagatla | 8b2ff32 | 2013-07-22 04:22:57 -0300 | [diff] [blame] | 854 | void rc_close(struct rc_dev *rdev) |
| 855 | { |
| 856 | if (rdev) { |
| 857 | mutex_lock(&rdev->lock); |
| 858 | |
Mauro Carvalho Chehab | 81b7d14 | 2015-04-28 09:43:17 -0300 | [diff] [blame] | 859 | if (!--rdev->users && rdev->close != NULL) |
Srinivas Kandagatla | 8b2ff32 | 2013-07-22 04:22:57 -0300 | [diff] [blame] | 860 | rdev->close(rdev); |
| 861 | |
| 862 | mutex_unlock(&rdev->lock); |
| 863 | } |
| 864 | } |
| 865 | EXPORT_SYMBOL_GPL(rc_close); |
| 866 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 867 | static void ir_close(struct input_dev *idev) |
Mauro Carvalho Chehab | 716aab4 | 2010-03-31 14:40:35 -0300 | [diff] [blame] | 868 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 869 | struct rc_dev *rdev = input_get_drvdata(idev); |
Srinivas Kandagatla | 8b2ff32 | 2013-07-22 04:22:57 -0300 | [diff] [blame] | 870 | rc_close(rdev); |
Mauro Carvalho Chehab | 716aab4 | 2010-03-31 14:40:35 -0300 | [diff] [blame] | 871 | } |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 872 | |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 873 | /* class for /sys/class/rc */ |
David Härdeman | 40fc532 | 2013-03-06 16:52:10 -0300 | [diff] [blame] | 874 | static char *rc_devnode(struct device *dev, umode_t *mode) |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 875 | { |
| 876 | return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev)); |
| 877 | } |
| 878 | |
David Härdeman | 40fc532 | 2013-03-06 16:52:10 -0300 | [diff] [blame] | 879 | static struct class rc_class = { |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 880 | .name = "rc", |
David Härdeman | 40fc532 | 2013-03-06 16:52:10 -0300 | [diff] [blame] | 881 | .devnode = rc_devnode, |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 882 | }; |
| 883 | |
David Härdeman | c003ab1 | 2012-10-11 19:11:54 -0300 | [diff] [blame] | 884 | /* |
| 885 | * These are the protocol textual descriptions that are |
| 886 | * used by the sysfs protocols file. Note that the order |
| 887 | * of the entries is relevant. |
| 888 | */ |
Heiner Kallweit | 53df877 | 2015-11-16 17:52:17 -0200 | [diff] [blame] | 889 | static const struct { |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 890 | u64 type; |
Heiner Kallweit | 53df877 | 2015-11-16 17:52:17 -0200 | [diff] [blame] | 891 | const char *name; |
Heiner Kallweit | 9f0bf36 | 2015-11-16 17:52:08 -0200 | [diff] [blame] | 892 | const char *module_name; |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 893 | } proto_names[] = { |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 894 | { RC_PROTO_BIT_NONE, "none", NULL }, |
| 895 | { RC_PROTO_BIT_OTHER, "other", NULL }, |
| 896 | { RC_PROTO_BIT_UNKNOWN, "unknown", NULL }, |
| 897 | { RC_PROTO_BIT_RC5 | |
| 898 | RC_PROTO_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" }, |
| 899 | { RC_PROTO_BIT_NEC | |
| 900 | RC_PROTO_BIT_NECX | |
| 901 | RC_PROTO_BIT_NEC32, "nec", "ir-nec-decoder" }, |
| 902 | { RC_PROTO_BIT_RC6_0 | |
| 903 | RC_PROTO_BIT_RC6_6A_20 | |
| 904 | RC_PROTO_BIT_RC6_6A_24 | |
| 905 | RC_PROTO_BIT_RC6_6A_32 | |
| 906 | RC_PROTO_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" }, |
| 907 | { RC_PROTO_BIT_JVC, "jvc", "ir-jvc-decoder" }, |
| 908 | { RC_PROTO_BIT_SONY12 | |
| 909 | RC_PROTO_BIT_SONY15 | |
| 910 | RC_PROTO_BIT_SONY20, "sony", "ir-sony-decoder" }, |
| 911 | { RC_PROTO_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" }, |
| 912 | { RC_PROTO_BIT_SANYO, "sanyo", "ir-sanyo-decoder" }, |
| 913 | { RC_PROTO_BIT_SHARP, "sharp", "ir-sharp-decoder" }, |
| 914 | { RC_PROTO_BIT_MCIR2_KBD | |
| 915 | RC_PROTO_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" }, |
| 916 | { RC_PROTO_BIT_XMP, "xmp", "ir-xmp-decoder" }, |
| 917 | { RC_PROTO_BIT_CEC, "cec", NULL }, |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 918 | }; |
| 919 | |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 920 | /** |
James Hogan | ab88c66 | 2014-02-28 20:17:05 -0300 | [diff] [blame] | 921 | * struct rc_filter_attribute - Device attribute relating to a filter type. |
| 922 | * @attr: Device attribute. |
| 923 | * @type: Filter type. |
| 924 | * @mask: false for filter value, true for filter mask. |
| 925 | */ |
| 926 | struct rc_filter_attribute { |
| 927 | struct device_attribute attr; |
| 928 | enum rc_filter_type type; |
| 929 | bool mask; |
| 930 | }; |
| 931 | #define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr) |
| 932 | |
James Hogan | ab88c66 | 2014-02-28 20:17:05 -0300 | [diff] [blame] | 933 | #define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \ |
| 934 | struct rc_filter_attribute dev_attr_##_name = { \ |
| 935 | .attr = __ATTR(_name, _mode, _show, _store), \ |
| 936 | .type = (_type), \ |
| 937 | .mask = (_mask), \ |
| 938 | } |
| 939 | |
David Härdeman | dd6ff6a | 2015-07-22 17:55:24 -0300 | [diff] [blame] | 940 | static bool lirc_is_present(void) |
| 941 | { |
| 942 | #if defined(CONFIG_LIRC_MODULE) |
| 943 | struct module *lirc; |
| 944 | |
| 945 | mutex_lock(&module_mutex); |
| 946 | lirc = find_module("lirc_dev"); |
| 947 | mutex_unlock(&module_mutex); |
| 948 | |
| 949 | return lirc ? true : false; |
| 950 | #elif defined(CONFIG_LIRC) |
| 951 | return true; |
| 952 | #else |
| 953 | return false; |
| 954 | #endif |
| 955 | } |
| 956 | |
James Hogan | ab88c66 | 2014-02-28 20:17:05 -0300 | [diff] [blame] | 957 | /** |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 958 | * show_protocols() - shows the current IR protocol(s) |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 959 | * @device: the device descriptor |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 960 | * @mattr: the device attribute struct |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 961 | * @buf: a pointer to the output buffer |
| 962 | * |
| 963 | * This routine is a callback routine for input read the IR protocol type(s). |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 964 | * it is trigged by reading /sys/class/rc/rc?/protocols. |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 965 | * It returns the protocol names of supported protocols. |
| 966 | * Enabled protocols are printed in brackets. |
Jarod Wilson | 08aeb7c | 2011-05-11 15:14:31 -0300 | [diff] [blame] | 967 | * |
David Härdeman | 18726a3 | 2017-04-27 17:34:08 -0300 | [diff] [blame] | 968 | * dev->lock is taken to guard against races between |
| 969 | * store_protocols and show_protocols. |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 970 | */ |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 971 | static ssize_t show_protocols(struct device *device, |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 972 | struct device_attribute *mattr, char *buf) |
| 973 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 974 | struct rc_dev *dev = to_rc_dev(device); |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 975 | u64 allowed, enabled; |
| 976 | char *tmp = buf; |
| 977 | int i; |
| 978 | |
Jarod Wilson | 08aeb7c | 2011-05-11 15:14:31 -0300 | [diff] [blame] | 979 | mutex_lock(&dev->lock); |
| 980 | |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 981 | enabled = dev->enabled_protocols; |
| 982 | allowed = dev->allowed_protocols; |
| 983 | if (dev->raw && !allowed) |
| 984 | allowed = ir_raw_get_allowed_protocols(); |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 985 | |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 986 | mutex_unlock(&dev->lock); |
| 987 | |
| 988 | IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n", |
| 989 | __func__, (long long)allowed, (long long)enabled); |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 990 | |
| 991 | for (i = 0; i < ARRAY_SIZE(proto_names); i++) { |
| 992 | if (allowed & enabled & proto_names[i].type) |
| 993 | tmp += sprintf(tmp, "[%s] ", proto_names[i].name); |
| 994 | else if (allowed & proto_names[i].type) |
| 995 | tmp += sprintf(tmp, "%s ", proto_names[i].name); |
David Härdeman | c003ab1 | 2012-10-11 19:11:54 -0300 | [diff] [blame] | 996 | |
| 997 | if (allowed & proto_names[i].type) |
| 998 | allowed &= ~proto_names[i].type; |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 999 | } |
| 1000 | |
David Härdeman | dd6ff6a | 2015-07-22 17:55:24 -0300 | [diff] [blame] | 1001 | if (dev->driver_type == RC_DRIVER_IR_RAW && lirc_is_present()) |
David Härdeman | 275ddb4 | 2015-05-19 19:03:22 -0300 | [diff] [blame] | 1002 | tmp += sprintf(tmp, "[lirc] "); |
| 1003 | |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1004 | if (tmp != buf) |
| 1005 | tmp--; |
| 1006 | *tmp = '\n'; |
Jarod Wilson | 08aeb7c | 2011-05-11 15:14:31 -0300 | [diff] [blame] | 1007 | |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1008 | return tmp + 1 - buf; |
| 1009 | } |
| 1010 | |
| 1011 | /** |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1012 | * parse_protocol_change() - parses a protocol change request |
| 1013 | * @protocols: pointer to the bitmask of current protocols |
| 1014 | * @buf: pointer to the buffer with a list of changes |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1015 | * |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1016 | * Writing "+proto" will add a protocol to the protocol mask. |
| 1017 | * Writing "-proto" will remove a protocol from protocol mask. |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1018 | * Writing "proto" will enable only "proto". |
| 1019 | * Writing "none" will disable all protocols. |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1020 | * Returns the number of changes performed or a negative error code. |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1021 | */ |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1022 | static int parse_protocol_change(u64 *protocols, const char *buf) |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1023 | { |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1024 | const char *tmp; |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1025 | unsigned count = 0; |
| 1026 | bool enable, disable; |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1027 | u64 mask; |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1028 | int i; |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1029 | |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1030 | while ((tmp = strsep((char **)&buf, " \n")) != NULL) { |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1031 | if (!*tmp) |
| 1032 | break; |
| 1033 | |
| 1034 | if (*tmp == '+') { |
| 1035 | enable = true; |
| 1036 | disable = false; |
| 1037 | tmp++; |
| 1038 | } else if (*tmp == '-') { |
| 1039 | enable = false; |
| 1040 | disable = true; |
| 1041 | tmp++; |
| 1042 | } else { |
| 1043 | enable = false; |
| 1044 | disable = false; |
| 1045 | } |
| 1046 | |
David Härdeman | c003ab1 | 2012-10-11 19:11:54 -0300 | [diff] [blame] | 1047 | for (i = 0; i < ARRAY_SIZE(proto_names); i++) { |
| 1048 | if (!strcasecmp(tmp, proto_names[i].name)) { |
| 1049 | mask = proto_names[i].type; |
| 1050 | break; |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1051 | } |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1052 | } |
| 1053 | |
David Härdeman | c003ab1 | 2012-10-11 19:11:54 -0300 | [diff] [blame] | 1054 | if (i == ARRAY_SIZE(proto_names)) { |
David Härdeman | 275ddb4 | 2015-05-19 19:03:22 -0300 | [diff] [blame] | 1055 | if (!strcasecmp(tmp, "lirc")) |
| 1056 | mask = 0; |
| 1057 | else { |
| 1058 | IR_dprintk(1, "Unknown protocol: '%s'\n", tmp); |
| 1059 | return -EINVAL; |
| 1060 | } |
David Härdeman | c003ab1 | 2012-10-11 19:11:54 -0300 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | count++; |
| 1064 | |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1065 | if (enable) |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1066 | *protocols |= mask; |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1067 | else if (disable) |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1068 | *protocols &= ~mask; |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1069 | else |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1070 | *protocols = mask; |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | if (!count) { |
| 1074 | IR_dprintk(1, "Protocol not specified\n"); |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1075 | return -EINVAL; |
| 1076 | } |
| 1077 | |
| 1078 | return count; |
| 1079 | } |
| 1080 | |
Heiner Kallweit | 9f0bf36 | 2015-11-16 17:52:08 -0200 | [diff] [blame] | 1081 | static void ir_raw_load_modules(u64 *protocols) |
Heiner Kallweit | 9f0bf36 | 2015-11-16 17:52:08 -0200 | [diff] [blame] | 1082 | { |
| 1083 | u64 available; |
| 1084 | int i, ret; |
| 1085 | |
| 1086 | for (i = 0; i < ARRAY_SIZE(proto_names); i++) { |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 1087 | if (proto_names[i].type == RC_PROTO_BIT_NONE || |
| 1088 | proto_names[i].type & (RC_PROTO_BIT_OTHER | |
| 1089 | RC_PROTO_BIT_UNKNOWN)) |
Heiner Kallweit | 9f0bf36 | 2015-11-16 17:52:08 -0200 | [diff] [blame] | 1090 | continue; |
| 1091 | |
| 1092 | available = ir_raw_get_allowed_protocols(); |
| 1093 | if (!(*protocols & proto_names[i].type & ~available)) |
| 1094 | continue; |
| 1095 | |
| 1096 | if (!proto_names[i].module_name) { |
| 1097 | pr_err("Can't enable IR protocol %s\n", |
| 1098 | proto_names[i].name); |
| 1099 | *protocols &= ~proto_names[i].type; |
| 1100 | continue; |
| 1101 | } |
| 1102 | |
| 1103 | ret = request_module("%s", proto_names[i].module_name); |
| 1104 | if (ret < 0) { |
| 1105 | pr_err("Couldn't load IR protocol module %s\n", |
| 1106 | proto_names[i].module_name); |
| 1107 | *protocols &= ~proto_names[i].type; |
| 1108 | continue; |
| 1109 | } |
| 1110 | msleep(20); |
| 1111 | available = ir_raw_get_allowed_protocols(); |
| 1112 | if (!(*protocols & proto_names[i].type & ~available)) |
| 1113 | continue; |
| 1114 | |
Sean Young | 8caebcd | 2017-01-19 19:33:49 -0200 | [diff] [blame] | 1115 | pr_err("Loaded IR protocol module %s, but protocol %s still not available\n", |
Heiner Kallweit | 9f0bf36 | 2015-11-16 17:52:08 -0200 | [diff] [blame] | 1116 | proto_names[i].module_name, |
| 1117 | proto_names[i].name); |
| 1118 | *protocols &= ~proto_names[i].type; |
| 1119 | } |
| 1120 | } |
| 1121 | |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1122 | /** |
| 1123 | * store_protocols() - changes the current/wakeup IR protocol(s) |
| 1124 | * @device: the device descriptor |
| 1125 | * @mattr: the device attribute struct |
| 1126 | * @buf: a pointer to the input buffer |
| 1127 | * @len: length of the input buffer |
| 1128 | * |
| 1129 | * This routine is for changing the IR protocol type. |
| 1130 | * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols. |
| 1131 | * See parse_protocol_change() for the valid commands. |
| 1132 | * Returns @len on success or a negative error code. |
| 1133 | * |
David Härdeman | 18726a3 | 2017-04-27 17:34:08 -0300 | [diff] [blame] | 1134 | * dev->lock is taken to guard against races between |
| 1135 | * store_protocols and show_protocols. |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1136 | */ |
| 1137 | static ssize_t store_protocols(struct device *device, |
| 1138 | struct device_attribute *mattr, |
| 1139 | const char *buf, size_t len) |
| 1140 | { |
| 1141 | struct rc_dev *dev = to_rc_dev(device); |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1142 | u64 *current_protocols; |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1143 | struct rc_scancode_filter *filter; |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1144 | u64 old_protocols, new_protocols; |
| 1145 | ssize_t rc; |
| 1146 | |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1147 | IR_dprintk(1, "Normal protocol change requested\n"); |
| 1148 | current_protocols = &dev->enabled_protocols; |
| 1149 | filter = &dev->scancode_filter; |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1150 | |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1151 | if (!dev->change_protocol) { |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1152 | IR_dprintk(1, "Protocol switching not supported\n"); |
| 1153 | return -EINVAL; |
| 1154 | } |
| 1155 | |
| 1156 | mutex_lock(&dev->lock); |
| 1157 | |
| 1158 | old_protocols = *current_protocols; |
| 1159 | new_protocols = old_protocols; |
| 1160 | rc = parse_protocol_change(&new_protocols, buf); |
| 1161 | if (rc < 0) |
| 1162 | goto out; |
| 1163 | |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1164 | rc = dev->change_protocol(dev, &new_protocols); |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1165 | if (rc < 0) { |
| 1166 | IR_dprintk(1, "Error setting protocols to 0x%llx\n", |
| 1167 | (long long)new_protocols); |
Jarod Wilson | 08aeb7c | 2011-05-11 15:14:31 -0300 | [diff] [blame] | 1168 | goto out; |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1169 | } |
| 1170 | |
Heiner Kallweit | 9f0bf36 | 2015-11-16 17:52:08 -0200 | [diff] [blame] | 1171 | if (dev->driver_type == RC_DRIVER_IR_RAW) |
| 1172 | ir_raw_load_modules(&new_protocols); |
| 1173 | |
James Hogan | 983c5bd | 2014-12-08 13:17:07 -0300 | [diff] [blame] | 1174 | if (new_protocols != old_protocols) { |
| 1175 | *current_protocols = new_protocols; |
| 1176 | IR_dprintk(1, "Protocols changed to 0x%llx\n", |
| 1177 | (long long)new_protocols); |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1178 | } |
| 1179 | |
James Hogan | 6bea25a | 2014-02-28 20:17:06 -0300 | [diff] [blame] | 1180 | /* |
James Hogan | 983c5bd | 2014-12-08 13:17:07 -0300 | [diff] [blame] | 1181 | * If a protocol change was attempted the filter may need updating, even |
| 1182 | * if the actual protocol mask hasn't changed (since the driver may have |
| 1183 | * cleared the filter). |
James Hogan | 6bea25a | 2014-02-28 20:17:06 -0300 | [diff] [blame] | 1184 | * Try setting the same filter with the new protocol (if any). |
| 1185 | * Fall back to clearing the filter. |
| 1186 | */ |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1187 | if (dev->s_filter && filter->mask) { |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1188 | if (new_protocols) |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1189 | rc = dev->s_filter(dev, filter); |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1190 | else |
| 1191 | rc = -1; |
David Härdeman | 23c843b | 2014-04-04 19:06:01 -0300 | [diff] [blame] | 1192 | |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1193 | if (rc < 0) { |
| 1194 | filter->data = 0; |
| 1195 | filter->mask = 0; |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1196 | dev->s_filter(dev, filter); |
James Hogan | 6bea25a | 2014-02-28 20:17:06 -0300 | [diff] [blame] | 1197 | } |
James Hogan | 6bea25a | 2014-02-28 20:17:06 -0300 | [diff] [blame] | 1198 | } |
| 1199 | |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1200 | rc = len; |
Jarod Wilson | 08aeb7c | 2011-05-11 15:14:31 -0300 | [diff] [blame] | 1201 | |
| 1202 | out: |
| 1203 | mutex_unlock(&dev->lock); |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1204 | return rc; |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1205 | } |
| 1206 | |
James Hogan | 00942d1 | 2014-01-17 10:58:49 -0300 | [diff] [blame] | 1207 | /** |
James Hogan | 00942d1 | 2014-01-17 10:58:49 -0300 | [diff] [blame] | 1208 | * show_filter() - shows the current scancode filter value or mask |
| 1209 | * @device: the device descriptor |
| 1210 | * @attr: the device attribute struct |
| 1211 | * @buf: a pointer to the output buffer |
| 1212 | * |
| 1213 | * This routine is a callback routine to read a scancode filter value or mask. |
| 1214 | * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask]. |
| 1215 | * It prints the current scancode filter value or mask of the appropriate filter |
| 1216 | * type in hexadecimal into @buf and returns the size of the buffer. |
| 1217 | * |
| 1218 | * Bits of the filter value corresponding to set bits in the filter mask are |
| 1219 | * compared against input scancodes and non-matching scancodes are discarded. |
| 1220 | * |
David Härdeman | 18726a3 | 2017-04-27 17:34:08 -0300 | [diff] [blame] | 1221 | * dev->lock is taken to guard against races between |
James Hogan | 00942d1 | 2014-01-17 10:58:49 -0300 | [diff] [blame] | 1222 | * store_filter and show_filter. |
| 1223 | */ |
| 1224 | static ssize_t show_filter(struct device *device, |
| 1225 | struct device_attribute *attr, |
| 1226 | char *buf) |
| 1227 | { |
| 1228 | struct rc_dev *dev = to_rc_dev(device); |
| 1229 | struct rc_filter_attribute *fattr = to_rc_filter_attr(attr); |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1230 | struct rc_scancode_filter *filter; |
James Hogan | 00942d1 | 2014-01-17 10:58:49 -0300 | [diff] [blame] | 1231 | u32 val; |
| 1232 | |
Mauro Carvalho Chehab | c73bbaa | 2016-02-11 10:33:31 -0200 | [diff] [blame] | 1233 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | c73bbaa | 2016-02-11 10:33:31 -0200 | [diff] [blame] | 1234 | |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1235 | if (fattr->type == RC_FILTER_NORMAL) |
David Härdeman | c5540fb | 2014-04-03 20:32:21 -0300 | [diff] [blame] | 1236 | filter = &dev->scancode_filter; |
James Hogan | 00942d1 | 2014-01-17 10:58:49 -0300 | [diff] [blame] | 1237 | else |
David Härdeman | c5540fb | 2014-04-03 20:32:21 -0300 | [diff] [blame] | 1238 | filter = &dev->scancode_wakeup_filter; |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1239 | |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1240 | if (fattr->mask) |
| 1241 | val = filter->mask; |
| 1242 | else |
| 1243 | val = filter->data; |
James Hogan | 00942d1 | 2014-01-17 10:58:49 -0300 | [diff] [blame] | 1244 | mutex_unlock(&dev->lock); |
| 1245 | |
| 1246 | return sprintf(buf, "%#x\n", val); |
| 1247 | } |
| 1248 | |
| 1249 | /** |
| 1250 | * store_filter() - changes the scancode filter value |
| 1251 | * @device: the device descriptor |
| 1252 | * @attr: the device attribute struct |
| 1253 | * @buf: a pointer to the input buffer |
| 1254 | * @len: length of the input buffer |
| 1255 | * |
| 1256 | * This routine is for changing a scancode filter value or mask. |
| 1257 | * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask]. |
| 1258 | * Returns -EINVAL if an invalid filter value for the current protocol was |
| 1259 | * specified or if scancode filtering is not supported by the driver, otherwise |
| 1260 | * returns @len. |
| 1261 | * |
| 1262 | * Bits of the filter value corresponding to set bits in the filter mask are |
| 1263 | * compared against input scancodes and non-matching scancodes are discarded. |
| 1264 | * |
David Härdeman | 18726a3 | 2017-04-27 17:34:08 -0300 | [diff] [blame] | 1265 | * dev->lock is taken to guard against races between |
James Hogan | 00942d1 | 2014-01-17 10:58:49 -0300 | [diff] [blame] | 1266 | * store_filter and show_filter. |
| 1267 | */ |
| 1268 | static ssize_t store_filter(struct device *device, |
| 1269 | struct device_attribute *attr, |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1270 | const char *buf, size_t len) |
James Hogan | 00942d1 | 2014-01-17 10:58:49 -0300 | [diff] [blame] | 1271 | { |
| 1272 | struct rc_dev *dev = to_rc_dev(device); |
| 1273 | struct rc_filter_attribute *fattr = to_rc_filter_attr(attr); |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1274 | struct rc_scancode_filter new_filter, *filter; |
James Hogan | 00942d1 | 2014-01-17 10:58:49 -0300 | [diff] [blame] | 1275 | int ret; |
| 1276 | unsigned long val; |
David Härdeman | 23c843b | 2014-04-04 19:06:01 -0300 | [diff] [blame] | 1277 | int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter); |
James Hogan | 00942d1 | 2014-01-17 10:58:49 -0300 | [diff] [blame] | 1278 | |
James Hogan | 00942d1 | 2014-01-17 10:58:49 -0300 | [diff] [blame] | 1279 | ret = kstrtoul(buf, 0, &val); |
| 1280 | if (ret < 0) |
| 1281 | return ret; |
| 1282 | |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1283 | if (fattr->type == RC_FILTER_NORMAL) { |
| 1284 | set_filter = dev->s_filter; |
David Härdeman | c5540fb | 2014-04-03 20:32:21 -0300 | [diff] [blame] | 1285 | filter = &dev->scancode_filter; |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1286 | } else { |
| 1287 | set_filter = dev->s_wakeup_filter; |
David Härdeman | c5540fb | 2014-04-03 20:32:21 -0300 | [diff] [blame] | 1288 | filter = &dev->scancode_wakeup_filter; |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1289 | } |
| 1290 | |
David Härdeman | 99b0f3c | 2014-04-04 19:06:06 -0300 | [diff] [blame] | 1291 | if (!set_filter) |
| 1292 | return -EINVAL; |
James Hogan | 00942d1 | 2014-01-17 10:58:49 -0300 | [diff] [blame] | 1293 | |
| 1294 | mutex_lock(&dev->lock); |
| 1295 | |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1296 | new_filter = *filter; |
James Hogan | 00942d1 | 2014-01-17 10:58:49 -0300 | [diff] [blame] | 1297 | if (fattr->mask) |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1298 | new_filter.mask = val; |
James Hogan | 00942d1 | 2014-01-17 10:58:49 -0300 | [diff] [blame] | 1299 | else |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1300 | new_filter.data = val; |
David Härdeman | 23c843b | 2014-04-04 19:06:01 -0300 | [diff] [blame] | 1301 | |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1302 | if (fattr->type == RC_FILTER_WAKEUP) { |
Sean Young | b590c0b | 2016-12-05 19:24:59 -0200 | [diff] [blame] | 1303 | /* |
| 1304 | * Refuse to set a filter unless a protocol is enabled |
| 1305 | * and the filter is valid for that protocol |
| 1306 | */ |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 1307 | if (dev->wakeup_protocol != RC_PROTO_UNKNOWN) |
James Hogan | f423ccc | 2015-03-31 14:48:10 -0300 | [diff] [blame] | 1308 | ret = rc_validate_filter(dev, &new_filter); |
Sean Young | b590c0b | 2016-12-05 19:24:59 -0200 | [diff] [blame] | 1309 | else |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1310 | ret = -EINVAL; |
Sean Young | b590c0b | 2016-12-05 19:24:59 -0200 | [diff] [blame] | 1311 | |
| 1312 | if (ret != 0) |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1313 | goto unlock; |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols && |
| 1317 | val) { |
James Hogan | 6bea25a | 2014-02-28 20:17:06 -0300 | [diff] [blame] | 1318 | /* refuse to set a filter unless a protocol is enabled */ |
| 1319 | ret = -EINVAL; |
| 1320 | goto unlock; |
| 1321 | } |
David Härdeman | 23c843b | 2014-04-04 19:06:01 -0300 | [diff] [blame] | 1322 | |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1323 | ret = set_filter(dev, &new_filter); |
David Härdeman | 99b0f3c | 2014-04-04 19:06:06 -0300 | [diff] [blame] | 1324 | if (ret < 0) |
| 1325 | goto unlock; |
James Hogan | 00942d1 | 2014-01-17 10:58:49 -0300 | [diff] [blame] | 1326 | |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1327 | *filter = new_filter; |
James Hogan | 00942d1 | 2014-01-17 10:58:49 -0300 | [diff] [blame] | 1328 | |
| 1329 | unlock: |
| 1330 | mutex_unlock(&dev->lock); |
David Härdeman | da6e162 | 2014-04-03 20:32:16 -0300 | [diff] [blame] | 1331 | return (ret < 0) ? ret : len; |
James Hogan | 00942d1 | 2014-01-17 10:58:49 -0300 | [diff] [blame] | 1332 | } |
| 1333 | |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1334 | /** |
| 1335 | * show_wakeup_protocols() - shows the wakeup IR protocol |
| 1336 | * @device: the device descriptor |
| 1337 | * @mattr: the device attribute struct |
| 1338 | * @buf: a pointer to the output buffer |
| 1339 | * |
| 1340 | * This routine is a callback routine for input read the IR protocol type(s). |
| 1341 | * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols. |
| 1342 | * It returns the protocol names of supported protocols. |
| 1343 | * The enabled protocols are printed in brackets. |
| 1344 | * |
David Härdeman | 18726a3 | 2017-04-27 17:34:08 -0300 | [diff] [blame] | 1345 | * dev->lock is taken to guard against races between |
| 1346 | * store_wakeup_protocols and show_wakeup_protocols. |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1347 | */ |
| 1348 | static ssize_t show_wakeup_protocols(struct device *device, |
| 1349 | struct device_attribute *mattr, |
| 1350 | char *buf) |
| 1351 | { |
| 1352 | struct rc_dev *dev = to_rc_dev(device); |
| 1353 | u64 allowed; |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 1354 | enum rc_proto enabled; |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1355 | char *tmp = buf; |
| 1356 | int i; |
| 1357 | |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1358 | mutex_lock(&dev->lock); |
| 1359 | |
| 1360 | allowed = dev->allowed_wakeup_protocols; |
| 1361 | enabled = dev->wakeup_protocol; |
| 1362 | |
| 1363 | mutex_unlock(&dev->lock); |
| 1364 | |
| 1365 | IR_dprintk(1, "%s: allowed - 0x%llx, enabled - %d\n", |
| 1366 | __func__, (long long)allowed, enabled); |
| 1367 | |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 1368 | for (i = 0; i < ARRAY_SIZE(protocols); i++) { |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1369 | if (allowed & (1ULL << i)) { |
| 1370 | if (i == enabled) |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 1371 | tmp += sprintf(tmp, "[%s] ", protocols[i].name); |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1372 | else |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 1373 | tmp += sprintf(tmp, "%s ", protocols[i].name); |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | if (tmp != buf) |
| 1378 | tmp--; |
| 1379 | *tmp = '\n'; |
| 1380 | |
| 1381 | return tmp + 1 - buf; |
| 1382 | } |
| 1383 | |
| 1384 | /** |
| 1385 | * store_wakeup_protocols() - changes the wakeup IR protocol(s) |
| 1386 | * @device: the device descriptor |
| 1387 | * @mattr: the device attribute struct |
| 1388 | * @buf: a pointer to the input buffer |
| 1389 | * @len: length of the input buffer |
| 1390 | * |
| 1391 | * This routine is for changing the IR protocol type. |
| 1392 | * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols. |
| 1393 | * Returns @len on success or a negative error code. |
| 1394 | * |
David Härdeman | 18726a3 | 2017-04-27 17:34:08 -0300 | [diff] [blame] | 1395 | * dev->lock is taken to guard against races between |
| 1396 | * store_wakeup_protocols and show_wakeup_protocols. |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1397 | */ |
| 1398 | static ssize_t store_wakeup_protocols(struct device *device, |
| 1399 | struct device_attribute *mattr, |
| 1400 | const char *buf, size_t len) |
| 1401 | { |
| 1402 | struct rc_dev *dev = to_rc_dev(device); |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 1403 | enum rc_proto protocol; |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1404 | ssize_t rc; |
| 1405 | u64 allowed; |
| 1406 | int i; |
| 1407 | |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1408 | mutex_lock(&dev->lock); |
| 1409 | |
| 1410 | allowed = dev->allowed_wakeup_protocols; |
| 1411 | |
| 1412 | if (sysfs_streq(buf, "none")) { |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 1413 | protocol = RC_PROTO_UNKNOWN; |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1414 | } else { |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 1415 | for (i = 0; i < ARRAY_SIZE(protocols); i++) { |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1416 | if ((allowed & (1ULL << i)) && |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 1417 | sysfs_streq(buf, protocols[i].name)) { |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1418 | protocol = i; |
| 1419 | break; |
| 1420 | } |
| 1421 | } |
| 1422 | |
Sean Young | d57ea87 | 2017-08-09 13:19:16 -0400 | [diff] [blame] | 1423 | if (i == ARRAY_SIZE(protocols)) { |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1424 | rc = -EINVAL; |
| 1425 | goto out; |
| 1426 | } |
James Hogan | f423ccc | 2015-03-31 14:48:10 -0300 | [diff] [blame] | 1427 | |
| 1428 | if (dev->encode_wakeup) { |
| 1429 | u64 mask = 1ULL << protocol; |
| 1430 | |
| 1431 | ir_raw_load_modules(&mask); |
| 1432 | if (!mask) { |
| 1433 | rc = -EINVAL; |
| 1434 | goto out; |
| 1435 | } |
| 1436 | } |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | if (dev->wakeup_protocol != protocol) { |
| 1440 | dev->wakeup_protocol = protocol; |
| 1441 | IR_dprintk(1, "Wakeup protocol changed to %d\n", protocol); |
| 1442 | |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 1443 | if (protocol == RC_PROTO_RC6_MCE) |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1444 | dev->scancode_wakeup_filter.data = 0x800f0000; |
| 1445 | else |
| 1446 | dev->scancode_wakeup_filter.data = 0; |
| 1447 | dev->scancode_wakeup_filter.mask = 0; |
| 1448 | |
| 1449 | rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter); |
| 1450 | if (rc == 0) |
| 1451 | rc = len; |
| 1452 | } else { |
| 1453 | rc = len; |
| 1454 | } |
| 1455 | |
| 1456 | out: |
| 1457 | mutex_unlock(&dev->lock); |
| 1458 | return rc; |
| 1459 | } |
| 1460 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1461 | static void rc_dev_release(struct device *device) |
| 1462 | { |
Max Kellermann | 47cae1e | 2016-03-21 08:33:05 -0300 | [diff] [blame] | 1463 | struct rc_dev *dev = to_rc_dev(device); |
| 1464 | |
| 1465 | kfree(dev); |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1466 | } |
| 1467 | |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1468 | #define ADD_HOTPLUG_VAR(fmt, val...) \ |
| 1469 | do { \ |
| 1470 | int err = add_uevent_var(env, fmt, val); \ |
| 1471 | if (err) \ |
| 1472 | return err; \ |
| 1473 | } while (0) |
| 1474 | |
| 1475 | static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env) |
| 1476 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1477 | struct rc_dev *dev = to_rc_dev(device); |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1478 | |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 1479 | if (dev->rc_map.name) |
| 1480 | ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name); |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1481 | if (dev->driver_name) |
| 1482 | ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name); |
Sean Young | b9f407e | 2017-09-01 11:34:23 -0300 | [diff] [blame^] | 1483 | if (dev->device_name) |
| 1484 | ADD_HOTPLUG_VAR("DEV_NAME=%s", dev->device_name); |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1485 | |
| 1486 | return 0; |
| 1487 | } |
| 1488 | |
| 1489 | /* |
| 1490 | * Static device attribute struct with the sysfs attributes for IR's |
| 1491 | */ |
Sean Young | 6d75db3 | 2017-09-01 11:30:50 -0300 | [diff] [blame] | 1492 | static struct device_attribute dev_attr_ro_protocols = |
| 1493 | __ATTR(protocols, 0444, show_protocols, NULL); |
| 1494 | static struct device_attribute dev_attr_rw_protocols = |
| 1495 | __ATTR(protocols, 0644, show_protocols, store_protocols); |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1496 | static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols, |
| 1497 | store_wakeup_protocols); |
James Hogan | 00942d1 | 2014-01-17 10:58:49 -0300 | [diff] [blame] | 1498 | static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR, |
| 1499 | show_filter, store_filter, RC_FILTER_NORMAL, false); |
| 1500 | static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR, |
| 1501 | show_filter, store_filter, RC_FILTER_NORMAL, true); |
| 1502 | static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR, |
| 1503 | show_filter, store_filter, RC_FILTER_WAKEUP, false); |
| 1504 | static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR, |
| 1505 | show_filter, store_filter, RC_FILTER_WAKEUP, true); |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1506 | |
Sean Young | 6d75db3 | 2017-09-01 11:30:50 -0300 | [diff] [blame] | 1507 | static struct attribute *rc_dev_rw_protocol_attrs[] = { |
| 1508 | &dev_attr_rw_protocols.attr, |
David Härdeman | 99b0f3c | 2014-04-04 19:06:06 -0300 | [diff] [blame] | 1509 | NULL, |
| 1510 | }; |
| 1511 | |
Sean Young | 6d75db3 | 2017-09-01 11:30:50 -0300 | [diff] [blame] | 1512 | static const struct attribute_group rc_dev_rw_protocol_attr_grp = { |
| 1513 | .attrs = rc_dev_rw_protocol_attrs, |
| 1514 | }; |
| 1515 | |
| 1516 | static struct attribute *rc_dev_ro_protocol_attrs[] = { |
| 1517 | &dev_attr_ro_protocols.attr, |
| 1518 | NULL, |
| 1519 | }; |
| 1520 | |
| 1521 | static const struct attribute_group rc_dev_ro_protocol_attr_grp = { |
| 1522 | .attrs = rc_dev_ro_protocol_attrs, |
David Härdeman | 99b0f3c | 2014-04-04 19:06:06 -0300 | [diff] [blame] | 1523 | }; |
| 1524 | |
David Härdeman | 99b0f3c | 2014-04-04 19:06:06 -0300 | [diff] [blame] | 1525 | static struct attribute *rc_dev_filter_attrs[] = { |
James Hogan | 00942d1 | 2014-01-17 10:58:49 -0300 | [diff] [blame] | 1526 | &dev_attr_filter.attr.attr, |
| 1527 | &dev_attr_filter_mask.attr.attr, |
David Härdeman | 99b0f3c | 2014-04-04 19:06:06 -0300 | [diff] [blame] | 1528 | NULL, |
| 1529 | }; |
| 1530 | |
Arvind Yadav | db68102 | 2017-07-07 04:23:54 -0400 | [diff] [blame] | 1531 | static const struct attribute_group rc_dev_filter_attr_grp = { |
David Härdeman | 99b0f3c | 2014-04-04 19:06:06 -0300 | [diff] [blame] | 1532 | .attrs = rc_dev_filter_attrs, |
| 1533 | }; |
| 1534 | |
| 1535 | static struct attribute *rc_dev_wakeup_filter_attrs[] = { |
James Hogan | 00942d1 | 2014-01-17 10:58:49 -0300 | [diff] [blame] | 1536 | &dev_attr_wakeup_filter.attr.attr, |
| 1537 | &dev_attr_wakeup_filter_mask.attr.attr, |
Sean Young | 0751d33 | 2016-12-05 17:08:35 -0200 | [diff] [blame] | 1538 | &dev_attr_wakeup_protocols.attr, |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1539 | NULL, |
| 1540 | }; |
| 1541 | |
Arvind Yadav | db68102 | 2017-07-07 04:23:54 -0400 | [diff] [blame] | 1542 | static const struct attribute_group rc_dev_wakeup_filter_attr_grp = { |
David Härdeman | 99b0f3c | 2014-04-04 19:06:06 -0300 | [diff] [blame] | 1543 | .attrs = rc_dev_wakeup_filter_attrs, |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1544 | }; |
| 1545 | |
Bhumika Goyal | f03f02f | 2017-08-19 05:22:15 -0300 | [diff] [blame] | 1546 | static const struct device_type rc_dev_type = { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1547 | .release = rc_dev_release, |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1548 | .uevent = rc_dev_uevent, |
| 1549 | }; |
| 1550 | |
Andi Shyti | 0f7499f | 2016-12-16 06:50:58 -0200 | [diff] [blame] | 1551 | struct rc_dev *rc_allocate_device(enum rc_driver_type type) |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1552 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1553 | struct rc_dev *dev; |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1554 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1555 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 1556 | if (!dev) |
| 1557 | return NULL; |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1558 | |
Andi Shyti | d34aee1 | 2016-12-16 04:12:15 -0200 | [diff] [blame] | 1559 | if (type != RC_DRIVER_IR_RAW_TX) { |
| 1560 | dev->input_dev = input_allocate_device(); |
| 1561 | if (!dev->input_dev) { |
| 1562 | kfree(dev); |
| 1563 | return NULL; |
| 1564 | } |
| 1565 | |
| 1566 | dev->input_dev->getkeycode = ir_getkeycode; |
| 1567 | dev->input_dev->setkeycode = ir_setkeycode; |
| 1568 | input_set_drvdata(dev->input_dev, dev); |
| 1569 | |
| 1570 | setup_timer(&dev->timer_keyup, ir_timer_keyup, |
| 1571 | (unsigned long)dev); |
| 1572 | |
| 1573 | spin_lock_init(&dev->rc_map.lock); |
| 1574 | spin_lock_init(&dev->keylock); |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1575 | } |
Jarod Wilson | 08aeb7c | 2011-05-11 15:14:31 -0300 | [diff] [blame] | 1576 | mutex_init(&dev->lock); |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1577 | |
| 1578 | dev->dev.type = &rc_dev_type; |
David Härdeman | 40fc532 | 2013-03-06 16:52:10 -0300 | [diff] [blame] | 1579 | dev->dev.class = &rc_class; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1580 | device_initialize(&dev->dev); |
| 1581 | |
Andi Shyti | 0f7499f | 2016-12-16 06:50:58 -0200 | [diff] [blame] | 1582 | dev->driver_type = type; |
| 1583 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1584 | __module_get(THIS_MODULE); |
| 1585 | return dev; |
| 1586 | } |
| 1587 | EXPORT_SYMBOL_GPL(rc_allocate_device); |
| 1588 | |
| 1589 | void rc_free_device(struct rc_dev *dev) |
| 1590 | { |
Mauro Carvalho Chehab | b05681b | 2011-07-29 02:23:20 -0300 | [diff] [blame] | 1591 | if (!dev) |
| 1592 | return; |
| 1593 | |
Markus Elfring | 3dd94f0 | 2014-11-20 09:01:32 -0300 | [diff] [blame] | 1594 | input_free_device(dev->input_dev); |
Mauro Carvalho Chehab | b05681b | 2011-07-29 02:23:20 -0300 | [diff] [blame] | 1595 | |
| 1596 | put_device(&dev->dev); |
| 1597 | |
Max Kellermann | 47cae1e | 2016-03-21 08:33:05 -0300 | [diff] [blame] | 1598 | /* kfree(dev) will be called by the callback function |
| 1599 | rc_dev_release() */ |
| 1600 | |
Mauro Carvalho Chehab | b05681b | 2011-07-29 02:23:20 -0300 | [diff] [blame] | 1601 | module_put(THIS_MODULE); |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1602 | } |
| 1603 | EXPORT_SYMBOL_GPL(rc_free_device); |
| 1604 | |
Heiner Kallweit | ddbf7d5 | 2016-09-30 17:42:07 -0300 | [diff] [blame] | 1605 | static void devm_rc_alloc_release(struct device *dev, void *res) |
| 1606 | { |
| 1607 | rc_free_device(*(struct rc_dev **)res); |
| 1608 | } |
| 1609 | |
Andi Shyti | 0f7499f | 2016-12-16 06:50:58 -0200 | [diff] [blame] | 1610 | struct rc_dev *devm_rc_allocate_device(struct device *dev, |
| 1611 | enum rc_driver_type type) |
Heiner Kallweit | ddbf7d5 | 2016-09-30 17:42:07 -0300 | [diff] [blame] | 1612 | { |
| 1613 | struct rc_dev **dr, *rc; |
| 1614 | |
| 1615 | dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL); |
| 1616 | if (!dr) |
| 1617 | return NULL; |
| 1618 | |
Andi Shyti | 0f7499f | 2016-12-16 06:50:58 -0200 | [diff] [blame] | 1619 | rc = rc_allocate_device(type); |
Heiner Kallweit | ddbf7d5 | 2016-09-30 17:42:07 -0300 | [diff] [blame] | 1620 | if (!rc) { |
| 1621 | devres_free(dr); |
| 1622 | return NULL; |
| 1623 | } |
| 1624 | |
| 1625 | rc->dev.parent = dev; |
| 1626 | rc->managed_alloc = true; |
| 1627 | *dr = rc; |
| 1628 | devres_add(dev, dr); |
| 1629 | |
| 1630 | return rc; |
| 1631 | } |
| 1632 | EXPORT_SYMBOL_GPL(devm_rc_allocate_device); |
| 1633 | |
David Härdeman | f56928a | 2017-05-03 07:04:00 -0300 | [diff] [blame] | 1634 | static int rc_prepare_rx_device(struct rc_dev *dev) |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1635 | { |
David Härdeman | fcb1309 | 2015-05-19 19:03:17 -0300 | [diff] [blame] | 1636 | int rc; |
Andi Shyti | 7ff2c2b | 2016-12-16 04:12:14 -0200 | [diff] [blame] | 1637 | struct rc_map *rc_map; |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 1638 | u64 rc_proto; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1639 | |
Andi Shyti | 7ff2c2b | 2016-12-16 04:12:14 -0200 | [diff] [blame] | 1640 | if (!dev->map_name) |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1641 | return -EINVAL; |
| 1642 | |
Mauro Carvalho Chehab | d100e65 | 2010-11-17 15:56:53 -0300 | [diff] [blame] | 1643 | rc_map = rc_map_get(dev->map_name); |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 1644 | if (!rc_map) |
Mauro Carvalho Chehab | d100e65 | 2010-11-17 15:56:53 -0300 | [diff] [blame] | 1645 | rc_map = rc_map_get(RC_MAP_EMPTY); |
Mauro Carvalho Chehab | b088ba6 | 2010-11-17 14:28:27 -0300 | [diff] [blame] | 1646 | if (!rc_map || !rc_map->scan || rc_map->size == 0) |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1647 | return -EINVAL; |
| 1648 | |
Andi Shyti | 7ff2c2b | 2016-12-16 04:12:14 -0200 | [diff] [blame] | 1649 | rc = ir_setkeytable(dev, rc_map); |
| 1650 | if (rc) |
| 1651 | return rc; |
| 1652 | |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 1653 | rc_proto = BIT_ULL(rc_map->rc_proto); |
Andi Shyti | 7ff2c2b | 2016-12-16 04:12:14 -0200 | [diff] [blame] | 1654 | |
Sean Young | 831c4c8 | 2017-09-01 09:55:59 -0300 | [diff] [blame] | 1655 | if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol) |
| 1656 | dev->enabled_protocols = dev->allowed_protocols; |
| 1657 | |
Sean Young | 4138086 | 2017-02-22 18:48:01 -0300 | [diff] [blame] | 1658 | if (dev->change_protocol) { |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 1659 | rc = dev->change_protocol(dev, &rc_proto); |
Andi Shyti | 7ff2c2b | 2016-12-16 04:12:14 -0200 | [diff] [blame] | 1660 | if (rc < 0) |
| 1661 | goto out_table; |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 1662 | dev->enabled_protocols = rc_proto; |
Andi Shyti | 7ff2c2b | 2016-12-16 04:12:14 -0200 | [diff] [blame] | 1663 | } |
| 1664 | |
Sean Young | 4138086 | 2017-02-22 18:48:01 -0300 | [diff] [blame] | 1665 | if (dev->driver_type == RC_DRIVER_IR_RAW) |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 1666 | ir_raw_load_modules(&rc_proto); |
Sean Young | 4138086 | 2017-02-22 18:48:01 -0300 | [diff] [blame] | 1667 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1668 | set_bit(EV_KEY, dev->input_dev->evbit); |
| 1669 | set_bit(EV_REP, dev->input_dev->evbit); |
| 1670 | set_bit(EV_MSC, dev->input_dev->evbit); |
| 1671 | set_bit(MSC_SCAN, dev->input_dev->mscbit); |
| 1672 | if (dev->open) |
| 1673 | dev->input_dev->open = ir_open; |
| 1674 | if (dev->close) |
| 1675 | dev->input_dev->close = ir_close; |
| 1676 | |
David Härdeman | b2aceb7 | 2017-04-27 17:33:58 -0300 | [diff] [blame] | 1677 | dev->input_dev->dev.parent = &dev->dev; |
| 1678 | memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id)); |
| 1679 | dev->input_dev->phys = dev->input_phys; |
Sean Young | 518f4b2 | 2017-07-01 12:13:19 -0400 | [diff] [blame] | 1680 | dev->input_dev->name = dev->device_name; |
David Härdeman | b2aceb7 | 2017-04-27 17:33:58 -0300 | [diff] [blame] | 1681 | |
David Härdeman | f56928a | 2017-05-03 07:04:00 -0300 | [diff] [blame] | 1682 | return 0; |
| 1683 | |
| 1684 | out_table: |
| 1685 | ir_free_table(&dev->rc_map); |
| 1686 | |
| 1687 | return rc; |
| 1688 | } |
| 1689 | |
| 1690 | static int rc_setup_rx_device(struct rc_dev *dev) |
| 1691 | { |
| 1692 | int rc; |
| 1693 | |
David Härdeman | b2aceb7 | 2017-04-27 17:33:58 -0300 | [diff] [blame] | 1694 | /* rc_open will be called here */ |
| 1695 | rc = input_register_device(dev->input_dev); |
| 1696 | if (rc) |
David Härdeman | f56928a | 2017-05-03 07:04:00 -0300 | [diff] [blame] | 1697 | return rc; |
David Härdeman | b2aceb7 | 2017-04-27 17:33:58 -0300 | [diff] [blame] | 1698 | |
Andi Shyti | 7ff2c2b | 2016-12-16 04:12:14 -0200 | [diff] [blame] | 1699 | /* |
| 1700 | * Default delay of 250ms is too short for some protocols, especially |
| 1701 | * since the timeout is currently set to 250ms. Increase it to 500ms, |
| 1702 | * to avoid wrong repetition of the keycodes. Note that this must be |
| 1703 | * set after the call to input_register_device(). |
| 1704 | */ |
| 1705 | dev->input_dev->rep[REP_DELAY] = 500; |
| 1706 | |
| 1707 | /* |
| 1708 | * As a repeat event on protocols like RC-5 and NEC take as long as |
| 1709 | * 110/114ms, using 33ms as a repeat period is not the right thing |
| 1710 | * to do. |
| 1711 | */ |
| 1712 | dev->input_dev->rep[REP_PERIOD] = 125; |
| 1713 | |
Andi Shyti | 7ff2c2b | 2016-12-16 04:12:14 -0200 | [diff] [blame] | 1714 | return 0; |
Andi Shyti | 7ff2c2b | 2016-12-16 04:12:14 -0200 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | static void rc_free_rx_device(struct rc_dev *dev) |
| 1718 | { |
David Härdeman | f56928a | 2017-05-03 07:04:00 -0300 | [diff] [blame] | 1719 | if (!dev) |
Andi Shyti | 7ff2c2b | 2016-12-16 04:12:14 -0200 | [diff] [blame] | 1720 | return; |
| 1721 | |
David Härdeman | f56928a | 2017-05-03 07:04:00 -0300 | [diff] [blame] | 1722 | if (dev->input_dev) { |
| 1723 | input_unregister_device(dev->input_dev); |
| 1724 | dev->input_dev = NULL; |
| 1725 | } |
Andi Shyti | 7ff2c2b | 2016-12-16 04:12:14 -0200 | [diff] [blame] | 1726 | |
David Härdeman | f56928a | 2017-05-03 07:04:00 -0300 | [diff] [blame] | 1727 | ir_free_table(&dev->rc_map); |
Andi Shyti | 7ff2c2b | 2016-12-16 04:12:14 -0200 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | int rc_register_device(struct rc_dev *dev) |
| 1731 | { |
Andi Shyti | 7ff2c2b | 2016-12-16 04:12:14 -0200 | [diff] [blame] | 1732 | const char *path; |
| 1733 | int attr = 0; |
| 1734 | int minor; |
| 1735 | int rc; |
| 1736 | |
| 1737 | if (!dev) |
| 1738 | return -EINVAL; |
| 1739 | |
David Härdeman | fcb1309 | 2015-05-19 19:03:17 -0300 | [diff] [blame] | 1740 | minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL); |
| 1741 | if (minor < 0) |
| 1742 | return minor; |
| 1743 | |
| 1744 | dev->minor = minor; |
| 1745 | dev_set_name(&dev->dev, "rc%u", dev->minor); |
| 1746 | dev_set_drvdata(&dev->dev, dev); |
Mauro Carvalho Chehab | 587d1b0 | 2014-01-14 16:27:55 -0300 | [diff] [blame] | 1747 | |
David Härdeman | 99b0f3c | 2014-04-04 19:06:06 -0300 | [diff] [blame] | 1748 | dev->dev.groups = dev->sysfs_groups; |
Sean Young | 6d75db3 | 2017-09-01 11:30:50 -0300 | [diff] [blame] | 1749 | if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol) |
| 1750 | dev->sysfs_groups[attr++] = &rc_dev_ro_protocol_attr_grp; |
| 1751 | else if (dev->driver_type != RC_DRIVER_IR_RAW_TX) |
| 1752 | dev->sysfs_groups[attr++] = &rc_dev_rw_protocol_attr_grp; |
David Härdeman | 99b0f3c | 2014-04-04 19:06:06 -0300 | [diff] [blame] | 1753 | if (dev->s_filter) |
David Härdeman | 120703f | 2014-04-03 20:31:30 -0300 | [diff] [blame] | 1754 | dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp; |
David Härdeman | 99b0f3c | 2014-04-04 19:06:06 -0300 | [diff] [blame] | 1755 | if (dev->s_wakeup_filter) |
| 1756 | dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp; |
David Härdeman | 99b0f3c | 2014-04-04 19:06:06 -0300 | [diff] [blame] | 1757 | dev->sysfs_groups[attr++] = NULL; |
| 1758 | |
David Härdeman | f56928a | 2017-05-03 07:04:00 -0300 | [diff] [blame] | 1759 | if (dev->driver_type == RC_DRIVER_IR_RAW || |
| 1760 | dev->driver_type == RC_DRIVER_IR_RAW_TX) { |
| 1761 | rc = ir_raw_event_prepare(dev); |
| 1762 | if (rc < 0) |
| 1763 | goto out_minor; |
| 1764 | } |
| 1765 | |
| 1766 | if (dev->driver_type != RC_DRIVER_IR_RAW_TX) { |
| 1767 | rc = rc_prepare_rx_device(dev); |
| 1768 | if (rc) |
| 1769 | goto out_raw; |
| 1770 | } |
| 1771 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1772 | rc = device_add(&dev->dev); |
| 1773 | if (rc) |
David Härdeman | f56928a | 2017-05-03 07:04:00 -0300 | [diff] [blame] | 1774 | goto out_rx_free; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1775 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1776 | path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL); |
Heiner Kallweit | 4dc0e90 | 2015-10-29 19:39:06 -0200 | [diff] [blame] | 1777 | dev_info(&dev->dev, "%s as %s\n", |
Sean Young | 518f4b2 | 2017-07-01 12:13:19 -0400 | [diff] [blame] | 1778 | dev->device_name ?: "Unspecified device", path ?: "N/A"); |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1779 | kfree(path); |
| 1780 | |
Sean Young | 5df6277 | 2017-02-23 06:11:21 -0300 | [diff] [blame] | 1781 | if (dev->driver_type != RC_DRIVER_IR_RAW_TX) { |
| 1782 | rc = rc_setup_rx_device(dev); |
| 1783 | if (rc) |
David Härdeman | f56928a | 2017-05-03 07:04:00 -0300 | [diff] [blame] | 1784 | goto out_dev; |
| 1785 | } |
| 1786 | |
| 1787 | if (dev->driver_type == RC_DRIVER_IR_RAW || |
| 1788 | dev->driver_type == RC_DRIVER_IR_RAW_TX) { |
| 1789 | rc = ir_raw_event_register(dev); |
| 1790 | if (rc < 0) |
| 1791 | goto out_rx; |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1792 | } |
| 1793 | |
Andi Shyti | 7ff2c2b | 2016-12-16 04:12:14 -0200 | [diff] [blame] | 1794 | IR_dprintk(1, "Registered rc%u (driver: %s)\n", |
David Härdeman | fcb1309 | 2015-05-19 19:03:17 -0300 | [diff] [blame] | 1795 | dev->minor, |
Andi Shyti | 7ff2c2b | 2016-12-16 04:12:14 -0200 | [diff] [blame] | 1796 | dev->driver_name ? dev->driver_name : "unknown"); |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1797 | |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1798 | return 0; |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1799 | |
David Härdeman | f56928a | 2017-05-03 07:04:00 -0300 | [diff] [blame] | 1800 | out_rx: |
| 1801 | rc_free_rx_device(dev); |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1802 | out_dev: |
| 1803 | device_del(&dev->dev); |
David Härdeman | f56928a | 2017-05-03 07:04:00 -0300 | [diff] [blame] | 1804 | out_rx_free: |
| 1805 | ir_free_table(&dev->rc_map); |
| 1806 | out_raw: |
| 1807 | ir_raw_event_free(dev); |
| 1808 | out_minor: |
David Härdeman | fcb1309 | 2015-05-19 19:03:17 -0300 | [diff] [blame] | 1809 | ida_simple_remove(&rc_ida, minor); |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1810 | return rc; |
| 1811 | } |
| 1812 | EXPORT_SYMBOL_GPL(rc_register_device); |
| 1813 | |
Heiner Kallweit | ddbf7d5 | 2016-09-30 17:42:07 -0300 | [diff] [blame] | 1814 | static void devm_rc_release(struct device *dev, void *res) |
| 1815 | { |
| 1816 | rc_unregister_device(*(struct rc_dev **)res); |
| 1817 | } |
| 1818 | |
| 1819 | int devm_rc_register_device(struct device *parent, struct rc_dev *dev) |
| 1820 | { |
| 1821 | struct rc_dev **dr; |
| 1822 | int ret; |
| 1823 | |
| 1824 | dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL); |
| 1825 | if (!dr) |
| 1826 | return -ENOMEM; |
| 1827 | |
| 1828 | ret = rc_register_device(dev); |
| 1829 | if (ret) { |
| 1830 | devres_free(dr); |
| 1831 | return ret; |
| 1832 | } |
| 1833 | |
| 1834 | *dr = dev; |
| 1835 | devres_add(parent, dr); |
| 1836 | |
| 1837 | return 0; |
| 1838 | } |
| 1839 | EXPORT_SYMBOL_GPL(devm_rc_register_device); |
| 1840 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1841 | void rc_unregister_device(struct rc_dev *dev) |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1842 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1843 | if (!dev) |
| 1844 | return; |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1845 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1846 | del_timer_sync(&dev->timer_keyup); |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1847 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1848 | if (dev->driver_type == RC_DRIVER_IR_RAW) |
| 1849 | ir_raw_event_unregister(dev); |
| 1850 | |
Andi Shyti | 7ff2c2b | 2016-12-16 04:12:14 -0200 | [diff] [blame] | 1851 | rc_free_rx_device(dev); |
Mauro Carvalho Chehab | b05681b | 2011-07-29 02:23:20 -0300 | [diff] [blame] | 1852 | |
| 1853 | device_del(&dev->dev); |
| 1854 | |
David Härdeman | fcb1309 | 2015-05-19 19:03:17 -0300 | [diff] [blame] | 1855 | ida_simple_remove(&rc_ida, dev->minor); |
| 1856 | |
Heiner Kallweit | ddbf7d5 | 2016-09-30 17:42:07 -0300 | [diff] [blame] | 1857 | if (!dev->managed_alloc) |
| 1858 | rc_free_device(dev); |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1859 | } |
Mauro Carvalho Chehab | b05681b | 2011-07-29 02:23:20 -0300 | [diff] [blame] | 1860 | |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 1861 | EXPORT_SYMBOL_GPL(rc_unregister_device); |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1862 | |
| 1863 | /* |
| 1864 | * Init/exit code for the module. Basically, creates/removes /sys/class/rc |
| 1865 | */ |
| 1866 | |
Mauro Carvalho Chehab | 6bda964 | 2010-11-17 13:28:38 -0300 | [diff] [blame] | 1867 | static int __init rc_core_init(void) |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1868 | { |
David Härdeman | 40fc532 | 2013-03-06 16:52:10 -0300 | [diff] [blame] | 1869 | int rc = class_register(&rc_class); |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1870 | if (rc) { |
Mauro Carvalho Chehab | d3d9682 | 2016-10-20 15:04:39 -0200 | [diff] [blame] | 1871 | pr_err("rc_core: unable to register rc class\n"); |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1872 | return rc; |
| 1873 | } |
| 1874 | |
Sean Young | 153a60b | 2013-07-30 19:00:01 -0300 | [diff] [blame] | 1875 | led_trigger_register_simple("rc-feedback", &led_feedback); |
Mauro Carvalho Chehab | d100e65 | 2010-11-17 15:56:53 -0300 | [diff] [blame] | 1876 | rc_map_register(&empty_map); |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1877 | |
| 1878 | return 0; |
| 1879 | } |
| 1880 | |
Mauro Carvalho Chehab | 6bda964 | 2010-11-17 13:28:38 -0300 | [diff] [blame] | 1881 | static void __exit rc_core_exit(void) |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1882 | { |
David Härdeman | 40fc532 | 2013-03-06 16:52:10 -0300 | [diff] [blame] | 1883 | class_unregister(&rc_class); |
Sean Young | 153a60b | 2013-07-30 19:00:01 -0300 | [diff] [blame] | 1884 | led_trigger_unregister_simple(led_feedback); |
Mauro Carvalho Chehab | d100e65 | 2010-11-17 15:56:53 -0300 | [diff] [blame] | 1885 | rc_map_unregister(&empty_map); |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1886 | } |
| 1887 | |
David Härdeman | e76d4ce | 2013-03-06 16:52:15 -0300 | [diff] [blame] | 1888 | subsys_initcall(rc_core_init); |
Mauro Carvalho Chehab | 6bda964 | 2010-11-17 13:28:38 -0300 | [diff] [blame] | 1889 | module_exit(rc_core_exit); |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1890 | |
Mauro Carvalho Chehab | 6bda964 | 2010-11-17 13:28:38 -0300 | [diff] [blame] | 1891 | int rc_core_debug; /* ir_debug level (0,1,2) */ |
| 1892 | EXPORT_SYMBOL_GPL(rc_core_debug); |
| 1893 | module_param_named(debug, rc_core_debug, int, 0644); |
Mauro Carvalho Chehab | 446e4a6 | 2009-12-11 08:34:07 -0300 | [diff] [blame] | 1894 | |
Mauro Carvalho Chehab | 37e59f8 | 2014-02-07 08:03:07 -0200 | [diff] [blame] | 1895 | MODULE_AUTHOR("Mauro Carvalho Chehab"); |
Mauro Carvalho Chehab | 446e4a6 | 2009-12-11 08:34:07 -0300 | [diff] [blame] | 1896 | MODULE_LICENSE("GPL"); |