David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [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 | |
Mauro Carvalho Chehab | f62de67 | 2010-11-09 23:09:57 -0300 | [diff] [blame] | 15 | #include "rc-core-priv.h" |
Paul Gortmaker | 7a707b8 | 2011-07-03 14:03:12 -0400 | [diff] [blame] | 16 | #include <linux/module.h> |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * This decoder currently supports: |
| 20 | * RC6-0-16 (standard toggle bit in header) |
lawrence rust | db9bc66 | 2011-10-31 08:39:32 -0300 | [diff] [blame] | 21 | * RC6-6A-20 (no toggle bit) |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 22 | * RC6-6A-24 (no toggle bit) |
| 23 | * RC6-6A-32 (MCE version with toggle bit in body) |
| 24 | */ |
| 25 | |
lawrence rust | db9bc66 | 2011-10-31 08:39:32 -0300 | [diff] [blame] | 26 | #define RC6_UNIT 444444 /* nanosecs */ |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 27 | #define RC6_HEADER_NBITS 4 /* not including toggle bit */ |
| 28 | #define RC6_0_NBITS 16 |
lawrence rust | db9bc66 | 2011-10-31 08:39:32 -0300 | [diff] [blame] | 29 | #define RC6_6A_32_NBITS 32 |
| 30 | #define RC6_6A_NBITS 128 /* Variable 8..128 */ |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 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) |
lawrence rust | db9bc66 | 2011-10-31 08:39:32 -0300 | [diff] [blame] | 37 | #define RC6_SUFFIX_SPACE (6 * RC6_UNIT) |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 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 */ |
lawrence rust | db9bc66 | 2011-10-31 08:39:32 -0300 | [diff] [blame] | 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 | #ifndef CHAR_BIT |
| 44 | #define CHAR_BIT 8 /* Normally in <limits.h> */ |
| 45 | #endif |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 46 | |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 47 | enum rc6_mode { |
| 48 | RC6_MODE_0, |
| 49 | RC6_MODE_6A, |
| 50 | RC6_MODE_UNKNOWN, |
| 51 | }; |
| 52 | |
| 53 | enum rc6_state { |
| 54 | STATE_INACTIVE, |
| 55 | STATE_PREFIX_SPACE, |
| 56 | STATE_HEADER_BIT_START, |
| 57 | STATE_HEADER_BIT_END, |
| 58 | STATE_TOGGLE_START, |
| 59 | STATE_TOGGLE_END, |
| 60 | STATE_BODY_BIT_START, |
| 61 | STATE_BODY_BIT_END, |
| 62 | STATE_FINISHED, |
| 63 | }; |
| 64 | |
David Härdeman | c216369 | 2010-06-13 17:29:36 -0300 | [diff] [blame] | 65 | static enum rc6_mode rc6_mode(struct rc6_dec *data) |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 66 | { |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 67 | switch (data->header & RC6_MODE_MASK) { |
| 68 | case 0: |
| 69 | return RC6_MODE_0; |
| 70 | case 6: |
| 71 | if (!data->toggle) |
| 72 | return RC6_MODE_6A; |
| 73 | /* fall through */ |
| 74 | default: |
| 75 | return RC6_MODE_UNKNOWN; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * ir_rc6_decode() - Decode one RC6 pulse or space |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 81 | * @dev: the struct rc_dev descriptor of the device |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 82 | * @ev: the struct ir_raw_event descriptor of the pulse/space |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 83 | * |
| 84 | * This function returns -EINVAL if the pulse violates the state machine |
| 85 | */ |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 86 | static int ir_rc6_decode(struct rc_dev *dev, struct ir_raw_event ev) |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 87 | { |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 88 | struct rc6_dec *data = &dev->raw->rc6; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 89 | u32 scancode; |
| 90 | u8 toggle; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 91 | |
David Härdeman | 9719afa | 2013-03-06 16:52:05 -0300 | [diff] [blame] | 92 | if (!(dev->enabled_protocols & |
David Härdeman | c003ab1 | 2012-10-11 19:11:54 -0300 | [diff] [blame] | 93 | (RC_BIT_RC6_0 | RC_BIT_RC6_6A_20 | RC_BIT_RC6_6A_24 | |
| 94 | RC_BIT_RC6_6A_32 | RC_BIT_RC6_MCE))) |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 95 | return 0; |
| 96 | |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 97 | if (!is_timing_event(ev)) { |
| 98 | if (ev.reset) |
| 99 | data->state = STATE_INACTIVE; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 100 | return 0; |
| 101 | } |
| 102 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 103 | if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2)) |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 104 | goto out; |
| 105 | |
| 106 | again: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 107 | IR_dprintk(2, "RC6 decode started at state %i (%uus %s)\n", |
| 108 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 109 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 110 | if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2)) |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 111 | return 0; |
| 112 | |
| 113 | switch (data->state) { |
| 114 | |
| 115 | case STATE_INACTIVE: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 116 | if (!ev.pulse) |
| 117 | break; |
| 118 | |
| 119 | /* Note: larger margin on first pulse since each RC6_UNIT |
| 120 | is quite short and some hardware takes some time to |
| 121 | adjust to the signal */ |
| 122 | if (!eq_margin(ev.duration, RC6_PREFIX_PULSE, RC6_UNIT)) |
| 123 | break; |
| 124 | |
| 125 | data->state = STATE_PREFIX_SPACE; |
| 126 | data->count = 0; |
| 127 | return 0; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 128 | |
| 129 | case STATE_PREFIX_SPACE: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 130 | if (ev.pulse) |
| 131 | break; |
| 132 | |
| 133 | if (!eq_margin(ev.duration, RC6_PREFIX_SPACE, RC6_UNIT / 2)) |
| 134 | break; |
| 135 | |
| 136 | data->state = STATE_HEADER_BIT_START; |
lawrence rust | db9bc66 | 2011-10-31 08:39:32 -0300 | [diff] [blame] | 137 | data->header = 0; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 138 | return 0; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 139 | |
| 140 | case STATE_HEADER_BIT_START: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 141 | if (!eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2)) |
| 142 | break; |
| 143 | |
| 144 | data->header <<= 1; |
| 145 | if (ev.pulse) |
| 146 | data->header |= 1; |
| 147 | data->count++; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 148 | data->state = STATE_HEADER_BIT_END; |
| 149 | return 0; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 150 | |
| 151 | case STATE_HEADER_BIT_END: |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 152 | if (!is_transition(&ev, &dev->raw->prev_ev)) |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 153 | break; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 154 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 155 | if (data->count == RC6_HEADER_NBITS) |
| 156 | data->state = STATE_TOGGLE_START; |
| 157 | else |
| 158 | data->state = STATE_HEADER_BIT_START; |
| 159 | |
| 160 | decrease_duration(&ev, RC6_BIT_END); |
| 161 | goto again; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 162 | |
| 163 | case STATE_TOGGLE_START: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 164 | if (!eq_margin(ev.duration, RC6_TOGGLE_START, RC6_UNIT / 2)) |
| 165 | break; |
| 166 | |
| 167 | data->toggle = ev.pulse; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 168 | data->state = STATE_TOGGLE_END; |
| 169 | return 0; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 170 | |
| 171 | case STATE_TOGGLE_END: |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 172 | if (!is_transition(&ev, &dev->raw->prev_ev) || |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 173 | !geq_margin(ev.duration, RC6_TOGGLE_END, RC6_UNIT / 2)) |
| 174 | break; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 175 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 176 | if (!(data->header & RC6_STARTBIT_MASK)) { |
| 177 | IR_dprintk(1, "RC6 invalid start bit\n"); |
| 178 | break; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 179 | } |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 180 | |
| 181 | data->state = STATE_BODY_BIT_START; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 182 | decrease_duration(&ev, RC6_TOGGLE_END); |
| 183 | data->count = 0; |
lawrence rust | db9bc66 | 2011-10-31 08:39:32 -0300 | [diff] [blame] | 184 | data->body = 0; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 185 | |
| 186 | switch (rc6_mode(data)) { |
| 187 | case RC6_MODE_0: |
| 188 | data->wanted_bits = RC6_0_NBITS; |
| 189 | break; |
| 190 | case RC6_MODE_6A: |
lawrence rust | db9bc66 | 2011-10-31 08:39:32 -0300 | [diff] [blame] | 191 | data->wanted_bits = RC6_6A_NBITS; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 192 | break; |
| 193 | default: |
| 194 | IR_dprintk(1, "RC6 unknown mode\n"); |
| 195 | goto out; |
| 196 | } |
| 197 | goto again; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 198 | |
| 199 | case STATE_BODY_BIT_START: |
lawrence rust | db9bc66 | 2011-10-31 08:39:32 -0300 | [diff] [blame] | 200 | if (eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2)) { |
| 201 | /* Discard LSB's that won't fit in data->body */ |
| 202 | if (data->count++ < CHAR_BIT * sizeof data->body) { |
| 203 | data->body <<= 1; |
| 204 | if (ev.pulse) |
| 205 | data->body |= 1; |
| 206 | } |
| 207 | data->state = STATE_BODY_BIT_END; |
| 208 | return 0; |
| 209 | } else if (RC6_MODE_6A == rc6_mode(data) && !ev.pulse && |
| 210 | geq_margin(ev.duration, RC6_SUFFIX_SPACE, RC6_UNIT / 2)) { |
| 211 | data->state = STATE_FINISHED; |
| 212 | goto again; |
| 213 | } |
| 214 | break; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 215 | |
| 216 | case STATE_BODY_BIT_END: |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 217 | if (!is_transition(&ev, &dev->raw->prev_ev)) |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 218 | break; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 219 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 220 | if (data->count == data->wanted_bits) |
| 221 | data->state = STATE_FINISHED; |
| 222 | else |
| 223 | data->state = STATE_BODY_BIT_START; |
| 224 | |
| 225 | decrease_duration(&ev, RC6_BIT_END); |
| 226 | goto again; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 227 | |
| 228 | case STATE_FINISHED: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 229 | if (ev.pulse) |
| 230 | break; |
| 231 | |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 232 | switch (rc6_mode(data)) { |
| 233 | case RC6_MODE_0: |
lawrence rust | db9bc66 | 2011-10-31 08:39:32 -0300 | [diff] [blame] | 234 | scancode = data->body; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 235 | toggle = data->toggle; |
| 236 | IR_dprintk(1, "RC6(0) scancode 0x%04x (toggle: %u)\n", |
| 237 | scancode, toggle); |
| 238 | break; |
| 239 | case RC6_MODE_6A: |
lawrence rust | db9bc66 | 2011-10-31 08:39:32 -0300 | [diff] [blame] | 240 | if (data->count > CHAR_BIT * sizeof data->body) { |
| 241 | IR_dprintk(1, "RC6 too many (%u) data bits\n", |
| 242 | data->count); |
| 243 | goto out; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 244 | } |
| 245 | |
lawrence rust | db9bc66 | 2011-10-31 08:39:32 -0300 | [diff] [blame] | 246 | scancode = data->body; |
| 247 | if (data->count == RC6_6A_32_NBITS && |
| 248 | (scancode & RC6_6A_LCC_MASK) == RC6_6A_MCE_CC) { |
| 249 | /* MCE RC */ |
| 250 | toggle = (scancode & RC6_6A_MCE_TOGGLE_MASK) ? 1 : 0; |
| 251 | scancode &= ~RC6_6A_MCE_TOGGLE_MASK; |
| 252 | } else { |
| 253 | toggle = 0; |
| 254 | } |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 255 | IR_dprintk(1, "RC6(6A) scancode 0x%08x (toggle: %u)\n", |
| 256 | scancode, toggle); |
| 257 | break; |
| 258 | default: |
| 259 | IR_dprintk(1, "RC6 unknown mode\n"); |
| 260 | goto out; |
| 261 | } |
| 262 | |
Mauro Carvalho Chehab | ca86674 | 2010-11-17 13:53:11 -0300 | [diff] [blame] | 263 | rc_keydown(dev, scancode, toggle); |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 264 | data->state = STATE_INACTIVE; |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | out: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 269 | IR_dprintk(1, "RC6 decode failed at state %i (%uus %s)\n", |
| 270 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 271 | data->state = STATE_INACTIVE; |
| 272 | return -EINVAL; |
| 273 | } |
| 274 | |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 275 | static struct ir_raw_handler rc6_handler = { |
David Härdeman | c003ab1 | 2012-10-11 19:11:54 -0300 | [diff] [blame] | 276 | .protocols = RC_BIT_RC6_0 | RC_BIT_RC6_6A_20 | |
| 277 | RC_BIT_RC6_6A_24 | RC_BIT_RC6_6A_32 | |
| 278 | RC_BIT_RC6_MCE, |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 279 | .decode = ir_rc6_decode, |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 280 | }; |
| 281 | |
| 282 | static int __init ir_rc6_decode_init(void) |
| 283 | { |
| 284 | ir_raw_handler_register(&rc6_handler); |
| 285 | |
| 286 | printk(KERN_INFO "IR RC6 protocol handler initialized\n"); |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static void __exit ir_rc6_decode_exit(void) |
| 291 | { |
| 292 | ir_raw_handler_unregister(&rc6_handler); |
| 293 | } |
| 294 | |
| 295 | module_init(ir_rc6_decode_init); |
| 296 | module_exit(ir_rc6_decode_exit); |
| 297 | |
| 298 | MODULE_LICENSE("GPL"); |
| 299 | MODULE_AUTHOR("David Härdeman <david@hardeman.nu>"); |
| 300 | MODULE_DESCRIPTION("RC6 IR protocol decoder"); |