]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/media/rc/ir-nec-decoder.c
media: cec: fix remote control passthrough
[mirror_ubuntu-bionic-kernel.git] / drivers / media / rc / ir-nec-decoder.c
CommitLineData
995187be 1/* ir-nec-decoder.c - handle NEC IR Pulse/Space protocol
a3572c34 2 *
37e59f87 3 * Copyright (C) 2010 by Mauro Carvalho Chehab
a3572c34
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
724e2495 15#include <linux/bitrev.h>
7a707b89 16#include <linux/module.h>
f62de675 17#include "rc-core-priv.h"
a3572c34 18
67780d6a 19#define NEC_NBITS 32
724e2495 20#define NEC_UNIT 562500 /* ns */
e40b1127
DH
21#define NEC_HEADER_PULSE (16 * NEC_UNIT)
22#define NECX_HEADER_PULSE (8 * NEC_UNIT) /* Less common NEC variant */
23#define NEC_HEADER_SPACE (8 * NEC_UNIT)
e31f4127 24#define NEC_REPEAT_SPACE (4 * NEC_UNIT)
e40b1127
DH
25#define NEC_BIT_PULSE (1 * NEC_UNIT)
26#define NEC_BIT_0_SPACE (1 * NEC_UNIT)
27#define NEC_BIT_1_SPACE (3 * NEC_UNIT)
28#define NEC_TRAILER_PULSE (1 * NEC_UNIT)
29#define NEC_TRAILER_SPACE (10 * NEC_UNIT) /* even longer in reality */
86ff071c 30#define NECX_REPEAT_BITS 1
9f154782 31
2f16f631
MCC
32enum nec_state {
33 STATE_INACTIVE,
2f16f631 34 STATE_HEADER_SPACE,
724e2495
DH
35 STATE_BIT_PULSE,
36 STATE_BIT_SPACE,
37 STATE_TRAILER_PULSE,
2f16f631
MCC
38 STATE_TRAILER_SPACE,
39};
40
2f16f631 41/**
587835a4 42 * ir_nec_decode() - Decode one NEC pulse or space
d8b4b582 43 * @dev: the struct rc_dev descriptor of the device
e40b1127 44 * @duration: the struct ir_raw_event descriptor of the pulse/space
2f16f631
MCC
45 *
46 * This function returns -EINVAL if the pulse violates the state machine
9f154782 47 */
d8b4b582 48static int ir_nec_decode(struct rc_dev *dev, struct ir_raw_event ev)
9f154782 49{
d8b4b582 50 struct nec_dec *data = &dev->raw->nec;
724e2495 51 u32 scancode;
2ceeca04 52 enum rc_type rc_type;
724e2495 53 u8 address, not_address, command, not_command;
9f154782 54
4651918a
ML
55 if (!is_timing_event(ev)) {
56 if (ev.reset)
57 data->state = STATE_INACTIVE;
724e2495
DH
58 return 0;
59 }
a3572c34 60
e40b1127
DH
61 IR_dprintk(2, "NEC decode started at state %d (%uus %s)\n",
62 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
9f154782 63
2f16f631 64 switch (data->state) {
a3572c34 65
724e2495 66 case STATE_INACTIVE:
e40b1127
DH
67 if (!ev.pulse)
68 break;
69
743135e7 70 if (eq_margin(ev.duration, NEC_HEADER_PULSE, NEC_UNIT * 2)) {
86ff071c
ML
71 data->is_nec_x = false;
72 data->necx_repeat = false;
73 } else if (eq_margin(ev.duration, NECX_HEADER_PULSE, NEC_UNIT / 2))
74 data->is_nec_x = true;
75 else
e40b1127
DH
76 break;
77
78 data->count = 0;
79 data->state = STATE_HEADER_SPACE;
2f16f631 80 return 0;
724e2495 81
2f16f631 82 case STATE_HEADER_SPACE:
e40b1127
DH
83 if (ev.pulse)
84 break;
85
743135e7 86 if (eq_margin(ev.duration, NEC_HEADER_SPACE, NEC_UNIT)) {
724e2495 87 data->state = STATE_BIT_PULSE;
9f154782 88 return 0;
e40b1127 89 } else if (eq_margin(ev.duration, NEC_REPEAT_SPACE, NEC_UNIT / 2)) {
265a2988
DH
90 rc_repeat(dev);
91 IR_dprintk(1, "Repeat last key\n");
92 data->state = STATE_TRAILER_PULSE;
2f16f631
MCC
93 return 0;
94 }
e40b1127 95
724e2495
DH
96 break;
97
98 case STATE_BIT_PULSE:
e40b1127
DH
99 if (!ev.pulse)
100 break;
101
102 if (!eq_margin(ev.duration, NEC_BIT_PULSE, NEC_UNIT / 2))
103 break;
104
105 data->state = STATE_BIT_SPACE;
106 return 0;
724e2495
DH
107
108 case STATE_BIT_SPACE:
e40b1127 109 if (ev.pulse)
724e2495
DH
110 break;
111
86ff071c
ML
112 if (data->necx_repeat && data->count == NECX_REPEAT_BITS &&
113 geq_margin(ev.duration,
114 NEC_TRAILER_SPACE, NEC_UNIT / 2)) {
115 IR_dprintk(1, "Repeat last key\n");
ca86674b 116 rc_repeat(dev);
86ff071c
ML
117 data->state = STATE_INACTIVE;
118 return 0;
119
120 } else if (data->count > NECX_REPEAT_BITS)
121 data->necx_repeat = false;
122
c216369e 123 data->bits <<= 1;
e40b1127 124 if (eq_margin(ev.duration, NEC_BIT_1_SPACE, NEC_UNIT / 2))
c216369e 125 data->bits |= 1;
e40b1127
DH
126 else if (!eq_margin(ev.duration, NEC_BIT_0_SPACE, NEC_UNIT / 2))
127 break;
724e2495
DH
128 data->count++;
129
e40b1127
DH
130 if (data->count == NEC_NBITS)
131 data->state = STATE_TRAILER_PULSE;
132 else
724e2495 133 data->state = STATE_BIT_PULSE;
e40b1127
DH
134
135 return 0;
136
137 case STATE_TRAILER_PULSE:
138 if (!ev.pulse)
139 break;
140
141 if (!eq_margin(ev.duration, NEC_TRAILER_PULSE, NEC_UNIT / 2))
142 break;
143
144 data->state = STATE_TRAILER_SPACE;
145 return 0;
146
147 case STATE_TRAILER_SPACE:
148 if (ev.pulse)
149 break;
150
151 if (!geq_margin(ev.duration, NEC_TRAILER_SPACE, NEC_UNIT / 2))
152 break;
724e2495 153
c216369e
DH
154 address = bitrev8((data->bits >> 24) & 0xff);
155 not_address = bitrev8((data->bits >> 16) & 0xff);
156 command = bitrev8((data->bits >> 8) & 0xff);
157 not_command = bitrev8((data->bits >> 0) & 0xff);
724e2495 158
e8ffda78
SG
159 scancode = ir_nec_bytes_to_scancode(address, not_address,
160 command, not_command,
161 &rc_type);
724e2495 162
86ff071c
ML
163 if (data->is_nec_x)
164 data->necx_repeat = true;
165
2ceeca04 166 rc_keydown(dev, rc_type, scancode, 0);
e40b1127 167 data->state = STATE_INACTIVE;
2f16f631 168 return 0;
724e2495 169 }
a3572c34 170
39cac375
MCC
171 IR_dprintk(1, "NEC decode failed at count %d state %d (%uus %s)\n",
172 data->count, data->state, TO_US(ev.duration), TO_STR(ev.pulse));
2f16f631 173 data->state = STATE_INACTIVE;
a3572c34
MCC
174 return -EINVAL;
175}
ada39630 176
141cfb14
JH
177/**
178 * ir_nec_scancode_to_raw() - encode an NEC scancode ready for modulation.
179 * @protocol: specific protocol to use
180 * @scancode: a single NEC scancode.
181 * @raw: raw data to be modulated.
182 */
183static u32 ir_nec_scancode_to_raw(enum rc_type protocol, u32 scancode)
184{
185 unsigned int addr, addr_inv, data, data_inv;
186
187 data = scancode & 0xff;
188
189 if (protocol == RC_TYPE_NEC32) {
190 /* 32-bit NEC (used by Apple and TiVo remotes) */
191 /* scan encoding: aaAAddDD */
192 addr_inv = (scancode >> 24) & 0xff;
193 addr = (scancode >> 16) & 0xff;
194 data_inv = (scancode >> 8) & 0xff;
195 } else if (protocol == RC_TYPE_NECX) {
196 /* Extended NEC */
197 /* scan encoding AAaaDD */
198 addr = (scancode >> 16) & 0xff;
199 addr_inv = (scancode >> 8) & 0xff;
200 data_inv = data ^ 0xff;
201 } else {
202 /* Normal NEC */
203 /* scan encoding: AADD */
204 addr = (scancode >> 8) & 0xff;
205 addr_inv = addr ^ 0xff;
206 data_inv = data ^ 0xff;
207 }
208
209 /* raw encoding: ddDDaaAA */
210 return data_inv << 24 |
211 data << 16 |
212 addr_inv << 8 |
213 addr;
214}
215
216static const struct ir_raw_timings_pd ir_nec_timings = {
217 .header_pulse = NEC_HEADER_PULSE,
218 .header_space = NEC_HEADER_SPACE,
219 .bit_pulse = NEC_BIT_PULSE,
220 .bit_space[0] = NEC_BIT_0_SPACE,
221 .bit_space[1] = NEC_BIT_1_SPACE,
222 .trailer_pulse = NEC_TRAILER_PULSE,
223 .trailer_space = NEC_TRAILER_SPACE,
224 .msb_first = 0,
225};
226
227/**
228 * ir_nec_encode() - Encode a scancode as a stream of raw events
229 *
230 * @protocol: protocol to encode
231 * @scancode: scancode to encode
232 * @events: array of raw ir events to write into
233 * @max: maximum size of @events
234 *
235 * Returns: The number of events written.
236 * -ENOBUFS if there isn't enough space in the array to fit the
237 * encoding. In this case all @max events will have been written.
238 */
239static int ir_nec_encode(enum rc_type protocol, u32 scancode,
240 struct ir_raw_event *events, unsigned int max)
241{
242 struct ir_raw_event *e = events;
243 int ret;
244 u32 raw;
245
246 /* Convert a NEC scancode to raw NEC data */
247 raw = ir_nec_scancode_to_raw(protocol, scancode);
248
249 /* Modulate the raw data using a pulse distance modulation */
250 ret = ir_raw_gen_pd(&e, max, &ir_nec_timings, NEC_NBITS, raw);
251 if (ret < 0)
252 return ret;
253
254 return e - events;
255}
256
995187be 257static struct ir_raw_handler nec_handler = {
2ceeca04 258 .protocols = RC_BIT_NEC | RC_BIT_NECX | RC_BIT_NEC32,
20d5f116 259 .decode = ir_nec_decode,
141cfb14 260 .encode = ir_nec_encode,
995187be
MCC
261};
262
263static int __init ir_nec_decode_init(void)
264{
265 ir_raw_handler_register(&nec_handler);
266
267 printk(KERN_INFO "IR NEC protocol handler initialized\n");
268 return 0;
269}
270
271static void __exit ir_nec_decode_exit(void)
272{
273 ir_raw_handler_unregister(&nec_handler);
274}
275
276module_init(ir_nec_decode_init);
277module_exit(ir_nec_decode_exit);
278
279MODULE_LICENSE("GPL");
37e59f87 280MODULE_AUTHOR("Mauro Carvalho Chehab");
995187be
MCC
281MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
282MODULE_DESCRIPTION("NEC IR protocol decoder");