1 // SPDX-License-Identifier: GPL-2.0 2 // ir-rc5-decoder.c - decoder for RC5(x) and StreamZap protocols 3 // 4 // Copyright (C) 2010 by Mauro Carvalho Chehab 5 // Copyright (C) 2010 by Jarod Wilson <jarod@redhat.com> 6 7 /* 8 * This decoder handles the 14 bit RC5 protocol, 15 bit "StreamZap" protocol 9 * and 20 bit RC5x protocol. 10 */ 11 12 #include "rc-core-priv.h" 13 #include <linux/module.h> 14 15 #define RC5_NBITS 14 16 #define RC5_SZ_NBITS 15 17 #define RC5X_NBITS 20 18 #define CHECK_RC5X_NBITS 8 19 #define RC5_UNIT 888888 /* ns */ 20 #define RC5_BIT_START (1 * RC5_UNIT) 21 #define RC5_BIT_END (1 * RC5_UNIT) 22 #define RC5X_SPACE (4 * RC5_UNIT) 23 #define RC5_TRAILER (6 * RC5_UNIT) /* In reality, approx 100 */ 24 25 enum rc5_state { 26 STATE_INACTIVE, 27 STATE_BIT_START, 28 STATE_BIT_END, 29 STATE_CHECK_RC5X, 30 STATE_FINISHED, 31 }; 32 33 /** 34 * ir_rc5_decode() - Decode one RC-5 pulse or space 35 * @dev: the struct rc_dev descriptor of the device 36 * @ev: the struct ir_raw_event descriptor of the pulse/space 37 * 38 * This function returns -EINVAL if the pulse violates the state machine 39 */ 40 static int ir_rc5_decode(struct rc_dev *dev, struct ir_raw_event ev) 41 { 42 struct rc5_dec *data = &dev->raw->rc5; 43 u8 toggle; 44 u32 scancode; 45 enum rc_proto protocol; 46 47 if (!is_timing_event(ev)) { 48 if (ev.reset) 49 data->state = STATE_INACTIVE; 50 return 0; 51 } 52 53 if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2)) 54 goto out; 55 56 again: 57 dev_dbg(&dev->dev, "RC5(x/sz) decode started at state %i (%uus %s)\n", 58 data->state, TO_US(ev.duration), TO_STR(ev.pulse)); 59 60 if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2)) 61 return 0; 62 63 switch (data->state) { 64 65 case STATE_INACTIVE: 66 if (!ev.pulse) 67 break; 68 69 data->state = STATE_BIT_START; 70 data->count = 1; 71 decrease_duration(&ev, RC5_BIT_START); 72 goto again; 73 74 case STATE_BIT_START: 75 if (!ev.pulse && geq_margin(ev.duration, RC5_TRAILER, RC5_UNIT / 2)) { 76 data->state = STATE_FINISHED; 77 goto again; 78 } 79 80 if (!eq_margin(ev.duration, RC5_BIT_START, RC5_UNIT / 2)) 81 break; 82 83 data->bits <<= 1; 84 if (!ev.pulse) 85 data->bits |= 1; 86 data->count++; 87 data->state = STATE_BIT_END; 88 return 0; 89 90 case STATE_BIT_END: 91 if (!is_transition(&ev, &dev->raw->prev_ev)) 92 break; 93 94 if (data->count == CHECK_RC5X_NBITS) 95 data->state = STATE_CHECK_RC5X; 96 else 97 data->state = STATE_BIT_START; 98 99 decrease_duration(&ev, RC5_BIT_END); 100 goto again; 101 102 case STATE_CHECK_RC5X: 103 if (!ev.pulse && geq_margin(ev.duration, RC5X_SPACE, RC5_UNIT / 2)) { 104 data->is_rc5x = true; 105 decrease_duration(&ev, RC5X_SPACE); 106 } else 107 data->is_rc5x = false; 108 data->state = STATE_BIT_START; 109 goto again; 110 111 case STATE_FINISHED: 112 if (ev.pulse) 113 break; 114 115 if (data->is_rc5x && data->count == RC5X_NBITS) { 116 /* RC5X */ 117 u8 xdata, command, system; 118 if (!(dev->enabled_protocols & RC_PROTO_BIT_RC5X_20)) { 119 data->state = STATE_INACTIVE; 120 return 0; 121 } 122 xdata = (data->bits & 0x0003F) >> 0; 123 command = (data->bits & 0x00FC0) >> 6; 124 system = (data->bits & 0x1F000) >> 12; 125 toggle = (data->bits & 0x20000) ? 1 : 0; 126 command += (data->bits & 0x40000) ? 0 : 0x40; 127 scancode = system << 16 | command << 8 | xdata; 128 protocol = RC_PROTO_RC5X_20; 129 130 } else if (!data->is_rc5x && data->count == RC5_NBITS) { 131 /* RC5 */ 132 u8 command, system; 133 if (!(dev->enabled_protocols & RC_PROTO_BIT_RC5)) { 134 data->state = STATE_INACTIVE; 135 return 0; 136 } 137 command = (data->bits & 0x0003F) >> 0; 138 system = (data->bits & 0x007C0) >> 6; 139 toggle = (data->bits & 0x00800) ? 1 : 0; 140 command += (data->bits & 0x01000) ? 0 : 0x40; 141 scancode = system << 8 | command; 142 protocol = RC_PROTO_RC5; 143 144 } else if (!data->is_rc5x && data->count == RC5_SZ_NBITS) { 145 /* RC5 StreamZap */ 146 u8 command, system; 147 if (!(dev->enabled_protocols & RC_PROTO_BIT_RC5_SZ)) { 148 data->state = STATE_INACTIVE; 149 return 0; 150 } 151 command = (data->bits & 0x0003F) >> 0; 152 system = (data->bits & 0x02FC0) >> 6; 153 toggle = (data->bits & 0x01000) ? 1 : 0; 154 scancode = system << 6 | command; 155 protocol = RC_PROTO_RC5_SZ; 156 157 } else 158 break; 159 160 dev_dbg(&dev->dev, "RC5(x/sz) scancode 0x%06x (p: %u, t: %u)\n", 161 scancode, protocol, toggle); 162 163 rc_keydown(dev, protocol, scancode, toggle); 164 data->state = STATE_INACTIVE; 165 return 0; 166 } 167 168 out: 169 dev_dbg(&dev->dev, "RC5(x/sz) decode failed at state %i count %d (%uus %s)\n", 170 data->state, data->count, TO_US(ev.duration), TO_STR(ev.pulse)); 171 data->state = STATE_INACTIVE; 172 return -EINVAL; 173 } 174 175 static const struct ir_raw_timings_manchester ir_rc5_timings = { 176 .leader_pulse = RC5_UNIT, 177 .clock = RC5_UNIT, 178 .trailer_space = RC5_UNIT * 10, 179 }; 180 181 static const struct ir_raw_timings_manchester ir_rc5x_timings[2] = { 182 { 183 .leader_pulse = RC5_UNIT, 184 .clock = RC5_UNIT, 185 .trailer_space = RC5X_SPACE, 186 }, 187 { 188 .clock = RC5_UNIT, 189 .trailer_space = RC5_UNIT * 10, 190 }, 191 }; 192 193 static const struct ir_raw_timings_manchester ir_rc5_sz_timings = { 194 .leader_pulse = RC5_UNIT, 195 .clock = RC5_UNIT, 196 .trailer_space = RC5_UNIT * 10, 197 }; 198 199 /** 200 * ir_rc5_encode() - Encode a scancode as a stream of raw events 201 * 202 * @protocol: protocol variant to encode 203 * @scancode: scancode to encode 204 * @events: array of raw ir events to write into 205 * @max: maximum size of @events 206 * 207 * Returns: The number of events written. 208 * -ENOBUFS if there isn't enough space in the array to fit the 209 * encoding. In this case all @max events will have been written. 210 * -EINVAL if the scancode is ambiguous or invalid. 211 */ 212 static int ir_rc5_encode(enum rc_proto protocol, u32 scancode, 213 struct ir_raw_event *events, unsigned int max) 214 { 215 int ret; 216 struct ir_raw_event *e = events; 217 unsigned int data, xdata, command, commandx, system, pre_space_data; 218 219 /* Detect protocol and convert scancode to raw data */ 220 if (protocol == RC_PROTO_RC5) { 221 /* decode scancode */ 222 command = (scancode & 0x003f) >> 0; 223 commandx = (scancode & 0x0040) >> 6; 224 system = (scancode & 0x1f00) >> 8; 225 /* encode data */ 226 data = !commandx << 12 | system << 6 | command; 227 228 /* First bit is encoded by leader_pulse */ 229 ret = ir_raw_gen_manchester(&e, max, &ir_rc5_timings, 230 RC5_NBITS - 1, data); 231 if (ret < 0) 232 return ret; 233 } else if (protocol == RC_PROTO_RC5X_20) { 234 /* decode scancode */ 235 xdata = (scancode & 0x00003f) >> 0; 236 command = (scancode & 0x003f00) >> 8; 237 commandx = !(scancode & 0x004000); 238 system = (scancode & 0x1f0000) >> 16; 239 240 /* encode data */ 241 data = commandx << 18 | system << 12 | command << 6 | xdata; 242 243 /* First bit is encoded by leader_pulse */ 244 pre_space_data = data >> (RC5X_NBITS - CHECK_RC5X_NBITS); 245 ret = ir_raw_gen_manchester(&e, max, &ir_rc5x_timings[0], 246 CHECK_RC5X_NBITS - 1, 247 pre_space_data); 248 if (ret < 0) 249 return ret; 250 ret = ir_raw_gen_manchester(&e, max - (e - events), 251 &ir_rc5x_timings[1], 252 RC5X_NBITS - CHECK_RC5X_NBITS, 253 data); 254 if (ret < 0) 255 return ret; 256 } else if (protocol == RC_PROTO_RC5_SZ) { 257 /* RC5-SZ scancode is raw enough for Manchester as it is */ 258 /* First bit is encoded by leader_pulse */ 259 ret = ir_raw_gen_manchester(&e, max, &ir_rc5_sz_timings, 260 RC5_SZ_NBITS - 1, 261 scancode & 0x2fff); 262 if (ret < 0) 263 return ret; 264 } else { 265 return -EINVAL; 266 } 267 268 return e - events; 269 } 270 271 static struct ir_raw_handler rc5_handler = { 272 .protocols = RC_PROTO_BIT_RC5 | RC_PROTO_BIT_RC5X_20 | 273 RC_PROTO_BIT_RC5_SZ, 274 .decode = ir_rc5_decode, 275 .encode = ir_rc5_encode, 276 .carrier = 36000, 277 }; 278 279 static int __init ir_rc5_decode_init(void) 280 { 281 ir_raw_handler_register(&rc5_handler); 282 283 printk(KERN_INFO "IR RC5(x/sz) protocol handler initialized\n"); 284 return 0; 285 } 286 287 static void __exit ir_rc5_decode_exit(void) 288 { 289 ir_raw_handler_unregister(&rc5_handler); 290 } 291 292 module_init(ir_rc5_decode_init); 293 module_exit(ir_rc5_decode_exit); 294 295 MODULE_LICENSE("GPL v2"); 296 MODULE_AUTHOR("Mauro Carvalho Chehab and Jarod Wilson"); 297 MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)"); 298 MODULE_DESCRIPTION("RC5(x/sz) IR protocol decoder"); 299