xref: /openbmc/linux/drivers/media/rc/ir-rc5-decoder.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
120835280SMauro Carvalho Chehab // SPDX-License-Identifier: GPL-2.0
220835280SMauro Carvalho Chehab // ir-rc5-decoder.c - decoder for RC5(x) and StreamZap protocols
320835280SMauro Carvalho Chehab //
420835280SMauro Carvalho Chehab // Copyright (C) 2010 by Mauro Carvalho Chehab
520835280SMauro Carvalho Chehab // Copyright (C) 2010 by Jarod Wilson <jarod@redhat.com>
632cf86f6SMauro Carvalho Chehab 
732cf86f6SMauro Carvalho Chehab /*
8e87b540bSDavid Härdeman  * This decoder handles the 14 bit RC5 protocol, 15 bit "StreamZap" protocol
9e87b540bSDavid Härdeman  * and 20 bit RC5x protocol.
1032cf86f6SMauro Carvalho Chehab  */
1132cf86f6SMauro Carvalho Chehab 
12f62de675SMauro Carvalho Chehab #include "rc-core-priv.h"
137a707b89SPaul Gortmaker #include <linux/module.h>
1432cf86f6SMauro Carvalho Chehab 
1532cf86f6SMauro Carvalho Chehab #define RC5_NBITS		14
16e87b540bSDavid Härdeman #define RC5_SZ_NBITS		15
1732cf86f6SMauro Carvalho Chehab #define RC5X_NBITS		20
1832cf86f6SMauro Carvalho Chehab #define CHECK_RC5X_NBITS	8
19528222d8SSean Young #define RC5_UNIT		889 /* us */
2032cf86f6SMauro Carvalho Chehab #define RC5_BIT_START		(1 * RC5_UNIT)
2132cf86f6SMauro Carvalho Chehab #define RC5_BIT_END		(1 * RC5_UNIT)
2232cf86f6SMauro Carvalho Chehab #define RC5X_SPACE		(4 * RC5_UNIT)
23bbdb34c9SJonathan McDowell #define RC5_TRAILER		(6 * RC5_UNIT) /* In reality, approx 100 */
2432cf86f6SMauro Carvalho Chehab 
2532cf86f6SMauro Carvalho Chehab enum rc5_state {
2632cf86f6SMauro Carvalho Chehab 	STATE_INACTIVE,
2732cf86f6SMauro Carvalho Chehab 	STATE_BIT_START,
2832cf86f6SMauro Carvalho Chehab 	STATE_BIT_END,
2932cf86f6SMauro Carvalho Chehab 	STATE_CHECK_RC5X,
3032cf86f6SMauro Carvalho Chehab 	STATE_FINISHED,
3132cf86f6SMauro Carvalho Chehab };
3232cf86f6SMauro Carvalho Chehab 
3332cf86f6SMauro Carvalho Chehab /**
3432cf86f6SMauro Carvalho Chehab  * ir_rc5_decode() - Decode one RC-5 pulse or space
35d8b4b582SDavid Härdeman  * @dev:	the struct rc_dev descriptor of the device
3632cf86f6SMauro Carvalho Chehab  * @ev:		the struct ir_raw_event descriptor of the pulse/space
3732cf86f6SMauro Carvalho Chehab  *
3832cf86f6SMauro Carvalho Chehab  * This function returns -EINVAL if the pulse violates the state machine
3932cf86f6SMauro Carvalho Chehab  */
ir_rc5_decode(struct rc_dev * dev,struct ir_raw_event ev)40d8b4b582SDavid Härdeman static int ir_rc5_decode(struct rc_dev *dev, struct ir_raw_event ev)
4132cf86f6SMauro Carvalho Chehab {
42d8b4b582SDavid Härdeman 	struct rc5_dec *data = &dev->raw->rc5;
4332cf86f6SMauro Carvalho Chehab 	u8 toggle;
4432cf86f6SMauro Carvalho Chehab 	u32 scancode;
456d741bfeSSean Young 	enum rc_proto protocol;
4632cf86f6SMauro Carvalho Chehab 
4732cf86f6SMauro Carvalho Chehab 	if (!is_timing_event(ev)) {
48*950170d6SSean Young 		if (ev.overflow)
4932cf86f6SMauro Carvalho Chehab 			data->state = STATE_INACTIVE;
5032cf86f6SMauro Carvalho Chehab 		return 0;
5132cf86f6SMauro Carvalho Chehab 	}
5232cf86f6SMauro Carvalho Chehab 
5332cf86f6SMauro Carvalho Chehab 	if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2))
5432cf86f6SMauro Carvalho Chehab 		goto out;
5532cf86f6SMauro Carvalho Chehab 
5632cf86f6SMauro Carvalho Chehab again:
5750078a90SSean Young 	dev_dbg(&dev->dev, "RC5(x/sz) decode started at state %i (%uus %s)\n",
58528222d8SSean Young 		data->state, ev.duration, TO_STR(ev.pulse));
5932cf86f6SMauro Carvalho Chehab 
6032cf86f6SMauro Carvalho Chehab 	if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2))
6132cf86f6SMauro Carvalho Chehab 		return 0;
6232cf86f6SMauro Carvalho Chehab 
6332cf86f6SMauro Carvalho Chehab 	switch (data->state) {
6432cf86f6SMauro Carvalho Chehab 
6532cf86f6SMauro Carvalho Chehab 	case STATE_INACTIVE:
6632cf86f6SMauro Carvalho Chehab 		if (!ev.pulse)
6732cf86f6SMauro Carvalho Chehab 			break;
6832cf86f6SMauro Carvalho Chehab 
6932cf86f6SMauro Carvalho Chehab 		data->state = STATE_BIT_START;
7032cf86f6SMauro Carvalho Chehab 		data->count = 1;
7132cf86f6SMauro Carvalho Chehab 		decrease_duration(&ev, RC5_BIT_START);
7232cf86f6SMauro Carvalho Chehab 		goto again;
7332cf86f6SMauro Carvalho Chehab 
7432cf86f6SMauro Carvalho Chehab 	case STATE_BIT_START:
75e87b540bSDavid Härdeman 		if (!ev.pulse && geq_margin(ev.duration, RC5_TRAILER, RC5_UNIT / 2)) {
76e87b540bSDavid Härdeman 			data->state = STATE_FINISHED;
77e87b540bSDavid Härdeman 			goto again;
78e87b540bSDavid Härdeman 		}
79e87b540bSDavid Härdeman 
8032cf86f6SMauro Carvalho Chehab 		if (!eq_margin(ev.duration, RC5_BIT_START, RC5_UNIT / 2))
8132cf86f6SMauro Carvalho Chehab 			break;
8232cf86f6SMauro Carvalho Chehab 
8332cf86f6SMauro Carvalho Chehab 		data->bits <<= 1;
8432cf86f6SMauro Carvalho Chehab 		if (!ev.pulse)
8532cf86f6SMauro Carvalho Chehab 			data->bits |= 1;
8632cf86f6SMauro Carvalho Chehab 		data->count++;
8732cf86f6SMauro Carvalho Chehab 		data->state = STATE_BIT_END;
8832cf86f6SMauro Carvalho Chehab 		return 0;
8932cf86f6SMauro Carvalho Chehab 
9032cf86f6SMauro Carvalho Chehab 	case STATE_BIT_END:
91e87b540bSDavid Härdeman 		if (data->count == CHECK_RC5X_NBITS)
9232cf86f6SMauro Carvalho Chehab 			data->state = STATE_CHECK_RC5X;
9332cf86f6SMauro Carvalho Chehab 		else
9432cf86f6SMauro Carvalho Chehab 			data->state = STATE_BIT_START;
9532cf86f6SMauro Carvalho Chehab 
9632cf86f6SMauro Carvalho Chehab 		decrease_duration(&ev, RC5_BIT_END);
9732cf86f6SMauro Carvalho Chehab 		goto again;
9832cf86f6SMauro Carvalho Chehab 
9932cf86f6SMauro Carvalho Chehab 	case STATE_CHECK_RC5X:
10032cf86f6SMauro Carvalho Chehab 		if (!ev.pulse && geq_margin(ev.duration, RC5X_SPACE, RC5_UNIT / 2)) {
101e87b540bSDavid Härdeman 			data->is_rc5x = true;
10232cf86f6SMauro Carvalho Chehab 			decrease_duration(&ev, RC5X_SPACE);
103e87b540bSDavid Härdeman 		} else
104e87b540bSDavid Härdeman 			data->is_rc5x = false;
10532cf86f6SMauro Carvalho Chehab 		data->state = STATE_BIT_START;
10632cf86f6SMauro Carvalho Chehab 		goto again;
10732cf86f6SMauro Carvalho Chehab 
10832cf86f6SMauro Carvalho Chehab 	case STATE_FINISHED:
10932cf86f6SMauro Carvalho Chehab 		if (ev.pulse)
11032cf86f6SMauro Carvalho Chehab 			break;
11132cf86f6SMauro Carvalho Chehab 
112e87b540bSDavid Härdeman 		if (data->is_rc5x && data->count == RC5X_NBITS) {
11332cf86f6SMauro Carvalho Chehab 			/* RC5X */
11432cf86f6SMauro Carvalho Chehab 			u8 xdata, command, system;
1156d741bfeSSean Young 			if (!(dev->enabled_protocols & RC_PROTO_BIT_RC5X_20)) {
116c003ab1bSDavid Härdeman 				data->state = STATE_INACTIVE;
117c003ab1bSDavid Härdeman 				return 0;
118c003ab1bSDavid Härdeman 			}
11932cf86f6SMauro Carvalho Chehab 			xdata    = (data->bits & 0x0003F) >> 0;
12032cf86f6SMauro Carvalho Chehab 			command  = (data->bits & 0x00FC0) >> 6;
12132cf86f6SMauro Carvalho Chehab 			system   = (data->bits & 0x1F000) >> 12;
12232cf86f6SMauro Carvalho Chehab 			toggle   = (data->bits & 0x20000) ? 1 : 0;
123fd844d90SSean Young 			command += (data->bits & 0x40000) ? 0 : 0x40;
12432cf86f6SMauro Carvalho Chehab 			scancode = system << 16 | command << 8 | xdata;
1256d741bfeSSean Young 			protocol = RC_PROTO_RC5X_20;
12632cf86f6SMauro Carvalho Chehab 
127e87b540bSDavid Härdeman 		} else if (!data->is_rc5x && data->count == RC5_NBITS) {
12832cf86f6SMauro Carvalho Chehab 			/* RC5 */
12932cf86f6SMauro Carvalho Chehab 			u8 command, system;
1306d741bfeSSean Young 			if (!(dev->enabled_protocols & RC_PROTO_BIT_RC5)) {
131c003ab1bSDavid Härdeman 				data->state = STATE_INACTIVE;
132c003ab1bSDavid Härdeman 				return 0;
133c003ab1bSDavid Härdeman 			}
13432cf86f6SMauro Carvalho Chehab 			command  = (data->bits & 0x0003F) >> 0;
13532cf86f6SMauro Carvalho Chehab 			system   = (data->bits & 0x007C0) >> 6;
13632cf86f6SMauro Carvalho Chehab 			toggle   = (data->bits & 0x00800) ? 1 : 0;
13732cf86f6SMauro Carvalho Chehab 			command += (data->bits & 0x01000) ? 0 : 0x40;
13832cf86f6SMauro Carvalho Chehab 			scancode = system << 8 | command;
1396d741bfeSSean Young 			protocol = RC_PROTO_RC5;
14032cf86f6SMauro Carvalho Chehab 
141e87b540bSDavid Härdeman 		} else if (!data->is_rc5x && data->count == RC5_SZ_NBITS) {
142e87b540bSDavid Härdeman 			/* RC5 StreamZap */
143e87b540bSDavid Härdeman 			u8 command, system;
1446d741bfeSSean Young 			if (!(dev->enabled_protocols & RC_PROTO_BIT_RC5_SZ)) {
145e87b540bSDavid Härdeman 				data->state = STATE_INACTIVE;
146e87b540bSDavid Härdeman 				return 0;
14732cf86f6SMauro Carvalho Chehab 			}
148e87b540bSDavid Härdeman 			command  = (data->bits & 0x0003F) >> 0;
149e87b540bSDavid Härdeman 			system   = (data->bits & 0x02FC0) >> 6;
150e87b540bSDavid Härdeman 			toggle   = (data->bits & 0x01000) ? 1 : 0;
151e87b540bSDavid Härdeman 			scancode = system << 6 | command;
1526d741bfeSSean Young 			protocol = RC_PROTO_RC5_SZ;
153e87b540bSDavid Härdeman 
154e87b540bSDavid Härdeman 		} else
155e87b540bSDavid Härdeman 			break;
156e87b540bSDavid Härdeman 
15750078a90SSean Young 		dev_dbg(&dev->dev, "RC5(x/sz) scancode 0x%06x (p: %u, t: %u)\n",
158e87b540bSDavid Härdeman 			scancode, protocol, toggle);
15932cf86f6SMauro Carvalho Chehab 
160120703f9SDavid Härdeman 		rc_keydown(dev, protocol, scancode, toggle);
16132cf86f6SMauro Carvalho Chehab 		data->state = STATE_INACTIVE;
16232cf86f6SMauro Carvalho Chehab 		return 0;
16332cf86f6SMauro Carvalho Chehab 	}
16432cf86f6SMauro Carvalho Chehab 
16532cf86f6SMauro Carvalho Chehab out:
16650078a90SSean Young 	dev_dbg(&dev->dev, "RC5(x/sz) decode failed at state %i count %d (%uus %s)\n",
167528222d8SSean Young 		data->state, data->count, ev.duration, TO_STR(ev.pulse));
16832cf86f6SMauro Carvalho Chehab 	data->state = STATE_INACTIVE;
16932cf86f6SMauro Carvalho Chehab 	return -EINVAL;
17032cf86f6SMauro Carvalho Chehab }
17132cf86f6SMauro Carvalho Chehab 
172e9ab364aSJames Hogan static const struct ir_raw_timings_manchester ir_rc5_timings = {
173ddf9c1bbSSean Young 	.leader_pulse		= RC5_UNIT,
174e9ab364aSJames Hogan 	.clock			= RC5_UNIT,
175e9ab364aSJames Hogan 	.trailer_space		= RC5_UNIT * 10,
176e9ab364aSJames Hogan };
177e9ab364aSJames Hogan 
178e9ab364aSJames Hogan static const struct ir_raw_timings_manchester ir_rc5x_timings[2] = {
179e9ab364aSJames Hogan 	{
180ddf9c1bbSSean Young 		.leader_pulse		= RC5_UNIT,
181e9ab364aSJames Hogan 		.clock			= RC5_UNIT,
182e9ab364aSJames Hogan 		.trailer_space		= RC5X_SPACE,
183e9ab364aSJames Hogan 	},
184e9ab364aSJames Hogan 	{
185e9ab364aSJames Hogan 		.clock			= RC5_UNIT,
186e9ab364aSJames Hogan 		.trailer_space		= RC5_UNIT * 10,
187e9ab364aSJames Hogan 	},
188e9ab364aSJames Hogan };
189e9ab364aSJames Hogan 
190e9ab364aSJames Hogan static const struct ir_raw_timings_manchester ir_rc5_sz_timings = {
191ddf9c1bbSSean Young 	.leader_pulse			= RC5_UNIT,
192e9ab364aSJames Hogan 	.clock				= RC5_UNIT,
193e9ab364aSJames Hogan 	.trailer_space			= RC5_UNIT * 10,
194e9ab364aSJames Hogan };
195e9ab364aSJames Hogan 
196e9ab364aSJames Hogan /**
197e9ab364aSJames Hogan  * ir_rc5_encode() - Encode a scancode as a stream of raw events
198e9ab364aSJames Hogan  *
199e9ab364aSJames Hogan  * @protocol:	protocol variant to encode
200e9ab364aSJames Hogan  * @scancode:	scancode to encode
201e9ab364aSJames Hogan  * @events:	array of raw ir events to write into
202e9ab364aSJames Hogan  * @max:	maximum size of @events
203e9ab364aSJames Hogan  *
204e9ab364aSJames Hogan  * Returns:	The number of events written.
205e9ab364aSJames Hogan  *		-ENOBUFS if there isn't enough space in the array to fit the
206e9ab364aSJames Hogan  *		encoding. In this case all @max events will have been written.
207e9ab364aSJames Hogan  *		-EINVAL if the scancode is ambiguous or invalid.
208e9ab364aSJames Hogan  */
ir_rc5_encode(enum rc_proto protocol,u32 scancode,struct ir_raw_event * events,unsigned int max)2096d741bfeSSean Young static int ir_rc5_encode(enum rc_proto protocol, u32 scancode,
210e9ab364aSJames Hogan 			 struct ir_raw_event *events, unsigned int max)
211e9ab364aSJames Hogan {
212e9ab364aSJames Hogan 	int ret;
213e9ab364aSJames Hogan 	struct ir_raw_event *e = events;
214e9ab364aSJames Hogan 	unsigned int data, xdata, command, commandx, system, pre_space_data;
215e9ab364aSJames Hogan 
216e9ab364aSJames Hogan 	/* Detect protocol and convert scancode to raw data */
2176d741bfeSSean Young 	if (protocol == RC_PROTO_RC5) {
218e9ab364aSJames Hogan 		/* decode scancode */
219e9ab364aSJames Hogan 		command  = (scancode & 0x003f) >> 0;
220e9ab364aSJames Hogan 		commandx = (scancode & 0x0040) >> 6;
221e9ab364aSJames Hogan 		system   = (scancode & 0x1f00) >> 8;
222e9ab364aSJames Hogan 		/* encode data */
223e9ab364aSJames Hogan 		data = !commandx << 12 | system << 6 | command;
224e9ab364aSJames Hogan 
22580008ddbSSean Young 		/* First bit is encoded by leader_pulse */
226e9ab364aSJames Hogan 		ret = ir_raw_gen_manchester(&e, max, &ir_rc5_timings,
22780008ddbSSean Young 					    RC5_NBITS - 1, data);
228e9ab364aSJames Hogan 		if (ret < 0)
229e9ab364aSJames Hogan 			return ret;
2306d741bfeSSean Young 	} else if (protocol == RC_PROTO_RC5X_20) {
231e9ab364aSJames Hogan 		/* decode scancode */
232e9ab364aSJames Hogan 		xdata    = (scancode & 0x00003f) >> 0;
233e9ab364aSJames Hogan 		command  = (scancode & 0x003f00) >> 8;
234e9ab364aSJames Hogan 		commandx = !(scancode & 0x004000);
235e9ab364aSJames Hogan 		system   = (scancode & 0x1f0000) >> 16;
236e9ab364aSJames Hogan 
237e9ab364aSJames Hogan 		/* encode data */
238e9ab364aSJames Hogan 		data = commandx << 18 | system << 12 | command << 6 | xdata;
239e9ab364aSJames Hogan 
24080008ddbSSean Young 		/* First bit is encoded by leader_pulse */
241e9ab364aSJames Hogan 		pre_space_data = data >> (RC5X_NBITS - CHECK_RC5X_NBITS);
242e9ab364aSJames Hogan 		ret = ir_raw_gen_manchester(&e, max, &ir_rc5x_timings[0],
24380008ddbSSean Young 					    CHECK_RC5X_NBITS - 1,
24480008ddbSSean Young 					    pre_space_data);
245e9ab364aSJames Hogan 		if (ret < 0)
246e9ab364aSJames Hogan 			return ret;
247e9ab364aSJames Hogan 		ret = ir_raw_gen_manchester(&e, max - (e - events),
248e9ab364aSJames Hogan 					    &ir_rc5x_timings[1],
249e9ab364aSJames Hogan 					    RC5X_NBITS - CHECK_RC5X_NBITS,
250e9ab364aSJames Hogan 					    data);
251e9ab364aSJames Hogan 		if (ret < 0)
252e9ab364aSJames Hogan 			return ret;
2536d741bfeSSean Young 	} else if (protocol == RC_PROTO_RC5_SZ) {
254e9ab364aSJames Hogan 		/* RC5-SZ scancode is raw enough for Manchester as it is */
25580008ddbSSean Young 		/* First bit is encoded by leader_pulse */
256e9ab364aSJames Hogan 		ret = ir_raw_gen_manchester(&e, max, &ir_rc5_sz_timings,
25780008ddbSSean Young 					    RC5_SZ_NBITS - 1,
25880008ddbSSean Young 					    scancode & 0x2fff);
259e9ab364aSJames Hogan 		if (ret < 0)
260e9ab364aSJames Hogan 			return ret;
261e9ab364aSJames Hogan 	} else {
262e9ab364aSJames Hogan 		return -EINVAL;
263e9ab364aSJames Hogan 	}
264e9ab364aSJames Hogan 
265e9ab364aSJames Hogan 	return e - events;
266e9ab364aSJames Hogan }
267e9ab364aSJames Hogan 
26832cf86f6SMauro Carvalho Chehab static struct ir_raw_handler rc5_handler = {
2696d741bfeSSean Young 	.protocols	= RC_PROTO_BIT_RC5 | RC_PROTO_BIT_RC5X_20 |
2706d741bfeSSean Young 							RC_PROTO_BIT_RC5_SZ,
27132cf86f6SMauro Carvalho Chehab 	.decode		= ir_rc5_decode,
272e9ab364aSJames Hogan 	.encode		= ir_rc5_encode,
273cdfaa01cSSean Young 	.carrier	= 36000,
274a86d6df8SSean Young 	.min_timeout	= RC5_TRAILER,
27532cf86f6SMauro Carvalho Chehab };
27632cf86f6SMauro Carvalho Chehab 
ir_rc5_decode_init(void)27732cf86f6SMauro Carvalho Chehab static int __init ir_rc5_decode_init(void)
27832cf86f6SMauro Carvalho Chehab {
27932cf86f6SMauro Carvalho Chehab 	ir_raw_handler_register(&rc5_handler);
28032cf86f6SMauro Carvalho Chehab 
281e87b540bSDavid Härdeman 	printk(KERN_INFO "IR RC5(x/sz) protocol handler initialized\n");
28232cf86f6SMauro Carvalho Chehab 	return 0;
28332cf86f6SMauro Carvalho Chehab }
28432cf86f6SMauro Carvalho Chehab 
ir_rc5_decode_exit(void)28532cf86f6SMauro Carvalho Chehab static void __exit ir_rc5_decode_exit(void)
28632cf86f6SMauro Carvalho Chehab {
28732cf86f6SMauro Carvalho Chehab 	ir_raw_handler_unregister(&rc5_handler);
28832cf86f6SMauro Carvalho Chehab }
28932cf86f6SMauro Carvalho Chehab 
29032cf86f6SMauro Carvalho Chehab module_init(ir_rc5_decode_init);
29132cf86f6SMauro Carvalho Chehab module_exit(ir_rc5_decode_exit);
29232cf86f6SMauro Carvalho Chehab 
29320835280SMauro Carvalho Chehab MODULE_LICENSE("GPL v2");
294e87b540bSDavid Härdeman MODULE_AUTHOR("Mauro Carvalho Chehab and Jarod Wilson");
29532cf86f6SMauro Carvalho Chehab MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
296e87b540bSDavid Härdeman MODULE_DESCRIPTION("RC5(x/sz) IR protocol decoder");
297