treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / media / rc / ir-jvc-decoder.c
blob864d9e316c33467001ba05b501db53fec3c9628b
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>
5 */
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 525000 /* ns */
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)
21 enum jvc_state {
22 STATE_INACTIVE,
23 STATE_HEADER_SPACE,
24 STATE_BIT_PULSE,
25 STATE_BIT_SPACE,
26 STATE_TRAILER_PULSE,
27 STATE_TRAILER_SPACE,
28 STATE_CHECK_REPEAT,
31 /**
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)) {
43 if (ev.reset)
44 data->state = STATE_INACTIVE;
45 return 0;
48 if (!geq_margin(ev.duration, JVC_UNIT, JVC_UNIT / 2))
49 goto out;
51 dev_dbg(&dev->dev, "JVC decode started at state %d (%uus %s)\n",
52 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
54 again:
55 switch (data->state) {
57 case STATE_INACTIVE:
58 if (!ev.pulse)
59 break;
61 if (!eq_margin(ev.duration, JVC_HEADER_PULSE, JVC_UNIT / 2))
62 break;
64 data->count = 0;
65 data->first = true;
66 data->toggle = !data->toggle;
67 data->state = STATE_HEADER_SPACE;
68 return 0;
70 case STATE_HEADER_SPACE:
71 if (ev.pulse)
72 break;
74 if (!eq_margin(ev.duration, JVC_HEADER_SPACE, JVC_UNIT / 2))
75 break;
77 data->state = STATE_BIT_PULSE;
78 return 0;
80 case STATE_BIT_PULSE:
81 if (!ev.pulse)
82 break;
84 if (!eq_margin(ev.duration, JVC_BIT_PULSE, JVC_UNIT / 2))
85 break;
87 data->state = STATE_BIT_SPACE;
88 return 0;
90 case STATE_BIT_SPACE:
91 if (ev.pulse)
92 break;
94 data->bits <<= 1;
95 if (eq_margin(ev.duration, JVC_BIT_1_SPACE, JVC_UNIT / 2)) {
96 data->bits |= 1;
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);
100 else
101 break;
102 data->count++;
104 if (data->count == JVC_NBITS)
105 data->state = STATE_TRAILER_PULSE;
106 else
107 data->state = STATE_BIT_PULSE;
108 return 0;
110 case STATE_TRAILER_PULSE:
111 if (!ev.pulse)
112 break;
114 if (!eq_margin(ev.duration, JVC_TRAILER_PULSE, JVC_UNIT / 2))
115 break;
117 data->state = STATE_TRAILER_SPACE;
118 return 0;
120 case STATE_TRAILER_SPACE:
121 if (ev.pulse)
122 break;
124 if (!geq_margin(ev.duration, JVC_TRAILER_SPACE, JVC_UNIT / 2))
125 break;
127 if (data->first) {
128 u32 scancode;
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);
133 data->first = false;
134 data->old_bits = data->bits;
135 } else if (data->bits == data->old_bits) {
136 dev_dbg(&dev->dev, "JVC repeat\n");
137 rc_repeat(dev);
138 } else {
139 dev_dbg(&dev->dev, "JVC invalid repeat msg\n");
140 break;
143 data->count = 0;
144 data->state = STATE_CHECK_REPEAT;
145 return 0;
147 case STATE_CHECK_REPEAT:
148 if (!ev.pulse)
149 break;
151 if (eq_margin(ev.duration, JVC_HEADER_PULSE, JVC_UNIT / 2))
152 data->state = STATE_INACTIVE;
153 else
154 data->state = STATE_BIT_PULSE;
155 goto again;
158 out:
159 dev_dbg(&dev->dev, "JVC decode failed at state %d (%uus %s)\n",
160 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
161 data->state = STATE_INACTIVE;
162 return -EINVAL;
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,
173 .msb_first = 1,
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;
192 int ret;
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);
197 if (ret < 0)
198 return ret;
200 return e - events;
203 static struct ir_raw_handler jvc_handler = {
204 .protocols = RC_PROTO_BIT_JVC,
205 .decode = ir_jvc_decode,
206 .encode = ir_jvc_encode,
207 .carrier = 38000,
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");
216 return 0;
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");