18e8e69d6SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
232cf86f6SMauro Carvalho Chehab /* ir-jvc-decoder.c - handle JVC IR Pulse/Space protocol
332cf86f6SMauro Carvalho Chehab *
432cf86f6SMauro Carvalho Chehab * Copyright (C) 2010 by David Härdeman <david@hardeman.nu>
532cf86f6SMauro Carvalho Chehab */
632cf86f6SMauro Carvalho Chehab
732cf86f6SMauro Carvalho Chehab #include <linux/bitrev.h>
87a707b89SPaul Gortmaker #include <linux/module.h>
9f62de675SMauro Carvalho Chehab #include "rc-core-priv.h"
1032cf86f6SMauro Carvalho Chehab
1132cf86f6SMauro Carvalho Chehab #define JVC_NBITS 16 /* dev(8) + func(8) */
12528222d8SSean Young #define JVC_UNIT 525 /* us */
1332cf86f6SMauro Carvalho Chehab #define JVC_HEADER_PULSE (16 * JVC_UNIT) /* lack of header -> repeat */
1432cf86f6SMauro Carvalho Chehab #define JVC_HEADER_SPACE (8 * JVC_UNIT)
1532cf86f6SMauro Carvalho Chehab #define JVC_BIT_PULSE (1 * JVC_UNIT)
1632cf86f6SMauro Carvalho Chehab #define JVC_BIT_0_SPACE (1 * JVC_UNIT)
1732cf86f6SMauro Carvalho Chehab #define JVC_BIT_1_SPACE (3 * JVC_UNIT)
1832cf86f6SMauro Carvalho Chehab #define JVC_TRAILER_PULSE (1 * JVC_UNIT)
1932cf86f6SMauro Carvalho Chehab #define JVC_TRAILER_SPACE (35 * JVC_UNIT)
2032cf86f6SMauro Carvalho Chehab
2132cf86f6SMauro Carvalho Chehab enum jvc_state {
2232cf86f6SMauro Carvalho Chehab STATE_INACTIVE,
2332cf86f6SMauro Carvalho Chehab STATE_HEADER_SPACE,
2432cf86f6SMauro Carvalho Chehab STATE_BIT_PULSE,
2532cf86f6SMauro Carvalho Chehab STATE_BIT_SPACE,
2632cf86f6SMauro Carvalho Chehab STATE_TRAILER_PULSE,
2732cf86f6SMauro Carvalho Chehab STATE_TRAILER_SPACE,
2832cf86f6SMauro Carvalho Chehab STATE_CHECK_REPEAT,
2932cf86f6SMauro Carvalho Chehab };
3032cf86f6SMauro Carvalho Chehab
3132cf86f6SMauro Carvalho Chehab /**
3232cf86f6SMauro Carvalho Chehab * ir_jvc_decode() - Decode one JVC pulse or space
33d8b4b582SDavid Härdeman * @dev: the struct rc_dev descriptor of the device
341855e988SMauro Carvalho Chehab * @ev: the struct ir_raw_event descriptor of the pulse/space
3532cf86f6SMauro Carvalho Chehab *
3632cf86f6SMauro Carvalho Chehab * This function returns -EINVAL if the pulse violates the state machine
3732cf86f6SMauro Carvalho Chehab */
ir_jvc_decode(struct rc_dev * dev,struct ir_raw_event ev)38d8b4b582SDavid Härdeman static int ir_jvc_decode(struct rc_dev *dev, struct ir_raw_event ev)
3932cf86f6SMauro Carvalho Chehab {
40d8b4b582SDavid Härdeman struct jvc_dec *data = &dev->raw->jvc;
4132cf86f6SMauro Carvalho Chehab
4232cf86f6SMauro Carvalho Chehab if (!is_timing_event(ev)) {
43*950170d6SSean Young if (ev.overflow)
4432cf86f6SMauro Carvalho Chehab data->state = STATE_INACTIVE;
4532cf86f6SMauro Carvalho Chehab return 0;
4632cf86f6SMauro Carvalho Chehab }
4732cf86f6SMauro Carvalho Chehab
4832cf86f6SMauro Carvalho Chehab if (!geq_margin(ev.duration, JVC_UNIT, JVC_UNIT / 2))
4932cf86f6SMauro Carvalho Chehab goto out;
5032cf86f6SMauro Carvalho Chehab
5150078a90SSean Young dev_dbg(&dev->dev, "JVC decode started at state %d (%uus %s)\n",
52528222d8SSean Young data->state, ev.duration, TO_STR(ev.pulse));
5332cf86f6SMauro Carvalho Chehab
5432cf86f6SMauro Carvalho Chehab again:
5532cf86f6SMauro Carvalho Chehab switch (data->state) {
5632cf86f6SMauro Carvalho Chehab
5732cf86f6SMauro Carvalho Chehab case STATE_INACTIVE:
5832cf86f6SMauro Carvalho Chehab if (!ev.pulse)
5932cf86f6SMauro Carvalho Chehab break;
6032cf86f6SMauro Carvalho Chehab
6132cf86f6SMauro Carvalho Chehab if (!eq_margin(ev.duration, JVC_HEADER_PULSE, JVC_UNIT / 2))
6232cf86f6SMauro Carvalho Chehab break;
6332cf86f6SMauro Carvalho Chehab
6432cf86f6SMauro Carvalho Chehab data->count = 0;
6532cf86f6SMauro Carvalho Chehab data->first = true;
6632cf86f6SMauro Carvalho Chehab data->toggle = !data->toggle;
6732cf86f6SMauro Carvalho Chehab data->state = STATE_HEADER_SPACE;
6832cf86f6SMauro Carvalho Chehab return 0;
6932cf86f6SMauro Carvalho Chehab
7032cf86f6SMauro Carvalho Chehab case STATE_HEADER_SPACE:
7132cf86f6SMauro Carvalho Chehab if (ev.pulse)
7232cf86f6SMauro Carvalho Chehab break;
7332cf86f6SMauro Carvalho Chehab
7432cf86f6SMauro Carvalho Chehab if (!eq_margin(ev.duration, JVC_HEADER_SPACE, JVC_UNIT / 2))
7532cf86f6SMauro Carvalho Chehab break;
7632cf86f6SMauro Carvalho Chehab
7732cf86f6SMauro Carvalho Chehab data->state = STATE_BIT_PULSE;
7832cf86f6SMauro Carvalho Chehab return 0;
7932cf86f6SMauro Carvalho Chehab
8032cf86f6SMauro Carvalho Chehab case STATE_BIT_PULSE:
8132cf86f6SMauro Carvalho Chehab if (!ev.pulse)
8232cf86f6SMauro Carvalho Chehab break;
8332cf86f6SMauro Carvalho Chehab
8432cf86f6SMauro Carvalho Chehab if (!eq_margin(ev.duration, JVC_BIT_PULSE, JVC_UNIT / 2))
8532cf86f6SMauro Carvalho Chehab break;
8632cf86f6SMauro Carvalho Chehab
8732cf86f6SMauro Carvalho Chehab data->state = STATE_BIT_SPACE;
8832cf86f6SMauro Carvalho Chehab return 0;
8932cf86f6SMauro Carvalho Chehab
9032cf86f6SMauro Carvalho Chehab case STATE_BIT_SPACE:
9132cf86f6SMauro Carvalho Chehab if (ev.pulse)
9232cf86f6SMauro Carvalho Chehab break;
9332cf86f6SMauro Carvalho Chehab
9432cf86f6SMauro Carvalho Chehab data->bits <<= 1;
9532cf86f6SMauro Carvalho Chehab if (eq_margin(ev.duration, JVC_BIT_1_SPACE, JVC_UNIT / 2)) {
9632cf86f6SMauro Carvalho Chehab data->bits |= 1;
9732cf86f6SMauro Carvalho Chehab decrease_duration(&ev, JVC_BIT_1_SPACE);
9832cf86f6SMauro Carvalho Chehab } else if (eq_margin(ev.duration, JVC_BIT_0_SPACE, JVC_UNIT / 2))
9932cf86f6SMauro Carvalho Chehab decrease_duration(&ev, JVC_BIT_0_SPACE);
10032cf86f6SMauro Carvalho Chehab else
10132cf86f6SMauro Carvalho Chehab break;
10232cf86f6SMauro Carvalho Chehab data->count++;
10332cf86f6SMauro Carvalho Chehab
10432cf86f6SMauro Carvalho Chehab if (data->count == JVC_NBITS)
10532cf86f6SMauro Carvalho Chehab data->state = STATE_TRAILER_PULSE;
10632cf86f6SMauro Carvalho Chehab else
10732cf86f6SMauro Carvalho Chehab data->state = STATE_BIT_PULSE;
10832cf86f6SMauro Carvalho Chehab return 0;
10932cf86f6SMauro Carvalho Chehab
11032cf86f6SMauro Carvalho Chehab case STATE_TRAILER_PULSE:
11132cf86f6SMauro Carvalho Chehab if (!ev.pulse)
11232cf86f6SMauro Carvalho Chehab break;
11332cf86f6SMauro Carvalho Chehab
11432cf86f6SMauro Carvalho Chehab if (!eq_margin(ev.duration, JVC_TRAILER_PULSE, JVC_UNIT / 2))
11532cf86f6SMauro Carvalho Chehab break;
11632cf86f6SMauro Carvalho Chehab
11732cf86f6SMauro Carvalho Chehab data->state = STATE_TRAILER_SPACE;
11832cf86f6SMauro Carvalho Chehab return 0;
11932cf86f6SMauro Carvalho Chehab
12032cf86f6SMauro Carvalho Chehab case STATE_TRAILER_SPACE:
12132cf86f6SMauro Carvalho Chehab if (ev.pulse)
12232cf86f6SMauro Carvalho Chehab break;
12332cf86f6SMauro Carvalho Chehab
12432cf86f6SMauro Carvalho Chehab if (!geq_margin(ev.duration, JVC_TRAILER_SPACE, JVC_UNIT / 2))
12532cf86f6SMauro Carvalho Chehab break;
12632cf86f6SMauro Carvalho Chehab
12732cf86f6SMauro Carvalho Chehab if (data->first) {
12832cf86f6SMauro Carvalho Chehab u32 scancode;
12932cf86f6SMauro Carvalho Chehab scancode = (bitrev8((data->bits >> 8) & 0xff) << 8) |
13032cf86f6SMauro Carvalho Chehab (bitrev8((data->bits >> 0) & 0xff) << 0);
13150078a90SSean Young dev_dbg(&dev->dev, "JVC scancode 0x%04x\n", scancode);
1326d741bfeSSean Young rc_keydown(dev, RC_PROTO_JVC, scancode, data->toggle);
13332cf86f6SMauro Carvalho Chehab data->first = false;
13432cf86f6SMauro Carvalho Chehab data->old_bits = data->bits;
13532cf86f6SMauro Carvalho Chehab } else if (data->bits == data->old_bits) {
13650078a90SSean Young dev_dbg(&dev->dev, "JVC repeat\n");
137ca86674bSMauro Carvalho Chehab rc_repeat(dev);
13832cf86f6SMauro Carvalho Chehab } else {
13950078a90SSean Young dev_dbg(&dev->dev, "JVC invalid repeat msg\n");
14032cf86f6SMauro Carvalho Chehab break;
14132cf86f6SMauro Carvalho Chehab }
14232cf86f6SMauro Carvalho Chehab
14332cf86f6SMauro Carvalho Chehab data->count = 0;
14432cf86f6SMauro Carvalho Chehab data->state = STATE_CHECK_REPEAT;
14532cf86f6SMauro Carvalho Chehab return 0;
14632cf86f6SMauro Carvalho Chehab
14732cf86f6SMauro Carvalho Chehab case STATE_CHECK_REPEAT:
14832cf86f6SMauro Carvalho Chehab if (!ev.pulse)
14932cf86f6SMauro Carvalho Chehab break;
15032cf86f6SMauro Carvalho Chehab
15132cf86f6SMauro Carvalho Chehab if (eq_margin(ev.duration, JVC_HEADER_PULSE, JVC_UNIT / 2))
15232cf86f6SMauro Carvalho Chehab data->state = STATE_INACTIVE;
15332cf86f6SMauro Carvalho Chehab else
15432cf86f6SMauro Carvalho Chehab data->state = STATE_BIT_PULSE;
15532cf86f6SMauro Carvalho Chehab goto again;
15632cf86f6SMauro Carvalho Chehab }
15732cf86f6SMauro Carvalho Chehab
15832cf86f6SMauro Carvalho Chehab out:
15950078a90SSean Young dev_dbg(&dev->dev, "JVC decode failed at state %d (%uus %s)\n",
160528222d8SSean Young data->state, ev.duration, TO_STR(ev.pulse));
16132cf86f6SMauro Carvalho Chehab data->state = STATE_INACTIVE;
16232cf86f6SMauro Carvalho Chehab return -EINVAL;
16332cf86f6SMauro Carvalho Chehab }
16432cf86f6SMauro Carvalho Chehab
165d8a2ae5cSSean Young static const struct ir_raw_timings_pd ir_jvc_timings = {
166d8a2ae5cSSean Young .header_pulse = JVC_HEADER_PULSE,
167d8a2ae5cSSean Young .header_space = JVC_HEADER_SPACE,
168d8a2ae5cSSean Young .bit_pulse = JVC_BIT_PULSE,
169d8a2ae5cSSean Young .bit_space[0] = JVC_BIT_0_SPACE,
170d8a2ae5cSSean Young .bit_space[1] = JVC_BIT_1_SPACE,
171d8a2ae5cSSean Young .trailer_pulse = JVC_TRAILER_PULSE,
172d8a2ae5cSSean Young .trailer_space = JVC_TRAILER_SPACE,
173d8a2ae5cSSean Young .msb_first = 1,
174d8a2ae5cSSean Young };
175d8a2ae5cSSean Young
176d8a2ae5cSSean Young /**
177d8a2ae5cSSean Young * ir_jvc_encode() - Encode a scancode as a stream of raw events
178d8a2ae5cSSean Young *
179d8a2ae5cSSean Young * @protocol: protocol to encode
180d8a2ae5cSSean Young * @scancode: scancode to encode
181d8a2ae5cSSean Young * @events: array of raw ir events to write into
182d8a2ae5cSSean Young * @max: maximum size of @events
183d8a2ae5cSSean Young *
184d8a2ae5cSSean Young * Returns: The number of events written.
185d8a2ae5cSSean Young * -ENOBUFS if there isn't enough space in the array to fit the
186d8a2ae5cSSean Young * encoding. In this case all @max events will have been written.
187d8a2ae5cSSean Young */
ir_jvc_encode(enum rc_proto protocol,u32 scancode,struct ir_raw_event * events,unsigned int max)1886d741bfeSSean Young static int ir_jvc_encode(enum rc_proto protocol, u32 scancode,
189d8a2ae5cSSean Young struct ir_raw_event *events, unsigned int max)
190d8a2ae5cSSean Young {
191d8a2ae5cSSean Young struct ir_raw_event *e = events;
192d8a2ae5cSSean Young int ret;
193d8a2ae5cSSean Young u32 raw = (bitrev8((scancode >> 8) & 0xff) << 8) |
194d8a2ae5cSSean Young (bitrev8((scancode >> 0) & 0xff) << 0);
195d8a2ae5cSSean Young
196d8a2ae5cSSean Young ret = ir_raw_gen_pd(&e, max, &ir_jvc_timings, JVC_NBITS, raw);
197d8a2ae5cSSean Young if (ret < 0)
198d8a2ae5cSSean Young return ret;
199d8a2ae5cSSean Young
200d8a2ae5cSSean Young return e - events;
201d8a2ae5cSSean Young }
202d8a2ae5cSSean Young
20332cf86f6SMauro Carvalho Chehab static struct ir_raw_handler jvc_handler = {
2046d741bfeSSean Young .protocols = RC_PROTO_BIT_JVC,
20532cf86f6SMauro Carvalho Chehab .decode = ir_jvc_decode,
206d8a2ae5cSSean Young .encode = ir_jvc_encode,
207cdfaa01cSSean Young .carrier = 38000,
208a86d6df8SSean Young .min_timeout = JVC_TRAILER_SPACE,
20932cf86f6SMauro Carvalho Chehab };
21032cf86f6SMauro Carvalho Chehab
ir_jvc_decode_init(void)21132cf86f6SMauro Carvalho Chehab static int __init ir_jvc_decode_init(void)
21232cf86f6SMauro Carvalho Chehab {
21332cf86f6SMauro Carvalho Chehab ir_raw_handler_register(&jvc_handler);
21432cf86f6SMauro Carvalho Chehab
21532cf86f6SMauro Carvalho Chehab printk(KERN_INFO "IR JVC protocol handler initialized\n");
21632cf86f6SMauro Carvalho Chehab return 0;
21732cf86f6SMauro Carvalho Chehab }
21832cf86f6SMauro Carvalho Chehab
ir_jvc_decode_exit(void)21932cf86f6SMauro Carvalho Chehab static void __exit ir_jvc_decode_exit(void)
22032cf86f6SMauro Carvalho Chehab {
22132cf86f6SMauro Carvalho Chehab ir_raw_handler_unregister(&jvc_handler);
22232cf86f6SMauro Carvalho Chehab }
22332cf86f6SMauro Carvalho Chehab
22432cf86f6SMauro Carvalho Chehab module_init(ir_jvc_decode_init);
22532cf86f6SMauro Carvalho Chehab module_exit(ir_jvc_decode_exit);
22632cf86f6SMauro Carvalho Chehab
22732cf86f6SMauro Carvalho Chehab MODULE_LICENSE("GPL");
22832cf86f6SMauro Carvalho Chehab MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
22932cf86f6SMauro Carvalho Chehab MODULE_DESCRIPTION("JVC IR protocol decoder");
230