xref: /linux/drivers/media/rc/ir-jvc-decoder.c (revision c003ab1bedf028db15b0185b683d5c387204e8f5)
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>
167a707b89SPaul Gortmaker #include <linux/module.h>
17f62de675SMauro Carvalho Chehab #include "rc-core-priv.h"
1832cf86f6SMauro Carvalho Chehab 
1932cf86f6SMauro Carvalho Chehab #define JVC_NBITS		16		/* dev(8) + func(8) */
2032cf86f6SMauro Carvalho Chehab #define JVC_UNIT		525000		/* ns */
2132cf86f6SMauro Carvalho Chehab #define JVC_HEADER_PULSE	(16 * JVC_UNIT) /* lack of header -> repeat */
2232cf86f6SMauro Carvalho Chehab #define JVC_HEADER_SPACE	(8  * JVC_UNIT)
2332cf86f6SMauro Carvalho Chehab #define JVC_BIT_PULSE		(1  * JVC_UNIT)
2432cf86f6SMauro Carvalho Chehab #define JVC_BIT_0_SPACE		(1  * JVC_UNIT)
2532cf86f6SMauro Carvalho Chehab #define JVC_BIT_1_SPACE		(3  * JVC_UNIT)
2632cf86f6SMauro Carvalho Chehab #define JVC_TRAILER_PULSE	(1  * JVC_UNIT)
2732cf86f6SMauro Carvalho Chehab #define	JVC_TRAILER_SPACE	(35 * JVC_UNIT)
2832cf86f6SMauro Carvalho Chehab 
2932cf86f6SMauro Carvalho Chehab enum jvc_state {
3032cf86f6SMauro Carvalho Chehab 	STATE_INACTIVE,
3132cf86f6SMauro Carvalho Chehab 	STATE_HEADER_SPACE,
3232cf86f6SMauro Carvalho Chehab 	STATE_BIT_PULSE,
3332cf86f6SMauro Carvalho Chehab 	STATE_BIT_SPACE,
3432cf86f6SMauro Carvalho Chehab 	STATE_TRAILER_PULSE,
3532cf86f6SMauro Carvalho Chehab 	STATE_TRAILER_SPACE,
3632cf86f6SMauro Carvalho Chehab 	STATE_CHECK_REPEAT,
3732cf86f6SMauro Carvalho Chehab };
3832cf86f6SMauro Carvalho Chehab 
3932cf86f6SMauro Carvalho Chehab /**
4032cf86f6SMauro Carvalho Chehab  * ir_jvc_decode() - Decode one JVC pulse or space
41d8b4b582SDavid Härdeman  * @dev:	the struct rc_dev descriptor of the device
4232cf86f6SMauro Carvalho Chehab  * @duration:   the struct ir_raw_event descriptor of the pulse/space
4332cf86f6SMauro Carvalho Chehab  *
4432cf86f6SMauro Carvalho Chehab  * This function returns -EINVAL if the pulse violates the state machine
4532cf86f6SMauro Carvalho Chehab  */
46d8b4b582SDavid Härdeman static int ir_jvc_decode(struct rc_dev *dev, struct ir_raw_event ev)
4732cf86f6SMauro Carvalho Chehab {
48d8b4b582SDavid Härdeman 	struct jvc_dec *data = &dev->raw->jvc;
4932cf86f6SMauro Carvalho Chehab 
50*c003ab1bSDavid Härdeman 	if (!(dev->raw->enabled_protocols & RC_BIT_JVC))
5132cf86f6SMauro Carvalho Chehab 		return 0;
5232cf86f6SMauro Carvalho Chehab 
5332cf86f6SMauro Carvalho Chehab 	if (!is_timing_event(ev)) {
5432cf86f6SMauro Carvalho Chehab 		if (ev.reset)
5532cf86f6SMauro Carvalho Chehab 			data->state = STATE_INACTIVE;
5632cf86f6SMauro Carvalho Chehab 		return 0;
5732cf86f6SMauro Carvalho Chehab 	}
5832cf86f6SMauro Carvalho Chehab 
5932cf86f6SMauro Carvalho Chehab 	if (!geq_margin(ev.duration, JVC_UNIT, JVC_UNIT / 2))
6032cf86f6SMauro Carvalho Chehab 		goto out;
6132cf86f6SMauro Carvalho Chehab 
6232cf86f6SMauro Carvalho Chehab 	IR_dprintk(2, "JVC decode started at state %d (%uus %s)\n",
6332cf86f6SMauro Carvalho Chehab 		   data->state, TO_US(ev.duration), TO_STR(ev.pulse));
6432cf86f6SMauro Carvalho Chehab 
6532cf86f6SMauro Carvalho Chehab again:
6632cf86f6SMauro Carvalho Chehab 	switch (data->state) {
6732cf86f6SMauro Carvalho Chehab 
6832cf86f6SMauro Carvalho Chehab 	case STATE_INACTIVE:
6932cf86f6SMauro Carvalho Chehab 		if (!ev.pulse)
7032cf86f6SMauro Carvalho Chehab 			break;
7132cf86f6SMauro Carvalho Chehab 
7232cf86f6SMauro Carvalho Chehab 		if (!eq_margin(ev.duration, JVC_HEADER_PULSE, JVC_UNIT / 2))
7332cf86f6SMauro Carvalho Chehab 			break;
7432cf86f6SMauro Carvalho Chehab 
7532cf86f6SMauro Carvalho Chehab 		data->count = 0;
7632cf86f6SMauro Carvalho Chehab 		data->first = true;
7732cf86f6SMauro Carvalho Chehab 		data->toggle = !data->toggle;
7832cf86f6SMauro Carvalho Chehab 		data->state = STATE_HEADER_SPACE;
7932cf86f6SMauro Carvalho Chehab 		return 0;
8032cf86f6SMauro Carvalho Chehab 
8132cf86f6SMauro Carvalho Chehab 	case STATE_HEADER_SPACE:
8232cf86f6SMauro Carvalho Chehab 		if (ev.pulse)
8332cf86f6SMauro Carvalho Chehab 			break;
8432cf86f6SMauro Carvalho Chehab 
8532cf86f6SMauro Carvalho Chehab 		if (!eq_margin(ev.duration, JVC_HEADER_SPACE, JVC_UNIT / 2))
8632cf86f6SMauro Carvalho Chehab 			break;
8732cf86f6SMauro Carvalho Chehab 
8832cf86f6SMauro Carvalho Chehab 		data->state = STATE_BIT_PULSE;
8932cf86f6SMauro Carvalho Chehab 		return 0;
9032cf86f6SMauro Carvalho Chehab 
9132cf86f6SMauro Carvalho Chehab 	case STATE_BIT_PULSE:
9232cf86f6SMauro Carvalho Chehab 		if (!ev.pulse)
9332cf86f6SMauro Carvalho Chehab 			break;
9432cf86f6SMauro Carvalho Chehab 
9532cf86f6SMauro Carvalho Chehab 		if (!eq_margin(ev.duration, JVC_BIT_PULSE, JVC_UNIT / 2))
9632cf86f6SMauro Carvalho Chehab 			break;
9732cf86f6SMauro Carvalho Chehab 
9832cf86f6SMauro Carvalho Chehab 		data->state = STATE_BIT_SPACE;
9932cf86f6SMauro Carvalho Chehab 		return 0;
10032cf86f6SMauro Carvalho Chehab 
10132cf86f6SMauro Carvalho Chehab 	case STATE_BIT_SPACE:
10232cf86f6SMauro Carvalho Chehab 		if (ev.pulse)
10332cf86f6SMauro Carvalho Chehab 			break;
10432cf86f6SMauro Carvalho Chehab 
10532cf86f6SMauro Carvalho Chehab 		data->bits <<= 1;
10632cf86f6SMauro Carvalho Chehab 		if (eq_margin(ev.duration, JVC_BIT_1_SPACE, JVC_UNIT / 2)) {
10732cf86f6SMauro Carvalho Chehab 			data->bits |= 1;
10832cf86f6SMauro Carvalho Chehab 			decrease_duration(&ev, JVC_BIT_1_SPACE);
10932cf86f6SMauro Carvalho Chehab 		} else if (eq_margin(ev.duration, JVC_BIT_0_SPACE, JVC_UNIT / 2))
11032cf86f6SMauro Carvalho Chehab 			decrease_duration(&ev, JVC_BIT_0_SPACE);
11132cf86f6SMauro Carvalho Chehab 		else
11232cf86f6SMauro Carvalho Chehab 			break;
11332cf86f6SMauro Carvalho Chehab 		data->count++;
11432cf86f6SMauro Carvalho Chehab 
11532cf86f6SMauro Carvalho Chehab 		if (data->count == JVC_NBITS)
11632cf86f6SMauro Carvalho Chehab 			data->state = STATE_TRAILER_PULSE;
11732cf86f6SMauro Carvalho Chehab 		else
11832cf86f6SMauro Carvalho Chehab 			data->state = STATE_BIT_PULSE;
11932cf86f6SMauro Carvalho Chehab 		return 0;
12032cf86f6SMauro Carvalho Chehab 
12132cf86f6SMauro Carvalho Chehab 	case STATE_TRAILER_PULSE:
12232cf86f6SMauro Carvalho Chehab 		if (!ev.pulse)
12332cf86f6SMauro Carvalho Chehab 			break;
12432cf86f6SMauro Carvalho Chehab 
12532cf86f6SMauro Carvalho Chehab 		if (!eq_margin(ev.duration, JVC_TRAILER_PULSE, JVC_UNIT / 2))
12632cf86f6SMauro Carvalho Chehab 			break;
12732cf86f6SMauro Carvalho Chehab 
12832cf86f6SMauro Carvalho Chehab 		data->state = STATE_TRAILER_SPACE;
12932cf86f6SMauro Carvalho Chehab 		return 0;
13032cf86f6SMauro Carvalho Chehab 
13132cf86f6SMauro Carvalho Chehab 	case STATE_TRAILER_SPACE:
13232cf86f6SMauro Carvalho Chehab 		if (ev.pulse)
13332cf86f6SMauro Carvalho Chehab 			break;
13432cf86f6SMauro Carvalho Chehab 
13532cf86f6SMauro Carvalho Chehab 		if (!geq_margin(ev.duration, JVC_TRAILER_SPACE, JVC_UNIT / 2))
13632cf86f6SMauro Carvalho Chehab 			break;
13732cf86f6SMauro Carvalho Chehab 
13832cf86f6SMauro Carvalho Chehab 		if (data->first) {
13932cf86f6SMauro Carvalho Chehab 			u32 scancode;
14032cf86f6SMauro Carvalho Chehab 			scancode = (bitrev8((data->bits >> 8) & 0xff) << 8) |
14132cf86f6SMauro Carvalho Chehab 				   (bitrev8((data->bits >> 0) & 0xff) << 0);
14232cf86f6SMauro Carvalho Chehab 			IR_dprintk(1, "JVC scancode 0x%04x\n", scancode);
143ca86674bSMauro Carvalho Chehab 			rc_keydown(dev, scancode, data->toggle);
14432cf86f6SMauro Carvalho Chehab 			data->first = false;
14532cf86f6SMauro Carvalho Chehab 			data->old_bits = data->bits;
14632cf86f6SMauro Carvalho Chehab 		} else if (data->bits == data->old_bits) {
14732cf86f6SMauro Carvalho Chehab 			IR_dprintk(1, "JVC repeat\n");
148ca86674bSMauro Carvalho Chehab 			rc_repeat(dev);
14932cf86f6SMauro Carvalho Chehab 		} else {
15032cf86f6SMauro Carvalho Chehab 			IR_dprintk(1, "JVC invalid repeat msg\n");
15132cf86f6SMauro Carvalho Chehab 			break;
15232cf86f6SMauro Carvalho Chehab 		}
15332cf86f6SMauro Carvalho Chehab 
15432cf86f6SMauro Carvalho Chehab 		data->count = 0;
15532cf86f6SMauro Carvalho Chehab 		data->state = STATE_CHECK_REPEAT;
15632cf86f6SMauro Carvalho Chehab 		return 0;
15732cf86f6SMauro Carvalho Chehab 
15832cf86f6SMauro Carvalho Chehab 	case STATE_CHECK_REPEAT:
15932cf86f6SMauro Carvalho Chehab 		if (!ev.pulse)
16032cf86f6SMauro Carvalho Chehab 			break;
16132cf86f6SMauro Carvalho Chehab 
16232cf86f6SMauro Carvalho Chehab 		if (eq_margin(ev.duration, JVC_HEADER_PULSE, JVC_UNIT / 2))
16332cf86f6SMauro Carvalho Chehab 			data->state = STATE_INACTIVE;
16432cf86f6SMauro Carvalho Chehab   else
16532cf86f6SMauro Carvalho Chehab 			data->state = STATE_BIT_PULSE;
16632cf86f6SMauro Carvalho Chehab 		goto again;
16732cf86f6SMauro Carvalho Chehab 	}
16832cf86f6SMauro Carvalho Chehab 
16932cf86f6SMauro Carvalho Chehab out:
17032cf86f6SMauro Carvalho Chehab 	IR_dprintk(1, "JVC decode failed at state %d (%uus %s)\n",
17132cf86f6SMauro Carvalho Chehab 		   data->state, TO_US(ev.duration), TO_STR(ev.pulse));
17232cf86f6SMauro Carvalho Chehab 	data->state = STATE_INACTIVE;
17332cf86f6SMauro Carvalho Chehab 	return -EINVAL;
17432cf86f6SMauro Carvalho Chehab }
17532cf86f6SMauro Carvalho Chehab 
17632cf86f6SMauro Carvalho Chehab static struct ir_raw_handler jvc_handler = {
177*c003ab1bSDavid Härdeman 	.protocols	= RC_BIT_JVC,
17832cf86f6SMauro Carvalho Chehab 	.decode		= ir_jvc_decode,
17932cf86f6SMauro Carvalho Chehab };
18032cf86f6SMauro Carvalho Chehab 
18132cf86f6SMauro Carvalho Chehab static int __init ir_jvc_decode_init(void)
18232cf86f6SMauro Carvalho Chehab {
18332cf86f6SMauro Carvalho Chehab 	ir_raw_handler_register(&jvc_handler);
18432cf86f6SMauro Carvalho Chehab 
18532cf86f6SMauro Carvalho Chehab 	printk(KERN_INFO "IR JVC protocol handler initialized\n");
18632cf86f6SMauro Carvalho Chehab 	return 0;
18732cf86f6SMauro Carvalho Chehab }
18832cf86f6SMauro Carvalho Chehab 
18932cf86f6SMauro Carvalho Chehab static void __exit ir_jvc_decode_exit(void)
19032cf86f6SMauro Carvalho Chehab {
19132cf86f6SMauro Carvalho Chehab 	ir_raw_handler_unregister(&jvc_handler);
19232cf86f6SMauro Carvalho Chehab }
19332cf86f6SMauro Carvalho Chehab 
19432cf86f6SMauro Carvalho Chehab module_init(ir_jvc_decode_init);
19532cf86f6SMauro Carvalho Chehab module_exit(ir_jvc_decode_exit);
19632cf86f6SMauro Carvalho Chehab 
19732cf86f6SMauro Carvalho Chehab MODULE_LICENSE("GPL");
19832cf86f6SMauro Carvalho Chehab MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
19932cf86f6SMauro Carvalho Chehab MODULE_DESCRIPTION("JVC IR protocol decoder");
200