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; |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 91 | enum rc_proto protocol; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 92 | |
Maxim Levitsky | 4651918 | 2010-10-16 19:56:28 -0300 | [diff] [blame] | 93 | if (!is_timing_event(ev)) { |
| 94 | if (ev.reset) |
| 95 | data->state = STATE_INACTIVE; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 99 | if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2)) |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 100 | goto out; |
| 101 | |
| 102 | again: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 103 | IR_dprintk(2, "RC6 decode started at state %i (%uus %s)\n", |
| 104 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 105 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 106 | if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2)) |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 107 | return 0; |
| 108 | |
| 109 | switch (data->state) { |
| 110 | |
| 111 | case STATE_INACTIVE: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 112 | if (!ev.pulse) |
| 113 | break; |
| 114 | |
| 115 | /* Note: larger margin on first pulse since each RC6_UNIT |
| 116 | is quite short and some hardware takes some time to |
| 117 | adjust to the signal */ |
| 118 | if (!eq_margin(ev.duration, RC6_PREFIX_PULSE, RC6_UNIT)) |
| 119 | break; |
| 120 | |
| 121 | data->state = STATE_PREFIX_SPACE; |
| 122 | data->count = 0; |
| 123 | return 0; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 124 | |
| 125 | case STATE_PREFIX_SPACE: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 126 | if (ev.pulse) |
| 127 | break; |
| 128 | |
| 129 | if (!eq_margin(ev.duration, RC6_PREFIX_SPACE, RC6_UNIT / 2)) |
| 130 | break; |
| 131 | |
| 132 | data->state = STATE_HEADER_BIT_START; |
lawrence rust | db9bc66 | 2011-10-31 08:39:32 -0300 | [diff] [blame] | 133 | data->header = 0; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 134 | return 0; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 135 | |
| 136 | case STATE_HEADER_BIT_START: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 137 | if (!eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2)) |
| 138 | break; |
| 139 | |
| 140 | data->header <<= 1; |
| 141 | if (ev.pulse) |
| 142 | data->header |= 1; |
| 143 | data->count++; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 144 | data->state = STATE_HEADER_BIT_END; |
| 145 | return 0; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 146 | |
| 147 | case STATE_HEADER_BIT_END: |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 148 | if (!is_transition(&ev, &dev->raw->prev_ev)) |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 149 | break; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 150 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 151 | if (data->count == RC6_HEADER_NBITS) |
| 152 | data->state = STATE_TOGGLE_START; |
| 153 | else |
| 154 | data->state = STATE_HEADER_BIT_START; |
| 155 | |
| 156 | decrease_duration(&ev, RC6_BIT_END); |
| 157 | goto again; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 158 | |
| 159 | case STATE_TOGGLE_START: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 160 | if (!eq_margin(ev.duration, RC6_TOGGLE_START, RC6_UNIT / 2)) |
| 161 | break; |
| 162 | |
| 163 | data->toggle = ev.pulse; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 164 | data->state = STATE_TOGGLE_END; |
| 165 | return 0; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 166 | |
| 167 | case STATE_TOGGLE_END: |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 168 | if (!is_transition(&ev, &dev->raw->prev_ev) || |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 169 | !geq_margin(ev.duration, RC6_TOGGLE_END, RC6_UNIT / 2)) |
| 170 | break; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 171 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 172 | if (!(data->header & RC6_STARTBIT_MASK)) { |
| 173 | IR_dprintk(1, "RC6 invalid start bit\n"); |
| 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 | |
| 177 | data->state = STATE_BODY_BIT_START; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 178 | decrease_duration(&ev, RC6_TOGGLE_END); |
| 179 | data->count = 0; |
lawrence rust | db9bc66 | 2011-10-31 08:39:32 -0300 | [diff] [blame] | 180 | data->body = 0; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 181 | |
| 182 | switch (rc6_mode(data)) { |
| 183 | case RC6_MODE_0: |
| 184 | data->wanted_bits = RC6_0_NBITS; |
| 185 | break; |
| 186 | case RC6_MODE_6A: |
lawrence rust | db9bc66 | 2011-10-31 08:39:32 -0300 | [diff] [blame] | 187 | data->wanted_bits = RC6_6A_NBITS; |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 188 | break; |
| 189 | default: |
| 190 | IR_dprintk(1, "RC6 unknown mode\n"); |
| 191 | goto out; |
| 192 | } |
| 193 | goto again; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 194 | |
| 195 | case STATE_BODY_BIT_START: |
lawrence rust | db9bc66 | 2011-10-31 08:39:32 -0300 | [diff] [blame] | 196 | if (eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2)) { |
| 197 | /* Discard LSB's that won't fit in data->body */ |
| 198 | if (data->count++ < CHAR_BIT * sizeof data->body) { |
| 199 | data->body <<= 1; |
| 200 | if (ev.pulse) |
| 201 | data->body |= 1; |
| 202 | } |
| 203 | data->state = STATE_BODY_BIT_END; |
| 204 | return 0; |
| 205 | } else if (RC6_MODE_6A == rc6_mode(data) && !ev.pulse && |
| 206 | geq_margin(ev.duration, RC6_SUFFIX_SPACE, RC6_UNIT / 2)) { |
| 207 | data->state = STATE_FINISHED; |
| 208 | goto again; |
| 209 | } |
| 210 | break; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 211 | |
| 212 | case STATE_BODY_BIT_END: |
David Härdeman | d8b4b58 | 2010-10-29 16:08:23 -0300 | [diff] [blame] | 213 | if (!is_transition(&ev, &dev->raw->prev_ev)) |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 214 | break; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 215 | |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 216 | if (data->count == data->wanted_bits) |
| 217 | data->state = STATE_FINISHED; |
| 218 | else |
| 219 | data->state = STATE_BODY_BIT_START; |
| 220 | |
| 221 | decrease_duration(&ev, RC6_BIT_END); |
| 222 | goto again; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 223 | |
| 224 | case STATE_FINISHED: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 225 | if (ev.pulse) |
| 226 | break; |
| 227 | |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 228 | switch (rc6_mode(data)) { |
| 229 | case RC6_MODE_0: |
lawrence rust | db9bc66 | 2011-10-31 08:39:32 -0300 | [diff] [blame] | 230 | scancode = data->body; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 231 | toggle = data->toggle; |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 232 | protocol = RC_PROTO_RC6_0; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 233 | IR_dprintk(1, "RC6(0) scancode 0x%04x (toggle: %u)\n", |
| 234 | scancode, toggle); |
| 235 | break; |
David Härdeman | 120703f | 2014-04-03 20:31:30 -0300 | [diff] [blame] | 236 | |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 237 | case RC6_MODE_6A: |
lawrence rust | db9bc66 | 2011-10-31 08:39:32 -0300 | [diff] [blame] | 238 | if (data->count > CHAR_BIT * sizeof data->body) { |
| 239 | IR_dprintk(1, "RC6 too many (%u) data bits\n", |
| 240 | data->count); |
| 241 | goto out; |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 242 | } |
| 243 | |
lawrence rust | db9bc66 | 2011-10-31 08:39:32 -0300 | [diff] [blame] | 244 | scancode = data->body; |
David Härdeman | 120703f | 2014-04-03 20:31:30 -0300 | [diff] [blame] | 245 | switch (data->count) { |
| 246 | case 20: |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 247 | protocol = RC_PROTO_RC6_6A_20; |
lawrence rust | db9bc66 | 2011-10-31 08:39:32 -0300 | [diff] [blame] | 248 | toggle = 0; |
David Härdeman | 120703f | 2014-04-03 20:31:30 -0300 | [diff] [blame] | 249 | break; |
| 250 | case 24: |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 251 | protocol = RC_PROTO_RC6_6A_24; |
David Härdeman | 120703f | 2014-04-03 20:31:30 -0300 | [diff] [blame] | 252 | toggle = 0; |
| 253 | break; |
| 254 | case 32: |
| 255 | if ((scancode & RC6_6A_LCC_MASK) == RC6_6A_MCE_CC) { |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 256 | protocol = RC_PROTO_RC6_MCE; |
David Härdeman | 120703f | 2014-04-03 20:31:30 -0300 | [diff] [blame] | 257 | toggle = !!(scancode & RC6_6A_MCE_TOGGLE_MASK); |
David Härdeman | d2a7458 | 2014-11-20 18:09:54 -0300 | [diff] [blame] | 258 | scancode &= ~RC6_6A_MCE_TOGGLE_MASK; |
David Härdeman | 120703f | 2014-04-03 20:31:30 -0300 | [diff] [blame] | 259 | } else { |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 260 | protocol = RC_PROTO_RC6_6A_32; |
David Härdeman | 120703f | 2014-04-03 20:31:30 -0300 | [diff] [blame] | 261 | toggle = 0; |
| 262 | } |
| 263 | break; |
| 264 | default: |
| 265 | IR_dprintk(1, "RC6(6A) unsupported length\n"); |
| 266 | goto out; |
lawrence rust | db9bc66 | 2011-10-31 08:39:32 -0300 | [diff] [blame] | 267 | } |
David Härdeman | 120703f | 2014-04-03 20:31:30 -0300 | [diff] [blame] | 268 | |
| 269 | IR_dprintk(1, "RC6(6A) proto 0x%04x, scancode 0x%08x (toggle: %u)\n", |
| 270 | protocol, scancode, toggle); |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 271 | break; |
| 272 | default: |
| 273 | IR_dprintk(1, "RC6 unknown mode\n"); |
| 274 | goto out; |
| 275 | } |
| 276 | |
David Härdeman | 120703f | 2014-04-03 20:31:30 -0300 | [diff] [blame] | 277 | rc_keydown(dev, protocol, scancode, toggle); |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 278 | data->state = STATE_INACTIVE; |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | out: |
David Härdeman | e40b112 | 2010-04-15 18:46:00 -0300 | [diff] [blame] | 283 | IR_dprintk(1, "RC6 decode failed at state %i (%uus %s)\n", |
| 284 | data->state, TO_US(ev.duration), TO_STR(ev.pulse)); |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 285 | data->state = STATE_INACTIVE; |
| 286 | return -EINVAL; |
| 287 | } |
| 288 | |
Antti Seppälä | 9d974e4 | 2015-03-31 14:48:09 -0300 | [diff] [blame] | 289 | static const struct ir_raw_timings_manchester ir_rc6_timings[4] = { |
| 290 | { |
Sean Young | ddf9c1b | 2018-01-05 08:26:51 -0500 | [diff] [blame] | 291 | .leader_pulse = RC6_PREFIX_PULSE, |
| 292 | .leader_space = RC6_PREFIX_SPACE, |
Antti Seppälä | 9d974e4 | 2015-03-31 14:48:09 -0300 | [diff] [blame] | 293 | .clock = RC6_UNIT, |
| 294 | .invert = 1, |
| 295 | }, |
| 296 | { |
| 297 | .clock = RC6_UNIT * 2, |
| 298 | .invert = 1, |
| 299 | }, |
| 300 | { |
| 301 | .clock = RC6_UNIT, |
| 302 | .invert = 1, |
| 303 | .trailer_space = RC6_SUFFIX_SPACE, |
| 304 | }, |
| 305 | }; |
| 306 | |
| 307 | /** |
| 308 | * ir_rc6_encode() - Encode a scancode as a stream of raw events |
| 309 | * |
| 310 | * @protocol: protocol to encode |
| 311 | * @scancode: scancode to encode |
| 312 | * @events: array of raw ir events to write into |
| 313 | * @max: maximum size of @events |
| 314 | * |
| 315 | * Returns: The number of events written. |
| 316 | * -ENOBUFS if there isn't enough space in the array to fit the |
| 317 | * encoding. In this case all @max events will have been written. |
| 318 | * -EINVAL if the scancode is ambiguous or invalid. |
| 319 | */ |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 320 | static int ir_rc6_encode(enum rc_proto protocol, u32 scancode, |
Antti Seppälä | 9d974e4 | 2015-03-31 14:48:09 -0300 | [diff] [blame] | 321 | struct ir_raw_event *events, unsigned int max) |
| 322 | { |
| 323 | int ret; |
| 324 | struct ir_raw_event *e = events; |
| 325 | |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 326 | if (protocol == RC_PROTO_RC6_0) { |
Antti Seppälä | 9d974e4 | 2015-03-31 14:48:09 -0300 | [diff] [blame] | 327 | /* Modulate the header (Start Bit & Mode-0) */ |
| 328 | ret = ir_raw_gen_manchester(&e, max - (e - events), |
Sean Young | ddf9c1b | 2018-01-05 08:26:51 -0500 | [diff] [blame] | 329 | &ir_rc6_timings[0], |
Sean Young | 80008dd | 2018-01-05 08:38:43 -0500 | [diff] [blame] | 330 | RC6_HEADER_NBITS, (1 << 3)); |
Antti Seppälä | 9d974e4 | 2015-03-31 14:48:09 -0300 | [diff] [blame] | 331 | if (ret < 0) |
| 332 | return ret; |
| 333 | |
| 334 | /* Modulate Trailer Bit */ |
| 335 | ret = ir_raw_gen_manchester(&e, max - (e - events), |
Sean Young | ddf9c1b | 2018-01-05 08:26:51 -0500 | [diff] [blame] | 336 | &ir_rc6_timings[1], 1, 0); |
Antti Seppälä | 9d974e4 | 2015-03-31 14:48:09 -0300 | [diff] [blame] | 337 | if (ret < 0) |
| 338 | return ret; |
| 339 | |
| 340 | /* Modulate rest of the data */ |
| 341 | ret = ir_raw_gen_manchester(&e, max - (e - events), |
Sean Young | ddf9c1b | 2018-01-05 08:26:51 -0500 | [diff] [blame] | 342 | &ir_rc6_timings[2], RC6_0_NBITS, |
Antti Seppälä | 9d974e4 | 2015-03-31 14:48:09 -0300 | [diff] [blame] | 343 | scancode); |
| 344 | if (ret < 0) |
| 345 | return ret; |
| 346 | |
| 347 | } else { |
| 348 | int bits; |
| 349 | |
| 350 | switch (protocol) { |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 351 | case RC_PROTO_RC6_MCE: |
| 352 | case RC_PROTO_RC6_6A_32: |
Antti Seppälä | 9d974e4 | 2015-03-31 14:48:09 -0300 | [diff] [blame] | 353 | bits = 32; |
| 354 | break; |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 355 | case RC_PROTO_RC6_6A_24: |
Antti Seppälä | 9d974e4 | 2015-03-31 14:48:09 -0300 | [diff] [blame] | 356 | bits = 24; |
| 357 | break; |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 358 | case RC_PROTO_RC6_6A_20: |
Antti Seppälä | 9d974e4 | 2015-03-31 14:48:09 -0300 | [diff] [blame] | 359 | bits = 20; |
| 360 | break; |
| 361 | default: |
| 362 | return -EINVAL; |
| 363 | } |
| 364 | |
Antti Seppälä | 9d974e4 | 2015-03-31 14:48:09 -0300 | [diff] [blame] | 365 | /* Modulate the header (Start Bit & Header-version 6 */ |
| 366 | ret = ir_raw_gen_manchester(&e, max - (e - events), |
Sean Young | ddf9c1b | 2018-01-05 08:26:51 -0500 | [diff] [blame] | 367 | &ir_rc6_timings[0], |
Sean Young | 80008dd | 2018-01-05 08:38:43 -0500 | [diff] [blame] | 368 | RC6_HEADER_NBITS, (1 << 3 | 6)); |
Antti Seppälä | 9d974e4 | 2015-03-31 14:48:09 -0300 | [diff] [blame] | 369 | if (ret < 0) |
| 370 | return ret; |
| 371 | |
| 372 | /* Modulate Trailer Bit */ |
| 373 | ret = ir_raw_gen_manchester(&e, max - (e - events), |
Sean Young | ddf9c1b | 2018-01-05 08:26:51 -0500 | [diff] [blame] | 374 | &ir_rc6_timings[1], 1, 0); |
Antti Seppälä | 9d974e4 | 2015-03-31 14:48:09 -0300 | [diff] [blame] | 375 | if (ret < 0) |
| 376 | return ret; |
| 377 | |
| 378 | /* Modulate rest of the data */ |
| 379 | ret = ir_raw_gen_manchester(&e, max - (e - events), |
Sean Young | ddf9c1b | 2018-01-05 08:26:51 -0500 | [diff] [blame] | 380 | &ir_rc6_timings[2], |
Antti Seppälä | 9d974e4 | 2015-03-31 14:48:09 -0300 | [diff] [blame] | 381 | bits, |
| 382 | scancode); |
| 383 | if (ret < 0) |
| 384 | return ret; |
| 385 | } |
| 386 | |
| 387 | return e - events; |
| 388 | } |
| 389 | |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 390 | static struct ir_raw_handler rc6_handler = { |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 391 | .protocols = RC_PROTO_BIT_RC6_0 | RC_PROTO_BIT_RC6_6A_20 | |
| 392 | RC_PROTO_BIT_RC6_6A_24 | RC_PROTO_BIT_RC6_6A_32 | |
| 393 | RC_PROTO_BIT_RC6_MCE, |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 394 | .decode = ir_rc6_decode, |
Antti Seppälä | 9d974e4 | 2015-03-31 14:48:09 -0300 | [diff] [blame] | 395 | .encode = ir_rc6_encode, |
Sean Young | cdfaa01 | 2017-02-25 06:51:30 -0500 | [diff] [blame] | 396 | .carrier = 36000, |
David Härdeman | 784a493 | 2010-04-08 20:04:40 -0300 | [diff] [blame] | 397 | }; |
| 398 | |
| 399 | static int __init ir_rc6_decode_init(void) |
| 400 | { |
| 401 | ir_raw_handler_register(&rc6_handler); |
| 402 | |
| 403 | printk(KERN_INFO "IR RC6 protocol handler initialized\n"); |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | static void __exit ir_rc6_decode_exit(void) |
| 408 | { |
| 409 | ir_raw_handler_unregister(&rc6_handler); |
| 410 | } |
| 411 | |
| 412 | module_init(ir_rc6_decode_init); |
| 413 | module_exit(ir_rc6_decode_exit); |
| 414 | |
| 415 | MODULE_LICENSE("GPL"); |
| 416 | MODULE_AUTHOR("David Härdeman <david@hardeman.nu>"); |
| 417 | MODULE_DESCRIPTION("RC6 IR protocol decoder"); |