1 /* ir-sony-decoder.c - handle Sony 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 <linux/module.h>
17 #include "rc-core-priv.h"
19 #define SONY_UNIT 600000 /* ns */
20 #define SONY_HEADER_PULSE (4 * SONY_UNIT)
21 #define SONY_HEADER_SPACE (1 * SONY_UNIT)
22 #define SONY_BIT_0_PULSE (1 * SONY_UNIT)
23 #define SONY_BIT_1_PULSE (2 * SONY_UNIT)
24 #define SONY_BIT_SPACE (1 * SONY_UNIT)
25 #define SONY_TRAILER_SPACE (10 * SONY_UNIT) /* minimum */
36 * ir_sony_decode() - Decode one Sony pulse or space
37 * @dev: the struct rc_dev descriptor of the device
38 * @ev: the struct ir_raw_event descriptor of the pulse/space
40 * This function returns -EINVAL if the pulse violates the state machine
42 static int ir_sony_decode(struct rc_dev *dev, struct ir_raw_event ev)
44 struct sony_dec *data = &dev->raw->sony;
45 enum rc_proto protocol;
47 u8 device, subdevice, function;
49 if (!is_timing_event(ev)) {
51 data->state = STATE_INACTIVE;
55 if (!geq_margin(ev.duration, SONY_UNIT, SONY_UNIT / 2))
58 dev_dbg(&dev->dev, "Sony decode started at state %d (%uus %s)\n",
59 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
61 switch (data->state) {
67 if (!eq_margin(ev.duration, SONY_HEADER_PULSE, SONY_UNIT / 2))
71 data->state = STATE_HEADER_SPACE;
74 case STATE_HEADER_SPACE:
78 if (!eq_margin(ev.duration, SONY_HEADER_SPACE, SONY_UNIT / 2))
81 data->state = STATE_BIT_PULSE;
89 if (eq_margin(ev.duration, SONY_BIT_1_PULSE, SONY_UNIT / 2))
91 else if (!eq_margin(ev.duration, SONY_BIT_0_PULSE, SONY_UNIT / 2))
95 data->state = STATE_BIT_SPACE;
102 if (!geq_margin(ev.duration, SONY_BIT_SPACE, SONY_UNIT / 2))
105 decrease_duration(&ev, SONY_BIT_SPACE);
107 if (!geq_margin(ev.duration, SONY_UNIT, SONY_UNIT / 2)) {
108 data->state = STATE_BIT_PULSE;
112 data->state = STATE_FINISHED;
119 if (!geq_margin(ev.duration, SONY_TRAILER_SPACE, SONY_UNIT / 2))
122 switch (data->count) {
124 if (!(dev->enabled_protocols & RC_PROTO_BIT_SONY12))
125 goto finish_state_machine;
127 device = bitrev8((data->bits << 3) & 0xF8);
129 function = bitrev8((data->bits >> 4) & 0xFE);
130 protocol = RC_PROTO_SONY12;
133 if (!(dev->enabled_protocols & RC_PROTO_BIT_SONY15))
134 goto finish_state_machine;
136 device = bitrev8((data->bits >> 0) & 0xFF);
138 function = bitrev8((data->bits >> 7) & 0xFE);
139 protocol = RC_PROTO_SONY15;
142 if (!(dev->enabled_protocols & RC_PROTO_BIT_SONY20))
143 goto finish_state_machine;
145 device = bitrev8((data->bits >> 5) & 0xF8);
146 subdevice = bitrev8((data->bits >> 0) & 0xFF);
147 function = bitrev8((data->bits >> 12) & 0xFE);
148 protocol = RC_PROTO_SONY20;
151 dev_dbg(&dev->dev, "Sony invalid bitcount %u\n",
156 scancode = device << 16 | subdevice << 8 | function;
157 dev_dbg(&dev->dev, "Sony(%u) scancode 0x%05x\n", data->count,
159 rc_keydown(dev, protocol, scancode, 0);
160 goto finish_state_machine;
164 dev_dbg(&dev->dev, "Sony decode failed at state %d (%uus %s)\n",
165 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
166 data->state = STATE_INACTIVE;
169 finish_state_machine:
170 data->state = STATE_INACTIVE;
174 static const struct ir_raw_timings_pl ir_sony_timings = {
175 .header_pulse = SONY_HEADER_PULSE,
176 .bit_space = SONY_BIT_SPACE,
177 .bit_pulse[0] = SONY_BIT_0_PULSE,
178 .bit_pulse[1] = SONY_BIT_1_PULSE,
179 .trailer_space = SONY_TRAILER_SPACE + SONY_BIT_SPACE,
184 * ir_sony_encode() - Encode a scancode as a stream of raw events
186 * @protocol: protocol to encode
187 * @scancode: scancode to encode
188 * @events: array of raw ir events to write into
189 * @max: maximum size of @events
191 * Returns: The number of events written.
192 * -ENOBUFS if there isn't enough space in the array to fit the
193 * encoding. In this case all @max events will have been written.
195 static int ir_sony_encode(enum rc_proto protocol, u32 scancode,
196 struct ir_raw_event *events, unsigned int max)
198 struct ir_raw_event *e = events;
202 if (protocol == RC_PROTO_SONY12) {
203 raw = (scancode & 0x7f) | ((scancode & 0x1f0000) >> 9);
205 } else if (protocol == RC_PROTO_SONY15) {
206 raw = (scancode & 0x7f) | ((scancode & 0xff0000) >> 9);
209 raw = (scancode & 0x7f) | ((scancode & 0x1f0000) >> 9) |
210 ((scancode & 0xff00) << 4);
214 ret = ir_raw_gen_pl(&e, max, &ir_sony_timings, len, raw);
221 static struct ir_raw_handler sony_handler = {
222 .protocols = RC_PROTO_BIT_SONY12 | RC_PROTO_BIT_SONY15 |
224 .decode = ir_sony_decode,
225 .encode = ir_sony_encode,
227 .min_timeout = SONY_TRAILER_SPACE,
230 static int __init ir_sony_decode_init(void)
232 ir_raw_handler_register(&sony_handler);
234 printk(KERN_INFO "IR Sony protocol handler initialized\n");
238 static void __exit ir_sony_decode_exit(void)
240 ir_raw_handler_unregister(&sony_handler);
243 module_init(ir_sony_decode_init);
244 module_exit(ir_sony_decode_exit);
246 MODULE_LICENSE("GPL");
247 MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
248 MODULE_DESCRIPTION("Sony IR protocol decoder");