1 // SPDX-License-Identifier: GPL-2.0-only
2 /* ir-sony-decoder.c - handle Sony 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 SONY_UNIT 600000 /* ns */
12 #define SONY_HEADER_PULSE (4 * SONY_UNIT)
13 #define SONY_HEADER_SPACE (1 * SONY_UNIT)
14 #define SONY_BIT_0_PULSE (1 * SONY_UNIT)
15 #define SONY_BIT_1_PULSE (2 * SONY_UNIT)
16 #define SONY_BIT_SPACE (1 * SONY_UNIT)
17 #define SONY_TRAILER_SPACE (10 * SONY_UNIT) /* minimum */
28 * ir_sony_decode() - Decode one Sony pulse or space
29 * @dev: the struct rc_dev descriptor of the device
30 * @ev: the struct ir_raw_event descriptor of the pulse/space
32 * This function returns -EINVAL if the pulse violates the state machine
34 static int ir_sony_decode(struct rc_dev
*dev
, struct ir_raw_event ev
)
36 struct sony_dec
*data
= &dev
->raw
->sony
;
37 enum rc_proto protocol
;
39 u8 device
, subdevice
, function
;
41 if (!is_timing_event(ev
)) {
43 data
->state
= STATE_INACTIVE
;
47 if (!geq_margin(ev
.duration
, SONY_UNIT
, SONY_UNIT
/ 2))
50 dev_dbg(&dev
->dev
, "Sony decode started at state %d (%uus %s)\n",
51 data
->state
, TO_US(ev
.duration
), TO_STR(ev
.pulse
));
53 switch (data
->state
) {
59 if (!eq_margin(ev
.duration
, SONY_HEADER_PULSE
, SONY_UNIT
/ 2))
63 data
->state
= STATE_HEADER_SPACE
;
66 case STATE_HEADER_SPACE
:
70 if (!eq_margin(ev
.duration
, SONY_HEADER_SPACE
, SONY_UNIT
/ 2))
73 data
->state
= STATE_BIT_PULSE
;
81 if (eq_margin(ev
.duration
, SONY_BIT_1_PULSE
, SONY_UNIT
/ 2))
83 else if (!eq_margin(ev
.duration
, SONY_BIT_0_PULSE
, SONY_UNIT
/ 2))
87 data
->state
= STATE_BIT_SPACE
;
94 if (!geq_margin(ev
.duration
, SONY_BIT_SPACE
, SONY_UNIT
/ 2))
97 decrease_duration(&ev
, SONY_BIT_SPACE
);
99 if (!geq_margin(ev
.duration
, SONY_UNIT
, SONY_UNIT
/ 2)) {
100 data
->state
= STATE_BIT_PULSE
;
104 data
->state
= STATE_FINISHED
;
111 if (!geq_margin(ev
.duration
, SONY_TRAILER_SPACE
, SONY_UNIT
/ 2))
114 switch (data
->count
) {
116 if (!(dev
->enabled_protocols
& RC_PROTO_BIT_SONY12
))
117 goto finish_state_machine
;
119 device
= bitrev8((data
->bits
<< 3) & 0xF8);
121 function
= bitrev8((data
->bits
>> 4) & 0xFE);
122 protocol
= RC_PROTO_SONY12
;
125 if (!(dev
->enabled_protocols
& RC_PROTO_BIT_SONY15
))
126 goto finish_state_machine
;
128 device
= bitrev8((data
->bits
>> 0) & 0xFF);
130 function
= bitrev8((data
->bits
>> 7) & 0xFE);
131 protocol
= RC_PROTO_SONY15
;
134 if (!(dev
->enabled_protocols
& RC_PROTO_BIT_SONY20
))
135 goto finish_state_machine
;
137 device
= bitrev8((data
->bits
>> 5) & 0xF8);
138 subdevice
= bitrev8((data
->bits
>> 0) & 0xFF);
139 function
= bitrev8((data
->bits
>> 12) & 0xFE);
140 protocol
= RC_PROTO_SONY20
;
143 dev_dbg(&dev
->dev
, "Sony invalid bitcount %u\n",
148 scancode
= device
<< 16 | subdevice
<< 8 | function
;
149 dev_dbg(&dev
->dev
, "Sony(%u) scancode 0x%05x\n", data
->count
,
151 rc_keydown(dev
, protocol
, scancode
, 0);
152 goto finish_state_machine
;
156 dev_dbg(&dev
->dev
, "Sony decode failed at state %d (%uus %s)\n",
157 data
->state
, TO_US(ev
.duration
), TO_STR(ev
.pulse
));
158 data
->state
= STATE_INACTIVE
;
161 finish_state_machine
:
162 data
->state
= STATE_INACTIVE
;
166 static const struct ir_raw_timings_pl ir_sony_timings
= {
167 .header_pulse
= SONY_HEADER_PULSE
,
168 .bit_space
= SONY_BIT_SPACE
,
169 .bit_pulse
[0] = SONY_BIT_0_PULSE
,
170 .bit_pulse
[1] = SONY_BIT_1_PULSE
,
171 .trailer_space
= SONY_TRAILER_SPACE
+ SONY_BIT_SPACE
,
176 * ir_sony_encode() - Encode a scancode as a stream of raw events
178 * @protocol: protocol to encode
179 * @scancode: scancode to encode
180 * @events: array of raw ir events to write into
181 * @max: maximum size of @events
183 * Returns: The number of events written.
184 * -ENOBUFS if there isn't enough space in the array to fit the
185 * encoding. In this case all @max events will have been written.
187 static int ir_sony_encode(enum rc_proto protocol
, u32 scancode
,
188 struct ir_raw_event
*events
, unsigned int max
)
190 struct ir_raw_event
*e
= events
;
194 if (protocol
== RC_PROTO_SONY12
) {
195 raw
= (scancode
& 0x7f) | ((scancode
& 0x1f0000) >> 9);
197 } else if (protocol
== RC_PROTO_SONY15
) {
198 raw
= (scancode
& 0x7f) | ((scancode
& 0xff0000) >> 9);
201 raw
= (scancode
& 0x7f) | ((scancode
& 0x1f0000) >> 9) |
202 ((scancode
& 0xff00) << 4);
206 ret
= ir_raw_gen_pl(&e
, max
, &ir_sony_timings
, len
, raw
);
213 static struct ir_raw_handler sony_handler
= {
214 .protocols
= RC_PROTO_BIT_SONY12
| RC_PROTO_BIT_SONY15
|
216 .decode
= ir_sony_decode
,
217 .encode
= ir_sony_encode
,
219 .min_timeout
= SONY_TRAILER_SPACE
,
222 static int __init
ir_sony_decode_init(void)
224 ir_raw_handler_register(&sony_handler
);
226 printk(KERN_INFO
"IR Sony protocol handler initialized\n");
230 static void __exit
ir_sony_decode_exit(void)
232 ir_raw_handler_unregister(&sony_handler
);
235 module_init(ir_sony_decode_init
);
236 module_exit(ir_sony_decode_exit
);
238 MODULE_LICENSE("GPL");
239 MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
240 MODULE_DESCRIPTION("Sony IR protocol decoder");