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 "rc-core-priv.h"
16 #include <linux/module.h>
17 
18 /*
19  * This decoder currently supports:
20  * RC6-0-16	(standard toggle bit in header)
21  * RC6-6A-20	(no toggle bit)
22  * RC6-6A-24	(no toggle bit)
23  * RC6-6A-32	(MCE version with toggle bit in body)
24  */
25 
26 #define RC6_UNIT		444444	/* nanosecs */
27 #define RC6_HEADER_NBITS	4	/* not including toggle bit */
28 #define RC6_0_NBITS		16
29 #define RC6_6A_32_NBITS		32
30 #define RC6_6A_NBITS		128	/* Variable 8..128 */
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)
37 #define RC6_SUFFIX_SPACE	(6 * RC6_UNIT)
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 */
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 #define RC6_6A_ZOTAC_CC		0x80340000 /* Zotac customer code */
44 #define RC6_6A_KATHREIN_CC	0x80460000 /* Kathrein RCU-676 customer code */
45 #ifndef CHAR_BIT
46 #define CHAR_BIT 8	/* Normally in <limits.h> */
47 #endif
48 
49 enum rc6_mode {
50 	RC6_MODE_0,
51 	RC6_MODE_6A,
52 	RC6_MODE_UNKNOWN,
53 };
54 
55 enum rc6_state {
56 	STATE_INACTIVE,
57 	STATE_PREFIX_SPACE,
58 	STATE_HEADER_BIT_START,
59 	STATE_HEADER_BIT_END,
60 	STATE_TOGGLE_START,
61 	STATE_TOGGLE_END,
62 	STATE_BODY_BIT_START,
63 	STATE_BODY_BIT_END,
64 	STATE_FINISHED,
65 };
66 
67 static enum rc6_mode rc6_mode(struct rc6_dec *data)
68 {
69 	switch (data->header & RC6_MODE_MASK) {
70 	case 0:
71 		return RC6_MODE_0;
72 	case 6:
73 		if (!data->toggle)
74 			return RC6_MODE_6A;
75 		/* fall through */
76 	default:
77 		return RC6_MODE_UNKNOWN;
78 	}
79 }
80 
81 /**
82  * ir_rc6_decode() - Decode one RC6 pulse or space
83  * @dev:	the struct rc_dev descriptor of the device
84  * @ev:		the struct ir_raw_event descriptor of the pulse/space
85  *
86  * This function returns -EINVAL if the pulse violates the state machine
87  */
88 static int ir_rc6_decode(struct rc_dev *dev, struct ir_raw_event ev)
89 {
90 	struct rc6_dec *data = &dev->raw->rc6;
91 	u32 scancode;
92 	u8 toggle;
93 	enum rc_proto protocol;
94 
95 	if (!is_timing_event(ev)) {
96 		if (ev.reset)
97 			data->state = STATE_INACTIVE;
98 		return 0;
99 	}
100 
101 	if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
102 		goto out;
103 
104 again:
105 	dev_dbg(&dev->dev, "RC6 decode started at state %i (%uus %s)\n",
106 		data->state, TO_US(ev.duration), TO_STR(ev.pulse));
107 
108 	if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
109 		return 0;
110 
111 	switch (data->state) {
112 
113 	case STATE_INACTIVE:
114 		if (!ev.pulse)
115 			break;
116 
117 		/* Note: larger margin on first pulse since each RC6_UNIT
118 		   is quite short and some hardware takes some time to
119 		   adjust to the signal */
120 		if (!eq_margin(ev.duration, RC6_PREFIX_PULSE, RC6_UNIT))
121 			break;
122 
123 		data->state = STATE_PREFIX_SPACE;
124 		data->count = 0;
125 		return 0;
126 
127 	case STATE_PREFIX_SPACE:
128 		if (ev.pulse)
129 			break;
130 
131 		if (!eq_margin(ev.duration, RC6_PREFIX_SPACE, RC6_UNIT / 2))
132 			break;
133 
134 		data->state = STATE_HEADER_BIT_START;
135 		data->header = 0;
136 		return 0;
137 
138 	case STATE_HEADER_BIT_START:
139 		if (!eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2))
140 			break;
141 
142 		data->header <<= 1;
143 		if (ev.pulse)
144 			data->header |= 1;
145 		data->count++;
146 		data->state = STATE_HEADER_BIT_END;
147 		return 0;
148 
149 	case STATE_HEADER_BIT_END:
150 		if (data->count == RC6_HEADER_NBITS)
151 			data->state = STATE_TOGGLE_START;
152 		else
153 			data->state = STATE_HEADER_BIT_START;
154 
155 		decrease_duration(&ev, RC6_BIT_END);
156 		goto again;
157 
158 	case STATE_TOGGLE_START:
159 		if (!eq_margin(ev.duration, RC6_TOGGLE_START, RC6_UNIT / 2))
160 			break;
161 
162 		data->toggle = ev.pulse;
163 		data->state = STATE_TOGGLE_END;
164 		return 0;
165 
166 	case STATE_TOGGLE_END:
167 		if (!(data->header & RC6_STARTBIT_MASK)) {
168 			dev_dbg(&dev->dev, "RC6 invalid start bit\n");
169 			break;
170 		}
171 
172 		data->state = STATE_BODY_BIT_START;
173 		decrease_duration(&ev, RC6_TOGGLE_END);
174 		data->count = 0;
175 		data->body = 0;
176 
177 		switch (rc6_mode(data)) {
178 		case RC6_MODE_0:
179 			data->wanted_bits = RC6_0_NBITS;
180 			break;
181 		case RC6_MODE_6A:
182 			data->wanted_bits = RC6_6A_NBITS;
183 			break;
184 		default:
185 			dev_dbg(&dev->dev, "RC6 unknown mode\n");
186 			goto out;
187 		}
188 		goto again;
189 
190 	case STATE_BODY_BIT_START:
191 		if (eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2)) {
192 			/* Discard LSB's that won't fit in data->body */
193 			if (data->count++ < CHAR_BIT * sizeof data->body) {
194 				data->body <<= 1;
195 				if (ev.pulse)
196 					data->body |= 1;
197 			}
198 			data->state = STATE_BODY_BIT_END;
199 			return 0;
200 		} else if (RC6_MODE_6A == rc6_mode(data) && !ev.pulse &&
201 				geq_margin(ev.duration, RC6_SUFFIX_SPACE, RC6_UNIT / 2)) {
202 			data->state = STATE_FINISHED;
203 			goto again;
204 		}
205 		break;
206 
207 	case STATE_BODY_BIT_END:
208 		if (data->count == data->wanted_bits)
209 			data->state = STATE_FINISHED;
210 		else
211 			data->state = STATE_BODY_BIT_START;
212 
213 		decrease_duration(&ev, RC6_BIT_END);
214 		goto again;
215 
216 	case STATE_FINISHED:
217 		if (ev.pulse)
218 			break;
219 
220 		switch (rc6_mode(data)) {
221 		case RC6_MODE_0:
222 			scancode = data->body;
223 			toggle = data->toggle;
224 			protocol = RC_PROTO_RC6_0;
225 			dev_dbg(&dev->dev, "RC6(0) scancode 0x%04x (toggle: %u)\n",
226 				scancode, toggle);
227 			break;
228 
229 		case RC6_MODE_6A:
230 			if (data->count > CHAR_BIT * sizeof data->body) {
231 				dev_dbg(&dev->dev, "RC6 too many (%u) data bits\n",
232 					data->count);
233 				goto out;
234 			}
235 
236 			scancode = data->body;
237 			switch (data->count) {
238 			case 20:
239 				protocol = RC_PROTO_RC6_6A_20;
240 				toggle = 0;
241 				break;
242 			case 24:
243 				protocol = RC_PROTO_RC6_6A_24;
244 				toggle = 0;
245 				break;
246 			case 32:
247 				switch (scancode & RC6_6A_LCC_MASK) {
248 				case RC6_6A_MCE_CC:
249 				case RC6_6A_KATHREIN_CC:
250 				case RC6_6A_ZOTAC_CC:
251 					protocol = RC_PROTO_RC6_MCE;
252 					toggle = !!(scancode & RC6_6A_MCE_TOGGLE_MASK);
253 					scancode &= ~RC6_6A_MCE_TOGGLE_MASK;
254 					break;
255 				default:
256 					protocol = RC_PROTO_RC6_6A_32;
257 					toggle = 0;
258 					break;
259 				}
260 				break;
261 			default:
262 				dev_dbg(&dev->dev, "RC6(6A) unsupported length\n");
263 				goto out;
264 			}
265 
266 			dev_dbg(&dev->dev, "RC6(6A) proto 0x%04x, scancode 0x%08x (toggle: %u)\n",
267 				protocol, scancode, toggle);
268 			break;
269 		default:
270 			dev_dbg(&dev->dev, "RC6 unknown mode\n");
271 			goto out;
272 		}
273 
274 		rc_keydown(dev, protocol, scancode, toggle);
275 		data->state = STATE_INACTIVE;
276 		return 0;
277 	}
278 
279 out:
280 	dev_dbg(&dev->dev, "RC6 decode failed at state %i (%uus %s)\n",
281 		data->state, TO_US(ev.duration), TO_STR(ev.pulse));
282 	data->state = STATE_INACTIVE;
283 	return -EINVAL;
284 }
285 
286 static const struct ir_raw_timings_manchester ir_rc6_timings[4] = {
287 	{
288 		.leader_pulse		= RC6_PREFIX_PULSE,
289 		.leader_space		= RC6_PREFIX_SPACE,
290 		.clock			= RC6_UNIT,
291 		.invert			= 1,
292 	},
293 	{
294 		.clock			= RC6_UNIT * 2,
295 		.invert			= 1,
296 	},
297 	{
298 		.clock			= RC6_UNIT,
299 		.invert			= 1,
300 		.trailer_space		= RC6_SUFFIX_SPACE,
301 	},
302 };
303 
304 /**
305  * ir_rc6_encode() - Encode a scancode as a stream of raw events
306  *
307  * @protocol:	protocol to encode
308  * @scancode:	scancode to encode
309  * @events:	array of raw ir events to write into
310  * @max:	maximum size of @events
311  *
312  * Returns:	The number of events written.
313  *		-ENOBUFS if there isn't enough space in the array to fit the
314  *		encoding. In this case all @max events will have been written.
315  *		-EINVAL if the scancode is ambiguous or invalid.
316  */
317 static int ir_rc6_encode(enum rc_proto protocol, u32 scancode,
318 			 struct ir_raw_event *events, unsigned int max)
319 {
320 	int ret;
321 	struct ir_raw_event *e = events;
322 
323 	if (protocol == RC_PROTO_RC6_0) {
324 		/* Modulate the header (Start Bit & Mode-0) */
325 		ret = ir_raw_gen_manchester(&e, max - (e - events),
326 					    &ir_rc6_timings[0],
327 					    RC6_HEADER_NBITS, (1 << 3));
328 		if (ret < 0)
329 			return ret;
330 
331 		/* Modulate Trailer Bit */
332 		ret = ir_raw_gen_manchester(&e, max - (e - events),
333 					    &ir_rc6_timings[1], 1, 0);
334 		if (ret < 0)
335 			return ret;
336 
337 		/* Modulate rest of the data */
338 		ret = ir_raw_gen_manchester(&e, max - (e - events),
339 					    &ir_rc6_timings[2], RC6_0_NBITS,
340 					    scancode);
341 		if (ret < 0)
342 			return ret;
343 
344 	} else {
345 		int bits;
346 
347 		switch (protocol) {
348 		case RC_PROTO_RC6_MCE:
349 		case RC_PROTO_RC6_6A_32:
350 			bits = 32;
351 			break;
352 		case RC_PROTO_RC6_6A_24:
353 			bits = 24;
354 			break;
355 		case RC_PROTO_RC6_6A_20:
356 			bits = 20;
357 			break;
358 		default:
359 			return -EINVAL;
360 		}
361 
362 		/* Modulate the header (Start Bit & Header-version 6 */
363 		ret = ir_raw_gen_manchester(&e, max - (e - events),
364 					    &ir_rc6_timings[0],
365 					    RC6_HEADER_NBITS, (1 << 3 | 6));
366 		if (ret < 0)
367 			return ret;
368 
369 		/* Modulate Trailer Bit */
370 		ret = ir_raw_gen_manchester(&e, max - (e - events),
371 					    &ir_rc6_timings[1], 1, 0);
372 		if (ret < 0)
373 			return ret;
374 
375 		/* Modulate rest of the data */
376 		ret = ir_raw_gen_manchester(&e, max - (e - events),
377 					    &ir_rc6_timings[2],
378 					    bits,
379 					    scancode);
380 		if (ret < 0)
381 			return ret;
382 	}
383 
384 	return e - events;
385 }
386 
387 static struct ir_raw_handler rc6_handler = {
388 	.protocols	= RC_PROTO_BIT_RC6_0 | RC_PROTO_BIT_RC6_6A_20 |
389 			  RC_PROTO_BIT_RC6_6A_24 | RC_PROTO_BIT_RC6_6A_32 |
390 			  RC_PROTO_BIT_RC6_MCE,
391 	.decode		= ir_rc6_decode,
392 	.encode		= ir_rc6_encode,
393 	.carrier	= 36000,
394 	.min_timeout	= RC6_SUFFIX_SPACE,
395 };
396 
397 static int __init ir_rc6_decode_init(void)
398 {
399 	ir_raw_handler_register(&rc6_handler);
400 
401 	printk(KERN_INFO "IR RC6 protocol handler initialized\n");
402 	return 0;
403 }
404 
405 static void __exit ir_rc6_decode_exit(void)
406 {
407 	ir_raw_handler_unregister(&rc6_handler);
408 }
409 
410 module_init(ir_rc6_decode_init);
411 module_exit(ir_rc6_decode_exit);
412 
413 MODULE_LICENSE("GPL");
414 MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
415 MODULE_DESCRIPTION("RC6 IR protocol decoder");
416