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