blob: d12dc3da59316f1380a07a2cd9d3bb0f86ba6664 [file] [log] [blame]
David Härdeman3fe29c82010-04-15 18:46:10 -03001/* ir-sony-decoder.c - handle Sony IR Pulse/Space protocol
2 *
3 * Copyright (C) 2010 by David Härdeman <david@hardeman.nu>
4 *
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.
8 *
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.
13 */
14
15#include <linux/bitrev.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -040016#include <linux/module.h>
Mauro Carvalho Chehabf62de672010-11-09 23:09:57 -030017#include "rc-core-priv.h"
David Härdeman3fe29c82010-04-15 18:46:10 -030018
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 */
26
David Härdeman3fe29c82010-04-15 18:46:10 -030027enum sony_state {
28 STATE_INACTIVE,
29 STATE_HEADER_SPACE,
30 STATE_BIT_PULSE,
31 STATE_BIT_SPACE,
32 STATE_FINISHED,
33};
34
David Härdeman3fe29c82010-04-15 18:46:10 -030035/**
36 * ir_sony_decode() - Decode one Sony pulse or space
David Härdemand8b4b582010-10-29 16:08:23 -030037 * @dev: the struct rc_dev descriptor of the device
David Härdeman3fe29c82010-04-15 18:46:10 -030038 * @ev: the struct ir_raw_event descriptor of the pulse/space
39 *
40 * This function returns -EINVAL if the pulse violates the state machine
41 */
David Härdemand8b4b582010-10-29 16:08:23 -030042static int ir_sony_decode(struct rc_dev *dev, struct ir_raw_event ev)
David Härdeman3fe29c82010-04-15 18:46:10 -030043{
David Härdemand8b4b582010-10-29 16:08:23 -030044 struct sony_dec *data = &dev->raw->sony;
David Härdeman120703f2014-04-03 20:31:30 -030045 enum rc_type protocol;
David Härdeman3fe29c82010-04-15 18:46:10 -030046 u32 scancode;
47 u8 device, subdevice, function;
48
David Härdemanc5540fb2014-04-03 20:32:21 -030049 if (!(dev->enabled_protocols &
50 (RC_BIT_SONY12 | RC_BIT_SONY15 | RC_BIT_SONY20)))
David Härdeman3fe29c82010-04-15 18:46:10 -030051 return 0;
52
Maxim Levitsky46519182010-10-16 19:56:28 -030053 if (!is_timing_event(ev)) {
54 if (ev.reset)
55 data->state = STATE_INACTIVE;
David Härdeman3fe29c82010-04-15 18:46:10 -030056 return 0;
57 }
58
59 if (!geq_margin(ev.duration, SONY_UNIT, SONY_UNIT / 2))
60 goto out;
61
62 IR_dprintk(2, "Sony decode started at state %d (%uus %s)\n",
63 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
64
65 switch (data->state) {
66
67 case STATE_INACTIVE:
68 if (!ev.pulse)
69 break;
70
71 if (!eq_margin(ev.duration, SONY_HEADER_PULSE, SONY_UNIT / 2))
72 break;
73
74 data->count = 0;
75 data->state = STATE_HEADER_SPACE;
76 return 0;
77
78 case STATE_HEADER_SPACE:
79 if (ev.pulse)
80 break;
81
82 if (!eq_margin(ev.duration, SONY_HEADER_SPACE, SONY_UNIT / 2))
83 break;
84
85 data->state = STATE_BIT_PULSE;
86 return 0;
87
88 case STATE_BIT_PULSE:
89 if (!ev.pulse)
90 break;
91
David Härdemanc2163692010-06-13 17:29:36 -030092 data->bits <<= 1;
David Härdeman3fe29c82010-04-15 18:46:10 -030093 if (eq_margin(ev.duration, SONY_BIT_1_PULSE, SONY_UNIT / 2))
David Härdemanc2163692010-06-13 17:29:36 -030094 data->bits |= 1;
David Härdeman3fe29c82010-04-15 18:46:10 -030095 else if (!eq_margin(ev.duration, SONY_BIT_0_PULSE, SONY_UNIT / 2))
96 break;
97
98 data->count++;
99 data->state = STATE_BIT_SPACE;
100 return 0;
101
102 case STATE_BIT_SPACE:
103 if (ev.pulse)
104 break;
105
106 if (!geq_margin(ev.duration, SONY_BIT_SPACE, SONY_UNIT / 2))
107 break;
108
109 decrease_duration(&ev, SONY_BIT_SPACE);
110
111 if (!geq_margin(ev.duration, SONY_UNIT, SONY_UNIT / 2)) {
112 data->state = STATE_BIT_PULSE;
113 return 0;
114 }
115
116 data->state = STATE_FINISHED;
117 /* Fall through */
118
119 case STATE_FINISHED:
120 if (ev.pulse)
121 break;
122
123 if (!geq_margin(ev.duration, SONY_TRAILER_SPACE, SONY_UNIT / 2))
124 break;
125
126 switch (data->count) {
127 case 12:
David Härdemanc5540fb2014-04-03 20:32:21 -0300128 if (!(dev->enabled_protocols & RC_BIT_SONY12)) {
David Härdemanc003ab12012-10-11 19:11:54 -0300129 data->state = STATE_INACTIVE;
130 return 0;
131 }
David Härdemanc2163692010-06-13 17:29:36 -0300132 device = bitrev8((data->bits << 3) & 0xF8);
David Härdeman3fe29c82010-04-15 18:46:10 -0300133 subdevice = 0;
David Härdemanc2163692010-06-13 17:29:36 -0300134 function = bitrev8((data->bits >> 4) & 0xFE);
David Härdeman120703f2014-04-03 20:31:30 -0300135 protocol = RC_TYPE_SONY12;
David Härdeman3fe29c82010-04-15 18:46:10 -0300136 break;
137 case 15:
David Härdemanc5540fb2014-04-03 20:32:21 -0300138 if (!(dev->enabled_protocols & RC_BIT_SONY15)) {
David Härdemanc003ab12012-10-11 19:11:54 -0300139 data->state = STATE_INACTIVE;
140 return 0;
141 }
David Härdemanc2163692010-06-13 17:29:36 -0300142 device = bitrev8((data->bits >> 0) & 0xFF);
David Härdeman3fe29c82010-04-15 18:46:10 -0300143 subdevice = 0;
James Hoganec9ee8e2012-02-20 06:13:54 -0300144 function = bitrev8((data->bits >> 7) & 0xFE);
David Härdeman120703f2014-04-03 20:31:30 -0300145 protocol = RC_TYPE_SONY15;
David Härdeman3fe29c82010-04-15 18:46:10 -0300146 break;
147 case 20:
David Härdemanc5540fb2014-04-03 20:32:21 -0300148 if (!(dev->enabled_protocols & RC_BIT_SONY20)) {
David Härdemanc003ab12012-10-11 19:11:54 -0300149 data->state = STATE_INACTIVE;
150 return 0;
151 }
David Härdemanc2163692010-06-13 17:29:36 -0300152 device = bitrev8((data->bits >> 5) & 0xF8);
153 subdevice = bitrev8((data->bits >> 0) & 0xFF);
154 function = bitrev8((data->bits >> 12) & 0xFE);
David Härdeman120703f2014-04-03 20:31:30 -0300155 protocol = RC_TYPE_SONY20;
David Härdeman3fe29c82010-04-15 18:46:10 -0300156 break;
157 default:
158 IR_dprintk(1, "Sony invalid bitcount %u\n", data->count);
159 goto out;
160 }
161
162 scancode = device << 16 | subdevice << 8 | function;
163 IR_dprintk(1, "Sony(%u) scancode 0x%05x\n", data->count, scancode);
David Härdeman120703f2014-04-03 20:31:30 -0300164 rc_keydown(dev, protocol, scancode, 0);
David Härdeman3fe29c82010-04-15 18:46:10 -0300165 data->state = STATE_INACTIVE;
166 return 0;
167 }
168
169out:
170 IR_dprintk(1, "Sony decode failed at state %d (%uus %s)\n",
171 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
172 data->state = STATE_INACTIVE;
173 return -EINVAL;
174}
175
David Härdeman3fe29c82010-04-15 18:46:10 -0300176static struct ir_raw_handler sony_handler = {
David Härdemanc003ab12012-10-11 19:11:54 -0300177 .protocols = RC_BIT_SONY12 | RC_BIT_SONY15 | RC_BIT_SONY20,
David Härdeman3fe29c82010-04-15 18:46:10 -0300178 .decode = ir_sony_decode,
David Härdeman3fe29c82010-04-15 18:46:10 -0300179};
180
181static int __init ir_sony_decode_init(void)
182{
183 ir_raw_handler_register(&sony_handler);
184
185 printk(KERN_INFO "IR Sony protocol handler initialized\n");
186 return 0;
187}
188
189static void __exit ir_sony_decode_exit(void)
190{
191 ir_raw_handler_unregister(&sony_handler);
192}
193
194module_init(ir_sony_decode_init);
195module_exit(ir_sony_decode_exit);
196
197MODULE_LICENSE("GPL");
198MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
199MODULE_DESCRIPTION("Sony IR protocol decoder");