]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/media/rc/ir-sanyo-decoder.c
Merge tag 'for-v4.14' of git://git.kernel.org/pub/scm/linux/kernel/git/sre/linux...
[mirror_ubuntu-bionic-kernel.git] / drivers / media / rc / ir-sanyo-decoder.c
CommitLineData
b32e7243
MCC
1/* ir-sanyo-decoder.c - handle SANYO IR Pulse/Space protocol
2 *
37e59f87 3 * Copyright (C) 2011 by Mauro Carvalho Chehab
b32e7243
MCC
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 * This protocol uses the NEC protocol timings. However, data is formatted as:
15 * 13 bits Custom Code
16 * 13 bits NOT(Custom Code)
17 * 8 bits Key data
18 * 8 bits NOT(Key data)
19 *
20 * According with LIRC, this protocol is used on Sanyo, Aiwa and Chinon
21 * Information for this protocol is available at the Sanyo LC7461 datasheet.
22 */
23
2e962f4e 24#include <linux/module.h>
b32e7243
MCC
25#include <linux/bitrev.h>
26#include "rc-core-priv.h"
27
28#define SANYO_NBITS (13+13+8+8)
29#define SANYO_UNIT 562500 /* ns */
30#define SANYO_HEADER_PULSE (16 * SANYO_UNIT)
31#define SANYO_HEADER_SPACE (8 * SANYO_UNIT)
32#define SANYO_BIT_PULSE (1 * SANYO_UNIT)
33#define SANYO_BIT_0_SPACE (1 * SANYO_UNIT)
34#define SANYO_BIT_1_SPACE (3 * SANYO_UNIT)
35#define SANYO_REPEAT_SPACE (150 * SANYO_UNIT)
36#define SANYO_TRAILER_PULSE (1 * SANYO_UNIT)
37#define SANYO_TRAILER_SPACE (10 * SANYO_UNIT) /* in fact, 42 */
38
39enum sanyo_state {
40 STATE_INACTIVE,
41 STATE_HEADER_SPACE,
42 STATE_BIT_PULSE,
43 STATE_BIT_SPACE,
44 STATE_TRAILER_PULSE,
45 STATE_TRAILER_SPACE,
46};
47
48/**
49 * ir_sanyo_decode() - Decode one SANYO pulse or space
50 * @dev: the struct rc_dev descriptor of the device
51 * @duration: the struct ir_raw_event descriptor of the pulse/space
52 *
53 * This function returns -EINVAL if the pulse violates the state machine
54 */
55static int ir_sanyo_decode(struct rc_dev *dev, struct ir_raw_event ev)
56{
57 struct sanyo_dec *data = &dev->raw->sanyo;
58 u32 scancode;
2bfc04d6
SY
59 u16 address;
60 u8 command, not_command;
b32e7243 61
b32e7243
MCC
62 if (!is_timing_event(ev)) {
63 if (ev.reset) {
64 IR_dprintk(1, "SANYO event reset received. reset to state 0\n");
65 data->state = STATE_INACTIVE;
66 }
67 return 0;
68 }
69
70 IR_dprintk(2, "SANYO decode started at state %d (%uus %s)\n",
71 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
72
73 switch (data->state) {
74
75 case STATE_INACTIVE:
76 if (!ev.pulse)
77 break;
78
79 if (eq_margin(ev.duration, SANYO_HEADER_PULSE, SANYO_UNIT / 2)) {
80 data->count = 0;
81 data->state = STATE_HEADER_SPACE;
82 return 0;
83 }
84 break;
85
86
87 case STATE_HEADER_SPACE:
88 if (ev.pulse)
89 break;
90
91 if (eq_margin(ev.duration, SANYO_HEADER_SPACE, SANYO_UNIT / 2)) {
92 data->state = STATE_BIT_PULSE;
93 return 0;
94 }
95
96 break;
97
98 case STATE_BIT_PULSE:
99 if (!ev.pulse)
100 break;
101
102 if (!eq_margin(ev.duration, SANYO_BIT_PULSE, SANYO_UNIT / 2))
103 break;
104
105 data->state = STATE_BIT_SPACE;
106 return 0;
107
108 case STATE_BIT_SPACE:
109 if (ev.pulse)
110 break;
111
112 if (!data->count && geq_margin(ev.duration, SANYO_REPEAT_SPACE, SANYO_UNIT / 2)) {
265a2988
DH
113 rc_repeat(dev);
114 IR_dprintk(1, "SANYO repeat last key\n");
115 data->state = STATE_INACTIVE;
b32e7243
MCC
116 return 0;
117 }
118
119 data->bits <<= 1;
120 if (eq_margin(ev.duration, SANYO_BIT_1_SPACE, SANYO_UNIT / 2))
121 data->bits |= 1;
122 else if (!eq_margin(ev.duration, SANYO_BIT_0_SPACE, SANYO_UNIT / 2))
123 break;
124 data->count++;
125
126 if (data->count == SANYO_NBITS)
127 data->state = STATE_TRAILER_PULSE;
128 else
129 data->state = STATE_BIT_PULSE;
130
131 return 0;
132
133 case STATE_TRAILER_PULSE:
134 if (!ev.pulse)
135 break;
136
137 if (!eq_margin(ev.duration, SANYO_TRAILER_PULSE, SANYO_UNIT / 2))
138 break;
139
140 data->state = STATE_TRAILER_SPACE;
141 return 0;
142
143 case STATE_TRAILER_SPACE:
144 if (ev.pulse)
145 break;
146
147 if (!geq_margin(ev.duration, SANYO_TRAILER_SPACE, SANYO_UNIT / 2))
148 break;
149
150 address = bitrev16((data->bits >> 29) & 0x1fff) >> 3;
5becbc58 151 /* not_address = bitrev16((data->bits >> 16) & 0x1fff) >> 3; */
b32e7243
MCC
152 command = bitrev8((data->bits >> 8) & 0xff);
153 not_command = bitrev8((data->bits >> 0) & 0xff);
154
155 if ((command ^ not_command) != 0xff) {
156 IR_dprintk(1, "SANYO checksum error: received 0x%08Lx\n",
157 data->bits);
158 data->state = STATE_INACTIVE;
159 return 0;
160 }
161
162 scancode = address << 8 | command;
163 IR_dprintk(1, "SANYO scancode: 0x%06x\n", scancode);
6d741bfe 164 rc_keydown(dev, RC_PROTO_SANYO, scancode, 0);
b32e7243
MCC
165 data->state = STATE_INACTIVE;
166 return 0;
167 }
168
169 IR_dprintk(1, "SANYO decode failed at count %d state %d (%uus %s)\n",
170 data->count, data->state, TO_US(ev.duration), TO_STR(ev.pulse));
171 data->state = STATE_INACTIVE;
172 return -EINVAL;
173}
174
cb981257
SY
175static const struct ir_raw_timings_pd ir_sanyo_timings = {
176 .header_pulse = SANYO_HEADER_PULSE,
177 .header_space = SANYO_HEADER_SPACE,
178 .bit_pulse = SANYO_BIT_PULSE,
179 .bit_space[0] = SANYO_BIT_0_SPACE,
180 .bit_space[1] = SANYO_BIT_1_SPACE,
181 .trailer_pulse = SANYO_TRAILER_PULSE,
182 .trailer_space = SANYO_TRAILER_SPACE,
183 .msb_first = 1,
184};
185
186/**
187 * ir_sanyo_encode() - Encode a scancode as a stream of raw events
188 *
189 * @protocol: protocol to encode
190 * @scancode: scancode to encode
191 * @events: array of raw ir events to write into
192 * @max: maximum size of @events
193 *
194 * Returns: The number of events written.
195 * -ENOBUFS if there isn't enough space in the array to fit the
196 * encoding. In this case all @max events will have been written.
197 */
6d741bfe 198static int ir_sanyo_encode(enum rc_proto protocol, u32 scancode,
cb981257
SY
199 struct ir_raw_event *events, unsigned int max)
200{
201 struct ir_raw_event *e = events;
202 int ret;
203 u64 raw;
204
205 raw = ((u64)(bitrev16(scancode >> 8) & 0xfff8) << (8 + 8 + 13 - 3)) |
206 ((u64)(bitrev16(~scancode >> 8) & 0xfff8) << (8 + 8 + 0 - 3)) |
207 ((bitrev8(scancode) & 0xff) << 8) |
208 (bitrev8(~scancode) & 0xff);
209
210 ret = ir_raw_gen_pd(&e, max, &ir_sanyo_timings, SANYO_NBITS, raw);
211 if (ret < 0)
212 return ret;
213
214 return e - events;
215}
216
b32e7243 217static struct ir_raw_handler sanyo_handler = {
6d741bfe 218 .protocols = RC_PROTO_BIT_SANYO,
b32e7243 219 .decode = ir_sanyo_decode,
cb981257 220 .encode = ir_sanyo_encode,
b32e7243
MCC
221};
222
223static int __init ir_sanyo_decode_init(void)
224{
225 ir_raw_handler_register(&sanyo_handler);
226
227 printk(KERN_INFO "IR SANYO protocol handler initialized\n");
228 return 0;
229}
230
231static void __exit ir_sanyo_decode_exit(void)
232{
233 ir_raw_handler_unregister(&sanyo_handler);
234}
235
236module_init(ir_sanyo_decode_init);
237module_exit(ir_sanyo_decode_exit);
238
239MODULE_LICENSE("GPL");
37e59f87 240MODULE_AUTHOR("Mauro Carvalho Chehab");
b32e7243
MCC
241MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
242MODULE_DESCRIPTION("SANYO IR protocol decoder");