1 // SPDX-License-Identifier: GPL-2.0-only 2 /* ir-xmp-decoder.c - handle XMP IR Pulse/Space protocol 3 * 4 * Copyright (C) 2014 by Marcel Mol 5 * 6 * - Based on info from http://www.hifi-remote.com 7 * - Ignore Toggle=9 frames 8 * - Ignore XMP-1 XMP-2 difference, always store 16 bit OBC 9 */ 10 11 #include <linux/bitrev.h> 12 #include <linux/module.h> 13 #include "rc-core-priv.h" 14 15 #define XMP_UNIT 136000 /* ns */ 16 #define XMP_LEADER 210000 /* ns */ 17 #define XMP_NIBBLE_PREFIX 760000 /* ns */ 18 #define XMP_HALFFRAME_SPACE 13800000 /* ns */ 19 #define XMP_TRAILER_SPACE 20000000 /* should be 80ms but not all dureation supliers can go that high */ 20 21 enum xmp_state { 22 STATE_INACTIVE, 23 STATE_LEADER_PULSE, 24 STATE_NIBBLE_SPACE, 25 }; 26 27 /** 28 * ir_xmp_decode() - Decode one XMP pulse or space 29 * @dev: the struct rc_dev descriptor of the device 30 * @ev: the struct ir_raw_event descriptor of the pulse/space 31 * 32 * This function returns -EINVAL if the pulse violates the state machine 33 */ 34 static int ir_xmp_decode(struct rc_dev *dev, struct ir_raw_event ev) 35 { 36 struct xmp_dec *data = &dev->raw->xmp; 37 38 if (!is_timing_event(ev)) { 39 if (ev.reset) 40 data->state = STATE_INACTIVE; 41 return 0; 42 } 43 44 dev_dbg(&dev->dev, "XMP decode started at state %d %d (%uus %s)\n", 45 data->state, data->count, TO_US(ev.duration), TO_STR(ev.pulse)); 46 47 switch (data->state) { 48 49 case STATE_INACTIVE: 50 if (!ev.pulse) 51 break; 52 53 if (eq_margin(ev.duration, XMP_LEADER, XMP_UNIT / 2)) { 54 data->count = 0; 55 data->state = STATE_NIBBLE_SPACE; 56 } 57 58 return 0; 59 60 case STATE_LEADER_PULSE: 61 if (!ev.pulse) 62 break; 63 64 if (eq_margin(ev.duration, XMP_LEADER, XMP_UNIT / 2)) 65 data->state = STATE_NIBBLE_SPACE; 66 67 return 0; 68 69 case STATE_NIBBLE_SPACE: 70 if (ev.pulse) 71 break; 72 73 if (geq_margin(ev.duration, XMP_TRAILER_SPACE, XMP_NIBBLE_PREFIX)) { 74 int divider, i; 75 u8 addr, subaddr, subaddr2, toggle, oem, obc1, obc2, sum1, sum2; 76 u32 *n; 77 u32 scancode; 78 79 if (data->count != 16) { 80 dev_dbg(&dev->dev, "received TRAILER period at index %d: %u\n", 81 data->count, ev.duration); 82 data->state = STATE_INACTIVE; 83 return -EINVAL; 84 } 85 86 n = data->durations; 87 /* 88 * the 4th nibble should be 15 so base the divider on this 89 * to transform durations into nibbles. Subtract 2000 from 90 * the divider to compensate for fluctuations in the signal 91 */ 92 divider = (n[3] - XMP_NIBBLE_PREFIX) / 15 - 2000; 93 if (divider < 50) { 94 dev_dbg(&dev->dev, "divider to small %d.\n", 95 divider); 96 data->state = STATE_INACTIVE; 97 return -EINVAL; 98 } 99 100 /* convert to nibbles and do some sanity checks */ 101 for (i = 0; i < 16; i++) 102 n[i] = (n[i] - XMP_NIBBLE_PREFIX) / divider; 103 sum1 = (15 + n[0] + n[1] + n[2] + n[3] + 104 n[4] + n[5] + n[6] + n[7]) % 16; 105 sum2 = (15 + n[8] + n[9] + n[10] + n[11] + 106 n[12] + n[13] + n[14] + n[15]) % 16; 107 108 if (sum1 != 15 || sum2 != 15) { 109 dev_dbg(&dev->dev, "checksum errors sum1=0x%X sum2=0x%X\n", 110 sum1, sum2); 111 data->state = STATE_INACTIVE; 112 return -EINVAL; 113 } 114 115 subaddr = n[0] << 4 | n[2]; 116 subaddr2 = n[8] << 4 | n[11]; 117 oem = n[4] << 4 | n[5]; 118 addr = n[6] << 4 | n[7]; 119 toggle = n[10]; 120 obc1 = n[12] << 4 | n[13]; 121 obc2 = n[14] << 4 | n[15]; 122 if (subaddr != subaddr2) { 123 dev_dbg(&dev->dev, "subaddress nibbles mismatch 0x%02X != 0x%02X\n", 124 subaddr, subaddr2); 125 data->state = STATE_INACTIVE; 126 return -EINVAL; 127 } 128 if (oem != 0x44) 129 dev_dbg(&dev->dev, "Warning: OEM nibbles 0x%02X. Expected 0x44\n", 130 oem); 131 132 scancode = addr << 24 | subaddr << 16 | 133 obc1 << 8 | obc2; 134 dev_dbg(&dev->dev, "XMP scancode 0x%06x\n", scancode); 135 136 if (toggle == 0) { 137 rc_keydown(dev, RC_PROTO_XMP, scancode, 0); 138 } else { 139 rc_repeat(dev); 140 dev_dbg(&dev->dev, "Repeat last key\n"); 141 } 142 data->state = STATE_INACTIVE; 143 144 return 0; 145 146 } else if (geq_margin(ev.duration, XMP_HALFFRAME_SPACE, XMP_NIBBLE_PREFIX)) { 147 /* Expect 8 or 16 nibble pulses. 16 in case of 'final' frame */ 148 if (data->count == 16) { 149 dev_dbg(&dev->dev, "received half frame pulse at index %d. Probably a final frame key-up event: %u\n", 150 data->count, ev.duration); 151 /* 152 * TODO: for now go back to half frame position 153 * so trailer can be found and key press 154 * can be handled. 155 */ 156 data->count = 8; 157 } 158 159 else if (data->count != 8) 160 dev_dbg(&dev->dev, "received half frame pulse at index %d: %u\n", 161 data->count, ev.duration); 162 data->state = STATE_LEADER_PULSE; 163 164 return 0; 165 166 } else if (geq_margin(ev.duration, XMP_NIBBLE_PREFIX, XMP_UNIT)) { 167 /* store nibble raw data, decode after trailer */ 168 if (data->count == 16) { 169 dev_dbg(&dev->dev, "to many pulses (%d) ignoring: %u\n", 170 data->count, ev.duration); 171 data->state = STATE_INACTIVE; 172 return -EINVAL; 173 } 174 data->durations[data->count] = ev.duration; 175 data->count++; 176 data->state = STATE_LEADER_PULSE; 177 178 return 0; 179 180 } 181 182 break; 183 } 184 185 dev_dbg(&dev->dev, "XMP decode failed at count %d state %d (%uus %s)\n", 186 data->count, data->state, TO_US(ev.duration), TO_STR(ev.pulse)); 187 data->state = STATE_INACTIVE; 188 return -EINVAL; 189 } 190 191 static struct ir_raw_handler xmp_handler = { 192 .protocols = RC_PROTO_BIT_XMP, 193 .decode = ir_xmp_decode, 194 .min_timeout = XMP_TRAILER_SPACE, 195 }; 196 197 static int __init ir_xmp_decode_init(void) 198 { 199 ir_raw_handler_register(&xmp_handler); 200 201 printk(KERN_INFO "IR XMP protocol handler initialized\n"); 202 return 0; 203 } 204 205 static void __exit ir_xmp_decode_exit(void) 206 { 207 ir_raw_handler_unregister(&xmp_handler); 208 } 209 210 module_init(ir_xmp_decode_init); 211 module_exit(ir_xmp_decode_exit); 212 213 MODULE_LICENSE("GPL"); 214 MODULE_AUTHOR("Marcel Mol <marcel@mesa.nl>"); 215 MODULE_AUTHOR("MESA Consulting (http://www.mesa.nl)"); 216 MODULE_DESCRIPTION("XMP IR protocol decoder"); 217