aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/common/ir-functions.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/common/ir-functions.c')
-rw-r--r--drivers/media/common/ir-functions.c272
1 files changed, 272 insertions, 0 deletions
diff --git a/drivers/media/common/ir-functions.c b/drivers/media/common/ir-functions.c
new file mode 100644
index 000000000000..397cff8b345b
--- /dev/null
+++ b/drivers/media/common/ir-functions.c
@@ -0,0 +1,272 @@
1/*
2 *
3 * some common structs and functions to handle infrared remotes via
4 * input layer ...
5 *
6 * (c) 2003 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#include <linux/module.h>
24#include <linux/moduleparam.h>
25#include <linux/string.h>
26#include <media/ir-common.h>
27
28/* -------------------------------------------------------------------------- */
29
30MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
31MODULE_LICENSE("GPL");
32
33static int repeat = 1;
34module_param(repeat, int, 0444);
35MODULE_PARM_DESC(repeat,"auto-repeat for IR keys (default: on)");
36
37static int debug = 0; /* debug level (0,1,2) */
38module_param(debug, int, 0644);
39
40#define dprintk(level, fmt, arg...) if (debug >= level) \
41 printk(KERN_DEBUG fmt , ## arg)
42
43/* -------------------------------------------------------------------------- */
44
45static void ir_input_key_event(struct input_dev *dev, struct ir_input_state *ir)
46{
47 if (KEY_RESERVED == ir->keycode) {
48 printk(KERN_INFO "%s: unknown key: key=0x%02x raw=0x%02x down=%d\n",
49 dev->name,ir->ir_key,ir->ir_raw,ir->keypressed);
50 return;
51 }
52 dprintk(1,"%s: key event code=%d down=%d\n",
53 dev->name,ir->keycode,ir->keypressed);
54 input_report_key(dev,ir->keycode,ir->keypressed);
55 input_sync(dev);
56}
57
58/* -------------------------------------------------------------------------- */
59
60void ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
61 int ir_type, IR_KEYTAB_TYPE *ir_codes)
62{
63 int i;
64
65 ir->ir_type = ir_type;
66 if (ir_codes)
67 memcpy(ir->ir_codes, ir_codes, sizeof(ir->ir_codes));
68
69
70 dev->keycode = ir->ir_codes;
71 dev->keycodesize = sizeof(IR_KEYTAB_TYPE);
72 dev->keycodemax = IR_KEYTAB_SIZE;
73 for (i = 0; i < IR_KEYTAB_SIZE; i++)
74 set_bit(ir->ir_codes[i], dev->keybit);
75 clear_bit(0, dev->keybit);
76
77 set_bit(EV_KEY, dev->evbit);
78 if (repeat)
79 set_bit(EV_REP, dev->evbit);
80}
81
82void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
83{
84 if (ir->keypressed) {
85 ir->keypressed = 0;
86 ir_input_key_event(dev,ir);
87 }
88}
89
90void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
91 u32 ir_key, u32 ir_raw)
92{
93 u32 keycode = IR_KEYCODE(ir->ir_codes, ir_key);
94
95 if (ir->keypressed && ir->keycode != keycode) {
96 ir->keypressed = 0;
97 ir_input_key_event(dev,ir);
98 }
99 if (!ir->keypressed) {
100 ir->ir_key = ir_key;
101 ir->ir_raw = ir_raw;
102 ir->keycode = keycode;
103 ir->keypressed = 1;
104 ir_input_key_event(dev,ir);
105 }
106}
107
108/* -------------------------------------------------------------------------- */
109
110u32 ir_extract_bits(u32 data, u32 mask)
111{
112 int mbit, vbit;
113 u32 value;
114
115 value = 0;
116 vbit = 0;
117 for (mbit = 0; mbit < 32; mbit++) {
118 if (!(mask & ((u32)1 << mbit)))
119 continue;
120 if (data & ((u32)1 << mbit))
121 value |= (1 << vbit);
122 vbit++;
123 }
124 return value;
125}
126
127static int inline getbit(u32 *samples, int bit)
128{
129 return (samples[bit/32] & (1 << (31-(bit%32)))) ? 1 : 0;
130}
131
132/* sump raw samples for visual debugging ;) */
133int ir_dump_samples(u32 *samples, int count)
134{
135 int i, bit, start;
136
137 printk(KERN_DEBUG "ir samples: ");
138 start = 0;
139 for (i = 0; i < count * 32; i++) {
140 bit = getbit(samples,i);
141 if (bit)
142 start = 1;
143 if (0 == start)
144 continue;
145 printk("%s", bit ? "#" : "_");
146 }
147 printk("\n");
148 return 0;
149}
150
151/* decode raw samples, pulse distance coding used by NEC remotes */
152int ir_decode_pulsedistance(u32 *samples, int count, int low, int high)
153{
154 int i,last,bit,len;
155 u32 curBit;
156 u32 value;
157
158 /* find start burst */
159 for (i = len = 0; i < count * 32; i++) {
160 bit = getbit(samples,i);
161 if (bit) {
162 len++;
163 } else {
164 if (len >= 29)
165 break;
166 len = 0;
167 }
168 }
169
170 /* start burst to short */
171 if (len < 29)
172 return 0xffffffff;
173
174 /* find start silence */
175 for (len = 0; i < count * 32; i++) {
176 bit = getbit(samples,i);
177 if (bit) {
178 break;
179 } else {
180 len++;
181 }
182 }
183
184 /* silence to short */
185 if (len < 7)
186 return 0xffffffff;
187
188 /* go decoding */
189 len = 0;
190 last = 1;
191 value = 0; curBit = 1;
192 for (; i < count * 32; i++) {
193 bit = getbit(samples,i);
194 if (last) {
195 if(bit) {
196 continue;
197 } else {
198 len = 1;
199 }
200 } else {
201 if (bit) {
202 if (len > (low + high) /2)
203 value |= curBit;
204 curBit <<= 1;
205 if (curBit == 1)
206 break;
207 } else {
208 len++;
209 }
210 }
211 last = bit;
212 }
213
214 return value;
215}
216
217/* decode raw samples, biphase coding, used by rc5 for example */
218int ir_decode_biphase(u32 *samples, int count, int low, int high)
219{
220 int i,last,bit,len,flips;
221 u32 value;
222
223 /* find start bit (1) */
224 for (i = 0; i < 32; i++) {
225 bit = getbit(samples,i);
226 if (bit)
227 break;
228 }
229
230 /* go decoding */
231 len = 0;
232 flips = 0;
233 value = 1;
234 for (; i < count * 32; i++) {
235 if (len > high)
236 break;
237 if (flips > 1)
238 break;
239 last = bit;
240 bit = getbit(samples,i);
241 if (last == bit) {
242 len++;
243 continue;
244 }
245 if (len < low) {
246 len++;
247 flips++;
248 continue;
249 }
250 value <<= 1;
251 value |= bit;
252 flips = 0;
253 len = 1;
254 }
255 return value;
256}
257
258EXPORT_SYMBOL_GPL(ir_input_init);
259EXPORT_SYMBOL_GPL(ir_input_nokey);
260EXPORT_SYMBOL_GPL(ir_input_keydown);
261
262EXPORT_SYMBOL_GPL(ir_extract_bits);
263EXPORT_SYMBOL_GPL(ir_dump_samples);
264EXPORT_SYMBOL_GPL(ir_decode_biphase);
265EXPORT_SYMBOL_GPL(ir_decode_pulsedistance);
266
267/*
268 * Local variables:
269 * c-basic-offset: 8
270 * End:
271 */
272