blob: 29ab9c2db060c6831536579d4cbc50fad33120d5 [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ärdeman3fe29c82010-04-15 18:46:10 -030045 u32 scancode;
46 u8 device, subdevice, function;
47
David Härdeman9719afa2013-03-06 16:52:05 -030048 if (!(dev->enabled_protocols &
David Härdemanc003ab12012-10-11 19:11:54 -030049 (RC_BIT_SONY12 | RC_BIT_SONY15 | RC_BIT_SONY20)))
David Härdeman3fe29c82010-04-15 18:46:10 -030050 return 0;
51
Maxim Levitsky46519182010-10-16 19:56:28 -030052 if (!is_timing_event(ev)) {
53 if (ev.reset)
54 data->state = STATE_INACTIVE;
David Härdeman3fe29c82010-04-15 18:46:10 -030055 return 0;
56 }
57
58 if (!geq_margin(ev.duration, SONY_UNIT, SONY_UNIT / 2))
59 goto out;
60
61 IR_dprintk(2, "Sony decode started at state %d (%uus %s)\n",
62 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
63
64 switch (data->state) {
65
66 case STATE_INACTIVE:
67 if (!ev.pulse)
68 break;
69
70 if (!eq_margin(ev.duration, SONY_HEADER_PULSE, SONY_UNIT / 2))
71 break;
72
73 data->count = 0;
74 data->state = STATE_HEADER_SPACE;
75 return 0;
76
77 case STATE_HEADER_SPACE:
78 if (ev.pulse)
79 break;
80
81 if (!eq_margin(ev.duration, SONY_HEADER_SPACE, SONY_UNIT / 2))
82 break;
83
84 data->state = STATE_BIT_PULSE;
85 return 0;
86
87 case STATE_BIT_PULSE:
88 if (!ev.pulse)
89 break;
90
David Härdemanc2163692010-06-13 17:29:36 -030091 data->bits <<= 1;
David Härdeman3fe29c82010-04-15 18:46:10 -030092 if (eq_margin(ev.duration, SONY_BIT_1_PULSE, SONY_UNIT / 2))
David Härdemanc2163692010-06-13 17:29:36 -030093 data->bits |= 1;
David Härdeman3fe29c82010-04-15 18:46:10 -030094 else if (!eq_margin(ev.duration, SONY_BIT_0_PULSE, SONY_UNIT / 2))
95 break;
96
97 data->count++;
98 data->state = STATE_BIT_SPACE;
99 return 0;
100
101 case STATE_BIT_SPACE:
102 if (ev.pulse)
103 break;
104
105 if (!geq_margin(ev.duration, SONY_BIT_SPACE, SONY_UNIT / 2))
106 break;
107
108 decrease_duration(&ev, SONY_BIT_SPACE);
109
110 if (!geq_margin(ev.duration, SONY_UNIT, SONY_UNIT / 2)) {
111 data->state = STATE_BIT_PULSE;
112 return 0;
113 }
114
115 data->state = STATE_FINISHED;
116 /* Fall through */
117
118 case STATE_FINISHED:
119 if (ev.pulse)
120 break;
121
122 if (!geq_margin(ev.duration, SONY_TRAILER_SPACE, SONY_UNIT / 2))
123 break;
124
125 switch (data->count) {
126 case 12:
David Härdeman9719afa2013-03-06 16:52:05 -0300127 if (!(dev->enabled_protocols & RC_BIT_SONY12)) {
David Härdemanc003ab12012-10-11 19:11:54 -0300128 data->state = STATE_INACTIVE;
129 return 0;
130 }
David Härdemanc2163692010-06-13 17:29:36 -0300131 device = bitrev8((data->bits << 3) & 0xF8);
David Härdeman3fe29c82010-04-15 18:46:10 -0300132 subdevice = 0;
David Härdemanc2163692010-06-13 17:29:36 -0300133 function = bitrev8((data->bits >> 4) & 0xFE);
David Härdeman3fe29c82010-04-15 18:46:10 -0300134 break;
135 case 15:
David Härdeman9719afa2013-03-06 16:52:05 -0300136 if (!(dev->enabled_protocols & RC_BIT_SONY15)) {
David Härdemanc003ab12012-10-11 19:11:54 -0300137 data->state = STATE_INACTIVE;
138 return 0;
139 }
David Härdemanc2163692010-06-13 17:29:36 -0300140 device = bitrev8((data->bits >> 0) & 0xFF);
David Härdeman3fe29c82010-04-15 18:46:10 -0300141 subdevice = 0;
James Hoganec9ee8e2012-02-20 06:13:54 -0300142 function = bitrev8((data->bits >> 7) & 0xFE);
David Härdeman3fe29c82010-04-15 18:46:10 -0300143 break;
144 case 20:
David Härdeman9719afa2013-03-06 16:52:05 -0300145 if (!(dev->enabled_protocols & RC_BIT_SONY20)) {
David Härdemanc003ab12012-10-11 19:11:54 -0300146 data->state = STATE_INACTIVE;
147 return 0;
148 }
David Härdemanc2163692010-06-13 17:29:36 -0300149 device = bitrev8((data->bits >> 5) & 0xF8);
150 subdevice = bitrev8((data->bits >> 0) & 0xFF);
151 function = bitrev8((data->bits >> 12) & 0xFE);
David Härdeman3fe29c82010-04-15 18:46:10 -0300152 break;
153 default:
154 IR_dprintk(1, "Sony invalid bitcount %u\n", data->count);
155 goto out;
156 }
157
158 scancode = device << 16 | subdevice << 8 | function;
159 IR_dprintk(1, "Sony(%u) scancode 0x%05x\n", data->count, scancode);
Mauro Carvalho Chehabca866742010-11-17 13:53:11 -0300160 rc_keydown(dev, scancode, 0);
David Härdeman3fe29c82010-04-15 18:46:10 -0300161 data->state = STATE_INACTIVE;
162 return 0;
163 }
164
165out:
166 IR_dprintk(1, "Sony decode failed at state %d (%uus %s)\n",
167 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
168 data->state = STATE_INACTIVE;
169 return -EINVAL;
170}
171
David Härdeman3fe29c82010-04-15 18:46:10 -0300172static struct ir_raw_handler sony_handler = {
David Härdemanc003ab12012-10-11 19:11:54 -0300173 .protocols = RC_BIT_SONY12 | RC_BIT_SONY15 | RC_BIT_SONY20,
David Härdeman3fe29c82010-04-15 18:46:10 -0300174 .decode = ir_sony_decode,
David Härdeman3fe29c82010-04-15 18:46:10 -0300175};
176
177static int __init ir_sony_decode_init(void)
178{
179 ir_raw_handler_register(&sony_handler);
180
181 printk(KERN_INFO "IR Sony protocol handler initialized\n");
182 return 0;
183}
184
185static void __exit ir_sony_decode_exit(void)
186{
187 ir_raw_handler_unregister(&sony_handler);
188}
189
190module_init(ir_sony_decode_init);
191module_exit(ir_sony_decode_exit);
192
193MODULE_LICENSE("GPL");
194MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
195MODULE_DESCRIPTION("Sony IR protocol decoder");