rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* ir-rc6-decoder.c - A decoder for the RC6 IR protocol |
| 2 | * |
| 3 | * Copyright (C) 2010 by David Härdeman <david@hardeman.nu> |
| 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. |
| 13 | */ |
| 14 | |
| 15 | #include "rc-core-priv.h" |
| 16 | #include <linux/module.h> |
| 17 | |
| 18 | /* |
| 19 | * This decoder currently supports: |
| 20 | * RC6-0-16 (standard toggle bit in header) |
| 21 | * RC6-6A-20 (no toggle bit) |
| 22 | * RC6-6A-24 (no toggle bit) |
| 23 | * RC6-6A-32 (MCE version with toggle bit in body) |
| 24 | */ |
| 25 | |
| 26 | #define RC6_UNIT 444444 /* nanosecs */ |
| 27 | #define RC6_HEADER_NBITS 4 /* not including toggle bit */ |
| 28 | #define RC6_0_NBITS 16 |
| 29 | #define RC6_6A_32_NBITS 32 |
| 30 | #define RC6_6A_NBITS 128 /* Variable 8..128 */ |
| 31 | #define RC6_PREFIX_PULSE (6 * RC6_UNIT) |
| 32 | #define RC6_PREFIX_SPACE (2 * RC6_UNIT) |
| 33 | #define RC6_BIT_START (1 * RC6_UNIT) |
| 34 | #define RC6_BIT_END (1 * RC6_UNIT) |
| 35 | #define RC6_TOGGLE_START (2 * RC6_UNIT) |
| 36 | #define RC6_TOGGLE_END (2 * RC6_UNIT) |
| 37 | #define RC6_SUFFIX_SPACE (6 * RC6_UNIT) |
| 38 | #define RC6_MODE_MASK 0x07 /* for the header bits */ |
| 39 | #define RC6_STARTBIT_MASK 0x08 /* for the header bits */ |
| 40 | #define RC6_6A_MCE_TOGGLE_MASK 0x8000 /* for the body bits */ |
| 41 | #define RC6_6A_LCC_MASK 0xffff0000 /* RC6-6A-32 long customer code mask */ |
| 42 | #define RC6_6A_MCE_CC 0x800f0000 /* MCE customer code */ |
| 43 | #define RC6_6A_KATHREIN_CC 0x80460000 /* Kathrein RCU-676 customer code */ |
| 44 | #ifndef CHAR_BIT |
| 45 | #define CHAR_BIT 8 /* Normally in <limits.h> */ |
| 46 | #endif |
| 47 | |
| 48 | enum rc6_mode { |
| 49 | RC6_MODE_0, |
| 50 | RC6_MODE_6A, |
| 51 | RC6_MODE_UNKNOWN, |
| 52 | }; |
| 53 | |
| 54 | enum rc6_state { |
| 55 | STATE_INACTIVE, |
| 56 | STATE_PREFIX_SPACE, |
| 57 | STATE_HEADER_BIT_START, |
| 58 | STATE_HEADER_BIT_END, |
| 59 | STATE_TOGGLE_START, |
| 60 | STATE_TOGGLE_END, |
| 61 | STATE_BODY_BIT_START, |
| 62 | STATE_BODY_BIT_END, |
| 63 | STATE_FINISHED, |
| 64 | }; |
| 65 | |
| 66 | static enum rc6_mode rc6_mode(struct rc6_dec *data) |
| 67 | { |
| 68 | switch (data->header & RC6_MODE_MASK) { |
| 69 | case 0: |
| 70 | return RC6_MODE_0; |
| 71 | case 6: |
| 72 | if (!data->toggle) |
| 73 | return RC6_MODE_6A; |
| 74 | /* fall through */ |
| 75 | default: |
| 76 | return RC6_MODE_UNKNOWN; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * ir_rc6_decode() - Decode one RC6 pulse or space |
| 82 | * @dev: the struct rc_dev descriptor of the device |
| 83 | * @ev: the struct ir_raw_event descriptor of the pulse/space |
| 84 | * |
| 85 | * This function returns -EINVAL if the pulse violates the state machine |
| 86 | */ |
| 87 | static int ir_rc6_decode(struct rc_dev *dev, struct ir_raw_event ev) |
| 88 | { |
| 89 | struct rc6_dec *data = &dev->raw->rc6; |
| 90 | u32 scancode; |
| 91 | u8 toggle; |
| 92 | enum rc_proto protocol; |
| 93 | |
| 94 | if (!is_timing_event(ev)) { |
| 95 | if (ev.reset) |
| 96 | data->state = STATE_INACTIVE; |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2)) |
| 101 | goto out; |
| 102 | |
| 103 | again: |
| 104 | IR_dprintk(2, "RC6 decode started at state %i (%uus %s)\n", |
| 105 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
| 106 | |
| 107 | if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2)) |
| 108 | return 0; |
| 109 | |
| 110 | switch (data->state) { |
| 111 | |
| 112 | case STATE_INACTIVE: |
| 113 | if (!ev.pulse) |
| 114 | break; |
| 115 | |
| 116 | /* Note: larger margin on first pulse since each RC6_UNIT |
| 117 | is quite short and some hardware takes some time to |
| 118 | adjust to the signal */ |
| 119 | if (!eq_margin(ev.duration, RC6_PREFIX_PULSE, RC6_UNIT)) |
| 120 | break; |
| 121 | |
| 122 | data->state = STATE_PREFIX_SPACE; |
| 123 | data->count = 0; |
| 124 | return 0; |
| 125 | |
| 126 | case STATE_PREFIX_SPACE: |
| 127 | if (ev.pulse) |
| 128 | break; |
| 129 | |
| 130 | if (!eq_margin(ev.duration, RC6_PREFIX_SPACE, RC6_UNIT / 2)) |
| 131 | break; |
| 132 | |
| 133 | data->state = STATE_HEADER_BIT_START; |
| 134 | data->header = 0; |
| 135 | return 0; |
| 136 | |
| 137 | case STATE_HEADER_BIT_START: |
| 138 | if (!eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2)) |
| 139 | break; |
| 140 | |
| 141 | data->header <<= 1; |
| 142 | if (ev.pulse) |
| 143 | data->header |= 1; |
| 144 | data->count++; |
| 145 | data->state = STATE_HEADER_BIT_END; |
| 146 | return 0; |
| 147 | |
| 148 | case STATE_HEADER_BIT_END: |
| 149 | if (!is_transition(&ev, &dev->raw->prev_ev)) |
| 150 | break; |
| 151 | |
| 152 | if (data->count == RC6_HEADER_NBITS) |
| 153 | data->state = STATE_TOGGLE_START; |
| 154 | else |
| 155 | data->state = STATE_HEADER_BIT_START; |
| 156 | |
| 157 | decrease_duration(&ev, RC6_BIT_END); |
| 158 | goto again; |
| 159 | |
| 160 | case STATE_TOGGLE_START: |
| 161 | if (!eq_margin(ev.duration, RC6_TOGGLE_START, RC6_UNIT / 2)) |
| 162 | break; |
| 163 | |
| 164 | data->toggle = ev.pulse; |
| 165 | data->state = STATE_TOGGLE_END; |
| 166 | return 0; |
| 167 | |
| 168 | case STATE_TOGGLE_END: |
| 169 | if (!is_transition(&ev, &dev->raw->prev_ev) || |
| 170 | !geq_margin(ev.duration, RC6_TOGGLE_END, RC6_UNIT / 2)) |
| 171 | break; |
| 172 | |
| 173 | if (!(data->header & RC6_STARTBIT_MASK)) { |
| 174 | IR_dprintk(1, "RC6 invalid start bit\n"); |
| 175 | break; |
| 176 | } |
| 177 | |
| 178 | data->state = STATE_BODY_BIT_START; |
| 179 | decrease_duration(&ev, RC6_TOGGLE_END); |
| 180 | data->count = 0; |
| 181 | data->body = 0; |
| 182 | |
| 183 | switch (rc6_mode(data)) { |
| 184 | case RC6_MODE_0: |
| 185 | data->wanted_bits = RC6_0_NBITS; |
| 186 | break; |
| 187 | case RC6_MODE_6A: |
| 188 | data->wanted_bits = RC6_6A_NBITS; |
| 189 | break; |
| 190 | default: |
| 191 | IR_dprintk(1, "RC6 unknown mode\n"); |
| 192 | goto out; |
| 193 | } |
| 194 | goto again; |
| 195 | |
| 196 | case STATE_BODY_BIT_START: |
| 197 | if (eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2)) { |
| 198 | /* Discard LSB's that won't fit in data->body */ |
| 199 | if (data->count++ < CHAR_BIT * sizeof data->body) { |
| 200 | data->body <<= 1; |
| 201 | if (ev.pulse) |
| 202 | data->body |= 1; |
| 203 | } |
| 204 | data->state = STATE_BODY_BIT_END; |
| 205 | return 0; |
| 206 | } else if (RC6_MODE_6A == rc6_mode(data) && !ev.pulse && |
| 207 | geq_margin(ev.duration, RC6_SUFFIX_SPACE, RC6_UNIT / 2)) { |
| 208 | data->state = STATE_FINISHED; |
| 209 | goto again; |
| 210 | } |
| 211 | break; |
| 212 | |
| 213 | case STATE_BODY_BIT_END: |
| 214 | if (!is_transition(&ev, &dev->raw->prev_ev)) |
| 215 | break; |
| 216 | |
| 217 | if (data->count == data->wanted_bits) |
| 218 | data->state = STATE_FINISHED; |
| 219 | else |
| 220 | data->state = STATE_BODY_BIT_START; |
| 221 | |
| 222 | decrease_duration(&ev, RC6_BIT_END); |
| 223 | goto again; |
| 224 | |
| 225 | case STATE_FINISHED: |
| 226 | if (ev.pulse) |
| 227 | break; |
| 228 | |
| 229 | switch (rc6_mode(data)) { |
| 230 | case RC6_MODE_0: |
| 231 | scancode = data->body; |
| 232 | toggle = data->toggle; |
| 233 | protocol = RC_PROTO_RC6_0; |
| 234 | IR_dprintk(1, "RC6(0) scancode 0x%04x (toggle: %u)\n", |
| 235 | scancode, toggle); |
| 236 | break; |
| 237 | |
| 238 | case RC6_MODE_6A: |
| 239 | if (data->count > CHAR_BIT * sizeof data->body) { |
| 240 | IR_dprintk(1, "RC6 too many (%u) data bits\n", |
| 241 | data->count); |
| 242 | goto out; |
| 243 | } |
| 244 | |
| 245 | scancode = data->body; |
| 246 | switch (data->count) { |
| 247 | case 20: |
| 248 | protocol = RC_PROTO_RC6_6A_20; |
| 249 | toggle = 0; |
| 250 | break; |
| 251 | case 24: |
| 252 | protocol = RC_PROTO_RC6_6A_24; |
| 253 | toggle = 0; |
| 254 | break; |
| 255 | case 32: |
| 256 | switch (scancode & RC6_6A_LCC_MASK) { |
| 257 | case RC6_6A_MCE_CC: |
| 258 | case RC6_6A_KATHREIN_CC: |
| 259 | protocol = RC_PROTO_RC6_MCE; |
| 260 | toggle = !!(scancode & RC6_6A_MCE_TOGGLE_MASK); |
| 261 | scancode &= ~RC6_6A_MCE_TOGGLE_MASK; |
| 262 | break; |
| 263 | default: |
| 264 | protocol = RC_PROTO_RC6_6A_32; |
| 265 | toggle = 0; |
| 266 | break; |
| 267 | } |
| 268 | break; |
| 269 | default: |
| 270 | IR_dprintk(1, "RC6(6A) unsupported length\n"); |
| 271 | goto out; |
| 272 | } |
| 273 | |
| 274 | IR_dprintk(1, "RC6(6A) proto 0x%04x, scancode 0x%08x (toggle: %u)\n", |
| 275 | protocol, scancode, toggle); |
| 276 | break; |
| 277 | default: |
| 278 | IR_dprintk(1, "RC6 unknown mode\n"); |
| 279 | goto out; |
| 280 | } |
| 281 | |
| 282 | rc_keydown(dev, protocol, scancode, toggle); |
| 283 | data->state = STATE_INACTIVE; |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | out: |
| 288 | IR_dprintk(1, "RC6 decode failed at state %i (%uus %s)\n", |
| 289 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
| 290 | data->state = STATE_INACTIVE; |
| 291 | return -EINVAL; |
| 292 | } |
| 293 | |
| 294 | static const struct ir_raw_timings_manchester ir_rc6_timings[4] = { |
| 295 | { |
| 296 | .leader = RC6_PREFIX_PULSE, |
| 297 | .pulse_space_start = 0, |
| 298 | .clock = RC6_UNIT, |
| 299 | .invert = 1, |
| 300 | .trailer_space = RC6_PREFIX_SPACE, |
| 301 | }, |
| 302 | { |
| 303 | .clock = RC6_UNIT, |
| 304 | .invert = 1, |
| 305 | }, |
| 306 | { |
| 307 | .clock = RC6_UNIT * 2, |
| 308 | .invert = 1, |
| 309 | }, |
| 310 | { |
| 311 | .clock = RC6_UNIT, |
| 312 | .invert = 1, |
| 313 | .trailer_space = RC6_SUFFIX_SPACE, |
| 314 | }, |
| 315 | }; |
| 316 | |
| 317 | /** |
| 318 | * ir_rc6_encode() - Encode a scancode as a stream of raw events |
| 319 | * |
| 320 | * @protocol: protocol to encode |
| 321 | * @scancode: scancode to encode |
| 322 | * @events: array of raw ir events to write into |
| 323 | * @max: maximum size of @events |
| 324 | * |
| 325 | * Returns: The number of events written. |
| 326 | * -ENOBUFS if there isn't enough space in the array to fit the |
| 327 | * encoding. In this case all @max events will have been written. |
| 328 | * -EINVAL if the scancode is ambiguous or invalid. |
| 329 | */ |
| 330 | static int ir_rc6_encode(enum rc_proto protocol, u32 scancode, |
| 331 | struct ir_raw_event *events, unsigned int max) |
| 332 | { |
| 333 | int ret; |
| 334 | struct ir_raw_event *e = events; |
| 335 | |
| 336 | if (protocol == RC_PROTO_RC6_0) { |
| 337 | /* Modulate the preamble */ |
| 338 | ret = ir_raw_gen_manchester(&e, max, &ir_rc6_timings[0], 0, 0); |
| 339 | if (ret < 0) |
| 340 | return ret; |
| 341 | |
| 342 | /* Modulate the header (Start Bit & Mode-0) */ |
| 343 | ret = ir_raw_gen_manchester(&e, max - (e - events), |
| 344 | &ir_rc6_timings[1], |
| 345 | RC6_HEADER_NBITS, (1 << 3)); |
| 346 | if (ret < 0) |
| 347 | return ret; |
| 348 | |
| 349 | /* Modulate Trailer Bit */ |
| 350 | ret = ir_raw_gen_manchester(&e, max - (e - events), |
| 351 | &ir_rc6_timings[2], 1, 0); |
| 352 | if (ret < 0) |
| 353 | return ret; |
| 354 | |
| 355 | /* Modulate rest of the data */ |
| 356 | ret = ir_raw_gen_manchester(&e, max - (e - events), |
| 357 | &ir_rc6_timings[3], RC6_0_NBITS, |
| 358 | scancode); |
| 359 | if (ret < 0) |
| 360 | return ret; |
| 361 | |
| 362 | } else { |
| 363 | int bits; |
| 364 | |
| 365 | switch (protocol) { |
| 366 | case RC_PROTO_RC6_MCE: |
| 367 | case RC_PROTO_RC6_6A_32: |
| 368 | bits = 32; |
| 369 | break; |
| 370 | case RC_PROTO_RC6_6A_24: |
| 371 | bits = 24; |
| 372 | break; |
| 373 | case RC_PROTO_RC6_6A_20: |
| 374 | bits = 20; |
| 375 | break; |
| 376 | default: |
| 377 | return -EINVAL; |
| 378 | } |
| 379 | |
| 380 | /* Modulate the preamble */ |
| 381 | ret = ir_raw_gen_manchester(&e, max, &ir_rc6_timings[0], 0, 0); |
| 382 | if (ret < 0) |
| 383 | return ret; |
| 384 | |
| 385 | /* Modulate the header (Start Bit & Header-version 6 */ |
| 386 | ret = ir_raw_gen_manchester(&e, max - (e - events), |
| 387 | &ir_rc6_timings[1], |
| 388 | RC6_HEADER_NBITS, (1 << 3 | 6)); |
| 389 | if (ret < 0) |
| 390 | return ret; |
| 391 | |
| 392 | /* Modulate Trailer Bit */ |
| 393 | ret = ir_raw_gen_manchester(&e, max - (e - events), |
| 394 | &ir_rc6_timings[2], 1, 0); |
| 395 | if (ret < 0) |
| 396 | return ret; |
| 397 | |
| 398 | /* Modulate rest of the data */ |
| 399 | ret = ir_raw_gen_manchester(&e, max - (e - events), |
| 400 | &ir_rc6_timings[3], |
| 401 | bits, |
| 402 | scancode); |
| 403 | if (ret < 0) |
| 404 | return ret; |
| 405 | } |
| 406 | |
| 407 | return e - events; |
| 408 | } |
| 409 | |
| 410 | static struct ir_raw_handler rc6_handler = { |
| 411 | .protocols = RC_PROTO_BIT_RC6_0 | RC_PROTO_BIT_RC6_6A_20 | |
| 412 | RC_PROTO_BIT_RC6_6A_24 | RC_PROTO_BIT_RC6_6A_32 | |
| 413 | RC_PROTO_BIT_RC6_MCE, |
| 414 | .decode = ir_rc6_decode, |
| 415 | .encode = ir_rc6_encode, |
| 416 | }; |
| 417 | |
| 418 | static int __init ir_rc6_decode_init(void) |
| 419 | { |
| 420 | ir_raw_handler_register(&rc6_handler); |
| 421 | |
| 422 | printk(KERN_INFO "IR RC6 protocol handler initialized\n"); |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | static void __exit ir_rc6_decode_exit(void) |
| 427 | { |
| 428 | ir_raw_handler_unregister(&rc6_handler); |
| 429 | } |
| 430 | |
| 431 | module_init(ir_rc6_decode_init); |
| 432 | module_exit(ir_rc6_decode_exit); |
| 433 | |
| 434 | MODULE_LICENSE("GPL"); |
| 435 | MODULE_AUTHOR("David Härdeman <david@hardeman.nu>"); |
| 436 | MODULE_DESCRIPTION("RC6 IR protocol decoder"); |