blob: b31bd27da374f758c1ba40b635cf39f4304fedb1 [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) {
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -030045 printk(KERN_INFO "%s: unknown key: key=0x%02x down=%d\n",
46 dev->name, ir->ir_key, ir->keypressed);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030047 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
Mauro Carvalho Chehab055cd552009-11-29 08:19:59 -030057int 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{
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030060 ir->ir_type = ir_type;
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -030061
Mauro Carvalho Chehab055cd552009-11-29 08:19:59 -030062 /* FIXME: Add the proper code to dynamically allocate IR table */
63
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -030064 ir_set_keycode_table(dev, ir_codes);
Mauro Carvalho Chehab715a2232009-08-29 14:15:55 -030065
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030066 clear_bit(0, dev->keybit);
67
68 set_bit(EV_KEY, dev->evbit);
69 if (repeat)
70 set_bit(EV_REP, dev->evbit);
Mauro Carvalho Chehab055cd552009-11-29 08:19:59 -030071
72 return 0;
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030073}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -030074EXPORT_SYMBOL_GPL(ir_input_init);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030075
Mauro Carvalho Chehab055cd552009-11-29 08:19:59 -030076void ir_input_free(struct input_dev *input_dev)
77{
78 /* FIXME: Add the proper code to free allocated resources */
79}
80EXPORT_SYMBOL_GPL(ir_input_free);
81
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030082void 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}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -030089EXPORT_SYMBOL_GPL(ir_input_nokey);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030090
91void ir_input_keydown(struct input_dev *dev, struct ir_input_state *ir,
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -030092 u32 ir_key)
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030093{
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -030094 u32 keycode = ir_g_keycode_from_table(dev, ir_key);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -030095
96 if (ir->keypressed && ir->keycode != keycode) {
97 ir->keypressed = 0;
98 ir_input_key_event(dev,ir);
99 }
100 if (!ir->keypressed) {
101 ir->ir_key = ir_key;
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300102 ir->keycode = keycode;
103 ir->keypressed = 1;
104 ir_input_key_event(dev,ir);
105 }
106}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300107EXPORT_SYMBOL_GPL(ir_input_keydown);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300108
109/* -------------------------------------------------------------------------- */
Trent Piephod67be612007-07-11 20:28:44 -0300110/* extract mask bits out of data and pack them into the result */
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300111u32 ir_extract_bits(u32 data, u32 mask)
112{
Trent Piephod67be612007-07-11 20:28:44 -0300113 u32 vbit = 1, value = 0;
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300114
Trent Piephod67be612007-07-11 20:28:44 -0300115 do {
116 if (mask&1) {
117 if (data&1)
118 value |= vbit;
119 vbit<<=1;
120 }
121 data>>=1;
122 } while (mask>>=1);
123
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300124 return value;
125}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300126EXPORT_SYMBOL_GPL(ir_extract_bits);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300127
128static int inline getbit(u32 *samples, int bit)
129{
130 return (samples[bit/32] & (1 << (31-(bit%32)))) ? 1 : 0;
131}
132
133/* sump raw samples for visual debugging ;) */
134int ir_dump_samples(u32 *samples, int count)
135{
136 int i, bit, start;
137
138 printk(KERN_DEBUG "ir samples: ");
139 start = 0;
140 for (i = 0; i < count * 32; i++) {
141 bit = getbit(samples,i);
142 if (bit)
143 start = 1;
144 if (0 == start)
145 continue;
146 printk("%s", bit ? "#" : "_");
147 }
148 printk("\n");
149 return 0;
150}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300151EXPORT_SYMBOL_GPL(ir_dump_samples);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300152
153/* decode raw samples, pulse distance coding used by NEC remotes */
154int ir_decode_pulsedistance(u32 *samples, int count, int low, int high)
155{
156 int i,last,bit,len;
157 u32 curBit;
158 u32 value;
159
160 /* find start burst */
161 for (i = len = 0; i < count * 32; i++) {
162 bit = getbit(samples,i);
163 if (bit) {
164 len++;
165 } else {
166 if (len >= 29)
167 break;
168 len = 0;
169 }
170 }
171
172 /* start burst to short */
173 if (len < 29)
174 return 0xffffffff;
175
176 /* find start silence */
177 for (len = 0; i < count * 32; i++) {
178 bit = getbit(samples,i);
179 if (bit) {
180 break;
181 } else {
182 len++;
183 }
184 }
185
186 /* silence to short */
187 if (len < 7)
188 return 0xffffffff;
189
190 /* go decoding */
191 len = 0;
192 last = 1;
193 value = 0; curBit = 1;
194 for (; i < count * 32; i++) {
195 bit = getbit(samples,i);
196 if (last) {
197 if(bit) {
198 continue;
199 } else {
200 len = 1;
201 }
202 } else {
203 if (bit) {
204 if (len > (low + high) /2)
205 value |= curBit;
206 curBit <<= 1;
207 if (curBit == 1)
208 break;
209 } else {
210 len++;
211 }
212 }
213 last = bit;
214 }
215
216 return value;
217}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300218EXPORT_SYMBOL_GPL(ir_decode_pulsedistance);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300219
220/* decode raw samples, biphase coding, used by rc5 for example */
221int ir_decode_biphase(u32 *samples, int count, int low, int high)
222{
223 int i,last,bit,len,flips;
224 u32 value;
225
226 /* find start bit (1) */
227 for (i = 0; i < 32; i++) {
228 bit = getbit(samples,i);
229 if (bit)
230 break;
231 }
232
233 /* go decoding */
234 len = 0;
235 flips = 0;
236 value = 1;
237 for (; i < count * 32; i++) {
238 if (len > high)
239 break;
240 if (flips > 1)
241 break;
242 last = bit;
243 bit = getbit(samples,i);
244 if (last == bit) {
245 len++;
246 continue;
247 }
248 if (len < low) {
249 len++;
250 flips++;
251 continue;
252 }
253 value <<= 1;
254 value |= bit;
255 flips = 0;
256 len = 1;
257 }
258 return value;
259}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300260EXPORT_SYMBOL_GPL(ir_decode_biphase);
Ricardo Cerqueirad15549a2006-03-03 12:13:42 -0300261
Hermann Pitton91607232006-12-07 21:45:28 -0300262/* RC5 decoding stuff, moved from bttv-input.c to share it with
263 * saa7134 */
264
265/* decode raw bit pattern to RC5 code */
Andy Walls1d23a002009-09-27 20:05:23 -0300266u32 ir_rc5_decode(unsigned int code)
Hermann Pitton91607232006-12-07 21:45:28 -0300267{
268 unsigned int org_code = code;
269 unsigned int pair;
270 unsigned int rc5 = 0;
271 int i;
272
273 for (i = 0; i < 14; ++i) {
274 pair = code & 0x3;
275 code >>= 2;
276
277 rc5 <<= 1;
278 switch (pair) {
279 case 0:
280 case 2:
281 break;
282 case 1:
283 rc5 |= 1;
284 break;
285 case 3:
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300286 IR_dprintk(1, "ir-common: ir_rc5_decode(%x) bad code\n", org_code);
Hermann Pitton91607232006-12-07 21:45:28 -0300287 return 0;
288 }
289 }
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300290 IR_dprintk(1, "ir-common: code=%x, rc5=%x, start=%x, toggle=%x, address=%x, "
Hermann Pitton91607232006-12-07 21:45:28 -0300291 "instr=%x\n", rc5, org_code, RC5_START(rc5),
292 RC5_TOGGLE(rc5), RC5_ADDR(rc5), RC5_INSTR(rc5));
293 return rc5;
294}
Andy Walls1d23a002009-09-27 20:05:23 -0300295EXPORT_SYMBOL_GPL(ir_rc5_decode);
Hermann Pitton91607232006-12-07 21:45:28 -0300296
297void ir_rc5_timer_end(unsigned long data)
298{
299 struct card_ir *ir = (struct card_ir *)data;
300 struct timeval tv;
301 unsigned long current_jiffies, timeout;
302 u32 gap;
303 u32 rc5 = 0;
304
305 /* get time */
306 current_jiffies = jiffies;
307 do_gettimeofday(&tv);
308
309 /* avoid overflow with gap >1s */
310 if (tv.tv_sec - ir->base_time.tv_sec > 1) {
311 gap = 200000;
312 } else {
313 gap = 1000000 * (tv.tv_sec - ir->base_time.tv_sec) +
314 tv.tv_usec - ir->base_time.tv_usec;
315 }
316
Vincent Penne726cf562007-03-25 11:58:23 -0300317 /* signal we're ready to start a new code */
318 ir->active = 0;
319
320 /* Allow some timer jitter (RC5 is ~24ms anyway so this is ok) */
Hermann Pitton91607232006-12-07 21:45:28 -0300321 if (gap < 28000) {
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300322 IR_dprintk(1, "ir-common: spurious timer_end\n");
Hermann Pitton91607232006-12-07 21:45:28 -0300323 return;
324 }
325
Hermann Pitton91607232006-12-07 21:45:28 -0300326 if (ir->last_bit < 20) {
327 /* ignore spurious codes (caused by light/other remotes) */
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300328 IR_dprintk(1, "ir-common: short code: %x\n", ir->code);
Hermann Pitton91607232006-12-07 21:45:28 -0300329 } else {
330 ir->code = (ir->code << ir->shift_by) | 1;
331 rc5 = ir_rc5_decode(ir->code);
332
333 /* two start bits? */
334 if (RC5_START(rc5) != ir->start) {
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300335 IR_dprintk(1, "ir-common: rc5 start bits invalid: %u\n", RC5_START(rc5));
Hermann Pitton91607232006-12-07 21:45:28 -0300336
337 /* right address? */
338 } else if (RC5_ADDR(rc5) == ir->addr) {
339 u32 toggle = RC5_TOGGLE(rc5);
340 u32 instr = RC5_INSTR(rc5);
341
342 /* Good code, decide if repeat/repress */
343 if (toggle != RC5_TOGGLE(ir->last_rc5) ||
344 instr != RC5_INSTR(ir->last_rc5)) {
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300345 IR_dprintk(1, "ir-common: instruction %x, toggle %x\n", instr,
Hermann Pitton91607232006-12-07 21:45:28 -0300346 toggle);
347 ir_input_nokey(ir->dev, &ir->ir);
Mauro Carvalho Chehab8573b742009-11-27 22:40:22 -0300348 ir_input_keydown(ir->dev, &ir->ir, instr);
Hermann Pitton91607232006-12-07 21:45:28 -0300349 }
350
351 /* Set/reset key-up timer */
Mauro Carvalho Chehabf7518bd2007-07-17 16:27:30 -0300352 timeout = current_jiffies +
353 msecs_to_jiffies(ir->rc5_key_timeout);
Hermann Pitton91607232006-12-07 21:45:28 -0300354 mod_timer(&ir->timer_keyup, timeout);
355
356 /* Save code for repeat test */
357 ir->last_rc5 = rc5;
358 }
359 }
360}
Mauro Carvalho Chehab4db16db2008-07-17 22:28:56 -0300361EXPORT_SYMBOL_GPL(ir_rc5_timer_end);
Hermann Pitton91607232006-12-07 21:45:28 -0300362
363void ir_rc5_timer_keyup(unsigned long data)
364{
365 struct card_ir *ir = (struct card_ir *)data;
366
Mauro Carvalho Chehab4e892172009-11-27 21:54:41 -0300367 IR_dprintk(1, "ir-common: key released\n");
Hermann Pitton91607232006-12-07 21:45:28 -0300368 ir_input_nokey(ir->dev, &ir->ir);
369}
Hermann Pitton91607232006-12-07 21:45:28 -0300370EXPORT_SYMBOL_GPL(ir_rc5_timer_keyup);