xref: /linux/drivers/media/rc/ir-jvc-decoder.c (revision ca86674b8a93ea11c4bb6f4dd0113b1adf1fa841)
132cf86f6SMauro Carvalho Chehab /* ir-jvc-decoder.c - handle JVC IR Pulse/Space protocol
232cf86f6SMauro Carvalho Chehab  *
332cf86f6SMauro Carvalho Chehab  * Copyright (C) 2010 by David Härdeman <david@hardeman.nu>
432cf86f6SMauro Carvalho Chehab  *
532cf86f6SMauro Carvalho Chehab  * This program is free software; you can redistribute it and/or modify
632cf86f6SMauro Carvalho Chehab  * it under the terms of the GNU General Public License as published by
732cf86f6SMauro Carvalho Chehab  * the Free Software Foundation version 2 of the License.
832cf86f6SMauro Carvalho Chehab  *
932cf86f6SMauro Carvalho Chehab  * This program is distributed in the hope that it will be useful,
1032cf86f6SMauro Carvalho Chehab  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1132cf86f6SMauro Carvalho Chehab  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1232cf86f6SMauro Carvalho Chehab  * GNU General Public License for more details.
1332cf86f6SMauro Carvalho Chehab  */
1432cf86f6SMauro Carvalho Chehab 
1532cf86f6SMauro Carvalho Chehab #include <linux/bitrev.h>
16f62de675SMauro Carvalho Chehab #include "rc-core-priv.h"
1732cf86f6SMauro Carvalho Chehab 
1832cf86f6SMauro Carvalho Chehab #define JVC_NBITS		16		/* dev(8) + func(8) */
1932cf86f6SMauro Carvalho Chehab #define JVC_UNIT		525000		/* ns */
2032cf86f6SMauro Carvalho Chehab #define JVC_HEADER_PULSE	(16 * JVC_UNIT) /* lack of header -> repeat */
2132cf86f6SMauro Carvalho Chehab #define JVC_HEADER_SPACE	(8  * JVC_UNIT)
2232cf86f6SMauro Carvalho Chehab #define JVC_BIT_PULSE		(1  * JVC_UNIT)
2332cf86f6SMauro Carvalho Chehab #define JVC_BIT_0_SPACE		(1  * JVC_UNIT)
2432cf86f6SMauro Carvalho Chehab #define JVC_BIT_1_SPACE		(3  * JVC_UNIT)
2532cf86f6SMauro Carvalho Chehab #define JVC_TRAILER_PULSE	(1  * JVC_UNIT)
2632cf86f6SMauro Carvalho Chehab #define	JVC_TRAILER_SPACE	(35 * JVC_UNIT)
2732cf86f6SMauro Carvalho Chehab 
2832cf86f6SMauro Carvalho Chehab enum jvc_state {
2932cf86f6SMauro Carvalho Chehab 	STATE_INACTIVE,
3032cf86f6SMauro Carvalho Chehab 	STATE_HEADER_SPACE,
3132cf86f6SMauro Carvalho Chehab 	STATE_BIT_PULSE,
3232cf86f6SMauro Carvalho Chehab 	STATE_BIT_SPACE,
3332cf86f6SMauro Carvalho Chehab 	STATE_TRAILER_PULSE,
3432cf86f6SMauro Carvalho Chehab 	STATE_TRAILER_SPACE,
3532cf86f6SMauro Carvalho Chehab 	STATE_CHECK_REPEAT,
3632cf86f6SMauro Carvalho Chehab };
3732cf86f6SMauro Carvalho Chehab 
3832cf86f6SMauro Carvalho Chehab /**
3932cf86f6SMauro Carvalho Chehab  * ir_jvc_decode() - Decode one JVC pulse or space
40d8b4b582SDavid Härdeman  * @dev:	the struct rc_dev descriptor of the device
4132cf86f6SMauro Carvalho Chehab  * @duration:   the struct ir_raw_event descriptor of the pulse/space
4232cf86f6SMauro Carvalho Chehab  *
4332cf86f6SMauro Carvalho Chehab  * This function returns -EINVAL if the pulse violates the state machine
4432cf86f6SMauro Carvalho Chehab  */
45d8b4b582SDavid Härdeman static int ir_jvc_decode(struct rc_dev *dev, struct ir_raw_event ev)
4632cf86f6SMauro Carvalho Chehab {
47d8b4b582SDavid Härdeman 	struct jvc_dec *data = &dev->raw->jvc;
4832cf86f6SMauro Carvalho Chehab 
49d8b4b582SDavid Härdeman 	if (!(dev->raw->enabled_protocols & IR_TYPE_JVC))
5032cf86f6SMauro Carvalho Chehab 		return 0;
5132cf86f6SMauro Carvalho Chehab 
5232cf86f6SMauro Carvalho Chehab 	if (!is_timing_event(ev)) {
5332cf86f6SMauro Carvalho Chehab 		if (ev.reset)
5432cf86f6SMauro Carvalho Chehab 			data->state = STATE_INACTIVE;
5532cf86f6SMauro Carvalho Chehab 		return 0;
5632cf86f6SMauro Carvalho Chehab 	}
5732cf86f6SMauro Carvalho Chehab 
5832cf86f6SMauro Carvalho Chehab 	if (!geq_margin(ev.duration, JVC_UNIT, JVC_UNIT / 2))
5932cf86f6SMauro Carvalho Chehab 		goto out;
6032cf86f6SMauro Carvalho Chehab 
6132cf86f6SMauro Carvalho Chehab 	IR_dprintk(2, "JVC decode started at state %d (%uus %s)\n",
6232cf86f6SMauro Carvalho Chehab 		   data->state, TO_US(ev.duration), TO_STR(ev.pulse));
6332cf86f6SMauro Carvalho Chehab 
6432cf86f6SMauro Carvalho Chehab again:
6532cf86f6SMauro Carvalho Chehab 	switch (data->state) {
6632cf86f6SMauro Carvalho Chehab 
6732cf86f6SMauro Carvalho Chehab 	case STATE_INACTIVE:
6832cf86f6SMauro Carvalho Chehab 		if (!ev.pulse)
6932cf86f6SMauro Carvalho Chehab 			break;
7032cf86f6SMauro Carvalho Chehab 
7132cf86f6SMauro Carvalho Chehab 		if (!eq_margin(ev.duration, JVC_HEADER_PULSE, JVC_UNIT / 2))
7232cf86f6SMauro Carvalho Chehab 			break;
7332cf86f6SMauro Carvalho Chehab 
7432cf86f6SMauro Carvalho Chehab 		data->count = 0;
7532cf86f6SMauro Carvalho Chehab 		data->first = true;
7632cf86f6SMauro Carvalho Chehab 		data->toggle = !data->toggle;
7732cf86f6SMauro Carvalho Chehab 		data->state = STATE_HEADER_SPACE;
7832cf86f6SMauro Carvalho Chehab 		return 0;
7932cf86f6SMauro Carvalho Chehab 
8032cf86f6SMauro Carvalho Chehab 	case STATE_HEADER_SPACE:
8132cf86f6SMauro Carvalho Chehab 		if (ev.pulse)
8232cf86f6SMauro Carvalho Chehab 			break;
8332cf86f6SMauro Carvalho Chehab 
8432cf86f6SMauro Carvalho Chehab 		if (!eq_margin(ev.duration, JVC_HEADER_SPACE, JVC_UNIT / 2))
8532cf86f6SMauro Carvalho Chehab 			break;
8632cf86f6SMauro Carvalho Chehab 
8732cf86f6SMauro Carvalho Chehab 		data->state = STATE_BIT_PULSE;
8832cf86f6SMauro Carvalho Chehab 		return 0;
8932cf86f6SMauro Carvalho Chehab 
9032cf86f6SMauro Carvalho Chehab 	case STATE_BIT_PULSE:
9132cf86f6SMauro Carvalho Chehab 		if (!ev.pulse)
9232cf86f6SMauro Carvalho Chehab 			break;
9332cf86f6SMauro Carvalho Chehab 
9432cf86f6SMauro Carvalho Chehab 		if (!eq_margin(ev.duration, JVC_BIT_PULSE, JVC_UNIT / 2))
9532cf86f6SMauro Carvalho Chehab 			break;
9632cf86f6SMauro Carvalho Chehab 
9732cf86f6SMauro Carvalho Chehab 		data->state = STATE_BIT_SPACE;
9832cf86f6SMauro Carvalho Chehab 		return 0;
9932cf86f6SMauro Carvalho Chehab 
10032cf86f6SMauro Carvalho Chehab 	case STATE_BIT_SPACE:
10132cf86f6SMauro Carvalho Chehab 		if (ev.pulse)
10232cf86f6SMauro Carvalho Chehab 			break;
10332cf86f6SMauro Carvalho Chehab 
10432cf86f6SMauro Carvalho Chehab 		data->bits <<= 1;
10532cf86f6SMauro Carvalho Chehab 		if (eq_margin(ev.duration, JVC_BIT_1_SPACE, JVC_UNIT / 2)) {
10632cf86f6SMauro Carvalho Chehab 			data->bits |= 1;
10732cf86f6SMauro Carvalho Chehab 			decrease_duration(&ev, JVC_BIT_1_SPACE);
10832cf86f6SMauro Carvalho Chehab 		} else if (eq_margin(ev.duration, JVC_BIT_0_SPACE, JVC_UNIT / 2))
10932cf86f6SMauro Carvalho Chehab 			decrease_duration(&ev, JVC_BIT_0_SPACE);
11032cf86f6SMauro Carvalho Chehab 		else
11132cf86f6SMauro Carvalho Chehab 			break;
11232cf86f6SMauro Carvalho Chehab 		data->count++;
11332cf86f6SMauro Carvalho Chehab 
11432cf86f6SMauro Carvalho Chehab 		if (data->count == JVC_NBITS)
11532cf86f6SMauro Carvalho Chehab 			data->state = STATE_TRAILER_PULSE;
11632cf86f6SMauro Carvalho Chehab 		else
11732cf86f6SMauro Carvalho Chehab 			data->state = STATE_BIT_PULSE;
11832cf86f6SMauro Carvalho Chehab 		return 0;
11932cf86f6SMauro Carvalho Chehab 
12032cf86f6SMauro Carvalho Chehab 	case STATE_TRAILER_PULSE:
12132cf86f6SMauro Carvalho Chehab 		if (!ev.pulse)
12232cf86f6SMauro Carvalho Chehab 			break;
12332cf86f6SMauro Carvalho Chehab 
12432cf86f6SMauro Carvalho Chehab 		if (!eq_margin(ev.duration, JVC_TRAILER_PULSE, JVC_UNIT / 2))
12532cf86f6SMauro Carvalho Chehab 			break;
12632cf86f6SMauro Carvalho Chehab 
12732cf86f6SMauro Carvalho Chehab 		data->state = STATE_TRAILER_SPACE;
12832cf86f6SMauro Carvalho Chehab 		return 0;
12932cf86f6SMauro Carvalho Chehab 
13032cf86f6SMauro Carvalho Chehab 	case STATE_TRAILER_SPACE:
13132cf86f6SMauro Carvalho Chehab 		if (ev.pulse)
13232cf86f6SMauro Carvalho Chehab 			break;
13332cf86f6SMauro Carvalho Chehab 
13432cf86f6SMauro Carvalho Chehab 		if (!geq_margin(ev.duration, JVC_TRAILER_SPACE, JVC_UNIT / 2))
13532cf86f6SMauro Carvalho Chehab 			break;
13632cf86f6SMauro Carvalho Chehab 
13732cf86f6SMauro Carvalho Chehab 		if (data->first) {
13832cf86f6SMauro Carvalho Chehab 			u32 scancode;
13932cf86f6SMauro Carvalho Chehab 			scancode = (bitrev8((data->bits >> 8) & 0xff) << 8) |
14032cf86f6SMauro Carvalho Chehab 				   (bitrev8((data->bits >> 0) & 0xff) << 0);
14132cf86f6SMauro Carvalho Chehab 			IR_dprintk(1, "JVC scancode 0x%04x\n", scancode);
142*ca86674bSMauro Carvalho Chehab 			rc_keydown(dev, scancode, data->toggle);
14332cf86f6SMauro Carvalho Chehab 			data->first = false;
14432cf86f6SMauro Carvalho Chehab 			data->old_bits = data->bits;
14532cf86f6SMauro Carvalho Chehab 		} else if (data->bits == data->old_bits) {
14632cf86f6SMauro Carvalho Chehab 			IR_dprintk(1, "JVC repeat\n");
147*ca86674bSMauro Carvalho Chehab 			rc_repeat(dev);
14832cf86f6SMauro Carvalho Chehab 		} else {
14932cf86f6SMauro Carvalho Chehab 			IR_dprintk(1, "JVC invalid repeat msg\n");
15032cf86f6SMauro Carvalho Chehab 			break;
15132cf86f6SMauro Carvalho Chehab 		}
15232cf86f6SMauro Carvalho Chehab 
15332cf86f6SMauro Carvalho Chehab 		data->count = 0;
15432cf86f6SMauro Carvalho Chehab 		data->state = STATE_CHECK_REPEAT;
15532cf86f6SMauro Carvalho Chehab 		return 0;
15632cf86f6SMauro Carvalho Chehab 
15732cf86f6SMauro Carvalho Chehab 	case STATE_CHECK_REPEAT:
15832cf86f6SMauro Carvalho Chehab 		if (!ev.pulse)
15932cf86f6SMauro Carvalho Chehab 			break;
16032cf86f6SMauro Carvalho Chehab 
16132cf86f6SMauro Carvalho Chehab 		if (eq_margin(ev.duration, JVC_HEADER_PULSE, JVC_UNIT / 2))
16232cf86f6SMauro Carvalho Chehab 			data->state = STATE_INACTIVE;
16332cf86f6SMauro Carvalho Chehab   else
16432cf86f6SMauro Carvalho Chehab 			data->state = STATE_BIT_PULSE;
16532cf86f6SMauro Carvalho Chehab 		goto again;
16632cf86f6SMauro Carvalho Chehab 	}
16732cf86f6SMauro Carvalho Chehab 
16832cf86f6SMauro Carvalho Chehab out:
16932cf86f6SMauro Carvalho Chehab 	IR_dprintk(1, "JVC decode failed at state %d (%uus %s)\n",
17032cf86f6SMauro Carvalho Chehab 		   data->state, TO_US(ev.duration), TO_STR(ev.pulse));
17132cf86f6SMauro Carvalho Chehab 	data->state = STATE_INACTIVE;
17232cf86f6SMauro Carvalho Chehab 	return -EINVAL;
17332cf86f6SMauro Carvalho Chehab }
17432cf86f6SMauro Carvalho Chehab 
17532cf86f6SMauro Carvalho Chehab static struct ir_raw_handler jvc_handler = {
17632cf86f6SMauro Carvalho Chehab 	.protocols	= IR_TYPE_JVC,
17732cf86f6SMauro Carvalho Chehab 	.decode		= ir_jvc_decode,
17832cf86f6SMauro Carvalho Chehab };
17932cf86f6SMauro Carvalho Chehab 
18032cf86f6SMauro Carvalho Chehab static int __init ir_jvc_decode_init(void)
18132cf86f6SMauro Carvalho Chehab {
18232cf86f6SMauro Carvalho Chehab 	ir_raw_handler_register(&jvc_handler);
18332cf86f6SMauro Carvalho Chehab 
18432cf86f6SMauro Carvalho Chehab 	printk(KERN_INFO "IR JVC protocol handler initialized\n");
18532cf86f6SMauro Carvalho Chehab 	return 0;
18632cf86f6SMauro Carvalho Chehab }
18732cf86f6SMauro Carvalho Chehab 
18832cf86f6SMauro Carvalho Chehab static void __exit ir_jvc_decode_exit(void)
18932cf86f6SMauro Carvalho Chehab {
19032cf86f6SMauro Carvalho Chehab 	ir_raw_handler_unregister(&jvc_handler);
19132cf86f6SMauro Carvalho Chehab }
19232cf86f6SMauro Carvalho Chehab 
19332cf86f6SMauro Carvalho Chehab module_init(ir_jvc_decode_init);
19432cf86f6SMauro Carvalho Chehab module_exit(ir_jvc_decode_exit);
19532cf86f6SMauro Carvalho Chehab 
19632cf86f6SMauro Carvalho Chehab MODULE_LICENSE("GPL");
19732cf86f6SMauro Carvalho Chehab MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
19832cf86f6SMauro Carvalho Chehab MODULE_DESCRIPTION("JVC IR protocol decoder");
199