1 /* ir-jvc-decoder.c - handle JVC IR Pulse/Space protocol
3 * Copyright (C) 2010 by David Härdeman <david@hardeman.nu>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/bitrev.h>
16 #include "rc-core-priv.h"
18 #define JVC_NBITS 16 /* dev(8) + func(8) */
19 #define JVC_UNIT 525000 /* ns */
20 #define JVC_HEADER_PULSE (16 * JVC_UNIT) /* lack of header -> repeat */
21 #define JVC_HEADER_SPACE (8 * JVC_UNIT)
22 #define JVC_BIT_PULSE (1 * JVC_UNIT)
23 #define JVC_BIT_0_SPACE (1 * JVC_UNIT)
24 #define JVC_BIT_1_SPACE (3 * JVC_UNIT)
25 #define JVC_TRAILER_PULSE (1 * JVC_UNIT)
26 #define JVC_TRAILER_SPACE (35 * JVC_UNIT)
39 * ir_jvc_decode() - Decode one JVC pulse or space
40 * @dev: the struct rc_dev descriptor of the device
41 * @duration: the struct ir_raw_event descriptor of the pulse/space
43 * This function returns -EINVAL if the pulse violates the state machine
45 static int ir_jvc_decode(struct rc_dev
*dev
, struct ir_raw_event ev
)
47 struct jvc_dec
*data
= &dev
->raw
->jvc
;
49 if (!(dev
->raw
->enabled_protocols
& RC_TYPE_JVC
))
52 if (!is_timing_event(ev
)) {
54 data
->state
= STATE_INACTIVE
;
58 if (!geq_margin(ev
.duration
, JVC_UNIT
, JVC_UNIT
/ 2))
61 IR_dprintk(2, "JVC decode started at state %d (%uus %s)\n",
62 data
->state
, TO_US(ev
.duration
), TO_STR(ev
.pulse
));
65 switch (data
->state
) {
71 if (!eq_margin(ev
.duration
, JVC_HEADER_PULSE
, JVC_UNIT
/ 2))
76 data
->toggle
= !data
->toggle
;
77 data
->state
= STATE_HEADER_SPACE
;
80 case STATE_HEADER_SPACE
:
84 if (!eq_margin(ev
.duration
, JVC_HEADER_SPACE
, JVC_UNIT
/ 2))
87 data
->state
= STATE_BIT_PULSE
;
94 if (!eq_margin(ev
.duration
, JVC_BIT_PULSE
, JVC_UNIT
/ 2))
97 data
->state
= STATE_BIT_SPACE
;
100 case STATE_BIT_SPACE
:
105 if (eq_margin(ev
.duration
, JVC_BIT_1_SPACE
, JVC_UNIT
/ 2)) {
107 decrease_duration(&ev
, JVC_BIT_1_SPACE
);
108 } else if (eq_margin(ev
.duration
, JVC_BIT_0_SPACE
, JVC_UNIT
/ 2))
109 decrease_duration(&ev
, JVC_BIT_0_SPACE
);
114 if (data
->count
== JVC_NBITS
)
115 data
->state
= STATE_TRAILER_PULSE
;
117 data
->state
= STATE_BIT_PULSE
;
120 case STATE_TRAILER_PULSE
:
124 if (!eq_margin(ev
.duration
, JVC_TRAILER_PULSE
, JVC_UNIT
/ 2))
127 data
->state
= STATE_TRAILER_SPACE
;
130 case STATE_TRAILER_SPACE
:
134 if (!geq_margin(ev
.duration
, JVC_TRAILER_SPACE
, JVC_UNIT
/ 2))
139 scancode
= (bitrev8((data
->bits
>> 8) & 0xff) << 8) |
140 (bitrev8((data
->bits
>> 0) & 0xff) << 0);
141 IR_dprintk(1, "JVC scancode 0x%04x\n", scancode
);
142 rc_keydown(dev
, scancode
, data
->toggle
);
144 data
->old_bits
= data
->bits
;
145 } else if (data
->bits
== data
->old_bits
) {
146 IR_dprintk(1, "JVC repeat\n");
149 IR_dprintk(1, "JVC invalid repeat msg\n");
154 data
->state
= STATE_CHECK_REPEAT
;
157 case STATE_CHECK_REPEAT
:
161 if (eq_margin(ev
.duration
, JVC_HEADER_PULSE
, JVC_UNIT
/ 2))
162 data
->state
= STATE_INACTIVE
;
164 data
->state
= STATE_BIT_PULSE
;
169 IR_dprintk(1, "JVC decode failed at state %d (%uus %s)\n",
170 data
->state
, TO_US(ev
.duration
), TO_STR(ev
.pulse
));
171 data
->state
= STATE_INACTIVE
;
175 static struct ir_raw_handler jvc_handler
= {
176 .protocols
= RC_TYPE_JVC
,
177 .decode
= ir_jvc_decode
,
180 static int __init
ir_jvc_decode_init(void)
182 ir_raw_handler_register(&jvc_handler
);
184 printk(KERN_INFO
"IR JVC protocol handler initialized\n");
188 static void __exit
ir_jvc_decode_exit(void)
190 ir_raw_handler_unregister(&jvc_handler
);
193 module_init(ir_jvc_decode_init
);
194 module_exit(ir_jvc_decode_exit
);
196 MODULE_LICENSE("GPL");
197 MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
198 MODULE_DESCRIPTION("JVC IR protocol decoder");