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 | |
| 15 | #include "ir-core-priv.h" |
| 16 | |
| 17 | /* |
| 18 | * This decoder currently supports: |
| 19 | * RC6-0-16 (standard toggle bit in header) |
| 20 | * RC6-6A-24 (no toggle bit) |
| 21 | * RC6-6A-32 (MCE version with toggle bit in body) |
| 22 | */ |
| 23 | |
| 24 | #define RC6_UNIT 444444 /* us */ |
| 25 | #define RC6_HEADER_NBITS 4 /* not including toggle bit */ |
| 26 | #define RC6_0_NBITS 16 |
| 27 | #define RC6_6A_SMALL_NBITS 24 |
| 28 | #define RC6_6A_LARGE_NBITS 32 |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 29 | #define RC6_PREFIX_PULSE (6 * RC6_UNIT) |
| 30 | #define RC6_PREFIX_SPACE (2 * RC6_UNIT) |
| 31 | #define RC6_BIT_START (1 * RC6_UNIT) |
| 32 | #define RC6_BIT_END (1 * RC6_UNIT) |
| 33 | #define RC6_TOGGLE_START (2 * RC6_UNIT) |
| 34 | #define RC6_TOGGLE_END (2 * RC6_UNIT) |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 35 | #define RC6_MODE_MASK 0x07 /* for the header bits */ |
| 36 | #define RC6_STARTBIT_MASK 0x08 /* for the header bits */ |
| 37 | #define RC6_6A_MCE_TOGGLE_MASK 0x8000 /* for the body bits */ |
| 38 | |
| 39 | /* Used to register rc6_decoder clients */ |
| 40 | static LIST_HEAD(decoder_list); |
| 41 | static DEFINE_SPINLOCK(decoder_lock); |
| 42 | |
| 43 | enum rc6_mode { |
| 44 | RC6_MODE_0, |
| 45 | RC6_MODE_6A, |
| 46 | RC6_MODE_UNKNOWN, |
| 47 | }; |
| 48 | |
| 49 | enum rc6_state { |
| 50 | STATE_INACTIVE, |
| 51 | STATE_PREFIX_SPACE, |
| 52 | STATE_HEADER_BIT_START, |
| 53 | STATE_HEADER_BIT_END, |
| 54 | STATE_TOGGLE_START, |
| 55 | STATE_TOGGLE_END, |
| 56 | STATE_BODY_BIT_START, |
| 57 | STATE_BODY_BIT_END, |
| 58 | STATE_FINISHED, |
| 59 | }; |
| 60 | |
| 61 | struct decoder_data { |
| 62 | struct list_head list; |
| 63 | struct ir_input_dev *ir_dev; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 64 | |
| 65 | /* State machine control */ |
| 66 | enum rc6_state state; |
| 67 | u8 header; |
| 68 | u32 body; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 69 | struct ir_raw_event prev_ev; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 70 | bool toggle; |
| 71 | unsigned count; |
| 72 | unsigned wanted_bits; |
| 73 | }; |
| 74 | |
| 75 | |
| 76 | /** |
| 77 | * get_decoder_data() - gets decoder data |
| 78 | * @input_dev: input device |
| 79 | * |
| 80 | * Returns the struct decoder_data that corresponds to a device |
| 81 | */ |
| 82 | static struct decoder_data *get_decoder_data(struct ir_input_dev *ir_dev) |
| 83 | { |
| 84 | struct decoder_data *data = NULL; |
| 85 | |
| 86 | spin_lock(&decoder_lock); |
| 87 | list_for_each_entry(data, &decoder_list, list) { |
| 88 | if (data->ir_dev == ir_dev) |
| 89 | break; |
| 90 | } |
| 91 | spin_unlock(&decoder_lock); |
| 92 | return data; |
| 93 | } |
| 94 | |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 95 | static enum rc6_mode rc6_mode(struct decoder_data *data) { |
| 96 | switch (data->header & RC6_MODE_MASK) { |
| 97 | case 0: |
| 98 | return RC6_MODE_0; |
| 99 | case 6: |
| 100 | if (!data->toggle) |
| 101 | return RC6_MODE_6A; |
| 102 | /* fall through */ |
| 103 | default: |
| 104 | return RC6_MODE_UNKNOWN; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * ir_rc6_decode() - Decode one RC6 pulse or space |
| 110 | * @input_dev: the struct input_dev descriptor of the device |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 111 | * @ev: the struct ir_raw_event descriptor of the pulse/space |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 112 | * |
| 113 | * This function returns -EINVAL if the pulse violates the state machine |
| 114 | */ |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 115 | static int ir_rc6_decode(struct input_dev *input_dev, struct ir_raw_event ev) |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 116 | { |
| 117 | struct decoder_data *data; |
| 118 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 119 | u32 scancode; |
| 120 | u8 toggle; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 121 | |
| 122 | data = get_decoder_data(ir_dev); |
| 123 | if (!data) |
| 124 | return -EINVAL; |
| 125 | |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame] | 126 | if (!(ir_dev->raw->enabled_protocols & IR_TYPE_RC6)) |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 127 | return 0; |
| 128 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 129 | if (IS_RESET(ev)) { |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 130 | data->state = STATE_INACTIVE; |
| 131 | return 0; |
| 132 | } |
| 133 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 134 | if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2)) |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 135 | goto out; |
| 136 | |
| 137 | again: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 138 | IR_dprintk(2, "RC6 decode started at state %i (%uus %s)\n", |
| 139 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 140 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 141 | if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2)) |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 142 | return 0; |
| 143 | |
| 144 | switch (data->state) { |
| 145 | |
| 146 | case STATE_INACTIVE: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 147 | if (!ev.pulse) |
| 148 | break; |
| 149 | |
| 150 | /* Note: larger margin on first pulse since each RC6_UNIT |
| 151 | is quite short and some hardware takes some time to |
| 152 | adjust to the signal */ |
| 153 | if (!eq_margin(ev.duration, RC6_PREFIX_PULSE, RC6_UNIT)) |
| 154 | break; |
| 155 | |
| 156 | data->state = STATE_PREFIX_SPACE; |
| 157 | data->count = 0; |
| 158 | return 0; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 159 | |
| 160 | case STATE_PREFIX_SPACE: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 161 | if (ev.pulse) |
| 162 | break; |
| 163 | |
| 164 | if (!eq_margin(ev.duration, RC6_PREFIX_SPACE, RC6_UNIT / 2)) |
| 165 | break; |
| 166 | |
| 167 | data->state = STATE_HEADER_BIT_START; |
| 168 | return 0; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 169 | |
| 170 | case STATE_HEADER_BIT_START: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 171 | if (!eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2)) |
| 172 | break; |
| 173 | |
| 174 | data->header <<= 1; |
| 175 | if (ev.pulse) |
| 176 | data->header |= 1; |
| 177 | data->count++; |
| 178 | data->prev_ev = ev; |
| 179 | data->state = STATE_HEADER_BIT_END; |
| 180 | return 0; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 181 | |
| 182 | case STATE_HEADER_BIT_END: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 183 | if (!is_transition(&ev, &data->prev_ev)) |
| 184 | break; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 185 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 186 | if (data->count == RC6_HEADER_NBITS) |
| 187 | data->state = STATE_TOGGLE_START; |
| 188 | else |
| 189 | data->state = STATE_HEADER_BIT_START; |
| 190 | |
| 191 | decrease_duration(&ev, RC6_BIT_END); |
| 192 | goto again; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 193 | |
| 194 | case STATE_TOGGLE_START: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 195 | if (!eq_margin(ev.duration, RC6_TOGGLE_START, RC6_UNIT / 2)) |
| 196 | break; |
| 197 | |
| 198 | data->toggle = ev.pulse; |
| 199 | data->prev_ev = ev; |
| 200 | data->state = STATE_TOGGLE_END; |
| 201 | return 0; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 202 | |
| 203 | case STATE_TOGGLE_END: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 204 | if (!is_transition(&ev, &data->prev_ev) || |
| 205 | !geq_margin(ev.duration, RC6_TOGGLE_END, RC6_UNIT / 2)) |
| 206 | break; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 207 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 208 | if (!(data->header & RC6_STARTBIT_MASK)) { |
| 209 | IR_dprintk(1, "RC6 invalid start bit\n"); |
| 210 | break; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 211 | } |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 212 | |
| 213 | data->state = STATE_BODY_BIT_START; |
| 214 | data->prev_ev = ev; |
| 215 | decrease_duration(&ev, RC6_TOGGLE_END); |
| 216 | data->count = 0; |
| 217 | |
| 218 | switch (rc6_mode(data)) { |
| 219 | case RC6_MODE_0: |
| 220 | data->wanted_bits = RC6_0_NBITS; |
| 221 | break; |
| 222 | case RC6_MODE_6A: |
| 223 | /* This might look weird, but we basically |
| 224 | check the value of the first body bit to |
| 225 | determine the number of bits in mode 6A */ |
| 226 | if ((!ev.pulse && !geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2)) || |
| 227 | geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2)) |
| 228 | data->wanted_bits = RC6_6A_LARGE_NBITS; |
| 229 | else |
| 230 | data->wanted_bits = RC6_6A_SMALL_NBITS; |
| 231 | break; |
| 232 | default: |
| 233 | IR_dprintk(1, "RC6 unknown mode\n"); |
| 234 | goto out; |
| 235 | } |
| 236 | goto again; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 237 | |
| 238 | case STATE_BODY_BIT_START: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 239 | if (!eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2)) |
| 240 | break; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 241 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 242 | data->body <<= 1; |
| 243 | if (ev.pulse) |
| 244 | data->body |= 1; |
| 245 | data->count++; |
| 246 | data->prev_ev = ev; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 247 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 248 | data->state = STATE_BODY_BIT_END; |
| 249 | return 0; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 250 | |
| 251 | case STATE_BODY_BIT_END: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 252 | if (!is_transition(&ev, &data->prev_ev)) |
| 253 | break; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 254 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 255 | if (data->count == data->wanted_bits) |
| 256 | data->state = STATE_FINISHED; |
| 257 | else |
| 258 | data->state = STATE_BODY_BIT_START; |
| 259 | |
| 260 | decrease_duration(&ev, RC6_BIT_END); |
| 261 | goto again; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 262 | |
| 263 | case STATE_FINISHED: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 264 | if (ev.pulse) |
| 265 | break; |
| 266 | |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 267 | switch (rc6_mode(data)) { |
| 268 | case RC6_MODE_0: |
| 269 | scancode = data->body & 0xffff; |
| 270 | toggle = data->toggle; |
| 271 | IR_dprintk(1, "RC6(0) scancode 0x%04x (toggle: %u)\n", |
| 272 | scancode, toggle); |
| 273 | break; |
| 274 | case RC6_MODE_6A: |
| 275 | if (data->wanted_bits == RC6_6A_LARGE_NBITS) { |
| 276 | toggle = data->body & RC6_6A_MCE_TOGGLE_MASK ? 1 : 0; |
| 277 | scancode = data->body & ~RC6_6A_MCE_TOGGLE_MASK; |
| 278 | } else { |
| 279 | toggle = 0; |
| 280 | scancode = data->body & 0xffffff; |
| 281 | } |
| 282 | |
| 283 | IR_dprintk(1, "RC6(6A) scancode 0x%08x (toggle: %u)\n", |
| 284 | scancode, toggle); |
| 285 | break; |
| 286 | default: |
| 287 | IR_dprintk(1, "RC6 unknown mode\n"); |
| 288 | goto out; |
| 289 | } |
| 290 | |
| 291 | ir_keydown(input_dev, scancode, toggle); |
| 292 | data->state = STATE_INACTIVE; |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | out: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 297 | IR_dprintk(1, "RC6 decode failed at state %i (%uus %s)\n", |
| 298 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 299 | data->state = STATE_INACTIVE; |
| 300 | return -EINVAL; |
| 301 | } |
| 302 | |
| 303 | static int ir_rc6_register(struct input_dev *input_dev) |
| 304 | { |
| 305 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 306 | struct decoder_data *data; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 307 | |
| 308 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame] | 309 | if (!data) |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 310 | return -ENOMEM; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 311 | |
| 312 | data->ir_dev = ir_dev; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 313 | |
| 314 | spin_lock(&decoder_lock); |
| 315 | list_add_tail(&data->list, &decoder_list); |
| 316 | spin_unlock(&decoder_lock); |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | static int ir_rc6_unregister(struct input_dev *input_dev) |
| 322 | { |
| 323 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 324 | static struct decoder_data *data; |
| 325 | |
| 326 | data = get_decoder_data(ir_dev); |
| 327 | if (!data) |
| 328 | return 0; |
| 329 | |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 330 | spin_lock(&decoder_lock); |
| 331 | list_del(&data->list); |
| 332 | spin_unlock(&decoder_lock); |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | static struct ir_raw_handler rc6_handler = { |
David Härdeman | 667c9eb | 2010-06-13 17:29:31 -0300 | [diff] [blame] | 338 | .protocols = IR_TYPE_RC6, |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 339 | .decode = ir_rc6_decode, |
| 340 | .raw_register = ir_rc6_register, |
| 341 | .raw_unregister = ir_rc6_unregister, |
| 342 | }; |
| 343 | |
| 344 | static int __init ir_rc6_decode_init(void) |
| 345 | { |
| 346 | ir_raw_handler_register(&rc6_handler); |
| 347 | |
| 348 | printk(KERN_INFO "IR RC6 protocol handler initialized\n"); |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | static void __exit ir_rc6_decode_exit(void) |
| 353 | { |
| 354 | ir_raw_handler_unregister(&rc6_handler); |
| 355 | } |
| 356 | |
| 357 | module_init(ir_rc6_decode_init); |
| 358 | module_exit(ir_rc6_decode_exit); |
| 359 | |
| 360 | MODULE_LICENSE("GPL"); |
| 361 | MODULE_AUTHOR("David Härdeman <david@hardeman.nu>"); |
| 362 | MODULE_DESCRIPTION("RC6 IR protocol decoder"); |