GNU Linux-libre 4.19.207-gnu1
[releases.git] / drivers / media / rc / ir-rc6-decoder.c
1 /* ir-rc6-decoder.c - A decoder for the RC6 IR 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 "rc-core-priv.h"
16 #include <linux/module.h>
17
18 /*
19  * This decoder currently supports:
20  * RC6-0-16     (standard toggle bit in header)
21  * RC6-6A-20    (no toggle bit)
22  * RC6-6A-24    (no toggle bit)
23  * RC6-6A-32    (MCE version with toggle bit in body)
24  */
25
26 #define RC6_UNIT                444444  /* nanosecs */
27 #define RC6_HEADER_NBITS        4       /* not including toggle bit */
28 #define RC6_0_NBITS             16
29 #define RC6_6A_32_NBITS         32
30 #define RC6_6A_NBITS            128     /* Variable 8..128 */
31 #define RC6_PREFIX_PULSE        (6 * RC6_UNIT)
32 #define RC6_PREFIX_SPACE        (2 * RC6_UNIT)
33 #define RC6_BIT_START           (1 * RC6_UNIT)
34 #define RC6_BIT_END             (1 * RC6_UNIT)
35 #define RC6_TOGGLE_START        (2 * RC6_UNIT)
36 #define RC6_TOGGLE_END          (2 * RC6_UNIT)
37 #define RC6_SUFFIX_SPACE        (6 * RC6_UNIT)
38 #define RC6_MODE_MASK           0x07    /* for the header bits */
39 #define RC6_STARTBIT_MASK       0x08    /* for the header bits */
40 #define RC6_6A_MCE_TOGGLE_MASK  0x8000  /* for the body bits */
41 #define RC6_6A_LCC_MASK         0xffff0000 /* RC6-6A-32 long customer code mask */
42 #define RC6_6A_MCE_CC           0x800f0000 /* MCE customer code */
43 #define RC6_6A_KATHREIN_CC      0x80460000 /* Kathrein RCU-676 customer code */
44 #ifndef CHAR_BIT
45 #define CHAR_BIT 8      /* Normally in <limits.h> */
46 #endif
47
48 enum rc6_mode {
49         RC6_MODE_0,
50         RC6_MODE_6A,
51         RC6_MODE_UNKNOWN,
52 };
53
54 enum rc6_state {
55         STATE_INACTIVE,
56         STATE_PREFIX_SPACE,
57         STATE_HEADER_BIT_START,
58         STATE_HEADER_BIT_END,
59         STATE_TOGGLE_START,
60         STATE_TOGGLE_END,
61         STATE_BODY_BIT_START,
62         STATE_BODY_BIT_END,
63         STATE_FINISHED,
64 };
65
66 static enum rc6_mode rc6_mode(struct rc6_dec *data)
67 {
68         switch (data->header & RC6_MODE_MASK) {
69         case 0:
70                 return RC6_MODE_0;
71         case 6:
72                 if (!data->toggle)
73                         return RC6_MODE_6A;
74                 /* fall through */
75         default:
76                 return RC6_MODE_UNKNOWN;
77         }
78 }
79
80 /**
81  * ir_rc6_decode() - Decode one RC6 pulse or space
82  * @dev:        the struct rc_dev descriptor of the device
83  * @ev:         the struct ir_raw_event descriptor of the pulse/space
84  *
85  * This function returns -EINVAL if the pulse violates the state machine
86  */
87 static int ir_rc6_decode(struct rc_dev *dev, struct ir_raw_event ev)
88 {
89         struct rc6_dec *data = &dev->raw->rc6;
90         u32 scancode;
91         u8 toggle;
92         enum rc_proto protocol;
93
94         if (!is_timing_event(ev)) {
95                 if (ev.reset)
96                         data->state = STATE_INACTIVE;
97                 return 0;
98         }
99
100         if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
101                 goto out;
102
103 again:
104         dev_dbg(&dev->dev, "RC6 decode started at state %i (%uus %s)\n",
105                 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
106
107         if (!geq_margin(ev.duration, RC6_UNIT, RC6_UNIT / 2))
108                 return 0;
109
110         switch (data->state) {
111
112         case STATE_INACTIVE:
113                 if (!ev.pulse)
114                         break;
115
116                 /* Note: larger margin on first pulse since each RC6_UNIT
117                    is quite short and some hardware takes some time to
118                    adjust to the signal */
119                 if (!eq_margin(ev.duration, RC6_PREFIX_PULSE, RC6_UNIT))
120                         break;
121
122                 data->state = STATE_PREFIX_SPACE;
123                 data->count = 0;
124                 return 0;
125
126         case STATE_PREFIX_SPACE:
127                 if (ev.pulse)
128                         break;
129
130                 if (!eq_margin(ev.duration, RC6_PREFIX_SPACE, RC6_UNIT / 2))
131                         break;
132
133                 data->state = STATE_HEADER_BIT_START;
134                 data->header = 0;
135                 return 0;
136
137         case STATE_HEADER_BIT_START:
138                 if (!eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2))
139                         break;
140
141                 data->header <<= 1;
142                 if (ev.pulse)
143                         data->header |= 1;
144                 data->count++;
145                 data->state = STATE_HEADER_BIT_END;
146                 return 0;
147
148         case STATE_HEADER_BIT_END:
149                 if (data->count == RC6_HEADER_NBITS)
150                         data->state = STATE_TOGGLE_START;
151                 else
152                         data->state = STATE_HEADER_BIT_START;
153
154                 decrease_duration(&ev, RC6_BIT_END);
155                 goto again;
156
157         case STATE_TOGGLE_START:
158                 if (!eq_margin(ev.duration, RC6_TOGGLE_START, RC6_UNIT / 2))
159                         break;
160
161                 data->toggle = ev.pulse;
162                 data->state = STATE_TOGGLE_END;
163                 return 0;
164
165         case STATE_TOGGLE_END:
166                 if (!(data->header & RC6_STARTBIT_MASK)) {
167                         dev_dbg(&dev->dev, "RC6 invalid start bit\n");
168                         break;
169                 }
170
171                 data->state = STATE_BODY_BIT_START;
172                 decrease_duration(&ev, RC6_TOGGLE_END);
173                 data->count = 0;
174                 data->body = 0;
175
176                 switch (rc6_mode(data)) {
177                 case RC6_MODE_0:
178                         data->wanted_bits = RC6_0_NBITS;
179                         break;
180                 case RC6_MODE_6A:
181                         data->wanted_bits = RC6_6A_NBITS;
182                         break;
183                 default:
184                         dev_dbg(&dev->dev, "RC6 unknown mode\n");
185                         goto out;
186                 }
187                 goto again;
188
189         case STATE_BODY_BIT_START:
190                 if (eq_margin(ev.duration, RC6_BIT_START, RC6_UNIT / 2)) {
191                         /* Discard LSB's that won't fit in data->body */
192                         if (data->count++ < CHAR_BIT * sizeof data->body) {
193                                 data->body <<= 1;
194                                 if (ev.pulse)
195                                         data->body |= 1;
196                         }
197                         data->state = STATE_BODY_BIT_END;
198                         return 0;
199                 } else if (RC6_MODE_6A == rc6_mode(data) && !ev.pulse &&
200                                 geq_margin(ev.duration, RC6_SUFFIX_SPACE, RC6_UNIT / 2)) {
201                         data->state = STATE_FINISHED;
202                         goto again;
203                 }
204                 break;
205
206         case STATE_BODY_BIT_END:
207                 if (data->count == data->wanted_bits)
208                         data->state = STATE_FINISHED;
209                 else
210                         data->state = STATE_BODY_BIT_START;
211
212                 decrease_duration(&ev, RC6_BIT_END);
213                 goto again;
214
215         case STATE_FINISHED:
216                 if (ev.pulse)
217                         break;
218
219                 switch (rc6_mode(data)) {
220                 case RC6_MODE_0:
221                         scancode = data->body;
222                         toggle = data->toggle;
223                         protocol = RC_PROTO_RC6_0;
224                         dev_dbg(&dev->dev, "RC6(0) scancode 0x%04x (toggle: %u)\n",
225                                 scancode, toggle);
226                         break;
227
228                 case RC6_MODE_6A:
229                         if (data->count > CHAR_BIT * sizeof data->body) {
230                                 dev_dbg(&dev->dev, "RC6 too many (%u) data bits\n",
231                                         data->count);
232                                 goto out;
233                         }
234
235                         scancode = data->body;
236                         switch (data->count) {
237                         case 20:
238                                 protocol = RC_PROTO_RC6_6A_20;
239                                 toggle = 0;
240                                 break;
241                         case 24:
242                                 protocol = RC_PROTO_RC6_6A_24;
243                                 toggle = 0;
244                                 break;
245                         case 32:
246                                 switch (scancode & RC6_6A_LCC_MASK) {
247                                 case RC6_6A_MCE_CC:
248                                 case RC6_6A_KATHREIN_CC:
249                                         protocol = RC_PROTO_RC6_MCE;
250                                         toggle = !!(scancode & RC6_6A_MCE_TOGGLE_MASK);
251                                         scancode &= ~RC6_6A_MCE_TOGGLE_MASK;
252                                         break;
253                                 default:
254                                         protocol = RC_PROTO_RC6_6A_32;
255                                         toggle = 0;
256                                         break;
257                                 }
258                                 break;
259                         default:
260                                 dev_dbg(&dev->dev, "RC6(6A) unsupported length\n");
261                                 goto out;
262                         }
263
264                         dev_dbg(&dev->dev, "RC6(6A) proto 0x%04x, scancode 0x%08x (toggle: %u)\n",
265                                 protocol, scancode, toggle);
266                         break;
267                 default:
268                         dev_dbg(&dev->dev, "RC6 unknown mode\n");
269                         goto out;
270                 }
271
272                 rc_keydown(dev, protocol, scancode, toggle);
273                 data->state = STATE_INACTIVE;
274                 return 0;
275         }
276
277 out:
278         dev_dbg(&dev->dev, "RC6 decode failed at state %i (%uus %s)\n",
279                 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
280         data->state = STATE_INACTIVE;
281         return -EINVAL;
282 }
283
284 static const struct ir_raw_timings_manchester ir_rc6_timings[4] = {
285         {
286                 .leader_pulse           = RC6_PREFIX_PULSE,
287                 .leader_space           = RC6_PREFIX_SPACE,
288                 .clock                  = RC6_UNIT,
289                 .invert                 = 1,
290         },
291         {
292                 .clock                  = RC6_UNIT * 2,
293                 .invert                 = 1,
294         },
295         {
296                 .clock                  = RC6_UNIT,
297                 .invert                 = 1,
298                 .trailer_space          = RC6_SUFFIX_SPACE,
299         },
300 };
301
302 /**
303  * ir_rc6_encode() - Encode a scancode as a stream of raw events
304  *
305  * @protocol:   protocol to encode
306  * @scancode:   scancode to encode
307  * @events:     array of raw ir events to write into
308  * @max:        maximum size of @events
309  *
310  * Returns:     The number of events written.
311  *              -ENOBUFS if there isn't enough space in the array to fit the
312  *              encoding. In this case all @max events will have been written.
313  *              -EINVAL if the scancode is ambiguous or invalid.
314  */
315 static int ir_rc6_encode(enum rc_proto protocol, u32 scancode,
316                          struct ir_raw_event *events, unsigned int max)
317 {
318         int ret;
319         struct ir_raw_event *e = events;
320
321         if (protocol == RC_PROTO_RC6_0) {
322                 /* Modulate the header (Start Bit & Mode-0) */
323                 ret = ir_raw_gen_manchester(&e, max - (e - events),
324                                             &ir_rc6_timings[0],
325                                             RC6_HEADER_NBITS, (1 << 3));
326                 if (ret < 0)
327                         return ret;
328
329                 /* Modulate Trailer Bit */
330                 ret = ir_raw_gen_manchester(&e, max - (e - events),
331                                             &ir_rc6_timings[1], 1, 0);
332                 if (ret < 0)
333                         return ret;
334
335                 /* Modulate rest of the data */
336                 ret = ir_raw_gen_manchester(&e, max - (e - events),
337                                             &ir_rc6_timings[2], RC6_0_NBITS,
338                                             scancode);
339                 if (ret < 0)
340                         return ret;
341
342         } else {
343                 int bits;
344
345                 switch (protocol) {
346                 case RC_PROTO_RC6_MCE:
347                 case RC_PROTO_RC6_6A_32:
348                         bits = 32;
349                         break;
350                 case RC_PROTO_RC6_6A_24:
351                         bits = 24;
352                         break;
353                 case RC_PROTO_RC6_6A_20:
354                         bits = 20;
355                         break;
356                 default:
357                         return -EINVAL;
358                 }
359
360                 /* Modulate the header (Start Bit & Header-version 6 */
361                 ret = ir_raw_gen_manchester(&e, max - (e - events),
362                                             &ir_rc6_timings[0],
363                                             RC6_HEADER_NBITS, (1 << 3 | 6));
364                 if (ret < 0)
365                         return ret;
366
367                 /* Modulate Trailer Bit */
368                 ret = ir_raw_gen_manchester(&e, max - (e - events),
369                                             &ir_rc6_timings[1], 1, 0);
370                 if (ret < 0)
371                         return ret;
372
373                 /* Modulate rest of the data */
374                 ret = ir_raw_gen_manchester(&e, max - (e - events),
375                                             &ir_rc6_timings[2],
376                                             bits,
377                                             scancode);
378                 if (ret < 0)
379                         return ret;
380         }
381
382         return e - events;
383 }
384
385 static struct ir_raw_handler rc6_handler = {
386         .protocols      = RC_PROTO_BIT_RC6_0 | RC_PROTO_BIT_RC6_6A_20 |
387                           RC_PROTO_BIT_RC6_6A_24 | RC_PROTO_BIT_RC6_6A_32 |
388                           RC_PROTO_BIT_RC6_MCE,
389         .decode         = ir_rc6_decode,
390         .encode         = ir_rc6_encode,
391         .carrier        = 36000,
392         .min_timeout    = RC6_SUFFIX_SPACE,
393 };
394
395 static int __init ir_rc6_decode_init(void)
396 {
397         ir_raw_handler_register(&rc6_handler);
398
399         printk(KERN_INFO "IR RC6 protocol handler initialized\n");
400         return 0;
401 }
402
403 static void __exit ir_rc6_decode_exit(void)
404 {
405         ir_raw_handler_unregister(&rc6_handler);
406 }
407
408 module_init(ir_rc6_decode_init);
409 module_exit(ir_rc6_decode_exit);
410
411 MODULE_LICENSE("GPL");
412 MODULE_AUTHOR("David Härdeman <david@hardeman.nu>");
413 MODULE_DESCRIPTION("RC6 IR protocol decoder");