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