blob: d760d52abaa2f6d5e455da84bfa29170a50a88e4 [file] [log] [blame]
Thomas Gleixner8e8e69d2019-05-29 07:17:59 -07001// SPDX-License-Identifier: GPL-2.0-only
David Härdeman3fe29c82010-04-15 18:46:10 -03002/* ir-sony-decoder.c - handle Sony IR Pulse/Space protocol
3 *
4 * Copyright (C) 2010 by David Härdeman <david@hardeman.nu>
David Härdeman3fe29c82010-04-15 18:46:10 -03005 */
6
7#include <linux/bitrev.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -04008#include <linux/module.h>
Mauro Carvalho Chehabf62de672010-11-09 23:09:57 -03009#include "rc-core-priv.h"
David Härdeman3fe29c82010-04-15 18:46:10 -030010
Sean Young528222d82020-08-23 19:23:05 +020011#define SONY_UNIT 600 /* us */
David Härdeman3fe29c82010-04-15 18:46:10 -030012#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 */
18
David Härdeman3fe29c82010-04-15 18:46:10 -030019enum sony_state {
20 STATE_INACTIVE,
21 STATE_HEADER_SPACE,
22 STATE_BIT_PULSE,
23 STATE_BIT_SPACE,
24 STATE_FINISHED,
25};
26
David Härdeman3fe29c82010-04-15 18:46:10 -030027/**
28 * ir_sony_decode() - Decode one Sony pulse or space
David Härdemand8b4b582010-10-29 16:08:23 -030029 * @dev: the struct rc_dev descriptor of the device
David Härdeman3fe29c82010-04-15 18:46:10 -030030 * @ev: the struct ir_raw_event descriptor of the pulse/space
31 *
32 * This function returns -EINVAL if the pulse violates the state machine
33 */
David Härdemand8b4b582010-10-29 16:08:23 -030034static int ir_sony_decode(struct rc_dev *dev, struct ir_raw_event ev)
David Härdeman3fe29c82010-04-15 18:46:10 -030035{
David Härdemand8b4b582010-10-29 16:08:23 -030036 struct sony_dec *data = &dev->raw->sony;
Sean Young6d741bf2017-08-07 16:20:58 -040037 enum rc_proto protocol;
David Härdeman3fe29c82010-04-15 18:46:10 -030038 u32 scancode;
39 u8 device, subdevice, function;
40
Maxim Levitsky46519182010-10-16 19:56:28 -030041 if (!is_timing_event(ev)) {
42 if (ev.reset)
43 data->state = STATE_INACTIVE;
David Härdeman3fe29c82010-04-15 18:46:10 -030044 return 0;
45 }
46
47 if (!geq_margin(ev.duration, SONY_UNIT, SONY_UNIT / 2))
48 goto out;
49
Sean Young50078a92018-02-12 07:20:52 -050050 dev_dbg(&dev->dev, "Sony decode started at state %d (%uus %s)\n",
Sean Young528222d82020-08-23 19:23:05 +020051 data->state, ev.duration, TO_STR(ev.pulse));
David Härdeman3fe29c82010-04-15 18:46:10 -030052
53 switch (data->state) {
54
55 case STATE_INACTIVE:
56 if (!ev.pulse)
57 break;
58
59 if (!eq_margin(ev.duration, SONY_HEADER_PULSE, SONY_UNIT / 2))
60 break;
61
62 data->count = 0;
63 data->state = STATE_HEADER_SPACE;
64 return 0;
65
66 case STATE_HEADER_SPACE:
67 if (ev.pulse)
68 break;
69
70 if (!eq_margin(ev.duration, SONY_HEADER_SPACE, SONY_UNIT / 2))
71 break;
72
73 data->state = STATE_BIT_PULSE;
74 return 0;
75
76 case STATE_BIT_PULSE:
77 if (!ev.pulse)
78 break;
79
David Härdemanc2163692010-06-13 17:29:36 -030080 data->bits <<= 1;
David Härdeman3fe29c82010-04-15 18:46:10 -030081 if (eq_margin(ev.duration, SONY_BIT_1_PULSE, SONY_UNIT / 2))
David Härdemanc2163692010-06-13 17:29:36 -030082 data->bits |= 1;
David Härdeman3fe29c82010-04-15 18:46:10 -030083 else if (!eq_margin(ev.duration, SONY_BIT_0_PULSE, SONY_UNIT / 2))
84 break;
85
86 data->count++;
87 data->state = STATE_BIT_SPACE;
88 return 0;
89
90 case STATE_BIT_SPACE:
91 if (ev.pulse)
92 break;
93
94 if (!geq_margin(ev.duration, SONY_BIT_SPACE, SONY_UNIT / 2))
95 break;
96
97 decrease_duration(&ev, SONY_BIT_SPACE);
98
99 if (!geq_margin(ev.duration, SONY_UNIT, SONY_UNIT / 2)) {
100 data->state = STATE_BIT_PULSE;
101 return 0;
102 }
103
104 data->state = STATE_FINISHED;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500105 fallthrough;
David Härdeman3fe29c82010-04-15 18:46:10 -0300106
107 case STATE_FINISHED:
108 if (ev.pulse)
109 break;
110
111 if (!geq_margin(ev.duration, SONY_TRAILER_SPACE, SONY_UNIT / 2))
112 break;
113
114 switch (data->count) {
115 case 12:
Sean Young6d741bf2017-08-07 16:20:58 -0400116 if (!(dev->enabled_protocols & RC_PROTO_BIT_SONY12))
Mauro Carvalho Chehababefe122015-04-28 12:23:56 -0300117 goto finish_state_machine;
118
David Härdemanc2163692010-06-13 17:29:36 -0300119 device = bitrev8((data->bits << 3) & 0xF8);
David Härdeman3fe29c82010-04-15 18:46:10 -0300120 subdevice = 0;
David Härdemanc2163692010-06-13 17:29:36 -0300121 function = bitrev8((data->bits >> 4) & 0xFE);
Sean Young6d741bf2017-08-07 16:20:58 -0400122 protocol = RC_PROTO_SONY12;
David Härdeman3fe29c82010-04-15 18:46:10 -0300123 break;
124 case 15:
Sean Young6d741bf2017-08-07 16:20:58 -0400125 if (!(dev->enabled_protocols & RC_PROTO_BIT_SONY15))
Mauro Carvalho Chehababefe122015-04-28 12:23:56 -0300126 goto finish_state_machine;
127
David Härdemanc2163692010-06-13 17:29:36 -0300128 device = bitrev8((data->bits >> 0) & 0xFF);
David Härdeman3fe29c82010-04-15 18:46:10 -0300129 subdevice = 0;
James Hoganec9ee8e2012-02-20 06:13:54 -0300130 function = bitrev8((data->bits >> 7) & 0xFE);
Sean Young6d741bf2017-08-07 16:20:58 -0400131 protocol = RC_PROTO_SONY15;
David Härdeman3fe29c82010-04-15 18:46:10 -0300132 break;
133 case 20:
Sean Young6d741bf2017-08-07 16:20:58 -0400134 if (!(dev->enabled_protocols & RC_PROTO_BIT_SONY20))
Mauro Carvalho Chehababefe122015-04-28 12:23:56 -0300135 goto finish_state_machine;
136
David Härdemanc2163692010-06-13 17:29:36 -0300137 device = bitrev8((data->bits >> 5) & 0xF8);
138 subdevice = bitrev8((data->bits >> 0) & 0xFF);
139 function = bitrev8((data->bits >> 12) & 0xFE);
Sean Young6d741bf2017-08-07 16:20:58 -0400140 protocol = RC_PROTO_SONY20;
David Härdeman3fe29c82010-04-15 18:46:10 -0300141 break;
142 default:
Sean Young50078a92018-02-12 07:20:52 -0500143 dev_dbg(&dev->dev, "Sony invalid bitcount %u\n",
144 data->count);
David Härdeman3fe29c82010-04-15 18:46:10 -0300145 goto out;
146 }
147
148 scancode = device << 16 | subdevice << 8 | function;
Sean Young50078a92018-02-12 07:20:52 -0500149 dev_dbg(&dev->dev, "Sony(%u) scancode 0x%05x\n", data->count,
150 scancode);
David Härdeman120703f2014-04-03 20:31:30 -0300151 rc_keydown(dev, protocol, scancode, 0);
Mauro Carvalho Chehababefe122015-04-28 12:23:56 -0300152 goto finish_state_machine;
David Härdeman3fe29c82010-04-15 18:46:10 -0300153 }
154
155out:
Sean Young50078a92018-02-12 07:20:52 -0500156 dev_dbg(&dev->dev, "Sony decode failed at state %d (%uus %s)\n",
Sean Young528222d82020-08-23 19:23:05 +0200157 data->state, ev.duration, TO_STR(ev.pulse));
David Härdeman3fe29c82010-04-15 18:46:10 -0300158 data->state = STATE_INACTIVE;
159 return -EINVAL;
Mauro Carvalho Chehababefe122015-04-28 12:23:56 -0300160
161finish_state_machine:
162 data->state = STATE_INACTIVE;
163 return 0;
David Härdeman3fe29c82010-04-15 18:46:10 -0300164}
165
Sean Young103293b2016-12-06 18:33:57 -0200166static 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,
172 .msb_first = 0,
173};
174
175/**
176 * ir_sony_encode() - Encode a scancode as a stream of raw events
177 *
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
182 *
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.
186 */
Sean Young6d741bf2017-08-07 16:20:58 -0400187static int ir_sony_encode(enum rc_proto protocol, u32 scancode,
Sean Young103293b2016-12-06 18:33:57 -0200188 struct ir_raw_event *events, unsigned int max)
189{
190 struct ir_raw_event *e = events;
191 u32 raw, len;
192 int ret;
193
Sean Young6d741bf2017-08-07 16:20:58 -0400194 if (protocol == RC_PROTO_SONY12) {
Sean Young103293b2016-12-06 18:33:57 -0200195 raw = (scancode & 0x7f) | ((scancode & 0x1f0000) >> 9);
196 len = 12;
Sean Young6d741bf2017-08-07 16:20:58 -0400197 } else if (protocol == RC_PROTO_SONY15) {
Sean Young103293b2016-12-06 18:33:57 -0200198 raw = (scancode & 0x7f) | ((scancode & 0xff0000) >> 9);
199 len = 15;
200 } else {
201 raw = (scancode & 0x7f) | ((scancode & 0x1f0000) >> 9) |
202 ((scancode & 0xff00) << 4);
203 len = 20;
204 }
205
206 ret = ir_raw_gen_pl(&e, max, &ir_sony_timings, len, raw);
207 if (ret < 0)
208 return ret;
209
210 return e - events;
211}
212
David Härdeman3fe29c82010-04-15 18:46:10 -0300213static struct ir_raw_handler sony_handler = {
Sean Young6d741bf2017-08-07 16:20:58 -0400214 .protocols = RC_PROTO_BIT_SONY12 | RC_PROTO_BIT_SONY15 |
215 RC_PROTO_BIT_SONY20,
David Härdeman3fe29c82010-04-15 18:46:10 -0300216 .decode = ir_sony_decode,
Sean Young103293b2016-12-06 18:33:57 -0200217 .encode = ir_sony_encode,
Sean Youngcdfaa012017-02-25 06:51:30 -0500218 .carrier = 40000,
Sean Younga86d6df2018-03-23 16:47:37 -0400219 .min_timeout = SONY_TRAILER_SPACE,
David Härdeman3fe29c82010-04-15 18:46:10 -0300220};
221
222static int __init ir_sony_decode_init(void)
223{
224 ir_raw_handler_register(&sony_handler);
225
226 printk(KERN_INFO "IR Sony protocol handler initialized\n");
227 return 0;
228}
229
230static void __exit ir_sony_decode_exit(void)
231{
232 ir_raw_handler_unregister(&sony_handler);
233}
234
235module_init(ir_sony_decode_init);
236module_exit(ir_sony_decode_exit);
237
238MODULE_LICENSE("GPL");
239MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
240MODULE_DESCRIPTION("Sony IR protocol decoder");