aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/rc/ir-rc5-decoder.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/rc/ir-rc5-decoder.c')
-rw-r--r--drivers/media/rc/ir-rc5-decoder.c190
1 files changed, 190 insertions, 0 deletions
diff --git a/drivers/media/rc/ir-rc5-decoder.c b/drivers/media/rc/ir-rc5-decoder.c
new file mode 100644
index 000000000000..572ed4ca8c68
--- /dev/null
+++ b/drivers/media/rc/ir-rc5-decoder.c
@@ -0,0 +1,190 @@
1/* ir-rc5-decoder.c - handle RC5(x) IR Pulse/Space protocol
2 *
3 * Copyright (C) 2010 by Mauro Carvalho Chehab <mchehab@redhat.com>
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/*
16 * This code handles 14 bits RC5 protocols and 20 bits RC5x protocols.
17 * There are other variants that use a different number of bits.
18 * This is currently unsupported.
19 * It considers a carrier of 36 kHz, with a total of 14/20 bits, where
20 * the first two bits are start bits, and a third one is a filing bit
21 */
22
23#include "ir-core-priv.h"
24
25#define RC5_NBITS 14
26#define RC5X_NBITS 20
27#define CHECK_RC5X_NBITS 8
28#define RC5_UNIT 888888 /* ns */
29#define RC5_BIT_START (1 * RC5_UNIT)
30#define RC5_BIT_END (1 * RC5_UNIT)
31#define RC5X_SPACE (4 * RC5_UNIT)
32
33enum rc5_state {
34 STATE_INACTIVE,
35 STATE_BIT_START,
36 STATE_BIT_END,
37 STATE_CHECK_RC5X,
38 STATE_FINISHED,
39};
40
41/**
42 * ir_rc5_decode() - Decode one RC-5 pulse or space
43 * @input_dev: the struct input_dev descriptor of the device
44 * @ev: the struct ir_raw_event descriptor of the pulse/space
45 *
46 * This function returns -EINVAL if the pulse violates the state machine
47 */
48static int ir_rc5_decode(struct input_dev *input_dev, struct ir_raw_event ev)
49{
50 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
51 struct rc5_dec *data = &ir_dev->raw->rc5;
52 u8 toggle;
53 u32 scancode;
54
55 if (!(ir_dev->raw->enabled_protocols & IR_TYPE_RC5))
56 return 0;
57
58 if (!is_timing_event(ev)) {
59 if (ev.reset)
60 data->state = STATE_INACTIVE;
61 return 0;
62 }
63
64 if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2))
65 goto out;
66
67again:
68 IR_dprintk(2, "RC5(x) decode started at state %i (%uus %s)\n",
69 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
70
71 if (!geq_margin(ev.duration, RC5_UNIT, RC5_UNIT / 2))
72 return 0;
73
74 switch (data->state) {
75
76 case STATE_INACTIVE:
77 if (!ev.pulse)
78 break;
79
80 data->state = STATE_BIT_START;
81 data->count = 1;
82 /* We just need enough bits to get to STATE_CHECK_RC5X */
83 data->wanted_bits = RC5X_NBITS;
84 decrease_duration(&ev, RC5_BIT_START);
85 goto again;
86
87 case STATE_BIT_START:
88 if (!eq_margin(ev.duration, RC5_BIT_START, RC5_UNIT / 2))
89 break;
90
91 data->bits <<= 1;
92 if (!ev.pulse)
93 data->bits |= 1;
94 data->count++;
95 data->state = STATE_BIT_END;
96 return 0;
97
98 case STATE_BIT_END:
99 if (!is_transition(&ev, &ir_dev->raw->prev_ev))
100 break;
101
102 if (data->count == data->wanted_bits)
103 data->state = STATE_FINISHED;
104 else if (data->count == CHECK_RC5X_NBITS)
105 data->state = STATE_CHECK_RC5X;
106 else
107 data->state = STATE_BIT_START;
108
109 decrease_duration(&ev, RC5_BIT_END);
110 goto again;
111
112 case STATE_CHECK_RC5X:
113 if (!ev.pulse && geq_margin(ev.duration, RC5X_SPACE, RC5_UNIT / 2)) {
114 /* RC5X */
115 data->wanted_bits = RC5X_NBITS;
116 decrease_duration(&ev, RC5X_SPACE);
117 } else {
118 /* RC5 */
119 data->wanted_bits = RC5_NBITS;
120 }
121 data->state = STATE_BIT_START;
122 goto again;
123
124 case STATE_FINISHED:
125 if (ev.pulse)
126 break;
127
128 if (data->wanted_bits == RC5X_NBITS) {
129 /* RC5X */
130 u8 xdata, command, system;
131 xdata = (data->bits & 0x0003F) >> 0;
132 command = (data->bits & 0x00FC0) >> 6;
133 system = (data->bits & 0x1F000) >> 12;
134 toggle = (data->bits & 0x20000) ? 1 : 0;
135 command += (data->bits & 0x01000) ? 0 : 0x40;
136 scancode = system << 16 | command << 8 | xdata;
137
138 IR_dprintk(1, "RC5X scancode 0x%06x (toggle: %u)\n",
139 scancode, toggle);
140
141 } else {
142 /* RC5 */
143 u8 command, system;
144 command = (data->bits & 0x0003F) >> 0;
145 system = (data->bits & 0x007C0) >> 6;
146 toggle = (data->bits & 0x00800) ? 1 : 0;
147 command += (data->bits & 0x01000) ? 0 : 0x40;
148 scancode = system << 8 | command;
149
150 IR_dprintk(1, "RC5 scancode 0x%04x (toggle: %u)\n",
151 scancode, toggle);
152 }
153
154 ir_keydown(input_dev, scancode, toggle);
155 data->state = STATE_INACTIVE;
156 return 0;
157 }
158
159out:
160 IR_dprintk(1, "RC5(x) decode failed at state %i (%uus %s)\n",
161 data->state, TO_US(ev.duration), TO_STR(ev.pulse));
162 data->state = STATE_INACTIVE;
163 return -EINVAL;
164}
165
166static struct ir_raw_handler rc5_handler = {
167 .protocols = IR_TYPE_RC5,
168 .decode = ir_rc5_decode,
169};
170
171static int __init ir_rc5_decode_init(void)
172{
173 ir_raw_handler_register(&rc5_handler);
174
175 printk(KERN_INFO "IR RC5(x) protocol handler initialized\n");
176 return 0;
177}
178
179static void __exit ir_rc5_decode_exit(void)
180{
181 ir_raw_handler_unregister(&rc5_handler);
182}
183
184module_init(ir_rc5_decode_init);
185module_exit(ir_rc5_decode_exit);
186
187MODULE_LICENSE("GPL");
188MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
189MODULE_AUTHOR("Red Hat Inc. (http://www.redhat.com)");
190MODULE_DESCRIPTION("RC5(x) IR protocol decoder");