blob: 03b1165eacbc3f0d13b13a8d065f261ebca2de09 [file] [log] [blame]
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -03001/*
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>
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030024#include <linux/string.h>
Mauro Carvalho Chehab0b778a52006-12-27 14:04:09 -020025#include <linux/jiffies.h>
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030026#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
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030037static int debug; /* debug level (0,1,2) */
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030038module_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
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030069 dev->keycode = ir->ir_codes;
70 dev->keycodesize = sizeof(IR_KEYTAB_TYPE);
71 dev->keycodemax = IR_KEYTAB_SIZE;
72 for (i = 0; i < IR_KEYTAB_SIZE; i++)
73 set_bit(ir->ir_codes[i], dev->keybit);
74 clear_bit(0, dev->keybit);
75
76 set_bit(EV_KEY, dev->evbit);
77 if (repeat)
78 set_bit(EV_REP, dev->evbit);
79}
80
81void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
82{
83 if (ir->keypressed) {
84 ir->keypressed = 0;
85 ir_input_key_event(dev,ir);
86 }
87}
88
89void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
90 u32 ir_key, u32 ir_raw)
91{
92 u32 keycode = IR_KEYCODE(ir->ir_codes, ir_key);
93
94 if (ir->keypressed && ir->keycode != keycode) {
95 ir->keypressed = 0;
96 ir_input_key_event(dev,ir);
97 }
98 if (!ir->keypressed) {
99 ir->ir_key = ir_key;
100 ir->ir_raw = ir_raw;
101 ir->keycode = keycode;
102 ir->keypressed = 1;
103 ir_input_key_event(dev,ir);
104 }
105}
106
107/* -------------------------------------------------------------------------- */
Trent Piephod67be612007-07-11 20:28:44 -0300108/* extract mask bits out of data and pack them into the result */
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300109u32 ir_extract_bits(u32 data, u32 mask)
110{
Trent Piephod67be612007-07-11 20:28:44 -0300111 u32 vbit = 1, value = 0;
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300112
Trent Piephod67be612007-07-11 20:28:44 -0300113 do {
114 if (mask&1) {
115 if (data&1)
116 value |= vbit;
117 vbit<<=1;
118 }
119 data>>=1;
120 } while (mask>>=1);
121
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300122 return value;
123}
124
125static int inline getbit(u32 *samples, int bit)
126{
127 return (samples[bit/32] & (1 << (31-(bit%32)))) ? 1 : 0;
128}
129
130/* sump raw samples for visual debugging ;) */
131int ir_dump_samples(u32 *samples, int count)
132{
133 int i, bit, start;
134
135 printk(KERN_DEBUG "ir samples: ");
136 start = 0;
137 for (i = 0; i < count * 32; i++) {
138 bit = getbit(samples,i);
139 if (bit)
140 start = 1;
141 if (0 == start)
142 continue;
143 printk("%s", bit ? "#" : "_");
144 }
145 printk("\n");
146 return 0;
147}
148
149/* decode raw samples, pulse distance coding used by NEC remotes */
150int ir_decode_pulsedistance(u32 *samples, int count, int low, int high)
151{
152 int i,last,bit,len;
153 u32 curBit;
154 u32 value;
155
156 /* find start burst */
157 for (i = len = 0; i < count * 32; i++) {
158 bit = getbit(samples,i);
159 if (bit) {
160 len++;
161 } else {
162 if (len >= 29)
163 break;
164 len = 0;
165 }
166 }
167
168 /* start burst to short */
169 if (len < 29)
170 return 0xffffffff;
171
172 /* find start silence */
173 for (len = 0; i < count * 32; i++) {
174 bit = getbit(samples,i);
175 if (bit) {
176 break;
177 } else {
178 len++;
179 }
180 }
181
182 /* silence to short */
183 if (len < 7)
184 return 0xffffffff;
185
186 /* go decoding */
187 len = 0;
188 last = 1;
189 value = 0; curBit = 1;
190 for (; i < count * 32; i++) {
191 bit = getbit(samples,i);
192 if (last) {
193 if(bit) {
194 continue;
195 } else {
196 len = 1;
197 }
198 } else {
199 if (bit) {
200 if (len > (low + high) /2)
201 value |= curBit;
202 curBit <<= 1;
203 if (curBit == 1)
204 break;
205 } else {
206 len++;
207 }
208 }
209 last = bit;
210 }
211
212 return value;
213}
214
215/* decode raw samples, biphase coding, used by rc5 for example */
216int ir_decode_biphase(u32 *samples, int count, int low, int high)
217{
218 int i,last,bit,len,flips;
219 u32 value;
220
221 /* find start bit (1) */
222 for (i = 0; i < 32; i++) {
223 bit = getbit(samples,i);
224 if (bit)
225 break;
226 }
227
228 /* go decoding */
229 len = 0;
230 flips = 0;
231 value = 1;
232 for (; i < count * 32; i++) {
233 if (len > high)
234 break;
235 if (flips > 1)
236 break;
237 last = bit;
238 bit = getbit(samples,i);
239 if (last == bit) {
240 len++;
241 continue;
242 }
243 if (len < low) {
244 len++;
245 flips++;
246 continue;
247 }
248 value <<= 1;
249 value |= bit;
250 flips = 0;
251 len = 1;
252 }
253 return value;
254}
255
Hermann Pitton91607232006-12-07 21:45:28 -0300256/* RC5 decoding stuff, moved from bttv-input.c to share it with
257 * saa7134 */
258
259/* decode raw bit pattern to RC5 code */
Adrian Bunk9695a492007-11-05 14:07:18 -0300260static u32 ir_rc5_decode(unsigned int code)
Hermann Pitton91607232006-12-07 21:45:28 -0300261{
262 unsigned int org_code = code;
263 unsigned int pair;
264 unsigned int rc5 = 0;
265 int i;
266
267 for (i = 0; i < 14; ++i) {
268 pair = code & 0x3;
269 code >>= 2;
270
271 rc5 <<= 1;
272 switch (pair) {
273 case 0:
274 case 2:
275 break;
276 case 1:
277 rc5 |= 1;
278 break;
279 case 3:
280 dprintk(1, "ir-common: ir_rc5_decode(%x) bad code\n", org_code);
281 return 0;
282 }
283 }
284 dprintk(1, "ir-common: code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
285 "instr=%x\n", rc5, org_code, RC5_START(rc5),
286 RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
287 return rc5;
288}
289
290void ir_rc5_timer_end(unsigned long data)
291{
292 struct card_ir *ir = (struct card_ir *)data;
293 struct timeval tv;
294 unsigned long current_jiffies, timeout;
295 u32 gap;
296 u32 rc5 = 0;
297
298 /* get time */
299 current_jiffies = jiffies;
300 do_gettimeofday(&tv);
301
302 /* avoid overflow with gap >1s */
303 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
304 gap = 200000;
305 } else {
306 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
307 tv.tv_usec - ir->base_time.tv_usec;
308 }
309
Vincent Penne726cf562007-03-25 11:58:23 -0300310 /* signal we're ready to start a new code */
311 ir->active = 0;
312
313 /* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
Hermann Pitton91607232006-12-07 21:45:28 -0300314 if (gap < 28000) {
315 dprintk(1, "ir-common: spurious timer_end\n");
316 return;
317 }
318
Hermann Pitton91607232006-12-07 21:45:28 -0300319 if (ir->last_bit < 20) {
320 /* ignore spurious codes (caused by light/other remotes) */
321 dprintk(1, "ir-common: short code: %x\n", ir->code);
322 } else {
323 ir->code = (ir->code << ir->shift_by) | 1;
324 rc5 = ir_rc5_decode(ir->code);
325
326 /* two start bits? */
327 if (RC5_START(rc5) != ir->start) {
328 dprintk(1, "ir-common: rc5 start bits invalid: %u\n", RC5_START(rc5));
329
330 /* right address? */
331 } else if (RC5_ADDR(rc5) == ir->addr) {
332 u32 toggle = RC5_TOGGLE(rc5);
333 u32 instr = RC5_INSTR(rc5);
334
335 /* Good code, decide if repeat/repress */
336 if (toggle != RC5_TOGGLE(ir->last_rc5) ||
337 instr != RC5_INSTR(ir->last_rc5)) {
338 dprintk(1, "ir-common: instruction %x, toggle %x\n", instr,
339 toggle);
340 ir_input_nokey(ir->dev, &ir->ir);
341 ir_input_keydown(ir->dev, &ir->ir, instr,
342 instr);
343 }
344
345 /* Set/reset key-up timer */
Mauro Carvalho Chehabf7518bd2007-07-17 16:27:30 -0300346 timeout = current_jiffies +
347 msecs_to_jiffies(ir->rc5_key_timeout);
Hermann Pitton91607232006-12-07 21:45:28 -0300348 mod_timer(&ir->timer_keyup, timeout);
349
350 /* Save code for repeat test */
351 ir->last_rc5 = rc5;
352 }
353 }
354}
355
356void ir_rc5_timer_keyup(unsigned long data)
357{
358 struct card_ir *ir = (struct card_ir *)data;
359
360 dprintk(1, "ir-common: key released\n");
361 ir_input_nokey(ir->dev, &ir->ir);
362}
363
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300364EXPORT_SYMBOL_GPL(ir_input_init);
365EXPORT_SYMBOL_GPL(ir_input_nokey);
366EXPORT_SYMBOL_GPL(ir_input_keydown);
367
368EXPORT_SYMBOL_GPL(ir_extract_bits);
369EXPORT_SYMBOL_GPL(ir_dump_samples);
370EXPORT_SYMBOL_GPL(ir_decode_biphase);
371EXPORT_SYMBOL_GPL(ir_decode_pulsedistance);
372
Hermann Pitton91607232006-12-07 21:45:28 -0300373EXPORT_SYMBOL_GPL(ir_rc5_timer_end);
374EXPORT_SYMBOL_GPL(ir_rc5_timer_keyup);
375
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300376/*
377 * Local variables:
378 * c-basic-offset: 8
379 * End:
380 */