xref: /linux/drivers/media/rc/ir-jvc-decoder.c (revision 50078a903830796a8a47f26edc4cc10b9061711f)
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
421855e988SMauro Carvalho Chehab  * @ev:   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 
5032cf86f6SMauro Carvalho Chehab 	if (!is_timing_event(ev)) {
5132cf86f6SMauro Carvalho Chehab 		if (ev.reset)
5232cf86f6SMauro Carvalho Chehab 			data->state = STATE_INACTIVE;
5332cf86f6SMauro Carvalho Chehab 		return 0;
5432cf86f6SMauro Carvalho Chehab 	}
5532cf86f6SMauro Carvalho Chehab 
5632cf86f6SMauro Carvalho Chehab 	if (!geq_margin(ev.duration, JVC_UNIT, JVC_UNIT / 2))
5732cf86f6SMauro Carvalho Chehab 		goto out;
5832cf86f6SMauro Carvalho Chehab 
59*50078a90SSean Young 	dev_dbg(&dev->dev, "JVC decode started at state %d (%uus %s)\n",
6032cf86f6SMauro Carvalho Chehab 		data->state, TO_US(ev.duration), TO_STR(ev.pulse));
6132cf86f6SMauro Carvalho Chehab 
6232cf86f6SMauro Carvalho Chehab again:
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 		if (!eq_margin(ev.duration, JVC_HEADER_PULSE, JVC_UNIT / 2))
7032cf86f6SMauro Carvalho Chehab 			break;
7132cf86f6SMauro Carvalho Chehab 
7232cf86f6SMauro Carvalho Chehab 		data->count = 0;
7332cf86f6SMauro Carvalho Chehab 		data->first = true;
7432cf86f6SMauro Carvalho Chehab 		data->toggle = !data->toggle;
7532cf86f6SMauro Carvalho Chehab 		data->state = STATE_HEADER_SPACE;
7632cf86f6SMauro Carvalho Chehab 		return 0;
7732cf86f6SMauro Carvalho Chehab 
7832cf86f6SMauro Carvalho Chehab 	case STATE_HEADER_SPACE:
7932cf86f6SMauro Carvalho Chehab 		if (ev.pulse)
8032cf86f6SMauro Carvalho Chehab 			break;
8132cf86f6SMauro Carvalho Chehab 
8232cf86f6SMauro Carvalho Chehab 		if (!eq_margin(ev.duration, JVC_HEADER_SPACE, JVC_UNIT / 2))
8332cf86f6SMauro Carvalho Chehab 			break;
8432cf86f6SMauro Carvalho Chehab 
8532cf86f6SMauro Carvalho Chehab 		data->state = STATE_BIT_PULSE;
8632cf86f6SMauro Carvalho Chehab 		return 0;
8732cf86f6SMauro Carvalho Chehab 
8832cf86f6SMauro Carvalho Chehab 	case STATE_BIT_PULSE:
8932cf86f6SMauro Carvalho Chehab 		if (!ev.pulse)
9032cf86f6SMauro Carvalho Chehab 			break;
9132cf86f6SMauro Carvalho Chehab 
9232cf86f6SMauro Carvalho Chehab 		if (!eq_margin(ev.duration, JVC_BIT_PULSE, JVC_UNIT / 2))
9332cf86f6SMauro Carvalho Chehab 			break;
9432cf86f6SMauro Carvalho Chehab 
9532cf86f6SMauro Carvalho Chehab 		data->state = STATE_BIT_SPACE;
9632cf86f6SMauro Carvalho Chehab 		return 0;
9732cf86f6SMauro Carvalho Chehab 
9832cf86f6SMauro Carvalho Chehab 	case STATE_BIT_SPACE:
9932cf86f6SMauro Carvalho Chehab 		if (ev.pulse)
10032cf86f6SMauro Carvalho Chehab 			break;
10132cf86f6SMauro Carvalho Chehab 
10232cf86f6SMauro Carvalho Chehab 		data->bits <<= 1;
10332cf86f6SMauro Carvalho Chehab 		if (eq_margin(ev.duration, JVC_BIT_1_SPACE, JVC_UNIT / 2)) {
10432cf86f6SMauro Carvalho Chehab 			data->bits |= 1;
10532cf86f6SMauro Carvalho Chehab 			decrease_duration(&ev, JVC_BIT_1_SPACE);
10632cf86f6SMauro Carvalho Chehab 		} else if (eq_margin(ev.duration, JVC_BIT_0_SPACE, JVC_UNIT / 2))
10732cf86f6SMauro Carvalho Chehab 			decrease_duration(&ev, JVC_BIT_0_SPACE);
10832cf86f6SMauro Carvalho Chehab 		else
10932cf86f6SMauro Carvalho Chehab 			break;
11032cf86f6SMauro Carvalho Chehab 		data->count++;
11132cf86f6SMauro Carvalho Chehab 
11232cf86f6SMauro Carvalho Chehab 		if (data->count == JVC_NBITS)
11332cf86f6SMauro Carvalho Chehab 			data->state = STATE_TRAILER_PULSE;
11432cf86f6SMauro Carvalho Chehab 		else
11532cf86f6SMauro Carvalho Chehab 			data->state = STATE_BIT_PULSE;
11632cf86f6SMauro Carvalho Chehab 		return 0;
11732cf86f6SMauro Carvalho Chehab 
11832cf86f6SMauro Carvalho Chehab 	case STATE_TRAILER_PULSE:
11932cf86f6SMauro Carvalho Chehab 		if (!ev.pulse)
12032cf86f6SMauro Carvalho Chehab 			break;
12132cf86f6SMauro Carvalho Chehab 
12232cf86f6SMauro Carvalho Chehab 		if (!eq_margin(ev.duration, JVC_TRAILER_PULSE, JVC_UNIT / 2))
12332cf86f6SMauro Carvalho Chehab 			break;
12432cf86f6SMauro Carvalho Chehab 
12532cf86f6SMauro Carvalho Chehab 		data->state = STATE_TRAILER_SPACE;
12632cf86f6SMauro Carvalho Chehab 		return 0;
12732cf86f6SMauro Carvalho Chehab 
12832cf86f6SMauro Carvalho Chehab 	case STATE_TRAILER_SPACE:
12932cf86f6SMauro Carvalho Chehab 		if (ev.pulse)
13032cf86f6SMauro Carvalho Chehab 			break;
13132cf86f6SMauro Carvalho Chehab 
13232cf86f6SMauro Carvalho Chehab 		if (!geq_margin(ev.duration, JVC_TRAILER_SPACE, JVC_UNIT / 2))
13332cf86f6SMauro Carvalho Chehab 			break;
13432cf86f6SMauro Carvalho Chehab 
13532cf86f6SMauro Carvalho Chehab 		if (data->first) {
13632cf86f6SMauro Carvalho Chehab 			u32 scancode;
13732cf86f6SMauro Carvalho Chehab 			scancode = (bitrev8((data->bits >> 8) & 0xff) << 8) |
13832cf86f6SMauro Carvalho Chehab 				   (bitrev8((data->bits >> 0) & 0xff) << 0);
139*50078a90SSean Young 			dev_dbg(&dev->dev, "JVC scancode 0x%04x\n", scancode);
1406d741bfeSSean Young 			rc_keydown(dev, RC_PROTO_JVC, scancode, data->toggle);
14132cf86f6SMauro Carvalho Chehab 			data->first = false;
14232cf86f6SMauro Carvalho Chehab 			data->old_bits = data->bits;
14332cf86f6SMauro Carvalho Chehab 		} else if (data->bits == data->old_bits) {
144*50078a90SSean Young 			dev_dbg(&dev->dev, "JVC repeat\n");
145ca86674bSMauro Carvalho Chehab 			rc_repeat(dev);
14632cf86f6SMauro Carvalho Chehab 		} else {
147*50078a90SSean Young 			dev_dbg(&dev->dev, "JVC invalid repeat msg\n");
14832cf86f6SMauro Carvalho Chehab 			break;
14932cf86f6SMauro Carvalho Chehab 		}
15032cf86f6SMauro Carvalho Chehab 
15132cf86f6SMauro Carvalho Chehab 		data->count = 0;
15232cf86f6SMauro Carvalho Chehab 		data->state = STATE_CHECK_REPEAT;
15332cf86f6SMauro Carvalho Chehab 		return 0;
15432cf86f6SMauro Carvalho Chehab 
15532cf86f6SMauro Carvalho Chehab 	case STATE_CHECK_REPEAT:
15632cf86f6SMauro Carvalho Chehab 		if (!ev.pulse)
15732cf86f6SMauro Carvalho Chehab 			break;
15832cf86f6SMauro Carvalho Chehab 
15932cf86f6SMauro Carvalho Chehab 		if (eq_margin(ev.duration, JVC_HEADER_PULSE, JVC_UNIT / 2))
16032cf86f6SMauro Carvalho Chehab 			data->state = STATE_INACTIVE;
16132cf86f6SMauro Carvalho Chehab   else
16232cf86f6SMauro Carvalho Chehab 			data->state = STATE_BIT_PULSE;
16332cf86f6SMauro Carvalho Chehab 		goto again;
16432cf86f6SMauro Carvalho Chehab 	}
16532cf86f6SMauro Carvalho Chehab 
16632cf86f6SMauro Carvalho Chehab out:
167*50078a90SSean Young 	dev_dbg(&dev->dev, "JVC decode failed at state %d (%uus %s)\n",
16832cf86f6SMauro Carvalho Chehab 		data->state, TO_US(ev.duration), TO_STR(ev.pulse));
16932cf86f6SMauro Carvalho Chehab 	data->state = STATE_INACTIVE;
17032cf86f6SMauro Carvalho Chehab 	return -EINVAL;
17132cf86f6SMauro Carvalho Chehab }
17232cf86f6SMauro Carvalho Chehab 
173d8a2ae5cSSean Young static const struct ir_raw_timings_pd ir_jvc_timings = {
174d8a2ae5cSSean Young 	.header_pulse  = JVC_HEADER_PULSE,
175d8a2ae5cSSean Young 	.header_space  = JVC_HEADER_SPACE,
176d8a2ae5cSSean Young 	.bit_pulse     = JVC_BIT_PULSE,
177d8a2ae5cSSean Young 	.bit_space[0]  = JVC_BIT_0_SPACE,
178d8a2ae5cSSean Young 	.bit_space[1]  = JVC_BIT_1_SPACE,
179d8a2ae5cSSean Young 	.trailer_pulse = JVC_TRAILER_PULSE,
180d8a2ae5cSSean Young 	.trailer_space = JVC_TRAILER_SPACE,
181d8a2ae5cSSean Young 	.msb_first     = 1,
182d8a2ae5cSSean Young };
183d8a2ae5cSSean Young 
184d8a2ae5cSSean Young /**
185d8a2ae5cSSean Young  * ir_jvc_encode() - Encode a scancode as a stream of raw events
186d8a2ae5cSSean Young  *
187d8a2ae5cSSean Young  * @protocol:	protocol to encode
188d8a2ae5cSSean Young  * @scancode:	scancode to encode
189d8a2ae5cSSean Young  * @events:	array of raw ir events to write into
190d8a2ae5cSSean Young  * @max:	maximum size of @events
191d8a2ae5cSSean Young  *
192d8a2ae5cSSean Young  * Returns:	The number of events written.
193d8a2ae5cSSean Young  *		-ENOBUFS if there isn't enough space in the array to fit the
194d8a2ae5cSSean Young  *		encoding. In this case all @max events will have been written.
195d8a2ae5cSSean Young  */
1966d741bfeSSean Young static int ir_jvc_encode(enum rc_proto protocol, u32 scancode,
197d8a2ae5cSSean Young 			 struct ir_raw_event *events, unsigned int max)
198d8a2ae5cSSean Young {
199d8a2ae5cSSean Young 	struct ir_raw_event *e = events;
200d8a2ae5cSSean Young 	int ret;
201d8a2ae5cSSean Young 	u32 raw = (bitrev8((scancode >> 8) & 0xff) << 8) |
202d8a2ae5cSSean Young 		  (bitrev8((scancode >> 0) & 0xff) << 0);
203d8a2ae5cSSean Young 
204d8a2ae5cSSean Young 	ret = ir_raw_gen_pd(&e, max, &ir_jvc_timings, JVC_NBITS, raw);
205d8a2ae5cSSean Young 	if (ret < 0)
206d8a2ae5cSSean Young 		return ret;
207d8a2ae5cSSean Young 
208d8a2ae5cSSean Young 	return e - events;
209d8a2ae5cSSean Young }
210d8a2ae5cSSean Young 
21132cf86f6SMauro Carvalho Chehab static struct ir_raw_handler jvc_handler = {
2126d741bfeSSean Young 	.protocols	= RC_PROTO_BIT_JVC,
21332cf86f6SMauro Carvalho Chehab 	.decode		= ir_jvc_decode,
214d8a2ae5cSSean Young 	.encode		= ir_jvc_encode,
215cdfaa01cSSean Young 	.carrier	= 38000,
21632cf86f6SMauro Carvalho Chehab };
21732cf86f6SMauro Carvalho Chehab 
21832cf86f6SMauro Carvalho Chehab static int __init ir_jvc_decode_init(void)
21932cf86f6SMauro Carvalho Chehab {
22032cf86f6SMauro Carvalho Chehab 	ir_raw_handler_register(&jvc_handler);
22132cf86f6SMauro Carvalho Chehab 
22232cf86f6SMauro Carvalho Chehab 	printk(KERN_INFO "IR JVC protocol handler initialized\n");
22332cf86f6SMauro Carvalho Chehab 	return 0;
22432cf86f6SMauro Carvalho Chehab }
22532cf86f6SMauro Carvalho Chehab 
22632cf86f6SMauro Carvalho Chehab static void __exit ir_jvc_decode_exit(void)
22732cf86f6SMauro Carvalho Chehab {
22832cf86f6SMauro Carvalho Chehab 	ir_raw_handler_unregister(&jvc_handler);
22932cf86f6SMauro Carvalho Chehab }
23032cf86f6SMauro Carvalho Chehab 
23132cf86f6SMauro Carvalho Chehab module_init(ir_jvc_decode_init);
23232cf86f6SMauro Carvalho Chehab module_exit(ir_jvc_decode_exit);
23332cf86f6SMauro Carvalho Chehab 
23432cf86f6SMauro Carvalho Chehab MODULE_LICENSE("GPL");
23532cf86f6SMauro Carvalho Chehab MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
23632cf86f6SMauro Carvalho Chehab MODULE_DESCRIPTION("JVC IR protocol decoder");
237