1 // SPDX-License-Identifier: GPL-2.0-only
2 /* ir-jvc-decoder.c - handle JVC IR Pulse/Space protocol
4 * Copyright (C) 2010 by David Härdeman <david@hardeman.nu>
7 #include <linux/bitrev.h>
8 #include <linux/module.h>
9 #include "rc-core-priv.h"
11 #define JVC_NBITS 16 /* dev(8) + func(8) */
12 #define JVC_UNIT 525 /* us */
13 #define JVC_HEADER_PULSE (16 * JVC_UNIT) /* lack of header -> repeat */
14 #define JVC_HEADER_SPACE (8 * JVC_UNIT)
15 #define JVC_BIT_PULSE (1 * JVC_UNIT)
16 #define JVC_BIT_0_SPACE (1 * JVC_UNIT)
17 #define JVC_BIT_1_SPACE (3 * JVC_UNIT)
18 #define JVC_TRAILER_PULSE (1 * JVC_UNIT)
19 #define JVC_TRAILER_SPACE (35 * JVC_UNIT)
32 * ir_jvc_decode() - Decode one JVC pulse or space
33 * @dev: the struct rc_dev descriptor of the device
34 * @ev: the struct ir_raw_event descriptor of the pulse/space
36 * This function returns -EINVAL if the pulse violates the state machine
38 static int ir_jvc_decode(struct rc_dev
*dev
, struct ir_raw_event ev
)
40 struct jvc_dec
*data
= &dev
->raw
->jvc
;
42 if (!is_timing_event(ev
)) {
44 data
->state
= STATE_INACTIVE
;
48 if (!geq_margin(ev
.duration
, JVC_UNIT
, JVC_UNIT
/ 2))
51 dev_dbg(&dev
->dev
, "JVC decode started at state %d (%uus %s)\n",
52 data
->state
, ev
.duration
, TO_STR(ev
.pulse
));
55 switch (data
->state
) {
61 if (!eq_margin(ev
.duration
, JVC_HEADER_PULSE
, JVC_UNIT
/ 2))
66 data
->toggle
= !data
->toggle
;
67 data
->state
= STATE_HEADER_SPACE
;
70 case STATE_HEADER_SPACE
:
74 if (!eq_margin(ev
.duration
, JVC_HEADER_SPACE
, JVC_UNIT
/ 2))
77 data
->state
= STATE_BIT_PULSE
;
84 if (!eq_margin(ev
.duration
, JVC_BIT_PULSE
, JVC_UNIT
/ 2))
87 data
->state
= STATE_BIT_SPACE
;
95 if (eq_margin(ev
.duration
, JVC_BIT_1_SPACE
, JVC_UNIT
/ 2)) {
97 decrease_duration(&ev
, JVC_BIT_1_SPACE
);
98 } else if (eq_margin(ev
.duration
, JVC_BIT_0_SPACE
, JVC_UNIT
/ 2))
99 decrease_duration(&ev
, JVC_BIT_0_SPACE
);
104 if (data
->count
== JVC_NBITS
)
105 data
->state
= STATE_TRAILER_PULSE
;
107 data
->state
= STATE_BIT_PULSE
;
110 case STATE_TRAILER_PULSE
:
114 if (!eq_margin(ev
.duration
, JVC_TRAILER_PULSE
, JVC_UNIT
/ 2))
117 data
->state
= STATE_TRAILER_SPACE
;
120 case STATE_TRAILER_SPACE
:
124 if (!geq_margin(ev
.duration
, JVC_TRAILER_SPACE
, JVC_UNIT
/ 2))
129 scancode
= (bitrev8((data
->bits
>> 8) & 0xff) << 8) |
130 (bitrev8((data
->bits
>> 0) & 0xff) << 0);
131 dev_dbg(&dev
->dev
, "JVC scancode 0x%04x\n", scancode
);
132 rc_keydown(dev
, RC_PROTO_JVC
, scancode
, data
->toggle
);
134 data
->old_bits
= data
->bits
;
135 } else if (data
->bits
== data
->old_bits
) {
136 dev_dbg(&dev
->dev
, "JVC repeat\n");
139 dev_dbg(&dev
->dev
, "JVC invalid repeat msg\n");
144 data
->state
= STATE_CHECK_REPEAT
;
147 case STATE_CHECK_REPEAT
:
151 if (eq_margin(ev
.duration
, JVC_HEADER_PULSE
, JVC_UNIT
/ 2))
152 data
->state
= STATE_INACTIVE
;
154 data
->state
= STATE_BIT_PULSE
;
159 dev_dbg(&dev
->dev
, "JVC decode failed at state %d (%uus %s)\n",
160 data
->state
, ev
.duration
, TO_STR(ev
.pulse
));
161 data
->state
= STATE_INACTIVE
;
165 static const struct ir_raw_timings_pd ir_jvc_timings
= {
166 .header_pulse
= JVC_HEADER_PULSE
,
167 .header_space
= JVC_HEADER_SPACE
,
168 .bit_pulse
= JVC_BIT_PULSE
,
169 .bit_space
[0] = JVC_BIT_0_SPACE
,
170 .bit_space
[1] = JVC_BIT_1_SPACE
,
171 .trailer_pulse
= JVC_TRAILER_PULSE
,
172 .trailer_space
= JVC_TRAILER_SPACE
,
177 * ir_jvc_encode() - Encode a scancode as a stream of raw events
179 * @protocol: protocol to encode
180 * @scancode: scancode to encode
181 * @events: array of raw ir events to write into
182 * @max: maximum size of @events
184 * Returns: The number of events written.
185 * -ENOBUFS if there isn't enough space in the array to fit the
186 * encoding. In this case all @max events will have been written.
188 static int ir_jvc_encode(enum rc_proto protocol
, u32 scancode
,
189 struct ir_raw_event
*events
, unsigned int max
)
191 struct ir_raw_event
*e
= events
;
193 u32 raw
= (bitrev8((scancode
>> 8) & 0xff) << 8) |
194 (bitrev8((scancode
>> 0) & 0xff) << 0);
196 ret
= ir_raw_gen_pd(&e
, max
, &ir_jvc_timings
, JVC_NBITS
, raw
);
203 static struct ir_raw_handler jvc_handler
= {
204 .protocols
= RC_PROTO_BIT_JVC
,
205 .decode
= ir_jvc_decode
,
206 .encode
= ir_jvc_encode
,
208 .min_timeout
= JVC_TRAILER_SPACE
,
211 static int __init
ir_jvc_decode_init(void)
213 ir_raw_handler_register(&jvc_handler
);
215 printk(KERN_INFO
"IR JVC protocol handler initialized\n");
219 static void __exit
ir_jvc_decode_exit(void)
221 ir_raw_handler_unregister(&jvc_handler
);
224 module_init(ir_jvc_decode_init
);
225 module_exit(ir_jvc_decode_exit
);
227 MODULE_LICENSE("GPL");
228 MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
229 MODULE_DESCRIPTION("JVC IR protocol decoder");