Mauro Carvalho Chehab | 2083528 | 2017-12-01 08:47:08 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // ir-nec-decoder.c - handle NEC IR Pulse/Space protocol |
| 3 | // |
| 4 | // Copyright (C) 2010 by Mauro Carvalho Chehab |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 5 | |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 6 | #include <linux/bitrev.h> |
Paul Gortmaker | 7a707b8 | 2011-07-03 14:03:12 -0400 | [diff] [blame] | 7 | #include <linux/module.h> |
Mauro Carvalho Chehab | f62de67 | 2010-11-09 23:09:57 -0300 | [diff] [blame] | 8 | #include "rc-core-priv.h" |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 9 | |
Mauro Carvalho Chehab | 67780d6 | 2010-04-03 20:33:00 -0300 | [diff] [blame] | 10 | #define NEC_NBITS 32 |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 11 | #define NEC_UNIT 562500 /* ns */ |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 12 | #define NEC_HEADER_PULSE (16 * NEC_UNIT) |
| 13 | #define NECX_HEADER_PULSE (8 * NEC_UNIT) /* Less common NEC variant */ |
| 14 | #define NEC_HEADER_SPACE (8 * NEC_UNIT) |
Maxim Levitsky | e31f412 | 2010-07-31 11:59:19 -0300 | [diff] [blame] | 15 | #define NEC_REPEAT_SPACE (4 * NEC_UNIT) |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 16 | #define NEC_BIT_PULSE (1 * NEC_UNIT) |
| 17 | #define NEC_BIT_0_SPACE (1 * NEC_UNIT) |
| 18 | #define NEC_BIT_1_SPACE (3 * NEC_UNIT) |
| 19 | #define NEC_TRAILER_PULSE (1 * NEC_UNIT) |
| 20 | #define NEC_TRAILER_SPACE (10 * NEC_UNIT) /* even longer in reality */ |
Maxim Levitsky | 86ff071 | 2010-07-31 11:59:20 -0300 | [diff] [blame] | 21 | #define NECX_REPEAT_BITS 1 |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 22 | |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 23 | enum nec_state { |
| 24 | STATE_INACTIVE, |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 25 | STATE_HEADER_SPACE, |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 26 | STATE_BIT_PULSE, |
| 27 | STATE_BIT_SPACE, |
| 28 | STATE_TRAILER_PULSE, |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 29 | STATE_TRAILER_SPACE, |
| 30 | }; |
| 31 | |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 32 | /** |
Mauro Carvalho Chehab | 587835a | 2010-04-04 10:44:51 -0300 | [diff] [blame] | 33 | * ir_nec_decode() - Decode one NEC pulse or space |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 34 | * @dev: the struct rc_dev descriptor of the device |
Mauro Carvalho Chehab | 64dc682 | 2017-11-27 10:23:07 -0500 | [diff] [blame] | 35 | * @ev: the struct ir_raw_event descriptor of the pulse/space |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 36 | * |
| 37 | * This function returns -EINVAL if the pulse violates the state machine |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 38 | */ |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 39 | static int ir_nec_decode(struct rc_dev *dev, struct ir_raw_event ev) |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 40 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 41 | struct nec_dec *data = &dev->raw->nec; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 42 | u32 scancode; |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 43 | enum rc_proto rc_proto; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 44 | u8 address, not_address, command, not_command; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 45 | |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 46 | if (!is_timing_event(ev)) { |
| 47 | if (ev.reset) |
| 48 | data->state = STATE_INACTIVE; |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 49 | return 0; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 50 | } |
| 51 | |
Sean Young | 50078a9 | 2018-02-12 07:20:52 -0500 | [diff] [blame] | 52 | dev_dbg(&dev->dev, "NEC decode started at state %d (%uus %s)\n", |
| 53 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 54 | |
| 55 | switch (data->state) { |
| 56 | |
| 57 | case STATE_INACTIVE: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 58 | if (!ev.pulse) |
| 59 | break; |
| 60 | |
Sean Young | 743135e | 2012-07-31 06:37:29 -0300 | [diff] [blame] | 61 | if (eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT * 2)) { |
Maxim Levitsky | 86ff071 | 2010-07-31 11:59:20 -0300 | [diff] [blame] | 62 | data->is_nec_x = false; |
| 63 | data->necx_repeat = false; |
| 64 | } else if (eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2)) |
| 65 | data->is_nec_x = true; |
| 66 | else |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 67 | break; |
| 68 | |
| 69 | data->count = 0; |
| 70 | data->state = STATE_HEADER_SPACE; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 71 | return 0; |
| 72 | |
| 73 | case STATE_HEADER_SPACE: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 74 | if (ev.pulse) |
| 75 | break; |
| 76 | |
Sean Young | 743135e | 2012-07-31 06:37:29 -0300 | [diff] [blame] | 77 | if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT)) { |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 78 | data->state = STATE_BIT_PULSE; |
| 79 | return 0; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 80 | } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) { |
David Härdeman | 265a298 | 2017-06-22 15:23:54 -0400 | [diff] [blame] | 81 | data->state = STATE_TRAILER_PULSE; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 82 | return 0; |
| 83 | } |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 84 | |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 85 | break; |
| 86 | |
| 87 | case STATE_BIT_PULSE: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 88 | if (!ev.pulse) |
| 89 | break; |
| 90 | |
| 91 | if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2)) |
| 92 | break; |
| 93 | |
| 94 | data->state = STATE_BIT_SPACE; |
| 95 | return 0; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 96 | |
| 97 | case STATE_BIT_SPACE: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 98 | if (ev.pulse) |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 99 | break; |
| 100 | |
Maxim Levitsky | 86ff071 | 2010-07-31 11:59:20 -0300 | [diff] [blame] | 101 | if (data->necx_repeat && data->count == NECX_REPEAT_BITS && |
Sean Young | 50078a9 | 2018-02-12 07:20:52 -0500 | [diff] [blame] | 102 | geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2)) { |
| 103 | dev_dbg(&dev->dev, "Repeat last key\n"); |
| 104 | rc_repeat(dev); |
| 105 | data->state = STATE_INACTIVE; |
| 106 | return 0; |
Maxim Levitsky | 86ff071 | 2010-07-31 11:59:20 -0300 | [diff] [blame] | 107 | } else if (data->count > NECX_REPEAT_BITS) |
| 108 | data->necx_repeat = false; |
| 109 | |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 110 | data->bits <<= 1; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 111 | if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2)) |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 112 | data->bits |= 1; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 113 | else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2)) |
| 114 | break; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 115 | data->count++; |
| 116 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 117 | if (data->count == NEC_NBITS) |
| 118 | data->state = STATE_TRAILER_PULSE; |
| 119 | else |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 120 | data->state = STATE_BIT_PULSE; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 121 | |
| 122 | return 0; |
| 123 | |
| 124 | case STATE_TRAILER_PULSE: |
| 125 | if (!ev.pulse) |
| 126 | break; |
| 127 | |
| 128 | if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2)) |
| 129 | break; |
| 130 | |
| 131 | data->state = STATE_TRAILER_SPACE; |
| 132 | return 0; |
| 133 | |
| 134 | case STATE_TRAILER_SPACE: |
| 135 | if (ev.pulse) |
| 136 | break; |
| 137 | |
| 138 | if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2)) |
| 139 | break; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 140 | |
Sean Young | 829bbf2 | 2017-10-01 16:38:29 -0400 | [diff] [blame] | 141 | if (data->count == NEC_NBITS) { |
| 142 | address = bitrev8((data->bits >> 24) & 0xff); |
| 143 | not_address = bitrev8((data->bits >> 16) & 0xff); |
| 144 | command = bitrev8((data->bits >> 8) & 0xff); |
| 145 | not_command = bitrev8((data->bits >> 0) & 0xff); |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 146 | |
Sean Young | 829bbf2 | 2017-10-01 16:38:29 -0400 | [diff] [blame] | 147 | scancode = ir_nec_bytes_to_scancode(address, |
| 148 | not_address, |
| 149 | command, |
| 150 | not_command, |
| 151 | &rc_proto); |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 152 | |
Sean Young | 829bbf2 | 2017-10-01 16:38:29 -0400 | [diff] [blame] | 153 | if (data->is_nec_x) |
| 154 | data->necx_repeat = true; |
Maxim Levitsky | 86ff071 | 2010-07-31 11:59:20 -0300 | [diff] [blame] | 155 | |
Sean Young | 829bbf2 | 2017-10-01 16:38:29 -0400 | [diff] [blame] | 156 | rc_keydown(dev, rc_proto, scancode, 0); |
| 157 | } else { |
| 158 | rc_repeat(dev); |
| 159 | } |
| 160 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 161 | data->state = STATE_INACTIVE; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 162 | return 0; |
David Härdeman | 724e249 | 2010-04-08 13:10:00 -0300 | [diff] [blame] | 163 | } |
| 164 | |
Sean Young | 50078a9 | 2018-02-12 07:20:52 -0500 | [diff] [blame] | 165 | dev_dbg(&dev->dev, "NEC decode failed at count %d state %d (%uus %s)\n", |
| 166 | data->count, data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
Mauro Carvalho Chehab | 2f16f63 | 2010-04-03 18:51:50 -0300 | [diff] [blame] | 167 | data->state = STATE_INACTIVE; |
Mauro Carvalho Chehab | a3572c3 | 2010-03-20 20:59:44 -0300 | [diff] [blame] | 168 | return -EINVAL; |
| 169 | } |
Mauro Carvalho Chehab | ada3963 | 2010-03-21 12:24:24 -0300 | [diff] [blame] | 170 | |
James Hogan | 141cfb1 | 2014-03-14 20:04:14 -0300 | [diff] [blame] | 171 | /** |
| 172 | * ir_nec_scancode_to_raw() - encode an NEC scancode ready for modulation. |
| 173 | * @protocol: specific protocol to use |
| 174 | * @scancode: a single NEC scancode. |
James Hogan | 141cfb1 | 2014-03-14 20:04:14 -0300 | [diff] [blame] | 175 | */ |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 176 | static u32 ir_nec_scancode_to_raw(enum rc_proto protocol, u32 scancode) |
James Hogan | 141cfb1 | 2014-03-14 20:04:14 -0300 | [diff] [blame] | 177 | { |
| 178 | unsigned int addr, addr_inv, data, data_inv; |
| 179 | |
| 180 | data = scancode & 0xff; |
| 181 | |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 182 | if (protocol == RC_PROTO_NEC32) { |
James Hogan | 141cfb1 | 2014-03-14 20:04:14 -0300 | [diff] [blame] | 183 | /* 32-bit NEC (used by Apple and TiVo remotes) */ |
| 184 | /* scan encoding: aaAAddDD */ |
| 185 | addr_inv = (scancode >> 24) & 0xff; |
| 186 | addr = (scancode >> 16) & 0xff; |
| 187 | data_inv = (scancode >> 8) & 0xff; |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 188 | } else if (protocol == RC_PROTO_NECX) { |
James Hogan | 141cfb1 | 2014-03-14 20:04:14 -0300 | [diff] [blame] | 189 | /* Extended NEC */ |
| 190 | /* scan encoding AAaaDD */ |
| 191 | addr = (scancode >> 16) & 0xff; |
| 192 | addr_inv = (scancode >> 8) & 0xff; |
| 193 | data_inv = data ^ 0xff; |
| 194 | } else { |
| 195 | /* Normal NEC */ |
| 196 | /* scan encoding: AADD */ |
| 197 | addr = (scancode >> 8) & 0xff; |
| 198 | addr_inv = addr ^ 0xff; |
| 199 | data_inv = data ^ 0xff; |
| 200 | } |
| 201 | |
| 202 | /* raw encoding: ddDDaaAA */ |
| 203 | return data_inv << 24 | |
| 204 | data << 16 | |
| 205 | addr_inv << 8 | |
| 206 | addr; |
| 207 | } |
| 208 | |
| 209 | static const struct ir_raw_timings_pd ir_nec_timings = { |
| 210 | .header_pulse = NEC_HEADER_PULSE, |
| 211 | .header_space = NEC_HEADER_SPACE, |
| 212 | .bit_pulse = NEC_BIT_PULSE, |
| 213 | .bit_space[0] = NEC_BIT_0_SPACE, |
| 214 | .bit_space[1] = NEC_BIT_1_SPACE, |
| 215 | .trailer_pulse = NEC_TRAILER_PULSE, |
| 216 | .trailer_space = NEC_TRAILER_SPACE, |
| 217 | .msb_first = 0, |
| 218 | }; |
| 219 | |
| 220 | /** |
| 221 | * ir_nec_encode() - Encode a scancode as a stream of raw events |
| 222 | * |
| 223 | * @protocol: protocol to encode |
| 224 | * @scancode: scancode to encode |
| 225 | * @events: array of raw ir events to write into |
| 226 | * @max: maximum size of @events |
| 227 | * |
| 228 | * Returns: The number of events written. |
| 229 | * -ENOBUFS if there isn't enough space in the array to fit the |
| 230 | * encoding. In this case all @max events will have been written. |
| 231 | */ |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 232 | static int ir_nec_encode(enum rc_proto protocol, u32 scancode, |
James Hogan | 141cfb1 | 2014-03-14 20:04:14 -0300 | [diff] [blame] | 233 | struct ir_raw_event *events, unsigned int max) |
| 234 | { |
| 235 | struct ir_raw_event *e = events; |
| 236 | int ret; |
| 237 | u32 raw; |
| 238 | |
| 239 | /* Convert a NEC scancode to raw NEC data */ |
| 240 | raw = ir_nec_scancode_to_raw(protocol, scancode); |
| 241 | |
| 242 | /* Modulate the raw data using a pulse distance modulation */ |
| 243 | ret = ir_raw_gen_pd(&e, max, &ir_nec_timings, NEC_NBITS, raw); |
| 244 | if (ret < 0) |
| 245 | return ret; |
| 246 | |
| 247 | return e - events; |
| 248 | } |
| 249 | |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 250 | static struct ir_raw_handler nec_handler = { |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 251 | .protocols = RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX | |
| 252 | RC_PROTO_BIT_NEC32, |
Mauro Carvalho Chehab | 20d5f11 | 2010-03-25 23:49:46 -0300 | [diff] [blame] | 253 | .decode = ir_nec_decode, |
James Hogan | 141cfb1 | 2014-03-14 20:04:14 -0300 | [diff] [blame] | 254 | .encode = ir_nec_encode, |
Sean Young | cdfaa01 | 2017-02-25 06:51:30 -0500 | [diff] [blame] | 255 | .carrier = 38000, |
Sean Young | a86d6df | 2018-03-23 16:47:37 -0400 | [diff] [blame] | 256 | .min_timeout = NEC_TRAILER_SPACE, |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 257 | }; |
| 258 | |
| 259 | static int __init ir_nec_decode_init(void) |
| 260 | { |
| 261 | ir_raw_handler_register(&nec_handler); |
| 262 | |
| 263 | printk(KERN_INFO "IR NEC protocol handler initialized\n"); |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static void __exit ir_nec_decode_exit(void) |
| 268 | { |
| 269 | ir_raw_handler_unregister(&nec_handler); |
| 270 | } |
| 271 | |
| 272 | module_init(ir_nec_decode_init); |
| 273 | module_exit(ir_nec_decode_exit); |
| 274 | |
Mauro Carvalho Chehab | 2083528 | 2017-12-01 08:47:08 -0500 | [diff] [blame] | 275 | MODULE_LICENSE("GPL v2"); |
Mauro Carvalho Chehab | 37e59f8 | 2014-02-07 08:03:07 -0200 | [diff] [blame] | 276 | MODULE_AUTHOR("Mauro Carvalho Chehab"); |
Mauro Carvalho Chehab | 995187b | 2010-03-24 20:47:53 -0300 | [diff] [blame] | 277 | MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)"); |
| 278 | MODULE_DESCRIPTION("NEC IR protocol decoder"); |