blob: b111a0d9409bfedf5dcf2343027cf92a15a02a83 [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
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -030037int media_ir_debug; /* media_ir_debug level (0,1,2) */
38module_param_named(debug, media_ir_debug, int, 0644);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030039
40/* -------------------------------------------------------------------------- */
41
42static void ir_input_key_event(struct input_dev *dev, struct ir_input_state *ir)
43{
44 if (KEY_RESERVED == ir->keycode) {
45 printk(KERN_INFO "%s: unknown key: key=0x%02x raw=0x%02x down=%d\n",
46 dev->name,ir->ir_key,ir->ir_raw,ir->keypressed);
47 return;
48 }
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -030049 IR_dprintk(1,"%s: key event code=%d down=%d\n",
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030050 dev->name,ir->keycode,ir->keypressed);
51 input_report_key(dev,ir->keycode,ir->keypressed);
52 input_sync(dev);
53}
54
55/* -------------------------------------------------------------------------- */
56
57void ir_input_init(struct input_dev *dev, struct ir_input_state *ir,
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -030058 int ir_type, struct ir_scancode_table *ir_codes)
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030059{
60 int i;
61
62 ir->ir_type = ir_type;
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -030063
Alan Cox361c9512009-11-26 12:22:11 -030064 memset(ir->ir_codes, 0, sizeof(ir->ir_codes));
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -030065
66 /*
67 * FIXME: This is a temporary workaround to use the new IR tables
68 * with the old approach. Later patches will replace this to a
69 * proper method
70 */
71
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030072 if (ir_codes)
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -030073 for (i = 0; i < ir_codes->size; i++)
74 if (ir_codes->scan[i].scancode < IR_KEYTAB_SIZE)
75 ir->ir_codes[ir_codes->scan[i].scancode] = ir_codes->scan[i].keycode;
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030076
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030077 dev->keycode = ir->ir_codes;
78 dev->keycodesize = sizeof(IR_KEYTAB_TYPE);
79 dev->keycodemax = IR_KEYTAB_SIZE;
80 for (i = 0; i < IR_KEYTAB_SIZE; i++)
81 set_bit(ir->ir_codes[i], dev->keybit);
82 clear_bit(0, dev->keybit);
83
84 set_bit(EV_KEY, dev->evbit);
85 if (repeat)
86 set_bit(EV_REP, dev->evbit);
87}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -030088EXPORT_SYMBOL_GPL(ir_input_init);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030089
90void ir_input_nokey(struct input_dev *dev, struct ir_input_state *ir)
91{
92 if (ir->keypressed) {
93 ir->keypressed = 0;
94 ir_input_key_event(dev,ir);
95 }
96}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -030097EXPORT_SYMBOL_GPL(ir_input_nokey);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030098
99void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
100 u32 ir_key, u32 ir_raw)
101{
102 u32 keycode = IR_KEYCODE(ir->ir_codes, ir_key);
103
104 if (ir->keypressed && ir->keycode != keycode) {
105 ir->keypressed = 0;
106 ir_input_key_event(dev,ir);
107 }
108 if (!ir->keypressed) {
109 ir->ir_key = ir_key;
110 ir->ir_raw = ir_raw;
111 ir->keycode = keycode;
112 ir->keypressed = 1;
113 ir_input_key_event(dev,ir);
114 }
115}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300116EXPORT_SYMBOL_GPL(ir_input_keydown);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300117
118/* -------------------------------------------------------------------------- */
Trent Piephod67be612007-07-11 20:28:44 -0300119/* extract mask bits out of data and pack them into the result */
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300120u32 ir_extract_bits(u32 data, u32 mask)
121{
Trent Piephod67be612007-07-11 20:28:44 -0300122 u32 vbit = 1, value = 0;
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300123
Trent Piephod67be612007-07-11 20:28:44 -0300124 do {
125 if (mask&1) {
126 if (data&1)
127 value |= vbit;
128 vbit<<=1;
129 }
130 data>>=1;
131 } while (mask>>=1);
132
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300133 return value;
134}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300135EXPORT_SYMBOL_GPL(ir_extract_bits);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300136
137static int inline getbit(u32 *samples, int bit)
138{
139 return (samples[bit/32] & (1 << (31-(bit%32)))) ? 1 : 0;
140}
141
142/* sump raw samples for visual debugging ;) */
143int ir_dump_samples(u32 *samples, int count)
144{
145 int i, bit, start;
146
147 printk(KERN_DEBUG "ir samples: ");
148 start = 0;
149 for (i = 0; i < count * 32; i++) {
150 bit = getbit(samples,i);
151 if (bit)
152 start = 1;
153 if (0 == start)
154 continue;
155 printk("%s", bit ? "#" : "_");
156 }
157 printk("\n");
158 return 0;
159}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300160EXPORT_SYMBOL_GPL(ir_dump_samples);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300161
162/* decode raw samples, pulse distance coding used by NEC remotes */
163int ir_decode_pulsedistance(u32 *samples, int count, int low, int high)
164{
165 int i,last,bit,len;
166 u32 curBit;
167 u32 value;
168
169 /* find start burst */
170 for (i = len = 0; i < count * 32; i++) {
171 bit = getbit(samples,i);
172 if (bit) {
173 len++;
174 } else {
175 if (len >= 29)
176 break;
177 len = 0;
178 }
179 }
180
181 /* start burst to short */
182 if (len < 29)
183 return 0xffffffff;
184
185 /* find start silence */
186 for (len = 0; i < count * 32; i++) {
187 bit = getbit(samples,i);
188 if (bit) {
189 break;
190 } else {
191 len++;
192 }
193 }
194
195 /* silence to short */
196 if (len < 7)
197 return 0xffffffff;
198
199 /* go decoding */
200 len = 0;
201 last = 1;
202 value = 0; curBit = 1;
203 for (; i < count * 32; i++) {
204 bit = getbit(samples,i);
205 if (last) {
206 if(bit) {
207 continue;
208 } else {
209 len = 1;
210 }
211 } else {
212 if (bit) {
213 if (len > (low + high) /2)
214 value |= curBit;
215 curBit <<= 1;
216 if (curBit == 1)
217 break;
218 } else {
219 len++;
220 }
221 }
222 last = bit;
223 }
224
225 return value;
226}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300227EXPORT_SYMBOL_GPL(ir_decode_pulsedistance);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300228
229/* decode raw samples, biphase coding, used by rc5 for example */
230int ir_decode_biphase(u32 *samples, int count, int low, int high)
231{
232 int i,last,bit,len,flips;
233 u32 value;
234
235 /* find start bit (1) */
236 for (i = 0; i < 32; i++) {
237 bit = getbit(samples,i);
238 if (bit)
239 break;
240 }
241
242 /* go decoding */
243 len = 0;
244 flips = 0;
245 value = 1;
246 for (; i < count * 32; i++) {
247 if (len > high)
248 break;
249 if (flips > 1)
250 break;
251 last = bit;
252 bit = getbit(samples,i);
253 if (last == bit) {
254 len++;
255 continue;
256 }
257 if (len < low) {
258 len++;
259 flips++;
260 continue;
261 }
262 value <<= 1;
263 value |= bit;
264 flips = 0;
265 len = 1;
266 }
267 return value;
268}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300269EXPORT_SYMBOL_GPL(ir_decode_biphase);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300270
Hermann Pitton91607232006-12-07 21:45:28 -0300271/* RC5 decoding stuff, moved from bttv-input.c to share it with
272 * saa7134 */
273
274/* decode raw bit pattern to RC5 code */
Andy Walls1d23a002009-09-27 20:05:23 -0300275u32 ir_rc5_decode(unsigned int code)
Hermann Pitton91607232006-12-07 21:45:28 -0300276{
277 unsigned int org_code = code;
278 unsigned int pair;
279 unsigned int rc5 = 0;
280 int i;
281
282 for (i = 0; i < 14; ++i) {
283 pair = code & 0x3;
284 code >>= 2;
285
286 rc5 <<= 1;
287 switch (pair) {
288 case 0:
289 case 2:
290 break;
291 case 1:
292 rc5 |= 1;
293 break;
294 case 3:
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300295 IR_dprintk(1, "ir-common: ir_rc5_decode(%x) bad code\n", org_code);
Hermann Pitton91607232006-12-07 21:45:28 -0300296 return 0;
297 }
298 }
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300299 IR_dprintk(1, "ir-common: code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
Hermann Pitton91607232006-12-07 21:45:28 -0300300 "instr=%x\n", rc5, org_code, RC5_START(rc5),
301 RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
302 return rc5;
303}
Andy Walls1d23a002009-09-27 20:05:23 -0300304EXPORT_SYMBOL_GPL(ir_rc5_decode);
Hermann Pitton91607232006-12-07 21:45:28 -0300305
306void ir_rc5_timer_end(unsigned long data)
307{
308 struct card_ir *ir = (struct card_ir *)data;
309 struct timeval tv;
310 unsigned long current_jiffies, timeout;
311 u32 gap;
312 u32 rc5 = 0;
313
314 /* get time */
315 current_jiffies = jiffies;
316 do_gettimeofday(&tv);
317
318 /* avoid overflow with gap >1s */
319 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
320 gap = 200000;
321 } else {
322 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
323 tv.tv_usec - ir->base_time.tv_usec;
324 }
325
Vincent Penne726cf562007-03-25 11:58:23 -0300326 /* signal we're ready to start a new code */
327 ir->active = 0;
328
329 /* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
Hermann Pitton91607232006-12-07 21:45:28 -0300330 if (gap < 28000) {
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300331 IR_dprintk(1, "ir-common: spurious timer_end\n");
Hermann Pitton91607232006-12-07 21:45:28 -0300332 return;
333 }
334
Hermann Pitton91607232006-12-07 21:45:28 -0300335 if (ir->last_bit < 20) {
336 /* ignore spurious codes (caused by light/other remotes) */
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300337 IR_dprintk(1, "ir-common: short code: %x\n", ir->code);
Hermann Pitton91607232006-12-07 21:45:28 -0300338 } else {
339 ir->code = (ir->code << ir->shift_by) | 1;
340 rc5 = ir_rc5_decode(ir->code);
341
342 /* two start bits? */
343 if (RC5_START(rc5) != ir->start) {
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300344 IR_dprintk(1, "ir-common: rc5 start bits invalid: %u\n", RC5_START(rc5));
Hermann Pitton91607232006-12-07 21:45:28 -0300345
346 /* right address? */
347 } else if (RC5_ADDR(rc5) == ir->addr) {
348 u32 toggle = RC5_TOGGLE(rc5);
349 u32 instr = RC5_INSTR(rc5);
350
351 /* Good code, decide if repeat/repress */
352 if (toggle != RC5_TOGGLE(ir->last_rc5) ||
353 instr != RC5_INSTR(ir->last_rc5)) {
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300354 IR_dprintk(1, "ir-common: instruction %x, toggle %x\n", instr,
Hermann Pitton91607232006-12-07 21:45:28 -0300355 toggle);
356 ir_input_nokey(ir->dev, &ir->ir);
357 ir_input_keydown(ir->dev, &ir->ir, instr,
358 instr);
359 }
360
361 /* Set/reset key-up timer */
Mauro Carvalho Chehabf7518bd2007-07-17 16:27:30 -0300362 timeout = current_jiffies +
363 msecs_to_jiffies(ir->rc5_key_timeout);
Hermann Pitton91607232006-12-07 21:45:28 -0300364 mod_timer(&ir->timer_keyup, timeout);
365
366 /* Save code for repeat test */
367 ir->last_rc5 = rc5;
368 }
369 }
370}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300371EXPORT_SYMBOL_GPL(ir_rc5_timer_end);
Hermann Pitton91607232006-12-07 21:45:28 -0300372
373void ir_rc5_timer_keyup(unsigned long data)
374{
375 struct card_ir *ir = (struct card_ir *)data;
376
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300377 IR_dprintk(1, "ir-common: key released\n");
Hermann Pitton91607232006-12-07 21:45:28 -0300378 ir_input_nokey(ir->dev, &ir->ir);
379}
Hermann Pitton91607232006-12-07 21:45:28 -0300380EXPORT_SYMBOL_GPL(ir_rc5_timer_keyup);